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