-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.patch
399 lines (383 loc) · 14.5 KB
/
lab2.patch
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/.check_pmm.log lab2/.check_pmm.log
--- /home/amadeus/ucore_os_lab/labcodes/lab2/.check_pmm.log 1970-01-01 08:00:00.000000000 +0800
+++ lab2/.check_pmm.log 2018-03-15 16:25:42.555288982 +0800
@@ -0,0 +1,58 @@
+(THU.CST) os is loading ...
+
+Special kernel symbols:
+ entry 0xc010002a (phys)
+ etext 0xc0105e42 (phys)
+ edata 0xc0117a36 (phys)
+ end 0xc0118988 (phys)
+Kernel executable memory footprint: 99KB
+ebp:0xc0116f48 eip:0xc0100a52 args:0x00010094 0x00010094 0xc0116f78 0xc01000a9
+ kern/debug/kdebug.c:309: print_stackframe+22
+ebp:0xc0116f58 eip:0xc0100d3e args:0x00000000 0x00000000 0x00000000 0xc0116fc8
+ kern/debug/kmonitor.c:129: mon_backtrace+10
+ebp:0xc0116f78 eip:0xc01000a9 args:0x00000000 0xc0116fa0 0xffff0000 0xc0116fa4
+ kern/init/init.c:49: grade_backtrace2+19
+ebp:0xc0116f98 eip:0xc01000cb args:0x00000000 0xffff0000 0xc0116fc4 0x00000029
+ kern/init/init.c:54: grade_backtrace1+27
+ebp:0xc0116fb8 eip:0xc01000e8 args:0x00000000 0xc010002a 0xffff0000 0xc010006d
+ kern/init/init.c:59: grade_backtrace0+19
+ebp:0xc0116fd8 eip:0xc0100109 args:0x00000000 0x00000000 0x00000000 0xc0105e60
+ kern/init/init.c:64: grade_backtrace+26
+ebp:0xc0116ff8 eip:0xc010007a args:0x00000000 0x00000000 0x0000ffff 0x40cf9a00
+ kern/init/init.c:29: kern_init+79
+memory management: default_pmm_manager
+e820map:
+ memory: 0009fc00, [00000000, 0009fbff], type = 1.
+ memory: 00000400, [0009fc00, 0009ffff], type = 2.
+ memory: 00010000, [000f0000, 000fffff], type = 2.
+ memory: 07ee0000, [00100000, 07fdffff], type = 1.
+ memory: 00020000, [07fe0000, 07ffffff], type = 2.
+ memory: 00040000, [fffc0000, ffffffff], type = 2.
+check_alloc_page() succeeded!
+check_pgdir() succeeded!
+check_boot_pgdir() succeeded!
+-------------------- BEGIN --------------------
+PDE(0e0) c0000000-f8000000 38000000 urw
+ |-- PTE(38000) c0000000-f8000000 38000000 -rw
+PDE(001) fac00000-fb000000 00400000 -rw
+ |-- PTE(000e0) faf00000-fafe0000 000e0000 urw
+ |-- PTE(00001) fafeb000-fafec000 00001000 -rw
+--------------------- END ---------------------
+++ setup timer interrupts
+100 ticks
+End of Test.
+ebp:0xc0116f40 eip:0xc0100a52 args:0xc0116f78 0xc010025f 0xc0116f70 0xc0101e2f
+ kern/debug/kdebug.c:309: print_stackframe+22
+ebp:0xc0116f50 eip:0xc0101888 args:0xc0106191 0xc0116fb4 0x00000000 0x00000400
+ kern/trap/trap.c:18: print_ticks+44
+ebp:0xc0116f70 eip:0xc0101e2f args:0xc0116f9c 0xc0100282 0xc0106176 0xc0116fb4
+ kern/trap/trap.c:167: trap_dispatch+101
+ebp:0xc0116f90 eip:0xc0101eeb args:0xc0116f9c 0x0000807c 0x00010094 0xc0116ff8
+ kern/trap/trap.c:205: trap+16
+ebp:0xc0116ff8 eip:0xc0102977 args:0x00000000 0x00000000 0x0000ffff 0x40cf9a00
+ kern/trap/trapentry.S:24: <unknown>+0
+kernel panic at kern/trap/trap.c:18:
+ EOT: kernel seems ok.
+Welcome to the kernel debug monitor!!
+Type 'help' for a list of commands.
+K>
\ No newline at end of file
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/.gitignore lab2/.gitignore
--- /home/amadeus/ucore_os_lab/labcodes/lab2/.gitignore 1970-01-01 08:00:00.000000000 +0800
+++ lab2/.gitignore 2018-03-15 16:42:56.089367252 +0800
@@ -0,0 +1,8 @@
+bin
+bin/*
+./bin/*
+obj
+obj/*
+./obj/*
+tags
+
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/kern/debug/assert.h lab2/kern/debug/assert.h
--- /home/amadeus/ucore_os_lab/labcodes/lab2/kern/debug/assert.h 2018-03-15 16:55:42.060056646 +0800
+++ lab2/kern/debug/assert.h 2018-03-15 16:25:42.555288982 +0800
@@ -10,6 +10,7 @@
__warn(__FILE__, __LINE__, __VA_ARGS__)
#define panic(...) \
+ print_stackframe(); \
__panic(__FILE__, __LINE__, __VA_ARGS__)
#define assert(x) \
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/kern/debug/kdebug.c lab2/kern/debug/kdebug.c
--- /home/amadeus/ucore_os_lab/labcodes/lab2/kern/debug/kdebug.c 2018-03-15 16:55:42.060056646 +0800
+++ lab2/kern/debug/kdebug.c 2018-03-15 16:25:42.559288927 +0800
@@ -293,7 +293,7 @@
* */
void
print_stackframe(void) {
- /* LAB1 YOUR CODE : STEP 1 */
+ /* LAB1 2015011358 : STEP 1 */
/* (1) call read_ebp() to get the value of ebp. the type is (uint32_t);
* (2) call read_eip() to get the value of eip. the type is (uint32_t);
* (3) from 0 .. STACKFRAME_DEPTH
@@ -305,5 +305,15 @@
* NOTICE: the calling funciton's return addr eip = ss:[ebp+4]
* the calling funciton's ebp = ss:[ebp]
*/
+ uint32_t ebp = read_ebp();
+ uint32_t ra = read_eip();
+ for (int i = 0; i < STACKFRAME_DEPTH && ebp != 0; ++ i) {
+ cprintf("ebp:0x%08x eip:0x%08x ", ebp, ra);
+ uint32_t* ptr = (uint32_t *) (ebp + 8);
+ cprintf("args:0x%08x 0x%08x 0x%08x 0x%08x\n", ptr[0], ptr[1], ptr[2], ptr[3]);
+ print_debuginfo(ra - 1);
+ ra = *((uint32_t *) (ebp + 4));
+ ebp = *((uint32_t *) ebp);
+ }
}
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/kern/mm/default_pmm.c lab2/kern/mm/default_pmm.c
--- /home/amadeus/ucore_os_lab/labcodes/lab2/kern/mm/default_pmm.c 2018-03-15 16:55:42.060056646 +0800
+++ lab2/kern/mm/default_pmm.c 2018-03-15 16:25:42.563288871 +0800
@@ -9,7 +9,7 @@
usually split, and the remainder added to the list as another free block.
Please see Page 196~198, Section 8.2 of Yan Wei Min's chinese book "Data Structure -- C programming language"
*/
-// LAB2 EXERCISE 1: YOUR CODE
+// LAB2 EXERCISE 1: 2015011358
// you should rewrite functions: default_init,default_init_memmap,default_alloc_pages, default_free_pages.
/*
* Details of FFMA
@@ -73,6 +73,7 @@
assert(PageReserved(p));
p->flags = p->property = 0;
set_page_ref(p, 0);
+ SetPageProperty(p);
}
base->property = n;
SetPageProperty(base);
@@ -82,6 +83,7 @@
static struct Page *
default_alloc_pages(size_t n) {
+ //cprintf("alloc\n");
assert(n > 0);
if (n > nr_free) {
return NULL;
@@ -96,6 +98,20 @@
}
}
if (page != NULL) {
+ for (struct Page *p = page; p != (page + n); ++p) {
+ ClearPageProperty(p);
+ }
+ if (page->property > n) {
+ struct Page *p = page + n;
+ p->property = page->property - n;
+ list_add(&(page->page_link), &(p->page_link));
+ }
+ list_del(&(page->page_link));
+ nr_free -= n;
+ //ClearPageProperty(page);
+ }
+ /*
+ if (page != NULL) {
list_del(&(page->page_link));
if (page->property > n) {
struct Page *p = page + n;
@@ -105,38 +121,52 @@
nr_free -= n;
ClearPageProperty(page);
}
+ */
+ //cprintf("\\alloc\n");
return page;
}
+static bool
+merge_backward(struct Page *base) {
+ list_entry_t *le = list_next(&(base->page_link));
+ //cprintf("base: %08x le: %08x free_list: %08x\n", &(base->page_link), le, &free_list);
+ if (le == &free_list) return 0;
+ struct Page *p = le2page(le, page_link);
+ if (PageProperty(p) == 0) return 0;
+ if (base + base->property != p) return 0;
+ base->property += p->property;
+ p->property = 0;
+ list_del(le);
+ //cprintf("************ merged! ************\n");
+ return 1;
+}
+
static void
default_free_pages(struct Page *base, size_t n) {
+ //cprintf("free\n");
assert(n > 0);
struct Page *p = base;
- for (; p != base + n; p ++) {
+ for (; p != (base + n); p ++) {
assert(!PageReserved(p) && !PageProperty(p));
- p->flags = 0;
+ SetPageProperty(p);
set_page_ref(p, 0);
}
base->property = n;
- SetPageProperty(base);
+ //SetPageProperty(base);
list_entry_t *le = list_next(&free_list);
- while (le != &free_list) {
- p = le2page(le, page_link);
- le = list_next(le);
- if (base + base->property == p) {
- base->property += p->property;
- ClearPageProperty(p);
- list_del(&(p->page_link));
- }
- else if (p + p->property == base) {
- p->property += base->property;
- ClearPageProperty(base);
- base = p;
- list_del(&(p->page_link));
- }
- }
+ //cprintf("to find position\n");
+ for (; le != (&free_list) && le < (&(base->page_link)); le = list_next(le));
+ //cprintf("found position\n");
+ list_add_before(le, &(base->page_link));
nr_free += n;
- list_add(&free_list, &(base->page_link));
+ //cprintf("to merge backward\n");
+ while (merge_backward(base));
+ //cprintf("merged backward\n");
+ for (list_entry_t *i = list_prev(&(base->page_link)); i!= &free_list; i = list_prev(i)) {
+ if (!merge_backward(le2page(i, page_link))) break;
+ }
+ //cprintf("\\free\n");
+
}
static size_t
@@ -207,6 +237,7 @@
count ++, total += p->property;
}
assert(total == nr_free_pages());
+ //cprintf("count = %08x\n", count);
basic_check();
@@ -232,6 +263,7 @@
p2 = p0 + 1;
free_page(p0);
free_pages(p1, 3);
+ //cprintf("%08x %08x\n", PageProperty(p0), p0->property);
assert(PageProperty(p0) && p0->property == 1);
assert(PageProperty(p1) && p1->property == 3);
@@ -255,7 +287,9 @@
while ((le = list_next(le)) != &free_list) {
struct Page *p = le2page(le, page_link);
count --, total -= p->property;
+ //cprintf("Page: %08x %08x %08x size: %08x\n", p, p + p->property, PageProperty(p), p->property);
}
+ //cprintf("count = %08x\n", count);
assert(count == 0);
assert(total == 0);
}
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/kern/mm/pmm.c lab2/kern/mm/pmm.c
--- /home/amadeus/ucore_os_lab/labcodes/lab2/kern/mm/pmm.c 2018-03-15 16:55:42.060056646 +0800
+++ lab2/kern/mm/pmm.c 2018-03-15 16:25:42.563288871 +0800
@@ -296,6 +296,7 @@
page_init();
//use pmm->check to verify the correctness of the alloc/free function in a pmm
+ //cprintf("to check alloc page\n");
check_alloc_page();
// create boot_pgdir, an initial page directory(Page Directory Table, PDT)
@@ -347,7 +348,7 @@
// return vaule: the kernel virtual address of this pte
pte_t *
get_pte(pde_t *pgdir, uintptr_t la, bool create) {
- /* LAB2 EXERCISE 2: YOUR CODE
+ /* LAB2 EXERCISE 2: 2015011358
*
* If you need to visit a physical address, please use KADDR()
* please read pmm.h for useful macros
@@ -379,7 +380,26 @@
// (7) set page directory entry's permission
}
return NULL; // (8) return page table entry
-#endif
+#endif
+ /*
+ pte_t *pt_addr;
+ if (pgdir[(PDX(la))] & PTE_P) {
+ pt_addr = (pte_t *)(KADDR(pgdir[(PDX(la))] & 0XFFFFF000));
+ return &pt_addr[(PTX(la))];
+ }*/
+ pde_t *pdep = pgdir + PDX(la); // get the page-dir entry
+ //cprintf("%08x\n", *pdep & PTE_P);
+ pte_t *ptep = ((pte_t *) (KADDR(*pdep & ~0XFFF)) + PTX(la));
+ //cprintf("%08x\n", *ptep & PTE_P);
+ if (*pdep & PTE_P) return ptep; // check if the entry is presented
+ if (!create) return NULL; // check if create a new page-table
+ struct Page* pt = alloc_page(); // allocate a page frame for the page-table
+ if (pt == NULL) return NULL;
+ set_page_ref(pt, 1);
+ ptep = KADDR(page2pa(pt)); // get the virtual address of page pt
+ memset(ptep, 0, PGSIZE);
+ *pdep = (page2pa(pt) & ~0XFFF) | PTE_U | PTE_W | PTE_P;
+ return ptep + PTX(la);
}
//get_page - get related Page struct for linear address la using PDT pgdir
@@ -400,7 +420,7 @@
//note: PT is changed, so the TLB need to be invalidate
static inline void
page_remove_pte(pde_t *pgdir, uintptr_t la, pte_t *ptep) {
- /* LAB2 EXERCISE 3: YOUR CODE
+ /* LAB2 EXERCISE 3: 2015011358
*
* Please check if ptep is valid, and tlb must be manually updated if mapping is updated
*
@@ -425,6 +445,12 @@
//(6) flush tlb
}
#endif
+ assert(*ptep & PTE_P); // check if pte is valid
+ struct Page *page = pte2page(*ptep);
+ page->ref --;
+ if (!page->ref) free_page(page);
+ *ptep &= (~PTE_P);
+ tlb_invalidate(pgdir, la);
}
//page_remove - free an Page which is related linear address la and has an validated pte
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/kern/trap/trap.c lab2/kern/trap/trap.c
--- /home/amadeus/ucore_os_lab/labcodes/lab2/kern/trap/trap.c 2018-03-15 16:55:42.060056646 +0800
+++ lab2/kern/trap/trap.c 2018-03-15 16:25:42.563288871 +0800
@@ -34,7 +34,7 @@
/* idt_init - initialize IDT to each of the entry points in kern/trap/vectors.S */
void
idt_init(void) {
- /* LAB1 YOUR CODE : STEP 2 */
+ /* LAB1 2015011358: STEP 2 */
/* (1) Where are the entry addrs of each Interrupt Service Routine (ISR)?
* All ISR's entry addrs are stored in __vectors. where is uintptr_t __vectors[] ?
* __vectors[] is in kern/trap/vector.S which is produced by tools/vector.c
@@ -46,6 +46,18 @@
* You don't know the meaning of this instruction? just google it! and check the libs/x86.h to know more.
* Notice: the argument of lidt is idt_pd. try to find it!
*/
+ extern uintptr_t __vectors[];
+ uint32_t kcs = GD_KTEXT; // kernel OS code segment selector
+ for (int i = 0; i < 256; ++ i) {
+ if (i == T_SYSCALL) {
+ SETGATE(idt[i], 1, kcs, __vectors[i], DPL_USER);
+ } else {
+ SETGATE(idt[i], 0, kcs, __vectors[i], DPL_KERNEL);
+ }
+ }
+ // challenge 1:
+ SETGATE(idt[T_SWITCH_TOK], 1, kcs, __vectors[T_SWITCH_TOK], DPL_USER);
+ lidt(&idt_pd);
}
static const char *
@@ -134,6 +146,8 @@
cprintf(" eax 0x%08x\n", regs->reg_eax);
}
+static uint32_t clock_count = 0;
+
/* trap_dispatch - dispatch based on what type of trap occurred */
static void
trap_dispatch(struct trapframe *tf) {
@@ -141,12 +155,18 @@
switch (tf->tf_trapno) {
case IRQ_OFFSET + IRQ_TIMER:
- /* LAB1 YOUR CODE : STEP 3 */
+ /* LAB1 2015011358 : STEP 3 */
/* handle the timer interrupt */
/* (1) After a timer interrupt, you should record this event using a global variable (increase it), such as ticks in kern/driver/clock.c
* (2) Every TICK_NUM cycle, you can print some info using a funciton, such as print_ticks().
* (3) Too Simple? Yes, I think so!
*/
+ clock_count ++;
+ if (clock_count == TICK_NUM) {
+ clock_count = 0;
+ print_ticks();
+ //print_trapframe(tf);
+ }
break;
case IRQ_OFFSET + IRQ_COM1:
c = cons_getc();
diff -r -u -P /home/amadeus/ucore_os_lab/labcodes/lab2/tools/gdbinit lab2/tools/gdbinit
--- /home/amadeus/ucore_os_lab/labcodes/lab2/tools/gdbinit 2018-03-15 16:55:42.060056646 +0800
+++ lab2/tools/gdbinit 2018-03-15 16:25:42.563288871 +0800
@@ -1,3 +1,3 @@
file bin/kernel
target remote :1234
-break kern_init
+break default_check