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