vl.c 121 KB

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