x86.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. /*
  2. * Copyright (c) 2003-2004 Fabrice Bellard
  3. * Copyright (c) 2019 Red Hat, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "qemu/osdep.h"
  24. #include "qemu/error-report.h"
  25. #include "qemu/option.h"
  26. #include "qemu/cutils.h"
  27. #include "qemu/units.h"
  28. #include "qemu-common.h"
  29. #include "qemu/datadir.h"
  30. #include "qapi/error.h"
  31. #include "qapi/qmp/qerror.h"
  32. #include "qapi/qapi-visit-common.h"
  33. #include "qapi/clone-visitor.h"
  34. #include "qapi/qapi-visit-machine.h"
  35. #include "qapi/visitor.h"
  36. #include "sysemu/qtest.h"
  37. #include "sysemu/whpx.h"
  38. #include "sysemu/numa.h"
  39. #include "sysemu/replay.h"
  40. #include "sysemu/sysemu.h"
  41. #include "sysemu/cpu-timers.h"
  42. #include "trace.h"
  43. #include "hw/i386/x86.h"
  44. #include "target/i386/cpu.h"
  45. #include "hw/i386/topology.h"
  46. #include "hw/i386/fw_cfg.h"
  47. #include "hw/intc/i8259.h"
  48. #include "hw/rtc/mc146818rtc.h"
  49. #include "target/i386/sev.h"
  50. #include "hw/acpi/cpu_hotplug.h"
  51. #include "hw/irq.h"
  52. #include "hw/nmi.h"
  53. #include "hw/loader.h"
  54. #include "multiboot.h"
  55. #include "elf.h"
  56. #include "standard-headers/asm-x86/bootparam.h"
  57. #include CONFIG_DEVICES
  58. #include "kvm/kvm_i386.h"
  59. /* Physical Address of PVH entry point read from kernel ELF NOTE */
  60. static size_t pvh_start_addr;
  61. inline void init_topo_info(X86CPUTopoInfo *topo_info,
  62. const X86MachineState *x86ms)
  63. {
  64. MachineState *ms = MACHINE(x86ms);
  65. topo_info->dies_per_pkg = ms->smp.dies;
  66. topo_info->cores_per_die = ms->smp.cores;
  67. topo_info->threads_per_core = ms->smp.threads;
  68. }
  69. /*
  70. * Calculates initial APIC ID for a specific CPU index
  71. *
  72. * Currently we need to be able to calculate the APIC ID from the CPU index
  73. * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
  74. * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
  75. * all CPUs up to max_cpus.
  76. */
  77. uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
  78. unsigned int cpu_index)
  79. {
  80. X86MachineClass *x86mc = X86_MACHINE_GET_CLASS(x86ms);
  81. X86CPUTopoInfo topo_info;
  82. uint32_t correct_id;
  83. static bool warned;
  84. init_topo_info(&topo_info, x86ms);
  85. correct_id = x86_apicid_from_cpu_idx(&topo_info, cpu_index);
  86. if (x86mc->compat_apic_id_mode) {
  87. if (cpu_index != correct_id && !warned && !qtest_enabled()) {
  88. error_report("APIC IDs set in compatibility mode, "
  89. "CPU topology won't match the configuration");
  90. warned = true;
  91. }
  92. return cpu_index;
  93. } else {
  94. return correct_id;
  95. }
  96. }
  97. void x86_cpu_new(X86MachineState *x86ms, int64_t apic_id, Error **errp)
  98. {
  99. Object *cpu = object_new(MACHINE(x86ms)->cpu_type);
  100. if (!object_property_set_uint(cpu, "apic-id", apic_id, errp)) {
  101. goto out;
  102. }
  103. qdev_realize(DEVICE(cpu), NULL, errp);
  104. out:
  105. object_unref(cpu);
  106. }
  107. void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
  108. {
  109. int i;
  110. const CPUArchIdList *possible_cpus;
  111. MachineState *ms = MACHINE(x86ms);
  112. MachineClass *mc = MACHINE_GET_CLASS(x86ms);
  113. x86_cpu_set_default_version(default_cpu_version);
  114. /*
  115. * Calculates the limit to CPU APIC ID values
  116. *
  117. * Limit for the APIC ID value, so that all
  118. * CPU APIC IDs are < x86ms->apic_id_limit.
  119. *
  120. * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
  121. */
  122. x86ms->apic_id_limit = x86_cpu_apic_id_from_index(x86ms,
  123. ms->smp.max_cpus - 1) + 1;
  124. possible_cpus = mc->possible_cpu_arch_ids(ms);
  125. for (i = 0; i < ms->smp.cpus; i++) {
  126. x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
  127. }
  128. }
  129. void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
  130. {
  131. if (cpus_count > 0xff) {
  132. /*
  133. * If the number of CPUs can't be represented in 8 bits, the
  134. * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
  135. * to make old BIOSes fail more predictably.
  136. */
  137. rtc_set_memory(rtc, 0x5f, 0);
  138. } else {
  139. rtc_set_memory(rtc, 0x5f, cpus_count - 1);
  140. }
  141. }
  142. static int x86_apic_cmp(const void *a, const void *b)
  143. {
  144. CPUArchId *apic_a = (CPUArchId *)a;
  145. CPUArchId *apic_b = (CPUArchId *)b;
  146. return apic_a->arch_id - apic_b->arch_id;
  147. }
  148. /*
  149. * returns pointer to CPUArchId descriptor that matches CPU's apic_id
  150. * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
  151. * entry corresponding to CPU's apic_id returns NULL.
  152. */
  153. CPUArchId *x86_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
  154. {
  155. CPUArchId apic_id, *found_cpu;
  156. apic_id.arch_id = id;
  157. found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
  158. ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
  159. x86_apic_cmp);
  160. if (found_cpu && idx) {
  161. *idx = found_cpu - ms->possible_cpus->cpus;
  162. }
  163. return found_cpu;
  164. }
  165. void x86_cpu_plug(HotplugHandler *hotplug_dev,
  166. DeviceState *dev, Error **errp)
  167. {
  168. CPUArchId *found_cpu;
  169. Error *local_err = NULL;
  170. X86CPU *cpu = X86_CPU(dev);
  171. X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  172. if (x86ms->acpi_dev) {
  173. hotplug_handler_plug(x86ms->acpi_dev, dev, &local_err);
  174. if (local_err) {
  175. goto out;
  176. }
  177. }
  178. /* increment the number of CPUs */
  179. x86ms->boot_cpus++;
  180. if (x86ms->rtc) {
  181. x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
  182. }
  183. if (x86ms->fw_cfg) {
  184. fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  185. }
  186. found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL);
  187. found_cpu->cpu = OBJECT(dev);
  188. out:
  189. error_propagate(errp, local_err);
  190. }
  191. void x86_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
  192. DeviceState *dev, Error **errp)
  193. {
  194. int idx = -1;
  195. X86CPU *cpu = X86_CPU(dev);
  196. X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  197. if (!x86ms->acpi_dev) {
  198. error_setg(errp, "CPU hot unplug not supported without ACPI");
  199. return;
  200. }
  201. x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx);
  202. assert(idx != -1);
  203. if (idx == 0) {
  204. error_setg(errp, "Boot CPU is unpluggable");
  205. return;
  206. }
  207. hotplug_handler_unplug_request(x86ms->acpi_dev, dev,
  208. errp);
  209. }
  210. void x86_cpu_unplug_cb(HotplugHandler *hotplug_dev,
  211. DeviceState *dev, Error **errp)
  212. {
  213. CPUArchId *found_cpu;
  214. Error *local_err = NULL;
  215. X86CPU *cpu = X86_CPU(dev);
  216. X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  217. hotplug_handler_unplug(x86ms->acpi_dev, dev, &local_err);
  218. if (local_err) {
  219. goto out;
  220. }
  221. found_cpu = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, NULL);
  222. found_cpu->cpu = NULL;
  223. qdev_unrealize(dev);
  224. /* decrement the number of CPUs */
  225. x86ms->boot_cpus--;
  226. /* Update the number of CPUs in CMOS */
  227. x86_rtc_set_cpus_count(x86ms->rtc, x86ms->boot_cpus);
  228. fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
  229. out:
  230. error_propagate(errp, local_err);
  231. }
  232. void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
  233. DeviceState *dev, Error **errp)
  234. {
  235. int idx;
  236. CPUState *cs;
  237. CPUArchId *cpu_slot;
  238. X86CPUTopoIDs topo_ids;
  239. X86CPU *cpu = X86_CPU(dev);
  240. CPUX86State *env = &cpu->env;
  241. MachineState *ms = MACHINE(hotplug_dev);
  242. X86MachineState *x86ms = X86_MACHINE(hotplug_dev);
  243. unsigned int smp_cores = ms->smp.cores;
  244. unsigned int smp_threads = ms->smp.threads;
  245. X86CPUTopoInfo topo_info;
  246. if (!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
  247. error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
  248. ms->cpu_type);
  249. return;
  250. }
  251. if (x86ms->acpi_dev) {
  252. Error *local_err = NULL;
  253. hotplug_handler_pre_plug(HOTPLUG_HANDLER(x86ms->acpi_dev), dev,
  254. &local_err);
  255. if (local_err) {
  256. error_propagate(errp, local_err);
  257. return;
  258. }
  259. }
  260. init_topo_info(&topo_info, x86ms);
  261. env->nr_dies = ms->smp.dies;
  262. /*
  263. * If APIC ID is not set,
  264. * set it based on socket/die/core/thread properties.
  265. */
  266. if (cpu->apic_id == UNASSIGNED_APIC_ID) {
  267. int max_socket = (ms->smp.max_cpus - 1) /
  268. smp_threads / smp_cores / ms->smp.dies;
  269. /*
  270. * die-id was optional in QEMU 4.0 and older, so keep it optional
  271. * if there's only one die per socket.
  272. */
  273. if (cpu->die_id < 0 && ms->smp.dies == 1) {
  274. cpu->die_id = 0;
  275. }
  276. if (cpu->socket_id < 0) {
  277. error_setg(errp, "CPU socket-id is not set");
  278. return;
  279. } else if (cpu->socket_id > max_socket) {
  280. error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
  281. cpu->socket_id, max_socket);
  282. return;
  283. }
  284. if (cpu->die_id < 0) {
  285. error_setg(errp, "CPU die-id is not set");
  286. return;
  287. } else if (cpu->die_id > ms->smp.dies - 1) {
  288. error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
  289. cpu->die_id, ms->smp.dies - 1);
  290. return;
  291. }
  292. if (cpu->core_id < 0) {
  293. error_setg(errp, "CPU core-id is not set");
  294. return;
  295. } else if (cpu->core_id > (smp_cores - 1)) {
  296. error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
  297. cpu->core_id, smp_cores - 1);
  298. return;
  299. }
  300. if (cpu->thread_id < 0) {
  301. error_setg(errp, "CPU thread-id is not set");
  302. return;
  303. } else if (cpu->thread_id > (smp_threads - 1)) {
  304. error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
  305. cpu->thread_id, smp_threads - 1);
  306. return;
  307. }
  308. topo_ids.pkg_id = cpu->socket_id;
  309. topo_ids.die_id = cpu->die_id;
  310. topo_ids.core_id = cpu->core_id;
  311. topo_ids.smt_id = cpu->thread_id;
  312. cpu->apic_id = x86_apicid_from_topo_ids(&topo_info, &topo_ids);
  313. }
  314. cpu_slot = x86_find_cpu_slot(MACHINE(x86ms), cpu->apic_id, &idx);
  315. if (!cpu_slot) {
  316. MachineState *ms = MACHINE(x86ms);
  317. x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids);
  318. error_setg(errp,
  319. "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
  320. " APIC ID %" PRIu32 ", valid index range 0:%d",
  321. topo_ids.pkg_id, topo_ids.die_id, topo_ids.core_id, topo_ids.smt_id,
  322. cpu->apic_id, ms->possible_cpus->len - 1);
  323. return;
  324. }
  325. if (cpu_slot->cpu) {
  326. error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
  327. idx, cpu->apic_id);
  328. return;
  329. }
  330. /* if 'address' properties socket-id/core-id/thread-id are not set, set them
  331. * so that machine_query_hotpluggable_cpus would show correct values
  332. */
  333. /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
  334. * once -smp refactoring is complete and there will be CPU private
  335. * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
  336. x86_topo_ids_from_apicid(cpu->apic_id, &topo_info, &topo_ids);
  337. if (cpu->socket_id != -1 && cpu->socket_id != topo_ids.pkg_id) {
  338. error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
  339. " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id,
  340. topo_ids.pkg_id);
  341. return;
  342. }
  343. cpu->socket_id = topo_ids.pkg_id;
  344. if (cpu->die_id != -1 && cpu->die_id != topo_ids.die_id) {
  345. error_setg(errp, "property die-id: %u doesn't match set apic-id:"
  346. " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo_ids.die_id);
  347. return;
  348. }
  349. cpu->die_id = topo_ids.die_id;
  350. if (cpu->core_id != -1 && cpu->core_id != topo_ids.core_id) {
  351. error_setg(errp, "property core-id: %u doesn't match set apic-id:"
  352. " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id,
  353. topo_ids.core_id);
  354. return;
  355. }
  356. cpu->core_id = topo_ids.core_id;
  357. if (cpu->thread_id != -1 && cpu->thread_id != topo_ids.smt_id) {
  358. error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
  359. " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id,
  360. topo_ids.smt_id);
  361. return;
  362. }
  363. cpu->thread_id = topo_ids.smt_id;
  364. if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
  365. !kvm_hv_vpindex_settable()) {
  366. error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
  367. return;
  368. }
  369. cs = CPU(cpu);
  370. cs->cpu_index = idx;
  371. numa_cpu_pre_plug(cpu_slot, dev, errp);
  372. }
  373. CpuInstanceProperties
  374. x86_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
  375. {
  376. MachineClass *mc = MACHINE_GET_CLASS(ms);
  377. const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
  378. assert(cpu_index < possible_cpus->len);
  379. return possible_cpus->cpus[cpu_index].props;
  380. }
  381. int64_t x86_get_default_cpu_node_id(const MachineState *ms, int idx)
  382. {
  383. X86CPUTopoIDs topo_ids;
  384. X86MachineState *x86ms = X86_MACHINE(ms);
  385. X86CPUTopoInfo topo_info;
  386. init_topo_info(&topo_info, x86ms);
  387. assert(idx < ms->possible_cpus->len);
  388. x86_topo_ids_from_apicid(ms->possible_cpus->cpus[idx].arch_id,
  389. &topo_info, &topo_ids);
  390. return topo_ids.pkg_id % ms->numa_state->num_nodes;
  391. }
  392. const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms)
  393. {
  394. X86MachineState *x86ms = X86_MACHINE(ms);
  395. unsigned int max_cpus = ms->smp.max_cpus;
  396. X86CPUTopoInfo topo_info;
  397. int i;
  398. if (ms->possible_cpus) {
  399. /*
  400. * make sure that max_cpus hasn't changed since the first use, i.e.
  401. * -smp hasn't been parsed after it
  402. */
  403. assert(ms->possible_cpus->len == max_cpus);
  404. return ms->possible_cpus;
  405. }
  406. ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
  407. sizeof(CPUArchId) * max_cpus);
  408. ms->possible_cpus->len = max_cpus;
  409. init_topo_info(&topo_info, x86ms);
  410. for (i = 0; i < ms->possible_cpus->len; i++) {
  411. X86CPUTopoIDs topo_ids;
  412. ms->possible_cpus->cpus[i].type = ms->cpu_type;
  413. ms->possible_cpus->cpus[i].vcpus_count = 1;
  414. ms->possible_cpus->cpus[i].arch_id =
  415. x86_cpu_apic_id_from_index(x86ms, i);
  416. x86_topo_ids_from_apicid(ms->possible_cpus->cpus[i].arch_id,
  417. &topo_info, &topo_ids);
  418. ms->possible_cpus->cpus[i].props.has_socket_id = true;
  419. ms->possible_cpus->cpus[i].props.socket_id = topo_ids.pkg_id;
  420. if (ms->smp.dies > 1) {
  421. ms->possible_cpus->cpus[i].props.has_die_id = true;
  422. ms->possible_cpus->cpus[i].props.die_id = topo_ids.die_id;
  423. }
  424. ms->possible_cpus->cpus[i].props.has_core_id = true;
  425. ms->possible_cpus->cpus[i].props.core_id = topo_ids.core_id;
  426. ms->possible_cpus->cpus[i].props.has_thread_id = true;
  427. ms->possible_cpus->cpus[i].props.thread_id = topo_ids.smt_id;
  428. }
  429. return ms->possible_cpus;
  430. }
  431. static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
  432. {
  433. /* cpu index isn't used */
  434. CPUState *cs;
  435. CPU_FOREACH(cs) {
  436. X86CPU *cpu = X86_CPU(cs);
  437. if (!cpu->apic_state) {
  438. cpu_interrupt(cs, CPU_INTERRUPT_NMI);
  439. } else {
  440. apic_deliver_nmi(cpu->apic_state);
  441. }
  442. }
  443. }
  444. static long get_file_size(FILE *f)
  445. {
  446. long where, size;
  447. /* XXX: on Unix systems, using fstat() probably makes more sense */
  448. where = ftell(f);
  449. fseek(f, 0, SEEK_END);
  450. size = ftell(f);
  451. fseek(f, where, SEEK_SET);
  452. return size;
  453. }
  454. /* TSC handling */
  455. uint64_t cpu_get_tsc(CPUX86State *env)
  456. {
  457. return cpus_get_elapsed_ticks();
  458. }
  459. /* IRQ handling */
  460. static void pic_irq_request(void *opaque, int irq, int level)
  461. {
  462. CPUState *cs = first_cpu;
  463. X86CPU *cpu = X86_CPU(cs);
  464. trace_x86_pic_interrupt(irq, level);
  465. if (cpu->apic_state && !kvm_irqchip_in_kernel() &&
  466. !whpx_apic_in_platform()) {
  467. CPU_FOREACH(cs) {
  468. cpu = X86_CPU(cs);
  469. if (apic_accept_pic_intr(cpu->apic_state)) {
  470. apic_deliver_pic_intr(cpu->apic_state, level);
  471. }
  472. }
  473. } else {
  474. if (level) {
  475. cpu_interrupt(cs, CPU_INTERRUPT_HARD);
  476. } else {
  477. cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
  478. }
  479. }
  480. }
  481. qemu_irq x86_allocate_cpu_irq(void)
  482. {
  483. return qemu_allocate_irq(pic_irq_request, NULL, 0);
  484. }
  485. int cpu_get_pic_interrupt(CPUX86State *env)
  486. {
  487. X86CPU *cpu = env_archcpu(env);
  488. int intno;
  489. if (!kvm_irqchip_in_kernel() && !whpx_apic_in_platform()) {
  490. intno = apic_get_interrupt(cpu->apic_state);
  491. if (intno >= 0) {
  492. return intno;
  493. }
  494. /* read the irq from the PIC */
  495. if (!apic_accept_pic_intr(cpu->apic_state)) {
  496. return -1;
  497. }
  498. }
  499. intno = pic_read_irq(isa_pic);
  500. return intno;
  501. }
  502. DeviceState *cpu_get_current_apic(void)
  503. {
  504. if (current_cpu) {
  505. X86CPU *cpu = X86_CPU(current_cpu);
  506. return cpu->apic_state;
  507. } else {
  508. return NULL;
  509. }
  510. }
  511. void gsi_handler(void *opaque, int n, int level)
  512. {
  513. GSIState *s = opaque;
  514. trace_x86_gsi_interrupt(n, level);
  515. switch (n) {
  516. case 0 ... ISA_NUM_IRQS - 1:
  517. if (s->i8259_irq[n]) {
  518. /* Under KVM, Kernel will forward to both PIC and IOAPIC */
  519. qemu_set_irq(s->i8259_irq[n], level);
  520. }
  521. /* fall through */
  522. case ISA_NUM_IRQS ... IOAPIC_NUM_PINS - 1:
  523. qemu_set_irq(s->ioapic_irq[n], level);
  524. break;
  525. case IO_APIC_SECONDARY_IRQBASE
  526. ... IO_APIC_SECONDARY_IRQBASE + IOAPIC_NUM_PINS - 1:
  527. qemu_set_irq(s->ioapic2_irq[n - IO_APIC_SECONDARY_IRQBASE], level);
  528. break;
  529. }
  530. }
  531. void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
  532. {
  533. DeviceState *dev;
  534. SysBusDevice *d;
  535. unsigned int i;
  536. assert(parent_name);
  537. if (kvm_ioapic_in_kernel()) {
  538. dev = qdev_new(TYPE_KVM_IOAPIC);
  539. } else {
  540. dev = qdev_new(TYPE_IOAPIC);
  541. }
  542. object_property_add_child(object_resolve_path(parent_name, NULL),
  543. "ioapic", OBJECT(dev));
  544. d = SYS_BUS_DEVICE(dev);
  545. sysbus_realize_and_unref(d, &error_fatal);
  546. sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
  547. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  548. gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
  549. }
  550. }
  551. DeviceState *ioapic_init_secondary(GSIState *gsi_state)
  552. {
  553. DeviceState *dev;
  554. SysBusDevice *d;
  555. unsigned int i;
  556. dev = qdev_new(TYPE_IOAPIC);
  557. d = SYS_BUS_DEVICE(dev);
  558. sysbus_realize_and_unref(d, &error_fatal);
  559. sysbus_mmio_map(d, 0, IO_APIC_SECONDARY_ADDRESS);
  560. for (i = 0; i < IOAPIC_NUM_PINS; i++) {
  561. gsi_state->ioapic2_irq[i] = qdev_get_gpio_in(dev, i);
  562. }
  563. return dev;
  564. }
  565. struct setup_data {
  566. uint64_t next;
  567. uint32_t type;
  568. uint32_t len;
  569. uint8_t data[];
  570. } __attribute__((packed));
  571. /*
  572. * The entry point into the kernel for PVH boot is different from
  573. * the native entry point. The PVH entry is defined by the x86/HVM
  574. * direct boot ABI and is available in an ELFNOTE in the kernel binary.
  575. *
  576. * This function is passed to load_elf() when it is called from
  577. * load_elfboot() which then additionally checks for an ELF Note of
  578. * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
  579. * parse the PVH entry address from the ELF Note.
  580. *
  581. * Due to trickery in elf_opts.h, load_elf() is actually available as
  582. * load_elf32() or load_elf64() and this routine needs to be able
  583. * to deal with being called as 32 or 64 bit.
  584. *
  585. * The address of the PVH entry point is saved to the 'pvh_start_addr'
  586. * global variable. (although the entry point is 32-bit, the kernel
  587. * binary can be either 32-bit or 64-bit).
  588. */
  589. static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64)
  590. {
  591. size_t *elf_note_data_addr;
  592. /* Check if ELF Note header passed in is valid */
  593. if (arg1 == NULL) {
  594. return 0;
  595. }
  596. if (is64) {
  597. struct elf64_note *nhdr64 = (struct elf64_note *)arg1;
  598. uint64_t nhdr_size64 = sizeof(struct elf64_note);
  599. uint64_t phdr_align = *(uint64_t *)arg2;
  600. uint64_t nhdr_namesz = nhdr64->n_namesz;
  601. elf_note_data_addr =
  602. ((void *)nhdr64) + nhdr_size64 +
  603. QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
  604. pvh_start_addr = *elf_note_data_addr;
  605. } else {
  606. struct elf32_note *nhdr32 = (struct elf32_note *)arg1;
  607. uint32_t nhdr_size32 = sizeof(struct elf32_note);
  608. uint32_t phdr_align = *(uint32_t *)arg2;
  609. uint32_t nhdr_namesz = nhdr32->n_namesz;
  610. elf_note_data_addr =
  611. ((void *)nhdr32) + nhdr_size32 +
  612. QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
  613. pvh_start_addr = *(uint32_t *)elf_note_data_addr;
  614. }
  615. return pvh_start_addr;
  616. }
  617. static bool load_elfboot(const char *kernel_filename,
  618. int kernel_file_size,
  619. uint8_t *header,
  620. size_t pvh_xen_start_addr,
  621. FWCfgState *fw_cfg)
  622. {
  623. uint32_t flags = 0;
  624. uint32_t mh_load_addr = 0;
  625. uint32_t elf_kernel_size = 0;
  626. uint64_t elf_entry;
  627. uint64_t elf_low, elf_high;
  628. int kernel_size;
  629. if (ldl_p(header) != 0x464c457f) {
  630. return false; /* no elfboot */
  631. }
  632. bool elf_is64 = header[EI_CLASS] == ELFCLASS64;
  633. flags = elf_is64 ?
  634. ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags;
  635. if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
  636. error_report("elfboot unsupported flags = %x", flags);
  637. exit(1);
  638. }
  639. uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY;
  640. kernel_size = load_elf(kernel_filename, read_pvh_start_addr,
  641. NULL, &elf_note_type, &elf_entry,
  642. &elf_low, &elf_high, NULL, 0, I386_ELF_MACHINE,
  643. 0, 0);
  644. if (kernel_size < 0) {
  645. error_report("Error while loading elf kernel");
  646. exit(1);
  647. }
  648. mh_load_addr = elf_low;
  649. elf_kernel_size = elf_high - elf_low;
  650. if (pvh_start_addr == 0) {
  651. error_report("Error loading uncompressed kernel without PVH ELF Note");
  652. exit(1);
  653. }
  654. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr);
  655. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
  656. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size);
  657. return true;
  658. }
  659. void x86_load_linux(X86MachineState *x86ms,
  660. FWCfgState *fw_cfg,
  661. int acpi_data_size,
  662. bool pvh_enabled)
  663. {
  664. bool linuxboot_dma_enabled = X86_MACHINE_GET_CLASS(x86ms)->fwcfg_dma_enabled;
  665. uint16_t protocol;
  666. int setup_size, kernel_size, cmdline_size;
  667. int dtb_size, setup_data_offset;
  668. uint32_t initrd_max;
  669. uint8_t header[8192], *setup, *kernel;
  670. hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
  671. FILE *f;
  672. char *vmode;
  673. MachineState *machine = MACHINE(x86ms);
  674. struct setup_data *setup_data;
  675. const char *kernel_filename = machine->kernel_filename;
  676. const char *initrd_filename = machine->initrd_filename;
  677. const char *dtb_filename = machine->dtb;
  678. const char *kernel_cmdline = machine->kernel_cmdline;
  679. SevKernelLoaderContext sev_load_ctx = {};
  680. /* Align to 16 bytes as a paranoia measure */
  681. cmdline_size = (strlen(kernel_cmdline) + 16) & ~15;
  682. /* load the kernel header */
  683. f = fopen(kernel_filename, "rb");
  684. if (!f) {
  685. fprintf(stderr, "qemu: could not open kernel file '%s': %s\n",
  686. kernel_filename, strerror(errno));
  687. exit(1);
  688. }
  689. kernel_size = get_file_size(f);
  690. if (!kernel_size ||
  691. fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
  692. MIN(ARRAY_SIZE(header), kernel_size)) {
  693. fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
  694. kernel_filename, strerror(errno));
  695. exit(1);
  696. }
  697. /* kernel protocol version */
  698. if (ldl_p(header + 0x202) == 0x53726448) {
  699. protocol = lduw_p(header + 0x206);
  700. } else {
  701. /*
  702. * This could be a multiboot kernel. If it is, let's stop treating it
  703. * like a Linux kernel.
  704. * Note: some multiboot images could be in the ELF format (the same of
  705. * PVH), so we try multiboot first since we check the multiboot magic
  706. * header before to load it.
  707. */
  708. if (load_multiboot(x86ms, fw_cfg, f, kernel_filename, initrd_filename,
  709. kernel_cmdline, kernel_size, header)) {
  710. return;
  711. }
  712. /*
  713. * Check if the file is an uncompressed kernel file (ELF) and load it,
  714. * saving the PVH entry point used by the x86/HVM direct boot ABI.
  715. * If load_elfboot() is successful, populate the fw_cfg info.
  716. */
  717. if (pvh_enabled &&
  718. load_elfboot(kernel_filename, kernel_size,
  719. header, pvh_start_addr, fw_cfg)) {
  720. fclose(f);
  721. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
  722. strlen(kernel_cmdline) + 1);
  723. fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
  724. fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
  725. fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
  726. header, sizeof(header));
  727. /* load initrd */
  728. if (initrd_filename) {
  729. GMappedFile *mapped_file;
  730. gsize initrd_size;
  731. gchar *initrd_data;
  732. GError *gerr = NULL;
  733. mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
  734. if (!mapped_file) {
  735. fprintf(stderr, "qemu: error reading initrd %s: %s\n",
  736. initrd_filename, gerr->message);
  737. exit(1);
  738. }
  739. x86ms->initrd_mapped_file = mapped_file;
  740. initrd_data = g_mapped_file_get_contents(mapped_file);
  741. initrd_size = g_mapped_file_get_length(mapped_file);
  742. initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
  743. if (initrd_size >= initrd_max) {
  744. fprintf(stderr, "qemu: initrd is too large, cannot support."
  745. "(max: %"PRIu32", need %"PRId64")\n",
  746. initrd_max, (uint64_t)initrd_size);
  747. exit(1);
  748. }
  749. initrd_addr = (initrd_max - initrd_size) & ~4095;
  750. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
  751. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  752. fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data,
  753. initrd_size);
  754. }
  755. option_rom[nb_option_roms].bootindex = 0;
  756. option_rom[nb_option_roms].name = "pvh.bin";
  757. nb_option_roms++;
  758. return;
  759. }
  760. protocol = 0;
  761. }
  762. if (protocol < 0x200 || !(header[0x211] & 0x01)) {
  763. /* Low kernel */
  764. real_addr = 0x90000;
  765. cmdline_addr = 0x9a000 - cmdline_size;
  766. prot_addr = 0x10000;
  767. } else if (protocol < 0x202) {
  768. /* High but ancient kernel */
  769. real_addr = 0x90000;
  770. cmdline_addr = 0x9a000 - cmdline_size;
  771. prot_addr = 0x100000;
  772. } else {
  773. /* High and recent kernel */
  774. real_addr = 0x10000;
  775. cmdline_addr = 0x20000;
  776. prot_addr = 0x100000;
  777. }
  778. /* highest address for loading the initrd */
  779. if (protocol >= 0x20c &&
  780. lduw_p(header + 0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) {
  781. /*
  782. * Linux has supported initrd up to 4 GB for a very long time (2007,
  783. * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
  784. * though it only sets initrd_max to 2 GB to "work around bootloader
  785. * bugs". Luckily, QEMU firmware(which does something like bootloader)
  786. * has supported this.
  787. *
  788. * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
  789. * be loaded into any address.
  790. *
  791. * In addition, initrd_max is uint32_t simply because QEMU doesn't
  792. * support the 64-bit boot protocol (specifically the ext_ramdisk_image
  793. * field).
  794. *
  795. * Therefore here just limit initrd_max to UINT32_MAX simply as well.
  796. */
  797. initrd_max = UINT32_MAX;
  798. } else if (protocol >= 0x203) {
  799. initrd_max = ldl_p(header + 0x22c);
  800. } else {
  801. initrd_max = 0x37ffffff;
  802. }
  803. if (initrd_max >= x86ms->below_4g_mem_size - acpi_data_size) {
  804. initrd_max = x86ms->below_4g_mem_size - acpi_data_size - 1;
  805. }
  806. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
  807. fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1);
  808. fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
  809. sev_load_ctx.cmdline_data = (char *)kernel_cmdline;
  810. sev_load_ctx.cmdline_size = strlen(kernel_cmdline) + 1;
  811. if (protocol >= 0x202) {
  812. stl_p(header + 0x228, cmdline_addr);
  813. } else {
  814. stw_p(header + 0x20, 0xA33F);
  815. stw_p(header + 0x22, cmdline_addr - real_addr);
  816. }
  817. /* handle vga= parameter */
  818. vmode = strstr(kernel_cmdline, "vga=");
  819. if (vmode) {
  820. unsigned int video_mode;
  821. const char *end;
  822. int ret;
  823. /* skip "vga=" */
  824. vmode += 4;
  825. if (!strncmp(vmode, "normal", 6)) {
  826. video_mode = 0xffff;
  827. } else if (!strncmp(vmode, "ext", 3)) {
  828. video_mode = 0xfffe;
  829. } else if (!strncmp(vmode, "ask", 3)) {
  830. video_mode = 0xfffd;
  831. } else {
  832. ret = qemu_strtoui(vmode, &end, 0, &video_mode);
  833. if (ret != 0 || (*end && *end != ' ')) {
  834. fprintf(stderr, "qemu: invalid 'vga=' kernel parameter.\n");
  835. exit(1);
  836. }
  837. }
  838. stw_p(header + 0x1fa, video_mode);
  839. }
  840. /* loader type */
  841. /*
  842. * High nybble = B reserved for QEMU; low nybble is revision number.
  843. * If this code is substantially changed, you may want to consider
  844. * incrementing the revision.
  845. */
  846. if (protocol >= 0x200) {
  847. header[0x210] = 0xB0;
  848. }
  849. /* heap */
  850. if (protocol >= 0x201) {
  851. header[0x211] |= 0x80; /* CAN_USE_HEAP */
  852. stw_p(header + 0x224, cmdline_addr - real_addr - 0x200);
  853. }
  854. /* load initrd */
  855. if (initrd_filename) {
  856. GMappedFile *mapped_file;
  857. gsize initrd_size;
  858. gchar *initrd_data;
  859. GError *gerr = NULL;
  860. if (protocol < 0x200) {
  861. fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
  862. exit(1);
  863. }
  864. mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
  865. if (!mapped_file) {
  866. fprintf(stderr, "qemu: error reading initrd %s: %s\n",
  867. initrd_filename, gerr->message);
  868. exit(1);
  869. }
  870. x86ms->initrd_mapped_file = mapped_file;
  871. initrd_data = g_mapped_file_get_contents(mapped_file);
  872. initrd_size = g_mapped_file_get_length(mapped_file);
  873. if (initrd_size >= initrd_max) {
  874. fprintf(stderr, "qemu: initrd is too large, cannot support."
  875. "(max: %"PRIu32", need %"PRId64")\n",
  876. initrd_max, (uint64_t)initrd_size);
  877. exit(1);
  878. }
  879. initrd_addr = (initrd_max - initrd_size) & ~4095;
  880. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
  881. fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
  882. fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
  883. sev_load_ctx.initrd_data = initrd_data;
  884. sev_load_ctx.initrd_size = initrd_size;
  885. stl_p(header + 0x218, initrd_addr);
  886. stl_p(header + 0x21c, initrd_size);
  887. }
  888. /* load kernel and setup */
  889. setup_size = header[0x1f1];
  890. if (setup_size == 0) {
  891. setup_size = 4;
  892. }
  893. setup_size = (setup_size + 1) * 512;
  894. if (setup_size > kernel_size) {
  895. fprintf(stderr, "qemu: invalid kernel header\n");
  896. exit(1);
  897. }
  898. kernel_size -= setup_size;
  899. setup = g_malloc(setup_size);
  900. kernel = g_malloc(kernel_size);
  901. fseek(f, 0, SEEK_SET);
  902. if (fread(setup, 1, setup_size, f) != setup_size) {
  903. fprintf(stderr, "fread() failed\n");
  904. exit(1);
  905. }
  906. if (fread(kernel, 1, kernel_size, f) != kernel_size) {
  907. fprintf(stderr, "fread() failed\n");
  908. exit(1);
  909. }
  910. fclose(f);
  911. /* append dtb to kernel */
  912. if (dtb_filename) {
  913. if (protocol < 0x209) {
  914. fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
  915. exit(1);
  916. }
  917. dtb_size = get_image_size(dtb_filename);
  918. if (dtb_size <= 0) {
  919. fprintf(stderr, "qemu: error reading dtb %s: %s\n",
  920. dtb_filename, strerror(errno));
  921. exit(1);
  922. }
  923. setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
  924. kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
  925. kernel = g_realloc(kernel, kernel_size);
  926. stq_p(header + 0x250, prot_addr + setup_data_offset);
  927. setup_data = (struct setup_data *)(kernel + setup_data_offset);
  928. setup_data->next = 0;
  929. setup_data->type = cpu_to_le32(SETUP_DTB);
  930. setup_data->len = cpu_to_le32(dtb_size);
  931. load_image_size(dtb_filename, setup_data->data, dtb_size);
  932. }
  933. /*
  934. * If we're starting an encrypted VM, it will be OVMF based, which uses the
  935. * efi stub for booting and doesn't require any values to be placed in the
  936. * kernel header. We therefore don't update the header so the hash of the
  937. * kernel on the other side of the fw_cfg interface matches the hash of the
  938. * file the user passed in.
  939. */
  940. if (!sev_enabled()) {
  941. memcpy(setup, header, MIN(sizeof(header), setup_size));
  942. }
  943. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
  944. fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
  945. fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
  946. sev_load_ctx.kernel_data = (char *)kernel;
  947. sev_load_ctx.kernel_size = kernel_size;
  948. fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
  949. fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
  950. fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
  951. sev_load_ctx.setup_data = (char *)setup;
  952. sev_load_ctx.setup_size = setup_size;
  953. if (sev_enabled()) {
  954. sev_add_kernel_loader_hashes(&sev_load_ctx, &error_fatal);
  955. }
  956. option_rom[nb_option_roms].bootindex = 0;
  957. option_rom[nb_option_roms].name = "linuxboot.bin";
  958. if (linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
  959. option_rom[nb_option_roms].name = "linuxboot_dma.bin";
  960. }
  961. nb_option_roms++;
  962. }
  963. void x86_bios_rom_init(MachineState *ms, const char *default_firmware,
  964. MemoryRegion *rom_memory, bool isapc_ram_fw)
  965. {
  966. const char *bios_name;
  967. char *filename;
  968. MemoryRegion *bios, *isa_bios;
  969. int bios_size, isa_bios_size;
  970. int ret;
  971. /* BIOS load */
  972. bios_name = ms->firmware ?: default_firmware;
  973. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  974. if (filename) {
  975. bios_size = get_image_size(filename);
  976. } else {
  977. bios_size = -1;
  978. }
  979. if (bios_size <= 0 ||
  980. (bios_size % 65536) != 0) {
  981. goto bios_error;
  982. }
  983. bios = g_malloc(sizeof(*bios));
  984. memory_region_init_ram(bios, NULL, "pc.bios", bios_size, &error_fatal);
  985. if (!isapc_ram_fw) {
  986. memory_region_set_readonly(bios, true);
  987. }
  988. ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
  989. if (ret != 0) {
  990. bios_error:
  991. fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
  992. exit(1);
  993. }
  994. g_free(filename);
  995. /* map the last 128KB of the BIOS in ISA space */
  996. isa_bios_size = MIN(bios_size, 128 * KiB);
  997. isa_bios = g_malloc(sizeof(*isa_bios));
  998. memory_region_init_alias(isa_bios, NULL, "isa-bios", bios,
  999. bios_size - isa_bios_size, isa_bios_size);
  1000. memory_region_add_subregion_overlap(rom_memory,
  1001. 0x100000 - isa_bios_size,
  1002. isa_bios,
  1003. 1);
  1004. if (!isapc_ram_fw) {
  1005. memory_region_set_readonly(isa_bios, true);
  1006. }
  1007. /* map all the bios at the top of memory */
  1008. memory_region_add_subregion(rom_memory,
  1009. (uint32_t)(-bios_size),
  1010. bios);
  1011. }
  1012. bool x86_machine_is_smm_enabled(const X86MachineState *x86ms)
  1013. {
  1014. bool smm_available = false;
  1015. if (x86ms->smm == ON_OFF_AUTO_OFF) {
  1016. return false;
  1017. }
  1018. if (tcg_enabled() || qtest_enabled()) {
  1019. smm_available = true;
  1020. } else if (kvm_enabled()) {
  1021. smm_available = kvm_has_smm();
  1022. }
  1023. if (smm_available) {
  1024. return true;
  1025. }
  1026. if (x86ms->smm == ON_OFF_AUTO_ON) {
  1027. error_report("System Management Mode not supported by this hypervisor.");
  1028. exit(1);
  1029. }
  1030. return false;
  1031. }
  1032. static void x86_machine_get_smm(Object *obj, Visitor *v, const char *name,
  1033. void *opaque, Error **errp)
  1034. {
  1035. X86MachineState *x86ms = X86_MACHINE(obj);
  1036. OnOffAuto smm = x86ms->smm;
  1037. visit_type_OnOffAuto(v, name, &smm, errp);
  1038. }
  1039. static void x86_machine_set_smm(Object *obj, Visitor *v, const char *name,
  1040. void *opaque, Error **errp)
  1041. {
  1042. X86MachineState *x86ms = X86_MACHINE(obj);
  1043. visit_type_OnOffAuto(v, name, &x86ms->smm, errp);
  1044. }
  1045. bool x86_machine_is_acpi_enabled(const X86MachineState *x86ms)
  1046. {
  1047. if (x86ms->acpi == ON_OFF_AUTO_OFF) {
  1048. return false;
  1049. }
  1050. return true;
  1051. }
  1052. static void x86_machine_get_acpi(Object *obj, Visitor *v, const char *name,
  1053. void *opaque, Error **errp)
  1054. {
  1055. X86MachineState *x86ms = X86_MACHINE(obj);
  1056. OnOffAuto acpi = x86ms->acpi;
  1057. visit_type_OnOffAuto(v, name, &acpi, errp);
  1058. }
  1059. static void x86_machine_set_acpi(Object *obj, Visitor *v, const char *name,
  1060. void *opaque, Error **errp)
  1061. {
  1062. X86MachineState *x86ms = X86_MACHINE(obj);
  1063. visit_type_OnOffAuto(v, name, &x86ms->acpi, errp);
  1064. }
  1065. static char *x86_machine_get_oem_id(Object *obj, Error **errp)
  1066. {
  1067. X86MachineState *x86ms = X86_MACHINE(obj);
  1068. return g_strdup(x86ms->oem_id);
  1069. }
  1070. static void x86_machine_set_oem_id(Object *obj, const char *value, Error **errp)
  1071. {
  1072. X86MachineState *x86ms = X86_MACHINE(obj);
  1073. size_t len = strlen(value);
  1074. if (len > 6) {
  1075. error_setg(errp,
  1076. "User specified "X86_MACHINE_OEM_ID" value is bigger than "
  1077. "6 bytes in size");
  1078. return;
  1079. }
  1080. strncpy(x86ms->oem_id, value, 6);
  1081. }
  1082. static char *x86_machine_get_oem_table_id(Object *obj, Error **errp)
  1083. {
  1084. X86MachineState *x86ms = X86_MACHINE(obj);
  1085. return g_strdup(x86ms->oem_table_id);
  1086. }
  1087. static void x86_machine_set_oem_table_id(Object *obj, const char *value,
  1088. Error **errp)
  1089. {
  1090. X86MachineState *x86ms = X86_MACHINE(obj);
  1091. size_t len = strlen(value);
  1092. if (len > 8) {
  1093. error_setg(errp,
  1094. "User specified "X86_MACHINE_OEM_TABLE_ID
  1095. " value is bigger than "
  1096. "8 bytes in size");
  1097. return;
  1098. }
  1099. strncpy(x86ms->oem_table_id, value, 8);
  1100. }
  1101. static void x86_machine_get_bus_lock_ratelimit(Object *obj, Visitor *v,
  1102. const char *name, void *opaque, Error **errp)
  1103. {
  1104. X86MachineState *x86ms = X86_MACHINE(obj);
  1105. uint64_t bus_lock_ratelimit = x86ms->bus_lock_ratelimit;
  1106. visit_type_uint64(v, name, &bus_lock_ratelimit, errp);
  1107. }
  1108. static void x86_machine_set_bus_lock_ratelimit(Object *obj, Visitor *v,
  1109. const char *name, void *opaque, Error **errp)
  1110. {
  1111. X86MachineState *x86ms = X86_MACHINE(obj);
  1112. visit_type_uint64(v, name, &x86ms->bus_lock_ratelimit, errp);
  1113. }
  1114. static void machine_get_sgx_epc(Object *obj, Visitor *v, const char *name,
  1115. void *opaque, Error **errp)
  1116. {
  1117. X86MachineState *x86ms = X86_MACHINE(obj);
  1118. SgxEPCList *list = x86ms->sgx_epc_list;
  1119. visit_type_SgxEPCList(v, name, &list, errp);
  1120. }
  1121. static void machine_set_sgx_epc(Object *obj, Visitor *v, const char *name,
  1122. void *opaque, Error **errp)
  1123. {
  1124. X86MachineState *x86ms = X86_MACHINE(obj);
  1125. SgxEPCList *list;
  1126. list = x86ms->sgx_epc_list;
  1127. visit_type_SgxEPCList(v, name, &x86ms->sgx_epc_list, errp);
  1128. qapi_free_SgxEPCList(list);
  1129. }
  1130. static void x86_machine_initfn(Object *obj)
  1131. {
  1132. X86MachineState *x86ms = X86_MACHINE(obj);
  1133. x86ms->smm = ON_OFF_AUTO_AUTO;
  1134. x86ms->acpi = ON_OFF_AUTO_AUTO;
  1135. x86ms->pci_irq_mask = ACPI_BUILD_PCI_IRQS;
  1136. x86ms->oem_id = g_strndup(ACPI_BUILD_APPNAME6, 6);
  1137. x86ms->oem_table_id = g_strndup(ACPI_BUILD_APPNAME8, 8);
  1138. x86ms->bus_lock_ratelimit = 0;
  1139. }
  1140. static void x86_machine_class_init(ObjectClass *oc, void *data)
  1141. {
  1142. MachineClass *mc = MACHINE_CLASS(oc);
  1143. X86MachineClass *x86mc = X86_MACHINE_CLASS(oc);
  1144. NMIClass *nc = NMI_CLASS(oc);
  1145. mc->cpu_index_to_instance_props = x86_cpu_index_to_props;
  1146. mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
  1147. mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
  1148. x86mc->compat_apic_id_mode = false;
  1149. x86mc->save_tsc_khz = true;
  1150. x86mc->fwcfg_dma_enabled = true;
  1151. nc->nmi_monitor_handler = x86_nmi;
  1152. object_class_property_add(oc, X86_MACHINE_SMM, "OnOffAuto",
  1153. x86_machine_get_smm, x86_machine_set_smm,
  1154. NULL, NULL);
  1155. object_class_property_set_description(oc, X86_MACHINE_SMM,
  1156. "Enable SMM");
  1157. object_class_property_add(oc, X86_MACHINE_ACPI, "OnOffAuto",
  1158. x86_machine_get_acpi, x86_machine_set_acpi,
  1159. NULL, NULL);
  1160. object_class_property_set_description(oc, X86_MACHINE_ACPI,
  1161. "Enable ACPI");
  1162. object_class_property_add_str(oc, X86_MACHINE_OEM_ID,
  1163. x86_machine_get_oem_id,
  1164. x86_machine_set_oem_id);
  1165. object_class_property_set_description(oc, X86_MACHINE_OEM_ID,
  1166. "Override the default value of field OEMID "
  1167. "in ACPI table header."
  1168. "The string may be up to 6 bytes in size");
  1169. object_class_property_add_str(oc, X86_MACHINE_OEM_TABLE_ID,
  1170. x86_machine_get_oem_table_id,
  1171. x86_machine_set_oem_table_id);
  1172. object_class_property_set_description(oc, X86_MACHINE_OEM_TABLE_ID,
  1173. "Override the default value of field OEM Table ID "
  1174. "in ACPI table header."
  1175. "The string may be up to 8 bytes in size");
  1176. object_class_property_add(oc, X86_MACHINE_BUS_LOCK_RATELIMIT, "uint64_t",
  1177. x86_machine_get_bus_lock_ratelimit,
  1178. x86_machine_set_bus_lock_ratelimit, NULL, NULL);
  1179. object_class_property_set_description(oc, X86_MACHINE_BUS_LOCK_RATELIMIT,
  1180. "Set the ratelimit for the bus locks acquired in VMs");
  1181. object_class_property_add(oc, "sgx-epc", "SgxEPC",
  1182. machine_get_sgx_epc, machine_set_sgx_epc,
  1183. NULL, NULL);
  1184. object_class_property_set_description(oc, "sgx-epc",
  1185. "SGX EPC device");
  1186. }
  1187. static const TypeInfo x86_machine_info = {
  1188. .name = TYPE_X86_MACHINE,
  1189. .parent = TYPE_MACHINE,
  1190. .abstract = true,
  1191. .instance_size = sizeof(X86MachineState),
  1192. .instance_init = x86_machine_initfn,
  1193. .class_size = sizeof(X86MachineClass),
  1194. .class_init = x86_machine_class_init,
  1195. .interfaces = (InterfaceInfo[]) {
  1196. { TYPE_NMI },
  1197. { }
  1198. },
  1199. };
  1200. static void x86_machine_register_types(void)
  1201. {
  1202. type_register_static(&x86_machine_info);
  1203. }
  1204. type_init(x86_machine_register_types)