ext2_get_ino_metablock() will allocate blocks
[akaros.git] / kern / src / syscall.c
index 1b934a2..97165e7 100644 (file)
@@ -30,7 +30,9 @@
 #include <colored_caches.h>
 #include <hashtable.h>
 #include <arch/bitmask.h>
-#include <kfs.h> // eventually replace this with vfs.h
+#include <vfs.h>
+#include <devfs.h>
+#include <smp.h>
 #include <arsc_server.h>
 
 
@@ -109,6 +111,7 @@ static int sys_cache_buster(struct proc *p, uint32_t num_writes,
                        upage_alloc(p, &a_page[i],1);
                        page_insert(p->env_pgdir, a_page[i], (void*)INSERT_ADDR + PGSIZE*i,
                                    PTE_USER_RW);
+                       page_decref(a_page[i]);
                }
                spin_unlock(&buster_lock);
        }
@@ -229,6 +232,11 @@ static int sys_proc_create(struct proc *p, char *path, size_t path_l,
        if (load_elf(new_p, program))
                goto late_error;
        kref_put(&program->f_kref);
+       /* Connect to stdin, stdout, stderr (part of proc_create()) */
+       assert(insert_file(&new_p->open_files, dev_stdin,  0) == 0);
+       assert(insert_file(&new_p->open_files, dev_stdout, 0) == 1);
+       assert(insert_file(&new_p->open_files, dev_stderr, 0) == 2);
+       __proc_ready(new_p);
        pid = new_p->pid;
        kref_put(&new_p->kref); /* give up the reference created in proc_create() */
        return pid;
@@ -275,12 +283,12 @@ 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;
        }
        if (!proc_controls(p, p_to_die)) {
                kref_put(&p_to_die->kref);
-               set_errno(current_tf, EPERM);
+               set_errno(EPERM);
                return -1;
        }
        if (p_to_die == p) {
@@ -305,12 +313,15 @@ static int sys_proc_yield(struct proc *p, bool being_nice)
 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;
+       }
+       /* 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);
@@ -340,8 +351,8 @@ static ssize_t sys_fork(env_t* e)
                                page_decref(pp);
                                return -1;
                        }
-
                        pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
+                       page_decref(pp);
                } else {
                        assert(PAGE_PAGED_OUT(*pte));
                        /* TODO: (SWAP) will need to either make a copy or CoW/refcnt the
@@ -367,10 +378,11 @@ static ssize_t sys_fork(env_t* e)
         * 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);
+               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);
 
@@ -380,7 +392,6 @@ static ssize_t sys_fork(env_t* e)
        // when the parent dies, or at least decref it
 
        printd("[PID %d] fork PID %d\n",e->pid,env->pid);
-
        return env->pid;
 }
 
@@ -395,9 +406,15 @@ static int sys_exec(struct proc *p, char *path, size_t path_l,
        struct file *program;
 
        /* We probably want it to never be allowed to exec if it ever was _M */
-       if(p->state != PROC_RUNNING_S)
+       if (p->state != PROC_RUNNING_S) {
+               set_errno(EINVAL);
                return -1;
-
+       }
+       /* Can't really exec if we don't have a current_tf to reset */
+       if (!current_tf) {
+               set_errno(EINVAL);
+               return -1;
+       }
        /* Copy in the path.  Consider putting an upper bound on path_l. */
        t_path = user_strdup_errno(p, path, path_l);
        if (!t_path)
@@ -461,13 +478,13 @@ 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;
                }
 
@@ -478,7 +495,7 @@ static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
                return ret;
        }
 
-       set_errno(current_tf,1);
+       set_errno(EPERM);
        return -1;
 }
 
