aboutsummaryrefslogtreecommitdiff
path: root/src/lib/syscall.zig
blob: 9f88aea3de0ae90d12348154f85d8c271b4e9a4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

const std = @import("std");

const instructions = @import("instructions.zig");
const paging = @import("paging.zig");
const process = @import("process.zig");
const sysexchange = @import("sysexchange.zig");
const trap = @import("trap.zig");
const vfs = @import("vfs.zig");

pub const Error = error{
    Unimplemented,
};

pub const HandleError = error{
    UnknownSyscall,
};

pub fn handler(proc: *process.Info, trap_frame: *trap.Frame) !void {
    const ret = sysexchange.frameReturn;
    switch (trap_frame.general_purpose_registers[17]) {
        100000 => ret(null, trap_frame, uprint(trap_frame)),
        100001 => ret(null, trap_frame, open(proc, trap_frame)),
        100002 => ret(null, trap_frame, close(proc, trap_frame)),
        100003 => ret(null, trap_frame, provideStream(proc, trap_frame)),
        100004 => ret(null, trap_frame, provideFile(proc, trap_frame)),
        100005 => ret(null, trap_frame, provideHook(proc, trap_frame)),
        100006 => ret(null, trap_frame, mkdir(proc, trap_frame)),
        100007 => ret(null, trap_frame, provideDirHook(proc, trap_frame)),
        100008 => ret(null, trap_frame, remove(trap_frame)),
        100009 => ret(null, trap_frame, read(proc, trap_frame)),
        100010 => ret(null, trap_frame, write(proc, trap_frame)),
        100011 => ret(null, trap_frame, list(proc, trap_frame)),
        100012 => ret(null, trap_frame, terminate(proc)),
        100013 => ret(null, trap_frame, processId(proc)),
        else => return HandleError.UnknownSyscall,
    }
}

// uprint(str_addr: usize, len: usize) void
fn uprint(trap_frame: *const trap.Frame) void {
    const procmem: *paging.Table = @ptrFromInt(instructions.satp.read().ppn << 12);

    const paddr = procmem.translate(trap_frame.general_purpose_registers[10]).?;

    const str_ptr: [*]const u8 = @ptrFromInt(paddr);
    const str = str_ptr[0..trap_frame.general_purpose_registers[11]];

    const w = @import("Console.zig").autoChoose().?.writer();
    w.print("User message: {s}\r\n", .{str}) catch unreachable;
}

// open(path_c: [*:0]const u8, data: usize) Result(usize) // fixme: Kernel panic if null pointer
fn open(proc: *process.Info, trap_frame: *trap.Frame) void {
    paging.setUserMemoryAccess(true);
    defer paging.setUserMemoryAccess(false);

    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);
    const data = trap_frame.general_purpose_registers[11];

    const result = vfs.openZ(proc, path_c, data) catch |err| {
        sysexchange.frameReturn(usize, trap_frame, err);
        return;
    };

    switch (result) {
        .rd => |rd| {
            const maybe_handle = proc.createRdHandle(rd);
            sysexchange.frameReturn(usize, trap_frame, maybe_handle);
        },
        .data => |result_data| {
            sysexchange.frameReturn(null, trap_frame, result_data);
        },
    }
}

// close(handle: usize) void
fn close(proc: *process.Info, trap_frame: *const trap.Frame) void {
    const handle = trap_frame.general_purpose_registers[10];
    proc.destroyRdHandle(handle);
}

// provideStream(
//     path_c: [*:0]const u8, // fixme: Kernel panic if null pointer
//     options: *const vfs.Options, // fixme: Kernel panic if null pointer
//     readFn: ?vfs.Stream.ReadFn,
//     writeFn: ?vfs.Stream.WriteFn,
// ) Result(void)
fn provideStream(proc: *const process.Info, trap_frame: *trap.Frame) void {
    paging.setUserMemoryAccess(true);
    defer paging.setUserMemoryAccess(false);

    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);
    const options: *const vfs.Options = @ptrFromInt(trap_frame.general_purpose_registers[11]);
    const readFn: ?vfs.Stream.ReadFn = @ptrFromInt(trap_frame.general_purpose_registers[12]);
    const writeFn: ?vfs.Stream.WriteFn = @ptrFromInt(trap_frame.general_purpose_registers[13]);

    sysexchange.frameReturn(null, trap_frame, vfs.provideResourceZ(path_c, .{
        .tag = .stream,
        .data = .{ .stream = .{
            .readFn = readFn,
            .writeFn = writeFn,
        } },
    }, proc.id, options.*));
}

