Complete reformat
This commit is contained in:
parent
b1a34398e9
commit
d6793bf093
60 changed files with 1952 additions and 1995 deletions
130
kernel/riscv.h
130
kernel/riscv.h
|
|
@ -5,61 +5,61 @@ static inline u64
|
|||
r_mhartid()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, mhartid" : "=r" (x) );
|
||||
asm volatile("csrr %0, mhartid" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
// Machine Status Register, mstatus
|
||||
|
||||
#define MSTATUS_MPP_MASK (3L << 11) // previous mode.
|
||||
#define MSTATUS_MPP_M (3L << 11)
|
||||
#define MSTATUS_MPP_S (1L << 11)
|
||||
#define MSTATUS_MPP_U (0L << 11)
|
||||
#define MSTATUS_MIE (1L << 3) // machine-mode interrupt enable.
|
||||
#define MSTATUS_MPP_M (3L << 11)
|
||||
#define MSTATUS_MPP_S (1L << 11)
|
||||
#define MSTATUS_MPP_U (0L << 11)
|
||||
#define MSTATUS_MIE (1L << 3) // machine-mode interrupt enable.
|
||||
|
||||
static inline u64
|
||||
r_mstatus()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, mstatus" : "=r" (x) );
|
||||
asm volatile("csrr %0, mstatus" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_mstatus(u64 x)
|
||||
{
|
||||
asm volatile("csrw mstatus, %0" : : "r" (x));
|
||||
asm volatile("csrw mstatus, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// machine exception program counter, holds the
|
||||
// instruction address to which a return from
|
||||
// exception will go.
|
||||
static inline void
|
||||
static inline void
|
||||
w_mepc(u64 x)
|
||||
{
|
||||
asm volatile("csrw mepc, %0" : : "r" (x));
|
||||
asm volatile("csrw mepc, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Supervisor Status Register, sstatus
|
||||
|
||||
#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User
|
||||
#define SSTATUS_SPP (1L << 8) // Previous mode, 1=Supervisor, 0=User
|
||||
#define SSTATUS_SPIE (1L << 5) // Supervisor Previous Interrupt Enable
|
||||
#define SSTATUS_UPIE (1L << 4) // User Previous Interrupt Enable
|
||||
#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable
|
||||
#define SSTATUS_UIE (1L << 0) // User Interrupt Enable
|
||||
#define SSTATUS_SIE (1L << 1) // Supervisor Interrupt Enable
|
||||
#define SSTATUS_UIE (1L << 0) // User Interrupt Enable
|
||||
|
||||
static inline u64
|
||||
r_sstatus()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, sstatus" : "=r" (x) );
|
||||
asm volatile("csrr %0, sstatus" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_sstatus(u64 x)
|
||||
{
|
||||
asm volatile("csrw sstatus, %0" : : "r" (x));
|
||||
asm volatile("csrw sstatus, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Supervisor Interrupt Pending
|
||||
|
|
@ -67,14 +67,14 @@ static inline u64
|
|||
r_sip()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, sip" : "=r" (x) );
|
||||
asm volatile("csrr %0, sip" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_sip(u64 x)
|
||||
{
|
||||
asm volatile("csrw sip, %0" : : "r" (x));
|
||||
asm volatile("csrw sip, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Supervisor Interrupt Enable
|
||||
|
|
@ -85,14 +85,14 @@ static inline u64
|
|||
r_sie()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, sie" : "=r" (x) );
|
||||
asm volatile("csrr %0, sie" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_sie(u64 x)
|
||||
{
|
||||
asm volatile("csrw sie, %0" : : "r" (x));
|
||||
asm volatile("csrw sie, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Machine-mode Interrupt Enable
|
||||
|
|
@ -103,30 +103,30 @@ static inline u64
|
|||
r_mie()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, mie" : "=r" (x) );
|
||||
asm volatile("csrr %0, mie" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_mie(u64 x)
|
||||
{
|
||||
asm volatile("csrw mie, %0" : : "r" (x));
|
||||
asm volatile("csrw mie, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// supervisor exception program counter, holds the
|
||||
// instruction address to which a return from
|
||||
// exception will go.
|
||||
static inline void
|
||||
static inline void
|
||||
w_sepc(u64 x)
|
||||
{
|
||||
asm volatile("csrw sepc, %0" : : "r" (x));
|
||||
asm volatile("csrw sepc, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
static inline u64
|
||||
r_sepc()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, sepc" : "=r" (x) );
|
||||
asm volatile("csrr %0, sepc" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
@ -135,14 +135,14 @@ static inline u64
|
|||
r_medeleg()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, medeleg" : "=r" (x) );
|
||||
asm volatile("csrr %0, medeleg" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_medeleg(u64 x)
|
||||
{
|
||||
asm volatile("csrw medeleg, %0" : : "r" (x));
|
||||
asm volatile("csrw medeleg, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Machine Interrupt Delegation
|
||||
|
|
@ -150,50 +150,50 @@ static inline u64
|
|||
r_mideleg()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, mideleg" : "=r" (x) );
|
||||
asm volatile("csrr %0, mideleg" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_mideleg(u64 x)
|
||||
{
|
||||
asm volatile("csrw mideleg, %0" : : "r" (x));
|
||||
asm volatile("csrw mideleg, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Supervisor Trap-Vector Base Address
|
||||
// low two bits are mode.
|
||||
static inline void
|
||||
static inline void
|
||||
w_stvec(u64 x)
|
||||
{
|
||||
asm volatile("csrw stvec, %0" : : "r" (x));
|
||||
asm volatile("csrw stvec, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
static inline u64
|
||||
r_stvec()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, stvec" : "=r" (x) );
|
||||
asm volatile("csrr %0, stvec" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
// Machine-mode interrupt vector
|
||||
static inline void
|
||||
static inline void
|
||||
w_mtvec(u64 x)
|
||||
{
|
||||
asm volatile("csrw mtvec, %0" : : "r" (x));
|
||||
asm volatile("csrw mtvec, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Physical Memory Protection
|
||||
static inline void
|
||||
w_pmpcfg0(u64 x)
|
||||
{
|
||||
asm volatile("csrw pmpcfg0, %0" : : "r" (x));
|
||||
asm volatile("csrw pmpcfg0, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
static inline void
|
||||
w_pmpaddr0(u64 x)
|
||||
{
|
||||
asm volatile("csrw pmpaddr0, %0" : : "r" (x));
|
||||
asm volatile("csrw pmpaddr0, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// use riscv's sv39 page table scheme.
|
||||
|
|
@ -203,24 +203,24 @@ w_pmpaddr0(u64 x)
|
|||
|
||||
// supervisor address translation and protection;
|
||||
// holds the address of the page table.
|
||||
static inline void
|
||||
static inline void
|
||||
w_satp(u64 x)
|
||||
{
|
||||
asm volatile("csrw satp, %0" : : "r" (x));
|
||||
asm volatile("csrw satp, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
static inline u64
|
||||
r_satp()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, satp" : "=r" (x) );
|
||||
asm volatile("csrr %0, satp" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_mscratch(u64 x)
|
||||
{
|
||||
asm volatile("csrw mscratch, %0" : : "r" (x));
|
||||
asm volatile("csrw mscratch, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
// Supervisor Trap Cause
|
||||
|
|
@ -228,7 +228,7 @@ static inline u64
|
|||
r_scause()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, scause" : "=r" (x) );
|
||||
asm volatile("csrr %0, scause" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
@ -237,22 +237,22 @@ static inline u64
|
|||
r_stval()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, stval" : "=r" (x) );
|
||||
asm volatile("csrr %0, stval" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
// Machine-mode Counter-Enable
|
||||
static inline void
|
||||
static inline void
|
||||
w_mcounteren(u64 x)
|
||||
{
|
||||
asm volatile("csrw mcounteren, %0" : : "r" (x));
|
||||
asm volatile("csrw mcounteren, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
static inline u64
|
||||
r_mcounteren()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, mcounteren" : "=r" (x) );
|
||||
asm volatile("csrr %0, mcounteren" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
@ -261,7 +261,7 @@ static inline u64
|
|||
r_time()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("csrr %0, time" : "=r" (x) );
|
||||
asm volatile("csrr %0, time" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
@ -291,7 +291,7 @@ static inline u64
|
|||
r_sp()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("mv %0, sp" : "=r" (x) );
|
||||
asm volatile("mv %0, sp" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
@ -301,21 +301,21 @@ static inline u64
|
|||
r_tp()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("mv %0, tp" : "=r" (x) );
|
||||
asm volatile("mv %0, tp" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
static inline void
|
||||
static inline void
|
||||
w_tp(u64 x)
|
||||
{
|
||||
asm volatile("mv tp, %0" : : "r" (x));
|
||||
asm volatile("mv tp, %0" : : "r"(x));
|
||||
}
|
||||
|
||||
static inline u64
|
||||
r_ra()
|
||||
{
|
||||
u64 x;
|
||||
asm volatile("mv %0, ra" : "=r" (x) );
|
||||
asm volatile("mv %0, ra" : "=r"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
|
|
@ -327,16 +327,16 @@ sfence_vma()
|
|||
asm volatile("sfence.vma zero, zero");
|
||||
}
|
||||
|
||||
typedef u64 pte_t;
|
||||
typedef u64 pte_t;
|
||||
typedef u64 *pagetable_t; // 512 PTEs
|
||||
|
||||
#endif // __ASSEMBLER__
|
||||
|
||||
#define PGSIZE 4096 // bytes per page
|
||||
#define PGSHIFT 12 // bits of offset within a page
|
||||
#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 PGROUNDUP(sz) (((sz) + PGSIZE - 1) & ~(PGSIZE - 1))
|
||||
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE - 1))
|
||||
|
||||
#define PTE_V (1L << 0) // valid
|
||||
#define PTE_R (1L << 1)
|
||||
|
|
@ -352,9 +352,9 @@ typedef u64 *pagetable_t; // 512 PTEs
|
|||
#define PTE_FLAGS(pte) ((pte) & 0x3FF)
|
||||
|
||||
// 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) ((((u64) (va)) >> PXSHIFT(level)) & PXMASK)
|
||||
#define PXMASK 0x1FF // 9 bits
|
||||
#define PXSHIFT(level) (PGSHIFT + (9 * (level)))
|
||||
#define PX(level, va) ((((u64)(va)) >> PXSHIFT(level)) & PXMASK)
|
||||
|
||||
// one beyond the highest possible virtual address.
|
||||
// MAXVA is actually one bit less than the max allowed by
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue