Statistics
| Branch: | Revision:

root / hw / pc.c @ d537cf6c

History | View | Annotate | Download (24.6 kB)

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
static qemu_irq ferr_irq;
53
/* XXX: add IGNNE support */
54
void cpu_set_ferr(CPUX86State *s)
55
{
56
    qemu_irq_raise(ferr_irq);
57
}
58

    
59
static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
60
{
61
    qemu_irq_lower(ferr_irq);
62
}
63

    
64
/* TSC handling */
65
uint64_t cpu_get_tsc(CPUX86State *env)
66
{
67
    /* Note: when using kqemu, it is more logical to return the host TSC
68
       because kqemu does not trap the RDTSC instruction for
69
       performance reasons */
70
#if USE_KQEMU
71
    if (env->kqemu_enabled) {
72
        return cpu_get_real_ticks();
73
    } else 
74
#endif
75
    {
76
        return cpu_get_ticks();
77
    }
78
}
79

    
80
/* SMM support */
81
void cpu_smm_update(CPUState *env)
82
{
83
    if (i440fx_state && env == first_cpu)
84
        i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
85
}
86

    
87

    
88
/* IRQ handling */
89
int cpu_get_pic_interrupt(CPUState *env)
90
{
91
    int intno;
92

    
93
    intno = apic_get_interrupt(env);
94
    if (intno >= 0) {
95
        /* set irq request if a PIC irq is still pending */
96
        /* XXX: improve that */
97
        pic_update_irq(isa_pic); 
98
        return intno;
99
    }
100
    /* read the irq from the PIC */
101
    intno = pic_read_irq(isa_pic);
102
    return intno;
103
}
104

    
105
static void pic_irq_request(void *opaque, int irq, int level)
106
{
107
    CPUState *env = opaque;
108
    if (level)
109
        cpu_interrupt(env, CPU_INTERRUPT_HARD);
110
    else
111
        cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
112
}
113

    
114
/* PC cmos mappings */
115

    
116
#define REG_EQUIPMENT_BYTE          0x14
117

    
118
static int cmos_get_fd_drive_type(int fd0)
119
{
120
    int val;
121

    
122
    switch (fd0) {
123
    case 0:
124
        /* 1.44 Mb 3"5 drive */
125
        val = 4;
126
        break;
127
    case 1:
128
        /* 2.88 Mb 3"5 drive */
129
        val = 5;
130
        break;
131
    case 2:
132
        /* 1.2 Mb 5"5 drive */
133
        val = 2;
134
        break;
135
    default:
136
        val = 0;
137
        break;
138
    }
139
    return val;
140
}
141

    
142
static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd) 
143
{
144
    RTCState *s = rtc_state;
145
    int cylinders, heads, sectors;
146
    bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
147
    rtc_set_memory(s, type_ofs, 47);
148
    rtc_set_memory(s, info_ofs, cylinders);
149
    rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
150
    rtc_set_memory(s, info_ofs + 2, heads);
151
    rtc_set_memory(s, info_ofs + 3, 0xff);
152
    rtc_set_memory(s, info_ofs + 4, 0xff);
153
    rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
154
    rtc_set_memory(s, info_ofs + 6, cylinders);
155
    rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
156
    rtc_set_memory(s, info_ofs + 8, sectors);
157
}
158

    
159
/* hd_table must contain 4 block drivers */
160
static void cmos_init(int ram_size, int boot_device, BlockDriverState **hd_table)
161
{
162
    RTCState *s = rtc_state;
163
    int val;
164
    int fd0, fd1, nb;
165
    int i;
166

    
167
    /* various important CMOS locations needed by PC/Bochs bios */
168

    
169
    /* memory size */
170
    val = 640; /* base memory in K */
171
    rtc_set_memory(s, 0x15, val);
172
    rtc_set_memory(s, 0x16, val >> 8);
173

    
174
    val = (ram_size / 1024) - 1024;
175
    if (val > 65535)
176
        val = 65535;
177
    rtc_set_memory(s, 0x17, val);
178
    rtc_set_memory(s, 0x18, val >> 8);
179
    rtc_set_memory(s, 0x30, val);
180
    rtc_set_memory(s, 0x31, val >> 8);
181

    
182
    if (ram_size > (16 * 1024 * 1024))
183
        val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
184
    else
185
        val = 0;
186
    if (val > 65535)
187
        val = 65535;
188
    rtc_set_memory(s, 0x34, val);
189
    rtc_set_memory(s, 0x35, val >> 8);
190
    
191
    switch(boot_device) {
192
    case 'a':
193
    case 'b':
194
        rtc_set_memory(s, 0x3d, 0x01); /* floppy boot */
195
        if (!fd_bootchk)
196
            rtc_set_memory(s, 0x38, 0x01); /* disable signature check */
197
        break;
198
    default:
199
    case 'c':
200
        rtc_set_memory(s, 0x3d, 0x02); /* hard drive boot */
201
        break;
202
    case 'd':
203
        rtc_set_memory(s, 0x3d, 0x03); /* CD-ROM boot */
204
        break;
205
    }
206

    
207
    /* floppy type */
208

    
209
    fd0 = fdctrl_get_drive_type(floppy_controller, 0);
210
    fd1 = fdctrl_get_drive_type(floppy_controller, 1);
211

    
212
    val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
213
    rtc_set_memory(s, 0x10, val);
214
    
215
    val = 0;
216
    nb = 0;
217
    if (fd0 < 3)
218
        nb++;
219
    if (fd1 < 3)
220
        nb++;
221
    switch (nb) {
222
    case 0:
223
        break;
224
    case 1:
225
        val |= 0x01; /* 1 drive, ready for boot */
226
        break;
227
    case 2:
228
        val |= 0x41; /* 2 drives, ready for boot */
229
        break;
230
    }
231
    val |= 0x02; /* FPU is there */
232
    val |= 0x04; /* PS/2 mouse installed */
233
    rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
234

    
235
    /* hard drives */
236

    
237
    rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
238
    if (hd_table[0])
239
        cmos_init_hd(0x19, 0x1b, hd_table[0]);
240
    if (hd_table[1]) 
241
        cmos_init_hd(0x1a, 0x24, hd_table[1]);
242

    
243
    val = 0;
244
    for (i = 0; i < 4; i++) {
245
        if (hd_table[i]) {
246
            int cylinders, heads, sectors, translation;
247
            /* NOTE: bdrv_get_geometry_hint() returns the physical
248
                geometry.  It is always such that: 1 <= sects <= 63, 1
249
                <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
250
                geometry can be different if a translation is done. */
251
            translation = bdrv_get_translation_hint(hd_table[i]);
252
            if (translation == BIOS_ATA_TRANSLATION_AUTO) {
253
                bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
254
                if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
255
                    /* No translation. */
256
                    translation = 0;
257
                } else {
258
                    /* LBA translation. */
259
                    translation = 1;
260
                }
261
            } else {
262
                translation--;
263
            }
264
            val |= translation << (i * 2);
265
        }
266
    }
