annotate cos/kernel/mm.c @ 37:5c20bd53cccd

Cleanup
author windel
date Mon, 16 Jan 2012 21:38:55 +0100
parents 8012221dd740
children 24ce177e01e8
rev   line source
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
1 /* Memory manager functions,
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
2 *
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
3 * Uses a bitmap to keep track of 4k pages that are in use.
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
4 *
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
5 * */
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
6
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
7 #include "kernel.h"
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
8
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
9 // Bitmap that keeps track of all the 4 kB memory pages in the system:
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
10 static uint64_t *frames = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
11 static uint64_t nframes = 0;
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
12
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
13 memmap_t* kernel_map = 0; // kernel memory mapping
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
14 memmap_t* current_map = 0; // The currently active memory mapping
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
15
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
16 static void set_frame(uint64_t frame)
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
17 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
18 uint64_t idx = frame / 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
19 uint64_t off = frame % 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
20 frames[idx] |= (0x1 << off);
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
21 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
22
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
23 static void clear_frame(uint64_t frame)
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
24 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
25 uint64_t idx = frame / 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
26 uint64_t off = frame % 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
27 frames[idx] &= ~(0x1 << off);
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
28 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
29
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
30 uint64_t test_frame(uint64_t frame)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
31 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
32 uint64_t idx = frame / 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
33 uint64_t off = frame % 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
34 return (frames[idx] & (0x1 << off));
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
35 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
36
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
37 static uint64_t first_frame()
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
38 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
39 uint64_t i, j;
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
40 for (i = 0; i < nframes / 64; i++)
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
41 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
42 if (frames[i] != 0xFFFFFFFFFFFFFFFF)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
43 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
44 for (j = 0; j < 64; j++)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
45 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
46 uint64_t dut = 0x1 << j;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
47 if ((frames[i] & dut) != dut)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
48 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
49 return i*64+j;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
50 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
51 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
52 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
53 }
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
54
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
55 // No frame found:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
56 return (uint64_t) -1;
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
57 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
58
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
59 /*
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
60 * Initializes the memory manager, allocating a bitmap once.
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
61 */
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
62 void init_memory(uint64_t total_mem_size)
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
63 {
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
64 printf("Running with %d MB ram\n", total_mem_size / 1000000);
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
65
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 30
diff changeset
66 // Only here placement malloc is used!
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 30
diff changeset
67 //
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
68 // Allocate and clear bits to remember which 4KiB-frames are in use:
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
69 nframes = (total_mem_size / 0x1000); // Calculate number of frames
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
70 frames = (uint64_t*)kmalloc_int( (nframes / 64) * sizeof(uint64_t) );
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
71 memset(frames, 0, (nframes / 64) * sizeof(uint64_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
72
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
73 // Create kernel map:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
74 kernel_map = (memmap_t*)kmalloc_aligned_int(sizeof(memmap_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
75 memset(kernel_map, 0, sizeof(memmap_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
76
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
77 // Identity map lower memory and mark as used by the kernel.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
78 // Mark as used by the kernel:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
79 uint64_t i;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
80 i = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
81 while ( i <= placement_address)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
82 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
83 page_t *page;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
84
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
85 page = get_page(i, kernel_map); // Get page for address
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
86 page->address = i >> 12; // Set the address of this page.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
87 page->present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
88 page->rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
89 page->us = 1; // Make all things accessable for users for now.
30
0148f55bfe24 Added static asserts and fixed pages
windel
parents: 29
diff changeset
90 //page->pwt = 1; // Is this bit required?
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
91
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
92 set_frame(i / 0x1000);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
93
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
94 i += 0x1000; // Increase a 4 k frame
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
95 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
96
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
97 // Set the created mapping as active:
30
0148f55bfe24 Added static asserts and fixed pages
windel
parents: 29
diff changeset
98 switch_mapping(kernel_map);
0148f55bfe24 Added static asserts and fixed pages
windel
parents: 29
diff changeset
99
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
100 // TODO: set the use of placement malloc to invalid after here.
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
101 }
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
102
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
103 void alloc_frame(page_t *page)
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
104 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
105 if (page->address != 0)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
106 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
107 return;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
108 }
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
109 uint64_t idx = first_frame();
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
110 if (idx == (uint64_t) -1)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
111 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
112 panic("No more memory!");
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
113 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
114 set_frame(idx);
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
115
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
116 page->present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
117 page->rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
118 page->us = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
119 page->address = idx; // set address in frame number, the byte address is 0x1000 times this value.
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
120 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
121
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
122 void free_frame(page_t *page)
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
123 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
124 clear_frame(page->address / 0x1000);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
125 page->address = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
126 page->present = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
127 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
128
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
129 void switch_mapping(memmap_t* mapping)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
130 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
131 current_map = mapping;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
132
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
133 asm volatile("mov %0, %%cr3" :: "r"(&mapping->table));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
134
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
135 // Enable paging (and flush cache):
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
136 uint64_t cr0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
137 asm volatile("mov %%cr0, %0": "=r"(cr0));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
138 cr0 |= 0x80000000;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
139 asm volatile("mov %0, %%cr0":: "r"(cr0));
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
140 }
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
141
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
142 /* Get a page for a virtual address, and create other tables if required */
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
143 page_t* get_page(uint64_t address, memmap_t *mapping)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
144 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
145 // Retrieve the correct PDP (page dir pointer table):
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
146 uint64_t pml4index = (address >> 39) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
147 PDPT_t *pdpt = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
148 if (mapping->table[pml4index].present == 1)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
149 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
150 pdpt = (PDPT_t*)((uint64_t)mapping->table[pml4index].address << 12);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
151 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
152 else
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
153 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
154 // Create new table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
155 pdpt = (PDPT_t*)kmalloc_aligned_int(sizeof(PDPT_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
156 memset(pdpt, 0, sizeof(PDPT_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
157
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
158 // TODO: get function like virt2phys or something here
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
159 uint64_t address = (uint64_t)pdpt; // get the physical address
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
160 mapping->table[pml4index].address = address >> 12; // shift right
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
161 mapping->table[pml4index].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
162 mapping->table[pml4index].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
163 mapping->table[pml4index].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
164 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
165
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
166 uint64_t pdptindex = (address >> 30) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
167 // Retrieve the correct page directory:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
168 PD_t *pd = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
169 if (pdpt->table[pdptindex].present == 1)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
170 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
171 pd = (PD_t*)((uint64_t)pdpt->table[pdptindex].address << 12);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
172 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
173 else
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
174 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
175 // Create a new table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
176 pd = (PD_t*)kmalloc_aligned_int(sizeof(PD_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
177 memset(pd, 0, sizeof(PD_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
178
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
179 // Enter table into PDPT:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
180 // TODO: make virt2phys function:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
181 uint64_t address = (uint64_t)pd;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
182 pdpt->table[pdptindex].address = address >> 12;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
183 pdpt->table[pdptindex].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
184 pdpt->table[pdptindex].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
185 pdpt->table[pdptindex].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
186 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
187
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
188 // Retrieve the correct page table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
189 uint64_t pdindex = (address >> 21) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
190 PT_t *pt = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
191 if (pd->table[pdindex].present == 1)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
192 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
193 pt = (PT_t*)((uint64_t)pd->table[pdindex].address << 12);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
194 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
195 else
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
196 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
197 // Create table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
198 pt = (PT_t*)kmalloc_aligned_int(sizeof(PD_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
199 memset(pt, 0, sizeof(PT_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
200
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
201 // Enter PT into PD:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
202 uint64_t address = (uint64_t)pt;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
203 pd->table[pdindex].address = address >> 12;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
204 pd->table[pdindex].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
205 pd->table[pdindex].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
206 pd->table[pdindex].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
207 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
208
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
209 // Finally get the page from the directory:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
210 // TODO: convert from physical address to virtual address:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
211 uint64_t ptindex = (address >> 12) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
212 return &pt->table[ptindex];
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
213 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
214