rustsbi 0.3.0-rc.2

Minimal RISC-V's SBI implementation library in Rust
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use crate::{
    spec::binary::SbiRet, Fence, HartMask, Hsm, Ipi, Pmu, Reset, Timer, IMPL_ID_RUSTSBI,
    RUSTSBI_VERSION, SBI_SPEC_MAJOR, SBI_SPEC_MINOR,
};
use core::convert::Infallible;
#[cfg(feature = "machine")]
use riscv::register::{marchid, mimpid, mvendorid};

/// RustSBI instance including standard extensions
///
/// By now RustSBI supports to run instance based interface on systems has environment pointer width
/// that is the same as supervisor pointer width.
#[derive(Clone, Debug)]
pub struct RustSBI<T, I, R, H, S, P> {
    timer: Option<T>,
    ipi: Option<I>,
    rfnc: Option<R>,
    hsm: Option<H>,
    srst: Option<S>,
    pmu: Option<P>,
    #[cfg(not(feature = "machine"))]
    info: MachineInfo,
}

/// Machine information for SBI environment
///
/// This structure is useful to build an SBI environment when RustSBI is not run directly on RISC-V machine mode.
#[cfg(not(feature = "machine"))]
#[derive(Clone, Copy, Debug)]
pub struct MachineInfo {
    /// Register `mvendorid` for supervisor environment
    pub mvendorid: usize,
    /// Register `marchid` for supervisor environment
    pub marchid: usize,
    /// Register `mimpid` for supervisor environment
    pub mimpid: usize,
}

impl<T: Timer, I: Ipi, R: Fence, H: Hsm, S: Reset, P: Pmu> RustSBI<T, I, R, H, S, P> {
    /// Create RustSBI instance on current machine environment for all the SBI extensions
    #[cfg(feature = "machine")]
    #[inline]
    pub const fn new_machine(timer: T, ipi: I, rfnc: R, hsm: H, srst: S, pmu: P) -> Self {
        Self {
            timer: Some(timer),
            ipi: Some(ipi),
            rfnc: Some(rfnc),
            hsm: Some(hsm),
            srst: Some(srst),
            pmu: Some(pmu),
        }
    }

    /// Create RustSBI instance on given machine information for all the SBI extensions
    #[cfg(not(feature = "machine"))]
    #[inline]
    pub const fn with_machine_info(
        timer: T,
        ipi: I,
        rfnc: R,
        hsm: H,
        srst: S,
        pmu: P,
        info: MachineInfo,
    ) -> Self {
        Self {
            timer: Some(timer),
            ipi: Some(ipi),
            rfnc: Some(rfnc),
            hsm: Some(hsm),
            srst: Some(srst),
            pmu: Some(pmu),
            info,
        }
    }

