2
0

main.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691
  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, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. * MA 02110-1301, USA.
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <unistd.h>
  27. #include <sys/mman.h>
  28. #include "qemu.h"
  29. #include "qemu-common.h"
  30. #include "cache-utils.h"
  31. /* For tb_lock */
  32. #include "exec-all.h"
  33. #include "envlist.h"
  34. #define DEBUG_LOGFILE "/tmp/qemu.log"
  35. char *exec_path;
  36. static const char *interp_prefix = CONFIG_QEMU_PREFIX;
  37. const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
  38. #if defined(__i386__) && !defined(CONFIG_STATIC)
  39. /* Force usage of an ELF interpreter even if it is an ELF shared
  40. object ! */
  41. const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
  42. #endif
  43. /* for recent libc, we add these dummy symbols which are not declared
  44. when generating a linked object (bug in ld ?) */
  45. #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined(CONFIG_STATIC)
  46. asm(".globl __preinit_array_start\n"
  47. ".globl __preinit_array_end\n"
  48. ".globl __init_array_start\n"
  49. ".globl __init_array_end\n"
  50. ".globl __fini_array_start\n"
  51. ".globl __fini_array_end\n"
  52. ".section \".rodata\"\n"
  53. "__preinit_array_start:\n"
  54. "__preinit_array_end:\n"
  55. "__init_array_start:\n"
  56. "__init_array_end:\n"
  57. "__fini_array_start:\n"
  58. "__fini_array_end:\n"
  59. ".long 0\n"
  60. ".previous\n");
  61. #endif
  62. /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
  63. we allocate a bigger stack. Need a better solution, for example
  64. by remapping the process stack directly at the right place */
  65. unsigned long x86_stack_size = 512 * 1024;
  66. void gemu_log(const char *fmt, ...)
  67. {
  68. va_list ap;
  69. va_start(ap, fmt);
  70. vfprintf(stderr, fmt, ap);
  71. va_end(ap);
  72. }
  73. void cpu_outb(CPUState *env, int addr, int val)
  74. {
  75. fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
  76. }
  77. void cpu_outw(CPUState *env, int addr, int val)
  78. {
  79. fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
  80. }
  81. void cpu_outl(CPUState *env, int addr, int val)
  82. {
  83. fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
  84. }
  85. int cpu_inb(CPUState *env, int addr)
  86. {
  87. fprintf(stderr, "inb: port=0x%04x\n", addr);
  88. return 0;
  89. }
  90. int cpu_inw(CPUState *env, int addr)
  91. {
  92. fprintf(stderr, "inw: port=0x%04x\n", addr);
  93. return 0;
  94. }
  95. int cpu_inl(CPUState *env, int addr)
  96. {
  97. fprintf(stderr, "inl: port=0x%04x\n", addr);
  98. return 0;
  99. }
  100. #if defined(TARGET_I386)
  101. int cpu_get_pic_interrupt(CPUState *env)
  102. {
  103. return -1;
  104. }
  105. #endif
  106. /* timers for rdtsc */
  107. #if 0
  108. static uint64_t emu_time;
  109. int64_t cpu_get_real_ticks(void)
  110. {
  111. return emu_time++;
  112. }
  113. #endif
  114. #if defined(USE_NPTL)
  115. /***********************************************************/
  116. /* Helper routines for implementing atomic operations. */
  117. /* To implement exclusive operations we force all cpus to syncronise.
  118. We don't require a full sync, only that no cpus are executing guest code.
  119. The alternative is to map target atomic ops onto host equivalents,
  120. which requires quite a lot of per host/target work. */
  121. static pthread_mutex_t exclusive_lock = PTHREAD_MUTEX_INITIALIZER;
  122. static pthread_cond_t exclusive_cond = PTHREAD_COND_INITIALIZER;
  123. static pthread_cond_t exclusive_resume = PTHREAD_COND_INITIALIZER;
  124. static int pending_cpus;
  125. /* Make sure everything is in a consistent state for calling fork(). */
  126. void fork_start(void)
  127. {
  128. mmap_fork_start();
  129. pthread_mutex_lock(&tb_lock);
  130. pthread_mutex_lock(&exclusive_lock);
  131. }
  132. void fork_end(int child)
  133. {
  134. if (child) {
  135. /* Child processes created by fork() only have a single thread.
  136. Discard information about the parent threads. */
  137. first_cpu = thread_env;
  138. thread_env->next_cpu = NULL;
  139. pending_cpus = 0;
  140. pthread_mutex_init(&exclusive_lock, NULL);
  141. pthread_cond_init(&exclusive_cond, NULL);
  142. pthread_cond_init(&exclusive_resume, NULL);
  143. pthread_mutex_init(&tb_lock, NULL);
  144. gdbserver_fork(thread_env);
  145. } else {
  146. pthread_mutex_unlock(&exclusive_lock);
  147. pthread_mutex_unlock(&tb_lock);
  148. }
  149. mmap_fork_end(child);
  150. }
  151. /* Wait for pending exclusive operations to complete. The exclusive lock
  152. must be held. */
  153. static inline void exclusive_idle(void)
  154. {
  155. while (pending_cpus) {
  156. pthread_cond_wait(&exclusive_resume, &exclusive_lock);
  157. }
  158. }
  159. /* Start an exclusive operation.
  160. Must only be called from outside cpu_arm_exec. */
  161. static inline void start_exclusive(void)
  162. {
  163. CPUState *other;
  164. pthread_mutex_lock(&exclusive_lock);
  165. exclusive_idle();
  166. pending_cpus = 1;
  167. /* Make all other cpus stop executing. */
  168. for (other = first_cpu; other; other = other->next_cpu) {
  169. if (other->running) {
  170. pending_cpus++;
  171. cpu_interrupt(other, CPU_INTERRUPT_EXIT);
  172. }
  173. }
  174. if (pending_cpus > 1) {
  175. pthread_cond_wait(&exclusive_cond, &exclusive_lock);
  176. }
  177. }
  178. /* Finish an exclusive operation. */
  179. static inline void end_exclusive(void)
  180. {
  181. pending_cpus = 0;
  182. pthread_cond_broadcast(&exclusive_resume);
  183. pthread_mutex_unlock(&exclusive_lock);
  184. }
  185. /* Wait for exclusive ops to finish, and begin cpu execution. */
  186. static inline void cpu_exec_start(CPUState *env)
  187. {
  188. pthread_mutex_lock(&exclusive_lock);
  189. exclusive_idle();
  190. env->running = 1;
  191. pthread_mutex_unlock(&exclusive_lock);
  192. }
  193. /* Mark cpu as not executing, and release pending exclusive ops. */
  194. static inline void cpu_exec_end(CPUState *env)
  195. {
  196. pthread_mutex_lock(&exclusive_lock);
  197. env->running = 0;
  198. if (pending_cpus > 1) {
  199. pending_cpus--;
  200. if (pending_cpus == 1) {
  201. pthread_cond_signal(&exclusive_cond);
  202. }
  203. }
  204. exclusive_idle();
  205. pthread_mutex_unlock(&exclusive_lock);
  206. }
  207. #else /* if !USE_NPTL */
  208. /* These are no-ops because we are not threadsafe. */
  209. static inline void cpu_exec_start(CPUState *env)
  210. {
  211. }
  212. static inline void cpu_exec_end(CPUState *env)
  213. {
  214. }
  215. static inline void start_exclusive(void)
  216. {
  217. }
  218. static inline void end_exclusive(void)
  219. {
  220. }
  221. void fork_start(void)
  222. {
  223. }
  224. void fork_end(int child)
  225. {
  226. if (child) {
  227. gdbserver_fork(thread_env);
  228. }
  229. }
  230. #endif
  231. #ifdef TARGET_I386
  232. /***********************************************************/
  233. /* CPUX86 core interface */
  234. void cpu_smm_update(CPUState *env)
  235. {
  236. }
  237. uint64_t cpu_get_tsc(CPUX86State *env)
  238. {
  239. return cpu_get_real_ticks();
  240. }
  241. static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
  242. int flags)
  243. {
  244. unsigned int e1, e2;
  245. uint32_t *p;
  246. e1 = (addr << 16) | (limit & 0xffff);
  247. e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
  248. e2 |= flags;
  249. p = ptr;
  250. p[0] = tswap32(e1);
  251. p[1] = tswap32(e2);
  252. }
  253. static uint64_t *idt_table;
  254. #ifdef TARGET_X86_64
  255. static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
  256. uint64_t addr, unsigned int sel)
  257. {
  258. uint32_t *p, e1, e2;
  259. e1 = (addr & 0xffff) | (sel << 16);
  260. e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
  261. p = ptr;
  262. p[0] = tswap32(e1);
  263. p[1] = tswap32(e2);
  264. p[2] = tswap32(addr >> 32);
  265. p[3] = 0;
  266. }
  267. /* only dpl matters as we do only user space emulation */
  268. static void set_idt(int n, unsigned int dpl)
  269. {
  270. set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
  271. }
  272. #else
  273. static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
  274. uint32_t addr, unsigned int sel)
  275. {
  276. uint32_t *p, e1, e2;
  277. e1 = (addr & 0xffff) | (sel << 16);
  278. e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
  279. p = ptr;
  280. p[0] = tswap32(e1);
  281. p[1] = tswap32(e2);
  282. }
  283. /* only dpl matters as we do only user space emulation */
  284. static void set_idt(int n, unsigned int dpl)
  285. {
  286. set_gate(idt_table + n, 0, dpl, 0, 0);
  287. }
  288. #endif
  289. void cpu_loop(CPUX86State *env)
  290. {
  291. int trapnr;
  292. abi_ulong pc;
  293. target_siginfo_t info;
  294. for(;;) {
  295. trapnr = cpu_x86_exec(env);
  296. switch(trapnr) {
  297. case 0x80:
  298. /* linux syscall from int $0x80 */
  299. env->regs[R_EAX] = do_syscall(env,
  300. env->regs[R_EAX],
  301. env->regs[R_EBX],
  302. env->regs[R_ECX],
  303. env->regs[R_EDX],
  304. env->regs[R_ESI],
  305. env->regs[R_EDI],
  306. env->regs[R_EBP]);
  307. break;
  308. #ifndef TARGET_ABI32
  309. case EXCP_SYSCALL:
  310. /* linux syscall from syscall intruction */
  311. env->regs[R_EAX] = do_syscall(env,
  312. env->regs[R_EAX],
  313. env->regs[R_EDI],
  314. env->regs[R_ESI],
  315. env->regs[R_EDX],
  316. env->regs[10],
  317. env->regs[8],
  318. env->regs[9]);
  319. env->eip = env->exception_next_eip;
  320. break;
  321. #endif
  322. case EXCP0B_NOSEG:
  323. case EXCP0C_STACK:
  324. info.si_signo = SIGBUS;
  325. info.si_errno = 0;
  326. info.si_code = TARGET_SI_KERNEL;
  327. info._sifields._sigfault._addr = 0;
  328. queue_signal(env, info.si_signo, &info);
  329. break;
  330. case EXCP0D_GPF:
  331. /* XXX: potential problem if ABI32 */
  332. #ifndef TARGET_X86_64
  333. if (env->eflags & VM_MASK) {
  334. handle_vm86_fault(env);
  335. } else
  336. #endif
  337. {
  338. info.si_signo = SIGSEGV;
  339. info.si_errno = 0;
  340. info.si_code = TARGET_SI_KERNEL;
  341. info._sifields._sigfault._addr = 0;
  342. queue_signal(env, info.si_signo, &info);
  343. }
  344. break;
  345. case EXCP0E_PAGE:
  346. info.si_signo = SIGSEGV;
  347. info.si_errno = 0;
  348. if (!(env->error_code & 1))
  349. info.si_code = TARGET_SEGV_MAPERR;
  350. else
  351. info.si_code = TARGET_SEGV_ACCERR;
  352. info._sifields._sigfault._addr = env->cr[2];
  353. queue_signal(env, info.si_signo, &info);
  354. break;
  355. case EXCP00_DIVZ:
  356. #ifndef TARGET_X86_64
  357. if (env->eflags & VM_MASK) {
  358. handle_vm86_trap(env, trapnr);
  359. } else
  360. #endif
  361. {
  362. /* division by zero */
  363. info.si_signo = SIGFPE;
  364. info.si_errno = 0;
  365. info.si_code = TARGET_FPE_INTDIV;
  366. info._sifields._sigfault._addr = env->eip;
  367. queue_signal(env, info.si_signo, &info);
  368. }
  369. break;
  370. case EXCP01_DB:
  371. case EXCP03_INT3:
  372. #ifndef TARGET_X86_64
  373. if (env->eflags & VM_MASK) {
  374. handle_vm86_trap(env, trapnr);
  375. } else
  376. #endif
  377. {
  378. info.si_signo = SIGTRAP;
  379. info.si_errno = 0;
  380. if (trapnr == EXCP01_DB) {
  381. info.si_code = TARGET_TRAP_BRKPT;
  382. info._sifields._sigfault._addr = env->eip;
  383. } else {
  384. info.si_code = TARGET_SI_KERNEL;
  385. info._sifields._sigfault._addr = 0;
  386. }
  387. queue_signal(env, info.si_signo, &info);
  388. }
  389. break;
  390. case EXCP04_INTO:
  391. case EXCP05_BOUND:
  392. #ifndef TARGET_X86_64
  393. if (env->eflags & VM_MASK) {
  394. handle_vm86_trap(env, trapnr);
  395. } else
  396. #endif
  397. {
  398. info.si_signo = SIGSEGV;
  399. info.si_errno = 0;
  400. info.si_code = TARGET_SI_KERNEL;
  401. info._sifields._sigfault._addr = 0;
  402. queue_signal(env, info.si_signo, &info);
  403. }
  404. break;
  405. case EXCP06_ILLOP:
  406. info.si_signo = SIGILL;
  407. info.si_errno = 0;
  408. info.si_code = TARGET_ILL_ILLOPN;
  409. info._sifields._sigfault._addr = env->eip;
  410. queue_signal(env, info.si_signo, &info);
  411. break;
  412. case EXCP_INTERRUPT:
  413. /* just indicate that signals should be handled asap */
  414. break;
  415. case EXCP_DEBUG:
  416. {
  417. int sig;
  418. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  419. if (sig)
  420. {
  421. info.si_signo = sig;
  422. info.si_errno = 0;
  423. info.si_code = TARGET_TRAP_BRKPT;
  424. queue_signal(env, info.si_signo, &info);
  425. }
  426. }
  427. break;
  428. default:
  429. pc = env->segs[R_CS].base + env->eip;
  430. fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
  431. (long)pc, trapnr);
  432. abort();
  433. }
  434. process_pending_signals(env);
  435. }
  436. }
  437. #endif
  438. #ifdef TARGET_ARM
  439. static void arm_cache_flush(abi_ulong start, abi_ulong last)
  440. {
  441. abi_ulong addr, last1;
  442. if (last < start)
  443. return;
  444. addr = start;
  445. for(;;) {
  446. last1 = ((addr + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK) - 1;
  447. if (last1 > last)
  448. last1 = last;
  449. tb_invalidate_page_range(addr, last1 + 1);
  450. if (last1 == last)
  451. break;
  452. addr = last1 + 1;
  453. }
  454. }
  455. /* Handle a jump to the kernel code page. */
  456. static int
  457. do_kernel_trap(CPUARMState *env)
  458. {
  459. uint32_t addr;
  460. uint32_t cpsr;
  461. uint32_t val;
  462. switch (env->regs[15]) {
  463. case 0xffff0fa0: /* __kernel_memory_barrier */
  464. /* ??? No-op. Will need to do better for SMP. */
  465. break;
  466. case 0xffff0fc0: /* __kernel_cmpxchg */
  467. /* XXX: This only works between threads, not between processes.
  468. It's probably possible to implement this with native host
  469. operations. However things like ldrex/strex are much harder so
  470. there's not much point trying. */
  471. start_exclusive();
  472. cpsr = cpsr_read(env);
  473. addr = env->regs[2];
  474. /* FIXME: This should SEGV if the access fails. */
  475. if (get_user_u32(val, addr))
  476. val = ~env->regs[0];
  477. if (val == env->regs[0]) {
  478. val = env->regs[1];
  479. /* FIXME: Check for segfaults. */
  480. put_user_u32(val, addr);
  481. env->regs[0] = 0;
  482. cpsr |= CPSR_C;
  483. } else {
  484. env->regs[0] = -1;
  485. cpsr &= ~CPSR_C;
  486. }
  487. cpsr_write(env, cpsr, CPSR_C);
  488. end_exclusive();
  489. break;
  490. case 0xffff0fe0: /* __kernel_get_tls */
  491. env->regs[0] = env->cp15.c13_tls2;
  492. break;
  493. default:
  494. return 1;
  495. }
  496. /* Jump back to the caller. */
  497. addr = env->regs[14];
  498. if (addr & 1) {
  499. env->thumb = 1;
  500. addr &= ~1;
  501. }
  502. env->regs[15] = addr;
  503. return 0;
  504. }
  505. void cpu_loop(CPUARMState *env)
  506. {
  507. int trapnr;
  508. unsigned int n, insn;
  509. target_siginfo_t info;
  510. uint32_t addr;
  511. for(;;) {
  512. cpu_exec_start(env);
  513. trapnr = cpu_arm_exec(env);
  514. cpu_exec_end(env);
  515. switch(trapnr) {
  516. case EXCP_UDEF:
  517. {
  518. TaskState *ts = env->opaque;
  519. uint32_t opcode;
  520. int rc;
  521. /* we handle the FPU emulation here, as Linux */
  522. /* we get the opcode */
  523. /* FIXME - what to do if get_user() fails? */
  524. get_user_u32(opcode, env->regs[15]);
  525. rc = EmulateAll(opcode, &ts->fpa, env);
  526. if (rc == 0) { /* illegal instruction */
  527. info.si_signo = SIGILL;
  528. info.si_errno = 0;
  529. info.si_code = TARGET_ILL_ILLOPN;
  530. info._sifields._sigfault._addr = env->regs[15];
  531. queue_signal(env, info.si_signo, &info);
  532. } else if (rc < 0) { /* FP exception */
  533. int arm_fpe=0;
  534. /* translate softfloat flags to FPSR flags */
  535. if (-rc & float_flag_invalid)
  536. arm_fpe |= BIT_IOC;
  537. if (-rc & float_flag_divbyzero)
  538. arm_fpe |= BIT_DZC;
  539. if (-rc & float_flag_overflow)
  540. arm_fpe |= BIT_OFC;
  541. if (-rc & float_flag_underflow)
  542. arm_fpe |= BIT_UFC;
  543. if (-rc & float_flag_inexact)
  544. arm_fpe |= BIT_IXC;
  545. FPSR fpsr = ts->fpa.fpsr;
  546. //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
  547. if (fpsr & (arm_fpe << 16)) { /* exception enabled? */
  548. info.si_signo = SIGFPE;
  549. info.si_errno = 0;
  550. /* ordered by priority, least first */
  551. if (arm_fpe & BIT_IXC) info.si_code = TARGET_FPE_FLTRES;
  552. if (arm_fpe & BIT_UFC) info.si_code = TARGET_FPE_FLTUND;
  553. if (arm_fpe & BIT_OFC) info.si_code = TARGET_FPE_FLTOVF;
  554. if (arm_fpe & BIT_DZC) info.si_code = TARGET_FPE_FLTDIV;
  555. if (arm_fpe & BIT_IOC) info.si_code = TARGET_FPE_FLTINV;
  556. info._sifields._sigfault._addr = env->regs[15];
  557. queue_signal(env, info.si_signo, &info);
  558. } else {
  559. env->regs[15] += 4;
  560. }
  561. /* accumulate unenabled exceptions */
  562. if ((!(fpsr & BIT_IXE)) && (arm_fpe & BIT_IXC))
  563. fpsr |= BIT_IXC;
  564. if ((!(fpsr & BIT_UFE)) && (arm_fpe & BIT_UFC))
  565. fpsr |= BIT_UFC;
  566. if ((!(fpsr & BIT_OFE)) && (arm_fpe & BIT_OFC))
  567. fpsr |= BIT_OFC;
  568. if ((!(fpsr & BIT_DZE)) && (arm_fpe & BIT_DZC))
  569. fpsr |= BIT_DZC;
  570. if ((!(fpsr & BIT_IOE)) && (arm_fpe & BIT_IOC))
  571. fpsr |= BIT_IOC;
  572. ts->fpa.fpsr=fpsr;
  573. } else { /* everything OK */
  574. /* increment PC */
  575. env->regs[15] += 4;
  576. }
  577. }
  578. break;
  579. case EXCP_SWI:
  580. case EXCP_BKPT:
  581. {
  582. env->eabi = 1;
  583. /* system call */
  584. if (trapnr == EXCP_BKPT) {
  585. if (env->thumb) {
  586. /* FIXME - what to do if get_user() fails? */
  587. get_user_u16(insn, env->regs[15]);
  588. n = insn & 0xff;
  589. env->regs[15] += 2;
  590. } else {
  591. /* FIXME - what to do if get_user() fails? */
  592. get_user_u32(insn, env->regs[15]);
  593. n = (insn & 0xf) | ((insn >> 4) & 0xff0);
  594. env->regs[15] += 4;
  595. }
  596. } else {
  597. if (env->thumb) {
  598. /* FIXME - what to do if get_user() fails? */
  599. get_user_u16(insn, env->regs[15] - 2);
  600. n = insn & 0xff;
  601. } else {
  602. /* FIXME - what to do if get_user() fails? */
  603. get_user_u32(insn, env->regs[15] - 4);
  604. n = insn & 0xffffff;
  605. }
  606. }
  607. if (n == ARM_NR_cacheflush) {
  608. arm_cache_flush(env->regs[0], env->regs[1]);
  609. } else if (n == ARM_NR_semihosting
  610. || n == ARM_NR_thumb_semihosting) {
  611. env->regs[0] = do_arm_semihosting (env);
  612. } else if (n == 0 || n >= ARM_SYSCALL_BASE
  613. || (env->thumb && n == ARM_THUMB_SYSCALL)) {
  614. /* linux syscall */
  615. if (env->thumb || n == 0) {
  616. n = env->regs[7];
  617. } else {
  618. n -= ARM_SYSCALL_BASE;
  619. env->eabi = 0;
  620. }
  621. if ( n > ARM_NR_BASE) {
  622. switch (n) {
  623. case ARM_NR_cacheflush:
  624. arm_cache_flush(env->regs[0], env->regs[1]);
  625. break;
  626. case ARM_NR_set_tls:
  627. cpu_set_tls(env, env->regs[0]);
  628. env->regs[0] = 0;
  629. break;
  630. default:
  631. gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
  632. n);
  633. env->regs[0] = -TARGET_ENOSYS;
  634. break;
  635. }
  636. } else {
  637. env->regs[0] = do_syscall(env,
  638. n,
  639. env->regs[0],
  640. env->regs[1],
  641. env->regs[2],
  642. env->regs[3],
  643. env->regs[4],
  644. env->regs[5]);
  645. }
  646. } else {
  647. goto error;
  648. }
  649. }
  650. break;
  651. case EXCP_INTERRUPT:
  652. /* just indicate that signals should be handled asap */
  653. break;
  654. case EXCP_PREFETCH_ABORT:
  655. addr = env->cp15.c6_insn;
  656. goto do_segv;
  657. case EXCP_DATA_ABORT:
  658. addr = env->cp15.c6_data;
  659. goto do_segv;
  660. do_segv:
  661. {
  662. info.si_signo = SIGSEGV;
  663. info.si_errno = 0;
  664. /* XXX: check env->error_code */
  665. info.si_code = TARGET_SEGV_MAPERR;
  666. info._sifields._sigfault._addr = addr;
  667. queue_signal(env, info.si_signo, &info);
  668. }
  669. break;
  670. case EXCP_DEBUG:
  671. {
  672. int sig;
  673. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  674. if (sig)
  675. {
  676. info.si_signo = sig;
  677. info.si_errno = 0;
  678. info.si_code = TARGET_TRAP_BRKPT;
  679. queue_signal(env, info.si_signo, &info);
  680. }
  681. }
  682. break;
  683. case EXCP_KERNEL_TRAP:
  684. if (do_kernel_trap(env))
  685. goto error;
  686. break;
  687. default:
  688. error:
  689. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
  690. trapnr);
  691. cpu_dump_state(env, stderr, fprintf, 0);
  692. abort();
  693. }
  694. process_pending_signals(env);
  695. }
  696. }
  697. #endif
  698. #ifdef TARGET_SPARC
  699. #define SPARC64_STACK_BIAS 2047
  700. //#define DEBUG_WIN
  701. /* WARNING: dealing with register windows _is_ complicated. More info
  702. can be found at http://www.sics.se/~psm/sparcstack.html */
  703. static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
  704. {
  705. index = (index + cwp * 16) % (16 * env->nwindows);
  706. /* wrap handling : if cwp is on the last window, then we use the
  707. registers 'after' the end */
  708. if (index < 8 && env->cwp == env->nwindows - 1)
  709. index += 16 * env->nwindows;
  710. return index;
  711. }
  712. /* save the register window 'cwp1' */
  713. static inline void save_window_offset(CPUSPARCState *env, int cwp1)
  714. {
  715. unsigned int i;
  716. abi_ulong sp_ptr;
  717. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  718. #ifdef TARGET_SPARC64
  719. if (sp_ptr & 3)
  720. sp_ptr += SPARC64_STACK_BIAS;
  721. #endif
  722. #if defined(DEBUG_WIN)
  723. printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
  724. sp_ptr, cwp1);
  725. #endif
  726. for(i = 0; i < 16; i++) {
  727. /* FIXME - what to do if put_user() fails? */
  728. put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  729. sp_ptr += sizeof(abi_ulong);
  730. }
  731. }
  732. static void save_window(CPUSPARCState *env)
  733. {
  734. #ifndef TARGET_SPARC64
  735. unsigned int new_wim;
  736. new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
  737. ((1LL << env->nwindows) - 1);
  738. save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
  739. env->wim = new_wim;
  740. #else
  741. save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
  742. env->cansave++;
  743. env->canrestore--;
  744. #endif
  745. }
  746. static void restore_window(CPUSPARCState *env)
  747. {
  748. #ifndef TARGET_SPARC64
  749. unsigned int new_wim;
  750. #endif
  751. unsigned int i, cwp1;
  752. abi_ulong sp_ptr;
  753. #ifndef TARGET_SPARC64
  754. new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
  755. ((1LL << env->nwindows) - 1);
  756. #endif
  757. /* restore the invalid window */
  758. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  759. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  760. #ifdef TARGET_SPARC64
  761. if (sp_ptr & 3)
  762. sp_ptr += SPARC64_STACK_BIAS;
  763. #endif
  764. #if defined(DEBUG_WIN)
  765. printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
  766. sp_ptr, cwp1);
  767. #endif
  768. for(i = 0; i < 16; i++) {
  769. /* FIXME - what to do if get_user() fails? */
  770. get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  771. sp_ptr += sizeof(abi_ulong);
  772. }
  773. #ifdef TARGET_SPARC64
  774. env->canrestore++;
  775. if (env->cleanwin < env->nwindows - 1)
  776. env->cleanwin++;
  777. env->cansave--;
  778. #else
  779. env->wim = new_wim;
  780. #endif
  781. }
  782. static void flush_windows(CPUSPARCState *env)
  783. {
  784. int offset, cwp1;
  785. offset = 1;
  786. for(;;) {
  787. /* if restore would invoke restore_window(), then we can stop */
  788. cwp1 = cpu_cwp_inc(env, env->cwp + offset);
  789. #ifndef TARGET_SPARC64
  790. if (env->wim & (1 << cwp1))
  791. break;
  792. #else
  793. if (env->canrestore == 0)
  794. break;
  795. env->cansave++;
  796. env->canrestore--;
  797. #endif
  798. save_window_offset(env, cwp1);
  799. offset++;
  800. }
  801. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  802. #ifndef TARGET_SPARC64
  803. /* set wim so that restore will reload the registers */
  804. env->wim = 1 << cwp1;
  805. #endif
  806. #if defined(DEBUG_WIN)
  807. printf("flush_windows: nb=%d\n", offset - 1);
  808. #endif
  809. }
  810. void cpu_loop (CPUSPARCState *env)
  811. {
  812. int trapnr, ret;
  813. target_siginfo_t info;
  814. while (1) {
  815. trapnr = cpu_sparc_exec (env);
  816. switch (trapnr) {
  817. #ifndef TARGET_SPARC64
  818. case 0x88:
  819. case 0x90:
  820. #else
  821. case 0x110:
  822. case 0x16d:
  823. #endif
  824. ret = do_syscall (env, env->gregs[1],
  825. env->regwptr[0], env->regwptr[1],
  826. env->regwptr[2], env->regwptr[3],
  827. env->regwptr[4], env->regwptr[5]);
  828. if ((unsigned int)ret >= (unsigned int)(-515)) {
  829. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  830. env->xcc |= PSR_CARRY;
  831. #else
  832. env->psr |= PSR_CARRY;
  833. #endif
  834. ret = -ret;
  835. } else {
  836. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  837. env->xcc &= ~PSR_CARRY;
  838. #else
  839. env->psr &= ~PSR_CARRY;
  840. #endif
  841. }
  842. env->regwptr[0] = ret;
  843. /* next instruction */
  844. env->pc = env->npc;
  845. env->npc = env->npc + 4;
  846. break;
  847. case 0x83: /* flush windows */
  848. #ifdef TARGET_ABI32
  849. case 0x103:
  850. #endif
  851. flush_windows(env);
  852. /* next instruction */
  853. env->pc = env->npc;
  854. env->npc = env->npc + 4;
  855. break;
  856. #ifndef TARGET_SPARC64
  857. case TT_WIN_OVF: /* window overflow */
  858. save_window(env);
  859. break;
  860. case TT_WIN_UNF: /* window underflow */
  861. restore_window(env);
  862. break;
  863. case TT_TFAULT:
  864. case TT_DFAULT:
  865. {
  866. info.si_signo = SIGSEGV;
  867. info.si_errno = 0;
  868. /* XXX: check env->error_code */
  869. info.si_code = TARGET_SEGV_MAPERR;
  870. info._sifields._sigfault._addr = env->mmuregs[4];
  871. queue_signal(env, info.si_signo, &info);
  872. }
  873. break;
  874. #else
  875. case TT_SPILL: /* window overflow */
  876. save_window(env);
  877. break;
  878. case TT_FILL: /* window underflow */
  879. restore_window(env);
  880. break;
  881. case TT_TFAULT:
  882. case TT_DFAULT:
  883. {
  884. info.si_signo = SIGSEGV;
  885. info.si_errno = 0;
  886. /* XXX: check env->error_code */
  887. info.si_code = TARGET_SEGV_MAPERR;
  888. if (trapnr == TT_DFAULT)
  889. info._sifields._sigfault._addr = env->dmmuregs[4];
  890. else
  891. info._sifields._sigfault._addr = env->tsptr->tpc;
  892. queue_signal(env, info.si_signo, &info);
  893. }
  894. break;
  895. #ifndef TARGET_ABI32
  896. case 0x16e:
  897. flush_windows(env);
  898. sparc64_get_context(env);
  899. break;
  900. case 0x16f:
  901. flush_windows(env);
  902. sparc64_set_context(env);
  903. break;
  904. #endif
  905. #endif
  906. case EXCP_INTERRUPT:
  907. /* just indicate that signals should be handled asap */
  908. break;
  909. case EXCP_DEBUG:
  910. {
  911. int sig;
  912. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  913. if (sig)
  914. {
  915. info.si_signo = sig;
  916. info.si_errno = 0;
  917. info.si_code = TARGET_TRAP_BRKPT;
  918. queue_signal(env, info.si_signo, &info);
  919. }
  920. }
  921. break;
  922. default:
  923. printf ("Unhandled trap: 0x%x\n", trapnr);
  924. cpu_dump_state(env, stderr, fprintf, 0);
  925. exit (1);
  926. }
  927. process_pending_signals (env);
  928. }
  929. }
  930. #endif
  931. #ifdef TARGET_PPC
  932. static inline uint64_t cpu_ppc_get_tb (CPUState *env)
  933. {
  934. /* TO FIX */
  935. return 0;
  936. }
  937. uint32_t cpu_ppc_load_tbl (CPUState *env)
  938. {
  939. return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
  940. }
  941. uint32_t cpu_ppc_load_tbu (CPUState *env)
  942. {
  943. return cpu_ppc_get_tb(env) >> 32;
  944. }
  945. uint32_t cpu_ppc_load_atbl (CPUState *env)
  946. {
  947. return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
  948. }
  949. uint32_t cpu_ppc_load_atbu (CPUState *env)
  950. {
  951. return cpu_ppc_get_tb(env) >> 32;
  952. }
  953. uint32_t cpu_ppc601_load_rtcu (CPUState *env)
  954. __attribute__ (( alias ("cpu_ppc_load_tbu") ));
  955. uint32_t cpu_ppc601_load_rtcl (CPUState *env)
  956. {
  957. return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
  958. }
  959. /* XXX: to be fixed */
  960. int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
  961. {
  962. return -1;
  963. }
  964. int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
  965. {
  966. return -1;
  967. }
  968. #define EXCP_DUMP(env, fmt, args...) \
  969. do { \
  970. fprintf(stderr, fmt , ##args); \
  971. cpu_dump_state(env, stderr, fprintf, 0); \
  972. qemu_log(fmt, ##args); \
  973. log_cpu_state(env, 0); \
  974. } while (0)
  975. void cpu_loop(CPUPPCState *env)
  976. {
  977. target_siginfo_t info;
  978. int trapnr;
  979. uint32_t ret;
  980. for(;;) {
  981. trapnr = cpu_ppc_exec(env);
  982. switch(trapnr) {
  983. case POWERPC_EXCP_NONE:
  984. /* Just go on */
  985. break;
  986. case POWERPC_EXCP_CRITICAL: /* Critical input */
  987. cpu_abort(env, "Critical interrupt while in user mode. "
  988. "Aborting\n");
  989. break;
  990. case POWERPC_EXCP_MCHECK: /* Machine check exception */
  991. cpu_abort(env, "Machine check exception while in user mode. "
  992. "Aborting\n");
  993. break;
  994. case POWERPC_EXCP_DSI: /* Data storage exception */
  995. EXCP_DUMP(env, "Invalid data memory access: 0x" ADDRX "\n",
  996. env->spr[SPR_DAR]);
  997. /* XXX: check this. Seems bugged */
  998. switch (env->error_code & 0xFF000000) {
  999. case 0x40000000:
  1000. info.si_signo = TARGET_SIGSEGV;
  1001. info.si_errno = 0;
  1002. info.si_code = TARGET_SEGV_MAPERR;
  1003. break;
  1004. case 0x04000000:
  1005. info.si_signo = TARGET_SIGILL;
  1006. info.si_errno = 0;
  1007. info.si_code = TARGET_ILL_ILLADR;
  1008. break;
  1009. case 0x08000000:
  1010. info.si_signo = TARGET_SIGSEGV;
  1011. info.si_errno = 0;
  1012. info.si_code = TARGET_SEGV_ACCERR;
  1013. break;
  1014. default:
  1015. /* Let's send a regular segfault... */
  1016. EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
  1017. env->error_code);
  1018. info.si_signo = TARGET_SIGSEGV;
  1019. info.si_errno = 0;
  1020. info.si_code = TARGET_SEGV_MAPERR;
  1021. break;
  1022. }
  1023. info._sifields._sigfault._addr = env->nip;
  1024. queue_signal(env, info.si_signo, &info);
  1025. break;
  1026. case POWERPC_EXCP_ISI: /* Instruction storage exception */
  1027. EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" ADDRX "\n",
  1028. env->spr[SPR_SRR0]);
  1029. /* XXX: check this */
  1030. switch (env->error_code & 0xFF000000) {
  1031. case 0x40000000:
  1032. info.si_signo = TARGET_SIGSEGV;
  1033. info.si_errno = 0;
  1034. info.si_code = TARGET_SEGV_MAPERR;
  1035. break;
  1036. case 0x10000000:
  1037. case 0x08000000:
  1038. info.si_signo = TARGET_SIGSEGV;
  1039. info.si_errno = 0;
  1040. info.si_code = TARGET_SEGV_ACCERR;
  1041. break;
  1042. default:
  1043. /* Let's send a regular segfault... */
  1044. EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
  1045. env->error_code);
  1046. info.si_signo = TARGET_SIGSEGV;
  1047. info.si_errno = 0;
  1048. info.si_code = TARGET_SEGV_MAPERR;
  1049. break;
  1050. }
  1051. info._sifields._sigfault._addr = env->nip - 4;
  1052. queue_signal(env, info.si_signo, &info);
  1053. break;
  1054. case POWERPC_EXCP_EXTERNAL: /* External input */
  1055. cpu_abort(env, "External interrupt while in user mode. "
  1056. "Aborting\n");
  1057. break;
  1058. case POWERPC_EXCP_ALIGN: /* Alignment exception */
  1059. EXCP_DUMP(env, "Unaligned memory access\n");
  1060. /* XXX: check this */
  1061. info.si_signo = TARGET_SIGBUS;
  1062. info.si_errno = 0;
  1063. info.si_code = TARGET_BUS_ADRALN;
  1064. info._sifields._sigfault._addr = env->nip - 4;
  1065. queue_signal(env, info.si_signo, &info);
  1066. break;
  1067. case POWERPC_EXCP_PROGRAM: /* Program exception */
  1068. /* XXX: check this */
  1069. switch (env->error_code & ~0xF) {
  1070. case POWERPC_EXCP_FP:
  1071. EXCP_DUMP(env, "Floating point program exception\n");
  1072. info.si_signo = TARGET_SIGFPE;
  1073. info.si_errno = 0;
  1074. switch (env->error_code & 0xF) {
  1075. case POWERPC_EXCP_FP_OX:
  1076. info.si_code = TARGET_FPE_FLTOVF;
  1077. break;
  1078. case POWERPC_EXCP_FP_UX:
  1079. info.si_code = TARGET_FPE_FLTUND;
  1080. break;
  1081. case POWERPC_EXCP_FP_ZX:
  1082. case POWERPC_EXCP_FP_VXZDZ:
  1083. info.si_code = TARGET_FPE_FLTDIV;
  1084. break;
  1085. case POWERPC_EXCP_FP_XX:
  1086. info.si_code = TARGET_FPE_FLTRES;
  1087. break;
  1088. case POWERPC_EXCP_FP_VXSOFT:
  1089. info.si_code = TARGET_FPE_FLTINV;
  1090. break;
  1091. case POWERPC_EXCP_FP_VXSNAN:
  1092. case POWERPC_EXCP_FP_VXISI:
  1093. case POWERPC_EXCP_FP_VXIDI:
  1094. case POWERPC_EXCP_FP_VXIMZ:
  1095. case POWERPC_EXCP_FP_VXVC:
  1096. case POWERPC_EXCP_FP_VXSQRT:
  1097. case POWERPC_EXCP_FP_VXCVI:
  1098. info.si_code = TARGET_FPE_FLTSUB;
  1099. break;
  1100. default:
  1101. EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
  1102. env->error_code);
  1103. break;
  1104. }
  1105. break;
  1106. case POWERPC_EXCP_INVAL:
  1107. EXCP_DUMP(env, "Invalid instruction\n");
  1108. info.si_signo = TARGET_SIGILL;
  1109. info.si_errno = 0;
  1110. switch (env->error_code & 0xF) {
  1111. case POWERPC_EXCP_INVAL_INVAL:
  1112. info.si_code = TARGET_ILL_ILLOPC;
  1113. break;
  1114. case POWERPC_EXCP_INVAL_LSWX:
  1115. info.si_code = TARGET_ILL_ILLOPN;
  1116. break;
  1117. case POWERPC_EXCP_INVAL_SPR:
  1118. info.si_code = TARGET_ILL_PRVREG;
  1119. break;
  1120. case POWERPC_EXCP_INVAL_FP:
  1121. info.si_code = TARGET_ILL_COPROC;
  1122. break;
  1123. default:
  1124. EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
  1125. env->error_code & 0xF);
  1126. info.si_code = TARGET_ILL_ILLADR;
  1127. break;
  1128. }
  1129. break;
  1130. case POWERPC_EXCP_PRIV:
  1131. EXCP_DUMP(env, "Privilege violation\n");
  1132. info.si_signo = TARGET_SIGILL;
  1133. info.si_errno = 0;
  1134. switch (env->error_code & 0xF) {
  1135. case POWERPC_EXCP_PRIV_OPC:
  1136. info.si_code = TARGET_ILL_PRVOPC;
  1137. break;
  1138. case POWERPC_EXCP_PRIV_REG:
  1139. info.si_code = TARGET_ILL_PRVREG;
  1140. break;
  1141. default:
  1142. EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
  1143. env->error_code & 0xF);
  1144. info.si_code = TARGET_ILL_PRVOPC;
  1145. break;
  1146. }
  1147. break;
  1148. case POWERPC_EXCP_TRAP:
  1149. cpu_abort(env, "Tried to call a TRAP\n");
  1150. break;
  1151. default:
  1152. /* Should not happen ! */
  1153. cpu_abort(env, "Unknown program exception (%02x)\n",
  1154. env->error_code);
  1155. break;
  1156. }
  1157. info._sifields._sigfault._addr = env->nip - 4;
  1158. queue_signal(env, info.si_signo, &info);
  1159. break;
  1160. case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
  1161. EXCP_DUMP(env, "No floating point allowed\n");
  1162. info.si_signo = TARGET_SIGILL;
  1163. info.si_errno = 0;
  1164. info.si_code = TARGET_ILL_COPROC;
  1165. info._sifields._sigfault._addr = env->nip - 4;
  1166. queue_signal(env, info.si_signo, &info);
  1167. break;
  1168. case POWERPC_EXCP_SYSCALL: /* System call exception */
  1169. cpu_abort(env, "Syscall exception while in user mode. "
  1170. "Aborting\n");
  1171. break;
  1172. case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
  1173. EXCP_DUMP(env, "No APU instruction allowed\n");
  1174. info.si_signo = TARGET_SIGILL;
  1175. info.si_errno = 0;
  1176. info.si_code = TARGET_ILL_COPROC;
  1177. info._sifields._sigfault._addr = env->nip - 4;
  1178. queue_signal(env, info.si_signo, &info);
  1179. break;
  1180. case POWERPC_EXCP_DECR: /* Decrementer exception */
  1181. cpu_abort(env, "Decrementer interrupt while in user mode. "
  1182. "Aborting\n");
  1183. break;
  1184. case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
  1185. cpu_abort(env, "Fix interval timer interrupt while in user mode. "
  1186. "Aborting\n");
  1187. break;
  1188. case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
  1189. cpu_abort(env, "Watchdog timer interrupt while in user mode. "
  1190. "Aborting\n");
  1191. break;
  1192. case POWERPC_EXCP_DTLB: /* Data TLB error */
  1193. cpu_abort(env, "Data TLB exception while in user mode. "
  1194. "Aborting\n");
  1195. break;
  1196. case POWERPC_EXCP_ITLB: /* Instruction TLB error */
  1197. cpu_abort(env, "Instruction TLB exception while in user mode. "
  1198. "Aborting\n");
  1199. break;
  1200. case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavail. */
  1201. EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
  1202. info.si_signo = TARGET_SIGILL;
  1203. info.si_errno = 0;
  1204. info.si_code = TARGET_ILL_COPROC;
  1205. info._sifields._sigfault._addr = env->nip - 4;
  1206. queue_signal(env, info.si_signo, &info);
  1207. break;
  1208. case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */
  1209. cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
  1210. break;
  1211. case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */
  1212. cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
  1213. break;
  1214. case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */
  1215. cpu_abort(env, "Performance monitor exception not handled\n");
  1216. break;
  1217. case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
  1218. cpu_abort(env, "Doorbell interrupt while in user mode. "
  1219. "Aborting\n");
  1220. break;
  1221. case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
  1222. cpu_abort(env, "Doorbell critical interrupt while in user mode. "
  1223. "Aborting\n");
  1224. break;
  1225. case POWERPC_EXCP_RESET: /* System reset exception */
  1226. cpu_abort(env, "Reset interrupt while in user mode. "
  1227. "Aborting\n");
  1228. break;
  1229. case POWERPC_EXCP_DSEG: /* Data segment exception */
  1230. cpu_abort(env, "Data segment exception while in user mode. "
  1231. "Aborting\n");
  1232. break;
  1233. case POWERPC_EXCP_ISEG: /* Instruction segment exception */
  1234. cpu_abort(env, "Instruction segment exception "
  1235. "while in user mode. Aborting\n");
  1236. break;
  1237. /* PowerPC 64 with hypervisor mode support */
  1238. case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
  1239. cpu_abort(env, "Hypervisor decrementer interrupt "
  1240. "while in user mode. Aborting\n");
  1241. break;
  1242. case POWERPC_EXCP_TRACE: /* Trace exception */
  1243. /* Nothing to do:
  1244. * we use this exception to emulate step-by-step execution mode.
  1245. */
  1246. break;
  1247. /* PowerPC 64 with hypervisor mode support */
  1248. case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
  1249. cpu_abort(env, "Hypervisor data storage exception "
  1250. "while in user mode. Aborting\n");
  1251. break;
  1252. case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */
  1253. cpu_abort(env, "Hypervisor instruction storage exception "
  1254. "while in user mode. Aborting\n");
  1255. break;
  1256. case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
  1257. cpu_abort(env, "Hypervisor data segment exception "
  1258. "while in user mode. Aborting\n");
  1259. break;
  1260. case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */
  1261. cpu_abort(env, "Hypervisor instruction segment exception "
  1262. "while in user mode. Aborting\n");
  1263. break;
  1264. case POWERPC_EXCP_VPU: /* Vector unavailable exception */
  1265. EXCP_DUMP(env, "No Altivec instructions allowed\n");
  1266. info.si_signo = TARGET_SIGILL;
  1267. info.si_errno = 0;
  1268. info.si_code = TARGET_ILL_COPROC;
  1269. info._sifields._sigfault._addr = env->nip - 4;
  1270. queue_signal(env, info.si_signo, &info);
  1271. break;
  1272. case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */
  1273. cpu_abort(env, "Programable interval timer interrupt "
  1274. "while in user mode. Aborting\n");
  1275. break;
  1276. case POWERPC_EXCP_IO: /* IO error exception */
  1277. cpu_abort(env, "IO error exception while in user mode. "
  1278. "Aborting\n");
  1279. break;
  1280. case POWERPC_EXCP_RUNM: /* Run mode exception */
  1281. cpu_abort(env, "Run mode exception while in user mode. "
  1282. "Aborting\n");
  1283. break;
  1284. case POWERPC_EXCP_EMUL: /* Emulation trap exception */
  1285. cpu_abort(env, "Emulation trap exception not handled\n");
  1286. break;
  1287. case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
  1288. cpu_abort(env, "Instruction fetch TLB exception "
  1289. "while in user-mode. Aborting");
  1290. break;
  1291. case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
  1292. cpu_abort(env, "Data load TLB exception while in user-mode. "
  1293. "Aborting");
  1294. break;
  1295. case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
  1296. cpu_abort(env, "Data store TLB exception while in user-mode. "
  1297. "Aborting");
  1298. break;
  1299. case POWERPC_EXCP_FPA: /* Floating-point assist exception */
  1300. cpu_abort(env, "Floating-point assist exception not handled\n");
  1301. break;
  1302. case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
  1303. cpu_abort(env, "Instruction address breakpoint exception "
  1304. "not handled\n");
  1305. break;
  1306. case POWERPC_EXCP_SMI: /* System management interrupt */
  1307. cpu_abort(env, "System management interrupt while in user mode. "
  1308. "Aborting\n");
  1309. break;
  1310. case POWERPC_EXCP_THERM: /* Thermal interrupt */
  1311. cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
  1312. "Aborting\n");
  1313. break;
  1314. case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */
  1315. cpu_abort(env, "Performance monitor exception not handled\n");
  1316. break;
  1317. case POWERPC_EXCP_VPUA: /* Vector assist exception */
  1318. cpu_abort(env, "Vector assist exception not handled\n");
  1319. break;
  1320. case POWERPC_EXCP_SOFTP: /* Soft patch exception */
  1321. cpu_abort(env, "Soft patch exception not handled\n");
  1322. break;
  1323. case POWERPC_EXCP_MAINT: /* Maintenance exception */
  1324. cpu_abort(env, "Maintenance exception while in user mode. "
  1325. "Aborting\n");
  1326. break;
  1327. case POWERPC_EXCP_STOP: /* stop translation */
  1328. /* We did invalidate the instruction cache. Go on */
  1329. break;
  1330. case POWERPC_EXCP_BRANCH: /* branch instruction: */
  1331. /* We just stopped because of a branch. Go on */
  1332. break;
  1333. case POWERPC_EXCP_SYSCALL_USER:
  1334. /* system call in user-mode emulation */
  1335. /* WARNING:
  1336. * PPC ABI uses overflow flag in cr0 to signal an error
  1337. * in syscalls.
  1338. */
  1339. #if 0
  1340. printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
  1341. env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
  1342. #endif
  1343. env->crf[0] &= ~0x1;
  1344. ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
  1345. env->gpr[5], env->gpr[6], env->gpr[7],
  1346. env->gpr[8]);
  1347. if (ret > (uint32_t)(-515)) {
  1348. env->crf[0] |= 0x1;
  1349. ret = -ret;
  1350. }
  1351. env->gpr[3] = ret;
  1352. #if 0
  1353. printf("syscall returned 0x%08x (%d)\n", ret, ret);
  1354. #endif
  1355. break;
  1356. case EXCP_DEBUG:
  1357. {
  1358. int sig;
  1359. sig = gdb_handlesig(env, TARGET_SIGTRAP);
  1360. if (sig) {
  1361. info.si_signo = sig;
  1362. info.si_errno = 0;
  1363. info.si_code = TARGET_TRAP_BRKPT;
  1364. queue_signal(env, info.si_signo, &info);
  1365. }
  1366. }
  1367. break;
  1368. case EXCP_INTERRUPT:
  1369. /* just indicate that signals should be handled asap */
  1370. break;
  1371. default:
  1372. cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
  1373. break;
  1374. }
  1375. process_pending_signals(env);
  1376. }
  1377. }
  1378. #endif
  1379. #ifdef TARGET_MIPS
  1380. #define MIPS_SYS(name, args) args,
  1381. static const uint8_t mips_syscall_args[] = {
  1382. MIPS_SYS(sys_syscall , 0) /* 4000 */
  1383. MIPS_SYS(sys_exit , 1)
  1384. MIPS_SYS(sys_fork , 0)
  1385. MIPS_SYS(sys_read , 3)
  1386. MIPS_SYS(sys_write , 3)
  1387. MIPS_SYS(sys_open , 3) /* 4005 */
  1388. MIPS_SYS(sys_close , 1)
  1389. MIPS_SYS(sys_waitpid , 3)
  1390. MIPS_SYS(sys_creat , 2)
  1391. MIPS_SYS(sys_link , 2)
  1392. MIPS_SYS(sys_unlink , 1) /* 4010 */
  1393. MIPS_SYS(sys_execve , 0)
  1394. MIPS_SYS(sys_chdir , 1)
  1395. MIPS_SYS(sys_time , 1)
  1396. MIPS_SYS(sys_mknod , 3)
  1397. MIPS_SYS(sys_chmod , 2) /* 4015 */
  1398. MIPS_SYS(sys_lchown , 3)
  1399. MIPS_SYS(sys_ni_syscall , 0)
  1400. MIPS_SYS(sys_ni_syscall , 0) /* was sys_stat */
  1401. MIPS_SYS(sys_lseek , 3)
  1402. MIPS_SYS(sys_getpid , 0) /* 4020 */
  1403. MIPS_SYS(sys_mount , 5)
  1404. MIPS_SYS(sys_oldumount , 1)
  1405. MIPS_SYS(sys_setuid , 1)
  1406. MIPS_SYS(sys_getuid , 0)
  1407. MIPS_SYS(sys_stime , 1) /* 4025 */
  1408. MIPS_SYS(sys_ptrace , 4)
  1409. MIPS_SYS(sys_alarm , 1)
  1410. MIPS_SYS(sys_ni_syscall , 0) /* was sys_fstat */
  1411. MIPS_SYS(sys_pause , 0)
  1412. MIPS_SYS(sys_utime , 2) /* 4030 */
  1413. MIPS_SYS(sys_ni_syscall , 0)
  1414. MIPS_SYS(sys_ni_syscall , 0)
  1415. MIPS_SYS(sys_access , 2)
  1416. MIPS_SYS(sys_nice , 1)
  1417. MIPS_SYS(sys_ni_syscall , 0) /* 4035 */
  1418. MIPS_SYS(sys_sync , 0)
  1419. MIPS_SYS(sys_kill , 2)
  1420. MIPS_SYS(sys_rename , 2)
  1421. MIPS_SYS(sys_mkdir , 2)
  1422. MIPS_SYS(sys_rmdir , 1) /* 4040 */
  1423. MIPS_SYS(sys_dup , 1)
  1424. MIPS_SYS(sys_pipe , 0)
  1425. MIPS_SYS(sys_times , 1)
  1426. MIPS_SYS(sys_ni_syscall , 0)
  1427. MIPS_SYS(sys_brk , 1) /* 4045 */
  1428. MIPS_SYS(sys_setgid , 1)
  1429. MIPS_SYS(sys_getgid , 0)
  1430. MIPS_SYS(sys_ni_syscall , 0) /* was signal(2) */
  1431. MIPS_SYS(sys_geteuid , 0)
  1432. MIPS_SYS(sys_getegid , 0) /* 4050 */
  1433. MIPS_SYS(sys_acct , 0)
  1434. MIPS_SYS(sys_umount , 2)
  1435. MIPS_SYS(sys_ni_syscall , 0)
  1436. MIPS_SYS(sys_ioctl , 3)
  1437. MIPS_SYS(sys_fcntl , 3) /* 4055 */
  1438. MIPS_SYS(sys_ni_syscall , 2)
  1439. MIPS_SYS(sys_setpgid , 2)
  1440. MIPS_SYS(sys_ni_syscall , 0)
  1441. MIPS_SYS(sys_olduname , 1)
  1442. MIPS_SYS(sys_umask , 1) /* 4060 */
  1443. MIPS_SYS(sys_chroot , 1)
  1444. MIPS_SYS(sys_ustat , 2)
  1445. MIPS_SYS(sys_dup2 , 2)
  1446. MIPS_SYS(sys_getppid , 0)
  1447. MIPS_SYS(sys_getpgrp , 0) /* 4065 */
  1448. MIPS_SYS(sys_setsid , 0)
  1449. MIPS_SYS(sys_sigaction , 3)
  1450. MIPS_SYS(sys_sgetmask , 0)
  1451. MIPS_SYS(sys_ssetmask , 1)
  1452. MIPS_SYS(sys_setreuid , 2) /* 4070 */
  1453. MIPS_SYS(sys_setregid , 2)
  1454. MIPS_SYS(sys_sigsuspend , 0)
  1455. MIPS_SYS(sys_sigpending , 1)
  1456. MIPS_SYS(sys_sethostname , 2)
  1457. MIPS_SYS(sys_setrlimit , 2) /* 4075 */
  1458. MIPS_SYS(sys_getrlimit , 2)
  1459. MIPS_SYS(sys_getrusage , 2)
  1460. MIPS_SYS(sys_gettimeofday, 2)
  1461. MIPS_SYS(sys_settimeofday, 2)
  1462. MIPS_SYS(sys_getgroups , 2) /* 4080 */
  1463. MIPS_SYS(sys_setgroups , 2)
  1464. MIPS_SYS(sys_ni_syscall , 0) /* old_select */
  1465. MIPS_SYS(sys_symlink , 2)
  1466. MIPS_SYS(sys_ni_syscall , 0) /* was sys_lstat */
  1467. MIPS_SYS(sys_readlink , 3) /* 4085 */
  1468. MIPS_SYS(sys_uselib , 1)
  1469. MIPS_SYS(sys_swapon , 2)
  1470. MIPS_SYS(sys_reboot , 3)
  1471. MIPS_SYS(old_readdir , 3)
  1472. MIPS_SYS(old_mmap , 6) /* 4090 */
  1473. MIPS_SYS(sys_munmap , 2)
  1474. MIPS_SYS(sys_truncate , 2)
  1475. MIPS_SYS(sys_ftruncate , 2)
  1476. MIPS_SYS(sys_fchmod , 2)
  1477. MIPS_SYS(sys_fchown , 3) /* 4095 */
  1478. MIPS_SYS(sys_getpriority , 2)
  1479. MIPS_SYS(sys_setpriority , 3)
  1480. MIPS_SYS(sys_ni_syscall , 0)
  1481. MIPS_SYS(sys_statfs , 2)
  1482. MIPS_SYS(sys_fstatfs , 2) /* 4100 */
  1483. MIPS_SYS(sys_ni_syscall , 0) /* was ioperm(2) */
  1484. MIPS_SYS(sys_socketcall , 2)
  1485. MIPS_SYS(sys_syslog , 3)
  1486. MIPS_SYS(sys_setitimer , 3)
  1487. MIPS_SYS(sys_getitimer , 2) /* 4105 */
  1488. MIPS_SYS(sys_newstat , 2)
  1489. MIPS_SYS(sys_newlstat , 2)
  1490. MIPS_SYS(sys_newfstat , 2)
  1491. MIPS_SYS(sys_uname , 1)
  1492. MIPS_SYS(sys_ni_syscall , 0) /* 4110 was iopl(2) */
  1493. MIPS_SYS(sys_vhangup , 0)
  1494. MIPS_SYS(sys_ni_syscall , 0) /* was sys_idle() */
  1495. MIPS_SYS(sys_ni_syscall , 0) /* was sys_vm86 */
  1496. MIPS_SYS(sys_wait4 , 4)
  1497. MIPS_SYS(sys_swapoff , 1) /* 4115 */
  1498. MIPS_SYS(sys_sysinfo , 1)
  1499. MIPS_SYS(sys_ipc , 6)
  1500. MIPS_SYS(sys_fsync , 1)
  1501. MIPS_SYS(sys_sigreturn , 0)
  1502. MIPS_SYS(sys_clone , 0) /* 4120 */
  1503. MIPS_SYS(sys_setdomainname, 2)
  1504. MIPS_SYS(sys_newuname , 1)
  1505. MIPS_SYS(sys_ni_syscall , 0) /* sys_modify_ldt */
  1506. MIPS_SYS(sys_adjtimex , 1)
  1507. MIPS_SYS(sys_mprotect , 3) /* 4125 */
  1508. MIPS_SYS(sys_sigprocmask , 3)
  1509. MIPS_SYS(sys_ni_syscall , 0) /* was create_module */
  1510. MIPS_SYS(sys_init_module , 5)
  1511. MIPS_SYS(sys_delete_module, 1)
  1512. MIPS_SYS(sys_ni_syscall , 0) /* 4130 was get_kernel_syms */
  1513. MIPS_SYS(sys_quotactl , 0)
  1514. MIPS_SYS(sys_getpgid , 1)
  1515. MIPS_SYS(sys_fchdir , 1)
  1516. MIPS_SYS(sys_bdflush , 2)
  1517. MIPS_SYS(sys_sysfs , 3) /* 4135 */
  1518. MIPS_SYS(sys_personality , 1)
  1519. MIPS_SYS(sys_ni_syscall , 0) /* for afs_syscall */
  1520. MIPS_SYS(sys_setfsuid , 1)
  1521. MIPS_SYS(sys_setfsgid , 1)
  1522. MIPS_SYS(sys_llseek , 5) /* 4140 */
  1523. MIPS_SYS(sys_getdents , 3)
  1524. MIPS_SYS(sys_select , 5)
  1525. MIPS_SYS(sys_flock , 2)
  1526. MIPS_SYS(sys_msync , 3)
  1527. MIPS_SYS(sys_readv , 3) /* 4145 */
  1528. MIPS_SYS(sys_writev , 3)
  1529. MIPS_SYS(sys_cacheflush , 3)
  1530. MIPS_SYS(sys_cachectl , 3)
  1531. MIPS_SYS(sys_sysmips , 4)
  1532. MIPS_SYS(sys_ni_syscall , 0) /* 4150 */
  1533. MIPS_SYS(sys_getsid , 1)
  1534. MIPS_SYS(sys_fdatasync , 0)
  1535. MIPS_SYS(sys_sysctl , 1)
  1536. MIPS_SYS(sys_mlock , 2)
  1537. MIPS_SYS(sys_munlock , 2) /* 4155 */
  1538. MIPS_SYS(sys_mlockall , 1)
  1539. MIPS_SYS(sys_munlockall , 0)
  1540. MIPS_SYS(sys_sched_setparam, 2)
  1541. MIPS_SYS(sys_sched_getparam, 2)
  1542. MIPS_SYS(sys_sched_setscheduler, 3) /* 4160 */
  1543. MIPS_SYS(sys_sched_getscheduler, 1)
  1544. MIPS_SYS(sys_sched_yield , 0)
  1545. MIPS_SYS(sys_sched_get_priority_max, 1)
  1546. MIPS_SYS(sys_sched_get_priority_min, 1)
  1547. MIPS_SYS(sys_sched_rr_get_interval, 2) /* 4165 */
  1548. MIPS_SYS(sys_nanosleep, 2)
  1549. MIPS_SYS(sys_mremap , 4)
  1550. MIPS_SYS(sys_accept , 3)
  1551. MIPS_SYS(sys_bind , 3)
  1552. MIPS_SYS(sys_connect , 3) /* 4170 */
  1553. MIPS_SYS(sys_getpeername , 3)
  1554. MIPS_SYS(sys_getsockname , 3)
  1555. MIPS_SYS(sys_getsockopt , 5)
  1556. MIPS_SYS(sys_listen , 2)
  1557. MIPS_SYS(sys_recv , 4) /* 4175 */
  1558. MIPS_SYS(sys_recvfrom , 6)
  1559. MIPS_SYS(sys_recvmsg , 3)
  1560. MIPS_SYS(sys_send , 4)
  1561. MIPS_SYS(sys_sendmsg , 3)
  1562. MIPS_SYS(sys_sendto , 6) /* 4180 */
  1563. MIPS_SYS(sys_setsockopt , 5)
  1564. MIPS_SYS(sys_shutdown , 2)
  1565. MIPS_SYS(sys_socket , 3)
  1566. MIPS_SYS(sys_socketpair , 4)
  1567. MIPS_SYS(sys_setresuid , 3) /* 4185 */
  1568. MIPS_SYS(sys_getresuid , 3)
  1569. MIPS_SYS(sys_ni_syscall , 0) /* was sys_query_module */
  1570. MIPS_SYS(sys_poll , 3)
  1571. MIPS_SYS(sys_nfsservctl , 3)
  1572. MIPS_SYS(sys_setresgid , 3) /* 4190 */
  1573. MIPS_SYS(sys_getresgid , 3)
  1574. MIPS_SYS(sys_prctl , 5)
  1575. MIPS_SYS(sys_rt_sigreturn, 0)
  1576. MIPS_SYS(sys_rt_sigaction, 4)
  1577. MIPS_SYS(sys_rt_sigprocmask, 4) /* 4195 */
  1578. MIPS_SYS(sys_rt_sigpending, 2)
  1579. MIPS_SYS(sys_rt_sigtimedwait, 4)
  1580. MIPS_SYS(sys_rt_sigqueueinfo, 3)
  1581. MIPS_SYS(sys_rt_sigsuspend, 0)
  1582. MIPS_SYS(sys_pread64 , 6) /* 4200 */
  1583. MIPS_SYS(sys_pwrite64 , 6)
  1584. MIPS_SYS(sys_chown , 3)
  1585. MIPS_SYS(sys_getcwd , 2)
  1586. MIPS_SYS(sys_capget , 2)
  1587. MIPS_SYS(sys_capset , 2) /* 4205 */
  1588. MIPS_SYS(sys_sigaltstack , 0)
  1589. MIPS_SYS(sys_sendfile , 4)
  1590. MIPS_SYS(sys_ni_syscall , 0)
  1591. MIPS_SYS(sys_ni_syscall , 0)
  1592. MIPS_SYS(sys_mmap2 , 6) /* 4210 */
  1593. MIPS_SYS(sys_truncate64 , 4)
  1594. MIPS_SYS(sys_ftruncate64 , 4)
  1595. MIPS_SYS(sys_stat64 , 2)
  1596. MIPS_SYS(sys_lstat64 , 2)
  1597. MIPS_SYS(sys_fstat64 , 2) /* 4215 */
  1598. MIPS_SYS(sys_pivot_root , 2)
  1599. MIPS_SYS(sys_mincore , 3)
  1600. MIPS_SYS(sys_madvise , 3)
  1601. MIPS_SYS(sys_getdents64 , 3)
  1602. MIPS_SYS(sys_fcntl64 , 3) /* 4220 */
  1603. MIPS_SYS(sys_ni_syscall , 0)
  1604. MIPS_SYS(sys_gettid , 0)
  1605. MIPS_SYS(sys_readahead , 5)
  1606. MIPS_SYS(sys_setxattr , 5)
  1607. MIPS_SYS(sys_lsetxattr , 5) /* 4225 */
  1608. MIPS_SYS(sys_fsetxattr , 5)
  1609. MIPS_SYS(sys_getxattr , 4)
  1610. MIPS_SYS(sys_lgetxattr , 4)
  1611. MIPS_SYS(sys_fgetxattr , 4)
  1612. MIPS_SYS(sys_listxattr , 3) /* 4230 */
  1613. MIPS_SYS(sys_llistxattr , 3)
  1614. MIPS_SYS(sys_flistxattr , 3)
  1615. MIPS_SYS(sys_removexattr , 2)
  1616. MIPS_SYS(sys_lremovexattr, 2)
  1617. MIPS_SYS(sys_fremovexattr, 2) /* 4235 */
  1618. MIPS_SYS(sys_tkill , 2)
  1619. MIPS_SYS(sys_sendfile64 , 5)
  1620. MIPS_SYS(sys_futex , 2)
  1621. MIPS_SYS(sys_sched_setaffinity, 3)
  1622. MIPS_SYS(sys_sched_getaffinity, 3) /* 4240 */
  1623. MIPS_SYS(sys_io_setup , 2)
  1624. MIPS_SYS(sys_io_destroy , 1)
  1625. MIPS_SYS(sys_io_getevents, 5)
  1626. MIPS_SYS(sys_io_submit , 3)
  1627. MIPS_SYS(sys_io_cancel , 3) /* 4245 */
  1628. MIPS_SYS(sys_exit_group , 1)
  1629. MIPS_SYS(sys_lookup_dcookie, 3)
  1630. MIPS_SYS(sys_epoll_create, 1)
  1631. MIPS_SYS(sys_epoll_ctl , 4)
  1632. MIPS_SYS(sys_epoll_wait , 3) /* 4250 */
  1633. MIPS_SYS(sys_remap_file_pages, 5)
  1634. MIPS_SYS(sys_set_tid_address, 1)
  1635. MIPS_SYS(sys_restart_syscall, 0)
  1636. MIPS_SYS(sys_fadvise64_64, 7)
  1637. MIPS_SYS(sys_statfs64 , 3) /* 4255 */
  1638. MIPS_SYS(sys_fstatfs64 , 2)
  1639. MIPS_SYS(sys_timer_create, 3)
  1640. MIPS_SYS(sys_timer_settime, 4)
  1641. MIPS_SYS(sys_timer_gettime, 2)
  1642. MIPS_SYS(sys_timer_getoverrun, 1) /* 4260 */
  1643. MIPS_SYS(sys_timer_delete, 1)
  1644. MIPS_SYS(sys_clock_settime, 2)
  1645. MIPS_SYS(sys_clock_gettime, 2)
  1646. MIPS_SYS(sys_clock_getres, 2)
  1647. MIPS_SYS(sys_clock_nanosleep, 4) /* 4265 */
  1648. MIPS_SYS(sys_tgkill , 3)
  1649. MIPS_SYS(sys_utimes , 2)
  1650. MIPS_SYS(sys_mbind , 4)
  1651. MIPS_SYS(sys_ni_syscall , 0) /* sys_get_mempolicy */
  1652. MIPS_SYS(sys_ni_syscall , 0) /* 4270 sys_set_mempolicy */
  1653. MIPS_SYS(sys_mq_open , 4)
  1654. MIPS_SYS(sys_mq_unlink , 1)
  1655. MIPS_SYS(sys_mq_timedsend, 5)
  1656. MIPS_SYS(sys_mq_timedreceive, 5)
  1657. MIPS_SYS(sys_mq_notify , 2) /* 4275 */
  1658. MIPS_SYS(sys_mq_getsetattr, 3)
  1659. MIPS_SYS(sys_ni_syscall , 0) /* sys_vserver */
  1660. MIPS_SYS(sys_waitid , 4)
  1661. MIPS_SYS(sys_ni_syscall , 0) /* available, was setaltroot */
  1662. MIPS_SYS(sys_add_key , 5)
  1663. MIPS_SYS(sys_request_key, 4)
  1664. MIPS_SYS(sys_keyctl , 5)
  1665. MIPS_SYS(sys_set_thread_area, 1)
  1666. MIPS_SYS(sys_inotify_init, 0)
  1667. MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
  1668. MIPS_SYS(sys_inotify_rm_watch, 2)
  1669. MIPS_SYS(sys_migrate_pages, 4)
  1670. MIPS_SYS(sys_openat, 4)
  1671. MIPS_SYS(sys_mkdirat, 3)
  1672. MIPS_SYS(sys_mknodat, 4) /* 4290 */
  1673. MIPS_SYS(sys_fchownat, 5)
  1674. MIPS_SYS(sys_futimesat, 3)
  1675. MIPS_SYS(sys_fstatat64, 4)
  1676. MIPS_SYS(sys_unlinkat, 3)
  1677. MIPS_SYS(sys_renameat, 4) /* 4295 */
  1678. MIPS_SYS(sys_linkat, 5)
  1679. MIPS_SYS(sys_symlinkat, 3)
  1680. MIPS_SYS(sys_readlinkat, 4)
  1681. MIPS_SYS(sys_fchmodat, 3)
  1682. MIPS_SYS(sys_faccessat, 3) /* 4300 */
  1683. MIPS_SYS(sys_pselect6, 6)
  1684. MIPS_SYS(sys_ppoll, 5)
  1685. MIPS_SYS(sys_unshare, 1)
  1686. MIPS_SYS(sys_splice, 4)
  1687. MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
  1688. MIPS_SYS(sys_tee, 4)
  1689. MIPS_SYS(sys_vmsplice, 4)
  1690. MIPS_SYS(sys_move_pages, 6)
  1691. MIPS_SYS(sys_set_robust_list, 2)
  1692. MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
  1693. MIPS_SYS(sys_kexec_load, 4)
  1694. MIPS_SYS(sys_getcpu, 3)
  1695. MIPS_SYS(sys_epoll_pwait, 6)
  1696. MIPS_SYS(sys_ioprio_set, 3)
  1697. MIPS_SYS(sys_ioprio_get, 2)
  1698. };
  1699. #undef MIPS_SYS
  1700. void cpu_loop(CPUMIPSState *env)
  1701. {
  1702. target_siginfo_t info;
  1703. int trapnr, ret;
  1704. unsigned int syscall_num;
  1705. for(;;) {
  1706. trapnr = cpu_mips_exec(env);
  1707. switch(trapnr) {
  1708. case EXCP_SYSCALL:
  1709. syscall_num = env->active_tc.gpr[2] - 4000;
  1710. env->active_tc.PC += 4;
  1711. if (syscall_num >= sizeof(mips_syscall_args)) {
  1712. ret = -ENOSYS;
  1713. } else {
  1714. int nb_args;
  1715. abi_ulong sp_reg;
  1716. abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
  1717. nb_args = mips_syscall_args[syscall_num];
  1718. sp_reg = env->active_tc.gpr[29];
  1719. switch (nb_args) {
  1720. /* these arguments are taken from the stack */
  1721. /* FIXME - what to do if get_user() fails? */
  1722. case 8: get_user_ual(arg8, sp_reg + 28);
  1723. case 7: get_user_ual(arg7, sp_reg + 24);
  1724. case 6: get_user_ual(arg6, sp_reg + 20);
  1725. case 5: get_user_ual(arg5, sp_reg + 16);
  1726. default:
  1727. break;
  1728. }
  1729. ret = do_syscall(env, env->active_tc.gpr[2],
  1730. env->active_tc.gpr[4],
  1731. env->active_tc.gpr[5],
  1732. env->active_tc.gpr[6],
  1733. env->active_tc.gpr[7],
  1734. arg5, arg6/*, arg7, arg8*/);
  1735. }
  1736. if ((unsigned int)ret >= (unsigned int)(-1133)) {
  1737. env->active_tc.gpr[7] = 1; /* error flag */
  1738. ret = -ret;
  1739. } else {
  1740. env->active_tc.gpr[7] = 0; /* error flag */
  1741. }
  1742. env->active_tc.gpr[2] = ret;
  1743. break;
  1744. case EXCP_TLBL:
  1745. case EXCP_TLBS:
  1746. case EXCP_CpU:
  1747. case EXCP_RI:
  1748. info.si_signo = TARGET_SIGILL;
  1749. info.si_errno = 0;
  1750. info.si_code = 0;
  1751. queue_signal(env, info.si_signo, &info);
  1752. break;
  1753. case EXCP_INTERRUPT:
  1754. /* just indicate that signals should be handled asap */
  1755. break;
  1756. case EXCP_DEBUG:
  1757. {
  1758. int sig;
  1759. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  1760. if (sig)
  1761. {
  1762. info.si_signo = sig;
  1763. info.si_errno = 0;
  1764. info.si_code = TARGET_TRAP_BRKPT;
  1765. queue_signal(env, info.si_signo, &info);
  1766. }
  1767. }
  1768. break;
  1769. default:
  1770. // error:
  1771. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
  1772. trapnr);
  1773. cpu_dump_state(env, stderr, fprintf, 0);
  1774. abort();
  1775. }
  1776. process_pending_signals(env);
  1777. }
  1778. }
  1779. #endif
  1780. #ifdef TARGET_SH4
  1781. void cpu_loop (CPUState *env)
  1782. {
  1783. int trapnr, ret;
  1784. target_siginfo_t info;
  1785. while (1) {
  1786. trapnr = cpu_sh4_exec (env);
  1787. switch (trapnr) {
  1788. case 0x160:
  1789. env->pc += 2;
  1790. ret = do_syscall(env,
  1791. env->gregs[3],
  1792. env->gregs[4],
  1793. env->gregs[5],
  1794. env->gregs[6],
  1795. env->gregs[7],
  1796. env->gregs[0],
  1797. env->gregs[1]);
  1798. env->gregs[0] = ret;
  1799. break;
  1800. case EXCP_INTERRUPT:
  1801. /* just indicate that signals should be handled asap */
  1802. break;
  1803. case EXCP_DEBUG:
  1804. {
  1805. int sig;
  1806. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  1807. if (sig)
  1808. {
  1809. info.si_signo = sig;
  1810. info.si_errno = 0;
  1811. info.si_code = TARGET_TRAP_BRKPT;
  1812. queue_signal(env, info.si_signo, &info);
  1813. }
  1814. }
  1815. break;
  1816. case 0xa0:
  1817. case 0xc0:
  1818. info.si_signo = SIGSEGV;
  1819. info.si_errno = 0;
  1820. info.si_code = TARGET_SEGV_MAPERR;
  1821. info._sifields._sigfault._addr = env->tea;
  1822. queue_signal(env, info.si_signo, &info);
  1823. break;
  1824. default:
  1825. printf ("Unhandled trap: 0x%x\n", trapnr);
  1826. cpu_dump_state(env, stderr, fprintf, 0);
  1827. exit (1);
  1828. }
  1829. process_pending_signals (env);
  1830. }
  1831. }
  1832. #endif
  1833. #ifdef TARGET_CRIS
  1834. void cpu_loop (CPUState *env)
  1835. {
  1836. int trapnr, ret;
  1837. target_siginfo_t info;
  1838. while (1) {
  1839. trapnr = cpu_cris_exec (env);
  1840. switch (trapnr) {
  1841. case 0xaa:
  1842. {
  1843. info.si_signo = SIGSEGV;
  1844. info.si_errno = 0;
  1845. /* XXX: check env->error_code */
  1846. info.si_code = TARGET_SEGV_MAPERR;
  1847. info._sifields._sigfault._addr = env->pregs[PR_EDA];
  1848. queue_signal(env, info.si_signo, &info);
  1849. }
  1850. break;
  1851. case EXCP_INTERRUPT:
  1852. /* just indicate that signals should be handled asap */
  1853. break;
  1854. case EXCP_BREAK:
  1855. ret = do_syscall(env,
  1856. env->regs[9],
  1857. env->regs[10],
  1858. env->regs[11],
  1859. env->regs[12],
  1860. env->regs[13],
  1861. env->pregs[7],
  1862. env->pregs[11]);
  1863. env->regs[10] = ret;
  1864. break;
  1865. case EXCP_DEBUG:
  1866. {
  1867. int sig;
  1868. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  1869. if (sig)
  1870. {
  1871. info.si_signo = sig;
  1872. info.si_errno = 0;
  1873. info.si_code = TARGET_TRAP_BRKPT;
  1874. queue_signal(env, info.si_signo, &info);
  1875. }
  1876. }
  1877. break;
  1878. default:
  1879. printf ("Unhandled trap: 0x%x\n", trapnr);
  1880. cpu_dump_state(env, stderr, fprintf, 0);
  1881. exit (1);
  1882. }
  1883. process_pending_signals (env);
  1884. }
  1885. }
  1886. #endif
  1887. #ifdef TARGET_M68K
  1888. void cpu_loop(CPUM68KState *env)
  1889. {
  1890. int trapnr;
  1891. unsigned int n;
  1892. target_siginfo_t info;
  1893. TaskState *ts = env->opaque;
  1894. for(;;) {
  1895. trapnr = cpu_m68k_exec(env);
  1896. switch(trapnr) {
  1897. case EXCP_ILLEGAL:
  1898. {
  1899. if (ts->sim_syscalls) {
  1900. uint16_t nr;
  1901. nr = lduw(env->pc + 2);
  1902. env->pc += 4;
  1903. do_m68k_simcall(env, nr);
  1904. } else {
  1905. goto do_sigill;
  1906. }
  1907. }
  1908. break;
  1909. case EXCP_HALT_INSN:
  1910. /* Semihosing syscall. */
  1911. env->pc += 4;
  1912. do_m68k_semihosting(env, env->dregs[0]);
  1913. break;
  1914. case EXCP_LINEA:
  1915. case EXCP_LINEF:
  1916. case EXCP_UNSUPPORTED:
  1917. do_sigill:
  1918. info.si_signo = SIGILL;
  1919. info.si_errno = 0;
  1920. info.si_code = TARGET_ILL_ILLOPN;
  1921. info._sifields._sigfault._addr = env->pc;
  1922. queue_signal(env, info.si_signo, &info);
  1923. break;
  1924. case EXCP_TRAP0:
  1925. {
  1926. ts->sim_syscalls = 0;
  1927. n = env->dregs[0];
  1928. env->pc += 2;
  1929. env->dregs[0] = do_syscall(env,
  1930. n,
  1931. env->dregs[1],
  1932. env->dregs[2],
  1933. env->dregs[3],
  1934. env->dregs[4],
  1935. env->dregs[5],
  1936. env->aregs[0]);
  1937. }
  1938. break;
  1939. case EXCP_INTERRUPT:
  1940. /* just indicate that signals should be handled asap */
  1941. break;
  1942. case EXCP_ACCESS:
  1943. {
  1944. info.si_signo = SIGSEGV;
  1945. info.si_errno = 0;
  1946. /* XXX: check env->error_code */
  1947. info.si_code = TARGET_SEGV_MAPERR;
  1948. info._sifields._sigfault._addr = env->mmu.ar;
  1949. queue_signal(env, info.si_signo, &info);
  1950. }
  1951. break;
  1952. case EXCP_DEBUG:
  1953. {
  1954. int sig;
  1955. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  1956. if (sig)
  1957. {
  1958. info.si_signo = sig;
  1959. info.si_errno = 0;
  1960. info.si_code = TARGET_TRAP_BRKPT;
  1961. queue_signal(env, info.si_signo, &info);
  1962. }
  1963. }
  1964. break;
  1965. default:
  1966. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
  1967. trapnr);
  1968. cpu_dump_state(env, stderr, fprintf, 0);
  1969. abort();
  1970. }
  1971. process_pending_signals(env);
  1972. }
  1973. }
  1974. #endif /* TARGET_M68K */
  1975. #ifdef TARGET_ALPHA
  1976. void cpu_loop (CPUState *env)
  1977. {
  1978. int trapnr;
  1979. target_siginfo_t info;
  1980. while (1) {
  1981. trapnr = cpu_alpha_exec (env);
  1982. switch (trapnr) {
  1983. case EXCP_RESET:
  1984. fprintf(stderr, "Reset requested. Exit\n");
  1985. exit(1);
  1986. break;
  1987. case EXCP_MCHK:
  1988. fprintf(stderr, "Machine check exception. Exit\n");
  1989. exit(1);
  1990. break;
  1991. case EXCP_ARITH:
  1992. fprintf(stderr, "Arithmetic trap.\n");
  1993. exit(1);
  1994. break;
  1995. case EXCP_HW_INTERRUPT:
  1996. fprintf(stderr, "External interrupt. Exit\n");
  1997. exit(1);
  1998. break;
  1999. case EXCP_DFAULT:
  2000. fprintf(stderr, "MMU data fault\n");
  2001. exit(1);
  2002. break;
  2003. case EXCP_DTB_MISS_PAL:
  2004. fprintf(stderr, "MMU data TLB miss in PALcode\n");
  2005. exit(1);
  2006. break;
  2007. case EXCP_ITB_MISS:
  2008. fprintf(stderr, "MMU instruction TLB miss\n");
  2009. exit(1);
  2010. break;
  2011. case EXCP_ITB_ACV:
  2012. fprintf(stderr, "MMU instruction access violation\n");
  2013. exit(1);
  2014. break;
  2015. case EXCP_DTB_MISS_NATIVE:
  2016. fprintf(stderr, "MMU data TLB miss\n");
  2017. exit(1);
  2018. break;
  2019. case EXCP_UNALIGN:
  2020. fprintf(stderr, "Unaligned access\n");
  2021. exit(1);
  2022. break;
  2023. case EXCP_OPCDEC:
  2024. fprintf(stderr, "Invalid instruction\n");
  2025. exit(1);
  2026. break;
  2027. case EXCP_FEN:
  2028. fprintf(stderr, "Floating-point not allowed\n");
  2029. exit(1);
  2030. break;
  2031. case EXCP_CALL_PAL ... (EXCP_CALL_PALP - 1):
  2032. call_pal(env, (trapnr >> 6) | 0x80);
  2033. break;
  2034. case EXCP_CALL_PALP ... (EXCP_CALL_PALE - 1):
  2035. fprintf(stderr, "Privileged call to PALcode\n");
  2036. exit(1);
  2037. break;
  2038. case EXCP_DEBUG:
  2039. {
  2040. int sig;
  2041. sig = gdb_handlesig (env, TARGET_SIGTRAP);
  2042. if (sig)
  2043. {
  2044. info.si_signo = sig;
  2045. info.si_errno = 0;
  2046. info.si_code = TARGET_TRAP_BRKPT;
  2047. queue_signal(env, info.si_signo, &info);
  2048. }
  2049. }
  2050. break;
  2051. default:
  2052. printf ("Unhandled trap: 0x%x\n", trapnr);
  2053. cpu_dump_state(env, stderr, fprintf, 0);
  2054. exit (1);
  2055. }
  2056. process_pending_signals (env);
  2057. }
  2058. }
  2059. #endif /* TARGET_ALPHA */
  2060. static void usage(void)
  2061. {
  2062. printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
  2063. "usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
  2064. "Linux CPU emulator (compiled for %s emulation)\n"
  2065. "\n"
  2066. "Standard options:\n"
  2067. "-h print this help\n"
  2068. "-g port wait gdb connection to port\n"
  2069. "-L path set the elf interpreter prefix (default=%s)\n"
  2070. "-s size set the stack size in bytes (default=%ld)\n"
  2071. "-cpu model select CPU (-cpu ? for list)\n"
  2072. "-drop-ld-preload drop LD_PRELOAD for target process\n"
  2073. "-E var=value sets/modifies targets environment variable(s)\n"
  2074. "-U var unsets targets environment variable(s)\n"
  2075. "\n"
  2076. "Debug options:\n"
  2077. "-d options activate log (logfile=%s)\n"
  2078. "-p pagesize set the host page size to 'pagesize'\n"
  2079. "-strace log system calls\n"
  2080. "\n"
  2081. "Environment variables:\n"
  2082. "QEMU_STRACE Print system calls and arguments similar to the\n"
  2083. " 'strace' program. Enable by setting to any value.\n"
  2084. "You can use -E and -U options to set/unset environment variables\n"
  2085. "for target process. It is possible to provide several variables\n"
  2086. "by repeating the option. For example:\n"
  2087. " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
  2088. "Note that if you provide several changes to single variable\n"
  2089. "last change will stay in effect.\n"
  2090. ,
  2091. TARGET_ARCH,
  2092. interp_prefix,
  2093. x86_stack_size,
  2094. DEBUG_LOGFILE);
  2095. exit(1);
  2096. }
  2097. THREAD CPUState *thread_env;
  2098. /* Assumes contents are already zeroed. */
  2099. void init_task_state(TaskState *ts)
  2100. {
  2101. int i;
  2102. ts->used = 1;
  2103. ts->first_free = ts->sigqueue_table;
  2104. for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
  2105. ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
  2106. }
  2107. ts->sigqueue_table[i].next = NULL;
  2108. }
  2109. int main(int argc, char **argv, char **envp)
  2110. {
  2111. const char *filename;
  2112. const char *cpu_model;
  2113. struct target_pt_regs regs1, *regs = &regs1;
  2114. struct image_info info1, *info = &info1;
  2115. TaskState ts1, *ts = &ts1;
  2116. CPUState *env;
  2117. int optind;
  2118. const char *r;
  2119. int gdbstub_port = 0;
  2120. char **target_environ, **wrk;
  2121. envlist_t *envlist = NULL;
  2122. if (argc <= 1)
  2123. usage();
  2124. qemu_cache_utils_init(envp);
  2125. /* init debug */
  2126. cpu_set_log_filename(DEBUG_LOGFILE);
  2127. if ((envlist = envlist_create()) == NULL) {
  2128. (void) fprintf(stderr, "Unable to allocate envlist\n");
  2129. exit(1);
  2130. }
  2131. /* add current environment into the list */
  2132. for (wrk = environ; *wrk != NULL; wrk++) {
  2133. (void) envlist_setenv(envlist, *wrk);
  2134. }
  2135. cpu_model = NULL;
  2136. optind = 1;
  2137. for(;;) {
  2138. if (optind >= argc)
  2139. break;
  2140. r = argv[optind];
  2141. if (r[0] != '-')
  2142. break;
  2143. optind++;
  2144. r++;
  2145. if (!strcmp(r, "-")) {
  2146. break;
  2147. } else if (!strcmp(r, "d")) {
  2148. int mask;
  2149. const CPULogItem *item;
  2150. if (optind >= argc)
  2151. break;
  2152. r = argv[optind++];
  2153. mask = cpu_str_to_log_mask(r);
  2154. if (!mask) {
  2155. printf("Log items (comma separated):\n");
  2156. for(item = cpu_log_items; item->mask != 0; item++) {
  2157. printf("%-10s %s\n", item->name, item->help);
  2158. }
  2159. exit(1);
  2160. }
  2161. cpu_set_log(mask);
  2162. } else if (!strcmp(r, "E")) {
  2163. r = argv[optind++];
  2164. if (envlist_setenv(envlist, r) != 0)
  2165. usage();
  2166. } else if (!strcmp(r, "U")) {
  2167. r = argv[optind++];
  2168. if (envlist_unsetenv(envlist, r) != 0)
  2169. usage();
  2170. } else if (!strcmp(r, "s")) {
  2171. if (optind >= argc)
  2172. break;
  2173. r = argv[optind++];
  2174. x86_stack_size = strtol(r, (char **)&r, 0);
  2175. if (x86_stack_size <= 0)
  2176. usage();
  2177. if (*r == 'M')
  2178. x86_stack_size *= 1024 * 1024;
  2179. else if (*r == 'k' || *r == 'K')
  2180. x86_stack_size *= 1024;
  2181. } else if (!strcmp(r, "L")) {
  2182. interp_prefix = argv[optind++];
  2183. } else if (!strcmp(r, "p")) {
  2184. if (optind >= argc)
  2185. break;
  2186. qemu_host_page_size = atoi(argv[optind++]);
  2187. if (qemu_host_page_size == 0 ||
  2188. (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
  2189. fprintf(stderr, "page size must be a power of two\n");
  2190. exit(1);
  2191. }
  2192. } else if (!strcmp(r, "g")) {
  2193. if (optind >= argc)
  2194. break;
  2195. gdbstub_port = atoi(argv[optind++]);
  2196. } else if (!strcmp(r, "r")) {
  2197. qemu_uname_release = argv[optind++];
  2198. } else if (!strcmp(r, "cpu")) {
  2199. cpu_model = argv[optind++];
  2200. if (cpu_model == NULL || strcmp(cpu_model, "?") == 0) {
  2201. /* XXX: implement xxx_cpu_list for targets that still miss it */
  2202. #if defined(cpu_list)
  2203. cpu_list(stdout, &fprintf);
  2204. #endif
  2205. exit(1);
  2206. }
  2207. } else if (!strcmp(r, "drop-ld-preload")) {
  2208. (void) envlist_unsetenv(envlist, "LD_PRELOAD");
  2209. } else if (!strcmp(r, "strace")) {
  2210. do_strace = 1;
  2211. } else
  2212. {
  2213. usage();
  2214. }
  2215. }
  2216. if (optind >= argc)
  2217. usage();
  2218. filename = argv[optind];
  2219. exec_path = argv[optind];
  2220. /* Zero out regs */
  2221. memset(regs, 0, sizeof(struct target_pt_regs));
  2222. /* Zero out image_info */
  2223. memset(info, 0, sizeof(struct image_info));
  2224. /* Scan interp_prefix dir for replacement files. */
  2225. init_paths(interp_prefix);
  2226. if (cpu_model == NULL) {
  2227. #if defined(TARGET_I386)
  2228. #ifdef TARGET_X86_64
  2229. cpu_model = "qemu64";
  2230. #else
  2231. cpu_model = "qemu32";
  2232. #endif
  2233. #elif defined(TARGET_ARM)
  2234. cpu_model = "arm926";
  2235. #elif defined(TARGET_M68K)
  2236. cpu_model = "any";
  2237. #elif defined(TARGET_SPARC)
  2238. #ifdef TARGET_SPARC64
  2239. cpu_model = "TI UltraSparc II";
  2240. #else
  2241. cpu_model = "Fujitsu MB86904";
  2242. #endif
  2243. #elif defined(TARGET_MIPS)
  2244. #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
  2245. cpu_model = "20Kc";
  2246. #else
  2247. cpu_model = "24Kf";
  2248. #endif
  2249. #elif defined(TARGET_PPC)
  2250. #ifdef TARGET_PPC64
  2251. cpu_model = "970";
  2252. #else
  2253. cpu_model = "750";
  2254. #endif
  2255. #else
  2256. cpu_model = "any";
  2257. #endif
  2258. }
  2259. cpu_exec_init_all(0);
  2260. /* NOTE: we need to init the CPU at this stage to get
  2261. qemu_host_page_size */
  2262. env = cpu_init(cpu_model);
  2263. if (!env) {
  2264. fprintf(stderr, "Unable to find CPU definition\n");
  2265. exit(1);
  2266. }
  2267. thread_env = env;
  2268. if (getenv("QEMU_STRACE")) {
  2269. do_strace = 1;
  2270. }
  2271. target_environ = envlist_to_environ(envlist, NULL);
  2272. envlist_free(envlist);
  2273. if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
  2274. printf("Error loading %s\n", filename);
  2275. _exit(1);
  2276. }
  2277. for (wrk = target_environ; *wrk; wrk++) {
  2278. free(*wrk);
  2279. }
  2280. free(target_environ);
  2281. if (qemu_log_enabled()) {
  2282. log_page_dump();
  2283. qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
  2284. qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
  2285. qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
  2286. info->start_code);
  2287. qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
  2288. info->start_data);
  2289. qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
  2290. qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
  2291. info->start_stack);
  2292. qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
  2293. qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
  2294. }
  2295. target_set_brk(info->brk);
  2296. syscall_init();
  2297. signal_init();
  2298. /* build Task State */
  2299. memset(ts, 0, sizeof(TaskState));
  2300. init_task_state(ts);
  2301. ts->info = info;
  2302. env->opaque = ts;
  2303. #if defined(TARGET_I386)
  2304. cpu_x86_set_cpl(env, 3);
  2305. env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
  2306. env->hflags |= HF_PE_MASK;
  2307. if (env->cpuid_features & CPUID_SSE) {
  2308. env->cr[4] |= CR4_OSFXSR_MASK;
  2309. env->hflags |= HF_OSFXSR_MASK;
  2310. }
  2311. #ifndef TARGET_ABI32
  2312. /* enable 64 bit mode if possible */
  2313. if (!(env->cpuid_ext2_features & CPUID_EXT2_LM)) {
  2314. fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
  2315. exit(1);
  2316. }
  2317. env->cr[4] |= CR4_PAE_MASK;
  2318. env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
  2319. env->hflags |= HF_LMA_MASK;
  2320. #endif
  2321. /* flags setup : we activate the IRQs by default as in user mode */
  2322. env->eflags |= IF_MASK;
  2323. /* linux register setup */
  2324. #ifndef TARGET_ABI32
  2325. env->regs[R_EAX] = regs->rax;
  2326. env->regs[R_EBX] = regs->rbx;
  2327. env->regs[R_ECX] = regs->rcx;
  2328. env->regs[R_EDX] = regs->rdx;
  2329. env->regs[R_ESI] = regs->rsi;
  2330. env->regs[R_EDI] = regs->rdi;
  2331. env->regs[R_EBP] = regs->rbp;
  2332. env->regs[R_ESP] = regs->rsp;
  2333. env->eip = regs->rip;
  2334. #else
  2335. env->regs[R_EAX] = regs->eax;
  2336. env->regs[R_EBX] = regs->ebx;
  2337. env->regs[R_ECX] = regs->ecx;
  2338. env->regs[R_EDX] = regs->edx;
  2339. env->regs[R_ESI] = regs->esi;
  2340. env->regs[R_EDI] = regs->edi;
  2341. env->regs[R_EBP] = regs->ebp;
  2342. env->regs[R_ESP] = regs->esp;
  2343. env->eip = regs->eip;
  2344. #endif
  2345. /* linux interrupt setup */
  2346. #ifndef TARGET_ABI32
  2347. env->idt.limit = 511;
  2348. #else
  2349. env->idt.limit = 255;
  2350. #endif
  2351. env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
  2352. PROT_READ|PROT_WRITE,
  2353. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  2354. idt_table = g2h(env->idt.base);
  2355. set_idt(0, 0);
  2356. set_idt(1, 0);
  2357. set_idt(2, 0);
  2358. set_idt(3, 3);
  2359. set_idt(4, 3);
  2360. set_idt(5, 0);
  2361. set_idt(6, 0);
  2362. set_idt(7, 0);
  2363. set_idt(8, 0);
  2364. set_idt(9, 0);
  2365. set_idt(10, 0);
  2366. set_idt(11, 0);
  2367. set_idt(12, 0);
  2368. set_idt(13, 0);
  2369. set_idt(14, 0);
  2370. set_idt(15, 0);
  2371. set_idt(16, 0);
  2372. set_idt(17, 0);
  2373. set_idt(18, 0);
  2374. set_idt(19, 0);
  2375. set_idt(0x80, 3);
  2376. /* linux segment setup */
  2377. {
  2378. uint64_t *gdt_table;
  2379. env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
  2380. PROT_READ|PROT_WRITE,
  2381. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  2382. env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
  2383. gdt_table = g2h(env->gdt.base);
  2384. #ifdef TARGET_ABI32
  2385. write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
  2386. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  2387. (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
  2388. #else
  2389. /* 64 bit code segment */
  2390. write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
  2391. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  2392. DESC_L_MASK |
  2393. (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
  2394. #endif
  2395. write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
  2396. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  2397. (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
  2398. }
  2399. cpu_x86_load_seg(env, R_CS, __USER_CS);
  2400. cpu_x86_load_seg(env, R_SS, __USER_DS);
  2401. #ifdef TARGET_ABI32
  2402. cpu_x86_load_seg(env, R_DS, __USER_DS);
  2403. cpu_x86_load_seg(env, R_ES, __USER_DS);
  2404. cpu_x86_load_seg(env, R_FS, __USER_DS);
  2405. cpu_x86_load_seg(env, R_GS, __USER_DS);
  2406. /* This hack makes Wine work... */
  2407. env->segs[R_FS].selector = 0;
  2408. #else
  2409. cpu_x86_load_seg(env, R_DS, 0);
  2410. cpu_x86_load_seg(env, R_ES, 0);
  2411. cpu_x86_load_seg(env, R_FS, 0);
  2412. cpu_x86_load_seg(env, R_GS, 0);
  2413. #endif
  2414. #elif defined(TARGET_ARM)
  2415. {
  2416. int i;
  2417. cpsr_write(env, regs->uregs[16], 0xffffffff);
  2418. for(i = 0; i < 16; i++) {
  2419. env->regs[i] = regs->uregs[i];
  2420. }
  2421. }
  2422. #elif defined(TARGET_SPARC)
  2423. {
  2424. int i;
  2425. env->pc = regs->pc;
  2426. env->npc = regs->npc;
  2427. env->y = regs->y;
  2428. for(i = 0; i < 8; i++)
  2429. env->gregs[i] = regs->u_regs[i];
  2430. for(i = 0; i < 8; i++)
  2431. env->regwptr[i] = regs->u_regs[i + 8];
  2432. }
  2433. #elif defined(TARGET_PPC)
  2434. {
  2435. int i;
  2436. #if defined(TARGET_PPC64)
  2437. #if defined(TARGET_ABI32)
  2438. env->msr &= ~((target_ulong)1 << MSR_SF);
  2439. #else
  2440. env->msr |= (target_ulong)1 << MSR_SF;
  2441. #endif
  2442. #endif
  2443. env->nip = regs->nip;
  2444. for(i = 0; i < 32; i++) {
  2445. env->gpr[i] = regs->gpr[i];
  2446. }
  2447. }
  2448. #elif defined(TARGET_M68K)
  2449. {
  2450. env->pc = regs->pc;
  2451. env->dregs[0] = regs->d0;
  2452. env->dregs[1] = regs->d1;
  2453. env->dregs[2] = regs->d2;
  2454. env->dregs[3] = regs->d3;
  2455. env->dregs[4] = regs->d4;
  2456. env->dregs[5] = regs->d5;
  2457. env->dregs[6] = regs->d6;
  2458. env->dregs[7] = regs->d7;
  2459. env->aregs[0] = regs->a0;
  2460. env->aregs[1] = regs->a1;
  2461. env->aregs[2] = regs->a2;
  2462. env->aregs[3] = regs->a3;
  2463. env->aregs[4] = regs->a4;
  2464. env->aregs[5] = regs->a5;
  2465. env->aregs[6] = regs->a6;
  2466. env->aregs[7] = regs->usp;
  2467. env->sr = regs->sr;
  2468. ts->sim_syscalls = 1;
  2469. }
  2470. #elif defined(TARGET_MIPS)
  2471. {
  2472. int i;
  2473. for(i = 0; i < 32; i++) {
  2474. env->active_tc.gpr[i] = regs->regs[i];
  2475. }
  2476. env->active_tc.PC = regs->cp0_epc;
  2477. }
  2478. #elif defined(TARGET_SH4)
  2479. {
  2480. int i;
  2481. for(i = 0; i < 16; i++) {
  2482. env->gregs[i] = regs->regs[i];
  2483. }
  2484. env->pc = regs->pc;
  2485. }
  2486. #elif defined(TARGET_ALPHA)
  2487. {
  2488. int i;
  2489. for(i = 0; i < 28; i++) {
  2490. env->ir[i] = ((abi_ulong *)regs)[i];
  2491. }
  2492. env->ipr[IPR_USP] = regs->usp;
  2493. env->ir[30] = regs->usp;
  2494. env->pc = regs->pc;
  2495. env->unique = regs->unique;
  2496. }
  2497. #elif defined(TARGET_CRIS)
  2498. {
  2499. env->regs[0] = regs->r0;
  2500. env->regs[1] = regs->r1;
  2501. env->regs[2] = regs->r2;
  2502. env->regs[3] = regs->r3;
  2503. env->regs[4] = regs->r4;
  2504. env->regs[5] = regs->r5;
  2505. env->regs[6] = regs->r6;
  2506. env->regs[7] = regs->r7;
  2507. env->regs[8] = regs->r8;
  2508. env->regs[9] = regs->r9;
  2509. env->regs[10] = regs->r10;
  2510. env->regs[11] = regs->r11;
  2511. env->regs[12] = regs->r12;
  2512. env->regs[13] = regs->r13;
  2513. env->regs[14] = info->start_stack;
  2514. env->regs[15] = regs->acr;
  2515. env->pc = regs->erp;
  2516. }
  2517. #else
  2518. #error unsupported target CPU
  2519. #endif
  2520. #if defined(TARGET_ARM) || defined(TARGET_M68K)
  2521. ts->stack_base = info->start_stack;
  2522. ts->heap_base = info->brk;
  2523. /* This will be filled in on the first SYS_HEAPINFO call. */
  2524. ts->heap_limit = 0;
  2525. #endif
  2526. if (gdbstub_port) {
  2527. gdbserver_start (gdbstub_port);
  2528. gdb_handlesig(env, 0);
  2529. }
  2530. cpu_loop(env);
  2531. /* never exits */
  2532. return 0;
  2533. }