aboutsummaryrefslogtreecommitdiff
path: root/system
diff options
context:
space:
mode:
authorBobby <[email protected]>2025-12-30 13:17:40 +0530
committerBobby <[email protected]>2025-12-30 13:17:40 +0530
commitd711d48317ad9fde23b9eefed1154cad0c4f32d7 (patch)
treefed0a9cf91f9c8c64942d24a03917117b8cfc1b5 /system
parent343e8dcd2bc11e5e01129bcc9c52f4e6feddc50b (diff)
downloadakiba-d711d48317ad9fde23b9eefed1154cad0c4f32d7.tar.xz
akiba-d711d48317ad9fde23b9eefed1154cad0c4f32d7.zip
refactor: reorganize library build process; add syscall interface and I/O operations
Diffstat (limited to 'system')
-rw-r--r--system/libraries/akiba/akiba.zig5
-rw-r--r--system/libraries/akiba/akiba.zon6
-rw-r--r--system/libraries/akiba/io.zig61
-rw-r--r--system/libraries/akiba/kata.zig24
-rw-r--r--system/libraries/akiba/sys.zig44
-rw-r--r--system/libraries/build.zig35
6 files changed, 175 insertions, 0 deletions
diff --git a/system/libraries/akiba/akiba.zig b/system/libraries/akiba/akiba.zig
new file mode 100644
index 0000000..fc7202b
--- /dev/null
+++ b/system/libraries/akiba/akiba.zig
@@ -0,0 +1,5 @@
+//! Akiba Runtime - Core system library
+
+pub const kata = @import("kata.zig");
+pub const io = @import("io.zig");
+pub const sys = @import("sys.zig");
diff --git a/system/libraries/akiba/akiba.zon b/system/libraries/akiba/akiba.zon
new file mode 100644
index 0000000..aad4a03
--- /dev/null
+++ b/system/libraries/akiba/akiba.zon
@@ -0,0 +1,6 @@
+.{
+ .name = "akiba",
+ .version = "1.0.0",
+ .type = .library,
+ .entry = "akiba.zig",
+}
diff --git a/system/libraries/akiba/io.zig b/system/libraries/akiba/io.zig
new file mode 100644
index 0000000..b8f8eef
--- /dev/null
+++ b/system/libraries/akiba/io.zig
@@ -0,0 +1,61 @@
+//! File I/O operations
+
+const sys = @import("sys.zig");
+
+pub const Error = error{
+ NotFound,
+ PermissionDenied,
+ InvalidDescriptor,
+ ReadFailed,
+ WriteFailed,
+};
+
+pub const FileDescriptor = u32;
+
+// Standard file descriptors
+pub const source: FileDescriptor = 0; // Input stream
+pub const stream: FileDescriptor = 1; // Output stream
+pub const trace: FileDescriptor = 2; // Error/trace stream
+
+// Open modes
+pub const VIEW_ONLY: u32 = 0x01;
+pub const MARK_ONLY: u32 = 0x02;
+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);
+ if (result == @as(u64, @bitCast(@as(i64, -1)))) {
+ return Error.NotFound;
+ }
+ return @truncate(result);
+}
+
+pub fn seal(fd: FileDescriptor) void {
+ _ = sys.syscall1(.seal, fd);
+}
+
+pub fn view(fd: FileDescriptor, buffer: []u8) Error!usize {
+ const result = sys.syscall3(.view, fd, @intFromPtr(buffer.ptr), buffer.len);
+ if (result == @as(u64, @bitCast(@as(i64, -1)))) {
+ return Error.ReadFailed;
+ }
+ return result;
+}
+
+pub fn mark(fd: FileDescriptor, data: []const u8) Error!usize {
+ const result = sys.syscall3(.mark, fd, @intFromPtr(data.ptr), data.len);
+ if (result == @as(u64, @bitCast(@as(i64, -1)))) {
+ return Error.WriteFailed;
+ }
+ return result;
+}
+
+pub fn print(text: []const u8) Error!void {
+ _ = try mark(stream, text);
+}
+
+pub fn println(text: []const u8) Error!void {
+ _ = try mark(stream, text);
+ _ = try mark(stream, "\n");
+}
diff --git a/system/libraries/akiba/kata.zig b/system/libraries/akiba/kata.zig
new file mode 100644
index 0000000..3d1ccef
--- /dev/null
+++ b/system/libraries/akiba/kata.zig
@@ -0,0 +1,24 @@
+//! Kata (process) control functions
+
+const sys = @import("sys.zig");
+
+pub fn exit(code: u64) noreturn {
+ _ = sys.syscall1(.exit, code);
+ unreachable;
+}
+
+pub fn spawn(path: []const u8) !u32 {
+ const result = sys.syscall1(.spawn, @intFromPtr(path.ptr));
+ if (result == @as(u64, @bitCast(@as(i64, -1)))) {
+ return error.SpawnFailed;
+ }
+ return @truncate(result);
+}
+
+pub fn wait(pid: u32) !u64 {
+ const result = sys.syscall1(.wait, pid);
+ if (result == @as(u64, @bitCast(@as(i64, -1)))) {
+ return error.WaitFailed;
+ }
+ return result;
+}
diff --git a/system/libraries/akiba/sys.zig b/system/libraries/akiba/sys.zig
new file mode 100644
index 0000000..bf0eec4
--- /dev/null
+++ b/system/libraries/akiba/sys.zig
@@ -0,0 +1,44 @@
+//! Low-level syscall interface
+
+pub const Invocation = enum(u64) {
+ exit = 0x01,
+ attach = 0x02,
+ seal = 0x03,
+ view = 0x04,
+ mark = 0x05,
+ spawn = 0x06,
+ wait = 0x07,
+};
+
+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),
+ : .{ .memory = true });
+ return result;
+}
+
+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),
+ : .{ .memory = true });
+ return result;
+}
+
+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),
+ : .{ .memory = true });
+ return result;
+}
diff --git a/system/libraries/build.zig b/system/libraries/build.zig
new file mode 100644
index 0000000..a38cbc3
--- /dev/null
+++ b/system/libraries/build.zig
@@ -0,0 +1,35 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+ const target = b.resolveTargetQuery(.{
+ .cpu_arch = .x86_64,
+ .os_tag = .freestanding,
+ .abi = .none,
+ });
+
+ var lib_dir = std.fs.cwd().openDir(".", .{ .iterate = true }) catch return;
+ defer lib_dir.close();
+
+ var lib_iter = lib_dir.iterate();
+ while (lib_iter.next() catch null) |entry| {
+ if (entry.kind != .directory) continue;
+
+ const lib_name = entry.name;
+ const lib_file = std.fmt.allocPrint(b.allocator, "{s}/{s}.zig", .{ lib_name, lib_name }) catch continue;
+ defer b.allocator.free(lib_file);
+
+ std.fs.cwd().access(lib_file, .{}) catch continue;
+
+ const lib = b.addLibrary(.{
+ .name = lib_name,
+ .root_module = b.createModule(.{
+ .root_source_file = b.path(lib_file),
+ .target = target,
+ .optimize = .ReleaseSmall,
+ }),
+ });
+
+ lib.linkage = .static;
+ b.installArtifact(lib);
+ }
+}