main.c 35 KB

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