vl.c 125 KB

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