Skip to content

Commit 349e906

Browse files
committed
Add vc memory in both parent and child, change access policy to USER_RW
1 parent ed4e9f6 commit 349e906

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

lab6/kernel/include/mmu.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
#define PD_BLOCK 0b01
2323
#define PD_PAGE 0b11
2424
#define PD_ACCESS (1 << 10)
25+
#define PD_USER_RW (0b01 << 6)
26+
#define PD_USER_R (0b11 << 6)
27+
#define PD_UXN (1L << 54)
28+
#define PD_PXN (1L << 53)
29+
2530
#define BOOT_PGD_ATTR (PD_TABLE)
2631
#define BOOT_PUD_ATTR (PD_TABLE)
2732
#define BOOT_PMD_ATTR (PD_TABLE)
@@ -41,6 +46,6 @@
4146

4247
void init_page_table(thread_info *thread, uint64_t **table);
4348
void update_page_table(thread_info *thread, uint64_t virtual_addr,
44-
uint64_t physical_addr, int permission);
49+
uint64_t physical_addr, uint64_t permission);
4550
uint64_t el0_VA2PA(thread_info *thread, uint64_t virtual_addr);
4651

lab6/kernel/src/exception.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ void enable_interrupt() { asm volatile("msr DAIFClr, 0xf"); }
1313
void disable_interrupt() { asm volatile("msr DAIFSet, 0xf"); }
1414