@@ -508,43 +525,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;
-
-       void * COUNT(1) * COUNT(1) addr = user_mem_assert(p1, _addr, sizeof(void *),
-                                                      PTE_USER_RW);
-       struct proc *p2 = pid2proc(p2_id);
-       if (!p2)
-               return -EBADPROC;
-
-       page_t* page;
-       error_t e = upage_alloc(p1, &page,1);
-       if (e < 0) {
-               kref_put(&p2->kref);
-               return e;
-       }
-
-       void* p2_addr = page_insert_in_range(p2->env_pgdir, page,
-                       (void*SNT)UTEXT, (void*SNT)UTOP, p2_flags);
-       if (p2_addr == NULL) {
-               page_free(page);
-               kref_put(&p2->kref);
-               return -EFAIL;
-       }
-
-       void* p1_addr = page_insert_in_range(p1->env_pgdir, page,
-                       (void*SNT)UTEXT, (void*SNT)UTOP, p1_flags);
-       if(p1_addr == NULL) {
-               page_remove(p2->env_pgdir, p2_addr);
-               page_free(page);
-               kref_put(&p2->kref);
-               return -EFAIL;
-       }
-       *addr = p1_addr;
-       kref_put(&p2->kref);
-       return ESUCCESS;
+       printk("[kernel] shared page alloc is deprecated/unimplemented.\n");
+       return -1;
 }
 
 static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
@@ -564,19 +546,19 @@ 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)) {
                kref_put(&target->kref);
-               set_errno(current_tf, EPERM);
+               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))) {
                        kref_put(&target->kref);
-                       set_errno(current_tf, EINVAL);
+                       set_errno(EINVAL);
                        return -1;
                }
                proc_notify(target, local_ne.ne_type, &local_ne);
@@ -599,7 +581,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);
@@ -769,7 +751,12 @@ static intreg_t sys_read(struct proc *p, int fd, void *buf, int len)
        ssize_t ret;
        struct file *file = get_file_from_fd(&p->open_files, fd);
        if (!file) {
-               set_errno(current_tf, EBADF);
+               set_errno(EBADF);
+               return -1;
+       }
+       if (!file->f_op->read) {
+               kref_put(&file->f_kref);
+               set_errno(EINVAL);
                return -1;
        }
        /* TODO: (UMEM) currently, read() handles user memcpy issues, but we
@@ -782,19 +769,15 @@ static 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)
 {
-       /* 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(current_tf, EBADF);
+               set_errno(EBADF);
+               return -1;
+       }
+       if (!file->f_op->write) {
+               kref_put(&file->f_kref);
+               set_errno(EINVAL);
                return -1;
        }
        /* TODO: (UMEM) */
@@ -813,14 +796,16 @@ static intreg_t sys_open(struct proc *p, const char *path, size_t path_l,
        int fd = 0;
        struct file *file;
 
+       printd("File %s Open attempt\n", path);
        char *t_path = user_strdup_errno(p, path, path_l);
        if (!t_path)
                return -1;
+       mode &= ~p->fs_env.umask;
        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 */
+       fd = insert_file(&p->open_files, file, 0);      /* stores the ref to file */
        kref_put(&file->f_kref);
        if (fd < 0) {
                warn("File insertion failed");
@@ -834,7 +819,7 @@ static 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);
+               set_errno(EBADF);
                return -1;
        }
        return 0;
@@ -850,13 +835,13 @@ static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
        struct kstat *kbuf;
        struct file *file = get_file_from_fd(&p->open_files, fd);
        if (!file) {
-               set_errno(current_tf, EBADF);
+               set_errno(EBADF);
                return -1;
        }
        kbuf = kmalloc(sizeof(struct kstat), 0);
        if (!kbuf) {
                kref_put(&file->f_kref);
-               set_errno(current_tf, ENOMEM);
+               set_errno(ENOMEM);
                return -1;
        }
        stat_inode(file->f_dentry->d_inode, kbuf);
