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 20db5ebe5fSPeter Maydell #include "qemu/osdep.h" 21070af384SBlue Swirl #include "cpu.h" 222f9d35fcSRichard Henderson #include "exec/exec-all.h" 232ef6175aSRichard Henderson #include "exec/helper-proto.h" 24870be6adSBlue Swirl #include "trace.h" 25070af384SBlue Swirl 26070af384SBlue Swirl static inline void memcpy32(target_ulong *dst, const target_ulong *src) 27070af384SBlue Swirl { 28070af384SBlue Swirl dst[0] = src[0]; 29070af384SBlue Swirl dst[1] = src[1]; 30070af384SBlue Swirl dst[2] = src[2]; 31070af384SBlue Swirl dst[3] = src[3]; 32070af384SBlue Swirl dst[4] = src[4]; 33070af384SBlue Swirl dst[5] = src[5]; 34070af384SBlue Swirl dst[6] = src[6]; 35070af384SBlue Swirl dst[7] = src[7]; 36070af384SBlue Swirl } 37070af384SBlue Swirl 38c5f9864eSAndreas Färber void cpu_set_cwp(CPUSPARCState *env, int new_cwp) 39070af384SBlue Swirl { 40070af384SBlue Swirl /* put the modified wrap registers at their proper location */ 41070af384SBlue Swirl if (env->cwp == env->nwindows - 1) { 42070af384SBlue Swirl memcpy32(env->regbase, env->regbase + env->nwindows * 16); 43070af384SBlue Swirl } 44070af384SBlue Swirl env->cwp = new_cwp; 45070af384SBlue Swirl 46070af384SBlue Swirl /* put the wrap registers at their temporary location */ 47070af384SBlue Swirl if (new_cwp == env->nwindows - 1) { 48070af384SBlue Swirl memcpy32(env->regbase + env->nwindows * 16, env->regbase); 49070af384SBlue Swirl } 50070af384SBlue Swirl env->regwptr = env->regbase + (new_cwp * 16); 51070af384SBlue Swirl } 52070af384SBlue Swirl 53c5f9864eSAndreas Färber target_ulong cpu_get_psr(CPUSPARCState *env) 54070af384SBlue Swirl { 55070af384SBlue Swirl helper_compute_psr(env); 56070af384SBlue Swirl 57070af384SBlue Swirl #if !defined(TARGET_SPARC64) 58070af384SBlue Swirl return env->version | (env->psr & PSR_ICC) | 59070af384SBlue Swirl (env->psref ? PSR_EF : 0) | 60070af384SBlue Swirl (env->psrpil << 8) | 61070af384SBlue Swirl (env->psrs ? PSR_S : 0) | 62070af384SBlue Swirl (env->psrps ? PSR_PS : 0) | 63070af384SBlue Swirl (env->psret ? PSR_ET : 0) | env->cwp; 64070af384SBlue Swirl #else 65070af384SBlue Swirl return env->psr & PSR_ICC; 66070af384SBlue Swirl #endif 67070af384SBlue Swirl } 68070af384SBlue Swirl 694552a09dSPeter Maydell void cpu_put_psr_raw(CPUSPARCState *env, target_ulong val) 70070af384SBlue Swirl { 71070af384SBlue Swirl env->psr = val & PSR_ICC; 72070af384SBlue Swirl #if !defined(TARGET_SPARC64) 73070af384SBlue Swirl env->psref = (val & PSR_EF) ? 1 : 0; 74070af384SBlue Swirl env->psrpil = (val & PSR_PIL) >> 8; 75070af384SBlue Swirl env->psrs = (val & PSR_S) ? 1 : 0; 76070af384SBlue Swirl env->psrps = (val & PSR_PS) ? 1 : 0; 77070af384SBlue Swirl env->psret = (val & PSR_ET) ? 1 : 0; 78070af384SBlue Swirl #endif 79070af384SBlue Swirl env->cc_op = CC_OP_FLAGS; 804552a09dSPeter Maydell #if !defined(TARGET_SPARC64) 814552a09dSPeter Maydell cpu_set_cwp(env, val & PSR_CWP); 824552a09dSPeter Maydell #endif 834552a09dSPeter Maydell } 844552a09dSPeter Maydell 854552a09dSPeter Maydell void cpu_put_psr(CPUSPARCState *env, target_ulong val) 864552a09dSPeter Maydell { 874552a09dSPeter Maydell cpu_put_psr_raw(env, val); 884552a09dSPeter Maydell #if ((!defined(TARGET_SPARC64)) && !defined(CONFIG_USER_ONLY)) 894552a09dSPeter Maydell cpu_check_irqs(env); 904552a09dSPeter Maydell #endif 91070af384SBlue Swirl } 92070af384SBlue Swirl 93c5f9864eSAndreas Färber int cpu_cwp_inc(CPUSPARCState *env, int cwp) 94070af384SBlue Swirl { 95070af384SBlue Swirl if (unlikely(cwp >= env->nwindows)) { 96070af384SBlue Swirl cwp -= env->nwindows; 97070af384SBlue Swirl } 98070af384SBlue Swirl return cwp; 99070af384SBlue Swirl } 100070af384SBlue Swirl 101c5f9864eSAndreas Färber int cpu_cwp_dec(CPUSPARCState *env, int cwp) 102070af384SBlue Swirl { 103070af384SBlue Swirl if (unlikely(cwp < 0)) { 104070af384SBlue Swirl cwp += env->nwindows; 105070af384SBlue Swirl } 106070af384SBlue Swirl return cwp; 107070af384SBlue Swirl } 108070af384SBlue Swirl 109070af384SBlue Swirl #ifndef TARGET_SPARC64 110c5f9864eSAndreas Färber void helper_rett(CPUSPARCState *env) 111070af384SBlue Swirl { 112070af384SBlue Swirl unsigned int cwp; 113070af384SBlue Swirl 114070af384SBlue Swirl if (env->psret == 1) { 1152f9d35fcSRichard Henderson cpu_raise_exception_ra(env, TT_ILL_INSN, GETPC()); 116070af384SBlue Swirl } 117070af384SBlue Swirl 118070af384SBlue Swirl env->psret = 1; 119063c3675SBlue Swirl cwp = cpu_cwp_inc(env, env->cwp + 1) ; 120070af384SBlue Swirl if (env->wim & (1 << cwp)) { 1212f9d35fcSRichard Henderson cpu_raise_exception_ra(env, TT_WIN_UNF, GETPC()); 122070af384SBlue Swirl } 123063c3675SBlue Swirl cpu_set_cwp(env, cwp); 124070af384SBlue Swirl env->psrs = env->psrps; 125070af384SBlue Swirl } 126070af384SBlue Swirl 127070af384SBlue Swirl /* XXX: use another pointer for %iN registers to avoid slow wrapping 128070af384SBlue Swirl handling ? */ 129c5f9864eSAndreas Färber void helper_save(CPUSPARCState *env) 130070af384SBlue Swirl { 131070af384SBlue Swirl uint32_t cwp; 132070af384SBlue Swirl 133063c3675SBlue Swirl cwp = cpu_cwp_dec(env, env->cwp - 1); 134070af384SBlue Swirl if (env->wim & (1 << cwp)) { 1352f9d35fcSRichard Henderson cpu_raise_exception_ra(env, TT_WIN_OVF, GETPC()); 136070af384SBlue Swirl } 137063c3675SBlue Swirl cpu_set_cwp(env, cwp); 138070af384SBlue Swirl } 139070af384SBlue Swirl 140c5f9864eSAndreas Färber void helper_restore(CPUSPARCState *env) 141070af384SBlue Swirl { 142070af384SBlue Swirl uint32_t cwp; 143070af384SBlue Swirl 144063c3675SBlue Swirl cwp = cpu_cwp_inc(env, env->cwp + 1); 145070af384SBlue Swirl if (env->wim & (1 << cwp)) { 1462f9d35fcSRichard Henderson cpu_raise_exception_ra(env, TT_WIN_UNF, GETPC()); 147070af384SBlue Swirl } 148063c3675SBlue Swirl cpu_set_cwp(env, cwp); 149070af384SBlue Swirl } 150070af384SBlue Swirl 151c5f9864eSAndreas Färber void helper_wrpsr(CPUSPARCState *env, target_ulong new_psr) 152070af384SBlue Swirl { 153070af384SBlue Swirl if ((new_psr & PSR_CWP) >= env->nwindows) { 1542f9d35fcSRichard Henderson cpu_raise_exception_ra(env, TT_ILL_INSN, GETPC()); 155070af384SBlue Swirl } else { 156070af384SBlue Swirl cpu_put_psr(env, new_psr); 157070af384SBlue Swirl } 158070af384SBlue Swirl } 159070af384SBlue Swirl 160c5f9864eSAndreas Färber target_ulong helper_rdpsr(CPUSPARCState *env) 161070af384SBlue Swirl { 162063c3675SBlue Swirl return cpu_get_psr(env); 163070af384SBlue Swirl } 164070af384SBlue Swirl 165070af384SBlue Swirl #else 166070af384SBlue Swirl /* XXX: use another pointer for %iN registers to avoid slow wrapping 167070af384SBlue Swirl handling ? */ 168c5f9864eSAndreas Färber void helper_save(CPUSPARCState *env) 169070af384SBlue Swirl { 170070af384SBlue Swirl uint32_t cwp; 171070af384SBlue Swirl 172063c3675SBlue Swirl cwp = cpu_cwp_dec(env, env->cwp - 1); 173070af384SBlue Swirl if (env->cansave == 0) { 1742f9d35fcSRichard Henderson int tt = TT_SPILL | (env->otherwin != 0 1752f9d35fcSRichard Henderson ? (TT_WOTHER | ((env->wstate & 0x38) >> 1)) 1762f9d35fcSRichard Henderson : ((env->wstate & 0x7) << 2)); 1772f9d35fcSRichard Henderson cpu_raise_exception_ra(env, tt, GETPC()); 178070af384SBlue Swirl } else { 179070af384SBlue Swirl if (env->cleanwin - env->canrestore == 0) { 180070af384SBlue Swirl /* XXX Clean windows without trap */ 1812f9d35fcSRichard Henderson cpu_raise_exception_ra(env, TT_CLRWIN, GETPC()); 182070af384SBlue Swirl } else { 183070af384SBlue Swirl env->cansave--; 184070af384SBlue Swirl env->canrestore++; 185063c3675SBlue Swirl cpu_set_cwp(env, cwp); 186070af384SBlue Swirl } 187070af384SBlue Swirl } 188070af384SBlue Swirl } 189070af384SBlue Swirl 190c5f9864eSAndreas Färber void helper_restore(CPUSPARCState *env) 191070af384SBlue Swirl { 192070af384SBlue Swirl uint32_t cwp; 193070af384SBlue Swirl 194063c3675SBlue Swirl cwp = cpu_cwp_inc(env, env->cwp + 1); 195070af384SBlue Swirl if (env->canrestore == 0) { 1962f9d35fcSRichard Henderson int tt = TT_FILL | (env->otherwin != 0 1972f9d35fcSRichard Henderson ? (TT_WOTHER | ((env->wstate & 0x38) >> 1)) 1982f9d35fcSRichard Henderson : ((env->wstate & 0x7) << 2)); 1992f9d35fcSRichard Henderson cpu_raise_exception_ra(env, tt, GETPC()); 200070af384SBlue Swirl } else { 201070af384SBlue Swirl env->cansave++; 202070af384SBlue Swirl env->canrestore--; 203063c3675SBlue Swirl cpu_set_cwp(env, cwp); 204070af384SBlue Swirl } 205070af384SBlue Swirl } 206070af384SBlue Swirl 207c5f9864eSAndreas Färber void helper_flushw(CPUSPARCState *env) 208070af384SBlue Swirl { 209070af384SBlue Swirl if (env->cansave != env->nwindows - 2) { 2102f9d35fcSRichard Henderson int tt = TT_SPILL | (env->otherwin != 0 2112f9d35fcSRichard Henderson ? (TT_WOTHER | ((env->wstate & 0x38) >> 1)) 2122f9d35fcSRichard Henderson : ((env->wstate & 0x7) << 2)); 2132f9d35fcSRichard Henderson cpu_raise_exception_ra(env, tt, GETPC()); 214070af384SBlue Swirl } 215070af384SBlue Swirl } 216070af384SBlue Swirl 217c5f9864eSAndreas Färber void helper_saved(CPUSPARCState *env) 218070af384SBlue Swirl { 219070af384SBlue Swirl env->cansave++; 220070af384SBlue Swirl if (env->otherwin == 0) { 221070af384SBlue Swirl env->canrestore--; 222070af384SBlue Swirl } else { 223070af384SBlue Swirl env->otherwin--; 224070af384SBlue Swirl } 225070af384SBlue Swirl } 226070af384SBlue Swirl 227c5f9864eSAndreas Färber void helper_restored(CPUSPARCState *env) 228070af384SBlue Swirl { 229070af384SBlue Swirl env->canrestore++; 230070af384SBlue Swirl if (env->cleanwin < env->nwindows - 1) { 231070af384SBlue Swirl env->cleanwin++; 232070af384SBlue Swirl } 233070af384SBlue Swirl if (env->otherwin == 0) { 234070af384SBlue Swirl env->cansave--; 235070af384SBlue Swirl } else { 236070af384SBlue Swirl env->otherwin--; 237070af384SBlue Swirl } 238070af384SBlue Swirl } 239070af384SBlue Swirl 240c5f9864eSAndreas Färber target_ulong cpu_get_ccr(CPUSPARCState *env) 241070af384SBlue Swirl { 242070af384SBlue Swirl target_ulong psr; 243070af384SBlue Swirl 244063c3675SBlue Swirl psr = cpu_get_psr(env); 245070af384SBlue Swirl 246070af384SBlue Swirl return ((env->xcc >> 20) << 4) | ((psr & PSR_ICC) >> 20); 247070af384SBlue Swirl } 248070af384SBlue Swirl 249c5f9864eSAndreas Färber void cpu_put_ccr(CPUSPARCState *env, target_ulong val) 250070af384SBlue Swirl { 251070af384SBlue Swirl env->xcc = (val >> 4) << 20; 252070af384SBlue Swirl env->psr = (val & 0xf) << 20; 253070af384SBlue Swirl CC_OP = CC_OP_FLAGS; 254070af384SBlue Swirl } 255070af384SBlue Swirl 256c5f9864eSAndreas Färber target_ulong cpu_get_cwp64(CPUSPARCState *env) 257070af384SBlue Swirl { 258070af384SBlue Swirl return env->nwindows - 1 - env->cwp; 259070af384SBlue Swirl } 260070af384SBlue Swirl 261c5f9864eSAndreas Färber void cpu_put_cwp64(CPUSPARCState *env, int cwp) 262070af384SBlue Swirl { 263070af384SBlue Swirl if (unlikely(cwp >= env->nwindows || cwp < 0)) { 264070af384SBlue Swirl cwp %= env->nwindows; 265070af384SBlue Swirl } 266063c3675SBlue Swirl cpu_set_cwp(env, env->nwindows - 1 - cwp); 267070af384SBlue Swirl } 268070af384SBlue Swirl 269c5f9864eSAndreas Färber target_ulong helper_rdccr(CPUSPARCState *env) 270070af384SBlue Swirl { 271063c3675SBlue Swirl return cpu_get_ccr(env); 272070af384SBlue Swirl } 273070af384SBlue Swirl 274c5f9864eSAndreas Färber void helper_wrccr(CPUSPARCState *env, target_ulong new_ccr) 275070af384SBlue Swirl { 276063c3675SBlue Swirl cpu_put_ccr(env, new_ccr); 277070af384SBlue Swirl } 278070af384SBlue Swirl 279070af384SBlue Swirl /* CWP handling is reversed in V9, but we still use the V8 register 280070af384SBlue Swirl order. */ 281c5f9864eSAndreas Färber target_ulong helper_rdcwp(CPUSPARCState *env) 282070af384SBlue Swirl { 283063c3675SBlue Swirl return cpu_get_cwp64(env); 284070af384SBlue Swirl } 285070af384SBlue Swirl 286c5f9864eSAndreas Färber void helper_wrcwp(CPUSPARCState *env, target_ulong new_cwp) 287070af384SBlue Swirl { 288063c3675SBlue Swirl cpu_put_cwp64(env, new_cwp); 289070af384SBlue Swirl } 290070af384SBlue Swirl 291c5f9864eSAndreas Färber static inline uint64_t *get_gregset(CPUSPARCState *env, uint32_t pstate) 292070af384SBlue Swirl { 293070af384SBlue Swirl switch (pstate) { 294070af384SBlue Swirl default: 295870be6adSBlue Swirl trace_win_helper_gregset_error(pstate); 296070af384SBlue Swirl /* pass through to normal set of global registers */ 297070af384SBlue Swirl case 0: 298070af384SBlue Swirl return env->bgregs; 299070af384SBlue Swirl case PS_AG: 300070af384SBlue Swirl return env->agregs; 301070af384SBlue Swirl case PS_MG: 302070af384SBlue Swirl return env->mgregs; 303070af384SBlue Swirl case PS_IG: 304070af384SBlue Swirl return env->igregs; 305070af384SBlue Swirl } 306070af384SBlue Swirl } 307070af384SBlue Swirl 308c5f9864eSAndreas Färber void cpu_change_pstate(CPUSPARCState *env, uint32_t new_pstate) 309070af384SBlue Swirl { 310070af384SBlue Swirl uint32_t pstate_regs, new_pstate_regs; 311070af384SBlue Swirl uint64_t *src, *dst; 312070af384SBlue Swirl 313070af384SBlue Swirl if (env->def->features & CPU_FEATURE_GL) { 314070af384SBlue Swirl /* PS_AG is not implemented in this case */ 315070af384SBlue Swirl new_pstate &= ~PS_AG; 316070af384SBlue Swirl } 317070af384SBlue Swirl 318070af384SBlue Swirl pstate_regs = env->pstate & 0xc01; 319070af384SBlue Swirl new_pstate_regs = new_pstate & 0xc01; 320070af384SBlue Swirl 321070af384SBlue Swirl if (new_pstate_regs != pstate_regs) { 322870be6adSBlue Swirl trace_win_helper_switch_pstate(pstate_regs, new_pstate_regs); 323870be6adSBlue Swirl 324070af384SBlue Swirl /* Switch global register bank */ 325063c3675SBlue Swirl src = get_gregset(env, new_pstate_regs); 326063c3675SBlue Swirl dst = get_gregset(env, pstate_regs); 327070af384SBlue Swirl memcpy32(dst, env->gregs); 328070af384SBlue Swirl memcpy32(env->gregs, src); 329070af384SBlue Swirl } else { 330870be6adSBlue Swirl trace_win_helper_no_switch_pstate(new_pstate_regs); 331070af384SBlue Swirl } 332070af384SBlue Swirl env->pstate = new_pstate; 333070af384SBlue Swirl } 334070af384SBlue Swirl 335c5f9864eSAndreas Färber void helper_wrpstate(CPUSPARCState *env, target_ulong new_state) 336070af384SBlue Swirl { 337063c3675SBlue Swirl cpu_change_pstate(env, new_state & 0xf3f); 338070af384SBlue Swirl 339070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY) 340070af384SBlue Swirl if (cpu_interrupts_enabled(env)) { 341070af384SBlue Swirl cpu_check_irqs(env); 342070af384SBlue Swirl } 343070af384SBlue Swirl #endif 344070af384SBlue Swirl } 345070af384SBlue Swirl 346c5f9864eSAndreas Färber void helper_wrpil(CPUSPARCState *env, target_ulong new_pil) 347070af384SBlue Swirl { 348070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY) 349870be6adSBlue Swirl trace_win_helper_wrpil(env->psrpil, (uint32_t)new_pil); 350070af384SBlue Swirl 351070af384SBlue Swirl env->psrpil = new_pil; 352070af384SBlue Swirl 353070af384SBlue Swirl if (cpu_interrupts_enabled(env)) { 354070af384SBlue Swirl cpu_check_irqs(env); 355070af384SBlue Swirl } 356070af384SBlue Swirl #endif 357070af384SBlue Swirl } 358070af384SBlue Swirl 359c5f9864eSAndreas Färber void helper_done(CPUSPARCState *env) 360070af384SBlue Swirl { 361070af384SBlue Swirl trap_state *tsptr = cpu_tsptr(env); 362070af384SBlue Swirl 363070af384SBlue Swirl env->pc = tsptr->tnpc; 364070af384SBlue Swirl env->npc = tsptr->tnpc + 4; 365063c3675SBlue Swirl cpu_put_ccr(env, tsptr->tstate >> 32); 366070af384SBlue Swirl env->asi = (tsptr->tstate >> 24) & 0xff; 367063c3675SBlue Swirl cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f); 368063c3675SBlue Swirl cpu_put_cwp64(env, tsptr->tstate & 0xff); 369*6e040755SArtyom Tarasenko if (cpu_has_hypervisor(env)) { 370*6e040755SArtyom Tarasenko env->hpstate = env->htstate[env->tl]; 371*6e040755SArtyom Tarasenko } 372070af384SBlue Swirl env->tl--; 373070af384SBlue Swirl 374870be6adSBlue Swirl trace_win_helper_done(env->tl); 375070af384SBlue Swirl 376070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY) 377070af384SBlue Swirl if (cpu_interrupts_enabled(env)) { 378070af384SBlue Swirl cpu_check_irqs(env); 379070af384SBlue Swirl } 380070af384SBlue Swirl #endif 381070af384SBlue Swirl } 382070af384SBlue Swirl 383c5f9864eSAndreas Färber void helper_retry(CPUSPARCState *env) 384070af384SBlue Swirl { 385070af384SBlue Swirl trap_state *tsptr = cpu_tsptr(env); 386070af384SBlue Swirl 387070af384SBlue Swirl env->pc = tsptr->tpc; 388070af384SBlue Swirl env->npc = tsptr->tnpc; 389063c3675SBlue Swirl cpu_put_ccr(env, tsptr->tstate >> 32); 390070af384SBlue Swirl env->asi = (tsptr->tstate >> 24) & 0xff; 391063c3675SBlue Swirl cpu_change_pstate(env, (tsptr->tstate >> 8) & 0xf3f); 392063c3675SBlue Swirl cpu_put_cwp64(env, tsptr->tstate & 0xff); 393*6e040755SArtyom Tarasenko if (cpu_has_hypervisor(env)) { 394*6e040755SArtyom Tarasenko env->hpstate = env->htstate[env->tl]; 395*6e040755SArtyom Tarasenko } 396070af384SBlue Swirl env->tl--; 397070af384SBlue Swirl 398870be6adSBlue Swirl trace_win_helper_retry(env->tl); 399070af384SBlue Swirl 400070af384SBlue Swirl #if !defined(CONFIG_USER_ONLY) 401070af384SBlue Swirl if (cpu_interrupts_enabled(env)) { 402070af384SBlue Swirl cpu_check_irqs(env); 403070af384SBlue Swirl } 404070af384SBlue Swirl #endif 405070af384SBlue Swirl } 406070af384SBlue Swirl #endif 407