WIP: Make userspace a bit more useful

This commit is contained in:
2025-03-26 21:57:18 +02:00
parent 3c27839bff
commit 0f157caf5c
7 changed files with 96 additions and 58 deletions
+1 -1
View File
@@ -327,7 +327,7 @@ pub fn test_create_user_from_code(a: *arena.Arena, code: []const u8) Thread.Erro
log.info("Enter with sp = 0x{x}", .{sp});
const thread = Thread.create_user(a, address_space, 0x200000, sp, 0);
const thread = Thread.create_user(a, address_space, 0x200000, sp, 1234);
return thread;
}
+9
View File
@@ -0,0 +1,9 @@
const builtin = @import("builtin");
const impl = switch (builtin.cpu.arch) {
.riscv64 => @import("arch/riscv64.zig"),
.aarch64 => @import("arch/aarch64.zig"),
else => @compileError("Unsupported architecture"),
};
pub const syscall = impl.syscall;
+20
View File
@@ -0,0 +1,20 @@
pub const syscall = struct {
pub fn syscall1(func: usize, arg0: usize) usize {
return asm volatile ("svc #0"
: [result] "={x0}" (-> usize),
: [arg0] "{x0}" (arg0),
[func] "{x8}" (func),
: "memory"
);
}
pub fn syscall2(func: usize, arg0: usize, arg1: usize) usize {
return asm volatile ("svc #0"
: [result] "={x0}" (-> usize),
: [arg0] "{x0}" (arg0),
[arg1] "{x1}" (arg1),
[func] "{x8}" (func),
: "memory"
);
}
};
+20
View File
@@ -0,0 +1,20 @@
pub const syscall = struct {
pub fn syscall1(func: usize, arg0: usize) usize {
return asm volatile ("ecall"
: [result] "={a0}" (-> usize),
: [func] "{a0}" (func),
[arg0] "{a1}" (arg0),
: "memory"
);
}
pub fn syscall2(func: usize, arg0: usize, arg1: usize) usize {
return asm volatile ("ecall"
: [result] "={a0}" (-> usize),
: [func] "{a0}" (func),
[arg0] "{a1}" (arg0),
[arg1] "{a2}" (arg1),
: "memory"
);
}
};
+28
View File
@@ -0,0 +1,28 @@
const std = @import("std");
const syscall = @import("syscall.zig");
pub const BUFFER_SIZE: usize = 512;
pub const Writer = struct {
buffer: [BUFFER_SIZE]u8 = undefined,
position: usize = 0,
fn write(self: *Writer, bytes: []const u8) error{}!usize {
const amount = @min(BUFFER_SIZE - self.position, bytes.len);
if (amount != 0) {
@memcpy(self.buffer[self.position .. self.position + amount], bytes[0..amount]);
self.position += amount;
}
return amount;
}
};
pub fn println(comptime format: []const u8, args: anytype) void {
const W = std.io.GenericWriter(*Writer, error{}, Writer.write);
var context = Writer{};
var w = W{ .context = &context };
w.print(format, args) catch return;
if (context.position != 0) {
syscall.debug_print(context.buffer[0..context.position]);
}
}
+6 -57
View File
@@ -1,59 +1,8 @@
const builtin = @import("builtin");
pub const arch = @import("arch.zig");
pub const syscall = @import("syscall.zig");
pub const log = @import("log.zig");
const syscall = switch (builtin.cpu.arch) {
.aarch64 => struct {
pub fn syscall1(func: usize, arg0: usize) usize {
return asm volatile ("svc #0"
: [result] "={x0}" (-> usize),
: [arg0] "{x0}" (arg0),
[func] "{x8}" (func),
: "memory"
);
}
pub fn syscall2(func: usize, arg0: usize, arg1: usize) usize {
return asm volatile ("svc #0"
: [result] "={x0}" (-> usize),
: [arg0] "{x0}" (arg0),
[arg1] "{x1}" (arg1),
[func] "{x8}" (func),
: "memory"
);
}
},
.riscv64 => struct {
pub fn syscall1(func: usize, arg0: usize) usize {
return asm volatile ("ecall"
: [result] "={a0}" (-> usize),
: [func] "{a0}" (func),
[arg0] "{a1}" (arg0),
: "memory"
);
}
pub fn syscall2(func: usize, arg0: usize, arg1: usize) usize {
return asm volatile ("ecall"
: [result] "={a0}" (-> usize),
: [func] "{a0}" (func),
[arg0] "{a1}" (arg0),
[arg1] "{a2}" (arg1),
: "memory"
);
}
},
else => @compileError("Unsupported architecture"),
};
fn exit(code: u32) noreturn {
_ = syscall.syscall1(2, code);
unreachable;
}
fn debug_print(text: []const u8) void {
_ = syscall.syscall2(1, @intFromPtr(text.ptr), text.len);
}
export fn _start() linksection(".text.entry") callconv(.C) noreturn {
debug_print("Hello!");
exit(0);
export fn _start(arg: usize) linksection(".text.entry") callconv(.C) noreturn {
log.println("arg=0x{x} ({})", .{ arg, arg });
syscall.exit(0);
}
+12
View File
@@ -0,0 +1,12 @@
const arch = @import("arch.zig");
const sc = arch.syscall;
pub fn exit(code: u32) noreturn {
_ = sc.syscall1(2, code);
unreachable;
}
pub fn debug_print(text: []const u8) void {
_ = sc.syscall2(1, @intFromPtr(text.ptr), text.len);
}