diff options
author | Himbeer <himbeer@disroot.org> | 2024-04-19 19:48:06 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-04-19 19:48:06 +0200 |
commit | 8451d36574fa4348c27bf9f83e0eb03f45bc5c53 (patch) | |
tree | c2b773a176bd1dee11a0ba16417ab839ef197484 | |
parent | fe1c80db9f7826967267a3533476ef3a771e7035 (diff) |
merge sbi base and error handling modules into single main module file
-rw-r--r-- | src/sbi.zig | 123 | ||||
-rw-r--r-- | src/sbi/base.zig | 94 | ||||
-rw-r--r-- | src/sbi/debug_console.zig | 13 | ||||
-rw-r--r-- | src/sbi/error.zig | 33 | ||||
-rw-r--r-- | src/sbi/sys_reset.zig | 9 |
5 files changed, 131 insertions, 141 deletions
diff --git a/src/sbi.zig b/src/sbi.zig new file mode 100644 index 0000000..2072b90 --- /dev/null +++ b/src/sbi.zig @@ -0,0 +1,123 @@ +// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +const instructions = @import("instructions.zig"); + +pub const Error = error{ + Success, + Failed, + NotSupported, + InvalidParam, + Denied, + InvalidAddr, + AlreadyAvail, + AlreadyStarted, + AlreadyStopped, + NoSharedMem, + Unknown, +}; + +pub fn errorFromCode(code: isize) Error { + return switch (code) { + 0 => Error.Success, + -1 => Error.Failed, + -2 => Error.NotSupported, + -3 => Error.InvalidParam, + -4 => Error.Denied, + -5 => Error.InvalidAddr, + -6 => Error.AlreadyAvail, + -7 => Error.AlreadyStarted, + -8 => Error.AlreadyStopped, + -9 => Error.NoSharedMem, + else => Error.Unknown, + }; +} + +const BaseExtId: usize = 0x10; + +const BaseFnId = enum(usize) { + GetSpecVer = 0, + GetImpId = 1, + GetImpVer = 2, + ProbeExt = 3, + GetMVendorId = 4, + GetMArchId = 5, + GetMImpId = 6, +}; + +pub const ImpId = enum(isize) { + Bbl = 0, + OpenSbi = 1, + Xvisor = 2, + Kvm = 3, + RustSbi = 4, + Diosix = 5, + Coffer = 6, + Xen = 7, + PolarFire = 8, + _, +}; + +pub fn specVer() !isize { + const ret = instructions.ecall(BaseExtId, @intFromEnum(BaseFnId.GetSpecVer), 0, 0, 0); + if (ret.err != 0) { + return errorFromCode(ret.err); + } + + return ret.val; +} + +pub fn impId() !ImpId { + const ret = instructions.ecall(BaseExtId, @intFromEnum(BaseFnId.GetImpId), 0, 0, 0); + if (ret.err != 0) { + return errorFromCode(ret.err); + } + + return @enumFromInt(ret.val); +} + +pub fn impVer() !isize { + const ret = instructions.ecall(BaseExtId, @intFromEnum(BaseFnId.GetImpVer), 0, 0, 0); + if (ret.err != 0) { + return errorFromCode(ret.err); + } + + return ret.val; +} + +pub fn probeExt(ext_id: usize) !bool { + const ret = instructions.ecall(BaseExtId, @intFromEnum(BaseFnId.ProbeExt), ext_id, 0, 0); + if (ret.err != 0) { + return errorFromCode(ret.err); + } + + return ret.val != 0; +} + +pub fn mVendorId() !isize { + const ret = instructions.ecall(BaseExtId, @intFromEnum(BaseFnId.GetMVendorId), 0, 0, 0); + if (ret.err != 0) { + return errorFromCode(ret.err); + } + + return ret.val; +} + +pub fn mArchId() !isize { + const ret = instructions.ecall(BaseExtId, @intFromEnum(BaseFnId.GetMarchId), 0, 0, 0); + if (ret.err != 0) { + return errorFromCode(ret.err); + } + + return ret.val; +} + +pub fn mImpId() !isize { + const ret = instructions.ecall(BaseExtId, @intFromEnum(BaseFnId.GetMImpId), 0, 0, 0); + if (ret.err != 0) { + return errorFromCode(ret.err); + } + + return ret.val; +} diff --git a/src/sbi/base.zig b/src/sbi/base.zig deleted file mode 100644 index 053d4dc..0000000 --- a/src/sbi/base.zig +++ /dev/null @@ -1,94 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org> -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -const instructions = @import("../instructions.zig"); -const sbierr = @import("error.zig"); - -const ExtId: usize = 0x10; - -const FnId = enum(usize) { - GetSpecVer = 0, - GetImpId = 1, - GetImpVer = 2, - ProbeExt = 3, - GetMVendorId = 4, - GetMArchId = 5, - GetMImpId = 6, -}; - -pub const ImpId = enum(isize) { - Bbl = 0, - OpenSbi = 1, - Xvisor = 2, - Kvm = 3, - RustSbi = 4, - Diosix = 5, - Coffer = 6, - Xen = 7, - PolarFire = 8, - _, -}; - -pub fn specVer() !isize { - const ret = instructions.ecall(ExtId, @intFromEnum(FnId.GetSpecVer), 0, 0, 0); - if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); - } - - return ret.val; -} - -pub fn impId() !ImpId { - const ret = instructions.ecall(ExtId, @intFromEnum(FnId.GetImpId), 0, 0, 0); - if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); - } - - return @enumFromInt(ret.val); -} - -pub fn impVer() !isize { - const ret = instructions.ecall(ExtId, @intFromEnum(FnId.GetImpVer), 0, 0, 0); - if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); - } - - return ret.val; -} - -pub fn probeExt(ext_id: usize) !bool { - const ret = instructions.ecall(ExtId, @intFromEnum(FnId.ProbeExt), ext_id, 0, 0); - if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); - } - - return ret.val != 0; -} - -pub fn mVendorId() !isize { - const ret = instructions.ecall(ExtId, @intFromEnum(FnId.GetMVendorId), 0, 0, 0); - if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); - } - - return ret.val; -} - -pub fn mArchId() !isize { - const ret = instructions.ecall(ExtId, @intFromEnum(FnId.GetMarchId), 0, 0, 0); - if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); - } - - return ret.val; -} - -pub fn mImpId() !isize { - const ret = instructions.ecall(ExtId, @intFromEnum(FnId.GetMImpId), 0, 0, 0); - if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); - } - - return ret.val; -} diff --git a/src/sbi/debug_console.zig b/src/sbi/debug_console.zig index 91bf24b..afd249f 100644 --- a/src/sbi/debug_console.zig +++ b/src/sbi/debug_console.zig @@ -4,11 +4,8 @@ const std = @import("std"); -const base = @import("base.zig"); const instructions = @import("../instructions.zig"); -const sbierr = @import("error.zig"); - -const SbiError = sbierr.SbiError; +const sbi = @import("../sbi.zig"); const ExtId: usize = 0x4442434E; @@ -18,20 +15,20 @@ const FnId = enum(usize) { WriteByte = 2, }; -pub const Writer = std.io.Writer(void, SbiError, write); +pub const Writer = std.io.Writer(void, sbi.Error, write); fn write(_: void, bytes: []const u8) !usize { const ret = instructions.ecall(ExtId, @intFromEnum(FnId.Write), bytes.len, @intFromPtr(bytes.ptr), 0); if (ret.err != 0) { - return sbierr.errorFromCode(ret.err); + return sbi.errorFromCode(ret.err); } return @intCast(ret.val); } pub fn writer() !Writer { - if (!try base.probeExt(ExtId)) { - return SbiError.NotSupported; + if (!try sbi.probeExt(ExtId)) { + return sbi.Error.NotSupported; } return .{ .context = {} }; diff --git a/src/sbi/error.zig b/src/sbi/error.zig deleted file mode 100644 index 5aaeb7d..0000000 --- a/src/sbi/error.zig +++ /dev/null @@ -1,33 +0,0 @@ -// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org> -// -// SPDX-License-Identifier: AGPL-3.0-or-later - -pub const SbiError = error{ - Success, - Failed, - NotSupported, - InvalidParam, - Denied, - InvalidAddr, - AlreadyAvail, - AlreadyStarted, - AlreadyStopped, - NoSharedMem, - Unknown, -}; - -pub fn errorFromCode(code: isize) SbiError { - return switch (code) { - 0 => SbiError.Success, - -1 => SbiError.Failed, - -2 => SbiError.NotSupported, - -3 => SbiError.InvalidParam, - -4 => SbiError.Denied, - -5 => SbiError.InvalidAddr, - -6 => SbiError.AlreadyAvail, - -7 => SbiError.AlreadyStarted, - -8 => SbiError.AlreadyStopped, - -9 => SbiError.NoSharedMem, - else => SbiError.Unknown, - }; -} diff --git a/src/sbi/sys_reset.zig b/src/sbi/sys_reset.zig index aa410e5..5651fba 100644 --- a/src/sbi/sys_reset.zig +++ b/src/sbi/sys_reset.zig @@ -2,11 +2,8 @@ // // SPDX-License-Identifier: AGPL-3.0-or-later -const base = @import("base.zig"); const instructions = @import("../instructions.zig"); -const sbierr = @import("error.zig"); - -const SBIError = sbierr.SBIError; +const sbi = @import("../sbi.zig"); const ExtId: usize = 0x53525354; @@ -26,8 +23,8 @@ pub const Reason = enum(u32) { }; pub fn reset(@"type": Type, reset_reason: Reason) !void { - if (!try base.probeExt(ExtId)) { - return SbiError.NotSupported; + if (!try sbi.probeExt(ExtId)) { + return sbi.Error.NotSupported; } const typeId = @intFromEnum(reset_type); |