Statistics
| Branch: | Revision:

root / hw / pc.c @ 6e02c38d

History | View | Annotate | Download (32.2 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 "hw.h"
25
#include "pc.h"
26
#include "fdc.h"
27
#include "pci.h"
28
#include "block.h"
29
#include "sysemu.h"
30
#include "audio/audio.h"
31
#include "net.h"
32
#include "smbus.h"
33
#include "boards.h"
34
#include "console.h"
35
#include "fw_cfg.h"
36
#include "virtio-blk.h"
37

    
38
/* output Bochs bios info messages */
39
//#define DEBUG_BIOS
40

    
41
#define BIOS_FILENAME "bios.bin"
42
#define VGABIOS_FILENAME "vgabios.bin"
43
#define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
44

    
45
#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
46

    
47
/* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
48
#define ACPI_DATA_SIZE       0x10000
49
#define BIOS_CFG_IOPORT 0x510
50

    
51
#define MAX_IDE_BUS 2
52

    
53
static fdctrl_t *floppy_controller;
54
static RTCState *rtc_state;
55
static PITState *pit;
56
static IOAPICState *ioapic;
57
static PCIDevice *i440fx_state;
58

    
59
static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
60
{
61
}
62

    
63
/* MSDOS compatibility mode FPU exception support */
64
static qemu_irq ferr_irq;
65
/* XXX: add IGNNE support */
66
void cpu_set_ferr(CPUX86State *s)
67
{
68
    qemu_irq_raise(ferr_irq);
69
}
70

    
71
static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
72
{
73
    qemu_irq_lower(ferr_irq);
74
}
75

    
76
/* TSC handling */
77
uint64_t cpu_get_tsc(CPUX86State *env)
78
{
79
    /* Note: when using kqemu, it is more logical to return the host TSC
80
       because kqemu does not trap the RDTSC instruction for
81
       performance reasons */
82
#ifdef USE_KQEMU
83
    if (env->kqemu_enabled) {
84
        return cpu_get_real_ticks();
85
    } else
86
#endif
87
    {
88
        return cpu_get_ticks();
89
    }
90
}
91

    
92
/* SMM support */
93
void cpu_smm_update(CPUState *env)
94
{
95
    if (i440fx_state && env == first_cpu)
96
        i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
97
}
98

    
99

    
100
/* IRQ handling */
101
int cpu_get_pic_interrupt(CPUState *env)
102
{
103
    int intno;
104

    
105
    intno = apic_get_interrupt(env);
106
    if (intno >= 0) {
107
        /* set irq request if a PIC irq is still pending */
108
        /* XXX: improve that */
109
        pic_update_irq(isa_pic);
110
        return intno;
111
    }
112
    /* read the irq from the PIC */
113
    if (!apic_accept_pic_intr(env))
114
        return -1;
115

    
116
    intno = pic_read_irq(isa_pic);
117
    return intno;
118
}
119

    
120
static void pic_irq_request(void *opaque, int irq, int level)
121
{
122
    CPUState *env = first_cpu;
123

    
124
    if (env->apic_state) {
125
        while (env) {
126
            if (apic_accept_pic_intr(env))
127
                apic_deliver_pic_intr(env, level);
128
            env = env->next_cpu;
129
        }
130
    } else {
131
        if (level)
132
            cpu_interrupt(env, CPU_INTERRUPT_HARD);
133
        else
134
            cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
135
    }
136
}
137

    
138
/* PC cmos mappings */
139

    
140
#define REG_EQUIPMENT_BYTE          0x14
141

    
142
static int cmos_get_fd_drive_type(int fd0)
143
{
144
    int val;
145

    
146
    switch (fd0) {
147
    case 0:
148
        /* 1.44 Mb 3"5 drive */
149
        val = 4;
150
        break;
151
    case 1:
152
        /* 2.88 Mb 3"5 drive */
153
        val = 5;
154
        break;
155
    case 2:
156
        /* 1.2 Mb 5"5 drive */
157
        val = 2;
158
        break;
159
    default:
160
        val = 0;
161
        break;
162
    }
163
    return val;
164
}
165

    
166
static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
167
{
168
    RTCState *s = rtc_state;
169
    int cylinders, heads, sectors;
170
    bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
171
    rtc_set_memory(s, type_ofs, 47);
172
    rtc_set_memory(s, info_ofs, cylinders);
173
    rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
174
    rtc_set_memory(s, info_ofs + 2, heads);
175
    rtc_set_memory(s, info_ofs + 3, 0xff);
176
    rtc_set_memory(s, info_ofs + 4, 0xff);
177
    rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
178
    rtc_set_memory(s, info_ofs + 6, cylinders);
179
    rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
180
    rtc_set_memory(s, info_ofs + 8, sectors);
181
}
182

    
183
/* convert boot_device letter to something recognizable by the bios */
184
static int boot_device2nibble(char boot_device)
185
{
186
    switch(boot_device) {
187
    case 'a':
188
    case 'b':
189
        return 0x01; /* floppy boot */
190
    case 'c':
191
        return 0x02; /* hard drive boot */
192
    case 'd':
193
        return 0x03; /* CD-ROM boot */
194
    case 'n':
195
        return 0x04; /* Network boot */
196
    }
197
    return 0;
198
}
199

    
200
/* copy/pasted from cmos_init, should be made a general function
201
 and used there as well */
202
static int pc_boot_set(void *opaque, const char *boot_device)
203
{
204
#define PC_MAX_BOOT_DEVICES 3
205
    RTCState *s = (RTCState *)opaque;
206
    int nbds, bds[3] = { 0, };
207
    int i;
208

    
209
    nbds = strlen(boot_device);
210
    if (nbds > PC_MAX_BOOT_DEVICES) {
211
        term_printf("Too many boot devices for PC\n");
212
        return(1);
213
    }
214
    for (i = 0; i < nbds; i++) {
215
        bds[i] = boot_device2nibble(boot_device[i]);
216
        if (bds[i] == 0) {
217
            term_printf("Invalid boot device for PC: '%c'\n",
218
                    boot_device[i]);
219
            return(1);
220
        }
221
    }
222
    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
223
    rtc_set_memory(s, 0x38, (bds[2] << 4));
224
    return(0);
225
}
226

    
227
/* hd_table must contain 4 block drivers */
228
static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
229
                      const char *boot_device, BlockDriverState **hd_table)
