readdir() and readdir_r() (XCC)
[akaros.git] / kern / src / syscall.c
index 0749a54..0eadc4f 100644 (file)
@@ -19,6 +19,7 @@
 #include <process.h>
 #include <schedule.h>
 #include <pmap.h>
+#include <umem.h>
 #include <mm.h>
 #include <trap.h>
 #include <syscall.h>
 #include <resource.h>
 #include <frontend.h>
 #include <colored_caches.h>
+#include <hashtable.h>
 #include <arch/bitmask.h>
 #include <kfs.h> // eventually replace this with vfs.h
+#include <smp.h>
+#include <arsc_server.h>
 
 
 #ifdef __CONFIG_NETWORKING__
@@ -144,23 +148,22 @@ static int sys_cache_invalidate(void)
 
 /* sys_reboot(): called directly from dispatch table. */
 
-// Print a string to the system console.
-// The string is exactly 'len' characters long.
-// Destroys the environment on memory errors.
-static ssize_t sys_cputs(env_t* e, const char *DANGEROUS s, size_t len)
+/* Print a string to the system console. */
+static ssize_t sys_cputs(struct proc *p, const char *DANGEROUS string,
+                         size_t strlen)
 {
-       // Check that the user has permission to read memory [s, s+len).
-       // Destroy the environment if not.
-       char *COUNT(len) _s = user_mem_assert(e, s, len, PTE_USER_RO);
-
-       // Print the string supplied by the user.
-       printk("%.*s", len, _s);
-       return (ssize_t)len;
+       char *t_string;
+       t_string = user_strdup_errno(p, string, strlen);
+       if (!t_string)
+               return -1;
+       printk("%.*s", strlen, t_string);
+       user_memdup_free(p, t_string);
+       return (ssize_t)strlen;
 }
 
 // Read a character from the system console.
 // Returns the character.
-static uint16_t sys_cgetc(env_t* e)
+static uint16_t sys_cgetc(struct proc *p)
 {
        uint16_t c;
 
@@ -193,38 +196,49 @@ 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)
 {
        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 on path_l. */
+       t_path = user_strdup_errno(p, path, path_l);
+       if (!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);
+       __proc_ready(new_p);
        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 */
@@ -239,17 +253,17 @@ static error_t sys_proc_run(struct proc *p, unsigned pid)
        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(&p->proc_lock);
-       proc_decref(target, 1);
+       kref_put(&target->kref);
        return retval;
 }
 
@@ -263,28 +277,24 @@ static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
        struct proc *p_to_die = pid2proc(pid);
 
        if (!p_to_die) {
-               set_errno(current_tf, ESRCH);
+               set_errno(ESRCH);
                return -1;
        }
-#ifndef __CONFIG_EXPER_TRADPROC__
        if (!proc_controls(p, p_to_die)) {
-               proc_decref(p_to_die, 1);
-               set_errno(current_tf, EPERM);
+               kref_put(&p_to_die->kref);
+               set_errno(EPERM);
                return -1;
        }
        if (p_to_die == p) {
-#else
-       if ((p_to_die == p) || (p_to_die == p->true_proc)) {
-#endif /* __CONFIG_EXPER_TRADPROC__ */
                // 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 {
                printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
        }
        proc_destroy(p_to_die);
-       proc_decref(p_to_die, 1);
+       kref_put(&p_to_die->kref);
        return ESUCCESS;
 }
 
@@ -294,41 +304,20 @@ static int sys_proc_yield(struct proc *p, bool being_nice)
        return 0;
 }
 
