Files
jsix/src/kernel/memory.h.cog
Justin C. Miller c1d9b35e7c [bootproto] Create new bootproto lib
This is a rather large commit that is widely focused on cleaning things
out of the 'junk drawer' that is src/include. Most notably, several
things that were put in there because they needed somewhere where both
the kernel, boot, and init could read them have been moved to a new lib,
'bootproto'.

- Moved kernel_args.h and init_args.h to bootproto as kernel.h and
  init.h, respectively.

- Moved counted.h and pointer_manipulation.h into util, renaming the
  latter to util/pointers.h.

- Created a new src/include/arch for very arch-dependent definitions,
  and moved some kernel_memory.h constants like frame size, page table
  entry count, etc to arch/amd64/memory.h. Also created arch/memory.h
  which detects platform and includes the former.

- Got rid of kernel_memory.h entirely in favor of a new, cog-based
  approach. The new definitions/memory_layout.csv lists memory regions
  in descending order from the top of memory, their sizes, and whether
  they are shared outside the kernel (ie, boot needs to know them). The
  new header bootproto/memory.h exposes the addresses of the shared
  regions, while the kernel's memory.h gains the start and size of all
  the regions. Also renamed the badly-named page-offset area the linear
  area.

- The python build scripts got a few new features: the ability to parse
  the csv mentioned above in a new memory.py module; the ability to add
  dependencies to existing source files (The list of files that I had to
  pull out of the main list just to add them with the dependency on
  memory.h was getting too large. So I put them back into the sources
  list, and added the dependency post-hoc.); and the ability to
  reference 'source_root', 'build_root', and 'module_root' variables in
  .module files.

- Some utility functions that were in the kernel's memory.h got moved to
  util/pointers.h and util/misc.h, and misc.h's byteswap was renamed
  byteswap32 to be more specific.
2022-01-03 17:44:13 -08:00

80 lines
2.3 KiB
C++

#pragma once
// vim: ft=cpp
/// \file bootproto/memory.h
/// Import memory layout constants necessary for boot
#include <stddef.h>
#include <stdint.h>
#include <arch/memory.h>
void * operator new (size_t, void *p) noexcept;
/// Allocate from the default allocator.
/// \arg size The size in bytes requested
/// \returns A pointer to the newly allocated memory,
/// or nullptr on error
void * kalloc(size_t size);
/// Free memory allocated by `kalloc`.
/// \arg p Pointer that was returned from a `kalloc` call
void kfree(void *p);
/// Kernel space virtual memory layout
namespace mem {
using arch::frame_size;
using arch::kernel_offset;
/// Max number of pages for a kernel stack
constexpr unsigned kernel_stack_pages = 2;
/// Max number of pages for a kernel buffer
constexpr unsigned kernel_buffer_pages = 16;
/*[[[cog code generation
from os.path import join
from memory import Layout
layout = Layout(join(definitions_path, "memory_layout.csv"))
l = max([len(r.name) for r in layout.regions])
for region in layout.regions:
cog.outl(f"constexpr size_t {region.name:>{l}}_size = {region.size:#14x};")
cog.outl("")
for region in layout.regions:
cog.outl(f"constexpr uintptr_t {region.name:>{l}}_offset = {region.start:#x};")
]]]*/
///[[[end]]]
/// Helper to determine if a physical address can be accessed
/// through the linear_offset area.
constexpr bool linear_mappable(uintptr_t a) { return (a & linear_offset) == 0; }
/// Convert a physical address to a virtual one (in the linear-mapped area)
template <typename T> T * to_virtual(uintptr_t a) {
return reinterpret_cast<T*>(a|linear_offset);
}
/// Convert a physical address to a virtual one (in the linear-mapped area)
template <typename T> T * to_virtual(T *p) {
return to_virtual<T>(reinterpret_cast<uintptr_t>(p));
}
/// Get the number of pages needed for a given number of bytes.
/// \arg bytes The number of bytes desired
/// \returns The number of pages needed to contain the desired bytes
constexpr size_t page_count(size_t bytes) { return ((bytes - 1) >> 12) + 1; }
/// Get the given address, aligned to the next lowest page
constexpr uintptr_t page_align_down(uintptr_t a) { return a & ~(frame_size-1); }
/// Get the given address, aligned to the next page
constexpr uintptr_t page_align_up(uintptr_t a) { return page_align_down(a-1) + frame_size; }
} // namespace mem