vl.c 127 KB

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