signal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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. /* A Sparc register window */
  25. struct target_reg_window {
  26. abi_ulong locals[8];
  27. abi_ulong ins[8];
  28. };
  29. /* A Sparc stack frame. */
  30. struct target_stackf {
  31. /*
  32. * Since qemu does not reference fp or callers_pc directly,
  33. * it's simpler to treat fp and callers_pc as elements of ins[],
  34. * and then bundle locals[] and ins[] into reg_window.
  35. */
  36. struct target_reg_window win;
  37. /*
  38. * Similarly, bundle structptr and xxargs into xargs[].
  39. * This portion of the struct is part of the function call abi,
  40. * and belongs to the callee for spilling argument registers.
  41. */
  42. abi_ulong xargs[8];
  43. };
  44. struct target_siginfo_fpu {
  45. #ifdef TARGET_SPARC64
  46. uint64_t si_double_regs[32];
  47. uint64_t si_fsr;
  48. uint64_t si_gsr;
  49. uint64_t si_fprs;
  50. #else
  51. /* It is more convenient for qemu to move doubles, not singles. */
  52. uint64_t si_double_regs[16];
  53. uint32_t si_fsr;
  54. uint32_t si_fpqdepth;
  55. struct {
  56. uint32_t insn_addr;
  57. uint32_t insn;
  58. } si_fpqueue [16];
  59. #endif
  60. };
  61. #ifdef TARGET_ARCH_HAS_SETUP_FRAME
  62. struct target_signal_frame {
  63. struct target_stackf ss;
  64. struct target_pt_regs regs;
  65. uint32_t si_mask;
  66. abi_ulong fpu_save;
  67. uint32_t insns[2] QEMU_ALIGNED(8);
  68. abi_ulong extramask[TARGET_NSIG_WORDS - 1];
  69. abi_ulong extra_size; /* Should be 0 */
  70. abi_ulong rwin_save;
  71. };
  72. #endif
  73. struct target_rt_signal_frame {
  74. struct target_stackf ss;
  75. target_siginfo_t info;
  76. struct target_pt_regs regs;
  77. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  78. abi_ulong fpu_save;
  79. target_stack_t stack;
  80. target_sigset_t mask;
  81. #else
  82. target_sigset_t mask;
  83. abi_ulong fpu_save;
  84. uint32_t insns[2];
  85. target_stack_t stack;
  86. abi_ulong extra_size; /* Should be 0 */
  87. #endif
  88. abi_ulong rwin_save;
  89. };
  90. static abi_ulong get_sigframe(struct target_sigaction *sa,
  91. CPUSPARCState *env,
  92. size_t framesize)
  93. {
  94. abi_ulong sp = get_sp_from_cpustate(env);
  95. /*
  96. * If we are on the alternate signal stack and would overflow it, don't.
  97. * Return an always-bogus address instead so we will die with SIGSEGV.
  98. */
  99. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize))) {
  100. return -1;
  101. }
  102. /* This is the X/Open sanctioned signal stack switching. */
  103. sp = target_sigsp(sp, sa) - framesize;
  104. /*
  105. * Always align the stack frame. This handles two cases. First,
  106. * sigaltstack need not be mindful of platform specific stack
  107. * alignment. Second, if we took this signal because the stack
  108. * is not aligned properly, we'd like to take the signal cleanly
  109. * and report that.
  110. */
  111. sp &= ~15UL;
  112. return sp;
  113. }
  114. static void save_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
  115. {
  116. int i;
  117. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  118. __put_user(sparc64_tstate(env), &regs->tstate);
  119. /* TODO: magic should contain PT_REG_MAGIC + %tt. */
  120. __put_user(0, &regs->magic);
  121. #else
  122. __put_user(cpu_get_psr(env), &regs->psr);
  123. #endif
  124. __put_user(env->pc, &regs->pc);
  125. __put_user(env->npc, &regs->npc);
  126. __put_user(env->y, &regs->y);
  127. for (i = 0; i < 8; i++) {
  128. __put_user(env->gregs[i], &regs->u_regs[i]);
  129. }
  130. for (i = 0; i < 8; i++) {
  131. __put_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
  132. }
  133. }
  134. static void restore_pt_regs(struct target_pt_regs *regs, CPUSPARCState *env)
  135. {
  136. int i;
  137. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  138. /* User can only change condition codes and %asi in %tstate. */
  139. uint64_t tstate;
  140. __get_user(tstate, &regs->tstate);
  141. cpu_put_ccr(env, tstate >> 32);
  142. env->asi = extract64(tstate, 24, 8);
  143. #else
  144. /*
  145. * User can only change condition codes and FPU enabling in %psr.
  146. * But don't bother with FPU enabling, since a real kernel would
  147. * just re-enable the FPU upon the next fpu trap.
  148. */
  149. uint32_t psr;
  150. __get_user(psr, &regs->psr);
  151. env->psr = (psr & PSR_ICC) | (env->psr & ~PSR_ICC);
  152. #endif
  153. /* Note that pc and npc are handled in the caller. */
  154. __get_user(env->y, &regs->y);
  155. for (i = 0; i < 8; i++) {
  156. __get_user(env->gregs[i], &regs->u_regs[i]);
  157. }
  158. for (i = 0; i < 8; i++) {
  159. __get_user(env->regwptr[WREG_O0 + i], &regs->u_regs[i + 8]);
  160. }
  161. }
  162. static void save_reg_win(struct target_reg_window *win, CPUSPARCState *env)
  163. {
  164. int i;
  165. for (i = 0; i < 8; i++) {
  166. __put_user(env->regwptr[i + WREG_L0], &win->locals[i]);
  167. }
  168. for (i = 0; i < 8; i++) {
  169. __put_user(env->regwptr[i + WREG_I0], &win->ins[i]);
  170. }
  171. }
  172. static void save_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
  173. {
  174. int i;
  175. #ifdef TARGET_SPARC64
  176. for (i = 0; i < 32; ++i) {
  177. __put_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
  178. }
  179. __put_user(env->fsr, &fpu->si_fsr);
  180. __put_user(env->gsr, &fpu->si_gsr);
  181. __put_user(env->fprs, &fpu->si_fprs);
  182. #else
  183. for (i = 0; i < 16; ++i) {
  184. __put_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
  185. }
  186. __put_user(env->fsr, &fpu->si_fsr);
  187. __put_user(0, &fpu->si_fpqdepth);
  188. #endif
  189. }
  190. static void restore_fpu(struct target_siginfo_fpu *fpu, CPUSPARCState *env)
  191. {
  192. int i;
  193. #ifdef TARGET_SPARC64
  194. uint64_t fprs;
  195. __get_user(fprs, &fpu->si_fprs);
  196. /* In case the user mucks about with FPRS, restore as directed. */
  197. if (fprs & FPRS_DL) {
  198. for (i = 0; i < 16; ++i) {
  199. __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
  200. }
  201. }
  202. if (fprs & FPRS_DU) {
  203. for (i = 16; i < 32; ++i) {
  204. __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
  205. }
  206. }
  207. __get_user(env->fsr, &fpu->si_fsr);
  208. __get_user(env->gsr, &fpu->si_gsr);
  209. env->fprs |= fprs;
  210. #else
  211. for (i = 0; i < 16; ++i) {
  212. __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
  213. }
  214. __get_user(env->fsr, &fpu->si_fsr);
  215. #endif
  216. }
  217. #ifdef TARGET_ARCH_HAS_SETUP_FRAME
  218. static void install_sigtramp(uint32_t *tramp, int syscall)
  219. {
  220. __put_user(0x82102000u + syscall, &tramp[0]); /* mov syscall, %g1 */
  221. __put_user(0x91d02010u, &tramp[1]); /* t 0x10 */
  222. }
  223. void setup_frame(int sig, struct target_sigaction *ka,
  224. target_sigset_t *set, CPUSPARCState *env)
  225. {
  226. abi_ulong sf_addr;
  227. struct target_signal_frame *sf;
  228. size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
  229. int i;
  230. sf_addr = get_sigframe(ka, env, sf_size);
  231. trace_user_setup_frame(env, sf_addr);
  232. sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
  233. if (!sf) {
  234. force_sigsegv(sig);
  235. return;
  236. }
  237. /* 2. Save the current process state */
  238. save_pt_regs(&sf->regs, env);
  239. __put_user(0, &sf->extra_size);
  240. save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
  241. __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
  242. __put_user(0, &sf->rwin_save); /* TODO: save_rwin_state */
  243. __put_user(set->sig[0], &sf->si_mask);
  244. for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
  245. __put_user(set->sig[i + 1], &sf->extramask[i]);
  246. }
  247. save_reg_win(&sf->ss.win, env);
  248. /* 3. signal handler back-trampoline and parameters */
  249. env->regwptr[WREG_SP] = sf_addr;
  250. env->regwptr[WREG_O0] = sig;
  251. env->regwptr[WREG_O1] = sf_addr +
  252. offsetof(struct target_signal_frame, regs);
  253. env->regwptr[WREG_O2] = sf_addr +
  254. offsetof(struct target_signal_frame, regs);
  255. /* 4. signal handler */
  256. env->pc = ka->_sa_handler;
  257. env->npc = env->pc + 4;
  258. /* 5. return to kernel instructions */
  259. if (ka->ka_restorer) {
  260. env->regwptr[WREG_O7] = ka->ka_restorer;
  261. } else {
  262. /* Not used, but retain for ABI compatibility. */
  263. install_sigtramp(sf->insns, TARGET_NR_sigreturn);
  264. env->regwptr[WREG_O7] = default_sigreturn;
  265. }
  266. unlock_user(sf, sf_addr, sf_size);
  267. }
  268. #endif /* TARGET_ARCH_HAS_SETUP_FRAME */
  269. void setup_rt_frame(int sig, struct target_sigaction *ka,
  270. target_siginfo_t *info,
  271. target_sigset_t *set, CPUSPARCState *env)
  272. {
  273. abi_ulong sf_addr;
  274. struct target_rt_signal_frame *sf;
  275. size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
  276. sf_addr = get_sigframe(ka, env, sf_size);
  277. trace_user_setup_rt_frame(env, sf_addr);
  278. sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
  279. if (!sf) {
  280. force_sigsegv(sig);
  281. return;
  282. }
  283. /* 2. Save the current process state */
  284. save_reg_win(&sf->ss.win, env);
  285. save_pt_regs(&sf->regs, env);
  286. save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
  287. __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
  288. __put_user(0, &sf->rwin_save); /* TODO: save_rwin_state */
  289. tswap_siginfo(&sf->info, info);
  290. tswap_sigset(&sf->mask, set);
  291. target_save_altstack(&sf->stack, env);
  292. #ifdef TARGET_ABI32
  293. __put_user(0, &sf->extra_size);
  294. #endif
  295. /* 3. signal handler back-trampoline and parameters */
  296. env->regwptr[WREG_SP] = sf_addr - TARGET_STACK_BIAS;
  297. env->regwptr[WREG_O0] = sig;
  298. env->regwptr[WREG_O1] =
  299. sf_addr + offsetof(struct target_rt_signal_frame, info);
  300. #ifdef TARGET_ABI32
  301. env->regwptr[WREG_O2] =
  302. sf_addr + offsetof(struct target_rt_signal_frame, regs);
  303. #else
  304. env->regwptr[WREG_O2] = env->regwptr[WREG_O1];
  305. #endif
  306. /* 4. signal handler */
  307. env->pc = ka->_sa_handler;
  308. env->npc = env->pc + 4;
  309. /* 5. return to kernel instructions */
  310. #ifdef TARGET_ABI32
  311. if (ka->ka_restorer) {
  312. env->regwptr[WREG_O7] = ka->ka_restorer;
  313. } else {
  314. /* Not used, but retain for ABI compatibility. */
  315. install_sigtramp(sf->insns, TARGET_NR_rt_sigreturn);
  316. env->regwptr[WREG_O7] = default_rt_sigreturn;
  317. }
  318. #else
  319. env->regwptr[WREG_O7] = ka->ka_restorer;
  320. #endif
  321. unlock_user(sf, sf_addr, sf_size);
  322. }
  323. long do_sigreturn(CPUSPARCState *env)
  324. {
  325. #ifdef TARGET_ARCH_HAS_SETUP_FRAME
  326. abi_ulong sf_addr;
  327. struct target_signal_frame *sf = NULL;
  328. abi_ulong pc, npc, ptr;
  329. target_sigset_t set;
  330. sigset_t host_set;
  331. int i;
  332. sf_addr = env->regwptr[WREG_SP];
  333. trace_user_do_sigreturn(env, sf_addr);
  334. /* 1. Make sure we are not getting garbage from the user */
  335. if ((sf_addr & 15) || !lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
  336. goto segv_and_exit;
  337. }
  338. /* Make sure stack pointer is aligned. */
  339. __get_user(ptr, &sf->regs.u_regs[14]);
  340. if (ptr & 7) {
  341. goto segv_and_exit;
  342. }
  343. /* Make sure instruction pointers are aligned. */
  344. __get_user(pc, &sf->regs.pc);
  345. __get_user(npc, &sf->regs.npc);
  346. if ((pc | npc) & 3) {
  347. goto segv_and_exit;
  348. }
  349. /* 2. Restore the state */
  350. restore_pt_regs(&sf->regs, env);
  351. env->pc = pc;
  352. env->npc = npc;
  353. __get_user(ptr, &sf->fpu_save);
  354. if (ptr) {
  355. struct target_siginfo_fpu *fpu;
  356. if ((ptr & 3) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
  357. goto segv_and_exit;
  358. }
  359. restore_fpu(fpu, env);
  360. unlock_user_struct(fpu, ptr, 0);
  361. }
  362. __get_user(ptr, &sf->rwin_save);
  363. if (ptr) {
  364. goto segv_and_exit; /* TODO: restore_rwin */
  365. }
  366. __get_user(set.sig[0], &sf->si_mask);
  367. for (i = 1; i < TARGET_NSIG_WORDS; i++) {
  368. __get_user(set.sig[i], &sf->extramask[i - 1]);
  369. }
  370. target_to_host_sigset_internal(&host_set, &set);
  371. set_sigmask(&host_set);
  372. unlock_user_struct(sf, sf_addr, 0);
  373. return -QEMU_ESIGRETURN;
  374. segv_and_exit:
  375. unlock_user_struct(sf, sf_addr, 0);
  376. force_sig(TARGET_SIGSEGV);
  377. return -QEMU_ESIGRETURN;
  378. #else
  379. return -TARGET_ENOSYS;
  380. #endif
  381. }
  382. long do_rt_sigreturn(CPUSPARCState *env)
  383. {
  384. abi_ulong sf_addr, tpc, tnpc, ptr;
  385. struct target_rt_signal_frame *sf = NULL;
  386. sigset_t set;
  387. sf_addr = get_sp_from_cpustate(env);
  388. trace_user_do_rt_sigreturn(env, sf_addr);
  389. /* 1. Make sure we are not getting garbage from the user */
  390. if ((sf_addr & 15) || !lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
  391. goto segv_and_exit;
  392. }
  393. /* Validate SP alignment. */
  394. __get_user(ptr, &sf->regs.u_regs[8 + WREG_SP]);
  395. if ((ptr + TARGET_STACK_BIAS) & 7) {
  396. goto segv_and_exit;
  397. }
  398. /* Validate PC and NPC alignment. */
  399. __get_user(tpc, &sf->regs.pc);
  400. __get_user(tnpc, &sf->regs.npc);
  401. if ((tpc | tnpc) & 3) {
  402. goto segv_and_exit;
  403. }
  404. /* 2. Restore the state */
  405. restore_pt_regs(&sf->regs, env);
  406. __get_user(ptr, &sf->fpu_save);
  407. if (ptr) {
  408. struct target_siginfo_fpu *fpu;
  409. if ((ptr & 7) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
  410. goto segv_and_exit;
  411. }
  412. restore_fpu(fpu, env);
  413. unlock_user_struct(fpu, ptr, 0);
  414. }
  415. __get_user(ptr, &sf->rwin_save);
  416. if (ptr) {
  417. goto segv_and_exit; /* TODO: restore_rwin_state */
  418. }
  419. target_restore_altstack(&sf->stack, env);
  420. target_to_host_sigset(&set, &sf->mask);
  421. set_sigmask(&set);
  422. env->pc = tpc;
  423. env->npc = tnpc;
  424. unlock_user_struct(sf, sf_addr, 0);
  425. return -QEMU_ESIGRETURN;
  426. segv_and_exit:
  427. unlock_user_struct(sf, sf_addr, 0);
  428. force_sig(TARGET_SIGSEGV);
  429. return -QEMU_ESIGRETURN;
  430. }
  431. #ifdef TARGET_ABI32
  432. void setup_sigtramp(abi_ulong sigtramp_page)
  433. {
  434. uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 2 * 8, 0);
  435. assert(tramp != NULL);
  436. default_sigreturn = sigtramp_page;
  437. install_sigtramp(tramp, TARGET_NR_sigreturn);
  438. default_rt_sigreturn = sigtramp_page + 8;
  439. install_sigtramp(tramp + 2, TARGET_NR_rt_sigreturn);
  440. unlock_user(tramp, sigtramp_page, 2 * 8);
  441. }
  442. #endif
  443. #ifdef TARGET_SPARC64
  444. #define SPARC_MC_TSTATE 0
  445. #define SPARC_MC_PC 1
  446. #define SPARC_MC_NPC 2
  447. #define SPARC_MC_Y 3
  448. #define SPARC_MC_G1 4
  449. #define SPARC_MC_G2 5
  450. #define SPARC_MC_G3 6
  451. #define SPARC_MC_G4 7
  452. #define SPARC_MC_G5 8
  453. #define SPARC_MC_G6 9
  454. #define SPARC_MC_G7 10
  455. #define SPARC_MC_O0 11
  456. #define SPARC_MC_O1 12
  457. #define SPARC_MC_O2 13
  458. #define SPARC_MC_O3 14
  459. #define SPARC_MC_O4 15
  460. #define SPARC_MC_O5 16
  461. #define SPARC_MC_O6 17
  462. #define SPARC_MC_O7 18
  463. #define SPARC_MC_NGREG 19
  464. typedef abi_ulong target_mc_greg_t;
  465. typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
  466. struct target_mc_fq {
  467. abi_ulong mcfq_addr;
  468. uint32_t mcfq_insn;
  469. };
  470. /*
  471. * Note the manual 16-alignment; the kernel gets this because it
  472. * includes a "long double qregs[16]" in the mcpu_fregs union,
  473. * which we can't do.
  474. */
  475. struct target_mc_fpu {
  476. union {
  477. uint32_t sregs[32];
  478. uint64_t dregs[32];
  479. //uint128_t qregs[16];
  480. } mcfpu_fregs;
  481. abi_ulong mcfpu_fsr;
  482. abi_ulong mcfpu_fprs;
  483. abi_ulong mcfpu_gsr;
  484. abi_ulong mcfpu_fq;
  485. unsigned char mcfpu_qcnt;
  486. unsigned char mcfpu_qentsz;
  487. unsigned char mcfpu_enab;
  488. } __attribute__((aligned(16)));
  489. typedef struct target_mc_fpu target_mc_fpu_t;
  490. typedef struct {
  491. target_mc_gregset_t mc_gregs;
  492. target_mc_greg_t mc_fp;
  493. target_mc_greg_t mc_i7;
  494. target_mc_fpu_t mc_fpregs;
  495. } target_mcontext_t;
  496. struct target_ucontext {
  497. abi_ulong tuc_link;
  498. abi_ulong tuc_flags;
  499. target_sigset_t tuc_sigmask;
  500. target_mcontext_t tuc_mcontext;
  501. };
  502. /* {set, get}context() needed for 64-bit SparcLinux userland. */
  503. void sparc64_set_context(CPUSPARCState *env)
  504. {
  505. abi_ulong ucp_addr;
  506. struct target_ucontext *ucp;
  507. target_mc_gregset_t *grp;
  508. target_mc_fpu_t *fpup;
  509. target_ulong pc, npc, tstate;
  510. unsigned int i;
  511. unsigned char fenab;
  512. ucp_addr = env->regwptr[WREG_O0];
  513. if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
  514. goto do_sigsegv;
  515. }
  516. grp = &ucp->tuc_mcontext.mc_gregs;
  517. __get_user(pc, &((*grp)[SPARC_MC_PC]));
  518. __get_user(npc, &((*grp)[SPARC_MC_NPC]));
  519. if ((pc | npc) & 3) {
  520. goto do_sigsegv;
  521. }
  522. if (env->regwptr[WREG_O1]) {
  523. target_sigset_t target_set;
  524. sigset_t set;
  525. if (TARGET_NSIG_WORDS == 1) {
  526. __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
  527. } else {
  528. abi_ulong *src, *dst;
  529. src = ucp->tuc_sigmask.sig;
  530. dst = target_set.sig;
  531. for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
  532. __get_user(*dst, src);
  533. }
  534. }
  535. target_to_host_sigset_internal(&set, &target_set);
  536. set_sigmask(&set);
  537. }
  538. env->pc = pc;
  539. env->npc = npc;
  540. __get_user(env->y, &((*grp)[SPARC_MC_Y]));
  541. __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
  542. /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
  543. env->asi = (tstate >> 24) & 0xff;
  544. cpu_put_ccr(env, (tstate >> 32) & 0xff);
  545. __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
  546. __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
  547. __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
  548. __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
  549. __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
  550. __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
  551. /* Skip g7 as that's the thread register in userspace */
  552. /*
  553. * Note that unlike the kernel, we didn't need to mess with the
  554. * guest register window state to save it into a pt_regs to run
  555. * the kernel. So for us the guest's O regs are still in WREG_O*
  556. * (unlike the kernel which has put them in UREG_I* in a pt_regs)
  557. * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
  558. * need to be written back to userspace memory.
  559. */
  560. __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0]));
  561. __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1]));
  562. __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2]));
  563. __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3]));
  564. __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4]));
  565. __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5]));
  566. __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6]));
  567. __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7]));
  568. __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp));
  569. __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7));
  570. fpup = &ucp->tuc_mcontext.mc_fpregs;
  571. __get_user(fenab, &(fpup->mcfpu_enab));
  572. if (fenab) {
  573. abi_ulong fprs;
  574. /*
  575. * We use the FPRS from the guest only in deciding whether
  576. * to restore the upper, lower, or both banks of the FPU regs.
  577. * The kernel here writes the FPU register data into the
  578. * process's current_thread_info state and unconditionally
  579. * clears FPRS and TSTATE_PEF: this disables the FPU so that the
  580. * next FPU-disabled trap will copy the data out of
  581. * current_thread_info and into the real FPU registers.
  582. * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
  583. * so we always load the data directly into the FPU registers
  584. * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
  585. * Note that because we (and the kernel) always write zeroes for
  586. * the fenab and fprs in sparc64_get_context() none of this code
  587. * will execute unless the guest manually constructed or changed
  588. * the context structure.
  589. */
  590. __get_user(fprs, &(fpup->mcfpu_fprs));
  591. if (fprs & FPRS_DL) {
  592. for (i = 0; i < 16; i++) {
  593. __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
  594. }
  595. }
  596. if (fprs & FPRS_DU) {
  597. for (i = 16; i < 32; i++) {
  598. __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
  599. }
  600. }
  601. __get_user(env->fsr, &(fpup->mcfpu_fsr));
  602. __get_user(env->gsr, &(fpup->mcfpu_gsr));
  603. }
  604. unlock_user_struct(ucp, ucp_addr, 0);
  605. return;
  606. do_sigsegv:
  607. unlock_user_struct(ucp, ucp_addr, 0);
  608. force_sig(TARGET_SIGSEGV);
  609. }
  610. void sparc64_get_context(CPUSPARCState *env)
  611. {
  612. abi_ulong ucp_addr;
  613. struct target_ucontext *ucp;
  614. target_mc_gregset_t *grp;
  615. target_mcontext_t *mcp;
  616. int err;
  617. unsigned int i;
  618. target_sigset_t target_set;
  619. sigset_t set;
  620. ucp_addr = env->regwptr[WREG_O0];
  621. if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
  622. goto do_sigsegv;
  623. }
  624. memset(ucp, 0, sizeof(*ucp));
  625. mcp = &ucp->tuc_mcontext;
  626. grp = &mcp->mc_gregs;
  627. /* Skip over the trap instruction, first. */
  628. env->pc = env->npc;
  629. env->npc += 4;
  630. /* If we're only reading the signal mask then do_sigprocmask()
  631. * is guaranteed not to fail, which is important because we don't
  632. * have any way to signal a failure or restart this operation since
  633. * this is not a normal syscall.
  634. */
  635. err = do_sigprocmask(0, NULL, &set);
  636. assert(err == 0);
  637. host_to_target_sigset_internal(&target_set, &set);
  638. if (TARGET_NSIG_WORDS == 1) {
  639. __put_user(target_set.sig[0],
  640. (abi_ulong *)&ucp->tuc_sigmask);
  641. } else {
  642. abi_ulong *src, *dst;
  643. src = target_set.sig;
  644. dst = ucp->tuc_sigmask.sig;
  645. for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
  646. __put_user(*src, dst);
  647. }
  648. }
  649. __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE]));
  650. __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
  651. __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
  652. __put_user(env->y, &((*grp)[SPARC_MC_Y]));
  653. __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
  654. __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
  655. __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
  656. __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
  657. __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
  658. __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
  659. __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
  660. /*
  661. * Note that unlike the kernel, we didn't need to mess with the
  662. * guest register window state to save it into a pt_regs to run
  663. * the kernel. So for us the guest's O regs are still in WREG_O*
  664. * (unlike the kernel which has put them in UREG_I* in a pt_regs)
  665. * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
  666. * need to be fished out of userspace memory.
  667. */
  668. __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0]));
  669. __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1]));
  670. __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2]));
  671. __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3]));
  672. __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4]));
  673. __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5]));
  674. __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6]));
  675. __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7]));
  676. __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp));
  677. __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7));
  678. /*
  679. * We don't write out the FPU state. This matches the kernel's
  680. * implementation (which has the code for doing this but
  681. * hidden behind an "if (fenab)" where fenab is always 0).
  682. */
  683. unlock_user_struct(ucp, ucp_addr, 1);
  684. return;
  685. do_sigsegv:
  686. unlock_user_struct(ucp, ucp_addr, 1);
  687. force_sig(TARGET_SIGSEGV);
  688. }
  689. #endif /* TARGET_SPARC64 */