-static ssize_t sys_run_binary(env_t* e, void *DANGEROUS binary_buf, size_t len,
-                              procinfo_t*DANGEROUS procinfo, size_t num_colors)
-{
-       env_t* env = proc_create(NULL,0);
-       assert(env != NULL);
-
-       // let me know if you use this.  we need to sort process creation better.
-       printk("sys_run_binary() is deprecated.  Use at your own risk.");
-       if(memcpy_from_user(e,e->procinfo,procinfo,sizeof(*procinfo)))
-               return -1;
-       proc_init_procinfo(e);
-
-       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, 0);
-       return 0;
-}
-
 static ssize_t sys_fork(env_t* e)
 {
        // TODO: right now we only support fork for single-core processes
-       if(e->state != PROC_RUNNING_S)
-       {
-               set_errno(current_tf,EINVAL);
+       if (e->state != PROC_RUNNING_S) {
+               set_errno(EINVAL);
                return -1;
        }
-
-       env_t* env = proc_create(NULL,0);
+       /* Can't really fork if we don't have a current_tf to fork */
+       if (!current_tf) {
+               set_errno(EINVAL);
+               return -1;
+       }
+       env_t* env;
+       assert(!proc_alloc(&env, current));
        assert(env != NULL);
 
        env->heap_top = e->heap_top;
@@ -340,6 +329,8 @@ 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;
@@ -356,22 +347,17 @@ static ssize_t sys_fork(env_t* e)
                        }
 
                        pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
-               }
-               else // PAGE_PAGED_OUT(*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;
-
-                       struct file* file = PTE2PFAULT_INFO(*pte)->file;
-                       pfault_info_t* newpfi = pfault_info_alloc(file);
-                       if(!newpfi)
-                               return -1;
-
-                       *newpfi = *PTE2PFAULT_INFO(*pte);
-                       *newpte = PFAULT_INFO2PTE(newpfi);
+                       *newpte = *pte;
                }
-
                return 0;
        }
 
@@ -382,65 +368,84 @@ static ssize_t sys_fork(env_t* e)
        env->procinfo->pid = env->pid;
        env->procinfo->ppid = env->ppid;
 
-       // copy all memory below procdata
-       if(env_user_mem_walk(e,0,UDATA,&copy_page,env))
-       {
-               proc_decref(env,2);
-               set_errno(current_tf,ENOMEM);
+       /* 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(ENOMEM);
                return -1;
        }
-
+       clone_files(&e->open_files, &env->open_files);
+       __proc_ready(env);
        __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;
 }
 
-intreg_t sys_exec(struct proc* p, int fd, procinfo_t* pi)
+/* 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)
 {
-       if(p->state != PROC_RUNNING_S)
-               return -1;
-
        int ret = -1;
-       struct file* f = file_open_from_fd(p,fd);
-       if(f == NULL) {
-               set_errno(current_tf, EBADF);
-               goto out;
-       }
+       char *t_path;
+       struct file *program;
 
-       // Set the argument stuff needed by glibc
-       if(memcpy_from_user(p,p->procinfo->argp,pi->argp,sizeof(pi->argp))) {
-               proc_destroy(p);
-               goto out;
+       /* We probably want it to never be allowed to exec if it ever was _M */
+       if (p->state != PROC_RUNNING_S) {
+               set_errno(EINVAL);
+               return -1;
        }
-       if(memcpy_from_user(p,p->procinfo->argbuf,pi->argbuf,sizeof(pi->argbuf))) {
-               proc_destroy(p);
-               goto out;
+       /* Can't really exec if we don't have a current_tf to reset */
+       if (!current_tf) {
+               set_errno(EINVAL);
+               return -1;
        }
-
-       // TODO: don't do this either (PC)
+       /* Copy in the path.  Consider putting an upper bound on path_l. */
+       t_path = user_strdup_errno(p, path, path_l);
+       if (!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,USTACKTOP);
-
-       if(load_elf(p,f))
-       {
+       destroy_vmrs(p);
+       close_all_files(&p->open_files, TRUE);
+       env_user_mem_free(p, 0, UMAPTOP);
+       if (load_elf(p, program)) {
+               kref_put(&program->f_kref);
                proc_destroy(p);
-               goto out;
+               smp_idle();             /* syscall can't return on failure now */
        }
-       file_decref(f);
+       printd("[PID %d] exec %s\n", p->pid, file_name(program));
+       kref_put(&program->f_kref);
        *current_tf = p->env_tf;
-       ret = 0;
-
-       printd("[PID %d] exec fd %d\n",p->pid,fd);
-
-out:
-       return ret;
+       return 0;
+mid_error:
+       kref_put(&program->f_kref);
+       return -1;
 }
 
 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
@@ -468,77 +473,46 @@ static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
                        }
                        else // not dead yet
                        {
-                               set_errno(current_tf,0);
+                               set_errno(ESUCCESS);
                                ret = -1;
                        }
                }
                else // not a child of the calling process
                {
-                       set_errno(current_tf,1);
+                       set_errno(EPERM);
                        ret = -1;
                }
 
                // 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;
        }
 
