vmap: Use {map,unmap}_segment() helpers
[akaros.git] / kern / arch / riscv / env.c
index b5653d2..2b7d32d 100644 (file)
@@ -1,23 +1,13 @@
-#include <arch/trap.h>
+#include <trap.h>
 #include <env.h>
 #include <assert.h>
 #include <arch/arch.h>
 #include <pmap.h>
 
-void
-env_push_ancillary_state(env_t* e)
-{
-       if(e->env_tf.sr & SR_EF)
-               save_fp_state(&e->env_ancillary_state);
-}
-
-void
-save_fp_state(ancillary_state_t* silly)
+void save_fp_state(ancillary_state_t* silly)
 {
-       uintptr_t sr = mfpcr(PCR_SR);
-       mtpcr(PCR_SR, sr | SR_EF);
-
-       asm("mffsr %0" : "=r"(silly->fsr));
+       uintptr_t sr = enable_fp();
+       uint32_t fsr = read_fsr();
 
        asm("fsd f0,%0" : "=m"(silly->fpr[0]));
        asm("fsd f1,%0" : "=m"(silly->fpr[1]));
@@ -52,23 +42,14 @@ save_fp_state(ancillary_state_t* silly)
        asm("fsd f30,%0" : "=m"(silly->fpr[30]));
        asm("fsd f31,%0" : "=m"(silly->fpr[31]));
 
+       silly->fsr = fsr;
        mtpcr(PCR_SR, sr);
 }
 
-void
-env_pop_ancillary_state(env_t* e)
-{ 
-       if(e->env_tf.sr & SR_EF)
-               restore_fp_state(&e->env_ancillary_state);
-}
-
-void
-restore_fp_state(ancillary_state_t* silly)
+void restore_fp_state(ancillary_state_t* silly)
 {
-       uintptr_t sr = mfpcr(PCR_SR);
-       mtpcr(PCR_SR, sr | SR_EF);
-
-       asm("mtfsr %0" : : "r"(silly->fsr));
+       uintptr_t sr = enable_fp();
+       uint32_t fsr = silly->fsr;
 
        asm("fld f0,%0" : : "m"(silly->fpr[0]));
        asm("fld f1,%0" : : "m"(silly->fpr[1]));
@@ -103,6 +84,24 @@ restore_fp_state(ancillary_state_t* silly)
        asm("fld f30,%0" : : "m"(silly->fpr[30]));
        asm("fld f31,%0" : : "m"(silly->fpr[31]));
 
+       write_fsr(fsr);
+       mtpcr(PCR_SR, sr);
+}
+
+void init_fp_state(void)
+{
+       uintptr_t sr = enable_fp();
+
+       asm("fcvt.d.w f0, x0; fcvt.d.w f1 ,x0; fcvt.d.w f2, x0; fcvt.d.w f3, x0;");
+       asm("fcvt.d.w f4, x0; fcvt.d.w f5, x0; fcvt.d.w f6, x0; fcvt.d.w f7, x0;");
+       asm("fcvt.d.w f8, x0; fcvt.d.w f9, x0; fcvt.d.w f10,x0; fcvt.d.w f11,x0;");
+       asm("fcvt.d.w f12,x0; fcvt.d.w f13,x0; fcvt.d.w f14,x0; fcvt.d.w f15,x0;");
+       asm("fcvt.d.w f16,x0; fcvt.d.w f17,x0; fcvt.d.w f18,x0; fcvt.d.w f19,x0;");
+       asm("fcvt.d.w f20,x0; fcvt.d.w f21,x0; fcvt.d.w f22,x0; fcvt.d.w f23,x0;");
+       asm("fcvt.d.w f24,x0; fcvt.d.w f25,x0; fcvt.d.w f26,x0; fcvt.d.w f27,x0;");
+       asm("fcvt.d.w f28,x0; fcvt.d.w f29,x0; fcvt.d.w f30,x0; fcvt.d.w f31,x0;");
+       asm("mtfsr x0");
+
        mtpcr(PCR_SR, sr);
 }
 
@@ -112,6 +111,7 @@ user_mem_walk_recursive(env_t* e, uintptr_t start, size_t len,
                         mem_walk_callback_t pt_callback, void* pt_arg,
                         pte_t* pt, int level)
 {
+       int ret = 0;
        int pgshift = L1PGSHIFT - level*(L1PGSHIFT-L2PGSHIFT);
        uintptr_t pgsize = 1UL << pgshift;
 
@@ -120,33 +120,34 @@ user_mem_walk_recursive(env_t* e, uintptr_t start, size_t len,
 
        for(uintptr_t idx = start_idx; idx <= end_idx; idx++)
        {
-               int ret;
-               uintptr_t pgaddr = ROUNDDOWN(start, pgsize) + idx*pgsize;
+               uintptr_t pgaddr = ROUNDDOWN(start, pgsize) + (idx-start_idx)*pgsize;
                pte_t* pte = &pt[idx];
 
                if(*pte & PTE_T)
                {
                        assert(level < NPTLEVELS-1);
-                       if((ret = user_mem_walk_recursive(e, MAX(pgaddr, start),
-                                                         MIN(pgsize, pgaddr+len),
-                                                         callback, arg,
-                                                                                                                                                               pt_callback, pt_arg,
-                                                                                                                                                               KADDR(PTD_ADDR(*pte)), level+1)))
-                               return ret;
+                       uintptr_t st = MAX(pgaddr, start);
+                       size_t ln = MIN(start + len, pgaddr + pgsize) - st;
+                       if((ret = user_mem_walk_recursive(e, st, ln, callback, arg,
+                                                         pt_callback, pt_arg,
+                                                         KADDR(PTD_ADDR(*pte)), level+1)))
+                               goto out;
                        if(pt_callback != NULL && (ret = pt_callback(e, pte, (void*)pgaddr, arg)))
-                               return ret;
+                               goto out;
                }
-               else if(callback != NULL && !PAGE_UNMAPPED(*pte))
+               else if(callback != NULL)
                        if((ret = callback(e, pte, (void*)pgaddr, arg)))
-                               return ret;
+                               goto out;
        }
-       return 0;
+out:
+       return ret;
 }
 
 int
 env_user_mem_walk(env_t* e, void* start, size_t len,
                   mem_walk_callback_t callback, void* arg)
 {
+       assert(PGOFF(start) == 0 && PGOFF(len) == 0);
        return user_mem_walk_recursive(e, (uintptr_t)start, len, callback, arg,
                                       NULL, NULL, e->env_pgdir, 0);
 }
@@ -156,6 +157,8 @@ env_pagetable_free(env_t* e)
 {
        int pt_free(env_t* e, pte_t* pte, void* va, void* arg)
        {
+               if (!PAGE_PRESENT(pte))
+                       return 0;
                page_decref(pa2page(PTD_ADDR(*pte)));
                return 0;
        }