Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/RISCVERS/opensbi into github
Browse files Browse the repository at this point in the history
  • Loading branch information
chenguokai committed Apr 7, 2021
2 parents 98a8723 + e71a7c1 commit 3685930
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 98 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ CPPFLAGS += $(GENFLAGS)
CPPFLAGS += $(platform-cppflags-y)
CPPFLAGS += $(firmware-cppflags-y)
ASFLAGS = -g -Wall -nostdlib -D__ASSEMBLY__
ASFLAGS = -g -Wall -nostdlib
ASFLAGS += -fno-omit-frame-pointer -fno-optimize-sibling-calls
ASFLAGS += -mno-save-restore -mstrict-align
ASFLAGS += -mabi=$(PLATFORM_RISCV_ABI) -march=$(PLATFORM_RISCV_ISA)
Expand Down
112 changes: 44 additions & 68 deletions firmware/fw_base.S
Original file line number Diff line number Diff line change
Expand Up @@ -655,62 +655,63 @@ fw_platform_init:
call sbi_trap_handler
.endm

.macro TRAP_RESTORE_GENERAL_REGS_EXCEPT_SP_T0
/* Restore all general regisers except SP and T0 */
REG_L ra, SBI_TRAP_REGS_OFFSET(ra)(sp)
REG_L gp, SBI_TRAP_REGS_OFFSET(gp)(sp)
REG_L tp, SBI_TRAP_REGS_OFFSET(tp)(sp)
REG_L t1, SBI_TRAP_REGS_OFFSET(t1)(sp)
REG_L t2, SBI_TRAP_REGS_OFFSET(t2)(sp)
REG_L s0, SBI_TRAP_REGS_OFFSET(s0)(sp)
REG_L s1, SBI_TRAP_REGS_OFFSET(s1)(sp)
REG_L a0, SBI_TRAP_REGS_OFFSET(a0)(sp)
REG_L a1, SBI_TRAP_REGS_OFFSET(a1)(sp)
REG_L a2, SBI_TRAP_REGS_OFFSET(a2)(sp)
REG_L a3, SBI_TRAP_REGS_OFFSET(a3)(sp)
REG_L a4, SBI_TRAP_REGS_OFFSET(a4)(sp)
REG_L a5, SBI_TRAP_REGS_OFFSET(a5)(sp)
REG_L a6, SBI_TRAP_REGS_OFFSET(a6)(sp)
REG_L a7, SBI_TRAP_REGS_OFFSET(a7)(sp)
REG_L s2, SBI_TRAP_REGS_OFFSET(s2)(sp)
REG_L s3, SBI_TRAP_REGS_OFFSET(s3)(sp)
REG_L s4, SBI_TRAP_REGS_OFFSET(s4)(sp)
REG_L s5, SBI_TRAP_REGS_OFFSET(s5)(sp)
REG_L s6, SBI_TRAP_REGS_OFFSET(s6)(sp)
REG_L s7, SBI_TRAP_REGS_OFFSET(s7)(sp)
REG_L s8, SBI_TRAP_REGS_OFFSET(s8)(sp)
REG_L s9, SBI_TRAP_REGS_OFFSET(s9)(sp)
REG_L s10, SBI_TRAP_REGS_OFFSET(s10)(sp)
REG_L s11, SBI_TRAP_REGS_OFFSET(s11)(sp)
REG_L t3, SBI_TRAP_REGS_OFFSET(t3)(sp)
REG_L t4, SBI_TRAP_REGS_OFFSET(t4)(sp)
REG_L t5, SBI_TRAP_REGS_OFFSET(t5)(sp)
REG_L t6, SBI_TRAP_REGS_OFFSET(t6)(sp)
.macro TRAP_RESTORE_GENERAL_REGS_EXCEPT_A0_T0
/* Restore all general regisers except A0 and T0 */
REG_L ra, SBI_TRAP_REGS_OFFSET(ra)(a0)
REG_L sp, SBI_TRAP_REGS_OFFSET(sp)(a0)
REG_L gp, SBI_TRAP_REGS_OFFSET(gp)(a0)
REG_L tp, SBI_TRAP_REGS_OFFSET(tp)(a0)
REG_L t1, SBI_TRAP_REGS_OFFSET(t1)(a0)
REG_L t2, SBI_TRAP_REGS_OFFSET(t2)(a0)
REG_L s0, SBI_TRAP_REGS_OFFSET(s0)(a0)
REG_L s1, SBI_TRAP_REGS_OFFSET(s1)(a0)
REG_L a1, SBI_TRAP_REGS_OFFSET(a1)(a0)
REG_L a2, SBI_TRAP_REGS_OFFSET(a2)(a0)
REG_L a3, SBI_TRAP_REGS_OFFSET(a3)(a0)
REG_L a4, SBI_TRAP_REGS_OFFSET(a4)(a0)
REG_L a5, SBI_TRAP_REGS_OFFSET(a5)(a0)
REG_L a6, SBI_TRAP_REGS_OFFSET(a6)(a0)
REG_L a7, SBI_TRAP_REGS_OFFSET(a7)(a0)
REG_L s2, SBI_TRAP_REGS_OFFSET(s2)(a0)
REG_L s3, SBI_TRAP_REGS_OFFSET(s3)(a0)
REG_L s4, SBI_TRAP_REGS_OFFSET(s4)(a0)
REG_L s5, SBI_TRAP_REGS_OFFSET(s5)(a0)
REG_L s6, SBI_TRAP_REGS_OFFSET(s6)(a0)
REG_L s7, SBI_TRAP_REGS_OFFSET(s7)(a0)
REG_L s8, SBI_TRAP_REGS_OFFSET(s8)(a0)
REG_L s9, SBI_TRAP_REGS_OFFSET(s9)(a0)
REG_L s10, SBI_TRAP_REGS_OFFSET(s10)(a0)
REG_L s11, SBI_TRAP_REGS_OFFSET(s11)(a0)
REG_L t3, SBI_TRAP_REGS_OFFSET(t3)(a0)
REG_L t4, SBI_TRAP_REGS_OFFSET(t4)(a0)
REG_L t5, SBI_TRAP_REGS_OFFSET(t5)(a0)
REG_L t6, SBI_TRAP_REGS_OFFSET(t6)(a0)
.endm

