| ... | ... | @@ -7,6 +7,7 @@ pub const Message = struct { |
| 7 | 7 | receiver: MailboxId, |
| 8 | 8 | type: usize, |
| 9 | 9 | payload: usize, |
| 10 | buffer: ?[]const u8, |
| 10 | 11 | |
| 11 | 12 | pub fn from(mailbox_id: &const MailboxId) Message { |
| 12 | 13 | return Message { |
| ... | ... | @@ -14,6 +15,7 @@ pub const Message = struct { |
| 14 | 15 | .receiver = *mailbox_id, |
| 15 | 16 | .type = 0, |
| 16 | 17 | .payload = 0, |
| 18 | .buffer = null, |
| 17 | 19 | }; |
| 18 | 20 | } |
| 19 | 21 | |
| ... | ... | @@ -23,16 +25,26 @@ pub const Message = struct { |
| 23 | 25 | .receiver = *mailbox_id, |
| 24 | 26 | .type = msg_type, |
| 25 | 27 | .payload = 0, |
| 28 | .buffer = null, |
| 26 | 29 | }; |
| 27 | 30 | } |
| 28 | 31 | |
| 29 | | pub fn withData(mailbox_id: &const MailboxId, msg_type: usize, payload: usize) Message { |
| 30 | | return Message { |
| 31 | | .sender = MailboxId.This, |
| 32 | | .receiver = *mailbox_id, |
| 33 | | .type = msg_type, |
| 34 | | .payload = payload, |
| 35 | | }; |
| 32 | pub fn as(self: &const Message, sender: &const MailboxId) Message { |
| 33 | var message = *self; |
| 34 | message.sender = *sender; |
| 35 | return message; |
| 36 | } |
| 37 | |
| 38 | pub fn data(self: &const Message, var_data: var) Message { |
| 39 | var message = *self; |
| 40 | |
| 41 | if (@canImplicitCast([]const u8, var_data)) { |
| 42 | message.buffer = var_data; |
| 43 | } else { |
| 44 | message.payload = var_data; |
| 45 | } |
| 46 | |
| 47 | return message; |
| 36 | 48 | } |
| 37 | 49 | }; |
| 38 | 50 | |
| ... | ... | @@ -91,10 +103,8 @@ pub fn read(fd: i32, buf: &u8, count: usize) usize { |
| 91 | 103 | pub fn write(fd: i32, buf: &const u8, count: usize) usize { |
| 92 | 104 | switch (fd) { |
| 93 | 105 | STDOUT_FILENO, STDERR_FILENO => { |
| 94 | | var i: usize = 0; |
| 95 | | while (i < count) : (i += 1) { |
| 96 | | send(Message.withData(Server.Terminal, 1, buf[i])); |
| 97 | | } |
| 106 | send(Message.to(Server.Terminal, 1) |
| 107 | .data(buf[0..count])); |
| 98 | 108 | }, |
| 99 | 109 | else => unreachable, |
| 100 | 110 | } |
| ... | ... | @@ -108,16 +118,12 @@ pub fn write(fd: i32, buf: &const u8, count: usize) usize { |
| 108 | 118 | |
| 109 | 119 | pub const Syscall = enum(usize) { |
| 110 | 120 | exit = 0, |
| 111 | | createPort = 1, |
| 112 | | send = 2, |
| 113 | | receive = 3, |
| 114 | | subscribeIRQ = 4, |
| 115 | | inb = 5, |
| 116 | | map = 6, |
| 117 | | createThread = 7, |
| 118 | | createProcess = 8, |
| 119 | | wait = 9, |
| 120 | | portReady = 10, |
| 121 | send = 1, |
| 122 | receive = 2, |
| 123 | subscribeIRQ = 3, |
| 124 | inb = 4, |
| 125 | map = 5, |
| 126 | createThread = 6, |
| 121 | 127 | }; |
| 122 | 128 | |
| 123 | 129 | |
| ... | ... | @@ -130,13 +136,6 @@ pub fn exit(status: i32) noreturn { |
| 130 | 136 | unreachable; |
| 131 | 137 | } |
| 132 | 138 | |
| 133 | | pub fn createPort(mailbox_id: &const MailboxId) void { |
| 134 | | _ = switch (*mailbox_id) { |
| 135 | | MailboxId.Port => |id| syscall1(Syscall.createPort, id), |
| 136 | | else => unreachable, |
| 137 | | }; |
| 138 | | } |
| 139 | | |
| 140 | 139 | pub fn send(message: &const Message) void { |
| 141 | 140 | _ = syscall1(Syscall.send, @ptrToInt(message)); |
| 142 | 141 | } |
| ... | ... | @@ -161,18 +160,6 @@ pub fn createThread(function: fn()void) u16 { |
| 161 | 160 | return u16(syscall1(Syscall.createThread, @ptrToInt(function))); |
| 162 | 161 | } |
| 163 | 162 | |
| 164 | | pub fn createProcess(elf_addr: usize) u16 { |
| 165 | | return u16(syscall1(Syscall.createProcess, elf_addr)); |
| 166 | | } |
| 167 | | |
| 168 | | pub fn wait(tid: u16) void { |
| 169 | | _ = syscall1(Syscall.wait, tid); |
| 170 | | } |
| 171 | | |
| 172 | | pub fn portReady(port: u16) bool { |
| 173 | | return syscall1(Syscall.portReady, port) != 0; |
| 174 | | } |
| 175 | | |
| 176 | 163 | ///////////////////////// |
| 177 | 164 | //// Syscall stubs //// |
| 178 | 165 | ///////////////////////// |