3432eda47a6d340cde0a3ff1b01f9500ca290180
[akaros.git] / kern / arch / sparc / env.c
1 /* See COPYRIGHT for copyright information. */
2 #ifdef __SHARC__
3 #pragma nosharc
4 #endif
5
6 #ifdef __DEPUTY__
7 #pragma noasync
8 #endif
9
10 #include <arch/trap.h>
11 #include <env.h>
12 #include <assert.h>
13 #include <arch/arch.h>
14 #include <pmap.h>
15
16 void
17 ( env_push_ancillary_state)(env_t* e)
18 {
19         if(e->env_tf.psr & PSR_EF)
20                 save_fp_state(&e->env_ancillary_state);
21 }
22
23 void
24 save_fp_state(ancillary_state_t* silly)
25 {
26         #define push_two_fp_regs(pdest,n) \
27             __asm__ __volatile__ ("std  %%f" XSTR(n) ",[%0+4*" XSTR(n) "]" \
28                               : : "r"(pdest) : "memory");
29
30         write_psr(read_psr() | PSR_EF);
31
32         silly->fsr = read_fsr();
33
34         push_two_fp_regs(silly->fpr,0);
35         push_two_fp_regs(silly->fpr,2);
36         push_two_fp_regs(silly->fpr,4);
37         push_two_fp_regs(silly->fpr,6);
38         push_two_fp_regs(silly->fpr,8);
39         push_two_fp_regs(silly->fpr,10);
40         push_two_fp_regs(silly->fpr,12);
41         push_two_fp_regs(silly->fpr,14);
42         push_two_fp_regs(silly->fpr,16);
43         push_two_fp_regs(silly->fpr,18);
44         push_two_fp_regs(silly->fpr,20);
45         push_two_fp_regs(silly->fpr,22);
46         push_two_fp_regs(silly->fpr,24);
47         push_two_fp_regs(silly->fpr,26);
48         push_two_fp_regs(silly->fpr,28);
49         push_two_fp_regs(silly->fpr,30);
50
51         write_psr(read_psr() & ~PSR_EF);
52 }
53
54 void
55 ( env_pop_ancillary_state)(env_t* e)
56
57         if(e->env_tf.psr & PSR_EF)
58                 restore_fp_state(&e->env_ancillary_state);
59 }
60
61 void
62 restore_fp_state(ancillary_state_t* silly)
63 {
64         #define pop_two_fp_regs(pdest,n) \
65             __asm__ __volatile__ ("ldd  [%0+4*" XSTR(n) "], %%f" XSTR(n) \
66                               : : "r"(pdest) : "memory");
67
68         write_psr(read_psr() | PSR_EF);
69
70         pop_two_fp_regs(silly->fpr,0);
71         pop_two_fp_regs(silly->fpr,2);
72         pop_two_fp_regs(silly->fpr,4);
73         pop_two_fp_regs(silly->fpr,6);
74         pop_two_fp_regs(silly->fpr,8);
75         pop_two_fp_regs(silly->fpr,10);
76         pop_two_fp_regs(silly->fpr,12);
77         pop_two_fp_regs(silly->fpr,14);
78         pop_two_fp_regs(silly->fpr,16);
79         pop_two_fp_regs(silly->fpr,18);
80         pop_two_fp_regs(silly->fpr,20);
81         pop_two_fp_regs(silly->fpr,22);
82         pop_two_fp_regs(silly->fpr,24);
83         pop_two_fp_regs(silly->fpr,26);
84         pop_two_fp_regs(silly->fpr,28);
85         pop_two_fp_regs(silly->fpr,30);
86
87         write_fsr(silly->fsr);
88
89         write_psr(read_psr() & ~PSR_EF);
90 }
91
92
93 // Flush all mapped pages in the user portion of the address space
94 // TODO: only supports L3 user pages
95 void
96 env_user_mem_free(env_t* e)
97 {
98         pte_t *l1pt = e->env_pgdir, *l2pt, *l3pt;
99         uint32_t l1x,l2x,l3x;
100         physaddr_t l2ptpa,l3ptpa,page_pa;
101         uint32_t l2_tables_per_page,l3_tables_per_page;
102
103         l2_tables_per_page = PGSIZE/(sizeof(pte_t)*NL2ENTRIES);
104         l3_tables_per_page = PGSIZE/(sizeof(pte_t)*NL3ENTRIES);
105
106         static_assert(L2X(KERNBASE) == 0 && L3X(KERNBASE) == 0);
107         for(l1x = 0; l1x < L1X(KERNBASE); l1x++)
108         {
109                 if(!(l1pt[l1x] & PTE_PTD))
110                         continue;
111
112                 l2ptpa = PTD_ADDR(l1pt[l1x]);
113                 l2pt = (pte_t*COUNT(NL2ENTRIES)) KADDR(l2ptpa);
114
115                 for(l2x = 0; l2x < NL2ENTRIES; l2x++)
116                 {
117                         if(!(l2pt[l2x] & PTE_PTD))
118                                 continue;
119
120                         l3ptpa = PTD_ADDR(l2pt[l2x]);
121                         l3pt = (pte_t*COUNT(NL3ENTRIES)) KADDR(l3ptpa);
122
123                         for(l3x = 0; l3x < NL3ENTRIES; l3x++)
124                         {
125                                 if(l3pt[l3x] & PTE_PTE)
126                                 {
127                                         page_pa = PTE_ADDR(l3pt[l3x]);
128                                         l3pt[l3x] = 0;
129                                         page_decref(pa2page(page_pa));
130                                 }
131                         }
132
133                         l2pt[l2x] = 0;
134
135                         // free the L3 PT itself
136                         page_decref(pa2page(l3ptpa));
137                 }
138
139                 l1pt[l1x] = 0;
140
141                 // free the L2 PT itself
142                 page_decref(pa2page(l2ptpa));
143         }
144
145         tlbflush();
146 }