xref: /qemu/accel/tcg/user-exec.c (revision ca86cf328ce216bb304bbf09a43614613f945d86)
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
9fb0343d5SThomas Huth  * version 2.1 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"
28e6cd4bb5SRichard Henderson #include "qemu/atomic128.h"
2942a623c7SBlue Swirl 
3042a623c7SBlue Swirl #undef EAX
3142a623c7SBlue Swirl #undef ECX
3242a623c7SBlue Swirl #undef EDX
3342a623c7SBlue Swirl #undef EBX
3442a623c7SBlue Swirl #undef ESP
3542a623c7SBlue Swirl #undef EBP
3642a623c7SBlue Swirl #undef ESI
3742a623c7SBlue Swirl #undef EDI
3842a623c7SBlue Swirl #undef EIP
3942a623c7SBlue Swirl #ifdef __linux__
4042a623c7SBlue Swirl #include <sys/ucontext.h>
4142a623c7SBlue Swirl #endif
4242a623c7SBlue Swirl 
43ec603b55SRichard Henderson __thread uintptr_t helper_retaddr;
44ec603b55SRichard Henderson 
4542a623c7SBlue Swirl //#define DEBUG_SIGNAL
4642a623c7SBlue Swirl 
4742a623c7SBlue Swirl /* exit the current TB from a signal handler. The host registers are
4842a623c7SBlue Swirl    restored in a state compatible with the CPU emulator
4942a623c7SBlue Swirl  */
50a5852dc5SPeter Maydell static void cpu_exit_tb_from_sighandler(CPUState *cpu, sigset_t *old_set)
5142a623c7SBlue Swirl {
5242a623c7SBlue Swirl     /* XXX: use siglongjmp ? */
53a5852dc5SPeter Maydell     sigprocmask(SIG_SETMASK, old_set, NULL);
546886b980SPeter Maydell     cpu_loop_exit_noexc(cpu);
5542a623c7SBlue Swirl }
5642a623c7SBlue Swirl 
5742a623c7SBlue Swirl /* 'pc' is the host PC at which the exception was raised. 'address' is
5842a623c7SBlue Swirl    the effective address of the memory exception. 'is_write' is 1 if a
5942a623c7SBlue Swirl    write caused the exception and otherwise 0'. 'old_set' is the
6042a623c7SBlue Swirl    signal set which should be restored */
61a78b1299SPeter Maydell static inline int handle_cpu_signal(uintptr_t pc, siginfo_t *info,
62a5852dc5SPeter Maydell                                     int is_write, sigset_t *old_set)
6342a623c7SBlue Swirl {
6402bed6bdSAlex Bennée     CPUState *cpu = current_cpu;
657510454eSAndreas Färber     CPUClass *cc;
66a78b1299SPeter Maydell     unsigned long address = (unsigned long)info->si_addr;
6752ba13f0SRichard Henderson     MMUAccessType access_type = is_write ? MMU_DATA_STORE : MMU_DATA_LOAD;
6842a623c7SBlue Swirl 
6952ba13f0SRichard Henderson     switch (helper_retaddr) {
7052ba13f0SRichard Henderson     default:
7152ba13f0SRichard Henderson         /*
7252ba13f0SRichard Henderson          * Fault during host memory operation within a helper function.
7352ba13f0SRichard Henderson          * The helper's host return address, saved here, gives us a
7452ba13f0SRichard Henderson          * pointer into the generated code that will unwind to the
7552ba13f0SRichard Henderson          * correct guest pc.
76ec603b55SRichard Henderson          */
77ec603b55SRichard Henderson         pc = helper_retaddr;
7852ba13f0SRichard Henderson         break;
7952ba13f0SRichard Henderson 
8052ba13f0SRichard Henderson     case 0:
8152ba13f0SRichard Henderson         /*
8252ba13f0SRichard Henderson          * Fault during host memory operation within generated code.
8352ba13f0SRichard Henderson          * (Or, a unrelated bug within qemu, but we can't tell from here).
8452ba13f0SRichard Henderson          *
8552ba13f0SRichard Henderson          * We take the host pc from the signal frame.  However, we cannot
8652ba13f0SRichard Henderson          * use that value directly.  Within cpu_restore_state_from_tb, we
8752ba13f0SRichard Henderson          * assume PC comes from GETPC(), as used by the helper functions,
8852ba13f0SRichard Henderson          * so we adjust the address by -GETPC_ADJ to form an address that
8952ba13f0SRichard Henderson          * is within the call insn, so that the address does not accidentially
9052ba13f0SRichard Henderson          * match the beginning of the next guest insn.  However, when the
9152ba13f0SRichard Henderson          * pc comes from the signal frame it points to the actual faulting
9252ba13f0SRichard Henderson          * host memory insn and not the return from a call insn.
9352ba13f0SRichard Henderson          *
9452ba13f0SRichard Henderson          * Therefore, adjust to compensate for what will be done later
9552ba13f0SRichard Henderson          * by cpu_restore_state_from_tb.
9652ba13f0SRichard Henderson          */
97ec603b55SRichard Henderson         pc += GETPC_ADJ;
9852ba13f0SRichard Henderson         break;
9952ba13f0SRichard Henderson 
10052ba13f0SRichard Henderson     case 1:
10152ba13f0SRichard Henderson         /*
10252ba13f0SRichard Henderson          * Fault during host read for translation, or loosely, "execution".
10352ba13f0SRichard Henderson          *
10452ba13f0SRichard Henderson          * The guest pc is already pointing to the start of the TB for which
10552ba13f0SRichard Henderson          * code is being generated.  If the guest translator manages the
10652ba13f0SRichard Henderson          * page crossings correctly, this is exactly the correct address
10752ba13f0SRichard Henderson          * (and if the translator doesn't handle page boundaries correctly
10852ba13f0SRichard Henderson          * there's little we can do about that here).  Therefore, do not
10952ba13f0SRichard Henderson          * trigger the unwinder.
11052ba13f0SRichard Henderson          *
11152ba13f0SRichard Henderson          * Like tb_gen_code, release the memory lock before cpu_loop_exit.
11252ba13f0SRichard Henderson          */
11352ba13f0SRichard Henderson         pc = 0;
11452ba13f0SRichard Henderson         access_type = MMU_INST_FETCH;
11552ba13f0SRichard Henderson         mmap_unlock();
11652ba13f0SRichard Henderson         break;
117ec603b55SRichard Henderson     }
118ec603b55SRichard Henderson 
11902bed6bdSAlex Bennée     /* For synchronous signals we expect to be coming from the vCPU
12002bed6bdSAlex Bennée      * thread (so current_cpu should be valid) and either from running
12102bed6bdSAlex Bennée      * code or during translation which can fault as we cross pages.
12202bed6bdSAlex Bennée      *
12302bed6bdSAlex Bennée      * If neither is true then something has gone wrong and we should
12402bed6bdSAlex Bennée      * abort rather than try and restart the vCPU execution.
12502bed6bdSAlex Bennée      */
12602bed6bdSAlex Bennée     if (!cpu || !cpu->running) {
12702bed6bdSAlex Bennée         printf("qemu:%s received signal outside vCPU context @ pc=0x%"
12802bed6bdSAlex Bennée                PRIxPTR "\n",  __func__, pc);
12902bed6bdSAlex Bennée         abort();
13002bed6bdSAlex Bennée     }
13102bed6bdSAlex Bennée 
13242a623c7SBlue Swirl #if defined(DEBUG_SIGNAL)
13371baf787SPeter Maydell     printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
13442a623c7SBlue Swirl            pc, address, is_write, *(unsigned long *)old_set);
13542a623c7SBlue Swirl #endif
13642a623c7SBlue Swirl     /* XXX: locking issue */
1379c4bbee9SPeter Maydell     /* Note that it is important that we don't call page_unprotect() unless
1389c4bbee9SPeter Maydell      * this is really a "write to nonwriteable page" fault, because
1399c4bbee9SPeter Maydell      * page_unprotect() assumes that if it is called for an access to
1409c4bbee9SPeter Maydell      * a page that's writeable this means we had two threads racing and
1419c4bbee9SPeter Maydell      * another thread got there first and already made the page writeable;
1429c4bbee9SPeter Maydell      * so we will retry the access. If we were to call page_unprotect()
1439c4bbee9SPeter Maydell      * for some other kind of fault that should really be passed to the
1449c4bbee9SPeter Maydell      * guest, we'd end up in an infinite loop of retrying the faulting
1459c4bbee9SPeter Maydell      * access.
1469c4bbee9SPeter Maydell      */
1479c4bbee9SPeter Maydell     if (is_write && info->si_signo == SIGSEGV && info->si_code == SEGV_ACCERR &&
1489c4bbee9SPeter Maydell         h2g_valid(address)) {
149f213e72fSPeter Maydell         switch (page_unprotect(h2g(address), pc)) {
150f213e72fSPeter Maydell         case 0:
151f213e72fSPeter Maydell             /* Fault not caused by a page marked unwritable to protect
152ec603b55SRichard Henderson              * cached translations, must be the guest binary's problem.
153f213e72fSPeter Maydell              */
154f213e72fSPeter Maydell             break;
155f213e72fSPeter Maydell         case 1:
156f213e72fSPeter Maydell             /* Fault caused by protection of cached translation; TBs
157ec603b55SRichard Henderson              * invalidated, so resume execution.  Retain helper_retaddr
158ec603b55SRichard Henderson              * for a possible second fault.
159f213e72fSPeter Maydell              */
16042a623c7SBlue Swirl             return 1;
161f213e72fSPeter Maydell         case 2:
162f213e72fSPeter Maydell             /* Fault caused by protection of cached translation, and the
163f213e72fSPeter Maydell              * currently executing TB was modified and must be exited
164ec603b55SRichard Henderson              * immediately.  Clear helper_retaddr for next execution.
165f213e72fSPeter Maydell              */
16608b97f7fSRichard Henderson             clear_helper_retaddr();
16702bed6bdSAlex Bennée             cpu_exit_tb_from_sighandler(cpu, old_set);
168ec603b55SRichard Henderson             /* NORETURN */
169ec603b55SRichard Henderson 
170f213e72fSPeter Maydell         default:
171f213e72fSPeter Maydell             g_assert_not_reached();
172f213e72fSPeter Maydell         }
17342a623c7SBlue Swirl     }
17442a623c7SBlue Swirl 
175732f9e89SAlexander Graf     /* Convert forcefully to guest address space, invalid addresses
176732f9e89SAlexander Graf        are still valid segv ones */
177732f9e89SAlexander Graf     address = h2g_nocheck(address);
178732f9e89SAlexander Graf 
179da6bbf85SRichard Henderson     /*
180da6bbf85SRichard Henderson      * There is no way the target can handle this other than raising
181da6bbf85SRichard Henderson      * an exception.  Undo signal and retaddr state prior to longjmp.
182ec603b55SRichard Henderson      */
183da6bbf85SRichard Henderson     sigprocmask(SIG_SETMASK, old_set, NULL);
18408b97f7fSRichard Henderson     clear_helper_retaddr();
185ec603b55SRichard Henderson 
186da6bbf85SRichard Henderson     cc = CPU_GET_CLASS(cpu);
187da6bbf85SRichard Henderson     cc->tlb_fill(cpu, address, 0, access_type, MMU_USER_IDX, false, pc);
188da6bbf85SRichard Henderson     g_assert_not_reached();
18942a623c7SBlue Swirl }
19042a623c7SBlue Swirl 
19159e96ac6SDavid Hildenbrand void probe_write(CPUArchState *env, target_ulong addr, int size, int mmu_idx,
19259e96ac6SDavid Hildenbrand                  uintptr_t retaddr)
19359e96ac6SDavid Hildenbrand {
194*ca86cf32SDavid Hildenbrand     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
195*ca86cf32SDavid Hildenbrand 
19659e96ac6SDavid Hildenbrand     if (!guest_addr_valid(addr) ||
19759e96ac6SDavid Hildenbrand         page_check_range(addr, size, PAGE_WRITE) < 0) {
19859e96ac6SDavid Hildenbrand         CPUState *cpu = env_cpu(env);
19959e96ac6SDavid Hildenbrand         CPUClass *cc = CPU_GET_CLASS(cpu);
20059e96ac6SDavid Hildenbrand 
20159e96ac6SDavid Hildenbrand         cc->tlb_fill(cpu, addr, size, MMU_DATA_STORE, MMU_USER_IDX, false,
20259e96ac6SDavid Hildenbrand                      retaddr);
20359e96ac6SDavid Hildenbrand         g_assert_not_reached();
20459e96ac6SDavid Hildenbrand     }
20559e96ac6SDavid Hildenbrand }
20659e96ac6SDavid Hildenbrand 
20742a623c7SBlue Swirl #if defined(__i386__)
20842a623c7SBlue Swirl 
209c5679026SPeter Maydell #if defined(__NetBSD__)
21042a623c7SBlue Swirl #include <ucontext.h>
21142a623c7SBlue Swirl 
21242a623c7SBlue Swirl #define EIP_sig(context)     ((context)->uc_mcontext.__gregs[_REG_EIP])
21342a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
21442a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->uc_mcontext.__gregs[_REG_ERR])
21542a623c7SBlue Swirl #define MASK_sig(context)    ((context)->uc_sigmask)
21642a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__)
21742a623c7SBlue Swirl #include <ucontext.h>
21842a623c7SBlue Swirl 
21942a623c7SBlue Swirl #define EIP_sig(context)  (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
22042a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->uc_mcontext.mc_trapno)
22142a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->uc_mcontext.mc_err)
22242a623c7SBlue Swirl #define MASK_sig(context)    ((context)->uc_sigmask)
22342a623c7SBlue Swirl #elif defined(__OpenBSD__)
22442a623c7SBlue Swirl #define EIP_sig(context)     ((context)->sc_eip)
22542a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->sc_trapno)
22642a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->sc_err)
22742a623c7SBlue Swirl #define MASK_sig(context)    ((context)->sc_mask)
22842a623c7SBlue Swirl #else
22942a623c7SBlue Swirl #define EIP_sig(context)     ((context)->uc_mcontext.gregs[REG_EIP])
23042a623c7SBlue Swirl #define TRAP_sig(context)    ((context)->uc_mcontext.gregs[REG_TRAPNO])
23142a623c7SBlue Swirl #define ERROR_sig(context)   ((context)->uc_mcontext.gregs[REG_ERR])
23242a623c7SBlue Swirl #define MASK_sig(context)    ((context)->uc_sigmask)
23342a623c7SBlue Swirl #endif
23442a623c7SBlue Swirl 
23542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
23642a623c7SBlue Swirl                        void *puc)
23742a623c7SBlue Swirl {
23842a623c7SBlue Swirl     siginfo_t *info = pinfo;
23942a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
24042a623c7SBlue Swirl     ucontext_t *uc = puc;
24142a623c7SBlue Swirl #elif defined(__OpenBSD__)
24242a623c7SBlue Swirl     struct sigcontext *uc = puc;
24342a623c7SBlue Swirl #else
24404b33e21SKhem Raj     ucontext_t *uc = puc;
24542a623c7SBlue Swirl #endif
24642a623c7SBlue Swirl     unsigned long pc;
24742a623c7SBlue Swirl     int trapno;
24842a623c7SBlue Swirl 
24942a623c7SBlue Swirl #ifndef REG_EIP
25042a623c7SBlue Swirl /* for glibc 2.1 */
25142a623c7SBlue Swirl #define REG_EIP    EIP
25242a623c7SBlue Swirl #define REG_ERR    ERR
25342a623c7SBlue Swirl #define REG_TRAPNO TRAPNO
25442a623c7SBlue Swirl #endif
25542a623c7SBlue Swirl     pc = EIP_sig(uc);
25642a623c7SBlue Swirl     trapno = TRAP_sig(uc);
257a78b1299SPeter Maydell     return handle_cpu_signal(pc, info,
258a78b1299SPeter Maydell                              trapno == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
259a5852dc5SPeter Maydell                              &MASK_sig(uc));
26042a623c7SBlue Swirl }
26142a623c7SBlue Swirl 
26242a623c7SBlue Swirl #elif defined(__x86_64__)
26342a623c7SBlue Swirl 
26442a623c7SBlue Swirl #ifdef __NetBSD__
26542a623c7SBlue Swirl #define PC_sig(context)       _UC_MACHINE_PC(context)
26642a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
26742a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->uc_mcontext.__gregs[_REG_ERR])
26842a623c7SBlue Swirl #define MASK_sig(context)     ((context)->uc_sigmask)
26942a623c7SBlue Swirl #elif defined(__OpenBSD__)
27042a623c7SBlue Swirl #define PC_sig(context)       ((context)->sc_rip)
27142a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->sc_trapno)
27242a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->sc_err)
27342a623c7SBlue Swirl #define MASK_sig(context)     ((context)->sc_mask)
27442a623c7SBlue Swirl #elif defined(__FreeBSD__) || defined(__DragonFly__)
27542a623c7SBlue Swirl #include <ucontext.h>
27642a623c7SBlue Swirl 
27742a623c7SBlue Swirl #define PC_sig(context)  (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
27842a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->uc_mcontext.mc_trapno)
27942a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->uc_mcontext.mc_err)
28042a623c7SBlue Swirl #define MASK_sig(context)     ((context)->uc_sigmask)
28142a623c7SBlue Swirl #else
28242a623c7SBlue Swirl #define PC_sig(context)       ((context)->uc_mcontext.gregs[REG_RIP])
28342a623c7SBlue Swirl #define TRAP_sig(context)     ((context)->uc_mcontext.gregs[REG_TRAPNO])
28442a623c7SBlue Swirl #define ERROR_sig(context)    ((context)->uc_mcontext.gregs[REG_ERR])
28542a623c7SBlue Swirl #define MASK_sig(context)     ((context)->uc_sigmask)
28642a623c7SBlue Swirl #endif
28742a623c7SBlue Swirl 
28842a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
28942a623c7SBlue Swirl                        void *puc)
29042a623c7SBlue Swirl {
29142a623c7SBlue Swirl     siginfo_t *info = pinfo;
29242a623c7SBlue Swirl     unsigned long pc;
29342a623c7SBlue Swirl #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
29442a623c7SBlue Swirl     ucontext_t *uc = puc;
29542a623c7SBlue Swirl #elif defined(__OpenBSD__)
29642a623c7SBlue Swirl     struct sigcontext *uc = puc;
29742a623c7SBlue Swirl #else
29804b33e21SKhem Raj     ucontext_t *uc = puc;
29942a623c7SBlue Swirl #endif
30042a623c7SBlue Swirl 
30142a623c7SBlue Swirl     pc = PC_sig(uc);
302a78b1299SPeter Maydell     return handle_cpu_signal(pc, info,
303a78b1299SPeter Maydell                              TRAP_sig(uc) == 0xe ? (ERROR_sig(uc) >> 1) & 1 : 0,
304a5852dc5SPeter Maydell                              &MASK_sig(uc));
30542a623c7SBlue Swirl }
30642a623c7SBlue Swirl 
30742a623c7SBlue Swirl #elif defined(_ARCH_PPC)
30842a623c7SBlue Swirl 
30942a623c7SBlue Swirl /***********************************************************************
31042a623c7SBlue Swirl  * signal context platform-specific definitions
31142a623c7SBlue Swirl  * From Wine
31242a623c7SBlue Swirl  */
31342a623c7SBlue Swirl #ifdef linux
31442a623c7SBlue Swirl /* All Registers access - only for local access */
31542a623c7SBlue Swirl #define REG_sig(reg_name, context)              \
31642a623c7SBlue Swirl     ((context)->uc_mcontext.regs->reg_name)
31742a623c7SBlue Swirl /* Gpr Registers access  */
31842a623c7SBlue Swirl #define GPR_sig(reg_num, context)              REG_sig(gpr[reg_num], context)
31942a623c7SBlue Swirl /* Program counter */
32042a623c7SBlue Swirl #define IAR_sig(context)                       REG_sig(nip, context)
32142a623c7SBlue Swirl /* Machine State Register (Supervisor) */
32242a623c7SBlue Swirl #define MSR_sig(context)                       REG_sig(msr, context)
32342a623c7SBlue Swirl /* Count register */
32442a623c7SBlue Swirl #define CTR_sig(context)                       REG_sig(ctr, context)
32542a623c7SBlue Swirl /* User's integer exception register */
32642a623c7SBlue Swirl #define XER_sig(context)                       REG_sig(xer, context)
32742a623c7SBlue Swirl /* Link register */
32842a623c7SBlue Swirl #define LR_sig(context)                        REG_sig(link, context)
32942a623c7SBlue Swirl /* Condition register */
33042a623c7SBlue Swirl #define CR_sig(context)                        REG_sig(ccr, context)
33142a623c7SBlue Swirl 
33242a623c7SBlue Swirl /* Float Registers access  */
33342a623c7SBlue Swirl #define FLOAT_sig(reg_num, context)                                     \
33442a623c7SBlue Swirl     (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
33542a623c7SBlue Swirl #define FPSCR_sig(context) \
33642a623c7SBlue Swirl     (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
33742a623c7SBlue Swirl /* Exception Registers access */
33842a623c7SBlue Swirl #define DAR_sig(context)                       REG_sig(dar, context)
33942a623c7SBlue Swirl #define DSISR_sig(context)                     REG_sig(dsisr, context)
34042a623c7SBlue Swirl #define TRAP_sig(context)                      REG_sig(trap, context)
34142a623c7SBlue Swirl #endif /* linux */
34242a623c7SBlue Swirl 
34342a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
34442a623c7SBlue Swirl #include <ucontext.h>
34542a623c7SBlue Swirl #define IAR_sig(context)               ((context)->uc_mcontext.mc_srr0)
34642a623c7SBlue Swirl #define MSR_sig(context)               ((context)->uc_mcontext.mc_srr1)
34742a623c7SBlue Swirl #define CTR_sig(context)               ((context)->uc_mcontext.mc_ctr)
34842a623c7SBlue Swirl #define XER_sig(context)               ((context)->uc_mcontext.mc_xer)
34942a623c7SBlue Swirl #define LR_sig(context)                ((context)->uc_mcontext.mc_lr)
35042a623c7SBlue Swirl #define CR_sig(context)                ((context)->uc_mcontext.mc_cr)
35142a623c7SBlue Swirl /* Exception Registers access */
35242a623c7SBlue Swirl #define DAR_sig(context)               ((context)->uc_mcontext.mc_dar)
35342a623c7SBlue Swirl #define DSISR_sig(context)             ((context)->uc_mcontext.mc_dsisr)
35442a623c7SBlue Swirl #define TRAP_sig(context)              ((context)->uc_mcontext.mc_exc)
35542a623c7SBlue Swirl #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
35642a623c7SBlue Swirl 
35742a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
35842a623c7SBlue Swirl                        void *puc)
35942a623c7SBlue Swirl {
36042a623c7SBlue Swirl     siginfo_t *info = pinfo;
36142a623c7SBlue Swirl #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
36242a623c7SBlue Swirl     ucontext_t *uc = puc;
36342a623c7SBlue Swirl #else
36404b33e21SKhem Raj     ucontext_t *uc = puc;
36542a623c7SBlue Swirl #endif
36642a623c7SBlue Swirl     unsigned long pc;
36742a623c7SBlue Swirl     int is_write;
36842a623c7SBlue Swirl 
36942a623c7SBlue Swirl     pc = IAR_sig(uc);
37042a623c7SBlue Swirl     is_write = 0;
37142a623c7SBlue Swirl #if 0
37242a623c7SBlue Swirl     /* ppc 4xx case */
37342a623c7SBlue Swirl     if (DSISR_sig(uc) & 0x00800000) {
37442a623c7SBlue Swirl         is_write = 1;
37542a623c7SBlue Swirl     }
37642a623c7SBlue Swirl #else
37742a623c7SBlue Swirl     if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
37842a623c7SBlue Swirl         is_write = 1;
37942a623c7SBlue Swirl     }
38042a623c7SBlue Swirl #endif
381a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
38242a623c7SBlue Swirl }
38342a623c7SBlue Swirl 
38442a623c7SBlue Swirl #elif defined(__alpha__)
38542a623c7SBlue Swirl 
38642a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
38742a623c7SBlue Swirl                            void *puc)
38842a623c7SBlue Swirl {
38942a623c7SBlue Swirl     siginfo_t *info = pinfo;
39004b33e21SKhem Raj     ucontext_t *uc = puc;
39142a623c7SBlue Swirl     uint32_t *pc = uc->uc_mcontext.sc_pc;
39242a623c7SBlue Swirl     uint32_t insn = *pc;
39342a623c7SBlue Swirl     int is_write = 0;
39442a623c7SBlue Swirl 
39542a623c7SBlue Swirl     /* XXX: need kernel patch to get write flag faster */
39642a623c7SBlue Swirl     switch (insn >> 26) {
39742a623c7SBlue Swirl     case 0x0d: /* stw */
39842a623c7SBlue Swirl     case 0x0e: /* stb */
39942a623c7SBlue Swirl     case 0x0f: /* stq_u */
40042a623c7SBlue Swirl     case 0x24: /* stf */
40142a623c7SBlue Swirl     case 0x25: /* stg */
40242a623c7SBlue Swirl     case 0x26: /* sts */
40342a623c7SBlue Swirl     case 0x27: /* stt */
40442a623c7SBlue Swirl     case 0x2c: /* stl */
40542a623c7SBlue Swirl     case 0x2d: /* stq */
40642a623c7SBlue Swirl     case 0x2e: /* stl_c */
40742a623c7SBlue Swirl     case 0x2f: /* stq_c */
40842a623c7SBlue Swirl         is_write = 1;
40942a623c7SBlue Swirl     }
41042a623c7SBlue Swirl 
411a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
41242a623c7SBlue Swirl }
41342a623c7SBlue Swirl #elif defined(__sparc__)
41442a623c7SBlue Swirl 
41542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
41642a623c7SBlue Swirl                        void *puc)
41742a623c7SBlue Swirl {
41842a623c7SBlue Swirl     siginfo_t *info = pinfo;
41942a623c7SBlue Swirl     int is_write;
42042a623c7SBlue Swirl     uint32_t insn;
42142a623c7SBlue Swirl #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
42242a623c7SBlue Swirl     uint32_t *regs = (uint32_t *)(info + 1);
42342a623c7SBlue Swirl     void *sigmask = (regs + 20);
42442a623c7SBlue Swirl     /* XXX: is there a standard glibc define ? */
42542a623c7SBlue Swirl     unsigned long pc = regs[1];
42642a623c7SBlue Swirl #else
42742a623c7SBlue Swirl #ifdef __linux__
42842a623c7SBlue Swirl     struct sigcontext *sc = puc;
42942a623c7SBlue Swirl     unsigned long pc = sc->sigc_regs.tpc;
43042a623c7SBlue Swirl     void *sigmask = (void *)sc->sigc_mask;
43142a623c7SBlue Swirl #elif defined(__OpenBSD__)
43242a623c7SBlue Swirl     struct sigcontext *uc = puc;
43342a623c7SBlue Swirl     unsigned long pc = uc->sc_pc;
43442a623c7SBlue Swirl     void *sigmask = (void *)(long)uc->sc_mask;
4357ccfb495STobias Nygren #elif defined(__NetBSD__)
4367ccfb495STobias Nygren     ucontext_t *uc = puc;
4377ccfb495STobias Nygren     unsigned long pc = _UC_MACHINE_PC(uc);
4387ccfb495STobias Nygren     void *sigmask = (void *)&uc->uc_sigmask;
43942a623c7SBlue Swirl #endif
44042a623c7SBlue Swirl #endif
44142a623c7SBlue Swirl 
44242a623c7SBlue Swirl     /* XXX: need kernel patch to get write flag faster */
44342a623c7SBlue Swirl     is_write = 0;
44442a623c7SBlue Swirl     insn = *(uint32_t *)pc;
44542a623c7SBlue Swirl     if ((insn >> 30) == 3) {
44642a623c7SBlue Swirl         switch ((insn >> 19) & 0x3f) {
44742a623c7SBlue Swirl         case 0x05: /* stb */
44842a623c7SBlue Swirl         case 0x15: /* stba */
44942a623c7SBlue Swirl         case 0x06: /* sth */
45042a623c7SBlue Swirl         case 0x16: /* stha */
45142a623c7SBlue Swirl         case 0x04: /* st */
45242a623c7SBlue Swirl         case 0x14: /* sta */
45342a623c7SBlue Swirl         case 0x07: /* std */
45442a623c7SBlue Swirl         case 0x17: /* stda */
45542a623c7SBlue Swirl         case 0x0e: /* stx */
45642a623c7SBlue Swirl         case 0x1e: /* stxa */
45742a623c7SBlue Swirl         case 0x24: /* stf */
45842a623c7SBlue Swirl         case 0x34: /* stfa */
45942a623c7SBlue Swirl         case 0x27: /* stdf */
46042a623c7SBlue Swirl         case 0x37: /* stdfa */
46142a623c7SBlue Swirl         case 0x26: /* stqf */
46242a623c7SBlue Swirl         case 0x36: /* stqfa */
46342a623c7SBlue Swirl         case 0x25: /* stfsr */
46442a623c7SBlue Swirl         case 0x3c: /* casa */
46542a623c7SBlue Swirl         case 0x3e: /* casxa */
46642a623c7SBlue Swirl             is_write = 1;
46742a623c7SBlue Swirl             break;
46842a623c7SBlue Swirl         }
46942a623c7SBlue Swirl     }
470a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, sigmask);
47142a623c7SBlue Swirl }
47242a623c7SBlue Swirl 
47342a623c7SBlue Swirl #elif defined(__arm__)
47442a623c7SBlue Swirl 
4757ccfb495STobias Nygren #if defined(__NetBSD__)
4767ccfb495STobias Nygren #include <ucontext.h>
4777ccfb495STobias Nygren #endif
4787ccfb495STobias Nygren 
47942a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
48042a623c7SBlue Swirl                        void *puc)
48142a623c7SBlue Swirl {
48242a623c7SBlue Swirl     siginfo_t *info = pinfo;
4837ccfb495STobias Nygren #if defined(__NetBSD__)
4847ccfb495STobias Nygren     ucontext_t *uc = puc;
4857ccfb495STobias Nygren #else
48604b33e21SKhem Raj     ucontext_t *uc = puc;
4877ccfb495STobias Nygren #endif
48842a623c7SBlue Swirl     unsigned long pc;
48942a623c7SBlue Swirl     int is_write;
49042a623c7SBlue Swirl 
4917ccfb495STobias Nygren #if defined(__NetBSD__)
4927ccfb495STobias Nygren     pc = uc->uc_mcontext.__gregs[_REG_R15];
4937ccfb495STobias Nygren #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
49442a623c7SBlue Swirl     pc = uc->uc_mcontext.gregs[R15];
49542a623c7SBlue Swirl #else
49642a623c7SBlue Swirl     pc = uc->uc_mcontext.arm_pc;
49742a623c7SBlue Swirl #endif
498023b0ae3SPeter Maydell 
499023b0ae3SPeter Maydell     /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
500023b0ae3SPeter Maydell      * later processor; on v5 we will always report this as a read).
501023b0ae3SPeter Maydell      */
502023b0ae3SPeter Maydell     is_write = extract32(uc->uc_mcontext.error_code, 11, 1);
503a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
50442a623c7SBlue Swirl }
50542a623c7SBlue Swirl 
506f129061cSClaudio Fontana #elif defined(__aarch64__)
507f129061cSClaudio Fontana 
508f454a54fSPeter Maydell #ifndef ESR_MAGIC
509f454a54fSPeter Maydell /* Pre-3.16 kernel headers don't have these, so provide fallback definitions */
510f454a54fSPeter Maydell #define ESR_MAGIC 0x45535201
511f454a54fSPeter Maydell struct esr_context {
512f454a54fSPeter Maydell     struct _aarch64_ctx head;
513f454a54fSPeter Maydell     uint64_t esr;
514f454a54fSPeter Maydell };
515f454a54fSPeter Maydell #endif
516f454a54fSPeter Maydell 
517f454a54fSPeter Maydell static inline struct _aarch64_ctx *first_ctx(ucontext_t *uc)
518f454a54fSPeter Maydell {
519f454a54fSPeter Maydell     return (struct _aarch64_ctx *)&uc->uc_mcontext.__reserved;
520f454a54fSPeter Maydell }
521f454a54fSPeter Maydell 
522f454a54fSPeter Maydell static inline struct _aarch64_ctx *next_ctx(struct _aarch64_ctx *hdr)
523f454a54fSPeter Maydell {
524f454a54fSPeter Maydell     return (struct _aarch64_ctx *)((char *)hdr + hdr->size);
525f454a54fSPeter Maydell }
526f454a54fSPeter Maydell 
527661f7fa4SRichard Henderson int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
528f129061cSClaudio Fontana {
529f129061cSClaudio Fontana     siginfo_t *info = pinfo;
53004b33e21SKhem Raj     ucontext_t *uc = puc;
531661f7fa4SRichard Henderson     uintptr_t pc = uc->uc_mcontext.pc;
532661f7fa4SRichard Henderson     bool is_write;
533f454a54fSPeter Maydell     struct _aarch64_ctx *hdr;
534f454a54fSPeter Maydell     struct esr_context const *esrctx = NULL;
535f129061cSClaudio Fontana 
536f454a54fSPeter Maydell     /* Find the esr_context, which has the WnR bit in it */
537f454a54fSPeter Maydell     for (hdr = first_ctx(uc); hdr->magic; hdr = next_ctx(hdr)) {
538f454a54fSPeter Maydell         if (hdr->magic == ESR_MAGIC) {
539f454a54fSPeter Maydell             esrctx = (struct esr_context const *)hdr;
540f454a54fSPeter Maydell             break;
541f454a54fSPeter Maydell         }
542f454a54fSPeter Maydell     }
543f454a54fSPeter Maydell 
544f454a54fSPeter Maydell     if (esrctx) {
545f454a54fSPeter Maydell         /* For data aborts ESR.EC is 0b10010x: then bit 6 is the WnR bit */
546f454a54fSPeter Maydell         uint64_t esr = esrctx->esr;
547f454a54fSPeter Maydell         is_write = extract32(esr, 27, 5) == 0x12 && extract32(esr, 6, 1) == 1;
548f454a54fSPeter Maydell     } else {
549f454a54fSPeter Maydell         /*
550f454a54fSPeter Maydell          * Fall back to parsing instructions; will only be needed
551f454a54fSPeter Maydell          * for really ancient (pre-3.16) kernels.
552f454a54fSPeter Maydell          */
553f454a54fSPeter Maydell         uint32_t insn = *(uint32_t *)pc;
554f454a54fSPeter Maydell 
555661f7fa4SRichard Henderson         is_write = ((insn & 0xbfff0000) == 0x0c000000   /* C3.3.1 */
556661f7fa4SRichard Henderson                     || (insn & 0xbfe00000) == 0x0c800000   /* C3.3.2 */
557661f7fa4SRichard Henderson                     || (insn & 0xbfdf0000) == 0x0d000000   /* C3.3.3 */
558661f7fa4SRichard Henderson                     || (insn & 0xbfc00000) == 0x0d800000   /* C3.3.4 */
559661f7fa4SRichard Henderson                     || (insn & 0x3f400000) == 0x08000000   /* C3.3.6 */
560661f7fa4SRichard Henderson                     || (insn & 0x3bc00000) == 0x39000000   /* C3.3.13 */
561661f7fa4SRichard Henderson                     || (insn & 0x3fc00000) == 0x3d800000   /* ... 128bit */
562f454a54fSPeter Maydell                     /* Ignore bits 10, 11 & 21, controlling indexing.  */
563661f7fa4SRichard Henderson                     || (insn & 0x3bc00000) == 0x38000000   /* C3.3.8-12 */
564661f7fa4SRichard Henderson                     || (insn & 0x3fe00000) == 0x3c800000   /* ... 128bit */
565661f7fa4SRichard Henderson                     /* Ignore bits 23 & 24, controlling indexing.  */
566661f7fa4SRichard Henderson                     || (insn & 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
567f454a54fSPeter Maydell     }
568a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
569f129061cSClaudio Fontana }
570f129061cSClaudio Fontana 
57142a623c7SBlue Swirl #elif defined(__s390__)
57242a623c7SBlue Swirl 
57342a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
57442a623c7SBlue Swirl                        void *puc)
57542a623c7SBlue Swirl {
57642a623c7SBlue Swirl     siginfo_t *info = pinfo;
57704b33e21SKhem Raj     ucontext_t *uc = puc;
57842a623c7SBlue Swirl     unsigned long pc;
57942a623c7SBlue Swirl     uint16_t *pinsn;
58042a623c7SBlue Swirl     int is_write = 0;
58142a623c7SBlue Swirl 
58242a623c7SBlue Swirl     pc = uc->uc_mcontext.psw.addr;
58342a623c7SBlue Swirl 
58442a623c7SBlue Swirl     /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
58542a623c7SBlue Swirl        of the normal 2 arguments.  The 3rd argument contains the "int_code"
58642a623c7SBlue Swirl        from the hardware which does in fact contain the is_write value.
58742a623c7SBlue Swirl        The rt signal handler, as far as I can tell, does not give this value
58842a623c7SBlue Swirl        at all.  Not that we could get to it from here even if it were.  */
58942a623c7SBlue Swirl     /* ??? This is not even close to complete, since it ignores all
59042a623c7SBlue Swirl        of the read-modify-write instructions.  */
59142a623c7SBlue Swirl     pinsn = (uint16_t *)pc;
59242a623c7SBlue Swirl     switch (pinsn[0] >> 8) {
59342a623c7SBlue Swirl     case 0x50: /* ST */
59442a623c7SBlue Swirl     case 0x42: /* STC */
59542a623c7SBlue Swirl     case 0x40: /* STH */
59642a623c7SBlue Swirl         is_write = 1;
59742a623c7SBlue Swirl         break;
59842a623c7SBlue Swirl     case 0xc4: /* RIL format insns */
59942a623c7SBlue Swirl         switch (pinsn[0] & 0xf) {
60042a623c7SBlue Swirl         case 0xf: /* STRL */
60142a623c7SBlue Swirl         case 0xb: /* STGRL */
60242a623c7SBlue Swirl         case 0x7: /* STHRL */
60342a623c7SBlue Swirl             is_write = 1;
60442a623c7SBlue Swirl         }
60542a623c7SBlue Swirl         break;
60642a623c7SBlue Swirl     case 0xe3: /* RXY format insns */
60742a623c7SBlue Swirl         switch (pinsn[2] & 0xff) {
60842a623c7SBlue Swirl         case 0x50: /* STY */
60942a623c7SBlue Swirl         case 0x24: /* STG */
61042a623c7SBlue Swirl         case 0x72: /* STCY */
61142a623c7SBlue Swirl         case 0x70: /* STHY */
61242a623c7SBlue Swirl         case 0x8e: /* STPQ */
61342a623c7SBlue Swirl         case 0x3f: /* STRVH */
61442a623c7SBlue Swirl         case 0x3e: /* STRV */
61542a623c7SBlue Swirl         case 0x2f: /* STRVG */
61642a623c7SBlue Swirl             is_write = 1;
61742a623c7SBlue Swirl         }
61842a623c7SBlue Swirl         break;
61942a623c7SBlue Swirl     }
620a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
62142a623c7SBlue Swirl }
62242a623c7SBlue Swirl 
62342a623c7SBlue Swirl #elif defined(__mips__)
62442a623c7SBlue Swirl 
62542a623c7SBlue Swirl int cpu_signal_handler(int host_signum, void *pinfo,
62642a623c7SBlue Swirl                        void *puc)
62742a623c7SBlue Swirl {
62842a623c7SBlue Swirl     siginfo_t *info = pinfo;
62904b33e21SKhem Raj     ucontext_t *uc = puc;
63042a623c7SBlue Swirl     greg_t pc = uc->uc_mcontext.pc;
63142a623c7SBlue Swirl     int is_write;
63242a623c7SBlue Swirl 
63342a623c7SBlue Swirl     /* XXX: compute is_write */
63442a623c7SBlue Swirl     is_write = 0;
635a78b1299SPeter Maydell     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
63642a623c7SBlue Swirl }
63742a623c7SBlue Swirl 
638464e447aSAlistair Francis #elif defined(__riscv)
639464e447aSAlistair Francis 
640464e447aSAlistair Francis int cpu_signal_handler(int host_signum, void *pinfo,
641464e447aSAlistair Francis                        void *puc)
642464e447aSAlistair Francis {
643464e447aSAlistair Francis     siginfo_t *info = pinfo;
644464e447aSAlistair Francis     ucontext_t *uc = puc;
645464e447aSAlistair Francis     greg_t pc = uc->uc_mcontext.__gregs[REG_PC];
646464e447aSAlistair Francis     uint32_t insn = *(uint32_t *)pc;
647464e447aSAlistair Francis     int is_write = 0;
648464e447aSAlistair Francis 
649464e447aSAlistair Francis     /* Detect store by reading the instruction at the program
650464e447aSAlistair Francis        counter. Note: we currently only generate 32-bit
651464e447aSAlistair Francis        instructions so we thus only detect 32-bit stores */
652464e447aSAlistair Francis     switch (((insn >> 0) & 0b11)) {
653464e447aSAlistair Francis     case 3:
654464e447aSAlistair Francis         switch (((insn >> 2) & 0b11111)) {
655464e447aSAlistair Francis         case 8:
656464e447aSAlistair Francis             switch (((insn >> 12) & 0b111)) {
657464e447aSAlistair Francis             case 0: /* sb */
658464e447aSAlistair Francis             case 1: /* sh */
659464e447aSAlistair Francis             case 2: /* sw */
660464e447aSAlistair Francis             case 3: /* sd */
661464e447aSAlistair Francis             case 4: /* sq */
662464e447aSAlistair Francis                 is_write = 1;
663464e447aSAlistair Francis                 break;
664464e447aSAlistair Francis             default:
665464e447aSAlistair Francis                 break;
666464e447aSAlistair Francis             }
667464e447aSAlistair Francis             break;
668464e447aSAlistair Francis         case 9:
669464e447aSAlistair Francis             switch (((insn >> 12) & 0b111)) {
670464e447aSAlistair Francis             case 2: /* fsw */
671464e447aSAlistair Francis             case 3: /* fsd */
672464e447aSAlistair Francis             case 4: /* fsq */
673464e447aSAlistair Francis                 is_write = 1;
674464e447aSAlistair Francis                 break;
675464e447aSAlistair Francis             default:
676464e447aSAlistair Francis                 break;
677464e447aSAlistair Francis             }
678464e447aSAlistair Francis             break;
679464e447aSAlistair Francis         default:
680464e447aSAlistair Francis             break;
681464e447aSAlistair Francis         }
682464e447aSAlistair Francis     }
683464e447aSAlistair Francis 
684464e447aSAlistair Francis     /* Check for compressed instructions */
685464e447aSAlistair Francis     switch (((insn >> 13) & 0b111)) {
686464e447aSAlistair Francis     case 7:
687464e447aSAlistair Francis         switch (insn & 0b11) {
688464e447aSAlistair Francis         case 0: /*c.sd */
689464e447aSAlistair Francis         case 2: /* c.sdsp */
690464e447aSAlistair Francis             is_write = 1;
691464e447aSAlistair Francis             break;
692464e447aSAlistair Francis         default:
693464e447aSAlistair Francis             break;
694464e447aSAlistair Francis         }
695464e447aSAlistair Francis         break;
696464e447aSAlistair Francis     case 6:
697464e447aSAlistair Francis         switch (insn & 0b11) {
698464e447aSAlistair Francis         case 0: /* c.sw */
699464e447aSAlistair Francis         case 3: /* c.swsp */
700464e447aSAlistair Francis             is_write = 1;
701464e447aSAlistair Francis             break;
702464e447aSAlistair Francis         default:
703464e447aSAlistair Francis             break;
704464e447aSAlistair Francis         }
705464e447aSAlistair Francis         break;
706464e447aSAlistair Francis     default:
707464e447aSAlistair Francis         break;
708464e447aSAlistair Francis     }
709464e447aSAlistair Francis 
710464e447aSAlistair Francis     return handle_cpu_signal(pc, info, is_write, &uc->uc_sigmask);
711464e447aSAlistair Francis }
712464e447aSAlistair Francis 
71342a623c7SBlue Swirl #else
71442a623c7SBlue Swirl 
71542a623c7SBlue Swirl #error host CPU specific signal handler needed
71642a623c7SBlue Swirl 
71742a623c7SBlue Swirl #endif
718a411d296SPhilippe Mathieu-Daudé 
719a411d296SPhilippe Mathieu-Daudé /* The softmmu versions of these helpers are in cputlb.c.  */
720a411d296SPhilippe Mathieu-Daudé 
721a411d296SPhilippe Mathieu-Daudé /* Do not allow unaligned operations to proceed.  Return the host address.  */
722a411d296SPhilippe Mathieu-Daudé static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
723a411d296SPhilippe Mathieu-Daudé                                int size, uintptr_t retaddr)
724a411d296SPhilippe Mathieu-Daudé {
725a411d296SPhilippe Mathieu-Daudé     /* Enforce qemu required alignment.  */
726a411d296SPhilippe Mathieu-Daudé     if (unlikely(addr & (size - 1))) {
72729a0af61SRichard Henderson         cpu_loop_exit_atomic(env_cpu(env), retaddr);
728a411d296SPhilippe Mathieu-Daudé     }
72908b97f7fSRichard Henderson     void *ret = g2h(addr);
73008b97f7fSRichard Henderson     set_helper_retaddr(retaddr);
73108b97f7fSRichard Henderson     return ret;
732a411d296SPhilippe Mathieu-Daudé }
733a411d296SPhilippe Mathieu-Daudé 
734a411d296SPhilippe Mathieu-Daudé /* Macro to call the above, with local variables from the use context.  */
73534d49937SPeter Maydell #define ATOMIC_MMU_DECLS do {} while (0)
736a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP  atomic_mmu_lookup(env, addr, DATA_SIZE, GETPC())
73708b97f7fSRichard Henderson #define ATOMIC_MMU_CLEANUP do { clear_helper_retaddr(); } while (0)
738a411d296SPhilippe Mathieu-Daudé 
739a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X)   HELPER(glue(glue(atomic_ ## X, SUFFIX), END))
740a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS
741a411d296SPhilippe Mathieu-Daudé 
742a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 1
743a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
744a411d296SPhilippe Mathieu-Daudé 
745a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 2
746a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
747a411d296SPhilippe Mathieu-Daudé 
748a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 4
749a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
750a411d296SPhilippe Mathieu-Daudé 
751a411d296SPhilippe Mathieu-Daudé #ifdef CONFIG_ATOMIC64
752a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 8
753a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
754a411d296SPhilippe Mathieu-Daudé #endif
755a411d296SPhilippe Mathieu-Daudé 
756a411d296SPhilippe Mathieu-Daudé /* The following is only callable from other helpers, and matches up
757a411d296SPhilippe Mathieu-Daudé    with the softmmu version.  */
758a411d296SPhilippe Mathieu-Daudé 
759e6cd4bb5SRichard Henderson #if HAVE_ATOMIC128 || HAVE_CMPXCHG128
760a411d296SPhilippe Mathieu-Daudé 
761a411d296SPhilippe Mathieu-Daudé #undef EXTRA_ARGS
762a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_NAME
763a411d296SPhilippe Mathieu-Daudé #undef ATOMIC_MMU_LOOKUP
764a411d296SPhilippe Mathieu-Daudé 
765a411d296SPhilippe Mathieu-Daudé #define EXTRA_ARGS     , TCGMemOpIdx oi, uintptr_t retaddr
766a411d296SPhilippe Mathieu-Daudé #define ATOMIC_NAME(X) \
767a411d296SPhilippe Mathieu-Daudé     HELPER(glue(glue(glue(atomic_ ## X, SUFFIX), END), _mmu))
768a411d296SPhilippe Mathieu-Daudé #define ATOMIC_MMU_LOOKUP  atomic_mmu_lookup(env, addr, DATA_SIZE, retaddr)
769a411d296SPhilippe Mathieu-Daudé 
770a411d296SPhilippe Mathieu-Daudé #define DATA_SIZE 16
771a411d296SPhilippe Mathieu-Daudé #include "atomic_template.h"
772e6cd4bb5SRichard Henderson #endif
773