@@ -864,7 +849,7 @@ static intreg_t sys_fstat(struct proc *p, int fd, struct kstat *u_stat)
        /* 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(current_tf, EINVAL);
+               set_errno(EINVAL);
                return -1;
        }
        kfree(kbuf);
@@ -878,26 +863,26 @@ static intreg_t stat_helper(struct proc *p, const char *path, size_t path_l,
                             struct kstat *u_stat, int flags)
 {
        struct kstat *kbuf;
-       struct inode *path_i;
+       struct dentry *path_d;
        char *t_path = user_strdup_errno(p, path, path_l);
        if (!t_path)
                return -1;
-       path_i = lookup_inode(t_path, flags);
+       path_d = lookup_dentry(t_path, flags);
        user_memdup_free(p, t_path);
-       if (!path_i)
+       if (!path_d)
                return -1;
        kbuf = kmalloc(sizeof(struct kstat), 0);
        if (!kbuf) {
-               set_errno(current_tf, ENOMEM);
-               kref_put(&path_i->i_kref);
+               set_errno(ENOMEM);
+               kref_put(&path_d->d_kref);
                return -1;
        }
-       stat_inode(path_i, kbuf);
-       kref_put(&path_i->i_kref);
+       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(current_tf, EINVAL);
+               set_errno(EINVAL);
                return -1;
        }
        kfree(kbuf);
@@ -920,22 +905,54 @@ static intreg_t sys_lstat(struct proc *p, const char *path, size_t path_l,
 
 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):
+                       retval = insert_file(&p->open_files, file, arg);
+                       if (retval < 0) {
+                               set_errno(-retval);
+                               retval = -1;
+                       }
+                       break;
+               case (F_GETFD):
+                       retval = p->open_files.fd[fd].fd_flags;
+                       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_FCNTL_FLAGS;
+                       file->f_flags = (file->f_flags & ~O_FCNTL_FLAGS) | arg;
+                       break;
+               default:
+                       warn("Unsupported fcntl cmd %d\n", cmd);
+       }
+       kref_put(&file->f_kref);
+       return retval;
 }
 
 static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
                            int mode)
 {
        int retval;
-
        char *t_path = user_strdup_errno(p, path, path_l);
        if (!t_path)
                return -1;
-       retval = do_file_access(t_path, mode);
+       retval = do_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(current_tf, -retval);
+               set_errno(-retval);
                return -1;
        }
        return retval;
@@ -943,17 +960,24 @@ static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
 
 intreg_t sys_umask(struct proc *p, int mask)
 {
-       return ufe(umask,mask,0,0,0);
+       int old_mask = p->fs_env.umask;
+       p->fs_env.umask = mask & S_PMASK;
+       return old_mask;
 }
 
 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)
+       int retval;
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
                return -1;
-       int ret = ufe(chmod,PADDR(fn),mode,0,0);
-       user_memdup_free(p,fn);
-       return ret;
+       retval = do_chmod(t_path, mode);
+       user_memdup_free(p, t_path);
+       if (retval < 0) {
+               set_errno(-retval);
+               return -1;
+       }
+       return retval;
 }
 
 static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
@@ -961,7 +985,7 @@ static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
        off_t ret;
        struct file *file = get_file_from_fd(&p->open_files, fd);
        if (!file) {
-               set_errno(current_tf, EBADF);
+               set_errno(EBADF);
                return -1;
        }
        ret = file->f_op->llseek(file, offset, whence);
@@ -969,56 +993,128 @@ static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
        return ret;
 }
 
-intreg_t sys_link(struct proc *p, const char *_old, size_t old_l,
-                  const char *_new, size_t new_l)
+intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
+                  char *new_path, size_t new_l)
 {
-       char* oldpath = user_strdup_errno(p,_old,PGSIZE);
-       if(oldpath == NULL)
+       int ret;
+       char *t_oldpath = user_strdup_errno(p, old_path, old_l);
+       if (t_oldpath == NULL)
                return -1;
-
-       char* newpath = user_strdup_errno(p,_new,PGSIZE);
-       if(newpath == NULL)
-       {
-               user_memdup_free(p,oldpath);
+       char *t_newpath = user_strdup_errno(p, new_path, new_l);
+       if (t_newpath == NULL) {
+               user_memdup_free(p, t_oldpath);
                return -1;
        }
-
-       int ret = ufe(link,PADDR(oldpath),PADDR(newpath),0,0);
-       user_memdup_free(p,oldpath);
-       user_memdup_free(p,newpath);
+       ret = do_link(t_oldpath, t_newpath);
+       user_memdup_free(p, t_oldpath);
+       user_memdup_free(p, t_newpath);
        return ret;
 }
 
 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)
+       int retval;
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
+               return -1;
+       retval = do_unlink(t_path);
+       user_memdup_free(p, t_path);
+       return retval;
+}
+
+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;
-       int ret = ufe(unlink,PADDR(fn),0,0,0);
-       user_memdup_free(p,fn);
+       }
+       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)
+       int retval;
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
                return -1;
-       int ret = ufe(chdir,PADDR(fn),0,0,0);
-       user_memdup_free(p,fn);
-       return ret;
+       retval = do_chdir(&p->fs_env, t_path);
+       user_memdup_free(p, t_path);
+       if (retval) {
+               set_errno(-retval);
+               return -1;
+       }
+       return 0;
 }
 
-intreg_t sys_getcwd(struct proc *p, char *pwd, int size)
+/* Note cwd_l is not a strlen, it's an absolute size */
+intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
 {
-       void* kbuf = kmalloc_errno(size);
-       if(kbuf == NULL)
+       int retval = 0;
+       char *kfree_this;
+       char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
+       if (!k_cwd)
+               return -1;              /* errno set by do_getcwd */
+       if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
+               retval = -1;
+       kfree(kfree_this);
+       return retval;
+}
+
+intreg_t sys_mkdir(struct proc *p, const char *path, size_t path_l, int mode)
+{
+       int retval;
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
                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;
+       mode &= ~p->fs_env.umask;
+       retval = do_mkdir(t_path, mode);
+       user_memdup_free(p, t_path);
+       return retval;
+}
+
+intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
+{
+       int retval;
+       char *t_path = user_strdup_errno(p, path, path_l);
+       if (!t_path)
+               return -1;
+       retval = do_rmdir(t_path);
+       user_memdup_free(p, t_path);
+       return retval;
 }
 
 intreg_t sys_gettimeofday(struct proc *p, int *buf)
