1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20#include "cpu.h"
21#include "disas/disas.h"
22#include "exec/exec-all.h"
23#include "tcg.h"
24#include "qemu/bitops.h"
25#include "exec/cpu_ldst.h"
26#include "translate-all.h"
27
28#undef EAX
29#undef ECX
30#undef EDX
31#undef EBX
32#undef ESP
33#undef EBP
34#undef ESI
35#undef EDI
36#undef EIP
37#ifdef __linux__
38#include <sys/ucontext.h>
39#endif
40
41
42
43
44
45
46static void cpu_exit_tb_from_sighandler(CPUState *cpu, sigset_t *old_set)
47{
48
49 sigprocmask(SIG_SETMASK, old_set, NULL);
50 cpu_loop_exit_noexc(cpu);
51}
52
53
54
55
56
57static inline int handle_cpu_signal(uintptr_t pc, unsigned long address,
58 int is_write, sigset_t *old_set)
59{
60 CPUState *cpu = current_cpu;
61 CPUClass *cc;
62 int ret;
63
64
65
66
67
68
69
70
71 if (!cpu || !cpu->running) {
72 printf("qemu:%s received signal outside vCPU context @ pc=0x%"
73 PRIxPTR "\n", __func__, pc);
74 abort();
75 }
76
77#if defined(DEBUG_SIGNAL)
78 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
79 pc, address, is_write, *(unsigned long *)old_set);
80#endif
81
82 if (is_write && h2g_valid(address)) {
83 switch (page_unprotect(h2g(address), pc)) {
84 case 0:
85
86
87
88 break;
89 case 1:
90
91
92
93 return 1;
94 case 2:
95
96
97
98
99 cpu_exit_tb_from_sighandler(cpu, old_set);
100 g_assert_not_reached();
101 default:
102 g_assert_not_reached();
103 }
104 }
105
106
107
108 address = h2g_nocheck(address);
109
110 cc = CPU_GET_CLASS(cpu);
111
112 g_assert(cc->handle_mmu_fault);
113 ret = cc->handle_mmu_fault(cpu, address, is_write, MMU_USER_IDX);
114 if (ret < 0) {
115 return 0;
116 }
117 if (ret == 0) {
118 return 1;
119 }
120
121
122
123
124 cpu_restore_state(cpu, pc + GETPC_ADJ);
125
126 sigprocmask(SIG_SETMASK, old_set, NULL);
127 cpu_loop_exit(cpu);
128
129
130 return 1;
131}
132
133#if defined(__i386__)
134
135#if defined(__NetBSD__)
136#include <ucontext.h>
137
138#define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
139#define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
140#define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
141#define MASK_sig(context) ((context)->uc_sigmask)
142#elif defined(__FreeBSD__) || defined(__DragonFly__)
143#include <ucontext.h>
144
145#define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
146#define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
147#define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
148#define MASK_sig(context) ((context)->uc_sigmask)
149#elif defined(__OpenBSD__)
150#define EIP_sig(context) ((context)->sc_eip)
151#define TRAP_sig(context) ((context)->sc_trapno)
152#define ERROR_sig(context) ((context)->sc_err)
153#define MASK_sig(context) ((context)->sc_mask)
154#else
155#define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
156#define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
157#define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
158#define MASK_sig(context) ((context)->uc_sigmask)
159#endif
160
161int cpu_signal_handler(int host_signum, void *pinfo,
162 void *puc)
163{
164 siginfo_t *info = pinfo;
165#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
166 ucontext_t *uc = puc;
167#elif defined(__OpenBSD__)
168 struct sigcontext *uc = puc;
169#else
170 ucontext_t *uc = puc;
171#endif
172 unsigned long pc;
173 int trapno;
174
175#ifndef REG_EIP
176
177#define REG_EIP EIP
178#define REG_ERR ERR
179#define REG_TRAPNO TRAPNO
180#endif
181 pc = EIP_sig(uc);
182 trapno = TRAP_sig(uc);
183 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
184 trapno == 0xe ?
185 (ERROR_sig(uc) >> 1) & 1 : 0,
186 &MASK_sig(uc));
187}
188
189#elif defined(__x86_64__)
190
191#ifdef __NetBSD__
192#define PC_sig(context) _UC_MACHINE_PC(context)
193#define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
194#define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
195#define MASK_sig(context) ((context)->uc_sigmask)
196#elif defined(__OpenBSD__)
197#define PC_sig(context) ((context)->sc_rip)
198#define TRAP_sig(context) ((context)->sc_trapno)
199#define ERROR_sig(context) ((context)->sc_err)
200#define MASK_sig(context) ((context)->sc_mask)
201#elif defined(__FreeBSD__) || defined(__DragonFly__)
202#include <ucontext.h>
203
204#define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
205#define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
206#define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
207#define MASK_sig(context) ((context)->uc_sigmask)
208#else
209#define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
210#define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
211#define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
212#define MASK_sig(context) ((context)->uc_sigmask)
213#endif
214
215int cpu_signal_handler(int host_signum, void *pinfo,
216 void *puc)
217{
218 siginfo_t *info = pinfo;
219 unsigned long pc;
220#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
221 ucontext_t *uc = puc;
222#elif defined(__OpenBSD__)
223 struct sigcontext *uc = puc;
224#else
225 ucontext_t *uc = puc;
226#endif
227
228 pc = PC_sig(uc);
229 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
230 TRAP_sig(uc) == 0xe ?
231 (ERROR_sig(uc) >> 1) & 1 : 0,
232 &MASK_sig(uc));
233}
234
235#elif defined(_ARCH_PPC)
236
237
238
239
240
241#ifdef linux
242
243#define REG_sig(reg_name, context) \
244 ((context)->uc_mcontext.regs->reg_name)
245
246#define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
247
248#define IAR_sig(context) REG_sig(nip, context)
249
250#define MSR_sig(context) REG_sig(msr, context)
251
252#define CTR_sig(context) REG_sig(ctr, context)
253
254#define XER_sig(context) REG_sig(xer, context)
255
256#define LR_sig(context) REG_sig(link, context)
257
258#define CR_sig(context) REG_sig(ccr, context)
259
260
261#define FLOAT_sig(reg_num, context) \
262 (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
263#define FPSCR_sig(context) \
264 (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
265
266#define DAR_sig(context) REG_sig(dar, context)
267#define DSISR_sig(context) REG_sig(dsisr, context)
268#define TRAP_sig(context) REG_sig(trap, context)
269#endif
270
271#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
272#include <ucontext.h>
273#define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
274#define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
275#define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
276#define XER_sig(context) ((context)->uc_mcontext.mc_xer)
277#define LR_sig(context) ((context)->uc_mcontext.mc_lr)
278#define CR_sig(context) ((context)->uc_mcontext.mc_cr)
279
280#define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
281#define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
282#define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
283#endif
284
285int cpu_signal_handler(int host_signum, void *pinfo,
286 void *puc)
287{
288 siginfo_t *info = pinfo;
289#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
290 ucontext_t *uc = puc;
291#else
292 ucontext_t *uc = puc;
293#endif
294 unsigned long pc;
295 int is_write;
296
297 pc = IAR_sig(uc);
298 is_write = 0;
299#if 0
300
301 if (DSISR_sig(uc) & 0x00800000) {
302 is_write = 1;
303 }
304#else
305 if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
306 is_write = 1;
307 }
308#endif
309 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
310 is_write, &uc->uc_sigmask);
311}
312
313#elif defined(__alpha__)
314
315int cpu_signal_handler(int host_signum, void *pinfo,
316 void *puc)
317{
318 siginfo_t *info = pinfo;
319 ucontext_t *uc = puc;
320 uint32_t *pc = uc->uc_mcontext.sc_pc;
321 uint32_t insn = *pc;
322 int is_write = 0;
323
324
325 switch (insn >> 26) {
326 case 0x0d:
327 case 0x0e:
328 case 0x0f:
329 case 0x24:
330 case 0x25:
331 case 0x26:
332 case 0x27:
333 case 0x2c:
334 case 0x2d:
335 case 0x2e:
336 case 0x2f:
337 is_write = 1;
338 }
339
340 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
341 is_write, &uc->uc_sigmask);
342}
343#elif defined(__sparc__)
344
345int cpu_signal_handler(int host_signum, void *pinfo,
346 void *puc)
347{
348 siginfo_t *info = pinfo;
349 int is_write;
350 uint32_t insn;
351#if !defined(__arch64__) || defined(CONFIG_SOLARIS)
352 uint32_t *regs = (uint32_t *)(info + 1);
353 void *sigmask = (regs + 20);
354
355 unsigned long pc = regs[1];
356#else
357#ifdef __linux__
358 struct sigcontext *sc = puc;
359 unsigned long pc = sc->sigc_regs.tpc;
360 void *sigmask = (void *)sc->sigc_mask;
361#elif defined(__OpenBSD__)
362 struct sigcontext *uc = puc;
363 unsigned long pc = uc->sc_pc;
364 void *sigmask = (void *)(long)uc->sc_mask;
365#elif defined(__NetBSD__)
366 ucontext_t *uc = puc;
367 unsigned long pc = _UC_MACHINE_PC(uc);
368 void *sigmask = (void *)&uc->uc_sigmask;
369#endif
370#endif
371
372
373 is_write = 0;
374 insn = *(uint32_t *)pc;
375 if ((insn >> 30) == 3) {
376 switch ((insn >> 19) & 0x3f) {
377 case 0x05:
378 case 0x15:
379 case 0x06:
380 case 0x16:
381 case 0x04:
382 case 0x14:
383 case 0x07:
384 case 0x17:
385 case 0x0e:
386 case 0x1e:
387 case 0x24:
388 case 0x34:
389 case 0x27:
390 case 0x37:
391 case 0x26:
392 case 0x36:
393 case 0x25:
394 case 0x3c:
395 case 0x3e:
396 is_write = 1;
397 break;
398 }
399 }
400 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
401 is_write, sigmask);
402}
403
404#elif defined(__arm__)
405
406#if defined(__NetBSD__)
407#include <ucontext.h>
408#endif
409
410int cpu_signal_handler(int host_signum, void *pinfo,
411 void *puc)
412{
413 siginfo_t *info = pinfo;
414#if defined(__NetBSD__)
415 ucontext_t *uc = puc;
416#else
417 ucontext_t *uc = puc;
418#endif
419 unsigned long pc;
420 int is_write;
421
422#if defined(__NetBSD__)
423 pc = uc->uc_mcontext.__gregs[_REG_R15];
424#elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
425 pc = uc->uc_mcontext.gregs[R15];
426#else
427 pc = uc->uc_mcontext.arm_pc;
428#endif
429
430
431
432
433 is_write = extract32(uc->uc_mcontext.error_code, 11, 1);
434 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
435 is_write,
436 &uc->uc_sigmask);
437}
438
439#elif defined(__aarch64__)
440
441int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
442{
443 siginfo_t *info = pinfo;
444 ucontext_t *uc = puc;
445 uintptr_t pc = uc->uc_mcontext.pc;
446 uint32_t insn = *(uint32_t *)pc;
447 bool is_write;
448
449
450 is_write = ( (insn & 0xbfff0000) == 0x0c000000
451 || (insn & 0xbfe00000) == 0x0c800000
452 || (insn & 0xbfdf0000) == 0x0d000000
453 || (insn & 0xbfc00000) == 0x0d800000
454 || (insn & 0x3f400000) == 0x08000000
455 || (insn & 0x3bc00000) == 0x39000000
456 || (insn & 0x3fc00000) == 0x3d800000
457
458 || (insn & 0x3bc00000) == 0x38000000
459 || (insn & 0x3fe00000) == 0x3c800000
460
461 || (insn & 0x3a400000) == 0x28000000);
462
463 return handle_cpu_signal(pc, (uintptr_t)info->si_addr,
464 is_write, &uc->uc_sigmask);
465}
466
467#elif defined(__ia64)
468
469#ifndef __ISR_VALID
470
471# define __ISR_VALID 1
472#endif
473
474int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
475{
476 siginfo_t *info = pinfo;
477 ucontext_t *uc = puc;
478 unsigned long ip;
479 int is_write = 0;
480
481 ip = uc->uc_mcontext.sc_ip;
482 switch (host_signum) {
483 case SIGILL:
484 case SIGFPE:
485 case SIGSEGV:
486 case SIGBUS:
487 case SIGTRAP:
488 if (info->si_code && (info->si_segvflags & __ISR_VALID)) {
489
490 is_write = (info->si_isr >> 33) & 1;
491 }
492 break;
493
494 default:
495 break;
496 }
497 return handle_cpu_signal(ip, (unsigned long)info->si_addr,
498 is_write,
499 (sigset_t *)&uc->uc_sigmask);
500}
501
502#elif defined(__s390__)
503
504int cpu_signal_handler(int host_signum, void *pinfo,
505 void *puc)
506{
507 siginfo_t *info = pinfo;
508 ucontext_t *uc = puc;
509 unsigned long pc;
510 uint16_t *pinsn;
511 int is_write = 0;
512
513 pc = uc->uc_mcontext.psw.addr;
514
515
516
517
518
519
520
521
522 pinsn = (uint16_t *)pc;
523 switch (pinsn[0] >> 8) {
524 case 0x50:
525 case 0x42:
526 case 0x40:
527 is_write = 1;
528 break;
529 case 0xc4:
530 switch (pinsn[0] & 0xf) {
531 case 0xf:
532 case 0xb:
533 case 0x7:
534 is_write = 1;
535 }
536 break;
537 case 0xe3:
538 switch (pinsn[2] & 0xff) {
539 case 0x50:
540 case 0x24:
541 case 0x72:
542 case 0x70:
543 case 0x8e:
544 case 0x3f:
545 case 0x3e:
546 case 0x2f:
547 is_write = 1;
548 }
549 break;
550 }
551 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
552 is_write, &uc->uc_sigmask);
553}
554
555#elif defined(__mips__)
556
557int cpu_signal_handler(int host_signum, void *pinfo,
558 void *puc)
559{
560 siginfo_t *info = pinfo;
561 ucontext_t *uc = puc;
562 greg_t pc = uc->uc_mcontext.pc;
563 int is_write;
564
565
566 is_write = 0;
567 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
568 is_write, &uc->uc_sigmask);
569}
570
571#else
572
573#error host CPU specific signal handler needed
574
575#endif
576