2011-08-01 17:03:20 +08:00
|
|
|
/*
|
|
|
|
* Helpers for CWP and PSTATE handling
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2005 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "helper.h"
|
2011-09-11 23:53:35 +08:00
|
|
|
#include "trace.h"
|
2011-08-01 17:03:20 +08:00
|
|
|
|
|
|
|
static inline void memcpy32(target_ulong *dst, const target_ulong *src)
|
|
|
|
{
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[1];
|
|
|
|
dst[2] = src[2];
|
|
|
|
dst[3] = src[3];
|
|
|
|
dst[4] = src[4];
|
|
|
|
dst[5] = src[5];
|
|
|
|
dst[6] = src[6];
|
|
|
|
dst[7] = src[7];
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void cpu_set_cwp(CPUState *env, int new_cwp)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
/* put the modified wrap registers at their proper location */
|
|
|
|
if (env->cwp == env->nwindows - 1) {
|
|
|
|
memcpy32(env->regbase, env->regbase + env->nwindows * 16);
|
|
|
|
}
|
|
|
|
env->cwp = new_cwp;
|
|
|
|
|
|
|
|
/* put the wrap registers at their temporary location */
|
|
|
|
if (new_cwp == env->nwindows - 1) {
|
|
|
|
memcpy32(env->regbase + env->nwindows * 16, env->regbase);
|
|
|
|
}
|
|
|
|
env->regwptr = env->regbase + (new_cwp * 16);
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
target_ulong cpu_get_psr(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
helper_compute_psr(env);
|
|
|
|
|
|
|
|
#if !defined(TARGET_SPARC64)
|
|
|
|
return env->version | (env->psr & PSR_ICC) |
|
|
|
|
(env->psref ? PSR_EF : 0) |
|
|
|
|
(env->psrpil << 8) |
|
|
|
|
(env->psrs ? PSR_S : 0) |
|
|
|
|
(env->psrps ? PSR_PS : 0) |
|
|
|
|
(env->psret ? PSR_ET : 0) | env->cwp;
|
|
|
|
#else
|
|
|
|
return env->psr & PSR_ICC;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void cpu_put_psr(CPUState *env, target_ulong val)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
env->psr = val & PSR_ICC;
|
|
|
|
#if !defined(TARGET_SPARC64)
|
|
|
|
env->psref = (val & PSR_EF) ? 1 : 0;
|
|
|
|
env->psrpil = (val & PSR_PIL) >> 8;
|
|
|
|
#endif
|
|
|
|
#if ((!defined(TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY))
|
|
|
|
cpu_check_irqs(env);
|
|
|
|
#endif
|
|
|
|
#if !defined(TARGET_SPARC64)
|
|
|
|
env->psrs = (val & PSR_S) ? 1 : 0;
|
|
|
|
env->psrps = (val & PSR_PS) ? 1 : 0;
|
|
|
|
env->psret = (val & PSR_ET) ? 1 : 0;
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_set_cwp(env, val & PSR_CWP);
|
2011-08-01 17:03:20 +08:00
|
|
|
#endif
|
|
|
|
env->cc_op = CC_OP_FLAGS;
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
int cpu_cwp_inc(CPUState *env, int cwp)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
if (unlikely(cwp >= env->nwindows)) {
|
|
|
|
cwp -= env->nwindows;
|
|
|
|
}
|
|
|
|
return cwp;
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
int cpu_cwp_dec(CPUState *env, int cwp)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
if (unlikely(cwp < 0)) {
|
|
|
|
cwp += env->nwindows;
|
|
|
|
}
|
|
|
|
return cwp;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef TARGET_SPARC64
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_rett(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
unsigned int cwp;
|
|
|
|
|
|
|
|
if (env->psret == 1) {
|
|
|
|
helper_raise_exception(env, TT_ILL_INSN);
|
|
|
|
}
|
|
|
|
|
|
|
|
env->psret = 1;
|
2011-07-04 05:01:59 +08:00
|
|
|
cwp = cpu_cwp_inc(env, env->cwp + 1) ;
|
2011-08-01 17:03:20 +08:00
|
|
|
if (env->wim & (1 << cwp)) {
|
|
|
|
helper_raise_exception(env, TT_WIN_UNF);
|
|
|
|
}
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_set_cwp(env, cwp);
|
2011-08-01 17:03:20 +08:00
|
|
|
env->psrs = env->psrps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: use another pointer for %iN registers to avoid slow wrapping
|
|
|
|
handling ? */
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_save(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
uint32_t cwp;
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
cwp = cpu_cwp_dec(env, env->cwp - 1);
|
2011-08-01 17:03:20 +08:00
|
|
|
if (env->wim & (1 << cwp)) {
|
|
|
|
helper_raise_exception(env, TT_WIN_OVF);
|
|
|
|
}
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_set_cwp(env, cwp);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_restore(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
uint32_t cwp;
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
cwp = cpu_cwp_inc(env, env->cwp + 1);
|
2011-08-01 17:03:20 +08:00
|
|
|
if (env->wim & (1 << cwp)) {
|
|
|
|
helper_raise_exception(env, TT_WIN_UNF);
|
|
|
|
}
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_set_cwp(env, cwp);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_wrpsr(CPUState *env, target_ulong new_psr)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
if ((new_psr & PSR_CWP) >= env->nwindows) {
|
|
|
|
helper_raise_exception(env, TT_ILL_INSN);
|
|
|
|
} else {
|
|
|
|
cpu_put_psr(env, new_psr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
target_ulong helper_rdpsr(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
2011-07-04 05:01:59 +08:00
|
|
|
return cpu_get_psr(env);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* XXX: use another pointer for %iN registers to avoid slow wrapping
|
|
|
|
handling ? */
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_save(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
uint32_t cwp;
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
cwp = cpu_cwp_dec(env, env->cwp - 1);
|
2011-08-01 17:03:20 +08:00
|
|
|
if (env->cansave == 0) {
|
|
|
|
helper_raise_exception(env, TT_SPILL | (env->otherwin != 0 ?
|
|
|
|
(TT_WOTHER |
|
|
|
|
((env->wstate & 0x38) >> 1)) :
|
|
|
|
((env->wstate & 0x7) << 2)));
|
|
|
|
} else {
|
|
|
|
if (env->cleanwin - env->canrestore == 0) {
|
|
|
|
/* XXX Clean windows without trap */
|
|
|
|
helper_raise_exception(env, TT_CLRWIN);
|
|
|
|
} else {
|
|
|
|
env->cansave--;
|
|
|
|
env->canrestore++;
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_set_cwp(env, cwp);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_restore(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
uint32_t cwp;
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
cwp = cpu_cwp_inc(env, env->cwp + 1);
|
2011-08-01 17:03:20 +08:00
|
|
|
if (env->canrestore == 0) {
|
|
|
|
helper_raise_exception(env, TT_FILL | (env->otherwin != 0 ?
|
|
|
|
(TT_WOTHER |
|
|
|
|
((env->wstate & 0x38) >> 1)) :
|
|
|
|
((env->wstate & 0x7) << 2)));
|
|
|
|
} else {
|
|
|
|
env->cansave++;
|
|
|
|
env->canrestore--;
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_set_cwp(env, cwp);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_flushw(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
if (env->cansave != env->nwindows - 2) {
|
|
|
|
helper_raise_exception(env, TT_SPILL | (env->otherwin != 0 ?
|
|
|
|
(TT_WOTHER |
|
|
|
|
((env->wstate & 0x38) >> 1)) :
|
|
|
|
((env->wstate & 0x7) << 2)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_saved(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
env->cansave++;
|
|
|
|
if (env->otherwin == 0) {
|
|
|
|
env->canrestore--;
|
|
|
|
} else {
|
|
|
|
env->otherwin--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_restored(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
env->canrestore++;
|
|
|
|
if (env->cleanwin < env->nwindows - 1) {
|
|
|
|
env->cleanwin++;
|
|
|
|
}
|
|
|
|
if (env->otherwin == 0) {
|
|
|
|
env->cansave--;
|
|
|
|
} else {
|
|
|
|
env->otherwin--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
target_ulong cpu_get_ccr(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
target_ulong psr;
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
psr = cpu_get_psr(env);
|
2011-08-01 17:03:20 +08:00
|
|
|
|
|
|
|
return ((env->xcc >> 20) << 4) | ((psr & PSR_ICC) >> 20);
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void cpu_put_ccr(CPUState *env, target_ulong val)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
env->xcc = (val >> 4) << 20;
|
|
|
|
env->psr = (val & 0xf) << 20;
|
|
|
|
CC_OP = CC_OP_FLAGS;
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
target_ulong cpu_get_cwp64(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
return env->nwindows - 1 - env->cwp;
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void cpu_put_cwp64(CPUState *env, int cwp)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
if (unlikely(cwp >= env->nwindows || cwp < 0)) {
|
|
|
|
cwp %= env->nwindows;
|
|
|
|
}
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_set_cwp(env, env->nwindows - 1 - cwp);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
target_ulong helper_rdccr(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
2011-07-04 05:01:59 +08:00
|
|
|
return cpu_get_ccr(env);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_wrccr(CPUState *env, target_ulong new_ccr)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_put_ccr(env, new_ccr);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CWP handling is reversed in V9, but we still use the V8 register
|
|
|
|
order. */
|
2011-07-04 05:01:59 +08:00
|
|
|
target_ulong helper_rdcwp(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
2011-07-04 05:01:59 +08:00
|
|
|
return cpu_get_cwp64(env);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_wrcwp(CPUState *env, target_ulong new_cwp)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_put_cwp64(env, new_cwp);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
static inline uint64_t *get_gregset(CPUState *env, uint32_t pstate)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
switch (pstate) {
|
|
|
|
default:
|
2011-09-11 23:53:35 +08:00
|
|
|
trace_win_helper_gregset_error(pstate);
|
2011-08-01 17:03:20 +08:00
|
|
|
/* pass through to normal set of global registers */
|
|
|
|
case 0:
|
|
|
|
return env->bgregs;
|
|
|
|
case PS_AG:
|
|
|
|
return env->agregs;
|
|
|
|
case PS_MG:
|
|
|
|
return env->mgregs;
|
|
|
|
case PS_IG:
|
|
|
|
return env->igregs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void cpu_change_pstate(CPUState *env, uint32_t new_pstate)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
uint32_t pstate_regs, new_pstate_regs;
|
|
|
|
uint64_t *src, *dst;
|
|
|
|
|
|
|
|
if (env->def->features & CPU_FEATURE_GL) {
|
|
|
|
/* PS_AG is not implemented in this case */
|
|
|
|
new_pstate &= ~PS_AG;
|
|
|
|
}
|
|
|
|
|
|
|
|
pstate_regs = env->pstate & 0xc01;
|
|
|
|
new_pstate_regs = new_pstate & 0xc01;
|
|
|
|
|
|
|
|
if (new_pstate_regs != pstate_regs) {
|
2011-09-11 23:53:35 +08:00
|
|
|
trace_win_helper_switch_pstate(pstate_regs, new_pstate_regs);
|
|
|
|
|
2011-08-01 17:03:20 +08:00
|
|
|
/* Switch global register bank */
|
2011-07-04 05:01:59 +08:00
|
|
|
src = get_gregset(env, new_pstate_regs);
|
|
|
|
dst = get_gregset(env, pstate_regs);
|
2011-08-01 17:03:20 +08:00
|
|
|
memcpy32(dst, env->gregs);
|
|
|
|
memcpy32(env->gregs, src);
|
|
|
|
} else {
|
2011-09-11 23:53:35 +08:00
|
|
|
trace_win_helper_no_switch_pstate(new_pstate_regs);
|
2011-08-01 17:03:20 +08:00
|
|
|
}
|
|
|
|
env->pstate = new_pstate;
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_wrpstate(CPUState *env, target_ulong new_state)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_change_pstate(env, new_state & 0xf3f);
|
2011-08-01 17:03:20 +08:00
|
|
|
|
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
|
|
|
if (cpu_interrupts_enabled(env)) {
|
|
|
|
cpu_check_irqs(env);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_wrpil(CPUState *env, target_ulong new_pil)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
2011-09-11 23:53:35 +08:00
|
|
|
trace_win_helper_wrpil(env->psrpil, (uint32_t)new_pil);
|
2011-08-01 17:03:20 +08:00
|
|
|
|
|
|
|
env->psrpil = new_pil;
|
|
|
|
|
|
|
|
if (cpu_interrupts_enabled(env)) {
|
|
|
|
cpu_check_irqs(env);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_done(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
trap_state *tsptr = cpu_tsptr(env);
|
|
|
|
|
|
|
|
env->pc = tsptr->tnpc;
|
|
|
|
env->npc = tsptr->tnpc + 4;
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_put_ccr(env, tsptr->tstate >> 32);
|
2011-08-01 17:03:20 +08:00
|
|
|
env->asi = (tsptr->tstate >> 24) & 0xff;
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
|
|
|
|
cpu_put_cwp64(env, tsptr->tstate & 0xff);
|
2011-08-01 17:03:20 +08:00
|
|
|
env->tl--;
|
|
|
|
|
2011-09-11 23:53:35 +08:00
|
|
|
trace_win_helper_done(env->tl);
|
2011-08-01 17:03:20 +08:00
|
|
|
|
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
|
|
|
if (cpu_interrupts_enabled(env)) {
|
|
|
|
cpu_check_irqs(env);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-07-04 05:01:59 +08:00
|
|
|
void helper_retry(CPUState *env)
|
2011-08-01 17:03:20 +08:00
|
|
|
{
|
|
|
|
trap_state *tsptr = cpu_tsptr(env);
|
|
|
|
|
|
|
|
env->pc = tsptr->tpc;
|
|
|
|
env->npc = tsptr->tnpc;
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_put_ccr(env, tsptr->tstate >> 32);
|
2011-08-01 17:03:20 +08:00
|
|
|
env->asi = (tsptr->tstate >> 24) & 0xff;
|
2011-07-04 05:01:59 +08:00
|
|
|
cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f);
|
|
|
|
cpu_put_cwp64(env, tsptr->tstate & 0xff);
|
2011-08-01 17:03:20 +08:00
|
|
|
env->tl--;
|
|
|
|
|
2011-09-11 23:53:35 +08:00
|
|
|
trace_win_helper_retry(env->tl);
|
2011-08-01 17:03:20 +08:00
|
|
|
|
|
|
|
#if !defined(CONFIG_USER_ONLY)
|
|
|
|
if (cpu_interrupts_enabled(env)) {
|
|
|
|
cpu_check_irqs(env);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|