-       set_errno(current_tf,1);
+       set_errno(EPERM);
        return -1;
 }
 
 /************** Memory Management Syscalls **************/
 
-static void *sys_mmap(struct procp, uintreg_t a1, uintreg_t a2, uintreg_t a3,
-                      uintreg_ta456)
+static void *sys_mmap(struct proc *p, uintreg_t a1, uintreg_t a2, uintreg_t a3,
+                      uintreg_t *a456)
 {
        uintreg_t _a456[3];
-       if(memcpy_from_user(p,_a456,a456,3*sizeof(uintreg_t)))
-               sys_proc_destroy(p,p->pid,-1);
-       return mmap(p,a1,a2,a3,_a456[0],_a456[1],_a456[2]);
+       if (memcpy_from_user(p, _a456, a456, 3 * sizeof(uintreg_t)))
+               sys_proc_destroy(p, p->pid, -1);
+       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)
+static intreg_t sys_mprotect(struct proc *p, void *addr, size_t len, int prot)
 {
-       return mprotect(p, addr, len, prot);
+       return mprotect(p, (uintptr_t)addr, len, prot);
 }
 
-static intreg_t sys_munmap(struct proc* p, void* addr, size_t len)
+static intreg_t sys_munmap(struct proc *p, void *addr, size_t len)
 {
-       return munmap(p, addr, len);
-}
-
-static void* sys_brk(struct proc *p, void* addr) {
-       ssize_t range;
-
-#ifdef __CONFIG_EXPER_TRADPROC__
-       printk("[kernel] don't use brk, unsupported.\n");
-       return (void*)-1;
-#endif /* __CONFIG_EXPER_TRADPROC__ */
-
-       spin_lock(&p->proc_lock);
-
-       if((addr < p->procinfo->heap_bottom) || (addr >= (void*)BRK_END))
-               goto out;
-
-       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 (range < 0) {
-               if(__munmap(p, (void*)real_new_heap_top, -range))
-                       goto out;
-       }
-       p->heap_top = addr;
-
-out:
-       spin_unlock(&p->proc_lock);
-       return p->heap_top;
+       return munmap(p, (uintptr_t)addr, len);
 }
 
 static ssize_t sys_shared_page_alloc(env_t* p1,
@@ -546,6 +520,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;
 
@@ -558,7 +534,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;
        }
 
@@ -566,7 +542,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;
        }
 
@@ -575,11 +551,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;
 }
 
@@ -600,26 +576,26 @@ static int sys_notify(struct proc *p, int target_pid, unsigned int notif,
        struct proc *target = pid2proc(target_pid);
 
        if (!target) {
-               set_errno(current_tf, EBADPROC);
+               set_errno(EBADPROC);
                return -1;
        }
        if (!proc_controls(p, target)) {
-               proc_decref(target, 1);
-               set_errno(current_tf, EPERM);
+               kref_put(&target->kref);
+               set_errno(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))) {
-                       proc_decref(target, 1);
-                       set_errno(current_tf, EINVAL);
+                       kref_put(&target->kref);
+                       set_errno(EINVAL);
                        return -1;
                }
                proc_notify(target, local_ne.ne_type, &local_ne);
        } else {
                proc_notify(target, notif, 0);
        }
-       proc_decref(target, 1);
+       kref_put(&target->kref);
        return 0;
 }
 
