authorgravatar for andrea@orru.ioAndrea Orru <andrea@orru.io> 2018-08-06 03:05:22-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-08-06 03:05:22-04:00
log72bac72338af64a73668721cd035dfdf1ff2badb
treeaff0e04e9deeb58250e5b02f42e9c57850bd46fc
parent63a23e848a62d5f167f8d5478de9766cb24aa6eb
parent79d77faebf4fe667cc4e2d97263df499131620b9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1339 from ziglang/zen_stdlib

Updates and fixes for the Zen stdlib

2 files changed, 64 insertions(+), 68 deletions(-)

std/os/zen.zig+64-62
......@@ -1,38 +1,55 @@
1const std = @import("../index.zig");
2const assert = std.debug.assert;
3
14//////////////////////////
25//// IPC structures ////
36//////////////////////////
47
58pub const Message = struct {
6 sender: MailboxId,
9sender: MailboxId,
710 receiver: MailboxId,
8 type: usize,
9 payload: usize,
11 code: usize,
12 args: [5]usize,
13 payload: ?[]const u8,
1014
1115 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,
16 return Message {
17 .sender = MailboxId.Undefined,
18 .receiver = mailbox_id.*,
19 .code = undefined,
20 .args = undefined,
21 .payload = null,
1722 };
1823 }
1924
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,
25 pub fn to(mailbox_id: *const MailboxId, msg_code: usize, args: ...) Message {
26 var message = Message {
27 .sender = MailboxId.This,
28 .receiver = mailbox_id.*,
29 .code = msg_code,
30 .args = undefined,
31 .payload = null,
2632 };
33
34 assert (args.len <= message.args.len);
35 comptime var i = 0;
36 inline while (i < args.len) : (i += 1) {
37 message.args[i] = args[i];
38 }
39
40 return message;
2741 }
2842
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 };
43 pub fn as(self: *const Message, sender: *const MailboxId) Message {
44 var message = self.*;
45 message.sender = sender.*;
46 return message;
47 }
48
49 pub fn withPayload(self: *const Message, payload: []const u8) Message {
50 var message = self.*;
51 message.payload = payload;
52 return message;
3653 }
3754};
3855
......@@ -63,21 +80,26 @@ pub const STDOUT_FILENO = 1;
6380pub const STDERR_FILENO = 2;
6481
6582// FIXME: let's borrow Linux's error numbers for now.
66pub const getErrno = @import("linux/index.zig").getErrno;
6783use @import("linux/errno.zig");
84// Get the errno from a syscall return value, or 0 for no error.
85pub fn getErrno(r: usize) usize {
86 const signed_r = @bitCast(isize, r);
87 return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
88}
6889
6990// TODO: implement this correctly.
70pub fn read(fd: i32, buf: *u8, count: usize) usize {
91pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
7192 switch (fd) {
7293 STDIN_FILENO => {
7394 var i: usize = 0;
7495 while (i < count) : (i += 1) {
7596 send(Message.to(Server.Keyboard, 0));
7697
98 // FIXME: we should be certain that we are receiving from Keyboard.
7799 var message = Message.from(MailboxId.This);
78 receive(*message);
100 receive(&message);
79101
80 buf[i] = u8(message.payload);
102 buf[i] = @intCast(u8, message.args[0]);
81103 }
82104 },
83105 else => unreachable,
......@@ -86,13 +108,11 @@ pub fn read(fd: i32, buf: *u8, count: usize) usize {
86108}
87109
88110// TODO: implement this correctly.
89pub fn write(fd: i32, buf: *const u8, count: usize) usize {
111pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
90112 switch (fd) {
91113 STDOUT_FILENO, STDERR_FILENO => {
92 var i: usize = 0;
93 while (i < count) : (i += 1) {
94 send(Message.withData(Server.Terminal, 1, buf[i]));
95 }
114 send(Message.to(Server.Terminal, 1)
115 .withPayload(buf[0..count]));
96116 },
97117 else => unreachable,
98118 }
......@@ -104,17 +124,14 @@ pub fn write(fd: i32, buf: *const u8, count: usize) usize {
104124///////////////////////////
105125
106126pub const Syscall = enum(usize) {
107 exit = 0,
108 createPort = 1,
109 send = 2,
110 receive = 3,
111 subscribeIRQ = 4,
112 inb = 5,
113 map = 6,
114 createThread = 7,
115 createProcess = 8,
116 wait = 9,
117 portReady = 10,
127 exit = 0,
128 send = 1,
129 receive = 2,
130 subscribeIRQ = 3,
131 inb = 4,
132 outb = 5,
133 map = 6,
134 createThread = 7,
118135};
119136
120137////////////////////
......@@ -126,13 +143,6 @@ pub fn exit(status: i32) noreturn {
126143 unreachable;
127144}
128145
129pub fn createPort(mailbox_id: *const MailboxId) void {
130 _ = switch (*mailbox_id) {
131 MailboxId.Port => |id| syscall1(Syscall.createPort, id),
132 else => unreachable,
133 };
134}
135
136146pub fn send(message: *const Message) void {
137147 _ = syscall1(Syscall.send, @ptrToInt(message));
138148}
......@@ -146,29 +156,21 @@ pub fn subscribeIRQ(irq: u8, mailbox_id: *const MailboxId) void {
146156}
147157
148158pub fn inb(port: u16) u8 {
149 return u8(syscall1(Syscall.inb, port));
159 return @intCast(u8, syscall1(Syscall.inb, port));
160}
161
162pub fn outb(port: u16, value: u8) void {
163 _ = syscall2(Syscall.outb, port, value);
150164}
151165
152166pub fn map(v_addr: usize, p_addr: usize, size: usize, writable: bool) bool {
153 return syscall4(Syscall.map, v_addr, p_addr, size, usize(writable)) != 0;
167 return syscall4(Syscall.map, v_addr, p_addr, size, @boolToInt(writable)) != 0;
154168}
155169
156170pub fn createThread(function: fn () void) u16 {
157171 return u16(syscall1(Syscall.createThread, @ptrToInt(function)));
158172}
159173
160pub fn createProcess(elf_addr: usize) u16 {
161 return u16(syscall1(Syscall.createProcess, elf_addr));
162}
163
164pub fn wait(tid: u16) void {
165 _ = syscall1(Syscall.wait, tid);
166}
167
168pub fn portReady(port: u16) bool {
169 return syscall1(Syscall.portReady, port) != 0;
170}
171
172174/////////////////////////
173175//// Syscall stubs ////
174176/////////////////////////
std/special/bootstrap.zig-6
......@@ -13,17 +13,11 @@ comptime {
1313 @export("main", main, strong_linkage);
1414 } else if (builtin.os == builtin.Os.windows) {
1515 @export("WinMainCRTStartup", WinMainCRTStartup, strong_linkage);
16 } else if (builtin.os == builtin.Os.zen) {
17 @export("_start", zen_start, strong_linkage);
1816 } else {
1917 @export("_start", _start, strong_linkage);
2018 }
2119}
2220
23extern fn zen_start() noreturn {
24 std.os.posix.exit(@inlineCall(callMain));
25}
26
2721nakedcc fn _start() noreturn {
2822 switch (builtin.arch) {
2923 builtin.Arch.x86_64 => {