2
0

signal.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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 "qemu/cutils.h"
  22. #include "gdbstub/user.h"
  23. #include "exec/page-protection.h"
  24. #include "hw/core/tcg-cpu-ops.h"
  25. #include <sys/ucontext.h>
  26. #include <sys/resource.h>
  27. #include "qemu.h"
  28. #include "user-internals.h"
  29. #include "strace.h"
  30. #include "loader.h"
  31. #include "trace.h"
  32. #include "signal-common.h"
  33. #include "host-signal.h"
  34. #include "user/cpu_loop.h"
  35. #include "user/page-protection.h"
  36. #include "user/safe-syscall.h"
  37. #include "tcg/tcg.h"
  38. /* target_siginfo_t must fit in gdbstub's siginfo save area. */
  39. QEMU_BUILD_BUG_ON(sizeof(target_siginfo_t) > MAX_SIGINFO_LENGTH);
  40. static struct target_sigaction sigact_table[TARGET_NSIG];
  41. static void host_signal_handler(int host_signum, siginfo_t *info,
  42. void *puc);
  43. /* Fallback addresses into sigtramp page. */
  44. abi_ulong default_sigreturn;
  45. abi_ulong default_rt_sigreturn;
  46. /*
  47. * System includes define _NSIG as SIGRTMAX + 1, but qemu (like the kernel)
  48. * defines TARGET_NSIG as TARGET_SIGRTMAX and the first signal is 1.
  49. * Signal number 0 is reserved for use as kill(pid, 0), to test whether
  50. * a process exists without sending it a signal.
  51. */
  52. #ifdef __SIGRTMAX
  53. QEMU_BUILD_BUG_ON(__SIGRTMAX + 1 != _NSIG);
  54. #endif
  55. static uint8_t host_to_target_signal_table[_NSIG] = {
  56. #define MAKE_SIG_ENTRY(sig) [sig] = TARGET_##sig,
  57. MAKE_SIGNAL_LIST
  58. #undef MAKE_SIG_ENTRY
  59. };
  60. static uint8_t target_to_host_signal_table[TARGET_NSIG + 1];
  61. /* valid sig is between 1 and _NSIG - 1 */
  62. int host_to_target_signal(int sig)
  63. {
  64. if (sig < 1) {
  65. return sig;
  66. }
  67. if (sig >= _NSIG) {
  68. return TARGET_NSIG + 1;
  69. }
  70. return host_to_target_signal_table[sig];
  71. }
  72. /* valid sig is between 1 and TARGET_NSIG */
  73. int target_to_host_signal(int sig)
  74. {
  75. if (sig < 1) {
  76. return sig;
  77. }
  78. if (sig > TARGET_NSIG) {
  79. return _NSIG;
  80. }
  81. return target_to_host_signal_table[sig];
  82. }
  83. static inline void target_sigaddset(target_sigset_t *set, int signum)
  84. {
  85. signum--;
  86. abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
  87. set->sig[signum / TARGET_NSIG_BPW] |= mask;
  88. }
  89. static inline int target_sigismember(const target_sigset_t *set, int signum)
  90. {
  91. signum--;
  92. abi_ulong mask = (abi_ulong)1 << (signum % TARGET_NSIG_BPW);
  93. return ((set->sig[signum / TARGET_NSIG_BPW] & mask) != 0);
  94. }
  95. void host_to_target_sigset_internal(target_sigset_t *d,
  96. const sigset_t *s)
  97. {
  98. int host_sig, target_sig;
  99. target_sigemptyset(d);
  100. for (host_sig = 1; host_sig < _NSIG; host_sig++) {
  101. target_sig = host_to_target_signal(host_sig);
  102. if (target_sig < 1 || target_sig > TARGET_NSIG) {
  103. continue;
  104. }
  105. if (sigismember(s, host_sig)) {
  106. target_sigaddset(d, target_sig);
  107. }
  108. }
  109. }
  110. void host_to_target_sigset(target_sigset_t *d, const sigset_t *s)
  111. {
  112. target_sigset_t d1;
  113. int i;
  114. host_to_target_sigset_internal(&d1, s);
  115. for(i = 0;i < TARGET_NSIG_WORDS; i++)
  116. d->sig[i] = tswapal(d1.sig[i]);
  117. }
  118. void target_to_host_sigset_internal(sigset_t *d,
  119. const target_sigset_t *s)
  120. {
  121. int host_sig, target_sig;
  122. sigemptyset(d);
  123. for (target_sig = 1; target_sig <= TARGET_NSIG; target_sig++) {
  124. host_sig = target_to_host_signal(target_sig);
  125. if (host_sig < 1 || host_sig >= _NSIG) {
  126. continue;
  127. }
  128. if (target_sigismember(s, target_sig)) {
  129. sigaddset(d, host_sig);
  130. }
  131. }
  132. }
  133. void target_to_host_sigset(sigset_t *d, const target_sigset_t *s)
  134. {
  135. target_sigset_t s1;
  136. int i;
  137. for(i = 0;i < TARGET_NSIG_WORDS; i++)
  138. s1.sig[i] = tswapal(s->sig[i]);
  139. target_to_host_sigset_internal(d, &s1);
  140. }
  141. void host_to_target_old_sigset(abi_ulong *old_sigset,
  142. const sigset_t *sigset)
  143. {
  144. target_sigset_t d;
  145. host_to_target_sigset(&d, sigset);
  146. *old_sigset = d.sig[0];
  147. }
  148. void target_to_host_old_sigset(sigset_t *sigset,
  149. const abi_ulong *old_sigset)
  150. {
  151. target_sigset_t d;
  152. int i;
  153. d.sig[0] = *old_sigset;
  154. for(i = 1;i < TARGET_NSIG_WORDS; i++)
  155. d.sig[i] = 0;
  156. target_to_host_sigset(sigset, &d);
  157. }
  158. int block_signals(void)
  159. {
  160. TaskState *ts = get_task_state(thread_cpu);
  161. sigset_t set;
  162. /* It's OK to block everything including SIGSEGV, because we won't
  163. * run any further guest code before unblocking signals in
  164. * process_pending_signals().
  165. */
  166. sigfillset(&set);
  167. sigprocmask(SIG_SETMASK, &set, 0);
  168. return qatomic_xchg(&ts->signal_pending, 1);
  169. }
  170. /* Wrapper for sigprocmask function
  171. * Emulates a sigprocmask in a safe way for the guest. Note that set and oldset
  172. * are host signal set, not guest ones. Returns -QEMU_ERESTARTSYS if
  173. * a signal was already pending and the syscall must be restarted, or
  174. * 0 on success.
  175. * If set is NULL, this is guaranteed not to fail.
  176. */
  177. int do_sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
  178. {
  179. TaskState *ts = get_task_state(thread_cpu);
  180. if (oldset) {
  181. *oldset = ts->signal_mask;
  182. }
  183. if (set) {
  184. int i;
  185. if (block_signals()) {
  186. return -QEMU_ERESTARTSYS;
  187. }
  188. switch (how) {
  189. case SIG_BLOCK:
  190. sigorset(&ts->signal_mask, &ts->signal_mask, set);
  191. break;
  192. case SIG_UNBLOCK:
  193. for (i = 1; i <= NSIG; ++i) {
  194. if (sigismember(set, i)) {
  195. sigdelset(&ts->signal_mask, i);
  196. }
  197. }
  198. break;
  199. case SIG_SETMASK:
  200. ts->signal_mask = *set;
  201. break;
  202. default:
  203. g_assert_not_reached();
  204. }
  205. /* Silently ignore attempts to change blocking status of KILL or STOP */
  206. sigdelset(&ts->signal_mask, SIGKILL);
  207. sigdelset(&ts->signal_mask, SIGSTOP);
  208. }
  209. return 0;
  210. }
  211. /* Just set the guest's signal mask to the specified value; the
  212. * caller is assumed to have called block_signals() already.
  213. */
  214. void set_sigmask(const sigset_t *set)
  215. {
  216. TaskState *ts = get_task_state(thread_cpu);
  217. ts->signal_mask = *set;
  218. }
  219. /* sigaltstack management */
  220. int on_sig_stack(unsigned long sp)
  221. {
  222. TaskState *ts = get_task_state(thread_cpu);
  223. return (sp - ts->sigaltstack_used.ss_sp
  224. < ts->sigaltstack_used.ss_size);
  225. }
  226. int sas_ss_flags(unsigned long sp)
  227. {
  228. TaskState *ts = get_task_state(thread_cpu);
  229. return (ts->sigaltstack_used.ss_size == 0 ? SS_DISABLE
  230. : on_sig_stack(sp) ? SS_ONSTACK : 0);
  231. }
  232. abi_ulong target_sigsp(abi_ulong sp, struct target_sigaction *ka)
  233. {
  234. /*
  235. * This is the X/Open sanctioned signal stack switching.
  236. */
  237. TaskState *ts = get_task_state(thread_cpu);
  238. if ((ka->sa_flags & TARGET_SA_ONSTACK) && !sas_ss_flags(sp)) {
  239. return ts->sigaltstack_used.ss_sp + ts->sigaltstack_used.ss_size;
  240. }
  241. return sp;
  242. }
  243. void target_save_altstack(target_stack_t *uss, CPUArchState *env)
  244. {
  245. TaskState *ts = get_task_state(thread_cpu);
  246. __put_user(ts->sigaltstack_used.ss_sp, &uss->ss_sp);
  247. __put_user(sas_ss_flags(get_sp_from_cpustate(env)), &uss->ss_flags);
  248. __put_user(ts->sigaltstack_used.ss_size, &uss->ss_size);
  249. }
  250. abi_long target_restore_altstack(target_stack_t *uss, CPUArchState *env)
  251. {
  252. TaskState *ts = get_task_state(thread_cpu);
  253. size_t minstacksize = TARGET_MINSIGSTKSZ;
  254. target_stack_t ss;
  255. #if defined(TARGET_PPC64)
  256. /* ELF V2 for PPC64 has a 4K minimum stack size for signal handlers */
  257. struct image_info *image = ts->info;
  258. if (get_ppc64_abi(image) > 1) {
  259. minstacksize = 4096;
  260. }
  261. #endif
  262. __get_user(ss.ss_sp, &uss->ss_sp);
  263. __get_user(ss.ss_size, &uss->ss_size);
  264. __get_user(ss.ss_flags, &uss->ss_flags);
  265. if (on_sig_stack(get_sp_from_cpustate(env))) {
  266. return -TARGET_EPERM;
  267. }
  268. switch (ss.ss_flags) {
  269. default:
  270. return -TARGET_EINVAL;
  271. case TARGET_SS_DISABLE:
  272. ss.ss_size = 0;
  273. ss.ss_sp = 0;
  274. break;
  275. case TARGET_SS_ONSTACK:
  276. case 0:
  277. if (ss.ss_size < minstacksize) {
  278. return -TARGET_ENOMEM;
  279. }
  280. break;
  281. }
  282. ts->sigaltstack_used.ss_sp = ss.ss_sp;
  283. ts->sigaltstack_used.ss_size = ss.ss_size;
  284. return 0;
  285. }
  286. /* siginfo conversion */
  287. static inline void host_to_target_siginfo_noswap(target_siginfo_t *tinfo,
  288. const siginfo_t *info)
  289. {
  290. int sig = host_to_target_signal(info->si_signo);
  291. int si_code = info->si_code;
  292. int si_type;
  293. tinfo->si_signo = sig;
  294. tinfo->si_errno = 0;
  295. tinfo->si_code = info->si_code;
  296. /* This memset serves two purposes:
  297. * (1) ensure we don't leak random junk to the guest later
  298. * (2) placate false positives from gcc about fields
  299. * being used uninitialized if it chooses to inline both this
  300. * function and tswap_siginfo() into host_to_target_siginfo().
  301. */
  302. memset(tinfo->_sifields._pad, 0, sizeof(tinfo->_sifields._pad));
  303. /* This is awkward, because we have to use a combination of
  304. * the si_code and si_signo to figure out which of the union's
  305. * members are valid. (Within the host kernel it is always possible
  306. * to tell, but the kernel carefully avoids giving userspace the
  307. * high 16 bits of si_code, so we don't have the information to
  308. * do this the easy way...) We therefore make our best guess,
  309. * bearing in mind that a guest can spoof most of the si_codes
  310. * via rt_sigqueueinfo() if it likes.
  311. *
  312. * Once we have made our guess, we record it in the top 16 bits of
  313. * the si_code, so that tswap_siginfo() later can use it.
  314. * tswap_siginfo() will strip these top bits out before writing
  315. * si_code to the guest (sign-extending the lower bits).
  316. */
  317. switch (si_code) {
  318. case SI_USER:
  319. case SI_TKILL:
  320. case SI_KERNEL:
  321. /* Sent via kill(), tkill() or tgkill(), or direct from the kernel.
  322. * These are the only unspoofable si_code values.
  323. */
  324. tinfo->_sifields._kill._pid = info->si_pid;
  325. tinfo->_sifields._kill._uid = info->si_uid;
  326. si_type = QEMU_SI_KILL;
  327. break;
  328. default:
  329. /* Everything else is spoofable. Make best guess based on signal */
  330. switch (sig) {
  331. case TARGET_SIGCHLD:
  332. tinfo->_sifields._sigchld._pid = info->si_pid;
  333. tinfo->_sifields._sigchld._uid = info->si_uid;
  334. if (si_code == CLD_EXITED)
  335. tinfo->_sifields._sigchld._status = info->si_status;
  336. else
  337. tinfo->_sifields._sigchld._status
  338. = host_to_target_signal(info->si_status & 0x7f)
  339. | (info->si_status & ~0x7f);
  340. tinfo->_sifields._sigchld._utime = info->si_utime;
  341. tinfo->_sifields._sigchld._stime = info->si_stime;
  342. si_type = QEMU_SI_CHLD;
  343. break;
  344. case TARGET_SIGIO:
  345. tinfo->_sifields._sigpoll._band = info->si_band;
  346. tinfo->_sifields._sigpoll._fd = info->si_fd;
  347. si_type = QEMU_SI_POLL;
  348. break;
  349. default:
  350. /* Assume a sigqueue()/mq_notify()/rt_sigqueueinfo() source. */
  351. tinfo->_sifields._rt._pid = info->si_pid;
  352. tinfo->_sifields._rt._uid = info->si_uid;
  353. /* XXX: potential problem if 64 bit */
  354. tinfo->_sifields._rt._sigval.sival_ptr
  355. = (abi_ulong)(unsigned long)info->si_value.sival_ptr;
  356. si_type = QEMU_SI_RT;
  357. break;
  358. }
  359. break;
  360. }
  361. tinfo->si_code = deposit32(si_code, 16, 16, si_type);
  362. }
  363. static void tswap_siginfo(target_siginfo_t *tinfo,
  364. const target_siginfo_t *info)
  365. {
  366. int si_type = extract32(info->si_code, 16, 16);
  367. int si_code = sextract32(info->si_code, 0, 16);
  368. __put_user(info->si_signo, &tinfo->si_signo);
  369. __put_user(info->si_errno, &tinfo->si_errno);
  370. __put_user(si_code, &tinfo->si_code);
  371. /* We can use our internal marker of which fields in the structure
  372. * are valid, rather than duplicating the guesswork of
  373. * host_to_target_siginfo_noswap() here.
  374. */
  375. switch (si_type) {
  376. case QEMU_SI_KILL:
  377. __put_user(info->_sifields._kill._pid, &tinfo->_sifields._kill._pid);
  378. __put_user(info->_sifields._kill._uid, &tinfo->_sifields._kill._uid);
  379. break;
  380. case QEMU_SI_TIMER:
  381. __put_user(info->_sifields._timer._timer1,
  382. &tinfo->_sifields._timer._timer1);
  383. __put_user(info->_sifields._timer._timer2,
  384. &tinfo->_sifields._timer._timer2);
  385. break;
  386. case QEMU_SI_POLL:
  387. __put_user(info->_sifields._sigpoll._band,
  388. &tinfo->_sifields._sigpoll._band);
  389. __put_user(info->_sifields._sigpoll._fd,
  390. &tinfo->_sifields._sigpoll._fd);
  391. break;
  392. case QEMU_SI_FAULT:
  393. __put_user(info->_sifields._sigfault._addr,
  394. &tinfo->_sifields._sigfault._addr);
  395. break;
  396. case QEMU_SI_CHLD:
  397. __put_user(info->_sifields._sigchld._pid,
  398. &tinfo->_sifields._sigchld._pid);
  399. __put_user(info->_sifields._sigchld._uid,
  400. &tinfo->_sifields._sigchld._uid);
  401. __put_user(info->_sifields._sigchld._status,
  402. &tinfo->_sifields._sigchld._status);
  403. __put_user(info->_sifields._sigchld._utime,
  404. &tinfo->_sifields._sigchld._utime);
  405. __put_user(info->_sifields._sigchld._stime,
  406. &tinfo->_sifields._sigchld._stime);
  407. break;
  408. case QEMU_SI_RT:
  409. __put_user(info->_sifields._rt._pid, &tinfo->_sifields._rt._pid);
  410. __put_user(info->_sifields._rt._uid, &tinfo->_sifields._rt._uid);
  411. __put_user(info->_sifields._rt._sigval.sival_ptr,
  412. &tinfo->_sifields._rt._sigval.sival_ptr);
  413. break;
  414. default:
  415. g_assert_not_reached();
  416. }
  417. }
  418. void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info)
  419. {
  420. target_siginfo_t tgt_tmp;
  421. host_to_target_siginfo_noswap(&tgt_tmp, info);
  422. tswap_siginfo(tinfo, &tgt_tmp);
  423. }
  424. /* XXX: we support only POSIX RT signals are used. */
  425. /* XXX: find a solution for 64 bit (additional malloced data is needed) */
  426. void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo)
  427. {
  428. /* This conversion is used only for the rt_sigqueueinfo syscall,
  429. * and so we know that the _rt fields are the valid ones.
  430. */
  431. abi_ulong sival_ptr;
  432. __get_user(info->si_signo, &tinfo->si_signo);
  433. __get_user(info->si_errno, &tinfo->si_errno);
  434. __get_user(info->si_code, &tinfo->si_code);
  435. __get_user(info->si_pid, &tinfo->_sifields._rt._pid);
  436. __get_user(info->si_uid, &tinfo->_sifields._rt._uid);
  437. __get_user(sival_ptr, &tinfo->_sifields._rt._sigval.sival_ptr);
  438. info->si_value.sival_ptr = (void *)(long)sival_ptr;
  439. }
  440. /* returns 1 if given signal should dump core if not handled */
  441. static int core_dump_signal(int sig)
  442. {
  443. switch (sig) {
  444. case TARGET_SIGABRT:
  445. case TARGET_SIGFPE:
  446. case TARGET_SIGILL:
  447. case TARGET_SIGQUIT:
  448. case TARGET_SIGSEGV:
  449. case TARGET_SIGTRAP:
  450. case TARGET_SIGBUS:
  451. return (1);
  452. default:
  453. return (0);
  454. }
  455. }
  456. static void signal_table_init(const char *rtsig_map)
  457. {
  458. int hsig, tsig, count;
  459. if (rtsig_map) {
  460. /*
  461. * Map host RT signals to target RT signals according to the
  462. * user-provided specification.
  463. */
  464. const char *s = rtsig_map;
  465. while (true) {
  466. int i;
  467. if (qemu_strtoi(s, &s, 10, &tsig) || *s++ != ' ') {
  468. fprintf(stderr, "Malformed target signal in QEMU_RTSIG_MAP\n");
  469. exit(EXIT_FAILURE);
  470. }
  471. if (qemu_strtoi(s, &s, 10, &hsig) || *s++ != ' ') {
  472. fprintf(stderr, "Malformed host signal in QEMU_RTSIG_MAP\n");
  473. exit(EXIT_FAILURE);
  474. }
  475. if (qemu_strtoi(s, &s, 10, &count) || (*s && *s != ',')) {
  476. fprintf(stderr, "Malformed signal count in QEMU_RTSIG_MAP\n");
  477. exit(EXIT_FAILURE);
  478. }
  479. for (i = 0; i < count; i++, tsig++, hsig++) {
  480. if (tsig < TARGET_SIGRTMIN || tsig > TARGET_NSIG) {
  481. fprintf(stderr, "%d is not a target rt signal\n", tsig);
  482. exit(EXIT_FAILURE);
  483. }
  484. if (hsig < SIGRTMIN || hsig > SIGRTMAX) {
  485. fprintf(stderr, "%d is not a host rt signal\n", hsig);
  486. exit(EXIT_FAILURE);
  487. }
  488. if (host_to_target_signal_table[hsig]) {
  489. fprintf(stderr, "%d already maps %d\n",
  490. hsig, host_to_target_signal_table[hsig]);
  491. exit(EXIT_FAILURE);
  492. }
  493. host_to_target_signal_table[hsig] = tsig;
  494. }
  495. if (*s) {
  496. s++;
  497. } else {
  498. break;
  499. }
  500. }
  501. } else {
  502. /*
  503. * Default host-to-target RT signal mapping.
  504. *
  505. * Signals are supported starting from TARGET_SIGRTMIN and going up
  506. * until we run out of host realtime signals. Glibc uses the lower 2
  507. * RT signals and (hopefully) nobody uses the upper ones.
  508. * This is why SIGRTMIN (34) is generally greater than __SIGRTMIN (32).
  509. * To fix this properly we would need to do manual signal delivery
  510. * multiplexed over a single host signal.
  511. * Attempts for configure "missing" signals via sigaction will be
  512. * silently ignored.
  513. *
  514. * Reserve one signal for internal usage (see below).
  515. */
  516. hsig = SIGRTMIN + 1;
  517. for (tsig = TARGET_SIGRTMIN;
  518. hsig <= SIGRTMAX && tsig <= TARGET_NSIG;
  519. hsig++, tsig++) {
  520. host_to_target_signal_table[hsig] = tsig;
  521. }
  522. }
  523. /*
  524. * Remap the target SIGABRT, so that we can distinguish host abort
  525. * from guest abort. When the guest registers a signal handler or
  526. * calls raise(SIGABRT), the host will raise SIG_RTn. If the guest
  527. * arrives at dump_core_and_abort(), we will map back to host SIGABRT
  528. * so that the parent (native or emulated) sees the correct signal.
  529. * Finally, also map host to guest SIGABRT so that the emulated
  530. * parent sees the correct mapping from wait status.
  531. */
  532. host_to_target_signal_table[SIGABRT] = 0;
  533. for (hsig = SIGRTMIN; hsig <= SIGRTMAX; hsig++) {
  534. if (!host_to_target_signal_table[hsig]) {
  535. host_to_target_signal_table[hsig] = TARGET_SIGABRT;
  536. break;
  537. }
  538. }
  539. if (hsig > SIGRTMAX) {
  540. fprintf(stderr, "No rt signals left for SIGABRT mapping\n");
  541. exit(EXIT_FAILURE);
  542. }
  543. /* Invert the mapping that has already been assigned. */
  544. for (hsig = 1; hsig < _NSIG; hsig++) {
  545. tsig = host_to_target_signal_table[hsig];
  546. if (tsig) {
  547. if (target_to_host_signal_table[tsig]) {
  548. fprintf(stderr, "%d is already mapped to %d\n",
  549. tsig, target_to_host_signal_table[tsig]);
  550. exit(EXIT_FAILURE);
  551. }
  552. target_to_host_signal_table[tsig] = hsig;
  553. }
  554. }
  555. host_to_target_signal_table[SIGABRT] = TARGET_SIGABRT;
  556. /* Map everything else out-of-bounds. */
  557. for (hsig = 1; hsig < _NSIG; hsig++) {
  558. if (host_to_target_signal_table[hsig] == 0) {
  559. host_to_target_signal_table[hsig] = TARGET_NSIG + 1;
  560. }
  561. }
  562. for (count = 0, tsig = 1; tsig <= TARGET_NSIG; tsig++) {
  563. if (target_to_host_signal_table[tsig] == 0) {
  564. target_to_host_signal_table[tsig] = _NSIG;
  565. count++;
  566. }
  567. }
  568. trace_signal_table_init(count);
  569. }
  570. void signal_init(const char *rtsig_map)
  571. {
  572. TaskState *ts = get_task_state(thread_cpu);
  573. struct sigaction act, oact;
  574. /* initialize signal conversion tables */
  575. signal_table_init(rtsig_map);
  576. /* Set the signal mask from the host mask. */
  577. sigprocmask(0, 0, &ts->signal_mask);
  578. sigfillset(&act.sa_mask);
  579. act.sa_flags = SA_SIGINFO;
  580. act.sa_sigaction = host_signal_handler;
  581. /*
  582. * A parent process may configure ignored signals, but all other
  583. * signals are default. For any target signals that have no host
  584. * mapping, set to ignore. For all core_dump_signal, install our
  585. * host signal handler so that we may invoke dump_core_and_abort.
  586. * This includes SIGSEGV and SIGBUS, which are also need our signal
  587. * handler for paging and exceptions.
  588. */
  589. for (int tsig = 1; tsig <= TARGET_NSIG; tsig++) {
  590. int hsig = target_to_host_signal(tsig);
  591. abi_ptr thand = TARGET_SIG_IGN;
  592. if (hsig >= _NSIG) {
  593. continue;
  594. }
  595. /* As we force remap SIGABRT, cannot probe and install in one step. */
  596. if (tsig == TARGET_SIGABRT) {
  597. sigaction(SIGABRT, NULL, &oact);
  598. sigaction(hsig, &act, NULL);
  599. } else {
  600. struct sigaction *iact = core_dump_signal(tsig) ? &act : NULL;
  601. sigaction(hsig, iact, &oact);
  602. }
  603. if (oact.sa_sigaction != (void *)SIG_IGN) {
  604. thand = TARGET_SIG_DFL;
  605. }
  606. sigact_table[tsig - 1]._sa_handler = thand;
  607. }
  608. }
  609. /* Force a synchronously taken signal. The kernel force_sig() function
  610. * also forces the signal to "not blocked, not ignored", but for QEMU
  611. * that work is done in process_pending_signals().
  612. */
  613. void force_sig(int sig)
  614. {
  615. CPUState *cpu = thread_cpu;
  616. target_siginfo_t info = {};
  617. info.si_signo = sig;
  618. info.si_errno = 0;
  619. info.si_code = TARGET_SI_KERNEL;
  620. info._sifields._kill._pid = 0;
  621. info._sifields._kill._uid = 0;
  622. queue_signal(cpu_env(cpu), info.si_signo, QEMU_SI_KILL, &info);
  623. }
  624. /*
  625. * Force a synchronously taken QEMU_SI_FAULT signal. For QEMU the
  626. * 'force' part is handled in process_pending_signals().
  627. */
  628. void force_sig_fault(int sig, int code, abi_ulong addr)
  629. {
  630. CPUState *cpu = thread_cpu;
  631. target_siginfo_t info = {};
  632. info.si_signo = sig;
  633. info.si_errno = 0;
  634. info.si_code = code;
  635. info._sifields._sigfault._addr = addr;
  636. queue_signal(cpu_env(cpu), sig, QEMU_SI_FAULT, &info);
  637. }
  638. /* Force a SIGSEGV if we couldn't write to memory trying to set
  639. * up the signal frame. oldsig is the signal we were trying to handle
  640. * at the point of failure.
  641. */
  642. #if !defined(TARGET_RISCV)
  643. void force_sigsegv(int oldsig)
  644. {
  645. if (oldsig == SIGSEGV) {
  646. /* Make sure we don't try to deliver the signal again; this will
  647. * end up with handle_pending_signal() calling dump_core_and_abort().
  648. */
  649. sigact_table[oldsig - 1]._sa_handler = TARGET_SIG_DFL;
  650. }
  651. force_sig(TARGET_SIGSEGV);
  652. }
  653. #endif
  654. void cpu_loop_exit_sigsegv(CPUState *cpu, target_ulong addr,
  655. MMUAccessType access_type, bool maperr, uintptr_t ra)
  656. {
  657. const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
  658. if (tcg_ops->record_sigsegv) {
  659. tcg_ops->record_sigsegv(cpu, addr, access_type, maperr, ra);
  660. }
  661. force_sig_fault(TARGET_SIGSEGV,
  662. maperr ? TARGET_SEGV_MAPERR : TARGET_SEGV_ACCERR,
  663. addr);
  664. cpu->exception_index = EXCP_INTERRUPT;
  665. cpu_loop_exit_restore(cpu, ra);
  666. }
  667. void cpu_loop_exit_sigbus(CPUState *cpu, target_ulong addr,
  668. MMUAccessType access_type, uintptr_t ra)
  669. {
  670. const TCGCPUOps *tcg_ops = CPU_GET_CLASS(cpu)->tcg_ops;
  671. if (tcg_ops->record_sigbus) {
  672. tcg_ops->record_sigbus(cpu, addr, access_type, ra);
  673. }
  674. force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, addr);
  675. cpu->exception_index = EXCP_INTERRUPT;
  676. cpu_loop_exit_restore(cpu, ra);
  677. }
  678. /* abort execution with signal */
  679. static G_NORETURN
  680. void die_with_signal(int host_sig)
  681. {
  682. struct sigaction act = {
  683. .sa_handler = SIG_DFL,
  684. };
  685. /*
  686. * The proper exit code for dying from an uncaught signal is -<signal>.
  687. * The kernel doesn't allow exit() or _exit() to pass a negative value.
  688. * To get the proper exit code we need to actually die from an uncaught
  689. * signal. Here the default signal handler is installed, we send
  690. * the signal and we wait for it to arrive.
  691. */
  692. sigfillset(&act.sa_mask);
  693. sigaction(host_sig, &act, NULL);
  694. kill(getpid(), host_sig);
  695. /* Make sure the signal isn't masked (reusing the mask inside of act). */
  696. sigdelset(&act.sa_mask, host_sig);
  697. sigsuspend(&act.sa_mask);
  698. /* unreachable */
  699. _exit(EXIT_FAILURE);
  700. }
  701. static G_NORETURN
  702. void dump_core_and_abort(CPUArchState *env, int target_sig)
  703. {
  704. CPUState *cpu = env_cpu(env);
  705. TaskState *ts = get_task_state(cpu);
  706. int host_sig, core_dumped = 0;
  707. /* On exit, undo the remapping of SIGABRT. */
  708. if (target_sig == TARGET_SIGABRT) {
  709. host_sig = SIGABRT;
  710. } else {
  711. host_sig = target_to_host_signal(target_sig);
  712. }
  713. trace_user_dump_core_and_abort(env, target_sig, host_sig);
  714. gdb_signalled(env, target_sig);
  715. /* dump core if supported by target binary format */
  716. if (core_dump_signal(target_sig) && (ts->bprm->core_dump != NULL)) {
  717. stop_all_tasks();
  718. core_dumped =
  719. ((*ts->bprm->core_dump)(target_sig, env) == 0);
  720. }
  721. if (core_dumped) {
  722. /* we already dumped the core of target process, we don't want
  723. * a coredump of qemu itself */
  724. struct rlimit nodump;
  725. getrlimit(RLIMIT_CORE, &nodump);
  726. nodump.rlim_cur=0;
  727. setrlimit(RLIMIT_CORE, &nodump);
  728. (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
  729. target_sig, strsignal(host_sig), "core dumped" );
  730. }
  731. preexit_cleanup(env, 128 + target_sig);
  732. die_with_signal(host_sig);
  733. }
  734. /* queue a signal so that it will be send to the virtual CPU as soon
  735. as possible */
  736. void queue_signal(CPUArchState *env, int sig, int si_type,
  737. target_siginfo_t *info)
  738. {
  739. CPUState *cpu = env_cpu(env);
  740. TaskState *ts = get_task_state(cpu);
  741. trace_user_queue_signal(env, sig);
  742. info->si_code = deposit32(info->si_code, 16, 16, si_type);
  743. ts->sync_signal.info = *info;
  744. ts->sync_signal.pending = sig;
  745. /* signal that a new signal is pending */
  746. qatomic_set(&ts->signal_pending, 1);
  747. }
  748. /* Adjust the signal context to rewind out of safe-syscall if we're in it */
  749. static inline void rewind_if_in_safe_syscall(void *puc)
  750. {
  751. host_sigcontext *uc = (host_sigcontext *)puc;
  752. uintptr_t pcreg = host_signal_pc(uc);
  753. if (pcreg > (uintptr_t)safe_syscall_start
  754. && pcreg < (uintptr_t)safe_syscall_end) {
  755. host_signal_set_pc(uc, (uintptr_t)safe_syscall_start);
  756. }
  757. }
  758. static G_NORETURN
  759. void die_from_signal(siginfo_t *info)
  760. {
  761. char sigbuf[4], codebuf[12];
  762. const char *sig, *code = NULL;
  763. switch (info->si_signo) {
  764. case SIGSEGV:
  765. sig = "SEGV";
  766. switch (info->si_code) {
  767. case SEGV_MAPERR:
  768. code = "MAPERR";
  769. break;
  770. case SEGV_ACCERR:
  771. code = "ACCERR";
  772. break;
  773. }
  774. break;
  775. case SIGBUS:
  776. sig = "BUS";
  777. switch (info->si_code) {
  778. case BUS_ADRALN:
  779. code = "ADRALN";
  780. break;
  781. case BUS_ADRERR:
  782. code = "ADRERR";
  783. break;
  784. }
  785. break;
  786. case SIGILL:
  787. sig = "ILL";
  788. switch (info->si_code) {
  789. case ILL_ILLOPC:
  790. code = "ILLOPC";
  791. break;
  792. case ILL_ILLOPN:
  793. code = "ILLOPN";
  794. break;
  795. case ILL_ILLADR:
  796. code = "ILLADR";
  797. break;
  798. case ILL_PRVOPC:
  799. code = "PRVOPC";
  800. break;
  801. case ILL_PRVREG:
  802. code = "PRVREG";
  803. break;
  804. case ILL_COPROC:
  805. code = "COPROC";
  806. break;
  807. }
  808. break;
  809. case SIGFPE:
  810. sig = "FPE";
  811. switch (info->si_code) {
  812. case FPE_INTDIV:
  813. code = "INTDIV";
  814. break;
  815. case FPE_INTOVF:
  816. code = "INTOVF";
  817. break;
  818. }
  819. break;
  820. case SIGTRAP:
  821. sig = "TRAP";
  822. break;
  823. default:
  824. snprintf(sigbuf, sizeof(sigbuf), "%d", info->si_signo);
  825. sig = sigbuf;
  826. break;
  827. }
  828. if (code == NULL) {
  829. snprintf(codebuf, sizeof(sigbuf), "%d", info->si_code);
  830. code = codebuf;
  831. }
  832. error_report("QEMU internal SIG%s {code=%s, addr=%p}",
  833. sig, code, info->si_addr);
  834. die_with_signal(info->si_signo);
  835. }
  836. static void host_sigsegv_handler(CPUState *cpu, siginfo_t *info,
  837. host_sigcontext *uc)
  838. {
  839. uintptr_t host_addr = (uintptr_t)info->si_addr;
  840. /*
  841. * Convert forcefully to guest address space: addresses outside
  842. * reserved_va are still valid to report via SEGV_MAPERR.
  843. */
  844. bool is_valid = h2g_valid(host_addr);
  845. abi_ptr guest_addr = h2g_nocheck(host_addr);
  846. uintptr_t pc = host_signal_pc(uc);
  847. bool is_write = host_signal_write(info, uc);
  848. MMUAccessType access_type = adjust_signal_pc(&pc, is_write);
  849. bool maperr;
  850. /* If this was a write to a TB protected page, restart. */
  851. if (is_write
  852. && is_valid
  853. && info->si_code == SEGV_ACCERR
  854. && handle_sigsegv_accerr_write(cpu, host_signal_mask(uc),
  855. pc, guest_addr)) {
  856. return;
  857. }
  858. /*
  859. * If the access was not on behalf of the guest, within the executable
  860. * mapping of the generated code buffer, then it is a host bug.
  861. */
  862. if (access_type != MMU_INST_FETCH
  863. && !in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) {
  864. die_from_signal(info);
  865. }
  866. maperr = true;
  867. if (is_valid && info->si_code == SEGV_ACCERR) {
  868. /*
  869. * With reserved_va, the whole address space is PROT_NONE,
  870. * which means that we may get ACCERR when we want MAPERR.
  871. */
  872. if (page_get_flags(guest_addr) & PAGE_VALID) {
  873. maperr = false;
  874. } else {
  875. info->si_code = SEGV_MAPERR;
  876. }
  877. }
  878. sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL);
  879. cpu_loop_exit_sigsegv(cpu, guest_addr, access_type, maperr, pc);
  880. }
  881. static uintptr_t host_sigbus_handler(CPUState *cpu, siginfo_t *info,
  882. host_sigcontext *uc)
  883. {
  884. uintptr_t pc = host_signal_pc(uc);
  885. bool is_write = host_signal_write(info, uc);
  886. MMUAccessType access_type = adjust_signal_pc(&pc, is_write);
  887. /*
  888. * If the access was not on behalf of the guest, within the executable
  889. * mapping of the generated code buffer, then it is a host bug.
  890. */
  891. if (!in_code_gen_buffer((void *)(pc - tcg_splitwx_diff))) {
  892. die_from_signal(info);
  893. }
  894. if (info->si_code == BUS_ADRALN) {
  895. uintptr_t host_addr = (uintptr_t)info->si_addr;
  896. abi_ptr guest_addr = h2g_nocheck(host_addr);
  897. sigprocmask(SIG_SETMASK, host_signal_mask(uc), NULL);
  898. cpu_loop_exit_sigbus(cpu, guest_addr, access_type, pc);
  899. }
  900. return pc;
  901. }
  902. static void host_signal_handler(int host_sig, siginfo_t *info, void *puc)
  903. {
  904. CPUState *cpu = thread_cpu;
  905. CPUArchState *env = cpu_env(cpu);
  906. TaskState *ts = get_task_state(cpu);
  907. target_siginfo_t tinfo;
  908. host_sigcontext *uc = puc;
  909. struct emulated_sigtable *k;
  910. int guest_sig;
  911. uintptr_t pc = 0;
  912. bool sync_sig = false;
  913. void *sigmask;
  914. /*
  915. * Non-spoofed SIGSEGV and SIGBUS are synchronous, and need special
  916. * handling wrt signal blocking and unwinding. Non-spoofed SIGILL,
  917. * SIGFPE, SIGTRAP are always host bugs.
  918. */
  919. if (info->si_code > 0) {
  920. switch (host_sig) {
  921. case SIGSEGV:
  922. /* Only returns on handle_sigsegv_accerr_write success. */
  923. host_sigsegv_handler(cpu, info, uc);
  924. return;
  925. case SIGBUS:
  926. pc = host_sigbus_handler(cpu, info, uc);
  927. sync_sig = true;
  928. break;
  929. case SIGILL:
  930. case SIGFPE:
  931. case SIGTRAP:
  932. die_from_signal(info);
  933. }
  934. }
  935. /* get target signal number */
  936. guest_sig = host_to_target_signal(host_sig);
  937. if (guest_sig < 1 || guest_sig > TARGET_NSIG) {
  938. return;
  939. }
  940. trace_user_host_signal(env, host_sig, guest_sig);
  941. host_to_target_siginfo_noswap(&tinfo, info);
  942. k = &ts->sigtab[guest_sig - 1];
  943. k->info = tinfo;
  944. k->pending = guest_sig;
  945. ts->signal_pending = 1;
  946. /*
  947. * For synchronous signals, unwind the cpu state to the faulting
  948. * insn and then exit back to the main loop so that the signal
  949. * is delivered immediately.
  950. */
  951. if (sync_sig) {
  952. cpu->exception_index = EXCP_INTERRUPT;
  953. cpu_loop_exit_restore(cpu, pc);
  954. }
  955. rewind_if_in_safe_syscall(puc);
  956. /*
  957. * Block host signals until target signal handler entered. We
  958. * can't block SIGSEGV or SIGBUS while we're executing guest
  959. * code in case the guest code provokes one in the window between
  960. * now and it getting out to the main loop. Signals will be
  961. * unblocked again in process_pending_signals().
  962. *
  963. * WARNING: we cannot use sigfillset() here because the sigmask
  964. * field is a kernel sigset_t, which is much smaller than the
  965. * libc sigset_t which sigfillset() operates on. Using sigfillset()
  966. * would write 0xff bytes off the end of the structure and trash
  967. * data on the struct.
  968. */
  969. sigmask = host_signal_mask(uc);
  970. memset(sigmask, 0xff, SIGSET_T_SIZE);
  971. sigdelset(sigmask, SIGSEGV);
  972. sigdelset(sigmask, SIGBUS);
  973. /* interrupt the virtual CPU as soon as possible */
  974. cpu_exit(thread_cpu);
  975. }
  976. /* do_sigaltstack() returns target values and errnos. */
  977. /* compare linux/kernel/signal.c:do_sigaltstack() */
  978. abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr,
  979. CPUArchState *env)
  980. {
  981. target_stack_t oss, *uoss = NULL;
  982. abi_long ret = -TARGET_EFAULT;
  983. if (uoss_addr) {
  984. /* Verify writability now, but do not alter user memory yet. */
  985. if (!lock_user_struct(VERIFY_WRITE, uoss, uoss_addr, 0)) {
  986. goto out;
  987. }
  988. target_save_altstack(&oss, env);
  989. }
  990. if (uss_addr) {
  991. target_stack_t *uss;
  992. if (!lock_user_struct(VERIFY_READ, uss, uss_addr, 1)) {
  993. goto out;
  994. }
  995. ret = target_restore_altstack(uss, env);
  996. if (ret) {
  997. goto out;
  998. }
  999. }
  1000. if (uoss_addr) {
  1001. memcpy(uoss, &oss, sizeof(oss));
  1002. unlock_user_struct(uoss, uoss_addr, 1);
  1003. uoss = NULL;
  1004. }
  1005. ret = 0;
  1006. out:
  1007. if (uoss) {
  1008. unlock_user_struct(uoss, uoss_addr, 0);
  1009. }
  1010. return ret;
  1011. }
  1012. /* do_sigaction() return target values and host errnos */
  1013. int do_sigaction(int sig, const struct target_sigaction *act,
  1014. struct target_sigaction *oact, abi_ulong ka_restorer)
  1015. {
  1016. struct target_sigaction *k;
  1017. int host_sig;
  1018. int ret = 0;
  1019. trace_signal_do_sigaction_guest(sig, TARGET_NSIG);
  1020. if (sig < 1 || sig > TARGET_NSIG) {
  1021. return -TARGET_EINVAL;
  1022. }
  1023. if (act && (sig == TARGET_SIGKILL || sig == TARGET_SIGSTOP)) {
  1024. return -TARGET_EINVAL;
  1025. }
  1026. if (block_signals()) {
  1027. return -QEMU_ERESTARTSYS;
  1028. }
  1029. k = &sigact_table[sig - 1];
  1030. if (oact) {
  1031. __put_user(k->_sa_handler, &oact->_sa_handler);
  1032. __put_user(k->sa_flags, &oact->sa_flags);
  1033. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  1034. __put_user(k->sa_restorer, &oact->sa_restorer);
  1035. #endif
  1036. /* Not swapped. */
  1037. oact->sa_mask = k->sa_mask;
  1038. }
  1039. if (act) {
  1040. __get_user(k->_sa_handler, &act->_sa_handler);
  1041. __get_user(k->sa_flags, &act->sa_flags);
  1042. #ifdef TARGET_ARCH_HAS_SA_RESTORER
  1043. __get_user(k->sa_restorer, &act->sa_restorer);
  1044. #endif
  1045. #ifdef TARGET_ARCH_HAS_KA_RESTORER
  1046. k->ka_restorer = ka_restorer;
  1047. #endif
  1048. /* To be swapped in target_to_host_sigset. */
  1049. k->sa_mask = act->sa_mask;
  1050. /* we update the host linux signal state */
  1051. host_sig = target_to_host_signal(sig);
  1052. trace_signal_do_sigaction_host(host_sig, TARGET_NSIG);
  1053. if (host_sig > SIGRTMAX) {
  1054. /* we don't have enough host signals to map all target signals */
  1055. qemu_log_mask(LOG_UNIMP, "Unsupported target signal #%d, ignored\n",
  1056. sig);
  1057. /*
  1058. * we don't return an error here because some programs try to
  1059. * register an handler for all possible rt signals even if they
  1060. * don't need it.
  1061. * An error here can abort them whereas there can be no problem
  1062. * to not have the signal available later.
  1063. * This is the case for golang,
  1064. * See https://github.com/golang/go/issues/33746
  1065. * So we silently ignore the error.
  1066. */
  1067. return 0;
  1068. }
  1069. if (host_sig != SIGSEGV && host_sig != SIGBUS) {
  1070. struct sigaction act1;
  1071. sigfillset(&act1.sa_mask);
  1072. act1.sa_flags = SA_SIGINFO;
  1073. if (k->_sa_handler == TARGET_SIG_IGN) {
  1074. /*
  1075. * It is important to update the host kernel signal ignore
  1076. * state to avoid getting unexpected interrupted syscalls.
  1077. */
  1078. act1.sa_sigaction = (void *)SIG_IGN;
  1079. } else if (k->_sa_handler == TARGET_SIG_DFL) {
  1080. if (core_dump_signal(sig)) {
  1081. act1.sa_sigaction = host_signal_handler;
  1082. } else {
  1083. act1.sa_sigaction = (void *)SIG_DFL;
  1084. }
  1085. } else {
  1086. act1.sa_sigaction = host_signal_handler;
  1087. if (k->sa_flags & TARGET_SA_RESTART) {
  1088. act1.sa_flags |= SA_RESTART;
  1089. }
  1090. }
  1091. ret = sigaction(host_sig, &act1, NULL);
  1092. }
  1093. }
  1094. return ret;
  1095. }
  1096. static void handle_pending_signal(CPUArchState *cpu_env, int sig,
  1097. struct emulated_sigtable *k)
  1098. {
  1099. CPUState *cpu = env_cpu(cpu_env);
  1100. abi_ulong handler;
  1101. sigset_t set;
  1102. target_siginfo_t unswapped;
  1103. target_sigset_t target_old_set;
  1104. struct target_sigaction *sa;
  1105. TaskState *ts = get_task_state(cpu);
  1106. trace_user_handle_signal(cpu_env, sig);
  1107. /* dequeue signal */
  1108. k->pending = 0;
  1109. /*
  1110. * Writes out siginfo values byteswapped, accordingly to the target.
  1111. * It also cleans the si_type from si_code making it correct for
  1112. * the target. We must hold on to the original unswapped copy for
  1113. * strace below, because si_type is still required there.
  1114. */
  1115. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  1116. unswapped = k->info;
  1117. }
  1118. tswap_siginfo(&k->info, &k->info);
  1119. sig = gdb_handlesig(cpu, sig, NULL, &k->info, sizeof(k->info));
  1120. if (!sig) {
  1121. sa = NULL;
  1122. handler = TARGET_SIG_IGN;
  1123. } else {
  1124. sa = &sigact_table[sig - 1];
  1125. handler = sa->_sa_handler;
  1126. }
  1127. if (unlikely(qemu_loglevel_mask(LOG_STRACE))) {
  1128. print_taken_signal(sig, &unswapped);
  1129. }
  1130. if (handler == TARGET_SIG_DFL) {
  1131. /* default handler : ignore some signal. The other are job control or fatal */
  1132. if (sig == TARGET_SIGTSTP || sig == TARGET_SIGTTIN || sig == TARGET_SIGTTOU) {
  1133. kill(getpid(),SIGSTOP);
  1134. } else if (sig != TARGET_SIGCHLD &&
  1135. sig != TARGET_SIGURG &&
  1136. sig != TARGET_SIGWINCH &&
  1137. sig != TARGET_SIGCONT) {
  1138. dump_core_and_abort(cpu_env, sig);
  1139. }
  1140. } else if (handler == TARGET_SIG_IGN) {
  1141. /* ignore sig */
  1142. } else if (handler == TARGET_SIG_ERR) {
  1143. dump_core_and_abort(cpu_env, sig);
  1144. } else {
  1145. /* compute the blocked signals during the handler execution */
  1146. sigset_t *blocked_set;
  1147. target_to_host_sigset(&set, &sa->sa_mask);
  1148. /* SA_NODEFER indicates that the current signal should not be
  1149. blocked during the handler */
  1150. if (!(sa->sa_flags & TARGET_SA_NODEFER))
  1151. sigaddset(&set, target_to_host_signal(sig));
  1152. /* save the previous blocked signal state to restore it at the
  1153. end of the signal execution (see do_sigreturn) */
  1154. host_to_target_sigset_internal(&target_old_set, &ts->signal_mask);
  1155. /* block signals in the handler */
  1156. blocked_set = ts->in_sigsuspend ?
  1157. &ts->sigsuspend_mask : &ts->signal_mask;
  1158. sigorset(&ts->signal_mask, blocked_set, &set);
  1159. ts->in_sigsuspend = 0;
  1160. /* if the CPU is in VM86 mode, we restore the 32 bit values */
  1161. #if defined(TARGET_I386) && !defined(TARGET_X86_64)
  1162. {
  1163. CPUX86State *env = cpu_env;
  1164. if (env->eflags & VM_MASK)
  1165. save_v86_state(env);
  1166. }
  1167. #endif
  1168. /* prepare the stack frame of the virtual CPU */
  1169. #if defined(TARGET_ARCH_HAS_SETUP_FRAME)
  1170. if (sa->sa_flags & TARGET_SA_SIGINFO) {
  1171. setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
  1172. } else {
  1173. setup_frame(sig, sa, &target_old_set, cpu_env);
  1174. }
  1175. #else
  1176. /* These targets do not have traditional signals. */
  1177. setup_rt_frame(sig, sa, &k->info, &target_old_set, cpu_env);
  1178. #endif
  1179. if (sa->sa_flags & TARGET_SA_RESETHAND) {
  1180. sa->_sa_handler = TARGET_SIG_DFL;
  1181. }
  1182. }
  1183. }
  1184. void process_pending_signals(CPUArchState *cpu_env)
  1185. {
  1186. CPUState *cpu = env_cpu(cpu_env);
  1187. int sig;
  1188. TaskState *ts = get_task_state(cpu);
  1189. sigset_t set;
  1190. sigset_t *blocked_set;
  1191. while (qatomic_read(&ts->signal_pending)) {
  1192. sigfillset(&set);
  1193. sigprocmask(SIG_SETMASK, &set, 0);
  1194. restart_scan:
  1195. sig = ts->sync_signal.pending;
  1196. if (sig) {
  1197. /* Synchronous signals are forced,
  1198. * see force_sig_info() and callers in Linux
  1199. * Note that not all of our queue_signal() calls in QEMU correspond
  1200. * to force_sig_info() calls in Linux (some are send_sig_info()).
  1201. * However it seems like a kernel bug to me to allow the process
  1202. * to block a synchronous signal since it could then just end up
  1203. * looping round and round indefinitely.
  1204. */
  1205. if (sigismember(&ts->signal_mask, target_to_host_signal_table[sig])
  1206. || sigact_table[sig - 1]._sa_handler == TARGET_SIG_IGN) {
  1207. sigdelset(&ts->signal_mask, target_to_host_signal_table[sig]);
  1208. sigact_table[sig - 1]._sa_handler = TARGET_SIG_DFL;
  1209. }
  1210. handle_pending_signal(cpu_env, sig, &ts->sync_signal);
  1211. }
  1212. for (sig = 1; sig <= TARGET_NSIG; sig++) {
  1213. blocked_set = ts->in_sigsuspend ?
  1214. &ts->sigsuspend_mask : &ts->signal_mask;
  1215. if (ts->sigtab[sig - 1].pending &&
  1216. (!sigismember(blocked_set,
  1217. target_to_host_signal_table[sig]))) {
  1218. handle_pending_signal(cpu_env, sig, &ts->sigtab[sig - 1]);
  1219. /* Restart scan from the beginning, as handle_pending_signal
  1220. * might have resulted in a new synchronous signal (eg SIGSEGV).
  1221. */
  1222. goto restart_scan;
  1223. }
  1224. }
  1225. /* if no signal is pending, unblock signals and recheck (the act
  1226. * of unblocking might cause us to take another host signal which
  1227. * will set signal_pending again).
  1228. */
  1229. qatomic_set(&ts->signal_pending, 0);
  1230. ts->in_sigsuspend = 0;
  1231. set = ts->signal_mask;
  1232. sigdelset(&set, SIGSEGV);
  1233. sigdelset(&set, SIGBUS);
  1234. sigprocmask(SIG_SETMASK, &set, 0);
  1235. }
  1236. ts->in_sigsuspend = 0;
  1237. }
  1238. int process_sigsuspend_mask(sigset_t **pset, target_ulong sigset,
  1239. target_ulong sigsize)
  1240. {
  1241. TaskState *ts = get_task_state(thread_cpu);
  1242. sigset_t *host_set = &ts->sigsuspend_mask;
  1243. target_sigset_t *target_sigset;
  1244. if (sigsize != sizeof(*target_sigset)) {
  1245. /* Like the kernel, we enforce correct size sigsets */
  1246. return -TARGET_EINVAL;
  1247. }
  1248. target_sigset = lock_user(VERIFY_READ, sigset, sigsize, 1);
  1249. if (!target_sigset) {
  1250. return -TARGET_EFAULT;
  1251. }
  1252. target_to_host_sigset(host_set, target_sigset);
  1253. unlock_user(target_sigset, sigset, 0);
  1254. *pset = host_set;
  1255. return 0;
  1256. }