rigless 0.0.2

Virtual hardware-in-the-loop runtime: run unmodified firmware on an emulated MCU with every peripheral, chip model, and unit of virtual time in Rust; needs the rigless-sidecar package from rigless-labs/qemu
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (c) 2026 Rigless Labs.
// SPDX-License-Identifier: Apache-2.0
//! The `rigless` command: `run` executes a firmware on each MCU of a machine, `svd generate`
//! turns a vendor's SVD into the register tables of an SoC. The README is the crate's front page.

mod signal;

use std::io::{self, BufWriter, Write};
use std::path::PathBuf;

use anyhow::{Context, bail};
use clap::{Parser, Subcommand};
use rigless::machine::Machine;
use rigless::session::Session;
use rigless::sidecar::{Access, AccessKind, BackendError, SidecarOptions, StopReason, TargetStart};
use rigless::time::VirtualNanos;

const NANOS_PER_MICRO: u64 = 1_000;
const NANOS_PER_MILLI: u64 = 1_000_000;
const NANOS_PER_SECOND: u64 = 1_000_000_000;

/// What `--until` means when it is not given: 292 years of virtual time, which no run reaches.
///
/// The engine's deadline is signed on its side, and a value past this one arms a timer it reads as
/// the past, so the run executes nothing at all. `parse_virtual_time` refuses anything larger.
const NO_DEADLINE: VirtualNanos = VirtualNanos(i64::MAX as u64);
const UNITS: [(&str, u64); 4] = [
    ("ns", 1),
    ("us", NANOS_PER_MICRO),
    ("ms", NANOS_PER_MILLI),
    ("s", NANOS_PER_SECOND),
];
#[derive(Parser)]
#[command(version, about)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Turn a vendor's SVD into the register tables of an SoC.
    Svd {
        #[command(subcommand)]
        command: Svd,
    },
    /// Run a firmware image on each MCU of a machine and print every peripheral access.
    Run(Run),
}

#[derive(Subcommand)]
enum Svd {
    /// Regenerate `generated.rs` and `generated/` beside the one SVD file in an SoC's directory.
    Generate {
        /// The SoC's directory under `src/soc`, holding its one SVD file.
        soc_directory: PathBuf,
    },
}

#[derive(clap::Args)]
struct Run {
    /// The machine to run on, by name.
    #[arg(long)]
    machine: String,
    /// How much virtual time to run, with a unit: ns, us, ms, or s. Without it the run has no
    /// deadline and ends when you interrupt it, which is what a debugger session wants.
    #[arg(long, value_parser = parse_virtual_time)]
    until: Option<VirtualNanos>,
    /// Serve GDB on this TCP port, or on this MCU's port as MCU=PORT. The run starts from reset;
    /// attach whenever you like.
    #[arg(long, value_name = "PORT | MCU=PORT")]
    gdb: Vec<String>,
    /// Hold every target that serves GDB before its first instruction, until the debugger continues.
    #[arg(long, requires = "gdb")]
    wait_for_debugger: bool,
    /// The sidecar binary, instead of the machine's name on PATH.
    #[arg(long, value_name = "PATH")]
    sidecar: Option<PathBuf>,
    /// The firmware image of one MCU of a machine that has several, as MCU=PATH.
    #[arg(long, value_name = "MCU=PATH")]
    firmware: Vec<String>,
    /// The firmware image to run, for a machine with one MCU.
    image: Option<PathBuf>,
}

fn main() -> anyhow::Result<()> {
    match Cli::parse().command {
        Command::Svd {
            command: Svd::Generate { soc_directory },
        } => rigless::svd::write_module(&soc_directory)
            .with_context(|| format!("generating {}", soc_directory.display())),
        Command::Run(run) => match run_firmware(&run) {
            Err(error) if matches!(error.downcast_ref(), Some(BackendError::Cancelled)) => {
                Err(signal::die_of_recorded_signal().into())
            }
            result => result,
        },
    }
}

