aboutsummaryrefslogtreecommitdiff
path: root/system/libraries/sys
diff options
context:
space:
mode:
Diffstat (limited to 'system/libraries/sys')
-rw-r--r--system/libraries/sys/start.zig16
-rw-r--r--system/libraries/sys/sys.zig58
-rw-r--r--system/libraries/sys/sys.zon6
3 files changed, 80 insertions, 0 deletions
diff --git a/system/libraries/sys/start.zig b/system/libraries/sys/start.zig
new file mode 100644
index 0000000..a65eec8
--- /dev/null
+++ b/system/libraries/sys/start.zig
@@ -0,0 +1,16 @@
+//! Entry point
+
+extern fn main(pc: u32, pv: [*]const [*:0]const u8) u8;
+
+export fn _start() callconv(.naked) noreturn {
+ asm volatile (
+ \\mov (%%rsp), %%edi
+ \\mov 8(%%rsp), %%rsi
+ \\and $-16, %%rsp
+ \\call main
+ \\movzbl %%al, %%edi
+ \\mov $0x01, %%eax
+ \\syscall
+ \\ud2
+ );
+}
diff --git a/system/libraries/sys/sys.zig b/system/libraries/sys/sys.zig
new file mode 100644
index 0000000..d37e4c8
--- /dev/null
+++ b/system/libraries/sys/sys.zig
@@ -0,0 +1,58 @@
+//! System runtime
+
+pub const start = @import("start.zig");
+
+comptime {
+ _ = start;
+}
+
+pub const Invocation = enum(u64) {
+ exit = 0x01,
+ attach = 0x02,
+ seal = 0x03,
+ view = 0x04,
+ mark = 0x05,
+ spawn = 0x06,
+ wait = 0x07,
+ yield = 0x08,
+ getkeychar = 0x09,
+ viewstack = 0x0A,
+ getlocation = 0x0B,
+ setlocation = 0x0C,
+ postman = 0x0D,
+ wipe = 0x0E,
+};
+
+pub inline fn syscall(invocation: Invocation, args: anytype) u64 {
+ const Args = @TypeOf(args);
+ const fields = @typeInfo(Args).@"struct".fields;
+
+ const arg0: u64 = if (fields.len > 0) toU64(args[0]) else 0;
+ const arg1: u64 = if (fields.len > 1) toU64(args[1]) else 0;
+ const arg2: u64 = if (fields.len > 2) toU64(args[2]) else 0;
+ const arg3: u64 = if (fields.len > 3) toU64(args[3]) else 0;
+ const arg4: u64 = if (fields.len > 4) toU64(args[4]) else 0;
+ const arg5: u64 = if (fields.len > 5) toU64(args[5]) else 0;
+
+ return asm volatile ("syscall"
+ : [ret] "={rax}" (-> u64),
+ : [number] "{rax}" (@intFromEnum(invocation)),
+ [arg0] "{rdi}" (arg0),
+ [arg1] "{rsi}" (arg1),
+ [arg2] "{rdx}" (arg2),
+ [arg3] "{r10}" (arg3),
+ [arg4] "{r8}" (arg4),
+ [arg5] "{r9}" (arg5),
+ : .{ .rcx = true, .r11 = true, .memory = true }
+ );
+}
+
+inline fn toU64(value: anytype) u64 {
+ const T = @TypeOf(value);
+ return switch (@typeInfo(T)) {
+ .int, .comptime_int => @intCast(value),
+ .pointer => @intFromPtr(value),
+ .@"enum" => @intFromEnum(value),
+ else => @bitCast(value),
+ };
+}
diff --git a/system/libraries/sys/sys.zon b/system/libraries/sys/sys.zon
new file mode 100644
index 0000000..35d065e
--- /dev/null
+++ b/system/libraries/sys/sys.zon
@@ -0,0 +1,6 @@
+.{
+ .name = "sys",
+ .version = "1.0.0",
+ .type = .library,
+ .entry = "sys.zig",
+}