mirror of
https://github.com/ziglang/zig.git
synced 2026-01-02 03:25:01 +00:00
Merge pull request #1339 from ziglang/zen_stdlib
Updates and fixes for the Zen stdlib
This commit is contained in:
commit
72bac72338
126
std/os/zen.zig
126
std/os/zen.zig
@ -1,38 +1,55 @@
|
||||
const std = @import("../index.zig");
|
||||
const assert = std.debug.assert;
|
||||
|
||||
//////////////////////////
|
||||
//// IPC structures ////
|
||||
//////////////////////////
|
||||
|
||||
pub const Message = struct {
|
||||
sender: MailboxId,
|
||||
sender: MailboxId,
|
||||
receiver: MailboxId,
|
||||
type: usize,
|
||||
payload: usize,
|
||||
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,
|
||||
return Message {
|
||||
.sender = MailboxId.Undefined,
|
||||
.receiver = mailbox_id.*,
|
||||
.code = undefined,
|
||||
.args = undefined,
|
||||
.payload = null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn to(mailbox_id: *const MailboxId, msg_type: usize) Message {
|
||||
return Message{
|
||||
.sender = MailboxId.This,
|
||||
.receiver = *mailbox_id,
|
||||
.type = msg_type,
|
||||
.payload = 0,
|
||||
pub fn to(mailbox_id: *const MailboxId, msg_code: usize, args: ...) Message {
|
||||
var message = Message {
|
||||
.sender = MailboxId.This,
|
||||
.receiver = mailbox_id.*,
|
||||
.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 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 withPayload(self: *const Message, payload: []const u8) Message {
|
||||
var message = self.*;
|
||||
message.payload = payload;
|
||||
return message;
|
||||
}
|
||||
};
|
||||
|
||||
@ -63,21 +80,26 @@ 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;
|
||||
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);
|
||||
receive(&message);
|
||||
|
||||
buf[i] = u8(message.payload);
|
||||
buf[i] = @intCast(u8, message.args[0]);
|
||||
}
|
||||
},
|
||||
else => unreachable,
|
||||
@ -86,13 +108,11 @@ 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 => {
|
||||
var i: usize = 0;
|
||||
while (i < count) : (i += 1) {
|
||||
send(Message.withData(Server.Terminal, 1, buf[i]));
|
||||
}
|
||||
send(Message.to(Server.Terminal, 1)
|
||||
.withPayload(buf[0..count]));
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
@ -104,17 +124,14 @@ 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,
|
||||
exit = 0,
|
||||
send = 1,
|
||||
receive = 2,
|
||||
subscribeIRQ = 3,
|
||||
inb = 4,
|
||||
outb = 5,
|
||||
map = 6,
|
||||
createThread = 7,
|
||||
};
|
||||
|
||||
////////////////////
|
||||
@ -126,13 +143,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));
|
||||
}
|
||||
@ -146,29 +156,21 @@ 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 {
|
||||
_ = 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;
|
||||
return syscall4(Syscall.map, v_addr, p_addr, size, @boolToInt(writable)) != 0;
|
||||
}
|
||||
|
||||
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 ////
|
||||
/////////////////////////
|
||||
|
||||
@ -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 => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user