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

vl.c 134KB

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