1 // arch/x86/mem.cc -- x86 paging and misc. memory management
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
15 #include <kern/kernel.h>
17 #include <kern/pagealloc.h>
18 #include <kern/libc.h>
20 // Initial page tables have the first 4 MiB mapped, using large pages.
22 __attribute__((aligned(4096))) u32 x86_init_ptbl_l2[1024] = {
28 using Mem::PageAllocZone;
34 #define zonelist(x) ((x) * ((x) + 1) / 2 - 1)
36 PageAllocZone pagezones[num_zones];
37 PageAllocZone *pagezonelists_real[zonelist(num_zones + 1)];
40 PageAllocZone **pagezonelists[Priv::num_zones];
42 uintptr_t next_free_bootmem = reinterpret_cast<uintptr_t>(&_end);
47 void early_adjust_mappings()
49 using Mem::get_bootmem;
51 // Clear low-address mapping and invalidate TLB
52 x86_init_ptbl_l2[0] = 0;
53 asm volatile("movl %0, %%cr3" : : "r" (kvirt_to_phys(x86_init_ptbl_l2)));
55 // Mark the ktext mapping global now that it's not mapped at address
56 // zero. FIXME: check for and enable PGE
58 x86_init_ptbl_l2[0x200] |= 0x100;
63 using Mem::get_bootmem;
65 // phys_to_ktext can be used for the first
66 // 4MiB-minus-size-of-kernel of bootmem allocations.
68 for (uintptr_t physpage = 1; physpage <= (mem_end - 1) / (4096*1024);
71 uintptr_t virtpage = physpage + (PHYSMEM_START >> 22);
72 x86_init_ptbl_l2[virtpage & 1023] = (physpage << 22) | 0x187;
75 size_t pages_size = (mem_end / page_size) * sizeof(Mem::Page);
76 Mem::pages = static_cast<Mem::Page *>(get_bootmem(pages_size, 4));
77 Mem::last_page = Mem::pages + pages_size - 1;
78 bzero(Mem::pages, pages_size);
80 int listpos[num_zones];
82 for (int i = num_zones - 1; i >= 0; i--) {
83 listpos[i] = zonelist(i);
84 pagezonelists[num_zones - 1 - i] = &pagezonelists_real[listpos[i]];
86 u64 rstart = mem_zone_regions[i].start;
87 u64 rend = mem_zone_regions[i].end;
89 if (mem_start <= rend && mem_end >= rstart) {
90 if (rstart < mem_start)
95 ulong page_start = rstart / page_size;
96 ulong page_len = (rend - rstart + 1) / page_size;
98 pagezones[i].init(page_start, page_len);
100 for (int j = i; j < num_zones; j++) {
101 assert(listpos[j] < zonelist(j + 1));
102 pagezonelists_real[listpos[j]++] = &pagezones[i];