2
0

vl.c 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853
  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/help-texts.h"
  26. #include "qemu/datadir.h"
  27. #include "qemu/units.h"
  28. #include "qemu/module.h"
  29. #include "exec/cpu-common.h"
  30. #include "exec/page-vary.h"
  31. #include "hw/qdev-properties.h"
  32. #include "qapi/compat-policy.h"
  33. #include "qapi/error.h"
  34. #include "qobject/qdict.h"
  35. #include "qobject/qstring.h"
  36. #include "qobject/qjson.h"
  37. #include "qemu-version.h"
  38. #include "qemu/cutils.h"
  39. #include "qemu/help_option.h"
  40. #include "qemu/hw-version.h"
  41. #include "qemu/uuid.h"
  42. #include "system/reset.h"
  43. #include "system/runstate.h"
  44. #include "system/runstate-action.h"
  45. #include "system/seccomp.h"
  46. #include "system/tcg.h"
  47. #include "system/xen.h"
  48. #include "qemu/error-report.h"
  49. #include "qemu/sockets.h"
  50. #include "qemu/accel.h"
  51. #include "qemu/async-teardown.h"
  52. #include "hw/usb.h"
  53. #include "hw/isa/isa.h"
  54. #include "hw/scsi/scsi.h"
  55. #include "hw/sd/sd.h"
  56. #include "hw/display/vga.h"
  57. #include "hw/firmware/smbios.h"
  58. #include "hw/acpi/acpi.h"
  59. #include "hw/xen/xen.h"
  60. #include "hw/loader.h"
  61. #include "monitor/qdev.h"
  62. #include "net/net.h"
  63. #include "net/slirp.h"
  64. #include "monitor/monitor.h"
  65. #include "ui/console.h"
  66. #include "ui/input.h"
  67. #include "system/system.h"
  68. #include "system/numa.h"
  69. #include "system/hostmem.h"
  70. #include "exec/gdbstub.h"
  71. #include "gdbstub/enums.h"
  72. #include "qemu/timer.h"
  73. #include "chardev/char.h"
  74. #include "qemu/bitmap.h"
  75. #include "qemu/log.h"
  76. #include "system/blockdev.h"
  77. #include "hw/block/block.h"
  78. #include "hw/i386/x86.h"
  79. #include "hw/i386/pc.h"
  80. #include "hw/core/cpu.h"
  81. #include "migration/cpr.h"
  82. #include "migration/misc.h"
  83. #include "migration/snapshot.h"
  84. #include "system/tpm.h"
  85. #include "system/dma.h"
  86. #include "hw/audio/soundhw.h"
  87. #include "audio/audio.h"
  88. #include "system/cpus.h"
  89. #include "system/cpu-timers.h"
  90. #include "migration/colo.h"
  91. #include "migration/postcopy-ram.h"
  92. #include "system/kvm.h"
  93. #include "qapi/qobject-input-visitor.h"
  94. #include "qemu/option.h"
  95. #include "qemu/config-file.h"
  96. #include "qemu/main-loop.h"
  97. #ifdef CONFIG_VIRTFS
  98. #include "fsdev/qemu-fsdev.h"
  99. #endif
  100. #include "system/qtest.h"
  101. #ifdef CONFIG_TCG
  102. #include "tcg/perf.h"
  103. #endif
  104. #include "disas/disas.h"
  105. #include "trace.h"
  106. #include "trace/control.h"
  107. #include "qemu/plugin.h"
  108. #include "qemu/queue.h"
  109. #include "system/arch_init.h"
  110. #include "system/confidential-guest-support.h"
  111. #include "ui/qemu-spice.h"
  112. #include "qapi/string-input-visitor.h"
  113. #include "qapi/opts-visitor.h"
  114. #include "qapi/clone-visitor.h"
  115. #include "qom/object_interfaces.h"
  116. #include "semihosting/semihost.h"
  117. #include "crypto/init.h"
  118. #include "system/replay.h"
  119. #include "qapi/qapi-events-run-state.h"
  120. #include "qapi/qapi-types-audio.h"
  121. #include "qapi/qapi-visit-audio.h"
  122. #include "qapi/qapi-visit-block-core.h"
  123. #include "qapi/qapi-visit-compat.h"
  124. #include "qapi/qapi-visit-machine.h"
  125. #include "qapi/qapi-visit-migration.h"
  126. #include "qapi/qapi-visit-ui.h"
  127. #include "qapi/qapi-commands-block-core.h"
  128. #include "qapi/qapi-commands-migration.h"
  129. #include "qapi/qapi-commands-misc.h"
  130. #include "qapi/qapi-visit-qom.h"
  131. #include "qapi/qapi-commands-ui.h"
  132. #include "block/qdict.h"
  133. #include "qapi/qmp/qerror.h"
  134. #include "system/iothread.h"
  135. #include "qemu/guest-random.h"
  136. #include "qemu/keyval.h"
  137. #define MAX_VIRTIO_CONSOLES 1
  138. typedef struct BlockdevOptionsQueueEntry {
  139. BlockdevOptions *bdo;
  140. Location loc;
  141. QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
  142. } BlockdevOptionsQueueEntry;
  143. typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
  144. typedef struct ObjectOption {
  145. ObjectOptions *opts;
  146. QTAILQ_ENTRY(ObjectOption) next;
  147. } ObjectOption;
  148. typedef struct DeviceOption {
  149. QDict *opts;
  150. Location loc;
  151. QTAILQ_ENTRY(DeviceOption) next;
  152. } DeviceOption;
  153. static const char *cpu_option;
  154. static const char *mem_path;
  155. static const char *incoming;
  156. static const char *incoming_str[MIGRATION_CHANNEL_TYPE__MAX];
  157. static MigrationChannel *incoming_channels[MIGRATION_CHANNEL_TYPE__MAX];
  158. static const char *loadvm;
  159. static const char *accelerators;
  160. static bool have_custom_ram_size;
  161. static const char *ram_memdev_id;
  162. static QDict *machine_opts_dict;
  163. static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
  164. static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
  165. static int display_remote;
  166. static int snapshot;
  167. static bool preconfig_requested;
  168. static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
  169. static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
  170. static bool nographic = false;
  171. static int mem_prealloc; /* force preallocation of physical target memory */
  172. static const char *vga_model = NULL;
  173. static DisplayOptions dpy;
  174. static int num_serial_hds;
  175. static Chardev **serial_hds;
  176. static const char *log_mask;
  177. static const char *log_file;
  178. static bool list_data_dirs;
  179. static const char *qtest_chrdev;
  180. static const char *qtest_log;
  181. static int has_defaults = 1;
  182. static int default_audio = 1;
  183. static int default_serial = 1;
  184. static int default_parallel = 1;
  185. static int default_monitor = 1;
  186. static int default_floppy = 1;
  187. static int default_cdrom = 1;
  188. static bool auto_create_sdcard = true;
  189. static int default_vga = 1;
  190. static int default_net = 1;
  191. static const struct {
  192. const char *driver;
  193. int *flag;
  194. } default_list[] = {
  195. { .driver = "xen-console", .flag = &default_serial },
  196. { .driver = "isa-serial", .flag = &default_serial },
  197. { .driver = "isa-parallel", .flag = &default_parallel },
  198. { .driver = "isa-fdc", .flag = &default_floppy },
  199. { .driver = "floppy", .flag = &default_floppy },
  200. { .driver = "ide-cd", .flag = &default_cdrom },
  201. { .driver = "ide-hd", .flag = &default_cdrom },
  202. { .driver = "scsi-cd", .flag = &default_cdrom },
  203. { .driver = "scsi-hd", .flag = &default_cdrom },
  204. { .driver = "VGA", .flag = &default_vga },
  205. { .driver = "isa-vga", .flag = &default_vga },
  206. { .driver = "cirrus-vga", .flag = &default_vga },
  207. { .driver = "isa-cirrus-vga", .flag = &default_vga },
  208. { .driver = "vmware-svga", .flag = &default_vga },
  209. { .driver = "qxl-vga", .flag = &default_vga },
  210. { .driver = "virtio-vga", .flag = &default_vga },
  211. { .driver = "ati-vga", .flag = &default_vga },
  212. { .driver = "vhost-user-vga", .flag = &default_vga },
  213. { .driver = "virtio-vga-gl", .flag = &default_vga },
  214. { .driver = "virtio-vga-rutabaga", .flag = &default_vga },
  215. };
  216. static QemuOptsList qemu_rtc_opts = {
  217. .name = "rtc",
  218. .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
  219. .merge_lists = true,
  220. .desc = {
  221. {
  222. .name = "base",
  223. .type = QEMU_OPT_STRING,
  224. },{
  225. .name = "clock",
  226. .type = QEMU_OPT_STRING,
  227. },{
  228. .name = "driftfix",
  229. .type = QEMU_OPT_STRING,
  230. },
  231. { /* end of list */ }
  232. },
  233. };
  234. static QemuOptsList qemu_option_rom_opts = {
  235. .name = "option-rom",
  236. .implied_opt_name = "romfile",
  237. .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
  238. .desc = {
  239. {
  240. .name = "bootindex",
  241. .type = QEMU_OPT_NUMBER,
  242. }, {
  243. .name = "romfile",
  244. .type = QEMU_OPT_STRING,
  245. },
  246. { /* end of list */ }
  247. },
  248. };
  249. static QemuOptsList qemu_accel_opts = {
  250. .name = "accel",
  251. .implied_opt_name = "accel",
  252. .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
  253. .desc = {
  254. /*
  255. * no elements => accept any
  256. * sanity checking will happen later
  257. * when setting accelerator properties
  258. */
  259. { }
  260. },
  261. };
  262. static QemuOptsList qemu_boot_opts = {
  263. .name = "boot-opts",
  264. .implied_opt_name = "order",
  265. .merge_lists = true,
  266. .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
  267. .desc = {
  268. {
  269. .name = "order",
  270. .type = QEMU_OPT_STRING,
  271. }, {
  272. .name = "once",
  273. .type = QEMU_OPT_STRING,
  274. }, {
  275. .name = "menu",
  276. .type = QEMU_OPT_BOOL,
  277. }, {
  278. .name = "splash",
  279. .type = QEMU_OPT_STRING,
  280. }, {
  281. .name = "splash-time",
  282. .type = QEMU_OPT_NUMBER,
  283. }, {
  284. .name = "reboot-timeout",
  285. .type = QEMU_OPT_NUMBER,
  286. }, {
  287. .name = "strict",
  288. .type = QEMU_OPT_BOOL,
  289. },
  290. { /*End of list */ }
  291. },
  292. };
  293. static QemuOptsList qemu_add_fd_opts = {
  294. .name = "add-fd",
  295. .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
  296. .desc = {
  297. {
  298. .name = "fd",
  299. .type = QEMU_OPT_NUMBER,
  300. .help = "file descriptor of which a duplicate is added to fd set",
  301. },{
  302. .name = "set",
  303. .type = QEMU_OPT_NUMBER,
  304. .help = "ID of the fd set to add fd to",
  305. },{
  306. .name = "opaque",
  307. .type = QEMU_OPT_STRING,
  308. .help = "free-form string used to describe fd",
  309. },
  310. { /* end of list */ }
  311. },
  312. };
  313. static QemuOptsList qemu_object_opts = {
  314. .name = "object",
  315. .implied_opt_name = "qom-type",
  316. .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
  317. .desc = {
  318. { }
  319. },
  320. };
  321. static QemuOptsList qemu_tpmdev_opts = {
  322. .name = "tpmdev",
  323. .implied_opt_name = "type",
  324. .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
  325. .desc = {
  326. /* options are defined in the TPM backends */
  327. { /* end of list */ }
  328. },
  329. };
  330. static QemuOptsList qemu_overcommit_opts = {
  331. .name = "overcommit",
  332. .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
  333. .desc = {
  334. {
  335. .name = "mem-lock",
  336. .type = QEMU_OPT_STRING,
  337. },
  338. {
  339. .name = "cpu-pm",
  340. .type = QEMU_OPT_BOOL,
  341. },
  342. { /* end of list */ }
  343. },
  344. };
  345. static QemuOptsList qemu_msg_opts = {
  346. .name = "msg",
  347. .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
  348. .desc = {
  349. {
  350. .name = "timestamp",
  351. .type = QEMU_OPT_BOOL,
  352. },
  353. {
  354. .name = "guest-name",
  355. .type = QEMU_OPT_BOOL,
  356. .help = "Prepends guest name for error messages but only if "
  357. "-name guest is set otherwise option is ignored\n",
  358. },
  359. { /* end of list */ }
  360. },
  361. };
  362. static QemuOptsList qemu_name_opts = {
  363. .name = "name",
  364. .implied_opt_name = "guest",
  365. .merge_lists = true,
  366. .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
  367. .desc = {
  368. {
  369. .name = "guest",
  370. .type = QEMU_OPT_STRING,
  371. .help = "Sets the name of the guest.\n"
  372. "This name will be displayed in the SDL window caption.\n"
  373. "The name will also be used for the VNC server",
  374. }, {
  375. .name = "process",
  376. .type = QEMU_OPT_STRING,
  377. .help = "Sets the name of the QEMU process, as shown in top etc",
  378. }, {
  379. .name = "debug-threads",
  380. .type = QEMU_OPT_BOOL,
  381. .help = "When enabled, name the individual threads; defaults off.\n"
  382. "NOTE: The thread names are for debugging and not a\n"
  383. "stable API.",
  384. },
  385. { /* End of list */ }
  386. },
  387. };
  388. static QemuOptsList qemu_mem_opts = {
  389. .name = "memory",
  390. .implied_opt_name = "size",
  391. .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
  392. .merge_lists = true,
  393. .desc = {
  394. {
  395. .name = "size",
  396. .type = QEMU_OPT_SIZE,
  397. },
  398. {
  399. .name = "slots",
  400. .type = QEMU_OPT_NUMBER,
  401. },
  402. {
  403. .name = "maxmem",
  404. .type = QEMU_OPT_SIZE,
  405. },
  406. { /* end of list */ }
  407. },
  408. };
  409. static QemuOptsList qemu_icount_opts = {
  410. .name = "icount",
  411. .implied_opt_name = "shift",
  412. .merge_lists = true,
  413. .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
  414. .desc = {
  415. {
  416. .name = "shift",
  417. .type = QEMU_OPT_STRING,
  418. }, {
  419. .name = "align",
  420. .type = QEMU_OPT_BOOL,
  421. }, {
  422. .name = "sleep",
  423. .type = QEMU_OPT_BOOL,
  424. }, {
  425. .name = "rr",
  426. .type = QEMU_OPT_STRING,
  427. }, {
  428. .name = "rrfile",
  429. .type = QEMU_OPT_STRING,
  430. }, {
  431. .name = "rrsnapshot",
  432. .type = QEMU_OPT_STRING,
  433. },
  434. { /* end of list */ }
  435. },
  436. };
  437. static QemuOptsList qemu_fw_cfg_opts = {
  438. .name = "fw_cfg",
  439. .implied_opt_name = "name",
  440. .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
  441. .desc = {
  442. {
  443. .name = "name",
  444. .type = QEMU_OPT_STRING,
  445. .help = "Sets the fw_cfg name of the blob to be inserted",
  446. }, {
  447. .name = "file",
  448. .type = QEMU_OPT_STRING,
  449. .help = "Sets the name of the file from which "
  450. "the fw_cfg blob will be loaded",
  451. }, {
  452. .name = "string",
  453. .type = QEMU_OPT_STRING,
  454. .help = "Sets content of the blob to be inserted from a string",
  455. }, {
  456. .name = "gen_id",
  457. .type = QEMU_OPT_STRING,
  458. .help = "Sets id of the object generating the fw_cfg blob "
  459. "to be inserted",
  460. },
  461. { /* end of list */ }
  462. },
  463. };
  464. static QemuOptsList qemu_action_opts = {
  465. .name = "action",
  466. .merge_lists = true,
  467. .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
  468. .desc = {
  469. {
  470. .name = "shutdown",
  471. .type = QEMU_OPT_STRING,
  472. },{
  473. .name = "reboot",
  474. .type = QEMU_OPT_STRING,
  475. },{
  476. .name = "panic",
  477. .type = QEMU_OPT_STRING,
  478. },{
  479. .name = "watchdog",
  480. .type = QEMU_OPT_STRING,
  481. },
  482. { /* end of list */ }
  483. },
  484. };
  485. const char *qemu_get_vm_name(void)
  486. {
  487. return qemu_name;
  488. }
  489. static void default_driver_disable(const char *driver)
  490. {
  491. int i;
  492. if (!driver) {
  493. return;
  494. }
  495. for (i = 0; i < ARRAY_SIZE(default_list); i++) {
  496. if (strcmp(default_list[i].driver, driver) != 0)
  497. continue;
  498. *(default_list[i].flag) = 0;
  499. }
  500. }
  501. static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
  502. {
  503. const char *driver = qemu_opt_get(opts, "driver");
  504. default_driver_disable(driver);
  505. return 0;
  506. }
  507. static void default_driver_check_json(void)
  508. {
  509. DeviceOption *opt;
  510. QTAILQ_FOREACH(opt, &device_opts, next) {
  511. const char *driver = qdict_get_try_str(opt->opts, "driver");
  512. default_driver_disable(driver);
  513. }
  514. }
  515. static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
  516. {
  517. const char *proc_name;
  518. if (qemu_opt_get(opts, "debug-threads")) {
  519. qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
  520. }
  521. qemu_name = qemu_opt_get(opts, "guest");
  522. proc_name = qemu_opt_get(opts, "process");
  523. if (proc_name) {
  524. os_set_proc_name(proc_name);
  525. }
  526. return 0;
  527. }
  528. bool defaults_enabled(void)
  529. {
  530. return has_defaults;
  531. }
  532. #ifndef _WIN32
  533. static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
  534. {
  535. int fd, dupfd, flags;
  536. int64_t fdset_id;
  537. const char *fd_opaque = NULL;
  538. AddfdInfo *fdinfo;
  539. fd = qemu_opt_get_number(opts, "fd", -1);
  540. fdset_id = qemu_opt_get_number(opts, "set", -1);
  541. fd_opaque = qemu_opt_get(opts, "opaque");
  542. if (fd < 0) {
  543. error_setg(errp, "fd option is required and must be non-negative");
  544. return -1;
  545. }
  546. if (fd <= STDERR_FILENO) {
  547. error_setg(errp, "fd cannot be a standard I/O stream");
  548. return -1;
  549. }
  550. /*
  551. * All fds inherited across exec() necessarily have FD_CLOEXEC
  552. * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
  553. */
  554. flags = fcntl(fd, F_GETFD);
  555. if (flags == -1 || (flags & FD_CLOEXEC)) {
  556. error_setg(errp, "fd is not valid or already in use");
  557. return -1;
  558. }
  559. if (fdset_id < 0) {
  560. error_setg(errp, "set option is required and must be non-negative");
  561. return -1;
  562. }
  563. #ifdef F_DUPFD_CLOEXEC
  564. dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  565. #else
  566. dupfd = dup(fd);
  567. if (dupfd != -1) {
  568. qemu_set_cloexec(dupfd);
  569. }
  570. #endif
  571. if (dupfd == -1) {
  572. error_setg(errp, "error duplicating fd: %s", strerror(errno));
  573. return -1;
  574. }
  575. /* add the duplicate fd, and optionally the opaque string, to the fd set */
  576. fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque,
  577. &error_abort);
  578. g_free(fdinfo);
  579. return 0;
  580. }
  581. static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
  582. {
  583. int fd;
  584. fd = qemu_opt_get_number(opts, "fd", -1);
  585. close(fd);
  586. return 0;
  587. }
  588. #endif
  589. /***********************************************************/
  590. /* QEMU Block devices */
  591. #define HD_OPTS "media=disk"
  592. #define CDROM_OPTS "media=cdrom"
  593. #define FD_OPTS ""
  594. #define PFLASH_OPTS ""
  595. #define MTD_OPTS ""
  596. #define SD_OPTS ""
  597. static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
  598. {
  599. BlockInterfaceType *block_default_type = opaque;
  600. return drive_new(opts, *block_default_type, errp) == NULL;
  601. }
  602. static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
  603. {
  604. if (qemu_opt_get(opts, "snapshot") == NULL) {
  605. qemu_opt_set(opts, "snapshot", "on", &error_abort);
  606. }
  607. return 0;
  608. }
  609. static void default_drive(int enable, int snapshot, BlockInterfaceType type,
  610. int index, const char *optstr)
  611. {
  612. QemuOpts *opts;
  613. DriveInfo *dinfo;
  614. if (!enable || drive_get_by_index(type, index)) {
  615. return;
  616. }
  617. opts = drive_add(type, index, NULL, optstr);
  618. if (snapshot) {
  619. drive_enable_snapshot(NULL, opts, NULL);
  620. }
  621. dinfo = drive_new(opts, type, &error_abort);
  622. dinfo->is_default = true;
  623. }
  624. static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
  625. MachineClass *machine_class, int snapshot)
  626. {
  627. /*
  628. * If the currently selected machine wishes to override the
  629. * units-per-bus property of its default HBA interface type, do so
  630. * now.
  631. */
  632. if (machine_class->units_per_default_bus) {
  633. override_max_devs(machine_class->block_default_type,
  634. machine_class->units_per_default_bus);
  635. }
  636. /* open the virtual block devices */
  637. while (!QSIMPLEQ_EMPTY(bdo_queue)) {
  638. BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
  639. QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
  640. loc_push_restore(&bdo->loc);
  641. qmp_blockdev_add(bdo->bdo, &error_fatal);
  642. loc_pop(&bdo->loc);
  643. qapi_free_BlockdevOptions(bdo->bdo);
  644. g_free(bdo);
  645. }
  646. if (snapshot) {
  647. qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
  648. NULL, NULL);
  649. }
  650. if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
  651. &machine_class->block_default_type, &error_fatal)) {
  652. /* We printed help */
  653. exit(0);
  654. }
  655. default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
  656. CDROM_OPTS);
  657. default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
  658. default_drive(auto_create_sdcard, snapshot, IF_SD, 0, SD_OPTS);
  659. }
  660. static QemuOptsList qemu_smp_opts = {
  661. .name = "smp-opts",
  662. .implied_opt_name = "cpus",
  663. .merge_lists = true,
  664. .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
  665. .desc = {
  666. {
  667. .name = "cpus",
  668. .type = QEMU_OPT_NUMBER,
  669. }, {
  670. .name = "drawers",
  671. .type = QEMU_OPT_NUMBER,
  672. }, {
  673. .name = "books",
  674. .type = QEMU_OPT_NUMBER,
  675. }, {
  676. .name = "sockets",
  677. .type = QEMU_OPT_NUMBER,
  678. }, {
  679. .name = "dies",
  680. .type = QEMU_OPT_NUMBER,
  681. }, {
  682. .name = "clusters",
  683. .type = QEMU_OPT_NUMBER,
  684. }, {
  685. .name = "modules",
  686. .type = QEMU_OPT_NUMBER,
  687. }, {
  688. .name = "cores",
  689. .type = QEMU_OPT_NUMBER,
  690. }, {
  691. .name = "threads",
  692. .type = QEMU_OPT_NUMBER,
  693. }, {
  694. .name = "maxcpus",
  695. .type = QEMU_OPT_NUMBER,
  696. },
  697. { /*End of list */ }
  698. },
  699. };
  700. #if defined(CONFIG_POSIX)
  701. static QemuOptsList qemu_run_with_opts = {
  702. .name = "run-with",
  703. .head = QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts.head),
  704. .desc = {
  705. #if defined(CONFIG_LINUX)
  706. {
  707. .name = "async-teardown",
  708. .type = QEMU_OPT_BOOL,
  709. },
  710. #endif
  711. {
  712. .name = "chroot",
  713. .type = QEMU_OPT_STRING,
  714. },
  715. {
  716. .name = "user",
  717. .type = QEMU_OPT_STRING,
  718. },
  719. { /* end of list */ }
  720. },
  721. };
  722. #define qemu_add_run_with_opts() qemu_add_opts(&qemu_run_with_opts)
  723. #else
  724. #define qemu_add_run_with_opts()
  725. #endif /* CONFIG_POSIX */
  726. static void realtime_init(void)
  727. {
  728. if (should_mlock(mlock_state)) {
  729. if (os_mlock(is_mlock_on_fault(mlock_state)) < 0) {
  730. error_report("locking memory failed");
  731. exit(1);
  732. }
  733. }
  734. }
  735. static void configure_msg(QemuOpts *opts)
  736. {
  737. message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
  738. error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
  739. }
  740. /***********************************************************/
  741. /* USB devices */
  742. static bool usb_parse(const char *cmdline, Error **errp)
  743. {
  744. g_assert(machine_usb(current_machine));
  745. if (!usbdevice_create(cmdline)) {
  746. error_setg(errp, "could not add USB device '%s'", cmdline);
  747. return false;
  748. }
  749. return true;
  750. }
  751. /***********************************************************/
  752. /* machine registration */
  753. static MachineClass *find_machine(const char *name, GSList *machines)
  754. {
  755. GSList *el;
  756. for (el = machines; el; el = el->next) {
  757. MachineClass *mc = el->data;
  758. if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
  759. return mc;
  760. }
  761. }
  762. return NULL;
  763. }
  764. static MachineClass *find_default_machine(GSList *machines)
  765. {
  766. GSList *el;
  767. MachineClass *default_machineclass = NULL;
  768. for (el = machines; el; el = el->next) {
  769. MachineClass *mc = el->data;
  770. if (mc->is_default) {
  771. assert(default_machineclass == NULL && "Multiple default machines");
  772. default_machineclass = mc;
  773. }
  774. }
  775. return default_machineclass;
  776. }
  777. static void version(void)
  778. {
  779. printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
  780. QEMU_COPYRIGHT "\n");
  781. }
  782. static void help(int exitcode)
  783. {
  784. version();
  785. printf("usage: %s [options] [disk_image]\n\n"
  786. "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
  787. g_get_prgname());
  788. #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
  789. if (qemu_arch_available(arch_mask)) \
  790. fputs(opt_help, stdout);
  791. #define ARCHHEADING(text, arch_mask) \
  792. if (qemu_arch_available(arch_mask)) \
  793. puts(stringify(text));
  794. #define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
  795. #include "qemu-options.def"
  796. printf("\nDuring emulation, the following keys are useful:\n"
  797. "ctrl-alt-f toggle full screen\n"
  798. "ctrl-alt-n switch to virtual console 'n'\n"
  799. "ctrl-alt-g toggle mouse and keyboard grab\n"
  800. "\n"
  801. "When using -nographic, press 'ctrl-a h' to get some help.\n"
  802. "\n"
  803. QEMU_HELP_BOTTOM "\n");
  804. exit(exitcode);
  805. }
  806. enum {
  807. #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
  808. opt_enum,
  809. #define DEFHEADING(text)
  810. #define ARCHHEADING(text, arch_mask)
  811. #include "qemu-options.def"
  812. };
  813. #define HAS_ARG 0x0001
  814. typedef struct QEMUOption {
  815. const char *name;
  816. int flags;
  817. int index;
  818. uint32_t arch_mask;
  819. } QEMUOption;
  820. static const QEMUOption qemu_options[] = {
  821. { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
  822. #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
  823. { option, opt_arg, opt_enum, arch_mask },
  824. #define DEFHEADING(text)
  825. #define ARCHHEADING(text, arch_mask)
  826. #include "qemu-options.def"
  827. { /* end of list */ }
  828. };
  829. typedef struct VGAInterfaceInfo {
  830. const char *opt_name; /* option name */
  831. const char *name; /* human-readable name */
  832. /* Class names indicating that support is available.
  833. * If no class is specified, the interface is always available */
  834. const char *class_names[2];
  835. } VGAInterfaceInfo;
  836. static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
  837. [VGA_NONE] = {
  838. .opt_name = "none",
  839. .name = "no graphic card",
  840. },
  841. [VGA_STD] = {
  842. .opt_name = "std",
  843. .name = "standard VGA",
  844. .class_names = { "VGA", "isa-vga" },
  845. },
  846. [VGA_CIRRUS] = {
  847. .opt_name = "cirrus",
  848. .name = "Cirrus VGA",
  849. .class_names = { "cirrus-vga", "isa-cirrus-vga" },
  850. },
  851. [VGA_VMWARE] = {
  852. .opt_name = "vmware",
  853. .name = "VMWare SVGA",
  854. .class_names = { "vmware-svga" },
  855. },
  856. [VGA_VIRTIO] = {
  857. .opt_name = "virtio",
  858. .name = "Virtio VGA",
  859. .class_names = { "virtio-vga" },
  860. },
  861. [VGA_QXL] = {
  862. .opt_name = "qxl",
  863. .name = "QXL VGA",
  864. .class_names = { "qxl-vga" },
  865. },
  866. [VGA_TCX] = {
  867. .opt_name = "tcx",
  868. .name = "TCX framebuffer",
  869. .class_names = { "sun-tcx" },
  870. },
  871. [VGA_CG3] = {
  872. .opt_name = "cg3",
  873. .name = "CG3 framebuffer",
  874. .class_names = { "cgthree" },
  875. },
  876. #ifdef CONFIG_XEN_BACKEND
  877. [VGA_XENFB] = {
  878. .opt_name = "xenfb",
  879. .name = "Xen paravirtualized framebuffer",
  880. },
  881. #endif
  882. };
  883. static bool vga_interface_available(VGAInterfaceType t)
  884. {
  885. const VGAInterfaceInfo *ti = &vga_interfaces[t];
  886. assert(t < VGA_TYPE_MAX);
  887. if (!ti->class_names[0] || module_object_class_by_name(ti->class_names[0])) {
  888. return true;
  889. }
  890. if (ti->class_names[1] && module_object_class_by_name(ti->class_names[1])) {
  891. return true;
  892. }
  893. return false;
  894. }
  895. static const char *
  896. get_default_vga_model(const MachineClass *machine_class)
  897. {
  898. if (machine_class->default_display) {
  899. for (int t = 0; t < VGA_TYPE_MAX; t++) {
  900. const VGAInterfaceInfo *ti = &vga_interfaces[t];
  901. if (ti->opt_name && vga_interface_available(t) &&
  902. g_str_equal(ti->opt_name, machine_class->default_display)) {
  903. return machine_class->default_display;
  904. }
  905. }
  906. warn_report_once("Default display '%s' is not available in this binary",
  907. machine_class->default_display);
  908. return NULL;
  909. } else if (vga_interface_available(VGA_CIRRUS)) {
  910. return "cirrus";
  911. } else if (vga_interface_available(VGA_STD)) {
  912. return "std";
  913. }
  914. return NULL;
  915. }
  916. static void select_vgahw(const MachineClass *machine_class, const char *p)
  917. {
  918. const char *opts;
  919. int t;
  920. if (g_str_equal(p, "help")) {
  921. const char *def = get_default_vga_model(machine_class);
  922. for (t = 0; t < VGA_TYPE_MAX; t++) {
  923. const VGAInterfaceInfo *ti = &vga_interfaces[t];
  924. if (vga_interface_available(t) && ti->opt_name) {
  925. printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
  926. (def && g_str_equal(ti->opt_name, def)) ?
  927. " (default)" : "");
  928. }
  929. }
  930. exit(0);
  931. }
  932. assert(vga_interface_type == VGA_NONE);
  933. for (t = 0; t < VGA_TYPE_MAX; t++) {
  934. const VGAInterfaceInfo *ti = &vga_interfaces[t];
  935. if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
  936. if (!vga_interface_available(t)) {
  937. error_report("%s not available", ti->name);
  938. exit(1);
  939. }
  940. vga_interface_type = t;
  941. break;
  942. }
  943. }
  944. if (t == VGA_TYPE_MAX) {
  945. invalid_vga:
  946. error_report("unknown vga type: %s", p);
  947. exit(1);
  948. }
  949. while (*opts) {
  950. const char *nextopt;
  951. if (strstart(opts, ",retrace=", &nextopt)) {
  952. opts = nextopt;
  953. if (strstart(opts, "dumb", &nextopt))
  954. vga_retrace_method = VGA_RETRACE_DUMB;
  955. else if (strstart(opts, "precise", &nextopt))
  956. vga_retrace_method = VGA_RETRACE_PRECISE;
  957. else goto invalid_vga;
  958. } else goto invalid_vga;
  959. opts = nextopt;
  960. }
  961. }
  962. static void parse_display_qapi(const char *str)
  963. {
  964. DisplayOptions *opts;
  965. Visitor *v;
  966. v = qobject_input_visitor_new_str(str, "type", &error_fatal);
  967. visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
  968. QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
  969. qapi_free_DisplayOptions(opts);
  970. visit_free(v);
  971. }
  972. DisplayOptions *qmp_query_display_options(Error **errp)
  973. {
  974. return QAPI_CLONE(DisplayOptions, &dpy);
  975. }
  976. static void parse_display(const char *p)
  977. {
  978. if (is_help_option(p)) {
  979. qemu_display_help();
  980. exit(0);
  981. }
  982. #ifdef CONFIG_VNC
  983. const char *opts;
  984. if (strstart(p, "vnc", &opts)) {
  985. /*
  986. * vnc isn't a (local) DisplayType but a protocol for remote
  987. * display access.
  988. */
  989. if (*opts == '=') {
  990. vnc_parse(opts + 1);
  991. display_remote++;
  992. } else {
  993. error_report("VNC requires a display argument vnc=<display>");
  994. exit(1);
  995. }
  996. return;
  997. }
  998. #endif
  999. parse_display_qapi(p);
  1000. }
  1001. static inline bool nonempty_str(const char *str)
  1002. {
  1003. return str && *str;
  1004. }
  1005. static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
  1006. {
  1007. gchar *buf;
  1008. size_t size;
  1009. const char *name, *file, *str, *gen_id;
  1010. FWCfgState *fw_cfg = (FWCfgState *) opaque;
  1011. if (fw_cfg == NULL) {
  1012. error_setg(errp, "fw_cfg device not available");
  1013. return -1;
  1014. }
  1015. name = qemu_opt_get(opts, "name");
  1016. file = qemu_opt_get(opts, "file");
  1017. str = qemu_opt_get(opts, "string");
  1018. gen_id = qemu_opt_get(opts, "gen_id");
  1019. /* we need the name, and exactly one of: file, content string, gen_id */
  1020. if (!nonempty_str(name) ||
  1021. nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
  1022. error_setg(errp, "name, plus exactly one of file,"
  1023. " string and gen_id, are needed");
  1024. return -1;
  1025. }
  1026. if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
  1027. error_setg(errp, "name too long (max. %d char)",
  1028. FW_CFG_MAX_FILE_PATH - 1);
  1029. return -1;
  1030. }
  1031. if (nonempty_str(gen_id)) {
  1032. /*
  1033. * In this particular case where the content is populated
  1034. * internally, the "etc/" namespace protection is relaxed,
  1035. * so do not emit a warning.
  1036. */
  1037. } else if (strncmp(name, "opt/", 4) != 0) {
  1038. warn_report("externally provided fw_cfg item names "
  1039. "should be prefixed with \"opt/\"");
  1040. }
  1041. if (nonempty_str(str)) {
  1042. size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
  1043. buf = g_memdup(str, size);
  1044. } else if (nonempty_str(gen_id)) {
  1045. if (!fw_cfg_add_file_from_generator(fw_cfg, object_get_objects_root(),
  1046. gen_id, name, errp)) {
  1047. return -1;
  1048. }
  1049. return 0;
  1050. } else {
  1051. GError *err = NULL;
  1052. if (!g_file_get_contents(file, &buf, &size, &err)) {
  1053. error_setg(errp, "can't load %s: %s", file, err->message);
  1054. g_error_free(err);
  1055. return -1;
  1056. }
  1057. }
  1058. /* For legacy, keep user files in a specific global order. */
  1059. fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
  1060. fw_cfg_add_file(fw_cfg, name, buf, size);
  1061. fw_cfg_reset_order_override(fw_cfg);
  1062. return 0;
  1063. }
  1064. static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
  1065. {
  1066. return qdev_device_help(opts);
  1067. }
  1068. static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
  1069. {
  1070. DeviceState *dev;
  1071. dev = qdev_device_add(opts, errp);
  1072. if (!dev && *errp) {
  1073. error_report_err(*errp);
  1074. return -1;
  1075. } else if (dev) {
  1076. object_unref(OBJECT(dev));
  1077. }
  1078. return 0;
  1079. }
  1080. static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
  1081. {
  1082. Error *local_err = NULL;
  1083. if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
  1084. if (local_err) {
  1085. error_propagate(errp, local_err);
  1086. return -1;
  1087. }
  1088. exit(0);
  1089. }
  1090. return 0;
  1091. }
  1092. #ifdef CONFIG_VIRTFS
  1093. static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
  1094. {
  1095. return qemu_fsdev_add(opts, errp);
  1096. }
  1097. #endif
  1098. static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
  1099. {
  1100. return monitor_init_opts(opts, errp);
  1101. }
  1102. static void monitor_parse(const char *str, const char *mode, bool pretty)
  1103. {
  1104. static int monitor_device_index = 0;
  1105. QemuOpts *opts;
  1106. const char *p;
  1107. char label[32];
  1108. if (strstart(str, "chardev:", &p)) {
  1109. snprintf(label, sizeof(label), "%s", p);
  1110. } else {
  1111. snprintf(label, sizeof(label), "compat_monitor%d",
  1112. monitor_device_index);
  1113. opts = qemu_chr_parse_compat(label, str, true);
  1114. if (!opts) {
  1115. error_report("parse error: %s", str);
  1116. exit(1);
  1117. }
  1118. }
  1119. opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
  1120. qemu_opt_set(opts, "mode", mode, &error_abort);
  1121. qemu_opt_set(opts, "chardev", label, &error_abort);
  1122. if (!strcmp(mode, "control")) {
  1123. qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
  1124. } else {
  1125. assert(pretty == false);
  1126. }
  1127. monitor_device_index++;
  1128. }
  1129. struct device_config {
  1130. enum {
  1131. DEV_USB, /* -usbdevice */
  1132. DEV_SERIAL, /* -serial */
  1133. DEV_PARALLEL, /* -parallel */
  1134. DEV_DEBUGCON, /* -debugcon */
  1135. DEV_GDB, /* -gdb, -s */
  1136. DEV_SCLP, /* s390 sclp */
  1137. } type;
  1138. const char *cmdline;
  1139. Location loc;
  1140. QTAILQ_ENTRY(device_config) next;
  1141. };
  1142. static QTAILQ_HEAD(, device_config) device_configs =
  1143. QTAILQ_HEAD_INITIALIZER(device_configs);
  1144. static void add_device_config(int type, const char *cmdline)
  1145. {
  1146. struct device_config *conf;
  1147. conf = g_malloc0(sizeof(*conf));
  1148. conf->type = type;
  1149. conf->cmdline = cmdline;
  1150. loc_save(&conf->loc);
  1151. QTAILQ_INSERT_TAIL(&device_configs, conf, next);
  1152. }
  1153. /**
  1154. * foreach_device_config_or_exit(): process per-device configs
  1155. * @type: device_config type
  1156. * @func: device specific config function, returning pass/fail
  1157. *
  1158. * @func is called with the &error_fatal handler so device specific
  1159. * error messages can be reported on failure.
  1160. */
  1161. static void foreach_device_config_or_exit(int type,
  1162. bool (*func)(const char *cmdline,
  1163. Error **errp))
  1164. {
  1165. struct device_config *conf;
  1166. QTAILQ_FOREACH(conf, &device_configs, next) {
  1167. if (conf->type != type)
  1168. continue;
  1169. loc_push_restore(&conf->loc);
  1170. func(conf->cmdline, &error_fatal);
  1171. loc_pop(&conf->loc);
  1172. }
  1173. }
  1174. static void qemu_disable_default_devices(void)
  1175. {
  1176. MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
  1177. default_driver_check_json();
  1178. qemu_opts_foreach(qemu_find_opts("device"),
  1179. default_driver_check, NULL, NULL);
  1180. qemu_opts_foreach(qemu_find_opts("global"),
  1181. default_driver_check, NULL, NULL);
  1182. if (!vga_model && !default_vga) {
  1183. vga_interface_type = VGA_DEVICE;
  1184. vga_interface_created = true;
  1185. }
  1186. if (!has_defaults || machine_class->no_serial) {
  1187. default_serial = 0;
  1188. }
  1189. if (!has_defaults || machine_class->no_parallel) {
  1190. default_parallel = 0;
  1191. }
  1192. if (!has_defaults || machine_class->no_floppy) {
  1193. default_floppy = 0;
  1194. }
  1195. if (!has_defaults || machine_class->no_cdrom) {
  1196. default_cdrom = 0;
  1197. }
  1198. if (!has_defaults || !machine_class->auto_create_sdcard) {
  1199. auto_create_sdcard = false;
  1200. }
  1201. if (!has_defaults) {
  1202. default_audio = 0;
  1203. default_monitor = 0;
  1204. default_net = 0;
  1205. default_vga = 0;
  1206. } else {
  1207. if (default_net && machine_class->default_nic &&
  1208. !module_object_class_by_name(machine_class->default_nic)) {
  1209. warn_report("Default NIC '%s' is not available in this binary",
  1210. machine_class->default_nic);
  1211. default_net = 0;
  1212. }
  1213. }
  1214. }
  1215. static void qemu_setup_display(void)
  1216. {
  1217. if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
  1218. if (!qemu_display_find_default(&dpy)) {
  1219. dpy.type = DISPLAY_TYPE_NONE;
  1220. #if defined(CONFIG_VNC)
  1221. vnc_parse("localhost:0,to=99,id=default");
  1222. display_remote++;
  1223. #endif
  1224. }
  1225. }
  1226. if (dpy.type == DISPLAY_TYPE_DEFAULT) {
  1227. dpy.type = DISPLAY_TYPE_NONE;
  1228. }
  1229. qemu_display_early_init(&dpy);
  1230. }
  1231. static void qemu_create_default_devices(void)
  1232. {
  1233. MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
  1234. const char *vc = qemu_display_get_vc(&dpy);
  1235. if (is_daemonized()) {
  1236. /* According to documentation and historically, -nographic redirects
  1237. * serial port, parallel port and monitor to stdio, which does not work
  1238. * with -daemonize. We can redirect these to null instead, but since
  1239. * -nographic is legacy, let's just error out.
  1240. * We disallow -nographic only if all other ports are not redirected
  1241. * explicitly, to not break existing legacy setups which uses
  1242. * -nographic _and_ redirects all ports explicitly - this is valid
  1243. * usage, -nographic is just a no-op in this case.
  1244. */
  1245. if (nographic
  1246. && (default_parallel || default_serial || default_monitor)) {
  1247. error_report("-nographic cannot be used with -daemonize");
  1248. exit(1);
  1249. }
  1250. }
  1251. if (nographic) {
  1252. if (default_parallel) {
  1253. add_device_config(DEV_PARALLEL, "null");
  1254. }
  1255. if (default_serial && default_monitor) {
  1256. add_device_config(DEV_SERIAL, "mon:stdio");
  1257. } else {
  1258. if (default_serial) {
  1259. add_device_config(DEV_SERIAL, "stdio");
  1260. }
  1261. if (default_monitor) {
  1262. monitor_parse("stdio", "readline", false);
  1263. }
  1264. }
  1265. } else {
  1266. if (default_serial) {
  1267. add_device_config(DEV_SERIAL, vc ?: "null");
  1268. }
  1269. if (default_parallel) {
  1270. add_device_config(DEV_PARALLEL, vc ?: "null");
  1271. }
  1272. if (default_monitor && vc) {
  1273. monitor_parse(vc, "readline", false);
  1274. }
  1275. }
  1276. if (default_net) {
  1277. QemuOptsList *net = qemu_find_opts("net");
  1278. qemu_opts_parse(net, "nic", true, &error_abort);
  1279. #ifdef CONFIG_SLIRP
  1280. qemu_opts_parse(net, "user", true, &error_abort);
  1281. #endif
  1282. }
  1283. /* If no default VGA is requested, the default is "none". */
  1284. if (default_vga) {
  1285. vga_model = get_default_vga_model(machine_class);
  1286. }
  1287. if (vga_model) {
  1288. select_vgahw(machine_class, vga_model);
  1289. }
  1290. }
  1291. static bool serial_parse(const char *devname, Error **errp)
  1292. {
  1293. int index = num_serial_hds;
  1294. serial_hds = g_renew(Chardev *, serial_hds, index + 1);
  1295. if (strcmp(devname, "none") == 0) {
  1296. /* Don't allocate a serial device for this index */
  1297. serial_hds[index] = NULL;
  1298. } else {
  1299. char label[32];
  1300. snprintf(label, sizeof(label), "serial%d", index);
  1301. serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
  1302. if (!serial_hds[index]) {
  1303. error_setg(errp, "could not connect serial device"
  1304. " to character backend '%s'", devname);
  1305. return false;
  1306. }
  1307. }
  1308. num_serial_hds++;
  1309. return true;
  1310. }
  1311. Chardev *serial_hd(int i)
  1312. {
  1313. assert(i >= 0);
  1314. if (i < num_serial_hds) {
  1315. return serial_hds[i];
  1316. }
  1317. return NULL;
  1318. }
  1319. static bool parallel_parse(const char *devname, Error **errp)
  1320. {
  1321. static int index = 0;
  1322. char label[32];
  1323. if (strcmp(devname, "none") == 0)
  1324. return true;
  1325. if (index == MAX_PARALLEL_PORTS) {
  1326. error_setg(errp, "too many parallel ports");
  1327. return false;
  1328. }
  1329. snprintf(label, sizeof(label), "parallel%d", index);
  1330. parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
  1331. if (!parallel_hds[index]) {
  1332. error_setg(errp, "could not connect parallel device"
  1333. " to character backend '%s'", devname);
  1334. return false;
  1335. }
  1336. index++;
  1337. return true;
  1338. }
  1339. static bool debugcon_parse(const char *devname, Error **errp)
  1340. {
  1341. QemuOpts *opts;
  1342. if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
  1343. error_setg(errp, "invalid character backend '%s'", devname);
  1344. return false;
  1345. }
  1346. opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
  1347. if (!opts) {
  1348. error_setg(errp, "already have a debugcon device");
  1349. return false;
  1350. }
  1351. qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
  1352. qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
  1353. return true;
  1354. }
  1355. static gint machine_class_cmp(gconstpointer a, gconstpointer b)
  1356. {
  1357. const MachineClass *mc1 = a, *mc2 = b;
  1358. int res;
  1359. if (mc1->family == NULL) {
  1360. if (mc2->family == NULL) {
  1361. /* Compare standalone machine types against each other; they sort
  1362. * in increasing order.
  1363. */
  1364. return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
  1365. object_class_get_name(OBJECT_CLASS(mc2)));
  1366. }
  1367. /* Standalone machine types sort after families. */
  1368. return 1;
  1369. }
  1370. if (mc2->family == NULL) {
  1371. /* Families sort before standalone machine types. */
  1372. return -1;
  1373. }
  1374. /* Families sort between each other alphabetically increasingly. */
  1375. res = strcmp(mc1->family, mc2->family);
  1376. if (res != 0) {
  1377. return res;
  1378. }
  1379. /* Within the same family, machine types sort in decreasing order. */
  1380. return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
  1381. object_class_get_name(OBJECT_CLASS(mc1)));
  1382. }
  1383. static void machine_help_func(const QDict *qdict)
  1384. {
  1385. g_autoptr(GSList) machines = NULL;
  1386. GSList *el;
  1387. const char *type = qdict_get_try_str(qdict, "type");
  1388. machines = object_class_get_list(TYPE_MACHINE, false);
  1389. if (type) {
  1390. ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
  1391. if (machine_class) {
  1392. type_print_class_properties(object_class_get_name(machine_class));
  1393. return;
  1394. }
  1395. }
  1396. printf("Supported machines are:\n");
  1397. machines = g_slist_sort(machines, machine_class_cmp);
  1398. for (el = machines; el; el = el->next) {
  1399. MachineClass *mc = el->data;
  1400. if (mc->alias) {
  1401. printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
  1402. }
  1403. printf("%-20s %s%s%s\n", mc->name, mc->desc,
  1404. mc->is_default ? " (default)" : "",
  1405. mc->deprecation_reason ? " (deprecated)" : "");
  1406. }
  1407. }
  1408. static void
  1409. machine_merge_property(const char *propname, QDict *prop, Error **errp)
  1410. {
  1411. QDict *opts;
  1412. opts = qdict_new();
  1413. /* Preserve the caller's reference to prop. */
  1414. qobject_ref(prop);
  1415. qdict_put(opts, propname, prop);
  1416. keyval_merge(machine_opts_dict, opts, errp);
  1417. qobject_unref(opts);
  1418. }
  1419. static void
  1420. machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
  1421. const char *arg)
  1422. {
  1423. QDict *prop = NULL;
  1424. bool help = false;
  1425. prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
  1426. if (help) {
  1427. qemu_opts_print_help(opts_list, true);
  1428. exit(0);
  1429. }
  1430. machine_merge_property(propname, prop, &error_fatal);
  1431. qobject_unref(prop);
  1432. }
  1433. static const char *pid_file;
  1434. struct UnlinkPidfileNotifier {
  1435. Notifier notifier;
  1436. char *pid_file_realpath;
  1437. };
  1438. static struct UnlinkPidfileNotifier qemu_unlink_pidfile_notifier;
  1439. static void qemu_unlink_pidfile(Notifier *n, void *data)
  1440. {
  1441. struct UnlinkPidfileNotifier *upn;
  1442. upn = DO_UPCAST(struct UnlinkPidfileNotifier, notifier, n);
  1443. unlink(upn->pid_file_realpath);
  1444. }
  1445. static const QEMUOption *lookup_opt(int argc, char **argv,
  1446. const char **poptarg, int *poptind)
  1447. {
  1448. const QEMUOption *popt;
  1449. int optind = *poptind;
  1450. char *r = argv[optind];
  1451. const char *optarg;
  1452. loc_set_cmdline(argv, optind, 1);
  1453. optind++;
  1454. /* Treat --foo the same as -foo. */
  1455. if (r[1] == '-')
  1456. r++;
  1457. popt = qemu_options;
  1458. for(;;) {
  1459. if (!popt->name) {
  1460. error_report("invalid option");
  1461. exit(1);
  1462. }
  1463. if (!strcmp(popt->name, r + 1))
  1464. break;
  1465. popt++;
  1466. }
  1467. if (popt->flags & HAS_ARG) {
  1468. if (optind >= argc) {
  1469. error_report("requires an argument");
  1470. exit(1);
  1471. }
  1472. optarg = argv[optind++];
  1473. loc_set_cmdline(argv, optind - 2, 2);
  1474. } else {
  1475. optarg = NULL;
  1476. }
  1477. *poptarg = optarg;
  1478. *poptind = optind;
  1479. return popt;
  1480. }
  1481. static MachineClass *select_machine(QDict *qdict, Error **errp)
  1482. {
  1483. ERRP_GUARD();
  1484. const char *machine_type = qdict_get_try_str(qdict, "type");
  1485. g_autoptr(GSList) machines = object_class_get_list(TYPE_MACHINE, false);
  1486. MachineClass *machine_class = NULL;
  1487. if (machine_type) {
  1488. machine_class = find_machine(machine_type, machines);
  1489. if (!machine_class) {
  1490. error_setg(errp, "unsupported machine type: \"%s\"", machine_type);
  1491. }
  1492. qdict_del(qdict, "type");
  1493. } else {
  1494. machine_class = find_default_machine(machines);
  1495. if (!machine_class) {
  1496. error_setg(errp, "No machine specified, and there is no default");
  1497. }
  1498. }
  1499. if (!machine_class) {
  1500. error_append_hint(errp,
  1501. "Use -machine help to list supported machines\n");
  1502. }
  1503. return machine_class;
  1504. }
  1505. static int object_parse_property_opt(Object *obj,
  1506. const char *name, const char *value,
  1507. const char *skip, Error **errp)
  1508. {
  1509. if (g_str_equal(name, skip)) {
  1510. return 0;
  1511. }
  1512. if (!object_property_parse(obj, name, value, errp)) {
  1513. return -1;
  1514. }
  1515. return 0;
  1516. }
  1517. /* *Non*recursively replace underscores with dashes in QDict keys. */
  1518. static void keyval_dashify(QDict *qdict, Error **errp)
  1519. {
  1520. const QDictEntry *ent, *next;
  1521. char *p;
  1522. for (ent = qdict_first(qdict); ent; ent = next) {
  1523. g_autofree char *new_key = NULL;
  1524. next = qdict_next(qdict, ent);
  1525. if (!strchr(ent->key, '_')) {
  1526. continue;
  1527. }
  1528. new_key = g_strdup(ent->key);
  1529. for (p = new_key; *p; p++) {
  1530. if (*p == '_') {
  1531. *p = '-';
  1532. }
  1533. }
  1534. if (qdict_haskey(qdict, new_key)) {
  1535. error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
  1536. return;
  1537. }
  1538. qobject_ref(ent->value);
  1539. qdict_put_obj(qdict, new_key, ent->value);
  1540. qdict_del(qdict, ent->key);
  1541. }
  1542. }
  1543. static void qemu_apply_legacy_machine_options(QDict *qdict)
  1544. {
  1545. const char *value;
  1546. QObject *prop;
  1547. keyval_dashify(qdict, &error_fatal);
  1548. /* Legacy options do not correspond to MachineState properties. */
  1549. value = qdict_get_try_str(qdict, "accel");
  1550. if (value) {
  1551. accelerators = g_strdup(value);
  1552. qdict_del(qdict, "accel");
  1553. }
  1554. value = qdict_get_try_str(qdict, "igd-passthru");
  1555. if (value) {
  1556. object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
  1557. false);
  1558. qdict_del(qdict, "igd-passthru");
  1559. }
  1560. value = qdict_get_try_str(qdict, "kvm-shadow-mem");
  1561. if (value) {
  1562. object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
  1563. false);
  1564. qdict_del(qdict, "kvm-shadow-mem");
  1565. }
  1566. value = qdict_get_try_str(qdict, "kernel-irqchip");
  1567. if (value) {
  1568. object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
  1569. false);
  1570. object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
  1571. false);
  1572. qdict_del(qdict, "kernel-irqchip");
  1573. }
  1574. value = qdict_get_try_str(qdict, "memory-backend");
  1575. if (value) {
  1576. if (mem_path) {
  1577. error_report("'-mem-path' can't be used together with"
  1578. "'-machine memory-backend'");
  1579. exit(EXIT_FAILURE);
  1580. }
  1581. /* Resolved later. */
  1582. ram_memdev_id = g_strdup(value);
  1583. qdict_del(qdict, "memory-backend");
  1584. }
  1585. prop = qdict_get(qdict, "memory");
  1586. if (prop) {
  1587. have_custom_ram_size =
  1588. qobject_type(prop) == QTYPE_QDICT &&
  1589. qdict_haskey(qobject_to(QDict, prop), "size");
  1590. }
  1591. }
  1592. static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
  1593. {
  1594. ObjectOption *opt, *next;
  1595. QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
  1596. const char *type = ObjectType_str(opt->opts->qom_type);
  1597. if (type_opt_predicate(type)) {
  1598. user_creatable_add_qapi(opt->opts, &error_fatal);
  1599. qapi_free_ObjectOptions(opt->opts);
  1600. QTAILQ_REMOVE(&object_opts, opt, next);
  1601. g_free(opt);
  1602. }
  1603. }
  1604. }
  1605. static void object_option_add_visitor(Visitor *v)
  1606. {
  1607. ObjectOption *opt = g_new0(ObjectOption, 1);
  1608. visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
  1609. QTAILQ_INSERT_TAIL(&object_opts, opt, next);
  1610. }
  1611. static void incoming_option_parse(const char *str)
  1612. {
  1613. MigrationChannelType type = MIGRATION_CHANNEL_TYPE_MAIN;
  1614. MigrationChannel *channel;
  1615. Visitor *v;
  1616. if (!strcmp(str, "defer")) {
  1617. channel = NULL;
  1618. } else if (migrate_is_uri(str)) {
  1619. migrate_uri_parse(str, &channel, &error_fatal);
  1620. } else {
  1621. v = qobject_input_visitor_new_str(str, "channel-type", &error_fatal);
  1622. visit_type_MigrationChannel(v, NULL, &channel, &error_fatal);
  1623. visit_free(v);
  1624. type = channel->channel_type;
  1625. }
  1626. /* New incoming spec replaces the previous */
  1627. qapi_free_MigrationChannel(incoming_channels[type]);
  1628. incoming_channels[type] = channel;
  1629. incoming_str[type] = str;
  1630. incoming = incoming_str[MIGRATION_CHANNEL_TYPE_MAIN];
  1631. }
  1632. static void object_option_parse(const char *str)
  1633. {
  1634. QemuOpts *opts;
  1635. const char *type;
  1636. Visitor *v;
  1637. if (str[0] == '{') {
  1638. QObject *obj = qobject_from_json(str, &error_fatal);
  1639. v = qobject_input_visitor_new(obj);
  1640. qobject_unref(obj);
  1641. } else {
  1642. opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
  1643. str, true);
  1644. if (!opts) {
  1645. exit(1);
  1646. }
  1647. type = qemu_opt_get(opts, "qom-type");
  1648. if (!type) {
  1649. error_report(QERR_MISSING_PARAMETER, "qom-type");
  1650. exit(1);
  1651. }
  1652. if (user_creatable_print_help(type, opts)) {
  1653. exit(0);
  1654. }
  1655. v = opts_visitor_new(opts);
  1656. }
  1657. object_option_add_visitor(v);
  1658. visit_free(v);
  1659. }
  1660. static void overcommit_parse(const char *str)
  1661. {
  1662. QemuOpts *opts;
  1663. const char *mem_lock_opt;
  1664. opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
  1665. str, false);
  1666. if (!opts) {
  1667. exit(1);
  1668. }
  1669. enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", enable_cpu_pm);
  1670. mem_lock_opt = qemu_opt_get(opts, "mem-lock");
  1671. if (!mem_lock_opt) {
  1672. return;
  1673. }
  1674. if (strcmp(mem_lock_opt, "on") == 0) {
  1675. mlock_state = MLOCK_ON;
  1676. return;
  1677. }
  1678. if (strcmp(mem_lock_opt, "off") == 0) {
  1679. mlock_state = MLOCK_OFF;
  1680. return;
  1681. }
  1682. if (strcmp(mem_lock_opt, "on-fault") == 0) {
  1683. mlock_state = MLOCK_ON_FAULT;
  1684. return;
  1685. }
  1686. error_report("parameter 'mem-lock' expects one of "
  1687. "'on', 'off', 'on-fault'");
  1688. exit(1);
  1689. }
  1690. /*
  1691. * Very early object creation, before the sandbox options have been activated.
  1692. */
  1693. static bool object_create_pre_sandbox(const char *type)
  1694. {
  1695. /*
  1696. * Objects should in general not get initialized "too early" without
  1697. * a reason. If you add one, state the reason in a comment!
  1698. */
  1699. /*
  1700. * Reason: -sandbox on,resourcecontrol=deny disallows setting CPU
  1701. * affinity of threads.
  1702. */
  1703. if (g_str_equal(type, "thread-context")) {
  1704. return true;
  1705. }
  1706. return false;
  1707. }
  1708. /*
  1709. * Initial object creation happens before all other
  1710. * QEMU data types are created. The majority of objects
  1711. * can be created at this point. The rng-egd object
  1712. * cannot be created here, as it depends on the chardev
  1713. * already existing.
  1714. */
  1715. static bool object_create_early(const char *type)
  1716. {
  1717. /*
  1718. * Objects should not be made "delayed" without a reason. If you
  1719. * add one, state the reason in a comment!
  1720. */
  1721. /* Reason: already created. */
  1722. if (object_create_pre_sandbox(type)) {
  1723. return false;
  1724. }
  1725. /* Reason: property "chardev" */
  1726. if (g_str_equal(type, "rng-egd") ||
  1727. g_str_equal(type, "qtest")) {
  1728. return false;
  1729. }
  1730. #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
  1731. /* Reason: cryptodev-vhost-user property "chardev" */
  1732. if (g_str_equal(type, "cryptodev-vhost-user")) {
  1733. return false;
  1734. }
  1735. #endif
  1736. /* Reason: vhost-user-blk-server property "node-name" */
  1737. if (g_str_equal(type, "vhost-user-blk-server")) {
  1738. return false;
  1739. }
  1740. /*
  1741. * Reason: filter-* property "netdev" etc.
  1742. */
  1743. if (g_str_equal(type, "filter-buffer") ||
  1744. g_str_equal(type, "filter-dump") ||
  1745. g_str_equal(type, "filter-mirror") ||
  1746. g_str_equal(type, "filter-redirector") ||
  1747. g_str_equal(type, "colo-compare") ||
  1748. g_str_equal(type, "filter-rewriter") ||
  1749. g_str_equal(type, "filter-replay")) {
  1750. return false;
  1751. }
  1752. /*
  1753. * Allocation of large amounts of memory may delay
  1754. * chardev initialization for too long, and trigger timeouts
  1755. * on software that waits for a monitor socket to be created
  1756. */
  1757. if (g_str_has_prefix(type, "memory-backend-")) {
  1758. return false;
  1759. }
  1760. return true;
  1761. }
  1762. static void qemu_apply_machine_options(QDict *qdict)
  1763. {
  1764. object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
  1765. if (semihosting_enabled(false) && !semihosting_get_argc()) {
  1766. /* fall back to the -kernel/-append */
  1767. semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
  1768. }
  1769. if (current_machine->smp.cpus > 1) {
  1770. replay_add_blocker("multiple CPUs");
  1771. }
  1772. }
  1773. static void qemu_create_early_backends(void)
  1774. {
  1775. MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
  1776. #if defined(CONFIG_SDL)
  1777. const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
  1778. #else
  1779. const bool use_sdl = false;
  1780. #endif
  1781. #if defined(CONFIG_GTK)
  1782. const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
  1783. #else
  1784. const bool use_gtk = false;
  1785. #endif
  1786. if (dpy.has_window_close && !use_gtk && !use_sdl) {
  1787. error_report("window-close is only valid for GTK and SDL, "
  1788. "ignoring option");
  1789. }
  1790. qemu_console_early_init();
  1791. if (dpy.has_gl && dpy.gl != DISPLAY_GL_MODE_OFF && display_opengl == 0) {
  1792. #if defined(CONFIG_OPENGL)
  1793. error_report("OpenGL is not supported by display backend '%s'",
  1794. DisplayType_str(dpy.type));
  1795. #else
  1796. error_report("OpenGL support was not enabled in this build of QEMU");
  1797. #endif
  1798. exit(1);
  1799. }
  1800. object_option_foreach_add(object_create_early);
  1801. /* spice needs the timers to be initialized by this point */
  1802. /* spice must initialize before audio as it changes the default audiodev */
  1803. /* spice must initialize before chardevs (for spicevmc and spiceport) */
  1804. qemu_spice.init();
  1805. qemu_opts_foreach(qemu_find_opts("chardev"),
  1806. chardev_init_func, NULL, &error_fatal);
  1807. #ifdef CONFIG_VIRTFS
  1808. qemu_opts_foreach(qemu_find_opts("fsdev"),
  1809. fsdev_init_func, NULL, &error_fatal);
  1810. #endif
  1811. /*
  1812. * Note: we need to create audio and block backends before
  1813. * setting machine properties, so they can be referred to.
  1814. */
  1815. configure_blockdev(&bdo_queue, machine_class, snapshot);
  1816. audio_init_audiodevs();
  1817. if (default_audio) {
  1818. audio_create_default_audiodevs();
  1819. }
  1820. }
  1821. /*
  1822. * The remainder of object creation happens after the
  1823. * creation of chardev, fsdev, net clients and device data types.
  1824. */
  1825. static bool object_create_late(const char *type)
  1826. {
  1827. return !object_create_early(type) && !object_create_pre_sandbox(type);
  1828. }
  1829. static void qemu_create_late_backends(void)
  1830. {
  1831. if (qtest_chrdev) {
  1832. qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
  1833. }
  1834. net_init_clients();
  1835. object_option_foreach_add(object_create_late);
  1836. /*
  1837. * Wait for any outstanding memory prealloc from created memory
  1838. * backends to complete.
  1839. */
  1840. if (!qemu_finish_async_prealloc_mem(&error_fatal)) {
  1841. exit(1);
  1842. }
  1843. if (tpm_init() < 0) {
  1844. exit(1);
  1845. }
  1846. qemu_opts_foreach(qemu_find_opts("mon"),
  1847. mon_init_func, NULL, &error_fatal);
  1848. foreach_device_config_or_exit(DEV_SERIAL, serial_parse);
  1849. foreach_device_config_or_exit(DEV_PARALLEL, parallel_parse);
  1850. foreach_device_config_or_exit(DEV_DEBUGCON, debugcon_parse);
  1851. /* now chardevs have been created we may have semihosting to connect */
  1852. qemu_semihosting_chardev_init();
  1853. }
  1854. static void qemu_resolve_machine_memdev(void)
  1855. {
  1856. if (ram_memdev_id) {
  1857. Object *backend;
  1858. ram_addr_t backend_size;
  1859. backend = object_resolve_path_type(ram_memdev_id,
  1860. TYPE_MEMORY_BACKEND, NULL);
  1861. if (!backend) {
  1862. error_report("Memory backend '%s' not found", ram_memdev_id);
  1863. exit(EXIT_FAILURE);
  1864. }
  1865. if (!have_custom_ram_size) {
  1866. backend_size = object_property_get_uint(backend, "size", &error_abort);
  1867. current_machine->ram_size = backend_size;
  1868. }
  1869. object_property_set_link(OBJECT(current_machine),
  1870. "memory-backend", backend, &error_fatal);
  1871. }
  1872. }
  1873. static void parse_memory_options(void)
  1874. {
  1875. QemuOpts *opts = qemu_find_opts_singleton("memory");
  1876. QDict *dict, *prop;
  1877. const char *mem_str;
  1878. Location loc;
  1879. loc_push_none(&loc);
  1880. qemu_opts_loc_restore(opts);
  1881. prop = qdict_new();
  1882. if (qemu_opt_get_size(opts, "size", 0) != 0) {
  1883. /* Fix up legacy suffix-less format */
  1884. mem_str = qemu_opt_get(opts, "size");
  1885. if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
  1886. g_autofree char *mib_str = g_strdup_printf("%sM", mem_str);
  1887. qdict_put_str(prop, "size", mib_str);
  1888. } else {
  1889. qdict_put_str(prop, "size", mem_str);
  1890. }
  1891. }
  1892. if (qemu_opt_get(opts, "maxmem")) {
  1893. qdict_put_str(prop, "max-size", qemu_opt_get(opts, "maxmem"));
  1894. }
  1895. if (qemu_opt_get(opts, "slots")) {
  1896. qdict_put_str(prop, "slots", qemu_opt_get(opts, "slots"));
  1897. }
  1898. dict = qdict_new();
  1899. qdict_put(dict, "memory", prop);
  1900. keyval_merge(machine_opts_dict, dict, &error_fatal);
  1901. qobject_unref(dict);
  1902. loc_pop(&loc);
  1903. }
  1904. static void qemu_create_machine_containers(Object *machine)
  1905. {
  1906. static const char *const containers[] = {
  1907. "unattached",
  1908. "peripheral",
  1909. "peripheral-anon",
  1910. };
  1911. for (unsigned i = 0; i < ARRAY_SIZE(containers); i++) {
  1912. object_property_add_new_container(machine, containers[i]);
  1913. }
  1914. }
  1915. static void qemu_create_machine(QDict *qdict)
  1916. {
  1917. MachineClass *machine_class = select_machine(qdict, &error_fatal);
  1918. object_set_machine_compat_props(machine_class->compat_props);
  1919. current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
  1920. object_property_add_child(object_get_root(), "machine",
  1921. OBJECT(current_machine));
  1922. qemu_create_machine_containers(OBJECT(current_machine));
  1923. object_property_add_child(machine_get_container("unattached"),
  1924. "sysbus", OBJECT(sysbus_get_default()));
  1925. if (machine_class->minimum_page_bits) {
  1926. if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
  1927. /* This would be a board error: specifying a minimum smaller than
  1928. * a target's compile-time fixed setting.
  1929. */
  1930. g_assert_not_reached();
  1931. }
  1932. }
  1933. cpu_exec_init_all();
  1934. if (machine_class->hw_version) {
  1935. qemu_set_hw_version(machine_class->hw_version);
  1936. }
  1937. /*
  1938. * Get the default machine options from the machine if it is not already
  1939. * specified either by the configuration file or by the command line.
  1940. */
  1941. if (machine_class->default_machine_opts) {
  1942. QDict *default_opts =
  1943. keyval_parse(machine_class->default_machine_opts, NULL, NULL,
  1944. &error_abort);
  1945. qemu_apply_legacy_machine_options(default_opts);
  1946. object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
  1947. false, &error_abort);
  1948. qobject_unref(default_opts);
  1949. }
  1950. }
  1951. static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
  1952. {
  1953. GlobalProperty *g;
  1954. g = g_malloc0(sizeof(*g));
  1955. g->driver = qemu_opt_get(opts, "driver");
  1956. g->property = qemu_opt_get(opts, "property");
  1957. g->value = qemu_opt_get(opts, "value");
  1958. qdev_prop_register_global(g);
  1959. return 0;
  1960. }
  1961. /*
  1962. * Return whether configuration group @group is stored in QemuOpts, or
  1963. * recorded as one or more QDicts by qemu_record_config_group.
  1964. */
  1965. static bool is_qemuopts_group(const char *group)
  1966. {
  1967. if (g_str_equal(group, "object") ||
  1968. g_str_equal(group, "audiodev") ||
  1969. g_str_equal(group, "machine") ||
  1970. g_str_equal(group, "smp-opts") ||
  1971. g_str_equal(group, "boot-opts")) {
  1972. return false;
  1973. }
  1974. return true;
  1975. }
  1976. static void qemu_record_config_group(const char *group, QDict *dict,
  1977. bool from_json, Error **errp)
  1978. {
  1979. if (g_str_equal(group, "object")) {
  1980. Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
  1981. object_option_add_visitor(v);
  1982. visit_free(v);
  1983. } else if (g_str_equal(group, "audiodev")) {
  1984. Audiodev *dev = NULL;
  1985. Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
  1986. if (visit_type_Audiodev(v, NULL, &dev, errp)) {
  1987. audio_define(dev);
  1988. }
  1989. visit_free(v);
  1990. } else if (g_str_equal(group, "machine")) {
  1991. /*
  1992. * Cannot merge string-valued and type-safe dictionaries, so JSON
  1993. * is not accepted yet for -M.
  1994. */
  1995. assert(!from_json);
  1996. keyval_merge(machine_opts_dict, dict, errp);
  1997. } else if (g_str_equal(group, "smp-opts")) {
  1998. machine_merge_property("smp", dict, &error_fatal);
  1999. } else if (g_str_equal(group, "boot-opts")) {
  2000. machine_merge_property("boot", dict, &error_fatal);
  2001. } else {
  2002. abort();
  2003. }
  2004. }
  2005. /*
  2006. * Parse non-QemuOpts config file groups, pass the rest to
  2007. * qemu_config_do_parse.
  2008. */
  2009. static void qemu_parse_config_group(const char *group, QDict *qdict,
  2010. void *opaque, Error **errp)
  2011. {
  2012. QObject *crumpled;
  2013. if (is_qemuopts_group(group)) {
  2014. qemu_config_do_parse(group, qdict, opaque, errp);
  2015. return;
  2016. }
  2017. crumpled = qdict_crumple(qdict, errp);
  2018. if (!crumpled) {
  2019. return;
  2020. }
  2021. switch (qobject_type(crumpled)) {
  2022. case QTYPE_QDICT:
  2023. qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
  2024. break;
  2025. case QTYPE_QLIST:
  2026. error_setg(errp, "Lists cannot be at top level of a configuration section");
  2027. break;
  2028. default:
  2029. g_assert_not_reached();
  2030. }
  2031. qobject_unref(crumpled);
  2032. }
  2033. static void qemu_read_default_config_file(Error **errp)
  2034. {
  2035. ERRP_GUARD();
  2036. int ret;
  2037. g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
  2038. ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
  2039. if (ret < 0) {
  2040. if (ret == -ENOENT) {
  2041. error_free(*errp);
  2042. *errp = NULL;
  2043. }
  2044. }
  2045. }
  2046. static void qemu_set_option(const char *str, Error **errp)
  2047. {
  2048. char group[64], id[64], arg[64];
  2049. QemuOptsList *list;
  2050. QemuOpts *opts;
  2051. int rc, offset;
  2052. rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
  2053. if (rc < 3 || str[offset] != '=') {
  2054. error_setg(errp, "can't parse: \"%s\"", str);
  2055. return;
  2056. }
  2057. if (!is_qemuopts_group(group)) {
  2058. error_setg(errp, "-set is not supported with %s", group);
  2059. } else {
  2060. list = qemu_find_opts_err(group, errp);
  2061. if (list) {
  2062. opts = qemu_opts_find(list, id);
  2063. if (!opts) {
  2064. error_setg(errp, "there is no %s \"%s\" defined", group, id);
  2065. return;
  2066. }
  2067. qemu_opt_set(opts, arg, str + offset + 1, errp);
  2068. }
  2069. }
  2070. }
  2071. static void user_register_global_props(void)
  2072. {
  2073. qemu_opts_foreach(qemu_find_opts("global"),
  2074. global_init_func, NULL, NULL);
  2075. }
  2076. static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
  2077. {
  2078. return !icount_configure(opts, errp);
  2079. }
  2080. static int accelerator_set_property(void *opaque,
  2081. const char *name, const char *value,
  2082. Error **errp)
  2083. {
  2084. return object_parse_property_opt(opaque, name, value, "accel", errp);
  2085. }
  2086. static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
  2087. {
  2088. bool *p_init_failed = opaque;
  2089. const char *acc = qemu_opt_get(opts, "accel");
  2090. AccelClass *ac = accel_find(acc);
  2091. AccelState *accel;
  2092. int ret;
  2093. bool qtest_with_kvm;
  2094. if (!acc) {
  2095. error_setg(errp, QERR_MISSING_PARAMETER, "accel");
  2096. goto bad;
  2097. }
  2098. qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
  2099. if (!ac) {
  2100. if (!qtest_with_kvm) {
  2101. error_report("invalid accelerator %s", acc);
  2102. }
  2103. goto bad;
  2104. }
  2105. accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
  2106. object_apply_compat_props(OBJECT(accel));
  2107. qemu_opt_foreach(opts, accelerator_set_property,
  2108. accel,
  2109. &error_fatal);
  2110. ret = accel_init_machine(accel, current_machine);
  2111. if (ret < 0) {
  2112. if (!qtest_with_kvm || ret != -ENOENT) {
  2113. error_report("failed to initialize %s: %s", acc, strerror(-ret));
  2114. }
  2115. goto bad;
  2116. }
  2117. return 1;
  2118. bad:
  2119. *p_init_failed = true;
  2120. return 0;
  2121. }
  2122. static void configure_accelerators(const char *progname)
  2123. {
  2124. bool init_failed = false;
  2125. qemu_opts_foreach(qemu_find_opts("icount"),
  2126. do_configure_icount, NULL, &error_fatal);
  2127. if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
  2128. char **accel_list, **tmp;
  2129. if (accelerators == NULL) {
  2130. /* Select the default accelerator */
  2131. bool have_tcg = accel_find("tcg");
  2132. bool have_kvm = accel_find("kvm");
  2133. bool have_hvf = accel_find("hvf");
  2134. if (have_tcg && have_kvm) {
  2135. if (g_str_has_suffix(progname, "kvm")) {
  2136. /* If the program name ends with "kvm", we prefer KVM */
  2137. accelerators = "kvm:tcg";
  2138. } else {
  2139. accelerators = "tcg:kvm";
  2140. }
  2141. } else if (have_kvm) {
  2142. accelerators = "kvm";
  2143. } else if (have_tcg) {
  2144. accelerators = "tcg";
  2145. } else if (have_hvf) {
  2146. accelerators = "hvf";
  2147. } else {
  2148. error_report("No accelerator selected and"
  2149. " no default accelerator available");
  2150. exit(1);
  2151. }
  2152. }
  2153. accel_list = g_strsplit(accelerators, ":", 0);
  2154. for (tmp = accel_list; *tmp; tmp++) {
  2155. /*
  2156. * Filter invalid accelerators here, to prevent obscenities
  2157. * such as "-machine accel=tcg,,thread=single".
  2158. */
  2159. if (accel_find(*tmp)) {
  2160. qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
  2161. } else {
  2162. init_failed = true;
  2163. error_report("invalid accelerator %s", *tmp);
  2164. }
  2165. }
  2166. g_strfreev(accel_list);
  2167. } else {
  2168. if (accelerators != NULL) {
  2169. error_report("The -accel and \"-machine accel=\" options are incompatible");
  2170. exit(1);
  2171. }
  2172. }
  2173. if (!qemu_opts_foreach(qemu_find_opts("accel"),
  2174. do_configure_accelerator, &init_failed, &error_fatal)) {
  2175. if (!init_failed) {
  2176. error_report("no accelerator found");
  2177. }
  2178. exit(1);
  2179. }
  2180. if (init_failed && !qtest_chrdev) {
  2181. error_report("falling back to %s", current_accel_name());
  2182. }
  2183. if (icount_enabled() && !tcg_enabled()) {
  2184. error_report("-icount is not allowed with hardware virtualization");
  2185. exit(1);
  2186. }
  2187. }
  2188. static void qemu_validate_options(const QDict *machine_opts)
  2189. {
  2190. const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
  2191. const char *shim_filename = qdict_get_try_str(machine_opts, "shim");
  2192. const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
  2193. const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
  2194. if (kernel_filename == NULL) {
  2195. if (kernel_cmdline != NULL) {
  2196. error_report("-append only allowed with -kernel option");
  2197. exit(1);
  2198. }
  2199. if (shim_filename != NULL) {
  2200. error_report("-shim only allowed with -kernel option");
  2201. exit(1);
  2202. }
  2203. if (initrd_filename != NULL) {
  2204. error_report("-initrd only allowed with -kernel option");
  2205. exit(1);
  2206. }
  2207. }
  2208. if (loadvm && incoming) {
  2209. error_report("'incoming' and 'loadvm' options are mutually exclusive");
  2210. exit(EXIT_FAILURE);
  2211. }
  2212. if (loadvm && preconfig_requested) {
  2213. error_report("'preconfig' and 'loadvm' options are "
  2214. "mutually exclusive");
  2215. exit(EXIT_FAILURE);
  2216. }
  2217. if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
  2218. error_report("'preconfig' supports '-incoming defer' only");
  2219. exit(EXIT_FAILURE);
  2220. }
  2221. #ifdef CONFIG_CURSES
  2222. if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
  2223. error_report("curses display cannot be used with -daemonize");
  2224. exit(1);
  2225. }
  2226. #endif
  2227. }
  2228. static void qemu_process_sugar_options(void)
  2229. {
  2230. if (mem_prealloc) {
  2231. QObject *smp = qdict_get(machine_opts_dict, "smp");
  2232. if (smp && qobject_type(smp) == QTYPE_QDICT) {
  2233. QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
  2234. if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
  2235. const char *val = qstring_get_str(qobject_to(QString, cpus));
  2236. object_register_sugar_prop("memory-backend", "prealloc-threads",
  2237. val, false);
  2238. }
  2239. }
  2240. object_register_sugar_prop("memory-backend", "prealloc", "on", false);
  2241. }
  2242. }
  2243. /* -action processing */
  2244. /*
  2245. * Process all the -action parameters parsed from cmdline.
  2246. */
  2247. static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
  2248. {
  2249. Error *local_err = NULL;
  2250. QDict *qdict = qemu_opts_to_qdict(opts, NULL);
  2251. QObject *ret = NULL;
  2252. qmp_marshal_set_action(qdict, &ret, &local_err);
  2253. qobject_unref(ret);
  2254. qobject_unref(qdict);
  2255. if (local_err) {
  2256. error_propagate(errp, local_err);
  2257. return 1;
  2258. }
  2259. return 0;
  2260. }
  2261. static void qemu_process_early_options(void)
  2262. {
  2263. qemu_opts_foreach(qemu_find_opts("name"),
  2264. parse_name, NULL, &error_fatal);
  2265. object_option_foreach_add(object_create_pre_sandbox);
  2266. #ifdef CONFIG_SECCOMP
  2267. QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
  2268. if (olist) {
  2269. qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
  2270. }
  2271. #endif
  2272. if (qemu_opts_foreach(qemu_find_opts("action"),
  2273. process_runstate_actions, NULL, &error_fatal)) {
  2274. exit(1);
  2275. }
  2276. #ifndef _WIN32
  2277. qemu_opts_foreach(qemu_find_opts("add-fd"),
  2278. parse_add_fd, NULL, &error_fatal);
  2279. qemu_opts_foreach(qemu_find_opts("add-fd"),
  2280. cleanup_add_fd, NULL, &error_fatal);
  2281. #endif
  2282. /* Open the logfile at this point and set the log mask if necessary. */
  2283. {
  2284. int mask = 0;
  2285. if (log_mask) {
  2286. mask = qemu_str_to_log_mask(log_mask);
  2287. if (!mask) {
  2288. qemu_print_log_usage(stdout);
  2289. exit(1);
  2290. }
  2291. }
  2292. qemu_set_log_filename_flags(log_file, mask, &error_fatal);
  2293. }
  2294. qemu_add_default_firmwarepath();
  2295. }
  2296. static void qemu_process_help_options(void)
  2297. {
  2298. /*
  2299. * Check for -cpu help and -device help before we call select_machine(),
  2300. * which will return an error if the architecture has no default machine
  2301. * type and the user did not specify one, so that the user doesn't need
  2302. * to say '-cpu help -machine something'.
  2303. */
  2304. if (cpu_option && is_help_option(cpu_option)) {
  2305. list_cpus();
  2306. exit(0);
  2307. }
  2308. if (qemu_opts_foreach(qemu_find_opts("device"),
  2309. device_help_func, NULL, NULL)) {
  2310. exit(0);
  2311. }
  2312. /* -L help lists the data directories and exits. */
  2313. if (list_data_dirs) {
  2314. qemu_list_data_dirs();
  2315. exit(0);
  2316. }
  2317. }
  2318. static void qemu_maybe_daemonize(const char *pid_file)
  2319. {
  2320. Error *err = NULL;
  2321. os_daemonize();
  2322. rcu_disable_atfork();
  2323. if (pid_file) {
  2324. char *pid_file_realpath = NULL;
  2325. if (!qemu_write_pidfile(pid_file, &err)) {
  2326. error_reportf_err(err, "cannot create PID file: ");
  2327. exit(1);
  2328. }
  2329. pid_file_realpath = g_malloc0(PATH_MAX);
  2330. if (!realpath(pid_file, pid_file_realpath)) {
  2331. if (errno != ENOENT) {
  2332. warn_report("not removing PID file on exit: cannot resolve PID "
  2333. "file path: %s: %s", pid_file, strerror(errno));
  2334. }
  2335. return;
  2336. }
  2337. qemu_unlink_pidfile_notifier = (struct UnlinkPidfileNotifier) {
  2338. .notifier = {
  2339. .notify = qemu_unlink_pidfile,
  2340. },
  2341. .pid_file_realpath = pid_file_realpath,
  2342. };
  2343. qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier.notifier);
  2344. }
  2345. }
  2346. static void qemu_init_displays(void)
  2347. {
  2348. DisplayState *ds;
  2349. /* init local displays */
  2350. ds = init_displaystate();
  2351. qemu_display_init(ds, &dpy);
  2352. /* must be after terminal init, SDL library changes signal handlers */
  2353. os_setup_signal_handling();
  2354. /* init remote displays */
  2355. #ifdef CONFIG_VNC
  2356. qemu_opts_foreach(qemu_find_opts("vnc"),
  2357. vnc_init_func, NULL, &error_fatal);
  2358. #endif
  2359. if (using_spice) {
  2360. qemu_spice.display_init();
  2361. }
  2362. }
  2363. static void qemu_init_board(void)
  2364. {
  2365. MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
  2366. /* process plugin before CPUs are created, but once -smp has been parsed */
  2367. qemu_plugin_load_list(&plugin_list, &error_fatal);
  2368. /* From here on we enter MACHINE_PHASE_INITIALIZED. */
  2369. machine_run_board_init(current_machine, mem_path, &error_fatal);
  2370. if (machine_class->auto_create_sdcard) {
  2371. bool ambigous;
  2372. /* Ensure there is a SD bus available to create SD card on */
  2373. Object *obj = object_resolve_path_type("", TYPE_SD_BUS, &ambigous);
  2374. if (!obj && !ambigous) {
  2375. fprintf(stderr, "Can not create sd-card on '%s' machine"
  2376. " because it lacks a sd-bus\n",
  2377. machine_class->name);
  2378. abort();
  2379. }
  2380. }
  2381. drive_check_orphaned();
  2382. realtime_init();
  2383. }
  2384. static void qemu_create_cli_devices(void)
  2385. {
  2386. DeviceOption *opt;
  2387. soundhw_init();
  2388. qemu_opts_foreach(qemu_find_opts("fw_cfg"),
  2389. parse_fw_cfg, fw_cfg_find(), &error_fatal);
  2390. /* init USB devices */
  2391. if (machine_usb(current_machine)) {
  2392. foreach_device_config_or_exit(DEV_USB, usb_parse);
  2393. }
  2394. /* init generic devices */
  2395. rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
  2396. qemu_opts_foreach(qemu_find_opts("device"),
  2397. device_init_func, NULL, &error_fatal);
  2398. QTAILQ_FOREACH(opt, &device_opts, next) {
  2399. QObject *ret_data = NULL;
  2400. loc_push_restore(&opt->loc);
  2401. qmp_device_add(opt->opts, &ret_data, &error_fatal);
  2402. assert(ret_data == NULL); /* error_fatal aborts */
  2403. loc_pop(&opt->loc);
  2404. }
  2405. rom_reset_order_override();
  2406. }
  2407. static bool qemu_machine_creation_done(Error **errp)
  2408. {
  2409. MachineState *machine = MACHINE(qdev_get_machine());
  2410. /* Did we create any drives that we failed to create a device for? */
  2411. drive_check_orphaned();
  2412. /* Don't warn about the default network setup that you get if
  2413. * no command line -net or -netdev options are specified. There
  2414. * are two cases that we would otherwise complain about:
  2415. * (1) board doesn't support a NIC but the implicit "-net nic"
  2416. * requested one
  2417. * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
  2418. * sets up a nic that isn't connected to anything.
  2419. */
  2420. if (!default_net && (!qtest_enabled() || has_defaults)) {
  2421. net_check_clients();
  2422. }
  2423. qdev_prop_check_globals();
  2424. qdev_machine_creation_done();
  2425. if (machine->cgs && !machine->cgs->ready) {
  2426. error_setg(errp, "accelerator does not support confidential guest %s",
  2427. object_get_typename(OBJECT(machine->cgs)));
  2428. exit(1);
  2429. }
  2430. foreach_device_config_or_exit(DEV_GDB, gdbserver_start);
  2431. if (!vga_interface_created && !default_vga &&
  2432. vga_interface_type != VGA_NONE) {
  2433. warn_report("A -vga option was passed but this machine "
  2434. "type does not use that option; "
  2435. "No VGA device has been created");
  2436. }
  2437. return true;
  2438. }
  2439. void qmp_x_exit_preconfig(Error **errp)
  2440. {
  2441. if (phase_check(PHASE_MACHINE_INITIALIZED)) {
  2442. error_setg(errp, "The command is permitted only before machine initialization");
  2443. return;
  2444. }
  2445. qemu_init_board();
  2446. qemu_create_cli_devices();
  2447. if (!qemu_machine_creation_done(errp)) {
  2448. return;
  2449. }
  2450. if (loadvm) {
  2451. RunState state = autostart ? RUN_STATE_RUNNING : runstate_get();
  2452. load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
  2453. load_snapshot_resume(state);
  2454. }
  2455. if (replay_mode != REPLAY_MODE_NONE) {
  2456. replay_vmstate_init();
  2457. }
  2458. if (incoming) {
  2459. Error *local_err = NULL;
  2460. if (strcmp(incoming, "defer") != 0) {
  2461. g_autofree MigrationChannelList *channels =
  2462. g_new0(MigrationChannelList, 1);
  2463. channels->value = incoming_channels[MIGRATION_CHANNEL_TYPE_MAIN];
  2464. qmp_migrate_incoming(NULL, true, channels, true, true, &local_err);
  2465. if (local_err) {
  2466. error_reportf_err(local_err, "-incoming %s: ", incoming);
  2467. exit(1);
  2468. }
  2469. }
  2470. } else if (autostart) {
  2471. qmp_cont(NULL);
  2472. }
  2473. }
  2474. void qemu_init(int argc, char **argv)
  2475. {
  2476. QemuOpts *opts;
  2477. QemuOpts *icount_opts = NULL, *accel_opts = NULL;
  2478. QemuOptsList *olist;
  2479. int optind;
  2480. const char *optarg;
  2481. MachineClass *machine_class;
  2482. bool userconfig = true;
  2483. FILE *vmstate_dump_file = NULL;
  2484. /* in non-library builds this is done in a constructor */
  2485. #ifdef CONFIG_SHARED_LIBRARY_BUILD
  2486. rcu_register_thread();
  2487. #endif
  2488. qemu_add_opts(&qemu_drive_opts);
  2489. qemu_add_drive_opts(&qemu_legacy_drive_opts);
  2490. qemu_add_drive_opts(&qemu_common_drive_opts);
  2491. qemu_add_drive_opts(&qemu_drive_opts);
  2492. qemu_add_drive_opts(&bdrv_runtime_opts);
  2493. qemu_add_opts(&qemu_chardev_opts);
  2494. qemu_add_opts(&qemu_device_opts);
  2495. qemu_add_opts(&qemu_netdev_opts);
  2496. qemu_add_opts(&qemu_nic_opts);
  2497. qemu_add_opts(&qemu_net_opts);
  2498. qemu_add_opts(&qemu_rtc_opts);
  2499. qemu_add_opts(&qemu_global_opts);
  2500. qemu_add_opts(&qemu_mon_opts);
  2501. qemu_add_opts(&qemu_trace_opts);
  2502. qemu_plugin_add_opts();
  2503. qemu_add_opts(&qemu_option_rom_opts);
  2504. qemu_add_opts(&qemu_accel_opts);
  2505. qemu_add_opts(&qemu_mem_opts);
  2506. qemu_add_opts(&qemu_smp_opts);
  2507. qemu_add_opts(&qemu_boot_opts);
  2508. qemu_add_opts(&qemu_add_fd_opts);
  2509. qemu_add_opts(&qemu_object_opts);
  2510. qemu_add_opts(&qemu_tpmdev_opts);
  2511. qemu_add_opts(&qemu_overcommit_opts);
  2512. qemu_add_opts(&qemu_msg_opts);
  2513. qemu_add_opts(&qemu_name_opts);
  2514. qemu_add_opts(&qemu_numa_opts);
  2515. qemu_add_opts(&qemu_icount_opts);
  2516. qemu_add_opts(&qemu_semihosting_config_opts);
  2517. qemu_add_opts(&qemu_fw_cfg_opts);
  2518. qemu_add_opts(&qemu_action_opts);
  2519. qemu_add_run_with_opts();
  2520. module_call_init(MODULE_INIT_OPTS);
  2521. error_init(argv[0]);
  2522. qemu_init_exec_dir(argv[0]);
  2523. os_setup_limits();
  2524. #ifdef CONFIG_MODULES
  2525. module_init_info(qemu_modinfo);
  2526. module_allow_arch(target_name());
  2527. #endif
  2528. qemu_init_subsystems();
  2529. /* first pass of option parsing */
  2530. optind = 1;
  2531. while (optind < argc) {
  2532. if (argv[optind][0] != '-') {
  2533. /* disk image */
  2534. optind++;
  2535. } else {
  2536. const QEMUOption *popt;
  2537. popt = lookup_opt(argc, argv, &optarg, &optind);
  2538. switch (popt->index) {
  2539. case QEMU_OPTION_nouserconfig:
  2540. userconfig = false;
  2541. break;
  2542. }
  2543. }
  2544. }
  2545. machine_opts_dict = qdict_new();
  2546. if (userconfig) {
  2547. qemu_read_default_config_file(&error_fatal);
  2548. }
  2549. /* second pass of option parsing */
  2550. optind = 1;
  2551. for(;;) {
  2552. if (optind >= argc)
  2553. break;
  2554. if (argv[optind][0] != '-') {
  2555. loc_set_cmdline(argv, optind, 1);
  2556. drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
  2557. } else {
  2558. const QEMUOption *popt;
  2559. popt = lookup_opt(argc, argv, &optarg, &optind);
  2560. if (!qemu_arch_available(popt->arch_mask)) {
  2561. error_report("Option not supported for this target");
  2562. exit(1);
  2563. }
  2564. switch(popt->index) {
  2565. case QEMU_OPTION_cpu:
  2566. /* hw initialization will check this */
  2567. cpu_option = optarg;
  2568. break;
  2569. case QEMU_OPTION_hda:
  2570. case QEMU_OPTION_hdb:
  2571. case QEMU_OPTION_hdc:
  2572. case QEMU_OPTION_hdd:
  2573. drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
  2574. HD_OPTS);
  2575. break;
  2576. case QEMU_OPTION_blockdev:
  2577. {
  2578. Visitor *v;
  2579. BlockdevOptionsQueueEntry *bdo;
  2580. v = qobject_input_visitor_new_str(optarg, "driver",
  2581. &error_fatal);
  2582. bdo = g_new(BlockdevOptionsQueueEntry, 1);
  2583. visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
  2584. &error_fatal);
  2585. visit_free(v);
  2586. loc_save(&bdo->loc);
  2587. QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
  2588. break;
  2589. }
  2590. case QEMU_OPTION_drive:
  2591. opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
  2592. optarg, false);
  2593. if (opts == NULL) {
  2594. exit(1);
  2595. }
  2596. break;
  2597. case QEMU_OPTION_set:
  2598. qemu_set_option(optarg, &error_fatal);
  2599. break;
  2600. case QEMU_OPTION_global:
  2601. if (qemu_global_option(optarg) != 0)
  2602. exit(1);
  2603. break;
  2604. case QEMU_OPTION_mtdblock:
  2605. drive_add(IF_MTD, -1, optarg, MTD_OPTS);
  2606. break;
  2607. case QEMU_OPTION_sd:
  2608. drive_add(IF_SD, -1, optarg, SD_OPTS);
  2609. break;
  2610. case QEMU_OPTION_pflash:
  2611. drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
  2612. break;
  2613. case QEMU_OPTION_snapshot:
  2614. snapshot = 1;
  2615. replay_add_blocker("-snapshot");
  2616. break;
  2617. case QEMU_OPTION_numa:
  2618. opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
  2619. optarg, true);
  2620. if (!opts) {
  2621. exit(1);
  2622. }
  2623. break;
  2624. case QEMU_OPTION_display:
  2625. parse_display(optarg);
  2626. break;
  2627. case QEMU_OPTION_nographic:
  2628. qdict_put_str(machine_opts_dict, "graphics", "off");
  2629. nographic = true;
  2630. dpy.type = DISPLAY_TYPE_NONE;
  2631. break;
  2632. case QEMU_OPTION_kernel:
  2633. qdict_put_str(machine_opts_dict, "kernel", optarg);
  2634. break;
  2635. case QEMU_OPTION_shim:
  2636. qdict_put_str(machine_opts_dict, "shim", optarg);
  2637. break;
  2638. case QEMU_OPTION_initrd:
  2639. qdict_put_str(machine_opts_dict, "initrd", optarg);
  2640. break;
  2641. case QEMU_OPTION_append:
  2642. qdict_put_str(machine_opts_dict, "append", optarg);
  2643. break;
  2644. case QEMU_OPTION_dtb:
  2645. qdict_put_str(machine_opts_dict, "dtb", optarg);
  2646. break;
  2647. case QEMU_OPTION_cdrom:
  2648. drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
  2649. break;
  2650. case QEMU_OPTION_boot:
  2651. machine_parse_property_opt(qemu_find_opts("boot-opts"), "boot", optarg);
  2652. break;
  2653. case QEMU_OPTION_fda:
  2654. case QEMU_OPTION_fdb:
  2655. drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
  2656. optarg, FD_OPTS);
  2657. break;
  2658. case QEMU_OPTION_no_fd_bootchk:
  2659. qdict_put_str(machine_opts_dict, "fd-bootchk", "off");
  2660. break;
  2661. case QEMU_OPTION_netdev:
  2662. default_net = 0;
  2663. if (netdev_is_modern(optarg)) {
  2664. netdev_parse_modern(optarg);
  2665. } else {
  2666. net_client_parse(qemu_find_opts("netdev"), optarg);
  2667. }
  2668. break;
  2669. case QEMU_OPTION_nic:
  2670. default_net = 0;
  2671. net_client_parse(qemu_find_opts("nic"), optarg);
  2672. break;
  2673. case QEMU_OPTION_net:
  2674. default_net = 0;
  2675. net_client_parse(qemu_find_opts("net"), optarg);
  2676. break;
  2677. #ifdef CONFIG_LIBISCSI
  2678. case QEMU_OPTION_iscsi:
  2679. opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
  2680. optarg, false);
  2681. if (!opts) {
  2682. exit(1);
  2683. }
  2684. break;
  2685. #endif
  2686. case QEMU_OPTION_audiodev:
  2687. default_audio = 0;
  2688. audio_parse_option(optarg);
  2689. break;
  2690. case QEMU_OPTION_audio: {
  2691. bool help;
  2692. char *model = NULL;
  2693. Audiodev *dev = NULL;
  2694. Visitor *v;
  2695. QDict *dict = keyval_parse(optarg, "driver", &help, &error_fatal);
  2696. default_audio = 0;
  2697. if (help || (qdict_haskey(dict, "driver") &&
  2698. is_help_option(qdict_get_str(dict, "driver")))) {
  2699. audio_help();
  2700. exit(EXIT_SUCCESS);
  2701. }
  2702. if (!qdict_haskey(dict, "id")) {
  2703. qdict_put_str(dict, "id", "audiodev0");
  2704. }
  2705. if (qdict_haskey(dict, "model")) {
  2706. model = g_strdup(qdict_get_str(dict, "model"));
  2707. qdict_del(dict, "model");
  2708. if (is_help_option(model)) {
  2709. show_valid_soundhw();
  2710. exit(0);
  2711. }
  2712. }
  2713. v = qobject_input_visitor_new_keyval(QOBJECT(dict));
  2714. qobject_unref(dict);
  2715. visit_type_Audiodev(v, NULL, &dev, &error_fatal);
  2716. visit_free(v);
  2717. if (model) {
  2718. audio_define(dev);
  2719. select_soundhw(model, dev->id);
  2720. g_free(model);
  2721. } else {
  2722. audio_define_default(dev, &error_fatal);
  2723. }
  2724. break;
  2725. }
  2726. case QEMU_OPTION_h:
  2727. help(0);
  2728. break;
  2729. case QEMU_OPTION_version:
  2730. version();
  2731. exit(0);
  2732. break;
  2733. case QEMU_OPTION_m:
  2734. opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), optarg, true);
  2735. if (opts == NULL) {
  2736. exit(1);
  2737. }
  2738. break;
  2739. #ifdef CONFIG_TPM
  2740. case QEMU_OPTION_tpmdev:
  2741. if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
  2742. exit(1);
  2743. }
  2744. break;
  2745. #endif
  2746. case QEMU_OPTION_mempath:
  2747. mem_path = optarg;
  2748. break;
  2749. case QEMU_OPTION_mem_prealloc:
  2750. mem_prealloc = 1;
  2751. break;
  2752. case QEMU_OPTION_d:
  2753. log_mask = optarg;
  2754. break;
  2755. case QEMU_OPTION_D:
  2756. log_file = optarg;
  2757. break;
  2758. case QEMU_OPTION_DFILTER:
  2759. qemu_set_dfilter_ranges(optarg, &error_fatal);
  2760. break;
  2761. #if defined(CONFIG_TCG) && defined(CONFIG_LINUX)
  2762. case QEMU_OPTION_perfmap:
  2763. perf_enable_perfmap();
  2764. break;
  2765. case QEMU_OPTION_jitdump:
  2766. perf_enable_jitdump();
  2767. break;
  2768. #endif
  2769. case QEMU_OPTION_seed:
  2770. qemu_guest_random_seed_main(optarg, &error_fatal);
  2771. break;
  2772. case QEMU_OPTION_s:
  2773. add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
  2774. break;
  2775. case QEMU_OPTION_gdb:
  2776. add_device_config(DEV_GDB, optarg);
  2777. break;
  2778. case QEMU_OPTION_L:
  2779. if (is_help_option(optarg)) {
  2780. list_data_dirs = true;
  2781. } else {
  2782. qemu_add_data_dir(g_strdup(optarg));
  2783. }
  2784. break;
  2785. case QEMU_OPTION_bios:
  2786. qdict_put_str(machine_opts_dict, "firmware", optarg);
  2787. break;
  2788. case QEMU_OPTION_S:
  2789. autostart = 0;
  2790. break;
  2791. case QEMU_OPTION_k:
  2792. keyboard_layout = optarg;
  2793. break;
  2794. case QEMU_OPTION_vga:
  2795. vga_model = optarg;
  2796. default_vga = 0;
  2797. break;
  2798. case QEMU_OPTION_g:
  2799. {
  2800. const char *p;
  2801. int w, h, depth;
  2802. p = optarg;
  2803. w = strtol(p, (char **)&p, 10);
  2804. if (w <= 0) {
  2805. graphic_error:
  2806. error_report("invalid resolution or depth");
  2807. exit(1);
  2808. }
  2809. if (*p != 'x')
  2810. goto graphic_error;
  2811. p++;
  2812. h = strtol(p, (char **)&p, 10);
  2813. if (h <= 0)
  2814. goto graphic_error;
  2815. if (*p == 'x') {
  2816. p++;
  2817. depth = strtol(p, (char **)&p, 10);
  2818. if (depth != 1 && depth != 2 && depth != 4 &&
  2819. depth != 8 && depth != 15 && depth != 16 &&
  2820. depth != 24 && depth != 32)
  2821. goto graphic_error;
  2822. } else if (*p == '\0') {
  2823. depth = graphic_depth;
  2824. } else {
  2825. goto graphic_error;
  2826. }
  2827. graphic_width = w;
  2828. graphic_height = h;
  2829. graphic_depth = depth;
  2830. }
  2831. break;
  2832. case QEMU_OPTION_echr:
  2833. {
  2834. char *r;
  2835. term_escape_char = strtol(optarg, &r, 0);
  2836. if (r == optarg)
  2837. printf("Bad argument to echr\n");
  2838. break;
  2839. }
  2840. case QEMU_OPTION_monitor:
  2841. default_monitor = 0;
  2842. if (strncmp(optarg, "none", 4)) {
  2843. monitor_parse(optarg, "readline", false);
  2844. }
  2845. break;
  2846. case QEMU_OPTION_qmp:
  2847. monitor_parse(optarg, "control", false);
  2848. default_monitor = 0;
  2849. break;
  2850. case QEMU_OPTION_qmp_pretty:
  2851. monitor_parse(optarg, "control", true);
  2852. default_monitor = 0;
  2853. break;
  2854. case QEMU_OPTION_mon:
  2855. opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
  2856. true);
  2857. if (!opts) {
  2858. exit(1);
  2859. }
  2860. default_monitor = 0;
  2861. break;
  2862. case QEMU_OPTION_chardev:
  2863. opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
  2864. optarg, true);
  2865. if (!opts) {
  2866. exit(1);
  2867. }
  2868. break;
  2869. case QEMU_OPTION_fsdev:
  2870. olist = qemu_find_opts("fsdev");
  2871. if (!olist) {
  2872. error_report("fsdev support is disabled");
  2873. exit(1);
  2874. }
  2875. opts = qemu_opts_parse_noisily(olist, optarg, true);
  2876. if (!opts) {
  2877. exit(1);
  2878. }
  2879. break;
  2880. case QEMU_OPTION_virtfs: {
  2881. QemuOpts *fsdev;
  2882. QemuOpts *device;
  2883. const char *writeout, *sock_fd, *socket, *path, *security_model,
  2884. *multidevs;
  2885. olist = qemu_find_opts("virtfs");
  2886. if (!olist) {
  2887. error_report("virtfs support is disabled");
  2888. exit(1);
  2889. }
  2890. opts = qemu_opts_parse_noisily(olist, optarg, true);
  2891. if (!opts) {
  2892. exit(1);
  2893. }
  2894. if (qemu_opt_get(opts, "fsdriver") == NULL ||
  2895. qemu_opt_get(opts, "mount_tag") == NULL) {
  2896. error_report("Usage: -virtfs fsdriver,mount_tag=tag");
  2897. exit(1);
  2898. }
  2899. fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
  2900. qemu_opts_id(opts) ?:
  2901. qemu_opt_get(opts, "mount_tag"),
  2902. 1, NULL);
  2903. if (!fsdev) {
  2904. error_report("duplicate or invalid fsdev id: %s",
  2905. qemu_opt_get(opts, "mount_tag"));
  2906. exit(1);
  2907. }
  2908. writeout = qemu_opt_get(opts, "writeout");
  2909. if (writeout) {
  2910. #ifdef CONFIG_SYNC_FILE_RANGE
  2911. qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
  2912. #else
  2913. error_report("writeout=immediate not supported "
  2914. "on this platform");
  2915. exit(1);
  2916. #endif
  2917. }
  2918. qemu_opt_set(fsdev, "fsdriver",
  2919. qemu_opt_get(opts, "fsdriver"), &error_abort);
  2920. path = qemu_opt_get(opts, "path");
  2921. if (path) {
  2922. qemu_opt_set(fsdev, "path", path, &error_abort);
  2923. }
  2924. security_model = qemu_opt_get(opts, "security_model");
  2925. if (security_model) {
  2926. qemu_opt_set(fsdev, "security_model", security_model,
  2927. &error_abort);
  2928. }
  2929. socket = qemu_opt_get(opts, "socket");
  2930. if (socket) {
  2931. qemu_opt_set(fsdev, "socket", socket, &error_abort);
  2932. }
  2933. sock_fd = qemu_opt_get(opts, "sock_fd");
  2934. if (sock_fd) {
  2935. qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
  2936. }
  2937. qemu_opt_set_bool(fsdev, "readonly",
  2938. qemu_opt_get_bool(opts, "readonly", 0),
  2939. &error_abort);
  2940. multidevs = qemu_opt_get(opts, "multidevs");
  2941. if (multidevs) {
  2942. qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
  2943. }
  2944. device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
  2945. &error_abort);
  2946. qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
  2947. qemu_opt_set(device, "fsdev",
  2948. qemu_opts_id(fsdev), &error_abort);
  2949. qemu_opt_set(device, "mount_tag",
  2950. qemu_opt_get(opts, "mount_tag"), &error_abort);
  2951. break;
  2952. }
  2953. case QEMU_OPTION_serial:
  2954. add_device_config(DEV_SERIAL, optarg);
  2955. default_serial = 0;
  2956. if (strncmp(optarg, "mon:", 4) == 0) {
  2957. default_monitor = 0;
  2958. }
  2959. break;
  2960. case QEMU_OPTION_action:
  2961. olist = qemu_find_opts("action");
  2962. if (!qemu_opts_parse_noisily(olist, optarg, false)) {
  2963. exit(1);
  2964. }
  2965. break;
  2966. case QEMU_OPTION_watchdog_action: {
  2967. opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
  2968. qemu_opt_set(opts, "watchdog", optarg, &error_abort);
  2969. break;
  2970. }
  2971. case QEMU_OPTION_parallel:
  2972. add_device_config(DEV_PARALLEL, optarg);
  2973. default_parallel = 0;
  2974. if (strncmp(optarg, "mon:", 4) == 0) {
  2975. default_monitor = 0;
  2976. }
  2977. break;
  2978. case QEMU_OPTION_debugcon:
  2979. add_device_config(DEV_DEBUGCON, optarg);
  2980. break;
  2981. case QEMU_OPTION_loadvm:
  2982. loadvm = optarg;
  2983. break;
  2984. case QEMU_OPTION_full_screen:
  2985. dpy.has_full_screen = true;
  2986. dpy.full_screen = true;
  2987. break;
  2988. case QEMU_OPTION_pidfile:
  2989. pid_file = optarg;
  2990. break;
  2991. case QEMU_OPTION_win2k_hack:
  2992. object_register_sugar_prop("ide-device", "win2k-install-hack", "true", true);
  2993. break;
  2994. case QEMU_OPTION_acpitable:
  2995. opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
  2996. optarg, true);
  2997. if (!opts) {
  2998. exit(1);
  2999. }
  3000. acpi_table_add(opts, &error_fatal);
  3001. break;
  3002. case QEMU_OPTION_smbios:
  3003. opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
  3004. optarg, false);
  3005. if (!opts) {
  3006. exit(1);
  3007. }
  3008. smbios_entry_add(opts, &error_fatal);
  3009. break;
  3010. case QEMU_OPTION_fwcfg:
  3011. opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
  3012. optarg, true);
  3013. if (opts == NULL) {
  3014. exit(1);
  3015. }
  3016. break;
  3017. case QEMU_OPTION_preconfig:
  3018. preconfig_requested = true;
  3019. break;
  3020. case QEMU_OPTION_enable_kvm:
  3021. qdict_put_str(machine_opts_dict, "accel", "kvm");
  3022. break;
  3023. case QEMU_OPTION_M:
  3024. case QEMU_OPTION_machine:
  3025. {
  3026. bool help;
  3027. keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
  3028. if (help) {
  3029. machine_help_func(machine_opts_dict);
  3030. exit(EXIT_SUCCESS);
  3031. }
  3032. break;
  3033. }
  3034. case QEMU_OPTION_accel:
  3035. accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
  3036. optarg, true);
  3037. optarg = qemu_opt_get(accel_opts, "accel");
  3038. if (!optarg || is_help_option(optarg)) {
  3039. printf("Accelerators supported in QEMU binary:\n");
  3040. GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
  3041. false);
  3042. for (el = accel_list; el; el = el->next) {
  3043. gchar *typename = g_strdup(object_class_get_name(
  3044. OBJECT_CLASS(el->data)));
  3045. /* omit qtest which is used for tests only */
  3046. if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
  3047. g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
  3048. gchar **optname = g_strsplit(typename,
  3049. ACCEL_CLASS_SUFFIX, 0);
  3050. printf("%s\n", optname[0]);
  3051. g_strfreev(optname);
  3052. }
  3053. g_free(typename);
  3054. }
  3055. g_slist_free(accel_list);
  3056. exit(0);
  3057. }
  3058. break;
  3059. case QEMU_OPTION_usb:
  3060. qdict_put_str(machine_opts_dict, "usb", "on");
  3061. break;
  3062. case QEMU_OPTION_usbdevice:
  3063. qdict_put_str(machine_opts_dict, "usb", "on");
  3064. add_device_config(DEV_USB, optarg);
  3065. break;
  3066. case QEMU_OPTION_device:
  3067. if (optarg[0] == '{') {
  3068. QObject *obj = qobject_from_json(optarg, &error_fatal);
  3069. DeviceOption *opt = g_new0(DeviceOption, 1);
  3070. opt->opts = qobject_to(QDict, obj);
  3071. loc_save(&opt->loc);
  3072. assert(opt->opts != NULL);
  3073. QTAILQ_INSERT_TAIL(&device_opts, opt, next);
  3074. } else {
  3075. if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
  3076. optarg, true)) {
  3077. exit(1);
  3078. }
  3079. }
  3080. break;
  3081. case QEMU_OPTION_smp:
  3082. machine_parse_property_opt(qemu_find_opts("smp-opts"),
  3083. "smp", optarg);
  3084. break;
  3085. #ifdef CONFIG_VNC
  3086. case QEMU_OPTION_vnc:
  3087. vnc_parse(optarg);
  3088. display_remote++;
  3089. break;
  3090. #endif
  3091. case QEMU_OPTION_no_reboot:
  3092. olist = qemu_find_opts("action");
  3093. qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
  3094. break;
  3095. case QEMU_OPTION_no_shutdown:
  3096. olist = qemu_find_opts("action");
  3097. qemu_opts_parse_noisily(olist, "shutdown=pause", false);
  3098. break;
  3099. case QEMU_OPTION_uuid:
  3100. if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
  3101. error_report("failed to parse UUID string: wrong format");
  3102. exit(1);
  3103. }
  3104. qemu_uuid_set = true;
  3105. break;
  3106. case QEMU_OPTION_option_rom:
  3107. if (nb_option_roms >= MAX_OPTION_ROMS) {
  3108. error_report("too many option ROMs");
  3109. exit(1);
  3110. }
  3111. opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
  3112. optarg, true);
  3113. if (!opts) {
  3114. exit(1);
  3115. }
  3116. option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
  3117. option_rom[nb_option_roms].bootindex =
  3118. qemu_opt_get_number(opts, "bootindex", -1);
  3119. if (!option_rom[nb_option_roms].name) {
  3120. error_report("Option ROM file is not specified");
  3121. exit(1);
  3122. }
  3123. nb_option_roms++;
  3124. break;
  3125. case QEMU_OPTION_semihosting:
  3126. qemu_semihosting_enable();
  3127. break;
  3128. case QEMU_OPTION_semihosting_config:
  3129. if (qemu_semihosting_config_options(optarg) != 0) {
  3130. exit(1);
  3131. }
  3132. break;
  3133. case QEMU_OPTION_name:
  3134. opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
  3135. optarg, true);
  3136. if (!opts) {
  3137. exit(1);
  3138. }
  3139. /* Capture guest name if -msg guest-name is used later */
  3140. error_guest_name = qemu_opt_get(opts, "guest");
  3141. break;
  3142. case QEMU_OPTION_prom_env:
  3143. if (nb_prom_envs >= MAX_PROM_ENVS) {
  3144. error_report("too many prom variables");
  3145. exit(1);
  3146. }
  3147. prom_envs[nb_prom_envs] = optarg;
  3148. nb_prom_envs++;
  3149. break;
  3150. case QEMU_OPTION_old_param:
  3151. warn_report("-old-param is deprecated");
  3152. old_param = 1;
  3153. break;
  3154. case QEMU_OPTION_rtc:
  3155. opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
  3156. false);
  3157. if (!opts) {
  3158. exit(1);
  3159. }
  3160. break;
  3161. case QEMU_OPTION_icount:
  3162. icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
  3163. optarg, true);
  3164. if (!icount_opts) {
  3165. exit(1);
  3166. }
  3167. break;
  3168. case QEMU_OPTION_incoming:
  3169. if (!incoming) {
  3170. runstate_set(RUN_STATE_INMIGRATE);
  3171. }
  3172. incoming_option_parse(optarg);
  3173. break;
  3174. case QEMU_OPTION_only_migratable:
  3175. only_migratable = 1;
  3176. break;
  3177. case QEMU_OPTION_nodefaults:
  3178. has_defaults = 0;
  3179. break;
  3180. case QEMU_OPTION_xen_domid:
  3181. if (!(accel_find("xen")) && !(accel_find("kvm"))) {
  3182. error_report("Option not supported for this target");
  3183. exit(1);
  3184. }
  3185. xen_domid = atoi(optarg);
  3186. break;
  3187. case QEMU_OPTION_xen_attach:
  3188. if (!(accel_find("xen"))) {
  3189. error_report("Option not supported for this target");
  3190. exit(1);
  3191. }
  3192. xen_mode = XEN_ATTACH;
  3193. break;
  3194. case QEMU_OPTION_xen_domid_restrict:
  3195. if (!(accel_find("xen"))) {
  3196. error_report("Option not supported for this target");
  3197. exit(1);
  3198. }
  3199. xen_domid_restrict = true;
  3200. break;
  3201. case QEMU_OPTION_trace:
  3202. trace_opt_parse(optarg);
  3203. break;
  3204. case QEMU_OPTION_plugin:
  3205. qemu_plugin_opt_parse(optarg, &plugin_list);
  3206. break;
  3207. case QEMU_OPTION_readconfig:
  3208. qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
  3209. break;
  3210. #ifdef CONFIG_SPICE
  3211. case QEMU_OPTION_spice:
  3212. opts = qemu_opts_parse_noisily(qemu_find_opts("spice"), optarg, false);
  3213. if (!opts) {
  3214. exit(1);
  3215. }
  3216. display_remote++;
  3217. break;
  3218. #endif
  3219. case QEMU_OPTION_qtest:
  3220. qtest_chrdev = optarg;
  3221. break;
  3222. case QEMU_OPTION_qtest_log:
  3223. qtest_log = optarg;
  3224. break;
  3225. case QEMU_OPTION_sandbox:
  3226. olist = qemu_find_opts("sandbox");
  3227. if (!olist) {
  3228. #ifndef CONFIG_SECCOMP
  3229. error_report("-sandbox support is not enabled "
  3230. "in this QEMU binary");
  3231. #endif
  3232. exit(1);
  3233. }
  3234. opts = qemu_opts_parse_noisily(olist, optarg, true);
  3235. if (!opts) {
  3236. exit(1);
  3237. }
  3238. break;
  3239. case QEMU_OPTION_add_fd:
  3240. #ifndef _WIN32
  3241. opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
  3242. optarg, false);
  3243. if (!opts) {
  3244. exit(1);
  3245. }
  3246. #else
  3247. error_report("File descriptor passing is disabled on this "
  3248. "platform");
  3249. exit(1);
  3250. #endif
  3251. break;
  3252. case QEMU_OPTION_object:
  3253. object_option_parse(optarg);
  3254. break;
  3255. case QEMU_OPTION_overcommit:
  3256. overcommit_parse(optarg);
  3257. break;
  3258. case QEMU_OPTION_compat:
  3259. {
  3260. CompatPolicy *opts_policy;
  3261. Visitor *v;
  3262. v = qobject_input_visitor_new_str(optarg, NULL,
  3263. &error_fatal);
  3264. visit_type_CompatPolicy(v, NULL, &opts_policy, &error_fatal);
  3265. QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts_policy);
  3266. qapi_free_CompatPolicy(opts_policy);
  3267. visit_free(v);
  3268. break;
  3269. }
  3270. case QEMU_OPTION_msg:
  3271. opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
  3272. false);
  3273. if (!opts) {
  3274. exit(1);
  3275. }
  3276. configure_msg(opts);
  3277. break;
  3278. case QEMU_OPTION_dump_vmstate:
  3279. if (vmstate_dump_file) {
  3280. error_report("only one '-dump-vmstate' "
  3281. "option may be given");
  3282. exit(1);
  3283. }
  3284. vmstate_dump_file = fopen(optarg, "w");
  3285. if (vmstate_dump_file == NULL) {
  3286. error_report("open %s: %s", optarg, strerror(errno));
  3287. exit(1);
  3288. }
  3289. break;
  3290. case QEMU_OPTION_enable_sync_profile:
  3291. qsp_enable();
  3292. break;
  3293. case QEMU_OPTION_nouserconfig:
  3294. /* Nothing to be parsed here. Especially, do not error out below. */
  3295. break;
  3296. #if defined(CONFIG_POSIX)
  3297. case QEMU_OPTION_daemonize:
  3298. os_set_daemonize(true);
  3299. break;
  3300. case QEMU_OPTION_run_with: {
  3301. const char *str;
  3302. opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"),
  3303. optarg, false);
  3304. if (!opts) {
  3305. exit(1);
  3306. }
  3307. #if defined(CONFIG_LINUX)
  3308. if (qemu_opt_get_bool(opts, "async-teardown", false)) {
  3309. init_async_teardown();
  3310. }
  3311. #endif
  3312. str = qemu_opt_get(opts, "chroot");
  3313. if (str) {
  3314. os_set_chroot(str);
  3315. }
  3316. str = qemu_opt_get(opts, "user");
  3317. if (str) {
  3318. if (!os_set_runas(str)) {
  3319. error_report("User \"%s\" doesn't exist"
  3320. " (and is not <uid>:<gid>)",
  3321. optarg);
  3322. exit(1);
  3323. }
  3324. }
  3325. break;
  3326. }
  3327. #endif /* CONFIG_POSIX */
  3328. default:
  3329. error_report("Option not supported in this build");
  3330. exit(1);
  3331. }
  3332. }
  3333. }
  3334. /*
  3335. * Clear error location left behind by the loop.
  3336. * Best done right after the loop. Do not insert code here!
  3337. */
  3338. loc_set_none();
  3339. qemu_validate_options(machine_opts_dict);
  3340. qemu_process_sugar_options();
  3341. /*
  3342. * These options affect everything else and should be processed
  3343. * before daemonizing.
  3344. */
  3345. qemu_process_early_options();
  3346. qemu_process_help_options();
  3347. qemu_maybe_daemonize(pid_file);
  3348. /*
  3349. * The trace backend must be initialized after daemonizing.
  3350. * trace_init_backends() will call st_init(), which will create the
  3351. * trace thread in the parent, and also register st_flush_trace_buffer()
  3352. * in atexit(). This function will force the parent to wait for the
  3353. * writeout thread to finish, which will not occur, and the parent
  3354. * process will be left in the host.
  3355. */
  3356. if (!trace_init_backends()) {
  3357. exit(1);
  3358. }
  3359. trace_init_file();
  3360. qemu_init_main_loop(&error_fatal);
  3361. cpu_timers_init();
  3362. user_register_global_props();
  3363. replay_configure(icount_opts);
  3364. configure_rtc(qemu_find_opts_singleton("rtc"));
  3365. /* Transfer QemuOpts options into machine options */
  3366. parse_memory_options();
  3367. qemu_create_machine(machine_opts_dict);
  3368. /*
  3369. * Load incoming CPR state before any devices are created, because it
  3370. * contains file descriptors that are needed in device initialization code.
  3371. */
  3372. cpr_state_load(incoming_channels[MIGRATION_CHANNEL_TYPE_CPR], &error_fatal);
  3373. suspend_mux_open();
  3374. qemu_disable_default_devices();
  3375. qemu_setup_display();
  3376. qemu_create_default_devices();
  3377. qemu_create_early_backends();
  3378. qemu_apply_legacy_machine_options(machine_opts_dict);
  3379. qemu_apply_machine_options(machine_opts_dict);
  3380. qobject_unref(machine_opts_dict);
  3381. phase_advance(PHASE_MACHINE_CREATED);
  3382. /*
  3383. * Note: uses machine properties such as kernel-irqchip, must run
  3384. * after qemu_apply_machine_options.
  3385. */
  3386. configure_accelerators(argv[0]);
  3387. phase_advance(PHASE_ACCEL_CREATED);
  3388. /*
  3389. * Beware, QOM objects created before this point miss global and
  3390. * compat properties.
  3391. *
  3392. * Global properties get set up by qdev_prop_register_global(),
  3393. * called from user_register_global_props(), and certain option
  3394. * desugaring. Also in CPU feature desugaring (buried in
  3395. * parse_cpu_option()), which happens below this point, but may
  3396. * only target the CPU type, which can only be created after
  3397. * parse_cpu_option() returned the type.
  3398. *
  3399. * Machine compat properties: object_set_machine_compat_props().
  3400. * Accelerator compat props: object_set_accelerator_compat_props(),
  3401. * called from do_configure_accelerator().
  3402. */
  3403. machine_class = MACHINE_GET_CLASS(current_machine);
  3404. if (!qtest_enabled() && machine_class->deprecation_reason) {
  3405. warn_report("Machine type '%s' is deprecated: %s",
  3406. machine_class->name, machine_class->deprecation_reason);
  3407. }
  3408. /*
  3409. * Create backends before creating migration objects, so that it can
  3410. * check against compatibilities on the backend memories (e.g. postcopy
  3411. * over memory-backend-file objects).
  3412. */
  3413. qemu_create_late_backends();
  3414. phase_advance(PHASE_LATE_BACKENDS_CREATED);
  3415. /*
  3416. * Note: creates a QOM object, must run only after global and
  3417. * compat properties have been set up.
  3418. */
  3419. migration_object_init();
  3420. /* parse features once if machine provides default cpu_type */
  3421. current_machine->cpu_type = machine_class_default_cpu_type(machine_class);
  3422. if (cpu_option) {
  3423. current_machine->cpu_type = parse_cpu_option(cpu_option);
  3424. }
  3425. /* NB: for machine none cpu_type could STILL be NULL here! */
  3426. qemu_resolve_machine_memdev();
  3427. parse_numa_opts(current_machine);
  3428. if (vmstate_dump_file) {
  3429. /* dump and exit */
  3430. module_load_qom_all();
  3431. dump_vmstate_json_to_file(vmstate_dump_file);
  3432. exit(0);
  3433. }
  3434. if (!preconfig_requested) {
  3435. qmp_x_exit_preconfig(&error_fatal);
  3436. }
  3437. qemu_init_displays();
  3438. accel_setup_post(current_machine);
  3439. os_setup_post();
  3440. resume_mux_open();
  3441. }