opentitanlib/debug/
openocd.rs

1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5use std::fmt::LowerHex;
6use std::io::{BufRead, BufReader, Write};
7use std::mem::size_of;
8use std::net::TcpStream;
9use std::os::unix::process::CommandExt;
10use std::path::Path;
11use std::process::{Child, Command, Stdio};
12use std::time::{Duration, Instant};
13
14use anyhow::{Context, Result, bail, ensure};
15use regex::Regex;
16use serde::{Deserialize, Serialize};
17use thiserror::Error;
18
19use ot_hal::dif::lc_ctrl::LcCtrlReg;
20
21use crate::impl_serializable_error;
22use crate::io::jtag::{Jtag, JtagChain, JtagError, JtagParams, JtagTap, RiscvReg};
23use crate::regex;
24use crate::util::parse_int::ParseInt;
25use crate::util::printer;
26
27/// Represents an OpenOCD server that we can interact with.
28pub struct OpenOcd {
29    /// OpenOCD child process.
30    server_process: Child,
31    /// Receiving side of the stream to the telnet interface of OpenOCD.
32    reader: BufReader<TcpStream>,
33    /// Sending side of the stream to the telnet interface of OpenOCD.
34    writer: TcpStream,
35}
36
37impl Drop for OpenOcd {
38    fn drop(&mut self) {
39        let _ = self.server_process.kill();
40    }
41}
42
43impl OpenOcd {
44    /// How long to wait for OpenOCD to get ready to accept a TCL connection.
45    const OPENOCD_TCL_READY_TMO: Duration = Duration::from_secs(30);
46
47    /// Wait until we see a particular message on the output.
48    fn wait_until_regex_match<'a>(
49        stderr: &mut impl BufRead,
50        regex: &Regex,
51        timeout: Duration,
52        log_stdio: bool,
53        s: &'a mut String,
54    ) -> Result<regex::Captures<'a>> {
55        let start = Instant::now();
56        loop {
57            // NOTE the read could block indefinitely, a proper solution would involved spawning
58            // a thread or using async.
59            let n = stderr.read_line(s)?;
60            if n == 0 {
61                bail!("OpenOCD stopped before being ready?");
62            }
63            if log_stdio {
64                log::info!(target: concat!(module_path!(), "::stderr"), "{}", s);
65            }
66            if regex.is_match(s) {
67                // This is not a `if let Some(capture) = regex.captures(s) {}` to to Rust
68                // borrow checker limitations. Can be modified if Polonius lands.
69                return Ok(regex.captures(s).unwrap());
70            }
71            s.clear();
72
73            if start.elapsed() >= timeout {
74                bail!("OpenOCD did not become ready to accept a TCL connection");
75            }
76        }
77    }
78
79    /// Spawn an OpenOCD server with given path.
80    pub fn spawn(path: &Path, log_stdio: bool) -> Result<Self> {
81        let mut cmd = Command::new(path);
82
83        // Let OpenOCD choose which port to bind to, in order to never unnecesarily run into
84        // issues due to a particular port already being in use.
85        // We don't use the telnet and GDB ports so disable them.
86        // The configuration will happen through the TCL interface, so use `noinit` to prevent
87        // OpenOCD from transition to execution mode.
88        cmd.arg("-c")
89            .arg("tcl_port 0; telnet_port disabled; gdb_port disabled; noinit;");
90
91        log::info!("Spawning OpenOCD: {cmd:?}");
92
93        cmd.stdin(Stdio::null())
94            .stdout(Stdio::piped())
95            .stderr(Stdio::piped());
96
97        // SAFETY: prctl is a syscall which is atomic and thus async-signal-safe.
98        unsafe {
99            cmd.pre_exec(|| {
100                // Since we use OpenOCD as a library, make sure it's killed when
101                // the parent process dies. This setting is preserved across execve.
102                rustix::process::set_parent_process_death_signal(Some(
103                    rustix::process::Signal::HUP,
104                ))?;
105                Ok(())
106            });
107        }
108
109        let mut child = cmd
110            .spawn()
111            .with_context(|| format!("failed to spawn openocd: {cmd:?}",))?;
112        let stdout = child.stdout.take().unwrap();
113        let mut stderr = BufReader::new(child.stderr.take().unwrap());
114        // Wait until we see 'Info : Listening on port XXX for tcl connections' before knowing
115        // which port to connect to.
116        if log_stdio {
117            log::info!("Waiting for OpenOCD to be ready to accept a TCL connection...");
118        }
119        let mut buf = String::new();
120        let regex_captures = Self::wait_until_regex_match(
121            &mut stderr,
122            regex!("Info : Listening on port ([0-9]+) for tcl connections"),
123            Self::OPENOCD_TCL_READY_TMO,
124            log_stdio,
125            &mut buf,
126        )
127        .context("OpenOCD was not ready in time to accept a connection")?;
128        let openocd_port: u16 = regex_captures.get(1).unwrap().as_str().parse()?;
129        // Print stdout and stderr with log
130        if log_stdio {
131            std::thread::spawn(move || {
132                printer::accumulate(
133                    stdout,
134                    concat!(module_path!(), "::stdout"),
135                    Default::default(),
136                )
137            });
138            std::thread::spawn(move || {
139                printer::accumulate(
140                    stderr,
141                    concat!(module_path!(), "::stderr"),
142                    Default::default(),
143                )
144            });
145        }
146
147        let kill_guard = scopeguard::guard(child, |mut child| {
148            let _ = child.kill();
149        });
150
151        log::info!("Connecting to OpenOCD tcl interface...");
152
153        let stream = TcpStream::connect(("localhost", openocd_port))
154            .context("failed to connect to OpenOCD socket")?;
155
156        // Disable TCP Nagle delay to ensure minimal latency to OpenOCD.
157        // Without this, roundtrip communications can take 50ms which adds
158        // up to be longer than certain timeouts, e.g. the RMA loop in ROM.
159        stream
160            .set_nodelay(true)
161            .context("failed to disable TCP socket delay")?;
162
163        let mut connection = Self {
164            server_process: scopeguard::ScopeGuard::into_inner(kill_guard),
165            reader: BufReader::new(stream.try_clone()?),
166            writer: stream,
167        };
168
169        // Test the connection by asking for OpenOCD's version.
170        let version = connection.execute("version")?;
171        log::info!("OpenOCD version: {version}");
172
173        Ok(connection)
174    }
175
176    /// Send a string to OpenOCD Tcl interface.
177    fn send(&mut self, cmd: &str) -> Result<()> {
178        // The protocol is to send the command followed by a `0x1a` byte,
179        // see https://openocd.org/doc/html/Tcl-Scripting-API.html#Tcl-RPC-server
180
181        // Sanity check to ensure that the command string is not malformed.
182        if cmd.contains('\x1A') {
183            bail!("TCL command string should be contained inside the text to send");
184        }
185
186        self.writer
187            .write_all(cmd.as_bytes())
188            .context("failed to send a command to OpenOCD server")?;
189        self.writer
190            .write_all(&[0x1a])
191            .context("failed to send the command terminator to OpenOCD server")?;
192        self.writer.flush().context("failed to flush stream")?;
193        Ok(())
194    }
195
196    fn recv(&mut self) -> Result<String> {
197        let mut buf = Vec::new();
198        self.reader.read_until(0x1A, &mut buf)?;
199        if !buf.ends_with(b"\x1A") {
200            bail!(OpenOcdError::PrematureExit);
201        }
202        buf.pop();
203        String::from_utf8(buf).context("failed to parse OpenOCD response as UTF-8")
204    }
205
206    pub fn shutdown(mut self) -> Result<()> {
207        self.execute("shutdown")?;
208        // Wait for it to exit.
209        self.server_process
210            .wait()
211            .context("failed to wait for OpenOCD server to exit")?;
212        Ok(())
213    }
214
215    /// Send a TCL command to OpenOCD and wait for its response.
216    pub fn execute(&mut self, cmd: &str) -> Result<String> {
217        self.send(cmd)?;
218        self.recv()
219    }
220
221    /// Load instruction register of a given tap.
222    pub fn irscan(&mut self, tap: &str, ir: u32) -> Result<()> {
223        let cmd = format!("irscan {} {:#x}", tap, ir);
224        let result = self.execute(&cmd)?;
225        ensure!(result.is_empty(), "unexpected response: '{result}'");
226        Ok(())
227    }
228
229    /// Load data register of a given tap and return the scan.
230    pub fn drscan<T: ParseInt + LowerHex>(
231        &mut self,
232        tap: &str,
233        numbits: u32,
234        data: T,
235    ) -> Result<T> {
236        let cmd = format!("drscan {} {} {:#x}", tap, numbits, data);
237        let result = self.execute(&cmd)?;
238        Ok(T::from_str_radix(&result, 16).map_err(|x| x.into())?)
239    }
240}
241
242/// An JTAG interface driver over OpenOCD.
243pub struct OpenOcdJtagChain {
244    /// OpenOCD server instance.
245    openocd: OpenOcd,
246}
247
248/// Errors related to the OpenOCD server.
249#[derive(Error, Debug, Deserialize, Serialize)]
250pub enum OpenOcdError {
251    #[error("OpenOCD initialization failed: {0}")]
252    InitializeFailure(String),
253    #[error("OpenOCD server exists prematurely")]
254    PrematureExit,
255    #[error("Generic error {0}")]
256    Generic(String),
257}
258impl_serializable_error!(OpenOcdError);
259
260impl OpenOcdJtagChain {
261    /// Start OpenOCD with given JTAG options but do not connect any TAP.
262    pub fn new(adapter_command: &str, opts: &JtagParams) -> Result<OpenOcdJtagChain> {
263        let mut openocd = OpenOcd::spawn(&opts.openocd, opts.log_stdio)?;
264
265        openocd.execute(adapter_command)?;
266        openocd.execute(&format!("adapter speed {}", opts.adapter_speed_khz))?;
267        openocd.execute("transport select jtag")?;
268        openocd.execute("scan_chain")?;
269
270        Ok(OpenOcdJtagChain { openocd })
271    }
272}
273
274impl JtagChain for OpenOcdJtagChain {
275    fn connect(mut self: Box<Self>, tap: JtagTap) -> Result<Box<dyn Jtag>> {
276        // Pass through the config for the chosen TAP.
277        let target = match tap {
278            JtagTap::RiscvTap => include_str!(env!("openocd_riscv_target_cfg")),
279            JtagTap::LcTap => include_str!(env!("openocd_lc_target_cfg")),
280        };
281        self.openocd.execute(target)?;
282
283        // Capture outputs during initialization to see if error has occurred during the process.
284        let resp = self.openocd.execute("capture init")?;
285        if resp.contains("JTAG scan chain interrogation failed") {
286            bail!(OpenOcdError::InitializeFailure(resp));
287        }
288
289        Ok(Box::new(OpenOcdJtagTap {
290            openocd: self.openocd,
291            jtag_tap: tap,
292        }))
293    }
294
295    fn into_raw(self: Box<Self>) -> Result<OpenOcd> {
296        Ok(self.openocd)
297    }
298}
299
300/// An JTAG interface driver over OpenOCD.
301pub struct OpenOcdJtagTap {
302    /// OpenOCD server instance.
303    openocd: OpenOcd,
304    /// JTAG TAP OpenOCD is connected to.
305    jtag_tap: JtagTap,
306}
307
308impl OpenOcdJtagTap {
309    /// Send a TCL command to OpenOCD and wait for its response.
310    fn send_tcl_cmd(&mut self, cmd: &str) -> Result<String> {
311        self.openocd.execute(cmd)
312    }
313
314    fn read_memory_impl<T: ParseInt>(&mut self, addr: u32, buf: &mut [T]) -> Result<usize> {
315        // Ibex does not have a MMU so always tell OpenOCD that we are using physical addresses
316        // otherwise it will try to translate the address through the (non-existent) MMU
317        let cmd = format!(
318            "read_memory 0x{addr:x} {width} {count} phys",
319            width = 8 * size_of::<T>(),
320            count = buf.len()
321        );
322        let response = self.send_tcl_cmd(cmd.as_str())?;
323        response.trim().split(' ').try_fold(0, |idx, val| {
324            if idx < buf.len() {
325                buf[idx] = T::from_str(val).context(format!(
326                    "expected response to be an hexadecimal byte, got '{response}'"
327                ))?;
328                Ok(idx + 1)
329            } else {
330                bail!("OpenOCD returned too much data on read".to_string())
331            }
332        })
333    }
334
335    fn write_memory_impl<T: ToString>(&mut self, addr: u32, bigbuf: &[T]) -> Result<()> {
336        const CHUNK_SIZE: usize = 1024;
337        for (idx, buf) in bigbuf.chunks(CHUNK_SIZE).enumerate() {
338            // Convert data to space-separated strings.
339            let data: Vec<_> = buf.iter().map(ToString::to_string).collect();
340            let data_str = &data[..].join(" ");
341            // See [read_memory] about physical addresses
342            let cmd = format!(
343                "write_memory 0x{chunk_addr:x} {width} {{ {data_str} }} phys",
344                chunk_addr = addr + (idx * CHUNK_SIZE * size_of::<T>()) as u32,
345                width = 8 * size_of::<T>()
346            );
347            let response = self.send_tcl_cmd(cmd.as_str())?;
348            if !response.is_empty() {
349                bail!("unexpected response: '{response}'");
350            }
351        }
352
353        Ok(())
354    }
355
356    /// Read a register: this function does not attempt to translate the
357    /// name or number of the register. If force is set, bypass OpenOCD's
358    /// register cache.
359    fn read_register<T: ParseInt>(&mut self, reg_name: &str, force: bool) -> Result<T> {
360        let cmd = format!(
361            "get_reg {} {{ {} }}",
362            if force { "-force" } else { "" },
363            reg_name,
364        );
365        let response = self.send_tcl_cmd(cmd.as_str())?;
366        // the expected output format is 'reg_name 0xabcdef', e.g 'pc 0x10009858'
367        let (out_reg_name, value) = response.trim().split_once(' ').with_context(|| {
368            format!("expected response of the form 'reg value', got '{response}'")
369        })?;
370        ensure!(
371            out_reg_name == reg_name,
372            "OpenOCD returned the value for register '{out_reg_name}' instead of '{reg_name}"
373        );
374        T::from_str(value).context(format!(
375            "expected value to be an hexadecimal string, got '{value}'"
376        ))
377    }
378
379    fn write_register<T: ToString>(&mut self, reg_name: &str, value: T) -> Result<()> {
380        let cmd = format!("set_reg {{ {reg_name} {} }}", T::to_string(&value));
381        let response = self.send_tcl_cmd(cmd.as_str())?;
382        if !response.is_empty() {
383            bail!("unexpected response: '{response}'");
384        }
385
386        Ok(())
387    }
388}
389
390impl Jtag for OpenOcdJtagTap {
391    fn into_raw(self: Box<Self>) -> Result<OpenOcd> {
392        Ok(self.openocd)
393    }
394
395    fn as_raw(&mut self) -> Result<&mut OpenOcd> {
396        Ok(&mut self.openocd)
397    }
398
399    fn disconnect(self: Box<Self>) -> Result<()> {
400        self.openocd.shutdown()
401    }
402
403    fn tap(&self) -> JtagTap {
404        self.jtag_tap
405    }
406
407    fn read_lc_ctrl_reg(&mut self, reg: &LcCtrlReg) -> Result<u32> {
408        ensure!(
409            matches!(self.jtag_tap, JtagTap::LcTap),
410            JtagError::Tap(self.jtag_tap)
411        );
412        let reg_offset = reg.word_offset();
413        let cmd = format!("riscv dmi_read 0x{reg_offset:x}");
414        let response = self.send_tcl_cmd(cmd.as_str())?;
415
416        let value = u32::from_str(response.trim()).context(format!(
417            "expected response to be hexadecimal word, got '{response}'"
418        ))?;
419
420        Ok(value)
421    }
422
423    fn write_lc_ctrl_reg(&mut self, reg: &LcCtrlReg, value: u32) -> Result<()> {
424        ensure!(
425            matches!(self.jtag_tap, JtagTap::LcTap),
426            JtagError::Tap(self.jtag_tap)
427        );
428        let reg_offset = reg.word_offset();
429        let cmd = format!("riscv dmi_write 0x{reg_offset:x} 0x{value:x}");
430        let response = self.send_tcl_cmd(cmd.as_str())?;
431
432        if !response.is_empty() {
433            bail!("unexpected response: '{response}'");
434        }
435
436        Ok(())
437    }
438
439    fn read_memory(&mut self, addr: u32, buf: &mut [u8]) -> Result<usize> {
440        ensure!(
441            matches!(self.jtag_tap, JtagTap::RiscvTap),
442            JtagError::Tap(self.jtag_tap)
443        );
444        self.read_memory_impl(addr, buf)
445    }
446
447    fn read_memory32(&mut self, addr: u32, buf: &mut [u32]) -> Result<usize> {
448        ensure!(
449            matches!(self.jtag_tap, JtagTap::RiscvTap),
450            JtagError::Tap(self.jtag_tap)
451        );
452        self.read_memory_impl(addr, buf)
453    }
454
455    fn write_memory(&mut self, addr: u32, buf: &[u8]) -> Result<()> {
456        ensure!(
457            matches!(self.jtag_tap, JtagTap::RiscvTap),
458            JtagError::Tap(self.jtag_tap)
459        );
460        self.write_memory_impl(addr, buf)
461    }
462
463    fn write_memory32(&mut self, addr: u32, buf: &[u32]) -> Result<()> {
464        ensure!(
465            matches!(self.jtag_tap, JtagTap::RiscvTap),
466            JtagError::Tap(self.jtag_tap)
467        );
468        self.write_memory_impl(addr, buf)
469    }
470
471    fn halt(&mut self) -> Result<()> {
472        ensure!(
473            matches!(self.jtag_tap, JtagTap::RiscvTap),
474            JtagError::Tap(self.jtag_tap)
475        );
476        let response = self.send_tcl_cmd("halt")?;
477        if !response.is_empty() {
478            bail!("unexpected response: '{response}'");
479        }
480
481        Ok(())
482    }
483
484    fn wait_halt(&mut self, timeout: Duration) -> Result<()> {
485        ensure!(
486            matches!(self.jtag_tap, JtagTap::RiscvTap),
487            JtagError::Tap(self.jtag_tap)
488        );
489        let cmd = format!("wait_halt {}", timeout.as_millis());
490        let response = self.send_tcl_cmd(cmd.as_str())?;
491        if !response.is_empty() {
492            bail!("unexpected response: '{response}'");
493        }
494        Ok(())
495    }
496
497    fn resume(&mut self) -> Result<()> {
498        ensure!(
499            matches!(self.jtag_tap, JtagTap::RiscvTap),
500            JtagError::Tap(self.jtag_tap)
501        );
502        let response = self.send_tcl_cmd("resume")?;
503        if !response.is_empty() {
504            bail!("unexpected response: '{response}'");
505        }
506
507        Ok(())
508    }
509
510    fn resume_at(&mut self, addr: u32) -> Result<()> {
511        ensure!(
512            matches!(self.jtag_tap, JtagTap::RiscvTap),
513            JtagError::Tap(self.jtag_tap)
514        );
515        let cmd = format!("resume 0x{:x}", addr);
516        let response = self.send_tcl_cmd(&cmd)?;
517        if !response.is_empty() {
518            bail!("unexpected response: '{response}'");
519        }
520
521        Ok(())
522    }
523
524    fn reset(&mut self, run: bool) -> Result<()> {
525        ensure!(
526            matches!(self.jtag_tap, JtagTap::RiscvTap),
527            JtagError::Tap(self.jtag_tap)
528        );
529        let cmd = format!("reset {}", if run { "run" } else { "halt" });
530        let response = self.send_tcl_cmd(&cmd)?;
531        if !response.is_empty() {
532            bail!("unexpected response: '{response}'");
533        }
534
535        Ok(())
536    }
537
538    fn step(&mut self) -> Result<()> {
539        ensure!(
540            matches!(self.jtag_tap, JtagTap::RiscvTap),
541            JtagError::Tap(self.jtag_tap)
542        );
543        let response = self.send_tcl_cmd("step")?;
544        if !response.is_empty() {
545            bail!("unexpected response: '{response}'");
546        }
547
548        Ok(())
549    }
550
551    fn step_at(&mut self, addr: u32) -> Result<()> {
552        ensure!(
553            matches!(self.jtag_tap, JtagTap::RiscvTap),
554            JtagError::Tap(self.jtag_tap)
555        );
556        let cmd = format!("step 0x{:x}", addr);
557        let response = self.send_tcl_cmd(&cmd)?;
558        if !response.is_empty() {
559            bail!("unexpected response: '{response}'");
560        }
561
562        Ok(())
563    }
564
565    fn read_riscv_reg(&mut self, reg: &RiscvReg) -> Result<u32> {
566        ensure!(
567            matches!(self.jtag_tap, JtagTap::RiscvTap),
568            JtagError::Tap(self.jtag_tap)
569        );
570        self.read_register::<u32>(reg.name(), true)
571    }
572
573    fn write_riscv_reg(&mut self, reg: &RiscvReg, val: u32) -> Result<()> {
574        ensure!(
575            matches!(self.jtag_tap, JtagTap::RiscvTap),
576            JtagError::Tap(self.jtag_tap)
577        );
578        self.write_register(reg.name(), val)
579    }
580
581    fn set_breakpoint(&mut self, address: u32, hw: bool) -> Result<()> {
582        let cmd = format!("bp {:#x} 2{}", address, if hw { " hw" } else { "" });
583        let response = self.send_tcl_cmd(&cmd)?;
584        if !response.starts_with("breakpoint set at ") {
585            bail!("unexpected response: '{response}'");
586        }
587        Ok(())
588    }
589
590    fn remove_breakpoint(&mut self, addr: u32) -> Result<()> {
591        let cmd = format!("rbp {:#x}", addr);
592        let response = self.send_tcl_cmd(&cmd)?;
593        if !response.is_empty() {
594            bail!("unexpected response: '{response}'");
595        }
596        Ok(())
597    }
598
599    fn remove_all_breakpoints(&mut self) -> Result<()> {
600        let response = self.send_tcl_cmd("rbp all")?;
601        if !response.is_empty() {
602            bail!("unexpected response: '{response}'");
603        }
604        Ok(())
605    }
606}