Added demand paging support
[akaros.git] / kern / src / frontend.c
index 366772e..e3fccb2 100644 (file)
 #include <frontend.h>
 #include <syscall.h>
 #include <smp.h>
+#include <slab.h>
 
 volatile int magic_mem[10];
 
+void
+frontend_proc_init(struct proc *SAFE p)
+{
+       pid_t parent_id = p->ppid, id = p->pid;
+       int32_t errno;
+       if(frontend_syscall(parent_id,APPSERVER_SYSCALL_proc_init,id,0,0,0,&errno))
+               panic("Front-end server couldn't initialize new process!");
+}
+
+void
+frontend_proc_free(struct proc *SAFE p)
+{
+       int32_t errno;
+       if(frontend_syscall(0,APPSERVER_SYSCALL_proc_free,p->pid,0,0,0,&errno))
+               panic("Front-end server couldn't free process!");
+}
+
 void* user_memdup(struct proc* p, const void* va, int len)
 {
        void* kva = NULL;
@@ -57,7 +75,7 @@ char* user_strdup(struct proc* p, const char* va0, int max)
        const char* va = va0;
        while(max > 0 && len == 0)
        {
-               int thislen = MIN(PGSIZE-(intptr_t)va%PGSIZE,max);
+               int thislen = MIN(PGSIZE-(uintptr_t)va%PGSIZE,max);
                if(memcpy_from_user(p,kbuf,va,thislen))
                {
                        kfree(kbuf);
@@ -107,31 +125,99 @@ void* kmalloc_errno(int len)
        return kva;
 }
 
-error_t read_page(struct proc* p, int fd, physaddr_t pa, int pgoff)
+struct kmem_cache* struct_file_cache;
+void file_init()
 {
-       int errno;
-       int ret = frontend_syscall(p->pid,APPSERVER_SYSCALL_pread,fd,
-                               pa,PGSIZE,pgoff*PGSIZE,&errno);
+       struct_file_cache = kmem_cache_create("struct_file",
+                                             sizeof(struct file), 8, 0, 0, 0);
+}
 
+error_t file_read_page(struct file* f, physaddr_t pa, size_t pgoff)
+{
+       int ret = frontend_syscall(0,APPSERVER_SYSCALL_pread,f->fd,pa,PGSIZE,
+                                  pgoff*PGSIZE,NULL);
        if(ret >= 0)
                memset(KADDR(pa)+ret,0,PGSIZE-ret);
        return ret;
 }
 
-error_t open_file(struct proc* p, const char* path, int oflag, int mode)
+struct file* file_open_from_fd(struct proc* p, int fd)
+{
+       struct file* f = NULL;
+       if(!(f = kmem_cache_alloc(struct_file_cache,0)))
+               goto out;
+
+       f->fd = frontend_syscall(p->pid,APPSERVER_SYSCALL_kdup,fd,0,0,0,NULL);
+       if(f->fd == -1)
+       {
+               kmem_cache_free(struct_file_cache,f);
+               f = NULL;
+               goto out;
+       }
+       spinlock_init(&f->lock);
+       f->refcnt = 1;
+
+out:
+       return f;
+}
+
+struct file* file_open(const char* path, int oflag, int mode)
+{
+       struct file* f = NULL;
+       // although path is a kernel pointer, it may be below KERNBASE.
+       // fix that if so.
+       char* malloced = NULL;
+       if((uintptr_t)path < KERNBASE)
+       {
+               size_t len = strlen(path)+1;
+               malloced = kmalloc(len,0);
+               if(!malloced)
+                       goto out;
+               path = memcpy(malloced,path,len);
+       }
+
+       if(!(f = kmem_cache_alloc(struct_file_cache,0)))
+               goto out;
+
+       f->fd = frontend_syscall(0,APPSERVER_SYSCALL_open,PADDR(path),
+                                oflag,mode,0,NULL);
+       if(f->fd == -1)
+       {
+               kmem_cache_free(struct_file_cache,f);
+               f = NULL;
+               goto out;
+       }
+       spinlock_init(&f->lock);
+       f->refcnt = 1;
+
+out:
+       if(malloced)
+               kfree(malloced);
+       return f;
+}
+
+void file_incref(struct file* f)
 {
-       int errno;
-       return frontend_syscall(p->pid,APPSERVER_SYSCALL_open,PADDR(path),
-                               oflag,mode,0,&errno);
+       spin_lock(&f->lock);
+       f->refcnt++;
+       spin_unlock(&f->lock);
 }
 
-error_t close_file(struct proc* p, int fd)
+void file_decref(struct file* f)
 {
-       int errno;
-       return frontend_syscall(p->pid,APPSERVER_SYSCALL_close,fd,0,0,0,&errno);
+       // if you decref too many times, you'll clobber memory :(
+       spin_lock(&f->lock);
+       if(--f->refcnt == 0)
+       {
+               int ret = frontend_syscall(0,APPSERVER_SYSCALL_close,f->fd,0,0,0,NULL);
+               assert(ret == 0);
+               kmem_cache_free(struct_file_cache,f);
+       }
+       else
+               spin_unlock(&f->lock);
 }
 
-int user_frontend_syscall(struct proc* p, int n, int a0, int a1, int a2, int a3)
+int frontend_syscall_errno(struct proc* p, int n, int a0, int a1, int a2, int a3)
 {
        int errno, ret = frontend_syscall(p->pid,n,a0,a1,a2,a3,&errno);
        if(errno && p)
@@ -166,45 +252,14 @@ int32_t frontend_syscall(pid_t pid, int32_t syscall_num,
                ;
 
        ret = magic_mem[1];
-       *errno = magic_mem[2];
+       if(errno)
+               *errno = magic_mem[2];
 
        spin_unlock_irqsave(&lock);
 
        return ret;
 }
 
-int32_t frontend_nbputch(char ch)
-{
-       static spinlock_t putch_lock = SPINLOCK_INITIALIZER;
-       spin_lock_irqsave(&putch_lock);
-
-       int ret = -1;
-       if(magic_mem[8] == 0)
-       {
-               magic_mem[8] = (unsigned int)(unsigned char)ch;
-               ret = 0;
-       }
-
-       spin_unlock_irqsave(&putch_lock);
-       return ret;
-}
-
-int32_t frontend_nbgetch()
-{
-       static spinlock_t getch_lock = SPINLOCK_INITIALIZER;
-       spin_lock_irqsave(&getch_lock);
-
-       int result = -1;
-       if(magic_mem[9]) 
-       {
-               result = magic_mem[9];
-               magic_mem[9] = 0;
-       }
-
-       spin_unlock_irqsave(&getch_lock);
-       return result;
-}
-
 void __diediedie(trapframe_t* tf, uint32_t srcid, uint32_t code, uint32_t a1, uint32_t a2)
 {
        int32_t errno;