annotate cos/kernel/mm.c @ 29:7e3bdcb391dc

Added get_page function to mm
author windel
date Thu, 29 Dec 2011 19:34:01 +0100
parents 47b7df514243
children 0148f55bfe24
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 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
61 // Allocate and clear bits to remember which 4kb-frames are in use:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
62 nframes = (total_mem_size / 0x1000);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
63 frames = (uint64_t*)kmalloc_int( (nframes / 64) * sizeof(uint64_t) );
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
64 memset(frames, 0, (nframes / 64) * sizeof(uint64_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
65
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
66 // Create kernel map:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
67 kernel_map = (memmap_t*)kmalloc_aligned_int(sizeof(memmap_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
68 memset(kernel_map, 0, sizeof(memmap_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
69
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
70 // Identity map lower memory and mark as used by the kernel.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
71 // Mark as used by the kernel:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
72 uint64_t i;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
73 i = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
74 while ( i <= placement_address)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
75 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
76 page_t *page;
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 = get_page(i, kernel_map); // Get page for address
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
79 page->address = i >> 12; // Set the address of this page.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
80 page->present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
81 page->rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
82 page->us = 1; // Make all things accessable for users for now.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
83
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
84 set_frame(i / 0x1000);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
85
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
86 i += 0x1000; // Increase a 4 k frame
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
87 }
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 // Set the created mapping as active:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
90 // switch_mapping(kernel_map);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
91 // TODO: debug crash after enable of new memory map
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
92 // TODO: set the use of placement malloc to invalid after here.
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
93 }
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
94
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
95 void alloc_frame(page_t *page)
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
96 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
97 if (page->address != 0)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
98 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
99 return;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
100 }
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
101 uint64_t idx = first_frame();
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
102 if (idx == (uint64_t) -1)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
103 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
104 panic("No more memory!");
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
105 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
106 set_frame(idx);
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
107
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
108 page->present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
109 page->rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
110 page->us = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
111 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
112 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
113
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
114 void free_frame(page_t *page)
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
115 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
116 clear_frame(page->address / 0x1000);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
117 page->address = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
118 page->present = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
119 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
120
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
121 void switch_mapping(memmap_t* mapping)
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 current_map = mapping;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
124 printf("Switching to use of other at %x\n", &mapping->table);
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 asm volatile("mov %0, %%cr3" :: "r"(&mapping->table));
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 // Enable paging (and flush cache):
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
129 uint64_t cr0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
130 asm volatile("mov %%cr0, %0": "=r"(cr0));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
131 cr0 |= 0x80000000;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
132 asm volatile("mov %0, %%cr0":: "r"(cr0));
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
133 }
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
134
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
135 /* 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
136 page_t* get_page(uint64_t address, memmap_t *mapping)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
137 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
138 // Retrieve the correct PDP (page dir pointer table):
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
139 uint64_t pml4index = (address >> 39) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
140 PDPT_t *pdpt = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
141 if (mapping->table[pml4index].present == 1)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
142 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
143 pdpt = (PDPT_t*)((uint64_t)mapping->table[pml4index].address << 12);
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 else
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
146 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
147 // Create new table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
148 pdpt = (PDPT_t*)kmalloc_aligned_int(sizeof(PDPT_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
149 printf("Creating PDPT %x\n", pdpt);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
150 memset(pdpt, 0, sizeof(PDPT_t));
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 // TODO: get function like virt2phys or something here
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
153 uint64_t address = (uint64_t)pdpt; // get the physical address
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
154 mapping->table[pml4index].address = address >> 12; // shift right
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
155 mapping->table[pml4index].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
156 mapping->table[pml4index].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
157 mapping->table[pml4index].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
158 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
159
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
160 uint64_t pdptindex = (address >> 30) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
161 // Retrieve the correct page directory:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
162 PD_t *pd = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
163 if (pdpt->table[pdptindex].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 pd = (PD_t*)((uint64_t)pdpt->table[pdptindex].address << 12);
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 else
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 printf("Creating PD\n");
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
170 // Create a new table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
171 pd = (PD_t*)kmalloc_aligned_int(sizeof(PD_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
172 memset(pd, 0, sizeof(PD_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
173
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
174 // Enter table into PDPT:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
175 // TODO: make virt2phys function:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
176 uint64_t address = (uint64_t)pd;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
177 pdpt->table[pdptindex].address = address >> 12;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
178 pdpt->table[pdptindex].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
179 pdpt->table[pdptindex].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
180 pdpt->table[pdptindex].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
181 }
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 // Retrieve the correct page table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
184 uint64_t pdindex = (address >> 21) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
185 PT_t *pt = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
186 if (pd->table[pdindex].present == 1)
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 pt = (PT_t*)((uint64_t)pd->table[pdindex].address << 12);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
189 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
190 else
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
191 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
192 printf("Creating PT\n");
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