File opening revisited
[akaros.git] / kern / src / syscall.c
index dc68230..19cb653 100644 (file)
@@ -5,38 +5,56 @@
 #endif
 
 #include <ros/common.h>
+#include <ros/notification.h>
 #include <arch/types.h>
 #include <arch/arch.h>
 #include <arch/mmu.h>
 #include <arch/console.h>
 #include <ros/timer.h>
-#include <ros/error.h>
+#include <error.h>
 
+#include <elf.h>
 #include <string.h>
 #include <assert.h>
 #include <process.h>
 #include <schedule.h>
 #include <pmap.h>
+#include <umem.h>
 #include <mm.h>
 #include <trap.h>
 #include <syscall.h>
 #include <kmalloc.h>
 #include <stdio.h>
 #include <resource.h>
+#include <frontend.h>
 #include <colored_caches.h>
 #include <arch/bitmask.h>
 #include <kfs.h> // eventually replace this with vfs.h
 
-#ifdef __sparc_v8__
-#include <arch/frontend.h>
-#endif 
 
-#ifdef __NETWORK__
+#ifdef __CONFIG_NETWORKING__
 #include <arch/nic_common.h>
-extern char *CT(PACKET_HEADER_SIZE + len) (*packet_wrap)(const char *CT(len) data, size_t len);
 extern int (*send_frame)(const char *CT(len) data, size_t len);
+extern unsigned char device_mac[6];
 #endif
 
+/* Tracing Globals */
+int systrace_flags = 0;
+struct systrace_record *systrace_buffer = 0;
+unsigned int systrace_bufidx = 0;
+size_t systrace_bufsize = 0;
+struct proc *systrace_procs[MAX_NUM_TRACED] = {0};
+spinlock_t systrace_lock = SPINLOCK_INITIALIZER;
+
+/* Not enforcing the packing of systrace_procs yet, but don't rely on that */
+static bool proc_is_traced(struct proc *p)
+{
+       for (int i = 0; i < MAX_NUM_TRACED; i++)
+               if (systrace_procs[i] == p)
+                       return true;
+       return false;
+}
+
 /************** Utility Syscalls **************/
 
 static int sys_null(void)
@@ -161,18 +179,11 @@ static uint32_t sys_getcpuid(void)
        return core_id();
 }
 
-// TODO: Temporary hack until thread-local storage is implemented on i386
-static size_t sys_getvcoreid(env_t* e)
+// TODO: Temporary hack until thread-local storage is implemented on i386 and
+// this is removed from the user interface
+static size_t sys_getvcoreid(struct proc *p)
 {
-       if(e->state == PROC_RUNNING_S)
-               return 0;
-
-       size_t i;
-       for(i = 0; i < e->num_vcores; i++)
-               if(core_id() == e->vcoremap[i])
-                       return i;
-
-       panic("virtual core id not found in sys_getvcoreid()!");
+       return proc_get_vcoreid(p, core_id());
 }
 
 /************** Process management syscalls **************/
@@ -183,39 +194,48 @@ static pid_t sys_getpid(struct proc *p)
        return p->pid;
 }
 
-/*
- * Creates a process found at the user string 'path'.  Currently uses KFS.
- * Not runnable by default, so it needs it's status to be changed so that the
- * next call to schedule() will try to run it.
- * TODO: once we have a decent VFS, consider splitting this up
- * and once there's an mmap, can have most of this in process.c
- */
-static int sys_proc_create(struct proc *p, const char *DANGEROUS path)
+/* Creates a process from the file 'path'.  The process is not runnable by
+ * default, so it needs it's status to be changed so that the next call to
+ * schedule() will try to run it.  TODO: take args/envs from userspace. */
+static int sys_proc_create(struct proc *p, char *path, size_t path_l,
+                           struct procinfo *pi)
 {
-       #define MAX_PATH_LEN 256 // totally arbitrary
        int pid = 0;
-       char tpath[MAX_PATH_LEN];
-       /*
-        * There's a bunch of issues with reading in the path, which we'll
-        * need to sort properly in the VFS.  Main concerns are TOCTOU (copy-in),
-        * whether or not it's a big deal that the pointer could be into kernel
-        * space, and resolving both of these without knowing the length of the
-        * string. (TODO)
-        * Change this so that all syscalls with a pointer take a length.
-        *
-        * zra: I've added this user_mem_strlcpy, which I think eliminates the
-     * the TOCTOU issue. Adding a length arg to this call would allow a more
-        * efficient implementation, though, since only one call to user_mem_check
-        * would be required.
-        */
-       int ret = user_mem_strlcpy(p,tpath, path, MAX_PATH_LEN, PTE_USER_RO);
-       int kfs_inode = kfs_lookup_path(tpath);
-       if (kfs_inode < 0)
-               return -EINVAL;
-       struct proc *new_p = kfs_proc_create(kfs_inode);
+       char *t_path;
+       struct file *program;
+       struct proc *new_p;
+
+       /* Copy in the path.  Consider putting an upper bound. */
+       t_path = user_strdup_errno(p, path, path_l);
+       if (IS_ERR(t_path))
+               return -1;
+       program = do_file_open(t_path, 0, 0);
+       user_memdup_free(p, t_path);
+       if (!program)
+               return -1;                      /* presumably, errno is already set */
+       /* 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))
+               goto mid_error;
+       /* Set the argument stuff needed by glibc */
+       if (memcpy_from_user_errno(p, new_p->procinfo->argp, pi->argp,
+                                  sizeof(pi->argp)))
+               goto late_error;
+       if (memcpy_from_user_errno(p, new_p->procinfo->argbuf, pi->argbuf,
+                                  sizeof(pi->argbuf)))
+               goto late_error;
+       if (load_elf(new_p, program))
+               goto late_error;
+       kref_put(&program->f_kref);
        pid = new_p->pid;
-       proc_decref(new_p, 1); // let go of the reference created in proc_create()
+       kref_put(&new_p->kref); /* give up the reference created in proc_create() */
        return pid;
+late_error:
+       proc_destroy(new_p);
+mid_error:
+       kref_put(&program->f_kref);
+       return -1;
 }
 
 /* Makes process PID runnable.  Consider moving the functionality to process.c */