1515
void sync_handler_currentEL_ELx() {
16-
// printf("[sync_handler_currentEL_ELx]\n");
16+
printf("[sync_handler_currentEL_ELx]\n");
1717

1818
uint64_t spsr_el1, elr_el1, esr_el1;
1919
asm volatile("mrs %0, spsr_el1" : "=r"(spsr_el1));
2020
asm volatile("mrs %0, elr_el1" : "=r"(elr_el1));
2121
asm volatile("mrs %0, esr_el1" : "=r"(esr_el1));
22+
uint32_t ec = (esr_el1 >> 26) & 0x3f;
23+
printf("EC: %x\n", ec);
24+
while(1){;}
2225
// printf("SPSR_EL1: 0x%08x\n", spsr_el1);
2326
// printf("ELR_EL1: 0x%08x\n", elr_el1);
2427
// printf("ESR_EL1: 0x%08x\n", esr_el1);
@@ -37,6 +40,7 @@ void sync_handler_lowerEL_64(uint64_t sp) {
3740

3841
uint32_t ec = (esr_el1 >> 26) & 0x3f;
3942
// printf("EC: %x\n", ec);
43+
4044
if (ec == 0b010101) { // SVC instruction
4145
uint64_t iss;
4246
asm volatile("mov %0, x8" : "=r"(iss));
@@ -61,8 +65,13 @@ void sync_handler_lowerEL_64(uint64_t sp) {
6165
const char **argv = (const char **)trap_frame->x[1];
6266
exec(program_name, argv);
6367
} else if (iss == 4) { // fork
64-
// printf("[fork]\n");
6568
fork(sp);
69+
// User shell need excute after child mbox_call in qemu
70+
// But in real machine can work without below code
71+
if(get_current()->pid==1){
72+
int magic_sleep = 50000000;
73+
while(magic_sleep--){}
74+
}
6675
} else if (iss == 5) { // exit
6776
exit();
6877
} else if (iss == 6) { // mbox_call

lab6/kernel/src/mmu.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void init_page_table(thread_info *thread, uint64_t **table) {
1717
}
1818

1919
void update_page_table(thread_info *thread, uint64_t virtual_addr,
20-
uint64_t physical_addr, int permission) {
20+
uint64_t physical_addr, uint64_t flags) {
2121
if (thread->pgd == 0) {
2222
printf("Invalid PGD!!\n");
2323
return;
@@ -39,20 +39,15 @@ void update_page_table(thread_info *thread, uint64_t virtual_addr,
3939
for (int level = 0; level < 3; level++) {
4040
if (table[index[level]] == 0) {
4141
// printf("level: %d, index: 0x%llx ", level, index[level]);
42-
init_page_table(thread, (uint64_t **)&(table[index[level]])); table[index[level]] |= PD_TABLE;
42+
init_page_table(thread, (uint64_t **)&(table[index[level]]));
43+
table[index[level]] |= PD_ACCESS | (MAIR_IDX_NORMAL_NOCACHE << 2) | PD_TABLE;
4344
}
4445
// printf("table PA: 0x%llx\n", (uint64_t)table[index[level]]);
4546
table = (uint64_t *)PA2VA(table[index[level]] & ~0xfff);
4647
// printf("table VA: 0x%llx\n", (uint64_t)table);
4748
}
48-
uint64_t BOOT_RWX_ATTR = (1 << 6);
49-
if (permission & 0b010)
50-
BOOT_RWX_ATTR |= 0;
51-
else
52-
BOOT_RWX_ATTR |= (1 << 7);
53-
// printf("0x%llx\n", BOOT_RWX_ATTR);
5449
table[index[3]] =
55-
physical_addr | BOOT_PTE_NORMAL_NOCACHE_ATTR | BOOT_RWX_ATTR;
50+
physical_addr | BOOT_PTE_NORMAL_NOCACHE_ATTR | flags;
5651
// printf("page PA: 0x%llx\n", (uint64_t)table[index[3]]);
5752
}
5853

lab6/kernel/src/thread.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,26 @@ void exec(const char *program_name, const char **argv) {
192192
for (uint64_t size = 0; size < cur->user_program_size; size += PAGE_SIZE) {
193193
uint64_t virtual_addr = USER_PROGRAM_BASE + size;
194194
uint64_t physical_addr = VA2PA(cur->user_program_base + size);
195-
if(size==0)
196-
printf("[first_map]va:%p map to pa:%p\n",virtual_addr,physical_addr);
197-
update_page_table(cur, virtual_addr, physical_addr, 0b101);
195+
update_page_table(cur, virtual_addr, physical_addr, PD_USER_RW);
198196
}
199197

200198
// map vc memory
201199
for (uint64_t size = 0; size < PERIPHERAL_END - PERIPHERAL_START; size += PAGE_SIZE) {
202200
uint64_t identity_page = PERIPHERAL_START + size;
203201
// printf("identity_page=%p\n",identity_page);
204-
update_page_table(cur, identity_page , identity_page, 0b101);
202+
update_page_table(cur, identity_page , identity_page, PD_USER_RW);
205203
}
206-
el0_VA2PA(cur,USER_PROGRAM_BASE);
207-
el0_VA2PA(cur,0x3f000000);
208-
el0_VA2PA(cur,0x3f000000-1);
209-
el0_VA2PA(cur,0x3c25e76c);
210-
el0_VA2PA(cur,0x3c100000);
211-
el0_VA2PA(cur,0x3c000000);
212-
el0_VA2PA(cur,0x3c000000-1);
213204
uint64_t virtual_addr = USER_STACK_BASE;
214205
uint64_t physical_addr = VA2PA(cur->user_stack_base);
215-
update_page_table(cur, virtual_addr, physical_addr, 0b110);
206+
update_page_table(cur, virtual_addr, physical_addr, PD_USER_RW );
207+
208+
// el0_VA2PA(cur,USER_PROGRAM_BASE);
209+
// el0_VA2PA(cur,0x3f000000);
210+
// el0_VA2PA(cur,0x3f000000-1);
211+
// el0_VA2PA(cur,0x3c25e76c);
212+
// el0_VA2PA(cur,0x3c100000);
213+
// el0_VA2PA(cur,0x3c000000);
214+
// el0_VA2PA(cur,0x3c000000-1);
216215

217216
uint64_t next_pgd = (uint64_t)cur->pgd;
218217
switch_pgd(next_pgd);
@@ -262,11 +261,25 @@ void create_child(thread_info *parent, thread_info *child) {
262261
for (uint64_t size = 0; size < child->user_program_size; size += PAGE_SIZE) {
263262
uint64_t virtual_addr = USER_PROGRAM_BASE + size;
264263
uint64_t physical_addr = VA2PA(child->user_program_base + size);
265-
update_page_table(child, virtual_addr, physical_addr, 0b101);
264+
update_page_table(child, virtual_addr, physical_addr, PD_USER_RW);
265+
}
266+
267+
for (uint64_t size = 0; size < PERIPHERAL_END - PERIPHERAL_START; size += PAGE_SIZE) {
268+
uint64_t identity_page = PERIPHERAL_START + size;
269+
// printf("identity_page=%p\n",identity_page);
270+
update_page_table(child, identity_page , identity_page, PD_USER_RW);
266271
}
267272
uint64_t virtual_addr = USER_STACK_BASE;
268273
uint64_t physical_addr = VA2PA(child->user_stack_base);
269-
update_page_table(child, virtual_addr, physical_addr, 0b110);
274+
update_page_table(child, virtual_addr, physical_addr, PD_USER_RW);
275+
276+
// el0_VA2PA(child,USER_PROGRAM_BASE);
277+
// el0_VA2PA(child,0x3f000000);
278+
// el0_VA2PA(child,0x3f000000-1);
279+
// el0_VA2PA(child,0x3c25e76c);
280+
// el0_VA2PA(child,0x3c100000);
281+
// el0_VA2PA(child,0x3c000000);
282+
// el0_VA2PA(child,0x3c000000-1);
270283

271284
char *src, *dst;
272285
// copy saved context in thread info

0 commit comments

Comments
 (0)