main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /*
  2. * qemu user main
  3. *
  4. * Copyright (c) 2003-2008 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-common.h"
  21. #include "qemu/units.h"
  22. #include "sysemu/tcg.h"
  23. #include "qemu-version.h"
  24. #include <machine/trap.h>
  25. #include "qapi/error.h"
  26. #include "qemu.h"
  27. #include "qemu/config-file.h"
  28. #include "qemu/error-report.h"
  29. #include "qemu/path.h"
  30. #include "qemu/help_option.h"
  31. #include "qemu/module.h"
  32. #include "cpu.h"
  33. #include "exec/exec-all.h"
  34. #include "tcg/tcg.h"
  35. #include "qemu/timer.h"
  36. #include "qemu/envlist.h"
  37. #include "exec/log.h"
  38. #include "trace/control.h"
  39. int singlestep;
  40. unsigned long mmap_min_addr;
  41. unsigned long guest_base;
  42. bool have_guest_base;
  43. unsigned long reserved_va;
  44. static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
  45. const char *qemu_uname_release;
  46. extern char **environ;
  47. enum BSDType bsd_type;
  48. /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
  49. we allocate a bigger stack. Need a better solution, for example
  50. by remapping the process stack directly at the right place */
  51. unsigned long x86_stack_size = 512 * 1024;
  52. void gemu_log(const char *fmt, ...)
  53. {
  54. va_list ap;
  55. va_start(ap, fmt);
  56. vfprintf(stderr, fmt, ap);
  57. va_end(ap);
  58. }
  59. #if defined(TARGET_I386)
  60. int cpu_get_pic_interrupt(CPUX86State *env)
  61. {
  62. return -1;
  63. }
  64. #endif
  65. void fork_start(void)
  66. {
  67. }
  68. void fork_end(int child)
  69. {
  70. if (child) {
  71. gdbserver_fork(thread_cpu);
  72. }
  73. }
  74. #ifdef TARGET_I386
  75. /***********************************************************/
  76. /* CPUX86 core interface */
  77. uint64_t cpu_get_tsc(CPUX86State *env)
  78. {
  79. return cpu_get_host_ticks();
  80. }
  81. static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
  82. int flags)
  83. {
  84. unsigned int e1, e2;
  85. uint32_t *p;
  86. e1 = (addr << 16) | (limit & 0xffff);
  87. e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
  88. e2 |= flags;
  89. p = ptr;
  90. p[0] = tswap32(e1);
  91. p[1] = tswap32(e2);
  92. }
  93. static uint64_t *idt_table;
  94. #ifdef TARGET_X86_64
  95. static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
  96. uint64_t addr, unsigned int sel)
  97. {
  98. uint32_t *p, e1, e2;
  99. e1 = (addr & 0xffff) | (sel << 16);
  100. e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
  101. p = ptr;
  102. p[0] = tswap32(e1);
  103. p[1] = tswap32(e2);
  104. p[2] = tswap32(addr >> 32);
  105. p[3] = 0;
  106. }
  107. /* only dpl matters as we do only user space emulation */
  108. static void set_idt(int n, unsigned int dpl)
  109. {
  110. set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
  111. }
  112. #else
  113. static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
  114. uint32_t addr, unsigned int sel)
  115. {
  116. uint32_t *p, e1, e2;
  117. e1 = (addr & 0xffff) | (sel << 16);
  118. e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
  119. p = ptr;
  120. p[0] = tswap32(e1);
  121. p[1] = tswap32(e2);
  122. }
  123. /* only dpl matters as we do only user space emulation */
  124. static void set_idt(int n, unsigned int dpl)
  125. {
  126. set_gate(idt_table + n, 0, dpl, 0, 0);
  127. }
  128. #endif
  129. void cpu_loop(CPUX86State *env)
  130. {
  131. CPUState *cs = env_cpu(env);
  132. int trapnr;
  133. abi_ulong pc;
  134. //target_siginfo_t info;
  135. for(;;) {
  136. cpu_exec_start(cs);
  137. trapnr = cpu_exec(cs);
  138. cpu_exec_end(cs);
  139. process_queued_cpu_work(cs);
  140. switch(trapnr) {
  141. case 0x80:
  142. /* syscall from int $0x80 */
  143. if (bsd_type == target_freebsd) {
  144. abi_ulong params = (abi_ulong) env->regs[R_ESP] +
  145. sizeof(int32_t);
  146. int32_t syscall_nr = env->regs[R_EAX];
  147. int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
  148. if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
  149. get_user_s32(syscall_nr, params);
  150. params += sizeof(int32_t);
  151. } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
  152. get_user_s32(syscall_nr, params);
  153. params += sizeof(int64_t);
  154. }
  155. get_user_s32(arg1, params);
  156. params += sizeof(int32_t);
  157. get_user_s32(arg2, params);
  158. params += sizeof(int32_t);
  159. get_user_s32(arg3, params);
  160. params += sizeof(int32_t);
  161. get_user_s32(arg4, params);
  162. params += sizeof(int32_t);
  163. get_user_s32(arg5, params);
  164. params += sizeof(int32_t);
  165. get_user_s32(arg6, params);
  166. params += sizeof(int32_t);
  167. get_user_s32(arg7, params);
  168. params += sizeof(int32_t);
  169. get_user_s32(arg8, params);
  170. env->regs[R_EAX] = do_freebsd_syscall(env,
  171. syscall_nr,
  172. arg1,
  173. arg2,
  174. arg3,
  175. arg4,
  176. arg5,
  177. arg6,
  178. arg7,
  179. arg8);
  180. } else { //if (bsd_type == target_openbsd)
  181. env->regs[R_EAX] = do_openbsd_syscall(env,
  182. env->regs[R_EAX],
  183. env->regs[R_EBX],
  184. env->regs[R_ECX],
  185. env->regs[R_EDX],
  186. env->regs[R_ESI],
  187. env->regs[R_EDI],
  188. env->regs[R_EBP]);
  189. }
  190. if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
  191. env->regs[R_EAX] = -env->regs[R_EAX];
  192. env->eflags |= CC_C;
  193. } else {
  194. env->eflags &= ~CC_C;
  195. }
  196. break;
  197. #ifndef TARGET_ABI32
  198. case EXCP_SYSCALL:
  199. /* syscall from syscall instruction */
  200. if (bsd_type == target_freebsd)
  201. env->regs[R_EAX] = do_freebsd_syscall(env,
  202. env->regs[R_EAX],
  203. env->regs[R_EDI],
  204. env->regs[R_ESI],
  205. env->regs[R_EDX],
  206. env->regs[R_ECX],
  207. env->regs[8],
  208. env->regs[9], 0, 0);
  209. else { //if (bsd_type == target_openbsd)
  210. env->regs[R_EAX] = do_openbsd_syscall(env,
  211. env->regs[R_EAX],
  212. env->regs[R_EDI],
  213. env->regs[R_ESI],
  214. env->regs[R_EDX],
  215. env->regs[10],
  216. env->regs[8],
  217. env->regs[9]);
  218. }
  219. env->eip = env->exception_next_eip;
  220. if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
  221. env->regs[R_EAX] = -env->regs[R_EAX];
  222. env->eflags |= CC_C;
  223. } else {
  224. env->eflags &= ~CC_C;
  225. }
  226. break;
  227. #endif
  228. #if 0
  229. case EXCP0B_NOSEG:
  230. case EXCP0C_STACK:
  231. info.si_signo = SIGBUS;
  232. info.si_errno = 0;
  233. info.si_code = TARGET_SI_KERNEL;
  234. info._sifields._sigfault._addr = 0;
  235. queue_signal(env, info.si_signo, &info);
  236. break;
  237. case EXCP0D_GPF:
  238. /* XXX: potential problem if ABI32 */
  239. #ifndef TARGET_X86_64
  240. if (env->eflags & VM_MASK) {
  241. handle_vm86_fault(env);
  242. } else
  243. #endif
  244. {
  245. info.si_signo = SIGSEGV;
  246. info.si_errno = 0;
  247. info.si_code = TARGET_SI_KERNEL;
  248. info._sifields._sigfault._addr = 0;
  249. queue_signal(env, info.si_signo, &info);
  250. }
  251. break;
  252. case EXCP0E_PAGE:
  253. info.si_signo = SIGSEGV;
  254. info.si_errno = 0;
  255. if (!(env->error_code & 1))
  256. info.si_code = TARGET_SEGV_MAPERR;
  257. else
  258. info.si_code = TARGET_SEGV_ACCERR;
  259. info._sifields._sigfault._addr = env->cr[2];
  260. queue_signal(env, info.si_signo, &info);
  261. break;
  262. case EXCP00_DIVZ:
  263. #ifndef TARGET_X86_64
  264. if (env->eflags & VM_MASK) {
  265. handle_vm86_trap(env, trapnr);
  266. } else
  267. #endif
  268. {
  269. /* division by zero */
  270. info.si_signo = SIGFPE;
  271. info.si_errno = 0;
  272. info.si_code = TARGET_FPE_INTDIV;
  273. info._sifields._sigfault._addr = env->eip;
  274. queue_signal(env, info.si_signo, &info);
  275. }
  276. break;
  277. case EXCP01_DB:
  278. case EXCP03_INT3:
  279. #ifndef TARGET_X86_64
  280. if (env->eflags & VM_MASK) {
  281. handle_vm86_trap(env, trapnr);
  282. } else
  283. #endif
  284. {
  285. info.si_signo = SIGTRAP;
  286. info.si_errno = 0;
  287. if (trapnr == EXCP01_DB) {
  288. info.si_code = TARGET_TRAP_BRKPT;
  289. info._sifields._sigfault._addr = env->eip;
  290. } else {
  291. info.si_code = TARGET_SI_KERNEL;
  292. info._sifields._sigfault._addr = 0;
  293. }
  294. queue_signal(env, info.si_signo, &info);
  295. }
  296. break;
  297. case EXCP04_INTO:
  298. case EXCP05_BOUND:
  299. #ifndef TARGET_X86_64
  300. if (env->eflags & VM_MASK) {
  301. handle_vm86_trap(env, trapnr);
  302. } else
  303. #endif
  304. {
  305. info.si_signo = SIGSEGV;
  306. info.si_errno = 0;
  307. info.si_code = TARGET_SI_KERNEL;
  308. info._sifields._sigfault._addr = 0;
  309. queue_signal(env, info.si_signo, &info);
  310. }
  311. break;
  312. case EXCP06_ILLOP:
  313. info.si_signo = SIGILL;
  314. info.si_errno = 0;
  315. info.si_code = TARGET_ILL_ILLOPN;
  316. info._sifields._sigfault._addr = env->eip;
  317. queue_signal(env, info.si_signo, &info);
  318. break;
  319. #endif
  320. case EXCP_INTERRUPT:
  321. /* just indicate that signals should be handled asap */
  322. break;
  323. #if 0
  324. case EXCP_DEBUG:
  325. {
  326. int sig;
  327. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  328. if (sig)
  329. {
  330. info.si_signo = sig;
  331. info.si_errno = 0;
  332. info.si_code = TARGET_TRAP_BRKPT;
  333. queue_signal(env, info.si_signo, &info);
  334. }
  335. }
  336. break;
  337. #endif
  338. default:
  339. pc = env->segs[R_CS].base + env->eip;
  340. fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
  341. (long)pc, trapnr);
  342. abort();
  343. }
  344. process_pending_signals(env);
  345. }
  346. }
  347. #endif
  348. #ifdef TARGET_SPARC
  349. #define SPARC64_STACK_BIAS 2047
  350. //#define DEBUG_WIN
  351. /* WARNING: dealing with register windows _is_ complicated. More info
  352. can be found at http://www.sics.se/~psm/sparcstack.html */
  353. static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
  354. {
  355. index = (index + cwp * 16) % (16 * env->nwindows);
  356. /* wrap handling : if cwp is on the last window, then we use the
  357. registers 'after' the end */
  358. if (index < 8 && env->cwp == env->nwindows - 1)
  359. index += 16 * env->nwindows;
  360. return index;
  361. }
  362. /* save the register window 'cwp1' */
  363. static inline void save_window_offset(CPUSPARCState *env, int cwp1)
  364. {
  365. unsigned int i;
  366. abi_ulong sp_ptr;
  367. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  368. #ifdef TARGET_SPARC64
  369. if (sp_ptr & 3)
  370. sp_ptr += SPARC64_STACK_BIAS;
  371. #endif
  372. #if defined(DEBUG_WIN)
  373. printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
  374. sp_ptr, cwp1);
  375. #endif
  376. for(i = 0; i < 16; i++) {
  377. /* FIXME - what to do if put_user() fails? */
  378. put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  379. sp_ptr += sizeof(abi_ulong);
  380. }
  381. }
  382. static void save_window(CPUSPARCState *env)
  383. {
  384. #ifndef TARGET_SPARC64
  385. unsigned int new_wim;
  386. new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
  387. ((1LL << env->nwindows) - 1);
  388. save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
  389. env->wim = new_wim;
  390. #else
  391. /*
  392. * cansave is zero if the spill trap handler is triggered by `save` and
  393. * nonzero if triggered by a `flushw`
  394. */
  395. save_window_offset(env, cpu_cwp_dec(env, env->cwp - env->cansave - 2));
  396. env->cansave++;
  397. env->canrestore--;
  398. #endif
  399. }
  400. static void restore_window(CPUSPARCState *env)
  401. {
  402. #ifndef TARGET_SPARC64
  403. unsigned int new_wim;
  404. #endif
  405. unsigned int i, cwp1;
  406. abi_ulong sp_ptr;
  407. #ifndef TARGET_SPARC64
  408. new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
  409. ((1LL << env->nwindows) - 1);
  410. #endif
  411. /* restore the invalid window */
  412. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  413. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  414. #ifdef TARGET_SPARC64
  415. if (sp_ptr & 3)
  416. sp_ptr += SPARC64_STACK_BIAS;
  417. #endif
  418. #if defined(DEBUG_WIN)
  419. printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
  420. sp_ptr, cwp1);
  421. #endif
  422. for(i = 0; i < 16; i++) {
  423. /* FIXME - what to do if get_user() fails? */
  424. get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  425. sp_ptr += sizeof(abi_ulong);
  426. }
  427. #ifdef TARGET_SPARC64
  428. env->canrestore++;
  429. if (env->cleanwin < env->nwindows - 1)
  430. env->cleanwin++;
  431. env->cansave--;
  432. #else
  433. env->wim = new_wim;
  434. #endif
  435. }
  436. static void flush_windows(CPUSPARCState *env)
  437. {
  438. int offset, cwp1;
  439. offset = 1;
  440. for(;;) {
  441. /* if restore would invoke restore_window(), then we can stop */
  442. cwp1 = cpu_cwp_inc(env, env->cwp + offset);
  443. #ifndef TARGET_SPARC64
  444. if (env->wim & (1 << cwp1))
  445. break;
  446. #else
  447. if (env->canrestore == 0)
  448. break;
  449. env->cansave++;
  450. env->canrestore--;
  451. #endif
  452. save_window_offset(env, cwp1);
  453. offset++;
  454. }
  455. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  456. #ifndef TARGET_SPARC64
  457. /* set wim so that restore will reload the registers */
  458. env->wim = 1 << cwp1;
  459. #endif
  460. #if defined(DEBUG_WIN)
  461. printf("flush_windows: nb=%d\n", offset - 1);
  462. #endif
  463. }
  464. void cpu_loop(CPUSPARCState *env)
  465. {
  466. CPUState *cs = env_cpu(env);
  467. int trapnr, ret, syscall_nr;
  468. //target_siginfo_t info;
  469. while (1) {
  470. cpu_exec_start(cs);
  471. trapnr = cpu_exec(cs);
  472. cpu_exec_end(cs);
  473. process_queued_cpu_work(cs);
  474. switch (trapnr) {
  475. #ifndef TARGET_SPARC64
  476. case 0x80:
  477. #else
  478. /* FreeBSD uses 0x141 for syscalls too */
  479. case 0x141:
  480. if (bsd_type != target_freebsd)
  481. goto badtrap;
  482. case 0x100:
  483. #endif
  484. syscall_nr = env->gregs[1];
  485. if (bsd_type == target_freebsd)
  486. ret = do_freebsd_syscall(env, syscall_nr,
  487. env->regwptr[0], env->regwptr[1],
  488. env->regwptr[2], env->regwptr[3],
  489. env->regwptr[4], env->regwptr[5], 0, 0);
  490. else if (bsd_type == target_netbsd)
  491. ret = do_netbsd_syscall(env, syscall_nr,
  492. env->regwptr[0], env->regwptr[1],
  493. env->regwptr[2], env->regwptr[3],
  494. env->regwptr[4], env->regwptr[5]);
  495. else { //if (bsd_type == target_openbsd)
  496. #if defined(TARGET_SPARC64)
  497. syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
  498. TARGET_OPENBSD_SYSCALL_G2RFLAG);
  499. #endif
  500. ret = do_openbsd_syscall(env, syscall_nr,
  501. env->regwptr[0], env->regwptr[1],
  502. env->regwptr[2], env->regwptr[3],
  503. env->regwptr[4], env->regwptr[5]);
  504. }
  505. if ((unsigned int)ret >= (unsigned int)(-515)) {
  506. ret = -ret;
  507. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  508. env->xcc |= PSR_CARRY;
  509. #else
  510. env->psr |= PSR_CARRY;
  511. #endif
  512. } else {
  513. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  514. env->xcc &= ~PSR_CARRY;
  515. #else
  516. env->psr &= ~PSR_CARRY;
  517. #endif
  518. }
  519. env->regwptr[0] = ret;
  520. /* next instruction */
  521. #if defined(TARGET_SPARC64)
  522. if (bsd_type == target_openbsd &&
  523. env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
  524. env->pc = env->gregs[2];
  525. env->npc = env->pc + 4;
  526. } else if (bsd_type == target_openbsd &&
  527. env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
  528. env->pc = env->gregs[7];
  529. env->npc = env->pc + 4;
  530. } else {
  531. env->pc = env->npc;
  532. env->npc = env->npc + 4;
  533. }
  534. #else
  535. env->pc = env->npc;
  536. env->npc = env->npc + 4;
  537. #endif
  538. break;
  539. case 0x83: /* flush windows */
  540. #ifdef TARGET_ABI32
  541. case 0x103:
  542. #endif
  543. flush_windows(env);
  544. /* next instruction */
  545. env->pc = env->npc;
  546. env->npc = env->npc + 4;
  547. break;
  548. #ifndef TARGET_SPARC64
  549. case TT_WIN_OVF: /* window overflow */
  550. save_window(env);
  551. break;
  552. case TT_WIN_UNF: /* window underflow */
  553. restore_window(env);
  554. break;
  555. case TT_TFAULT:
  556. case TT_DFAULT:
  557. #if 0
  558. {
  559. info.si_signo = SIGSEGV;
  560. info.si_errno = 0;
  561. /* XXX: check env->error_code */
  562. info.si_code = TARGET_SEGV_MAPERR;
  563. info._sifields._sigfault._addr = env->mmuregs[4];
  564. queue_signal(env, info.si_signo, &info);
  565. }
  566. #endif
  567. break;
  568. #else
  569. case TT_SPILL: /* window overflow */
  570. save_window(env);
  571. break;
  572. case TT_FILL: /* window underflow */
  573. restore_window(env);
  574. break;
  575. case TT_TFAULT:
  576. case TT_DFAULT:
  577. #if 0
  578. {
  579. info.si_signo = SIGSEGV;
  580. info.si_errno = 0;
  581. /* XXX: check env->error_code */
  582. info.si_code = TARGET_SEGV_MAPERR;
  583. if (trapnr == TT_DFAULT)
  584. info._sifields._sigfault._addr = env->dmmuregs[4];
  585. else
  586. info._sifields._sigfault._addr = env->tsptr->tpc;
  587. //queue_signal(env, info.si_signo, &info);
  588. }
  589. #endif
  590. break;
  591. #endif
  592. case EXCP_INTERRUPT:
  593. /* just indicate that signals should be handled asap */
  594. break;
  595. case EXCP_DEBUG:
  596. {
  597. #if 0
  598. int sig =
  599. #endif
  600. gdb_handlesig(cs, TARGET_SIGTRAP);
  601. #if 0
  602. if (sig)
  603. {
  604. info.si_signo = sig;
  605. info.si_errno = 0;
  606. info.si_code = TARGET_TRAP_BRKPT;
  607. //queue_signal(env, info.si_signo, &info);
  608. }
  609. #endif
  610. }
  611. break;
  612. default:
  613. #ifdef TARGET_SPARC64
  614. badtrap:
  615. #endif
  616. printf ("Unhandled trap: 0x%x\n", trapnr);
  617. cpu_dump_state(cs, stderr, 0);
  618. exit (1);
  619. }
  620. process_pending_signals (env);
  621. }
  622. }
  623. #endif
  624. static void usage(void)
  625. {
  626. printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
  627. "\n" QEMU_COPYRIGHT "\n"
  628. "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
  629. "BSD CPU emulator (compiled for %s emulation)\n"
  630. "\n"
  631. "Standard options:\n"
  632. "-h print this help\n"
  633. "-g port wait gdb connection to port\n"
  634. "-L path set the elf interpreter prefix (default=%s)\n"
  635. "-s size set the stack size in bytes (default=%ld)\n"
  636. "-cpu model select CPU (-cpu help for list)\n"
  637. "-drop-ld-preload drop LD_PRELOAD for target process\n"
  638. "-E var=value sets/modifies targets environment variable(s)\n"
  639. "-U var unsets targets environment variable(s)\n"
  640. "-B address set guest_base address to address\n"
  641. "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
  642. "\n"
  643. "Debug options:\n"
  644. "-d item1[,...] enable logging of specified items\n"
  645. " (use '-d help' for a list of log items)\n"
  646. "-D logfile write logs to 'logfile' (default stderr)\n"
  647. "-p pagesize set the host page size to 'pagesize'\n"
  648. "-singlestep always run in singlestep mode\n"
  649. "-strace log system calls\n"
  650. "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
  651. " specify tracing options\n"
  652. "\n"
  653. "Environment variables:\n"
  654. "QEMU_STRACE Print system calls and arguments similar to the\n"
  655. " 'strace' program. Enable by setting to any value.\n"
  656. "You can use -E and -U options to set/unset environment variables\n"
  657. "for target process. It is possible to provide several variables\n"
  658. "by repeating the option. For example:\n"
  659. " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
  660. "Note that if you provide several changes to single variable\n"
  661. "last change will stay in effect.\n"
  662. "\n"
  663. QEMU_HELP_BOTTOM "\n"
  664. ,
  665. TARGET_NAME,
  666. interp_prefix,
  667. x86_stack_size);
  668. exit(1);
  669. }
  670. THREAD CPUState *thread_cpu;
  671. bool qemu_cpu_is_self(CPUState *cpu)
  672. {
  673. return thread_cpu == cpu;
  674. }
  675. void qemu_cpu_kick(CPUState *cpu)
  676. {
  677. cpu_exit(cpu);
  678. }
  679. /* Assumes contents are already zeroed. */
  680. void init_task_state(TaskState *ts)
  681. {
  682. int i;
  683. ts->used = 1;
  684. ts->first_free = ts->sigqueue_table;
  685. for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
  686. ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
  687. }
  688. ts->sigqueue_table[i].next = NULL;
  689. }
  690. int main(int argc, char **argv)
  691. {
  692. const char *filename;
  693. const char *cpu_model;
  694. const char *cpu_type;
  695. const char *log_file = NULL;
  696. const char *log_mask = NULL;
  697. struct target_pt_regs regs1, *regs = &regs1;
  698. struct image_info info1, *info = &info1;
  699. TaskState ts1, *ts = &ts1;
  700. CPUArchState *env;
  701. CPUState *cpu;
  702. int optind;
  703. const char *r;
  704. const char *gdbstub = NULL;
  705. char **target_environ, **wrk;
  706. envlist_t *envlist = NULL;
  707. char *trace_file = NULL;
  708. bsd_type = target_openbsd;
  709. if (argc <= 1)
  710. usage();
  711. error_init(argv[0]);
  712. module_call_init(MODULE_INIT_TRACE);
  713. qemu_init_cpu_list();
  714. module_call_init(MODULE_INIT_QOM);
  715. envlist = envlist_create();
  716. /* add current environment into the list */
  717. for (wrk = environ; *wrk != NULL; wrk++) {
  718. (void) envlist_setenv(envlist, *wrk);
  719. }
  720. cpu_model = NULL;
  721. qemu_add_opts(&qemu_trace_opts);
  722. optind = 1;
  723. for (;;) {
  724. if (optind >= argc)
  725. break;
  726. r = argv[optind];
  727. if (r[0] != '-')
  728. break;
  729. optind++;
  730. r++;
  731. if (!strcmp(r, "-")) {
  732. break;
  733. } else if (!strcmp(r, "d")) {
  734. if (optind >= argc) {
  735. break;
  736. }
  737. log_mask = argv[optind++];
  738. } else if (!strcmp(r, "D")) {
  739. if (optind >= argc) {
  740. break;
  741. }
  742. log_file = argv[optind++];
  743. } else if (!strcmp(r, "E")) {
  744. r = argv[optind++];
  745. if (envlist_setenv(envlist, r) != 0)
  746. usage();
  747. } else if (!strcmp(r, "ignore-environment")) {
  748. envlist_free(envlist);
  749. envlist = envlist_create();
  750. } else if (!strcmp(r, "U")) {
  751. r = argv[optind++];
  752. if (envlist_unsetenv(envlist, r) != 0)
  753. usage();
  754. } else if (!strcmp(r, "s")) {
  755. r = argv[optind++];
  756. x86_stack_size = strtol(r, (char **)&r, 0);
  757. if (x86_stack_size <= 0)
  758. usage();
  759. if (*r == 'M')
  760. x86_stack_size *= MiB;
  761. else if (*r == 'k' || *r == 'K')
  762. x86_stack_size *= KiB;
  763. } else if (!strcmp(r, "L")) {
  764. interp_prefix = argv[optind++];
  765. } else if (!strcmp(r, "p")) {
  766. qemu_host_page_size = atoi(argv[optind++]);
  767. if (qemu_host_page_size == 0 ||
  768. (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
  769. fprintf(stderr, "page size must be a power of two\n");
  770. exit(1);
  771. }
  772. } else if (!strcmp(r, "g")) {
  773. gdbstub = g_strdup(argv[optind++]);
  774. } else if (!strcmp(r, "r")) {
  775. qemu_uname_release = argv[optind++];
  776. } else if (!strcmp(r, "cpu")) {
  777. cpu_model = argv[optind++];
  778. if (is_help_option(cpu_model)) {
  779. /* XXX: implement xxx_cpu_list for targets that still miss it */
  780. #if defined(cpu_list)
  781. cpu_list();
  782. #endif
  783. exit(1);
  784. }
  785. } else if (!strcmp(r, "B")) {
  786. guest_base = strtol(argv[optind++], NULL, 0);
  787. have_guest_base = true;
  788. } else if (!strcmp(r, "drop-ld-preload")) {
  789. (void) envlist_unsetenv(envlist, "LD_PRELOAD");
  790. } else if (!strcmp(r, "bsd")) {
  791. if (!strcasecmp(argv[optind], "freebsd")) {
  792. bsd_type = target_freebsd;
  793. } else if (!strcasecmp(argv[optind], "netbsd")) {
  794. bsd_type = target_netbsd;
  795. } else if (!strcasecmp(argv[optind], "openbsd")) {
  796. bsd_type = target_openbsd;
  797. } else {
  798. usage();
  799. }
  800. optind++;
  801. } else if (!strcmp(r, "singlestep")) {
  802. singlestep = 1;
  803. } else if (!strcmp(r, "strace")) {
  804. do_strace = 1;
  805. } else if (!strcmp(r, "trace")) {
  806. g_free(trace_file);
  807. trace_file = trace_opt_parse(optarg);
  808. } else {
  809. usage();
  810. }
  811. }
  812. /* init debug */
  813. qemu_log_needs_buffers();
  814. qemu_set_log_filename(log_file, &error_fatal);
  815. if (log_mask) {
  816. int mask;
  817. mask = qemu_str_to_log_mask(log_mask);
  818. if (!mask) {
  819. qemu_print_log_usage(stdout);
  820. exit(1);
  821. }
  822. qemu_set_log(mask);
  823. }
  824. if (optind >= argc) {
  825. usage();
  826. }
  827. filename = argv[optind];
  828. if (!trace_init_backends()) {
  829. exit(1);
  830. }
  831. trace_init_file(trace_file);
  832. /* Zero out regs */
  833. memset(regs, 0, sizeof(struct target_pt_regs));
  834. /* Zero out image_info */
  835. memset(info, 0, sizeof(struct image_info));
  836. /* Scan interp_prefix dir for replacement files. */
  837. init_paths(interp_prefix);
  838. if (cpu_model == NULL) {
  839. #if defined(TARGET_I386)
  840. #ifdef TARGET_X86_64
  841. cpu_model = "qemu64";
  842. #else
  843. cpu_model = "qemu32";
  844. #endif
  845. #elif defined(TARGET_SPARC)
  846. #ifdef TARGET_SPARC64
  847. cpu_model = "TI UltraSparc II";
  848. #else
  849. cpu_model = "Fujitsu MB86904";
  850. #endif
  851. #else
  852. cpu_model = "any";
  853. #endif
  854. }
  855. /* init tcg before creating CPUs and to get qemu_host_page_size */
  856. tcg_exec_init(0, false);
  857. cpu_type = parse_cpu_option(cpu_model);
  858. cpu = cpu_create(cpu_type);
  859. env = cpu->env_ptr;
  860. #if defined(TARGET_SPARC) || defined(TARGET_PPC)
  861. cpu_reset(cpu);
  862. #endif
  863. thread_cpu = cpu;
  864. if (getenv("QEMU_STRACE")) {
  865. do_strace = 1;
  866. }
  867. target_environ = envlist_to_environ(envlist, NULL);
  868. envlist_free(envlist);
  869. /*
  870. * Now that page sizes are configured in tcg_exec_init() we can do
  871. * proper page alignment for guest_base.
  872. */
  873. guest_base = HOST_PAGE_ALIGN(guest_base);
  874. /*
  875. * Read in mmap_min_addr kernel parameter. This value is used
  876. * When loading the ELF image to determine whether guest_base
  877. * is needed.
  878. *
  879. * When user has explicitly set the quest base, we skip this
  880. * test.
  881. */
  882. if (!have_guest_base) {
  883. FILE *fp;
  884. if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
  885. unsigned long tmp;
  886. if (fscanf(fp, "%lu", &tmp) == 1) {
  887. mmap_min_addr = tmp;
  888. qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
  889. }
  890. fclose(fp);
  891. }
  892. }
  893. if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
  894. printf("Error loading %s\n", filename);
  895. _exit(1);
  896. }
  897. for (wrk = target_environ; *wrk; wrk++) {
  898. g_free(*wrk);
  899. }
  900. g_free(target_environ);
  901. if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
  902. qemu_log("guest_base 0x%lx\n", guest_base);
  903. log_page_dump("binary load");
  904. qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
  905. qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
  906. qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
  907. info->start_code);
  908. qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
  909. info->start_data);
  910. qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
  911. qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
  912. info->start_stack);
  913. qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
  914. qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
  915. }
  916. target_set_brk(info->brk);
  917. syscall_init();
  918. signal_init();
  919. /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
  920. generating the prologue until now so that the prologue can take
  921. the real value of GUEST_BASE into account. */
  922. tcg_prologue_init(tcg_ctx);
  923. tcg_region_init();
  924. /* build Task State */
  925. memset(ts, 0, sizeof(TaskState));
  926. init_task_state(ts);
  927. ts->info = info;
  928. cpu->opaque = ts;
  929. #if defined(TARGET_I386)
  930. env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
  931. env->hflags |= HF_PE_MASK | HF_CPL_MASK;
  932. if (env->features[FEAT_1_EDX] & CPUID_SSE) {
  933. env->cr[4] |= CR4_OSFXSR_MASK;
  934. env->hflags |= HF_OSFXSR_MASK;
  935. }
  936. #ifndef TARGET_ABI32
  937. /* enable 64 bit mode if possible */
  938. if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
  939. fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
  940. exit(1);
  941. }
  942. env->cr[4] |= CR4_PAE_MASK;
  943. env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
  944. env->hflags |= HF_LMA_MASK;
  945. #endif
  946. /* flags setup : we activate the IRQs by default as in user mode */
  947. env->eflags |= IF_MASK;
  948. /* linux register setup */
  949. #ifndef TARGET_ABI32
  950. env->regs[R_EAX] = regs->rax;
  951. env->regs[R_EBX] = regs->rbx;
  952. env->regs[R_ECX] = regs->rcx;
  953. env->regs[R_EDX] = regs->rdx;
  954. env->regs[R_ESI] = regs->rsi;
  955. env->regs[R_EDI] = regs->rdi;
  956. env->regs[R_EBP] = regs->rbp;
  957. env->regs[R_ESP] = regs->rsp;
  958. env->eip = regs->rip;
  959. #else
  960. env->regs[R_EAX] = regs->eax;
  961. env->regs[R_EBX] = regs->ebx;
  962. env->regs[R_ECX] = regs->ecx;
  963. env->regs[R_EDX] = regs->edx;
  964. env->regs[R_ESI] = regs->esi;
  965. env->regs[R_EDI] = regs->edi;
  966. env->regs[R_EBP] = regs->ebp;
  967. env->regs[R_ESP] = regs->esp;
  968. env->eip = regs->eip;
  969. #endif
  970. /* linux interrupt setup */
  971. #ifndef TARGET_ABI32
  972. env->idt.limit = 511;
  973. #else
  974. env->idt.limit = 255;
  975. #endif
  976. env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
  977. PROT_READ|PROT_WRITE,
  978. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  979. idt_table = g2h(env->idt.base);
  980. set_idt(0, 0);
  981. set_idt(1, 0);
  982. set_idt(2, 0);
  983. set_idt(3, 3);
  984. set_idt(4, 3);
  985. set_idt(5, 0);
  986. set_idt(6, 0);
  987. set_idt(7, 0);
  988. set_idt(8, 0);
  989. set_idt(9, 0);
  990. set_idt(10, 0);
  991. set_idt(11, 0);
  992. set_idt(12, 0);
  993. set_idt(13, 0);
  994. set_idt(14, 0);
  995. set_idt(15, 0);
  996. set_idt(16, 0);
  997. set_idt(17, 0);
  998. set_idt(18, 0);
  999. set_idt(19, 0);
  1000. set_idt(0x80, 3);
  1001. /* linux segment setup */
  1002. {
  1003. uint64_t *gdt_table;
  1004. env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
  1005. PROT_READ|PROT_WRITE,
  1006. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  1007. env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
  1008. gdt_table = g2h(env->gdt.base);
  1009. #ifdef TARGET_ABI32
  1010. write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
  1011. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  1012. (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
  1013. #else
  1014. /* 64 bit code segment */
  1015. write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
  1016. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  1017. DESC_L_MASK |
  1018. (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
  1019. #endif
  1020. write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
  1021. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  1022. (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
  1023. }
  1024. cpu_x86_load_seg(env, R_CS, __USER_CS);
  1025. cpu_x86_load_seg(env, R_SS, __USER_DS);
  1026. #ifdef TARGET_ABI32
  1027. cpu_x86_load_seg(env, R_DS, __USER_DS);
  1028. cpu_x86_load_seg(env, R_ES, __USER_DS);
  1029. cpu_x86_load_seg(env, R_FS, __USER_DS);
  1030. cpu_x86_load_seg(env, R_GS, __USER_DS);
  1031. /* This hack makes Wine work... */
  1032. env->segs[R_FS].selector = 0;
  1033. #else
  1034. cpu_x86_load_seg(env, R_DS, 0);
  1035. cpu_x86_load_seg(env, R_ES, 0);
  1036. cpu_x86_load_seg(env, R_FS, 0);
  1037. cpu_x86_load_seg(env, R_GS, 0);
  1038. #endif
  1039. #elif defined(TARGET_SPARC)
  1040. {
  1041. int i;
  1042. env->pc = regs->pc;
  1043. env->npc = regs->npc;
  1044. env->y = regs->y;
  1045. for(i = 0; i < 8; i++)
  1046. env->gregs[i] = regs->u_regs[i];
  1047. for(i = 0; i < 8; i++)
  1048. env->regwptr[i] = regs->u_regs[i + 8];
  1049. }
  1050. #else
  1051. #error unsupported target CPU
  1052. #endif
  1053. if (gdbstub) {
  1054. gdbserver_start(gdbstub);
  1055. gdb_handlesig(cpu, 0);
  1056. }
  1057. cpu_loop(env);
  1058. /* never exits */
  1059. return 0;
  1060. }