    /// Handle supervisor environment call with given parameters and return the `SbiRet` result.
    #[inline]
    pub fn handle_ecall(&mut self, extension: usize, function: usize, param: [usize; 6]) -> SbiRet {
        match extension {
            spec::rfnc::EID_RFNC => {
                let rfnc = if let Some(rfnc) = &mut self.rfnc {
                    rfnc
                } else {
                    return SbiRet::not_supported();
                };
                let [param0, param1, param2, param3, param4] =
                    [param[0], param[1], param[2], param[3], param[4]];
                let hart_mask = crate::HartMask::from_mask_base(param0, param1);
                match function {
                    spec::rfnc::REMOTE_FENCE_I => rfnc.remote_fence_i(hart_mask),
                    spec::rfnc::REMOTE_SFENCE_VMA => {
                        rfnc.remote_sfence_vma(hart_mask, param2, param3)
                    }
                    spec::rfnc::REMOTE_SFENCE_VMA_ASID => {
                        rfnc.remote_sfence_vma_asid(hart_mask, param2, param3, param4)
                    }
                    spec::rfnc::REMOTE_HFENCE_GVMA_VMID => {
                        rfnc.remote_hfence_gvma_vmid(hart_mask, param2, param3, param4)
                    }
                    spec::rfnc::REMOTE_HFENCE_GVMA => {
                        rfnc.remote_hfence_gvma(hart_mask, param2, param3)
                    }
                    spec::rfnc::REMOTE_HFENCE_VVMA_ASID => {
                        rfnc.remote_hfence_vvma_asid(hart_mask, param2, param3, param4)
                    }
                    spec::rfnc::REMOTE_HFENCE_VVMA => {
                        rfnc.remote_hfence_vvma(hart_mask, param2, param3)
                    }
                    _ => SbiRet::not_supported(),
                }
            }
            spec::time::EID_TIME => match () {
                #[cfg(target_pointer_width = "64")]
                () => {
                    let timer = if let Some(timer) = &mut self.timer {
                        timer
                    } else {
                        return SbiRet::not_supported();
                    };
                    let [param0] = [param[0]];
                    match function {
                        spec::time::SET_TIMER => {
                            timer.set_timer(param0 as _);
                            SbiRet::success(0)
                        }
                        _ => SbiRet::not_supported(),
                    }
                }
                #[cfg(target_pointer_width = "32")]
                () => {
                    let timer = if let Some(timer) = &mut self.timer {
                        timer
                    } else {
                        return SbiRet::not_supported();
                    };
                    let [param0, param1] = [param[0], param[1]];
                    match function {
                        spec::time::SET_TIMER => {
                            timer.set_timer(concat_u32(param1, param0));
                            SbiRet::success(0)
                        }
                        _ => SbiRet::not_supported(),
                    }
                }
            },
            spec::spi::EID_SPI => {
                let ipi = if let Some(ipi) = &mut self.ipi {
                    ipi
                } else {
                    return SbiRet::not_supported();
                };
                let [param0, param1] = [param[0], param[1]];
                match function {
                    spec::spi::SEND_IPI => ipi.send_ipi(HartMask::from_mask_base(param0, param1)),
                    _ => SbiRet::not_supported(),
                }
            }
            spec::base::EID_BASE => {
                let [param0] = [param[0]];
                let value = match function {
                    spec::base::GET_SBI_SPEC_VERSION => (SBI_SPEC_MAJOR << 24) | (SBI_SPEC_MINOR),
                    spec::base::GET_SBI_IMPL_ID => IMPL_ID_RUSTSBI,
                    spec::base::GET_SBI_IMPL_VERSION => RUSTSBI_VERSION,
                    spec::base::PROBE_EXTENSION => {
                        // only provides probes to standard extensions. If you have customized extensions to be probed,
                        // run it even before this `handle_ecall` function.
                        self.probe_extension(param0)
                    }
                    spec::base::GET_MVENDORID => match () {
                        #[cfg(feature = "machine")]
                        () => mvendorid::read().map(|r| r.bits()).unwrap_or(0),
                        #[cfg(not(feature = "machine"))]
                        () => self.info.mvendorid,
                    },
                    spec::base::GET_MARCHID => match () {
                        #[cfg(feature = "machine")]
                        () => marchid::read().map(|r| r.bits()).unwrap_or(0),
                        #[cfg(not(feature = "machine"))]
                        () => self.info.marchid,
                    },
                    spec::base::GET_MIMPID => match () {
                        #[cfg(feature = "machine")]
                        () => mimpid::read().map(|r| r.bits()).unwrap_or(0),
                        #[cfg(not(feature = "machine"))]
                        () => self.info.mimpid,
                    },
                    _ => return SbiRet::not_supported(),
                };
                SbiRet::success(value)
            }
            spec::hsm::EID_HSM => {
                let hsm = if let Some(hsm) = &mut self.hsm {
                    hsm
                } else {
                    return SbiRet::not_supported();
                };
                let [param0, param1, param2] = [param[0], param[1], param[2]];
                match function {
                    spec::hsm::HART_START => hsm.hart_start(param0, param1, param2),
                    spec::hsm::HART_STOP => hsm.hart_stop(),
                    spec::hsm::HART_GET_STATUS => hsm.hart_get_status(param0),
                    spec::hsm::HART_SUSPEND => {
                        if let Ok(suspend_type) = u32::try_from(param0) {
                            hsm.hart_suspend(suspend_type, param1, param2)
                        } else {
                            SbiRet::invalid_param()
                        }
                    }
                    _ => SbiRet::not_supported(),
                }
            }
            spec::srst::EID_SRST => {
                let srst = if let Some(srst) = &mut self.srst {
                    srst
                } else {
                    return SbiRet::not_supported();
                };
                let [param0, param1] = [param[0], param[1]];
                match function {
                    spec::srst::SYSTEM_RESET => {
                        match (u32::try_from(param0), u32::try_from(param1)) {
                            (Ok(reset_type), Ok(reset_reason)) => {
                                srst.system_reset(reset_type, reset_reason)
                            }
                            (_, _) => SbiRet::invalid_param(),
                        }
                    }
                    _ => SbiRet::not_supported(),
                }
            }
            spec::pmu::EID_PMU => match () {
                #[cfg(target_pointer_width = "64")]
                () => {
                    let pmu = if let Some(pmu) = &mut self.pmu {
                        pmu
                    } else {
                        return SbiRet::not_supported();
                    };
                    let [param0, param1, param2, param3, param4] =
                        [param[0], param[1], param[2], param[3], param[4]];
                    match function {
                        spec::pmu::PMU_NUM_COUNTERS => SbiRet::success(pmu.num_counters()),
                        spec::pmu::PMU_COUNTER_GET_INFO => pmu.counter_get_info(param0),
                        spec::pmu::PMU_COUNTER_CONFIG_MATCHING => {
                            pmu.counter_config_matching(param0, param1, param2, param3, param4 as _)
                        }
                        spec::pmu::PMU_COUNTER_START => {
                            pmu.counter_start(param0, param1, param2, param3 as _)
                        }
                        spec::pmu::PMU_COUNTER_STOP => pmu.counter_stop(param0, param1, param2),
                        spec::pmu::PMU_COUNTER_FW_READ => pmu.counter_fw_read(param0),
                        _ => SbiRet::not_supported(),
                    }
                }
                #[cfg(target_pointer_width = "32")]
                () => {
                    let pmu = if let Some(pmu) = &mut self.pmu {
                        pmu
                    } else {
                        return SbiRet::not_supported();
                    };
                    let [param0, param1, param2, param3, param4, param5] =
                        [param[0], param[1], param[2], param[3], param[4], param[5]];
                    match function {
                        spec::pmu::PMU_NUM_COUNTERS => SbiRet::success(pmu.num_counters()),
                        spec::pmu::PMU_COUNTER_GET_INFO => pmu.counter_get_info(param0),
                        spec::pmu::PMU_COUNTER_CONFIG_MATCHING => pmu.counter_config_matching(
                            param0,
                            param1,
                            param2,
                            param3,
                            concat_u32(param5, param4),
                        ),
                        spec::pmu::PMU_COUNTER_START => {
                            pmu.counter_start(param0, param1, param2, concat_u32(param4, param3))
                        }
                        spec::pmu::PMU_COUNTER_STOP => pmu.counter_stop(param0, param1, param2),
                        spec::pmu::PMU_COUNTER_FW_READ => pmu.counter_fw_read(param0),
                        _ => SbiRet::not_supported(),
                    }
                }
            },
            _ => SbiRet::not_supported(),
        }
    }

