summaryrefslogtreecommitdiff
path: root/src/kernel/execbuf.c
blob: 71fc6626a5c9d54c8240dfd271a1d1cb48548ffc (plain)
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
#include <camellia/execbuf.h>
#include <camellia/syscalls.h>
#include <kernel/execbuf.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <shared/mem.h>

_Noreturn static void halt(Proc *proc) {
	kfree(proc->execbuf.buf);
	proc->execbuf.buf = NULL;
	proc_switch_any();
}

static void try_fetch(Proc *proc, uint64_t *buf, size_t amt) {
	size_t bytes = amt * sizeof(uint64_t);
	if (proc->execbuf.pos + bytes > proc->execbuf.len)
		halt(proc);
	memcpy(buf, proc->execbuf.buf + proc->execbuf.pos, bytes);
	proc->execbuf.pos += bytes;
}

_Noreturn void execbuf_run(Proc *proc) {
	uint64_t buf[6];

	for (;;) {
		assert(proc == proc_cur); /* idiotic, but needed because of _syscall. */
		assert(proc->state == PS_RUNNING);
		assert(proc->execbuf.buf);

		try_fetch(proc, buf, 1);
		switch (buf[0]) {
			case EXECBUF_SYSCALL:
				try_fetch(proc, buf, 6);
				_syscall(buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
				proc_switch_any();

			case EXECBUF_JMP:
				try_fetch(proc, buf, 1);
				proc->regs.rip = buf[0];
				break;

			default:
				halt(proc);
		}
	}
}