Software APIs
fault_test.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include <stdalign.h>
6 
12 #include "sw/device/lib/runtime/irq.h"
13 #include "sw/device/silicon_creator/lib/dbg_print.h"
14 #include "sw/device/silicon_creator/lib/manifest_def.h"
15 
17 #include "uart_regs.h" // Generated.
18 
19 dif_rv_plic_t plic;
20 
21 void fault_test_main(void) {
22 #if defined(LOAD_ACCESS_FAULT)
23  // This address is not a valid address. It is located near the end of the
24  // peripheral MMIO regiion 0x4000_0000 to 0x5000_0000, but there is no
25  // peripheral located there.
26  //
27  // We expeect the ROM_EXT to report BFV:05524902.
28  volatile uint32_t *p = (volatile uint32_t *)0x4FFF0000;
29  uint32_t value = *p;
30  dbg_printf("Got value: %x\r\n", value);
31  dbg_printf("LOAD_ACCESS_FAULT: FAIL!\r\n");
32 #elif defined(STORE_ACCESS_FAULT)
33  // This address is not a valid address. It is located near the end of the
34  // peripheral MMIO regiion 0x4000_0000 to 0x5000_0000, but there is no
35  // peripheral located there.
36  //
37  // We expeect the ROM_EXT to report BFV:07524902.
38  volatile uint32_t *p = (volatile uint32_t *)0x4FFF0000;
39  *p = 100;
40  dbg_printf("STORE_ACCESS_FAULT: FAIL!\r\n");
41 #elif defined(ILLEGAL_INSTRUCTION_FAULT)
42  // The "HARDENED_TRAP" emits some "unimp" instructions into the instruction
43  // stream.
44  //
45  // We expeect the ROM_EXT to report BFV:02524902.
46  HARDENED_TRAP();
47  dbg_printf("ILLEGAL_INSTRUCTION_FAULT: FAIL!\r\n");
48 #elif defined(HARDWARE_INTERRUPT)
49  // To check that hardware interrupts cause a fault, we need to enable
50  // IRQs at the CPU, the PLIC and the peripheral itself. We'll use the
51  // UART INTR_TEST register to cause a TX_WATERMARK interrupt.
52  //
53  // We expeect the ROM_EXT to report BFV:8b524902.
54  dif_result_t result = dif_rv_plic_init(
56  dbg_printf("plic_init = 0x%x\r\n", result);
57  // Set IRQ priorities to MAX
60  dbg_printf("plic_set_priority = 0x%x\r\n", result);
61  // Set Ibex IRQ priority threshold level
64  dbg_printf("plic_target_set_threshold = 0x%x\r\n", result);
65  // Enable IRQs in PLIC
69  dbg_printf("plic_set_enabled = 0x%x\r\n", result);
70  irq_global_ctrl(true);
71  irq_external_ctrl(true);
72  uint32_t val =
73  bitfield_bit32_write(0, UART_INTR_COMMON_TX_WATERMARK_BIT, true);
74  abs_mmio_write32(TOP_EARLGREY_UART0_BASE_ADDR + UART_INTR_ENABLE_REG_OFFSET,
75  val);
76  abs_mmio_write32(TOP_EARLGREY_UART0_BASE_ADDR + UART_INTR_TEST_REG_OFFSET,
77  val);
78  dbg_printf("HARDWARE_INTERRUPT: FAIL!\r\n");
79 #elif defined(NO_FAULT)
80  dbg_printf("NO_FAULT: PASS!\r\n");
81 #else
82  dbg_printf("Fault not defined. FAIL!\r\n");
83 #endif
84  while (true) {
85  }
86 }