    #[inline]
    fn probe_extension(&self, extension: usize) -> usize {
        let ans = match extension {
            spec::base::EID_BASE => true,
            spec::time::EID_TIME => self.timer.is_some(),
            spec::spi::EID_SPI => self.ipi.is_some(),
            spec::rfnc::EID_RFNC => self.rfnc.is_some(),
            spec::srst::EID_SRST => self.srst.is_some(),
            spec::hsm::EID_HSM => self.hsm.is_some(),
            spec::pmu::EID_PMU => self.pmu.is_some(),
            _ => false,
        };
        if ans {
            spec::base::UNAVAILABLE_EXTENSION.wrapping_add(1)
        } else {
            spec::base::UNAVAILABLE_EXTENSION
        }
    }
}

#[cfg(target_pointer_width = "32")]
#[inline]
const fn concat_u32(h: usize, l: usize) -> u64 {
    ((h as u64) << 32) | (l as u64)
}

/// Structure to build a RustSBI instance
pub struct Builder<T, I, R, H, S, P> {
    inner: RustSBI<T, I, R, H, S, P>,
}

impl Builder<Infallible, Infallible, Infallible, Infallible, Infallible, Infallible> {
    /// Create a new `Builder` from current machine environment
    #[inline]
    #[cfg(feature = "machine")]
    pub const fn new_machine(
    ) -> Builder<Infallible, Infallible, Infallible, Infallible, Infallible, Infallible> {
        Builder {
            inner: RustSBI {
                timer: None,
                ipi: None,
                rfnc: None,
                hsm: None,
                srst: None,
                pmu: None,
            },
        }
    }

