xref: /qemu/target/sparc/win_helper.c (revision c5f9864e892c473ee3b2cfe080c0def229dac2a7)
1070af384SBlue Swirl /*
2070af384SBlue Swirl  * Helpers for CWP and PSTATE handling
3070af384SBlue Swirl  *
4070af384SBlue Swirl  *  Copyright (c) 2003-2005 Fabrice Bellard
5070af384SBlue Swirl  *
6070af384SBlue Swirl  * This library is free software; you can redistribute it and/or
7070af384SBlue Swirl  * modify it under the terms of the GNU Lesser General Public
8070af384SBlue Swirl  * License as published by the Free Software Foundation; either
9070af384SBlue Swirl  * version 2 of the License, or (at your option) any later version.
10070af384SBlue Swirl  *
11070af384SBlue Swirl  * This library is distributed in the hope that it will be useful,
12070af384SBlue Swirl  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13070af384SBlue Swirl  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14070af384SBlue Swirl  * Lesser General Public License for more details.
15070af384SBlue Swirl  *
16070af384SBlue Swirl  * You should have received a copy of the GNU Lesser General Public
17070af384SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18070af384SBlue Swirl  */
19070af384SBlue Swirl 
20070af384SBlue Swirl #include "cpu.h"
21070af384SBlue Swirl #include "helper.h"
22870be6adSBlue Swirl #include "trace.h"
23070af384SBlue Swirl 
24070af384SBlue Swirl static inline void memcpy32(target_ulong *dst, const target_ulong *src)
25070af384SBlue Swirl {
26070af384SBlue Swirl     dst[0] = src[0];
27070af384SBlue Swirl     dst[1] = src[1];
28070af384SBlue Swirl     dst[2] = src[2];
29070af384SBlue Swirl     dst[3] = src[3];
30070af384SBlue Swirl     dst[4] = src[4];
31070af384SBlue Swirl     dst[5] = src[5];
32070af384SBlue Swirl     dst[6] = src[6];
33070af384SBlue Swirl     dst[7] = src[7];
34070af384SBlue Swirl }
35070af384SBlue Swirl 
36c5f9864eSAndreas Färber void cpu_set_cwp(CPUSPARCState *env, int new_cwp)
37070af384SBlue Swirl {
38070af384SBlue Swirl     /* put the modified wrap registers at their proper location */
39070af384SBlue Swirl     if (env->cwp == env->nwindows - 1) {
40070af384SBlue Swirl         memcpy32(env->regbase, env->regbase + env->nwindows * 16);
41070af384SBlue Swirl     }
42070af384SBlue Swirl     env->cwp = new_cwp;
43070af384SBlue Swirl 
44070af384SBlue Swirl     /* put the wrap registers at their temporary location */
45070af384SBlue Swirl     if (new_cwp == env->nwindows - 1) {
46070af384SBlue Swirl         memcpy32(env->regbase + env->nwindows * 16, env->regbase);
47070af384SBlue Swirl     }
48070af384SBlue Swirl     env->regwptr = env->regbase + (new_cwp * 16);
49070af384SBlue Swirl }
50070af384SBlue Swirl 
51c5f9864eSAndreas Färber target_ulong cpu_get_psr(CPUSPARCState *env)
52070af384SBlue Swirl {
53070af384SBlue Swirl     helper_compute_psr(env);
54070af384SBlue Swirl 
55070af384SBlue Swirl #if !defined(TARGET_SPARC64)
56070af384SBlue Swirl     return env->version | (env->psr & PSR_ICC) |
57070af384SBlue Swirl         (env->psref ? PSR_EF : 0) |
58070af384SBlue Swirl         (env->psrpil << 8) |
59070af384SBlue Swirl         (env->psrs ? PSR_S : 0) |
60070af384SBlue Swirl         (env->psrps ? PSR_PS : 0) |
61070af384SBlue Swirl         (env->psret ? PSR_ET : 0) | env->cwp;
62070af384SBlue Swirl #else
63070af384SBlue Swirl     return env->psr & PSR_ICC;
64070af384SBlue Swirl #endif
65070af384SBlue Swirl }
66070af384SBlue Swirl 
67c5f9864eSAndreas Färber void cpu_put_psr(CPUSPARCState *env, target_ulong val)
68070af384SBlue Swirl {
69070af384SBlue Swirl     env->psr = val & PSR_ICC;
70070af384SBlue Swirl #if !defined(TARGET_SPARC64)
71070af384SBlue Swirl     env->psref = (val & PSR_EF) ? 1 : 0;
72070af384SBlue Swirl     env->psrpil = (val & PSR_PIL) >> 8;
73070af384SBlue Swirl #endif
74070af384SBlue Swirl #if ((!defined(TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY))
75070af384SBlue Swirl     cpu_check_irqs(env);
76070af384SBlue Swirl #endif
77070af384SBlue Swirl #if !defined(TARGET_SPARC64)
78070af384SBlue Swirl     env->psrs = (val & PSR_S) ? 1 : 0;
79070af384SBlue Swirl     env->psrps = (val & PSR_PS) ? 1 : 0;
80070af384SBlue Swirl     env->psret = (val & PSR_ET) ? 1 : 0;
81063c3675SBlue Swirl     cpu_set_cwp(env, val & PSR_CWP);
82070af384SBlue Swirl #endif
83070af384SBlue Swirl     env->cc_op = CC_OP_FLAGS;
84070af384SBlue Swirl }
85070af384SBlue Swirl 
86c5f9864eSAndreas Färber int cpu_cwp_inc(CPUSPARCState *env, int cwp)
87070af384SBlue Swirl {
88070af384SBlue Swirl     if (unlikely(cwp >= env->nwindows)) {
89070af384SBlue Swirl         cwp -= env->nwindows;
90070af384SBlue Swirl     }
91070af384SBlue Swirl     return cwp;
92070af384SBlue Swirl }
93070af384SBlue Swirl 
94c5f9864eSAndreas Färber int cpu_cwp_dec(CPUSPARCState *env, int cwp)
95070af384SBlue Swirl {
96070af384SBlue Swirl     if (unlikely(cwp < 0)) {
97070af384SBlue Swirl         cwp += env->nwindows;
98070af384SBlue Swirl     }
99070af384SBlue Swirl     return cwp;
100070af384SBlue Swirl }
101070af384SBlue Swirl 
102070af384SBlue Swirl #ifndef TARGET_SPARC64
103c5f9864eSAndreas Färber void helper_rett(CPUSPARCState *env)
104070af384SBlue Swirl {
105070af384SBlue Swirl     unsigned int cwp;
106070af384SBlue Swirl 
107070af384SBlue Swirl     if (env->psret == 1) {
108070af384SBlue Swirl         helper_raise_exception(env, TT_ILL_INSN);
109070af384SBlue Swirl     }
110070af384SBlue Swirl 
111070af384SBlue Swirl     env->psret = 1;
112063c3675SBlue Swirl     cwp = cpu_cwp_inc(env, env->cwp + 1) ;
113070af384SBlue Swirl     if (env->wim & (1 << cwp)) {
114070af384SBlue Swirl         helper_raise_exception(env, TT_WIN_UNF);
115070af384SBlue Swirl     }
116063c3675SBlue Swirl     cpu_set_cwp(env, cwp);
117070af384SBlue Swirl     env->psrs = env->psrps;
118070af384SBlue Swirl }
119070af384SBlue Swirl 
120070af384SBlue Swirl /* XXX: use another pointer for %iN registers to avoid slow wrapping
121070af384SBlue Swirl    handling ? */
122c5f9864eSAndreas Färber void helper_save(CPUSPARCState *env)
123070af384SBlue Swirl {
124070af384SBlue Swirl     uint32_t cwp;
125070af384SBlue Swirl 
126063c3675SBlue Swirl     cwp = cpu_cwp_dec(env, env->cwp - 1);
127070af384SBlue Swirl     if (env->wim & (1 << cwp)) {
128070af384SBlue Swirl         helper_raise_exception(env, TT_WIN_OVF);
129070af384SBlue Swirl     }
130063c3675SBlue Swirl     cpu_set_cwp(env, cwp);
131070af384SBlue Swirl }
132070af384SBlue Swirl 
133c5f9864eSAndreas Färber void helper_restore(CPUSPARCState *env)
134070af384SBlue Swirl {
135070af384SBlue Swirl     uint32_t cwp;
136070af384SBlue Swirl 
137063c3675SBlue Swirl     cwp = cpu_cwp_inc(env, env->cwp + 1);
138070af384SBlue Swirl     if (env->wim & (1 << cwp)) {
139070af384SBlue Swirl         helper_raise_exception(env, TT_WIN_UNF);
140070af384SBlue Swirl     }
141063c3675SBlue Swirl     cpu_set_cwp(env, cwp);
142070af384SBlue Swirl }
143070af384SBlue Swirl 
144c5f9864eSAndreas Färber void helper_wrpsr(CPUSPARCState *env, target_ulong new_psr)
145070af384SBlue Swirl {
146070af384SBlue Swirl     if ((new_psr & PSR_CWP) >= env->nwindows) {
147070af384SBlue Swirl         helper_raise_exception(env, TT_ILL_INSN);
148070af384SBlue Swirl     } else {
149070af384SBlue Swirl         cpu_put_psr(env, new_psr);
150070af384SBlue Swirl     }
151070af384SBlue Swirl }
152070af384SBlue Swirl 
153c5f9864eSAndreas Färber target_ulong helper_rdpsr(CPUSPARCState *env)
154070af384SBlue Swirl {
155063c3675SBlue Swirl     return cpu_get_psr(env);
156070af384SBlue Swirl }
157070af384SBlue Swirl 
158070af384SBlue Swirl #else
159070af384SBlue Swirl /* XXX: use another pointer for %iN registers to avoid slow wrapping
160070af384SBlue Swirl    handling ? */
161c5f9864eSAndreas Färber void helper_save(CPUSPARCState *env)
162070af384SBlue Swirl {
163070af384SBlue Swirl     uint32_t cwp;
164070af384SBlue Swirl 
165063c3675SBlue Swirl     cwp = cpu_cwp_dec(env, env->cwp - 1);
166070af384SBlue Swirl     if (env->cansave == 0) {
167070af384SBlue Swirl         helper_raise_exception(env, TT_SPILL | (env->otherwin != 0 ?
168070af384SBlue Swirl                                                 (TT_WOTHER |
169070af384SBlue Swirl                                                  ((env->wstate & 0x38) >> 1)) :
170070af384SBlue Swirl                                                 ((env->wstate & 0x7) << 2)));
171070af384SBlue Swirl     } else {
172070af384SBlue Swirl         if (env->cleanwin - env->canrestore == 0) {
173070af384SBlue Swirl             /* XXX Clean windows without trap */
174070af384SBlue Swirl             helper_raise_exception(env, TT_CLRWIN);
175070af384SBlue Swirl         } else {
176070af384SBlue Swirl             env->cansave--;
177070af384SBlue Swirl             env->canrestore++;
178063c3675SBlue Swirl             cpu_set_cwp(env, cwp);
179070af384SBlue Swirl         }
180070af384SBlue Swirl     }
181070af384SBlue Swirl }
182070af384SBlue Swirl 
183c5f9864eSAndreas Färber void helper_restore(CPUSPARCState *env)
184070af384SBlue Swirl {
185070af384SBlue Swirl     uint32_t cwp;
186070af384SBlue Swirl 
187063c3675SBlue Swirl     cwp = cpu_cwp_inc(env, env->cwp + 1);
188070af384SBlue Swirl     if (env->canrestore == 0) {
189070af384SBlue Swirl         helper_raise_exception(env, TT_FILL | (env->otherwin != 0 ?
190070af384SBlue Swirl                                                (TT_WOTHER |
191070af384SBlue Swirl                                                 ((env->wstate & 0x38) >> 1)) :
192070af384SBlue Swirl                                                ((env->wstate & 0x7) << 2)));
193070af384SBlue Swirl     } else {
194070af384SBlue Swirl         env->cansave++;
195070af384SBlue Swirl         env->canrestore--;
196063c3675SBlue Swirl         cpu_set_cwp(env, cwp);
197070af384SBlue Swirl     }
198070af384SBlue Swirl }
199070af384SBlue Swirl 
200c5f9864eSAndreas Färber void helper_flushw(CPUSPARCState *env)
201070af384SBlue Swirl {
202070af384SBlue Swirl     if (env->cansave != env->nwindows - 2) {
203070af384SBlue Swirl         helper_raise_exception(env, TT_SPILL | (env->otherwin != 0 ?
204070af384SBlue Swirl                                                 (TT_WOTHER |
205070af384SBlue Swirl                                                  ((env->wstate & 0x38) >> 1)) :
206070af384SBlue Swirl                                                 ((env->wstate & 0x7) << 2)));
207070af384SBlue Swirl     }
208070af384SBlue Swirl }
209070af384SBlue Swirl 
210c5f9864eSAndreas Färber void helper_saved(CPUSPARCState *env)
211070af384SBlue Swirl {
212070af384SBlue Swirl     env->cansave++;
213070af384SBlue Swirl     if (env->otherwin == 0) {
214070af384SBlue Swirl         env->canrestore--;
215070af384SBlue Swirl     } else {
216070af384SBlue Swirl         env->otherwin--;
217070af384SBlue Swirl     }
218070af384SBlue Swirl }
219070af384SBlue Swirl 
220c5f9864eSAndreas Färber void helper_restored(CPUSPARCState *env)
221070af384SBlue Swirl {
222070af384SBlue Swirl     env->canrestore++;
223070af384SBlue Swirl     if (env->cleanwin < env->nwindows - 1) {
224070af384SBlue Swirl         env->cleanwin++;
225070af384SBlue Swirl     }
226070af384SBlue Swirl     if (env->otherwin == 0) {
227070af384SBlue Swirl         env->cansave--;
228070af384SBlue Swirl     } else {
229070af384SBlue Swirl         env->otherwin--;
230070af384SBlue Swirl     }
231070af384SBlue Swirl }
232070af384SBlue Swirl 
233c5f9864eSAndreas Färber target_ulong cpu_get_ccr(CPUSPARCState *env)
234070af384SBlue Swirl {
235070af384SBlue Swirl     target_ulong psr;
236070af384SBlue Swirl 
237063c3675SBlue Swirl     psr = cpu_get_psr(env);
238070af384SBlue Swirl 
239070af384SBlue Swirl     return ((env->xcc >> 20) << 4) | ((psr & PSR_ICC) >> 20);
240070af384SBlue Swirl }
241070af384SBlue Swirl 
242c5f9864eSAndreas Färber void cpu_put_ccr(CPUSPARCState *env, target_ulong val)
243070af384SBlue Swirl {
244070af384SBlue Swirl     env->xcc = (val >> 4) << 20;
245070af384SBlue Swirl     env->psr = (val & 0xf) << 20;
246070af384SBlue Swirl     CC_OP = CC_OP_FLAGS;
247070af384SBlue Swirl }
248070af384SBlue Swirl 
249c5f9864eSAndreas Färber target_ulong cpu_get_cwp64(CPUSPARCState *env)
250070af384SBlue Swirl {
251070af384SBlue Swirl     return env->nwindows - 1 - env->cwp;
252070af384SBlue Swirl }
253070af384SBlue Swirl 
254c5f9864eSAndreas Färber void cpu_put_cwp64(CPUSPARCState *env, int cwp)
255070af384SBlue Swirl {
256070af384SBlue Swirl     if (unlikely(cwp >= env->nwindows || cwp < 0)) {
257070af384SBlue Swirl         cwp %= env->nwindows;
258070af384SBlue Swirl     }
259063c3675SBlue Swirl     cpu_set_cwp(env, env->nwindows - 1 - cwp);
260070af384SBlue Swirl }
261070af384SBlue Swirl 
262c5f9864eSAndreas Färber target_ulong helper_rdccr(CPUSPARCState *env)
263070af384SBlue Swirl {
264063c3675SBlue Swirl     return cpu_get_ccr(env);
265070af384SBlue Swirl }
266070af384SBlue Swirl 
267c5f9864eSAndreas Färber void helper_wrccr(CPUSPARCState *env, target_ulong new_ccr)
268070af384SBlue Swirl {
269063c3675SBlue Swirl     cpu_put_ccr(env, new_ccr);
270070af384SBlue Swirl }
271070af384SBlue Swirl 
272070af384SBlue Swirl /* CWP handling is reversed in V9, but we still use the V8 register
273070af384SBlue Swirl    order. */
274c5f9864eSAndreas Färber target_ulong helper_rdcwp(CPUSPARCState *env)
275070af384SBlue Swirl {
276063c3675SBlue Swirl     return cpu_get_cwp64(env);
277070af384SBlue Swirl }
278070af384SBlue Swirl 
279c5f9864eSAndreas Färber void helper_wrcwp(CPUSPARCState *env, target_ulong new_cwp)
280070af384SBlue Swirl {
281063c3675SBlue Swirl     cpu_put_cwp64(env, new_cwp);
282070af384SBlue Swirl }
283070af384SBlue Swirl 
284c5f9864eSAndreas Färber static inline uint64_t *get_gregset(CPUSPARCState *env, uint32_t pstate)
285070af384SBlue Swirl {
286070af384SBlue Swirl     switch (pstate) {
287070af384SBlue Swirl     default:
288870be6adSBlue Swirl         trace_win_helper_gregset_error(pstate);
289070af384SBlue Swirl         /* pass through to normal set of global registers */
290070af384SBlue Swirl     case 0:
291070af384SBlue Swirl         return env->bgregs;
292070af384SBlue Swirl     case PS_AG:
293070af384SBlue Swirl         return env->agregs;
294070af384SBlue Swirl     case PS_MG:
295070af384SBlue Swirl         return env->mgregs;
296070af384SBlue Swirl     case PS_IG:
297070af384SBlue Swirl         return env->igregs;
298070af384SBlue Swirl     }
299070af384SBlue Swirl }
300070af384SBlue Swirl 
301c5f9864eSAndreas Färber void cpu_change_pstate(CPUSPARCState *env, uint32_t new_pstate)
302070af384SBlue Swirl {
303070af384SBlue Swirl     uint32_t pstate_regs, new_pstate_regs;
304070af384SBlue Swirl     uint64_t *src, *dst;
305070af384SBlue Swirl 
306070af384SBlue Swirl     if (env->def->features & CPU_FEATURE_GL) {
307070af384SBlue Swirl         /* PS_AG is not implemented in this case */
308070af384SBlue Swirl         new_pstate &= ~PS_AG;
309070af384SBlue Swirl     }
310070af384SBlue Swirl 
311070af384SBlue Swirl     pstate_regs = env->pstate & 0xc01;
312070af384SBlue Swirl     new_pstate_regs = new_pstate & 0xc01;
313070af384SBlue Swirl 
314070af384SBlue Swirl     if (new_pstate_regs != pstate_regs) {
315870be6adSBlue Swirl         trace_win_helper_switch_pstate(pstate_regs, new_pstate_regs);
316870be6adSBlue Swirl 
317070af384SBlue Swirl         /* Switch global register bank */
318063c3675SBlue Swirl         src = get_gregset(env, new_pstate_regs);
319063c3675SBlue Swirl         dst = get_gregset(env, pstate_regs);
320070af384SBlue Swirl         memcpy32(dst, env->gregs);
321070af384SBlue Swirl         memcpy32(env->gregs, src);
322070af384SBlue Swirl     } else {
323870be6adSBlue Swirl         trace_win_helper_no_switch_pstate(new_pstate_regs);
324070af384SBlue Swirl     }
325070af384SBlue Swirl     env->pstate = new_pstate;
326070af384SBlue Swirl }
327070af384SBlue Swirl 
328c5f9864eSAndreas Färber void helper_wrpstate(CPUSPARCState *env, target_ulong new_state)
329070af384SBlue Swirl {
330063c3675SBlue Swirl     cpu_change_pstate(env, new_state & 0xf3f);
331070af384SBlue Swirl 
332070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY)
333070af384SBlue Swirl     if (cpu_interrupts_enabled(env)) {
334070af384SBlue Swirl         cpu_check_irqs(env);
335070af384SBlue Swirl     }
336070af384SBlue Swirl #endif
337070af384SBlue Swirl }
338070af384SBlue Swirl 
339c5f9864eSAndreas Färber void helper_wrpil(CPUSPARCState *env, target_ulong new_pil)
340070af384SBlue Swirl {
341070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY)
342870be6adSBlue Swirl     trace_win_helper_wrpil(env->psrpil, (uint32_t)new_pil);
343070af384SBlue Swirl 
344070af384SBlue Swirl     env->psrpil = new_pil;
345070af384SBlue Swirl 
346070af384SBlue Swirl     if (cpu_interrupts_enabled(env)) {
347070af384SBlue Swirl         cpu_check_irqs(env);
348070af384SBlue Swirl     }
349070af384SBlue Swirl #endif
350070af384SBlue Swirl }
351070af384SBlue Swirl 
352c5f9864eSAndreas Färber void helper_done(CPUSPARCState *env)
353070af384SBlue Swirl {
354070af384SBlue Swirl     trap_state *tsptr = cpu_tsptr(env);
355070af384SBlue Swirl 
356070af384SBlue Swirl     env->pc = tsptr->tnpc;
357070af384SBlue Swirl     env->npc = tsptr->tnpc + 4;
358063c3675SBlue Swirl     cpu_put_ccr(env, tsptr->tstate >> 32);
359070af384SBlue Swirl     env->asi = (tsptr->tstate >> 24) & 0xff;
360063c3675SBlue Swirl     cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
361063c3675SBlue Swirl     cpu_put_cwp64(env, tsptr->tstate & 0xff);
362070af384SBlue Swirl     env->tl--;
363070af384SBlue Swirl 
364870be6adSBlue Swirl     trace_win_helper_done(env->tl);
365070af384SBlue Swirl 
366070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY)
367070af384SBlue Swirl     if (cpu_interrupts_enabled(env)) {
368070af384SBlue Swirl         cpu_check_irqs(env);
369070af384SBlue Swirl     }
370070af384SBlue Swirl #endif
371070af384SBlue Swirl }
372070af384SBlue Swirl 
373c5f9864eSAndreas Färber void helper_retry(CPUSPARCState *env)
374070af384SBlue Swirl {
375070af384SBlue Swirl     trap_state *tsptr = cpu_tsptr(env);
376070af384SBlue Swirl 
377070af384SBlue Swirl     env->pc = tsptr->tpc;
378070af384SBlue Swirl     env->npc = tsptr->tnpc;
379063c3675SBlue Swirl     cpu_put_ccr(env, tsptr->tstate >> 32);
380070af384SBlue Swirl     env->asi = (tsptr->tstate >> 24) & 0xff;
381063c3675SBlue Swirl     cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
382063c3675SBlue Swirl     cpu_put_cwp64(env, tsptr->tstate & 0xff);
383070af384SBlue Swirl     env->tl--;
384070af384SBlue Swirl 
385870be6adSBlue Swirl     trace_win_helper_retry(env->tl);
386070af384SBlue Swirl 
387070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY)
388070af384SBlue Swirl     if (cpu_interrupts_enabled(env)) {
389070af384SBlue Swirl         cpu_check_irqs(env);
390070af384SBlue Swirl     }
391070af384SBlue Swirl #endif
392070af384SBlue Swirl }
393070af384SBlue Swirl #endif
394