ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
emu_exc.h
Go to the documentation of this file.
1
18
19#pragma once
20
21#include <stdint.h>
22#include <unicorn/unicorn.h>
23
24#include "ra8_attributes.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/* Cortex-M system control space (architectural, all cores) -- inside the PPB.
31 * The PPB is mapped as plain RAM here (not callback MMIO), so SCB/NVIC writes
32 * the firmware performs land in memory and read back -- the exception model
33 * below polls/edits these words to model what real M-profile hardware would do
34 * with the NVIC that Unicorn does not implement. */
35typedef enum : uint64_t {
36 k_syst_csr = 0xE000E010UL,
38 k_scb_icsr = 0xE000ED04UL,
39 k_scb_vtor = 0xE000ED08UL,
40 k_scb_aircr = 0xE000ED0CUL,
42 k_scb_shpr2 = 0xE000ED1CUL,
43 k_scb_shpr3 = 0xE000ED20UL,
44 k_mpu_type = 0xE000ED90UL,
45 k_mpu_type_seed = 0x00000800UL,
46 k_mpu_ctrl = 0xE000ED94UL,
47 k_mpu_rnr = 0xE000ED98UL,
48 k_mpu_rbar = 0xE000ED9CUL,
49 k_mpu_rlar = 0xE000EDA0UL,
50 k_scb_ccr = 0xE000ED14UL,
51 k_ccr_div_0_trp = 0x10UL,
52 k_scb_cfsr = 0xE000ED28UL,
53 k_scb_mmfar = 0xE000ED34UL,
58 k_exc_svcall = 11UL,
59 k_exc_pendsv = 14UL,
61 k_nvic_ipr_base = 0xE000E400UL,
62 k_nvic_ispr_base = 0xE000E200UL,
63 k_nvic_iser_base = 0xE000E100UL,
64 k_nvic_icer_base = 0xE000E180UL,
66 k_nvic_en_span = 8UL * 4UL,
69
70/* Armv7E-M / Armv8-M exception model constants (the part Unicorn's Cortex-M33
71 * core leaves to software here -- it has no NVIC/exception unit). EXC_RETURN
72 * magic values steer the unstack: bit2 picks the return stack (1 = PSP, 0 =
73 * MSP), bit3 the return mode (1 = Thread, 0 = Handler), bit4 the frame type
74 * (1 = basic 8-word frame, 0 = FP extended frame). On Armv8-M the value also
75 * carries bit6 = S (1 = return to Secure, 0 = Non-Secure) and bit5 = DCRS, so a
76 * Non-Secure thread return is 0xFFFFFFBC -- NOT one of the Armv7-M 0xFFFFFFF_
77 * values. is_exc_return() therefore matches the whole bits[31:7]-set EXC_RETURN
78 * prefix (0xFFFFFF80..FF), and exc_enter/exc_return branch on bit4 (FType) to
79 * stack/unstack the S0-S15 + FPSCR words whenever an FP extended frame is in
80 * play (ThreadX's PendSV mirrors this with its own TST LR,#0x10 on S16-S31). */
126
143uint32_t exc_vector(uc_engine* uc, uint32_t vtor_base, uint32_t exc_num);
144
168void exc_enter(uc_engine* uc, uint32_t exc_num, uint32_t handler);
169
189void exc_return(uc_engine* uc, uint32_t exc_ret);
190
215bool exc_take_pending(uc_engine* uc, uint32_t vtor_base, bool allow_systick);
216
238bool idle_spin_at(uc_engine* uc, uint32_t pc);
239
257void dwt_cyccnt_advance(uc_engine* uc);
258
273void emu_exc_install_core(uc_engine* uc);
274
288void emu_exc_install_scb_nvic(uc_engine* uc);
289
302void emu_exc_arm_systick(void);
303
318bool emu_exc_take_exc_return(uint64_t* out_pc);
319
333
347bool emu_exc_pendsv_stop(void);
348
362bool emu_exc_bkpt_hit(void);
363
377uint32_t emu_exc_bkpt_pc(void);
378
392bool emu_exc_reboot_requested(void);
393
407
421uint32_t emu_exc_systick_fires(void);
422
436uint32_t emu_exc_pendsv_takes(void);
437
451uint32_t emu_exc_svc_takes(void);
452
469void emu_exc_reset(void);
470
471#ifdef __cplusplus
472}
473#endif
bool emu_exc_take_exc_return(uint64_t *out_pc)
Consume a latched EXC_RETURN branch (read + clear).
Definition emu_exc.c:932
void emu_exc_arm_systick(void)
Pend the periodic SysTick for this outer chunk.
Definition emu_exc.c:926
void emu_exc_clear_pendsv_stop(void)
Clear the PendSV context-switch stop marker (per relaunch).
Definition emu_exc.c:943
bool idle_spin_at(uc_engine *uc, uint32_t pc)
True if pc sits in a wait-for-interrupt spin (the core is idle).
Definition emu_idle.c:120
cortexm_exc_t
Definition emu_exc.h:81
@ k_fp_s_words
S0-S15 saved in the FP extended frame.
Definition emu_exc.h:105
@ k_xpsr_t_bit
xPSR.T (Thumb) – must stay set.
Definition emu_exc.h:93
@ k_exc_ret_spsel
EXC_RETURN bit2: return stack = PSP.
Definition emu_exc.h:89
@ k_thumb2_insn_bytes
32-bit Thumb-2 instruction width.
Definition emu_exc.h:112
@ k_frame_off_xpsr
Basic exception-frame offset of xPSR.
Definition emu_exc.h:103
@ k_exc_frame_words
{R0-R3,R12,LR,PC,xPSR} basic frame.
Definition emu_exc.h:82
@ k_control_spsel
CONTROL.SPSEL: thread SP = PSP.
Definition emu_exc.h:91
@ k_exc_ret_mode
EXC_RETURN bit3: return to Thread.
Definition emu_exc.h:90
@ k_xpsr_ipsr_mask
xPSR[8:0] = IPSR (active exception).
Definition emu_exc.h:95
@ k_vscclrm_hw0_s
VSCCLRM {s..,VPR} first halfword.
Definition emu_exc.h:117
@ k_fpcxtns_pop
VLDR FPCXTNS,[sp],#4 (LE word).
Definition emu_exc.h:114
@ k_lo4_mask
Low nibble (register / cond field).
Definition emu_exc.h:110
@ k_frame_off_lr
Basic exception-frame offset of LR.
Definition emu_exc.h:101
@ k_exc_ret_ftype
EXC_RETURN bit4: 1 = basic, 0 = FP.
Definition emu_exc.h:85
@ k_exc_prio_max
Lowest configurable priority value.
Definition emu_exc.h:97
@ k_thumb_bx_lr
BX LR (stub a function to return).
Definition emu_exc.h:122
@ k_frame_off_pc
Basic exception-frame offset of PC.
Definition emu_exc.h:102
@ k_frame_off_fpscr
FP-frame offset of FPSCR (32 + 16*4).
Definition emu_exc.h:107
@ k_exc_frame_bytes
8 words * 4 bytes.
Definition emu_exc.h:83
@ k_lo16_mask
Low halfword of a 32-bit fetch.
Definition emu_exc.h:119
@ k_exc_ret_handler
Return to Handler mode, MSP.
Definition emu_exc.h:86
@ k_exc_ret_psp
Return to Thread mode, PSP.
Definition emu_exc.h:88
@ k_thumb_hw_bytes
Bytes per Thumb halfword.
Definition emu_exc.h:123
@ k_fpcxtns_push
VSTR FPCXTNS,[sp,#-4]! (LE word).
Definition emu_exc.h:113
@ k_exc_ret_v8_mask
Armv8-M EXC_RETURN prefix: bits[31:7].
Definition emu_exc.h:84
@ k_nvic_prio_shift
Implemented priority is the 4 MSBs.
Definition emu_exc.h:109
@ k_vector_erased
Erased-flash / invalid vector word.
Definition emu_exc.h:108
@ k_exc_ret_msp
Return to Thread mode, MSP.
Definition emu_exc.h:87
@ k_frame_off_r3
Basic exception-frame offset of R3.
Definition emu_exc.h:100
@ k_fp_frame_extra
FP ext frame above basic: S0-15+FPSCR.
Definition emu_exc.h:104
@ k_bkpt_hw_base
BKPT #imm8 halfword (imm free).
Definition emu_exc.h:120
@ k_vscclrm_hw0_d
VSCCLRM {d..,VPR} first halfword.
Definition emu_exc.h:118
@ k_exc_nest_max
Tracked active-exception nesting cap.
Definition emu_exc.h:98
@ k_byte_bits
Bits per byte (SHPR field width).
Definition emu_exc.h:99
@ k_word_bytes
One stacked word.
Definition emu_exc.h:115
@ k_frame_off_s0
FP-frame offset of S0 (above basic).
Definition emu_exc.h:106
@ k_control_fpca
CONTROL.FPCA: FP context is active.
Definition emu_exc.h:92
@ k_clrm_hw0
CLRM {regs} first halfword.
Definition emu_exc.h:116
@ k_armv8m_sg_opcode
Armv8-M SG secure-gateway opcode.
Definition emu_exc.h:111
@ k_bkpt_hw_mask
Mask isolating the BKPT opcode.
Definition emu_exc.h:121
@ k_xpsr_align9
xPSR bit9: stack-frame realignment.
Definition emu_exc.h:94
@ k_exc_prio_none
Sentinel "no handler active" prio.
Definition emu_exc.h:96
cortexm_scs_t
Definition emu_exc.h:35
@ k_scb_cfsr
Config Fault Status (MMFSR low byte).
Definition emu_exc.h:52
@ k_scb_ccr
Configuration and Control (CCR).
Definition emu_exc.h:50
@ k_nvic_en_span
Byte span of one set/clear array.
Definition emu_exc.h:66
@ k_scb_mmfar
MemManage Fault Address Register.
Definition emu_exc.h:53
@ k_nvic_en_words
ISER/ICER words modelled (256 lines).
Definition emu_exc.h:65
@ k_syst_csr
SysTick control/status (SYST_CSR).
Definition emu_exc.h:36
@ k_nvic_ipr_base
NVIC IPR priority bytes (one per IRQ).
Definition emu_exc.h:61
@ k_scb_aircr
App interrupt/reset control (AIRCR).
Definition emu_exc.h:40
@ k_scb_shpr2
System handler priority 2 (SVC=b3).
Definition emu_exc.h:42
@ k_mpu_type_seed
8 data regions (matches the M85 MPU).
Definition emu_exc.h:45
@ k_icsr_pendstset
ICSR.PENDSTSET bit (request SysTick).
Definition emu_exc.h:55
@ k_aircr_sysresetreq
AIRCR.SYSRESETREQ bit (request reset).
Definition emu_exc.h:41
@ k_ccr_div_0_trp
CCR.DIV_0_TRP bit4: divide-by-0 traps.
Definition emu_exc.h:51
@ k_scb_vtor
Vector table offset register.
Definition emu_exc.h:39
@ k_mpu_rbar
MPU_RBAR (BASE[31:5]|SH|AP[2:1]|XN).
Definition emu_exc.h:48
@ k_icsr_pendsvset
ICSR.PENDSVSET bit (request PendSV).
Definition emu_exc.h:54
@ k_syst_csr_run
ENABLE | TICKINT both set.
Definition emu_exc.h:37
@ k_exc_memmanage
MemManage exception / vector index.
Definition emu_exc.h:57
@ k_exc_usagefault
UsageFault exception / vector index.
Definition emu_exc.h:56
@ k_scb_shpr3
System handler priority 3 (PSV/SYT).
Definition emu_exc.h:43
@ k_mpu_ctrl
MPU_CTRL (ENABLE in bit 0).
Definition emu_exc.h:46
@ k_mpu_type
MPU_TYPE (DREGION in bits 15:8).
Definition emu_exc.h:44
@ k_exc_svcall
SVCall exception / vector index.
Definition emu_exc.h:58
@ k_exc_pendsv
PendSV exception / vector index.
Definition emu_exc.h:59
@ k_exc_systick
SysTick exception / vector index.
Definition emu_exc.h:60
@ k_scb_icsr
Interrupt control/state (ICSR).
Definition emu_exc.h:38
@ k_exc_irq_vec0
Vector index of IRQ0 (16 + IRQn).
Definition emu_exc.h:67
@ k_mpu_rnr
MPU_RNR (region number select).
Definition emu_exc.h:47
@ k_mpu_rlar
MPU_RLAR (LIMIT[31:5]|AttrIdx|EN).
Definition emu_exc.h:49
void dwt_cyccnt_advance(uc_engine *uc)
Advance the DWT cycle counter by one outer chunk's worth of cycles.
bool emu_exc_bkpt_hit(void)
Whether the firmware executed a BKPT (deliberate trap / give-up).
Definition emu_exc.c:955
uint32_t exc_vector(uc_engine *uc, uint32_t vtor_base, uint32_t exc_num)
Read the handler address for an exception from the vector table.
Definition emu_exc.c:417
bool emu_exc_pendsv_stop(void)
Whether the last engine stop was a PENDSVSET context-switch stop.
Definition emu_exc.c:949
uint32_t emu_exc_pendsv_takes(void)
PendSV exceptions taken this run (report + idle signature).
Definition emu_exc.c:973
void exc_enter(uc_engine *uc, uint32_t exc_num, uint32_t handler)
Enter a Cortex-M exception: stack the basic frame and vector in.
Definition emu_exc.c:210
void emu_exc_clear_reboot_request(void)
Clear the latched warm-reboot request (after performing it).
bool emu_exc_reboot_requested(void)
Whether AIRCR.SYSRESETREQ requested a warm reboot.
void emu_exc_install_scb_nvic(uc_engine *uc)
Arm the SCB control-word and NVIC ISER/ICER write watchers.
uint32_t emu_exc_bkpt_pc(void)
PC of the BKPT that halted the run.
Definition emu_exc.c:961
void exc_return(uc_engine *uc, uint32_t exc_ret)
Perform a Cortex-M exception return for an observed EXC_RETURN branch.
Definition emu_exc.c:335
void emu_exc_install_core(uc_engine *uc)
Arm the core exception hooks (unmapped / INTR / ICSR watch).
Definition emu_exc.c:898
uint32_t emu_exc_svc_takes(void)
SVCall exceptions taken this run (report + idle signature).
Definition emu_exc.c:979
bool exc_take_pending(uc_engine *uc, uint32_t vtor_base, bool allow_systick)
Take the highest-priority pending exception, if one may activate now.
Definition emu_exc.c:530
void emu_exc_reset(void)
Reset the exception bookkeeping for a warm reboot.
Definition emu_exc.c:985
uint32_t emu_exc_systick_fires(void)
SysTick exceptions taken this run (report telemetry).
Definition emu_exc.c:967
Annotation-attribute framework macros for ra8-firmware.
@ k_byte_mask
Byte mask.
@ k_nvic_ispr_base
NVIC Interrupt Set-Pending Register array.
@ k_nvic_icer_base
NVIC Interrupt Clear-Enable Register array.
@ k_nvic_iser_base
NVIC Interrupt Set-Enable Register array.