miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
vmm.h
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2026 Christian Spoo
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef _MINIOS_MM_VMM_H_
24#define _MINIOS_MM_VMM_H_
25
26#include <miniOS/types.h>
27
28
29#define PAGE_PRESENT (1ULL << 0)
30#define PAGE_WRITE (1ULL << 1)
31#define PAGE_USER (1ULL << 2)
32#define PAGE_COW (1ULL << 9) /* software: copy-on-write pending */
33
34/* ------------------------------------------------------------------ */
35/* Higher-half kernel virtual address layout */
36/* ------------------------------------------------------------------ */
37/* KERNEL_VMA: base of higher-half kernel window (PML4[256]). *
38 * Physical address P maps to virtual P + KERNEL_VMA. *
39 * NOTE: Also defined as KERNEL_VMA equ in src/arch/x86_64/main64.asm *
40 * (ASM boot code cannot include C headers). Keep in sync. */
41#define KERNEL_VMA 0xFFFF800000000000ULL
42
43/* HEAP_START: kernel heap virtual base (PML4[260]). *
44 * src/kernel/mm/heap.c honours #ifndef HEAP_START so test builds *
45 * can override this value via -D on the compiler command line. */
46#ifndef HEAP_START
47#define HEAP_START 0xFFFF820000000000ULL
48#endif
49
50/* HEAP_MAX: upper bound of heap (4 MiB above HEAP_START). */
51#ifndef HEAP_MAX
52#define HEAP_MAX (HEAP_START + 4ULL * 1024 * 1024)
53#endif
54
55/* DMA_BUFFER_VA: 4 KiB DMA bounce buffer virtual address (PML4[262]).*/
56#define DMA_BUFFER_VA 0xFFFF830000000000ULL
57
58/* GOP_FB_VA: linear framebuffer virtual base (PML4[264]). *
59 * Mapped at boot by gop_init_from_mb2() when CONSOLE_GOP is defined. */
60#define GOP_FB_VA 0xFFFF840000000000ULL
61
62/* VGA_BUFFER_VA: text-mode VGA framebuffer virtual address. *
63 * Physical 0xB8000 mapped via the KERNEL_VMA identity window. */
64#define VGA_BUFFER_VA (KERNEL_VMA + 0xB8000ULL)
65
66/* ------------------------------------------------------------------ */
67/* x86_64 page size */
68/* ------------------------------------------------------------------ */
69/* PAGE_SIZE: 4 KiB frame size for this kernel (4-level paging). */
70#define PAGE_SIZE 4096ULL
71
72/* ------------------------------------------------------------------ */
73/* Page-table index extraction (4-level paging, 9-bit indices) */
74/* ------------------------------------------------------------------ */
75#define PML4_SHIFT 39 /* bits [47:39] index PML4 */
76#define PDPT_SHIFT 30 /* bits [38:30] index PDPT */
77#define PD_SHIFT 21 /* bits [29:21] index PD */
78#define PT_SHIFT 12 /* bits [20:12] index PT */
79#define PT_INDEX_MASK 0x1FFULL /* 9-bit mask for any level index */
80
81/* ------------------------------------------------------------------ */
82/* PTE physical address mask */
83/* ------------------------------------------------------------------ */
84/* PTE_ADDR_MASK: bits [51:12] hold the physical frame address in a PTE. */
85#define PTE_ADDR_MASK 0x000FFFFFFFFFF000ULL
86
105
114void vmm_unmap_page(uint64_t virt);
115
128
143int vmm_resolve_user_fault(uint64_t cr2, uint64_t error_code);
144
155
165void vmm_free_address_space(uint64_t pml4_phys);
166
167/* PML4-parameterized variants of the four core VMM primitives — operate on
168 * an explicit address space instead of the live CR3. Used to build a new
169 * process's page tables (fork) or inspect another process's memory (ptrace,
170 * once implemented) without ever loading that process's CR3. */
171int vmm_map_page_in(uint64_t pml4_phys, uint64_t virt, uint64_t phys, uint64_t flags);
172void vmm_unmap_page_in(uint64_t pml4_phys, uint64_t virt);
175
176#endif /* _MINIOS_MM_VMM_H_ */
unsigned long int uint64_t
Definition types.h:37
uint16_t flags
Definition virtio_net.c:2
void vmm_unmap_page(uint64_t virt)
Definition vmm.c:164
uint64_t vmm_virt_to_phys(uint64_t virt)
Definition vmm.c:204
void vmm_unmap_page_in(uint64_t pml4_phys, uint64_t virt)
Definition vmm.c:130
uint64_t vmm_virt_to_pte(uint64_t virt)
Definition vmm.c:236
uint64_t vmm_virt_to_phys_in(uint64_t pml4_phys, uint64_t virt)
Definition vmm.c:174
int vmm_map_page(uint64_t virt, uint64_t phys, uint64_t flags)
Definition vmm.c:120
int vmm_resolve_user_fault(uint64_t cr2, uint64_t error_code)
Definition vmm.c:325
uint64_t vmm_new_address_space(void)
Definition vmm.c:265
void vmm_free_address_space(uint64_t pml4_phys)
Definition vmm.c:293
int vmm_map_page_in(uint64_t pml4_phys, uint64_t virt, uint64_t phys, uint64_t flags)
Definition vmm.c:81
uint64_t vmm_virt_to_pte_in(uint64_t pml4_phys, uint64_t virt)
Definition vmm.c:212