@@ -227,20 +247,20 @@ static error_t sys_proc_run(struct proc *p, unsigned pid)
        if (!target)
                return -EBADPROC;
        // note we can get interrupted here. it's not bad.
-       spin_lock_irqsave(&p->proc_lock);
+       spin_lock(&p->proc_lock);
        // make sure we have access and it's in the right state to be activated
        if (!proc_controls(p, target)) {
-               proc_decref(target, 1);
+               kref_put(&target->kref);
                retval = -EPERM;
        } else if (target->state != PROC_CREATED) {
-               proc_decref(target, 1);
+               kref_put(&target->kref);
                retval = -EINVAL;
        } else {
                __proc_set_state(target, PROC_RUNNABLE_S);
                schedule_proc(target);
        }
-       spin_unlock_irqsave(&p->proc_lock);
-       proc_decref(target, 1);
+       spin_unlock(&p->proc_lock);
+       kref_put(&target->kref);
        return retval;
 }
 
@@ -253,51 +273,31 @@ static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
        error_t r;
        struct proc *p_to_die = pid2proc(pid);
 
-       if (!p_to_die)
-               return -EBADPROC;
+       if (!p_to_die) {
+               set_errno(current_tf, ESRCH);
+               return -1;
+       }
        if (!proc_controls(p, p_to_die)) {
-               proc_decref(p_to_die, 1);
-               return -EPERM;
+               kref_put(&p_to_die->kref);
+               set_errno(current_tf, EPERM);
+               return -1;
        }
        if (p_to_die == p) {
                // syscall code and pid2proc both have edible references, only need 1.
                p->exitcode = exitcode;
-               proc_decref(p, 1);
+               kref_put(&p_to_die->kref);
                printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
        } else {
-               panic("Destroying other processes is not supported yet.");
-               //printk("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
+               printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
        }
        proc_destroy(p_to_die);
+       kref_put(&p_to_die->kref);
        return ESUCCESS;
 }
 
-static int sys_proc_yield(struct proc *p)
-{
-       proc_yield(p);
-       return 0;
-}
-
-static ssize_t sys_run_binary(env_t* e, void *DANGEROUS binary_buf, size_t len,
-                              void*DANGEROUS arg, size_t num_colors)
+static int sys_proc_yield(struct proc *p, bool being_nice)
 {
-       env_t* env = proc_create(NULL,0);
-       assert(env != NULL);
-
-       static_assert(PROCINFO_NUM_PAGES == 1);
-       assert(memcpy_from_user(e,env->env_procinfo->argv_buf,arg,PROCINFO_MAX_ARGV_SIZE) == ESUCCESS);
-       *(intptr_t*)env->env_procinfo->env_buf = 0;
-
-       env_load_icode(env,e,binary_buf,len);
-       __proc_set_state(env, PROC_RUNNABLE_S);
-       schedule_proc(env);
-       if(num_colors > 0) {
-               env->cache_colors_map = cache_colors_map_alloc();
-               for(int i=0; i<num_colors; i++)
-                       cache_color_alloc(llc_cache, env->cache_colors_map);
-       }
-       proc_decref(env, 1);
-       proc_yield(e);
+       proc_yield(p, being_nice);
        return 0;
 }
 
@@ -310,10 +310,10 @@ static ssize_t sys_fork(env_t* e)
                return -1;
        }
 
-       env_t* env = proc_create(NULL,0);
+       env_t* env;
+       assert(!proc_alloc(&env, current));
        assert(env != NULL);
 
-       env->heap_bottom = e->heap_bottom;
        env->heap_top = e->heap_top;
        env->ppid = e->pid;
        env->env_tf = *current_tf;
@@ -323,68 +323,115 @@ static ssize_t sys_fork(env_t* e)
                if(GET_BITMASK_BIT(e->cache_colors_map,i))
                        cache_color_alloc(llc_cache, env->cache_colors_map);
 