230
{
231
    RTCState *s = rtc_state;
232
    int nbds, bds[3] = { 0, };
233
    int val;
234
    int fd0, fd1, nb;
235
    int i;
236

    
237
    /* various important CMOS locations needed by PC/Bochs bios */
238

    
239
    /* memory size */
240
    val = 640; /* base memory in K */
241
    rtc_set_memory(s, 0x15, val);
242
    rtc_set_memory(s, 0x16, val >> 8);
243

    
244
    val = (ram_size / 1024) - 1024;
245
    if (val > 65535)
246
        val = 65535;
247
    rtc_set_memory(s, 0x17, val);
248
    rtc_set_memory(s, 0x18, val >> 8);
249
    rtc_set_memory(s, 0x30, val);
250
    rtc_set_memory(s, 0x31, val >> 8);
251

    
252
    if (above_4g_mem_size) {
253
        rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
254
        rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
255
        rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
256
    }
257

    
258
    if (ram_size > (16 * 1024 * 1024))
259
        val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
260
    else
261
        val = 0;
262
    if (val > 65535)
263
        val = 65535;
264
    rtc_set_memory(s, 0x34, val);
265
    rtc_set_memory(s, 0x35, val >> 8);
266

    
267
    /* set the number of CPU */
268
    rtc_set_memory(s, 0x5f, smp_cpus - 1);
269

    
270
    /* set boot devices, and disable floppy signature check if requested */
271
#define PC_MAX_BOOT_DEVICES 3
272
    nbds = strlen(boot_device);
273
    if (nbds > PC_MAX_BOOT_DEVICES) {
274
        fprintf(stderr, "Too many boot devices for PC\n");
275
        exit(1);
276
    }
277
    for (i = 0; i < nbds; i++) {
278
        bds[i] = boot_device2nibble(boot_device[i]);
279
        if (bds[i] == 0) {
280
            fprintf(stderr, "Invalid boot device for PC: '%c'\n",
281
                    boot_device[i]);
282
            exit(1);
283
        }
284
    }
285
    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
286
    rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ?  0x0 : 0x1));
287

    
288
    /* floppy type */
289

    
290
    fd0 = fdctrl_get_drive_type(floppy_controller, 0);
291
    fd1 = fdctrl_get_drive_type(floppy_controller, 1);
292

    
293
    val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
294
    rtc_set_memory(s, 0x10, val);
295

    
296
    val = 0;
297
    nb = 0;
298
    if (fd0 < 3)
299
        nb++;
300
    if (fd1 < 3)
301
        nb++;
302
    switch (nb) {
303
    case 0:
304
        break;
305
    case 1:
306
        val |= 0x01; /* 1 drive, ready for boot */
307
        break;
308
    case 2:
309
        val |= 0x41; /* 2 drives, ready for boot */
310
        break;
311
    }
312
    val |= 0x02; /* FPU is there */
313
    val |= 0x04; /* PS/2 mouse installed */
314
    rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
315

    
316
    /* hard drives */
317

    
318
    rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
319
    if (hd_table[0])
320
        cmos_init_hd(0x19, 0x1b, hd_table[0]);
