summaryrefslogtreecommitdiff
path: root/kernel/syscalls.c
blob: 9fc1a7fa2f76e1c079db7853a2f2b4ee8d3448e4 (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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// remember to sync this with interface/syscalls.c

#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 <sys/termios.h>
#include <stdbool.h>
#include <stddef.h>
#include "syscalls.h"
#include "scheduler.h"
#include "log.h"
#include "timer.h"
#include "mount.h"
#include "mem.h"
#include "reent.h"
#include "errno.h"
#include "compositor.h"
#include "stdstreams.h"
#include "sys/unistd.h"

#define MAX_PID 200

//TODO move to process.c and implement per process //
static fd  fds[MAX_PID][MAX_FD];
static ringbuffer invl[MAX_PID];
//static uint32_t opendir_pos[MAX_PID][MAX_FD];
//static char     opendir_name[MAX_PID][MAX_FD][256];
static int tty[MAX_PID]; // keep track of /dev/tty fd for each process :P

static bool open_fd[MAX_PID][MAX_FD];

fd *get_tty(uint32_t pid)
{
    return &(fds[pid][tty[pid]]);
}

void fd_init_std_streams(uint32_t pid) 
{
    fds[pid][0]=fd_from_ringbuffer();
    open_fd[pid][0]=true;

    fds[pid][1]=fd_from_ringbuffer();
    open_fd[pid][1]=true;

    fds[pid][2]=fd_from_ringbuffer();
    open_fd[pid][2]=true;

    return;
}
//

/** helper */
int nextfd(int pid)
{
    for(int i=0;i<MAX_FD;i++)
    {
	if(!open_fd[pid][i])
	{
	    open_fd[pid][i]=true;
	    return i;
	}
    }
    return -1;
}
/** errno helper */
void set_errno(int no)
{
    struct _reent *impure_ptr=VMEM_USER_NEWLIB;
    impure_ptr->_errno=no;
}

/** get string represnation of syscall */
char* syscall_get_name(uint32_t num)
{
    switch(num)
    {
	case SYSCALL_EXIT:
	    return "SYSCALL_EXIT"; 
	case SYSCALL_CLOSE:
	    return "SYSCALL_CLOSE"; 
	case SYSCALL_EXECVE:
	    return "SYSCALL_EXECVE"; 
	case SYSCALL_FORK:
	    return "SYSCALL_FORK"; 
	case SYSCALL_GETPID:
	    return "SYSCALL_GETPID"; 
	case SYSCALL_ISATTY:
	    return "SYSCALL_ISATTY"; 
	case SYSCALL_LINK:
	    return "SYSCALL_LINK"; 
	case SYSCALL_LSEEK:
	    return "SYSCALL_LSEEK"; 
	case SYSCALL_OPEN:
	    return "SYSCALL_OPEN"; 
	case SYSCALL_READ:
	    return "SYSCALL_READ"; 
	case SYSCALL_SBRK:
	    return "SYSCALL_SBRK"; 
	case SYSCALL_STAT:
	    return "SYSCALL_STAT"; 
	case SYSCALL_FSTAT:
	    return "SYSCALL_FSTAT"; 
	case SYSCALL_LSTAT:
	    return "SYSCALL_LSTAT"; 
	case SYSCALL_TIMES:
	    return "SYSCALL_TIMES"; 
        case SYSCALL_UNLINK:
	    return "SYSCALL_UNLINK"; 
	case SYSCALL_WAIT:
	    return "SYSCALL_WAIT"; 
	case SYSCALL_WRITE:
	    return "SYSCALL_WRITE"; 
	case SYSCALL_GETTIMEOFDAY:
	    return "SYSCALL_GETTIMEOFDAY"; 
	case SYSCALL_READDIR:
	    return "SYSCALL_READDIR"; 
	case SYSCALL_OPENDIR:
	    return "SYSCALL_OPENDIR"; 
	case SYSCALL_KILL:
	    return "SYSCALL_KILL"; 
	case SYSCALL_CLONE:
	    return "SYSCALL_CLONE"; 
	case SYSCALL_PIPE:
	    return "SYSCALL_PIPE"; 
	case SYSCALL_DUP2:
	    return "SYSCALL_DUP2"; 
	case SYSCALL_GUI_RECT:
	    return "SYSCALL_GUI_RECT"; 
	case SYSCALL_GUI_WIN:
	    return "SYSCALL_GUI_WIN"; 
	case SYSCALL_SELECT:
	    return "SYSCALL_SELECT"; 
        case SYSCALL_TCGETATTR:
	    return "SYSCALL_TCGETATTR"; 
        case SYSCALL_TCSETATTR:
	    return "SYSCALL_TCSETATTR"; 
        case SYSCALL_UNIMPLEMENTED:
	    return "SYSCALL_UNIMPLEMENTED"; 
    }
    kpanic("UNKNOWN SYSCALL NUM: %d",num);
}

int syscall_gettimeofday(struct timeval *tv, struct timezone *tz,uint32_t none1, uint32_t pid)
{
    if(tv!=NULL)
    {
	uint64_t t=timer_get_ms();
	tv->tv_sec=t/1000+2*3600; // add gmt+2
	tv->tv_usec=1000*(t%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,uint32_t pid) 
{

    if(dir==SEEK_CUR)
    {
	uint32_t *dat=fds[pid][file].data;
	dat[1]+=ptr;
	return dat[1];
    }
    else if(dir==SEEK_SET)
    {
	uint32_t *dat=fds[pid][file].data;
	dat[1]=ptr;
	return dat[1];
    }
    else if(dir==SEEK_END)
    {
        kpanic("SEEK_END dir for lseek!");
    }
    else
    {
        kpanic("wrong dir for lseek!");
    }

    return 0;
}

// TODO: /dev/console or /dev/tty1 - /dev/ttyN
int syscall_write(int file, char *buf, int len,uint32_t pid)
{
    if(!open_fd[pid][file])kpanic("writing to closed file descriptor");
    for(int i=0;i<len;i++)
    {
        if(!fd_can_write(&fds[pid][file]))return i;
	fd_write(&fds[pid][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,uint32_t pid)
{
        int ret=0;

        if(!open_fd[pid][file])kpanic("reading from closed file descriptor");
	if(fd_eof(&fds[pid][file]))return 0;
        
        while((fd_has(&fds[pid][file])||fd_eof(&fds[pid][file]))&&len-->0)
        {
            if(fd_eof(&fds[pid][file]))return ret;
            *buf++=fd_read(&fds[pid][file]);
            ret++;
        }
	return ret;
}

    /** 
     * Monitor multiple descriptors
     * ============================
     *
     * nfds is the MAX file descriptor number +1.
     *
     * It is possible to provide 3 different sets of filedescriptors 
     * (they can be NULL as well)
     *
     * - readfds        - reading   
     * - writefds       - writing
     * - exceptfds      - exceptions (all cleared unless exception occurs)
     *
     * The call returns the total number of descriptors contained in all
     * sets after returning.
     *
     * This call will block until time (timeout) runs out 
     * OR a file descriptor becomes ready.
     */

int syscall_select(int maxxfd,struct timeval *tv, fd_set **fd_sets, uint32_t pid, bool test)
{
    int ret=0;
    
    if(test&&tv!=NULL) // check if time did not run out already
    {
        if(tv->tv_sec==0&&tv->tv_usec==0)return 1;

        uint64_t t=timer_get_ms(); // current time in milliseconds (10^-3)

        uint64_t t0=0;
        t0+=tv->tv_sec*1000;        // seconds * 10^3
        t0+=tv->tv_usec/1000;       // microseconds  * 10^-3

        if(t>t0)return 1; // ready to fire!
    }

    // TODO: wake when timeout runs out!
   
    if(!test)
    {
  //    if(tv==NULL)klog("select with infinite timeout");
//       else klog ("select with timeout: sec=%d, usec=%d",tv->tv_sec,tv->tv_usec);
    }

    for(int i=0;i<maxxfd;i++)
    {
        if(FD_ISSET(i,fd_sets[0]))
        {
  //          klog("%d in readfds",i);
	    if(fd_has(&fds[pid][i])||fd_eof(&fds[pid][i]))
            {
                ret++;
            }
            else
            {
                if(!test)FD_CLR(i,fd_sets[0]);
            }
        }
        if(FD_ISSET(i,fd_sets[1]))
        {
//            klog("%d in writefds",i);

            if(fd_can_write(&fds[pid][i]))
            {
                ret++;
            }
            else
            {
                if(!test)FD_CLR(i,fd_sets[1]);
            }
        }

        if(FD_ISSET(i,fd_sets[2]))
        {
    //        klog("%d in exceptfds",i); // TODO!
        }
    }

    if(!test)FD_ZERO(fd_sets[2]); // we dont give a shit about exceptions! :P TODO!!!

    return ret;

}

//TODO: use fd and inode (instead of path)... now this is a crazy trick how it works.. 
//is it that bad??? yes it was bad and vim crashed so we changed to even more hacky approach now.. :( :(
//ok it was other reason but we should not use it ...
//since userspace might zero or reuse dirent* and corrupt our DIR* ! TODO TODO TODO danger!
struct dirent *syscall_opendir(const char *name,struct dirent *dirs,int none,uint32_t pid)
{
    strcpy(dirs->dirname,name);
    if(dirs->dirname[strlen(dirs->dirname)-1]=='/')dirs->dirname[strlen(dirs->dirname)-1]=0;
    klog("syscall to opendir(%s)",dirs->dirname);

    // test if exists....
    struct dirent testdirent;
    uint32_t testpos;
    int ret=mount_read_dir(dirs->dirname, &testdirent, &testpos);
    if(ret==-1)
    {
        // no it does not!
        set_errno(ENOENT);
        return NULL;
    }

    dirs->pos=0;

    /*
    uint32_t fdn=nextfd(pid);
    strcpy(opendir_name[pid][fdn],dirs->dirname);
    opendir_pos[pid][fdn]=0;
    */

    return dirs;
}

struct dirent *syscall_readdir(struct dirent *dirs,int none1, int none2,uint32_t pid)
{
    // this dirent will get filled with next entry... 
    int ret=mount_read_dir(dirs->dirname, dirs, &dirs->pos);

    // no more entries
    if(ret==0)return NULL;

    return dirs;
}

/** execve helper */
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;
}

/** does not return on success otherwise -1 and errrno set */
// int execve(const char *filename, char *const argv[], char *const envp[]); 
int syscall_execve(const char *name, char *const argv[], char *const env[], int pid)
{
    uint32_t alloc;
    char **argv1= VMEM_USER_ENV;
    char **env1 = VMEM_USER_ENV+1024*2;
    int arg_count=0;
    //*argv1=NULL;
    //*env1=NULL;

    // TODO!
    fixme("not overwrite yourself badly here!?");
    while(argv[arg_count]!=NULL)arg_count++;
    if(argv!=NULL)copy_args(argv,argv1);
    if(env!=NULL)copy_args(env,env1);

    uint32_t entry_global=load_elf(name,&alloc); // overwrites ourselves (in userspace)

    if(!entry_global)
    {
        kpanic("no entry point!"); // TODO: rem
        set_errno(ENOENT);
        return -1;
    }

    // here we might overwrite our CURRENT stack!!! oh fuck TODO: check it!
    uint32_t *stack=VMEM_USER_STACK_TOP-4*32; 
    *--stack=argv1;
    *--stack=arg_count;
    *--stack=env1;

    task_set_name(pid,name);
    task_reset(pid,entry_global,stack,alloc);

    return 0;
}


int syscall_open(char *name, int flags, int mode,uint32_t pid)
{
    klog("open %s",name);
    if(!strcmp("/dev/tty",name))return tty[pid];

    uint32_t fdn=nextfd(pid);
    fds[pid][fdn]=mount_file_open(name);
    if(*(uint32_t *)fds[pid][fdn].data==0)return -1;
    return fdn;
}

// pid_t fork(void);
uint32_t syscall_fork(int none1, int none2, int none3, int pid)
{
    uint32_t newpid=task_fork(pid);

    for(int i=0;i<MAX_FD;i++)
    {
	if(!open_fd[pid][i])continue;
	fds[newpid][i]=fd_dupl(&fds[pid][i]);
	open_fd[newpid][i]=true;
    }

    tty[newpid]=tty[pid];
    invl[newpid]=invl[pid];
//  fds[newpid][0]=fd_from_ringbuffer(); // TODO fix
//  open_fd[newpid][0]=true;

    return newpid;
}

uint32_t syscall_clone(int none1, int none2, int none3, int pid)
{
    uint32_t newpid=task_clone(pid);

    for(int i=0;i<MAX_FD;i++) // TODO: use same pid and use same list maybe?
    {
	if(!open_fd[pid][i])continue;
	fds[newpid][i]=fd_dupl(&fds[pid][i]);
	open_fd[newpid][i]=true;
    }
    tty[newpid]=tty[pid];

    return newpid;
}

uint32_t syscall_wait(int none1, int none2, int none3, int pid)
{
    return 0;
}

// void _exit(int status)
uint32_t syscall_exit(int status, uint32_t none1, uint32_t none2,int pid)
{
    fixme("free allll mem");
    klog("pid %d exited with %d",pid,status);
    for(int i=0;i<MAX_FD;i++)
    {
	if(open_fd[pid][i])
	{
	    fd_close(&fds[pid][i]);
	    open_fd[pid][i]=false;
	}

    }
    task_exit(pid);
    return 0;
}

// int close (int fd) (TODO; close or not to close open filedescirptors?)
int syscall_close(int file,int none1,int none2,int pid)  
{
    if(open_fd[pid][file])
    {
	fd_close(&fds[pid][file]);
	open_fd[pid][file]=false;
    }

    return 0;
}

// TODO: check if file is a termminal!
int syscall_isatty(int file,int none1,int none2,int pid)  
{
    return 1;
    if(fds[pid][file].type==FD_TYPE_FIFO)return 1;
    return 0;
}

// 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);

    uint32_t max_addy=VMEM_USER_PROG+VMEM_USER_PROG_PAGES*4096;

    if(alloc>max_addy)kpanic("can not set sbrk . TODO: dynamic allocation!");
    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,uint32_t pid)
{
    st->st_mode = S_IFCHR;
    return 0;
}

uint32_t syscall_pipe(uint32_t *addr,int none1, int none2, uint32_t pid)
{
    fd pipfds[2];
    int ret=fds_from_pipe(pipfds);

    int fd1=nextfd(pid);
    int fd2=nextfd(pid);

    klog("new pipe read=%d, write=%d",fd1,fd2 );

    fds[pid][fd1]=pipfds[0] ;
    *addr=fd1;
    addr++;

    fds[pid][fd2]=pipfds[1] ;
    *addr=fd2;

    return ret;
}

uint32_t syscall_dup2(uint32_t oldfd,int newfd, int none2, uint32_t pid)
{
    if(newfd==0xffffffff)kpanic("dup mode not supported yet !!!");

    fds[pid][newfd]=fd_dupl(&fds[pid][oldfd]);
    return newfd;
}

 /// testing TODO: 
 struct termios tty1;
 bool   termios_init=false;

uint32_t syscall_tcgetattr(int fd, struct termios *termios_p, uint32_t none, uint32_t pid)
{
    if(!termios_init)
    {
        tty1.c_cc[VMIN]=1; // reading from terminal returns after each byte
        termios_init=true;
    }
    klog("pid %d calls tcgetattr on %d",pid,fd);
    *termios_p=tty1;
    return 0;
}

uint32_t syscall_tcsetattr(int fd, struct termios *termios_p, uint32_t none, uint32_t pid)
{
    klog("pid %d calls tcsetattr on %d",pid,fd);
    klog("c_iflag=%08X",termios_p->c_iflag);
    klog("c_oflag=%08X",termios_p->c_oflag);
    klog("c_cflag=%08X",termios_p->c_cflag);
    klog("c_lflag=%08X",termios_p->c_lflag);
    for(int i=0;i<NCCS;i++)
    {
        if(termios_p->c_cc[i]!=0)
        {
            klog("set c_cc[%d]=%d",i,termios_p->c_cc[i]);
        }
    }
    tty1=*termios_p;
    return 0;
}

uint32_t syscall_gui_rect(uint32_t xy, uint32_t wh, uint32_t none, uint32_t pid)
{
//    klog("pid %d x y %d %d w h %d %d",pid,xy>>16,xy&0xffff,wh>>16,wh&0xffff);

    ringbuffer_put(&invl[pid],xy&0x000000ff);
    ringbuffer_put(&invl[pid],(xy&0x0000ff00)>>8);
    ringbuffer_put(&invl[pid],(xy&0x00ff0000)>>16);
    ringbuffer_put(&invl[pid],(xy&0xff000000)>>24);

    ringbuffer_put(&invl[pid],wh&0x000000ff);
    ringbuffer_put(&invl[pid],(wh&0x0000ff00)>>8);
    ringbuffer_put(&invl[pid],(wh&0x00ff0000)>>16);
    ringbuffer_put(&invl[pid],(wh&0xff000000)>>24);
    
    compositor_wake();

    return 1;
}
uint32_t syscall_gui_win(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{
    uint32_t fdn=nextfd(pid);
    fds[pid][fdn]=fd_from_ringbuffer();
    tty[pid]=fdn;
    invl[pid]=ringbuffer_init(1); 
    task_add_win(pid,&invl[pid]);
    return 1;
}

/** Generics . prep before we reenable interrupts*/
uint32_t syscall_generic_prep(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{
#ifdef LOG_SYSCALLS
            klog("prep syscall [%s] for pid:%d",syscall_get_name(nr),pid);
#endif
    struct timeval *tv=p2; 

    switch(nr){
	case  SYSCALL_SELECT :

            // change to absolute time for easier timout
            if(tv!=NULL&&(tv->tv_sec!=0||tv->tv_usec!=0))
            {
	        uint64_t t=timer_get_ms(); // current time in milliseconds (10^-3)
                t+=tv->tv_sec*1000;        // seconds * 10^3
                t+=tv->tv_usec/1000;       // microseconds  * 10^-3

                tv->tv_sec=t/1000;
                tv->tv_usec=(t%1000)*1000;
            }

            break;
    }
    return 1;
}

/** Generics. test if we can continue.. reschedule otherwise */
volatile uint32_t syscall_generic_test(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{
#ifdef LOG_SYSCALLS
            klog("testing syscall [%s] for pid:%d",syscall_get_name(nr),pid);
#endif
    switch(nr){
	case SYSCALL_WAIT :
	    return !task_runs(p1);
	case SYSCALL_READ :
	    return fd_has(&fds[pid][p1])||fd_eof(&fds[pid][p1]);
	case SYSCALL_WRITE :
            return fd_can_write(&fds[pid][p1]);
	case  SYSCALL_SELECT :
	    return syscall_select(p1,p2,p3,pid,true);
    }

    return 1;//other syscalls never block for now.
}

/** Generics. finally do the work! */
uint32_t syscall_generic(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{

#ifdef LOG_SYSCALLS
            klog("processing syscall [%s] for pid:%d",syscall_get_name(nr),pid);
#endif

    switch(nr){
	case SYSCALL_EXIT :
	    return syscall_exit(p1,p2,p3,pid);
	case  SYSCALL_CLOSE       :
	    return syscall_close(p1,p2,p3,pid);
	case  SYSCALL_EXECVE      :
	    return syscall_execve(p1,p2,p3,pid);
	case  SYSCALL_FORK        :
	    return syscall_fork(p1,p2,p3,pid);
	case  SYSCALL_CLONE        :
	    return syscall_clone(p1,p2,p3,pid);
	case  SYSCALL_GETPID      :
    //	return syscall_getpid(p1,p2,p3);
	    return -1;
	case  SYSCALL_ISATTY      :
	    return syscall_isatty(p1,p2,p3,pid);
	case  SYSCALL_LINK        :
    //	return syscall_link(p1,p2,p3);
	    return -1;
	case  SYSCALL_LSEEK       :
	    return syscall_lseek(p1,p2,p3,pid);
	case  SYSCALL_OPEN        :
	    return syscall_open(p1,p2,p3,pid);
	case  SYSCALL_READ        :
	    return syscall_read(p1,p2,p3,pid);
	case  SYSCALL_SBRK        :
	    return syscall_sbrk(p1,p2,p3,pid);
	case  SYSCALL_STAT        :
	    return syscall_stat(p1,p2,p3,pid);
	case  SYSCALL_FSTAT       :
	    return syscall_stat(p1,p2,p3,pid);
	case  SYSCALL_LSTAT       :
	    return syscall_stat(p1,p2,p3,pid);
	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(p1,p2,p3,pid);
	case  SYSCALL_WRITE       :
	    return syscall_write(p1,p2,p3,pid);
	case  SYSCALL_GETTIMEOFDAY:
	    return syscall_gettimeofday(p1,p2,p3,pid);
	case  SYSCALL_READDIR     :
	    return syscall_readdir(p1,p2,p3,pid);
	case  SYSCALL_OPENDIR     :
	    return syscall_opendir(p1,p2,p3,pid);
	case  SYSCALL_KILL        :
    //	return task_kill(p1,p2,p3);
	    return -1;
	case  SYSCALL_PIPE :
	    return syscall_pipe(p1,p2,p3,pid);
	case  SYSCALL_DUP2 :
	    return syscall_dup2(p1,p2,p3,pid);
	case  SYSCALL_SELECT :
	    return syscall_select(p1,p2,p3,pid,false);
	case  SYSCALL_GUI_RECT :
	    return syscall_gui_rect(p1,p2,p3,pid);
	case  SYSCALL_GUI_WIN :
	    return syscall_gui_win(p1,p2,p3,pid);
        case SYSCALL_TCGETATTR:
            return syscall_tcgetattr(p1,p2,p3,pid);
        case SYSCALL_TCSETATTR:
            return syscall_tcsetattr(p1,p2,p3,pid);
        case SYSCALL_UNIMPLEMENTED:
            if(!strcmp(p1,"fcntl")){
                klog("todo: quickfix hack for fcntl");
            }
    }
    klog("unknown syscall %s / %d we just return 0",p1,nr);
    return 0;
}