vl.c 128 KB

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