+       duplicate_vmrs(e, env);
+
        int copy_page(env_t* e, pte_t* pte, void* va, void* arg)
        {
                env_t* env = (env_t*)arg;
 
-               page_t* pp;
-               if(upage_alloc(env,&pp,0))
-                       return -1;
-               if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
+               if(PAGE_PRESENT(*pte))
                {
-                       page_decref(pp);
-                       return -1;
-               }
+                       page_t* pp;
+                       if(upage_alloc(env,&pp,0))
+                               return -1;
+                       if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
+                       {
+                               page_decref(pp);
+                               return -1;
+                       }
 
-               pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
+                       pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
+               } else {
+                       assert(PAGE_PAGED_OUT(*pte));
+                       /* TODO: (SWAP) will need to either make a copy or CoW/refcnt the
+                        * backend store.  For now, this PTE will be the same as the
+                        * original PTE */
+                       panic("Swapping not supported!");
+                       pte_t* newpte = pgdir_walk(env->env_pgdir,va,1);
+                       if(!newpte)
+                               return -1;
+                       *newpte = *pte;
+               }
                return 0;
        }
 
-       if(env_user_mem_walk(e,0,UTOP,&copy_page,env))
-       {
-               proc_decref(env,2);
+       // TODO: (PC) this won't work.  Needs revisiting.
+       // copy procdata and procinfo
+       memcpy(env->procdata,e->procdata,sizeof(struct procdata));
+       memcpy(env->procinfo,e->procinfo,sizeof(struct procinfo));
+       env->procinfo->pid = env->pid;
+       env->procinfo->ppid = env->ppid;
+
+       /* for now, just copy the contents of every present page in the entire
+        * address space. */
+       if (env_user_mem_walk(e, 0, UMAPTOP, &copy_page, env)) {
+               proc_destroy(env);      /* this is prob what you want, not decref by 2 */
                set_errno(current_tf,ENOMEM);
                return -1;
        }
+       
+       /* TODO: copy all open files, except O_CLOEXEC */
 
        __proc_set_state(env, PROC_RUNNABLE_S);
        schedule_proc(env);
 
        // don't decref the new process.
        // that will happen when the parent waits for it.
+       // TODO: if the parent doesn't wait, we need to change the child's parent
+       // when the parent dies, or at least decref it
 
        printd("[PID %d] fork PID %d\n",e->pid,env->pid);
 
        return env->pid;
 }
 
-static ssize_t sys_exec(env_t* e, void *DANGEROUS binary_buf, size_t len,
-                        procinfo_t*DANGEROUS procinfo)
+/* 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 */
+static int sys_exec(struct proc *p, char *path, size_t path_l,
+                    struct procinfo *pi)
 {
-       // TODO: right now we only support exec for single-core processes
-       if(e->state != PROC_RUNNING_S)
-               return -1;
+       int ret = -1;
+       char *t_path;
+       struct file *program;
 
-       if(memcpy_from_user(e,e->env_procinfo,procinfo,sizeof(*procinfo)))
+       /* We probably want it to never be allowed to exec if it ever was _M */
+       if(p->state != PROC_RUNNING_S)
                return -1;
-       proc_init_procinfo(e);
-
-       void* binary = kmalloc(len,0);
-       if(binary == NULL)
-               return -1;
-       if(memcpy_from_user(e,binary,binary_buf,len))
-       {
-               kfree(binary);
+       /* Copy in the path.  Consider putting an upper bound. */
+       t_path = user_strdup_errno(p, path, path_l);
+       if (IS_ERR(t_path))
                return -1;
+       program = do_file_open(t_path, 0, 0);
+       user_memdup_free(p, t_path);
+       if (!program)
+               return -1;                      /* presumably, errno is already set */
+       /* Set the argument stuff needed by glibc */
+       if (memcpy_from_user_errno(p, p->procinfo->argp, pi->argp,
+                                  sizeof(pi->argp)))
+               goto mid_error;
+       if (memcpy_from_user_errno(p, p->procinfo->argbuf, pi->argbuf,
+                                  sizeof(pi->argbuf)))
+               goto mid_error;
+       /* This is the point of no return for the process. */
+       /* TODO: issues with this: Need to also assert there are no outstanding
+        * users of the sysrings.  the ldt page will get freed shortly, so that's
+        * okay.  Potentially issues with the nm and vcpd if we were in _M before
+        * and someone is trying to notify. */
+       memset(p->procdata, 0, sizeof(procdata_t));
+       env_user_mem_free(p, 0, UMAPTOP);
+       if (load_elf(p, program)) {
+               kref_put(&program->f_kref);
+               proc_destroy(p);
+               smp_idle();             /* syscall can't return on failure now */
        }
-
-       env_segment_free(e,0,USTACKTOP);
-
-       proc_init_trapframe(current_tf,0);
-       env_load_icode(e,NULL,binary,len);
-
-       kfree(binary);
+       printd("[PID %d] exec %s\n", p->pid, file_name(program));
+       kref_put(&program->f_kref);
+       *current_tf = p->env_tf;
        return 0;
+mid_error:
+       kref_put(&program->f_kref);
+       return -1;
 }
 
 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
@@ -423,7 +470,9 @@ static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
                }
 
                // if the wait succeeded, decref twice
-               proc_decref(p,1 + (ret == 0));
+               if (ret == 0)
+                       kref_put(&p->kref);
+               kref_put(&p->kref);
                return ret;
        }
 
