vl.c 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw/hw.h"
  25. #include "hw/boards.h"
  26. #include "hw/usb.h"
  27. #include "hw/pcmcia.h"
  28. #include "hw/pc.h"
  29. #include "hw/audiodev.h"
  30. #include "hw/isa.h"
  31. #include "hw/baum.h"
  32. #include "hw/bt.h"
  33. #include "net.h"
  34. #include "console.h"
  35. #include "sysemu.h"
  36. #include "gdbstub.h"
  37. #include "qemu-timer.h"
  38. #include "qemu-char.h"
  39. #include "cache-utils.h"
  40. #include "block.h"
  41. #include "audio/audio.h"
  42. #include "migration.h"
  43. #include "kvm.h"
  44. #include "balloon.h"
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #include <signal.h>
  48. #include <time.h>
  49. #include <errno.h>
  50. #include <sys/time.h>
  51. #include <zlib.h>
  52. #ifndef _WIN32
  53. #include <pwd.h>
  54. #include <sys/times.h>
  55. #include <sys/wait.h>
  56. #include <termios.h>
  57. #include <sys/mman.h>
  58. #include <sys/ioctl.h>
  59. #include <sys/resource.h>
  60. #include <sys/socket.h>
  61. #include <netinet/in.h>
  62. #include <net/if.h>
  63. #if defined(__NetBSD__)
  64. #include <net/if_tap.h>
  65. #endif
  66. #ifdef __linux__
  67. #include <linux/if_tun.h>
  68. #endif
  69. #include <arpa/inet.h>
  70. #include <dirent.h>
  71. #include <netdb.h>
  72. #include <sys/select.h>
  73. #ifdef _BSD
  74. #include <sys/stat.h>
  75. #ifdef __FreeBSD__
  76. #include <libutil.h>
  77. #else
  78. #include <util.h>
  79. #endif
  80. #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
  81. #include <freebsd/stdlib.h>
  82. #else
  83. #ifdef __linux__
  84. #include <pty.h>
  85. #include <malloc.h>
  86. #include <linux/rtc.h>
  87. /* For the benefit of older linux systems which don't supply it,
  88. we use a local copy of hpet.h. */
  89. /* #include <linux/hpet.h> */
  90. #include "hpet.h"
  91. #include <linux/ppdev.h>
  92. #include <linux/parport.h>
  93. #endif
  94. #ifdef __sun__
  95. #include <sys/stat.h>
  96. #include <sys/ethernet.h>
  97. #include <sys/sockio.h>
  98. #include <netinet/arp.h>
  99. #include <netinet/in.h>
  100. #include <netinet/in_systm.h>
  101. #include <netinet/ip.h>
  102. #include <netinet/ip_icmp.h> // must come after ip.h
  103. #include <netinet/udp.h>
  104. #include <netinet/tcp.h>
  105. #include <net/if.h>
  106. #include <syslog.h>
  107. #include <stropts.h>
  108. #endif
  109. #endif
  110. #endif
  111. #include "qemu_socket.h"
  112. #if defined(CONFIG_SLIRP)
  113. #include "libslirp.h"
  114. #endif
  115. #if defined(__OpenBSD__)
  116. #include <util.h>
  117. #endif
  118. #if defined(CONFIG_VDE)
  119. #include <libvdeplug.h>
  120. #endif
  121. #ifdef _WIN32
  122. #include <malloc.h>
  123. #include <sys/timeb.h>
  124. #include <mmsystem.h>
  125. #define getopt_long_only getopt_long
  126. #define memalign(align, size) malloc(size)
  127. #endif
  128. #ifdef CONFIG_SDL
  129. #ifdef __APPLE__
  130. #include <SDL/SDL.h>
  131. int qemu_main(int argc, char **argv, char **envp);
  132. int main(int argc, char **argv)
  133. {
  134. qemu_main(argc, argv, NULL);
  135. }
  136. #undef main
  137. #define main qemu_main
  138. #endif
  139. #endif /* CONFIG_SDL */
  140. #ifdef CONFIG_COCOA
  141. #undef main
  142. #define main qemu_main
  143. #endif /* CONFIG_COCOA */
  144. #include "disas.h"
  145. #include "exec-all.h"
  146. //#define DEBUG_UNUSED_IOPORT
  147. //#define DEBUG_IOPORT
  148. //#define DEBUG_NET
  149. //#define DEBUG_SLIRP
  150. #ifdef DEBUG_IOPORT
  151. # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
  152. #else
  153. # define LOG_IOPORT(...) do { } while (0)
  154. #endif
  155. #define DEFAULT_RAM_SIZE 128
  156. /* Max number of USB devices that can be specified on the commandline. */
  157. #define MAX_USB_CMDLINE 8
  158. /* Max number of bluetooth switches on the commandline. */
  159. #define MAX_BT_CMDLINE 10
  160. /* XXX: use a two level table to limit memory usage */
  161. #define MAX_IOPORTS 65536
  162. const char *bios_dir = CONFIG_QEMU_SHAREDIR;
  163. const char *bios_name = NULL;
  164. static void *ioport_opaque[MAX_IOPORTS];
  165. static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
  166. static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
  167. /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
  168. to store the VM snapshots */
  169. DriveInfo drives_table[MAX_DRIVES+1];
  170. int nb_drives;
  171. static int vga_ram_size;
  172. enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
  173. static DisplayState *display_state;
  174. int nographic;
  175. static int curses;
  176. static int sdl;
  177. const char* keyboard_layout = NULL;
  178. int64_t ticks_per_sec;
  179. ram_addr_t ram_size;
  180. int nb_nics;
  181. NICInfo nd_table[MAX_NICS];
  182. int vm_running;
  183. int autostart;
  184. static int rtc_utc = 1;
  185. static int rtc_date_offset = -1; /* -1 means no change */
  186. int cirrus_vga_enabled = 1;
  187. int std_vga_enabled = 0;
  188. int vmsvga_enabled = 0;
  189. #ifdef TARGET_SPARC
  190. int graphic_width = 1024;
  191. int graphic_height = 768;
  192. int graphic_depth = 8;
  193. #else
  194. int graphic_width = 800;
  195. int graphic_height = 600;
  196. int graphic_depth = 15;
  197. #endif
  198. static int full_screen = 0;
  199. #ifdef CONFIG_SDL
  200. static int no_frame = 0;
  201. #endif
  202. int no_quit = 0;
  203. CharDriverState *serial_hds[MAX_SERIAL_PORTS];
  204. CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
  205. CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
  206. #ifdef TARGET_I386
  207. int win2k_install_hack = 0;
  208. int rtc_td_hack = 0;
  209. #endif
  210. int usb_enabled = 0;
  211. int smp_cpus = 1;
  212. const char *vnc_display;
  213. int acpi_enabled = 1;
  214. int no_hpet = 0;
  215. int fd_bootchk = 1;
  216. int no_reboot = 0;
  217. int no_shutdown = 0;
  218. int cursor_hide = 1;
  219. int graphic_rotate = 0;
  220. int daemonize = 0;
  221. const char *option_rom[MAX_OPTION_ROMS];
  222. int nb_option_roms;
  223. int semihosting_enabled = 0;
  224. #ifdef TARGET_ARM
  225. int old_param = 0;
  226. #endif
  227. const char *qemu_name;
  228. int alt_grab = 0;
  229. #if defined(TARGET_SPARC) || defined(TARGET_PPC)
  230. unsigned int nb_prom_envs = 0;
  231. const char *prom_envs[MAX_PROM_ENVS];
  232. #endif
  233. int nb_drives_opt;
  234. struct drive_opt drives_opt[MAX_DRIVES];
  235. static CPUState *cur_cpu;
  236. static CPUState *next_cpu;
  237. static int event_pending = 1;
  238. /* Conversion factor from emulated instructions to virtual clock ticks. */
  239. static int icount_time_shift;
  240. /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
  241. #define MAX_ICOUNT_SHIFT 10
  242. /* Compensate for varying guest execution speed. */
  243. static int64_t qemu_icount_bias;
  244. static QEMUTimer *icount_rt_timer;
  245. static QEMUTimer *icount_vm_timer;
  246. static QEMUTimer *nographic_timer;
  247. uint8_t qemu_uuid[16];
  248. /***********************************************************/
  249. /* x86 ISA bus support */
  250. target_phys_addr_t isa_mem_base = 0;
  251. PicState2 *isa_pic;
  252. static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
  253. static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
  254. static uint32_t ioport_read(int index, uint32_t address)
  255. {
  256. static IOPortReadFunc *default_func[3] = {
  257. default_ioport_readb,
  258. default_ioport_readw,
  259. default_ioport_readl
  260. };
  261. IOPortReadFunc *func = ioport_read_table[index][address];
  262. if (!func)
  263. func = default_func[index];
  264. return func(ioport_opaque[address], address);
  265. }
  266. static void ioport_write(int index, uint32_t address, uint32_t data)
  267. {
  268. static IOPortWriteFunc *default_func[3] = {
  269. default_ioport_writeb,
  270. default_ioport_writew,
  271. default_ioport_writel
  272. };
  273. IOPortWriteFunc *func = ioport_write_table[index][address];
  274. if (!func)
  275. func = default_func[index];
  276. func(ioport_opaque[address], address, data);
  277. }
  278. static uint32_t default_ioport_readb(void *opaque, uint32_t address)
  279. {
  280. #ifdef DEBUG_UNUSED_IOPORT
  281. fprintf(stderr, "unused inb: port=0x%04x\n", address);
  282. #endif
  283. return 0xff;
  284. }
  285. static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
  286. {
  287. #ifdef DEBUG_UNUSED_IOPORT
  288. fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
  289. #endif
  290. }
  291. /* default is to make two byte accesses */
  292. static uint32_t default_ioport_readw(void *opaque, uint32_t address)
  293. {
  294. uint32_t data;
  295. data = ioport_read(0, address);
  296. address = (address + 1) & (MAX_IOPORTS - 1);
  297. data |= ioport_read(0, address) << 8;
  298. return data;
  299. }
  300. static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
  301. {
  302. ioport_write(0, address, data & 0xff);
  303. address = (address + 1) & (MAX_IOPORTS - 1);
  304. ioport_write(0, address, (data >> 8) & 0xff);
  305. }
  306. static uint32_t default_ioport_readl(void *opaque, uint32_t address)
  307. {
  308. #ifdef DEBUG_UNUSED_IOPORT
  309. fprintf(stderr, "unused inl: port=0x%04x\n", address);
  310. #endif
  311. return 0xffffffff;
  312. }
  313. static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
  314. {
  315. #ifdef DEBUG_UNUSED_IOPORT
  316. fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
  317. #endif
  318. }
  319. /* size is the word size in byte */
  320. int register_ioport_read(int start, int length, int size,
  321. IOPortReadFunc *func, void *opaque)
  322. {
  323. int i, bsize;
  324. if (size == 1) {
  325. bsize = 0;
  326. } else if (size == 2) {
  327. bsize = 1;
  328. } else if (size == 4) {
  329. bsize = 2;
  330. } else {
  331. hw_error("register_ioport_read: invalid size");
  332. return -1;
  333. }
  334. for(i = start; i < start + length; i += size) {
  335. ioport_read_table[bsize][i] = func;
  336. if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
  337. hw_error("register_ioport_read: invalid opaque");
  338. ioport_opaque[i] = opaque;
  339. }
  340. return 0;
  341. }
  342. /* size is the word size in byte */
  343. int register_ioport_write(int start, int length, int size,
  344. IOPortWriteFunc *func, void *opaque)
  345. {
  346. int i, bsize;
  347. if (size == 1) {
  348. bsize = 0;
  349. } else if (size == 2) {
  350. bsize = 1;
  351. } else if (size == 4) {
  352. bsize = 2;
  353. } else {
  354. hw_error("register_ioport_write: invalid size");
  355. return -1;
  356. }
  357. for(i = start; i < start + length; i += size) {
  358. ioport_write_table[bsize][i] = func;
  359. if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
  360. hw_error("register_ioport_write: invalid opaque");
  361. ioport_opaque[i] = opaque;
  362. }
  363. return 0;
  364. }
  365. void isa_unassign_ioport(int start, int length)
  366. {
  367. int i;
  368. for(i = start; i < start + length; i++) {
  369. ioport_read_table[0][i] = default_ioport_readb;
  370. ioport_read_table[1][i] = default_ioport_readw;
  371. ioport_read_table[2][i] = default_ioport_readl;
  372. ioport_write_table[0][i] = default_ioport_writeb;
  373. ioport_write_table[1][i] = default_ioport_writew;
  374. ioport_write_table[2][i] = default_ioport_writel;
  375. ioport_opaque[i] = NULL;
  376. }
  377. }
  378. /***********************************************************/
  379. void cpu_outb(CPUState *env, int addr, int val)
  380. {
  381. LOG_IOPORT("outb: %04x %02x\n", addr, val);
  382. ioport_write(0, addr, val);
  383. #ifdef USE_KQEMU
  384. if (env)
  385. env->last_io_time = cpu_get_time_fast();
  386. #endif
  387. }
  388. void cpu_outw(CPUState *env, int addr, int val)
  389. {
  390. LOG_IOPORT("outw: %04x %04x\n", addr, val);
  391. ioport_write(1, addr, val);
  392. #ifdef USE_KQEMU
  393. if (env)
  394. env->last_io_time = cpu_get_time_fast();
  395. #endif
  396. }
  397. void cpu_outl(CPUState *env, int addr, int val)
  398. {
  399. LOG_IOPORT("outl: %04x %08x\n", addr, val);
  400. ioport_write(2, addr, val);
  401. #ifdef USE_KQEMU
  402. if (env)
  403. env->last_io_time = cpu_get_time_fast();
  404. #endif
  405. }
  406. int cpu_inb(CPUState *env, int addr)
  407. {
  408. int val;
  409. val = ioport_read(0, addr);
  410. LOG_IOPORT("inb : %04x %02x\n", addr, val);
  411. #ifdef USE_KQEMU
  412. if (env)
  413. env->last_io_time = cpu_get_time_fast();
  414. #endif
  415. return val;
  416. }
  417. int cpu_inw(CPUState *env, int addr)
  418. {
  419. int val;
  420. val = ioport_read(1, addr);
  421. LOG_IOPORT("inw : %04x %04x\n", addr, val);
  422. #ifdef USE_KQEMU
  423. if (env)
  424. env->last_io_time = cpu_get_time_fast();
  425. #endif
  426. return val;
  427. }
  428. int cpu_inl(CPUState *env, int addr)
  429. {
  430. int val;
  431. val = ioport_read(2, addr);
  432. LOG_IOPORT("inl : %04x %08x\n", addr, val);
  433. #ifdef USE_KQEMU
  434. if (env)
  435. env->last_io_time = cpu_get_time_fast();
  436. #endif
  437. return val;
  438. }
  439. /***********************************************************/
  440. void hw_error(const char *fmt, ...)
  441. {
  442. va_list ap;
  443. CPUState *env;
  444. va_start(ap, fmt);
  445. fprintf(stderr, "qemu: hardware error: ");
  446. vfprintf(stderr, fmt, ap);
  447. fprintf(stderr, "\n");
  448. for(env = first_cpu; env != NULL; env = env->next_cpu) {
  449. fprintf(stderr, "CPU #%d:\n", env->cpu_index);
  450. #ifdef TARGET_I386
  451. cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
  452. #else
  453. cpu_dump_state(env, stderr, fprintf, 0);
  454. #endif
  455. }
  456. va_end(ap);
  457. abort();
  458. }
  459. /***************/
  460. /* ballooning */
  461. static QEMUBalloonEvent *qemu_balloon_event;
  462. void *qemu_balloon_event_opaque;
  463. void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
  464. {
  465. qemu_balloon_event = func;
  466. qemu_balloon_event_opaque = opaque;
  467. }
  468. void qemu_balloon(ram_addr_t target)
  469. {
  470. if (qemu_balloon_event)
  471. qemu_balloon_event(qemu_balloon_event_opaque, target);
  472. }
  473. ram_addr_t qemu_balloon_status(void)
  474. {
  475. if (qemu_balloon_event)
  476. return qemu_balloon_event(qemu_balloon_event_opaque, 0);
  477. return 0;
  478. }
  479. /***********************************************************/
  480. /* keyboard/mouse */
  481. static QEMUPutKBDEvent *qemu_put_kbd_event;
  482. static void *qemu_put_kbd_event_opaque;
  483. static QEMUPutMouseEntry *qemu_put_mouse_event_head;
  484. static QEMUPutMouseEntry *qemu_put_mouse_event_current;
  485. void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
  486. {
  487. qemu_put_kbd_event_opaque = opaque;
  488. qemu_put_kbd_event = func;
  489. }
  490. QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
  491. void *opaque, int absolute,
  492. const char *name)
  493. {
  494. QEMUPutMouseEntry *s, *cursor;
  495. s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
  496. s->qemu_put_mouse_event = func;
  497. s->qemu_put_mouse_event_opaque = opaque;
  498. s->qemu_put_mouse_event_absolute = absolute;
  499. s->qemu_put_mouse_event_name = qemu_strdup(name);
  500. s->next = NULL;
  501. if (!qemu_put_mouse_event_head) {
  502. qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
  503. return s;
  504. }
  505. cursor = qemu_put_mouse_event_head;
  506. while (cursor->next != NULL)
  507. cursor = cursor->next;
  508. cursor->next = s;
  509. qemu_put_mouse_event_current = s;
  510. return s;
  511. }
  512. void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
  513. {
  514. QEMUPutMouseEntry *prev = NULL, *cursor;
  515. if (!qemu_put_mouse_event_head || entry == NULL)
  516. return;
  517. cursor = qemu_put_mouse_event_head;
  518. while (cursor != NULL && cursor != entry) {
  519. prev = cursor;
  520. cursor = cursor->next;
  521. }
  522. if (cursor == NULL) // does not exist or list empty
  523. return;
  524. else if (prev == NULL) { // entry is head
  525. qemu_put_mouse_event_head = cursor->next;
  526. if (qemu_put_mouse_event_current == entry)
  527. qemu_put_mouse_event_current = cursor->next;
  528. qemu_free(entry->qemu_put_mouse_event_name);
  529. qemu_free(entry);
  530. return;
  531. }
  532. prev->next = entry->next;
  533. if (qemu_put_mouse_event_current == entry)
  534. qemu_put_mouse_event_current = prev;
  535. qemu_free(entry->qemu_put_mouse_event_name);
  536. qemu_free(entry);
  537. }
  538. void kbd_put_keycode(int keycode)
  539. {
  540. if (qemu_put_kbd_event) {
  541. qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
  542. }
  543. }
  544. void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
  545. {
  546. QEMUPutMouseEvent *mouse_event;
  547. void *mouse_event_opaque;
  548. int width;
  549. if (!qemu_put_mouse_event_current) {
  550. return;
  551. }
  552. mouse_event =
  553. qemu_put_mouse_event_current->qemu_put_mouse_event;
  554. mouse_event_opaque =
  555. qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
  556. if (mouse_event) {
  557. if (graphic_rotate) {
  558. if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
  559. width = 0x7fff;
  560. else
  561. width = graphic_width - 1;
  562. mouse_event(mouse_event_opaque,
  563. width - dy, dx, dz, buttons_state);
  564. } else
  565. mouse_event(mouse_event_opaque,
  566. dx, dy, dz, buttons_state);
  567. }
  568. }
  569. int kbd_mouse_is_absolute(void)
  570. {
  571. if (!qemu_put_mouse_event_current)
  572. return 0;
  573. return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
  574. }
  575. void do_info_mice(void)
  576. {
  577. QEMUPutMouseEntry *cursor;
  578. int index = 0;
  579. if (!qemu_put_mouse_event_head) {
  580. term_printf("No mouse devices connected\n");
  581. return;
  582. }
  583. term_printf("Mouse devices available:\n");
  584. cursor = qemu_put_mouse_event_head;
  585. while (cursor != NULL) {
  586. term_printf("%c Mouse #%d: %s\n",
  587. (cursor == qemu_put_mouse_event_current ? '*' : ' '),
  588. index, cursor->qemu_put_mouse_event_name);
  589. index++;
  590. cursor = cursor->next;
  591. }
  592. }
  593. void do_mouse_set(int index)
  594. {
  595. QEMUPutMouseEntry *cursor;
  596. int i = 0;
  597. if (!qemu_put_mouse_event_head) {
  598. term_printf("No mouse devices connected\n");
  599. return;
  600. }
  601. cursor = qemu_put_mouse_event_head;
  602. while (cursor != NULL && index != i) {
  603. i++;
  604. cursor = cursor->next;
  605. }
  606. if (cursor != NULL)
  607. qemu_put_mouse_event_current = cursor;
  608. else
  609. term_printf("Mouse at given index not found\n");
  610. }
  611. /* compute with 96 bit intermediate result: (a*b)/c */
  612. uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
  613. {
  614. union {
  615. uint64_t ll;
  616. struct {
  617. #ifdef WORDS_BIGENDIAN
  618. uint32_t high, low;
  619. #else
  620. uint32_t low, high;
  621. #endif
  622. } l;
  623. } u, res;
  624. uint64_t rl, rh;
  625. u.ll = a;
  626. rl = (uint64_t)u.l.low * (uint64_t)b;
  627. rh = (uint64_t)u.l.high * (uint64_t)b;
  628. rh += (rl >> 32);
  629. res.l.high = rh / c;
  630. res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
  631. return res.ll;
  632. }
  633. /***********************************************************/
  634. /* real time host monotonic timer */
  635. #define QEMU_TIMER_BASE 1000000000LL
  636. #ifdef WIN32
  637. static int64_t clock_freq;
  638. static void init_get_clock(void)
  639. {
  640. LARGE_INTEGER freq;
  641. int ret;
  642. ret = QueryPerformanceFrequency(&freq);
  643. if (ret == 0) {
  644. fprintf(stderr, "Could not calibrate ticks\n");
  645. exit(1);
  646. }
  647. clock_freq = freq.QuadPart;
  648. }
  649. static int64_t get_clock(void)
  650. {
  651. LARGE_INTEGER ti;
  652. QueryPerformanceCounter(&ti);
  653. return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
  654. }
  655. #else
  656. static int use_rt_clock;
  657. static void init_get_clock(void)
  658. {
  659. use_rt_clock = 0;
  660. #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
  661. {
  662. struct timespec ts;
  663. if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
  664. use_rt_clock = 1;
  665. }
  666. }
  667. #endif
  668. }
  669. static int64_t get_clock(void)
  670. {
  671. #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
  672. if (use_rt_clock) {
  673. struct timespec ts;
  674. clock_gettime(CLOCK_MONOTONIC, &ts);
  675. return ts.tv_sec * 1000000000LL + ts.tv_nsec;
  676. } else
  677. #endif
  678. {
  679. /* XXX: using gettimeofday leads to problems if the date
  680. changes, so it should be avoided. */
  681. struct timeval tv;
  682. gettimeofday(&tv, NULL);
  683. return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
  684. }
  685. }
  686. #endif
  687. /* Return the virtual CPU time, based on the instruction counter. */
  688. static int64_t cpu_get_icount(void)
  689. {
  690. int64_t icount;
  691. CPUState *env = cpu_single_env;;
  692. icount = qemu_icount;
  693. if (env) {
  694. if (!can_do_io(env))
  695. fprintf(stderr, "Bad clock read\n");
  696. icount -= (env->icount_decr.u16.low + env->icount_extra);
  697. }
  698. return qemu_icount_bias + (icount << icount_time_shift);
  699. }
  700. /***********************************************************/
  701. /* guest cycle counter */
  702. static int64_t cpu_ticks_prev;
  703. static int64_t cpu_ticks_offset;
  704. static int64_t cpu_clock_offset;
  705. static int cpu_ticks_enabled;
  706. /* return the host CPU cycle counter and handle stop/restart */
  707. int64_t cpu_get_ticks(void)
  708. {
  709. if (use_icount) {
  710. return cpu_get_icount();
  711. }
  712. if (!cpu_ticks_enabled) {
  713. return cpu_ticks_offset;
  714. } else {
  715. int64_t ticks;
  716. ticks = cpu_get_real_ticks();
  717. if (cpu_ticks_prev > ticks) {
  718. /* Note: non increasing ticks may happen if the host uses
  719. software suspend */
  720. cpu_ticks_offset += cpu_ticks_prev - ticks;
  721. }
  722. cpu_ticks_prev = ticks;
  723. return ticks + cpu_ticks_offset;
  724. }
  725. }
  726. /* return the host CPU monotonic timer and handle stop/restart */
  727. static int64_t cpu_get_clock(void)
  728. {
  729. int64_t ti;
  730. if (!cpu_ticks_enabled) {
  731. return cpu_clock_offset;
  732. } else {
  733. ti = get_clock();
  734. return ti + cpu_clock_offset;
  735. }
  736. }
  737. /* enable cpu_get_ticks() */
  738. void cpu_enable_ticks(void)
  739. {
  740. if (!cpu_ticks_enabled) {
  741. cpu_ticks_offset -= cpu_get_real_ticks();
  742. cpu_clock_offset -= get_clock();
  743. cpu_ticks_enabled = 1;
  744. }
  745. }
  746. /* disable cpu_get_ticks() : the clock is stopped. You must not call
  747. cpu_get_ticks() after that. */
  748. void cpu_disable_ticks(void)
  749. {
  750. if (cpu_ticks_enabled) {
  751. cpu_ticks_offset = cpu_get_ticks();
  752. cpu_clock_offset = cpu_get_clock();
  753. cpu_ticks_enabled = 0;
  754. }
  755. }
  756. /***********************************************************/
  757. /* timers */
  758. #define QEMU_TIMER_REALTIME 0
  759. #define QEMU_TIMER_VIRTUAL 1
  760. struct QEMUClock {
  761. int type;
  762. /* XXX: add frequency */
  763. };
  764. struct QEMUTimer {
  765. QEMUClock *clock;
  766. int64_t expire_time;
  767. QEMUTimerCB *cb;
  768. void *opaque;
  769. struct QEMUTimer *next;
  770. };
  771. struct qemu_alarm_timer {
  772. char const *name;
  773. unsigned int flags;
  774. int (*start)(struct qemu_alarm_timer *t);
  775. void (*stop)(struct qemu_alarm_timer *t);
  776. void (*rearm)(struct qemu_alarm_timer *t);
  777. void *priv;
  778. };
  779. #define ALARM_FLAG_DYNTICKS 0x1
  780. #define ALARM_FLAG_EXPIRED 0x2
  781. static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
  782. {
  783. return t && (t->flags & ALARM_FLAG_DYNTICKS);
  784. }
  785. static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
  786. {
  787. if (!alarm_has_dynticks(t))
  788. return;
  789. t->rearm(t);
  790. }
  791. /* TODO: MIN_TIMER_REARM_US should be optimized */
  792. #define MIN_TIMER_REARM_US 250
  793. static struct qemu_alarm_timer *alarm_timer;
  794. #ifndef _WIN32
  795. static int alarm_timer_rfd, alarm_timer_wfd;
  796. #endif
  797. #ifdef _WIN32
  798. struct qemu_alarm_win32 {
  799. MMRESULT timerId;
  800. HANDLE host_alarm;
  801. unsigned int period;
  802. } alarm_win32_data = {0, NULL, -1};
  803. static int win32_start_timer(struct qemu_alarm_timer *t);
  804. static void win32_stop_timer(struct qemu_alarm_timer *t);
  805. static void win32_rearm_timer(struct qemu_alarm_timer *t);
  806. #else
  807. static int unix_start_timer(struct qemu_alarm_timer *t);
  808. static void unix_stop_timer(struct qemu_alarm_timer *t);
  809. #ifdef __linux__
  810. static int dynticks_start_timer(struct qemu_alarm_timer *t);
  811. static void dynticks_stop_timer(struct qemu_alarm_timer *t);
  812. static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
  813. static int hpet_start_timer(struct qemu_alarm_timer *t);
  814. static void hpet_stop_timer(struct qemu_alarm_timer *t);
  815. static int rtc_start_timer(struct qemu_alarm_timer *t);
  816. static void rtc_stop_timer(struct qemu_alarm_timer *t);
  817. #endif /* __linux__ */
  818. #endif /* _WIN32 */
  819. /* Correlation between real and virtual time is always going to be
  820. fairly approximate, so ignore small variation.
  821. When the guest is idle real and virtual time will be aligned in
  822. the IO wait loop. */
  823. #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
  824. static void icount_adjust(void)
  825. {
  826. int64_t cur_time;
  827. int64_t cur_icount;
  828. int64_t delta;
  829. static int64_t last_delta;
  830. /* If the VM is not running, then do nothing. */
  831. if (!vm_running)
  832. return;
  833. cur_time = cpu_get_clock();
  834. cur_icount = qemu_get_clock(vm_clock);
  835. delta = cur_icount - cur_time;
  836. /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
  837. if (delta > 0
  838. && last_delta + ICOUNT_WOBBLE < delta * 2
  839. && icount_time_shift > 0) {
  840. /* The guest is getting too far ahead. Slow time down. */
  841. icount_time_shift--;
  842. }
  843. if (delta < 0
  844. && last_delta - ICOUNT_WOBBLE > delta * 2
  845. && icount_time_shift < MAX_ICOUNT_SHIFT) {
  846. /* The guest is getting too far behind. Speed time up. */
  847. icount_time_shift++;
  848. }
  849. last_delta = delta;
  850. qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
  851. }
  852. static void icount_adjust_rt(void * opaque)
  853. {
  854. qemu_mod_timer(icount_rt_timer,
  855. qemu_get_clock(rt_clock) + 1000);
  856. icount_adjust();
  857. }
  858. static void icount_adjust_vm(void * opaque)
  859. {
  860. qemu_mod_timer(icount_vm_timer,
  861. qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
  862. icount_adjust();
  863. }
  864. static void init_icount_adjust(void)
  865. {
  866. /* Have both realtime and virtual time triggers for speed adjustment.
  867. The realtime trigger catches emulated time passing too slowly,
  868. the virtual time trigger catches emulated time passing too fast.
  869. Realtime triggers occur even when idle, so use them less frequently
  870. than VM triggers. */
  871. icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
  872. qemu_mod_timer(icount_rt_timer,
  873. qemu_get_clock(rt_clock) + 1000);
  874. icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
  875. qemu_mod_timer(icount_vm_timer,
  876. qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
  877. }
  878. static struct qemu_alarm_timer alarm_timers[] = {
  879. #ifndef _WIN32
  880. #ifdef __linux__
  881. {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
  882. dynticks_stop_timer, dynticks_rearm_timer, NULL},
  883. /* HPET - if available - is preferred */
  884. {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
  885. /* ...otherwise try RTC */
  886. {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
  887. #endif
  888. {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
  889. #else
  890. {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
  891. win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
  892. {"win32", 0, win32_start_timer,
  893. win32_stop_timer, NULL, &alarm_win32_data},
  894. #endif
  895. {NULL, }
  896. };
  897. static void show_available_alarms(void)
  898. {
  899. int i;
  900. printf("Available alarm timers, in order of precedence:\n");
  901. for (i = 0; alarm_timers[i].name; i++)
  902. printf("%s\n", alarm_timers[i].name);
  903. }
  904. static void configure_alarms(char const *opt)
  905. {
  906. int i;
  907. int cur = 0;
  908. int count = ARRAY_SIZE(alarm_timers) - 1;
  909. char *arg;
  910. char *name;
  911. struct qemu_alarm_timer tmp;
  912. if (!strcmp(opt, "?")) {
  913. show_available_alarms();
  914. exit(0);
  915. }
  916. arg = strdup(opt);
  917. /* Reorder the array */
  918. name = strtok(arg, ",");
  919. while (name) {
  920. for (i = 0; i < count && alarm_timers[i].name; i++) {
  921. if (!strcmp(alarm_timers[i].name, name))
  922. break;
  923. }
  924. if (i == count) {
  925. fprintf(stderr, "Unknown clock %s\n", name);
  926. goto next;
  927. }
  928. if (i < cur)
  929. /* Ignore */
  930. goto next;
  931. /* Swap */
  932. tmp = alarm_timers[i];
  933. alarm_timers[i] = alarm_timers[cur];
  934. alarm_timers[cur] = tmp;
  935. cur++;
  936. next:
  937. name = strtok(NULL, ",");
  938. }
  939. free(arg);
  940. if (cur) {
  941. /* Disable remaining timers */
  942. for (i = cur; i < count; i++)
  943. alarm_timers[i].name = NULL;
  944. } else {
  945. show_available_alarms();
  946. exit(1);
  947. }
  948. }
  949. QEMUClock *rt_clock;
  950. QEMUClock *vm_clock;
  951. static QEMUTimer *active_timers[2];
  952. static QEMUClock *qemu_new_clock(int type)
  953. {
  954. QEMUClock *clock;
  955. clock = qemu_mallocz(sizeof(QEMUClock));
  956. clock->type = type;
  957. return clock;
  958. }
  959. QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
  960. {
  961. QEMUTimer *ts;
  962. ts = qemu_mallocz(sizeof(QEMUTimer));
  963. ts->clock = clock;
  964. ts->cb = cb;
  965. ts->opaque = opaque;
  966. return ts;
  967. }
  968. void qemu_free_timer(QEMUTimer *ts)
  969. {
  970. qemu_free(ts);
  971. }
  972. /* stop a timer, but do not dealloc it */
  973. void qemu_del_timer(QEMUTimer *ts)
  974. {
  975. QEMUTimer **pt, *t;
  976. /* NOTE: this code must be signal safe because
  977. qemu_timer_expired() can be called from a signal. */
  978. pt = &active_timers[ts->clock->type];
  979. for(;;) {
  980. t = *pt;
  981. if (!t)
  982. break;
  983. if (t == ts) {
  984. *pt = t->next;
  985. break;
  986. }
  987. pt = &t->next;
  988. }
  989. }
  990. /* modify the current timer so that it will be fired when current_time
  991. >= expire_time. The corresponding callback will be called. */
  992. void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
  993. {
  994. QEMUTimer **pt, *t;
  995. qemu_del_timer(ts);
  996. /* add the timer in the sorted list */
  997. /* NOTE: this code must be signal safe because
  998. qemu_timer_expired() can be called from a signal. */
  999. pt = &active_timers[ts->clock->type];
  1000. for(;;) {
  1001. t = *pt;
  1002. if (!t)
  1003. break;
  1004. if (t->expire_time > expire_time)
  1005. break;
  1006. pt = &t->next;
  1007. }
  1008. ts->expire_time = expire_time;
  1009. ts->next = *pt;
  1010. *pt = ts;
  1011. /* Rearm if necessary */
  1012. if (pt == &active_timers[ts->clock->type]) {
  1013. if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
  1014. qemu_rearm_alarm_timer(alarm_timer);
  1015. }
  1016. /* Interrupt execution to force deadline recalculation. */
  1017. if (use_icount && cpu_single_env) {
  1018. cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
  1019. }
  1020. }
  1021. }
  1022. int qemu_timer_pending(QEMUTimer *ts)
  1023. {
  1024. QEMUTimer *t;
  1025. for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
  1026. if (t == ts)
  1027. return 1;
  1028. }
  1029. return 0;
  1030. }
  1031. static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
  1032. {
  1033. if (!timer_head)
  1034. return 0;
  1035. return (timer_head->expire_time <= current_time);
  1036. }
  1037. static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
  1038. {
  1039. QEMUTimer *ts;
  1040. for(;;) {
  1041. ts = *ptimer_head;
  1042. if (!ts || ts->expire_time > current_time)
  1043. break;
  1044. /* remove timer from the list before calling the callback */
  1045. *ptimer_head = ts->next;
  1046. ts->next = NULL;
  1047. /* run the callback (the timer list can be modified) */
  1048. ts->cb(ts->opaque);
  1049. }
  1050. }
  1051. int64_t qemu_get_clock(QEMUClock *clock)
  1052. {
  1053. switch(clock->type) {
  1054. case QEMU_TIMER_REALTIME:
  1055. return get_clock() / 1000000;
  1056. default:
  1057. case QEMU_TIMER_VIRTUAL:
  1058. if (use_icount) {
  1059. return cpu_get_icount();
  1060. } else {
  1061. return cpu_get_clock();
  1062. }
  1063. }
  1064. }
  1065. static void init_timers(void)
  1066. {
  1067. init_get_clock();
  1068. ticks_per_sec = QEMU_TIMER_BASE;
  1069. rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
  1070. vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
  1071. }
  1072. /* save a timer */
  1073. void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
  1074. {
  1075. uint64_t expire_time;
  1076. if (qemu_timer_pending(ts)) {
  1077. expire_time = ts->expire_time;
  1078. } else {
  1079. expire_time = -1;
  1080. }
  1081. qemu_put_be64(f, expire_time);
  1082. }
  1083. void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
  1084. {
  1085. uint64_t expire_time;
  1086. expire_time = qemu_get_be64(f);
  1087. if (expire_time != -1) {
  1088. qemu_mod_timer(ts, expire_time);
  1089. } else {
  1090. qemu_del_timer(ts);
  1091. }
  1092. }
  1093. static void timer_save(QEMUFile *f, void *opaque)
  1094. {
  1095. if (cpu_ticks_enabled) {
  1096. hw_error("cannot save state if virtual timers are running");
  1097. }
  1098. qemu_put_be64(f, cpu_ticks_offset);
  1099. qemu_put_be64(f, ticks_per_sec);
  1100. qemu_put_be64(f, cpu_clock_offset);
  1101. }
  1102. static int timer_load(QEMUFile *f, void *opaque, int version_id)
  1103. {
  1104. if (version_id != 1 && version_id != 2)
  1105. return -EINVAL;
  1106. if (cpu_ticks_enabled) {
  1107. return -EINVAL;
  1108. }
  1109. cpu_ticks_offset=qemu_get_be64(f);
  1110. ticks_per_sec=qemu_get_be64(f);
  1111. if (version_id == 2) {
  1112. cpu_clock_offset=qemu_get_be64(f);
  1113. }
  1114. return 0;
  1115. }
  1116. #ifdef _WIN32
  1117. void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
  1118. DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
  1119. #else
  1120. static void host_alarm_handler(int host_signum)
  1121. #endif
  1122. {
  1123. #if 0
  1124. #define DISP_FREQ 1000
  1125. {
  1126. static int64_t delta_min = INT64_MAX;
  1127. static int64_t delta_max, delta_cum, last_clock, delta, ti;
  1128. static int count;
  1129. ti = qemu_get_clock(vm_clock);
  1130. if (last_clock != 0) {
  1131. delta = ti - last_clock;
  1132. if (delta < delta_min)
  1133. delta_min = delta;
  1134. if (delta > delta_max)
  1135. delta_max = delta;
  1136. delta_cum += delta;
  1137. if (++count == DISP_FREQ) {
  1138. printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
  1139. muldiv64(delta_min, 1000000, ticks_per_sec),
  1140. muldiv64(delta_max, 1000000, ticks_per_sec),
  1141. muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
  1142. (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
  1143. count = 0;
  1144. delta_min = INT64_MAX;
  1145. delta_max = 0;
  1146. delta_cum = 0;
  1147. }
  1148. }
  1149. last_clock = ti;
  1150. }
  1151. #endif
  1152. if (alarm_has_dynticks(alarm_timer) ||
  1153. (!use_icount &&
  1154. qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
  1155. qemu_get_clock(vm_clock))) ||
  1156. qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
  1157. qemu_get_clock(rt_clock))) {
  1158. CPUState *env = next_cpu;
  1159. #ifdef _WIN32
  1160. struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
  1161. SetEvent(data->host_alarm);
  1162. #else
  1163. static const char byte = 0;
  1164. write(alarm_timer_wfd, &byte, sizeof(byte));
  1165. #endif
  1166. if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
  1167. if (env) {
  1168. /* stop the currently executing cpu because a timer occured */
  1169. cpu_interrupt(env, CPU_INTERRUPT_EXIT);
  1170. #ifdef USE_KQEMU
  1171. if (env->kqemu_enabled) {
  1172. kqemu_cpu_interrupt(env);
  1173. }
  1174. #endif
  1175. }
  1176. event_pending = 1;
  1177. }
  1178. }
  1179. static int64_t qemu_next_deadline(void)
  1180. {
  1181. int64_t delta;
  1182. if (active_timers[QEMU_TIMER_VIRTUAL]) {
  1183. delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
  1184. qemu_get_clock(vm_clock);
  1185. } else {
  1186. /* To avoid problems with overflow limit this to 2^32. */
  1187. delta = INT32_MAX;
  1188. }
  1189. if (delta < 0)
  1190. delta = 0;
  1191. return delta;
  1192. }
  1193. #if defined(__linux__) || defined(_WIN32)
  1194. static uint64_t qemu_next_deadline_dyntick(void)
  1195. {
  1196. int64_t delta;
  1197. int64_t rtdelta;
  1198. if (use_icount)
  1199. delta = INT32_MAX;
  1200. else
  1201. delta = (qemu_next_deadline() + 999) / 1000;
  1202. if (active_timers[QEMU_TIMER_REALTIME]) {
  1203. rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
  1204. qemu_get_clock(rt_clock))*1000;
  1205. if (rtdelta < delta)
  1206. delta = rtdelta;
  1207. }
  1208. if (delta < MIN_TIMER_REARM_US)
  1209. delta = MIN_TIMER_REARM_US;
  1210. return delta;
  1211. }
  1212. #endif
  1213. #ifndef _WIN32
  1214. /* Sets a specific flag */
  1215. static int fcntl_setfl(int fd, int flag)
  1216. {
  1217. int flags;
  1218. flags = fcntl(fd, F_GETFL);
  1219. if (flags == -1)
  1220. return -errno;
  1221. if (fcntl(fd, F_SETFL, flags | flag) == -1)
  1222. return -errno;
  1223. return 0;
  1224. }
  1225. #if defined(__linux__)
  1226. #define RTC_FREQ 1024
  1227. static void enable_sigio_timer(int fd)
  1228. {
  1229. struct sigaction act;
  1230. /* timer signal */
  1231. sigfillset(&act.sa_mask);
  1232. act.sa_flags = 0;
  1233. act.sa_handler = host_alarm_handler;
  1234. sigaction(SIGIO, &act, NULL);
  1235. fcntl_setfl(fd, O_ASYNC);
  1236. fcntl(fd, F_SETOWN, getpid());
  1237. }
  1238. static int hpet_start_timer(struct qemu_alarm_timer *t)
  1239. {
  1240. struct hpet_info info;
  1241. int r, fd;
  1242. fd = open("/dev/hpet", O_RDONLY);
  1243. if (fd < 0)
  1244. return -1;
  1245. /* Set frequency */
  1246. r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
  1247. if (r < 0) {
  1248. fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
  1249. "error, but for better emulation accuracy type:\n"
  1250. "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
  1251. goto fail;
  1252. }
  1253. /* Check capabilities */
  1254. r = ioctl(fd, HPET_INFO, &info);
  1255. if (r < 0)
  1256. goto fail;
  1257. /* Enable periodic mode */
  1258. r = ioctl(fd, HPET_EPI, 0);
  1259. if (info.hi_flags && (r < 0))
  1260. goto fail;
  1261. /* Enable interrupt */
  1262. r = ioctl(fd, HPET_IE_ON, 0);
  1263. if (r < 0)
  1264. goto fail;
  1265. enable_sigio_timer(fd);
  1266. t->priv = (void *)(long)fd;
  1267. return 0;
  1268. fail:
  1269. close(fd);
  1270. return -1;
  1271. }
  1272. static void hpet_stop_timer(struct qemu_alarm_timer *t)
  1273. {
  1274. int fd = (long)t->priv;
  1275. close(fd);
  1276. }
  1277. static int rtc_start_timer(struct qemu_alarm_timer *t)
  1278. {
  1279. int rtc_fd;
  1280. unsigned long current_rtc_freq = 0;
  1281. TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
  1282. if (rtc_fd < 0)
  1283. return -1;
  1284. ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
  1285. if (current_rtc_freq != RTC_FREQ &&
  1286. ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
  1287. fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
  1288. "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
  1289. "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
  1290. goto fail;
  1291. }
  1292. if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
  1293. fail:
  1294. close(rtc_fd);
  1295. return -1;
  1296. }
  1297. enable_sigio_timer(rtc_fd);
  1298. t->priv = (void *)(long)rtc_fd;
  1299. return 0;
  1300. }
  1301. static void rtc_stop_timer(struct qemu_alarm_timer *t)
  1302. {
  1303. int rtc_fd = (long)t->priv;
  1304. close(rtc_fd);
  1305. }
  1306. static int dynticks_start_timer(struct qemu_alarm_timer *t)
  1307. {
  1308. struct sigevent ev;
  1309. timer_t host_timer;
  1310. struct sigaction act;
  1311. sigfillset(&act.sa_mask);
  1312. act.sa_flags = 0;
  1313. act.sa_handler = host_alarm_handler;
  1314. sigaction(SIGALRM, &act, NULL);
  1315. /*
  1316. * Initialize ev struct to 0 to avoid valgrind complaining
  1317. * about uninitialized data in timer_create call
  1318. */
  1319. memset(&ev, 0, sizeof(ev));
  1320. ev.sigev_value.sival_int = 0;
  1321. ev.sigev_notify = SIGEV_SIGNAL;
  1322. ev.sigev_signo = SIGALRM;
  1323. if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
  1324. perror("timer_create");
  1325. /* disable dynticks */
  1326. fprintf(stderr, "Dynamic Ticks disabled\n");
  1327. return -1;
  1328. }
  1329. t->priv = (void *)(long)host_timer;
  1330. return 0;
  1331. }
  1332. static void dynticks_stop_timer(struct qemu_alarm_timer *t)
  1333. {
  1334. timer_t host_timer = (timer_t)(long)t->priv;
  1335. timer_delete(host_timer);
  1336. }
  1337. static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
  1338. {
  1339. timer_t host_timer = (timer_t)(long)t->priv;
  1340. struct itimerspec timeout;
  1341. int64_t nearest_delta_us = INT64_MAX;
  1342. int64_t current_us;
  1343. if (!active_timers[QEMU_TIMER_REALTIME] &&
  1344. !active_timers[QEMU_TIMER_VIRTUAL])
  1345. return;
  1346. nearest_delta_us = qemu_next_deadline_dyntick();
  1347. /* check whether a timer is already running */
  1348. if (timer_gettime(host_timer, &timeout)) {
  1349. perror("gettime");
  1350. fprintf(stderr, "Internal timer error: aborting\n");
  1351. exit(1);
  1352. }
  1353. current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
  1354. if (current_us && current_us <= nearest_delta_us)
  1355. return;
  1356. timeout.it_interval.tv_sec = 0;
  1357. timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
  1358. timeout.it_value.tv_sec = nearest_delta_us / 1000000;
  1359. timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
  1360. if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
  1361. perror("settime");
  1362. fprintf(stderr, "Internal timer error: aborting\n");
  1363. exit(1);
  1364. }
  1365. }
  1366. #endif /* defined(__linux__) */
  1367. static int unix_start_timer(struct qemu_alarm_timer *t)
  1368. {
  1369. struct sigaction act;
  1370. struct itimerval itv;
  1371. int err;
  1372. /* timer signal */
  1373. sigfillset(&act.sa_mask);
  1374. act.sa_flags = 0;
  1375. act.sa_handler = host_alarm_handler;
  1376. sigaction(SIGALRM, &act, NULL);
  1377. itv.it_interval.tv_sec = 0;
  1378. /* for i386 kernel 2.6 to get 1 ms */
  1379. itv.it_interval.tv_usec = 999;
  1380. itv.it_value.tv_sec = 0;
  1381. itv.it_value.tv_usec = 10 * 1000;
  1382. err = setitimer(ITIMER_REAL, &itv, NULL);
  1383. if (err)
  1384. return -1;
  1385. return 0;
  1386. }
  1387. static void unix_stop_timer(struct qemu_alarm_timer *t)
  1388. {
  1389. struct itimerval itv;
  1390. memset(&itv, 0, sizeof(itv));
  1391. setitimer(ITIMER_REAL, &itv, NULL);
  1392. }
  1393. #endif /* !defined(_WIN32) */
  1394. static void try_to_rearm_timer(void *opaque)
  1395. {
  1396. struct qemu_alarm_timer *t = opaque;
  1397. #ifndef _WIN32
  1398. ssize_t len;
  1399. /* Drain the notify pipe */
  1400. do {
  1401. char buffer[512];
  1402. len = read(alarm_timer_rfd, buffer, sizeof(buffer));
  1403. } while ((len == -1 && errno == EINTR) || len > 0);
  1404. #endif
  1405. if (t->flags & ALARM_FLAG_EXPIRED) {
  1406. alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
  1407. qemu_rearm_alarm_timer(alarm_timer);
  1408. }
  1409. }
  1410. #ifdef _WIN32
  1411. static int win32_start_timer(struct qemu_alarm_timer *t)
  1412. {
  1413. TIMECAPS tc;
  1414. struct qemu_alarm_win32 *data = t->priv;
  1415. UINT flags;
  1416. data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
  1417. if (!data->host_alarm) {
  1418. perror("Failed CreateEvent");
  1419. return -1;
  1420. }
  1421. memset(&tc, 0, sizeof(tc));
  1422. timeGetDevCaps(&tc, sizeof(tc));
  1423. if (data->period < tc.wPeriodMin)
  1424. data->period = tc.wPeriodMin;
  1425. timeBeginPeriod(data->period);
  1426. flags = TIME_CALLBACK_FUNCTION;
  1427. if (alarm_has_dynticks(t))
  1428. flags |= TIME_ONESHOT;
  1429. else
  1430. flags |= TIME_PERIODIC;
  1431. data->timerId = timeSetEvent(1, // interval (ms)
  1432. data->period, // resolution
  1433. host_alarm_handler, // function
  1434. (DWORD)t, // parameter
  1435. flags);
  1436. if (!data->timerId) {
  1437. perror("Failed to initialize win32 alarm timer");
  1438. timeEndPeriod(data->period);
  1439. CloseHandle(data->host_alarm);
  1440. return -1;
  1441. }
  1442. qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
  1443. return 0;
  1444. }
  1445. static void win32_stop_timer(struct qemu_alarm_timer *t)
  1446. {
  1447. struct qemu_alarm_win32 *data = t->priv;
  1448. timeKillEvent(data->timerId);
  1449. timeEndPeriod(data->period);
  1450. CloseHandle(data->host_alarm);
  1451. }
  1452. static void win32_rearm_timer(struct qemu_alarm_timer *t)
  1453. {
  1454. struct qemu_alarm_win32 *data = t->priv;
  1455. uint64_t nearest_delta_us;
  1456. if (!active_timers[QEMU_TIMER_REALTIME] &&
  1457. !active_timers[QEMU_TIMER_VIRTUAL])
  1458. return;
  1459. nearest_delta_us = qemu_next_deadline_dyntick();
  1460. nearest_delta_us /= 1000;
  1461. timeKillEvent(data->timerId);
  1462. data->timerId = timeSetEvent(1,
  1463. data->period,
  1464. host_alarm_handler,
  1465. (DWORD)t,
  1466. TIME_ONESHOT | TIME_PERIODIC);
  1467. if (!data->timerId) {
  1468. perror("Failed to re-arm win32 alarm timer");
  1469. timeEndPeriod(data->period);
  1470. CloseHandle(data->host_alarm);
  1471. exit(1);
  1472. }
  1473. }
  1474. #endif /* _WIN32 */
  1475. static int init_timer_alarm(void)
  1476. {
  1477. struct qemu_alarm_timer *t = NULL;
  1478. int i, err = -1;
  1479. #ifndef _WIN32
  1480. int fds[2];
  1481. err = pipe(fds);
  1482. if (err == -1)
  1483. return -errno;
  1484. err = fcntl_setfl(fds[0], O_NONBLOCK);
  1485. if (err < 0)
  1486. goto fail;
  1487. err = fcntl_setfl(fds[1], O_NONBLOCK);
  1488. if (err < 0)
  1489. goto fail;
  1490. alarm_timer_rfd = fds[0];
  1491. alarm_timer_wfd = fds[1];
  1492. #endif
  1493. for (i = 0; alarm_timers[i].name; i++) {
  1494. t = &alarm_timers[i];
  1495. err = t->start(t);
  1496. if (!err)
  1497. break;
  1498. }
  1499. if (err) {
  1500. err = -ENOENT;
  1501. goto fail;
  1502. }
  1503. #ifndef _WIN32
  1504. qemu_set_fd_handler2(alarm_timer_rfd, NULL,
  1505. try_to_rearm_timer, NULL, t);
  1506. #endif
  1507. alarm_timer = t;
  1508. return 0;
  1509. fail:
  1510. #ifndef _WIN32
  1511. close(fds[0]);
  1512. close(fds[1]);
  1513. #endif
  1514. return err;
  1515. }
  1516. static void quit_timers(void)
  1517. {
  1518. alarm_timer->stop(alarm_timer);
  1519. alarm_timer = NULL;
  1520. }
  1521. /***********************************************************/
  1522. /* host time/date access */
  1523. void qemu_get_timedate(struct tm *tm, int offset)
  1524. {
  1525. time_t ti;
  1526. struct tm *ret;
  1527. time(&ti);
  1528. ti += offset;
  1529. if (rtc_date_offset == -1) {
  1530. if (rtc_utc)
  1531. ret = gmtime(&ti);
  1532. else
  1533. ret = localtime(&ti);
  1534. } else {
  1535. ti -= rtc_date_offset;
  1536. ret = gmtime(&ti);
  1537. }
  1538. memcpy(tm, ret, sizeof(struct tm));
  1539. }
  1540. int qemu_timedate_diff(struct tm *tm)
  1541. {
  1542. time_t seconds;
  1543. if (rtc_date_offset == -1)
  1544. if (rtc_utc)
  1545. seconds = mktimegm(tm);
  1546. else
  1547. seconds = mktime(tm);
  1548. else
  1549. seconds = mktimegm(tm) + rtc_date_offset;
  1550. return seconds - time(NULL);
  1551. }
  1552. #ifdef _WIN32
  1553. static void socket_cleanup(void)
  1554. {
  1555. WSACleanup();
  1556. }
  1557. static int socket_init(void)
  1558. {
  1559. WSADATA Data;
  1560. int ret, err;
  1561. ret = WSAStartup(MAKEWORD(2,2), &Data);
  1562. if (ret != 0) {
  1563. err = WSAGetLastError();
  1564. fprintf(stderr, "WSAStartup: %d\n", err);
  1565. return -1;
  1566. }
  1567. atexit(socket_cleanup);
  1568. return 0;
  1569. }
  1570. #endif
  1571. const char *get_opt_name(char *buf, int buf_size, const char *p)
  1572. {
  1573. char *q;
  1574. q = buf;
  1575. while (*p != '\0' && *p != '=') {
  1576. if (q && (q - buf) < buf_size - 1)
  1577. *q++ = *p;
  1578. p++;
  1579. }
  1580. if (q)
  1581. *q = '\0';
  1582. return p;
  1583. }
  1584. const char *get_opt_value(char *buf, int buf_size, const char *p)
  1585. {
  1586. char *q;
  1587. q = buf;
  1588. while (*p != '\0') {
  1589. if (*p == ',') {
  1590. if (*(p + 1) != ',')
  1591. break;
  1592. p++;
  1593. }
  1594. if (q && (q - buf) < buf_size - 1)
  1595. *q++ = *p;
  1596. p++;
  1597. }
  1598. if (q)
  1599. *q = '\0';
  1600. return p;
  1601. }
  1602. int get_param_value(char *buf, int buf_size,
  1603. const char *tag, const char *str)
  1604. {
  1605. const char *p;
  1606. char option[128];
  1607. p = str;
  1608. for(;;) {
  1609. p = get_opt_name(option, sizeof(option), p);
  1610. if (*p != '=')
  1611. break;
  1612. p++;
  1613. if (!strcmp(tag, option)) {
  1614. (void)get_opt_value(buf, buf_size, p);
  1615. return strlen(buf);
  1616. } else {
  1617. p = get_opt_value(NULL, 0, p);
  1618. }
  1619. if (*p != ',')
  1620. break;
  1621. p++;
  1622. }
  1623. return 0;
  1624. }
  1625. int check_params(char *buf, int buf_size,
  1626. const char * const *params, const char *str)
  1627. {
  1628. const char *p;
  1629. int i;
  1630. p = str;
  1631. for(;;) {
  1632. p = get_opt_name(buf, buf_size, p);
  1633. if (*p != '=')
  1634. return -1;
  1635. p++;
  1636. for(i = 0; params[i] != NULL; i++)
  1637. if (!strcmp(params[i], buf))
  1638. break;
  1639. if (params[i] == NULL)
  1640. return -1;
  1641. p = get_opt_value(NULL, 0, p);
  1642. if (*p != ',')
  1643. break;
  1644. p++;
  1645. }
  1646. return 0;
  1647. }
  1648. /***********************************************************/
  1649. /* Bluetooth support */
  1650. static int nb_hcis;
  1651. static int cur_hci;
  1652. static struct HCIInfo *hci_table[MAX_NICS];
  1653. static struct bt_vlan_s {
  1654. struct bt_scatternet_s net;
  1655. int id;
  1656. struct bt_vlan_s *next;
  1657. } *first_bt_vlan;
  1658. /* find or alloc a new bluetooth "VLAN" */
  1659. static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
  1660. {
  1661. struct bt_vlan_s **pvlan, *vlan;
  1662. for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
  1663. if (vlan->id == id)
  1664. return &vlan->net;
  1665. }
  1666. vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
  1667. vlan->id = id;
  1668. pvlan = &first_bt_vlan;
  1669. while (*pvlan != NULL)
  1670. pvlan = &(*pvlan)->next;
  1671. *pvlan = vlan;
  1672. return &vlan->net;
  1673. }
  1674. static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
  1675. {
  1676. }
  1677. static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
  1678. {
  1679. return -ENOTSUP;
  1680. }
  1681. static struct HCIInfo null_hci = {
  1682. .cmd_send = null_hci_send,
  1683. .sco_send = null_hci_send,
  1684. .acl_send = null_hci_send,
  1685. .bdaddr_set = null_hci_addr_set,
  1686. };
  1687. struct HCIInfo *qemu_next_hci(void)
  1688. {
  1689. if (cur_hci == nb_hcis)
  1690. return &null_hci;
  1691. return hci_table[cur_hci++];
  1692. }
  1693. static struct HCIInfo *hci_init(const char *str)
  1694. {
  1695. char *endp;
  1696. struct bt_scatternet_s *vlan = 0;
  1697. if (!strcmp(str, "null"))
  1698. /* null */
  1699. return &null_hci;
  1700. else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
  1701. /* host[:hciN] */
  1702. return bt_host_hci(str[4] ? str + 5 : "hci0");
  1703. else if (!strncmp(str, "hci", 3)) {
  1704. /* hci[,vlan=n] */
  1705. if (str[3]) {
  1706. if (!strncmp(str + 3, ",vlan=", 6)) {
  1707. vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
  1708. if (*endp)
  1709. vlan = 0;
  1710. }
  1711. } else
  1712. vlan = qemu_find_bt_vlan(0);
  1713. if (vlan)
  1714. return bt_new_hci(vlan);
  1715. }
  1716. fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
  1717. return 0;
  1718. }
  1719. static int bt_hci_parse(const char *str)
  1720. {
  1721. struct HCIInfo *hci;
  1722. bdaddr_t bdaddr;
  1723. if (nb_hcis >= MAX_NICS) {
  1724. fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
  1725. return -1;
  1726. }
  1727. hci = hci_init(str);
  1728. if (!hci)
  1729. return -1;
  1730. bdaddr.b[0] = 0x52;
  1731. bdaddr.b[1] = 0x54;
  1732. bdaddr.b[2] = 0x00;
  1733. bdaddr.b[3] = 0x12;
  1734. bdaddr.b[4] = 0x34;
  1735. bdaddr.b[5] = 0x56 + nb_hcis;
  1736. hci->bdaddr_set(hci, bdaddr.b);
  1737. hci_table[nb_hcis++] = hci;
  1738. return 0;
  1739. }
  1740. static void bt_vhci_add(int vlan_id)
  1741. {
  1742. struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
  1743. if (!vlan->slave)
  1744. fprintf(stderr, "qemu: warning: adding a VHCI to "
  1745. "an empty scatternet %i\n", vlan_id);
  1746. bt_vhci_init(bt_new_hci(vlan));
  1747. }
  1748. static struct bt_device_s *bt_device_add(const char *opt)
  1749. {
  1750. struct bt_scatternet_s *vlan;
  1751. int vlan_id = 0;
  1752. char *endp = strstr(opt, ",vlan=");
  1753. int len = (endp ? endp - opt : strlen(opt)) + 1;
  1754. char devname[10];
  1755. pstrcpy(devname, MIN(sizeof(devname), len), opt);
  1756. if (endp) {
  1757. vlan_id = strtol(endp + 6, &endp, 0);
  1758. if (*endp) {
  1759. fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
  1760. return 0;
  1761. }
  1762. }
  1763. vlan = qemu_find_bt_vlan(vlan_id);
  1764. if (!vlan->slave)
  1765. fprintf(stderr, "qemu: warning: adding a slave device to "
  1766. "an empty scatternet %i\n", vlan_id);
  1767. if (!strcmp(devname, "keyboard"))
  1768. return bt_keyboard_init(vlan);
  1769. fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
  1770. return 0;
  1771. }
  1772. static int bt_parse(const char *opt)
  1773. {
  1774. const char *endp, *p;
  1775. int vlan;
  1776. if (strstart(opt, "hci", &endp)) {
  1777. if (!*endp || *endp == ',') {
  1778. if (*endp)
  1779. if (!strstart(endp, ",vlan=", 0))
  1780. opt = endp + 1;
  1781. return bt_hci_parse(opt);
  1782. }
  1783. } else if (strstart(opt, "vhci", &endp)) {
  1784. if (!*endp || *endp == ',') {
  1785. if (*endp) {
  1786. if (strstart(endp, ",vlan=", &p)) {
  1787. vlan = strtol(p, (char **) &endp, 0);
  1788. if (*endp) {
  1789. fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
  1790. return 1;
  1791. }
  1792. } else {
  1793. fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
  1794. return 1;
  1795. }
  1796. } else
  1797. vlan = 0;
  1798. bt_vhci_add(vlan);
  1799. return 0;
  1800. }
  1801. } else if (strstart(opt, "device:", &endp))
  1802. return !bt_device_add(endp);
  1803. fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
  1804. return 1;
  1805. }
  1806. /***********************************************************/
  1807. /* QEMU Block devices */
  1808. #define HD_ALIAS "index=%d,media=disk"
  1809. #ifdef TARGET_PPC
  1810. #define CDROM_ALIAS "index=1,media=cdrom"
  1811. #else
  1812. #define CDROM_ALIAS "index=2,media=cdrom"
  1813. #endif
  1814. #define FD_ALIAS "index=%d,if=floppy"
  1815. #define PFLASH_ALIAS "if=pflash"
  1816. #define MTD_ALIAS "if=mtd"
  1817. #define SD_ALIAS "index=0,if=sd"
  1818. static int drive_opt_get_free_idx(void)
  1819. {
  1820. int index;
  1821. for (index = 0; index < MAX_DRIVES; index++)
  1822. if (!drives_opt[index].used) {
  1823. drives_opt[index].used = 1;
  1824. return index;
  1825. }
  1826. return -1;
  1827. }
  1828. static int drive_get_free_idx(void)
  1829. {
  1830. int index;
  1831. for (index = 0; index < MAX_DRIVES; index++)
  1832. if (!drives_table[index].used) {
  1833. drives_table[index].used = 1;
  1834. return index;
  1835. }
  1836. return -1;
  1837. }
  1838. int drive_add(const char *file, const char *fmt, ...)
  1839. {
  1840. va_list ap;
  1841. int index = drive_opt_get_free_idx();
  1842. if (nb_drives_opt >= MAX_DRIVES || index == -1) {
  1843. fprintf(stderr, "qemu: too many drives\n");
  1844. return -1;
  1845. }
  1846. drives_opt[index].file = file;
  1847. va_start(ap, fmt);
  1848. vsnprintf(drives_opt[index].opt,
  1849. sizeof(drives_opt[0].opt), fmt, ap);
  1850. va_end(ap);
  1851. nb_drives_opt++;
  1852. return index;
  1853. }
  1854. void drive_remove(int index)
  1855. {
  1856. drives_opt[index].used = 0;
  1857. nb_drives_opt--;
  1858. }
  1859. int drive_get_index(BlockInterfaceType type, int bus, int unit)
  1860. {
  1861. int index;
  1862. /* seek interface, bus and unit */
  1863. for (index = 0; index < MAX_DRIVES; index++)
  1864. if (drives_table[index].type == type &&
  1865. drives_table[index].bus == bus &&
  1866. drives_table[index].unit == unit &&
  1867. drives_table[index].used)
  1868. return index;
  1869. return -1;
  1870. }
  1871. int drive_get_max_bus(BlockInterfaceType type)
  1872. {
  1873. int max_bus;
  1874. int index;
  1875. max_bus = -1;
  1876. for (index = 0; index < nb_drives; index++) {
  1877. if(drives_table[index].type == type &&
  1878. drives_table[index].bus > max_bus)
  1879. max_bus = drives_table[index].bus;
  1880. }
  1881. return max_bus;
  1882. }
  1883. const char *drive_get_serial(BlockDriverState *bdrv)
  1884. {
  1885. int index;
  1886. for (index = 0; index < nb_drives; index++)
  1887. if (drives_table[index].bdrv == bdrv)
  1888. return drives_table[index].serial;
  1889. return "\0";
  1890. }
  1891. BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
  1892. {
  1893. int index;
  1894. for (index = 0; index < nb_drives; index++)
  1895. if (drives_table[index].bdrv == bdrv)
  1896. return drives_table[index].onerror;
  1897. return BLOCK_ERR_STOP_ENOSPC;
  1898. }
  1899. static void bdrv_format_print(void *opaque, const char *name)
  1900. {
  1901. fprintf(stderr, " %s", name);
  1902. }
  1903. void drive_uninit(BlockDriverState *bdrv)
  1904. {
  1905. int i;
  1906. for (i = 0; i < MAX_DRIVES; i++)
  1907. if (drives_table[i].bdrv == bdrv) {
  1908. drives_table[i].bdrv = NULL;
  1909. drives_table[i].used = 0;
  1910. drive_remove(drives_table[i].drive_opt_idx);
  1911. nb_drives--;
  1912. break;
  1913. }
  1914. }
  1915. int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
  1916. {
  1917. char buf[128];
  1918. char file[1024];
  1919. char devname[128];
  1920. char serial[21];
  1921. const char *mediastr = "";
  1922. BlockInterfaceType type;
  1923. enum { MEDIA_DISK, MEDIA_CDROM } media;
  1924. int bus_id, unit_id;
  1925. int cyls, heads, secs, translation;
  1926. BlockDriverState *bdrv;
  1927. BlockDriver *drv = NULL;
  1928. QEMUMachine *machine = opaque;
  1929. int max_devs;
  1930. int index;
  1931. int cache;
  1932. int bdrv_flags, onerror;
  1933. int drives_table_idx;
  1934. char *str = arg->opt;
  1935. static const char * const params[] = { "bus", "unit", "if", "index",
  1936. "cyls", "heads", "secs", "trans",
  1937. "media", "snapshot", "file",
  1938. "cache", "format", "serial", "werror",
  1939. NULL };
  1940. if (check_params(buf, sizeof(buf), params, str) < 0) {
  1941. fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
  1942. buf, str);
  1943. return -1;
  1944. }
  1945. file[0] = 0;
  1946. cyls = heads = secs = 0;
  1947. bus_id = 0;
  1948. unit_id = -1;
  1949. translation = BIOS_ATA_TRANSLATION_AUTO;
  1950. index = -1;
  1951. cache = 3;
  1952. if (machine->use_scsi) {
  1953. type = IF_SCSI;
  1954. max_devs = MAX_SCSI_DEVS;
  1955. pstrcpy(devname, sizeof(devname), "scsi");
  1956. } else {
  1957. type = IF_IDE;
  1958. max_devs = MAX_IDE_DEVS;
  1959. pstrcpy(devname, sizeof(devname), "ide");
  1960. }
  1961. media = MEDIA_DISK;
  1962. /* extract parameters */
  1963. if (get_param_value(buf, sizeof(buf), "bus", str)) {
  1964. bus_id = strtol(buf, NULL, 0);
  1965. if (bus_id < 0) {
  1966. fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
  1967. return -1;
  1968. }
  1969. }
  1970. if (get_param_value(buf, sizeof(buf), "unit", str)) {
  1971. unit_id = strtol(buf, NULL, 0);
  1972. if (unit_id < 0) {
  1973. fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
  1974. return -1;
  1975. }
  1976. }
  1977. if (get_param_value(buf, sizeof(buf), "if", str)) {
  1978. pstrcpy(devname, sizeof(devname), buf);
  1979. if (!strcmp(buf, "ide")) {
  1980. type = IF_IDE;
  1981. max_devs = MAX_IDE_DEVS;
  1982. } else if (!strcmp(buf, "scsi")) {
  1983. type = IF_SCSI;
  1984. max_devs = MAX_SCSI_DEVS;
  1985. } else if (!strcmp(buf, "floppy")) {
  1986. type = IF_FLOPPY;
  1987. max_devs = 0;
  1988. } else if (!strcmp(buf, "pflash")) {
  1989. type = IF_PFLASH;
  1990. max_devs = 0;
  1991. } else if (!strcmp(buf, "mtd")) {
  1992. type = IF_MTD;
  1993. max_devs = 0;
  1994. } else if (!strcmp(buf, "sd")) {
  1995. type = IF_SD;
  1996. max_devs = 0;
  1997. } else if (!strcmp(buf, "virtio")) {
  1998. type = IF_VIRTIO;
  1999. max_devs = 0;
  2000. } else {
  2001. fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
  2002. return -1;
  2003. }
  2004. }
  2005. if (get_param_value(buf, sizeof(buf), "index", str)) {
  2006. index = strtol(buf, NULL, 0);
  2007. if (index < 0) {
  2008. fprintf(stderr, "qemu: '%s' invalid index\n", str);
  2009. return -1;
  2010. }
  2011. }
  2012. if (get_param_value(buf, sizeof(buf), "cyls", str)) {
  2013. cyls = strtol(buf, NULL, 0);
  2014. }
  2015. if (get_param_value(buf, sizeof(buf), "heads", str)) {
  2016. heads = strtol(buf, NULL, 0);
  2017. }
  2018. if (get_param_value(buf, sizeof(buf), "secs", str)) {
  2019. secs = strtol(buf, NULL, 0);
  2020. }
  2021. if (cyls || heads || secs) {
  2022. if (cyls < 1 || cyls > 16383) {
  2023. fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
  2024. return -1;
  2025. }
  2026. if (heads < 1 || heads > 16) {
  2027. fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
  2028. return -1;
  2029. }
  2030. if (secs < 1 || secs > 63) {
  2031. fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
  2032. return -1;
  2033. }
  2034. }
  2035. if (get_param_value(buf, sizeof(buf), "trans", str)) {
  2036. if (!cyls) {
  2037. fprintf(stderr,
  2038. "qemu: '%s' trans must be used with cyls,heads and secs\n",
  2039. str);
  2040. return -1;
  2041. }
  2042. if (!strcmp(buf, "none"))
  2043. translation = BIOS_ATA_TRANSLATION_NONE;
  2044. else if (!strcmp(buf, "lba"))
  2045. translation = BIOS_ATA_TRANSLATION_LBA;
  2046. else if (!strcmp(buf, "auto"))
  2047. translation = BIOS_ATA_TRANSLATION_AUTO;
  2048. else {
  2049. fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
  2050. return -1;
  2051. }
  2052. }
  2053. if (get_param_value(buf, sizeof(buf), "media", str)) {
  2054. if (!strcmp(buf, "disk")) {
  2055. media = MEDIA_DISK;
  2056. } else if (!strcmp(buf, "cdrom")) {
  2057. if (cyls || secs || heads) {
  2058. fprintf(stderr,
  2059. "qemu: '%s' invalid physical CHS format\n", str);
  2060. return -1;
  2061. }
  2062. media = MEDIA_CDROM;
  2063. } else {
  2064. fprintf(stderr, "qemu: '%s' invalid media\n", str);
  2065. return -1;
  2066. }
  2067. }
  2068. if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
  2069. if (!strcmp(buf, "on"))
  2070. snapshot = 1;
  2071. else if (!strcmp(buf, "off"))
  2072. snapshot = 0;
  2073. else {
  2074. fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
  2075. return -1;
  2076. }
  2077. }
  2078. if (get_param_value(buf, sizeof(buf), "cache", str)) {
  2079. if (!strcmp(buf, "off") || !strcmp(buf, "none"))
  2080. cache = 0;
  2081. else if (!strcmp(buf, "writethrough"))
  2082. cache = 1;
  2083. else if (!strcmp(buf, "writeback"))
  2084. cache = 2;
  2085. else {
  2086. fprintf(stderr, "qemu: invalid cache option\n");
  2087. return -1;
  2088. }
  2089. }
  2090. if (get_param_value(buf, sizeof(buf), "format", str)) {
  2091. if (strcmp(buf, "?") == 0) {
  2092. fprintf(stderr, "qemu: Supported formats:");
  2093. bdrv_iterate_format(bdrv_format_print, NULL);
  2094. fprintf(stderr, "\n");
  2095. return -1;
  2096. }
  2097. drv = bdrv_find_format(buf);
  2098. if (!drv) {
  2099. fprintf(stderr, "qemu: '%s' invalid format\n", buf);
  2100. return -1;
  2101. }
  2102. }
  2103. if (arg->file == NULL)
  2104. get_param_value(file, sizeof(file), "file", str);
  2105. else
  2106. pstrcpy(file, sizeof(file), arg->file);
  2107. if (!get_param_value(serial, sizeof(serial), "serial", str))
  2108. memset(serial, 0, sizeof(serial));
  2109. onerror = BLOCK_ERR_STOP_ENOSPC;
  2110. if (get_param_value(buf, sizeof(serial), "werror", str)) {
  2111. if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
  2112. fprintf(stderr, "werror is no supported by this format\n");
  2113. return -1;
  2114. }
  2115. if (!strcmp(buf, "ignore"))
  2116. onerror = BLOCK_ERR_IGNORE;
  2117. else if (!strcmp(buf, "enospc"))
  2118. onerror = BLOCK_ERR_STOP_ENOSPC;
  2119. else if (!strcmp(buf, "stop"))
  2120. onerror = BLOCK_ERR_STOP_ANY;
  2121. else if (!strcmp(buf, "report"))
  2122. onerror = BLOCK_ERR_REPORT;
  2123. else {
  2124. fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
  2125. return -1;
  2126. }
  2127. }
  2128. /* compute bus and unit according index */
  2129. if (index != -1) {
  2130. if (bus_id != 0 || unit_id != -1) {
  2131. fprintf(stderr,
  2132. "qemu: '%s' index cannot be used with bus and unit\n", str);
  2133. return -1;
  2134. }
  2135. if (max_devs == 0)
  2136. {
  2137. unit_id = index;
  2138. bus_id = 0;
  2139. } else {
  2140. unit_id = index % max_devs;
  2141. bus_id = index / max_devs;
  2142. }
  2143. }
  2144. /* if user doesn't specify a unit_id,
  2145. * try to find the first free
  2146. */
  2147. if (unit_id == -1) {
  2148. unit_id = 0;
  2149. while (drive_get_index(type, bus_id, unit_id) != -1) {
  2150. unit_id++;
  2151. if (max_devs && unit_id >= max_devs) {
  2152. unit_id -= max_devs;
  2153. bus_id++;
  2154. }
  2155. }
  2156. }
  2157. /* check unit id */
  2158. if (max_devs && unit_id >= max_devs) {
  2159. fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
  2160. str, unit_id, max_devs - 1);
  2161. return -1;
  2162. }
  2163. /*
  2164. * ignore multiple definitions
  2165. */
  2166. if (drive_get_index(type, bus_id, unit_id) != -1)
  2167. return -2;
  2168. /* init */
  2169. if (type == IF_IDE || type == IF_SCSI)
  2170. mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
  2171. if (max_devs)
  2172. snprintf(buf, sizeof(buf), "%s%i%s%i",
  2173. devname, bus_id, mediastr, unit_id);
  2174. else
  2175. snprintf(buf, sizeof(buf), "%s%s%i",
  2176. devname, mediastr, unit_id);
  2177. bdrv = bdrv_new(buf);
  2178. drives_table_idx = drive_get_free_idx();
  2179. drives_table[drives_table_idx].bdrv = bdrv;
  2180. drives_table[drives_table_idx].type = type;
  2181. drives_table[drives_table_idx].bus = bus_id;
  2182. drives_table[drives_table_idx].unit = unit_id;
  2183. drives_table[drives_table_idx].onerror = onerror;
  2184. drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
  2185. strncpy(drives_table[drives_table_idx].serial, serial, sizeof(serial));
  2186. nb_drives++;
  2187. switch(type) {
  2188. case IF_IDE:
  2189. case IF_SCSI:
  2190. switch(media) {
  2191. case MEDIA_DISK:
  2192. if (cyls != 0) {
  2193. bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
  2194. bdrv_set_translation_hint(bdrv, translation);
  2195. }
  2196. break;
  2197. case MEDIA_CDROM:
  2198. bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
  2199. break;
  2200. }
  2201. break;
  2202. case IF_SD:
  2203. /* FIXME: This isn't really a floppy, but it's a reasonable
  2204. approximation. */
  2205. case IF_FLOPPY:
  2206. bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
  2207. break;
  2208. case IF_PFLASH:
  2209. case IF_MTD:
  2210. case IF_VIRTIO:
  2211. break;
  2212. }
  2213. if (!file[0])
  2214. return -2;
  2215. bdrv_flags = 0;
  2216. if (snapshot) {
  2217. bdrv_flags |= BDRV_O_SNAPSHOT;
  2218. cache = 2; /* always use write-back with snapshot */
  2219. }
  2220. if (cache == 0) /* no caching */
  2221. bdrv_flags |= BDRV_O_NOCACHE;
  2222. else if (cache == 2) /* write-back */
  2223. bdrv_flags |= BDRV_O_CACHE_WB;
  2224. else if (cache == 3) /* not specified */
  2225. bdrv_flags |= BDRV_O_CACHE_DEF;
  2226. if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
  2227. fprintf(stderr, "qemu: could not open disk image %s\n",
  2228. file);
  2229. return -1;
  2230. }
  2231. if (bdrv_key_required(bdrv))
  2232. autostart = 0;
  2233. return drives_table_idx;
  2234. }
  2235. /***********************************************************/
  2236. /* USB devices */
  2237. static USBPort *used_usb_ports;
  2238. static USBPort *free_usb_ports;
  2239. /* ??? Maybe change this to register a hub to keep track of the topology. */
  2240. void qemu_register_usb_port(USBPort *port, void *opaque, int index,
  2241. usb_attachfn attach)
  2242. {
  2243. port->opaque = opaque;
  2244. port->index = index;
  2245. port->attach = attach;
  2246. port->next = free_usb_ports;
  2247. free_usb_ports = port;
  2248. }
  2249. int usb_device_add_dev(USBDevice *dev)
  2250. {
  2251. USBPort *port;
  2252. /* Find a USB port to add the device to. */
  2253. port = free_usb_ports;
  2254. if (!port->next) {
  2255. USBDevice *hub;
  2256. /* Create a new hub and chain it on. */
  2257. free_usb_ports = NULL;
  2258. port->next = used_usb_ports;
  2259. used_usb_ports = port;
  2260. hub = usb_hub_init(VM_USB_HUB_SIZE);
  2261. usb_attach(port, hub);
  2262. port = free_usb_ports;
  2263. }
  2264. free_usb_ports = port->next;
  2265. port->next = used_usb_ports;
  2266. used_usb_ports = port;
  2267. usb_attach(port, dev);
  2268. return 0;
  2269. }
  2270. static int usb_device_add(const char *devname, int is_hotplug)
  2271. {
  2272. const char *p;
  2273. USBDevice *dev;
  2274. if (!free_usb_ports)
  2275. return -1;
  2276. if (strstart(devname, "host:", &p)) {
  2277. dev = usb_host_device_open(p);
  2278. } else if (!strcmp(devname, "mouse")) {
  2279. dev = usb_mouse_init();
  2280. } else if (!strcmp(devname, "tablet")) {
  2281. dev = usb_tablet_init();
  2282. } else if (!strcmp(devname, "keyboard")) {
  2283. dev = usb_keyboard_init();
  2284. } else if (strstart(devname, "disk:", &p)) {
  2285. BlockDriverState *bs;
  2286. dev = usb_msd_init(p, &bs);
  2287. if (!dev)
  2288. return -1;
  2289. if (bdrv_key_required(bs)) {
  2290. autostart = 0;
  2291. if (is_hotplug && monitor_read_bdrv_key(bs) < 0) {
  2292. dev->handle_destroy(dev);
  2293. return -1;
  2294. }
  2295. }
  2296. } else if (!strcmp(devname, "wacom-tablet")) {
  2297. dev = usb_wacom_init();
  2298. } else if (strstart(devname, "serial:", &p)) {
  2299. dev = usb_serial_init(p);
  2300. #ifdef CONFIG_BRLAPI
  2301. } else if (!strcmp(devname, "braille")) {
  2302. dev = usb_baum_init();
  2303. #endif
  2304. } else if (strstart(devname, "net:", &p)) {
  2305. int nic = nb_nics;
  2306. if (net_client_init("nic", p) < 0)
  2307. return -1;
  2308. nd_table[nic].model = "usb";
  2309. dev = usb_net_init(&nd_table[nic]);
  2310. } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
  2311. dev = usb_bt_init(devname[2] ? hci_init(p) :
  2312. bt_new_hci(qemu_find_bt_vlan(0)));
  2313. } else {
  2314. return -1;
  2315. }
  2316. if (!dev)
  2317. return -1;
  2318. return usb_device_add_dev(dev);
  2319. }
  2320. int usb_device_del_addr(int bus_num, int addr)
  2321. {
  2322. USBPort *port;
  2323. USBPort **lastp;
  2324. USBDevice *dev;
  2325. if (!used_usb_ports)
  2326. return -1;
  2327. if (bus_num != 0)
  2328. return -1;
  2329. lastp = &used_usb_ports;
  2330. port = used_usb_ports;
  2331. while (port && port->dev->addr != addr) {
  2332. lastp = &port->next;
  2333. port = port->next;
  2334. }
  2335. if (!port)
  2336. return -1;
  2337. dev = port->dev;
  2338. *lastp = port->next;
  2339. usb_attach(port, NULL);
  2340. dev->handle_destroy(dev);
  2341. port->next = free_usb_ports;
  2342. free_usb_ports = port;
  2343. return 0;
  2344. }
  2345. static int usb_device_del(const char *devname)
  2346. {
  2347. int bus_num, addr;
  2348. const char *p;
  2349. if (strstart(devname, "host:", &p))
  2350. return usb_host_device_close(p);
  2351. if (!used_usb_ports)
  2352. return -1;
  2353. p = strchr(devname, '.');
  2354. if (!p)
  2355. return -1;
  2356. bus_num = strtoul(devname, NULL, 0);
  2357. addr = strtoul(p + 1, NULL, 0);
  2358. return usb_device_del_addr(bus_num, addr);
  2359. }
  2360. void do_usb_add(const char *devname)
  2361. {
  2362. usb_device_add(devname, 1);
  2363. }
  2364. void do_usb_del(const char *devname)
  2365. {
  2366. usb_device_del(devname);
  2367. }
  2368. void usb_info(void)
  2369. {
  2370. USBDevice *dev;
  2371. USBPort *port;
  2372. const char *speed_str;
  2373. if (!usb_enabled) {
  2374. term_printf("USB support not enabled\n");
  2375. return;
  2376. }
  2377. for (port = used_usb_ports; port; port = port->next) {
  2378. dev = port->dev;
  2379. if (!dev)
  2380. continue;
  2381. switch(dev->speed) {
  2382. case USB_SPEED_LOW:
  2383. speed_str = "1.5";
  2384. break;
  2385. case USB_SPEED_FULL:
  2386. speed_str = "12";
  2387. break;
  2388. case USB_SPEED_HIGH:
  2389. speed_str = "480";
  2390. break;
  2391. default:
  2392. speed_str = "?";
  2393. break;
  2394. }
  2395. term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
  2396. 0, dev->addr, speed_str, dev->devname);
  2397. }
  2398. }
  2399. /***********************************************************/
  2400. /* PCMCIA/Cardbus */
  2401. static struct pcmcia_socket_entry_s {
  2402. struct pcmcia_socket_s *socket;
  2403. struct pcmcia_socket_entry_s *next;
  2404. } *pcmcia_sockets = 0;
  2405. void pcmcia_socket_register(struct pcmcia_socket_s *socket)
  2406. {
  2407. struct pcmcia_socket_entry_s *entry;
  2408. entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
  2409. entry->socket = socket;
  2410. entry->next = pcmcia_sockets;
  2411. pcmcia_sockets = entry;
  2412. }
  2413. void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
  2414. {
  2415. struct pcmcia_socket_entry_s *entry, **ptr;
  2416. ptr = &pcmcia_sockets;
  2417. for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
  2418. if (entry->socket == socket) {
  2419. *ptr = entry->next;
  2420. qemu_free(entry);
  2421. }
  2422. }
  2423. void pcmcia_info(void)
  2424. {
  2425. struct pcmcia_socket_entry_s *iter;
  2426. if (!pcmcia_sockets)
  2427. term_printf("No PCMCIA sockets\n");
  2428. for (iter = pcmcia_sockets; iter; iter = iter->next)
  2429. term_printf("%s: %s\n", iter->socket->slot_string,
  2430. iter->socket->attached ? iter->socket->card_string :
  2431. "Empty");
  2432. }
  2433. /***********************************************************/
  2434. /* register display */
  2435. void register_displaystate(DisplayState *ds)
  2436. {
  2437. DisplayState **s;
  2438. s = &display_state;
  2439. while (*s != NULL)
  2440. s = &(*s)->next;
  2441. ds->next = NULL;
  2442. *s = ds;
  2443. }
  2444. DisplayState *get_displaystate(void)
  2445. {
  2446. return display_state;
  2447. }
  2448. /* dumb display */
  2449. static void dumb_display_init(void)
  2450. {
  2451. DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
  2452. ds->surface = qemu_create_displaysurface(640, 480, 32, 640 * 4);
  2453. register_displaystate(ds);
  2454. }
  2455. /***********************************************************/
  2456. /* I/O handling */
  2457. #define MAX_IO_HANDLERS 64
  2458. typedef struct IOHandlerRecord {
  2459. int fd;
  2460. IOCanRWHandler *fd_read_poll;
  2461. IOHandler *fd_read;
  2462. IOHandler *fd_write;
  2463. int deleted;
  2464. void *opaque;
  2465. /* temporary data */
  2466. struct pollfd *ufd;
  2467. struct IOHandlerRecord *next;
  2468. } IOHandlerRecord;
  2469. static IOHandlerRecord *first_io_handler;
  2470. /* XXX: fd_read_poll should be suppressed, but an API change is
  2471. necessary in the character devices to suppress fd_can_read(). */
  2472. int qemu_set_fd_handler2(int fd,
  2473. IOCanRWHandler *fd_read_poll,
  2474. IOHandler *fd_read,
  2475. IOHandler *fd_write,
  2476. void *opaque)
  2477. {
  2478. IOHandlerRecord **pioh, *ioh;
  2479. if (!fd_read && !fd_write) {
  2480. pioh = &first_io_handler;
  2481. for(;;) {
  2482. ioh = *pioh;
  2483. if (ioh == NULL)
  2484. break;
  2485. if (ioh->fd == fd) {
  2486. ioh->deleted = 1;
  2487. break;
  2488. }
  2489. pioh = &ioh->next;
  2490. }
  2491. } else {
  2492. for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
  2493. if (ioh->fd == fd)
  2494. goto found;
  2495. }
  2496. ioh = qemu_mallocz(sizeof(IOHandlerRecord));
  2497. ioh->next = first_io_handler;
  2498. first_io_handler = ioh;
  2499. found:
  2500. ioh->fd = fd;
  2501. ioh->fd_read_poll = fd_read_poll;
  2502. ioh->fd_read = fd_read;
  2503. ioh->fd_write = fd_write;
  2504. ioh->opaque = opaque;
  2505. ioh->deleted = 0;
  2506. }
  2507. return 0;
  2508. }
  2509. int qemu_set_fd_handler(int fd,
  2510. IOHandler *fd_read,
  2511. IOHandler *fd_write,
  2512. void *opaque)
  2513. {
  2514. return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
  2515. }
  2516. #ifdef _WIN32
  2517. /***********************************************************/
  2518. /* Polling handling */
  2519. typedef struct PollingEntry {
  2520. PollingFunc *func;
  2521. void *opaque;
  2522. struct PollingEntry *next;
  2523. } PollingEntry;
  2524. static PollingEntry *first_polling_entry;
  2525. int qemu_add_polling_cb(PollingFunc *func, void *opaque)
  2526. {
  2527. PollingEntry **ppe, *pe;
  2528. pe = qemu_mallocz(sizeof(PollingEntry));
  2529. pe->func = func;
  2530. pe->opaque = opaque;
  2531. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
  2532. *ppe = pe;
  2533. return 0;
  2534. }
  2535. void qemu_del_polling_cb(PollingFunc *func, void *opaque)
  2536. {
  2537. PollingEntry **ppe, *pe;
  2538. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
  2539. pe = *ppe;
  2540. if (pe->func == func && pe->opaque == opaque) {
  2541. *ppe = pe->next;
  2542. qemu_free(pe);
  2543. break;
  2544. }
  2545. }
  2546. }
  2547. /***********************************************************/
  2548. /* Wait objects support */
  2549. typedef struct WaitObjects {
  2550. int num;
  2551. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  2552. WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
  2553. void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
  2554. } WaitObjects;
  2555. static WaitObjects wait_objects = {0};
  2556. int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  2557. {
  2558. WaitObjects *w = &wait_objects;
  2559. if (w->num >= MAXIMUM_WAIT_OBJECTS)
  2560. return -1;
  2561. w->events[w->num] = handle;
  2562. w->func[w->num] = func;
  2563. w->opaque[w->num] = opaque;
  2564. w->num++;
  2565. return 0;
  2566. }
  2567. void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  2568. {
  2569. int i, found;
  2570. WaitObjects *w = &wait_objects;
  2571. found = 0;
  2572. for (i = 0; i < w->num; i++) {
  2573. if (w->events[i] == handle)
  2574. found = 1;
  2575. if (found) {
  2576. w->events[i] = w->events[i + 1];
  2577. w->func[i] = w->func[i + 1];
  2578. w->opaque[i] = w->opaque[i + 1];
  2579. }
  2580. }
  2581. if (found)
  2582. w->num--;
  2583. }
  2584. #endif
  2585. /***********************************************************/
  2586. /* ram save/restore */
  2587. static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
  2588. {
  2589. int v;
  2590. v = qemu_get_byte(f);
  2591. switch(v) {
  2592. case 0:
  2593. if (qemu_get_buffer(f, buf, len) != len)
  2594. return -EIO;
  2595. break;
  2596. case 1:
  2597. v = qemu_get_byte(f);
  2598. memset(buf, v, len);
  2599. break;
  2600. default:
  2601. return -EINVAL;
  2602. }
  2603. if (qemu_file_has_error(f))
  2604. return -EIO;
  2605. return 0;
  2606. }
  2607. static int ram_load_v1(QEMUFile *f, void *opaque)
  2608. {
  2609. int ret;
  2610. ram_addr_t i;
  2611. if (qemu_get_be32(f) != phys_ram_size)
  2612. return -EINVAL;
  2613. for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
  2614. ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
  2615. if (ret)
  2616. return ret;
  2617. }
  2618. return 0;
  2619. }
  2620. #define BDRV_HASH_BLOCK_SIZE 1024
  2621. #define IOBUF_SIZE 4096
  2622. #define RAM_CBLOCK_MAGIC 0xfabe
  2623. typedef struct RamDecompressState {
  2624. z_stream zstream;
  2625. QEMUFile *f;
  2626. uint8_t buf[IOBUF_SIZE];
  2627. } RamDecompressState;
  2628. static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
  2629. {
  2630. int ret;
  2631. memset(s, 0, sizeof(*s));
  2632. s->f = f;
  2633. ret = inflateInit(&s->zstream);
  2634. if (ret != Z_OK)
  2635. return -1;
  2636. return 0;
  2637. }
  2638. static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
  2639. {
  2640. int ret, clen;
  2641. s->zstream.avail_out = len;
  2642. s->zstream.next_out = buf;
  2643. while (s->zstream.avail_out > 0) {
  2644. if (s->zstream.avail_in == 0) {
  2645. if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
  2646. return -1;
  2647. clen = qemu_get_be16(s->f);
  2648. if (clen > IOBUF_SIZE)
  2649. return -1;
  2650. qemu_get_buffer(s->f, s->buf, clen);
  2651. s->zstream.avail_in = clen;
  2652. s->zstream.next_in = s->buf;
  2653. }
  2654. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  2655. if (ret != Z_OK && ret != Z_STREAM_END) {
  2656. return -1;
  2657. }
  2658. }
  2659. return 0;
  2660. }
  2661. static void ram_decompress_close(RamDecompressState *s)
  2662. {
  2663. inflateEnd(&s->zstream);
  2664. }
  2665. #define RAM_SAVE_FLAG_FULL 0x01
  2666. #define RAM_SAVE_FLAG_COMPRESS 0x02
  2667. #define RAM_SAVE_FLAG_MEM_SIZE 0x04
  2668. #define RAM_SAVE_FLAG_PAGE 0x08
  2669. #define RAM_SAVE_FLAG_EOS 0x10
  2670. static int is_dup_page(uint8_t *page, uint8_t ch)
  2671. {
  2672. uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
  2673. uint32_t *array = (uint32_t *)page;
  2674. int i;
  2675. for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
  2676. if (array[i] != val)
  2677. return 0;
  2678. }
  2679. return 1;
  2680. }
  2681. static int ram_save_block(QEMUFile *f)
  2682. {
  2683. static ram_addr_t current_addr = 0;
  2684. ram_addr_t saved_addr = current_addr;
  2685. ram_addr_t addr = 0;
  2686. int found = 0;
  2687. while (addr < phys_ram_size) {
  2688. if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
  2689. uint8_t ch;
  2690. cpu_physical_memory_reset_dirty(current_addr,
  2691. current_addr + TARGET_PAGE_SIZE,
  2692. MIGRATION_DIRTY_FLAG);
  2693. ch = *(phys_ram_base + current_addr);
  2694. if (is_dup_page(phys_ram_base + current_addr, ch)) {
  2695. qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
  2696. qemu_put_byte(f, ch);
  2697. } else {
  2698. qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
  2699. qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
  2700. }
  2701. found = 1;
  2702. break;
  2703. }
  2704. addr += TARGET_PAGE_SIZE;
  2705. current_addr = (saved_addr + addr) % phys_ram_size;
  2706. }
  2707. return found;
  2708. }
  2709. static ram_addr_t ram_save_threshold = 10;
  2710. static ram_addr_t ram_save_remaining(void)
  2711. {
  2712. ram_addr_t addr;
  2713. ram_addr_t count = 0;
  2714. for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
  2715. if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
  2716. count++;
  2717. }
  2718. return count;
  2719. }
  2720. static int ram_save_live(QEMUFile *f, int stage, void *opaque)
  2721. {
  2722. ram_addr_t addr;
  2723. if (stage == 1) {
  2724. /* Make sure all dirty bits are set */
  2725. for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
  2726. if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
  2727. cpu_physical_memory_set_dirty(addr);
  2728. }
  2729. /* Enable dirty memory tracking */
  2730. cpu_physical_memory_set_dirty_tracking(1);
  2731. qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
  2732. }
  2733. while (!qemu_file_rate_limit(f)) {
  2734. int ret;
  2735. ret = ram_save_block(f);
  2736. if (ret == 0) /* no more blocks */
  2737. break;
  2738. }
  2739. /* try transferring iterative blocks of memory */
  2740. if (stage == 3) {
  2741. /* flush all remaining blocks regardless of rate limiting */
  2742. while (ram_save_block(f) != 0);
  2743. cpu_physical_memory_set_dirty_tracking(0);
  2744. }
  2745. qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
  2746. return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
  2747. }
  2748. static int ram_load_dead(QEMUFile *f, void *opaque)
  2749. {
  2750. RamDecompressState s1, *s = &s1;
  2751. uint8_t buf[10];
  2752. ram_addr_t i;
  2753. if (ram_decompress_open(s, f) < 0)
  2754. return -EINVAL;
  2755. for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
  2756. if (ram_decompress_buf(s, buf, 1) < 0) {
  2757. fprintf(stderr, "Error while reading ram block header\n");
  2758. goto error;
  2759. }
  2760. if (buf[0] == 0) {
  2761. if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
  2762. fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
  2763. goto error;
  2764. }
  2765. } else {
  2766. error:
  2767. printf("Error block header\n");
  2768. return -EINVAL;
  2769. }
  2770. }
  2771. ram_decompress_close(s);
  2772. return 0;
  2773. }
  2774. static int ram_load(QEMUFile *f, void *opaque, int version_id)
  2775. {
  2776. ram_addr_t addr;
  2777. int flags;
  2778. if (version_id == 1)
  2779. return ram_load_v1(f, opaque);
  2780. if (version_id == 2) {
  2781. if (qemu_get_be32(f) != phys_ram_size)
  2782. return -EINVAL;
  2783. return ram_load_dead(f, opaque);
  2784. }
  2785. if (version_id != 3)
  2786. return -EINVAL;
  2787. do {
  2788. addr = qemu_get_be64(f);
  2789. flags = addr & ~TARGET_PAGE_MASK;
  2790. addr &= TARGET_PAGE_MASK;
  2791. if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
  2792. if (addr != phys_ram_size)
  2793. return -EINVAL;
  2794. }
  2795. if (flags & RAM_SAVE_FLAG_FULL) {
  2796. if (ram_load_dead(f, opaque) < 0)
  2797. return -EINVAL;
  2798. }
  2799. if (flags & RAM_SAVE_FLAG_COMPRESS) {
  2800. uint8_t ch = qemu_get_byte(f);
  2801. memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
  2802. } else if (flags & RAM_SAVE_FLAG_PAGE)
  2803. qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
  2804. } while (!(flags & RAM_SAVE_FLAG_EOS));
  2805. return 0;
  2806. }
  2807. void qemu_service_io(void)
  2808. {
  2809. CPUState *env = cpu_single_env;
  2810. if (env) {
  2811. cpu_interrupt(env, CPU_INTERRUPT_EXIT);
  2812. #ifdef USE_KQEMU
  2813. if (env->kqemu_enabled) {
  2814. kqemu_cpu_interrupt(env);
  2815. }
  2816. #endif
  2817. }
  2818. }
  2819. /***********************************************************/
  2820. /* bottom halves (can be seen as timers which expire ASAP) */
  2821. struct QEMUBH {
  2822. QEMUBHFunc *cb;
  2823. void *opaque;
  2824. int scheduled;
  2825. int idle;
  2826. int deleted;
  2827. QEMUBH *next;
  2828. };
  2829. static QEMUBH *first_bh = NULL;
  2830. QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
  2831. {
  2832. QEMUBH *bh;
  2833. bh = qemu_mallocz(sizeof(QEMUBH));
  2834. bh->cb = cb;
  2835. bh->opaque = opaque;
  2836. bh->next = first_bh;
  2837. first_bh = bh;
  2838. return bh;
  2839. }
  2840. int qemu_bh_poll(void)
  2841. {
  2842. QEMUBH *bh, **bhp;
  2843. int ret;
  2844. ret = 0;
  2845. for (bh = first_bh; bh; bh = bh->next) {
  2846. if (!bh->deleted && bh->scheduled) {
  2847. bh->scheduled = 0;
  2848. if (!bh->idle)
  2849. ret = 1;
  2850. bh->idle = 0;
  2851. bh->cb(bh->opaque);
  2852. }
  2853. }
  2854. /* remove deleted bhs */
  2855. bhp = &first_bh;
  2856. while (*bhp) {
  2857. bh = *bhp;
  2858. if (bh->deleted) {
  2859. *bhp = bh->next;
  2860. qemu_free(bh);
  2861. } else
  2862. bhp = &bh->next;
  2863. }
  2864. return ret;
  2865. }
  2866. void qemu_bh_schedule_idle(QEMUBH *bh)
  2867. {
  2868. if (bh->scheduled)
  2869. return;
  2870. bh->scheduled = 1;
  2871. bh->idle = 1;
  2872. }
  2873. void qemu_bh_schedule(QEMUBH *bh)
  2874. {
  2875. CPUState *env = cpu_single_env;
  2876. if (bh->scheduled)
  2877. return;
  2878. bh->scheduled = 1;
  2879. bh->idle = 0;
  2880. /* stop the currently executing CPU to execute the BH ASAP */
  2881. if (env) {
  2882. cpu_interrupt(env, CPU_INTERRUPT_EXIT);
  2883. }
  2884. }
  2885. void qemu_bh_cancel(QEMUBH *bh)
  2886. {
  2887. bh->scheduled = 0;
  2888. }
  2889. void qemu_bh_delete(QEMUBH *bh)
  2890. {
  2891. bh->scheduled = 0;
  2892. bh->deleted = 1;
  2893. }
  2894. static void qemu_bh_update_timeout(int *timeout)
  2895. {
  2896. QEMUBH *bh;
  2897. for (bh = first_bh; bh; bh = bh->next) {
  2898. if (!bh->deleted && bh->scheduled) {
  2899. if (bh->idle) {
  2900. /* idle bottom halves will be polled at least
  2901. * every 10ms */
  2902. *timeout = MIN(10, *timeout);
  2903. } else {
  2904. /* non-idle bottom halves will be executed
  2905. * immediately */
  2906. *timeout = 0;
  2907. break;
  2908. }
  2909. }
  2910. }
  2911. }
  2912. /***********************************************************/
  2913. /* machine registration */
  2914. static QEMUMachine *first_machine = NULL;
  2915. QEMUMachine *current_machine = NULL;
  2916. int qemu_register_machine(QEMUMachine *m)
  2917. {
  2918. QEMUMachine **pm;
  2919. pm = &first_machine;
  2920. while (*pm != NULL)
  2921. pm = &(*pm)->next;
  2922. m->next = NULL;
  2923. *pm = m;
  2924. return 0;
  2925. }
  2926. static QEMUMachine *find_machine(const char *name)
  2927. {
  2928. QEMUMachine *m;
  2929. for(m = first_machine; m != NULL; m = m->next) {
  2930. if (!strcmp(m->name, name))
  2931. return m;
  2932. }
  2933. return NULL;
  2934. }
  2935. /***********************************************************/
  2936. /* main execution loop */
  2937. static void gui_update(void *opaque)
  2938. {
  2939. uint64_t interval = GUI_REFRESH_INTERVAL;
  2940. DisplayState *ds = opaque;
  2941. DisplayChangeListener *dcl = ds->listeners;
  2942. dpy_refresh(ds);
  2943. while (dcl != NULL) {
  2944. if (dcl->gui_timer_interval &&
  2945. dcl->gui_timer_interval < interval)
  2946. interval = dcl->gui_timer_interval;
  2947. dcl = dcl->next;
  2948. }
  2949. qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
  2950. }
  2951. static void nographic_update(void *opaque)
  2952. {
  2953. uint64_t interval = GUI_REFRESH_INTERVAL;
  2954. qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
  2955. }
  2956. struct vm_change_state_entry {
  2957. VMChangeStateHandler *cb;
  2958. void *opaque;
  2959. LIST_ENTRY (vm_change_state_entry) entries;
  2960. };
  2961. static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
  2962. VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
  2963. void *opaque)
  2964. {
  2965. VMChangeStateEntry *e;
  2966. e = qemu_mallocz(sizeof (*e));
  2967. e->cb = cb;
  2968. e->opaque = opaque;
  2969. LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
  2970. return e;
  2971. }
  2972. void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
  2973. {
  2974. LIST_REMOVE (e, entries);
  2975. qemu_free (e);
  2976. }
  2977. static void vm_state_notify(int running, int reason)
  2978. {
  2979. VMChangeStateEntry *e;
  2980. for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
  2981. e->cb(e->opaque, running, reason);
  2982. }
  2983. }
  2984. void vm_start(void)
  2985. {
  2986. if (!vm_running) {
  2987. cpu_enable_ticks();
  2988. vm_running = 1;
  2989. vm_state_notify(1, 0);
  2990. qemu_rearm_alarm_timer(alarm_timer);
  2991. }
  2992. }
  2993. void vm_stop(int reason)
  2994. {
  2995. if (vm_running) {
  2996. cpu_disable_ticks();
  2997. vm_running = 0;
  2998. vm_state_notify(0, reason);
  2999. }
  3000. }
  3001. /* reset/shutdown handler */
  3002. typedef struct QEMUResetEntry {
  3003. QEMUResetHandler *func;
  3004. void *opaque;
  3005. struct QEMUResetEntry *next;
  3006. } QEMUResetEntry;
  3007. static QEMUResetEntry *first_reset_entry;
  3008. static int reset_requested;
  3009. static int shutdown_requested;
  3010. static int powerdown_requested;
  3011. int qemu_shutdown_requested(void)
  3012. {
  3013. int r = shutdown_requested;
  3014. shutdown_requested = 0;
  3015. return r;
  3016. }
  3017. int qemu_reset_requested(void)
  3018. {
  3019. int r = reset_requested;
  3020. reset_requested = 0;
  3021. return r;
  3022. }
  3023. int qemu_powerdown_requested(void)
  3024. {
  3025. int r = powerdown_requested;
  3026. powerdown_requested = 0;
  3027. return r;
  3028. }
  3029. void qemu_register_reset(QEMUResetHandler *func, void *opaque)
  3030. {
  3031. QEMUResetEntry **pre, *re;
  3032. pre = &first_reset_entry;
  3033. while (*pre != NULL)
  3034. pre = &(*pre)->next;
  3035. re = qemu_mallocz(sizeof(QEMUResetEntry));
  3036. re->func = func;
  3037. re->opaque = opaque;
  3038. re->next = NULL;
  3039. *pre = re;
  3040. }
  3041. void qemu_system_reset(void)
  3042. {
  3043. QEMUResetEntry *re;
  3044. /* reset all devices */
  3045. for(re = first_reset_entry; re != NULL; re = re->next) {
  3046. re->func(re->opaque);
  3047. }
  3048. }
  3049. void qemu_system_reset_request(void)
  3050. {
  3051. if (no_reboot) {
  3052. shutdown_requested = 1;
  3053. } else {
  3054. reset_requested = 1;
  3055. }
  3056. if (cpu_single_env)
  3057. cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
  3058. }
  3059. void qemu_system_shutdown_request(void)
  3060. {
  3061. shutdown_requested = 1;
  3062. if (cpu_single_env)
  3063. cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
  3064. }
  3065. void qemu_system_powerdown_request(void)
  3066. {
  3067. powerdown_requested = 1;
  3068. if (cpu_single_env)
  3069. cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
  3070. }
  3071. #ifdef _WIN32
  3072. static void host_main_loop_wait(int *timeout)
  3073. {
  3074. int ret, ret2, i;
  3075. PollingEntry *pe;
  3076. /* XXX: need to suppress polling by better using win32 events */
  3077. ret = 0;
  3078. for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
  3079. ret |= pe->func(pe->opaque);
  3080. }
  3081. if (ret == 0) {
  3082. int err;
  3083. WaitObjects *w = &wait_objects;
  3084. ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
  3085. if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
  3086. if (w->func[ret - WAIT_OBJECT_0])
  3087. w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
  3088. /* Check for additional signaled events */
  3089. for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
  3090. /* Check if event is signaled */
  3091. ret2 = WaitForSingleObject(w->events[i], 0);
  3092. if(ret2 == WAIT_OBJECT_0) {
  3093. if (w->func[i])
  3094. w->func[i](w->opaque[i]);
  3095. } else if (ret2 == WAIT_TIMEOUT) {
  3096. } else {
  3097. err = GetLastError();
  3098. fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
  3099. }
  3100. }
  3101. } else if (ret == WAIT_TIMEOUT) {
  3102. } else {
  3103. err = GetLastError();
  3104. fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
  3105. }
  3106. }
  3107. *timeout = 0;
  3108. }
  3109. #else
  3110. static void host_main_loop_wait(int *timeout)
  3111. {
  3112. }
  3113. #endif
  3114. void main_loop_wait(int timeout)
  3115. {
  3116. IOHandlerRecord *ioh;
  3117. fd_set rfds, wfds, xfds;
  3118. int ret, nfds;
  3119. struct timeval tv;
  3120. qemu_bh_update_timeout(&timeout);
  3121. host_main_loop_wait(&timeout);
  3122. /* poll any events */
  3123. /* XXX: separate device handlers from system ones */
  3124. nfds = -1;
  3125. FD_ZERO(&rfds);
  3126. FD_ZERO(&wfds);
  3127. FD_ZERO(&xfds);
  3128. for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
  3129. if (ioh->deleted)
  3130. continue;
  3131. if (ioh->fd_read &&
  3132. (!ioh->fd_read_poll ||
  3133. ioh->fd_read_poll(ioh->opaque) != 0)) {
  3134. FD_SET(ioh->fd, &rfds);
  3135. if (ioh->fd > nfds)
  3136. nfds = ioh->fd;
  3137. }
  3138. if (ioh->fd_write) {
  3139. FD_SET(ioh->fd, &wfds);
  3140. if (ioh->fd > nfds)
  3141. nfds = ioh->fd;
  3142. }
  3143. }
  3144. tv.tv_sec = timeout / 1000;
  3145. tv.tv_usec = (timeout % 1000) * 1000;
  3146. #if defined(CONFIG_SLIRP)
  3147. if (slirp_is_inited()) {
  3148. slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
  3149. }
  3150. #endif
  3151. ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
  3152. if (ret > 0) {
  3153. IOHandlerRecord **pioh;
  3154. for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
  3155. if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
  3156. ioh->fd_read(ioh->opaque);
  3157. }
  3158. if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
  3159. ioh->fd_write(ioh->opaque);
  3160. }
  3161. }
  3162. /* remove deleted IO handlers */
  3163. pioh = &first_io_handler;
  3164. while (*pioh) {
  3165. ioh = *pioh;
  3166. if (ioh->deleted) {
  3167. *pioh = ioh->next;
  3168. qemu_free(ioh);
  3169. } else
  3170. pioh = &ioh->next;
  3171. }
  3172. }
  3173. #if defined(CONFIG_SLIRP)
  3174. if (slirp_is_inited()) {
  3175. if (ret < 0) {
  3176. FD_ZERO(&rfds);
  3177. FD_ZERO(&wfds);
  3178. FD_ZERO(&xfds);
  3179. }
  3180. slirp_select_poll(&rfds, &wfds, &xfds);
  3181. }
  3182. #endif
  3183. /* vm time timers */
  3184. if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
  3185. qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
  3186. qemu_get_clock(vm_clock));
  3187. /* real time timers */
  3188. qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
  3189. qemu_get_clock(rt_clock));
  3190. /* Check bottom-halves last in case any of the earlier events triggered
  3191. them. */
  3192. qemu_bh_poll();
  3193. }
  3194. static int main_loop(void)
  3195. {
  3196. int ret, timeout;
  3197. #ifdef CONFIG_PROFILER
  3198. int64_t ti;
  3199. #endif
  3200. CPUState *env;
  3201. cur_cpu = first_cpu;
  3202. next_cpu = cur_cpu->next_cpu ?: first_cpu;
  3203. for(;;) {
  3204. if (vm_running) {
  3205. for(;;) {
  3206. /* get next cpu */
  3207. env = next_cpu;
  3208. #ifdef CONFIG_PROFILER
  3209. ti = profile_getclock();
  3210. #endif
  3211. if (use_icount) {
  3212. int64_t count;
  3213. int decr;
  3214. qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
  3215. env->icount_decr.u16.low = 0;
  3216. env->icount_extra = 0;
  3217. count = qemu_next_deadline();
  3218. count = (count + (1 << icount_time_shift) - 1)
  3219. >> icount_time_shift;
  3220. qemu_icount += count;
  3221. decr = (count > 0xffff) ? 0xffff : count;
  3222. count -= decr;
  3223. env->icount_decr.u16.low = decr;
  3224. env->icount_extra = count;
  3225. }
  3226. ret = cpu_exec(env);
  3227. #ifdef CONFIG_PROFILER
  3228. qemu_time += profile_getclock() - ti;
  3229. #endif
  3230. if (use_icount) {
  3231. /* Fold pending instructions back into the
  3232. instruction counter, and clear the interrupt flag. */
  3233. qemu_icount -= (env->icount_decr.u16.low
  3234. + env->icount_extra);
  3235. env->icount_decr.u32 = 0;
  3236. env->icount_extra = 0;
  3237. }
  3238. next_cpu = env->next_cpu ?: first_cpu;
  3239. if (event_pending && likely(ret != EXCP_DEBUG)) {
  3240. ret = EXCP_INTERRUPT;
  3241. event_pending = 0;
  3242. break;
  3243. }
  3244. if (ret == EXCP_HLT) {
  3245. /* Give the next CPU a chance to run. */
  3246. cur_cpu = env;
  3247. continue;
  3248. }
  3249. if (ret != EXCP_HALTED)
  3250. break;
  3251. /* all CPUs are halted ? */
  3252. if (env == cur_cpu)
  3253. break;
  3254. }
  3255. cur_cpu = env;
  3256. if (shutdown_requested) {
  3257. ret = EXCP_INTERRUPT;
  3258. if (no_shutdown) {
  3259. vm_stop(0);
  3260. no_shutdown = 0;
  3261. }
  3262. else
  3263. break;
  3264. }
  3265. if (reset_requested) {
  3266. reset_requested = 0;
  3267. qemu_system_reset();
  3268. ret = EXCP_INTERRUPT;
  3269. }
  3270. if (powerdown_requested) {
  3271. powerdown_requested = 0;
  3272. qemu_system_powerdown();
  3273. ret = EXCP_INTERRUPT;
  3274. }
  3275. if (unlikely(ret == EXCP_DEBUG)) {
  3276. gdb_set_stop_cpu(cur_cpu);
  3277. vm_stop(EXCP_DEBUG);
  3278. }
  3279. /* If all cpus are halted then wait until the next IRQ */
  3280. /* XXX: use timeout computed from timers */
  3281. if (ret == EXCP_HALTED) {
  3282. if (use_icount) {
  3283. int64_t add;
  3284. int64_t delta;
  3285. /* Advance virtual time to the next event. */
  3286. if (use_icount == 1) {
  3287. /* When not using an adaptive execution frequency
  3288. we tend to get badly out of sync with real time,
  3289. so just delay for a reasonable amount of time. */
  3290. delta = 0;
  3291. } else {
  3292. delta = cpu_get_icount() - cpu_get_clock();
  3293. }
  3294. if (delta > 0) {
  3295. /* If virtual time is ahead of real time then just
  3296. wait for IO. */
  3297. timeout = (delta / 1000000) + 1;
  3298. } else {
  3299. /* Wait for either IO to occur or the next
  3300. timer event. */
  3301. add = qemu_next_deadline();
  3302. /* We advance the timer before checking for IO.
  3303. Limit the amount we advance so that early IO
  3304. activity won't get the guest too far ahead. */
  3305. if (add > 10000000)
  3306. add = 10000000;
  3307. delta += add;
  3308. add = (add + (1 << icount_time_shift) - 1)
  3309. >> icount_time_shift;
  3310. qemu_icount += add;
  3311. timeout = delta / 1000000;
  3312. if (timeout < 0)
  3313. timeout = 0;
  3314. }
  3315. } else {
  3316. timeout = 5000;
  3317. }
  3318. } else {
  3319. timeout = 0;
  3320. }
  3321. } else {
  3322. if (shutdown_requested) {
  3323. ret = EXCP_INTERRUPT;
  3324. break;
  3325. }
  3326. timeout = 5000;
  3327. }
  3328. #ifdef CONFIG_PROFILER
  3329. ti = profile_getclock();
  3330. #endif
  3331. main_loop_wait(timeout);
  3332. #ifdef CONFIG_PROFILER
  3333. dev_time += profile_getclock() - ti;
  3334. #endif
  3335. }
  3336. cpu_disable_ticks();
  3337. return ret;
  3338. }
  3339. static void help(int exitcode)
  3340. {
  3341. /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
  3342. and qemu-doc.texi */
  3343. printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
  3344. "usage: %s [options] [disk_image]\n"
  3345. "\n"
  3346. "'disk_image' is a raw hard image image for IDE hard disk 0\n"
  3347. "\n"
  3348. "Standard options:\n"
  3349. "-h or -help display this help and exit\n"
  3350. "-M machine select emulated machine (-M ? for list)\n"
  3351. "-cpu cpu select CPU (-cpu ? for list)\n"
  3352. "-smp n set the number of CPUs to 'n' [default=1]\n"
  3353. "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
  3354. "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
  3355. "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
  3356. "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
  3357. "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
  3358. " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
  3359. " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
  3360. " use 'file' as a drive image\n"
  3361. "-mtdblock file use 'file' as on-board Flash memory image\n"
  3362. "-sd file use 'file' as SecureDigital card image\n"
  3363. "-pflash file use 'file' as a parallel flash image\n"
  3364. "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
  3365. "-snapshot write to temporary files instead of disk image files\n"
  3366. "-m megs set virtual RAM size to megs MB [default=%d]\n"
  3367. #ifndef _WIN32
  3368. "-k language use keyboard layout (for example \"fr\" for French)\n"
  3369. #endif
  3370. #ifdef HAS_AUDIO
  3371. "-audio-help print list of audio drivers and their options\n"
  3372. "-soundhw c1,... enable audio support\n"
  3373. " and only specified sound cards (comma separated list)\n"
  3374. " use -soundhw ? to get the list of supported cards\n"
  3375. " use -soundhw all to enable all of them\n"
  3376. #endif
  3377. "-usb enable the USB driver (will be the default soon)\n"
  3378. "-usbdevice name add the host or guest USB device 'name'\n"
  3379. "-name string set the name of the guest\n"
  3380. "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
  3381. " specify machine UUID\n"
  3382. "\n"
  3383. "Display options:\n"
  3384. "-nographic disable graphical output and redirect serial I/Os to console\n"
  3385. #ifdef CONFIG_CURSES
  3386. "-curses use a curses/ncurses interface instead of SDL\n"
  3387. #endif
  3388. #ifdef CONFIG_SDL
  3389. "-no-frame open SDL window without a frame and window decorations\n"
  3390. "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
  3391. "-no-quit disable SDL window close capability\n"
  3392. "-sdl enable SDL\n"
  3393. #endif
  3394. "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
  3395. "-vga [std|cirrus|vmware|none]\n"
  3396. " select video card type\n"
  3397. "-full-screen start in full screen\n"
  3398. #if defined(TARGET_PPC) || defined(TARGET_SPARC)
  3399. "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
  3400. #endif
  3401. "-vnc display start a VNC server on display\n"
  3402. "\n"
  3403. "Network options:\n"
  3404. "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
  3405. " create a new Network Interface Card and connect it to VLAN 'n'\n"
  3406. #ifdef CONFIG_SLIRP
  3407. "-net user[,vlan=n][,name=str][,hostname=host]\n"
  3408. " connect the user mode network stack to VLAN 'n' and send\n"
  3409. " hostname 'host' to DHCP clients\n"
  3410. #endif
  3411. #ifdef _WIN32
  3412. "-net tap[,vlan=n][,name=str],ifname=name\n"
  3413. " connect the host TAP network interface to VLAN 'n'\n"
  3414. #else
  3415. "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
  3416. " connect the host TAP network interface to VLAN 'n' and use the\n"
  3417. " network scripts 'file' (default=%s)\n"
  3418. " and 'dfile' (default=%s);\n"
  3419. " use '[down]script=no' to disable script execution;\n"
  3420. " use 'fd=h' to connect to an already opened TAP interface\n"
  3421. #endif
  3422. "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
  3423. " connect the vlan 'n' to another VLAN using a socket connection\n"
  3424. "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
  3425. " connect the vlan 'n' to multicast maddr and port\n"
  3426. #ifdef CONFIG_VDE
  3427. "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
  3428. " connect the vlan 'n' to port 'n' of a vde switch running\n"
  3429. " on host and listening for incoming connections on 'socketpath'.\n"
  3430. " Use group 'groupname' and mode 'octalmode' to change default\n"
  3431. " ownership and permissions for communication port.\n"
  3432. #endif
  3433. "-net none use it alone to have zero network devices; if no -net option\n"
  3434. " is provided, the default is '-net nic -net user'\n"
  3435. #ifdef CONFIG_SLIRP
  3436. "-tftp dir allow tftp access to files in dir [-net user]\n"
  3437. "-bootp file advertise file in BOOTP replies\n"
  3438. #ifndef _WIN32
  3439. "-smb dir allow SMB access to files in 'dir' [-net user]\n"
  3440. #endif
  3441. "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
  3442. " redirect TCP or UDP connections from host to guest [-net user]\n"
  3443. #endif
  3444. "\n"
  3445. "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
  3446. "-bt hci,host[:id]\n"
  3447. " use host's HCI with the given name\n"
  3448. "-bt hci[,vlan=n]\n"
  3449. " emulate a standard HCI in virtual scatternet 'n'\n"
  3450. "-bt vhci[,vlan=n]\n"
  3451. " add host computer to virtual scatternet 'n' using VHCI\n"
  3452. "-bt device:dev[,vlan=n]\n"
  3453. " emulate a bluetooth device 'dev' in scatternet 'n'\n"
  3454. "\n"
  3455. #ifdef TARGET_I386
  3456. "\n"
  3457. "i386 target only:\n"
  3458. "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
  3459. "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
  3460. "-no-fd-bootchk disable boot signature checking for floppy disks\n"
  3461. "-no-acpi disable ACPI\n"
  3462. "-no-hpet disable HPET\n"
  3463. "-acpitable [sig=str][,rev=n][,oem_id=str][,oem_table_id=str][,oem_rev=n][,asl_compiler_id=str][,asl_compiler_rev=n][,data=file1[:file2]...]\n"
  3464. " ACPI table description\n"
  3465. #endif
  3466. "Linux boot specific:\n"
  3467. "-kernel bzImage use 'bzImage' as kernel image\n"
  3468. "-append cmdline use 'cmdline' as kernel command line\n"
  3469. "-initrd file use 'file' as initial ram disk\n"
  3470. "\n"
  3471. "Debug/Expert options:\n"
  3472. "-serial dev redirect the serial port to char device 'dev'\n"
  3473. "-parallel dev redirect the parallel port to char device 'dev'\n"
  3474. "-monitor dev redirect the monitor to char device 'dev'\n"
  3475. "-pidfile file write PID to 'file'\n"
  3476. "-S freeze CPU at startup (use 'c' to start execution)\n"
  3477. "-s wait gdb connection to port\n"
  3478. "-p port set gdb connection port [default=%s]\n"
  3479. "-d item1,... output log to %s (use -d ? for a list of log items)\n"
  3480. "-hdachs c,h,s[,t]\n"
  3481. " force hard disk 0 physical geometry and the optional BIOS\n"
  3482. " translation (t=none or lba) (usually qemu can guess them)\n"
  3483. "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
  3484. "-bios file set the filename for the BIOS\n"
  3485. #ifdef USE_KQEMU
  3486. "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
  3487. "-no-kqemu disable KQEMU kernel module usage\n"
  3488. #endif
  3489. #ifdef CONFIG_KVM
  3490. "-enable-kvm enable KVM full virtualization support\n"
  3491. #endif
  3492. "-no-reboot exit instead of rebooting\n"
  3493. "-no-shutdown stop before shutdown\n"
  3494. "-loadvm [tag|id]\n"
  3495. " start right away with a saved state (loadvm in monitor)\n"
  3496. #ifndef _WIN32
  3497. "-daemonize daemonize QEMU after initializing\n"
  3498. #endif
  3499. "-option-rom rom load a file, rom, into the option ROM space\n"
  3500. #if defined(TARGET_SPARC) || defined(TARGET_PPC)
  3501. "-prom-env variable=value\n"
  3502. " set OpenBIOS nvram variables\n"
  3503. #endif
  3504. "-clock force the use of the given methods for timer alarm.\n"
  3505. " To see what timers are available use -clock ?\n"
  3506. "-localtime set the real time clock to local time [default=utc]\n"
  3507. "-startdate select initial date of the clock\n"
  3508. "-icount [N|auto]\n"
  3509. " enable virtual instruction counter with 2^N clock ticks per instruction\n"
  3510. "-echr chr set terminal escape character instead of ctrl-a\n"
  3511. "-virtioconsole c\n"
  3512. " set virtio console\n"
  3513. "-show-cursor show cursor\n"
  3514. #if defined(TARGET_ARM) || defined(TARGET_M68K)
  3515. "-semihosting semihosting mode\n"
  3516. #endif
  3517. #if defined(TARGET_ARM)
  3518. "-old-param old param mode\n"
  3519. #endif
  3520. "-tb-size n set TB size\n"
  3521. "-incoming p prepare for incoming migration, listen on port p\n"
  3522. #ifndef _WIN32
  3523. "-chroot dir Chroot to dir just before starting the VM.\n"
  3524. "-runas user Change to user id user just before starting the VM.\n"
  3525. #endif
  3526. "\n"
  3527. "During emulation, the following keys are useful:\n"
  3528. "ctrl-alt-f toggle full screen\n"
  3529. "ctrl-alt-n switch to virtual console 'n'\n"
  3530. "ctrl-alt toggle mouse and keyboard grab\n"
  3531. "\n"
  3532. "When using -nographic, press 'ctrl-a h' to get some help.\n"
  3533. ,
  3534. "qemu",
  3535. DEFAULT_RAM_SIZE,
  3536. #ifndef _WIN32
  3537. DEFAULT_NETWORK_SCRIPT,
  3538. DEFAULT_NETWORK_DOWN_SCRIPT,
  3539. #endif
  3540. DEFAULT_GDBSTUB_PORT,
  3541. "/tmp/qemu.log");
  3542. exit(exitcode);
  3543. }
  3544. #define HAS_ARG 0x0001
  3545. enum {
  3546. /* Please keep in synch with help, qemu_options[] and
  3547. qemu-doc.texi */
  3548. /* Standard options: */
  3549. QEMU_OPTION_h,
  3550. QEMU_OPTION_M,
  3551. QEMU_OPTION_cpu,
  3552. QEMU_OPTION_smp,
  3553. QEMU_OPTION_fda,
  3554. QEMU_OPTION_fdb,
  3555. QEMU_OPTION_hda,
  3556. QEMU_OPTION_hdb,
  3557. QEMU_OPTION_hdc,
  3558. QEMU_OPTION_hdd,
  3559. QEMU_OPTION_cdrom,
  3560. QEMU_OPTION_drive,
  3561. QEMU_OPTION_mtdblock,
  3562. QEMU_OPTION_sd,
  3563. QEMU_OPTION_pflash,
  3564. QEMU_OPTION_boot,
  3565. QEMU_OPTION_snapshot,
  3566. QEMU_OPTION_m,
  3567. QEMU_OPTION_k,
  3568. QEMU_OPTION_audio_help,
  3569. QEMU_OPTION_soundhw,
  3570. QEMU_OPTION_usb,
  3571. QEMU_OPTION_usbdevice,
  3572. QEMU_OPTION_name,
  3573. QEMU_OPTION_uuid,
  3574. /* Display options: */
  3575. QEMU_OPTION_nographic,
  3576. QEMU_OPTION_curses,
  3577. QEMU_OPTION_no_frame,
  3578. QEMU_OPTION_alt_grab,
  3579. QEMU_OPTION_no_quit,
  3580. QEMU_OPTION_sdl,
  3581. QEMU_OPTION_portrait,
  3582. QEMU_OPTION_vga,
  3583. QEMU_OPTION_full_screen,
  3584. QEMU_OPTION_g,
  3585. QEMU_OPTION_vnc,
  3586. /* Network options: */
  3587. QEMU_OPTION_net,
  3588. QEMU_OPTION_tftp,
  3589. QEMU_OPTION_bootp,
  3590. QEMU_OPTION_smb,
  3591. QEMU_OPTION_redir,
  3592. QEMU_OPTION_bt,
  3593. /* i386 target only: */
  3594. QEMU_OPTION_win2k_hack,
  3595. QEMU_OPTION_rtc_td_hack,
  3596. QEMU_OPTION_no_fd_bootchk,
  3597. QEMU_OPTION_no_acpi,
  3598. QEMU_OPTION_no_hpet,
  3599. QEMU_OPTION_acpitable,
  3600. /* Linux boot specific: */
  3601. QEMU_OPTION_kernel,
  3602. QEMU_OPTION_append,
  3603. QEMU_OPTION_initrd,
  3604. /* Debug/Expert options: */
  3605. QEMU_OPTION_serial,
  3606. QEMU_OPTION_parallel,
  3607. QEMU_OPTION_monitor,
  3608. QEMU_OPTION_pidfile,
  3609. QEMU_OPTION_S,
  3610. QEMU_OPTION_s,
  3611. QEMU_OPTION_p,
  3612. QEMU_OPTION_d,
  3613. QEMU_OPTION_hdachs,
  3614. QEMU_OPTION_L,
  3615. QEMU_OPTION_bios,
  3616. QEMU_OPTION_kernel_kqemu,
  3617. QEMU_OPTION_no_kqemu,
  3618. QEMU_OPTION_enable_kvm,
  3619. QEMU_OPTION_no_reboot,
  3620. QEMU_OPTION_no_shutdown,
  3621. QEMU_OPTION_loadvm,
  3622. QEMU_OPTION_daemonize,
  3623. QEMU_OPTION_option_rom,
  3624. QEMU_OPTION_prom_env,
  3625. QEMU_OPTION_clock,
  3626. QEMU_OPTION_localtime,
  3627. QEMU_OPTION_startdate,
  3628. QEMU_OPTION_icount,
  3629. QEMU_OPTION_echr,
  3630. QEMU_OPTION_virtiocon,
  3631. QEMU_OPTION_show_cursor,
  3632. QEMU_OPTION_semihosting,
  3633. QEMU_OPTION_old_param,
  3634. QEMU_OPTION_tb_size,
  3635. QEMU_OPTION_incoming,
  3636. QEMU_OPTION_chroot,
  3637. QEMU_OPTION_runas,
  3638. };
  3639. typedef struct QEMUOption {
  3640. const char *name;
  3641. int flags;
  3642. int index;
  3643. } QEMUOption;
  3644. static const QEMUOption qemu_options[] = {
  3645. /* Please keep in synch with help, QEMU_OPTION_ enums, and
  3646. qemu-doc.texi */
  3647. /* Standard options: */
  3648. { "h", 0, QEMU_OPTION_h },
  3649. { "help", 0, QEMU_OPTION_h },
  3650. { "M", HAS_ARG, QEMU_OPTION_M },
  3651. { "cpu", HAS_ARG, QEMU_OPTION_cpu },
  3652. { "smp", HAS_ARG, QEMU_OPTION_smp },
  3653. { "fda", HAS_ARG, QEMU_OPTION_fda },
  3654. { "fdb", HAS_ARG, QEMU_OPTION_fdb },
  3655. { "hda", HAS_ARG, QEMU_OPTION_hda },
  3656. { "hdb", HAS_ARG, QEMU_OPTION_hdb },
  3657. { "hdc", HAS_ARG, QEMU_OPTION_hdc },
  3658. { "hdd", HAS_ARG, QEMU_OPTION_hdd },
  3659. { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
  3660. { "drive", HAS_ARG, QEMU_OPTION_drive },
  3661. { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
  3662. { "sd", HAS_ARG, QEMU_OPTION_sd },
  3663. { "pflash", HAS_ARG, QEMU_OPTION_pflash },
  3664. { "boot", HAS_ARG, QEMU_OPTION_boot },
  3665. { "snapshot", 0, QEMU_OPTION_snapshot },
  3666. { "m", HAS_ARG, QEMU_OPTION_m },
  3667. { "k", HAS_ARG, QEMU_OPTION_k },
  3668. #ifdef HAS_AUDIO
  3669. { "audio-help", 0, QEMU_OPTION_audio_help },
  3670. { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
  3671. #endif
  3672. { "usb", 0, QEMU_OPTION_usb },
  3673. { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
  3674. { "name", HAS_ARG, QEMU_OPTION_name },
  3675. { "uuid", HAS_ARG, QEMU_OPTION_uuid },
  3676. /* Display options: */
  3677. { "nographic", 0, QEMU_OPTION_nographic },
  3678. #ifdef CONFIG_CURSES
  3679. { "curses", 0, QEMU_OPTION_curses },
  3680. #endif
  3681. #ifdef CONFIG_SDL
  3682. { "no-frame", 0, QEMU_OPTION_no_frame },
  3683. { "alt-grab", 0, QEMU_OPTION_alt_grab },
  3684. { "no-quit", 0, QEMU_OPTION_no_quit },
  3685. { "sdl", 0, QEMU_OPTION_sdl },
  3686. #endif
  3687. { "portrait", 0, QEMU_OPTION_portrait },
  3688. { "vga", HAS_ARG, QEMU_OPTION_vga },
  3689. { "full-screen", 0, QEMU_OPTION_full_screen },
  3690. #if defined(TARGET_PPC) || defined(TARGET_SPARC)
  3691. { "g", 1, QEMU_OPTION_g },
  3692. #endif
  3693. { "vnc", HAS_ARG, QEMU_OPTION_vnc },
  3694. /* Network options: */
  3695. { "net", HAS_ARG, QEMU_OPTION_net},
  3696. #ifdef CONFIG_SLIRP
  3697. { "tftp", HAS_ARG, QEMU_OPTION_tftp },
  3698. { "bootp", HAS_ARG, QEMU_OPTION_bootp },
  3699. #ifndef _WIN32
  3700. { "smb", HAS_ARG, QEMU_OPTION_smb },
  3701. #endif
  3702. { "redir", HAS_ARG, QEMU_OPTION_redir },
  3703. #endif
  3704. { "bt", HAS_ARG, QEMU_OPTION_bt },
  3705. #ifdef TARGET_I386
  3706. /* i386 target only: */
  3707. { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
  3708. { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack },
  3709. { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
  3710. { "no-acpi", 0, QEMU_OPTION_no_acpi },
  3711. { "no-hpet", 0, QEMU_OPTION_no_hpet },
  3712. { "acpitable", HAS_ARG, QEMU_OPTION_acpitable },
  3713. #endif
  3714. /* Linux boot specific: */
  3715. { "kernel", HAS_ARG, QEMU_OPTION_kernel },
  3716. { "append", HAS_ARG, QEMU_OPTION_append },
  3717. { "initrd", HAS_ARG, QEMU_OPTION_initrd },
  3718. /* Debug/Expert options: */
  3719. { "serial", HAS_ARG, QEMU_OPTION_serial },
  3720. { "parallel", HAS_ARG, QEMU_OPTION_parallel },
  3721. { "monitor", HAS_ARG, QEMU_OPTION_monitor },
  3722. { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
  3723. { "S", 0, QEMU_OPTION_S },
  3724. { "s", 0, QEMU_OPTION_s },
  3725. { "p", HAS_ARG, QEMU_OPTION_p },
  3726. { "d", HAS_ARG, QEMU_OPTION_d },
  3727. { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
  3728. { "L", HAS_ARG, QEMU_OPTION_L },
  3729. { "bios", HAS_ARG, QEMU_OPTION_bios },
  3730. #ifdef USE_KQEMU
  3731. { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
  3732. { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
  3733. #endif
  3734. #ifdef CONFIG_KVM
  3735. { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
  3736. #endif
  3737. { "no-reboot", 0, QEMU_OPTION_no_reboot },
  3738. { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
  3739. { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
  3740. { "daemonize", 0, QEMU_OPTION_daemonize },
  3741. { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
  3742. #if defined(TARGET_SPARC) || defined(TARGET_PPC)
  3743. { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
  3744. #endif
  3745. { "clock", HAS_ARG, QEMU_OPTION_clock },
  3746. { "localtime", 0, QEMU_OPTION_localtime },
  3747. { "startdate", HAS_ARG, QEMU_OPTION_startdate },
  3748. { "icount", HAS_ARG, QEMU_OPTION_icount },
  3749. { "echr", HAS_ARG, QEMU_OPTION_echr },
  3750. { "virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon },
  3751. { "show-cursor", 0, QEMU_OPTION_show_cursor },
  3752. #if defined(TARGET_ARM) || defined(TARGET_M68K)
  3753. { "semihosting", 0, QEMU_OPTION_semihosting },
  3754. #endif
  3755. #if defined(TARGET_ARM)
  3756. { "old-param", 0, QEMU_OPTION_old_param },
  3757. #endif
  3758. { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
  3759. { "incoming", HAS_ARG, QEMU_OPTION_incoming },
  3760. { "chroot", HAS_ARG, QEMU_OPTION_chroot },
  3761. { "runas", HAS_ARG, QEMU_OPTION_runas },
  3762. { NULL },
  3763. };
  3764. #ifdef HAS_AUDIO
  3765. struct soundhw soundhw[] = {
  3766. #ifdef HAS_AUDIO_CHOICE
  3767. #if defined(TARGET_I386) || defined(TARGET_MIPS)
  3768. {
  3769. "pcspk",
  3770. "PC speaker",
  3771. 0,
  3772. 1,
  3773. { .init_isa = pcspk_audio_init }
  3774. },
  3775. #endif
  3776. #ifdef CONFIG_SB16
  3777. {
  3778. "sb16",
  3779. "Creative Sound Blaster 16",
  3780. 0,
  3781. 1,
  3782. { .init_isa = SB16_init }
  3783. },
  3784. #endif
  3785. #ifdef CONFIG_CS4231A
  3786. {
  3787. "cs4231a",
  3788. "CS4231A",
  3789. 0,
  3790. 1,
  3791. { .init_isa = cs4231a_init }
  3792. },
  3793. #endif
  3794. #ifdef CONFIG_ADLIB
  3795. {
  3796. "adlib",
  3797. #ifdef HAS_YMF262
  3798. "Yamaha YMF262 (OPL3)",
  3799. #else
  3800. "Yamaha YM3812 (OPL2)",
  3801. #endif
  3802. 0,
  3803. 1,
  3804. { .init_isa = Adlib_init }
  3805. },
  3806. #endif
  3807. #ifdef CONFIG_GUS
  3808. {
  3809. "gus",
  3810. "Gravis Ultrasound GF1",
  3811. 0,
  3812. 1,
  3813. { .init_isa = GUS_init }
  3814. },
  3815. #endif
  3816. #ifdef CONFIG_AC97
  3817. {
  3818. "ac97",
  3819. "Intel 82801AA AC97 Audio",
  3820. 0,
  3821. 0,
  3822. { .init_pci = ac97_init }
  3823. },
  3824. #endif
  3825. #ifdef CONFIG_ES1370
  3826. {
  3827. "es1370",
  3828. "ENSONIQ AudioPCI ES1370",
  3829. 0,
  3830. 0,
  3831. { .init_pci = es1370_init }
  3832. },
  3833. #endif
  3834. #endif /* HAS_AUDIO_CHOICE */
  3835. { NULL, NULL, 0, 0, { NULL } }
  3836. };
  3837. static void select_soundhw (const char *optarg)
  3838. {
  3839. struct soundhw *c;
  3840. if (*optarg == '?') {
  3841. show_valid_cards:
  3842. printf ("Valid sound card names (comma separated):\n");
  3843. for (c = soundhw; c->name; ++c) {
  3844. printf ("%-11s %s\n", c->name, c->descr);
  3845. }
  3846. printf ("\n-soundhw all will enable all of the above\n");
  3847. exit (*optarg != '?');
  3848. }
  3849. else {
  3850. size_t l;
  3851. const char *p;
  3852. char *e;
  3853. int bad_card = 0;
  3854. if (!strcmp (optarg, "all")) {
  3855. for (c = soundhw; c->name; ++c) {
  3856. c->enabled = 1;
  3857. }
  3858. return;
  3859. }
  3860. p = optarg;
  3861. while (*p) {
  3862. e = strchr (p, ',');
  3863. l = !e ? strlen (p) : (size_t) (e - p);
  3864. for (c = soundhw; c->name; ++c) {
  3865. if (!strncmp (c->name, p, l)) {
  3866. c->enabled = 1;
  3867. break;
  3868. }
  3869. }
  3870. if (!c->name) {
  3871. if (l > 80) {
  3872. fprintf (stderr,
  3873. "Unknown sound card name (too big to show)\n");
  3874. }
  3875. else {
  3876. fprintf (stderr, "Unknown sound card name `%.*s'\n",
  3877. (int) l, p);
  3878. }
  3879. bad_card = 1;
  3880. }
  3881. p += l + (e != NULL);
  3882. }
  3883. if (bad_card)
  3884. goto show_valid_cards;
  3885. }
  3886. }
  3887. #endif
  3888. static void select_vgahw (const char *p)
  3889. {
  3890. const char *opts;
  3891. if (strstart(p, "std", &opts)) {
  3892. std_vga_enabled = 1;
  3893. cirrus_vga_enabled = 0;
  3894. vmsvga_enabled = 0;
  3895. } else if (strstart(p, "cirrus", &opts)) {
  3896. cirrus_vga_enabled = 1;
  3897. std_vga_enabled = 0;
  3898. vmsvga_enabled = 0;
  3899. } else if (strstart(p, "vmware", &opts)) {
  3900. cirrus_vga_enabled = 0;
  3901. std_vga_enabled = 0;
  3902. vmsvga_enabled = 1;
  3903. } else if (strstart(p, "none", &opts)) {
  3904. cirrus_vga_enabled = 0;
  3905. std_vga_enabled = 0;
  3906. vmsvga_enabled = 0;
  3907. } else {
  3908. invalid_vga:
  3909. fprintf(stderr, "Unknown vga type: %s\n", p);
  3910. exit(1);
  3911. }
  3912. while (*opts) {
  3913. const char *nextopt;
  3914. if (strstart(opts, ",retrace=", &nextopt)) {
  3915. opts = nextopt;
  3916. if (strstart(opts, "dumb", &nextopt))
  3917. vga_retrace_method = VGA_RETRACE_DUMB;
  3918. else if (strstart(opts, "precise", &nextopt))
  3919. vga_retrace_method = VGA_RETRACE_PRECISE;
  3920. else goto invalid_vga;
  3921. } else goto invalid_vga;
  3922. opts = nextopt;
  3923. }
  3924. }
  3925. #ifdef _WIN32
  3926. static BOOL WINAPI qemu_ctrl_handler(DWORD type)
  3927. {
  3928. exit(STATUS_CONTROL_C_EXIT);
  3929. return TRUE;
  3930. }
  3931. #endif
  3932. static int qemu_uuid_parse(const char *str, uint8_t *uuid)
  3933. {
  3934. int ret;
  3935. if(strlen(str) != 36)
  3936. return -1;
  3937. ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
  3938. &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
  3939. &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
  3940. if(ret != 16)
  3941. return -1;
  3942. return 0;
  3943. }
  3944. #define MAX_NET_CLIENTS 32
  3945. #ifndef _WIN32
  3946. static void termsig_handler(int signal)
  3947. {
  3948. qemu_system_shutdown_request();
  3949. }
  3950. static void termsig_setup(void)
  3951. {
  3952. struct sigaction act;
  3953. memset(&act, 0, sizeof(act));
  3954. act.sa_handler = termsig_handler;
  3955. sigaction(SIGINT, &act, NULL);
  3956. sigaction(SIGHUP, &act, NULL);
  3957. sigaction(SIGTERM, &act, NULL);
  3958. }
  3959. #endif
  3960. int main(int argc, char **argv, char **envp)
  3961. {
  3962. #ifdef CONFIG_GDBSTUB
  3963. int use_gdbstub;
  3964. const char *gdbstub_port;
  3965. #endif
  3966. uint32_t boot_devices_bitmap = 0;
  3967. int i;
  3968. int snapshot, linux_boot, net_boot;
  3969. const char *initrd_filename;
  3970. const char *kernel_filename, *kernel_cmdline;
  3971. const char *boot_devices = "";
  3972. DisplayState *ds;
  3973. DisplayChangeListener *dcl;
  3974. int cyls, heads, secs, translation;
  3975. const char *net_clients[MAX_NET_CLIENTS];
  3976. int nb_net_clients;
  3977. const char *bt_opts[MAX_BT_CMDLINE];
  3978. int nb_bt_opts;
  3979. int hda_index;
  3980. int optind;
  3981. const char *r, *optarg;
  3982. CharDriverState *monitor_hd = NULL;
  3983. const char *monitor_device;
  3984. const char *serial_devices[MAX_SERIAL_PORTS];
  3985. int serial_device_index;
  3986. const char *parallel_devices[MAX_PARALLEL_PORTS];
  3987. int parallel_device_index;
  3988. const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
  3989. int virtio_console_index;
  3990. const char *loadvm = NULL;
  3991. QEMUMachine *machine;
  3992. const char *cpu_model;
  3993. const char *usb_devices[MAX_USB_CMDLINE];
  3994. int usb_devices_index;
  3995. int fds[2];
  3996. int tb_size;
  3997. const char *pid_file = NULL;
  3998. const char *incoming = NULL;
  3999. int fd = 0;
  4000. struct passwd *pwd = NULL;
  4001. const char *chroot_dir = NULL;
  4002. const char *run_as = NULL;
  4003. qemu_cache_utils_init(envp);
  4004. LIST_INIT (&vm_change_state_head);
  4005. #ifndef _WIN32
  4006. {
  4007. struct sigaction act;
  4008. sigfillset(&act.sa_mask);
  4009. act.sa_flags = 0;
  4010. act.sa_handler = SIG_IGN;
  4011. sigaction(SIGPIPE, &act, NULL);
  4012. }
  4013. #else
  4014. SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
  4015. /* Note: cpu_interrupt() is currently not SMP safe, so we force
  4016. QEMU to run on a single CPU */
  4017. {
  4018. HANDLE h;
  4019. DWORD mask, smask;
  4020. int i;
  4021. h = GetCurrentProcess();
  4022. if (GetProcessAffinityMask(h, &mask, &smask)) {
  4023. for(i = 0; i < 32; i++) {
  4024. if (mask & (1 << i))
  4025. break;
  4026. }
  4027. if (i != 32) {
  4028. mask = 1 << i;
  4029. SetProcessAffinityMask(h, mask);
  4030. }
  4031. }
  4032. }
  4033. #endif
  4034. register_machines();
  4035. machine = first_machine;
  4036. cpu_model = NULL;
  4037. initrd_filename = NULL;
  4038. ram_size = 0;
  4039. vga_ram_size = VGA_RAM_SIZE;
  4040. #ifdef CONFIG_GDBSTUB
  4041. use_gdbstub = 0;
  4042. gdbstub_port = DEFAULT_GDBSTUB_PORT;
  4043. #endif
  4044. snapshot = 0;
  4045. nographic = 0;
  4046. curses = 0;
  4047. kernel_filename = NULL;
  4048. kernel_cmdline = "";
  4049. cyls = heads = secs = 0;
  4050. translation = BIOS_ATA_TRANSLATION_AUTO;
  4051. monitor_device = "vc:80Cx24C";
  4052. serial_devices[0] = "vc:80Cx24C";
  4053. for(i = 1; i < MAX_SERIAL_PORTS; i++)
  4054. serial_devices[i] = NULL;
  4055. serial_device_index = 0;
  4056. parallel_devices[0] = "vc:80Cx24C";
  4057. for(i = 1; i < MAX_PARALLEL_PORTS; i++)
  4058. parallel_devices[i] = NULL;
  4059. parallel_device_index = 0;
  4060. for(i = 0; i < MAX_VIRTIO_CONSOLES; i++)
  4061. virtio_consoles[i] = NULL;
  4062. virtio_console_index = 0;
  4063. usb_devices_index = 0;
  4064. nb_net_clients = 0;
  4065. nb_bt_opts = 0;
  4066. nb_drives = 0;
  4067. nb_drives_opt = 0;
  4068. hda_index = -1;
  4069. nb_nics = 0;
  4070. tb_size = 0;
  4071. autostart= 1;
  4072. optind = 1;
  4073. for(;;) {
  4074. if (optind >= argc)
  4075. break;
  4076. r = argv[optind];
  4077. if (r[0] != '-') {
  4078. hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
  4079. } else {
  4080. const QEMUOption *popt;
  4081. optind++;
  4082. /* Treat --foo the same as -foo. */
  4083. if (r[1] == '-')
  4084. r++;
  4085. popt = qemu_options;
  4086. for(;;) {
  4087. if (!popt->name) {
  4088. fprintf(stderr, "%s: invalid option -- '%s'\n",
  4089. argv[0], r);
  4090. exit(1);
  4091. }
  4092. if (!strcmp(popt->name, r + 1))
  4093. break;
  4094. popt++;
  4095. }
  4096. if (popt->flags & HAS_ARG) {
  4097. if (optind >= argc) {
  4098. fprintf(stderr, "%s: option '%s' requires an argument\n",
  4099. argv[0], r);
  4100. exit(1);
  4101. }
  4102. optarg = argv[optind++];
  4103. } else {
  4104. optarg = NULL;
  4105. }
  4106. switch(popt->index) {
  4107. case QEMU_OPTION_M:
  4108. machine = find_machine(optarg);
  4109. if (!machine) {
  4110. QEMUMachine *m;
  4111. printf("Supported machines are:\n");
  4112. for(m = first_machine; m != NULL; m = m->next) {
  4113. printf("%-10s %s%s\n",
  4114. m->name, m->desc,
  4115. m == first_machine ? " (default)" : "");
  4116. }
  4117. exit(*optarg != '?');
  4118. }
  4119. break;
  4120. case QEMU_OPTION_cpu:
  4121. /* hw initialization will check this */
  4122. if (*optarg == '?') {
  4123. /* XXX: implement xxx_cpu_list for targets that still miss it */
  4124. #if defined(cpu_list)
  4125. cpu_list(stdout, &fprintf);
  4126. #endif
  4127. exit(0);
  4128. } else {
  4129. cpu_model = optarg;
  4130. }
  4131. break;
  4132. case QEMU_OPTION_initrd:
  4133. initrd_filename = optarg;
  4134. break;
  4135. case QEMU_OPTION_hda:
  4136. if (cyls == 0)
  4137. hda_index = drive_add(optarg, HD_ALIAS, 0);
  4138. else
  4139. hda_index = drive_add(optarg, HD_ALIAS
  4140. ",cyls=%d,heads=%d,secs=%d%s",
  4141. 0, cyls, heads, secs,
  4142. translation == BIOS_ATA_TRANSLATION_LBA ?
  4143. ",trans=lba" :
  4144. translation == BIOS_ATA_TRANSLATION_NONE ?
  4145. ",trans=none" : "");
  4146. break;
  4147. case QEMU_OPTION_hdb:
  4148. case QEMU_OPTION_hdc:
  4149. case QEMU_OPTION_hdd:
  4150. drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
  4151. break;
  4152. case QEMU_OPTION_drive:
  4153. drive_add(NULL, "%s", optarg);
  4154. break;
  4155. case QEMU_OPTION_mtdblock:
  4156. drive_add(optarg, MTD_ALIAS);
  4157. break;
  4158. case QEMU_OPTION_sd:
  4159. drive_add(optarg, SD_ALIAS);
  4160. break;
  4161. case QEMU_OPTION_pflash:
  4162. drive_add(optarg, PFLASH_ALIAS);
  4163. break;
  4164. case QEMU_OPTION_snapshot:
  4165. snapshot = 1;
  4166. break;
  4167. case QEMU_OPTION_hdachs:
  4168. {
  4169. const char *p;
  4170. p = optarg;
  4171. cyls = strtol(p, (char **)&p, 0);
  4172. if (cyls < 1 || cyls > 16383)
  4173. goto chs_fail;
  4174. if (*p != ',')
  4175. goto chs_fail;
  4176. p++;
  4177. heads = strtol(p, (char **)&p, 0);
  4178. if (heads < 1 || heads > 16)
  4179. goto chs_fail;
  4180. if (*p != ',')
  4181. goto chs_fail;
  4182. p++;
  4183. secs = strtol(p, (char **)&p, 0);
  4184. if (secs < 1 || secs > 63)
  4185. goto chs_fail;
  4186. if (*p == ',') {
  4187. p++;
  4188. if (!strcmp(p, "none"))
  4189. translation = BIOS_ATA_TRANSLATION_NONE;
  4190. else if (!strcmp(p, "lba"))
  4191. translation = BIOS_ATA_TRANSLATION_LBA;
  4192. else if (!strcmp(p, "auto"))
  4193. translation = BIOS_ATA_TRANSLATION_AUTO;
  4194. else
  4195. goto chs_fail;
  4196. } else if (*p != '\0') {
  4197. chs_fail:
  4198. fprintf(stderr, "qemu: invalid physical CHS format\n");
  4199. exit(1);
  4200. }
  4201. if (hda_index != -1)
  4202. snprintf(drives_opt[hda_index].opt,
  4203. sizeof(drives_opt[hda_index].opt),
  4204. HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
  4205. 0, cyls, heads, secs,
  4206. translation == BIOS_ATA_TRANSLATION_LBA ?
  4207. ",trans=lba" :
  4208. translation == BIOS_ATA_TRANSLATION_NONE ?
  4209. ",trans=none" : "");
  4210. }
  4211. break;
  4212. case QEMU_OPTION_nographic:
  4213. nographic = 1;
  4214. break;
  4215. #ifdef CONFIG_CURSES
  4216. case QEMU_OPTION_curses:
  4217. curses = 1;
  4218. break;
  4219. #endif
  4220. case QEMU_OPTION_portrait:
  4221. graphic_rotate = 1;
  4222. break;
  4223. case QEMU_OPTION_kernel:
  4224. kernel_filename = optarg;
  4225. break;
  4226. case QEMU_OPTION_append:
  4227. kernel_cmdline = optarg;
  4228. break;
  4229. case QEMU_OPTION_cdrom:
  4230. drive_add(optarg, CDROM_ALIAS);
  4231. break;
  4232. case QEMU_OPTION_boot:
  4233. boot_devices = optarg;
  4234. /* We just do some generic consistency checks */
  4235. {
  4236. /* Could easily be extended to 64 devices if needed */
  4237. const char *p;
  4238. boot_devices_bitmap = 0;
  4239. for (p = boot_devices; *p != '\0'; p++) {
  4240. /* Allowed boot devices are:
  4241. * a b : floppy disk drives
  4242. * c ... f : IDE disk drives
  4243. * g ... m : machine implementation dependant drives
  4244. * n ... p : network devices
  4245. * It's up to each machine implementation to check
  4246. * if the given boot devices match the actual hardware
  4247. * implementation and firmware features.
  4248. */
  4249. if (*p < 'a' || *p > 'q') {
  4250. fprintf(stderr, "Invalid boot device '%c'\n", *p);
  4251. exit(1);
  4252. }
  4253. if (boot_devices_bitmap & (1 << (*p - 'a'))) {
  4254. fprintf(stderr,
  4255. "Boot device '%c' was given twice\n",*p);
  4256. exit(1);
  4257. }
  4258. boot_devices_bitmap |= 1 << (*p - 'a');
  4259. }
  4260. }
  4261. break;
  4262. case QEMU_OPTION_fda:
  4263. case QEMU_OPTION_fdb:
  4264. drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
  4265. break;
  4266. #ifdef TARGET_I386
  4267. case QEMU_OPTION_no_fd_bootchk:
  4268. fd_bootchk = 0;
  4269. break;
  4270. #endif
  4271. case QEMU_OPTION_net:
  4272. if (nb_net_clients >= MAX_NET_CLIENTS) {
  4273. fprintf(stderr, "qemu: too many network clients\n");
  4274. exit(1);
  4275. }
  4276. net_clients[nb_net_clients] = optarg;
  4277. nb_net_clients++;
  4278. break;
  4279. #ifdef CONFIG_SLIRP
  4280. case QEMU_OPTION_tftp:
  4281. tftp_prefix = optarg;
  4282. break;
  4283. case QEMU_OPTION_bootp:
  4284. bootp_filename = optarg;
  4285. break;
  4286. #ifndef _WIN32
  4287. case QEMU_OPTION_smb:
  4288. net_slirp_smb(optarg);
  4289. break;
  4290. #endif
  4291. case QEMU_OPTION_redir:
  4292. net_slirp_redir(optarg);
  4293. break;
  4294. #endif
  4295. case QEMU_OPTION_bt:
  4296. if (nb_bt_opts >= MAX_BT_CMDLINE) {
  4297. fprintf(stderr, "qemu: too many bluetooth options\n");
  4298. exit(1);
  4299. }
  4300. bt_opts[nb_bt_opts++] = optarg;
  4301. break;
  4302. #ifdef HAS_AUDIO
  4303. case QEMU_OPTION_audio_help:
  4304. AUD_help ();
  4305. exit (0);
  4306. break;
  4307. case QEMU_OPTION_soundhw:
  4308. select_soundhw (optarg);
  4309. break;
  4310. #endif
  4311. case QEMU_OPTION_h:
  4312. help(0);
  4313. break;
  4314. case QEMU_OPTION_m: {
  4315. uint64_t value;
  4316. char *ptr;
  4317. value = strtoul(optarg, &ptr, 10);
  4318. switch (*ptr) {
  4319. case 0: case 'M': case 'm':
  4320. value <<= 20;
  4321. break;
  4322. case 'G': case 'g':
  4323. value <<= 30;
  4324. break;
  4325. default:
  4326. fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
  4327. exit(1);
  4328. }
  4329. /* On 32-bit hosts, QEMU is limited by virtual address space */
  4330. if (value > (2047 << 20)
  4331. #ifndef USE_KQEMU
  4332. && HOST_LONG_BITS == 32
  4333. #endif
  4334. ) {
  4335. fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
  4336. exit(1);
  4337. }
  4338. if (value != (uint64_t)(ram_addr_t)value) {
  4339. fprintf(stderr, "qemu: ram size too large\n");
  4340. exit(1);
  4341. }
  4342. ram_size = value;
  4343. break;
  4344. }
  4345. case QEMU_OPTION_d:
  4346. {
  4347. int mask;
  4348. const CPULogItem *item;
  4349. mask = cpu_str_to_log_mask(optarg);
  4350. if (!mask) {
  4351. printf("Log items (comma separated):\n");
  4352. for(item = cpu_log_items; item->mask != 0; item++) {
  4353. printf("%-10s %s\n", item->name, item->help);
  4354. }
  4355. exit(1);
  4356. }
  4357. cpu_set_log(mask);
  4358. }
  4359. break;
  4360. #ifdef CONFIG_GDBSTUB
  4361. case QEMU_OPTION_s:
  4362. use_gdbstub = 1;
  4363. break;
  4364. case QEMU_OPTION_p:
  4365. gdbstub_port = optarg;
  4366. break;
  4367. #endif
  4368. case QEMU_OPTION_L:
  4369. bios_dir = optarg;
  4370. break;
  4371. case QEMU_OPTION_bios:
  4372. bios_name = optarg;
  4373. break;
  4374. case QEMU_OPTION_S:
  4375. autostart = 0;
  4376. break;
  4377. case QEMU_OPTION_k:
  4378. keyboard_layout = optarg;
  4379. break;
  4380. case QEMU_OPTION_localtime:
  4381. rtc_utc = 0;
  4382. break;
  4383. case QEMU_OPTION_vga:
  4384. select_vgahw (optarg);
  4385. break;
  4386. case QEMU_OPTION_g:
  4387. {
  4388. const char *p;
  4389. int w, h, depth;
  4390. p = optarg;
  4391. w = strtol(p, (char **)&p, 10);
  4392. if (w <= 0) {
  4393. graphic_error:
  4394. fprintf(stderr, "qemu: invalid resolution or depth\n");
  4395. exit(1);
  4396. }
  4397. if (*p != 'x')
  4398. goto graphic_error;
  4399. p++;
  4400. h = strtol(p, (char **)&p, 10);
  4401. if (h <= 0)
  4402. goto graphic_error;
  4403. if (*p == 'x') {
  4404. p++;
  4405. depth = strtol(p, (char **)&p, 10);
  4406. if (depth != 8 && depth != 15 && depth != 16 &&
  4407. depth != 24 && depth != 32)
  4408. goto graphic_error;
  4409. } else if (*p == '\0') {
  4410. depth = graphic_depth;
  4411. } else {
  4412. goto graphic_error;
  4413. }
  4414. graphic_width = w;
  4415. graphic_height = h;
  4416. graphic_depth = depth;
  4417. }
  4418. break;
  4419. case QEMU_OPTION_echr:
  4420. {
  4421. char *r;
  4422. term_escape_char = strtol(optarg, &r, 0);
  4423. if (r == optarg)
  4424. printf("Bad argument to echr\n");
  4425. break;
  4426. }
  4427. case QEMU_OPTION_monitor:
  4428. monitor_device = optarg;
  4429. break;
  4430. case QEMU_OPTION_serial:
  4431. if (serial_device_index >= MAX_SERIAL_PORTS) {
  4432. fprintf(stderr, "qemu: too many serial ports\n");
  4433. exit(1);
  4434. }
  4435. serial_devices[serial_device_index] = optarg;
  4436. serial_device_index++;
  4437. break;
  4438. case QEMU_OPTION_virtiocon:
  4439. if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
  4440. fprintf(stderr, "qemu: too many virtio consoles\n");
  4441. exit(1);
  4442. }
  4443. virtio_consoles[virtio_console_index] = optarg;
  4444. virtio_console_index++;
  4445. break;
  4446. case QEMU_OPTION_parallel:
  4447. if (parallel_device_index >= MAX_PARALLEL_PORTS) {
  4448. fprintf(stderr, "qemu: too many parallel ports\n");
  4449. exit(1);
  4450. }
  4451. parallel_devices[parallel_device_index] = optarg;
  4452. parallel_device_index++;
  4453. break;
  4454. case QEMU_OPTION_loadvm:
  4455. loadvm = optarg;
  4456. break;
  4457. case QEMU_OPTION_full_screen:
  4458. full_screen = 1;
  4459. break;
  4460. #ifdef CONFIG_SDL
  4461. case QEMU_OPTION_no_frame:
  4462. no_frame = 1;
  4463. break;
  4464. case QEMU_OPTION_alt_grab:
  4465. alt_grab = 1;
  4466. break;
  4467. case QEMU_OPTION_no_quit:
  4468. no_quit = 1;
  4469. break;
  4470. case QEMU_OPTION_sdl:
  4471. sdl = 1;
  4472. break;
  4473. #endif
  4474. case QEMU_OPTION_pidfile:
  4475. pid_file = optarg;
  4476. break;
  4477. #ifdef TARGET_I386
  4478. case QEMU_OPTION_win2k_hack:
  4479. win2k_install_hack = 1;
  4480. break;
  4481. case QEMU_OPTION_rtc_td_hack:
  4482. rtc_td_hack = 1;
  4483. break;
  4484. case QEMU_OPTION_acpitable:
  4485. if(acpi_table_add(optarg) < 0) {
  4486. fprintf(stderr, "Wrong acpi table provided\n");
  4487. exit(1);
  4488. }
  4489. break;
  4490. #endif
  4491. #ifdef USE_KQEMU
  4492. case QEMU_OPTION_no_kqemu:
  4493. kqemu_allowed = 0;
  4494. break;
  4495. case QEMU_OPTION_kernel_kqemu:
  4496. kqemu_allowed = 2;
  4497. break;
  4498. #endif
  4499. #ifdef CONFIG_KVM
  4500. case QEMU_OPTION_enable_kvm:
  4501. kvm_allowed = 1;
  4502. #ifdef USE_KQEMU
  4503. kqemu_allowed = 0;
  4504. #endif
  4505. break;
  4506. #endif
  4507. case QEMU_OPTION_usb:
  4508. usb_enabled = 1;
  4509. break;
  4510. case QEMU_OPTION_usbdevice:
  4511. usb_enabled = 1;
  4512. if (usb_devices_index >= MAX_USB_CMDLINE) {
  4513. fprintf(stderr, "Too many USB devices\n");
  4514. exit(1);
  4515. }
  4516. usb_devices[usb_devices_index] = optarg;
  4517. usb_devices_index++;
  4518. break;
  4519. case QEMU_OPTION_smp:
  4520. smp_cpus = atoi(optarg);
  4521. if (smp_cpus < 1) {
  4522. fprintf(stderr, "Invalid number of CPUs\n");
  4523. exit(1);
  4524. }
  4525. break;
  4526. case QEMU_OPTION_vnc:
  4527. vnc_display = optarg;
  4528. break;
  4529. case QEMU_OPTION_no_acpi:
  4530. acpi_enabled = 0;
  4531. break;
  4532. case QEMU_OPTION_no_hpet:
  4533. no_hpet = 1;
  4534. break;
  4535. case QEMU_OPTION_no_reboot:
  4536. no_reboot = 1;
  4537. break;
  4538. case QEMU_OPTION_no_shutdown:
  4539. no_shutdown = 1;
  4540. break;
  4541. case QEMU_OPTION_show_cursor:
  4542. cursor_hide = 0;
  4543. break;
  4544. case QEMU_OPTION_uuid:
  4545. if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
  4546. fprintf(stderr, "Fail to parse UUID string."
  4547. " Wrong format.\n");
  4548. exit(1);
  4549. }
  4550. break;
  4551. case QEMU_OPTION_daemonize:
  4552. daemonize = 1;
  4553. break;
  4554. case QEMU_OPTION_option_rom:
  4555. if (nb_option_roms >= MAX_OPTION_ROMS) {
  4556. fprintf(stderr, "Too many option ROMs\n");
  4557. exit(1);
  4558. }
  4559. option_rom[nb_option_roms] = optarg;
  4560. nb_option_roms++;
  4561. break;
  4562. case QEMU_OPTION_semihosting:
  4563. semihosting_enabled = 1;
  4564. break;
  4565. case QEMU_OPTION_name:
  4566. qemu_name = optarg;
  4567. break;
  4568. #if defined(TARGET_SPARC) || defined(TARGET_PPC)
  4569. case QEMU_OPTION_prom_env:
  4570. if (nb_prom_envs >= MAX_PROM_ENVS) {
  4571. fprintf(stderr, "Too many prom variables\n");
  4572. exit(1);
  4573. }
  4574. prom_envs[nb_prom_envs] = optarg;
  4575. nb_prom_envs++;
  4576. break;
  4577. #endif
  4578. #ifdef TARGET_ARM
  4579. case QEMU_OPTION_old_param:
  4580. old_param = 1;
  4581. break;
  4582. #endif
  4583. case QEMU_OPTION_clock:
  4584. configure_alarms(optarg);
  4585. break;
  4586. case QEMU_OPTION_startdate:
  4587. {
  4588. struct tm tm;
  4589. time_t rtc_start_date;
  4590. if (!strcmp(optarg, "now")) {
  4591. rtc_date_offset = -1;
  4592. } else {
  4593. if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
  4594. &tm.tm_year,
  4595. &tm.tm_mon,
  4596. &tm.tm_mday,
  4597. &tm.tm_hour,
  4598. &tm.tm_min,
  4599. &tm.tm_sec) == 6) {
  4600. /* OK */
  4601. } else if (sscanf(optarg, "%d-%d-%d",
  4602. &tm.tm_year,
  4603. &tm.tm_mon,
  4604. &tm.tm_mday) == 3) {
  4605. tm.tm_hour = 0;
  4606. tm.tm_min = 0;
  4607. tm.tm_sec = 0;
  4608. } else {
  4609. goto date_fail;
  4610. }
  4611. tm.tm_year -= 1900;
  4612. tm.tm_mon--;
  4613. rtc_start_date = mktimegm(&tm);
  4614. if (rtc_start_date == -1) {
  4615. date_fail:
  4616. fprintf(stderr, "Invalid date format. Valid format are:\n"
  4617. "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
  4618. exit(1);
  4619. }
  4620. rtc_date_offset = time(NULL) - rtc_start_date;
  4621. }
  4622. }
  4623. break;
  4624. case QEMU_OPTION_tb_size:
  4625. tb_size = strtol(optarg, NULL, 0);
  4626. if (tb_size < 0)
  4627. tb_size = 0;
  4628. break;
  4629. case QEMU_OPTION_icount:
  4630. use_icount = 1;
  4631. if (strcmp(optarg, "auto") == 0) {
  4632. icount_time_shift = -1;
  4633. } else {
  4634. icount_time_shift = strtol(optarg, NULL, 0);
  4635. }
  4636. break;
  4637. case QEMU_OPTION_incoming:
  4638. incoming = optarg;
  4639. break;
  4640. case QEMU_OPTION_chroot:
  4641. chroot_dir = optarg;
  4642. break;
  4643. case QEMU_OPTION_runas:
  4644. run_as = optarg;
  4645. break;
  4646. }
  4647. }
  4648. }
  4649. #if defined(CONFIG_KVM) && defined(USE_KQEMU)
  4650. if (kvm_allowed && kqemu_allowed) {
  4651. fprintf(stderr,
  4652. "You can not enable both KVM and kqemu at the same time\n");
  4653. exit(1);
  4654. }
  4655. #endif
  4656. machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
  4657. if (smp_cpus > machine->max_cpus) {
  4658. fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
  4659. "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
  4660. machine->max_cpus);
  4661. exit(1);
  4662. }
  4663. if (nographic) {
  4664. if (serial_device_index == 0)
  4665. serial_devices[0] = "stdio";
  4666. if (parallel_device_index == 0)
  4667. parallel_devices[0] = "null";
  4668. if (strncmp(monitor_device, "vc", 2) == 0)
  4669. monitor_device = "stdio";
  4670. }
  4671. #ifndef _WIN32
  4672. if (daemonize) {
  4673. pid_t pid;
  4674. if (pipe(fds) == -1)
  4675. exit(1);
  4676. pid = fork();
  4677. if (pid > 0) {
  4678. uint8_t status;
  4679. ssize_t len;
  4680. close(fds[1]);
  4681. again:
  4682. len = read(fds[0], &status, 1);
  4683. if (len == -1 && (errno == EINTR))
  4684. goto again;
  4685. if (len != 1)
  4686. exit(1);
  4687. else if (status == 1) {
  4688. fprintf(stderr, "Could not acquire pidfile\n");
  4689. exit(1);
  4690. } else
  4691. exit(0);
  4692. } else if (pid < 0)
  4693. exit(1);
  4694. setsid();
  4695. pid = fork();
  4696. if (pid > 0)
  4697. exit(0);
  4698. else if (pid < 0)
  4699. exit(1);
  4700. umask(027);
  4701. signal(SIGTSTP, SIG_IGN);
  4702. signal(SIGTTOU, SIG_IGN);
  4703. signal(SIGTTIN, SIG_IGN);
  4704. }
  4705. #endif
  4706. if (pid_file && qemu_create_pidfile(pid_file) != 0) {
  4707. if (daemonize) {
  4708. uint8_t status = 1;
  4709. write(fds[1], &status, 1);
  4710. } else
  4711. fprintf(stderr, "Could not acquire pid file\n");
  4712. exit(1);
  4713. }
  4714. #ifdef USE_KQEMU
  4715. if (smp_cpus > 1)
  4716. kqemu_allowed = 0;
  4717. #endif
  4718. linux_boot = (kernel_filename != NULL);
  4719. net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
  4720. if (!linux_boot && net_boot == 0 &&
  4721. !machine->nodisk_ok && nb_drives_opt == 0)
  4722. help(1);
  4723. if (!linux_boot && *kernel_cmdline != '\0') {
  4724. fprintf(stderr, "-append only allowed with -kernel option\n");
  4725. exit(1);
  4726. }
  4727. if (!linux_boot && initrd_filename != NULL) {
  4728. fprintf(stderr, "-initrd only allowed with -kernel option\n");
  4729. exit(1);
  4730. }
  4731. /* boot to floppy or the default cd if no hard disk defined yet */
  4732. if (!boot_devices[0]) {
  4733. boot_devices = "cad";
  4734. }
  4735. setvbuf(stdout, NULL, _IOLBF, 0);
  4736. init_timers();
  4737. if (init_timer_alarm() < 0) {
  4738. fprintf(stderr, "could not initialize alarm timer\n");
  4739. exit(1);
  4740. }
  4741. if (use_icount && icount_time_shift < 0) {
  4742. use_icount = 2;
  4743. /* 125MIPS seems a reasonable initial guess at the guest speed.
  4744. It will be corrected fairly quickly anyway. */
  4745. icount_time_shift = 3;
  4746. init_icount_adjust();
  4747. }
  4748. #ifdef _WIN32
  4749. socket_init();
  4750. #endif
  4751. /* init network clients */
  4752. if (nb_net_clients == 0) {
  4753. /* if no clients, we use a default config */
  4754. net_clients[nb_net_clients++] = "nic";
  4755. #ifdef CONFIG_SLIRP
  4756. net_clients[nb_net_clients++] = "user";
  4757. #endif
  4758. }
  4759. for(i = 0;i < nb_net_clients; i++) {
  4760. if (net_client_parse(net_clients[i]) < 0)
  4761. exit(1);
  4762. }
  4763. net_client_check();
  4764. #ifdef TARGET_I386
  4765. /* XXX: this should be moved in the PC machine instantiation code */
  4766. if (net_boot != 0) {
  4767. int netroms = 0;
  4768. for (i = 0; i < nb_nics && i < 4; i++) {
  4769. const char *model = nd_table[i].model;
  4770. char buf[1024];
  4771. if (net_boot & (1 << i)) {
  4772. if (model == NULL)
  4773. model = "ne2k_pci";
  4774. snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
  4775. if (get_image_size(buf) > 0) {
  4776. if (nb_option_roms >= MAX_OPTION_ROMS) {
  4777. fprintf(stderr, "Too many option ROMs\n");
  4778. exit(1);
  4779. }
  4780. option_rom[nb_option_roms] = strdup(buf);
  4781. nb_option_roms++;
  4782. netroms++;
  4783. }
  4784. }
  4785. }
  4786. if (netroms == 0) {
  4787. fprintf(stderr, "No valid PXE rom found for network device\n");
  4788. exit(1);
  4789. }
  4790. }
  4791. #endif
  4792. /* init the bluetooth world */
  4793. for (i = 0; i < nb_bt_opts; i++)
  4794. if (bt_parse(bt_opts[i]))
  4795. exit(1);
  4796. /* init the memory */
  4797. phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
  4798. if (machine->ram_require & RAMSIZE_FIXED) {
  4799. if (ram_size > 0) {
  4800. if (ram_size < phys_ram_size) {
  4801. fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
  4802. machine->name, (unsigned long long) phys_ram_size);
  4803. exit(-1);
  4804. }
  4805. phys_ram_size = ram_size;
  4806. } else
  4807. ram_size = phys_ram_size;
  4808. } else {
  4809. if (ram_size == 0)
  4810. ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
  4811. phys_ram_size += ram_size;
  4812. }
  4813. phys_ram_base = qemu_vmalloc(phys_ram_size);
  4814. if (!phys_ram_base) {
  4815. fprintf(stderr, "Could not allocate physical memory\n");
  4816. exit(1);
  4817. }
  4818. /* init the dynamic translator */
  4819. cpu_exec_init_all(tb_size * 1024 * 1024);
  4820. bdrv_init();
  4821. dma_helper_init();
  4822. /* we always create the cdrom drive, even if no disk is there */
  4823. if (nb_drives_opt < MAX_DRIVES)
  4824. drive_add(NULL, CDROM_ALIAS);
  4825. /* we always create at least one floppy */
  4826. if (nb_drives_opt < MAX_DRIVES)
  4827. drive_add(NULL, FD_ALIAS, 0);
  4828. /* we always create one sd slot, even if no card is in it */
  4829. if (nb_drives_opt < MAX_DRIVES)
  4830. drive_add(NULL, SD_ALIAS);
  4831. /* open the virtual block devices */
  4832. for(i = 0; i < nb_drives_opt; i++)
  4833. if (drive_init(&drives_opt[i], snapshot, machine) == -1)
  4834. exit(1);
  4835. register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
  4836. register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
  4837. #ifndef _WIN32
  4838. /* must be after terminal init, SDL library changes signal handlers */
  4839. termsig_setup();
  4840. #endif
  4841. /* Maintain compatibility with multiple stdio monitors */
  4842. if (!strcmp(monitor_device,"stdio")) {
  4843. for (i = 0; i < MAX_SERIAL_PORTS; i++) {
  4844. const char *devname = serial_devices[i];
  4845. if (devname && !strcmp(devname,"mon:stdio")) {
  4846. monitor_device = NULL;
  4847. break;
  4848. } else if (devname && !strcmp(devname,"stdio")) {
  4849. monitor_device = NULL;
  4850. serial_devices[i] = "mon:stdio";
  4851. break;
  4852. }
  4853. }
  4854. }
  4855. if (kvm_enabled()) {
  4856. int ret;
  4857. ret = kvm_init(smp_cpus);
  4858. if (ret < 0) {
  4859. fprintf(stderr, "failed to initialize KVM\n");
  4860. exit(1);
  4861. }
  4862. }
  4863. if (monitor_device) {
  4864. monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
  4865. if (!monitor_hd) {
  4866. fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
  4867. exit(1);
  4868. }
  4869. }
  4870. for(i = 0; i < MAX_SERIAL_PORTS; i++) {
  4871. const char *devname = serial_devices[i];
  4872. if (devname && strcmp(devname, "none")) {
  4873. char label[32];
  4874. snprintf(label, sizeof(label), "serial%d", i);
  4875. serial_hds[i] = qemu_chr_open(label, devname, NULL);
  4876. if (!serial_hds[i]) {
  4877. fprintf(stderr, "qemu: could not open serial device '%s'\n",
  4878. devname);
  4879. exit(1);
  4880. }
  4881. }
  4882. }
  4883. for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
  4884. const char *devname = parallel_devices[i];
  4885. if (devname && strcmp(devname, "none")) {
  4886. char label[32];
  4887. snprintf(label, sizeof(label), "parallel%d", i);
  4888. parallel_hds[i] = qemu_chr_open(label, devname, NULL);
  4889. if (!parallel_hds[i]) {
  4890. fprintf(stderr, "qemu: could not open parallel device '%s'\n",
  4891. devname);
  4892. exit(1);
  4893. }
  4894. }
  4895. }
  4896. for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
  4897. const char *devname = virtio_consoles[i];
  4898. if (devname && strcmp(devname, "none")) {
  4899. char label[32];
  4900. snprintf(label, sizeof(label), "virtcon%d", i);
  4901. virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
  4902. if (!virtcon_hds[i]) {
  4903. fprintf(stderr, "qemu: could not open virtio console '%s'\n",
  4904. devname);
  4905. exit(1);
  4906. }
  4907. }
  4908. }
  4909. machine->init(ram_size, vga_ram_size, boot_devices,
  4910. kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
  4911. current_machine = machine;
  4912. /* Set KVM's vcpu state to qemu's initial CPUState. */
  4913. if (kvm_enabled()) {
  4914. int ret;
  4915. ret = kvm_sync_vcpus();
  4916. if (ret < 0) {
  4917. fprintf(stderr, "failed to initialize vcpus\n");
  4918. exit(1);
  4919. }
  4920. }
  4921. /* init USB devices */
  4922. if (usb_enabled) {
  4923. for(i = 0; i < usb_devices_index; i++) {
  4924. if (usb_device_add(usb_devices[i], 0) < 0) {
  4925. fprintf(stderr, "Warning: could not add USB device %s\n",
  4926. usb_devices[i]);
  4927. }
  4928. }
  4929. }
  4930. if (!display_state)
  4931. dumb_display_init();
  4932. /* just use the first displaystate for the moment */
  4933. ds = display_state;
  4934. /* terminal init */
  4935. if (nographic) {
  4936. if (curses) {
  4937. fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
  4938. exit(1);
  4939. }
  4940. } else {
  4941. #if defined(CONFIG_CURSES)
  4942. if (curses) {
  4943. /* At the moment curses cannot be used with other displays */
  4944. curses_display_init(ds, full_screen);
  4945. } else
  4946. #endif
  4947. {
  4948. if (vnc_display != NULL) {
  4949. vnc_display_init(ds);
  4950. if (vnc_display_open(ds, vnc_display) < 0)
  4951. exit(1);
  4952. }
  4953. #if defined(CONFIG_SDL)
  4954. if (sdl || !vnc_display)
  4955. sdl_display_init(ds, full_screen, no_frame);
  4956. #elif defined(CONFIG_COCOA)
  4957. if (sdl || !vnc_display)
  4958. cocoa_display_init(ds, full_screen);
  4959. #endif
  4960. }
  4961. }
  4962. dpy_resize(ds);
  4963. dcl = ds->listeners;
  4964. while (dcl != NULL) {
  4965. if (dcl->dpy_refresh != NULL) {
  4966. ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
  4967. qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
  4968. }
  4969. dcl = dcl->next;
  4970. }
  4971. if (nographic || (vnc_display && !sdl)) {
  4972. nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
  4973. qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
  4974. }
  4975. text_consoles_set_display(display_state);
  4976. qemu_chr_initial_reset();
  4977. if (monitor_device && monitor_hd)
  4978. monitor_init(monitor_hd, !nographic);
  4979. for(i = 0; i < MAX_SERIAL_PORTS; i++) {
  4980. const char *devname = serial_devices[i];
  4981. if (devname && strcmp(devname, "none")) {
  4982. char label[32];
  4983. snprintf(label, sizeof(label), "serial%d", i);
  4984. if (strstart(devname, "vc", 0))
  4985. qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
  4986. }
  4987. }
  4988. for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
  4989. const char *devname = parallel_devices[i];
  4990. if (devname && strcmp(devname, "none")) {
  4991. char label[32];
  4992. snprintf(label, sizeof(label), "parallel%d", i);
  4993. if (strstart(devname, "vc", 0))
  4994. qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
  4995. }
  4996. }
  4997. for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
  4998. const char *devname = virtio_consoles[i];
  4999. if (virtcon_hds[i] && devname) {
  5000. char label[32];
  5001. snprintf(label, sizeof(label), "virtcon%d", i);
  5002. if (strstart(devname, "vc", 0))
  5003. qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
  5004. }
  5005. }
  5006. #ifdef CONFIG_GDBSTUB
  5007. if (use_gdbstub) {
  5008. /* XXX: use standard host:port notation and modify options
  5009. accordingly. */
  5010. if (gdbserver_start(gdbstub_port) < 0) {
  5011. fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
  5012. gdbstub_port);
  5013. exit(1);
  5014. }
  5015. }
  5016. #endif
  5017. if (loadvm)
  5018. do_loadvm(loadvm);
  5019. if (incoming) {
  5020. autostart = 0;
  5021. qemu_start_incoming_migration(incoming);
  5022. }
  5023. else if (autostart)
  5024. vm_start();
  5025. if (daemonize) {
  5026. uint8_t status = 0;
  5027. ssize_t len;
  5028. again1:
  5029. len = write(fds[1], &status, 1);
  5030. if (len == -1 && (errno == EINTR))
  5031. goto again1;
  5032. if (len != 1)
  5033. exit(1);
  5034. chdir("/");
  5035. TFR(fd = open("/dev/null", O_RDWR));
  5036. if (fd == -1)
  5037. exit(1);
  5038. }
  5039. #ifndef _WIN32
  5040. if (run_as) {
  5041. pwd = getpwnam(run_as);
  5042. if (!pwd) {
  5043. fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
  5044. exit(1);
  5045. }
  5046. }
  5047. if (chroot_dir) {
  5048. if (chroot(chroot_dir) < 0) {
  5049. fprintf(stderr, "chroot failed\n");
  5050. exit(1);
  5051. }
  5052. chdir("/");
  5053. }
  5054. if (run_as) {
  5055. if (setgid(pwd->pw_gid) < 0) {
  5056. fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
  5057. exit(1);
  5058. }
  5059. if (setuid(pwd->pw_uid) < 0) {
  5060. fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
  5061. exit(1);
  5062. }
  5063. if (setuid(0) != -1) {
  5064. fprintf(stderr, "Dropping privileges failed\n");
  5065. exit(1);
  5066. }
  5067. }
  5068. #endif
  5069. if (daemonize) {
  5070. dup2(fd, 0);
  5071. dup2(fd, 1);
  5072. dup2(fd, 2);
  5073. close(fd);
  5074. }
  5075. main_loop();
  5076. quit_timers();
  5077. net_cleanup();
  5078. return 0;
  5079. }