main.c 35 KB

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