267
    rtc_set_memory(s, 0x39, val);
268
}
269

    
270
void ioport_set_a20(int enable)
271
{
272
    /* XXX: send to all CPUs ? */
273
    cpu_x86_set_a20(first_cpu, enable);
274
}
275

    
276
int ioport_get_a20(void)
277
{
278
    return ((first_cpu->a20_mask >> 20) & 1);
279
}
280

    
281
static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
282
{
283
    ioport_set_a20((val >> 1) & 1);
284
    /* XXX: bit 0 is fast reset */
285
}
286

    
287
static uint32_t ioport92_read(void *opaque, uint32_t addr)
288
{
289
    return ioport_get_a20() << 1;
290
}
291

    
292
/***********************************************************/
293
/* Bochs BIOS debug ports */
294

    
295
void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
296
{
297
    static const char shutdown_str[8] = "Shutdown";
298
    static int shutdown_index = 0;
299
    
300
    switch(addr) {
301
        /* Bochs BIOS messages */
302
    case 0x400:
303
    case 0x401:
304
        fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
305
        exit(1);
306
    case 0x402:
307
    case 0x403:
308
#ifdef DEBUG_BIOS
309
        fprintf(stderr, "%c", val);
310
#endif
311
        break;
312
    case 0x8900:
313
        /* same as Bochs power off */
314
        if (val == shutdown_str[shutdown_index]) {
315
            shutdown_index++;
316
            if (shutdown_index == 8) {
317
                shutdown_index = 0;
318
                qemu_system_shutdown_request();
319
            }
320
        } else {
321
            shutdown_index = 0;
322
        }
323
        break;
324

    
325
        /* LGPL'ed VGA BIOS messages */
326
    case 0x501:
327
    case 0x502:
328
        fprintf(stderr, "VGA BIOS panic, line %d\n", val);
329
        exit(1);
330
    case 0x500:
331
    case 0x503:
332
#ifdef DEBUG_BIOS
333
        fprintf(stderr, "%c", val);
334
#endif
335
        break;
336
    }
337
}
338

    
339
void bochs_bios_init(void)
340
{
341
    register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
342
    register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
343
    register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
344
    register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
345
    register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
346

    
347
    register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
348
    register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
349
    register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
350
    register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
351
}
352

    
353

    
354
int load_kernel(const char *filename, uint8_t *addr, 
355
                uint8_t *real_addr)
