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