@@ -442,23 +491,44 @@ static void *sys_mmap(struct proc* p, uintreg_t a1, uintreg_t a2, uintreg_t a3,
        return mmap(p,a1,a2,a3,_a456[0],_a456[1],_a456[2]);
 }
 
+static intreg_t sys_mprotect(struct proc* p, void* addr, size_t len, int prot)
+{
+       return mprotect(p, (uintptr_t)addr, len, prot);
+}
+
+static intreg_t sys_munmap(struct proc* p, void* addr, size_t len)
+{
+       return munmap(p, (uintptr_t)addr, len);
+}
+
 static void* sys_brk(struct proc *p, void* addr) {
-       size_t range;
+       ssize_t range;
+
+       // TODO: remove sys_brk
+       printk("[kernel] don't use brk, unsupported and will be removed soon.\n");
 
-       if((addr < p->heap_bottom) || (addr >= (void*)USTACKBOT))
+       spin_lock(&p->proc_lock);
+
+       if((addr < p->procinfo->heap_bottom) || (addr >= (void*)BRK_END))
                goto out;
 
-       if (addr > p->heap_top) {
-               range = addr - p->heap_top;
-               env_segment_alloc(p, p->heap_top, range);
+       uintptr_t real_heap_top = ROUNDUP((uintptr_t)p->heap_top,PGSIZE);
+       uintptr_t real_new_heap_top = ROUNDUP((uintptr_t)addr,PGSIZE);
+       range = real_new_heap_top - real_heap_top;
+
+       if (range > 0) {
+               if(__do_mmap(p, real_heap_top, range, PROT_READ | PROT_WRITE,
+                            MAP_FIXED | MAP_ANONYMOUS, NULL, 0) == MAP_FAILED)
+                       goto out;
        }
-       else if (addr < p->heap_top) {
-               range = p->heap_top - addr;
-               env_segment_free(p, addr, range);
+       else if (range < 0) {
+               if(__do_munmap(p, real_new_heap_top, -range))
+                       goto out;
        }
        p->heap_top = addr;
 
 out:
+       spin_unlock(&p->proc_lock);
        return p->heap_top;
 }
 
@@ -467,6 +537,8 @@ static ssize_t sys_shared_page_alloc(env_t* p1,
                                      int p1_flags, int p2_flags
                                     )
 {
+       /* When we remove/change this, also get rid of page_insert_in_range() */
+       printk("[kernel] the current shared page alloc is deprecated.\n");
        //if (!VALID_USER_PERMS(p1_flags)) return -EPERM;
        //if (!VALID_USER_PERMS(p2_flags)) return -EPERM;
 
@@ -479,7 +551,7 @@ static ssize_t sys_shared_page_alloc(env_t* p1,
        page_t* page;
        error_t e = upage_alloc(p1, &page,1);
        if (e < 0) {
-               proc_decref(p2, 1);
+               kref_put(&p2->kref);
                return e;
        }
 
@@ -487,7 +559,7 @@ static ssize_t sys_shared_page_alloc(env_t* p1,
                        (void*SNT)UTEXT, (void*SNT)UTOP, p2_flags);
        if (p2_addr == NULL) {
                page_free(page);
-               proc_decref(p2, 1);
+               kref_put(&p2->kref);
                return -EFAIL;
        }
 
@@ -496,11 +568,11 @@ static ssize_t sys_shared_page_alloc(env_t* p1,
        if(p1_addr == NULL) {
                page_remove(p2->env_pgdir, p2_addr);
                page_free(page);
-               proc_decref(p2, 1);
+               kref_put(&p2->kref);
                return -EFAIL;
        }
        *addr = p1_addr;
-       proc_decref(p2, 1);
+       kref_put(&p2->kref);
        return ESUCCESS;
 }
 
@@ -510,10 +582,75 @@ static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
 }
 
 
-/************** Resource Request Syscalls **************/
-
 /* sys_resource_req(): called directly from dispatch table. */
 
+/* Will notify the target on the given vcore, if the caller controls the target.
+ * Will honor the target's wanted/vcoreid.  u_ne can be NULL. */
+static int sys_notify(struct proc *p, int target_pid, unsigned int notif,
+                      struct notif_event *u_ne)
+{
+       struct notif_event local_ne;
+       struct proc *target = pid2proc(target_pid);
+
+       if (!target) {
+               set_errno(current_tf, EBADPROC);
+               return -1;
+       }
+       if (!proc_controls(p, target)) {
+               kref_put(&target->kref);
+               set_errno(current_tf, EPERM);
+               return -1;
+       }
+       /* if the user provided a notif_event, copy it in and use that */
+       if (u_ne) {
+               if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
+                       kref_put(&target->kref);
+                       set_errno(current_tf, EINVAL);
+                       return -1;
+               }
+               proc_notify(target, local_ne.ne_type, &local_ne);
+       } else {
+               proc_notify(target, notif, 0);
+       }
+       kref_put(&target->kref);
+       return 0;
+}
+
+/* Will notify the calling process on the given vcore, independently of WANTED
+ * or advertised vcoreid.  If you change the parameters, change pop_ros_tf() */
+static int sys_self_notify(struct proc *p, uint32_t vcoreid, unsigned int notif,
+                           struct notif_event *u_ne)
+{
+       struct notif_event local_ne;
+
+       printd("[kernel] received self notify for vcoreid %d, notif %d, ne %08p\n",
+              vcoreid, notif, u_ne);
+       /* if the user provided a notif_event, copy it in and use that */
+       if (u_ne) {
+               if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
+                       set_errno(current_tf, EINVAL);
+                       return -1;
+               }
+               do_notify(p, vcoreid, local_ne.ne_type, &local_ne);
+       } else {
+               do_notify(p, vcoreid, notif, 0);
+       }
+       return 0;
+}
+
+/* This will set a local timer for usec, then shut down the core */
+static int sys_halt_core(struct proc *p, unsigned int usec)
+{
+       /* TODO: ought to check and see if a timer was already active, etc, esp so
+        * userspace can't turn off timers.  also note we will also call whatever
+        * timer_interrupt() will do, though all we care about is just
+        * self_ipi/interrupting. */
+       set_core_timer(usec);
+       cpu_halt();
+
+       return 0;
+}
+
 /************** Platform Specific Syscalls **************/
 
 //Read a buffer over the serial port
@@ -522,7 +659,7 @@ static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
        if (len == 0)
                return 0;
 
-       #ifdef SERIAL_IO
+       #ifdef __CONFIG_SERIAL_IO__
            char *COUNT(len) buf = user_mem_assert(e, _buf, len, PTE_USER_RO);
                size_t bytes_read = 0;
                int c;
@@ -541,7 +678,7 @@ static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
 {
        if (len == 0)
                return 0;
-       #ifdef SERIAL_IO
+       #ifdef __CONFIG_SERIAL_IO__
                char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_USER_RO);
                for(int i =0; i<len; i++)
                        serial_send_byte(buf[i]);
@@ -551,39 +688,37 @@ static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
        #endif
 }
 
