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