Fixed 32bit error in lseek
[akaros.git] / kern / src / syscall.c
index a5c0538..2673eb5 100644 (file)
 #include <manager.h>
 #include <ros/procinfo.h>
 
-/* Tracing Globals */
-int systrace_flags = 0;
-struct systrace_record *systrace_buffer = 0;
-uint32_t systrace_bufidx = 0;
-size_t systrace_bufsize = 0;
-spinlock_t systrace_lock = SPINLOCK_INITIALIZER_IRQSAVE;
+static int execargs_stringer(struct proc *p, char *d, size_t slen,
+                            char *path, size_t path_l,
+                            char *argenv, size_t argenv_l);
 
-static bool __trace_this_proc(struct proc *p)
-{
-       return (systrace_flags & SYSTRACE_ON) &&
-               ((systrace_flags & SYSTRACE_ALLPROC) || is_traced_proc(p));
-}
+/* Global, used by the kernel monitor for syscall debugging. */
+bool systrace_loud = FALSE;
 
-static size_t systrace_fill_pretty_buf(struct systrace_record *trace)
+/* Helper, given the trace record, pretty-print the trace's contents into the
+ * trace's pretty buf.  'entry' says whether we're an entry record or not
+ * (exit).  Returns the number of bytes put into the pretty_buf. */
+static size_t systrace_fill_pretty_buf(struct systrace_record *trace,
+                                       bool entry)
 {
        size_t len = 0;
-       struct timespec ts_start;
-       struct timespec ts_end;
-       char what = 'X';
-       tsc2timespec(trace->start_timestamp, &ts_start);
-       tsc2timespec(trace->end_timestamp, &ts_end);
-       if (trace->end_timestamp == 0)
-               what = 'E';
-
-       len = snprintf(trace->pretty_buf, SYSTR_PRETTY_BUF_SZ - len,
-                  "%c [%7d.%09d]-[%7d.%09d] Syscall %3d (%12s):(0x%llx, 0x%llx, "
-                  "0x%llx, 0x%llx, 0x%llx, 0x%llx) ret: 0x%llx proc: %d core: %d "
-                  "vcore: %d data: ",
-                  what,
-                  ts_start.tv_sec,
-                  ts_start.tv_nsec,
-                  ts_end.tv_sec,
-                  ts_end.tv_nsec,
-                  trace->syscallno,
-                  syscall_table[trace->syscallno].name,
-                  trace->arg0,
-                  trace->arg1,
-                  trace->arg2,
-                  trace->arg3,
-                  trace->arg4,
-                  trace->arg5,
-                  trace->retval,
-                  trace->pid,
-                  trace->coreid,
-                  trace->vcoreid);
-
-       len += printdump(trace->pretty_buf + len,
-                        trace->datalen,
+       struct timespec ts_start = tsc2timespec(trace->start_timestamp);
+       struct timespec ts_end = tsc2timespec(trace->end_timestamp);
+
+       /* Slightly different formats between entry and exit.  Entry has retval set
+        * to ---, and begins with E.  Exit begins with X. */
+       if (entry) {
+               len = snprintf(trace->pretty_buf, SYSTR_PRETTY_BUF_SZ - len,
+                     "E [%7d.%09d]-[%7d.%09d] Syscall %3d (%12s):(0x%llx, 0x%llx, "
+                     "0x%llx, 0x%llx, 0x%llx, 0x%llx) ret: --- proc: %d core: %d "
+                     "vcore: %d data: ",
+                              ts_start.tv_sec,
+                              ts_start.tv_nsec,
+                              ts_end.tv_sec,
+                              ts_end.tv_nsec,
+                              trace->syscallno,
+                              syscall_table[trace->syscallno].name,
+                              trace->arg0,
+                              trace->arg1,
+                              trace->arg2,
+                              trace->arg3,
+                              trace->arg4,
+                              trace->arg5,
+                              trace->pid,
+                              trace->coreid,
+                              trace->vcoreid);
+       } else {
+               len = snprintf(trace->pretty_buf, SYSTR_PRETTY_BUF_SZ - len,
+                     "X [%7d.%09d]-[%7d.%09d] Syscall %3d (%12s):(0x%llx, 0x%llx, "
+                     "0x%llx, 0x%llx, 0x%llx, 0x%llx) ret: 0x%llx proc: %d core: %d "
+                     "vcore: %d data: ",
+                              ts_start.tv_sec,
+                              ts_start.tv_nsec,
+                              ts_end.tv_sec,
+                              ts_end.tv_nsec,
+                              trace->syscallno,
+                              syscall_table[trace->syscallno].name,
+                              trace->arg0,
+                              trace->arg1,
+                              trace->arg2,
+                              trace->arg3,
+                              trace->arg4,
+                              trace->arg5,
+                              trace->retval,
+                              trace->pid,
+                              trace->coreid,
+                              trace->vcoreid);
+       }
+       len += printdump(trace->pretty_buf + len, trace->datalen,
                         SYSTR_PRETTY_BUF_SZ - len - 1,
                         trace->data);
        len += snprintf(trace->pretty_buf + len, SYSTR_PRETTY_BUF_SZ - len, "\n");
        return len;
 }
 
-/* On enter, we have !trace, a sysc, and retval is meaningless.  On exit, we had
- * trace, retval and !sysc */
-static struct systrace_record *sctrace(struct systrace_record *trace,
-                                       struct proc *p, struct syscall *sysc,
-                                       long retval)
+/* Helper: spits out our trace to the various sinks. */
+static void systrace_output(struct systrace_record *trace,
+                            struct strace *strace, bool entry)
 {
-       int n;
-       uintreg_t cp = 0;
-       int datalen = 0;
+       size_t pretty_len;
 
-       assert(p->strace);
+       pretty_len = systrace_fill_pretty_buf(trace, entry);
+       if (strace)
+               qiwrite(strace->q, trace->pretty_buf, pretty_len);
+       if (systrace_loud)
+               printk("%s", trace->pretty_buf);
+}
 
-       if (!trace) {
-               /* We're using qiwrite, which has no flow control.  We'll do it
-                * manually. */
+/* Starts a trace for p running sysc, attaching it to kthread.  Pairs with
+ * systrace_finish_trace(). */
+static void systrace_start_trace(struct kthread *kthread, struct syscall *sysc)
+{
+       struct proc *p = current;
+       struct systrace_record *trace;
+       uintreg_t data_arg;
+       size_t data_len = 0;
+
+       kthread->strace = 0;
+       if (!p->strace_on && !systrace_loud)
+               return;
+       trace = kmalloc(SYSTR_BUF_SZ, MEM_ATOMIC);
+       if (p->strace) {
+               /* We're using qiwrite below, which has no flow control.  We'll do it
+                * manually.  TODO: consider a block_alloc and qpass, though note that
+                * we actually write the same trace in twice (entry and exit).
+                * Alternatively, we can add another qio method that has flow control
+                * and non blocking. */
                if (qfull(p->strace->q)) {
                        atomic_inc(&p->strace->nr_drops);
-                       return NULL;
+                       kfree(trace);
+                       return;
                }
-               // TODO: could we allocb and then write that block?
-               // Still, if we're tracing, we take a hit, and this is so
-               // much more efficient than strace it's not clear we care.
-               trace = kmalloc(SYSTR_BUF_SZ, 0);
-
-               if (!trace) {
+               if (!trace)
                        atomic_inc(&p->strace->nr_drops);
-                       return NULL;
-               }
                /* Avoiding the atomic op.  We sacrifice accuracy for less overhead. */
                p->strace->appx_nr_sysc++;
-
-               int coreid, vcoreid;
-               struct proc *p = current;
-
-               coreid = core_id();
-               vcoreid = proc_get_vcoreid(p);
-
-               // TODO: functionalize this, if we decide this
-               // approach is OK.
-               trace->start_timestamp = read_tsc();
-               trace->end_timestamp = 0;
-               trace->syscallno = sysc->num;
-               trace->arg0 = sysc->arg0;
-               trace->arg1 = sysc->arg1;
-               trace->arg2 = sysc->arg2;
-               trace->arg3 = sysc->arg3;
-               trace->arg4 = sysc->arg4;
-               trace->arg5 = sysc->arg5;
-               trace->pid = p->pid;
-               trace->coreid = coreid;
-               trace->vcoreid = vcoreid;
-               trace->pretty_buf = (char*)trace + sizeof(struct systrace_record);
-               trace->datalen = 0;
-               trace->data[0] = 0;
-               switch (sysc->num) {
-               case SYS_write:
-                       cp = sysc->arg1;
-                       datalen = sysc->arg2;
-                       break;
-               case SYS_openat:
-                       cp = sysc->arg1;
-                       datalen = sysc->arg2;
-                       break;
-               }
-       } else {
-               trace->end_timestamp = read_tsc();
-               trace->retval = retval;
-               switch (trace->syscallno) {
-               case SYS_read:
-                       cp = trace->arg1;
-                       datalen = retval < 0 ? 0 : retval;
-                       break;
-               }
        }
-
-       trace->datalen = MIN(sizeof(trace->data), datalen);
-       memmove(trace->data, (void *)cp, trace->datalen);
-       n = systrace_fill_pretty_buf(trace);
-       qiwrite(p->strace->q, trace->pretty_buf, n);
-       return trace;
-}
-
-static void systrace_start_trace(struct kthread *kthread, struct syscall *sysc)
-{
-       struct systrace_record *trace;
-       int coreid, vcoreid;
-       struct proc *p = current;
-
-       if (p->strace_on)
-               kthread->strace = sctrace(NULL, p, sysc, 0);
-       else
-               kthread->strace = 0;
-
-       /* TODO: merge these two types of tracing, or just remove this old one */
-       if (!__trace_this_proc(p))
-               return;
-       assert(!kthread->trace);        /* catch memory leaks */
-       coreid = core_id();
-       vcoreid = proc_get_vcoreid(p);
-       if (systrace_flags & SYSTRACE_LOUD) {
-               printk("ENTER [%16llu] Syscall %3d (%12s):(0x%llx, 0x%llx, 0x%llx, "
-                      "0x%llx, 0x%llx, 0x%llx) proc: %d core: %d vcore: %d\n",
-                      read_tsc(),
-                      sysc->num, syscall_table[sysc->num].name,
-                          sysc->arg0, sysc->arg1, sysc->arg2, sysc->arg3, sysc->arg4,
-                          sysc->arg5, p->pid, coreid, vcoreid);
-       }
-       trace = kmalloc(SYSTR_BUF_SZ, 0);
        if (!trace)
                return;
-       kthread->trace = trace;
+       /* if you ever need to debug just one strace function, this is
+        * handy way to do it: just bail out if it's not the one you
+        * want.
+        * if (sysc->num != SYS_exec)
+        * return; */
        trace->start_timestamp = read_tsc();
+       trace->end_timestamp = 0;
        trace->syscallno = sysc->num;
        trace->arg0 = sysc->arg0;
        trace->arg1 = sysc->arg1;
@@ -210,42 +163,90 @@ static void systrace_start_trace(struct kthread *kthread, struct syscall *sysc)
        trace->arg3 = sysc->arg3;
        trace->arg4 = sysc->arg4;
        trace->arg5 = sysc->arg5;
+       trace->retval = 0;
        trace->pid = p->pid;
-       trace->coreid = coreid;
-       trace->vcoreid = vcoreid;
+       trace->coreid = core_id();
+       trace->vcoreid = proc_get_vcoreid(p);
        trace->pretty_buf = (char*)trace + sizeof(struct systrace_record);
        trace->datalen = 0;
        trace->data[0] = 0;
-}
 
+       switch (sysc->num) {
+       case SYS_write:
+               data_arg = sysc->arg1;
+               data_len = sysc->arg2;
+               break;
+       case SYS_openat:
+               data_arg = sysc->arg1;
+               data_len = sysc->arg2;
+               break;
+       case SYS_exec:
+               trace->datalen = execargs_stringer(current,
+                                                  (char *)trace->data,
+                                                  sizeof(trace->data),
+                                                  (char *)sysc->arg0,
+                                                  sysc->arg1,
+                                                  (char *)sysc->arg2,
+                                                  sysc->arg3);
+               break;
+       case SYS_proc_create:
+               trace->datalen = execargs_stringer(current,
+                                                  (char *)trace->data,
+                                                  sizeof(trace->data),
+                                                  (char *)sysc->arg0,
+                                                  sysc->arg1,
+                                                  (char *)sysc->arg2,
+                                                  sysc->arg3);
+               break;
+       }
+       if (data_len) {
+               trace->datalen = MIN(sizeof(trace->data), data_len);
+               copy_from_user(trace->data, (void*)data_arg, trace->datalen);
+       }
+
+       systrace_output(trace, p->strace, TRUE);
+
+       kthread->strace = trace;
+}
+
+/* Finishes the trace on kthread for p, with retval being the return from the
+ * syscall we're tracing.  Pairs with systrace_start_trace(). */
 static void systrace_finish_trace(struct kthread *kthread, long retval)
 {
-       struct systrace_record *trace = kthread->trace;
-       size_t pretty_len;
+       struct proc *p = current;
+       struct systrace_record *trace;
+       long data_arg;
+       size_t data_len = 0;
 
-       if (trace) {
-               trace->end_timestamp = read_tsc();
-               trace->retval = retval;
-               kthread->trace = 0;
-               pretty_len = systrace_fill_pretty_buf(trace);
-               kprof_tracedata_write(trace->pretty_buf, pretty_len);
-               if (systrace_flags & SYSTRACE_LOUD)
-                       printk("EXIT %s", trace->pretty_buf);
-               kfree(trace);
-       }
-       /* TODO: merge with or remove the old tracer */
-       if (kthread->strace) {
-               sctrace(kthread->strace, current, 0, retval);
-               kfree(kthread->strace);
-               kthread->strace = 0;
+       if (!kthread->strace)
+               return;
+       trace = kthread->strace;
+       trace->end_timestamp = read_tsc();
+       trace->retval = retval;
+
+       /* Only try to do the trace data if we didn't do it on entry */
+       if (!trace->datalen) {
+               switch (trace->syscallno) {
+               case SYS_read:
+                       data_arg = trace->arg1;
+                       data_len = retval < 0 ? 0 : retval;
+                       break;
+               }
+               trace->datalen = MIN(sizeof(trace->data), data_len);
+               if (trace->datalen)
+                       copy_from_user(trace->data, (void*)data_arg, trace->datalen);
        }
+
+       systrace_output(trace, p->strace, FALSE);
+       kfree(kthread->strace);
+       kthread->strace = 0;
 }
 
 #ifdef CONFIG_SYSCALL_STRING_SAVING
 
 static void alloc_sysc_str(struct kthread *kth)
 {
-       kth->name = kmalloc(SYSCALL_STRLEN, KMALLOC_WAIT);
+       kth->name = kmalloc(SYSCALL_STRLEN, MEM_WAIT);
        kth->name[0] = 0;
 }
 
@@ -286,7 +287,7 @@ static void finish_sysc(struct syscall *sysc, struct proc *p)
         * to not muck with the flags while we're signalling. */
        atomic_or(&sysc->flags, SC_K_LOCK | SC_DONE);
        __signal_syscall(sysc, p);
-       atomic_and(&sysc->flags, ~SC_K_LOCK); 
+       atomic_and(&sysc->flags, ~SC_K_LOCK);
 }
 
 /* Helper that "finishes" the current async syscall.  This should be used with
@@ -514,7 +515,7 @@ static int sys_nanosleep(struct proc *p,
         * years, which should be sufficiently long enough to ensure we don't
         * overflow). */
        if (waserror()) {
-               tsc2timespec(read_tsc() - tsc, &krem);
+               krem = tsc2timespec(read_tsc() - tsc);
                if (rem && memcpy_to_user(p, rem, &krem, sizeof(struct timespec)))
                        set_errno(EFAULT);
                poperror();
@@ -609,21 +610,6 @@ static int sys_cache_invalidate(void)
 
 /* sys_reboot(): called directly from dispatch table. */
 
-// Read a character from the system console.
-// Returns the character.
-/* TODO: remove me */
-static uint16_t sys_cgetc(struct proc *p)
-{
-       uint16_t c;
-
-       // The cons_get_any_char() primitive doesn't wait for a character,
-       // but the sys_cgetc() system call does.
-       while ((c = cons_get_any_char()) == 0)
-               cpu_relax();
-
-       return c;
-}
-
 /* Returns the id of the physical core this syscall is executed on. */
 static uint32_t sys_getpcoreid(void)
 {
@@ -678,42 +664,44 @@ static int sys_proc_create(struct proc *p, char *path, size_t path_l,
        /* TODO: 9ns support */
        program = do_file_open(t_path, O_READ, 0);
        if (!program)
-               goto error_user_memdup;
-
+               goto error_with_path;
+       if (!is_valid_elf(program)) {
+               set_errno(ENOEXEC);
+               goto error_with_file;
+       }
        /* Check the size of the argenv array, error out if too large. */
        if ((argenv_l < sizeof(struct argenv)) || (argenv_l > ARG_MAX)) {
                set_error(EINVAL, "The argenv array has an invalid size: %lu\n",
                                  argenv_l);
-               goto error_user_memdup;
+               goto error_with_file;
        }
        /* Copy the argenv array into a kernel buffer. Delay processing of the
         * array to load_elf(). */
        kargenv = user_memdup_errno(p, argenv, argenv_l);
        if (!kargenv) {
-               set_errstr("Failed to copy in the args");
-               goto error_user_memdup;
+               set_error(EINVAL, "Failed to copy in the args");
+               goto error_with_file;
        }
        /* Unpack the argenv array into more usable variables. Integrity checking
         * done along side this as well. */
        if (unpack_argenv(kargenv, argenv_l, &argc, &argv, &envc, &envp)) {
-               set_errstr("Failed to unpack the args");
-               goto error_unpack;
+               set_error(EINVAL, "Failed to unpack the args");
+               goto error_with_kargenv;
        }
-
        /* TODO: need to split the proc creation, since you must load after setting
         * args/env, since auxp gets set up there. */
        //new_p = proc_create(program, 0, 0);
        if (proc_alloc(&new_p, current, flags)) {
-               set_errstr("Failed to alloc new proc");
-               goto error_proc_alloc;
+               set_error(ENOMEM, "Failed to alloc new proc");
+               goto error_with_kargenv;
        }
        inherit_strace(p, new_p);
        /* close the CLOEXEC ones, even though this isn't really an exec */
        close_fdt(&new_p->open_files, TRUE);
        /* Load the elf. */
        if (load_elf(new_p, program, argc, argv, envc, envp)) {
-               set_errstr("Failed to load elf");
-               goto error_load_elf;
+               set_error(EINVAL, "Failed to load elf");
+               goto error_with_proc;
        }
        /* progname is argv0, which accounts for symlinks */
        proc_set_progname(new_p, argc ? argv[0] : NULL);
@@ -725,18 +713,17 @@ static int sys_proc_create(struct proc *p, char *path, size_t path_l,
        profiler_notify_new_process(new_p);
        proc_decref(new_p);     /* give up the reference created in proc_create() */
        return pid;
-error_load_elf:
-       set_errno(EINVAL);
+error_with_proc:
        /* proc_destroy will decref once, which is for the ref created in
         * proc_create().  We don't decref again (the usual "+1 for existing"),
         * since the scheduler, which usually handles that, hasn't heard about the
         * process (via __proc_ready()). */
        proc_destroy(new_p);
-error_proc_alloc:
-       kref_put(&program->f_kref);
-error_unpack:
+error_with_kargenv:
        user_memdup_free(p, kargenv);
-error_user_memdup:
+error_with_file:
+       kref_put(&program->f_kref);
+error_with_path:
        free_path(p, t_path);
        return -1;
 }
@@ -882,11 +869,65 @@ static ssize_t sys_fork(env_t* e)
        return ret;
 }
 
+/* string for sys_exec arguments. Assumes that d is pointing to zero'd
+ * storage or storage that does not require null termination or
+ * provides the null. */
+static int execargs_stringer(struct proc *p, char *d, size_t slen,
+                            char *path, size_t path_l,
+                            char *argenv, size_t argenv_l)
+{
+       int argc, envc, i;
+       char **argv, **envp;
+       struct argenv *kargenv;
+       int amt;
+       char *s = d;
+       char *e = d + slen;
+
+       if (path_l > slen)
+               path_l = slen;
+       if (memcpy_from_user(p, d, path, path_l)) {
+               s = seprintf(s, e, "Invalid exec path");
+               return s - d;
+       }
+       s += path_l;
+
+       /* yes, this code is cloned from below. I wrote a helper but
+        * Barret and I concluded after talking about it that the
+        * helper was not really helper-ful, as it has almost 10
+        * arguments. Please, don't suggest a cpp macro. Thank you. */
+       /* Check the size of the argenv array, error out if too large. */
+       if ((argenv_l < sizeof(struct argenv)) || (argenv_l > ARG_MAX)) {
+               s = seprintf(s, e, "The argenv array has an invalid size: %lu\n",
+                                 argenv_l);
+               return s - d;
+       }
+       /* Copy the argenv array into a kernel buffer. */
+       kargenv = user_memdup_errno(p, argenv, argenv_l);
+       if (!kargenv) {
+               s = seprintf(s, e, "Failed to copy in the args and environment");
+               return s - d;
+       }
+       /* Unpack the argenv array into more usable variables. Integrity checking
+        * done along side this as well. */
+       if (unpack_argenv(kargenv, argenv_l, &argc, &argv, &envc, &envp)) {
+               s = seprintf(s, e, "Failed to unpack the args");
+               user_memdup_free(p, kargenv);
+               return s - d;
+       }
+       s = seprintf(s, e, "[%d]{", argc);
+       for (i = 0; i < argc; i++)
+               s = seprintf(s, e, "%s, ", argv[i]);
+       s = seprintf(s, e, "}");
+
+       user_memdup_free(p, kargenv);
+       return s - d;
+}
+
 /* Load the binary "path" into the current process, and start executing it.
  * argv and envp are magically bundled in procinfo for now.  Keep in sync with
  * glibc's sysdeps/ros/execve.c.  Once past a certain point, this function won't
  * return.  It assumes (and checks) that it is current.  Don't give it an extra
- * refcnt'd *p (syscall won't do that). 
+ * refcnt'd *p (syscall won't do that).
  * Note: if someone batched syscalls with this call, they could clobber their
  * old memory (and will likely PF and die).  Don't do it... */
 static int sys_exec(struct proc *p, char *path, size_t path_l,
@@ -1435,7 +1476,7 @@ static int sys_vmm_poke_guest(struct proc *p, int guest_pcoreid)
 
 /* Pokes the ksched for the given resource for target_pid.  If the target pid
  * == 0, we just poke for the calling process.  The common case is poking for
- * self, so we avoid the lookup. 
+ * self, so we avoid the lookup.
  *
  * Not sure if you could harm someone via asking the kernel to look at them, so
  * we'll do a 'controls' check for now.  In the future, we might have something
@@ -1486,7 +1527,6 @@ static unsigned long sys_populate_va(struct proc *p, uintptr_t va,
 static intreg_t sys_read(struct proc *p, int fd, void *buf, size_t len)
 {
        struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
-       struct systrace_record *t = pcpui->cur_kthread->trace;
        ssize_t ret;
        struct file *file = get_file_from_fd(&p->open_files, fd);
        sysc_save_str("read on fd %d", fd);
@@ -1507,21 +1547,15 @@ static intreg_t sys_read(struct proc *p, int fd, void *buf, size_t len)
                /* plan9, should also handle errors (EBADF) */
                ret = sysread(fd, buf, len);
        }
-
-       if ((ret > 0) && t) {
-               t->datalen = MIN(sizeof(t->data), ret);
-               memcpy(t->data, buf, t->datalen);
-       }
-
        return ret;
 }
 
 static intreg_t sys_write(struct proc *p, int fd, const void *buf, size_t len)
 {
        struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
-       struct systrace_record *t = pcpui->cur_kthread->trace;
        ssize_t ret;
        struct file *file = get_file_from_fd(&p->open_files, fd);
+
        sysc_save_str("write on fd %d", fd);
        /* VFS */
        if (file) {
@@ -1537,13 +1571,7 @@ static intreg_t sys_write(struct proc *p, int fd, const void *buf, size_t len)
                /* plan9, should also handle errors */
                ret = syswrite(fd, (void*)buf, len);
        }
-
-       if (t) {
-               t->datalen = MIN(sizeof(t->data), len);
-               memcpy(t->data, buf, t->datalen);
-       }
        return ret;
-
 }
 
 /* Checks args/reads in the path, opens the file (relative to fromfd if the path
@@ -1567,10 +1595,24 @@ static intreg_t sys_openat(struct proc *p, int fromfd, const char *path,
        mode &= ~p->fs_env.umask;
        /* Only check the VFS for legacy opens.  It doesn't support openat.  Actual
         * openats won't check here, and file == 0. */
+#define REMOVE_BEFORE_FLIGHT 1
+#if REMOVE_BEFORE_FLIGHT
+       /*
+        * HACK. This is another stopgap until we move away from the
+        * vfs. People need to see /dev. This is written in such a way
+        * as to fail quickly, be easily removed, and still do what we
+        * want.
+        */
+       if ((fromfd == AT_FDCWD) && (path_l == 4) && (t_path[0] == '/')
+               && (t_path[1] == 'd') && (t_path[2] == 'e') && (t_path[3] == 'v')) {
+               set_errno(ENOENT);
+       } else {
+#endif
        if ((t_path[0] == '/') || (fromfd == AT_FDCWD))
                file = do_file_open(t_path, oflag, mode);
        else
                set_errno(ENOENT);      /* was not in the VFS. */
+       }
        if (file) {
                /* VFS lookup succeeded */
                /* stores the ref to file */
@@ -1827,10 +1869,8 @@ static intreg_t sys_llseek(struct proc *p, int fd, off_t offset_hi,
                ret = file->f_op->llseek(file, tempoff, &retoff, whence);
                kref_put(&file->f_kref);
        } else {
-               /* won't return here if error ... */
-               ret = sysseek(fd, tempoff, whence);
-               retoff = ret;
-               ret = 0;
+               retoff = sysseek(fd, tempoff, whence);
+               ret = (retoff < 0);
        }
 
        if (ret)
@@ -1906,7 +1946,7 @@ intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
        path_d = lookup_dentry(t_path, 0);
        if (!path_d){
                int n = 2048;
-               buf = kmalloc(n*2, KMALLOC_WAIT);
+               buf = kmalloc(n*2, MEM_WAIT);
                struct dir *d = (void *)&buf[n];
                /* try 9ns. */
                if (sysstat(t_path, buf, n) > 0) {
@@ -2026,44 +2066,11 @@ intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
        return retval;
 }
 
-intreg_t sys_pipe(struct proc *p, int *u_pipefd, int flags)
-{
-       int pipefd[2] = {0};
-       int retval = syspipe(pipefd);
-
-       if (retval)
-               return -1;
-       if (memcpy_to_user_errno(p, u_pipefd, pipefd, sizeof(pipefd))) {
-               sysclose(pipefd[0]);
-               sysclose(pipefd[1]);
-               set_errno(EFAULT);
-               return -1;
-       }
-       return 0;
-}
-
 intreg_t sys_gettimeofday(struct proc *p, int *buf)
 {
-       static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
-       static int t0 = 0;
-
-       spin_lock(&gtod_lock);
-       if(t0 == 0)
-
-#if (defined CONFIG_APPSERVER)
-       t0 = ufe(time,0,0,0,0);
-#else
-       // Nanwan's birthday, bitches!!
-       t0 = 1242129600;
-#endif
-       spin_unlock(&gtod_lock);
-
-       long long dt = read_tsc();
-       /* TODO: This probably wants its own function, using a struct timeval */
-       long kbuf[2] = {t0+dt/__proc_global_info.tsc_freq,
-           (dt%__proc_global_info.tsc_freq)*1000000/__proc_global_info.tsc_freq};
+       struct timeval tv = nsec2timeval(epoch_nsec());
 
-       return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
+       return memcpy_to_user_errno(p, buf, &tv, sizeof(tv));
 }
 
 intreg_t sys_tcgetattr(struct proc *p, int fd, void *termios_p)
@@ -2259,7 +2266,7 @@ static int vfs_wstat(struct file *file, uint8_t *stat_m, size_t stat_sz,
        int m_sz;
        int retval = 0;
 
-       dir = kzmalloc(sizeof(struct dir) + stat_sz, KMALLOC_WAIT);
+       dir = kzmalloc(sizeof(struct dir) + stat_sz, MEM_WAIT);
        m_sz = convM2D(stat_m, stat_sz, &dir[0], (char*)&dir[1]);
        if (m_sz != stat_sz) {
                set_error(EINVAL, ERROR_FIXME);
@@ -2340,7 +2347,6 @@ intreg_t sys_rename(struct proc *p, char *old_path, size_t old_path_l,
                     char *new_path, size_t new_path_l)
 {
        struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
-       struct systrace_record *t = pcpui->cur_kthread->trace;
        ERRSTACK(1);
        int mountpointlen = 0;
        char *from_path = copy_in_path(p, old_path, old_path_l);
@@ -2351,9 +2357,6 @@ intreg_t sys_rename(struct proc *p, char *old_path, size_t old_path_l,
        if ((!from_path) || (!to_path))
                return -1;
        printd("sys_rename :%s: to :%s: : ", from_path, to_path);
-       if (t) {
-               t->datalen = snprintf((char *)t->data, sizeof(t->data), "Rename :%s: to :%s:", from_path, to_path);
-       }
 
        /* we need a fid for the wstat. */
        /* TODO: maybe wrap the 9ns stuff better.  sysrename maybe? */
@@ -2398,7 +2401,7 @@ intreg_t sys_rename(struct proc *p, char *old_path, size_t old_path_l,
        printd("Newchan: %C\n", newchan);
        printd("Newchan: mchan %C\n", newchan->mchan);
 
-       if ((newchan->dev != oldchan->dev) || 
+       if ((newchan->dev != oldchan->dev) ||
                (newchan->type != oldchan->type)) {
                printd("Old chan and new chan do not match\n");
                set_errno(ENODEV);
@@ -2453,7 +2456,7 @@ intreg_t sys_rename(struct proc *p, char *old_path, size_t old_path_l,
        };
        printk("syswstat returns %d\n", retval);
 
-done: 
+done:
        free_path(p, from_path);
        free_path(p, to_path);
        cclose(oldchan);
@@ -2543,7 +2546,6 @@ const struct sys_table_entry syscall_table[] = {
        [SYS_cache_buster] = {(syscall_t)sys_cache_buster, "buster"},
        [SYS_cache_invalidate] = {(syscall_t)sys_cache_invalidate, "wbinv"},
        [SYS_reboot] = {(syscall_t)reboot, "reboot!"},
-       [SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
        [SYS_getpcoreid] = {(syscall_t)sys_getpcoreid, "getpcoreid"},
        [SYS_getvcoreid] = {(syscall_t)sys_getvcoreid, "getvcoreid"},
        [SYS_getpid] = {(syscall_t)sys_getpid, "getpid"},
@@ -2598,7 +2600,6 @@ const struct sys_table_entry syscall_table[] = {
        [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
        [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdir"},
        [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
-       [SYS_pipe] = {(syscall_t)sys_pipe, "pipe"},
        [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
        [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
        [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
@@ -2736,110 +2737,6 @@ void __signal_syscall(struct syscall *sysc, struct proc *p)
        }
 }
 
-/* Syscall tracing */
-static void __init_systrace(void)
-{
-       systrace_buffer = kmalloc(MAX_SYSTRACES*sizeof(struct systrace_record), 0);
-       if (!systrace_buffer)
-               panic("Unable to alloc a trace buffer\n");
-       systrace_bufidx = 0;
-       systrace_bufsize = MAX_SYSTRACES;
-       /* Note we never free the buffer - it's around forever.  Feel free to change
-        * this if you want to change the size or something dynamically. */
-}
-
-/* If you call this while it is running, it will change the mode */
-void systrace_start(bool silent)
-{
-       static bool init = FALSE;
-       spin_lock_irqsave(&systrace_lock);
-       if (!init) {
-               __init_systrace();
-               init = TRUE;
-       }
-       systrace_flags = silent ? SYSTRACE_ON : SYSTRACE_ON | SYSTRACE_LOUD;
-       spin_unlock_irqsave(&systrace_lock);
-}
-
-int systrace_reg(bool all, struct proc *p)
-{
-       spin_lock_irqsave(&systrace_lock);
-       if (all) {
-               printk("Tracing syscalls for all processes\n");
-               systrace_flags |= SYSTRACE_ALLPROC;
-       } else {
-               set_traced_proc(p, TRUE);
-
-               printk("Tracing syscalls for process %d\n", p->pid);
-       }
-       spin_unlock_irqsave(&systrace_lock);
-       return 0;
-}
-
-int systrace_trace_pid(struct proc *p)
-{
-       if (systrace_reg(false, p))
-               error(EFAIL, "no more processes");
-       systrace_start(true);
-       return 0;
-}
-
-void systrace_stop(void)
-{
-       spin_lock_irqsave(&systrace_lock);
-       systrace_flags = 0;
-       spin_unlock_irqsave(&systrace_lock);
-}
-
-/* If you registered a process specifically, then you need to dereg it
- * specifically.  Or just fully stop, which will do it for all. */
-int systrace_dereg(bool all, struct proc *p)
-{
-       spin_lock_irqsave(&systrace_lock);
-       if (all) {
-               printk("No longer tracing syscalls for all processes.\n");
-               systrace_flags &= ~SYSTRACE_ALLPROC;
-       } else {
-               set_traced_proc(p, FALSE);
-
-               printk("No longer tracing syscalls for process %d\n", p->pid);
-       }
-       spin_unlock_irqsave(&systrace_lock);
-       return 0;
-}
-
-/* Regardless of locking, someone could be writing into the buffer */
-void systrace_print(bool all, struct proc *p)
-{
-       spin_lock_irqsave(&systrace_lock);
-       /* if you want to be clever, you could make this start from the earliest
-        * timestamp and loop around.  Careful of concurrent writes. */
-       for (int i = 0; i < systrace_bufsize; i++)
-               if (systrace_buffer[i].start_timestamp)
-                       printk("[%16llu] Syscall %3d (%12s):(%p, %p, %p, %p, %p,"
-                              "%p) proc: %d core: %d vcore: %d\n",
-                              systrace_buffer[i].start_timestamp,
-                              systrace_buffer[i].syscallno,
-                              syscall_table[systrace_buffer[i].syscallno].name,
-                              systrace_buffer[i].arg0,
-                              systrace_buffer[i].arg1,
-                              systrace_buffer[i].arg2,
-                              systrace_buffer[i].arg3,
-                              systrace_buffer[i].arg4,
-                              systrace_buffer[i].arg5,
-                              systrace_buffer[i].pid,
-                              systrace_buffer[i].coreid,
-                              systrace_buffer[i].vcoreid);
-       spin_unlock_irqsave(&systrace_lock);
-}
-
-void systrace_clear_buffer(void)
-{
-       spin_lock_irqsave(&systrace_lock);
-       memset(systrace_buffer, 0, sizeof(struct systrace_record) * MAX_SYSTRACES);
-       spin_unlock_irqsave(&systrace_lock);
-}
-
 bool syscall_uses_fd(struct syscall *sysc, int fd)
 {
        switch (sysc->num) {