signal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. cpu_put_psr_icc(env, psr);
  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(cpu_get_fsr(env), &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(cpu_get_fsr(env), &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. target_ulong fsr;
  193. int i;
  194. #ifdef TARGET_SPARC64
  195. uint64_t fprs;
  196. __get_user(fprs, &fpu->si_fprs);
  197. /* In case the user mucks about with FPRS, restore as directed. */
  198. if (fprs & FPRS_DL) {
  199. for (i = 0; i < 16; ++i) {
  200. __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
  201. }
  202. }
  203. if (fprs & FPRS_DU) {
  204. for (i = 16; i < 32; ++i) {
  205. __get_user(env->fpr[i].ll, &fpu->si_double_regs[i]);
  206. }
  207. }
  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. #endif
  215. __get_user(fsr, &fpu->si_fsr);
  216. cpu_put_fsr(env, fsr);
  217. }
  218. #ifdef TARGET_ARCH_HAS_SETUP_FRAME
  219. static void install_sigtramp(uint32_t *tramp, int syscall)
  220. {
  221. __put_user(0x82102000u + syscall, &tramp[0]); /* mov syscall, %g1 */
  222. __put_user(0x91d02010u, &tramp[1]); /* t 0x10 */
  223. }
  224. void setup_frame(int sig, struct target_sigaction *ka,
  225. target_sigset_t *set, CPUSPARCState *env)
  226. {
  227. abi_ulong sf_addr;
  228. struct target_signal_frame *sf;
  229. size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
  230. int i;
  231. sf_addr = get_sigframe(ka, env, sf_size);
  232. trace_user_setup_frame(env, sf_addr);
  233. sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
  234. if (!sf) {
  235. force_sigsegv(sig);
  236. return;
  237. }
  238. /* 2. Save the current process state */
  239. save_pt_regs(&sf->regs, env);
  240. __put_user(0, &sf->extra_size);
  241. save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
  242. __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
  243. __put_user(0, &sf->rwin_save); /* TODO: save_rwin_state */
  244. __put_user(set->sig[0], &sf->si_mask);
  245. for (i = 0; i < TARGET_NSIG_WORDS - 1; i++) {
  246. __put_user(set->sig[i + 1], &sf->extramask[i]);
  247. }
  248. save_reg_win(&sf->ss.win, env);
  249. /* 3. signal handler back-trampoline and parameters */
  250. env->regwptr[WREG_SP] = sf_addr;
  251. env->regwptr[WREG_O0] = sig;
  252. env->regwptr[WREG_O1] = sf_addr +
  253. offsetof(struct target_signal_frame, regs);
  254. env->regwptr[WREG_O2] = sf_addr +
  255. offsetof(struct target_signal_frame, regs);
  256. /* 4. signal handler */
  257. env->pc = ka->_sa_handler;
  258. env->npc = env->pc + 4;
  259. /* 5. return to kernel instructions */
  260. if (ka->ka_restorer) {
  261. env->regwptr[WREG_O7] = ka->ka_restorer;
  262. } else {
  263. /* Not used, but retain for ABI compatibility. */
  264. install_sigtramp(sf->insns, TARGET_NR_sigreturn);
  265. env->regwptr[WREG_O7] = default_sigreturn;
  266. }
  267. unlock_user(sf, sf_addr, sf_size);
  268. }
  269. #endif /* TARGET_ARCH_HAS_SETUP_FRAME */
  270. void setup_rt_frame(int sig, struct target_sigaction *ka,
  271. target_siginfo_t *info,
  272. target_sigset_t *set, CPUSPARCState *env)
  273. {
  274. abi_ulong sf_addr;
  275. struct target_rt_signal_frame *sf;
  276. size_t sf_size = sizeof(*sf) + sizeof(struct target_siginfo_fpu);
  277. sf_addr = get_sigframe(ka, env, sf_size);
  278. trace_user_setup_rt_frame(env, sf_addr);
  279. sf = lock_user(VERIFY_WRITE, sf_addr, sf_size, 0);
  280. if (!sf) {
  281. force_sigsegv(sig);
  282. return;
  283. }
  284. /* 2. Save the current process state */
  285. save_reg_win(&sf->ss.win, env);
  286. save_pt_regs(&sf->regs, env);
  287. save_fpu((struct target_siginfo_fpu *)(sf + 1), env);
  288. __put_user(sf_addr + sizeof(*sf), &sf->fpu_save);
  289. __put_user(0, &sf->rwin_save); /* TODO: save_rwin_state */
  290. sf->info = *info;
  291. tswap_sigset(&sf->mask, set);
  292. target_save_altstack(&sf->stack, env);
  293. #ifdef TARGET_ABI32
  294. __put_user(0, &sf->extra_size);
  295. #endif
  296. /* 3. signal handler back-trampoline and parameters */
  297. env->regwptr[WREG_SP] = sf_addr - TARGET_STACK_BIAS;
  298. env->regwptr[WREG_O0] = sig;
  299. env->regwptr[WREG_O1] =
  300. sf_addr + offsetof(struct target_rt_signal_frame, info);
  301. #ifdef TARGET_ABI32
  302. env->regwptr[WREG_O2] =
  303. sf_addr + offsetof(struct target_rt_signal_frame, regs);
  304. #else
  305. env->regwptr[WREG_O2] = env->regwptr[WREG_O1];
  306. #endif
  307. /* 4. signal handler */
  308. env->pc = ka->_sa_handler;
  309. env->npc = env->pc + 4;
  310. /* 5. return to kernel instructions */
  311. #ifdef TARGET_ABI32
  312. if (ka->ka_restorer) {
  313. env->regwptr[WREG_O7] = ka->ka_restorer;
  314. } else {
  315. /* Not used, but retain for ABI compatibility. */
  316. install_sigtramp(sf->insns, TARGET_NR_rt_sigreturn);
  317. env->regwptr[WREG_O7] = default_rt_sigreturn;
  318. }
  319. #else
  320. env->regwptr[WREG_O7] = ka->ka_restorer;
  321. #endif
  322. unlock_user(sf, sf_addr, sf_size);
  323. }
  324. long do_sigreturn(CPUSPARCState *env)
  325. {
  326. #ifdef TARGET_ARCH_HAS_SETUP_FRAME
  327. abi_ulong sf_addr;
  328. struct target_signal_frame *sf = NULL;
  329. abi_ulong pc, npc, ptr;
  330. target_sigset_t set;
  331. sigset_t host_set;
  332. int i;
  333. sf_addr = env->regwptr[WREG_SP];
  334. trace_user_do_sigreturn(env, sf_addr);
  335. /* 1. Make sure we are not getting garbage from the user */
  336. if ((sf_addr & 15) || !lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
  337. goto segv_and_exit;
  338. }
  339. /* Make sure stack pointer is aligned. */
  340. __get_user(ptr, &sf->regs.u_regs[14]);
  341. if (ptr & 7) {
  342. goto segv_and_exit;
  343. }
  344. /* Make sure instruction pointers are aligned. */
  345. __get_user(pc, &sf->regs.pc);
  346. __get_user(npc, &sf->regs.npc);
  347. if ((pc | npc) & 3) {
  348. goto segv_and_exit;
  349. }
  350. /* 2. Restore the state */
  351. restore_pt_regs(&sf->regs, env);
  352. env->pc = pc;
  353. env->npc = npc;
  354. __get_user(ptr, &sf->fpu_save);
  355. if (ptr) {
  356. struct target_siginfo_fpu *fpu;
  357. if ((ptr & 3) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
  358. goto segv_and_exit;
  359. }
  360. restore_fpu(fpu, env);
  361. unlock_user_struct(fpu, ptr, 0);
  362. }
  363. __get_user(ptr, &sf->rwin_save);
  364. if (ptr) {
  365. goto segv_and_exit; /* TODO: restore_rwin */
  366. }
  367. __get_user(set.sig[0], &sf->si_mask);
  368. for (i = 1; i < TARGET_NSIG_WORDS; i++) {
  369. __get_user(set.sig[i], &sf->extramask[i - 1]);
  370. }
  371. target_to_host_sigset_internal(&host_set, &set);
  372. set_sigmask(&host_set);
  373. unlock_user_struct(sf, sf_addr, 0);
  374. return -QEMU_ESIGRETURN;
  375. segv_and_exit:
  376. unlock_user_struct(sf, sf_addr, 0);
  377. force_sig(TARGET_SIGSEGV);
  378. return -QEMU_ESIGRETURN;
  379. #else
  380. return -TARGET_ENOSYS;
  381. #endif
  382. }
  383. long do_rt_sigreturn(CPUSPARCState *env)
  384. {
  385. abi_ulong sf_addr, tpc, tnpc, ptr;
  386. struct target_rt_signal_frame *sf = NULL;
  387. sigset_t set;
  388. sf_addr = get_sp_from_cpustate(env);
  389. trace_user_do_rt_sigreturn(env, sf_addr);
  390. /* 1. Make sure we are not getting garbage from the user */
  391. if ((sf_addr & 15) || !lock_user_struct(VERIFY_READ, sf, sf_addr, 1)) {
  392. goto segv_and_exit;
  393. }
  394. /* Validate SP alignment. */
  395. __get_user(ptr, &sf->regs.u_regs[8 + WREG_SP]);
  396. if ((ptr + TARGET_STACK_BIAS) & 7) {
  397. goto segv_and_exit;
  398. }
  399. /* Validate PC and NPC alignment. */
  400. __get_user(tpc, &sf->regs.pc);
  401. __get_user(tnpc, &sf->regs.npc);
  402. if ((tpc | tnpc) & 3) {
  403. goto segv_and_exit;
  404. }
  405. /* 2. Restore the state */
  406. restore_pt_regs(&sf->regs, env);
  407. __get_user(ptr, &sf->fpu_save);
  408. if (ptr) {
  409. struct target_siginfo_fpu *fpu;
  410. if ((ptr & 7) || !lock_user_struct(VERIFY_READ, fpu, ptr, 1)) {
  411. goto segv_and_exit;
  412. }
  413. restore_fpu(fpu, env);
  414. unlock_user_struct(fpu, ptr, 0);
  415. }
  416. __get_user(ptr, &sf->rwin_save);
  417. if (ptr) {
  418. goto segv_and_exit; /* TODO: restore_rwin_state */
  419. }
  420. target_restore_altstack(&sf->stack, env);
  421. target_to_host_sigset(&set, &sf->mask);
  422. set_sigmask(&set);
  423. env->pc = tpc;
  424. env->npc = tnpc;
  425. unlock_user_struct(sf, sf_addr, 0);
  426. return -QEMU_ESIGRETURN;
  427. segv_and_exit:
  428. unlock_user_struct(sf, sf_addr, 0);
  429. force_sig(TARGET_SIGSEGV);
  430. return -QEMU_ESIGRETURN;
  431. }
  432. #ifdef TARGET_ABI32
  433. void setup_sigtramp(abi_ulong sigtramp_page)
  434. {
  435. uint32_t *tramp = lock_user(VERIFY_WRITE, sigtramp_page, 2 * 8, 0);
  436. assert(tramp != NULL);
  437. default_sigreturn = sigtramp_page;
  438. install_sigtramp(tramp, TARGET_NR_sigreturn);
  439. default_rt_sigreturn = sigtramp_page + 8;
  440. install_sigtramp(tramp + 2, TARGET_NR_rt_sigreturn);
  441. unlock_user(tramp, sigtramp_page, 2 * 8);
  442. }
  443. #endif
  444. #ifdef TARGET_SPARC64
  445. #define SPARC_MC_TSTATE 0
  446. #define SPARC_MC_PC 1
  447. #define SPARC_MC_NPC 2
  448. #define SPARC_MC_Y 3
  449. #define SPARC_MC_G1 4
  450. #define SPARC_MC_G2 5
  451. #define SPARC_MC_G3 6
  452. #define SPARC_MC_G4 7
  453. #define SPARC_MC_G5 8
  454. #define SPARC_MC_G6 9
  455. #define SPARC_MC_G7 10
  456. #define SPARC_MC_O0 11
  457. #define SPARC_MC_O1 12
  458. #define SPARC_MC_O2 13
  459. #define SPARC_MC_O3 14
  460. #define SPARC_MC_O4 15
  461. #define SPARC_MC_O5 16
  462. #define SPARC_MC_O6 17
  463. #define SPARC_MC_O7 18
  464. #define SPARC_MC_NGREG 19
  465. typedef abi_ulong target_mc_greg_t;
  466. typedef target_mc_greg_t target_mc_gregset_t[SPARC_MC_NGREG];
  467. /*
  468. * Note the manual 16-alignment; the kernel gets this because it
  469. * includes a "long double qregs[16]" in the mcpu_fregs union,
  470. * which we can't do.
  471. */
  472. struct target_mc_fpu {
  473. union {
  474. uint32_t sregs[32];
  475. uint64_t dregs[32];
  476. //uint128_t qregs[16];
  477. } mcfpu_fregs;
  478. abi_ulong mcfpu_fsr;
  479. abi_ulong mcfpu_fprs;
  480. abi_ulong mcfpu_gsr;
  481. abi_ulong mcfpu_fq;
  482. unsigned char mcfpu_qcnt;
  483. unsigned char mcfpu_qentsz;
  484. unsigned char mcfpu_enab;
  485. } __attribute__((aligned(16)));
  486. typedef struct target_mc_fpu target_mc_fpu_t;
  487. typedef struct {
  488. target_mc_gregset_t mc_gregs;
  489. target_mc_greg_t mc_fp;
  490. target_mc_greg_t mc_i7;
  491. target_mc_fpu_t mc_fpregs;
  492. } target_mcontext_t;
  493. struct target_ucontext {
  494. abi_ulong tuc_link;
  495. abi_ulong tuc_flags;
  496. target_sigset_t tuc_sigmask;
  497. target_mcontext_t tuc_mcontext;
  498. };
  499. /* {set, get}context() needed for 64-bit SparcLinux userland. */
  500. void sparc64_set_context(CPUSPARCState *env)
  501. {
  502. abi_ulong ucp_addr;
  503. struct target_ucontext *ucp;
  504. target_mc_gregset_t *grp;
  505. target_mc_fpu_t *fpup;
  506. target_ulong pc, npc, tstate;
  507. unsigned int i;
  508. unsigned char fenab;
  509. ucp_addr = env->regwptr[WREG_O0];
  510. if (!lock_user_struct(VERIFY_READ, ucp, ucp_addr, 1)) {
  511. goto do_sigsegv;
  512. }
  513. grp = &ucp->tuc_mcontext.mc_gregs;
  514. __get_user(pc, &((*grp)[SPARC_MC_PC]));
  515. __get_user(npc, &((*grp)[SPARC_MC_NPC]));
  516. if ((pc | npc) & 3) {
  517. goto do_sigsegv;
  518. }
  519. if (env->regwptr[WREG_O1]) {
  520. target_sigset_t target_set;
  521. sigset_t set;
  522. if (TARGET_NSIG_WORDS == 1) {
  523. __get_user(target_set.sig[0], &ucp->tuc_sigmask.sig[0]);
  524. } else {
  525. abi_ulong *src, *dst;
  526. src = ucp->tuc_sigmask.sig;
  527. dst = target_set.sig;
  528. for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
  529. __get_user(*dst, src);
  530. }
  531. }
  532. target_to_host_sigset_internal(&set, &target_set);
  533. set_sigmask(&set);
  534. }
  535. env->pc = pc;
  536. env->npc = npc;
  537. __get_user(env->y, &((*grp)[SPARC_MC_Y]));
  538. __get_user(tstate, &((*grp)[SPARC_MC_TSTATE]));
  539. /* Honour TSTATE_ASI, TSTATE_ICC and TSTATE_XCC only */
  540. env->asi = (tstate >> 24) & 0xff;
  541. cpu_put_ccr(env, (tstate >> 32) & 0xff);
  542. __get_user(env->gregs[1], (&(*grp)[SPARC_MC_G1]));
  543. __get_user(env->gregs[2], (&(*grp)[SPARC_MC_G2]));
  544. __get_user(env->gregs[3], (&(*grp)[SPARC_MC_G3]));
  545. __get_user(env->gregs[4], (&(*grp)[SPARC_MC_G4]));
  546. __get_user(env->gregs[5], (&(*grp)[SPARC_MC_G5]));
  547. __get_user(env->gregs[6], (&(*grp)[SPARC_MC_G6]));
  548. /* Skip g7 as that's the thread register in userspace */
  549. /*
  550. * Note that unlike the kernel, we didn't need to mess with the
  551. * guest register window state to save it into a pt_regs to run
  552. * the kernel. So for us the guest's O regs are still in WREG_O*
  553. * (unlike the kernel which has put them in UREG_I* in a pt_regs)
  554. * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
  555. * need to be written back to userspace memory.
  556. */
  557. __get_user(env->regwptr[WREG_O0], (&(*grp)[SPARC_MC_O0]));
  558. __get_user(env->regwptr[WREG_O1], (&(*grp)[SPARC_MC_O1]));
  559. __get_user(env->regwptr[WREG_O2], (&(*grp)[SPARC_MC_O2]));
  560. __get_user(env->regwptr[WREG_O3], (&(*grp)[SPARC_MC_O3]));
  561. __get_user(env->regwptr[WREG_O4], (&(*grp)[SPARC_MC_O4]));
  562. __get_user(env->regwptr[WREG_O5], (&(*grp)[SPARC_MC_O5]));
  563. __get_user(env->regwptr[WREG_O6], (&(*grp)[SPARC_MC_O6]));
  564. __get_user(env->regwptr[WREG_O7], (&(*grp)[SPARC_MC_O7]));
  565. __get_user(env->regwptr[WREG_FP], &(ucp->tuc_mcontext.mc_fp));
  566. __get_user(env->regwptr[WREG_I7], &(ucp->tuc_mcontext.mc_i7));
  567. fpup = &ucp->tuc_mcontext.mc_fpregs;
  568. __get_user(fenab, &(fpup->mcfpu_enab));
  569. if (fenab) {
  570. abi_ulong fprs;
  571. abi_ulong fsr;
  572. /*
  573. * We use the FPRS from the guest only in deciding whether
  574. * to restore the upper, lower, or both banks of the FPU regs.
  575. * The kernel here writes the FPU register data into the
  576. * process's current_thread_info state and unconditionally
  577. * clears FPRS and TSTATE_PEF: this disables the FPU so that the
  578. * next FPU-disabled trap will copy the data out of
  579. * current_thread_info and into the real FPU registers.
  580. * QEMU doesn't need to handle lazy-FPU-state-restoring like that,
  581. * so we always load the data directly into the FPU registers
  582. * and leave FPRS and TSTATE_PEF alone (so the FPU stays enabled).
  583. * Note that because we (and the kernel) always write zeroes for
  584. * the fenab and fprs in sparc64_get_context() none of this code
  585. * will execute unless the guest manually constructed or changed
  586. * the context structure.
  587. */
  588. __get_user(fprs, &(fpup->mcfpu_fprs));
  589. if (fprs & FPRS_DL) {
  590. for (i = 0; i < 16; i++) {
  591. __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
  592. }
  593. }
  594. if (fprs & FPRS_DU) {
  595. for (i = 16; i < 32; i++) {
  596. __get_user(env->fpr[i].ll, &(fpup->mcfpu_fregs.dregs[i]));
  597. }
  598. }
  599. __get_user(fsr, &(fpup->mcfpu_fsr));
  600. cpu_put_fsr(env, fsr);
  601. __get_user(env->gsr, &(fpup->mcfpu_gsr));
  602. }
  603. unlock_user_struct(ucp, ucp_addr, 0);
  604. return;
  605. do_sigsegv:
  606. unlock_user_struct(ucp, ucp_addr, 0);
  607. force_sig(TARGET_SIGSEGV);
  608. }
  609. void sparc64_get_context(CPUSPARCState *env)
  610. {
  611. abi_ulong ucp_addr;
  612. struct target_ucontext *ucp;
  613. target_mc_gregset_t *grp;
  614. target_mcontext_t *mcp;
  615. int err;
  616. unsigned int i;
  617. target_sigset_t target_set;
  618. sigset_t set;
  619. ucp_addr = env->regwptr[WREG_O0];
  620. if (!lock_user_struct(VERIFY_WRITE, ucp, ucp_addr, 0)) {
  621. goto do_sigsegv;
  622. }
  623. memset(ucp, 0, sizeof(*ucp));
  624. mcp = &ucp->tuc_mcontext;
  625. grp = &mcp->mc_gregs;
  626. /* Skip over the trap instruction, first. */
  627. env->pc = env->npc;
  628. env->npc += 4;
  629. /* If we're only reading the signal mask then do_sigprocmask()
  630. * is guaranteed not to fail, which is important because we don't
  631. * have any way to signal a failure or restart this operation since
  632. * this is not a normal syscall.
  633. */
  634. err = do_sigprocmask(0, NULL, &set);
  635. assert(err == 0);
  636. host_to_target_sigset_internal(&target_set, &set);
  637. if (TARGET_NSIG_WORDS == 1) {
  638. __put_user(target_set.sig[0],
  639. (abi_ulong *)&ucp->tuc_sigmask);
  640. } else {
  641. abi_ulong *src, *dst;
  642. src = target_set.sig;
  643. dst = ucp->tuc_sigmask.sig;
  644. for (i = 0; i < TARGET_NSIG_WORDS; i++, dst++, src++) {
  645. __put_user(*src, dst);
  646. }
  647. }
  648. __put_user(sparc64_tstate(env), &((*grp)[SPARC_MC_TSTATE]));
  649. __put_user(env->pc, &((*grp)[SPARC_MC_PC]));
  650. __put_user(env->npc, &((*grp)[SPARC_MC_NPC]));
  651. __put_user(env->y, &((*grp)[SPARC_MC_Y]));
  652. __put_user(env->gregs[1], &((*grp)[SPARC_MC_G1]));
  653. __put_user(env->gregs[2], &((*grp)[SPARC_MC_G2]));
  654. __put_user(env->gregs[3], &((*grp)[SPARC_MC_G3]));
  655. __put_user(env->gregs[4], &((*grp)[SPARC_MC_G4]));
  656. __put_user(env->gregs[5], &((*grp)[SPARC_MC_G5]));
  657. __put_user(env->gregs[6], &((*grp)[SPARC_MC_G6]));
  658. __put_user(env->gregs[7], &((*grp)[SPARC_MC_G7]));
  659. /*
  660. * Note that unlike the kernel, we didn't need to mess with the
  661. * guest register window state to save it into a pt_regs to run
  662. * the kernel. So for us the guest's O regs are still in WREG_O*
  663. * (unlike the kernel which has put them in UREG_I* in a pt_regs)
  664. * and the fp and i7 are still in WREG_I6 and WREG_I7 and don't
  665. * need to be fished out of userspace memory.
  666. */
  667. __put_user(env->regwptr[WREG_O0], &((*grp)[SPARC_MC_O0]));
  668. __put_user(env->regwptr[WREG_O1], &((*grp)[SPARC_MC_O1]));
  669. __put_user(env->regwptr[WREG_O2], &((*grp)[SPARC_MC_O2]));
  670. __put_user(env->regwptr[WREG_O3], &((*grp)[SPARC_MC_O3]));
  671. __put_user(env->regwptr[WREG_O4], &((*grp)[SPARC_MC_O4]));
  672. __put_user(env->regwptr[WREG_O5], &((*grp)[SPARC_MC_O5]));
  673. __put_user(env->regwptr[WREG_O6], &((*grp)[SPARC_MC_O6]));
  674. __put_user(env->regwptr[WREG_O7], &((*grp)[SPARC_MC_O7]));
  675. __put_user(env->regwptr[WREG_FP], &(mcp->mc_fp));
  676. __put_user(env->regwptr[WREG_I7], &(mcp->mc_i7));
  677. /*
  678. * We don't write out the FPU state. This matches the kernel's
  679. * implementation (which has the code for doing this but
  680. * hidden behind an "if (fenab)" where fenab is always 0).
  681. */
  682. unlock_user_struct(ucp, ucp_addr, 1);
  683. return;
  684. do_sigsegv:
  685. unlock_user_struct(ucp, ucp_addr, 1);
  686. force_sig(TARGET_SIGSEGV);
  687. }
  688. #endif /* TARGET_SPARC64 */