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