]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/pc.c
hw/i386/pc: Let pc_build_feature_control() take a MachineState argument
[mirror_qemu.git] / hw / i386 / pc.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU PC System Emulator
5fafdf24 3 *
80cabfad 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
80cabfad
FB
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 */
e688df6b 24
b6a0aa05 25#include "qemu/osdep.h"
d471bf3e 26#include "qemu/units.h"
0d09e41a
PB
27#include "hw/i386/pc.h"
28#include "hw/char/serial.h"
bb3d5ea8 29#include "hw/char/parallel.h"
0d09e41a 30#include "hw/i386/apic.h"
54a40293 31#include "hw/i386/topology.h"
87abaa5d 32#include "hw/i386/fw_cfg.h"
54a40293 33#include "sysemu/cpus.h"
0d09e41a 34#include "hw/block/fdc.h"
83c9f4ca
PB
35#include "hw/ide.h"
36#include "hw/pci/pci.h"
2118196b 37#include "hw/pci/pci_bus.h"
0d09e41a
PB
38#include "hw/nvram/fw_cfg.h"
39#include "hw/timer/hpet.h"
a2eb5c0c 40#include "hw/firmware/smbios.h"
83c9f4ca 41#include "hw/loader.h"
ca20cf32 42#include "elf.h"
d6454270 43#include "migration/vmstate.h"
47b43a1f 44#include "multiboot.h"
0d09e41a 45#include "hw/timer/mc146818rtc.h"
55f613ac 46#include "hw/dma/i8257.h"
0d09e41a 47#include "hw/timer/i8254.h"
47973a2d 48#include "hw/input/i8042.h"
64552b6b 49#include "hw/irq.h"
0d09e41a 50#include "hw/audio/pcspk.h"
83c9f4ca
PB
51#include "hw/pci/msi.h"
52#include "hw/sysbus.h"
9c17d615 53#include "sysemu/sysemu.h"
14a48c1d 54#include "sysemu/tcg.h"
e35704ba 55#include "sysemu/numa.h"
9c17d615 56#include "sysemu/kvm.h"
b1c12027 57#include "sysemu/qtest.h"
71e8a915 58#include "sysemu/reset.h"
54d31236 59#include "sysemu/runstate.h"
1d31f66b 60#include "kvm_i386.h"
0d09e41a 61#include "hw/xen/xen.h"
ab969087 62#include "hw/xen/start_info.h"
a19cbfb3 63#include "ui/qemu-spice.h"
022c62cb
PB
64#include "exec/memory.h"
65#include "exec/address-spaces.h"
9c17d615 66#include "sysemu/arch_init.h"
1de7afc9 67#include "qemu/bitmap.h"
0c764a9d 68#include "qemu/config-file.h"
d49b6836 69#include "qemu/error-report.h"
922a01a0 70#include "qemu/option.h"
0445259b 71#include "hw/acpi/acpi.h"
5ff020b7 72#include "hw/acpi/cpu_hotplug.h"
c649983b 73#include "hw/boards.h"
72c194f7 74#include "acpi-build.h"
95bee274 75#include "hw/mem/pc-dimm.h"
e688df6b 76#include "qapi/error.h"
9af23989 77#include "qapi/qapi-visit-common.h"
bf1e8939 78#include "qapi/visitor.h"
2e5b09fd 79#include "hw/core/cpu.h"
1255166b 80#include "hw/nmi.h"
a310e653 81#include "hw/usb.h"
60c5e104 82#include "hw/i386/intel_iommu.h"
489983d6 83#include "hw/net/ne2000-isa.h"
06e0259a 84#include "standard-headers/asm-x86/bootparam.h"
a0a49813
DH
85#include "hw/virtio/virtio-pmem-pci.h"
86#include "hw/mem/memory-device.h"
6f479566
LX
87#include "sysemu/replay.h"
88#include "qapi/qmp/qerror.h"
97fd1ea8 89#include "config-devices.h"
d6d059ca 90#include "e820_memory_layout.h"
80cabfad 91
471fd342
BS
92/* debug PC/ISA interrupts */
93//#define DEBUG_IRQ
94
95#ifdef DEBUG_IRQ
96#define DPRINTF(fmt, ...) \
97 do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
98#else
99#define DPRINTF(fmt, ...)
100#endif
101
dd703b99 102struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
4c5b10b7 103
ab969087
LM
104/* Physical Address of PVH entry point read from kernel ELF NOTE */
105static size_t pvh_start_addr;
106
9aec2e52
CH
107GlobalProperty pc_compat_4_1[] = {};
108const size_t pc_compat_4_1_len = G_N_ELEMENTS(pc_compat_4_1);
109
9bf2650b
CH
110GlobalProperty pc_compat_4_0[] = {};
111const size_t pc_compat_4_0_len = G_N_ELEMENTS(pc_compat_4_0);
112
abd93cc7 113GlobalProperty pc_compat_3_1[] = {
6c36bddf 114 { "intel-iommu", "dma-drain", "off" },
483c6ad4
BP
115 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "off" },
116 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "off" },
9fe8b7be
VK
117 { "Opteron_G4" "-" TYPE_X86_CPU, "npt", "off" },
118 { "Opteron_G4" "-" TYPE_X86_CPU, "nrip-save", "off" },
483c6ad4 119 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "off" },
9fe8b7be
VK
120 { "Opteron_G5" "-" TYPE_X86_CPU, "npt", "off" },
121 { "Opteron_G5" "-" TYPE_X86_CPU, "nrip-save", "off" },
122 { "EPYC" "-" TYPE_X86_CPU, "npt", "off" },
123 { "EPYC" "-" TYPE_X86_CPU, "nrip-save", "off" },
124 { "EPYC-IBPB" "-" TYPE_X86_CPU, "npt", "off" },
125 { "EPYC-IBPB" "-" TYPE_X86_CPU, "nrip-save", "off" },
ecb85fe4
PB
126 { "Skylake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
127 { "Skylake-Client-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
128 { "Skylake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
129 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "mpx", "on" },
130 { "Cascadelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
131 { "Icelake-Client" "-" TYPE_X86_CPU, "mpx", "on" },
132 { "Icelake-Server" "-" TYPE_X86_CPU, "mpx", "on" },
b0a19803 133 { "Cascadelake-Server" "-" TYPE_X86_CPU, "stepping", "5" },
f24c3a79 134 { TYPE_X86_CPU, "x-intel-pt-auto-level", "off" },
abd93cc7
MAL
135};
136const size_t pc_compat_3_1_len = G_N_ELEMENTS(pc_compat_3_1);
137
ddb3235d 138GlobalProperty pc_compat_3_0[] = {
6c36bddf
EH
139 { TYPE_X86_CPU, "x-hv-synic-kvm-only", "on" },
140 { "Skylake-Server" "-" TYPE_X86_CPU, "pku", "off" },
141 { "Skylake-Server-IBRS" "-" TYPE_X86_CPU, "pku", "off" },
ddb3235d
MAL
142};
143const size_t pc_compat_3_0_len = G_N_ELEMENTS(pc_compat_3_0);
144
0d47310b 145GlobalProperty pc_compat_2_12[] = {
6c36bddf
EH
146 { TYPE_X86_CPU, "legacy-cache", "on" },
147 { TYPE_X86_CPU, "topoext", "off" },
148 { "EPYC-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
149 { "EPYC-IBPB-" TYPE_X86_CPU, "xlevel", "0x8000000a" },
0d47310b
MAL
150};
151const size_t pc_compat_2_12_len = G_N_ELEMENTS(pc_compat_2_12);
152
43df70a9 153GlobalProperty pc_compat_2_11[] = {
6c36bddf
EH
154 { TYPE_X86_CPU, "x-migrate-smi-count", "off" },
155 { "Skylake-Server" "-" TYPE_X86_CPU, "clflushopt", "off" },
43df70a9
MAL
156};
157const size_t pc_compat_2_11_len = G_N_ELEMENTS(pc_compat_2_11);
158
503224f4 159GlobalProperty pc_compat_2_10[] = {
6c36bddf
EH
160 { TYPE_X86_CPU, "x-hv-max-vps", "0x40" },
161 { "i440FX-pcihost", "x-pci-hole64-fix", "off" },
162 { "q35-pcihost", "x-pci-hole64-fix", "off" },
503224f4
MAL
163};
164const size_t pc_compat_2_10_len = G_N_ELEMENTS(pc_compat_2_10);
165
3e803152 166GlobalProperty pc_compat_2_9[] = {
6c36bddf 167 { "mch", "extended-tseg-mbytes", "0" },
3e803152
MAL
168};
169const size_t pc_compat_2_9_len = G_N_ELEMENTS(pc_compat_2_9);
170
edc24ccd 171GlobalProperty pc_compat_2_8[] = {
6c36bddf
EH
172 { TYPE_X86_CPU, "tcg-cpuid", "off" },
173 { "kvmclock", "x-mach-use-reliable-get-clock", "off" },
174 { "ICH9-LPC", "x-smi-broadcast", "off" },
175 { TYPE_X86_CPU, "vmware-cpuid-freq", "off" },
176 { "Haswell-" TYPE_X86_CPU, "stepping", "1" },
edc24ccd
MAL
177};
178const size_t pc_compat_2_8_len = G_N_ELEMENTS(pc_compat_2_8);
179
5a995064 180GlobalProperty pc_compat_2_7[] = {
6c36bddf
EH
181 { TYPE_X86_CPU, "l3-cache", "off" },
182 { TYPE_X86_CPU, "full-cpuid-auto-level", "off" },
183 { "Opteron_G3" "-" TYPE_X86_CPU, "family", "15" },
184 { "Opteron_G3" "-" TYPE_X86_CPU, "model", "6" },
185 { "Opteron_G3" "-" TYPE_X86_CPU, "stepping", "1" },
186 { "isa-pcspk", "migrate", "off" },
5a995064
MAL
187};
188const size_t pc_compat_2_7_len = G_N_ELEMENTS(pc_compat_2_7);
189
ff8f261f 190GlobalProperty pc_compat_2_6[] = {
6c36bddf
EH
191 { TYPE_X86_CPU, "cpuid-0xb", "off" },
192 { "vmxnet3", "romfile", "" },
193 { TYPE_X86_CPU, "fill-mtrr-mask", "off" },
194 { "apic-common", "legacy-instance-id", "on", }
ff8f261f
MAL
195};
196const size_t pc_compat_2_6_len = G_N_ELEMENTS(pc_compat_2_6);
197
fe759610
MAL
198GlobalProperty pc_compat_2_5[] = {};
199const size_t pc_compat_2_5_len = G_N_ELEMENTS(pc_compat_2_5);
200
2f99b9c2
MAL
201GlobalProperty pc_compat_2_4[] = {
202 PC_CPU_MODEL_IDS("2.4.0")
6c36bddf
EH
203 { "Haswell-" TYPE_X86_CPU, "abm", "off" },
204 { "Haswell-noTSX-" TYPE_X86_CPU, "abm", "off" },
205 { "Broadwell-" TYPE_X86_CPU, "abm", "off" },
206 { "Broadwell-noTSX-" TYPE_X86_CPU, "abm", "off" },
207 { "host" "-" TYPE_X86_CPU, "host-cache-info", "on" },
208 { TYPE_X86_CPU, "check", "off" },
209 { "qemu64" "-" TYPE_X86_CPU, "sse4a", "on" },
210 { "qemu64" "-" TYPE_X86_CPU, "abm", "on" },
211 { "qemu64" "-" TYPE_X86_CPU, "popcnt", "on" },
212 { "qemu32" "-" TYPE_X86_CPU, "popcnt", "on" },
213 { "Opteron_G2" "-" TYPE_X86_CPU, "rdtscp", "on" },
214 { "Opteron_G3" "-" TYPE_X86_CPU, "rdtscp", "on" },
215 { "Opteron_G4" "-" TYPE_X86_CPU, "rdtscp", "on" },
216 { "Opteron_G5" "-" TYPE_X86_CPU, "rdtscp", "on", }
2f99b9c2
MAL
217};
218const size_t pc_compat_2_4_len = G_N_ELEMENTS(pc_compat_2_4);
219
8995dd90
MAL
220GlobalProperty pc_compat_2_3[] = {
221 PC_CPU_MODEL_IDS("2.3.0")
6c36bddf
EH
222 { TYPE_X86_CPU, "arat", "off" },
223 { "qemu64" "-" TYPE_X86_CPU, "min-level", "4" },
224 { "kvm64" "-" TYPE_X86_CPU, "min-level", "5" },
225 { "pentium3" "-" TYPE_X86_CPU, "min-level", "2" },
226 { "n270" "-" TYPE_X86_CPU, "min-level", "5" },
227 { "Conroe" "-" TYPE_X86_CPU, "min-level", "4" },
228 { "Penryn" "-" TYPE_X86_CPU, "min-level", "4" },
229 { "Nehalem" "-" TYPE_X86_CPU, "min-level", "4" },
230 { "n270" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
231 { "Penryn" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
232 { "Conroe" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
233 { "Nehalem" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
234 { "Westmere" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
235 { "SandyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
236 { "IvyBridge" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
237 { "Haswell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
238 { "Haswell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
239 { "Broadwell" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
240 { "Broadwell-noTSX" "-" TYPE_X86_CPU, "min-xlevel", "0x8000000a" },
241 { TYPE_X86_CPU, "kvm-no-smi-migration", "on" },
8995dd90
MAL
242};
243const size_t pc_compat_2_3_len = G_N_ELEMENTS(pc_compat_2_3);
244
1c30044e
MAL
245GlobalProperty pc_compat_2_2[] = {
246 PC_CPU_MODEL_IDS("2.2.0")
6c36bddf
EH
247 { "kvm64" "-" TYPE_X86_CPU, "vme", "off" },
248 { "kvm32" "-" TYPE_X86_CPU, "vme", "off" },
249 { "Conroe" "-" TYPE_X86_CPU, "vme", "off" },
250 { "Penryn" "-" TYPE_X86_CPU, "vme", "off" },
251 { "Nehalem" "-" TYPE_X86_CPU, "vme", "off" },
252 { "Westmere" "-" TYPE_X86_CPU, "vme", "off" },
253 { "SandyBridge" "-" TYPE_X86_CPU, "vme", "off" },
254 { "Haswell" "-" TYPE_X86_CPU, "vme", "off" },
255 { "Broadwell" "-" TYPE_X86_CPU, "vme", "off" },
256 { "Opteron_G1" "-" TYPE_X86_CPU, "vme", "off" },
257 { "Opteron_G2" "-" TYPE_X86_CPU, "vme", "off" },
258 { "Opteron_G3" "-" TYPE_X86_CPU, "vme", "off" },
259 { "Opteron_G4" "-" TYPE_X86_CPU, "vme", "off" },
260 { "Opteron_G5" "-" TYPE_X86_CPU, "vme", "off" },
261 { "Haswell" "-" TYPE_X86_CPU, "f16c", "off" },
262 { "Haswell" "-" TYPE_X86_CPU, "rdrand", "off" },
263 { "Broadwell" "-" TYPE_X86_CPU, "f16c", "off" },
264 { "Broadwell" "-" TYPE_X86_CPU, "rdrand", "off" },
1c30044e
MAL
265};
266const size_t pc_compat_2_2_len = G_N_ELEMENTS(pc_compat_2_2);
267
c4fc5695
MAL
268GlobalProperty pc_compat_2_1[] = {
269 PC_CPU_MODEL_IDS("2.1.0")
6c36bddf
EH
270 { "coreduo" "-" TYPE_X86_CPU, "vmx", "on" },
271 { "core2duo" "-" TYPE_X86_CPU, "vmx", "on" },
c4fc5695
MAL
272};
273const size_t pc_compat_2_1_len = G_N_ELEMENTS(pc_compat_2_1);
274
a310e653
MAL
275GlobalProperty pc_compat_2_0[] = {
276 PC_CPU_MODEL_IDS("2.0.0")
6c36bddf
EH
277 { "virtio-scsi-pci", "any_layout", "off" },
278 { "PIIX4_PM", "memory-hotplug-support", "off" },
279 { "apic", "version", "0x11" },
280 { "nec-usb-xhci", "superspeed-ports-first", "off" },
281 { "nec-usb-xhci", "force-pcie-endcap", "on" },
282 { "pci-serial", "prog_if", "0" },
283 { "pci-serial-2x", "prog_if", "0" },
284 { "pci-serial-4x", "prog_if", "0" },
285 { "virtio-net-pci", "guest_announce", "off" },
286 { "ICH9-LPC", "memory-hotplug-support", "off" },
287 { "xio3130-downstream", COMPAT_PROP_PCP, "off" },
288 { "ioh3420", COMPAT_PROP_PCP, "off" },
a310e653
MAL
289};
290const size_t pc_compat_2_0_len = G_N_ELEMENTS(pc_compat_2_0);
291
292GlobalProperty pc_compat_1_7[] = {
293 PC_CPU_MODEL_IDS("1.7.0")
6c36bddf
EH
294 { TYPE_USB_DEVICE, "msos-desc", "no" },
295 { "PIIX4_PM", "acpi-pci-hotplug-with-bridge-support", "off" },
296 { "hpet", HPET_INTCAP, "4" },
a310e653
MAL
297};
298const size_t pc_compat_1_7_len = G_N_ELEMENTS(pc_compat_1_7);
299
300GlobalProperty pc_compat_1_6[] = {
301 PC_CPU_MODEL_IDS("1.6.0")
6c36bddf
EH
302 { "e1000", "mitigation", "off" },
303 { "qemu64-" TYPE_X86_CPU, "model", "2" },
304 { "qemu32-" TYPE_X86_CPU, "model", "3" },
305 { "i440FX-pcihost", "short_root_bus", "1" },
306 { "q35-pcihost", "short_root_bus", "1" },
a310e653
MAL
307};
308const size_t pc_compat_1_6_len = G_N_ELEMENTS(pc_compat_1_6);
309
310GlobalProperty pc_compat_1_5[] = {
311 PC_CPU_MODEL_IDS("1.5.0")
6c36bddf
EH
312 { "Conroe-" TYPE_X86_CPU, "model", "2" },
313 { "Conroe-" TYPE_X86_CPU, "min-level", "2" },
314 { "Penryn-" TYPE_X86_CPU, "model", "2" },
315 { "Penryn-" TYPE_X86_CPU, "min-level", "2" },
316 { "Nehalem-" TYPE_X86_CPU, "model", "2" },
317 { "Nehalem-" TYPE_X86_CPU, "min-level", "2" },
318 { "virtio-net-pci", "any_layout", "off" },
319 { TYPE_X86_CPU, "pmu", "on" },
320 { "i440FX-pcihost", "short_root_bus", "0" },
321 { "q35-pcihost", "short_root_bus", "0" },
a310e653
MAL
322};
323const size_t pc_compat_1_5_len = G_N_ELEMENTS(pc_compat_1_5);
324
325GlobalProperty pc_compat_1_4[] = {
326 PC_CPU_MODEL_IDS("1.4.0")
6c36bddf
EH
327 { "scsi-hd", "discard_granularity", "0" },
328 { "scsi-cd", "discard_granularity", "0" },
329 { "scsi-disk", "discard_granularity", "0" },
330 { "ide-hd", "discard_granularity", "0" },
331 { "ide-cd", "discard_granularity", "0" },
332 { "ide-drive", "discard_granularity", "0" },
333 { "virtio-blk-pci", "discard_granularity", "0" },
334 /* DEV_NVECTORS_UNSPECIFIED as a uint32_t string: */
335 { "virtio-serial-pci", "vectors", "0xFFFFFFFF" },
336 { "virtio-net-pci", "ctrl_guest_offloads", "off" },
337 { "e1000", "romfile", "pxe-e1000.rom" },
338 { "ne2k_pci", "romfile", "pxe-ne2k_pci.rom" },
339 { "pcnet", "romfile", "pxe-pcnet.rom" },
340 { "rtl8139", "romfile", "pxe-rtl8139.rom" },
341 { "virtio-net-pci", "romfile", "pxe-virtio.rom" },
342 { "486-" TYPE_X86_CPU, "model", "0" },
343 { "n270" "-" TYPE_X86_CPU, "movbe", "off" },
344 { "Westmere" "-" TYPE_X86_CPU, "pclmulqdq", "off" },
a310e653
MAL
345};
346const size_t pc_compat_1_4_len = G_N_ELEMENTS(pc_compat_1_4);
347
b881fbe9 348void gsi_handler(void *opaque, int n, int level)
1452411b 349{
b881fbe9 350 GSIState *s = opaque;
1452411b 351
b881fbe9
JK
352 DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
353 if (n < ISA_NUM_IRQS) {
354 qemu_set_irq(s->i8259_irq[n], level);
1632dc6a 355 }
b881fbe9 356 qemu_set_irq(s->ioapic_irq[n], level);
2e9947d2 357}
1452411b 358
258711c6
JG
359static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
360 unsigned size)
80cabfad
FB
361{
362}
363
c02e1eac
JG
364static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
365{
a6fc23e5 366 return 0xffffffffffffffffULL;
c02e1eac
JG
367}
368
f929aad6 369/* MSDOS compatibility mode FPU exception support */
d537cf6c 370static qemu_irq ferr_irq;
8e78eb28
IY
371
372void pc_register_ferr_irq(qemu_irq irq)
373{
374 ferr_irq = irq;
375}
376
f929aad6
FB
377/* XXX: add IGNNE support */
378void cpu_set_ferr(CPUX86State *s)
379{
d537cf6c 380 qemu_irq_raise(ferr_irq);
f929aad6
FB
381}
382
258711c6
JG
383static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
384 unsigned size)
f929aad6 385{
d537cf6c 386 qemu_irq_lower(ferr_irq);
f929aad6
FB
387}
388
c02e1eac
JG
389static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
390{
a6fc23e5 391 return 0xffffffffffffffffULL;
c02e1eac
JG
392}
393
28ab0e2e 394/* TSC handling */
28ab0e2e
FB
395uint64_t cpu_get_tsc(CPUX86State *env)
396{
4a1418e0 397 return cpu_get_ticks();
28ab0e2e
FB
398}
399
3de388f6 400/* IRQ handling */
4a8fa5dc 401int cpu_get_pic_interrupt(CPUX86State *env)
3de388f6 402{
6aa9e42f 403 X86CPU *cpu = env_archcpu(env);
3de388f6
FB
404 int intno;
405
bb93e099
WL
406 if (!kvm_irqchip_in_kernel()) {
407 intno = apic_get_interrupt(cpu->apic_state);
408 if (intno >= 0) {
409 return intno;
410 }
411 /* read the irq from the PIC */
412 if (!apic_accept_pic_intr(cpu->apic_state)) {
413 return -1;
414 }
cf6d64bf 415 }
0e21e12b 416
3de388f6
FB
417 intno = pic_read_irq(isa_pic);
418 return intno;
419}
420
d537cf6c 421static void pic_irq_request(void *opaque, int irq, int level)
3de388f6 422{
182735ef
AF
423 CPUState *cs = first_cpu;
424 X86CPU *cpu = X86_CPU(cs);
a5b38b51 425
471fd342 426 DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
bb93e099 427 if (cpu->apic_state && !kvm_irqchip_in_kernel()) {
bdc44640 428 CPU_FOREACH(cs) {
182735ef 429 cpu = X86_CPU(cs);
02e51483
CF
430 if (apic_accept_pic_intr(cpu->apic_state)) {
431 apic_deliver_pic_intr(cpu->apic_state, level);
cf6d64bf 432 }
d5529471
AJ
433 }
434 } else {
d8ed887b 435 if (level) {
c3affe56 436 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
d8ed887b
AF
437 } else {
438 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
439 }
a5b38b51 440 }
3de388f6
FB
441}
442
b0a21b53
FB
443/* PC cmos mappings */
444
80cabfad
FB
445#define REG_EQUIPMENT_BYTE 0x14
446
bda05509 447int cmos_get_fd_drive_type(FloppyDriveType fd0)
777428f2
FB
448{
449 int val;
450
451 switch (fd0) {
2da44dd0 452 case FLOPPY_DRIVE_TYPE_144:
777428f2
FB
453 /* 1.44 Mb 3"5 drive */
454 val = 4;
455 break;
2da44dd0 456 case FLOPPY_DRIVE_TYPE_288:
777428f2
FB
457 /* 2.88 Mb 3"5 drive */
458 val = 5;
459 break;
2da44dd0 460 case FLOPPY_DRIVE_TYPE_120:
777428f2
FB
461 /* 1.2 Mb 5"5 drive */
462 val = 2;
463 break;
2da44dd0 464 case FLOPPY_DRIVE_TYPE_NONE:
777428f2
FB
465 default:
466 val = 0;
467 break;
468 }
469 return val;
470}
471
9139046c
MA
472static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
473 int16_t cylinders, int8_t heads, int8_t sectors)
ba6c2377 474{
ba6c2377
FB
475 rtc_set_memory(s, type_ofs, 47);
476 rtc_set_memory(s, info_ofs, cylinders);
477 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
478 rtc_set_memory(s, info_ofs + 2, heads);
479 rtc_set_memory(s, info_ofs + 3, 0xff);
480 rtc_set_memory(s, info_ofs + 4, 0xff);
481 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
482 rtc_set_memory(s, info_ofs + 6, cylinders);
483 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
484 rtc_set_memory(s, info_ofs + 8, sectors);
485}
486
6ac0e82d
AZ
487/* convert boot_device letter to something recognizable by the bios */
488static int boot_device2nibble(char boot_device)
489{
490 switch(boot_device) {
491 case 'a':
492 case 'b':
493 return 0x01; /* floppy boot */
494 case 'c':
495 return 0x02; /* hard drive boot */
496 case 'd':
497 return 0x03; /* CD-ROM boot */
498 case 'n':
499 return 0x04; /* Network boot */
500 }
501 return 0;
502}
503
ddcd5531 504static void set_boot_dev(ISADevice *s, const char *boot_device, Error **errp)
0ecdffbb
AJ
505{
506#define PC_MAX_BOOT_DEVICES 3
0ecdffbb
AJ
507 int nbds, bds[3] = { 0, };
508 int i;
509
510 nbds = strlen(boot_device);
511 if (nbds > PC_MAX_BOOT_DEVICES) {
ddcd5531
GA
512 error_setg(errp, "Too many boot devices for PC");
513 return;
0ecdffbb
AJ
514 }
515 for (i = 0; i < nbds; i++) {
516 bds[i] = boot_device2nibble(boot_device[i]);
517 if (bds[i] == 0) {
ddcd5531
GA
518 error_setg(errp, "Invalid boot device for PC: '%c'",
519 boot_device[i]);
520 return;
0ecdffbb
AJ
521 }
522 }
523 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
d9346e81 524 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
0ecdffbb
AJ
525}
526
ddcd5531 527static void pc_boot_set(void *opaque, const char *boot_device, Error **errp)
d9346e81 528{
ddcd5531 529 set_boot_dev(opaque, boot_device, errp);
d9346e81
MA
530}
531
7444ca4e
LE
532static void pc_cmos_init_floppy(ISADevice *rtc_state, ISADevice *floppy)
533{
534 int val, nb, i;
2da44dd0
JS
535 FloppyDriveType fd_type[2] = { FLOPPY_DRIVE_TYPE_NONE,
536 FLOPPY_DRIVE_TYPE_NONE };
7444ca4e
LE
537
538 /* floppy type */
539 if (floppy) {
540 for (i = 0; i < 2; i++) {
541 fd_type[i] = isa_fdc_get_drive_type(floppy, i);
542 }
543 }
544 val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
545 cmos_get_fd_drive_type(fd_type[1]);
546 rtc_set_memory(rtc_state, 0x10, val);
547
548 val = rtc_get_memory(rtc_state, REG_EQUIPMENT_BYTE);
549 nb = 0;
2da44dd0 550 if (fd_type[0] != FLOPPY_DRIVE_TYPE_NONE) {
7444ca4e
LE
551 nb++;
552 }
2da44dd0 553 if (fd_type[1] != FLOPPY_DRIVE_TYPE_NONE) {
7444ca4e
LE
554 nb++;
555 }
556 switch (nb) {
557 case 0:
558 break;
559 case 1:
560 val |= 0x01; /* 1 drive, ready for boot */
561 break;
562 case 2:
563 val |= 0x41; /* 2 drives, ready for boot */
564 break;
565 }
566 rtc_set_memory(rtc_state, REG_EQUIPMENT_BYTE, val);
567}
568
c0897e0c
MA
569typedef struct pc_cmos_init_late_arg {
570 ISADevice *rtc_state;
9139046c 571 BusState *idebus[2];
c0897e0c
MA
572} pc_cmos_init_late_arg;
573
b86f4613
LE
574typedef struct check_fdc_state {
575 ISADevice *floppy;
576 bool multiple;
577} CheckFdcState;
578
579static int check_fdc(Object *obj, void *opaque)
580{
581 CheckFdcState *state = opaque;
582 Object *fdc;
583 uint32_t iobase;
584 Error *local_err = NULL;
585
586 fdc = object_dynamic_cast(obj, TYPE_ISA_FDC);
587 if (!fdc) {
588 return 0;
589 }
590
1ea1572a 591 iobase = object_property_get_uint(obj, "iobase", &local_err);
b86f4613
LE
592 if (local_err || iobase != 0x3f0) {
593 error_free(local_err);
594 return 0;
595 }
596
597 if (state->floppy) {
598 state->multiple = true;
599 } else {
600 state->floppy = ISA_DEVICE(obj);
601 }
602 return 0;
603}
604
605static const char * const fdc_container_path[] = {
606 "/unattached", "/peripheral", "/peripheral-anon"
607};
608
424e4a87
RK
609/*
610 * Locate the FDC at IO address 0x3f0, in order to configure the CMOS registers
611 * and ACPI objects.
612 */
613ISADevice *pc_find_fdc0(void)
614{
615 int i;
616 Object *container;
617 CheckFdcState state = { 0 };
618
619 for (i = 0; i < ARRAY_SIZE(fdc_container_path); i++) {
620 container = container_get(qdev_get_machine(), fdc_container_path[i]);
621 object_child_foreach(container, check_fdc, &state);
622 }
623
624 if (state.multiple) {
3dc6f869
AF
625 warn_report("multiple floppy disk controllers with "
626 "iobase=0x3f0 have been found");
433672b0 627 error_printf("the one being picked for CMOS setup might not reflect "
9e5d2c52 628 "your intent");
424e4a87
RK
629 }
630
631 return state.floppy;
632}
633
c0897e0c
MA
634static void pc_cmos_init_late(void *opaque)
635{
636 pc_cmos_init_late_arg *arg = opaque;
637 ISADevice *s = arg->rtc_state;
9139046c
MA
638 int16_t cylinders;
639 int8_t heads, sectors;
c0897e0c 640 int val;
2adc99b2 641 int i, trans;
c0897e0c 642
9139046c 643 val = 0;
272f0428
CP
644 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 0,
645 &cylinders, &heads, &sectors) >= 0) {
9139046c
MA
646 cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
647 val |= 0xf0;
648 }
272f0428
CP
649 if (arg->idebus[0] && ide_get_geometry(arg->idebus[0], 1,
650 &cylinders, &heads, &sectors) >= 0) {
9139046c
MA
651 cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
652 val |= 0x0f;
653 }
654 rtc_set_memory(s, 0x12, val);
c0897e0c
MA
655
656 val = 0;
657 for (i = 0; i < 4; i++) {
9139046c
MA
658 /* NOTE: ide_get_geometry() returns the physical
659 geometry. It is always such that: 1 <= sects <= 63, 1
660 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
661 geometry can be different if a translation is done. */
272f0428
CP
662 if (arg->idebus[i / 2] &&
663 ide_get_geometry(arg->idebus[i / 2], i % 2,
9139046c 664 &cylinders, &heads, &sectors) >= 0) {
2adc99b2
MA
665 trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
666 assert((trans & ~3) == 0);
667 val |= trans << (i * 2);
c0897e0c
MA
668 }
669 }
670 rtc_set_memory(s, 0x39, val);
671
424e4a87 672 pc_cmos_init_floppy(s, pc_find_fdc0());
b86f4613 673
c0897e0c
MA
674 qemu_unregister_reset(pc_cmos_init_late, opaque);
675}
676
23d30407 677void pc_cmos_init(PCMachineState *pcms,
220a8846 678 BusState *idebus0, BusState *idebus1,
63ffb564 679 ISADevice *s)
80cabfad 680{
7444ca4e 681 int val;
c0897e0c 682 static pc_cmos_init_late_arg arg;
b0a21b53 683
b0a21b53 684 /* various important CMOS locations needed by PC/Bochs bios */
80cabfad
FB
685
686 /* memory size */
e89001f7 687 /* base memory (first MiB) */
d471bf3e 688 val = MIN(pcms->below_4g_mem_size / KiB, 640);
333190eb
FB
689 rtc_set_memory(s, 0x15, val);
690 rtc_set_memory(s, 0x16, val >> 8);
e89001f7 691 /* extended memory (next 64MiB) */
d471bf3e
PB
692 if (pcms->below_4g_mem_size > 1 * MiB) {
693 val = (pcms->below_4g_mem_size - 1 * MiB) / KiB;
e89001f7
MA
694 } else {
695 val = 0;
696 }
80cabfad
FB
697 if (val > 65535)
698 val = 65535;
b0a21b53
FB
699 rtc_set_memory(s, 0x17, val);
700 rtc_set_memory(s, 0x18, val >> 8);
701 rtc_set_memory(s, 0x30, val);
702 rtc_set_memory(s, 0x31, val >> 8);
e89001f7 703 /* memory between 16MiB and 4GiB */
d471bf3e
PB
704 if (pcms->below_4g_mem_size > 16 * MiB) {
705 val = (pcms->below_4g_mem_size - 16 * MiB) / (64 * KiB);
e89001f7 706 } else {
9da98861 707 val = 0;
e89001f7 708 }
80cabfad
FB
709 if (val > 65535)
710 val = 65535;
b0a21b53
FB
711 rtc_set_memory(s, 0x34, val);
712 rtc_set_memory(s, 0x35, val >> 8);
e89001f7 713 /* memory above 4GiB */
88076854 714 val = pcms->above_4g_mem_size / 65536;
e89001f7
MA
715 rtc_set_memory(s, 0x5b, val);
716 rtc_set_memory(s, 0x5c, val >> 8);
717 rtc_set_memory(s, 0x5d, val >> 16);
3b46e624 718
23d30407 719 object_property_add_link(OBJECT(pcms), "rtc_state",
2d996150 720 TYPE_ISA_DEVICE,
ec68007a 721 (Object **)&pcms->rtc,
2d996150 722 object_property_allow_set_link,
265b578c 723 OBJ_PROP_LINK_STRONG, &error_abort);
23d30407 724 object_property_set_link(OBJECT(pcms), OBJECT(s),
2d996150 725 "rtc_state", &error_abort);
298e01b6 726
007b0657 727 set_boot_dev(s, MACHINE(pcms)->boot_order, &error_fatal);
80cabfad 728
b0a21b53 729 val = 0;
b0a21b53
FB
730 val |= 0x02; /* FPU is there */
731 val |= 0x04; /* PS/2 mouse installed */
732 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
733
b86f4613 734 /* hard drives and FDC */
c0897e0c 735 arg.rtc_state = s;
9139046c
MA
736 arg.idebus[0] = idebus0;
737 arg.idebus[1] = idebus1;
c0897e0c 738 qemu_register_reset(pc_cmos_init_late, &arg);
80cabfad
FB
739}
740
a0881c64
AF
741#define TYPE_PORT92 "port92"
742#define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
743
4b78a802
BS
744/* port 92 stuff: could be split off */
745typedef struct Port92State {
a0881c64
AF
746 ISADevice parent_obj;
747
23af670e 748 MemoryRegion io;
4b78a802 749 uint8_t outport;
d812b3d6 750 qemu_irq a20_out;
4b78a802
BS
751} Port92State;
752
93ef4192
AG
753static void port92_write(void *opaque, hwaddr addr, uint64_t val,
754 unsigned size)
4b78a802
BS
755{
756 Port92State *s = opaque;
4700a316 757 int oldval = s->outport;
4b78a802 758
c5539cb4 759 DPRINTF("port92: write 0x%02" PRIx64 "\n", val);
4b78a802 760 s->outport = val;
d812b3d6 761 qemu_set_irq(s->a20_out, (val >> 1) & 1);
4700a316 762 if ((val & 1) && !(oldval & 1)) {
cf83f140 763 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
4b78a802
BS
764 }
765}
766
93ef4192
AG
767static uint64_t port92_read(void *opaque, hwaddr addr,
768 unsigned size)
4b78a802
BS
769{
770 Port92State *s = opaque;
771 uint32_t ret;
772
773 ret = s->outport;
774 DPRINTF("port92: read 0x%02x\n", ret);
775 return ret;
776}
777
d80fe99d 778static void port92_init(ISADevice *dev, qemu_irq a20_out)
4b78a802 779{
d80fe99d 780 qdev_connect_gpio_out_named(DEVICE(dev), PORT92_A20_LINE, 0, a20_out);
4b78a802
BS
781}
782
783static const VMStateDescription vmstate_port92_isa = {
784 .name = "port92",
785 .version_id = 1,
786 .minimum_version_id = 1,
d49805ae 787 .fields = (VMStateField[]) {
4b78a802
BS
788 VMSTATE_UINT8(outport, Port92State),
789 VMSTATE_END_OF_LIST()
790 }
791};
792
793static void port92_reset(DeviceState *d)
794{
a0881c64 795 Port92State *s = PORT92(d);
4b78a802
BS
796
797 s->outport &= ~1;
798}
799
23af670e 800static const MemoryRegionOps port92_ops = {
93ef4192
AG
801 .read = port92_read,
802 .write = port92_write,
803 .impl = {
804 .min_access_size = 1,
805 .max_access_size = 1,
806 },
807 .endianness = DEVICE_LITTLE_ENDIAN,
23af670e
RH
808};
809
db895a1e 810static void port92_initfn(Object *obj)
4b78a802 811{
db895a1e 812 Port92State *s = PORT92(obj);
4b78a802 813
1437c94b 814 memory_region_init_io(&s->io, OBJECT(s), &port92_ops, s, "port92", 1);
23af670e 815
4b78a802 816 s->outport = 0;
d812b3d6
EV
817
818 qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, PORT92_A20_LINE, 1);
db895a1e
AF
819}
820
821static void port92_realizefn(DeviceState *dev, Error **errp)
822{
823 ISADevice *isadev = ISA_DEVICE(dev);
824 Port92State *s = PORT92(dev);
825
826 isa_register_ioport(isadev, &s->io, 0x92);
4b78a802
BS
827}
828
8f04ee08
AL
829static void port92_class_initfn(ObjectClass *klass, void *data)
830{
39bffca2 831 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e 832
db895a1e 833 dc->realize = port92_realizefn;
39bffca2
AL
834 dc->reset = port92_reset;
835 dc->vmsd = &vmstate_port92_isa;
f3b17640
MA
836 /*
837 * Reason: unlike ordinary ISA devices, this one needs additional
838 * wiring: its A20 output line needs to be wired up by
839 * port92_init().
840 */
e90f2a8c 841 dc->user_creatable = false;
8f04ee08
AL
842}
843
8c43a6f0 844static const TypeInfo port92_info = {
a0881c64 845 .name = TYPE_PORT92,
39bffca2
AL
846 .parent = TYPE_ISA_DEVICE,
847 .instance_size = sizeof(Port92State),
db895a1e 848 .instance_init = port92_initfn,
39bffca2 849 .class_init = port92_class_initfn,
4b78a802
BS
850};
851
83f7d43a 852static void port92_register_types(void)
4b78a802 853{
39bffca2 854 type_register_static(&port92_info);
4b78a802 855}
83f7d43a
AF
856
857type_init(port92_register_types)
4b78a802 858
956a3e6b 859static void handle_a20_line_change(void *opaque, int irq, int level)
59b8ad81 860{
cc36a7a2 861 X86CPU *cpu = opaque;
e1a23744 862
956a3e6b 863 /* XXX: send to all CPUs ? */
4b78a802 864 /* XXX: add logic to handle multiple A20 line sources */
cc36a7a2 865 x86_cpu_set_a20(cpu, level);
e1a23744
FB
866}
867
54a40293
EH
868/* Calculates initial APIC ID for a specific CPU index
869 *
870 * Currently we need to be able to calculate the APIC ID from the CPU index
871 * alone (without requiring a CPU object), as the QEMU<->Seabios interfaces have
872 * no concept of "CPU index", and the NUMA tables on fw_cfg need the APIC ID of
873 * all CPUs up to max_cpus.
874 */
457cfccc
EH
875static uint32_t x86_cpu_apic_id_from_index(PCMachineState *pcms,
876 unsigned int cpu_index)
54a40293 877{
0e11fc69 878 MachineState *ms = MACHINE(pcms);
457cfccc 879 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
54a40293
EH
880 uint32_t correct_id;
881 static bool warned;
882
d65af288 883 correct_id = x86_apicid_from_cpu_idx(pcms->smp_dies, ms->smp.cores,
0e11fc69 884 ms->smp.threads, cpu_index);
457cfccc 885 if (pcmc->compat_apic_id_mode) {
b1c12027 886 if (cpu_index != correct_id && !warned && !qtest_enabled()) {
54a40293
EH
887 error_report("APIC IDs set in compatibility mode, "
888 "CPU topology won't match the configuration");
889 warned = true;
890 }
891 return cpu_index;
892 } else {
893 return correct_id;
894 }
895}
896
e0e949d7 897static void fw_cfg_build_smbios(MachineState *ms, FWCfgState *fw_cfg)
80cabfad 898{
c97294ec
GS
899 uint8_t *smbios_tables, *smbios_anchor;
900 size_t smbios_tables_len, smbios_anchor_len;
89cc4a27
WH
901 struct smbios_phys_mem_area *mem_array;
902 unsigned i, array_count;
38690a1c 903 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu);
f2098f48
IM
904
905 /* tell smbios about cpuid version and features */
906 smbios_set_cpuid(cpu->env.cpuid_version, cpu->env.features[FEAT_1_EDX]);
5fd0a9d4 907
a0628599 908 smbios_tables = smbios_get_table_legacy(ms, &smbios_tables_len);
5fd0a9d4 909 if (smbios_tables) {
14fe2807 910 fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
5fd0a9d4
WH
911 smbios_tables, smbios_tables_len);
912 }
913
89cc4a27
WH
914 /* build the array of physical mem area from e820 table */
915 mem_array = g_malloc0(sizeof(*mem_array) * e820_get_num_entries());
916 for (i = 0, array_count = 0; i < e820_get_num_entries(); i++) {
917 uint64_t addr, len;
918
919 if (e820_get_entry(i, E820_RAM, &addr, &len)) {
920 mem_array[array_count].address = addr;
921 mem_array[array_count].length = len;
922 array_count++;
923 }
924 }
a0628599 925 smbios_get_tables(ms, mem_array, array_count,
89cc4a27 926 &smbios_tables, &smbios_tables_len,
5fd0a9d4 927 &smbios_anchor, &smbios_anchor_len);
89cc4a27
WH
928 g_free(mem_array);
929
5fd0a9d4 930 if (smbios_anchor) {
14fe2807 931 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-tables",
5fd0a9d4 932 smbios_tables, smbios_tables_len);
14fe2807 933 fw_cfg_add_file(fw_cfg, "etc/smbios/smbios-anchor",
5fd0a9d4
WH
934 smbios_anchor, smbios_anchor_len);
935 }
936}
937
bd802bd9 938static FWCfgState *fw_cfg_arch_create(MachineState *ms,
57e23e78
PMD
939 uint16_t boot_cpus,
940 uint16_t apic_id_limit)
5fd0a9d4
WH
941{
942 FWCfgState *fw_cfg;
11c2fd3e 943 uint64_t *numa_fw_cfg;
ea265072 944 int i;
bd802bd9
PB
945 MachineClass *mc = MACHINE_GET_CLASS(ms);
946 const CPUArchIdList *cpus = mc->possible_cpu_arch_ids(ms);
aa570207 947 int nb_numa_nodes = ms->numa_state->num_nodes;
3cce6243 948
0f432b30
PMD
949 fw_cfg = fw_cfg_init_io_dma(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4,
950 &address_space_memory);
524acbe9 951 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, boot_cpus);
c886fc4c 952
1d934e89
EH
953 /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86:
954 *
a3abd0f2
IM
955 * For machine types prior to 1.8, SeaBIOS needs FW_CFG_MAX_CPUS for
956 * building MPTable, ACPI MADT, ACPI CPU hotplug and ACPI SRAT table,
957 * that tables are based on xAPIC ID and QEMU<->SeaBIOS interface
958 * for CPU hotplug also uses APIC ID and not "CPU index".
959 * This means that FW_CFG_MAX_CPUS is not the "maximum number of CPUs",
960 * but the "limit to the APIC ID values SeaBIOS may see".
1d934e89 961 *
a3abd0f2
IM
962 * So for compatibility reasons with old BIOSes we are stuck with
963 * "etc/max-cpus" actually being apic_id_limit
1d934e89 964 */
264b4857 965 fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, apic_id_limit);
905fdcb5 966 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
089da572
MA
967 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES,
968 acpi_tables, acpi_tables_len);
9b5b76d4 969 fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
b6f6e3d3 970
089da572 971 fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE,
7d67110f
GH
972 &e820_reserve, sizeof(e820_reserve));
973 fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
42d400ac 974 sizeof(struct e820_entry) * e820_get_num_entries());
11c2fd3e 975
089da572 976 fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg));
11c2fd3e
AL
977 /* allocate memory for the NUMA channel: one (64bit) word for the number
978 * of nodes, one word for each VCPU->node and one word for each node to
979 * hold the amount of memory.
980 */
264b4857 981 numa_fw_cfg = g_new0(uint64_t, 1 + apic_id_limit + nb_numa_nodes);
11c2fd3e 982 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
ea265072
IM
983 for (i = 0; i < cpus->len; i++) {
984 unsigned int apic_id = cpus->cpus[i].arch_id;
264b4857 985 assert(apic_id < apic_id_limit);
d41f3e75 986 numa_fw_cfg[apic_id + 1] = cpu_to_le64(cpus->cpus[i].props.node_id);
11c2fd3e
AL
987 }
988 for (i = 0; i < nb_numa_nodes; i++) {
264b4857 989 numa_fw_cfg[apic_id_limit + 1 + i] =
7e721e7b 990 cpu_to_le64(ms->numa_state->nodes[i].node_mem);
11c2fd3e 991 }
089da572 992 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
264b4857 993 (1 + apic_id_limit + nb_numa_nodes) *
1d934e89 994 sizeof(*numa_fw_cfg));
bf483392
AG
995
996 return fw_cfg;
80cabfad
FB
997}
998
642a4f96
TS
999static long get_file_size(FILE *f)
1000{
1001 long where, size;
1002
1003 /* XXX: on Unix systems, using fstat() probably makes more sense */
1004
1005 where = ftell(f);
1006 fseek(f, 0, SEEK_END);
1007 size = ftell(f);
1008 fseek(f, where, SEEK_SET);
1009
1010 return size;
1011}
1012
3cbeb524
AB
1013struct setup_data {
1014 uint64_t next;
1015 uint32_t type;
1016 uint32_t len;
1017 uint8_t data[0];
1018} __attribute__((packed));
1019
ab969087
LM
1020
1021/*
1022 * The entry point into the kernel for PVH boot is different from
1023 * the native entry point. The PVH entry is defined by the x86/HVM
1024 * direct boot ABI and is available in an ELFNOTE in the kernel binary.
1025 *
1026 * This function is passed to load_elf() when it is called from
1027 * load_elfboot() which then additionally checks for an ELF Note of
1028 * type XEN_ELFNOTE_PHYS32_ENTRY and passes it to this function to
1029 * parse the PVH entry address from the ELF Note.
1030 *
1031 * Due to trickery in elf_opts.h, load_elf() is actually available as
1032 * load_elf32() or load_elf64() and this routine needs to be able
1033 * to deal with being called as 32 or 64 bit.
1034 *
1035 * The address of the PVH entry point is saved to the 'pvh_start_addr'
1036 * global variable. (although the entry point is 32-bit, the kernel
1037 * binary can be either 32-bit or 64-bit).
1038 */
1039static uint64_t read_pvh_start_addr(void *arg1, void *arg2, bool is64)
1040{
1041 size_t *elf_note_data_addr;
1042
1043 /* Check if ELF Note header passed in is valid */
1044 if (arg1 == NULL) {
1045 return 0;
1046 }
1047
1048 if (is64) {
1049 struct elf64_note *nhdr64 = (struct elf64_note *)arg1;
1050 uint64_t nhdr_size64 = sizeof(struct elf64_note);
1051 uint64_t phdr_align = *(uint64_t *)arg2;
1052 uint64_t nhdr_namesz = nhdr64->n_namesz;
1053
1054 elf_note_data_addr =
1055 ((void *)nhdr64) + nhdr_size64 +
1056 QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
1057 } else {
1058 struct elf32_note *nhdr32 = (struct elf32_note *)arg1;
1059 uint32_t nhdr_size32 = sizeof(struct elf32_note);
1060 uint32_t phdr_align = *(uint32_t *)arg2;
1061 uint32_t nhdr_namesz = nhdr32->n_namesz;
1062
1063 elf_note_data_addr =
1064 ((void *)nhdr32) + nhdr_size32 +
1065 QEMU_ALIGN_UP(nhdr_namesz, phdr_align);
1066 }
1067
1068 pvh_start_addr = *elf_note_data_addr;
1069
1070 return pvh_start_addr;
1071}
1072
1073static bool load_elfboot(const char *kernel_filename,
1074 int kernel_file_size,
1075 uint8_t *header,
1076 size_t pvh_xen_start_addr,
1077 FWCfgState *fw_cfg)
1078{
1079 uint32_t flags = 0;
1080 uint32_t mh_load_addr = 0;
1081 uint32_t elf_kernel_size = 0;
1082 uint64_t elf_entry;
1083 uint64_t elf_low, elf_high;
1084 int kernel_size;
1085
1086 if (ldl_p(header) != 0x464c457f) {
1087 return false; /* no elfboot */
1088 }
1089
1090 bool elf_is64 = header[EI_CLASS] == ELFCLASS64;
1091 flags = elf_is64 ?
1092 ((Elf64_Ehdr *)header)->e_flags : ((Elf32_Ehdr *)header)->e_flags;
1093
1094 if (flags & 0x00010004) { /* LOAD_ELF_HEADER_HAS_ADDR */
1095 error_report("elfboot unsupported flags = %x", flags);
1096 exit(1);
1097 }
1098
1099 uint64_t elf_note_type = XEN_ELFNOTE_PHYS32_ENTRY;
1100 kernel_size = load_elf(kernel_filename, read_pvh_start_addr,
1101 NULL, &elf_note_type, &elf_entry,
1102 &elf_low, &elf_high, 0, I386_ELF_MACHINE,
1103 0, 0);
1104
1105 if (kernel_size < 0) {
1106 error_report("Error while loading elf kernel");
1107 exit(1);
1108 }
1109 mh_load_addr = elf_low;
1110 elf_kernel_size = elf_high - elf_low;
1111
1112 if (pvh_start_addr == 0) {
1113 error_report("Error loading uncompressed kernel without PVH ELF Note");
1114 exit(1);
1115 }
1116 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, pvh_start_addr);
1117 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
1118 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, elf_kernel_size);
1119
1120 return true;
1121}
1122
df1f79fd
EH
1123static void load_linux(PCMachineState *pcms,
1124 FWCfgState *fw_cfg)
642a4f96
TS
1125{
1126 uint16_t protocol;
f3839fda 1127 int setup_size, kernel_size, cmdline_size;
3cbeb524 1128 int dtb_size, setup_data_offset;
642a4f96 1129 uint32_t initrd_max;
c24323dd 1130 uint8_t header[8192], *setup, *kernel;
a8170e5e 1131 hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
45a50b16 1132 FILE *f;
bf4e5d92 1133 char *vmode;
df1f79fd 1134 MachineState *machine = MACHINE(pcms);
cd4040ec 1135 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
3cbeb524 1136 struct setup_data *setup_data;
df1f79fd
EH
1137 const char *kernel_filename = machine->kernel_filename;
1138 const char *initrd_filename = machine->initrd_filename;
3cbeb524 1139 const char *dtb_filename = machine->dtb;
df1f79fd 1140 const char *kernel_cmdline = machine->kernel_cmdline;
642a4f96
TS
1141
1142 /* Align to 16 bytes as a paranoia measure */
1143 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
1144
1145 /* load the kernel header */
1146 f = fopen(kernel_filename, "rb");
1147 if (!f || !(kernel_size = get_file_size(f)) ||
0f9d76e5
LG
1148 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
1149 MIN(ARRAY_SIZE(header), kernel_size)) {
1150 fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
1151 kernel_filename, strerror(errno));
1152 exit(1);
642a4f96
TS
1153 }
1154
1155 /* kernel protocol version */
bc4edd79 1156#if 0
642a4f96 1157 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
bc4edd79 1158#endif
0f9d76e5
LG
1159 if (ldl_p(header+0x202) == 0x53726448) {
1160 protocol = lduw_p(header+0x206);
1161 } else {
5dc8ab36
SG
1162 /*
1163 * This could be a multiboot kernel. If it is, let's stop treating it
1164 * like a Linux kernel.
1165 * Note: some multiboot images could be in the ELF format (the same of
1166 * PVH), so we try multiboot first since we check the multiboot magic
1167 * header before to load it.
1168 */
1169 if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
1170 kernel_cmdline, kernel_size, header)) {
1171 return;
1172 }
ab969087
LM
1173 /*
1174 * Check if the file is an uncompressed kernel file (ELF) and load it,
1175 * saving the PVH entry point used by the x86/HVM direct boot ABI.
1176 * If load_elfboot() is successful, populate the fw_cfg info.
1177 */
fda672b5
SG
1178 if (pcmc->pvh_enabled &&
1179 load_elfboot(kernel_filename, kernel_size,
ab969087 1180 header, pvh_start_addr, fw_cfg)) {
ab969087
LM
1181 fclose(f);
1182
1183 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
1184 strlen(kernel_cmdline) + 1);
1185 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
1186
ab969087
LM
1187 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
1188 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
1189 header, sizeof(header));
1190
c5bf7847
SG
1191 /* load initrd */
1192 if (initrd_filename) {
e502fe96 1193 GMappedFile *mapped_file;
c5bf7847
SG
1194 gsize initrd_size;
1195 gchar *initrd_data;
1196 GError *gerr = NULL;
1197
e502fe96
SG
1198 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
1199 if (!mapped_file) {
c5bf7847
SG
1200 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
1201 initrd_filename, gerr->message);
1202 exit(1);
1203 }
e502fe96 1204 pcms->initrd_mapped_file = mapped_file;
c5bf7847 1205
e502fe96
SG
1206 initrd_data = g_mapped_file_get_contents(mapped_file);
1207 initrd_size = g_mapped_file_get_length(mapped_file);
c5bf7847
SG
1208 initrd_max = pcms->below_4g_mem_size - pcmc->acpi_data_size - 1;
1209 if (initrd_size >= initrd_max) {
1210 fprintf(stderr, "qemu: initrd is too large, cannot support."
1211 "(max: %"PRIu32", need %"PRId64")\n",
1212 initrd_max, (uint64_t)initrd_size);
1213 exit(1);
1214 }
1215
1216 initrd_addr = (initrd_max - initrd_size) & ~4095;
1217
1218 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
1219 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
1220 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data,
1221 initrd_size);
1222 }
1223
1fb0d709
SG
1224 option_rom[nb_option_roms].bootindex = 0;
1225 option_rom[nb_option_roms].name = "pvh.bin";
1226 nb_option_roms++;
1227
ab969087
LM
1228 return;
1229 }
0f9d76e5 1230 protocol = 0;
f16408df 1231 }
642a4f96
TS
1232
1233 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
0f9d76e5
LG
1234 /* Low kernel */
1235 real_addr = 0x90000;
1236 cmdline_addr = 0x9a000 - cmdline_size;
1237 prot_addr = 0x10000;
642a4f96 1238 } else if (protocol < 0x202) {
0f9d76e5
LG
1239 /* High but ancient kernel */
1240 real_addr = 0x90000;
1241 cmdline_addr = 0x9a000 - cmdline_size;
1242 prot_addr = 0x100000;
642a4f96 1243 } else {
0f9d76e5
LG
1244 /* High and recent kernel */
1245 real_addr = 0x10000;
1246 cmdline_addr = 0x20000;
1247 prot_addr = 0x100000;
642a4f96
TS
1248 }
1249
bc4edd79 1250#if 0
642a4f96 1251 fprintf(stderr,
0f9d76e5
LG
1252 "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
1253 "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
1254 "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
1255 real_addr,
1256 cmdline_addr,
1257 prot_addr);
bc4edd79 1258#endif
642a4f96
TS
1259
1260 /* highest address for loading the initrd */
aab50e53
LZ
1261 if (protocol >= 0x20c &&
1262 lduw_p(header+0x236) & XLF_CAN_BE_LOADED_ABOVE_4G) {
1263 /*
1264 * Linux has supported initrd up to 4 GB for a very long time (2007,
1265 * long before XLF_CAN_BE_LOADED_ABOVE_4G which was added in 2013),
1266 * though it only sets initrd_max to 2 GB to "work around bootloader
1267 * bugs". Luckily, QEMU firmware(which does something like bootloader)
1268 * has supported this.
1269 *
1270 * It's believed that if XLF_CAN_BE_LOADED_ABOVE_4G is set, initrd can
1271 * be loaded into any address.
1272 *
1273 * In addition, initrd_max is uint32_t simply because QEMU doesn't
1274 * support the 64-bit boot protocol (specifically the ext_ramdisk_image
1275 * field).
1276 *
1277 * Therefore here just limit initrd_max to UINT32_MAX simply as well.
1278 */
1279 initrd_max = UINT32_MAX;
1280 } else if (protocol >= 0x203) {
0f9d76e5
LG
1281 initrd_max = ldl_p(header+0x22c);
1282 } else {
1283 initrd_max = 0x37ffffff;
1284 }
642a4f96 1285
cd4040ec
EH
1286 if (initrd_max >= pcms->below_4g_mem_size - pcmc->acpi_data_size) {
1287 initrd_max = pcms->below_4g_mem_size - pcmc->acpi_data_size - 1;
927766c7 1288 }
642a4f96 1289
57a46d05
AG
1290 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
1291 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
96f80586 1292 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
642a4f96
TS
1293
1294 if (protocol >= 0x202) {
0f9d76e5 1295 stl_p(header+0x228, cmdline_addr);
642a4f96 1296 } else {
0f9d76e5
LG
1297 stw_p(header+0x20, 0xA33F);
1298 stw_p(header+0x22, cmdline_addr-real_addr);
642a4f96
TS
1299 }
1300
bf4e5d92
PT
1301 /* handle vga= parameter */
1302 vmode = strstr(kernel_cmdline, "vga=");
1303 if (vmode) {
1304 unsigned int video_mode;
1305 /* skip "vga=" */
1306 vmode += 4;
1307 if (!strncmp(vmode, "normal", 6)) {
1308 video_mode = 0xffff;
1309 } else if (!strncmp(vmode, "ext", 3)) {
1310 video_mode = 0xfffe;
1311 } else if (!strncmp(vmode, "ask", 3)) {
1312 video_mode = 0xfffd;
1313 } else {
1314 video_mode = strtol(vmode, NULL, 0);
1315 }
1316 stw_p(header+0x1fa, video_mode);
1317 }
1318
642a4f96 1319 /* loader type */
5cbdb3a3 1320 /* High nybble = B reserved for QEMU; low nybble is revision number.
642a4f96
TS
1321 If this code is substantially changed, you may want to consider
1322 incrementing the revision. */
0f9d76e5
LG
1323 if (protocol >= 0x200) {
1324 header[0x210] = 0xB0;
1325 }
642a4f96
TS
1326 /* heap */
1327 if (protocol >= 0x201) {
0f9d76e5
LG
1328 header[0x211] |= 0x80; /* CAN_USE_HEAP */
1329 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
642a4f96
TS
1330 }
1331
1332 /* load initrd */
1333 if (initrd_filename) {
e502fe96 1334 GMappedFile *mapped_file;
c24323dd
PM
1335 gsize initrd_size;
1336 gchar *initrd_data;
1337 GError *gerr = NULL;
1338
0f9d76e5
LG
1339 if (protocol < 0x200) {
1340 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
1341 exit(1);
1342 }
642a4f96 1343
e502fe96
SG
1344 mapped_file = g_mapped_file_new(initrd_filename, false, &gerr);
1345 if (!mapped_file) {
7454e51d 1346 fprintf(stderr, "qemu: error reading initrd %s: %s\n",
c24323dd 1347 initrd_filename, gerr->message);
d6fa4b77 1348 exit(1);
c24323dd 1349 }
e502fe96
SG
1350 pcms->initrd_mapped_file = mapped_file;
1351
1352 initrd_data = g_mapped_file_get_contents(mapped_file);
1353 initrd_size = g_mapped_file_get_length(mapped_file);
c24323dd 1354 if (initrd_size >= initrd_max) {
f3839fda 1355 fprintf(stderr, "qemu: initrd is too large, cannot support."
c24323dd
PM
1356 "(max: %"PRIu32", need %"PRId64")\n",
1357 initrd_max, (uint64_t)initrd_size);
f3839fda 1358 exit(1);
d6fa4b77
MK
1359 }
1360
45a50b16 1361 initrd_addr = (initrd_max-initrd_size) & ~4095;
57a46d05 1362
57a46d05
AG
1363 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
1364 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
1365 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
642a4f96 1366
0f9d76e5
LG
1367 stl_p(header+0x218, initrd_addr);
1368 stl_p(header+0x21c, initrd_size);
642a4f96
TS
1369 }
1370
45a50b16 1371 /* load kernel and setup */
642a4f96 1372 setup_size = header[0x1f1];
0f9d76e5
LG
1373 if (setup_size == 0) {
1374 setup_size = 4;
1375 }
642a4f96 1376 setup_size = (setup_size+1)*512;
ec5fd402
PB
1377 if (setup_size > kernel_size) {
1378 fprintf(stderr, "qemu: invalid kernel header\n");
1379 exit(1);
1380 }
45a50b16 1381 kernel_size -= setup_size;
642a4f96 1382
7267c094
AL
1383 setup = g_malloc(setup_size);
1384 kernel = g_malloc(kernel_size);
45a50b16 1385 fseek(f, 0, SEEK_SET);
5a41ecc5
KS
1386 if (fread(setup, 1, setup_size, f) != setup_size) {
1387 fprintf(stderr, "fread() failed\n");
1388 exit(1);
1389 }
1390 if (fread(kernel, 1, kernel_size, f) != kernel_size) {
1391 fprintf(stderr, "fread() failed\n");
1392 exit(1);
1393 }
642a4f96 1394 fclose(f);
3cbeb524
AB
1395
1396 /* append dtb to kernel */
1397 if (dtb_filename) {
1398 if (protocol < 0x209) {
1399 fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
1400 exit(1);
1401 }
1402
1403 dtb_size = get_image_size(dtb_filename);
1404 if (dtb_size <= 0) {
1405 fprintf(stderr, "qemu: error reading dtb %s: %s\n",
1406 dtb_filename, strerror(errno));
1407 exit(1);
1408 }
1409
1410 setup_data_offset = QEMU_ALIGN_UP(kernel_size, 16);
1411 kernel_size = setup_data_offset + sizeof(struct setup_data) + dtb_size;
1412 kernel = g_realloc(kernel, kernel_size);
1413
1414 stq_p(header+0x250, prot_addr + setup_data_offset);
1415
1416 setup_data = (struct setup_data *)(kernel + setup_data_offset);
1417 setup_data->next = 0;
1418 setup_data->type = cpu_to_le32(SETUP_DTB);
1419 setup_data->len = cpu_to_le32(dtb_size);
1420
1421 load_image_size(dtb_filename, setup_data->data, dtb_size);
1422 }
1423
45a50b16 1424 memcpy(setup, header, MIN(sizeof(header), setup_size));
57a46d05
AG
1425
1426 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
1427 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
1428 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
1429
1430 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
1431 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
1432 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
1433
98e753a6
IM
1434 option_rom[nb_option_roms].bootindex = 0;
1435 option_rom[nb_option_roms].name = "linuxboot.bin";
1436 if (pcmc->linuxboot_dma_enabled && fw_cfg_dma_enabled(fw_cfg)) {
b2a575a1 1437 option_rom[nb_option_roms].name = "linuxboot_dma.bin";
b2a575a1 1438 }
57a46d05 1439 nb_option_roms++;
642a4f96
TS
1440}
1441
b41a2cd1
FB
1442#define NE2000_NB_MAX 6
1443
675d6f82
BS
1444static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
1445 0x280, 0x380 };
1446static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
b41a2cd1 1447
48a18b3c 1448void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
a41b2ff2
PB
1449{
1450 static int nb_ne2k = 0;
1451
1452 if (nb_ne2k == NE2000_NB_MAX)
1453 return;
48a18b3c 1454 isa_ne2000_init(bus, ne2000_io[nb_ne2k],
9453c5bc 1455 ne2000_irq[nb_ne2k], nd);
a41b2ff2
PB
1456 nb_ne2k++;
1457}
1458
92a16d7a 1459DeviceState *cpu_get_current_apic(void)
0e26b7b8 1460{
4917cf44
AF
1461 if (current_cpu) {
1462 X86CPU *cpu = X86_CPU(current_cpu);
02e51483 1463 return cpu->apic_state;
0e26b7b8
BS
1464 } else {
1465 return NULL;
1466 }
1467}
1468
845773ab 1469void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
53b67b30 1470{
c3affe56 1471 X86CPU *cpu = opaque;
53b67b30
BS
1472
1473 if (level) {
c3affe56 1474 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
53b67b30
BS
1475 }
1476}
1477
cabea7dc 1478static void pc_new_cpu(PCMachineState *pcms, int64_t apic_id, Error **errp)
31050930 1479{
074281d6 1480 Object *cpu = NULL;
31050930 1481 Error *local_err = NULL;
cabea7dc 1482 CPUX86State *env = NULL;
31050930 1483
cabea7dc
LX
1484 cpu = object_new(MACHINE(pcms)->cpu_type);
1485
1486 env = &X86_CPU(cpu)->env;
1487 env->nr_dies = pcms->smp_dies;
31050930 1488
c7b4efb4 1489 object_property_set_uint(cpu, apic_id, "apic-id", &local_err);
074281d6 1490 object_property_set_bool(cpu, true, "realized", &local_err);
31050930 1491
074281d6 1492 object_unref(cpu);
021c9d25 1493 error_propagate(errp, local_err);
31050930
IM
1494}
1495
6f479566
LX
1496/*
1497 * This function is very similar to smp_parse()
1498 * in hw/core/machine.c but includes CPU die support.
1499 */
1500void pc_smp_parse(MachineState *ms, QemuOpts *opts)
1501{
1b458422
LX
1502 PCMachineState *pcms = PC_MACHINE(ms);
1503
6f479566
LX
1504 if (opts) {
1505 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1506 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1b458422 1507 unsigned dies = qemu_opt_get_number(opts, "dies", 1);
6f479566
LX
1508 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1509 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1510
1511 /* compute missing values, prefer sockets over cores over threads */
1512 if (cpus == 0 || sockets == 0) {
1513 cores = cores > 0 ? cores : 1;
1514 threads = threads > 0 ? threads : 1;
1515 if (cpus == 0) {
1516 sockets = sockets > 0 ? sockets : 1;
1b458422 1517 cpus = cores * threads * dies * sockets;
6f479566
LX
1518 } else {
1519 ms->smp.max_cpus =
1520 qemu_opt_get_number(opts, "maxcpus", cpus);
1b458422 1521 sockets = ms->smp.max_cpus / (cores * threads * dies);
6f479566
LX
1522 }
1523 } else if (cores == 0) {
1524 threads = threads > 0 ? threads : 1;
1b458422 1525 cores = cpus / (sockets * dies * threads);
6f479566
LX
1526 cores = cores > 0 ? cores : 1;
1527 } else if (threads == 0) {
1b458422 1528 threads = cpus / (cores * dies * sockets);
6f479566 1529 threads = threads > 0 ? threads : 1;
1b458422 1530 } else if (sockets * dies * cores * threads < cpus) {
6f479566 1531 error_report("cpu topology: "
1b458422 1532 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) < "
6f479566 1533 "smp_cpus (%u)",
1b458422 1534 sockets, dies, cores, threads, cpus);
6f479566
LX
1535 exit(1);
1536 }
1537
1538 ms->smp.max_cpus =
1539 qemu_opt_get_number(opts, "maxcpus", cpus);
1540
1541 if (ms->smp.max_cpus < cpus) {
1542 error_report("maxcpus must be equal to or greater than smp");
1543 exit(1);
1544 }
1545
1b458422 1546 if (sockets * dies * cores * threads > ms->smp.max_cpus) {
6f479566 1547 error_report("cpu topology: "
1b458422 1548 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) > "
6f479566 1549 "maxcpus (%u)",
1b458422 1550 sockets, dies, cores, threads,
6f479566
LX
1551 ms->smp.max_cpus);
1552 exit(1);
1553 }
1554
1b458422 1555 if (sockets * dies * cores * threads != ms->smp.max_cpus) {
6f479566 1556 warn_report("Invalid CPU topology deprecated: "
1b458422 1557 "sockets (%u) * dies (%u) * cores (%u) * threads (%u) "
6f479566 1558 "!= maxcpus (%u)",
1b458422 1559 sockets, dies, cores, threads,
6f479566
LX
1560 ms->smp.max_cpus);
1561 }
1562
1563 ms->smp.cpus = cpus;
1564 ms->smp.cores = cores;
1565 ms->smp.threads = threads;
1b458422 1566 pcms->smp_dies = dies;
6f479566
LX
1567 }
1568
1569 if (ms->smp.cpus > 1) {
1570 Error *blocker = NULL;
1571 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1572 replay_add_blocker(blocker);
1573 }
1574}
1575
a0628599 1576void pc_hot_add_cpu(MachineState *ms, const int64_t id, Error **errp)
c649983b 1577{
457cfccc
EH
1578 PCMachineState *pcms = PC_MACHINE(ms);
1579 int64_t apic_id = x86_cpu_apic_id_from_index(pcms, id);
0e3bd562 1580 Error *local_err = NULL;
c649983b 1581
8de433cb
IM
1582 if (id < 0) {
1583 error_setg(errp, "Invalid CPU id: %" PRIi64, id);
1584 return;
1585 }
1586
5ff020b7
EH
1587 if (apic_id >= ACPI_CPU_HOTPLUG_ID_LIMIT) {
1588 error_setg(errp, "Unable to add CPU: %" PRIi64
1589 ", resulting APIC ID (%" PRIi64 ") is too large",
1590 id, apic_id);
1591 return;
1592 }
1593
cabea7dc 1594 pc_new_cpu(PC_MACHINE(ms), apic_id, &local_err);
0e3bd562
AF
1595 if (local_err) {
1596 error_propagate(errp, local_err);
1597 return;
1598 }
c649983b
IM
1599}
1600
4884b7bf 1601void pc_cpus_init(PCMachineState *pcms)
70166477
IY
1602{
1603 int i;
c96a1c0b 1604 const CPUArchIdList *possible_cpus;
311ca98d 1605 MachineState *ms = MACHINE(pcms);
c96a1c0b 1606 MachineClass *mc = MACHINE_GET_CLASS(pcms);
0788a56b
EH
1607 PCMachineClass *pcmc = PC_MACHINE_CLASS(mc);
1608
1609 x86_cpu_set_default_version(pcmc->default_cpu_version);
70166477 1610
ebde2465
IM
1611 /* Calculates the limit to CPU APIC ID values
1612 *
1613 * Limit for the APIC ID value, so that all
1614 * CPU APIC IDs are < pcms->apic_id_limit.
1615 *
5888e01c 1616 * This is used for FW_CFG_MAX_CPUS. See comments on fw_cfg_arch_create().
ebde2465 1617 */
0e11fc69
LX
1618 pcms->apic_id_limit = x86_cpu_apic_id_from_index(pcms,
1619 ms->smp.max_cpus - 1) + 1;
311ca98d 1620 possible_cpus = mc->possible_cpu_arch_ids(ms);
0e11fc69 1621 for (i = 0; i < ms->smp.cpus; i++) {
cabea7dc 1622 pc_new_cpu(pcms, possible_cpus->cpus[i].arch_id, &error_fatal);
70166477
IY
1623 }
1624}
1625
4e91c7f3 1626static void pc_build_feature_control_file(MachineState *ms,
eb6449fb 1627 FWCfgState *fw_cfg)
217f1b4a 1628{
38690a1c 1629 X86CPU *cpu = X86_CPU(ms->possible_cpus->cpus[0].cpu);
217f1b4a
HZ
1630 CPUX86State *env = &cpu->env;
1631 uint32_t unused, ecx, edx;
1632 uint64_t feature_control_bits = 0;
1633 uint64_t *val;
1634
1635 cpu_x86_cpuid(env, 1, 0, &unused, &unused, &ecx, &edx);
1636 if (ecx & CPUID_EXT_VMX) {
1637 feature_control_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1638 }
1639
1640 if ((edx & (CPUID_EXT2_MCE | CPUID_EXT2_MCA)) ==
1641 (CPUID_EXT2_MCE | CPUID_EXT2_MCA) &&
1642 (env->mcg_cap & MCG_LMCE_P)) {
1643 feature_control_bits |= FEATURE_CONTROL_LMCE;
1644 }
1645
1646 if (!feature_control_bits) {
1647 return;
1648 }
1649
1650 val = g_malloc(sizeof(*val));
1651 *val = cpu_to_le64(feature_control_bits | FEATURE_CONTROL_LOCKED);
eb6449fb 1652 fw_cfg_add_file(fw_cfg, "etc/msr_feature_control", val, sizeof(*val));
217f1b4a
HZ
1653}
1654
e3cadac0
IM
1655static void rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count)
1656{
1657 if (cpus_count > 0xff) {
1658 /* If the number of CPUs can't be represented in 8 bits, the
1659 * BIOS must use "FW_CFG_NB_CPUS". Set RTC field to 0 just
1660 * to make old BIOSes fail more predictably.
1661 */
1662 rtc_set_memory(rtc, 0x5f, 0);
1663 } else {
1664 rtc_set_memory(rtc, 0x5f, cpus_count - 1);
1665 }
1666}
1667
3459a625 1668static
9ebeed0c 1669void pc_machine_done(Notifier *notifier, void *data)
3459a625 1670{
9ebeed0c
EH
1671 PCMachineState *pcms = container_of(notifier,
1672 PCMachineState, machine_done);
1673 PCIBus *bus = pcms->bus;
2118196b 1674
ba157b69 1675 /* set the number of CPUs */
e3cadac0 1676 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
ba157b69 1677
2118196b
MA
1678 if (bus) {
1679 int extra_hosts = 0;
1680
1681 QLIST_FOREACH(bus, &bus->child, sibling) {
1682 /* look for expander root buses */
1683 if (pci_bus_is_root(bus)) {
1684 extra_hosts++;
1685 }
1686 }
f264d360 1687 if (extra_hosts && pcms->fw_cfg) {
2118196b
MA
1688 uint64_t *val = g_malloc(sizeof(*val));
1689 *val = cpu_to_le64(extra_hosts);
f264d360 1690 fw_cfg_add_file(pcms->fw_cfg,
2118196b
MA
1691 "etc/extra-pci-roots", val, sizeof(*val));
1692 }
1693 }
1694
bb292f5a 1695 acpi_setup();
6d42eefa 1696 if (pcms->fw_cfg) {
e0e949d7 1697 fw_cfg_build_smbios(MACHINE(pcms), pcms->fw_cfg);
4e91c7f3 1698 pc_build_feature_control_file(MACHINE(pcms), pcms->fw_cfg);
e3cadac0
IM
1699 /* update FW_CFG_NB_CPUS to account for -device added CPUs */
1700 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
6d42eefa 1701 }
60c5e104 1702
1a26f466 1703 if (pcms->apic_id_limit > 255 && !xen_enabled()) {
60c5e104
IM
1704 IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
1705
a924b3d8 1706 if (!iommu || !x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu)) ||
60c5e104
IM
1707 iommu->intr_eim != ON_OFF_AUTO_ON) {
1708 error_report("current -smp configuration requires "
1709 "Extended Interrupt Mode enabled. "
1710 "You can add an IOMMU using: "
1711 "-device intel-iommu,intremap=on,eim=on");
1712 exit(EXIT_FAILURE);
1713 }
1714 }
3459a625
MT
1715}
1716
e4e8ba04 1717void pc_guest_info_init(PCMachineState *pcms)
3459a625 1718{
1f3aba37 1719 int i;
aa570207 1720 MachineState *ms = MACHINE(pcms);
b20c9bd5 1721
dd4c2f01 1722 pcms->apic_xrupt_override = kvm_allows_irq0_override();
aa570207 1723 pcms->numa_nodes = ms->numa_state->num_nodes;
dd4c2f01
EH
1724 pcms->node_mem = g_malloc0(pcms->numa_nodes *
1725 sizeof *pcms->node_mem);
aa570207 1726 for (i = 0; i < ms->numa_state->num_nodes; i++) {
7e721e7b 1727 pcms->node_mem[i] = ms->numa_state->nodes[i].node_mem;
8c85901e
WG
1728 }
1729
9ebeed0c
EH
1730 pcms->machine_done.notify = pc_machine_done;
1731 qemu_add_machine_init_done_notifier(&pcms->machine_done);
3459a625
MT
1732}
1733
83d08f26
MT
1734/* setup pci memory address space mapping into system address space */
1735void pc_pci_as_mapping_init(Object *owner, MemoryRegion *system_memory,
1736 MemoryRegion *pci_address_space)
39848901 1737{
83d08f26
MT
1738 /* Set to lower priority than RAM */
1739 memory_region_add_subregion_overlap(system_memory, 0x0,
1740 pci_address_space, -1);
39848901
IM
1741}
1742
7bc35e0f 1743void xen_load_linux(PCMachineState *pcms)
b33a5bbf
CL
1744{
1745 int i;
1746 FWCfgState *fw_cfg;
1747
df1f79fd 1748 assert(MACHINE(pcms)->kernel_filename != NULL);
b33a5bbf 1749
305ae888 1750 fw_cfg = fw_cfg_init_io(FW_CFG_IO_BASE);
e3cadac0 1751 fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
b33a5bbf
CL
1752 rom_set_fw(fw_cfg);
1753
df1f79fd 1754 load_linux(pcms, fw_cfg);
b33a5bbf
CL
1755 for (i = 0; i < nb_option_roms; i++) {
1756 assert(!strcmp(option_rom[i].name, "linuxboot.bin") ||
b2a575a1 1757 !strcmp(option_rom[i].name, "linuxboot_dma.bin") ||
1fb0d709 1758 !strcmp(option_rom[i].name, "pvh.bin") ||
b33a5bbf
CL
1759 !strcmp(option_rom[i].name, "multiboot.bin"));
1760 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1761 }
f264d360 1762 pcms->fw_cfg = fw_cfg;
b33a5bbf
CL
1763}
1764
5934e216
EH
1765void pc_memory_init(PCMachineState *pcms,
1766 MemoryRegion *system_memory,
1767 MemoryRegion *rom_memory,
1768 MemoryRegion **ram_memory)
80cabfad 1769{
cbc5b5f3
JJ
1770 int linux_boot, i;
1771 MemoryRegion *ram, *option_rom_mr;
00cb2a99 1772 MemoryRegion *ram_below_4g, *ram_above_4g;
a88b362c 1773 FWCfgState *fw_cfg;
62b160c0 1774 MachineState *machine = MACHINE(pcms);
264b4857 1775 MachineClass *mc = MACHINE_GET_CLASS(machine);
16a9e8a5 1776 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
d592d303 1777
c8d163bc
EH
1778 assert(machine->ram_size == pcms->below_4g_mem_size +
1779 pcms->above_4g_mem_size);
9521d42b
PB
1780
1781 linux_boot = (machine->kernel_filename != NULL);
80cabfad 1782
00cb2a99 1783 /* Allocate RAM. We allocate it as a single memory region and use
66a0a2cb 1784 * aliases to address portions of it, mostly for backwards compatibility
00cb2a99
AK
1785 * with older qemus that used qemu_ram_alloc().
1786 */
7267c094 1787 ram = g_malloc(sizeof(*ram));
9521d42b
PB
1788 memory_region_allocate_system_memory(ram, NULL, "pc.ram",
1789 machine->ram_size);
ae0a5466 1790 *ram_memory = ram;
7267c094 1791 ram_below_4g = g_malloc(sizeof(*ram_below_4g));
2c9b15ca 1792 memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
c8d163bc 1793 0, pcms->below_4g_mem_size);
00cb2a99 1794 memory_region_add_subregion(system_memory, 0, ram_below_4g);
c8d163bc
EH
1795 e820_add_entry(0, pcms->below_4g_mem_size, E820_RAM);
1796 if (pcms->above_4g_mem_size > 0) {
7267c094 1797 ram_above_4g = g_malloc(sizeof(*ram_above_4g));
2c9b15ca 1798 memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
c8d163bc
EH
1799 pcms->below_4g_mem_size,
1800 pcms->above_4g_mem_size);
00cb2a99
AK
1801 memory_region_add_subregion(system_memory, 0x100000000ULL,
1802 ram_above_4g);
c8d163bc 1803 e820_add_entry(0x100000000ULL, pcms->above_4g_mem_size, E820_RAM);
bbe80adf 1804 }
82b36dc3 1805
bb292f5a 1806 if (!pcmc->has_reserved_memory &&
ca8336f3 1807 (machine->ram_slots ||
9521d42b 1808 (machine->maxram_size > machine->ram_size))) {
ca8336f3
IM
1809
1810 error_report("\"-memory 'slots|maxmem'\" is not supported by: %s",
1811 mc->name);
1812 exit(EXIT_FAILURE);
1813 }
1814
b0c14ec4
DH
1815 /* always allocate the device memory information */
1816 machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
1817
f2ffbe2b 1818 /* initialize device memory address space */
bb292f5a 1819 if (pcmc->has_reserved_memory &&
9521d42b 1820 (machine->ram_size < machine->maxram_size)) {
f2ffbe2b 1821 ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
619d11e4 1822
a0cc8856
IM
1823 if (machine->ram_slots > ACPI_MAX_RAM_SLOTS) {
1824 error_report("unsupported amount of memory slots: %"PRIu64,
1825 machine->ram_slots);
1826 exit(EXIT_FAILURE);
1827 }
1828
f2c38522
PK
1829 if (QEMU_ALIGN_UP(machine->maxram_size,
1830 TARGET_PAGE_SIZE) != machine->maxram_size) {
1831 error_report("maximum memory size must by aligned to multiple of "
1832 "%d bytes", TARGET_PAGE_SIZE);
1833 exit(EXIT_FAILURE);
1834 }
1835
b0c14ec4 1836 machine->device_memory->base =
d471bf3e 1837 ROUND_UP(0x100000000ULL + pcms->above_4g_mem_size, 1 * GiB);
619d11e4 1838
16a9e8a5 1839 if (pcmc->enforce_aligned_dimm) {
f2ffbe2b 1840 /* size device region assuming 1G page max alignment per slot */
d471bf3e 1841 device_mem_size += (1 * GiB) * machine->ram_slots;
085f8e88
IM
1842 }
1843
f2ffbe2b
DH
1844 if ((machine->device_memory->base + device_mem_size) <
1845 device_mem_size) {
619d11e4
IM
1846 error_report("unsupported amount of maximum memory: " RAM_ADDR_FMT,
1847 machine->maxram_size);
1848 exit(EXIT_FAILURE);
1849 }
1850
b0c14ec4 1851 memory_region_init(&machine->device_memory->mr, OBJECT(pcms),
f2ffbe2b 1852 "device-memory", device_mem_size);
b0c14ec4
DH
1853 memory_region_add_subregion(system_memory, machine->device_memory->base,
1854 &machine->device_memory->mr);
619d11e4 1855 }
cbc5b5f3
JJ
1856
1857 /* Initialize PC system firmware */
5e640a9e 1858 pc_system_firmware_init(pcms, rom_memory);
00cb2a99 1859
7267c094 1860 option_rom_mr = g_malloc(sizeof(*option_rom_mr));
98a99ce0 1861 memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE,
f8ed85ac 1862 &error_fatal);
208fa0e4
IM
1863 if (pcmc->pci_enabled) {
1864 memory_region_set_readonly(option_rom_mr, true);
1865 }
4463aee6 1866 memory_region_add_subregion_overlap(rom_memory,
00cb2a99
AK
1867 PC_ROM_MIN_VGA,
1868 option_rom_mr,
1869 1);
f753ff16 1870
bd802bd9 1871 fw_cfg = fw_cfg_arch_create(machine,
264b4857 1872 pcms->boot_cpus, pcms->apic_id_limit);
c886fc4c 1873
8832cb80 1874 rom_set_fw(fw_cfg);
1d108d97 1875
b0c14ec4 1876 if (pcmc->has_reserved_memory && machine->device_memory->base) {
de268e13 1877 uint64_t *val = g_malloc(sizeof(*val));
2f8b5008 1878 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
b0c14ec4 1879 uint64_t res_mem_end = machine->device_memory->base;
2f8b5008
IM
1880
1881 if (!pcmc->broken_reserved_end) {
b0c14ec4 1882 res_mem_end += memory_region_size(&machine->device_memory->mr);
2f8b5008 1883 }
d471bf3e 1884 *val = cpu_to_le64(ROUND_UP(res_mem_end, 1 * GiB));
de268e13
IM
1885 fw_cfg_add_file(fw_cfg, "etc/reserved-memory-end", val, sizeof(*val));
1886 }
1887
f753ff16 1888 if (linux_boot) {
df1f79fd 1889 load_linux(pcms, fw_cfg);
f753ff16
PB
1890 }
1891
1892 for (i = 0; i < nb_option_roms; i++) {
2e55e842 1893 rom_add_option(option_rom[i].name, option_rom[i].bootindex);
406c8df3 1894 }
f264d360 1895 pcms->fw_cfg = fw_cfg;
cb135f59
PX
1896
1897 /* Init default IOAPIC address space */
1898 pcms->ioapic_as = &address_space_memory;
3d53f5c3
IY
1899}
1900
9fa99d25
MA
1901/*
1902 * The 64bit pci hole starts after "above 4G RAM" and
1903 * potentially the space reserved for memory hotplug.
1904 */
1905uint64_t pc_pci_hole64_start(void)
1906{
1907 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
1908 PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
b0c14ec4 1909 MachineState *ms = MACHINE(pcms);
9fa99d25
MA
1910 uint64_t hole64_start = 0;
1911
b0c14ec4
DH
1912 if (pcmc->has_reserved_memory && ms->device_memory->base) {
1913 hole64_start = ms->device_memory->base;
9fa99d25 1914 if (!pcmc->broken_reserved_end) {
b0c14ec4 1915 hole64_start += memory_region_size(&ms->device_memory->mr);
9fa99d25
MA
1916 }
1917 } else {
1918 hole64_start = 0x100000000ULL + pcms->above_4g_mem_size;
1919 }
1920
d471bf3e 1921 return ROUND_UP(hole64_start, 1 * GiB);
9fa99d25
MA
1922}
1923
0b0cc076 1924qemu_irq pc_allocate_cpu_irq(void)
845773ab 1925{
0b0cc076 1926 return qemu_allocate_irq(pic_irq_request, NULL, 0);
845773ab
IY
1927}
1928
48a18b3c 1929DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
765d7908 1930{
ad6d45fa
AL
1931 DeviceState *dev = NULL;
1932
bab47d9a 1933 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_VGA);
16094b75
AJ
1934 if (pci_bus) {
1935 PCIDevice *pcidev = pci_vga_init(pci_bus);
1936 dev = pcidev ? &pcidev->qdev : NULL;
1937 } else if (isa_bus) {
1938 ISADevice *isadev = isa_vga_init(isa_bus);
4a17cc4f 1939 dev = isadev ? DEVICE(isadev) : NULL;
765d7908 1940 }
bab47d9a 1941 rom_reset_order_override();
ad6d45fa 1942 return dev;
765d7908
IY
1943}
1944
258711c6
JG
1945static const MemoryRegionOps ioport80_io_ops = {
1946 .write = ioport80_write,
c02e1eac 1947 .read = ioport80_read,
258711c6
JG
1948 .endianness = DEVICE_NATIVE_ENDIAN,
1949 .impl = {
1950 .min_access_size = 1,
1951 .max_access_size = 1,
1952 },
1953};
1954
1955static const MemoryRegionOps ioportF0_io_ops = {
1956 .write = ioportF0_write,
c02e1eac 1957 .read = ioportF0_read,
258711c6
JG
1958 .endianness = DEVICE_NATIVE_ENDIAN,
1959 .impl = {
1960 .min_access_size = 1,
1961 .max_access_size = 1,
1962 },
1963};
1964
ac64273c
PMD
1965static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl, bool no_vmport)
1966{
1967 int i;
1968 DriveInfo *fd[MAX_FD];
1969 qemu_irq *a20_line;
1970 ISADevice *i8042, *port92, *vmmouse;
1971
def337ff 1972 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
ac64273c
PMD
1973 parallel_hds_isa_init(isa_bus, MAX_PARALLEL_PORTS);
1974
1975 for (i = 0; i < MAX_FD; i++) {
1976 fd[i] = drive_get(IF_FLOPPY, 0, i);
1977 create_fdctrl |= !!fd[i];
1978 }
1979 if (create_fdctrl) {
1980 fdctrl_init_isa(isa_bus, fd);
1981 }
1982
1983 i8042 = isa_create_simple(isa_bus, "i8042");
1984 if (!no_vmport) {
1985 vmport_init(isa_bus);
1986 vmmouse = isa_try_create(isa_bus, "vmmouse");
1987 } else {
1988 vmmouse = NULL;
1989 }
1990 if (vmmouse) {
1991 DeviceState *dev = DEVICE(vmmouse);
1992 qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1993 qdev_init_nofail(dev);
1994 }
1995 port92 = isa_create_simple(isa_bus, "port92");
1996
1997 a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1998 i8042_setup_a20_line(i8042, a20_line[0]);
1999 port92_init(port92, a20_line[1]);
2000 g_free(a20_line);
2001}
2002
48a18b3c 2003void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1611977c 2004 ISADevice **rtc_state,
fd53c87c 2005 bool create_fdctrl,
7a10ef51 2006 bool no_vmport,
feddd2fd 2007 bool has_pit,
3a87d009 2008 uint32_t hpet_irqs)
ffe513da
IY
2009{
2010 int i;
ce967e2f
JK
2011 DeviceState *hpet = NULL;
2012 int pit_isa_irq = 0;
2013 qemu_irq pit_alt_irq = NULL;
7d932dfd 2014 qemu_irq rtc_irq = NULL;
ac64273c 2015 ISADevice *pit = NULL;
258711c6
JG
2016 MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
2017 MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
ffe513da 2018
2c9b15ca 2019 memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
258711c6 2020 memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
ffe513da 2021
2c9b15ca 2022 memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
258711c6 2023 memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
ffe513da 2024
5d17c0d2
JK
2025 /*
2026 * Check if an HPET shall be created.
2027 *
2028 * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
2029 * when the HPET wants to take over. Thus we have to disable the latter.
2030 */
2031 if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
7a10ef51 2032 /* In order to set property, here not using sysbus_try_create_simple */
51116102 2033 hpet = qdev_try_create(NULL, TYPE_HPET);
dd703b99 2034 if (hpet) {
7a10ef51
LPF
2035 /* For pc-piix-*, hpet's intcap is always IRQ2. For pc-q35-1.7
2036 * and earlier, use IRQ2 for compat. Otherwise, use IRQ16~23,
2037 * IRQ8 and IRQ2.
2038 */
5d7fb0f2 2039 uint8_t compat = object_property_get_uint(OBJECT(hpet),
7a10ef51
LPF
2040 HPET_INTCAP, NULL);
2041 if (!compat) {
2042 qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
2043 }
2044 qdev_init_nofail(hpet);
2045 sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
2046
b881fbe9 2047 for (i = 0; i < GSI_NUM_PINS; i++) {
1356b98d 2048 sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
dd703b99 2049 }
ce967e2f
JK
2050 pit_isa_irq = -1;
2051 pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
2052 rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
822557eb 2053 }
ffe513da 2054 }
6c646a11 2055 *rtc_state = mc146818_rtc_init(isa_bus, 2000, rtc_irq);
7d932dfd
JK
2056
2057 qemu_register_boot_set(pc_boot_set, *rtc_state);
2058
feddd2fd 2059 if (!xen_enabled() && has_pit) {
15eafc2e 2060 if (kvm_pit_in_kernel()) {
c2d8d311
SS
2061 pit = kvm_pit_init(isa_bus, 0x40);
2062 } else {
acf695ec 2063 pit = i8254_pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
c2d8d311
SS
2064 }
2065 if (hpet) {
2066 /* connect PIT to output control line of the HPET */
4a17cc4f 2067 qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
c2d8d311
SS
2068 }
2069 pcspk_init(isa_bus, pit);
ce967e2f 2070 }
ffe513da 2071
55f613ac 2072 i8257_dma_init(isa_bus, 0);
ffe513da 2073
ac64273c
PMD
2074 /* Super I/O */
2075 pc_superio_init(isa_bus, create_fdctrl, no_vmport);
ffe513da
IY
2076}
2077
4b9c264b 2078void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
9011a1a7
IY
2079{
2080 int i;
2081
bab47d9a 2082 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_NIC);
9011a1a7
IY
2083 for (i = 0; i < nb_nics; i++) {
2084 NICInfo *nd = &nd_table[i];
4b9c264b 2085 const char *model = nd->model ? nd->model : pcmc->default_nic_model;
9011a1a7 2086
4b9c264b 2087 if (g_str_equal(model, "ne2k_isa")) {
9011a1a7
IY
2088 pc_init_ne2k_isa(isa_bus, nd);
2089 } else {
4b9c264b 2090 pci_nic_init_nofail(nd, pci_bus, model, NULL);
9011a1a7
IY
2091 }
2092 }
bab47d9a 2093 rom_reset_order_override();
9011a1a7
IY
2094}
2095
a39e3564
JB
2096void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
2097{
2098 DeviceState *dev;
2099 SysBusDevice *d;
2100 unsigned int i;
2101
15eafc2e 2102 if (kvm_ioapic_in_kernel()) {
34bec7a8 2103 dev = qdev_create(NULL, TYPE_KVM_IOAPIC);
a39e3564 2104 } else {
34bec7a8 2105 dev = qdev_create(NULL, TYPE_IOAPIC);
a39e3564
JB
2106 }
2107 if (parent_name) {
2108 object_property_add_child(object_resolve_path(parent_name, NULL),
2109 "ioapic", OBJECT(dev), NULL);
2110 }
2111 qdev_init_nofail(dev);
1356b98d 2112 d = SYS_BUS_DEVICE(dev);
3a4a4697 2113 sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
a39e3564
JB
2114
2115 for (i = 0; i < IOAPIC_NUM_PINS; i++) {
2116 gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
2117 }
2118}
d5747cac 2119
d468115b
DH
2120static void pc_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
2121 Error **errp)
2122{
2123 const PCMachineState *pcms = PC_MACHINE(hotplug_dev);
b0e62443 2124 const PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
f6a0d06b 2125 const MachineState *ms = MACHINE(hotplug_dev);
d468115b 2126 const bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
b0e62443 2127 const uint64_t legacy_align = TARGET_PAGE_SIZE;
ae909496 2128 Error *local_err = NULL;
d468115b
DH
2129
2130 /*
2131 * When -no-acpi is used with Q35 machine type, no ACPI is built,
2132 * but pcms->acpi_dev is still created. Check !acpi_enabled in
2133 * addition to cover this case.
2134 */
2135 if (!pcms->acpi_dev || !acpi_enabled) {
2136 error_setg(errp,
2137 "memory hotplug is not enabled: missing acpi device or acpi disabled");
2138 return;
2139 }
2140
f6a0d06b 2141 if (is_nvdimm && !ms->nvdimms_state->is_enabled) {
d468115b
DH
2142 error_setg(errp, "nvdimm is not enabled: missing 'nvdimm' in '-M'");
2143 return;
2144 }
8f1ffe5b 2145
ae909496
TH
2146 hotplug_handler_pre_plug(pcms->acpi_dev, dev, &local_err);
2147 if (local_err) {
2148 error_propagate(errp, local_err);
2149 return;
2150 }
2151
fd3416f5 2152 pc_dimm_pre_plug(PC_DIMM(dev), MACHINE(hotplug_dev),
b0e62443 2153 pcmc->enforce_aligned_dimm ? NULL : &legacy_align, errp);
d468115b
DH
2154}
2155
bb6e2f7a
DH
2156static void pc_memory_plug(HotplugHandler *hotplug_dev,
2157 DeviceState *dev, Error **errp)
95bee274
IM
2158{
2159 Error *local_err = NULL;
2160 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
f6a0d06b 2161 MachineState *ms = MACHINE(hotplug_dev);
7f3cf2d6 2162 bool is_nvdimm = object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM);
95bee274 2163
fd3416f5 2164 pc_dimm_plug(PC_DIMM(dev), MACHINE(pcms), &local_err);
43bbb49e 2165 if (local_err) {
b8865591
IM
2166 goto out;
2167 }
2168
7f3cf2d6 2169 if (is_nvdimm) {
f6a0d06b 2170 nvdimm_plug(ms->nvdimms_state);
c7f8d0f3
XG
2171 }
2172
473ac567 2173 hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &error_abort);
95bee274
IM
2174out:
2175 error_propagate(errp, local_err);
2176}
2177
bb6e2f7a
DH
2178static void pc_memory_unplug_request(HotplugHandler *hotplug_dev,
2179 DeviceState *dev, Error **errp)
64fec58e 2180{
64fec58e
TC
2181 Error *local_err = NULL;
2182 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2183
8cd91ace
HZ
2184 /*
2185 * When -no-acpi is used with Q35 machine type, no ACPI is built,
2186 * but pcms->acpi_dev is still created. Check !acpi_enabled in
2187 * addition to cover this case.
2188 */
2189 if (!pcms->acpi_dev || !acpi_enabled) {
64fec58e 2190 error_setg(&local_err,
8cd91ace 2191 "memory hotplug is not enabled: missing acpi device or acpi disabled");
64fec58e
TC
2192 goto out;
2193 }
2194
b097cc52
XG
2195 if (object_dynamic_cast(OBJECT(dev), TYPE_NVDIMM)) {
2196 error_setg(&local_err,
2197 "nvdimm device hot unplug is not supported yet.");
2198 goto out;
2199 }
2200
473ac567
DH
2201 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
2202 &local_err);
64fec58e
TC
2203out:
2204 error_propagate(errp, local_err);
2205}
2206
bb6e2f7a
DH
2207static void pc_memory_unplug(HotplugHandler *hotplug_dev,
2208 DeviceState *dev, Error **errp)
f7d3e29d
TC
2209{
2210 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
f7d3e29d
TC
2211 Error *local_err = NULL;
2212
473ac567 2213 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
f7d3e29d
TC
2214 if (local_err) {
2215 goto out;
2216 }
2217
fd3416f5 2218 pc_dimm_unplug(PC_DIMM(dev), MACHINE(pcms));
07578b0a 2219 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
f7d3e29d
TC
2220 out:
2221 error_propagate(errp, local_err);
2222}
2223
3811ef14
IM
2224static int pc_apic_cmp(const void *a, const void *b)
2225{
2226 CPUArchId *apic_a = (CPUArchId *)a;
2227 CPUArchId *apic_b = (CPUArchId *)b;
2228
2229 return apic_a->arch_id - apic_b->arch_id;
2230}
2231
7baef5cf 2232/* returns pointer to CPUArchId descriptor that matches CPU's apic_id
38690a1c 2233 * in ms->possible_cpus->cpus, if ms->possible_cpus->cpus has no
b12227af 2234 * entry corresponding to CPU's apic_id returns NULL.
7baef5cf 2235 */
1ea69c0e 2236static CPUArchId *pc_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
7baef5cf 2237{
7baef5cf
IM
2238 CPUArchId apic_id, *found_cpu;
2239
1ea69c0e 2240 apic_id.arch_id = id;
38690a1c
IM
2241 found_cpu = bsearch(&apic_id, ms->possible_cpus->cpus,
2242 ms->possible_cpus->len, sizeof(*ms->possible_cpus->cpus),
7baef5cf
IM
2243 pc_apic_cmp);
2244 if (found_cpu && idx) {
38690a1c 2245 *idx = found_cpu - ms->possible_cpus->cpus;
7baef5cf
IM
2246 }
2247 return found_cpu;
2248}
2249
5279569e
GZ
2250static void pc_cpu_plug(HotplugHandler *hotplug_dev,
2251 DeviceState *dev, Error **errp)
2252{
7baef5cf 2253 CPUArchId *found_cpu;
5279569e 2254 Error *local_err = NULL;
1ea69c0e 2255 X86CPU *cpu = X86_CPU(dev);
5279569e
GZ
2256 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2257
a44a49db 2258 if (pcms->acpi_dev) {
473ac567 2259 hotplug_handler_plug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
a44a49db
IM
2260 if (local_err) {
2261 goto out;
2262 }
5279569e
GZ
2263 }
2264
e3cadac0
IM
2265 /* increment the number of CPUs */
2266 pcms->boot_cpus++;
26ef65be 2267 if (pcms->rtc) {
e3cadac0 2268 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
26ef65be
IM
2269 }
2270 if (pcms->fw_cfg) {
e3cadac0 2271 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
2d996150
GZ
2272 }
2273
1ea69c0e 2274 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
8aba3842 2275 found_cpu->cpu = OBJECT(dev);
5279569e
GZ
2276out:
2277 error_propagate(errp, local_err);
2278}
8872c25a
IM
2279static void pc_cpu_unplug_request_cb(HotplugHandler *hotplug_dev,
2280 DeviceState *dev, Error **errp)
2281{
73360e27 2282 int idx = -1;
8872c25a 2283 Error *local_err = NULL;
1ea69c0e 2284 X86CPU *cpu = X86_CPU(dev);
8872c25a
IM
2285 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2286
75ba2ddb
IM
2287 if (!pcms->acpi_dev) {
2288 error_setg(&local_err, "CPU hot unplug not supported without ACPI");
2289 goto out;
2290 }
2291
1ea69c0e 2292 pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
73360e27
IM
2293 assert(idx != -1);
2294 if (idx == 0) {
2295 error_setg(&local_err, "Boot CPU is unpluggable");
2296 goto out;
2297 }
2298
473ac567
DH
2299 hotplug_handler_unplug_request(HOTPLUG_HANDLER(pcms->acpi_dev), dev,
2300 &local_err);
8872c25a
IM
2301 if (local_err) {
2302 goto out;
2303 }
2304
2305 out:
2306 error_propagate(errp, local_err);
2307
2308}
2309
2310static void pc_cpu_unplug_cb(HotplugHandler *hotplug_dev,
2311 DeviceState *dev, Error **errp)
2312{
8fe6374e 2313 CPUArchId *found_cpu;
8872c25a 2314 Error *local_err = NULL;
1ea69c0e 2315 X86CPU *cpu = X86_CPU(dev);
8872c25a
IM
2316 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
2317
473ac567 2318 hotplug_handler_unplug(HOTPLUG_HANDLER(pcms->acpi_dev), dev, &local_err);
8872c25a
IM
2319 if (local_err) {
2320 goto out;
2321 }
2322
1ea69c0e 2323 found_cpu = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, NULL);
8fe6374e 2324 found_cpu->cpu = NULL;
07578b0a 2325 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
8872c25a 2326
e3cadac0
IM
2327 /* decrement the number of CPUs */
2328 pcms->boot_cpus--;
2329 /* Update the number of CPUs in CMOS */
2330 rtc_set_cpus_count(pcms->rtc, pcms->boot_cpus);
2331 fw_cfg_modify_i16(pcms->fw_cfg, FW_CFG_NB_CPUS, pcms->boot_cpus);
8872c25a
IM
2332 out:
2333 error_propagate(errp, local_err);
2334}
5279569e 2335
4ec60c76
IM
2336static void pc_cpu_pre_plug(HotplugHandler *hotplug_dev,
2337 DeviceState *dev, Error **errp)
2338{
2339 int idx;
a15d2728 2340 CPUState *cs;
e8f7b83e 2341 CPUArchId *cpu_slot;
d89c2b8b 2342 X86CPUTopoInfo topo;
4ec60c76 2343 X86CPU *cpu = X86_CPU(dev);
cabea7dc 2344 CPUX86State *env = &cpu->env;
6970c5ff 2345 MachineState *ms = MACHINE(hotplug_dev);
4ec60c76 2346 PCMachineState *pcms = PC_MACHINE(hotplug_dev);
0e11fc69
LX
2347 unsigned int smp_cores = ms->smp.cores;
2348 unsigned int smp_threads = ms->smp.threads;
4ec60c76 2349
6970c5ff
IM
2350 if(!object_dynamic_cast(OBJECT(cpu), ms->cpu_type)) {
2351 error_setg(errp, "Invalid CPU type, expected cpu type: '%s'",
2352 ms->cpu_type);
2353 return;
2354 }
2355
cabea7dc
LX
2356 env->nr_dies = pcms->smp_dies;
2357
c26ae610
LX
2358 /*
2359 * If APIC ID is not set,
2360 * set it based on socket/die/core/thread properties.
2361 */
e8f7b83e 2362 if (cpu->apic_id == UNASSIGNED_APIC_ID) {
c26ae610
LX
2363 int max_socket = (ms->smp.max_cpus - 1) /
2364 smp_threads / smp_cores / pcms->smp_dies;
e8f7b83e 2365
fea374e7
EH
2366 /*
2367 * die-id was optional in QEMU 4.0 and older, so keep it optional
2368 * if there's only one die per socket.
2369 */
2370 if (cpu->die_id < 0 && pcms->smp_dies == 1) {
2371 cpu->die_id = 0;
2372 }
2373
e8f7b83e
IM
2374 if (cpu->socket_id < 0) {
2375 error_setg(errp, "CPU socket-id is not set");
2376 return;
2377 } else if (cpu->socket_id > max_socket) {
2378 error_setg(errp, "Invalid CPU socket-id: %u must be in range 0:%u",
2379 cpu->socket_id, max_socket);
2380 return;
23d9cff4
EH
2381 }
2382 if (cpu->die_id < 0) {
2383 error_setg(errp, "CPU die-id is not set");
2384 return;
176d2cda
LX
2385 } else if (cpu->die_id > pcms->smp_dies - 1) {
2386 error_setg(errp, "Invalid CPU die-id: %u must be in range 0:%u",
2a0585e1 2387 cpu->die_id, pcms->smp_dies - 1);
176d2cda 2388 return;
e8f7b83e
IM
2389 }
2390 if (cpu->core_id < 0) {
2391 error_setg(errp, "CPU core-id is not set");
2392 return;
2393 } else if (cpu->core_id > (smp_cores - 1)) {
2394 error_setg(errp, "Invalid CPU core-id: %u must be in range 0:%u",
2395 cpu->core_id, smp_cores - 1);
2396 return;
2397 }
2398 if (cpu->thread_id < 0) {
2399 error_setg(errp, "CPU thread-id is not set");
2400 return;
2401 } else if (cpu->thread_id > (smp_threads - 1)) {
2402 error_setg(errp, "Invalid CPU thread-id: %u must be in range 0:%u",
2403 cpu->thread_id, smp_threads - 1);
2404 return;
2405 }
2406
2407 topo.pkg_id = cpu->socket_id;
176d2cda 2408 topo.die_id = cpu->die_id;
e8f7b83e
IM
2409 topo.core_id = cpu->core_id;
2410 topo.smt_id = cpu->thread_id;
d65af288
LX
2411 cpu->apic_id = apicid_from_topo_ids(pcms->smp_dies, smp_cores,
2412 smp_threads, &topo);
e8f7b83e
IM
2413 }
2414
1ea69c0e 2415 cpu_slot = pc_find_cpu_slot(MACHINE(pcms), cpu->apic_id, &idx);
4ec60c76 2416 if (!cpu_slot) {
38690a1c
IM
2417 MachineState *ms = MACHINE(pcms);
2418
d65af288
LX
2419 x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
2420 smp_cores, smp_threads, &topo);
2421 error_setg(errp,
2422 "Invalid CPU [socket: %u, die: %u, core: %u, thread: %u] with"
2423 " APIC ID %" PRIu32 ", valid index range 0:%d",
2424 topo.pkg_id, topo.die_id, topo.core_id, topo.smt_id,
2425 cpu->apic_id, ms->possible_cpus->len - 1);
4ec60c76
IM
2426 return;
2427 }
2428
2429 if (cpu_slot->cpu) {
2430 error_setg(errp, "CPU[%d] with APIC ID %" PRIu32 " exists",
2431 idx, cpu->apic_id);
2432 return;
2433 }
d89c2b8b
IM
2434
2435 /* if 'address' properties socket-id/core-id/thread-id are not set, set them
c5514d0e 2436 * so that machine_query_hotpluggable_cpus would show correct values
d89c2b8b
IM
2437 */
2438 /* TODO: move socket_id/core_id/thread_id checks into x86_cpu_realizefn()
2439 * once -smp refactoring is complete and there will be CPU private
2440 * CPUState::nr_cores and CPUState::nr_threads fields instead of globals */
d65af288
LX
2441 x86_topo_ids_from_apicid(cpu->apic_id, pcms->smp_dies,
2442 smp_cores, smp_threads, &topo);
d89c2b8b
IM
2443 if (cpu->socket_id != -1 && cpu->socket_id != topo.pkg_id) {
2444 error_setg(errp, "property socket-id: %u doesn't match set apic-id:"
2445 " 0x%x (socket-id: %u)", cpu->socket_id, cpu->apic_id, topo.pkg_id);
2446 return;
2447 }
2448 cpu->socket_id = topo.pkg_id;
2449
176d2cda
LX
2450 if (cpu->die_id != -1 && cpu->die_id != topo.die_id) {
2451 error_setg(errp, "property die-id: %u doesn't match set apic-id:"
2452 " 0x%x (die-id: %u)", cpu->die_id, cpu->apic_id, topo.die_id);
2453 return;
2454 }
2455 cpu->die_id = topo.die_id;
2456
d89c2b8b
IM
2457 if (cpu->core_id != -1 && cpu->core_id != topo.core_id) {
2458 error_setg(errp, "property core-id: %u doesn't match set apic-id:"
2459 " 0x%x (core-id: %u)", cpu->core_id, cpu->apic_id, topo.core_id);
2460 return;
2461 }
2462 cpu->core_id = topo.core_id;
2463
2464 if (cpu->thread_id != -1 && cpu->thread_id != topo.smt_id) {
2465 error_setg(errp, "property thread-id: %u doesn't match set apic-id:"
2466 " 0x%x (thread-id: %u)", cpu->thread_id, cpu->apic_id, topo.smt_id);
2467 return;
2468 }
2469 cpu->thread_id = topo.smt_id;
a15d2728 2470
2d384d7c
VK
2471 if (hyperv_feat_enabled(cpu, HYPERV_FEAT_VPINDEX) &&
2472 !kvm_hv_vpindex_settable()) {
e9688fab
RK
2473 error_setg(errp, "kernel doesn't allow setting HyperV VP_INDEX");
2474 return;
2475 }
2476
a15d2728
IM
2477 cs = CPU(cpu);
2478 cs->cpu_index = idx;
93b2a8cb 2479
a0ceb640 2480 numa_cpu_pre_plug(cpu_slot, dev, errp);
4ec60c76
IM
2481}
2482
a0a49813
DH
2483static void pc_virtio_pmem_pci_pre_plug(HotplugHandler *hotplug_dev,
2484 DeviceState *dev, Error **errp)
2485{
2486 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
2487 Error *local_err = NULL;
2488
2489 if (!hotplug_dev2) {
2490 /*
2491 * Without a bus hotplug handler, we cannot control the plug/unplug
2492 * order. This should never be the case on x86, however better add
2493 * a safety net.
2494 */
2495 error_setg(errp, "virtio-pmem-pci not supported on this bus.");
2496 return;
2497 }
2498 /*
2499 * First, see if we can plug this memory device at all. If that
2500 * succeeds, branch of to the actual hotplug handler.
2501 */
2502 memory_device_pre_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev), NULL,
2503 &local_err);
2504 if (!local_err) {
2505 hotplug_handler_pre_plug(hotplug_dev2, dev, &local_err);
2506 }
2507 error_propagate(errp, local_err);
2508}
2509
2510static void pc_virtio_pmem_pci_plug(HotplugHandler *hotplug_dev,
2511 DeviceState *dev, Error **errp)
2512{
2513 HotplugHandler *hotplug_dev2 = qdev_get_bus_hotplug_handler(dev);
2514 Error *local_err = NULL;
2515
2516 /*
2517 * Plug the memory device first and then branch off to the actual
2518 * hotplug handler. If that one fails, we can easily undo the memory
2519 * device bits.
2520 */
2521 memory_device_plug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
2522 hotplug_handler_plug(hotplug_dev2, dev, &local_err);
2523 if (local_err) {
2524 memory_device_unplug(MEMORY_DEVICE(dev), MACHINE(hotplug_dev));
2525 }
2526 error_propagate(errp, local_err);
2527}
2528
2529static void pc_virtio_pmem_pci_unplug_request(HotplugHandler *hotplug_dev,
2530 DeviceState *dev, Error **errp)
2531{
2532 /* We don't support virtio pmem hot unplug */
2533 error_setg(errp, "virtio pmem device unplug not supported.");
2534}
2535
2536static void pc_virtio_pmem_pci_unplug(HotplugHandler *hotplug_dev,
2537 DeviceState *dev, Error **errp)
2538{
2539 /* We don't support virtio pmem hot unplug */
2540}
2541
4ec60c76
IM
2542static void pc_machine_device_pre_plug_cb(HotplugHandler *hotplug_dev,
2543 DeviceState *dev, Error **errp)
2544{
d468115b
DH
2545 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
2546 pc_memory_pre_plug(hotplug_dev, dev, errp);
2547 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
4ec60c76 2548 pc_cpu_pre_plug(hotplug_dev, dev, errp);
a0a49813
DH
2549 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2550 pc_virtio_pmem_pci_pre_plug(hotplug_dev, dev, errp);
4ec60c76
IM
2551 }
2552}
2553
95bee274
IM
2554static void pc_machine_device_plug_cb(HotplugHandler *hotplug_dev,
2555 DeviceState *dev, Error **errp)
2556{
2557 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
bb6e2f7a 2558 pc_memory_plug(hotplug_dev, dev, errp);
5279569e
GZ
2559 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2560 pc_cpu_plug(hotplug_dev, dev, errp);
a0a49813
DH
2561 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2562 pc_virtio_pmem_pci_plug(hotplug_dev, dev, errp);
95bee274
IM
2563 }
2564}
2565
d9c5c5b8
TC
2566static void pc_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
2567 DeviceState *dev, Error **errp)
2568{
64fec58e 2569 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
bb6e2f7a 2570 pc_memory_unplug_request(hotplug_dev, dev, errp);
8872c25a
IM
2571 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2572 pc_cpu_unplug_request_cb(hotplug_dev, dev, errp);
a0a49813
DH
2573 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2574 pc_virtio_pmem_pci_unplug_request(hotplug_dev, dev, errp);
64fec58e
TC
2575 } else {
2576 error_setg(errp, "acpi: device unplug request for not supported device"
2577 " type: %s", object_get_typename(OBJECT(dev)));
2578 }
d9c5c5b8
TC
2579}
2580
232391c1
TC
2581static void pc_machine_device_unplug_cb(HotplugHandler *hotplug_dev,
2582 DeviceState *dev, Error **errp)
2583{
f7d3e29d 2584 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
bb6e2f7a 2585 pc_memory_unplug(hotplug_dev, dev, errp);
8872c25a
IM
2586 } else if (object_dynamic_cast(OBJECT(dev), TYPE_CPU)) {
2587 pc_cpu_unplug_cb(hotplug_dev, dev, errp);
a0a49813
DH
2588 } else if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
2589 pc_virtio_pmem_pci_unplug(hotplug_dev, dev, errp);
f7d3e29d
TC
2590 } else {
2591 error_setg(errp, "acpi: device unplug for not supported device"
2592 " type: %s", object_get_typename(OBJECT(dev)));
2593 }
232391c1
TC
2594}
2595
285816d7 2596static HotplugHandler *pc_get_hotplug_handler(MachineState *machine,
95bee274
IM
2597 DeviceState *dev)
2598{
5279569e 2599 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
a0a49813
DH
2600 object_dynamic_cast(OBJECT(dev), TYPE_CPU) ||
2601 object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_PMEM_PCI)) {
95bee274
IM
2602 return HOTPLUG_HANDLER(machine);
2603 }
2604
38aefb57 2605 return NULL;
95bee274
IM
2606}
2607
bf1e8939 2608static void
f2ffbe2b
DH
2609pc_machine_get_device_memory_region_size(Object *obj, Visitor *v,
2610 const char *name, void *opaque,
2611 Error **errp)
bf1e8939 2612{
b0c14ec4 2613 MachineState *ms = MACHINE(obj);
fc3b77e2
IM
2614 int64_t value = 0;
2615
2616 if (ms->device_memory) {
2617 value = memory_region_size(&ms->device_memory->mr);
2618 }
bf1e8939 2619
51e72bc1 2620 visit_type_int(v, name, &value, errp);
bf1e8939
IM
2621}
2622
c87b1520 2623static void pc_machine_get_max_ram_below_4g(Object *obj, Visitor *v,
d7bce999
EB
2624 const char *name, void *opaque,
2625 Error **errp)
c87b1520
DS
2626{
2627 PCMachineState *pcms = PC_MACHINE(obj);
2628 uint64_t value = pcms->max_ram_below_4g;
2629
51e72bc1 2630 visit_type_size(v, name, &value, errp);
c87b1520
DS
2631}
2632
2633static void pc_machine_set_max_ram_below_4g(Object *obj, Visitor *v,
d7bce999
EB
2634 const char *name, void *opaque,
2635 Error **errp)
c87b1520
DS
2636{
2637 PCMachineState *pcms = PC_MACHINE(obj);
2638 Error *error = NULL;
2639 uint64_t value;
2640
51e72bc1 2641 visit_type_size(v, name, &value, &error);
c87b1520
DS
2642 if (error) {
2643 error_propagate(errp, error);
2644 return;
2645 }
d471bf3e 2646 if (value > 4 * GiB) {
455b0fde
EB
2647 error_setg(&error,
2648 "Machine option 'max-ram-below-4g=%"PRIu64
2649 "' expects size less than or equal to 4G", value);
c87b1520
DS
2650 error_propagate(errp, error);
2651 return;
2652 }
2653
d471bf3e 2654 if (value < 1 * MiB) {
9e5d2c52
AF
2655 warn_report("Only %" PRIu64 " bytes of RAM below the 4GiB boundary,"
2656 "BIOS may not work with less than 1MiB", value);
c87b1520
DS
2657 }
2658
2659 pcms->max_ram_below_4g = value;
2660}
2661
d7bce999
EB
2662static void pc_machine_get_vmport(Object *obj, Visitor *v, const char *name,
2663 void *opaque, Error **errp)
9b23cfb7
DDAG
2664{
2665 PCMachineState *pcms = PC_MACHINE(obj);
d1048bef 2666 OnOffAuto vmport = pcms->vmport;
9b23cfb7 2667
51e72bc1 2668 visit_type_OnOffAuto(v, name, &vmport, errp);
9b23cfb7
DDAG
2669}
2670
d7bce999
EB
2671static void pc_machine_set_vmport(Object *obj, Visitor *v, const char *name,
2672 void *opaque, Error **errp)
9b23cfb7
DDAG
2673{
2674 PCMachineState *pcms = PC_MACHINE(obj);
2675
51e72bc1 2676 visit_type_OnOffAuto(v, name, &pcms->vmport, errp);
9b23cfb7
DDAG
2677}
2678
355023f2
PB
2679bool pc_machine_is_smm_enabled(PCMachineState *pcms)
2680{
2681 bool smm_available = false;
2682
2683 if (pcms->smm == ON_OFF_AUTO_OFF) {
2684 return false;
2685 }
2686
2687 if (tcg_enabled() || qtest_enabled()) {
2688 smm_available = true;
2689 } else if (kvm_enabled()) {
2690 smm_available = kvm_has_smm();
2691 }
2692
2693 if (smm_available) {
2694 return true;
2695 }
2696
2697 if (pcms->smm == ON_OFF_AUTO_ON) {
2698 error_report("System Management Mode not supported by this hypervisor.");
2699 exit(1);
2700 }
2701 return false;
2702}
2703
d7bce999
EB
2704static void pc_machine_get_smm(Object *obj, Visitor *v, const char *name,
2705 void *opaque, Error **errp)
355023f2
PB
2706{
2707 PCMachineState *pcms = PC_MACHINE(obj);
2708 OnOffAuto smm = pcms->smm;
2709
51e72bc1 2710 visit_type_OnOffAuto(v, name, &smm, errp);
355023f2
PB
2711}
2712
d7bce999
EB
2713static void pc_machine_set_smm(Object *obj, Visitor *v, const char *name,
2714 void *opaque, Error **errp)
355023f2
PB
2715{
2716 PCMachineState *pcms = PC_MACHINE(obj);
2717
51e72bc1 2718 visit_type_OnOffAuto(v, name, &pcms->smm, errp);
355023f2
PB
2719}
2720
be232eb0
CP
2721static bool pc_machine_get_smbus(Object *obj, Error **errp)
2722{
2723 PCMachineState *pcms = PC_MACHINE(obj);
2724
f5878b03 2725 return pcms->smbus_enabled;
be232eb0
CP
2726}
2727
2728static void pc_machine_set_smbus(Object *obj, bool value, Error **errp)
2729{
2730 PCMachineState *pcms = PC_MACHINE(obj);
2731
f5878b03 2732 pcms->smbus_enabled = value;
be232eb0
CP
2733}
2734
272f0428
CP
2735static bool pc_machine_get_sata(Object *obj, Error **errp)
2736{
2737 PCMachineState *pcms = PC_MACHINE(obj);
2738
f5878b03 2739 return pcms->sata_enabled;
272f0428
CP
2740}
2741
2742static void pc_machine_set_sata(Object *obj, bool value, Error **errp)
2743{
2744 PCMachineState *pcms = PC_MACHINE(obj);
2745
f5878b03 2746 pcms->sata_enabled = value;
272f0428
CP
2747}
2748
feddd2fd
CP
2749static bool pc_machine_get_pit(Object *obj, Error **errp)
2750{
2751 PCMachineState *pcms = PC_MACHINE(obj);
2752
f5878b03 2753 return pcms->pit_enabled;
feddd2fd
CP
2754}
2755
2756static void pc_machine_set_pit(Object *obj, bool value, Error **errp)
2757{
2758 PCMachineState *pcms = PC_MACHINE(obj);
2759
f5878b03 2760 pcms->pit_enabled = value;
feddd2fd
CP
2761}
2762
bf1e8939
IM
2763static void pc_machine_initfn(Object *obj)
2764{
c87b1520
DS
2765 PCMachineState *pcms = PC_MACHINE(obj);
2766
5ec7d098 2767 pcms->max_ram_below_4g = 0; /* use default */
355023f2 2768 pcms->smm = ON_OFF_AUTO_AUTO;
97fd1ea8 2769#ifdef CONFIG_VMPORT
d1048bef 2770 pcms->vmport = ON_OFF_AUTO_AUTO;
97fd1ea8
JM
2771#else
2772 pcms->vmport = ON_OFF_AUTO_OFF;
2773#endif /* CONFIG_VMPORT */
021746c1
WL
2774 /* acpi build is enabled by default if machine supports it */
2775 pcms->acpi_build_enabled = PC_MACHINE_GET_CLASS(pcms)->has_acpi_build;
f5878b03
CM
2776 pcms->smbus_enabled = true;
2777 pcms->sata_enabled = true;
2778 pcms->pit_enabled = true;
c26ae610 2779 pcms->smp_dies = 1;
ebc29e1b
MA
2780
2781 pc_system_flash_create(pcms);
bf1e8939
IM
2782}
2783
a0628599 2784static void pc_machine_reset(MachineState *machine)
ae50c55a
ZG
2785{
2786 CPUState *cs;
2787 X86CPU *cpu;
2788
2789 qemu_devices_reset();
2790
2791 /* Reset APIC after devices have been reset to cancel
2792 * any changes that qemu_devices_reset() might have done.
2793 */
2794 CPU_FOREACH(cs) {
2795 cpu = X86_CPU(cs);
2796
2797 if (cpu->apic_state) {
2798 device_reset(cpu->apic_state);
2799 }
2800 }
2801}
2802
c508bd12
NP
2803static void pc_machine_wakeup(MachineState *machine)
2804{
2805 cpu_synchronize_all_states();
2806 pc_machine_reset(machine);
2807 cpu_synchronize_all_post_reset();
2808}
2809
ea089eeb
IM
2810static CpuInstanceProperties
2811pc_cpu_index_to_props(MachineState *ms, unsigned cpu_index)
fb43b73b 2812{
ea089eeb
IM
2813 MachineClass *mc = MACHINE_GET_CLASS(ms);
2814 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
2815
2816 assert(cpu_index < possible_cpus->len);
2817 return possible_cpus->cpus[cpu_index].props;
fb43b73b
IM
2818}
2819
79e07936
IM
2820static int64_t pc_get_default_cpu_node_id(const MachineState *ms, int idx)
2821{
2822 X86CPUTopoInfo topo;
d65af288 2823 PCMachineState *pcms = PC_MACHINE(ms);
79e07936
IM
2824
2825 assert(idx < ms->possible_cpus->len);
2826 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[idx].arch_id,
d65af288
LX
2827 pcms->smp_dies, ms->smp.cores,
2828 ms->smp.threads, &topo);
aa570207 2829 return topo.pkg_id % ms->numa_state->num_nodes;
79e07936
IM
2830}
2831
c96a1c0b 2832static const CPUArchIdList *pc_possible_cpu_arch_ids(MachineState *ms)
3811ef14 2833{
457cfccc 2834 PCMachineState *pcms = PC_MACHINE(ms);
c96a1c0b 2835 int i;
0e11fc69 2836 unsigned int max_cpus = ms->smp.max_cpus;
c96a1c0b
IM
2837
2838 if (ms->possible_cpus) {
2839 /*
2840 * make sure that max_cpus hasn't changed since the first use, i.e.
2841 * -smp hasn't been parsed after it
2842 */
2843 assert(ms->possible_cpus->len == max_cpus);
2844 return ms->possible_cpus;
2845 }
2846
2847 ms->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
2848 sizeof(CPUArchId) * max_cpus);
2849 ms->possible_cpus->len = max_cpus;
2850 for (i = 0; i < ms->possible_cpus->len; i++) {
c67ae933
IM
2851 X86CPUTopoInfo topo;
2852
d342eb76 2853 ms->possible_cpus->cpus[i].type = ms->cpu_type;
f2d672c2 2854 ms->possible_cpus->cpus[i].vcpus_count = 1;
457cfccc 2855 ms->possible_cpus->cpus[i].arch_id = x86_cpu_apic_id_from_index(pcms, i);
c67ae933 2856 x86_topo_ids_from_apicid(ms->possible_cpus->cpus[i].arch_id,
d65af288
LX
2857 pcms->smp_dies, ms->smp.cores,
2858 ms->smp.threads, &topo);
c67ae933
IM
2859 ms->possible_cpus->cpus[i].props.has_socket_id = true;
2860 ms->possible_cpus->cpus[i].props.socket_id = topo.pkg_id;
c6c1bb89
IM
2861 if (pcms->smp_dies > 1) {
2862 ms->possible_cpus->cpus[i].props.has_die_id = true;
2863 ms->possible_cpus->cpus[i].props.die_id = topo.die_id;
2864 }
c67ae933
IM
2865 ms->possible_cpus->cpus[i].props.has_core_id = true;
2866 ms->possible_cpus->cpus[i].props.core_id = topo.core_id;
2867 ms->possible_cpus->cpus[i].props.has_thread_id = true;
2868 ms->possible_cpus->cpus[i].props.thread_id = topo.smt_id;
c96a1c0b
IM
2869 }
2870 return ms->possible_cpus;
3811ef14
IM
2871}
2872
1255166b
BD
2873static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
2874{
2875 /* cpu index isn't used */
2876 CPUState *cs;
2877
2878 CPU_FOREACH(cs) {
2879 X86CPU *cpu = X86_CPU(cs);
2880
2881 if (!cpu->apic_state) {
2882 cpu_interrupt(cs, CPU_INTERRUPT_NMI);
2883 } else {
2884 apic_deliver_nmi(cpu->apic_state);
2885 }
2886 }
2887}
2888
95bee274
IM
2889static void pc_machine_class_init(ObjectClass *oc, void *data)
2890{
2891 MachineClass *mc = MACHINE_CLASS(oc);
2892 PCMachineClass *pcmc = PC_MACHINE_CLASS(oc);
2893 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1255166b 2894 NMIClass *nc = NMI_CLASS(oc);
95bee274 2895
7102fa70
EH
2896 pcmc->pci_enabled = true;
2897 pcmc->has_acpi_build = true;
2898 pcmc->rsdp_in_ram = true;
2899 pcmc->smbios_defaults = true;
2900 pcmc->smbios_uuid_encoded = true;
2901 pcmc->gigabyte_align = true;
2902 pcmc->has_reserved_memory = true;
2903 pcmc->kvmclock_enabled = true;
16a9e8a5 2904 pcmc->enforce_aligned_dimm = true;
cd4040ec
EH
2905 /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
2906 * to be used at the moment, 32K should be enough for a while. */
2907 pcmc->acpi_data_size = 0x20000 + 0x8000;
36f96c4b 2908 pcmc->save_tsc_khz = true;
98e753a6 2909 pcmc->linuxboot_dma_enabled = true;
fda672b5 2910 pcmc->pvh_enabled = true;
debbdc00 2911 assert(!mc->get_hotplug_handler);
285816d7 2912 mc->get_hotplug_handler = pc_get_hotplug_handler;
ea089eeb 2913 mc->cpu_index_to_instance_props = pc_cpu_index_to_props;
79e07936 2914 mc->get_default_cpu_node_id = pc_get_default_cpu_node_id;
3811ef14 2915 mc->possible_cpu_arch_ids = pc_possible_cpu_arch_ids;
7b8be49d 2916 mc->auto_enable_numa_with_memhp = true;
c5514d0e 2917 mc->has_hotpluggable_cpus = true;
41742767 2918 mc->default_boot_order = "cad";
4458fb3a 2919 mc->hot_add_cpu = pc_hot_add_cpu;
6f479566 2920 mc->smp_parse = pc_smp_parse;
2059839b 2921 mc->block_default_type = IF_IDE;
4458fb3a 2922 mc->max_cpus = 255;
ae50c55a 2923 mc->reset = pc_machine_reset;
c508bd12 2924 mc->wakeup = pc_machine_wakeup;
4ec60c76 2925 hc->pre_plug = pc_machine_device_pre_plug_cb;
95bee274 2926 hc->plug = pc_machine_device_plug_cb;
d9c5c5b8 2927 hc->unplug_request = pc_machine_device_unplug_request_cb;
232391c1 2928 hc->unplug = pc_machine_device_unplug_cb;
1255166b 2929 nc->nmi_monitor_handler = x86_nmi;
311ca98d 2930 mc->default_cpu_type = TARGET_DEFAULT_CPU_TYPE;
f6a0d06b 2931 mc->nvdimm_supported = true;
cd5ff833 2932 mc->numa_mem_supported = true;
0efc257d 2933
f2ffbe2b
DH
2934 object_class_property_add(oc, PC_MACHINE_DEVMEM_REGION_SIZE, "int",
2935 pc_machine_get_device_memory_region_size, NULL,
0efc257d
EH
2936 NULL, NULL, &error_abort);
2937
2938 object_class_property_add(oc, PC_MACHINE_MAX_RAM_BELOW_4G, "size",
2939 pc_machine_get_max_ram_below_4g, pc_machine_set_max_ram_below_4g,
2940 NULL, NULL, &error_abort);
2941
2942 object_class_property_set_description(oc, PC_MACHINE_MAX_RAM_BELOW_4G,
2943 "Maximum ram below the 4G boundary (32bit boundary)", &error_abort);
2944
2945 object_class_property_add(oc, PC_MACHINE_SMM, "OnOffAuto",
2946 pc_machine_get_smm, pc_machine_set_smm,
2947 NULL, NULL, &error_abort);
2948 object_class_property_set_description(oc, PC_MACHINE_SMM,
2949 "Enable SMM (pc & q35)", &error_abort);
2950
2951 object_class_property_add(oc, PC_MACHINE_VMPORT, "OnOffAuto",
2952 pc_machine_get_vmport, pc_machine_set_vmport,
2953 NULL, NULL, &error_abort);
2954 object_class_property_set_description(oc, PC_MACHINE_VMPORT,
2955 "Enable vmport (pc & q35)", &error_abort);
2956
be232eb0
CP
2957 object_class_property_add_bool(oc, PC_MACHINE_SMBUS,
2958 pc_machine_get_smbus, pc_machine_set_smbus, &error_abort);
272f0428
CP
2959
2960 object_class_property_add_bool(oc, PC_MACHINE_SATA,
2961 pc_machine_get_sata, pc_machine_set_sata, &error_abort);
feddd2fd
CP
2962
2963 object_class_property_add_bool(oc, PC_MACHINE_PIT,
2964 pc_machine_get_pit, pc_machine_set_pit, &error_abort);
95bee274
IM
2965}
2966
d5747cac
IM
2967static const TypeInfo pc_machine_info = {
2968 .name = TYPE_PC_MACHINE,
2969 .parent = TYPE_MACHINE,
2970 .abstract = true,
2971 .instance_size = sizeof(PCMachineState),
bf1e8939 2972 .instance_init = pc_machine_initfn,
d5747cac 2973 .class_size = sizeof(PCMachineClass),
95bee274
IM
2974 .class_init = pc_machine_class_init,
2975 .interfaces = (InterfaceInfo[]) {
2976 { TYPE_HOTPLUG_HANDLER },
1255166b 2977 { TYPE_NMI },
95bee274
IM
2978 { }
2979 },
d5747cac
IM
2980};
2981
2982static void pc_machine_register_types(void)
2983{
2984 type_register_static(&pc_machine_info);
2985}
2986
2987type_init(pc_machine_register_types)