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