From b01c5a95c468650f143e0ae96f6c3865852fdcda Mon Sep 17 00:00:00 2001 From: Andrea Orru Date: Wed, 11 Apr 2018 00:31:32 -0700 Subject: [PATCH 1/6] Update zen library --- std/os/zen.zig | 67 ++++++++++++++++----------------------- std/special/bootstrap.zig | 6 ---- 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/std/os/zen.zig b/std/os/zen.zig index 51528ca391..50d53dca5f 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -7,6 +7,7 @@ pub const Message = struct { receiver: MailboxId, type: usize, payload: usize, + buffer: ?[]const u8, pub fn from(mailbox_id: &const MailboxId) Message { return Message { @@ -14,6 +15,7 @@ pub const Message = struct { .receiver = *mailbox_id, .type = 0, .payload = 0, + .buffer = null, }; } @@ -23,16 +25,26 @@ pub const Message = struct { .receiver = *mailbox_id, .type = msg_type, .payload = 0, + .buffer = null, }; } - pub fn withData(mailbox_id: &const MailboxId, msg_type: usize, payload: usize) Message { - return Message { - .sender = MailboxId.This, - .receiver = *mailbox_id, - .type = msg_type, - .payload = payload, - }; + pub fn as(self: &const Message, sender: &const MailboxId) Message { + var message = *self; + message.sender = *sender; + return message; + } + + pub fn data(self: &const Message, var_data: var) Message { + var message = *self; + + if (@canImplicitCast([]const u8, var_data)) { + message.buffer = var_data; + } else { + message.payload = var_data; + } + + return message; } }; @@ -91,10 +103,8 @@ pub fn read(fd: i32, buf: &u8, count: usize) usize { pub fn write(fd: i32, buf: &const u8, count: usize) usize { switch (fd) { STDOUT_FILENO, STDERR_FILENO => { - var i: usize = 0; - while (i < count) : (i += 1) { - send(Message.withData(Server.Terminal, 1, buf[i])); - } + send(Message.to(Server.Terminal, 1) + .data(buf[0..count])); }, else => unreachable, } @@ -108,16 +118,12 @@ pub fn write(fd: i32, buf: &const u8, count: usize) usize { pub const Syscall = enum(usize) { exit = 0, - createPort = 1, - send = 2, - receive = 3, - subscribeIRQ = 4, - inb = 5, - map = 6, - createThread = 7, - createProcess = 8, - wait = 9, - portReady = 10, + send = 1, + receive = 2, + subscribeIRQ = 3, + inb = 4, + map = 5, + createThread = 6, }; @@ -130,13 +136,6 @@ pub fn exit(status: i32) noreturn { unreachable; } -pub fn createPort(mailbox_id: &const MailboxId) void { - _ = switch (*mailbox_id) { - MailboxId.Port => |id| syscall1(Syscall.createPort, id), - else => unreachable, - }; -} - pub fn send(message: &const Message) void { _ = syscall1(Syscall.send, @ptrToInt(message)); } @@ -161,18 +160,6 @@ pub fn createThread(function: fn()void) u16 { return u16(syscall1(Syscall.createThread, @ptrToInt(function))); } -pub fn createProcess(elf_addr: usize) u16 { - return u16(syscall1(Syscall.createProcess, elf_addr)); -} - -pub fn wait(tid: u16) void { - _ = syscall1(Syscall.wait, tid); -} - -pub fn portReady(port: u16) bool { - return syscall1(Syscall.portReady, port) != 0; -} - ///////////////////////// //// Syscall stubs //// ///////////////////////// diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index d2c22c13e1..d2b1af76a7 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -13,17 +13,11 @@ comptime { @export("main", main, strong_linkage); } else if (builtin.os == builtin.Os.windows) { @export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage); - } else if (builtin.os == builtin.Os.zen) { - @export("_start", zen_start, strong_linkage); } else { @export("_start", _start, strong_linkage); } } -extern fn zen_start() noreturn { - std.os.posix.exit(@inlineCall(callMain)); -} - nakedcc fn _start() noreturn { switch (builtin.arch) { builtin.Arch.x86_64 => { From 70f2bb03fd0ac63243afd985ece7713055845363 Mon Sep 17 00:00:00 2001 From: Andrea Orru Date: Wed, 11 Apr 2018 23:11:26 -0700 Subject: [PATCH 2/6] outb syscall --- std/os/zen.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/std/os/zen.zig b/std/os/zen.zig index 50d53dca5f..c008243ec7 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -122,8 +122,9 @@ pub const Syscall = enum(usize) { receive = 2, subscribeIRQ = 3, inb = 4, - map = 5, - createThread = 6, + outb = 5, + map = 6, + createThread = 7, }; @@ -152,6 +153,10 @@ pub fn inb(port: u16) u8 { return u8(syscall1(Syscall.inb, port)); } +pub fn outb(port: u16, value: u8) void { + _ = syscall2(Syscall.outb, port, value); +} + pub fn map(v_addr: usize, p_addr: usize, size: usize, writable: bool) bool { return syscall4(Syscall.map, v_addr, p_addr, size, usize(writable)) != 0; } From c5f088e52c5f49524f79dcdceb82f13d295b7d70 Mon Sep 17 00:00:00 2001 From: Andrea Orru Date: Thu, 12 Apr 2018 22:24:57 -0700 Subject: [PATCH 3/6] Pass up to 5 arguments in Zen IPC --- std/os/zen.zig | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/std/os/zen.zig b/std/os/zen.zig index c008243ec7..b4b63f729f 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -1,3 +1,6 @@ +const std = @import("../index.zig"); +const assert = std.debug.assert; + ////////////////////////// //// IPC structures //// ////////////////////////// @@ -5,28 +8,36 @@ pub const Message = struct { sender: MailboxId, receiver: MailboxId, - type: usize, - payload: usize, - buffer: ?[]const u8, + code: usize, + args: [5]usize, + payload: ?[]const u8, pub fn from(mailbox_id: &const MailboxId) Message { return Message { .sender = MailboxId.Undefined, .receiver = *mailbox_id, - .type = 0, - .payload = 0, - .buffer = null, + .code = undefined, + .args = undefined, + .payload = null, }; } - pub fn to(mailbox_id: &const MailboxId, msg_type: usize) Message { - return Message { + pub fn to(mailbox_id: &const MailboxId, msg_code: usize, args: ...) Message { + var message = Message { .sender = MailboxId.This, .receiver = *mailbox_id, - .type = msg_type, - .payload = 0, - .buffer = null, + .code = msg_code, + .args = undefined, + .payload = null, }; + + assert (args.len <= message.args.len); + comptime var i = 0; + inline while (i < args.len) : (i += 1) { + message.args[i] = args[i]; + } + + return message; } pub fn as(self: &const Message, sender: &const MailboxId) Message { @@ -35,15 +46,9 @@ pub const Message = struct { return message; } - pub fn data(self: &const Message, var_data: var) Message { + pub fn withPayload(self: &const Message, payload: []const u8) Message { var message = *self; - - if (@canImplicitCast([]const u8, var_data)) { - message.buffer = var_data; - } else { - message.payload = var_data; - } - + message.payload = payload; return message; } }; @@ -91,7 +96,7 @@ pub fn read(fd: i32, buf: &u8, count: usize) usize { var message = Message.from(MailboxId.This); receive(&message); - buf[i] = u8(message.payload); + buf[i] = u8(message.args[0]); } }, else => unreachable, @@ -104,7 +109,7 @@ pub fn write(fd: i32, buf: &const u8, count: usize) usize { switch (fd) { STDOUT_FILENO, STDERR_FILENO => { send(Message.to(Server.Terminal, 1) - .data(buf[0..count])); + .withPayload(buf[0..count])); }, else => unreachable, } From d2c672ab0cc969f97e30cf6a12e4bffcac7cee18 Mon Sep 17 00:00:00 2001 From: Andrea Orru Date: Fri, 13 Apr 2018 11:10:36 -0700 Subject: [PATCH 4/6] FIXME note --- std/os/zen.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/std/os/zen.zig b/std/os/zen.zig index b4b63f729f..40c2c468c3 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -93,6 +93,7 @@ pub fn read(fd: i32, buf: &u8, count: usize) usize { while (i < count) : (i += 1) { send(Message.to(Server.Keyboard, 0)); + // FIXME: we should be certain that we are receiving from Keyboard. var message = Message.from(MailboxId.This); receive(&message); From 641066d82e1701d34acb81cb4bfed95405c5b506 Mon Sep 17 00:00:00 2001 From: Andrea Orru Date: Mon, 6 Aug 2018 02:29:11 -0400 Subject: [PATCH 5/6] Fix casts --- std/os/zen.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/os/zen.zig b/std/os/zen.zig index b418744e1d..ed21867f3a 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -6,7 +6,7 @@ const assert = std.debug.assert; ////////////////////////// pub const Message = struct { - sender: MailboxId, +sender: MailboxId, receiver: MailboxId, code: usize, args: [5]usize, @@ -152,7 +152,7 @@ pub fn subscribeIRQ(irq: u8, mailbox_id: *const MailboxId) void { } pub fn inb(port: u16) u8 { - return u8(syscall1(Syscall.inb, port)); + return @intCast(u8, syscall1(Syscall.inb, port)); } pub fn outb(port: u16, value: u8) void { @@ -160,7 +160,7 @@ pub fn outb(port: u16, value: u8) void { } pub fn map(v_addr: usize, p_addr: usize, size: usize, writable: bool) bool { - return syscall4(Syscall.map, v_addr, p_addr, size, usize(writable)) != 0; + return syscall4(Syscall.map, v_addr, p_addr, size, @boolToInt(writable)) != 0; } pub fn createThread(function: fn () void) u16 { From 79d77faebf4fe667cc4e2d97263df499131620b9 Mon Sep 17 00:00:00 2001 From: Andrea Orru Date: Mon, 6 Aug 2018 02:42:12 -0400 Subject: [PATCH 6/6] More type cast fixes --- std/os/zen.zig | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/std/os/zen.zig b/std/os/zen.zig index ed21867f3a..55b6d91128 100644 --- a/std/os/zen.zig +++ b/std/os/zen.zig @@ -13,8 +13,8 @@ sender: MailboxId, payload: ?[]const u8, pub fn from(mailbox_id: *const MailboxId) Message { - return Message{ - .sender = MailboxId.Undefined, + return Message { + .sender = MailboxId.Undefined, .receiver = mailbox_id.*, .code = undefined, .args = undefined, @@ -80,11 +80,15 @@ pub const STDOUT_FILENO = 1; pub const STDERR_FILENO = 2; // FIXME: let's borrow Linux's error numbers for now. -pub const getErrno = @import("linux/index.zig").getErrno; use @import("linux/errno.zig"); +// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) usize { + const signed_r = @bitCast(isize, r); + return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0; +} // TODO: implement this correctly. -pub fn read(fd: i32, buf: *u8, count: usize) usize { +pub fn read(fd: i32, buf: [*]u8, count: usize) usize { switch (fd) { STDIN_FILENO => { var i: usize = 0; @@ -93,9 +97,9 @@ pub fn read(fd: i32, buf: *u8, count: usize) usize { // FIXME: we should be certain that we are receiving from Keyboard. var message = Message.from(MailboxId.This); - receive(message.*); + receive(&message); - buf[i] = u8(message.args[0]); + buf[i] = @intCast(u8, message.args[0]); } }, else => unreachable, @@ -104,7 +108,7 @@ pub fn read(fd: i32, buf: *u8, count: usize) usize { } // TODO: implement this correctly. -pub fn write(fd: i32, buf: *const u8, count: usize) usize { +pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { switch (fd) { STDOUT_FILENO, STDERR_FILENO => { send(Message.to(Server.Terminal, 1)