356
{
357
    int fd, size;
358
    int setup_sects;
359

    
360
    fd = open(filename, O_RDONLY | O_BINARY);
361
    if (fd < 0)
362
        return -1;
363

    
364
    /* load 16 bit code */
365
    if (read(fd, real_addr, 512) != 512)
366
        goto fail;
367
    setup_sects = real_addr[0x1F1];
368
    if (!setup_sects)
369
        setup_sects = 4;
370
    if (read(fd, real_addr + 512, setup_sects * 512) != 
371
        setup_sects * 512)
372
        goto fail;
373
    
374
    /* load 32 bit code */
375
    size = read(fd, addr, 16 * 1024 * 1024);
376
    if (size < 0)
377
        goto fail;
378
    close(fd);
379
    return size;
380
 fail:
381
    close(fd);
382
    return -1;
383
}
384

    
385
static void main_cpu_reset(void *opaque)
386
{
387
    CPUState *env = opaque;
388
    cpu_reset(env);
389
}
390

    
391
static const int ide_iobase[2] = { 0x1f0, 0x170 };
392
static const int ide_iobase2[2] = { 0x3f6, 0x376 };
393
static const int ide_irq[2] = { 14, 15 };
394

    
395
#define NE2000_NB_MAX 6
396

    
397
static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
398
static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
399

    
400
static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
401
static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
402

    
403
static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
404
static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
405

    
406
#ifdef HAS_AUDIO
407
static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
408
{
409
    struct soundhw *c;
410
    int audio_enabled = 0;
411

    
412
    for (c = soundhw; !audio_enabled && c->name; ++c) {
413
        audio_enabled = c->enabled;
414
    }
415

    
416
    if (audio_enabled) {
417
        AudioState *s;
418

    
419
        s = AUD_init ();
420
        if (s) {
421
            for (c = soundhw; c->name; ++c) {
422
                if (c->enabled) {
423
                    if (c->isa) {
424
                        c->init.init_isa (s, pic);
425
                    }
426
                    else {
427
                        if (pci_bus) {
428
                            c->init.init_pci (pci_bus, s);
429
                        }
430
                    }
431
                }
432
            }
433
        }
434
    }
435
}
436
#endif
437

    
438
static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
439
{
440
    static int nb_ne2k = 0;
441

    
442
    if (nb_ne2k == NE2000_NB_MAX)
443
        return;
444
    isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd);
445
    nb_ne2k++;
446
}
447

    
448
/* PC hardware initialisation */
449
static void pc_init1(int ram_size, int vga_ram_size, int boot_device,
450
                     DisplayState *ds, const char **fd_filename, int snapshot,
451
                     const char *kernel_filename, const char *kernel_cmdline,
452
                     const char *initrd_filename,
453
                     int pci_enabled)