321
    if (hd_table[1])
322
        cmos_init_hd(0x1a, 0x24, hd_table[1]);
323

    
324
    val = 0;
325
    for (i = 0; i < 4; i++) {
326
        if (hd_table[i]) {
327
            int cylinders, heads, sectors, translation;
328
            /* NOTE: bdrv_get_geometry_hint() returns the physical
329
                geometry.  It is always such that: 1 <= sects <= 63, 1
330
                <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
331
                geometry can be different if a translation is done. */
332
            translation = bdrv_get_translation_hint(hd_table[i]);
333
            if (translation == BIOS_ATA_TRANSLATION_AUTO) {
334
                bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
335
                if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
336
                    /* No translation. */
337
                    translation = 0;
338
                } else {
339
                    /* LBA translation. */
340
                    translation = 1;
341
                }
342
            } else {
343
                translation--;
344
            }
345
            val |= translation << (i * 2);
346
        }
347
    }
348
    rtc_set_memory(s, 0x39, val);
349
}
350

    
351
void ioport_set_a20(int enable)
352
{
353
    /* XXX: send to all CPUs ? */
354
    cpu_x86_set_a20(first_cpu, enable);
355
}
356

    
357
int ioport_get_a20(void)
358
{
359
    return ((first_cpu->a20_mask >> 20) & 1);
360
}
361

    
362
static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
363
{
364
    ioport_set_a20((val >> 1) & 1);
365
    /* XXX: bit 0 is fast reset */
366
}
367

    
368
static uint32_t ioport92_read(void *opaque, uint32_t addr)
369
{
370
    return ioport_get_a20() << 1;
371
}
372

    
373
/***********************************************************/
374
/* Bochs BIOS debug ports */
375

    
376
static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
377
{
378
    static const char shutdown_str[8] = "Shutdown";
379
    static int shutdown_index = 0;
380

    
381
    switch(addr) {
382
        /* Bochs BIOS messages */
383
    case 0x400:
384
    case 0x401:
385
        fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
386
        exit(1);
387
    case 0x402:
388
    case 0x403:
389
#ifdef DEBUG_BIOS
390
        fprintf(stderr, "%c", val);
391
#endif
392
        break;
393
    case 0x8900:
394
        /* same as Bochs power off */
395
        if (val == shutdown_str[shutdown_index]) {
396
            shutdown_index++;
397
            if (shutdown_index == 8) {
398
                shutdown_index = 0;
399
                qemu_system_shutdown_request();
400
            }
401
        } else {
402
            shutdown_index = 0;
403
        }
404
        break;
405

    
406
        /* LGPL'ed VGA BIOS messages */
407
    case 0x501:
408
    case 0x502:
409
        fprintf(stderr, "VGA BIOS panic, line %d\n", val);
410
        exit(1);
411
    case 0x500:
412
    case 0x503:
413
#ifdef DEBUG_BIOS
414
        fprintf(stderr, "%c", val);
415
#endif
416
        break;
417
    }
418
}
419

    
420
static void bochs_bios_init(void)
421
{
422
    void *fw_cfg;
423

    
424
    register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
425
    register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
426
    register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
427
    register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
428
    register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
429

    
430
    register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
431
    register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
432
    register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
433
    register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
434

    
435
    fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
436
    fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
437
    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
438
}
439

    
440
/* Generate an initial boot sector which sets state and jump to
441
   a specified vector */
