xref: /qemu/accel/tcg/user-exec.c (revision afd46fcad2dceffda35c0586f5723c127b6e09d8)
142a623c7SBlue Swirl /*
242a623c7SBlue Swirl  *  User emulator execution
342a623c7SBlue Swirl  *
442a623c7SBlue Swirl  *  Copyright (c) 2003-2005 Fabrice Bellard
542a623c7SBlue Swirl  *
642a623c7SBlue Swirl  * This library is free software; you can redistribute it and/or
742a623c7SBlue Swirl  * modify it under the terms of the GNU Lesser General Public
842a623c7SBlue Swirl  * License as published by the Free Software Foundation; either
942a623c7SBlue Swirl  * version 2 of the License, or (at your option) any later version.
1042a623c7SBlue Swirl  *
1142a623c7SBlue Swirl  * This library is distributed in the hope that it will be useful,
1242a623c7SBlue Swirl  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1342a623c7SBlue Swirl  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1442a623c7SBlue Swirl  * Lesser General Public License for more details.
1542a623c7SBlue Swirl  *
1642a623c7SBlue Swirl  * You should have received a copy of the GNU Lesser General Public
1742a623c7SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1842a623c7SBlue Swirl  */
19d38ea87aSPeter Maydell #include "qemu/osdep.h"
203e457172SBlue Swirl #include "cpu.h"
2176cad711SPaolo Bonzini #include "disas/disas.h"
2263c91552SPaolo Bonzini #include "exec/exec-all.h"
2342a623c7SBlue Swirl #include "tcg.h"
24023b0ae3SPeter Maydell #include "qemu/bitops.h"
25f08b6170SPaolo Bonzini #include "exec/cpu_ldst.h"
261652b974SPaolo Bonzini #include "translate-all.h"
27a411d296SPhilippe Mathieu-Daudé #include "exec/helper-proto.h"
2842a623c7SBlue Swirl 
2942a623c7SBlue Swirl #undef EAX
3042a623c7SBlue Swirl #undef ECX
3142a623c7SBlue Swirl #undef EDX
3242a623c7SBlue Swirl #undef EBX
3342a623c7SBlue Swirl #undef ESP
3442a623c7SBlue Swirl #undef EBP
3542a623c7SBlue Swirl #undef ESI
3642a623c7SBlue Swirl #undef EDI
3742a623c7SBlue Swirl #undef EIP
3842a623c7SBlue Swirl #ifdef __linux__
3942a623c7SBlue Swirl #include <sys/ucontext.h>
4042a623c7SBlue Swirl #endif
4142a623c7SBlue Swirl 
42ec603b55SRichard Henderson __thread uintptr_t helper_retaddr;
43ec603b55SRichard Henderson 
4442a623c7SBlue Swirl //#define DEBUG_SIGNAL
4542a623c7SBlue Swirl 
4642a623c7SBlue Swirl /* exit the current TB from a signal handler. The host registers are
4742a623c7SBlue Swirl    restored in a state compatible with the CPU emulator
4842a623c7SBlue Swirl  */
49a5852dc5SPeter Maydell static void cpu_exit_tb_from_sighandler(CPUState *cpu, sigset_t *old_set)
5042a623c7SBlue Swirl {
5142a623c7SBlue Swirl     /* XXX: use siglongjmp ? */
52a5852dc5SPeter Maydell     sigprocmask(SIG_SETMASK, old_set, NULL);
536886b980SPeter Maydell     cpu_loop_exit_noexc(cpu);
5442a623c7SBlue Swirl }
5542a623c7SBlue Swirl 
5642a623c7SBlue Swirl /* 'pc' is the host PC at which the exception was raised. 'address' is
5742a623c7SBlue Swirl    the effective address of the memory exception. 'is_write' is 1 if a
5842a623c7SBlue Swirl    write caused the exception and otherwise 0'. 'old_set' is the
5942a623c7SBlue Swirl    signal set which should be restored */
60a78b1299SPeter Maydell static inline int handle_cpu_signal(uintptr_t pc, siginfo_t *info,
61a5852dc5SPeter Maydell                                     int is_write, sigset_t *old_set)
6242a623c7SBlue Swirl {
6302bed6bdSAlex Bennée     CPUState *cpu = current_cpu;
647510454eSAndreas Färber     CPUClass *cc;
6542a623c7SBlue Swirl     int ret;
66a78b1299SPeter Maydell     unsigned long address = (unsigned long)info->si_addr;
6742a623c7SBlue Swirl 
68ec603b55SRichard Henderson     /* We must handle PC addresses from two different sources:
69ec603b55SRichard Henderson      * a call return address and a signal frame address.
70ec603b55SRichard Henderson      *
71ec603b55SRichard Henderson      * Within cpu_restore_state_from_tb we assume the former and adjust
72ec603b55SRichard Henderson      * the address by -GETPC_ADJ so that the address is within the call
73ec603b55SRichard Henderson      * insn so that addr does not accidentally match the beginning of the
74ec603b55SRichard Henderson      * next guest insn.
75ec603b55SRichard Henderson      *
76ec603b55SRichard Henderson      * However, when the PC comes from the signal frame, it points to
77ec603b55SRichard Henderson      * the actual faulting host insn and not a call insn.  Subtracting
78ec603b55SRichard Henderson      * GETPC_ADJ in that case may accidentally match the previous guest insn.
79ec603b55SRichard Henderson      *
80ec603b55SRichard Henderson      * So for the later case, adjust forward to compensate for what
81ec603b55SRichard Henderson      * will be done later by cpu_restore_state_from_tb.
82ec603b55SRichard Henderson      */
83ec603b55SRichard Henderson     if (helper_retaddr) {
84ec603b55SRichard Henderson         pc = helper_retaddr;
85ec603b55SRichard Henderson     } else {
86ec603b55SRichard Henderson         pc += GETPC_ADJ;
87ec603b55SRichard Henderson     }
88ec603b55SRichard Henderson 
8902bed6bdSAlex Bennée     /* For synchronous signals we expect to be coming from the vCPU
9002bed6bdSAlex Bennée      * thread (so current_cpu should be valid) and either from running
9102bed6bdSAlex Bennée      * code or during translation which can fault as we cross pages.
9202bed6bdSAlex Bennée      *
9302bed6bdSAlex Bennée      * If neither is true then something has gone wrong and we should
9402bed6bdSAlex Bennée      * abort rather than try and restart the vCPU execution.
9502bed6bdSAlex Bennée      */
9602bed6bdSAlex Bennée     if (!cpu || !cpu->running) {
9702bed6bdSAlex Bennée         printf("qemu:%s received signal outside vCPU context @ pc=0x%"
9802bed6bdSAlex Bennée                PRIxPTR "\n",  __func__, pc);
9902bed6bdSAlex Bennée         abort();
10002bed6bdSAlex Bennée     }
10102bed6bdSAlex Bennée 
10242a623c7SBlue Swirl #if defined(DEBUG_SIGNAL)
10371baf787SPeter Maydell     printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
10442a623c7SBlue Swirl            pc, address, is_write, *(unsigned long *)old_set);
10542a623c7SBlue Swirl #endif
10642a623c7SBlue Swirl     /* XXX: locking issue */
1079c4bbee9SPeter Maydell     /* Note that it is important that we don't call page_unprotect() unless
1089c4bbee9SPeter Maydell      * this is really a "write to nonwriteable page" fault, because
1099c4bbee9SPeter Maydell      * page_unprotect() assumes that if it is called for an access to
1109c4bbee9SPeter Maydell      * a page that's writeable this means we had two threads racing and
1119c4bbee9SPeter Maydell      * another thread got there first and already made the page writeable;
1129c4bbee9SPeter Maydell      * so we will retry the access. If we were to call page_unprotect()
1139c4bbee9SPeter Maydell      * for some other kind of fault that should really be passed to the
1149c4bbee9SPeter Maydell      * guest, we'd end up in an infinite loop of retrying the faulting
1159c4bbee9SPeter Maydell      * access.
1169c4bbee9SPeter Maydell      */
1179c4bbee9SPeter Maydell     if (is_write && info->si_signo == SIGSEGV && info->si_code == SEGV_ACCERR &&
1189c4bbee9SPeter Maydell         h2g_valid(address)) {
119f213e72fSPeter Maydell         switch (page_unprotect(h2g(address), pc)) {
120f213e72fSPeter Maydell         case 0:
121f213e72fSPeter Maydell             /* Fault not caused by a page marked unwritable to protect
122ec603b55SRichard Henderson              * cached translations, must be the guest binary's problem.
123f213e72fSPeter Maydell              */
124f213e72fSPeter Maydell             break;
125f213e72fSPeter Maydell         case 1:
126f213e72fSPeter Maydell             /* Fault caused by protection of cached translation; TBs
127ec603b55SRichard Henderson              * invalidated, so resume execution.  Retain helper_retaddr
128ec603b55SRichard Henderson              * for a possible second fault.
129f213e72fSPeter Maydell              */
13042a623c7SBlue Swirl             return 1;
131f213e72fSPeter Maydell         case 2:
132f213e72fSPeter Maydell             /* Fault caused by protection of cached translation, and the
133f213e72fSPeter Maydell              * currently executing TB was modified and must be exited
134ec603b55SRichard Henderson              * immediately.  Clear helper_retaddr for next execution.
135f213e72fSPeter Maydell              */
136ec603b55SRichard Henderson             helper_retaddr = 0;
13702bed6bdSAlex Bennée             cpu_exit_tb_from_sighandler(cpu, old_set);
138ec603b55SRichard Henderson             /* NORETURN */
139ec603b55SRichard Henderson 
140f213e72fSPeter Maydell         default:
141f213e72fSPeter Maydell             g_assert_not_reached();
142f213e72fSPeter Maydell         }
14342a623c7SBlue Swirl     }
14442a623c7SBlue Swirl 
145732f9e89SAlexander Graf     /* Convert forcefully to guest address space, invalid addresses
146732f9e89SAlexander Graf        are still valid segv ones */
147732f9e89SAlexander Graf     address = h2g_nocheck(address);
148732f9e89SAlexander Graf 
1497510454eSAndreas Färber     cc = CPU_GET_CLASS(cpu);
15042a623c7SBlue Swirl     /* see if it is an MMU fault */
1517510454eSAndreas Färber     g_assert(cc->handle_mmu_fault);
15298670d47SLaurent Vivier     ret = cc->handle_mmu_fault(cpu, address, 0, is_write, MMU_USER_IDX);
153ec603b55SRichard Henderson 
154ec603b55SRichard Henderson     if (ret == 0) {
155ec603b55SRichard Henderson         /* The MMU fault was handled without causing real CPU fault.
156ec603b55SRichard Henderson          *  Retain helper_retaddr for a possible second fault.
157ec603b55SRichard Henderson          */
158ec603b55SRichard Henderson         return 1;
159ec603b55SRichard Henderson     }
160ec603b55SRichard Henderson 
161ec603b55SRichard Henderson     /* All other paths lead to cpu_exit; clear helper_retaddr
162ec603b55SRichard Henderson      * for next execution.
163ec603b55SRichard Henderson      */
164ec603b55SRichard Henderson     helper_retaddr = 0;
165ec603b55SRichard Henderson 
16642a623c7SBlue Swirl     if (ret < 0) {
16742a623c7SBlue Swirl         return 0; /* not an MMU fault */
16842a623c7SBlue Swirl     }
16901ecaf43SRichard Henderson 
170ec603b55SRichard Henderson     /* Now we have a real cpu fault.  */
171*afd46fcaSPavel Dovgalyuk     cpu_restore_state(cpu, pc, true);
17242a623c7SBlue Swirl 
17342a623c7SBlue Swirl     sigprocmask(SIG_SETMASK, old_set, NULL);
1740c33682dSPeter Maydell     cpu_loop_exit(cpu);
17542a623c7SBlue Swirl 
17642a623c7SBlue Swirl     /* never comes here */
17742a623c7SBlue Swirl     return 1;
17842a623c7SBlue Swirl }
17942a623c7SBlue Swirl 
18042a623c7SBlue Swirl #if defined(__i386__)
18142a623c7SBlue Swirl 
182c5679026SPeter Maydell #if defined(__NetBSD__)
18342a623c7SBlue Swirl #include <ucontext.h>
18442a623c7SBlue Swirl 
18542a623c7SBlue Swirl #define EIP_sig(context)     ((context)->uc_mcontext.__gregs[_REG_EIP])
18642a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
18742a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->uc_mcontext.__gregs[_REG_ERR])
18842a623c7SBlue Swirl #define MASK_sig(context)    ((context)->uc_sigmask)
18942a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__)
19042a623c7SBlue Swirl #include <ucontext.h>
19142a623c7SBlue Swirl 
19242a623c7SBlue Swirl #define EIP_sig(context)  (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
19342a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->uc_mcontext.mc_trapno)
19442a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->uc_mcontext.mc_err)
19542a623c7SBlue Swirl #define MASK_sig(context)    ((context)->uc_sigmask)
19642a623c7SBlue Swirl #elif defined(__OpenBSD__)
19742a623c7SBlue Swirl #define EIP_sig(context)     ((context)->sc_eip)
19842a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->sc_trapno)
19942a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->sc_err)
20042a623c7SBlue Swirl #define MASK_sig(context)    ((context)->sc_mask)
20142a623c7SBlue Swirl #else
20242a623c7SBlue Swirl #define EIP_sig(context)     ((context)->uc_mcontext.gregs[REG_EIP])
20342a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->uc_mcontext.gregs[REG_TRAPNO])
20442a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->uc_mcontext.gregs[REG_ERR])
20542a623c7SBlue Swirl #define MASK_sig(context)    ((context)->uc_sigmask)
20642a623c7SBlue Swirl #endif
20742a623c7SBlue Swirl 
20842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
20942a623c7SBlue Swirl                        void *puc)
21042a623c7SBlue Swirl {
21142a623c7SBlue Swirl     siginfo_t *info = pinfo;
21242a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
21342a623c7SBlue Swirl     ucontext_t *uc = puc;
21442a623c7SBlue Swirl #elif defined(__OpenBSD__)
21542a623c7SBlue Swirl     struct sigcontext *uc = puc;
21642a623c7SBlue Swirl #else
21704b33e21SKhem Raj     ucontext_t *uc = puc;
21842a623c7SBlue Swirl #endif
21942a623c7SBlue Swirl     unsigned long pc;
22042a623c7SBlue Swirl     int trapno;
22142a623c7SBlue Swirl 
22242a623c7SBlue Swirl #ifndef REG_EIP
22342a623c7SBlue Swirl /* for glibc 2.1 */
22442a623c7SBlue Swirl #define REG_EIP    EIP
22542a623c7SBlue Swirl #define REG_ERR    ERR
22642a623c7SBlue Swirl #define REG_TRAPNO TRAPNO
22742a623c7SBlue Swirl #endif
22842a623c7SBlue Swirl     pc = EIP_sig(uc);
22942a623c7SBlue Swirl     trapno = TRAP_sig(uc);
230a78b1299SPeter Maydell     return handle_cpu_signal(pc, info,
231a78b1299SPeter Maydell                              trapno == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
232a5852dc5SPeter Maydell                              &MASK_sig(uc));
23342a623c7SBlue Swirl }
23442a623c7SBlue Swirl 
23542a623c7SBlue Swirl #elif defined(__x86_64__)
23642a623c7SBlue Swirl 
23742a623c7SBlue Swirl #ifdef __NetBSD__
23842a623c7SBlue Swirl #define PC_sig(context)       _UC_MACHINE_PC(context)
23942a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
24042a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->uc_mcontext.__gregs[_REG_ERR])
24142a623c7SBlue Swirl #define MASK_sig(context)     ((context)->uc_sigmask)
24242a623c7SBlue Swirl #elif defined(__OpenBSD__)
24342a623c7SBlue Swirl #define PC_sig(context)       ((context)->sc_rip)
24442a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->sc_trapno)
24542a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->sc_err)
24642a623c7SBlue Swirl #define MASK_sig(context)     ((context)->sc_mask)
24742a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__)
24842a623c7SBlue Swirl #include <ucontext.h>
24942a623c7SBlue Swirl 
25042a623c7SBlue Swirl #define PC_sig(context)  (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
25142a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->uc_mcontext.mc_trapno)
25242a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->uc_mcontext.mc_err)
25342a623c7SBlue Swirl #define MASK_sig(context)     ((context)->uc_sigmask)
25442a623c7SBlue Swirl #else
25542a623c7SBlue Swirl #define PC_sig(context)       ((context)->uc_mcontext.gregs[REG_RIP])
25642a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->uc_mcontext.gregs[REG_TRAPNO])
25742a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->uc_mcontext.gregs[REG_ERR])
25842a623c7SBlue Swirl #define MASK_sig(context)     ((context)->uc_sigmask)
25942a623c7SBlue Swirl #endif
26042a623c7SBlue Swirl 
26142a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
26242a623c7SBlue Swirl                        void *puc)
26342a623c7SBlue Swirl {
26442a623c7SBlue Swirl     siginfo_t *info = pinfo;
26542a623c7SBlue Swirl     unsigned long pc;
26642a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
26742a623c7SBlue Swirl     ucontext_t *uc = puc;
26842a623c7SBlue Swirl #elif defined(__OpenBSD__)
26942a623c7SBlue Swirl     struct sigcontext *uc = puc;
27042a623c7SBlue Swirl #else
27104b33e21SKhem Raj     ucontext_t *uc = puc;
27242a623c7SBlue Swirl #endif
27342a623c7SBlue Swirl 
27442a623c7SBlue Swirl     pc = PC_sig(uc);
275a78b1299SPeter Maydell     return handle_cpu_signal(pc, info,
276a78b1299SPeter Maydell                              TRAP_sig(uc) == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
277a5852dc5SPeter Maydell                              &MASK_sig(uc));
27842a623c7SBlue Swirl }
27942a623c7SBlue Swirl 
28042a623c7SBlue Swirl #elif defined(_ARCH_PPC)
28142a623c7SBlue Swirl 
28242a623c7SBlue Swirl /***********************************************************************
28342a623c7SBlue Swirl  * signal context platform-specific definitions
28442a623c7SBlue Swirl  * From Wine
28542a623c7SBlue Swirl  */
28642a623c7SBlue Swirl #ifdef linux
28742a623c7SBlue Swirl /* All Registers access - only for local access */
28842a623c7SBlue Swirl #define REG_sig(reg_name, context)              \
28942a623c7SBlue Swirl     ((context)->uc_mcontext.regs->reg_name)
29042a623c7SBlue Swirl /* Gpr Registers access  */
29142a623c7SBlue Swirl #define GPR_sig(reg_num, context)              REG_sig(gpr[reg_num], context)
29242a623c7SBlue Swirl /* Program counter */
29342a623c7SBlue Swirl #define IAR_sig(context)                       REG_sig(nip, context)
29442a623c7SBlue Swirl /* Machine State Register (Supervisor) */
29542a623c7SBlue Swirl #define MSR_sig(context)                       REG_sig(msr, context)
29642a623c7SBlue Swirl /* Count register */
29742a623c7SBlue Swirl #define CTR_sig(context)                       REG_sig(ctr, context)
29842a623c7SBlue Swirl /* User's integer exception register */
29942a623c7SBlue Swirl #define XER_sig(context)                       REG_sig(xer, context)
30042a623c7SBlue Swirl /* Link register */
30142a623c7SBlue Swirl #define LR_sig(context)                        REG_sig(link, context)
30242a623c7SBlue Swirl /* Condition register */
30342a623c7SBlue Swirl #define CR_sig(context)                        REG_sig(ccr, context)
30442a623c7SBlue Swirl 
30542a623c7SBlue Swirl /* Float Registers access  */
30642a623c7SBlue Swirl #define FLOAT_sig(reg_num, context)                                     \
30742a623c7SBlue Swirl     (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
30842a623c7SBlue Swirl #define FPSCR_sig(context) \
30942a623c7SBlue Swirl     (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
31042a623c7SBlue Swirl /* Exception Registers access */
31142a623c7SBlue Swirl #define DAR_sig(context)                       REG_sig(dar, context)
31242a623c7SBlue Swirl #define DSISR_sig(context)                     REG_sig(dsisr, context)
31342a623c7SBlue Swirl #define TRAP_sig(context)                      REG_sig(trap, context)
31442a623c7SBlue Swirl #endif /* linux */
31542a623c7SBlue Swirl 
31642a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
31742a623c7SBlue Swirl #include <ucontext.h>
31842a623c7SBlue Swirl #define IAR_sig(context)               ((context)->uc_mcontext.mc_srr0)
31942a623c7SBlue Swirl #define MSR_sig(context)               ((context)->uc_mcontext.mc_srr1)
32042a623c7SBlue Swirl #define CTR_sig(context)               ((context)->uc_mcontext.mc_ctr)
32142a623c7SBlue Swirl #define XER_sig(context)               ((context)->uc_mcontext.mc_xer)
32242a623c7SBlue Swirl #define LR_sig(context)                ((context)->uc_mcontext.mc_lr)
32342a623c7SBlue Swirl #define CR_sig(context)                ((context)->uc_mcontext.mc_cr)
32442a623c7SBlue Swirl /* Exception Registers access */
32542a623c7SBlue Swirl #define DAR_sig(context)               ((context)->uc_mcontext.mc_dar)
32642a623c7SBlue Swirl #define DSISR_sig(context)             ((context)->uc_mcontext.mc_dsisr)
32742a623c7SBlue Swirl #define TRAP_sig(context)              ((context)->uc_mcontext.mc_exc)
32842a623c7SBlue Swirl #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
32942a623c7SBlue Swirl 
33042a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
33142a623c7SBlue Swirl                        void *puc)
33242a623c7SBlue Swirl {
33342a623c7SBlue Swirl     siginfo_t *info = pinfo;
33442a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
33542a623c7SBlue Swirl     ucontext_t *uc = puc;
33642a623c7SBlue Swirl #else
33704b33e21SKhem Raj     ucontext_t *uc = puc;
33842a623c7SBlue Swirl #endif
33942a623c7SBlue Swirl     unsigned long pc;
34042a623c7SBlue Swirl     int is_write;
34142a623c7SBlue Swirl 
34242a623c7SBlue Swirl     pc = IAR_sig(uc);
34342a623c7SBlue Swirl     is_write = 0;
34442a623c7SBlue Swirl #if 0
34542a623c7SBlue Swirl     /* ppc 4xx case */
34642a623c7SBlue Swirl     if (DSISR_sig(uc) & 0x00800000) {
34742a623c7SBlue Swirl         is_write = 1;
34842a623c7SBlue Swirl     }
34942a623c7SBlue Swirl #else
35042a623c7SBlue Swirl     if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
35142a623c7SBlue Swirl         is_write = 1;
35242a623c7SBlue Swirl     }
35342a623c7SBlue Swirl #endif
354a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
35542a623c7SBlue Swirl }
35642a623c7SBlue Swirl 
35742a623c7SBlue Swirl #elif defined(__alpha__)
35842a623c7SBlue Swirl 
35942a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
36042a623c7SBlue Swirl                            void *puc)
36142a623c7SBlue Swirl {
36242a623c7SBlue Swirl     siginfo_t *info = pinfo;
36304b33e21SKhem Raj     ucontext_t *uc = puc;
36442a623c7SBlue Swirl     uint32_t *pc = uc->uc_mcontext.sc_pc;
36542a623c7SBlue Swirl     uint32_t insn = *pc;
36642a623c7SBlue Swirl     int is_write = 0;
36742a623c7SBlue Swirl 
36842a623c7SBlue Swirl     /* XXX: need kernel patch to get write flag faster */
36942a623c7SBlue Swirl     switch (insn >> 26) {
37042a623c7SBlue Swirl     case 0x0d: /* stw */
37142a623c7SBlue Swirl     case 0x0e: /* stb */
37242a623c7SBlue Swirl     case 0x0f: /* stq_u */
37342a623c7SBlue Swirl     case 0x24: /* stf */
37442a623c7SBlue Swirl     case 0x25: /* stg */
37542a623c7SBlue Swirl     case 0x26: /* sts */
37642a623c7SBlue Swirl     case 0x27: /* stt */
37742a623c7SBlue Swirl     case 0x2c: /* stl */
37842a623c7SBlue Swirl     case 0x2d: /* stq */
37942a623c7SBlue Swirl     case 0x2e: /* stl_c */
38042a623c7SBlue Swirl     case 0x2f: /* stq_c */
38142a623c7SBlue Swirl         is_write = 1;
38242a623c7SBlue Swirl     }
38342a623c7SBlue Swirl 
384a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
38542a623c7SBlue Swirl }
38642a623c7SBlue Swirl #elif defined(__sparc__)
38742a623c7SBlue Swirl 
38842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
38942a623c7SBlue Swirl                        void *puc)
39042a623c7SBlue Swirl {
39142a623c7SBlue Swirl     siginfo_t *info = pinfo;
39242a623c7SBlue Swirl     int is_write;
39342a623c7SBlue Swirl     uint32_t insn;
39442a623c7SBlue Swirl #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
39542a623c7SBlue Swirl     uint32_t *regs = (uint32_t *)(info + 1);
39642a623c7SBlue Swirl     void *sigmask = (regs + 20);
39742a623c7SBlue Swirl     /* XXX: is there a standard glibc define ? */
39842a623c7SBlue Swirl     unsigned long pc = regs[1];
39942a623c7SBlue Swirl #else
40042a623c7SBlue Swirl #ifdef __linux__
40142a623c7SBlue Swirl     struct sigcontext *sc = puc;
40242a623c7SBlue Swirl     unsigned long pc = sc->sigc_regs.tpc;
40342a623c7SBlue Swirl     void *sigmask = (void *)sc->sigc_mask;
40442a623c7SBlue Swirl #elif defined(__OpenBSD__)
40542a623c7SBlue Swirl     struct sigcontext *uc = puc;
40642a623c7SBlue Swirl     unsigned long pc = uc->sc_pc;
40742a623c7SBlue Swirl     void *sigmask = (void *)(long)uc->sc_mask;
4087ccfb495STobias Nygren #elif defined(__NetBSD__)
4097ccfb495STobias Nygren     ucontext_t *uc = puc;
4107ccfb495STobias Nygren     unsigned long pc = _UC_MACHINE_PC(uc);
4117ccfb495STobias Nygren     void *sigmask = (void *)&uc->uc_sigmask;
41242a623c7SBlue Swirl #endif
41342a623c7SBlue Swirl #endif
41442a623c7SBlue Swirl 
41542a623c7SBlue Swirl     /* XXX: need kernel patch to get write flag faster */
41642a623c7SBlue Swirl     is_write = 0;
41742a623c7SBlue Swirl     insn = *(uint32_t *)pc;
41842a623c7SBlue Swirl     if ((insn >> 30) == 3) {
41942a623c7SBlue Swirl         switch ((insn >> 19) & 0x3f) {
42042a623c7SBlue Swirl         case 0x05: /* stb */
42142a623c7SBlue Swirl         case 0x15: /* stba */
42242a623c7SBlue Swirl         case 0x06: /* sth */
42342a623c7SBlue Swirl         case 0x16: /* stha */
42442a623c7SBlue Swirl         case 0x04: /* st */
42542a623c7SBlue Swirl         case 0x14: /* sta */
42642a623c7SBlue Swirl         case 0x07: /* std */
42742a623c7SBlue Swirl         case 0x17: /* stda */
42842a623c7SBlue Swirl         case 0x0e: /* stx */
42942a623c7SBlue Swirl         case 0x1e: /* stxa */
43042a623c7SBlue Swirl         case 0x24: /* stf */
43142a623c7SBlue Swirl         case 0x34: /* stfa */
43242a623c7SBlue Swirl         case 0x27: /* stdf */
43342a623c7SBlue Swirl         case 0x37: /* stdfa */
43442a623c7SBlue Swirl         case 0x26: /* stqf */
43542a623c7SBlue Swirl         case 0x36: /* stqfa */
43642a623c7SBlue Swirl         case 0x25: /* stfsr */
43742a623c7SBlue Swirl         case 0x3c: /* casa */
43842a623c7SBlue Swirl         case 0x3e: /* casxa */
43942a623c7SBlue Swirl             is_write = 1;
44042a623c7SBlue Swirl             break;
44142a623c7SBlue Swirl         }
44242a623c7SBlue Swirl     }
443a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, sigmask);
44442a623c7SBlue Swirl }
44542a623c7SBlue Swirl 
44642a623c7SBlue Swirl #elif defined(__arm__)
44742a623c7SBlue Swirl 
4487ccfb495STobias Nygren #if defined(__NetBSD__)
4497ccfb495STobias Nygren #include <ucontext.h>
4507ccfb495STobias Nygren #endif
4517ccfb495STobias Nygren 
45242a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
45342a623c7SBlue Swirl                        void *puc)
45442a623c7SBlue Swirl {
45542a623c7SBlue Swirl     siginfo_t *info = pinfo;
4567ccfb495STobias Nygren #if defined(__NetBSD__)
4577ccfb495STobias Nygren     ucontext_t *uc = puc;
4587ccfb495STobias Nygren #else
45904b33e21SKhem Raj     ucontext_t *uc = puc;
4607ccfb495STobias Nygren #endif
46142a623c7SBlue Swirl     unsigned long pc;
46242a623c7SBlue Swirl     int is_write;
46342a623c7SBlue Swirl 
4647ccfb495STobias Nygren #if defined(__NetBSD__)
4657ccfb495STobias Nygren     pc = uc->uc_mcontext.__gregs[_REG_R15];
4667ccfb495STobias Nygren #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
46742a623c7SBlue Swirl     pc = uc->uc_mcontext.gregs[R15];
46842a623c7SBlue Swirl #else
46942a623c7SBlue Swirl     pc = uc->uc_mcontext.arm_pc;
47042a623c7SBlue Swirl #endif
471023b0ae3SPeter Maydell 
472023b0ae3SPeter Maydell     /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
473023b0ae3SPeter Maydell      * later processor; on v5 we will always report this as a read).
474023b0ae3SPeter Maydell      */
475023b0ae3SPeter Maydell     is_write = extract32(uc->uc_mcontext.error_code, 11, 1);
476a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
47742a623c7SBlue Swirl }
47842a623c7SBlue Swirl 
479f129061cSClaudio Fontana #elif defined(__aarch64__)
480f129061cSClaudio Fontana 
481661f7fa4SRichard Henderson int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
482f129061cSClaudio Fontana {
483f129061cSClaudio Fontana     siginfo_t *info = pinfo;
48404b33e21SKhem Raj     ucontext_t *uc = puc;
485661f7fa4SRichard Henderson     uintptr_t pc = uc->uc_mcontext.pc;
486661f7fa4SRichard Henderson     uint32_t insn = *(uint32_t *)pc;
487661f7fa4SRichard Henderson     bool is_write;
488f129061cSClaudio Fontana 
489661f7fa4SRichard Henderson     /* XXX: need kernel patch to get write flag faster.  */
490661f7fa4SRichard Henderson     is_write = (   (insn & 0xbfff0000) == 0x0c000000   /* C3.3.1 */
491661f7fa4SRichard Henderson                 || (insn & 0xbfe00000) == 0x0c800000   /* C3.3.2 */
492661f7fa4SRichard Henderson                 || (insn & 0xbfdf0000) == 0x0d000000   /* C3.3.3 */
493661f7fa4SRichard Henderson                 || (insn & 0xbfc00000) == 0x0d800000   /* C3.3.4 */
494661f7fa4SRichard Henderson                 || (insn & 0x3f400000) == 0x08000000   /* C3.3.6 */
495661f7fa4SRichard Henderson                 || (insn & 0x3bc00000) == 0x39000000   /* C3.3.13 */
496661f7fa4SRichard Henderson                 || (insn & 0x3fc00000) == 0x3d800000   /* ... 128bit */
497661f7fa4SRichard Henderson                 /* Ingore bits 10, 11 & 21, controlling indexing.  */
498661f7fa4SRichard Henderson                 || (insn & 0x3bc00000) == 0x38000000   /* C3.3.8-12 */
499661f7fa4SRichard Henderson                 || (insn & 0x3fe00000) == 0x3c800000   /* ... 128bit */
500661f7fa4SRichard Henderson                 /* Ignore bits 23 & 24, controlling indexing.  */
501661f7fa4SRichard Henderson                 || (insn & 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
502661f7fa4SRichard Henderson 
503a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
504f129061cSClaudio Fontana }
505f129061cSClaudio Fontana 
50642a623c7SBlue Swirl #elif defined(__s390__)
50742a623c7SBlue Swirl 
50842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
50942a623c7SBlue Swirl                        void *puc)
51042a623c7SBlue Swirl {
51142a623c7SBlue Swirl     siginfo_t *info = pinfo;
51204b33e21SKhem Raj     ucontext_t *uc = puc;
51342a623c7SBlue Swirl     unsigned long pc;
51442a623c7SBlue Swirl     uint16_t *pinsn;
51542a623c7SBlue Swirl     int is_write = 0;
51642a623c7SBlue Swirl 
51742a623c7SBlue Swirl     pc = uc->uc_mcontext.psw.addr;
51842a623c7SBlue Swirl 
51942a623c7SBlue Swirl     /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
52042a623c7SBlue Swirl        of the normal 2 arguments.  The 3rd argument contains the "int_code"
52142a623c7SBlue Swirl        from the hardware which does in fact contain the is_write value.
52242a623c7SBlue Swirl        The rt signal handler, as far as I can tell, does not give this value
52342a623c7SBlue Swirl        at all.  Not that we could get to it from here even if it were.  */
52442a623c7SBlue Swirl     /* ??? This is not even close to complete, since it ignores all
52542a623c7SBlue Swirl        of the read-modify-write instructions.  */
52642a623c7SBlue Swirl     pinsn = (uint16_t *)pc;
52742a623c7SBlue Swirl     switch (pinsn[0] >> 8) {
52842a623c7SBlue Swirl     case 0x50: /* ST */
52942a623c7SBlue Swirl     case 0x42: /* STC */
53042a623c7SBlue Swirl     case 0x40: /* STH */
53142a623c7SBlue Swirl         is_write = 1;
53242a623c7SBlue Swirl         break;
53342a623c7SBlue Swirl     case 0xc4: /* RIL format insns */
53442a623c7SBlue Swirl         switch (pinsn[0] & 0xf) {
53542a623c7SBlue Swirl         case 0xf: /* STRL */
53642a623c7SBlue Swirl         case 0xb: /* STGRL */
53742a623c7SBlue Swirl         case 0x7: /* STHRL */
53842a623c7SBlue Swirl             is_write = 1;
53942a623c7SBlue Swirl         }
54042a623c7SBlue Swirl         break;
54142a623c7SBlue Swirl     case 0xe3: /* RXY format insns */
54242a623c7SBlue Swirl         switch (pinsn[2] & 0xff) {
54342a623c7SBlue Swirl         case 0x50: /* STY */
54442a623c7SBlue Swirl         case 0x24: /* STG */
54542a623c7SBlue Swirl         case 0x72: /* STCY */
54642a623c7SBlue Swirl         case 0x70: /* STHY */
54742a623c7SBlue Swirl         case 0x8e: /* STPQ */
54842a623c7SBlue Swirl         case 0x3f: /* STRVH */
54942a623c7SBlue Swirl         case 0x3e: /* STRV */
55042a623c7SBlue Swirl         case 0x2f: /* STRVG */
55142a623c7SBlue Swirl             is_write = 1;
55242a623c7SBlue Swirl         }
55342a623c7SBlue Swirl         break;
55442a623c7SBlue Swirl     }
555a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
55642a623c7SBlue Swirl }
55742a623c7SBlue Swirl 
55842a623c7SBlue Swirl #elif defined(__mips__)
55942a623c7SBlue Swirl 
56042a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
56142a623c7SBlue Swirl                        void *puc)
56242a623c7SBlue Swirl {
56342a623c7SBlue Swirl     siginfo_t *info = pinfo;
56404b33e21SKhem Raj     ucontext_t *uc = puc;
56542a623c7SBlue Swirl     greg_t pc = uc->uc_mcontext.pc;
56642a623c7SBlue Swirl     int is_write;
56742a623c7SBlue Swirl 
56842a623c7SBlue Swirl     /* XXX: compute is_write */
56942a623c7SBlue Swirl     is_write = 0;
570a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
57142a623c7SBlue Swirl }
57242a623c7SBlue Swirl 
57342a623c7SBlue Swirl #else
57442a623c7SBlue Swirl 
57542a623c7SBlue Swirl #error host CPU specific signal handler needed
57642a623c7SBlue Swirl 
57742a623c7SBlue Swirl #endif
578a411d296SPhilippe Mathieu-Daudé 
579a411d296SPhilippe Mathieu-Daudé /* The softmmu versions of these helpers are in cputlb.c.  */
580a411d296SPhilippe Mathieu-Daudé 
581a411d296SPhilippe Mathieu-Daudé /* Do not allow unaligned operations to proceed.  Return the host address.  */
582a411d296SPhilippe Mathieu-Daudé static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
583a411d296SPhilippe Mathieu-Daudé                                int size, uintptr_t retaddr)
584a411d296SPhilippe Mathieu-Daudé {
585a411d296SPhilippe Mathieu-Daudé     /* Enforce qemu required alignment.  */
586a411d296SPhilippe Mathieu-Daudé     if (unlikely(addr & (size - 1))) {
587a411d296SPhilippe Mathieu-Daudé         cpu_loop_exit_atomic(ENV_GET_CPU(env), retaddr);
588a411d296SPhilippe Mathieu-Daudé     }
589ec603b55SRichard Henderson     helper_retaddr = retaddr;
590a411d296SPhilippe Mathieu-Daudé     return g2h(addr);
591a411d296SPhilippe Mathieu-Daudé }
592a411d296SPhilippe Mathieu-Daudé 
593a411d296SPhilippe Mathieu-Daudé /* Macro to call the above, with local variables from the use context.  */
59434d49937SPeter Maydell #define ATOMIC_MMU_DECLS do {} while (0)
595a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP  atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC())
596ec603b55SRichard Henderson #define ATOMIC_MMU_CLEANUP do { helper_retaddr = 0; } while (0)
597a411d296SPhilippe Mathieu-Daudé 
598a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X)   HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
599a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS
600a411d296SPhilippe Mathieu-Daudé 
601a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 1
602a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
603a411d296SPhilippe Mathieu-Daudé 
604a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 2
605a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
606a411d296SPhilippe Mathieu-Daudé 
607a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 4
608a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
609a411d296SPhilippe Mathieu-Daudé 
610a411d296SPhilippe Mathieu-Daudé #ifdef CONFIG_ATOMIC64
611a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 8
612a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
613a411d296SPhilippe Mathieu-Daudé #endif
614a411d296SPhilippe Mathieu-Daudé 
615a411d296SPhilippe Mathieu-Daudé /* The following is only callable from other helpers, and matches up
616a411d296SPhilippe Mathieu-Daudé    with the softmmu version.  */
617a411d296SPhilippe Mathieu-Daudé 
618a411d296SPhilippe Mathieu-Daudé #ifdef CONFIG_ATOMIC128
619a411d296SPhilippe Mathieu-Daudé 
620a411d296SPhilippe Mathieu-Daudé #undef EXTRA_ARGS
621a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_NAME
622a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_MMU_LOOKUP
623a411d296SPhilippe Mathieu-Daudé 
624a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS     , TCGMemOpIdx oi, uintptr_t retaddr
625a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X) \
626a411d296SPhilippe Mathieu-Daudé     HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
627a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP  atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr)
628a411d296SPhilippe Mathieu-Daudé 
629a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 16
630a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
631a411d296SPhilippe Mathieu-Daudé #endif /* CONFIG_ATOMIC128 */
632