summaryrefslogtreecommitdiff
path: root/interface/syscalls.c
blob: 06846bbed7c76a07f9ed326c5d6c3ec781a1af07 (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
#include "syscalls.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/termios.h>

#include <errno.h>

// BASICS //

// everybody is root
uid_t getuid(void)
{
    return 0;
}

// everybody is root
uid_t getgid(void)
{
    return 0;
}

// no sync needed for our ram-image so far (DMA?)
void sync(void)
{
}

// set working directory - we simply save this in PWD environment var
int chdir(const char *path)
{
        sprintf(buf,"PWD=%s",path);
        putenv(buf);
        return 0; // assume success
}

// get working dir (see chdir)
char* getwd(char *buf)
{
        return getenv("PWD");
}

// C NEWLIB // 

// first of all we have a few posix syscalls required by newlib 
// (we use version newlib-3.0.0.20180802)
// https://sourceware.org/newlib/libc.html#Syscalls
// just check liunx man-pages, how they SHOULD work.

// holds environment variables
extern char **environ;

// terminate caller
void _exit(int status)
{
    return syscall(SYSCALL_EXIT,status,0,0);
}

// close file
int _close(int file) 
{
    return syscall(SYSCALL_CLOSE,file,0,0);
}
int close(int file) 
{
    return syscall(SYSCALL_CLOSE,file,0,0);
}

// execute programm
int _execve(const char *filename, char *const argv[], char *const envp[]) 
{
    return syscall(SYSCALL_EXECVE,filename,argv,envp);
}
int execve(const char *filename, char *const argv[], char *const envp[]) 
{
    return syscall(SYSCALL_EXECVE,filename,argv,envp);
}

// create a child process
pid_t _fork(void)
{
    return syscall(SYSCALL_FORK,0,0,0);
}
pid_t fork(void)
{
    return syscall(SYSCALL_FORK,0,0,0);
}

int _fstat(int file, struct stat *st)
{
    return syscall(SYSCALL_FSTAT,file,st,0);
}
int fstat(int file, struct stat *st)
{
    return syscall(SYSCALL_FSTAT,file,st,0);
}

int _getpid(void)
{
    return syscall(SYSCALL_GETPID,0,0,0);
}
int getpid(void)
{
    return syscall(SYSCALL_GETPID,0,0,0);
}

int _isatty(int file) 
{
    return syscall(SYSCALL_ISATTY,file,0,0);
}
int isatty(int file) 
{
    return syscall(SYSCALL_ISATTY,file,0,0);
}

int _kill(int pid, int sig)
{
    return syscall(SYSCALL_KILL,pid,sig,0);
}
int kill(int pid, int sig)
{
    return syscall(SYSCALL_KILL,pid,sig,0);
}

int _link(char *old, char *ne)
{
    return syscall(SYSCALL_LINK,old,ne,0);
}
int link(char *old, char *ne)
{
    return syscall(SYSCALL_LINK,old,ne,0);
}

int _lseek(int file, int ptr, int dir) 
{
    return syscall(SYSCALL_LSEEK,file,ptr,dir);
}
int lseek(int file, int ptr, int dir) 
{
    return syscall(SYSCALL_LSEEK,file,ptr,dir);
}

int _open(const char *name, int flags, int mode)
{
    return syscall(SYSCALL_OPEN,name,flags,mode);
}
int open(const char *name, int flags, int mode)
{
    return syscall(SYSCALL_OPEN,name,flags,mode);
}

int _read(int file, char *ptr, int len)
{
    return syscall(SYSCALL_READ,file,ptr,len);
}
int read(int file, char *ptr, int len)
{
    return syscall(SYSCALL_READ,file,ptr,len);
}

uint32_t _sbrk(int incr) 
{
    return syscall(SYSCALL_SBRK,incr,0,0);
}
uint32_t sbrk(int incr) 
{
    return syscall(SYSCALL_SBRK,incr,0,0);
}

int _stat(const char *file, struct stat *st)
{
    return syscall(SYSCALL_STAT,file,st,0);
}
int stat(const char *file, struct stat *st)
{
    return syscall(SYSCALL_STAT,file,st,0);
}

int _times(struct tms *buf)
{
    return syscall(SYSCALL_TIMES,buf,0,0);
}
int times(struct tms *buf)
{
    return syscall(SYSCALL_TIMES,buf,0,0);
}

int _unlink(char *name)
{
    return syscall(SYSCALL_UNLINK,name,0,0);
}
int unlink(char *name)
{
    return syscall(SYSCALL_UNLINK,name,0,0);
}

int _wait(int *status)
{
    return syscall(SYSCALL_WAIT,status,0,0);
}
int wait(int *status)
{
    return syscall(SYSCALL_WAIT,status,0,0);
}

int _write(int file, char *ptr, int len) 
{
    return syscall(SYSCALL_WRITE,file,ptr,len);
}
int write(int file, char *ptr, int len) 
{
    return syscall(SYSCALL_WRITE,file,ptr,len);
}

//////////////////////////////////////////////////////////////////////


int _readdir(const char *name,void *dirs,int max)
{
    return syscall(SYSCALL_READDIR,name,dirs,max);
}

int _poll(int file)
{
    return syscall(SYSCALL_POLL,file,0,0);
}

int _gettimeofday(struct timeval *tv, void *tz)
{
    return syscall(SYSCALL_GETTIMEOFDAY,tv,tz,0);
}

int _lstat(const char *file, struct stat *st)
{
    return syscall(SYSCALL_LSTAT,file,st,0);
}

// termios

        int tcgetattr(int fd, struct termios *termios_p)
        {
           return syscall(SYSCALL_TCGETATTR,fd,termios_p,0);
        }

        int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
        {
           return syscall(SYSCALL_TCSETATTR,fd,termios_p,0);
        }

/*

       int tcsendbreak(int fd, int duration);

       int tcdrain(int fd);


       int tcflow(int fd, int action);
*/

//       void cfmakeraw(struct termios *termios_p);

       /*
       speed_t cfgetispeed(const struct termios *termios_p);

       */

       speed_t cfgetospeed(const struct termios *termios_p)
       {
            syscall(SYSCALL_UNIMPLEMENTED,"cfgetospeed",0,0);
            return B230400;
       }

       long fpathconf(int fd, int name)
        {
            return syscall(SYSCALL_UNIMPLEMENTED,"fpathconf",0,0);
        }

       int tcflush(int fd, int queue_selector)
       {
            return syscall(SYSCALL_UNIMPLEMENTED,"tcflush",0,0);
       }

       /*
       int cfsetispeed(struct termios *termios_p, speed_t speed);

       int cfsetospeed(struct termios *termios_p, speed_t speed);

       int cfsetspeed(struct termios *termios_p, speed_t speed);
       */

////////////////////////////////////////// move along ///

    int access(const char *pathname, int mode)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"access",0,0);
    }

    #include <sys/types.h>
    #include <sys/stat.h>

    mode_t umask(mode_t mask)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"umask",0,0);
    }

    int mkdir(const char *pathname, mode_t mode)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"mkdir",0,0);
    }


    char *ttyname(int fd)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"ttyname",0,0);
    }