@@ -635,7 +611,7 @@ static int sys_self_notify(struct proc *p, uint32_t vcoreid, unsigned int notif,
        /* 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);
+                       set_errno(EINVAL);
                        return -1;
                }
                do_notify(p, vcoreid, local_ne.ne_type, &local_ne);
@@ -663,6 +639,7 @@ static int sys_halt_core(struct proc *p, unsigned int usec)
 //Read a buffer over the serial port
 static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
 {
+       printk("[kernel] serial reading is deprecated.\n");
        if (len == 0)
                return 0;
 
@@ -683,6 +660,7 @@ static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
 //Write a buffer over the serial port
 static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
 {
+       printk("[kernel] serial writing is deprecated.\n");
        if (len == 0)
                return 0;
        #ifdef __CONFIG_SERIAL_IO__
@@ -798,150 +776,226 @@ static int sys_eth_recv_check(env_t* e)
 
 #endif // Network
 
-// 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)
+static intreg_t sys_read(struct proc *p, int fd, void *buf, int len)
 {
-       int ret = 0;
-       void* kbuf = user_memdup_errno(p,buf,len);
-       if(kbuf == NULL)
+       ssize_t ret;
+       struct file *file = get_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(EBADF);
                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);
+       assert(file->f_op->read);
+       /* TODO: (UMEM) currently, read() handles user memcpy issues, but we
+        * probably should user_mem_check and pin the region here, so read doesn't
+        * worry about it */
+       ret = file->f_op->read(file, buf, len, &file->f_pos);
+       kref_put(&file->f_kref);
        return ret;
 }
 
-intreg_t sys_read(struct proc* p, int fd, void* buf, int len)
+static intreg_t sys_write(struct proc *p, int fd, const void *buf, int len)
 {
-       void* kbuf = kmalloc_errno(len);
-       if(kbuf == NULL)
+       /* Catch common usage of stdout and stderr.  No protections or anything. */
+       if (fd == 1) {
+               printk("[stdout]: %s\n", buf);
+               return len;
+       } else if (fd == 2) {
+               printk("[stderr]: %s\n", buf);
+               return len;
+       }
+       /* the real sys_write: */
+       ssize_t ret;
+       struct file *file = get_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(EBADF);
                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)
+       }
+       if (!file->f_op->write) {
+               kref_put(&file->f_kref);
+               set_errno(EINVAL);
                return -1;
-       int ret = ufe(pwrite,fd,PADDR(kbuf),len,offset);
-       user_memdup_free(p,kbuf);
+       }
+       /* TODO: (UMEM) */
+       ret = file->f_op->write(file, buf, len, &file->f_pos);
+       kref_put(&file->f_kref);
        return ret;
 }
 
-intreg_t sys_pread(struct proc* p, int fd, void* buf, int len, int offset)
+/* Checks args/reads in the path, opens the file, and inserts it into the
+ * process's open file list. 
+ *
+ * TODO: take the path length */
+static intreg_t sys_open(struct proc *p, const char *path, size_t path_l,
+                         int oflag, int mode)
 {
-       void* kbuf = kmalloc_errno(len);
-       if(kbuf == NULL)
+       int fd = 0;
+       struct file *file;
+
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
                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;
+       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 %s Open, res=%d\n", path, fd);
+       return fd;
 }
 
-intreg_t sys_open(struct proc* p, const char* path, int oflag, int mode)
+static intreg_t sys_close(struct proc *p, int fd)
 {
-       printd("File Open, p: %p, path: %s, oflag: %d, mode: 0x%x\n", p, path, oflag, mode);
-       char* fn = user_strdup_errno(p,path,PGSIZE);
-       if(fn == NULL) {
-               printd("File Open, user_strdup_errno failed\n");
+       struct file *file = put_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(EBADF);
                return -1;
        }
-       printd("File Open, About to open\n");
-       int ret = ufe(open,PADDR(fn),oflag,mode,0);
-       printd("File Open, res=%d\n", ret);
-       user_memdup_free(p,fn);
-       return ret;
-}
-intreg_t sys_close(struct proc* p, int fd)
-{
-       return ufe(close,fd,0,0,0);
+       return 0;
 }
 
