sbi 0.3.0

A pure-Rust library to interact with the RISC-V Supervisor Binary Interface
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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2023 repnop
//
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at https://mozilla.org/MPL/2.0/.

use crate::{ecall1, ecall3, PhysicalAddress, SbiError};

/// Nested Acceleration extension ID
pub const EXTENSION_ID: usize = 0x4E41434C;

mod sealed {
    pub trait Sealed {}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct CsrAddress(u16);

impl CsrAddress {
    pub const fn new(raw: u16) -> Option<Self> {
        match raw {
            0x200..=0x2FF
            | 0x600..=0x67F
            | 0x680..=0x6BF
            | 0x6C0..=0x6FF
            | 0xA00..=0xA7F
            | 0xA80..=0xABF
            | 0xAC0..=0xAFF
            | 0xE00..=0xE7F
            | 0xE80..=0xEBF
            | 0xEC0..=0xEFF => Some(Self(raw)),
            _ => None,
        }
    }

    pub const fn new_unchecked(raw: u16) -> Self {
        Self(raw)
    }
}

pub trait HExtensionCsr: Sized + Copy {
    const ADDRESS: CsrAddress;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct UpdateCsrAddress(u16);

impl From<CsrAddress> for UpdateCsrAddress {
    fn from(value: CsrAddress) -> Self {
        Self(value.0)
    }
}

pub const UPDATE_ALL_CSRS: UpdateCsrAddress = UpdateCsrAddress(u16::MAX);

pub mod csrs {
    use super::CsrAddress;

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hstatus(usize);
    impl super::HExtensionCsr for Hstatus {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x600);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hedeleg(usize);
    impl super::HExtensionCsr for Hedeleg {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x602);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hideleg(usize);
    impl super::HExtensionCsr for Hideleg {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x603);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hie(usize);
    impl super::HExtensionCsr for Hie {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x604);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hcounteren(usize);
    impl super::HExtensionCsr for Hcounteren {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x606);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hgeie(usize);
    impl super::HExtensionCsr for Hgeie {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x607);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Htval(usize);
    impl super::HExtensionCsr for Htval {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x643);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hip(usize);
    impl super::HExtensionCsr for Hip {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x644);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hvip(usize);
    impl super::HExtensionCsr for Hvip {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x645);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Htinst(usize);
    impl super::HExtensionCsr for Htinst {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x64A);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hgeip(usize);
    impl super::HExtensionCsr for Hgeip {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0xE12);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Henvcfg(usize);
    impl super::HExtensionCsr for Henvcfg {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x60A);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Henvcfgh(usize);
    impl super::HExtensionCsr for Henvcfgh {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x61A);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hgatp(usize);
    impl super::HExtensionCsr for Hgatp {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x680);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Hcontext(usize);
    impl super::HExtensionCsr for Hcontext {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x6A8);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Htimedelta(usize);
    impl super::HExtensionCsr for Htimedelta {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x605);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Htimedeltah(usize);
    impl super::HExtensionCsr for Htimedeltah {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x615);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vsstatus(usize);
    impl super::HExtensionCsr for Vsstatus {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x200);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vsie(usize);
    impl super::HExtensionCsr for Vsie {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x204);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vstvec(usize);
    impl super::HExtensionCsr for Vstvec {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x205);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vsscratch(usize);
    impl super::HExtensionCsr for Vsscratch {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x240);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vsepc(usize);
    impl super::HExtensionCsr for Vsepc {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x241);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vscause(usize);
    impl super::HExtensionCsr for Vscause {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x242);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vstval(usize);
    impl super::HExtensionCsr for Vstval {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x243);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vsip(usize);
    impl super::HExtensionCsr for Vsip {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x244);
    }

    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    #[repr(transparent)]
    pub struct Vsatp(usize);
    impl super::HExtensionCsr for Vsatp {
        const ADDRESS: CsrAddress = CsrAddress::new_unchecked(0x280);
    }
}

#[repr(transparent)]
pub struct Volatile<T: Copy>(T);

pub trait VolatileOps<T: Copy> {
    unsafe fn volatile_read(self) -> T;
    unsafe fn volatile_write(self, value: T);
}

impl<T: Copy> VolatileOps<T> for *mut Volatile<T> {
    unsafe fn volatile_read(self) -> T {
        unsafe { core::ptr::addr_of_mut!((*self).0).read_volatile() }
    }

