qemu-kvm: Reindent pc_new_cpu
[qemu-kvm/amd-iommu.git] / hw / pc.c
blobb5bef0572c47921497905c60224db1aee15d35c8
1 /*
2 * QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "hw.h"
25 #include "pc.h"
26 #include "fdc.h"
27 #include "pci.h"
28 #include "block.h"
29 #include "sysemu.h"
30 #include "audio/audio.h"
31 #include "net.h"
32 #include "smbus.h"
33 #include "boards.h"
34 #include "monitor.h"
35 #include "fw_cfg.h"
36 #include "hpet_emul.h"
37 #include "watchdog.h"
38 #include "smbios.h"
39 #include "ide.h"
40 #include "device-assignment.h"
42 #include "qemu-kvm.h"
44 /* output Bochs bios info messages */
45 //#define DEBUG_BIOS
47 /* Show multiboot debug output */
48 //#define DEBUG_MULTIBOOT
50 #define BIOS_FILENAME "bios.bin"
51 #define VGABIOS_FILENAME "vgabios.bin"
52 #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
53 #define EXTBOOT_FILENAME "extboot.bin"
55 #define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
57 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
58 #define ACPI_DATA_SIZE 0x10000
59 #define BIOS_CFG_IOPORT 0x510
60 #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
61 #define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
62 #define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
64 #define MAX_IDE_BUS 2
66 static fdctrl_t *floppy_controller;
67 static RTCState *rtc_state;
68 static PITState *pit;
69 static PCII440FXState *i440fx_state;
71 qemu_irq *ioapic_irq_hack;
73 typedef struct rom_reset_data {
74 uint8_t *data;
75 target_phys_addr_t addr;
76 unsigned size;
77 } RomResetData;
79 static void option_rom_reset(void *_rrd)
81 RomResetData *rrd = _rrd;
83 cpu_physical_memory_write_rom(rrd->addr, rrd->data, rrd->size);
86 static void option_rom_setup_reset(target_phys_addr_t addr, unsigned size)
88 RomResetData *rrd = qemu_malloc(sizeof *rrd);
90 rrd->data = qemu_malloc(size);
91 cpu_physical_memory_read(addr, rrd->data, size);
92 rrd->addr = addr;
93 rrd->size = size;
94 qemu_register_reset(option_rom_reset, rrd);
97 typedef struct isa_irq_state {
98 qemu_irq *i8259;
99 qemu_irq *ioapic;
100 } IsaIrqState;
102 static void isa_irq_handler(void *opaque, int n, int level)
104 IsaIrqState *isa = (IsaIrqState *)opaque;
106 if (n < 16) {
107 qemu_set_irq(isa->i8259[n], level);
109 if (isa->ioapic)
110 qemu_set_irq(isa->ioapic[n], level);
113 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
117 /* MSDOS compatibility mode FPU exception support */
118 static qemu_irq ferr_irq;
119 /* XXX: add IGNNE support */
120 void cpu_set_ferr(CPUX86State *s)
122 qemu_irq_raise(ferr_irq);
125 static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
127 qemu_irq_lower(ferr_irq);
130 /* TSC handling */
131 uint64_t cpu_get_tsc(CPUX86State *env)
133 return cpu_get_ticks();
136 /* SMM support */
137 void cpu_smm_update(CPUState *env)
139 if (i440fx_state && env == first_cpu)
140 i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
144 /* IRQ handling */
145 int cpu_get_pic_interrupt(CPUState *env)
147 int intno;
149 intno = apic_get_interrupt(env);
150 if (intno >= 0) {
151 /* set irq request if a PIC irq is still pending */
152 /* XXX: improve that */
153 pic_update_irq(isa_pic);
154 return intno;
156 /* read the irq from the PIC */
157 if (!apic_accept_pic_intr(env))
158 return -1;
160 intno = pic_read_irq(isa_pic);
161 return intno;
164 static void pic_irq_request(void *opaque, int irq, int level)
166 CPUState *env = first_cpu;
168 if (env->apic_state) {
169 while (env) {
170 if (apic_accept_pic_intr(env))
171 apic_deliver_pic_intr(env, level);
172 env = env->next_cpu;
174 } else {
175 if (level)
176 cpu_interrupt(env, CPU_INTERRUPT_HARD);
177 else
178 cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
182 /* PC cmos mappings */
184 #define REG_EQUIPMENT_BYTE 0x14
186 static int cmos_get_fd_drive_type(int fd0)
188 int val;
190 switch (fd0) {
191 case 0:
192 /* 1.44 Mb 3"5 drive */
193 val = 4;
194 break;
195 case 1:
196 /* 2.88 Mb 3"5 drive */
197 val = 5;
198 break;
199 case 2:
200 /* 1.2 Mb 5"5 drive */
201 val = 2;
202 break;
203 default:
204 val = 0;
205 break;
207 return val;
210 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
212 RTCState *s = rtc_state;
213 int cylinders, heads, sectors;
214 bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
215 rtc_set_memory(s, type_ofs, 47);
216 rtc_set_memory(s, info_ofs, cylinders);
217 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
218 rtc_set_memory(s, info_ofs + 2, heads);
219 rtc_set_memory(s, info_ofs + 3, 0xff);
220 rtc_set_memory(s, info_ofs + 4, 0xff);
221 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
222 rtc_set_memory(s, info_ofs + 6, cylinders);
223 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
224 rtc_set_memory(s, info_ofs + 8, sectors);
227 /* convert boot_device letter to something recognizable by the bios */
228 static int boot_device2nibble(char boot_device)
230 switch(boot_device) {
231 case 'a':
232 case 'b':
233 return 0x01; /* floppy boot */
234 case 'c':
235 return 0x02; /* hard drive boot */
236 case 'd':
237 return 0x03; /* CD-ROM boot */
238 case 'n':
239 return 0x04; /* Network boot */
241 return 0;
244 /* copy/pasted from cmos_init, should be made a general function
245 and used there as well */
246 static int pc_boot_set(void *opaque, const char *boot_device)
248 Monitor *mon = cur_mon;
249 #define PC_MAX_BOOT_DEVICES 3
250 RTCState *s = (RTCState *)opaque;
251 int nbds, bds[3] = { 0, };
252 int i;
254 nbds = strlen(boot_device);
255 if (nbds > PC_MAX_BOOT_DEVICES) {
256 monitor_printf(mon, "Too many boot devices for PC\n");
257 return(1);
259 for (i = 0; i < nbds; i++) {
260 bds[i] = boot_device2nibble(boot_device[i]);
261 if (bds[i] == 0) {
262 monitor_printf(mon, "Invalid boot device for PC: '%c'\n",
263 boot_device[i]);
264 return(1);
267 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
268 rtc_set_memory(s, 0x38, (bds[2] << 4));
269 return(0);
272 /* hd_table must contain 4 block drivers */
273 static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
274 const char *boot_device, DriveInfo **hd_table)
276 RTCState *s = rtc_state;
277 int nbds, bds[3] = { 0, };
278 int val;
279 int fd0, fd1, nb;
280 int i;
282 /* various important CMOS locations needed by PC/Bochs bios */
284 /* memory size */
285 val = 640; /* base memory in K */
286 rtc_set_memory(s, 0x15, val);
287 rtc_set_memory(s, 0x16, val >> 8);
289 val = (ram_size / 1024) - 1024;
290 if (val > 65535)
291 val = 65535;
292 rtc_set_memory(s, 0x17, val);
293 rtc_set_memory(s, 0x18, val >> 8);
294 rtc_set_memory(s, 0x30, val);
295 rtc_set_memory(s, 0x31, val >> 8);
297 if (above_4g_mem_size) {
298 rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
299 rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
300 rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
303 if (ram_size > (16 * 1024 * 1024))
304 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
305 else
306 val = 0;
307 if (val > 65535)
308 val = 65535;
309 rtc_set_memory(s, 0x34, val);
310 rtc_set_memory(s, 0x35, val >> 8);
312 /* set the number of CPU */
313 rtc_set_memory(s, 0x5f, smp_cpus - 1);
315 /* set boot devices, and disable floppy signature check if requested */
316 #define PC_MAX_BOOT_DEVICES 3
317 nbds = strlen(boot_device);
318 if (nbds > PC_MAX_BOOT_DEVICES) {
319 fprintf(stderr, "Too many boot devices for PC\n");
320 exit(1);
322 for (i = 0; i < nbds; i++) {
323 bds[i] = boot_device2nibble(boot_device[i]);
324 if (bds[i] == 0) {
325 fprintf(stderr, "Invalid boot device for PC: '%c'\n",
326 boot_device[i]);
327 exit(1);
330 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
331 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
333 /* floppy type */
335 fd0 = fdctrl_get_drive_type(floppy_controller, 0);
336 fd1 = fdctrl_get_drive_type(floppy_controller, 1);
338 val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
339 rtc_set_memory(s, 0x10, val);
341 val = 0;
342 nb = 0;
343 if (fd0 < 3)
344 nb++;
345 if (fd1 < 3)
346 nb++;
347 switch (nb) {
348 case 0:
349 break;
350 case 1:
351 val |= 0x01; /* 1 drive, ready for boot */
352 break;
353 case 2:
354 val |= 0x41; /* 2 drives, ready for boot */
355 break;
357 val |= 0x02; /* FPU is there */
358 val |= 0x04; /* PS/2 mouse installed */
359 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
361 /* hard drives */
363 rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
364 if (hd_table[0])
365 cmos_init_hd(0x19, 0x1b, hd_table[0]->bdrv);
366 if (hd_table[1])
367 cmos_init_hd(0x1a, 0x24, hd_table[1]->bdrv);
369 val = 0;
370 for (i = 0; i < 4; i++) {
371 if (hd_table[i]) {
372 int cylinders, heads, sectors, translation;
373 /* NOTE: bdrv_get_geometry_hint() returns the physical
374 geometry. It is always such that: 1 <= sects <= 63, 1
375 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
376 geometry can be different if a translation is done. */
377 translation = bdrv_get_translation_hint(hd_table[i]->bdrv);
378 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
379 bdrv_get_geometry_hint(hd_table[i]->bdrv, &cylinders, &heads, &sectors);
380 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
381 /* No translation. */
382 translation = 0;
383 } else {
384 /* LBA translation. */
385 translation = 1;
387 } else {
388 translation--;
390 val |= translation << (i * 2);
393 rtc_set_memory(s, 0x39, val);
396 void ioport_set_a20(int enable)
398 /* XXX: send to all CPUs ? */
399 cpu_x86_set_a20(first_cpu, enable);
402 int ioport_get_a20(void)
404 return ((first_cpu->a20_mask >> 20) & 1);
407 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
409 ioport_set_a20((val >> 1) & 1);
410 /* XXX: bit 0 is fast reset */
413 static uint32_t ioport92_read(void *opaque, uint32_t addr)
415 return ioport_get_a20() << 1;
418 /***********************************************************/
419 /* Bochs BIOS debug ports */
421 static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
423 static const char shutdown_str[8] = "Shutdown";
424 static int shutdown_index = 0;
426 switch(addr) {
427 /* Bochs BIOS messages */
428 case 0x400:
429 case 0x401:
430 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
431 exit(1);
432 case 0x402:
433 case 0x403:
434 #ifdef DEBUG_BIOS
435 fprintf(stderr, "%c", val);
436 #endif
437 break;
438 case 0x8900:
439 /* same as Bochs power off */
440 if (val == shutdown_str[shutdown_index]) {
441 shutdown_index++;
442 if (shutdown_index == 8) {
443 shutdown_index = 0;
444 qemu_system_shutdown_request();
446 } else {
447 shutdown_index = 0;
449 break;
451 /* LGPL'ed VGA BIOS messages */
452 case 0x501:
453 case 0x502:
454 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
455 exit(1);
456 case 0x500:
457 case 0x503:
458 #ifdef DEBUG_BIOS
459 fprintf(stderr, "%c", val);
460 #endif
461 break;
465 static void *bochs_bios_init(void)
467 void *fw_cfg;
468 uint8_t *smbios_table;
469 size_t smbios_len;
470 uint64_t *numa_fw_cfg;
471 int i, j;
473 register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
474 register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
475 register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
476 register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
477 register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
479 register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
480 register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
481 register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
482 register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
484 fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
486 fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
487 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
488 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
489 acpi_tables_len);
490 fw_cfg_add_bytes(fw_cfg, FW_CFG_IRQ0_OVERRIDE, &irq0override, 1);
492 smbios_table = smbios_get_table(&smbios_len);
493 if (smbios_table)
494 fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
495 smbios_table, smbios_len);
497 /* allocate memory for the NUMA channel: one (64bit) word for the number
498 * of nodes, one word for each VCPU->node and one word for each node to
499 * hold the amount of memory.
501 numa_fw_cfg = qemu_mallocz((1 + smp_cpus + nb_numa_nodes) * 8);
502 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
503 for (i = 0; i < smp_cpus; i++) {
504 for (j = 0; j < nb_numa_nodes; j++) {
505 if (node_cpumask[j] & (1 << i)) {
506 numa_fw_cfg[i + 1] = cpu_to_le64(j);
507 break;
511 for (i = 0; i < nb_numa_nodes; i++) {
512 numa_fw_cfg[smp_cpus + 1 + i] = cpu_to_le64(node_mem[i]);
514 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, (uint8_t *)numa_fw_cfg,
515 (1 + smp_cpus + nb_numa_nodes) * 8);
517 return fw_cfg;
520 /* Generate an initial boot sector which sets state and jump to
521 a specified vector */
522 static void generate_bootsect(target_phys_addr_t option_rom,
523 uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
525 uint8_t rom[512], *p, *reloc;
526 uint8_t sum;
527 int i;
529 memset(rom, 0, sizeof(rom));
531 p = rom;
532 /* Make sure we have an option rom signature */
533 *p++ = 0x55;
534 *p++ = 0xaa;
536 /* ROM size in sectors*/
537 *p++ = 1;
539 /* Hook int19 */
541 *p++ = 0x50; /* push ax */
542 *p++ = 0x1e; /* push ds */
543 *p++ = 0x31; *p++ = 0xc0; /* xor ax, ax */
544 *p++ = 0x8e; *p++ = 0xd8; /* mov ax, ds */
546 *p++ = 0xc7; *p++ = 0x06; /* movvw _start,0x64 */
547 *p++ = 0x64; *p++ = 0x00;
548 reloc = p;
549 *p++ = 0x00; *p++ = 0x00;
551 *p++ = 0x8c; *p++ = 0x0e; /* mov cs,0x66 */
552 *p++ = 0x66; *p++ = 0x00;
554 *p++ = 0x1f; /* pop ds */
555 *p++ = 0x58; /* pop ax */
556 *p++ = 0xcb; /* lret */
558 /* Actual code */
559 *reloc = (p - rom);
561 *p++ = 0xfa; /* CLI */
562 *p++ = 0xfc; /* CLD */
564 for (i = 0; i < 6; i++) {
565 if (i == 1) /* Skip CS */
566 continue;
568 *p++ = 0xb8; /* MOV AX,imm16 */
569 *p++ = segs[i];
570 *p++ = segs[i] >> 8;
571 *p++ = 0x8e; /* MOV <seg>,AX */
572 *p++ = 0xc0 + (i << 3);
575 for (i = 0; i < 8; i++) {
576 *p++ = 0x66; /* 32-bit operand size */
577 *p++ = 0xb8 + i; /* MOV <reg>,imm32 */
578 *p++ = gpr[i];
579 *p++ = gpr[i] >> 8;
580 *p++ = gpr[i] >> 16;
581 *p++ = gpr[i] >> 24;
584 *p++ = 0xea; /* JMP FAR */
585 *p++ = ip; /* IP */
586 *p++ = ip >> 8;
587 *p++ = segs[1]; /* CS */
588 *p++ = segs[1] >> 8;
590 /* sign rom */
591 sum = 0;
592 for (i = 0; i < (sizeof(rom) - 1); i++)
593 sum += rom[i];
594 rom[sizeof(rom) - 1] = -sum;
596 cpu_physical_memory_write_rom(option_rom, rom, sizeof(rom));
597 option_rom_setup_reset(option_rom, sizeof (rom));
600 static long get_file_size(FILE *f)
602 long where, size;
604 /* XXX: on Unix systems, using fstat() probably makes more sense */
606 where = ftell(f);
607 fseek(f, 0, SEEK_END);
608 size = ftell(f);
609 fseek(f, where, SEEK_SET);
611 return size;
614 #define MULTIBOOT_STRUCT_ADDR 0x9000
616 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
617 #error multiboot struct needs to fit in 16 bit real mode
618 #endif
620 static int load_multiboot(void *fw_cfg,
621 FILE *f,
622 const char *kernel_filename,
623 const char *initrd_filename,
624 const char *kernel_cmdline,
625 uint8_t *header)
627 int i, t, is_multiboot = 0;
628 uint32_t flags = 0;
629 uint32_t mh_entry_addr;
630 uint32_t mh_load_addr;
631 uint32_t mb_kernel_size;
632 uint32_t mmap_addr = MULTIBOOT_STRUCT_ADDR;
633 uint32_t mb_bootinfo = MULTIBOOT_STRUCT_ADDR + 0x500;
634 uint32_t mb_cmdline = mb_bootinfo + 0x200;
635 uint32_t mb_mod_end;
637 /* Ok, let's see if it is a multiboot image.
638 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
639 for (i = 0; i < (8192 - 48); i += 4) {
640 if (ldl_p(header+i) == 0x1BADB002) {
641 uint32_t checksum = ldl_p(header+i+8);
642 flags = ldl_p(header+i+4);
643 checksum += flags;
644 checksum += (uint32_t)0x1BADB002;
645 if (!checksum) {
646 is_multiboot = 1;
647 break;
652 if (!is_multiboot)
653 return 0; /* no multiboot */
655 #ifdef DEBUG_MULTIBOOT
656 fprintf(stderr, "qemu: I believe we found a multiboot image!\n");
657 #endif
659 if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
660 fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
662 if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
663 uint64_t elf_entry;
664 int kernel_size;
665 fclose(f);
666 kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, NULL);
667 if (kernel_size < 0) {
668 fprintf(stderr, "Error while loading elf kernel\n");
669 exit(1);
671 mh_load_addr = mh_entry_addr = elf_entry;
672 mb_kernel_size = kernel_size;
674 #ifdef DEBUG_MULTIBOOT
675 fprintf(stderr, "qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
676 mb_kernel_size, (size_t)mh_entry_addr);
677 #endif
678 } else {
679 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
680 uint32_t mh_header_addr = ldl_p(header+i+12);
681 mh_load_addr = ldl_p(header+i+16);
682 #ifdef DEBUG_MULTIBOOT
683 uint32_t mh_load_end_addr = ldl_p(header+i+20);
684 uint32_t mh_bss_end_addr = ldl_p(header+i+24);
685 #endif
686 uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
688 mh_entry_addr = ldl_p(header+i+28);
689 mb_kernel_size = get_file_size(f) - mb_kernel_text_offset;
691 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
692 uint32_t mh_mode_type = ldl_p(header+i+32);
693 uint32_t mh_width = ldl_p(header+i+36);
694 uint32_t mh_height = ldl_p(header+i+40);
695 uint32_t mh_depth = ldl_p(header+i+44); */
697 #ifdef DEBUG_MULTIBOOT
698 fprintf(stderr, "multiboot: mh_header_addr = %#x\n", mh_header_addr);
699 fprintf(stderr, "multiboot: mh_load_addr = %#x\n", mh_load_addr);
700 fprintf(stderr, "multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr);
701 fprintf(stderr, "multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr);
702 #endif
704 fseek(f, mb_kernel_text_offset, SEEK_SET);
706 #ifdef DEBUG_MULTIBOOT
707 fprintf(stderr, "qemu: loading multiboot kernel (%#x bytes) at %#x\n",
708 mb_kernel_size, mh_load_addr);
709 #endif
711 if (!fread_targphys_ok(mh_load_addr, mb_kernel_size, f)) {
712 fprintf(stderr, "qemu: read error on multiboot kernel '%s' (%#x)\n",
713 kernel_filename, mb_kernel_size);
714 exit(1);
716 fclose(f);
719 /* blob size is only the kernel for now */
720 mb_mod_end = mh_load_addr + mb_kernel_size;
722 /* load modules */
723 stl_phys(mb_bootinfo + 20, 0x0); /* mods_count */
724 if (initrd_filename) {
725 uint32_t mb_mod_info = mb_bootinfo + 0x100;
726 uint32_t mb_mod_cmdline = mb_bootinfo + 0x300;
727 uint32_t mb_mod_start = mh_load_addr;
728 uint32_t mb_mod_length = mb_kernel_size;
729 char *next_initrd;
730 char *next_space;
731 int mb_mod_count = 0;
733 do {
734 next_initrd = strchr(initrd_filename, ',');
735 if (next_initrd)
736 *next_initrd = '\0';
737 /* if a space comes after the module filename, treat everything
738 after that as parameters */
739 cpu_physical_memory_write(mb_mod_cmdline, (uint8_t*)initrd_filename,
740 strlen(initrd_filename) + 1);
741 stl_phys(mb_mod_info + 8, mb_mod_cmdline); /* string */
742 mb_mod_cmdline += strlen(initrd_filename) + 1;
743 if ((next_space = strchr(initrd_filename, ' ')))
744 *next_space = '\0';
745 #ifdef DEBUG_MULTIBOOT
746 printf("multiboot loading module: %s\n", initrd_filename);
747 #endif
748 f = fopen(initrd_filename, "rb");
749 if (f) {
750 mb_mod_start = (mb_mod_start + mb_mod_length + (TARGET_PAGE_SIZE - 1))
751 & (TARGET_PAGE_MASK);
752 mb_mod_length = get_file_size(f);
753 mb_mod_end = mb_mod_start + mb_mod_length;
755 if (!fread_targphys_ok(mb_mod_start, mb_mod_length, f)) {
756 fprintf(stderr, "qemu: read error on multiboot module '%s' (%#x)\n",
757 initrd_filename, mb_mod_length);
758 exit(1);
761 mb_mod_count++;
762 stl_phys(mb_mod_info + 0, mb_mod_start);
763 stl_phys(mb_mod_info + 4, mb_mod_start + mb_mod_length);
764 #ifdef DEBUG_MULTIBOOT
765 printf("mod_start: %#x\nmod_end: %#x\n", mb_mod_start,
766 mb_mod_start + mb_mod_length);
767 #endif
768 stl_phys(mb_mod_info + 12, 0x0); /* reserved */
770 initrd_filename = next_initrd+1;
771 mb_mod_info += 16;
772 } while (next_initrd);
773 stl_phys(mb_bootinfo + 20, mb_mod_count); /* mods_count */
774 stl_phys(mb_bootinfo + 24, mb_bootinfo + 0x100); /* mods_addr */
777 /* Make sure we're getting kernel + modules back after reset */
778 option_rom_setup_reset(mh_load_addr, mb_mod_end - mh_load_addr);
780 /* Commandline support */
781 stl_phys(mb_bootinfo + 16, mb_cmdline);
782 t = strlen(kernel_filename);
783 cpu_physical_memory_write(mb_cmdline, (uint8_t*)kernel_filename, t);
784 mb_cmdline += t;
785 stb_phys(mb_cmdline++, ' ');
786 t = strlen(kernel_cmdline) + 1;
787 cpu_physical_memory_write(mb_cmdline, (uint8_t*)kernel_cmdline, t);
789 /* the kernel is where we want it to be now */
791 #define MULTIBOOT_FLAGS_MEMORY (1 << 0)
792 #define MULTIBOOT_FLAGS_BOOT_DEVICE (1 << 1)
793 #define MULTIBOOT_FLAGS_CMDLINE (1 << 2)
794 #define MULTIBOOT_FLAGS_MODULES (1 << 3)
795 #define MULTIBOOT_FLAGS_MMAP (1 << 6)
796 stl_phys(mb_bootinfo, MULTIBOOT_FLAGS_MEMORY
797 | MULTIBOOT_FLAGS_BOOT_DEVICE
798 | MULTIBOOT_FLAGS_CMDLINE
799 | MULTIBOOT_FLAGS_MODULES
800 | MULTIBOOT_FLAGS_MMAP);
801 stl_phys(mb_bootinfo + 4, 640); /* mem_lower */
802 stl_phys(mb_bootinfo + 8, ram_size / 1024); /* mem_upper */
803 stl_phys(mb_bootinfo + 12, 0x8001ffff); /* XXX: use the -boot switch? */
804 stl_phys(mb_bootinfo + 48, mmap_addr); /* mmap_addr */
806 #ifdef DEBUG_MULTIBOOT
807 fprintf(stderr, "multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
808 #endif
810 /* Pass variables to option rom */
811 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_entry_addr);
812 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, mb_bootinfo);
813 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, mmap_addr);
815 /* Make sure we're getting the config space back after reset */
816 option_rom_setup_reset(mb_bootinfo, 0x500);
818 option_rom[nb_option_roms] = "multiboot.bin";
819 nb_option_roms++;
821 return 1; /* yes, we are multiboot */
824 static void load_linux(void *fw_cfg,
825 target_phys_addr_t option_rom,
826 const char *kernel_filename,
827 const char *initrd_filename,
828 const char *kernel_cmdline,
829 target_phys_addr_t max_ram_size)
831 uint16_t protocol;
832 uint32_t gpr[8];
833 uint16_t seg[6];
834 uint16_t real_seg;
835 int setup_size, kernel_size, initrd_size = 0, cmdline_size;
836 uint32_t initrd_max;
837 uint8_t header[8192];
838 target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
839 FILE *f, *fi;
840 char *vmode;
842 /* Align to 16 bytes as a paranoia measure */
843 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
845 /* load the kernel header */
846 f = fopen(kernel_filename, "rb");
847 if (!f || !(kernel_size = get_file_size(f)) ||
848 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
849 MIN(ARRAY_SIZE(header), kernel_size)) {
850 fprintf(stderr, "qemu: could not load kernel '%s'\n",
851 kernel_filename);
852 exit(1);
855 /* kernel protocol version */
856 #if 0
857 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
858 #endif
859 if (ldl_p(header+0x202) == 0x53726448)
860 protocol = lduw_p(header+0x206);
861 else {
862 /* This looks like a multiboot kernel. If it is, let's stop
863 treating it like a Linux kernel. */
864 if (load_multiboot(fw_cfg, f, kernel_filename,
865 initrd_filename, kernel_cmdline, header))
866 return;
867 protocol = 0;
870 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
871 /* Low kernel */
872 real_addr = 0x90000;
873 cmdline_addr = 0x9a000 - cmdline_size;
874 prot_addr = 0x10000;
875 } else if (protocol < 0x202) {
876 /* High but ancient kernel */
877 real_addr = 0x90000;
878 cmdline_addr = 0x9a000 - cmdline_size;
879 prot_addr = 0x100000;
880 } else {
881 /* High and recent kernel */
882 real_addr = 0x10000;
883 cmdline_addr = 0x20000;
884 prot_addr = 0x100000;
887 #if 0
888 fprintf(stderr,
889 "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
890 "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
891 "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
892 real_addr,
893 cmdline_addr,
894 prot_addr);
895 #endif
897 /* highest address for loading the initrd */
898 if (protocol >= 0x203)
899 initrd_max = ldl_p(header+0x22c);
900 else
901 initrd_max = 0x37ffffff;
903 if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
904 initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
906 /* kernel command line */
907 pstrcpy_targphys(cmdline_addr, 4096, kernel_cmdline);
909 if (protocol >= 0x202) {
910 stl_p(header+0x228, cmdline_addr);
911 } else {
912 stw_p(header+0x20, 0xA33F);
913 stw_p(header+0x22, cmdline_addr-real_addr);
916 /* handle vga= parameter */
917 vmode = strstr(kernel_cmdline, "vga=");
918 if (vmode) {
919 unsigned int video_mode;
920 /* skip "vga=" */
921 vmode += 4;
922 if (!strncmp(vmode, "normal", 6)) {
923 video_mode = 0xffff;
924 } else if (!strncmp(vmode, "ext", 3)) {
925 video_mode = 0xfffe;
926 } else if (!strncmp(vmode, "ask", 3)) {
927 video_mode = 0xfffd;
928 } else {
929 video_mode = strtol(vmode, NULL, 0);
931 stw_p(header+0x1fa, video_mode);
934 /* loader type */
935 /* High nybble = B reserved for Qemu; low nybble is revision number.
936 If this code is substantially changed, you may want to consider
937 incrementing the revision. */
938 if (protocol >= 0x200)
939 header[0x210] = 0xB0;
941 /* heap */
942 if (protocol >= 0x201) {
943 header[0x211] |= 0x80; /* CAN_USE_HEAP */
944 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
947 /* load initrd */
948 if (initrd_filename) {
949 if (protocol < 0x200) {
950 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
951 exit(1);
954 fi = fopen(initrd_filename, "rb");
955 if (!fi) {
956 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
957 initrd_filename);
958 exit(1);
961 initrd_size = get_file_size(fi);
962 initrd_addr = (initrd_max-initrd_size) & ~4095;
964 if (!fread_targphys_ok(initrd_addr, initrd_size, fi)) {
965 fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
966 initrd_filename);
967 exit(1);
969 fclose(fi);
971 stl_p(header+0x218, initrd_addr);
972 stl_p(header+0x21c, initrd_size);
975 /* store the finalized header and load the rest of the kernel */
976 cpu_physical_memory_write(real_addr, header, ARRAY_SIZE(header));
978 setup_size = header[0x1f1];
979 if (setup_size == 0)
980 setup_size = 4;
982 setup_size = (setup_size+1)*512;
983 /* Size of protected-mode code */
984 kernel_size -= (setup_size > ARRAY_SIZE(header)) ? setup_size : ARRAY_SIZE(header);
986 /* In case we have read too much already, copy that over */
987 if (setup_size < ARRAY_SIZE(header)) {
988 cpu_physical_memory_write(prot_addr, header + setup_size, ARRAY_SIZE(header) - setup_size);
989 prot_addr += (ARRAY_SIZE(header) - setup_size);
990 setup_size = ARRAY_SIZE(header);
993 if (!fread_targphys_ok(real_addr + ARRAY_SIZE(header),
994 setup_size - ARRAY_SIZE(header), f) ||
995 !fread_targphys_ok(prot_addr, kernel_size, f)) {
996 fprintf(stderr, "qemu: read error on kernel '%s'\n",
997 kernel_filename);
998 exit(1);
1000 fclose(f);
1002 /* generate bootsector to set up the initial register state */
1003 real_seg = real_addr >> 4;
1004 seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg;
1005 seg[1] = real_seg+0x20; /* CS */
1006 memset(gpr, 0, sizeof gpr);
1007 gpr[4] = cmdline_addr-real_addr-16; /* SP (-16 is paranoia) */
1009 option_rom_setup_reset(real_addr, setup_size);
1010 option_rom_setup_reset(prot_addr, kernel_size);
1011 option_rom_setup_reset(cmdline_addr, cmdline_size);
1012 if (initrd_filename)
1013 option_rom_setup_reset(initrd_addr, initrd_size);
1015 generate_bootsect(option_rom, gpr, seg, 0);
1018 static const int ide_iobase[2] = { 0x1f0, 0x170 };
1019 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
1020 static const int ide_irq[2] = { 14, 15 };
1022 #define NE2000_NB_MAX 6
1024 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
1025 0x280, 0x380 };
1026 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
1028 static const int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
1029 static const int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
1031 static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
1032 static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
1034 #ifdef HAS_AUDIO
1035 static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
1037 struct soundhw *c;
1039 for (c = soundhw; c->name; ++c) {
1040 if (c->enabled) {
1041 if (c->isa) {
1042 c->init.init_isa(pic);
1043 } else {
1044 if (pci_bus) {
1045 c->init.init_pci(pci_bus);
1051 #endif
1053 static void pc_init_ne2k_isa(NICInfo *nd)
1055 static int nb_ne2k = 0;
1057 if (nb_ne2k == NE2000_NB_MAX)
1058 return;
1059 isa_ne2000_init(ne2000_io[nb_ne2k],
1060 ne2000_irq[nb_ne2k], nd);
1061 nb_ne2k++;
1064 static int load_option_rom(const char *oprom, target_phys_addr_t start,
1065 target_phys_addr_t end)
1067 int size;
1068 char *filename;
1070 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, oprom);
1071 if (filename) {
1072 size = get_image_size(filename);
1073 if (size > 0 && start + size > end) {
1074 fprintf(stderr, "Not enough space to load option rom '%s'\n",
1075 oprom);
1076 exit(1);
1078 size = load_image_targphys(filename, start, end - start);
1079 qemu_free(filename);
1080 } else {
1081 size = -1;
1083 if (size < 0) {
1084 fprintf(stderr, "Could not load option rom '%s'\n", oprom);
1085 exit(1);
1087 /* Round up optiom rom size to the next 2k boundary */
1088 size = (size + 2047) & ~2047;
1089 option_rom_setup_reset(start, size);
1090 return size;
1093 int cpu_is_bsp(CPUState *env)
1095 return env->cpuid_apic_id == 0;
1098 CPUState *pc_new_cpu(const char *cpu_model)
1100 CPUState *env;
1102 env = cpu_init(cpu_model);
1103 if (!env) {
1104 fprintf(stderr, "Unable to find x86 CPU definition\n");
1105 exit(1);
1107 env->kvm_cpu_state.regs_modified = 1;
1108 if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) {
1109 env->cpuid_apic_id = env->cpu_index;
1110 /* APIC reset callback resets cpu */
1111 apic_init(env);
1112 } else {
1113 qemu_register_reset((QEMUResetHandler*)cpu_reset, env);
1116 /* kvm needs this to run after the apic is initialized. Otherwise,
1117 * it can access invalid state and crash.
1119 qemu_init_vcpu(env);
1120 return env;
1123 /* PC hardware initialisation */
1124 static void pc_init1(ram_addr_t ram_size,
1125 const char *boot_device,
1126 const char *kernel_filename,
1127 const char *kernel_cmdline,
1128 const char *initrd_filename,
1129 const char *cpu_model,
1130 int pci_enabled)
1132 char *filename;
1133 int ret, linux_boot, i;
1134 ram_addr_t ram_addr, bios_offset, option_rom_offset;
1135 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
1136 int bios_size, isa_bios_size, oprom_area_size;
1137 int pci_option_rom_offset = 0;
1138 PCIBus *pci_bus;
1139 ISADevice *isa_dev;
1140 int piix3_devfn = -1;
1141 CPUState *env;
1142 qemu_irq *cpu_irq;
1143 qemu_irq *isa_irq;
1144 qemu_irq *i8259;
1145 IsaIrqState *isa_irq_state;
1146 DriveInfo *dinfo;
1147 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
1148 BlockDriverState *fd[MAX_FD];
1149 int using_vga = cirrus_vga_enabled || std_vga_enabled || vmsvga_enabled;
1150 void *fw_cfg;
1152 if (ram_size >= 0xe0000000 ) {
1153 above_4g_mem_size = ram_size - 0xe0000000;
1154 below_4g_mem_size = 0xe0000000;
1155 } else {
1156 below_4g_mem_size = ram_size;
1159 linux_boot = (kernel_filename != NULL);
1161 /* init CPUs */
1162 if (cpu_model == NULL) {
1163 #ifdef TARGET_X86_64
1164 cpu_model = "qemu64";
1165 #else
1166 cpu_model = "qemu32";
1167 #endif
1170 if (kvm_enabled()) {
1171 kvm_set_boot_cpu_id(0);
1173 for (i = 0; i < smp_cpus; i++) {
1174 env = pc_new_cpu(cpu_model);
1177 vmport_init();
1179 /* allocate RAM */
1180 ram_addr = qemu_ram_alloc(below_4g_mem_size);
1181 cpu_register_physical_memory(0, 0xa0000, ram_addr);
1182 cpu_register_physical_memory(0x100000,
1183 below_4g_mem_size - 0x100000,
1184 ram_addr + 0x100000);
1186 /* above 4giga memory allocation */
1187 if (above_4g_mem_size > 0) {
1188 #if TARGET_PHYS_ADDR_BITS == 32
1189 hw_error("To much RAM for 32-bit physical address");
1190 #else
1191 ram_addr = qemu_ram_alloc(above_4g_mem_size);
1192 cpu_register_physical_memory(0x100000000ULL,
1193 above_4g_mem_size,
1194 ram_addr);
1195 #endif
1199 /* BIOS load */
1200 if (bios_name == NULL)
1201 bios_name = BIOS_FILENAME;
1202 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1203 if (filename) {
1204 bios_size = get_image_size(filename);
1205 } else {
1206 bios_size = -1;
1208 if (bios_size <= 0 ||
1209 (bios_size % 65536) != 0) {
1210 goto bios_error;
1212 bios_offset = qemu_ram_alloc(bios_size);
1213 ret = load_image(filename, qemu_get_ram_ptr(bios_offset));
1214 if (ret != bios_size) {
1215 bios_error:
1216 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1217 exit(1);
1219 if (filename) {
1220 qemu_free(filename);
1222 /* map the last 128KB of the BIOS in ISA space */
1223 isa_bios_size = bios_size;
1224 if (isa_bios_size > (128 * 1024))
1225 isa_bios_size = 128 * 1024;
1226 cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size,
1227 IO_MEM_UNASSIGNED);
1228 /* kvm tpr optimization needs the bios accessible for write, at least to qemu itself */
1229 cpu_register_physical_memory(0x100000 - isa_bios_size,
1230 isa_bios_size,
1231 (bios_offset + bios_size - isa_bios_size) /* | IO_MEM_ROM */);
1233 if (extboot_drive) {
1234 option_rom[nb_option_roms++] = qemu_strdup(EXTBOOT_FILENAME);
1237 option_rom_offset = qemu_ram_alloc(0x20000);
1238 oprom_area_size = 0;
1239 cpu_register_physical_memory(0xc0000, 0x20000, option_rom_offset);
1241 if (using_vga) {
1242 const char *vgabios_filename;
1243 /* VGA BIOS load */
1244 if (cirrus_vga_enabled) {
1245 vgabios_filename = VGABIOS_CIRRUS_FILENAME;
1246 } else {
1247 vgabios_filename = VGABIOS_FILENAME;
1249 oprom_area_size = load_option_rom(vgabios_filename, 0xc0000, 0xe0000);
1250 pci_option_rom_offset = oprom_area_size;
1252 /* Although video roms can grow larger than 0x8000, the area between
1253 * 0xc0000 - 0xc8000 is reserved for them. It means we won't be looking
1254 * for any other kind of option rom inside this area */
1255 if (oprom_area_size < 0x8000)
1256 oprom_area_size = 0x8000;
1258 /* map all the bios at the top of memory */
1259 cpu_register_physical_memory((uint32_t)(-bios_size),
1260 bios_size, bios_offset | IO_MEM_ROM);
1262 fw_cfg = bochs_bios_init();
1264 if (linux_boot) {
1265 load_linux(fw_cfg, 0xc0000 + oprom_area_size,
1266 kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
1267 oprom_area_size += 2048;
1270 for (i = 0; i < nb_option_roms; i++) {
1271 oprom_area_size += load_option_rom(option_rom[i], 0xc0000 + oprom_area_size,
1272 0xe0000);
1275 for (i = 0; i < nb_nics; i++) {
1276 char nic_oprom[1024];
1277 const char *model = nd_table[i].model;
1279 if (!nd_table[i].bootable)
1280 continue;
1282 if (model == NULL)
1283 model = "rtl8139";
1284 snprintf(nic_oprom, sizeof(nic_oprom), "pxe-%s.bin", model);
1286 oprom_area_size += load_option_rom(nic_oprom, 0xc0000 + oprom_area_size,
1287 0xe0000);
1290 cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
1291 #ifdef KVM_CAP_IRQCHIP
1292 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
1293 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
1294 isa_irq = i8259 = kvm_i8259_init(cpu_irq[0]);
1295 } else
1296 #endif
1298 i8259 = i8259_init(cpu_irq[0]);
1299 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
1300 isa_irq_state->i8259 = i8259;
1301 isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 24);
1304 if (pci_enabled) {
1305 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, isa_irq);
1306 } else {
1307 pci_bus = NULL;
1308 isa_bus_new(NULL);
1310 isa_bus_irqs(isa_irq);
1312 ferr_irq = isa_reserve_irq(13);
1314 /* init basic PC hardware */
1315 register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
1317 register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
1319 if (cirrus_vga_enabled) {
1320 if (pci_enabled) {
1321 pci_cirrus_vga_init(pci_bus);
1322 } else {
1323 isa_cirrus_vga_init();
1325 } else if (vmsvga_enabled) {
1326 if (pci_enabled)
1327 pci_vmsvga_init(pci_bus);
1328 else
1329 fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
1330 } else if (std_vga_enabled) {
1331 if (pci_enabled) {
1332 pci_vga_init(pci_bus, 0, 0);
1333 } else {
1334 isa_vga_init();
1338 rtc_state = rtc_init(2000);
1340 qemu_register_boot_set(pc_boot_set, rtc_state);
1342 register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
1343 register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
1345 if (pci_enabled) {
1346 isa_irq_state->ioapic = ioapic_init();
1347 ioapic_irq_hack = isa_irq;
1349 #ifdef CONFIG_KVM_PIT
1350 if (kvm_enabled() && qemu_kvm_pit_in_kernel())
1351 pit = kvm_pit_init(0x40, isa_reserve_irq(0));
1352 else
1353 #endif
1354 pit = pit_init(0x40, isa_reserve_irq(0));
1355 pcspk_init(pit);
1356 if (!no_hpet) {
1357 hpet_init(isa_irq);
1360 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
1361 if (serial_hds[i]) {
1362 serial_init(serial_io[i], isa_reserve_irq(serial_irq[i]), 115200,
1363 serial_hds[i]);
1367 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
1368 if (parallel_hds[i]) {
1369 parallel_init(parallel_io[i], isa_reserve_irq(parallel_irq[i]),
1370 parallel_hds[i]);
1374 for(i = 0; i < nb_nics; i++) {
1375 NICInfo *nd = &nd_table[i];
1377 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
1378 pc_init_ne2k_isa(nd);
1379 else
1380 pci_nic_init(nd, "rtl8139", NULL);
1383 piix4_acpi_system_hot_add_init(cpu_model);
1385 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
1386 fprintf(stderr, "qemu: too many IDE bus\n");
1387 exit(1);
1390 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
1391 hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
1394 if (pci_enabled) {
1395 pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
1396 } else {
1397 for(i = 0; i < MAX_IDE_BUS; i++) {
1398 isa_ide_init(ide_iobase[i], ide_iobase2[i],
1399 isa_reserve_irq(ide_irq[i]),
1400 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
1404 isa_dev = isa_create_simple("i8042");
1405 DMA_init(0);
1406 #ifdef HAS_AUDIO
1407 audio_init(pci_enabled ? pci_bus : NULL, isa_irq);
1408 #endif
1410 for(i = 0; i < MAX_FD; i++) {
1411 dinfo = drive_get(IF_FLOPPY, 0, i);
1412 fd[i] = dinfo ? dinfo->bdrv : NULL;
1414 floppy_controller = fdctrl_init_isa(fd);
1416 cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
1418 if (pci_enabled && usb_enabled) {
1419 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
1422 if (pci_enabled && acpi_enabled) {
1423 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
1424 i2c_bus *smbus;
1426 /* TODO: Populate SPD eeprom data. */
1427 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
1428 isa_reserve_irq(9));
1429 for (i = 0; i < 8; i++) {
1430 DeviceState *eeprom;
1431 eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
1432 qdev_prop_set_uint32(eeprom, "address", 0x50 + i);
1433 qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
1434 qdev_init(eeprom);
1438 if (i440fx_state) {
1439 i440fx_init_memory_mappings(i440fx_state);
1442 if (pci_enabled) {
1443 int max_bus;
1444 int bus;
1446 max_bus = drive_get_max_bus(IF_SCSI);
1447 for (bus = 0; bus <= max_bus; bus++) {
1448 pci_create_simple(pci_bus, -1, "lsi53c895a");
1452 if (extboot_drive) {
1453 DriveInfo *info = extboot_drive;
1454 int cyls, heads, secs;
1456 if (info->type != IF_IDE && info->type != IF_VIRTIO) {
1457 bdrv_guess_geometry(info->bdrv, &cyls, &heads, &secs);
1458 bdrv_set_geometry_hint(info->bdrv, cyls, heads, secs);
1461 extboot_init(info->bdrv, 1);
1464 /* Add virtio console devices */
1465 if (pci_enabled) {
1466 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
1467 if (virtcon_hds[i]) {
1468 pci_create_simple(pci_bus, -1, "virtio-console-pci");
1473 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
1474 if (kvm_enabled()) {
1475 add_assigned_devices(pci_bus, assigned_devices, assigned_devices_index);
1476 assigned_dev_load_option_roms(pci_option_rom_offset);
1478 #endif /* CONFIG_KVM_DEVICE_ASSIGNMENT */
1481 static void pc_init_pci(ram_addr_t ram_size,
1482 const char *boot_device,
1483 const char *kernel_filename,
1484 const char *kernel_cmdline,
1485 const char *initrd_filename,
1486 const char *cpu_model)
1488 pc_init1(ram_size, boot_device,
1489 kernel_filename, kernel_cmdline,
1490 initrd_filename, cpu_model, 1);
1493 static void pc_init_isa(ram_addr_t ram_size,
1494 const char *boot_device,
1495 const char *kernel_filename,
1496 const char *kernel_cmdline,
1497 const char *initrd_filename,
1498 const char *cpu_model)
1500 if (cpu_model == NULL)
1501 cpu_model = "486";
1502 pc_init1(ram_size, boot_device,
1503 kernel_filename, kernel_cmdline,
1504 initrd_filename, cpu_model, 0);
1507 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
1508 BIOS will read it and start S3 resume at POST Entry */
1509 void cmos_set_s3_resume(void)
1511 if (rtc_state)
1512 rtc_set_memory(rtc_state, 0xF, 0xFE);
1515 static QEMUMachine pc_machine = {
1516 .name = "pc-0.11",
1517 .alias = "pc",
1518 .desc = "Standard PC",
1519 .init = pc_init_pci,
1520 .max_cpus = 255,
1521 .is_default = 1,
1524 static QEMUMachine pc_machine_v0_10 = {
1525 .name = "pc-0.10",
1526 .desc = "Standard PC, qemu 0.10",
1527 .init = pc_init_pci,
1528 .max_cpus = 255,
1529 .compat_props = (CompatProperty[]) {
1531 .driver = "virtio-blk-pci",
1532 .property = "class",
1533 .value = stringify(PCI_CLASS_STORAGE_OTHER),
1535 .driver = "virtio-console-pci",
1536 .property = "class",
1537 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
1539 .driver = "virtio-net-pci",
1540 .property = "vectors",
1541 .value = stringify(0),
1543 .driver = "virtio-blk-pci",
1544 .property = "vectors",
1545 .value = stringify(0),
1547 { /* end of list */ }
1551 static QEMUMachine isapc_machine = {
1552 .name = "isapc",
1553 .desc = "ISA-only PC",
1554 .init = pc_init_isa,
1555 .max_cpus = 1,
1558 static void pc_machine_init(void)
1560 qemu_register_machine(&pc_machine);
1561 qemu_register_machine(&pc_machine_v0_10);
1562 qemu_register_machine(&isapc_machine);
1565 machine_init(pc_machine_init);