fn run_firmware(run: &Run) -> anyhow::Result<()> {
    let machines = rigless::machine::all();
    let Some((_name, build_machine)) = machines.iter().find(|(name, _)| *name == run.machine)
    else {
        let names: Vec<&str> = machines.iter().map(|(name, _)| *name).collect();
        bail!(
            "no machine named {}; this build has {}",
            run.machine,
            names.join(", ")
        );
    };
    let machine = build_machine().with_context(|| format!("building {}", run.machine))?;
    let images = images_of(&machine, run)?;
    let ports = debugger_ports_of(&machine, run)?;
    let labels = labels_of(&machine);
    let starts: Vec<TargetStart<'_>> = images
        .iter()
        .zip(&ports)
        .map(|(firmware, &debugger_port)| TargetStart {
            firmware,
            debugger_port,
            wait_for_debugger: run.wait_for_debugger,
        })
        .collect();
    signal::install().context("routing SIGINT and SIGTERM to the run")?;
    let options = SidecarOptions {
        binary: run.sidecar.clone(),
        cancel: &signal::CANCEL,
    };
    for (label, port) in labels.iter().zip(&ports) {
        if let Some(port) = port {
            let state = if run.wait_for_debugger {
                "waiting for a debugger"
            } else {
                "serving GDB"
            };
            writeln!(io::stderr(), "{label}sidecar {state} on :{port}")?;
        }
    }
    let mut session = Session::spawn(machine, &starts, &options)?;
    let mut output = BufWriter::new(io::stdout().lock());
    let mut write_error = None;
    // No `--until` is no deadline: the run ends when the firmware stops or a signal cancels it.
    // `--until` exists because the protocol has no stop channel, but a person at a terminal is one.
    let until = run.until.unwrap_or(NO_DEADLINE);
    let stops = session.run(until, &mut |target, access, value| {
        if write_error.is_none() {
            write_error = write_access(&mut output, &labels[target], access, value).err();
        }
    })?;
    if let Some(error) = write_error {
        return Err(error.into());
    }
    for (target, (label, stop)) in labels.iter().zip(&stops).enumerate() {
        writeln!(
            output,
            "stop {label}t={} n={} reason={}",
            stop.at.0,
            stop.instruction_count,
            stop_reason(stop.reason)
        )?;
        for (address, program_counter) in session.unmodeled(target) {
            writeln!(
                output,
                "unmodeled {label}at={address:#010x} pc={program_counter:#010x}"
            )?;
        }
        for (instance, interrupt) in session.unrouted(target) {
            writeln!(
                output,
                "unrouted {label}instance={instance} interrupt={interrupt}"
            )?;
        }
    }
    output.flush()?;
    session.shutdown()?;
    Ok(())
}

/// How each target's lines name it: a machine with one MCU names none, since there is nothing to tell apart.
fn labels_of(machine: &Machine) -> Vec<String> {
    if machine.targets.len() == 1 {
        return vec![String::new()];
    }
    machine
        .target_names()
        .map(|name| format!("target={name} "))
        .collect()
}

/// The firmware of every target, from the positional image or from one `MCU=PATH` for each.
fn images_of(machine: &Machine, run: &Run) -> anyhow::Result<Vec<PathBuf>> {
    let named = by_target(machine, &run.firmware, |value| Ok(PathBuf::from(value)))?;
    match (&run.image, machine.targets.len()) {
        (Some(image), 1) if run.firmware.is_empty() => Ok(vec![image.clone()]),
        (Some(_), 1) => bail!("give {} one firmware image, not two", machine.name),
        (Some(_), _) => bail!(
            "{} has the MCUs {}, so name each image as MCU=PATH",
            machine.name,
            names_of(machine)
        ),
        (None, _) => named
            .into_iter()
            .zip(machine.target_names())
            .map(|(image, target)| image.with_context(|| format!("{target} has no firmware image")))
            .collect(),
    }
}

/// The debugger port of every target, from a bare port or from one `MCU=PORT` for each that wants one.
fn debugger_ports_of(machine: &Machine, run: &Run) -> anyhow::Result<Vec<Option<u16>>> {
    if let [only] = &run.gdb[..]
        && machine.targets.len() == 1
        && !only.contains('=')
    {
        let port = only.parse().context("the debugger port is not a port")?;
        return Ok(vec![Some(port)]);
    }
    by_target(machine, &run.gdb, |value| {
        value.parse().context("the debugger port is not a port")
    })
}