-#define NEWLIB_STAT_SIZE 64
-intreg_t sys_fstat(struct proc* p, int fd, void* buf)
+/* 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)
 {
-       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;
+       struct kstat *kbuf;
+       struct file *file = get_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(EBADF);
+               return -1;
+       }
+       kbuf = kmalloc(sizeof(struct kstat), 0);
+       if (!kbuf) {
+               kref_put(&file->f_kref);
+               set_errno(ENOMEM);
+               return -1;
+       }
+       stat_inode(file->f_dentry->d_inode, kbuf);
+       kref_put(&file->f_kref);
+       /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
+       if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
+               kfree(kbuf);
+               set_errno(EINVAL);
+               return -1;
+       }
        kfree(kbuf);
-       return ret;
+       return 0;
 }
 
-intreg_t sys_stat(struct proc* p, const char* path, void* buf)
+/* sys_stat() and sys_lstat() do nearly the same thing, differing in how they
+ * treat a symlink for the final item, which (probably) will be controlled by
+ * the lookup flags */
+static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
+                            struct kstat *u_stat, int flags)
 {
-       char* fn = user_strdup_errno(p,path,PGSIZE);
-       if(fn == NULL)
+       struct kstat *kbuf;
+       struct dentry *path_d;
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
                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);
+       path_d = lookup_dentry(t_path, flags);
+       user_memdup_free(p, t_path);
+       if (!path_d)
+               return -1;
+       kbuf = kmalloc(sizeof(struct kstat), 0);
+       if (!kbuf) {
+               set_errno(ENOMEM);
+               kref_put(&path_d->d_kref);
+               return -1;
+       }
+       stat_inode(path_d->d_inode, kbuf);
+       kref_put(&path_d->d_kref);
+       /* TODO: UMEM: pin the memory, copy directly, and skip the kernel buffer */
+       if (memcpy_to_user_errno(p, u_stat, kbuf, sizeof(struct kstat))) {
+               kfree(kbuf);
+               set_errno(EINVAL);
+               return -1;
+       }
        kfree(kbuf);
-       return ret;
+       return 0;
 }
 
-intreg_t sys_lstat(struct proc* p, const char* path, void* buf)
+/* Follow a final symlink */
+static intreg_t sys_stat(struct proc *p, const char *path, size_t path_l,
+                         struct kstat *u_stat)
 {
-       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;
+       return stat_helper(p, path, path_l, u_stat, LOOKUP_FOLLOW);
+}
 
-       user_memdup_free(p,fn);
-       kfree(kbuf);
-       return ret;
+/* Don't follow a final symlink */
+static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
+                          struct kstat *u_stat)
+{
+       return stat_helper(p, path, path_l, u_stat, 0);
 }
 
-intreg_t sys_fcntl(struct procp, int fd, int cmd, int arg)
+intreg_t sys_fcntl(struct proc *p, int fd, int cmd, int arg)
 {
-       return ufe(fcntl,fd,cmd,arg,0);
+       int retval = 0;
+       struct file *file = get_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(EBADF);
+               return -1;
+       }
+       switch (cmd) {
+               case (F_DUPFD):
+                       printk("[kernel] dup not supported yet\n");
+                       break;
+               case (F_GETFD):
+                       /* GET and SETFD just care about CLOEXEC.  We don't have a separate
+                        * flag variable for the FD (we might need to, technically). */
+                       if (file->f_flags & O_CLOEXEC)
+                               retval = FD_CLOEXEC;
+                       break;
+               case (F_SETFD):
+                       if (arg == FD_CLOEXEC)
+                               file->f_flags |= O_CLOEXEC;
+                       break;
+               case (F_GETFL):
+                       retval = file->f_flags;
+                       break;
+               case (F_SETFL):
+                       /* only allowed to set certain flags. */
+                       arg &= O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NONBLOCK;
+                       break;
+               default:
+                       warn("Unsupported fcntl cmd %d\n", cmd);
+       }
+       kref_put(&file->f_kref);
+       return 0;
 }
 
