xref: /qemu/target/sparc/int64_helper.c (revision 10fb1340b161682d64320a5976f88f68472410bf)
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
95650b549SChetan 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 
65*10fb1340SPhilippe Mathieu-Daudé void cpu_check_irqs(CPUSPARCState *env)
66*10fb1340SPhilippe Mathieu-Daudé {
67*10fb1340SPhilippe Mathieu-Daudé     CPUState *cs;
68*10fb1340SPhilippe Mathieu-Daudé     uint32_t pil = env->pil_in |
69*10fb1340SPhilippe Mathieu-Daudé                   (env->softint & ~(SOFTINT_TIMER | SOFTINT_STIMER));
70*10fb1340SPhilippe Mathieu-Daudé 
71*10fb1340SPhilippe Mathieu-Daudé     /* We should be holding the BQL before we mess with IRQs */
72*10fb1340SPhilippe Mathieu-Daudé     g_assert(qemu_mutex_iothread_locked());
73*10fb1340SPhilippe Mathieu-Daudé 
74*10fb1340SPhilippe Mathieu-Daudé     /* TT_IVEC has a higher priority (16) than TT_EXTINT (31..17) */
75*10fb1340SPhilippe Mathieu-Daudé     if (env->ivec_status & 0x20) {
76*10fb1340SPhilippe Mathieu-Daudé         return;
77*10fb1340SPhilippe Mathieu-Daudé     }
78*10fb1340SPhilippe Mathieu-Daudé     cs = env_cpu(env);
79*10fb1340SPhilippe Mathieu-Daudé     /*
80*10fb1340SPhilippe Mathieu-Daudé      * check if TM or SM in SOFTINT are set
81*10fb1340SPhilippe Mathieu-Daudé      * setting these also causes interrupt 14
82*10fb1340SPhilippe Mathieu-Daudé      */
83*10fb1340SPhilippe Mathieu-Daudé     if (env->softint & (SOFTINT_TIMER | SOFTINT_STIMER)) {
84*10fb1340SPhilippe Mathieu-Daudé         pil |= 1 << 14;
85*10fb1340SPhilippe Mathieu-Daudé     }
86*10fb1340SPhilippe Mathieu-Daudé 
87*10fb1340SPhilippe Mathieu-Daudé     /*
88*10fb1340SPhilippe Mathieu-Daudé      * The bit corresponding to psrpil is (1<< psrpil),
89*10fb1340SPhilippe Mathieu-Daudé      * the next bit is (2 << psrpil).
90*10fb1340SPhilippe Mathieu-Daudé      */
91*10fb1340SPhilippe Mathieu-Daudé     if (pil < (2 << env->psrpil)) {
92*10fb1340SPhilippe Mathieu-Daudé         if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
93*10fb1340SPhilippe Mathieu-Daudé             trace_sparc64_cpu_check_irqs_reset_irq(env->interrupt_index);
94*10fb1340SPhilippe Mathieu-Daudé             env->interrupt_index = 0;
95*10fb1340SPhilippe Mathieu-Daudé             cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
96*10fb1340SPhilippe Mathieu-Daudé         }
97*10fb1340SPhilippe Mathieu-Daudé         return;
98*10fb1340SPhilippe Mathieu-Daudé     }
99*10fb1340SPhilippe Mathieu-Daudé 
100*10fb1340SPhilippe Mathieu-Daudé     if (cpu_interrupts_enabled(env)) {
101*10fb1340SPhilippe Mathieu-Daudé 
102*10fb1340SPhilippe Mathieu-Daudé         unsigned int i;
103*10fb1340SPhilippe Mathieu-Daudé 
104*10fb1340SPhilippe Mathieu-Daudé         for (i = 15; i > env->psrpil; i--) {
105*10fb1340SPhilippe Mathieu-Daudé             if (pil & (1 << i)) {
106*10fb1340SPhilippe Mathieu-Daudé                 int old_interrupt = env->interrupt_index;
107*10fb1340SPhilippe Mathieu-Daudé                 int new_interrupt = TT_EXTINT | i;
108*10fb1340SPhilippe Mathieu-Daudé 
109*10fb1340SPhilippe Mathieu-Daudé                 if (unlikely(env->tl > 0 && cpu_tsptr(env)->tt > new_interrupt
110*10fb1340SPhilippe Mathieu-Daudé                   && ((cpu_tsptr(env)->tt & 0x1f0) == TT_EXTINT))) {
111*10fb1340SPhilippe Mathieu-Daudé                     trace_sparc64_cpu_check_irqs_noset_irq(env->tl,
112*10fb1340SPhilippe Mathieu-Daudé                                                       cpu_tsptr(env)->tt,
113*10fb1340SPhilippe Mathieu-Daudé                                                       new_interrupt);
114*10fb1340SPhilippe Mathieu-Daudé                 } else if (old_interrupt != new_interrupt) {
115*10fb1340SPhilippe Mathieu-Daudé                     env->interrupt_index = new_interrupt;
116*10fb1340SPhilippe Mathieu-Daudé                     trace_sparc64_cpu_check_irqs_set_irq(i, old_interrupt,
117*10fb1340SPhilippe Mathieu-Daudé                                                          new_interrupt);
118*10fb1340SPhilippe Mathieu-Daudé                     cpu_interrupt(cs, CPU_INTERRUPT_HARD);
119*10fb1340SPhilippe Mathieu-Daudé                 }
120*10fb1340SPhilippe Mathieu-Daudé                 break;
121*10fb1340SPhilippe Mathieu-Daudé             }
122*10fb1340SPhilippe Mathieu-Daudé         }
123*10fb1340SPhilippe Mathieu-Daudé     } else if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
124*10fb1340SPhilippe Mathieu-Daudé         trace_sparc64_cpu_check_irqs_disabled(pil, env->pil_in, env->softint,
125*10fb1340SPhilippe Mathieu-Daudé                                               env->interrupt_index);
126*10fb1340SPhilippe Mathieu-Daudé         env->interrupt_index = 0;
127*10fb1340SPhilippe Mathieu-Daudé         cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
128*10fb1340SPhilippe Mathieu-Daudé     }
129*10fb1340SPhilippe Mathieu-Daudé }
130*10fb1340SPhilippe Mathieu-Daudé 
13197a8ea5aSAndreas Färber void sparc_cpu_do_interrupt(CPUState *cs)
132ab3b491fSBlue Swirl {
13397a8ea5aSAndreas Färber     SPARCCPU *cpu = SPARC_CPU(cs);
13497a8ea5aSAndreas Färber     CPUSPARCState *env = &cpu->env;
13527103424SAndreas Färber     int intno = cs->exception_index;
136ab3b491fSBlue Swirl     trap_state *tsptr;
137ab3b491fSBlue Swirl 
13820132b96SRichard Henderson     /* Compute PSR before exposing state.  */
13920132b96SRichard Henderson     if (env->cc_op != CC_OP_FLAGS) {
14020132b96SRichard Henderson         cpu_get_psr(env);
14120132b96SRichard Henderson     }
14220132b96SRichard Henderson 
143ab3b491fSBlue Swirl #ifdef DEBUG_PCALL
144ab3b491fSBlue Swirl     if (qemu_loglevel_mask(CPU_LOG_INT)) {
145ab3b491fSBlue Swirl         static int count;
146ab3b491fSBlue Swirl         const char *name;
147ab3b491fSBlue Swirl 
1486e040755SArtyom Tarasenko         if (intno < 0 || intno >= 0x1ff) {
149ab3b491fSBlue Swirl             name = "Unknown";
1506e040755SArtyom Tarasenko         } else if (intno >= 0x180) {
1516e040755SArtyom Tarasenko             name = "Hyperprivileged Trap Instruction";
152ab3b491fSBlue Swirl         } else if (intno >= 0x100) {
153ab3b491fSBlue Swirl             name = "Trap Instruction";
154ab3b491fSBlue Swirl         } else if (intno >= 0xc0) {
155ab3b491fSBlue Swirl             name = "Window Fill";
156ab3b491fSBlue Swirl         } else if (intno >= 0x80) {
157ab3b491fSBlue Swirl             name = "Window Spill";
158ab3b491fSBlue Swirl         } else {
159ab3b491fSBlue Swirl             name = excp_names[intno];
160ab3b491fSBlue Swirl             if (!name) {
161ab3b491fSBlue Swirl                 name = "Unknown";
162ab3b491fSBlue Swirl             }
163ab3b491fSBlue Swirl         }
164ab3b491fSBlue Swirl 
165b884fc5eSRichard Henderson         qemu_log("%6d: %s (v=%04x)\n", count, name, intno);
166a0762859SAndreas Färber         log_cpu_state(cs, 0);
167ab3b491fSBlue Swirl #if 0
168ab3b491fSBlue Swirl         {
169ab3b491fSBlue Swirl             int i;
170ab3b491fSBlue Swirl             uint8_t *ptr;
171ab3b491fSBlue Swirl 
172ab3b491fSBlue Swirl             qemu_log("       code=");
173ab3b491fSBlue Swirl             ptr = (uint8_t *)env->pc;
174ab3b491fSBlue Swirl             for (i = 0; i < 16; i++) {
175ab3b491fSBlue Swirl                 qemu_log(" %02x", ldub(ptr + i));
176ab3b491fSBlue Swirl             }
177ab3b491fSBlue Swirl             qemu_log("\n");
178ab3b491fSBlue Swirl         }
179ab3b491fSBlue Swirl #endif
180ab3b491fSBlue Swirl         count++;
181ab3b491fSBlue Swirl     }
182ab3b491fSBlue Swirl #endif
183ab3b491fSBlue Swirl #if !defined(CONFIG_USER_ONLY)
184ab3b491fSBlue Swirl     if (env->tl >= env->maxtl) {
185a47dddd7SAndreas Färber         cpu_abort(cs, "Trap 0x%04x while trap level (%d) >= MAXTL (%d),"
18627103424SAndreas Färber                   " Error state", cs->exception_index, env->tl, env->maxtl);
187ab3b491fSBlue Swirl         return;
188ab3b491fSBlue Swirl     }
189ab3b491fSBlue Swirl #endif
190ab3b491fSBlue Swirl     if (env->tl < env->maxtl - 1) {
191ab3b491fSBlue Swirl         env->tl++;
192ab3b491fSBlue Swirl     } else {
193ab3b491fSBlue Swirl         env->pstate |= PS_RED;
194ab3b491fSBlue Swirl         if (env->tl < env->maxtl) {
195ab3b491fSBlue Swirl             env->tl++;
196ab3b491fSBlue Swirl         }
197ab3b491fSBlue Swirl     }
198ab3b491fSBlue Swirl     tsptr = cpu_tsptr(env);
199ab3b491fSBlue Swirl 
2007a5805a0SPeter Maydell     tsptr->tstate = sparc64_tstate(env);
201ab3b491fSBlue Swirl     tsptr->tpc = env->pc;
202ab3b491fSBlue Swirl     tsptr->tnpc = env->npc;
203ab3b491fSBlue Swirl     tsptr->tt = intno;
204ab3b491fSBlue Swirl 
2056e040755SArtyom Tarasenko     if (cpu_has_hypervisor(env)) {
2066e040755SArtyom Tarasenko         env->htstate[env->tl] = env->hpstate;
2076e040755SArtyom Tarasenko         /* XXX OpenSPARC T1 - UltraSPARC T3 have MAXPTL=2
2086e040755SArtyom Tarasenko            but this may change in the future */
2096e040755SArtyom Tarasenko         if (env->tl > 2) {
2106e040755SArtyom Tarasenko             env->hpstate |= HS_PRIV;
2116e040755SArtyom Tarasenko         }
2126e040755SArtyom Tarasenko     }
2136e040755SArtyom Tarasenko 
214576e1c4cSIgor Mammedov     if (env->def.features & CPU_FEATURE_GL) {
215cbc3a6a4SArtyom Tarasenko         cpu_gl_switch_gregs(env, env->gl + 1);
216cbc3a6a4SArtyom Tarasenko         env->gl++;
217cbc3a6a4SArtyom Tarasenko     }
218cbc3a6a4SArtyom Tarasenko 
219ab3b491fSBlue Swirl     switch (intno) {
220ab3b491fSBlue Swirl     case TT_IVEC:
2216e040755SArtyom Tarasenko         if (!cpu_has_hypervisor(env)) {
222ab3b491fSBlue Swirl             cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_IG);
2236e040755SArtyom Tarasenko         }
224ab3b491fSBlue Swirl         break;
225ab3b491fSBlue Swirl     case TT_TFAULT:
226ab3b491fSBlue Swirl     case TT_DFAULT:
227ab3b491fSBlue Swirl     case TT_TMISS ... TT_TMISS + 3:
228ab3b491fSBlue Swirl     case TT_DMISS ... TT_DMISS + 3:
229ab3b491fSBlue Swirl     case TT_DPROT ... TT_DPROT + 3:
2306e040755SArtyom Tarasenko         if (cpu_has_hypervisor(env)) {
2316e040755SArtyom Tarasenko             env->hpstate |= HS_PRIV;
2326e040755SArtyom Tarasenko             env->pstate = PS_PEF | PS_PRIV;
2336e040755SArtyom Tarasenko         } else {
234ab3b491fSBlue Swirl             cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_MG);
2356e040755SArtyom Tarasenko         }
2366e040755SArtyom Tarasenko         break;
2376e040755SArtyom Tarasenko     case TT_INSN_REAL_TRANSLATION_MISS ... TT_DATA_REAL_TRANSLATION_MISS:
2386e040755SArtyom Tarasenko     case TT_HTRAP ... TT_HTRAP + 127:
2396e040755SArtyom Tarasenko         env->hpstate |= HS_PRIV;
240ab3b491fSBlue Swirl         break;
241ab3b491fSBlue Swirl     default:
242ab3b491fSBlue Swirl         cpu_change_pstate(env, PS_PEF | PS_PRIV | PS_AG);
243ab3b491fSBlue Swirl         break;
244ab3b491fSBlue Swirl     }
245ab3b491fSBlue Swirl 
246ab3b491fSBlue Swirl     if (intno == TT_CLRWIN) {
247ab3b491fSBlue Swirl         cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - 1));
248ab3b491fSBlue Swirl     } else if ((intno & 0x1c0) == TT_SPILL) {
249ab3b491fSBlue Swirl         cpu_set_cwp(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
250ab3b491fSBlue Swirl     } else if ((intno & 0x1c0) == TT_FILL) {
251ab3b491fSBlue Swirl         cpu_set_cwp(env, cpu_cwp_inc(env, env->cwp + 1));
252ab3b491fSBlue Swirl     }
2536e040755SArtyom Tarasenko 
2546e040755SArtyom Tarasenko     if (cpu_hypervisor_mode(env)) {
2556e040755SArtyom Tarasenko         env->pc = (env->htba & ~0x3fffULL) | (intno << 5);
2566e040755SArtyom Tarasenko     } else {
257de5f1077SArtyom Tarasenko         env->pc = env->tbr  & ~0x7fffULL;
258de5f1077SArtyom Tarasenko         env->pc |= ((env->tl > 1) ? 1 << 14 : 0) | (intno << 5);
2596e040755SArtyom Tarasenko     }
260ab3b491fSBlue Swirl     env->npc = env->pc + 4;
26127103424SAndreas Färber     cs->exception_index = -1;
262ab3b491fSBlue Swirl }
2632336c1f1SBlue Swirl 
264c5f9864eSAndreas Färber trap_state *cpu_tsptr(CPUSPARCState* env)
2652336c1f1SBlue Swirl {
2662336c1f1SBlue Swirl     return &env->ts[env->tl & MAXTL_MASK];
2672336c1f1SBlue Swirl }
26879227036SBlue Swirl 
269c5f9864eSAndreas Färber static bool do_modify_softint(CPUSPARCState *env, uint32_t value)
27079227036SBlue Swirl {
27179227036SBlue Swirl     if (env->softint != value) {
27279227036SBlue Swirl         env->softint = value;
27379227036SBlue Swirl #if !defined(CONFIG_USER_ONLY)
27479227036SBlue Swirl         if (cpu_interrupts_enabled(env)) {
2755ee59930SAlex Bennée             qemu_mutex_lock_iothread();
27679227036SBlue Swirl             cpu_check_irqs(env);
2775ee59930SAlex Bennée             qemu_mutex_unlock_iothread();
27879227036SBlue Swirl         }
27979227036SBlue Swirl #endif
28011e66bcaSBlue Swirl         return true;
28179227036SBlue Swirl     }
28211e66bcaSBlue Swirl     return false;
28379227036SBlue Swirl }
28479227036SBlue Swirl 
285c5f9864eSAndreas Färber void helper_set_softint(CPUSPARCState *env, uint64_t value)
28679227036SBlue Swirl {
28711e66bcaSBlue Swirl     if (do_modify_softint(env, env->softint | (uint32_t)value)) {
28811e66bcaSBlue Swirl         trace_int_helper_set_softint(env->softint);
28911e66bcaSBlue Swirl     }
29079227036SBlue Swirl }
29179227036SBlue Swirl 
292c5f9864eSAndreas Färber void helper_clear_softint(CPUSPARCState *env, uint64_t value)
29379227036SBlue Swirl {
29411e66bcaSBlue Swirl     if (do_modify_softint(env, env->softint & (uint32_t)~value)) {
29511e66bcaSBlue Swirl         trace_int_helper_clear_softint(env->softint);
29611e66bcaSBlue Swirl     }
29779227036SBlue Swirl }
29879227036SBlue Swirl 
299c5f9864eSAndreas Färber void helper_write_softint(CPUSPARCState *env, uint64_t value)
30079227036SBlue Swirl {
30111e66bcaSBlue Swirl     if (do_modify_softint(env, (uint32_t)value)) {
30211e66bcaSBlue Swirl         trace_int_helper_write_softint(env->softint);
30311e66bcaSBlue Swirl     }
30479227036SBlue Swirl }
305