/* State and interfaces shared by the machine implementation files. */ #ifndef MACHINE_INTERNAL_H #define MACHINE_INTERNAL_H #include "machine.h" #include "vector.h" /* Fixed physical addresses, also published in the FDT. */ #define MACHINE_FDT_BASE (MACHINE_RAM_BASE + 0x00800000U) #define MACHINE_FDT_SIZE 0x00010000U #define MACHINE_CLINT_BASE 0x02000000U #define MACHINE_CLINT_SIZE 0x00010000U #define MACHINE_PLIC_BASE 0x0c000000U #define MACHINE_PLIC_SIZE 0x04000000U #define MACHINE_UART_BASE 0x10000000U #define MACHINE_UART_SIZE 0x00001000U #define MACHINE_FINISH_BASE 0x10001000U #define MACHINE_TIMEBASE 10000000U /* Implemented mstatus fields. */ #define MSTATUS_MIE (1ULL << 3) #define MSTATUS_MPIE (1ULL << 7) #define MSTATUS_MPP (3ULL << 11) /* Interrupt bits of mip and mie. The bit index is the interrupt cause. */ #define MIP_MSIP (1ULL << 3) #define MIP_MTIP (1ULL << 7) #define MIP_MEIP (1ULL << 11) #define MIP_ALL (MIP_MSIP | MIP_MTIP | MIP_MEIP) /* The mcause bit that marks an interrupt. */ #define MCAUSE_INTERRUPT (1ULL << 63) /* Synchronous exception causes. */ enum machine_cause { CAUSE_FETCH_MISALIGNED = 0, CAUSE_FETCH_FAULT = 1, CAUSE_ILLEGAL = 2, CAUSE_BREAKPOINT = 3, CAUSE_LOAD_MISALIGNED = 4, CAUSE_LOAD_FAULT = 5, CAUSE_STORE_MISALIGNED = 6, CAUSE_STORE_FAULT = 7, CAUSE_ECALL_U = 8, CAUSE_ECALL_M = 11, }; /* Bus access flags. A plain load has none. */ enum machine_access { ACCESS_STORE = 1, ACCESS_FETCH = 2, ACCESS_ATOMIC = 4, /* Part of an LR, SC or AMO, which only reach RAM. */ }; /* Independent architectural state of one RV64IMA hart. */ struct machine_hart { u64 regs[32]; /* Integer registers; x0 is never written. */ u64 pc; /* Full-width physical instruction address. */ u64 mstatus; /* Implemented M/U status fields. */ u64 mie; /* MSIE, MTIE and MEIE enable bits. */ u64 mtvec; /* Aligned trap base and direct/vectored mode. */ u64 mscratch; /* Software-owned trap scratch register. */ u64 mepc; /* Aligned exception return address. */ u64 mcause; /* Last architectural trap cause. */ u64 mtval; /* Fault address or illegal instruction. */ u64 minstret; /* Retired instructions. */ u32 id; /* Stable zero-based hardware hart ID. */ u8 privilege; /* Architectural privilege: U=0, M=3. */ bool waiting; /* WFI sleep until an enabled interrupt pends. */ bool reserved; /* Whether the LR reservation is live. */ u64 reservation; /* Exact physical address of the last LR. */ u32 reservation_size; /* Width of the last LR, 4 or 8 bytes. */ bool wrote_instret; /* The current instruction wrote minstret. */ /* Integer vector registers and CSRs. */ struct vector_state vector; }; /* Shared CLINT state. mtime is writable, so it is kept apart from the * scheduler clock. */ struct machine_clint { u64 time; /* Writable mtime counter. */ u64 compare[MACHINE_MAX_HARTS]; /* Per-hart mtimecmp, reset to all ones. */ u32 software[MACHINE_MAX_HARTS]; /* Per-hart MSIP bit. */ }; /* PLIC gateway state with one M context per enabled hart. */ struct machine_plic { u32 priority[MACHINE_IRQ_COUNT + 1]; /* Source priority, WARL 0..7. */ u32 pending; /* Latched requests not yet claimed. */ u32 level; /* Asserted level-sensitive sources. */ u32 active; /* Sources between claim and completion. */ u8 owner[MACHINE_IRQ_COUNT + 1]; /* Context that owns an active source. */ u32 enable[MACHINE_MAX_HARTS]; /* Per-context source enable bits. */ u32 threshold[MACHINE_MAX_HARTS]; /* Per-context threshold, WARL 0..7. */ }; /* Transmit-only 16550 subset; transmission is synchronous. */ struct machine_uart { u8 ier; /* Interrupt enable; only THRE (value 2) is modeled. */ bool tx_irq; /* THRE interrupt latch: set when the holding register empties or THRE is enabled, cleared by IIR reads. */ }; /* Whole-machine state, never shared with the hosted interpreter or JIT. */ struct machine { const struct machine_config *config; /* Borrowed immutable options. */ struct machine_hart harts[MACHINE_MAX_HARTS]; /* Independent live harts. */ struct machine_clint clint; /* Shared local interrupts. */ struct machine_plic plic; /* Shared external interrupts. */ struct machine_uart uart; /* Shared console device. */ bool injected_irq; /* One-shot edge has occurred. */ u64 ticks; /* Monotonic scheduling time. */ u64 retired; /* Total retired instructions. */ u32 next_hart; /* Round-robin selection. */ u64 stack_base[MACHINE_MAX_HARTS]; /* Reserved initial stack bases. */ bool finished; /* Explicit platform termination. */ int result; /* Host process exit result. */ }; /* Return whether two finite physical intervals intersect. */ bool machine_overlap(u64 a, u64 asize, u64 b, u64 bsize); /* Checked, naturally aligned bus access of 1, 2, 4 or 8 bytes. `access` * holds machine_access flags; fetches and atomics reach RAM only. On failure, * cause receives the exception code. No access uses a host pointer before * range validation. */ bool machine_bus( struct machine *m, struct machine_hart *h, u64 address, u32 width, u32 access, u64 *value, u64 *cause ); /* Validate an atomic RAM store without performing it, including failed SC. */ bool machine_store_probe( const struct machine *m, const struct machine_hart *h, u64 address, u32 width, u64 *cause ); /* Refresh the one-shot edge and level-sensitive device interrupt state. */ void machine_devices_update(struct machine *m); /* Return the next future timer/edge wake tick, bounded by the run deadline. */ u64 machine_next_event(const struct machine *m); /* Whether the PLIC has a notification above threshold for a context. */ bool machine_plic_notify(const struct machine *m, u32 context); /* Derive a hart's hardware mip bits from CLINT and PLIC state. */ static inline u64 machine_pending( const struct machine *m, const struct machine_hart *h ) { u64 pending = 0; if (m->clint.software[h->id] & 1) pending |= MIP_MSIP; if (m->clint.time >= m->clint.compare[h->id]) pending |= MIP_MTIP; if (machine_plic_notify(m, h->id)) pending |= MIP_MEIP; return pending; } /* Deliver an exception or interrupt; no instruction retires. */ void machine_trap( struct machine *m, struct machine_hart *h, u64 cause, u64 value ); /* Read/modify/write a CSR instruction; false means illegal instruction. */ bool machine_csr(struct machine *m, struct machine_hart *h, u32 instruction); /* Execute at most one instruction, or take an interrupt. */ void machine_step(struct machine *m, struct machine_hart *h); /* Write a bounded flattened device tree into the reserved RAM interval. */ bool machine_fdt(struct machine *m); #endif /* MACHINE_INTERNAL_H */