aboutsummaryrefslogtreecommitdiff
path: root/system
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-01-29 17:21:58 +0530
committerBobby <[email protected]>2026-01-29 17:21:58 +0530
commit3f9b7d3239fd4f1c98529ae0f8bb9b84a0f61267 (patch)
tree640080e85bab782cc9fddfcc69294f02eebe3ffa /system
parenta1ab9877a149832dd5a3d5160656c9d3a5047a2a (diff)
downloadakiba-3f9b7d3239fd4f1c98529ae0f8bb9b84a0f61267.tar.xz
akiba-3f9b7d3239fd4f1c98529ae0f8bb9b84a0f61267.zip
refactor: Consolidate syscall handling by replacing individual syscall functions with a unified syscall interface
Diffstat (limited to 'system')
-rw-r--r--system/libraries/akiba/io.zig12
-rw-r--r--system/libraries/akiba/kata.zig8
-rw-r--r--system/libraries/akiba/sys.zig73
3 files changed, 39 insertions, 54 deletions
diff --git a/system/libraries/akiba/io.zig b/system/libraries/akiba/io.zig
index 6dba329..29933c5 100644
--- a/system/libraries/akiba/io.zig
+++ b/system/libraries/akiba/io.zig
@@ -25,7 +25,7 @@ pub const BOTH: u32 = 0x03;
pub const CREATE: u32 = 0x0100;
pub fn attach(path: []const u8, flags: u32) Error!FileDescriptor {
- const result = sys.syscall2(.attach, @intFromPtr(path.ptr), flags);
+ const result = sys.syscall(.attach, .{ @intFromPtr(path.ptr), flags });
if (result == @as(u64, @bitCast(@as(i64, -1)))) {
return Error.NotFound;
}
@@ -33,11 +33,11 @@ pub fn attach(path: []const u8, flags: u32) Error!FileDescriptor {
}
pub fn seal(fd: FileDescriptor) void {
- _ = sys.syscall1(.seal, fd);
+ _ = sys.syscall(.seal, .{fd});
}
pub fn view(fd: FileDescriptor, buffer: []u8) Error!usize {
- const result = sys.syscall3(.view, fd, @intFromPtr(buffer.ptr), buffer.len);
+ const result = sys.syscall(.view, .{ fd, @intFromPtr(buffer.ptr), buffer.len });
if (result == @as(u64, @bitCast(@as(i64, -1)))) {
return Error.ReadFailed;
}
@@ -45,7 +45,7 @@ pub fn view(fd: FileDescriptor, buffer: []u8) Error!usize {
}
pub fn mark(fd: FileDescriptor, data: []const u8, color: u32) Error!usize {
- const result = sys.syscall4(.mark, fd, @intFromPtr(data.ptr), data.len, color);
+ const result = sys.syscall(.mark, .{ fd, @intFromPtr(data.ptr), data.len, color });
if (result == @as(u64, @bitCast(@as(i64, -1)))) {
return Error.WriteFailed;
}
@@ -55,7 +55,7 @@ pub fn mark(fd: FileDescriptor, data: []const u8, color: u32) Error!usize {
pub fn getchar() !u8 {
// Loop until character is available
while (true) {
- const result = sys.syscall0(.getkeychar);
+ const result = sys.syscall(.getkeychar, .{});
if (result != @as(u64, @bitCast(@as(i64, -2)))) {
if (result != @as(u64, @bitCast(@as(i64, -1)))) {
return @truncate(result);
@@ -77,7 +77,7 @@ pub fn println(text: []const u8) Error!void {
}
pub fn viewstack(path: []const u8, entries: []StackEntry) Error!usize {
- const result = sys.syscall4(.viewstack, @intFromPtr(path.ptr), path.len, @intFromPtr(entries.ptr), entries.len);
+ const result = sys.syscall(.viewstack, .{ @intFromPtr(path.ptr), path.len, @intFromPtr(entries.ptr), entries.len });
if (result == @as(u64, @bitCast(@as(i64, -1)))) {
return Error.ReadFailed;
}
diff --git a/system/libraries/akiba/kata.zig b/system/libraries/akiba/kata.zig
index 7345532..1e7cb11 100644
--- a/system/libraries/akiba/kata.zig
+++ b/system/libraries/akiba/kata.zig
@@ -3,16 +3,16 @@
const sys = @import("sys.zig");
pub fn yield() void {
- _ = sys.syscall0(.yield);
+ _ = sys.syscall(.yield, .{});
}
pub fn exit(code: u64) noreturn {
- _ = sys.syscall1(.exit, code);
+ _ = sys.syscall(.exit, .{code});
unreachable;
}
pub fn spawn(path: []const u8) !u32 {
- const result = sys.syscall2(.spawn, @intFromPtr(path.ptr), path.len);
+ const result = sys.syscall(.spawn, .{ @intFromPtr(path.ptr), path.len });
if (result == @as(u64, @bitCast(@as(i64, -1)))) {
return error.SpawnFailed;
}
@@ -22,7 +22,7 @@ pub fn spawn(path: []const u8) !u32 {
pub fn wait(pid: u32) !u64 {
// Retry loop: keep checking until the target exits
while (true) {
- const result = sys.syscall1(.wait, pid);
+ const result = sys.syscall(.wait, .{pid});
if (result != @as(u64, @bitCast(@as(i64, -1)))) {
return result;
}
diff --git a/system/libraries/akiba/sys.zig b/system/libraries/akiba/sys.zig
index 796a09a..a79e19d 100644
--- a/system/libraries/akiba/sys.zig
+++ b/system/libraries/akiba/sys.zig
@@ -13,57 +13,42 @@ pub const Invocation = enum(u64) {
viewstack = 0x0A,
};
-pub fn syscall0(inv: Invocation) u64 {
- var result: u64 = undefined;
- asm volatile ("syscall"
- : [ret] "={rax}" (result),
- : [inv] "{rax}" (@intFromEnum(inv)),
- : .{ .rcx = true, .r11 = true, .memory = true });
- return result;
-}
+/// System call interface - handles variable parameter counts
+/// Always sets all 6 parameter registers (unused ones are zero)
+pub fn syscall(inv: Invocation, params: anytype) u64 {
+ const params_type = @typeInfo(@TypeOf(params));
-pub fn syscall1(inv: Invocation, arg1: u64) u64 {
- var result: u64 = undefined;
- asm volatile ("syscall"
- : [ret] "={rax}" (result),
- : [inv] "{rax}" (@intFromEnum(inv)),
- [arg1] "{rdi}" (arg1),
- : .{ .rcx = true, .r11 = true, .memory = true });
- return result;
-}
+ if (params_type != .@"struct") {
+ @compileError("syscall params must be a tuple");
+ }
-pub fn syscall2(inv: Invocation, arg1: u64, arg2: u64) u64 {
- var result: u64 = undefined;
- asm volatile ("syscall"
- : [ret] "={rax}" (result),
- : [inv] "{rax}" (@intFromEnum(inv)),
- [arg1] "{rdi}" (arg1),
- [arg2] "{rsi}" (arg2),
- : .{ .rcx = true, .r11 = true, .memory = true });
- return result;
-}
+ const fields = params_type.@"struct".fields;
-pub fn syscall3(inv: Invocation, arg1: u64, arg2: u64, arg3: u64) u64 {
- var result: u64 = undefined;
- asm volatile ("syscall"
- : [ret] "={rax}" (result),
- : [inv] "{rax}" (@intFromEnum(inv)),
- [arg1] "{rdi}" (arg1),
- [arg2] "{rsi}" (arg2),
- [arg3] "{rdx}" (arg3),
- : .{ .rcx = true, .r11 = true, .memory = true });
- return result;
-}
+ // Convert parameters to u64 at comptime
+ var param_values: [6]u64 = .{0} ** 6;
+
+ inline for (fields, 0..) |field, i| {
+ if (i >= 6) @compileError("Maximum 6 syscall parameters");
+
+ param_values[i] = switch (@typeInfo(field.type)) {
+ .int, .comptime_int => @intCast(@field(params, field.name)),
+ .pointer => @intFromPtr(@field(params, field.name)),
+ .@"enum" => @intFromEnum(@field(params, field.name)),
+ else => @compileError("Unsupported syscall parameter type"),
+ };
+ }
-pub fn syscall4(inv: Invocation, arg1: u64, arg2: u64, arg3: u64, arg4: u64) u64 {
+ // Always set all 6 registers
var result: u64 = undefined;
asm volatile ("syscall"
: [ret] "={rax}" (result),
- : [inv] "{rax}" (@intFromEnum(inv)),
- [arg1] "{rdi}" (arg1),
- [arg2] "{rsi}" (arg2),
- [arg3] "{rdx}" (arg3),
- [arg4] "{r10}" (arg4),
+ : [num] "{rax}" (@intFromEnum(inv)),
+ [p0] "{rdi}" (param_values[0]),
+ [p1] "{rsi}" (param_values[1]),
+ [p2] "{rdx}" (param_values[2]),
+ [p3] "{r10}" (param_values[3]),
+ [p4] "{r8}" (param_values[4]),
+ [p5] "{r9}" (param_values[5]),
: .{ .rcx = true, .r11 = true, .memory = true });
return result;
}