.macro TRAP_RESTORE_MEPC_MSTATUS have_mstatush
/* Restore MEPC and MSTATUS CSRs */
REG_L t0, SBI_TRAP_REGS_OFFSET(mepc)(sp)
REG_L t0, SBI_TRAP_REGS_OFFSET(mepc)(a0)
csrw CSR_MEPC, t0
REG_L t0, SBI_TRAP_REGS_OFFSET(mstatus)(sp)
REG_L t0, SBI_TRAP_REGS_OFFSET(mstatus)(a0)
csrw CSR_MSTATUS, t0
.if \have_mstatush
REG_L t0, SBI_TRAP_REGS_OFFSET(mstatusH)(sp)
REG_L t0, SBI_TRAP_REGS_OFFSET(mstatusH)(a0)
csrw CSR_MSTATUSH, t0
.endif
.endm

.macro TRAP_RESTORE_SP_T0
.macro TRAP_RESTORE_A0_T0
/* Restore T0 */
REG_L t0, SBI_TRAP_REGS_OFFSET(t0)(sp)
REG_L t0, SBI_TRAP_REGS_OFFSET(t0)(a0)

/* Restore SP */
REG_L sp, SBI_TRAP_REGS_OFFSET(sp)(sp)
/* Restore A0 */
REG_L a0, SBI_TRAP_REGS_OFFSET(a0)(a0)
.endm

.section .entry, "ax", %progbits
.align 3
.globl _trap_handler
.globl _trap_exit
_trap_handler:
TRAP_SAVE_AND_SETUP_SP_T0

Expand All @@ -720,32 +721,20 @@ _trap_handler:

TRAP_CALL_C_ROUTINE

TRAP_RESTORE_GENERAL_REGS_EXCEPT_SP_T0

TRAP_RESTORE_MEPC_MSTATUS 0

TRAP_RESTORE_SP_T0

mret

.section .entry, "ax", %progbits
.align 3
.globl _trap_exit
_trap_exit:
add sp, a0, zero

TRAP_RESTORE_GENERAL_REGS_EXCEPT_SP_T0
TRAP_RESTORE_GENERAL_REGS_EXCEPT_A0_T0

TRAP_RESTORE_MEPC_MSTATUS 0

TRAP_RESTORE_SP_T0
TRAP_RESTORE_A0_T0

mret

#if __riscv_xlen == 32
.section .entry, "ax", %progbits
.align 3
.globl _trap_handler_rv32_hyp
.globl _trap_exit_rv32_hyp
_trap_handler_rv32_hyp:
TRAP_SAVE_AND_SETUP_SP_T0

