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.1 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 "qemu/osdep.h"
21 #include "qemu/main-loop.h"
23 #include "exec/exec-all.h"
24 #include "exec/helper-proto.h"
27 static inline void memcpy32(target_ulong
*dst
, const target_ulong
*src
)
39 void cpu_set_cwp(CPUSPARCState
*env
, int new_cwp
)
41 /* put the modified wrap registers at their proper location */
42 if (env
->cwp
== env
->nwindows
- 1) {
43 memcpy32(env
->regbase
, env
->regbase
+ env
->nwindows
* 16);
47 /* put the wrap registers at their temporary location */
48 if (new_cwp
== env
->nwindows
- 1) {
49 memcpy32(env
->regbase
+ env
->nwindows
* 16, env
->regbase
);
51 env
->regwptr
= env
->regbase
+ (new_cwp
* 16);
54 target_ulong
cpu_get_psr(CPUSPARCState
*env
)
56 helper_compute_psr(env
);
58 #if !defined(TARGET_SPARC64)
59 return env
->version
| (env
->psr
& PSR_ICC
) |
60 (env
->psref
? PSR_EF
: 0) |
62 (env
->psrs
? PSR_S
: 0) |
63 (env
->psrps
? PSR_PS
: 0) |
64 (env
->psret
? PSR_ET
: 0) | env
->cwp
;
66 return env
->psr
& PSR_ICC
;
70 void cpu_put_psr_raw(CPUSPARCState
*env
, target_ulong val
)
72 env
->psr
= val
& PSR_ICC
;
73 #if !defined(TARGET_SPARC64)
74 env
->psref
= (val
& PSR_EF
) ? 1 : 0;
75 env
->psrpil
= (val
& PSR_PIL
) >> 8;
76 env
->psrs
= (val
& PSR_S
) ? 1 : 0;
77 env
->psrps
= (val
& PSR_PS
) ? 1 : 0;
78 env
->psret
= (val
& PSR_ET
) ? 1 : 0;
80 env
->cc_op
= CC_OP_FLAGS
;
81 #if !defined(TARGET_SPARC64)
82 cpu_set_cwp(env
, val
& PSR_CWP
);
86 /* Called with BQL held */
87 void cpu_put_psr(CPUSPARCState
*env
, target_ulong val
)
89 cpu_put_psr_raw(env
, val
);
90 #if ((!defined(TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY))
95 int cpu_cwp_inc(CPUSPARCState
*env
, int cwp
)
97 if (unlikely(cwp
>= env
->nwindows
)) {
103 int cpu_cwp_dec(CPUSPARCState
*env
, int cwp
)
105 if (unlikely(cwp
< 0)) {
106 cwp
+= env
->nwindows
;
111 #ifndef TARGET_SPARC64
112 void helper_rett(CPUSPARCState
*env
)
116 if (env
->psret
== 1) {
117 cpu_raise_exception_ra(env
, TT_ILL_INSN
, GETPC());
121 cwp
= cpu_cwp_inc(env
, env
->cwp
+ 1) ;
122 if (env
->wim
& (1 << cwp
)) {
123 cpu_raise_exception_ra(env
, TT_WIN_UNF
, GETPC());
125 cpu_set_cwp(env
, cwp
);
126 env
->psrs
= env
->psrps
;
129 /* XXX: use another pointer for %iN registers to avoid slow wrapping
131 void helper_save(CPUSPARCState
*env
)
135 cwp
= cpu_cwp_dec(env
, env
->cwp
- 1);
136 if (env
->wim
& (1 << cwp
)) {
137 cpu_raise_exception_ra(env
, TT_WIN_OVF
, GETPC());
139 cpu_set_cwp(env
, cwp
);
142 void helper_restore(CPUSPARCState
*env
)
146 cwp
= cpu_cwp_inc(env
, env
->cwp
+ 1);
147 if (env
->wim
& (1 << cwp
)) {
148 cpu_raise_exception_ra(env
, TT_WIN_UNF
, GETPC());
150 cpu_set_cwp(env
, cwp
);
153 void helper_wrpsr(CPUSPARCState
*env
, target_ulong new_psr
)
155 if ((new_psr
& PSR_CWP
) >= env
->nwindows
) {
156 cpu_raise_exception_ra(env
, TT_ILL_INSN
, GETPC());
158 /* cpu_put_psr may trigger interrupts, hence BQL */
159 qemu_mutex_lock_iothread();
160 cpu_put_psr(env
, new_psr
);
161 qemu_mutex_unlock_iothread();
165 target_ulong
helper_rdpsr(CPUSPARCState
*env
)
167 return cpu_get_psr(env
);
171 /* XXX: use another pointer for %iN registers to avoid slow wrapping
173 void helper_save(CPUSPARCState
*env
)
177 cwp
= cpu_cwp_dec(env
, env
->cwp
- 1);
178 if (env
->cansave
== 0) {
179 int tt
= TT_SPILL
| (env
->otherwin
!= 0
180 ? (TT_WOTHER
| ((env
->wstate
& 0x38) >> 1))
181 : ((env
->wstate
& 0x7) << 2));
182 cpu_raise_exception_ra(env
, tt
, GETPC());
184 if (env
->cleanwin
- env
->canrestore
== 0) {
185 /* XXX Clean windows without trap */
186 cpu_raise_exception_ra(env
, TT_CLRWIN
, GETPC());
190 cpu_set_cwp(env
, cwp
);
195 void helper_restore(CPUSPARCState
*env
)
199 cwp
= cpu_cwp_inc(env
, env
->cwp
+ 1);
200 if (env
->canrestore
== 0) {
201 int tt
= TT_FILL
| (env
->otherwin
!= 0
202 ? (TT_WOTHER
| ((env
->wstate
& 0x38) >> 1))
203 : ((env
->wstate
& 0x7) << 2));
204 cpu_raise_exception_ra(env
, tt
, GETPC());
208 cpu_set_cwp(env
, cwp
);
212 void helper_flushw(CPUSPARCState
*env
)
214 if (env
->cansave
!= env
->nwindows
- 2) {
215 int tt
= TT_SPILL
| (env
->otherwin
!= 0
216 ? (TT_WOTHER
| ((env
->wstate
& 0x38) >> 1))
217 : ((env
->wstate
& 0x7) << 2));
218 cpu_raise_exception_ra(env
, tt
, GETPC());
222 void helper_saved(CPUSPARCState
*env
)
225 if (env
->otherwin
== 0) {
232 void helper_restored(CPUSPARCState
*env
)
235 if (env
->cleanwin
< env
->nwindows
- 1) {
238 if (env
->otherwin
== 0) {
245 target_ulong
cpu_get_ccr(CPUSPARCState
*env
)
249 psr
= cpu_get_psr(env
);
251 return ((env
->xcc
>> 20) << 4) | ((psr
& PSR_ICC
) >> 20);
254 void cpu_put_ccr(CPUSPARCState
*env
, target_ulong val
)
256 env
->xcc
= (val
>> 4) << 20;
257 env
->psr
= (val
& 0xf) << 20;
261 target_ulong
cpu_get_cwp64(CPUSPARCState
*env
)
263 return env
->nwindows
- 1 - env
->cwp
;
266 void cpu_put_cwp64(CPUSPARCState
*env
, int cwp
)
268 if (unlikely(cwp
>= env
->nwindows
|| cwp
< 0)) {
269 cwp
%= env
->nwindows
;
271 cpu_set_cwp(env
, env
->nwindows
- 1 - cwp
);
274 target_ulong
helper_rdccr(CPUSPARCState
*env
)
276 return cpu_get_ccr(env
);
279 void helper_wrccr(CPUSPARCState
*env
, target_ulong new_ccr
)
281 cpu_put_ccr(env
, new_ccr
);
284 /* CWP handling is reversed in V9, but we still use the V8 register
286 target_ulong
helper_rdcwp(CPUSPARCState
*env
)
288 return cpu_get_cwp64(env
);
291 void helper_wrcwp(CPUSPARCState
*env
, target_ulong new_cwp
)
293 cpu_put_cwp64(env
, new_cwp
);
296 static inline uint64_t *get_gregset(CPUSPARCState
*env
, uint32_t pstate
)
298 if (env
->def
.features
& CPU_FEATURE_GL
) {
299 return env
->glregs
+ (env
->gl
& 7) * 8;
304 trace_win_helper_gregset_error(pstate
);
305 /* fall through to normal set of global registers */
317 static inline uint64_t *get_gl_gregset(CPUSPARCState
*env
, uint32_t gl
)
319 return env
->glregs
+ (gl
& 7) * 8;
322 /* Switch global register bank */
323 void cpu_gl_switch_gregs(CPUSPARCState
*env
, uint32_t new_gl
)
326 src
= get_gl_gregset(env
, new_gl
);
327 dst
= get_gl_gregset(env
, env
->gl
);
330 memcpy32(dst
, env
->gregs
);
331 memcpy32(env
->gregs
, src
);
335 void helper_wrgl(CPUSPARCState
*env
, target_ulong new_gl
)
337 cpu_gl_switch_gregs(env
, new_gl
& 7);
338 env
->gl
= new_gl
& 7;
341 void cpu_change_pstate(CPUSPARCState
*env
, uint32_t new_pstate
)
343 uint32_t pstate_regs
, new_pstate_regs
;
346 if (env
->def
.features
& CPU_FEATURE_GL
) {
347 /* PS_AG, IG and MG are not implemented in this case */
348 new_pstate
&= ~(PS_AG
| PS_IG
| PS_MG
);
349 env
->pstate
= new_pstate
;
353 pstate_regs
= env
->pstate
& 0xc01;
354 new_pstate_regs
= new_pstate
& 0xc01;
356 if (new_pstate_regs
!= pstate_regs
) {
357 trace_win_helper_switch_pstate(pstate_regs
, new_pstate_regs
);
359 /* Switch global register bank */
360 src
= get_gregset(env
, new_pstate_regs
);
361 dst
= get_gregset(env
, pstate_regs
);
362 memcpy32(dst
, env
->gregs
);
363 memcpy32(env
->gregs
, src
);
365 trace_win_helper_no_switch_pstate(new_pstate_regs
);
367 env
->pstate
= new_pstate
;
370 void helper_wrpstate(CPUSPARCState
*env
, target_ulong new_state
)
372 cpu_change_pstate(env
, new_state
& 0xf3f);
374 #if !defined(CONFIG_USER_ONLY)
375 if (cpu_interrupts_enabled(env
)) {
376 qemu_mutex_lock_iothread();
378 qemu_mutex_unlock_iothread();
383 void helper_wrpil(CPUSPARCState
*env
, target_ulong new_pil
)
385 #if !defined(CONFIG_USER_ONLY)
386 trace_win_helper_wrpil(env
->psrpil
, (uint32_t)new_pil
);
388 env
->psrpil
= new_pil
;
390 if (cpu_interrupts_enabled(env
)) {
391 qemu_mutex_lock_iothread();
393 qemu_mutex_unlock_iothread();
398 void helper_done(CPUSPARCState
*env
)
400 trap_state
*tsptr
= cpu_tsptr(env
);
402 env
->pc
= tsptr
->tnpc
;
403 env
->npc
= tsptr
->tnpc
+ 4;
404 cpu_put_ccr(env
, tsptr
->tstate
>> 32);
405 env
->asi
= (tsptr
->tstate
>> 24) & 0xff;
406 cpu_change_pstate(env
, (tsptr
->tstate
>> 8) & 0xf3f);
407 cpu_put_cwp64(env
, tsptr
->tstate
& 0xff);
408 if (cpu_has_hypervisor(env
)) {
409 uint32_t new_gl
= (tsptr
->tstate
>> 40) & 7;
410 env
->hpstate
= env
->htstate
[env
->tl
];
411 cpu_gl_switch_gregs(env
, new_gl
);
416 trace_win_helper_done(env
->tl
);
418 #if !defined(CONFIG_USER_ONLY)
419 if (cpu_interrupts_enabled(env
)) {
420 qemu_mutex_lock_iothread();
422 qemu_mutex_unlock_iothread();
427 void helper_retry(CPUSPARCState
*env
)
429 trap_state
*tsptr
= cpu_tsptr(env
);
431 env
->pc
= tsptr
->tpc
;
432 env
->npc
= tsptr
->tnpc
;
433 cpu_put_ccr(env
, tsptr
->tstate
>> 32);
434 env
->asi
= (tsptr
->tstate
>> 24) & 0xff;
435 cpu_change_pstate(env
, (tsptr
->tstate
>> 8) & 0xf3f);
436 cpu_put_cwp64(env
, tsptr
->tstate
& 0xff);
437 if (cpu_has_hypervisor(env
)) {
438 uint32_t new_gl
= (tsptr
->tstate
>> 40) & 7;
439 env
->hpstate
= env
->htstate
[env
->tl
];
440 cpu_gl_switch_gregs(env
, new_gl
);
445 trace_win_helper_retry(env
->tl
);
447 #if !defined(CONFIG_USER_ONLY)
448 if (cpu_interrupts_enabled(env
)) {
449 qemu_mutex_lock_iothread();
451 qemu_mutex_unlock_iothread();