/* Traps and the machine CSR file. */ #include #include "machine_internal.h" /* Implemented CSR numbers. */ enum { CSR_MSTATUS = 0x300, CSR_MIE = 0x304, CSR_MTVEC = 0x305, CSR_MSCRATCH = 0x340, CSR_MEPC = 0x341, CSR_MCAUSE = 0x342, CSR_MTVAL = 0x343, CSR_MIP = 0x344, CSR_MINSTRET = 0xb02, CSR_INSTRET = 0xc02, CSR_MHARTID = 0xf14, }; void machine_trap( struct machine *m, struct machine_hart *h, u64 cause, u64 value ) { u64 old = h->mstatus; h->mstatus = (old & ~(MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_MIE)) | ((u64)h->privilege << 11) | ((old & MSTATUS_MIE) << 4); h->mepc = h->pc & ~3ULL; h->mcause = cause; h->mtval = value; h->privilege = 3; h->waiting = false; h->reserved = false; if (m->config->trace) fprintf( stderr, "hart=%u trap pc=0x%llx cause=0x%llx value=0x%llx\n", h->id, h->pc, cause, value ); h->pc = h->mtvec & ~3ULL; if ((cause & MCAUSE_INTERRUPT) && (h->mtvec & 3) == 1) h->pc += 4 * (cause & ~MCAUSE_INTERRUPT); } /* Keep the implemented mstatus fields; MPP holds only M or U. */ static u64 status_warl(u64 value) { u64 result = value & (MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_MPP | (3ULL << 9)); if ((result & MSTATUS_MPP) != MSTATUS_MPP) result &= ~MSTATUS_MPP; return result; } static bool csr_read( struct machine *m, struct machine_hart *h, u32 csr, u64 *value ) { if (h->privilege < ((csr >> 8) & 3)) return false; if (csr == CSR_INSTRET && h->privilege == 0) return false; /* mcounteren is zero. */ switch (csr) { case CSR_MSTATUS: *value = h->mstatus | (2ULL << 32) | (((h->mstatus >> 9) & 3) == 3 ? 1ULL << 63 : 0); /* UXL: U mode is 64-bit. */ break; case CSR_MIE: *value = h->mie; break; case CSR_MTVEC: *value = h->mtvec; break; case CSR_MSCRATCH: *value = h->mscratch; break; case CSR_MEPC: *value = h->mepc; break; case CSR_MCAUSE: *value = h->mcause; break; case CSR_MTVAL: *value = h->mtval; break; case CSR_MIP: *value = machine_pending(m, h); break; case CSR_MINSTRET: case CSR_INSTRET: *value = h->minstret; break; case CSR_MHARTID: *value = h->id; break; default: return false; } return true; } static void csr_write(struct machine_hart *h, u32 csr, u64 value) { switch (csr) { case CSR_MSTATUS: h->mstatus = status_warl(value); break; case CSR_MIE: h->mie = value & MIP_ALL; break; case CSR_MTVEC: h->mtvec = (value & ~3ULL) | ((value & 3) == 1 ? 1 : 0); break; case CSR_MSCRATCH: h->mscratch = value; break; case CSR_MEPC: h->mepc = value & ~3ULL; break; case CSR_MCAUSE: h->mcause = value; break; case CSR_MTVAL: h->mtval = value; break; case CSR_MIP: break; /* MSIP/MTIP/MEIP are device-owned read-only fields. */ case CSR_MINSTRET: /* An explicit write suppresses this instruction's increment. */ h->minstret = value; h->wrote_instret = true; break; } } bool machine_csr(struct machine *m, struct machine_hart *h, u32 instruction) { u32 vector_address = instruction >> 20; if (vector_address == 8 || vector_address == 9 || vector_address == 10 || vector_address == 15 || (vector_address >= 0xc20 && vector_address <= 0xc22)) { if (!(h->mstatus & (3ULL << 9))) return false; if (!vector_csr(&h->vector, instruction, h->regs)) return false; h->mstatus |= 3ULL << 9; return true; } u32 funct = (instruction >> 12) & 7; u32 csr = instruction >> 20; u32 rd = (instruction >> 7) & 31; u32 rs = (instruction >> 15) & 31; if (funct == 0 || funct == 4) return false; bool writes = (funct & 3) == 1 || rs != 0; if (writes && (csr >> 10) == 3) return false; u64 old; if (!csr_read(m, h, csr, &old)) return false; u64 source = (funct & 4) ? rs : h->regs[rs]; if (writes) { u64 value = (funct & 3) == 1 ? source : (funct & 3) == 2 ? old | source : old & ~source; csr_write(h, csr, value); } if (rd) h->regs[rd] = old; return true; }