lib/std/sys/host.rad 4.5 KiB raw
1
//! Hosted system call interface: an `ecall` with the call number and up to
2
//! four arguments. A negative result is the negated error code. Paths are
3
//! passed as pointer and length. The record layouts belong to `std::fs` and
4
//! `std::process`.
5
6
use std::intrinsics;
7
use std::lang::alloc;
8
use std::mem::heap;
9
use std::sys;
10
11
/// Call number of `read`.
12
export constant READ: u32 = 0x401;
13
/// Call number of `open`.
14
export constant OPEN: u32 = 0x403;
15
/// Call number of `close`.
16
export constant CLOSE: u32 = 0x404;
17
/// Call number of `stat`.
18
export constant STAT: u32 = 0x405;
19
/// Call number of `makeDir`.
20
export constant MAKE_DIR: u32 = 0x406;
21
/// Call number of `remove`.
22
export constant REMOVE: u32 = 0x407;
23
/// Call number of `rename`.
24
export constant RENAME: u32 = 0x408;
25
/// Call number of `listDir`.
26
export constant LIST_DIR: u32 = 0x409;
27
/// Call number of `spawn`.
28
export constant SPAWN: u32 = 0x40A;
29
/// Call number of `wait`.
30
export constant WAIT: u32 = 0x40B;
31
/// Call number of `waitAny`.
32
export constant WAIT_ANY: u32 = 0x40C;
33
/// Call number of `exchange`.
34
export constant EXCHANGE: u32 = 0x40D;
35
36
/// Standard input descriptor.
37
export constant STDIN: i64 = 0;
38
/// Standard output descriptor.
39
export constant STDOUT: i64 = 1;
40
/// Standard error descriptor.
41
export constant STDERR: i64 = 2;
42
43
/// Return the address of the bytes as a call argument. The caller keeps the
44
/// storage alive until the call returns. This and [`callPath`] hold the only
45
/// pointer casts of the host interface.
46
export fn addr(bytes: &[u8]) -> i64 {
47
    unsafe {
48
        return bytes.ptr as i64;
49
    }
50
}
51
52
/// Issue a call. Returns the non-negative result, or throws the error that a
53
/// negative result stands for.
54
export fn call(number: u32, a: i64, b: i64, c: i64, d: i64) -> i64 throws (sys::Error) {
55
    let result = intrinsics::ecall(number, a, b, c, d);
56
    if result >= 0 {
57
        return result;
58
    }
59
    // Errors by negated result code. Codes beyond the table are `Io`.
60
    let errors: [sys::Error; 9] = [
61
        sys::Error::Io, sys::Error::NotFound, sys::Error::Exists,
62
        sys::Error::Denied, sys::Error::NotEmpty, sys::Error::Invalid,
63
        sys::Error::TooLarge, sys::Error::Limit, sys::Error::Io,
64
    ];
65
    let code = (0 - result) as u64;
66
    if code < errors.len as u64 {
67
        throw errors[code as u32];
68
    }
69
    throw sys::Error::Io;
70
}
71
72
/// Issue a call whose first two arguments are `path`.
73
export fn callPath(number: u32, path: &[u8], c: i64, d: i64) -> i64 throws (sys::Error) {
74
    return try call(number, addr(path), path.len as i64, c, d);
75
}
76
77
/// Issue a call that writes into storage given as an address and a size in
78
/// `args[at]` and `args[at + 1]`, and returns the number of bytes it wrote.
79
/// The storage is `count` bytes past the end of `bytes`, reserved in `arena`
80
/// without being filled, and the written bytes are appended to `bytes`.
81
/// Returns their number. A call that reports more than `count` bytes throws
82
/// `Invalid` and appends nothing.
83
export fn callInto(
84
    number: u32,
85
    args: [i64; 4],
86
    at: u32,
87
    bytes: &mut *mut [u8],
88
    count: u32,
89
    arena: &heap::Heap
90
) -> u32 throws (sys::Error, alloc::AllocError) {
91
    assert at < 3;
92
    try bytes.reserve(count, arena);
93
    let start = bytes.len;
94
    let mut values = args;
95
    set values[at] = addr(&bytes[..]) + start as i64;
96
    set values[at + 1] = count as i64;
97
    let n = try call(number, values[0], values[1], values[2], values[3]);
98
    if n > count as i64 {
99
        throw sys::Error::Invalid;
100
    }
101
    // The call wrote the first `n` reserved bytes, so the slice exposes no
102
    // byte that was not written.
103
    unsafe {
104
        set bytes.len = start + n as u32;
105
    }
106
    return n as u32;
107
}
108
109
/// Append at most `count` bytes read from a descriptor to `bytes`, in storage
110
/// from `arena`. Returns the number of bytes read, which is zero at end of file.
111
export fn readInto(fd: i64, bytes: &mut *mut [u8], count: u32, arena: &heap::Heap) -> u32
112
    throws (sys::Error, alloc::AllocError)
113
{
114
    return try callInto(READ, [fd, 0, 0, 0], 1, bytes, count, arena);
115
}
116
117
/// Read from a descriptor into the buffer (call 0x401). Returns the number of bytes read,
118
/// which is zero at end of file.
119
export fn read(fd: i64, buf: &mut [u8]) -> u32 throws (sys::Error) {
120
    return try call(READ, fd, addr(buf), buf.len as i64, 0) as u32;
121
}
122
123
/// Write the buffer to a descriptor (call 0x402). Returns the number of bytes written.
124
export fn write(fd: i64, buf: &[u8]) -> u32 throws (sys::Error) {
125
    return try call(0x402, fd, addr(buf), buf.len as i64, 0) as u32;
126
}