vl.c 98 KB

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