442
static void generate_bootsect(uint8_t *option_rom,
443
                              uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
444
{
445
    uint8_t rom[512], *p, *reloc;
446
    uint8_t sum;
447
    int i;
448

    
449
    memset(rom, 0, sizeof(rom));
450

    
451
    p = rom;
452
    /* Make sure we have an option rom signature */
453
    *p++ = 0x55;
454
    *p++ = 0xaa;
455

    
456
    /* ROM size in sectors*/
457
    *p++ = 1;
458

    
459
    /* Hook int19 */
460

    
461
    *p++ = 0x50;                /* push ax */
462
    *p++ = 0x1e;                /* push ds */
463
    *p++ = 0x31; *p++ = 0xc0;        /* xor ax, ax */
464
    *p++ = 0x8e; *p++ = 0xd8;        /* mov ax, ds */
465

    
466
    *p++ = 0xc7; *p++ = 0x06;   /* movvw _start,0x64 */
467
    *p++ = 0x64; *p++ = 0x00;
468
    reloc = p;
469
    *p++ = 0x00; *p++ = 0x00;
470

    
471
    *p++ = 0x8c; *p++ = 0x0e;   /* mov cs,0x66 */
472
    *p++ = 0x66; *p++ = 0x00;
473

    
474
    *p++ = 0x1f;                /* pop ds */
475
    *p++ = 0x58;                /* pop ax */
476
    *p++ = 0xcb;                /* lret */
477
    
478
    /* Actual code */
479
    *reloc = (p - rom);
480

    
481
    *p++ = 0xfa;                /* CLI */
482
    *p++ = 0xfc;                /* CLD */
483

    
484
    for (i = 0; i < 6; i++) {
485
        if (i == 1)                /* Skip CS */
486
            continue;
487

    
488
        *p++ = 0xb8;                /* MOV AX,imm16 */
489
        *p++ = segs[i];
490
        *p++ = segs[i] >> 8;
491
        *p++ = 0x8e;                /* MOV <seg>,AX */
492
        *p++ = 0xc0 + (i << 3);
493
    }
494

    
495
    for (i = 0; i < 8; i++) {
496
        *p++ = 0x66;                /* 32-bit operand size */
497
        *p++ = 0xb8 + i;        /* MOV <reg>,imm32 */
498
        *p++ = gpr[i];
499
        *p++ = gpr[i] >> 8;
500
        *p++ = gpr[i] >> 16;
501
        *p++ = gpr[i] >> 24;
502
    }
503

    
504
    *p++ = 0xea;                /* JMP FAR */
505
    *p++ = ip;                        /* IP */
506
    *p++ = ip >> 8;
507
    *p++ = segs[1];                /* CS */
508
    *p++ = segs[1] >> 8;
509

    
510
    /* sign rom */
511
    sum = 0;
512
    for (i = 0; i < (sizeof(rom) - 1); i++)
513
        sum += rom[i];
514
    rom[sizeof(rom) - 1] = -sum;
515

    
516
    memcpy(option_rom, rom, sizeof(rom));
517
}
518

    
519
static long get_file_size(FILE *f)
520
{
521
    long where, size;
522

    
523
    /* XXX: on Unix systems, using fstat() probably makes more sense */
524

    
525
    where = ftell(f);
526
    fseek(f, 0, SEEK_END);
527
    size = ftell(f);
528
    fseek(f, where, SEEK_SET);
529

    
530
    return size;
531
}
532

    
533
static void load_linux(uint8_t *option_rom,
534
                       const char *kernel_filename,
535
                       const char *initrd_filename,
536
                       const char *kernel_cmdline)
537
{
538
    uint16_t protocol;
539
    uint32_t gpr[8];
540
    uint16_t seg[6];
541
    uint16_t real_seg;
542
    int setup_size, kernel_size, initrd_size, cmdline_size;
543
    uint32_t initrd_max;
544
    uint8_t header[1024];
545
    target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr;
546
    FILE *f, *fi;
547

    
548
    /* Align to 16 bytes as a paranoia measure */
549
    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
550

    
551
    /* load the kernel header */
552
    f = fopen(kernel_filename, "rb");
553
    if (!f || !(kernel_size = get_file_size(f)) ||
554
        fread(header, 1, 1024, f) != 1024) {
555
        fprintf(stderr, "qemu: could not load kernel '%s'\n",
556
                kernel_filename);
557
        exit(1);
558
    }
559

    
560
    /* kernel protocol version */
561
#if 0
562
    fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
563
#endif
564
    if (ldl_p(header+0x202) == 0x53726448)
565
        protocol = lduw_p(header+0x206);
566
    else
567
        protocol = 0;
568

    
569
    if (protocol < 0x200 || !(header[0x211] & 0x01)) {
570
        /* Low kernel */
571
        real_addr    = 0x90000;
572
        cmdline_addr = 0x9a000 - cmdline_size;
573
        prot_addr    = 0x10000;
574
    } else if (protocol < 0x202) {
575
        /* High but ancient kernel */
576
        real_addr    = 0x90000;
577
        cmdline_addr = 0x9a000 - cmdline_size;
578
        prot_addr    = 0x100000;
579
    } else {
580
        /* High and recent kernel */
581
        real_addr    = 0x10000;
582
        cmdline_addr = 0x20000;
583
        prot_addr    = 0x100000;
584
    }
585

    
586
#if 0
587
    fprintf(stderr,
588
            "qemu: real_addr     = 0x" TARGET_FMT_plx "\n"
589
            "qemu: cmdline_addr  = 0x" TARGET_FMT_plx "\n"
590
            "qemu: prot_addr     = 0x" TARGET_FMT_plx "\n",
591
            real_addr,
592
            cmdline_addr,
593
            prot_addr);
594
#endif
595

    
596
    /* highest address for loading the initrd */
597
    if (protocol >= 0x203)
598
        initrd_max = ldl_p(header+0x22c);
599
    else
600
        initrd_max = 0x37ffffff;
601

    
602
    if (initrd_max >= ram_size-ACPI_DATA_SIZE)
603
        initrd_max = ram_size-ACPI_DATA_SIZE-1;
604

    
605
    /* kernel command line */
606
    pstrcpy_targphys(cmdline_addr, 4096, kernel_cmdline);
607

    
608
    if (protocol >= 0x202) {
609
        stl_p(header+0x228, cmdline_addr);
610
    } else {
611
        stw_p(header+0x20, 0xA33F);
612
        stw_p(header+0x22, cmdline_addr-real_addr);
613
    }
614

    
615
    /* loader type */
616
    /* High nybble = B reserved for Qemu; low nybble is revision number.
617
       If this code is substantially changed, you may want to consider
618
       incrementing the revision. */
619
    if (protocol >= 0x200)
620
        header[0x210] = 0xB0;
621

    
622
    /* heap */
623
    if (protocol >= 0x201) {
624
        header[0x211] |= 0x80;        /* CAN_USE_HEAP */
625
        stw_p(header+0x224, cmdline_addr-real_addr-0x200);
626
    }
627

    
628
    /* load initrd */
629
    if (initrd_filename) {
630
        if (protocol < 0x200) {
631
            fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
632
            exit(1);
633
        }
634

    
635
        fi = fopen(initrd_filename, "rb");
636
        if (!fi) {
637
            fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
638
                    initrd_filename);
639
            exit(1);
640
        }
641

    
642
        initrd_size = get_file_size(fi);
643
        initrd_addr = (initrd_max-initrd_size) & ~4095;
644

    
645
        fprintf(stderr, "qemu: loading initrd (%#x bytes) at 0x" TARGET_FMT_plx
646
                "\n", initrd_size, initrd_addr);
647

    
648
        if (!fread_targphys_ok(initrd_addr, initrd_size, fi)) {
649
            fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
650
                    initrd_filename);
651
            exit(1);
652
        }
653
        fclose(fi);
654

    
655
        stl_p(header+0x218, initrd_addr);
656
        stl_p(header+0x21c, initrd_size);
657
    }
