Large kernel initrd fix (initial patch by Daniel Jacobowitz).
[qemu] / 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 "vl.h"
25
26 /* output Bochs bios info messages */
27 //#define DEBUG_BIOS
28
29 #define BIOS_FILENAME "bios.bin"
30 #define VGABIOS_FILENAME "vgabios.bin"
31 #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
32 #define LINUX_BOOT_FILENAME "linux_boot.bin"
33
34 #define KERNEL_LOAD_ADDR     0x00100000
35 #define MAX_INITRD_LOAD_ADDR 0x38000000
36 #define KERNEL_PARAMS_ADDR   0x00090000
37 #define KERNEL_CMDLINE_ADDR  0x00099000
38 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
39 #define ACPI_DATA_SIZE       0x10000
40
41 static fdctrl_t *floppy_controller;
42 static RTCState *rtc_state;
43 static PITState *pit;
44 static IOAPICState *ioapic;
45 static PCIDevice *i440fx_state;
46
47 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
48 {
49 }
50
51 /* MSDOS compatibility mode FPU exception support */
52 /* XXX: add IGNNE support */
53 void cpu_set_ferr(CPUX86State *s)
54 {
55     pic_set_irq(13, 1);
56 }
57
58 static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
59 {
60     pic_set_irq(13, 0);
61 }
62
63 /* TSC handling */
64 uint64_t cpu_get_tsc(CPUX86State *env)
65 {
66     /* Note: when using kqemu, it is more logical to return the host TSC
67        because kqemu does not trap the RDTSC instruction for
68        performance reasons */
69 #if USE_KQEMU
70     if (env->kqemu_enabled) {
71         return cpu_get_real_ticks();
72     } else 
73 #endif
74     {
75         return cpu_get_ticks();
76     }
77 }
78
79 /* SMM support */
80 void cpu_smm_update(CPUState *env)
81 {
82     if (i440fx_state && env == first_cpu)
83         i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
84 }
85
86
87 /* IRQ handling */
88 int cpu_get_pic_interrupt(CPUState *env)
89 {
90     int intno;
91
92     intno = apic_get_interrupt(env);
93     if (intno >= 0) {
94         /* set irq request if a PIC irq is still pending */
95         /* XXX: improve that */
96         pic_update_irq(isa_pic); 
97         return intno;
98     }
99     /* read the irq from the PIC */
100     intno = pic_read_irq(isa_pic);
101     return intno;
102 }
103
104 static void pic_irq_request(void *opaque, int level)
105 {
106     CPUState *env = opaque;
107     if (level)
108         cpu_interrupt(env, CPU_INTERRUPT_HARD);
109     else
110         cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
111 }
112
113 /* PC cmos mappings */
114
115 #define REG_EQUIPMENT_BYTE          0x14
116
117 static int cmos_get_fd_drive_type(int fd0)
118 {
119     int val;
120
121     switch (fd0) {
122     case 0:
123         /* 1.44 Mb 3"5 drive */
124         val = 4;
125         break;
126     case 1:
127         /* 2.88 Mb 3"5 drive */
128         val = 5;
129         break;
130     case 2:
131         /* 1.2 Mb 5"5 drive */
132         val = 2;
133         break;
134     default:
135         val = 0;
136         break;
137     }
138     return val;
139 }
140
141 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd) 
142 {
143     RTCState *s = rtc_state;
144     int cylinders, heads, sectors;
145     bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
146     rtc_set_memory(s, type_ofs, 47);
147     rtc_set_memory(s, info_ofs, cylinders);
148     rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
149     rtc_set_memory(s, info_ofs + 2, heads);
150     rtc_set_memory(s, info_ofs + 3, 0xff);
151     rtc_set_memory(s, info_ofs + 4, 0xff);
152     rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
153     rtc_set_memory(s, info_ofs + 6, cylinders);
154     rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
155     rtc_set_memory(s, info_ofs + 8, sectors);
156 }
157
158 /* hd_table must contain 4 block drivers */
159 static void cmos_init(int ram_size, int boot_device, BlockDriverState **hd_table)
160 {
161     RTCState *s = rtc_state;
162     int val;
163     int fd0, fd1, nb;
164     int i;
165
166     /* various important CMOS locations needed by PC/Bochs bios */
167
168     /* memory size */
169     val = 640; /* base memory in K */
170     rtc_set_memory(s, 0x15, val);
171     rtc_set_memory(s, 0x16, val >> 8);
172
173     val = (ram_size / 1024) - 1024;
174     if (val > 65535)
175         val = 65535;
176     rtc_set_memory(s, 0x17, val);
177     rtc_set_memory(s, 0x18, val >> 8);
178     rtc_set_memory(s, 0x30, val);
179     rtc_set_memory(s, 0x31, val >> 8);
180
181     if (ram_size > (16 * 1024 * 1024))
182         val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
183     else
184         val = 0;
185     if (val > 65535)
186         val = 65535;
187     rtc_set_memory(s, 0x34, val);
188     rtc_set_memory(s, 0x35, val >> 8);
189     
190     switch(boot_device) {
191     case 'a':
192     case 'b':
193         rtc_set_memory(s, 0x3d, 0x01); /* floppy boot */
194         if (!fd_bootchk)
195             rtc_set_memory(s, 0x38, 0x01); /* disable signature check */
196         break;
197     default:
198     case 'c':
199         rtc_set_memory(s, 0x3d, 0x02); /* hard drive boot */
200         break;
201     case 'd':
202         rtc_set_memory(s, 0x3d, 0x03); /* CD-ROM boot */
203         break;
204     }
205
206     /* floppy type */
207
208     fd0 = fdctrl_get_drive_type(floppy_controller, 0);
209     fd1 = fdctrl_get_drive_type(floppy_controller, 1);
210
211     val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
212     rtc_set_memory(s, 0x10, val);
213     
214     val = 0;
215     nb = 0;
216     if (fd0 < 3)
217         nb++;
218     if (fd1 < 3)
219         nb++;
220     switch (nb) {
221     case 0:
222         break;
223     case 1:
224         val |= 0x01; /* 1 drive, ready for boot */
225         break;
226     case 2:
227         val |= 0x41; /* 2 drives, ready for boot */
228         break;
229     }
230     val |= 0x02; /* FPU is there */
231     val |= 0x04; /* PS/2 mouse installed */
232     rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
233
234     /* hard drives */
235
236     rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
237     if (hd_table[0])
238         cmos_init_hd(0x19, 0x1b, hd_table[0]);
239     if (hd_table[1]) 
240         cmos_init_hd(0x1a, 0x24, hd_table[1]);
241
242     val = 0;
243     for (i = 0; i < 4; i++) {
244         if (hd_table[i]) {
245             int cylinders, heads, sectors, translation;
246             /* NOTE: bdrv_get_geometry_hint() returns the physical
247                 geometry.  It is always such that: 1 <= sects <= 63, 1
248                 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
249                 geometry can be different if a translation is done. */
250             translation = bdrv_get_translation_hint(hd_table[i]);
251             if (translation == BIOS_ATA_TRANSLATION_AUTO) {
252                 bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
253                 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
254                     /* No translation. */
255                     translation = 0;
256                 } else {
257                     /* LBA translation. */
258                     translation = 1;
259                 }
260             } else {
261                 translation--;
262             }
263             val |= translation << (i * 2);
264         }
265     }
266     rtc_set_memory(s, 0x39, val);
267 }
268
269 void ioport_set_a20(int enable)
270 {
271     /* XXX: send to all CPUs ? */
272     cpu_x86_set_a20(first_cpu, enable);
273 }
274
275 int ioport_get_a20(void)
276 {
277     return ((first_cpu->a20_mask >> 20) & 1);
278 }
279
280 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
281 {
282     ioport_set_a20((val >> 1) & 1);
283     /* XXX: bit 0 is fast reset */
284 }
285
286 static uint32_t ioport92_read(void *opaque, uint32_t addr)
287 {
288     return ioport_get_a20() << 1;
289 }
290
291 /***********************************************************/
292 /* Bochs BIOS debug ports */
293
294 void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
295 {
296     static const char shutdown_str[8] = "Shutdown";
297     static int shutdown_index = 0;
298     
299     switch(addr) {
300         /* Bochs BIOS messages */
301     case 0x400:
302     case 0x401:
303         fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
304         exit(1);
305     case 0x402:
306     case 0x403:
307 #ifdef DEBUG_BIOS
308         fprintf(stderr, "%c", val);
309 #endif
310         break;
311     case 0x8900:
312         /* same as Bochs power off */
313         if (val == shutdown_str[shutdown_index]) {
314             shutdown_index++;
315             if (shutdown_index == 8) {
316                 shutdown_index = 0;
317                 qemu_system_shutdown_request();
318             }
319         } else {
320             shutdown_index = 0;
321         }
322         break;
323
324         /* LGPL'ed VGA BIOS messages */
325     case 0x501:
326     case 0x502:
327         fprintf(stderr, "VGA BIOS panic, line %d\n", val);
328         exit(1);
329     case 0x500:
330     case 0x503:
331 #ifdef DEBUG_BIOS
332         fprintf(stderr, "%c", val);
333 #endif
334         break;
335     }
336 }
337
338 void bochs_bios_init(void)
339 {
340     register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
341     register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
342     register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
343     register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
344     register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
345
346     register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
347     register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
348     register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
349     register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
350 }
351
352
353 int load_kernel(const char *filename, uint8_t *addr, 
354                 uint8_t *real_addr)
355 {
356     int fd, size;
357     int setup_sects;
358
359     fd = open(filename, O_RDONLY | O_BINARY);
360     if (fd < 0)
361         return -1;
362
363     /* load 16 bit code */
364     if (read(fd, real_addr, 512) != 512)
365         goto fail;
366     setup_sects = real_addr[0x1F1];
367     if (!setup_sects)
368         setup_sects = 4;
369     if (read(fd, real_addr + 512, setup_sects * 512) != 
370         setup_sects * 512)
371         goto fail;
372     
373     /* load 32 bit code */
374     size = read(fd, addr, 16 * 1024 * 1024);
375     if (size < 0)
376         goto fail;
377     close(fd);
378     return size;
379  fail:
380     close(fd);
381     return -1;
382 }
383
384 static void main_cpu_reset(void *opaque)
385 {
386     CPUState *env = opaque;
387     cpu_reset(env);
388 }
389
390 static const int ide_iobase[2] = { 0x1f0, 0x170 };
391 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
392 static const int ide_irq[2] = { 14, 15 };
393
394 #define NE2000_NB_MAX 6
395
396 static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
397 static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
398
399 static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
400 static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
401
402 static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
403 static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
404
405 #ifdef HAS_AUDIO
406 static void audio_init (PCIBus *pci_bus)
407 {
408     struct soundhw *c;
409     int audio_enabled = 0;
410
411     for (c = soundhw; !audio_enabled && c->name; ++c) {
412         audio_enabled = c->enabled;
413     }
414
415     if (audio_enabled) {
416         AudioState *s;
417
418         s = AUD_init ();
419         if (s) {
420             for (c = soundhw; c->name; ++c) {
421                 if (c->enabled) {
422                     if (c->isa) {
423                         c->init.init_isa (s);
424                     }
425                     else {
426                         if (pci_bus) {
427                             c->init.init_pci (pci_bus, s);
428                         }
429                     }
430                 }
431             }
432         }
433     }
434 }
435 #endif
436
437 static void pc_init_ne2k_isa(NICInfo *nd)
438 {
439     static int nb_ne2k = 0;
440
441     if (nb_ne2k == NE2000_NB_MAX)
442         return;
443     isa_ne2000_init(ne2000_io[nb_ne2k], ne2000_irq[nb_ne2k], nd);
444     nb_ne2k++;
445 }
446
447 /* PC hardware initialisation */
448 static void pc_init1(int ram_size, int vga_ram_size, int boot_device,
449                      DisplayState *ds, const char **fd_filename, int snapshot,
450                      const char *kernel_filename, const char *kernel_cmdline,
451                      const char *initrd_filename,
452                      int pci_enabled)
453 {
454     char buf[1024];
455     int ret, linux_boot, initrd_size, i;
456     ram_addr_t ram_addr, vga_ram_addr, bios_offset, vga_bios_offset;
457     ram_addr_t initrd_offset;
458     int bios_size, isa_bios_size, vga_bios_size;
459     PCIBus *pci_bus;
460     int piix3_devfn = -1;
461     CPUState *env;
462     NICInfo *nd;
463
464     linux_boot = (kernel_filename != NULL);
465
466     /* init CPUs */
467     for(i = 0; i < smp_cpus; i++) {
468         env = cpu_init();
469         if (i != 0)
470             env->hflags |= HF_HALTED_MASK;
471         if (smp_cpus > 1) {
472             /* XXX: enable it in all cases */
473             env->cpuid_features |= CPUID_APIC;
474         }
475         register_savevm("cpu", i, 4, cpu_save, cpu_load, env);
476         qemu_register_reset(main_cpu_reset, env);
477         if (pci_enabled) {
478             apic_init(env);
479         }
480     }
481
482     /* allocate RAM */
483     ram_addr = qemu_ram_alloc(ram_size);
484     cpu_register_physical_memory(0, ram_size, ram_addr);
485
486     /* allocate VGA RAM */
487     vga_ram_addr = qemu_ram_alloc(vga_ram_size);
488
489     /* BIOS load */
490     snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
491     bios_size = get_image_size(buf);
492     if (bios_size <= 0 || 
493         (bios_size % 65536) != 0) {
494         goto bios_error;
495     }
496     bios_offset = qemu_ram_alloc(bios_size);
497     ret = load_image(buf, phys_ram_base + bios_offset);
498     if (ret != bios_size) {
499     bios_error:
500         fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
501         exit(1);
502     }
503
504     /* VGA BIOS load */
505     if (cirrus_vga_enabled) {
506         snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
507     } else {
508         snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
509     }
510     vga_bios_size = get_image_size(buf);
511     if (vga_bios_size <= 0 || vga_bios_size > 65536) 
512         goto vga_bios_error;
513     vga_bios_offset = qemu_ram_alloc(65536);
514
515     ret = load_image(buf, phys_ram_base + vga_bios_offset);
516     if (ret != vga_bios_size) {
517     vga_bios_error:
518         fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf);
519         exit(1);
520     }
521
522     /* setup basic memory access */
523     cpu_register_physical_memory(0xc0000, 0x10000, 
524                                  vga_bios_offset | IO_MEM_ROM);
525
526     /* map the last 128KB of the BIOS in ISA space */
527     isa_bios_size = bios_size;
528     if (isa_bios_size > (128 * 1024))
529         isa_bios_size = 128 * 1024;
530     cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size, 
531                                  IO_MEM_UNASSIGNED);
532     cpu_register_physical_memory(0x100000 - isa_bios_size, 
533                                  isa_bios_size, 
534                                  (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
535
536     {
537         ram_addr_t option_rom_offset;
538         int size, offset;
539
540         offset = 0;
541         for (i = 0; i < nb_option_roms; i++) {
542             size = get_image_size(option_rom[i]);
543             if (size < 0) {
544                 fprintf(stderr, "Could not load option rom '%s'\n", 
545                         option_rom[i]);
546                 exit(1);
547             }
548             if (size > (0x10000 - offset))
549                 goto option_rom_error;
550             option_rom_offset = qemu_ram_alloc(size);
551             ret = load_image(option_rom[i], phys_ram_base + option_rom_offset);
552             if (ret != size) {
553             option_rom_error:
554                 fprintf(stderr, "Too many option ROMS\n");
555                 exit(1);
556             }
557             size = (size + 4095) & ~4095;
558             cpu_register_physical_memory(0xd0000 + offset,
559                                          size, option_rom_offset | IO_MEM_ROM);
560             offset += size;
561         }
562     }
563
564     /* map all the bios at the top of memory */
565     cpu_register_physical_memory((uint32_t)(-bios_size), 
566                                  bios_size, bios_offset | IO_MEM_ROM);
567     
568     bochs_bios_init();
569
570     if (linux_boot) {
571         uint8_t bootsect[512];
572         uint8_t old_bootsect[512];
573
574         if (bs_table[0] == NULL) {
575             fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n");
576             exit(1);
577         }
578         snprintf(buf, sizeof(buf), "%s/%s", bios_dir, LINUX_BOOT_FILENAME);
579         ret = load_image(buf, bootsect);
580         if (ret != sizeof(bootsect)) {
581             fprintf(stderr, "qemu: could not load linux boot sector '%s'\n",
582                     buf);
583             exit(1);
584         }
585
586         if (bdrv_read(bs_table[0], 0, old_bootsect, 1) >= 0) {
587             /* copy the MSDOS partition table */
588             memcpy(bootsect + 0x1be, old_bootsect + 0x1be, 0x40);
589         }
590
591         bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect));
592
593         /* now we can load the kernel */
594         ret = load_kernel(kernel_filename, 
595                           phys_ram_base + KERNEL_LOAD_ADDR,
596                           phys_ram_base + KERNEL_PARAMS_ADDR);
597         if (ret < 0) {
598             fprintf(stderr, "qemu: could not load kernel '%s'\n", 
599                     kernel_filename);
600             exit(1);
601         }
602         
603         /* load initrd */
604         initrd_size = 0;
605         initrd_offset = 0;
606         if (initrd_filename) {
607             initrd_size = get_image_size (initrd_filename);
608             if (initrd_size > 0) {
609                 initrd_offset = (ram_size - initrd_size) & TARGET_PAGE_MASK;
610                 /* Leave space for BIOS ACPI tables.  */
611                 initrd_offset -= ACPI_DATA_SIZE;
612                 /* Avoid the last 64k to avoid 2.2.x kernel bugs.  */
613                 initrd_offset -= 0x10000;
614                 if (initrd_offset > MAX_INITRD_LOAD_ADDR)
615                     initrd_offset = MAX_INITRD_LOAD_ADDR;
616
617                 if (initrd_size > ram_size
618                     || initrd_offset < KERNEL_LOAD_ADDR + ret) {
619                     fprintf(stderr,
620                             "qemu: memory too small for initial ram disk '%s'\n",
621                             initrd_filename);
622                     exit(1);
623                 }
624                 initrd_size = load_image(initrd_filename,
625                                          phys_ram_base + initrd_offset);
626             }
627             if (initrd_size < 0) {
628                 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 
629                         initrd_filename);
630                 exit(1);
631             }
632         }
633         if (initrd_size > 0) {
634             stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x218, initrd_offset);
635             stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x21c, initrd_size);
636         }
637         pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096,
638                 kernel_cmdline);
639         stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x20, 0xA33F);
640         stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x22,
641                 KERNEL_CMDLINE_ADDR - KERNEL_PARAMS_ADDR);
642         /* loader type */
643         stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01);
644     }
645
646     if (pci_enabled) {
647         pci_bus = i440fx_init(&i440fx_state);
648         piix3_devfn = piix3_init(pci_bus, -1);
649     } else {
650         pci_bus = NULL;
651     }
652
653     /* init basic PC hardware */
654     register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
655
656     register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
657
658     if (cirrus_vga_enabled) {
659         if (pci_enabled) {
660             pci_cirrus_vga_init(pci_bus, 
661                                 ds, phys_ram_base + vga_ram_addr, 
662                                 vga_ram_addr, vga_ram_size);
663         } else {
664             isa_cirrus_vga_init(ds, phys_ram_base + vga_ram_addr, 
665                                 vga_ram_addr, vga_ram_size);
666         }
667     } else {
668         if (pci_enabled) {
669             pci_vga_init(pci_bus, ds, phys_ram_base + vga_ram_addr, 
670                          vga_ram_addr, vga_ram_size, 0, 0);
671         } else {
672             isa_vga_init(ds, phys_ram_base + vga_ram_addr, 
673                          vga_ram_addr, vga_ram_size);
674         }
675     }
676
677     rtc_state = rtc_init(0x70, 8);
678
679     register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
680     register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
681
682     if (pci_enabled) {
683         ioapic = ioapic_init();
684     }
685     isa_pic = pic_init(pic_irq_request, first_cpu);
686     pit = pit_init(0x40, 0);
687     pcspk_init(pit);
688     if (pci_enabled) {
689         pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
690     }
691
692     for(i = 0; i < MAX_SERIAL_PORTS; i++) {
693         if (serial_hds[i]) {
694             serial_init(&pic_set_irq_new, isa_pic,
695                         serial_io[i], serial_irq[i], serial_hds[i]);
696         }
697     }
698
699     for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
700         if (parallel_hds[i]) {
701             parallel_init(parallel_io[i], parallel_irq[i], parallel_hds[i]);
702         }
703     }
704
705     for(i = 0; i < nb_nics; i++) {
706         nd = &nd_table[i];
707         if (!nd->model) {
708             if (pci_enabled) {
709                 nd->model = "ne2k_pci";
710             } else {
711                 nd->model = "ne2k_isa";
712             }
713         }
714         if (strcmp(nd->model, "ne2k_isa") == 0) {
715             pc_init_ne2k_isa(nd);
716         } else if (pci_enabled) {
717             pci_nic_init(pci_bus, nd, -1);
718         } else {
719             fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
720             exit(1);
721         }
722     }
723
724     if (pci_enabled) {
725         pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1);
726     } else {
727         for(i = 0; i < 2; i++) {
728             isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
729                          bs_table[2 * i], bs_table[2 * i + 1]);
730         }
731     }
732
733     kbd_init();
734     DMA_init(0);
735 #ifdef HAS_AUDIO
736     audio_init(pci_enabled ? pci_bus : NULL);
737 #endif
738
739     floppy_controller = fdctrl_init(6, 2, 0, 0x3f0, fd_table);
740
741     cmos_init(ram_size, boot_device, bs_table);
742
743     if (pci_enabled && usb_enabled) {
744         usb_uhci_init(pci_bus, piix3_devfn + 2);
745     }
746
747     if (pci_enabled && acpi_enabled) {
748         uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
749         piix4_pm_init(pci_bus, piix3_devfn + 3);
750         for (i = 0; i < 8; i++) {
751             SMBusDevice *eeprom = smbus_eeprom_device_init(0x50 + i,
752                 eeprom_buf + (i * 256));
753             piix4_smbus_register_device(eeprom, 0x50 + i);
754         }
755     }
756     
757     if (i440fx_state) {
758         i440fx_init_memory_mappings(i440fx_state);
759     }
760 #if 0
761     /* ??? Need to figure out some way for the user to
762        specify SCSI devices.  */
763     if (pci_enabled) {
764         void *scsi;
765         BlockDriverState *bdrv;
766
767         scsi = lsi_scsi_init(pci_bus, -1);
768         bdrv = bdrv_new("scsidisk");
769         bdrv_open(bdrv, "scsi_disk.img", 0);
770         lsi_scsi_attach(scsi, bdrv, -1);
771         bdrv = bdrv_new("scsicd");
772         bdrv_open(bdrv, "scsi_cd.iso", 0);
773         bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
774         lsi_scsi_attach(scsi, bdrv, -1);
775     }
776 #endif
777 }
778
779 static void pc_init_pci(int ram_size, int vga_ram_size, int boot_device,
780                         DisplayState *ds, const char **fd_filename, 
781                         int snapshot, 
782                         const char *kernel_filename, 
783                         const char *kernel_cmdline,
784                         const char *initrd_filename,
785                         const char *cpu_model)
786 {
787     pc_init1(ram_size, vga_ram_size, boot_device,
788              ds, fd_filename, snapshot,
789              kernel_filename, kernel_cmdline,
790              initrd_filename, 1);
791 }
792
793 static void pc_init_isa(int ram_size, int vga_ram_size, int boot_device,
794                         DisplayState *ds, const char **fd_filename, 
795                         int snapshot, 
796                         const char *kernel_filename, 
797                         const char *kernel_cmdline,
798                         const char *initrd_filename,
799                         const char *cpu_model)
800 {
801     pc_init1(ram_size, vga_ram_size, boot_device,
802              ds, fd_filename, snapshot,
803              kernel_filename, kernel_cmdline,
804              initrd_filename, 0);
805 }
806
807 QEMUMachine pc_machine = {
808     "pc",
809     "Standard PC",
810     pc_init_pci,
811 };
812
813 QEMUMachine isapc_machine = {
814     "isapc",
815     "ISA-only PC",
816     pc_init_isa,
817 };