Fix MMU A and D flag handeling

This commit is contained in:
Dolu1990 2023-02-23 12:00:08 +01:00
parent 344b2d4eda
commit a40d5f19b2
5 changed files with 3612 additions and 4908 deletions

View File

@ -281,11 +281,11 @@ class MmuPlugin(ioRange : UInt => Bool,
when(port.entryToReplace === lineId){
val superPage = state === State.L1_RSP
line.valid := True
line.exception := dBusRsp.exception || (superPage && dBusRsp.pte.PPN0 =/= 0)
line.exception := dBusRsp.exception || (superPage && dBusRsp.pte.PPN0 =/= 0) || !dBusRsp.pte.A
line.virtualAddress := vpn
line.physicalAddress := Vec(dBusRsp.pte.PPN0, dBusRsp.pte.PPN1(9 downto 0))
line.allowRead := dBusRsp.pte.R
line.allowWrite := dBusRsp.pte.W
line.allowWrite := dBusRsp.pte.W && dBusRsp.pte.D
line.allowExecute := dBusRsp.pte.X
line.allowUser := dBusRsp.pte.U
line.superPage := state === State.L1_RSP

View File

@ -53,13 +53,13 @@ test5: //setup MMU
la x1, MMU_TABLE_0 + 0x800
la x2, MMU_TABLE_1
srli x2, x2, 2
ori x2, x2, 0x11
ori x2, x2, 0x1
sw x2, 0(x1)
la x1, MMU_TABLE_1 + 0x000*4
li x2, 0x80000000
srli x2, x2, 2
ori x2, x2, 0x0F
ori x2, x2, 0x0F | 0xC0
sw x2, 0(x1)
@ -67,50 +67,50 @@ test5: //setup MMU
la x1, MMU_TABLE_0 + 0x900
la x2, MMU_TABLE_2
srli x2, x2, 2
ori x2, x2, 0x11
ori x2, x2, 0x01
sw x2, 0(x1)
la x1, MMU_TABLE_2 + 0x00A*4
la x2, ROM_4
srli x2, x2, 2
ori x2, x2, 0x1F
ori x2, x2, 0x1F | 0xC0
sw x2, 0(x1)
la x1, MMU_TABLE_2 + 0x010*4 // Read Only
la x2, ROM_5
srli x2, x2, 2
ori x2, x2, 0x11 + (0x1 << 1)
ori x2, x2, 0x11 + (0x1 << 1) | 0xC0
sw x2, 0(x1)
la x1, MMU_TABLE_2 + 0x011*4 // Read Write
la x2, ROM_5
srli x2, x2, 2
ori x2, x2, 0x11 + (0x3 << 1)
ori x2, x2, 0x11 + (0x3 << 1) | 0xC0
sw x2, 0(x1)
la x1, MMU_TABLE_2 + 0x012*4 // Execute only
la x2, ROM_5
srli x2, x2, 2
ori x2, x2, 0x11 + (0x4 << 1)
ori x2, x2, 0x11 + (0x4 << 1) | 0xC0
sw x2, 0(x1)
la x1, MMU_TABLE_2 + 0x013*4 //Read Execute
la x2, ROM_5
srli x2, x2, 2
ori x2, x2, 0x11 + (0x5 << 1)
ori x2, x2, 0x11 + (0x5 << 1) | 0xC0
sw x2, 0(x1)
la x1, MMU_TABLE_2 + 0x014*4 //no user
la x2, ROM_5
srli x2, x2, 2
ori x2, x2, 0x01 + (0x7 << 1)
ori x2, x2, 0x01 + (0x7 << 1) | 0xC0
sw x2, 0(x1)
li TEST_ID, 5
la x1, MMU_TABLE_0 + 0xA00
la x2, ROM_SUPER_0
srli x2, x2, 2
ori x2, x2, 0x1F
ori x2, x2, 0x1F | 0xC0
sw x2, 0(x1)
li TEST_ID, 5
@ -124,7 +124,9 @@ test5: //setup MMU
srli x1, x1, 12
li x2, 0x80000000
or x1, x1, x2
sfence.vma
csrw satp, x1
fence.i
test6: //read through MMU
@ -254,8 +256,8 @@ test17: //Test limited write access
test18: //Test limited execute access
li TEST_ID, 18
la x1, test18_end
li TRAP_OK, 1
la TRAP_RET, test18_end
li x2, 0x90012010
jr x2
j fail
@ -305,7 +307,6 @@ test21: //supervisor accessing not user
bne x1, x2, fail
li x1, 1 << 18 //clear SUM
csrc sstatus, x1
li TRAP_OK, 1
@ -320,6 +321,7 @@ test21_pass:
test50: //User mode setup
li TEST_ID, 50
li TRAP_OK, 0
@ -331,10 +333,10 @@ test50: //User mode setup
//remap code tlb into userspace
csrr x10, satp
csrw satp, x0
la x1, MMU_TABLE_1 + 0x000*4
li x2, 0x80000000
la x1, MMU_TABLE_1 + 0x00D*4 //test51
li x2, 0x8000D000
srli x2, x2, 2
ori x2, x2, 0x1F
ori x2, x2, 0x1F | 0x40
sw x2, 0(x1)
csrw satp, x10
@ -347,83 +349,32 @@ test50: //User mode setup
sret
j fail
test51: //user read/write
li TEST_ID, 51
li x1, 0x90011008
li x2, 0x5B5A5958
lw x1, 0(x1)
bne x1, x2, fail
li x1, 0xA000A324
li x2, 0xAAEE0008
sw x2, 0(x1)
lw x1, 0(x1)
bne x1, x2, fail
test52: //user fetch
li TEST_ID, 52
la x1, test53
li x2, 0x90012010
jr x2
j fail
test53: // user fetch page fault
li TEST_ID, 53
li TRAP_OK, 1
la TRAP_RET, test54
li x1, 0x90014390
jr x1
j fail
test54: //user load page fault
li TEST_ID, 54
li TRAP_OK, 1
la TRAP_RET, test55
li x1, 0x90014394
lw x1, 0(x1)
j fail
test55: //user store page fault
li TEST_ID, 55
li TRAP_OK, 1
la TRAP_RET, test56
li x1, 0x90014398
sw x1, 0(x1)
j fail
test56:
j pass
fail: //TEST_ID => error code
li TRAP_OK, 0
la TRAP_RET, failFence
ecall
failFence:
li x2, 0xF00FFF24
sw TEST_ID, 0(x2)
pass:
li TRAP_OK, 2
li TRAP_OK, 1
la TRAP_RET, passFence
ecall
passFence:
li x2, 0xF00FFF20
sw x0, 0(x2)
.align 4
trap:
beq TRAP_OK, x0, failFence
beq TRAP_OK, x0, fail
csrr x1, mcause
csrr x1, mepc
csrr x1, mstatus
csrr x1, mbadaddr
li x1, 2
beq TRAP_OK, x1, passFence
beq TRAP_OK, x1, pass
csrw mepc, TRAP_RET
mret
@ -435,6 +386,7 @@ trap:
nop
nop
.align 12
MMU_TABLE_0:
.word 0
@ -508,19 +460,53 @@ ROM_7:
.word 0x7B7A7978
.word 0x7F7E7D7C
/*
.align 22
ROM_SUPER_0:
.word 0x83828180
.word 0x87868584
.word 0x8B8A8988
.word 0x8F8E8D8C
.align 12
ROM_SUPER_1:
.word 0x93929190
.word 0x97969594
.word 0x9B9A9998
.word 0x9F9E9D9C*/
test51: //user read/write
li TEST_ID, 51
li x1, 0x90011008
li x2, 0x5B5A5958
lw x1, 0(x1)
bne x1, x2, fail
li x1, 0xA000A324
li x2, 0xAAEE0008
sw x2, 0(x1)
lw x1, 0(x1)
bne x1, x2, fail
test52: //user fetch
li TEST_ID, 52
la x1, test53
li x2, 0x90012010
jr x2
j fail
test53: // user fetch page fault
li TEST_ID, 53
li TRAP_OK, 1
la TRAP_RET, test54
li x1, 0x90014390
jr x1
j fail
test54: //user load page fault
li TEST_ID, 54
li TRAP_OK, 1
la TRAP_RET, test55
li x1, 0x90014394
lw x1, 0(x1)
j fail
test55: //user store page fault
li TEST_ID, 55
li TRAP_OK, 1
la TRAP_RET, test56
li x1, 0x90014398
sw x1, 0(x1)
j fail
test56:
li TRAP_OK, 2
ecall

View File

@ -407,7 +407,10 @@ public:
uint32_t w : 1;
uint32_t x : 1;
uint32_t u : 1;
uint32_t _dummy : 5;
uint32_t g : 1;
uint32_t a : 1;
uint32_t d : 1;
uint32_t _dummy : 2;
uint32_t ppn : 22;
};
struct __attribute__((packed)){
@ -505,9 +508,9 @@ public:
}
if(!tlb.u && effectivePrivilege == 0) return true;
if( tlb.u && effectivePrivilege == 1 && !status.sum) return true;
if(superPage && tlb.ppn0 != 0) return true;
if(superPage && tlb.ppn0 != 0 || !tlb.a) return true;
if(kind == READ || kind == READ_WRITE) if(!tlb.r && !(status.mxr && tlb.x)) return true;
if(kind == WRITE || kind == READ_WRITE) if(!tlb.w) return true;
if(kind == WRITE || kind == READ_WRITE) if(!tlb.w || !tlb.d) return true;
if(kind == EXECUTE) if(!tlb.x) return true;
*p = (tlb.ppn1 << 22) | (superPage ? v & 0x3FF000 : tlb.ppn0 << 12) | (v & 0xFFF);
@ -628,6 +631,9 @@ public:
default: return true; break;
}
// if(csr == MSTATUS || csr == SSTATUS){
// printf("READ %x %x\n", pc, *value);
// }
return false;
}
@ -644,6 +650,9 @@ public:
virtual bool csrWrite(int32_t csr, uint32_t value){
if(((csr >> 8) & 0x3) > privilege) return true;
// if(csr == MSTATUS || csr == SSTATUS){
// printf("MIAOU %x %x\n", pc, value);
// }
switch(csr){
case MSTATUS: status.raw = value & 0x7FFFFFFF; break;
case MIP: ipSoft = value; break;
@ -675,6 +684,9 @@ public:
default: ilegalInstruction(); return true; break;
}
// if(csr == MSTATUS || csr == SSTATUS){
// printf(" %x %x\n", pc, status.raw);
// }
return false;
}

