1ab3b491fSBlue Swirl /* 2ab3b491fSBlue Swirl * Sparc64 interrupt helpers 3ab3b491fSBlue Swirl * 4ab3b491fSBlue Swirl * Copyright (c) 2003-2005 Fabrice Bellard 5ab3b491fSBlue Swirl * 6ab3b491fSBlue Swirl * This library is free software; you can redistribute it and/or 7ab3b491fSBlue Swirl * modify it under the terms of the GNU Lesser General Public 8ab3b491fSBlue Swirl * License as published by the Free Software Foundation; either 9*5650b549SChetan Pant * version 2.1 of the License, or (at your option) any later version. 10ab3b491fSBlue Swirl * 11ab3b491fSBlue Swirl * This library is distributed in the hope that it will be useful, 12ab3b491fSBlue Swirl * but WITHOUT ANY WARRANTY; without even the implied warranty of 13ab3b491fSBlue Swirl * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14ab3b491fSBlue Swirl * Lesser General Public License for more details. 15ab3b491fSBlue Swirl * 16ab3b491fSBlue Swirl * You should have received a copy of the GNU Lesser General Public 17ab3b491fSBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18ab3b491fSBlue Swirl */ 19ab3b491fSBlue Swirl 20db5ebe5fSPeter Maydell #include "qemu/osdep.h" 215ee59930SAlex Bennée #include "qemu/main-loop.h" 22ab3b491fSBlue Swirl #include "cpu.h" 232ef6175aSRichard Henderson #include "exec/helper-proto.h" 24508127e2SPaolo Bonzini #include "exec/log.h" 2511e66bcaSBlue Swirl #include "trace.h" 26ab3b491fSBlue Swirl 27b884fc5eSRichard Henderson #define DEBUG_PCALL 28ab3b491fSBlue Swirl 29ab3b491fSBlue Swirl #ifdef DEBUG_PCALL 30ab3b491fSBlue Swirl static const char * const excp_names[0x80] = { 31ab3b491fSBlue Swirl [TT_TFAULT] = "Instruction Access Fault", 32ab3b491fSBlue Swirl [TT_TMISS] = "Instruction Access MMU Miss", 33ab3b491fSBlue Swirl [TT_CODE_ACCESS] = "Instruction Access Error", 34ab3b491fSBlue Swirl [TT_ILL_INSN] = "Illegal Instruction", 35ab3b491fSBlue Swirl [TT_PRIV_INSN] = "Privileged Instruction", 36ab3b491fSBlue Swirl [TT_NFPU_INSN] = "FPU Disabled", 37ab3b491fSBlue Swirl [TT_FP_EXCP] = "FPU Exception", 38ab3b491fSBlue Swirl [TT_TOVF] = "Tag Overflow", 39ab3b491fSBlue Swirl [TT_CLRWIN] = "Clean Windows", 40ab3b491fSBlue Swirl [TT_DIV_ZERO] = "Division By Zero", 41ab3b491fSBlue Swirl [TT_DFAULT] = "Data Access Fault", 42ab3b491fSBlue Swirl [TT_DMISS] = "Data Access MMU Miss", 43ab3b491fSBlue Swirl [TT_DATA_ACCESS] = "Data Access Error", 44ab3b491fSBlue Swirl [TT_DPROT] = "Data Protection Error", 45ab3b491fSBlue Swirl [TT_UNALIGNED] = "Unaligned Memory Access", 46ab3b491fSBlue Swirl [TT_PRIV_ACT] = "Privileged Action", 47ab3b491fSBlue Swirl [TT_EXTINT | 0x1] = "External Interrupt 1", 48ab3b491fSBlue Swirl [TT_EXTINT | 0x2] = "External Interrupt 2", 49ab3b491fSBlue Swirl [TT_EXTINT | 0x3] = "External Interrupt 3", 50ab3b491fSBlue Swirl [TT_EXTINT | 0x4] = "External Interrupt 4", 51ab3b491fSBlue Swirl [TT_EXTINT | 0x5] = "External Interrupt 5", 52ab3b491fSBlue Swirl [TT_EXTINT | 0x6] = "External Interrupt 6", 53ab3b491fSBlue Swirl [TT_EXTINT | 0x7] = "External Interrupt 7", 54ab3b491fSBlue Swirl [TT_EXTINT | 0x8] = "External Interrupt 8", 55ab3b491fSBlue Swirl [TT_EXTINT | 0x9] = "External Interrupt 9", 56ab3b491fSBlue Swirl [TT_EXTINT | 0xa] = "External Interrupt 10", 57ab3b491fSBlue Swirl [TT_EXTINT | 0xb] = "External Interrupt 11", 58ab3b491fSBlue Swirl [TT_EXTINT | 0xc] = "External Interrupt 12", 59ab3b491fSBlue Swirl [TT_EXTINT | 0xd] = "External Interrupt 13", 60ab3b491fSBlue Swirl [TT_EXTINT | 0xe] = "External Interrupt 14", 61ab3b491fSBlue Swirl [TT_EXTINT | 0xf] = "External Interrupt 15", 62ab3b491fSBlue Swirl }; 63ab3b491fSBlue Swirl #endif 64ab3b491fSBlue Swirl 6597a8ea5aSAndreas Färber void sparc_cpu_do_interrupt(CPUState *cs) 66ab3b491fSBlue Swirl { 6797a8ea5aSAndreas Färber SPARCCPU *cpu = SPARC_CPU(cs); 6897a8ea5aSAndreas Färber CPUSPARCState *env = &cpu->env; 6927103424SAndreas Färber int intno = cs->exception_index; 70ab3b491fSBlue Swirl trap_state *tsptr; 71ab3b491fSBlue Swirl 7220132b96SRichard Henderson /* Compute PSR before exposing state. */ 7320132b96SRichard Henderson if (env->cc_op != CC_OP_FLAGS) { 7420132b96SRichard Henderson cpu_get_psr(env); 7520132b96SRichard Henderson } 7620132b96SRichard Henderson 77ab3b491fSBlue Swirl #ifdef DEBUG_PCALL 78ab3b491fSBlue Swirl if (qemu_loglevel_mask(CPU_LOG_INT)) { 79ab3b491fSBlue Swirl static int count; 80ab3b491fSBlue Swirl const char *name; 81ab3b491fSBlue Swirl 826e040755SArtyom Tarasenko if (intno < 0 || intno >= 0x1ff) { 83ab3b491fSBlue Swirl name = "Unknown"; 846e040755SArtyom Tarasenko } else if (intno >= 0x180) { 856e040755SArtyom Tarasenko name = "Hyperprivileged Trap Instruction"; 86ab3b491fSBlue Swirl } else if (intno >= 0x100) { 87ab3b491fSBlue Swirl name = "Trap Instruction"; 88ab3b491fSBlue Swirl } else if (intno >= 0xc0) { 89ab3b491fSBlue Swirl name = "Window Fill"; 90ab3b491fSBlue Swirl } else if (intno >= 0x80) { 91ab3b491fSBlue Swirl name = "Window Spill"; 92ab3b491fSBlue Swirl } else { 93ab3b491fSBlue Swirl name = excp_names[intno]; 94ab3b491fSBlue Swirl if (!name) { 95ab3b491fSBlue Swirl name = "Unknown"; 96ab3b491fSBlue Swirl } 97ab3b491fSBlue Swirl } 98ab3b491fSBlue Swirl 99b884fc5eSRichard Henderson qemu_log("%6d: %s (v=%04x)\n", count, name, intno); 100a0762859SAndreas Färber log_cpu_state(cs, 0); 101ab3b491fSBlue Swirl #if 0 102ab3b491fSBlue Swirl { 103ab3b491fSBlue Swirl int i; 104ab3b491fSBlue Swirl uint8_t *ptr; 105ab3b491fSBlue Swirl 106ab3b491fSBlue Swirl qemu_log(" code="); 107ab3b491fSBlue Swirl ptr = (uint8_t *)env->pc; 108ab3b491fSBlue Swirl for (i = 0; i < 16; i++) { 109ab3b491fSBlue Swirl qemu_log(" %02x", ldub(ptr + i)); 110ab3b491fSBlue Swirl } 111ab3b491fSBlue Swirl qemu_log("\n"); 112ab3b491fSBlue Swirl } 113ab3b491fSBlue Swirl #endif 114ab3b491fSBlue Swirl count++; 115ab3b491fSBlue Swirl } 116ab3b491fSBlue Swirl #endif 117ab3b491fSBlue Swirl #if !defined(CONFIG_USER_ONLY) 118ab3b491fSBlue Swirl if (env->tl >= env->maxtl) { 119a47dddd7SAndreas Färber cpu_abort(cs, "Trap 0x%04x while trap level (%d) >= MAXTL (%d)," 12027103424SAndreas Färber " Error state", cs->exception_index, env->tl, env->maxtl); 121ab3b491fSBlue Swirl return; 122ab3b491fSBlue Swirl } 123ab3b491fSBlue Swirl #endif 124ab3b491fSBlue Swirl if (env->tl < env->maxtl - 1) { 125ab3b491fSBlue Swirl env->tl++; 126ab3b491fSBlue Swirl } else { 127ab3b491fSBlue Swirl env->pstate |= PS_RED; 128ab3b491fSBlue Swirl if (env->tl < env->maxtl) { 129ab3b491fSBlue Swirl env->tl++; 130ab3b491fSBlue Swirl } 131ab3b491fSBlue Swirl } 132ab3b491fSBlue Swirl tsptr = cpu_tsptr(env); 133ab3b491fSBlue Swirl 134ab3b491fSBlue Swirl tsptr->tstate = (cpu_get_ccr(env) << 32) | 135ab3b491fSBlue Swirl ((env->asi & 0xff) << 24) | ((env->pstate & 0xf3f) << 8) | 136ab3b491fSBlue Swirl cpu_get_cwp64(env); 137ab3b491fSBlue Swirl tsptr->tpc = env->pc; 138ab3b491fSBlue Swirl tsptr->tnpc = env->npc; 139ab3b491fSBlue Swirl tsptr->tt = intno; 140ab3b491fSBlue Swirl 1416e040755SArtyom Tarasenko if (cpu_has_hypervisor(env)) { 1426e040755SArtyom Tarasenko env->htstate[env->tl] = env->hpstate; 1436e040755SArtyom Tarasenko /* XXX OpenSPARC T1 - UltraSPARC T3 have MAXPTL=2 1446e040755SArtyom Tarasenko but this may change in the future */ 1456e040755SArtyom Tarasenko if (env->tl > 2) { 1466e040755SArtyom Tarasenko env->hpstate |= HS_PRIV; 1476e040755SArtyom Tarasenko } 1486e040755SArtyom Tarasenko } 1496e040755SArtyom Tarasenko 150576e1c4cSIgor Mammedov if (env->def.features & CPU_FEATURE_GL) { 151cbc3a6a4SArtyom Tarasenko tsptr->tstate |= (env->gl & 7ULL) << 40; 152cbc3a6a4SArtyom Tarasenko cpu_gl_switch_gregs(env, env->gl + 1); 153cbc3a6a4SArtyom Tarasenko env->gl++; 154cbc3a6a4SArtyom Tarasenko } 155cbc3a6a4SArtyom Tarasenko 156ab3b491fSBlue Swirl switch (intno) { 157ab3b491fSBlue Swirl case TT_IVEC: 1586e040755SArtyom Tarasenko if (!cpu_has_hypervisor(env)) { 159ab3b491fSBlue Swirl cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_IG); 1606e040755SArtyom Tarasenko } 161ab3b491fSBlue Swirl break; 162ab3b491fSBlue Swirl case TT_TFAULT: 163ab3b491fSBlue Swirl case TT_DFAULT: 164ab3b491fSBlue Swirl case TT_TMISS ... TT_TMISS + 3: 165ab3b491fSBlue Swirl case TT_DMISS ... TT_DMISS + 3: 166ab3b491fSBlue Swirl case TT_DPROT ... TT_DPROT + 3: 1676e040755SArtyom Tarasenko if (cpu_has_hypervisor(env)) { 1686e040755SArtyom Tarasenko env->hpstate |= HS_PRIV; 1696e040755SArtyom Tarasenko env->pstate = PS_PEF | PS_PRIV; 1706e040755SArtyom Tarasenko } else { 171ab3b491fSBlue Swirl cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_MG); 1726e040755SArtyom Tarasenko } 1736e040755SArtyom Tarasenko break; 1746e040755SArtyom Tarasenko case TT_INSN_REAL_TRANSLATION_MISS ... TT_DATA_REAL_TRANSLATION_MISS: 1756e040755SArtyom Tarasenko case TT_HTRAP ... TT_HTRAP + 127: 1766e040755SArtyom Tarasenko env->hpstate |= HS_PRIV; 177ab3b491fSBlue Swirl break; 178ab3b491fSBlue Swirl default: 179ab3b491fSBlue Swirl cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_AG); 180ab3b491fSBlue Swirl break; 181ab3b491fSBlue Swirl } 182ab3b491fSBlue Swirl 183ab3b491fSBlue Swirl if (intno == TT_CLRWIN) { 184ab3b491fSBlue Swirl cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - 1)); 185ab3b491fSBlue Swirl } else if ((intno & 0x1c0) == TT_SPILL) { 186ab3b491fSBlue Swirl cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2)); 187ab3b491fSBlue Swirl } else if ((intno & 0x1c0) == TT_FILL) { 188ab3b491fSBlue Swirl cpu_set_cwp(env, cpu_cwp_inc(env, env->cwp + 1)); 189ab3b491fSBlue Swirl } 1906e040755SArtyom Tarasenko 1916e040755SArtyom Tarasenko if (cpu_hypervisor_mode(env)) { 1926e040755SArtyom Tarasenko env->pc = (env->htba & ~0x3fffULL) | (intno << 5); 1936e040755SArtyom Tarasenko } else { 194de5f1077SArtyom Tarasenko env->pc = env->tbr & ~0x7fffULL; 195de5f1077SArtyom Tarasenko env->pc |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5); 1966e040755SArtyom Tarasenko } 197ab3b491fSBlue Swirl env->npc = env->pc + 4; 19827103424SAndreas Färber cs->exception_index = -1; 199ab3b491fSBlue Swirl } 2002336c1f1SBlue Swirl 201c5f9864eSAndreas Färber trap_state *cpu_tsptr(CPUSPARCState* env) 2022336c1f1SBlue Swirl { 2032336c1f1SBlue Swirl return &env->ts[env->tl & MAXTL_MASK]; 2042336c1f1SBlue Swirl } 20579227036SBlue Swirl 206c5f9864eSAndreas Färber static bool do_modify_softint(CPUSPARCState *env, uint32_t value) 20779227036SBlue Swirl { 20879227036SBlue Swirl if (env->softint != value) { 20979227036SBlue Swirl env->softint = value; 21079227036SBlue Swirl #if !defined(CONFIG_USER_ONLY) 21179227036SBlue Swirl if (cpu_interrupts_enabled(env)) { 2125ee59930SAlex Bennée qemu_mutex_lock_iothread(); 21379227036SBlue Swirl cpu_check_irqs(env); 2145ee59930SAlex Bennée qemu_mutex_unlock_iothread(); 21579227036SBlue Swirl } 21679227036SBlue Swirl #endif 21711e66bcaSBlue Swirl return true; 21879227036SBlue Swirl } 21911e66bcaSBlue Swirl return false; 22079227036SBlue Swirl } 22179227036SBlue Swirl 222c5f9864eSAndreas Färber void helper_set_softint(CPUSPARCState *env, uint64_t value) 22379227036SBlue Swirl { 22411e66bcaSBlue Swirl if (do_modify_softint(env, env->softint | (uint32_t)value)) { 22511e66bcaSBlue Swirl trace_int_helper_set_softint(env->softint); 22611e66bcaSBlue Swirl } 22779227036SBlue Swirl } 22879227036SBlue Swirl 229c5f9864eSAndreas Färber void helper_clear_softint(CPUSPARCState *env, uint64_t value) 23079227036SBlue Swirl { 23111e66bcaSBlue Swirl if (do_modify_softint(env, env->softint & (uint32_t)~value)) { 23211e66bcaSBlue Swirl trace_int_helper_clear_softint(env->softint); 23311e66bcaSBlue Swirl } 23479227036SBlue Swirl } 23579227036SBlue Swirl 236c5f9864eSAndreas Färber void helper_write_softint(CPUSPARCState *env, uint64_t value) 23779227036SBlue Swirl { 23811e66bcaSBlue Swirl if (do_modify_softint(env, (uint32_t)value)) { 23911e66bcaSBlue Swirl trace_int_helper_write_softint(env->softint); 24011e66bcaSBlue Swirl } 24179227036SBlue Swirl } 242