vl.c 133 KB

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