Remove the frontend appserver code
[akaros.git] / kern / src / syscall.c
index beb6e1d..d4ffa34 100644 (file)
@@ -23,7 +23,6 @@
 #include <kmalloc.h>
 #include <profiler.h>
 #include <stdio.h>
-#include <frontend.h>
 #include <hashtable.h>
 #include <bitmask.h>
 #include <vfs.h>
@@ -104,17 +103,105 @@ static size_t systrace_fill_pretty_buf(struct systrace_record *trace,
        return len;
 }
 
+/* If some syscalls block, then they can really hurt the user and the
+ * kernel.  For instance, if you blocked another call because the trace queue is
+ * full, the 2LS will want to yield the vcore, but then *that* call would block
+ * too.  Since that caller was in vcore context, the core will just spin
+ * forever.
+ *
+ * Even worse, some syscalls operate on the calling core or current context,
+ * thus accessing pcpui.  If we block, then that old context is gone.  Worse, we
+ * could migrate and then be operating on a different core.  Imagine
+ * SYS_halt_core.  Doh! */
+static bool sysc_can_block(unsigned int sysc_num)
+{
+       switch (sysc_num) {
+       case SYS_proc_yield:
+       case SYS_fork:
+       case SYS_exec:
+       case SYS_pop_ctx:
+       case SYS_getvcoreid:
+       case SYS_halt_core:
+       case SYS_vc_entry:
+       case SYS_change_vcore:
+       case SYS_change_to_m:
+               return FALSE;
+       }
+       return TRUE;
+}
+
 /* Helper: spits out our trace to the various sinks. */
 static void systrace_output(struct systrace_record *trace,
                             struct strace *strace, bool entry)
 {
+       ERRSTACK(1);
        size_t pretty_len;
 
+       /* qio ops can throw, especially the blocking qwrite.  I had it block on the
+        * outbound path of sys_proc_destroy().  The rendez immediately throws. */
+       if (waserror()) {
+               poperror();
+               return;
+       }
        pretty_len = systrace_fill_pretty_buf(trace, entry);
-       if (strace)
-               qiwrite(strace->q, trace->pretty_buf, pretty_len);
+       if (strace) {
+               /* At this point, we're going to emit the exit trace.  It's just a
+                * question of whether or not we block while doing it. */
+               if (strace->drop_overflow || !sysc_can_block(trace->syscallno))
+                       qiwrite(strace->q, trace->pretty_buf, pretty_len);
+               else
+                       qwrite(strace->q, trace->pretty_buf, pretty_len);
+       }
        if (systrace_loud)
                printk("%s", trace->pretty_buf);
+       poperror();
+}
+
+static bool should_strace(struct proc *p, struct syscall *sysc)
+{
+       unsigned int sysc_num;
+
+       if (systrace_loud)
+               return TRUE;
+       if (!p->strace || !p->strace->tracing)
+               return FALSE;
+       /* TOCTTOU concerns - sysc is __user. */
+       sysc_num = ACCESS_ONCE(sysc->num);
+       if (qfull(p->strace->q)) {
+               if (p->strace->drop_overflow || !sysc_can_block(sysc_num)) {
+                       atomic_inc(&p->strace->nr_drops);
+                       return FALSE;
+               }
+       }
+       if (sysc_num > MAX_SYSCALL_NR)
+               return FALSE;
+       return test_bit(sysc_num, p->strace->trace_set);
+}
+
+/* Helper, copies len bytes from u_data to the trace->data, if there's room. */
+static void copy_tracedata_from_user(struct systrace_record *trace,
+                                     long u_data, size_t len)
+{
+       size_t copy_amt;
+
+       copy_amt = MIN(sizeof(trace->data) - trace->datalen, len);
+       copy_from_user(trace->data + trace->datalen, (void*)u_data, copy_amt);
+       trace->datalen += copy_amt;
+}
+
+/* Helper, snprintfs to the trace, if there's room. */
+static void snprintf_to_trace(struct systrace_record *trace, const char *fmt,
+                              ...)
+{
+       va_list ap;
+       int rc;
+
+       va_start(ap, fmt);
+       rc = vsnprintf((char*)trace->data + trace->datalen,
+                      sizeof(trace->data) - trace->datalen, fmt, ap);
+       va_end(ap);
+       if (!snprintf_error(rc, sizeof(trace->data) - trace->datalen))
+               trace->datalen += rc;
 }
 
 /* Starts a trace for p running sysc, attaching it to kthread.  Pairs with
@@ -123,31 +210,24 @@ 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)
+       if (!should_strace(p, sysc))
                return;
-       trace = kmalloc(SYSTR_BUF_SZ, MEM_ATOMIC);
+       /* TODO: consider a block_alloc and qpass, though note that we actually
+        * write the same trace in twice (entry and exit). */
+       trace = kpages_alloc(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)) {
+               if (!trace) {
                        atomic_inc(&p->strace->nr_drops);
-                       kfree(trace);
                        return;
                }
-               if (!trace)
-                       atomic_inc(&p->strace->nr_drops);
                /* Avoiding the atomic op.  We sacrifice accuracy for less overhead. */
                p->strace->appx_nr_sysc++;
+       } else {
+               if (!trace)
+                       return;
        }