658

    
659
    /* store the finalized header and load the rest of the kernel */
660
    cpu_physical_memory_write(real_addr, header, 1024);
661

    
662
    setup_size = header[0x1f1];
663
    if (setup_size == 0)
664
        setup_size = 4;
665

    
666
    setup_size = (setup_size+1)*512;
667
    kernel_size -= setup_size;        /* Size of protected-mode code */
668

    
669
    if (!fread_targphys_ok(real_addr+1024, setup_size-1024, f) ||
670
        !fread_targphys_ok(prot_addr, kernel_size, f)) {
671
        fprintf(stderr, "qemu: read error on kernel '%s'\n",
672
                kernel_filename);
673
        exit(1);
674
    }
675
    fclose(f);
676

    
677
    /* generate bootsector to set up the initial register state */
678
    real_seg = real_addr >> 4;
679
    seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg;
680
    seg[1] = real_seg+0x20;        /* CS */
681
    memset(gpr, 0, sizeof gpr);
682
    gpr[4] = cmdline_addr-real_addr-16;        /* SP (-16 is paranoia) */
683

    
684
    generate_bootsect(option_rom, gpr, seg, 0);
685
}
686

    
687
static void main_cpu_reset(void *opaque)
688
{
689
    CPUState *env = opaque;
690
    cpu_reset(env);
691
}
692

    
693
static const int ide_iobase[2] = { 0x1f0, 0x170 };
694
static const int ide_iobase2[2] = { 0x3f6, 0x376 };
695
static const int ide_irq[2] = { 14, 15 };
696

    
697
#define NE2000_NB_MAX 6
698

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

    
702
static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
703
static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
704

    
705
static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
706
static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
707

    
708
#ifdef HAS_AUDIO
709
static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
710
{
711
    struct soundhw *c;
712
    int audio_enabled = 0;
713

    
714
    for (c = soundhw; !audio_enabled && c->name; ++c) {
715
        audio_enabled = c->enabled;
716
    }
717

    
718
    if (audio_enabled) {
719
        AudioState *s;
720

    
721
        s = AUD_init ();
722
        if (s) {
723
            for (c = soundhw; c->name; ++c) {
724
                if (c->enabled) {
725
                    if (c->isa) {
726
                        c->init.init_isa (s, pic);
727
                    }
728
                    else {
729
                        if (pci_bus) {
730
                            c->init.init_pci (pci_bus, s);
731
                        }
732
                    }
733
                }
734
            }
735
        }
736
    }
737
}
738
#endif
739

    
740
static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
741
{
742
    static int nb_ne2k = 0;
743

    
744
    if (nb_ne2k == NE2000_NB_MAX)
745
        return;
746
    isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd);
747
    nb_ne2k++;