    unsafe fn volatile_write(self, value: T) {
        unsafe { core::ptr::addr_of_mut!((*self).0).write_volatile(value) }
    }
}

pub trait NaclFeature: sealed::Sealed {
    const ID: u32;
}

#[repr(C, align(4096))]
pub struct SharedMemoryLayout {
    scratch_space: [u8; 4096],
    csr_space: [usize; 128],
}

pub trait CsrSpace {
    fn index<C: HExtensionCsr>(self, csr: C) -> *mut Volatile<C>;
}

#[repr(transparent)]
pub struct SynchronizeCsr([u8; 128]);

const _: () = assert!(core::mem::size_of::<SynchronizeCsr>() == 128);

impl sealed::Sealed for SynchronizeCsr {}
impl NaclFeature for SynchronizeCsr {
    const ID: u32 = 0x00000000;
}

pub trait SynchronizeCsrFeature {
    unsafe fn synchronize_csr(self) -> *mut SynchronizeCsr;
}

impl SynchronizeCsrFeature for *mut SharedMemoryLayout {
    unsafe fn synchronize_csr(self) -> *mut SynchronizeCsr {
        unsafe {
            core::ptr::addr_of_mut!((*self).scratch_space)
                .offset(0x0F80)
                .cast::<SynchronizeCsr>()
        }
    }
}

const NUM_HFENCE_ENTRIES: usize = 1920 / core::mem::size_of::<usize>() / 4;

#[repr(transparent)]
pub struct SynchronizeHfence([[usize; 4]; NUM_HFENCE_ENTRIES]);

const _: () = assert!(core::mem::size_of::<SynchronizeHfence>() == 1920);

impl sealed::Sealed for SynchronizeHfence {}
impl NaclFeature for SynchronizeHfence {
    const ID: u32 = 0x00000001;
}

pub trait SynchronizeHfenceFeature {
    unsafe fn synchronize_hfence(self) -> *mut SynchronizeHfence;
}

impl SynchronizeHfenceFeature for *mut SharedMemoryLayout {
    unsafe fn synchronize_hfence(self) -> *mut SynchronizeHfence {
        unsafe {
            core::ptr::addr_of_mut!((*self).scratch_space)
                .offset(0x0800)
                .cast::<SynchronizeHfence>()
        }
    }
}

const NUM_SRET_ENTRIES: usize = 512 / core::mem::size_of::<usize>();

#[repr(transparent)]
pub struct SynchronizeSret([usize; NUM_SRET_ENTRIES]);

const _: () = assert!(core::mem::size_of::<SynchronizeSret>() == 512);

impl sealed::Sealed for SynchronizeSret {}
impl NaclFeature for SynchronizeSret {
    const ID: u32 = 0x00000002;
}

pub trait SynchronizeSretFeature {
    unsafe fn synchronize_sret(self) -> *mut SynchronizeSret;
}

impl SynchronizeSretFeature for *mut SharedMemoryLayout {
    unsafe fn synchronize_sret(self) -> *mut SynchronizeSret {
        unsafe {
            core::ptr::addr_of_mut!((*self).scratch_space)
                .offset(0x0000)
                .cast::<SynchronizeSret>()
        }
    }
}

const NUM_AUTOSWAP_RESERVED_ENTRIES: usize = 128 / core::mem::size_of::<usize>() - 2;

#[repr(transparent)]
pub struct AutoswapFlags(usize);

#[repr(C)]
pub struct AutoswapCsr {
    autoswap_flags: AutoswapFlags,
    hstatus: csrs::Hstatus,
    _reserved: [usize; NUM_AUTOSWAP_RESERVED_ENTRIES],
}

const _: () = assert!(core::mem::size_of::<AutoswapCsr>() == 128);

impl sealed::Sealed for AutoswapCsr {}
impl NaclFeature for AutoswapCsr {
    const ID: u32 = 0x00000003;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct AutoswapCsrToken(());

pub trait AutoswapCsrFeature {
    unsafe fn autoswap_csr(self) -> *mut AutoswapCsr;
}

impl AutoswapCsrFeature for *mut SharedMemoryLayout {
    unsafe fn autoswap_csr(self) -> *mut AutoswapCsr {
        unsafe {
            core::ptr::addr_of_mut!((*self).scratch_space)
                .offset(0x0200)
                .cast::<AutoswapCsr>()
        }
    }
}

#[doc(alias = "sbi_nacl_probe_feature")]
pub fn probe_feature<F: NaclFeature>() -> Result<bool, SbiError> {
    let value = unsafe { ecall1(F::ID as usize, EXTENSION_ID, 0) }?;

    match value {
        1 => Ok(true),
        _ => Ok(false),
    }
}

#[repr(transparent)]
pub struct Flags(usize);

impl Flags {
    pub const NONE: Self = Self(0);
}

#[doc(alias = "sbi_nacl_set_shmem")]
pub unsafe fn set_shared_memory(
    lo: PhysicalAddress<SharedMemoryLayout>,
    hi: PhysicalAddress<SharedMemoryLayout>,
    flags: Flags,
) -> Result<(), SbiError> {
    unsafe { ecall3(lo.0, hi.0, flags.0, EXTENSION_ID, 1) }.map(drop)
}

pub unsafe fn synchronize_csr<U: Into<UpdateCsrAddress>>(address: U) -> Result<(), SbiError> {
    let UpdateCsrAddress(addr) = address.into();
    unsafe { ecall1(addr as usize, EXTENSION_ID, 2) }.map(drop)
}

pub fn foo() {
    unsafe { synchronize_csr(csrs::Hstatus::ADDRESS) };
}