rvsim-core 2.0.0

A cycle-level RISC-V 64-bit system simulator.
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
//! Device Tree Blob (DTB) generation.
//!
//! Generates a Flattened Device Tree (FDT) binary matching the system-on-chip layout.
//! This allows the simulator to provide a DTB to OpenSBI/Linux without
//! requiring an external `dtc` compilation step.

use crate::config::Config;

// FDT constants
const FDT_MAGIC: u32 = 0xd00dfeed;
const FDT_VERSION: u32 = 17;
const FDT_LAST_COMP_VERSION: u32 = 16;
const FDT_BEGIN_NODE: u32 = 1;
const FDT_END_NODE: u32 = 2;
const FDT_PROP: u32 = 3;
const FDT_END: u32 = 9;

/// Builder for constructing FDT binary blobs.
struct FdtBuilder {
    struct_buf: Vec<u8>,
    strings_buf: Vec<u8>,
    /// Map of string -> offset in `strings_buf` for dedup.
    string_offsets: Vec<(String, u32)>,
}

impl FdtBuilder {
    const fn new() -> Self {
        Self { struct_buf: Vec::new(), strings_buf: Vec::new(), string_offsets: Vec::new() }
    }

    fn push_u32(&mut self, val: u32) {
        self.struct_buf.extend_from_slice(&val.to_be_bytes());
    }

    fn begin_node(&mut self, name: &str) {
        self.push_u32(FDT_BEGIN_NODE);
        self.struct_buf.extend_from_slice(name.as_bytes());
        self.struct_buf.push(0); // null terminator
        // Align to 4 bytes
        while !self.struct_buf.len().is_multiple_of(4) {
            self.struct_buf.push(0);
        }
    }

    fn end_node(&mut self) {
        self.push_u32(FDT_END_NODE);
    }

    fn string_offset(&mut self, name: &str) -> u32 {
        for (s, off) in &self.string_offsets {
            if s == name {
                return *off;
            }
        }
        let off = self.strings_buf.len() as u32;
        self.strings_buf.extend_from_slice(name.as_bytes());
        self.strings_buf.push(0);
        self.string_offsets.push((name.to_string(), off));
        off
    }

    fn prop_u32(&mut self, name: &str, val: u32) {
        let name_off = self.string_offset(name);
        self.push_u32(FDT_PROP);
        self.push_u32(4); // length
        self.push_u32(name_off);
        self.push_u32(val);
    }

    fn prop_string(&mut self, name: &str, val: &str) {
        let name_off = self.string_offset(name);
        let data = val.as_bytes();
        let len = data.len() + 1; // include null terminator
        self.push_u32(FDT_PROP);
        self.push_u32(len as u32);
        self.push_u32(name_off);
        self.struct_buf.extend_from_slice(data);
        self.struct_buf.push(0);
        while !self.struct_buf.len().is_multiple_of(4) {
            self.struct_buf.push(0);
        }
    }

    fn prop_bytes(&mut self, name: &str, data: &[u8]) {
        let name_off = self.string_offset(name);
        self.push_u32(FDT_PROP);
        self.push_u32(data.len() as u32);
        self.push_u32(name_off);
        self.struct_buf.extend_from_slice(data);
        while !self.struct_buf.len().is_multiple_of(4) {
            self.struct_buf.push(0);
        }
    }

    fn prop_empty(&mut self, name: &str) {
        let name_off = self.string_offset(name);
        self.push_u32(FDT_PROP);
        self.push_u32(0);
        self.push_u32(name_off);
    }

    /// Encode a reg property with pairs of (`addr_hi`, `addr_lo`, `size_hi`, `size_lo`)
    /// for #address-cells=2, #size-cells=2.
    fn prop_reg_2_2(&mut self, addr: u64, size: u64) {
        let mut data = Vec::with_capacity(16);
        data.extend_from_slice(&((addr >> 32) as u32).to_be_bytes());
        data.extend_from_slice(&(addr as u32).to_be_bytes());
        data.extend_from_slice(&((size >> 32) as u32).to_be_bytes());
        data.extend_from_slice(&(size as u32).to_be_bytes());
        self.prop_bytes("reg", &data);
    }

    /// Encode a reg property for #address-cells=1, #size-cells=0.
    fn prop_reg_1_0(&mut self, val: u32) {
        self.prop_bytes("reg", &val.to_be_bytes());
    }

