user-exec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 "qemu/osdep.h"
  20. #include "cpu.h"
  21. #include "disas/disas.h"
  22. #include "exec/exec-all.h"
  23. #include "tcg.h"
  24. #include "qemu/bitops.h"
  25. #include "exec/cpu_ldst.h"
  26. #include "translate-all.h"
  27. #undef EAX
  28. #undef ECX
  29. #undef EDX
  30. #undef EBX
  31. #undef ESP
  32. #undef EBP
  33. #undef ESI
  34. #undef EDI
  35. #undef EIP
  36. #ifdef __linux__
  37. #include <sys/ucontext.h>
  38. #endif
  39. //#define DEBUG_SIGNAL
  40. /* exit the current TB from a signal handler. The host registers are
  41. restored in a state compatible with the CPU emulator
  42. */
  43. static void cpu_exit_tb_from_sighandler(CPUState *cpu, sigset_t *old_set)
  44. {
  45. /* XXX: use siglongjmp ? */
  46. sigprocmask(SIG_SETMASK, old_set, NULL);
  47. cpu_loop_exit_noexc(cpu);
  48. }
  49. /* 'pc' is the host PC at which the exception was raised. 'address' is
  50. the effective address of the memory exception. 'is_write' is 1 if a
  51. write caused the exception and otherwise 0'. 'old_set' is the
  52. signal set which should be restored */
  53. static inline int handle_cpu_signal(uintptr_t pc, unsigned long address,
  54. int is_write, sigset_t *old_set)
  55. {
  56. CPUState *cpu = current_cpu;
  57. CPUClass *cc;
  58. int ret;
  59. /* For synchronous signals we expect to be coming from the vCPU
  60. * thread (so current_cpu should be valid) and either from running
  61. * code or during translation which can fault as we cross pages.
  62. *
  63. * If neither is true then something has gone wrong and we should
  64. * abort rather than try and restart the vCPU execution.
  65. */
  66. if (!cpu || !cpu->running) {
  67. printf("qemu:%s received signal outside vCPU context @ pc=0x%"
  68. PRIxPTR "\n", __func__, pc);
  69. abort();
  70. }
  71. #if defined(DEBUG_SIGNAL)
  72. printf("qemu: SIGSEGV pc=0x%08lx address=%08lx w=%d oldset=0x%08lx\n",
  73. pc, address, is_write, *(unsigned long *)old_set);
  74. #endif
  75. /* XXX: locking issue */
  76. if (is_write && h2g_valid(address)) {
  77. switch (page_unprotect(h2g(address), pc)) {
  78. case 0:
  79. /* Fault not caused by a page marked unwritable to protect
  80. * cached translations, must be the guest binary's problem
  81. */
  82. break;
  83. case 1:
  84. /* Fault caused by protection of cached translation; TBs
  85. * invalidated, so resume execution
  86. */
  87. return 1;
  88. case 2:
  89. /* Fault caused by protection of cached translation, and the
  90. * currently executing TB was modified and must be exited
  91. * immediately.
  92. */
  93. cpu_exit_tb_from_sighandler(cpu, old_set);
  94. g_assert_not_reached();
  95. default:
  96. g_assert_not_reached();
  97. }
  98. }
  99. /* Convert forcefully to guest address space, invalid addresses
  100. are still valid segv ones */
  101. address = h2g_nocheck(address);
  102. cc = CPU_GET_CLASS(cpu);
  103. /* see if it is an MMU fault */
  104. g_assert(cc->handle_mmu_fault);
  105. ret = cc->handle_mmu_fault(cpu, address, is_write, MMU_USER_IDX);
  106. if (ret < 0) {
  107. return 0; /* not an MMU fault */
  108. }
  109. if (ret == 0) {
  110. return 1; /* the MMU fault was handled without causing real CPU fault */
  111. }
  112. /* Now we have a real cpu fault. Since this is the exact location of
  113. * the exception, we must undo the adjustment done by cpu_restore_state
  114. * for handling call return addresses. */
  115. cpu_restore_state(cpu, pc + GETPC_ADJ);
  116. sigprocmask(SIG_SETMASK, old_set, NULL);
  117. cpu_loop_exit(cpu);
  118. /* never comes here */
  119. return 1;
  120. }
  121. #if defined(__i386__)
  122. #if defined(__NetBSD__)
  123. #include <ucontext.h>
  124. #define EIP_sig(context) ((context)->uc_mcontext.__gregs[_REG_EIP])
  125. #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
  126. #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
  127. #define MASK_sig(context) ((context)->uc_sigmask)
  128. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  129. #include <ucontext.h>
  130. #define EIP_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_eip))
  131. #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
  132. #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
  133. #define MASK_sig(context) ((context)->uc_sigmask)
  134. #elif defined(__OpenBSD__)
  135. #define EIP_sig(context) ((context)->sc_eip)
  136. #define TRAP_sig(context) ((context)->sc_trapno)
  137. #define ERROR_sig(context) ((context)->sc_err)
  138. #define MASK_sig(context) ((context)->sc_mask)
  139. #else
  140. #define EIP_sig(context) ((context)->uc_mcontext.gregs[REG_EIP])
  141. #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
  142. #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
  143. #define MASK_sig(context) ((context)->uc_sigmask)
  144. #endif
  145. int cpu_signal_handler(int host_signum, void *pinfo,
  146. void *puc)
  147. {
  148. siginfo_t *info = pinfo;
  149. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
  150. ucontext_t *uc = puc;
  151. #elif defined(__OpenBSD__)
  152. struct sigcontext *uc = puc;
  153. #else
  154. struct ucontext *uc = puc;
  155. #endif
  156. unsigned long pc;
  157. int trapno;
  158. #ifndef REG_EIP
  159. /* for glibc 2.1 */
  160. #define REG_EIP EIP
  161. #define REG_ERR ERR
  162. #define REG_TRAPNO TRAPNO
  163. #endif
  164. pc = EIP_sig(uc);
  165. trapno = TRAP_sig(uc);
  166. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  167. trapno == 0xe ?
  168. (ERROR_sig(uc) >> 1) & 1 : 0,
  169. &MASK_sig(uc));
  170. }
  171. #elif defined(__x86_64__)
  172. #ifdef __NetBSD__
  173. #define PC_sig(context) _UC_MACHINE_PC(context)
  174. #define TRAP_sig(context) ((context)->uc_mcontext.__gregs[_REG_TRAPNO])
  175. #define ERROR_sig(context) ((context)->uc_mcontext.__gregs[_REG_ERR])
  176. #define MASK_sig(context) ((context)->uc_sigmask)
  177. #elif defined(__OpenBSD__)
  178. #define PC_sig(context) ((context)->sc_rip)
  179. #define TRAP_sig(context) ((context)->sc_trapno)
  180. #define ERROR_sig(context) ((context)->sc_err)
  181. #define MASK_sig(context) ((context)->sc_mask)
  182. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  183. #include <ucontext.h>
  184. #define PC_sig(context) (*((unsigned long *)&(context)->uc_mcontext.mc_rip))
  185. #define TRAP_sig(context) ((context)->uc_mcontext.mc_trapno)
  186. #define ERROR_sig(context) ((context)->uc_mcontext.mc_err)
  187. #define MASK_sig(context) ((context)->uc_sigmask)
  188. #else
  189. #define PC_sig(context) ((context)->uc_mcontext.gregs[REG_RIP])
  190. #define TRAP_sig(context) ((context)->uc_mcontext.gregs[REG_TRAPNO])
  191. #define ERROR_sig(context) ((context)->uc_mcontext.gregs[REG_ERR])
  192. #define MASK_sig(context) ((context)->uc_sigmask)
  193. #endif
  194. int cpu_signal_handler(int host_signum, void *pinfo,
  195. void *puc)
  196. {
  197. siginfo_t *info = pinfo;
  198. unsigned long pc;
  199. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
  200. ucontext_t *uc = puc;
  201. #elif defined(__OpenBSD__)
  202. struct sigcontext *uc = puc;
  203. #else
  204. struct ucontext *uc = puc;
  205. #endif
  206. pc = PC_sig(uc);
  207. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  208. TRAP_sig(uc) == 0xe ?
  209. (ERROR_sig(uc) >> 1) & 1 : 0,
  210. &MASK_sig(uc));
  211. }
  212. #elif defined(_ARCH_PPC)
  213. /***********************************************************************
  214. * signal context platform-specific definitions
  215. * From Wine
  216. */
  217. #ifdef linux
  218. /* All Registers access - only for local access */
  219. #define REG_sig(reg_name, context) \
  220. ((context)->uc_mcontext.regs->reg_name)
  221. /* Gpr Registers access */
  222. #define GPR_sig(reg_num, context) REG_sig(gpr[reg_num], context)
  223. /* Program counter */
  224. #define IAR_sig(context) REG_sig(nip, context)
  225. /* Machine State Register (Supervisor) */
  226. #define MSR_sig(context) REG_sig(msr, context)
  227. /* Count register */
  228. #define CTR_sig(context) REG_sig(ctr, context)
  229. /* User's integer exception register */
  230. #define XER_sig(context) REG_sig(xer, context)
  231. /* Link register */
  232. #define LR_sig(context) REG_sig(link, context)
  233. /* Condition register */
  234. #define CR_sig(context) REG_sig(ccr, context)
  235. /* Float Registers access */
  236. #define FLOAT_sig(reg_num, context) \
  237. (((double *)((char *)((context)->uc_mcontext.regs + 48 * 4)))[reg_num])
  238. #define FPSCR_sig(context) \
  239. (*(int *)((char *)((context)->uc_mcontext.regs + (48 + 32 * 2) * 4)))
  240. /* Exception Registers access */
  241. #define DAR_sig(context) REG_sig(dar, context)
  242. #define DSISR_sig(context) REG_sig(dsisr, context)
  243. #define TRAP_sig(context) REG_sig(trap, context)
  244. #endif /* linux */
  245. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  246. #include <ucontext.h>
  247. #define IAR_sig(context) ((context)->uc_mcontext.mc_srr0)
  248. #define MSR_sig(context) ((context)->uc_mcontext.mc_srr1)
  249. #define CTR_sig(context) ((context)->uc_mcontext.mc_ctr)
  250. #define XER_sig(context) ((context)->uc_mcontext.mc_xer)
  251. #define LR_sig(context) ((context)->uc_mcontext.mc_lr)
  252. #define CR_sig(context) ((context)->uc_mcontext.mc_cr)
  253. /* Exception Registers access */
  254. #define DAR_sig(context) ((context)->uc_mcontext.mc_dar)
  255. #define DSISR_sig(context) ((context)->uc_mcontext.mc_dsisr)
  256. #define TRAP_sig(context) ((context)->uc_mcontext.mc_exc)
  257. #endif /* __FreeBSD__|| __FreeBSD_kernel__ */
  258. int cpu_signal_handler(int host_signum, void *pinfo,
  259. void *puc)
  260. {
  261. siginfo_t *info = pinfo;
  262. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
  263. ucontext_t *uc = puc;
  264. #else
  265. struct ucontext *uc = puc;
  266. #endif
  267. unsigned long pc;
  268. int is_write;
  269. pc = IAR_sig(uc);
  270. is_write = 0;
  271. #if 0
  272. /* ppc 4xx case */
  273. if (DSISR_sig(uc) & 0x00800000) {
  274. is_write = 1;
  275. }
  276. #else
  277. if (TRAP_sig(uc) != 0x400 && (DSISR_sig(uc) & 0x02000000)) {
  278. is_write = 1;
  279. }
  280. #endif
  281. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  282. is_write, &uc->uc_sigmask);
  283. }
  284. #elif defined(__alpha__)
  285. int cpu_signal_handler(int host_signum, void *pinfo,
  286. void *puc)
  287. {
  288. siginfo_t *info = pinfo;
  289. struct ucontext *uc = puc;
  290. uint32_t *pc = uc->uc_mcontext.sc_pc;
  291. uint32_t insn = *pc;
  292. int is_write = 0;
  293. /* XXX: need kernel patch to get write flag faster */
  294. switch (insn >> 26) {
  295. case 0x0d: /* stw */
  296. case 0x0e: /* stb */
  297. case 0x0f: /* stq_u */
  298. case 0x24: /* stf */
  299. case 0x25: /* stg */
  300. case 0x26: /* sts */
  301. case 0x27: /* stt */
  302. case 0x2c: /* stl */
  303. case 0x2d: /* stq */
  304. case 0x2e: /* stl_c */
  305. case 0x2f: /* stq_c */
  306. is_write = 1;
  307. }
  308. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  309. is_write, &uc->uc_sigmask);
  310. }
  311. #elif defined(__sparc__)
  312. int cpu_signal_handler(int host_signum, void *pinfo,
  313. void *puc)
  314. {
  315. siginfo_t *info = pinfo;
  316. int is_write;
  317. uint32_t insn;
  318. #if !defined(__arch64__) || defined(CONFIG_SOLARIS)
  319. uint32_t *regs = (uint32_t *)(info + 1);
  320. void *sigmask = (regs + 20);
  321. /* XXX: is there a standard glibc define ? */
  322. unsigned long pc = regs[1];
  323. #else
  324. #ifdef __linux__
  325. struct sigcontext *sc = puc;
  326. unsigned long pc = sc->sigc_regs.tpc;
  327. void *sigmask = (void *)sc->sigc_mask;
  328. #elif defined(__OpenBSD__)
  329. struct sigcontext *uc = puc;
  330. unsigned long pc = uc->sc_pc;
  331. void *sigmask = (void *)(long)uc->sc_mask;
  332. #elif defined(__NetBSD__)
  333. ucontext_t *uc = puc;
  334. unsigned long pc = _UC_MACHINE_PC(uc);
  335. void *sigmask = (void *)&uc->uc_sigmask;
  336. #endif
  337. #endif
  338. /* XXX: need kernel patch to get write flag faster */
  339. is_write = 0;
  340. insn = *(uint32_t *)pc;
  341. if ((insn >> 30) == 3) {
  342. switch ((insn >> 19) & 0x3f) {
  343. case 0x05: /* stb */
  344. case 0x15: /* stba */
  345. case 0x06: /* sth */
  346. case 0x16: /* stha */
  347. case 0x04: /* st */
  348. case 0x14: /* sta */
  349. case 0x07: /* std */
  350. case 0x17: /* stda */
  351. case 0x0e: /* stx */
  352. case 0x1e: /* stxa */
  353. case 0x24: /* stf */
  354. case 0x34: /* stfa */
  355. case 0x27: /* stdf */
  356. case 0x37: /* stdfa */
  357. case 0x26: /* stqf */
  358. case 0x36: /* stqfa */
  359. case 0x25: /* stfsr */
  360. case 0x3c: /* casa */
  361. case 0x3e: /* casxa */
  362. is_write = 1;
  363. break;
  364. }
  365. }
  366. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  367. is_write, sigmask);
  368. }
  369. #elif defined(__arm__)
  370. #if defined(__NetBSD__)
  371. #include <ucontext.h>
  372. #endif
  373. int cpu_signal_handler(int host_signum, void *pinfo,
  374. void *puc)
  375. {
  376. siginfo_t *info = pinfo;
  377. #if defined(__NetBSD__)
  378. ucontext_t *uc = puc;
  379. #else
  380. struct ucontext *uc = puc;
  381. #endif
  382. unsigned long pc;
  383. int is_write;
  384. #if defined(__NetBSD__)
  385. pc = uc->uc_mcontext.__gregs[_REG_R15];
  386. #elif defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 3))
  387. pc = uc->uc_mcontext.gregs[R15];
  388. #else
  389. pc = uc->uc_mcontext.arm_pc;
  390. #endif
  391. /* error_code is the FSR value, in which bit 11 is WnR (assuming a v6 or
  392. * later processor; on v5 we will always report this as a read).
  393. */
  394. is_write = extract32(uc->uc_mcontext.error_code, 11, 1);
  395. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  396. is_write,
  397. &uc->uc_sigmask);
  398. }
  399. #elif defined(__aarch64__)
  400. int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
  401. {
  402. siginfo_t *info = pinfo;
  403. struct ucontext *uc = puc;
  404. uintptr_t pc = uc->uc_mcontext.pc;
  405. uint32_t insn = *(uint32_t *)pc;
  406. bool is_write;
  407. /* XXX: need kernel patch to get write flag faster. */
  408. is_write = ( (insn & 0xbfff0000) == 0x0c000000 /* C3.3.1 */
  409. || (insn & 0xbfe00000) == 0x0c800000 /* C3.3.2 */
  410. || (insn & 0xbfdf0000) == 0x0d000000 /* C3.3.3 */
  411. || (insn & 0xbfc00000) == 0x0d800000 /* C3.3.4 */
  412. || (insn & 0x3f400000) == 0x08000000 /* C3.3.6 */
  413. || (insn & 0x3bc00000) == 0x39000000 /* C3.3.13 */
  414. || (insn & 0x3fc00000) == 0x3d800000 /* ... 128bit */
  415. /* Ingore bits 10, 11 & 21, controlling indexing. */
  416. || (insn & 0x3bc00000) == 0x38000000 /* C3.3.8-12 */
  417. || (insn & 0x3fe00000) == 0x3c800000 /* ... 128bit */
  418. /* Ignore bits 23 & 24, controlling indexing. */
  419. || (insn & 0x3a400000) == 0x28000000); /* C3.3.7,14-16 */
  420. return handle_cpu_signal(pc, (uintptr_t)info->si_addr,
  421. is_write, &uc->uc_sigmask);
  422. }
  423. #elif defined(__ia64)
  424. #ifndef __ISR_VALID
  425. /* This ought to be in <bits/siginfo.h>... */
  426. # define __ISR_VALID 1
  427. #endif
  428. int cpu_signal_handler(int host_signum, void *pinfo, void *puc)
  429. {
  430. siginfo_t *info = pinfo;
  431. struct ucontext *uc = puc;
  432. unsigned long ip;
  433. int is_write = 0;
  434. ip = uc->uc_mcontext.sc_ip;
  435. switch (host_signum) {
  436. case SIGILL:
  437. case SIGFPE:
  438. case SIGSEGV:
  439. case SIGBUS:
  440. case SIGTRAP:
  441. if (info->si_code && (info->si_segvflags & __ISR_VALID)) {
  442. /* ISR.W (write-access) is bit 33: */
  443. is_write = (info->si_isr >> 33) & 1;
  444. }
  445. break;
  446. default:
  447. break;
  448. }
  449. return handle_cpu_signal(ip, (unsigned long)info->si_addr,
  450. is_write,
  451. (sigset_t *)&uc->uc_sigmask);
  452. }
  453. #elif defined(__s390__)
  454. int cpu_signal_handler(int host_signum, void *pinfo,
  455. void *puc)
  456. {
  457. siginfo_t *info = pinfo;
  458. struct ucontext *uc = puc;
  459. unsigned long pc;
  460. uint16_t *pinsn;
  461. int is_write = 0;
  462. pc = uc->uc_mcontext.psw.addr;
  463. /* ??? On linux, the non-rt signal handler has 4 (!) arguments instead
  464. of the normal 2 arguments. The 3rd argument contains the "int_code"
  465. from the hardware which does in fact contain the is_write value.
  466. The rt signal handler, as far as I can tell, does not give this value
  467. at all. Not that we could get to it from here even if it were. */
  468. /* ??? This is not even close to complete, since it ignores all
  469. of the read-modify-write instructions. */
  470. pinsn = (uint16_t *)pc;
  471. switch (pinsn[0] >> 8) {
  472. case 0x50: /* ST */
  473. case 0x42: /* STC */
  474. case 0x40: /* STH */
  475. is_write = 1;
  476. break;
  477. case 0xc4: /* RIL format insns */
  478. switch (pinsn[0] & 0xf) {
  479. case 0xf: /* STRL */
  480. case 0xb: /* STGRL */
  481. case 0x7: /* STHRL */
  482. is_write = 1;
  483. }
  484. break;
  485. case 0xe3: /* RXY format insns */
  486. switch (pinsn[2] & 0xff) {
  487. case 0x50: /* STY */
  488. case 0x24: /* STG */
  489. case 0x72: /* STCY */
  490. case 0x70: /* STHY */
  491. case 0x8e: /* STPQ */
  492. case 0x3f: /* STRVH */
  493. case 0x3e: /* STRV */
  494. case 0x2f: /* STRVG */
  495. is_write = 1;
  496. }
  497. break;
  498. }
  499. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  500. is_write, &uc->uc_sigmask);
  501. }
  502. #elif defined(__mips__)
  503. int cpu_signal_handler(int host_signum, void *pinfo,
  504. void *puc)
  505. {
  506. siginfo_t *info = pinfo;
  507. struct ucontext *uc = puc;
  508. greg_t pc = uc->uc_mcontext.pc;
  509. int is_write;
  510. /* XXX: compute is_write */
  511. is_write = 0;
  512. return handle_cpu_signal(pc, (unsigned long)info->si_addr,
  513. is_write, &uc->uc_sigmask);
  514. }
  515. #else
  516. #error host CPU specific signal handler needed
  517. #endif