Expand All @@ -755,25 +744,12 @@ _trap_handler_rv32_hyp:

TRAP_CALL_C_ROUTINE

TRAP_RESTORE_GENERAL_REGS_EXCEPT_SP_T0

TRAP_RESTORE_MEPC_MSTATUS 1

TRAP_RESTORE_SP_T0

mret

.section .entry, "ax", %progbits
.align 3
.globl _trap_exit_rv32_hyp
_trap_exit_rv32_hyp:
add sp, a0, zero

TRAP_RESTORE_GENERAL_REGS_EXCEPT_SP_T0
TRAP_RESTORE_GENERAL_REGS_EXCEPT_A0_T0

TRAP_RESTORE_MEPC_MSTATUS 1

TRAP_RESTORE_SP_T0
TRAP_RESTORE_A0_T0

mret
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/sbi/fw_dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

/* clang-format on */

#ifndef __ASSEMBLY__
#ifndef __ASSEMBLER__

#include <sbi/sbi_types.h>

Expand Down
10 changes: 5 additions & 5 deletions include/sbi/riscv_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/* clang-format off */

#ifdef __ASSEMBLY__
#ifdef __ASSEMBLER__
#define __ASM_STR(x) x
#else
#define __ASM_STR(x) #x
Expand All @@ -38,7 +38,7 @@
#define LGREG __REG_SEL(3, 2)

#if __SIZEOF_POINTER__ == 8
#ifdef __ASSEMBLY__
#ifdef __ASSEMBLER__
#define RISCV_PTR .dword
#define RISCV_SZPTR 8
#define RISCV_LGPTR 3
Expand All @@ -48,7 +48,7 @@
#define RISCV_LGPTR "3"
#endif
#elif __SIZEOF_POINTER__ == 4
#ifdef __ASSEMBLY__
#ifdef __ASSEMBLER__
#define RISCV_PTR .word
#define RISCV_SZPTR 4
#define RISCV_LGPTR 2
Expand Down Expand Up @@ -79,7 +79,7 @@

/* clang-format on */

#ifndef __ASSEMBLY__
#ifndef __ASSEMBLER__

