vl.c 105 KB

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