-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sbi.zig
1826 lines (1631 loc) · 61.6 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
const std = @import("std");
const builtin = @import("builtin");
const runtime_safety = std.debug.runtime_safety;
const is_64: bool = switch (builtin.cpu.arch) {
.riscv64 => true,
.riscv32 => false,
else => |arch| @compileError("only riscv64 and riscv32 targets supported, found target: " ++ @tagName(arch)),
};
pub const Error = error{
FAILED,
NOT_SUPPORTED,
INVALID_PARAM,
DENIED,
INVALID_ADDRESS,
ALREADY_AVAILABLE,
ALREADY_STARTED,
ALREADY_STOPPED,
};
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,
_,
};
/// 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, so there
/// are no error returns defined.
pub const base = struct {
/// Returns the current SBI specification version.
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
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.
pub fn getImplementationVersion() isize {
return ecall.zeroArgsWithReturnNoError(.BASE, @intFromEnum(BASE_FID.GET_IMP_VERSION));
}
/// Returns false if the given SBI extension ID (EID) is not available, or true if it is available.
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.
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.
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.
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,
_,
};
pub const SpecVersion = packed struct {
minor: u24,
major: u7,
_reserved: u1,
_: if (is_64) u32 else u0,
comptime {
std.debug.assert(@sizeOf(usize) == @sizeOf(SpecVersion));
std.debug.assert(@bitSizeOf(usize) == @bitSizeOf(SpecVersion));
}
comptime {
std.testing.refAllDecls(@This());
}
};
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,
};
comptime {
std.testing.refAllDecls(@This());
}
};
/// These legacy SBI extension are deprecated in favor of the other extensions.
/// Each function needs to be individually probed to check for support.
pub const legacy = struct {
pub fn setTimerAvailable() bool {
return base.probeExtension(.LEGACY_SET_TIMER);
}
/// Programs the clock for next event after time_value time.
/// This function also clears the pending timer interrupt bit.
///
/// If the supervisor wishes to clear the timer interrupt without scheduling the next timer event,
/// it can either request a timer interrupt infinitely far into the future
/// (i.e., `@bitCast(u64, @as(i64, -1))`), or it can instead mask the timer interrupt by clearing `sie.STIE` CSR bit.
///
/// This function returns `ImplementationDefinedError` as an implementation specific error is possible.
pub fn setTimer(time_value: u64) ImplementationDefinedError {
return ecall.legacyOneArgs64NoReturnWithRawError(.LEGACY_SET_TIMER, time_value);
}
pub fn consolePutCharAvailable() bool {
return base.probeExtension(.LEGACY_CONSOLE_PUTCHAR);
}
/// Write data present in char to debug console.
/// Unlike `consoleGetChar`, this SBI call will block if there remain any pending characters to be
/// transmitted or if the receiving terminal is not yet ready to receive the byte.
/// However, if the console doesn’t exist at all, then the character is thrown away
///
/// This function returns `ImplementationDefinedError` as an implementation specific error is possible.
pub fn consolePutChar(char: u8) ImplementationDefinedError {
return ecall.legacyOneArgsNoReturnWithRawError(.LEGACY_CONSOLE_PUTCHAR, char);
}
pub fn consoleGetCharAvailable() bool {
return base.probeExtension(.LEGACY_CONSOLE_GETCHAR);
}
/// Read a byte from debug console.
pub fn consoleGetChar() error{FAILED}!u8 {
if (runtime_safety) {
return @intCast(ecall.legacyZeroArgsWithReturnWithError(
.LEGACY_CONSOLE_GETCHAR,
error{ NOT_SUPPORTED, FAILED },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
});
}
return @intCast(try ecall.legacyZeroArgsWithReturnWithError(.LEGACY_CONSOLE_GETCHAR, error{FAILED}));
}
pub fn clearIPIAvailable() bool {
return base.probeExtension(.LEGACY_CLEAR_IPI);
}
/// Clears the pending IPIs if any. The IPI is cleared only in the hart for which this SBI call is invoked.
/// `clearIPI` is deprecated because S-mode code can clear `sip.SSIP` CSR bit directly
pub fn clearIPI() void {
if (runtime_safety) {
ecall.legacyZeroArgsNoReturnWithError(.LEGACY_CLEAR_IPI, error{NOT_SUPPORTED}) catch unreachable;
return;
}
ecall.legacyZeroArgsNoReturnNoError(.LEGACY_CLEAR_IPI);
}
pub fn sendIPIAvailable() bool {
return base.probeExtension(.LEGACY_SEND_IPI);
}
/// Send an inter-processor interrupt to all the harts defined in hart_mask.
/// Interprocessor interrupts manifest at the receiving harts as Supervisor Software Interrupts.
/// `hart_mask` is a virtual address that points to a bit-vector of harts. The bit vector is represented as a
/// sequence of `usize` whose length equals the number of harts in the system divided by the number of bits in a `usize`,
/// rounded up to the next integer.
///
/// This function returns `ImplementationDefinedError` as an implementation specific error is possible.
pub fn sendIPI(hart_mask: [*]const usize) ImplementationDefinedError {
return ecall.legacyOneArgsNoReturnWithRawError(.LEGACY_SEND_IPI, @bitCast(@intFromPtr(hart_mask)));
}
pub fn remoteFenceIAvailable() bool {
return base.probeExtension(.LEGACY_REMOTE_FENCE_I);
}
/// Instructs remote harts to execute FENCE.I instruction.
/// The `hart_mask` is the same as described in `sendIPI`.
///
/// This function returns `ImplementationDefinedError` as an implementation specific error is possible.
pub fn remoteFenceI(hart_mask: [*]const usize) ImplementationDefinedError {
return ecall.legacyOneArgsNoReturnWithRawError(.LEGACY_REMOTE_FENCE_I, @bitCast(@intFromPtr(hart_mask)));
}
pub fn remoteSFenceVMAAvailable() bool {
return base.probeExtension(.LEGACY_REMOTE_SFENCE_VMA);
}
/// Instructs the remote harts to execute one or more SFENCE.VMA instructions, covering the range of
/// virtual addresses between `start` and `size`.
/// The `hart_mask` is the same as described in `sendIPI`.
pub fn remoteSFenceVMA(hart_mask: [*]const usize, start: usize, size: usize) void {
if (runtime_safety) {
ecall.legacyThreeArgsNoReturnWithError(
.LEGACY_REMOTE_SFENCE_VMA,
@bitCast(@intFromPtr(hart_mask)),
@bitCast(start),
@bitCast(size),
error{NOT_SUPPORTED},
) catch unreachable;
return;
}
ecall.legacyThreeArgsNoReturnNoError(
.LEGACY_REMOTE_SFENCE_VMA,
@bitCast(@intFromPtr(hart_mask)),
@bitCast(start),
@bitCast(size),
);
}
pub fn remoteSFenceVMAWithASIDAvailable() bool {
return base.probeExtension(.LEGACY_REMOTE_SFENCE_VMA_ASID);
}
/// Instruct the remote harts to execute one or more SFENCE.VMA instructions, covering the range of
/// virtual addresses between `start` and `size`. This covers only the given ASID.
/// The `hart_mask` is the same as described in `sendIPI`.
///
/// This function returns `ImplementationDefinedError` as an implementation specific error is possible.
pub fn remoteSFenceVMAWithASID(hart_mask: [*]const usize, start: usize, size: usize, asid: usize) ImplementationDefinedError {
return ecall.legacyFourArgsNoReturnWithRawError(
.LEGACY_REMOTE_SFENCE_VMA_ASID,
@bitCast(@intFromPtr(hart_mask)),
@bitCast(start),
@bitCast(size),
@bitCast(asid),
);
}
pub fn systemShutdownAvailable() bool {
return base.probeExtension(.LEGACY_SHUTDOWN);
}
/// Puts all the harts to shutdown state from supervisor point of view.
///
/// This SBI call doesn't return irrespective whether it succeeds or fails.
pub fn systemShutdown() void {
if (runtime_safety) {
ecall.legacyZeroArgsNoReturnWithError(.LEGACY_SHUTDOWN, error{NOT_SUPPORTED}) catch unreachable;
} else {
ecall.legacyZeroArgsNoReturnNoError(.LEGACY_SHUTDOWN);
}
unreachable;
}
comptime {
std.testing.refAllDecls(@This());
}
};
pub const time = struct {
pub fn available() bool {
return base.probeExtension(.TIME);
}
/// Programs the clock for next event after time_value time.
/// This function also clears the pending timer interrupt bit.
///
/// If the supervisor wishes to clear the timer interrupt without scheduling the next timer event,
/// it can either request a timer interrupt infinitely far into the future
/// (i.e., `@bitCast(u64, @as(i64, -1))`), or it can instead mask the timer interrupt by clearing `sie.STIE` CSR bit.
pub fn setTimer(time_value: u64) void {
if (runtime_safety) {
ecall.oneArgs64NoReturnWithError(
.TIME,
@intFromEnum(TIME_FID.TIME_SET_TIMER),
time_value,
error{NOT_SUPPORTED},
) catch unreachable;
return;
}
ecall.oneArgs64NoReturnNoError(
.TIME,
@intFromEnum(TIME_FID.TIME_SET_TIMER),
time_value,
);
}
const TIME_FID = enum(i32) {
TIME_SET_TIMER = 0x0,
};
comptime {
std.testing.refAllDecls(@This());
}
};
pub const HartMask = union(enum) {
/// all available ids must be considered
all,
mask: struct {
/// a scalar bit-vector containing ids
mask: usize,
/// the starting id from which bit-vector must be computed
base: usize,
},
};
pub const ipi = struct {
pub fn available() bool {
return base.probeExtension(.IPI);
}
/// Send an inter-processor interrupt to all the harts defined in `hart_mask`.
/// Interprocessor interrupts manifest at the receiving harts as the supervisor software interrupts.
pub fn sendIPI(hart_mask: HartMask) error{INVALID_PARAM}!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
if (runtime_safety) {
ecall.twoArgsNoReturnWithError(
.IPI,
@intFromEnum(IPI_FID.SEND_IPI),
bit_mask,
mask_base,
error{ NOT_SUPPORTED, INVALID_PARAM },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
};
return;
}
return ecall.twoArgsNoReturnWithError(
.IPI,
@intFromEnum(IPI_FID.SEND_IPI),
bit_mask,
mask_base,
error{INVALID_PARAM},
);
}
const IPI_FID = enum(i32) {
SEND_IPI = 0x0,
};
comptime {
std.testing.refAllDecls(@This());
}
};
/// Any function that wishes to use range of addresses (i.e. `start_addr` and `size`), have to abide by the below
/// constraints on range parameters.
///
/// The remote fence function acts as a full TLB flush if
/// • `start_addr` and `size` are both 0
/// • `size` is equal to 2^XLEN-1
pub const rfence = struct {
pub fn available() bool {
return base.probeExtension(.RFENCE);
}
/// Instructs remote harts to execute FENCE.I instruction.
pub fn remoteFenceI(hart_mask: HartMask) error{INVALID_PARAM}!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
if (runtime_safety) {
ecall.twoArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.FENCE_I),
bit_mask,
mask_base,
error{ NOT_SUPPORTED, INVALID_PARAM },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
};
return;
}
return ecall.twoArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.FENCE_I),
bit_mask,
mask_base,
error{INVALID_PARAM},
);
}
/// Instructs the remote harts to execute one or more SFENCE.VMA instructions, covering the range of
/// virtual addresses between `start_addr` and `size`.
pub fn remoteSFenceVMA(
hart_mask: HartMask,
start_addr: usize,
size: usize,
) error{ INVALID_PARAM, INVALID_ADDRESS }!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
if (runtime_safety) {
ecall.fourArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.SFENCE_VMA),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
};
return;
}
return ecall.fourArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.SFENCE_VMA),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
error{ INVALID_PARAM, INVALID_ADDRESS },
);
}
/// Instructs the remote harts to execute one or more SFENCE.VMA instructions, covering the range of
/// virtual addresses between `start_addr` and `size`.
/// This covers only the given ASID.
pub fn remoteSFenceVMAWithASID(
hart_mask: HartMask,
start_addr: usize,
size: usize,
asid: usize,
) error{ INVALID_PARAM, INVALID_ADDRESS }!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
if (runtime_safety) {
ecall.fiveArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.SFENCE_VMA_ASID),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
@bitCast(asid),
error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
};
return;
}
return ecall.fiveArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.SFENCE_VMA_ASID),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
@bitCast(asid),
error{ INVALID_PARAM, INVALID_ADDRESS },
);
}
/// Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of
/// guest physical addresses between start and size only for the given VMID.
/// This function call is only valid for harts implementing hypervisor extension.
pub fn remoteHFenceGVMAWithVMID(
hart_mask: HartMask,
start_addr: usize,
size: usize,
vmid: usize,
) error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS }!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
return ecall.fiveArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_GVMA_VMID),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
@bitCast(vmid),
error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS },
);
}
/// Instruct the remote harts to execute one or more HFENCE.GVMA instructions, covering the range of
/// guest physical addresses between start and size only for all guests.
/// This function call is only valid for harts implementing hypervisor extension.
pub fn remoteHFenceGVMA(
hart_mask: HartMask,
start_addr: usize,
size: usize,
) error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS }!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
return ecall.fourArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_GVMA),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS },
);
}
/// Instruct the remote harts to execute one or more HFENCE.VVMA instructions, covering the range of
/// guest virtual addresses between `start_addr` and `size` for the given ASID and current VMID (in hgatp CSR) of
/// calling hart.
/// This function call is only valid for harts implementing hypervisor extension.
pub fn remoteHFenceVVMAWithASID(
hart_mask: HartMask,
start_addr: usize,
size: usize,
asid: usize,
) error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS }!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
return ecall.fiveArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_VVMA_ASID),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
@bitCast(asid),
error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS },
);
}
/// Instruct the remote harts to execute one or more HFENCE.VVMA instructions, covering the range of
/// guest virtual addresses between `start_addr` and `size` for current VMID (in hgatp CSR) of calling hart.
/// This function call is only valid for harts implementing hypervisor extension.
pub fn remoteHFenceVVMA(
hart_mask: HartMask,
start_addr: usize,
size: usize,
) error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS }!void {
var bit_mask: isize = undefined;
var mask_base: isize = undefined;
switch (hart_mask) {
.all => {
bit_mask = 0;
mask_base = 0;
},
.mask => |mask| {
bit_mask = @bitCast(mask.mask);
mask_base = @bitCast(mask.base);
},
}
return ecall.fourArgsNoReturnWithError(
.RFENCE,
@intFromEnum(RFENCE_FID.HFENCE_VVMA),
bit_mask,
mask_base,
@bitCast(start_addr),
@bitCast(size),
error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS },
);
}
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,
};
comptime {
std.testing.refAllDecls(@This());
}
};
/// 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.
pub const hsm = struct {
pub fn available() bool {
return base.probeExtension(.HSM);
}
/// Request the SBI implementation to start executing the target hart in supervisor-mode at address specified
/// by `start_addr` parameter with specific registers values described in the SBI Specification.
///
/// 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 PMP and other M-mode state before transferring control to supervisor-mode software.
///
/// The `hartid` parameter specifies the target hart which is to be started.
///
/// The `start_addr` parameter points to a runtime-specified physical address, where the hart can start
/// executing in supervisor-mode.
///
/// The `value` parameter is a XLEN-bit value which will be set in the a1 register when the hart starts
/// executing at `start_addr`.
pub fn hartStart(
hartid: usize,
start_addr: usize,
value: usize,
) error{ INVALID_ADDRESS, INVALID_PARAM, ALREADY_AVAILABLE, FAILED }!void {
if (runtime_safety) {
ecall.threeArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_START),
@bitCast(hartid),
@bitCast(start_addr),
@bitCast(value),
error{ NOT_SUPPORTED, INVALID_ADDRESS, INVALID_PARAM, ALREADY_AVAILABLE, FAILED },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
};
return;
}
return ecall.threeArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_START),
@bitCast(hartid),
@bitCast(start_addr),
@bitCast(value),
error{ INVALID_ADDRESS, INVALID_PARAM, ALREADY_AVAILABLE, FAILED },
);
}
/// Request the SBI implementation to stop executing the calling hart in supervisor-mode and return it’s
/// ownership to the SBI implementation.
/// This call is not expected to return under normal conditions.
/// `hartStop` must be called with the supervisor-mode interrupts disabled.
pub fn hartStop() error{FAILED}!void {
if (runtime_safety) {
ecall.zeroArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_STOP),
error{ NOT_SUPPORTED, FAILED },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
};
} else {
try ecall.zeroArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_STOP),
error{FAILED},
);
}
unreachable;
}
/// 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.
pub fn hartStatus(hartid: usize) error{INVALID_PARAM}!State {
if (runtime_safety) {
return @enumFromInt(ecall.oneArgsWithReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_GET_STATUS),
@bitCast(hartid),
error{ NOT_SUPPORTED, INVALID_PARAM },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
});
}
return @enumFromInt(try ecall.oneArgsWithReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_GET_STATUS),
@bitCast(hartid),
error{INVALID_PARAM},
));
}
/// 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
/// SBI suspend call return without any failures.
///
/// The `resume_addr` 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_addr` with specific registers values described
/// in the SBI Specification
///
/// The `resume_addr` parameter points to a runtime-specified physical address, where the hart can resume execution in
/// supervisor-mode after a non-retentive suspend.
///
/// The `value` parameter is a XLEN-bit value which will be set in the a1 register when the hart resumes execution at
/// `resume_addr` after a non-retentive suspend.
pub fn hartSuspend(
suspend_type: SuspendType,
resume_addr: usize,
value: usize,
) error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS, FAILED }!void {
return ecall.threeArgsNoReturnWithError(
.HSM,
@intFromEnum(HSM_FID.HART_SUSPEND),
@intCast(@intFromEnum(suspend_type)),
@bitCast(resume_addr),
@bitCast(value),
error{ NOT_SUPPORTED, INVALID_PARAM, INVALID_ADDRESS, FAILED },
);
}
pub const SuspendType = enum(u32) {
/// Default retentive suspend
RETENTIVE = 0,
/// Default non-retentive suspend
NON_RETENTIVE = 0x80000000,
_,
};
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,
};
comptime {
std.testing.refAllDecls(@This());
}
};
/// 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 machine mode firmware or hypervisor.
pub const reset = struct {
pub fn available() bool {
return base.probeExtension(.SRST);
}
/// 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 machine mode firmware.
/// In this case, shutdown is equivalent to physical power down of the entire system and cold reboot is
/// equivalent to physical power cycle of the entire system. Further, warm reboot is equivalent to a power
/// cycle of 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 a hypervisor.
/// The shutdown, cold reboot and warm reboot will behave functionally the same as the native case but
/// might not result in any physical power changes.
pub fn systemReset(
reset_type: ResetType,
reset_reason: ResetReason,
) error{ NOT_SUPPORTED, INVALID_PARAM, FAILED }!void {
try ecall.twoArgsNoReturnWithError(
.SRST,
@intFromEnum(SRST_FID.RESET),
@intCast(@intFromEnum(reset_type)),
@intCast(@intFromEnum(reset_reason)),
error{ NOT_SUPPORTED, INVALID_PARAM, FAILED },
);
unreachable;
}
pub const ResetType = enum(u32) {
SHUTDOWN = 0x0,
COLD_REBOOT = 0x1,
WARM_REBOOT = 0x2,
_,
};
pub const ResetReason = enum(u32) {
NONE = 0x0,
SYSFAIL = 0x1,
_,
};
const SRST_FID = enum(i32) {
RESET = 0x0,
};
comptime {
std.testing.refAllDecls(@This());
}
};
pub const pmu = struct {
pub fn available() bool {
return base.probeExtension(.PMU);
}
/// Returns the number of counters (both hardware and firmware)
pub fn getNumberOfCounters() usize {
if (runtime_safety) {
return @bitCast(ecall.zeroArgsWithReturnWithError(
.PMU,
@intFromEnum(PMU_FID.NUM_COUNTERS),
error{NOT_SUPPORTED},
) catch unreachable);
}
return @bitCast(ecall.zeroArgsWithReturnNoError(.PMU, @intFromEnum(PMU_FID.NUM_COUNTERS)));
}
/// Get details about the specified counter such as underlying CSR number, width of the counter, type of
/// counter hardware/firmware, etc.
pub fn getCounterInfo(counter_index: usize) error{INVALID_PARAM}!CounterInfo {
if (runtime_safety) {
return @bitCast(ecall.oneArgsWithReturnWithError(
.PMU,
@intFromEnum(PMU_FID.COUNTER_GET_INFO),
@bitCast(counter_index),
error{ NOT_SUPPORTED, INVALID_PARAM },
) catch |err| switch (err) {
error.NOT_SUPPORTED => unreachable,
else => |e| return e,
});
}
return @bitCast(try ecall.oneArgsWithReturnWithError(
.PMU,
@intFromEnum(PMU_FID.COUNTER_GET_INFO),
@bitCast(counter_index),
error{INVALID_PARAM},
));
}
/// Find and configure a counter from a set of counters which is not started (or enabled) and can monitor
/// the specified event.
pub fn configureMatchingCounter(
counter_base: usize,
counter_mask: usize,
config_flags: ConfigFlags,
event: Event,
) error{ NOT_SUPPORTED, INVALID_PARAM }!usize {
const event_data = event.toEventData();
return @bitCast(try ecall.fiveArgsLastArg64WithReturnWithError(
.PMU,
@intFromEnum(PMU_FID.COUNTER_CFG_MATCH),
@bitCast(counter_base),
@bitCast(counter_mask),
@bitCast(config_flags),
@bitCast(event_data.event_index),
event_data.event_data,
error{ NOT_SUPPORTED, INVALID_PARAM },