target-arm: A64: Add SIMD across-lanes instructions
[qemu/kevin.git] / target-sparc / win_helper.c
blob3e82eb71d6a129f72a0d397b0c7cdc30c4714db2
1 /*
2 * Helpers for CWP and PSTATE handling
4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "cpu.h"
21 #include "helper.h"
22 #include "trace.h"
24 static inline void memcpy32(target_ulong *dst, const target_ulong *src)
26 dst[0] = src[0];
27 dst[1] = src[1];
28 dst[2] = src[2];
29 dst[3] = src[3];
30 dst[4] = src[4];
31 dst[5] = src[5];
32 dst[6] = src[6];
33 dst[7] = src[7];
36 void cpu_set_cwp(CPUSPARCState *env, int new_cwp)
38 /* put the modified wrap registers at their proper location */
39 if (env->cwp == env->nwindows - 1) {
40 memcpy32(env->regbase, env->regbase + env->nwindows * 16);
42 env->cwp = new_cwp;
44 /* put the wrap registers at their temporary location */
45 if (new_cwp == env->nwindows - 1) {
46 memcpy32(env->regbase + env->nwindows * 16, env->regbase);
48 env->regwptr = env->regbase + (new_cwp * 16);
51 target_ulong cpu_get_psr(CPUSPARCState *env)
53 helper_compute_psr(env);
55 #if !defined(TARGET_SPARC64)
56 return env->version | (env->psr & PSR_ICC) |
57 (env->psref ? PSR_EF : 0) |
58 (env->psrpil << 8) |
59 (env->psrs ? PSR_S : 0) |
60 (env->psrps ? PSR_PS : 0) |
61 (env->psret ? PSR_ET : 0) | env->cwp;
62 #else
63 return env->psr & PSR_ICC;
64 #endif
67 void cpu_put_psr(CPUSPARCState *env, target_ulong val)
69 env->psr = val & PSR_ICC;
70 #if !defined(TARGET_SPARC64)
71 env->psref = (val & PSR_EF) ? 1 : 0;
72 env->psrpil = (val & PSR_PIL) >> 8;
73 #endif
74 #if ((!defined(TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY))
75 cpu_check_irqs(env);
76 #endif
77 #if !defined(TARGET_SPARC64)
78 env->psrs = (val & PSR_S) ? 1 : 0;
79 env->psrps = (val & PSR_PS) ? 1 : 0;
80 env->psret = (val & PSR_ET) ? 1 : 0;
81 cpu_set_cwp(env, val & PSR_CWP);
82 #endif
83 env->cc_op = CC_OP_FLAGS;
86 int cpu_cwp_inc(CPUSPARCState *env, int cwp)
88 if (unlikely(cwp >= env->nwindows)) {
89 cwp -= env->nwindows;
91 return cwp;
94 int cpu_cwp_dec(CPUSPARCState *env, int cwp)
96 if (unlikely(cwp < 0)) {
97 cwp += env->nwindows;
99 return cwp;
102 #ifndef TARGET_SPARC64
103 void helper_rett(CPUSPARCState *env)
105 unsigned int cwp;
107 if (env->psret == 1) {
108 helper_raise_exception(env, TT_ILL_INSN);
111 env->psret = 1;
112 cwp = cpu_cwp_inc(env, env->cwp + 1) ;
113 if (env->wim & (1 << cwp)) {
114 helper_raise_exception(env, TT_WIN_UNF);
116 cpu_set_cwp(env, cwp);
117 env->psrs = env->psrps;
120 /* XXX: use another pointer for %iN registers to avoid slow wrapping
121 handling ? */
122 void helper_save(CPUSPARCState *env)
124 uint32_t cwp;
126 cwp = cpu_cwp_dec(env, env->cwp - 1);
127 if (env->wim & (1 << cwp)) {
128 helper_raise_exception(env, TT_WIN_OVF);
130 cpu_set_cwp(env, cwp);
133 void helper_restore(CPUSPARCState *env)
135 uint32_t cwp;
137 cwp = cpu_cwp_inc(env, env->cwp + 1);
138 if (env->wim & (1 << cwp)) {
139 helper_raise_exception(env, TT_WIN_UNF);
141 cpu_set_cwp(env, cwp);
144 void helper_wrpsr(CPUSPARCState *env, target_ulong new_psr)
146 if ((new_psr & PSR_CWP) >= env->nwindows) {
147 helper_raise_exception(env, TT_ILL_INSN);
148 } else {
149 cpu_put_psr(env, new_psr);
153 target_ulong helper_rdpsr(CPUSPARCState *env)
155 return cpu_get_psr(env);
158 #else
159 /* XXX: use another pointer for %iN registers to avoid slow wrapping
160 handling ? */
161 void helper_save(CPUSPARCState *env)
163 uint32_t cwp;
165 cwp = cpu_cwp_dec(env, env->cwp - 1);
166 if (env->cansave == 0) {
167 helper_raise_exception(env, TT_SPILL | (env->otherwin != 0 ?
168 (TT_WOTHER |
169 ((env->wstate & 0x38) >> 1)) :
170 ((env->wstate & 0x7) << 2)));
171 } else {
172 if (env->cleanwin - env->canrestore == 0) {
173 /* XXX Clean windows without trap */
174 helper_raise_exception(env, TT_CLRWIN);
175 } else {
176 env->cansave--;
177 env->canrestore++;
178 cpu_set_cwp(env, cwp);
183 void helper_restore(CPUSPARCState *env)
185 uint32_t cwp;
187 cwp = cpu_cwp_inc(env, env->cwp + 1);
188 if (env->canrestore == 0) {
189 helper_raise_exception(env, TT_FILL | (env->otherwin != 0 ?
190 (TT_WOTHER |
191 ((env->wstate & 0x38) >> 1)) :
192 ((env->wstate & 0x7) << 2)));
193 } else {
194 env->cansave++;
195 env->canrestore--;
196 cpu_set_cwp(env, cwp);
200 void helper_flushw(CPUSPARCState *env)
202 if (env->cansave != env->nwindows - 2) {
203 helper_raise_exception(env, TT_SPILL | (env->otherwin != 0 ?
204 (TT_WOTHER |
205 ((env->wstate & 0x38) >> 1)) :
206 ((env->wstate & 0x7) << 2)));
210 void helper_saved(CPUSPARCState *env)
212 env->cansave++;
213 if (env->otherwin == 0) {
214 env->canrestore--;
215 } else {
216 env->otherwin--;
220 void helper_restored(CPUSPARCState *env)
222 env->canrestore++;
223 if (env->cleanwin < env->nwindows - 1) {
224 env->cleanwin++;
226 if (env->otherwin == 0) {
227 env->cansave--;
228 } else {
229 env->otherwin--;
233 target_ulong cpu_get_ccr(CPUSPARCState *env)
235 target_ulong psr;
237 psr = cpu_get_psr(env);
239 return ((env->xcc >> 20) << 4) | ((psr & PSR_ICC) >> 20);
242 void cpu_put_ccr(CPUSPARCState *env, target_ulong val)
244 env->xcc = (val >> 4) << 20;
245 env->psr = (val & 0xf) << 20;
246 CC_OP = CC_OP_FLAGS;
249 target_ulong cpu_get_cwp64(CPUSPARCState *env)
251 return env->nwindows - 1 - env->cwp;
254 void cpu_put_cwp64(CPUSPARCState *env, int cwp)
256 if (unlikely(cwp >= env->nwindows || cwp < 0)) {
257 cwp %= env->nwindows;
259 cpu_set_cwp(env, env->nwindows - 1 - cwp);
262 target_ulong helper_rdccr(CPUSPARCState *env)
264 return cpu_get_ccr(env);
267 void helper_wrccr(CPUSPARCState *env, target_ulong new_ccr)
269 cpu_put_ccr(env, new_ccr);
272 /* CWP handling is reversed in V9, but we still use the V8 register
273 order. */
274 target_ulong helper_rdcwp(CPUSPARCState *env)
276 return cpu_get_cwp64(env);
279 void helper_wrcwp(CPUSPARCState *env, target_ulong new_cwp)
281 cpu_put_cwp64(env, new_cwp);
284 static inline uint64_t *get_gregset(CPUSPARCState *env, uint32_t pstate)
286 switch (pstate) {
287 default:
288 trace_win_helper_gregset_error(pstate);
289 /* pass through to normal set of global registers */
290 case 0:
291 return env->bgregs;
292 case PS_AG:
293 return env->agregs;
294 case PS_MG:
295 return env->mgregs;
296 case PS_IG:
297 return env->igregs;
301 void cpu_change_pstate(CPUSPARCState *env, uint32_t new_pstate)
303 uint32_t pstate_regs, new_pstate_regs;
304 uint64_t *src, *dst;
306 if (env->def->features & CPU_FEATURE_GL) {
307 /* PS_AG is not implemented in this case */
308 new_pstate &= ~PS_AG;
311 pstate_regs = env->pstate & 0xc01;
312 new_pstate_regs = new_pstate & 0xc01;
314 if (new_pstate_regs != pstate_regs) {
315 trace_win_helper_switch_pstate(pstate_regs, new_pstate_regs);
317 /* Switch global register bank */
318 src = get_gregset(env, new_pstate_regs);
319 dst = get_gregset(env, pstate_regs);
320 memcpy32(dst, env->gregs);
321 memcpy32(env->gregs, src);
322 } else {
323 trace_win_helper_no_switch_pstate(new_pstate_regs);
325 env->pstate = new_pstate;
328 void helper_wrpstate(CPUSPARCState *env, target_ulong new_state)
330 cpu_change_pstate(env, new_state & 0xf3f);
332 #if !defined(CONFIG_USER_ONLY)
333 if (cpu_interrupts_enabled(env)) {
334 cpu_check_irqs(env);
336 #endif
339 void helper_wrpil(CPUSPARCState *env, target_ulong new_pil)
341 #if !defined(CONFIG_USER_ONLY)
342 trace_win_helper_wrpil(env->psrpil, (uint32_t)new_pil);
344 env->psrpil = new_pil;
346 if (cpu_interrupts_enabled(env)) {
347 cpu_check_irqs(env);
349 #endif
352 void helper_done(CPUSPARCState *env)
354 trap_state *tsptr = cpu_tsptr(env);
356 env->pc = tsptr->tnpc;
357 env->npc = tsptr->tnpc + 4;
358 cpu_put_ccr(env, tsptr->tstate >> 32);
359 env->asi = (tsptr->tstate >> 24) & 0xff;
360 cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
361 cpu_put_cwp64(env, tsptr->tstate & 0xff);
362 env->tl--;
364 trace_win_helper_done(env->tl);
366 #if !defined(CONFIG_USER_ONLY)
367 if (cpu_interrupts_enabled(env)) {
368 cpu_check_irqs(env);
370 #endif
373 void helper_retry(CPUSPARCState *env)
375 trap_state *tsptr = cpu_tsptr(env);
377 env->pc = tsptr->tpc;
378 env->npc = tsptr->tnpc;
379 cpu_put_ccr(env, tsptr->tstate >> 32);
380 env->asi = (tsptr->tstate >> 24) & 0xff;
381 cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
382 cpu_put_cwp64(env, tsptr->tstate & 0xff);
383 env->tl--;
385 trace_win_helper_retry(env->tl);
387 #if !defined(CONFIG_USER_ONLY)
388 if (cpu_interrupts_enabled(env)) {
389 cpu_check_irqs(env);
391 #endif
393 #endif