annotate src/impl/vm_impl_private.cbc @ 202:66db83ec1ec2

kpt_alloc_check_impl and freerange
author tobaru
date Thu, 23 Jan 2020 20:48:16 +0900
parents a0620ca23f19
children 6e03cee9733e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
193
1301727600cc success build
anatofuz
parents:
diff changeset
1 #include "../../context.h"
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
2 #include "mmu.h"
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
3 #include "memlayout.h"
193
1301727600cc success build
anatofuz
parents:
diff changeset
4 #interface "vm_impl.h"
1301727600cc success build
anatofuz
parents:
diff changeset
5
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
6 /*
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
7 vm_impl* createvm_impl2();
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
8 */
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
9
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
10 __code loaduvm_ptesize_checkvm_impl(struct vm_impl* vm_impl, __code next(...)) {
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
11 char* addr = vm_impl->addr;
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
12
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
13 if ((uint) addr %PTE_SZ != 0) {
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
14 // goto panic
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
15 }
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
16
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
17 goto loaduvm_loopvm_impl(vm_impl, next(...));
193
1301727600cc success build
anatofuz
parents:
diff changeset
18 }
1301727600cc success build
anatofuz
parents:
diff changeset
19
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
20 __code loaduvm_loopvm_impl(struct vm_impl* vm_impl, __code next(...)) {
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
21 uint i = vm_impl->i;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
22 uint sz = vm_impl->sz;
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
23
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
24 if (i < sz) {
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
25 goto loaduvm_check_pgdir(vm_impl, next(...));
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
26 }
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
27
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
28 goto loaduvm_exit(vm_impl, next(...));
193
1301727600cc success build
anatofuz
parents:
diff changeset
29 }
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
30
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
31
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
32 static pte_t* walkpgdir (pde_t *pgdir, const void *va, int alloc)
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
33 {
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
34 pde_t *pde;
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
35 pte_t *pgtab;
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
36
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
37 // pgdir points to the page directory, get the page direcotry entry (pde)
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
38 pde = &pgdir[PDE_IDX(va)];
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
39
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
40 if (*pde & PE_TYPES) {
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
41 pgtab = (pte_t*) p2v(PT_ADDR(*pde));
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
42
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
43 } else {
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
44 if (!alloc || (pgtab = (pte_t*) kpt_alloc()) == 0) {
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
45 return 0;
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
46 }
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
47
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
48 // Make sure all those PTE_P bits are zero.
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
49 memset(pgtab, 0, PT_SZ);
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
50
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
51 // The permissions here are overly generous, but they can
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
52 // be further restricted by the permissions in the page table
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
53 // entries, if necessary.
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
54 *pde = v2p(pgtab) | UPDE_TYPE;
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
55 }
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
56
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
57 return &pgtab[PTE_IDX(va)];
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
58 }
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
59
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
60
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
61 __code loaduvm_check_pgdir(struct vm_impl* vm_impl, __code next(...)) {
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
62 pte_t* pte = vm_impl->pte;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
63 pde_t* pgdir = vm_impl->pgdir;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
64 uint i = vm_impl->i;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
65 char* addr = vm_impl->addr;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
66 uint pa = vm_impl->pa;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
67
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
68 if ((pte = walkpgdir(pgdir, addr + i, 0)) == 0) {
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
69 // goto panic
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
70 }
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
71 pa = PTE_ADDR(*pte);
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
72
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
73 vm_impl->pte = pte;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
74 vm_impl->pgdir = pgdir;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
75 vm_impl->addr = addr;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
76 vm_impl->pa = pa;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
77
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
78 goto loaduvm_check_PTE_SZ(vm_impl, next(...));
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
79 }
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
80
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
81 __code loaduvm_check_PTE_SZ(struct vm_impl* vm_impl, __code next(...)) {
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
82 uint sz = vm_impl->sz;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
83 uint i = vm_impl->i;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
84 uint n = vm_impl->n;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
85 struct inode* ip = vm_impl->ip;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
86 uint pa = vm_impl->pa;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
87 uint offset = vm_impl->offset;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
88
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
89 if (sz - i < PTE_SZ) {
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
90 n = sz - i;
199
8a55878f6a25 loaduvm
tobaru
parents: 198
diff changeset
91 } else {
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
92 n = PTE_SZ;
199
8a55878f6a25 loaduvm
tobaru
parents: 198
diff changeset
93 }
8a55878f6a25 loaduvm
tobaru
parents: 198
diff changeset
94
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
95 if (readi(ip, p2v(pa), offset + i, n) != n) {
199
8a55878f6a25 loaduvm
tobaru
parents: 198
diff changeset
96 // panic
8a55878f6a25 loaduvm
tobaru
parents: 198
diff changeset
97 // return -1;
8a55878f6a25 loaduvm
tobaru
parents: 198
diff changeset
98 }
8a55878f6a25 loaduvm
tobaru
parents: 198
diff changeset
99
200
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
100 vm_impl->n = n;
a0620ca23f19 be functional loaduvm
tobaru
parents: 199
diff changeset
101
198
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
102 goto loaduvm_exit(vm_impl, next(...));
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
103 }
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
104
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
105 __code loaduvm_exit(struct vm_impl* vm_impl, __code next(...)) {
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
106
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
107 goto next(...);
247aa9ee931c loaduvm_loopvm_impl
tobaru
parents: 197
diff changeset
108 }
202
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
109
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
110 __code kpt_alloc_check_impl(struct vm_impl* vm_impl, __code next(...)) {
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
111
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
112 goto kpt_alloc_check_inital_page(vm_impl, next(...));
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
113 }
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
114
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
115 __code kpt_alloc_check_inital_page(struct vm_impl* vm_impl, __code next(...)) {
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
116 goto next((char*)r);
66db83ec1ec2 kpt_alloc_check_impl and freerange
tobaru
parents: 200
diff changeset
117 }