main.c 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388
  1. /*
  2. * qemu user main
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <unistd.h>
  25. #include <sys/mman.h>
  26. #include <sys/syscall.h>
  27. #include <sys/resource.h>
  28. #include "qemu.h"
  29. #include "qemu-common.h"
  30. #include "cpu.h"
  31. #include "tcg.h"
  32. #include "qemu/timer.h"
  33. #include "qemu/envlist.h"
  34. #include "elf.h"
  35. char *exec_path;
  36. int singlestep;
  37. const char *filename;
  38. const char *argv0;
  39. int gdbstub_port;
  40. envlist_t *envlist;
  41. static const char *cpu_model;
  42. unsigned long mmap_min_addr;
  43. #if defined(CONFIG_USE_GUEST_BASE)
  44. unsigned long guest_base;
  45. int have_guest_base;
  46. #if (TARGET_LONG_BITS == 32) && (HOST_LONG_BITS == 64)
  47. /*
  48. * When running 32-on-64 we should make sure we can fit all of the possible
  49. * guest address space into a contiguous chunk of virtual host memory.
  50. *
  51. * This way we will never overlap with our own libraries or binaries or stack
  52. * or anything else that QEMU maps.
  53. */
  54. # ifdef TARGET_MIPS
  55. /* MIPS only supports 31 bits of virtual address space for user space */
  56. unsigned long reserved_va = 0x77000000;
  57. # else
  58. unsigned long reserved_va = 0xf7000000;
  59. # endif
  60. #else
  61. unsigned long reserved_va;
  62. #endif
  63. #endif
  64. static void usage(void);
  65. static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
  66. const char *qemu_uname_release;
  67. /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
  68. we allocate a bigger stack. Need a better solution, for example
  69. by remapping the process stack directly at the right place */
  70. unsigned long guest_stack_size = 8 * 1024 * 1024UL;
  71. void gemu_log(const char *fmt, ...)
  72. {
  73. va_list ap;
  74. va_start(ap, fmt);
  75. vfprintf(stderr, fmt, ap);
  76. va_end(ap);
  77. }
  78. #if defined(TARGET_I386)
  79. int cpu_get_pic_interrupt(CPUX86State *env)
  80. {
  81. return -1;
  82. }
  83. #endif
  84. /***********************************************************/
  85. /* Helper routines for implementing atomic operations. */
  86. /* To implement exclusive operations we force all cpus to syncronise.
  87. We don't require a full sync, only that no cpus are executing guest code.
  88. The alternative is to map target atomic ops onto host equivalents,
  89. which requires quite a lot of per host/target work. */
  90. static pthread_mutex_t cpu_list_mutex = PTHREAD_MUTEX_INITIALIZER;
  91. static pthread_mutex_t exclusive_lock = PTHREAD_MUTEX_INITIALIZER;
  92. static pthread_cond_t exclusive_cond = PTHREAD_COND_INITIALIZER;
  93. static pthread_cond_t exclusive_resume = PTHREAD_COND_INITIALIZER;
  94. static int pending_cpus;
  95. /* Make sure everything is in a consistent state for calling fork(). */
  96. void fork_start(void)
  97. {
  98. pthread_mutex_lock(&tcg_ctx.tb_ctx.tb_lock);
  99. pthread_mutex_lock(&exclusive_lock);
  100. mmap_fork_start();
  101. }
  102. void fork_end(int child)
  103. {
  104. mmap_fork_end(child);
  105. if (child) {
  106. CPUState *cpu, *next_cpu;
  107. /* Child processes created by fork() only have a single thread.
  108. Discard information about the parent threads. */
  109. CPU_FOREACH_SAFE(cpu, next_cpu) {
  110. if (cpu != thread_cpu) {
  111. QTAILQ_REMOVE(&cpus, thread_cpu, node);
  112. }
  113. }
  114. pending_cpus = 0;
  115. pthread_mutex_init(&exclusive_lock, NULL);
  116. pthread_mutex_init(&cpu_list_mutex, NULL);
  117. pthread_cond_init(&exclusive_cond, NULL);
  118. pthread_cond_init(&exclusive_resume, NULL);
  119. pthread_mutex_init(&tcg_ctx.tb_ctx.tb_lock, NULL);
  120. gdbserver_fork((CPUArchState *)thread_cpu->env_ptr);
  121. } else {
  122. pthread_mutex_unlock(&exclusive_lock);
  123. pthread_mutex_unlock(&tcg_ctx.tb_ctx.tb_lock);
  124. }
  125. }
  126. /* Wait for pending exclusive operations to complete. The exclusive lock
  127. must be held. */
  128. static inline void exclusive_idle(void)
  129. {
  130. while (pending_cpus) {
  131. pthread_cond_wait(&exclusive_resume, &exclusive_lock);
  132. }
  133. }
  134. /* Start an exclusive operation.
  135. Must only be called from outside cpu_arm_exec. */
  136. static inline void start_exclusive(void)
  137. {
  138. CPUState *other_cpu;
  139. pthread_mutex_lock(&exclusive_lock);
  140. exclusive_idle();
  141. pending_cpus = 1;
  142. /* Make all other cpus stop executing. */
  143. CPU_FOREACH(other_cpu) {
  144. if (other_cpu->running) {
  145. pending_cpus++;
  146. cpu_exit(other_cpu);
  147. }
  148. }
  149. if (pending_cpus > 1) {
  150. pthread_cond_wait(&exclusive_cond, &exclusive_lock);
  151. }
  152. }
  153. /* Finish an exclusive operation. */
  154. static inline void end_exclusive(void)
  155. {
  156. pending_cpus = 0;
  157. pthread_cond_broadcast(&exclusive_resume);
  158. pthread_mutex_unlock(&exclusive_lock);
  159. }
  160. /* Wait for exclusive ops to finish, and begin cpu execution. */
  161. static inline void cpu_exec_start(CPUState *cpu)
  162. {
  163. pthread_mutex_lock(&exclusive_lock);
  164. exclusive_idle();
  165. cpu->running = true;
  166. pthread_mutex_unlock(&exclusive_lock);
  167. }
  168. /* Mark cpu as not executing, and release pending exclusive ops. */
  169. static inline void cpu_exec_end(CPUState *cpu)
  170. {
  171. pthread_mutex_lock(&exclusive_lock);
  172. cpu->running = false;
  173. if (pending_cpus > 1) {
  174. pending_cpus--;
  175. if (pending_cpus == 1) {
  176. pthread_cond_signal(&exclusive_cond);
  177. }
  178. }
  179. exclusive_idle();
  180. pthread_mutex_unlock(&exclusive_lock);
  181. }
  182. void cpu_list_lock(void)
  183. {
  184. pthread_mutex_lock(&cpu_list_mutex);
  185. }
  186. void cpu_list_unlock(void)
  187. {
  188. pthread_mutex_unlock(&cpu_list_mutex);
  189. }
  190. #ifdef TARGET_I386
  191. /***********************************************************/
  192. /* CPUX86 core interface */
  193. void cpu_smm_update(CPUX86State *env)
  194. {
  195. }
  196. uint64_t cpu_get_tsc(CPUX86State *env)
  197. {
  198. return cpu_get_real_ticks();
  199. }
  200. static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
  201. int flags)
  202. {
  203. unsigned int e1, e2;
  204. uint32_t *p;
  205. e1 = (addr << 16) | (limit & 0xffff);
  206. e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
  207. e2 |= flags;
  208. p = ptr;
  209. p[0] = tswap32(e1);
  210. p[1] = tswap32(e2);
  211. }
  212. static uint64_t *idt_table;
  213. #ifdef TARGET_X86_64
  214. static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
  215. uint64_t addr, unsigned int sel)
  216. {
  217. uint32_t *p, e1, e2;
  218. e1 = (addr & 0xffff) | (sel << 16);
  219. e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
  220. p = ptr;
  221. p[0] = tswap32(e1);
  222. p[1] = tswap32(e2);
  223. p[2] = tswap32(addr >> 32);
  224. p[3] = 0;
  225. }
  226. /* only dpl matters as we do only user space emulation */
  227. static void set_idt(int n, unsigned int dpl)
  228. {
  229. set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
  230. }
  231. #else
  232. static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
  233. uint32_t addr, unsigned int sel)
  234. {
  235. uint32_t *p, e1, e2;
  236. e1 = (addr & 0xffff) | (sel << 16);
  237. e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
  238. p = ptr;
  239. p[0] = tswap32(e1);
  240. p[1] = tswap32(e2);
  241. }
  242. /* only dpl matters as we do only user space emulation */
  243. static void set_idt(int n, unsigned int dpl)
  244. {
  245. set_gate(idt_table + n, 0, dpl, 0, 0);
  246. }
  247. #endif
  248. void cpu_loop(CPUX86State *env)
  249. {
  250. CPUState *cs = CPU(x86_env_get_cpu(env));
  251. int trapnr;
  252. abi_ulong pc;
  253. target_siginfo_t info;
  254. for(;;) {
  255. trapnr = cpu_x86_exec(env);
  256. switch(trapnr) {
  257. case 0x80:
  258. /* linux syscall from int $0x80 */
  259. env->regs[R_EAX] = do_syscall(env,
  260. env->regs[R_EAX],
  261. env->regs[R_EBX],
  262. env->regs[R_ECX],
  263. env->regs[R_EDX],
  264. env->regs[R_ESI],
  265. env->regs[R_EDI],
  266. env->regs[R_EBP],
  267. 0, 0);
  268. break;
  269. #ifndef TARGET_ABI32
  270. case EXCP_SYSCALL:
  271. /* linux syscall from syscall instruction */
  272. env->regs[R_EAX] = do_syscall(env,
  273. env->regs[R_EAX],
  274. env->regs[R_EDI],
  275. env->regs[R_ESI],
  276. env->regs[R_EDX],
  277. env->regs[10],
  278. env->regs[8],
  279. env->regs[9],
  280. 0, 0);
  281. env->eip = env->exception_next_eip;
  282. break;
  283. #endif
  284. case EXCP0B_NOSEG:
  285. case EXCP0C_STACK:
  286. info.si_signo = SIGBUS;
  287. info.si_errno = 0;
  288. info.si_code = TARGET_SI_KERNEL;
  289. info._sifields._sigfault._addr = 0;
  290. queue_signal(env, info.si_signo, &info);
  291. break;
  292. case EXCP0D_GPF:
  293. /* XXX: potential problem if ABI32 */
  294. #ifndef TARGET_X86_64
  295. if (env->eflags & VM_MASK) {
  296. handle_vm86_fault(env);
  297. } else
  298. #endif
  299. {
  300. info.si_signo = SIGSEGV;
  301. info.si_errno = 0;
  302. info.si_code = TARGET_SI_KERNEL;
  303. info._sifields._sigfault._addr = 0;
  304. queue_signal(env, info.si_signo, &info);
  305. }
  306. break;
  307. case EXCP0E_PAGE:
  308. info.si_signo = SIGSEGV;
  309. info.si_errno = 0;
  310. if (!(env->error_code & 1))
  311. info.si_code = TARGET_SEGV_MAPERR;
  312. else
  313. info.si_code = TARGET_SEGV_ACCERR;
  314. info._sifields._sigfault._addr = env->cr[2];
  315. queue_signal(env, info.si_signo, &info);
  316. break;
  317. case EXCP00_DIVZ:
  318. #ifndef TARGET_X86_64
  319. if (env->eflags & VM_MASK) {
  320. handle_vm86_trap(env, trapnr);
  321. } else
  322. #endif
  323. {
  324. /* division by zero */
  325. info.si_signo = SIGFPE;
  326. info.si_errno = 0;
  327. info.si_code = TARGET_FPE_INTDIV;
  328. info._sifields._sigfault._addr = env->eip;
  329. queue_signal(env, info.si_signo, &info);
  330. }
  331. break;
  332. case EXCP01_DB:
  333. case EXCP03_INT3:
  334. #ifndef TARGET_X86_64
  335. if (env->eflags & VM_MASK) {
  336. handle_vm86_trap(env, trapnr);
  337. } else
  338. #endif
  339. {
  340. info.si_signo = SIGTRAP;
  341. info.si_errno = 0;
  342. if (trapnr == EXCP01_DB) {
  343. info.si_code = TARGET_TRAP_BRKPT;
  344. info._sifields._sigfault._addr = env->eip;
  345. } else {
  346. info.si_code = TARGET_SI_KERNEL;
  347. info._sifields._sigfault._addr = 0;
  348. }
  349. queue_signal(env, info.si_signo, &info);
  350. }
  351. break;
  352. case EXCP04_INTO:
  353. case EXCP05_BOUND:
  354. #ifndef TARGET_X86_64
  355. if (env->eflags & VM_MASK) {
  356. handle_vm86_trap(env, trapnr);
  357. } else
  358. #endif
  359. {
  360. info.si_signo = SIGSEGV;
  361. info.si_errno = 0;
  362. info.si_code = TARGET_SI_KERNEL;
  363. info._sifields._sigfault._addr = 0;
  364. queue_signal(env, info.si_signo, &info);
  365. }
  366. break;
  367. case EXCP06_ILLOP:
  368. info.si_signo = SIGILL;
  369. info.si_errno = 0;
  370. info.si_code = TARGET_ILL_ILLOPN;
  371. info._sifields._sigfault._addr = env->eip;
  372. queue_signal(env, info.si_signo, &info);
  373. break;
  374. case EXCP_INTERRUPT:
  375. /* just indicate that signals should be handled asap */
  376. break;
  377. case EXCP_DEBUG:
  378. {
  379. int sig;
  380. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  381. if (sig)
  382. {
  383. info.si_signo = sig;
  384. info.si_errno = 0;
  385. info.si_code = TARGET_TRAP_BRKPT;
  386. queue_signal(env, info.si_signo, &info);
  387. }
  388. }
  389. break;
  390. default:
  391. pc = env->segs[R_CS].base + env->eip;
  392. fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
  393. (long)pc, trapnr);
  394. abort();
  395. }
  396. process_pending_signals(env);
  397. }
  398. }
  399. #endif
  400. #ifdef TARGET_ARM
  401. #define get_user_code_u32(x, gaddr, doswap) \
  402. ({ abi_long __r = get_user_u32((x), (gaddr)); \
  403. if (!__r && (doswap)) { \
  404. (x) = bswap32(x); \
  405. } \
  406. __r; \
  407. })
  408. #define get_user_code_u16(x, gaddr, doswap) \
  409. ({ abi_long __r = get_user_u16((x), (gaddr)); \
  410. if (!__r && (doswap)) { \
  411. (x) = bswap16(x); \
  412. } \
  413. __r; \
  414. })
  415. #ifdef TARGET_ABI32
  416. /* Commpage handling -- there is no commpage for AArch64 */
  417. /*
  418. * See the Linux kernel's Documentation/arm/kernel_user_helpers.txt
  419. * Input:
  420. * r0 = pointer to oldval
  421. * r1 = pointer to newval
  422. * r2 = pointer to target value
  423. *
  424. * Output:
  425. * r0 = 0 if *ptr was changed, non-0 if no exchange happened
  426. * C set if *ptr was changed, clear if no exchange happened
  427. *
  428. * Note segv's in kernel helpers are a bit tricky, we can set the
  429. * data address sensibly but the PC address is just the entry point.
  430. */
  431. static void arm_kernel_cmpxchg64_helper(CPUARMState *env)
  432. {
  433. uint64_t oldval, newval, val;
  434. uint32_t addr, cpsr;
  435. target_siginfo_t info;
  436. /* Based on the 32 bit code in do_kernel_trap */
  437. /* XXX: This only works between threads, not between processes.
  438. It's probably possible to implement this with native host
  439. operations. However things like ldrex/strex are much harder so
  440. there's not much point trying. */
  441. start_exclusive();
  442. cpsr = cpsr_read(env);
  443. addr = env->regs[2];
  444. if (get_user_u64(oldval, env->regs[0])) {
  445. env->exception.vaddress = env->regs[0];
  446. goto segv;
  447. };
  448. if (get_user_u64(newval, env->regs[1])) {
  449. env->exception.vaddress = env->regs[1];
  450. goto segv;
  451. };
  452. if (get_user_u64(val, addr)) {
  453. env->exception.vaddress = addr;
  454. goto segv;
  455. }
  456. if (val == oldval) {
  457. val = newval;
  458. if (put_user_u64(val, addr)) {
  459. env->exception.vaddress = addr;
  460. goto segv;
  461. };
  462. env->regs[0] = 0;
  463. cpsr |= CPSR_C;
  464. } else {
  465. env->regs[0] = -1;
  466. cpsr &= ~CPSR_C;
  467. }
  468. cpsr_write(env, cpsr, CPSR_C);
  469. end_exclusive();
  470. return;
  471. segv:
  472. end_exclusive();
  473. /* We get the PC of the entry address - which is as good as anything,
  474. on a real kernel what you get depends on which mode it uses. */
  475. info.si_signo = SIGSEGV;
  476. info.si_errno = 0;
  477. /* XXX: check env->error_code */
  478. info.si_code = TARGET_SEGV_MAPERR;
  479. info._sifields._sigfault._addr = env->exception.vaddress;
  480. queue_signal(env, info.si_signo, &info);
  481. end_exclusive();
  482. }
  483. /* Handle a jump to the kernel code page. */
  484. static int
  485. do_kernel_trap(CPUARMState *env)
  486. {
  487. uint32_t addr;
  488. uint32_t cpsr;
  489. uint32_t val;
  490. switch (env->regs[15]) {
  491. case 0xffff0fa0: /* __kernel_memory_barrier */
  492. /* ??? No-op. Will need to do better for SMP. */
  493. break;
  494. case 0xffff0fc0: /* __kernel_cmpxchg */
  495. /* XXX: This only works between threads, not between processes.
  496. It's probably possible to implement this with native host
  497. operations. However things like ldrex/strex are much harder so
  498. there's not much point trying. */
  499. start_exclusive();
  500. cpsr = cpsr_read(env);
  501. addr = env->regs[2];
  502. /* FIXME: This should SEGV if the access fails. */
  503. if (get_user_u32(val, addr))
  504. val = ~env->regs[0];
  505. if (val == env->regs[0]) {
  506. val = env->regs[1];
  507. /* FIXME: Check for segfaults. */
  508. put_user_u32(val, addr);
  509. env->regs[0] = 0;
  510. cpsr |= CPSR_C;
  511. } else {
  512. env->regs[0] = -1;
  513. cpsr &= ~CPSR_C;
  514. }
  515. cpsr_write(env, cpsr, CPSR_C);
  516. end_exclusive();
  517. break;
  518. case 0xffff0fe0: /* __kernel_get_tls */
  519. env->regs[0] = env->cp15.tpidrro_el0;
  520. break;
  521. case 0xffff0f60: /* __kernel_cmpxchg64 */
  522. arm_kernel_cmpxchg64_helper(env);
  523. break;
  524. default:
  525. return 1;
  526. }
  527. /* Jump back to the caller. */
  528. addr = env->regs[14];
  529. if (addr & 1) {
  530. env->thumb = 1;
  531. addr &= ~1;
  532. }
  533. env->regs[15] = addr;
  534. return 0;
  535. }
  536. /* Store exclusive handling for AArch32 */
  537. static int do_strex(CPUARMState *env)
  538. {
  539. uint64_t val;
  540. int size;
  541. int rc = 1;
  542. int segv = 0;
  543. uint32_t addr;
  544. start_exclusive();
  545. if (env->exclusive_addr != env->exclusive_test) {
  546. goto fail;
  547. }
  548. /* We know we're always AArch32 so the address is in uint32_t range
  549. * unless it was the -1 exclusive-monitor-lost value (which won't
  550. * match exclusive_test above).
  551. */
  552. assert(extract64(env->exclusive_addr, 32, 32) == 0);
  553. addr = env->exclusive_addr;
  554. size = env->exclusive_info & 0xf;
  555. switch (size) {
  556. case 0:
  557. segv = get_user_u8(val, addr);
  558. break;
  559. case 1:
  560. segv = get_user_u16(val, addr);
  561. break;
  562. case 2:
  563. case 3:
  564. segv = get_user_u32(val, addr);
  565. break;
  566. default:
  567. abort();
  568. }
  569. if (segv) {
  570. env->exception.vaddress = addr;
  571. goto done;
  572. }
  573. if (size == 3) {
  574. uint32_t valhi;
  575. segv = get_user_u32(valhi, addr + 4);
  576. if (segv) {
  577. env->exception.vaddress = addr + 4;
  578. goto done;
  579. }
  580. val = deposit64(val, 32, 32, valhi);
  581. }
  582. if (val != env->exclusive_val) {
  583. goto fail;
  584. }
  585. val = env->regs[(env->exclusive_info >> 8) & 0xf];
  586. switch (size) {
  587. case 0:
  588. segv = put_user_u8(val, addr);
  589. break;
  590. case 1:
  591. segv = put_user_u16(val, addr);
  592. break;
  593. case 2:
  594. case 3:
  595. segv = put_user_u32(val, addr);
  596. break;
  597. }
  598. if (segv) {
  599. env->exception.vaddress = addr;
  600. goto done;
  601. }
  602. if (size == 3) {
  603. val = env->regs[(env->exclusive_info >> 12) & 0xf];
  604. segv = put_user_u32(val, addr + 4);
  605. if (segv) {
  606. env->exception.vaddress = addr + 4;
  607. goto done;
  608. }
  609. }
  610. rc = 0;
  611. fail:
  612. env->regs[15] += 4;
  613. env->regs[(env->exclusive_info >> 4) & 0xf] = rc;
  614. done:
  615. end_exclusive();
  616. return segv;
  617. }
  618. void cpu_loop(CPUARMState *env)
  619. {
  620. CPUState *cs = CPU(arm_env_get_cpu(env));
  621. int trapnr;
  622. unsigned int n, insn;
  623. target_siginfo_t info;
  624. uint32_t addr;
  625. for(;;) {
  626. cpu_exec_start(cs);
  627. trapnr = cpu_arm_exec(env);
  628. cpu_exec_end(cs);
  629. switch(trapnr) {
  630. case EXCP_UDEF:
  631. {
  632. TaskState *ts = cs->opaque;
  633. uint32_t opcode;
  634. int rc;
  635. /* we handle the FPU emulation here, as Linux */
  636. /* we get the opcode */
  637. /* FIXME - what to do if get_user() fails? */
  638. get_user_code_u32(opcode, env->regs[15], env->bswap_code);
  639. rc = EmulateAll(opcode, &ts->fpa, env);
  640. if (rc == 0) { /* illegal instruction */
  641. info.si_signo = SIGILL;
  642. info.si_errno = 0;
  643. info.si_code = TARGET_ILL_ILLOPN;
  644. info._sifields._sigfault._addr = env->regs[15];
  645. queue_signal(env, info.si_signo, &info);
  646. } else if (rc < 0) { /* FP exception */
  647. int arm_fpe=0;
  648. /* translate softfloat flags to FPSR flags */
  649. if (-rc & float_flag_invalid)
  650. arm_fpe |= BIT_IOC;
  651. if (-rc & float_flag_divbyzero)
  652. arm_fpe |= BIT_DZC;
  653. if (-rc & float_flag_overflow)
  654. arm_fpe |= BIT_OFC;
  655. if (-rc & float_flag_underflow)
  656. arm_fpe |= BIT_UFC;
  657. if (-rc & float_flag_inexact)
  658. arm_fpe |= BIT_IXC;
  659. FPSR fpsr = ts->fpa.fpsr;
  660. //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
  661. if (fpsr & (arm_fpe << 16)) { /* exception enabled? */
  662. info.si_signo = SIGFPE;
  663. info.si_errno = 0;
  664. /* ordered by priority, least first */
  665. if (arm_fpe & BIT_IXC) info.si_code = TARGET_FPE_FLTRES;
  666. if (arm_fpe & BIT_UFC) info.si_code = TARGET_FPE_FLTUND;
  667. if (arm_fpe & BIT_OFC) info.si_code = TARGET_FPE_FLTOVF;
  668. if (arm_fpe & BIT_DZC) info.si_code = TARGET_FPE_FLTDIV;
  669. if (arm_fpe & BIT_IOC) info.si_code = TARGET_FPE_FLTINV;
  670. info._sifields._sigfault._addr = env->regs[15];
  671. queue_signal(env, info.si_signo, &info);
  672. } else {
  673. env->regs[15] += 4;
  674. }
  675. /* accumulate unenabled exceptions */
  676. if ((!(fpsr & BIT_IXE)) && (arm_fpe & BIT_IXC))
  677. fpsr |= BIT_IXC;
  678. if ((!(fpsr & BIT_UFE)) && (arm_fpe & BIT_UFC))
  679. fpsr |= BIT_UFC;
  680. if ((!(fpsr & BIT_OFE)) && (arm_fpe & BIT_OFC))
  681. fpsr |= BIT_OFC;
  682. if ((!(fpsr & BIT_DZE)) && (arm_fpe & BIT_DZC))
  683. fpsr |= BIT_DZC;
  684. if ((!(fpsr & BIT_IOE)) && (arm_fpe & BIT_IOC))
  685. fpsr |= BIT_IOC;
  686. ts->fpa.fpsr=fpsr;
  687. } else { /* everything OK */
  688. /* increment PC */
  689. env->regs[15] += 4;
  690. }
  691. }
  692. break;
  693. case EXCP_SWI:
  694. case EXCP_BKPT:
  695. {
  696. env->eabi = 1;
  697. /* system call */
  698. if (trapnr == EXCP_BKPT) {
  699. if (env->thumb) {
  700. /* FIXME - what to do if get_user() fails? */
  701. get_user_code_u16(insn, env->regs[15], env->bswap_code);
  702. n = insn & 0xff;
  703. env->regs[15] += 2;
  704. } else {
  705. /* FIXME - what to do if get_user() fails? */
  706. get_user_code_u32(insn, env->regs[15], env->bswap_code);
  707. n = (insn & 0xf) | ((insn >> 4) & 0xff0);
  708. env->regs[15] += 4;
  709. }
  710. } else {
  711. if (env->thumb) {
  712. /* FIXME - what to do if get_user() fails? */
  713. get_user_code_u16(insn, env->regs[15] - 2,
  714. env->bswap_code);
  715. n = insn & 0xff;
  716. } else {
  717. /* FIXME - what to do if get_user() fails? */
  718. get_user_code_u32(insn, env->regs[15] - 4,
  719. env->bswap_code);
  720. n = insn & 0xffffff;
  721. }
  722. }
  723. if (n == ARM_NR_cacheflush) {
  724. /* nop */
  725. } else if (n == ARM_NR_semihosting
  726. || n == ARM_NR_thumb_semihosting) {
  727. env->regs[0] = do_arm_semihosting (env);
  728. } else if (n == 0 || n >= ARM_SYSCALL_BASE || env->thumb) {
  729. /* linux syscall */
  730. if (env->thumb || n == 0) {
  731. n = env->regs[7];
  732. } else {
  733. n -= ARM_SYSCALL_BASE;
  734. env->eabi = 0;
  735. }
  736. if ( n > ARM_NR_BASE) {
  737. switch (n) {
  738. case ARM_NR_cacheflush:
  739. /* nop */
  740. break;
  741. case ARM_NR_set_tls:
  742. cpu_set_tls(env, env->regs[0]);
  743. env->regs[0] = 0;
  744. break;
  745. case ARM_NR_breakpoint:
  746. env->regs[15] -= env->thumb ? 2 : 4;
  747. goto excp_debug;
  748. default:
  749. gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
  750. n);
  751. env->regs[0] = -TARGET_ENOSYS;
  752. break;
  753. }
  754. } else {
  755. env->regs[0] = do_syscall(env,
  756. n,
  757. env->regs[0],
  758. env->regs[1],
  759. env->regs[2],
  760. env->regs[3],
  761. env->regs[4],
  762. env->regs[5],
  763. 0, 0);
  764. }
  765. } else {
  766. goto error;
  767. }
  768. }
  769. break;
  770. case EXCP_INTERRUPT:
  771. /* just indicate that signals should be handled asap */
  772. break;
  773. case EXCP_STREX:
  774. if (!do_strex(env)) {
  775. break;
  776. }
  777. /* fall through for segv */
  778. case EXCP_PREFETCH_ABORT:
  779. case EXCP_DATA_ABORT:
  780. addr = env->exception.vaddress;
  781. {
  782. info.si_signo = SIGSEGV;
  783. info.si_errno = 0;
  784. /* XXX: check env->error_code */
  785. info.si_code = TARGET_SEGV_MAPERR;
  786. info._sifields._sigfault._addr = addr;
  787. queue_signal(env, info.si_signo, &info);
  788. }
  789. break;
  790. case EXCP_DEBUG:
  791. excp_debug:
  792. {
  793. int sig;
  794. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  795. if (sig)
  796. {
  797. info.si_signo = sig;
  798. info.si_errno = 0;
  799. info.si_code = TARGET_TRAP_BRKPT;
  800. queue_signal(env, info.si_signo, &info);
  801. }
  802. }
  803. break;
  804. case EXCP_KERNEL_TRAP:
  805. if (do_kernel_trap(env))
  806. goto error;
  807. break;
  808. default:
  809. error:
  810. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
  811. trapnr);
  812. cpu_dump_state(cs, stderr, fprintf, 0);
  813. abort();
  814. }
  815. process_pending_signals(env);
  816. }
  817. }
  818. #else
  819. /*
  820. * Handle AArch64 store-release exclusive
  821. *
  822. * rs = gets the status result of store exclusive
  823. * rt = is the register that is stored
  824. * rt2 = is the second register store (in STP)
  825. *
  826. */
  827. static int do_strex_a64(CPUARMState *env)
  828. {
  829. uint64_t val;
  830. int size;
  831. bool is_pair;
  832. int rc = 1;
  833. int segv = 0;
  834. uint64_t addr;
  835. int rs, rt, rt2;
  836. start_exclusive();
  837. /* size | is_pair << 2 | (rs << 4) | (rt << 9) | (rt2 << 14)); */
  838. size = extract32(env->exclusive_info, 0, 2);
  839. is_pair = extract32(env->exclusive_info, 2, 1);
  840. rs = extract32(env->exclusive_info, 4, 5);
  841. rt = extract32(env->exclusive_info, 9, 5);
  842. rt2 = extract32(env->exclusive_info, 14, 5);
  843. addr = env->exclusive_addr;
  844. if (addr != env->exclusive_test) {
  845. goto finish;
  846. }
  847. switch (size) {
  848. case 0:
  849. segv = get_user_u8(val, addr);
  850. break;
  851. case 1:
  852. segv = get_user_u16(val, addr);
  853. break;
  854. case 2:
  855. segv = get_user_u32(val, addr);
  856. break;
  857. case 3:
  858. segv = get_user_u64(val, addr);
  859. break;
  860. default:
  861. abort();
  862. }
  863. if (segv) {
  864. env->exception.vaddress = addr;
  865. goto error;
  866. }
  867. if (val != env->exclusive_val) {
  868. goto finish;
  869. }
  870. if (is_pair) {
  871. if (size == 2) {
  872. segv = get_user_u32(val, addr + 4);
  873. } else {
  874. segv = get_user_u64(val, addr + 8);
  875. }
  876. if (segv) {
  877. env->exception.vaddress = addr + (size == 2 ? 4 : 8);
  878. goto error;
  879. }
  880. if (val != env->exclusive_high) {
  881. goto finish;
  882. }
  883. }
  884. /* handle the zero register */
  885. val = rt == 31 ? 0 : env->xregs[rt];
  886. switch (size) {
  887. case 0:
  888. segv = put_user_u8(val, addr);
  889. break;
  890. case 1:
  891. segv = put_user_u16(val, addr);
  892. break;
  893. case 2:
  894. segv = put_user_u32(val, addr);
  895. break;
  896. case 3:
  897. segv = put_user_u64(val, addr);
  898. break;
  899. }
  900. if (segv) {
  901. goto error;
  902. }
  903. if (is_pair) {
  904. /* handle the zero register */
  905. val = rt2 == 31 ? 0 : env->xregs[rt2];
  906. if (size == 2) {
  907. segv = put_user_u32(val, addr + 4);
  908. } else {
  909. segv = put_user_u64(val, addr + 8);
  910. }
  911. if (segv) {
  912. env->exception.vaddress = addr + (size == 2 ? 4 : 8);
  913. goto error;
  914. }
  915. }
  916. rc = 0;
  917. finish:
  918. env->pc += 4;
  919. /* rs == 31 encodes a write to the ZR, thus throwing away
  920. * the status return. This is rather silly but valid.
  921. */
  922. if (rs < 31) {
  923. env->xregs[rs] = rc;
  924. }
  925. error:
  926. /* instruction faulted, PC does not advance */
  927. /* either way a strex releases any exclusive lock we have */
  928. env->exclusive_addr = -1;
  929. end_exclusive();
  930. return segv;
  931. }
  932. /* AArch64 main loop */
  933. void cpu_loop(CPUARMState *env)
  934. {
  935. CPUState *cs = CPU(arm_env_get_cpu(env));
  936. int trapnr, sig;
  937. target_siginfo_t info;
  938. uint32_t addr;
  939. for (;;) {
  940. cpu_exec_start(cs);
  941. trapnr = cpu_arm_exec(env);
  942. cpu_exec_end(cs);
  943. switch (trapnr) {
  944. case EXCP_SWI:
  945. env->xregs[0] = do_syscall(env,
  946. env->xregs[8],
  947. env->xregs[0],
  948. env->xregs[1],
  949. env->xregs[2],
  950. env->xregs[3],
  951. env->xregs[4],
  952. env->xregs[5],
  953. 0, 0);
  954. break;
  955. case EXCP_INTERRUPT:
  956. /* just indicate that signals should be handled asap */
  957. break;
  958. case EXCP_UDEF:
  959. info.si_signo = SIGILL;
  960. info.si_errno = 0;
  961. info.si_code = TARGET_ILL_ILLOPN;
  962. info._sifields._sigfault._addr = env->pc;
  963. queue_signal(env, info.si_signo, &info);
  964. break;
  965. case EXCP_STREX:
  966. if (!do_strex_a64(env)) {
  967. break;
  968. }
  969. /* fall through for segv */
  970. case EXCP_PREFETCH_ABORT:
  971. case EXCP_DATA_ABORT:
  972. addr = env->exception.vaddress;
  973. info.si_signo = SIGSEGV;
  974. info.si_errno = 0;
  975. /* XXX: check env->error_code */
  976. info.si_code = TARGET_SEGV_MAPERR;
  977. info._sifields._sigfault._addr = addr;
  978. queue_signal(env, info.si_signo, &info);
  979. break;
  980. case EXCP_DEBUG:
  981. case EXCP_BKPT:
  982. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  983. if (sig) {
  984. info.si_signo = sig;
  985. info.si_errno = 0;
  986. info.si_code = TARGET_TRAP_BRKPT;
  987. queue_signal(env, info.si_signo, &info);
  988. }
  989. break;
  990. default:
  991. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
  992. trapnr);
  993. cpu_dump_state(cs, stderr, fprintf, 0);
  994. abort();
  995. }
  996. process_pending_signals(env);
  997. /* Exception return on AArch64 always clears the exclusive monitor,
  998. * so any return to running guest code implies this.
  999. * A strex (successful or otherwise) also clears the monitor, so
  1000. * we don't need to specialcase EXCP_STREX.
  1001. */
  1002. env->exclusive_addr = -1;
  1003. }
  1004. }
  1005. #endif /* ndef TARGET_ABI32 */
  1006. #endif
  1007. #ifdef TARGET_UNICORE32
  1008. void cpu_loop(CPUUniCore32State *env)
  1009. {
  1010. CPUState *cs = CPU(uc32_env_get_cpu(env));
  1011. int trapnr;
  1012. unsigned int n, insn;
  1013. target_siginfo_t info;
  1014. for (;;) {
  1015. cpu_exec_start(cs);
  1016. trapnr = uc32_cpu_exec(env);
  1017. cpu_exec_end(cs);
  1018. switch (trapnr) {
  1019. case UC32_EXCP_PRIV:
  1020. {
  1021. /* system call */
  1022. get_user_u32(insn, env->regs[31] - 4);
  1023. n = insn & 0xffffff;
  1024. if (n >= UC32_SYSCALL_BASE) {
  1025. /* linux syscall */
  1026. n -= UC32_SYSCALL_BASE;
  1027. if (n == UC32_SYSCALL_NR_set_tls) {
  1028. cpu_set_tls(env, env->regs[0]);
  1029. env->regs[0] = 0;
  1030. } else {
  1031. env->regs[0] = do_syscall(env,
  1032. n,
  1033. env->regs[0],
  1034. env->regs[1],
  1035. env->regs[2],
  1036. env->regs[3],
  1037. env->regs[4],
  1038. env->regs[5],
  1039. 0, 0);
  1040. }
  1041. } else {
  1042. goto error;
  1043. }
  1044. }
  1045. break;
  1046. case UC32_EXCP_DTRAP:
  1047. case UC32_EXCP_ITRAP:
  1048. info.si_signo = SIGSEGV;
  1049. info.si_errno = 0;
  1050. /* XXX: check env->error_code */
  1051. info.si_code = TARGET_SEGV_MAPERR;
  1052. info._sifields._sigfault._addr = env->cp0.c4_faultaddr;
  1053. queue_signal(env, info.si_signo, &info);
  1054. break;
  1055. case EXCP_INTERRUPT:
  1056. /* just indicate that signals should be handled asap */
  1057. break;
  1058. case EXCP_DEBUG:
  1059. {
  1060. int sig;
  1061. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  1062. if (sig) {
  1063. info.si_signo = sig;
  1064. info.si_errno = 0;
  1065. info.si_code = TARGET_TRAP_BRKPT;
  1066. queue_signal(env, info.si_signo, &info);
  1067. }
  1068. }
  1069. break;
  1070. default:
  1071. goto error;
  1072. }
  1073. process_pending_signals(env);
  1074. }
  1075. error:
  1076. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
  1077. cpu_dump_state(cs, stderr, fprintf, 0);
  1078. abort();
  1079. }
  1080. #endif
  1081. #ifdef TARGET_SPARC
  1082. #define SPARC64_STACK_BIAS 2047
  1083. //#define DEBUG_WIN
  1084. /* WARNING: dealing with register windows _is_ complicated. More info
  1085. can be found at http://www.sics.se/~psm/sparcstack.html */
  1086. static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
  1087. {
  1088. index = (index + cwp * 16) % (16 * env->nwindows);
  1089. /* wrap handling : if cwp is on the last window, then we use the
  1090. registers 'after' the end */
  1091. if (index < 8 && env->cwp == env->nwindows - 1)
  1092. index += 16 * env->nwindows;
  1093. return index;
  1094. }
  1095. /* save the register window 'cwp1' */
  1096. static inline void save_window_offset(CPUSPARCState *env, int cwp1)
  1097. {
  1098. unsigned int i;
  1099. abi_ulong sp_ptr;
  1100. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  1101. #ifdef TARGET_SPARC64
  1102. if (sp_ptr & 3)
  1103. sp_ptr += SPARC64_STACK_BIAS;
  1104. #endif
  1105. #if defined(DEBUG_WIN)
  1106. printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
  1107. sp_ptr, cwp1);
  1108. #endif
  1109. for(i = 0; i < 16; i++) {
  1110. /* FIXME - what to do if put_user() fails? */
  1111. put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  1112. sp_ptr += sizeof(abi_ulong);
  1113. }
  1114. }
  1115. static void save_window(CPUSPARCState *env)
  1116. {
  1117. #ifndef TARGET_SPARC64
  1118. unsigned int new_wim;
  1119. new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
  1120. ((1LL << env->nwindows) - 1);
  1121. save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
  1122. env->wim = new_wim;
  1123. #else
  1124. save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
  1125. env->cansave++;
  1126. env->canrestore--;
  1127. #endif
  1128. }
  1129. static void restore_window(CPUSPARCState *env)
  1130. {
  1131. #ifndef TARGET_SPARC64
  1132. unsigned int new_wim;
  1133. #endif
  1134. unsigned int i, cwp1;
  1135. abi_ulong sp_ptr;
  1136. #ifndef TARGET_SPARC64
  1137. new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
  1138. ((1LL << env->nwindows) - 1);
  1139. #endif
  1140. /* restore the invalid window */
  1141. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  1142. sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
  1143. #ifdef TARGET_SPARC64
  1144. if (sp_ptr & 3)
  1145. sp_ptr += SPARC64_STACK_BIAS;
  1146. #endif
  1147. #if defined(DEBUG_WIN)
  1148. printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
  1149. sp_ptr, cwp1);
  1150. #endif
  1151. for(i = 0; i < 16; i++) {
  1152. /* FIXME - what to do if get_user() fails? */
  1153. get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
  1154. sp_ptr += sizeof(abi_ulong);
  1155. }
  1156. #ifdef TARGET_SPARC64
  1157. env->canrestore++;
  1158. if (env->cleanwin < env->nwindows - 1)
  1159. env->cleanwin++;
  1160. env->cansave--;
  1161. #else
  1162. env->wim = new_wim;
  1163. #endif
  1164. }
  1165. static void flush_windows(CPUSPARCState *env)
  1166. {
  1167. int offset, cwp1;
  1168. offset = 1;
  1169. for(;;) {
  1170. /* if restore would invoke restore_window(), then we can stop */
  1171. cwp1 = cpu_cwp_inc(env, env->cwp + offset);
  1172. #ifndef TARGET_SPARC64
  1173. if (env->wim & (1 << cwp1))
  1174. break;
  1175. #else
  1176. if (env->canrestore == 0)
  1177. break;
  1178. env->cansave++;
  1179. env->canrestore--;
  1180. #endif
  1181. save_window_offset(env, cwp1);
  1182. offset++;
  1183. }
  1184. cwp1 = cpu_cwp_inc(env, env->cwp + 1);
  1185. #ifndef TARGET_SPARC64
  1186. /* set wim so that restore will reload the registers */
  1187. env->wim = 1 << cwp1;
  1188. #endif
  1189. #if defined(DEBUG_WIN)
  1190. printf("flush_windows: nb=%d\n", offset - 1);
  1191. #endif
  1192. }
  1193. void cpu_loop (CPUSPARCState *env)
  1194. {
  1195. CPUState *cs = CPU(sparc_env_get_cpu(env));
  1196. int trapnr;
  1197. abi_long ret;
  1198. target_siginfo_t info;
  1199. while (1) {
  1200. trapnr = cpu_sparc_exec (env);
  1201. /* Compute PSR before exposing state. */
  1202. if (env->cc_op != CC_OP_FLAGS) {
  1203. cpu_get_psr(env);
  1204. }
  1205. switch (trapnr) {
  1206. #ifndef TARGET_SPARC64
  1207. case 0x88:
  1208. case 0x90:
  1209. #else
  1210. case 0x110:
  1211. case 0x16d:
  1212. #endif
  1213. ret = do_syscall (env, env->gregs[1],
  1214. env->regwptr[0], env->regwptr[1],
  1215. env->regwptr[2], env->regwptr[3],
  1216. env->regwptr[4], env->regwptr[5],
  1217. 0, 0);
  1218. if ((abi_ulong)ret >= (abi_ulong)(-515)) {
  1219. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  1220. env->xcc |= PSR_CARRY;
  1221. #else
  1222. env->psr |= PSR_CARRY;
  1223. #endif
  1224. ret = -ret;
  1225. } else {
  1226. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  1227. env->xcc &= ~PSR_CARRY;
  1228. #else
  1229. env->psr &= ~PSR_CARRY;
  1230. #endif
  1231. }
  1232. env->regwptr[0] = ret;
  1233. /* next instruction */
  1234. env->pc = env->npc;
  1235. env->npc = env->npc + 4;
  1236. break;
  1237. case 0x83: /* flush windows */
  1238. #ifdef TARGET_ABI32
  1239. case 0x103:
  1240. #endif
  1241. flush_windows(env);
  1242. /* next instruction */
  1243. env->pc = env->npc;
  1244. env->npc = env->npc + 4;
  1245. break;
  1246. #ifndef TARGET_SPARC64
  1247. case TT_WIN_OVF: /* window overflow */
  1248. save_window(env);
  1249. break;
  1250. case TT_WIN_UNF: /* window underflow */
  1251. restore_window(env);
  1252. break;
  1253. case TT_TFAULT:
  1254. case TT_DFAULT:
  1255. {
  1256. info.si_signo = TARGET_SIGSEGV;
  1257. info.si_errno = 0;
  1258. /* XXX: check env->error_code */
  1259. info.si_code = TARGET_SEGV_MAPERR;
  1260. info._sifields._sigfault._addr = env->mmuregs[4];
  1261. queue_signal(env, info.si_signo, &info);
  1262. }
  1263. break;
  1264. #else
  1265. case TT_SPILL: /* window overflow */
  1266. save_window(env);
  1267. break;
  1268. case TT_FILL: /* window underflow */
  1269. restore_window(env);
  1270. break;
  1271. case TT_TFAULT:
  1272. case TT_DFAULT:
  1273. {
  1274. info.si_signo = TARGET_SIGSEGV;
  1275. info.si_errno = 0;
  1276. /* XXX: check env->error_code */
  1277. info.si_code = TARGET_SEGV_MAPERR;
  1278. if (trapnr == TT_DFAULT)
  1279. info._sifields._sigfault._addr = env->dmmuregs[4];
  1280. else
  1281. info._sifields._sigfault._addr = cpu_tsptr(env)->tpc;
  1282. queue_signal(env, info.si_signo, &info);
  1283. }
  1284. break;
  1285. #ifndef TARGET_ABI32
  1286. case 0x16e:
  1287. flush_windows(env);
  1288. sparc64_get_context(env);
  1289. break;
  1290. case 0x16f:
  1291. flush_windows(env);
  1292. sparc64_set_context(env);
  1293. break;
  1294. #endif
  1295. #endif
  1296. case EXCP_INTERRUPT:
  1297. /* just indicate that signals should be handled asap */
  1298. break;
  1299. case TT_ILL_INSN:
  1300. {
  1301. info.si_signo = TARGET_SIGILL;
  1302. info.si_errno = 0;
  1303. info.si_code = TARGET_ILL_ILLOPC;
  1304. info._sifields._sigfault._addr = env->pc;
  1305. queue_signal(env, info.si_signo, &info);
  1306. }
  1307. break;
  1308. case EXCP_DEBUG:
  1309. {
  1310. int sig;
  1311. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  1312. if (sig)
  1313. {
  1314. info.si_signo = sig;
  1315. info.si_errno = 0;
  1316. info.si_code = TARGET_TRAP_BRKPT;
  1317. queue_signal(env, info.si_signo, &info);
  1318. }
  1319. }
  1320. break;
  1321. default:
  1322. printf ("Unhandled trap: 0x%x\n", trapnr);
  1323. cpu_dump_state(cs, stderr, fprintf, 0);
  1324. exit (1);
  1325. }
  1326. process_pending_signals (env);
  1327. }
  1328. }
  1329. #endif
  1330. #ifdef TARGET_PPC
  1331. static inline uint64_t cpu_ppc_get_tb(CPUPPCState *env)
  1332. {
  1333. /* TO FIX */
  1334. return 0;
  1335. }
  1336. uint64_t cpu_ppc_load_tbl(CPUPPCState *env)
  1337. {
  1338. return cpu_ppc_get_tb(env);
  1339. }
  1340. uint32_t cpu_ppc_load_tbu(CPUPPCState *env)
  1341. {
  1342. return cpu_ppc_get_tb(env) >> 32;
  1343. }
  1344. uint64_t cpu_ppc_load_atbl(CPUPPCState *env)
  1345. {
  1346. return cpu_ppc_get_tb(env);
  1347. }
  1348. uint32_t cpu_ppc_load_atbu(CPUPPCState *env)
  1349. {
  1350. return cpu_ppc_get_tb(env) >> 32;
  1351. }
  1352. uint32_t cpu_ppc601_load_rtcu(CPUPPCState *env)
  1353. __attribute__ (( alias ("cpu_ppc_load_tbu") ));
  1354. uint32_t cpu_ppc601_load_rtcl(CPUPPCState *env)
  1355. {
  1356. return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
  1357. }
  1358. /* XXX: to be fixed */
  1359. int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp)
  1360. {
  1361. return -1;
  1362. }
  1363. int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val)
  1364. {
  1365. return -1;
  1366. }
  1367. #define EXCP_DUMP(env, fmt, ...) \
  1368. do { \
  1369. CPUState *cs = ENV_GET_CPU(env); \
  1370. fprintf(stderr, fmt , ## __VA_ARGS__); \
  1371. cpu_dump_state(cs, stderr, fprintf, 0); \
  1372. qemu_log(fmt, ## __VA_ARGS__); \
  1373. if (qemu_log_enabled()) { \
  1374. log_cpu_state(cs, 0); \
  1375. } \
  1376. } while (0)
  1377. static int do_store_exclusive(CPUPPCState *env)
  1378. {
  1379. target_ulong addr;
  1380. target_ulong page_addr;
  1381. target_ulong val, val2 __attribute__((unused)) = 0;
  1382. int flags;
  1383. int segv = 0;
  1384. addr = env->reserve_ea;
  1385. page_addr = addr & TARGET_PAGE_MASK;
  1386. start_exclusive();
  1387. mmap_lock();
  1388. flags = page_get_flags(page_addr);
  1389. if ((flags & PAGE_READ) == 0) {
  1390. segv = 1;
  1391. } else {
  1392. int reg = env->reserve_info & 0x1f;
  1393. int size = env->reserve_info >> 5;
  1394. int stored = 0;
  1395. if (addr == env->reserve_addr) {
  1396. switch (size) {
  1397. case 1: segv = get_user_u8(val, addr); break;
  1398. case 2: segv = get_user_u16(val, addr); break;
  1399. case 4: segv = get_user_u32(val, addr); break;
  1400. #if defined(TARGET_PPC64)
  1401. case 8: segv = get_user_u64(val, addr); break;
  1402. case 16: {
  1403. segv = get_user_u64(val, addr);
  1404. if (!segv) {
  1405. segv = get_user_u64(val2, addr + 8);
  1406. }
  1407. break;
  1408. }
  1409. #endif
  1410. default: abort();
  1411. }
  1412. if (!segv && val == env->reserve_val) {
  1413. val = env->gpr[reg];
  1414. switch (size) {
  1415. case 1: segv = put_user_u8(val, addr); break;
  1416. case 2: segv = put_user_u16(val, addr); break;
  1417. case 4: segv = put_user_u32(val, addr); break;
  1418. #if defined(TARGET_PPC64)
  1419. case 8: segv = put_user_u64(val, addr); break;
  1420. case 16: {
  1421. if (val2 == env->reserve_val2) {
  1422. if (msr_le) {
  1423. val2 = val;
  1424. val = env->gpr[reg+1];
  1425. } else {
  1426. val2 = env->gpr[reg+1];
  1427. }
  1428. segv = put_user_u64(val, addr);
  1429. if (!segv) {
  1430. segv = put_user_u64(val2, addr + 8);
  1431. }
  1432. }
  1433. break;
  1434. }
  1435. #endif
  1436. default: abort();
  1437. }
  1438. if (!segv) {
  1439. stored = 1;
  1440. }
  1441. }
  1442. }
  1443. env->crf[0] = (stored << 1) | xer_so;
  1444. env->reserve_addr = (target_ulong)-1;
  1445. }
  1446. if (!segv) {
  1447. env->nip += 4;
  1448. }
  1449. mmap_unlock();
  1450. end_exclusive();
  1451. return segv;
  1452. }
  1453. void cpu_loop(CPUPPCState *env)
  1454. {
  1455. CPUState *cs = CPU(ppc_env_get_cpu(env));
  1456. target_siginfo_t info;
  1457. int trapnr;
  1458. target_ulong ret;
  1459. for(;;) {
  1460. cpu_exec_start(cs);
  1461. trapnr = cpu_ppc_exec(env);
  1462. cpu_exec_end(cs);
  1463. switch(trapnr) {
  1464. case POWERPC_EXCP_NONE:
  1465. /* Just go on */
  1466. break;
  1467. case POWERPC_EXCP_CRITICAL: /* Critical input */
  1468. cpu_abort(cs, "Critical interrupt while in user mode. "
  1469. "Aborting\n");
  1470. break;
  1471. case POWERPC_EXCP_MCHECK: /* Machine check exception */
  1472. cpu_abort(cs, "Machine check exception while in user mode. "
  1473. "Aborting\n");
  1474. break;
  1475. case POWERPC_EXCP_DSI: /* Data storage exception */
  1476. EXCP_DUMP(env, "Invalid data memory access: 0x" TARGET_FMT_lx "\n",
  1477. env->spr[SPR_DAR]);
  1478. /* XXX: check this. Seems bugged */
  1479. switch (env->error_code & 0xFF000000) {
  1480. case 0x40000000:
  1481. info.si_signo = TARGET_SIGSEGV;
  1482. info.si_errno = 0;
  1483. info.si_code = TARGET_SEGV_MAPERR;
  1484. break;
  1485. case 0x04000000:
  1486. info.si_signo = TARGET_SIGILL;
  1487. info.si_errno = 0;
  1488. info.si_code = TARGET_ILL_ILLADR;
  1489. break;
  1490. case 0x08000000:
  1491. info.si_signo = TARGET_SIGSEGV;
  1492. info.si_errno = 0;
  1493. info.si_code = TARGET_SEGV_ACCERR;
  1494. break;
  1495. default:
  1496. /* Let's send a regular segfault... */
  1497. EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
  1498. env->error_code);
  1499. info.si_signo = TARGET_SIGSEGV;
  1500. info.si_errno = 0;
  1501. info.si_code = TARGET_SEGV_MAPERR;
  1502. break;
  1503. }
  1504. info._sifields._sigfault._addr = env->nip;
  1505. queue_signal(env, info.si_signo, &info);
  1506. break;
  1507. case POWERPC_EXCP_ISI: /* Instruction storage exception */
  1508. EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" TARGET_FMT_lx
  1509. "\n", env->spr[SPR_SRR0]);
  1510. /* XXX: check this */
  1511. switch (env->error_code & 0xFF000000) {
  1512. case 0x40000000:
  1513. info.si_signo = TARGET_SIGSEGV;
  1514. info.si_errno = 0;
  1515. info.si_code = TARGET_SEGV_MAPERR;
  1516. break;
  1517. case 0x10000000:
  1518. case 0x08000000:
  1519. info.si_signo = TARGET_SIGSEGV;
  1520. info.si_errno = 0;
  1521. info.si_code = TARGET_SEGV_ACCERR;
  1522. break;
  1523. default:
  1524. /* Let's send a regular segfault... */
  1525. EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
  1526. env->error_code);
  1527. info.si_signo = TARGET_SIGSEGV;
  1528. info.si_errno = 0;
  1529. info.si_code = TARGET_SEGV_MAPERR;
  1530. break;
  1531. }
  1532. info._sifields._sigfault._addr = env->nip - 4;
  1533. queue_signal(env, info.si_signo, &info);
  1534. break;
  1535. case POWERPC_EXCP_EXTERNAL: /* External input */
  1536. cpu_abort(cs, "External interrupt while in user mode. "
  1537. "Aborting\n");
  1538. break;
  1539. case POWERPC_EXCP_ALIGN: /* Alignment exception */
  1540. EXCP_DUMP(env, "Unaligned memory access\n");
  1541. /* XXX: check this */
  1542. info.si_signo = TARGET_SIGBUS;
  1543. info.si_errno = 0;
  1544. info.si_code = TARGET_BUS_ADRALN;
  1545. info._sifields._sigfault._addr = env->nip - 4;
  1546. queue_signal(env, info.si_signo, &info);
  1547. break;
  1548. case POWERPC_EXCP_PROGRAM: /* Program exception */
  1549. /* XXX: check this */
  1550. switch (env->error_code & ~0xF) {
  1551. case POWERPC_EXCP_FP:
  1552. EXCP_DUMP(env, "Floating point program exception\n");
  1553. info.si_signo = TARGET_SIGFPE;
  1554. info.si_errno = 0;
  1555. switch (env->error_code & 0xF) {
  1556. case POWERPC_EXCP_FP_OX:
  1557. info.si_code = TARGET_FPE_FLTOVF;
  1558. break;
  1559. case POWERPC_EXCP_FP_UX:
  1560. info.si_code = TARGET_FPE_FLTUND;
  1561. break;
  1562. case POWERPC_EXCP_FP_ZX:
  1563. case POWERPC_EXCP_FP_VXZDZ:
  1564. info.si_code = TARGET_FPE_FLTDIV;
  1565. break;
  1566. case POWERPC_EXCP_FP_XX:
  1567. info.si_code = TARGET_FPE_FLTRES;
  1568. break;
  1569. case POWERPC_EXCP_FP_VXSOFT:
  1570. info.si_code = TARGET_FPE_FLTINV;
  1571. break;
  1572. case POWERPC_EXCP_FP_VXSNAN:
  1573. case POWERPC_EXCP_FP_VXISI:
  1574. case POWERPC_EXCP_FP_VXIDI:
  1575. case POWERPC_EXCP_FP_VXIMZ:
  1576. case POWERPC_EXCP_FP_VXVC:
  1577. case POWERPC_EXCP_FP_VXSQRT:
  1578. case POWERPC_EXCP_FP_VXCVI:
  1579. info.si_code = TARGET_FPE_FLTSUB;
  1580. break;
  1581. default:
  1582. EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
  1583. env->error_code);
  1584. break;
  1585. }
  1586. break;
  1587. case POWERPC_EXCP_INVAL:
  1588. EXCP_DUMP(env, "Invalid instruction\n");
  1589. info.si_signo = TARGET_SIGILL;
  1590. info.si_errno = 0;
  1591. switch (env->error_code & 0xF) {
  1592. case POWERPC_EXCP_INVAL_INVAL:
  1593. info.si_code = TARGET_ILL_ILLOPC;
  1594. break;
  1595. case POWERPC_EXCP_INVAL_LSWX:
  1596. info.si_code = TARGET_ILL_ILLOPN;
  1597. break;
  1598. case POWERPC_EXCP_INVAL_SPR:
  1599. info.si_code = TARGET_ILL_PRVREG;
  1600. break;
  1601. case POWERPC_EXCP_INVAL_FP:
  1602. info.si_code = TARGET_ILL_COPROC;
  1603. break;
  1604. default:
  1605. EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
  1606. env->error_code & 0xF);
  1607. info.si_code = TARGET_ILL_ILLADR;
  1608. break;
  1609. }
  1610. break;
  1611. case POWERPC_EXCP_PRIV:
  1612. EXCP_DUMP(env, "Privilege violation\n");
  1613. info.si_signo = TARGET_SIGILL;
  1614. info.si_errno = 0;
  1615. switch (env->error_code & 0xF) {
  1616. case POWERPC_EXCP_PRIV_OPC:
  1617. info.si_code = TARGET_ILL_PRVOPC;
  1618. break;
  1619. case POWERPC_EXCP_PRIV_REG:
  1620. info.si_code = TARGET_ILL_PRVREG;
  1621. break;
  1622. default:
  1623. EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
  1624. env->error_code & 0xF);
  1625. info.si_code = TARGET_ILL_PRVOPC;
  1626. break;
  1627. }
  1628. break;
  1629. case POWERPC_EXCP_TRAP:
  1630. cpu_abort(cs, "Tried to call a TRAP\n");
  1631. break;
  1632. default:
  1633. /* Should not happen ! */
  1634. cpu_abort(cs, "Unknown program exception (%02x)\n",
  1635. env->error_code);
  1636. break;
  1637. }
  1638. info._sifields._sigfault._addr = env->nip - 4;
  1639. queue_signal(env, info.si_signo, &info);
  1640. break;
  1641. case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
  1642. EXCP_DUMP(env, "No floating point allowed\n");
  1643. info.si_signo = TARGET_SIGILL;
  1644. info.si_errno = 0;
  1645. info.si_code = TARGET_ILL_COPROC;
  1646. info._sifields._sigfault._addr = env->nip - 4;
  1647. queue_signal(env, info.si_signo, &info);
  1648. break;
  1649. case POWERPC_EXCP_SYSCALL: /* System call exception */
  1650. cpu_abort(cs, "Syscall exception while in user mode. "
  1651. "Aborting\n");
  1652. break;
  1653. case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
  1654. EXCP_DUMP(env, "No APU instruction allowed\n");
  1655. info.si_signo = TARGET_SIGILL;
  1656. info.si_errno = 0;
  1657. info.si_code = TARGET_ILL_COPROC;
  1658. info._sifields._sigfault._addr = env->nip - 4;
  1659. queue_signal(env, info.si_signo, &info);
  1660. break;
  1661. case POWERPC_EXCP_DECR: /* Decrementer exception */
  1662. cpu_abort(cs, "Decrementer interrupt while in user mode. "
  1663. "Aborting\n");
  1664. break;
  1665. case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
  1666. cpu_abort(cs, "Fix interval timer interrupt while in user mode. "
  1667. "Aborting\n");
  1668. break;
  1669. case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
  1670. cpu_abort(cs, "Watchdog timer interrupt while in user mode. "
  1671. "Aborting\n");
  1672. break;
  1673. case POWERPC_EXCP_DTLB: /* Data TLB error */
  1674. cpu_abort(cs, "Data TLB exception while in user mode. "
  1675. "Aborting\n");
  1676. break;
  1677. case POWERPC_EXCP_ITLB: /* Instruction TLB error */
  1678. cpu_abort(cs, "Instruction TLB exception while in user mode. "
  1679. "Aborting\n");
  1680. break;
  1681. case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavail. */
  1682. EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
  1683. info.si_signo = TARGET_SIGILL;
  1684. info.si_errno = 0;
  1685. info.si_code = TARGET_ILL_COPROC;
  1686. info._sifields._sigfault._addr = env->nip - 4;
  1687. queue_signal(env, info.si_signo, &info);
  1688. break;
  1689. case POWERPC_EXCP_EFPDI: /* Embedded floating-point data IRQ */
  1690. cpu_abort(cs, "Embedded floating-point data IRQ not handled\n");
  1691. break;
  1692. case POWERPC_EXCP_EFPRI: /* Embedded floating-point round IRQ */
  1693. cpu_abort(cs, "Embedded floating-point round IRQ not handled\n");
  1694. break;
  1695. case POWERPC_EXCP_EPERFM: /* Embedded performance monitor IRQ */
  1696. cpu_abort(cs, "Performance monitor exception not handled\n");
  1697. break;
  1698. case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
  1699. cpu_abort(cs, "Doorbell interrupt while in user mode. "
  1700. "Aborting\n");
  1701. break;
  1702. case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
  1703. cpu_abort(cs, "Doorbell critical interrupt while in user mode. "
  1704. "Aborting\n");
  1705. break;
  1706. case POWERPC_EXCP_RESET: /* System reset exception */
  1707. cpu_abort(cs, "Reset interrupt while in user mode. "
  1708. "Aborting\n");
  1709. break;
  1710. case POWERPC_EXCP_DSEG: /* Data segment exception */
  1711. cpu_abort(cs, "Data segment exception while in user mode. "
  1712. "Aborting\n");
  1713. break;
  1714. case POWERPC_EXCP_ISEG: /* Instruction segment exception */
  1715. cpu_abort(cs, "Instruction segment exception "
  1716. "while in user mode. Aborting\n");
  1717. break;
  1718. /* PowerPC 64 with hypervisor mode support */
  1719. case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
  1720. cpu_abort(cs, "Hypervisor decrementer interrupt "
  1721. "while in user mode. Aborting\n");
  1722. break;
  1723. case POWERPC_EXCP_TRACE: /* Trace exception */
  1724. /* Nothing to do:
  1725. * we use this exception to emulate step-by-step execution mode.
  1726. */
  1727. break;
  1728. /* PowerPC 64 with hypervisor mode support */
  1729. case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
  1730. cpu_abort(cs, "Hypervisor data storage exception "
  1731. "while in user mode. Aborting\n");
  1732. break;
  1733. case POWERPC_EXCP_HISI: /* Hypervisor instruction storage excp */
  1734. cpu_abort(cs, "Hypervisor instruction storage exception "
  1735. "while in user mode. Aborting\n");
  1736. break;
  1737. case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
  1738. cpu_abort(cs, "Hypervisor data segment exception "
  1739. "while in user mode. Aborting\n");
  1740. break;
  1741. case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment excp */
  1742. cpu_abort(cs, "Hypervisor instruction segment exception "
  1743. "while in user mode. Aborting\n");
  1744. break;
  1745. case POWERPC_EXCP_VPU: /* Vector unavailable exception */
  1746. EXCP_DUMP(env, "No Altivec instructions allowed\n");
  1747. info.si_signo = TARGET_SIGILL;
  1748. info.si_errno = 0;
  1749. info.si_code = TARGET_ILL_COPROC;
  1750. info._sifields._sigfault._addr = env->nip - 4;
  1751. queue_signal(env, info.si_signo, &info);
  1752. break;
  1753. case POWERPC_EXCP_PIT: /* Programmable interval timer IRQ */
  1754. cpu_abort(cs, "Programmable interval timer interrupt "
  1755. "while in user mode. Aborting\n");
  1756. break;
  1757. case POWERPC_EXCP_IO: /* IO error exception */
  1758. cpu_abort(cs, "IO error exception while in user mode. "
  1759. "Aborting\n");
  1760. break;
  1761. case POWERPC_EXCP_RUNM: /* Run mode exception */
  1762. cpu_abort(cs, "Run mode exception while in user mode. "
  1763. "Aborting\n");
  1764. break;
  1765. case POWERPC_EXCP_EMUL: /* Emulation trap exception */
  1766. cpu_abort(cs, "Emulation trap exception not handled\n");
  1767. break;
  1768. case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
  1769. cpu_abort(cs, "Instruction fetch TLB exception "
  1770. "while in user-mode. Aborting");
  1771. break;
  1772. case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
  1773. cpu_abort(cs, "Data load TLB exception while in user-mode. "
  1774. "Aborting");
  1775. break;
  1776. case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
  1777. cpu_abort(cs, "Data store TLB exception while in user-mode. "
  1778. "Aborting");
  1779. break;
  1780. case POWERPC_EXCP_FPA: /* Floating-point assist exception */
  1781. cpu_abort(cs, "Floating-point assist exception not handled\n");
  1782. break;
  1783. case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
  1784. cpu_abort(cs, "Instruction address breakpoint exception "
  1785. "not handled\n");
  1786. break;
  1787. case POWERPC_EXCP_SMI: /* System management interrupt */
  1788. cpu_abort(cs, "System management interrupt while in user mode. "
  1789. "Aborting\n");
  1790. break;
  1791. case POWERPC_EXCP_THERM: /* Thermal interrupt */
  1792. cpu_abort(cs, "Thermal interrupt interrupt while in user mode. "
  1793. "Aborting\n");
  1794. break;
  1795. case POWERPC_EXCP_PERFM: /* Embedded performance monitor IRQ */
  1796. cpu_abort(cs, "Performance monitor exception not handled\n");
  1797. break;
  1798. case POWERPC_EXCP_VPUA: /* Vector assist exception */
  1799. cpu_abort(cs, "Vector assist exception not handled\n");
  1800. break;
  1801. case POWERPC_EXCP_SOFTP: /* Soft patch exception */
  1802. cpu_abort(cs, "Soft patch exception not handled\n");
  1803. break;
  1804. case POWERPC_EXCP_MAINT: /* Maintenance exception */
  1805. cpu_abort(cs, "Maintenance exception while in user mode. "
  1806. "Aborting\n");
  1807. break;
  1808. case POWERPC_EXCP_STOP: /* stop translation */
  1809. /* We did invalidate the instruction cache. Go on */
  1810. break;
  1811. case POWERPC_EXCP_BRANCH: /* branch instruction: */
  1812. /* We just stopped because of a branch. Go on */
  1813. break;
  1814. case POWERPC_EXCP_SYSCALL_USER:
  1815. /* system call in user-mode emulation */
  1816. /* WARNING:
  1817. * PPC ABI uses overflow flag in cr0 to signal an error
  1818. * in syscalls.
  1819. */
  1820. env->crf[0] &= ~0x1;
  1821. ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
  1822. env->gpr[5], env->gpr[6], env->gpr[7],
  1823. env->gpr[8], 0, 0);
  1824. if (ret == (target_ulong)(-TARGET_QEMU_ESIGRETURN)) {
  1825. /* Returning from a successful sigreturn syscall.
  1826. Avoid corrupting register state. */
  1827. break;
  1828. }
  1829. if (ret > (target_ulong)(-515)) {
  1830. env->crf[0] |= 0x1;
  1831. ret = -ret;
  1832. }
  1833. env->gpr[3] = ret;
  1834. break;
  1835. case POWERPC_EXCP_STCX:
  1836. if (do_store_exclusive(env)) {
  1837. info.si_signo = TARGET_SIGSEGV;
  1838. info.si_errno = 0;
  1839. info.si_code = TARGET_SEGV_MAPERR;
  1840. info._sifields._sigfault._addr = env->nip;
  1841. queue_signal(env, info.si_signo, &info);
  1842. }
  1843. break;
  1844. case EXCP_DEBUG:
  1845. {
  1846. int sig;
  1847. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  1848. if (sig) {
  1849. info.si_signo = sig;
  1850. info.si_errno = 0;
  1851. info.si_code = TARGET_TRAP_BRKPT;
  1852. queue_signal(env, info.si_signo, &info);
  1853. }
  1854. }
  1855. break;
  1856. case EXCP_INTERRUPT:
  1857. /* just indicate that signals should be handled asap */
  1858. break;
  1859. default:
  1860. cpu_abort(cs, "Unknown exception 0x%d. Aborting\n", trapnr);
  1861. break;
  1862. }
  1863. process_pending_signals(env);
  1864. }
  1865. }
  1866. #endif
  1867. #ifdef TARGET_MIPS
  1868. # ifdef TARGET_ABI_MIPSO32
  1869. # define MIPS_SYS(name, args) args,
  1870. static const uint8_t mips_syscall_args[] = {
  1871. MIPS_SYS(sys_syscall , 8) /* 4000 */
  1872. MIPS_SYS(sys_exit , 1)
  1873. MIPS_SYS(sys_fork , 0)
  1874. MIPS_SYS(sys_read , 3)
  1875. MIPS_SYS(sys_write , 3)
  1876. MIPS_SYS(sys_open , 3) /* 4005 */
  1877. MIPS_SYS(sys_close , 1)
  1878. MIPS_SYS(sys_waitpid , 3)
  1879. MIPS_SYS(sys_creat , 2)
  1880. MIPS_SYS(sys_link , 2)
  1881. MIPS_SYS(sys_unlink , 1) /* 4010 */
  1882. MIPS_SYS(sys_execve , 0)
  1883. MIPS_SYS(sys_chdir , 1)
  1884. MIPS_SYS(sys_time , 1)
  1885. MIPS_SYS(sys_mknod , 3)
  1886. MIPS_SYS(sys_chmod , 2) /* 4015 */
  1887. MIPS_SYS(sys_lchown , 3)
  1888. MIPS_SYS(sys_ni_syscall , 0)
  1889. MIPS_SYS(sys_ni_syscall , 0) /* was sys_stat */
  1890. MIPS_SYS(sys_lseek , 3)
  1891. MIPS_SYS(sys_getpid , 0) /* 4020 */
  1892. MIPS_SYS(sys_mount , 5)
  1893. MIPS_SYS(sys_umount , 1)
  1894. MIPS_SYS(sys_setuid , 1)
  1895. MIPS_SYS(sys_getuid , 0)
  1896. MIPS_SYS(sys_stime , 1) /* 4025 */
  1897. MIPS_SYS(sys_ptrace , 4)
  1898. MIPS_SYS(sys_alarm , 1)
  1899. MIPS_SYS(sys_ni_syscall , 0) /* was sys_fstat */
  1900. MIPS_SYS(sys_pause , 0)
  1901. MIPS_SYS(sys_utime , 2) /* 4030 */
  1902. MIPS_SYS(sys_ni_syscall , 0)
  1903. MIPS_SYS(sys_ni_syscall , 0)
  1904. MIPS_SYS(sys_access , 2)
  1905. MIPS_SYS(sys_nice , 1)
  1906. MIPS_SYS(sys_ni_syscall , 0) /* 4035 */
  1907. MIPS_SYS(sys_sync , 0)
  1908. MIPS_SYS(sys_kill , 2)
  1909. MIPS_SYS(sys_rename , 2)
  1910. MIPS_SYS(sys_mkdir , 2)
  1911. MIPS_SYS(sys_rmdir , 1) /* 4040 */
  1912. MIPS_SYS(sys_dup , 1)
  1913. MIPS_SYS(sys_pipe , 0)
  1914. MIPS_SYS(sys_times , 1)
  1915. MIPS_SYS(sys_ni_syscall , 0)
  1916. MIPS_SYS(sys_brk , 1) /* 4045 */
  1917. MIPS_SYS(sys_setgid , 1)
  1918. MIPS_SYS(sys_getgid , 0)
  1919. MIPS_SYS(sys_ni_syscall , 0) /* was signal(2) */
  1920. MIPS_SYS(sys_geteuid , 0)
  1921. MIPS_SYS(sys_getegid , 0) /* 4050 */
  1922. MIPS_SYS(sys_acct , 0)
  1923. MIPS_SYS(sys_umount2 , 2)
  1924. MIPS_SYS(sys_ni_syscall , 0)
  1925. MIPS_SYS(sys_ioctl , 3)
  1926. MIPS_SYS(sys_fcntl , 3) /* 4055 */
  1927. MIPS_SYS(sys_ni_syscall , 2)
  1928. MIPS_SYS(sys_setpgid , 2)
  1929. MIPS_SYS(sys_ni_syscall , 0)
  1930. MIPS_SYS(sys_olduname , 1)
  1931. MIPS_SYS(sys_umask , 1) /* 4060 */
  1932. MIPS_SYS(sys_chroot , 1)
  1933. MIPS_SYS(sys_ustat , 2)
  1934. MIPS_SYS(sys_dup2 , 2)
  1935. MIPS_SYS(sys_getppid , 0)
  1936. MIPS_SYS(sys_getpgrp , 0) /* 4065 */
  1937. MIPS_SYS(sys_setsid , 0)
  1938. MIPS_SYS(sys_sigaction , 3)
  1939. MIPS_SYS(sys_sgetmask , 0)
  1940. MIPS_SYS(sys_ssetmask , 1)
  1941. MIPS_SYS(sys_setreuid , 2) /* 4070 */
  1942. MIPS_SYS(sys_setregid , 2)
  1943. MIPS_SYS(sys_sigsuspend , 0)
  1944. MIPS_SYS(sys_sigpending , 1)
  1945. MIPS_SYS(sys_sethostname , 2)
  1946. MIPS_SYS(sys_setrlimit , 2) /* 4075 */
  1947. MIPS_SYS(sys_getrlimit , 2)
  1948. MIPS_SYS(sys_getrusage , 2)
  1949. MIPS_SYS(sys_gettimeofday, 2)
  1950. MIPS_SYS(sys_settimeofday, 2)
  1951. MIPS_SYS(sys_getgroups , 2) /* 4080 */
  1952. MIPS_SYS(sys_setgroups , 2)
  1953. MIPS_SYS(sys_ni_syscall , 0) /* old_select */
  1954. MIPS_SYS(sys_symlink , 2)
  1955. MIPS_SYS(sys_ni_syscall , 0) /* was sys_lstat */
  1956. MIPS_SYS(sys_readlink , 3) /* 4085 */
  1957. MIPS_SYS(sys_uselib , 1)
  1958. MIPS_SYS(sys_swapon , 2)
  1959. MIPS_SYS(sys_reboot , 3)
  1960. MIPS_SYS(old_readdir , 3)
  1961. MIPS_SYS(old_mmap , 6) /* 4090 */
  1962. MIPS_SYS(sys_munmap , 2)
  1963. MIPS_SYS(sys_truncate , 2)
  1964. MIPS_SYS(sys_ftruncate , 2)
  1965. MIPS_SYS(sys_fchmod , 2)
  1966. MIPS_SYS(sys_fchown , 3) /* 4095 */
  1967. MIPS_SYS(sys_getpriority , 2)
  1968. MIPS_SYS(sys_setpriority , 3)
  1969. MIPS_SYS(sys_ni_syscall , 0)
  1970. MIPS_SYS(sys_statfs , 2)
  1971. MIPS_SYS(sys_fstatfs , 2) /* 4100 */
  1972. MIPS_SYS(sys_ni_syscall , 0) /* was ioperm(2) */
  1973. MIPS_SYS(sys_socketcall , 2)
  1974. MIPS_SYS(sys_syslog , 3)
  1975. MIPS_SYS(sys_setitimer , 3)
  1976. MIPS_SYS(sys_getitimer , 2) /* 4105 */
  1977. MIPS_SYS(sys_newstat , 2)
  1978. MIPS_SYS(sys_newlstat , 2)
  1979. MIPS_SYS(sys_newfstat , 2)
  1980. MIPS_SYS(sys_uname , 1)
  1981. MIPS_SYS(sys_ni_syscall , 0) /* 4110 was iopl(2) */
  1982. MIPS_SYS(sys_vhangup , 0)
  1983. MIPS_SYS(sys_ni_syscall , 0) /* was sys_idle() */
  1984. MIPS_SYS(sys_ni_syscall , 0) /* was sys_vm86 */
  1985. MIPS_SYS(sys_wait4 , 4)
  1986. MIPS_SYS(sys_swapoff , 1) /* 4115 */
  1987. MIPS_SYS(sys_sysinfo , 1)
  1988. MIPS_SYS(sys_ipc , 6)
  1989. MIPS_SYS(sys_fsync , 1)
  1990. MIPS_SYS(sys_sigreturn , 0)
  1991. MIPS_SYS(sys_clone , 6) /* 4120 */
  1992. MIPS_SYS(sys_setdomainname, 2)
  1993. MIPS_SYS(sys_newuname , 1)
  1994. MIPS_SYS(sys_ni_syscall , 0) /* sys_modify_ldt */
  1995. MIPS_SYS(sys_adjtimex , 1)
  1996. MIPS_SYS(sys_mprotect , 3) /* 4125 */
  1997. MIPS_SYS(sys_sigprocmask , 3)
  1998. MIPS_SYS(sys_ni_syscall , 0) /* was create_module */
  1999. MIPS_SYS(sys_init_module , 5)
  2000. MIPS_SYS(sys_delete_module, 1)
  2001. MIPS_SYS(sys_ni_syscall , 0) /* 4130 was get_kernel_syms */
  2002. MIPS_SYS(sys_quotactl , 0)
  2003. MIPS_SYS(sys_getpgid , 1)
  2004. MIPS_SYS(sys_fchdir , 1)
  2005. MIPS_SYS(sys_bdflush , 2)
  2006. MIPS_SYS(sys_sysfs , 3) /* 4135 */
  2007. MIPS_SYS(sys_personality , 1)
  2008. MIPS_SYS(sys_ni_syscall , 0) /* for afs_syscall */
  2009. MIPS_SYS(sys_setfsuid , 1)
  2010. MIPS_SYS(sys_setfsgid , 1)
  2011. MIPS_SYS(sys_llseek , 5) /* 4140 */
  2012. MIPS_SYS(sys_getdents , 3)
  2013. MIPS_SYS(sys_select , 5)
  2014. MIPS_SYS(sys_flock , 2)
  2015. MIPS_SYS(sys_msync , 3)
  2016. MIPS_SYS(sys_readv , 3) /* 4145 */
  2017. MIPS_SYS(sys_writev , 3)
  2018. MIPS_SYS(sys_cacheflush , 3)
  2019. MIPS_SYS(sys_cachectl , 3)
  2020. MIPS_SYS(sys_sysmips , 4)
  2021. MIPS_SYS(sys_ni_syscall , 0) /* 4150 */
  2022. MIPS_SYS(sys_getsid , 1)
  2023. MIPS_SYS(sys_fdatasync , 0)
  2024. MIPS_SYS(sys_sysctl , 1)
  2025. MIPS_SYS(sys_mlock , 2)
  2026. MIPS_SYS(sys_munlock , 2) /* 4155 */
  2027. MIPS_SYS(sys_mlockall , 1)
  2028. MIPS_SYS(sys_munlockall , 0)
  2029. MIPS_SYS(sys_sched_setparam, 2)
  2030. MIPS_SYS(sys_sched_getparam, 2)
  2031. MIPS_SYS(sys_sched_setscheduler, 3) /* 4160 */
  2032. MIPS_SYS(sys_sched_getscheduler, 1)
  2033. MIPS_SYS(sys_sched_yield , 0)
  2034. MIPS_SYS(sys_sched_get_priority_max, 1)
  2035. MIPS_SYS(sys_sched_get_priority_min, 1)
  2036. MIPS_SYS(sys_sched_rr_get_interval, 2) /* 4165 */
  2037. MIPS_SYS(sys_nanosleep, 2)
  2038. MIPS_SYS(sys_mremap , 5)
  2039. MIPS_SYS(sys_accept , 3)
  2040. MIPS_SYS(sys_bind , 3)
  2041. MIPS_SYS(sys_connect , 3) /* 4170 */
  2042. MIPS_SYS(sys_getpeername , 3)
  2043. MIPS_SYS(sys_getsockname , 3)
  2044. MIPS_SYS(sys_getsockopt , 5)
  2045. MIPS_SYS(sys_listen , 2)
  2046. MIPS_SYS(sys_recv , 4) /* 4175 */
  2047. MIPS_SYS(sys_recvfrom , 6)
  2048. MIPS_SYS(sys_recvmsg , 3)
  2049. MIPS_SYS(sys_send , 4)
  2050. MIPS_SYS(sys_sendmsg , 3)
  2051. MIPS_SYS(sys_sendto , 6) /* 4180 */
  2052. MIPS_SYS(sys_setsockopt , 5)
  2053. MIPS_SYS(sys_shutdown , 2)
  2054. MIPS_SYS(sys_socket , 3)
  2055. MIPS_SYS(sys_socketpair , 4)
  2056. MIPS_SYS(sys_setresuid , 3) /* 4185 */
  2057. MIPS_SYS(sys_getresuid , 3)
  2058. MIPS_SYS(sys_ni_syscall , 0) /* was sys_query_module */
  2059. MIPS_SYS(sys_poll , 3)
  2060. MIPS_SYS(sys_nfsservctl , 3)
  2061. MIPS_SYS(sys_setresgid , 3) /* 4190 */
  2062. MIPS_SYS(sys_getresgid , 3)
  2063. MIPS_SYS(sys_prctl , 5)
  2064. MIPS_SYS(sys_rt_sigreturn, 0)
  2065. MIPS_SYS(sys_rt_sigaction, 4)
  2066. MIPS_SYS(sys_rt_sigprocmask, 4) /* 4195 */
  2067. MIPS_SYS(sys_rt_sigpending, 2)
  2068. MIPS_SYS(sys_rt_sigtimedwait, 4)
  2069. MIPS_SYS(sys_rt_sigqueueinfo, 3)
  2070. MIPS_SYS(sys_rt_sigsuspend, 0)
  2071. MIPS_SYS(sys_pread64 , 6) /* 4200 */
  2072. MIPS_SYS(sys_pwrite64 , 6)
  2073. MIPS_SYS(sys_chown , 3)
  2074. MIPS_SYS(sys_getcwd , 2)
  2075. MIPS_SYS(sys_capget , 2)
  2076. MIPS_SYS(sys_capset , 2) /* 4205 */
  2077. MIPS_SYS(sys_sigaltstack , 2)
  2078. MIPS_SYS(sys_sendfile , 4)
  2079. MIPS_SYS(sys_ni_syscall , 0)
  2080. MIPS_SYS(sys_ni_syscall , 0)
  2081. MIPS_SYS(sys_mmap2 , 6) /* 4210 */
  2082. MIPS_SYS(sys_truncate64 , 4)
  2083. MIPS_SYS(sys_ftruncate64 , 4)
  2084. MIPS_SYS(sys_stat64 , 2)
  2085. MIPS_SYS(sys_lstat64 , 2)
  2086. MIPS_SYS(sys_fstat64 , 2) /* 4215 */
  2087. MIPS_SYS(sys_pivot_root , 2)
  2088. MIPS_SYS(sys_mincore , 3)
  2089. MIPS_SYS(sys_madvise , 3)
  2090. MIPS_SYS(sys_getdents64 , 3)
  2091. MIPS_SYS(sys_fcntl64 , 3) /* 4220 */
  2092. MIPS_SYS(sys_ni_syscall , 0)
  2093. MIPS_SYS(sys_gettid , 0)
  2094. MIPS_SYS(sys_readahead , 5)
  2095. MIPS_SYS(sys_setxattr , 5)
  2096. MIPS_SYS(sys_lsetxattr , 5) /* 4225 */
  2097. MIPS_SYS(sys_fsetxattr , 5)
  2098. MIPS_SYS(sys_getxattr , 4)
  2099. MIPS_SYS(sys_lgetxattr , 4)
  2100. MIPS_SYS(sys_fgetxattr , 4)
  2101. MIPS_SYS(sys_listxattr , 3) /* 4230 */
  2102. MIPS_SYS(sys_llistxattr , 3)
  2103. MIPS_SYS(sys_flistxattr , 3)
  2104. MIPS_SYS(sys_removexattr , 2)
  2105. MIPS_SYS(sys_lremovexattr, 2)
  2106. MIPS_SYS(sys_fremovexattr, 2) /* 4235 */
  2107. MIPS_SYS(sys_tkill , 2)
  2108. MIPS_SYS(sys_sendfile64 , 5)
  2109. MIPS_SYS(sys_futex , 6)
  2110. MIPS_SYS(sys_sched_setaffinity, 3)
  2111. MIPS_SYS(sys_sched_getaffinity, 3) /* 4240 */
  2112. MIPS_SYS(sys_io_setup , 2)
  2113. MIPS_SYS(sys_io_destroy , 1)
  2114. MIPS_SYS(sys_io_getevents, 5)
  2115. MIPS_SYS(sys_io_submit , 3)
  2116. MIPS_SYS(sys_io_cancel , 3) /* 4245 */
  2117. MIPS_SYS(sys_exit_group , 1)
  2118. MIPS_SYS(sys_lookup_dcookie, 3)
  2119. MIPS_SYS(sys_epoll_create, 1)
  2120. MIPS_SYS(sys_epoll_ctl , 4)
  2121. MIPS_SYS(sys_epoll_wait , 3) /* 4250 */
  2122. MIPS_SYS(sys_remap_file_pages, 5)
  2123. MIPS_SYS(sys_set_tid_address, 1)
  2124. MIPS_SYS(sys_restart_syscall, 0)
  2125. MIPS_SYS(sys_fadvise64_64, 7)
  2126. MIPS_SYS(sys_statfs64 , 3) /* 4255 */
  2127. MIPS_SYS(sys_fstatfs64 , 2)
  2128. MIPS_SYS(sys_timer_create, 3)
  2129. MIPS_SYS(sys_timer_settime, 4)
  2130. MIPS_SYS(sys_timer_gettime, 2)
  2131. MIPS_SYS(sys_timer_getoverrun, 1) /* 4260 */
  2132. MIPS_SYS(sys_timer_delete, 1)
  2133. MIPS_SYS(sys_clock_settime, 2)
  2134. MIPS_SYS(sys_clock_gettime, 2)
  2135. MIPS_SYS(sys_clock_getres, 2)
  2136. MIPS_SYS(sys_clock_nanosleep, 4) /* 4265 */
  2137. MIPS_SYS(sys_tgkill , 3)
  2138. MIPS_SYS(sys_utimes , 2)
  2139. MIPS_SYS(sys_mbind , 4)
  2140. MIPS_SYS(sys_ni_syscall , 0) /* sys_get_mempolicy */
  2141. MIPS_SYS(sys_ni_syscall , 0) /* 4270 sys_set_mempolicy */
  2142. MIPS_SYS(sys_mq_open , 4)
  2143. MIPS_SYS(sys_mq_unlink , 1)
  2144. MIPS_SYS(sys_mq_timedsend, 5)
  2145. MIPS_SYS(sys_mq_timedreceive, 5)
  2146. MIPS_SYS(sys_mq_notify , 2) /* 4275 */
  2147. MIPS_SYS(sys_mq_getsetattr, 3)
  2148. MIPS_SYS(sys_ni_syscall , 0) /* sys_vserver */
  2149. MIPS_SYS(sys_waitid , 4)
  2150. MIPS_SYS(sys_ni_syscall , 0) /* available, was setaltroot */
  2151. MIPS_SYS(sys_add_key , 5)
  2152. MIPS_SYS(sys_request_key, 4)
  2153. MIPS_SYS(sys_keyctl , 5)
  2154. MIPS_SYS(sys_set_thread_area, 1)
  2155. MIPS_SYS(sys_inotify_init, 0)
  2156. MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
  2157. MIPS_SYS(sys_inotify_rm_watch, 2)
  2158. MIPS_SYS(sys_migrate_pages, 4)
  2159. MIPS_SYS(sys_openat, 4)
  2160. MIPS_SYS(sys_mkdirat, 3)
  2161. MIPS_SYS(sys_mknodat, 4) /* 4290 */
  2162. MIPS_SYS(sys_fchownat, 5)
  2163. MIPS_SYS(sys_futimesat, 3)
  2164. MIPS_SYS(sys_fstatat64, 4)
  2165. MIPS_SYS(sys_unlinkat, 3)
  2166. MIPS_SYS(sys_renameat, 4) /* 4295 */
  2167. MIPS_SYS(sys_linkat, 5)
  2168. MIPS_SYS(sys_symlinkat, 3)
  2169. MIPS_SYS(sys_readlinkat, 4)
  2170. MIPS_SYS(sys_fchmodat, 3)
  2171. MIPS_SYS(sys_faccessat, 3) /* 4300 */
  2172. MIPS_SYS(sys_pselect6, 6)
  2173. MIPS_SYS(sys_ppoll, 5)
  2174. MIPS_SYS(sys_unshare, 1)
  2175. MIPS_SYS(sys_splice, 6)
  2176. MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
  2177. MIPS_SYS(sys_tee, 4)
  2178. MIPS_SYS(sys_vmsplice, 4)
  2179. MIPS_SYS(sys_move_pages, 6)
  2180. MIPS_SYS(sys_set_robust_list, 2)
  2181. MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
  2182. MIPS_SYS(sys_kexec_load, 4)
  2183. MIPS_SYS(sys_getcpu, 3)
  2184. MIPS_SYS(sys_epoll_pwait, 6)
  2185. MIPS_SYS(sys_ioprio_set, 3)
  2186. MIPS_SYS(sys_ioprio_get, 2)
  2187. MIPS_SYS(sys_utimensat, 4)
  2188. MIPS_SYS(sys_signalfd, 3)
  2189. MIPS_SYS(sys_ni_syscall, 0) /* was timerfd */
  2190. MIPS_SYS(sys_eventfd, 1)
  2191. MIPS_SYS(sys_fallocate, 6) /* 4320 */
  2192. MIPS_SYS(sys_timerfd_create, 2)
  2193. MIPS_SYS(sys_timerfd_gettime, 2)
  2194. MIPS_SYS(sys_timerfd_settime, 4)
  2195. MIPS_SYS(sys_signalfd4, 4)
  2196. MIPS_SYS(sys_eventfd2, 2) /* 4325 */
  2197. MIPS_SYS(sys_epoll_create1, 1)
  2198. MIPS_SYS(sys_dup3, 3)
  2199. MIPS_SYS(sys_pipe2, 2)
  2200. MIPS_SYS(sys_inotify_init1, 1)
  2201. MIPS_SYS(sys_preadv, 6) /* 4330 */
  2202. MIPS_SYS(sys_pwritev, 6)
  2203. MIPS_SYS(sys_rt_tgsigqueueinfo, 4)
  2204. MIPS_SYS(sys_perf_event_open, 5)
  2205. MIPS_SYS(sys_accept4, 4)
  2206. MIPS_SYS(sys_recvmmsg, 5) /* 4335 */
  2207. MIPS_SYS(sys_fanotify_init, 2)
  2208. MIPS_SYS(sys_fanotify_mark, 6)
  2209. MIPS_SYS(sys_prlimit64, 4)
  2210. MIPS_SYS(sys_name_to_handle_at, 5)
  2211. MIPS_SYS(sys_open_by_handle_at, 3) /* 4340 */
  2212. MIPS_SYS(sys_clock_adjtime, 2)
  2213. MIPS_SYS(sys_syncfs, 1)
  2214. };
  2215. # undef MIPS_SYS
  2216. # endif /* O32 */
  2217. static int do_store_exclusive(CPUMIPSState *env)
  2218. {
  2219. target_ulong addr;
  2220. target_ulong page_addr;
  2221. target_ulong val;
  2222. int flags;
  2223. int segv = 0;
  2224. int reg;
  2225. int d;
  2226. addr = env->lladdr;
  2227. page_addr = addr & TARGET_PAGE_MASK;
  2228. start_exclusive();
  2229. mmap_lock();
  2230. flags = page_get_flags(page_addr);
  2231. if ((flags & PAGE_READ) == 0) {
  2232. segv = 1;
  2233. } else {
  2234. reg = env->llreg & 0x1f;
  2235. d = (env->llreg & 0x20) != 0;
  2236. if (d) {
  2237. segv = get_user_s64(val, addr);
  2238. } else {
  2239. segv = get_user_s32(val, addr);
  2240. }
  2241. if (!segv) {
  2242. if (val != env->llval) {
  2243. env->active_tc.gpr[reg] = 0;
  2244. } else {
  2245. if (d) {
  2246. segv = put_user_u64(env->llnewval, addr);
  2247. } else {
  2248. segv = put_user_u32(env->llnewval, addr);
  2249. }
  2250. if (!segv) {
  2251. env->active_tc.gpr[reg] = 1;
  2252. }
  2253. }
  2254. }
  2255. }
  2256. env->lladdr = -1;
  2257. if (!segv) {
  2258. env->active_tc.PC += 4;
  2259. }
  2260. mmap_unlock();
  2261. end_exclusive();
  2262. return segv;
  2263. }
  2264. /* Break codes */
  2265. enum {
  2266. BRK_OVERFLOW = 6,
  2267. BRK_DIVZERO = 7
  2268. };
  2269. static int do_break(CPUMIPSState *env, target_siginfo_t *info,
  2270. unsigned int code)
  2271. {
  2272. int ret = -1;
  2273. switch (code) {
  2274. case BRK_OVERFLOW:
  2275. case BRK_DIVZERO:
  2276. info->si_signo = TARGET_SIGFPE;
  2277. info->si_errno = 0;
  2278. info->si_code = (code == BRK_OVERFLOW) ? FPE_INTOVF : FPE_INTDIV;
  2279. queue_signal(env, info->si_signo, &*info);
  2280. ret = 0;
  2281. break;
  2282. default:
  2283. info->si_signo = TARGET_SIGTRAP;
  2284. info->si_errno = 0;
  2285. queue_signal(env, info->si_signo, &*info);
  2286. ret = 0;
  2287. break;
  2288. }
  2289. return ret;
  2290. }
  2291. void cpu_loop(CPUMIPSState *env)
  2292. {
  2293. CPUState *cs = CPU(mips_env_get_cpu(env));
  2294. target_siginfo_t info;
  2295. int trapnr;
  2296. abi_long ret;
  2297. # ifdef TARGET_ABI_MIPSO32
  2298. unsigned int syscall_num;
  2299. # endif
  2300. for(;;) {
  2301. cpu_exec_start(cs);
  2302. trapnr = cpu_mips_exec(env);
  2303. cpu_exec_end(cs);
  2304. switch(trapnr) {
  2305. case EXCP_SYSCALL:
  2306. env->active_tc.PC += 4;
  2307. # ifdef TARGET_ABI_MIPSO32
  2308. syscall_num = env->active_tc.gpr[2] - 4000;
  2309. if (syscall_num >= sizeof(mips_syscall_args)) {
  2310. ret = -TARGET_ENOSYS;
  2311. } else {
  2312. int nb_args;
  2313. abi_ulong sp_reg;
  2314. abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
  2315. nb_args = mips_syscall_args[syscall_num];
  2316. sp_reg = env->active_tc.gpr[29];
  2317. switch (nb_args) {
  2318. /* these arguments are taken from the stack */
  2319. case 8:
  2320. if ((ret = get_user_ual(arg8, sp_reg + 28)) != 0) {
  2321. goto done_syscall;
  2322. }
  2323. case 7:
  2324. if ((ret = get_user_ual(arg7, sp_reg + 24)) != 0) {
  2325. goto done_syscall;
  2326. }
  2327. case 6:
  2328. if ((ret = get_user_ual(arg6, sp_reg + 20)) != 0) {
  2329. goto done_syscall;
  2330. }
  2331. case 5:
  2332. if ((ret = get_user_ual(arg5, sp_reg + 16)) != 0) {
  2333. goto done_syscall;
  2334. }
  2335. default:
  2336. break;
  2337. }
  2338. ret = do_syscall(env, env->active_tc.gpr[2],
  2339. env->active_tc.gpr[4],
  2340. env->active_tc.gpr[5],
  2341. env->active_tc.gpr[6],
  2342. env->active_tc.gpr[7],
  2343. arg5, arg6, arg7, arg8);
  2344. }
  2345. done_syscall:
  2346. # else
  2347. ret = do_syscall(env, env->active_tc.gpr[2],
  2348. env->active_tc.gpr[4], env->active_tc.gpr[5],
  2349. env->active_tc.gpr[6], env->active_tc.gpr[7],
  2350. env->active_tc.gpr[8], env->active_tc.gpr[9],
  2351. env->active_tc.gpr[10], env->active_tc.gpr[11]);
  2352. # endif /* O32 */
  2353. if (ret == -TARGET_QEMU_ESIGRETURN) {
  2354. /* Returning from a successful sigreturn syscall.
  2355. Avoid clobbering register state. */
  2356. break;
  2357. }
  2358. if ((abi_ulong)ret >= (abi_ulong)-1133) {
  2359. env->active_tc.gpr[7] = 1; /* error flag */
  2360. ret = -ret;
  2361. } else {
  2362. env->active_tc.gpr[7] = 0; /* error flag */
  2363. }
  2364. env->active_tc.gpr[2] = ret;
  2365. break;
  2366. case EXCP_TLBL:
  2367. case EXCP_TLBS:
  2368. case EXCP_AdEL:
  2369. case EXCP_AdES:
  2370. info.si_signo = TARGET_SIGSEGV;
  2371. info.si_errno = 0;
  2372. /* XXX: check env->error_code */
  2373. info.si_code = TARGET_SEGV_MAPERR;
  2374. info._sifields._sigfault._addr = env->CP0_BadVAddr;
  2375. queue_signal(env, info.si_signo, &info);
  2376. break;
  2377. case EXCP_CpU:
  2378. case EXCP_RI:
  2379. info.si_signo = TARGET_SIGILL;
  2380. info.si_errno = 0;
  2381. info.si_code = 0;
  2382. queue_signal(env, info.si_signo, &info);
  2383. break;
  2384. case EXCP_INTERRUPT:
  2385. /* just indicate that signals should be handled asap */
  2386. break;
  2387. case EXCP_DEBUG:
  2388. {
  2389. int sig;
  2390. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  2391. if (sig)
  2392. {
  2393. info.si_signo = sig;
  2394. info.si_errno = 0;
  2395. info.si_code = TARGET_TRAP_BRKPT;
  2396. queue_signal(env, info.si_signo, &info);
  2397. }
  2398. }
  2399. break;
  2400. case EXCP_SC:
  2401. if (do_store_exclusive(env)) {
  2402. info.si_signo = TARGET_SIGSEGV;
  2403. info.si_errno = 0;
  2404. info.si_code = TARGET_SEGV_MAPERR;
  2405. info._sifields._sigfault._addr = env->active_tc.PC;
  2406. queue_signal(env, info.si_signo, &info);
  2407. }
  2408. break;
  2409. case EXCP_DSPDIS:
  2410. info.si_signo = TARGET_SIGILL;
  2411. info.si_errno = 0;
  2412. info.si_code = TARGET_ILL_ILLOPC;
  2413. queue_signal(env, info.si_signo, &info);
  2414. break;
  2415. /* The code below was inspired by the MIPS Linux kernel trap
  2416. * handling code in arch/mips/kernel/traps.c.
  2417. */
  2418. case EXCP_BREAK:
  2419. {
  2420. abi_ulong trap_instr;
  2421. unsigned int code;
  2422. if (env->hflags & MIPS_HFLAG_M16) {
  2423. if (env->insn_flags & ASE_MICROMIPS) {
  2424. /* microMIPS mode */
  2425. ret = get_user_u16(trap_instr, env->active_tc.PC);
  2426. if (ret != 0) {
  2427. goto error;
  2428. }
  2429. if ((trap_instr >> 10) == 0x11) {
  2430. /* 16-bit instruction */
  2431. code = trap_instr & 0xf;
  2432. } else {
  2433. /* 32-bit instruction */
  2434. abi_ulong instr_lo;
  2435. ret = get_user_u16(instr_lo,
  2436. env->active_tc.PC + 2);
  2437. if (ret != 0) {
  2438. goto error;
  2439. }
  2440. trap_instr = (trap_instr << 16) | instr_lo;
  2441. code = ((trap_instr >> 6) & ((1 << 20) - 1));
  2442. /* Unfortunately, microMIPS also suffers from
  2443. the old assembler bug... */
  2444. if (code >= (1 << 10)) {
  2445. code >>= 10;
  2446. }
  2447. }
  2448. } else {
  2449. /* MIPS16e mode */
  2450. ret = get_user_u16(trap_instr, env->active_tc.PC);
  2451. if (ret != 0) {
  2452. goto error;
  2453. }
  2454. code = (trap_instr >> 6) & 0x3f;
  2455. }
  2456. } else {
  2457. ret = get_user_ual(trap_instr, env->active_tc.PC);
  2458. if (ret != 0) {
  2459. goto error;
  2460. }
  2461. /* As described in the original Linux kernel code, the
  2462. * below checks on 'code' are to work around an old
  2463. * assembly bug.
  2464. */
  2465. code = ((trap_instr >> 6) & ((1 << 20) - 1));
  2466. if (code >= (1 << 10)) {
  2467. code >>= 10;
  2468. }
  2469. }
  2470. if (do_break(env, &info, code) != 0) {
  2471. goto error;
  2472. }
  2473. }
  2474. break;
  2475. case EXCP_TRAP:
  2476. {
  2477. abi_ulong trap_instr;
  2478. unsigned int code = 0;
  2479. if (env->hflags & MIPS_HFLAG_M16) {
  2480. /* microMIPS mode */
  2481. abi_ulong instr[2];
  2482. ret = get_user_u16(instr[0], env->active_tc.PC) ||
  2483. get_user_u16(instr[1], env->active_tc.PC + 2);
  2484. trap_instr = (instr[0] << 16) | instr[1];
  2485. } else {
  2486. ret = get_user_ual(trap_instr, env->active_tc.PC);
  2487. }
  2488. if (ret != 0) {
  2489. goto error;
  2490. }
  2491. /* The immediate versions don't provide a code. */
  2492. if (!(trap_instr & 0xFC000000)) {
  2493. if (env->hflags & MIPS_HFLAG_M16) {
  2494. /* microMIPS mode */
  2495. code = ((trap_instr >> 12) & ((1 << 4) - 1));
  2496. } else {
  2497. code = ((trap_instr >> 6) & ((1 << 10) - 1));
  2498. }
  2499. }
  2500. if (do_break(env, &info, code) != 0) {
  2501. goto error;
  2502. }
  2503. }
  2504. break;
  2505. default:
  2506. error:
  2507. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
  2508. trapnr);
  2509. cpu_dump_state(cs, stderr, fprintf, 0);
  2510. abort();
  2511. }
  2512. process_pending_signals(env);
  2513. }
  2514. }
  2515. #endif
  2516. #ifdef TARGET_OPENRISC
  2517. void cpu_loop(CPUOpenRISCState *env)
  2518. {
  2519. CPUState *cs = CPU(openrisc_env_get_cpu(env));
  2520. int trapnr, gdbsig;
  2521. for (;;) {
  2522. trapnr = cpu_exec(env);
  2523. gdbsig = 0;
  2524. switch (trapnr) {
  2525. case EXCP_RESET:
  2526. qemu_log("\nReset request, exit, pc is %#x\n", env->pc);
  2527. exit(1);
  2528. break;
  2529. case EXCP_BUSERR:
  2530. qemu_log("\nBus error, exit, pc is %#x\n", env->pc);
  2531. gdbsig = SIGBUS;
  2532. break;
  2533. case EXCP_DPF:
  2534. case EXCP_IPF:
  2535. cpu_dump_state(cs, stderr, fprintf, 0);
  2536. gdbsig = TARGET_SIGSEGV;
  2537. break;
  2538. case EXCP_TICK:
  2539. qemu_log("\nTick time interrupt pc is %#x\n", env->pc);
  2540. break;
  2541. case EXCP_ALIGN:
  2542. qemu_log("\nAlignment pc is %#x\n", env->pc);
  2543. gdbsig = SIGBUS;
  2544. break;
  2545. case EXCP_ILLEGAL:
  2546. qemu_log("\nIllegal instructionpc is %#x\n", env->pc);
  2547. gdbsig = SIGILL;
  2548. break;
  2549. case EXCP_INT:
  2550. qemu_log("\nExternal interruptpc is %#x\n", env->pc);
  2551. break;
  2552. case EXCP_DTLBMISS:
  2553. case EXCP_ITLBMISS:
  2554. qemu_log("\nTLB miss\n");
  2555. break;
  2556. case EXCP_RANGE:
  2557. qemu_log("\nRange\n");
  2558. gdbsig = SIGSEGV;
  2559. break;
  2560. case EXCP_SYSCALL:
  2561. env->pc += 4; /* 0xc00; */
  2562. env->gpr[11] = do_syscall(env,
  2563. env->gpr[11], /* return value */
  2564. env->gpr[3], /* r3 - r7 are params */
  2565. env->gpr[4],
  2566. env->gpr[5],
  2567. env->gpr[6],
  2568. env->gpr[7],
  2569. env->gpr[8], 0, 0);
  2570. break;
  2571. case EXCP_FPE:
  2572. qemu_log("\nFloating point error\n");
  2573. break;
  2574. case EXCP_TRAP:
  2575. qemu_log("\nTrap\n");
  2576. gdbsig = SIGTRAP;
  2577. break;
  2578. case EXCP_NR:
  2579. qemu_log("\nNR\n");
  2580. break;
  2581. default:
  2582. qemu_log("\nqemu: unhandled CPU exception %#x - aborting\n",
  2583. trapnr);
  2584. cpu_dump_state(cs, stderr, fprintf, 0);
  2585. gdbsig = TARGET_SIGILL;
  2586. break;
  2587. }
  2588. if (gdbsig) {
  2589. gdb_handlesig(cs, gdbsig);
  2590. if (gdbsig != TARGET_SIGTRAP) {
  2591. exit(1);
  2592. }
  2593. }
  2594. process_pending_signals(env);
  2595. }
  2596. }
  2597. #endif /* TARGET_OPENRISC */
  2598. #ifdef TARGET_SH4
  2599. void cpu_loop(CPUSH4State *env)
  2600. {
  2601. CPUState *cs = CPU(sh_env_get_cpu(env));
  2602. int trapnr, ret;
  2603. target_siginfo_t info;
  2604. while (1) {
  2605. trapnr = cpu_sh4_exec (env);
  2606. switch (trapnr) {
  2607. case 0x160:
  2608. env->pc += 2;
  2609. ret = do_syscall(env,
  2610. env->gregs[3],
  2611. env->gregs[4],
  2612. env->gregs[5],
  2613. env->gregs[6],
  2614. env->gregs[7],
  2615. env->gregs[0],
  2616. env->gregs[1],
  2617. 0, 0);
  2618. env->gregs[0] = ret;
  2619. break;
  2620. case EXCP_INTERRUPT:
  2621. /* just indicate that signals should be handled asap */
  2622. break;
  2623. case EXCP_DEBUG:
  2624. {
  2625. int sig;
  2626. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  2627. if (sig)
  2628. {
  2629. info.si_signo = sig;
  2630. info.si_errno = 0;
  2631. info.si_code = TARGET_TRAP_BRKPT;
  2632. queue_signal(env, info.si_signo, &info);
  2633. }
  2634. }
  2635. break;
  2636. case 0xa0:
  2637. case 0xc0:
  2638. info.si_signo = SIGSEGV;
  2639. info.si_errno = 0;
  2640. info.si_code = TARGET_SEGV_MAPERR;
  2641. info._sifields._sigfault._addr = env->tea;
  2642. queue_signal(env, info.si_signo, &info);
  2643. break;
  2644. default:
  2645. printf ("Unhandled trap: 0x%x\n", trapnr);
  2646. cpu_dump_state(cs, stderr, fprintf, 0);
  2647. exit (1);
  2648. }
  2649. process_pending_signals (env);
  2650. }
  2651. }
  2652. #endif
  2653. #ifdef TARGET_CRIS
  2654. void cpu_loop(CPUCRISState *env)
  2655. {
  2656. CPUState *cs = CPU(cris_env_get_cpu(env));
  2657. int trapnr, ret;
  2658. target_siginfo_t info;
  2659. while (1) {
  2660. trapnr = cpu_cris_exec (env);
  2661. switch (trapnr) {
  2662. case 0xaa:
  2663. {
  2664. info.si_signo = SIGSEGV;
  2665. info.si_errno = 0;
  2666. /* XXX: check env->error_code */
  2667. info.si_code = TARGET_SEGV_MAPERR;
  2668. info._sifields._sigfault._addr = env->pregs[PR_EDA];
  2669. queue_signal(env, info.si_signo, &info);
  2670. }
  2671. break;
  2672. case EXCP_INTERRUPT:
  2673. /* just indicate that signals should be handled asap */
  2674. break;
  2675. case EXCP_BREAK:
  2676. ret = do_syscall(env,
  2677. env->regs[9],
  2678. env->regs[10],
  2679. env->regs[11],
  2680. env->regs[12],
  2681. env->regs[13],
  2682. env->pregs[7],
  2683. env->pregs[11],
  2684. 0, 0);
  2685. env->regs[10] = ret;
  2686. break;
  2687. case EXCP_DEBUG:
  2688. {
  2689. int sig;
  2690. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  2691. if (sig)
  2692. {
  2693. info.si_signo = sig;
  2694. info.si_errno = 0;
  2695. info.si_code = TARGET_TRAP_BRKPT;
  2696. queue_signal(env, info.si_signo, &info);
  2697. }
  2698. }
  2699. break;
  2700. default:
  2701. printf ("Unhandled trap: 0x%x\n", trapnr);
  2702. cpu_dump_state(cs, stderr, fprintf, 0);
  2703. exit (1);
  2704. }
  2705. process_pending_signals (env);
  2706. }
  2707. }
  2708. #endif
  2709. #ifdef TARGET_MICROBLAZE
  2710. void cpu_loop(CPUMBState *env)
  2711. {
  2712. CPUState *cs = CPU(mb_env_get_cpu(env));
  2713. int trapnr, ret;
  2714. target_siginfo_t info;
  2715. while (1) {
  2716. trapnr = cpu_mb_exec (env);
  2717. switch (trapnr) {
  2718. case 0xaa:
  2719. {
  2720. info.si_signo = SIGSEGV;
  2721. info.si_errno = 0;
  2722. /* XXX: check env->error_code */
  2723. info.si_code = TARGET_SEGV_MAPERR;
  2724. info._sifields._sigfault._addr = 0;
  2725. queue_signal(env, info.si_signo, &info);
  2726. }
  2727. break;
  2728. case EXCP_INTERRUPT:
  2729. /* just indicate that signals should be handled asap */
  2730. break;
  2731. case EXCP_BREAK:
  2732. /* Return address is 4 bytes after the call. */
  2733. env->regs[14] += 4;
  2734. env->sregs[SR_PC] = env->regs[14];
  2735. ret = do_syscall(env,
  2736. env->regs[12],
  2737. env->regs[5],
  2738. env->regs[6],
  2739. env->regs[7],
  2740. env->regs[8],
  2741. env->regs[9],
  2742. env->regs[10],
  2743. 0, 0);
  2744. env->regs[3] = ret;
  2745. break;
  2746. case EXCP_HW_EXCP:
  2747. env->regs[17] = env->sregs[SR_PC] + 4;
  2748. if (env->iflags & D_FLAG) {
  2749. env->sregs[SR_ESR] |= 1 << 12;
  2750. env->sregs[SR_PC] -= 4;
  2751. /* FIXME: if branch was immed, replay the imm as well. */
  2752. }
  2753. env->iflags &= ~(IMM_FLAG | D_FLAG);
  2754. switch (env->sregs[SR_ESR] & 31) {
  2755. case ESR_EC_DIVZERO:
  2756. info.si_signo = SIGFPE;
  2757. info.si_errno = 0;
  2758. info.si_code = TARGET_FPE_FLTDIV;
  2759. info._sifields._sigfault._addr = 0;
  2760. queue_signal(env, info.si_signo, &info);
  2761. break;
  2762. case ESR_EC_FPU:
  2763. info.si_signo = SIGFPE;
  2764. info.si_errno = 0;
  2765. if (env->sregs[SR_FSR] & FSR_IO) {
  2766. info.si_code = TARGET_FPE_FLTINV;
  2767. }
  2768. if (env->sregs[SR_FSR] & FSR_DZ) {
  2769. info.si_code = TARGET_FPE_FLTDIV;
  2770. }
  2771. info._sifields._sigfault._addr = 0;
  2772. queue_signal(env, info.si_signo, &info);
  2773. break;
  2774. default:
  2775. printf ("Unhandled hw-exception: 0x%x\n",
  2776. env->sregs[SR_ESR] & ESR_EC_MASK);
  2777. cpu_dump_state(cs, stderr, fprintf, 0);
  2778. exit (1);
  2779. break;
  2780. }
  2781. break;
  2782. case EXCP_DEBUG:
  2783. {
  2784. int sig;
  2785. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  2786. if (sig)
  2787. {
  2788. info.si_signo = sig;
  2789. info.si_errno = 0;
  2790. info.si_code = TARGET_TRAP_BRKPT;
  2791. queue_signal(env, info.si_signo, &info);
  2792. }
  2793. }
  2794. break;
  2795. default:
  2796. printf ("Unhandled trap: 0x%x\n", trapnr);
  2797. cpu_dump_state(cs, stderr, fprintf, 0);
  2798. exit (1);
  2799. }
  2800. process_pending_signals (env);
  2801. }
  2802. }
  2803. #endif
  2804. #ifdef TARGET_M68K
  2805. void cpu_loop(CPUM68KState *env)
  2806. {
  2807. CPUState *cs = CPU(m68k_env_get_cpu(env));
  2808. int trapnr;
  2809. unsigned int n;
  2810. target_siginfo_t info;
  2811. TaskState *ts = cs->opaque;
  2812. for(;;) {
  2813. trapnr = cpu_m68k_exec(env);
  2814. switch(trapnr) {
  2815. case EXCP_ILLEGAL:
  2816. {
  2817. if (ts->sim_syscalls) {
  2818. uint16_t nr;
  2819. nr = lduw(env->pc + 2);
  2820. env->pc += 4;
  2821. do_m68k_simcall(env, nr);
  2822. } else {
  2823. goto do_sigill;
  2824. }
  2825. }
  2826. break;
  2827. case EXCP_HALT_INSN:
  2828. /* Semihosing syscall. */
  2829. env->pc += 4;
  2830. do_m68k_semihosting(env, env->dregs[0]);
  2831. break;
  2832. case EXCP_LINEA:
  2833. case EXCP_LINEF:
  2834. case EXCP_UNSUPPORTED:
  2835. do_sigill:
  2836. info.si_signo = SIGILL;
  2837. info.si_errno = 0;
  2838. info.si_code = TARGET_ILL_ILLOPN;
  2839. info._sifields._sigfault._addr = env->pc;
  2840. queue_signal(env, info.si_signo, &info);
  2841. break;
  2842. case EXCP_TRAP0:
  2843. {
  2844. ts->sim_syscalls = 0;
  2845. n = env->dregs[0];
  2846. env->pc += 2;
  2847. env->dregs[0] = do_syscall(env,
  2848. n,
  2849. env->dregs[1],
  2850. env->dregs[2],
  2851. env->dregs[3],
  2852. env->dregs[4],
  2853. env->dregs[5],
  2854. env->aregs[0],
  2855. 0, 0);
  2856. }
  2857. break;
  2858. case EXCP_INTERRUPT:
  2859. /* just indicate that signals should be handled asap */
  2860. break;
  2861. case EXCP_ACCESS:
  2862. {
  2863. info.si_signo = SIGSEGV;
  2864. info.si_errno = 0;
  2865. /* XXX: check env->error_code */
  2866. info.si_code = TARGET_SEGV_MAPERR;
  2867. info._sifields._sigfault._addr = env->mmu.ar;
  2868. queue_signal(env, info.si_signo, &info);
  2869. }
  2870. break;
  2871. case EXCP_DEBUG:
  2872. {
  2873. int sig;
  2874. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  2875. if (sig)
  2876. {
  2877. info.si_signo = sig;
  2878. info.si_errno = 0;
  2879. info.si_code = TARGET_TRAP_BRKPT;
  2880. queue_signal(env, info.si_signo, &info);
  2881. }
  2882. }
  2883. break;
  2884. default:
  2885. fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
  2886. trapnr);
  2887. cpu_dump_state(cs, stderr, fprintf, 0);
  2888. abort();
  2889. }
  2890. process_pending_signals(env);
  2891. }
  2892. }
  2893. #endif /* TARGET_M68K */
  2894. #ifdef TARGET_ALPHA
  2895. static void do_store_exclusive(CPUAlphaState *env, int reg, int quad)
  2896. {
  2897. target_ulong addr, val, tmp;
  2898. target_siginfo_t info;
  2899. int ret = 0;
  2900. addr = env->lock_addr;
  2901. tmp = env->lock_st_addr;
  2902. env->lock_addr = -1;
  2903. env->lock_st_addr = 0;
  2904. start_exclusive();
  2905. mmap_lock();
  2906. if (addr == tmp) {
  2907. if (quad ? get_user_s64(val, addr) : get_user_s32(val, addr)) {
  2908. goto do_sigsegv;
  2909. }
  2910. if (val == env->lock_value) {
  2911. tmp = env->ir[reg];
  2912. if (quad ? put_user_u64(tmp, addr) : put_user_u32(tmp, addr)) {
  2913. goto do_sigsegv;
  2914. }
  2915. ret = 1;
  2916. }
  2917. }
  2918. env->ir[reg] = ret;
  2919. env->pc += 4;
  2920. mmap_unlock();
  2921. end_exclusive();
  2922. return;
  2923. do_sigsegv:
  2924. mmap_unlock();
  2925. end_exclusive();
  2926. info.si_signo = TARGET_SIGSEGV;
  2927. info.si_errno = 0;
  2928. info.si_code = TARGET_SEGV_MAPERR;
  2929. info._sifields._sigfault._addr = addr;
  2930. queue_signal(env, TARGET_SIGSEGV, &info);
  2931. }
  2932. void cpu_loop(CPUAlphaState *env)
  2933. {
  2934. CPUState *cs = CPU(alpha_env_get_cpu(env));
  2935. int trapnr;
  2936. target_siginfo_t info;
  2937. abi_long sysret;
  2938. while (1) {
  2939. trapnr = cpu_alpha_exec (env);
  2940. /* All of the traps imply a transition through PALcode, which
  2941. implies an REI instruction has been executed. Which means
  2942. that the intr_flag should be cleared. */
  2943. env->intr_flag = 0;
  2944. switch (trapnr) {
  2945. case EXCP_RESET:
  2946. fprintf(stderr, "Reset requested. Exit\n");
  2947. exit(1);
  2948. break;
  2949. case EXCP_MCHK:
  2950. fprintf(stderr, "Machine check exception. Exit\n");
  2951. exit(1);
  2952. break;
  2953. case EXCP_SMP_INTERRUPT:
  2954. case EXCP_CLK_INTERRUPT:
  2955. case EXCP_DEV_INTERRUPT:
  2956. fprintf(stderr, "External interrupt. Exit\n");
  2957. exit(1);
  2958. break;
  2959. case EXCP_MMFAULT:
  2960. env->lock_addr = -1;
  2961. info.si_signo = TARGET_SIGSEGV;
  2962. info.si_errno = 0;
  2963. info.si_code = (page_get_flags(env->trap_arg0) & PAGE_VALID
  2964. ? TARGET_SEGV_ACCERR : TARGET_SEGV_MAPERR);
  2965. info._sifields._sigfault._addr = env->trap_arg0;
  2966. queue_signal(env, info.si_signo, &info);
  2967. break;
  2968. case EXCP_UNALIGN:
  2969. env->lock_addr = -1;
  2970. info.si_signo = TARGET_SIGBUS;
  2971. info.si_errno = 0;
  2972. info.si_code = TARGET_BUS_ADRALN;
  2973. info._sifields._sigfault._addr = env->trap_arg0;
  2974. queue_signal(env, info.si_signo, &info);
  2975. break;
  2976. case EXCP_OPCDEC:
  2977. do_sigill:
  2978. env->lock_addr = -1;
  2979. info.si_signo = TARGET_SIGILL;
  2980. info.si_errno = 0;
  2981. info.si_code = TARGET_ILL_ILLOPC;
  2982. info._sifields._sigfault._addr = env->pc;
  2983. queue_signal(env, info.si_signo, &info);
  2984. break;
  2985. case EXCP_ARITH:
  2986. env->lock_addr = -1;
  2987. info.si_signo = TARGET_SIGFPE;
  2988. info.si_errno = 0;
  2989. info.si_code = TARGET_FPE_FLTINV;
  2990. info._sifields._sigfault._addr = env->pc;
  2991. queue_signal(env, info.si_signo, &info);
  2992. break;
  2993. case EXCP_FEN:
  2994. /* No-op. Linux simply re-enables the FPU. */
  2995. break;
  2996. case EXCP_CALL_PAL:
  2997. env->lock_addr = -1;
  2998. switch (env->error_code) {
  2999. case 0x80:
  3000. /* BPT */
  3001. info.si_signo = TARGET_SIGTRAP;
  3002. info.si_errno = 0;
  3003. info.si_code = TARGET_TRAP_BRKPT;
  3004. info._sifields._sigfault._addr = env->pc;
  3005. queue_signal(env, info.si_signo, &info);
  3006. break;
  3007. case 0x81:
  3008. /* BUGCHK */
  3009. info.si_signo = TARGET_SIGTRAP;
  3010. info.si_errno = 0;
  3011. info.si_code = 0;
  3012. info._sifields._sigfault._addr = env->pc;
  3013. queue_signal(env, info.si_signo, &info);
  3014. break;
  3015. case 0x83:
  3016. /* CALLSYS */
  3017. trapnr = env->ir[IR_V0];
  3018. sysret = do_syscall(env, trapnr,
  3019. env->ir[IR_A0], env->ir[IR_A1],
  3020. env->ir[IR_A2], env->ir[IR_A3],
  3021. env->ir[IR_A4], env->ir[IR_A5],
  3022. 0, 0);
  3023. if (trapnr == TARGET_NR_sigreturn
  3024. || trapnr == TARGET_NR_rt_sigreturn) {
  3025. break;
  3026. }
  3027. /* Syscall writes 0 to V0 to bypass error check, similar
  3028. to how this is handled internal to Linux kernel.
  3029. (Ab)use trapnr temporarily as boolean indicating error. */
  3030. trapnr = (env->ir[IR_V0] != 0 && sysret < 0);
  3031. env->ir[IR_V0] = (trapnr ? -sysret : sysret);
  3032. env->ir[IR_A3] = trapnr;
  3033. break;
  3034. case 0x86:
  3035. /* IMB */
  3036. /* ??? We can probably elide the code using page_unprotect
  3037. that is checking for self-modifying code. Instead we
  3038. could simply call tb_flush here. Until we work out the
  3039. changes required to turn off the extra write protection,
  3040. this can be a no-op. */
  3041. break;
  3042. case 0x9E:
  3043. /* RDUNIQUE */
  3044. /* Handled in the translator for usermode. */
  3045. abort();
  3046. case 0x9F:
  3047. /* WRUNIQUE */
  3048. /* Handled in the translator for usermode. */
  3049. abort();
  3050. case 0xAA:
  3051. /* GENTRAP */
  3052. info.si_signo = TARGET_SIGFPE;
  3053. switch (env->ir[IR_A0]) {
  3054. case TARGET_GEN_INTOVF:
  3055. info.si_code = TARGET_FPE_INTOVF;
  3056. break;
  3057. case TARGET_GEN_INTDIV:
  3058. info.si_code = TARGET_FPE_INTDIV;
  3059. break;
  3060. case TARGET_GEN_FLTOVF:
  3061. info.si_code = TARGET_FPE_FLTOVF;
  3062. break;
  3063. case TARGET_GEN_FLTUND:
  3064. info.si_code = TARGET_FPE_FLTUND;
  3065. break;
  3066. case TARGET_GEN_FLTINV:
  3067. info.si_code = TARGET_FPE_FLTINV;
  3068. break;
  3069. case TARGET_GEN_FLTINE:
  3070. info.si_code = TARGET_FPE_FLTRES;
  3071. break;
  3072. case TARGET_GEN_ROPRAND:
  3073. info.si_code = 0;
  3074. break;
  3075. default:
  3076. info.si_signo = TARGET_SIGTRAP;
  3077. info.si_code = 0;
  3078. break;
  3079. }
  3080. info.si_errno = 0;
  3081. info._sifields._sigfault._addr = env->pc;
  3082. queue_signal(env, info.si_signo, &info);
  3083. break;
  3084. default:
  3085. goto do_sigill;
  3086. }
  3087. break;
  3088. case EXCP_DEBUG:
  3089. info.si_signo = gdb_handlesig(cs, TARGET_SIGTRAP);
  3090. if (info.si_signo) {
  3091. env->lock_addr = -1;
  3092. info.si_errno = 0;
  3093. info.si_code = TARGET_TRAP_BRKPT;
  3094. queue_signal(env, info.si_signo, &info);
  3095. }
  3096. break;
  3097. case EXCP_STL_C:
  3098. case EXCP_STQ_C:
  3099. do_store_exclusive(env, env->error_code, trapnr - EXCP_STL_C);
  3100. break;
  3101. case EXCP_INTERRUPT:
  3102. /* Just indicate that signals should be handled asap. */
  3103. break;
  3104. default:
  3105. printf ("Unhandled trap: 0x%x\n", trapnr);
  3106. cpu_dump_state(cs, stderr, fprintf, 0);
  3107. exit (1);
  3108. }
  3109. process_pending_signals (env);
  3110. }
  3111. }
  3112. #endif /* TARGET_ALPHA */
  3113. #ifdef TARGET_S390X
  3114. void cpu_loop(CPUS390XState *env)
  3115. {
  3116. CPUState *cs = CPU(s390_env_get_cpu(env));
  3117. int trapnr, n, sig;
  3118. target_siginfo_t info;
  3119. target_ulong addr;
  3120. while (1) {
  3121. trapnr = cpu_s390x_exec(env);
  3122. switch (trapnr) {
  3123. case EXCP_INTERRUPT:
  3124. /* Just indicate that signals should be handled asap. */
  3125. break;
  3126. case EXCP_SVC:
  3127. n = env->int_svc_code;
  3128. if (!n) {
  3129. /* syscalls > 255 */
  3130. n = env->regs[1];
  3131. }
  3132. env->psw.addr += env->int_svc_ilen;
  3133. env->regs[2] = do_syscall(env, n, env->regs[2], env->regs[3],
  3134. env->regs[4], env->regs[5],
  3135. env->regs[6], env->regs[7], 0, 0);
  3136. break;
  3137. case EXCP_DEBUG:
  3138. sig = gdb_handlesig(cs, TARGET_SIGTRAP);
  3139. if (sig) {
  3140. n = TARGET_TRAP_BRKPT;
  3141. goto do_signal_pc;
  3142. }
  3143. break;
  3144. case EXCP_PGM:
  3145. n = env->int_pgm_code;
  3146. switch (n) {
  3147. case PGM_OPERATION:
  3148. case PGM_PRIVILEGED:
  3149. sig = SIGILL;
  3150. n = TARGET_ILL_ILLOPC;
  3151. goto do_signal_pc;
  3152. case PGM_PROTECTION:
  3153. case PGM_ADDRESSING:
  3154. sig = SIGSEGV;
  3155. /* XXX: check env->error_code */
  3156. n = TARGET_SEGV_MAPERR;
  3157. addr = env->__excp_addr;
  3158. goto do_signal;
  3159. case PGM_EXECUTE:
  3160. case PGM_SPECIFICATION:
  3161. case PGM_SPECIAL_OP:
  3162. case PGM_OPERAND:
  3163. do_sigill_opn:
  3164. sig = SIGILL;
  3165. n = TARGET_ILL_ILLOPN;
  3166. goto do_signal_pc;
  3167. case PGM_FIXPT_OVERFLOW:
  3168. sig = SIGFPE;
  3169. n = TARGET_FPE_INTOVF;
  3170. goto do_signal_pc;
  3171. case PGM_FIXPT_DIVIDE:
  3172. sig = SIGFPE;
  3173. n = TARGET_FPE_INTDIV;
  3174. goto do_signal_pc;
  3175. case PGM_DATA:
  3176. n = (env->fpc >> 8) & 0xff;
  3177. if (n == 0xff) {
  3178. /* compare-and-trap */
  3179. goto do_sigill_opn;
  3180. } else {
  3181. /* An IEEE exception, simulated or otherwise. */
  3182. if (n & 0x80) {
  3183. n = TARGET_FPE_FLTINV;
  3184. } else if (n & 0x40) {
  3185. n = TARGET_FPE_FLTDIV;
  3186. } else if (n & 0x20) {
  3187. n = TARGET_FPE_FLTOVF;
  3188. } else if (n & 0x10) {
  3189. n = TARGET_FPE_FLTUND;
  3190. } else if (n & 0x08) {
  3191. n = TARGET_FPE_FLTRES;
  3192. } else {
  3193. /* ??? Quantum exception; BFP, DFP error. */
  3194. goto do_sigill_opn;
  3195. }
  3196. sig = SIGFPE;
  3197. goto do_signal_pc;
  3198. }
  3199. default:
  3200. fprintf(stderr, "Unhandled program exception: %#x\n", n);
  3201. cpu_dump_state(cs, stderr, fprintf, 0);
  3202. exit(1);
  3203. }
  3204. break;
  3205. do_signal_pc:
  3206. addr = env->psw.addr;
  3207. do_signal:
  3208. info.si_signo = sig;
  3209. info.si_errno = 0;
  3210. info.si_code = n;
  3211. info._sifields._sigfault._addr = addr;
  3212. queue_signal(env, info.si_signo, &info);
  3213. break;
  3214. default:
  3215. fprintf(stderr, "Unhandled trap: 0x%x\n", trapnr);
  3216. cpu_dump_state(cs, stderr, fprintf, 0);
  3217. exit(1);
  3218. }
  3219. process_pending_signals (env);
  3220. }
  3221. }
  3222. #endif /* TARGET_S390X */
  3223. THREAD CPUState *thread_cpu;
  3224. void task_settid(TaskState *ts)
  3225. {
  3226. if (ts->ts_tid == 0) {
  3227. ts->ts_tid = (pid_t)syscall(SYS_gettid);
  3228. }
  3229. }
  3230. void stop_all_tasks(void)
  3231. {
  3232. /*
  3233. * We trust that when using NPTL, start_exclusive()
  3234. * handles thread stopping correctly.
  3235. */
  3236. start_exclusive();
  3237. }
  3238. /* Assumes contents are already zeroed. */
  3239. void init_task_state(TaskState *ts)
  3240. {
  3241. int i;
  3242. ts->used = 1;
  3243. ts->first_free = ts->sigqueue_table;
  3244. for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
  3245. ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
  3246. }
  3247. ts->sigqueue_table[i].next = NULL;
  3248. }
  3249. CPUArchState *cpu_copy(CPUArchState *env)
  3250. {
  3251. CPUState *cpu = ENV_GET_CPU(env);
  3252. CPUArchState *new_env = cpu_init(cpu_model);
  3253. CPUState *new_cpu = ENV_GET_CPU(new_env);
  3254. #if defined(TARGET_HAS_ICE)
  3255. CPUBreakpoint *bp;
  3256. CPUWatchpoint *wp;
  3257. #endif
  3258. /* Reset non arch specific state */
  3259. cpu_reset(new_cpu);
  3260. memcpy(new_env, env, sizeof(CPUArchState));
  3261. /* Clone all break/watchpoints.
  3262. Note: Once we support ptrace with hw-debug register access, make sure
  3263. BP_CPU break/watchpoints are handled correctly on clone. */
  3264. QTAILQ_INIT(&cpu->breakpoints);
  3265. QTAILQ_INIT(&cpu->watchpoints);
  3266. #if defined(TARGET_HAS_ICE)
  3267. QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
  3268. cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
  3269. }
  3270. QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
  3271. cpu_watchpoint_insert(new_cpu, wp->vaddr, (~wp->len_mask) + 1,
  3272. wp->flags, NULL);
  3273. }
  3274. #endif
  3275. return new_env;
  3276. }
  3277. static void handle_arg_help(const char *arg)
  3278. {
  3279. usage();
  3280. }
  3281. static void handle_arg_log(const char *arg)
  3282. {
  3283. int mask;
  3284. mask = qemu_str_to_log_mask(arg);
  3285. if (!mask) {
  3286. qemu_print_log_usage(stdout);
  3287. exit(1);
  3288. }
  3289. qemu_set_log(mask);
  3290. }
  3291. static void handle_arg_log_filename(const char *arg)
  3292. {
  3293. qemu_set_log_filename(arg);
  3294. }
  3295. static void handle_arg_set_env(const char *arg)
  3296. {
  3297. char *r, *p, *token;
  3298. r = p = strdup(arg);
  3299. while ((token = strsep(&p, ",")) != NULL) {
  3300. if (envlist_setenv(envlist, token) != 0) {
  3301. usage();
  3302. }
  3303. }
  3304. free(r);
  3305. }
  3306. static void handle_arg_unset_env(const char *arg)
  3307. {
  3308. char *r, *p, *token;
  3309. r = p = strdup(arg);
  3310. while ((token = strsep(&p, ",")) != NULL) {
  3311. if (envlist_unsetenv(envlist, token) != 0) {
  3312. usage();
  3313. }
  3314. }
  3315. free(r);
  3316. }
  3317. static void handle_arg_argv0(const char *arg)
  3318. {
  3319. argv0 = strdup(arg);
  3320. }
  3321. static void handle_arg_stack_size(const char *arg)
  3322. {
  3323. char *p;
  3324. guest_stack_size = strtoul(arg, &p, 0);
  3325. if (guest_stack_size == 0) {
  3326. usage();
  3327. }
  3328. if (*p == 'M') {
  3329. guest_stack_size *= 1024 * 1024;
  3330. } else if (*p == 'k' || *p == 'K') {
  3331. guest_stack_size *= 1024;
  3332. }
  3333. }
  3334. static void handle_arg_ld_prefix(const char *arg)
  3335. {
  3336. interp_prefix = strdup(arg);
  3337. }
  3338. static void handle_arg_pagesize(const char *arg)
  3339. {
  3340. qemu_host_page_size = atoi(arg);
  3341. if (qemu_host_page_size == 0 ||
  3342. (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
  3343. fprintf(stderr, "page size must be a power of two\n");
  3344. exit(1);
  3345. }
  3346. }
  3347. static void handle_arg_gdb(const char *arg)
  3348. {
  3349. gdbstub_port = atoi(arg);
  3350. }
  3351. static void handle_arg_uname(const char *arg)
  3352. {
  3353. qemu_uname_release = strdup(arg);
  3354. }
  3355. static void handle_arg_cpu(const char *arg)
  3356. {
  3357. cpu_model = strdup(arg);
  3358. if (cpu_model == NULL || is_help_option(cpu_model)) {
  3359. /* XXX: implement xxx_cpu_list for targets that still miss it */
  3360. #if defined(cpu_list)
  3361. cpu_list(stdout, &fprintf);
  3362. #endif
  3363. exit(1);
  3364. }
  3365. }
  3366. #if defined(CONFIG_USE_GUEST_BASE)
  3367. static void handle_arg_guest_base(const char *arg)
  3368. {
  3369. guest_base = strtol(arg, NULL, 0);
  3370. have_guest_base = 1;
  3371. }
  3372. static void handle_arg_reserved_va(const char *arg)
  3373. {
  3374. char *p;
  3375. int shift = 0;
  3376. reserved_va = strtoul(arg, &p, 0);
  3377. switch (*p) {
  3378. case 'k':
  3379. case 'K':
  3380. shift = 10;
  3381. break;
  3382. case 'M':
  3383. shift = 20;
  3384. break;
  3385. case 'G':
  3386. shift = 30;
  3387. break;
  3388. }
  3389. if (shift) {
  3390. unsigned long unshifted = reserved_va;
  3391. p++;
  3392. reserved_va <<= shift;
  3393. if (((reserved_va >> shift) != unshifted)
  3394. #if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
  3395. || (reserved_va > (1ul << TARGET_VIRT_ADDR_SPACE_BITS))
  3396. #endif
  3397. ) {
  3398. fprintf(stderr, "Reserved virtual address too big\n");
  3399. exit(1);
  3400. }
  3401. }
  3402. if (*p) {
  3403. fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
  3404. exit(1);
  3405. }
  3406. }
  3407. #endif
  3408. static void handle_arg_singlestep(const char *arg)
  3409. {
  3410. singlestep = 1;
  3411. }
  3412. static void handle_arg_strace(const char *arg)
  3413. {
  3414. do_strace = 1;
  3415. }
  3416. static void handle_arg_version(const char *arg)
  3417. {
  3418. printf("qemu-" TARGET_NAME " version " QEMU_VERSION QEMU_PKGVERSION
  3419. ", Copyright (c) 2003-2008 Fabrice Bellard\n");
  3420. exit(0);
  3421. }
  3422. struct qemu_argument {
  3423. const char *argv;
  3424. const char *env;
  3425. bool has_arg;
  3426. void (*handle_opt)(const char *arg);
  3427. const char *example;
  3428. const char *help;
  3429. };
  3430. static const struct qemu_argument arg_table[] = {
  3431. {"h", "", false, handle_arg_help,
  3432. "", "print this help"},
  3433. {"g", "QEMU_GDB", true, handle_arg_gdb,
  3434. "port", "wait gdb connection to 'port'"},
  3435. {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix,
  3436. "path", "set the elf interpreter prefix to 'path'"},
  3437. {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size,
  3438. "size", "set the stack size to 'size' bytes"},
  3439. {"cpu", "QEMU_CPU", true, handle_arg_cpu,
  3440. "model", "select CPU (-cpu help for list)"},
  3441. {"E", "QEMU_SET_ENV", true, handle_arg_set_env,
  3442. "var=value", "sets targets environment variable (see below)"},
  3443. {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env,
  3444. "var", "unsets targets environment variable (see below)"},
  3445. {"0", "QEMU_ARGV0", true, handle_arg_argv0,
  3446. "argv0", "forces target process argv[0] to be 'argv0'"},
  3447. {"r", "QEMU_UNAME", true, handle_arg_uname,
  3448. "uname", "set qemu uname release string to 'uname'"},
  3449. #if defined(CONFIG_USE_GUEST_BASE)
  3450. {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base,
  3451. "address", "set guest_base address to 'address'"},
  3452. {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
  3453. "size", "reserve 'size' bytes for guest virtual address space"},
  3454. #endif
  3455. {"d", "QEMU_LOG", true, handle_arg_log,
  3456. "item[,...]", "enable logging of specified items "
  3457. "(use '-d help' for a list of items)"},
  3458. {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
  3459. "logfile", "write logs to 'logfile' (default stderr)"},
  3460. {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize,
  3461. "pagesize", "set the host page size to 'pagesize'"},
  3462. {"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep,
  3463. "", "run in singlestep mode"},
  3464. {"strace", "QEMU_STRACE", false, handle_arg_strace,
  3465. "", "log system calls"},
  3466. {"version", "QEMU_VERSION", false, handle_arg_version,
  3467. "", "display version information and exit"},
  3468. {NULL, NULL, false, NULL, NULL, NULL}
  3469. };
  3470. static void usage(void)
  3471. {
  3472. const struct qemu_argument *arginfo;
  3473. int maxarglen;
  3474. int maxenvlen;
  3475. printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
  3476. "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
  3477. "\n"
  3478. "Options and associated environment variables:\n"
  3479. "\n");
  3480. /* Calculate column widths. We must always have at least enough space
  3481. * for the column header.
  3482. */
  3483. maxarglen = strlen("Argument");
  3484. maxenvlen = strlen("Env-variable");
  3485. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  3486. int arglen = strlen(arginfo->argv);
  3487. if (arginfo->has_arg) {
  3488. arglen += strlen(arginfo->example) + 1;
  3489. }
  3490. if (strlen(arginfo->env) > maxenvlen) {
  3491. maxenvlen = strlen(arginfo->env);
  3492. }
  3493. if (arglen > maxarglen) {
  3494. maxarglen = arglen;
  3495. }
  3496. }
  3497. printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
  3498. maxenvlen, "Env-variable");
  3499. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  3500. if (arginfo->has_arg) {
  3501. printf("-%s %-*s %-*s %s\n", arginfo->argv,
  3502. (int)(maxarglen - strlen(arginfo->argv) - 1),
  3503. arginfo->example, maxenvlen, arginfo->env, arginfo->help);
  3504. } else {
  3505. printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
  3506. maxenvlen, arginfo->env,
  3507. arginfo->help);
  3508. }
  3509. }
  3510. printf("\n"
  3511. "Defaults:\n"
  3512. "QEMU_LD_PREFIX = %s\n"
  3513. "QEMU_STACK_SIZE = %ld byte\n",
  3514. interp_prefix,
  3515. guest_stack_size);
  3516. printf("\n"
  3517. "You can use -E and -U options or the QEMU_SET_ENV and\n"
  3518. "QEMU_UNSET_ENV environment variables to set and unset\n"
  3519. "environment variables for the target process.\n"
  3520. "It is possible to provide several variables by separating them\n"
  3521. "by commas in getsubopt(3) style. Additionally it is possible to\n"
  3522. "provide the -E and -U options multiple times.\n"
  3523. "The following lines are equivalent:\n"
  3524. " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
  3525. " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
  3526. " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
  3527. "Note that if you provide several changes to a single variable\n"
  3528. "the last change will stay in effect.\n");
  3529. exit(1);
  3530. }
  3531. static int parse_args(int argc, char **argv)
  3532. {
  3533. const char *r;
  3534. int optind;
  3535. const struct qemu_argument *arginfo;
  3536. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  3537. if (arginfo->env == NULL) {
  3538. continue;
  3539. }
  3540. r = getenv(arginfo->env);
  3541. if (r != NULL) {
  3542. arginfo->handle_opt(r);
  3543. }
  3544. }
  3545. optind = 1;
  3546. for (;;) {
  3547. if (optind >= argc) {
  3548. break;
  3549. }
  3550. r = argv[optind];
  3551. if (r[0] != '-') {
  3552. break;
  3553. }
  3554. optind++;
  3555. r++;
  3556. if (!strcmp(r, "-")) {
  3557. break;
  3558. }
  3559. for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
  3560. if (!strcmp(r, arginfo->argv)) {
  3561. if (arginfo->has_arg) {
  3562. if (optind >= argc) {
  3563. usage();
  3564. }
  3565. arginfo->handle_opt(argv[optind]);
  3566. optind++;
  3567. } else {
  3568. arginfo->handle_opt(NULL);
  3569. }
  3570. break;
  3571. }
  3572. }
  3573. /* no option matched the current argv */
  3574. if (arginfo->handle_opt == NULL) {
  3575. usage();
  3576. }
  3577. }
  3578. if (optind >= argc) {
  3579. usage();
  3580. }
  3581. filename = argv[optind];
  3582. exec_path = argv[optind];
  3583. return optind;
  3584. }
  3585. int main(int argc, char **argv, char **envp)
  3586. {
  3587. struct target_pt_regs regs1, *regs = &regs1;
  3588. struct image_info info1, *info = &info1;
  3589. struct linux_binprm bprm;
  3590. TaskState *ts;
  3591. CPUArchState *env;
  3592. CPUState *cpu;
  3593. int optind;
  3594. char **target_environ, **wrk;
  3595. char **target_argv;
  3596. int target_argc;
  3597. int i;
  3598. int ret;
  3599. int execfd;
  3600. module_call_init(MODULE_INIT_QOM);
  3601. if ((envlist = envlist_create()) == NULL) {
  3602. (void) fprintf(stderr, "Unable to allocate envlist\n");
  3603. exit(1);
  3604. }
  3605. /* add current environment into the list */
  3606. for (wrk = environ; *wrk != NULL; wrk++) {
  3607. (void) envlist_setenv(envlist, *wrk);
  3608. }
  3609. /* Read the stack limit from the kernel. If it's "unlimited",
  3610. then we can do little else besides use the default. */
  3611. {
  3612. struct rlimit lim;
  3613. if (getrlimit(RLIMIT_STACK, &lim) == 0
  3614. && lim.rlim_cur != RLIM_INFINITY
  3615. && lim.rlim_cur == (target_long)lim.rlim_cur) {
  3616. guest_stack_size = lim.rlim_cur;
  3617. }
  3618. }
  3619. cpu_model = NULL;
  3620. #if defined(cpudef_setup)
  3621. cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
  3622. #endif
  3623. optind = parse_args(argc, argv);
  3624. /* Zero out regs */
  3625. memset(regs, 0, sizeof(struct target_pt_regs));
  3626. /* Zero out image_info */
  3627. memset(info, 0, sizeof(struct image_info));
  3628. memset(&bprm, 0, sizeof (bprm));
  3629. /* Scan interp_prefix dir for replacement files. */
  3630. init_paths(interp_prefix);
  3631. init_qemu_uname_release();
  3632. if (cpu_model == NULL) {
  3633. #if defined(TARGET_I386)
  3634. #ifdef TARGET_X86_64
  3635. cpu_model = "qemu64";
  3636. #else
  3637. cpu_model = "qemu32";
  3638. #endif
  3639. #elif defined(TARGET_ARM)
  3640. cpu_model = "any";
  3641. #elif defined(TARGET_UNICORE32)
  3642. cpu_model = "any";
  3643. #elif defined(TARGET_M68K)
  3644. cpu_model = "any";
  3645. #elif defined(TARGET_SPARC)
  3646. #ifdef TARGET_SPARC64
  3647. cpu_model = "TI UltraSparc II";
  3648. #else
  3649. cpu_model = "Fujitsu MB86904";
  3650. #endif
  3651. #elif defined(TARGET_MIPS)
  3652. #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
  3653. cpu_model = "20Kc";
  3654. #else
  3655. cpu_model = "24Kf";
  3656. #endif
  3657. #elif defined TARGET_OPENRISC
  3658. cpu_model = "or1200";
  3659. #elif defined(TARGET_PPC)
  3660. # ifdef TARGET_PPC64
  3661. cpu_model = "POWER7";
  3662. # else
  3663. cpu_model = "750";
  3664. # endif
  3665. #else
  3666. cpu_model = "any";
  3667. #endif
  3668. }
  3669. tcg_exec_init(0);
  3670. cpu_exec_init_all();
  3671. /* NOTE: we need to init the CPU at this stage to get
  3672. qemu_host_page_size */
  3673. env = cpu_init(cpu_model);
  3674. if (!env) {
  3675. fprintf(stderr, "Unable to find CPU definition\n");
  3676. exit(1);
  3677. }
  3678. cpu = ENV_GET_CPU(env);
  3679. cpu_reset(cpu);
  3680. thread_cpu = cpu;
  3681. if (getenv("QEMU_STRACE")) {
  3682. do_strace = 1;
  3683. }
  3684. target_environ = envlist_to_environ(envlist, NULL);
  3685. envlist_free(envlist);
  3686. #if defined(CONFIG_USE_GUEST_BASE)
  3687. /*
  3688. * Now that page sizes are configured in cpu_init() we can do
  3689. * proper page alignment for guest_base.
  3690. */
  3691. guest_base = HOST_PAGE_ALIGN(guest_base);
  3692. if (reserved_va || have_guest_base) {
  3693. guest_base = init_guest_space(guest_base, reserved_va, 0,
  3694. have_guest_base);
  3695. if (guest_base == (unsigned long)-1) {
  3696. fprintf(stderr, "Unable to reserve 0x%lx bytes of virtual address "
  3697. "space for use as guest address space (check your virtual "
  3698. "memory ulimit setting or reserve less using -R option)\n",
  3699. reserved_va);
  3700. exit(1);
  3701. }
  3702. if (reserved_va) {
  3703. mmap_next_start = reserved_va;
  3704. }
  3705. }
  3706. #endif /* CONFIG_USE_GUEST_BASE */
  3707. /*
  3708. * Read in mmap_min_addr kernel parameter. This value is used
  3709. * When loading the ELF image to determine whether guest_base
  3710. * is needed. It is also used in mmap_find_vma.
  3711. */
  3712. {
  3713. FILE *fp;
  3714. if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
  3715. unsigned long tmp;
  3716. if (fscanf(fp, "%lu", &tmp) == 1) {
  3717. mmap_min_addr = tmp;
  3718. qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr);
  3719. }
  3720. fclose(fp);
  3721. }
  3722. }
  3723. /*
  3724. * Prepare copy of argv vector for target.
  3725. */
  3726. target_argc = argc - optind;
  3727. target_argv = calloc(target_argc + 1, sizeof (char *));
  3728. if (target_argv == NULL) {
  3729. (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
  3730. exit(1);
  3731. }
  3732. /*
  3733. * If argv0 is specified (using '-0' switch) we replace
  3734. * argv[0] pointer with the given one.
  3735. */
  3736. i = 0;
  3737. if (argv0 != NULL) {
  3738. target_argv[i++] = strdup(argv0);
  3739. }
  3740. for (; i < target_argc; i++) {
  3741. target_argv[i] = strdup(argv[optind + i]);
  3742. }
  3743. target_argv[target_argc] = NULL;
  3744. ts = g_malloc0 (sizeof(TaskState));
  3745. init_task_state(ts);
  3746. /* build Task State */
  3747. ts->info = info;
  3748. ts->bprm = &bprm;
  3749. cpu->opaque = ts;
  3750. task_settid(ts);
  3751. execfd = qemu_getauxval(AT_EXECFD);
  3752. if (execfd == 0) {
  3753. execfd = open(filename, O_RDONLY);
  3754. if (execfd < 0) {
  3755. printf("Error while loading %s: %s\n", filename, strerror(errno));
  3756. _exit(1);
  3757. }
  3758. }
  3759. ret = loader_exec(execfd, filename, target_argv, target_environ, regs,
  3760. info, &bprm);
  3761. if (ret != 0) {
  3762. printf("Error while loading %s: %s\n", filename, strerror(-ret));
  3763. _exit(1);
  3764. }
  3765. for (wrk = target_environ; *wrk; wrk++) {
  3766. free(*wrk);
  3767. }
  3768. free(target_environ);
  3769. if (qemu_log_enabled()) {
  3770. #if defined(CONFIG_USE_GUEST_BASE)
  3771. qemu_log("guest_base 0x%lx\n", guest_base);
  3772. #endif
  3773. log_page_dump();
  3774. qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
  3775. qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
  3776. qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
  3777. info->start_code);
  3778. qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
  3779. info->start_data);
  3780. qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
  3781. qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
  3782. info->start_stack);
  3783. qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
  3784. qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
  3785. }
  3786. target_set_brk(info->brk);
  3787. syscall_init();
  3788. signal_init();
  3789. #if defined(CONFIG_USE_GUEST_BASE)
  3790. /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
  3791. generating the prologue until now so that the prologue can take
  3792. the real value of GUEST_BASE into account. */
  3793. tcg_prologue_init(&tcg_ctx);
  3794. #endif
  3795. #if defined(TARGET_I386)
  3796. env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
  3797. env->hflags |= HF_PE_MASK | HF_CPL_MASK;
  3798. if (env->features[FEAT_1_EDX] & CPUID_SSE) {
  3799. env->cr[4] |= CR4_OSFXSR_MASK;
  3800. env->hflags |= HF_OSFXSR_MASK;
  3801. }
  3802. #ifndef TARGET_ABI32
  3803. /* enable 64 bit mode if possible */
  3804. if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
  3805. fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
  3806. exit(1);
  3807. }
  3808. env->cr[4] |= CR4_PAE_MASK;
  3809. env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
  3810. env->hflags |= HF_LMA_MASK;
  3811. #endif
  3812. /* flags setup : we activate the IRQs by default as in user mode */
  3813. env->eflags |= IF_MASK;
  3814. /* linux register setup */
  3815. #ifndef TARGET_ABI32
  3816. env->regs[R_EAX] = regs->rax;
  3817. env->regs[R_EBX] = regs->rbx;
  3818. env->regs[R_ECX] = regs->rcx;
  3819. env->regs[R_EDX] = regs->rdx;
  3820. env->regs[R_ESI] = regs->rsi;
  3821. env->regs[R_EDI] = regs->rdi;
  3822. env->regs[R_EBP] = regs->rbp;
  3823. env->regs[R_ESP] = regs->rsp;
  3824. env->eip = regs->rip;
  3825. #else
  3826. env->regs[R_EAX] = regs->eax;
  3827. env->regs[R_EBX] = regs->ebx;
  3828. env->regs[R_ECX] = regs->ecx;
  3829. env->regs[R_EDX] = regs->edx;
  3830. env->regs[R_ESI] = regs->esi;
  3831. env->regs[R_EDI] = regs->edi;
  3832. env->regs[R_EBP] = regs->ebp;
  3833. env->regs[R_ESP] = regs->esp;
  3834. env->eip = regs->eip;
  3835. #endif
  3836. /* linux interrupt setup */
  3837. #ifndef TARGET_ABI32
  3838. env->idt.limit = 511;
  3839. #else
  3840. env->idt.limit = 255;
  3841. #endif
  3842. env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
  3843. PROT_READ|PROT_WRITE,
  3844. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  3845. idt_table = g2h(env->idt.base);
  3846. set_idt(0, 0);
  3847. set_idt(1, 0);
  3848. set_idt(2, 0);
  3849. set_idt(3, 3);
  3850. set_idt(4, 3);
  3851. set_idt(5, 0);
  3852. set_idt(6, 0);
  3853. set_idt(7, 0);
  3854. set_idt(8, 0);
  3855. set_idt(9, 0);
  3856. set_idt(10, 0);
  3857. set_idt(11, 0);
  3858. set_idt(12, 0);
  3859. set_idt(13, 0);
  3860. set_idt(14, 0);
  3861. set_idt(15, 0);
  3862. set_idt(16, 0);
  3863. set_idt(17, 0);
  3864. set_idt(18, 0);
  3865. set_idt(19, 0);
  3866. set_idt(0x80, 3);
  3867. /* linux segment setup */
  3868. {
  3869. uint64_t *gdt_table;
  3870. env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
  3871. PROT_READ|PROT_WRITE,
  3872. MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  3873. env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
  3874. gdt_table = g2h(env->gdt.base);
  3875. #ifdef TARGET_ABI32
  3876. write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
  3877. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  3878. (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
  3879. #else
  3880. /* 64 bit code segment */
  3881. write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
  3882. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  3883. DESC_L_MASK |
  3884. (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
  3885. #endif
  3886. write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
  3887. DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
  3888. (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
  3889. }
  3890. cpu_x86_load_seg(env, R_CS, __USER_CS);
  3891. cpu_x86_load_seg(env, R_SS, __USER_DS);
  3892. #ifdef TARGET_ABI32
  3893. cpu_x86_load_seg(env, R_DS, __USER_DS);
  3894. cpu_x86_load_seg(env, R_ES, __USER_DS);
  3895. cpu_x86_load_seg(env, R_FS, __USER_DS);
  3896. cpu_x86_load_seg(env, R_GS, __USER_DS);
  3897. /* This hack makes Wine work... */
  3898. env->segs[R_FS].selector = 0;
  3899. #else
  3900. cpu_x86_load_seg(env, R_DS, 0);
  3901. cpu_x86_load_seg(env, R_ES, 0);
  3902. cpu_x86_load_seg(env, R_FS, 0);
  3903. cpu_x86_load_seg(env, R_GS, 0);
  3904. #endif
  3905. #elif defined(TARGET_AARCH64)
  3906. {
  3907. int i;
  3908. if (!(arm_feature(env, ARM_FEATURE_AARCH64))) {
  3909. fprintf(stderr,
  3910. "The selected ARM CPU does not support 64 bit mode\n");
  3911. exit(1);
  3912. }
  3913. for (i = 0; i < 31; i++) {
  3914. env->xregs[i] = regs->regs[i];
  3915. }
  3916. env->pc = regs->pc;
  3917. env->xregs[31] = regs->sp;
  3918. }
  3919. #elif defined(TARGET_ARM)
  3920. {
  3921. int i;
  3922. cpsr_write(env, regs->uregs[16], 0xffffffff);
  3923. for(i = 0; i < 16; i++) {
  3924. env->regs[i] = regs->uregs[i];
  3925. }
  3926. /* Enable BE8. */
  3927. if (EF_ARM_EABI_VERSION(info->elf_flags) >= EF_ARM_EABI_VER4
  3928. && (info->elf_flags & EF_ARM_BE8)) {
  3929. env->bswap_code = 1;
  3930. }
  3931. }
  3932. #elif defined(TARGET_UNICORE32)
  3933. {
  3934. int i;
  3935. cpu_asr_write(env, regs->uregs[32], 0xffffffff);
  3936. for (i = 0; i < 32; i++) {
  3937. env->regs[i] = regs->uregs[i];
  3938. }
  3939. }
  3940. #elif defined(TARGET_SPARC)
  3941. {
  3942. int i;
  3943. env->pc = regs->pc;
  3944. env->npc = regs->npc;
  3945. env->y = regs->y;
  3946. for(i = 0; i < 8; i++)
  3947. env->gregs[i] = regs->u_regs[i];
  3948. for(i = 0; i < 8; i++)
  3949. env->regwptr[i] = regs->u_regs[i + 8];
  3950. }
  3951. #elif defined(TARGET_PPC)
  3952. {
  3953. int i;
  3954. #if defined(TARGET_PPC64)
  3955. #if defined(TARGET_ABI32)
  3956. env->msr &= ~((target_ulong)1 << MSR_SF);
  3957. #else
  3958. env->msr |= (target_ulong)1 << MSR_SF;
  3959. #endif
  3960. #endif
  3961. env->nip = regs->nip;
  3962. for(i = 0; i < 32; i++) {
  3963. env->gpr[i] = regs->gpr[i];
  3964. }
  3965. }
  3966. #elif defined(TARGET_M68K)
  3967. {
  3968. env->pc = regs->pc;
  3969. env->dregs[0] = regs->d0;
  3970. env->dregs[1] = regs->d1;
  3971. env->dregs[2] = regs->d2;
  3972. env->dregs[3] = regs->d3;
  3973. env->dregs[4] = regs->d4;
  3974. env->dregs[5] = regs->d5;
  3975. env->dregs[6] = regs->d6;
  3976. env->dregs[7] = regs->d7;
  3977. env->aregs[0] = regs->a0;
  3978. env->aregs[1] = regs->a1;
  3979. env->aregs[2] = regs->a2;
  3980. env->aregs[3] = regs->a3;
  3981. env->aregs[4] = regs->a4;
  3982. env->aregs[5] = regs->a5;
  3983. env->aregs[6] = regs->a6;
  3984. env->aregs[7] = regs->usp;
  3985. env->sr = regs->sr;
  3986. ts->sim_syscalls = 1;
  3987. }
  3988. #elif defined(TARGET_MICROBLAZE)
  3989. {
  3990. env->regs[0] = regs->r0;
  3991. env->regs[1] = regs->r1;
  3992. env->regs[2] = regs->r2;
  3993. env->regs[3] = regs->r3;
  3994. env->regs[4] = regs->r4;
  3995. env->regs[5] = regs->r5;
  3996. env->regs[6] = regs->r6;
  3997. env->regs[7] = regs->r7;
  3998. env->regs[8] = regs->r8;
  3999. env->regs[9] = regs->r9;
  4000. env->regs[10] = regs->r10;
  4001. env->regs[11] = regs->r11;
  4002. env->regs[12] = regs->r12;
  4003. env->regs[13] = regs->r13;
  4004. env->regs[14] = regs->r14;
  4005. env->regs[15] = regs->r15;
  4006. env->regs[16] = regs->r16;
  4007. env->regs[17] = regs->r17;
  4008. env->regs[18] = regs->r18;
  4009. env->regs[19] = regs->r19;
  4010. env->regs[20] = regs->r20;
  4011. env->regs[21] = regs->r21;
  4012. env->regs[22] = regs->r22;
  4013. env->regs[23] = regs->r23;
  4014. env->regs[24] = regs->r24;
  4015. env->regs[25] = regs->r25;
  4016. env->regs[26] = regs->r26;
  4017. env->regs[27] = regs->r27;
  4018. env->regs[28] = regs->r28;
  4019. env->regs[29] = regs->r29;
  4020. env->regs[30] = regs->r30;
  4021. env->regs[31] = regs->r31;
  4022. env->sregs[SR_PC] = regs->pc;
  4023. }
  4024. #elif defined(TARGET_MIPS)
  4025. {
  4026. int i;
  4027. for(i = 0; i < 32; i++) {
  4028. env->active_tc.gpr[i] = regs->regs[i];
  4029. }
  4030. env->active_tc.PC = regs->cp0_epc & ~(target_ulong)1;
  4031. if (regs->cp0_epc & 1) {
  4032. env->hflags |= MIPS_HFLAG_M16;
  4033. }
  4034. }
  4035. #elif defined(TARGET_OPENRISC)
  4036. {
  4037. int i;
  4038. for (i = 0; i < 32; i++) {
  4039. env->gpr[i] = regs->gpr[i];
  4040. }
  4041. env->sr = regs->sr;
  4042. env->pc = regs->pc;
  4043. }
  4044. #elif defined(TARGET_SH4)
  4045. {
  4046. int i;
  4047. for(i = 0; i < 16; i++) {
  4048. env->gregs[i] = regs->regs[i];
  4049. }
  4050. env->pc = regs->pc;
  4051. }
  4052. #elif defined(TARGET_ALPHA)
  4053. {
  4054. int i;
  4055. for(i = 0; i < 28; i++) {
  4056. env->ir[i] = ((abi_ulong *)regs)[i];
  4057. }
  4058. env->ir[IR_SP] = regs->usp;
  4059. env->pc = regs->pc;
  4060. }
  4061. #elif defined(TARGET_CRIS)
  4062. {
  4063. env->regs[0] = regs->r0;
  4064. env->regs[1] = regs->r1;
  4065. env->regs[2] = regs->r2;
  4066. env->regs[3] = regs->r3;
  4067. env->regs[4] = regs->r4;
  4068. env->regs[5] = regs->r5;
  4069. env->regs[6] = regs->r6;
  4070. env->regs[7] = regs->r7;
  4071. env->regs[8] = regs->r8;
  4072. env->regs[9] = regs->r9;
  4073. env->regs[10] = regs->r10;
  4074. env->regs[11] = regs->r11;
  4075. env->regs[12] = regs->r12;
  4076. env->regs[13] = regs->r13;
  4077. env->regs[14] = info->start_stack;
  4078. env->regs[15] = regs->acr;
  4079. env->pc = regs->erp;
  4080. }
  4081. #elif defined(TARGET_S390X)
  4082. {
  4083. int i;
  4084. for (i = 0; i < 16; i++) {
  4085. env->regs[i] = regs->gprs[i];
  4086. }
  4087. env->psw.mask = regs->psw.mask;
  4088. env->psw.addr = regs->psw.addr;
  4089. }
  4090. #else
  4091. #error unsupported target CPU
  4092. #endif
  4093. #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
  4094. ts->stack_base = info->start_stack;
  4095. ts->heap_base = info->brk;
  4096. /* This will be filled in on the first SYS_HEAPINFO call. */
  4097. ts->heap_limit = 0;
  4098. #endif
  4099. if (gdbstub_port) {
  4100. if (gdbserver_start(gdbstub_port) < 0) {
  4101. fprintf(stderr, "qemu: could not open gdbserver on port %d\n",
  4102. gdbstub_port);
  4103. exit(1);
  4104. }
  4105. gdb_handlesig(cpu, 0);
  4106. }
  4107. cpu_loop(env);
  4108. /* never exits */
  4109. return 0;
  4110. }