/// One value per target, each written `MCU=VALUE`, in the machine's order.
fn by_target<T>(
    machine: &Machine,
    values: &[String],
    parse: impl Fn(&str) -> anyhow::Result<T>,
) -> anyhow::Result<Vec<Option<T>>> {
    let mut by_index: Vec<Option<T>> = machine.targets.iter().map(|_| None).collect();
    for value in values {
        let (name, rest) = value.split_once('=').with_context(|| {
            format!(
                "{value} names no MCU; {} has {}",
                machine.name,
                names_of(machine)
            )
        })?;
        let target = machine.target(name).with_context(|| {
            format!(
                "{} has no MCU named {name}; it has {}",
                machine.name,
                names_of(machine)
            )
        })?;
        if by_index[target].is_some() {
            bail!("{name} is named twice");
        }
        by_index[target] = Some(parse(rest)?);
    }
    Ok(by_index)
}

fn names_of(machine: &Machine) -> String {
    machine.target_names().collect::<Vec<_>>().join(", ")
}

fn write_access(
    output: &mut impl Write,
    label: &str,
    access: &Access,
    value: u64,
) -> io::Result<()> {
    let kind = match access.kind {
        AccessKind::Read => "read ",
        AccessKind::Write => "write",
    };
    writeln!(
        output,
        "{kind} {label}t={} n={} pc={:#010x} at={:#010x} w={} v={value:#010x}",
        access.virtual_time.0,
        access.instruction_count,
        access.program_counter,
        access.address,
        access.width.bits(),
    )
}

fn stop_reason(reason: StopReason) -> &'static str {
    match reason {
        StopReason::Deadline => "deadline",
        StopReason::Idle => "idle",
    }
}

fn parse_virtual_time(text: &str) -> Result<VirtualNanos, String> {
    let (digits, scale) = UNITS
        .iter()
        .find_map(|(unit, scale)| text.strip_suffix(unit).map(|digits| (digits, *scale)))
        .ok_or_else(|| format!("{text} has no unit; use ns, us, ms, or s"))?;
    let count: u64 = digits
        .parse()
        .map_err(|error| format!("{digits} is not a whole number: {error}"))?;
    let nanos = count
        .checked_mul(scale)
        .ok_or_else(|| format!("{text} does not fit the virtual clock"))?;
    // Past this the engine reads its deadline as the past and runs nothing, silently.
    if nanos > NO_DEADLINE.0 {
        return Err(format!(
            "{text} is longer than the virtual clock runs; the most is {} ns",
            NO_DEADLINE.0
        ));
    }
    Ok(VirtualNanos(nanos))
}

#[cfg(test)]
mod tests {
    use super::*;

    use rigless::machine::Target;
    use rigless::sidecar::{InterruptLine, SidecarCommand};
    use rigless::soc::{Cpu, Region, Soc};

    const AN_IMAGE: &str = "firmware.elf";

    static MEMORY: [Region; 1] = [Region::ram("sram", 0x2000_0000, 0x1000)];

    /// A target with nothing on it: these tests read only the names and how many there are, and
    /// building one here rather than borrowing a real machine keeps them working in a build that
    /// carries no SoC.
    fn target(name: &'static str) -> Target {
        Target {
            name,
            soc: Soc::builder(
                Cpu {
                    name: "a-core",
                    fpu: false,
                    interrupt_lines: 1,
                },
                &MEMORY,
                [],
            )
            .build()
            .unwrap(),
            sidecar: SidecarCommand {
                binary: "a-sidecar",
                machine: "a-machine",
                cpu: "a-core",
                properties: Vec::new(),
                icount_shift: 0,
                aliases: Vec::new(),
            },
            interrupt_line: InterruptLine::new,
        }
    }

    fn one_mcu() -> Machine {
        Machine {
            name: "one",
            targets: vec![target("only")],
        }
    }

    fn two_mcus() -> Machine {
        Machine {
            name: "two",
            targets: vec![target("first"), target("second")],
        }
    }

    fn run_of(image: Option<&str>, firmware: &[&str], gdb: &[&str]) -> Run {
        Run {
            machine: "test".to_owned(),
            until: Some(VirtualNanos(1)),
            wait_for_debugger: false,
            gdb: gdb.iter().map(|value| (*value).to_owned()).collect(),
            sidecar: None,
            firmware: firmware.iter().map(|value| (*value).to_owned()).collect(),
            image: image.map(PathBuf::from),
        }
    }

