-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyscall_handler.c
executable file
·352 lines (295 loc) · 11.7 KB
/
syscall_handler.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "asm.h"
#include "idt.h"
#include "scheduler/scheduler.h"
#include "virtual_memory/vm.h"
#include "drivers/ata/ata.h"
#include "syscall_handler.h"
#include "debug.h"
#include "ext2/ext2.h"
#define K_STACK 0x1FFFFB
#define SELECT_FS(ext2) if (system.device_desc->num == 0) \
{ \
ext2 = system.root_fs; \
} \
else \
{ \
ext2 = system.scnd_fs; \
} \
void syscall_handler()
{
static int free_vm_proc;
int syscall_num;
unsigned int mem_size;
struct t_process_context* current_process_context;
struct t_processor_reg processor_reg;
int* params;
char data;
unsigned int on_exit_action;
u8 flush_network;
t_ext2* ext2;
SAVE_PROCESSOR_REG
//CLI
//call can come from kernel mode (sleep)
SWITCH_DS_TO_KERNEL_MODE
syscall_num=processor_reg.eax;
on_exit_action=0;
current_process_context=system.process_info->current_process->val;
t_console_desc *console_desc=current_process_context->console_desc;
syscall_num=processor_reg.eax;
params=processor_reg.ecx;
flush_network = 0;
//printk("syscall num = %d \n",syscall_num);
//printk("fg_pgid = %d \n",console_desc->fg_pgid);
switch (syscall_num)
{
case 1:
params[0]=_fork(processor_reg);
break;
case 2:
params[1]=_malloc(params[0]);
break;
case 3:
_free(params[0]);
break;
case 150:
params[1]=_bigMalloc(params[0]);
break;
case 151:
_bigFree(params[0]);
break;
case 4:
_write_char(console_desc,params[0]);
break;
case 5:
data=_read_char(console_desc);
*((char*)params[0])=data;
if (data==NULL)
{
on_exit_action=1;
}
break;
case 6:
_echo_char(console_desc,params[0]);
break;
case 7:
_enable_cursor(console_desc);
break;
case 8:
_disable_cursor(console_desc);
break;
case 9:
_update_cursor(console_desc);
break;
case 10:
_delete_char(console_desc);
break;
case 11:
_pause();
on_exit_action=1;
break;
case 12:
_awake(params[0]);
break;
case 13:
_exit(params[0]);
on_exit_action=2;
break;
case 14:
params[2]=_exec(params[0],params[1]);
break;
case 15:
_sleep_time(params[0]);
on_exit_action=1;
break;
case 18:
//--SELECT_FS(ext2)
if (system.device_desc->num == 0)
{
ext2 = system.root_fs;
}
else
{
ext2 = system.scnd_fs;
}
params[2] = _open(ext2,(char*) params[0],params[1]);
on_exit_action=1;
break;
case 19:
SELECT_FS(ext2)
params[1]=_close(ext2,params[0]);
on_exit_action=1;
break;
case 20:
SELECT_FS(ext2)
params[3]=_read(ext2,params[0],params[1],params[2],1);
on_exit_action=1;
break;
case 36:
SELECT_FS(ext2)
params[3] = _seek(ext2,params[0],params[1],params[2]);
on_exit_action=1;
break;
case 22:
SELECT_FS(ext2)
params[1]=_rm(ext2,(char*)params[0]);
on_exit_action=1;
break;
case 23:
SELECT_FS(ext2)
params[1]=_mkdir(ext2,params[0]);
on_exit_action=1;
break;
case 26:
SELECT_FS(ext2)
params[1]=_chdir(system.root_fs,(char*) params[0]);
on_exit_action=1;
break;
case 27:
SELECT_FS(ext2)
params[2]=_stat(system.root_fs,(char*) params[0],params[1]);
break;
case 28:
params[1]=_open_socket(params[0]);
break;
case 29:
params[5]=_bind(params[0],params[1],params[2],params[3],params[4]);
break;
case 30:
//printk("redc ");
params[5]=_recvfrom(params[0],params[1],params[2],params[3],params[4]);
break;
case 31:
params[5]=_sendto(params[0],params[1],params[2],params[3],params[4]);
break;
case 32:
params[0]=_close_socket(params[0]);
break;
case 33:
params[1]=_listen(params[0]);
break;
case 34:
params[3]=_accept(params[0]);
break;
case 35:
params[3]=_connect(params[0],params[1],params[2]);
break;
case 37:
params[3] = _icmp_echo_request(params[0],params[1],params[2]);
break;
case 38:
params[0] = _getpid();
break;
case 39:
params[1] = _getpgid(params[0]);
break;
case 40:
params[2] = _setpgid(params[0],params[1]);
break;
case 41:
params[0] = _tcgetpgrp();
break;
case 42:
params[1] = _tcsetpgrp(params[0]);
break;
case 101:
on_exit_action=1;
break;
case 106:
params[1]=_listen(params[0]);
break;
case 107:
_signal(params[0]);
break;
case 108:
_select_dev(params[0]);
break;
case 109:
SELECT_FS(ext2)
params[4] = _read_write(ext2, params[0], params[1], params[2], params[3], 1);
on_exit_action = 1;
break;
case 103:
check_free_mem();
break;
case 104:
panic();
break;
case 105:
check_open_conn();
break;
case 200:
flush_inode_cache(system.root_fs);
break;
default:
panic();
}
if (syscall_num == 30 || syscall_num == 31 || syscall_num == 32 || syscall_num == 35)
{
system.flush_network = 1;
}
//EXIT_INT_HANDLER(on_exit_action,processor_reg)
static struct t_process_context _current_process_context;
static struct t_process_context _old_process_context;
static struct t_process_context _new_process_context;
static struct t_processor_reg _processor_reg;
static unsigned int _action2;
static u8 stop = 0;
CLI
if (system.int_path_count == 0 && system.force_scheduling == 0 && system.flush_network == 1)
{
system.flush_network = 0;
dequeue_packet(system.network_desc);
equeue_packet(system.network_desc);
system.flush_network = 1;
}
_action2=on_exit_action;
_current_process_context=*(struct t_process_context*)system.process_info->current_process->val;
_old_process_context=_current_process_context;
_processor_reg=processor_reg;
if (system.force_scheduling == 1 && 0 == 0 && system.int_path_count == 0)
{
_action2 = 1;
if (_current_process_context.proc_status == EXITING)
{
_action2 = 2;
}
}
if (_action2>0)
{ system.force_scheduling = 0;
stop = 0;
while(!stop)
{
schedule(&_current_process_context,&_processor_reg);
_new_process_context = *(struct t_process_context*) system.process_info->current_process->val;
if (_new_process_context.sig_num == SIGINT)
{
_exit(0);
free_vm_process(&_new_process_context);
buddy_free_page(system.buddy_desc,FROM_PHY_TO_VIRT(_new_process_context.phy_kernel_stack));
}
else
{
stop = 1;
}
}
if (_new_process_context.pid != _old_process_context.pid)
{
_processor_reg=_new_process_context.processor_reg;
}
SWITCH_PAGE_DIR(FROM_VIRT_TO_PHY(((unsigned int) _new_process_context.page_dir)))
DO_STACK_FRAME(_processor_reg.esp-8);
if (_action2==2)
{
DO_STACK_FRAME(_processor_reg.esp-8);
free_vm_process(&_old_process_context);
buddy_free_page(system.buddy_desc,FROM_PHY_TO_VIRT(_old_process_context.phy_kernel_stack));
}
RESTORE_PROCESSOR_REG
EXIT_SYSCALL_HANDLER
}
else
{
RESTORE_PROCESSOR_REG
RET_FROM_INT_HANDLER
}
}