aboutsummaryrefslogtreecommitdiff
path: root/kernel/features/syscall.c
blob: 4197985fd1176a4f23ad5678938b007942210c7f (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
// MIT License, Copyright (c) 2020 Marvin Borner

#include <cpu.h>
#include <event.h>
#include <fs.h>
#include <interrupts.h>
#include <load.h>
#include <mem.h>
#include <print.h>
#include <proc.h>
#include <str.h>
#include <sys.h>
#include <timer.h>

void syscall_handler(struct regs *r)
{
	enum sys num = r->eax;
	r->eax = 0;

	if (num != SYS_RECEIVE && num != SYS_YIELD)
		printf("[SYSCALL] %d: ", num);

	switch (num) {
	case SYS_LOOP: {
		printf("loop\n");
		loop();
		break;
	}
	case SYS_MALLOC: {
		printf("malloc\n");
		r->eax = (u32)malloc(r->ebx);
		break;
	}
	case SYS_FREE: {
		printf("free\n");
		free(r->eax);
		break;
	}
	case SYS_READ: {
		printf("read\n");
		r->eax = (u32)read_file((char *)r->ebx);
		break;
	}
	case SYS_WRITE: {
		printf("write\n");
		// TODO: Write ext2 support
		break;
	}
	case SYS_EXEC: {
		printf("exec\n");
		char *path = (char *)r->ebx;
		struct proc *proc = proc_make();
		r->eax = bin_load(path, proc);
		int argc = 3; // TODO: Add argc evaluator
		char **argv = malloc(sizeof(*argv) * (argc + 1));
		argv[0] = (char *)r->ecx;
		argv[1] = (char *)r->edx;
		argv[2] = (char *)r->esi;
		argv[3] = (char *)r->edi;
		argv[4] = NULL;
		((u32 *)proc->regs.useresp)[0] = argc;
		((u32 *)proc->regs.useresp)[1] = (u32)argv;
		if (r->eax)
			proc_exit(proc, r->eax);
		break;
	}
	case SYS_EXIT: {
		printf("exit\n");
		proc_exit(proc_current(), r->ebx);
		break;
	}
	case SYS_YIELD: {
		/* printf("yield\n"); */
		proc_yield();
		break;
	}
	case SYS_TIME: {
		printf("time\n");
		r->eax = timer_get();
		break;
	}
	case SYS_MAP: {
		printf("map\n");
		event_map(r->ebx, proc_current(), (u32 *)r->ecx);
		break;
	}
	case SYS_UNMAP: {
		printf("unmap\n");
		event_unmap(r->ebx, proc_current(), (u32 *)r->ecx);
		break;
	}
	case SYS_RESOLVE: {
		printf("resolve\n");
		proc_resolve(proc_current());
		break;
	}
	case SYS_SEND: {
		printf("send\n");
		proc_send(proc_current(), proc_from_pid(r->ebx), r->ecx, (void *)r->edx);
		break;
	}
	case SYS_RECEIVE: {
		/* printf("receive\n"); */
		struct proc_message *msg = proc_receive(proc_current());
		r->eax = (u32)(msg ? msg->msg : NULL);
		break;
	}
	default: {
		printf("unknown\n");
		loop();
		break;
	}
	}
}

void syscall_init()
{
	idt_set_gate(0x80, (u32)isr128, 0x08, 0x8E);
	isr_install_handler(0x80, syscall_handler);
}