-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsyscalls.c
More file actions
119 lines (100 loc) · 3.09 KB
/
Copy pathsyscalls.c
File metadata and controls
119 lines (100 loc) · 3.09 KB
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
/*
* sycalls.c
*
* Created on: 17.07.2012
* Author: pascal
*/
#include "syscalls.h"
#include "stddef.h"
#include "stdbool.h"
#include "isr.h"
#include "cmos.h"
#include "pm.h"
#include "vfs.h"
#include "loader.h"
#include "pit.h"
#include "system.h"
#include "cpu.h"
#include "scheduler.h"
#include "cleaner.h"
#include "assert.h"
#include <bits/syscall_numbers.h>
#define STAR 0xC0000081
#define LSTAR 0xC0000082
#define CSTAR 0xC0000083
#define SFMASK 0xC0000084
extern void isr_syscall();
static void nop();
static uint64_t createThreadHandler(void *entry, void *arg);
static void exitThreadHandler();
static void sleepHandler(uint64_t msec);
typedef uint64_t(*syscall)(uint64_t arg, ...);
static syscall syscalls[_SYSCALL_NUM] = {
[SYSCALL_ALLOC_PAGES] (syscall)&mm_Alloc,
[SYSCALL_FREE_PAGES] (syscall)&mm_Free,
[SYSCALL_UNUSE_PAGES] (syscall)&vmm_unusePages,
[SYSCALL_EXEC] (syscall)&loader_syscall_load,
[SYSCALL_EXIT] (syscall)&pm_syscall_exit,
[SYSCALL_WAIT] (syscall)&pm_syscall_wait,
[SYSCALL_THREAD_CREATE] (syscall)&createThreadHandler,
[SYSCALL_THREAD_EXIT] (syscall)&exitThreadHandler,
[SYSCALL_GET_TIMESTAMP] (syscall)&cmos_syscall_timestamp,
[SYSCALL_SLEEP] (syscall)&sleepHandler,
[SYSCALL_OPEN] (syscall)&vfs_syscall_open,
[SYSCALL_CLOSE] (syscall)&vfs_syscall_close,
[SYSCALL_READ] (syscall)&vfs_syscall_read,
[SYSCALL_WRITE] (syscall)&vfs_syscall_write,
[SYSCALL_INFO_GET] (syscall)&vfs_syscall_getFileinfo,
[SYSCALL_INFO_SET] (syscall)&vfs_syscall_setFileinfo,
[SYSCALL_TRUNCATE] (syscall)&vfs_syscall_truncate,
[SYSCALL_MKDIR] (syscall)&vfs_syscall_mkdir,
[SYSCALL_MOUNT] (syscall)&vfs_syscall_mount,
[SYSCALL_UNMOUNT] (syscall)&vfs_syscall_unmount,
[SYSCALL_SYSINF_GET] (syscall)&getSystemInformation
};
void syscall_Init()
{
//Prüfen, ob syscall/sysret unterstützt wird
if(cpuInfo.syscall)
{
cpu_MSRwrite(STAR, (0x8ul << 32) | (0x13ul << 48)); //Segementregister
cpu_MSRwrite(LSTAR, (uintptr_t)isr_syscall); //Einsprungspunkt
cpu_MSRwrite(SFMASK, 1ul << 9); //Wir setzen das interrupt enable bit zurück, wir müssen nämlich den Stack switchen
//Syscall-Instruktion aktivieren (ansonsten #UD)
//Bit 0
cpu_MSRwrite(0xC0000080, cpu_MSRread(0xC0000080) | 1);
}
for(size_t i = 0; i < _SYSCALL_NUM; i++)
{
if(syscalls[i] == NULL)
syscalls[i] = (syscall)nop;
}
}
uint64_t syscall_syscallHandler(uint64_t func, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
{
//FIXME
assert(func < _SYSCALL_NUM);
return syscalls[func](arg1, arg2, arg3, arg4, arg5);
}
static void nop()
{
asm volatile("nop");
}
static uint64_t createThreadHandler(void *entry, void *arg)
{
ERROR_TYPE_POINTER(thread_t) thread_ret = thread_create(currentProcess, entry, sizeof(void*), &arg, false);
if(ERROR_DETECT(thread_ret))
return 0; //TODO: return error to userspace
thread_t *thread = ERROR_GET_VALUE(thread_ret);
thread_unblock(thread);
return thread->tid;
}
static void exitThreadHandler()
{
cleaner_cleanThread(currentThread);
yield();
}
static void sleepHandler(uint64_t msec)
{
pit_RegisterTimer(currentThread, msec);
}