You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vl.c 133KB

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