blob: b4c1b330773c563af7b35d4d34082ed950c1d970 [file] [log] [blame]
swissChilie20b79b2021-03-17 21:20:13 -07001#include "task.h"
2#include "alloc.h"
3#include "io.h"
4#include "paging.h"
5
6uint clone_page_table(uint table)
7{
8 // is this table a 4mb mapping OR is it not mapped?
9 // See Intel manual volume 3 figure 4-4.
10
11 if (table & 1 << 7 || !(table & 1))
12 {
13 // if so, return as is, 4mb mappings only used by kernel and should
14 // be mapped to the same location
15 return table;
16 }
17 else
18 {
19 // else, look for the used pages in this table, copy them to a new
20 // frame, and store them in a new table, then return that table
21 // TODO: add copy on write
22
23 uint new_p;
24 uint *new = kmalloc_ap(sizeof(uint[1024]), (void **)&new_p);
25
26 // We don't care about the lower 12 bits, just want the address
27 uint *old_virt = PHYS_TO_VIRT((uint *)(table ^ 0xfff));
28
29 for (int i = 0; i < 1024; i++)
30 {
31 if (old_virt[i] & 1)
32 {
33 // If mapped
34
35 // again, don't care about bottom 12 bits
36 uchar *old_page = PHYS_TO_VIRT(old_virt[i] ^ 0xfff);
37
38 alloc_frame(&new[i], BOOL(old_virt[i] & 1 << 2),
39 BOOL(old_virt[i] & 1 << 1));
40
41 uchar *new_page = PHYS_TO_VIRT(new[i] ^ 0xfff);
42 memcpy(new_page, old_page, 0x1000);
43 }
44 }
45
46 return new_p | (table & 0xfff); // same lower 12 bits
47 }
48}
49
50uint *clone_page_directory(uint *dir)
51{
52 uint new_p;
53 uint *new = kmalloc_ap(sizeof(uint[1024]), (void **)&new_p);
54
55 for (int i = 0; i < 1024; i++)
56 {
57 new[i] = clone_page_table(dir[i]);
58 }
59
60 return new;
61}