Added miscellaneous syscalls to linuxemu
[akaros.git] / user / vmm / linuxemu.c
1 /* Copyright (c) 2016 Google Inc.
2  * See LICENSE for details.
3  *
4  * Linux emulation for virtual machines. */
5
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <vmm/vmm.h>
13 #include <errno.h>
14 #include <sys/syscall.h>
15 #include <vmm/linux_syscalls.h>
16 #include <sys/time.h>
17 #include <vmm/linuxemu.h>
18 #include <dlfcn.h>
19 #include <sys/mman.h>
20 #include <futex.h>
21 #include <sys/epoll.h>
22
23 // This is the maximum fd number we allow opened in dune
24 #define DUNE_NR_FILE_DESC 100
25
26 // Some defines used in linux syscalls
27 #define FALLOC_FL_KEEP_SIZE 1
28 #define FALLOC_FL_PUNCH_HOLE 2
29 #define GRND_NONBLOCK 0x0001
30 #define GRND_RANDOM 0x0002
31
32 static int lemu_debug;
33
34 static uth_mutex_t *lemu_logging_lock;
35 static uth_mutex_t *fd_table_lock;
36
37 static FILE *lemu_global_logfile;
38
39 //Records the open fds of files opened with linuxemu
40 // TODO: Make this a dynamic array in the future
41 char *openfd_filenames[DUNE_NR_FILE_DESC];
42
43 void init_lemu_logging(int log_level)
44 {
45         const char *logfile_name = "lemu.log";
46         FILE *x = fopen(logfile_name, "w");
47
48         lemu_debug = log_level;
49         lemu_logging_lock = uth_mutex_alloc();
50
51         if (x != NULL)
52                 lemu_global_logfile = x;
53         else
54                 lemu_global_logfile = stderr;
55 }
56
57 void destroy_lemu_logging(void)
58 {
59         if (lemu_logging_lock != NULL)
60                 uth_mutex_free(lemu_logging_lock);
61
62         if (lemu_global_logfile != stderr)
63                 fclose(lemu_global_logfile);
64 }
65
66 /////////////////////////////////////
67 // BEGIN SYSCALL HELPERS
68 /////////////////////////////////////
69
70 // Checks if path is an absolute path
71 // Symlinks are not resolved, we just check if we refer to the root directory
72 bool is_absolute_path(const char *path)
73 {
74         if (!path || strlen(path) == 0)
75                 return false;
76         return (strncmp(path, "/", 1) == 0);
77 }
78
79
80 // This resolves relative paths in syscalls like openat and unlinkat, based on
81 // our currently opened fds in dune
82 //
83 // This function will allocate memory for the string absolute_path, it is the
84 // caller's responsibility to free this memory when it is done
85 //
86 // If path is an absolute path already, we ignore fd and retrun a copy of path
87 // in absolute_path
88 bool get_absolute_path_from_fd(int fd, const char *path, char **absolute_path)
89 {
90         if (!path) {
91                 fprintf(stderr, "get_absolute_path_from_fd: suffix is null.\n");
92                 return false;
93         }
94
95         if (fd >= DUNE_NR_FILE_DESC) {
96                 fprintf(stderr, "get_absolute_path_from_fd: fd too large.\n");
97                 return false;
98         }
99
100         int len1 = strlen(path);
101
102         if (len1 == 0) {
103                 fprintf(stderr, "get_absolute_path_from_fd: suffix is empty.\n");
104                 return false;
105         }
106
107         if (is_absolute_path(path)) {
108                 *absolute_path = calloc(sizeof(char), len1 + 1);
109                 if (!(*absolute_path)) {
110                         fprintf(stderr,
111                                 "get_absolute_path_from_fd: couldn't allocate memory.\n");
112                         return false;
113                 }
114                 strcpy(*absolute_path, path);
115                 return true;
116         }
117
118         uth_mutex_lock(fd_table_lock);
119         if (!openfd_filenames[fd]) {
120                 uth_mutex_unlock(fd_table_lock);
121                 fprintf(stderr, "get_absolute_path_from_fd: no file open at fd.\n");
122                 return false;
123         }
124
125         int len2 = strlen(openfd_filenames[fd]);
126
127         if (len2 == 0) {
128                 uth_mutex_unlock(fd_table_lock);
129                 fprintf(stderr,
130                         "get_absolute_path_from_fd: prefix has length 0, fd was probably not open.\n");
131                 return false;
132         }
133
134         // Add space for an extra slash and a null terminator
135         int total_len = len1 + len2 + 2;
136
137         *absolute_path = calloc(sizeof(char), total_len);
138
139         if (!(*absolute_path)) {
140                 uth_mutex_unlock(fd_table_lock);
141                 fprintf(stderr,
142                         "get_absolute_path_from_fd: couldn't allocate memory.\n");
143                 return false;
144         }
145
146         strcat(*absolute_path, openfd_filenames[fd]);
147         uth_mutex_unlock(fd_table_lock);
148
149         // Add a slash if we don't have one
150         if ((*absolute_path)[len2 - 1] != '/')
151                 strcat(*absolute_path, "/");
152
153         strcat(*absolute_path, path);
154
155         fprintf(stderr, "Constructed full path \"%s\"\n", *absolute_path);
156         return true;
157 }
158
159
160 //Akaros open flags are different than linux
161 //This function converts them
162 int translate_open_flags(int flags)
163 {
164         int lower3bits = flags & 0x7;
165         int otherstuff = flags & ~(0x7);
166
167         switch (lower3bits) {
168         case 0:
169                 otherstuff |= O_RDONLY;
170                 break;
171         case 1:
172                 otherstuff |= O_WRONLY;
173                 break;
174         case 2:
175                 otherstuff |= O_RDWR;
176                 break;
177         case 3:
178                 otherstuff |= O_ACCMODE;
179                 break;
180         default:
181                 // TODO(ganshun): We panic here for now if they are trying behavior we
182                 // do not expect
183                 panic("linuxemu, translate_open_flags: unknown open flags provided\n");
184                 break;
185         }
186         return otherstuff;
187 }
188
189 // Updates the fd mapping for a specific fd. Allows the fd mapping to
190 // be freed (in the case of a close). Calls to the function may leave
191 // values in the fd table as NULL
192 bool update_fd_map(int fd, const char *path)
193 {
194         int len = 0;
195
196         if (fd >= DUNE_NR_FILE_DESC)
197                 return false;
198
199         if (path)
200                 len = strlen(path);
201
202         uth_mutex_lock(fd_table_lock);
203
204         // If there was a file here before, free the string
205         // Potential optimization would be to only free when
206         // we need to grow the size of the string.
207         if (openfd_filenames[fd]) {
208                 free(openfd_filenames[fd]);
209                 openfd_filenames[fd] = NULL;
210         }
211         // If we have a new file to put here, allocate memory
212         // and copy it in.
213         if (len) {
214                 openfd_filenames[fd] = (char*) calloc(sizeof(char), len + 1);
215                 if (!openfd_filenames[fd]) {
216                         uth_mutex_unlock(fd_table_lock);
217                         panic("update_fd_map could not allocate memory\n");
218                         return false;
219                 }
220                 strcpy(openfd_filenames[fd], path);
221         }
222
223         uth_mutex_unlock(fd_table_lock);
224         return true;
225 }
226
227 void convert_stat_akaros_to_linux(struct stat *si_akaros,
228                                   struct linux_stat_amd64 *si)
229 {
230         si->st_dev =  (uint64_t) si_akaros->st_dev;
231         si->st_ino = (uint64_t) si_akaros->st_ino;
232         si->st_mode = (uint32_t) si_akaros->st_mode;
233         si->st_nlink = (uint64_t) si_akaros->st_nlink;
234         si->st_uid = (uint32_t) si_akaros->st_uid;
235         si->st_gid = (uint32_t) si_akaros->st_gid;
236         si->st_rdev = (uint64_t) si_akaros->st_rdev;
237         si->st_size = (int64_t) si_akaros->st_size;
238         si->st_blksize = (int64_t) si_akaros->st_blksize;
239         si->st_blocks = (int64_t) si_akaros->st_blocks;
240
241         //For now the akaros timespec works out... this might change
242         //akaros timespec must be 2x int64_t for this to be valid
243         si->st_atim = si_akaros->st_atim;
244         si->st_mtim = si_akaros->st_mtim;
245         si->st_ctim = si_akaros->st_ctim;
246 }
247
248 /////////////////////////////////////
249 // BEGIN DUNE SYSCALL IMPLEMENTATIONS
250 /////////////////////////////////////
251
252 //////////////////////////////////////////////////////
253 // Ported Syscalls (we just call the akaros version)
254 //////////////////////////////////////////////////////
255
256 bool dune_sys_ftruncate(struct vm_trapframe *tf)
257 {
258         int retval = ftruncate((int) tf->tf_rdi, (off_t) tf->tf_rsi);
259         int err = errno;
260
261         if (retval == -1) {
262                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
263                           "ERROR %d\n", err);
264                 tf->tf_rax = -err;
265         } else {
266                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
267                           "SUCCESS %d\n", retval);
268                 tf->tf_rax = retval;
269         }
270         return true;
271
272 }
273
274 bool dune_sys_fcntl(struct vm_trapframe *tf)
275 {
276         int retval = fcntl((int) tf->tf_rdi, (int) tf->tf_rsi, (int) tf->tf_rdx);
277         int err = errno;
278
279         if (retval == -1) {
280                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
281                           "ERROR %d\n", err);
282                 tf->tf_rax = -err;
283         } else {
284                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
285                           "SUCCESS %d\n", retval);
286                 tf->tf_rax = retval;
287         }
288         return true;
289 }
290
291 bool dune_sys_pread64(struct vm_trapframe *tf)
292 {
293         int fd = (int) tf->tf_rdi;
294         void *buf = (void*) tf->tf_rsi;
295         size_t count = tf->tf_rdx;
296         off_t offset = (off_t) tf->tf_r10;
297
298         ssize_t retval = pread(fd, buf, count, offset);
299         int err = errno;
300
301         if (retval == -1) {
302                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
303                           "ERROR %zd\n", err);
304                 tf->tf_rax = -err;
305         } else {
306                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
307                           "SUCCESS %zd\n", retval);
308                 tf->tf_rax = retval;
309         }
310         return true;
311 }
312
313 bool dune_sys_read(struct vm_trapframe *tf)
314 {
315         ssize_t retval = read(tf->tf_rdi, (void*) tf->tf_rsi, (size_t) tf->tf_rdx);
316         int err = errno;
317
318         if (retval == -1) {
319                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
320                           "ERROR %zd\n", err);
321                 tf->tf_rax = -err;
322         } else {
323                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
324                           "SUCCESS %zd\n", retval);
325                 tf->tf_rax = retval;
326         }
327         return true;
328 }
329
330 bool dune_sys_pwrite64(struct vm_trapframe *tf)
331 {
332         int fd = (int) tf->tf_rdi;
333         const void *buf = (const void*) tf->tf_rsi;
334         size_t length = (size_t) tf->tf_rdx;
335         off_t offset = (off_t) tf->tf_r10;
336
337         ssize_t retval = pwrite(fd, buf, length, offset);
338         int err = errno;
339
340         if (retval == -1) {
341                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
342                           "ERROR %zd\n", err);
343                 tf->tf_rax = -err;
344         } else {
345                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
346                           "SUCCESS %zd\n", retval);
347                 tf->tf_rax = retval;
348         }
349         return true;
350 }
351
352 bool dune_sys_write(struct vm_trapframe *tf)
353 {
354         ssize_t retval = write((int) tf->tf_rdi, (const void *) tf->tf_rsi,
355                                (size_t) tf->tf_rdx);
356         int err = errno;
357
358         if (retval == -1) {
359                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
360                           "ERROR %zd\n", err);
361                 tf->tf_rax = -err;
362         } else {
363                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
364                           "SUCCESS %zd\n", retval);
365                 tf->tf_rax = retval;
366         }
367         return true;
368 }
369
370 bool dune_sys_getpid(struct vm_trapframe *tf)
371 {
372         // Getpid always suceeds
373         int retval = getpid();
374
375         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false, "SUCCESS %d\n", retval);
376         tf->tf_rax = retval;
377         return true;
378 }
379
380 bool dune_sys_gettimeofday(struct vm_trapframe *tf)
381 {
382         int retval = gettimeofday((struct timeval*) tf->tf_rdi,
383                                   (struct timezone*) tf->tf_rsi);
384         int err = errno;
385
386         if (retval == -1) {
387                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
388                           "ERROR %d\n", err);
389                 tf->tf_rax = -err;
390         } else {
391                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
392                           "SUCCESS %d\n", retval);
393                 tf->tf_rax = retval;
394         }
395         return true;
396 }
397
398 bool dune_sys_dup(struct vm_trapframe *tf)
399 {
400         int retval = dup((int) tf->tf_rdi);
401         int err = errno;
402
403         if (retval == -1) {
404                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
405                           "ERROR %d\n", err);
406                 tf->tf_rax = -err;
407         } else {
408                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
409                           "SUCCESS %d\n", retval);
410                 tf->tf_rax = retval;
411         }
412         return true;
413 }
414
415 bool dune_sys_dup2(struct vm_trapframe *tf)
416 {
417         int retval = dup2((int) tf->tf_rdi, (int)tf->tf_rsi);
418         int err = errno;
419
420         if (retval == -1) {
421                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
422                           "ERROR %d\n", err);
423                 tf->tf_rax = -err;
424         } else {
425                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
426                           "SUCCESS %d\n", retval);
427                 tf->tf_rax = retval;
428         }
429         return true;
430 }
431
432 bool dune_sys_umask(struct vm_trapframe *tf)
433 {
434         //Umask always succeeds
435         int retval = umask((mode_t) tf->tf_rdi);
436
437         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false, "SUCCESS %d\n", retval);
438         tf->tf_rax = retval;
439         return true;
440 }
441
442 bool dune_sys_clock_gettime(struct vm_trapframe *tf)
443 {
444         int retval = clock_gettime(tf->tf_rdi, (struct timespec*) tf->tf_rsi);
445         int err = errno;
446
447         if (retval == -1) {
448                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
449                           "ERROR %d\n", err);
450                 tf->tf_rax = -err;
451         } else {
452                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
453                           "SUCCESS %d\n", retval);
454                 tf->tf_rax = retval;
455         }
456         return true;
457 }
458
459 bool dune_sys_munmap(struct vm_trapframe *tf)
460 {
461         int retval = munmap((void *) tf->tf_rdi, tf->tf_rsi);
462         int err = errno;
463
464         if (retval == -1) {
465                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
466                           "ERROR %d\n", err);
467                 tf->tf_rax = -err;
468         } else {
469                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
470                           "SUCCESS %d\n", retval);
471                 tf->tf_rax = retval;
472         }
473         return true;
474 }
475
476 bool dune_sys_futex(struct vm_trapframe *tf)
477 {
478         int *uaddr = (int*) tf->tf_rdi;
479         int op = (int) tf->tf_rsi;
480         int val = (int) tf->tf_rdx;
481         struct timespec *timeout = (struct timespec *) tf->tf_r10;
482         int *uaddr2 = (int*) tf->tf_r8;
483         int val3 = (int) tf->tf_r9;
484         int retval = futex(uaddr, op, val, timeout, uaddr2, val3);
485         int err = errno;
486
487         if (retval == -1) {
488                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
489                           "ERROR %d\n", err);
490                 tf->tf_rax = -err;
491         } else {
492                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
493                           "SUCCESS %d\n", retval);
494                 tf->tf_rax = retval;
495         }
496         return true;
497 }
498
499 bool dune_sys_gettid(struct vm_trapframe *tf)
500 {
501         // Gettid always succeeds
502         int retval = tf->tf_guest_pcoreid;
503
504         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false, "SUCCESS %d\n", retval);
505         tf->tf_rax = retval;
506         return true;
507 }
508
509 /////////////////////////////////////////////////////////
510 // Modified Syscalls (Partially implemented in Akaros)
511 ////////////////////////////////////////////////////////
512
513 bool dune_sys_open(struct vm_trapframe *tf)
514 {
515         const char *file = (const char *) tf->tf_rdi;
516         int flags = tf->tf_rsi;
517
518         flags = translate_open_flags(flags);
519         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
520                   "Trying to open \"%s\"\n", file);
521
522         int retval  = open(file, flags);
523         int err = errno;
524
525         if (retval == -1) {
526                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
527                           "ERROR %d\n", err);
528                 tf->tf_rax = -err;
529                 return true;
530         }
531
532         if (!update_fd_map(retval, file))
533                 panic("[TID %d] %s: ERROR in updating fd_mapping\n",
534                       tf->tf_guest_pcoreid, dune_syscall_table[tf->tf_rax].name);
535
536         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
537                   "SUCCESS %d\n", retval);
538         tf->tf_rax = retval;
539         return true;
540 }
541
542 bool dune_sys_openat(struct vm_trapframe *tf)
543 {
544
545         int fd = (int) tf->tf_rdi;
546         const char *s = (const char *) tf->tf_rsi;
547         int flags = (int) tf->tf_rdx;
548         char *s_absolute = NULL;
549         int retval;
550         int err;
551
552         // TODO: we panic here on failure, but there are probably instances
553         // where we'd want to recover and return EBADF or ENOTDIR
554         if (!get_absolute_path_from_fd(fd, s, &s_absolute)) {
555                 panic("[TID %d] %s: ERROR in getting absolute path fd was %d, suffix was %s\n",
556                       tf->tf_guest_pcoreid, dune_syscall_table[tf->tf_rax].name, fd, s);
557         }
558
559         flags = translate_open_flags(flags);
560
561         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
562                   "trying to open absolute path %s with translated flags %p\n",
563                   s, flags);
564         retval = open(s_absolute, flags);
565         err = errno;
566
567         if (retval == -1) {
568                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
569                           "ERROR %d\n", err);
570                 tf->tf_rax = -err;
571                 free(s_absolute);
572                 return true;
573         }
574
575         if (!update_fd_map(retval, s_absolute)) {
576                 panic("[TID %d] %s: ERROR in updating fd_mapping\n",
577                       tf->tf_guest_pcoreid,
578                       dune_syscall_table[tf->tf_rax].name);
579         }
580
581         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
582                   "SUCCESS %d\n", retval);
583         free(s_absolute);
584         tf->tf_rax = retval;
585         return true;
586 }
587
588 bool dune_sys_readlinkat(struct vm_trapframe *tf)
589 {
590         int fd = (int) tf->tf_rdi;
591         const char *s = (const char*) tf->tf_rsi;
592         char *buf = (char *) tf->tf_rdx;
593         size_t bufsize = (size_t) tf->tf_r10;
594         ssize_t retval;
595         int err;
596         char *s_absolute = NULL;
597
598
599         if (!get_absolute_path_from_fd(fd, s, &s_absolute)) {
600                 panic("[TID %d] %s: ERROR in getting absolute path fd was %d, suffix was %s\n",
601                       tf->tf_guest_pcoreid, dune_syscall_table[tf->tf_rax].name, fd,
602                       s);
603         }
604
605         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
606                   "trying to readlink %s\n", s_absolute);
607         retval = readlink(s_absolute, buf, bufsize);
608         err = errno;
609         free(s_absolute);
610
611         if (retval == -1) {
612                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
613                           "ERROR %d\n", err);
614                 tf->tf_rax = -err;
615         } else {
616                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
617                           "SUCCESS %zd\n", retval);
618                 tf->tf_rax = retval;
619         }
620         return true;
621 }
622
623 bool dune_sys_unlinkat(struct vm_trapframe *tf)
624 {
625         int fd = (int) tf->tf_rdi;
626         const char *s = (const char*) tf->tf_rsi;
627         int flags = (int) tf->tf_rdx;
628         char *s_absolute = NULL;
629         int retval, err;
630
631         if (!get_absolute_path_from_fd(fd, s, &s_absolute)) {
632                 panic("[TID %d] %s: ERROR in getting absolute path fd was %d, suffix was %s\n",
633                       tf->tf_guest_pcoreid, dune_syscall_table[tf->tf_rax].name, fd,
634                       s);
635         }
636
637         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
638                   "trying to unlink %s\n", s_absolute);
639         retval = unlink(s_absolute);
640         err = errno;
641         free(s_absolute);
642
643         if (retval == -1) {
644                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
645                           "ERROR %d\n", err);
646                 tf->tf_rax = -err;
647         } else {
648                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
649                           "SUCCESS %d\n", retval);
650                 tf->tf_rax = retval;
651         }
652         return true;
653 }
654
655 bool dune_sys_close(struct vm_trapframe *tf)
656 {
657         int fd = tf->tf_rdi;
658         int retval, err;
659
660         retval = close(fd);
661         err = errno;
662
663         if (retval == -1) {
664                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
665                           "ERROR %d\n", err);
666                 tf->tf_rax = -err;
667                 return true;
668         }
669
670         if (!update_fd_map(fd, NULL)) {
671                 panic("[TID %d] %s: ERROR in updating fd_mapping\n",
672                       tf->tf_guest_pcoreid,
673                       dune_syscall_table[tf->tf_rax].name);
674         }
675
676         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
677                   "SUCCESS %d\n", retval);
678         tf->tf_rax = retval;
679         return true;
680 }
681
682 bool dune_sys_sched_yield(struct vm_trapframe *tf)
683 {
684         tf->tf_rax = 0;
685         uthread_sched_yield();
686         return true;
687 }
688
689
690 bool dune_sys_fstat(struct vm_trapframe *tf)
691 {
692         struct stat si_akaros_val;
693         int fd = tf->tf_rdi;
694         struct linux_stat_amd64 *si = (struct linux_stat_amd64*) tf->tf_rsi;
695
696         // TODO(ganshun): Check if mmaped
697         if (!si) {
698                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
699                           "ERROR %d\n", EFAULT);
700                 tf->tf_rax = -EFAULT;
701                 return true;
702         }
703
704         struct stat *si_akaros = &si_akaros_val;
705
706         // Make sure we zero out the data on the stack
707         memset((void*) si_akaros, 0, sizeof(struct stat));
708
709         int retval = fstat(fd, si_akaros);
710         int err = errno;
711
712         if (retval == -1) {
713                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
714                           "ERROR %d\n", err);
715                 tf->tf_rax = -err;
716         } else {
717                 convert_stat_akaros_to_linux(si_akaros, si);
718                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
719                           "SUCCESS %d\n", retval);
720                 tf->tf_rax = retval;
721         }
722         return true;
723 }
724
725 bool dune_sys_stat(struct vm_trapframe *tf)
726 {
727         struct stat si_akaros_val;
728         const char *path = (const char*) tf->tf_rdi;
729         struct linux_stat_amd64 *si = (struct linux_stat_amd64*) tf->tf_rsi;
730
731         // TODO(ganshun): Check if mmaped
732         if (!si) {
733                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
734                           "ERROR %d\n", EFAULT);
735                 tf->tf_rax = -EFAULT;
736                 return true;
737         }
738
739         struct stat *si_akaros = &si_akaros_val;
740
741         // Make sure we zero out the data on the stack
742         memset((void*) si_akaros, 0, sizeof(struct stat));
743
744         int retval = stat(path, si_akaros);
745         int err = errno;
746
747         if (retval == -1) {
748                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
749                           "ERROR %d\n", err);
750                 tf->tf_rax = -err;
751         } else {
752                 convert_stat_akaros_to_linux(si_akaros, si);
753                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
754                           "SUCCESS %d\n", retval);
755                 tf->tf_rax = retval;
756         }
757         return true;
758 }
759
760 ///////////////////////////////////////////////////
761 // Newly Implemented Syscalls
762 ///////////////////////////////////////////////////
763
764 // Dune implementation of fallocate, it just writes zeros for now
765 int dune_fallocate(int fd, int mode, off_t offset, off_t len)
766 {
767         if (!(mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)))
768                 return posix_fallocate(fd, offset, len);
769
770         if (offset < 0 || len <= 0) {
771                 errno = EINVAL;
772                 return -1;
773         }
774         struct stat st;
775         int ret = fstat(fd, &st);
776
777         if (ret == -1) {
778                 errno == EBADF;
779                 return -1;
780         }
781         if (offset + len >= st.st_size) {
782                 // Panic here as we cannot support changing the size of the file
783                 // right now.
784                 panic("dune_fallocate: we would write over the size of the file!");
785         }
786         if (S_ISFIFO(st.st_mode)) {
787                 errno = ESPIPE;
788                 return -1;
789         }
790         if (!(S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))) {
791                 errno = ENODEV;
792                 return -1;
793         }
794
795         // TODO(ganshun): For punch hole, we just write zeros to the file for now
796         if ((mode & FALLOC_FL_PUNCH_HOLE) && (mode & FALLOC_FL_KEEP_SIZE)) {
797                 const size_t buffer_size = 0x100000;
798                 int pos;
799                 ssize_t amt = 0;
800                 size_t tot = 0;
801                 size_t size;
802                 char *buf = calloc(sizeof(char), buffer_size);
803
804                 if (!buf)
805                         panic("dune_fallocate: could not allocate a buffer\n");
806
807                 for (pos = offset; pos < offset + len; pos += amt) {
808                         size = len + offset - pos;
809                         if (size > buffer_size)
810                                 size = buffer_size;
811                         amt = write(fd, buf, size);
812                         if (amt == -1) {
813                                 free(buf);
814                                 errno = EIO;
815                                 return -1;
816                         }
817                         tot += amt;
818                         fprintf(stderr, "%d bytes written so far\n", tot);
819                 }
820                 free(buf);
821                 return tot;
822         }
823
824         // Unsupported otherwise
825         errno = ENOSYS;
826         return -1;
827 }
828
829 // Fallocate syscall
830 bool dune_sys_fallocate(struct vm_trapframe *tf)
831 {
832         int fd = (int) tf->tf_rdi;
833         int mode = (int) tf->tf_rsi;
834         off_t offset = (off_t) tf->tf_rdx;
835         off_t len = (off_t) tf->tf_r10;
836
837         int retval = dune_fallocate(fd, mode, offset, len);
838         int err = errno;
839
840         if (retval == -1) {
841                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
842                           "ERROR %d\n", err);
843                 tf->tf_rax = -err;
844         } else {
845                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
846                           "SUCCESS %d\n", retval);
847                 tf->tf_rax = retval;
848         }
849         return true;
850 }
851
852 // Currently unsupported
853 bool dune_sys_sched_getaffinity(struct vm_trapframe *tf)
854 {
855         tf->tf_rax = -ENOSYS;
856         return true;
857 }
858
859 // We do not implement pselect; however, some applications may try to
860 // use it as a portable way to sleep. If that is the case, then we
861 // allow it
862 bool dune_sys_pselect6(struct vm_trapframe *tf)
863 {
864         int nfds = (int) tf->tf_rdi;
865         fd_set *readfds = (fd_set *) tf->tf_rsi;
866         fd_set *writefds = (fd_set *) tf->tf_rdx;
867         fd_set *exceptfds = (fd_set *) tf->tf_r10;
868         const struct timespec *timeout = (const struct timespec *) tf->tf_r8;
869         const sigset_t *sigmask = (const sigset_t *) tf->tf_r9;
870
871         // Check if process wants to sleep
872         if (nfds == 0 && readfds == NULL && writefds == NULL && exceptfds == NULL
873             && timeout != NULL) {
874                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
875                           "Sleeping for %ld seconds, %ld nanoseconds\n",
876                           timeout->tv_sec, timeout->tv_nsec);
877                 nanosleep(timeout, NULL);
878                 tf->tf_rax = 0;
879                 return true;
880         }
881
882         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
883                   "unimplemented, will now fail...\n");
884         return false;
885 }
886
887 bool dune_sys_getrandom(struct vm_trapframe *tf)
888 {
889         const char *random_source = "/dev/urandom";
890         void *buf = (void*) tf->tf_rdi;
891         size_t len = (size_t) tf->tf_rsi;
892         unsigned int flags = (unsigned int) tf->tf_rdx;
893
894         if (!buf) {
895                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
896                           "buffer inaccessable\n");
897                 tf->tf_rax = -EFAULT;
898                 return true;
899         }
900         if (flags & GRND_RANDOM || flags & GRND_NONBLOCK) {
901                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
902                           "unsupported flags specified\n");
903                 tf->tf_rax = -EINVAL;
904                 return true;
905         }
906
907         int fd = open(random_source, O_RDONLY);
908         int err = errno;
909
910         if (fd == -1) {
911                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
912                           "ERROR opening random source %s, errno=%d\n", random_source,
913                            err);
914                 return false;
915         }
916
917         ssize_t retval = read(fd, buf, len);
918
919         err = errno;
920         close(fd);
921         if (retval == -1) {
922                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
923                           "ERROR reading from random source %s, errno=%d\n",
924                           random_source, err);
925                 tf->tf_rax = -err;
926         } else {
927                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
928                           "SUCCESS %zd\n", retval);
929                 tf->tf_rax = retval;
930         }
931         return true;
932 }
933
934 /////////////////////////////////////////////////////////////////
935 /// We don't have a good implementation for these syscalls,
936 /// they will not work in all cases
937 /////////////////////////////////////////////////////////////////
938
939 bool dune_sys_getgroups(struct vm_trapframe *tf)
940 {
941         tf->tf_rax = 0;
942         return true;
943 }
944
945
946 bool dune_sys_geteuid(struct vm_trapframe *tf)
947 {
948         tf->tf_rax = 0;
949         return true;
950 }
951
952 bool dune_sys_getegid(struct vm_trapframe *tf)
953 {
954         tf->tf_rax = 0;
955         return true;
956 }
957
958
959 bool dune_sys_getuid(struct vm_trapframe *tf)
960 {
961         tf->tf_rax = 0;
962         return true;
963 }
964
965
966 bool dune_sys_getgid(struct vm_trapframe *tf)
967 {
968         tf->tf_rax = 0;
969         return true;
970 }
971
972 // TODO(ganshun): implement mincore
973 bool dune_sys_mincore(struct vm_trapframe *tf)
974 {
975         tf->tf_rax = -ENOMEM;
976         return true;
977 }
978
979 bool dune_sys_rt_sigprocmask(struct vm_trapframe *tf)
980 {
981         int retval = sigprocmask(tf->tf_rdi, (const sigset_t*) tf->tf_rsi,
982                                  (sigset_t*) tf->tf_rdx);
983         int err = errno;
984
985         if (retval == -1) {
986                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
987                           "ERROR %d\n", err);
988                 tf->tf_rax = -err;
989         } else {
990                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
991                           "SUCCESS %d\n", retval);
992                 tf->tf_rax = retval;
993         }
994         return true;
995 }
996
997 // TODO(ganshun): sigaltstack needs to implemented for the guest
998 bool dune_sys_sigaltstack(struct vm_trapframe *tf)
999 {
1000         tf->tf_rax = 0;
1001         return true;
1002 }
1003
1004
1005 // TODO(ganshun): more signal code, we need to be careful with this one,
1006 // we should not register guest signal handlers in akaros
1007 bool dune_sys_rt_sigaction(struct vm_trapframe *tf)
1008 {
1009         tf->tf_rax = 0;
1010         return true;
1011 }
1012
1013 //TODO(ganshun): we do not support epoll currently except for create and wait
1014 bool dune_sys_epoll_create1(struct vm_trapframe *tf)
1015 {
1016         int flags = 0;
1017         // TODO(ganshun): epoll_create is not fully supported for all flags
1018         // so we ignore the flags variable in the trapframe since it is not used.
1019         int retval = epoll_create1(flags);
1020         int err = errno;
1021
1022         if (retval == -1) {
1023                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
1024                           "ERROR %d\n", err);
1025                 tf->tf_rax = -err;
1026         } else {
1027                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
1028                           "SUCCESS %d\n", retval);
1029                 tf->tf_rax = retval;
1030         }
1031         return true;
1032 }
1033
1034 bool dune_sys_epoll_wait(struct vm_trapframe *tf)
1035 {
1036         int epfd = (int) tf->tf_rdi;
1037         struct epoll_event *events = (struct epoll_event*) tf->tf_rsi;
1038         int maxevents = (int) tf->tf_rdx;
1039         int timeout = (int) tf->tf_r10;
1040         int retval = epoll_wait(epfd, events, maxevents, timeout);
1041         int err = errno;
1042
1043         if (retval == -1) {
1044                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, true,
1045                           "ERROR %d\n", err);
1046                 tf->tf_rax = -err;
1047         } else {
1048                 lemuprint(tf->tf_guest_pcoreid, tf->tf_rax, false,
1049                           "SUCCESS %d\n", retval);
1050                 tf->tf_rax = retval;
1051         }
1052         return true;
1053 }
1054
1055 // Unimplemented
1056 bool dune_sys_epoll_ctl(struct vm_trapframe *tf)
1057 {
1058         tf->tf_rax = -ENOSYS;
1059         return true;
1060 }
1061
1062 // Unimplemented
1063 bool dune_sys_fstatfs(struct vm_trapframe *tf)
1064 {
1065         tf->tf_rax = -ENOSYS;
1066         return true;
1067 }
1068
1069 // Main syscall table
1070 struct dune_sys_table_entry dune_syscall_table[DUNE_MAX_NUM_SYSCALLS] = {
1071         [DUNE_SYS_READ] = {dune_sys_read, "DUNE_SYS_READ"},
1072         [DUNE_SYS_WRITE] = {dune_sys_write, "DUNE_SYS_WRITE"},
1073         [DUNE_SYS_OPEN] = {dune_sys_open, "DUNE_SYS_OPEN"},
1074         [DUNE_SYS_CLOSE] = {dune_sys_close, "DUNE_SYS_CLOSE"},
1075         [DUNE_SYS_STAT] = {dune_sys_stat, "DUNE_SYS_STAT"},
1076         [DUNE_SYS_FSTAT] = {dune_sys_fstat, "DUNE_SYS_FSTAT"},
1077         [DUNE_SYS_LSTAT] = {NULL, "DUNE_SYS_LSTAT"},
1078         [DUNE_SYS_POLL] = {NULL, "DUNE_SYS_POLL"},
1079         [DUNE_SYS_LSEEK] = {NULL, "DUNE_SYS_LSEEK"},
1080         [DUNE_SYS_MMAP] = {NULL, "DUNE_SYS_MMAP"},
1081         [DUNE_SYS_MPROTECT] = {NULL, "DUNE_SYS_MPROTECT"},
1082         [DUNE_SYS_MUNMAP] = {dune_sys_munmap, "DUNE_SYS_MUNMAP"},
1083         [DUNE_SYS_BRK] = {NULL, "DUNE_SYS_BRK"},
1084         [DUNE_SYS_RT_SIGACTION] = {dune_sys_rt_sigaction, "DUNE_SYS_RT_SIGACTION"},
1085         [DUNE_SYS_RT_SIGPROCMASK] = {dune_sys_rt_sigprocmask,
1086                                      "DUNE_SYS_RT_SIGPROCMASK"},
1087         [DUNE_SYS_RT_SIGRETURN] = {NULL, "DUNE_SYS_RT_SIGRETURN"},
1088         [DUNE_SYS_IOCTL] = {NULL, "DUNE_SYS_IOCTL"},
1089         [DUNE_SYS_PREAD64] = {dune_sys_pread64, "DUNE_SYS_PREAD64"},
1090         [DUNE_SYS_PWRITE64] = {dune_sys_pwrite64, "DUNE_SYS_PWRITE64"},
1091         [DUNE_SYS_READV] = {NULL, "DUNE_SYS_READV"},
1092         [DUNE_SYS_WRITEV] = {NULL, "DUNE_SYS_WRITEV"},
1093         [DUNE_SYS_ACCESS] = {NULL, "DUNE_SYS_ACCESS"},
1094         [DUNE_SYS_PIPE] = {NULL, "DUNE_SYS_PIPE"},
1095         [DUNE_SYS_SELECT] = {NULL, "DUNE_SYS_SELECT"},
1096         [DUNE_SYS_SCHED_YIELD] = {dune_sys_sched_yield, "DUNE_SYS_SCHED_YIELD"},
1097         [DUNE_SYS_MREMAP] = {NULL, "DUNE_SYS_MREMAP"},
1098         [DUNE_SYS_MSYNC] = {NULL, "DUNE_SYS_MSYNC"},
1099         [DUNE_SYS_MINCORE] = {dune_sys_mincore, "DUNE_SYS_MINCORE"},
1100         [DUNE_SYS_MADVISE] = {NULL, "DUNE_SYS_MADVISE"},
1101         [DUNE_SYS_SHMGET] = {NULL, "DUNE_SYS_SHMGET"},
1102         [DUNE_SYS_SHMAT] = {NULL, "DUNE_SYS_SHMAT"},
1103         [DUNE_SYS_SHMCTL] = {NULL, "DUNE_SYS_SHMCTL"},
1104         [DUNE_SYS_DUP] = {dune_sys_dup, "DUNE_SYS_DUP"},
1105         [DUNE_SYS_DUP2] = {dune_sys_dup2, "DUNE_SYS_DUP2"},
1106         [DUNE_SYS_PAUSE] = {NULL, "DUNE_SYS_PAUSE"},
1107         [DUNE_SYS_NANOSLEEP] = {NULL, "DUNE_SYS_NANOSLEEP"},
1108         [DUNE_SYS_GETITIMER] = {NULL, "DUNE_SYS_GETITIMER"},
1109         [DUNE_SYS_ALARM] = {NULL, "DUNE_SYS_ALARM"},
1110         [DUNE_SYS_SETITIMER] = {NULL, "DUNE_SYS_SETITIMER"},
1111         [DUNE_SYS_GETPID] = {dune_sys_getpid, "DUNE_SYS_GETPID"},
1112         [DUNE_SYS_SENDFILE] = {NULL, "DUNE_SYS_SENDFILE"},
1113         [DUNE_SYS_SOCKET] = {NULL, "DUNE_SYS_SOCKET"},
1114         [DUNE_SYS_CONNECT] = {NULL, "DUNE_SYS_CONNECT"},
1115         [DUNE_SYS_ACCEPT] = {NULL, "DUNE_SYS_ACCEPT"},
1116         [DUNE_SYS_SENDTO] = {NULL, "DUNE_SYS_SENDTO"},
1117         [DUNE_SYS_RECVFROM] = {NULL, "DUNE_SYS_RECVFROM"},
1118         [DUNE_SYS_SENDMSG] = {NULL, "DUNE_SYS_SENDMSG"},
1119         [DUNE_SYS_RECVMSG] = {NULL, "DUNE_SYS_RECVMSG"},
1120         [DUNE_SYS_SHUTDOWN] = {NULL, "DUNE_SYS_SHUTDOWN"},
1121         [DUNE_SYS_BIND] = {NULL, "DUNE_SYS_BIND"},
1122         [DUNE_SYS_LISTEN] = {NULL, "DUNE_SYS_LISTEN"},
1123         [DUNE_SYS_GETSOCKNAME] = {NULL, "DUNE_SYS_GETSOCKNAME"},
1124         [DUNE_SYS_GETPEERNAME] = {NULL, "DUNE_SYS_GETPEERNAME"},
1125         [DUNE_SYS_SOCKETPAIR] = {NULL, "DUNE_SYS_SOCKETPAIR"},
1126         [DUNE_SYS_SETSOCKOPT] = {NULL, "DUNE_SYS_SETSOCKOPT"},
1127         [DUNE_SYS_GETSOCKOPT] = {NULL, "DUNE_SYS_GETSOCKOPT"},
1128         [DUNE_SYS_CLONE] = {NULL, "DUNE_SYS_CLONE"},
1129         [DUNE_SYS_FORK] = {NULL, "DUNE_SYS_FORK"},
1130         [DUNE_SYS_VFORK] = {NULL, "DUNE_SYS_VFORK"},
1131         [DUNE_SYS_EXECVE] = {NULL, "DUNE_SYS_EXECVE"},
1132         [DUNE_SYS_EXIT] = {NULL, "DUNE_SYS_EXIT"},
1133         [DUNE_SYS_WAIT4] = {NULL, "DUNE_SYS_WAIT4"},
1134         [DUNE_SYS_KILL] = {NULL, "DUNE_SYS_KILL"},
1135         [DUNE_SYS_UNAME] = {NULL, "DUNE_SYS_UNAME"},
1136         [DUNE_SYS_SEMGET] = {NULL, "DUNE_SYS_SEMGET"},
1137         [DUNE_SYS_SEMOP] = {NULL, "DUNE_SYS_SEMOP"},
1138         [DUNE_SYS_SEMCTL] = {NULL, "DUNE_SYS_SEMCTL"},
1139         [DUNE_SYS_SHMDT] = {NULL, "DUNE_SYS_SHMDT"},
1140         [DUNE_SYS_MSGGET] = {NULL, "DUNE_SYS_MSGGET"},
1141         [DUNE_SYS_MSGSND] = {NULL, "DUNE_SYS_MSGSND"},
1142         [DUNE_SYS_MSGRCV] = {NULL, "DUNE_SYS_MSGRCV"},
1143         [DUNE_SYS_MSGCTL] = {NULL, "DUNE_SYS_MSGCTL"},
1144         [DUNE_SYS_FCNTL] = {dune_sys_fcntl, "DUNE_SYS_FCNTL"},
1145         [DUNE_SYS_FLOCK] = {NULL, "DUNE_SYS_FLOCK"},
1146         [DUNE_SYS_FSYNC] = {NULL, "DUNE_SYS_FSYNC"},
1147         [DUNE_SYS_FDATASYNC] = {NULL, "DUNE_SYS_FDATASYNC"},
1148         [DUNE_SYS_TRUNCATE] = {NULL, "DUNE_SYS_TRUNCATE"},
1149         [DUNE_SYS_FTRUNCATE] = {dune_sys_ftruncate, "DUNE_SYS_FTRUNCATE"},
1150         [DUNE_SYS_GETDENTS] = {NULL, "DUNE_SYS_GETDENTS"},
1151         [DUNE_SYS_GETCWD] = {NULL, "DUNE_SYS_GETCWD"},
1152         [DUNE_SYS_CHDIR] = {NULL, "DUNE_SYS_CHDIR"},
1153         [DUNE_SYS_FCHDIR] = {NULL, "DUNE_SYS_FCHDIR"},
1154         [DUNE_SYS_RENAME] = {NULL, "DUNE_SYS_RENAME"},
1155         [DUNE_SYS_MKDIR] = {NULL, "DUNE_SYS_MKDIR"},
1156         [DUNE_SYS_RMDIR] = {NULL, "DUNE_SYS_RMDIR"},
1157         [DUNE_SYS_CREAT] = {NULL, "DUNE_SYS_CREAT"},
1158         [DUNE_SYS_LINK] = {NULL, "DUNE_SYS_LINK"},
1159         [DUNE_SYS_UNLINK] = {NULL, "DUNE_SYS_UNLINK"},
1160         [DUNE_SYS_SYMLINK] = {NULL, "DUNE_SYS_SYMLINK"},
1161         [DUNE_SYS_READLINK] = {NULL, "DUNE_SYS_READLINK"},
1162         [DUNE_SYS_CHMOD] = {NULL, "DUNE_SYS_CHMOD"},
1163         [DUNE_SYS_FCHMOD] = {NULL, "DUNE_SYS_FCHMOD"},
1164         [DUNE_SYS_CHOWN] = {NULL, "DUNE_SYS_CHOWN"},
1165         [DUNE_SYS_FCHOWN] = {NULL, "DUNE_SYS_FCHOWN"},
1166         [DUNE_SYS_LCHOWN] = {NULL, "DUNE_SYS_LCHOWN"},
1167         [DUNE_SYS_UMASK] = {dune_sys_umask, "DUNE_SYS_UMASK"},
1168         [DUNE_SYS_GETTIMEOFDAY] = {dune_sys_gettimeofday, "DUNE_SYS_GETTIMEOFDAY"},
1169         [DUNE_SYS_GETRLIMIT] = {NULL, "DUNE_SYS_GETRLIMIT"},
1170         [DUNE_SYS_GETRUSAGE] = {NULL, "DUNE_SYS_GETRUSAGE"},
1171         [DUNE_SYS_SYSINFO] = {NULL, "DUNE_SYS_SYSINFO"},
1172         [DUNE_SYS_TIMES] = {NULL, "DUNE_SYS_TIMES"},
1173         [DUNE_SYS_PTRACE] = {NULL, "DUNE_SYS_PTRACE"},
1174         [DUNE_SYS_GETUID] = {dune_sys_getuid, "DUNE_SYS_GETUID"},
1175         [DUNE_SYS_SYSLOG] = {NULL, "DUNE_SYS_SYSLOG"},
1176         [DUNE_SYS_GETGID] = {dune_sys_getgid, "DUNE_SYS_GETGID"},
1177         [DUNE_SYS_SETUID] = {NULL, "DUNE_SYS_SETUID"},
1178         [DUNE_SYS_SETGID] = {NULL, "DUNE_SYS_SETGID"},
1179         [DUNE_SYS_GETEUID] = {dune_sys_geteuid, "DUNE_SYS_GETEUID"},
1180         [DUNE_SYS_GETEGID] = {dune_sys_getegid, "DUNE_SYS_GETEGID"},
1181         [DUNE_SYS_SETPGID] = {NULL, "DUNE_SYS_SETPGID"},
1182         [DUNE_SYS_GETPPID] = {NULL, "DUNE_SYS_GETPPID"},
1183         [DUNE_SYS_GETPGRP] = {NULL, "DUNE_SYS_GETPGRP"},
1184         [DUNE_SYS_SETSID] = {NULL, "DUNE_SYS_SETSID"},
1185         [DUNE_SYS_SETREUID] = {NULL, "DUNE_SYS_SETREUID"},
1186         [DUNE_SYS_SETREGID] = {NULL, "DUNE_SYS_SETREGID"},
1187         [DUNE_SYS_GETGROUPS] = {dune_sys_getgroups, "DUNE_SYS_GETGROUPS"},
1188         [DUNE_SYS_SETGROUPS] = {NULL, "DUNE_SYS_SETGROUPS"},
1189         [DUNE_SYS_SETRESUID] = {NULL, "DUNE_SYS_SETRESUID"},
1190         [DUNE_SYS_GETRESUID] = {NULL, "DUNE_SYS_GETRESUID"},
1191         [DUNE_SYS_SETRESGID] = {NULL, "DUNE_SYS_SETRESGID"},
1192         [DUNE_SYS_GETRESGID] = {NULL, "DUNE_SYS_GETRESGID"},
1193         [DUNE_SYS_GETPGID] = {NULL, "DUNE_SYS_GETPGID"},
1194         [DUNE_SYS_SETFSUID] = {NULL, "DUNE_SYS_SETFSUID"},
1195         [DUNE_SYS_SETFSGID] = {NULL, "DUNE_SYS_SETFSGID"},
1196         [DUNE_SYS_GETSID] = {NULL, "DUNE_SYS_GETSID"},
1197         [DUNE_SYS_CAPGET] = {NULL, "DUNE_SYS_CAPGET"},
1198         [DUNE_SYS_CAPSET] = {NULL, "DUNE_SYS_CAPSET"},
1199         [DUNE_SYS_RT_SIGPENDING] = {NULL, "DUNE_SYS_RT_SIGPENDING"},
1200         [DUNE_SYS_RT_SIGTIMEDWAIT] = {NULL, "DUNE_SYS_RT_SIGTIMEDWAIT"},
1201         [DUNE_SYS_RT_SIGQUEUEINFO] = {NULL, "DUNE_SYS_RT_SIGQUEUEINFO"},
1202         [DUNE_SYS_RT_SIGSUSPEND] = {NULL, "DUNE_SYS_RT_SIGSUSPEND"},
1203         [DUNE_SYS_SIGALTSTACK] = {dune_sys_sigaltstack, "DUNE_SYS_SIGALTSTACK"},
1204         [DUNE_SYS_UTIME] = {NULL, "DUNE_SYS_UTIME"},
1205         [DUNE_SYS_MKNOD] = {NULL, "DUNE_SYS_MKNOD"},
1206         [DUNE_SYS_USELIB] = {NULL, "DUNE_SYS_USELIB"},
1207         [DUNE_SYS_PERSONALITY] = {NULL, "DUNE_SYS_PERSONALITY"},
1208         [DUNE_SYS_USTAT] = {NULL, "DUNE_SYS_USTAT"},
1209         [DUNE_SYS_STATFS] = {NULL, "DUNE_SYS_STATFS"},
1210         [DUNE_SYS_FSTATFS] = {dune_sys_fstatfs, "DUNE_SYS_FSTATFS"},
1211         [DUNE_SYS_SYSFS] = {NULL, "DUNE_SYS_SYSFS"},
1212         [DUNE_SYS_GETPRIORITY] = {NULL, "DUNE_SYS_GETPRIORITY"},
1213         [DUNE_SYS_SETPRIORITY] = {NULL, "DUNE_SYS_SETPRIORITY"},
1214         [DUNE_SYS_SCHED_SETPARAM] = {NULL, "DUNE_SYS_SCHED_SETPARAM"},
1215         [DUNE_SYS_SCHED_GETPARAM] = {NULL, "DUNE_SYS_SCHED_GETPARAM"},
1216         [DUNE_SYS_SCHED_SETSCHEDULER] = {NULL, "DUNE_SYS_SCHED_SETSCHEDULER"},
1217         [DUNE_SYS_SCHED_GETSCHEDULER] = {NULL, "DUNE_SYS_SCHED_GETSCHEDULER"},
1218         [DUNE_SYS_SCHED_GET_PRIORITY_MAX] = {NULL,
1219                                             "DUNE_SYS_SCHED_GET_PRIORITY_MAX"},
1220         [DUNE_SYS_SCHED_GET_PRIORITY_MIN] = {NULL,
1221                                             "DUNE_SYS_SCHED_GET_PRIORITY_MIN"},
1222         [DUNE_SYS_SCHED_RR_GET_INTERVAL] = {NULL, "DUNE_SYS_SCHED_RR_GET_INTERVAL"},
1223         [DUNE_SYS_MLOCK] = {NULL, "DUNE_SYS_MLOCK"},
1224         [DUNE_SYS_MUNLOCK] = {NULL, "DUNE_SYS_MUNLOCK"},
1225         [DUNE_SYS_MLOCKALL] = {NULL, "DUNE_SYS_MLOCKALL"},
1226         [DUNE_SYS_MUNLOCKALL] = {NULL, "DUNE_SYS_MUNLOCKALL"},
1227         [DUNE_SYS_VHANGUP] = {NULL, "DUNE_SYS_VHANGUP"},
1228         [DUNE_SYS_MODIFY_LDT] = {NULL, "DUNE_SYS_MODIFY_LDT"},
1229         [DUNE_SYS_PIVOT_ROOT] = {NULL, "DUNE_SYS_PIVOT_ROOT"},
1230         [DUNE_SYS__SYSCTL] = {NULL, "DUNE_SYS__SYSCTL"},
1231         [DUNE_SYS_PRCTL] = {NULL, "DUNE_SYS_PRCTL"},
1232         [DUNE_SYS_ARCH_PRCTL] = {NULL, "DUNE_SYS_ARCH_PRCTL"},
1233         [DUNE_SYS_ADJTIMEX] = {NULL, "DUNE_SYS_ADJTIMEX"},
1234         [DUNE_SYS_SETRLIMIT] = {NULL, "DUNE_SYS_SETRLIMIT"},
1235         [DUNE_SYS_CHROOT] = {NULL, "DUNE_SYS_CHROOT"},
1236         [DUNE_SYS_SYNC] = {NULL, "DUNE_SYS_SYNC"},
1237         [DUNE_SYS_ACCT] = {NULL, "DUNE_SYS_ACCT"},
1238         [DUNE_SYS_SETTIMEOFDAY] = {NULL, "DUNE_SYS_SETTIMEOFDAY"},
1239         [DUNE_SYS_MOUNT] = {NULL, "DUNE_SYS_MOUNT"},
1240         [DUNE_SYS_UMOUNT2] = {NULL, "DUNE_SYS_UMOUNT2"},
1241         [DUNE_SYS_SWAPON] = {NULL, "DUNE_SYS_SWAPON"},
1242         [DUNE_SYS_SWAPOFF] = {NULL, "DUNE_SYS_SWAPOFF"},
1243         [DUNE_SYS_REBOOT] = {NULL, "DUNE_SYS_REBOOT"},
1244         [DUNE_SYS_SETHOSTNAME] = {NULL, "DUNE_SYS_SETHOSTNAME"},
1245         [DUNE_SYS_SETDOMAINNAME] = {NULL, "DUNE_SYS_SETDOMAINNAME"},
1246         [DUNE_SYS_IOPL] = {NULL, "DUNE_SYS_IOPL"},
1247         [DUNE_SYS_IOPERM] = {NULL, "DUNE_SYS_IOPERM"},
1248         [DUNE_SYS_CREATE_MODULE] = {NULL, "DUNE_SYS_CREATE_MODULE"},
1249         [DUNE_SYS_INIT_MODULE] = {NULL, "DUNE_SYS_INIT_MODULE"},
1250         [DUNE_SYS_DELETE_MODULE] = {NULL, "DUNE_SYS_DELETE_MODULE"},
1251         [DUNE_SYS_GET_KERNEL_SYMS] = {NULL, "DUNE_SYS_GET_KERNEL_SYMS"},
1252         [DUNE_SYS_QUERY_MODULE] = {NULL, "DUNE_SYS_QUERY_MODULE"},
1253         [DUNE_SYS_QUOTACTL] = {NULL, "DUNE_SYS_QUOTACTL"},
1254         [DUNE_SYS_NFSSERVCTL] = {NULL, "DUNE_SYS_NFSSERVCTL"},
1255         [DUNE_SYS_GETPMSG] = {NULL, "DUNE_SYS_GETPMSG"},
1256         [DUNE_SYS_PUTPMSG] = {NULL, "DUNE_SYS_PUTPMSG"},
1257         [DUNE_SYS_AFS_SYSCALL] = {NULL, "DUNE_SYS_AFS_SYSCALL"},
1258         [DUNE_SYS_TUXCALL] = {NULL, "DUNE_SYS_TUXCALL"},
1259         [DUNE_SYS_SECURITY] = {NULL, "DUNE_SYS_SECURITY"},
1260         [DUNE_SYS_GETTID] = {dune_sys_gettid, "DUNE_SYS_GETTID"},
1261         [DUNE_SYS_READAHEAD] = {NULL, "DUNE_SYS_READAHEAD"},
1262         [DUNE_SYS_SETXATTR] = {NULL, "DUNE_SYS_SETXATTR"},
1263         [DUNE_SYS_LSETXATTR] = {NULL, "DUNE_SYS_LSETXATTR"},
1264         [DUNE_SYS_FSETXATTR] = {NULL, "DUNE_SYS_FSETXATTR"},
1265         [DUNE_SYS_GETXATTR] = {NULL, "DUNE_SYS_GETXATTR"},
1266         [DUNE_SYS_LGETXATTR] = {NULL, "DUNE_SYS_LGETXATTR"},
1267         [DUNE_SYS_FGETXATTR] = {NULL, "DUNE_SYS_FGETXATTR"},
1268         [DUNE_SYS_LISTXATTR] = {NULL, "DUNE_SYS_LISTXATTR"},
1269         [DUNE_SYS_LLISTXATTR] = {NULL, "DUNE_SYS_LLISTXATTR"},
1270         [DUNE_SYS_FLISTXATTR] = {NULL, "DUNE_SYS_FLISTXATTR"},
1271         [DUNE_SYS_REMOVEXATTR] = {NULL, "DUNE_SYS_REMOVEXATTR"},
1272         [DUNE_SYS_LREMOVEXATTR] = {NULL, "DUNE_SYS_LREMOVEXATTR"},
1273         [DUNE_SYS_FREMOVEXATTR] = {NULL, "DUNE_SYS_FREMOVEXATTR"},
1274         [DUNE_SYS_TKILL] = {NULL, "DUNE_SYS_TKILL"},
1275         [DUNE_SYS_TIME] = {NULL, "DUNE_SYS_TIME"},
1276         [DUNE_SYS_FUTEX] = {dune_sys_futex, "DUNE_SYS_FUTEX"},
1277         [DUNE_SYS_SCHED_SETAFFINITY] = {NULL, "DUNE_SYS_SCHED_SETAFFINITY"},
1278         [DUNE_SYS_SCHED_GETAFFINITY] = {dune_sys_sched_getaffinity,
1279                                         "DUNE_SYS_SCHED_GETAFFINITY"},
1280         [DUNE_SYS_SET_THREAD_AREA] = {NULL, "DUNE_SYS_SET_THREAD_AREA"},
1281         [DUNE_SYS_IO_SETUP] = {NULL, "DUNE_SYS_IO_SETUP"},
1282         [DUNE_SYS_IO_DESTROY] = {NULL, "DUNE_SYS_IO_DESTROY"},
1283         [DUNE_SYS_IO_GETEVENTS] = {NULL, "DUNE_SYS_IO_GETEVENTS"},
1284         [DUNE_SYS_IO_SUBMIT] = {NULL, "DUNE_SYS_IO_SUBMIT"},
1285         [DUNE_SYS_IO_CANCEL] = {NULL, "DUNE_SYS_IO_CANCEL"},
1286         [DUNE_SYS_GET_THREAD_AREA] = {NULL, "DUNE_SYS_GET_THREAD_AREA"},
1287         [DUNE_SYS_LOOKUP_DCOOKIE] = {NULL, "DUNE_SYS_LOOKUP_DCOOKIE"},
1288         [DUNE_SYS_EPOLL_CREATE] = {NULL, "DUNE_SYS_EPOLL_CREATE"},
1289         [DUNE_SYS_EPOLL_CTL_OLD] = {NULL, "DUNE_SYS_EPOLL_CTL_OLD"},
1290         [DUNE_SYS_EPOLL_WAIT_OLD] = {NULL, "DUNE_SYS_EPOLL_WAIT_OLD"},
1291         [DUNE_SYS_REMAP_FILE_PAGES] = {NULL, "DUNE_SYS_REMAP_FILE_PAGES"},
1292         [DUNE_SYS_GETDENTS64] = {NULL, "DUNE_SYS_GETDENTS64"},
1293         [DUNE_SYS_SET_TID_ADDRESS] = {NULL, "DUNE_SYS_SET_TID_ADDRESS"},
1294         [DUNE_SYS_RESTART_SYSCALL] = {NULL, "DUNE_SYS_RESTART_SYSCALL"},
1295         [DUNE_SYS_SEMTIMEDOP] = {NULL, "DUNE_SYS_SEMTIMEDOP"},
1296         [DUNE_SYS_FADVISE64] = {NULL, "DUNE_SYS_FADVISE64"},
1297         [DUNE_SYS_TIMER_CREATE] = {NULL, "DUNE_SYS_TIMER_CREATE"},
1298         [DUNE_SYS_TIMER_SETTIME] = {NULL, "DUNE_SYS_TIMER_SETTIME"},
1299         [DUNE_SYS_TIMER_GETTIME] = {NULL, "DUNE_SYS_TIMER_GETTIME"},
1300         [DUNE_SYS_TIMER_GETOVERRUN] = {NULL, "DUNE_SYS_TIMER_GETOVERRUN"},
1301         [DUNE_SYS_TIMER_DELETE] = {NULL, "DUNE_SYS_TIMER_DELETE"},
1302         [DUNE_SYS_CLOCK_SETTIME] = {NULL, "DUNE_SYS_CLOCK_SETTIME"},
1303         [DUNE_SYS_CLOCK_GETTIME] = {dune_sys_clock_gettime,
1304                                     "DUNE_SYS_CLOCK_GETTIME"},
1305         [DUNE_SYS_CLOCK_GETRES] = {NULL, "DUNE_SYS_CLOCK_GETRES"},
1306         [DUNE_SYS_CLOCK_NANOSLEEP] = {NULL, "DUNE_SYS_CLOCK_NANOSLEEP"},
1307         [DUNE_SYS_EXIT_GROUP] = {NULL, "DUNE_SYS_EXIT_GROUP"},
1308         [DUNE_SYS_EPOLL_WAIT] = {dune_sys_epoll_wait, "DUNE_SYS_EPOLL_WAIT"},
1309         [DUNE_SYS_EPOLL_CTL] = {dune_sys_epoll_ctl, "DUNE_SYS_EPOLL_CTL"},
1310         [DUNE_SYS_TGKILL] = {NULL, "DUNE_SYS_TGKILL"},
1311         [DUNE_SYS_UTIMES] = {NULL, "DUNE_SYS_UTIMES"},
1312         [DUNE_SYS_VSERVER] = {NULL, "DUNE_SYS_VSERVER"},
1313         [DUNE_SYS_MBIND] = {NULL, "DUNE_SYS_MBIND"},
1314         [DUNE_SYS_SET_MEMPOLICY] = {NULL, "DUNE_SYS_SET_MEMPOLICY"},
1315         [DUNE_SYS_GET_MEMPOLICY] = {NULL, "DUNE_SYS_GET_MEMPOLICY"},
1316         [DUNE_SYS_MQ_OPEN] = {NULL, "DUNE_SYS_MQ_OPEN"},
1317         [DUNE_SYS_MQ_UNLINK] = {NULL, "DUNE_SYS_MQ_UNLINK"},
1318         [DUNE_SYS_MQ_TIMEDSEND] = {NULL, "DUNE_SYS_MQ_TIMEDSEND"},
1319         [DUNE_SYS_MQ_TIMEDRECEIVE] = {NULL, "DUNE_SYS_MQ_TIMEDRECEIVE"},
1320         [DUNE_SYS_MQ_NOTIFY] = {NULL, "DUNE_SYS_MQ_NOTIFY"},
1321         [DUNE_SYS_MQ_GETSETATTR] = {NULL, "DUNE_SYS_MQ_GETSETATTR"},
1322         [DUNE_SYS_KEXEC_LOAD] = {NULL, "DUNE_SYS_KEXEC_LOAD"},
1323         [DUNE_SYS_WAITID] = {NULL, "DUNE_SYS_WAITID"},
1324         [DUNE_SYS_ADD_KEY] = {NULL, "DUNE_SYS_ADD_KEY"},
1325         [DUNE_SYS_REQUEST_KEY] = {NULL, "DUNE_SYS_REQUEST_KEY"},
1326         [DUNE_SYS_KEYCTL] = {NULL, "DUNE_SYS_KEYCTL"},
1327         [DUNE_SYS_IOPRIO_SET] = {NULL, "DUNE_SYS_IOPRIO_SET"},
1328         [DUNE_SYS_IOPRIO_GET] = {NULL, "DUNE_SYS_IOPRIO_GET"},
1329         [DUNE_SYS_INOTIFY_INIT] = {NULL, "DUNE_SYS_INOTIFY_INIT"},
1330         [DUNE_SYS_INOTIFY_ADD_WATCH] = {NULL, "DUNE_SYS_INOTIFY_ADD_WATCH"},
1331         [DUNE_SYS_INOTIFY_RM_WATCH] = {NULL, "DUNE_SYS_INOTIFY_RM_WATCH"},
1332         [DUNE_SYS_MIGRATE_PAGES] = {NULL, "DUNE_SYS_MIGRATE_PAGES"},
1333         [DUNE_SYS_OPENAT] = {dune_sys_openat, "DUNE_SYS_OPENAT"},
1334         [DUNE_SYS_MKDIRAT] = {NULL, "DUNE_SYS_MKDIRAT"},
1335         [DUNE_SYS_MKNODAT] = {NULL, "DUNE_SYS_MKNODAT"},
1336         [DUNE_SYS_FCHOWNAT] = {NULL, "DUNE_SYS_FCHOWNAT"},
1337         [DUNE_SYS_FUTIMESAT] = {NULL, "DUNE_SYS_FUTIMESAT"},
1338         [DUNE_SYS_NEWFSTATAT] = {NULL, "DUNE_SYS_NEWFSTATAT"},
1339         [DUNE_SYS_UNLINKAT] = {dune_sys_unlinkat, "DUNE_SYS_UNLINKAT"},
1340         [DUNE_SYS_RENAMEAT] = {NULL, "DUNE_SYS_RENAMEAT"},
1341         [DUNE_SYS_LINKAT] = {NULL, "DUNE_SYS_LINKAT"},
1342         [DUNE_SYS_SYMLINKAT] = {NULL, "DUNE_SYS_SYMLINKAT"},
1343         [DUNE_SYS_READLINKAT] = {dune_sys_readlinkat, "DUNE_SYS_READLINKAT"},
1344         [DUNE_SYS_FCHMODAT] = {NULL, "DUNE_SYS_FCHMODAT"},
1345         [DUNE_SYS_FACCESSAT] = {NULL, "DUNE_SYS_FACCESSAT"},
1346         [DUNE_SYS_PSELECT6] = {dune_sys_pselect6, "DUNE_SYS_PSELECT6"},
1347         [DUNE_SYS_PPOLL] = {NULL, "DUNE_SYS_PPOLL"},
1348         [DUNE_SYS_UNSHARE] = {NULL, "DUNE_SYS_UNSHARE"},
1349         [DUNE_SYS_SET_ROBUST_LIST] = {NULL, "DUNE_SYS_SET_ROBUST_LIST"},
1350         [DUNE_SYS_GET_ROBUST_LIST] = {NULL, "DUNE_SYS_GET_ROBUST_LIST"},
1351         [DUNE_SYS_SPLICE] = {NULL, "DUNE_SYS_SPLICE"},
1352         [DUNE_SYS_TEE] = {NULL, "DUNE_SYS_TEE"},
1353         [DUNE_SYS_SYNC_FILE_RANGE] = {NULL, "DUNE_SYS_SYNC_FILE_RANGE"},
1354         [DUNE_SYS_VMSPLICE] = {NULL, "DUNE_SYS_VMSPLICE"},
1355         [DUNE_SYS_MOVE_PAGES] = {NULL, "DUNE_SYS_MOVE_PAGES"},
1356         [DUNE_SYS_UTIMENSAT] = {NULL, "DUNE_SYS_UTIMENSAT"},
1357         [DUNE_SYS_EPOLL_PWAIT] = {NULL, "DUNE_SYS_EPOLL_PWAIT"},
1358         [DUNE_SYS_SIGNALFD] = {NULL, "DUNE_SYS_SIGNALFD"},
1359         [DUNE_SYS_TIMERFD_CREATE] = {NULL, "DUNE_SYS_TIMERFD_CREATE"},
1360         [DUNE_SYS_EVENTFD] = {NULL, "DUNE_SYS_EVENTFD"},
1361         [DUNE_SYS_FALLOCATE] = {dune_sys_fallocate, "DUNE_SYS_FALLOCATE"},
1362         [DUNE_SYS_TIMERFD_SETTIME] = {NULL, "DUNE_SYS_TIMERFD_SETTIME"},
1363         [DUNE_SYS_TIMERFD_GETTIME] = {NULL, "DUNE_SYS_TIMERFD_GETTIME"},
1364         [DUNE_SYS_ACCEPT4] = {NULL, "DUNE_SYS_ACCEPT4"},
1365         [DUNE_SYS_SIGNALFD4] = {NULL, "DUNE_SYS_SIGNALFD4"},
1366         [DUNE_SYS_EVENTFD2] = {NULL, "DUNE_SYS_EVENTFD2"},
1367         [DUNE_SYS_EPOLL_CREATE1] = {dune_sys_epoll_create1,
1368                                     "DUNE_SYS_EPOLL_CREATE1"},
1369         [DUNE_SYS_DUP3] = {NULL, "DUNE_SYS_DUP3"},
1370         [DUNE_SYS_PIPE2] = {NULL, "DUNE_SYS_PIPE2"},
1371         [DUNE_SYS_INOTIFY_INIT1] = {NULL, "DUNE_SYS_INOTIFY_INIT1"},
1372         [DUNE_SYS_PREADV] = {NULL, "DUNE_SYS_PREADV"},
1373         [DUNE_SYS_PWRITEV] = {NULL, "DUNE_SYS_PWRITEV"},
1374         [DUNE_SYS_RT_TGSIGQUEUEINFO] = {NULL, "DUNE_SYS_RT_TGSIGQUEUEINFO"},
1375         [DUNE_SYS_PERF_EVENT_OPEN] = {NULL, "DUNE_SYS_PERF_EVENT_OPEN"},
1376         [DUNE_SYS_RECVMMSG] = {NULL, "DUNE_SYS_RECVMMSG"},
1377         [DUNE_SYS_FANOTIFY_INIT] = {NULL, "DUNE_SYS_FANOTIFY_INIT"},
1378         [DUNE_SYS_FANOTIFY_MARK] = {NULL, "DUNE_SYS_FANOTIFY_MARK"},
1379         [DUNE_SYS_PRLIMIT64] = {NULL, "DUNE_SYS_PRLIMIT64"},
1380         [DUNE_SYS_NAME_TO_HANDLE_AT] = {NULL, "DUNE_SYS_NAME_TO_HANDLE_AT"},
1381         [DUNE_SYS_OPEN_BY_HANDLE_AT] = {NULL, "DUNE_SYS_OPEN_BY_HANDLE_AT"},
1382         [DUNE_SYS_CLOCK_ADJTIME] = {NULL, "DUNE_SYS_CLOCK_ADJTIME"},
1383         [DUNE_SYS_SYNCFS] = {NULL, "DUNE_SYS_SYNCFS"},
1384         [DUNE_SYS_SENDMMSG] = {NULL, "DUNE_SYS_SENDMMSG"},
1385         [DUNE_SYS_SETNS] = {NULL, "DUNE_SYS_SETNS"},
1386         [DUNE_SYS_GETCPU] = {NULL, "DUNE_SYS_GETCPU"},
1387         [DUNE_SYS_PROCESS_VM_READV] = {NULL, "DUNE_SYS_PROCESS_VM_READV"},
1388         [DUNE_SYS_PROCESS_VM_WRITEV] = {NULL, "DUNE_SYS_PROCESS_VM_WRITEV"},
1389         [DUNE_SYS_KCMP] = {NULL, "DUNE_KCMP"},
1390         [DUNE_SYS_FINIT_MODULE] = {NULL, "DUNE_SYS_FINIT_MODULE"},
1391         [DUNE_SYS_SCHED_SETATTR] = {NULL, "DUNE_SYS_SCHED_SETATTR"},
1392         [DUNE_SYS_SCHED_GETATTR] = {NULL, "DUNE_SYS_SCHED_GETATTR"},
1393         [DUNE_SYS_RENAMEAT2] = {NULL, "DUNE_SYS_RENAMEAT2"},
1394         [DUNE_SYS_SECCOMP] = {NULL, "DUNE_SYS_SECCOMP"},
1395         [DUNE_SYS_GETRANDOM] = {dune_sys_getrandom, "DUNE_SYS_GETRANDOM"},
1396         [DUNE_SYS_MEMFD_CREATE] = {NULL, "DUNE_SYS_MEMFD_CREATE"},
1397         [DUNE_SYS_KEXEC_FILE_LOAD] = {NULL, "DUNE_SYS_KEXEC_FILE_LOAD"},
1398         [DUNE_SYS_BPF] = {NULL, "DUNE_SYS_BPF"},
1399         [DUNE_STUB_EXECVEAT] = {NULL, "DUNE_STUB_EXECVEAT"},
1400         [DUNE_USERFAULTFD] = {NULL, "DUNE_USERFAULTFD"},
1401         [DUNE_MEMBARRIER] = {NULL, "DUNE_MEMBARRIER"},
1402         [DUNE_MLOCK2] = {NULL, "DUNE_MLOCK2"},
1403         [DUNE_COPY_FILE_RANGE] = {NULL, "DUNE_COPY_FILE_RANGE"},
1404         [DUNE_PREADV2] = {NULL, "DUNE_PREADV2"},
1405         [DUNE_PWRITEV2] = {NULL, "DUNE_PWRITEV2"},
1406
1407 };
1408
1409 bool init_linuxemu(void)
1410 {
1411         fd_table_lock = uth_mutex_alloc();
1412         int i;
1413
1414         for (i = 0; i < DUNE_MAX_NUM_SYSCALLS ; ++i) {
1415                 if (dune_syscall_table[i].name == NULL)
1416                         dune_syscall_table[i].name = "nosyscall";
1417         }
1418
1419         if (dlopen("liblinuxemu_extend.so", RTLD_NOW) == NULL) {
1420                 fprintf(stderr, "Not using any syscall extensions\n Reason: %s\n",
1421                         dlerror());
1422                 return false;
1423         }
1424
1425         return true;
1426 }
1427
1428 void lemuprint(const uint32_t tid, uint64_t syscall_number,
1429                const bool isError, const char *fmt, ...)
1430 {
1431         va_list valist;
1432         const char *prefix = "[TID %d] %s: ";
1433         bool double_logging = false;
1434
1435         // Do not use global variable as a check to acquire lock.
1436         // make sure it is not changed during our acquire/release.
1437         int debug = lemu_debug;
1438
1439         // If we are not going to log anything anyway, just bail out.
1440         if (!(debug > 0 || isError))
1441                 return;
1442
1443         const char *syscall_name;
1444
1445         if (syscall_number >= DUNE_MAX_NUM_SYSCALLS)
1446                 panic("lemuprint: Illegal Syscall #%d!\n", syscall_number);
1447         else
1448                 syscall_name = dune_syscall_table[syscall_number].name;
1449
1450         va_start(valist, fmt);
1451
1452         uth_mutex_lock(lemu_logging_lock);
1453
1454         // Print to stderr if debug level is sufficient
1455         if (debug > 1) {
1456                 fprintf(stderr, prefix, tid, syscall_name);
1457                 vfprintf(stderr, fmt, valist);
1458                 // Checks if we will double log to stderr
1459                 if (lemu_global_logfile == stderr)
1460                         double_logging = true;
1461         }
1462
1463         // Log to the global logfile, if we defaulted the global logging to
1464         // stderr then we don't want to log 2 times to stderr.
1465         if (lemu_global_logfile != NULL && !double_logging) {
1466                 fprintf(lemu_global_logfile, prefix, tid, syscall_name);
1467                 vfprintf(lemu_global_logfile, fmt, valist);
1468         }
1469
1470         uth_mutex_unlock(lemu_logging_lock);
1471
1472         va_end(valist);
1473 }
1474
1475
1476 /* TODO: have an array which classifies syscall args
1477  * and "special" system calls (ones with weird return
1478  * values etc.). For some cases, we don't even do a system
1479  * call, and in many cases we have to rearrange arguments
1480  * since Linux and Akaros don't share signatures, so this
1481  * gets tricky. */
1482 bool
1483 linuxemu(struct guest_thread *gth, struct vm_trapframe *tf)
1484 {
1485         bool ret = false;
1486
1487         if (tf->tf_rax >= DUNE_MAX_NUM_SYSCALLS) {
1488                 fprintf(stderr, "System call %d is out of range\n", tf->tf_rax);
1489                 return false;
1490         }
1491
1492
1493         if (dune_syscall_table[tf->tf_rax].call == NULL) {
1494                 fprintf(stderr, "System call #%d (%s) is not implemented\n",
1495                         tf->tf_rax, dune_syscall_table[tf->tf_rax].name);
1496                 return false;
1497         }
1498
1499         lemuprint(tf->tf_guest_pcoreid, tf->tf_rax,
1500                   false, "vmcall(%d, %p, %p, %p, %p, %p, %p);\n", tf->tf_rax,
1501                   tf->tf_rdi, tf->tf_rsi, tf->tf_rdx, tf->tf_r10, tf->tf_r8,
1502                   tf->tf_r9);
1503
1504         tf->tf_rip += 3;
1505
1506         return (dune_syscall_table[tf->tf_rax].call)(tf);
1507 }