    #[test]
    fn a_machine_with_one_mcu_takes_the_image_on_its_own() {
        let images = images_of(&one_mcu(), &run_of(Some(AN_IMAGE), &[], &[])).unwrap();
        assert_eq!(images, [PathBuf::from(AN_IMAGE)]);
        let ports = debugger_ports_of(&one_mcu(), &run_of(Some(AN_IMAGE), &[], &["3333"])).unwrap();
        assert_eq!(ports, [Some(3333)]);
    }

    #[test]
    fn a_machine_with_several_mcus_takes_one_image_each_by_name() {
        let run = run_of(None, &["second=two.elf", "first=one.elf"], &["second=3333"]);
        assert_eq!(
            images_of(&two_mcus(), &run).unwrap(),
            [PathBuf::from("one.elf"), PathBuf::from("two.elf")],
            "the images come back in the machine's order, not the command line's"
        );
        assert_eq!(
            debugger_ports_of(&two_mcus(), &run).unwrap(),
            [None, Some(3333)]
        );
    }

    #[test]
    fn an_image_that_names_no_mcu_of_the_machine_is_refused() {
        let named = |values: &[&str]| {
            images_of(&two_mcus(), &run_of(None, values, &[]))
                .unwrap_err()
                .to_string()
        };
        assert!(
            named(&["one.elf"]).contains("names no MCU"),
            "a bare image names none of several"
        );
        assert!(named(&["third=one.elf"]).contains("no MCU named third"));
        assert!(named(&["first=one.elf"]).contains("second has no firmware image"));
        assert!(named(&["first=one.elf", "first=two.elf"]).contains("first is named twice"));
        assert!(
            images_of(&two_mcus(), &run_of(Some(AN_IMAGE), &[], &[]))
                .unwrap_err()
                .to_string()
                .contains("name each image as MCU=PATH"),
            "the positional image belongs to a machine with one MCU"
        );
        let named_as_well = format!("{}=other.elf", one_mcu().targets[0].name);
        assert!(
            images_of(&one_mcu(), &run_of(Some(AN_IMAGE), &[&named_as_well], &[]))
                .unwrap_err()
                .to_string()
                .contains("one firmware image, not two")
        );
    }

    #[test]
    fn a_duration_parses_with_each_unit_and_refuses_the_rest() {
        assert_eq!(parse_virtual_time("48ns"), Ok(VirtualNanos(48)));
        assert_eq!(parse_virtual_time("7us"), Ok(VirtualNanos(7_000)));
        assert_eq!(parse_virtual_time("2ms"), Ok(VirtualNanos(2_000_000)));
        assert_eq!(parse_virtual_time("1s"), Ok(VirtualNanos(NANOS_PER_SECOND)));
        assert!(parse_virtual_time("10").is_err());
        assert!(parse_virtual_time("1.5ms").is_err());
        assert!(parse_virtual_time("99999999999999999999s").is_err());
        assert_eq!(
            parse_virtual_time(&format!("{}ns", NO_DEADLINE.0)),
            Ok(NO_DEADLINE)
        );
        assert!(
            parse_virtual_time(&format!("{}ns", NO_DEADLINE.0 + 1)).is_err(),
            "a deadline the engine reads as the past runs nothing, so it is refused here"
        );
    }

    #[test]
    fn a_run_without_until_has_no_deadline_and_serves_gdb_without_holding_it() {
        let run = Cli::parse_from(["rigless", "run", "--machine", "pixracer", AN_IMAGE]);
        let Command::Run(run) = run.command else {
            panic!("the run command")
        };
        assert_eq!(run.until, None, "--until has no default");
        assert!(!run.wait_for_debugger);
        let held = Cli::parse_from([
            "rigless",
            "run",
            "--machine",
            "pixracer",
            "--gdb",
            "3333",
            "--wait-for-debugger",
            AN_IMAGE,
        ]);
        let Command::Run(held) = held.command else {
            panic!("the run command")
        };
        assert!(held.wait_for_debugger);
        assert!(
            Cli::try_parse_from([
                "rigless",
                "run",
                "--machine",
                "pixracer",
                "--wait-for-debugger",
                AN_IMAGE
            ])
            .is_err(),
            "waiting needs a port to wait on"
        );
    }
}