signal.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * Emulation of BSD signals
  3. *
  4. * Copyright (c) 2003 - 2008 Fabrice Bellard
  5. * Copyright (c) 2013 Stacey Son
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/log.h"
  22. #include "qemu.h"
  23. #include "exec/page-protection.h"
  24. #include "user/tswap-target.h"
  25. #include "gdbstub/user.h"
  26. #include "signal-common.h"
  27. #include "trace.h"
  28. #include "hw/core/tcg-cpu-ops.h"
  29. #include "host-signal.h"
  30. /* target_siginfo_t must fit in gdbstub's siginfo save area. */
  31. QEMU_BUILD_BUG_ON(sizeof(target_siginfo_t) > MAX_SIGINFO_LENGTH);
  32. static struct target_sigaction sigact_table[TARGET_NSIG];
  33. static void host_signal_handler(int host_sig, siginfo_t *info, void *puc);
  34. static void target_to_host_sigset_internal(sigset_t *d,
  35. const target_sigset_t *s);
  36. static inline int on_sig_stack(TaskState *ts, unsigned long sp)
  37. {
  38. return sp - ts->sigaltstack_used.ss_sp < ts->sigaltstack_used.ss_size;
  39. }
  40. static inline int sas_ss_flags(TaskState *ts, unsigned long sp)
  41. {
  42. return ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE :
  43. on_sig_stack(ts, sp) ? SS_ONSTACK : 0;
  44. }
  45. /*
  46. * The BSD ABIs use the same signal numbers across all the CPU architectures, so
  47. * (unlike Linux) these functions are just the identity mapping. This might not
  48. * be true for XyzBSD running on AbcBSD, which doesn't currently work.
  49. */
  50. int host_to_target_signal(int sig)
  51. {
  52. return sig;
  53. }
  54. int target_to_host_signal(int sig)
  55. {
  56. return sig;
  57. }
  58. static inline void target_sigemptyset(target_sigset_t *set)
  59. {
  60. memset(set, 0, sizeof(*set));
  61. }
  62. static inline void target_sigaddset(target_sigset_t *set, int signum)
  63. {
  64. signum--;
  65. uint32_t mask = (uint32_t)1 << (signum % TARGET_NSIG_BPW);
  66. set->__bits[signum / TARGET_NSIG_BPW] |= mask;
  67. }
  68. static inline int target_sigismember(const target_sigset_t *set, int signum)
  69. {
  70. signum--;
  71. abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
  72. return (set->__bits[signum / TARGET_NSIG_BPW] & mask) != 0;
  73. }
  74. /* Adjust the signal context to rewind out of safe-syscall if we're in it */
  75. static inline void rewind_if_in_safe_syscall(void *puc)
  76. {
  77. ucontext_t *uc = (ucontext_t *)puc;
  78. uintptr_t pcreg = host_signal_pc(uc);
  79. if (pcreg > (uintptr_t)safe_syscall_start
  80. && pcreg < (uintptr_t)safe_syscall_end) {
  81. host_signal_set_pc(uc, (uintptr_t)safe_syscall_start);
  82. }
  83. }
  84. /*
  85. * Note: The following take advantage of the BSD signal property that all
  86. * signals are available on all architectures.
  87. */
  88. static void host_to_target_sigset_internal(target_sigset_t *d,
  89. const sigset_t *s)
  90. {
  91. int i;
  92. target_sigemptyset(d);
  93. for (i = 1; i <= NSIG; i++) {
  94. if (sigismember(s, i)) {
  95. target_sigaddset(d, host_to_target_signal(i));
  96. }
  97. }
  98. }
  99. void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
  100. {
  101. target_sigset_t d1;
  102. int i;
  103. host_to_target_sigset_internal(&d1, s);
  104. for (i = 0; i < _SIG_WORDS; i++) {
  105. d->__bits[i] = tswap32(d1.__bits[i]);
  106. }
  107. }
  108. static void target_to_host_sigset_internal(sigset_t *d,
  109. const target_sigset_t *s)
  110. {
  111. int i;
  112. sigemptyset(d);
  113. for (i = 1; i <= TARGET_NSIG; i++) {
  114. if (target_sigismember(s, i)) {
  115. sigaddset(d, target_to_host_signal(i));
  116. }
  117. }
  118. }
  119. void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
  120. {
  121. target_sigset_t s1;
  122. int i;
  123. for (i = 0; i < TARGET_NSIG_WORDS; i++) {
  124. s1.__bits[i] = tswap32(s->__bits[i]);
  125. }
  126. target_to_host_sigset_internal(d, &s1);
  127. }
  128. static bool has_trapno(int tsig)
  129. {
  130. return tsig == TARGET_SIGILL ||
  131. tsig == TARGET_SIGFPE ||
  132. tsig == TARGET_SIGSEGV ||
  133. tsig == TARGET_SIGBUS ||
  134. tsig == TARGET_SIGTRAP;
  135. }
  136. /* Siginfo conversion. */
  137. /*
  138. * Populate tinfo w/o swapping based on guessing which fields are valid.
  139. */
  140. static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
  141. const siginfo_t *info)
  142. {
  143. int sig = host_to_target_signal(info->si_signo);
  144. int si_code = info->si_code;
  145. int si_type;
  146. /*
  147. * Make sure we that the variable portion of the target siginfo is zeroed
  148. * out so we don't leak anything into that.
  149. */
  150. memset(&tinfo->_reason, 0, sizeof(tinfo->_reason));
  151. /*
  152. * This is awkward, because we have to use a combination of the si_code and
  153. * si_signo to figure out which of the union's members are valid.o We
  154. * therefore make our best guess.
  155. *
  156. * Once we have made our guess, we record it in the top 16 bits of
  157. * the si_code, so that tswap_siginfo() later can use it.
  158. * tswap_siginfo() will strip these top bits out before writing
  159. * si_code to the guest (sign-extending the lower bits).
  160. */
  161. tinfo->si_signo = sig;
  162. tinfo->si_errno = info->si_errno;
  163. tinfo->si_code = info->si_code;
  164. tinfo->si_pid = info->si_pid;
  165. tinfo->si_uid = info->si_uid;
  166. tinfo->si_status = info->si_status;
  167. tinfo->si_addr = (abi_ulong)(unsigned long)info->si_addr;
  168. /*
  169. * si_value is opaque to kernel. On all FreeBSD platforms,
  170. * sizeof(sival_ptr) >= sizeof(sival_int) so the following
  171. * always will copy the larger element.
  172. */
  173. tinfo->si_value.sival_ptr =
  174. (abi_ulong)(unsigned long)info->si_value.sival_ptr;
  175. switch (si_code) {
  176. /*
  177. * All the SI_xxx codes that are defined here are global to
  178. * all the signals (they have values that none of the other,
  179. * more specific signal info will set).
  180. */
  181. case SI_USER:
  182. case SI_LWP:
  183. case SI_KERNEL:
  184. case SI_QUEUE:
  185. case SI_ASYNCIO:
  186. /*
  187. * Only the fixed parts are valid (though FreeBSD doesn't always
  188. * set all the fields to non-zero values.
  189. */
  190. si_type = QEMU_SI_NOINFO;
  191. break;
  192. case SI_TIMER:
  193. tinfo->_reason._timer._timerid = info->_reason._timer._timerid;
  194. tinfo->_reason._timer._overrun = info->_reason._timer._overrun;
  195. si_type = QEMU_SI_TIMER;
  196. break;
  197. case SI_MESGQ:
  198. tinfo->_reason._mesgq._mqd = info->_reason._mesgq._mqd;
  199. si_type = QEMU_SI_MESGQ;
  200. break;
  201. default:
  202. /*
  203. * We have to go based on the signal number now to figure out
  204. * what's valid.
  205. */
  206. si_type = QEMU_SI_NOINFO;
  207. if (has_trapno(sig)) {
  208. tinfo->_reason._fault._trapno = info->_reason._fault._trapno;
  209. si_type = QEMU_SI_FAULT;
  210. }
  211. #ifdef TARGET_SIGPOLL
  212. /*
  213. * FreeBSD never had SIGPOLL, but emulates it for Linux so there's
  214. * a chance it may popup in the future.
  215. */
  216. if (sig == TARGET_SIGPOLL) {
  217. tinfo->_reason._poll._band = info->_reason._poll._band;
  218. si_type = QEMU_SI_POLL;
  219. }
  220. #endif
  221. /*
  222. * Unsure that this can actually be generated, and our support for
  223. * capsicum is somewhere between weak and non-existent, but if we get
  224. * one, then we know what to save.
  225. */
  226. #ifdef QEMU_SI_CAPSICUM
  227. if (sig == TARGET_SIGTRAP) {
  228. tinfo->_reason._capsicum._syscall =
  229. info->_reason._capsicum._syscall;
  230. si_type = QEMU_SI_CAPSICUM;
  231. }
  232. #endif
  233. break;
  234. }
  235. tinfo->si_code = deposit32(si_code, 24, 8, si_type);
  236. }
  237. static void tswap_siginfo(target_siginfo_t *tinfo, const target_siginfo_t *info)
  238. {
  239. int si_type = extract32(info->si_code, 24, 8);
  240. int si_code = sextract32(info->si_code, 0, 24);
  241. __put_user(info->si_signo, &tinfo->si_signo);
  242. __put_user(info->si_errno, &tinfo->si_errno);
  243. __put_user(si_code, &tinfo->si_code); /* Zero out si_type, it's internal */
  244. __put_user(info->si_pid, &tinfo->si_pid);
  245. __put_user(info->si_uid, &tinfo->si_uid);
  246. __put_user(info->si_status, &tinfo->si_status);
  247. __put_user(info->si_addr, &tinfo->si_addr);
  248. /*
  249. * Unswapped, because we passed it through mostly untouched. si_value is
  250. * opaque to the kernel, so we didn't bother with potentially wasting cycles
  251. * to swap it into host byte order.
  252. */
  253. tinfo->si_value.sival_ptr = info->si_value.sival_ptr;
  254. /*
  255. * We can use our internal marker of which fields in the structure
  256. * are valid, rather than duplicating the guesswork of
  257. * host_to_target_siginfo_noswap() here.
  258. */
  259. switch (si_type) {
  260. case QEMU_SI_NOINFO: /* No additional info */
  261. break;
  262. case QEMU_SI_FAULT:
  263. __put_user(info->_reason._fault._trapno,
  264. &tinfo->_reason._fault._trapno);
  265. break;
  266. case QEMU_SI_TIMER:
  267. __put_user(info->_reason._timer._timerid,
  268. &tinfo->_reason._timer._timerid);
  269. __put_user(info->_reason._timer._overrun,
  270. &tinfo->_reason._timer._overrun);
  271. break;
  272. case QEMU_SI_MESGQ:
  273. __put_user(info->_reason._mesgq._mqd, &tinfo->_reason._mesgq._mqd);
  274. break;
  275. case QEMU_SI_POLL:
  276. /* Note: Not generated on FreeBSD */
  277. __put_user(info->_reason._poll._band, &tinfo->_reason._poll._band);
  278. break;
  279. #ifdef QEMU_SI_CAPSICUM
  280. case QEMU_SI_CAPSICUM:
  281. __put_user(info->_reason._capsicum._syscall,
  282. &tinfo->_reason._capsicum._syscall);
  283. break;
  284. #endif
  285. default:
  286. g_assert_not_reached();
  287. }
  288. }
  289. void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
  290. {
  291. host_to_target_siginfo_noswap(tinfo, info);
  292. tswap_siginfo(tinfo, tinfo);
  293. }
  294. int block_signals(void)
  295. {
  296. TaskState *ts = get_task_state(thread_cpu);
  297. sigset_t set;
  298. /*
  299. * It's OK to block everything including SIGSEGV, because we won't run any
  300. * further guest code before unblocking signals in
  301. * process_pending_signals(). We depend on the FreeBSD behavior here where
  302. * this will only affect this thread's signal mask. We don't use
  303. * pthread_sigmask which might seem more correct because that routine also
  304. * does odd things with SIGCANCEL to implement pthread_cancel().
  305. */
  306. sigfillset(&set);
  307. sigprocmask(SIG_SETMASK, &set, 0);
  308. return qatomic_xchg(&ts->signal_pending, 1);
  309. }
  310. /* Returns 1 if given signal should dump core if not handled. */
  311. static int core_dump_signal(int sig)
  312. {
  313. switch (sig) {
  314. case TARGET_SIGABRT:
  315. case TARGET_SIGFPE:
  316. case TARGET_SIGILL:
  317. case TARGET_SIGQUIT:
  318. case TARGET_SIGSEGV:
  319. case TARGET_SIGTRAP:
  320. case TARGET_SIGBUS:
  321. return 1;
  322. default:
  323. return 0;
  324. }
  325. }
  326. /* Abort execution with signal. */
  327. static G_NORETURN
  328. void dump_core_and_abort(int target_sig)
  329. {
  330. CPUState *cpu = thread_cpu;
  331. CPUArchState *env = cpu_env(cpu);
  332. TaskState *ts = get_task_state(cpu);
  333. int core_dumped = 0;
  334. int host_sig;
  335. struct sigaction act;
  336. host_sig = target_to_host_signal(target_sig);
  337. gdb_signalled(env, target_sig);
  338. /* Dump core if supported by target binary format */
  339. if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
  340. stop_all_tasks();
  341. core_dumped =
  342. ((*ts->bprm->core_dump)(target_sig, env) == 0);
  343. }
  344. if (core_dumped) {
  345. struct rlimit nodump;
  346. /*
  347. * We already dumped the core of target process, we don't want
  348. * a coredump of qemu itself.
  349. */
  350. getrlimit(RLIMIT_CORE, &nodump);
  351. nodump.rlim_cur = 0;
  352. setrlimit(RLIMIT_CORE, &nodump);
  353. (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) "
  354. "- %s\n", target_sig, strsignal(host_sig), "core dumped");
  355. }
  356. /*
  357. * The proper exit code for dying from an uncaught signal is
  358. * -<signal>. The kernel doesn't allow exit() or _exit() to pass
  359. * a negative value. To get the proper exit code we need to
  360. * actually die from an uncaught signal. Here the default signal
  361. * handler is installed, we send ourself a signal and we wait for
  362. * it to arrive.
  363. */
  364. memset(&act, 0, sizeof(act));
  365. sigfillset(&act.sa_mask);
  366. act.sa_handler = SIG_DFL;
  367. sigaction(host_sig, &act, NULL);
  368. kill(getpid(), host_sig);
  369. /*
  370. * Make sure the signal isn't masked (just reuse the mask inside
  371. * of act).
  372. */
  373. sigdelset(&act.sa_mask, host_sig);
  374. sigsuspend(&act.sa_mask);
  375. /* unreachable */
  376. abort();
  377. }
  378. /*
  379. * Queue a signal so that it will be send to the virtual CPU as soon as
  380. * possible.
  381. */
  382. void queue_signal(CPUArchState *env, int sig, int si_type,
  383. target_siginfo_t *info)
  384. {
  385. CPUState *cpu = env_cpu(env);
  386. TaskState *ts = get_task_state(cpu);
  387. trace_user_queue_signal(env, sig);
  388. info->si_code = deposit32(info->si_code, 24, 8, si_type);
  389. ts->sync_signal.info = *info;
  390. ts->sync_signal.pending = sig;
  391. /* Signal that a new signal is pending. */
  392. qatomic_set(&ts->signal_pending, 1);
  393. return;
  394. }
  395. static int fatal_signal(int sig)
  396. {
  397. switch (sig) {
  398. case TARGET_SIGCHLD:
  399. case TARGET_SIGURG:
  400. case TARGET_SIGWINCH:
  401. case TARGET_SIGINFO:
  402. /* Ignored by default. */
  403. return 0;
  404. case TARGET_SIGCONT:
  405. case TARGET_SIGSTOP:
  406. case TARGET_SIGTSTP:
  407. case TARGET_SIGTTIN:
  408. case TARGET_SIGTTOU:
  409. /* Job control signals. */
  410. return 0;
  411. default:
  412. return 1;
  413. }
  414. }
  415. /*
  416. * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
  417. * 'force' part is handled in process_pending_signals().
  418. */
  419. void force_sig_fault(int sig, int code, abi_ulong addr)
  420. {
  421. CPUState *cpu = thread_cpu;
  422. target_siginfo_t info = {};
  423. info.si_signo = sig;
  424. info.si_errno = 0;
  425. info.si_code = code;
  426. info.si_addr = addr;
  427. queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, &info);
  428. }
  429. static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
  430. {
  431. CPUState *cpu = thread_cpu;
  432. TaskState *ts = get_task_state(cpu);
  433. target_siginfo_t tinfo;
  434. ucontext_t *uc = puc;
  435. struct emulated_sigtable *k;
  436. int guest_sig;
  437. uintptr_t pc = 0;
  438. bool sync_sig = false;
  439. /*
  440. * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
  441. * handling wrt signal blocking and unwinding.
  442. */
  443. if ((host_sig == SIGSEGV || host_sig == SIGBUS) && info->si_code > 0) {
  444. MMUAccessType access_type;
  445. uintptr_t host_addr;
  446. abi_ptr guest_addr;
  447. bool is_write;
  448. host_addr = (uintptr_t)info->si_addr;
  449. /*
  450. * Convert forcefully to guest address space: addresses outside
  451. * reserved_va are still valid to report via SEGV_MAPERR.
  452. */
  453. guest_addr = h2g_nocheck(host_addr);
  454. pc = host_signal_pc(uc);
  455. is_write = host_signal_write(info, uc);
  456. access_type = adjust_signal_pc(&pc, is_write);
  457. if (host_sig == SIGSEGV) {
  458. bool maperr = true;
  459. if (info->si_code == SEGV_ACCERR && h2g_valid(host_addr)) {
  460. /* If this was a write to a TB protected page, restart. */
  461. if (is_write &&
  462. handle_sigsegv_accerr_write(cpu, &uc->uc_sigmask,
  463. pc, guest_addr)) {
  464. return;
  465. }
  466. /*
  467. * With reserved_va, the whole address space is PROT_NONE,
  468. * which means that we may get ACCERR when we want MAPERR.
  469. */
  470. if (page_get_flags(guest_addr) & PAGE_VALID) {
  471. maperr = false;
  472. } else {
  473. info->si_code = SEGV_MAPERR;
  474. }
  475. }
  476. sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
  477. cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc);
  478. } else {
  479. sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
  480. if (info->si_code == BUS_ADRALN) {
  481. cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc);
  482. }
  483. }
  484. sync_sig = true;
  485. }
  486. /* Get the target signal number. */
  487. guest_sig = host_to_target_signal(host_sig);
  488. if (guest_sig < 1 || guest_sig > TARGET_NSIG) {
  489. return;
  490. }
  491. trace_user_host_signal(cpu, host_sig, guest_sig);
  492. host_to_target_siginfo_noswap(&tinfo, info);
  493. k = &ts->sigtab[guest_sig - 1];
  494. k->info = tinfo;
  495. k->pending = guest_sig;
  496. ts->signal_pending = 1;
  497. /*
  498. * For synchronous signals, unwind the cpu state to the faulting
  499. * insn and then exit back to the main loop so that the signal
  500. * is delivered immediately.
  501. */
  502. if (sync_sig) {
  503. cpu->exception_index = EXCP_INTERRUPT;
  504. cpu_loop_exit_restore(cpu, pc);
  505. }
  506. rewind_if_in_safe_syscall(puc);
  507. /*
  508. * Block host signals until target signal handler entered. We
  509. * can't block SIGSEGV or SIGBUS while we're executing guest
  510. * code in case the guest code provokes one in the window between
  511. * now and it getting out to the main loop. Signals will be
  512. * unblocked again in process_pending_signals().
  513. */
  514. sigfillset(&uc->uc_sigmask);
  515. sigdelset(&uc->uc_sigmask, SIGSEGV);
  516. sigdelset(&uc->uc_sigmask, SIGBUS);
  517. /* Interrupt the virtual CPU as soon as possible. */
  518. cpu_exit(thread_cpu);
  519. }
  520. /* do_sigaltstack() returns target values and errnos. */
  521. /* compare to kern/kern_sig.c sys_sigaltstack() and kern_sigaltstack() */
  522. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
  523. {
  524. TaskState *ts = get_task_state(thread_cpu);
  525. int ret;
  526. target_stack_t oss;
  527. if (uoss_addr) {
  528. /* Save current signal stack params */
  529. oss.ss_sp = tswapl(ts->sigaltstack_used.ss_sp);
  530. oss.ss_size = tswapl(ts->sigaltstack_used.ss_size);
  531. oss.ss_flags = tswapl(sas_ss_flags(ts, sp));
  532. }
  533. if (uss_addr) {
  534. target_stack_t *uss;
  535. target_stack_t ss;
  536. size_t minstacksize = TARGET_MINSIGSTKSZ;
  537. ret = -TARGET_EFAULT;
  538. if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
  539. goto out;
  540. }
  541. __get_user(ss.ss_sp, &uss->ss_sp);
  542. __get_user(ss.ss_size, &uss->ss_size);
  543. __get_user(ss.ss_flags, &uss->ss_flags);
  544. unlock_user_struct(uss, uss_addr, 0);
  545. ret = -TARGET_EPERM;
  546. if (on_sig_stack(ts, sp)) {
  547. goto out;
  548. }
  549. ret = -TARGET_EINVAL;
  550. if (ss.ss_flags != TARGET_SS_DISABLE
  551. && ss.ss_flags != TARGET_SS_ONSTACK
  552. && ss.ss_flags != 0) {
  553. goto out;
  554. }
  555. if (ss.ss_flags == TARGET_SS_DISABLE) {
  556. ss.ss_size = 0;
  557. ss.ss_sp = 0;
  558. } else {
  559. ret = -TARGET_ENOMEM;
  560. if (ss.ss_size < minstacksize) {
  561. goto out;
  562. }
  563. }
  564. ts->sigaltstack_used.ss_sp = ss.ss_sp;
  565. ts->sigaltstack_used.ss_size = ss.ss_size;
  566. }
  567. if (uoss_addr) {
  568. ret = -TARGET_EFAULT;
  569. if (copy_to_user(uoss_addr, &oss, sizeof(oss))) {
  570. goto out;
  571. }
  572. }
  573. ret = 0;
  574. out:
  575. return ret;
  576. }
  577. /* do_sigaction() return host values and errnos */
  578. int do_sigaction(int sig, const struct target_sigaction *act,
  579. struct target_sigaction *oact)
  580. {
  581. struct target_sigaction *k;
  582. struct sigaction act1;
  583. int host_sig;
  584. int ret = 0;
  585. if (sig < 1 || sig > TARGET_NSIG) {
  586. return -TARGET_EINVAL;
  587. }
  588. if ((sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) &&
  589. act != NULL && act->_sa_handler != TARGET_SIG_DFL) {
  590. return -TARGET_EINVAL;
  591. }
  592. if (block_signals()) {
  593. return -TARGET_ERESTART;
  594. }
  595. k = &sigact_table[sig - 1];
  596. if (oact) {
  597. oact->_sa_handler = tswapal(k->_sa_handler);
  598. oact->sa_flags = tswap32(k->sa_flags);
  599. oact->sa_mask = k->sa_mask;
  600. }
  601. if (act) {
  602. k->_sa_handler = tswapal(act->_sa_handler);
  603. k->sa_flags = tswap32(act->sa_flags);
  604. k->sa_mask = act->sa_mask;
  605. /* Update the host signal state. */
  606. host_sig = target_to_host_signal(sig);
  607. if (host_sig != SIGSEGV && host_sig != SIGBUS) {
  608. memset(&act1, 0, sizeof(struct sigaction));
  609. sigfillset(&act1.sa_mask);
  610. act1.sa_flags = SA_SIGINFO;
  611. if (k->sa_flags & TARGET_SA_RESTART) {
  612. act1.sa_flags |= SA_RESTART;
  613. }
  614. /*
  615. * Note: It is important to update the host kernel signal mask to
  616. * avoid getting unexpected interrupted system calls.
  617. */
  618. if (k->_sa_handler == TARGET_SIG_IGN) {
  619. act1.sa_sigaction = (void *)SIG_IGN;
  620. } else if (k->_sa_handler == TARGET_SIG_DFL) {
  621. if (fatal_signal(sig)) {
  622. act1.sa_sigaction = host_signal_handler;
  623. } else {
  624. act1.sa_sigaction = (void *)SIG_DFL;
  625. }
  626. } else {
  627. act1.sa_sigaction = host_signal_handler;
  628. }
  629. ret = sigaction(host_sig, &act1, NULL);
  630. }
  631. }
  632. return ret;
  633. }
  634. static inline abi_ulong get_sigframe(struct target_sigaction *ka,
  635. CPUArchState *env, size_t frame_size)
  636. {
  637. TaskState *ts = get_task_state(thread_cpu);
  638. abi_ulong sp;
  639. /* Use default user stack */
  640. sp = get_sp_from_cpustate(env);
  641. if ((ka->sa_flags & TARGET_SA_ONSTACK) && sas_ss_flags(ts, sp) == 0) {
  642. sp = ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
  643. }
  644. /* TODO: make this a target_arch function / define */
  645. #if defined(TARGET_ARM)
  646. return (sp - frame_size) & ~7;
  647. #elif defined(TARGET_AARCH64)
  648. return (sp - frame_size) & ~15;
  649. #else
  650. return sp - frame_size;
  651. #endif
  652. }
  653. /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
  654. static void setup_frame(int sig, int code, struct target_sigaction *ka,
  655. target_sigset_t *set, target_siginfo_t *tinfo, CPUArchState *env)
  656. {
  657. struct target_sigframe *frame;
  658. abi_ulong frame_addr;
  659. int i;
  660. frame_addr = get_sigframe(ka, env, sizeof(*frame));
  661. trace_user_setup_frame(env, frame_addr);
  662. if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
  663. unlock_user_struct(frame, frame_addr, 1);
  664. dump_core_and_abort(TARGET_SIGILL);
  665. return;
  666. }
  667. memset(frame, 0, sizeof(*frame));
  668. setup_sigframe_arch(env, frame_addr, frame, 0);
  669. for (i = 0; i < TARGET_NSIG_WORDS; i++) {
  670. __put_user(set->__bits[i], &frame->sf_uc.uc_sigmask.__bits[i]);
  671. }
  672. if (tinfo) {
  673. frame->sf_si.si_signo = tinfo->si_signo;
  674. frame->sf_si.si_errno = tinfo->si_errno;
  675. frame->sf_si.si_code = tinfo->si_code;
  676. frame->sf_si.si_pid = tinfo->si_pid;
  677. frame->sf_si.si_uid = tinfo->si_uid;
  678. frame->sf_si.si_status = tinfo->si_status;
  679. frame->sf_si.si_addr = tinfo->si_addr;
  680. /* see host_to_target_siginfo_noswap() for more details */
  681. frame->sf_si.si_value.sival_ptr = tinfo->si_value.sival_ptr;
  682. /*
  683. * At this point, whatever is in the _reason union is complete
  684. * and in target order, so just copy the whole thing over, even
  685. * if it's too large for this specific signal.
  686. * host_to_target_siginfo_noswap() and tswap_siginfo() have ensured
  687. * that's so.
  688. */
  689. memcpy(&frame->sf_si._reason, &tinfo->_reason,
  690. sizeof(tinfo->_reason));
  691. }
  692. set_sigtramp_args(env, sig, frame, frame_addr, ka);
  693. unlock_user_struct(frame, frame_addr, 1);
  694. }
  695. static int reset_signal_mask(target_ucontext_t *ucontext)
  696. {
  697. int i;
  698. sigset_t blocked;
  699. target_sigset_t target_set;
  700. TaskState *ts = get_task_state(thread_cpu);
  701. for (i = 0; i < TARGET_NSIG_WORDS; i++) {
  702. __get_user(target_set.__bits[i], &ucontext->uc_sigmask.__bits[i]);
  703. }
  704. target_to_host_sigset_internal(&blocked, &target_set);
  705. ts->signal_mask = blocked;
  706. return 0;
  707. }
  708. /* See sys/$M/$M/exec_machdep.c sigreturn() */
  709. long do_sigreturn(CPUArchState *env, abi_ulong addr)
  710. {
  711. long ret;
  712. abi_ulong target_ucontext;
  713. target_ucontext_t *ucontext = NULL;
  714. /* Get the target ucontext address from the stack frame */
  715. ret = get_ucontext_sigreturn(env, addr, &target_ucontext);
  716. if (is_error(ret)) {
  717. return ret;
  718. }
  719. trace_user_do_sigreturn(env, addr);
  720. if (!lock_user_struct(VERIFY_READ, ucontext, target_ucontext, 0)) {
  721. goto badframe;
  722. }
  723. /* Set the register state back to before the signal. */
  724. if (set_mcontext(env, &ucontext->uc_mcontext, 1)) {
  725. goto badframe;
  726. }
  727. /* And reset the signal mask. */
  728. if (reset_signal_mask(ucontext)) {
  729. goto badframe;
  730. }
  731. unlock_user_struct(ucontext, target_ucontext, 0);
  732. return -TARGET_EJUSTRETURN;
  733. badframe:
  734. if (ucontext != NULL) {
  735. unlock_user_struct(ucontext, target_ucontext, 0);
  736. }
  737. return -TARGET_EFAULT;
  738. }
  739. void signal_init(void)
  740. {
  741. TaskState *ts = get_task_state(thread_cpu);
  742. struct sigaction act;
  743. struct sigaction oact;
  744. int i;
  745. int host_sig;
  746. /* Set the signal mask from the host mask. */
  747. sigprocmask(0, 0, &ts->signal_mask);
  748. sigfillset(&act.sa_mask);
  749. act.sa_sigaction = host_signal_handler;
  750. act.sa_flags = SA_SIGINFO;
  751. for (i = 1; i <= TARGET_NSIG; i++) {
  752. host_sig = target_to_host_signal(i);
  753. sigaction(host_sig, NULL, &oact);
  754. if (oact.sa_sigaction == (void *)SIG_IGN) {
  755. sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
  756. } else if (oact.sa_sigaction == (void *)SIG_DFL) {
  757. sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
  758. }
  759. /*
  760. * If there's already a handler installed then something has
  761. * gone horribly wrong, so don't even try to handle that case.
  762. * Install some handlers for our own use. We need at least
  763. * SIGSEGV and SIGBUS, to detect exceptions. We can not just
  764. * trap all signals because it affects syscall interrupt
  765. * behavior. But do trap all default-fatal signals.
  766. */
  767. if (fatal_signal(i)) {
  768. sigaction(host_sig, &act, NULL);
  769. }
  770. }
  771. }
  772. static void handle_pending_signal(CPUArchState *env, int sig,
  773. struct emulated_sigtable *k)
  774. {
  775. CPUState *cpu = env_cpu(env);
  776. TaskState *ts = get_task_state(cpu);
  777. struct target_sigaction *sa;
  778. int code;
  779. sigset_t set;
  780. abi_ulong handler;
  781. target_siginfo_t tinfo;
  782. target_sigset_t target_old_set;
  783. trace_user_handle_signal(env, sig);
  784. k->pending = 0;
  785. sig = gdb_handlesig(cpu, sig, NULL, &k->info, sizeof(k->info));
  786. if (!sig) {
  787. sa = NULL;
  788. handler = TARGET_SIG_IGN;
  789. } else {
  790. sa = &sigact_table[sig - 1];
  791. handler = sa->_sa_handler;
  792. }
  793. if (do_strace) {
  794. print_taken_signal(sig, &k->info);
  795. }
  796. if (handler == TARGET_SIG_DFL) {
  797. /*
  798. * default handler : ignore some signal. The other are job
  799. * control or fatal.
  800. */
  801. if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN ||
  802. sig == TARGET_SIGTTOU) {
  803. kill(getpid(), SIGSTOP);
  804. } else if (sig != TARGET_SIGCHLD && sig != TARGET_SIGURG &&
  805. sig != TARGET_SIGINFO && sig != TARGET_SIGWINCH &&
  806. sig != TARGET_SIGCONT) {
  807. dump_core_and_abort(sig);
  808. }
  809. } else if (handler == TARGET_SIG_IGN) {
  810. /* ignore sig */
  811. } else if (handler == TARGET_SIG_ERR) {
  812. dump_core_and_abort(sig);
  813. } else {
  814. /* compute the blocked signals during the handler execution */
  815. sigset_t *blocked_set;
  816. target_to_host_sigset(&set, &sa->sa_mask);
  817. /*
  818. * SA_NODEFER indicates that the current signal should not be
  819. * blocked during the handler.
  820. */
  821. if (!(sa->sa_flags & TARGET_SA_NODEFER)) {
  822. sigaddset(&set, target_to_host_signal(sig));
  823. }
  824. /*
  825. * Save the previous blocked signal state to restore it at the
  826. * end of the signal execution (see do_sigreturn).
  827. */
  828. host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
  829. blocked_set = ts->in_sigsuspend ?
  830. &ts->sigsuspend_mask : &ts->signal_mask;
  831. sigorset(&ts->signal_mask, blocked_set, &set);
  832. ts->in_sigsuspend = false;
  833. sigprocmask(SIG_SETMASK, &ts->signal_mask, NULL);
  834. /* XXX VM86 on x86 ??? */
  835. code = k->info.si_code; /* From host, so no si_type */
  836. /* prepare the stack frame of the virtual CPU */
  837. if (sa->sa_flags & TARGET_SA_SIGINFO) {
  838. tswap_siginfo(&tinfo, &k->info);
  839. setup_frame(sig, code, sa, &target_old_set, &tinfo, env);
  840. } else {
  841. setup_frame(sig, code, sa, &target_old_set, NULL, env);
  842. }
  843. if (sa->sa_flags & TARGET_SA_RESETHAND) {
  844. sa->_sa_handler = TARGET_SIG_DFL;
  845. }
  846. }
  847. }
  848. void process_pending_signals(CPUArchState *env)
  849. {
  850. CPUState *cpu = env_cpu(env);
  851. int sig;
  852. sigset_t *blocked_set, set;
  853. struct emulated_sigtable *k;
  854. TaskState *ts = get_task_state(cpu);
  855. while (qatomic_read(&ts->signal_pending)) {
  856. sigfillset(&set);
  857. sigprocmask(SIG_SETMASK, &set, 0);
  858. restart_scan:
  859. sig = ts->sync_signal.pending;
  860. if (sig) {
  861. /*
  862. * Synchronous signals are forced by the emulated CPU in some way.
  863. * If they are set to ignore, restore the default handler (see
  864. * sys/kern_sig.c trapsignal() and execsigs() for this behavior)
  865. * though maybe this is done only when forcing exit for non SIGCHLD.
  866. */
  867. if (sigismember(&ts->signal_mask, target_to_host_signal(sig)) ||
  868. sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
  869. sigdelset(&ts->signal_mask, target_to_host_signal(sig));
  870. sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
  871. }
  872. handle_pending_signal(env, sig, &ts->sync_signal);
  873. }
  874. k = ts->sigtab;
  875. for (sig = 1; sig <= TARGET_NSIG; sig++, k++) {
  876. blocked_set = ts->in_sigsuspend ?
  877. &ts->sigsuspend_mask : &ts->signal_mask;
  878. if (k->pending &&
  879. !sigismember(blocked_set, target_to_host_signal(sig))) {
  880. handle_pending_signal(env, sig, k);
  881. /*
  882. * Restart scan from the beginning, as handle_pending_signal
  883. * might have resulted in a new synchronous signal (eg SIGSEGV).
  884. */
  885. goto restart_scan;
  886. }
  887. }
  888. /*
  889. * Unblock signals and check one more time. Unblocking signals may cause
  890. * us to take another host signal, which will set signal_pending again.
  891. */
  892. qatomic_set(&ts->signal_pending, 0);
  893. ts->in_sigsuspend = false;
  894. set = ts->signal_mask;
  895. sigdelset(&set, SIGSEGV);
  896. sigdelset(&set, SIGBUS);
  897. sigprocmask(SIG_SETMASK, &set, 0);
  898. }
  899. ts->in_sigsuspend = false;
  900. }
  901. void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
  902. MMUAccessType access_type, bool maperr, uintptr_t ra)
  903. {
  904. const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
  905. if (tcg_ops->record_sigsegv) {
  906. tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
  907. }
  908. force_sig_fault(TARGET_SIGSEGV,
  909. maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
  910. addr);
  911. cpu->exception_index = EXCP_INTERRUPT;
  912. cpu_loop_exit_restore(cpu, ra);
  913. }
  914. void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
  915. MMUAccessType access_type, uintptr_t ra)
  916. {
  917. const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
  918. if (tcg_ops->record_sigbus) {
  919. tcg_ops->record_sigbus(cpu, addr, access_type, ra);
  920. }
  921. force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
  922. cpu->exception_index = EXCP_INTERRUPT;
  923. cpu_loop_exit_restore(cpu, ra);
  924. }