-       if (!trace)
-               return;
        /* 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.
@@ -172,12 +252,32 @@ static void systrace_start_trace(struct kthread *kthread, struct syscall *sysc)
 
        switch (sysc->num) {
        case SYS_write:
-               data_arg = sysc->arg1;
-               data_len = sysc->arg2;
+               copy_tracedata_from_user(trace, sysc->arg1, sysc->arg2);
                break;
        case SYS_openat:
-               data_arg = sysc->arg1;
-               data_len = sysc->arg2;
+       case SYS_chdir:
+       case SYS_rmdir:
+       case SYS_nmount:
+               copy_tracedata_from_user(trace, sysc->arg1, sysc->arg2);
+               break;
+       case SYS_stat:
+       case SYS_lstat:
+       case SYS_access:
+       case SYS_unlink:
+       case SYS_mkdir:
+       case SYS_wstat:
+               copy_tracedata_from_user(trace, sysc->arg0, sysc->arg1);
+               break;
+       case SYS_link:
+       case SYS_symlink:
+       case SYS_rename:
+       case SYS_nbind:
+               copy_tracedata_from_user(trace, sysc->arg0, sysc->arg1);
+               snprintf_to_trace(trace, " -> ");
+               copy_tracedata_from_user(trace, sysc->arg2, sysc->arg3);
+               break;
+       case SYS_nunmount:
+               copy_tracedata_from_user(trace, sysc->arg2, sysc->arg3);
                break;
        case SYS_exec:
                trace->datalen = execargs_stringer(current,
@@ -198,11 +298,6 @@ static void systrace_start_trace(struct kthread *kthread, struct syscall *sysc)
                                                   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;
@@ -214,8 +309,6 @@ static void systrace_finish_trace(struct kthread *kthread, long retval)
 {
        struct proc *p = current;
        struct systrace_record *trace;
-       long data_arg;
-       size_t data_len = 0;
 
        if (!kthread->strace)
                return;
@@ -227,17 +320,22 @@ static void systrace_finish_trace(struct kthread *kthread, long retval)
        if (!trace->datalen) {
                switch (trace->syscallno) {
                case SYS_read:
-                       data_arg = trace->arg1;
-                       data_len = retval < 0 ? 0 : retval;
+                       if (retval <= 0)
+                               break;
+                       copy_tracedata_from_user(trace, trace->arg1, retval);
+                       break;
+               case SYS_readlink:
+                       if (retval <= 0)
+                               break;
+                       copy_tracedata_from_user(trace, trace->arg0, trace->arg1);
+                       snprintf_to_trace(trace, " -> ");
+                       copy_tracedata_from_user(trace, trace->arg2, trace->arg3);
                        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);
+       kpages_free(kthread->strace, SYSTR_BUF_SZ);
        kthread->strace = 0;
 }
 
@@ -245,13 +343,16 @@ static void systrace_finish_trace(struct kthread *kthread, long retval)
 
 static void alloc_sysc_str(struct kthread *kth)
 {
-       kth->name = kmalloc(SYSCALL_STRLEN, MEM_WAIT);
+       kth->name = kmalloc(SYSCALL_STRLEN, MEM_ATOMIC);
+       if (!kth->name)
+               return;
        kth->name[0] = 0;
 }
 
 static void free_sysc_str(struct kthread *kth)
 {
        char *str = kth->name;
+
        kth->name = 0;
        kfree(str);
 }
@@ -259,7 +360,9 @@ static void free_sysc_str(struct kthread *kth)
 #define sysc_save_str(...)                                                     \
 {                                                                              \
        struct per_cpu_info *pcpui = &per_cpu_info[core_id()];                     \
-       snprintf(pcpui->cur_kthread->name, SYSCALL_STRLEN, __VA_ARGS__);           \
+                                                                               \
+       if (pcpui->cur_kthread->name)                                              \
+               snprintf(pcpui->cur_kthread->name, SYSCALL_STRLEN, __VA_ARGS__);       \
 }
 
 #else
@@ -552,13 +655,11 @@ static size_t sys_getvcoreid(struct proc *p)
 /* Helper for proc_create and fork */
 static void inherit_strace(struct proc *parent, struct proc *child)
 {
-       if (parent->strace && parent->strace_inherit) {
+       if (parent->strace && parent->strace->inherit) {
                /* Refcnt on both, put in the child's ->strace. */
                kref_get(&parent->strace->users, 1);
                kref_get(&parent->strace->procs, 1);
                child->strace = parent->strace;
-               child->strace_on = TRUE;
-               child->strace_inherit = TRUE;
        }
 }
 
@@ -717,6 +818,7 @@ static ssize_t sys_fork(env_t* e)
 {
        uintptr_t temp;
        int ret;
+       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
 
        // TODO: right now we only support fork for single-core processes
        if (e->state != PROC_RUNNING_S) {
@@ -736,6 +838,7 @@ static ssize_t sys_fork(env_t* e)
                set_errno(EINVAL);
                return -1;
        }
+       assert(pcpui->cur_proc == pcpui->owning_proc);
        copy_current_ctx_to(&env->scp_ctx);
 
        /* Make the new process have the same VMRs as the older.  This will copy the
@@ -870,6 +973,7 @@ static int sys_exec(struct proc *p, char *path, size_t path_l,
        }
        /* Preemptively copy out the cur_ctx, in case we fail later (easier on
         * cur_ctx if we do this now) */
+       assert(pcpui->cur_proc == pcpui->owning_proc);
        copy_current_ctx_to(&p->scp_ctx);
        /* Check the size of the argenv array, error out if too large. */
        if ((argenv_l < sizeof(struct argenv)) || (argenv_l > ARG_MAX)) {
@@ -1251,6 +1355,19 @@ static int sys_self_notify(struct proc *p, uint32_t vcoreid,
        return 0;
 }
 
+static int sys_send_event(struct proc *p, struct event_queue *ev_q,
+                          struct event_msg *u_msg, uint32_t vcoreid)
+{
+       struct event_msg local_msg = {0};
+
+       if (memcpy_from_user(p, &local_msg, u_msg, sizeof(struct event_msg))) {
+               set_errno(EINVAL);
+               return -1;
+       }
+       send_event(p, ev_q, &local_msg, vcoreid);
+       return 0;
+}
+
 /* Puts the calling core into vcore context, if it wasn't already, via a
  * self-IPI / active notification.  Barring any weird unmappings, we just send
  * ourselves a __notify. */
@@ -1372,21 +1489,23 @@ static int sys_pop_ctx(struct proc *p, struct user_context *ctx)
        return 0;
 }
 
-/* Initializes a process to run virtual machine contexts, returning the number
- * initialized, optionally setting errno */
-static int sys_vmm_setup(struct proc *p, unsigned int nr_guest_pcores,
-                         struct vmm_gpcore_init *gpcis, int flags)
+static int sys_vmm_add_gpcs(struct proc *p, unsigned int nr_more_gpcs,
+                            struct vmm_gpcore_init *gpcis)
 {
-       int ret;
        ERRSTACK(1);
+       struct vmm *vmm = &p->vmm;
 
+       qlock(&vmm->qlock);
        if (waserror()) {
+               qunlock(&vmm->qlock);
                poperror();
                return -1;
        }
-       ret = vmm_struct_init(p, nr_guest_pcores, gpcis, flags);
+       __vmm_struct_init(p);
+       __vmm_add_gpcs(p, nr_more_gpcs, gpcis);
+       qunlock(&vmm->qlock);
        poperror();
-       return ret;
+       return nr_more_gpcs;
 }
 
 static int sys_vmm_poke_guest(struct proc *p, int guest_pcoreid)
@@ -1394,6 +1513,55 @@ static int sys_vmm_poke_guest(struct proc *p, int guest_pcoreid)
        return vmm_poke_guest(p, guest_pcoreid);
 }
 
+static int sys_vmm_ctl(struct proc *p, int cmd, unsigned long arg1,
+                       unsigned long arg2, unsigned long arg3,
+                       unsigned long arg4)
+{
+       ERRSTACK(1);
+       int ret;
+       struct vmm *vmm = &p->vmm;
+
+       /* Protects against concurrent setters and for gets that are not atomic
+        * reads (say, multiple exec ctls). */
+       qlock(&vmm->qlock);
+       if (waserror()) {
+               qunlock(&vmm->qlock);
+               poperror();
+               return -1;
+       }
+       __vmm_struct_init(p);
+       switch (cmd) {
+       case VMM_CTL_GET_EXITS:
+               if (vmm->amd)
+                       error(ENOTSUP, "AMD VMMs unsupported");
+               ret = vmx_ctl_get_exits(&vmm->vmx);
+               break;
+       case VMM_CTL_SET_EXITS:
+               if (arg1 & ~VMM_CTL_ALL_EXITS)
+                       error(EINVAL, "Bad vmm_ctl_exits %x (%x)", arg1,
+                             VMM_CTL_ALL_EXITS);
+               if (vmm->amd)
+                       error(ENOTSUP, "AMD VMMs unsupported");
+               ret = vmx_ctl_set_exits(&vmm->vmx, arg1);
+               break;
+       case VMM_CTL_GET_FLAGS:
+               ret = vmm->flags;
+               break;
+       case VMM_CTL_SET_FLAGS:
+               if (arg1 & ~VMM_CTL_ALL_FLAGS)
+                       error(EINVAL, "Bad vmm_ctl flags.  Got 0x%lx, allowed 0x%lx\n",
+                             arg1, VMM_CTL_ALL_FLAGS);
+               vmm->flags = arg1;
+               ret = 0;
+               break;
+       default:
+               error(EINVAL, "Bad vmm_ctl cmd %d", cmd);
+       }
+       qunlock(&vmm->qlock);
+       poperror();
+       return ret;
+}
+
 /* 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.
@@ -1566,11 +1734,6 @@ static intreg_t sys_close(struct proc *p, int fd)
        return retval;
 }
 
-/* kept around til we remove the last ufe */
-#define ufe(which,a0,a1,a2,a3) \
-       frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
-                          (int)(a0),(int)(a1),(int)(a2),(int)(a3))
-
 static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
 {
        struct kstat *kbuf;
@@ -2449,7 +2612,7 @@ const struct sys_table_entry syscall_table[] = {
        [SYS_proc_create] = {(syscall_t)sys_proc_create, "proc_create"},
        [SYS_proc_run] = {(syscall_t)sys_proc_run, "proc_run"},
        [SYS_proc_destroy] = {(syscall_t)sys_proc_destroy, "proc_destroy"},
-       [SYS_yield] = {(syscall_t)sys_proc_yield, "proc_yield"},
+       [SYS_proc_yield] = {(syscall_t)sys_proc_yield, "proc_yield"},
        [SYS_change_vcore] = {(syscall_t)sys_change_vcore, "change_vcore"},
        [SYS_fork] = {(syscall_t)sys_fork, "fork"},
        [SYS_exec] = {(syscall_t)sys_exec, "exec"},
@@ -2462,14 +2625,16 @@ const struct sys_table_entry syscall_table[] = {
        [SYS_provision] = {(syscall_t)sys_provision, "provision"},
        [SYS_notify] = {(syscall_t)sys_notify, "notify"},
        [SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
+       [SYS_send_event] = {(syscall_t)sys_send_event, "send_event"},
        [SYS_vc_entry] = {(syscall_t)sys_vc_entry, "vc_entry"},
        [SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
 #ifdef CONFIG_ARSC_SERVER
        [SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
 #endif
        [SYS_change_to_m] = {(syscall_t)sys_change_to_m, "change_to_m"},
-       [SYS_vmm_setup] = {(syscall_t)sys_vmm_setup, "vmm_setup"},
+       [SYS_vmm_add_gpcs] = {(syscall_t)sys_vmm_add_gpcs, "vmm_add_gpcs"},
        [SYS_vmm_poke_guest] = {(syscall_t)sys_vmm_poke_guest, "vmm_poke_guest"},
+       [SYS_vmm_ctl] = {(syscall_t)sys_vmm_ctl, "vmm_ctl"},
        [SYS_poke_ksched] = {(syscall_t)sys_poke_ksched, "poke_ksched"},
        [SYS_abort_sysc] = {(syscall_t)sys_abort_sysc, "abort_sysc"},
        [SYS_abort_sysc_fd] = {(syscall_t)sys_abort_sysc_fd, "abort_sysc_fd"},
@@ -2576,6 +2741,7 @@ void run_local_syscall(struct syscall *sysc)
        }
        pcpui->cur_kthread->sysc = sysc;        /* let the core know which sysc it is */
        systrace_start_trace(pcpui->cur_kthread, sysc);
+       pcpui = &per_cpu_info[core_id()];       /* reload again */
        alloc_sysc_str(pcpui->cur_kthread);
        /* syscall() does not return for exec and yield, so put any cleanup in there
         * too. */
@@ -2585,6 +2751,7 @@ void run_local_syscall(struct syscall *sysc)
        pcpui = &per_cpu_info[core_id()];
        free_sysc_str(pcpui->cur_kthread);
        systrace_finish_trace(pcpui->cur_kthread, sysc->retval);
+       pcpui = &per_cpu_info[core_id()];       /* reload again */
        /* Some 9ns paths set errstr, but not errno.  glibc will ignore errstr.
         * this is somewhat hacky, since errno might get set unnecessarily */
        if ((current_errstr()[0] != 0) && (!sysc->err))