    /// Create a new `Builder` from machine information
    #[inline]
    #[cfg(not(feature = "machine"))]
    pub const fn with_machine_info(
        info: MachineInfo,
    ) -> Builder<Infallible, Infallible, Infallible, Infallible, Infallible, Infallible> {
        Builder {
            inner: RustSBI {
                timer: None,
                ipi: None,
                rfnc: None,
                hsm: None,
                srst: None,
                pmu: None,
                info,
            },
        }
    }
}

// fixme: in future releases we may use type-changing struct update syntax like:
// Builder { inner: RustSBI { timer: None, ..self.inner } }
// https://github.com/rust-lang/rust/issues/86555

// fixme: struct `Infallible` should be replaced to never type once it's stablized

impl<T, I, R, H, S, P> Builder<T, I, R, H, S, P> {
    /// Add Timer programmer extension to RustSBI
    #[inline]
    pub fn with_timer<T2: Timer>(self, timer: T2) -> Builder<T2, I, R, H, S, P> {
        Builder {
            inner: RustSBI {
                timer: Some(timer),
                ipi: self.inner.ipi,
                rfnc: self.inner.rfnc,
                hsm: self.inner.hsm,
                srst: self.inner.srst,
                pmu: self.inner.pmu,
                #[cfg(not(feature = "machine"))]
                info: self.inner.info,
            },
        }
    }

    /// Add Inter-processor Interrupt extension to RustSBI
    #[inline]
    pub fn with_ipi<I2: Ipi>(self, ipi: I2) -> Builder<T, I2, R, H, S, P> {
        Builder {
            inner: RustSBI {
                timer: self.inner.timer,
                ipi: Some(ipi),
                rfnc: self.inner.rfnc,
                hsm: self.inner.hsm,
                srst: self.inner.srst,
                pmu: self.inner.pmu,
                #[cfg(not(feature = "machine"))]
                info: self.inner.info,
            },
        }
    }

    /// Add Remote Fence extension to RustSBI
    #[inline]
    pub fn with_fence<R2: Fence>(self, fence: R2) -> Builder<T, I, R2, H, S, P> {
        Builder {
            inner: RustSBI {
                timer: self.inner.timer,
                ipi: self.inner.ipi,
                rfnc: Some(fence),
                hsm: self.inner.hsm,
                srst: self.inner.srst,
                pmu: self.inner.pmu,
                #[cfg(not(feature = "machine"))]
                info: self.inner.info,
            },
        }
    }

