172 lines
3.4 KiB
C
172 lines
3.4 KiB
C
// Machine Status Register, mstatus
|
|
|
|
#define MSTATUS_MPP_MASK (3L << 11)
|
|
#define MSTATUS_MPP_M (3L << 11)
|
|
#define MSTATUS_MPP_S (1L << 11)
|
|
#define MSTATUS_MPP_U (0L << 11)
|
|
|
|
static inline uint64
|
|
r_mstatus()
|
|
{
|
|
uint64 x;
|
|
asm("csrr %0, mstatus" : "=r" (x) );
|
|
return x;
|
|
}
|
|
|
|
static inline void
|
|
w_mstatus(uint64 x)
|
|
{
|
|
asm("csrw mstatus, %0" : : "r" (x));
|
|
}
|
|
|
|
// machine exception program counter, holds the
|
|
// instruction address to which a return from
|
|
// exception will go.
|
|
static inline void
|
|
w_mepc(uint64 x)
|
|
{
|
|
asm("csrw mepc, %0" : : "r" (x));
|
|
}
|
|
|
|
// Supervisor Status Register, sstatus
|
|
|
|
#define SSTATUS_SPP (1L << 8) // 1=Supervisor, 0=User
|
|
|
|
static inline uint64
|
|
r_sstatus()
|
|
{
|
|
uint64 x;
|
|
asm("csrr %0, sstatus" : "=r" (x) );
|
|
return x;
|
|
}
|
|
|
|
static inline void
|
|
w_sstatus(uint64 x)
|
|
{
|
|
asm("csrw sstatus, %0" : : "r" (x));
|
|
}
|
|
|
|
// machine exception program counter, holds the
|
|
// instruction address to which a return from
|
|
// exception will go.
|
|
static inline void
|
|
w_sepc(uint64 x)
|
|
{
|
|
asm("csrw sepc, %0" : : "r" (x));
|
|
}
|
|
|
|
static inline uint64
|
|
r_sepc()
|
|
{
|
|
uint64 x;
|
|
asm("csrr %0, sepc" : "=r" (x) );
|
|
return x;
|
|
}
|
|
|
|
// Machine Exception Delegation
|
|
static inline uint64
|
|
r_medeleg()
|
|
{
|
|
uint64 x;
|
|
asm("csrr %0, medeleg" : "=r" (x) );
|
|
return x;
|
|
}
|
|
|
|
static inline void
|
|
w_medeleg(uint64 x)
|
|
{
|
|
asm("csrw medeleg, %0" : : "r" (x));
|
|
}
|
|
|
|
// Machine Interrupt Delegation
|
|
static inline uint64
|
|
r_mideleg()
|
|
{
|
|
uint64 x;
|
|
asm("csrr %0, mideleg" : "=r" (x) );
|
|
return x;
|
|
}
|
|
|
|
static inline void
|
|
w_mideleg(uint64 x)
|
|
{
|
|
asm("csrw mideleg, %0" : : "r" (x));
|
|
}
|
|
|
|
// Supervisor Trap-Vector Base Address
|
|
// low two bits are mode.
|
|
static inline void
|
|
w_stvec(uint64 x)
|
|
{
|
|
asm("csrw stvec, %0" : : "r" (x));
|
|
}
|
|
|
|
// use riscv's sv39 page table scheme.
|
|
#define SATP_SV39 (8L << 60)
|
|
|
|
#define MAKE_SATP(pagetable) (SATP_SV39 | (((uint64)pagetable) >> 12))
|
|
|
|
// supervisor address translation and protection;
|
|
// holds the address of the page table.
|
|
static inline void
|
|
w_satp(uint64 x)
|
|
{
|
|
asm("csrw satp, %0" : : "r" (x));
|
|
}
|
|
|
|
static inline uint64
|
|
r_satp()
|
|
{
|
|
uint64 x;
|
|
asm("csrr %0, satp" : "=r" (x) );
|
|
return x;
|
|
}
|
|
|
|
// Supervisor Scratch register, for early trap handler in trampoline.S.
|
|
static inline void
|
|
w_sscratch(uint64 x)
|
|
{
|
|
asm("csrw sscratch, %0" : : "r" (x));
|
|
}
|
|
|
|
// Supervisor trap cause
|
|
static inline uint64
|
|
r_scause()
|
|
{
|
|
uint64 x;
|
|
asm("csrr %0, scause" : "=r" (x) );
|
|
return x;
|
|
}
|
|
|
|
#define PGSIZE 4096 // bytes per page
|
|
#define PGSHIFT 12 // bits of offset within a page
|
|
|
|
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
|
|
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
|
|
|
|
#define PTE_V (1L << 0) // valid
|
|
#define PTE_R (1L << 1)
|
|
#define PTE_W (1L << 2)
|
|
#define PTE_X (1L << 3)
|
|
#define PTE_U (1L << 4) // 1 -> user can access
|
|
|
|
// shift a physical address to the right place for a PTE.
|
|
#define PA2PTE(pa) ((((uint64)pa) >> 12) << 10)
|
|
|
|
#define PTE2PA(pte) (((pte) >> 10) << 12)
|
|
|
|
#define PTE_FLAGS(pte) ((pte) & (PTE_V|PTE_R|PTE_W|PTE_X|PTE_U))
|
|
|
|
// extract the three 9-bit page table indices from a virtual address.
|
|
#define PXMASK 0x1FF // 9 bits
|
|
#define PXSHIFT(level) (PGSHIFT+(9*(level)))
|
|
#define PX(level, va) ((((uint64) (va)) >> PXSHIFT(level)) & PXMASK)
|
|
|
|
// one beyond the highest possible virtual address.
|
|
// MAXVA is actually one bit less than the max allowed by
|
|
// Sv39, to avoid having to sign-extend virtual addresses
|
|
// that have the high bit set.
|
|
#define MAXVA (1L << (9 + 9 + 9 + 12 - 1))
|
|
|
|
typedef uint64 pte_t;
|
|
typedef uint64 *pagetable_t; // 512 PTEs
|