1#[inline(always)]
5pub fn debugger_connected() -> bool {
6 cfg_select! {
7 xtensa => xtensa_lx::is_debugger_attached(),
8 all(riscv, soc_has_assist_debug) => crate::peripherals::ASSIST_DEBUG::regs()
9 .cpu(0)
10 .debug_mode()
11 .read()
12 .debug_module_active()
13 .bit_is_set(),
14 _ => false,
15 }
16}
17
18pub unsafe fn set_stack_watchpoint(addr: usize) {
27 assert!(addr.is_multiple_of(4));
28
29 if cfg!(stack_guard_monitoring_with_debugger_connected)
30 || !crate::debugger::debugger_connected()
31 {
32 cfg_select! {
33 xtensa => {
34 let addr = addr & !0b11;
35 let dbreakc = 0b1111100 | (1 << 31); unsafe {
38 core::arch::asm!(
39 "
40 wsr {addr}, 144 // 144 = dbreaka0
41 wsr {dbreakc}, 160 // 160 = dbreakc0
42 ",
43 addr = in(reg) addr,
44 dbreakc = in(reg) dbreakc,
45 );
46 }
47 }
48 _ => unsafe {
49 set_watchpoint(0, addr, 4);
50 },
51 }
52 }
53}
54
55#[cfg(riscv)]
56pub(crate) static DEBUGGER_LOCK: esp_sync::RawMutex = esp_sync::RawMutex::new();
57
58#[cfg(riscv)]
59const NAPOT_MATCH: u8 = 1;
60
61#[cfg(riscv)]
62bitfield::bitfield! {
63 #[derive(Clone, Copy, Default)]
65 pub(crate) struct Tdata1(u32);
66
67 pub bool, load, set_load: 0;
70
71 pub bool, store, set_store: 1;
74
75 pub bool, execute, set_execute: 2;
78
79 pub bool, u, set_u: 3;
81
82 pub bool, m, set_m: 6;
84
85 pub u8, _match, set_match: 10, 7;
93
94 pub u8, action, set_action: 15, 12;
100
101 pub bool, hit, set_hit: 20;
103
104 pub bool, dmode, set_dmode: 27;
109}
110
111#[cfg(riscv)]
112bitfield::bitfield! {
113 #[derive(Clone, Copy, Default)]
115 pub(crate) struct Tcontrol(u32);
116
117 pub bool, mte, set_mte: 3;
119
120 pub bool, mpte, set_mpte: 7;
122
123}
124
125#[cfg(interrupt_controller = "clic")]
127#[inline(always)]
128pub(crate) fn triggers_enabled_on_mret() -> bool {
129 let tcontrol: u32;
130 unsafe { core::arch::asm!("csrr {}, 0x7a5", out(reg) tcontrol) };
131 Tcontrol(tcontrol).mpte()
132}
133
134#[cfg(interrupt_controller = "clic")]
136#[inline(always)]
137pub(crate) unsafe fn enable_triggers_on_mret() {
138 let mut tcontrol = Tcontrol::default();
139 tcontrol.set_mpte(true);
140 unsafe { core::arch::asm!("csrs 0x7a5, {}", in(reg) tcontrol.0) };
141}
142
143#[cfg(riscv)]
144pub(crate) struct WatchPoint {
145 tdata1: u32,
146 tdata2: u32,
147}
148
149#[cfg(riscv)]
151pub(crate) unsafe fn clear_watchpoint(id: u8) -> WatchPoint {
152 assert!(id < 4);
153
154 let mut tdata1 = 0;
156 let mut tdata2 = 0;
157
158 DEBUGGER_LOCK.lock(|| unsafe {
159 core::arch::asm!(
160 "
161 csrw 0x7a0, {id} // tselect
162 csrrw {tdata1}, 0x7a1, {tdata2} // tdata1
163 csrr {tdata2}, 0x7a2 // tdata2
164 ", id = in(reg) id,
165 tdata1 = inout(reg) tdata1,
166 tdata2 = out(reg) tdata2,
167 );
168 });
169
170 WatchPoint { tdata1, tdata2 }
171}
172
173#[cfg(riscv)]
175pub(crate) unsafe fn restore_watchpoint(id: u8, watchpoint: WatchPoint) {
176 DEBUGGER_LOCK.lock(|| unsafe {
177 core::arch::asm!(
178 "
179 csrw 0x7a0, {id} // tselect
180 csrw 0x7a1, {tdata1} // tdata1
181 csrw 0x7a2, {tdata2} // tdata2
182 ", id = in(reg) id,
183 tdata1 = in(reg) watchpoint.tdata1,
184 tdata2 = in(reg) watchpoint.tdata2,
185 );
186 });
187}
188
189#[cfg(all(riscv, feature = "exception-handler"))]
191pub(crate) unsafe fn watchpoint_hit(id: u8) -> bool {
192 assert!(id < 4);
193 let mut tdata = Tdata1::default();
194
195 DEBUGGER_LOCK.lock(|| unsafe {
196 core::arch::asm!(
197 "
198 csrw 0x7a0, {id} // tselect
199 csrr {tdata}, 0x7a1 // tdata1
200 ", id = in(reg) id,
201 tdata = out(reg) tdata.0,
202 );
203 });
204
205 tdata.hit()
206}
207
208#[cfg(riscv)]
210pub(crate) unsafe fn set_watchpoint(id: u8, addr: usize, len: usize) {
211 assert!(id < 4);
212 assert!(len.is_power_of_two());
213 assert!(addr.is_multiple_of(len));
214
215 let z = len.trailing_zeros();
216 let mask = {
217 let mut mask: usize = 0;
218 for i in 0..z {
219 mask |= 1 << i;
220 }
221 mask
222 };
223
224 let napot_encoding = { mask & !(1 << (z - 1)) };
225 let addr = (addr & !mask) | napot_encoding;
226
227 let mut tdata = Tdata1::default();
228 tdata.set_m(true);
229 tdata.set_store(true);
230 tdata.set_match(NAPOT_MATCH);
231 let tdata: u32 = tdata.0;
232
233 let mut tcontrol = Tcontrol::default();
236 tcontrol.set_mte(true);
237 tcontrol.set_mpte(true);
238 let tcontrol: u32 = tcontrol.0;
239
240 DEBUGGER_LOCK.lock(|| unsafe {
241 core::arch::asm!(
242 "
243 csrw 0x7a0, {id} // tselect
244 csrw 0x7a5, {tcontrol} // tcontrol
245 csrw 0x7a1, {tdata} // tdata1
246 csrw 0x7a2, {addr} // tdata2
247 ", id = in(reg) id,
248 addr = in(reg) addr,
249 tdata = in(reg) tdata,
250 tcontrol = in(reg) tcontrol,
251 );
252 });
253}