-intreg_t sys_access(struct proc* p, const char* path, int type)
+static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
+                           int mode)
 {
-       char* fn = user_strdup_errno(p,path,PGSIZE);
-       if(fn == NULL)
+       int retval;
+
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
                return -1;
-       int ret = ufe(access,PADDR(fn),type,0,0);
-       user_memdup_free(p,fn);
-       return ret;
+       retval = do_file_access(t_path, mode);
+       user_memdup_free(p, t_path);
+       printd("Access for path: %s retval: %d\n", path, retval);
+       if (retval < 0) {
+               set_errno(-retval);
+               return -1;
+       }
+       return retval;
 }
 
-intreg_t sys_umask(struct procp, int mask)
+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)
+intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
 {
        char* fn = user_strdup_errno(p,path,PGSIZE);
        if(fn == NULL)
@@ -951,12 +1005,21 @@ intreg_t sys_chmod(struct proc* p, const char* path, int mode)
        return ret;
 }
 
-intreg_t sys_lseek(struct proc* p, int fd, int offset, int whence)
+static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
 {
-       return ufe(lseek,fd,offset,whence,0);
+       off_t ret;
+       struct file *file = get_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(EBADF);
+               return -1;
+       }
+       ret = file->f_op->llseek(file, offset, whence);
+       kref_put(&file->f_kref);
+       return ret;
 }
 
-intreg_t sys_link(struct proc* p, const char* _old, const char* _new)
+intreg_t sys_link(struct proc *p, const char *_old, size_t old_l,
+                  const char *_new, size_t new_l)
 {
        char* oldpath = user_strdup_errno(p,_old,PGSIZE);
        if(oldpath == NULL)
@@ -975,7 +1038,7 @@ intreg_t sys_link(struct proc* p, const char* _old, const char* _new)
        return ret;
 }
 
-intreg_t sys_unlink(struct proc* p, const char* path)
+intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
 {
        char* fn = user_strdup_errno(p,path,PGSIZE);
        if(fn == NULL)
@@ -985,7 +1048,50 @@ intreg_t sys_unlink(struct proc* p, const char* path)
        return ret;
 }
 
-intreg_t sys_chdir(struct proc* p, const char* path)
+intreg_t sys_symlink(struct proc *p, char *old_path, size_t old_l,
+                     char *new_path, size_t new_l)
+{
+       int ret;
+       char *t_oldpath = user_strdup_errno(p, old_path, old_l);
+       if (t_oldpath == NULL)
+               return -1;
+       char *t_newpath = user_strdup_errno(p, new_path, new_l);
+       if (t_newpath == NULL) {
+               user_memdup_free(p, t_oldpath);
+               return -1;
+       }
+       ret = do_symlink(new_path, old_path, S_IRWXU | S_IRWXG | S_IRWXO);
+       user_memdup_free(p, t_oldpath);
+       user_memdup_free(p, t_newpath);
+       return ret;
+}
+
+intreg_t sys_readlink(struct proc *p, char *path, size_t path_l,
+                      char *u_buf, size_t buf_l)
+{
+       char *symname;
+       ssize_t copy_amt;
+       struct dentry *path_d;
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (t_path == NULL)
+               return -1;
+       path_d = lookup_dentry(t_path, 0);
+       user_memdup_free(p, t_path);
+       if (!path_d)
+               return -1;
+       symname = path_d->d_inode->i_op->readlink(path_d);
+       copy_amt = strnlen(symname, buf_l - 1) + 1;
+       if (memcpy_to_user_errno(p, u_buf, symname, copy_amt)) {
+               kref_put(&path_d->d_kref);
+               set_errno(EINVAL);
+               return -1;
+       }
+       kref_put(&path_d->d_kref);
+       printd("READLINK returning %s\n", u_buf);
+       return copy_amt;
+}
+
+intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
 {
        char* fn = user_strdup_errno(p,path,PGSIZE);
        if(fn == NULL)
@@ -995,7 +1101,7 @@ intreg_t sys_chdir(struct proc* p, const char* path)
        return ret;
 }
 