    /// Add Hart State Monitor extension to RustSBI
    #[inline]
    pub fn with_hsm<H2: Hsm>(self, hsm: H2) -> Builder<T, I, R, H2, S, P> {
        Builder {
            inner: RustSBI {
                timer: self.inner.timer,
                ipi: self.inner.ipi,
                rfnc: self.inner.rfnc,
                hsm: Some(hsm),
                srst: self.inner.srst,
                pmu: self.inner.pmu,
                #[cfg(not(feature = "machine"))]
                info: self.inner.info,
            },
        }
    }

    /// Add System Reset extension to RustSBI
    #[inline]
    pub fn with_reset<S2: Reset>(self, reset: S2) -> Builder<T, I, R, H, S2, P> {
        Builder {
            inner: RustSBI {
                timer: self.inner.timer,
                ipi: self.inner.ipi,
                rfnc: self.inner.rfnc,
                hsm: self.inner.hsm,
                srst: Some(reset),
                pmu: self.inner.pmu,
                #[cfg(not(feature = "machine"))]
                info: self.inner.info,
            },
        }
    }

    /// Add Performance Monitor Unit extension to RustSBI
    #[inline]
    pub fn with_pmu<P2: Pmu>(self, pmu: P2) -> Builder<T, I, R, H, S, P2> {
        Builder {
            inner: RustSBI {
                timer: self.inner.timer,
                ipi: self.inner.ipi,
                rfnc: self.inner.rfnc,
                hsm: self.inner.hsm,
                srst: self.inner.srst,
                pmu: Some(pmu),
                #[cfg(not(feature = "machine"))]
                info: self.inner.info,
            },
        }
    }

    /// Build the target RustSBI instance
    #[inline]
    pub fn build(self) -> RustSBI<T, I, R, H, S, P> {
        self.inner
    }
}

// Placeholder for a structure that implements all RustSBI traits but is never accessed

// fixme: Should be replaced to never type `!` once it's stablized
// https://github.com/rust-lang/rust/issues/35121

// fixme: should be replaced to impl SomeTrait for ! once never type is stablized

impl Timer for Infallible {
    fn set_timer(&self, _: u64) {
        unreachable!()
    }
}

impl Ipi for Infallible {
    fn send_ipi(&self, _: HartMask) -> SbiRet {
        unreachable!()
    }
}

impl Fence for Infallible {
    fn remote_fence_i(&self, _: HartMask) -> SbiRet {
        unreachable!()
    }

    fn remote_sfence_vma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }

    fn remote_sfence_vma_asid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }

    fn remote_hfence_gvma_vmid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }

    fn remote_hfence_gvma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }

    fn remote_hfence_vvma_asid(&self, _: HartMask, _: usize, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }

    fn remote_hfence_vvma(&self, _: HartMask, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }
}

impl Hsm for Infallible {
    fn hart_start(&self, _: usize, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }

    fn hart_stop(&self) -> SbiRet {
        unreachable!()
    }

    fn hart_get_status(&self, _: usize) -> SbiRet {
        unreachable!()
    }

    fn hart_suspend(&self, _: u32, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }
}

impl Reset for Infallible {
    fn system_reset(&self, _: u32, _: u32) -> SbiRet {
        unreachable!()
    }
    // no leagcy_reset here, instance based interface is not compatible to legacy extension
}

impl Pmu for Infallible {
    fn num_counters(&self) -> usize {
        unreachable!()
    }

    fn counter_get_info(&self, _: usize) -> SbiRet {
        unreachable!()
    }

    fn counter_config_matching(&self, _: usize, _: usize, _: usize, _: usize, _: u64) -> SbiRet {
        unreachable!()
    }

    fn counter_start(&self, _: usize, _: usize, _: usize, _: u64) -> SbiRet {
        unreachable!()
    }

    fn counter_stop(&self, _: usize, _: usize, _: usize) -> SbiRet {
        unreachable!()
    }

    fn counter_fw_read(&self, _: usize) -> SbiRet {
        unreachable!()
    }
}