diff options
author | Himbeer <himbeer@disroot.org> | 2024-07-27 13:01:52 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-07-27 13:01:52 +0200 |
commit | 44793ab563463de8ce8ee28f9114000e04d63ebb (patch) | |
tree | f2fd3a07e9c8d39e7e58d5205d83f64131ccb1a8 /src | |
parent | df6bad0f5b00f0c764d608792ad767998f557b86 (diff) |
syscall: Convert VFS hook /process/create => launch(bytes: [*]align(@alignOf(std.elf.Elf64_Ehdr)) const u8, len: usize) !usize (#60)
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/resources.zig | 152 | ||||
-rw-r--r-- | src/lib/syscall.zig | 16 |
2 files changed, 16 insertions, 152 deletions
diff --git a/src/lib/resources.zig b/src/lib/resources.zig index 315fe57..7c6ca19 100644 --- a/src/lib/resources.zig +++ b/src/lib/resources.zig @@ -8,7 +8,6 @@ const Console = @import("Console.zig"); const instructions = @import("instructions.zig"); const mem = @import("mem.zig"); const paging = @import("paging.zig"); -const process = @import("process.zig"); const syscall = @import("syscall.zig"); const sysexchange = @import("sysexchange.zig"); const userinit = @import("userinit.zig"); @@ -51,142 +50,6 @@ const iofs = struct { }; }; -const processfs = struct { - const CreationContext = struct { - rd: vfs.ResourceDescriptor, - buffer: std.ArrayListAligned(u8, paging.page_size), - proc: *process.Info, - }; - - fn create(pid: u16, thread_id: usize, data: usize) callconv(.C) Result(usize) { - const result = doCreate(pid, thread_id, data); - return Result(usize).fromAnyTypeOrError(result); - } - - fn doCreate(pid: u16, thread_id: usize, data: usize) !usize { - paging.setUserMemoryAccess(true); - defer paging.setUserMemoryAccess(false); - - const path_c: [*:0]const u8 = @ptrFromInt(data); // fixme: Kernel panic if null pointer - - const allocator = mem.page_allocator; - - const rd = try vfs.openNonHookZ(path_c); - defer rd.deinit(); - - if (rd.inode.resource.tag == .dir or rd.inode.resource.tag == .dir_hook) { - return vfs.Error.IsAContainer; - } - - const proc = process.findThread(pid, thread_id).?; - - var buffer = std.ArrayListAligned(u8, paging.page_size).init(allocator); - defer buffer.clearAndFree(); - - const ctx = try allocator.create(CreationContext); - defer allocator.destroy(ctx); - - ctx.* = .{ - .rd = rd, - .buffer = buffer, - .proc = proc, - }; - - paging.setUserMemoryAccess(false); - - defer proc.allowResume(); - - try ctx.buffer.ensureUnusedCapacity(4096); - - var unused_capacity = ctx.buffer.allocatedSlice()[ctx.buffer.items.len..]; - var n = try rd.readHooked(proc, unused_capacity, .{ - .hookFn = loadExe, - .context = ctx, - }); - - ctx.buffer.items.len += n; - - while (n > 0) { - try buffer.ensureUnusedCapacity(4096); - - unused_capacity = ctx.buffer.allocatedSlice()[ctx.buffer.items.len..]; - n = try rd.readHooked(proc, unused_capacity, .{ - .hookFn = loadExe, - .context = ctx, - }); - - ctx.buffer.items.len += n; - } - - const new_proc = try process.create(allocator, ctx.buffer.items); - return new_proc.id; - } - - fn loadExe(context: *anyopaque, driver: *const process.Info) void { - const ctx: *CreationContext = @alignCast(@ptrCast(context)); - doLoadExe(ctx, driver) catch |err| { - sysexchange.frameReturn(usize, &ctx.proc.trap_frame, err); - }; - } - - fn doLoadExe(ctx: *CreationContext, driver: *const process.Info) !void { - const allocator = mem.page_allocator; - - defer ctx.proc.allowResume(); - - const result: Result(usize) = .{ - .value = driver.trap_frame.general_purpose_registers[12], - .status = @enumFromInt(driver.trap_frame.general_purpose_registers[13]), - }; - if (result.status != .success) { - defer allocator.destroy(ctx); - defer ctx.buffer.clearAndFree(); - defer ctx.rd.deinit(); - - sysexchange.frameReturnResult(usize, &ctx.proc.trap_frame, result); - return; - } - - const n = result.value; - if (n == 0) { - defer allocator.destroy(ctx); - defer ctx.buffer.clearAndFree(); - defer ctx.rd.deinit(); - - const new_proc = try process.create(allocator, ctx.buffer.items); - sysexchange.frameReturn(null, &ctx.proc.trap_frame, new_proc.id); - return; - } - - ctx.buffer.items.len += n; - - try ctx.buffer.ensureUnusedCapacity(4096); - - var unused_capacity = ctx.buffer.allocatedSlice()[ctx.buffer.items.len..]; - var n2 = try ctx.rd.readHooked(ctx.proc, unused_capacity, .{ - .hookFn = loadExe, - .context = ctx, - }); - - ctx.buffer.items.len += n; - - while (n2 > 0) { - try ctx.buffer.ensureUnusedCapacity(4096); - - unused_capacity = ctx.buffer.allocatedSlice()[ctx.buffer.items.len..]; - n2 = try ctx.rd.readHooked(ctx.proc, unused_capacity, .{ - .hookFn = loadExe, - .context = ctx, - }); - - ctx.buffer.items.len += n2; - } - - const new_proc = try process.create(allocator, ctx.buffer.items); - sysexchange.frameReturn(null, &ctx.proc.trap_frame, new_proc.id); - } -}; - pub const Error = error{ NoTarFileInitializer, NoConsole, @@ -195,11 +58,9 @@ pub const Error = error{ pub fn provideBuiltin(allocator: std.mem.Allocator) !void { try addDir("/io"); try addDir("/userinit"); - try addDir("/process"); try provideConsole(); try provideUserinit(allocator); - try provideProcess(); } fn provideConsole() !void { @@ -239,19 +100,6 @@ fn provideUserinit(allocator: std.mem.Allocator) !void { } } -fn provideProcess() !void { - try vfs.provideResource("/process/create", .{ - .tag = .hook, - .data = .{ - .hook = .{ - .callback = processfs.create, - }, - }, - }, 0, .{ - .reclaimable = false, - }); -} - fn addFile(path: []const u8, file: File) !void { const allocator = vfs.treeRoot().allocator; diff --git a/src/lib/syscall.zig b/src/lib/syscall.zig index 0767fbd..9841675 100644 --- a/src/lib/syscall.zig +++ b/src/lib/syscall.zig @@ -5,6 +5,7 @@ const std = @import("std"); const instructions = @import("instructions.zig"); +const mem = @import("mem.zig"); const paging = @import("paging.zig"); const process = @import("process.zig"); const sysexchange = @import("sysexchange.zig"); @@ -37,6 +38,7 @@ pub fn handler(proc: *process.Info, trap_frame: *trap.Frame) !void { 100012 => ret(null, trap_frame, terminate(proc)), 100013 => ret(null, trap_frame, processId(proc)), 100014 => ret(null, trap_frame, threadId(proc)), + 100015 => ret(null, trap_frame, launch(trap_frame)), else => return HandleError.UnknownSyscall, } } @@ -321,3 +323,17 @@ fn processId(proc: *const process.Info) usize { fn threadId(proc: *const process.Info) usize { return proc.thread_id; } + +// launch(bytes: [*]align(@alignOf(std.elf.Elf64_Ehdr)) const u8, len: usize) !usize +fn launch(trap_frame: *const trap.Frame) !usize { + const alignment = @alignOf(std.elf.Elf64_Ehdr); + const bytes_addr = trap_frame.general_purpose_registers[10]; + // fixme: Kernel panic if null pointer + const bytes_ptr: [*]align(alignment) const u8 = @ptrFromInt(bytes_addr); + const len = trap_frame.general_purpose_registers[11]; + + const bytes = bytes_ptr[0..len]; + + const new_proc = try process.create(mem.page_allocator, bytes); + return new_proc.id; +} |