main.c 35 KB

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