-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsbi.zig
3070 lines (2680 loc) · 111 KB
/
sbi.zig
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2025 Lee Cannon <[email protected]>
/// The base extension is designed to be as small as possible.
///
/// As such, it only contains functionality for probing which SBI extensions are available and for querying the version
/// of the SBI.
///
/// All functions in the base extension must be supported by all SBI implementations.
pub const base = struct {
/// Returns the current SBI specification version.
///
/// Available from SBI v0.2.
pub fn getSpecVersion() SpecVersion {
return @bitCast(ecall.zeroArgsWithReturnNoError(
.BASE,
@intFromEnum(BASE_FID.GET_SPEC_VERSION),
));
}
/// Returns the current SBI implementation ID, which is different for every SBI implementation.
///
/// It is intended that this implementation ID allows software to probe for SBI implementation quirks
///
/// Available from SBI v0.2.
pub fn getImplementationId() ImplementationId {
return @enumFromInt(ecall.zeroArgsWithReturnNoError(
.BASE,
@intFromEnum(BASE_FID.GET_IMP_ID),
));
}
/// Returns the current SBI implementation version.
///
/// The encoding of this version number is specific to the SBI implementation.
///
/// Available from SBI v0.2.
pub fn getImplementationVersion() isize {
return ecall.zeroArgsWithReturnNoError(
.BASE,
@intFromEnum(BASE_FID.GET_IMP_VERSION),
);
}
/// Returns `true` if the given SBI extension ID (EID) is available, or `false` if it is not available.
///
/// Available from SBI v0.2.
pub fn probeExtension(eid: EID) bool {
return ecall.oneArgsWithReturnNoError(
.BASE,
@intFromEnum(BASE_FID.PROBE_EXT),
@intFromEnum(eid),
) != 0;
}
/// Return a value that is legal for the `mvendorid` CSR and 0 is always a legal value for this CSR.
///
/// Available from SBI v0.2.
pub fn machineVendorId() isize {
return ecall.zeroArgsWithReturnNoError(
.BASE,
@intFromEnum(BASE_FID.GET_MVENDORID),
);
}
/// Return a value that is legal for the `marchid` CSR and 0 is always a legal value for this CSR.
///
/// Available from SBI v0.2.
pub fn machineArchId() isize {
return ecall.zeroArgsWithReturnNoError(
.BASE,
@intFromEnum(BASE_FID.GET_MARCHID),
);
}
/// Return a value that is legal for the `mimpid` CSR and 0 is always a legal value for this CSR.
///
/// Available from SBI v0.2.
pub fn machineImplementationId() isize {
return ecall.zeroArgsWithReturnNoError(
.BASE,
@intFromEnum(BASE_FID.GET_MIMPID),
);
}
pub const ImplementationId = enum(isize) {
@"Berkeley Boot Loader (BBL)" = 0,
OpenSBI = 1,
Xvisor = 2,
KVM = 3,
RustSBI = 4,
Diosix = 5,
Coffer = 6,
@"Xen Project" = 7,
@"PolarFire Hart Software Services" = 8,
coreboot = 9,
oreboot = 10,
bhyve = 11,
_,
};
pub const SpecVersion = packed struct(usize) {
minor: u24,
major: u7,
_reserved: u1,
_: if (is_64) u32 else u0,
};
pub const EID = enum(i32) {
LEGACY_SET_TIMER = 0x0,
LEGACY_CONSOLE_PUTCHAR = 0x1,
LEGACY_CONSOLE_GETCHAR = 0x2,
LEGACY_CLEAR_IPI = 0x3,
LEGACY_SEND_IPI = 0x4,
LEGACY_REMOTE_FENCE_I = 0x5,
LEGACY_REMOTE_SFENCE_VMA = 0x6,
LEGACY_REMOTE_SFENCE_VMA_ASID = 0x7,
LEGACY_SHUTDOWN = 0x8,
BASE = 0x10,
TIME = 0x54494D45,
IPI = 0x735049,
RFENCE = 0x52464E43,
HSM = 0x48534D,
SRST = 0x53525354,
PMU = 0x504D55,
DBCN = 0x4442434E,
SUSP = 0x53555350,
CPPC = 0x43505043,
NACL = 0x4E41434C,
STA = 0x535441,
_,
};
const BASE_FID = enum(i32) {
GET_SPEC_VERSION = 0x0,
GET_IMP_ID = 0x1,
GET_IMP_VERSION = 0x2,
PROBE_EXT = 0x3,
GET_MVENDORID = 0x4,
GET_MARCHID = 0x5,
GET_MIMPID = 0x6,
};
};
pub const time = struct {
pub fn available() bool {
return base.probeExtension(.TIME);
}
/// Programs the clock for next event after `time_value` time.
///
/// `stime_value` is in absolute time.
///
/// If the supervisor wishes to clear the timer interrupt without scheduling the next timer event, it may request a
/// timer interrupt infinitely far into the future (i.e., `setTimer(std.math.maxInt(u64)`).
///
/// Alternatively, to not receive timer interrupts, it may mask timer interrupts by clearing the `sie.STIE` CSR bit.
///
/// This function must clear the pending timer interrupt bit when `time_value` is set to some time in the future,
/// regardless of whether timer interrupts are masked or not.
///
/// Available from SBI v0.2.
pub fn setTimer(time_value: u64) void {
ecall.oneArgs64NoReturnNoError(
.TIME,
@intFromEnum(TIME_FID.TIME_SET_TIMER),
time_value,
);
}
const TIME_FID = enum(i32) {
TIME_SET_TIMER = 0x0,
};
};
pub const ipi = struct {
pub fn available() bool {
return base.probeExtension(.IPI);
}
pub const SendIPIError = error{
/// Either hart_mask_base or at least one hartid from hart_mask is not valid, i.e., either the hartid is not
/// enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Send an inter-processor interrupt to all the harts defined in `hart_mask`.
///
/// Interprocessor interrupts manifest at the receiving harts as supervisor software interrupts.
///
/// Available from SBI v0.2.
pub fn sendIPI(hart_mask: HartMask) SendIPIError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.twoArgsNoReturnWithError(
.IPI,
@intFromEnum(IPI_FID.SEND_IPI),
mask,
mask_base,
SendIPIError,
);
}
const IPI_FID = enum(i32) {
SEND_IPI = 0x0,
};
};
pub const rfence = struct {
pub fn available() bool {
return base.probeExtension(.RFENCE);
}
pub const RemoteFenceIError = error{
/// Either hart_mask_base or at least one hartid from hart_mask is not valid, i.e., either the hartid is not
/// enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Instructs remote harts to execute `FENCE.I` instruction.
///
/// Available from SBI v0.2.
pub fn remoteFenceI(hart_mask: HartMask) RemoteFenceIError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.twoArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.FENCE_I),
mask,
mask_base,
RemoteFenceIError,
);
}
pub const RemoteSFenceVMAError = error{
/// Either hart_mask_base or at least one hartid from hart_mask is not valid, i.e., either the hartid is not
/// enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// start_addr or size is not valid.
InvalidAddress,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Instructs the remote harts to execute one or more `SFENCE.VMA` instructions, covering the range of
/// virtual addresses between `start_addr` and `start_addr + size`.
///
/// The remote fence operation applies to the entire address space if either:
/// - `start_addr` and `size` are both `0`
/// - `size` is equal to `2^XLEN-1`
///
/// Available from SBI v0.2.
pub fn remoteSFenceVMA(
hart_mask: HartMask,
start_addr: usize,
size: usize,
) RemoteSFenceVMAError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.fourArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.SFENCE_VMA),
mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
RemoteSFenceVMAError,
);
}
pub const RemoteSFenceVMAWithASIDError = error{
/// Either asid, hart_mask_base, or at least one hartid from hart_mask is not valid, i.e., either the hartid is
/// not enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// start_addr or size is not valid.
InvalidAddress,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Instructs the remote harts to execute one or more `SFENCE.VMA` instructions, covering the range of virtual
/// addresses between `start_addr` and `start_addr + size`.
///
/// This covers only the given ASID.
///
/// The remote fence operation applies to the entire address space if either:
/// - `start_addr` and `size` are both `0`
/// - `size` is equal to `2^XLEN-1`
///
/// Available from SBI v0.2.
pub fn remoteSFenceVMAWithASID(
hart_mask: HartMask,
start_addr: usize,
size: usize,
asid: usize,
) RemoteSFenceVMAWithASIDError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.fiveArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.SFENCE_VMA_ASID),
mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
@bitCast(asid),
RemoteSFenceVMAWithASIDError,
);
}
pub const RemoteHFenceGVMAWithVMIDError = error{
/// This function is not supported as it is not implemented or one of the target hart doesn’t support
/// hypervisor extension.
NotSupported,
/// Either vmid, hart_mask_base, or at least one hartid from hart_mask is not valid, i.e., either the hartid is
/// not enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// start_addr or size is not valid.
InvalidAddress,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Instruct the remote harts to execute one or more `HFENCE.GVMA` instructions, covering the range of guest physical
/// addresses between `start_addr` and `start_addr + size` only for the given VMID.
///
/// The remote fence operation applies to the entire address space if either:
/// - `start_addr` and `size` are both `0`
/// - `size` is equal to `2^XLEN-1`
///
/// This function call is only valid for harts implementing hypervisor extension.
///
/// Available from SBI v0.2.
pub fn remoteHFenceGVMAWithVMID(
hart_mask: HartMask,
start_addr: usize,
size: usize,
vmid: usize,
) RemoteHFenceGVMAWithVMIDError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.fiveArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_GVMA_VMID),
mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
@bitCast(vmid),
RemoteHFenceGVMAWithVMIDError,
);
}
pub const RemoteHFenceGVMAError = error{
/// This function is not supported as it is not implemented or one of the target hart doesn’t support
/// hypervisor extension.
NotSupported,
/// Either hart_mask_base or at least one hartid from hart_mask is not valid, i.e., either the hartid is
/// not enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// start_addr or size is not valid.
InvalidAddress,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Instruct the remote harts to execute one or more `HFENCE.GVMA` instructions, covering the range of guest physical
/// addresses between `start_addr` and `start_addr + size` for all guests.
///
/// The remote fence operation applies to the entire address space if either:
/// - `start_addr` and `size` are both `0`
/// - `size` is equal to `2^XLEN-1`
///
/// This function call is only valid for harts implementing hypervisor extension.
///
/// Available from SBI v0.2.
pub fn remoteHFenceGVMA(
hart_mask: HartMask,
start_addr: usize,
size: usize,
) RemoteHFenceGVMAError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.fourArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_GVMA),
mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
RemoteHFenceGVMAError,
);
}
pub const RemoteHFenceVVMAWithASIDError = error{
/// This function is not supported as it is not implemented or one of the target hart doesn’t support
/// hypervisor extension.
NotSupported,
/// Either asid, hart_mask_base, or at least one hartid from hart_mask is not valid, i.e., either the hartid is
/// not enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// start_addr or size is not valid.
InvalidAddress,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Instruct the remote harts to execute one or more `HFENCE.VVMA` instructions, covering the range of guest virtual
/// addresses between `start_addr` and `start_addr + size` for the given ASID and current VMID (in hgatp CSR) of
/// calling hart.
///
/// The remote fence operation applies to the entire address space if either:
/// - `start_addr` and `size` are both `0`
/// - `size` is equal to `2^XLEN-1`
///
/// This function call is only valid for harts implementing hypervisor extension.
///
/// Available from SBI v0.2.
pub fn remoteHFenceVVMAWithASID(
hart_mask: HartMask,
start_addr: usize,
size: usize,
asid: usize,
) RemoteHFenceVVMAWithASIDError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.fiveArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_VVMA_ASID),
mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
@bitCast(asid),
RemoteHFenceVVMAWithASIDError,
);
}
pub const RemoteHFenceVVMAError = error{
/// This function is not supported as it is not implemented or one of the target hart doesn’t support
/// hypervisor extension.
NotSupported,
/// Either hart_mask_base or at least one hartid from hart_mask is not valid, i.e., either the hartid is
/// not enabled by the platform or is not available to the supervisor.
InvalidParameter,
/// start_addr or size is not valid.
InvalidAddress,
/// The request failed for unspecified or unknown other reasons.
Failed,
};
/// Instruct the remote harts to execute one or more `HFENCE.VVMA` instructions, covering the range of guest virtual
/// addresses between `start_addr` and `start_addr + size` for current VMID (in hgatp CSR) of calling hart.
///
/// The remote fence operation applies to the entire address space if either:
/// - `start_addr` and `size` are both `0`
/// - `size` is equal to `2^XLEN-1`
///
/// This function call is only valid for harts implementing hypervisor extension.
///
/// Available from SBI v0.2.
pub fn remoteHFenceVVMA(
hart_mask: HartMask,
start_addr: usize,
size: usize,
) RemoteHFenceVVMAError!void {
const mask, const mask_base = hart_mask.toMaskAndBase();
return ecall.fourArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_VVMA),
mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
RemoteHFenceVVMAError,
);
}
const RFENCE_FID = enum(i32) {
FENCE_I = 0x0,
SFENCE_VMA = 0x1,
SFENCE_VMA_ASID = 0x2,
HFENCE_GVMA_VMID = 0x3,
HFENCE_GVMA = 0x4,
HFENCE_VVMA_ASID = 0x5,
HFENCE_VVMA = 0x6,
};
};
/// The Hart State Management (HSM) Extension introduces a set of hart states and a set of functions which allow the
/// supervisor-mode software to request a hart state change.
///
/// A platform can have multiple harts grouped into hierarchical topology groups (namely cores, clusters, nodes, etc.)
/// with separate platform specific low-power states for each hierarchical group. These platform specific low-power
/// states of hierarchical topology groups can be represented as platform specific suspend states of a hart.
///
/// An SBI implementation can utilize the suspend states of higher topology groups using one of the following approaches:
/// - Platform-coordinated: In this approach, when a hart becomes idle the supervisor-mode power-management software
/// will request deepest suspend state for the hart and higher topology groups.
/// An SBI implementation should choose a suspend state at higher topology group which is:
/// - Not deeper than the specified suspend state
/// - Wake-up latency is not higher than the wake-up latency of the specified suspend state
///
/// - OS-inititated: In this approach, the supervisor-mode power-managment software will directly request a suspend
/// state for higher topology group after the last hart in that group becomes idle. When a hart becomes idle, the
/// supervisor-mode power-managment software will always select suspend state for the hart itself but it will select a
/// suspend state for a higher topology group only if the hart is the last running hart in the group.
/// An SBI implementation should:
/// - Never choose a suspend state for higher topology group different from the specified suspend state
/// - Always prefer most recent suspend state requested for higher topology group
pub const hsm = struct {
pub fn available() bool {
return base.probeExtension(.HSM);
}
pub const HartStartError = error{
/// `start_physical_address` is not valid, possibly due to the following reasons:
/// - It is not a valid physical address.
/// - Executable access to the address is prohibited by a physical memory protection mechanism or H-extension
/// G-stage for supervisor-mode.
InvalidAddress,
/// `hartid` is not a valid hartid as the corresponding hart cannot be started in supervisor mode.
InvalidParameter,
/// The given hartid is already started.
AlreadyAvailable,
/// The start request failed for unspecified or unknown other reasons.
Failed,
};
/// Request the SBI implementation to start executing the target hart in supervisor-mode, at the address specified
/// by `start_addr`, with register values:
///
/// | Register Name | Register Value |
/// | ------------- | ---------------------- |
/// | `satp` | `0` |
/// | `sstatus.SIE` | `0` |
/// | `a0` | `hartid` |
/// | `a1` | `user_value` parameter |
///
/// All other registers are in an undefined state.
///
/// This call is asynchronous — more specifically, `hartStart` may return before the target hart starts executing as
/// long as the SBI implementation is capable of ensuring the return code is accurate.
///
/// If the SBI implementation is a platform runtime firmware executing in machine-mode (M-mode), then it MUST
/// configure any physical memory protection it supports, such as that defined by PMP, and other M-mode state,
/// before transferring control to supervisor-mode software.
///
/// Available from SBI v0.2.
pub fn hartStart(
hartid: usize,
start_physical_address: usize,
user_value: usize,
) HartStartError!void {
return ecall.threeArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_START),
@bitCast(hartid),
@bitCast(start_physical_address),
@bitCast(user_value),
HartStartError,
);
}
pub const HartStopError = error{
/// Failed to stop execution of the current hart.
Failed,
};
/// Request the SBI implementation to stop executing the calling hart in supervisor-mode and return its ownership to
/// the SBI implementation.
///
/// This call is not expected to return under normal conditions.
///
/// Must be called with supervisor-mode interrupts disabled.
///
/// Available from SBI v0.2.
pub fn hartStop() HartStopError!noreturn {
try ecall.zeroArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_STOP),
HartStopError,
);
unreachable;
}
pub const HartStatusError = error{
/// The given hartid is not valid.
InvalidParameter,
};
/// Get the current status (or HSM state id) of the given hart.
///
/// The harts may transition HSM states at any time due to any concurrent `hartStart`, `hartStop` or `hartSuspend`
/// calls, the return value from this function may not represent the actual state of the hart at the time of return
/// value verification.
///
/// Available from SBI v0.2.
pub fn hartStatus(hartid: usize) HartStatusError!State {
return @enumFromInt(try ecall.oneArgsWithReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_GET_STATUS),
@bitCast(hartid),
HartStatusError,
));
}
pub const HartSuspendError = error{
/// `suspend_type` is not reserved and is implemented, but the platform does not support it due to one or more
/// missing dependencies.
NotSupported,
/// `suspend_type` is reserved or is platform-specific and unimplemented.
InvalidParameter,
/// `resume_physical_address` is not valid, possibly due to the following reasons:
/// - It is not a valid physical address.
/// - Executable access to the address is prohibited by a physical memory protection mechanism or H-extension
/// G-stage for supervisor-mode.
InvalidAddress,
/// The suspend request failed for unspecified or unknown other reasons.
Failed,
};
/// Request the SBI implementation to put the calling hart in a platform specific suspend (or low power) state
/// specified by the `suspend_type` parameter.
///
/// The hart will automatically come out of suspended state and resume normal execution when it receives an
/// interrupt or platform specific hardware event.
///
/// The platform specific suspend states for a hart can be either retentive or non-retentive in nature.
///
/// A retentive suspend state will preserve hart register and CSR values for all privilege modes whereas a
/// non-retentive suspend state will not preserve hart register and CSR values.
///
/// Resuming from a retentive suspend state is straight forward and the supervisor-mode software will see
/// `hartSuspend` return without any failures. The `resume_physical_address` parameter is unused during retentive
/// suspend.
///
/// Resuming from a non-retentive suspend state is relatively more involved and requires software to restore various
/// hart registers and CSRs for all privilege modes. Upon resuming from non-retentive suspend state, the hart will
/// jump to supervisor-mode at address specified by `resume_physical_address` with register values:
///
/// | Register Name | Register Value |
/// | ------------- | ---------------------- |
/// | `satp` | `0` |
/// | `sstatus.SIE` | `0` |
/// | `a0` | `hartid` |
/// | `a1` | `user_value` parameter |
///
/// All other registers are in an undefined state.
///
/// Available from SBI v0.3.
pub fn hartSuspend(
suspend_type: SuspendType,
resume_physical_address: usize,
user_value: usize,
) HartSuspendError!void {
return ecall.threeArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_SUSPEND),
@bitCast(@as(usize, suspend_type.toRaw())),
@bitCast(resume_physical_address),
@bitCast(user_value),
HartSuspendError,
);
}
pub const SuspendType = union(enum) {
/// Default retentive suspend
retentive,
/// Default non-retentive suspend
non_retentive,
/// | Value | Description |
/// | ------------------------- | --------------------------------------- |
/// | `0x00000000` | Default retentive suspend |
/// | `0x00000001 - 0x0FFFFFFF` | Reserved for future use |
/// | `0x10000000 - 0x7FFFFFFF` | Platform specific retentive suspend |
/// | `0x80000000` | Default non-retentive suspend |
/// | `0x80000001 - 0x8FFFFFFF` | Reserved for future use |
/// | `0x90000000 - 0xFFFFFFFF` | Platform specific non-retentive suspend |
custom: u32,
fn toRaw(self: SuspendType) u32 {
return switch (self) {
.retentive => 0x00000000,
.non_retentive => 0x80000000,
.custom => |custom| custom,
};
}
};
pub const State = enum(isize) {
/// The hart is physically powered-up and executing normally.
started = 0x0,
/// The hart is not executing in supervisor-mode or any lower privilege mode.
///
/// It is probably powered-down by the SBI implementation if the underlying platform has a mechanism to
/// physically power-down harts.
stopped = 0x1,
/// Some other hart has requested to start (or power-up) the hart from the `stopped` state and the SBI
/// implementation is still working to get the hart in the `started` state.
start_pending = 0x2,
/// The hart has requested to stop (or power-down) itself from the `started` state and the SBI implementation is
/// still working to get the hart in the `stopped` state.
stop_pending = 0x3,
/// This hart is in a platform specific suspend (or low power) state.
suspended = 0x4,
/// The hart has requested to put itself in a platform specific low power state from the `started` state and the
/// SBI implementation is still working to get the hart in the platform specific `suspended` state.
suspend_pending = 0x5,
/// An interrupt or platform specific hardware event has caused the hart to resume normal execution from the
/// `suspended` state and the SBI implementation is still working to get the hart in the `started` state.
resume_pending = 0x6,
_,
};
const HSM_FID = enum(i32) {
HART_START = 0x0,
HART_STOP = 0x1,
HART_GET_STATUS = 0x2,
HART_SUSPEND = 0x3,
};
};
/// The System Reset Extension provides a function that allow the supervisor software to request system-level reboot or
/// shutdown.
///
/// The term "system" refers to the world-view of supervisor software and the underlying SBI implementation could be
/// provided by machine mode firmware or a hypervisor.
pub const reset = struct {
pub fn available() bool {
return base.probeExtension(.SRST);
}
pub const SystemResetError = error{
/// `reset_type` is not reserved and is implemented, but the platform does not support it due to one or more
/// missing dependencies.
NotSupported,
/// At least one of `reset_type` or `reset_reason` is reserved or is platform-specific and unimplemented.
InvalidParameter,
/// The reset request failed for unspecified or unknown other reasons.
Failed,
};
/// Reset the system based on provided `reset_type` and `reset_reason`.
///
/// This is a synchronous call and does not return if it succeeds.
///
/// When supervisor software is running natively, the SBI implementation is provided by machine mode firmware.
/// In this case, shutdown is equivalent to a physical power down of the entire system and cold reboot is equivalent
/// to a physical power cycle of the entire system.
/// Further, warm reboot is equivalent to a power cycle of the main processor and parts of the system, but not the entire system.
///
/// For example, on a server class system with a BMC (board management controller), a warm reboot will not power
/// cycle the BMC whereas a cold reboot will definitely power cycle the BMC.
///
/// When supervisor software is running inside a virtual machine, the SBI implementation is provided by a hypervisor.
/// Shutdown, cold reboot and warm reboot will behave functionally the same as the native case, but might not result
/// in any physical power changes.
///
/// Available from SBI v0.3.
pub fn systemReset(
reset_type: ResetType,
reset_reason: ResetReason,
) SystemResetError!noreturn {
try ecall.twoArgsNoReturnWithError(
.SRST,
@intFromEnum(SRST_FID.RESET),
@bitCast(@as(usize, reset_type.toRaw())),
@bitCast(@as(usize, reset_reason.toRaw())),
SystemResetError,
);
unreachable;
}
pub const ResetType = union(enum) {
shutdown,
cold_reboot,
warm_reboot,
/// | Value | Description |
/// | ------------------------- | -------------------------------------- |
/// | `0x00000000` | Shutdown |
/// | `0x00000001` | Cold reboot |
/// | `0x00000002` | Warm reboot |
/// | `0x00000003 - 0xEFFFFFFF` | Reserved for future use |
/// | `0xF0000000 - 0xFFFFFFFF` | Vendor or platform specific reset type |
custom: u32,
fn toRaw(self: ResetType) u32 {
return switch (self) {
.shutdown => 0x00000000,
.cold_reboot => 0x00000001,
.warm_reboot => 0x00000002,
.custom => |custom| custom,
};
}
};
pub const ResetReason = union(enum) {
no_reason,
system_failure,
/// | Value | Description |
/// | ------------------------- | ---------------------------------------- |
/// | `0x00000000` | No reason |
/// | `0x00000001` | System failure |
/// | `0x00000002 - 0xDFFFFFFF` | Reserved for future use |
/// | `0xE0000000 - 0xEFFFFFFF` | SBI implementation specific reset reason |
/// | `0xF0000000 - 0xFFFFFFFF` | Vendor or platform specific reset reason |
custom: u32,
fn toRaw(self: ResetReason) u32 {
return switch (self) {
.no_reason => 0x00000000,
.system_failure => 0x00000001,
.custom => |custom| custom,
};
}
};
const SRST_FID = enum(i32) {
RESET = 0x0,
};
};
/// The RISC-V hardware performance counters such as `mcycle`, `minstret`, and `mhpmcounterX` CSRs are accessible as
/// read-only from supervisor-mode using `cycle`, `instret`, and `hpmcounterX` CSRs.
///
/// The SBI performance monitoring unit (PMU) extension is an interface for supervisor-mode to configure and use the
/// RISC-V hardware performance counters with assistance from the machine-mode (or hypervisor-mode).
///
/// These hardware performance counters can only be started, stopped, or configured from machine-mode using
/// `mcountinhibit` and `mhpmeventX` CSRs. Due to this, a machine-mode SBI implementation may choose to disallow SBI PMU
/// extension if `mcountinhibit` CSR is not implemented by the RISC-V platform.
///
/// A RISC-V platform generally supports monitoring of various hardware events using a limited number of hardware
/// performance counters which are up to 64 bits wide. In addition, a SBI implementation can also provide firmware
/// performance counters which can monitor firmware events such as numberof misaligned load/store instructions, number
/// of RFENCEs, number of IPIs, etc.
///
/// All firmware counters must have same number of bits and can be up to 64 bits wide.
///
/// The SBI PMU extension provides:
/// - An interface for supervisor-mode software to discover and configure per-hart hardware/firmware counters
/// - A typical perf compatible interface for hardware/firmware performance counters and events
/// - Full access to microarchitecture’s raw event encodings
pub const pmu = struct {
pub fn available() bool {
return base.probeExtension(.PMU);
}
/// Returns the number of counters (both hardware and firmware).
///
/// Available from SBI v0.3.
pub fn getNumberOfCounters() usize {
return @bitCast(ecall.zeroArgsWithReturnNoError(.PMU, @intFromEnum(PMU_FID.NUM_COUNTERS)));
}
pub const GetCounterInfoError = error{
/// `counter_index` points to an invalid counter.
InvalidParameter,
};
/// Get details about the specified counter such as underlying CSR number, width of the counter, type of counter
/// hardware/firmware, etc.
///
/// Available from SBI v0.3.
pub fn getCounterInfo(counter_index: usize) GetCounterInfoError!CounterInfo {
const raw: CounterInfo.Raw = @bitCast(try ecall.oneArgsWithReturnWithError(
.PMU,
@intFromEnum(PMU_FID.COUNTER_GET_INFO),
@bitCast(counter_index),
GetCounterInfoError,
));
return switch (raw.type) {
.hardware => .{
.hardware = .{
.csr = raw.csr,
.width = raw.width,
},
},
.firmware => .firmware,
};
}
pub const ConfigureMatchingCounterError = error{
/// none of the counters can monitor the specified event.
NotSupported,
/// set of counters has at least one invalid counter.
InvalidParameter,
};
/// Find and configure a counter from a set of counters which is not started (or enabled) and can monitor the
/// specified event.
///
/// The `counter_mask` parameter represent the set of counters whereas `event` represents the event to be monitored.
///
/// Available from SBI v0.3.
pub fn configureMatchingCounter(
counter_mask: CounterMask,
event: Event,
config_flags: ConfigFlags,
) ConfigureMatchingCounterError!usize {
const raw = event.toRaw();
return @bitCast(try ecall.fiveArgsLastArg64WithReturnWithError(
.PMU,
@intFromEnum(PMU_FID.COUNTER_CFG_MATCH),
@bitCast(counter_mask.base),
@bitCast(counter_mask.mask),
@bitCast(config_flags),
@as(u20, @bitCast(raw.event_index)),
raw.event_data,
ConfigureMatchingCounterError,
));
}
pub const StartCountersError = error{
/// set of counters has at least one invalid counter or the given flag parameter has an undefined bit set.
InvalidParameter,
/// set of counters includes at least one counter which is already started.
AlreadyStarted,
/// the snapshot shared memory is not available and `start_mode` is `.init_snapshot`.
NoSharedMemory,
};
/// Start or enable a set of counters on the calling hart with the specified initial value.
///
/// The `counter_mask` parameter represents the set of counters.
///
/// Available from SBI v0.3.
pub fn startCounters(
counter_mask: CounterMask,
start_mode: StartMode,
) StartCountersError!void {
return ecall.fourArgsLastArg64NoReturnWithError(
.PMU,
@intFromEnum(PMU_FID.COUNTER_START),
@bitCast(counter_mask.base),
@bitCast(counter_mask.mask),
@bitCast(start_mode.toRaw()),
start_mode.initialValue(),
StartCountersError,
);
}
pub const StopCountersError = error{
InvalidParameter,
AlreadyStopped,
};
/// Stop or disable a set of counters on the calling hart.
///