1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use std::fmt::{Debug, Display};
use std::fs;
use std::ops::{Deref, DerefMut, Range};
use std::path::Path;
use std::time::Duration;

use anyhow::Result;
use object::{Object, ObjectSymbol};

use crate::io::jtag::{Jtag, RiscvCsr, RiscvGpr, RiscvReg};

pub struct ElfSymbols {
    symbols: HashMap<String, u32>,
}

pub struct ElfDebugger<'a> {
    symbols: &'a ElfSymbols,
    jtag: Box<dyn Jtag + 'a>,
}

impl<'a> Deref for ElfDebugger<'a> {
    type Target = dyn Jtag + 'a;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &*self.jtag
    }
}

impl<'a> DerefMut for ElfDebugger<'a> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.jtag
    }
}

#[derive(Clone)]
pub enum SymbolicAddress {
    Absolute(u32),
    SymbolRelative(String, u32),
}

impl From<String> for SymbolicAddress {
    #[inline]
    fn from(s: String) -> Self {
        Self::SymbolRelative(s, 0)
    }
}

impl From<&str> for SymbolicAddress {
    #[inline]
    fn from(s: &str) -> Self {
        Self::SymbolRelative(s.to_owned(), 0)
    }
}

impl From<u32> for SymbolicAddress {
    #[inline]
    fn from(s: u32) -> Self {
        Self::Absolute(s)
    }
}

impl std::ops::Add<u32> for SymbolicAddress {
    type Output = Self;

    #[inline]
    fn add(self, rhs: u32) -> Self::Output {
        match self {
            SymbolicAddress::Absolute(addr) => SymbolicAddress::Absolute(addr + rhs),
            SymbolicAddress::SymbolRelative(symbol, offset) => {
                SymbolicAddress::SymbolRelative(symbol, offset + rhs)
            }
        }
    }
}

impl Debug for SymbolicAddress {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self, f)
    }
}

impl Display for SymbolicAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SymbolicAddress::Absolute(addr) => write!(f, "{addr:#x}"),
            SymbolicAddress::SymbolRelative(symbol, offset) => {
                if *offset == 0 {
                    write!(f, "{symbol}")
                } else {
                    write!(f, "{symbol} + {offset:#x}")
                }
            }
        }
    }
}

#[derive(Clone)]
pub struct ResolvedAddress {
    pub address: SymbolicAddress,
    pub resolution: u32,
}

impl Debug for ResolvedAddress {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self, f)
    }
}

impl Display for ResolvedAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.address, f)?;

        // Only output the resolved address if it's not already absolute.
        if !matches!(self.address, SymbolicAddress::Absolute(_)) {
            write!(f, " ({:#x})", self.resolution)?;
        }

        Ok(())
    }
}

impl ElfSymbols {
    pub fn load_elf(path: impl AsRef<Path>) -> Result<Self> {
        let elf_binary = fs::read(path)?;
        let elf_file = object::File::parse(&*elf_binary)?;
        let mut symbols = HashMap::new();
        for sym in elf_file.symbols() {
            symbols.insert(sym.name()?.to_owned(), sym.address() as u32);
        }
        Ok(Self { symbols })
    }

    /// Resolve a symbolic address.
    pub fn resolve(&self, address: impl Into<SymbolicAddress>) -> Result<ResolvedAddress> {
        let address = address.into();
        let resolution = match address {
            SymbolicAddress::Absolute(addr) => addr,
            SymbolicAddress::SymbolRelative(ref symbol, offset) => {
                let Some(symbol_addr) = self.symbols.get(symbol).copied() else {
                    anyhow::bail!("Cannot resolve symbol {symbol}");
                };
                symbol_addr + offset
            }
        };
        Ok(ResolvedAddress {
            address,
            resolution,
        })
    }

    /// Attach to a JTAG interface for debugging.
    pub fn attach<'a>(&'a self, jtag: Box<dyn Jtag + 'a>) -> ElfDebugger<'a> {
        ElfDebugger {
            symbols: self,
            jtag,
        }
    }
}

impl<'a> ElfDebugger<'a> {
    pub fn disconnect(self) -> Result<()> {
        self.jtag.disconnect()
    }

    pub fn resolve(&self, address: impl Into<SymbolicAddress>) -> Result<ResolvedAddress> {
        self.symbols.resolve(address)
    }

    /// Read a RISC-V register.
    ///
    /// This is a convience wrapper around `read_riscv_reg` provided by the JTAG trait.
    pub fn read_reg(&mut self, reg: impl Into<RiscvReg>) -> Result<u32> {
        self.read_riscv_reg(&reg.into())
    }

    /// Write a RISC-V register.
    ///
    /// This is a convience wrapper around `write_riscv_reg` provided by the JTAG trait.
    pub fn write_reg(&mut self, reg: impl Into<RiscvReg>, value: u32) -> Result<()> {
        self.write_riscv_reg(&reg.into(), value)
    }

    /// Read a 32-bit word from memory.
    ///
    /// This is a convience wrapper around `read_memory32` provided by the JTAG trait.
    pub fn read_u32(&mut self, addr: u32) -> Result<u32> {
        let mut ret = [0];
        self.read_memory32(addr, &mut ret)?;
        Ok(ret[0])
    }

    /// Write a 32-bit word to memory.
    ///
    /// This is a convience wrapper around `write_memory32` provided by the JTAG trait.
    pub fn write_u32(&mut self, addr: u32, value: u32) -> Result<()> {
        self.write_memory32(addr, &[value])
    }

