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