Skip to content

Pagination

Sukant Pal edited this page Feb 24, 2018 · 12 revisions

Memory Pagination

Silcos kernel uses the memory protection and organization technique known as paging. In this technique, each virtual address is mapped to a physical address in blocks known as pages. These pages can be of various sizes - 4KB and 2MB (IA32, PAE). The blocks in physical memory are known as page-frames and in virtual memory they are called only pages.

Kernel Memory

Silcos has a "build" time map of the locations of various permanent data structures in kernel memory. This map is declared through sets of macros in Memory/KMemorySpace.h. On changing the values in this file & re-building the kernel, the data structures will be mapped to different offsets in kernel memory. The data structures include -

  1. Platform Information

  2. ACPI Tables & Objects

  3. CPU Status Table

  4. Dynamically-allocable Memory

Platform Information

This is not being used currently and is reserved. It is intended for use to store architectural information - x2APIC enabled, TSC enabled, etc.

ACPI Tables & Objects

This memory is intended for use by the kernel's ACPI subsystem. Later it will be shared by drivers (when the kernel finishes). It is used for mapping ACPI tables in non-cacheable memory currently.

CPU Status Table

This region of memory is intended for storing the struct Processor and related data structures which are usually accessed only by the owner-CPU. Each CPU gets a 32-KB block in kernel-memory which can be accessed by the GetProcessorById and GetIRQTableById macros included in HardwareAbstraction/Processor.h.

Dynamically-allocable Memory

This memory is used for allocating objects & module-segments (plus some other minute purposes). The size of dynamic-memory is decided during boot depending on the amount of physical memory available. The KMemoryManager was built to manage this portion of kernel-memory.

Kernel Paging

The kernel supports various functions to control memory paging in a cross-platform manner. The functions `EnsureUsability, EnsureMapping, EnsureFaulty** operate on single pages in memory.

In-kernel Pager

The KernelHost is supposed to expose an interface for managing page-tables and mapping ranges of virtual addresses to physical addresses in a platform-independent manner. Note that this responsibility does not come under the jurisdiction of the HAL as it is considered primitive and therefore kernel-initialization directly depends on pagination support. This is done through the header Memory/Pager.h in which the struct CONTEXT is given and various utility function prototypes to manipulate the object.

Function: EnsureMapping

Module: KernelHost

File: IA32/Boot/Pager.cpp

Prototype:

decl_c void EnsureMapping(unsigned long vaddr, PADDRESS paddr, CONTEXT *cxt, unsigned long frFlags, PAGE_ATTRIBUTES pgAttr);

Summary:

This function maps the given physical address paddr to the virtual address vaddr that resides in the address space represented by the given context object. If the virtual address is kernel-owned, then null is also a valid argument for the context object.

The caller can set the flags by which the page-table is to be allocated from the page-frame manager using allocation flags. This can be necessary when the kernel has not fully initialization or if the call is coming from a interrupt-handler.

Parameters:

  1. vaddr - the virtual address of the page that needs to be mapped. This need not be 4-KB aligned as it will be automatically by this function.

  2. paddr - the physical address to which the page is to be mapped. This should be 4-KB aligned otherwise the flags for mapping may be corrupted.

  3. cxt - address in which the virtual-address resides.

  4. frFlags - allocation-flags for the page-frame allocator; used if the page-table for mapping was not already present in the page-directory. This can be used to turn frame-caching off or keeping interrupts off.

  5. pgAttr - attributes of mapping. (see page-transalator)

Function: EnsureUsability

Module: KernelHost

File: IA32/Paging/Pager.cpp

Prototype:

decl_c void EnsureUsability(unsigned long address, CONTEXT *cxt, unsigned long frFrames, PAGE_ATTRIBUTES pgAttr);

Summary:

This function ensures that the page at the given virtual address address is mapped to a valid page-frame by allocating one before mapping it. In other words, it ensures that the page is usable and will not cause a page-fault on reading (and writing unless the PageReadWrite attribute wasn't passed).

Parameters:

  1. address - the virtual address of the page to be used. It need not be 4-KB aligned as it will be automatically.

  2. cxt - the context in which the virtual address resides. Note that if the address is kernel-owned, then null should be passed as the argument.

  3. frFlags - allocation flags for getting a page-frame; these will also be used in case the page-table also needs allocation.

  4. pgAttr - attributes for mapping the page.

Clone this wiki locally