diff options
Diffstat (limited to 'targets')
| -rw-r--r-- | targets/kernel_touch.c | 10 | ||||
| -rw-r--r-- | targets/kernel_vminit.zig | 44 |
2 files changed, 52 insertions, 2 deletions
diff --git a/targets/kernel_touch.c b/targets/kernel_touch.c index b2db1ce..c0399a9 100644 --- a/targets/kernel_touch.c +++ b/targets/kernel_touch.c @@ -1,7 +1,8 @@ // One of everything the kernel can report about a program, in this order: // fresh pages touched one by one (argv[1] of them, default 64), a global -// written and a function called (argv[2] times, default 100), a file opened -// and written, a sleep, a thread that calls the function three more times. +// written and a function called (argv[2] times, default 100), a block +// allocated, a file opened and written, a sleep, a thread that calls the +// function three more times. #include <fcntl.h> #include <pthread.h> #include <stdlib.h> @@ -27,11 +28,16 @@ int main(int argc, char **argv) { unsigned long steps = argc > 2 ? strtoul(argv[2], 0, 0) : 100; volatile char *fresh = mmap(0, pages * 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + // A page a fault, however many: no huge ones. + madvise((void *)fresh, pages * 4096, MADV_NOHUGEPAGE); for (unsigned long i = 0; i < pages; i++) fresh[i * 4096] = 1; unsigned long acc = 0; for (unsigned long i = 0; i < steps; i++) acc = step(i, acc); + void *volatile block = malloc(1000 + steps); + free(block); + int fd = open("/dev/null", O_WRONLY); if (write(fd, "hello", 5) != 5) return 1; close(fd); diff --git a/targets/kernel_vminit.zig b/targets/kernel_vminit.zig new file mode 100644 index 0000000..a539689 --- /dev/null +++ b/targets/kernel_vminit.zig @@ -0,0 +1,44 @@ +//! /init of the virtual machine `zig build test-kernel-vm` boots: this +//! machine's kernel, where we are root and BPF may be loaded. Mounts /proc, +//! /sys and /dev, runs each line of /commands, powers off. +const std = @import("std"); +const linux = std.os.linux; + +fn say(text: []const u8) void { + _ = linux.write(1, text.ptr, text.len); +} + +pub fn main() void { + _ = linux.mount("proc", "/proc", "proc", 0, 0); + _ = linux.mount("sysfs", "/sys", "sysfs", 0, 0); + _ = linux.mount("devtmpfs", "/dev", "devtmpfs", 0, 0); + var commands: [4096]u8 = undefined; + const fd = linux.open("/commands", .{}, 0); + const len = linux.read(@intCast(fd), &commands, commands.len); + if (linux.errno(len) != .SUCCESS) say("init: no /commands\n"); + var lines = std.mem.tokenizeScalar(u8, commands[0..if (linux.errno(len) == .SUCCESS) len else 0], '\n'); + while (lines.next()) |line| { + say("$ "); + say(line); + say("\n"); + // The words, each ended by a 0, in a copy. + var text: [512]u8 = @splat(0); + @memcpy(text[0..line.len], line); + var argv: [32:null]?[*:0]const u8 = @splat(null); + var words = std.mem.tokenizeScalar(u8, line, ' '); + var count: usize = 0; + while (words.next()) |word| : (count += 1) { + const at = word.ptr - line.ptr; + text[at + word.len] = 0; + argv[count] = @ptrCast(&text[at]); + } + const pid = linux.fork(); + if (pid == 0) { + _ = linux.execve(argv[0].?, &argv, &[_:null]?[*:0]const u8{}); + linux.exit_group(127); + } + var status: u32 = 0; + _ = linux.waitpid(@intCast(pid), @ptrCast(&status), 0); + } + _ = linux.reboot(.MAGIC1, .MAGIC2, .POWER_OFF, null); +} |