// provideFile(
//     path_c: [*:0]const u8, // fixme: Kernel panic if null pointer
//     options: *const vfs.Options, // fixme: Kernel panic if null pointer
//     readFn: ?vfs.File.ReadFn,
//     writeFn: ?vfs.File.WriteFn,
//     closeFn: ?vfs.File.CloseFn,
//     initializer: ?*anyopaque,
// ) Result(void)
fn provideFile(proc: *const process.Info, trap_frame: *trap.Frame) void {
    paging.setUserMemoryAccess(true);
    defer paging.setUserMemoryAccess(false);

    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);
    const options: *const vfs.Options = @ptrFromInt(trap_frame.general_purpose_registers[11]);
    const readFn: ?vfs.File.ReadFn = @ptrFromInt(trap_frame.general_purpose_registers[12]);
    const writeFn: ?vfs.File.WriteFn = @ptrFromInt(trap_frame.general_purpose_registers[13]);
    const closeFn: ?vfs.File.CloseFn = @ptrFromInt(trap_frame.general_purpose_registers[14]);
    const initializer: ?*anyopaque = @ptrFromInt(trap_frame.general_purpose_registers[15]);

    sysexchange.frameReturn(null, trap_frame, vfs.provideResourceZ(path_c, .{
        .tag = .file,
        .data = .{ .file = .{
            .readFn = readFn,
            .writeFn = writeFn,
            .closeFn = closeFn,
            .initializer = initializer,
        } },
    }, proc.id, options.*));
}

// provideHook(
//     path_c: [*:0]const u8, // fixme: Kernel panic if null pointer
//     options: *const vfs.Options, // fixme: Kernel panic if null pointer,
//     callback: vfs.Hook.Callback,
// ) Result(void)
fn provideHook(proc: *const process.Info, trap_frame: *trap.Frame) void {
    paging.setUserMemoryAccess(true);
    defer paging.setUserMemoryAccess(false);

    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);
    const options: *const vfs.Options = @ptrFromInt(trap_frame.general_purpose_registers[11]);
    const callback: vfs.Hook.Callback = @ptrFromInt(trap_frame.general_purpose_registers[12]);

    sysexchange.frameReturn(null, trap_frame, vfs.provideResourceZ(path_c, .{
        .tag = .hook,
        .data = .{ .hook = .{
            .callback = callback,
        } },
    }, proc.id, options.*));
}

// mkdir(
//     path_c: [*:0]const u8, // fixme: Kernel panic if null pointer
// ) Result(void)
fn mkdir(proc: *const process.Info, trap_frame: *trap.Frame) void {
    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);

    const allocator = vfs.treeRoot().allocator;
    const tree = allocator.create(vfs.Tree) catch |err| {
        sysexchange.frameReturn(void, trap_frame, err);
        return;
    };
    tree.* = vfs.Tree.init(allocator);

    sysexchange.frameReturn(null, trap_frame, vfs.provideResourceZ(path_c, .{
        .tag = .dir,
        .data = .{ .dir = tree },
    }, proc.id, .{
        .reclaimable = false,
    }));
}

// provideDirHook(
//     path_c: [*:0]const u8, // fixme: Kernel panic if null pointer
//     options: *const vfs.Options, // fixme: Kernel panic if null pointer
//     provideFn: vfs.DirHook.ProvideFn,
//     findFn: vfs.DirHook.FindFn,
//     listFn:vfs.DirHook.ListFn,
//     removeFn: vfs.DirHook.RemoveFn,
// ) Result(void)
fn provideDirHook(proc: *const process.Info, trap_frame: *trap.Frame) void {
    paging.setUserMemoryAccess(true);
    defer paging.setUserMemoryAccess(false);

    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);
    const options: *const vfs.Options = @ptrFromInt(trap_frame.general_purpose_registers[11]);
    const provideFn: vfs.DirHook.ProvideFn = @ptrFromInt(trap_frame.general_purpose_registers[12]);
    const findFn: vfs.DirHook.FindFn = @ptrFromInt(trap_frame.general_purpose_registers[13]);
    const listFn: vfs.DirHook.ListFn = @ptrFromInt(trap_frame.general_purpose_registers[14]);
    const removeFn: vfs.DirHook.RemoveFn = @ptrFromInt(trap_frame.general_purpose_registers[15]);

    sysexchange.frameReturn(null, trap_frame, vfs.provideResourceZ(path_c, .{
        .tag = .dir_hook,
        .data = .{ .dir_hook = .{
            .provideFn = provideFn,
            .findFn = findFn,
            .listFn = listFn,
            .removeFn = removeFn,
        } },
    }, proc.id, options.*));
}