748
}
749

    
750
/* PC hardware initialisation */
751
static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
752
                     const char *boot_device, DisplayState *ds,
753
                     const char *kernel_filename, const char *kernel_cmdline,
754
                     const char *initrd_filename,
755
                     int pci_enabled, const char *cpu_model)
756
{
757
    char buf[1024];
758
    int ret, linux_boot, i;
759
    ram_addr_t ram_addr, vga_ram_addr, bios_offset, vga_bios_offset;
760
    ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
761
    int bios_size, isa_bios_size, vga_bios_size;
762
    PCIBus *pci_bus;
763
    int piix3_devfn = -1;
764
    CPUState *env;
765
    NICInfo *nd;
766
    qemu_irq *cpu_irq;
767
    qemu_irq *i8259;
768
    int index;
769
    BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
770
    BlockDriverState *fd[MAX_FD];
771

    
772
    if (ram_size >= 0xe0000000 ) {
773
        above_4g_mem_size = ram_size - 0xe0000000;
774
        below_4g_mem_size = 0xe0000000;
775
    } else {
776
        below_4g_mem_size = ram_size;
777
    }
778

    
779
    linux_boot = (kernel_filename != NULL);
780

    
781
    /* init CPUs */
782
    if (cpu_model == NULL) {
783
#ifdef TARGET_X86_64
784
        cpu_model = "qemu64";
785
#else
786
        cpu_model = "qemu32";
787
#endif
788
    }
789
    
790
    for(i = 0; i < smp_cpus; i++) {
791
        env = cpu_init(cpu_model);
792
        if (!env) {
793
            fprintf(stderr, "Unable to find x86 CPU definition\n");
794
            exit(1);
795
        }
796
        if (i != 0)
797
            env->halted = 1;
798
        if (smp_cpus > 1) {
799
            /* XXX: enable it in all cases */
800
            env->cpuid_features |= CPUID_APIC;
801
        }
802
        qemu_register_reset(main_cpu_reset, env);
803
        if (pci_enabled) {
804
            apic_init(env);
805
        }
806
    }
807

    
808
    vmport_init();
809

    
810
    /* allocate RAM */
811
    ram_addr = qemu_ram_alloc(0xa0000);
812
    cpu_register_physical_memory(0, 0xa0000, ram_addr);
813

    
814
    /* Allocate, even though we won't register, so we don't break the
815
     * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
816
     * and some bios areas, which will be registered later
817
     */
818
    ram_addr = qemu_ram_alloc(0x100000 - 0xa0000);
819
    ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000);
820
    cpu_register_physical_memory(0x100000,
821
                 below_4g_mem_size - 0x100000,
822
                 ram_addr);
823

    
824
    /* above 4giga memory allocation */
825
    if (above_4g_mem_size > 0) {
826
        ram_addr = qemu_ram_alloc(above_4g_mem_size);
827
        cpu_register_physical_memory(0x100000000ULL,
828
                                     above_4g_mem_size,
829
                                     ram_addr);
830
    }
831

    
832

    
833
    /* allocate VGA RAM */
834
    vga_ram_addr = qemu_ram_alloc(vga_ram_size);
835

    
836
    /* BIOS load */
837
    if (bios_name == NULL)
838
        bios_name = BIOS_FILENAME;
839
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
840
    bios_size = get_image_size(buf);
841
    if (bios_size <= 0 ||
842
        (bios_size % 65536) != 0) {
843
        goto bios_error;
844
    }
845
    bios_offset = qemu_ram_alloc(bios_size);
846
    ret = load_image(buf, phys_ram_base + bios_offset);
847
    if (ret != bios_size) {
848
    bios_error:
849
        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
850
        exit(1);
851
    }
852

    
853
    /* VGA BIOS load */
854
    if (cirrus_vga_enabled) {
855
        snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
856
    } else {
857
        snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
858
    }
859
    vga_bios_size = get_image_size(buf);
860
    if (vga_bios_size <= 0 || vga_bios_size > 65536)
861
        goto vga_bios_error;
862
    vga_bios_offset = qemu_ram_alloc(65536);
863

    
864
    ret = load_image(buf, phys_ram_base + vga_bios_offset);
865
    if (ret != vga_bios_size) {
866
    vga_bios_error:
867
        fprintf(stderr, "qemu: could not load VGA BIOS '%s'\n", buf);
868
        exit(1);
869
    }
870

    
871
    /* setup basic memory access */
872
    cpu_register_physical_memory(0xc0000, 0x10000,
873
                                 vga_bios_offset | IO_MEM_ROM);
874

    
875
    /* map the last 128KB of the BIOS in ISA space */
876
    isa_bios_size = bios_size;
877
    if (isa_bios_size > (128 * 1024))
878
        isa_bios_size = 128 * 1024;
