annotate cos/kernel/mm.c @ 328:0bb16d2a5699

Added cool sphinx plugin for creation of unittest result table
author Windel Bouwman
date Tue, 04 Feb 2014 09:01:11 +0100
parents 35cc54e078dd
children
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
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
9 #define HEAP_START 0xC000000000000000
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
10 #define HEAP_INITIAL_SIZE 0x100000
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
11
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
12 // 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
13 static uint64_t *frames = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
14 static uint64_t nframes = 0;
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
15
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
16 memmap_t* kernel_map = 0; // kernel memory mapping
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
17 // TODO: determine if this is required.
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
18 memmap_t* current_map = 0; // The currently active memory mapping
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
19
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
20 heap_t* kernel_heap = 0;
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
21
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
22 static void set_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
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
29 static void clear_frame(uint64_t frame)
26
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 frames[idx] &= ~(0x1 << off);
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
34 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
35
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
36 uint64_t test_frame(uint64_t frame)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
37 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
38 uint64_t idx = frame / 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
39 uint64_t off = frame % 64;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
40 return (frames[idx] & (0x1 << off));
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
41 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
42
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
43 static uint64_t first_frame()
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
44 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
45 uint64_t i, j;
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
46 for (i = 0; i < nframes / 64; i++)
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
47 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
48 if (frames[i] != 0xFFFFFFFFFFFFFFFF)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
49 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
50 for (j = 0; j < 64; j++)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
51 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
52 uint64_t dut = 0x1 << j;
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
53 if ((frames[i] & dut) != dut)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
54 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
55 return i*64+j;
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 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
59 }
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
60
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
61 // No frame found:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
62 return (uint64_t) -1;
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
63 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
64
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
65 void memory_status(void)
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
66 {
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
67 // Determine amount of used memory.
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
68 uint64_t i, j;
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
69 uint64_t inuse = 0;
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
70 for (i = 0; i < nframes / 64; i++)
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
71 {
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
72 for (j = 0; j < 64; j++)
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
73 {
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
74 if ((frames[i] & (1UL<<j)) == (1UL<<j))
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
75 {
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
76 inuse++;
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
77 }
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
78 }
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
79 }
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
80 printf("Memory: %d, in use: %d\n", nframes/0x100, inuse/0x100);
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
81 }
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
82
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
83 /*
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
84 * Initializes the memory manager, allocating a bitmap once.
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
85 */
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
86 void init_memory(uint64_t total_mem_size)
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
87 {
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
88 printf("Running with %d MB ram\n", total_mem_size / 1000000);
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
89
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
90 // Memory size is from address 0x100000
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
91 total_mem_size += 0x100000;
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
92
32
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 30
diff changeset
93 // Only here placement malloc is used!
3a6a9b929db0 Added initial ramdisk and some virtual file system functions
windel
parents: 30
diff changeset
94 //
37
5c20bd53cccd Cleanup
windel
parents: 34
diff changeset
95 // Allocate and clear bits to remember which 4KiB-frames are in use:
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
96 // Calculate number of frames. memory size is indicated from address 0x100000
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
97 nframes = (total_mem_size) / 0x1000; // Calculate number of frames
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
98 int numbytes = ((nframes / 64) + 1) * sizeof(uint64_t);
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
99 frames = (uint64_t*)kmalloc(numbytes);
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
100 memset(frames, 0, numbytes);
29
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 // Create kernel map:
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
103 kernel_map = (memmap_t*)kmalloc_a(sizeof(memmap_t));
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
104 memset(kernel_map, 0, sizeof(memmap_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
105
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
106 // Identity map lower memory and mark as used by the kernel.
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
107 // Mark as used by the kernel:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
108 uint64_t i;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
109 i = 0;
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
110 while ( i <= placement_address + 0x100000)
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
111 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
112 page_t *page;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
113
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
114 page = get_page(i, kernel_map); // Get page for address
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
115 page->address = i >> 12; // Set the address of this page.
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 = 1; // Make all things accessable for users for now.
30
0148f55bfe24 Added static asserts and fixed pages
windel
parents: 29
diff changeset
119 //page->pwt = 1; // Is this bit required?
29
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 set_frame(i / 0x1000);
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 i += 0x1000; // Increase a 4 k frame
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
124 }
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 // Set the created mapping as active:
41
35cc54e078dd Added memory functions
windel
parents: 40
diff changeset
127 switch_mapping(kernel_map); // Loads cr3
30
0148f55bfe24 Added static asserts and fixed pages
windel
parents: 29
diff changeset
128
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
129 // TODO: set the use of placement malloc to invalid after here.
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
130 // kernel_heap = create_heap(HEAP_START, HEAP_INITIAL_SIZE);
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
131 }
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
132
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
133 void alloc_frame(page_t *page)
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
134 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
135 if (page->address != 0)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
136 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
137 return;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
138 }
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
139 uint64_t idx = first_frame();
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
140 if (idx == (uint64_t) -1)
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
141 {
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
142 panic("No more memory!");
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
143 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
144 set_frame(idx);
29
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 page->present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
147 page->rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
148 page->us = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
149 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
150 }
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
151
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
152 void free_frame(page_t *page)
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
153 {
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
154 clear_frame(page->address / 0x1000);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
155 page->address = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
156 page->present = 0;
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
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
159 void switch_mapping(memmap_t* mapping)
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 current_map = mapping;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
162
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
163 // Load table address:
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
164 asm volatile("mov %0, %%cr3" :: "r"(&mapping->table));
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 // Enable paging (and flush cache):
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
167 uint64_t cr0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
168 asm volatile("mov %%cr0, %0": "=r"(cr0));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
169 cr0 |= 0x80000000;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
170 asm volatile("mov %0, %%cr0":: "r"(cr0));
28
47b7df514243 Moved Makefiles
windel
parents: 26
diff changeset
171 }
26
dcce92b1efbc Added mm.c
windel
parents:
diff changeset
172
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
173 /* 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
174 page_t* get_page(uint64_t address, memmap_t *mapping)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
175 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
176 // Retrieve the correct PDP (page dir pointer table):
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
177 uint64_t pml4index = (address >> 39) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
178 PDPT_t *pdpt = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
179 if (mapping->table[pml4index].present == 1)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
180 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
181 pdpt = (PDPT_t*)((uint64_t)mapping->table[pml4index].address << 12);
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 else
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
184 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
185 // Create new table:
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
186 printf("Get page for address %x\n", address);
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
187 // TODO: make sure that this function calls kmalloc instead of placement malloc.
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
188 pdpt = (PDPT_t*)kmalloc_a(sizeof(PDPT_t));
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
189 memset(pdpt, 0, sizeof(PDPT_t));
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 // TODO: get function like virt2phys or something here
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
192 uint64_t address = (uint64_t)pdpt; // get the physical address
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
193 mapping->table[pml4index].address = address >> 12; // shift right
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
194 mapping->table[pml4index].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
195 mapping->table[pml4index].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
196 mapping->table[pml4index].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
197 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
198
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
199 uint64_t pdptindex = (address >> 30) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
200 // Retrieve the correct page directory:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
201 PD_t *pd = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
202 if (pdpt->table[pdptindex].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 pd = (PD_t*)((uint64_t)pdpt->table[pdptindex].address << 12);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
205 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
206 else
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 // Create a new table:
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
209 pd = (PD_t*)kmalloc_a(sizeof(PD_t));
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
210 memset(pd, 0, sizeof(PD_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
211
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
212 // Enter table into PDPT:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
213 // TODO: make virt2phys function:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
214 uint64_t address = (uint64_t)pd;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
215 pdpt->table[pdptindex].address = address >> 12;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
216 pdpt->table[pdptindex].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
217 pdpt->table[pdptindex].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
218 pdpt->table[pdptindex].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
219 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
220
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
221 // Retrieve the correct page table:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
222 uint64_t pdindex = (address >> 21) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
223 PT_t *pt = 0;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
224 if (pd->table[pdindex].present == 1)
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
225 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
226 pt = (PT_t*)((uint64_t)pd->table[pdindex].address << 12);
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
227 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
228 else
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
229 {
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
230 // Create table:
40
24ce177e01e8 Added more malloc stuff. Added mem usage
windel
parents: 37
diff changeset
231 pt = (PT_t*)kmalloc_a(sizeof(PD_t));
29
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
232 memset(pt, 0, sizeof(PT_t));
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
233
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
234 // Enter PT into PD:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
235 uint64_t address = (uint64_t)pt;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
236 pd->table[pdindex].address = address >> 12;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
237 pd->table[pdindex].rw = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
238 pd->table[pdindex].us = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
239 pd->table[pdindex].present = 1;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
240 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
241
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
242 // Finally get the page from the directory:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
243 // TODO: convert from physical address to virtual address:
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
244 uint64_t ptindex = (address >> 12) & 0x1FF;
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
245 return &pt->table[ptindex];
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
246 }
7e3bdcb391dc Added get_page function to mm
windel
parents: 28
diff changeset
247