2
0

pc.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  1. /*
  2. * QEMU PC System Emulator
  3. *
  4. * Copyright (c) 2003-2004 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 "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "hw/i386/x86.h"
  27. #include "hw/i386/pc.h"
  28. #include "hw/char/serial.h"
  29. #include "hw/char/parallel.h"
  30. #include "hw/i386/apic.h"
  31. #include "hw/i386/topology.h"
  32. #include "hw/i386/fw_cfg.h"
  33. #include "sysemu/cpus.h"
  34. #include "hw/block/fdc.h"
  35. #include "hw/ide.h"
  36. #include "hw/pci/pci.h"
  37. #include "hw/pci/pci_bus.h"
  38. #include "hw/nvram/fw_cfg.h"
  39. #include "hw/timer/hpet.h"
  40. #include "hw/firmware/smbios.h"
  41. #include "hw/loader.h"
  42. #include "elf.h"
  43. #include "migration/vmstate.h"
  44. #include "multiboot.h"
  45. #include "hw/rtc/mc146818rtc.h"
  46. #include "hw/dma/i8257.h"
  47. #include "hw/timer/i8254.h"
  48. #include "hw/input/i8042.h"
  49. #include "hw/irq.h"
  50. #include "hw/audio/pcspk.h"
  51. #include "hw/pci/msi.h"
  52. #include "hw/sysbus.h"
  53. #include "sysemu/sysemu.h"
  54. #include "sysemu/tcg.h"
  55. #include "sysemu/numa.h"
  56. #include "sysemu/kvm.h"
  57. #include "sysemu/qtest.h"
  58. #include "sysemu/reset.h"
  59. #include "sysemu/runstate.h"
  60. #include "kvm_i386.h"
  61. #include "hw/xen/xen.h"
  62. #include "hw/xen/start_info.h"
  63. #include "ui/qemu-spice.h"
  64. #include "exec/memory.h"
  65. #include "exec/address-spaces.h"
  66. #include "sysemu/arch_init.h"
  67. #include "qemu/bitmap.h"
  68. #include "qemu/config-file.h"
  69. #include "qemu/error-report.h"
  70. #include "qemu/option.h"
  71. #include "qemu/cutils.h"
  72. #include "hw/acpi/acpi.h"
  73. #include "hw/acpi/cpu_hotplug.h"
  74. #include "hw/boards.h"
  75. #include "acpi-build.h"
  76. #include "hw/mem/pc-dimm.h"
  77. #include "qapi/error.h"
  78. #include "qapi/qapi-visit-common.h"
  79. #include "qapi/visitor.h"
  80. #include "hw/core/cpu.h"
  81. #include "hw/usb.h"
  82. #include "hw/i386/intel_iommu.h"
  83. #include "hw/net/ne2000-isa.h"
  84. #include "standard-headers/asm-x86/bootparam.h"
  85. #include "hw/virtio/virtio-pmem-pci.h"
  86. #include "hw/mem/memory-device.h"
  87. #include "sysemu/replay.h"
  88. #include "qapi/qmp/qerror.h"
  89. #include "config-devices.h"
  90. #include "e820_memory_layout.h"
  91. #include "fw_cfg.h"
  92. /* debug PC/ISA interrupts */
  93. //#define DEBUG_IRQ
  94. #ifdef DEBUG_IRQ
  95. #define DPRINTF(fmt, ...) \
  96. do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
  97. #else
  98. #define DPRINTF(fmt, ...)
  99. #endif
  100. struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
  101. GlobalProperty pc_compat_4_1[] = {};
  102. const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
  103. GlobalProperty pc_compat_4_0[] = {};
  104. const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
  105. GlobalProperty pc_compat_3_1[] = {
  106. { "intel-iommu", "dma-drain", "off" },
  107. { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
  108. { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
  109. { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
  110. { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
  111. { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
  112. { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
  113. { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
  114. { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
  115. { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
  116. { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
  117. { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
  118. { "Skylake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
  119. { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
  120. { "Skylake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
  121. { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
  122. { "Cascadelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
  123. { "Icelake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
  124. { "Icelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
  125. { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
  126. { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
  127. };
  128. const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
  129. GlobalProperty pc_compat_3_0[] = {
  130. { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
  131. { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
  132. { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
  133. };
  134. const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
  135. GlobalProperty pc_compat_2_12[] = {
  136. { TYPE_X86_CPU, "legacy-cache", "on" },
  137. { TYPE_X86_CPU, "topoext", "off" },
  138. { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
  139. { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
  140. };
  141. const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
  142. GlobalProperty pc_compat_2_11[] = {
  143. { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
  144. { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
  145. };
  146. const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
  147. GlobalProperty pc_compat_2_10[] = {
  148. { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
  149. { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
  150. { "q35-pcihost", "x-pci-hole64-fix", "off" },
  151. };
  152. const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
  153. GlobalProperty pc_compat_2_9[] = {
  154. { "mch", "extended-tseg-mbytes", "0" },
  155. };
  156. const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
  157. GlobalProperty pc_compat_2_8[] = {
  158. { TYPE_X86_CPU, "tcg-cpuid", "off" },
  159. { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
  160. { "ICH9-LPC", "x-smi-broadcast", "off" },
  161. { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
  162. { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
  163. };
  164. const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
  165. GlobalProperty pc_compat_2_7[] = {
  166. { TYPE_X86_CPU, "l3-cache", "off" },
  167. { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
  168. { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
  169. { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
  170. { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
  171. { "isa-pcspk", "migrate", "off" },
  172. };
  173. const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
  174. GlobalProperty pc_compat_2_6[] = {
  175. { TYPE_X86_CPU, "cpuid-0xb", "off" },
  176. { "vmxnet3", "romfile", "" },
  177. { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
  178. { "apic-common", "legacy-instance-id", "on", }
  179. };
  180. const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
  181. GlobalProperty pc_compat_2_5[] = {};
  182. const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
  183. GlobalProperty pc_compat_2_4[] = {
  184. PC_CPU_MODEL_IDS("2.4.0")
  185. { "Haswell-" TYPE_X86_CPU, "abm", "off" },
  186. { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
  187. { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
  188. { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
  189. { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
  190. { TYPE_X86_CPU, "check", "off" },
  191. { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
  192. { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
  193. { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
  194. { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
  195. { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
  196. { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
  197. { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
  198. { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
  199. };
  200. const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
  201. GlobalProperty pc_compat_2_3[] = {
  202. PC_CPU_MODEL_IDS("2.3.0")
  203. { TYPE_X86_CPU, "arat", "off" },
  204. { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
  205. { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
  206. { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
  207. { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
  208. { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
  209. { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
  210. { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
  211. { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  212. { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  213. { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  214. { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  215. { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  216. { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  217. { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  218. { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  219. { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  220. { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  221. { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
  222. { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
  223. };
  224. const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
  225. GlobalProperty pc_compat_2_2[] = {
  226. PC_CPU_MODEL_IDS("2.2.0")
  227. { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
  228. { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
  229. { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
  230. { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
  231. { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
  232. { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
  233. { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
  234. { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
  235. { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
  236. { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
  237. { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
  238. { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
  239. { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
  240. { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
  241. { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
  242. { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
  243. { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
  244. { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
  245. };
  246. const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
  247. GlobalProperty pc_compat_2_1[] = {
  248. PC_CPU_MODEL_IDS("2.1.0")
  249. { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
  250. { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
  251. };
  252. const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
  253. GlobalProperty pc_compat_2_0[] = {
  254. PC_CPU_MODEL_IDS("2.0.0")
  255. { "virtio-scsi-pci", "any_layout", "off" },
  256. { "PIIX4_PM", "memory-hotplug-support", "off" },
  257. { "apic", "version", "0x11" },
  258. { "nec-usb-xhci", "superspeed-ports-first", "off" },
  259. { "nec-usb-xhci", "force-pcie-endcap", "on" },
  260. { "pci-serial", "prog_if", "0" },
  261. { "pci-serial-2x", "prog_if", "0" },
  262. { "pci-serial-4x", "prog_if", "0" },
  263. { "virtio-net-pci", "guest_announce", "off" },
  264. { "ICH9-LPC", "memory-hotplug-support", "off" },
  265. { "xio3130-downstream", COMPAT_PROP_PCP, "off" },
  266. { "ioh3420", COMPAT_PROP_PCP, "off" },
  267. };
  268. const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
  269. GlobalProperty pc_compat_1_7[] = {
  270. PC_CPU_MODEL_IDS("1.7.0")
  271. { TYPE_USB_DEVICE, "msos-desc", "no" },
  272. { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
  273. { "hpet", HPET_INTCAP, "4" },
  274. };
  275. const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
  276. GlobalProperty pc_compat_1_6[] = {
  277. PC_CPU_MODEL_IDS("1.6.0")
  278. { "e1000", "mitigation", "off" },
  279. { "qemu64-" TYPE_X86_CPU, "model", "2" },
  280. { "qemu32-" TYPE_X86_CPU, "model", "3" },
  281. { "i440FX-pcihost", "short_root_bus", "1" },
  282. { "q35-pcihost", "short_root_bus", "1" },
  283. };
  284. const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
  285. GlobalProperty pc_compat_1_5[] = {
  286. PC_CPU_MODEL_IDS("1.5.0")
  287. { "Conroe-" TYPE_X86_CPU, "model", "2" },
  288. { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
  289. { "Penryn-" TYPE_X86_CPU, "model", "2" },
  290. { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
  291. { "Nehalem-" TYPE_X86_CPU, "model", "2" },
  292. { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
  293. { "virtio-net-pci", "any_layout", "off" },
  294. { TYPE_X86_CPU, "pmu", "on" },
  295. { "i440FX-pcihost", "short_root_bus", "0" },
  296. { "q35-pcihost", "short_root_bus", "0" },
  297. };
  298. const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
  299. GlobalProperty pc_compat_1_4[] = {
  300. PC_CPU_MODEL_IDS("1.4.0")
  301. { "scsi-hd", "discard_granularity", "0" },
  302. { "scsi-cd", "discard_granularity", "0" },
  303. { "scsi-disk", "discard_granularity", "0" },
  304. { "ide-hd", "discard_granularity", "0" },
  305. { "ide-cd", "discard_granularity", "0" },
  306. { "ide-drive", "discard_granularity", "0" },
  307. { "virtio-blk-pci", "discard_granularity", "0" },
  308. /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
  309. { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
  310. { "virtio-net-pci", "ctrl_guest_offloads", "off" },
  311. { "e1000", "romfile", "pxe-e1000.rom" },
  312. { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
  313. { "pcnet", "romfile", "pxe-pcnet.rom" },
  314. { "rtl8139", "romfile", "pxe-rtl8139.rom" },
  315. { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
  316. { "486-" TYPE_X86_CPU, "model", "0" },
  317. { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
  318. { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
  319. };
  320. const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
  321. void gsi_handler(void *opaque, int n, int level)
  322. {
  323. GSIState *s = opaque;
  324. DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
  325. if (n < ISA_NUM_IRQS) {
  326. qemu_set_irq(s->i8259_irq[n], level);
  327. }
  328. qemu_set_irq(s->ioapic_irq[n], level);
  329. }
  330. GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled)
  331. {
  332. GSIState *s;
  333. s = g_new0(GSIState, 1);
  334. if (kvm_ioapic_in_kernel()) {
  335. kvm_pc_setup_irq_routing(pci_enabled);
  336. *irqs = qemu_allocate_irqs(kvm_pc_gsi_handler, s, GSI_NUM_PINS);
  337. } else {
  338. *irqs = qemu_allocate_irqs(gsi_handler, s, GSI_NUM_PINS);
  339. }
  340. return s;
  341. }
  342. static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
  343. unsigned size)
  344. {
  345. }
  346. static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
  347. {
  348. return 0xffffffffffffffffULL;
  349. }
  350. /* MSDOS compatibility mode FPU exception support */
  351. static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
  352. unsigned size)
  353. {
  354. if (tcg_enabled()) {
  355. cpu_set_ignne();
  356. }
  357. }
  358. static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
  359. {
  360. return 0xffffffffffffffffULL;
  361. }
  362. /* TSC handling */
  363. uint64_t cpu_get_tsc(CPUX86State *env)
  364. {
  365. return cpu_get_ticks();
  366. }
  367. /* IRQ handling */
  368. int cpu_get_pic_interrupt(CPUX86State *env)
  369. {
  370. X86CPU *cpu = env_archcpu(env);
  371. int intno;
  372. if (!kvm_irqchip_in_kernel()) {
  373. intno = apic_get_interrupt(cpu->apic_state);
  374. if (intno >= 0) {
  375. return intno;
  376. }
  377. /* read the irq from the PIC */
  378. if (!apic_accept_pic_intr(cpu->apic_state)) {
  379. return -1;
  380. }
  381. }
  382. intno = pic_read_irq(isa_pic);
  383. return intno;
  384. }
  385. static void pic_irq_request(void *opaque, int irq, int level)
  386. {
  387. CPUState *cs = first_cpu;
  388. X86CPU *cpu = X86_CPU(cs);
  389. DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
  390. if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
  391. CPU_FOREACH(cs) {
  392. cpu = X86_CPU(cs);
  393. if (apic_accept_pic_intr(cpu->apic_state)) {
  394. apic_deliver_pic_intr(cpu->apic_state, level);
  395. }
  396. }
  397. } else {
  398. if (level) {
  399. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  400. } else {
  401. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  402. }
  403. }
  404. }
  405. /* PC cmos mappings */
  406. #define REG_EQUIPMENT_BYTE 0x14
  407. int cmos_get_fd_drive_type(FloppyDriveType fd0)
  408. {
  409. int val;
  410. switch (fd0) {
  411. case FLOPPY_DRIVE_TYPE_144:
  412. /* 1.44 Mb 3"5 drive */
  413. val = 4;
  414. break;
  415. case FLOPPY_DRIVE_TYPE_288:
  416. /* 2.88 Mb 3"5 drive */
  417. val = 5;
  418. break;
  419. case FLOPPY_DRIVE_TYPE_120:
  420. /* 1.2 Mb 5"5 drive */
  421. val = 2;
  422. break;
  423. case FLOPPY_DRIVE_TYPE_NONE:
  424. default:
  425. val = 0;
  426. break;
  427. }
  428. return val;
  429. }
  430. static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
  431. int16_t cylinders, int8_t heads, int8_t sectors)
  432. {
  433. rtc_set_memory(s, type_ofs, 47);
  434. rtc_set_memory(s, info_ofs, cylinders);
  435. rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
  436. rtc_set_memory(s, info_ofs + 2, heads);
  437. rtc_set_memory(s, info_ofs + 3, 0xff);
  438. rtc_set_memory(s, info_ofs + 4, 0xff);
  439. rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
  440. rtc_set_memory(s, info_ofs + 6, cylinders);
  441. rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
  442. rtc_set_memory(s, info_ofs + 8, sectors);
  443. }
  444. /* convert boot_device letter to something recognizable by the bios */
  445. static int boot_device2nibble(char boot_device)
  446. {
  447. switch(boot_device) {
  448. case 'a':
  449. case 'b':
  450. return 0x01; /* floppy boot */
  451. case 'c':
  452. return 0x02; /* hard drive boot */
  453. case 'd':
  454. return 0x03; /* CD-ROM boot */
  455. case 'n':
  456. return 0x04; /* Network boot */
  457. }
  458. return 0;
  459. }
  460. static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
  461. {
  462. #define PC_MAX_BOOT_DEVICES 3
  463. int nbds, bds[3] = { 0, };
  464. int i;
  465. nbds = strlen(boot_device);
  466. if (nbds > PC_MAX_BOOT_DEVICES) {
  467. error_setg(errp, "Too many boot devices for PC");
  468. return;
  469. }
  470. for (i = 0; i < nbds; i++) {
  471. bds[i] = boot_device2nibble(boot_device[i]);
  472. if (bds[i] == 0) {
  473. error_setg(errp, "Invalid boot device for PC: '%c'",
  474. boot_device[i]);
  475. return;
  476. }
  477. }
  478. rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
  479. rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
  480. }
  481. static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
  482. {
  483. set_boot_dev(opaque, boot_device, errp);
  484. }
  485. static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
  486. {
  487. int val, nb, i;
  488. FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
  489. FLOPPY_DRIVE_TYPE_NONE };
  490. /* floppy type */
  491. if (floppy) {
  492. for (i = 0; i < 2; i++) {
  493. fd_type[i] = isa_fdc_get_drive_type(floppy, i);
  494. }
  495. }
  496. val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
  497. cmos_get_fd_drive_type(fd_type[1]);
  498. rtc_set_memory(rtc_state, 0x10, val);
  499. val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
  500. nb = 0;
  501. if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
  502. nb++;
  503. }
  504. if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
  505. nb++;
  506. }
  507. switch (nb) {
  508. case 0:
  509. break;
  510. case 1:
  511. val |= 0x01; /* 1 drive, ready for boot */
  512. break;
  513. case 2:
  514. val |= 0x41; /* 2 drives, ready for boot */
  515. break;
  516. }
  517. rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
  518. }
  519. typedef struct pc_cmos_init_late_arg {
  520. ISADevice *rtc_state;
  521. BusState *idebus[2];
  522. } pc_cmos_init_late_arg;
  523. typedef struct check_fdc_state {
  524. ISADevice *floppy;
  525. bool multiple;
  526. } CheckFdcState;
  527. static int check_fdc(Object *obj, void *opaque)
  528. {
  529. CheckFdcState *state = opaque;
  530. Object *fdc;
  531. uint32_t iobase;
  532. Error *local_err = NULL;
  533. fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
  534. if (!fdc) {
  535. return 0;
  536. }
  537. iobase = object_property_get_uint(obj, "iobase", &local_err);
  538. if (local_err || iobase != 0x3f0) {
  539. error_free(local_err);
  540. return 0;
  541. }
  542. if (state->floppy) {
  543. state->multiple = true;
  544. } else {
  545. state->floppy = ISA_DEVICE(obj);
  546. }
  547. return 0;
  548. }
  549. static const char * const fdc_container_path[] = {
  550. "/unattached", "/peripheral", "/peripheral-anon"
  551. };
  552. /*
  553. * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
  554. * and ACPI objects.
  555. */
  556. ISADevice *pc_find_fdc0(void)
  557. {
  558. int i;
  559. Object *container;
  560. CheckFdcState state = { 0 };
  561. for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
  562. container = container_get(qdev_get_machine(), fdc_container_path[i]);
  563. object_child_foreach(container, check_fdc, &state);
  564. }
  565. if (state.multiple) {
  566. warn_report("multiple floppy disk controllers with "
  567. "iobase=0x3f0 have been found");
  568. error_printf("the one being picked for CMOS setup might not reflect "
  569. "your intent");
  570. }
  571. return state.floppy;
  572. }
  573. static void pc_cmos_init_late(void *opaque)
  574. {
  575. pc_cmos_init_late_arg *arg = opaque;
  576. ISADevice *s = arg->rtc_state;
  577. int16_t cylinders;
  578. int8_t heads, sectors;
  579. int val;
  580. int i, trans;
  581. val = 0;
  582. if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
  583. &cylinders, &heads, &sectors) >= 0) {
  584. cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
  585. val |= 0xf0;
  586. }
  587. if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
  588. &cylinders, &heads, &sectors) >= 0) {
  589. cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
  590. val |= 0x0f;
  591. }
  592. rtc_set_memory(s, 0x12, val);
  593. val = 0;
  594. for (i = 0; i < 4; i++) {
  595. /* NOTE: ide_get_geometry() returns the physical
  596. geometry. It is always such that: 1 <= sects <= 63, 1
  597. <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
  598. geometry can be different if a translation is done. */
  599. if (arg->idebus[i / 2] &&
  600. ide_get_geometry(arg->idebus[i / 2], i % 2,
  601. &cylinders, &heads, &sectors) >= 0) {
  602. trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
  603. assert((trans & ~3) == 0);
  604. val |= trans << (i * 2);
  605. }
  606. }
  607. rtc_set_memory(s, 0x39, val);
  608. pc_cmos_init_floppy(s, pc_find_fdc0());
  609. qemu_unregister_reset(pc_cmos_init_late, opaque);
  610. }
  611. void pc_cmos_init(PCMachineState *pcms,
  612. BusState *idebus0, BusState *idebus1,
  613. ISADevice *s)
  614. {
  615. int val;
  616. static pc_cmos_init_late_arg arg;
  617. X86MachineState *x86ms = X86_MACHINE(pcms);
  618. /* various important CMOS locations needed by PC/Bochs bios */
  619. /* memory size */
  620. /* base memory (first MiB) */
  621. val = MIN(x86ms->below_4g_mem_size / KiB, 640);
  622. rtc_set_memory(s, 0x15, val);
  623. rtc_set_memory(s, 0x16, val >> 8);
  624. /* extended memory (next 64MiB) */
  625. if (x86ms->below_4g_mem_size > 1 * MiB) {
  626. val = (x86ms->below_4g_mem_size - 1 * MiB) / KiB;
  627. } else {
  628. val = 0;
  629. }
  630. if (val > 65535)
  631. val = 65535;
  632. rtc_set_memory(s, 0x17, val);
  633. rtc_set_memory(s, 0x18, val >> 8);
  634. rtc_set_memory(s, 0x30, val);
  635. rtc_set_memory(s, 0x31, val >> 8);
  636. /* memory between 16MiB and 4GiB */
  637. if (x86ms->below_4g_mem_size > 16 * MiB) {
  638. val = (x86ms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
  639. } else {
  640. val = 0;
  641. }
  642. if (val > 65535)
  643. val = 65535;
  644. rtc_set_memory(s, 0x34, val);
  645. rtc_set_memory(s, 0x35, val >> 8);
  646. /* memory above 4GiB */
  647. val = x86ms->above_4g_mem_size / 65536;
  648. rtc_set_memory(s, 0x5b, val);
  649. rtc_set_memory(s, 0x5c, val >> 8);
  650. rtc_set_memory(s, 0x5d, val >> 16);
  651. object_property_add_link(OBJECT(pcms), "rtc_state",
  652. TYPE_ISA_DEVICE,
  653. (Object **)&x86ms->rtc,
  654. object_property_allow_set_link,
  655. OBJ_PROP_LINK_STRONG, &error_abort);
  656. object_property_set_link(OBJECT(pcms), OBJECT(s),
  657. "rtc_state", &error_abort);
  658. set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
  659. val = 0;
  660. val |= 0x02; /* FPU is there */
  661. val |= 0x04; /* PS/2 mouse installed */
  662. rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
  663. /* hard drives and FDC */
  664. arg.rtc_state = s;
  665. arg.idebus[0] = idebus0;
  666. arg.idebus[1] = idebus1;
  667. qemu_register_reset(pc_cmos_init_late, &arg);
  668. }
  669. #define TYPE_PORT92 "port92"
  670. #define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
  671. /* port 92 stuff: could be split off */
  672. typedef struct Port92State {
  673. ISADevice parent_obj;
  674. MemoryRegion io;
  675. uint8_t outport;
  676. qemu_irq a20_out;
  677. } Port92State;
  678. static void port92_write(void *opaque, hwaddr addr, uint64_t val,
  679. unsigned size)
  680. {
  681. Port92State *s = opaque;
  682. int oldval = s->outport;
  683. DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
  684. s->outport = val;
  685. qemu_set_irq(s->a20_out, (val >> 1) & 1);
  686. if ((val & 1) && !(oldval & 1)) {
  687. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  688. }
  689. }
  690. static uint64_t port92_read(void *opaque, hwaddr addr,
  691. unsigned size)
  692. {
  693. Port92State *s = opaque;
  694. uint32_t ret;
  695. ret = s->outport;
  696. DPRINTF("port92: read 0x%02x\n", ret);
  697. return ret;
  698. }
  699. static void port92_init(ISADevice *dev, qemu_irq a20_out)
  700. {
  701. qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out);
  702. }
  703. static const VMStateDescription vmstate_port92_isa = {
  704. .name = "port92",
  705. .version_id = 1,
  706. .minimum_version_id = 1,
  707. .fields = (VMStateField[]) {
  708. VMSTATE_UINT8(outport, Port92State),
  709. VMSTATE_END_OF_LIST()
  710. }
  711. };
  712. static void port92_reset(DeviceState *d)
  713. {
  714. Port92State *s = PORT92(d);
  715. s->outport &= ~1;
  716. }
  717. static const MemoryRegionOps port92_ops = {
  718. .read = port92_read,
  719. .write = port92_write,
  720. .impl = {
  721. .min_access_size = 1,
  722. .max_access_size = 1,
  723. },
  724. .endianness = DEVICE_LITTLE_ENDIAN,
  725. };
  726. static void port92_initfn(Object *obj)
  727. {
  728. Port92State *s = PORT92(obj);
  729. memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
  730. s->outport = 0;
  731. qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1);
  732. }
  733. static void port92_realizefn(DeviceState *dev, Error **errp)
  734. {
  735. ISADevice *isadev = ISA_DEVICE(dev);
  736. Port92State *s = PORT92(dev);
  737. isa_register_ioport(isadev, &s->io, 0x92);
  738. }
  739. static void port92_class_initfn(ObjectClass *klass, void *data)
  740. {
  741. DeviceClass *dc = DEVICE_CLASS(klass);
  742. dc->realize = port92_realizefn;
  743. dc->reset = port92_reset;
  744. dc->vmsd = &vmstate_port92_isa;
  745. /*
  746. * Reason: unlike ordinary ISA devices, this one needs additional
  747. * wiring: its A20 output line needs to be wired up by
  748. * port92_init().
  749. */
  750. dc->user_creatable = false;
  751. }
  752. static const TypeInfo port92_info = {
  753. .name = TYPE_PORT92,
  754. .parent = TYPE_ISA_DEVICE,
  755. .instance_size = sizeof(Port92State),
  756. .instance_init = port92_initfn,
  757. .class_init = port92_class_initfn,
  758. };
  759. static void port92_register_types(void)
  760. {
  761. type_register_static(&port92_info);
  762. }
  763. type_init(port92_register_types)
  764. static void handle_a20_line_change(void *opaque, int irq, int level)
  765. {
  766. X86CPU *cpu = opaque;
  767. /* XXX: send to all CPUs ? */
  768. /* XXX: add logic to handle multiple A20 line sources */
  769. x86_cpu_set_a20(cpu, level);
  770. }
  771. #define NE2000_NB_MAX 6
  772. static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
  773. 0x280, 0x380 };
  774. static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
  775. void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
  776. {
  777. static int nb_ne2k = 0;
  778. if (nb_ne2k == NE2000_NB_MAX)
  779. return;
  780. isa_ne2000_init(bus, ne2000_io[nb_ne2k],
  781. ne2000_irq[nb_ne2k], nd);
  782. nb_ne2k++;
  783. }
  784. DeviceState *cpu_get_current_apic(void)
  785. {
  786. if (current_cpu) {
  787. X86CPU *cpu = X86_CPU(current_cpu);
  788. return cpu->apic_state;
  789. } else {
  790. return NULL;
  791. }
  792. }
  793. void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
  794. {
  795. X86CPU *cpu = opaque;
  796. if (level) {
  797. cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
  798. }
  799. }
  800. /*
  801. * This function is very similar to smp_parse()
  802. * in hw/core/machine.c but includes CPU die support.
  803. */
  804. void pc_smp_parse(MachineState *ms, QemuOpts *opts)
  805. {
  806. X86MachineState *x86ms = X86_MACHINE(ms);
  807. if (opts) {
  808. unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
  809. unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
  810. unsigned dies = qemu_opt_get_number(opts, "dies", 1);
  811. unsigned cores = qemu_opt_get_number(opts, "cores", 0);
  812. unsigned threads = qemu_opt_get_number(opts, "threads", 0);
  813. /* compute missing values, prefer sockets over cores over threads */
  814. if (cpus == 0 || sockets == 0) {
  815. cores = cores > 0 ? cores : 1;
  816. threads = threads > 0 ? threads : 1;
  817. if (cpus == 0) {
  818. sockets = sockets > 0 ? sockets : 1;
  819. cpus = cores * threads * dies * sockets;
  820. } else {
  821. ms->smp.max_cpus =
  822. qemu_opt_get_number(opts, "maxcpus", cpus);
  823. sockets = ms->smp.max_cpus / (cores * threads * dies);
  824. }
  825. } else if (cores == 0) {
  826. threads = threads > 0 ? threads : 1;
  827. cores = cpus / (sockets * dies * threads);
  828. cores = cores > 0 ? cores : 1;
  829. } else if (threads == 0) {
  830. threads = cpus / (cores * dies * sockets);
  831. threads = threads > 0 ? threads : 1;
  832. } else if (sockets * dies * cores * threads < cpus) {
  833. error_report("cpu topology: "
  834. "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
  835. "smp_cpus (%u)",
  836. sockets, dies, cores, threads, cpus);
  837. exit(1);
  838. }
  839. ms->smp.max_cpus =
  840. qemu_opt_get_number(opts, "maxcpus", cpus);
  841. if (ms->smp.max_cpus < cpus) {
  842. error_report("maxcpus must be equal to or greater than smp");
  843. exit(1);
  844. }
  845. if (sockets * dies * cores * threads > ms->smp.max_cpus) {
  846. error_report("cpu topology: "
  847. "sockets (%u) * dies (%u) * cores (%u) * threads (%u) > "
  848. "maxcpus (%u)",
  849. sockets, dies, cores, threads,
  850. ms->smp.max_cpus);
  851. exit(1);
  852. }
  853. if (sockets * dies * cores * threads != ms->smp.max_cpus) {
  854. warn_report("Invalid CPU topology deprecated: "
  855. "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
  856. "!= maxcpus (%u)",
  857. sockets, dies, cores, threads,
  858. ms->smp.max_cpus);
  859. }
  860. ms->smp.cpus = cpus;
  861. ms->smp.cores = cores;
  862. ms->smp.threads = threads;
  863. x86ms->smp_dies = dies;
  864. }
  865. if (ms->smp.cpus > 1) {
  866. Error *blocker = NULL;
  867. error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
  868. replay_add_blocker(blocker);
  869. }
  870. }
  871. void pc_hot_add_cpu(MachineState *ms, const int64_t id, Error **errp)
  872. {
  873. X86MachineState *x86ms = X86_MACHINE(ms);
  874. int64_t apic_id = x86_cpu_apic_id_from_index(x86ms, id);
  875. Error *local_err = NULL;
  876. if (id < 0) {
  877. error_setg(errp, "Invalid CPU id: %" PRIi64, id);
  878. return;
  879. }
  880. if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
  881. error_setg(errp, "Unable to add CPU: %" PRIi64
  882. ", resulting APIC ID (%" PRIi64 ") is too large",
  883. id, apic_id);
  884. return;
  885. }
  886. x86_cpu_new(X86_MACHINE(ms), apic_id, &local_err);
  887. if (local_err) {
  888. error_propagate(errp, local_err);
  889. return;
  890. }
  891. }
  892. static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
  893. {
  894. if (cpus_count > 0xff) {
  895. /* If the number of CPUs can't be represented in 8 bits, the
  896. * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
  897. * to make old BIOSes fail more predictably.
  898. */
  899. rtc_set_memory(rtc, 0x5f, 0);
  900. } else {
  901. rtc_set_memory(rtc, 0x5f, cpus_count - 1);
  902. }
  903. }
  904. static
  905. void pc_machine_done(Notifier *notifier, void *data)
  906. {
  907. PCMachineState *pcms = container_of(notifier,
  908. PCMachineState, machine_done);
  909. X86MachineState *x86ms = X86_MACHINE(pcms);
  910. PCIBus *bus = pcms->bus;
  911. /* set the number of CPUs */
  912. rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
  913. if (bus) {
  914. int extra_hosts = 0;
  915. QLIST_FOREACH(bus, &bus->child, sibling) {
  916. /* look for expander root buses */
  917. if (pci_bus_is_root(bus)) {
  918. extra_hosts++;
  919. }
  920. }
  921. if (extra_hosts && x86ms->fw_cfg) {
  922. uint64_t *val = g_malloc(sizeof(*val));
  923. *val = cpu_to_le64(extra_hosts);
  924. fw_cfg_add_file(x86ms->fw_cfg,
  925. "etc/extra-pci-roots", val, sizeof(*val));
  926. }
  927. }
  928. acpi_setup();
  929. if (x86ms->fw_cfg) {
  930. fw_cfg_build_smbios(MACHINE(pcms), x86ms->fw_cfg);
  931. fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg);
  932. /* update FW_CFG_NB_CPUS to account for -device added CPUs */
  933. fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  934. }
  935. if (x86ms->apic_id_limit > 255 && !xen_enabled()) {
  936. IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
  937. if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) ||
  938. iommu->intr_eim != ON_OFF_AUTO_ON) {
  939. error_report("current -smp configuration requires "
  940. "Extended Interrupt Mode enabled. "
  941. "You can add an IOMMU using: "
  942. "-device intel-iommu,intremap=on,eim=on");
  943. exit(EXIT_FAILURE);
  944. }
  945. }
  946. }
  947. void pc_guest_info_init(PCMachineState *pcms)
  948. {
  949. int i;
  950. MachineState *ms = MACHINE(pcms);
  951. X86MachineState *x86ms = X86_MACHINE(pcms);
  952. x86ms->apic_xrupt_override = kvm_allows_irq0_override();
  953. pcms->numa_nodes = ms->numa_state->num_nodes;
  954. pcms->node_mem = g_malloc0(pcms->numa_nodes *
  955. sizeof *pcms->node_mem);
  956. for (i = 0; i < ms->numa_state->num_nodes; i++) {
  957. pcms->node_mem[i] = ms->numa_state->nodes[i].node_mem;
  958. }
  959. pcms->machine_done.notify = pc_machine_done;
  960. qemu_add_machine_init_done_notifier(&pcms->machine_done);
  961. }
  962. /* setup pci memory address space mapping into system address space */
  963. void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
  964. MemoryRegion *pci_address_space)
  965. {
  966. /* Set to lower priority than RAM */
  967. memory_region_add_subregion_overlap(system_memory, 0x0,
  968. pci_address_space, -1);
  969. }
  970. void xen_load_linux(PCMachineState *pcms)
  971. {
  972. int i;
  973. FWCfgState *fw_cfg;
  974. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  975. X86MachineState *x86ms = X86_MACHINE(pcms);
  976. assert(MACHINE(pcms)->kernel_filename != NULL);
  977. fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
  978. fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  979. rom_set_fw(fw_cfg);
  980. x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
  981. pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled);
  982. for (i = 0; i < nb_option_roms; i++) {
  983. assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
  984. !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
  985. !strcmp(option_rom[i].name, "pvh.bin") ||
  986. !strcmp(option_rom[i].name, "multiboot.bin"));
  987. rom_add_option(option_rom[i].name, option_rom[i].bootindex);
  988. }
  989. x86ms->fw_cfg = fw_cfg;
  990. }
  991. void pc_memory_init(PCMachineState *pcms,
  992. MemoryRegion *system_memory,
  993. MemoryRegion *rom_memory,
  994. MemoryRegion **ram_memory)
  995. {
  996. int linux_boot, i;
  997. MemoryRegion *ram, *option_rom_mr;
  998. MemoryRegion *ram_below_4g, *ram_above_4g;
  999. FWCfgState *fw_cfg;
  1000. MachineState *machine = MACHINE(pcms);
  1001. MachineClass *mc = MACHINE_GET_CLASS(machine);
  1002. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  1003. X86MachineState *x86ms = X86_MACHINE(pcms);
  1004. assert(machine->ram_size == x86ms->below_4g_mem_size +
  1005. x86ms->above_4g_mem_size);
  1006. linux_boot = (machine->kernel_filename != NULL);
  1007. /* Allocate RAM. We allocate it as a single memory region and use
  1008. * aliases to address portions of it, mostly for backwards compatibility
  1009. * with older qemus that used qemu_ram_alloc().
  1010. */
  1011. ram = g_malloc(sizeof(*ram));
  1012. memory_region_allocate_system_memory(ram, NULL, "pc.ram",
  1013. machine->ram_size);
  1014. *ram_memory = ram;
  1015. ram_below_4g = g_malloc(sizeof(*ram_below_4g));
  1016. memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
  1017. 0, x86ms->below_4g_mem_size);
  1018. memory_region_add_subregion(system_memory, 0, ram_below_4g);
  1019. e820_add_entry(0, x86ms->below_4g_mem_size, E820_RAM);
  1020. if (x86ms->above_4g_mem_size > 0) {
  1021. ram_above_4g = g_malloc(sizeof(*ram_above_4g));
  1022. memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
  1023. x86ms->below_4g_mem_size,
  1024. x86ms->above_4g_mem_size);
  1025. memory_region_add_subregion(system_memory, 0x100000000ULL,
  1026. ram_above_4g);
  1027. e820_add_entry(0x100000000ULL, x86ms->above_4g_mem_size, E820_RAM);
  1028. }
  1029. if (!pcmc->has_reserved_memory &&
  1030. (machine->ram_slots ||
  1031. (machine->maxram_size > machine->ram_size))) {
  1032. error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
  1033. mc->name);
  1034. exit(EXIT_FAILURE);
  1035. }
  1036. /* always allocate the device memory information */
  1037. machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
  1038. /* initialize device memory address space */
  1039. if (pcmc->has_reserved_memory &&
  1040. (machine->ram_size < machine->maxram_size)) {
  1041. ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
  1042. if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
  1043. error_report("unsupported amount of memory slots: %"PRIu64,
  1044. machine->ram_slots);
  1045. exit(EXIT_FAILURE);
  1046. }
  1047. if (QEMU_ALIGN_UP(machine->maxram_size,
  1048. TARGET_PAGE_SIZE) != machine->maxram_size) {
  1049. error_report("maximum memory size must by aligned to multiple of "
  1050. "%d bytes", TARGET_PAGE_SIZE);
  1051. exit(EXIT_FAILURE);
  1052. }
  1053. machine->device_memory->base =
  1054. ROUND_UP(0x100000000ULL + x86ms->above_4g_mem_size, 1 * GiB);
  1055. if (pcmc->enforce_aligned_dimm) {
  1056. /* size device region assuming 1G page max alignment per slot */
  1057. device_mem_size += (1 * GiB) * machine->ram_slots;
  1058. }
  1059. if ((machine->device_memory->base + device_mem_size) <
  1060. device_mem_size) {
  1061. error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
  1062. machine->maxram_size);
  1063. exit(EXIT_FAILURE);
  1064. }
  1065. memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
  1066. "device-memory", device_mem_size);
  1067. memory_region_add_subregion(system_memory, machine->device_memory->base,
  1068. &machine->device_memory->mr);
  1069. }
  1070. /* Initialize PC system firmware */
  1071. pc_system_firmware_init(pcms, rom_memory);
  1072. option_rom_mr = g_malloc(sizeof(*option_rom_mr));
  1073. memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
  1074. &error_fatal);
  1075. if (pcmc->pci_enabled) {
  1076. memory_region_set_readonly(option_rom_mr, true);
  1077. }
  1078. memory_region_add_subregion_overlap(rom_memory,
  1079. PC_ROM_MIN_VGA,
  1080. option_rom_mr,
  1081. 1);
  1082. fw_cfg = fw_cfg_arch_create(machine,
  1083. x86ms->boot_cpus, x86ms->apic_id_limit);
  1084. rom_set_fw(fw_cfg);
  1085. if (pcmc->has_reserved_memory && machine->device_memory->base) {
  1086. uint64_t *val = g_malloc(sizeof(*val));
  1087. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  1088. uint64_t res_mem_end = machine->device_memory->base;
  1089. if (!pcmc->broken_reserved_end) {
  1090. res_mem_end += memory_region_size(&machine->device_memory->mr);
  1091. }
  1092. *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
  1093. fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
  1094. }
  1095. if (linux_boot) {
  1096. x86_load_linux(x86ms, fw_cfg, pcmc->acpi_data_size,
  1097. pcmc->pvh_enabled, pcmc->linuxboot_dma_enabled);
  1098. }
  1099. for (i = 0; i < nb_option_roms; i++) {
  1100. rom_add_option(option_rom[i].name, option_rom[i].bootindex);
  1101. }
  1102. x86ms->fw_cfg = fw_cfg;
  1103. /* Init default IOAPIC address space */
  1104. x86ms->ioapic_as = &address_space_memory;
  1105. /* Init ACPI memory hotplug IO base address */
  1106. pcms->memhp_io_base = ACPI_MEMORY_HOTPLUG_BASE;
  1107. }
  1108. /*
  1109. * The 64bit pci hole starts after "above 4G RAM" and
  1110. * potentially the space reserved for memory hotplug.
  1111. */
  1112. uint64_t pc_pci_hole64_start(void)
  1113. {
  1114. PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
  1115. PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  1116. MachineState *ms = MACHINE(pcms);
  1117. X86MachineState *x86ms = X86_MACHINE(pcms);
  1118. uint64_t hole64_start = 0;
  1119. if (pcmc->has_reserved_memory && ms->device_memory->base) {
  1120. hole64_start = ms->device_memory->base;
  1121. if (!pcmc->broken_reserved_end) {
  1122. hole64_start += memory_region_size(&ms->device_memory->mr);
  1123. }
  1124. } else {
  1125. hole64_start = 0x100000000ULL + x86ms->above_4g_mem_size;
  1126. }
  1127. return ROUND_UP(hole64_start, 1 * GiB);
  1128. }
  1129. qemu_irq pc_allocate_cpu_irq(void)
  1130. {
  1131. return qemu_allocate_irq(pic_irq_request, NULL, 0);
  1132. }
  1133. DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
  1134. {
  1135. DeviceState *dev = NULL;
  1136. rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
  1137. if (pci_bus) {
  1138. PCIDevice *pcidev = pci_vga_init(pci_bus);
  1139. dev = pcidev ? &pcidev->qdev : NULL;
  1140. } else if (isa_bus) {
  1141. ISADevice *isadev = isa_vga_init(isa_bus);
  1142. dev = isadev ? DEVICE(isadev) : NULL;
  1143. }
  1144. rom_reset_order_override();
  1145. return dev;
  1146. }
  1147. static const MemoryRegionOps ioport80_io_ops = {
  1148. .write = ioport80_write,
  1149. .read = ioport80_read,
  1150. .endianness = DEVICE_NATIVE_ENDIAN,
  1151. .impl = {
  1152. .min_access_size = 1,
  1153. .max_access_size = 1,
  1154. },
  1155. };
  1156. static const MemoryRegionOps ioportF0_io_ops = {
  1157. .write = ioportF0_write,
  1158. .read = ioportF0_read,
  1159. .endianness = DEVICE_NATIVE_ENDIAN,
  1160. .impl = {
  1161. .min_access_size = 1,
  1162. .max_access_size = 1,
  1163. },
  1164. };
  1165. static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
  1166. {
  1167. int i;
  1168. DriveInfo *fd[MAX_FD];
  1169. qemu_irq *a20_line;
  1170. ISADevice *i8042, *port92, *vmmouse;
  1171. serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
  1172. parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
  1173. for (i = 0; i < MAX_FD; i++) {
  1174. fd[i] = drive_get(IF_FLOPPY, 0, i);
  1175. create_fdctrl |= !!fd[i];
  1176. }
  1177. if (create_fdctrl) {
  1178. fdctrl_init_isa(isa_bus, fd);
  1179. }
  1180. i8042 = isa_create_simple(isa_bus, "i8042");
  1181. if (!no_vmport) {
  1182. vmport_init(isa_bus);
  1183. vmmouse = isa_try_create(isa_bus, "vmmouse");
  1184. } else {
  1185. vmmouse = NULL;
  1186. }
  1187. if (vmmouse) {
  1188. DeviceState *dev = DEVICE(vmmouse);
  1189. qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
  1190. qdev_init_nofail(dev);
  1191. }
  1192. port92 = isa_create_simple(isa_bus, "port92");
  1193. a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
  1194. i8042_setup_a20_line(i8042, a20_line[0]);
  1195. port92_init(port92, a20_line[1]);
  1196. g_free(a20_line);
  1197. }
  1198. void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
  1199. ISADevice **rtc_state,
  1200. bool create_fdctrl,
  1201. bool no_vmport,
  1202. bool has_pit,
  1203. uint32_t hpet_irqs)
  1204. {
  1205. int i;
  1206. DeviceState *hpet = NULL;
  1207. int pit_isa_irq = 0;
  1208. qemu_irq pit_alt_irq = NULL;
  1209. qemu_irq rtc_irq = NULL;
  1210. ISADevice *pit = NULL;
  1211. MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
  1212. MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
  1213. memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
  1214. memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
  1215. memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
  1216. memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
  1217. /*
  1218. * Check if an HPET shall be created.
  1219. *
  1220. * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
  1221. * when the HPET wants to take over. Thus we have to disable the latter.
  1222. */
  1223. if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
  1224. /* In order to set property, here not using sysbus_try_create_simple */
  1225. hpet = qdev_try_create(NULL, TYPE_HPET);
  1226. if (hpet) {
  1227. /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
  1228. * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
  1229. * IRQ8 and IRQ2.
  1230. */
  1231. uint8_t compat = object_property_get_uint(OBJECT(hpet),
  1232. HPET_INTCAP, NULL);
  1233. if (!compat) {
  1234. qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
  1235. }
  1236. qdev_init_nofail(hpet);
  1237. sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
  1238. for (i = 0; i < GSI_NUM_PINS; i++) {
  1239. sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
  1240. }
  1241. pit_isa_irq = -1;
  1242. pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
  1243. rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
  1244. }
  1245. }
  1246. *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
  1247. qemu_register_boot_set(pc_boot_set, *rtc_state);
  1248. if (!xen_enabled() && has_pit) {
  1249. if (kvm_pit_in_kernel()) {
  1250. pit = kvm_pit_init(isa_bus, 0x40);
  1251. } else {
  1252. pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
  1253. }
  1254. if (hpet) {
  1255. /* connect PIT to output control line of the HPET */
  1256. qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
  1257. }
  1258. pcspk_init(isa_bus, pit);
  1259. }
  1260. i8257_dma_init(isa_bus, 0);
  1261. /* Super I/O */
  1262. pc_superio_init(isa_bus, create_fdctrl, no_vmport);
  1263. }
  1264. void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
  1265. {
  1266. int i;
  1267. rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
  1268. for (i = 0; i < nb_nics; i++) {
  1269. NICInfo *nd = &nd_table[i];
  1270. const char *model = nd->model ? nd->model : pcmc->default_nic_model;
  1271. if (g_str_equal(model, "ne2k_isa")) {
  1272. pc_init_ne2k_isa(isa_bus, nd);
  1273. } else {
  1274. pci_nic_init_nofail(nd, pci_bus, model, NULL);
  1275. }
  1276. }
  1277. rom_reset_order_override();
  1278. }
  1279. void pc_i8259_create(ISABus *isa_bus, qemu_irq *i8259_irqs)
  1280. {
  1281. qemu_irq *i8259;
  1282. if (kvm_pic_in_kernel()) {
  1283. i8259 = kvm_i8259_init(isa_bus);
  1284. } else if (xen_enabled()) {
  1285. i8259 = xen_interrupt_controller_init();
  1286. } else {
  1287. i8259 = i8259_init(isa_bus, pc_allocate_cpu_irq());
  1288. }
  1289. for (size_t i = 0; i < ISA_NUM_IRQS; i++) {
  1290. i8259_irqs[i] = i8259[i];
  1291. }
  1292. g_free(i8259);
  1293. }
  1294. void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
  1295. {
  1296. DeviceState *dev;
  1297. SysBusDevice *d;
  1298. unsigned int i;
  1299. if (kvm_ioapic_in_kernel()) {
  1300. dev = qdev_create(NULL, TYPE_KVM_IOAPIC);
  1301. } else {
  1302. dev = qdev_create(NULL, TYPE_IOAPIC);
  1303. }
  1304. if (parent_name) {
  1305. object_property_add_child(object_resolve_path(parent_name, NULL),
  1306. "ioapic", OBJECT(dev), NULL);
  1307. }
  1308. qdev_init_nofail(dev);
  1309. d = SYS_BUS_DEVICE(dev);
  1310. sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
  1311. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  1312. gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
  1313. }
  1314. }
  1315. static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
  1316. Error **errp)
  1317. {
  1318. const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1319. const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
  1320. const MachineState *ms = MACHINE(hotplug_dev);
  1321. const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  1322. const uint64_t legacy_align = TARGET_PAGE_SIZE;
  1323. Error *local_err = NULL;
  1324. /*
  1325. * When -no-acpi is used with Q35 machine type, no ACPI is built,
  1326. * but pcms->acpi_dev is still created. Check !acpi_enabled in
  1327. * addition to cover this case.
  1328. */
  1329. if (!pcms->acpi_dev || !acpi_enabled) {
  1330. error_setg(errp,
  1331. "memory hotplug is not enabled: missing acpi device or acpi disabled");
  1332. return;
  1333. }
  1334. if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
  1335. error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
  1336. return;
  1337. }
  1338. hotplug_handler_pre_plug(pcms->acpi_dev, dev, &local_err);
  1339. if (local_err) {
  1340. error_propagate(errp, local_err);
  1341. return;
  1342. }
  1343. pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
  1344. pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
  1345. }
  1346. static void pc_memory_plug(HotplugHandler *hotplug_dev,
  1347. DeviceState *dev, Error **errp)
  1348. {
  1349. Error *local_err = NULL;
  1350. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1351. MachineState *ms = MACHINE(hotplug_dev);
  1352. bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
  1353. pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err);
  1354. if (local_err) {
  1355. goto out;
  1356. }
  1357. if (is_nvdimm) {
  1358. nvdimm_plug(ms->nvdimms_state);
  1359. }
  1360. hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort);
  1361. out:
  1362. error_propagate(errp, local_err);
  1363. }
  1364. static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
  1365. DeviceState *dev, Error **errp)
  1366. {
  1367. Error *local_err = NULL;
  1368. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1369. /*
  1370. * When -no-acpi is used with Q35 machine type, no ACPI is built,
  1371. * but pcms->acpi_dev is still created. Check !acpi_enabled in
  1372. * addition to cover this case.
  1373. */
  1374. if (!pcms->acpi_dev || !acpi_enabled) {
  1375. error_setg(&local_err,
  1376. "memory hotplug is not enabled: missing acpi device or acpi disabled");
  1377. goto out;
  1378. }
  1379. if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
  1380. error_setg(&local_err,
  1381. "nvdimm device hot unplug is not supported yet.");
  1382. goto out;
  1383. }
  1384. hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
  1385. &local_err);
  1386. out:
  1387. error_propagate(errp, local_err);
  1388. }
  1389. static void pc_memory_unplug(HotplugHandler *hotplug_dev,
  1390. DeviceState *dev, Error **errp)
  1391. {
  1392. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1393. Error *local_err = NULL;
  1394. hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
  1395. if (local_err) {
  1396. goto out;
  1397. }
  1398. pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
  1399. object_property_set_bool(OBJECT(dev), false, "realized", NULL);
  1400. out:
  1401. error_propagate(errp, local_err);
  1402. }
  1403. static int pc_apic_cmp(const void *a, const void *b)
  1404. {
  1405. CPUArchId *apic_a = (CPUArchId *)a;
  1406. CPUArchId *apic_b = (CPUArchId *)b;
  1407. return apic_a->arch_id - apic_b->arch_id;
  1408. }
  1409. /* returns pointer to CPUArchId descriptor that matches CPU's apic_id
  1410. * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
  1411. * entry corresponding to CPU's apic_id returns NULL.
  1412. */
  1413. static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
  1414. {
  1415. CPUArchId apic_id, *found_cpu;
  1416. apic_id.arch_id = id;
  1417. found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
  1418. ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
  1419. pc_apic_cmp);
  1420. if (found_cpu && idx) {
  1421. *idx = found_cpu - ms->possible_cpus->cpus;
  1422. }
  1423. return found_cpu;
  1424. }
  1425. static void pc_cpu_plug(HotplugHandler *hotplug_dev,
  1426. DeviceState *dev, Error **errp)
  1427. {
  1428. CPUArchId *found_cpu;
  1429. Error *local_err = NULL;
  1430. X86CPU *cpu = X86_CPU(dev);
  1431. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1432. X86MachineState *x86ms = X86_MACHINE(pcms);
  1433. if (pcms->acpi_dev) {
  1434. hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
  1435. if (local_err) {
  1436. goto out;
  1437. }
  1438. }
  1439. /* increment the number of CPUs */
  1440. x86ms->boot_cpus++;
  1441. if (x86ms->rtc) {
  1442. rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
  1443. }
  1444. if (x86ms->fw_cfg) {
  1445. fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  1446. }
  1447. found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
  1448. found_cpu->cpu = OBJECT(dev);
  1449. out:
  1450. error_propagate(errp, local_err);
  1451. }
  1452. static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
  1453. DeviceState *dev, Error **errp)
  1454. {
  1455. int idx = -1;
  1456. Error *local_err = NULL;
  1457. X86CPU *cpu = X86_CPU(dev);
  1458. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1459. if (!pcms->acpi_dev) {
  1460. error_setg(&local_err, "CPU hot unplug not supported without ACPI");
  1461. goto out;
  1462. }
  1463. pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
  1464. assert(idx != -1);
  1465. if (idx == 0) {
  1466. error_setg(&local_err, "Boot CPU is unpluggable");
  1467. goto out;
  1468. }
  1469. hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
  1470. &local_err);
  1471. if (local_err) {
  1472. goto out;
  1473. }
  1474. out:
  1475. error_propagate(errp, local_err);
  1476. }
  1477. static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
  1478. DeviceState *dev, Error **errp)
  1479. {
  1480. CPUArchId *found_cpu;
  1481. Error *local_err = NULL;
  1482. X86CPU *cpu = X86_CPU(dev);
  1483. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1484. X86MachineState *x86ms = X86_MACHINE(pcms);
  1485. hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
  1486. if (local_err) {
  1487. goto out;
  1488. }
  1489. found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
  1490. found_cpu->cpu = NULL;
  1491. object_property_set_bool(OBJECT(dev), false, "realized", NULL);
  1492. /* decrement the number of CPUs */
  1493. x86ms->boot_cpus--;
  1494. /* Update the number of CPUs in CMOS */
  1495. rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
  1496. fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  1497. out:
  1498. error_propagate(errp, local_err);
  1499. }
  1500. static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
  1501. DeviceState *dev, Error **errp)
  1502. {
  1503. int idx;
  1504. CPUState *cs;
  1505. CPUArchId *cpu_slot;
  1506. X86CPUTopoInfo topo;
  1507. X86CPU *cpu = X86_CPU(dev);
  1508. CPUX86State *env = &cpu->env;
  1509. MachineState *ms = MACHINE(hotplug_dev);
  1510. PCMachineState *pcms = PC_MACHINE(hotplug_dev);
  1511. X86MachineState *x86ms = X86_MACHINE(pcms);
  1512. unsigned int smp_cores = ms->smp.cores;
  1513. unsigned int smp_threads = ms->smp.threads;
  1514. if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
  1515. error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
  1516. ms->cpu_type);
  1517. return;
  1518. }
  1519. env->nr_dies = x86ms->smp_dies;
  1520. /*
  1521. * If APIC ID is not set,
  1522. * set it based on socket/die/core/thread properties.
  1523. */
  1524. if (cpu->apic_id == UNASSIGNED_APIC_ID) {
  1525. int max_socket = (ms->smp.max_cpus - 1) /
  1526. smp_threads / smp_cores / x86ms->smp_dies;
  1527. /*
  1528. * die-id was optional in QEMU 4.0 and older, so keep it optional
  1529. * if there's only one die per socket.
  1530. */
  1531. if (cpu->die_id < 0 && x86ms->smp_dies == 1) {
  1532. cpu->die_id = 0;
  1533. }
  1534. if (cpu->socket_id < 0) {
  1535. error_setg(errp, "CPU socket-id is not set");
  1536. return;
  1537. } else if (cpu->socket_id > max_socket) {
  1538. error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
  1539. cpu->socket_id, max_socket);
  1540. return;
  1541. }
  1542. if (cpu->die_id < 0) {
  1543. error_setg(errp, "CPU die-id is not set");
  1544. return;
  1545. } else if (cpu->die_id > x86ms->smp_dies - 1) {
  1546. error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
  1547. cpu->die_id, x86ms->smp_dies - 1);
  1548. return;
  1549. }
  1550. if (cpu->core_id < 0) {
  1551. error_setg(errp, "CPU core-id is not set");
  1552. return;
  1553. } else if (cpu->core_id > (smp_cores - 1)) {
  1554. error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
  1555. cpu->core_id, smp_cores - 1);
  1556. return;
  1557. }
  1558. if (cpu->thread_id < 0) {
  1559. error_setg(errp, "CPU thread-id is not set");
  1560. return;
  1561. } else if (cpu->thread_id > (smp_threads - 1)) {
  1562. error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
  1563. cpu->thread_id, smp_threads - 1);
  1564. return;
  1565. }
  1566. topo.pkg_id = cpu->socket_id;
  1567. topo.die_id = cpu->die_id;
  1568. topo.core_id = cpu->core_id;
  1569. topo.smt_id = cpu->thread_id;
  1570. cpu->apic_id = apicid_from_topo_ids(x86ms->smp_dies, smp_cores,
  1571. smp_threads, &topo);
  1572. }
  1573. cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
  1574. if (!cpu_slot) {
  1575. MachineState *ms = MACHINE(pcms);
  1576. x86_topo_ids_from_apicid(cpu->apic_id, x86ms->smp_dies,
  1577. smp_cores, smp_threads, &topo);
  1578. error_setg(errp,
  1579. "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
  1580. " APIC ID %" PRIu32 ", valid index range 0:%d",
  1581. topo.pkg_id, topo.die_id, topo.core_id, topo.smt_id,
  1582. cpu->apic_id, ms->possible_cpus->len - 1);
  1583. return;
  1584. }
  1585. if (cpu_slot->cpu) {
  1586. error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
  1587. idx, cpu->apic_id);
  1588. return;
  1589. }
  1590. /* if 'address' properties socket-id/core-id/thread-id are not set, set them
  1591. * so that machine_query_hotpluggable_cpus would show correct values
  1592. */
  1593. /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
  1594. * once -smp refactoring is complete and there will be CPU private
  1595. * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
  1596. x86_topo_ids_from_apicid(cpu->apic_id, x86ms->smp_dies,
  1597. smp_cores, smp_threads, &topo);
  1598. if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) {
  1599. error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
  1600. " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id);
  1601. return;
  1602. }
  1603. cpu->socket_id = topo.pkg_id;
  1604. if (cpu->die_id != -1 && cpu->die_id != topo.die_id) {
  1605. error_setg(errp, "property die-id: %u doesn't match set apic-id:"
  1606. " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo.die_id);
  1607. return;
  1608. }
  1609. cpu->die_id = topo.die_id;
  1610. if (cpu->core_id != -1 && cpu->core_id != topo.core_id) {
  1611. error_setg(errp, "property core-id: %u doesn't match set apic-id:"
  1612. " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id);
  1613. return;
  1614. }
  1615. cpu->core_id = topo.core_id;
  1616. if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) {
  1617. error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
  1618. " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id);
  1619. return;
  1620. }
  1621. cpu->thread_id = topo.smt_id;
  1622. if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
  1623. !kvm_hv_vpindex_settable()) {
  1624. error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
  1625. return;
  1626. }
  1627. cs = CPU(cpu);
  1628. cs->cpu_index = idx;
  1629. numa_cpu_pre_plug(cpu_slot, dev, errp);
  1630. }
  1631. static void pc_virtio_pmem_pci_pre_plug(HotplugHandler *hotplug_dev,
  1632. DeviceState *dev, Error **errp)
  1633. {
  1634. HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
  1635. Error *local_err = NULL;
  1636. if (!hotplug_dev2) {
  1637. /*
  1638. * Without a bus hotplug handler, we cannot control the plug/unplug
  1639. * order. This should never be the case on x86, however better add
  1640. * a safety net.
  1641. */
  1642. error_setg(errp, "virtio-pmem-pci not supported on this bus.");
  1643. return;
  1644. }
  1645. /*
  1646. * First, see if we can plug this memory device at all. If that
  1647. * succeeds, branch of to the actual hotplug handler.
  1648. */
  1649. memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
  1650. &local_err);
  1651. if (!local_err) {
  1652. hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err);
  1653. }
  1654. error_propagate(errp, local_err);
  1655. }
  1656. static void pc_virtio_pmem_pci_plug(HotplugHandler *hotplug_dev,
  1657. DeviceState *dev, Error **errp)
  1658. {
  1659. HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
  1660. Error *local_err = NULL;
  1661. /*
  1662. * Plug the memory device first and then branch off to the actual
  1663. * hotplug handler. If that one fails, we can easily undo the memory
  1664. * device bits.
  1665. */
  1666. memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
  1667. hotplug_handler_plug(hotplug_dev2, dev, &local_err);
  1668. if (local_err) {
  1669. memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
  1670. }
  1671. error_propagate(errp, local_err);
  1672. }
  1673. static void pc_virtio_pmem_pci_unplug_request(HotplugHandler *hotplug_dev,
  1674. DeviceState *dev, Error **errp)
  1675. {
  1676. /* We don't support virtio pmem hot unplug */
  1677. error_setg(errp, "virtio pmem device unplug not supported.");
  1678. }
  1679. static void pc_virtio_pmem_pci_unplug(HotplugHandler *hotplug_dev,
  1680. DeviceState *dev, Error **errp)
  1681. {
  1682. /* We don't support virtio pmem hot unplug */
  1683. }
  1684. static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
  1685. DeviceState *dev, Error **errp)
  1686. {
  1687. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1688. pc_memory_pre_plug(hotplug_dev, dev, errp);
  1689. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1690. pc_cpu_pre_plug(hotplug_dev, dev, errp);
  1691. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
  1692. pc_virtio_pmem_pci_pre_plug(hotplug_dev, dev, errp);
  1693. }
  1694. }
  1695. static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
  1696. DeviceState *dev, Error **errp)
  1697. {
  1698. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1699. pc_memory_plug(hotplug_dev, dev, errp);
  1700. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1701. pc_cpu_plug(hotplug_dev, dev, errp);
  1702. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
  1703. pc_virtio_pmem_pci_plug(hotplug_dev, dev, errp);
  1704. }
  1705. }
  1706. static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
  1707. DeviceState *dev, Error **errp)
  1708. {
  1709. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1710. pc_memory_unplug_request(hotplug_dev, dev, errp);
  1711. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1712. pc_cpu_unplug_request_cb(hotplug_dev, dev, errp);
  1713. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
  1714. pc_virtio_pmem_pci_unplug_request(hotplug_dev, dev, errp);
  1715. } else {
  1716. error_setg(errp, "acpi: device unplug request for not supported device"
  1717. " type: %s", object_get_typename(OBJECT(dev)));
  1718. }
  1719. }
  1720. static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
  1721. DeviceState *dev, Error **errp)
  1722. {
  1723. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
  1724. pc_memory_unplug(hotplug_dev, dev, errp);
  1725. } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
  1726. pc_cpu_unplug_cb(hotplug_dev, dev, errp);
  1727. } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
  1728. pc_virtio_pmem_pci_unplug(hotplug_dev, dev, errp);
  1729. } else {
  1730. error_setg(errp, "acpi: device unplug for not supported device"
  1731. " type: %s", object_get_typename(OBJECT(dev)));
  1732. }
  1733. }
  1734. static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
  1735. DeviceState *dev)
  1736. {
  1737. if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
  1738. object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
  1739. object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
  1740. return HOTPLUG_HANDLER(machine);
  1741. }
  1742. return NULL;
  1743. }
  1744. static void
  1745. pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
  1746. const char *name, void *opaque,
  1747. Error **errp)
  1748. {
  1749. MachineState *ms = MACHINE(obj);
  1750. int64_t value = 0;
  1751. if (ms->device_memory) {
  1752. value = memory_region_size(&ms->device_memory->mr);
  1753. }
  1754. visit_type_int(v, name, &value, errp);
  1755. }
  1756. static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
  1757. void *opaque, Error **errp)
  1758. {
  1759. PCMachineState *pcms = PC_MACHINE(obj);
  1760. OnOffAuto vmport = pcms->vmport;
  1761. visit_type_OnOffAuto(v, name, &vmport, errp);
  1762. }
  1763. static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
  1764. void *opaque, Error **errp)
  1765. {
  1766. PCMachineState *pcms = PC_MACHINE(obj);
  1767. visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
  1768. }
  1769. bool pc_machine_is_smm_enabled(PCMachineState *pcms)
  1770. {
  1771. bool smm_available = false;
  1772. if (pcms->smm == ON_OFF_AUTO_OFF) {
  1773. return false;
  1774. }
  1775. if (tcg_enabled() || qtest_enabled()) {
  1776. smm_available = true;
  1777. } else if (kvm_enabled()) {
  1778. smm_available = kvm_has_smm();
  1779. }
  1780. if (smm_available) {
  1781. return true;
  1782. }
  1783. if (pcms->smm == ON_OFF_AUTO_ON) {
  1784. error_report("System Management Mode not supported by this hypervisor.");
  1785. exit(1);
  1786. }
  1787. return false;
  1788. }
  1789. static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name,
  1790. void *opaque, Error **errp)
  1791. {
  1792. PCMachineState *pcms = PC_MACHINE(obj);
  1793. OnOffAuto smm = pcms->smm;
  1794. visit_type_OnOffAuto(v, name, &smm, errp);
  1795. }
  1796. static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name,
  1797. void *opaque, Error **errp)
  1798. {
  1799. PCMachineState *pcms = PC_MACHINE(obj);
  1800. visit_type_OnOffAuto(v, name, &pcms->smm, errp);
  1801. }
  1802. static bool pc_machine_get_smbus(Object *obj, Error **errp)
  1803. {
  1804. PCMachineState *pcms = PC_MACHINE(obj);
  1805. return pcms->smbus_enabled;
  1806. }
  1807. static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
  1808. {
  1809. PCMachineState *pcms = PC_MACHINE(obj);
  1810. pcms->smbus_enabled = value;
  1811. }
  1812. static bool pc_machine_get_sata(Object *obj, Error **errp)
  1813. {
  1814. PCMachineState *pcms = PC_MACHINE(obj);
  1815. return pcms->sata_enabled;
  1816. }
  1817. static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
  1818. {
  1819. PCMachineState *pcms = PC_MACHINE(obj);
  1820. pcms->sata_enabled = value;
  1821. }
  1822. static bool pc_machine_get_pit(Object *obj, Error **errp)
  1823. {
  1824. PCMachineState *pcms = PC_MACHINE(obj);
  1825. return pcms->pit_enabled;
  1826. }
  1827. static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
  1828. {
  1829. PCMachineState *pcms = PC_MACHINE(obj);
  1830. pcms->pit_enabled = value;
  1831. }
  1832. static void pc_machine_initfn(Object *obj)
  1833. {
  1834. PCMachineState *pcms = PC_MACHINE(obj);
  1835. pcms->smm = ON_OFF_AUTO_AUTO;
  1836. #ifdef CONFIG_VMPORT
  1837. pcms->vmport = ON_OFF_AUTO_AUTO;
  1838. #else
  1839. pcms->vmport = ON_OFF_AUTO_OFF;
  1840. #endif /* CONFIG_VMPORT */
  1841. /* acpi build is enabled by default if machine supports it */
  1842. pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
  1843. pcms->smbus_enabled = true;
  1844. pcms->sata_enabled = true;
  1845. pcms->pit_enabled = true;
  1846. pc_system_flash_create(pcms);
  1847. }
  1848. static void pc_machine_reset(MachineState *machine)
  1849. {
  1850. CPUState *cs;
  1851. X86CPU *cpu;
  1852. qemu_devices_reset();
  1853. /* Reset APIC after devices have been reset to cancel
  1854. * any changes that qemu_devices_reset() might have done.
  1855. */
  1856. CPU_FOREACH(cs) {
  1857. cpu = X86_CPU(cs);
  1858. if (cpu->apic_state) {
  1859. device_reset(cpu->apic_state);
  1860. }
  1861. }
  1862. }
  1863. static void pc_machine_wakeup(MachineState *machine)
  1864. {
  1865. cpu_synchronize_all_states();
  1866. pc_machine_reset(machine);
  1867. cpu_synchronize_all_post_reset();
  1868. }
  1869. static bool pc_hotplug_allowed(MachineState *ms, DeviceState *dev, Error **errp)
  1870. {
  1871. X86IOMMUState *iommu = x86_iommu_get_default();
  1872. IntelIOMMUState *intel_iommu;
  1873. if (iommu &&
  1874. object_dynamic_cast((Object *)iommu, TYPE_INTEL_IOMMU_DEVICE) &&
  1875. object_dynamic_cast((Object *)dev, "vfio-pci")) {
  1876. intel_iommu = INTEL_IOMMU_DEVICE(iommu);
  1877. if (!intel_iommu->caching_mode) {
  1878. error_setg(errp, "Device assignment is not allowed without "
  1879. "enabling caching-mode=on for Intel IOMMU.");
  1880. return false;
  1881. }
  1882. }
  1883. return true;
  1884. }
  1885. static void pc_machine_class_init(ObjectClass *oc, void *data)
  1886. {
  1887. MachineClass *mc = MACHINE_CLASS(oc);
  1888. PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
  1889. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
  1890. pcmc->pci_enabled = true;
  1891. pcmc->has_acpi_build = true;
  1892. pcmc->rsdp_in_ram = true;
  1893. pcmc->smbios_defaults = true;
  1894. pcmc->smbios_uuid_encoded = true;
  1895. pcmc->gigabyte_align = true;
  1896. pcmc->has_reserved_memory = true;
  1897. pcmc->kvmclock_enabled = true;
  1898. pcmc->enforce_aligned_dimm = true;
  1899. /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
  1900. * to be used at the moment, 32K should be enough for a while. */
  1901. pcmc->acpi_data_size = 0x20000 + 0x8000;
  1902. pcmc->linuxboot_dma_enabled = true;
  1903. pcmc->pvh_enabled = true;
  1904. assert(!mc->get_hotplug_handler);
  1905. mc->get_hotplug_handler = pc_get_hotplug_handler;
  1906. mc->hotplug_allowed = pc_hotplug_allowed;
  1907. mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
  1908. mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
  1909. mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
  1910. mc->auto_enable_numa_with_memhp = true;
  1911. mc->has_hotpluggable_cpus = true;
  1912. mc->default_boot_order = "cad";
  1913. mc->hot_add_cpu = pc_hot_add_cpu;
  1914. mc->smp_parse = pc_smp_parse;
  1915. mc->block_default_type = IF_IDE;
  1916. mc->max_cpus = 255;
  1917. mc->reset = pc_machine_reset;
  1918. mc->wakeup = pc_machine_wakeup;
  1919. hc->pre_plug = pc_machine_device_pre_plug_cb;
  1920. hc->plug = pc_machine_device_plug_cb;
  1921. hc->unplug_request = pc_machine_device_unplug_request_cb;
  1922. hc->unplug = pc_machine_device_unplug_cb;
  1923. mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
  1924. mc->nvdimm_supported = true;
  1925. mc->numa_mem_supported = true;
  1926. object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
  1927. pc_machine_get_device_memory_region_size, NULL,
  1928. NULL, NULL, &error_abort);
  1929. object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto",
  1930. pc_machine_get_smm, pc_machine_set_smm,
  1931. NULL, NULL, &error_abort);
  1932. object_class_property_set_description(oc, PC_MACHINE_SMM,
  1933. "Enable SMM (pc & q35)", &error_abort);
  1934. object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
  1935. pc_machine_get_vmport, pc_machine_set_vmport,
  1936. NULL, NULL, &error_abort);
  1937. object_class_property_set_description(oc, PC_MACHINE_VMPORT,
  1938. "Enable vmport (pc & q35)", &error_abort);
  1939. object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
  1940. pc_machine_get_smbus, pc_machine_set_smbus, &error_abort);
  1941. object_class_property_add_bool(oc, PC_MACHINE_SATA,
  1942. pc_machine_get_sata, pc_machine_set_sata, &error_abort);
  1943. object_class_property_add_bool(oc, PC_MACHINE_PIT,
  1944. pc_machine_get_pit, pc_machine_set_pit, &error_abort);
  1945. }
  1946. static const TypeInfo pc_machine_info = {
  1947. .name = TYPE_PC_MACHINE,
  1948. .parent = TYPE_X86_MACHINE,
  1949. .abstract = true,
  1950. .instance_size = sizeof(PCMachineState),
  1951. .instance_init = pc_machine_initfn,
  1952. .class_size = sizeof(PCMachineClass),
  1953. .class_init = pc_machine_class_init,
  1954. .interfaces = (InterfaceInfo[]) {
  1955. { TYPE_HOTPLUG_HANDLER },
  1956. { }
  1957. },
  1958. };
  1959. static void pc_machine_register_types(void)
  1960. {
  1961. type_register_static(&pc_machine_info);
  1962. }
  1963. type_init(pc_machine_register_types)