2
0

signal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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.h"
  21. #include "user-internals.h"
  22. #include "signal-common.h"
  23. #include "linux-user/trace.h"
  24. # if defined(TARGET_ABI_MIPSO32)
  25. struct target_sigcontext {
  26. uint32_t sc_regmask; /* Unused */
  27. uint32_t sc_status;
  28. uint64_t sc_pc;
  29. uint64_t sc_regs[32];
  30. uint64_t sc_fpregs[32];
  31. uint32_t sc_ownedfp; /* Unused */
  32. uint32_t sc_fpc_csr;
  33. uint32_t sc_fpc_eir; /* Unused */
  34. uint32_t sc_used_math;
  35. uint32_t sc_dsp; /* dsp status, was sc_ssflags */
  36. uint32_t pad0;
  37. uint64_t sc_mdhi;
  38. uint64_t sc_mdlo;
  39. target_ulong sc_hi1; /* Was sc_cause */
  40. target_ulong sc_lo1; /* Was sc_badvaddr */
  41. target_ulong sc_hi2; /* Was sc_sigset[4] */
  42. target_ulong sc_lo2;
  43. target_ulong sc_hi3;
  44. target_ulong sc_lo3;
  45. };
  46. # else /* N32 || N64 */
  47. struct target_sigcontext {
  48. uint64_t sc_regs[32];
  49. uint64_t sc_fpregs[32];
  50. uint64_t sc_mdhi;
  51. uint64_t sc_hi1;
  52. uint64_t sc_hi2;
  53. uint64_t sc_hi3;
  54. uint64_t sc_mdlo;
  55. uint64_t sc_lo1;
  56. uint64_t sc_lo2;
  57. uint64_t sc_lo3;
  58. uint64_t sc_pc;
  59. uint32_t sc_fpc_csr;
  60. uint32_t sc_used_math;
  61. uint32_t sc_dsp;
  62. uint32_t sc_reserved;
  63. };
  64. # endif /* O32 */
  65. struct sigframe {
  66. uint32_t sf_ass[4]; /* argument save space for o32 */
  67. uint32_t sf_code[2]; /* signal trampoline */
  68. struct target_sigcontext sf_sc;
  69. target_sigset_t sf_mask;
  70. };
  71. struct target_ucontext {
  72. abi_ulong tuc_flags;
  73. abi_ulong tuc_link;
  74. target_stack_t tuc_stack;
  75. struct target_sigcontext tuc_mcontext;
  76. target_sigset_t tuc_sigmask;
  77. };
  78. struct target_rt_sigframe {
  79. uint32_t rs_ass[4]; /* argument save space for o32 */
  80. uint32_t rs_code[2]; /* signal trampoline */
  81. struct target_siginfo rs_info;
  82. struct target_ucontext rs_uc;
  83. };
  84. /* Install trampoline to jump back from signal handler */
  85. static void install_sigtramp(uint32_t *tramp, unsigned int syscall)
  86. {
  87. /*
  88. * Set up the return code ...
  89. *
  90. * li v0, __NR__foo_sigreturn
  91. * syscall
  92. */
  93. __put_user(0x24020000 + syscall, tramp + 0);
  94. __put_user(0x0000000c , tramp + 1);
  95. }
  96. static inline void setup_sigcontext(CPUMIPSState *regs,
  97. struct target_sigcontext *sc)
  98. {
  99. int i;
  100. __put_user(exception_resume_pc(regs), &sc->sc_pc);
  101. regs->hflags &= ~MIPS_HFLAG_BMASK;
  102. __put_user(0, &sc->sc_regs[0]);
  103. for (i = 1; i < 32; ++i) {
  104. __put_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
  105. }
  106. __put_user(regs->active_tc.HI[0], &sc->sc_mdhi);
  107. __put_user(regs->active_tc.LO[0], &sc->sc_mdlo);
  108. /* Rather than checking for dsp existence, always copy. The storage
  109. would just be garbage otherwise. */
  110. __put_user(regs->active_tc.HI[1], &sc->sc_hi1);
  111. __put_user(regs->active_tc.HI[2], &sc->sc_hi2);
  112. __put_user(regs->active_tc.HI[3], &sc->sc_hi3);
  113. __put_user(regs->active_tc.LO[1], &sc->sc_lo1);
  114. __put_user(regs->active_tc.LO[2], &sc->sc_lo2);
  115. __put_user(regs->active_tc.LO[3], &sc->sc_lo3);
  116. {
  117. uint32_t dsp = cpu_rddsp(0x3ff, regs);
  118. __put_user(dsp, &sc->sc_dsp);
  119. }
  120. __put_user(1, &sc->sc_used_math);
  121. for (i = 0; i < 32; ++i) {
  122. __put_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
  123. }
  124. }
  125. static inline void
  126. restore_sigcontext(CPUMIPSState *regs, struct target_sigcontext *sc)
  127. {
  128. int i;
  129. __get_user(regs->CP0_EPC, &sc->sc_pc);
  130. __get_user(regs->active_tc.HI[0], &sc->sc_mdhi);
  131. __get_user(regs->active_tc.LO[0], &sc->sc_mdlo);
  132. for (i = 1; i < 32; ++i) {
  133. __get_user(regs->active_tc.gpr[i], &sc->sc_regs[i]);
  134. }
  135. __get_user(regs->active_tc.HI[1], &sc->sc_hi1);
  136. __get_user(regs->active_tc.HI[2], &sc->sc_hi2);
  137. __get_user(regs->active_tc.HI[3], &sc->sc_hi3);
  138. __get_user(regs->active_tc.LO[1], &sc->sc_lo1);
  139. __get_user(regs->active_tc.LO[2], &sc->sc_lo2);
  140. __get_user(regs->active_tc.LO[3], &sc->sc_lo3);
  141. {
  142. uint32_t dsp;
  143. __get_user(dsp, &sc->sc_dsp);
  144. cpu_wrdsp(dsp, 0x3ff, regs);
  145. }
  146. for (i = 0; i < 32; ++i) {
  147. __get_user(regs->active_fpu.fpr[i].d, &sc->sc_fpregs[i]);
  148. }
  149. }
  150. /*
  151. * Determine which stack to use..
  152. */
  153. static inline abi_ulong
  154. get_sigframe(struct target_sigaction *ka, CPUMIPSState *regs, size_t frame_size)
  155. {
  156. unsigned long sp;
  157. /*
  158. * FPU emulator may have its own trampoline active just
  159. * above the user stack, 16-bytes before the next lowest
  160. * 16 byte boundary. Try to avoid trashing it.
  161. */
  162. sp = target_sigsp(get_sp_from_cpustate(regs) - 32, ka);
  163. return (sp - frame_size) & ~7;
  164. }
  165. static void mips_set_hflags_isa_mode_from_pc(CPUMIPSState *env)
  166. {
  167. if (env->insn_flags & (ASE_MIPS16 | ASE_MICROMIPS)) {
  168. env->hflags &= ~MIPS_HFLAG_M16;
  169. env->hflags |= (env->active_tc.PC & 1) << MIPS_HFLAG_M16_SHIFT;
  170. env->active_tc.PC &= ~(target_ulong) 1;
  171. }
  172. }
  173. # if defined(TARGET_ABI_MIPSO32)
  174. /* compare linux/arch/mips/kernel/signal.c:setup_frame() */
  175. void setup_frame(int sig, struct target_sigaction * ka,
  176. target_sigset_t *set, CPUMIPSState *regs)
  177. {
  178. struct sigframe *frame;
  179. abi_ulong frame_addr;
  180. int i;
  181. frame_addr = get_sigframe(ka, regs, sizeof(*frame));
  182. trace_user_setup_frame(regs, frame_addr);
  183. if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
  184. goto give_sigsegv;
  185. }
  186. setup_sigcontext(regs, &frame->sf_sc);
  187. for(i = 0; i < TARGET_NSIG_WORDS; i++) {
  188. __put_user(set->sig[i], &frame->sf_mask.sig[i]);
  189. }
  190. /*
  191. * Arguments to signal handler:
  192. *
  193. * a0 = signal number
  194. * a1 = 0 (should be cause)
  195. * a2 = pointer to struct sigcontext
  196. *
  197. * $25 and PC point to the signal handler, $29 points to the
  198. * struct sigframe.
  199. */
  200. regs->active_tc.gpr[ 4] = sig;
  201. regs->active_tc.gpr[ 5] = 0;
  202. regs->active_tc.gpr[ 6] = frame_addr + offsetof(struct sigframe, sf_sc);
  203. regs->active_tc.gpr[29] = frame_addr;
  204. regs->active_tc.gpr[31] = default_sigreturn;
  205. /* The original kernel code sets CP0_EPC to the handler
  206. * since it returns to userland using eret
  207. * we cannot do this here, and we must set PC directly */
  208. regs->active_tc.PC = regs->active_tc.gpr[25] = ka->_sa_handler;
  209. mips_set_hflags_isa_mode_from_pc(regs);
  210. unlock_user_struct(frame, frame_addr, 1);
  211. return;
  212. give_sigsegv:
  213. force_sigsegv(sig);
  214. }
  215. long do_sigreturn(CPUMIPSState *regs)
  216. {
  217. struct sigframe *frame;
  218. abi_ulong frame_addr;
  219. sigset_t blocked;
  220. target_sigset_t target_set;
  221. int i;
  222. frame_addr = regs->active_tc.gpr[29];
  223. trace_user_do_sigreturn(regs, frame_addr);
  224. if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1))
  225. goto badframe;
  226. for(i = 0; i < TARGET_NSIG_WORDS; i++) {
  227. __get_user(target_set.sig[i], &frame->sf_mask.sig[i]);
  228. }
  229. target_to_host_sigset_internal(&blocked, &target_set);
  230. set_sigmask(&blocked);
  231. restore_sigcontext(regs, &frame->sf_sc);
  232. #if 0
  233. /*
  234. * Don't let your children do this ...
  235. */
  236. __asm__ __volatile__(
  237. "move\t$29, %0\n\t"
  238. "j\tsyscall_exit"
  239. :/* no outputs */
  240. :"r" (&regs));
  241. /* Unreached */
  242. #endif
  243. regs->active_tc.PC = regs->CP0_EPC;
  244. mips_set_hflags_isa_mode_from_pc(regs);
  245. /* I am not sure this is right, but it seems to work
  246. * maybe a problem with nested signals ? */
  247. regs->CP0_EPC = 0;
  248. return -QEMU_ESIGRETURN;
  249. badframe:
  250. force_sig(TARGET_SIGSEGV);
  251. return -QEMU_ESIGRETURN;
  252. }
  253. # endif /* O32 */
  254. void setup_rt_frame(int sig, struct target_sigaction *ka,
  255. target_siginfo_t *info,
  256. target_sigset_t *set, CPUMIPSState *env)
  257. {
  258. struct target_rt_sigframe *frame;
  259. abi_ulong frame_addr;
  260. int i;
  261. frame_addr = get_sigframe(ka, env, sizeof(*frame));
  262. trace_user_setup_rt_frame(env, frame_addr);
  263. if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
  264. goto give_sigsegv;
  265. }
  266. frame->rs_info = *info;
  267. __put_user(0, &frame->rs_uc.tuc_flags);
  268. __put_user(0, &frame->rs_uc.tuc_link);
  269. target_save_altstack(&frame->rs_uc.tuc_stack, env);
  270. setup_sigcontext(env, &frame->rs_uc.tuc_mcontext);
  271. for (i = 0; i < TARGET_NSIG_WORDS; i++) {
  272. __put_user(set->sig[i], &frame->rs_uc.tuc_sigmask.sig[i]);
  273. }
  274. /*
  275. * Arguments to signal handler:
  276. *
  277. * a0 = signal number
  278. * a1 = pointer to siginfo_t
  279. * a2 = pointer to ucontext_t
  280. *
  281. * $25 and PC point to the signal handler, $29 points to the
  282. * struct sigframe.
  283. */
  284. env->active_tc.gpr[ 4] = sig;
  285. env->active_tc.gpr[ 5] = frame_addr
  286. + offsetof(struct target_rt_sigframe, rs_info);
  287. env->active_tc.gpr[ 6] = frame_addr
  288. + offsetof(struct target_rt_sigframe, rs_uc);
  289. env->active_tc.gpr[29] = frame_addr;
  290. env->active_tc.gpr[31] = default_rt_sigreturn;
  291. /*
  292. * The original kernel code sets CP0_EPC to the handler
  293. * since it returns to userland using eret
  294. * we cannot do this here, and we must set PC directly
  295. */
  296. env->active_tc.PC = env->active_tc.gpr[25] = ka->_sa_handler;
  297. mips_set_hflags_isa_mode_from_pc(env);
  298. unlock_user_struct(frame, frame_addr, 1);
  299. return;
  300. give_sigsegv:
  301. unlock_user_struct(frame, frame_addr, 1);
  302. force_sigsegv(sig);
  303. }
  304. long do_rt_sigreturn(CPUMIPSState *env)
  305. {
  306. struct target_rt_sigframe *frame;
  307. abi_ulong frame_addr;
  308. sigset_t blocked;
  309. frame_addr = env->active_tc.gpr[29];
  310. trace_user_do_rt_sigreturn(env, frame_addr);
  311. if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
  312. goto badframe;
  313. }
  314. target_to_host_sigset(&blocked, &frame->rs_uc.tuc_sigmask);
  315. set_sigmask(&blocked);
  316. restore_sigcontext(env, &frame->rs_uc.tuc_mcontext);
  317. target_restore_altstack(&frame->rs_uc.tuc_stack, env);
  318. env->active_tc.PC = env->CP0_EPC;
  319. mips_set_hflags_isa_mode_from_pc(env);
  320. /* I am not sure this is right, but it seems to work
  321. * maybe a problem with nested signals ? */
  322. env->CP0_EPC = 0;
  323. return -QEMU_ESIGRETURN;
  324. badframe:
  325. force_sig(TARGET_SIGSEGV);
  326. return -QEMU_ESIGRETURN;
  327. }
  328. void setup_sigtramp(abi_ulong sigtramp_page)
  329. {
  330. uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 2 * 8, 0);
  331. assert(tramp != NULL);
  332. #ifdef TARGET_ARCH_HAS_SETUP_FRAME
  333. default_sigreturn = sigtramp_page;
  334. install_sigtramp(tramp, TARGET_NR_sigreturn);
  335. #endif
  336. default_rt_sigreturn = sigtramp_page + 8;
  337. install_sigtramp(tramp + 2, TARGET_NR_rt_sigreturn);
  338. unlock_user(tramp, sigtramp_page, 2 * 8);
  339. }