summaryrefslogtreecommitdiff
path: root/Kernel/Syscall.h
blob: d18aa803b76998c0eac6949ada1b3addc473fe9a (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
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
121
122
123
124
125
126
127
128
129
130
131
132
#pragma once

#include <AK/Types.h>

#define ENUMERATE_SYSCALLS \
    __ENUMERATE_SYSCALL(sleep) \
    __ENUMERATE_SYSCALL(yield) \
    __ENUMERATE_SYSCALL(putch) \
    __ENUMERATE_SYSCALL(open) \
    __ENUMERATE_SYSCALL(close) \
    __ENUMERATE_SYSCALL(read) \
    __ENUMERATE_SYSCALL(lseek) \
    __ENUMERATE_SYSCALL(kill) \
    __ENUMERATE_SYSCALL(getuid) \
    __ENUMERATE_SYSCALL(exit) \
    __ENUMERATE_SYSCALL(getgid) \
    __ENUMERATE_SYSCALL(getpid) \
    __ENUMERATE_SYSCALL(waitpid) \
    __ENUMERATE_SYSCALL(mmap) \
    __ENUMERATE_SYSCALL(munmap) \
    __ENUMERATE_SYSCALL(get_dir_entries) \
    __ENUMERATE_SYSCALL(lstat) \
    __ENUMERATE_SYSCALL(getcwd) \
    __ENUMERATE_SYSCALL(gettimeofday) \
    __ENUMERATE_SYSCALL(gethostname) \
    __ENUMERATE_SYSCALL(get_arguments) \
    __ENUMERATE_SYSCALL(chdir) \
    __ENUMERATE_SYSCALL(uname) \
    __ENUMERATE_SYSCALL(set_mmap_name) \
    __ENUMERATE_SYSCALL(readlink) \
    __ENUMERATE_SYSCALL(write) \
    __ENUMERATE_SYSCALL(ttyname_r) \
    __ENUMERATE_SYSCALL(stat) \
    __ENUMERATE_SYSCALL(get_environment) \
    __ENUMERATE_SYSCALL(getsid) \
    __ENUMERATE_SYSCALL(setsid) \
    __ENUMERATE_SYSCALL(getpgid) \
    __ENUMERATE_SYSCALL(setpgid) \
    __ENUMERATE_SYSCALL(getpgrp) \
    __ENUMERATE_SYSCALL(tcsetpgrp) \
    __ENUMERATE_SYSCALL(tcgetpgrp) \
    __ENUMERATE_SYSCALL(fork) \
    __ENUMERATE_SYSCALL(execve) \
    __ENUMERATE_SYSCALL(geteuid) \
    __ENUMERATE_SYSCALL(getegid) \
    __ENUMERATE_SYSCALL(signal) \
    __ENUMERATE_SYSCALL(isatty) \
    __ENUMERATE_SYSCALL(getdtablesize) \
    __ENUMERATE_SYSCALL(dup) \
    __ENUMERATE_SYSCALL(dup2) \
    __ENUMERATE_SYSCALL(sigaction) \
    __ENUMERATE_SYSCALL(getppid) \
    __ENUMERATE_SYSCALL(umask) \
    __ENUMERATE_SYSCALL(getgroups) \
    __ENUMERATE_SYSCALL(setgroups) \
    __ENUMERATE_SYSCALL(sigreturn) \
    __ENUMERATE_SYSCALL(sigprocmask) \
    __ENUMERATE_SYSCALL(sigpending) \
    __ENUMERATE_SYSCALL(pipe) \
    __ENUMERATE_SYSCALL(killpg) \
    __ENUMERATE_SYSCALL(setuid) \
    __ENUMERATE_SYSCALL(setgid) \
    __ENUMERATE_SYSCALL(alarm) \
    __ENUMERATE_SYSCALL(fstat) \
    __ENUMERATE_SYSCALL(access) \


#define DO_SYSCALL_A0(function) Syscall::invoke((dword)(function))
#define DO_SYSCALL_A1(function, arg1) Syscall::invoke((dword)(function), (dword)(arg1))
#define DO_SYSCALL_A2(function, arg1, arg2) Syscall::invoke((dword)(function), (dword)(arg1), (dword)(arg2))
#define DO_SYSCALL_A3(function, arg1, arg2, arg3) Syscall::invoke((dword)(function), (dword)(arg1), (dword)(arg2), (dword)arg3)

namespace Syscall {

enum Function {
#undef __ENUMERATE_SYSCALL
#define __ENUMERATE_SYSCALL(x) SC_ ##x ,
    ENUMERATE_SYSCALLS
#undef __ENUMERATE_SYSCALL
};

inline constexpr const char* toString(Function function)
{
    switch (function) {
#undef __ENUMERATE_SYSCALL
#define __ENUMERATE_SYSCALL(x) case SC_ ##x: return #x;
    ENUMERATE_SYSCALLS
#undef __ENUMERATE_SYSCALL
    }
    return "Unknown";
}

struct SC_mmap_params {
    uint32_t addr;
    uint32_t size;
    int32_t prot;
    int32_t flags;
    int32_t fd;
    int32_t offset; // FIXME: 64-bit off_t?
};

void initialize();

inline dword invoke(dword function)
{
    dword result;
    asm volatile("int $0x80":"=a"(result):"a"(function):"memory");
    return result;
}

inline dword invoke(dword function, dword arg1)
{
    dword result;
    asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1):"memory");
    return result;
}

inline dword invoke(dword function, dword arg1, dword arg2)
{
    dword result;
    asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2):"memory");
    return result;
}

inline dword invoke(dword function, dword arg1, dword arg2, dword arg3)
{
    dword result;
    asm volatile("int $0x80":"=a"(result):"a"(function),"d"(arg1),"c"(arg2),"b"(arg3):"memory");
    return result;
}

}