Use correct types to enable > 2G support, based on a patch from
[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 "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
35 /* output Bochs bios info messages */
36 //#define DEBUG_BIOS
37
38 #define BIOS_FILENAME "bios.bin"
39 #define VGABIOS_FILENAME "vgabios.bin"
40 #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
41
42 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
43 #define ACPI_DATA_SIZE       0x10000
44
45 #define MAX_IDE_BUS 2
46
47 static fdctrl_t *floppy_controller;
48 static RTCState *rtc_state;
49 static PITState *pit;
50 static IOAPICState *ioapic;
51 static PCIDevice *i440fx_state;
52
53 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
54 {
55 }
56
57 /* MSDOS compatibility mode FPU exception support */
58 static qemu_irq ferr_irq;
59 /* XXX: add IGNNE support */
60 void cpu_set_ferr(CPUX86State *s)
61 {
62     qemu_irq_raise(ferr_irq);
63 }
64
65 static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
66 {
67     qemu_irq_lower(ferr_irq);
68 }
69
70 /* TSC handling */
71 uint64_t cpu_get_tsc(CPUX86State *env)
72 {
73     /* Note: when using kqemu, it is more logical to return the host TSC
74        because kqemu does not trap the RDTSC instruction for
75        performance reasons */
76 #if USE_KQEMU
77     if (env->kqemu_enabled) {
78         return cpu_get_real_ticks();
79     } else
80 #endif
81     {
82         return cpu_get_ticks();
83     }
84 }
85
86 /* SMM support */
87 void cpu_smm_update(CPUState *env)
88 {
89     if (i440fx_state && env == first_cpu)
90         i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
91 }
92
93
94 /* IRQ handling */
95 int cpu_get_pic_interrupt(CPUState *env)
96 {
97     int intno;
98
99     intno = apic_get_interrupt(env);
100     if (intno >= 0) {
101         /* set irq request if a PIC irq is still pending */
102         /* XXX: improve that */
103         pic_update_irq(isa_pic);
104         return intno;
105     }
106     /* read the irq from the PIC */
107     if (!apic_accept_pic_intr(env))
108         return -1;
109
110     intno = pic_read_irq(isa_pic);
111     return intno;
112 }
113
114 static void pic_irq_request(void *opaque, int irq, int level)
115 {
116     CPUState *env = first_cpu;
117
118     if (!level)
119         return;
120
121     while (env) {
122         if (apic_accept_pic_intr(env))
123             apic_local_deliver(env, APIC_LINT0);
124         env = env->next_cpu;
125     }
126 }
127
128 /* PC cmos mappings */
129
130 #define REG_EQUIPMENT_BYTE          0x14
131
132 static int cmos_get_fd_drive_type(int fd0)
133 {
134     int val;
135
136     switch (fd0) {
137     case 0:
138         /* 1.44 Mb 3"5 drive */
139         val = 4;
140         break;
141     case 1:
142         /* 2.88 Mb 3"5 drive */
143         val = 5;
144         break;
145     case 2:
146         /* 1.2 Mb 5"5 drive */
147         val = 2;
148         break;
149     default:
150         val = 0;
151         break;
152     }
153     return val;
154 }
155
156 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
157 {
158     RTCState *s = rtc_state;
159     int cylinders, heads, sectors;
160     bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
161     rtc_set_memory(s, type_ofs, 47);
162     rtc_set_memory(s, info_ofs, cylinders);
163     rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
164     rtc_set_memory(s, info_ofs + 2, heads);
165     rtc_set_memory(s, info_ofs + 3, 0xff);
166     rtc_set_memory(s, info_ofs + 4, 0xff);
167     rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
168     rtc_set_memory(s, info_ofs + 6, cylinders);
169     rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
170     rtc_set_memory(s, info_ofs + 8, sectors);
171 }
172
173 /* convert boot_device letter to something recognizable by the bios */
174 static int boot_device2nibble(char boot_device)
175 {
176     switch(boot_device) {
177     case 'a':
178     case 'b':
179         return 0x01; /* floppy boot */
180     case 'c':
181         return 0x02; /* hard drive boot */
182     case 'd':
183         return 0x03; /* CD-ROM boot */
184     case 'n':
185         return 0x04; /* Network boot */
186     }
187     return 0;
188 }
189
190 /* hd_table must contain 4 block drivers */
191 static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
192                       const char *boot_device, BlockDriverState **hd_table)
193 {
194     RTCState *s = rtc_state;
195     int nbds, bds[3] = { 0, };
196     int val;
197     int fd0, fd1, nb;
198     int i;
199
200     /* various important CMOS locations needed by PC/Bochs bios */
201
202     /* memory size */
203     val = 640; /* base memory in K */
204     rtc_set_memory(s, 0x15, val);
205     rtc_set_memory(s, 0x16, val >> 8);
206
207     val = (ram_size / 1024) - 1024;
208     if (val > 65535)
209         val = 65535;
210     rtc_set_memory(s, 0x17, val);
211     rtc_set_memory(s, 0x18, val >> 8);
212     rtc_set_memory(s, 0x30, val);
213     rtc_set_memory(s, 0x31, val >> 8);
214
215     if (above_4g_mem_size) {
216         rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
217         rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
218         rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
219     }
220
221     if (ram_size > (16 * 1024 * 1024))
222         val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
223     else
224         val = 0;
225     if (val > 65535)
226         val = 65535;
227     rtc_set_memory(s, 0x34, val);
228     rtc_set_memory(s, 0x35, val >> 8);
229
230     /* set the number of CPU */
231     rtc_set_memory(s, 0x5f, smp_cpus - 1);
232
233     /* set boot devices, and disable floppy signature check if requested */
234 #define PC_MAX_BOOT_DEVICES 3
235     nbds = strlen(boot_device);
236     if (nbds > PC_MAX_BOOT_DEVICES) {
237         fprintf(stderr, "Too many boot devices for PC\n");
238         exit(1);
239     }
240     for (i = 0; i < nbds; i++) {
241         bds[i] = boot_device2nibble(boot_device[i]);
242         if (bds[i] == 0) {
243             fprintf(stderr, "Invalid boot device for PC: '%c'\n",
244                     boot_device[i]);
245             exit(1);
246         }
247     }
248     rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
249     rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ?  0x0 : 0x1));
250
251     /* floppy type */
252
253     fd0 = fdctrl_get_drive_type(floppy_controller, 0);
254     fd1 = fdctrl_get_drive_type(floppy_controller, 1);
255
256     val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
257     rtc_set_memory(s, 0x10, val);
258
259     val = 0;
260     nb = 0;
261     if (fd0 < 3)
262         nb++;
263     if (fd1 < 3)
264         nb++;
265     switch (nb) {
266     case 0:
267         break;
268     case 1:
269         val |= 0x01; /* 1 drive, ready for boot */
270         break;
271     case 2:
272         val |= 0x41; /* 2 drives, ready for boot */
273         break;
274     }
275     val |= 0x02; /* FPU is there */
276     val |= 0x04; /* PS/2 mouse installed */
277     rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
278
279     /* hard drives */
280
281     rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
282     if (hd_table[0])
283         cmos_init_hd(0x19, 0x1b, hd_table[0]);
284     if (hd_table[1])
285         cmos_init_hd(0x1a, 0x24, hd_table[1]);
286
287     val = 0;
288     for (i = 0; i < 4; i++) {
289         if (hd_table[i]) {
290             int cylinders, heads, sectors, translation;
291             /* NOTE: bdrv_get_geometry_hint() returns the physical
292                 geometry.  It is always such that: 1 <= sects <= 63, 1
293                 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
294                 geometry can be different if a translation is done. */
295             translation = bdrv_get_translation_hint(hd_table[i]);
296             if (translation == BIOS_ATA_TRANSLATION_AUTO) {
297                 bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
298                 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
299                     /* No translation. */
300                     translation = 0;
301                 } else {
302                     /* LBA translation. */
303                     translation = 1;
304                 }
305             } else {
306                 translation--;
307             }
308             val |= translation << (i * 2);
309         }
310     }
311     rtc_set_memory(s, 0x39, val);
312 }
313
314 void ioport_set_a20(int enable)
315 {
316     /* XXX: send to all CPUs ? */
317     cpu_x86_set_a20(first_cpu, enable);
318 }
319
320 int ioport_get_a20(void)
321 {
322     return ((first_cpu->a20_mask >> 20) & 1);
323 }
324
325 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
326 {
327     ioport_set_a20((val >> 1) & 1);
328     /* XXX: bit 0 is fast reset */
329 }
330
331 static uint32_t ioport92_read(void *opaque, uint32_t addr)
332 {
333     return ioport_get_a20() << 1;
334 }
335
336 /***********************************************************/
337 /* Bochs BIOS debug ports */
338
339 static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
340 {
341     static const char shutdown_str[8] = "Shutdown";
342     static int shutdown_index = 0;
343
344     switch(addr) {
345         /* Bochs BIOS messages */
346     case 0x400:
347     case 0x401:
348         fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
349         exit(1);
350     case 0x402:
351     case 0x403:
352 #ifdef DEBUG_BIOS
353         fprintf(stderr, "%c", val);
354 #endif
355         break;
356     case 0x8900:
357         /* same as Bochs power off */
358         if (val == shutdown_str[shutdown_index]) {
359             shutdown_index++;
360             if (shutdown_index == 8) {
361                 shutdown_index = 0;
362                 qemu_system_shutdown_request();
363             }
364         } else {
365             shutdown_index = 0;
366         }
367         break;
368
369         /* LGPL'ed VGA BIOS messages */
370     case 0x501:
371     case 0x502:
372         fprintf(stderr, "VGA BIOS panic, line %d\n", val);
373         exit(1);
374     case 0x500:
375     case 0x503:
376 #ifdef DEBUG_BIOS
377         fprintf(stderr, "%c", val);
378 #endif
379         break;
380     }
381 }
382
383 static void bochs_bios_init(void)
384 {
385     register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
386     register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
387     register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
388     register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
389     register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
390
391     register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
392     register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
393     register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
394     register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
395 }
396
397 /* Generate an initial boot sector which sets state and jump to
398    a specified vector */
399 static void generate_bootsect(uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
400 {
401     uint8_t bootsect[512], *p;
402     int i;
403     int hda;
404
405     hda = drive_get_index(IF_IDE, 0, 0);
406     if (hda == -1) {
407         fprintf(stderr, "A disk image must be given for 'hda' when booting "
408                 "a Linux kernel\n");
409         exit(1);
410     }
411
412     memset(bootsect, 0, sizeof(bootsect));
413
414     /* Copy the MSDOS partition table if possible */
415     bdrv_read(drives_table[hda].bdrv, 0, bootsect, 1);
416
417     /* Make sure we have a partition signature */
418     bootsect[510] = 0x55;
419     bootsect[511] = 0xaa;
420
421     /* Actual code */
422     p = bootsect;
423     *p++ = 0xfa;                /* CLI */
424     *p++ = 0xfc;                /* CLD */
425
426     for (i = 0; i < 6; i++) {
427         if (i == 1)             /* Skip CS */
428             continue;
429
430         *p++ = 0xb8;            /* MOV AX,imm16 */
431         *p++ = segs[i];
432         *p++ = segs[i] >> 8;
433         *p++ = 0x8e;            /* MOV <seg>,AX */
434         *p++ = 0xc0 + (i << 3);
435     }
436
437     for (i = 0; i < 8; i++) {
438         *p++ = 0x66;            /* 32-bit operand size */
439         *p++ = 0xb8 + i;        /* MOV <reg>,imm32 */
440         *p++ = gpr[i];
441         *p++ = gpr[i] >> 8;
442         *p++ = gpr[i] >> 16;
443         *p++ = gpr[i] >> 24;
444     }
445
446     *p++ = 0xea;                /* JMP FAR */
447     *p++ = ip;                  /* IP */
448     *p++ = ip >> 8;
449     *p++ = segs[1];             /* CS */
450     *p++ = segs[1] >> 8;
451
452     bdrv_set_boot_sector(drives_table[hda].bdrv, bootsect, sizeof(bootsect));
453 }
454
455 static long get_file_size(FILE *f)
456 {
457     long where, size;
458
459     /* XXX: on Unix systems, using fstat() probably makes more sense */
460
461     where = ftell(f);
462     fseek(f, 0, SEEK_END);
463     size = ftell(f);
464     fseek(f, where, SEEK_SET);
465
466     return size;
467 }
468
469 static void load_linux(const char *kernel_filename,
470                        const char *initrd_filename,
471                        const char *kernel_cmdline)
472 {
473     uint16_t protocol;
474     uint32_t gpr[8];
475     uint16_t seg[6];
476     uint16_t real_seg;
477     int setup_size, kernel_size, initrd_size, cmdline_size;
478     uint32_t initrd_max;
479     uint8_t header[1024];
480     uint8_t *real_addr, *prot_addr, *cmdline_addr, *initrd_addr;
481     FILE *f, *fi;
482
483     /* Align to 16 bytes as a paranoia measure */
484     cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
485
486     /* load the kernel header */
487     f = fopen(kernel_filename, "rb");
488     if (!f || !(kernel_size = get_file_size(f)) ||
489         fread(header, 1, 1024, f) != 1024) {
490         fprintf(stderr, "qemu: could not load kernel '%s'\n",
491                 kernel_filename);
492         exit(1);
493     }
494
495     /* kernel protocol version */
496 #if 0
497     fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
498 #endif
499     if (ldl_p(header+0x202) == 0x53726448)
500         protocol = lduw_p(header+0x206);
501     else
502         protocol = 0;
503
504     if (protocol < 0x200 || !(header[0x211] & 0x01)) {
505         /* Low kernel */
506         real_addr    = phys_ram_base + 0x90000;
507         cmdline_addr = phys_ram_base + 0x9a000 - cmdline_size;
508         prot_addr    = phys_ram_base + 0x10000;
509     } else if (protocol < 0x202) {
510         /* High but ancient kernel */
511         real_addr    = phys_ram_base + 0x90000;
512         cmdline_addr = phys_ram_base + 0x9a000 - cmdline_size;
513         prot_addr    = phys_ram_base + 0x100000;
514     } else {
515         /* High and recent kernel */
516         real_addr    = phys_ram_base + 0x10000;
517         cmdline_addr = phys_ram_base + 0x20000;
518         prot_addr    = phys_ram_base + 0x100000;
519     }
520
521 #if 0
522     fprintf(stderr,
523             "qemu: real_addr     = %#zx\n"
524             "qemu: cmdline_addr  = %#zx\n"
525             "qemu: prot_addr     = %#zx\n",
526             real_addr-phys_ram_base,
527             cmdline_addr-phys_ram_base,
528             prot_addr-phys_ram_base);
529 #endif
530
531     /* highest address for loading the initrd */
532     if (protocol >= 0x203)
533         initrd_max = ldl_p(header+0x22c);
534     else
535         initrd_max = 0x37ffffff;
536
537     if (initrd_max >= ram_size-ACPI_DATA_SIZE)
538         initrd_max = ram_size-ACPI_DATA_SIZE-1;
539
540     /* kernel command line */
541     pstrcpy((char*)cmdline_addr, 4096, kernel_cmdline);
542
543     if (protocol >= 0x202) {
544         stl_p(header+0x228, cmdline_addr-phys_ram_base);
545     } else {
546         stw_p(header+0x20, 0xA33F);
547         stw_p(header+0x22, cmdline_addr-real_addr);
548     }
549
550     /* loader type */
551     /* High nybble = B reserved for Qemu; low nybble is revision number.
552        If this code is substantially changed, you may want to consider
553        incrementing the revision. */
554     if (protocol >= 0x200)
555         header[0x210] = 0xB0;
556
557     /* heap */
558     if (protocol >= 0x201) {
559         header[0x211] |= 0x80;  /* CAN_USE_HEAP */
560         stw_p(header+0x224, cmdline_addr-real_addr-0x200);
561     }
562
563     /* load initrd */
564     if (initrd_filename) {
565         if (protocol < 0x200) {
566             fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
567             exit(1);
568         }
569
570         fi = fopen(initrd_filename, "rb");
571         if (!fi) {
572             fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
573                     initrd_filename);
574             exit(1);
575         }
576
577         initrd_size = get_file_size(fi);
578         initrd_addr = phys_ram_base + ((initrd_max-initrd_size) & ~4095);
579
580         fprintf(stderr, "qemu: loading initrd (%#x bytes) at %#zx\n",
581                 initrd_size, initrd_addr-phys_ram_base);
582
583         if (fread(initrd_addr, 1, initrd_size, fi) != initrd_size) {
584             fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
585                     initrd_filename);
586             exit(1);
587         }
588         fclose(fi);
589
590         stl_p(header+0x218, initrd_addr-phys_ram_base);
591         stl_p(header+0x21c, initrd_size);
592     }
593
594     /* store the finalized header and load the rest of the kernel */
595     memcpy(real_addr, header, 1024);
596
597     setup_size = header[0x1f1];
598     if (setup_size == 0)
599         setup_size = 4;
600
601     setup_size = (setup_size+1)*512;
602     kernel_size -= setup_size;  /* Size of protected-mode code */
603
604     if (fread(real_addr+1024, 1, setup_size-1024, f) != setup_size-1024 ||
605         fread(prot_addr, 1, kernel_size, f) != kernel_size) {
606         fprintf(stderr, "qemu: read error on kernel '%s'\n",
607                 kernel_filename);
608         exit(1);
609     }
610     fclose(f);
611
612     /* generate bootsector to set up the initial register state */
613     real_seg = (real_addr-phys_ram_base) >> 4;
614     seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg;
615     seg[1] = real_seg+0x20;     /* CS */
616     memset(gpr, 0, sizeof gpr);
617     gpr[4] = cmdline_addr-real_addr-16; /* SP (-16 is paranoia) */
618
619     generate_bootsect(gpr, seg, 0);
620 }
621
622 static void main_cpu_reset(void *opaque)
623 {
624     CPUState *env = opaque;
625     cpu_reset(env);
626 }
627
628 static const int ide_iobase[2] = { 0x1f0, 0x170 };
629 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
630 static const int ide_irq[2] = { 14, 15 };
631
632 #define NE2000_NB_MAX 6
633
634 static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
635 static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
636
637 static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
638 static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
639
640 static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
641 static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
642
643 #ifdef HAS_AUDIO
644 static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
645 {
646     struct soundhw *c;
647     int audio_enabled = 0;
648
649     for (c = soundhw; !audio_enabled && c->name; ++c) {
650         audio_enabled = c->enabled;
651     }
652
653     if (audio_enabled) {
654         AudioState *s;
655
656         s = AUD_init ();
657         if (s) {
658             for (c = soundhw; c->name; ++c) {
659                 if (c->enabled) {
660                     if (c->isa) {
661                         c->init.init_isa (s, pic);
662                     }
663                     else {
664                         if (pci_bus) {
665                             c->init.init_pci (pci_bus, s);
666                         }
667                     }
668                 }
669             }
670         }
671     }
672 }
673 #endif
674
675 static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
676 {
677     static int nb_ne2k = 0;
678
679     if (nb_ne2k == NE2000_NB_MAX)
680         return;
681     isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd);
682     nb_ne2k++;
683 }
684
685 /* PC hardware initialisation */
686 static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
687                      const char *boot_device, DisplayState *ds,
688                      const char *kernel_filename, const char *kernel_cmdline,
689                      const char *initrd_filename,
690                      int pci_enabled, const char *cpu_model)
691 {
692     char buf[1024];
693     int ret, linux_boot, i;
694     ram_addr_t ram_addr, vga_ram_addr, bios_offset, vga_bios_offset;
695     ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
696     int bios_size, isa_bios_size, vga_bios_size;
697     PCIBus *pci_bus;
698     int piix3_devfn = -1;
699     CPUState *env;
700     NICInfo *nd;
701     qemu_irq *cpu_irq;
702     qemu_irq *i8259;
703     int index;
704     BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
705     BlockDriverState *fd[MAX_FD];
706
707     if (ram_size >= 0xe0000000 ) {
708         above_4g_mem_size = ram_size - 0xe0000000;
709         below_4g_mem_size = 0xe0000000;
710     } else {
711         below_4g_mem_size = ram_size;
712     }
713
714     linux_boot = (kernel_filename != NULL);
715
716     /* init CPUs */
717     if (cpu_model == NULL) {
718 #ifdef TARGET_X86_64
719         cpu_model = "qemu64";
720 #else
721         cpu_model = "qemu32";
722 #endif
723     }
724     
725     for(i = 0; i < smp_cpus; i++) {
726         env = cpu_init(cpu_model);
727         if (!env) {
728             fprintf(stderr, "Unable to find x86 CPU definition\n");
729             exit(1);
730         }
731         if (i != 0)
732             env->hflags |= HF_HALTED_MASK;
733         if (smp_cpus > 1) {
734             /* XXX: enable it in all cases */
735             env->cpuid_features |= CPUID_APIC;
736         }
737         register_savevm("cpu", i, 4, cpu_save, cpu_load, env);
738         qemu_register_reset(main_cpu_reset, env);
739         if (pci_enabled) {
740             apic_init(env);
741         }
742     }
743
744     vmport_init();
745
746     /* allocate RAM */
747     ram_addr = qemu_ram_alloc(ram_size);
748     cpu_register_physical_memory(0, below_4g_mem_size, ram_addr);
749
750     /* above 4giga memory allocation */
751     if (above_4g_mem_size > 0) {
752         cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size,
753                                      ram_addr + below_4g_mem_size);
754     }
755
756     /* allocate VGA RAM */
757     vga_ram_addr = qemu_ram_alloc(vga_ram_size);
758
759     /* BIOS load */
760     if (bios_name == NULL)
761         bios_name = BIOS_FILENAME;
762     snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
763     bios_size = get_image_size(buf);
764     if (bios_size <= 0 ||
765         (bios_size % 65536) != 0) {
766         goto bios_error;
767     }
768     bios_offset = qemu_ram_alloc(bios_size);
769     ret = load_image(buf, phys_ram_base + bios_offset);
770     if (ret != bios_size) {
771     bios_error:
772         fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
773         exit(1);
774     }
775
776     /* VGA BIOS load */
777     if (cirrus_vga_enabled) {
778         snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
779     } else {
780         snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
781     }
782     vga_bios_size = get_image_size(buf);
783     if (vga_bios_size <= 0 || vga_bios_size > 65536)
784         goto vga_bios_error;
785     vga_bios_offset = qemu_ram_alloc(65536);
786
787     ret = load_image(buf, phys_ram_base + vga_bios_offset);
788     if (ret != vga_bios_size) {
789     vga_bios_error:
790         fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf);
791         exit(1);
792     }
793
794     /* setup basic memory access */
795     cpu_register_physical_memory(0xc0000, 0x10000,
796                                  vga_bios_offset | IO_MEM_ROM);
797
798     /* map the last 128KB of the BIOS in ISA space */
799     isa_bios_size = bios_size;
800     if (isa_bios_size > (128 * 1024))
801         isa_bios_size = 128 * 1024;
802     cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size,
803                                  IO_MEM_UNASSIGNED);
804     cpu_register_physical_memory(0x100000 - isa_bios_size,
805                                  isa_bios_size,
806                                  (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
807
808     {
809         ram_addr_t option_rom_offset;
810         int size, offset;
811
812         offset = 0;
813         for (i = 0; i < nb_option_roms; i++) {
814             size = get_image_size(option_rom[i]);
815             if (size < 0) {
816                 fprintf(stderr, "Could not load option rom '%s'\n",
817                         option_rom[i]);
818                 exit(1);
819             }
820             if (size > (0x10000 - offset))
821                 goto option_rom_error;
822             option_rom_offset = qemu_ram_alloc(size);
823             ret = load_image(option_rom[i], phys_ram_base + option_rom_offset);
824             if (ret != size) {
825             option_rom_error:
826                 fprintf(stderr, "Too many option ROMS\n");
827                 exit(1);
828             }
829             size = (size + 4095) & ~4095;
830             cpu_register_physical_memory(0xd0000 + offset,
831                                          size, option_rom_offset | IO_MEM_ROM);
832             offset += size;
833         }
834     }
835
836     /* map all the bios at the top of memory */
837     cpu_register_physical_memory((uint32_t)(-bios_size),
838                                  bios_size, bios_offset | IO_MEM_ROM);
839
840     bochs_bios_init();
841
842     if (linux_boot)
843         load_linux(kernel_filename, initrd_filename, kernel_cmdline);
844
845     cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
846     i8259 = i8259_init(cpu_irq[0]);
847     ferr_irq = i8259[13];
848
849     if (pci_enabled) {
850         pci_bus = i440fx_init(&i440fx_state, i8259);
851         piix3_devfn = piix3_init(pci_bus, -1);
852     } else {
853         pci_bus = NULL;
854     }
855
856     /* init basic PC hardware */
857     register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
858
859     register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
860
861     if (cirrus_vga_enabled) {
862         if (pci_enabled) {
863             pci_cirrus_vga_init(pci_bus,
864                                 ds, phys_ram_base + vga_ram_addr,
865                                 vga_ram_addr, vga_ram_size);
866         } else {
867             isa_cirrus_vga_init(ds, phys_ram_base + vga_ram_addr,
868                                 vga_ram_addr, vga_ram_size);
869         }
870     } else if (vmsvga_enabled) {
871         if (pci_enabled)
872             pci_vmsvga_init(pci_bus, ds, phys_ram_base + vga_ram_addr,
873                             vga_ram_addr, vga_ram_size);
874         else
875             fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
876     } else {
877         if (pci_enabled) {
878             pci_vga_init(pci_bus, ds, phys_ram_base + vga_ram_addr,
879                          vga_ram_addr, vga_ram_size, 0, 0);
880         } else {
881             isa_vga_init(ds, phys_ram_base + vga_ram_addr,
882                          vga_ram_addr, vga_ram_size);
883         }
884     }
885
886     rtc_state = rtc_init(0x70, i8259[8]);
887
888     register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
889     register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
890
891     if (pci_enabled) {
892         ioapic = ioapic_init();
893     }
894     pit = pit_init(0x40, i8259[0]);
895     pcspk_init(pit);
896     if (pci_enabled) {
897         pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
898     }
899
900     for(i = 0; i < MAX_SERIAL_PORTS; i++) {
901         if (serial_hds[i]) {
902             serial_init(serial_io[i], i8259[serial_irq[i]], serial_hds[i]);
903         }
904     }
905
906     for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
907         if (parallel_hds[i]) {
908             parallel_init(parallel_io[i], i8259[parallel_irq[i]],
909                           parallel_hds[i]);
910         }
911     }
912
913     for(i = 0; i < nb_nics; i++) {
914         nd = &nd_table[i];
915         if (!nd->model) {
916             if (pci_enabled) {
917                 nd->model = "ne2k_pci";
918             } else {
919                 nd->model = "ne2k_isa";
920             }
921         }
922         if (strcmp(nd->model, "ne2k_isa") == 0) {
923             pc_init_ne2k_isa(nd, i8259);
924         } else if (pci_enabled) {
925             if (strcmp(nd->model, "?") == 0)
926                 fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n");
927             pci_nic_init(pci_bus, nd, -1);
928         } else if (strcmp(nd->model, "?") == 0) {
929             fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n");
930             exit(1);
931         } else {
932             fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
933             exit(1);
934         }
935     }
936
937     if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
938         fprintf(stderr, "qemu: too many IDE bus\n");
939         exit(1);
940     }
941
942     for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
943         index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
944         if (index != -1)
945             hd[i] = drives_table[index].bdrv;
946         else
947             hd[i] = NULL;
948     }
949
950     if (pci_enabled) {
951         pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, i8259);
952     } else {
953         for(i = 0; i < MAX_IDE_BUS; i++) {
954             isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
955                          hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
956         }
957     }
958
959     i8042_init(i8259[1], i8259[12], 0x60);
960     DMA_init(0);
961 #ifdef HAS_AUDIO
962     audio_init(pci_enabled ? pci_bus : NULL, i8259);
963 #endif
964
965     for(i = 0; i < MAX_FD; i++) {
966         index = drive_get_index(IF_FLOPPY, 0, i);
967         if (index != -1)
968             fd[i] = drives_table[index].bdrv;
969         else
970             fd[i] = NULL;
971     }
972     floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
973
974     cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
975
976     if (pci_enabled && usb_enabled) {
977         usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
978     }
979
980     if (pci_enabled && acpi_enabled) {
981         uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
982         i2c_bus *smbus;
983
984         /* TODO: Populate SPD eeprom data.  */
985         smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]);
986         for (i = 0; i < 8; i++) {
987             smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256));
988         }
989     }
990
991     if (i440fx_state) {
992         i440fx_init_memory_mappings(i440fx_state);
993     }
994
995     if (pci_enabled) {
996         int max_bus;
997         int bus, unit;
998         void *scsi;
999
1000         max_bus = drive_get_max_bus(IF_SCSI);
1001
1002         for (bus = 0; bus <= max_bus; bus++) {
1003             scsi = lsi_scsi_init(pci_bus, -1);
1004             for (unit = 0; unit < LSI_MAX_DEVS; unit++) {
1005                 index = drive_get_index(IF_SCSI, bus, unit);
1006                 if (index == -1)
1007                     continue;
1008                 lsi_scsi_attach(scsi, drives_table[index].bdrv, unit);
1009             }
1010         }
1011     }
1012 }
1013
1014 static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size,
1015                         const char *boot_device, DisplayState *ds,
1016                         const char *kernel_filename,
1017                         const char *kernel_cmdline,
1018                         const char *initrd_filename,
1019                         const char *cpu_model)
1020 {
1021     pc_init1(ram_size, vga_ram_size, boot_device, ds,
1022              kernel_filename, kernel_cmdline,
1023              initrd_filename, 1, cpu_model);
1024 }
1025
1026 static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size,
1027                         const char *boot_device, DisplayState *ds,
1028                         const char *kernel_filename,
1029                         const char *kernel_cmdline,
1030                         const char *initrd_filename,
1031                         const char *cpu_model)
1032 {
1033     pc_init1(ram_size, vga_ram_size, boot_device, ds,
1034              kernel_filename, kernel_cmdline,
1035              initrd_filename, 0, cpu_model);
1036 }
1037
1038 QEMUMachine pc_machine = {
1039     "pc",
1040     "Standard PC",
1041     pc_init_pci,
1042 };
1043
1044 QEMUMachine isapc_machine = {
1045     "isapc",
1046     "ISA-only PC",
1047     pc_init_isa,
1048 };