2
0

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