454
{
455
    char buf[1024];
456
    int ret, linux_boot, initrd_size, i;
457
    ram_addr_t ram_addr, vga_ram_addr, bios_offset, vga_bios_offset;
458
    ram_addr_t initrd_offset;
459
    int bios_size, isa_bios_size, vga_bios_size;
460
    PCIBus *pci_bus;
461
    int piix3_devfn = -1;
462
    CPUState *env;
463
    NICInfo *nd;
464
    qemu_irq *cpu_irq;
465
    qemu_irq *i8259;
466

    
467
    linux_boot = (kernel_filename != NULL);
468

    
469
    /* init CPUs */
470
    for(i = 0; i < smp_cpus; i++) {
471
        env = cpu_init();
472
        if (i != 0)
473
            env->hflags |= HF_HALTED_MASK;
474
        if (smp_cpus > 1) {
475
            /* XXX: enable it in all cases */
476
            env->cpuid_features |= CPUID_APIC;
477
        }
478
        register_savevm("cpu", i, 4, cpu_save, cpu_load, env);
479
        qemu_register_reset(main_cpu_reset, env);
480
        if (pci_enabled) {
481
            apic_init(env);
482
        }
483
    }
484

    
485
    /* allocate RAM */
486
    ram_addr = qemu_ram_alloc(ram_size);
487
    cpu_register_physical_memory(0, ram_size, ram_addr);
488

    
489
    /* allocate VGA RAM */
490
    vga_ram_addr = qemu_ram_alloc(vga_ram_size);
491

    
492
    /* BIOS load */
493
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
494
    bios_size = get_image_size(buf);
495
    if (bios_size <= 0 || 
496
        (bios_size % 65536) != 0) {
497
        goto bios_error;
498
    }
499
    bios_offset = qemu_ram_alloc(bios_size);
500
    ret = load_image(buf, phys_ram_base + bios_offset);
501
    if (ret != bios_size) {
502
    bios_error:
503
        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
504
        exit(1);
505
    }
506

    
507
    /* VGA BIOS load */
508
    if (cirrus_vga_enabled) {
509
        snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
510
    } else {
511
        snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
512
    }
513
    vga_bios_size = get_image_size(buf);
514
    if (vga_bios_size <= 0 || vga_bios_size > 65536) 
515
        goto vga_bios_error;
516
    vga_bios_offset = qemu_ram_alloc(65536);
517

    
518
    ret = load_image(buf, phys_ram_base + vga_bios_offset);
519
    if (ret != vga_bios_size) {
520
    vga_bios_error:
521
        fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf);
522
        exit(1);
523
    }
524

    
525
    /* setup basic memory access */
526
    cpu_register_physical_memory(0xc0000, 0x10000, 
527
                                 vga_bios_offset | IO_MEM_ROM);
528

    
529
    /* map the last 128KB of the BIOS in ISA space */
530
    isa_bios_size = bios_size;
531
    if (isa_bios_size > (128 * 1024))
532
        isa_bios_size = 128 * 1024;
533
    cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size, 
534
                                 IO_MEM_UNASSIGNED);
535
    cpu_register_physical_memory(0x100000 - isa_bios_size, 
536
                                 isa_bios_size, 
537
                                 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
538

    
539
    {
540
        ram_addr_t option_rom_offset;
541
        int size, offset;
542

    
543
        offset = 0;
544
        for (i = 0; i < nb_option_roms; i++) {
545
            size = get_image_size(option_rom[i]);
546
            if (size < 0) {
547
                fprintf(stderr, "Could not load option rom '%s'\n", 
548
                        option_rom[i]);
549
                exit(1);
550
            }
551
            if (size > (0x10000 - offset))
552
                goto option_rom_error;
553
            option_rom_offset = qemu_ram_alloc(size);
554
            ret = load_image(option_rom[i], phys_ram_base + option_rom_offset);
555
            if (ret != size) {
556
            option_rom_error:
557
                fprintf(stderr, "Too many option ROMS\n");
558
                exit(1);
559
            }
560
            size = (size + 4095) & ~4095;
561
            cpu_register_physical_memory(0xd0000 + offset,
562
                                         size, option_rom_offset | IO_MEM_ROM);
563
            offset += size;
564
        }
565
    }
566

    
567
    /* map all the bios at the top of memory */
568
    cpu_register_physical_memory((uint32_t)(-bios_size), 
569
                                 bios_size, bios_offset | IO_MEM_ROM);
570
    
571
    bochs_bios_init();
572

    
573
    if (linux_boot) {
574
        uint8_t bootsect[512];
575
        uint8_t old_bootsect[512];
576

    
577
        if (bs_table[0] == NULL) {
578
            fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n");
579
            exit(1);
580
        }
581
        snprintf(buf, sizeof(buf), "%s/%s", bios_dir, LINUX_BOOT_FILENAME);
582
        ret = load_image(buf, bootsect);
583
        if (ret != sizeof(bootsect)) {
584
            fprintf(stderr, "qemu: could not load linux boot sector '%s'\n",
585
                    buf);
586
            exit(1);
587
        }
588

    
589
        if (bdrv_read(bs_table[0], 0, old_bootsect, 1) >= 0) {
590
            /* copy the MSDOS partition table */
591
            memcpy(bootsect + 0x1be, old_bootsect + 0x1be, 0x40);
592
        }
593

    
594
        bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect));