-#ifdef __NETWORK__
+#ifdef __CONFIG_NETWORKING__
 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
-static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf, size_t len)
+static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
 {
-       extern int eth_up;
-
        if (eth_up) {
-               extern int packet_waiting;
-               extern int packet_buffer_size;
-               extern char*CT(packet_buffer_size) packet_buffer;
-               extern char*CT(MAX_FRAME_SIZE) packet_buffer_orig;
-               extern int packet_buffer_pos;
 
-               if (len == 0)
-                       return 0;
+               uint32_t len;
+               char *ptr;
 
-               char *CT(len) _buf = user_mem_assert(e, buf,len, PTE_U);
+               spin_lock(&packet_buffers_lock);
 
-               if (packet_waiting == 0)
+               if (num_packet_buffers == 0) {
+                       spin_unlock(&packet_buffers_lock);
                        return 0;
+               }
 
-               int read_len = ((packet_buffer_pos + len) > packet_buffer_size) ? packet_buffer_size - packet_buffer_pos : len;
+               ptr = packet_buffers[packet_buffers_head];
+               len = packet_buffers_sizes[packet_buffers_head];
 
-               memcpy(_buf, packet_buffer + packet_buffer_pos, read_len);
+               num_packet_buffers--;
+               packet_buffers_head = (packet_buffers_head + 1) % MAX_PACKET_BUFFERS;
 
-               packet_buffer_pos = packet_buffer_pos + read_len;
+               spin_unlock(&packet_buffers_lock);
 
-               if (packet_buffer_pos == packet_buffer_size) {
-                       kfree(packet_buffer_orig);
-                       packet_waiting = 0;
-               }
+               char* _buf = user_mem_assert(e, buf, len, PTE_U);
+
+               memcpy(_buf, ptr, len);
+
+               kfree(ptr);
 
-               return read_len;
+               return len;
        }
        else
                return -EINVAL;
@@ -592,21 +727,32 @@ static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf, size_t len)
 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
 static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
 {
-       extern int eth_up;
-
        if (eth_up) {
 
                if (len == 0)
                        return 0;
 
+               // HACK TO BYPASS HACK
+               int just_sent = send_frame(buf, len);
+
+               if (just_sent < 0) {
+                       printk("Packet send fail\n");
+                       return 0;
+               }
+
+               return just_sent;
+
+               // END OF RECURSIVE HACK
+/*
                char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_U);
                int total_sent = 0;
                int just_sent = 0;
                int cur_packet_len = 0;
                while (total_sent != len) {
-                       cur_packet_len = ((len - total_sent) > MAX_PACKET_DATA) ? MAX_PACKET_DATA : (len - total_sent);
-                       char* wrap_buffer = packet_wrap(_buf + total_sent, cur_packet_len);
-                       just_sent = send_frame(wrap_buffer, cur_packet_len + PACKET_HEADER_SIZE);
+                       cur_packet_len = ((len - total_sent) > MTU) ? MTU : (len - total_sent);
+                       char dest_mac[6] = APPSERVER_MAC_ADDRESS;
+                       char* wrap_buffer = eth_wrap(_buf + total_sent, cur_packet_len, device_mac, dest_mac, APPSERVER_PORT);
+                       just_sent = send_frame(wrap_buffer, cur_packet_len + sizeof(struct ETH_Header));
 
                        if (just_sent < 0)
                                return 0; // This should be an error code of its own
@@ -618,15 +764,308 @@ static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
                }
 
                return (ssize_t)len;
+*/
+       }
+       else
+               return -EINVAL;
+}
 
