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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
#include "lib/string/string.h"
#include "lib/printf/printf.h"
#include "fs/ext2.h"
#include "kernel.h"
#include "driver/vesa.h"
#include "fifo.h"
#include "fd.h"
#include "fs/elf.h"
#include "driver/terminal.h"
#include "driver/screen.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <stdbool.h>
#include <stddef.h>
#include "syscalls.h"
#include "scheduler.h"
#include "log.h"
#include "timer.h"
static fd fds[MAX_FD];
static uint32_t next_fd=0;
// fifos for backing up some file descrpitors
static fifo fifos[MAX_FIFOS];
static uint32_t next_fifo=0;
// screen / terminal
term_out screen;
terminal_tty tty1;
char* syscall_get_name(uint32_t num)
{
switch(num)
{
case 60:
return "SYSCALL_EXIT";
case 66:
return "SYSCALL_CLOSE";
case 64:
return "SYSCALL_EXECVE";
case 72:
return "SYSCALL_FORK";
case 78:
return "SYSCALL_GETPID";
case 68:
return "SYSCALL_ISATTY";
case 82:
return "SYSCALL_LINK";
case 69:
return "SYSCALL_LSEEK";
case 65:
return "SYSCALL_OPEN";
case 62:
return "SYSCALL_READ";
case 70:
return "SYSCALL_SBRK";
case 74:
return "SYSCALL_STAT";
case 67:
return "SYSCALL_FSTAT";
case 79:
return "SYSCALL_LSTAT";
case 75:
return "SYSCALL_TIMES";
case 76:
return "SYSCALL_UNLINK";
case 77:
return "SYSCALL_WAIT";
case 61:
return "SYSCALL_WRITE";
case 71:
return "SYSCALL_GETTIMEOFDAY";
case 63:
return "SYSCALL_READDIR";
case 73:
return "SYSCALL_KILL";
case 80:
return "SYSCALL_POLL";
case 83:
return "SYSCALL_CLONE";
}
return "UNKNOWN SYSCALL NUM";
}
int syscall_unhandled(int nr)
{
char msg[256];
tfp_sprintf(msg, "unhandled syscall : %d",nr);
kpanic("%s",msg);
}
int syscall_gettimeofday(struct timeval *tv, struct timezone *tz)
{
if(tv!=NULL)
{
uint64_t t=timer_get_ms();
tv->tv_sec=t/1000+2*3600; // add gmt+2
tv->tv_usec=0;//t-tv->tv_sec*1000;
}
// tz struct is obsolote
if(tz!=NULL)
{
tz->tz_minuteswest=0;// -21*60; // warsaw
tz->tz_dsttime=DST_NONE;
}
return 0;
}
int syscall_lseek(int file,int ptr,int dir)
{
#ifndef SEEK_SET
#define SEEK_SET 0 /* set file offset to offset */
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1 /* set file offset to current plus offset */
#endif
#ifndef SEEK_END
#define SEEK_END 2 /* set file offset to EOF plus offset */
#endif
if(dir==SEEK_CUR)
{
uint32_t *dat=fds[file].data;
dat[1]+=ptr;
return dat[1];
}
kpanic("not fully handled lseek! dir=%d",dir);
return 0;
}
// TODO: /dev/console or /dev/tty1 - /dev/ttyN
int syscall_write(int file, char *buf, int len)
{
for(int i=0;i<len;i++)
{
fd_write(&fds[file],buf[i]);
}
return len;
}
/**
* __read()__ attemts to read up to _len_ bytes from file descriptor _file_
* into the buffer starting at _buf_.
*/
int syscall_read(int file, char *buf, int len)
{
if(fds[file].type==FD_TYPE_EXT2_FILE && !fd_has(&fds[file]))return 0;
*buf=fd_read(&fds[file]);
return 1;
}
//TODO: replace with dirent!
int syscall_readdir(const char *name,fs_dirent *dirs,int *pos)
{
uint32_t inode = ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,name);
if(inode==0)return 0;
return ext2_read_dir(VMEM_EXT2_RAMIMAGE, inode, dirs, pos);
}
// for non blocking io?
int syscall_poll(int file)
{
file=2; //workaround
return fd_has(&fds[file]);
}
// TODO: DELETE THIS SHIT!
int syscall_tune(int v1,int v2, int v3)
{
// osbolete
/*
if(v1==0) // regular tty mode
{
get_fool()->tty->set_buff=true;
get_fool()->tty->set_echo=true;
}
if(v1==1) // gaming tty mode
{
get_fool()->tty->set_buff=false;
get_fool()->tty->set_echo=false;
}
*/
return 0;
}
int copy_args(char **in, char **out)
{
//klog("copy_args(0x%08x, 0x%08X)",in,out);
int count=0;
while(in[count]!=NULL)
{
// klog("args(0x%08x: %s)",in[count],out);
count++;
};
// klog("copy_args : count: %d",count);
char **pos=out;
pos+=sizeof(char **)*(count+1);
int i=0;
do{
int l=strlen(in[i]);
char *var=pos;
memcpy(var,in[i],l+1);
pos+=sizeof(char *)*(l+1);
out[i]=var;
i++;
}while(in[i]!=NULL);
out[i]=NULL;
return count;
}
int syscall_execve(char *name, char **argv, char **env,int pid)
{
fixme("not overwrite yourself?");
int arg_count=0;
while(argv[arg_count]!=NULL)arg_count++;
char **argv1=VMEM_USER_ENV;
if(argv!=NULL)
{
copy_args(argv,argv1);
}
else{
argv1=NULL;
}
char **env1=VMEM_USER_ENV+1024*2;
if(env!=NULL)
{
copy_args(env,env1);
}
else{
env1=NULL;
}
uint32_t alloc;
uint32_t entry_global=load_elf(name,&alloc);
if(!entry_global)
{
return -1; // errror loading
}
uint32_t *stack=VMEM_USER_STACK_TOP-4*32;
*--stack=argv1;
*--stack=arg_count;
*--stack=env1;
task_reset(pid,entry_global,stack,alloc);
return 0;
/* try to move this to asm */
// asm volatile("jmp ."); // loop forever
//klog("returning to jump addy (0x%08X)", entry_global);
/*
asm volatile("mov $0x08fff000,%esp"); // set stack at high end of process image
asm volatile ("push %0" :: "r" (argv1));
asm volatile ("push %0" :: "r" (arg_count));
//asm volatile ("push %0" :: "r" (kballoc(1)));
asm volatile ("push %0" :: "r" (env1));
// push addr and return to it
asm volatile ("pushl %0"::"r"(entry_global));
asm volatile ("sti");
asm volatile ("ret");
*/
// this is never reached!
}
// minihack
int get_max_fd()
{
return next_fd-1;
}
// TODO: support other files too (not only fifo pipes)
// TODO: allow opening existing files/named pipes
int syscall_open(char *name, int flags, int mode)
{
if( next_fifo>=MAX_FIFOS || next_fd>=MAX_FD)kpanic("we ran out of fd's or fifo's");
bool create_fifo=true;
if(name[0]!='~')create_fifo=false;
if(create_fifo)
{
if(!strcmp(name,"~term"))
{
screen.put_char=console_put_char;
screen.update_cursor=update_cursor;
tty1=terminal_init(&screen,NULL);
fifos[next_fifo].data=&tty1;
fifos[next_fifo].put=terminal_put;
fds[next_fd]=fd_from_fifo(&fifos[next_fifo]);
}
else if(!strcmp(name,"~xterm"))
{
screen.put_char=vesa_console_put_char;
screen.update_cursor=vesa_update_cursor;
tty1=terminal_init(&screen,NULL);
fifos[next_fifo].data=&tty1;
fifos[next_fifo].put=terminal_put;
fds[next_fd]=fd_from_fifo(&fifos[next_fifo]);
}
else
{
fifos[next_fifo]=fifo_create_buffered(1);
fds[next_fd]=fd_from_fifo(&fifos[next_fifo]);
}
next_fifo++;
}
else
{
fds[next_fd]=fd_from_path(name);
if(*(uint32_t *)fds[next_fd].data==0)return -1;
}
next_fd++;
return next_fd-1;
}
uint32_t syscall_fork(int pid)
{
return task_fork(pid);
}
uint32_t syscall_clone(int pid)
{
return task_clone(pid);
}
uint32_t syscall_wait(int pid)
{
return 0;
}
uint32_t syscall_exit(int pid)
{
fixme("free allll mem");
task_exit(pid);
return 0;
}
//newcomers
//
int syscall_close(int file,int none1,int none2)
{
//if(file!=0&&file!=1&&file!=2)
// kpanic("unhandled syscall: close");
return -1;
}
// TODO: check if file is termminal!
int syscall_isatty(int file,int none1,int none2)
{
return 1;
}
// TODO: per process basis!
uint32_t syscall_sbrk(uint32_t incr, int none1, int none2, uint32_t pid)
{
uint32_t alloc=task_get_brk(pid);
uint32_t oldalloc=alloc;
alloc+=incr;
task_set_brk(pid,alloc);
fixme("fake syscall_sbrk(0x%08X) return %08X",incr,oldalloc);
return oldalloc;
}
// stat, fstat, lstat
int syscall_stat(const char *path, struct stat *st,int none)
{
st->st_mode = S_IFCHR;
return 0;
}
/// there also is task_fork, task_wait, task_exit.. which is in scheduler.c
////////////////////////////////////////
uint32_t syscall_generic_test(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{
switch(nr){
case SYSCALL_WAIT :
return !task_runs(p1);
case SYSCALL_READ :
if(fds[p1].type==FD_TYPE_EXT2_FILE)return 1;
return fd_has(&fds[p1]);
case SYSCALL_EXIT :
return 1;
}
return 1;
}
uint32_t syscall_generic(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{
switch(nr){
case SYSCALL_EXIT :
return syscall_exit(pid);
case SYSCALL_CLOSE :
return syscall_close(p1,p2,p3);
case SYSCALL_EXECVE :
return syscall_execve(p1,p2,p3,pid);
case SYSCALL_FORK :
return syscall_fork(pid);
case SYSCALL_CLONE :
return syscall_clone(pid);
case SYSCALL_GETPID :
// return syscall_getpid(p1,p2,p3);
return -1;
case SYSCALL_ISATTY :
return syscall_isatty(p1,p2,p3);
case SYSCALL_LINK :
// return syscall_link(p1,p2,p3);
return -1;
case SYSCALL_LSEEK :
return syscall_lseek(p1,p2,p3);
case SYSCALL_OPEN :
return syscall_open(p1,p2,p3);
case SYSCALL_READ :
return syscall_read(p1,p2,p3);
case SYSCALL_SBRK :
return syscall_sbrk(p1,p2,p3,pid);
case SYSCALL_STAT :
return syscall_stat(p1,p2,p3);
case SYSCALL_FSTAT :
return syscall_stat(p1,p2,p3);
case SYSCALL_LSTAT :
return syscall_stat(p1,p2,p3);
case SYSCALL_TIMES :
// return syscall_times(p1,p2,p3);
return -1;
case SYSCALL_UNLINK :
// return syscall_unlink(p1,p2,p3);
return -1;
case SYSCALL_WAIT :
return syscall_wait(pid);
case SYSCALL_WRITE :
return syscall_write(p1,p2,p3);
case SYSCALL_GETTIMEOFDAY:
return syscall_gettimeofday(p1,p2);
case SYSCALL_READDIR :
return syscall_readdir(p1,p2,p3);
case SYSCALL_KILL :
// return task_kill(p1,p2,p3);
return -1;
case SYSCALL_POLL :
return syscall_poll(p1);
}
return -1;
}
|