2
0

signal.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "signal-common.h"
  22. #include "linux-user/trace.h"
  23. struct target_sigcontext {
  24. union {
  25. /* General-purpose registers. */
  26. abi_ulong gregs[56];
  27. struct {
  28. abi_ulong __gregs[53];
  29. abi_ulong tp; /* Aliases gregs[TREG_TP]. */
  30. abi_ulong sp; /* Aliases gregs[TREG_SP]. */
  31. abi_ulong lr; /* Aliases gregs[TREG_LR]. */
  32. };
  33. };
  34. abi_ulong pc; /* Program counter. */
  35. abi_ulong ics; /* In Interrupt Critical Section? */
  36. abi_ulong faultnum; /* Fault number. */
  37. abi_ulong pad[5];
  38. };
  39. struct target_ucontext {
  40. abi_ulong tuc_flags;
  41. abi_ulong tuc_link;
  42. target_stack_t tuc_stack;
  43. struct target_sigcontext tuc_mcontext;
  44. target_sigset_t tuc_sigmask; /* mask last for extensibility */
  45. };
  46. struct target_rt_sigframe {
  47. unsigned char save_area[16]; /* caller save area */
  48. struct target_siginfo info;
  49. struct target_ucontext uc;
  50. abi_ulong retcode[2];
  51. };
  52. #define INSN_MOVELI_R10_139 0x00045fe551483000ULL /* { moveli r10, 139 } */
  53. #define INSN_SWINT1 0x286b180051485000ULL /* { swint1 } */
  54. static void setup_sigcontext(struct target_sigcontext *sc,
  55. CPUArchState *env, int signo)
  56. {
  57. int i;
  58. for (i = 0; i < TILEGX_R_COUNT; ++i) {
  59. __put_user(env->regs[i], &sc->gregs[i]);
  60. }
  61. __put_user(env->pc, &sc->pc);
  62. __put_user(0, &sc->ics);
  63. __put_user(signo, &sc->faultnum);
  64. }
  65. static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
  66. {
  67. int i;
  68. for (i = 0; i < TILEGX_R_COUNT; ++i) {
  69. __get_user(env->regs[i], &sc->gregs[i]);
  70. }
  71. __get_user(env->pc, &sc->pc);
  72. }
  73. static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
  74. size_t frame_size)
  75. {
  76. unsigned long sp = get_sp_from_cpustate(env);
  77. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
  78. return -1UL;
  79. }
  80. sp = target_sigsp(sp, ka) - frame_size;
  81. sp &= -16UL;
  82. return sp;
  83. }
  84. void setup_rt_frame(int sig, struct target_sigaction *ka,
  85. target_siginfo_t *info,
  86. target_sigset_t *set, CPUArchState *env)
  87. {
  88. abi_ulong frame_addr;
  89. struct target_rt_sigframe *frame;
  90. unsigned long restorer;
  91. frame_addr = get_sigframe(ka, env, sizeof(*frame));
  92. trace_user_setup_rt_frame(env, frame_addr);
  93. if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
  94. goto give_sigsegv;
  95. }
  96. /* Always write at least the signal number for the stack backtracer. */
  97. if (ka->sa_flags & TARGET_SA_SIGINFO) {
  98. /* At sigreturn time, restore the callee-save registers too. */
  99. tswap_siginfo(&frame->info, info);
  100. /* regs->flags |= PT_FLAGS_RESTORE_REGS; FIXME: we can skip it? */
  101. } else {
  102. __put_user(info->si_signo, &frame->info.si_signo);
  103. }
  104. /* Create the ucontext. */
  105. __put_user(0, &frame->uc.tuc_flags);
  106. __put_user(0, &frame->uc.tuc_link);
  107. target_save_altstack(&frame->uc.tuc_stack, env);
  108. setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
  109. if (ka->sa_flags & TARGET_SA_RESTORER) {
  110. restorer = (unsigned long) ka->sa_restorer;
  111. } else {
  112. __put_user(INSN_MOVELI_R10_139, &frame->retcode[0]);
  113. __put_user(INSN_SWINT1, &frame->retcode[1]);
  114. restorer = frame_addr + offsetof(struct target_rt_sigframe, retcode);
  115. }
  116. env->pc = (unsigned long) ka->_sa_handler;
  117. env->regs[TILEGX_R_SP] = (unsigned long) frame;
  118. env->regs[TILEGX_R_LR] = restorer;
  119. env->regs[0] = (unsigned long) sig;
  120. env->regs[1] = (unsigned long) &frame->info;
  121. env->regs[2] = (unsigned long) &frame->uc;
  122. /* regs->flags |= PT_FLAGS_CALLER_SAVES; FIXME: we can skip it? */
  123. unlock_user_struct(frame, frame_addr, 1);
  124. return;
  125. give_sigsegv:
  126. force_sigsegv(sig);
  127. }
  128. long do_rt_sigreturn(CPUTLGState *env)
  129. {
  130. abi_ulong frame_addr = env->regs[TILEGX_R_SP];
  131. struct target_rt_sigframe *frame;
  132. sigset_t set;
  133. trace_user_do_rt_sigreturn(env, frame_addr);
  134. if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
  135. goto badframe;
  136. }
  137. target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
  138. set_sigmask(&set);
  139. restore_sigcontext(env, &frame->uc.tuc_mcontext);
  140. if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
  141. uc.tuc_stack),
  142. 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
  143. goto badframe;
  144. }
  145. unlock_user_struct(frame, frame_addr, 0);
  146. return -TARGET_QEMU_ESIGRETURN;
  147. badframe:
  148. unlock_user_struct(frame, frame_addr, 0);
  149. force_sig(TARGET_SIGSEGV);
  150. return -TARGET_QEMU_ESIGRETURN;
  151. }