vl.c 138 KB

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