Skip to content

Commit

Permalink
riscv: sifive: Apply errata "cip-453" patch
Browse files Browse the repository at this point in the history
Add sign extension to the $badaddr before addressing the instruction page
fault and instruction access fault to workaround the issue "cip-453".

To avoid affecting the existing code sequence, this patch will creates two
trampolines to add sign extension to the $badaddr. By the "alternative"
mechanism, these two trampolines will replace the original exception
handler of instruction page fault and instruction access fault in the
excp_vect_table. In this case, only the specific SiFive CPU core jumps to
the do_page_fault and do_trap_insn_fault through these two trampolines.
Other CPUs are not affected.

Signed-off-by: Vincent Chen <vincent.chen@sifive.com>
Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
  • Loading branch information
Vincent Chen authored and Palmer Dabbelt committed Apr 26, 2021
1 parent 1a0e5db commit 800149a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
11 changes: 11 additions & 0 deletions arch/riscv/Kconfig.erratas
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ config ERRATA_SIFIVE

Otherwise, please say "N" here to avoid unnecessary overhead.

config ERRATA_SIFIVE_CIP_453
bool "Apply SiFive errata CIP-453"
depends on ERRATA_SIFIVE
default y
help
This will apply the SiFive CIP-453 errata to add sign extension
to the $badaddr when exception type is instruction page fault
and instruction access fault.

If you don't know what to do here, say "Y".

endmenu
1 change: 1 addition & 0 deletions arch/riscv/errata/sifive/Makefile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
obj-y += errata_cip_453.o
obj-y += errata.o
20 changes: 20 additions & 0 deletions arch/riscv/errata/sifive/errata.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ struct errata_info_t {
bool (*check_func)(unsigned long arch_id, unsigned long impid);
};

static bool errata_cip_453_check_func(unsigned long arch_id, unsigned long impid)
{
/*
* Affected cores:
* Architecture ID: 0x8000000000000007
* Implement ID: 0x20181004 <= impid <= 0x20191105
*/
if (arch_id != 0x8000000000000007 ||
(impid < 0x20181004 || impid > 0x20191105))
return false;
return true;
}

static struct errata_info_t errata_list[ERRATA_SIFIVE_NUMBER] = {
{
.name = "cip-453",
.check_func = errata_cip_453_check_func
},
};

static u32 __init sifive_errata_probe(unsigned long archid, unsigned long impid)
{
int idx;
Expand Down
38 changes: 38 additions & 0 deletions arch/riscv/errata/sifive/errata_cip_453.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright (C) 2021 SiFive
*/

#include <linux/linkage.h>
#include <asm/asm.h>
#include <asm/asm-offsets.h>
#include <asm/alternative.h>

.macro ADD_SIGN_EXT pt_reg badaddr tmp_reg
REG_L \badaddr, PT_BADADDR(\pt_reg)
li \tmp_reg,1
slli \tmp_reg,\tmp_reg,0x26
and \tmp_reg,\tmp_reg,\badaddr
beqz \tmp_reg, 1f
li \tmp_reg,-1
slli \tmp_reg,\tmp_reg,0x27
or \badaddr,\tmp_reg,\badaddr
REG_S \badaddr, PT_BADADDR(\pt_reg)
1:
.endm

ENTRY(sifive_cip_453_page_fault_trp)
ADD_SIGN_EXT a0, t0, t1
#ifdef CONFIG_MMU
la t0, do_page_fault
#else
la t0, do_trap_unknown
#endif
jr t0
END(sifive_cip_453_page_fault_trp)

ENTRY(sifive_cip_453_insn_fault_trp)
ADD_SIGN_EXT a0, t0, t1
la t0, do_trap_insn_fault
jr t0
END(sifive_cip_453_insn_fault_trp)
21 changes: 20 additions & 1 deletion arch/riscv/include/asm/errata_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,27 @@
#ifndef ASM_ERRATA_LIST_H
#define ASM_ERRATA_LIST_H

#include <asm/alternative.h>
#include <asm/vendorid_list.h>

#ifdef CONFIG_ERRATA_SIFIVE
#define ERRATA_SIFIVE_NUMBER 0
#define ERRATA_SIFIVE_CIP_453 0
#define ERRATA_SIFIVE_NUMBER 1
#endif

#ifdef __ASSEMBLY__

#define ALT_INSN_FAULT(x) \
ALTERNATIVE(__stringify(RISCV_PTR do_trap_insn_fault), \
__stringify(RISCV_PTR sifive_cip_453_insn_fault_trp), \
SIFIVE_VENDOR_ID, ERRATA_SIFIVE_CIP_453, \
CONFIG_ERRATA_SIFIVE_CIP_453)

#define ALT_PAGE_FAULT(x) \
ALTERNATIVE(__stringify(RISCV_PTR do_page_fault), \
__stringify(RISCV_PTR sifive_cip_453_page_fault_trp), \
SIFIVE_VENDOR_ID, ERRATA_SIFIVE_CIP_453, \
CONFIG_ERRATA_SIFIVE_CIP_453)
#endif /* __ASSEMBLY__ */

#endif
6 changes: 4 additions & 2 deletions arch/riscv/kernel/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <asm/unistd.h>
#include <asm/thread_info.h>
#include <asm/asm-offsets.h>
#include <asm/errata_list.h>

#if !IS_ENABLED(CONFIG_PREEMPTION)
.set resume_kernel, restore_all
Expand Down Expand Up @@ -450,7 +451,7 @@ ENDPROC(__switch_to)
/* Exception vector table */
ENTRY(excp_vect_table)
RISCV_PTR do_trap_insn_misaligned
RISCV_PTR do_trap_insn_fault
ALT_INSN_FAULT(RISCV_PTR do_trap_insn_fault)
RISCV_PTR do_trap_insn_illegal
RISCV_PTR do_trap_break
RISCV_PTR do_trap_load_misaligned
Expand All @@ -461,7 +462,8 @@ ENTRY(excp_vect_table)
RISCV_PTR do_trap_ecall_s
RISCV_PTR do_trap_unknown
RISCV_PTR do_trap_ecall_m
RISCV_PTR do_page_fault /* instruction page fault */
/* instruciton page fault */
ALT_PAGE_FAULT(RISCV_PTR do_page_fault)
RISCV_PTR do_page_fault /* load page fault */
RISCV_PTR do_trap_unknown
RISCV_PTR do_page_fault /* store page fault */
Expand Down

0 comments on commit 800149a

Please sign in to comment.