View File

@ -7,7 +7,7 @@ Disassembly of section .crt_section:
80000000 <_start>:
80000000: 00000e93 li t4,0
80000004: 00000097 auipc ra,0x0
80000008: 4ec08093 addi ra,ra,1260 # 800004f0 <trap>
80000008: 4fc08093 addi ra,ra,1276 # 80000500 <trap>
8000000c: 30509073 csrw mtvec,ra
80000010 <test1>:
@ -360,38 +360,38 @@ Disassembly of section .crt_section:
800004c0 <fail>:
800004c0: 00000e93 li t4,0
800004c4: 00000073 ecall
800004c4: 00000f17 auipc t5,0x0
800004c8: 00cf0f13 addi t5,t5,12 # 800004d0 <failFence>
800004cc: 00000073 ecall
800004c8 <failFence>:
800004c8: f0100137 lui sp,0xf0100
800004cc: f2410113 addi sp,sp,-220 # f00fff24 <test56+0x700f2e68>
800004d0: 01c12023 sw t3,0(sp)
800004d0 <failFence>:
800004d0: f0100137 lui sp,0xf0100
800004d4: f2410113 addi sp,sp,-220 # f00fff24 <test56+0x700f2e68>
800004d8: 01c12023 sw t3,0(sp)
800004d4 <pass>:
800004d4: 00200e93 li t4,2
800004d8: 00000073 ecall
800004dc <pass>:
800004dc: 00100e93 li t4,1
800004e0: 00000f17 auipc t5,0x0
800004e4: 00cf0f13 addi t5,t5,12 # 800004ec <passFence>
800004e8: 00000073 ecall
800004dc <passFence>:
800004dc: f0100137 lui sp,0xf0100
800004e0: f2010113 addi sp,sp,-224 # f00fff20 <test56+0x700f2e64>
800004e4: 00012023 sw zero,0(sp)
800004e8: 00000013 nop
800004ec: 00000013 nop
800004ec <passFence>:
800004ec: f0100137 lui sp,0xf0100
800004f0: f2010113 addi sp,sp,-224 # f00fff20 <test56+0x700f2e64>
800004f4: 00012023 sw zero,0(sp)
800004f8: 00000013 nop
800004fc: 00000013 nop
800004f0 <trap>:
800004f0: fc0e88e3 beqz t4,800004c0 <fail>
800004f4: 342020f3 csrr ra,mcause
800004f8: 341020f3 csrr ra,mepc
800004fc: 300020f3 csrr ra,mstatus
80000500: 343020f3 csrr ra,mtval
80000504: 00200093 li ra,2
80000508: fc1e86e3 beq t4,ra,800004d4 <pass>
8000050c: 341f1073 csrw mepc,t5
80000510: 30200073 mret
80000514: 00000013 nop
80000518: 00000013 nop
8000051c: 00000013 nop
80000520: 00000013 nop
80000500 <trap>:
80000500: fc0e80e3 beqz t4,800004c0 <fail>
80000504: 342020f3 csrr ra,mcause
80000508: 341020f3 csrr ra,mepc
8000050c: 300020f3 csrr ra,mstatus
80000510: 343020f3 csrr ra,mtval
80000514: 00200093 li ra,2
80000518: fc1e82e3 beq t4,ra,800004dc <pass>
8000051c: 341f1073 csrw mepc,t5
80000520: 30200073 mret
80000524: 00000013 nop
80000528: 00000013 nop
8000052c: 00000013 nop

File diff suppressed because it is too large Load Diff