#define csr_swap(csr, val) \
({ \
Expand Down Expand Up @@ -182,6 +182,6 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
unsigned long *log2len);

#endif /* !__ASSEMBLY__ */
#endif /* !__ASSEMBLER__ */

#endif
2 changes: 1 addition & 1 deletion include/sbi/sbi_const.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/* clang-format off */

#ifdef __ASSEMBLY__
#ifdef __ASSEMBLER__
#define _AC(X,Y) X
#define _AT(T,X) X
#else
Expand Down
3 changes: 3 additions & 0 deletions include/sbi/sbi_csr_detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@

#include <sbi/riscv_encoding.h>
#include <sbi/sbi_hart.h>
#include <sbi/sbi_trap.h>

#define csr_read_allowed(csr_num, trap) \
({ \
register ulong tinfo asm("a3") = (ulong)trap; \
register ulong ttmp asm("a4"); \
register ulong mtvec = sbi_hart_expected_trap_addr(); \
register ulong ret = 0; \
((struct sbi_trap_info *)(trap))->cause = 0; \
asm volatile( \
"add %[ttmp], %[tinfo], zero\n" \
"csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \
Expand All @@ -36,6 +38,7 @@
register ulong tinfo asm("a3") = (ulong)trap; \
register ulong ttmp asm("a4"); \
register ulong mtvec = sbi_hart_expected_trap_addr(); \
((struct sbi_trap_info *)(trap))->cause = 0; \
asm volatile( \
"add %[ttmp], %[tinfo], zero\n" \
"csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n" \
Expand Down
2 changes: 1 addition & 1 deletion include/sbi/sbi_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

#define SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT (1UL << 12)

#ifndef __ASSEMBLY__
#ifndef __ASSEMBLER__

#include <sbi/sbi_ecall_interface.h>
#include <sbi/sbi_error.h>
Expand Down
2 changes: 1 addition & 1 deletion include/sbi/sbi_scratch.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

/* clang-format on */

#ifndef __ASSEMBLY__
#ifndef __ASSEMBLER__

#include <sbi/sbi_types.h>

Expand Down
4 changes: 2 additions & 2 deletions include/sbi/sbi_trap.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
/** Size (in bytes) of sbi_trap_info */
#define SBI_TRAP_INFO_SIZE SBI_TRAP_INFO_OFFSET(last)

#ifndef __ASSEMBLY__
#ifndef __ASSEMBLER__

#include <sbi/sbi_types.h>

Expand Down Expand Up @@ -205,7 +205,7 @@ struct sbi_trap_info {
int sbi_trap_redirect(struct sbi_trap_regs *regs,
struct sbi_trap_info *trap);

void sbi_trap_handler(struct sbi_trap_regs *regs);
struct sbi_trap_regs *sbi_trap_handler(struct sbi_trap_regs *regs);

void __noreturn sbi_trap_exit(const struct sbi_trap_regs *regs);

Expand Down
3 changes: 1 addition & 2 deletions include/sbi_utils/fdt/fdt_fixup.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ void fdt_cpu_fixup(void *fdt);
* It is recommended that platform codes call this helper in their final_init()
*
* @param fdt: device tree blob
* @param compat: PLIC node compatible string
*/
void fdt_plic_fixup(void *fdt, const char *compat);
void fdt_plic_fixup(void *fdt);

/**
* Fix up the reserved memory node in the device tree
Expand Down
19 changes: 14 additions & 5 deletions lib/sbi/sbi_emulate_csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@
static bool hpm_allowed(int hpm_num, ulong prev_mode, bool virt)
{
ulong cen = -1UL;
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();

if (prev_mode <= PRV_S) {
cen &= csr_read(CSR_MCOUNTEREN);
if (virt)
cen &= csr_read(CSR_HCOUNTEREN);
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_MCOUNTEREN)) {
cen &= csr_read(CSR_MCOUNTEREN);
if (virt)
cen &= csr_read(CSR_HCOUNTEREN);
} else {
cen = 0;
}
}
if (prev_mode == PRV_U) {
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SCOUNTEREN))
cen &= csr_read(CSR_SCOUNTEREN);
else
cen = 0;
}
if (prev_mode == PRV_U)
cen &= csr_read(CSR_SCOUNTEREN);

return ((cen >> hpm_num) & 1) ? TRUE : FALSE;
}
Expand Down
3 changes: 0 additions & 3 deletions lib/sbi/sbi_hart.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ static void hart_detect_features(struct sbi_scratch *scratch)
#undef __check_csr

/* Detect if hart supports SCOUNTEREN feature */
trap.cause = 0;
val = csr_read_allowed(CSR_SCOUNTEREN, (unsigned long)&trap);
if (!trap.cause) {
csr_write_allowed(CSR_SCOUNTEREN, (unsigned long)&trap, val);
Expand All @@ -415,7 +414,6 @@ static void hart_detect_features(struct sbi_scratch *scratch)
}

/* Detect if hart supports MCOUNTEREN feature */
trap.cause = 0;
val = csr_read_allowed(CSR_MCOUNTEREN, (unsigned long)&trap);
if (!trap.cause) {
csr_write_allowed(CSR_MCOUNTEREN, (unsigned long)&trap, val);
Expand All @@ -424,7 +422,6 @@ static void hart_detect_features(struct sbi_scratch *scratch)
}

/* Detect if hart supports time CSR */
trap.cause = 0;
csr_read_allowed(CSR_TIME, (unsigned long)&trap);
if (!trap.cause)
hfeatures->features |= SBI_HART_HAS_TIME;
Expand Down
5 changes: 3 additions & 2 deletions lib/sbi/sbi_trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ int sbi_trap_redirect(struct sbi_trap_regs *regs,
*
* @param regs pointer to register state
*/
void sbi_trap_handler(struct sbi_trap_regs *regs)
struct sbi_trap_regs *sbi_trap_handler(struct sbi_trap_regs *regs)
{
int rc = SBI_ENOTSUPP;
const char *msg = "trap handler failed";
Expand All @@ -236,7 +236,7 @@ void sbi_trap_handler(struct sbi_trap_regs *regs)
msg = "unhandled external interrupt";
goto trap_error;
};
return;
return regs;
}

switch (mcause) {
Expand Down Expand Up @@ -271,6 +271,7 @@ void sbi_trap_handler(struct sbi_trap_regs *regs)
trap_error:
if (rc)
sbi_trap_error(msg, rc, mcause, mtval, mtval2, mtinst, regs);
return regs;
}

typedef void (*trap_exit_t)(const struct sbi_trap_regs *regs);
Expand Down
Loading

0 comments on commit 3685930

Please sign in to comment.