/
syscalls.c
205 lines (166 loc) · 4.53 KB
/
syscalls.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "syscalls.h"
#include "x86.h"
#include "kernel/interrupt.h"
#include <errno.h>
#undef errno
extern int32_t errno;
#include "dev/console.h"
#include "kernel/process.h"
#include "kernel/heap.h"
void syscall_init()
{
syscall_table[SYS_exit ] = sys_exit;
syscall_table[SYS_close ] = sys_close;
syscall_table[SYS_open ] = sys_open;
syscall_table[SYS_read ] = sys_read;
syscall_table[SYS_write ] = sys_write;
syscall_table[SYS_lseek ] = sys_lseek;
syscall_table[SYS_link ] = sys_link;
syscall_table[SYS_unlink ] = sys_unlink;
syscall_table[SYS_getpid ] = sys_getpid;
syscall_table[SYS_fork ] = sys_fork;
syscall_table[SYS_kill ] = sys_kill;
syscall_table[SYS_execve ] = sys_execve;
syscall_table[SYS_sbrk ] = sys_sbrk;
syscall_table[SYS_stat ] = sys_stat;
syscall_table[SYS_fstat ] = sys_fstat;
syscall_table[SYS_isatty ] = sys_isatty;
syscall_table[SYS_times ] = sys_times;
syscall_table[SYS_wait ] = sys_wait;
_install_isr(INT_VEC_SYSCALL, syscall_isr);
}
// kernel system call implementations
int32_t sys_exit(context_t *c, uint32_t *args)
{
kill_process(current_proc->pid);
current_proc = NULL;
dispatch();
}
int32_t sys_close(context_t *c, uint32_t *args)
{
c_printf("unimplemented call close\n");
c->eax = -1;
}
int32_t sys_open(context_t *c, uint32_t *args)
{
// this is going to implement VFS open()
char *filename = (char *) args[0];
int flags = (int) args[1];
c_printf("> open(%s, %d)\n", filename, flags);
// now well have a filename and flags, have vfs_open find the inode
vfs_open(filename, flags);
c->eax = -1;
}
int32_t sys_read(context_t *c, uint32_t *args)
{
int32_t fd = args[0];
unsigned char *buf = (unsigned char *) args[1];
int32_t len = args[2];
// how should this be done? can we wait for keypresses in a syscall?
// need to clear bss
// what?
c_printf("read(%d, 0x%x, %d)\n", fd, buf, len);
// for (i = 0; i < len; ++i)
// buf[i] = c_getcode();
int32_t i = vfs_read(fd, buf, len);
c->eax = i;
}
int32_t sys_write(context_t *c, uint32_t *args)
{
// use the console driver here
int32_t file = args[0];
char *p = (char *) args[1];
int32_t len = args[2];
//c_printf("write(%d, 0x%x, %d) ", file, p, len);
c_write(p, len);
c->eax = len;
}
int32_t sys_lseek(context_t *c, uint32_t *args)
{
c_printf("unimplemented call lseek\n");
c->eax = 0;
}
int32_t sys_link(context_t *c, uint32_t *args)
{
c_printf("unimplemented call link\n");
errno = EMLINK;
c->eax = -1;
}
int32_t sys_unlink(context_t *c, uint32_t *args)
{
c_printf("unimplemented call unlink\n");
errno = ENOENT;
c->eax = -1;
}
int32_t sys_getpid(context_t *c, uint32_t *args)
{
c->eax = current_proc->pid;
}
int32_t sys_fork(context_t *c, uint32_t *args)
{
c_printf("unimplemented call fork\n");
errno = EAGAIN;
c->eax = -1;
}
int32_t sys_kill(context_t *c, uint32_t *args)
{
c_printf("unimplemented call kill\n");
errno = EINVAL;
c->eax = -1;
}
int32_t sys_execve(context_t *c, uint32_t *args)
{
c_printf("unimplemented call execve\n");
errno = ENOMEM;
c->eax = -1;
}
// increases program data space (needed by malloc)
int32_t sys_sbrk(context_t *c, uint32_t *args)
{
uint32_t prev_brk = k_heap->end;
heap_expand(args[0], k_heap);
c->eax = prev_brk;
}
int32_t sys_stat(context_t *c, uint32_t *args)
{
//c_printf("stat(%d, 0x%x)\n", args[0], args[1]);
struct stat *st = (struct stat *) args[1];
st->st_mode = S_IFCHR;
c->eax = 0;
}
int32_t sys_fstat(context_t *c, uint32_t *args)
{
//c_printf("fstat(%d,0x%x)\n", args[0], args[1]);
struct stat *st = (struct stat *) args[1];
st->st_mode = S_IFCHR;
c->eax = 0;
}
int32_t sys_isatty(context_t *c, uint32_t *args)
{
c_printf("isatty(%d)\n", args[0]);
c->eax = 1;
}
int32_t sys_times(context_t *c, uint32_t *args)
{
c_printf("unimplemented call times\n");
c->eax = -1;
}
int32_t sys_wait(context_t *c, uint32_t *args)
{
c_printf("unimplemented call wait\n");
errno = ECHILD;
c->eax = -1;
}
// do it like linux, use int32_t 80h
void syscall_isr(int32_t vector, int32_t code)
{
// from here call the actual system call
int32_t call_num = current_proc->context->eax;
uint32_t *args = ((uint32_t *)(current_proc->context + 1)) + 1;
// Invoke the handler. The first argument is the process
// context; the second is the address of the first user
// syscall argument, which is the second longword following
// the context save area on the stack.
(*syscall_table[call_num])(current_proc->context, args);
__outb( PIC_MASTER_CMD_PORT, PIC_EOI );
}