vl.c 124 KB

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