summaryrefslogtreecommitdiff
path: root/kernel/syscalls.c
blob: 228b70a328acf3791574652e74aad7133b6d1de3 (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
// 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 <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 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;
}
//

/** 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_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"; 
    }
    kpanic("UNKNOWN SYSCALL 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];
    }

    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,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)
{
        if(!open_fd[pid][file])kpanic("reading from closed file descriptor");
	if(fd_eof(&fds[pid][file]))return 0;
        *buf=fd_read(&fds[pid][file]);
	return 1;
}

int syscall_select(int maxxfd,struct timeval *tv, fd_set **fd_sets, uint32_t pid, bool test)
{
    int ret=0;

    // 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!!!

    if(test&&ret==0&&tv!=NULL) // check if time did not run out already
    {
        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!
    }

    return ret;



}

//TODO: replace with dirent!
int syscall_readdir(const char *name,fs_dirent *dirs,int *pos,uint32_t pid)
{
    int ret=mount_read_dir(name, dirs, pos);
    if(ret==-1)
    {
	set_errno(ENOENT);
    }
    return ret;
}

/** 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)
{

    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){
	set_errno(ENOENT);
	return -1;
    }

    uint32_t *stack=VMEM_USER_STACK_TOP-4*32;
    *--stack=argv1;
    *--stack=arg_count;
    *--stack=env1;
    task_reset(pid,entry_global,stack,alloc);
    task_set_name(pid,name);

    return 0;
}

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

int syscall_open(char *name, int flags, int mode,uint32_t pid)
{
    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];
//    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);
    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)klog("dup mode not supported yet !!!");

    fds[pid][newfd]=fd_dupl(&fds[pid][oldfd]);
    return newfd;
}
uint32_t syscall_gui_rect(uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{
    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;

    task_add_win(pid);

    return 1;
}

/** Generics */
uint32_t syscall_generic_prep(uint32_t nr,uint32_t p1, uint32_t p2, uint32_t p3, uint32_t pid)
{
    struct timeval *tv=p2; 

    switch(nr){
	case  SYSCALL_SELECT :

            // change to absolute time for easier timout
            if(tv!=NULL)
            {
	        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 */
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 :
	    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 */
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(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_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);
    }
    klog("unknown syscall %s / %d",p1,nr);
    return 0;
}