vl.c 94 KB

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