    /// Read the program counter.
    pub fn get_pc(&mut self) -> Result<u32> {
        self.read_riscv_reg(&RiscvReg::Csr(RiscvCsr::DPC))
    }

    /// Set the program counter to the given address.
    pub fn set_pc(&mut self, address: impl Into<SymbolicAddress>) -> Result<()> {
        let resolved = self.resolve(address)?;
        log::info!("Set PC to {}", resolved);
        self.write_reg(RiscvCsr::DPC, resolved.resolution)?;
        Ok(())
    }

    /// Set a breakpoint at the given address.
    pub fn set_breakpoint(&mut self, address: impl Into<SymbolicAddress>) -> Result<()> {
        let resolved = self.resolve(address)?;
        log::info!("Set breakpoint at {}", resolved);
        self.jtag.set_breakpoint(resolved.resolution, true)?;
        Ok(())
    }

    /// Assert the current program counter is at the given address.
    pub fn expect_pc(&mut self, address: impl Into<SymbolicAddress>) -> Result<()> {
        let resolved = self.resolve(address)?;
        let pc = self.get_pc()?;
        log::info!("PC = {:#x}, expected PC = {}", pc, resolved);
        if pc != resolved.resolution {
            anyhow::bail!("unexpected PC");
        }
        Ok(())
    }

    /// Assert the current program counter is within the given range.
    pub fn expect_pc_range(&mut self, range: Range<impl Into<SymbolicAddress>>) -> Result<()> {
        let start = self.resolve(range.start)?;
        let end = self.resolve(range.end)?;
        let pc = self.get_pc()?;
        log::info!("PC = {:#x}, expected PC = {}..{}", pc, start, end,);
        if !(start.resolution..end.resolution).contains(&pc) {
            anyhow::bail!("unexpected PC");
        }
        Ok(())
    }

    /// Continue execution until the address is hit.
    ///
    /// Note that a breakpoint must not already exist for the target address, otherwise
    /// this function will fail.
    ///
    /// This function will also fail if a pre-existing breakpoint is hit before the target
    /// address is hit.
    pub fn run_until(
        &mut self,
        address: impl Into<SymbolicAddress>,
        timeout: Duration,
    ) -> Result<()> {
        let resolved = self.resolve(address)?;
        log::info!("Run until {}", resolved);
        self.jtag.set_breakpoint(resolved.resolution, true)?;
        self.jtag.resume()?;
        self.jtag.wait_halt(timeout)?;
        self.expect_pc(resolved.resolution)?;
        self.jtag.remove_breakpoint(resolved.resolution)?;
        Ok(())
    }

    /// Execute until the current function returns.
    ///
    /// This implementation does not use the debugging information from ELF files, and only uses the RA register,
    /// so it only works when RA has not been overriden, e.g. at the preamble of the function.
    pub fn finish(&mut self, timeout: Duration) -> Result<()> {
        let ra = self.read_reg(RiscvGpr::RA)?;
        self.run_until(ra, timeout)
    }

    /// Call a function with the given arguments.
    pub fn call(
        &mut self,
        address: impl Into<SymbolicAddress>,
        args: &[u32],
        timeout: Duration,
    ) -> Result<(u32, u32)> {
        // We're oblivious to the current context, so save all caller-saved registers.
        // In addition, also save SP, just in case that we have to modify it to align.
        const REGS_TO_SAVE: &[RiscvGpr] = &[
            RiscvGpr::RA,
            RiscvGpr::SP,
            RiscvGpr::T0,
            RiscvGpr::T1,
            RiscvGpr::T2,
            RiscvGpr::A0,
            RiscvGpr::A1,
            RiscvGpr::A2,
            RiscvGpr::A3,
            RiscvGpr::A4,
            RiscvGpr::A5,
            RiscvGpr::A6,
            RiscvGpr::A7,
            RiscvGpr::T3,
            RiscvGpr::T4,
            RiscvGpr::T5,
            RiscvGpr::T6,
        ];
        let mut saved = [0; REGS_TO_SAVE.len()];
        for (idx, gpr) in REGS_TO_SAVE.iter().copied().enumerate() {
            saved[idx] = self.read_reg(gpr)?;
        }

        // Align the stack if it's not currently 16-byte aligned.
        // This rounds down to avoid overwriting anything on stack.
        if saved[1] % 16 != 0 {
            self.write_reg(RiscvGpr::SP, saved[1] & !15)?;
        }

        // Set up arguments
        const ARG_GPRS: &[RiscvGpr] = &[
            RiscvGpr::A0,
            RiscvGpr::A1,
            RiscvGpr::A2,
            RiscvGpr::A3,
            RiscvGpr::A4,
            RiscvGpr::A5,
            RiscvGpr::A6,
        ];
        assert!(args.len() < ARG_GPRS.len());
        for (gpr, arg) in ARG_GPRS.iter().copied().zip(args.iter().copied()) {
            self.write_reg(gpr, arg)?;
        }

        // Set up call frame, and run until the current PC.
        // NOTE: This wouldn't guard against recursive function calls;
        // to support that we also need to check that SP is expected, but
        // this should be enough for all our current tests.
        let pc = self.get_pc()?;
        self.write_reg(RiscvGpr::RA, pc)?;
        self.set_pc(address)?;
        self.run_until(pc, timeout)?;

        // A0 and A1 serve as return value, read them.
        let a0 = self.read_reg(RiscvGpr::A0)?;
        let a1 = self.read_reg(RiscvGpr::A1)?;

        // Restore all registers
        for (idx, gpr) in REGS_TO_SAVE.iter().copied().enumerate() {
            self.write_reg(gpr, saved[idx])?;
        }

        Ok((a0, a1))
    }
}