]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/arch/x86/mem.cc
Switch to a simple X11-style license.
[polintos/scott/priv.git] / kernel / arch / x86 / mem.cc
1 // arch/x86/mem.cc -- x86 paging and misc. memory management
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal with
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 // of the Software, and to permit persons to whom the Software is furnished to do
10 // so, subject to the following condition:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all 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, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 // CONTRIBUTORS 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 WITH THE
21 // SOFTWARE.
22
23 #include <kern/kernel.h>
24 #include <kern/mem.h>
25 #include <kern/pagealloc.h>
26 #include <kern/libc.h>
27
28 // Initial page tables have the first 4 MiB mapped, using large pages.
29
30 __attribute__((aligned(4096))) u32 x86_init_ptbl_l2[1024] = {
31         0x87
32 };
33
34 extern int _end;
35
36 using Mem::PageAllocZone;
37
38 namespace Arch {
39         u64 mem_end;
40         
41         namespace Priv {
42                 #define zonelist(x) ((x) * ((x) + 1) / 2 - 1)
43                 
44                 PageAllocZone pagezones[num_zones];
45                 PageAllocZone *pagezonelists_real[zonelist(num_zones + 1)];
46         }
47         
48         PageAllocZone **pagezonelists[Priv::num_zones];
49
50         uintptr_t next_free_bootmem = reinterpret_cast<uintptr_t>(&_end);
51 }
52
53 namespace Arch {
54 namespace Priv {
55         void early_adjust_mappings()
56         {
57                 using Mem::get_bootmem;
58         
59                 // Clear low-address mapping and invalidate TLB
60                 x86_init_ptbl_l2[0] = 0;
61                 asm volatile("movl %0, %%cr3" : : "r" (kvirt_to_phys(x86_init_ptbl_l2)));
62                 
63                 // Mark the ktext mapping global now that it's not mapped at address
64                 // zero.  FIXME: check for and enable PGE
65                 
66                 x86_init_ptbl_l2[0x200] |= 0x100;
67         }
68
69         void map_physmem()
70         {
71                 using Mem::get_bootmem;
72         
73                 // phys_to_ktext can be used for the first
74                 // 4MiB-minus-size-of-kernel of bootmem allocations.
75         
76                 for (uintptr_t physpage = 1; physpage <= (mem_end - 1) / (4096*1024);
77                      physpage++)
78                 {
79                         uintptr_t virtpage = physpage + (PHYSMEM_START >> 22);
80                         x86_init_ptbl_l2[virtpage & 1023] = (physpage << 22) | 0x187;
81                 }
82                 
83                 size_t pages_size = (mem_end / page_size) * sizeof(Mem::Page);
84                 Mem::pages = static_cast<Mem::Page *>(get_bootmem(pages_size, 4));
85                 Mem::last_page = Mem::pages + pages_size - 1;
86                 bzero(Mem::pages, pages_size);
87                 
88                 int listpos[num_zones];
89                 
90                 for (int i = num_zones - 1; i >= 0; i--) {
91                         listpos[i] = zonelist(i);
92                         pagezonelists[num_zones - 1 - i] = &pagezonelists_real[listpos[i]];
93
94                         u64 rstart = mem_zone_regions[i].start;
95                         u64 rend = mem_zone_regions[i].end;
96                 
97                         if (mem_start <= rend && mem_end >= rstart) {
98                                 if (rstart < mem_start)
99                                         rstart = mem_start;
100                                 if (rend > mem_end)
101                                         rend = mem_end;
102                                 
103                                 ulong page_start = rstart / page_size;
104                                 ulong page_len = (rend - rstart + 1) / page_size;
105                                 
106                                 pagezones[i].init(page_start, page_len);
107                                 
108                                 for (int j = i; j < num_zones; j++) {
109                                         assert(listpos[j] < zonelist(j + 1));
110                                         pagezonelists_real[listpos[j]++] = &pagezones[i];
111                                 }
112                         }
113                 }
114         }
115 }
116 }