-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyscall_handler.c
More file actions
executable file
·116 lines (106 loc) · 2.81 KB
/
syscall_handler.c
File metadata and controls
executable file
·116 lines (106 loc) · 2.81 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
//
// Created by dcat on 7/7/17.
//
#include <str.h>
#include <exec.h>
#include <wait.h>
#include <signal.h>
#include <exit.h>
#include <umalloc.h>
#include <iov.h>
#include <ioctl.h>
#include <brk.h>
#include <fcntl.h>
#include <errno.h>
#include <open.h>
#include <stat.h>
#include <getdents.h>
#include <readlink.h>
#include "idt.h"
#include "proc.h"
#include "syscall_handler.h"
#include "timer.h"
#include "syscall_names.h"
long errno;
long helloworld(uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi, regs_t *r) {
dprintf("fastdump:");
dump_regs(r);
return 0;
}
long screen_print(const char *str) {
putf_const("");//去掉这行下面就输出乱码....幬
for (int x = 0; str[x] != '\0' && x < 0xFFFF; x++) {
putc(str[x]);
}
return 0;
}
long fork_s(uint32_t ebx, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi, regs_t *r) {
return fork(r);
}
long hello_switcher(pid_t pid, uint32_t ecx, uint32_t edx, uint32_t esi, uint32_t edi, regs_t *r) {
cli();
save_proc_state(getpcb(getpid()), r);//so silly the cat...
sti();
switch_to_proc(getpcb(pid));
}
void *syscalls_table[] = {
&helloworld,
&screen_print,
&delay,
&fork_s,
&getpid,
&hello_switcher,
&sys_exit,
&sys_open,
&sys_close,
&sys_read,
&sys_write,
&sys_stat,
&sys_ls,
&sys_exec,
&sys_waitpid,
&sys_kill,
&sys_access,
&sys_chdir,
&sys_getcwd,
&sys_lseek,
&sys_malloc,
&sys_free,
&sys_dup3,
&sys_readv,
&sys_writev,
&sys_stat64,
&sys_ioctl,
&sys_brk,
&sys_execve,
&sys_signal,
&sys_sigaction,
&sys_wait4,
&sys_fcntl,
&sys_getdents,
&sys_readlink,
&sys_readlinkat
};
uint32_t syscalls_count = sizeof(syscalls_table) / sizeof(uint32_t);
void syscall_install() {
extern void _isr_syscall();
extern void _isr_taskswitch();
idt_set_gate(0x60, (unsigned) _isr_syscall, 1 << 3, 0xEF);
idt_set_gate(0x61, (unsigned) _isr_taskswitch, 1 << 3, 0xEF);
}
typedef uint32_t (*syscall_fun_t)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, regs_t *r);
int syscall_handler(regs_t *r) {
dprintf("syscall[%d][%s] a0:%x a1:%x a2:%x", r->eax,
r->eax > SYSCALL_NAMES_COUNT ? "UNDEF" : SYSCALL_NAMES_TABLE[r->eax],
r->ebx, r->ecx, r->edx);
if (r->eax >= syscalls_count || syscalls_table[r->eax] == NULL) {
dwprintf("syscall not found:%d", r->eax);
r->eax = (uint32_t) -ENOSYS;
return 0;
}
syscall_fun_t fun = (syscall_fun_t) syscalls_table[r->eax];
//A better implement?
uint32_t ret = fun(r->ebx, r->ecx, r->edx, r->esi, r->edi, r);
r->eax = ret;
return 0;
}