+static ssize_t sys_eth_get_mac_addr(env_t* e, char *DANGEROUS buf) 
+{
+       if (eth_up) {
+               for (int i = 0; i < 6; i++)
+                       buf[i] = device_mac[i];
+               return 0;
        }
        else
                return -EINVAL;
 }
 
+static int sys_eth_recv_check(env_t* e) 
+{
+       if (num_packet_buffers != 0) 
+               return 1;
+       else
+               return 0;
+}
+
 #endif // Network
 
-/* sys_frontend_syscall_from_user(): called directly from dispatch table. */
+// Syscalls below here are serviced by the appserver for now.
+#define ufe(which,a0,a1,a2,a3) \
+       frontend_syscall_errno(p,APPSERVER_SYSCALL_##which,\
+                          (int)(a0),(int)(a1),(int)(a2),(int)(a3))
+
+intreg_t sys_write(struct proc* p, int fd, const void* buf, int len)
+{
+       int ret = 0;
+       void* kbuf = user_memdup_errno(p,buf,len);
+       if(kbuf == NULL)
+               return -1;
+#ifndef __CONFIG_APPSERVER__
+       /* Catch a common usage of stderr */
+       if (fd == 2) {
+               ((char*)kbuf)[len-1] = 0;
+               printk("[stderr]: %s\n", kbuf);
+               ret = len;
+       } else { // but warn/panic otherwise in ufe()
+               ret = ufe(write, fd, PADDR(kbuf), len, 0);
+       }
+#else
+       ret = ufe(write, fd, PADDR(kbuf), len, 0);
+#endif
+       user_memdup_free(p,kbuf);
+       return ret;
+}
+
+intreg_t sys_read(struct proc* p, int fd, void* buf, int len)
+{
+       void* kbuf = kmalloc_errno(len);
+       if(kbuf == NULL)
+               return -1;
+       int ret = ufe(read,fd,PADDR(kbuf),len,0);
+       if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
+               ret = -1;
+       user_memdup_free(p,kbuf);
+       return ret;
+}
+
+intreg_t sys_pwrite(struct proc* p, int fd, const void* buf, int len, int offset)
+{
+       void* kbuf = user_memdup_errno(p,buf,len);
+       if(kbuf == NULL)
+               return -1;
+       int ret = ufe(pwrite,fd,PADDR(kbuf),len,offset);
+       user_memdup_free(p,kbuf);
+       return ret;
+}
+
+intreg_t sys_pread(struct proc* p, int fd, void* buf, int len, int offset)
+{
+       void* kbuf = kmalloc_errno(len);
+       if(kbuf == NULL)
+               return -1;
+       int ret = ufe(pread,fd,PADDR(kbuf),len,offset);
+       if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,len))
+               ret = -1;
+       user_memdup_free(p,kbuf);
+       return ret;
+}
+
+/* Checks args/reads in the path, opens the file, and inserts it into the
+ * process's open file list. 
+ *
+ * TODO: take the path length */
+intreg_t sys_open(struct proc *p, const char *path, int oflag, int mode)
+{
+       int fd = 0;
+       struct file *file;
+
+       char *t_path = user_strdup_errno(p, path, PGSIZE);
+       if (t_path == NULL)
+               return -1;
+       file = do_file_open(t_path, oflag, mode);
+       user_memdup_free(p, t_path);
+       if (!file)
+               return -1;
+       fd = insert_file(&p->open_files, file); /* stores the ref to file */
+       kref_put(&file->f_kref);
+       if (fd < 0) {
+               warn("File insertion failed");
+               return -1;
+       }
+       printd("File Open, res=%d\n", fd);
+       return fd;
+}
+
+intreg_t sys_close(struct proc *p, int fd)
+{
+       struct file *file = put_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(current_tf, EBADF);
+               return -1;
+       }
+       /* TEMP TEST */
+       if (kref_refcnt(&file->f_kref)) {
+               printk("sys_close: Detected positive refcnt %d for file %s\n",
+                      kref_refcnt(&file->f_kref), file_name(file));
+               panic("Idiot.");
+       }
+       return 0;
+}
+
+#define NEWLIB_STAT_SIZE 64
+intreg_t sys_fstat(struct proc* p, int fd, void* buf)
+{
+       int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
+       int ret = ufe(fstat,fd,PADDR(kbuf),0,0);
+       if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
+               ret = -1;
+       kfree(kbuf);
+       return ret;
+}
+
+intreg_t sys_stat(struct proc* p, const char* path, void* buf)
+{
+       char* fn = user_strdup_errno(p,path,PGSIZE);
+       if(fn == NULL)
+               return -1;
+
+       int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
+       int ret = ufe(stat,PADDR(fn),PADDR(kbuf),0,0);
+       if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
+               ret = -1;
+
+       user_memdup_free(p,fn);
+       kfree(kbuf);
+       return ret;
+}
+
+intreg_t sys_lstat(struct proc* p, const char* path, void* buf)
+{
+       char* fn = user_strdup_errno(p,path,PGSIZE);
+       if(fn == NULL)
+               return -1;
+
+       int *kbuf = kmalloc(NEWLIB_STAT_SIZE, 0);
+       int ret = ufe(lstat,PADDR(fn),PADDR(kbuf),0,0);
+       if(ret != -1 && memcpy_to_user_errno(p,buf,kbuf,NEWLIB_STAT_SIZE))
+               ret = -1;
+
+       user_memdup_free(p,fn);
+       kfree(kbuf);
+       return ret;
+}
+
+intreg_t sys_fcntl(struct proc* p, int fd, int cmd, int arg)
+{
+       return ufe(fcntl,fd,cmd,arg,0);
+}
+
+intreg_t sys_access(struct proc* p, const char* path, int type)
+{
+       char* fn = user_strdup_errno(p,path,PGSIZE);
+       if(fn == NULL)
+               return -1;
+       int ret = ufe(access,PADDR(fn),type,0,0);
+       user_memdup_free(p,fn);
+       return ret;
+}
+
+intreg_t sys_umask(struct proc* p, int mask)
+{
+       return ufe(umask,mask,0,0,0);
+}
+
+intreg_t sys_chmod(struct proc* p, const char* path, int mode)
+{
+       char* fn = user_strdup_errno(p,path,PGSIZE);
+       if(fn == NULL)
+               return -1;
+       int ret = ufe(chmod,PADDR(fn),mode,0,0);
+       user_memdup_free(p,fn);
+       return ret;
+}
+
+intreg_t sys_lseek(struct proc* p, int fd, int offset, int whence)
+{
+       return ufe(lseek,fd,offset,whence,0);
+}
+
+intreg_t sys_link(struct proc* p, const char* _old, const char* _new)
+{
+       char* oldpath = user_strdup_errno(p,_old,PGSIZE);
+       if(oldpath == NULL)
+               return -1;
+
+       char* newpath = user_strdup_errno(p,_new,PGSIZE);
+       if(newpath == NULL)
+       {
+               user_memdup_free(p,oldpath);
+               return -1;
+       }
+
+       int ret = ufe(link,PADDR(oldpath),PADDR(newpath),0,0);
+       user_memdup_free(p,oldpath);
+       user_memdup_free(p,newpath);
+       return ret;
+}
+
+intreg_t sys_unlink(struct proc* p, const char* path)
+{
+       char* fn = user_strdup_errno(p,path,PGSIZE);
+       if(fn == NULL)
+               return -1;
+       int ret = ufe(unlink,PADDR(fn),0,0,0);
+       user_memdup_free(p,fn);
+       return ret;
+}
+
+intreg_t sys_chdir(struct proc* p, const char* path)
+{
+       char* fn = user_strdup_errno(p,path,PGSIZE);
+       if(fn == NULL)
+               return -1;
+       int ret = ufe(chdir,PADDR(fn),0,0,0);
+       user_memdup_free(p,fn);
+       return ret;
+}
+
+intreg_t sys_getcwd(struct proc* p, char* pwd, int size)
+{
+       void* kbuf = kmalloc_errno(size);
+       if(kbuf == NULL)
+               return -1;
+       int ret = ufe(read,PADDR(kbuf),size,0,0);
+       if(ret != -1 && memcpy_to_user_errno(p,pwd,kbuf,strnlen(kbuf,size)))
+               ret = -1;
+       user_memdup_free(p,kbuf);
+       return ret;
+}
+
+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();
+       int kbuf[2] = {t0+dt/system_timing.tsc_freq,
+           (dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
+
+       return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
+}
+
+#define SIZEOF_STRUCT_TERMIOS 60
+intreg_t sys_tcgetattr(struct proc* p, int fd, void* termios_p)
+{
+       int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
+       int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
+       if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
+               ret = -1;
+       kfree(kbuf);
+       return ret;
+}
+
+intreg_t sys_tcsetattr(struct proc* p, int fd, int optional_actions, const void* termios_p)
+{
+       void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
+       if(kbuf == NULL)
+               return -1;
+       int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
+       user_memdup_free(p,kbuf);
+       return ret;
+}
 
 /************** Syscall Invokation **************/
 
@@ -642,6 +1081,10 @@ static ssize_t sys_eth_write(env_t* e, const char *DANGEROUS buf, size_t len)
 intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
                  uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
 {
+       // Initialize the return value and error code returned to 0
+       proc_set_syscall_retval(current_tf, 0);
+       set_errno(current_tf,0);
+
        typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
                                      uintreg_t,uintreg_t,uintreg_t);
 
@@ -659,7 +1102,6 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
                [SYS_proc_run] = (syscall_t)sys_proc_run,
                [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
                [SYS_yield] = (syscall_t)sys_proc_yield,
-               [SYS_run_binary] = (syscall_t)sys_run_binary,
                [SYS_fork] = (syscall_t)sys_fork,
                [SYS_exec] = (syscall_t)sys_exec,
                [SYS_trywait] = (syscall_t)sys_trywait,
@@ -670,16 +1112,20 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
                [SYS_shared_page_alloc] = (syscall_t)sys_shared_page_alloc,
                [SYS_shared_page_free] = (syscall_t)sys_shared_page_free,
                [SYS_resource_req] = (syscall_t)resource_req,
-       #ifdef __i386__
+               [SYS_notify] = (syscall_t)sys_notify,
+               [SYS_self_notify] = (syscall_t)sys_self_notify,
+               [SYS_halt_core] = (syscall_t)sys_halt_core,
+       #ifdef __CONFIG_SERIAL_IO__
                [SYS_serial_read] = (syscall_t)sys_serial_read,
                [SYS_serial_write] = (syscall_t)sys_serial_write,
        #endif
-       #ifdef __NETWORK__
+       #ifdef __CONFIG_NETWORKING__
                [SYS_eth_read] = (syscall_t)sys_eth_read,
                [SYS_eth_write] = (syscall_t)sys_eth_write,
+               [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
+               [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
        #endif
-       #ifdef __sparc_v8__
-               [SYS_frontend] = (syscall_t)frontend_syscall_from_user,
+               // Syscalls serviced by the appserver for now.
                [SYS_read] = (syscall_t)sys_read,
                [SYS_write] = (syscall_t)sys_write,
                [SYS_open] = (syscall_t)sys_open,
@@ -697,11 +1143,36 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
                [SYS_chdir] = (syscall_t)sys_chdir,
                [SYS_getcwd] = (syscall_t)sys_getcwd,
                [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
-       #endif
+               [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
+               [SYS_tcsetattr] = (syscall_t)sys_tcsetattr
        };
 
        const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
 
+       uint32_t coreid, vcoreid;
+       if (systrace_flags & SYSTRACE_ON) {
+               if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
+                       coreid = core_id();
+                       vcoreid = proc_get_vcoreid(p, core_id());
+                       if (systrace_flags & SYSTRACE_LOUD) {
+                               printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
+                                      read_tsc(), syscallno, p->pid, coreid, vcoreid);
+                       } else {
+                               struct systrace_record *trace;
+                               unsigned int idx, new_idx;
+                               do {
+                                       idx = systrace_bufidx;
+                                       new_idx = (idx + 1) % systrace_bufsize;
+                               } while (!atomic_comp_swap(&systrace_bufidx, idx, new_idx));
+                               trace = &systrace_buffer[idx];
+                               trace->timestamp = read_tsc();
+                               trace->syscallno = syscallno;
+                               trace->pid = p->pid;
+                               trace->coreid = coreid;
+                               trace->vcoreid = vcoreid;
+                       }
+               }
+       }
        //printk("Incoming syscall on core: %d number: %d\n    a1: %x\n   "
        //       " a2: %x\n    a3: %x\n    a4: %x\n    a5: %x\n", core_id(),
        //       syscallno, a1, a2, a3, a4, a5);
@@ -730,7 +1201,7 @@ intreg_t process_generic_syscalls(struct proc *p, size_t max)
         * the *p). */
        // TODO: ought to be unnecessary, if you called this right, kept here for
        // now in case anyone actually uses the ARSCs.
-       proc_incref(p, 1);
+       kref_get(&p->kref, 1);
 
        // max is the most we'll process.  max = 0 means do as many as possible
        while (RING_HAS_UNCONSUMED_REQUESTS(sysbr) && ((!max)||(count < max)) ) {
@@ -760,6 +1231,106 @@ intreg_t process_generic_syscalls(struct proc *p, size_t max)
        }
        // load sane page tables (and don't rely on decref to do it for you).
        lcr3(boot_cr3);
-       proc_decref(p, 1);
+       kref_put(&p->kref);
        return (intreg_t)count;
 }
+
+/* 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)
+{
+       int retval = 0;
+       spin_lock_irqsave(&systrace_lock);
+       if (all) {
+               printk("Tracing syscalls for all processes\n");
+               systrace_flags |= SYSTRACE_ALLPROC;
+               retval = 0;
+       } else {
+               for (int i = 0; i < MAX_NUM_TRACED; i++) {
+                       if (!systrace_procs[i]) {
+                               printk("Tracing syscalls for process %d\n", p->pid);
+                               systrace_procs[i] = p;
+                               retval = 0;
+                               break;
+                       }
+               }
+       }
+       spin_unlock_irqsave(&systrace_lock);
+       return retval;
+}
+
+void systrace_stop(void)
+{
+       spin_lock_irqsave(&systrace_lock);
+       systrace_flags = 0;
+       for (int i = 0; i < MAX_NUM_TRACED; i++)
+               systrace_procs[i] = 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 {
+               for (int i = 0; i < MAX_NUM_TRACED; i++) {
+                       if (systrace_procs[i] == p) {
+                               systrace_procs[i] = 0;
+                               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].timestamp)
+                       printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
+                              systrace_buffer[i].timestamp,
+                              systrace_buffer[i].syscallno,
+                              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_NUM_TRACED);
+       spin_unlock_irqsave(&systrace_lock);
+}