aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/instructions.zig27
1 files changed, 20 insertions, 7 deletions
diff --git a/src/instructions.zig b/src/instructions.zig
index 6e4fe00..831a62c 100644
--- a/src/instructions.zig
+++ b/src/instructions.zig
@@ -2,6 +2,9 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
+const std = @import("std");
+
+const interrupts = @import("interrupts.zig");
const paging = @import("paging.zig");
pub const SbiRet = struct {
@@ -43,12 +46,22 @@ pub inline fn setStackPointer(sp: usize) void {
);
}
-pub fn setSatp(satp: paging.Satp) void {
- const value: usize = @bitCast(satp);
+pub const setSatp = setCsrFn(paging.Satp, "satp").?;
+pub const setSupervisorTrapVector = setCsrFn(interrupts.SupervisorTrapVector, "stvec").?;
- asm volatile (
- \\ csrw satp, %[value]
- :
- : [value] "r" (value),
- );
+fn setCsrFn(comptime T: type, csr: []const u8) ?fn (T) void {
+ if (csr.len > 5) return null;
+
+ return struct {
+ fn setCsr(value: T) void {
+ const bits: usize = @bitCast(value);
+
+ comptime var buf = [_]u8{0} ** 20;
+
+ asm volatile (std.fmt.bufPrint(buf[0..], "csrw {s}, %[bits]", .{csr}) catch unreachable
+ :
+ : [bits] "r" (bits),
+ );
+ }
+ }.setCsr;
}