IBNOS
Classes | Macros | Functions | Variables
Physical memory

Classes

struct  physMemExtraInfo
 

Macros

#define PAGE_SIZE   0x1000
 
#define PAGE_MASK   0xFFF
 
#define PAGE_BITS   12
 
#define PAGE_COUNT   0x100000
 
#define PHYSMEM_FREE   0
 
#define PHYSMEM_RESERVED   1
 
#define PHYSMEMEXTRA_SIZE   0x1000 /* must match PAGE_SIZE for now */
 
#define PHYSMEMEXTRA_MASK   0x3FF
 
#define PHYSMEMEXTRA_BITS   10
 
#define PHYSMEMEXTRA_COUNT   0x400
 
#define LINKER_KERNEL_BEGIN   ((uint32_t)&__kernelBegin)
 
#define LINKER_KERNEL_SIZE   ((uint32_t)&__kernelEnd - (uint32_t)&__kernelBegin)
 

Functions

void physMemInit (multiboot_info_t *bootInfo)
 Initializes the physical memory management. More...
 
uint32_t physMemRAMSize ()
 Query RAM size. More...
 
uint32_t physMemUsableMemory ()
 Query usable RAM size. More...
 
void physMemProtectBootEntry (uint32_t addr, uint32_t length)
 Marks all pages within a memory range as reserved and adds them to the boot map. More...
 
void physMemClearMemoryBits (bool reserved)
 Marks the full memory range as reserved or free. More...
 
void physMemReserveMemory (uint32_t addr, uint32_t length)
 Marks all pages within a memory range as reserved. More...
 
void physMemFreeMemory (uint32_t addr, uint32_t length)
 Marks all pages fully contained within the memory range as free. More...
 
void physMemSetMemoryBits (uint32_t startIndex, uint32_t length, bool reserved)
 Marks a specific range of pages as reserved or free. More...
 
uint32_t physMemAllocPage (bool lowmem)
 Allocates a page of physical memory. More...
 
uint32_t physMemReleasePage (uint32_t index)
 Releases a page of physical memory. More...
 
uint32_t physMemAddRefPage (uint32_t index)
 Increment the refcounter of a physical page. More...
 
uint32_t physMemMarkUnpageable (uint32_t index)
 Marks a physical page as unpageable. More...
 
bool physMemIsLastRef (uint32_t index)
 Checks if a physical page is only referenced exactly one time. More...
 
void physMemPageOut (UNUSED uint32_t length)
 Pages out some memory to the hard drive. More...
 
uint32_t physMemPageIn (UNUSED uint32_t hdd_index)
 Pages in some data from the hard drive. More...
 
void physMemDumpMemInfo ()
 Dumps information about the physical memory usage. More...
 
struct physMemExtraInfo __attribute__ ((packed))
 
