|
@@ -4,7 +4,7 @@
|
|
|
<meta charset="utf-8">
|
|
<meta charset="utf-8">
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
<title>Memory | NebOS Architecture</title>
|
|
<title>Memory | NebOS Architecture</title>
|
|
|
-<meta name="description" content="NebOS memory architecture: fixed low-memory addresses, E820 parsing, bitmap frame allocator, identity paging, MTRR write-combining, user arenas and boot-pack lifetime.">
|
|
|
|
|
|
|
+<meta name="description" content="NebOS memory architecture: fixed low-memory addresses, E820 parsing, bitmap frame allocator with lowmem/highmem zones, identity paging, kmap, MTRR write-combining, user arenas and boot-pack lifetime.">
|
|
|
<link rel="icon" href="favicon.svg" type="image/svg+xml">
|
|
<link rel="icon" href="favicon.svg" type="image/svg+xml">
|
|
|
<link rel="stylesheet" href="style.css">
|
|
<link rel="stylesheet" href="style.css">
|
|
|
</head>
|
|
</head>
|
|
@@ -35,13 +35,15 @@
|
|
|
<p class="kicker">Memory & paging</p>
|
|
<p class="kicker">Memory & paging</p>
|
|
|
<h1>A fixed ladder and a 4 KB frame allocator.</h1>
|
|
<h1>A fixed ladder and a 4 KB frame allocator.</h1>
|
|
|
<p class="lede">
|
|
<p class="lede">
|
|
|
- NebOS identity-maps physical RAM with 4 MB pages and hands out
|
|
|
|
|
- 4 KB frames from a bitmap. A small set of low-memory addresses is
|
|
|
|
|
|
|
+ NebOS identity-maps the first 1 GB of RAM (lowmem) with 4 MB pages
|
|
|
|
|
+ and hands out 4 KB frames from a bitmap; RAM from 1 GB up to 4 GB
|
|
|
|
|
+ (highmem) backs ring-3 pages and is reached by the kernel through
|
|
|
|
|
+ per-CPU <code>kmap</code> slots. A small set of low-memory addresses is
|
|
|
fixed by the boot path and the ABI; everything else — process stacks,
|
|
fixed by the boot path and the ABI; everything else — process stacks,
|
|
|
window canvases, the ring-3 heap and the rootfs copy — is dynamic.
|
|
window canvases, the ring-3 heap and the rootfs copy — is dynamic.
|
|
|
</p>
|
|
</p>
|
|
|
<div class="meta">
|
|
<div class="meta">
|
|
|
- <span class="pill">1 GB physical ceiling</span>
|
|
|
|
|
|
|
+ <span class="pill">1 GB lowmem · 4 GB highmem</span>
|
|
|
<span class="pill">4 KB frames</span>
|
|
<span class="pill">4 KB frames</span>
|
|
|
<span class="pill">4 MB PSE pages</span>
|
|
<span class="pill">4 MB PSE pages</span>
|
|
|
<span class="pill">identity map</span>
|
|
<span class="pill">identity map</span>
|
|
@@ -96,12 +98,13 @@
|
|
|
<text class="svg-tiny" x="40" y="150">The 8 MB reserve protects the shadow buffer; the allocator starts at 0x900000.</text>
|
|
<text class="svg-tiny" x="40" y="150">The 8 MB reserve protects the shadow buffer; the allocator starts at 0x900000.</text>
|
|
|
</svg>
|
|
</svg>
|
|
|
<figcaption>
|
|
<figcaption>
|
|
|
- <strong>1 GB is a hard ceiling</strong> (<code>MM_MAX_PHYS</code>,
|
|
|
|
|
|
|
+ <strong>Lowmem ends at 1 GB</strong> (<code>MM_MAX_PHYS</code>,
|
|
|
<code>include/phys.h</code>). The identity map covers
|
|
<code>include/phys.h</code>). The identity map covers
|
|
|
<code>[0, 0x40000000)</code> and ring-3 space starts at
|
|
<code>[0, 0x40000000)</code> and ring-3 space starts at
|
|
|
- <code>USER_BASE</code> = <code>0x40000000</code>, so RAM above 1 GB is
|
|
|
|
|
- ignored until a highmem zone exists; <code>paging.c</code>
|
|
|
|
|
- statically asserts the two never overlap. The VGA shadow reserve is conservative: the
|
|
|
|
|
|
|
+ <code>USER_BASE</code> = <code>0x40000000</code>; <code>paging.c</code>
|
|
|
|
|
+ statically asserts the two never overlap. RAM above 1 GB (up to
|
|
|
|
|
+ <code>MM_HIGH_LIMIT</code>, 4 GB) is highmem: tracked by the same
|
|
|
|
|
+ bitmap, never identity-mapped, used only for ring-3 pages. The VGA shadow reserve is conservative: the
|
|
|
actual buffer is smaller, but the gap keeps a future resolution bump
|
|
actual buffer is smaller, but the gap keeps a future resolution bump
|
|
|
from colliding with allocated pages.
|
|
from colliding with allocated pages.
|
|
|
</figcaption>
|
|
</figcaption>
|
|
@@ -138,6 +141,7 @@
|
|
|
<tr><td><code>0x900000</code></td><td>Boot pack (written directly by the legacy stage), then the frame bitmap right after it; dynamic stacks and kexec staging</td><td>The pack is freed after <code>ramfs_init</code> takes its copy; the bitmap stays</td></tr>
|
|
<tr><td><code>0x900000</code></td><td>Boot pack (written directly by the legacy stage), then the frame bitmap right after it; dynamic stacks and kexec staging</td><td>The pack is freed after <code>ramfs_init</code> takes its copy; the bitmap stays</td></tr>
|
|
|
<tr><td><code>0x100000</code></td><td>VGA shadow buffer base</td><td><code>VGA_SHADOW_BASE</code>; 8 MB reserved</td></tr>
|
|
<tr><td><code>0x100000</code></td><td>VGA shadow buffer base</td><td><code>VGA_SHADOW_BASE</code>; 8 MB reserved</td></tr>
|
|
|
<tr><td><code>0x40000000</code></td><td>Ring-3 ELF link/load address</td><td><code>USER_BASE</code>, 4 MB window up to <code>0x40400000</code></td></tr>
|
|
<tr><td><code>0x40000000</code></td><td>Ring-3 ELF link/load address</td><td><code>USER_BASE</code>, 4 MB window up to <code>0x40400000</code></td></tr>
|
|
|
|
|
+ <tr><td><code>0x4FC00000</code></td><td>kmap window (virtual)</td><td><code>MM_KMAP_VA</code>; supervisor page table shared by every directory, 4 slots per CPU</td></tr>
|
|
|
<tr><td><code>0x50000000</code></td><td>Ring-3 window canvas</td><td><code>USER_WIN_BASE</code>, 4 MB slot</td></tr>
|
|
<tr><td><code>0x50000000</code></td><td>Ring-3 window canvas</td><td><code>USER_WIN_BASE</code>, 4 MB slot</td></tr>
|
|
|
<tr><td><code>0x58000000</code></td><td>WM shadow mapping</td><td><code>WM_SHADOW_VA</code>; ring-3 WM only</td></tr>
|
|
<tr><td><code>0x58000000</code></td><td>WM shadow mapping</td><td><code>WM_SHADOW_VA</code>; ring-3 WM only</td></tr>
|
|
|
<tr><td><code>0x59000000</code></td><td>WM content slots</td><td><code>WM_CONTENT_VA_BASE</code> + index × 4 MB (<code>WM_CONTENT_SLOT</code>)</td></tr>
|
|
<tr><td><code>0x59000000</code></td><td>WM content slots</td><td><code>WM_CONTENT_VA_BASE</code> + index × 4 MB (<code>WM_CONTENT_SLOT</code>)</td></tr>
|
|
@@ -158,7 +162,8 @@
|
|
|
<code>EFER.LME</code>.
|
|
<code>EFER.LME</code>.
|
|
|
</p>
|
|
</p>
|
|
|
<ul>
|
|
<ul>
|
|
|
- <li><strong>Identity map.</strong> Physical and virtual addresses are equal. Page-directory entries are shared with every process.</li>
|
|
|
|
|
|
|
+ <li><strong>Identity map.</strong> Physical and virtual addresses are equal below 1 GB (lowmem). Page-directory entries are shared with every process.</li>
|
|
|
|
|
+ <li><strong>kmap window.</strong> The 4 MB slot at <code>MM_KMAP_VA</code> (<code>0x4FC00000</code>, in the unused gap below <code>USER_WIN_BASE</code>) has one supervisor page table that every process directory shares. <code>kmap(phys)</code> returns the identity address for lowmem, and for a highmem frame takes this CPU's next slot (4 per CPU, nesting LIFO) with preemption disabled until <code>kunmap()</code>; only the local TLB entry is flushed, because no other CPU uses that slot. <code>paging_map_ram</code>, <code>paging_map_mmio_cr3</code> and the LFB mapping refuse to overwrite that slot. It is not the last lowmem slot, because firmware puts the ACPI tables at the top of RAM, and not the top of the address space, where a VBE LFB may sit.</li>
|
|
|
<li><strong>Kernel stacks mapped everywhere.</strong> Each process gets a page directory cloned from the kernel identity map, so CR3 can switch while still running on the old kernel stack.</li>
|
|
<li><strong>Kernel stacks mapped everywhere.</strong> Each process gets a page directory cloned from the kernel identity map, so CR3 can switch while still running on the old kernel stack.</li>
|
|
|
<li><strong>LFB is uncacheable by default.</strong> If <code>mtrr_set_wc()</code> successfully covers the framebuffer, the LFB pages are mapped cacheable so the shadow-buffer flush can write-combine.</li>
|
|
<li><strong>LFB is uncacheable by default.</strong> If <code>mtrr_set_wc()</code> successfully covers the framebuffer, the LFB pages are mapped cacheable so the shadow-buffer flush can write-combine.</li>
|
|
|
<li><strong>No process isolation.</strong> There is one address space with per-process page directories only for mappings; a wild ring-0 or ring-3 pointer can reach any mapped physical page.</li>
|
|
<li><strong>No process isolation.</strong> There is one address space with per-process page directories only for mappings; a wild ring-0 or ring-3 pointer can reach any mapped physical page.</li>
|
|
@@ -183,26 +188,30 @@
|
|
|
<h2>Frame allocator</h2>
|
|
<h2>Frame allocator</h2>
|
|
|
<p>
|
|
<p>
|
|
|
<span class="file">mm.c</span> keeps one bit per 4 KB page for the
|
|
<span class="file">mm.c</span> keeps one bit per 4 KB page for the
|
|
|
- RAM below the 1 GB ceiling: the bitmap is sized to the highest E820 RAM
|
|
|
|
|
- address (32 KB at 1 GB) and is not part of the kernel image.
|
|
|
|
|
- <code>mm_init</code> places it in the first E820 RAM run at or above
|
|
|
|
|
|
|
+ RAM below 4 GB: the bitmap is sized to the highest E820 RAM
|
|
|
|
|
+ address (32 KB at 1 GB, 96 KB at 3 GB) and is not part of the kernel
|
|
|
|
|
+ image. <code>mm_init</code> places it in the first lowmem E820 RAM run at or above
|
|
|
<code>0x900000</code> that avoids the boot rootfs pack and the LFB, and
|
|
<code>0x900000</code> that avoids the boot rootfs pack and the LFB, and
|
|
|
marks its own pages used (legacy boots put it right after the pack;
|
|
marks its own pages used (legacy boots put it right after the pack;
|
|
|
a 16 MB static fallback covers a missing E820 map). The allocators
|
|
a 16 MB static fallback covers a missing E820 map). The allocators
|
|
|
disable interrupts around the bitmap and hand out the lowest free
|
|
disable interrupts around the bitmap and hand out the lowest free
|
|
|
- frames: a <code>first_free</code> word hint skips the full low part,
|
|
|
|
|
- <code>mm_alloc_page()</code> finds the bit with <code>bsf</code>, and
|
|
|
|
|
|
|
+ frames of a zone: each zone has a first-free word hint that skips its
|
|
|
|
|
+ full low part, single pages are found with <code>bsf</code>, and
|
|
|
<code>mm_alloc_pages(n)</code> steps over full and empty words 32 pages
|
|
<code>mm_alloc_pages(n)</code> steps over full and empty words 32 pages
|
|
|
- at a time. The allocator returns:
|
|
|
|
|
|
|
+ at a time. Everything the kernel dereferences by physical address
|
|
|
|
|
+ (page tables, DMA buffers, canvases, kernel stacks, <code>kmalloc</code>)
|
|
|
|
|
+ comes from lowmem; ring-3 program, stack and heap frames prefer
|
|
|
|
|
+ highmem. The allocator returns:
|
|
|
</p>
|
|
</p>
|
|
|
<ul>
|
|
<ul>
|
|
|
- <li><code>mm_alloc_page()</code> — one zero-address-ambiguous page (physical address 0 means failure).</li>
|
|
|
|
|
- <li><code>mm_alloc_pages(n)</code> — <code>n</code> contiguous pages.</li>
|
|
|
|
|
|
|
+ <li><code>mm_alloc_page()</code> — one lowmem page (physical address 0 means failure).</li>
|
|
|
|
|
+ <li><code>mm_alloc_user_page()</code> — one frame for a ring-3 mapping: highmem first, lowmem when highmem is exhausted. Touch it only through <code>kmap()</code> or the user mapping (<span class="file">elf.c</span>, the user stack in <span class="file">process.c</span>, the heap in <span class="file">sys_mem.c</span>).</li>
|
|
|
|
|
+ <li><code>mm_alloc_pages(n)</code> — <code>n</code> contiguous lowmem pages.</li>
|
|
|
<li><code>mm_free_page()</code> / <code>mm_free_range()</code> — release a page or a page-aligned boot reservation.</li>
|
|
<li><code>mm_free_page()</code> / <code>mm_free_range()</code> — release a page or a page-aligned boot reservation.</li>
|
|
|
</ul>
|
|
</ul>
|
|
|
<p>
|
|
<p>
|
|
|
At boot, <code>mm_init</code> reserves the low 1 MB, the 8 MB VGA
|
|
At boot, <code>mm_init</code> reserves the low 1 MB, the 8 MB VGA
|
|
|
- shadow region, any LFB that happens to live below 1 GB, the bitmap itself and the
|
|
|
|
|
|
|
+ shadow region, any LFB that overlaps RAM, the bitmap itself and the
|
|
|
boot-time rootfs pack. The pack reservation is released by
|
|
boot-time rootfs pack. The pack reservation is released by
|
|
|
<code>ramfs_init</code> after it copies/inflates the pack into fresh
|
|
<code>ramfs_init</code> after it copies/inflates the pack into fresh
|
|
|
pages.
|
|
pages.
|
|
@@ -279,9 +288,9 @@
|
|
|
<tr><th>Range</th><th>Size</th><th>Contents</th></tr>
|
|
<tr><th>Range</th><th>Size</th><th>Contents</th></tr>
|
|
|
</thead>
|
|
</thead>
|
|
|
<tbody>
|
|
<tbody>
|
|
|
- <tr><td><code>0x40000000</code>–<code>0x40400000</code></td><td>4 MB</td><td>ELF segments, user stack top at <code>0x40400000</code> (32 pages / 128 KB, grows down)</td></tr>
|
|
|
|
|
|
|
+ <tr><td><code>0x40000000</code>–<code>0x40400000</code></td><td>4 MB</td><td>ELF segments, user stack top at <code>0x40400000</code> (32 pages / 128 KB, grows down); highmem frames when available</td></tr>
|
|
|
<tr><td><code>0x50000000</code>–<code>0x50400000</code></td><td>4 MB</td><td>One window canvas mapping (RW, per window)</td></tr>
|
|
<tr><td><code>0x50000000</code>–<code>0x50400000</code></td><td>4 MB</td><td>One window canvas mapping (RW, per window)</td></tr>
|
|
|
- <tr><td><code>0x60000000</code>–<code>0x64000000</code></td><td>up to 64 MB</td><td>Anonymous heap; initial allocation zeroed, growth pages are not</td></tr>
|
|
|
|
|
|
|
+ <tr><td><code>0x60000000</code>–<code>0x64000000</code></td><td>up to 64 MB</td><td>Anonymous heap (highmem frames when available, zeroed through <code>kmap</code>); initial allocation zeroed, growth pages are not</td></tr>
|
|
|
</tbody>
|
|
</tbody>
|
|
|
</table>
|
|
</table>
|
|
|
</div>
|
|
</div>
|
|
@@ -323,7 +332,8 @@
|
|
|
<h2>Limits & leaks</h2>
|
|
<h2>Limits & leaks</h2>
|
|
|
<ul>
|
|
<ul>
|
|
|
<li><strong>No per-process address spaces.</strong> Paging is identity-mapped; there is no memory protection between processes.</li>
|
|
<li><strong>No per-process address spaces.</strong> Paging is identity-mapped; there is no memory protection between processes.</li>
|
|
|
- <li><strong>1 GB ceiling.</strong> Physical memory above <code>0x40000000</code> is not used: that address range is ring-3 space, so the kernel has no mapping for frames above it (a highmem zone with temporary kernel mappings, or PAE for RAM above 4 GB, would be needed). A wild kernel pointer between the identity map and the user windows is an instant <code>#PF</code>.</li>
|
|
|
|
|
|
|
+ <li><strong>4 GB ceiling, 64 MB per process.</strong> RAM above 4 GB is ignored (it would need PAE). Highmem raises the system total, but one process still has a 64 MB heap and a 4 MB image/stack window. MMIO is still identity-mapped at its physical address, which blocks growing ring-3 space toward the PCI hole. A wild kernel pointer between the identity map and the user windows is an instant <code>#PF</code>.</li>
|
|
|
|
|
+ <li><strong>Highmem is user-only.</strong> Contiguous kernel buffers and every kernel structure stay in the 1 GB lowmem zone, so a machine with 4 GB can still run out of lowmem first.</li>
|
|
|
<li><strong>Kernel heap fragmentation.</strong> <code>kmalloc</code>'s power-of-two size classes make long-running mixed-size kernel allocation wasteful; ring-3 commands use the separate first-fit libc heap described above.</li>
|
|
<li><strong>Kernel heap fragmentation.</strong> <code>kmalloc</code>'s power-of-two size classes make long-running mixed-size kernel allocation wasteful; ring-3 commands use the separate first-fit libc heap described above.</li>
|
|
|
<li><strong>No TLB shootdown.</strong> Cross-CPU mapping changes rely on not-present entries not being cached.</li>
|
|
<li><strong>No TLB shootdown.</strong> Cross-CPU mapping changes rely on not-present entries not being cached.</li>
|
|
|
</ul>
|
|
</ul>
|
|
@@ -333,11 +343,11 @@
|
|
|
<h2>Source map</h2>
|
|
<h2>Source map</h2>
|
|
|
<ul class="filelist">
|
|
<ul class="filelist">
|
|
|
<li><code>kernel/mm.c</code>, <code>kernel/mm.h</code> — E820 + bitmap allocator</li>
|
|
<li><code>kernel/mm.c</code>, <code>kernel/mm.h</code> — E820 + bitmap allocator</li>
|
|
|
- <li><code>include/phys.h</code> — shared 1 GB physical ceiling</li>
|
|
|
|
|
|
|
+ <li><code>include/phys.h</code> — lowmem ceiling, kmap window, highmem limit</li>
|
|
|
<li><code>kernel/kmalloc.c</code>, <code>kernel/kmalloc.h</code> — kernel heap</li>
|
|
<li><code>kernel/kmalloc.c</code>, <code>kernel/kmalloc.h</code> — kernel heap</li>
|
|
|
<li><code>user/lib/libc/malloc.c</code>, <code>user/lib/libc/ucmd_mem.c</code> — ring-3 heap and <code>SYS_MEM_*</code> backend</li>
|
|
<li><code>user/lib/libc/malloc.c</code>, <code>user/lib/libc/ucmd_mem.c</code> — ring-3 heap and <code>SYS_MEM_*</code> backend</li>
|
|
|
<li><code>user/lib/libc/string.c</code>, <code>user/lib/libc/stdlib.c</code> — ring-3 libc support</li>
|
|
<li><code>user/lib/libc/string.c</code>, <code>user/lib/libc/stdlib.c</code> — ring-3 libc support</li>
|
|
|
- <li><code>kernel/paging.c</code>, <code>kernel/paging.h</code> — identity map</li>
|
|
|
|
|
|
|
+ <li><code>kernel/paging.c</code>, <code>kernel/paging.h</code> — identity map, kmap window</li>
|
|
|
<li><code>kernel/mtrr.c</code> — write-combining MTRRs</li>
|
|
<li><code>kernel/mtrr.c</code> — write-combining MTRRs</li>
|
|
|
<li><code>kernel/process.c</code>, <code>kernel/process.h</code> — stacks and user layout</li>
|
|
<li><code>kernel/process.c</code>, <code>kernel/process.h</code> — stacks and user layout</li>
|
|
|
<li><code>kernel/ramfs.c</code> — pack copy / inflate</li>
|
|
<li><code>kernel/ramfs.c</code> — pack copy / inflate</li>
|