-intreg_t sys_getcwd(struct proc* p, char* pwd, int size)
+intreg_t sys_getcwd(struct proc *p, char *pwd, int size)
 {
        void* kbuf = kmalloc_errno(size);
        if(kbuf == NULL)
@@ -1007,7 +1113,7 @@ intreg_t sys_getcwd(struct proc* p, char* pwd, int size)
        return ret;
 }
 
-intreg_t sys_gettimeofday(struct proc* p, int* buf)
+intreg_t sys_gettimeofday(struct proc *p, int *buf)
 {
        static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
        static int t0 = 0;
@@ -1015,8 +1121,7 @@ intreg_t sys_gettimeofday(struct proc* p, int* buf)
        spin_lock(&gtod_lock);
        if(t0 == 0)
 
-#if (defined __CONFIG_APPSERVER__) && (!defined __CONFIG_OSDI__)
-       // For OSDI, do not get time from appserver because it would lead to inaccurate measurements.
+#if (defined __CONFIG_APPSERVER__)
        t0 = ufe(time,0,0,0,0);
 #else
        // Nanwan's birthday, bitches!!
@@ -1032,7 +1137,7 @@ intreg_t sys_gettimeofday(struct proc* p, int* buf)
 }
 
 #define SIZEOF_STRUCT_TERMIOS 60
-intreg_t sys_tcgetattr(struct proc* p, int fd, void* termios_p)
+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);
@@ -1042,7 +1147,8 @@ intreg_t sys_tcgetattr(struct proc* p, int fd, void* termios_p)
        return ret;
 }
 
-intreg_t sys_tcsetattr(struct proc* p, int fd, int optional_actions, const void* termios_p)
+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)
@@ -1052,22 +1158,6 @@ intreg_t sys_tcsetattr(struct proc* p, int fd, int optional_actions, const void*
        return ret;
 }
 
-intreg_t sys_fillmeup(struct proc *p, uint8_t *bufs, 
-                      uint16_t num_bufs, int16_t *last_written)
-{
-#if defined(__CONFIG_OSDI__) && defined(__CONFIG_NETWORKING__)
-       extern struct fillmeup fillmeup_data;
-       fillmeup_data.proc = p;
-       fillmeup_data.bufs = bufs;
-       fillmeup_data.num_bufs = num_bufs;
-       fillmeup_data.last_written = last_written;
-       *last_written = -1;
-       return 0;
-#else
-       return -1;
-#endif
-}
-
 /************** Syscall Invokation **************/
 
 /* Executes the given syscall.
@@ -1075,16 +1165,15 @@ intreg_t sys_fillmeup(struct proc *p, uint8_t *bufs,
  * Note tf is passed in, which points to the tf of the context on the kernel
  * stack.  If any syscall needs to block, it needs to save this info, as well as
  * any silly state.
- *
- * TODO: Build a dispatch table instead of switching on the syscallno
- * Dispatches to the correct kernel function, passing the arguments.
- */
+ * 
+ * This syscall function is used by both local syscall and arsc, and should
+ * remain oblivious of the caller. */
 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);
+       /* Initialize the return value and error code returned to 0 */
+       set_retval(ESUCCESS);
+       set_errno(ESUCCESS);
 
        typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
                                      uintreg_t,uintreg_t,uintreg_t);
@@ -1103,14 +1192,12 @@ 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,
                [SYS_mmap] = (syscall_t)sys_mmap,
                [SYS_munmap] = (syscall_t)sys_munmap,
                [SYS_mprotect] = (syscall_t)sys_mprotect,