595

    
596
        /* now we can load the kernel */
597
        ret = load_kernel(kernel_filename, 
598
                          phys_ram_base + KERNEL_LOAD_ADDR,
599
                          phys_ram_base + KERNEL_PARAMS_ADDR);
600
        if (ret < 0) {
601
            fprintf(stderr, "qemu: could not load kernel '%s'\n", 
602
                    kernel_filename);
603
            exit(1);
604
        }
605
        
606
        /* load initrd */
607
        initrd_size = 0;
608
        initrd_offset = 0;
609
        if (initrd_filename) {
610
            initrd_size = get_image_size (initrd_filename);
611
            if (initrd_size > 0) {
612
                initrd_offset = (ram_size - initrd_size) & TARGET_PAGE_MASK;
613
                /* Leave space for BIOS ACPI tables.  */
614
                initrd_offset -= ACPI_DATA_SIZE;
615
                /* Avoid the last 64k to avoid 2.2.x kernel bugs.  */
616
                initrd_offset -= 0x10000;
617
                if (initrd_offset > MAX_INITRD_LOAD_ADDR)
618
                    initrd_offset = MAX_INITRD_LOAD_ADDR;
619

    
620
                if (initrd_size > ram_size
621
                    || initrd_offset < KERNEL_LOAD_ADDR + ret) {
622
                    fprintf(stderr,
623
                            "qemu: memory too small for initial ram disk '%s'\n",
624
                            initrd_filename);
625
                    exit(1);
626
                }
627
                initrd_size = load_image(initrd_filename,
628
                                         phys_ram_base + initrd_offset);
629
            }
630
            if (initrd_size < 0) {
631
                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 
632
                        initrd_filename);
633
                exit(1);
634
            }
635
        }
636
        if (initrd_size > 0) {
637
            stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x218, initrd_offset);
638
            stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x21c, initrd_size);
639
        }
640
        pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096,
641
                kernel_cmdline);
642
        stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x20, 0xA33F);
643
        stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x22,
644
                KERNEL_CMDLINE_ADDR - KERNEL_PARAMS_ADDR);
645
        /* loader type */
646
        stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01);
647
    }
648

    
649
    cpu_irq = qemu_allocate_irqs(pic_irq_request, first_cpu, 1);
650
    i8259 = i8259_init(cpu_irq[0]);
651
    ferr_irq = i8259[13];
652

    
653
    if (pci_enabled) {
654
        pci_bus = i440fx_init(&i440fx_state, i8259);
655
        piix3_devfn = piix3_init(pci_bus, -1);
656
    } else {
657
        pci_bus = NULL;
658
    }
659

    
660
    /* init basic PC hardware */
661
    register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
662

    
663
    register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
664

    
665
    if (cirrus_vga_enabled) {
666
        if (pci_enabled) {
667
            pci_cirrus_vga_init(pci_bus, 
668
                                ds, phys_ram_base + vga_ram_addr, 
669
                                vga_ram_addr, vga_ram_size);
670
        } else {
671
            isa_cirrus_vga_init(ds, phys_ram_base + vga_ram_addr, 
672
                                vga_ram_addr, vga_ram_size);
673
        }
674
    } else if (vmsvga_enabled) {
675
        if (pci_enabled)
676
            pci_vmsvga_init(pci_bus, ds, phys_ram_base + ram_size,
677
                            ram_size, vga_ram_size);
678
        else
679
            fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
680
    } else {
681
        if (pci_enabled) {
682
            pci_vga_init(pci_bus, ds, phys_ram_base + vga_ram_addr, 
683
                         vga_ram_addr, vga_ram_size, 0, 0);
684
        } else {
685
            isa_vga_init(ds, phys_ram_base + vga_ram_addr, 
686
                         vga_ram_addr, vga_ram_size);
687
        }
688
    }
689

    
690
    rtc_state = rtc_init(0x70, i8259[8]);
691

    
692
    register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
693
    register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
694

    
695
    if (pci_enabled) {
696
        ioapic = ioapic_init();
697
    }
698
    pit = pit_init(0x40, i8259[0]);
699
    pcspk_init(pit);
700
    if (pci_enabled) {
701
        pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
702
    }
703

    
704
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
705
        if (serial_hds[i]) {
706
            serial_init(serial_io[i], i8259[serial_irq[i]], serial_hds[i]);
707
        }
