-
Notifications
You must be signed in to change notification settings - Fork 3
/
kvirtmem.c
243 lines (208 loc) · 5.8 KB
/
kvirtmem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <assert.h>
#include <string.h>
#include "x86.h"
#include "kernel.h"
#include "dev/ata.h"
static inline u32
get_cr2()
{
u32 __force_order;
u32 val;
asm volatile("mov %%cr2,%0\n\t" : "=r" (val), "=m" (__force_order));
return val;
}
// high-water mark for allocated physical pages
static u32 nextpage = FIRST_PHYS_PAGE;
u32 get_phys_page()
{
nextpage += 0x1000;
return nextpage - 0x1000;
}
void
page_fault(u32 errcode, const struct registers *regs)
{
u32 faultaddr = get_cr2();
if (faultaddr < 0x1000) {
dump_regs(regs);
kprintf("NULL page fault at 0x%x\r\n", faultaddr);
halt();
}
if (errcode & 0x2) { // caused by page write
if (PAGE_DIR[faultaddr >> 22] & 0x1) {
if ((PAGE_TABLES[faultaddr >> 12] & 0x3) == 0x1) {
// present but not writable
kprintf("invalid write to 0x%08X\r\n", faultaddr);
halt();
}
}
}
DPRINT(3, "page fault at 0x%x", faultaddr);
int disknum = -1;
int pagenum = -1;
if ((errcode & 0x1) == 0x0) { // not present
// check for valid entry in PAGE_DIR
if ((PAGE_DIR[faultaddr >> 22] & 0x1) == 0) // not present
{
PAGE_DIR[faultaddr >> 22] = get_phys_page() | 0x3;
}
else
{
u32 entry = PAGE_TABLES[faultaddr >> 12];
if ((entry & 0x2) == 0x2) // on disk
{
disknum = (entry >> 2) & 0x3;
pagenum = entry >> 4;
}
// fill in page table entry with free page
PAGE_TABLES[faultaddr >> 12] = get_phys_page() | 0x3; // RW + PRESENT
}
}
void *pageaddr = (void *) (faultaddr & 0xfffff000);
if (disknum == -1) {
memset(pageaddr, 0, 4096);
} else {
if (hdd_read_page(disknum, pagenum, pageaddr)) {
kprintf("unable to read 0x%08X from disk%d (page#=%d)\r\n", pageaddr, disknum, pagenum);
halt();
}
}
#if 0
// after reading the page from disk, make the page read-only if
// it comes from disk0
if (disknum == 0) {
PAGE_TABLES[faultaddr >> 12] &= ~0x2; // not writable
}
#endif
}
int
write_sector(const void *ptr)
{
if (ptr < (void *) SAVEDISK_ADDR || ptr > (void *) SAVEDISK_ADDR_MAX) {
kprintf("ptr to write (0x%08X) not on disk1", ptr);
return -1;
}
uint32_t diskoffset = (uint32_t) ptr - SAVEDISK_ADDR;
int lba = diskoffset >> 9;
void *src = (void *) (((uint32_t) ptr) & 0xfffffe00);
DPRINT(1, "writing sector %u from 0x%x", lba, src);
#if 0
int rc = reg_pio_data_out_lba28(1 // writable device
, CMD_WRITE_SECTORS
, 0 // feature
, 1 // sectorCount
, lba // LBA
, src // bufAddr
, 1 // numSect
, 0 // multiCnt
);
if (rc != 0) {
kprintf("unable to write sector to disk");
return -1;
}
#endif
return 0;
}
int
write_page(u32 ptr)
{
if (ptr < SAVEDISK_ADDR || ptr > SAVEDISK_ADDR_MAX) {
kprintf("page to write (0x%08X) not on disk1", ptr);
return -1;
}
void *src = (void *) (ptr & 0xfffff000);
uint32_t diskoffset = (uint32_t) src - SAVEDISK_ADDR;
int lba = diskoffset >> 9;
DPRINT(0, "writing page at lba %u from 0x%x", lba, src);
#if 0
int rc = reg_pio_data_out_lba28(1 // writable device
, CMD_WRITE_SECTORS
, 0 // feature
, 8 // sectorCount
, lba // LBA
, src // bufAddr
, 8 // numSect
, 0 // multiCnt
);
if (rc != 0) {
kprintf("unable to write page to disk");
return -1;
}
#endif
return 0;
}
int
is_dirty(u32 addr)
{
return (PAGE_DIR[addr >> 22] & 0x01) // page table itself is Present
&& (PAGE_TABLES[addr >> 12] & 0x40); // DIRTY bit is set
}
int
ksync()
{
int ret = 0;
u32 ptr = SAVEDISK_ADDR;
for ( ; ptr < SAVEDISK_ADDR_MAX; ptr += 4096)
{
if (is_dirty(ptr)) {
DPRINT(1, "page at 0x%08X is dirty, writing to disk", ptr);
if (write_page(ptr) != 0)
ret = -1; // but keep writing the rest
}
}
return ret;
}
// makes sure the PAGE_DIR entry exists first
void *
set_pt_entry(u32 addr, u32 val)
{
if (PAGE_DIR[addr >> 22] == 0) {
PAGE_DIR[addr >> 22] = get_phys_page() | 0x3;
}
PAGE_TABLES[addr >> 12] = val;
return (void *) addr;
}
u32 get_pt_entry(u32 addr)
{
return PAGE_TABLES[addr >> 12];
}
void *
map(void *dest, const void *src, size_t len)
{
u32 d = (u32) dest;
u32 s = (u32) src;
size_t i=0;
while (i < len)
{
// assert (get_pt_entry(d + i) == 0);
set_pt_entry(d + i, get_pt_entry(s + i));
i += 4096;
}
return dest;
}
void
map_phys(void *virtaddr, u32 physaddr, size_t len)
{
size_t i;
for (i=0; i < len; i += 4096)
{
u32 val = (physaddr + i*4096) | 0x3;
set_pt_entry((u32) virtaddr + i*4096, val);
}
}
int
map_disk(int disknum, unsigned long addr)
{
const ata_disk *d = &disks[disknum];
int npages = disks[disknum].max_lba * disks[disknum].sector_size / 4096;
int i;
for (i=0; i < npages; ++i)
{
u32 val = (i << 4) | (disknum << 2) | 0x2; // not-present, on gamedisk
set_pt_entry(addr + i*4096, val);
}
return 0;
}
void
init_pagetable()
{
}