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