879
    cpu_register_physical_memory(0x100000 - isa_bios_size,
880
                                 isa_bios_size,
881
                                 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
882

    
883
    {
884
        ram_addr_t option_rom_offset;
885
        int size, offset;
886

    
887
        offset = 0;
888
        if (linux_boot) {
889
            option_rom_offset = qemu_ram_alloc(TARGET_PAGE_SIZE);
890
            load_linux(phys_ram_base + option_rom_offset,
891
                       kernel_filename, initrd_filename, kernel_cmdline);
892
            cpu_register_physical_memory(0xd0000, TARGET_PAGE_SIZE,
893
                                         option_rom_offset | IO_MEM_ROM);
894
            offset = TARGET_PAGE_SIZE;
895
        }
896

    
897
        for (i = 0; i < nb_option_roms; i++) {
898
            size = get_image_size(option_rom[i]);
899
            if (size < 0) {
900
                fprintf(stderr, "Could not load option rom '%s'\n",
901
                        option_rom[i]);
902
                exit(1);
903
            }
904
            if (size > (0x10000 - offset))
905
                goto option_rom_error;
906
            option_rom_offset = qemu_ram_alloc(size);
907
            ret = load_image(option_rom[i], phys_ram_base + option_rom_offset);
908
            if (ret != size) {
909
            option_rom_error:
910
                fprintf(stderr, "Too many option ROMS\n");
911
                exit(1);
912
            }
913
            size = (size + 4095) & ~4095;
914
            cpu_register_physical_memory(0xd0000 + offset,
915
                                         size, option_rom_offset | IO_MEM_ROM);
916
            offset += size;
917
        }
918
    }
919

    
920
    /* map all the bios at the top of memory */
921
    cpu_register_physical_memory((uint32_t)(-bios_size),
922
                                 bios_size, bios_offset | IO_MEM_ROM);
923

    
924
    bochs_bios_init();
925

    
926
    cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
927
    i8259 = i8259_init(cpu_irq[0]);
928
    ferr_irq = i8259[13];
929

    
930
    if (pci_enabled) {
931
        pci_bus = i440fx_init(&i440fx_state, i8259);
932
        piix3_devfn = piix3_init(pci_bus, -1);
933
    } else {
934
        pci_bus = NULL;
935
    }
936

    
937
    /* init basic PC hardware */
938
    register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
939

    
940
    register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
941

    
942
    if (cirrus_vga_enabled) {
943
        if (pci_enabled) {
944
            pci_cirrus_vga_init(pci_bus,
945
                                ds, phys_ram_base + vga_ram_addr,
946
                                vga_ram_addr, vga_ram_size);
947
        } else {
948
            isa_cirrus_vga_init(ds, phys_ram_base + vga_ram_addr,
949
                                vga_ram_addr, vga_ram_size);
950
        }
951
    } else if (vmsvga_enabled) {
952
        if (pci_enabled)
953
            pci_vmsvga_init(pci_bus, ds, phys_ram_base + vga_ram_addr,
954
                            vga_ram_addr, vga_ram_size);
955
        else
956
            fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
957
    } else {
958
        if (pci_enabled) {
959
            pci_vga_init(pci_bus, ds, phys_ram_base + vga_ram_addr,
960
                         vga_ram_addr, vga_ram_size, 0, 0);
961
        } else {
962
            isa_vga_init(ds, phys_ram_base + vga_ram_addr,
963
                         vga_ram_addr, vga_ram_size);
964
        }
965
    }
966

    
967
    rtc_state = rtc_init(0x70, i8259[8]);
968

    
969
    qemu_register_boot_set(pc_boot_set, rtc_state);
970

    
971
    register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
972
    register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
973

    
974
    if (pci_enabled) {
975
        ioapic = ioapic_init();
976
    }
977
    pit = pit_init(0x40, i8259[0]);
978
    pcspk_init(pit);
979
    if (pci_enabled) {
980
        pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
981
    }
982

    
983
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
984
        if (serial_hds[i]) {
985
            serial_init(serial_io[i], i8259[serial_irq[i]], 115200,
986
                        serial_hds[i]);
987
        }
988
    }
989

    
990
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
991
        if (parallel_hds[i]) {
992
            parallel_init(parallel_io[i], i8259[parallel_irq[i]],
993
                          parallel_hds[i]);
994
        }
995
    }
