authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2023-06-14 15:39:40-04:00
committergravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2023-06-16 08:34:30-04:00
log4dac9f54ddfcd6036b8b2b0704d0b9aa3ed9bd6d
tree3e878f77b95a1f6ddb1b6ff368f32ac327a90549
parent153def146038efd62eaea1b8ed2fdd11f38cf6ef

plan9: flesh out stdlib enough to allow not using simplified start logic


5 files changed, 166 insertions(+), 3 deletions(-)

lib/std/os.zig+1
......@@ -67,6 +67,7 @@ else if (builtin.link_libc or is_windows)
6767 std.c
6868else switch (builtin.os.tag) {
6969 .linux => linux,
70 .plan9 => plan9,
7071 .wasi => wasi,
7172 .uefi => uefi,
7273 else => struct {},
lib/std/os/plan9.zig+88-1
......@@ -5,6 +5,78 @@ pub const syscall_bits = switch (builtin.cpu.arch) {
55 .x86_64 => @import("plan9/x86_64.zig"),
66 else => @compileError("more plan9 syscall implementations (needs more inline asm in stage2"),
77};
8pub const E = @import("plan9/errno.zig").E;
9/// Get the errno from a syscall return value, or 0 for no error.
10pub fn getErrno(r: usize) E {
11 const signed_r = @bitCast(isize, r);
12 const int = if (signed_r > -4096 and signed_r < 0) -signed_r else 0;
13 return @intToEnum(E, int);
14}
15pub const SIG = struct {
16 /// hangup
17 pub const HUP = 1;
18 /// interrupt
19 pub const INT = 2;
20 /// quit
21 pub const QUIT = 3;
22 /// illegal instruction (not reset when caught)
23 pub const ILL = 4;
24 /// used by abort
25 pub const ABRT = 5;
26 /// floating point exception
27 pub const FPE = 6;
28 /// kill (cannot be caught or ignored)
29 pub const KILL = 7;
30 /// segmentation violation
31 pub const SEGV = 8;
32 /// write on a pipe with no one to read it
33 pub const PIPE = 9;
34 /// alarm clock
35 pub const ALRM = 10;
36 /// software termination signal from kill
37 pub const TERM = 11;
38 /// user defined signal 1
39 pub const USR1 = 12;
40 /// user defined signal 2
41 pub const USR2 = 13;
42 /// bus error
43 pub const BUS = 14;
44 // The following symbols must be defined, but the signals needn't be supported
45 /// child process terminated or stopped
46 pub const CHLD = 15;
47 /// continue if stopped
48 pub const CONT = 16;
49 /// stop
50 pub const STOP = 17;
51 /// interactive stop
52 pub const TSTP = 18;
53 /// read from ctl tty by member of background
54 pub const TTIN = 19;
55 /// write to ctl tty by member of background
56 pub const TTOU = 20;
57};
58pub const sigset_t = c_long;
59pub const empty_sigset = 0;
60pub const siginfo_t = c_long; // TODO plan9 doesn't have sigaction_fn. Sigaction is not a union, but we incude it here to be compatible.
61pub const Sigaction = extern struct {
62 pub const handler_fn = *const fn (c_int) callconv(.C) void;
63 pub const sigaction_fn = *const fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
64
65 handler: extern union {
66 handler: ?handler_fn,
67 sigaction: ?sigaction_fn,
68 },
69 mask: sigset_t,
70 flags: c_int,
71};
72// TODO implement sigaction
73// right now it is just a shim to allow using start.zig code
74pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) usize {
75 _ = oact;
76 _ = act;
77 _ = sig;
78 return 0;
79}
880pub const SYS = enum(usize) {
981 SYSR1 = 0,
1082 _ERRSTR = 1,
......@@ -64,6 +136,10 @@ pub fn pwrite(fd: usize, buf: [*]const u8, count: usize, offset: usize) usize {
64136 return syscall_bits.syscall4(.PWRITE, fd, @ptrToInt(buf), count, offset);
65137}
66138
139pub fn pread(fd: usize, buf: [*]const u8, count: usize, offset: usize) usize {
140 return syscall_bits.syscall4(.PREAD, fd, @ptrToInt(buf), count, offset);
141}
142
67143pub fn open(path: [*:0]const u8, omode: OpenMode) usize {
68144 return syscall_bits.syscall2(.OPEN, @ptrToInt(path), @enumToInt(omode));
69145}
......@@ -72,8 +148,19 @@ pub fn create(path: [*:0]const u8, omode: OpenMode, perms: usize) usize {
72148 return syscall_bits.syscall3(.CREATE, @ptrToInt(path), @enumToInt(omode), perms);
73149}
74150
75pub fn exits(status: ?[*:0]const u8) void {
151pub fn exit(status: u8) noreturn {
152 if (status == 0) {
153 exits(null);
154 } else {
155 // TODO plan9 does not have exit codes. You either exit with 0 or a string
156 const arr: [1:0]u8 = .{status};
157 exits(&arr);
158 }
159}
160
161pub fn exits(status: ?[*:0]const u8) noreturn {
76162 _ = syscall_bits.syscall1(.EXITS, if (status) |s| @ptrToInt(s) else 0);
163 unreachable;
77164}
78165
79166pub fn close(fd: usize) usize {
lib/std/os/plan9/errno.zig created+76
......@@ -0,0 +1,76 @@
1//! Ported from /sys/include/ape/errno.h
2pub const E = enum(u16) {
3 SUCCESS = 0,
4 DOM = 1000,
5 RANGE = 1001,
6 PLAN9 = 1002,
7
8 @"2BIG" = 1,
9 ACCES = 2,
10 AGAIN = 3,
11 // WOULDBLOCK = 3, // TODO errno.h has 2 names for 3
12 BADF = 4,
13 BUSY = 5,
14 CHILD = 6,
15 DEADLK = 7,
16 EXIST = 8,
17 FAULT = 9,
18 FBIG = 10,
19 INTR = 11,
20 INVAL = 12,
21 IO = 13,
22 ISDIR = 14,
23 MFILE = 15,
24 MLINK = 16,
25 NAMETOOLONG = 17,
26 NFILE = 18,
27 NODEV = 19,
28 NOENT = 20,
29 NOEXEC = 21,
30 NOLCK = 22,
31 NOMEM = 23,
32 NOSPC = 24,
33 NOSYS = 25,
34 NOTDIR = 26,
35 NOTEMPTY = 27,
36 NOTTY = 28,
37 NXIO = 29,
38 PERM = 30,
39 PIPE = 31,
40 ROFS = 32,
41 SPIPE = 33,
42 SRCH = 34,
43 XDEV = 35,
44
45 // bsd networking software
46 NOTSOCK = 36,
47 PROTONOSUPPORT = 37,
48 // PROTOTYPE = 37, // TODO errno.h has two names for 37
49 CONNREFUSED = 38,
50 AFNOSUPPORT = 39,
51 NOBUFS = 40,
52 OPNOTSUPP = 41,
53 ADDRINUSE = 42,
54 DESTADDRREQ = 43,
55 MSGSIZE = 44,
56 NOPROTOOPT = 45,
57 SOCKTNOSUPPORT = 46,
58 PFNOSUPPORT = 47,
59 ADDRNOTAVAIL = 48,
60 NETDOWN = 49,
61 NETUNREACH = 50,
62 NETRESET = 51,
63 CONNABORTED = 52,
64 ISCONN = 53,
65 NOTCONN = 54,
66 SHUTDOWN = 55,
67 TOOMANYREFS = 56,
68 TIMEDOUT = 57,
69 HOSTDOWN = 58,
70 HOSTUNREACH = 59,
71 GREG = 60,
72
73 // These added in 1003.1b-1993
74 CANCELED = 61,
75 INPROGRESS = 62,
76};
lib/std/os/plan9/x86_64.zig+1-1
......@@ -66,7 +66,7 @@ pub fn syscall4(sys: plan9.SYS, arg0: usize, arg1: usize, arg2: usize, arg3: usi
6666 : [arg0] "{r8}" (arg0),
6767 [arg1] "{r9}" (arg1),
6868 [arg2] "{r10}" (arg2),
69 [arg2] "{r11}" (arg3),
69 [arg3] "{r11}" (arg3),
7070 [syscall_number] "{rbp}" (@enumToInt(sys)),
7171 : "rcx", "rax", "rbp", "r11", "memory"
7272 );
lib/std/start.zig-1
......@@ -18,7 +18,6 @@ const start_sym_name = if (native_arch.isMIPS()) "__start" else "_start";
1818// Until then, we have simplified logic here for self-hosted. TODO remove this once
1919// self-hosted is capable enough to handle all of the real start.zig logic.
2020pub const simplified_logic =
21 (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag == .plan9) or
2221 builtin.zig_backend == .stage2_x86 or
2322 builtin.zig_backend == .stage2_aarch64 or
2423 builtin.zig_backend == .stage2_arm or