|
enum | Fill { NOFILL = 0
, FILL_0
, FILL_1
} |
|
|
| Kalloc (mword virt_begin, mword virt_end) |
|
void * | alloc (unsigned size) |
|
void * | alloc_page (unsigned count, Fill fill=NOFILL) |
| Allocate count virtually contiguous pages and optionally fill them with 0x00 or 0xFF. More...
|
|
void | free_page (void *) |
| Free a page previously allocated with Kalloc::alloc_page(). More...
|
|
|
static void * | phys2virt (mword) |
| Returns the virtual address that can be used to access memory with physical address phys . More...
|
|
static mword | virt2phys (void *) |
| Return the physical address that is mapped from the virtual address virt (opposite of phys2virt ).
|
|
◆ alloc_page()
void * Kalloc::alloc_page |
( |
unsigned |
count, |
|
|
Fill |
fill = NOFILL |
|
) |
| |
Allocate count virtually contiguous pages and optionally fill them with 0x00 or 0xFF.
- Returns
- Virtual address of the first allocated page
48 unsigned found = 0, first = 0;
50 for (
unsigned i = 0; i < ((end - begin) >> PAGE_BITS) && found < count; i++) {
51 if (page_bitmap[i >> 5] == 0xffffffff) {
56 if (!is_page_allocated(i)) {
67 for (
unsigned i = 0; i < count; i++)
68 mark_page_allocated(first + i,
true);
70 void * p =
reinterpret_cast<void *
>(begin + first * PAGE_SIZE);
72 memset (p, fill == FILL_0 ? 0 : ~0, count * PAGE_SIZE);
◆ free_page()
void Kalloc::free_page |
( |
void * |
virt | ) |
|
Free a page previously allocated with Kalloc::alloc_page().
- Parameters
-
[in] | virt | Virtual address of the page to free. |
84 mword p =
reinterpret_cast<mword
>(virt);
85 if (p < begin || p >= end || (p & (PAGE_SIZE - 1)) != 0 ||
86 !is_page_allocated((p - begin) >> PAGE_BITS))
87 panic (
"kernel: attempt to free invalid page\n");
89 mark_page_allocated((p - begin) >> PAGE_BITS,
false);
◆ phys2virt()
void * Kalloc::phys2virt |
( |
mword |
phys | ) |
|
|
static |
Returns the virtual address that can be used to access memory with physical address phys
.
Even kernel can only access virtual memory, it cannot access physical memory directly. However, CPU and bootloader work with physical addresses, so we sometimes need to access memory via its physical addresses.
To resolve this, part of the physical memory is mapped 1:1 to a sequential block of virtual memory addresses at a known offset OFFSET
, so that the physical address X is accessible at the virtual address OFFSET + X. This mapping is configured during kernel startup at kern/src/start.S
(section SETUP BOOT PAGE TABLE
).
94 mword virt = phys +
reinterpret_cast<mword
>(&OFFSET);
95 return reinterpret_cast<void*
>(virt);
The documentation for this class was generated from the following files: