1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "pico/runtime.h"
#if !PICO_RUNTIME_NO_INIT_PER_CORE_INSTALL_STACK_GUARD
#if PICO_RP2040
#include "hardware/structs/mpu.h"
#elif defined(__riscv)
#include "hardware/riscv.h"
#endif
void runtime_init_per_core_install_stack_guard(void *stack_bottom) {
uintptr_t addr = (uintptr_t) stack_bottom;
addr = (addr + 31u) & ~31u;
#if PICO_RP2040
if (mpu_hw->ctrl) {
__breakpoint();
}if (mpu_hw->ctrl) { ... }
uint32_t subregion_select = 0xffu ^ (1u << ((addr >> 5u) & 7u));
mpu_hw->ctrl = 5;
mpu_hw->rbar = (addr & (uint)~0xff) | M0PLUS_MPU_RBAR_VALID_BITS | 0;
mpu_hw->rasr = 1
| (0x7 << 1)
| (subregion_select << 8)
| 0x10000000;
/* ... */
#elif defined(__riscv)
#if !PICO_RP2350
#error "Check PMP configuration for new platform"
#endif
bool dirty_pmp =
riscv_read_csr(pmpcfg0) != 0 ||
riscv_read_csr(pmpcfg1) != 0 ||
riscv_read_csr(RVCSR_PMPCFGM0_OFFSET) != 0;
if (dirty_pmp) {
__breakpoint();
}if (dirty_pmp) { ... }
riscv_write_csr(pmpaddr0, (addr | 0x0fu) >> 2);
riscv_write_csr(RVCSR_PMPCFGM0_OFFSET, 0x1u);
riscv_write_csr(pmpcfg0, RVCSR_PMPCFG0_R0_A_VALUE_NAPOT << RVCSR_PMPCFG0_R0_A_LSB);
/* ... */
#else
pico_default_asm_volatile(
"msr msplim, %0"
:
: "r" (stack_bottom));/* ... */
#endif
}{ ... }
/* ... */#endif