signal.c 34 KB

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