main.c 35 KB

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