vl.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663
  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 <zlib.h>
  31. /* Needed early for CONFIG_BSD etc. */
  32. #include "config-host.h"
  33. #ifndef _WIN32
  34. #include <libgen.h>
  35. #include <sys/times.h>
  36. #include <sys/wait.h>
  37. #include <termios.h>
  38. #include <sys/mman.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/resource.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <net/if.h>
  44. #include <arpa/inet.h>
  45. #include <dirent.h>
  46. #include <netdb.h>
  47. #include <sys/select.h>
  48. #ifdef CONFIG_BSD
  49. #include <sys/stat.h>
  50. #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
  51. #include <libutil.h>
  52. #include <sys/sysctl.h>
  53. #else
  54. #include <util.h>
  55. #endif
  56. #else
  57. #ifdef __linux__
  58. #include <pty.h>
  59. #include <malloc.h>
  60. #include <linux/ppdev.h>
  61. #include <linux/parport.h>
  62. #endif
  63. #ifdef __sun__
  64. #include <sys/stat.h>
  65. #include <sys/ethernet.h>
  66. #include <sys/sockio.h>
  67. #include <netinet/arp.h>
  68. #include <netinet/in_systm.h>
  69. #include <netinet/ip.h>
  70. #include <netinet/ip_icmp.h> // must come after ip.h
  71. #include <netinet/udp.h>
  72. #include <netinet/tcp.h>
  73. #include <net/if.h>
  74. #include <syslog.h>
  75. #include <stropts.h>
  76. #endif
  77. #endif
  78. #endif
  79. #if defined(__OpenBSD__)
  80. #include <util.h>
  81. #endif
  82. #if defined(CONFIG_VDE)
  83. #include <libvdeplug.h>
  84. #endif
  85. #ifdef _WIN32
  86. #include <windows.h>
  87. #endif
  88. #ifdef CONFIG_SDL
  89. #if defined(__APPLE__) || defined(main)
  90. #include <SDL.h>
  91. int qemu_main(int argc, char **argv, char **envp);
  92. int main(int argc, char **argv)
  93. {
  94. return qemu_main(argc, argv, NULL);
  95. }
  96. #undef main
  97. #define main qemu_main
  98. #endif
  99. #endif /* CONFIG_SDL */
  100. #ifdef CONFIG_COCOA
  101. #undef main
  102. #define main qemu_main
  103. #endif /* CONFIG_COCOA */
  104. #include <glib.h>
  105. #include "hw/hw.h"
  106. #include "hw/boards.h"
  107. #include "hw/usb.h"
  108. #include "hw/pcmcia.h"
  109. #include "hw/pc.h"
  110. #include "hw/isa.h"
  111. #include "hw/baum.h"
  112. #include "hw/bt.h"
  113. #include "hw/watchdog.h"
  114. #include "hw/smbios.h"
  115. #include "hw/xen.h"
  116. #include "hw/qdev.h"
  117. #include "hw/loader.h"
  118. #include "bt-host.h"
  119. #include "net.h"
  120. #include "net/slirp.h"
  121. #include "monitor.h"
  122. #include "console.h"
  123. #include "sysemu.h"
  124. #include "gdbstub.h"
  125. #include "qemu-timer.h"
  126. #include "qemu-char.h"
  127. #include "cache-utils.h"
  128. #include "block.h"
  129. #include "blockdev.h"
  130. #include "block-migration.h"
  131. #include "dma.h"
  132. #include "audio/audio.h"
  133. #include "migration.h"
  134. #include "kvm.h"
  135. #include "qjson.h"
  136. #include "qemu-option.h"
  137. #include "qemu-config.h"
  138. #include "qemu-options.h"
  139. #include "qmp-commands.h"
  140. #include "main-loop.h"
  141. #ifdef CONFIG_VIRTFS
  142. #include "fsdev/qemu-fsdev.h"
  143. #endif
  144. #include "qtest.h"
  145. #include "disas.h"
  146. #include "qemu_socket.h"
  147. #include "slirp/libslirp.h"
  148. #include "trace.h"
  149. #include "trace/control.h"
  150. #include "qemu-queue.h"
  151. #include "cpus.h"
  152. #include "arch_init.h"
  153. #include "ui/qemu-spice.h"
  154. //#define DEBUG_NET
  155. //#define DEBUG_SLIRP
  156. #define DEFAULT_RAM_SIZE 128
  157. #define MAX_VIRTIO_CONSOLES 1
  158. static const char *data_dir;
  159. const char *bios_name = NULL;
  160. enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
  161. DisplayType display_type = DT_DEFAULT;
  162. int display_remote = 0;
  163. const char* keyboard_layout = NULL;
  164. ram_addr_t ram_size;
  165. const char *mem_path = NULL;
  166. #ifdef MAP_POPULATE
  167. int mem_prealloc = 0; /* force preallocation of physical target memory */
  168. #endif
  169. int nb_nics;
  170. NICInfo nd_table[MAX_NICS];
  171. int autostart;
  172. static int rtc_utc = 1;
  173. static int rtc_date_offset = -1; /* -1 means no change */
  174. QEMUClock *rtc_clock;
  175. int vga_interface_type = VGA_NONE;
  176. static int full_screen = 0;
  177. #ifdef CONFIG_SDL
  178. static int no_frame = 0;
  179. #endif
  180. int no_quit = 0;
  181. CharDriverState *serial_hds[MAX_SERIAL_PORTS];
  182. CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
  183. CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
  184. int win2k_install_hack = 0;
  185. int usb_enabled = 0;
  186. int singlestep = 0;
  187. int smp_cpus = 1;
  188. int max_cpus = 0;
  189. int smp_cores = 1;
  190. int smp_threads = 1;
  191. #ifdef CONFIG_VNC
  192. const char *vnc_display;
  193. #endif
  194. int acpi_enabled = 1;
  195. int no_hpet = 0;
  196. int fd_bootchk = 1;
  197. int no_reboot = 0;
  198. int no_shutdown = 0;
  199. int cursor_hide = 1;
  200. int graphic_rotate = 0;
  201. const char *watchdog;
  202. QEMUOptionRom option_rom[MAX_OPTION_ROMS];
  203. int nb_option_roms;
  204. int semihosting_enabled = 0;
  205. int old_param = 0;
  206. const char *qemu_name;
  207. int alt_grab = 0;
  208. int ctrl_grab = 0;
  209. unsigned int nb_prom_envs = 0;
  210. const char *prom_envs[MAX_PROM_ENVS];
  211. int boot_menu;
  212. uint8_t *boot_splash_filedata;
  213. int boot_splash_filedata_size;
  214. uint8_t qemu_extra_params_fw[2];
  215. typedef struct FWBootEntry FWBootEntry;
  216. struct FWBootEntry {
  217. QTAILQ_ENTRY(FWBootEntry) link;
  218. int32_t bootindex;
  219. DeviceState *dev;
  220. char *suffix;
  221. };
  222. QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
  223. int nb_numa_nodes;
  224. uint64_t node_mem[MAX_NODES];
  225. uint64_t node_cpumask[MAX_NODES];
  226. uint8_t qemu_uuid[16];
  227. static QEMUBootSetHandler *boot_set_handler;
  228. static void *boot_set_opaque;
  229. static NotifierList exit_notifiers =
  230. NOTIFIER_LIST_INITIALIZER(exit_notifiers);
  231. static NotifierList machine_init_done_notifiers =
  232. NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
  233. static int tcg_allowed = 1;
  234. int kvm_allowed = 0;
  235. int xen_allowed = 0;
  236. uint32_t xen_domid;
  237. enum xen_mode xen_mode = XEN_EMULATE;
  238. static int tcg_tb_size;
  239. static int default_serial = 1;
  240. static int default_parallel = 1;
  241. static int default_virtcon = 1;
  242. static int default_monitor = 1;
  243. static int default_floppy = 1;
  244. static int default_cdrom = 1;
  245. static int default_sdcard = 1;
  246. static int default_vga = 1;
  247. static struct {
  248. const char *driver;
  249. int *flag;
  250. } default_list[] = {
  251. { .driver = "isa-serial", .flag = &default_serial },
  252. { .driver = "isa-parallel", .flag = &default_parallel },
  253. { .driver = "isa-fdc", .flag = &default_floppy },
  254. { .driver = "ide-cd", .flag = &default_cdrom },
  255. { .driver = "ide-hd", .flag = &default_cdrom },
  256. { .driver = "ide-drive", .flag = &default_cdrom },
  257. { .driver = "scsi-cd", .flag = &default_cdrom },
  258. { .driver = "virtio-serial-pci", .flag = &default_virtcon },
  259. { .driver = "virtio-serial-s390", .flag = &default_virtcon },
  260. { .driver = "virtio-serial", .flag = &default_virtcon },
  261. { .driver = "VGA", .flag = &default_vga },
  262. { .driver = "isa-vga", .flag = &default_vga },
  263. { .driver = "cirrus-vga", .flag = &default_vga },
  264. { .driver = "isa-cirrus-vga", .flag = &default_vga },
  265. { .driver = "vmware-svga", .flag = &default_vga },
  266. { .driver = "qxl-vga", .flag = &default_vga },
  267. };
  268. static void res_free(void)
  269. {
  270. if (boot_splash_filedata != NULL) {
  271. g_free(boot_splash_filedata);
  272. boot_splash_filedata = NULL;
  273. }
  274. }
  275. static int default_driver_check(QemuOpts *opts, void *opaque)
  276. {
  277. const char *driver = qemu_opt_get(opts, "driver");
  278. int i;
  279. if (!driver)
  280. return 0;
  281. for (i = 0; i < ARRAY_SIZE(default_list); i++) {
  282. if (strcmp(default_list[i].driver, driver) != 0)
  283. continue;
  284. *(default_list[i].flag) = 0;
  285. }
  286. return 0;
  287. }
  288. /***********************************************************/
  289. /* QEMU state */
  290. static RunState current_run_state = RUN_STATE_PRELAUNCH;
  291. typedef struct {
  292. RunState from;
  293. RunState to;
  294. } RunStateTransition;
  295. static const RunStateTransition runstate_transitions_def[] = {
  296. /* from -> to */
  297. { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
  298. { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
  299. { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
  300. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
  301. { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
  302. { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
  303. { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
  304. { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
  305. { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
  306. { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
  307. { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
  308. { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
  309. { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
  310. { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
  311. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
  312. { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
  313. { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
  314. { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
  315. { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
  316. { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
  317. { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
  318. { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
  319. { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
  320. { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
  321. { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
  322. { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
  323. { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
  324. { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
  325. { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
  326. { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
  327. { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
  328. { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
  329. { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
  330. { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
  331. { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
  332. { RUN_STATE_MAX, RUN_STATE_MAX },
  333. };
  334. static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
  335. bool runstate_check(RunState state)
  336. {
  337. return current_run_state == state;
  338. }
  339. void runstate_init(void)
  340. {
  341. const RunStateTransition *p;
  342. memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
  343. for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
  344. runstate_valid_transitions[p->from][p->to] = true;
  345. }
  346. }
  347. /* This function will abort() on invalid state transitions */
  348. void runstate_set(RunState new_state)
  349. {
  350. assert(new_state < RUN_STATE_MAX);
  351. if (!runstate_valid_transitions[current_run_state][new_state]) {
  352. fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
  353. RunState_lookup[current_run_state],
  354. RunState_lookup[new_state]);
  355. abort();
  356. }
  357. current_run_state = new_state;
  358. }
  359. int runstate_is_running(void)
  360. {
  361. return runstate_check(RUN_STATE_RUNNING);
  362. }
  363. StatusInfo *qmp_query_status(Error **errp)
  364. {
  365. StatusInfo *info = g_malloc0(sizeof(*info));
  366. info->running = runstate_is_running();
  367. info->singlestep = singlestep;
  368. info->status = current_run_state;
  369. return info;
  370. }
  371. /***********************************************************/
  372. /* real time host monotonic timer */
  373. /***********************************************************/
  374. /* host time/date access */
  375. void qemu_get_timedate(struct tm *tm, int offset)
  376. {
  377. time_t ti;
  378. struct tm *ret;
  379. time(&ti);
  380. ti += offset;
  381. if (rtc_date_offset == -1) {
  382. if (rtc_utc)
  383. ret = gmtime(&ti);
  384. else
  385. ret = localtime(&ti);
  386. } else {
  387. ti -= rtc_date_offset;
  388. ret = gmtime(&ti);
  389. }
  390. memcpy(tm, ret, sizeof(struct tm));
  391. }
  392. int qemu_timedate_diff(struct tm *tm)
  393. {
  394. time_t seconds;
  395. if (rtc_date_offset == -1)
  396. if (rtc_utc)
  397. seconds = mktimegm(tm);
  398. else {
  399. struct tm tmp = *tm;
  400. tmp.tm_isdst = -1; /* use timezone to figure it out */
  401. seconds = mktime(&tmp);
  402. }
  403. else
  404. seconds = mktimegm(tm) + rtc_date_offset;
  405. return seconds - time(NULL);
  406. }
  407. void rtc_change_mon_event(struct tm *tm)
  408. {
  409. QObject *data;
  410. data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
  411. monitor_protocol_event(QEVENT_RTC_CHANGE, data);
  412. qobject_decref(data);
  413. }
  414. static void configure_rtc_date_offset(const char *startdate, int legacy)
  415. {
  416. time_t rtc_start_date;
  417. struct tm tm;
  418. if (!strcmp(startdate, "now") && legacy) {
  419. rtc_date_offset = -1;
  420. } else {
  421. if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
  422. &tm.tm_year,
  423. &tm.tm_mon,
  424. &tm.tm_mday,
  425. &tm.tm_hour,
  426. &tm.tm_min,
  427. &tm.tm_sec) == 6) {
  428. /* OK */
  429. } else if (sscanf(startdate, "%d-%d-%d",
  430. &tm.tm_year,
  431. &tm.tm_mon,
  432. &tm.tm_mday) == 3) {
  433. tm.tm_hour = 0;
  434. tm.tm_min = 0;
  435. tm.tm_sec = 0;
  436. } else {
  437. goto date_fail;
  438. }
  439. tm.tm_year -= 1900;
  440. tm.tm_mon--;
  441. rtc_start_date = mktimegm(&tm);
  442. if (rtc_start_date == -1) {
  443. date_fail:
  444. fprintf(stderr, "Invalid date format. Valid formats are:\n"
  445. "'2006-06-17T16:01:21' or '2006-06-17'\n");
  446. exit(1);
  447. }
  448. rtc_date_offset = time(NULL) - rtc_start_date;
  449. }
  450. }
  451. static void configure_rtc(QemuOpts *opts)
  452. {
  453. const char *value;
  454. value = qemu_opt_get(opts, "base");
  455. if (value) {
  456. if (!strcmp(value, "utc")) {
  457. rtc_utc = 1;
  458. } else if (!strcmp(value, "localtime")) {
  459. rtc_utc = 0;
  460. } else {
  461. configure_rtc_date_offset(value, 0);
  462. }
  463. }
  464. value = qemu_opt_get(opts, "clock");
  465. if (value) {
  466. if (!strcmp(value, "host")) {
  467. rtc_clock = host_clock;
  468. } else if (!strcmp(value, "rt")) {
  469. rtc_clock = rt_clock;
  470. } else if (!strcmp(value, "vm")) {
  471. rtc_clock = vm_clock;
  472. } else {
  473. fprintf(stderr, "qemu: invalid option value '%s'\n", value);
  474. exit(1);
  475. }
  476. }
  477. value = qemu_opt_get(opts, "driftfix");
  478. if (value) {
  479. if (!strcmp(value, "slew")) {
  480. static GlobalProperty slew_lost_ticks[] = {
  481. {
  482. .driver = "mc146818rtc",
  483. .property = "lost_tick_policy",
  484. .value = "slew",
  485. },
  486. { /* end of list */ }
  487. };
  488. qdev_prop_register_global_list(slew_lost_ticks);
  489. } else if (!strcmp(value, "none")) {
  490. /* discard is default */
  491. } else {
  492. fprintf(stderr, "qemu: invalid option value '%s'\n", value);
  493. exit(1);
  494. }
  495. }
  496. }
  497. /***********************************************************/
  498. /* Bluetooth support */
  499. static int nb_hcis;
  500. static int cur_hci;
  501. static struct HCIInfo *hci_table[MAX_NICS];
  502. static struct bt_vlan_s {
  503. struct bt_scatternet_s net;
  504. int id;
  505. struct bt_vlan_s *next;
  506. } *first_bt_vlan;
  507. /* find or alloc a new bluetooth "VLAN" */
  508. static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
  509. {
  510. struct bt_vlan_s **pvlan, *vlan;
  511. for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
  512. if (vlan->id == id)
  513. return &vlan->net;
  514. }
  515. vlan = g_malloc0(sizeof(struct bt_vlan_s));
  516. vlan->id = id;
  517. pvlan = &first_bt_vlan;
  518. while (*pvlan != NULL)
  519. pvlan = &(*pvlan)->next;
  520. *pvlan = vlan;
  521. return &vlan->net;
  522. }
  523. static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
  524. {
  525. }
  526. static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
  527. {
  528. return -ENOTSUP;
  529. }
  530. static struct HCIInfo null_hci = {
  531. .cmd_send = null_hci_send,
  532. .sco_send = null_hci_send,
  533. .acl_send = null_hci_send,
  534. .bdaddr_set = null_hci_addr_set,
  535. };
  536. struct HCIInfo *qemu_next_hci(void)
  537. {
  538. if (cur_hci == nb_hcis)
  539. return &null_hci;
  540. return hci_table[cur_hci++];
  541. }
  542. static struct HCIInfo *hci_init(const char *str)
  543. {
  544. char *endp;
  545. struct bt_scatternet_s *vlan = 0;
  546. if (!strcmp(str, "null"))
  547. /* null */
  548. return &null_hci;
  549. else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
  550. /* host[:hciN] */
  551. return bt_host_hci(str[4] ? str + 5 : "hci0");
  552. else if (!strncmp(str, "hci", 3)) {
  553. /* hci[,vlan=n] */
  554. if (str[3]) {
  555. if (!strncmp(str + 3, ",vlan=", 6)) {
  556. vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
  557. if (*endp)
  558. vlan = 0;
  559. }
  560. } else
  561. vlan = qemu_find_bt_vlan(0);
  562. if (vlan)
  563. return bt_new_hci(vlan);
  564. }
  565. fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
  566. return 0;
  567. }
  568. static int bt_hci_parse(const char *str)
  569. {
  570. struct HCIInfo *hci;
  571. bdaddr_t bdaddr;
  572. if (nb_hcis >= MAX_NICS) {
  573. fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
  574. return -1;
  575. }
  576. hci = hci_init(str);
  577. if (!hci)
  578. return -1;
  579. bdaddr.b[0] = 0x52;
  580. bdaddr.b[1] = 0x54;
  581. bdaddr.b[2] = 0x00;
  582. bdaddr.b[3] = 0x12;
  583. bdaddr.b[4] = 0x34;
  584. bdaddr.b[5] = 0x56 + nb_hcis;
  585. hci->bdaddr_set(hci, bdaddr.b);
  586. hci_table[nb_hcis++] = hci;
  587. return 0;
  588. }
  589. static void bt_vhci_add(int vlan_id)
  590. {
  591. struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
  592. if (!vlan->slave)
  593. fprintf(stderr, "qemu: warning: adding a VHCI to "
  594. "an empty scatternet %i\n", vlan_id);
  595. bt_vhci_init(bt_new_hci(vlan));
  596. }
  597. static struct bt_device_s *bt_device_add(const char *opt)
  598. {
  599. struct bt_scatternet_s *vlan;
  600. int vlan_id = 0;
  601. char *endp = strstr(opt, ",vlan=");
  602. int len = (endp ? endp - opt : strlen(opt)) + 1;
  603. char devname[10];
  604. pstrcpy(devname, MIN(sizeof(devname), len), opt);
  605. if (endp) {
  606. vlan_id = strtol(endp + 6, &endp, 0);
  607. if (*endp) {
  608. fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
  609. return 0;
  610. }
  611. }
  612. vlan = qemu_find_bt_vlan(vlan_id);
  613. if (!vlan->slave)
  614. fprintf(stderr, "qemu: warning: adding a slave device to "
  615. "an empty scatternet %i\n", vlan_id);
  616. if (!strcmp(devname, "keyboard"))
  617. return bt_keyboard_init(vlan);
  618. fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
  619. return 0;
  620. }
  621. static int bt_parse(const char *opt)
  622. {
  623. const char *endp, *p;
  624. int vlan;
  625. if (strstart(opt, "hci", &endp)) {
  626. if (!*endp || *endp == ',') {
  627. if (*endp)
  628. if (!strstart(endp, ",vlan=", 0))
  629. opt = endp + 1;
  630. return bt_hci_parse(opt);
  631. }
  632. } else if (strstart(opt, "vhci", &endp)) {
  633. if (!*endp || *endp == ',') {
  634. if (*endp) {
  635. if (strstart(endp, ",vlan=", &p)) {
  636. vlan = strtol(p, (char **) &endp, 0);
  637. if (*endp) {
  638. fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
  639. return 1;
  640. }
  641. } else {
  642. fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
  643. return 1;
  644. }
  645. } else
  646. vlan = 0;
  647. bt_vhci_add(vlan);
  648. return 0;
  649. }
  650. } else if (strstart(opt, "device:", &endp))
  651. return !bt_device_add(endp);
  652. fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
  653. return 1;
  654. }
  655. /***********************************************************/
  656. /* QEMU Block devices */
  657. #define HD_OPTS "media=disk"
  658. #define CDROM_OPTS "media=cdrom"
  659. #define FD_OPTS ""
  660. #define PFLASH_OPTS ""
  661. #define MTD_OPTS ""
  662. #define SD_OPTS ""
  663. static int drive_init_func(QemuOpts *opts, void *opaque)
  664. {
  665. int *use_scsi = opaque;
  666. return drive_init(opts, *use_scsi) == NULL;
  667. }
  668. static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
  669. {
  670. if (NULL == qemu_opt_get(opts, "snapshot")) {
  671. qemu_opt_set(opts, "snapshot", "on");
  672. }
  673. return 0;
  674. }
  675. static void default_drive(int enable, int snapshot, int use_scsi,
  676. BlockInterfaceType type, int index,
  677. const char *optstr)
  678. {
  679. QemuOpts *opts;
  680. if (type == IF_DEFAULT) {
  681. type = use_scsi ? IF_SCSI : IF_IDE;
  682. }
  683. if (!enable || drive_get_by_index(type, index)) {
  684. return;
  685. }
  686. opts = drive_add(type, index, NULL, optstr);
  687. if (snapshot) {
  688. drive_enable_snapshot(opts, NULL);
  689. }
  690. if (!drive_init(opts, use_scsi)) {
  691. exit(1);
  692. }
  693. }
  694. void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
  695. {
  696. boot_set_handler = func;
  697. boot_set_opaque = opaque;
  698. }
  699. int qemu_boot_set(const char *boot_devices)
  700. {
  701. if (!boot_set_handler) {
  702. return -EINVAL;
  703. }
  704. return boot_set_handler(boot_set_opaque, boot_devices);
  705. }
  706. static void validate_bootdevices(char *devices)
  707. {
  708. /* We just do some generic consistency checks */
  709. const char *p;
  710. int bitmap = 0;
  711. for (p = devices; *p != '\0'; p++) {
  712. /* Allowed boot devices are:
  713. * a-b: floppy disk drives
  714. * c-f: IDE disk drives
  715. * g-m: machine implementation dependent drives
  716. * n-p: network devices
  717. * It's up to each machine implementation to check if the given boot
  718. * devices match the actual hardware implementation and firmware
  719. * features.
  720. */
  721. if (*p < 'a' || *p > 'p') {
  722. fprintf(stderr, "Invalid boot device '%c'\n", *p);
  723. exit(1);
  724. }
  725. if (bitmap & (1 << (*p - 'a'))) {
  726. fprintf(stderr, "Boot device '%c' was given twice\n", *p);
  727. exit(1);
  728. }
  729. bitmap |= 1 << (*p - 'a');
  730. }
  731. }
  732. static void restore_boot_devices(void *opaque)
  733. {
  734. char *standard_boot_devices = opaque;
  735. static int first = 1;
  736. /* Restore boot order and remove ourselves after the first boot */
  737. if (first) {
  738. first = 0;
  739. return;
  740. }
  741. qemu_boot_set(standard_boot_devices);
  742. qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
  743. g_free(standard_boot_devices);
  744. }
  745. void add_boot_device_path(int32_t bootindex, DeviceState *dev,
  746. const char *suffix)
  747. {
  748. FWBootEntry *node, *i;
  749. if (bootindex < 0) {
  750. return;
  751. }
  752. assert(dev != NULL || suffix != NULL);
  753. node = g_malloc0(sizeof(FWBootEntry));
  754. node->bootindex = bootindex;
  755. node->suffix = suffix ? g_strdup(suffix) : NULL;
  756. node->dev = dev;
  757. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  758. if (i->bootindex == bootindex) {
  759. fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
  760. exit(1);
  761. } else if (i->bootindex < bootindex) {
  762. continue;
  763. }
  764. QTAILQ_INSERT_BEFORE(i, node, link);
  765. return;
  766. }
  767. QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
  768. }
  769. /*
  770. * This function returns null terminated string that consist of new line
  771. * separated device paths.
  772. *
  773. * memory pointed by "size" is assigned total length of the array in bytes
  774. *
  775. */
  776. char *get_boot_devices_list(uint32_t *size)
  777. {
  778. FWBootEntry *i;
  779. uint32_t total = 0;
  780. char *list = NULL;
  781. QTAILQ_FOREACH(i, &fw_boot_order, link) {
  782. char *devpath = NULL, *bootpath;
  783. int len;
  784. if (i->dev) {
  785. devpath = qdev_get_fw_dev_path(i->dev);
  786. assert(devpath);
  787. }
  788. if (i->suffix && devpath) {
  789. size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
  790. bootpath = g_malloc(bootpathlen);
  791. snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
  792. g_free(devpath);
  793. } else if (devpath) {
  794. bootpath = devpath;
  795. } else {
  796. assert(i->suffix);
  797. bootpath = g_strdup(i->suffix);
  798. }
  799. if (total) {
  800. list[total-1] = '\n';
  801. }
  802. len = strlen(bootpath) + 1;
  803. list = g_realloc(list, total + len);
  804. memcpy(&list[total], bootpath, len);
  805. total += len;
  806. g_free(bootpath);
  807. }
  808. *size = total;
  809. return list;
  810. }
  811. static void numa_add(const char *optarg)
  812. {
  813. char option[128];
  814. char *endptr;
  815. unsigned long long value, endvalue;
  816. int nodenr;
  817. optarg = get_opt_name(option, 128, optarg, ',') + 1;
  818. if (!strcmp(option, "node")) {
  819. if (get_param_value(option, 128, "nodeid", optarg) == 0) {
  820. nodenr = nb_numa_nodes;
  821. } else {
  822. nodenr = strtoull(option, NULL, 10);
  823. }
  824. if (get_param_value(option, 128, "mem", optarg) == 0) {
  825. node_mem[nodenr] = 0;
  826. } else {
  827. int64_t sval;
  828. sval = strtosz(option, &endptr);
  829. if (sval < 0 || *endptr) {
  830. fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
  831. exit(1);
  832. }
  833. node_mem[nodenr] = sval;
  834. }
  835. if (get_param_value(option, 128, "cpus", optarg) == 0) {
  836. node_cpumask[nodenr] = 0;
  837. } else {
  838. value = strtoull(option, &endptr, 10);
  839. if (value >= 64) {
  840. value = 63;
  841. fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
  842. } else {
  843. if (*endptr == '-') {
  844. endvalue = strtoull(endptr+1, &endptr, 10);
  845. if (endvalue >= 63) {
  846. endvalue = 62;
  847. fprintf(stderr,
  848. "only 63 CPUs in NUMA mode supported.\n");
  849. }
  850. value = (2ULL << endvalue) - (1ULL << value);
  851. } else {
  852. value = 1ULL << value;
  853. }
  854. }
  855. node_cpumask[nodenr] = value;
  856. }
  857. nb_numa_nodes++;
  858. }
  859. return;
  860. }
  861. static void smp_parse(const char *optarg)
  862. {
  863. int smp, sockets = 0, threads = 0, cores = 0;
  864. char *endptr;
  865. char option[128];
  866. smp = strtoul(optarg, &endptr, 10);
  867. if (endptr != optarg) {
  868. if (*endptr == ',') {
  869. endptr++;
  870. }
  871. }
  872. if (get_param_value(option, 128, "sockets", endptr) != 0)
  873. sockets = strtoull(option, NULL, 10);
  874. if (get_param_value(option, 128, "cores", endptr) != 0)
  875. cores = strtoull(option, NULL, 10);
  876. if (get_param_value(option, 128, "threads", endptr) != 0)
  877. threads = strtoull(option, NULL, 10);
  878. if (get_param_value(option, 128, "maxcpus", endptr) != 0)
  879. max_cpus = strtoull(option, NULL, 10);
  880. /* compute missing values, prefer sockets over cores over threads */
  881. if (smp == 0 || sockets == 0) {
  882. sockets = sockets > 0 ? sockets : 1;
  883. cores = cores > 0 ? cores : 1;
  884. threads = threads > 0 ? threads : 1;
  885. if (smp == 0) {
  886. smp = cores * threads * sockets;
  887. }
  888. } else {
  889. if (cores == 0) {
  890. threads = threads > 0 ? threads : 1;
  891. cores = smp / (sockets * threads);
  892. } else {
  893. threads = smp / (cores * sockets);
  894. }
  895. }
  896. smp_cpus = smp;
  897. smp_cores = cores > 0 ? cores : 1;
  898. smp_threads = threads > 0 ? threads : 1;
  899. if (max_cpus == 0)
  900. max_cpus = smp_cpus;
  901. }
  902. /***********************************************************/
  903. /* USB devices */
  904. static int usb_device_add(const char *devname)
  905. {
  906. const char *p;
  907. USBDevice *dev = NULL;
  908. if (!usb_enabled)
  909. return -1;
  910. /* drivers with .usbdevice_name entry in USBDeviceInfo */
  911. dev = usbdevice_create(devname);
  912. if (dev)
  913. goto done;
  914. /* the other ones */
  915. #ifndef CONFIG_LINUX
  916. /* only the linux version is qdev-ified, usb-bsd still needs this */
  917. if (strstart(devname, "host:", &p)) {
  918. dev = usb_host_device_open(usb_bus_find(-1), p);
  919. } else
  920. #endif
  921. if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
  922. dev = usb_bt_init(usb_bus_find(-1),
  923. devname[2] ? hci_init(p)
  924. : bt_new_hci(qemu_find_bt_vlan(0)));
  925. } else {
  926. return -1;
  927. }
  928. if (!dev)
  929. return -1;
  930. done:
  931. return 0;
  932. }
  933. static int usb_device_del(const char *devname)
  934. {
  935. int bus_num, addr;
  936. const char *p;
  937. if (strstart(devname, "host:", &p))
  938. return usb_host_device_close(p);
  939. if (!usb_enabled)
  940. return -1;
  941. p = strchr(devname, '.');
  942. if (!p)
  943. return -1;
  944. bus_num = strtoul(devname, NULL, 0);
  945. addr = strtoul(p + 1, NULL, 0);
  946. return usb_device_delete_addr(bus_num, addr);
  947. }
  948. static int usb_parse(const char *cmdline)
  949. {
  950. int r;
  951. r = usb_device_add(cmdline);
  952. if (r < 0) {
  953. fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
  954. }
  955. return r;
  956. }
  957. void do_usb_add(Monitor *mon, const QDict *qdict)
  958. {
  959. const char *devname = qdict_get_str(qdict, "devname");
  960. if (usb_device_add(devname) < 0) {
  961. error_report("could not add USB device '%s'", devname);
  962. }
  963. }
  964. void do_usb_del(Monitor *mon, const QDict *qdict)
  965. {
  966. const char *devname = qdict_get_str(qdict, "devname");
  967. if (usb_device_del(devname) < 0) {
  968. error_report("could not delete USB device '%s'", devname);
  969. }
  970. }
  971. /***********************************************************/
  972. /* PCMCIA/Cardbus */
  973. static struct pcmcia_socket_entry_s {
  974. PCMCIASocket *socket;
  975. struct pcmcia_socket_entry_s *next;
  976. } *pcmcia_sockets = 0;
  977. void pcmcia_socket_register(PCMCIASocket *socket)
  978. {
  979. struct pcmcia_socket_entry_s *entry;
  980. entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
  981. entry->socket = socket;
  982. entry->next = pcmcia_sockets;
  983. pcmcia_sockets = entry;
  984. }
  985. void pcmcia_socket_unregister(PCMCIASocket *socket)
  986. {
  987. struct pcmcia_socket_entry_s *entry, **ptr;
  988. ptr = &pcmcia_sockets;
  989. for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
  990. if (entry->socket == socket) {
  991. *ptr = entry->next;
  992. g_free(entry);
  993. }
  994. }
  995. void pcmcia_info(Monitor *mon)
  996. {
  997. struct pcmcia_socket_entry_s *iter;
  998. if (!pcmcia_sockets)
  999. monitor_printf(mon, "No PCMCIA sockets\n");
  1000. for (iter = pcmcia_sockets; iter; iter = iter->next)
  1001. monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
  1002. iter->socket->attached ? iter->socket->card_string :
  1003. "Empty");
  1004. }
  1005. /***********************************************************/
  1006. /* machine registration */
  1007. static QEMUMachine *first_machine = NULL;
  1008. QEMUMachine *current_machine = NULL;
  1009. int qemu_register_machine(QEMUMachine *m)
  1010. {
  1011. QEMUMachine **pm;
  1012. pm = &first_machine;
  1013. while (*pm != NULL)
  1014. pm = &(*pm)->next;
  1015. m->next = NULL;
  1016. *pm = m;
  1017. return 0;
  1018. }
  1019. static QEMUMachine *find_machine(const char *name)
  1020. {
  1021. QEMUMachine *m;
  1022. for(m = first_machine; m != NULL; m = m->next) {
  1023. if (!strcmp(m->name, name))
  1024. return m;
  1025. if (m->alias && !strcmp(m->alias, name))
  1026. return m;
  1027. }
  1028. return NULL;
  1029. }
  1030. QEMUMachine *find_default_machine(void)
  1031. {
  1032. QEMUMachine *m;
  1033. for(m = first_machine; m != NULL; m = m->next) {
  1034. if (m->is_default) {
  1035. return m;
  1036. }
  1037. }
  1038. return NULL;
  1039. }
  1040. /***********************************************************/
  1041. /* main execution loop */
  1042. static void gui_update(void *opaque)
  1043. {
  1044. uint64_t interval = GUI_REFRESH_INTERVAL;
  1045. DisplayState *ds = opaque;
  1046. DisplayChangeListener *dcl = ds->listeners;
  1047. dpy_refresh(ds);
  1048. while (dcl != NULL) {
  1049. if (dcl->gui_timer_interval &&
  1050. dcl->gui_timer_interval < interval)
  1051. interval = dcl->gui_timer_interval;
  1052. dcl = dcl->next;
  1053. }
  1054. qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
  1055. }
  1056. struct vm_change_state_entry {
  1057. VMChangeStateHandler *cb;
  1058. void *opaque;
  1059. QLIST_ENTRY (vm_change_state_entry) entries;
  1060. };
  1061. static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
  1062. VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
  1063. void *opaque)
  1064. {
  1065. VMChangeStateEntry *e;
  1066. e = g_malloc0(sizeof (*e));
  1067. e->cb = cb;
  1068. e->opaque = opaque;
  1069. QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
  1070. return e;
  1071. }
  1072. void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
  1073. {
  1074. QLIST_REMOVE (e, entries);
  1075. g_free (e);
  1076. }
  1077. void vm_state_notify(int running, RunState state)
  1078. {
  1079. VMChangeStateEntry *e;
  1080. trace_vm_state_notify(running, state);
  1081. for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
  1082. e->cb(e->opaque, running, state);
  1083. }
  1084. }
  1085. void vm_start(void)
  1086. {
  1087. if (!runstate_is_running()) {
  1088. cpu_enable_ticks();
  1089. runstate_set(RUN_STATE_RUNNING);
  1090. vm_state_notify(1, RUN_STATE_RUNNING);
  1091. resume_all_vcpus();
  1092. monitor_protocol_event(QEVENT_RESUME, NULL);
  1093. }
  1094. }
  1095. /* reset/shutdown handler */
  1096. typedef struct QEMUResetEntry {
  1097. QTAILQ_ENTRY(QEMUResetEntry) entry;
  1098. QEMUResetHandler *func;
  1099. void *opaque;
  1100. } QEMUResetEntry;
  1101. static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
  1102. QTAILQ_HEAD_INITIALIZER(reset_handlers);
  1103. static int reset_requested;
  1104. static int shutdown_requested, shutdown_signal = -1;
  1105. static pid_t shutdown_pid;
  1106. static int powerdown_requested;
  1107. static int debug_requested;
  1108. static int suspend_requested;
  1109. static NotifierList suspend_notifiers =
  1110. NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
  1111. static NotifierList wakeup_notifiers =
  1112. NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
  1113. static uint32_t wakeup_reason_mask = ~0;
  1114. static RunState vmstop_requested = RUN_STATE_MAX;
  1115. int qemu_shutdown_requested_get(void)
  1116. {
  1117. return shutdown_requested;
  1118. }
  1119. int qemu_reset_requested_get(void)
  1120. {
  1121. return reset_requested;
  1122. }
  1123. int qemu_shutdown_requested(void)
  1124. {
  1125. int r = shutdown_requested;
  1126. shutdown_requested = 0;
  1127. return r;
  1128. }
  1129. void qemu_kill_report(void)
  1130. {
  1131. if (!qtest_enabled() && shutdown_signal != -1) {
  1132. fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
  1133. if (shutdown_pid == 0) {
  1134. /* This happens for eg ^C at the terminal, so it's worth
  1135. * avoiding printing an odd message in that case.
  1136. */
  1137. fputc('\n', stderr);
  1138. } else {
  1139. fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
  1140. }
  1141. shutdown_signal = -1;
  1142. }
  1143. }
  1144. int qemu_reset_requested(void)
  1145. {
  1146. int r = reset_requested;
  1147. reset_requested = 0;
  1148. return r;
  1149. }
  1150. static int qemu_suspend_requested(void)
  1151. {
  1152. int r = suspend_requested;
  1153. suspend_requested = 0;
  1154. return r;
  1155. }
  1156. int qemu_powerdown_requested(void)
  1157. {
  1158. int r = powerdown_requested;
  1159. powerdown_requested = 0;
  1160. return r;
  1161. }
  1162. static int qemu_debug_requested(void)
  1163. {
  1164. int r = debug_requested;
  1165. debug_requested = 0;
  1166. return r;
  1167. }
  1168. /* We use RUN_STATE_MAX but any invalid value will do */
  1169. static bool qemu_vmstop_requested(RunState *r)
  1170. {
  1171. if (vmstop_requested < RUN_STATE_MAX) {
  1172. *r = vmstop_requested;
  1173. vmstop_requested = RUN_STATE_MAX;
  1174. return true;
  1175. }
  1176. return false;
  1177. }
  1178. void qemu_register_reset(QEMUResetHandler *func, void *opaque)
  1179. {
  1180. QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
  1181. re->func = func;
  1182. re->opaque = opaque;
  1183. QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
  1184. }
  1185. void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
  1186. {
  1187. QEMUResetEntry *re;
  1188. QTAILQ_FOREACH(re, &reset_handlers, entry) {
  1189. if (re->func == func && re->opaque == opaque) {
  1190. QTAILQ_REMOVE(&reset_handlers, re, entry);
  1191. g_free(re);
  1192. return;
  1193. }
  1194. }
  1195. }
  1196. void qemu_system_reset(bool report)
  1197. {
  1198. QEMUResetEntry *re, *nre;
  1199. /* reset all devices */
  1200. QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
  1201. re->func(re->opaque);
  1202. }
  1203. if (report) {
  1204. monitor_protocol_event(QEVENT_RESET, NULL);
  1205. }
  1206. cpu_synchronize_all_post_reset();
  1207. }
  1208. void qemu_system_reset_request(void)
  1209. {
  1210. if (no_reboot) {
  1211. shutdown_requested = 1;
  1212. } else {
  1213. reset_requested = 1;
  1214. }
  1215. cpu_stop_current();
  1216. qemu_notify_event();
  1217. }
  1218. static void qemu_system_suspend(void)
  1219. {
  1220. pause_all_vcpus();
  1221. notifier_list_notify(&suspend_notifiers, NULL);
  1222. runstate_set(RUN_STATE_SUSPENDED);
  1223. monitor_protocol_event(QEVENT_SUSPEND, NULL);
  1224. }
  1225. void qemu_system_suspend_request(void)
  1226. {
  1227. if (runstate_check(RUN_STATE_SUSPENDED)) {
  1228. return;
  1229. }
  1230. suspend_requested = 1;
  1231. cpu_stop_current();
  1232. qemu_notify_event();
  1233. }
  1234. void qemu_register_suspend_notifier(Notifier *notifier)
  1235. {
  1236. notifier_list_add(&suspend_notifiers, notifier);
  1237. }
  1238. void qemu_system_wakeup_request(WakeupReason reason)
  1239. {
  1240. if (!runstate_check(RUN_STATE_SUSPENDED)) {
  1241. return;
  1242. }
  1243. if (!(wakeup_reason_mask & (1 << reason))) {
  1244. return;
  1245. }
  1246. runstate_set(RUN_STATE_RUNNING);
  1247. monitor_protocol_event(QEVENT_WAKEUP, NULL);
  1248. notifier_list_notify(&wakeup_notifiers, &reason);
  1249. reset_requested = 1;
  1250. qemu_notify_event();
  1251. }
  1252. void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
  1253. {
  1254. if (enabled) {
  1255. wakeup_reason_mask |= (1 << reason);
  1256. } else {
  1257. wakeup_reason_mask &= ~(1 << reason);
  1258. }
  1259. }
  1260. void qemu_register_wakeup_notifier(Notifier *notifier)
  1261. {
  1262. notifier_list_add(&wakeup_notifiers, notifier);
  1263. }
  1264. void qemu_system_killed(int signal, pid_t pid)
  1265. {
  1266. shutdown_signal = signal;
  1267. shutdown_pid = pid;
  1268. no_shutdown = 0;
  1269. qemu_system_shutdown_request();
  1270. }
  1271. void qemu_system_shutdown_request(void)
  1272. {
  1273. shutdown_requested = 1;
  1274. qemu_notify_event();
  1275. }
  1276. void qemu_system_powerdown_request(void)
  1277. {
  1278. powerdown_requested = 1;
  1279. qemu_notify_event();
  1280. }
  1281. void qemu_system_debug_request(void)
  1282. {
  1283. debug_requested = 1;
  1284. qemu_notify_event();
  1285. }
  1286. void qemu_system_vmstop_request(RunState state)
  1287. {
  1288. vmstop_requested = state;
  1289. qemu_notify_event();
  1290. }
  1291. qemu_irq qemu_system_powerdown;
  1292. static bool main_loop_should_exit(void)
  1293. {
  1294. RunState r;
  1295. if (qemu_debug_requested()) {
  1296. vm_stop(RUN_STATE_DEBUG);
  1297. }
  1298. if (qemu_suspend_requested()) {
  1299. qemu_system_suspend();
  1300. }
  1301. if (qemu_shutdown_requested()) {
  1302. qemu_kill_report();
  1303. monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
  1304. if (no_shutdown) {
  1305. vm_stop(RUN_STATE_SHUTDOWN);
  1306. } else {
  1307. return true;
  1308. }
  1309. }
  1310. if (qemu_reset_requested()) {
  1311. pause_all_vcpus();
  1312. cpu_synchronize_all_states();
  1313. qemu_system_reset(VMRESET_REPORT);
  1314. resume_all_vcpus();
  1315. if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
  1316. runstate_check(RUN_STATE_SHUTDOWN)) {
  1317. runstate_set(RUN_STATE_PAUSED);
  1318. }
  1319. }
  1320. if (qemu_powerdown_requested()) {
  1321. monitor_protocol_event(QEVENT_POWERDOWN, NULL);
  1322. qemu_irq_raise(qemu_system_powerdown);
  1323. }
  1324. if (qemu_vmstop_requested(&r)) {
  1325. vm_stop(r);
  1326. }
  1327. return false;
  1328. }
  1329. static void main_loop(void)
  1330. {
  1331. bool nonblocking;
  1332. int last_io = 0;
  1333. #ifdef CONFIG_PROFILER
  1334. int64_t ti;
  1335. #endif
  1336. do {
  1337. nonblocking = !kvm_enabled() && last_io > 0;
  1338. #ifdef CONFIG_PROFILER
  1339. ti = profile_getclock();
  1340. #endif
  1341. last_io = main_loop_wait(nonblocking);
  1342. #ifdef CONFIG_PROFILER
  1343. dev_time += profile_getclock() - ti;
  1344. #endif
  1345. } while (!main_loop_should_exit());
  1346. }
  1347. static void version(void)
  1348. {
  1349. printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
  1350. }
  1351. static void help(int exitcode)
  1352. {
  1353. version();
  1354. printf("usage: %s [options] [disk_image]\n\n"
  1355. "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
  1356. error_get_progname());
  1357. #define QEMU_OPTIONS_GENERATE_HELP
  1358. #include "qemu-options-wrapper.h"
  1359. printf("\nDuring emulation, the following keys are useful:\n"
  1360. "ctrl-alt-f toggle full screen\n"
  1361. "ctrl-alt-n switch to virtual console 'n'\n"
  1362. "ctrl-alt toggle mouse and keyboard grab\n"
  1363. "\n"
  1364. "When using -nographic, press 'ctrl-a h' to get some help.\n");
  1365. exit(exitcode);
  1366. }
  1367. #define HAS_ARG 0x0001
  1368. typedef struct QEMUOption {
  1369. const char *name;
  1370. int flags;
  1371. int index;
  1372. uint32_t arch_mask;
  1373. } QEMUOption;
  1374. static const QEMUOption qemu_options[] = {
  1375. { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
  1376. #define QEMU_OPTIONS_GENERATE_OPTIONS
  1377. #include "qemu-options-wrapper.h"
  1378. { NULL },
  1379. };
  1380. static bool vga_available(void)
  1381. {
  1382. return qdev_exists("VGA") || qdev_exists("isa-vga");
  1383. }
  1384. static bool cirrus_vga_available(void)
  1385. {
  1386. return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
  1387. }
  1388. static bool vmware_vga_available(void)
  1389. {
  1390. return qdev_exists("vmware-svga");
  1391. }
  1392. static void select_vgahw (const char *p)
  1393. {
  1394. const char *opts;
  1395. vga_interface_type = VGA_NONE;
  1396. if (strstart(p, "std", &opts)) {
  1397. if (vga_available()) {
  1398. vga_interface_type = VGA_STD;
  1399. } else {
  1400. fprintf(stderr, "Error: standard VGA not available\n");
  1401. exit(0);
  1402. }
  1403. } else if (strstart(p, "cirrus", &opts)) {
  1404. if (cirrus_vga_available()) {
  1405. vga_interface_type = VGA_CIRRUS;
  1406. } else {
  1407. fprintf(stderr, "Error: Cirrus VGA not available\n");
  1408. exit(0);
  1409. }
  1410. } else if (strstart(p, "vmware", &opts)) {
  1411. if (vmware_vga_available()) {
  1412. vga_interface_type = VGA_VMWARE;
  1413. } else {
  1414. fprintf(stderr, "Error: VMWare SVGA not available\n");
  1415. exit(0);
  1416. }
  1417. } else if (strstart(p, "xenfb", &opts)) {
  1418. vga_interface_type = VGA_XENFB;
  1419. } else if (strstart(p, "qxl", &opts)) {
  1420. vga_interface_type = VGA_QXL;
  1421. } else if (!strstart(p, "none", &opts)) {
  1422. invalid_vga:
  1423. fprintf(stderr, "Unknown vga type: %s\n", p);
  1424. exit(1);
  1425. }
  1426. while (*opts) {
  1427. const char *nextopt;
  1428. if (strstart(opts, ",retrace=", &nextopt)) {
  1429. opts = nextopt;
  1430. if (strstart(opts, "dumb", &nextopt))
  1431. vga_retrace_method = VGA_RETRACE_DUMB;
  1432. else if (strstart(opts, "precise", &nextopt))
  1433. vga_retrace_method = VGA_RETRACE_PRECISE;
  1434. else goto invalid_vga;
  1435. } else goto invalid_vga;
  1436. opts = nextopt;
  1437. }
  1438. }
  1439. static DisplayType select_display(const char *p)
  1440. {
  1441. const char *opts;
  1442. DisplayType display = DT_DEFAULT;
  1443. if (strstart(p, "sdl", &opts)) {
  1444. #ifdef CONFIG_SDL
  1445. display = DT_SDL;
  1446. while (*opts) {
  1447. const char *nextopt;
  1448. if (strstart(opts, ",frame=", &nextopt)) {
  1449. opts = nextopt;
  1450. if (strstart(opts, "on", &nextopt)) {
  1451. no_frame = 0;
  1452. } else if (strstart(opts, "off", &nextopt)) {
  1453. no_frame = 1;
  1454. } else {
  1455. goto invalid_sdl_args;
  1456. }
  1457. } else if (strstart(opts, ",alt_grab=", &nextopt)) {
  1458. opts = nextopt;
  1459. if (strstart(opts, "on", &nextopt)) {
  1460. alt_grab = 1;
  1461. } else if (strstart(opts, "off", &nextopt)) {
  1462. alt_grab = 0;
  1463. } else {
  1464. goto invalid_sdl_args;
  1465. }
  1466. } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
  1467. opts = nextopt;
  1468. if (strstart(opts, "on", &nextopt)) {
  1469. ctrl_grab = 1;
  1470. } else if (strstart(opts, "off", &nextopt)) {
  1471. ctrl_grab = 0;
  1472. } else {
  1473. goto invalid_sdl_args;
  1474. }
  1475. } else if (strstart(opts, ",window_close=", &nextopt)) {
  1476. opts = nextopt;
  1477. if (strstart(opts, "on", &nextopt)) {
  1478. no_quit = 0;
  1479. } else if (strstart(opts, "off", &nextopt)) {
  1480. no_quit = 1;
  1481. } else {
  1482. goto invalid_sdl_args;
  1483. }
  1484. } else {
  1485. invalid_sdl_args:
  1486. fprintf(stderr, "Invalid SDL option string: %s\n", p);
  1487. exit(1);
  1488. }
  1489. opts = nextopt;
  1490. }
  1491. #else
  1492. fprintf(stderr, "SDL support is disabled\n");
  1493. exit(1);
  1494. #endif
  1495. } else if (strstart(p, "vnc", &opts)) {
  1496. #ifdef CONFIG_VNC
  1497. display_remote++;
  1498. if (*opts) {
  1499. const char *nextopt;
  1500. if (strstart(opts, "=", &nextopt)) {
  1501. vnc_display = nextopt;
  1502. }
  1503. }
  1504. if (!vnc_display) {
  1505. fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
  1506. exit(1);
  1507. }
  1508. #else
  1509. fprintf(stderr, "VNC support is disabled\n");
  1510. exit(1);
  1511. #endif
  1512. } else if (strstart(p, "curses", &opts)) {
  1513. #ifdef CONFIG_CURSES
  1514. display = DT_CURSES;
  1515. #else
  1516. fprintf(stderr, "Curses support is disabled\n");
  1517. exit(1);
  1518. #endif
  1519. } else if (strstart(p, "none", &opts)) {
  1520. display = DT_NONE;
  1521. } else {
  1522. fprintf(stderr, "Unknown display type: %s\n", p);
  1523. exit(1);
  1524. }
  1525. return display;
  1526. }
  1527. static int balloon_parse(const char *arg)
  1528. {
  1529. QemuOpts *opts;
  1530. if (strcmp(arg, "none") == 0) {
  1531. return 0;
  1532. }
  1533. if (!strncmp(arg, "virtio", 6)) {
  1534. if (arg[6] == ',') {
  1535. /* have params -> parse them */
  1536. opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
  1537. if (!opts)
  1538. return -1;
  1539. } else {
  1540. /* create empty opts */
  1541. opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
  1542. }
  1543. qemu_opt_set(opts, "driver", "virtio-balloon");
  1544. return 0;
  1545. }
  1546. return -1;
  1547. }
  1548. char *qemu_find_file(int type, const char *name)
  1549. {
  1550. int len;
  1551. const char *subdir;
  1552. char *buf;
  1553. /* If name contains path separators then try it as a straight path. */
  1554. if ((strchr(name, '/') || strchr(name, '\\'))
  1555. && access(name, R_OK) == 0) {
  1556. return g_strdup(name);
  1557. }
  1558. switch (type) {
  1559. case QEMU_FILE_TYPE_BIOS:
  1560. subdir = "";
  1561. break;
  1562. case QEMU_FILE_TYPE_KEYMAP:
  1563. subdir = "keymaps/";
  1564. break;
  1565. default:
  1566. abort();
  1567. }
  1568. len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
  1569. buf = g_malloc0(len);
  1570. snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
  1571. if (access(buf, R_OK)) {
  1572. g_free(buf);
  1573. return NULL;
  1574. }
  1575. return buf;
  1576. }
  1577. static int device_help_func(QemuOpts *opts, void *opaque)
  1578. {
  1579. return qdev_device_help(opts);
  1580. }
  1581. static int device_init_func(QemuOpts *opts, void *opaque)
  1582. {
  1583. DeviceState *dev;
  1584. dev = qdev_device_add(opts);
  1585. if (!dev)
  1586. return -1;
  1587. return 0;
  1588. }
  1589. static int chardev_init_func(QemuOpts *opts, void *opaque)
  1590. {
  1591. CharDriverState *chr;
  1592. chr = qemu_chr_new_from_opts(opts, NULL);
  1593. if (!chr)
  1594. return -1;
  1595. return 0;
  1596. }
  1597. #ifdef CONFIG_VIRTFS
  1598. static int fsdev_init_func(QemuOpts *opts, void *opaque)
  1599. {
  1600. int ret;
  1601. ret = qemu_fsdev_add(opts);
  1602. return ret;
  1603. }
  1604. #endif
  1605. static int mon_init_func(QemuOpts *opts, void *opaque)
  1606. {
  1607. CharDriverState *chr;
  1608. const char *chardev;
  1609. const char *mode;
  1610. int flags;
  1611. mode = qemu_opt_get(opts, "mode");
  1612. if (mode == NULL) {
  1613. mode = "readline";
  1614. }
  1615. if (strcmp(mode, "readline") == 0) {
  1616. flags = MONITOR_USE_READLINE;
  1617. } else if (strcmp(mode, "control") == 0) {
  1618. flags = MONITOR_USE_CONTROL;
  1619. } else {
  1620. fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
  1621. exit(1);
  1622. }
  1623. if (qemu_opt_get_bool(opts, "pretty", 0))
  1624. flags |= MONITOR_USE_PRETTY;
  1625. if (qemu_opt_get_bool(opts, "default", 0))
  1626. flags |= MONITOR_IS_DEFAULT;
  1627. chardev = qemu_opt_get(opts, "chardev");
  1628. chr = qemu_chr_find(chardev);
  1629. if (chr == NULL) {
  1630. fprintf(stderr, "chardev \"%s\" not found\n", chardev);
  1631. exit(1);
  1632. }
  1633. monitor_init(chr, flags);
  1634. return 0;
  1635. }
  1636. static void monitor_parse(const char *optarg, const char *mode)
  1637. {
  1638. static int monitor_device_index = 0;
  1639. QemuOpts *opts;
  1640. const char *p;
  1641. char label[32];
  1642. int def = 0;
  1643. if (strstart(optarg, "chardev:", &p)) {
  1644. snprintf(label, sizeof(label), "%s", p);
  1645. } else {
  1646. snprintf(label, sizeof(label), "compat_monitor%d",
  1647. monitor_device_index);
  1648. if (monitor_device_index == 0) {
  1649. def = 1;
  1650. }
  1651. opts = qemu_chr_parse_compat(label, optarg);
  1652. if (!opts) {
  1653. fprintf(stderr, "parse error: %s\n", optarg);
  1654. exit(1);
  1655. }
  1656. }
  1657. opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
  1658. if (!opts) {
  1659. fprintf(stderr, "duplicate chardev: %s\n", label);
  1660. exit(1);
  1661. }
  1662. qemu_opt_set(opts, "mode", mode);
  1663. qemu_opt_set(opts, "chardev", label);
  1664. if (def)
  1665. qemu_opt_set(opts, "default", "on");
  1666. monitor_device_index++;
  1667. }
  1668. struct device_config {
  1669. enum {
  1670. DEV_USB, /* -usbdevice */
  1671. DEV_BT, /* -bt */
  1672. DEV_SERIAL, /* -serial */
  1673. DEV_PARALLEL, /* -parallel */
  1674. DEV_VIRTCON, /* -virtioconsole */
  1675. DEV_DEBUGCON, /* -debugcon */
  1676. DEV_GDB, /* -gdb, -s */
  1677. } type;
  1678. const char *cmdline;
  1679. Location loc;
  1680. QTAILQ_ENTRY(device_config) next;
  1681. };
  1682. QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
  1683. static void add_device_config(int type, const char *cmdline)
  1684. {
  1685. struct device_config *conf;
  1686. conf = g_malloc0(sizeof(*conf));
  1687. conf->type = type;
  1688. conf->cmdline = cmdline;
  1689. loc_save(&conf->loc);
  1690. QTAILQ_INSERT_TAIL(&device_configs, conf, next);
  1691. }
  1692. static int foreach_device_config(int type, int (*func)(const char *cmdline))
  1693. {
  1694. struct device_config *conf;
  1695. int rc;
  1696. QTAILQ_FOREACH(conf, &device_configs, next) {
  1697. if (conf->type != type)
  1698. continue;
  1699. loc_push_restore(&conf->loc);
  1700. rc = func(conf->cmdline);
  1701. loc_pop(&conf->loc);
  1702. if (0 != rc)
  1703. return rc;
  1704. }
  1705. return 0;
  1706. }
  1707. static int serial_parse(const char *devname)
  1708. {
  1709. static int index = 0;
  1710. char label[32];
  1711. if (strcmp(devname, "none") == 0)
  1712. return 0;
  1713. if (index == MAX_SERIAL_PORTS) {
  1714. fprintf(stderr, "qemu: too many serial ports\n");
  1715. exit(1);
  1716. }
  1717. snprintf(label, sizeof(label), "serial%d", index);
  1718. serial_hds[index] = qemu_chr_new(label, devname, NULL);
  1719. if (!serial_hds[index]) {
  1720. fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
  1721. devname, strerror(errno));
  1722. return -1;
  1723. }
  1724. index++;
  1725. return 0;
  1726. }
  1727. static int parallel_parse(const char *devname)
  1728. {
  1729. static int index = 0;
  1730. char label[32];
  1731. if (strcmp(devname, "none") == 0)
  1732. return 0;
  1733. if (index == MAX_PARALLEL_PORTS) {
  1734. fprintf(stderr, "qemu: too many parallel ports\n");
  1735. exit(1);
  1736. }
  1737. snprintf(label, sizeof(label), "parallel%d", index);
  1738. parallel_hds[index] = qemu_chr_new(label, devname, NULL);
  1739. if (!parallel_hds[index]) {
  1740. fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
  1741. devname, strerror(errno));
  1742. return -1;
  1743. }
  1744. index++;
  1745. return 0;
  1746. }
  1747. static int virtcon_parse(const char *devname)
  1748. {
  1749. QemuOptsList *device = qemu_find_opts("device");
  1750. static int index = 0;
  1751. char label[32];
  1752. QemuOpts *bus_opts, *dev_opts;
  1753. if (strcmp(devname, "none") == 0)
  1754. return 0;
  1755. if (index == MAX_VIRTIO_CONSOLES) {
  1756. fprintf(stderr, "qemu: too many virtio consoles\n");
  1757. exit(1);
  1758. }
  1759. bus_opts = qemu_opts_create(device, NULL, 0);
  1760. if (arch_type == QEMU_ARCH_S390X) {
  1761. qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
  1762. } else {
  1763. qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
  1764. }
  1765. dev_opts = qemu_opts_create(device, NULL, 0);
  1766. qemu_opt_set(dev_opts, "driver", "virtconsole");
  1767. snprintf(label, sizeof(label), "virtcon%d", index);
  1768. virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
  1769. if (!virtcon_hds[index]) {
  1770. fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
  1771. devname, strerror(errno));
  1772. return -1;
  1773. }
  1774. qemu_opt_set(dev_opts, "chardev", label);
  1775. index++;
  1776. return 0;
  1777. }
  1778. static int debugcon_parse(const char *devname)
  1779. {
  1780. QemuOpts *opts;
  1781. if (!qemu_chr_new("debugcon", devname, NULL)) {
  1782. exit(1);
  1783. }
  1784. opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
  1785. if (!opts) {
  1786. fprintf(stderr, "qemu: already have a debugcon device\n");
  1787. exit(1);
  1788. }
  1789. qemu_opt_set(opts, "driver", "isa-debugcon");
  1790. qemu_opt_set(opts, "chardev", "debugcon");
  1791. return 0;
  1792. }
  1793. static QEMUMachine *machine_parse(const char *name)
  1794. {
  1795. QEMUMachine *m, *machine = NULL;
  1796. if (name) {
  1797. machine = find_machine(name);
  1798. }
  1799. if (machine) {
  1800. return machine;
  1801. }
  1802. printf("Supported machines are:\n");
  1803. for (m = first_machine; m != NULL; m = m->next) {
  1804. if (m->alias) {
  1805. printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
  1806. }
  1807. printf("%-20s %s%s\n", m->name, m->desc,
  1808. m->is_default ? " (default)" : "");
  1809. }
  1810. exit(!name || *name != '?');
  1811. }
  1812. static int tcg_init(void)
  1813. {
  1814. tcg_exec_init(tcg_tb_size * 1024 * 1024);
  1815. return 0;
  1816. }
  1817. static struct {
  1818. const char *opt_name;
  1819. const char *name;
  1820. int (*available)(void);
  1821. int (*init)(void);
  1822. int *allowed;
  1823. } accel_list[] = {
  1824. { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
  1825. { "xen", "Xen", xen_available, xen_init, &xen_allowed },
  1826. { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
  1827. { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
  1828. };
  1829. static int configure_accelerator(void)
  1830. {
  1831. const char *p = NULL;
  1832. char buf[10];
  1833. int i, ret;
  1834. bool accel_initialised = 0;
  1835. bool init_failed = 0;
  1836. QemuOptsList *list = qemu_find_opts("machine");
  1837. if (!QTAILQ_EMPTY(&list->head)) {
  1838. p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
  1839. }
  1840. if (p == NULL) {
  1841. /* Use the default "accelerator", tcg */
  1842. p = "tcg";
  1843. }
  1844. while (!accel_initialised && *p != '\0') {
  1845. if (*p == ':') {
  1846. p++;
  1847. }
  1848. p = get_opt_name(buf, sizeof (buf), p, ':');
  1849. for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
  1850. if (strcmp(accel_list[i].opt_name, buf) == 0) {
  1851. *(accel_list[i].allowed) = 1;
  1852. ret = accel_list[i].init();
  1853. if (ret < 0) {
  1854. init_failed = 1;
  1855. if (!accel_list[i].available()) {
  1856. printf("%s not supported for this target\n",
  1857. accel_list[i].name);
  1858. } else {
  1859. fprintf(stderr, "failed to initialize %s: %s\n",
  1860. accel_list[i].name,
  1861. strerror(-ret));
  1862. }
  1863. *(accel_list[i].allowed) = 0;
  1864. } else {
  1865. accel_initialised = 1;
  1866. }
  1867. break;
  1868. }
  1869. }
  1870. if (i == ARRAY_SIZE(accel_list)) {
  1871. fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
  1872. }
  1873. }
  1874. if (!accel_initialised) {
  1875. fprintf(stderr, "No accelerator found!\n");
  1876. exit(1);
  1877. }
  1878. if (init_failed) {
  1879. fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
  1880. }
  1881. return !accel_initialised;
  1882. }
  1883. void qemu_add_exit_notifier(Notifier *notify)
  1884. {
  1885. notifier_list_add(&exit_notifiers, notify);
  1886. }
  1887. void qemu_remove_exit_notifier(Notifier *notify)
  1888. {
  1889. notifier_remove(notify);
  1890. }
  1891. static void qemu_run_exit_notifiers(void)
  1892. {
  1893. notifier_list_notify(&exit_notifiers, NULL);
  1894. }
  1895. void qemu_add_machine_init_done_notifier(Notifier *notify)
  1896. {
  1897. notifier_list_add(&machine_init_done_notifiers, notify);
  1898. }
  1899. static void qemu_run_machine_init_done_notifiers(void)
  1900. {
  1901. notifier_list_notify(&machine_init_done_notifiers, NULL);
  1902. }
  1903. static const QEMUOption *lookup_opt(int argc, char **argv,
  1904. const char **poptarg, int *poptind)
  1905. {
  1906. const QEMUOption *popt;
  1907. int optind = *poptind;
  1908. char *r = argv[optind];
  1909. const char *optarg;
  1910. loc_set_cmdline(argv, optind, 1);
  1911. optind++;
  1912. /* Treat --foo the same as -foo. */
  1913. if (r[1] == '-')
  1914. r++;
  1915. popt = qemu_options;
  1916. for(;;) {
  1917. if (!popt->name) {
  1918. error_report("invalid option");
  1919. exit(1);
  1920. }
  1921. if (!strcmp(popt->name, r + 1))
  1922. break;
  1923. popt++;
  1924. }
  1925. if (popt->flags & HAS_ARG) {
  1926. if (optind >= argc) {
  1927. error_report("requires an argument");
  1928. exit(1);
  1929. }
  1930. optarg = argv[optind++];
  1931. loc_set_cmdline(argv, optind - 2, 2);
  1932. } else {
  1933. optarg = NULL;
  1934. }
  1935. *poptarg = optarg;
  1936. *poptind = optind;
  1937. return popt;
  1938. }
  1939. static gpointer malloc_and_trace(gsize n_bytes)
  1940. {
  1941. void *ptr = malloc(n_bytes);
  1942. trace_g_malloc(n_bytes, ptr);
  1943. return ptr;
  1944. }
  1945. static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
  1946. {
  1947. void *ptr = realloc(mem, n_bytes);
  1948. trace_g_realloc(mem, n_bytes, ptr);
  1949. return ptr;
  1950. }
  1951. static void free_and_trace(gpointer mem)
  1952. {
  1953. trace_g_free(mem);
  1954. free(mem);
  1955. }
  1956. int qemu_init_main_loop(void)
  1957. {
  1958. return main_loop_init();
  1959. }
  1960. int main(int argc, char **argv, char **envp)
  1961. {
  1962. int i;
  1963. int snapshot, linux_boot;
  1964. const char *icount_option = NULL;
  1965. const char *initrd_filename;
  1966. const char *kernel_filename, *kernel_cmdline;
  1967. char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
  1968. DisplayState *ds;
  1969. DisplayChangeListener *dcl;
  1970. int cyls, heads, secs, translation;
  1971. QemuOpts *hda_opts = NULL, *opts, *machine_opts;
  1972. QemuOptsList *olist;
  1973. int optind;
  1974. const char *optarg;
  1975. const char *loadvm = NULL;
  1976. QEMUMachine *machine;
  1977. const char *cpu_model;
  1978. const char *vga_model = "none";
  1979. const char *pid_file = NULL;
  1980. const char *incoming = NULL;
  1981. #ifdef CONFIG_VNC
  1982. int show_vnc_port = 0;
  1983. #endif
  1984. bool defconfig = true;
  1985. bool userconfig = true;
  1986. const char *log_mask = NULL;
  1987. const char *log_file = NULL;
  1988. GMemVTable mem_trace = {
  1989. .malloc = malloc_and_trace,
  1990. .realloc = realloc_and_trace,
  1991. .free = free_and_trace,
  1992. };
  1993. const char *trace_events = NULL;
  1994. const char *trace_file = NULL;
  1995. atexit(qemu_run_exit_notifiers);
  1996. error_set_progname(argv[0]);
  1997. g_mem_set_vtable(&mem_trace);
  1998. if (!g_thread_supported()) {
  1999. #if !GLIB_CHECK_VERSION(2, 31, 0)
  2000. g_thread_init(NULL);
  2001. #else
  2002. fprintf(stderr, "glib threading failed to initialize.\n");
  2003. exit(1);
  2004. #endif
  2005. }
  2006. module_call_init(MODULE_INIT_QOM);
  2007. runstate_init();
  2008. init_clocks();
  2009. rtc_clock = host_clock;
  2010. qemu_cache_utils_init(envp);
  2011. QLIST_INIT (&vm_change_state_head);
  2012. os_setup_early_signal_handling();
  2013. module_call_init(MODULE_INIT_MACHINE);
  2014. machine = find_default_machine();
  2015. cpu_model = NULL;
  2016. ram_size = 0;
  2017. snapshot = 0;
  2018. cyls = heads = secs = 0;
  2019. translation = BIOS_ATA_TRANSLATION_AUTO;
  2020. for (i = 0; i < MAX_NODES; i++) {
  2021. node_mem[i] = 0;
  2022. node_cpumask[i] = 0;
  2023. }
  2024. nb_numa_nodes = 0;
  2025. nb_nics = 0;
  2026. autostart= 1;
  2027. /* first pass of option parsing */
  2028. optind = 1;
  2029. while (optind < argc) {
  2030. if (argv[optind][0] != '-') {
  2031. /* disk image */
  2032. optind++;
  2033. continue;
  2034. } else {
  2035. const QEMUOption *popt;
  2036. popt = lookup_opt(argc, argv, &optarg, &optind);
  2037. switch (popt->index) {
  2038. case QEMU_OPTION_nodefconfig:
  2039. defconfig = false;
  2040. break;
  2041. case QEMU_OPTION_nouserconfig:
  2042. userconfig = false;
  2043. break;
  2044. }
  2045. }
  2046. }
  2047. if (defconfig) {
  2048. int ret;
  2049. ret = qemu_read_default_config_files(userconfig);
  2050. if (ret < 0) {
  2051. exit(1);
  2052. }
  2053. }
  2054. /* second pass of option parsing */
  2055. optind = 1;
  2056. for(;;) {
  2057. if (optind >= argc)
  2058. break;
  2059. if (argv[optind][0] != '-') {
  2060. hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
  2061. } else {
  2062. const QEMUOption *popt;
  2063. popt = lookup_opt(argc, argv, &optarg, &optind);
  2064. if (!(popt->arch_mask & arch_type)) {
  2065. printf("Option %s not supported for this target\n", popt->name);
  2066. exit(1);
  2067. }
  2068. switch(popt->index) {
  2069. case QEMU_OPTION_M:
  2070. machine = machine_parse(optarg);
  2071. break;
  2072. case QEMU_OPTION_cpu:
  2073. /* hw initialization will check this */
  2074. cpu_model = optarg;
  2075. break;
  2076. case QEMU_OPTION_hda:
  2077. {
  2078. char buf[256];
  2079. if (cyls == 0)
  2080. snprintf(buf, sizeof(buf), "%s", HD_OPTS);
  2081. else
  2082. snprintf(buf, sizeof(buf),
  2083. "%s,cyls=%d,heads=%d,secs=%d%s",
  2084. HD_OPTS , cyls, heads, secs,
  2085. translation == BIOS_ATA_TRANSLATION_LBA ?
  2086. ",trans=lba" :
  2087. translation == BIOS_ATA_TRANSLATION_NONE ?
  2088. ",trans=none" : "");
  2089. drive_add(IF_DEFAULT, 0, optarg, buf);
  2090. break;
  2091. }
  2092. case QEMU_OPTION_hdb:
  2093. case QEMU_OPTION_hdc:
  2094. case QEMU_OPTION_hdd:
  2095. drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
  2096. HD_OPTS);
  2097. break;
  2098. case QEMU_OPTION_drive:
  2099. if (drive_def(optarg) == NULL) {
  2100. exit(1);
  2101. }
  2102. break;
  2103. case QEMU_OPTION_set:
  2104. if (qemu_set_option(optarg) != 0)
  2105. exit(1);
  2106. break;
  2107. case QEMU_OPTION_global:
  2108. if (qemu_global_option(optarg) != 0)
  2109. exit(1);
  2110. break;
  2111. case QEMU_OPTION_mtdblock:
  2112. drive_add(IF_MTD, -1, optarg, MTD_OPTS);
  2113. break;
  2114. case QEMU_OPTION_sd:
  2115. drive_add(IF_SD, 0, optarg, SD_OPTS);
  2116. break;
  2117. case QEMU_OPTION_pflash:
  2118. drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
  2119. break;
  2120. case QEMU_OPTION_snapshot:
  2121. snapshot = 1;
  2122. break;
  2123. case QEMU_OPTION_hdachs:
  2124. {
  2125. const char *p;
  2126. p = optarg;
  2127. cyls = strtol(p, (char **)&p, 0);
  2128. if (cyls < 1 || cyls > 16383)
  2129. goto chs_fail;
  2130. if (*p != ',')
  2131. goto chs_fail;
  2132. p++;
  2133. heads = strtol(p, (char **)&p, 0);
  2134. if (heads < 1 || heads > 16)
  2135. goto chs_fail;
  2136. if (*p != ',')
  2137. goto chs_fail;
  2138. p++;
  2139. secs = strtol(p, (char **)&p, 0);
  2140. if (secs < 1 || secs > 63)
  2141. goto chs_fail;
  2142. if (*p == ',') {
  2143. p++;
  2144. if (!strcmp(p, "none"))
  2145. translation = BIOS_ATA_TRANSLATION_NONE;
  2146. else if (!strcmp(p, "lba"))
  2147. translation = BIOS_ATA_TRANSLATION_LBA;
  2148. else if (!strcmp(p, "auto"))
  2149. translation = BIOS_ATA_TRANSLATION_AUTO;
  2150. else
  2151. goto chs_fail;
  2152. } else if (*p != '\0') {
  2153. chs_fail:
  2154. fprintf(stderr, "qemu: invalid physical CHS format\n");
  2155. exit(1);
  2156. }
  2157. if (hda_opts != NULL) {
  2158. char num[16];
  2159. snprintf(num, sizeof(num), "%d", cyls);
  2160. qemu_opt_set(hda_opts, "cyls", num);
  2161. snprintf(num, sizeof(num), "%d", heads);
  2162. qemu_opt_set(hda_opts, "heads", num);
  2163. snprintf(num, sizeof(num), "%d", secs);
  2164. qemu_opt_set(hda_opts, "secs", num);
  2165. if (translation == BIOS_ATA_TRANSLATION_LBA)
  2166. qemu_opt_set(hda_opts, "trans", "lba");
  2167. if (translation == BIOS_ATA_TRANSLATION_NONE)
  2168. qemu_opt_set(hda_opts, "trans", "none");
  2169. }
  2170. }
  2171. break;
  2172. case QEMU_OPTION_numa:
  2173. if (nb_numa_nodes >= MAX_NODES) {
  2174. fprintf(stderr, "qemu: too many NUMA nodes\n");
  2175. exit(1);
  2176. }
  2177. numa_add(optarg);
  2178. break;
  2179. case QEMU_OPTION_display:
  2180. display_type = select_display(optarg);
  2181. break;
  2182. case QEMU_OPTION_nographic:
  2183. display_type = DT_NOGRAPHIC;
  2184. break;
  2185. case QEMU_OPTION_curses:
  2186. #ifdef CONFIG_CURSES
  2187. display_type = DT_CURSES;
  2188. #else
  2189. fprintf(stderr, "Curses support is disabled\n");
  2190. exit(1);
  2191. #endif
  2192. break;
  2193. case QEMU_OPTION_portrait:
  2194. graphic_rotate = 90;
  2195. break;
  2196. case QEMU_OPTION_rotate:
  2197. graphic_rotate = strtol(optarg, (char **) &optarg, 10);
  2198. if (graphic_rotate != 0 && graphic_rotate != 90 &&
  2199. graphic_rotate != 180 && graphic_rotate != 270) {
  2200. fprintf(stderr,
  2201. "qemu: only 90, 180, 270 deg rotation is available\n");
  2202. exit(1);
  2203. }
  2204. break;
  2205. case QEMU_OPTION_kernel:
  2206. qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
  2207. break;
  2208. case QEMU_OPTION_initrd:
  2209. qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
  2210. break;
  2211. case QEMU_OPTION_append:
  2212. qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
  2213. break;
  2214. case QEMU_OPTION_dtb:
  2215. qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
  2216. break;
  2217. case QEMU_OPTION_cdrom:
  2218. drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
  2219. break;
  2220. case QEMU_OPTION_boot:
  2221. {
  2222. static const char * const params[] = {
  2223. "order", "once", "menu",
  2224. "splash", "splash-time", NULL
  2225. };
  2226. char buf[sizeof(boot_devices)];
  2227. char *standard_boot_devices;
  2228. int legacy = 0;
  2229. if (!strchr(optarg, '=')) {
  2230. legacy = 1;
  2231. pstrcpy(buf, sizeof(buf), optarg);
  2232. } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
  2233. fprintf(stderr,
  2234. "qemu: unknown boot parameter '%s' in '%s'\n",
  2235. buf, optarg);
  2236. exit(1);
  2237. }
  2238. if (legacy ||
  2239. get_param_value(buf, sizeof(buf), "order", optarg)) {
  2240. validate_bootdevices(buf);
  2241. pstrcpy(boot_devices, sizeof(boot_devices), buf);
  2242. }
  2243. if (!legacy) {
  2244. if (get_param_value(buf, sizeof(buf),
  2245. "once", optarg)) {
  2246. validate_bootdevices(buf);
  2247. standard_boot_devices = g_strdup(boot_devices);
  2248. pstrcpy(boot_devices, sizeof(boot_devices), buf);
  2249. qemu_register_reset(restore_boot_devices,
  2250. standard_boot_devices);
  2251. }
  2252. if (get_param_value(buf, sizeof(buf),
  2253. "menu", optarg)) {
  2254. if (!strcmp(buf, "on")) {
  2255. boot_menu = 1;
  2256. } else if (!strcmp(buf, "off")) {
  2257. boot_menu = 0;
  2258. } else {
  2259. fprintf(stderr,
  2260. "qemu: invalid option value '%s'\n",
  2261. buf);
  2262. exit(1);
  2263. }
  2264. }
  2265. qemu_opts_parse(qemu_find_opts("boot-opts"),
  2266. optarg, 0);
  2267. }
  2268. }
  2269. break;
  2270. case QEMU_OPTION_fda:
  2271. case QEMU_OPTION_fdb:
  2272. drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
  2273. optarg, FD_OPTS);
  2274. break;
  2275. case QEMU_OPTION_no_fd_bootchk:
  2276. fd_bootchk = 0;
  2277. break;
  2278. case QEMU_OPTION_netdev:
  2279. if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
  2280. exit(1);
  2281. }
  2282. break;
  2283. case QEMU_OPTION_net:
  2284. if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
  2285. exit(1);
  2286. }
  2287. break;
  2288. #ifdef CONFIG_LIBISCSI
  2289. case QEMU_OPTION_iscsi:
  2290. opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
  2291. if (!opts) {
  2292. exit(1);
  2293. }
  2294. break;
  2295. #endif
  2296. #ifdef CONFIG_SLIRP
  2297. case QEMU_OPTION_tftp:
  2298. legacy_tftp_prefix = optarg;
  2299. break;
  2300. case QEMU_OPTION_bootp:
  2301. legacy_bootp_filename = optarg;
  2302. break;
  2303. case QEMU_OPTION_redir:
  2304. if (net_slirp_redir(optarg) < 0)
  2305. exit(1);
  2306. break;
  2307. #endif
  2308. case QEMU_OPTION_bt:
  2309. add_device_config(DEV_BT, optarg);
  2310. break;
  2311. case QEMU_OPTION_audio_help:
  2312. if (!(audio_available())) {
  2313. printf("Option %s not supported for this target\n", popt->name);
  2314. exit(1);
  2315. }
  2316. AUD_help ();
  2317. exit (0);
  2318. break;
  2319. case QEMU_OPTION_soundhw:
  2320. if (!(audio_available())) {
  2321. printf("Option %s not supported for this target\n", popt->name);
  2322. exit(1);
  2323. }
  2324. select_soundhw (optarg);
  2325. break;
  2326. case QEMU_OPTION_h:
  2327. help(0);
  2328. break;
  2329. case QEMU_OPTION_version:
  2330. version();
  2331. exit(0);
  2332. break;
  2333. case QEMU_OPTION_m: {
  2334. int64_t value;
  2335. uint64_t sz;
  2336. char *end;
  2337. value = strtosz(optarg, &end);
  2338. if (value < 0 || *end) {
  2339. fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
  2340. exit(1);
  2341. }
  2342. sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
  2343. ram_size = sz;
  2344. if (ram_size != sz) {
  2345. fprintf(stderr, "qemu: ram size too large\n");
  2346. exit(1);
  2347. }
  2348. break;
  2349. }
  2350. case QEMU_OPTION_mempath:
  2351. mem_path = optarg;
  2352. break;
  2353. #ifdef MAP_POPULATE
  2354. case QEMU_OPTION_mem_prealloc:
  2355. mem_prealloc = 1;
  2356. break;
  2357. #endif
  2358. case QEMU_OPTION_d:
  2359. log_mask = optarg;
  2360. break;
  2361. case QEMU_OPTION_D:
  2362. log_file = optarg;
  2363. break;
  2364. case QEMU_OPTION_s:
  2365. add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
  2366. break;
  2367. case QEMU_OPTION_gdb:
  2368. add_device_config(DEV_GDB, optarg);
  2369. break;
  2370. case QEMU_OPTION_L:
  2371. data_dir = optarg;
  2372. break;
  2373. case QEMU_OPTION_bios:
  2374. bios_name = optarg;
  2375. break;
  2376. case QEMU_OPTION_singlestep:
  2377. singlestep = 1;
  2378. break;
  2379. case QEMU_OPTION_S:
  2380. autostart = 0;
  2381. break;
  2382. case QEMU_OPTION_k:
  2383. keyboard_layout = optarg;
  2384. break;
  2385. case QEMU_OPTION_localtime:
  2386. rtc_utc = 0;
  2387. break;
  2388. case QEMU_OPTION_vga:
  2389. vga_model = optarg;
  2390. default_vga = 0;
  2391. break;
  2392. case QEMU_OPTION_g:
  2393. {
  2394. const char *p;
  2395. int w, h, depth;
  2396. p = optarg;
  2397. w = strtol(p, (char **)&p, 10);
  2398. if (w <= 0) {
  2399. graphic_error:
  2400. fprintf(stderr, "qemu: invalid resolution or depth\n");
  2401. exit(1);
  2402. }
  2403. if (*p != 'x')
  2404. goto graphic_error;
  2405. p++;
  2406. h = strtol(p, (char **)&p, 10);
  2407. if (h <= 0)
  2408. goto graphic_error;
  2409. if (*p == 'x') {
  2410. p++;
  2411. depth = strtol(p, (char **)&p, 10);
  2412. if (depth != 8 && depth != 15 && depth != 16 &&
  2413. depth != 24 && depth != 32)
  2414. goto graphic_error;
  2415. } else if (*p == '\0') {
  2416. depth = graphic_depth;
  2417. } else {
  2418. goto graphic_error;
  2419. }
  2420. graphic_width = w;
  2421. graphic_height = h;
  2422. graphic_depth = depth;
  2423. }
  2424. break;
  2425. case QEMU_OPTION_echr:
  2426. {
  2427. char *r;
  2428. term_escape_char = strtol(optarg, &r, 0);
  2429. if (r == optarg)
  2430. printf("Bad argument to echr\n");
  2431. break;
  2432. }
  2433. case QEMU_OPTION_monitor:
  2434. monitor_parse(optarg, "readline");
  2435. default_monitor = 0;
  2436. break;
  2437. case QEMU_OPTION_qmp:
  2438. monitor_parse(optarg, "control");
  2439. default_monitor = 0;
  2440. break;
  2441. case QEMU_OPTION_mon:
  2442. opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
  2443. if (!opts) {
  2444. exit(1);
  2445. }
  2446. default_monitor = 0;
  2447. break;
  2448. case QEMU_OPTION_chardev:
  2449. opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
  2450. if (!opts) {
  2451. exit(1);
  2452. }
  2453. break;
  2454. case QEMU_OPTION_fsdev:
  2455. olist = qemu_find_opts("fsdev");
  2456. if (!olist) {
  2457. fprintf(stderr, "fsdev is not supported by this qemu build.\n");
  2458. exit(1);
  2459. }
  2460. opts = qemu_opts_parse(olist, optarg, 1);
  2461. if (!opts) {
  2462. fprintf(stderr, "parse error: %s\n", optarg);
  2463. exit(1);
  2464. }
  2465. break;
  2466. case QEMU_OPTION_virtfs: {
  2467. QemuOpts *fsdev;
  2468. QemuOpts *device;
  2469. const char *writeout, *sock_fd, *socket;
  2470. olist = qemu_find_opts("virtfs");
  2471. if (!olist) {
  2472. fprintf(stderr, "virtfs is not supported by this qemu build.\n");
  2473. exit(1);
  2474. }
  2475. opts = qemu_opts_parse(olist, optarg, 1);
  2476. if (!opts) {
  2477. fprintf(stderr, "parse error: %s\n", optarg);
  2478. exit(1);
  2479. }
  2480. if (qemu_opt_get(opts, "fsdriver") == NULL ||
  2481. qemu_opt_get(opts, "mount_tag") == NULL) {
  2482. fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
  2483. exit(1);
  2484. }
  2485. fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
  2486. qemu_opt_get(opts, "mount_tag"), 1);
  2487. if (!fsdev) {
  2488. fprintf(stderr, "duplicate fsdev id: %s\n",
  2489. qemu_opt_get(opts, "mount_tag"));
  2490. exit(1);
  2491. }
  2492. writeout = qemu_opt_get(opts, "writeout");
  2493. if (writeout) {
  2494. #ifdef CONFIG_SYNC_FILE_RANGE
  2495. qemu_opt_set(fsdev, "writeout", writeout);
  2496. #else
  2497. fprintf(stderr, "writeout=immediate not supported on "
  2498. "this platform\n");
  2499. exit(1);
  2500. #endif
  2501. }
  2502. qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
  2503. qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
  2504. qemu_opt_set(fsdev, "security_model",
  2505. qemu_opt_get(opts, "security_model"));
  2506. socket = qemu_opt_get(opts, "socket");
  2507. if (socket) {
  2508. qemu_opt_set(fsdev, "socket", socket);
  2509. }
  2510. sock_fd = qemu_opt_get(opts, "sock_fd");
  2511. if (sock_fd) {
  2512. qemu_opt_set(fsdev, "sock_fd", sock_fd);
  2513. }
  2514. qemu_opt_set_bool(fsdev, "readonly",
  2515. qemu_opt_get_bool(opts, "readonly", 0));
  2516. device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
  2517. qemu_opt_set(device, "driver", "virtio-9p-pci");
  2518. qemu_opt_set(device, "fsdev",
  2519. qemu_opt_get(opts, "mount_tag"));
  2520. qemu_opt_set(device, "mount_tag",
  2521. qemu_opt_get(opts, "mount_tag"));
  2522. break;
  2523. }
  2524. case QEMU_OPTION_virtfs_synth: {
  2525. QemuOpts *fsdev;
  2526. QemuOpts *device;
  2527. fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
  2528. if (!fsdev) {
  2529. fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
  2530. exit(1);
  2531. }
  2532. qemu_opt_set(fsdev, "fsdriver", "synth");
  2533. device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
  2534. qemu_opt_set(device, "driver", "virtio-9p-pci");
  2535. qemu_opt_set(device, "fsdev", "v_synth");
  2536. qemu_opt_set(device, "mount_tag", "v_synth");
  2537. break;
  2538. }
  2539. case QEMU_OPTION_serial:
  2540. add_device_config(DEV_SERIAL, optarg);
  2541. default_serial = 0;
  2542. if (strncmp(optarg, "mon:", 4) == 0) {
  2543. default_monitor = 0;
  2544. }
  2545. break;
  2546. case QEMU_OPTION_watchdog:
  2547. if (watchdog) {
  2548. fprintf(stderr,
  2549. "qemu: only one watchdog option may be given\n");
  2550. return 1;
  2551. }
  2552. watchdog = optarg;
  2553. break;
  2554. case QEMU_OPTION_watchdog_action:
  2555. if (select_watchdog_action(optarg) == -1) {
  2556. fprintf(stderr, "Unknown -watchdog-action parameter\n");
  2557. exit(1);
  2558. }
  2559. break;
  2560. case QEMU_OPTION_virtiocon:
  2561. add_device_config(DEV_VIRTCON, optarg);
  2562. default_virtcon = 0;
  2563. if (strncmp(optarg, "mon:", 4) == 0) {
  2564. default_monitor = 0;
  2565. }
  2566. break;
  2567. case QEMU_OPTION_parallel:
  2568. add_device_config(DEV_PARALLEL, optarg);
  2569. default_parallel = 0;
  2570. if (strncmp(optarg, "mon:", 4) == 0) {
  2571. default_monitor = 0;
  2572. }
  2573. break;
  2574. case QEMU_OPTION_debugcon:
  2575. add_device_config(DEV_DEBUGCON, optarg);
  2576. break;
  2577. case QEMU_OPTION_loadvm:
  2578. loadvm = optarg;
  2579. break;
  2580. case QEMU_OPTION_full_screen:
  2581. full_screen = 1;
  2582. break;
  2583. #ifdef CONFIG_SDL
  2584. case QEMU_OPTION_no_frame:
  2585. no_frame = 1;
  2586. break;
  2587. case QEMU_OPTION_alt_grab:
  2588. alt_grab = 1;
  2589. break;
  2590. case QEMU_OPTION_ctrl_grab:
  2591. ctrl_grab = 1;
  2592. break;
  2593. case QEMU_OPTION_no_quit:
  2594. no_quit = 1;
  2595. break;
  2596. case QEMU_OPTION_sdl:
  2597. display_type = DT_SDL;
  2598. break;
  2599. #else
  2600. case QEMU_OPTION_no_frame:
  2601. case QEMU_OPTION_alt_grab:
  2602. case QEMU_OPTION_ctrl_grab:
  2603. case QEMU_OPTION_no_quit:
  2604. case QEMU_OPTION_sdl:
  2605. fprintf(stderr, "SDL support is disabled\n");
  2606. exit(1);
  2607. #endif
  2608. case QEMU_OPTION_pidfile:
  2609. pid_file = optarg;
  2610. break;
  2611. case QEMU_OPTION_win2k_hack:
  2612. win2k_install_hack = 1;
  2613. break;
  2614. case QEMU_OPTION_rtc_td_hack: {
  2615. static GlobalProperty slew_lost_ticks[] = {
  2616. {
  2617. .driver = "mc146818rtc",
  2618. .property = "lost_tick_policy",
  2619. .value = "slew",
  2620. },
  2621. { /* end of list */ }
  2622. };
  2623. qdev_prop_register_global_list(slew_lost_ticks);
  2624. break;
  2625. }
  2626. case QEMU_OPTION_acpitable:
  2627. do_acpitable_option(optarg);
  2628. break;
  2629. case QEMU_OPTION_smbios:
  2630. do_smbios_option(optarg);
  2631. break;
  2632. case QEMU_OPTION_enable_kvm:
  2633. olist = qemu_find_opts("machine");
  2634. qemu_opts_parse(olist, "accel=kvm", 0);
  2635. break;
  2636. case QEMU_OPTION_machine:
  2637. olist = qemu_find_opts("machine");
  2638. opts = qemu_opts_parse(olist, optarg, 1);
  2639. if (!opts) {
  2640. fprintf(stderr, "parse error: %s\n", optarg);
  2641. exit(1);
  2642. }
  2643. optarg = qemu_opt_get(opts, "type");
  2644. if (optarg) {
  2645. machine = machine_parse(optarg);
  2646. }
  2647. break;
  2648. case QEMU_OPTION_usb:
  2649. usb_enabled = 1;
  2650. break;
  2651. case QEMU_OPTION_usbdevice:
  2652. usb_enabled = 1;
  2653. add_device_config(DEV_USB, optarg);
  2654. break;
  2655. case QEMU_OPTION_device:
  2656. if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
  2657. exit(1);
  2658. }
  2659. break;
  2660. case QEMU_OPTION_smp:
  2661. smp_parse(optarg);
  2662. if (smp_cpus < 1) {
  2663. fprintf(stderr, "Invalid number of CPUs\n");
  2664. exit(1);
  2665. }
  2666. if (max_cpus < smp_cpus) {
  2667. fprintf(stderr, "maxcpus must be equal to or greater than "
  2668. "smp\n");
  2669. exit(1);
  2670. }
  2671. if (max_cpus > 255) {
  2672. fprintf(stderr, "Unsupported number of maxcpus\n");
  2673. exit(1);
  2674. }
  2675. break;
  2676. case QEMU_OPTION_vnc:
  2677. #ifdef CONFIG_VNC
  2678. display_remote++;
  2679. vnc_display = optarg;
  2680. #else
  2681. fprintf(stderr, "VNC support is disabled\n");
  2682. exit(1);
  2683. #endif
  2684. break;
  2685. case QEMU_OPTION_no_acpi:
  2686. acpi_enabled = 0;
  2687. break;
  2688. case QEMU_OPTION_no_hpet:
  2689. no_hpet = 1;
  2690. break;
  2691. case QEMU_OPTION_balloon:
  2692. if (balloon_parse(optarg) < 0) {
  2693. fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
  2694. exit(1);
  2695. }
  2696. break;
  2697. case QEMU_OPTION_no_reboot:
  2698. no_reboot = 1;
  2699. break;
  2700. case QEMU_OPTION_no_shutdown:
  2701. no_shutdown = 1;
  2702. break;
  2703. case QEMU_OPTION_show_cursor:
  2704. cursor_hide = 0;
  2705. break;
  2706. case QEMU_OPTION_uuid:
  2707. if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
  2708. fprintf(stderr, "Fail to parse UUID string."
  2709. " Wrong format.\n");
  2710. exit(1);
  2711. }
  2712. break;
  2713. case QEMU_OPTION_option_rom:
  2714. if (nb_option_roms >= MAX_OPTION_ROMS) {
  2715. fprintf(stderr, "Too many option ROMs\n");
  2716. exit(1);
  2717. }
  2718. opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
  2719. option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
  2720. option_rom[nb_option_roms].bootindex =
  2721. qemu_opt_get_number(opts, "bootindex", -1);
  2722. if (!option_rom[nb_option_roms].name) {
  2723. fprintf(stderr, "Option ROM file is not specified\n");
  2724. exit(1);
  2725. }
  2726. nb_option_roms++;
  2727. break;
  2728. case QEMU_OPTION_semihosting:
  2729. semihosting_enabled = 1;
  2730. break;
  2731. case QEMU_OPTION_name:
  2732. qemu_name = g_strdup(optarg);
  2733. {
  2734. char *p = strchr(qemu_name, ',');
  2735. if (p != NULL) {
  2736. *p++ = 0;
  2737. if (strncmp(p, "process=", 8)) {
  2738. fprintf(stderr, "Unknown subargument %s to -name\n", p);
  2739. exit(1);
  2740. }
  2741. p += 8;
  2742. os_set_proc_name(p);
  2743. }
  2744. }
  2745. break;
  2746. case QEMU_OPTION_prom_env:
  2747. if (nb_prom_envs >= MAX_PROM_ENVS) {
  2748. fprintf(stderr, "Too many prom variables\n");
  2749. exit(1);
  2750. }
  2751. prom_envs[nb_prom_envs] = optarg;
  2752. nb_prom_envs++;
  2753. break;
  2754. case QEMU_OPTION_old_param:
  2755. old_param = 1;
  2756. break;
  2757. case QEMU_OPTION_clock:
  2758. configure_alarms(optarg);
  2759. break;
  2760. case QEMU_OPTION_startdate:
  2761. configure_rtc_date_offset(optarg, 1);
  2762. break;
  2763. case QEMU_OPTION_rtc:
  2764. opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
  2765. if (!opts) {
  2766. exit(1);
  2767. }
  2768. configure_rtc(opts);
  2769. break;
  2770. case QEMU_OPTION_tb_size:
  2771. tcg_tb_size = strtol(optarg, NULL, 0);
  2772. if (tcg_tb_size < 0) {
  2773. tcg_tb_size = 0;
  2774. }
  2775. break;
  2776. case QEMU_OPTION_icount:
  2777. icount_option = optarg;
  2778. break;
  2779. case QEMU_OPTION_incoming:
  2780. incoming = optarg;
  2781. runstate_set(RUN_STATE_INMIGRATE);
  2782. break;
  2783. case QEMU_OPTION_nodefaults:
  2784. default_serial = 0;
  2785. default_parallel = 0;
  2786. default_virtcon = 0;
  2787. default_monitor = 0;
  2788. default_net = 0;
  2789. default_floppy = 0;
  2790. default_cdrom = 0;
  2791. default_sdcard = 0;
  2792. default_vga = 0;
  2793. break;
  2794. case QEMU_OPTION_xen_domid:
  2795. if (!(xen_available())) {
  2796. printf("Option %s not supported for this target\n", popt->name);
  2797. exit(1);
  2798. }
  2799. xen_domid = atoi(optarg);
  2800. break;
  2801. case QEMU_OPTION_xen_create:
  2802. if (!(xen_available())) {
  2803. printf("Option %s not supported for this target\n", popt->name);
  2804. exit(1);
  2805. }
  2806. xen_mode = XEN_CREATE;
  2807. break;
  2808. case QEMU_OPTION_xen_attach:
  2809. if (!(xen_available())) {
  2810. printf("Option %s not supported for this target\n", popt->name);
  2811. exit(1);
  2812. }
  2813. xen_mode = XEN_ATTACH;
  2814. break;
  2815. case QEMU_OPTION_trace:
  2816. {
  2817. opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
  2818. if (!opts) {
  2819. exit(1);
  2820. }
  2821. trace_events = qemu_opt_get(opts, "events");
  2822. trace_file = qemu_opt_get(opts, "file");
  2823. break;
  2824. }
  2825. case QEMU_OPTION_readconfig:
  2826. {
  2827. int ret = qemu_read_config_file(optarg);
  2828. if (ret < 0) {
  2829. fprintf(stderr, "read config %s: %s\n", optarg,
  2830. strerror(-ret));
  2831. exit(1);
  2832. }
  2833. break;
  2834. }
  2835. case QEMU_OPTION_spice:
  2836. olist = qemu_find_opts("spice");
  2837. if (!olist) {
  2838. fprintf(stderr, "spice is not supported by this qemu build.\n");
  2839. exit(1);
  2840. }
  2841. opts = qemu_opts_parse(olist, optarg, 0);
  2842. if (!opts) {
  2843. fprintf(stderr, "parse error: %s\n", optarg);
  2844. exit(1);
  2845. }
  2846. break;
  2847. case QEMU_OPTION_writeconfig:
  2848. {
  2849. FILE *fp;
  2850. if (strcmp(optarg, "-") == 0) {
  2851. fp = stdout;
  2852. } else {
  2853. fp = fopen(optarg, "w");
  2854. if (fp == NULL) {
  2855. fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
  2856. exit(1);
  2857. }
  2858. }
  2859. qemu_config_write(fp);
  2860. fclose(fp);
  2861. break;
  2862. }
  2863. case QEMU_OPTION_qtest:
  2864. qtest_chrdev = optarg;
  2865. break;
  2866. case QEMU_OPTION_qtest_log:
  2867. qtest_log = optarg;
  2868. break;
  2869. default:
  2870. os_parse_cmd_args(popt->index, optarg);
  2871. }
  2872. }
  2873. }
  2874. loc_set_none();
  2875. /* Init CPU def lists, based on config
  2876. * - Must be called after all the qemu_read_config_file() calls
  2877. * - Must be called before list_cpus()
  2878. * - Must be called before machine->init()
  2879. */
  2880. cpudef_init();
  2881. if (cpu_model && *cpu_model == '?') {
  2882. list_cpus(stdout, &fprintf, cpu_model);
  2883. exit(0);
  2884. }
  2885. /* Open the logfile at this point, if necessary. We can't open the logfile
  2886. * when encountering either of the logging options (-d or -D) because the
  2887. * other one may be encountered later on the command line, changing the
  2888. * location or level of logging.
  2889. */
  2890. if (log_mask) {
  2891. if (log_file) {
  2892. set_cpu_log_filename(log_file);
  2893. }
  2894. set_cpu_log(log_mask);
  2895. }
  2896. if (!trace_backend_init(trace_events, trace_file)) {
  2897. exit(1);
  2898. }
  2899. /* If no data_dir is specified then try to find it relative to the
  2900. executable path. */
  2901. if (!data_dir) {
  2902. data_dir = os_find_datadir(argv[0]);
  2903. }
  2904. /* If all else fails use the install path specified when building. */
  2905. if (!data_dir) {
  2906. data_dir = CONFIG_QEMU_DATADIR;
  2907. }
  2908. if (machine == NULL) {
  2909. fprintf(stderr, "No machine found.\n");
  2910. exit(1);
  2911. }
  2912. /*
  2913. * Default to max_cpus = smp_cpus, in case the user doesn't
  2914. * specify a max_cpus value.
  2915. */
  2916. if (!max_cpus)
  2917. max_cpus = smp_cpus;
  2918. machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
  2919. if (smp_cpus > machine->max_cpus) {
  2920. fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
  2921. "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
  2922. machine->max_cpus);
  2923. exit(1);
  2924. }
  2925. /*
  2926. * Get the default machine options from the machine if it is not already
  2927. * specified either by the configuration file or by the command line.
  2928. */
  2929. if (machine->default_machine_opts) {
  2930. qemu_opts_set_defaults(qemu_find_opts("machine"),
  2931. machine->default_machine_opts, 0);
  2932. }
  2933. qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
  2934. qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
  2935. if (machine->no_serial) {
  2936. default_serial = 0;
  2937. }
  2938. if (machine->no_parallel) {
  2939. default_parallel = 0;
  2940. }
  2941. if (!machine->use_virtcon) {
  2942. default_virtcon = 0;
  2943. }
  2944. if (machine->no_floppy) {
  2945. default_floppy = 0;
  2946. }
  2947. if (machine->no_cdrom) {
  2948. default_cdrom = 0;
  2949. }
  2950. if (machine->no_sdcard) {
  2951. default_sdcard = 0;
  2952. }
  2953. if (display_type == DT_NOGRAPHIC) {
  2954. if (default_parallel)
  2955. add_device_config(DEV_PARALLEL, "null");
  2956. if (default_serial && default_monitor) {
  2957. add_device_config(DEV_SERIAL, "mon:stdio");
  2958. } else if (default_virtcon && default_monitor) {
  2959. add_device_config(DEV_VIRTCON, "mon:stdio");
  2960. } else {
  2961. if (default_serial)
  2962. add_device_config(DEV_SERIAL, "stdio");
  2963. if (default_virtcon)
  2964. add_device_config(DEV_VIRTCON, "stdio");
  2965. if (default_monitor)
  2966. monitor_parse("stdio", "readline");
  2967. }
  2968. } else {
  2969. if (default_serial)
  2970. add_device_config(DEV_SERIAL, "vc:80Cx24C");
  2971. if (default_parallel)
  2972. add_device_config(DEV_PARALLEL, "vc:80Cx24C");
  2973. if (default_monitor)
  2974. monitor_parse("vc:80Cx24C", "readline");
  2975. if (default_virtcon)
  2976. add_device_config(DEV_VIRTCON, "vc:80Cx24C");
  2977. }
  2978. socket_init();
  2979. if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
  2980. exit(1);
  2981. #ifdef CONFIG_VIRTFS
  2982. if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
  2983. exit(1);
  2984. }
  2985. #endif
  2986. os_daemonize();
  2987. if (pid_file && qemu_create_pidfile(pid_file) != 0) {
  2988. os_pidfile_error();
  2989. exit(1);
  2990. }
  2991. /* init the memory */
  2992. if (ram_size == 0) {
  2993. ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
  2994. }
  2995. configure_accelerator();
  2996. qemu_init_cpu_loop();
  2997. if (qemu_init_main_loop()) {
  2998. fprintf(stderr, "qemu_init_main_loop failed\n");
  2999. exit(1);
  3000. }
  3001. machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
  3002. if (machine_opts) {
  3003. kernel_filename = qemu_opt_get(machine_opts, "kernel");
  3004. initrd_filename = qemu_opt_get(machine_opts, "initrd");
  3005. kernel_cmdline = qemu_opt_get(machine_opts, "append");
  3006. } else {
  3007. kernel_filename = initrd_filename = kernel_cmdline = NULL;
  3008. }
  3009. if (!kernel_cmdline) {
  3010. kernel_cmdline = "";
  3011. }
  3012. linux_boot = (kernel_filename != NULL);
  3013. if (!linux_boot && *kernel_cmdline != '\0') {
  3014. fprintf(stderr, "-append only allowed with -kernel option\n");
  3015. exit(1);
  3016. }
  3017. if (!linux_boot && initrd_filename != NULL) {
  3018. fprintf(stderr, "-initrd only allowed with -kernel option\n");
  3019. exit(1);
  3020. }
  3021. if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
  3022. fprintf(stderr, "-dtb only allowed with -kernel option\n");
  3023. exit(1);
  3024. }
  3025. os_set_line_buffering();
  3026. if (init_timer_alarm() < 0) {
  3027. fprintf(stderr, "could not initialize alarm timer\n");
  3028. exit(1);
  3029. }
  3030. #ifdef CONFIG_SPICE
  3031. /* spice needs the timers to be initialized by this point */
  3032. qemu_spice_init();
  3033. #endif
  3034. if (icount_option && (kvm_enabled() || xen_enabled())) {
  3035. fprintf(stderr, "-icount is not allowed with kvm or xen\n");
  3036. exit(1);
  3037. }
  3038. configure_icount(icount_option);
  3039. if (net_init_clients() < 0) {
  3040. exit(1);
  3041. }
  3042. /* init the bluetooth world */
  3043. if (foreach_device_config(DEV_BT, bt_parse))
  3044. exit(1);
  3045. if (!xen_enabled()) {
  3046. /* On 32-bit hosts, QEMU is limited by virtual address space */
  3047. if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
  3048. fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
  3049. exit(1);
  3050. }
  3051. }
  3052. cpu_exec_init_all();
  3053. bdrv_init_with_whitelist();
  3054. blk_mig_init();
  3055. /* open the virtual block devices */
  3056. if (snapshot)
  3057. qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
  3058. if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
  3059. exit(1);
  3060. default_drive(default_cdrom, snapshot, machine->use_scsi,
  3061. IF_DEFAULT, 2, CDROM_OPTS);
  3062. default_drive(default_floppy, snapshot, machine->use_scsi,
  3063. IF_FLOPPY, 0, FD_OPTS);
  3064. default_drive(default_sdcard, snapshot, machine->use_scsi,
  3065. IF_SD, 0, SD_OPTS);
  3066. register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
  3067. ram_load, NULL);
  3068. if (nb_numa_nodes > 0) {
  3069. int i;
  3070. if (nb_numa_nodes > MAX_NODES) {
  3071. nb_numa_nodes = MAX_NODES;
  3072. }
  3073. /* If no memory size if given for any node, assume the default case
  3074. * and distribute the available memory equally across all nodes
  3075. */
  3076. for (i = 0; i < nb_numa_nodes; i++) {
  3077. if (node_mem[i] != 0)
  3078. break;
  3079. }
  3080. if (i == nb_numa_nodes) {
  3081. uint64_t usedmem = 0;
  3082. /* On Linux, the each node's border has to be 8MB aligned,
  3083. * the final node gets the rest.
  3084. */
  3085. for (i = 0; i < nb_numa_nodes - 1; i++) {
  3086. node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
  3087. usedmem += node_mem[i];
  3088. }
  3089. node_mem[i] = ram_size - usedmem;
  3090. }
  3091. for (i = 0; i < nb_numa_nodes; i++) {
  3092. if (node_cpumask[i] != 0)
  3093. break;
  3094. }
  3095. /* assigning the VCPUs round-robin is easier to implement, guest OSes
  3096. * must cope with this anyway, because there are BIOSes out there in
  3097. * real machines which also use this scheme.
  3098. */
  3099. if (i == nb_numa_nodes) {
  3100. for (i = 0; i < max_cpus; i++) {
  3101. node_cpumask[i % nb_numa_nodes] |= 1 << i;
  3102. }
  3103. }
  3104. }
  3105. if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
  3106. exit(1);
  3107. }
  3108. if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
  3109. exit(1);
  3110. if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
  3111. exit(1);
  3112. if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
  3113. exit(1);
  3114. if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
  3115. exit(1);
  3116. /* If no default VGA is requested, the default is "none". */
  3117. if (default_vga && cirrus_vga_available()) {
  3118. vga_model = "cirrus";
  3119. }
  3120. select_vgahw(vga_model);
  3121. if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
  3122. exit(0);
  3123. if (watchdog) {
  3124. i = select_watchdog(watchdog);
  3125. if (i > 0)
  3126. exit (i == 1 ? 1 : 0);
  3127. }
  3128. if (machine->compat_props) {
  3129. qdev_prop_register_global_list(machine->compat_props);
  3130. }
  3131. qemu_add_globals();
  3132. qdev_machine_init();
  3133. machine->init(ram_size, boot_devices,
  3134. kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
  3135. cpu_synchronize_all_post_init();
  3136. set_numa_modes();
  3137. current_machine = machine;
  3138. /* init USB devices */
  3139. if (usb_enabled) {
  3140. if (foreach_device_config(DEV_USB, usb_parse) < 0)
  3141. exit(1);
  3142. }
  3143. /* init generic devices */
  3144. if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
  3145. exit(1);
  3146. net_check_clients();
  3147. /* just use the first displaystate for the moment */
  3148. ds = get_displaystate();
  3149. if (using_spice)
  3150. display_remote++;
  3151. if (display_type == DT_DEFAULT && !display_remote) {
  3152. #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
  3153. display_type = DT_SDL;
  3154. #elif defined(CONFIG_VNC)
  3155. vnc_display = "localhost:0,to=99";
  3156. show_vnc_port = 1;
  3157. #else
  3158. display_type = DT_NONE;
  3159. #endif
  3160. }
  3161. /* init local displays */
  3162. switch (display_type) {
  3163. case DT_NOGRAPHIC:
  3164. break;
  3165. #if defined(CONFIG_CURSES)
  3166. case DT_CURSES:
  3167. curses_display_init(ds, full_screen);
  3168. break;
  3169. #endif
  3170. #if defined(CONFIG_SDL)
  3171. case DT_SDL:
  3172. sdl_display_init(ds, full_screen, no_frame);
  3173. break;
  3174. #elif defined(CONFIG_COCOA)
  3175. case DT_SDL:
  3176. cocoa_display_init(ds, full_screen);
  3177. break;
  3178. #endif
  3179. default:
  3180. break;
  3181. }
  3182. /* must be after terminal init, SDL library changes signal handlers */
  3183. os_setup_signal_handling();
  3184. #ifdef CONFIG_VNC
  3185. /* init remote displays */
  3186. if (vnc_display) {
  3187. vnc_display_init(ds);
  3188. if (vnc_display_open(ds, vnc_display) < 0)
  3189. exit(1);
  3190. if (show_vnc_port) {
  3191. printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
  3192. }
  3193. }
  3194. #endif
  3195. #ifdef CONFIG_SPICE
  3196. if (using_spice && !qxl_enabled) {
  3197. qemu_spice_display_init(ds);
  3198. }
  3199. #endif
  3200. /* display setup */
  3201. dpy_resize(ds);
  3202. dcl = ds->listeners;
  3203. while (dcl != NULL) {
  3204. if (dcl->dpy_refresh != NULL) {
  3205. ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
  3206. qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
  3207. break;
  3208. }
  3209. dcl = dcl->next;
  3210. }
  3211. text_consoles_set_display(ds);
  3212. if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
  3213. exit(1);
  3214. }
  3215. qdev_machine_creation_done();
  3216. if (rom_load_all() != 0) {
  3217. fprintf(stderr, "rom loading failed\n");
  3218. exit(1);
  3219. }
  3220. /* TODO: once all bus devices are qdevified, this should be done
  3221. * when bus is created by qdev.c */
  3222. qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
  3223. qemu_run_machine_init_done_notifiers();
  3224. qemu_system_reset(VMRESET_SILENT);
  3225. if (loadvm) {
  3226. if (load_vmstate(loadvm) < 0) {
  3227. autostart = 0;
  3228. }
  3229. }
  3230. if (incoming) {
  3231. Error *errp = NULL;
  3232. int ret = qemu_start_incoming_migration(incoming, &errp);
  3233. if (ret < 0) {
  3234. if (error_is_set(&errp)) {
  3235. fprintf(stderr, "Migrate: %s\n", error_get_pretty(errp));
  3236. error_free(errp);
  3237. }
  3238. fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
  3239. incoming, ret);
  3240. exit(ret);
  3241. }
  3242. } else if (autostart) {
  3243. vm_start();
  3244. }
  3245. os_setup_post();
  3246. resume_all_vcpus();
  3247. main_loop();
  3248. bdrv_close_all();
  3249. pause_all_vcpus();
  3250. net_cleanup();
  3251. res_free();
  3252. return 0;
  3253. }