uint32_t __getCR0 ()
 
 asm (".text\align 4\ "__getCR0:\" " movl %cr0, %eax\" " ret\")
 

Variables

uint32_t ramSize = 0
 
uint32_t ramUsableSize = 0
 
uint32_t __kernelBegin
 
uint32_t __kernelEnd
 

Detailed Description

Macro Definition Documentation

◆ LINKER_KERNEL_BEGIN

#define LINKER_KERNEL_BEGIN   ((uint32_t)&__kernelBegin)

Definition at line 69 of file physmem.c.

◆ LINKER_KERNEL_SIZE

#define LINKER_KERNEL_SIZE   ((uint32_t)&__kernelEnd - (uint32_t)&__kernelBegin)

Definition at line 70 of file physmem.c.

◆ PAGE_BITS

#define PAGE_BITS   12

Definition at line 37 of file physmem.h.

◆ PAGE_COUNT

#define PAGE_COUNT   0x100000

Definition at line 38 of file physmem.h.

◆ PAGE_MASK

#define PAGE_MASK   0xFFF

Definition at line 36 of file physmem.h.

◆ PAGE_SIZE

#define PAGE_SIZE   0x1000

Definition at line 35 of file physmem.h.

◆ PHYSMEM_FREE

#define PHYSMEM_FREE   0

Definition at line 48 of file physmem.h.

◆ PHYSMEM_RESERVED

#define PHYSMEM_RESERVED   1

Definition at line 49 of file physmem.h.

◆ PHYSMEMEXTRA_BITS

#define PHYSMEMEXTRA_BITS   10

Definition at line 44 of file physmem.c.

◆ PHYSMEMEXTRA_COUNT

#define PHYSMEMEXTRA_COUNT   0x400

Definition at line 45 of file physmem.c.

◆ PHYSMEMEXTRA_MASK

#define PHYSMEMEXTRA_MASK   0x3FF

Definition at line 43 of file physmem.c.

◆ PHYSMEMEXTRA_SIZE

#define PHYSMEMEXTRA_SIZE   0x1000 /* must match PAGE_SIZE for now */

Definition at line 42 of file physmem.c.

Function Documentation

◆ __attribute__()

struct physMemExtraInfo __attribute__ ( (packed)  )

◆ __getCR0()

uint32_t __getCR0 ( )

◆ asm()

asm ( ".text\ign 4\__getCR0:\ " movl %  cr0,
%eax\" " ret\"   
)

◆ physMemAddRefPage()

uint32_t physMemAddRefPage ( uint32_t  index)

Increment the refcounter of a physical page.

Parameters
indexIndex of the physical page
Returns
Index of the physical page

Definition at line 460 of file physmem.c.

◆ physMemAllocPage()

uint32_t physMemAllocPage ( bool  lowmem)

Allocates a page of physical memory.

This command searches for an unused page in the physical memory bitmap and afterwards marks the specific page as reserved. If there is no physical memory left then the algorithm tries to page out some memory to the hard drive. If this fails then a system failure is triggered.

Parameters
lowmemIf true then the search also includes the physical memory area below 1MB
Returns
Index of the physical page which was allocated

Definition at line 383 of file physmem.c.

◆ physMemClearMemoryBits()

void physMemClearMemoryBits ( bool  reserved)

Marks the full memory range as reserved or free.

Parameters
reservedIf true the whole memory is marked as reserved, otherwise as free.

Definition at line 244 of file physmem.c.

◆ physMemDumpMemInfo()

void physMemDumpMemInfo ( )

Dumps information about the physical memory usage.

Definition at line 544 of file physmem.c.

◆ physMemFreeMemory()

void physMemFreeMemory ( uint32_t  addr,
uint32_t  length 
)

Marks all pages fully contained within the memory range as free.

Parameters
addrPhysical base address of the memory block
lengthLength of the memory block in bytes

Definition at line 299 of file physmem.c.

◆ physMemInit()

void physMemInit ( multiboot_info_t bootInfo)

Initializes the physical memory management.

In order to implement physMemAllocPage() the operating system has to keep track of all used and unused physical pages. Internally this module works using a huge bitmap, where a 1 represents a used page, and 0 an unused page. This function initializes all the structures using the memory layout information filled by the GRUB boot loader.

Parameters
bootInfoBootinfo structure filled by the GRUB boot loader

Definition at line 116 of file physmem.c.

◆ physMemIsLastRef()

bool physMemIsLastRef ( uint32_t  index)

Checks if a physical page is only referenced exactly one time.

Looks up the refcount of a specific page, and returns true if the refcount is exactly one.

Parameters
indexIndex of the physical page
Returns
True if the page has a refcount of exactly one, otherwise false

Definition at line 510 of file physmem.c.

◆ physMemMarkUnpageable()

uint32_t physMemMarkUnpageable ( uint32_t  index)

Marks a physical page as unpageable.

Marks a physical page as unpageable, to ensure that it is never paged out to the hard drive. This is necessary for some kernel related pages, like GDT or interrupt handler functions.

Parameters
indexIndex of the physical page
Returns
Index of the physical page

Definition at line 486 of file physmem.c.

◆ physMemPageIn()

uint32_t physMemPageIn ( UNUSED uint32_t  hdd_index)

Pages in some data from the hard drive.

Allocates a physical page (which possibily pages out other stuff, if the physical memory is exhausted). Afterwards loads the data from the hard drive (including permission bits and other stuff which was modified).

Parameters
hdd_indexIndex to a page on the hard drive
Returns
Index of the physical page

Definition at line 535 of file physmem.c.

◆ physMemPageOut()

void physMemPageOut ( UNUSED uint32_t  length)

Pages out some memory to the hard drive.

Parameters
lengthNumber of pages to page out

Definition at line 521 of file physmem.c.

◆ physMemProtectBootEntry()

void physMemProtectBootEntry ( uint32_t  addr,
uint32_t  length 
)

Marks all pages within a memory range as reserved and adds them to the boot map.

Parameters
addrPhysical base address of the memory block
lengthLength of the memory block in bytes

Definition at line 259 of file physmem.c.

◆ physMemRAMSize()

uint32_t physMemRAMSize ( )

Query RAM size.

Returns the amount of available physical memory. This information is only available after the physical memory management was initialized by calling physMemInit().

Returns
Available RAM size in bytes

Definition at line 219 of file physmem.c.

◆ physMemReleasePage()

uint32_t physMemReleasePage ( uint32_t  index)

Releases a page of physical memory.

Deallocates a page of physical memory, or if the page has a refcount of greater than 1, then decrements the refcount. The refcounting menchanism ensures that the memory is not released before all processes containing the same physical memory page have released it again.

Parameters
indexIndex of the physical page to deallocate
Returns
New value of the refcounter (0 = the page was deallocated)

Definition at line 426 of file physmem.c.

◆ physMemReserveMemory()

void physMemReserveMemory ( uint32_t  addr,
uint32_t  length 
)

Marks all pages within a memory range as reserved.

Parameters
addrPhysical base address of the memory block
lengthLength of the memory block in bytes

Definition at line 280 of file physmem.c.

◆ physMemSetMemoryBits()

void physMemSetMemoryBits ( uint32_t  startIndex,
uint32_t  length,
bool  reserved 
)

Marks a specific range of pages as reserved or free.

Parameters
startIndexPhysical index of the first page which should be modified
lengthNumber of pages which should be changed
reservedIf true the pages are marked as reserved, otherwise as free.

Definition at line 319 of file physmem.c.

◆ physMemUsableMemory()

uint32_t physMemUsableMemory ( )

Query usable RAM size.

Returns the amount of useable physical memory. This is similar to physMemRAMSize(), except that all unpageable and kernel related pages are subtracted. This information is only available after the physical memory management was initialized.

Returns
Useable RAM size in bytes

Definition at line 232 of file physmem.c.

Variable Documentation

◆ __kernelBegin

uint32_t __kernelBegin

◆ __kernelEnd

uint32_t __kernelEnd

◆ ramSize

uint32_t ramSize = 0

Definition at line 39 of file physmem.c.

◆ ramUsableSize

uint32_t ramUsableSize = 0

Definition at line 40 of file physmem.c.