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/>.
24 static inline void memcpy32(target_ulong
*dst
, const target_ulong
*src
)
36 void cpu_set_cwp(CPUState
*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);
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(CPUState
*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) |
59 (env
->psrs
? PSR_S
: 0) |
60 (env
->psrps
? PSR_PS
: 0) |
61 (env
->psret
? PSR_ET
: 0) | env
->cwp
;
63 return env
->psr
& PSR_ICC
;
67 void cpu_put_psr(CPUState
*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;
74 #if ((!defined(TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY))
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
);
83 env
->cc_op
= CC_OP_FLAGS
;
86 int cpu_cwp_inc(CPUState
*env
, int cwp
)
88 if (unlikely(cwp
>= env
->nwindows
)) {
94 int cpu_cwp_dec(CPUState
*env
, int cwp
)
96 if (unlikely(cwp
< 0)) {
102 #ifndef TARGET_SPARC64
103 void helper_rett(CPUState
*env
)
107 if (env
->psret
== 1) {
108 helper_raise_exception(env
, TT_ILL_INSN
);
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
122 void helper_save(CPUState
*env
)
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(CPUState
*env
)
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(CPUState
*env
, target_ulong new_psr
)
146 if ((new_psr
& PSR_CWP
) >= env
->nwindows
) {
147 helper_raise_exception(env
, TT_ILL_INSN
);
149 cpu_put_psr(env
, new_psr
);
153 target_ulong
helper_rdpsr(CPUState
*env
)
155 return cpu_get_psr(env
);
159 /* XXX: use another pointer for %iN registers to avoid slow wrapping
161 void helper_save(CPUState
*env
)
165 cwp
= cpu_cwp_dec(env
, env
->cwp
- 1);
166 if (env
->cansave
== 0) {
167 helper_raise_exception(env
, TT_SPILL
| (env
->otherwin
!= 0 ?
169 ((env
->wstate
& 0x38) >> 1)) :
170 ((env
->wstate
& 0x7) << 2)));
172 if (env
->cleanwin
- env
->canrestore
== 0) {
173 /* XXX Clean windows without trap */
174 helper_raise_exception(env
, TT_CLRWIN
);
178 cpu_set_cwp(env
, cwp
);
183 void helper_restore(CPUState
*env
)
187 cwp
= cpu_cwp_inc(env
, env
->cwp
+ 1);
188 if (env
->canrestore
== 0) {
189 helper_raise_exception(env
, TT_FILL
| (env
->otherwin
!= 0 ?
191 ((env
->wstate
& 0x38) >> 1)) :
192 ((env
->wstate
& 0x7) << 2)));
196 cpu_set_cwp(env
, cwp
);
200 void helper_flushw(CPUState
*env
)
202 if (env
->cansave
!= env
->nwindows
- 2) {
203 helper_raise_exception(env
, TT_SPILL
| (env
->otherwin
!= 0 ?
205 ((env
->wstate
& 0x38) >> 1)) :
206 ((env
->wstate
& 0x7) << 2)));
210 void helper_saved(CPUState
*env
)
213 if (env
->otherwin
== 0) {
220 void helper_restored(CPUState
*env
)
223 if (env
->cleanwin
< env
->nwindows
- 1) {
226 if (env
->otherwin
== 0) {
233 target_ulong
cpu_get_ccr(CPUState
*env
)
237 psr
= cpu_get_psr(env
);
239 return ((env
->xcc
>> 20) << 4) | ((psr
& PSR_ICC
) >> 20);
242 void cpu_put_ccr(CPUState
*env
, target_ulong val
)
244 env
->xcc
= (val
>> 4) << 20;
245 env
->psr
= (val
& 0xf) << 20;
249 target_ulong
cpu_get_cwp64(CPUState
*env
)
251 return env
->nwindows
- 1 - env
->cwp
;
254 void cpu_put_cwp64(CPUState
*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(CPUState
*env
)
264 return cpu_get_ccr(env
);
267 void helper_wrccr(CPUState
*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
274 target_ulong
helper_rdcwp(CPUState
*env
)
276 return cpu_get_cwp64(env
);
279 void helper_wrcwp(CPUState
*env
, target_ulong new_cwp
)
281 cpu_put_cwp64(env
, new_cwp
);
284 static inline uint64_t *get_gregset(CPUState
*env
, uint32_t pstate
)
288 trace_win_helper_gregset_error(pstate
);
289 /* pass through to normal set of global registers */
301 void cpu_change_pstate(CPUState
*env
, uint32_t new_pstate
)
303 uint32_t pstate_regs
, new_pstate_regs
;
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
);
323 trace_win_helper_no_switch_pstate(new_pstate_regs
);
325 env
->pstate
= new_pstate
;
328 void helper_wrpstate(CPUState
*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
)) {
339 void helper_wrpil(CPUState
*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
)) {
352 void helper_done(CPUState
*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);
364 trace_win_helper_done(env
->tl
);
366 #if !defined(CONFIG_USER_ONLY)
367 if (cpu_interrupts_enabled(env
)) {
373 void helper_retry(CPUState
*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);
385 trace_win_helper_retry(env
->tl
);
387 #if !defined(CONFIG_USER_ONLY)
388 if (cpu_interrupts_enabled(env
)) {