annotate cos/kernel/mm.c @ 31:88590c42320f

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