//

    struct void *readdir(DIR *dirp) // returns dirent
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"readdir",0,0);
    }

    DIR *opendir(const char *name)
    {
            errno=EACCES;
            return syscall(SYSCALL_UNIMPLEMENTED,"opendir",name,0);
    }

    int closedir(DIR *dirp)
    {   
            return syscall(SYSCALL_UNIMPLEMENTED,"closedir",0,0);
    }
    

    unsigned int sleep(unsigned int seconds)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"sleep",0,0);
    }

    char *getlogin(void)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"getlogin",0,0);
    }


    /** 
     * 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 select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
    {
            // pack all our sets togehter
            fd_set *fd_sets[3];

            fd_sets[0]=readfds;
            fd_sets[1]=writefds;
            fd_sets[2]=exceptfds;

            return syscall(SYSCALL_SELECT,nfds,timeout,fd_sets);
    }

    int pclose(FILE *stream)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"pclose",0,0);
    }

    FILE *popen(const char *command, const char *type)
    {
            return syscall(SYSCALL_UNIMPLEMENTED,"popen",0,0);
    }

    // call dup2 in dup emulation mode
    int dup(int oldfd)
    {
        return _dup2(oldfd,0xffffffff); // dup emulation mode
    }


int _clone(void)
{
    return syscall(SYSCALL_CLONE,0,0,0);
}
int _pipe(uint32_t fds[2])
{
    return syscall(SYSCALL_PIPE,fds,0,0);
}
int pipe(uint32_t fds[2])
{
    return syscall(SYSCALL_PIPE,fds,0,0);
}
int _dup2(uint32_t oldfd,uint32_t newfd)
{
    return syscall(SYSCALL_DUP2,oldfd,newfd,0);
}
int dup2(uint32_t oldfd,uint32_t newfd)
{
    return syscall(SYSCALL_DUP2,oldfd,newfd,0);
}
int _gui_rect()
{
    return syscall(SYSCALL_GUI_RECT,0,0,0);
}
int _gui_win()
{
    return syscall(SYSCALL_GUI_WIN,0,0,0);
}

int gethostname(char *name, size_t len)
{
    strcpy(name,"foolcomp"); 
    return 0; //success
}

ssize_t readlink(const char *pathname, char *buf, size_t bufsiz)
{
    return syscall(SYSCALL_UNIMPLEMENTED,"readlink",pathname,0);
}

int rmdir(const char *pathname)
{
    syscall(SYSCALL_UNIMPLEMENTED,"rmdir",0,0);
    return 0;
}

int fcntl(int fd, int cmd, ... /* arg */ )
{
    syscall(SYSCALL_UNIMPLEMENTED,"fcntl",0,0);
    return 0;
}

pid_t waitpid(pid_t pid, int *wstatus, int options)
{
    syscall(SYSCALL_UNIMPLEMENTED,"waitpid",0,0);
    return 0;
}

int execl(const char *path, const char *arg, ...)
{
    syscall(SYSCALL_UNIMPLEMENTED,"execl",0,0);
    return 0;
}

int execvp(const char *file, char *const argv[])
{
    syscall(SYSCALL_UNIMPLEMENTED,"execvp",0,0);
    return 0;
}

long sysconf(int name)
{
    return syscall(SYSCALL_UNIMPLEMENTED,"sysconf",name,0);
    return 0;
}

int chmod(const char *pathname, mode_t mode)
{
    syscall(SYSCALL_UNIMPLEMENTED,"chmod",0,0);
    return 0;
}

void _flushing()
{
    fflush(stdout);
}