    fn finalize(mut self) -> Vec<u8> {
        // End of structure block
        self.push_u32(FDT_END);

        let struct_size = self.struct_buf.len() as u32;
        let strings_size = self.strings_buf.len() as u32;

        // Header is 40 bytes
        let header_size = 40u32;
        // Memory reservation block: one empty entry (16 bytes of zeros)
        let memrsv_size = 16u32;

        let dt_struct_offset = header_size + memrsv_size;
        let dt_strings_offset = dt_struct_offset + struct_size;
        let total_size = dt_strings_offset + strings_size;

        let mut out = Vec::with_capacity(total_size as usize);

        // Header
        out.extend_from_slice(&FDT_MAGIC.to_be_bytes());
        out.extend_from_slice(&total_size.to_be_bytes());
        out.extend_from_slice(&dt_struct_offset.to_be_bytes());
        out.extend_from_slice(&dt_strings_offset.to_be_bytes());
        out.extend_from_slice(&(header_size).to_be_bytes()); // off_mem_rsvmap
        out.extend_from_slice(&FDT_VERSION.to_be_bytes());
        out.extend_from_slice(&FDT_LAST_COMP_VERSION.to_be_bytes());
        out.extend_from_slice(&0u32.to_be_bytes()); // boot_cpuid_phys
        out.extend_from_slice(&strings_size.to_be_bytes());
        out.extend_from_slice(&struct_size.to_be_bytes());

        // Memory reservation block (empty)
        out.extend_from_slice(&[0u8; 16]);

        // Structure block
        out.extend_from_slice(&self.struct_buf);

        // Strings block
        out.extend_from_slice(&self.strings_buf);

        out
    }
}

/// Phandles for one system: interrupt controllers first so a single-hart
/// tree keeps the values it always had (cpu0 intc = 1, PLIC = 2, syscon = 3).
struct Phandles {
    hart_count: u32,
}

impl Phandles {
    const fn cpu_intc(hart: u32) -> u32 {
        1 + hart
    }

    const fn plic(&self) -> u32 {
        self.hart_count + 1
    }

    const fn syscon(&self) -> u32 {
        self.hart_count + 2
    }

    const fn cpu(&self, hart: u32) -> u32 {
        self.hart_count + 3 + hart
    }
}

/// The ISA string the device tree advertises: `misa`'s letters and the
/// multi-letter extensions the hart implements.
fn isa_string(config: &Config) -> String {
    let misa = config.misa();
    let mut isa = misa.isa_string();
    // Every hart with S-mode implements Sstc's stimecmp.
    if misa.has_s() {
        isa.push_str("_sstc");
    }
    if config.isa.svadu {
        isa.push_str("_svadu");
    }
    isa
}