// remove(path_c: [*:0]const u8) Result(void) // fixme: Kernel panic if null pointer
fn remove(trap_frame: *trap.Frame) void {
    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);
    const path = std.mem.sliceTo(path_c, 0);
    const dirname = std.fs.path.dirnamePosix(path) orelse "/";

    if (vfs.find(dirname)) |inode| {
        const basename = std.fs.path.basenamePosix(path);
        removeFrom(trap_frame, inode, basename);
    } else {
        sysexchange.frameReturn(void, trap_frame, vfs.Error.NotFound);
    }
}

fn removeFrom(trap_frame: *trap.Frame, inode: *vfs.Inode, basename: []const u8) void {
    switch (inode.resource.tag) {
        .dir => {
            const dir = inode.resource.data.dir;
            sysexchange.frameReturn(null, trap_frame, dir.remove(basename));
        },
        .dir_hook => {
            const dir_hook = inode.resource.data.dir_hook;

            const removeFn = dir_hook.removeFn orelse {
                sysexchange.frameReturn(void, trap_frame, vfs.Error.RemoveNotSupported);
                return;
            };
            const result = removeFn(basename.ptr, basename.len);
            sysexchange.frameReturnResult(void, trap_frame, result);
        },
        else => sysexchange.frameReturn(void, trap_frame, vfs.Error.NotADirectory),
    }
}

// read(handle: usize, buffer: [*]u8, len: usize) Result(usize)
fn read(proc: *process.Info, trap_frame: *trap.Frame) void {
    paging.setUserMemoryAccess(true);
    defer paging.setUserMemoryAccess(false);

    const handle = trap_frame.general_purpose_registers[10];
    const buffer: [*]u8 = @ptrFromInt(trap_frame.general_purpose_registers[11]);
    const len = trap_frame.general_purpose_registers[12];

    const rd = proc.rds.get(handle) orelse {
        sysexchange.frameReturn(usize, trap_frame, process.Error.BadRdHandle);
        return;
    };

    const result = rd.read(proc, buffer[0..len]) catch |err| blk: {
        if (err == vfs.Error.Orphaned) {
            proc.destroyRdHandle(handle);
        }

        break :blk err;
    };
    sysexchange.frameReturn(usize, trap_frame, result);
}

// write(handle: usize, bytes: [*]const u8, len: usize) Result(usize)
fn write(proc: *process.Info, trap_frame: *trap.Frame) void {
    paging.setUserMemoryAccess(true);
    defer paging.setUserMemoryAccess(false);

    const handle = trap_frame.general_purpose_registers[10];
    const bytes: [*]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[11]);
    const len = trap_frame.general_purpose_registers[12];

    const rd = proc.rds.get(handle) orelse {
        sysexchange.frameReturn(usize, trap_frame, process.Error.BadRdHandle);
        return;
    };

    const result = rd.write(proc, bytes[0..len]) catch |err| blk: {
        if (err == vfs.Error.Orphaned) {
            proc.destroyRdHandle(handle);
        }

        break :blk err;
    };
    sysexchange.frameReturn(usize, trap_frame, result);
}

// list(path_c: [*:0]const u8, entries: [*]vfs.DirEntry, len: usize) Result(usize)
fn list(proc: *process.Info, trap_frame: *trap.Frame) void {
    // fixme: Kernel panic if null pointer
    const path_c: [*:0]const u8 = @ptrFromInt(trap_frame.general_purpose_registers[10]);
    // fixme: Kernel panic if null pointer
    const entries: [*]vfs.DirEntry = @ptrFromInt(trap_frame.general_purpose_registers[11]);
    const len = trap_frame.general_purpose_registers[12];

    const result = vfs.listZ(proc, path_c, entries[0..len]);
    sysexchange.frameReturn(null, trap_frame, result);
}

// terminate() void
fn terminate(proc: *process.Info) void {
    proc.terminate();
    process.schedule() catch |err| {
        std.debug.panic("Unable to schedule because all processes are terminated: {}", .{err});
    };
}

// processId() usize
fn processId(proc: *process.Info) usize {
    return proc.id;
}