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
|
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
const std = @import("std");
const Console = @import("Console.zig");
const instructions = @import("instructions.zig");
const paging = @import("paging.zig");
const process = @import("process.zig");
const syscall = @import("syscall.zig");
const sysexchange = @import("sysexchange.zig");
const userinit = @import("userinit.zig");
const vfs = @import("vfs.zig");
const Result = sysexchange.Result;
const io = std.io;
const tar = std.tar;
const File = tar.Iterator(io.FixedBufferStream([]const u8).Reader).File;
const iofs = struct {
const debug = struct {
fn write(ptr: [*]const u8, len: usize) callconv(.C) Result(usize) {
// Raise page fault if applicable.
if (len > 0) {
std.mem.doNotOptimizeAway(ptr[0]);
}
const procmem: *paging.Table = @ptrFromInt(instructions.satp.read().ppn << 12);
const paddr = procmem.translate(@intFromPtr(ptr)).?;
const pptr: [*]const u8 = @ptrFromInt(paddr);
const bytes = pptr[0..len];
const console = Console.autoChoose() orelse {
return Result(usize).fromAnyTypeOrError(Error.NoConsole);
};
const w = console.writer();
w.print("{s}", .{bytes}) catch |err| {
return Result(usize).fromAnyTypeOrError(err);
};
return Result(usize).fromAnyTypeOrError(bytes.len);
}
};
};
pub const Error = error{
NoTarFileInitializer,
NoConsole,
};
pub fn provideBuiltin() !void {
try addDir("/io");
try addDir("/userinit");
try provideConsole();
try provideUserinit();
}
fn provideConsole() !void {
try vfs.provideResource("/io/debug", .{
.tag = .stream,
.data = .{
.stream = .{
.readFn = null,
.writeFn = iofs.debug.write,
},
},
}, 0);
}
fn provideUserinit() !void {
var userinit_stream = io.fixedBufferStream(userinit.tarball);
const reader = userinit_stream.reader();
var file_name_buffer: [4086]u8 = undefined;
var link_name_buffer: [4086]u8 = undefined;
var path_buffer: [4096]u8 = undefined;
var it = tar.iterator(reader, .{
.file_name_buffer = file_name_buffer[0..],
.link_name_buffer = link_name_buffer[0..],
});
while (try it.next()) |file| {
const path = try std.fmt.bufPrint(path_buffer[0..], "/userinit/{s}", .{file.name});
switch (file.kind) {
.file => try addFile(path, file),
.directory => try addDir(path),
.sym_link => {},
}
}
}
fn addFile(path: []const u8, file: File) !void {
const allocator = vfs.treeRoot().allocator;
const initializer = try allocator.create(File);
initializer.* = file;
try vfs.provideResource(path, .{
.tag = .file,
.data = .{
.file = .{
.openFn = open,
.readFn = read,
.writeFn = null,
.closeFn = close,
.initializer = initializer,
},
},
}, 0);
}
fn addDir(path: []const u8) !void {
const allocator = vfs.treeRoot().allocator;
const tree = try allocator.create(vfs.Tree);
tree.* = vfs.Tree.init(allocator);
try vfs.provideResource(path, .{
.tag = .dir,
.data = .{ .dir = tree },
}, 0);
}
fn open(initializer: ?*anyopaque, _: u16) callconv(.C) Result(*anyopaque) {
const file_template: *File = @alignCast(@ptrCast(initializer orelse {
return Result(*anyopaque).fromAnyTypeOrError(Error.NoTarFileInitializer);
}));
const allocator = vfs.treeRoot().allocator;
const context = allocator.create(File) catch |err| {
return Result(*anyopaque).fromAnyTypeOrError(err);
};
context.* = file_template.*;
return Result(*anyopaque).fromAnyTypeOrError(context);
}
fn read(context: *anyopaque, ptr: [*]u8, len: usize) callconv(.C) Result(usize) {
const ctx: *File = @alignCast(@ptrCast(context));
const buffer = ptr[0..len];
return Result(usize).fromAnyTypeOrError(ctx.read(buffer));
}
fn close(context: *anyopaque) callconv(.C) void {
const ctx: *File = @alignCast(@ptrCast(context));
const allocator = vfs.treeRoot().allocator;
allocator.destroy(ctx);
}
|