2
0

signal.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. #include "target/arm/cpu-features.h"
  25. #include "vdso-asmoffset.h"
  26. struct target_sigcontext {
  27. abi_ulong trap_no;
  28. abi_ulong error_code;
  29. abi_ulong oldmask;
  30. abi_ulong arm_r0;
  31. abi_ulong arm_r1;
  32. abi_ulong arm_r2;
  33. abi_ulong arm_r3;
  34. abi_ulong arm_r4;
  35. abi_ulong arm_r5;
  36. abi_ulong arm_r6;
  37. abi_ulong arm_r7;
  38. abi_ulong arm_r8;
  39. abi_ulong arm_r9;
  40. abi_ulong arm_r10;
  41. abi_ulong arm_fp;
  42. abi_ulong arm_ip;
  43. abi_ulong arm_sp;
  44. abi_ulong arm_lr;
  45. abi_ulong arm_pc;
  46. abi_ulong arm_cpsr;
  47. abi_ulong fault_address;
  48. };
  49. struct target_ucontext {
  50. abi_ulong tuc_flags;
  51. abi_ulong tuc_link;
  52. target_stack_t tuc_stack;
  53. struct target_sigcontext tuc_mcontext;
  54. target_sigset_t tuc_sigmask; /* mask last for extensibility */
  55. char __unused[128 - sizeof(target_sigset_t)];
  56. abi_ulong tuc_regspace[128] __attribute__((__aligned__(8)));
  57. };
  58. struct target_user_vfp {
  59. uint64_t fpregs[32];
  60. abi_ulong fpscr;
  61. };
  62. struct target_user_vfp_exc {
  63. abi_ulong fpexc;
  64. abi_ulong fpinst;
  65. abi_ulong fpinst2;
  66. };
  67. struct target_vfp_sigframe {
  68. abi_ulong magic;
  69. abi_ulong size;
  70. struct target_user_vfp ufp;
  71. struct target_user_vfp_exc ufp_exc;
  72. } __attribute__((__aligned__(8)));
  73. struct target_iwmmxt_sigframe {
  74. abi_ulong magic;
  75. abi_ulong size;
  76. uint64_t regs[16];
  77. /* Note that not all the coprocessor control registers are stored here */
  78. uint32_t wcssf;
  79. uint32_t wcasf;
  80. uint32_t wcgr0;
  81. uint32_t wcgr1;
  82. uint32_t wcgr2;
  83. uint32_t wcgr3;
  84. } __attribute__((__aligned__(8)));
  85. #define TARGET_VFP_MAGIC 0x56465001
  86. #define TARGET_IWMMXT_MAGIC 0x12ef842a
  87. struct sigframe
  88. {
  89. struct target_ucontext uc;
  90. abi_ulong retcode[4];
  91. };
  92. struct rt_sigframe
  93. {
  94. struct target_siginfo info;
  95. struct sigframe sig;
  96. };
  97. QEMU_BUILD_BUG_ON(offsetof(struct sigframe, retcode[3])
  98. != SIGFRAME_RC3_OFFSET);
  99. QEMU_BUILD_BUG_ON(offsetof(struct rt_sigframe, sig.retcode[3])
  100. != RT_SIGFRAME_RC3_OFFSET);
  101. static abi_ptr sigreturn_fdpic_tramp;
  102. /*
  103. * Up to 3 words of 'retcode' in the sigframe are code,
  104. * with retcode[3] being used by fdpic for the function descriptor.
  105. * This code is not actually executed, but is retained for ABI compat.
  106. *
  107. * We will create a table of 8 retcode variants in the sigtramp page.
  108. * Let each table entry use 3 words.
  109. */
  110. #define RETCODE_WORDS 3
  111. #define RETCODE_BYTES (RETCODE_WORDS * 4)
  112. static inline int valid_user_regs(CPUARMState *regs)
  113. {
  114. return 1;
  115. }
  116. static void
  117. setup_sigcontext(struct target_sigcontext *sc, /*struct _fpstate *fpstate,*/
  118. CPUARMState *env, abi_ulong mask)
  119. {
  120. __put_user(env->regs[0], &sc->arm_r0);
  121. __put_user(env->regs[1], &sc->arm_r1);
  122. __put_user(env->regs[2], &sc->arm_r2);
  123. __put_user(env->regs[3], &sc->arm_r3);
  124. __put_user(env->regs[4], &sc->arm_r4);
  125. __put_user(env->regs[5], &sc->arm_r5);
  126. __put_user(env->regs[6], &sc->arm_r6);
  127. __put_user(env->regs[7], &sc->arm_r7);
  128. __put_user(env->regs[8], &sc->arm_r8);
  129. __put_user(env->regs[9], &sc->arm_r9);
  130. __put_user(env->regs[10], &sc->arm_r10);
  131. __put_user(env->regs[11], &sc->arm_fp);
  132. __put_user(env->regs[12], &sc->arm_ip);
  133. __put_user(env->regs[13], &sc->arm_sp);
  134. __put_user(env->regs[14], &sc->arm_lr);
  135. __put_user(env->regs[15], &sc->arm_pc);
  136. __put_user(cpsr_read(env), &sc->arm_cpsr);
  137. __put_user(/* current->thread.trap_no */ 0, &sc->trap_no);
  138. __put_user(/* current->thread.error_code */ 0, &sc->error_code);
  139. __put_user(/* current->thread.address */ 0, &sc->fault_address);
  140. __put_user(mask, &sc->oldmask);
  141. }
  142. static inline abi_ulong
  143. get_sigframe(struct target_sigaction *ka, CPUARMState *regs, int framesize)
  144. {
  145. unsigned long sp;
  146. sp = target_sigsp(get_sp_from_cpustate(regs), ka);
  147. /*
  148. * ATPCS B01 mandates 8-byte alignment
  149. */
  150. return (sp - framesize) & ~7;
  151. }
  152. static void write_arm_sigreturn(uint32_t *rc, int syscall);
  153. static void write_arm_fdpic_sigreturn(uint32_t *rc, int ofs);
  154. static int
  155. setup_return(CPUARMState *env, struct target_sigaction *ka, int usig,
  156. struct sigframe *frame, abi_ulong sp_addr)
  157. {
  158. abi_ulong handler = 0;
  159. abi_ulong handler_fdpic_GOT = 0;
  160. abi_ulong retcode;
  161. bool is_fdpic = info_is_fdpic(get_task_state(thread_cpu)->info);
  162. bool is_rt = ka->sa_flags & TARGET_SA_SIGINFO;
  163. bool thumb;
  164. if (is_fdpic) {
  165. /* In FDPIC mode, ka->_sa_handler points to a function
  166. * descriptor (FD). The first word contains the address of the
  167. * handler. The second word contains the value of the PIC
  168. * register (r9). */
  169. abi_ulong funcdesc_ptr = ka->_sa_handler;
  170. if (get_user_ual(handler, funcdesc_ptr)
  171. || get_user_ual(handler_fdpic_GOT, funcdesc_ptr + 4)) {
  172. return 1;
  173. }
  174. } else {
  175. handler = ka->_sa_handler;
  176. }
  177. thumb = handler & 1;
  178. uint32_t cpsr = cpsr_read(env);
  179. cpsr &= ~CPSR_IT;
  180. if (thumb) {
  181. cpsr |= CPSR_T;
  182. } else {
  183. cpsr &= ~CPSR_T;
  184. }
  185. if (env->cp15.sctlr_el[1] & SCTLR_E0E) {
  186. cpsr |= CPSR_E;
  187. } else {
  188. cpsr &= ~CPSR_E;
  189. }
  190. /* Our vdso default_sigreturn label is a table of entry points. */
  191. retcode = default_sigreturn + (is_fdpic * 2 + is_rt) * 8;
  192. /*
  193. * Put the sigreturn code on the stack no matter which return
  194. * mechanism we use in order to remain ABI compliant.
  195. * Because this is about ABI, always use the A32 instructions,
  196. * despite the fact that our actual vdso trampoline is T16.
  197. */
  198. if (is_fdpic) {
  199. write_arm_fdpic_sigreturn(frame->retcode,
  200. is_rt ? RT_SIGFRAME_RC3_OFFSET
  201. : SIGFRAME_RC3_OFFSET);
  202. } else {
  203. write_arm_sigreturn(frame->retcode,
  204. is_rt ? TARGET_NR_rt_sigreturn
  205. : TARGET_NR_sigreturn);
  206. }
  207. if (ka->sa_flags & TARGET_SA_RESTORER) {
  208. if (is_fdpic) {
  209. /* Place the function descriptor in slot 3. */
  210. __put_user((abi_ulong)ka->sa_restorer, &frame->retcode[3]);
  211. } else {
  212. retcode = ka->sa_restorer;
  213. }
  214. }
  215. env->regs[0] = usig;
  216. if (is_fdpic) {
  217. env->regs[9] = handler_fdpic_GOT;
  218. }
  219. env->regs[13] = sp_addr;
  220. env->regs[14] = retcode;
  221. env->regs[15] = handler & (thumb ? ~1 : ~3);
  222. cpsr_write(env, cpsr, CPSR_IT | CPSR_T | CPSR_E, CPSRWriteByInstr);
  223. return 0;
  224. }
  225. static abi_ulong *setup_sigframe_vfp(abi_ulong *regspace, CPUARMState *env)
  226. {
  227. int i;
  228. struct target_vfp_sigframe *vfpframe;
  229. vfpframe = (struct target_vfp_sigframe *)regspace;
  230. __put_user(TARGET_VFP_MAGIC, &vfpframe->magic);
  231. __put_user(sizeof(*vfpframe), &vfpframe->size);
  232. for (i = 0; i < 32; i++) {
  233. __put_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]);
  234. }
  235. __put_user(vfp_get_fpscr(env), &vfpframe->ufp.fpscr);
  236. __put_user(env->vfp.xregs[ARM_VFP_FPEXC], &vfpframe->ufp_exc.fpexc);
  237. __put_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
  238. __put_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
  239. return (abi_ulong*)(vfpframe+1);
  240. }
  241. static abi_ulong *setup_sigframe_iwmmxt(abi_ulong *regspace, CPUARMState *env)
  242. {
  243. int i;
  244. struct target_iwmmxt_sigframe *iwmmxtframe;
  245. iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
  246. __put_user(TARGET_IWMMXT_MAGIC, &iwmmxtframe->magic);
  247. __put_user(sizeof(*iwmmxtframe), &iwmmxtframe->size);
  248. for (i = 0; i < 16; i++) {
  249. __put_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
  250. }
  251. __put_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
  252. __put_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
  253. __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
  254. __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
  255. __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
  256. __put_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
  257. return (abi_ulong*)(iwmmxtframe+1);
  258. }
  259. static void setup_sigframe(struct target_ucontext *uc,
  260. target_sigset_t *set, CPUARMState *env)
  261. {
  262. struct target_sigaltstack stack;
  263. int i;
  264. abi_ulong *regspace;
  265. /* Clear all the bits of the ucontext we don't use. */
  266. memset(uc, 0, offsetof(struct target_ucontext, tuc_mcontext));
  267. memset(&stack, 0, sizeof(stack));
  268. target_save_altstack(&stack, env);
  269. memcpy(&uc->tuc_stack, &stack, sizeof(stack));
  270. setup_sigcontext(&uc->tuc_mcontext, env, set->sig[0]);
  271. /* Save coprocessor signal frame. */
  272. regspace = uc->tuc_regspace;
  273. if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
  274. regspace = setup_sigframe_vfp(regspace, env);
  275. }
  276. if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
  277. regspace = setup_sigframe_iwmmxt(regspace, env);
  278. }
  279. /* Write terminating magic word */
  280. __put_user(0, regspace);
  281. for(i = 0; i < TARGET_NSIG_WORDS; i++) {
  282. __put_user(set->sig[i], &uc->tuc_sigmask.sig[i]);
  283. }
  284. }
  285. void setup_frame(int usig, struct target_sigaction *ka,
  286. target_sigset_t *set, CPUARMState *regs)
  287. {
  288. struct sigframe *frame;
  289. abi_ulong frame_addr = get_sigframe(ka, regs, sizeof(*frame));
  290. trace_user_setup_frame(regs, frame_addr);
  291. if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
  292. goto sigsegv;
  293. }
  294. setup_sigframe(&frame->uc, set, regs);
  295. if (setup_return(regs, ka, usig, frame, frame_addr)) {
  296. goto sigsegv;
  297. }
  298. unlock_user_struct(frame, frame_addr, 1);
  299. return;
  300. sigsegv:
  301. unlock_user_struct(frame, frame_addr, 1);
  302. force_sigsegv(usig);
  303. }
  304. void setup_rt_frame(int usig, struct target_sigaction *ka,
  305. target_siginfo_t *info,
  306. target_sigset_t *set, CPUARMState *env)
  307. {
  308. struct rt_sigframe *frame;
  309. abi_ulong frame_addr = get_sigframe(ka, env, sizeof(*frame));
  310. abi_ulong info_addr, uc_addr;
  311. trace_user_setup_rt_frame(env, frame_addr);
  312. if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
  313. goto sigsegv;
  314. }
  315. info_addr = frame_addr + offsetof(struct rt_sigframe, info);
  316. uc_addr = frame_addr + offsetof(struct rt_sigframe, sig.uc);
  317. frame->info = *info;
  318. setup_sigframe(&frame->sig.uc, set, env);
  319. if (setup_return(env, ka, usig, &frame->sig, frame_addr)) {
  320. goto sigsegv;
  321. }
  322. env->regs[1] = info_addr;
  323. env->regs[2] = uc_addr;
  324. unlock_user_struct(frame, frame_addr, 1);
  325. return;
  326. sigsegv:
  327. unlock_user_struct(frame, frame_addr, 1);
  328. force_sigsegv(usig);
  329. }
  330. static int
  331. restore_sigcontext(CPUARMState *env, struct target_sigcontext *sc)
  332. {
  333. int err = 0;
  334. uint32_t cpsr;
  335. __get_user(env->regs[0], &sc->arm_r0);
  336. __get_user(env->regs[1], &sc->arm_r1);
  337. __get_user(env->regs[2], &sc->arm_r2);
  338. __get_user(env->regs[3], &sc->arm_r3);
  339. __get_user(env->regs[4], &sc->arm_r4);
  340. __get_user(env->regs[5], &sc->arm_r5);
  341. __get_user(env->regs[6], &sc->arm_r6);
  342. __get_user(env->regs[7], &sc->arm_r7);
  343. __get_user(env->regs[8], &sc->arm_r8);
  344. __get_user(env->regs[9], &sc->arm_r9);
  345. __get_user(env->regs[10], &sc->arm_r10);
  346. __get_user(env->regs[11], &sc->arm_fp);
  347. __get_user(env->regs[12], &sc->arm_ip);
  348. __get_user(env->regs[13], &sc->arm_sp);
  349. __get_user(env->regs[14], &sc->arm_lr);
  350. __get_user(env->regs[15], &sc->arm_pc);
  351. __get_user(cpsr, &sc->arm_cpsr);
  352. cpsr_write(env, cpsr, CPSR_USER | CPSR_EXEC, CPSRWriteByInstr);
  353. err |= !valid_user_regs(env);
  354. return err;
  355. }
  356. static abi_ulong *restore_sigframe_vfp(CPUARMState *env, abi_ulong *regspace)
  357. {
  358. int i;
  359. abi_ulong magic, sz;
  360. uint32_t fpscr, fpexc;
  361. struct target_vfp_sigframe *vfpframe;
  362. vfpframe = (struct target_vfp_sigframe *)regspace;
  363. __get_user(magic, &vfpframe->magic);
  364. __get_user(sz, &vfpframe->size);
  365. if (magic != TARGET_VFP_MAGIC || sz != sizeof(*vfpframe)) {
  366. return 0;
  367. }
  368. for (i = 0; i < 32; i++) {
  369. __get_user(*aa32_vfp_dreg(env, i), &vfpframe->ufp.fpregs[i]);
  370. }
  371. __get_user(fpscr, &vfpframe->ufp.fpscr);
  372. vfp_set_fpscr(env, fpscr);
  373. __get_user(fpexc, &vfpframe->ufp_exc.fpexc);
  374. /* Sanitise FPEXC: ensure VFP is enabled, FPINST2 is invalid
  375. * and the exception flag is cleared
  376. */
  377. fpexc |= (1 << 30);
  378. fpexc &= ~((1 << 31) | (1 << 28));
  379. env->vfp.xregs[ARM_VFP_FPEXC] = fpexc;
  380. __get_user(env->vfp.xregs[ARM_VFP_FPINST], &vfpframe->ufp_exc.fpinst);
  381. __get_user(env->vfp.xregs[ARM_VFP_FPINST2], &vfpframe->ufp_exc.fpinst2);
  382. return (abi_ulong*)(vfpframe + 1);
  383. }
  384. static abi_ulong *restore_sigframe_iwmmxt(CPUARMState *env,
  385. abi_ulong *regspace)
  386. {
  387. int i;
  388. abi_ulong magic, sz;
  389. struct target_iwmmxt_sigframe *iwmmxtframe;
  390. iwmmxtframe = (struct target_iwmmxt_sigframe *)regspace;
  391. __get_user(magic, &iwmmxtframe->magic);
  392. __get_user(sz, &iwmmxtframe->size);
  393. if (magic != TARGET_IWMMXT_MAGIC || sz != sizeof(*iwmmxtframe)) {
  394. return 0;
  395. }
  396. for (i = 0; i < 16; i++) {
  397. __get_user(env->iwmmxt.regs[i], &iwmmxtframe->regs[i]);
  398. }
  399. __get_user(env->vfp.xregs[ARM_IWMMXT_wCSSF], &iwmmxtframe->wcssf);
  400. __get_user(env->vfp.xregs[ARM_IWMMXT_wCASF], &iwmmxtframe->wcssf);
  401. __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR0], &iwmmxtframe->wcgr0);
  402. __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR1], &iwmmxtframe->wcgr1);
  403. __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR2], &iwmmxtframe->wcgr2);
  404. __get_user(env->vfp.xregs[ARM_IWMMXT_wCGR3], &iwmmxtframe->wcgr3);
  405. return (abi_ulong*)(iwmmxtframe + 1);
  406. }
  407. static int do_sigframe_return(CPUARMState *env,
  408. target_ulong context_addr,
  409. struct target_ucontext *uc)
  410. {
  411. sigset_t host_set;
  412. abi_ulong *regspace;
  413. target_to_host_sigset(&host_set, &uc->tuc_sigmask);
  414. set_sigmask(&host_set);
  415. if (restore_sigcontext(env, &uc->tuc_mcontext)) {
  416. return 1;
  417. }
  418. /* Restore coprocessor signal frame */
  419. regspace = uc->tuc_regspace;
  420. if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) {
  421. regspace = restore_sigframe_vfp(env, regspace);
  422. if (!regspace) {
  423. return 1;
  424. }
  425. }
  426. if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
  427. regspace = restore_sigframe_iwmmxt(env, regspace);
  428. if (!regspace) {
  429. return 1;
  430. }
  431. }
  432. target_restore_altstack(&uc->tuc_stack, env);
  433. #if 0
  434. /* Send SIGTRAP if we're single-stepping */
  435. if (ptrace_cancel_bpt(current))
  436. send_sig(SIGTRAP, current, 1);
  437. #endif
  438. return 0;
  439. }
  440. long do_sigreturn(CPUARMState *env)
  441. {
  442. abi_ulong frame_addr;
  443. struct sigframe *frame = NULL;
  444. /*
  445. * Since we stacked the signal on a 64-bit boundary,
  446. * then 'sp' should be word aligned here. If it's
  447. * not, then the user is trying to mess with us.
  448. */
  449. frame_addr = env->regs[13];
  450. trace_user_do_sigreturn(env, frame_addr);
  451. if (frame_addr & 7) {
  452. goto badframe;
  453. }
  454. if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
  455. goto badframe;
  456. }
  457. if (do_sigframe_return(env,
  458. frame_addr + offsetof(struct sigframe, uc),
  459. &frame->uc)) {
  460. goto badframe;
  461. }
  462. unlock_user_struct(frame, frame_addr, 0);
  463. return -QEMU_ESIGRETURN;
  464. badframe:
  465. unlock_user_struct(frame, frame_addr, 0);
  466. force_sig(TARGET_SIGSEGV);
  467. return -QEMU_ESIGRETURN;
  468. }
  469. long do_rt_sigreturn(CPUARMState *env)
  470. {
  471. abi_ulong frame_addr;
  472. struct rt_sigframe *frame = NULL;
  473. /*
  474. * Since we stacked the signal on a 64-bit boundary,
  475. * then 'sp' should be word aligned here. If it's
  476. * not, then the user is trying to mess with us.
  477. */
  478. frame_addr = env->regs[13];
  479. trace_user_do_rt_sigreturn(env, frame_addr);
  480. if (frame_addr & 7) {
  481. goto badframe;
  482. }
  483. if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
  484. goto badframe;
  485. }
  486. if (do_sigframe_return(env,
  487. frame_addr + offsetof(struct rt_sigframe, sig.uc),
  488. &frame->sig.uc)) {
  489. goto badframe;
  490. }
  491. unlock_user_struct(frame, frame_addr, 0);
  492. return -QEMU_ESIGRETURN;
  493. badframe:
  494. unlock_user_struct(frame, frame_addr, 0);
  495. force_sig(TARGET_SIGSEGV);
  496. return -QEMU_ESIGRETURN;
  497. }
  498. /*
  499. * EABI syscalls pass the number via r7.
  500. * Note that the kernel still adds the OABI syscall number to the trap,
  501. * presumably for backward ABI compatibility with unwinders.
  502. */
  503. #define ARM_MOV_R7_IMM(X) (0xe3a07000 | (X))
  504. #define ARM_SWI_SYS(X) (0xef000000 | (X) | ARM_SYSCALL_BASE)
  505. #define THUMB_MOVS_R7_IMM(X) (0x2700 | (X))
  506. #define THUMB_SWI_SYS 0xdf00
  507. static void write_arm_sigreturn(uint32_t *rc, int syscall)
  508. {
  509. __put_user(ARM_MOV_R7_IMM(syscall), rc);
  510. __put_user(ARM_SWI_SYS(syscall), rc + 1);
  511. /* Wrote 8 of 12 bytes */
  512. }
  513. static void write_thm_sigreturn(uint32_t *rc, int syscall)
  514. {
  515. __put_user(THUMB_SWI_SYS << 16 | THUMB_MOVS_R7_IMM(syscall), rc);
  516. /* Wrote 4 of 12 bytes */
  517. }
  518. /*
  519. * Stub needed to make sure the FD register (r9) contains the right value.
  520. * Use the same instruction sequence as the kernel.
  521. */
  522. static void write_arm_fdpic_sigreturn(uint32_t *rc, int ofs)
  523. {
  524. assert(ofs <= 0xfff);
  525. __put_user(0xe59d3000 | ofs, rc + 0); /* ldr r3, [sp, #ofs] */
  526. __put_user(0xe8930908, rc + 1); /* ldm r3, { r3, r9 } */
  527. __put_user(0xe12fff13, rc + 2); /* bx r3 */
  528. /* Wrote 12 of 12 bytes */
  529. }
  530. static void write_thm_fdpic_sigreturn(void *vrc, int ofs)
  531. {
  532. uint16_t *rc = vrc;
  533. assert((ofs & ~0x3fc) == 0);
  534. __put_user(0x9b00 | (ofs >> 2), rc + 0); /* ldr r3, [sp, #ofs] */
  535. __put_user(0xcb0c, rc + 1); /* ldm r3, { r2, r3 } */
  536. __put_user(0x4699, rc + 2); /* mov r9, r3 */
  537. __put_user(0x4710, rc + 3); /* bx r2 */
  538. /* Wrote 8 of 12 bytes */
  539. }
  540. void setup_sigtramp(abi_ulong sigtramp_page)
  541. {
  542. uint32_t total_size = 8 * RETCODE_BYTES;
  543. uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, total_size, 0);
  544. assert(tramp != NULL);
  545. default_sigreturn = sigtramp_page;
  546. write_arm_sigreturn(&tramp[0 * RETCODE_WORDS], TARGET_NR_sigreturn);
  547. write_thm_sigreturn(&tramp[1 * RETCODE_WORDS], TARGET_NR_sigreturn);
  548. write_arm_sigreturn(&tramp[2 * RETCODE_WORDS], TARGET_NR_rt_sigreturn);
  549. write_thm_sigreturn(&tramp[3 * RETCODE_WORDS], TARGET_NR_rt_sigreturn);
  550. sigreturn_fdpic_tramp = sigtramp_page + 4 * RETCODE_BYTES;
  551. write_arm_fdpic_sigreturn(tramp + 4 * RETCODE_WORDS,
  552. offsetof(struct sigframe, retcode[3]));
  553. write_thm_fdpic_sigreturn(tramp + 5 * RETCODE_WORDS,
  554. offsetof(struct sigframe, retcode[3]));
  555. write_arm_fdpic_sigreturn(tramp + 6 * RETCODE_WORDS,
  556. offsetof(struct rt_sigframe, sig.retcode[3]));
  557. write_thm_fdpic_sigreturn(tramp + 7 * RETCODE_WORDS,
  558. offsetof(struct rt_sigframe, sig.retcode[3]));
  559. unlock_user(tramp, sigtramp_page, total_size);
  560. }