708
    }
709

    
710
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
711
        if (parallel_hds[i]) {
712
            parallel_init(parallel_io[i], i8259[parallel_irq[i]],
713
                          parallel_hds[i]);
714
        }
715
    }
716

    
717
    for(i = 0; i < nb_nics; i++) {
718
        nd = &nd_table[i];
719
        if (!nd->model) {
720
            if (pci_enabled) {
721
                nd->model = "ne2k_pci";
722
            } else {
723
                nd->model = "ne2k_isa";
724
            }
725
        }
726
        if (strcmp(nd->model, "ne2k_isa") == 0) {
727
            pc_init_ne2k_isa(nd, i8259);
728
        } else if (pci_enabled) {
729
            pci_nic_init(pci_bus, nd, -1);
730
        } else {
731
            fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
732
            exit(1);
733
        }
734
    }
735

    
736
    if (pci_enabled) {
737
        pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1, i8259);
738
    } else {
739
        for(i = 0; i < 2; i++) {
740
            isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
741
                         bs_table[2 * i], bs_table[2 * i + 1]);
742
        }
743
    }
744

    
745
    i8042_init(i8259[1], i8259[12], 0x60);
746
    DMA_init(0);
747
#ifdef HAS_AUDIO
748
    audio_init(pci_enabled ? pci_bus : NULL, i8259);
749
#endif
750

    
751
    floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd_table);
752

    
753
    cmos_init(ram_size, boot_device, bs_table);
754

    
755
    if (pci_enabled && usb_enabled) {
756
        usb_uhci_init(pci_bus, piix3_devfn + 2);
757
    }
758

    
759
    if (pci_enabled && acpi_enabled) {
760
        uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
761
        piix4_pm_init(pci_bus, piix3_devfn + 3);
762
        for (i = 0; i < 8; i++) {
763
            SMBusDevice *eeprom = smbus_eeprom_device_init(0x50 + i,
764
                eeprom_buf + (i * 256));
765
            piix4_smbus_register_device(eeprom, 0x50 + i);
766
        }
767
    }
768
    
769
    if (i440fx_state) {
770
        i440fx_init_memory_mappings(i440fx_state);
771
    }
772
#if 0
773
    /* ??? Need to figure out some way for the user to
774
       specify SCSI devices.  */
775
    if (pci_enabled) {
776
        void *scsi;
777
        BlockDriverState *bdrv;
778

779
        scsi = lsi_scsi_init(pci_bus, -1);
780
        bdrv = bdrv_new("scsidisk");
781
        bdrv_open(bdrv, "scsi_disk.img", 0);
782
        lsi_scsi_attach(scsi, bdrv, -1);
783
        bdrv = bdrv_new("scsicd");
784
        bdrv_open(bdrv, "scsi_cd.iso", 0);
785
        bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
786
        lsi_scsi_attach(scsi, bdrv, -1);
787
    }
788
#endif
789
}
790

    
791
static void pc_init_pci(int ram_size, int vga_ram_size, int boot_device,
792
                        DisplayState *ds, const char **fd_filename, 
793
                        int snapshot, 
794
                        const char *kernel_filename, 
795
                        const char *kernel_cmdline,
796
                        const char *initrd_filename,
797
                        const char *cpu_model)
798
{
799
    pc_init1(ram_size, vga_ram_size, boot_device,
800
             ds, fd_filename, snapshot,
801
             kernel_filename, kernel_cmdline,
802
             initrd_filename, 1);
803
}
804

    
805
static void pc_init_isa(int ram_size, int vga_ram_size, int boot_device,
806
                        DisplayState *ds, const char **fd_filename, 
807
                        int snapshot, 
808
                        const char *kernel_filename, 
809
                        const char *kernel_cmdline,
810
                        const char *initrd_filename,
811
                        const char *cpu_model)
812
{
813
    pc_init1(ram_size, vga_ram_size, boot_device,
814
             ds, fd_filename, snapshot,
815
             kernel_filename, kernel_cmdline,
816
             initrd_filename, 0);
817
}
818

    
819
QEMUMachine pc_machine = {
820
    "pc",
821
    "Standard PC",
822
    pc_init_pci,
823
};
824

    
825
QEMUMachine isapc_machine = {
826
    "isapc",
827
    "ISA-only PC",
828
    pc_init_isa,
829
};