signal.c 32 KB

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