The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
memory_layout.h
Go to the documentation of this file.
1#pragma once
2
3#include "common/stdint.h"
4
5#ifdef __x86_64__
6
7/**
8 * ginnOS virtual memory layout constants (x86_64, 4-level paging, 48-bit
9 * addresses). the user half is the lower canonical half; the kernel half is
10 * the upper one.
11 * 0x0000000000000000 - 0x0000000000000FFF : null guard (never mapped)
12 * 0x0000000000400000 - ... : user program text/data/bss
13 * ... - ... : user heap (sbrk)
14 * 0x0000100000000000 - 0x00007EFFFFFFFFFF : user mmap region
15 * 0x00007FFFFEFFF000 - 0x00007FFFFFFFEFFF : user stack (up to 16 MiB)
16 * 0x00007FFFFFFFF000 - 0x00007FFFFFFFFFFF : user stack guard
17 * 0xFFFF800000000000 - ... : direct map of RAM (limine HHDM)
18 * 0xFFFFC00000000000 - ... : kernel stacks (kstack.h)
19 * 0xFFFFD00000000000 - 0xFFFFD00003FFFFFF : kernel MMIO window (ioremap)
20 * 0xFFFFFFFF80000000 - ... : the kernel image
21 */
22
23#define USER_SPACE_START 0x0000000000001000ul
24#define USER_SPACE_END 0x0000800000000000ul
25
26/* the physical address of the kernel image is set by limine (phys.h) */
27
28/* first physical byte above the legacy area (VGA memory, BIOS ROMs) */
29#define LOW_MEMORY_END 0x00100000ul
30
31#define USER_LOAD_ADDR 0x00400000ul
32
33#define USER_STACK_TOP 0x00007FFFFFFFF000ul
34#define USER_STACK_MAX 0x01000000ul
35#define USER_STACK_LIMIT (USER_STACK_TOP - USER_STACK_MAX)
36#define USER_STACK_SIZE 0x00004000ul
37#define USER_STACK_BASE (USER_STACK_TOP - USER_STACK_SIZE)
38
39/* the direct map. limine maps the HHDM here (4-level paging, no
40 * randomisation); the kernel checks it at boot and makes its own map of all
41 * RAM at the same address */
42#define KERNEL_VIRT_BASE 0xFFFF800000000000ul
43
44/* most RAM the kernel uses (the direct map is 64 TiB, the PMM is smaller) */
45#define PHYS_MAP_SIZE 0x0000001000000000ul
46
47#define KSTACK_REGION_BASE 0xFFFFC00000000000ul
48
49#define MMIO_REGION_BASE 0xFFFFD00000000000ul
50#define MMIO_REGION_END 0xFFFFD00004000000ul
51
52#define USER_MMAP_BASE 0x0000100000000000ul
53#define USER_MMAP_END 0x00007F0000000000ul
54
55/* the kernel image is linked here (linker/x86_64/kernel.ld) */
56#define KERNEL_IMAGE_BASE 0xFFFFFFFF80000000ul
57
58#else
59
60/**
61 * ginnOS virtual memory layout constants.
62 *
63 * every address space (i686, 4 GiB):
64 * 0x00000000 - 0x00000FFF : null guard (never mapped)
65 * 0x00400000 - ... : user program text/data/bss (linker/user.ld)
66 * ... - ... : user heap (grows up via sbrk from the ELF end)
67 * 0x40000000 - 0xAFFFFFFF : user mmap region
68 * 0xBEFFF000 - 0xBFFFEFFF : user stack (up to 16 MiB, grows down on demand)
69 * 0xBFFFF000 - 0xBFFFFFFF : user stack guard (never mapped)
70 * 0xC0000000 - 0xDFFFFFFF : kernel: direct map of RAM (phys_to_virt)
71 * 0xE0000000 - ... : kernel stacks (kstack.h)
72 * 0xF0000000 - 0xF3FFFFFF : kernel MMIO window (ioremap)
73 *
74 * the user half (below 0xC0000000) is per process; the kernel half is shared
75 * by every address space and supervisor-only.
76 */
77
78/* lowest address a user pointer can validly reference (above the null guard) */
79#define USER_SPACE_START 0x00001000u
80
81/* one past the highest user-accessible address */
82#define USER_SPACE_END 0xC0000000u
83
84/* kernel physical load address */
85#define KERNEL_PHYS_BASE 0x00010000u
86
87/* first physical byte above the legacy area. the range from 0xA0000 to here
88 * holds VGA memory and the BIOS ROMs, so boot-time allocations (the PMM
89 * metadata and the first heap pages) start at this address or higher. */
90#define LOW_MEMORY_END 0x00100000u
91
92/* user program load address (matches linker/user.ld) */
93#define USER_LOAD_ADDR 0x00400000u
94
95/* top of the user stack (exclusive); the page above it is an unmapped guard */
96#define USER_STACK_TOP 0xBFFFF000u
97
98/* the user stack area: up to 16 MiB below USER_STACK_TOP, backed on demand
99 * as the stack grows down */
100#define USER_STACK_MAX 0x01000000u
101
102/* lowest address the user stack may grow to */
103#define USER_STACK_LIMIT (USER_STACK_TOP - USER_STACK_MAX)
104
105/* the top of the stack, mapped before the process starts (argc/argv) */
106#define USER_STACK_SIZE 0x00004000u
107
108/* lowest address of the initially mapped part of the stack */
109#define USER_STACK_BASE (USER_STACK_TOP - USER_STACK_SIZE)
110
111/* the kernel half: every address from here up is kernel-only and shared by
112 * all address spaces. RAM is direct-mapped at its start (phys_to_virt). */
113#define KERNEL_VIRT_BASE 0xC0000000u
114
115/* most RAM the direct map covers on 32-bit (it must stay below the kernel
116 * stack region); RAM above this is not used. x86_64 removes the cap. */
117#define PHYS_MAP_SIZE 0x20000000u
118
119/* kernel stacks: one slot per process, each an unmapped guard page followed
120 * by the stack itself. kernel-only, shared by every address space (its page
121 * table is reserved at boot), above user space. */
122#define KSTACK_REGION_BASE 0xE0000000u
123
124/* device memory (MMIO) mapped with ioremap, e.g. the framebuffer: a kernel
125 * window shared by every address space. */
126#define MMIO_REGION_BASE 0xF0000000u
127#define MMIO_REGION_END 0xF4000000u
128
129/* userspace mmap region: SYS_mmap hands out page-aligned virtual ranges here,
130 * bumping upward per mapping. sits below kernel space (0xC0000000) and clear of
131 * the program, heap, and stack. no unmap/reuse yet, just a bump cursor per
132 * process. this is also where a device mapping (e.g. mmap of /dev/fb0) lands. */
133#define USER_MMAP_BASE 0x40000000u
134#define USER_MMAP_END 0xB0000000u
135
136#endif // __x86_64__
137
138/**
139 * check whether a pointer with a given length falls entirely within
140 * the user address space. used to validate syscall arguments before
141 * the kernel dereferences them.
142 *
143 * @param ptr pointer to validate.
144 * @param len number of bytes the kernel will access starting at ptr.
145 * @return true if the entire range [ptr, ptr+len) is in user space.
146 */
147static inline bool is_user_ptr(const void *ptr, size_t len)
148{
149 uintptr_t addr = (uintptr_t)ptr;
150
151 /* null check */
152 if (addr == 0)
153 return false;
154
155 /* must be within user space bounds */
156 if (addr < USER_SPACE_START)
157 return false;
158
159 if (addr >= USER_SPACE_END)
160 return false;
161
162 /* overflow check: addr + len must not wrap or exceed user space */
163 if (len > 0 && (addr + len < addr || addr + len > USER_SPACE_END))
164 return false;
165
166 return true;
167}
168
169/**
170 * check whether a user string pointer is valid.
171 * only checks that the pointer itself is in user space, not the full
172 * string length (we can't know the length without reading it).
173 * callers should bound string reads with a maximum length.
174 *
175 * @param str pointer to validate.
176 * @return true if str points into user space.
177 */
178static inline bool is_user_str(const char *str)
179{
180 return is_user_ptr(str, 1);
181}
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
#define USER_SPACE_END
Definition memory_layout.h:82
#define USER_SPACE_START
ginnOS virtual memory layout constants.
Definition memory_layout.h:79