Prototype of env_user_mem_walk
[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
96 typedef void (*env_user_mem_walk_t)(env_t* e, void* va, pte_t* pte, void* arg);
97
98 void
99 env_user_mem_walk(env_t* e, env_user_mem_walk_t callback, void* arg)
100 {
101         pte_t *l1pt = e->env_pgdir, *l2pt, *l3pt;
102         uint32_t l1x,l2x,l3x;
103         physaddr_t l2ptpa,l3ptpa,page_pa;
104         uint32_t l2_tables_per_page,l3_tables_per_page;
105
106         l2_tables_per_page = PGSIZE/(sizeof(pte_t)*NL2ENTRIES);
107         l3_tables_per_page = PGSIZE/(sizeof(pte_t)*NL3ENTRIES);
108
109         static_assert(L2X(KERNBASE) == 0 && L3X(KERNBASE) == 0);
110         for(l1x = 0; l1x < L1X(KERNBASE); l1x++)
111         {
112                 if(!(l1pt[l1x] & PTE_PTD))
113                         continue;
114
115                 l2ptpa = PTD_ADDR(l1pt[l1x]);
116                 l2pt = (pte_t*COUNT(NL2ENTRIES)) KADDR(l2ptpa);
117
118                 for(l2x = 0; l2x < NL2ENTRIES; l2x++)
119                 {
120                         if(!(l2pt[l2x] & PTE_PTD))
121                                 continue;
122
123                         l3ptpa = PTD_ADDR(l2pt[l2x]);
124                         l3pt = (pte_t*COUNT(NL3ENTRIES)) KADDR(l3ptpa);
125
126                         for(l3x = 0; l3x < NL3ENTRIES; l3x++)
127                         {
128                                 if(l3pt[l3x] & PTE_PTE)
129                                 {
130                                         callback(e,PGADDR(l1x,l2x,l3x,0),
131                                                  &l3pt[l3x],arg);
132                                 }
133                         }
134
135                         l2pt[l2x] = 0;
136
137                         // free the L3 PT itself
138                         page_decref(pa2page(l3ptpa));
139                 }
140
141                 l1pt[l1x] = 0;
142
143                 // free the L2 PT itself
144                 page_decref(pa2page(l2ptpa));
145         }
146
147         tlbflush();
148 }
149
150 void
151 env_user_mem_free(env_t* e)
152 {
153         void mem_free_callback(env_t* e, void* va, pte_t* pte, void* arg)
154         {
155                 page_t* page = ppn2page(PTE2PPN(*pte));
156                 page_decref(page);
157                 *pte = 0;
158         }
159         env_user_mem_walk(e,&mem_free_callback,NULL);
160 }
161