| ... | ... | @@ -1,20 +1,121 @@ |
| 1 | | ////////////////////////////// |
| 2 | | //// Reserved mailboxes //// |
| 3 | | ////////////////////////////// |
| 1 | ////////////////////////// |
| 2 | //// IPC structures //// |
| 3 | ////////////////////////// |
| 4 | |
| 5 | pub const Message = struct { |
| 6 | sender: MailboxId, |
| 7 | receiver: MailboxId, |
| 8 | type: usize, |
| 9 | payload: usize, |
| 10 | |
| 11 | pub fn from(mailbox_id: &const MailboxId) Message { |
| 12 | return Message { |
| 13 | .sender = MailboxId.Undefined, |
| 14 | .receiver = *mailbox_id, |
| 15 | .type = 0, |
| 16 | .payload = 0, |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | pub fn to(mailbox_id: &const MailboxId, msg_type: usize) Message { |
| 21 | return Message { |
| 22 | .sender = MailboxId.This, |
| 23 | .receiver = *mailbox_id, |
| 24 | .type = msg_type, |
| 25 | .payload = 0, |
| 26 | }; |
| 27 | } |
| 28 | |
| 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 | }; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | pub const MailboxId = union(enum) { |
| 40 | Undefined, |
| 41 | This, |
| 42 | Kernel, |
| 43 | Port: u16, |
| 44 | Thread: u16, |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | ////////////////////////////////////// |
| 49 | //// Ports reserved for servers //// |
| 50 | ////////////////////////////////////// |
| 51 | |
| 52 | pub const Server = struct { |
| 53 | pub const Keyboard = MailboxId { .Port = 0 }; |
| 54 | pub const Terminal = MailboxId { .Port = 1 }; |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | //////////////////////// |
| 59 | //// POSIX things //// |
| 60 | //////////////////////// |
| 61 | |
| 62 | // Standard streams. |
| 63 | pub const STDIN_FILENO = 0; |
| 64 | pub const STDOUT_FILENO = 1; |
| 65 | pub const STDERR_FILENO = 2; |
| 66 | |
| 67 | // FIXME: let's borrow Linux's error numbers for now. |
| 68 | pub const getErrno = @import("linux/index.zig").getErrno; |
| 69 | use @import("linux/errno.zig"); |
| 70 | |
| 71 | // TODO: implement this correctly. |
| 72 | pub fn read(fd: i32, buf: &u8, count: usize) usize { |
| 73 | switch (fd) { |
| 74 | STDIN_FILENO => { |
| 75 | var i: usize = 0; |
| 76 | while (i < count) : (i += 1) { |
| 77 | send(Message.to(Server.Keyboard, 0)); |
| 78 | |
| 79 | var message = Message.from(MailboxId.This); |
| 80 | receive(&message); |
| 81 | |
| 82 | buf[i] = u8(message.payload); |
| 83 | } |
| 84 | }, |
| 85 | else => unreachable, |
| 86 | } |
| 87 | return count; |
| 88 | } |
| 4 | 89 | |
| 5 | | pub const MBOX_TERMINAL = 1; |
| 90 | // TODO: implement this correctly. |
| 91 | pub fn write(fd: i32, buf: &const u8, count: usize) usize { |
| 92 | switch (fd) { |
| 93 | 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 | } |
| 98 | }, |
| 99 | else => unreachable, |
| 100 | } |
| 101 | return count; |
| 102 | } |
| 6 | 103 | |
| 7 | 104 | |
| 8 | 105 | /////////////////////////// |
| 9 | 106 | //// Syscall numbers //// |
| 10 | 107 | /////////////////////////// |
| 11 | 108 | |
| 12 | | pub const SYS_exit = 0; |
| 13 | | pub const SYS_createMailbox = 1; |
| 14 | | pub const SYS_send = 2; |
| 15 | | pub const SYS_receive = 3; |
| 16 | | pub const SYS_map = 4; |
| 17 | | pub const SYS_createThread = 5; |
| 109 | pub const Syscall = enum(usize) { |
| 110 | exit = 0, |
| 111 | createPort = 1, |
| 112 | send = 2, |
| 113 | receive = 3, |
| 114 | subscribeIRQ = 4, |
| 115 | inb = 5, |
| 116 | map = 6, |
| 117 | createThread = 7, |
| 118 | }; |
| 18 | 119 | |
| 19 | 120 | |
| 20 | 121 | //////////////////// |
| ... | ... | @@ -22,28 +123,39 @@ pub const SYS_createThread = 5; |
| 22 | 123 | //////////////////// |
| 23 | 124 | |
| 24 | 125 | pub fn exit(status: i32) noreturn { |
| 25 | | _ = syscall1(SYS_exit, @bitCast(usize, isize(status))); |
| 126 | _ = syscall1(Syscall.exit, @bitCast(usize, isize(status))); |
| 26 | 127 | unreachable; |
| 27 | 128 | } |
| 28 | 129 | |
| 29 | | pub fn createMailbox(id: u16) void { |
| 30 | | _ = syscall1(SYS_createMailbox, id); |
| 130 | pub fn createPort(mailbox_id: &const MailboxId) void { |
| 131 | _ = switch (*mailbox_id) { |
| 132 | MailboxId.Port => |id| syscall1(Syscall.createPort, id), |
| 133 | else => unreachable, |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | pub fn send(message: &const Message) void { |
| 138 | _ = syscall1(Syscall.send, @ptrToInt(message)); |
| 139 | } |
| 140 | |
| 141 | pub fn receive(destination: &Message) void { |
| 142 | _ = syscall1(Syscall.receive, @ptrToInt(destination)); |
| 31 | 143 | } |
| 32 | 144 | |
| 33 | | pub fn send(mailbox_id: u16, data: usize) void { |
| 34 | | _ = syscall2(SYS_send, mailbox_id, data); |
| 145 | pub fn subscribeIRQ(irq: u8, mailbox_id: &const MailboxId) void { |
| 146 | _ = syscall2(Syscall.subscribeIRQ, irq, @ptrToInt(mailbox_id)); |
| 35 | 147 | } |
| 36 | 148 | |
| 37 | | pub fn receive(mailbox_id: u16) usize { |
| 38 | | return syscall1(SYS_receive, mailbox_id); |
| 149 | pub fn inb(port: u16) u8 { |
| 150 | return u8(syscall1(Syscall.inb, port)); |
| 39 | 151 | } |
| 40 | 152 | |
| 41 | 153 | pub fn map(v_addr: usize, p_addr: usize, size: usize, writable: bool) bool { |
| 42 | | return syscall4(SYS_map, v_addr, p_addr, size, usize(writable)) != 0; |
| 154 | return syscall4(Syscall.map, v_addr, p_addr, size, usize(writable)) != 0; |
| 43 | 155 | } |
| 44 | 156 | |
| 45 | 157 | pub fn createThread(function: fn()void) u16 { |
| 46 | | return u16(syscall1(SYS_createThread, @ptrToInt(function))); |
| 158 | return u16(syscall1(Syscall.createThread, @ptrToInt(function))); |
| 47 | 159 | } |
| 48 | 160 | |
| 49 | 161 | |
| ... | ... | @@ -51,20 +163,20 @@ pub fn createThread(function: fn()void) u16 { |
| 51 | 163 | //// Syscall stubs //// |
| 52 | 164 | ///////////////////////// |
| 53 | 165 | |
| 54 | | pub inline fn syscall0(number: usize) usize { |
| 166 | inline fn syscall0(number: Syscall) usize { |
| 55 | 167 | return asm volatile ("int $0x80" |
| 56 | 168 | : [ret] "={eax}" (-> usize) |
| 57 | 169 | : [number] "{eax}" (number)); |
| 58 | 170 | } |
| 59 | 171 | |
| 60 | | pub inline fn syscall1(number: usize, arg1: usize) usize { |
| 172 | inline fn syscall1(number: Syscall, arg1: usize) usize { |
| 61 | 173 | return asm volatile ("int $0x80" |
| 62 | 174 | : [ret] "={eax}" (-> usize) |
| 63 | 175 | : [number] "{eax}" (number), |
| 64 | 176 | [arg1] "{ecx}" (arg1)); |
| 65 | 177 | } |
| 66 | 178 | |
| 67 | | pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize { |
| 179 | inline fn syscall2(number: Syscall, arg1: usize, arg2: usize) usize { |
| 68 | 180 | return asm volatile ("int $0x80" |
| 69 | 181 | : [ret] "={eax}" (-> usize) |
| 70 | 182 | : [number] "{eax}" (number), |
| ... | ... | @@ -72,7 +184,7 @@ pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) usize { |
| 72 | 184 | [arg2] "{edx}" (arg2)); |
| 73 | 185 | } |
| 74 | 186 | |
| 75 | | pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize { |
| 187 | inline fn syscall3(number: Syscall, arg1: usize, arg2: usize, arg3: usize) usize { |
| 76 | 188 | return asm volatile ("int $0x80" |
| 77 | 189 | : [ret] "={eax}" (-> usize) |
| 78 | 190 | : [number] "{eax}" (number), |
| ... | ... | @@ -81,7 +193,7 @@ pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usi |
| 81 | 193 | [arg3] "{ebx}" (arg3)); |
| 82 | 194 | } |
| 83 | 195 | |
| 84 | | pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { |
| 196 | inline fn syscall4(number: Syscall, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize { |
| 85 | 197 | return asm volatile ("int $0x80" |
| 86 | 198 | : [ret] "={eax}" (-> usize) |
| 87 | 199 | : [number] "{eax}" (number), |
| ... | ... | @@ -91,7 +203,7 @@ pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg |
| 91 | 203 | [arg4] "{esi}" (arg4)); |
| 92 | 204 | } |
| 93 | 205 | |
| 94 | | pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, |
| 206 | inline fn syscall5(number: Syscall, arg1: usize, arg2: usize, arg3: usize, |
| 95 | 207 | arg4: usize, arg5: usize) usize |
| 96 | 208 | { |
| 97 | 209 | return asm volatile ("int $0x80" |
| ... | ... | @@ -104,7 +216,7 @@ pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, |
| 104 | 216 | [arg5] "{edi}" (arg5)); |
| 105 | 217 | } |
| 106 | 218 | |
| 107 | | pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, |
| 219 | inline fn syscall6(number: Syscall, arg1: usize, arg2: usize, arg3: usize, |
| 108 | 220 | arg4: usize, arg5: usize, arg6: usize) usize |
| 109 | 221 | { |
| 110 | 222 | return asm volatile ("int $0x80" |