user-exec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * User emulator execution
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include "cpu.h"
  21. #include "disas/disas.h"
  22. #include "tcg.h"
  23. #undef EAX
  24. #undef ECX
  25. #undef EDX
  26. #undef EBX
  27. #undef ESP
  28. #undef EBP
  29. #undef ESI
  30. #undef EDI
  31. #undef EIP
  32. #include <signal.h>
  33. #ifdef __linux__
  34. #include <sys/ucontext.h>
  35. #endif
  36. //#define DEBUG_SIGNAL
  37. static void exception_action(CPUArchState *env1)
  38. {
  39. #if defined(TARGET_I386)
  40. raise_exception_err(env1, env1->exception_index, env1->error_code);
  41. #else
  42. cpu_loop_exit(env1);
  43. #endif
  44. }
  45. /* exit the current TB from a signal handler. The host registers are
  46. restored in a state compatible with the CPU emulator
  47. */
  48. void cpu_resume_from_signal(CPUArchState *env1, void *puc)
  49. {
  50. #ifdef __linux__
  51. struct ucontext *uc = puc;
  52. #elif defined(__OpenBSD__)
  53. struct sigcontext *uc = puc;
  54. #endif
  55. if (puc) {
  56. /* XXX: use siglongjmp ? */
  57. #ifdef __linux__
  58. #ifdef __ia64
  59. sigprocmask(SIG_SETMASK, (sigset_t *)&uc->uc_sigmask, NULL);
  60. #else
  61. sigprocmask(SIG_SETMASK, &uc->uc_sigmask, NULL);
  62. #endif
  63. #elif defined(__OpenBSD__)
  64. sigprocmask(SIG_SETMASK, &uc->sc_mask, NULL);
  65. #endif
  66. }
  67. env1->exception_index = -1;
  68. longjmp(env1->jmp_env, 1);
  69. }
  70. /* 'pc' is the host PC at which the exception was raised. 'address' is
  71. the effective address of the memory exception. 'is_write' is 1 if a
  72. write caused the exception and otherwise 0'. 'old_set' is the
  73. signal set which should be restored */
  74. static inline int handle_cpu_signal(uintptr_t pc, unsigned long address,
  75. int is_write, sigset_t *old_set,
  76. void *puc)
  77. {
  78. int ret;
  79. #if defined(DEBUG_SIGNAL)
  80. qemu_printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
  81. pc, address, is_write, *(unsigned long *)old_set);
  82. #endif
  83. /* XXX: locking issue */
  84. if (is_write && h2g_valid(address)
  85. && page_unprotect(h2g(address), pc, puc)) {
  86. return 1;
  87. }
  88. /* see if it is an MMU fault */
  89. ret = cpu_handle_mmu_fault(cpu_single_env, address, is_write,
  90. MMU_USER_IDX);
  91. if (ret < 0) {
  92. return 0; /* not an MMU fault */
  93. }
  94. if (ret == 0) {
  95. return 1; /* the MMU fault was handled without causing real CPU fault */
  96. }
  97. /* now we have a real cpu fault */
  98. cpu_restore_state(cpu_single_env, pc);
  99. /* we restore the process signal mask as the sigreturn should
  100. do it (XXX: use sigsetjmp) */
  101. sigprocmask(SIG_SETMASK, old_set, NULL);
  102. exception_action(cpu_single_env);
  103. /* never comes here */
  104. return 1;
  105. }
  106. #if defined(__i386__)
  107. #if defined(__APPLE__)
  108. #include <sys/ucontext.h>
  109. #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext->ss.eip))
  110. #define TRAP_sig(context) ((context)->uc_mcontext->es.trapno)
  111. #define ERROR_sig(context) ((context)->uc_mcontext->es.err)
  112. #define MASK_sig(context) ((context)->uc_sigmask)
  113. #elif defined(__NetBSD__)
  114. #include <ucontext.h>
  115. #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
  116. #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
  117. #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
  118. #define MASK_sig(context) ((context)->uc_sigmask)
  119. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  120. #include <ucontext.h>
  121. #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
  122. #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
  123. #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
  124. #define MASK_sig(context) ((context)->uc_sigmask)
  125. #elif defined(__OpenBSD__)
  126. #define EIP_sig(context) ((context)->sc_eip)
  127. #define TRAP_sig(context) ((context)->sc_trapno)
  128. #define ERROR_sig(context) ((context)->sc_err)
  129. #define MASK_sig(context) ((context)->sc_mask)
  130. #else
  131. #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
  132. #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
  133. #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
  134. #define MASK_sig(context) ((context)->uc_sigmask)
  135. #endif
  136. int cpu_signal_handler(int host_signum, void *pinfo,
  137. void *puc)
  138. {
  139. siginfo_t *info = pinfo;
  140. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
  141. ucontext_t *uc = puc;
  142. #elif defined(__OpenBSD__)
  143. struct sigcontext *uc = puc;
  144. #else
  145. struct ucontext *uc = puc;
  146. #endif
  147. unsigned long pc;
  148. int trapno;
  149. #ifndef REG_EIP
  150. /* for glibc 2.1 */
  151. #define REG_EIP EIP
  152. #define REG_ERR ERR
  153. #define REG_TRAPNO TRAPNO
  154. #endif
  155. pc = EIP_sig(uc);
  156. trapno = TRAP_sig(uc);
  157. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  158. trapno == 0xe ?
  159. (ERROR_sig(uc) >> 1) & 1 : 0,
  160. &MASK_sig(uc), puc);
  161. }
  162. #elif defined(__x86_64__)
  163. #ifdef __NetBSD__
  164. #define PC_sig(context) _UC_MACHINE_PC(context)
  165. #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
  166. #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
  167. #define MASK_sig(context) ((context)->uc_sigmask)
  168. #elif defined(__OpenBSD__)
  169. #define PC_sig(context) ((context)->sc_rip)
  170. #define TRAP_sig(context) ((context)->sc_trapno)
  171. #define ERROR_sig(context) ((context)->sc_err)
  172. #define MASK_sig(context) ((context)->sc_mask)
  173. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  174. #include <ucontext.h>
  175. #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
  176. #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
  177. #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
  178. #define MASK_sig(context) ((context)->uc_sigmask)
  179. #else
  180. #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
  181. #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
  182. #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
  183. #define MASK_sig(context) ((context)->uc_sigmask)
  184. #endif
  185. int cpu_signal_handler(int host_signum, void *pinfo,
  186. void *puc)
  187. {
  188. siginfo_t *info = pinfo;
  189. unsigned long pc;
  190. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
  191. ucontext_t *uc = puc;
  192. #elif defined(__OpenBSD__)
  193. struct sigcontext *uc = puc;
  194. #else
  195. struct ucontext *uc = puc;
  196. #endif
  197. pc = PC_sig(uc);
  198. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  199. TRAP_sig(uc) == 0xe ?
  200. (ERROR_sig(uc) >> 1) & 1 : 0,
  201. &MASK_sig(uc), puc);
  202. }
  203. #elif defined(_ARCH_PPC)
  204. /***********************************************************************
  205. * signal context platform-specific definitions
  206. * From Wine
  207. */
  208. #ifdef linux
  209. /* All Registers access - only for local access */
  210. #define REG_sig(reg_name, context) \
  211. ((context)->uc_mcontext.regs->reg_name)
  212. /* Gpr Registers access */
  213. #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
  214. /* Program counter */
  215. #define IAR_sig(context) REG_sig(nip, context)
  216. /* Machine State Register (Supervisor) */
  217. #define MSR_sig(context) REG_sig(msr, context)
  218. /* Count register */
  219. #define CTR_sig(context) REG_sig(ctr, context)
  220. /* User's integer exception register */
  221. #define XER_sig(context) REG_sig(xer, context)
  222. /* Link register */
  223. #define LR_sig(context) REG_sig(link, context)
  224. /* Condition register */
  225. #define CR_sig(context) REG_sig(ccr, context)
  226. /* Float Registers access */
  227. #define FLOAT_sig(reg_num, context) \
  228. (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
  229. #define FPSCR_sig(context) \
  230. (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
  231. /* Exception Registers access */
  232. #define DAR_sig(context) REG_sig(dar, context)
  233. #define DSISR_sig(context) REG_sig(dsisr, context)
  234. #define TRAP_sig(context) REG_sig(trap, context)
  235. #endif /* linux */
  236. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  237. #include <ucontext.h>
  238. #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
  239. #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
  240. #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
  241. #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
  242. #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
  243. #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
  244. /* Exception Registers access */
  245. #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
  246. #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
  247. #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
  248. #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
  249. #ifdef __APPLE__
  250. #include <sys/ucontext.h>
  251. typedef struct ucontext SIGCONTEXT;
  252. /* All Registers access - only for local access */
  253. #define REG_sig(reg_name, context) \
  254. ((context)->uc_mcontext->ss.reg_name)
  255. #define FLOATREG_sig(reg_name, context) \
  256. ((context)->uc_mcontext->fs.reg_name)
  257. #define EXCEPREG_sig(reg_name, context) \
  258. ((context)->uc_mcontext->es.reg_name)
  259. #define VECREG_sig(reg_name, context) \
  260. ((context)->uc_mcontext->vs.reg_name)
  261. /* Gpr Registers access */
  262. #define GPR_sig(reg_num, context) REG_sig(r##reg_num, context)
  263. /* Program counter */
  264. #define IAR_sig(context) REG_sig(srr0, context)
  265. /* Machine State Register (Supervisor) */
  266. #define MSR_sig(context) REG_sig(srr1, context)
  267. #define CTR_sig(context) REG_sig(ctr, context)
  268. /* Link register */
  269. #define XER_sig(context) REG_sig(xer, context)
  270. /* User's integer exception register */
  271. #define LR_sig(context) REG_sig(lr, context)
  272. /* Condition register */
  273. #define CR_sig(context) REG_sig(cr, context)
  274. /* Float Registers access */
  275. #define FLOAT_sig(reg_num, context) \
  276. FLOATREG_sig(fpregs[reg_num], context)
  277. #define FPSCR_sig(context) \
  278. ((double)FLOATREG_sig(fpscr, context))
  279. /* Exception Registers access */
  280. /* Fault registers for coredump */
  281. #define DAR_sig(context) EXCEPREG_sig(dar, context)
  282. #define DSISR_sig(context) EXCEPREG_sig(dsisr, context)
  283. /* number of powerpc exception taken */
  284. #define TRAP_sig(context) EXCEPREG_sig(exception, context)
  285. #endif /* __APPLE__ */
  286. int cpu_signal_handler(int host_signum, void *pinfo,
  287. void *puc)
  288. {
  289. siginfo_t *info = pinfo;
  290. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  291. ucontext_t *uc = puc;
  292. #else
  293. struct ucontext *uc = puc;
  294. #endif
  295. unsigned long pc;
  296. int is_write;
  297. pc = IAR_sig(uc);
  298. is_write = 0;
  299. #if 0
  300. /* ppc 4xx case */
  301. if (DSISR_sig(uc) & 0x00800000) {
  302. is_write = 1;
  303. }
  304. #else
  305. if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
  306. is_write = 1;
  307. }
  308. #endif
  309. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  310. is_write, &uc->uc_sigmask, puc);
  311. }
  312. #elif defined(__alpha__)
  313. int cpu_signal_handler(int host_signum, void *pinfo,
  314. void *puc)
  315. {
  316. siginfo_t *info = pinfo;
  317. struct ucontext *uc = puc;
  318. uint32_t *pc = uc->uc_mcontext.sc_pc;
  319. uint32_t insn = *pc;
  320. int is_write = 0;
  321. /* XXX: need kernel patch to get write flag faster */
  322. switch (insn >> 26) {
  323. case 0x0d: /* stw */
  324. case 0x0e: /* stb */
  325. case 0x0f: /* stq_u */
  326. case 0x24: /* stf */
  327. case 0x25: /* stg */
  328. case 0x26: /* sts */
  329. case 0x27: /* stt */
  330. case 0x2c: /* stl */
  331. case 0x2d: /* stq */
  332. case 0x2e: /* stl_c */
  333. case 0x2f: /* stq_c */
  334. is_write = 1;
  335. }
  336. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  337. is_write, &uc->uc_sigmask, puc);
  338. }
  339. #elif defined(__sparc__)
  340. int cpu_signal_handler(int host_signum, void *pinfo,
  341. void *puc)
  342. {
  343. siginfo_t *info = pinfo;
  344. int is_write;
  345. uint32_t insn;
  346. #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
  347. uint32_t *regs = (uint32_t *)(info + 1);
  348. void *sigmask = (regs + 20);
  349. /* XXX: is there a standard glibc define ? */
  350. unsigned long pc = regs[1];
  351. #else
  352. #ifdef __linux__
  353. struct sigcontext *sc = puc;
  354. unsigned long pc = sc->sigc_regs.tpc;
  355. void *sigmask = (void *)sc->sigc_mask;
  356. #elif defined(__OpenBSD__)
  357. struct sigcontext *uc = puc;
  358. unsigned long pc = uc->sc_pc;
  359. void *sigmask = (void *)(long)uc->sc_mask;
  360. #endif
  361. #endif
  362. /* XXX: need kernel patch to get write flag faster */
  363. is_write = 0;
  364. insn = *(uint32_t *)pc;
  365. if ((insn >> 30) == 3) {
  366. switch ((insn >> 19) & 0x3f) {
  367. case 0x05: /* stb */
  368. case 0x15: /* stba */
  369. case 0x06: /* sth */
  370. case 0x16: /* stha */
  371. case 0x04: /* st */
  372. case 0x14: /* sta */
  373. case 0x07: /* std */
  374. case 0x17: /* stda */
  375. case 0x0e: /* stx */
  376. case 0x1e: /* stxa */
  377. case 0x24: /* stf */
  378. case 0x34: /* stfa */
  379. case 0x27: /* stdf */
  380. case 0x37: /* stdfa */
  381. case 0x26: /* stqf */
  382. case 0x36: /* stqfa */
  383. case 0x25: /* stfsr */
  384. case 0x3c: /* casa */
  385. case 0x3e: /* casxa */
  386. is_write = 1;
  387. break;
  388. }
  389. }
  390. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  391. is_write, sigmask, NULL);
  392. }
  393. #elif defined(__arm__)
  394. int cpu_signal_handler(int host_signum, void *pinfo,
  395. void *puc)
  396. {
  397. siginfo_t *info = pinfo;
  398. struct ucontext *uc = puc;
  399. unsigned long pc;
  400. int is_write;
  401. #if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
  402. pc = uc->uc_mcontext.gregs[R15];
  403. #else
  404. pc = uc->uc_mcontext.arm_pc;
  405. #endif
  406. /* XXX: compute is_write */
  407. is_write = 0;
  408. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  409. is_write,
  410. &uc->uc_sigmask, puc);
  411. }
  412. #elif defined(__mc68000)
  413. int cpu_signal_handler(int host_signum, void *pinfo,
  414. void *puc)
  415. {
  416. siginfo_t *info = pinfo;
  417. struct ucontext *uc = puc;
  418. unsigned long pc;
  419. int is_write;
  420. pc = uc->uc_mcontext.gregs[16];
  421. /* XXX: compute is_write */
  422. is_write = 0;
  423. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  424. is_write,
  425. &uc->uc_sigmask, puc);
  426. }
  427. #elif defined(__ia64)
  428. #ifndef __ISR_VALID
  429. /* This ought to be in <bits/siginfo.h>... */
  430. # define __ISR_VALID 1
  431. #endif
  432. int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
  433. {
  434. siginfo_t *info = pinfo;
  435. struct ucontext *uc = puc;
  436. unsigned long ip;
  437. int is_write = 0;
  438. ip = uc->uc_mcontext.sc_ip;
  439. switch (host_signum) {
  440. case SIGILL:
  441. case SIGFPE:
  442. case SIGSEGV:
  443. case SIGBUS:
  444. case SIGTRAP:
  445. if (info->si_code && (info->si_segvflags & __ISR_VALID)) {
  446. /* ISR.W (write-access) is bit 33: */
  447. is_write = (info->si_isr >> 33) & 1;
  448. }
  449. break;
  450. default:
  451. break;
  452. }
  453. return handle_cpu_signal(ip, (unsigned long)info->si_addr,
  454. is_write,
  455. (sigset_t *)&uc->uc_sigmask, puc);
  456. }
  457. #elif defined(__s390__)
  458. int cpu_signal_handler(int host_signum, void *pinfo,
  459. void *puc)
  460. {
  461. siginfo_t *info = pinfo;
  462. struct ucontext *uc = puc;
  463. unsigned long pc;
  464. uint16_t *pinsn;
  465. int is_write = 0;
  466. pc = uc->uc_mcontext.psw.addr;
  467. /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
  468. of the normal 2 arguments. The 3rd argument contains the "int_code"
  469. from the hardware which does in fact contain the is_write value.
  470. The rt signal handler, as far as I can tell, does not give this value
  471. at all. Not that we could get to it from here even if it were. */
  472. /* ??? This is not even close to complete, since it ignores all
  473. of the read-modify-write instructions. */
  474. pinsn = (uint16_t *)pc;
  475. switch (pinsn[0] >> 8) {
  476. case 0x50: /* ST */
  477. case 0x42: /* STC */
  478. case 0x40: /* STH */
  479. is_write = 1;
  480. break;
  481. case 0xc4: /* RIL format insns */
  482. switch (pinsn[0] & 0xf) {
  483. case 0xf: /* STRL */
  484. case 0xb: /* STGRL */
  485. case 0x7: /* STHRL */
  486. is_write = 1;
  487. }
  488. break;
  489. case 0xe3: /* RXY format insns */
  490. switch (pinsn[2] & 0xff) {
  491. case 0x50: /* STY */
  492. case 0x24: /* STG */
  493. case 0x72: /* STCY */
  494. case 0x70: /* STHY */
  495. case 0x8e: /* STPQ */
  496. case 0x3f: /* STRVH */
  497. case 0x3e: /* STRV */
  498. case 0x2f: /* STRVG */
  499. is_write = 1;
  500. }
  501. break;
  502. }
  503. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  504. is_write, &uc->uc_sigmask, puc);
  505. }
  506. #elif defined(__mips__)
  507. int cpu_signal_handler(int host_signum, void *pinfo,
  508. void *puc)
  509. {
  510. siginfo_t *info = pinfo;
  511. struct ucontext *uc = puc;
  512. greg_t pc = uc->uc_mcontext.pc;
  513. int is_write;
  514. /* XXX: compute is_write */
  515. is_write = 0;
  516. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  517. is_write, &uc->uc_sigmask, puc);
  518. }
  519. #elif defined(__hppa__)
  520. int cpu_signal_handler(int host_signum, void *pinfo,
  521. void *puc)
  522. {
  523. siginfo_t *info = pinfo;
  524. struct ucontext *uc = puc;
  525. unsigned long pc = uc->uc_mcontext.sc_iaoq[0];
  526. uint32_t insn = *(uint32_t *)pc;
  527. int is_write = 0;
  528. /* XXX: need kernel patch to get write flag faster. */
  529. switch (insn >> 26) {
  530. case 0x1a: /* STW */
  531. case 0x19: /* STH */
  532. case 0x18: /* STB */
  533. case 0x1b: /* STWM */
  534. is_write = 1;
  535. break;
  536. case 0x09: /* CSTWX, FSTWX, FSTWS */
  537. case 0x0b: /* CSTDX, FSTDX, FSTDS */
  538. /* Distinguish from coprocessor load ... */
  539. is_write = (insn >> 9) & 1;
  540. break;
  541. case 0x03:
  542. switch ((insn >> 6) & 15) {
  543. case 0xa: /* STWS */
  544. case 0x9: /* STHS */
  545. case 0x8: /* STBS */
  546. case 0xe: /* STWAS */
  547. case 0xc: /* STBYS */
  548. is_write = 1;
  549. }
  550. break;
  551. }
  552. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  553. is_write, &uc->uc_sigmask, puc);
  554. }
  555. #else
  556. #error host CPU specific signal handler needed
  557. #endif