-               [SYS_brk] = (syscall_t)sys_brk,
                [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,
@@ -1127,8 +1214,8 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
                [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
                [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
        #endif
-       #ifdef __CONFIG_OSDI__
-               [SYS_fillmeup] = (syscall_t)sys_fillmeup,
+       #ifdef __CONFIG_ARSC_SERVER__
+               [SYS_init_arsc] = (syscall_t)sys_init_arsc,
        #endif
                // Syscalls serviced by the appserver for now.
                [SYS_read] = (syscall_t)sys_read,
@@ -1145,6 +1232,8 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
                [SYS_lseek] = (syscall_t)sys_lseek,
                [SYS_link] = (syscall_t)sys_link,
                [SYS_unlink] = (syscall_t)sys_unlink,
+               [SYS_symlink] = (syscall_t)sys_symlink,
+               [SYS_readlink] = (syscall_t)sys_readlink,
                [SYS_chdir] = (syscall_t)sys_chdir,
                [SYS_getcwd] = (syscall_t)sys_getcwd,
                [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
@@ -1188,58 +1277,6 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
        return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
 }
 
-intreg_t syscall_async(struct proc *p, syscall_req_t *call)
-{
-       return syscall(p, call->num, call->args[0], call->args[1],
-                      call->args[2], call->args[3], call->args[4]);
-}
-
-/* You should already have a refcnt'd ref to p before calling this */
-intreg_t process_generic_syscalls(struct proc *p, size_t max)
-{
-       size_t count = 0;
-       syscall_back_ring_t* sysbr = &p->syscallbackring;
-
-       /* make sure the proc is still alive, and keep it from dying from under us
-        * incref will return ESUCCESS on success.  This might need some thought
-        * regarding when the incref should have happened (like by whoever passed us
-        * 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);
-
-       // max is the most we'll process.  max = 0 means do as many as possible
-       while (RING_HAS_UNCONSUMED_REQUESTS(sysbr) && ((!max)||(count < max)) ) {
-               if (!count) {
-                       // ASSUME: one queue per process
-                       // only switch cr3 for the very first request for this queue
-                       // need to switch to the right context, so we can handle the user pointer
-                       // that points to a data payload of the syscall
-                       lcr3(p->env_cr3);
-               }
-               count++;
-               //printk("DEBUG PRE: sring->req_prod: %d, sring->rsp_prod: %d\n",
-               //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
-               // might want to think about 0-ing this out, if we aren't
-               // going to explicitly fill in all fields
-               syscall_rsp_t rsp;
-               // this assumes we get our answer immediately for the syscall.
-               syscall_req_t* req = RING_GET_REQUEST(sysbr, ++(sysbr->req_cons));
-               rsp.retval = syscall_async(p, req);
-               // write response into the slot it came from
-               memcpy(req, &rsp, sizeof(syscall_rsp_t));
-               // update our counter for what we've produced (assumes we went in order!)
-               (sysbr->rsp_prod_pvt)++;
-               RING_PUSH_RESPONSES(sysbr);
-               //printk("DEBUG POST: sring->req_prod: %d, sring->rsp_prod: %d\n",
-               //         sysbr->sring->req_prod, sysbr->sring->rsp_prod);
-       }
-       // load sane page tables (and don't rely on decref to do it for you).
-       lcr3(boot_cr3);
-       proc_decref(p, 1);
-       return (intreg_t)count;
-}
-
 /* Syscall tracing */
 static void __init_systrace(void)
 {
@@ -1339,3 +1376,15 @@ void systrace_clear_buffer(void)
        memset(systrace_buffer, 0, sizeof(struct systrace_record)*MAX_NUM_TRACED);
        spin_unlock_irqsave(&systrace_lock);
 }
+
+void set_retval(uint32_t retval)
+{
+       struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
+       *(coreinfo->cur_ret.returnloc) = retval;
+}
+void set_errno(uint32_t errno)
+{
+       struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
+       if (coreinfo && coreinfo->cur_ret.errno_loc)
+               *(coreinfo->cur_ret.errno_loc) = errno;
+}