signal.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. return ROUND_DOWN(sp - frame_size, TARGET_SIGSTACK_ALIGN);
  645. }
  646. /* compare to $M/$M/exec_machdep.c sendsig and sys/kern/kern_sig.c sigexit */
  647. static void setup_frame(int sig, int code, struct target_sigaction *ka,
  648. target_sigset_t *set, target_siginfo_t *tinfo, CPUArchState *env)
  649. {
  650. struct target_sigframe *frame;
  651. abi_ulong frame_addr;
  652. int i;
  653. frame_addr = get_sigframe(ka, env, sizeof(*frame));
  654. trace_user_setup_frame(env, frame_addr);
  655. if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
  656. unlock_user_struct(frame, frame_addr, 1);
  657. dump_core_and_abort(TARGET_SIGILL);
  658. return;
  659. }
  660. memset(frame, 0, sizeof(*frame));
  661. setup_sigframe_arch(env, frame_addr, frame, 0);
  662. for (i = 0; i < TARGET_NSIG_WORDS; i++) {
  663. __put_user(set->__bits[i], &frame->sf_uc.uc_sigmask.__bits[i]);
  664. }
  665. if (tinfo) {
  666. frame->sf_si.si_signo = tinfo->si_signo;
  667. frame->sf_si.si_errno = tinfo->si_errno;
  668. frame->sf_si.si_code = tinfo->si_code;
  669. frame->sf_si.si_pid = tinfo->si_pid;
  670. frame->sf_si.si_uid = tinfo->si_uid;
  671. frame->sf_si.si_status = tinfo->si_status;
  672. frame->sf_si.si_addr = tinfo->si_addr;
  673. /* see host_to_target_siginfo_noswap() for more details */
  674. frame->sf_si.si_value.sival_ptr = tinfo->si_value.sival_ptr;
  675. /*
  676. * At this point, whatever is in the _reason union is complete
  677. * and in target order, so just copy the whole thing over, even
  678. * if it's too large for this specific signal.
  679. * host_to_target_siginfo_noswap() and tswap_siginfo() have ensured
  680. * that's so.
  681. */
  682. memcpy(&frame->sf_si._reason, &tinfo->_reason,
  683. sizeof(tinfo->_reason));
  684. }
  685. set_sigtramp_args(env, sig, frame, frame_addr, ka);
  686. unlock_user_struct(frame, frame_addr, 1);
  687. }
  688. static int reset_signal_mask(target_ucontext_t *ucontext)
  689. {
  690. int i;
  691. sigset_t blocked;
  692. target_sigset_t target_set;
  693. TaskState *ts = get_task_state(thread_cpu);
  694. for (i = 0; i < TARGET_NSIG_WORDS; i++) {
  695. __get_user(target_set.__bits[i], &ucontext->uc_sigmask.__bits[i]);
  696. }
  697. target_to_host_sigset_internal(&blocked, &target_set);
  698. ts->signal_mask = blocked;
  699. return 0;
  700. }
  701. /* See sys/$M/$M/exec_machdep.c sigreturn() */
  702. long do_sigreturn(CPUArchState *env, abi_ulong addr)
  703. {
  704. long ret;
  705. abi_ulong target_ucontext;
  706. target_ucontext_t *ucontext = NULL;
  707. /* Get the target ucontext address from the stack frame */
  708. ret = get_ucontext_sigreturn(env, addr, &target_ucontext);
  709. if (is_error(ret)) {
  710. return ret;
  711. }
  712. trace_user_do_sigreturn(env, addr);
  713. if (!lock_user_struct(VERIFY_READ, ucontext, target_ucontext, 0)) {
  714. goto badframe;
  715. }
  716. /* Set the register state back to before the signal. */
  717. if (set_mcontext(env, &ucontext->uc_mcontext, 1)) {
  718. goto badframe;
  719. }
  720. /* And reset the signal mask. */
  721. if (reset_signal_mask(ucontext)) {
  722. goto badframe;
  723. }
  724. unlock_user_struct(ucontext, target_ucontext, 0);
  725. return -TARGET_EJUSTRETURN;
  726. badframe:
  727. if (ucontext != NULL) {
  728. unlock_user_struct(ucontext, target_ucontext, 0);
  729. }
  730. return -TARGET_EFAULT;
  731. }
  732. void signal_init(void)
  733. {
  734. TaskState *ts = get_task_state(thread_cpu);
  735. struct sigaction act;
  736. struct sigaction oact;
  737. int i;
  738. int host_sig;
  739. /* Set the signal mask from the host mask. */
  740. sigprocmask(0, 0, &ts->signal_mask);
  741. sigfillset(&act.sa_mask);
  742. act.sa_sigaction = host_signal_handler;
  743. act.sa_flags = SA_SIGINFO;
  744. for (i = 1; i <= TARGET_NSIG; i++) {
  745. host_sig = target_to_host_signal(i);
  746. sigaction(host_sig, NULL, &oact);
  747. if (oact.sa_sigaction == (void *)SIG_IGN) {
  748. sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
  749. } else if (oact.sa_sigaction == (void *)SIG_DFL) {
  750. sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
  751. }
  752. /*
  753. * If there's already a handler installed then something has
  754. * gone horribly wrong, so don't even try to handle that case.
  755. * Install some handlers for our own use. We need at least
  756. * SIGSEGV and SIGBUS, to detect exceptions. We can not just
  757. * trap all signals because it affects syscall interrupt
  758. * behavior. But do trap all default-fatal signals.
  759. */
  760. if (fatal_signal(i)) {
  761. sigaction(host_sig, &act, NULL);
  762. }
  763. }
  764. }
  765. static void handle_pending_signal(CPUArchState *env, int sig,
  766. struct emulated_sigtable *k)
  767. {
  768. CPUState *cpu = env_cpu(env);
  769. TaskState *ts = get_task_state(cpu);
  770. struct target_sigaction *sa;
  771. int code;
  772. sigset_t set;
  773. abi_ulong handler;
  774. target_siginfo_t tinfo;
  775. target_sigset_t target_old_set;
  776. trace_user_handle_signal(env, sig);
  777. k->pending = 0;
  778. sig = gdb_handlesig(cpu, sig, NULL, &k->info, sizeof(k->info));
  779. if (!sig) {
  780. sa = NULL;
  781. handler = TARGET_SIG_IGN;
  782. } else {
  783. sa = &sigact_table[sig - 1];
  784. handler = sa->_sa_handler;
  785. }
  786. if (do_strace) {
  787. print_taken_signal(sig, &k->info);
  788. }
  789. if (handler == TARGET_SIG_DFL) {
  790. /*
  791. * default handler : ignore some signal. The other are job
  792. * control or fatal.
  793. */
  794. if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN ||
  795. sig == TARGET_SIGTTOU) {
  796. kill(getpid(), SIGSTOP);
  797. } else if (sig != TARGET_SIGCHLD && sig != TARGET_SIGURG &&
  798. sig != TARGET_SIGINFO && sig != TARGET_SIGWINCH &&
  799. sig != TARGET_SIGCONT) {
  800. dump_core_and_abort(sig);
  801. }
  802. } else if (handler == TARGET_SIG_IGN) {
  803. /* ignore sig */
  804. } else if (handler == TARGET_SIG_ERR) {
  805. dump_core_and_abort(sig);
  806. } else {
  807. /* compute the blocked signals during the handler execution */
  808. sigset_t *blocked_set;
  809. target_to_host_sigset(&set, &sa->sa_mask);
  810. /*
  811. * SA_NODEFER indicates that the current signal should not be
  812. * blocked during the handler.
  813. */
  814. if (!(sa->sa_flags & TARGET_SA_NODEFER)) {
  815. sigaddset(&set, target_to_host_signal(sig));
  816. }
  817. /*
  818. * Save the previous blocked signal state to restore it at the
  819. * end of the signal execution (see do_sigreturn).
  820. */
  821. host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
  822. blocked_set = ts->in_sigsuspend ?
  823. &ts->sigsuspend_mask : &ts->signal_mask;
  824. sigorset(&ts->signal_mask, blocked_set, &set);
  825. ts->in_sigsuspend = false;
  826. sigprocmask(SIG_SETMASK, &ts->signal_mask, NULL);
  827. /* XXX VM86 on x86 ??? */
  828. code = k->info.si_code; /* From host, so no si_type */
  829. /* prepare the stack frame of the virtual CPU */
  830. if (sa->sa_flags & TARGET_SA_SIGINFO) {
  831. tswap_siginfo(&tinfo, &k->info);
  832. setup_frame(sig, code, sa, &target_old_set, &tinfo, env);
  833. } else {
  834. setup_frame(sig, code, sa, &target_old_set, NULL, env);
  835. }
  836. if (sa->sa_flags & TARGET_SA_RESETHAND) {
  837. sa->_sa_handler = TARGET_SIG_DFL;
  838. }
  839. }
  840. }
  841. void process_pending_signals(CPUArchState *env)
  842. {
  843. CPUState *cpu = env_cpu(env);
  844. int sig;
  845. sigset_t *blocked_set, set;
  846. struct emulated_sigtable *k;
  847. TaskState *ts = get_task_state(cpu);
  848. while (qatomic_read(&ts->signal_pending)) {
  849. sigfillset(&set);
  850. sigprocmask(SIG_SETMASK, &set, 0);
  851. restart_scan:
  852. sig = ts->sync_signal.pending;
  853. if (sig) {
  854. /*
  855. * Synchronous signals are forced by the emulated CPU in some way.
  856. * If they are set to ignore, restore the default handler (see
  857. * sys/kern_sig.c trapsignal() and execsigs() for this behavior)
  858. * though maybe this is done only when forcing exit for non SIGCHLD.
  859. */
  860. if (sigismember(&ts->signal_mask, target_to_host_signal(sig)) ||
  861. sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
  862. sigdelset(&ts->signal_mask, target_to_host_signal(sig));
  863. sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
  864. }
  865. handle_pending_signal(env, sig, &ts->sync_signal);
  866. }
  867. k = ts->sigtab;
  868. for (sig = 1; sig <= TARGET_NSIG; sig++, k++) {
  869. blocked_set = ts->in_sigsuspend ?
  870. &ts->sigsuspend_mask : &ts->signal_mask;
  871. if (k->pending &&
  872. !sigismember(blocked_set, target_to_host_signal(sig))) {
  873. handle_pending_signal(env, sig, k);
  874. /*
  875. * Restart scan from the beginning, as handle_pending_signal
  876. * might have resulted in a new synchronous signal (eg SIGSEGV).
  877. */
  878. goto restart_scan;
  879. }
  880. }
  881. /*
  882. * Unblock signals and check one more time. Unblocking signals may cause
  883. * us to take another host signal, which will set signal_pending again.
  884. */
  885. qatomic_set(&ts->signal_pending, 0);
  886. ts->in_sigsuspend = false;
  887. set = ts->signal_mask;
  888. sigdelset(&set, SIGSEGV);
  889. sigdelset(&set, SIGBUS);
  890. sigprocmask(SIG_SETMASK, &set, 0);
  891. }
  892. ts->in_sigsuspend = false;
  893. }
  894. void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
  895. MMUAccessType access_type, bool maperr, uintptr_t ra)
  896. {
  897. const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
  898. if (tcg_ops->record_sigsegv) {
  899. tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
  900. }
  901. force_sig_fault(TARGET_SIGSEGV,
  902. maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
  903. addr);
  904. cpu->exception_index = EXCP_INTERRUPT;
  905. cpu_loop_exit_restore(cpu, ra);
  906. }
  907. void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
  908. MMUAccessType access_type, uintptr_t ra)
  909. {
  910. const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
  911. if (tcg_ops->record_sigbus) {
  912. tcg_ops->record_sigbus(cpu, addr, access_type, ra);
  913. }
  914. force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
  915. cpu->exception_index = EXCP_INTERRUPT;
  916. cpu_loop_exit_restore(cpu, ra);
  917. }