signal.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. /*
  2. * Emulation of Linux signals
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/bitops.h"
  21. #include <sys/ucontext.h>
  22. #include <sys/resource.h>
  23. #include "qemu.h"
  24. #include "trace.h"
  25. #include "signal-common.h"
  26. static struct target_sigaction sigact_table[TARGET_NSIG];
  27. static void host_signal_handler(int host_signum, siginfo_t *info,
  28. void *puc);
  29. static uint8_t host_to_target_signal_table[_NSIG] = {
  30. [SIGHUP] = TARGET_SIGHUP,
  31. [SIGINT] = TARGET_SIGINT,
  32. [SIGQUIT] = TARGET_SIGQUIT,
  33. [SIGILL] = TARGET_SIGILL,
  34. [SIGTRAP] = TARGET_SIGTRAP,
  35. [SIGABRT] = TARGET_SIGABRT,
  36. /* [SIGIOT] = TARGET_SIGIOT,*/
  37. [SIGBUS] = TARGET_SIGBUS,
  38. [SIGFPE] = TARGET_SIGFPE,
  39. [SIGKILL] = TARGET_SIGKILL,
  40. [SIGUSR1] = TARGET_SIGUSR1,
  41. [SIGSEGV] = TARGET_SIGSEGV,
  42. [SIGUSR2] = TARGET_SIGUSR2,
  43. [SIGPIPE] = TARGET_SIGPIPE,
  44. [SIGALRM] = TARGET_SIGALRM,
  45. [SIGTERM] = TARGET_SIGTERM,
  46. #ifdef SIGSTKFLT
  47. [SIGSTKFLT] = TARGET_SIGSTKFLT,
  48. #endif
  49. [SIGCHLD] = TARGET_SIGCHLD,
  50. [SIGCONT] = TARGET_SIGCONT,
  51. [SIGSTOP] = TARGET_SIGSTOP,
  52. [SIGTSTP] = TARGET_SIGTSTP,
  53. [SIGTTIN] = TARGET_SIGTTIN,
  54. [SIGTTOU] = TARGET_SIGTTOU,
  55. [SIGURG] = TARGET_SIGURG,
  56. [SIGXCPU] = TARGET_SIGXCPU,
  57. [SIGXFSZ] = TARGET_SIGXFSZ,
  58. [SIGVTALRM] = TARGET_SIGVTALRM,
  59. [SIGPROF] = TARGET_SIGPROF,
  60. [SIGWINCH] = TARGET_SIGWINCH,
  61. [SIGIO] = TARGET_SIGIO,
  62. [SIGPWR] = TARGET_SIGPWR,
  63. [SIGSYS] = TARGET_SIGSYS,
  64. /* next signals stay the same */
  65. /* Nasty hack: Reverse SIGRTMIN and SIGRTMAX to avoid overlap with
  66. host libpthread signals. This assumes no one actually uses SIGRTMAX :-/
  67. To fix this properly we need to do manual signal delivery multiplexed
  68. over a single host signal. */
  69. [__SIGRTMIN] = __SIGRTMAX,
  70. [__SIGRTMAX] = __SIGRTMIN,
  71. };
  72. static uint8_t target_to_host_signal_table[_NSIG];
  73. int host_to_target_signal(int sig)
  74. {
  75. if (sig < 0 || sig >= _NSIG)
  76. return sig;
  77. return host_to_target_signal_table[sig];
  78. }
  79. int target_to_host_signal(int sig)
  80. {
  81. if (sig < 0 || sig >= _NSIG)
  82. return sig;
  83. return target_to_host_signal_table[sig];
  84. }
  85. static inline void target_sigaddset(target_sigset_t *set, int signum)
  86. {
  87. signum--;
  88. abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
  89. set->sig[signum / TARGET_NSIG_BPW] |= mask;
  90. }
  91. static inline int target_sigismember(const target_sigset_t *set, int signum)
  92. {
  93. signum--;
  94. abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
  95. return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
  96. }
  97. void host_to_target_sigset_internal(target_sigset_t *d,
  98. const sigset_t *s)
  99. {
  100. int i;
  101. target_sigemptyset(d);
  102. for (i = 1; i <= TARGET_NSIG; i++) {
  103. if (sigismember(s, i)) {
  104. target_sigaddset(d, host_to_target_signal(i));
  105. }
  106. }
  107. }
  108. void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
  109. {
  110. target_sigset_t d1;
  111. int i;
  112. host_to_target_sigset_internal(&d1, s);
  113. for(i = 0;i < TARGET_NSIG_WORDS; i++)
  114. d->sig[i] = tswapal(d1.sig[i]);
  115. }
  116. void target_to_host_sigset_internal(sigset_t *d,
  117. const target_sigset_t *s)
  118. {
  119. int i;
  120. sigemptyset(d);
  121. for (i = 1; i <= TARGET_NSIG; i++) {
  122. if (target_sigismember(s, i)) {
  123. sigaddset(d, target_to_host_signal(i));
  124. }
  125. }
  126. }
  127. void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
  128. {
  129. target_sigset_t s1;
  130. int i;
  131. for(i = 0;i < TARGET_NSIG_WORDS; i++)
  132. s1.sig[i] = tswapal(s->sig[i]);
  133. target_to_host_sigset_internal(d, &s1);
  134. }
  135. void host_to_target_old_sigset(abi_ulong *old_sigset,
  136. const sigset_t *sigset)
  137. {
  138. target_sigset_t d;
  139. host_to_target_sigset(&d, sigset);
  140. *old_sigset = d.sig[0];
  141. }
  142. void target_to_host_old_sigset(sigset_t *sigset,
  143. const abi_ulong *old_sigset)
  144. {
  145. target_sigset_t d;
  146. int i;
  147. d.sig[0] = *old_sigset;
  148. for(i = 1;i < TARGET_NSIG_WORDS; i++)
  149. d.sig[i] = 0;
  150. target_to_host_sigset(sigset, &d);
  151. }
  152. int block_signals(void)
  153. {
  154. TaskState *ts = (TaskState *)thread_cpu->opaque;
  155. sigset_t set;
  156. /* It's OK to block everything including SIGSEGV, because we won't
  157. * run any further guest code before unblocking signals in
  158. * process_pending_signals().
  159. */
  160. sigfillset(&set);
  161. sigprocmask(SIG_SETMASK, &set, 0);
  162. return atomic_xchg(&ts->signal_pending, 1);
  163. }
  164. /* Wrapper for sigprocmask function
  165. * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
  166. * are host signal set, not guest ones. Returns -TARGET_ERESTARTSYS if
  167. * a signal was already pending and the syscall must be restarted, or
  168. * 0 on success.
  169. * If set is NULL, this is guaranteed not to fail.
  170. */
  171. int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
  172. {
  173. TaskState *ts = (TaskState *)thread_cpu->opaque;
  174. if (oldset) {
  175. *oldset = ts->signal_mask;
  176. }
  177. if (set) {
  178. int i;
  179. if (block_signals()) {
  180. return -TARGET_ERESTARTSYS;
  181. }
  182. switch (how) {
  183. case SIG_BLOCK:
  184. sigorset(&ts->signal_mask, &ts->signal_mask, set);
  185. break;
  186. case SIG_UNBLOCK:
  187. for (i = 1; i <= NSIG; ++i) {
  188. if (sigismember(set, i)) {
  189. sigdelset(&ts->signal_mask, i);
  190. }
  191. }
  192. break;
  193. case SIG_SETMASK:
  194. ts->signal_mask = *set;
  195. break;
  196. default:
  197. g_assert_not_reached();
  198. }
  199. /* Silently ignore attempts to change blocking status of KILL or STOP */
  200. sigdelset(&ts->signal_mask, SIGKILL);
  201. sigdelset(&ts->signal_mask, SIGSTOP);
  202. }
  203. return 0;
  204. }
  205. #if !defined(TARGET_NIOS2)
  206. /* Just set the guest's signal mask to the specified value; the
  207. * caller is assumed to have called block_signals() already.
  208. */
  209. void set_sigmask(const sigset_t *set)
  210. {
  211. TaskState *ts = (TaskState *)thread_cpu->opaque;
  212. ts->signal_mask = *set;
  213. }
  214. #endif
  215. /* sigaltstack management */
  216. int on_sig_stack(unsigned long sp)
  217. {
  218. TaskState *ts = (TaskState *)thread_cpu->opaque;
  219. return (sp - ts->sigaltstack_used.ss_sp
  220. < ts->sigaltstack_used.ss_size);
  221. }
  222. int sas_ss_flags(unsigned long sp)
  223. {
  224. TaskState *ts = (TaskState *)thread_cpu->opaque;
  225. return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE
  226. : on_sig_stack(sp) ? SS_ONSTACK : 0);
  227. }
  228. abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka)
  229. {
  230. /*
  231. * This is the X/Open sanctioned signal stack switching.
  232. */
  233. TaskState *ts = (TaskState *)thread_cpu->opaque;
  234. if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
  235. return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
  236. }
  237. return sp;
  238. }
  239. void target_save_altstack(target_stack_t *uss, CPUArchState *env)
  240. {
  241. TaskState *ts = (TaskState *)thread_cpu->opaque;
  242. __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp);
  243. __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags);
  244. __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
  245. }
  246. /* siginfo conversion */
  247. static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
  248. const siginfo_t *info)
  249. {
  250. int sig = host_to_target_signal(info->si_signo);
  251. int si_code = info->si_code;
  252. int si_type;
  253. tinfo->si_signo = sig;
  254. tinfo->si_errno = 0;
  255. tinfo->si_code = info->si_code;
  256. /* This memset serves two purposes:
  257. * (1) ensure we don't leak random junk to the guest later
  258. * (2) placate false positives from gcc about fields
  259. * being used uninitialized if it chooses to inline both this
  260. * function and tswap_siginfo() into host_to_target_siginfo().
  261. */
  262. memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
  263. /* This is awkward, because we have to use a combination of
  264. * the si_code and si_signo to figure out which of the union's
  265. * members are valid. (Within the host kernel it is always possible
  266. * to tell, but the kernel carefully avoids giving userspace the
  267. * high 16 bits of si_code, so we don't have the information to
  268. * do this the easy way...) We therefore make our best guess,
  269. * bearing in mind that a guest can spoof most of the si_codes
  270. * via rt_sigqueueinfo() if it likes.
  271. *
  272. * Once we have made our guess, we record it in the top 16 bits of
  273. * the si_code, so that tswap_siginfo() later can use it.
  274. * tswap_siginfo() will strip these top bits out before writing
  275. * si_code to the guest (sign-extending the lower bits).
  276. */
  277. switch (si_code) {
  278. case SI_USER:
  279. case SI_TKILL:
  280. case SI_KERNEL:
  281. /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
  282. * These are the only unspoofable si_code values.
  283. */
  284. tinfo->_sifields._kill._pid = info->si_pid;
  285. tinfo->_sifields._kill._uid = info->si_uid;
  286. si_type = QEMU_SI_KILL;
  287. break;
  288. default:
  289. /* Everything else is spoofable. Make best guess based on signal */
  290. switch (sig) {
  291. case TARGET_SIGCHLD:
  292. tinfo->_sifields._sigchld._pid = info->si_pid;
  293. tinfo->_sifields._sigchld._uid = info->si_uid;
  294. tinfo->_sifields._sigchld._status
  295. = host_to_target_waitstatus(info->si_status);
  296. tinfo->_sifields._sigchld._utime = info->si_utime;
  297. tinfo->_sifields._sigchld._stime = info->si_stime;
  298. si_type = QEMU_SI_CHLD;
  299. break;
  300. case TARGET_SIGIO:
  301. tinfo->_sifields._sigpoll._band = info->si_band;
  302. tinfo->_sifields._sigpoll._fd = info->si_fd;
  303. si_type = QEMU_SI_POLL;
  304. break;
  305. default:
  306. /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
  307. tinfo->_sifields._rt._pid = info->si_pid;
  308. tinfo->_sifields._rt._uid = info->si_uid;
  309. /* XXX: potential problem if 64 bit */
  310. tinfo->_sifields._rt._sigval.sival_ptr
  311. = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
  312. si_type = QEMU_SI_RT;
  313. break;
  314. }
  315. break;
  316. }
  317. tinfo->si_code = deposit32(si_code, 16, 16, si_type);
  318. }
  319. void tswap_siginfo(target_siginfo_t *tinfo,
  320. const target_siginfo_t *info)
  321. {
  322. int si_type = extract32(info->si_code, 16, 16);
  323. int si_code = sextract32(info->si_code, 0, 16);
  324. __put_user(info->si_signo, &tinfo->si_signo);
  325. __put_user(info->si_errno, &tinfo->si_errno);
  326. __put_user(si_code, &tinfo->si_code);
  327. /* We can use our internal marker of which fields in the structure
  328. * are valid, rather than duplicating the guesswork of
  329. * host_to_target_siginfo_noswap() here.
  330. */
  331. switch (si_type) {
  332. case QEMU_SI_KILL:
  333. __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
  334. __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
  335. break;
  336. case QEMU_SI_TIMER:
  337. __put_user(info->_sifields._timer._timer1,
  338. &tinfo->_sifields._timer._timer1);
  339. __put_user(info->_sifields._timer._timer2,
  340. &tinfo->_sifields._timer._timer2);
  341. break;
  342. case QEMU_SI_POLL:
  343. __put_user(info->_sifields._sigpoll._band,
  344. &tinfo->_sifields._sigpoll._band);
  345. __put_user(info->_sifields._sigpoll._fd,
  346. &tinfo->_sifields._sigpoll._fd);
  347. break;
  348. case QEMU_SI_FAULT:
  349. __put_user(info->_sifields._sigfault._addr,
  350. &tinfo->_sifields._sigfault._addr);
  351. break;
  352. case QEMU_SI_CHLD:
  353. __put_user(info->_sifields._sigchld._pid,
  354. &tinfo->_sifields._sigchld._pid);
  355. __put_user(info->_sifields._sigchld._uid,
  356. &tinfo->_sifields._sigchld._uid);
  357. __put_user(info->_sifields._sigchld._status,
  358. &tinfo->_sifields._sigchld._status);
  359. __put_user(info->_sifields._sigchld._utime,
  360. &tinfo->_sifields._sigchld._utime);
  361. __put_user(info->_sifields._sigchld._stime,
  362. &tinfo->_sifields._sigchld._stime);
  363. break;
  364. case QEMU_SI_RT:
  365. __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
  366. __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
  367. __put_user(info->_sifields._rt._sigval.sival_ptr,
  368. &tinfo->_sifields._rt._sigval.sival_ptr);
  369. break;
  370. default:
  371. g_assert_not_reached();
  372. }
  373. }
  374. void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
  375. {
  376. target_siginfo_t tgt_tmp;
  377. host_to_target_siginfo_noswap(&tgt_tmp, info);
  378. tswap_siginfo(tinfo, &tgt_tmp);
  379. }
  380. /* XXX: we support only POSIX RT signals are used. */
  381. /* XXX: find a solution for 64 bit (additional malloced data is needed) */
  382. void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
  383. {
  384. /* This conversion is used only for the rt_sigqueueinfo syscall,
  385. * and so we know that the _rt fields are the valid ones.
  386. */
  387. abi_ulong sival_ptr;
  388. __get_user(info->si_signo, &tinfo->si_signo);
  389. __get_user(info->si_errno, &tinfo->si_errno);
  390. __get_user(info->si_code, &tinfo->si_code);
  391. __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
  392. __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
  393. __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
  394. info->si_value.sival_ptr = (void *)(long)sival_ptr;
  395. }
  396. static int fatal_signal (int sig)
  397. {
  398. switch (sig) {
  399. case TARGET_SIGCHLD:
  400. case TARGET_SIGURG:
  401. case TARGET_SIGWINCH:
  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. /* returns 1 if given signal should dump core if not handled */
  416. static int core_dump_signal(int sig)
  417. {
  418. switch (sig) {
  419. case TARGET_SIGABRT:
  420. case TARGET_SIGFPE:
  421. case TARGET_SIGILL:
  422. case TARGET_SIGQUIT:
  423. case TARGET_SIGSEGV:
  424. case TARGET_SIGTRAP:
  425. case TARGET_SIGBUS:
  426. return (1);
  427. default:
  428. return (0);
  429. }
  430. }
  431. void signal_init(void)
  432. {
  433. TaskState *ts = (TaskState *)thread_cpu->opaque;
  434. struct sigaction act;
  435. struct sigaction oact;
  436. int i, j;
  437. int host_sig;
  438. /* generate signal conversion tables */
  439. for(i = 1; i < _NSIG; i++) {
  440. if (host_to_target_signal_table[i] == 0)
  441. host_to_target_signal_table[i] = i;
  442. }
  443. for(i = 1; i < _NSIG; i++) {
  444. j = host_to_target_signal_table[i];
  445. target_to_host_signal_table[j] = i;
  446. }
  447. /* Set the signal mask from the host mask. */
  448. sigprocmask(0, 0, &ts->signal_mask);
  449. /* set all host signal handlers. ALL signals are blocked during
  450. the handlers to serialize them. */
  451. memset(sigact_table, 0, sizeof(sigact_table));
  452. sigfillset(&act.sa_mask);
  453. act.sa_flags = SA_SIGINFO;
  454. act.sa_sigaction = host_signal_handler;
  455. for(i = 1; i <= TARGET_NSIG; i++) {
  456. #ifdef TARGET_GPROF
  457. if (i == SIGPROF) {
  458. continue;
  459. }
  460. #endif
  461. host_sig = target_to_host_signal(i);
  462. sigaction(host_sig, NULL, &oact);
  463. if (oact.sa_sigaction == (void *)SIG_IGN) {
  464. sigact_table[i - 1]._sa_handler = TARGET_SIG_IGN;
  465. } else if (oact.sa_sigaction == (void *)SIG_DFL) {
  466. sigact_table[i - 1]._sa_handler = TARGET_SIG_DFL;
  467. }
  468. /* If there's already a handler installed then something has
  469. gone horribly wrong, so don't even try to handle that case. */
  470. /* Install some handlers for our own use. We need at least
  471. SIGSEGV and SIGBUS, to detect exceptions. We can not just
  472. trap all signals because it affects syscall interrupt
  473. behavior. But do trap all default-fatal signals. */
  474. if (fatal_signal (i))
  475. sigaction(host_sig, &act, NULL);
  476. }
  477. }
  478. /* Force a synchronously taken signal. The kernel force_sig() function
  479. * also forces the signal to "not blocked, not ignored", but for QEMU
  480. * that work is done in process_pending_signals().
  481. */
  482. void force_sig(int sig)
  483. {
  484. CPUState *cpu = thread_cpu;
  485. CPUArchState *env = cpu->env_ptr;
  486. target_siginfo_t info;
  487. info.si_signo = sig;
  488. info.si_errno = 0;
  489. info.si_code = TARGET_SI_KERNEL;
  490. info._sifields._kill._pid = 0;
  491. info._sifields._kill._uid = 0;
  492. queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
  493. }
  494. /* Force a SIGSEGV if we couldn't write to memory trying to set
  495. * up the signal frame. oldsig is the signal we were trying to handle
  496. * at the point of failure.
  497. */
  498. #if !defined(TARGET_RISCV)
  499. void force_sigsegv(int oldsig)
  500. {
  501. if (oldsig == SIGSEGV) {
  502. /* Make sure we don't try to deliver the signal again; this will
  503. * end up with handle_pending_signal() calling dump_core_and_abort().
  504. */
  505. sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
  506. }
  507. force_sig(TARGET_SIGSEGV);
  508. }
  509. #endif
  510. /* abort execution with signal */
  511. static void QEMU_NORETURN dump_core_and_abort(int target_sig)
  512. {
  513. CPUState *cpu = thread_cpu;
  514. CPUArchState *env = cpu->env_ptr;
  515. TaskState *ts = (TaskState *)cpu->opaque;
  516. int host_sig, core_dumped = 0;
  517. struct sigaction act;
  518. host_sig = target_to_host_signal(target_sig);
  519. trace_user_force_sig(env, target_sig, host_sig);
  520. gdb_signalled(env, target_sig);
  521. /* dump core if supported by target binary format */
  522. if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
  523. stop_all_tasks();
  524. core_dumped =
  525. ((*ts->bprm->core_dump)(target_sig, env) == 0);
  526. }
  527. if (core_dumped) {
  528. /* we already dumped the core of target process, we don't want
  529. * a coredump of qemu itself */
  530. struct rlimit nodump;
  531. getrlimit(RLIMIT_CORE, &nodump);
  532. nodump.rlim_cur=0;
  533. setrlimit(RLIMIT_CORE, &nodump);
  534. (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
  535. target_sig, strsignal(host_sig), "core dumped" );
  536. }
  537. /* The proper exit code for dying from an uncaught signal is
  538. * -<signal>. The kernel doesn't allow exit() or _exit() to pass
  539. * a negative value. To get the proper exit code we need to
  540. * actually die from an uncaught signal. Here the default signal
  541. * handler is installed, we send ourself a signal and we wait for
  542. * it to arrive. */
  543. sigfillset(&act.sa_mask);
  544. act.sa_handler = SIG_DFL;
  545. act.sa_flags = 0;
  546. sigaction(host_sig, &act, NULL);
  547. /* For some reason raise(host_sig) doesn't send the signal when
  548. * statically linked on x86-64. */
  549. kill(getpid(), host_sig);
  550. /* Make sure the signal isn't masked (just reuse the mask inside
  551. of act) */
  552. sigdelset(&act.sa_mask, host_sig);
  553. sigsuspend(&act.sa_mask);
  554. /* unreachable */
  555. abort();
  556. }
  557. /* queue a signal so that it will be send to the virtual CPU as soon
  558. as possible */
  559. int queue_signal(CPUArchState *env, int sig, int si_type,
  560. target_siginfo_t *info)
  561. {
  562. CPUState *cpu = env_cpu(env);
  563. TaskState *ts = cpu->opaque;
  564. trace_user_queue_signal(env, sig);
  565. info->si_code = deposit32(info->si_code, 16, 16, si_type);
  566. ts->sync_signal.info = *info;
  567. ts->sync_signal.pending = sig;
  568. /* signal that a new signal is pending */
  569. atomic_set(&ts->signal_pending, 1);
  570. return 1; /* indicates that the signal was queued */
  571. }
  572. #ifndef HAVE_SAFE_SYSCALL
  573. static inline void rewind_if_in_safe_syscall(void *puc)
  574. {
  575. /* Default version: never rewind */
  576. }
  577. #endif
  578. static void host_signal_handler(int host_signum, siginfo_t *info,
  579. void *puc)
  580. {
  581. CPUArchState *env = thread_cpu->env_ptr;
  582. CPUState *cpu = env_cpu(env);
  583. TaskState *ts = cpu->opaque;
  584. int sig;
  585. target_siginfo_t tinfo;
  586. ucontext_t *uc = puc;
  587. struct emulated_sigtable *k;
  588. /* the CPU emulator uses some host signals to detect exceptions,
  589. we forward to it some signals */
  590. if ((host_signum == SIGSEGV || host_signum == SIGBUS)
  591. && info->si_code > 0) {
  592. if (cpu_signal_handler(host_signum, info, puc))
  593. return;
  594. }
  595. /* get target signal number */
  596. sig = host_to_target_signal(host_signum);
  597. if (sig < 1 || sig > TARGET_NSIG)
  598. return;
  599. trace_user_host_signal(env, host_signum, sig);
  600. rewind_if_in_safe_syscall(puc);
  601. host_to_target_siginfo_noswap(&tinfo, info);
  602. k = &ts->sigtab[sig - 1];
  603. k->info = tinfo;
  604. k->pending = sig;
  605. ts->signal_pending = 1;
  606. /* Block host signals until target signal handler entered. We
  607. * can't block SIGSEGV or SIGBUS while we're executing guest
  608. * code in case the guest code provokes one in the window between
  609. * now and it getting out to the main loop. Signals will be
  610. * unblocked again in process_pending_signals().
  611. *
  612. * WARNING: we cannot use sigfillset() here because the uc_sigmask
  613. * field is a kernel sigset_t, which is much smaller than the
  614. * libc sigset_t which sigfillset() operates on. Using sigfillset()
  615. * would write 0xff bytes off the end of the structure and trash
  616. * data on the struct.
  617. * We can't use sizeof(uc->uc_sigmask) either, because the libc
  618. * headers define the struct field with the wrong (too large) type.
  619. */
  620. memset(&uc->uc_sigmask, 0xff, SIGSET_T_SIZE);
  621. sigdelset(&uc->uc_sigmask, SIGSEGV);
  622. sigdelset(&uc->uc_sigmask, SIGBUS);
  623. /* interrupt the virtual CPU as soon as possible */
  624. cpu_exit(thread_cpu);
  625. }
  626. /* do_sigaltstack() returns target values and errnos. */
  627. /* compare linux/kernel/signal.c:do_sigaltstack() */
  628. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp)
  629. {
  630. int ret;
  631. struct target_sigaltstack oss;
  632. TaskState *ts = (TaskState *)thread_cpu->opaque;
  633. /* XXX: test errors */
  634. if(uoss_addr)
  635. {
  636. __put_user(ts->sigaltstack_used.ss_sp, &oss.ss_sp);
  637. __put_user(ts->sigaltstack_used.ss_size, &oss.ss_size);
  638. __put_user(sas_ss_flags(sp), &oss.ss_flags);
  639. }
  640. if(uss_addr)
  641. {
  642. struct target_sigaltstack *uss;
  643. struct target_sigaltstack ss;
  644. size_t minstacksize = TARGET_MINSIGSTKSZ;
  645. #if defined(TARGET_PPC64)
  646. /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
  647. struct image_info *image = ((TaskState *)thread_cpu->opaque)->info;
  648. if (get_ppc64_abi(image) > 1) {
  649. minstacksize = 4096;
  650. }
  651. #endif
  652. ret = -TARGET_EFAULT;
  653. if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
  654. goto out;
  655. }
  656. __get_user(ss.ss_sp, &uss->ss_sp);
  657. __get_user(ss.ss_size, &uss->ss_size);
  658. __get_user(ss.ss_flags, &uss->ss_flags);
  659. unlock_user_struct(uss, uss_addr, 0);
  660. ret = -TARGET_EPERM;
  661. if (on_sig_stack(sp))
  662. goto out;
  663. ret = -TARGET_EINVAL;
  664. if (ss.ss_flags != TARGET_SS_DISABLE
  665. && ss.ss_flags != TARGET_SS_ONSTACK
  666. && ss.ss_flags != 0)
  667. goto out;
  668. if (ss.ss_flags == TARGET_SS_DISABLE) {
  669. ss.ss_size = 0;
  670. ss.ss_sp = 0;
  671. } else {
  672. ret = -TARGET_ENOMEM;
  673. if (ss.ss_size < minstacksize) {
  674. goto out;
  675. }
  676. }
  677. ts->sigaltstack_used.ss_sp = ss.ss_sp;
  678. ts->sigaltstack_used.ss_size = ss.ss_size;
  679. }
  680. if (uoss_addr) {
  681. ret = -TARGET_EFAULT;
  682. if (copy_to_user(uoss_addr, &oss, sizeof(oss)))
  683. goto out;
  684. }
  685. ret = 0;
  686. out:
  687. return ret;
  688. }
  689. /* do_sigaction() return target values and host errnos */
  690. int do_sigaction(int sig, const struct target_sigaction *act,
  691. struct target_sigaction *oact)
  692. {
  693. struct target_sigaction *k;
  694. struct sigaction act1;
  695. int host_sig;
  696. int ret = 0;
  697. if (sig < 1 || sig > TARGET_NSIG || sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP) {
  698. return -TARGET_EINVAL;
  699. }
  700. if (block_signals()) {
  701. return -TARGET_ERESTARTSYS;
  702. }
  703. k = &sigact_table[sig - 1];
  704. if (oact) {
  705. __put_user(k->_sa_handler, &oact->_sa_handler);
  706. __put_user(k->sa_flags, &oact->sa_flags);
  707. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  708. __put_user(k->sa_restorer, &oact->sa_restorer);
  709. #endif
  710. /* Not swapped. */
  711. oact->sa_mask = k->sa_mask;
  712. }
  713. if (act) {
  714. /* FIXME: This is not threadsafe. */
  715. __get_user(k->_sa_handler, &act->_sa_handler);
  716. __get_user(k->sa_flags, &act->sa_flags);
  717. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  718. __get_user(k->sa_restorer, &act->sa_restorer);
  719. #endif
  720. /* To be swapped in target_to_host_sigset. */
  721. k->sa_mask = act->sa_mask;
  722. /* we update the host linux signal state */
  723. host_sig = target_to_host_signal(sig);
  724. if (host_sig != SIGSEGV && host_sig != SIGBUS) {
  725. sigfillset(&act1.sa_mask);
  726. act1.sa_flags = SA_SIGINFO;
  727. if (k->sa_flags & TARGET_SA_RESTART)
  728. act1.sa_flags |= SA_RESTART;
  729. /* NOTE: it is important to update the host kernel signal
  730. ignore state to avoid getting unexpected interrupted
  731. syscalls */
  732. if (k->_sa_handler == TARGET_SIG_IGN) {
  733. act1.sa_sigaction = (void *)SIG_IGN;
  734. } else if (k->_sa_handler == TARGET_SIG_DFL) {
  735. if (fatal_signal (sig))
  736. act1.sa_sigaction = host_signal_handler;
  737. else
  738. act1.sa_sigaction = (void *)SIG_DFL;
  739. } else {
  740. act1.sa_sigaction = host_signal_handler;
  741. }
  742. ret = sigaction(host_sig, &act1, NULL);
  743. }
  744. }
  745. return ret;
  746. }
  747. static void handle_pending_signal(CPUArchState *cpu_env, int sig,
  748. struct emulated_sigtable *k)
  749. {
  750. CPUState *cpu = env_cpu(cpu_env);
  751. abi_ulong handler;
  752. sigset_t set;
  753. target_sigset_t target_old_set;
  754. struct target_sigaction *sa;
  755. TaskState *ts = cpu->opaque;
  756. trace_user_handle_signal(cpu_env, sig);
  757. /* dequeue signal */
  758. k->pending = 0;
  759. sig = gdb_handlesig(cpu, sig);
  760. if (!sig) {
  761. sa = NULL;
  762. handler = TARGET_SIG_IGN;
  763. } else {
  764. sa = &sigact_table[sig - 1];
  765. handler = sa->_sa_handler;
  766. }
  767. if (do_strace) {
  768. print_taken_signal(sig, &k->info);
  769. }
  770. if (handler == TARGET_SIG_DFL) {
  771. /* default handler : ignore some signal. The other are job control or fatal */
  772. if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
  773. kill(getpid(),SIGSTOP);
  774. } else if (sig != TARGET_SIGCHLD &&
  775. sig != TARGET_SIGURG &&
  776. sig != TARGET_SIGWINCH &&
  777. sig != TARGET_SIGCONT) {
  778. dump_core_and_abort(sig);
  779. }
  780. } else if (handler == TARGET_SIG_IGN) {
  781. /* ignore sig */
  782. } else if (handler == TARGET_SIG_ERR) {
  783. dump_core_and_abort(sig);
  784. } else {
  785. /* compute the blocked signals during the handler execution */
  786. sigset_t *blocked_set;
  787. target_to_host_sigset(&set, &sa->sa_mask);
  788. /* SA_NODEFER indicates that the current signal should not be
  789. blocked during the handler */
  790. if (!(sa->sa_flags & TARGET_SA_NODEFER))
  791. sigaddset(&set, target_to_host_signal(sig));
  792. /* save the previous blocked signal state to restore it at the
  793. end of the signal execution (see do_sigreturn) */
  794. host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
  795. /* block signals in the handler */
  796. blocked_set = ts->in_sigsuspend ?
  797. &ts->sigsuspend_mask : &ts->signal_mask;
  798. sigorset(&ts->signal_mask, blocked_set, &set);
  799. ts->in_sigsuspend = 0;
  800. /* if the CPU is in VM86 mode, we restore the 32 bit values */
  801. #if defined(TARGET_I386) && !defined(TARGET_X86_64)
  802. {
  803. CPUX86State *env = cpu_env;
  804. if (env->eflags & VM_MASK)
  805. save_v86_state(env);
  806. }
  807. #endif
  808. /* prepare the stack frame of the virtual CPU */
  809. #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
  810. if (sa->sa_flags & TARGET_SA_SIGINFO) {
  811. setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
  812. } else {
  813. setup_frame(sig, sa, &target_old_set, cpu_env);
  814. }
  815. #else
  816. /* These targets do not have traditional signals. */
  817. setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
  818. #endif
  819. if (sa->sa_flags & TARGET_SA_RESETHAND) {
  820. sa->_sa_handler = TARGET_SIG_DFL;
  821. }
  822. }
  823. }
  824. void process_pending_signals(CPUArchState *cpu_env)
  825. {
  826. CPUState *cpu = env_cpu(cpu_env);
  827. int sig;
  828. TaskState *ts = cpu->opaque;
  829. sigset_t set;
  830. sigset_t *blocked_set;
  831. while (atomic_read(&ts->signal_pending)) {
  832. /* FIXME: This is not threadsafe. */
  833. sigfillset(&set);
  834. sigprocmask(SIG_SETMASK, &set, 0);
  835. restart_scan:
  836. sig = ts->sync_signal.pending;
  837. if (sig) {
  838. /* Synchronous signals are forced,
  839. * see force_sig_info() and callers in Linux
  840. * Note that not all of our queue_signal() calls in QEMU correspond
  841. * to force_sig_info() calls in Linux (some are send_sig_info()).
  842. * However it seems like a kernel bug to me to allow the process
  843. * to block a synchronous signal since it could then just end up
  844. * looping round and round indefinitely.
  845. */
  846. if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
  847. || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
  848. sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
  849. sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
  850. }
  851. handle_pending_signal(cpu_env, sig, &ts->sync_signal);
  852. }
  853. for (sig = 1; sig <= TARGET_NSIG; sig++) {
  854. blocked_set = ts->in_sigsuspend ?
  855. &ts->sigsuspend_mask : &ts->signal_mask;
  856. if (ts->sigtab[sig - 1].pending &&
  857. (!sigismember(blocked_set,
  858. target_to_host_signal_table[sig]))) {
  859. handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
  860. /* Restart scan from the beginning, as handle_pending_signal
  861. * might have resulted in a new synchronous signal (eg SIGSEGV).
  862. */
  863. goto restart_scan;
  864. }
  865. }
  866. /* if no signal is pending, unblock signals and recheck (the act
  867. * of unblocking might cause us to take another host signal which
  868. * will set signal_pending again).
  869. */
  870. atomic_set(&ts->signal_pending, 0);
  871. ts->in_sigsuspend = 0;
  872. set = ts->signal_mask;
  873. sigdelset(&set, SIGSEGV);
  874. sigdelset(&set, SIGBUS);
  875. sigprocmask(SIG_SETMASK, &set, 0);
  876. }
  877. ts->in_sigsuspend = 0;
  878. }