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