996

    
997
    for(i = 0; i < nb_nics; i++) {
998
        nd = &nd_table[i];
999
        if (!nd->model) {
1000
            if (pci_enabled) {
1001
                nd->model = "ne2k_pci";
1002
            } else {
1003
                nd->model = "ne2k_isa";
1004
            }
1005
        }
1006
        if (strcmp(nd->model, "ne2k_isa") == 0) {
1007
            pc_init_ne2k_isa(nd, i8259);
1008
        } else if (pci_enabled) {
1009
            if (strcmp(nd->model, "?") == 0)
1010
                fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n");
1011
            pci_nic_init(pci_bus, nd, -1);
1012
        } else if (strcmp(nd->model, "?") == 0) {
1013
            fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n");
1014
            exit(1);
1015
        } else {
1016
            fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
1017
            exit(1);
1018
        }
1019
    }
1020

    
1021
    if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
1022
        fprintf(stderr, "qemu: too many IDE bus\n");
1023
        exit(1);
1024
    }
1025

    
1026
    for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
1027
        index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
1028
        if (index != -1)
1029
            hd[i] = drives_table[index].bdrv;
1030
        else
1031
            hd[i] = NULL;
1032
    }
1033

    
1034
    if (pci_enabled) {
1035
        pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, i8259);
1036
    } else {
1037
        for(i = 0; i < MAX_IDE_BUS; i++) {
1038
            isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
1039
                         hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
1040
        }
1041
    }
1042

    
1043
    i8042_init(i8259[1], i8259[12], 0x60);
1044
    DMA_init(0);
1045
#ifdef HAS_AUDIO
1046
    audio_init(pci_enabled ? pci_bus : NULL, i8259);
1047
#endif
1048

    
1049
    for(i = 0; i < MAX_FD; i++) {
1050
        index = drive_get_index(IF_FLOPPY, 0, i);
1051
        if (index != -1)
1052
            fd[i] = drives_table[index].bdrv;
1053
        else
1054
            fd[i] = NULL;
1055
    }
1056
    floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
1057

    
1058
    cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
1059

    
1060
    if (pci_enabled && usb_enabled) {
1061
        usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
1062
    }
1063

    
1064
    if (pci_enabled && acpi_enabled) {
1065
        uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
1066
        i2c_bus *smbus;
1067

    
1068
        /* TODO: Populate SPD eeprom data.  */
1069
        smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]);
1070
        for (i = 0; i < 8; i++) {
1071
            smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256));
1072
        }
1073
    }
1074

    
1075
    if (i440fx_state) {
1076
        i440fx_init_memory_mappings(i440fx_state);
1077
    }
1078

    
1079
    if (pci_enabled) {
1080
        int max_bus;
1081
        int bus, unit;
1082
        void *scsi;
1083

    
1084
        max_bus = drive_get_max_bus(IF_SCSI);
1085

    
1086
        for (bus = 0; bus <= max_bus; bus++) {
1087
            scsi = lsi_scsi_init(pci_bus, -1);
1088
            for (unit = 0; unit < LSI_MAX_DEVS; unit++) {
1089
                index = drive_get_index(IF_SCSI, bus, unit);
1090
                if (index == -1)
1091
                    continue;
1092
                lsi_scsi_attach(scsi, drives_table[index].bdrv, unit);
1093
            }
1094
        }
1095
    }
1096

    
1097
    /* Add virtio block devices */
1098
    if (pci_enabled) {
1099
        int index;
1100
        int unit_id = 0;
1101

    
1102
        while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
1103
            virtio_blk_init(pci_bus, 0x1AF4, 0x1001,
1104
                            drives_table[index].bdrv);
1105
            unit_id++;
1106
        }
1107
    }
1108
}
1109

    
1110
static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size,
1111
                        const char *boot_device, DisplayState *ds,
1112
                        const char *kernel_filename,
1113
                        const char *kernel_cmdline,
1114
                        const char *initrd_filename,
1115
                        const char *cpu_model)
1116
{
1117
    pc_init1(ram_size, vga_ram_size, boot_device, ds,
1118
             kernel_filename, kernel_cmdline,
1119
             initrd_filename, 1, cpu_model);
1120
}
1121

    
1122
static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size,
1123
                        const char *boot_device, DisplayState *ds,
1124
                        const char *kernel_filename,
1125
                        const char *kernel_cmdline,
1126
                        const char *initrd_filename,
1127
                        const char *cpu_model)
1128
{
1129
    pc_init1(ram_size, vga_ram_size, boot_device, ds,
1130
             kernel_filename, kernel_cmdline,
1131
             initrd_filename, 0, cpu_model);
1132
}
1133

    
1134
QEMUMachine pc_machine = {
1135
    .name = "pc",
1136
    .desc = "Standard PC",
1137
    .init = pc_init_pci,
1138
    .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE,
1139
    .max_cpus = 255,
1140
};
1141

    
1142
QEMUMachine isapc_machine = {
1143
    .name = "isapc",
1144
    .desc = "ISA-only PC",
1145
    .init = pc_init_isa,
1146
    .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE,
1147
    .max_cpus = 1,
1148
};