]> git.proxmox.com Git - qemu.git/blob - hw/pc.c
2dca777c95119eeac90f4ae5c972fcd442716a8f
[qemu.git] / hw / pc.c
1 /*
2 * QEMU PC System Emulator
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include "hw.h"
25 #include "pc.h"
26 #include "fdc.h"
27 #include "pci.h"
28 #include "vmware_vga.h"
29 #include "usb-uhci.h"
30 #include "usb-ohci.h"
31 #include "prep_pci.h"
32 #include "apb_pci.h"
33 #include "block.h"
34 #include "sysemu.h"
35 #include "audio/audio.h"
36 #include "net.h"
37 #include "smbus.h"
38 #include "boards.h"
39 #include "monitor.h"
40 #include "fw_cfg.h"
41 #include "hpet_emul.h"
42 #include "watchdog.h"
43 #include "smbios.h"
44 #include "ide.h"
45 #include "loader.h"
46 #include "elf.h"
47
48 /* output Bochs bios info messages */
49 //#define DEBUG_BIOS
50
51 /* Show multiboot debug output */
52 //#define DEBUG_MULTIBOOT
53
54 #ifdef DEBUG_MULTIBOOT
55 #define mb_debug(a...) fprintf(stderr, ## a)
56 #else
57 #define mb_debug(a...)
58 #endif
59
60 #define BIOS_FILENAME "bios.bin"
61
62 #define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
63
64 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
65 #define ACPI_DATA_SIZE 0x10000
66 #define BIOS_CFG_IOPORT 0x510
67 #define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
68 #define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
69 #define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
70
71 #define MAX_IDE_BUS 2
72
73 static fdctrl_t *floppy_controller;
74 static RTCState *rtc_state;
75 static PITState *pit;
76 static PCII440FXState *i440fx_state;
77
78 typedef struct isa_irq_state {
79 qemu_irq *i8259;
80 qemu_irq *ioapic;
81 } IsaIrqState;
82
83 static void isa_irq_handler(void *opaque, int n, int level)
84 {
85 IsaIrqState *isa = (IsaIrqState *)opaque;
86
87 if (n < 16) {
88 qemu_set_irq(isa->i8259[n], level);
89 }
90 if (isa->ioapic)
91 qemu_set_irq(isa->ioapic[n], level);
92 };
93
94 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
95 {
96 }
97
98 /* MSDOS compatibility mode FPU exception support */
99 static qemu_irq ferr_irq;
100 /* XXX: add IGNNE support */
101 void cpu_set_ferr(CPUX86State *s)
102 {
103 qemu_irq_raise(ferr_irq);
104 }
105
106 static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
107 {
108 qemu_irq_lower(ferr_irq);
109 }
110
111 /* TSC handling */
112 uint64_t cpu_get_tsc(CPUX86State *env)
113 {
114 return cpu_get_ticks();
115 }
116
117 /* SMM support */
118 void cpu_smm_update(CPUState *env)
119 {
120 if (i440fx_state && env == first_cpu)
121 i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
122 }
123
124
125 /* IRQ handling */
126 int cpu_get_pic_interrupt(CPUState *env)
127 {
128 int intno;
129
130 intno = apic_get_interrupt(env);
131 if (intno >= 0) {
132 /* set irq request if a PIC irq is still pending */
133 /* XXX: improve that */
134 pic_update_irq(isa_pic);
135 return intno;
136 }
137 /* read the irq from the PIC */
138 if (!apic_accept_pic_intr(env))
139 return -1;
140
141 intno = pic_read_irq(isa_pic);
142 return intno;
143 }
144
145 static void pic_irq_request(void *opaque, int irq, int level)
146 {
147 CPUState *env = first_cpu;
148
149 if (env->apic_state) {
150 while (env) {
151 if (apic_accept_pic_intr(env))
152 apic_deliver_pic_intr(env, level);
153 env = env->next_cpu;
154 }
155 } else {
156 if (level)
157 cpu_interrupt(env, CPU_INTERRUPT_HARD);
158 else
159 cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
160 }
161 }
162
163 /* PC cmos mappings */
164
165 #define REG_EQUIPMENT_BYTE 0x14
166
167 static int cmos_get_fd_drive_type(int fd0)
168 {
169 int val;
170
171 switch (fd0) {
172 case 0:
173 /* 1.44 Mb 3"5 drive */
174 val = 4;
175 break;
176 case 1:
177 /* 2.88 Mb 3"5 drive */
178 val = 5;
179 break;
180 case 2:
181 /* 1.2 Mb 5"5 drive */
182 val = 2;
183 break;
184 default:
185 val = 0;
186 break;
187 }
188 return val;
189 }
190
191 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
192 {
193 RTCState *s = rtc_state;
194 int cylinders, heads, sectors;
195 bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
196 rtc_set_memory(s, type_ofs, 47);
197 rtc_set_memory(s, info_ofs, cylinders);
198 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
199 rtc_set_memory(s, info_ofs + 2, heads);
200 rtc_set_memory(s, info_ofs + 3, 0xff);
201 rtc_set_memory(s, info_ofs + 4, 0xff);
202 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
203 rtc_set_memory(s, info_ofs + 6, cylinders);
204 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
205 rtc_set_memory(s, info_ofs + 8, sectors);
206 }
207
208 /* convert boot_device letter to something recognizable by the bios */
209 static int boot_device2nibble(char boot_device)
210 {
211 switch(boot_device) {
212 case 'a':
213 case 'b':
214 return 0x01; /* floppy boot */
215 case 'c':
216 return 0x02; /* hard drive boot */
217 case 'd':
218 return 0x03; /* CD-ROM boot */
219 case 'n':
220 return 0x04; /* Network boot */
221 }
222 return 0;
223 }
224
225 /* copy/pasted from cmos_init, should be made a general function
226 and used there as well */
227 static int pc_boot_set(void *opaque, const char *boot_device)
228 {
229 Monitor *mon = cur_mon;
230 #define PC_MAX_BOOT_DEVICES 3
231 RTCState *s = (RTCState *)opaque;
232 int nbds, bds[3] = { 0, };
233 int i;
234
235 nbds = strlen(boot_device);
236 if (nbds > PC_MAX_BOOT_DEVICES) {
237 monitor_printf(mon, "Too many boot devices for PC\n");
238 return(1);
239 }
240 for (i = 0; i < nbds; i++) {
241 bds[i] = boot_device2nibble(boot_device[i]);
242 if (bds[i] == 0) {
243 monitor_printf(mon, "Invalid boot device for PC: '%c'\n",
244 boot_device[i]);
245 return(1);
246 }
247 }
248 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
249 rtc_set_memory(s, 0x38, (bds[2] << 4));
250 return(0);
251 }
252
253 /* hd_table must contain 4 block drivers */
254 static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
255 const char *boot_device, DriveInfo **hd_table)
256 {
257 RTCState *s = rtc_state;
258 int nbds, bds[3] = { 0, };
259 int val;
260 int fd0, fd1, nb;
261 int i;
262
263 /* various important CMOS locations needed by PC/Bochs bios */
264
265 /* memory size */
266 val = 640; /* base memory in K */
267 rtc_set_memory(s, 0x15, val);
268 rtc_set_memory(s, 0x16, val >> 8);
269
270 val = (ram_size / 1024) - 1024;
271 if (val > 65535)
272 val = 65535;
273 rtc_set_memory(s, 0x17, val);
274 rtc_set_memory(s, 0x18, val >> 8);
275 rtc_set_memory(s, 0x30, val);
276 rtc_set_memory(s, 0x31, val >> 8);
277
278 if (above_4g_mem_size) {
279 rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
280 rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
281 rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
282 }
283
284 if (ram_size > (16 * 1024 * 1024))
285 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
286 else
287 val = 0;
288 if (val > 65535)
289 val = 65535;
290 rtc_set_memory(s, 0x34, val);
291 rtc_set_memory(s, 0x35, val >> 8);
292
293 /* set the number of CPU */
294 rtc_set_memory(s, 0x5f, smp_cpus - 1);
295
296 /* set boot devices, and disable floppy signature check if requested */
297 #define PC_MAX_BOOT_DEVICES 3
298 nbds = strlen(boot_device);
299 if (nbds > PC_MAX_BOOT_DEVICES) {
300 fprintf(stderr, "Too many boot devices for PC\n");
301 exit(1);
302 }
303 for (i = 0; i < nbds; i++) {
304 bds[i] = boot_device2nibble(boot_device[i]);
305 if (bds[i] == 0) {
306 fprintf(stderr, "Invalid boot device for PC: '%c'\n",
307 boot_device[i]);
308 exit(1);
309 }
310 }
311 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
312 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
313
314 /* floppy type */
315
316 fd0 = fdctrl_get_drive_type(floppy_controller, 0);
317 fd1 = fdctrl_get_drive_type(floppy_controller, 1);
318
319 val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
320 rtc_set_memory(s, 0x10, val);
321
322 val = 0;
323 nb = 0;
324 if (fd0 < 3)
325 nb++;
326 if (fd1 < 3)
327 nb++;
328 switch (nb) {
329 case 0:
330 break;
331 case 1:
332 val |= 0x01; /* 1 drive, ready for boot */
333 break;
334 case 2:
335 val |= 0x41; /* 2 drives, ready for boot */
336 break;
337 }
338 val |= 0x02; /* FPU is there */
339 val |= 0x04; /* PS/2 mouse installed */
340 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
341
342 /* hard drives */
343
344 rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
345 if (hd_table[0])
346 cmos_init_hd(0x19, 0x1b, hd_table[0]->bdrv);
347 if (hd_table[1])
348 cmos_init_hd(0x1a, 0x24, hd_table[1]->bdrv);
349
350 val = 0;
351 for (i = 0; i < 4; i++) {
352 if (hd_table[i]) {
353 int cylinders, heads, sectors, translation;
354 /* NOTE: bdrv_get_geometry_hint() returns the physical
355 geometry. It is always such that: 1 <= sects <= 63, 1
356 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
357 geometry can be different if a translation is done. */
358 translation = bdrv_get_translation_hint(hd_table[i]->bdrv);
359 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
360 bdrv_get_geometry_hint(hd_table[i]->bdrv, &cylinders, &heads, &sectors);
361 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
362 /* No translation. */
363 translation = 0;
364 } else {
365 /* LBA translation. */
366 translation = 1;
367 }
368 } else {
369 translation--;
370 }
371 val |= translation << (i * 2);
372 }
373 }
374 rtc_set_memory(s, 0x39, val);
375 }
376
377 void ioport_set_a20(int enable)
378 {
379 /* XXX: send to all CPUs ? */
380 cpu_x86_set_a20(first_cpu, enable);
381 }
382
383 int ioport_get_a20(void)
384 {
385 return ((first_cpu->a20_mask >> 20) & 1);
386 }
387
388 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
389 {
390 ioport_set_a20((val >> 1) & 1);
391 /* XXX: bit 0 is fast reset */
392 }
393
394 static uint32_t ioport92_read(void *opaque, uint32_t addr)
395 {
396 return ioport_get_a20() << 1;
397 }
398
399 /***********************************************************/
400 /* Bochs BIOS debug ports */
401
402 static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
403 {
404 static const char shutdown_str[8] = "Shutdown";
405 static int shutdown_index = 0;
406
407 switch(addr) {
408 /* Bochs BIOS messages */
409 case 0x400:
410 case 0x401:
411 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
412 exit(1);
413 case 0x402:
414 case 0x403:
415 #ifdef DEBUG_BIOS
416 fprintf(stderr, "%c", val);
417 #endif
418 break;
419 case 0x8900:
420 /* same as Bochs power off */
421 if (val == shutdown_str[shutdown_index]) {
422 shutdown_index++;
423 if (shutdown_index == 8) {
424 shutdown_index = 0;
425 qemu_system_shutdown_request();
426 }
427 } else {
428 shutdown_index = 0;
429 }
430 break;
431
432 /* LGPL'ed VGA BIOS messages */
433 case 0x501:
434 case 0x502:
435 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
436 exit(1);
437 case 0x500:
438 case 0x503:
439 #ifdef DEBUG_BIOS
440 fprintf(stderr, "%c", val);
441 #endif
442 break;
443 }
444 }
445
446 static void *bochs_bios_init(void)
447 {
448 void *fw_cfg;
449 uint8_t *smbios_table;
450 size_t smbios_len;
451 uint64_t *numa_fw_cfg;
452 int i, j;
453
454 register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
455 register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
456 register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
457 register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
458 register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
459
460 register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
461 register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
462 register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
463 register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
464
465 fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
466
467 fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
468 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
469 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
470 acpi_tables_len);
471 fw_cfg_add_bytes(fw_cfg, FW_CFG_IRQ0_OVERRIDE, &irq0override, 1);
472
473 smbios_table = smbios_get_table(&smbios_len);
474 if (smbios_table)
475 fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
476 smbios_table, smbios_len);
477
478 /* allocate memory for the NUMA channel: one (64bit) word for the number
479 * of nodes, one word for each VCPU->node and one word for each node to
480 * hold the amount of memory.
481 */
482 numa_fw_cfg = qemu_mallocz((1 + smp_cpus + nb_numa_nodes) * 8);
483 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
484 for (i = 0; i < smp_cpus; i++) {
485 for (j = 0; j < nb_numa_nodes; j++) {
486 if (node_cpumask[j] & (1 << i)) {
487 numa_fw_cfg[i + 1] = cpu_to_le64(j);
488 break;
489 }
490 }
491 }
492 for (i = 0; i < nb_numa_nodes; i++) {
493 numa_fw_cfg[smp_cpus + 1 + i] = cpu_to_le64(node_mem[i]);
494 }
495 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, (uint8_t *)numa_fw_cfg,
496 (1 + smp_cpus + nb_numa_nodes) * 8);
497
498 return fw_cfg;
499 }
500
501 static long get_file_size(FILE *f)
502 {
503 long where, size;
504
505 /* XXX: on Unix systems, using fstat() probably makes more sense */
506
507 where = ftell(f);
508 fseek(f, 0, SEEK_END);
509 size = ftell(f);
510 fseek(f, where, SEEK_SET);
511
512 return size;
513 }
514
515 #define MULTIBOOT_STRUCT_ADDR 0x9000
516
517 #if MULTIBOOT_STRUCT_ADDR > 0xf0000
518 #error multiboot struct needs to fit in 16 bit real mode
519 #endif
520
521 enum {
522 /* Multiboot info */
523 MBI_FLAGS = 0,
524 MBI_MEM_LOWER = 4,
525 MBI_MEM_UPPER = 8,
526 MBI_BOOT_DEVICE = 12,
527 MBI_CMDLINE = 16,
528 MBI_MODS_COUNT = 20,
529 MBI_MODS_ADDR = 24,
530 MBI_MMAP_ADDR = 48,
531
532 MBI_SIZE = 88,
533
534 /* Multiboot modules */
535 MB_MOD_START = 0,
536 MB_MOD_END = 4,
537 MB_MOD_CMDLINE = 8,
538
539 MB_MOD_SIZE = 16,
540
541 /* Region offsets */
542 ADDR_E820_MAP = MULTIBOOT_STRUCT_ADDR + 0,
543 ADDR_MBI = ADDR_E820_MAP + 0x500,
544
545 /* Multiboot flags */
546 MULTIBOOT_FLAGS_MEMORY = 1 << 0,
547 MULTIBOOT_FLAGS_BOOT_DEVICE = 1 << 1,
548 MULTIBOOT_FLAGS_CMDLINE = 1 << 2,
549 MULTIBOOT_FLAGS_MODULES = 1 << 3,
550 MULTIBOOT_FLAGS_MMAP = 1 << 6,
551 };
552
553 typedef struct {
554 /* buffer holding kernel, cmdlines and mb_infos */
555 void *mb_buf;
556 /* address in target */
557 target_phys_addr_t mb_buf_phys;
558 /* size of mb_buf in bytes */
559 unsigned mb_buf_size;
560 /* offset of mb-info's in bytes */
561 target_phys_addr_t offset_mbinfo;
562 /* offset in buffer for cmdlines in bytes */
563 target_phys_addr_t offset_cmdlines;
564 /* offset of modules in bytes */
565 target_phys_addr_t offset_mods;
566 /* available slots for mb modules infos */
567 int mb_mods_avail;
568 /* currently used slots of mb modules */
569 int mb_mods_count;
570 } MultibootState;
571
572 static uint32_t mb_add_cmdline(MultibootState *s, const char *cmdline)
573 {
574 int len = strlen(cmdline) + 1;
575 target_phys_addr_t p = s->offset_cmdlines;
576
577 pstrcpy((char *)s->mb_buf + p, len, cmdline);
578 s->offset_cmdlines += len;
579 return s->mb_buf_phys + p;
580 }
581
582 static void mb_add_mod(MultibootState *s,
583 target_phys_addr_t start, target_phys_addr_t end,
584 target_phys_addr_t cmdline_phys)
585 {
586 char *p;
587 assert(s->mb_mods_count < s->mb_mods_avail);
588
589 p = (char *)s->mb_buf + s->offset_mbinfo + MB_MOD_SIZE * s->mb_mods_count;
590
591 stl_p(p + MB_MOD_START, start);
592 stl_p(p + MB_MOD_END, end);
593 stl_p(p + MB_MOD_CMDLINE, cmdline_phys);
594
595 mb_debug("mod%02d: %08x - %08x\n", s->mb_mods_count, start, end);
596
597 s->mb_mods_count++;
598 }
599
600 static int load_multiboot(void *fw_cfg,
601 FILE *f,
602 const char *kernel_filename,
603 const char *initrd_filename,
604 const char *kernel_cmdline,
605 uint8_t *header)
606 {
607 int i, is_multiboot = 0;
608 uint32_t flags = 0;
609 uint32_t mh_entry_addr;
610 uint32_t mh_load_addr;
611 uint32_t mb_kernel_size;
612 MultibootState mbs;
613 uint8_t bootinfo[MBI_SIZE];
614 uint8_t *mb_bootinfo_data;
615
616 /* Ok, let's see if it is a multiboot image.
617 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
618 for (i = 0; i < (8192 - 48); i += 4) {
619 if (ldl_p(header+i) == 0x1BADB002) {
620 uint32_t checksum = ldl_p(header+i+8);
621 flags = ldl_p(header+i+4);
622 checksum += flags;
623 checksum += (uint32_t)0x1BADB002;
624 if (!checksum) {
625 is_multiboot = 1;
626 break;
627 }
628 }
629 }
630
631 if (!is_multiboot)
632 return 0; /* no multiboot */
633
634 mb_debug("qemu: I believe we found a multiboot image!\n");
635 memset(bootinfo, 0, sizeof(bootinfo));
636 memset(&mbs, 0, sizeof(mbs));
637
638 if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
639 fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
640 }
641 if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
642 uint64_t elf_entry;
643 uint64_t elf_low, elf_high;
644 int kernel_size;
645 fclose(f);
646 kernel_size = load_elf(kernel_filename, 0, &elf_entry, &elf_low, &elf_high,
647 0, ELF_MACHINE, 0);
648 if (kernel_size < 0) {
649 fprintf(stderr, "Error while loading elf kernel\n");
650 exit(1);
651 }
652 mh_load_addr = elf_low;
653 mb_kernel_size = elf_high - elf_low;
654 mh_entry_addr = elf_entry;
655
656 mbs.mb_buf = qemu_malloc(mb_kernel_size);
657 if (rom_copy(mbs.mb_buf, mh_load_addr, mb_kernel_size) != mb_kernel_size) {
658 fprintf(stderr, "Error while fetching elf kernel from rom\n");
659 exit(1);
660 }
661
662 mb_debug("qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
663 mb_kernel_size, (size_t)mh_entry_addr);
664 } else {
665 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
666 uint32_t mh_header_addr = ldl_p(header+i+12);
667 mh_load_addr = ldl_p(header+i+16);
668 uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
669
670 mh_entry_addr = ldl_p(header+i+28);
671 mb_kernel_size = get_file_size(f) - mb_kernel_text_offset;
672
673 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
674 uint32_t mh_mode_type = ldl_p(header+i+32);
675 uint32_t mh_width = ldl_p(header+i+36);
676 uint32_t mh_height = ldl_p(header+i+40);
677 uint32_t mh_depth = ldl_p(header+i+44); */
678
679 mb_debug("multiboot: mh_header_addr = %#x\n", mh_header_addr);
680 mb_debug("multiboot: mh_load_addr = %#x\n", mh_load_addr);
681 mb_debug("multiboot: mh_load_end_addr = %#x\n", ldl_p(header+i+20));
682 mb_debug("multiboot: mh_bss_end_addr = %#x\n", ldl_p(header+i+24));
683 mb_debug("qemu: loading multiboot kernel (%#x bytes) at %#x\n",
684 mb_kernel_size, mh_load_addr);
685
686 mbs.mb_buf = qemu_malloc(mb_kernel_size);
687 fseek(f, mb_kernel_text_offset, SEEK_SET);
688 if (fread(mbs.mb_buf, 1, mb_kernel_size, f) != mb_kernel_size) {
689 fprintf(stderr, "fread() failed\n");
690 exit(1);
691 }
692 fclose(f);
693 }
694
695 mbs.mb_buf_phys = mh_load_addr;
696
697 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_kernel_size);
698 mbs.offset_mbinfo = mbs.mb_buf_size;
699
700 /* Calculate space for cmdlines and mb_mods */
701 mbs.mb_buf_size += strlen(kernel_filename) + 1;
702 mbs.mb_buf_size += strlen(kernel_cmdline) + 1;
703 if (initrd_filename) {
704 const char *r = initrd_filename;
705 mbs.mb_buf_size += strlen(r) + 1;
706 mbs.mb_mods_avail = 1;
707 while ((r = strchr(r, ','))) {
708 mbs.mb_mods_avail++;
709 r++;
710 }
711 mbs.mb_buf_size += MB_MOD_SIZE * mbs.mb_mods_avail;
712 }
713
714 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mbs.mb_buf_size);
715
716 /* enlarge mb_buf to hold cmdlines and mb-info structs */
717 mbs.mb_buf = qemu_realloc(mbs.mb_buf, mbs.mb_buf_size);
718 mbs.offset_cmdlines = mbs.offset_mbinfo + mbs.mb_mods_avail * MB_MOD_SIZE;
719
720 if (initrd_filename) {
721 char *next_initrd;
722
723 mbs.offset_mods = mbs.mb_buf_size;
724
725 do {
726 char *next_space;
727 uint32_t mb_mod_length;
728 uint32_t offs = mbs.mb_buf_size;
729
730 next_initrd = strchr(initrd_filename, ',');
731 if (next_initrd)
732 *next_initrd = '\0';
733 /* if a space comes after the module filename, treat everything
734 after that as parameters */
735 target_phys_addr_t c = mb_add_cmdline(&mbs, initrd_filename);
736 if ((next_space = strchr(initrd_filename, ' ')))
737 *next_space = '\0';
738 mb_debug("multiboot loading module: %s\n", initrd_filename);
739 mb_mod_length = get_image_size(initrd_filename);
740 if (mb_mod_length < 0) {
741 fprintf(stderr, "failed to get %s image size\n", initrd_filename);
742 exit(1);
743 }
744
745 mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
746 mbs.mb_buf = qemu_realloc(mbs.mb_buf, mbs.mb_buf_size);
747
748 load_image(initrd_filename, (unsigned char *)mbs.mb_buf + offs);
749 mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
750 mbs.mb_buf_phys + offs + mb_mod_length, c);
751
752 mb_debug("mod_start: %p\nmod_end: %p\n cmdline: %#x\n",
753 (char *)mbs.mb_buf + offs,
754 (char *)mbs.mb_buf + offs + mb_mod_length, c);
755 initrd_filename = next_initrd+1;
756 } while (next_initrd);
757 }
758
759 /* Commandline support */
760 char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
761 snprintf(kcmdline, sizeof(kcmdline), "%s %s",
762 kernel_filename, kernel_cmdline);
763 stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
764
765 stl_p(bootinfo + MBI_MODS_ADDR, mbs.mb_buf_phys + mbs.offset_mbinfo);
766 stl_p(bootinfo + MBI_MODS_COUNT, mbs.mb_mods_count); /* mods_count */
767
768 /* the kernel is where we want it to be now */
769 stl_p(bootinfo + MBI_FLAGS, MULTIBOOT_FLAGS_MEMORY
770 | MULTIBOOT_FLAGS_BOOT_DEVICE
771 | MULTIBOOT_FLAGS_CMDLINE
772 | MULTIBOOT_FLAGS_MODULES
773 | MULTIBOOT_FLAGS_MMAP);
774 stl_p(bootinfo + MBI_MEM_LOWER, 640);
775 stl_p(bootinfo + MBI_MEM_UPPER, ram_size / 1024);
776 stl_p(bootinfo + MBI_BOOT_DEVICE, 0x8001ffff); /* XXX: use the -boot switch? */
777 stl_p(bootinfo + MBI_MMAP_ADDR, ADDR_E820_MAP);
778
779 mb_debug("multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
780 mb_debug(" mb_buf_phys = %x\n", mbs.mb_buf_phys);
781 mb_debug(" mod_start = %x\n", mbs.mb_buf_phys + mbs.offset_mods);
782 mb_debug(" mb_mods_count = %d\n", mbs.mb_mods_count);
783
784 /* save bootinfo off the stack */
785 mb_bootinfo_data = qemu_malloc(sizeof(bootinfo));
786 memcpy(mb_bootinfo_data, bootinfo, sizeof(bootinfo));
787
788 /* Pass variables to option rom */
789 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ENTRY, mh_entry_addr);
790 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_load_addr);
791 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, mbs.mb_buf_size);
792 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA,
793 mbs.mb_buf, mbs.mb_buf_size);
794
795 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, ADDR_MBI);
796 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, sizeof(bootinfo));
797 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, mb_bootinfo_data,
798 sizeof(bootinfo));
799
800 option_rom[nb_option_roms] = "multiboot.bin";
801 nb_option_roms++;
802
803 return 1; /* yes, we are multiboot */
804 }
805
806 static void load_linux(void *fw_cfg,
807 const char *kernel_filename,
808 const char *initrd_filename,
809 const char *kernel_cmdline,
810 target_phys_addr_t max_ram_size)
811 {
812 uint16_t protocol;
813 int setup_size, kernel_size, initrd_size = 0, cmdline_size;
814 uint32_t initrd_max;
815 uint8_t header[8192], *setup, *kernel, *initrd_data;
816 target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
817 FILE *f;
818 char *vmode;
819
820 /* Align to 16 bytes as a paranoia measure */
821 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
822
823 /* load the kernel header */
824 f = fopen(kernel_filename, "rb");
825 if (!f || !(kernel_size = get_file_size(f)) ||
826 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
827 MIN(ARRAY_SIZE(header), kernel_size)) {
828 fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
829 kernel_filename, strerror(errno));
830 exit(1);
831 }
832
833 /* kernel protocol version */
834 #if 0
835 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
836 #endif
837 if (ldl_p(header+0x202) == 0x53726448)
838 protocol = lduw_p(header+0x206);
839 else {
840 /* This looks like a multiboot kernel. If it is, let's stop
841 treating it like a Linux kernel. */
842 if (load_multiboot(fw_cfg, f, kernel_filename,
843 initrd_filename, kernel_cmdline, header))
844 return;
845 protocol = 0;
846 }
847
848 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
849 /* Low kernel */
850 real_addr = 0x90000;
851 cmdline_addr = 0x9a000 - cmdline_size;
852 prot_addr = 0x10000;
853 } else if (protocol < 0x202) {
854 /* High but ancient kernel */
855 real_addr = 0x90000;
856 cmdline_addr = 0x9a000 - cmdline_size;
857 prot_addr = 0x100000;
858 } else {
859 /* High and recent kernel */
860 real_addr = 0x10000;
861 cmdline_addr = 0x20000;
862 prot_addr = 0x100000;
863 }
864
865 #if 0
866 fprintf(stderr,
867 "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
868 "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
869 "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
870 real_addr,
871 cmdline_addr,
872 prot_addr);
873 #endif
874
875 /* highest address for loading the initrd */
876 if (protocol >= 0x203)
877 initrd_max = ldl_p(header+0x22c);
878 else
879 initrd_max = 0x37ffffff;
880
881 if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
882 initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
883
884 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
885 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
886 fw_cfg_add_bytes(fw_cfg, FW_CFG_CMDLINE_DATA,
887 (uint8_t*)strdup(kernel_cmdline),
888 strlen(kernel_cmdline)+1);
889
890 if (protocol >= 0x202) {
891 stl_p(header+0x228, cmdline_addr);
892 } else {
893 stw_p(header+0x20, 0xA33F);
894 stw_p(header+0x22, cmdline_addr-real_addr);
895 }
896
897 /* handle vga= parameter */
898 vmode = strstr(kernel_cmdline, "vga=");
899 if (vmode) {
900 unsigned int video_mode;
901 /* skip "vga=" */
902 vmode += 4;
903 if (!strncmp(vmode, "normal", 6)) {
904 video_mode = 0xffff;
905 } else if (!strncmp(vmode, "ext", 3)) {
906 video_mode = 0xfffe;
907 } else if (!strncmp(vmode, "ask", 3)) {
908 video_mode = 0xfffd;
909 } else {
910 video_mode = strtol(vmode, NULL, 0);
911 }
912 stw_p(header+0x1fa, video_mode);
913 }
914
915 /* loader type */
916 /* High nybble = B reserved for Qemu; low nybble is revision number.
917 If this code is substantially changed, you may want to consider
918 incrementing the revision. */
919 if (protocol >= 0x200)
920 header[0x210] = 0xB0;
921
922 /* heap */
923 if (protocol >= 0x201) {
924 header[0x211] |= 0x80; /* CAN_USE_HEAP */
925 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
926 }
927
928 /* load initrd */
929 if (initrd_filename) {
930 if (protocol < 0x200) {
931 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
932 exit(1);
933 }
934
935 initrd_size = get_image_size(initrd_filename);
936 initrd_addr = (initrd_max-initrd_size) & ~4095;
937
938 initrd_data = qemu_malloc(initrd_size);
939 load_image(initrd_filename, initrd_data);
940
941 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
942 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
943 fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
944
945 stl_p(header+0x218, initrd_addr);
946 stl_p(header+0x21c, initrd_size);
947 }
948
949 /* load kernel and setup */
950 setup_size = header[0x1f1];
951 if (setup_size == 0)
952 setup_size = 4;
953 setup_size = (setup_size+1)*512;
954 kernel_size -= setup_size;
955
956 setup = qemu_malloc(setup_size);
957 kernel = qemu_malloc(kernel_size);
958 fseek(f, 0, SEEK_SET);
959 if (fread(setup, 1, setup_size, f) != setup_size) {
960 fprintf(stderr, "fread() failed\n");
961 exit(1);
962 }
963 if (fread(kernel, 1, kernel_size, f) != kernel_size) {
964 fprintf(stderr, "fread() failed\n");
965 exit(1);
966 }
967 fclose(f);
968 memcpy(setup, header, MIN(sizeof(header), setup_size));
969
970 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
971 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
972 fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
973
974 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
975 fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
976 fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
977
978 option_rom[nb_option_roms] = "linuxboot.bin";
979 nb_option_roms++;
980 }
981
982 static const int ide_iobase[2] = { 0x1f0, 0x170 };
983 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
984 static const int ide_irq[2] = { 14, 15 };
985
986 #define NE2000_NB_MAX 6
987
988 static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
989 0x280, 0x380 };
990 static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
991
992 static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
993 static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
994
995 #ifdef HAS_AUDIO
996 static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
997 {
998 struct soundhw *c;
999
1000 for (c = soundhw; c->name; ++c) {
1001 if (c->enabled) {
1002 if (c->isa) {
1003 c->init.init_isa(pic);
1004 } else {
1005 if (pci_bus) {
1006 c->init.init_pci(pci_bus);
1007 }
1008 }
1009 }
1010 }
1011 }
1012 #endif
1013
1014 static void pc_init_ne2k_isa(NICInfo *nd)
1015 {
1016 static int nb_ne2k = 0;
1017
1018 if (nb_ne2k == NE2000_NB_MAX)
1019 return;
1020 isa_ne2000_init(ne2000_io[nb_ne2k],
1021 ne2000_irq[nb_ne2k], nd);
1022 nb_ne2k++;
1023 }
1024
1025 int cpu_is_bsp(CPUState *env)
1026 {
1027 return env->cpuid_apic_id == 0;
1028 }
1029
1030 static CPUState *pc_new_cpu(const char *cpu_model)
1031 {
1032 CPUState *env;
1033
1034 env = cpu_init(cpu_model);
1035 if (!env) {
1036 fprintf(stderr, "Unable to find x86 CPU definition\n");
1037 exit(1);
1038 }
1039 if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) {
1040 env->cpuid_apic_id = env->cpu_index;
1041 /* APIC reset callback resets cpu */
1042 apic_init(env);
1043 } else {
1044 qemu_register_reset((QEMUResetHandler*)cpu_reset, env);
1045 }
1046 return env;
1047 }
1048
1049 /* PC hardware initialisation */
1050 static void pc_init1(ram_addr_t ram_size,
1051 const char *boot_device,
1052 const char *kernel_filename,
1053 const char *kernel_cmdline,
1054 const char *initrd_filename,
1055 const char *cpu_model,
1056 int pci_enabled)
1057 {
1058 char *filename;
1059 int ret, linux_boot, i;
1060 ram_addr_t ram_addr, bios_offset, option_rom_offset;
1061 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
1062 int bios_size, isa_bios_size;
1063 PCIBus *pci_bus;
1064 ISADevice *isa_dev;
1065 int piix3_devfn = -1;
1066 CPUState *env;
1067 qemu_irq *cpu_irq;
1068 qemu_irq *isa_irq;
1069 qemu_irq *i8259;
1070 IsaIrqState *isa_irq_state;
1071 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
1072 DriveInfo *fd[MAX_FD];
1073 void *fw_cfg;
1074
1075 if (ram_size >= 0xe0000000 ) {
1076 above_4g_mem_size = ram_size - 0xe0000000;
1077 below_4g_mem_size = 0xe0000000;
1078 } else {
1079 below_4g_mem_size = ram_size;
1080 }
1081
1082 linux_boot = (kernel_filename != NULL);
1083
1084 /* init CPUs */
1085 if (cpu_model == NULL) {
1086 #ifdef TARGET_X86_64
1087 cpu_model = "qemu64";
1088 #else
1089 cpu_model = "qemu32";
1090 #endif
1091 }
1092
1093 for (i = 0; i < smp_cpus; i++) {
1094 env = pc_new_cpu(cpu_model);
1095 }
1096
1097 vmport_init();
1098
1099 /* allocate RAM */
1100 ram_addr = qemu_ram_alloc(0xa0000);
1101 cpu_register_physical_memory(0, 0xa0000, ram_addr);
1102
1103 /* Allocate, even though we won't register, so we don't break the
1104 * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
1105 * and some bios areas, which will be registered later
1106 */
1107 ram_addr = qemu_ram_alloc(0x100000 - 0xa0000);
1108 ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000);
1109 cpu_register_physical_memory(0x100000,
1110 below_4g_mem_size - 0x100000,
1111 ram_addr);
1112
1113 /* above 4giga memory allocation */
1114 if (above_4g_mem_size > 0) {
1115 #if TARGET_PHYS_ADDR_BITS == 32
1116 hw_error("To much RAM for 32-bit physical address");
1117 #else
1118 ram_addr = qemu_ram_alloc(above_4g_mem_size);
1119 cpu_register_physical_memory(0x100000000ULL,
1120 above_4g_mem_size,
1121 ram_addr);
1122 #endif
1123 }
1124
1125
1126 /* BIOS load */
1127 if (bios_name == NULL)
1128 bios_name = BIOS_FILENAME;
1129 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1130 if (filename) {
1131 bios_size = get_image_size(filename);
1132 } else {
1133 bios_size = -1;
1134 }
1135 if (bios_size <= 0 ||
1136 (bios_size % 65536) != 0) {
1137 goto bios_error;
1138 }
1139 bios_offset = qemu_ram_alloc(bios_size);
1140 ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size));
1141 if (ret != 0) {
1142 bios_error:
1143 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1144 exit(1);
1145 }
1146 if (filename) {
1147 qemu_free(filename);
1148 }
1149 /* map the last 128KB of the BIOS in ISA space */
1150 isa_bios_size = bios_size;
1151 if (isa_bios_size > (128 * 1024))
1152 isa_bios_size = 128 * 1024;
1153 cpu_register_physical_memory(0x100000 - isa_bios_size,
1154 isa_bios_size,
1155 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
1156
1157
1158
1159 rom_enable_driver_roms = 1;
1160 option_rom_offset = qemu_ram_alloc(PC_ROM_SIZE);
1161 cpu_register_physical_memory(PC_ROM_MIN_VGA, PC_ROM_SIZE, option_rom_offset);
1162
1163 /* map all the bios at the top of memory */
1164 cpu_register_physical_memory((uint32_t)(-bios_size),
1165 bios_size, bios_offset | IO_MEM_ROM);
1166
1167 fw_cfg = bochs_bios_init();
1168
1169 if (linux_boot) {
1170 load_linux(fw_cfg, kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
1171 }
1172
1173 for (i = 0; i < nb_option_roms; i++) {
1174 rom_add_option(option_rom[i]);
1175 }
1176
1177 cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
1178 i8259 = i8259_init(cpu_irq[0]);
1179 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
1180 isa_irq_state->i8259 = i8259;
1181 isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 24);
1182
1183 if (pci_enabled) {
1184 pci_bus = i440fx_init(&i440fx_state, &piix3_devfn, isa_irq);
1185 } else {
1186 pci_bus = NULL;
1187 isa_bus_new(NULL);
1188 }
1189 isa_bus_irqs(isa_irq);
1190
1191 ferr_irq = isa_reserve_irq(13);
1192
1193 /* init basic PC hardware */
1194 register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
1195
1196 register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
1197
1198 if (cirrus_vga_enabled) {
1199 if (pci_enabled) {
1200 pci_cirrus_vga_init(pci_bus);
1201 } else {
1202 isa_cirrus_vga_init();
1203 }
1204 } else if (vmsvga_enabled) {
1205 if (pci_enabled)
1206 pci_vmsvga_init(pci_bus);
1207 else
1208 fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
1209 } else if (std_vga_enabled) {
1210 if (pci_enabled) {
1211 pci_vga_init(pci_bus, 0, 0);
1212 } else {
1213 isa_vga_init();
1214 }
1215 }
1216
1217 rtc_state = rtc_init(2000);
1218
1219 qemu_register_boot_set(pc_boot_set, rtc_state);
1220
1221 register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
1222 register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
1223
1224 if (pci_enabled) {
1225 isa_irq_state->ioapic = ioapic_init();
1226 }
1227 pit = pit_init(0x40, isa_reserve_irq(0));
1228 pcspk_init(pit);
1229 if (!no_hpet) {
1230 hpet_init(isa_irq);
1231 }
1232
1233 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
1234 if (serial_hds[i]) {
1235 serial_isa_init(i, serial_hds[i]);
1236 }
1237 }
1238
1239 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
1240 if (parallel_hds[i]) {
1241 parallel_init(i, parallel_hds[i]);
1242 }
1243 }
1244
1245 for(i = 0; i < nb_nics; i++) {
1246 NICInfo *nd = &nd_table[i];
1247
1248 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
1249 pc_init_ne2k_isa(nd);
1250 else
1251 pci_nic_init_nofail(nd, "e1000", NULL);
1252 }
1253
1254 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
1255 fprintf(stderr, "qemu: too many IDE bus\n");
1256 exit(1);
1257 }
1258
1259 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
1260 hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
1261 }
1262
1263 if (pci_enabled) {
1264 pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1);
1265 } else {
1266 for(i = 0; i < MAX_IDE_BUS; i++) {
1267 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
1268 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
1269 }
1270 }
1271
1272 isa_dev = isa_create_simple("i8042");
1273 DMA_init(0);
1274 #ifdef HAS_AUDIO
1275 audio_init(pci_enabled ? pci_bus : NULL, isa_irq);
1276 #endif
1277
1278 for(i = 0; i < MAX_FD; i++) {
1279 fd[i] = drive_get(IF_FLOPPY, 0, i);
1280 }
1281 floppy_controller = fdctrl_init_isa(fd);
1282
1283 cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
1284
1285 if (pci_enabled && usb_enabled) {
1286 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
1287 }
1288
1289 if (pci_enabled && acpi_enabled) {
1290 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
1291 i2c_bus *smbus;
1292
1293 /* TODO: Populate SPD eeprom data. */
1294 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100,
1295 isa_reserve_irq(9));
1296 for (i = 0; i < 8; i++) {
1297 DeviceState *eeprom;
1298 eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
1299 qdev_prop_set_uint8(eeprom, "address", 0x50 + i);
1300 qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
1301 qdev_init_nofail(eeprom);
1302 }
1303 piix4_acpi_system_hot_add_init(pci_bus);
1304 }
1305
1306 if (i440fx_state) {
1307 i440fx_init_memory_mappings(i440fx_state);
1308 }
1309
1310 if (pci_enabled) {
1311 int max_bus;
1312 int bus;
1313
1314 max_bus = drive_get_max_bus(IF_SCSI);
1315 for (bus = 0; bus <= max_bus; bus++) {
1316 pci_create_simple(pci_bus, -1, "lsi53c895a");
1317 }
1318 }
1319
1320 /* Add virtio console devices */
1321 if (pci_enabled) {
1322 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
1323 if (virtcon_hds[i]) {
1324 pci_create_simple(pci_bus, -1, "virtio-console-pci");
1325 }
1326 }
1327 }
1328
1329 rom_load_fw(fw_cfg);
1330 }
1331
1332 static void pc_init_pci(ram_addr_t ram_size,
1333 const char *boot_device,
1334 const char *kernel_filename,
1335 const char *kernel_cmdline,
1336 const char *initrd_filename,
1337 const char *cpu_model)
1338 {
1339 pc_init1(ram_size, boot_device,
1340 kernel_filename, kernel_cmdline,
1341 initrd_filename, cpu_model, 1);
1342 }
1343
1344 static void pc_init_isa(ram_addr_t ram_size,
1345 const char *boot_device,
1346 const char *kernel_filename,
1347 const char *kernel_cmdline,
1348 const char *initrd_filename,
1349 const char *cpu_model)
1350 {
1351 if (cpu_model == NULL)
1352 cpu_model = "486";
1353 pc_init1(ram_size, boot_device,
1354 kernel_filename, kernel_cmdline,
1355 initrd_filename, cpu_model, 0);
1356 }
1357
1358 /* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
1359 BIOS will read it and start S3 resume at POST Entry */
1360 void cmos_set_s3_resume(void)
1361 {
1362 if (rtc_state)
1363 rtc_set_memory(rtc_state, 0xF, 0xFE);
1364 }
1365
1366 static QEMUMachine pc_machine = {
1367 .name = "pc-0.11",
1368 .alias = "pc",
1369 .desc = "Standard PC",
1370 .init = pc_init_pci,
1371 .max_cpus = 255,
1372 .is_default = 1,
1373 };
1374
1375 static QEMUMachine pc_machine_v0_10 = {
1376 .name = "pc-0.10",
1377 .desc = "Standard PC, qemu 0.10",
1378 .init = pc_init_pci,
1379 .max_cpus = 255,
1380 .compat_props = (GlobalProperty[]) {
1381 {
1382 .driver = "virtio-blk-pci",
1383 .property = "class",
1384 .value = stringify(PCI_CLASS_STORAGE_OTHER),
1385 },{
1386 .driver = "virtio-console-pci",
1387 .property = "class",
1388 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
1389 },{
1390 .driver = "virtio-net-pci",
1391 .property = "vectors",
1392 .value = stringify(0),
1393 },{
1394 .driver = "virtio-blk-pci",
1395 .property = "vectors",
1396 .value = stringify(0),
1397 },
1398 { /* end of list */ }
1399 },
1400 };
1401
1402 static QEMUMachine isapc_machine = {
1403 .name = "isapc",
1404 .desc = "ISA-only PC",
1405 .init = pc_init_isa,
1406 .max_cpus = 1,
1407 };
1408
1409 static void pc_machine_init(void)
1410 {
1411 qemu_register_machine(&pc_machine);
1412 qemu_register_machine(&pc_machine_v0_10);
1413 qemu_register_machine(&isapc_machine);
1414 }
1415
1416 machine_init(pc_machine_init);