@@ -1066,8 +1162,88 @@ intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
        return ret;
 }
 
+/* TODO: we don't have any notion of UIDs or GIDs yet, but don't let that stop a
+ * process from thinking it can do these.  The other alternative is to have
+ * glibc return 0 right away, though someone might want to do something with
+ * these calls.  Someday. */
+intreg_t sys_setuid(struct proc *p, uid_t uid)
+{
+       return 0;
+}
+
+intreg_t sys_setgid(struct proc *p, gid_t gid)
+{
+       return 0;
+}
+
 /************** Syscall Invokation **************/
 
+const static struct sys_table_entry syscall_table[] = {
+       [SYS_null] = {(syscall_t)sys_null, "null"},
+       [SYS_cache_buster] = {(syscall_t)sys_cache_buster, "buster"},
+       [SYS_cache_invalidate] = {(syscall_t)sys_cache_invalidate, "wbinv"},
+       [SYS_reboot] = {(syscall_t)reboot, "reboot!"},
+       [SYS_cputs] = {(syscall_t)sys_cputs, "cputs"},
+       [SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
+       [SYS_getcpuid] = {(syscall_t)sys_getcpuid, "getcpuid"},
+       [SYS_getvcoreid] = {(syscall_t)sys_getvcoreid, "getvcoreid"},
+       [SYS_getpid] = {(syscall_t)sys_getpid, "getpid"},
+       [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_fork] = {(syscall_t)sys_fork, "fork"},
+       [SYS_exec] = {(syscall_t)sys_exec, "exec"},
+       [SYS_trywait] = {(syscall_t)sys_trywait, "trywait"},
+       [SYS_mmap] = {(syscall_t)sys_mmap, "mmap"},
+       [SYS_munmap] = {(syscall_t)sys_munmap, "munmap"},
+       [SYS_mprotect] = {(syscall_t)sys_mprotect, "mprotect"},
+       [SYS_shared_page_alloc] = {(syscall_t)sys_shared_page_alloc, "pa"},
+       [SYS_shared_page_free] = {(syscall_t)sys_shared_page_free, "pf"},
+       [SYS_resource_req] = {(syscall_t)resource_req, "resource_req"},
+       [SYS_notify] = {(syscall_t)sys_notify, "notify"},
+       [SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
+       [SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
+#ifdef __CONFIG_SERIAL_IO__
+       [SYS_serial_read] = {(syscall_t)sys_serial_read, "ser_read"},
+       [SYS_serial_write] = {(syscall_t)sys_serial_write, "ser_write"},
+#endif
+#ifdef __CONFIG_NETWORKING__
+       [SYS_eth_read] = {(syscall_t)sys_eth_read, "eth_read"},
+       [SYS_eth_write] = {(syscall_t)sys_eth_write, "eth_write"},
+       [SYS_eth_get_mac_addr] = {(syscall_t)sys_eth_get_mac_addr, "get_mac"},
+       [SYS_eth_recv_check] = {(syscall_t)sys_eth_recv_check, "recv_check"},
+#endif
+#ifdef __CONFIG_ARSC_SERVER__
+       [SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
+#endif
+       [SYS_read] = {(syscall_t)sys_read, "read"},
+       [SYS_write] = {(syscall_t)sys_write, "write"},
+       [SYS_open] = {(syscall_t)sys_open, "open"},
+       [SYS_close] = {(syscall_t)sys_close, "close"},
+       [SYS_fstat] = {(syscall_t)sys_fstat, "fstat"},
+       [SYS_stat] = {(syscall_t)sys_stat, "stat"},
+       [SYS_lstat] = {(syscall_t)sys_lstat, "lstat"},
+       [SYS_fcntl] = {(syscall_t)sys_fcntl, "fcntl"},
+       [SYS_access] = {(syscall_t)sys_access, "access"},
+       [SYS_umask] = {(syscall_t)sys_umask, "umask"},
+       [SYS_chmod] = {(syscall_t)sys_chmod, "chmod"},
+       [SYS_lseek] = {(syscall_t)sys_lseek, "lseek"},
+       [SYS_link] = {(syscall_t)sys_link, "link"},
+       [SYS_unlink] = {(syscall_t)sys_unlink, "unlink"},
+       [SYS_symlink] = {(syscall_t)sys_symlink, "symlink"},
+       [SYS_readlink] = {(syscall_t)sys_readlink, "readlink"},
+       [SYS_chdir] = {(syscall_t)sys_chdir, "chdir"},
+       [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
+       [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdri"},
+       [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
+       [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
+       [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
+       [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
+       [SYS_setuid] = {(syscall_t)sys_setuid, "setuid"},
+       [SYS_setgid] = {(syscall_t)sys_setgid, "setgid"}
+};
+
 /* Executes the given syscall.
  *
  * Note tf is passed in, which points to the tf of the context on the kernel
@@ -1075,83 +1251,13 @@ intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
  * any silly state.
  * 
  * This syscall function is used by both local syscall and arsc, and should
- * remain oblivious of the caller.
- *
- * TODO: Keep in mind that not every syscall has a user trapframe. 
- * e.g. ARSC
- */
+ * 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
-       if(current_tf != NULL){
-               proc_set_syscall_retval(&p->env_tf, ESUCCESS);
-               set_errno(current_tf,0);
-       }
-
-       typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
-                                     uintreg_t,uintreg_t,uintreg_t);
-
-       const static syscall_t syscall_table[] = {
-               [SYS_null] = (syscall_t)sys_null,
-               [SYS_cache_buster] = (syscall_t)sys_cache_buster,
-               [SYS_cache_invalidate] = (syscall_t)sys_cache_invalidate,
-               [SYS_reboot] = (syscall_t)reboot,
-               [SYS_cputs] = (syscall_t)sys_cputs,
-               [SYS_cgetc] = (syscall_t)sys_cgetc,
-               [SYS_getcpuid] = (syscall_t)sys_getcpuid,
-               [SYS_getvcoreid] = (syscall_t)sys_getvcoreid,
-               [SYS_getpid] = (syscall_t)sys_getpid,
-               [SYS_proc_create] = (syscall_t)sys_proc_create,
-               [SYS_proc_run] = (syscall_t)sys_proc_run,
-               [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
-               [SYS_yield] = (syscall_t)sys_proc_yield,
-               [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_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,
-               [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 __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 __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,
-               [SYS_write] = (syscall_t)sys_write,
-               [SYS_open] = (syscall_t)sys_open,
-               [SYS_close] = (syscall_t)sys_close,
-               [SYS_fstat] = (syscall_t)sys_fstat,
-               [SYS_stat] = (syscall_t)sys_stat,
-               [SYS_lstat] = (syscall_t)sys_lstat,
-               [SYS_fcntl] = (syscall_t)sys_fcntl,
-               [SYS_access] = (syscall_t)sys_access,
-               [SYS_umask] = (syscall_t)sys_umask,
-               [SYS_chmod] = (syscall_t)sys_chmod,
-               [SYS_lseek] = (syscall_t)sys_lseek,
-               [SYS_link] = (syscall_t)sys_link,
-               [SYS_unlink] = (syscall_t)sys_unlink,
-               [SYS_chdir] = (syscall_t)sys_chdir,
-               [SYS_getcwd] = (syscall_t)sys_getcwd,
-               [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
-               [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
-               [SYS_tcsetattr] = (syscall_t)sys_tcsetattr
-       };
+       /* Initialize the return value and error code returned to 0 */
+       set_retval(ESUCCESS);
+       set_errno(ESUCCESS);
 
        const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
 
@@ -1159,10 +1265,12 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
        if (systrace_flags & SYSTRACE_ON) {
                if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
                        coreid = core_id();
-                       vcoreid = proc_get_vcoreid(p, core_id());
+                       vcoreid = proc_get_vcoreid(p, coreid);
                        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);
+                               printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
+                                      "%08p) proc: %d core: %d vcore: %d\n", read_tsc(),
+                                      syscallno, syscall_table[syscallno].name, a1, a2, a3,
+                                      a4, a5, p->pid, coreid, vcoreid);
                        } else {
                                struct systrace_record *trace;
                                unsigned int idx, new_idx;
@@ -1173,6 +1281,11 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
                                trace = &systrace_buffer[idx];
                                trace->timestamp = read_tsc();
                                trace->syscallno = syscallno;
+                               trace->arg1 = a1;
+                               trace->arg2 = a2;
+                               trace->arg3 = a3;
+                               trace->arg4 = a4;
+                               trace->arg5 = a5;
                                trace->pid = p->pid;
                                trace->coreid = coreid;
                                trace->vcoreid = vcoreid;
@@ -1183,10 +1296,10 @@ intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
        //       " a2: %x\n    a3: %x\n    a4: %x\n    a5: %x\n", core_id(),
        //       syscallno, a1, a2, a3, a4, a5);
 
-       if(syscallno > max_syscall || syscall_table[syscallno] == NULL)
+       if (syscallno > max_syscall || syscall_table[syscallno].call == NULL)
                panic("Invalid syscall number %d for proc %x!", syscallno, *p);
 
-       return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
+       return syscall_table[syscallno].call(p, a1, a2, a3, a4, a5);
 }
 
 /* Syscall tracing */
@@ -1273,9 +1386,16 @@ void systrace_print(bool all, struct proc *p)
         * 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",
+                       printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
+                              "%08p) proc: %d core: %d vcore: %d\n",
                               systrace_buffer[i].timestamp,
                               systrace_buffer[i].syscallno,
+                              syscall_table[systrace_buffer[i].syscallno].name,
+                              systrace_buffer[i].arg1,
+                              systrace_buffer[i].arg2,
+                              systrace_buffer[i].arg3,
+                              systrace_buffer[i].arg4,
+                              systrace_buffer[i].arg5,
                               systrace_buffer[i].pid,
                               systrace_buffer[i].coreid,
                               systrace_buffer[i].vcoreid);
@@ -1285,6 +1405,18 @@ void systrace_print(bool all, struct proc *p)
 void systrace_clear_buffer(void)
 {
        spin_lock_irqsave(&systrace_lock);
-       memset(systrace_buffer, 0, sizeof(struct systrace_record)*MAX_NUM_TRACED);
+       memset(systrace_buffer, 0, sizeof(struct systrace_record) * MAX_SYSTRACES);
        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;
+}