1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth * Sparc64 interrupt helpers
3fcf5ef2aSThomas Huth *
4fcf5ef2aSThomas Huth * Copyright (c) 2003-2005 Fabrice Bellard
5fcf5ef2aSThomas Huth *
6fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either
95650b549SChetan Pant * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth *
11fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14fcf5ef2aSThomas Huth * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth *
16fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth */
19fcf5ef2aSThomas Huth
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
215ee59930SAlex Bennée #include "qemu/main-loop.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
24fcf5ef2aSThomas Huth #include "exec/log.h"
25fcf5ef2aSThomas Huth #include "trace.h"
26fcf5ef2aSThomas Huth
27fcf5ef2aSThomas Huth #define DEBUG_PCALL
28fcf5ef2aSThomas Huth
29fcf5ef2aSThomas Huth #ifdef DEBUG_PCALL
30fcf5ef2aSThomas Huth static const char * const excp_names[0x80] = {
31fcf5ef2aSThomas Huth [TT_TFAULT] = "Instruction Access Fault",
32fcf5ef2aSThomas Huth [TT_TMISS] = "Instruction Access MMU Miss",
33fcf5ef2aSThomas Huth [TT_CODE_ACCESS] = "Instruction Access Error",
34fcf5ef2aSThomas Huth [TT_ILL_INSN] = "Illegal Instruction",
35fcf5ef2aSThomas Huth [TT_PRIV_INSN] = "Privileged Instruction",
36fcf5ef2aSThomas Huth [TT_NFPU_INSN] = "FPU Disabled",
37fcf5ef2aSThomas Huth [TT_FP_EXCP] = "FPU Exception",
38fcf5ef2aSThomas Huth [TT_TOVF] = "Tag Overflow",
39fcf5ef2aSThomas Huth [TT_CLRWIN] = "Clean Windows",
40fcf5ef2aSThomas Huth [TT_DIV_ZERO] = "Division By Zero",
41fcf5ef2aSThomas Huth [TT_DFAULT] = "Data Access Fault",
42fcf5ef2aSThomas Huth [TT_DMISS] = "Data Access MMU Miss",
43fcf5ef2aSThomas Huth [TT_DATA_ACCESS] = "Data Access Error",
44fcf5ef2aSThomas Huth [TT_DPROT] = "Data Protection Error",
45fcf5ef2aSThomas Huth [TT_UNALIGNED] = "Unaligned Memory Access",
46fcf5ef2aSThomas Huth [TT_PRIV_ACT] = "Privileged Action",
47fcf5ef2aSThomas Huth [TT_EXTINT | 0x1] = "External Interrupt 1",
48fcf5ef2aSThomas Huth [TT_EXTINT | 0x2] = "External Interrupt 2",
49fcf5ef2aSThomas Huth [TT_EXTINT | 0x3] = "External Interrupt 3",
50fcf5ef2aSThomas Huth [TT_EXTINT | 0x4] = "External Interrupt 4",
51fcf5ef2aSThomas Huth [TT_EXTINT | 0x5] = "External Interrupt 5",
52fcf5ef2aSThomas Huth [TT_EXTINT | 0x6] = "External Interrupt 6",
53fcf5ef2aSThomas Huth [TT_EXTINT | 0x7] = "External Interrupt 7",
54fcf5ef2aSThomas Huth [TT_EXTINT | 0x8] = "External Interrupt 8",
55fcf5ef2aSThomas Huth [TT_EXTINT | 0x9] = "External Interrupt 9",
56fcf5ef2aSThomas Huth [TT_EXTINT | 0xa] = "External Interrupt 10",
57fcf5ef2aSThomas Huth [TT_EXTINT | 0xb] = "External Interrupt 11",
58fcf5ef2aSThomas Huth [TT_EXTINT | 0xc] = "External Interrupt 12",
59fcf5ef2aSThomas Huth [TT_EXTINT | 0xd] = "External Interrupt 13",
60fcf5ef2aSThomas Huth [TT_EXTINT | 0xe] = "External Interrupt 14",
61fcf5ef2aSThomas Huth [TT_EXTINT | 0xf] = "External Interrupt 15",
62fcf5ef2aSThomas Huth };
63fcf5ef2aSThomas Huth #endif
64fcf5ef2aSThomas Huth
cpu_check_irqs(CPUSPARCState * env)6510fb1340SPhilippe Mathieu-Daudé void cpu_check_irqs(CPUSPARCState *env)
6610fb1340SPhilippe Mathieu-Daudé {
6710fb1340SPhilippe Mathieu-Daudé CPUState *cs;
6810fb1340SPhilippe Mathieu-Daudé uint32_t pil = env->pil_in |
6910fb1340SPhilippe Mathieu-Daudé (env->softint & ~(SOFTINT_TIMER | SOFTINT_STIMER));
7010fb1340SPhilippe Mathieu-Daudé
7110fb1340SPhilippe Mathieu-Daudé /* We should be holding the BQL before we mess with IRQs */
72195801d7SStefan Hajnoczi g_assert(bql_locked());
7310fb1340SPhilippe Mathieu-Daudé
7410fb1340SPhilippe Mathieu-Daudé /* TT_IVEC has a higher priority (16) than TT_EXTINT (31..17) */
7510fb1340SPhilippe Mathieu-Daudé if (env->ivec_status & 0x20) {
7610fb1340SPhilippe Mathieu-Daudé return;
7710fb1340SPhilippe Mathieu-Daudé }
7810fb1340SPhilippe Mathieu-Daudé cs = env_cpu(env);
7910fb1340SPhilippe Mathieu-Daudé /*
8010fb1340SPhilippe Mathieu-Daudé * check if TM or SM in SOFTINT are set
8110fb1340SPhilippe Mathieu-Daudé * setting these also causes interrupt 14
8210fb1340SPhilippe Mathieu-Daudé */
8310fb1340SPhilippe Mathieu-Daudé if (env->softint & (SOFTINT_TIMER | SOFTINT_STIMER)) {
8410fb1340SPhilippe Mathieu-Daudé pil |= 1 << 14;
8510fb1340SPhilippe Mathieu-Daudé }
8610fb1340SPhilippe Mathieu-Daudé
8710fb1340SPhilippe Mathieu-Daudé /*
8810fb1340SPhilippe Mathieu-Daudé * The bit corresponding to psrpil is (1<< psrpil),
8910fb1340SPhilippe Mathieu-Daudé * the next bit is (2 << psrpil).
9010fb1340SPhilippe Mathieu-Daudé */
9110fb1340SPhilippe Mathieu-Daudé if (pil < (2 << env->psrpil)) {
9210fb1340SPhilippe Mathieu-Daudé if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
9310fb1340SPhilippe Mathieu-Daudé trace_sparc64_cpu_check_irqs_reset_irq(env->interrupt_index);
9410fb1340SPhilippe Mathieu-Daudé env->interrupt_index = 0;
9510fb1340SPhilippe Mathieu-Daudé cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
9610fb1340SPhilippe Mathieu-Daudé }
9710fb1340SPhilippe Mathieu-Daudé return;
9810fb1340SPhilippe Mathieu-Daudé }
9910fb1340SPhilippe Mathieu-Daudé
10010fb1340SPhilippe Mathieu-Daudé if (cpu_interrupts_enabled(env)) {
10110fb1340SPhilippe Mathieu-Daudé
10210fb1340SPhilippe Mathieu-Daudé unsigned int i;
10310fb1340SPhilippe Mathieu-Daudé
10410fb1340SPhilippe Mathieu-Daudé for (i = 15; i > env->psrpil; i--) {
10510fb1340SPhilippe Mathieu-Daudé if (pil & (1 << i)) {
10610fb1340SPhilippe Mathieu-Daudé int old_interrupt = env->interrupt_index;
10710fb1340SPhilippe Mathieu-Daudé int new_interrupt = TT_EXTINT | i;
10810fb1340SPhilippe Mathieu-Daudé
10910fb1340SPhilippe Mathieu-Daudé if (unlikely(env->tl > 0 && cpu_tsptr(env)->tt > new_interrupt
11010fb1340SPhilippe Mathieu-Daudé && ((cpu_tsptr(env)->tt & 0x1f0) == TT_EXTINT))) {
11110fb1340SPhilippe Mathieu-Daudé trace_sparc64_cpu_check_irqs_noset_irq(env->tl,
11210fb1340SPhilippe Mathieu-Daudé cpu_tsptr(env)->tt,
11310fb1340SPhilippe Mathieu-Daudé new_interrupt);
11410fb1340SPhilippe Mathieu-Daudé } else if (old_interrupt != new_interrupt) {
11510fb1340SPhilippe Mathieu-Daudé env->interrupt_index = new_interrupt;
11610fb1340SPhilippe Mathieu-Daudé trace_sparc64_cpu_check_irqs_set_irq(i, old_interrupt,
11710fb1340SPhilippe Mathieu-Daudé new_interrupt);
11810fb1340SPhilippe Mathieu-Daudé cpu_interrupt(cs, CPU_INTERRUPT_HARD);
11910fb1340SPhilippe Mathieu-Daudé }
12010fb1340SPhilippe Mathieu-Daudé break;
12110fb1340SPhilippe Mathieu-Daudé }
12210fb1340SPhilippe Mathieu-Daudé }
12310fb1340SPhilippe Mathieu-Daudé } else if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
12410fb1340SPhilippe Mathieu-Daudé trace_sparc64_cpu_check_irqs_disabled(pil, env->pil_in, env->softint,
12510fb1340SPhilippe Mathieu-Daudé env->interrupt_index);
12610fb1340SPhilippe Mathieu-Daudé env->interrupt_index = 0;
12710fb1340SPhilippe Mathieu-Daudé cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
12810fb1340SPhilippe Mathieu-Daudé }
12910fb1340SPhilippe Mathieu-Daudé }
13010fb1340SPhilippe Mathieu-Daudé
sparc_cpu_do_interrupt(CPUState * cs)131fcf5ef2aSThomas Huth void sparc_cpu_do_interrupt(CPUState *cs)
132fcf5ef2aSThomas Huth {
133*77976769SPhilippe Mathieu-Daudé CPUSPARCState *env = cpu_env(cs);
134fcf5ef2aSThomas Huth int intno = cs->exception_index;
135fcf5ef2aSThomas Huth trap_state *tsptr;
136fcf5ef2aSThomas Huth
137fcf5ef2aSThomas Huth #ifdef DEBUG_PCALL
138fcf5ef2aSThomas Huth if (qemu_loglevel_mask(CPU_LOG_INT)) {
139fcf5ef2aSThomas Huth static int count;
140fcf5ef2aSThomas Huth const char *name;
141fcf5ef2aSThomas Huth
1426e040755SArtyom Tarasenko if (intno < 0 || intno >= 0x1ff) {
143fcf5ef2aSThomas Huth name = "Unknown";
1446e040755SArtyom Tarasenko } else if (intno >= 0x180) {
1456e040755SArtyom Tarasenko name = "Hyperprivileged Trap Instruction";
146fcf5ef2aSThomas Huth } else if (intno >= 0x100) {
147fcf5ef2aSThomas Huth name = "Trap Instruction";
148fcf5ef2aSThomas Huth } else if (intno >= 0xc0) {
149fcf5ef2aSThomas Huth name = "Window Fill";
150fcf5ef2aSThomas Huth } else if (intno >= 0x80) {
151fcf5ef2aSThomas Huth name = "Window Spill";
152fcf5ef2aSThomas Huth } else {
153fcf5ef2aSThomas Huth name = excp_names[intno];
154fcf5ef2aSThomas Huth if (!name) {
155fcf5ef2aSThomas Huth name = "Unknown";
156fcf5ef2aSThomas Huth }
157fcf5ef2aSThomas Huth }
158fcf5ef2aSThomas Huth
159fcf5ef2aSThomas Huth qemu_log("%6d: %s (v=%04x)\n", count, name, intno);
160fcf5ef2aSThomas Huth log_cpu_state(cs, 0);
161fcf5ef2aSThomas Huth #if 0
162fcf5ef2aSThomas Huth {
163fcf5ef2aSThomas Huth int i;
164fcf5ef2aSThomas Huth uint8_t *ptr;
165fcf5ef2aSThomas Huth
166fcf5ef2aSThomas Huth qemu_log(" code=");
167fcf5ef2aSThomas Huth ptr = (uint8_t *)env->pc;
168fcf5ef2aSThomas Huth for (i = 0; i < 16; i++) {
169fcf5ef2aSThomas Huth qemu_log(" %02x", ldub(ptr + i));
170fcf5ef2aSThomas Huth }
171fcf5ef2aSThomas Huth qemu_log("\n");
172fcf5ef2aSThomas Huth }
173fcf5ef2aSThomas Huth #endif
174fcf5ef2aSThomas Huth count++;
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth #endif
177fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
178fcf5ef2aSThomas Huth if (env->tl >= env->maxtl) {
179fcf5ef2aSThomas Huth cpu_abort(cs, "Trap 0x%04x while trap level (%d) >= MAXTL (%d),"
180fcf5ef2aSThomas Huth " Error state", cs->exception_index, env->tl, env->maxtl);
181fcf5ef2aSThomas Huth return;
182fcf5ef2aSThomas Huth }
183fcf5ef2aSThomas Huth #endif
184fcf5ef2aSThomas Huth if (env->tl < env->maxtl - 1) {
185fcf5ef2aSThomas Huth env->tl++;
186fcf5ef2aSThomas Huth } else {
187fcf5ef2aSThomas Huth env->pstate |= PS_RED;
188fcf5ef2aSThomas Huth if (env->tl < env->maxtl) {
189fcf5ef2aSThomas Huth env->tl++;
190fcf5ef2aSThomas Huth }
191fcf5ef2aSThomas Huth }
192fcf5ef2aSThomas Huth tsptr = cpu_tsptr(env);
193fcf5ef2aSThomas Huth
1947a5805a0SPeter Maydell tsptr->tstate = sparc64_tstate(env);
195fcf5ef2aSThomas Huth tsptr->tpc = env->pc;
196fcf5ef2aSThomas Huth tsptr->tnpc = env->npc;
197fcf5ef2aSThomas Huth tsptr->tt = intno;
198fcf5ef2aSThomas Huth
1996e040755SArtyom Tarasenko if (cpu_has_hypervisor(env)) {
2006e040755SArtyom Tarasenko env->htstate[env->tl] = env->hpstate;
2016e040755SArtyom Tarasenko /* XXX OpenSPARC T1 - UltraSPARC T3 have MAXPTL=2
2026e040755SArtyom Tarasenko but this may change in the future */
2036e040755SArtyom Tarasenko if (env->tl > 2) {
2046e040755SArtyom Tarasenko env->hpstate |= HS_PRIV;
2056e040755SArtyom Tarasenko }
2066e040755SArtyom Tarasenko }
2076e040755SArtyom Tarasenko
208576e1c4cSIgor Mammedov if (env->def.features & CPU_FEATURE_GL) {
209cbc3a6a4SArtyom Tarasenko cpu_gl_switch_gregs(env, env->gl + 1);
210cbc3a6a4SArtyom Tarasenko env->gl++;
211cbc3a6a4SArtyom Tarasenko }
212cbc3a6a4SArtyom Tarasenko
213fcf5ef2aSThomas Huth switch (intno) {
214fcf5ef2aSThomas Huth case TT_IVEC:
2156e040755SArtyom Tarasenko if (!cpu_has_hypervisor(env)) {
216fcf5ef2aSThomas Huth cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_IG);
2176e040755SArtyom Tarasenko }
218fcf5ef2aSThomas Huth break;
219fcf5ef2aSThomas Huth case TT_TFAULT:
220fcf5ef2aSThomas Huth case TT_DFAULT:
221fcf5ef2aSThomas Huth case TT_TMISS ... TT_TMISS + 3:
222fcf5ef2aSThomas Huth case TT_DMISS ... TT_DMISS + 3:
223fcf5ef2aSThomas Huth case TT_DPROT ... TT_DPROT + 3:
2246e040755SArtyom Tarasenko if (cpu_has_hypervisor(env)) {
2256e040755SArtyom Tarasenko env->hpstate |= HS_PRIV;
2266e040755SArtyom Tarasenko env->pstate = PS_PEF | PS_PRIV;
2276e040755SArtyom Tarasenko } else {
228fcf5ef2aSThomas Huth cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_MG);
2296e040755SArtyom Tarasenko }
2306e040755SArtyom Tarasenko break;
2316e040755SArtyom Tarasenko case TT_INSN_REAL_TRANSLATION_MISS ... TT_DATA_REAL_TRANSLATION_MISS:
2326e040755SArtyom Tarasenko case TT_HTRAP ... TT_HTRAP + 127:
2336e040755SArtyom Tarasenko env->hpstate |= HS_PRIV;
234fcf5ef2aSThomas Huth break;
235fcf5ef2aSThomas Huth default:
236fcf5ef2aSThomas Huth cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_AG);
237fcf5ef2aSThomas Huth break;
238fcf5ef2aSThomas Huth }
239fcf5ef2aSThomas Huth
240fcf5ef2aSThomas Huth if (intno == TT_CLRWIN) {
241fcf5ef2aSThomas Huth cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - 1));
242fcf5ef2aSThomas Huth } else if ((intno & 0x1c0) == TT_SPILL) {
243fcf5ef2aSThomas Huth cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
244fcf5ef2aSThomas Huth } else if ((intno & 0x1c0) == TT_FILL) {
245fcf5ef2aSThomas Huth cpu_set_cwp(env, cpu_cwp_inc(env, env->cwp + 1));
246fcf5ef2aSThomas Huth }
2476e040755SArtyom Tarasenko
2486e040755SArtyom Tarasenko if (cpu_hypervisor_mode(env)) {
2496e040755SArtyom Tarasenko env->pc = (env->htba & ~0x3fffULL) | (intno << 5);
2506e040755SArtyom Tarasenko } else {
251fcf5ef2aSThomas Huth env->pc = env->tbr & ~0x7fffULL;
252fcf5ef2aSThomas Huth env->pc |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
2536e040755SArtyom Tarasenko }
254fcf5ef2aSThomas Huth env->npc = env->pc + 4;
255fcf5ef2aSThomas Huth cs->exception_index = -1;
256fcf5ef2aSThomas Huth }
257fcf5ef2aSThomas Huth
cpu_tsptr(CPUSPARCState * env)258fcf5ef2aSThomas Huth trap_state *cpu_tsptr(CPUSPARCState* env)
259fcf5ef2aSThomas Huth {
260fcf5ef2aSThomas Huth return &env->ts[env->tl & MAXTL_MASK];
261fcf5ef2aSThomas Huth }
262fcf5ef2aSThomas Huth
do_modify_softint(CPUSPARCState * env,uint32_t value)263fcf5ef2aSThomas Huth static bool do_modify_softint(CPUSPARCState *env, uint32_t value)
264fcf5ef2aSThomas Huth {
265fcf5ef2aSThomas Huth if (env->softint != value) {
266fcf5ef2aSThomas Huth env->softint = value;
267fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
268fcf5ef2aSThomas Huth if (cpu_interrupts_enabled(env)) {
269195801d7SStefan Hajnoczi bql_lock();
270fcf5ef2aSThomas Huth cpu_check_irqs(env);
271195801d7SStefan Hajnoczi bql_unlock();
272fcf5ef2aSThomas Huth }
273fcf5ef2aSThomas Huth #endif
274fcf5ef2aSThomas Huth return true;
275fcf5ef2aSThomas Huth }
276fcf5ef2aSThomas Huth return false;
277fcf5ef2aSThomas Huth }
278fcf5ef2aSThomas Huth
helper_set_softint(CPUSPARCState * env,uint64_t value)279fcf5ef2aSThomas Huth void helper_set_softint(CPUSPARCState *env, uint64_t value)
280fcf5ef2aSThomas Huth {
281fcf5ef2aSThomas Huth if (do_modify_softint(env, env->softint | (uint32_t)value)) {
282fcf5ef2aSThomas Huth trace_int_helper_set_softint(env->softint);
283fcf5ef2aSThomas Huth }
284fcf5ef2aSThomas Huth }
285fcf5ef2aSThomas Huth
helper_clear_softint(CPUSPARCState * env,uint64_t value)286fcf5ef2aSThomas Huth void helper_clear_softint(CPUSPARCState *env, uint64_t value)
287fcf5ef2aSThomas Huth {
288fcf5ef2aSThomas Huth if (do_modify_softint(env, env->softint & (uint32_t)~value)) {
289fcf5ef2aSThomas Huth trace_int_helper_clear_softint(env->softint);
290fcf5ef2aSThomas Huth }
291fcf5ef2aSThomas Huth }
292fcf5ef2aSThomas Huth
helper_write_softint(CPUSPARCState * env,uint64_t value)293fcf5ef2aSThomas Huth void helper_write_softint(CPUSPARCState *env, uint64_t value)
294fcf5ef2aSThomas Huth {
295fcf5ef2aSThomas Huth if (do_modify_softint(env, (uint32_t)value)) {
296fcf5ef2aSThomas Huth trace_int_helper_write_softint(env->softint);
297fcf5ef2aSThomas Huth }
298fcf5ef2aSThomas Huth }
299