vl.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929
  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 "qemu/osdep.h"
  25. #include "qemu-version.h"
  26. #include "qemu/cutils.h"
  27. #include "qemu/help_option.h"
  28. #include "qemu/uuid.h"
  29. #ifdef CONFIG_SECCOMP
  30. #include "sysemu/seccomp.h"
  31. #include "sys/prctl.h"
  32. #endif
  33. #ifdef CONFIG_SDL
  34. #if defined(__APPLE__) || defined(main)
  35. #include <SDL.h>
  36. int qemu_main(int argc, char **argv, char **envp);
  37. int main(int argc, char **argv)
  38. {
  39. return qemu_main(argc, argv, NULL);
  40. }
  41. #undef main
  42. #define main qemu_main
  43. #endif
  44. #endif /* CONFIG_SDL */
  45. #ifdef CONFIG_COCOA
  46. #undef main
  47. #define main qemu_main
  48. #endif /* CONFIG_COCOA */
  49. #include "qemu/error-report.h"
  50. #include "qemu/sockets.h"
  51. #include "hw/hw.h"
  52. #include "hw/boards.h"
  53. #include "sysemu/accel.h"
  54. #include "hw/usb.h"
  55. #include "hw/i386/pc.h"
  56. #include "hw/isa/isa.h"
  57. #include "hw/scsi/scsi.h"
  58. #include "hw/bt.h"
  59. #include "sysemu/watchdog.h"
  60. #include "hw/smbios/smbios.h"
  61. #include "hw/acpi/acpi.h"
  62. #include "hw/xen/xen.h"
  63. #include "hw/qdev.h"
  64. #include "hw/loader.h"
  65. #include "monitor/qdev.h"
  66. #include "sysemu/bt.h"
  67. #include "net/net.h"
  68. #include "net/slirp.h"
  69. #include "monitor/monitor.h"
  70. #include "ui/console.h"
  71. #include "ui/input.h"
  72. #include "sysemu/sysemu.h"
  73. #include "sysemu/numa.h"
  74. #include "exec/gdbstub.h"
  75. #include "qemu/timer.h"
  76. #include "chardev/char.h"
  77. #include "qemu/bitmap.h"
  78. #include "qemu/log.h"
  79. #include "sysemu/blockdev.h"
  80. #include "hw/block/block.h"
  81. #include "migration/misc.h"
  82. #include "migration/snapshot.h"
  83. #include "migration/global_state.h"
  84. #include "sysemu/tpm.h"
  85. #include "sysemu/dma.h"
  86. #include "hw/audio/soundhw.h"
  87. #include "audio/audio.h"
  88. #include "sysemu/cpus.h"
  89. #include "migration/colo.h"
  90. #include "sysemu/kvm.h"
  91. #include "sysemu/hax.h"
  92. #include "qapi/qobject-input-visitor.h"
  93. #include "qapi/qobject-input-visitor.h"
  94. #include "qapi-visit.h"
  95. #include "qapi/qmp/qjson.h"
  96. #include "qemu/option.h"
  97. #include "qemu/config-file.h"
  98. #include "qemu-options.h"
  99. #include "qmp-commands.h"
  100. #include "qemu/main-loop.h"
  101. #ifdef CONFIG_VIRTFS
  102. #include "fsdev/qemu-fsdev.h"
  103. #endif
  104. #include "sysemu/qtest.h"
  105. #include "disas/disas.h"
  106. #include "slirp/libslirp.h"
  107. #include "trace-root.h"
  108. #include "trace/control.h"
  109. #include "qemu/queue.h"
  110. #include "sysemu/arch_init.h"
  111. #include "ui/qemu-spice.h"
  112. #include "qapi/string-input-visitor.h"
  113. #include "qapi/opts-visitor.h"
  114. #include "qom/object_interfaces.h"
  115. #include "qapi-event.h"
  116. #include "exec/semihost.h"
  117. #include "crypto/init.h"
  118. #include "sysemu/replay.h"
  119. #include "qapi/qmp/qerror.h"
  120. #include "sysemu/iothread.h"
  121. #define MAX_VIRTIO_CONSOLES 1
  122. #define MAX_SCLP_CONSOLES 1
  123. static const char *data_dir[16];
  124. static int data_dir_idx;
  125. const char *bios_name = NULL;
  126. enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
  127. int request_opengl = -1;
  128. int display_opengl;
  129. const char* keyboard_layout = NULL;
  130. ram_addr_t ram_size;
  131. const char *mem_path = NULL;
  132. int mem_prealloc = 0; /* force preallocation of physical target memory */
  133. bool enable_mlock = false;
  134. int nb_nics;
  135. NICInfo nd_table[MAX_NICS];
  136. int autostart;
  137. static int rtc_utc = 1;
  138. static int rtc_date_offset = -1; /* -1 means no change */
  139. QEMUClockType rtc_clock;
  140. int vga_interface_type = VGA_NONE;
  141. static int full_screen = 0;
  142. static int no_frame = 0;
  143. int no_quit = 0;
  144. static bool grab_on_hover;
  145. Chardev *serial_hds[MAX_SERIAL_PORTS];
  146. Chardev *parallel_hds[MAX_PARALLEL_PORTS];
  147. Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
  148. Chardev *sclp_hds[MAX_SCLP_CONSOLES];
  149. int win2k_install_hack = 0;
  150. int singlestep = 0;
  151. int smp_cpus;
  152. unsigned int max_cpus;
  153. int smp_cores = 1;
  154. int smp_threads = 1;
  155. int acpi_enabled = 1;
  156. int no_hpet = 0;
  157. int fd_bootchk = 1;
  158. static int no_reboot;
  159. int no_shutdown = 0;
  160. int cursor_hide = 1;
  161. int graphic_rotate = 0;
  162. const char *watchdog;
  163. QEMUOptionRom option_rom[MAX_OPTION_ROMS];
  164. int nb_option_roms;
  165. int old_param = 0;
  166. const char *qemu_name;
  167. int alt_grab = 0;
  168. int ctrl_grab = 0;
  169. unsigned int nb_prom_envs = 0;
  170. const char *prom_envs[MAX_PROM_ENVS];
  171. int boot_menu;
  172. bool boot_strict;
  173. uint8_t *boot_splash_filedata;
  174. size_t boot_splash_filedata_size;
  175. uint8_t qemu_extra_params_fw[2];
  176. int icount_align_option;
  177. /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
  178. * little-endian "wire format" described in the SMBIOS 2.6 specification.
  179. */
  180. QemuUUID qemu_uuid;
  181. bool qemu_uuid_set;
  182. static NotifierList exit_notifiers =
  183. NOTIFIER_LIST_INITIALIZER(exit_notifiers);
  184. static NotifierList machine_init_done_notifiers =
  185. NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
  186. bool xen_allowed;
  187. uint32_t xen_domid;
  188. enum xen_mode xen_mode = XEN_EMULATE;
  189. bool xen_domid_restrict;
  190. static int has_defaults = 1;
  191. static int default_serial = 1;
  192. static int default_parallel = 1;
  193. static int default_virtcon = 1;
  194. static int default_sclp = 1;
  195. static int default_monitor = 1;
  196. static int default_floppy = 1;
  197. static int default_cdrom = 1;
  198. static int default_sdcard = 1;
  199. static int default_vga = 1;
  200. static int default_net = 1;
  201. static struct {
  202. const char *driver;
  203. int *flag;
  204. } default_list[] = {
  205. { .driver = "isa-serial", .flag = &default_serial },
  206. { .driver = "isa-parallel", .flag = &default_parallel },
  207. { .driver = "isa-fdc", .flag = &default_floppy },
  208. { .driver = "floppy", .flag = &default_floppy },
  209. { .driver = "ide-cd", .flag = &default_cdrom },
  210. { .driver = "ide-hd", .flag = &default_cdrom },
  211. { .driver = "ide-drive", .flag = &default_cdrom },
  212. { .driver = "scsi-cd", .flag = &default_cdrom },
  213. { .driver = "scsi-hd", .flag = &default_cdrom },
  214. { .driver = "virtio-serial-pci", .flag = &default_virtcon },
  215. { .driver = "virtio-serial", .flag = &default_virtcon },
  216. { .driver = "VGA", .flag = &default_vga },
  217. { .driver = "isa-vga", .flag = &default_vga },
  218. { .driver = "cirrus-vga", .flag = &default_vga },
  219. { .driver = "isa-cirrus-vga", .flag = &default_vga },
  220. { .driver = "vmware-svga", .flag = &default_vga },
  221. { .driver = "qxl-vga", .flag = &default_vga },
  222. { .driver = "virtio-vga", .flag = &default_vga },
  223. };
  224. static QemuOptsList qemu_rtc_opts = {
  225. .name = "rtc",
  226. .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
  227. .desc = {
  228. {
  229. .name = "base",
  230. .type = QEMU_OPT_STRING,
  231. },{
  232. .name = "clock",
  233. .type = QEMU_OPT_STRING,
  234. },{
  235. .name = "driftfix",
  236. .type = QEMU_OPT_STRING,
  237. },
  238. { /* end of list */ }
  239. },
  240. };
  241. static QemuOptsList qemu_sandbox_opts = {
  242. .name = "sandbox",
  243. .implied_opt_name = "enable",
  244. .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
  245. .desc = {
  246. {
  247. .name = "enable",
  248. .type = QEMU_OPT_BOOL,
  249. },
  250. {
  251. .name = "obsolete",
  252. .type = QEMU_OPT_STRING,
  253. },
  254. {
  255. .name = "elevateprivileges",
  256. .type = QEMU_OPT_STRING,
  257. },
  258. {
  259. .name = "spawn",
  260. .type = QEMU_OPT_STRING,
  261. },
  262. {
  263. .name = "resourcecontrol",
  264. .type = QEMU_OPT_STRING,
  265. },
  266. { /* end of list */ }
  267. },
  268. };
  269. static QemuOptsList qemu_option_rom_opts = {
  270. .name = "option-rom",
  271. .implied_opt_name = "romfile",
  272. .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
  273. .desc = {
  274. {
  275. .name = "bootindex",
  276. .type = QEMU_OPT_NUMBER,
  277. }, {
  278. .name = "romfile",
  279. .type = QEMU_OPT_STRING,
  280. },
  281. { /* end of list */ }
  282. },
  283. };
  284. static QemuOptsList qemu_machine_opts = {
  285. .name = "machine",
  286. .implied_opt_name = "type",
  287. .merge_lists = true,
  288. .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
  289. .desc = {
  290. /*
  291. * no elements => accept any
  292. * sanity checking will happen later
  293. * when setting machine properties
  294. */
  295. { }
  296. },
  297. };
  298. static QemuOptsList qemu_accel_opts = {
  299. .name = "accel",
  300. .implied_opt_name = "accel",
  301. .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
  302. .merge_lists = true,
  303. .desc = {
  304. {
  305. .name = "accel",
  306. .type = QEMU_OPT_STRING,
  307. .help = "Select the type of accelerator",
  308. },
  309. {
  310. .name = "thread",
  311. .type = QEMU_OPT_STRING,
  312. .help = "Enable/disable multi-threaded TCG",
  313. },
  314. { /* end of list */ }
  315. },
  316. };
  317. static QemuOptsList qemu_boot_opts = {
  318. .name = "boot-opts",
  319. .implied_opt_name = "order",
  320. .merge_lists = true,
  321. .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
  322. .desc = {
  323. {
  324. .name = "order",
  325. .type = QEMU_OPT_STRING,
  326. }, {
  327. .name = "once",
  328. .type = QEMU_OPT_STRING,
  329. }, {
  330. .name = "menu",
  331. .type = QEMU_OPT_BOOL,
  332. }, {
  333. .name = "splash",
  334. .type = QEMU_OPT_STRING,
  335. }, {
  336. .name = "splash-time",
  337. .type = QEMU_OPT_STRING,
  338. }, {
  339. .name = "reboot-timeout",
  340. .type = QEMU_OPT_STRING,
  341. }, {
  342. .name = "strict",
  343. .type = QEMU_OPT_BOOL,
  344. },
  345. { /*End of list */ }
  346. },
  347. };
  348. static QemuOptsList qemu_add_fd_opts = {
  349. .name = "add-fd",
  350. .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
  351. .desc = {
  352. {
  353. .name = "fd",
  354. .type = QEMU_OPT_NUMBER,
  355. .help = "file descriptor of which a duplicate is added to fd set",
  356. },{
  357. .name = "set",
  358. .type = QEMU_OPT_NUMBER,
  359. .help = "ID of the fd set to add fd to",
  360. },{
  361. .name = "opaque",
  362. .type = QEMU_OPT_STRING,
  363. .help = "free-form string used to describe fd",
  364. },
  365. { /* end of list */ }
  366. },
  367. };
  368. static QemuOptsList qemu_object_opts = {
  369. .name = "object",
  370. .implied_opt_name = "qom-type",
  371. .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
  372. .desc = {
  373. { }
  374. },
  375. };
  376. static QemuOptsList qemu_tpmdev_opts = {
  377. .name = "tpmdev",
  378. .implied_opt_name = "type",
  379. .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
  380. .desc = {
  381. /* options are defined in the TPM backends */
  382. { /* end of list */ }
  383. },
  384. };
  385. static QemuOptsList qemu_realtime_opts = {
  386. .name = "realtime",
  387. .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
  388. .desc = {
  389. {
  390. .name = "mlock",
  391. .type = QEMU_OPT_BOOL,
  392. },
  393. { /* end of list */ }
  394. },
  395. };
  396. static QemuOptsList qemu_msg_opts = {
  397. .name = "msg",
  398. .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
  399. .desc = {
  400. {
  401. .name = "timestamp",
  402. .type = QEMU_OPT_BOOL,
  403. },
  404. { /* end of list */ }
  405. },
  406. };
  407. static QemuOptsList qemu_name_opts = {
  408. .name = "name",
  409. .implied_opt_name = "guest",
  410. .merge_lists = true,
  411. .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
  412. .desc = {
  413. {
  414. .name = "guest",
  415. .type = QEMU_OPT_STRING,
  416. .help = "Sets the name of the guest.\n"
  417. "This name will be displayed in the SDL window caption.\n"
  418. "The name will also be used for the VNC server",
  419. }, {
  420. .name = "process",
  421. .type = QEMU_OPT_STRING,
  422. .help = "Sets the name of the QEMU process, as shown in top etc",
  423. }, {
  424. .name = "debug-threads",
  425. .type = QEMU_OPT_BOOL,
  426. .help = "When enabled, name the individual threads; defaults off.\n"
  427. "NOTE: The thread names are for debugging and not a\n"
  428. "stable API.",
  429. },
  430. { /* End of list */ }
  431. },
  432. };
  433. static QemuOptsList qemu_mem_opts = {
  434. .name = "memory",
  435. .implied_opt_name = "size",
  436. .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
  437. .merge_lists = true,
  438. .desc = {
  439. {
  440. .name = "size",
  441. .type = QEMU_OPT_SIZE,
  442. },
  443. {
  444. .name = "slots",
  445. .type = QEMU_OPT_NUMBER,
  446. },
  447. {
  448. .name = "maxmem",
  449. .type = QEMU_OPT_SIZE,
  450. },
  451. { /* end of list */ }
  452. },
  453. };
  454. static QemuOptsList qemu_icount_opts = {
  455. .name = "icount",
  456. .implied_opt_name = "shift",
  457. .merge_lists = true,
  458. .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
  459. .desc = {
  460. {
  461. .name = "shift",
  462. .type = QEMU_OPT_STRING,
  463. }, {
  464. .name = "align",
  465. .type = QEMU_OPT_BOOL,
  466. }, {
  467. .name = "sleep",
  468. .type = QEMU_OPT_BOOL,
  469. }, {
  470. .name = "rr",
  471. .type = QEMU_OPT_STRING,
  472. }, {
  473. .name = "rrfile",
  474. .type = QEMU_OPT_STRING,
  475. }, {
  476. .name = "rrsnapshot",
  477. .type = QEMU_OPT_STRING,
  478. },
  479. { /* end of list */ }
  480. },
  481. };
  482. static QemuOptsList qemu_semihosting_config_opts = {
  483. .name = "semihosting-config",
  484. .implied_opt_name = "enable",
  485. .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
  486. .desc = {
  487. {
  488. .name = "enable",
  489. .type = QEMU_OPT_BOOL,
  490. }, {
  491. .name = "target",
  492. .type = QEMU_OPT_STRING,
  493. }, {
  494. .name = "arg",
  495. .type = QEMU_OPT_STRING,
  496. },
  497. { /* end of list */ }
  498. },
  499. };
  500. static QemuOptsList qemu_fw_cfg_opts = {
  501. .name = "fw_cfg",
  502. .implied_opt_name = "name",
  503. .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
  504. .desc = {
  505. {
  506. .name = "name",
  507. .type = QEMU_OPT_STRING,
  508. .help = "Sets the fw_cfg name of the blob to be inserted",
  509. }, {
  510. .name = "file",
  511. .type = QEMU_OPT_STRING,
  512. .help = "Sets the name of the file from which\n"
  513. "the fw_cfg blob will be loaded",
  514. }, {
  515. .name = "string",
  516. .type = QEMU_OPT_STRING,
  517. .help = "Sets content of the blob to be inserted from a string",
  518. },
  519. { /* end of list */ }
  520. },
  521. };
  522. /**
  523. * Get machine options
  524. *
  525. * Returns: machine options (never null).
  526. */
  527. QemuOpts *qemu_get_machine_opts(void)
  528. {
  529. return qemu_find_opts_singleton("machine");
  530. }
  531. const char *qemu_get_vm_name(void)
  532. {
  533. return qemu_name;
  534. }
  535. static void res_free(void)
  536. {
  537. g_free(boot_splash_filedata);
  538. boot_splash_filedata = NULL;
  539. }
  540. static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
  541. {
  542. const char *driver = qemu_opt_get(opts, "driver");
  543. int i;
  544. if (!driver)
  545. return 0;
  546. for (i = 0; i < ARRAY_SIZE(default_list); i++) {
  547. if (strcmp(default_list[i].driver, driver) != 0)
  548. continue;
  549. *(default_list[i].flag) = 0;
  550. }
  551. return 0;
  552. }
  553. /***********************************************************/
  554. /* QEMU state */
  555. static RunState current_run_state = RUN_STATE_PRELAUNCH;
  556. /* We use RUN_STATE__MAX but any invalid value will do */
  557. static RunState vmstop_requested = RUN_STATE__MAX;
  558. static QemuMutex vmstop_lock;
  559. typedef struct {
  560. RunState from;
  561. RunState to;
  562. } RunStateTransition;
  563. static const RunStateTransition runstate_transitions_def[] = {
  564. /* from -> to */
  565. { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
  566. { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
  567. { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
  568. { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
  569. { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
  570. { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
  571. { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
  572. { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
  573. { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
  574. { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
  575. { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
  576. { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
  577. { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
  578. { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
  579. { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
  580. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
  581. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
  582. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
  583. { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
  584. { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
  585. { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
  586. { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
  587. { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
  588. { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
  589. { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
  590. { RUN_STATE_PAUSED, RUN_STATE_COLO},
  591. { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
  592. { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
  593. { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
  594. { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
  595. { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
  596. { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
  597. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
  598. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
  599. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
  600. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
  601. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
  602. { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
  603. { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
  604. { RUN_STATE_COLO, RUN_STATE_RUNNING },
  605. { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
  606. { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
  607. { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
  608. { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
  609. { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
  610. { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
  611. { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
  612. { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
  613. { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
  614. { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
  615. { RUN_STATE_RUNNING, RUN_STATE_COLO},
  616. { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
  617. { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
  618. { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
  619. { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
  620. { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
  621. { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
  622. { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
  623. { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
  624. { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
  625. { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
  626. { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
  627. { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
  628. { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
  629. { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
  630. { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
  631. { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
  632. { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
  633. { RUN_STATE__MAX, RUN_STATE__MAX },
  634. };
  635. static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
  636. bool runstate_check(RunState state)
  637. {
  638. return current_run_state == state;
  639. }
  640. bool runstate_store(char *str, size_t size)
  641. {
  642. const char *state = RunState_str(current_run_state);
  643. size_t len = strlen(state) + 1;
  644. if (len > size) {
  645. return false;
  646. }
  647. memcpy(str, state, len);
  648. return true;
  649. }
  650. static void runstate_init(void)
  651. {
  652. const RunStateTransition *p;
  653. memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
  654. for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
  655. runstate_valid_transitions[p->from][p->to] = true;
  656. }
  657. qemu_mutex_init(&vmstop_lock);
  658. }
  659. /* This function will abort() on invalid state transitions */
  660. void runstate_set(RunState new_state)
  661. {
  662. assert(new_state < RUN_STATE__MAX);
  663. if (current_run_state == new_state) {
  664. return;
  665. }
  666. if (!runstate_valid_transitions[current_run_state][new_state]) {
  667. error_report("invalid runstate transition: '%s' -> '%s'",
  668. RunState_str(current_run_state),
  669. RunState_str(new_state));
  670. abort();
  671. }
  672. trace_runstate_set(new_state);
  673. current_run_state = new_state;
  674. }
  675. int runstate_is_running(void)
  676. {
  677. return runstate_check(RUN_STATE_RUNNING);
  678. }
  679. bool runstate_needs_reset(void)
  680. {
  681. return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
  682. runstate_check(RUN_STATE_SHUTDOWN);
  683. }
  684. StatusInfo *qmp_query_status(Error **errp)
  685. {
  686. StatusInfo *info = g_malloc0(sizeof(*info));
  687. info->running = runstate_is_running();
  688. info->singlestep = singlestep;
  689. info->status = current_run_state;
  690. return info;
  691. }
  692. bool qemu_vmstop_requested(RunState *r)
  693. {
  694. qemu_mutex_lock(&vmstop_lock);
  695. *r = vmstop_requested;
  696. vmstop_requested = RUN_STATE__MAX;
  697. qemu_mutex_unlock(&vmstop_lock);
  698. return *r < RUN_STATE__MAX;
  699. }
  700. void qemu_system_vmstop_request_prepare(void)
  701. {
  702. qemu_mutex_lock(&vmstop_lock);
  703. }
  704. void qemu_system_vmstop_request(RunState state)
  705. {
  706. vmstop_requested = state;
  707. qemu_mutex_unlock(&vmstop_lock);
  708. qemu_notify_event();
  709. }
  710. /***********************************************************/
  711. /* real time host monotonic timer */
  712. static time_t qemu_time(void)
  713. {
  714. return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
  715. }
  716. /***********************************************************/
  717. /* host time/date access */
  718. void qemu_get_timedate(struct tm *tm, int offset)
  719. {
  720. time_t ti = qemu_time();
  721. ti += offset;
  722. if (rtc_date_offset == -1) {
  723. if (rtc_utc)
  724. gmtime_r(&ti, tm);
  725. else
  726. localtime_r(&ti, tm);
  727. } else {
  728. ti -= rtc_date_offset;
  729. gmtime_r(&ti, tm);
  730. }
  731. }
  732. int qemu_timedate_diff(struct tm *tm)
  733. {
  734. time_t seconds;
  735. if (rtc_date_offset == -1)
  736. if (rtc_utc)
  737. seconds = mktimegm(tm);
  738. else {
  739. struct tm tmp = *tm;
  740. tmp.tm_isdst = -1; /* use timezone to figure it out */
  741. seconds = mktime(&tmp);
  742. }
  743. else
  744. seconds = mktimegm(tm) + rtc_date_offset;
  745. return seconds - qemu_time();
  746. }
  747. static void configure_rtc_date_offset(const char *startdate, int legacy)
  748. {
  749. time_t rtc_start_date;
  750. struct tm tm;
  751. if (!strcmp(startdate, "now") && legacy) {
  752. rtc_date_offset = -1;
  753. } else {
  754. if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
  755. &tm.tm_year,
  756. &tm.tm_mon,
  757. &tm.tm_mday,
  758. &tm.tm_hour,
  759. &tm.tm_min,
  760. &tm.tm_sec) == 6) {
  761. /* OK */
  762. } else if (sscanf(startdate, "%d-%d-%d",
  763. &tm.tm_year,
  764. &tm.tm_mon,
  765. &tm.tm_mday) == 3) {
  766. tm.tm_hour = 0;
  767. tm.tm_min = 0;
  768. tm.tm_sec = 0;
  769. } else {
  770. goto date_fail;
  771. }
  772. tm.tm_year -= 1900;
  773. tm.tm_mon--;
  774. rtc_start_date = mktimegm(&tm);
  775. if (rtc_start_date == -1) {
  776. date_fail:
  777. error_report("invalid date format");
  778. error_printf("valid formats: "
  779. "'2006-06-17T16:01:21' or '2006-06-17'\n");
  780. exit(1);
  781. }
  782. rtc_date_offset = qemu_time() - rtc_start_date;
  783. }
  784. }
  785. static void configure_rtc(QemuOpts *opts)
  786. {
  787. const char *value;
  788. value = qemu_opt_get(opts, "base");
  789. if (value) {
  790. if (!strcmp(value, "utc")) {
  791. rtc_utc = 1;
  792. } else if (!strcmp(value, "localtime")) {
  793. Error *blocker = NULL;
  794. rtc_utc = 0;
  795. error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
  796. "-rtc base=localtime");
  797. replay_add_blocker(blocker);
  798. } else {
  799. configure_rtc_date_offset(value, 0);
  800. }
  801. }
  802. value = qemu_opt_get(opts, "clock");
  803. if (value) {
  804. if (!strcmp(value, "host")) {
  805. rtc_clock = QEMU_CLOCK_HOST;
  806. } else if (!strcmp(value, "rt")) {
  807. rtc_clock = QEMU_CLOCK_REALTIME;
  808. } else if (!strcmp(value, "vm")) {
  809. rtc_clock = QEMU_CLOCK_VIRTUAL;
  810. } else {
  811. error_report("invalid option value '%s'", value);
  812. exit(1);
  813. }
  814. }
  815. value = qemu_opt_get(opts, "driftfix");
  816. if (value) {
  817. if (!strcmp(value, "slew")) {
  818. static GlobalProperty slew_lost_ticks = {
  819. .driver = "mc146818rtc",
  820. .property = "lost_tick_policy",
  821. .value = "slew",
  822. };
  823. qdev_prop_register_global(&slew_lost_ticks);
  824. } else if (!strcmp(value, "none")) {
  825. /* discard is default */
  826. } else {
  827. error_report("invalid option value '%s'", value);
  828. exit(1);
  829. }
  830. }
  831. }
  832. /***********************************************************/
  833. /* Bluetooth support */
  834. static int nb_hcis;
  835. static int cur_hci;
  836. static struct HCIInfo *hci_table[MAX_NICS];
  837. struct HCIInfo *qemu_next_hci(void)
  838. {
  839. if (cur_hci == nb_hcis)
  840. return &null_hci;
  841. return hci_table[cur_hci++];
  842. }
  843. static int bt_hci_parse(const char *str)
  844. {
  845. struct HCIInfo *hci;
  846. bdaddr_t bdaddr;
  847. if (nb_hcis >= MAX_NICS) {
  848. error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
  849. return -1;
  850. }
  851. hci = hci_init(str);
  852. if (!hci)
  853. return -1;
  854. bdaddr.b[0] = 0x52;
  855. bdaddr.b[1] = 0x54;
  856. bdaddr.b[2] = 0x00;
  857. bdaddr.b[3] = 0x12;
  858. bdaddr.b[4] = 0x34;
  859. bdaddr.b[5] = 0x56 + nb_hcis;
  860. hci->bdaddr_set(hci, bdaddr.b);
  861. hci_table[nb_hcis++] = hci;
  862. return 0;
  863. }
  864. static void bt_vhci_add(int vlan_id)
  865. {
  866. struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
  867. if (!vlan->slave)
  868. warn_report("adding a VHCI to an empty scatternet %i",
  869. vlan_id);
  870. bt_vhci_init(bt_new_hci(vlan));
  871. }
  872. static struct bt_device_s *bt_device_add(const char *opt)
  873. {
  874. struct bt_scatternet_s *vlan;
  875. int vlan_id = 0;
  876. char *endp = strstr(opt, ",vlan=");
  877. int len = (endp ? endp - opt : strlen(opt)) + 1;
  878. char devname[10];
  879. pstrcpy(devname, MIN(sizeof(devname), len), opt);
  880. if (endp) {
  881. vlan_id = strtol(endp + 6, &endp, 0);
  882. if (*endp) {
  883. error_report("unrecognised bluetooth vlan Id");
  884. return 0;
  885. }
  886. }
  887. vlan = qemu_find_bt_vlan(vlan_id);
  888. if (!vlan->slave)
  889. warn_report("adding a slave device to an empty scatternet %i",
  890. vlan_id);
  891. if (!strcmp(devname, "keyboard"))
  892. return bt_keyboard_init(vlan);
  893. error_report("unsupported bluetooth device '%s'", devname);
  894. return 0;
  895. }
  896. static int bt_parse(const char *opt)
  897. {
  898. const char *endp, *p;
  899. int vlan;
  900. if (strstart(opt, "hci", &endp)) {
  901. if (!*endp || *endp == ',') {
  902. if (*endp)
  903. if (!strstart(endp, ",vlan=", 0))
  904. opt = endp + 1;
  905. return bt_hci_parse(opt);
  906. }
  907. } else if (strstart(opt, "vhci", &endp)) {
  908. if (!*endp || *endp == ',') {
  909. if (*endp) {
  910. if (strstart(endp, ",vlan=", &p)) {
  911. vlan = strtol(p, (char **) &endp, 0);
  912. if (*endp) {
  913. error_report("bad scatternet '%s'", p);
  914. return 1;
  915. }
  916. } else {
  917. error_report("bad parameter '%s'", endp + 1);
  918. return 1;
  919. }
  920. } else
  921. vlan = 0;
  922. bt_vhci_add(vlan);
  923. return 0;
  924. }
  925. } else if (strstart(opt, "device:", &endp))
  926. return !bt_device_add(endp);
  927. error_report("bad bluetooth parameter '%s'", opt);
  928. return 1;
  929. }
  930. static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
  931. {
  932. if (qemu_opt_get_bool(opts, "enable", false)) {
  933. #ifdef CONFIG_SECCOMP
  934. uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
  935. | QEMU_SECCOMP_SET_OBSOLETE;
  936. const char *value = NULL;
  937. value = qemu_opt_get(opts, "obsolete");
  938. if (value) {
  939. if (g_str_equal(value, "allow")) {
  940. seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
  941. } else if (g_str_equal(value, "deny")) {
  942. /* this is the default option, this if is here
  943. * to provide a little bit of consistency for
  944. * the command line */
  945. } else {
  946. error_report("invalid argument for obsolete");
  947. return -1;
  948. }
  949. }
  950. value = qemu_opt_get(opts, "elevateprivileges");
  951. if (value) {
  952. if (g_str_equal(value, "deny")) {
  953. seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
  954. } else if (g_str_equal(value, "children")) {
  955. seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
  956. /* calling prctl directly because we're
  957. * not sure if host has CAP_SYS_ADMIN set*/
  958. if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
  959. error_report("failed to set no_new_privs "
  960. "aborting");
  961. return -1;
  962. }
  963. } else if (g_str_equal(value, "allow")) {
  964. /* default value */
  965. } else {
  966. error_report("invalid argument for elevateprivileges");
  967. return -1;
  968. }
  969. }
  970. value = qemu_opt_get(opts, "spawn");
  971. if (value) {
  972. if (g_str_equal(value, "deny")) {
  973. seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
  974. } else if (g_str_equal(value, "allow")) {
  975. /* default value */
  976. } else {
  977. error_report("invalid argument for spawn");
  978. return -1;
  979. }
  980. }
  981. value = qemu_opt_get(opts, "resourcecontrol");
  982. if (value) {
  983. if (g_str_equal(value, "deny")) {
  984. seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
  985. } else if (g_str_equal(value, "allow")) {
  986. /* default value */
  987. } else {
  988. error_report("invalid argument for resourcecontrol");
  989. return -1;
  990. }
  991. }
  992. if (seccomp_start(seccomp_opts) < 0) {
  993. error_report("failed to install seccomp syscall filter "
  994. "in the kernel");
  995. return -1;
  996. }
  997. #else
  998. error_report("seccomp support is disabled");
  999. return -1;
  1000. #endif
  1001. }
  1002. return 0;
  1003. }
  1004. static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
  1005. {
  1006. const char *proc_name;
  1007. if (qemu_opt_get(opts, "debug-threads")) {
  1008. qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
  1009. }
  1010. qemu_name = qemu_opt_get(opts, "guest");
  1011. proc_name = qemu_opt_get(opts, "process");
  1012. if (proc_name) {
  1013. os_set_proc_name(proc_name);
  1014. }
  1015. return 0;
  1016. }
  1017. bool defaults_enabled(void)
  1018. {
  1019. return has_defaults;
  1020. }
  1021. #ifndef _WIN32
  1022. static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
  1023. {
  1024. int fd, dupfd, flags;
  1025. int64_t fdset_id;
  1026. const char *fd_opaque = NULL;
  1027. AddfdInfo *fdinfo;
  1028. fd = qemu_opt_get_number(opts, "fd", -1);
  1029. fdset_id = qemu_opt_get_number(opts, "set", -1);
  1030. fd_opaque = qemu_opt_get(opts, "opaque");
  1031. if (fd < 0) {
  1032. error_report("fd option is required and must be non-negative");
  1033. return -1;
  1034. }
  1035. if (fd <= STDERR_FILENO) {
  1036. error_report("fd cannot be a standard I/O stream");
  1037. return -1;
  1038. }
  1039. /*
  1040. * All fds inherited across exec() necessarily have FD_CLOEXEC
  1041. * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
  1042. */
  1043. flags = fcntl(fd, F_GETFD);
  1044. if (flags == -1 || (flags & FD_CLOEXEC)) {
  1045. error_report("fd is not valid or already in use");
  1046. return -1;
  1047. }
  1048. if (fdset_id < 0) {
  1049. error_report("set option is required and must be non-negative");
  1050. return -1;
  1051. }
  1052. #ifdef F_DUPFD_CLOEXEC
  1053. dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  1054. #else
  1055. dupfd = dup(fd);
  1056. if (dupfd != -1) {
  1057. qemu_set_cloexec(dupfd);
  1058. }
  1059. #endif
  1060. if (dupfd == -1) {
  1061. error_report("error duplicating fd: %s", strerror(errno));
  1062. return -1;
  1063. }
  1064. /* add the duplicate fd, and optionally the opaque string, to the fd set */
  1065. fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
  1066. &error_abort);
  1067. g_free(fdinfo);
  1068. return 0;
  1069. }
  1070. static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
  1071. {
  1072. int fd;
  1073. fd = qemu_opt_get_number(opts, "fd", -1);
  1074. close(fd);
  1075. return 0;
  1076. }
  1077. #endif
  1078. /***********************************************************/
  1079. /* QEMU Block devices */
  1080. #define HD_OPTS "media=disk"
  1081. #define CDROM_OPTS "media=cdrom"
  1082. #define FD_OPTS ""
  1083. #define PFLASH_OPTS ""
  1084. #define MTD_OPTS ""
  1085. #define SD_OPTS ""
  1086. static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
  1087. {
  1088. BlockInterfaceType *block_default_type = opaque;
  1089. return drive_new(opts, *block_default_type) == NULL;
  1090. }
  1091. static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
  1092. {
  1093. if (qemu_opt_get(opts, "snapshot") == NULL) {
  1094. qemu_opt_set(opts, "snapshot", "on", &error_abort);
  1095. }
  1096. return 0;
  1097. }
  1098. static void default_drive(int enable, int snapshot, BlockInterfaceType type,
  1099. int index, const char *optstr)
  1100. {
  1101. QemuOpts *opts;
  1102. DriveInfo *dinfo;
  1103. if (!enable || drive_get_by_index(type, index)) {
  1104. return;
  1105. }
  1106. opts = drive_add(type, index, NULL, optstr);
  1107. if (snapshot) {
  1108. drive_enable_snapshot(NULL, opts, NULL);
  1109. }
  1110. dinfo = drive_new(opts, type);
  1111. if (!dinfo) {
  1112. exit(1);
  1113. }
  1114. dinfo->is_default = true;
  1115. }
  1116. static QemuOptsList qemu_smp_opts = {
  1117. .name = "smp-opts",
  1118. .implied_opt_name = "cpus",
  1119. .merge_lists = true,
  1120. .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
  1121. .desc = {
  1122. {
  1123. .name = "cpus",
  1124. .type = QEMU_OPT_NUMBER,
  1125. }, {
  1126. .name = "sockets",
  1127. .type = QEMU_OPT_NUMBER,
  1128. }, {
  1129. .name = "cores",
  1130. .type = QEMU_OPT_NUMBER,
  1131. }, {
  1132. .name = "threads",
  1133. .type = QEMU_OPT_NUMBER,
  1134. }, {
  1135. .name = "maxcpus",
  1136. .type = QEMU_OPT_NUMBER,
  1137. },
  1138. { /*End of list */ }
  1139. },
  1140. };
  1141. static void smp_parse(QemuOpts *opts)
  1142. {
  1143. if (opts) {
  1144. unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
  1145. unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
  1146. unsigned cores = qemu_opt_get_number(opts, "cores", 0);
  1147. unsigned threads = qemu_opt_get_number(opts, "threads", 0);
  1148. /* compute missing values, prefer sockets over cores over threads */
  1149. if (cpus == 0 || sockets == 0) {
  1150. sockets = sockets > 0 ? sockets : 1;
  1151. cores = cores > 0 ? cores : 1;
  1152. threads = threads > 0 ? threads : 1;
  1153. if (cpus == 0) {
  1154. cpus = cores * threads * sockets;
  1155. }
  1156. } else if (cores == 0) {
  1157. threads = threads > 0 ? threads : 1;
  1158. cores = cpus / (sockets * threads);
  1159. cores = cores > 0 ? cores : 1;
  1160. } else if (threads == 0) {
  1161. threads = cpus / (cores * sockets);
  1162. threads = threads > 0 ? threads : 1;
  1163. } else if (sockets * cores * threads < cpus) {
  1164. error_report("cpu topology: "
  1165. "sockets (%u) * cores (%u) * threads (%u) < "
  1166. "smp_cpus (%u)",
  1167. sockets, cores, threads, cpus);
  1168. exit(1);
  1169. }
  1170. max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
  1171. if (max_cpus < cpus) {
  1172. error_report("maxcpus must be equal to or greater than smp");
  1173. exit(1);
  1174. }
  1175. if (sockets * cores * threads > max_cpus) {
  1176. error_report("cpu topology: "
  1177. "sockets (%u) * cores (%u) * threads (%u) > "
  1178. "maxcpus (%u)",
  1179. sockets, cores, threads, max_cpus);
  1180. exit(1);
  1181. }
  1182. smp_cpus = cpus;
  1183. smp_cores = cores;
  1184. smp_threads = threads;
  1185. }
  1186. if (smp_cpus > 1) {
  1187. Error *blocker = NULL;
  1188. error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
  1189. replay_add_blocker(blocker);
  1190. }
  1191. }
  1192. static void realtime_init(void)
  1193. {
  1194. if (enable_mlock) {
  1195. if (os_mlock() < 0) {
  1196. error_report("locking memory failed");
  1197. exit(1);
  1198. }
  1199. }
  1200. }
  1201. static void configure_msg(QemuOpts *opts)
  1202. {
  1203. enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
  1204. }
  1205. /***********************************************************/
  1206. /* Semihosting */
  1207. typedef struct SemihostingConfig {
  1208. bool enabled;
  1209. SemihostingTarget target;
  1210. const char **argv;
  1211. int argc;
  1212. const char *cmdline; /* concatenated argv */
  1213. } SemihostingConfig;
  1214. static SemihostingConfig semihosting;
  1215. bool semihosting_enabled(void)
  1216. {
  1217. return semihosting.enabled;
  1218. }
  1219. SemihostingTarget semihosting_get_target(void)
  1220. {
  1221. return semihosting.target;
  1222. }
  1223. const char *semihosting_get_arg(int i)
  1224. {
  1225. if (i >= semihosting.argc) {
  1226. return NULL;
  1227. }
  1228. return semihosting.argv[i];
  1229. }
  1230. int semihosting_get_argc(void)
  1231. {
  1232. return semihosting.argc;
  1233. }
  1234. const char *semihosting_get_cmdline(void)
  1235. {
  1236. if (semihosting.cmdline == NULL && semihosting.argc > 0) {
  1237. semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
  1238. }
  1239. return semihosting.cmdline;
  1240. }
  1241. static int add_semihosting_arg(void *opaque,
  1242. const char *name, const char *val,
  1243. Error **errp)
  1244. {
  1245. SemihostingConfig *s = opaque;
  1246. if (strcmp(name, "arg") == 0) {
  1247. s->argc++;
  1248. /* one extra element as g_strjoinv() expects NULL-terminated array */
  1249. s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
  1250. s->argv[s->argc - 1] = val;
  1251. s->argv[s->argc] = NULL;
  1252. }
  1253. return 0;
  1254. }
  1255. /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
  1256. static inline void semihosting_arg_fallback(const char *file, const char *cmd)
  1257. {
  1258. char *cmd_token;
  1259. /* argv[0] */
  1260. add_semihosting_arg(&semihosting, "arg", file, NULL);
  1261. /* split -append and initialize argv[1..n] */
  1262. cmd_token = strtok(g_strdup(cmd), " ");
  1263. while (cmd_token) {
  1264. add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
  1265. cmd_token = strtok(NULL, " ");
  1266. }
  1267. }
  1268. /* Now we still need this for compatibility with XEN. */
  1269. bool has_igd_gfx_passthru;
  1270. static void igd_gfx_passthru(void)
  1271. {
  1272. has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
  1273. }
  1274. /***********************************************************/
  1275. /* USB devices */
  1276. static int usb_device_add(const char *devname)
  1277. {
  1278. USBDevice *dev = NULL;
  1279. #ifndef CONFIG_LINUX
  1280. const char *p;
  1281. #endif
  1282. if (!machine_usb(current_machine)) {
  1283. return -1;
  1284. }
  1285. /* drivers with .usbdevice_name entry in USBDeviceInfo */
  1286. dev = usbdevice_create(devname);
  1287. if (dev)
  1288. goto done;
  1289. /* the other ones */
  1290. #ifndef CONFIG_LINUX
  1291. /* only the linux version is qdev-ified, usb-bsd still needs this */
  1292. if (strstart(devname, "host:", &p)) {
  1293. dev = usb_host_device_open(usb_bus_find(-1), p);
  1294. }
  1295. #endif
  1296. if (!dev)
  1297. return -1;
  1298. done:
  1299. return 0;
  1300. }
  1301. static int usb_device_del(const char *devname)
  1302. {
  1303. int bus_num, addr;
  1304. const char *p;
  1305. if (strstart(devname, "host:", &p)) {
  1306. return -1;
  1307. }
  1308. if (!machine_usb(current_machine)) {
  1309. return -1;
  1310. }
  1311. p = strchr(devname, '.');
  1312. if (!p)
  1313. return -1;
  1314. bus_num = strtoul(devname, NULL, 0);
  1315. addr = strtoul(p + 1, NULL, 0);
  1316. return usb_device_delete_addr(bus_num, addr);
  1317. }
  1318. static int usb_parse(const char *cmdline)
  1319. {
  1320. int r;
  1321. r = usb_device_add(cmdline);
  1322. if (r < 0) {
  1323. error_report("could not add USB device '%s'", cmdline);
  1324. }
  1325. return r;
  1326. }
  1327. void hmp_usb_add(Monitor *mon, const QDict *qdict)
  1328. {
  1329. const char *devname = qdict_get_str(qdict, "devname");
  1330. error_report("usb_add is deprecated, please use device_add instead");
  1331. if (usb_device_add(devname) < 0) {
  1332. error_report("could not add USB device '%s'", devname);
  1333. }
  1334. }
  1335. void hmp_usb_del(Monitor *mon, const QDict *qdict)
  1336. {
  1337. const char *devname = qdict_get_str(qdict, "devname");
  1338. error_report("usb_del is deprecated, please use device_del instead");
  1339. if (usb_device_del(devname) < 0) {
  1340. error_report("could not delete USB device '%s'", devname);
  1341. }
  1342. }
  1343. /***********************************************************/
  1344. /* machine registration */
  1345. MachineState *current_machine;
  1346. static MachineClass *find_machine(const char *name)
  1347. {
  1348. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  1349. MachineClass *mc = NULL;
  1350. for (el = machines; el; el = el->next) {
  1351. MachineClass *temp = el->data;
  1352. if (!strcmp(temp->name, name)) {
  1353. mc = temp;
  1354. break;
  1355. }
  1356. if (temp->alias &&
  1357. !strcmp(temp->alias, name)) {
  1358. mc = temp;
  1359. break;
  1360. }
  1361. }
  1362. g_slist_free(machines);
  1363. return mc;
  1364. }
  1365. MachineClass *find_default_machine(void)
  1366. {
  1367. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  1368. MachineClass *mc = NULL;
  1369. for (el = machines; el; el = el->next) {
  1370. MachineClass *temp = el->data;
  1371. if (temp->is_default) {
  1372. mc = temp;
  1373. break;
  1374. }
  1375. }
  1376. g_slist_free(machines);
  1377. return mc;
  1378. }
  1379. MachineInfoList *qmp_query_machines(Error **errp)
  1380. {
  1381. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  1382. MachineInfoList *mach_list = NULL;
  1383. for (el = machines; el; el = el->next) {
  1384. MachineClass *mc = el->data;
  1385. MachineInfoList *entry;
  1386. MachineInfo *info;
  1387. info = g_malloc0(sizeof(*info));
  1388. if (mc->is_default) {
  1389. info->has_is_default = true;
  1390. info->is_default = true;
  1391. }
  1392. if (mc->alias) {
  1393. info->has_alias = true;
  1394. info->alias = g_strdup(mc->alias);
  1395. }
  1396. info->name = g_strdup(mc->name);
  1397. info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
  1398. info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
  1399. entry = g_malloc0(sizeof(*entry));
  1400. entry->value = info;
  1401. entry->next = mach_list;
  1402. mach_list = entry;
  1403. }
  1404. g_slist_free(machines);
  1405. return mach_list;
  1406. }
  1407. static int machine_help_func(QemuOpts *opts, MachineState *machine)
  1408. {
  1409. ObjectProperty *prop;
  1410. ObjectPropertyIterator iter;
  1411. if (!qemu_opt_has_help_opt(opts)) {
  1412. return 0;
  1413. }
  1414. object_property_iter_init(&iter, OBJECT(machine));
  1415. while ((prop = object_property_iter_next(&iter))) {
  1416. if (!prop->set) {
  1417. continue;
  1418. }
  1419. error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
  1420. prop->name, prop->type);
  1421. if (prop->description) {
  1422. error_printf(" (%s)\n", prop->description);
  1423. } else {
  1424. error_printf("\n");
  1425. }
  1426. }
  1427. return 1;
  1428. }
  1429. /***********************************************************/
  1430. /* main execution loop */
  1431. struct vm_change_state_entry {
  1432. VMChangeStateHandler *cb;
  1433. void *opaque;
  1434. QLIST_ENTRY (vm_change_state_entry) entries;
  1435. };
  1436. static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
  1437. VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
  1438. void *opaque)
  1439. {
  1440. VMChangeStateEntry *e;
  1441. e = g_malloc0(sizeof (*e));
  1442. e->cb = cb;
  1443. e->opaque = opaque;
  1444. QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
  1445. return e;
  1446. }
  1447. void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
  1448. {
  1449. QLIST_REMOVE (e, entries);
  1450. g_free (e);
  1451. }
  1452. void vm_state_notify(int running, RunState state)
  1453. {
  1454. VMChangeStateEntry *e, *next;
  1455. trace_vm_state_notify(running, state);
  1456. QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
  1457. e->cb(e->opaque, running, state);
  1458. }
  1459. }
  1460. static ShutdownCause reset_requested;
  1461. static ShutdownCause shutdown_requested;
  1462. static int shutdown_signal;
  1463. static pid_t shutdown_pid;
  1464. static int powerdown_requested;
  1465. static int debug_requested;
  1466. static int suspend_requested;
  1467. static WakeupReason wakeup_reason;
  1468. static NotifierList powerdown_notifiers =
  1469. NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
  1470. static NotifierList suspend_notifiers =
  1471. NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
  1472. static NotifierList wakeup_notifiers =
  1473. NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
  1474. static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
  1475. ShutdownCause qemu_shutdown_requested_get(void)
  1476. {
  1477. return shutdown_requested;
  1478. }
  1479. ShutdownCause qemu_reset_requested_get(void)
  1480. {
  1481. return reset_requested;
  1482. }
  1483. static int qemu_shutdown_requested(void)
  1484. {
  1485. return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
  1486. }
  1487. static void qemu_kill_report(void)
  1488. {
  1489. if (!qtest_driver() && shutdown_signal) {
  1490. if (shutdown_pid == 0) {
  1491. /* This happens for eg ^C at the terminal, so it's worth
  1492. * avoiding printing an odd message in that case.
  1493. */
  1494. error_report("terminating on signal %d", shutdown_signal);
  1495. } else {
  1496. char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
  1497. error_report("terminating on signal %d from pid " FMT_pid " (%s)",
  1498. shutdown_signal, shutdown_pid,
  1499. shutdown_cmd ? shutdown_cmd : "<unknown process>");
  1500. g_free(shutdown_cmd);
  1501. }
  1502. shutdown_signal = 0;
  1503. }
  1504. }
  1505. static ShutdownCause qemu_reset_requested(void)
  1506. {
  1507. ShutdownCause r = reset_requested;
  1508. if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
  1509. reset_requested = SHUTDOWN_CAUSE_NONE;
  1510. return r;
  1511. }
  1512. return SHUTDOWN_CAUSE_NONE;
  1513. }
  1514. static int qemu_suspend_requested(void)
  1515. {
  1516. int r = suspend_requested;
  1517. if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
  1518. suspend_requested = 0;
  1519. return r;
  1520. }
  1521. return false;
  1522. }
  1523. static WakeupReason qemu_wakeup_requested(void)
  1524. {
  1525. return wakeup_reason;
  1526. }
  1527. static int qemu_powerdown_requested(void)
  1528. {
  1529. int r = powerdown_requested;
  1530. powerdown_requested = 0;
  1531. return r;
  1532. }
  1533. static int qemu_debug_requested(void)
  1534. {
  1535. int r = debug_requested;
  1536. debug_requested = 0;
  1537. return r;
  1538. }
  1539. /*
  1540. * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
  1541. */
  1542. void qemu_system_reset(ShutdownCause reason)
  1543. {
  1544. MachineClass *mc;
  1545. mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
  1546. cpu_synchronize_all_states();
  1547. if (mc && mc->reset) {
  1548. mc->reset();
  1549. } else {
  1550. qemu_devices_reset();
  1551. }
  1552. if (reason) {
  1553. qapi_event_send_reset(shutdown_caused_by_guest(reason),
  1554. &error_abort);
  1555. }
  1556. cpu_synchronize_all_post_reset();
  1557. }
  1558. void qemu_system_guest_panicked(GuestPanicInformation *info)
  1559. {
  1560. qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
  1561. if (current_cpu) {
  1562. current_cpu->crash_occurred = true;
  1563. }
  1564. qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
  1565. !!info, info, &error_abort);
  1566. vm_stop(RUN_STATE_GUEST_PANICKED);
  1567. if (!no_shutdown) {
  1568. qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
  1569. !!info, info, &error_abort);
  1570. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
  1571. }
  1572. if (info) {
  1573. if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
  1574. qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
  1575. " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
  1576. info->u.hyper_v.arg1,
  1577. info->u.hyper_v.arg2,
  1578. info->u.hyper_v.arg3,
  1579. info->u.hyper_v.arg4,
  1580. info->u.hyper_v.arg5);
  1581. }
  1582. qapi_free_GuestPanicInformation(info);
  1583. }
  1584. }
  1585. void qemu_system_reset_request(ShutdownCause reason)
  1586. {
  1587. if (no_reboot) {
  1588. shutdown_requested = reason;
  1589. } else {
  1590. reset_requested = reason;
  1591. }
  1592. cpu_stop_current();
  1593. qemu_notify_event();
  1594. }
  1595. static void qemu_system_suspend(void)
  1596. {
  1597. pause_all_vcpus();
  1598. notifier_list_notify(&suspend_notifiers, NULL);
  1599. runstate_set(RUN_STATE_SUSPENDED);
  1600. qapi_event_send_suspend(&error_abort);
  1601. }
  1602. void qemu_system_suspend_request(void)
  1603. {
  1604. if (runstate_check(RUN_STATE_SUSPENDED)) {
  1605. return;
  1606. }
  1607. suspend_requested = 1;
  1608. cpu_stop_current();
  1609. qemu_notify_event();
  1610. }
  1611. void qemu_register_suspend_notifier(Notifier *notifier)
  1612. {
  1613. notifier_list_add(&suspend_notifiers, notifier);
  1614. }
  1615. void qemu_system_wakeup_request(WakeupReason reason)
  1616. {
  1617. trace_system_wakeup_request(reason);
  1618. if (!runstate_check(RUN_STATE_SUSPENDED)) {
  1619. return;
  1620. }
  1621. if (!(wakeup_reason_mask & (1 << reason))) {
  1622. return;
  1623. }
  1624. runstate_set(RUN_STATE_RUNNING);
  1625. wakeup_reason = reason;
  1626. qemu_notify_event();
  1627. }
  1628. void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
  1629. {
  1630. if (enabled) {
  1631. wakeup_reason_mask |= (1 << reason);
  1632. } else {
  1633. wakeup_reason_mask &= ~(1 << reason);
  1634. }
  1635. }
  1636. void qemu_register_wakeup_notifier(Notifier *notifier)
  1637. {
  1638. notifier_list_add(&wakeup_notifiers, notifier);
  1639. }
  1640. void qemu_system_killed(int signal, pid_t pid)
  1641. {
  1642. shutdown_signal = signal;
  1643. shutdown_pid = pid;
  1644. no_shutdown = 0;
  1645. /* Cannot call qemu_system_shutdown_request directly because
  1646. * we are in a signal handler.
  1647. */
  1648. shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
  1649. qemu_notify_event();
  1650. }
  1651. void qemu_system_shutdown_request(ShutdownCause reason)
  1652. {
  1653. trace_qemu_system_shutdown_request(reason);
  1654. replay_shutdown_request(reason);
  1655. shutdown_requested = reason;
  1656. qemu_notify_event();
  1657. }
  1658. static void qemu_system_powerdown(void)
  1659. {
  1660. qapi_event_send_powerdown(&error_abort);
  1661. notifier_list_notify(&powerdown_notifiers, NULL);
  1662. }
  1663. void qemu_system_powerdown_request(void)
  1664. {
  1665. trace_qemu_system_powerdown_request();
  1666. powerdown_requested = 1;
  1667. qemu_notify_event();
  1668. }
  1669. void qemu_register_powerdown_notifier(Notifier *notifier)
  1670. {
  1671. notifier_list_add(&powerdown_notifiers, notifier);
  1672. }
  1673. void qemu_system_debug_request(void)
  1674. {
  1675. debug_requested = 1;
  1676. qemu_notify_event();
  1677. }
  1678. static bool main_loop_should_exit(void)
  1679. {
  1680. RunState r;
  1681. ShutdownCause request;
  1682. if (qemu_debug_requested()) {
  1683. vm_stop(RUN_STATE_DEBUG);
  1684. }
  1685. if (qemu_suspend_requested()) {
  1686. qemu_system_suspend();
  1687. }
  1688. request = qemu_shutdown_requested();
  1689. if (request) {
  1690. qemu_kill_report();
  1691. qapi_event_send_shutdown(shutdown_caused_by_guest(request),
  1692. &error_abort);
  1693. if (no_shutdown) {
  1694. vm_stop(RUN_STATE_SHUTDOWN);
  1695. } else {
  1696. return true;
  1697. }
  1698. }
  1699. request = qemu_reset_requested();
  1700. if (request) {
  1701. pause_all_vcpus();
  1702. qemu_system_reset(request);
  1703. resume_all_vcpus();
  1704. if (!runstate_check(RUN_STATE_RUNNING) &&
  1705. !runstate_check(RUN_STATE_INMIGRATE)) {
  1706. runstate_set(RUN_STATE_PRELAUNCH);
  1707. }
  1708. }
  1709. if (qemu_wakeup_requested()) {
  1710. pause_all_vcpus();
  1711. qemu_system_reset(SHUTDOWN_CAUSE_NONE);
  1712. notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
  1713. wakeup_reason = QEMU_WAKEUP_REASON_NONE;
  1714. resume_all_vcpus();
  1715. qapi_event_send_wakeup(&error_abort);
  1716. }
  1717. if (qemu_powerdown_requested()) {
  1718. qemu_system_powerdown();
  1719. }
  1720. if (qemu_vmstop_requested(&r)) {
  1721. vm_stop(r);
  1722. }
  1723. return false;
  1724. }
  1725. static void main_loop(void)
  1726. {
  1727. #ifdef CONFIG_PROFILER
  1728. int64_t ti;
  1729. #endif
  1730. do {
  1731. #ifdef CONFIG_PROFILER
  1732. ti = profile_getclock();
  1733. #endif
  1734. main_loop_wait(false);
  1735. #ifdef CONFIG_PROFILER
  1736. dev_time += profile_getclock() - ti;
  1737. #endif
  1738. } while (!main_loop_should_exit());
  1739. }
  1740. static void version(void)
  1741. {
  1742. printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
  1743. QEMU_COPYRIGHT "\n");
  1744. }
  1745. static void help(int exitcode)
  1746. {
  1747. version();
  1748. printf("usage: %s [options] [disk_image]\n\n"
  1749. "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
  1750. error_get_progname());
  1751. #define QEMU_OPTIONS_GENERATE_HELP
  1752. #include "qemu-options-wrapper.h"
  1753. printf("\nDuring emulation, the following keys are useful:\n"
  1754. "ctrl-alt-f toggle full screen\n"
  1755. "ctrl-alt-n switch to virtual console 'n'\n"
  1756. "ctrl-alt toggle mouse and keyboard grab\n"
  1757. "\n"
  1758. "When using -nographic, press 'ctrl-a h' to get some help.\n"
  1759. "\n"
  1760. QEMU_HELP_BOTTOM "\n");
  1761. exit(exitcode);
  1762. }
  1763. #define HAS_ARG 0x0001
  1764. typedef struct QEMUOption {
  1765. const char *name;
  1766. int flags;
  1767. int index;
  1768. uint32_t arch_mask;
  1769. } QEMUOption;
  1770. static const QEMUOption qemu_options[] = {
  1771. { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
  1772. #define QEMU_OPTIONS_GENERATE_OPTIONS
  1773. #include "qemu-options-wrapper.h"
  1774. { NULL },
  1775. };
  1776. typedef struct VGAInterfaceInfo {
  1777. const char *opt_name; /* option name */
  1778. const char *name; /* human-readable name */
  1779. /* Class names indicating that support is available.
  1780. * If no class is specified, the interface is always available */
  1781. const char *class_names[2];
  1782. } VGAInterfaceInfo;
  1783. static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
  1784. [VGA_NONE] = {
  1785. .opt_name = "none",
  1786. },
  1787. [VGA_STD] = {
  1788. .opt_name = "std",
  1789. .name = "standard VGA",
  1790. .class_names = { "VGA", "isa-vga" },
  1791. },
  1792. [VGA_CIRRUS] = {
  1793. .opt_name = "cirrus",
  1794. .name = "Cirrus VGA",
  1795. .class_names = { "cirrus-vga", "isa-cirrus-vga" },
  1796. },
  1797. [VGA_VMWARE] = {
  1798. .opt_name = "vmware",
  1799. .name = "VMWare SVGA",
  1800. .class_names = { "vmware-svga" },
  1801. },
  1802. [VGA_VIRTIO] = {
  1803. .opt_name = "virtio",
  1804. .name = "Virtio VGA",
  1805. .class_names = { "virtio-vga" },
  1806. },
  1807. [VGA_QXL] = {
  1808. .opt_name = "qxl",
  1809. .name = "QXL VGA",
  1810. .class_names = { "qxl-vga" },
  1811. },
  1812. [VGA_TCX] = {
  1813. .opt_name = "tcx",
  1814. .name = "TCX framebuffer",
  1815. .class_names = { "SUNW,tcx" },
  1816. },
  1817. [VGA_CG3] = {
  1818. .opt_name = "cg3",
  1819. .name = "CG3 framebuffer",
  1820. .class_names = { "cgthree" },
  1821. },
  1822. [VGA_XENFB] = {
  1823. .opt_name = "xenfb",
  1824. },
  1825. };
  1826. static bool vga_interface_available(VGAInterfaceType t)
  1827. {
  1828. VGAInterfaceInfo *ti = &vga_interfaces[t];
  1829. assert(t < VGA_TYPE_MAX);
  1830. return !ti->class_names[0] ||
  1831. object_class_by_name(ti->class_names[0]) ||
  1832. object_class_by_name(ti->class_names[1]);
  1833. }
  1834. static void select_vgahw(const char *p)
  1835. {
  1836. const char *opts;
  1837. int t;
  1838. assert(vga_interface_type == VGA_NONE);
  1839. for (t = 0; t < VGA_TYPE_MAX; t++) {
  1840. VGAInterfaceInfo *ti = &vga_interfaces[t];
  1841. if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
  1842. if (!vga_interface_available(t)) {
  1843. error_report("%s not available", ti->name);
  1844. exit(1);
  1845. }
  1846. vga_interface_type = t;
  1847. break;
  1848. }
  1849. }
  1850. if (t == VGA_TYPE_MAX) {
  1851. invalid_vga:
  1852. error_report("unknown vga type: %s", p);
  1853. exit(1);
  1854. }
  1855. while (*opts) {
  1856. const char *nextopt;
  1857. if (strstart(opts, ",retrace=", &nextopt)) {
  1858. opts = nextopt;
  1859. if (strstart(opts, "dumb", &nextopt))
  1860. vga_retrace_method = VGA_RETRACE_DUMB;
  1861. else if (strstart(opts, "precise", &nextopt))
  1862. vga_retrace_method = VGA_RETRACE_PRECISE;
  1863. else goto invalid_vga;
  1864. } else goto invalid_vga;
  1865. opts = nextopt;
  1866. }
  1867. }
  1868. typedef enum DisplayType {
  1869. DT_DEFAULT,
  1870. DT_CURSES,
  1871. DT_SDL,
  1872. DT_COCOA,
  1873. DT_GTK,
  1874. DT_EGL,
  1875. DT_NONE,
  1876. } DisplayType;
  1877. static DisplayType select_display(const char *p)
  1878. {
  1879. const char *opts;
  1880. DisplayType display = DT_DEFAULT;
  1881. if (strstart(p, "sdl", &opts)) {
  1882. #ifdef CONFIG_SDL
  1883. display = DT_SDL;
  1884. while (*opts) {
  1885. const char *nextopt;
  1886. if (strstart(opts, ",frame=", &nextopt)) {
  1887. opts = nextopt;
  1888. if (strstart(opts, "on", &nextopt)) {
  1889. no_frame = 0;
  1890. } else if (strstart(opts, "off", &nextopt)) {
  1891. no_frame = 1;
  1892. } else {
  1893. goto invalid_sdl_args;
  1894. }
  1895. } else if (strstart(opts, ",alt_grab=", &nextopt)) {
  1896. opts = nextopt;
  1897. if (strstart(opts, "on", &nextopt)) {
  1898. alt_grab = 1;
  1899. } else if (strstart(opts, "off", &nextopt)) {
  1900. alt_grab = 0;
  1901. } else {
  1902. goto invalid_sdl_args;
  1903. }
  1904. } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
  1905. opts = nextopt;
  1906. if (strstart(opts, "on", &nextopt)) {
  1907. ctrl_grab = 1;
  1908. } else if (strstart(opts, "off", &nextopt)) {
  1909. ctrl_grab = 0;
  1910. } else {
  1911. goto invalid_sdl_args;
  1912. }
  1913. } else if (strstart(opts, ",window_close=", &nextopt)) {
  1914. opts = nextopt;
  1915. if (strstart(opts, "on", &nextopt)) {
  1916. no_quit = 0;
  1917. } else if (strstart(opts, "off", &nextopt)) {
  1918. no_quit = 1;
  1919. } else {
  1920. goto invalid_sdl_args;
  1921. }
  1922. } else if (strstart(opts, ",gl=", &nextopt)) {
  1923. opts = nextopt;
  1924. if (strstart(opts, "on", &nextopt)) {
  1925. request_opengl = 1;
  1926. } else if (strstart(opts, "off", &nextopt)) {
  1927. request_opengl = 0;
  1928. } else {
  1929. goto invalid_sdl_args;
  1930. }
  1931. } else {
  1932. invalid_sdl_args:
  1933. error_report("invalid SDL option string");
  1934. exit(1);
  1935. }
  1936. opts = nextopt;
  1937. }
  1938. #else
  1939. error_report("SDL support is disabled");
  1940. exit(1);
  1941. #endif
  1942. } else if (strstart(p, "vnc", &opts)) {
  1943. if (*opts == '=') {
  1944. vnc_parse(opts + 1, &error_fatal);
  1945. } else {
  1946. error_report("VNC requires a display argument vnc=<display>");
  1947. exit(1);
  1948. }
  1949. } else if (strstart(p, "egl-headless", &opts)) {
  1950. #ifdef CONFIG_OPENGL_DMABUF
  1951. request_opengl = 1;
  1952. display_opengl = 1;
  1953. display = DT_EGL;
  1954. #else
  1955. fprintf(stderr, "egl support is disabled\n");
  1956. exit(1);
  1957. #endif
  1958. } else if (strstart(p, "curses", &opts)) {
  1959. #ifdef CONFIG_CURSES
  1960. display = DT_CURSES;
  1961. #else
  1962. error_report("curses support is disabled");
  1963. exit(1);
  1964. #endif
  1965. } else if (strstart(p, "gtk", &opts)) {
  1966. #ifdef CONFIG_GTK
  1967. display = DT_GTK;
  1968. while (*opts) {
  1969. const char *nextopt;
  1970. if (strstart(opts, ",grab_on_hover=", &nextopt)) {
  1971. opts = nextopt;
  1972. if (strstart(opts, "on", &nextopt)) {
  1973. grab_on_hover = true;
  1974. } else if (strstart(opts, "off", &nextopt)) {
  1975. grab_on_hover = false;
  1976. } else {
  1977. goto invalid_gtk_args;
  1978. }
  1979. } else if (strstart(opts, ",gl=", &nextopt)) {
  1980. opts = nextopt;
  1981. if (strstart(opts, "on", &nextopt)) {
  1982. request_opengl = 1;
  1983. } else if (strstart(opts, "off", &nextopt)) {
  1984. request_opengl = 0;
  1985. } else {
  1986. goto invalid_gtk_args;
  1987. }
  1988. } else {
  1989. invalid_gtk_args:
  1990. error_report("invalid GTK option string");
  1991. exit(1);
  1992. }
  1993. opts = nextopt;
  1994. }
  1995. #else
  1996. error_report("GTK support is disabled");
  1997. exit(1);
  1998. #endif
  1999. } else if (strstart(p, "none", &opts)) {
  2000. display = DT_NONE;
  2001. } else {
  2002. error_report("unknown display type");
  2003. exit(1);
  2004. }
  2005. return display;
  2006. }
  2007. static int balloon_parse(const char *arg)
  2008. {
  2009. QemuOpts *opts;
  2010. if (strcmp(arg, "none") == 0) {
  2011. return 0;
  2012. }
  2013. if (!strncmp(arg, "virtio", 6)) {
  2014. if (arg[6] == ',') {
  2015. /* have params -> parse them */
  2016. opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
  2017. false);
  2018. if (!opts)
  2019. return -1;
  2020. } else {
  2021. /* create empty opts */
  2022. opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  2023. &error_abort);
  2024. }
  2025. qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
  2026. return 0;
  2027. }
  2028. return -1;
  2029. }
  2030. char *qemu_find_file(int type, const char *name)
  2031. {
  2032. int i;
  2033. const char *subdir;
  2034. char *buf;
  2035. /* Try the name as a straight path first */
  2036. if (access(name, R_OK) == 0) {
  2037. trace_load_file(name, name);
  2038. return g_strdup(name);
  2039. }
  2040. switch (type) {
  2041. case QEMU_FILE_TYPE_BIOS:
  2042. subdir = "";
  2043. break;
  2044. case QEMU_FILE_TYPE_KEYMAP:
  2045. subdir = "keymaps/";
  2046. break;
  2047. default:
  2048. abort();
  2049. }
  2050. for (i = 0; i < data_dir_idx; i++) {
  2051. buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
  2052. if (access(buf, R_OK) == 0) {
  2053. trace_load_file(name, buf);
  2054. return buf;
  2055. }
  2056. g_free(buf);
  2057. }
  2058. return NULL;
  2059. }
  2060. static void qemu_add_data_dir(const char *path)
  2061. {
  2062. int i;
  2063. if (path == NULL) {
  2064. return;
  2065. }
  2066. if (data_dir_idx == ARRAY_SIZE(data_dir)) {
  2067. return;
  2068. }
  2069. for (i = 0; i < data_dir_idx; i++) {
  2070. if (strcmp(data_dir[i], path) == 0) {
  2071. return; /* duplicate */
  2072. }
  2073. }
  2074. data_dir[data_dir_idx++] = path;
  2075. }
  2076. static inline bool nonempty_str(const char *str)
  2077. {
  2078. return str && *str;
  2079. }
  2080. static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
  2081. {
  2082. gchar *buf;
  2083. size_t size;
  2084. const char *name, *file, *str;
  2085. FWCfgState *fw_cfg = (FWCfgState *) opaque;
  2086. if (fw_cfg == NULL) {
  2087. error_report("fw_cfg device not available");
  2088. return -1;
  2089. }
  2090. name = qemu_opt_get(opts, "name");
  2091. file = qemu_opt_get(opts, "file");
  2092. str = qemu_opt_get(opts, "string");
  2093. /* we need name and either a file or the content string */
  2094. if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
  2095. error_report("invalid argument(s)");
  2096. return -1;
  2097. }
  2098. if (nonempty_str(file) && nonempty_str(str)) {
  2099. error_report("file and string are mutually exclusive");
  2100. return -1;
  2101. }
  2102. if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
  2103. error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
  2104. return -1;
  2105. }
  2106. if (strncmp(name, "opt/", 4) != 0) {
  2107. warn_report("externally provided fw_cfg item names "
  2108. "should be prefixed with \"opt/\"");
  2109. }
  2110. if (nonempty_str(str)) {
  2111. size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
  2112. buf = g_memdup(str, size);
  2113. } else {
  2114. if (!g_file_get_contents(file, &buf, &size, NULL)) {
  2115. error_report("can't load %s", file);
  2116. return -1;
  2117. }
  2118. }
  2119. /* For legacy, keep user files in a specific global order. */
  2120. fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
  2121. fw_cfg_add_file(fw_cfg, name, buf, size);
  2122. fw_cfg_reset_order_override(fw_cfg);
  2123. return 0;
  2124. }
  2125. static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
  2126. {
  2127. return qdev_device_help(opts);
  2128. }
  2129. static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2130. {
  2131. Error *err = NULL;
  2132. DeviceState *dev;
  2133. dev = qdev_device_add(opts, &err);
  2134. if (!dev) {
  2135. error_report_err(err);
  2136. return -1;
  2137. }
  2138. object_unref(OBJECT(dev));
  2139. return 0;
  2140. }
  2141. static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2142. {
  2143. Error *local_err = NULL;
  2144. if (!qemu_chr_new_from_opts(opts, &local_err)) {
  2145. if (local_err) {
  2146. error_report_err(local_err);
  2147. return -1;
  2148. }
  2149. exit(0);
  2150. }
  2151. return 0;
  2152. }
  2153. #ifdef CONFIG_VIRTFS
  2154. static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2155. {
  2156. return qemu_fsdev_add(opts);
  2157. }
  2158. #endif
  2159. static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2160. {
  2161. Chardev *chr;
  2162. const char *chardev;
  2163. const char *mode;
  2164. int flags;
  2165. mode = qemu_opt_get(opts, "mode");
  2166. if (mode == NULL) {
  2167. mode = "readline";
  2168. }
  2169. if (strcmp(mode, "readline") == 0) {
  2170. flags = MONITOR_USE_READLINE;
  2171. } else if (strcmp(mode, "control") == 0) {
  2172. flags = MONITOR_USE_CONTROL;
  2173. } else {
  2174. error_report("unknown monitor mode \"%s\"", mode);
  2175. exit(1);
  2176. }
  2177. if (qemu_opt_get_bool(opts, "pretty", 0))
  2178. flags |= MONITOR_USE_PRETTY;
  2179. if (qemu_opt_get_bool(opts, "default", 0)) {
  2180. error_report("option 'default' does nothing and is deprecated");
  2181. }
  2182. chardev = qemu_opt_get(opts, "chardev");
  2183. chr = qemu_chr_find(chardev);
  2184. if (chr == NULL) {
  2185. error_report("chardev \"%s\" not found", chardev);
  2186. exit(1);
  2187. }
  2188. monitor_init(chr, flags);
  2189. return 0;
  2190. }
  2191. static void monitor_parse(const char *optarg, const char *mode, bool pretty)
  2192. {
  2193. static int monitor_device_index = 0;
  2194. QemuOpts *opts;
  2195. const char *p;
  2196. char label[32];
  2197. if (strstart(optarg, "chardev:", &p)) {
  2198. snprintf(label, sizeof(label), "%s", p);
  2199. } else {
  2200. snprintf(label, sizeof(label), "compat_monitor%d",
  2201. monitor_device_index);
  2202. opts = qemu_chr_parse_compat(label, optarg);
  2203. if (!opts) {
  2204. error_report("parse error: %s", optarg);
  2205. exit(1);
  2206. }
  2207. }
  2208. opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
  2209. qemu_opt_set(opts, "mode", mode, &error_abort);
  2210. qemu_opt_set(opts, "chardev", label, &error_abort);
  2211. qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
  2212. monitor_device_index++;
  2213. }
  2214. struct device_config {
  2215. enum {
  2216. DEV_USB, /* -usbdevice */
  2217. DEV_BT, /* -bt */
  2218. DEV_SERIAL, /* -serial */
  2219. DEV_PARALLEL, /* -parallel */
  2220. DEV_VIRTCON, /* -virtioconsole */
  2221. DEV_DEBUGCON, /* -debugcon */
  2222. DEV_GDB, /* -gdb, -s */
  2223. DEV_SCLP, /* s390 sclp */
  2224. } type;
  2225. const char *cmdline;
  2226. Location loc;
  2227. QTAILQ_ENTRY(device_config) next;
  2228. };
  2229. static QTAILQ_HEAD(, device_config) device_configs =
  2230. QTAILQ_HEAD_INITIALIZER(device_configs);
  2231. static void add_device_config(int type, const char *cmdline)
  2232. {
  2233. struct device_config *conf;
  2234. conf = g_malloc0(sizeof(*conf));
  2235. conf->type = type;
  2236. conf->cmdline = cmdline;
  2237. loc_save(&conf->loc);
  2238. QTAILQ_INSERT_TAIL(&device_configs, conf, next);
  2239. }
  2240. static int foreach_device_config(int type, int (*func)(const char *cmdline))
  2241. {
  2242. struct device_config *conf;
  2243. int rc;
  2244. QTAILQ_FOREACH(conf, &device_configs, next) {
  2245. if (conf->type != type)
  2246. continue;
  2247. loc_push_restore(&conf->loc);
  2248. rc = func(conf->cmdline);
  2249. loc_pop(&conf->loc);
  2250. if (rc) {
  2251. return rc;
  2252. }
  2253. }
  2254. return 0;
  2255. }
  2256. static int serial_parse(const char *devname)
  2257. {
  2258. static int index = 0;
  2259. char label[32];
  2260. if (strcmp(devname, "none") == 0)
  2261. return 0;
  2262. if (index == MAX_SERIAL_PORTS) {
  2263. error_report("too many serial ports");
  2264. exit(1);
  2265. }
  2266. snprintf(label, sizeof(label), "serial%d", index);
  2267. serial_hds[index] = qemu_chr_new(label, devname);
  2268. if (!serial_hds[index]) {
  2269. error_report("could not connect serial device"
  2270. " to character backend '%s'", devname);
  2271. return -1;
  2272. }
  2273. index++;
  2274. return 0;
  2275. }
  2276. static int parallel_parse(const char *devname)
  2277. {
  2278. static int index = 0;
  2279. char label[32];
  2280. if (strcmp(devname, "none") == 0)
  2281. return 0;
  2282. if (index == MAX_PARALLEL_PORTS) {
  2283. error_report("too many parallel ports");
  2284. exit(1);
  2285. }
  2286. snprintf(label, sizeof(label), "parallel%d", index);
  2287. parallel_hds[index] = qemu_chr_new(label, devname);
  2288. if (!parallel_hds[index]) {
  2289. error_report("could not connect parallel device"
  2290. " to character backend '%s'", devname);
  2291. return -1;
  2292. }
  2293. index++;
  2294. return 0;
  2295. }
  2296. static int virtcon_parse(const char *devname)
  2297. {
  2298. QemuOptsList *device = qemu_find_opts("device");
  2299. static int index = 0;
  2300. char label[32];
  2301. QemuOpts *bus_opts, *dev_opts;
  2302. if (strcmp(devname, "none") == 0)
  2303. return 0;
  2304. if (index == MAX_VIRTIO_CONSOLES) {
  2305. error_report("too many virtio consoles");
  2306. exit(1);
  2307. }
  2308. bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
  2309. qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
  2310. dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
  2311. qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
  2312. snprintf(label, sizeof(label), "virtcon%d", index);
  2313. virtcon_hds[index] = qemu_chr_new(label, devname);
  2314. if (!virtcon_hds[index]) {
  2315. error_report("could not connect virtio console"
  2316. " to character backend '%s'", devname);
  2317. return -1;
  2318. }
  2319. qemu_opt_set(dev_opts, "chardev", label, &error_abort);
  2320. index++;
  2321. return 0;
  2322. }
  2323. static int sclp_parse(const char *devname)
  2324. {
  2325. QemuOptsList *device = qemu_find_opts("device");
  2326. static int index = 0;
  2327. char label[32];
  2328. QemuOpts *dev_opts;
  2329. if (strcmp(devname, "none") == 0) {
  2330. return 0;
  2331. }
  2332. if (index == MAX_SCLP_CONSOLES) {
  2333. error_report("too many sclp consoles");
  2334. exit(1);
  2335. }
  2336. assert(arch_type == QEMU_ARCH_S390X);
  2337. dev_opts = qemu_opts_create(device, NULL, 0, NULL);
  2338. qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
  2339. snprintf(label, sizeof(label), "sclpcon%d", index);
  2340. sclp_hds[index] = qemu_chr_new(label, devname);
  2341. if (!sclp_hds[index]) {
  2342. error_report("could not connect sclp console"
  2343. " to character backend '%s'", devname);
  2344. return -1;
  2345. }
  2346. qemu_opt_set(dev_opts, "chardev", label, &error_abort);
  2347. index++;
  2348. return 0;
  2349. }
  2350. static int debugcon_parse(const char *devname)
  2351. {
  2352. QemuOpts *opts;
  2353. if (!qemu_chr_new("debugcon", devname)) {
  2354. exit(1);
  2355. }
  2356. opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
  2357. if (!opts) {
  2358. error_report("already have a debugcon device");
  2359. exit(1);
  2360. }
  2361. qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
  2362. qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
  2363. return 0;
  2364. }
  2365. static gint machine_class_cmp(gconstpointer a, gconstpointer b)
  2366. {
  2367. const MachineClass *mc1 = a, *mc2 = b;
  2368. int res;
  2369. if (mc1->family == NULL) {
  2370. if (mc2->family == NULL) {
  2371. /* Compare standalone machine types against each other; they sort
  2372. * in increasing order.
  2373. */
  2374. return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
  2375. object_class_get_name(OBJECT_CLASS(mc2)));
  2376. }
  2377. /* Standalone machine types sort after families. */
  2378. return 1;
  2379. }
  2380. if (mc2->family == NULL) {
  2381. /* Families sort before standalone machine types. */
  2382. return -1;
  2383. }
  2384. /* Families sort between each other alphabetically increasingly. */
  2385. res = strcmp(mc1->family, mc2->family);
  2386. if (res != 0) {
  2387. return res;
  2388. }
  2389. /* Within the same family, machine types sort in decreasing order. */
  2390. return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
  2391. object_class_get_name(OBJECT_CLASS(mc1)));
  2392. }
  2393. static MachineClass *machine_parse(const char *name)
  2394. {
  2395. MachineClass *mc = NULL;
  2396. GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
  2397. if (name) {
  2398. mc = find_machine(name);
  2399. }
  2400. if (mc) {
  2401. g_slist_free(machines);
  2402. return mc;
  2403. }
  2404. if (name && !is_help_option(name)) {
  2405. error_report("unsupported machine type");
  2406. error_printf("Use -machine help to list supported machines\n");
  2407. } else {
  2408. printf("Supported machines are:\n");
  2409. machines = g_slist_sort(machines, machine_class_cmp);
  2410. for (el = machines; el; el = el->next) {
  2411. MachineClass *mc = el->data;
  2412. if (mc->alias) {
  2413. printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
  2414. }
  2415. printf("%-20s %s%s\n", mc->name, mc->desc,
  2416. mc->is_default ? " (default)" : "");
  2417. }
  2418. }
  2419. g_slist_free(machines);
  2420. exit(!name || !is_help_option(name));
  2421. }
  2422. void qemu_add_exit_notifier(Notifier *notify)
  2423. {
  2424. notifier_list_add(&exit_notifiers, notify);
  2425. }
  2426. void qemu_remove_exit_notifier(Notifier *notify)
  2427. {
  2428. notifier_remove(notify);
  2429. }
  2430. static void qemu_run_exit_notifiers(void)
  2431. {
  2432. notifier_list_notify(&exit_notifiers, NULL);
  2433. }
  2434. static bool machine_init_done;
  2435. void qemu_add_machine_init_done_notifier(Notifier *notify)
  2436. {
  2437. notifier_list_add(&machine_init_done_notifiers, notify);
  2438. if (machine_init_done) {
  2439. notify->notify(notify, NULL);
  2440. }
  2441. }
  2442. void qemu_remove_machine_init_done_notifier(Notifier *notify)
  2443. {
  2444. notifier_remove(notify);
  2445. }
  2446. static void qemu_run_machine_init_done_notifiers(void)
  2447. {
  2448. notifier_list_notify(&machine_init_done_notifiers, NULL);
  2449. machine_init_done = true;
  2450. }
  2451. static const QEMUOption *lookup_opt(int argc, char **argv,
  2452. const char **poptarg, int *poptind)
  2453. {
  2454. const QEMUOption *popt;
  2455. int optind = *poptind;
  2456. char *r = argv[optind];
  2457. const char *optarg;
  2458. loc_set_cmdline(argv, optind, 1);
  2459. optind++;
  2460. /* Treat --foo the same as -foo. */
  2461. if (r[1] == '-')
  2462. r++;
  2463. popt = qemu_options;
  2464. for(;;) {
  2465. if (!popt->name) {
  2466. error_report("invalid option");
  2467. exit(1);
  2468. }
  2469. if (!strcmp(popt->name, r + 1))
  2470. break;
  2471. popt++;
  2472. }
  2473. if (popt->flags & HAS_ARG) {
  2474. if (optind >= argc) {
  2475. error_report("requires an argument");
  2476. exit(1);
  2477. }
  2478. optarg = argv[optind++];
  2479. loc_set_cmdline(argv, optind - 2, 2);
  2480. } else {
  2481. optarg = NULL;
  2482. }
  2483. *poptarg = optarg;
  2484. *poptind = optind;
  2485. return popt;
  2486. }
  2487. static MachineClass *select_machine(void)
  2488. {
  2489. MachineClass *machine_class = find_default_machine();
  2490. const char *optarg;
  2491. QemuOpts *opts;
  2492. Location loc;
  2493. loc_push_none(&loc);
  2494. opts = qemu_get_machine_opts();
  2495. qemu_opts_loc_restore(opts);
  2496. optarg = qemu_opt_get(opts, "type");
  2497. if (optarg) {
  2498. machine_class = machine_parse(optarg);
  2499. }
  2500. if (!machine_class) {
  2501. error_report("No machine specified, and there is no default");
  2502. error_printf("Use -machine help to list supported machines\n");
  2503. exit(1);
  2504. }
  2505. loc_pop(&loc);
  2506. return machine_class;
  2507. }
  2508. static int machine_set_property(void *opaque,
  2509. const char *name, const char *value,
  2510. Error **errp)
  2511. {
  2512. Object *obj = OBJECT(opaque);
  2513. Error *local_err = NULL;
  2514. char *p, *qom_name;
  2515. if (strcmp(name, "type") == 0) {
  2516. return 0;
  2517. }
  2518. qom_name = g_strdup(name);
  2519. for (p = qom_name; *p; p++) {
  2520. if (*p == '_') {
  2521. *p = '-';
  2522. }
  2523. }
  2524. object_property_parse(obj, value, qom_name, &local_err);
  2525. g_free(qom_name);
  2526. if (local_err) {
  2527. error_report_err(local_err);
  2528. return -1;
  2529. }
  2530. return 0;
  2531. }
  2532. /*
  2533. * Initial object creation happens before all other
  2534. * QEMU data types are created. The majority of objects
  2535. * can be created at this point. The rng-egd object
  2536. * cannot be created here, as it depends on the chardev
  2537. * already existing.
  2538. */
  2539. static bool object_create_initial(const char *type)
  2540. {
  2541. if (g_str_equal(type, "rng-egd") ||
  2542. g_str_has_prefix(type, "pr-manager-")) {
  2543. return false;
  2544. }
  2545. /*
  2546. * return false for concrete netfilters since
  2547. * they depend on netdevs already existing
  2548. */
  2549. if (g_str_equal(type, "filter-buffer") ||
  2550. g_str_equal(type, "filter-dump") ||
  2551. g_str_equal(type, "filter-mirror") ||
  2552. g_str_equal(type, "filter-redirector") ||
  2553. g_str_equal(type, "colo-compare") ||
  2554. g_str_equal(type, "filter-rewriter") ||
  2555. g_str_equal(type, "filter-replay")) {
  2556. return false;
  2557. }
  2558. /* Memory allocation by backends needs to be done
  2559. * after configure_accelerator() (due to the tcg_enabled()
  2560. * checks at memory_region_init_*()).
  2561. *
  2562. * Also, allocation of large amounts of memory may delay
  2563. * chardev initialization for too long, and trigger timeouts
  2564. * on software that waits for a monitor socket to be created
  2565. * (e.g. libvirt).
  2566. */
  2567. if (g_str_has_prefix(type, "memory-backend-")) {
  2568. return false;
  2569. }
  2570. return true;
  2571. }
  2572. /*
  2573. * The remainder of object creation happens after the
  2574. * creation of chardev, fsdev, net clients and device data types.
  2575. */
  2576. static bool object_create_delayed(const char *type)
  2577. {
  2578. return !object_create_initial(type);
  2579. }
  2580. static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
  2581. MachineClass *mc)
  2582. {
  2583. uint64_t sz;
  2584. const char *mem_str;
  2585. const char *maxmem_str, *slots_str;
  2586. const ram_addr_t default_ram_size = mc->default_ram_size;
  2587. QemuOpts *opts = qemu_find_opts_singleton("memory");
  2588. Location loc;
  2589. loc_push_none(&loc);
  2590. qemu_opts_loc_restore(opts);
  2591. sz = 0;
  2592. mem_str = qemu_opt_get(opts, "size");
  2593. if (mem_str) {
  2594. if (!*mem_str) {
  2595. error_report("missing 'size' option value");
  2596. exit(EXIT_FAILURE);
  2597. }
  2598. sz = qemu_opt_get_size(opts, "size", ram_size);
  2599. /* Fix up legacy suffix-less format */
  2600. if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
  2601. uint64_t overflow_check = sz;
  2602. sz <<= 20;
  2603. if ((sz >> 20) != overflow_check) {
  2604. error_report("too large 'size' option value");
  2605. exit(EXIT_FAILURE);
  2606. }
  2607. }
  2608. }
  2609. /* backward compatibility behaviour for case "-m 0" */
  2610. if (sz == 0) {
  2611. sz = default_ram_size;
  2612. }
  2613. sz = QEMU_ALIGN_UP(sz, 8192);
  2614. ram_size = sz;
  2615. if (ram_size != sz) {
  2616. error_report("ram size too large");
  2617. exit(EXIT_FAILURE);
  2618. }
  2619. /* store value for the future use */
  2620. qemu_opt_set_number(opts, "size", ram_size, &error_abort);
  2621. *maxram_size = ram_size;
  2622. maxmem_str = qemu_opt_get(opts, "maxmem");
  2623. slots_str = qemu_opt_get(opts, "slots");
  2624. if (maxmem_str && slots_str) {
  2625. uint64_t slots;
  2626. sz = qemu_opt_get_size(opts, "maxmem", 0);
  2627. slots = qemu_opt_get_number(opts, "slots", 0);
  2628. if (sz < ram_size) {
  2629. error_report("invalid value of -m option maxmem: "
  2630. "maximum memory size (0x%" PRIx64 ") must be at least "
  2631. "the initial memory size (0x" RAM_ADDR_FMT ")",
  2632. sz, ram_size);
  2633. exit(EXIT_FAILURE);
  2634. } else if (sz > ram_size) {
  2635. if (!slots) {
  2636. error_report("invalid value of -m option: maxmem was "
  2637. "specified, but no hotplug slots were specified");
  2638. exit(EXIT_FAILURE);
  2639. }
  2640. } else if (slots) {
  2641. error_report("invalid value of -m option maxmem: "
  2642. "memory slots were specified but maximum memory size "
  2643. "(0x%" PRIx64 ") is equal to the initial memory size "
  2644. "(0x" RAM_ADDR_FMT ")", sz, ram_size);
  2645. exit(EXIT_FAILURE);
  2646. }
  2647. *maxram_size = sz;
  2648. *ram_slots = slots;
  2649. } else if ((!maxmem_str && slots_str) ||
  2650. (maxmem_str && !slots_str)) {
  2651. error_report("invalid -m option value: missing "
  2652. "'%s' option", slots_str ? "maxmem" : "slots");
  2653. exit(EXIT_FAILURE);
  2654. }
  2655. loc_pop(&loc);
  2656. }
  2657. static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
  2658. {
  2659. GlobalProperty *g;
  2660. g = g_malloc0(sizeof(*g));
  2661. g->driver = qemu_opt_get(opts, "driver");
  2662. g->property = qemu_opt_get(opts, "property");
  2663. g->value = qemu_opt_get(opts, "value");
  2664. g->user_provided = true;
  2665. g->errp = &error_fatal;
  2666. qdev_prop_register_global(g);
  2667. return 0;
  2668. }
  2669. static int qemu_read_default_config_file(void)
  2670. {
  2671. int ret;
  2672. ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
  2673. if (ret < 0 && ret != -ENOENT) {
  2674. return ret;
  2675. }
  2676. return 0;
  2677. }
  2678. static void user_register_global_props(void)
  2679. {
  2680. qemu_opts_foreach(qemu_find_opts("global"),
  2681. global_init_func, NULL, NULL);
  2682. }
  2683. /*
  2684. * Note: we should see that these properties are actually having a
  2685. * priority: accel < machine < user. This means e.g. when user
  2686. * specifies something in "-global", it'll always be used with highest
  2687. * priority than either machine/accelerator compat properties.
  2688. */
  2689. static void register_global_properties(MachineState *ms)
  2690. {
  2691. accel_register_compat_props(ms->accelerator);
  2692. machine_register_compat_props(ms);
  2693. user_register_global_props();
  2694. }
  2695. int main(int argc, char **argv, char **envp)
  2696. {
  2697. int i;
  2698. int snapshot, linux_boot;
  2699. const char *initrd_filename;
  2700. const char *kernel_filename, *kernel_cmdline;
  2701. const char *boot_order = NULL;
  2702. const char *boot_once = NULL;
  2703. DisplayState *ds;
  2704. int cyls, heads, secs, translation;
  2705. QemuOpts *opts, *machine_opts;
  2706. QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
  2707. QemuOptsList *olist;
  2708. int optind;
  2709. const char *optarg;
  2710. const char *loadvm = NULL;
  2711. MachineClass *machine_class;
  2712. const char *cpu_model;
  2713. const char *vga_model = NULL;
  2714. const char *qtest_chrdev = NULL;
  2715. const char *qtest_log = NULL;
  2716. const char *pid_file = NULL;
  2717. const char *incoming = NULL;
  2718. bool userconfig = true;
  2719. bool nographic = false;
  2720. DisplayType display_type = DT_DEFAULT;
  2721. int display_remote = 0;
  2722. const char *log_mask = NULL;
  2723. const char *log_file = NULL;
  2724. char *trace_file = NULL;
  2725. ram_addr_t maxram_size;
  2726. uint64_t ram_slots = 0;
  2727. FILE *vmstate_dump_file = NULL;
  2728. Error *main_loop_err = NULL;
  2729. Error *err = NULL;
  2730. bool list_data_dirs = false;
  2731. char **dirs;
  2732. typedef struct BlockdevOptions_queue {
  2733. BlockdevOptions *bdo;
  2734. Location loc;
  2735. QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
  2736. } BlockdevOptions_queue;
  2737. QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
  2738. = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
  2739. module_call_init(MODULE_INIT_TRACE);
  2740. qemu_init_cpu_list();
  2741. qemu_init_cpu_loop();
  2742. qemu_mutex_lock_iothread();
  2743. atexit(qemu_run_exit_notifiers);
  2744. error_set_progname(argv[0]);
  2745. qemu_init_exec_dir(argv[0]);
  2746. module_call_init(MODULE_INIT_QOM);
  2747. monitor_init_qmp_commands();
  2748. qemu_add_opts(&qemu_drive_opts);
  2749. qemu_add_drive_opts(&qemu_legacy_drive_opts);
  2750. qemu_add_drive_opts(&qemu_common_drive_opts);
  2751. qemu_add_drive_opts(&qemu_drive_opts);
  2752. qemu_add_drive_opts(&bdrv_runtime_opts);
  2753. qemu_add_opts(&qemu_chardev_opts);
  2754. qemu_add_opts(&qemu_device_opts);
  2755. qemu_add_opts(&qemu_netdev_opts);
  2756. qemu_add_opts(&qemu_net_opts);
  2757. qemu_add_opts(&qemu_rtc_opts);
  2758. qemu_add_opts(&qemu_global_opts);
  2759. qemu_add_opts(&qemu_mon_opts);
  2760. qemu_add_opts(&qemu_trace_opts);
  2761. qemu_add_opts(&qemu_option_rom_opts);
  2762. qemu_add_opts(&qemu_machine_opts);
  2763. qemu_add_opts(&qemu_accel_opts);
  2764. qemu_add_opts(&qemu_mem_opts);
  2765. qemu_add_opts(&qemu_smp_opts);
  2766. qemu_add_opts(&qemu_boot_opts);
  2767. qemu_add_opts(&qemu_sandbox_opts);
  2768. qemu_add_opts(&qemu_add_fd_opts);
  2769. qemu_add_opts(&qemu_object_opts);
  2770. qemu_add_opts(&qemu_tpmdev_opts);
  2771. qemu_add_opts(&qemu_realtime_opts);
  2772. qemu_add_opts(&qemu_msg_opts);
  2773. qemu_add_opts(&qemu_name_opts);
  2774. qemu_add_opts(&qemu_numa_opts);
  2775. qemu_add_opts(&qemu_icount_opts);
  2776. qemu_add_opts(&qemu_semihosting_config_opts);
  2777. qemu_add_opts(&qemu_fw_cfg_opts);
  2778. module_call_init(MODULE_INIT_OPTS);
  2779. runstate_init();
  2780. if (qcrypto_init(&err) < 0) {
  2781. error_reportf_err(err, "cannot initialize crypto: ");
  2782. exit(1);
  2783. }
  2784. rtc_clock = QEMU_CLOCK_HOST;
  2785. QLIST_INIT (&vm_change_state_head);
  2786. os_setup_early_signal_handling();
  2787. cpu_model = NULL;
  2788. snapshot = 0;
  2789. cyls = heads = secs = 0;
  2790. translation = BIOS_ATA_TRANSLATION_AUTO;
  2791. nb_nics = 0;
  2792. bdrv_init_with_whitelist();
  2793. autostart = 1;
  2794. /* first pass of option parsing */
  2795. optind = 1;
  2796. while (optind < argc) {
  2797. if (argv[optind][0] != '-') {
  2798. /* disk image */
  2799. optind++;
  2800. } else {
  2801. const QEMUOption *popt;
  2802. popt = lookup_opt(argc, argv, &optarg, &optind);
  2803. switch (popt->index) {
  2804. case QEMU_OPTION_nodefconfig:
  2805. case QEMU_OPTION_nouserconfig:
  2806. userconfig = false;
  2807. break;
  2808. }
  2809. }
  2810. }
  2811. if (userconfig) {
  2812. if (qemu_read_default_config_file() < 0) {
  2813. exit(1);
  2814. }
  2815. }
  2816. /* second pass of option parsing */
  2817. optind = 1;
  2818. for(;;) {
  2819. if (optind >= argc)
  2820. break;
  2821. if (argv[optind][0] != '-') {
  2822. hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
  2823. } else {
  2824. const QEMUOption *popt;
  2825. popt = lookup_opt(argc, argv, &optarg, &optind);
  2826. if (!(popt->arch_mask & arch_type)) {
  2827. error_report("Option not supported for this target");
  2828. exit(1);
  2829. }
  2830. switch(popt->index) {
  2831. case QEMU_OPTION_no_kvm_irqchip: {
  2832. olist = qemu_find_opts("machine");
  2833. qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
  2834. break;
  2835. }
  2836. case QEMU_OPTION_cpu:
  2837. /* hw initialization will check this */
  2838. cpu_model = optarg;
  2839. break;
  2840. case QEMU_OPTION_hda:
  2841. {
  2842. char buf[256];
  2843. if (cyls == 0)
  2844. snprintf(buf, sizeof(buf), "%s", HD_OPTS);
  2845. else
  2846. snprintf(buf, sizeof(buf),
  2847. "%s,cyls=%d,heads=%d,secs=%d%s",
  2848. HD_OPTS , cyls, heads, secs,
  2849. translation == BIOS_ATA_TRANSLATION_LBA ?
  2850. ",trans=lba" :
  2851. translation == BIOS_ATA_TRANSLATION_NONE ?
  2852. ",trans=none" : "");
  2853. drive_add(IF_DEFAULT, 0, optarg, buf);
  2854. break;
  2855. }
  2856. case QEMU_OPTION_hdb:
  2857. case QEMU_OPTION_hdc:
  2858. case QEMU_OPTION_hdd:
  2859. drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
  2860. HD_OPTS);
  2861. break;
  2862. case QEMU_OPTION_blockdev:
  2863. {
  2864. Visitor *v;
  2865. BlockdevOptions_queue *bdo;
  2866. v = qobject_input_visitor_new_str(optarg, "driver", &err);
  2867. if (!v) {
  2868. error_report_err(err);
  2869. exit(1);
  2870. }
  2871. bdo = g_new(BlockdevOptions_queue, 1);
  2872. visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
  2873. &error_fatal);
  2874. visit_free(v);
  2875. loc_save(&bdo->loc);
  2876. QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
  2877. break;
  2878. }
  2879. case QEMU_OPTION_drive:
  2880. if (drive_def(optarg) == NULL) {
  2881. exit(1);
  2882. }
  2883. break;
  2884. case QEMU_OPTION_set:
  2885. if (qemu_set_option(optarg) != 0)
  2886. exit(1);
  2887. break;
  2888. case QEMU_OPTION_global:
  2889. if (qemu_global_option(optarg) != 0)
  2890. exit(1);
  2891. break;
  2892. case QEMU_OPTION_mtdblock:
  2893. drive_add(IF_MTD, -1, optarg, MTD_OPTS);
  2894. break;
  2895. case QEMU_OPTION_sd:
  2896. drive_add(IF_SD, -1, optarg, SD_OPTS);
  2897. break;
  2898. case QEMU_OPTION_pflash:
  2899. drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
  2900. break;
  2901. case QEMU_OPTION_snapshot:
  2902. snapshot = 1;
  2903. break;
  2904. case QEMU_OPTION_hdachs:
  2905. {
  2906. const char *p;
  2907. p = optarg;
  2908. cyls = strtol(p, (char **)&p, 0);
  2909. if (cyls < 1 || cyls > 16383)
  2910. goto chs_fail;
  2911. if (*p != ',')
  2912. goto chs_fail;
  2913. p++;
  2914. heads = strtol(p, (char **)&p, 0);
  2915. if (heads < 1 || heads > 16)
  2916. goto chs_fail;
  2917. if (*p != ',')
  2918. goto chs_fail;
  2919. p++;
  2920. secs = strtol(p, (char **)&p, 0);
  2921. if (secs < 1 || secs > 63)
  2922. goto chs_fail;
  2923. if (*p == ',') {
  2924. p++;
  2925. if (!strcmp(p, "large")) {
  2926. translation = BIOS_ATA_TRANSLATION_LARGE;
  2927. } else if (!strcmp(p, "rechs")) {
  2928. translation = BIOS_ATA_TRANSLATION_RECHS;
  2929. } else if (!strcmp(p, "none")) {
  2930. translation = BIOS_ATA_TRANSLATION_NONE;
  2931. } else if (!strcmp(p, "lba")) {
  2932. translation = BIOS_ATA_TRANSLATION_LBA;
  2933. } else if (!strcmp(p, "auto")) {
  2934. translation = BIOS_ATA_TRANSLATION_AUTO;
  2935. } else {
  2936. goto chs_fail;
  2937. }
  2938. } else if (*p != '\0') {
  2939. chs_fail:
  2940. error_report("invalid physical CHS format");
  2941. exit(1);
  2942. }
  2943. if (hda_opts != NULL) {
  2944. qemu_opt_set_number(hda_opts, "cyls", cyls,
  2945. &error_abort);
  2946. qemu_opt_set_number(hda_opts, "heads", heads,
  2947. &error_abort);
  2948. qemu_opt_set_number(hda_opts, "secs", secs,
  2949. &error_abort);
  2950. if (translation == BIOS_ATA_TRANSLATION_LARGE) {
  2951. qemu_opt_set(hda_opts, "trans", "large",
  2952. &error_abort);
  2953. } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
  2954. qemu_opt_set(hda_opts, "trans", "rechs",
  2955. &error_abort);
  2956. } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
  2957. qemu_opt_set(hda_opts, "trans", "lba",
  2958. &error_abort);
  2959. } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
  2960. qemu_opt_set(hda_opts, "trans", "none",
  2961. &error_abort);
  2962. }
  2963. }
  2964. }
  2965. error_report("'-hdachs' is deprecated, please use '-device"
  2966. " ide-hd,cyls=c,heads=h,secs=s,...' instead");
  2967. break;
  2968. case QEMU_OPTION_numa:
  2969. opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
  2970. optarg, true);
  2971. if (!opts) {
  2972. exit(1);
  2973. }
  2974. break;
  2975. case QEMU_OPTION_display:
  2976. display_type = select_display(optarg);
  2977. break;
  2978. case QEMU_OPTION_nographic:
  2979. olist = qemu_find_opts("machine");
  2980. qemu_opts_parse_noisily(olist, "graphics=off", false);
  2981. nographic = true;
  2982. display_type = DT_NONE;
  2983. break;
  2984. case QEMU_OPTION_curses:
  2985. #ifdef CONFIG_CURSES
  2986. display_type = DT_CURSES;
  2987. #else
  2988. error_report("curses support is disabled");
  2989. exit(1);
  2990. #endif
  2991. break;
  2992. case QEMU_OPTION_portrait:
  2993. graphic_rotate = 90;
  2994. break;
  2995. case QEMU_OPTION_rotate:
  2996. graphic_rotate = strtol(optarg, (char **) &optarg, 10);
  2997. if (graphic_rotate != 0 && graphic_rotate != 90 &&
  2998. graphic_rotate != 180 && graphic_rotate != 270) {
  2999. error_report("only 90, 180, 270 deg rotation is available");
  3000. exit(1);
  3001. }
  3002. break;
  3003. case QEMU_OPTION_kernel:
  3004. qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
  3005. &error_abort);
  3006. break;
  3007. case QEMU_OPTION_initrd:
  3008. qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
  3009. &error_abort);
  3010. break;
  3011. case QEMU_OPTION_append:
  3012. qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
  3013. &error_abort);
  3014. break;
  3015. case QEMU_OPTION_dtb:
  3016. qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
  3017. &error_abort);
  3018. break;
  3019. case QEMU_OPTION_cdrom:
  3020. drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
  3021. break;
  3022. case QEMU_OPTION_boot:
  3023. opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
  3024. optarg, true);
  3025. if (!opts) {
  3026. exit(1);
  3027. }
  3028. break;
  3029. case QEMU_OPTION_fda:
  3030. case QEMU_OPTION_fdb:
  3031. drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
  3032. optarg, FD_OPTS);
  3033. break;
  3034. case QEMU_OPTION_no_fd_bootchk:
  3035. fd_bootchk = 0;
  3036. break;
  3037. case QEMU_OPTION_netdev:
  3038. default_net = 0;
  3039. if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
  3040. exit(1);
  3041. }
  3042. break;
  3043. case QEMU_OPTION_net:
  3044. default_net = 0;
  3045. if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
  3046. exit(1);
  3047. }
  3048. break;
  3049. #ifdef CONFIG_LIBISCSI
  3050. case QEMU_OPTION_iscsi:
  3051. opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
  3052. optarg, false);
  3053. if (!opts) {
  3054. exit(1);
  3055. }
  3056. break;
  3057. #endif
  3058. #ifdef CONFIG_SLIRP
  3059. case QEMU_OPTION_tftp:
  3060. error_report("The -tftp option is deprecated. "
  3061. "Please use '-netdev user,tftp=...' instead.");
  3062. legacy_tftp_prefix = optarg;
  3063. break;
  3064. case QEMU_OPTION_bootp:
  3065. error_report("The -bootp option is deprecated. "
  3066. "Please use '-netdev user,bootfile=...' instead.");
  3067. legacy_bootp_filename = optarg;
  3068. break;
  3069. case QEMU_OPTION_redir:
  3070. error_report("The -redir option is deprecated. "
  3071. "Please use '-netdev user,hostfwd=...' instead.");
  3072. if (net_slirp_redir(optarg) < 0)
  3073. exit(1);
  3074. break;
  3075. #endif
  3076. case QEMU_OPTION_bt:
  3077. add_device_config(DEV_BT, optarg);
  3078. break;
  3079. case QEMU_OPTION_audio_help:
  3080. AUD_help ();
  3081. exit (0);
  3082. break;
  3083. case QEMU_OPTION_soundhw:
  3084. select_soundhw (optarg);
  3085. break;
  3086. case QEMU_OPTION_h:
  3087. help(0);
  3088. break;
  3089. case QEMU_OPTION_version:
  3090. version();
  3091. exit(0);
  3092. break;
  3093. case QEMU_OPTION_m:
  3094. opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
  3095. optarg, true);
  3096. if (!opts) {
  3097. exit(EXIT_FAILURE);
  3098. }
  3099. break;
  3100. #ifdef CONFIG_TPM
  3101. case QEMU_OPTION_tpmdev:
  3102. if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
  3103. exit(1);
  3104. }
  3105. break;
  3106. #endif
  3107. case QEMU_OPTION_mempath:
  3108. mem_path = optarg;
  3109. break;
  3110. case QEMU_OPTION_mem_prealloc:
  3111. mem_prealloc = 1;
  3112. break;
  3113. case QEMU_OPTION_d:
  3114. log_mask = optarg;
  3115. break;
  3116. case QEMU_OPTION_D:
  3117. log_file = optarg;
  3118. break;
  3119. case QEMU_OPTION_DFILTER:
  3120. qemu_set_dfilter_ranges(optarg, &error_fatal);
  3121. break;
  3122. case QEMU_OPTION_s:
  3123. add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
  3124. break;
  3125. case QEMU_OPTION_gdb:
  3126. add_device_config(DEV_GDB, optarg);
  3127. break;
  3128. case QEMU_OPTION_L:
  3129. if (is_help_option(optarg)) {
  3130. list_data_dirs = true;
  3131. } else {
  3132. qemu_add_data_dir(optarg);
  3133. }
  3134. break;
  3135. case QEMU_OPTION_bios:
  3136. qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
  3137. &error_abort);
  3138. break;
  3139. case QEMU_OPTION_singlestep:
  3140. singlestep = 1;
  3141. break;
  3142. case QEMU_OPTION_S:
  3143. autostart = 0;
  3144. break;
  3145. case QEMU_OPTION_k:
  3146. keyboard_layout = optarg;
  3147. break;
  3148. case QEMU_OPTION_localtime:
  3149. rtc_utc = 0;
  3150. break;
  3151. case QEMU_OPTION_vga:
  3152. vga_model = optarg;
  3153. default_vga = 0;
  3154. break;
  3155. case QEMU_OPTION_g:
  3156. {
  3157. const char *p;
  3158. int w, h, depth;
  3159. p = optarg;
  3160. w = strtol(p, (char **)&p, 10);
  3161. if (w <= 0) {
  3162. graphic_error:
  3163. error_report("invalid resolution or depth");
  3164. exit(1);
  3165. }
  3166. if (*p != 'x')
  3167. goto graphic_error;
  3168. p++;
  3169. h = strtol(p, (char **)&p, 10);
  3170. if (h <= 0)
  3171. goto graphic_error;
  3172. if (*p == 'x') {
  3173. p++;
  3174. depth = strtol(p, (char **)&p, 10);
  3175. if (depth != 8 && depth != 15 && depth != 16 &&
  3176. depth != 24 && depth != 32)
  3177. goto graphic_error;
  3178. } else if (*p == '\0') {
  3179. depth = graphic_depth;
  3180. } else {
  3181. goto graphic_error;
  3182. }
  3183. graphic_width = w;
  3184. graphic_height = h;
  3185. graphic_depth = depth;
  3186. }
  3187. break;
  3188. case QEMU_OPTION_echr:
  3189. {
  3190. char *r;
  3191. term_escape_char = strtol(optarg, &r, 0);
  3192. if (r == optarg)
  3193. printf("Bad argument to echr\n");
  3194. break;
  3195. }
  3196. case QEMU_OPTION_monitor:
  3197. default_monitor = 0;
  3198. if (strncmp(optarg, "none", 4)) {
  3199. monitor_parse(optarg, "readline", false);
  3200. }
  3201. break;
  3202. case QEMU_OPTION_qmp:
  3203. monitor_parse(optarg, "control", false);
  3204. default_monitor = 0;
  3205. break;
  3206. case QEMU_OPTION_qmp_pretty:
  3207. monitor_parse(optarg, "control", true);
  3208. default_monitor = 0;
  3209. break;
  3210. case QEMU_OPTION_mon:
  3211. opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
  3212. true);
  3213. if (!opts) {
  3214. exit(1);
  3215. }
  3216. default_monitor = 0;
  3217. break;
  3218. case QEMU_OPTION_chardev:
  3219. opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
  3220. optarg, true);
  3221. if (!opts) {
  3222. exit(1);
  3223. }
  3224. break;
  3225. case QEMU_OPTION_fsdev:
  3226. olist = qemu_find_opts("fsdev");
  3227. if (!olist) {
  3228. error_report("fsdev support is disabled");
  3229. exit(1);
  3230. }
  3231. opts = qemu_opts_parse_noisily(olist, optarg, true);
  3232. if (!opts) {
  3233. exit(1);
  3234. }
  3235. break;
  3236. case QEMU_OPTION_virtfs: {
  3237. QemuOpts *fsdev;
  3238. QemuOpts *device;
  3239. const char *writeout, *sock_fd, *socket, *path, *security_model;
  3240. olist = qemu_find_opts("virtfs");
  3241. if (!olist) {
  3242. error_report("virtfs support is disabled");
  3243. exit(1);
  3244. }
  3245. opts = qemu_opts_parse_noisily(olist, optarg, true);
  3246. if (!opts) {
  3247. exit(1);
  3248. }
  3249. if (qemu_opt_get(opts, "fsdriver") == NULL ||
  3250. qemu_opt_get(opts, "mount_tag") == NULL) {
  3251. error_report("Usage: -virtfs fsdriver,mount_tag=tag");
  3252. exit(1);
  3253. }
  3254. fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
  3255. qemu_opts_id(opts) ?:
  3256. qemu_opt_get(opts, "mount_tag"),
  3257. 1, NULL);
  3258. if (!fsdev) {
  3259. error_report("duplicate or invalid fsdev id: %s",
  3260. qemu_opt_get(opts, "mount_tag"));
  3261. exit(1);
  3262. }
  3263. writeout = qemu_opt_get(opts, "writeout");
  3264. if (writeout) {
  3265. #ifdef CONFIG_SYNC_FILE_RANGE
  3266. qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
  3267. #else
  3268. error_report("writeout=immediate not supported "
  3269. "on this platform");
  3270. exit(1);
  3271. #endif
  3272. }
  3273. qemu_opt_set(fsdev, "fsdriver",
  3274. qemu_opt_get(opts, "fsdriver"), &error_abort);
  3275. path = qemu_opt_get(opts, "path");
  3276. if (path) {
  3277. qemu_opt_set(fsdev, "path", path, &error_abort);
  3278. }
  3279. security_model = qemu_opt_get(opts, "security_model");
  3280. if (security_model) {
  3281. qemu_opt_set(fsdev, "security_model", security_model,
  3282. &error_abort);
  3283. }
  3284. socket = qemu_opt_get(opts, "socket");
  3285. if (socket) {
  3286. qemu_opt_set(fsdev, "socket", socket, &error_abort);
  3287. }
  3288. sock_fd = qemu_opt_get(opts, "sock_fd");
  3289. if (sock_fd) {
  3290. qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
  3291. }
  3292. qemu_opt_set_bool(fsdev, "readonly",
  3293. qemu_opt_get_bool(opts, "readonly", 0),
  3294. &error_abort);
  3295. device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  3296. &error_abort);
  3297. qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
  3298. qemu_opt_set(device, "fsdev",
  3299. qemu_opts_id(fsdev), &error_abort);
  3300. qemu_opt_set(device, "mount_tag",
  3301. qemu_opt_get(opts, "mount_tag"), &error_abort);
  3302. break;
  3303. }
  3304. case QEMU_OPTION_virtfs_synth: {
  3305. QemuOpts *fsdev;
  3306. QemuOpts *device;
  3307. fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
  3308. 1, NULL);
  3309. if (!fsdev) {
  3310. error_report("duplicate option: %s", "virtfs_synth");
  3311. exit(1);
  3312. }
  3313. qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
  3314. device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  3315. &error_abort);
  3316. qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
  3317. qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
  3318. qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
  3319. break;
  3320. }
  3321. case QEMU_OPTION_serial:
  3322. add_device_config(DEV_SERIAL, optarg);
  3323. default_serial = 0;
  3324. if (strncmp(optarg, "mon:", 4) == 0) {
  3325. default_monitor = 0;
  3326. }
  3327. break;
  3328. case QEMU_OPTION_watchdog:
  3329. if (watchdog) {
  3330. error_report("only one watchdog option may be given");
  3331. return 1;
  3332. }
  3333. watchdog = optarg;
  3334. break;
  3335. case QEMU_OPTION_watchdog_action:
  3336. if (select_watchdog_action(optarg) == -1) {
  3337. error_report("unknown -watchdog-action parameter");
  3338. exit(1);
  3339. }
  3340. break;
  3341. case QEMU_OPTION_virtiocon:
  3342. add_device_config(DEV_VIRTCON, optarg);
  3343. default_virtcon = 0;
  3344. if (strncmp(optarg, "mon:", 4) == 0) {
  3345. default_monitor = 0;
  3346. }
  3347. break;
  3348. case QEMU_OPTION_parallel:
  3349. add_device_config(DEV_PARALLEL, optarg);
  3350. default_parallel = 0;
  3351. if (strncmp(optarg, "mon:", 4) == 0) {
  3352. default_monitor = 0;
  3353. }
  3354. break;
  3355. case QEMU_OPTION_debugcon:
  3356. add_device_config(DEV_DEBUGCON, optarg);
  3357. break;
  3358. case QEMU_OPTION_loadvm:
  3359. loadvm = optarg;
  3360. break;
  3361. case QEMU_OPTION_full_screen:
  3362. full_screen = 1;
  3363. break;
  3364. case QEMU_OPTION_no_frame:
  3365. no_frame = 1;
  3366. break;
  3367. case QEMU_OPTION_alt_grab:
  3368. alt_grab = 1;
  3369. break;
  3370. case QEMU_OPTION_ctrl_grab:
  3371. ctrl_grab = 1;
  3372. break;
  3373. case QEMU_OPTION_no_quit:
  3374. no_quit = 1;
  3375. break;
  3376. case QEMU_OPTION_sdl:
  3377. #ifdef CONFIG_SDL
  3378. display_type = DT_SDL;
  3379. break;
  3380. #else
  3381. error_report("SDL support is disabled");
  3382. exit(1);
  3383. #endif
  3384. case QEMU_OPTION_pidfile:
  3385. pid_file = optarg;
  3386. break;
  3387. case QEMU_OPTION_win2k_hack:
  3388. win2k_install_hack = 1;
  3389. break;
  3390. case QEMU_OPTION_rtc_td_hack: {
  3391. static GlobalProperty slew_lost_ticks = {
  3392. .driver = "mc146818rtc",
  3393. .property = "lost_tick_policy",
  3394. .value = "slew",
  3395. };
  3396. qdev_prop_register_global(&slew_lost_ticks);
  3397. break;
  3398. }
  3399. case QEMU_OPTION_acpitable:
  3400. opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
  3401. optarg, true);
  3402. if (!opts) {
  3403. exit(1);
  3404. }
  3405. acpi_table_add(opts, &error_fatal);
  3406. break;
  3407. case QEMU_OPTION_smbios:
  3408. opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
  3409. optarg, false);
  3410. if (!opts) {
  3411. exit(1);
  3412. }
  3413. smbios_entry_add(opts, &error_fatal);
  3414. break;
  3415. case QEMU_OPTION_fwcfg:
  3416. opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
  3417. optarg, true);
  3418. if (opts == NULL) {
  3419. exit(1);
  3420. }
  3421. break;
  3422. case QEMU_OPTION_enable_kvm:
  3423. olist = qemu_find_opts("machine");
  3424. qemu_opts_parse_noisily(olist, "accel=kvm", false);
  3425. break;
  3426. case QEMU_OPTION_enable_hax:
  3427. olist = qemu_find_opts("machine");
  3428. qemu_opts_parse_noisily(olist, "accel=hax", false);
  3429. break;
  3430. case QEMU_OPTION_M:
  3431. case QEMU_OPTION_machine:
  3432. olist = qemu_find_opts("machine");
  3433. opts = qemu_opts_parse_noisily(olist, optarg, true);
  3434. if (!opts) {
  3435. exit(1);
  3436. }
  3437. break;
  3438. case QEMU_OPTION_no_kvm:
  3439. olist = qemu_find_opts("machine");
  3440. qemu_opts_parse_noisily(olist, "accel=tcg", false);
  3441. break;
  3442. case QEMU_OPTION_no_kvm_pit: {
  3443. warn_report("ignoring deprecated option");
  3444. break;
  3445. }
  3446. case QEMU_OPTION_no_kvm_pit_reinjection: {
  3447. static GlobalProperty kvm_pit_lost_tick_policy = {
  3448. .driver = "kvm-pit",
  3449. .property = "lost_tick_policy",
  3450. .value = "discard",
  3451. };
  3452. warn_report("deprecated, replaced by "
  3453. "-global kvm-pit.lost_tick_policy=discard");
  3454. qdev_prop_register_global(&kvm_pit_lost_tick_policy);
  3455. break;
  3456. }
  3457. case QEMU_OPTION_accel:
  3458. accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
  3459. optarg, true);
  3460. optarg = qemu_opt_get(accel_opts, "accel");
  3461. if (!optarg || is_help_option(optarg)) {
  3462. error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
  3463. exit(0);
  3464. }
  3465. opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
  3466. false, &error_abort);
  3467. qemu_opt_set(opts, "accel", optarg, &error_abort);
  3468. break;
  3469. case QEMU_OPTION_usb:
  3470. olist = qemu_find_opts("machine");
  3471. qemu_opts_parse_noisily(olist, "usb=on", false);
  3472. break;
  3473. case QEMU_OPTION_usbdevice:
  3474. error_report("'-usbdevice' is deprecated, please use "
  3475. "'-device usb-...' instead");
  3476. olist = qemu_find_opts("machine");
  3477. qemu_opts_parse_noisily(olist, "usb=on", false);
  3478. add_device_config(DEV_USB, optarg);
  3479. break;
  3480. case QEMU_OPTION_device:
  3481. if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
  3482. optarg, true)) {
  3483. exit(1);
  3484. }
  3485. break;
  3486. case QEMU_OPTION_smp:
  3487. if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
  3488. optarg, true)) {
  3489. exit(1);
  3490. }
  3491. break;
  3492. case QEMU_OPTION_vnc:
  3493. vnc_parse(optarg, &error_fatal);
  3494. break;
  3495. case QEMU_OPTION_no_acpi:
  3496. acpi_enabled = 0;
  3497. break;
  3498. case QEMU_OPTION_no_hpet:
  3499. no_hpet = 1;
  3500. break;
  3501. case QEMU_OPTION_balloon:
  3502. if (balloon_parse(optarg) < 0) {
  3503. error_report("unknown -balloon argument %s", optarg);
  3504. exit(1);
  3505. }
  3506. break;
  3507. case QEMU_OPTION_no_reboot:
  3508. no_reboot = 1;
  3509. break;
  3510. case QEMU_OPTION_no_shutdown:
  3511. no_shutdown = 1;
  3512. break;
  3513. case QEMU_OPTION_show_cursor:
  3514. cursor_hide = 0;
  3515. break;
  3516. case QEMU_OPTION_uuid:
  3517. if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
  3518. error_report("failed to parse UUID string: wrong format");
  3519. exit(1);
  3520. }
  3521. qemu_uuid_set = true;
  3522. break;
  3523. case QEMU_OPTION_option_rom:
  3524. if (nb_option_roms >= MAX_OPTION_ROMS) {
  3525. error_report("too many option ROMs");
  3526. exit(1);
  3527. }
  3528. opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
  3529. optarg, true);
  3530. if (!opts) {
  3531. exit(1);
  3532. }
  3533. option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
  3534. option_rom[nb_option_roms].bootindex =
  3535. qemu_opt_get_number(opts, "bootindex", -1);
  3536. if (!option_rom[nb_option_roms].name) {
  3537. error_report("Option ROM file is not specified");
  3538. exit(1);
  3539. }
  3540. nb_option_roms++;
  3541. break;
  3542. case QEMU_OPTION_semihosting:
  3543. semihosting.enabled = true;
  3544. semihosting.target = SEMIHOSTING_TARGET_AUTO;
  3545. break;
  3546. case QEMU_OPTION_semihosting_config:
  3547. semihosting.enabled = true;
  3548. opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
  3549. optarg, false);
  3550. if (opts != NULL) {
  3551. semihosting.enabled = qemu_opt_get_bool(opts, "enable",
  3552. true);
  3553. const char *target = qemu_opt_get(opts, "target");
  3554. if (target != NULL) {
  3555. if (strcmp("native", target) == 0) {
  3556. semihosting.target = SEMIHOSTING_TARGET_NATIVE;
  3557. } else if (strcmp("gdb", target) == 0) {
  3558. semihosting.target = SEMIHOSTING_TARGET_GDB;
  3559. } else if (strcmp("auto", target) == 0) {
  3560. semihosting.target = SEMIHOSTING_TARGET_AUTO;
  3561. } else {
  3562. error_report("unsupported semihosting-config %s",
  3563. optarg);
  3564. exit(1);
  3565. }
  3566. } else {
  3567. semihosting.target = SEMIHOSTING_TARGET_AUTO;
  3568. }
  3569. /* Set semihosting argument count and vector */
  3570. qemu_opt_foreach(opts, add_semihosting_arg,
  3571. &semihosting, NULL);
  3572. } else {
  3573. error_report("unsupported semihosting-config %s", optarg);
  3574. exit(1);
  3575. }
  3576. break;
  3577. case QEMU_OPTION_tdf:
  3578. warn_report("ignoring deprecated option");
  3579. break;
  3580. case QEMU_OPTION_name:
  3581. opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
  3582. optarg, true);
  3583. if (!opts) {
  3584. exit(1);
  3585. }
  3586. break;
  3587. case QEMU_OPTION_prom_env:
  3588. if (nb_prom_envs >= MAX_PROM_ENVS) {
  3589. error_report("too many prom variables");
  3590. exit(1);
  3591. }
  3592. prom_envs[nb_prom_envs] = optarg;
  3593. nb_prom_envs++;
  3594. break;
  3595. case QEMU_OPTION_old_param:
  3596. old_param = 1;
  3597. break;
  3598. case QEMU_OPTION_clock:
  3599. /* Clock options no longer exist. Keep this option for
  3600. * backward compatibility.
  3601. */
  3602. break;
  3603. case QEMU_OPTION_startdate:
  3604. configure_rtc_date_offset(optarg, 1);
  3605. break;
  3606. case QEMU_OPTION_rtc:
  3607. opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
  3608. false);
  3609. if (!opts) {
  3610. exit(1);
  3611. }
  3612. configure_rtc(opts);
  3613. break;
  3614. case QEMU_OPTION_tb_size:
  3615. #ifndef CONFIG_TCG
  3616. error_report("TCG is disabled");
  3617. exit(1);
  3618. #endif
  3619. if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
  3620. error_report("Invalid argument to -tb-size");
  3621. exit(1);
  3622. }
  3623. break;
  3624. case QEMU_OPTION_icount:
  3625. icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
  3626. optarg, true);
  3627. if (!icount_opts) {
  3628. exit(1);
  3629. }
  3630. break;
  3631. case QEMU_OPTION_incoming:
  3632. if (!incoming) {
  3633. runstate_set(RUN_STATE_INMIGRATE);
  3634. }
  3635. incoming = optarg;
  3636. break;
  3637. case QEMU_OPTION_only_migratable:
  3638. /*
  3639. * TODO: we can remove this option one day, and we
  3640. * should all use:
  3641. *
  3642. * "-global migration.only-migratable=true"
  3643. */
  3644. qemu_global_option("migration.only-migratable=true");
  3645. break;
  3646. case QEMU_OPTION_nodefaults:
  3647. has_defaults = 0;
  3648. break;
  3649. case QEMU_OPTION_xen_domid:
  3650. if (!(xen_available())) {
  3651. error_report("Option not supported for this target");
  3652. exit(1);
  3653. }
  3654. xen_domid = atoi(optarg);
  3655. break;
  3656. case QEMU_OPTION_xen_create:
  3657. if (!(xen_available())) {
  3658. error_report("Option not supported for this target");
  3659. exit(1);
  3660. }
  3661. xen_mode = XEN_CREATE;
  3662. break;
  3663. case QEMU_OPTION_xen_attach:
  3664. if (!(xen_available())) {
  3665. error_report("Option not supported for this target");
  3666. exit(1);
  3667. }
  3668. xen_mode = XEN_ATTACH;
  3669. break;
  3670. case QEMU_OPTION_xen_domid_restrict:
  3671. if (!(xen_available())) {
  3672. error_report("Option not supported for this target");
  3673. exit(1);
  3674. }
  3675. xen_domid_restrict = true;
  3676. break;
  3677. case QEMU_OPTION_trace:
  3678. g_free(trace_file);
  3679. trace_file = trace_opt_parse(optarg);
  3680. break;
  3681. case QEMU_OPTION_readconfig:
  3682. {
  3683. int ret = qemu_read_config_file(optarg);
  3684. if (ret < 0) {
  3685. error_report("read config %s: %s", optarg,
  3686. strerror(-ret));
  3687. exit(1);
  3688. }
  3689. break;
  3690. }
  3691. case QEMU_OPTION_spice:
  3692. olist = qemu_find_opts("spice");
  3693. if (!olist) {
  3694. error_report("spice support is disabled");
  3695. exit(1);
  3696. }
  3697. opts = qemu_opts_parse_noisily(olist, optarg, false);
  3698. if (!opts) {
  3699. exit(1);
  3700. }
  3701. display_remote++;
  3702. break;
  3703. case QEMU_OPTION_writeconfig:
  3704. {
  3705. FILE *fp;
  3706. if (strcmp(optarg, "-") == 0) {
  3707. fp = stdout;
  3708. } else {
  3709. fp = fopen(optarg, "w");
  3710. if (fp == NULL) {
  3711. error_report("open %s: %s", optarg,
  3712. strerror(errno));
  3713. exit(1);
  3714. }
  3715. }
  3716. qemu_config_write(fp);
  3717. if (fp != stdout) {
  3718. fclose(fp);
  3719. }
  3720. break;
  3721. }
  3722. case QEMU_OPTION_qtest:
  3723. qtest_chrdev = optarg;
  3724. break;
  3725. case QEMU_OPTION_qtest_log:
  3726. qtest_log = optarg;
  3727. break;
  3728. case QEMU_OPTION_sandbox:
  3729. opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
  3730. optarg, true);
  3731. if (!opts) {
  3732. exit(1);
  3733. }
  3734. break;
  3735. case QEMU_OPTION_add_fd:
  3736. #ifndef _WIN32
  3737. opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
  3738. optarg, false);
  3739. if (!opts) {
  3740. exit(1);
  3741. }
  3742. #else
  3743. error_report("File descriptor passing is disabled on this "
  3744. "platform");
  3745. exit(1);
  3746. #endif
  3747. break;
  3748. case QEMU_OPTION_object:
  3749. opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
  3750. optarg, true);
  3751. if (!opts) {
  3752. exit(1);
  3753. }
  3754. break;
  3755. case QEMU_OPTION_realtime:
  3756. opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
  3757. optarg, false);
  3758. if (!opts) {
  3759. exit(1);
  3760. }
  3761. enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
  3762. break;
  3763. case QEMU_OPTION_msg:
  3764. opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
  3765. false);
  3766. if (!opts) {
  3767. exit(1);
  3768. }
  3769. configure_msg(opts);
  3770. break;
  3771. case QEMU_OPTION_dump_vmstate:
  3772. if (vmstate_dump_file) {
  3773. error_report("only one '-dump-vmstate' "
  3774. "option may be given");
  3775. exit(1);
  3776. }
  3777. vmstate_dump_file = fopen(optarg, "w");
  3778. if (vmstate_dump_file == NULL) {
  3779. error_report("open %s: %s", optarg, strerror(errno));
  3780. exit(1);
  3781. }
  3782. break;
  3783. default:
  3784. os_parse_cmd_args(popt->index, optarg);
  3785. }
  3786. }
  3787. }
  3788. /*
  3789. * Clear error location left behind by the loop.
  3790. * Best done right after the loop. Do not insert code here!
  3791. */
  3792. loc_set_none();
  3793. replay_configure(icount_opts);
  3794. machine_class = select_machine();
  3795. set_memory_options(&ram_slots, &maxram_size, machine_class);
  3796. os_daemonize();
  3797. rcu_disable_atfork();
  3798. if (pid_file && qemu_create_pidfile(pid_file) != 0) {
  3799. error_report("could not acquire pid file: %s", strerror(errno));
  3800. exit(1);
  3801. }
  3802. if (qemu_init_main_loop(&main_loop_err)) {
  3803. error_report_err(main_loop_err);
  3804. exit(1);
  3805. }
  3806. if (qemu_opts_foreach(qemu_find_opts("sandbox"),
  3807. parse_sandbox, NULL, NULL)) {
  3808. exit(1);
  3809. }
  3810. if (qemu_opts_foreach(qemu_find_opts("name"),
  3811. parse_name, NULL, NULL)) {
  3812. exit(1);
  3813. }
  3814. #ifndef _WIN32
  3815. if (qemu_opts_foreach(qemu_find_opts("add-fd"),
  3816. parse_add_fd, NULL, NULL)) {
  3817. exit(1);
  3818. }
  3819. if (qemu_opts_foreach(qemu_find_opts("add-fd"),
  3820. cleanup_add_fd, NULL, NULL)) {
  3821. exit(1);
  3822. }
  3823. #endif
  3824. current_machine = MACHINE(object_new(object_class_get_name(
  3825. OBJECT_CLASS(machine_class))));
  3826. if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
  3827. exit(0);
  3828. }
  3829. object_property_add_child(object_get_root(), "machine",
  3830. OBJECT(current_machine), &error_abort);
  3831. if (machine_class->minimum_page_bits) {
  3832. if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
  3833. /* This would be a board error: specifying a minimum smaller than
  3834. * a target's compile-time fixed setting.
  3835. */
  3836. g_assert_not_reached();
  3837. }
  3838. }
  3839. cpu_exec_init_all();
  3840. if (machine_class->hw_version) {
  3841. qemu_set_hw_version(machine_class->hw_version);
  3842. }
  3843. if (cpu_model && is_help_option(cpu_model)) {
  3844. list_cpus(stdout, &fprintf, cpu_model);
  3845. exit(0);
  3846. }
  3847. if (!trace_init_backends()) {
  3848. exit(1);
  3849. }
  3850. trace_init_file(trace_file);
  3851. /* Open the logfile at this point and set the log mask if necessary.
  3852. */
  3853. if (log_file) {
  3854. qemu_set_log_filename(log_file, &error_fatal);
  3855. }
  3856. if (log_mask) {
  3857. int mask;
  3858. mask = qemu_str_to_log_mask(log_mask);
  3859. if (!mask) {
  3860. qemu_print_log_usage(stdout);
  3861. exit(1);
  3862. }
  3863. qemu_set_log(mask);
  3864. } else {
  3865. qemu_set_log(0);
  3866. }
  3867. /* add configured firmware directories */
  3868. dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
  3869. for (i = 0; dirs[i] != NULL; i++) {
  3870. qemu_add_data_dir(dirs[i]);
  3871. }
  3872. /* try to find datadir relative to the executable path */
  3873. qemu_add_data_dir(os_find_datadir());
  3874. /* add the datadir specified when building */
  3875. qemu_add_data_dir(CONFIG_QEMU_DATADIR);
  3876. /* -L help lists the data directories and exits. */
  3877. if (list_data_dirs) {
  3878. for (i = 0; i < data_dir_idx; i++) {
  3879. printf("%s\n", data_dir[i]);
  3880. }
  3881. exit(0);
  3882. }
  3883. /* machine_class: default to UP */
  3884. machine_class->max_cpus = machine_class->max_cpus ?: 1;
  3885. machine_class->min_cpus = machine_class->min_cpus ?: 1;
  3886. machine_class->default_cpus = machine_class->default_cpus ?: 1;
  3887. /* default to machine_class->default_cpus */
  3888. smp_cpus = machine_class->default_cpus;
  3889. max_cpus = machine_class->default_cpus;
  3890. smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
  3891. /* sanity-check smp_cpus and max_cpus against machine_class */
  3892. if (smp_cpus < machine_class->min_cpus) {
  3893. error_report("Invalid SMP CPUs %d. The min CPUs "
  3894. "supported by machine '%s' is %d", smp_cpus,
  3895. machine_class->name, machine_class->min_cpus);
  3896. exit(1);
  3897. }
  3898. if (max_cpus > machine_class->max_cpus) {
  3899. error_report("Invalid SMP CPUs %d. The max CPUs "
  3900. "supported by machine '%s' is %d", max_cpus,
  3901. machine_class->name, machine_class->max_cpus);
  3902. exit(1);
  3903. }
  3904. /*
  3905. * Get the default machine options from the machine if it is not already
  3906. * specified either by the configuration file or by the command line.
  3907. */
  3908. if (machine_class->default_machine_opts) {
  3909. qemu_opts_set_defaults(qemu_find_opts("machine"),
  3910. machine_class->default_machine_opts, 0);
  3911. }
  3912. qemu_opts_foreach(qemu_find_opts("device"),
  3913. default_driver_check, NULL, NULL);
  3914. qemu_opts_foreach(qemu_find_opts("global"),
  3915. default_driver_check, NULL, NULL);
  3916. if (!vga_model && !default_vga) {
  3917. vga_interface_type = VGA_DEVICE;
  3918. }
  3919. if (!has_defaults || machine_class->no_serial) {
  3920. default_serial = 0;
  3921. }
  3922. if (!has_defaults || machine_class->no_parallel) {
  3923. default_parallel = 0;
  3924. }
  3925. if (!has_defaults || !machine_class->use_virtcon) {
  3926. default_virtcon = 0;
  3927. }
  3928. if (!has_defaults || !machine_class->use_sclp) {
  3929. default_sclp = 0;
  3930. }
  3931. if (!has_defaults || machine_class->no_floppy) {
  3932. default_floppy = 0;
  3933. }
  3934. if (!has_defaults || machine_class->no_cdrom) {
  3935. default_cdrom = 0;
  3936. }
  3937. if (!has_defaults || machine_class->no_sdcard) {
  3938. default_sdcard = 0;
  3939. }
  3940. if (!has_defaults) {
  3941. default_monitor = 0;
  3942. default_net = 0;
  3943. default_vga = 0;
  3944. }
  3945. if (is_daemonized()) {
  3946. /* According to documentation and historically, -nographic redirects
  3947. * serial port, parallel port and monitor to stdio, which does not work
  3948. * with -daemonize. We can redirect these to null instead, but since
  3949. * -nographic is legacy, let's just error out.
  3950. * We disallow -nographic only if all other ports are not redirected
  3951. * explicitly, to not break existing legacy setups which uses
  3952. * -nographic _and_ redirects all ports explicitly - this is valid
  3953. * usage, -nographic is just a no-op in this case.
  3954. */
  3955. if (nographic
  3956. && (default_parallel || default_serial
  3957. || default_monitor || default_virtcon)) {
  3958. error_report("-nographic cannot be used with -daemonize");
  3959. exit(1);
  3960. }
  3961. #ifdef CONFIG_CURSES
  3962. if (display_type == DT_CURSES) {
  3963. error_report("curses display cannot be used with -daemonize");
  3964. exit(1);
  3965. }
  3966. #endif
  3967. }
  3968. if (nographic) {
  3969. if (default_parallel)
  3970. add_device_config(DEV_PARALLEL, "null");
  3971. if (default_serial && default_monitor) {
  3972. add_device_config(DEV_SERIAL, "mon:stdio");
  3973. } else if (default_virtcon && default_monitor) {
  3974. add_device_config(DEV_VIRTCON, "mon:stdio");
  3975. } else if (default_sclp && default_monitor) {
  3976. add_device_config(DEV_SCLP, "mon:stdio");
  3977. } else {
  3978. if (default_serial)
  3979. add_device_config(DEV_SERIAL, "stdio");
  3980. if (default_virtcon)
  3981. add_device_config(DEV_VIRTCON, "stdio");
  3982. if (default_sclp) {
  3983. add_device_config(DEV_SCLP, "stdio");
  3984. }
  3985. if (default_monitor)
  3986. monitor_parse("stdio", "readline", false);
  3987. }
  3988. } else {
  3989. if (default_serial)
  3990. add_device_config(DEV_SERIAL, "vc:80Cx24C");
  3991. if (default_parallel)
  3992. add_device_config(DEV_PARALLEL, "vc:80Cx24C");
  3993. if (default_monitor)
  3994. monitor_parse("vc:80Cx24C", "readline", false);
  3995. if (default_virtcon)
  3996. add_device_config(DEV_VIRTCON, "vc:80Cx24C");
  3997. if (default_sclp) {
  3998. add_device_config(DEV_SCLP, "vc:80Cx24C");
  3999. }
  4000. }
  4001. #if defined(CONFIG_VNC)
  4002. if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
  4003. display_remote++;
  4004. }
  4005. #endif
  4006. if (display_type == DT_DEFAULT && !display_remote) {
  4007. #if defined(CONFIG_GTK)
  4008. display_type = DT_GTK;
  4009. #elif defined(CONFIG_SDL)
  4010. display_type = DT_SDL;
  4011. #elif defined(CONFIG_COCOA)
  4012. display_type = DT_COCOA;
  4013. #elif defined(CONFIG_VNC)
  4014. vnc_parse("localhost:0,to=99,id=default", &error_abort);
  4015. #else
  4016. display_type = DT_NONE;
  4017. #endif
  4018. }
  4019. if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
  4020. error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
  4021. "for SDL, ignoring option");
  4022. }
  4023. if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
  4024. error_report("-no-quit is only valid for GTK and SDL, "
  4025. "ignoring option");
  4026. }
  4027. if (display_type == DT_GTK) {
  4028. early_gtk_display_init(request_opengl);
  4029. }
  4030. if (display_type == DT_SDL) {
  4031. sdl_display_early_init(request_opengl);
  4032. }
  4033. qemu_console_early_init();
  4034. if (request_opengl == 1 && display_opengl == 0) {
  4035. #if defined(CONFIG_OPENGL)
  4036. error_report("OpenGL is not supported by the display");
  4037. #else
  4038. error_report("OpenGL support is disabled");
  4039. #endif
  4040. exit(1);
  4041. }
  4042. page_size_init();
  4043. socket_init();
  4044. if (qemu_opts_foreach(qemu_find_opts("object"),
  4045. user_creatable_add_opts_foreach,
  4046. object_create_initial, NULL)) {
  4047. exit(1);
  4048. }
  4049. if (qemu_opts_foreach(qemu_find_opts("chardev"),
  4050. chardev_init_func, NULL, NULL)) {
  4051. exit(1);
  4052. }
  4053. #ifdef CONFIG_VIRTFS
  4054. if (qemu_opts_foreach(qemu_find_opts("fsdev"),
  4055. fsdev_init_func, NULL, NULL)) {
  4056. exit(1);
  4057. }
  4058. #endif
  4059. if (qemu_opts_foreach(qemu_find_opts("device"),
  4060. device_help_func, NULL, NULL)) {
  4061. exit(0);
  4062. }
  4063. machine_opts = qemu_get_machine_opts();
  4064. if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
  4065. NULL)) {
  4066. object_unref(OBJECT(current_machine));
  4067. exit(1);
  4068. }
  4069. configure_accelerator(current_machine);
  4070. /*
  4071. * Register all the global properties, including accel properties,
  4072. * machine properties, and user-specified ones.
  4073. */
  4074. register_global_properties(current_machine);
  4075. /*
  4076. * Migration object can only be created after global properties
  4077. * are applied correctly.
  4078. */
  4079. migration_object_init();
  4080. if (qtest_chrdev) {
  4081. qtest_init(qtest_chrdev, qtest_log, &error_fatal);
  4082. }
  4083. machine_opts = qemu_get_machine_opts();
  4084. kernel_filename = qemu_opt_get(machine_opts, "kernel");
  4085. initrd_filename = qemu_opt_get(machine_opts, "initrd");
  4086. kernel_cmdline = qemu_opt_get(machine_opts, "append");
  4087. bios_name = qemu_opt_get(machine_opts, "firmware");
  4088. opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
  4089. if (opts) {
  4090. boot_order = qemu_opt_get(opts, "order");
  4091. if (boot_order) {
  4092. validate_bootdevices(boot_order, &error_fatal);
  4093. }
  4094. boot_once = qemu_opt_get(opts, "once");
  4095. if (boot_once) {
  4096. validate_bootdevices(boot_once, &error_fatal);
  4097. }
  4098. boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
  4099. boot_strict = qemu_opt_get_bool(opts, "strict", false);
  4100. }
  4101. if (!boot_order) {
  4102. boot_order = machine_class->default_boot_order;
  4103. }
  4104. if (!kernel_cmdline) {
  4105. kernel_cmdline = "";
  4106. current_machine->kernel_cmdline = (char *)kernel_cmdline;
  4107. }
  4108. linux_boot = (kernel_filename != NULL);
  4109. if (!linux_boot && *kernel_cmdline != '\0') {
  4110. error_report("-append only allowed with -kernel option");
  4111. exit(1);
  4112. }
  4113. if (!linux_boot && initrd_filename != NULL) {
  4114. error_report("-initrd only allowed with -kernel option");
  4115. exit(1);
  4116. }
  4117. if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
  4118. /* fall back to the -kernel/-append */
  4119. semihosting_arg_fallback(kernel_filename, kernel_cmdline);
  4120. }
  4121. os_set_line_buffering();
  4122. /* spice needs the timers to be initialized by this point */
  4123. qemu_spice_init();
  4124. cpu_ticks_init();
  4125. if (icount_opts) {
  4126. if (!tcg_enabled()) {
  4127. error_report("-icount is not allowed with hardware virtualization");
  4128. exit(1);
  4129. }
  4130. configure_icount(icount_opts, &error_abort);
  4131. qemu_opts_del(icount_opts);
  4132. }
  4133. if (tcg_enabled()) {
  4134. qemu_tcg_configure(accel_opts, &error_fatal);
  4135. }
  4136. if (default_net) {
  4137. QemuOptsList *net = qemu_find_opts("net");
  4138. qemu_opts_set(net, NULL, "type", "nic", &error_abort);
  4139. #ifdef CONFIG_SLIRP
  4140. qemu_opts_set(net, NULL, "type", "user", &error_abort);
  4141. #endif
  4142. }
  4143. colo_info_init();
  4144. if (net_init_clients() < 0) {
  4145. exit(1);
  4146. }
  4147. if (qemu_opts_foreach(qemu_find_opts("object"),
  4148. user_creatable_add_opts_foreach,
  4149. object_create_delayed, NULL)) {
  4150. exit(1);
  4151. }
  4152. if (tpm_init() < 0) {
  4153. exit(1);
  4154. }
  4155. /* init the bluetooth world */
  4156. if (foreach_device_config(DEV_BT, bt_parse))
  4157. exit(1);
  4158. if (!xen_enabled()) {
  4159. /* On 32-bit hosts, QEMU is limited by virtual address space */
  4160. if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
  4161. error_report("at most 2047 MB RAM can be simulated");
  4162. exit(1);
  4163. }
  4164. }
  4165. blk_mig_init();
  4166. ram_mig_init();
  4167. /* If the currently selected machine wishes to override the units-per-bus
  4168. * property of its default HBA interface type, do so now. */
  4169. if (machine_class->units_per_default_bus) {
  4170. override_max_devs(machine_class->block_default_type,
  4171. machine_class->units_per_default_bus);
  4172. }
  4173. /* open the virtual block devices */
  4174. while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
  4175. BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
  4176. QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
  4177. loc_push_restore(&bdo->loc);
  4178. qmp_blockdev_add(bdo->bdo, &error_fatal);
  4179. loc_pop(&bdo->loc);
  4180. qapi_free_BlockdevOptions(bdo->bdo);
  4181. g_free(bdo);
  4182. }
  4183. if (snapshot || replay_mode != REPLAY_MODE_NONE) {
  4184. qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
  4185. NULL, NULL);
  4186. }
  4187. if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
  4188. &machine_class->block_default_type, NULL)) {
  4189. exit(1);
  4190. }
  4191. default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
  4192. CDROM_OPTS);
  4193. default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
  4194. default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
  4195. if (qemu_opts_foreach(qemu_find_opts("mon"),
  4196. mon_init_func, NULL, NULL)) {
  4197. exit(1);
  4198. }
  4199. if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
  4200. exit(1);
  4201. if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
  4202. exit(1);
  4203. if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
  4204. exit(1);
  4205. if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
  4206. exit(1);
  4207. }
  4208. if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
  4209. exit(1);
  4210. /* If no default VGA is requested, the default is "none". */
  4211. if (default_vga) {
  4212. if (machine_class->default_display) {
  4213. vga_model = machine_class->default_display;
  4214. } else if (vga_interface_available(VGA_CIRRUS)) {
  4215. vga_model = "cirrus";
  4216. } else if (vga_interface_available(VGA_STD)) {
  4217. vga_model = "std";
  4218. }
  4219. }
  4220. if (vga_model) {
  4221. select_vgahw(vga_model);
  4222. }
  4223. if (watchdog) {
  4224. i = select_watchdog(watchdog);
  4225. if (i > 0)
  4226. exit (i == 1 ? 1 : 0);
  4227. }
  4228. /* This checkpoint is required by replay to separate prior clock
  4229. reading from the other reads, because timer polling functions query
  4230. clock values from the log. */
  4231. replay_checkpoint(CHECKPOINT_INIT);
  4232. qdev_machine_init();
  4233. current_machine->ram_size = ram_size;
  4234. current_machine->maxram_size = maxram_size;
  4235. current_machine->ram_slots = ram_slots;
  4236. current_machine->boot_order = boot_order;
  4237. current_machine->cpu_model = cpu_model;
  4238. parse_numa_opts(current_machine);
  4239. /* parse features once if machine provides default cpu_type */
  4240. if (machine_class->default_cpu_type) {
  4241. current_machine->cpu_type = machine_class->default_cpu_type;
  4242. if (cpu_model) {
  4243. current_machine->cpu_type =
  4244. cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
  4245. }
  4246. }
  4247. machine_run_board_init(current_machine);
  4248. realtime_init();
  4249. soundhw_init();
  4250. if (hax_enabled()) {
  4251. hax_sync_vcpus();
  4252. }
  4253. if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
  4254. parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
  4255. exit(1);
  4256. }
  4257. /* init USB devices */
  4258. if (machine_usb(current_machine)) {
  4259. if (foreach_device_config(DEV_USB, usb_parse) < 0)
  4260. exit(1);
  4261. }
  4262. /* Check if IGD GFX passthrough. */
  4263. igd_gfx_passthru();
  4264. /* init generic devices */
  4265. rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
  4266. if (qemu_opts_foreach(qemu_find_opts("device"),
  4267. device_init_func, NULL, NULL)) {
  4268. exit(1);
  4269. }
  4270. cpu_synchronize_all_post_init();
  4271. rom_reset_order_override();
  4272. /*
  4273. * Create frontends for -drive if=scsi leftovers.
  4274. * Normally, frontends for -drive get created by machine
  4275. * initialization for onboard SCSI HBAs. However, we create a few
  4276. * more ever since SCSI qdevification, but this is pretty much an
  4277. * implementation accident, and deprecated.
  4278. */
  4279. scsi_legacy_handle_cmdline();
  4280. /* Did we create any drives that we failed to create a device for? */
  4281. drive_check_orphaned();
  4282. /* Don't warn about the default network setup that you get if
  4283. * no command line -net or -netdev options are specified. There
  4284. * are two cases that we would otherwise complain about:
  4285. * (1) board doesn't support a NIC but the implicit "-net nic"
  4286. * requested one
  4287. * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
  4288. * sets up a nic that isn't connected to anything.
  4289. */
  4290. if (!default_net) {
  4291. net_check_clients();
  4292. }
  4293. if (boot_once) {
  4294. qemu_boot_set(boot_once, &error_fatal);
  4295. qemu_register_reset(restore_boot_order, g_strdup(boot_order));
  4296. }
  4297. ds = init_displaystate();
  4298. /* init local displays */
  4299. switch (display_type) {
  4300. case DT_CURSES:
  4301. curses_display_init(ds, full_screen);
  4302. break;
  4303. case DT_SDL:
  4304. sdl_display_init(ds, full_screen, no_frame);
  4305. break;
  4306. case DT_COCOA:
  4307. cocoa_display_init(ds, full_screen);
  4308. break;
  4309. case DT_GTK:
  4310. gtk_display_init(ds, full_screen, grab_on_hover);
  4311. break;
  4312. default:
  4313. break;
  4314. }
  4315. /* must be after terminal init, SDL library changes signal handlers */
  4316. os_setup_signal_handling();
  4317. /* init remote displays */
  4318. #ifdef CONFIG_VNC
  4319. qemu_opts_foreach(qemu_find_opts("vnc"),
  4320. vnc_init_func, NULL, NULL);
  4321. #endif
  4322. if (using_spice) {
  4323. qemu_spice_display_init();
  4324. }
  4325. #ifdef CONFIG_OPENGL_DMABUF
  4326. if (display_type == DT_EGL) {
  4327. egl_headless_init();
  4328. }
  4329. #endif
  4330. if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
  4331. exit(1);
  4332. }
  4333. qdev_machine_creation_done();
  4334. /* TODO: once all bus devices are qdevified, this should be done
  4335. * when bus is created by qdev.c */
  4336. qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
  4337. qemu_run_machine_init_done_notifiers();
  4338. if (rom_check_and_register_reset() != 0) {
  4339. error_report("rom check and register reset failed");
  4340. exit(1);
  4341. }
  4342. replay_start();
  4343. /* This checkpoint is required by replay to separate prior clock
  4344. reading from the other reads, because timer polling functions query
  4345. clock values from the log. */
  4346. replay_checkpoint(CHECKPOINT_RESET);
  4347. qemu_system_reset(SHUTDOWN_CAUSE_NONE);
  4348. register_global_state();
  4349. if (replay_mode != REPLAY_MODE_NONE) {
  4350. replay_vmstate_init();
  4351. } else if (loadvm) {
  4352. Error *local_err = NULL;
  4353. if (load_snapshot(loadvm, &local_err) < 0) {
  4354. error_report_err(local_err);
  4355. autostart = 0;
  4356. }
  4357. }
  4358. qdev_prop_check_globals();
  4359. if (vmstate_dump_file) {
  4360. /* dump and exit */
  4361. dump_vmstate_json_to_file(vmstate_dump_file);
  4362. return 0;
  4363. }
  4364. if (incoming) {
  4365. Error *local_err = NULL;
  4366. qemu_start_incoming_migration(incoming, &local_err);
  4367. if (local_err) {
  4368. error_reportf_err(local_err, "-incoming %s: ", incoming);
  4369. exit(1);
  4370. }
  4371. } else if (autostart) {
  4372. vm_start();
  4373. }
  4374. os_setup_post();
  4375. main_loop();
  4376. replay_disable_events();
  4377. iothread_stop_all();
  4378. pause_all_vcpus();
  4379. bdrv_close_all();
  4380. res_free();
  4381. /* vhost-user must be cleaned up before chardevs. */
  4382. tpm_cleanup();
  4383. net_cleanup();
  4384. audio_cleanup();
  4385. monitor_cleanup();
  4386. qemu_chr_cleanup();
  4387. user_creatable_cleanup();
  4388. /* TODO: unref root container, check all devices are ok */
  4389. return 0;
  4390. }