Add proc_global_info (XCC)
[akaros.git] / kern / src / env.c
index 4a3930c..6d2811a 100644 (file)
@@ -1,9 +1,5 @@
 /* See COPYRIGHT for copyright information. */
 
-#ifdef __SHARC__
-#pragma nosharc
-#endif
-
 #include <arch/arch.h>
 #include <arch/mmu.h>
 #include <bitmask.h>
@@ -37,44 +33,14 @@ atomic_t num_envs;
 //     -ENOMEM if page directory or table could not be allocated.
 //
 int env_setup_vm(env_t *e)
-WRITES(e->env_pgdir, e->env_cr3, e->procinfo, e->procdata)
 {
-       int i, r;
-       page_t *pgdir = NULL;
-       static page_t * RO shared_page = 0;
-
-       /* Get a page for the pgdir.  Storing the ref in pgdir/env_pgdir */
-       r = kpage_alloc(&pgdir);
-       if (r < 0)
-               return r;
-
-       /*
-        * Next, set up the e->env_pgdir and e->env_cr3 pointers to point
-        * to this newly allocated page and clear its contents
-        */
-       memset(page2kva(pgdir), 0, PGSIZE);
-       e->env_pgdir = (pde_t *COUNT(NPDENTRIES)) TC(page2kva(pgdir));
-       e->env_cr3 =   (physaddr_t) TC(page2pa(pgdir));
-
-       /*
-        * Now start filling in the pgdir with mappings required by all newly
-        * created address spaces
-        */
+       int i, ret;
+       static page_t *shared_page = 0;
 
-       // Map in the kernel to the top of every address space
-       // should be able to do this so long as boot_pgdir never has
-       // anything put below ULIM
-       // TODO check on this!  had a nasty bug because of it
-       // this is a bit wonky, since if it's not PGSIZE, lots of other things are
-       // screwed up...
-       memcpy(e->env_pgdir, boot_pgdir, NPDENTRIES*sizeof(pde_t));
-
-       // VPT and UVPT map the env's own page table, with
-       // different permissions.
-       #ifndef NOVPT
-       e->env_pgdir[PDX(VPT)]  = PTE(LA2PPN(e->env_cr3), PTE_P | PTE_KERN_RW);
-       e->env_pgdir[PDX(UVPT)] = PTE(LA2PPN(e->env_cr3), PTE_P | PTE_USER_RO);
-       #endif
+       if ((ret = arch_pgdir_setup(boot_pgdir, &e->env_pgdir)))
+               return ret;
+       /* TODO: verify there is nothing below ULIM */
+       e->env_cr3 = arch_pgdir_get_cr3(e->env_pgdir);
 
        /* These need to be contiguous, so the kernel can alias them.  Note the
         * pages return with a refcnt, but it's okay to insert them since we free
@@ -87,12 +53,18 @@ WRITES(e->env_pgdir, e->env_cr3, e->procinfo, e->procdata)
         * start the process without calling those. */
        for (int i = 0; i < PROCINFO_NUM_PAGES; i++) {
                if (page_insert(e->env_pgdir, kva2page((void*)e->procinfo + i *
-                               PGSIZE), (void*SNT)(UINFO + i*PGSIZE), PTE_USER_RO) < 0)
+                               PGSIZE), (void*)(UINFO + i*PGSIZE), PTE_USER_RO) < 0)
                        goto env_setup_vm_error;
        }
        for (int i = 0; i < PROCDATA_NUM_PAGES; i++) {
                if (page_insert(e->env_pgdir, kva2page((void*)e->procdata + i *
-                               PGSIZE), (void*SNT)(UDATA + i*PGSIZE), PTE_USER_RW) < 0)
+                               PGSIZE), (void*)(UDATA + i*PGSIZE), PTE_USER_RW) < 0)
+                       goto env_setup_vm_error;
+       }
+       for (int i = 0; i < PROCGINFO_NUM_PAGES; i++) {
+               if (page_insert(e->env_pgdir,
+                               kva2page((void*)&__proc_global_info + i * PGSIZE),
+                               (void*)(UGINFO + i * PGSIZE), PTE_USER_RO) < 0)
                        goto env_setup_vm_error;
        }
        /* Finally, set up the Global Shared Data page for all processes.  Can't be
@@ -135,19 +107,16 @@ type SLOCKED(name##_lock) *\
 void env_user_mem_free(env_t* e, void* start, size_t len)
 {
        assert((uintptr_t)start + len <= UVPT); //since this keeps fucking happening
-       int user_page_free(env_t* e, pte_t* pte, void* va, void* arg)
+       int user_page_free(env_t* e, pte_t pte, void* va, void* arg)
        {
-               if(PAGE_PRESENT(*pte))
-               {
-                       page_t* page = ppn2page(PTE2PPN(*pte));
-                       *pte = 0;
-                       page_decref(page);
-               } else {
-                       assert(PAGE_PAGED_OUT(*pte));
-                       /* TODO: (SWAP) deal with this */
-                       panic("Swapping not supported!");
-                       *pte = 0;
-               }
+               if (!pte_is_mapped(pte))
+                       return 0;
+               page_t *page = pa2page(pte_get_paddr(pte));
+               pte_clear(pte);
+               page_decref(page);
+               /* TODO: consider other states here (like !P, yet still tracking a page,
+                * for VM tricks, page map stuff, etc.  Should be okay: once we're
+                * freeing, everything else about this proc is dead. */
                return 0;
        }