/// Generates a DTB binary matching the simulator's `SoC` layout.
///
/// The generated DTB includes:
/// - Memory region at `ram_base` with `ram_size`
/// - CLINT at `clint_base` with timer and software interrupts for every hart
/// - PLIC at 0x0c000000 with M- and S-mode contexts for every hart
/// - UART at `uart_base`
/// - virtio block device at `disk_base`
/// - One `cpu@N` per hart (the ISA string of its `misa`, SV39 MMU) and a `cpu-map`
pub fn generate_dtb(config: &Config) -> Vec<u8> {
    let ram_base = config.system.ram_base;
    let ram_size = config.memory.ram_size as u64;
    let uart_base = config.system.uart_base;
    let disk_base = config.system.disk_base;
    let clint_base = config.system.clint_base;
    let syscon_base = config.system.syscon_base;
    let rtc_base: u64 = 0x10_1000;
    let plic_base: u64 = 0x0c00_0000;
    let timebase_freq: u32 = 10_000_000;
    let hart_count = u32::try_from(config.system.hart_count.max(1)).unwrap_or(u32::MAX);

    let bootargs =
        format!("root=/dev/vda rw console=ttyS0 earlycon=uart8250,mmio,{uart_base:#x} rootwait");
    let stdout_path = format!("/soc/uart@{uart_base:x}");

    let phandles = Phandles { hart_count };
    let plic_phandle = phandles.plic();

    let mut b = FdtBuilder::new();

    // Root node
    b.begin_node("");
    b.prop_u32("#address-cells", 2);
    b.prop_u32("#size-cells", 2);
    b.prop_string("compatible", "riscv-virtio");
    b.prop_string("model", "riscv-virtio,qemu");

    // /chosen
    b.begin_node("chosen");
    b.prop_string("bootargs", &bootargs);
    b.prop_string("stdout-path", &stdout_path);
    b.end_node();

    // /cpus
    b.begin_node("cpus");
    b.prop_u32("#address-cells", 1);
    b.prop_u32("#size-cells", 0);
    b.prop_u32("timebase-frequency", timebase_freq);

    for hart in 0..hart_count {
        b.begin_node(&format!("cpu@{hart}"));
        b.prop_string("device_type", "cpu");
        b.prop_reg_1_0(hart);
        b.prop_string("status", "okay");
        b.prop_string("compatible", "riscv");
        b.prop_string("riscv,isa", &isa_string(config));
        b.prop_string("mmu-type", "riscv,sv39");
        b.prop_u32("phandle", phandles.cpu(hart));

        b.begin_node("interrupt-controller");
        b.prop_u32("#interrupt-cells", 1);
        b.prop_empty("interrupt-controller");
        b.prop_string("compatible", "riscv,cpu-intc");
        b.prop_u32("phandle", Phandles::cpu_intc(hart));
        b.end_node(); // interrupt-controller

        b.end_node(); // cpu@N
    }

    // /cpus/cpu-map: one cluster, one core per hart.
    b.begin_node("cpu-map");
    b.begin_node("cluster0");
    for hart in 0..hart_count {
        b.begin_node(&format!("core{hart}"));
        b.prop_u32("cpu", phandles.cpu(hart));
        b.end_node();
    }
    b.end_node(); // cluster0
    b.end_node(); // cpu-map

    b.end_node(); // cpus

    // /memory
    {
        let node_name = format!("memory@{ram_base:x}");
        b.begin_node(&node_name);
        b.prop_string("device_type", "memory");
        b.prop_reg_2_2(ram_base, ram_size);
        b.end_node();
    }

    // /soc
    b.begin_node("soc");
    b.prop_u32("#address-cells", 2);
    b.prop_u32("#size-cells", 2);
    b.prop_string("compatible", "simple-bus");
    b.prop_empty("ranges");

    // /soc/clint
    {
        let node_name = format!("clint@{clint_base:x}");
        b.begin_node(&node_name);
        b.prop_string("compatible", "riscv,clint0");
        b.prop_reg_2_2(clint_base, 0x10000);
        // Per hart: <&intc 3> (M-mode software), <&intc 7> (M-mode timer).
        b.prop_bytes("interrupts-extended", &interrupts_extended(&phandles, &[3, 7]));
        b.end_node();
    }

    // /soc/uart
    {
        let node_name = format!("uart@{uart_base:x}");
        b.begin_node(&node_name);
        b.prop_string("compatible", "ns16550a");
        b.prop_reg_2_2(uart_base, 0x100);
        b.prop_u32("clock-frequency", 10_000_000);
        b.prop_u32("interrupt-parent", plic_phandle);
        b.prop_bytes("interrupts", &10u32.to_be_bytes());
        b.prop_string("status", "okay");
        b.end_node();
    }

    // /soc/virtio_mmio
    {
        let node_name = format!("virtio_mmio@{disk_base:x}");
        b.begin_node(&node_name);
        b.prop_string("compatible", "virtio,mmio");
        b.prop_reg_2_2(disk_base, 0x1000);
        b.prop_u32("interrupt-parent", plic_phandle);
        b.prop_bytes("interrupts", &1u32.to_be_bytes());
        b.end_node();
    }

    // /soc/plic
    {
        let node_name = format!("interrupt-controller@{plic_base:x}");
        b.begin_node(&node_name);
        b.prop_string("compatible", "riscv,plic0");
        b.prop_reg_2_2(plic_base, 0x4000000);
        b.prop_u32("#interrupt-cells", 1);
        b.prop_empty("interrupt-controller");
        // Per hart: <&intc 11> (M-mode external), <&intc 9> (S-mode external).
        b.prop_bytes("interrupts-extended", &interrupts_extended(&phandles, &[11, 9]));
        b.prop_u32("riscv,ndev", 0x35);
        b.prop_u32("phandle", plic_phandle);
        b.end_node();
    }

    // /soc/syscon (with poweroff and reboot sub-nodes)
    let syscon_phandle = phandles.syscon();
    {
        let node_name = format!("syscon@{syscon_base:x}");
        b.begin_node(&node_name);
        b.prop_string("compatible", "syscon");
        b.prop_reg_2_2(syscon_base, 0x1000);
        b.prop_u32("phandle", syscon_phandle);
        b.end_node();
    }

    // /soc/poweroff — writes 0x5555 to syscon offset 0x00
    {
        b.begin_node("poweroff");
        b.prop_string("compatible", "syscon-poweroff");
        b.prop_u32("regmap", syscon_phandle);
        b.prop_u32("offset", 0x00);
        b.prop_u32("value", 0x5555);
        b.end_node();
    }

    // /soc/reboot — writes 0x7777 to syscon offset 0x00
    {
        b.begin_node("reboot");
        b.prop_string("compatible", "syscon-reboot");
        b.prop_u32("regmap", syscon_phandle);
        b.prop_u32("offset", 0x00);
        b.prop_u32("value", 0x7777);
        b.end_node();
    }

    // /soc/rtc (Goldfish RTC)
    {
        let node_name = format!("rtc@{rtc_base:x}");
        b.begin_node(&node_name);
        b.prop_string("compatible", "google,goldfish-rtc");
        b.prop_reg_2_2(rtc_base, 0x1000);
        b.prop_u32("interrupt-parent", plic_phandle);
        b.prop_bytes("interrupts", &11u32.to_be_bytes());
        b.end_node();
    }

    b.end_node(); // soc
    b.end_node(); // root

    b.finalize()
}

/// `interrupts-extended` cells naming every hart's interrupt controller once
/// per entry in `irqs`: `<&intc0 irqs[0]> <&intc0 irqs[1]> <&intc1 irqs[0]> ...`.
fn interrupts_extended(phandles: &Phandles, irqs: &[u32]) -> Vec<u8> {
    let mut cells = Vec::with_capacity(phandles.hart_count as usize * irqs.len() * 8);
    for hart in 0..phandles.hart_count {
        for irq in irqs {
            cells.extend_from_slice(&Phandles::cpu_intc(hart).to_be_bytes());
            cells.extend_from_slice(&irq.to_be_bytes());
        }
    }
    cells
}