aboutsummaryrefslogtreecommitdiff
path: root/kernel/features/syscall.c
blob: 9d5ac825036408c94b9c6a190bae9d7173b0bf61 (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
// 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>

void syscall_handler(struct regs *r)
{
	enum sys num = r->eax;
	r->eax = 0;
	printf("[SYSCALL] %d\n", num);

	switch (num) {
	case SYS_LOOP: {
		loop();
		break;
	}
	case SYS_MALLOC: {
		r->eax = (u32)malloc(r->eax);
		break;
	}
	case SYS_FREE: {
		free(r->eax);
		break;
	}
	case SYS_READ: {
		r->eax = (u32)read_file((char *)r->ebx);
		break;
	}
	case SYS_WRITE: {
		// TODO: Write ext2 support
		break;
	}
	case SYS_EXEC: {
		char *path = (char *)r->ebx;
		struct proc *proc = proc_make();
		((u32 *)proc->regs.esp)[0] = r->ecx;
		((u32 *)proc->regs.esp)[1] = r->edx;
		((u32 *)proc->regs.esp)[2] = r->esi;
		((u32 *)proc->regs.esp)[3] = r->edi;
		r->eax = bin_load(path, proc);
		break;
	}
	case SYS_EXIT: {
		proc_exit();
		break;
	}
	case SYS_MAP: {
		event_map(r->ebx, (u32 *)r->ecx);
		break;
	}
	case SYS_UNMAP: {
		event_unmap(r->ebx, (u32 *)r->ecx);
		break;
	}
	default: {
		printf("Unknown syscall!\n");
		loop();
		break;
	}
	}
}

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