Statistics
| Branch: | Revision:

root / hw / pc.c @ 7ffa4767

History | View | Annotate | Download (32.3 kB)

1 80cabfad bellard
/*
2 80cabfad bellard
 * QEMU PC System Emulator
3 5fafdf24 ths
 *
4 80cabfad bellard
 * Copyright (c) 2003-2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 80cabfad bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 80cabfad bellard
 * of this software and associated documentation files (the "Software"), to deal
8 80cabfad bellard
 * in the Software without restriction, including without limitation the rights
9 80cabfad bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 80cabfad bellard
 * copies of the Software, and to permit persons to whom the Software is
11 80cabfad bellard
 * furnished to do so, subject to the following conditions:
12 80cabfad bellard
 *
13 80cabfad bellard
 * The above copyright notice and this permission notice shall be included in
14 80cabfad bellard
 * all copies or substantial portions of the Software.
15 80cabfad bellard
 *
16 80cabfad bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 80cabfad bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 80cabfad bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 80cabfad bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 80cabfad bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 80cabfad bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 80cabfad bellard
 * THE SOFTWARE.
23 80cabfad bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "pc.h"
26 87ecb68b pbrook
#include "fdc.h"
27 87ecb68b pbrook
#include "pci.h"
28 87ecb68b pbrook
#include "block.h"
29 87ecb68b pbrook
#include "sysemu.h"
30 87ecb68b pbrook
#include "audio/audio.h"
31 87ecb68b pbrook
#include "net.h"
32 87ecb68b pbrook
#include "smbus.h"
33 87ecb68b pbrook
#include "boards.h"
34 376253ec aliguori
#include "monitor.h"
35 3cce6243 blueswir1
#include "fw_cfg.h"
36 6e02c38d aliguori
#include "virtio-blk.h"
37 bd322087 aliguori
#include "virtio-balloon.h"
38 a2fa19f9 aliguori
#include "virtio-console.h"
39 16b29ae1 aliguori
#include "hpet_emul.h"
40 80cabfad bellard
41 b41a2cd1 bellard
/* output Bochs bios info messages */
42 b41a2cd1 bellard
//#define DEBUG_BIOS
43 b41a2cd1 bellard
44 80cabfad bellard
#define BIOS_FILENAME "bios.bin"
45 80cabfad bellard
#define VGABIOS_FILENAME "vgabios.bin"
46 de9258a8 bellard
#define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
47 80cabfad bellard
48 7fb4fdcf balrog
#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
49 7fb4fdcf balrog
50 a80274c3 pbrook
/* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
51 a80274c3 pbrook
#define ACPI_DATA_SIZE       0x10000
52 3cce6243 blueswir1
#define BIOS_CFG_IOPORT 0x510
53 8a92ea2f aliguori
#define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
54 80cabfad bellard
55 e4bcb14c ths
#define MAX_IDE_BUS 2
56 e4bcb14c ths
57 baca51fa bellard
static fdctrl_t *floppy_controller;
58 b0a21b53 bellard
static RTCState *rtc_state;
59 ec844b96 bellard
static PITState *pit;
60 d592d303 bellard
static IOAPICState *ioapic;
61 a5954d5c bellard
static PCIDevice *i440fx_state;
62 80cabfad bellard
63 b41a2cd1 bellard
static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
64 80cabfad bellard
{
65 80cabfad bellard
}
66 80cabfad bellard
67 f929aad6 bellard
/* MSDOS compatibility mode FPU exception support */
68 d537cf6c pbrook
static qemu_irq ferr_irq;
69 f929aad6 bellard
/* XXX: add IGNNE support */
70 f929aad6 bellard
void cpu_set_ferr(CPUX86State *s)
71 f929aad6 bellard
{
72 d537cf6c pbrook
    qemu_irq_raise(ferr_irq);
73 f929aad6 bellard
}
74 f929aad6 bellard
75 f929aad6 bellard
static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
76 f929aad6 bellard
{
77 d537cf6c pbrook
    qemu_irq_lower(ferr_irq);
78 f929aad6 bellard
}
79 f929aad6 bellard
80 28ab0e2e bellard
/* TSC handling */
81 28ab0e2e bellard
uint64_t cpu_get_tsc(CPUX86State *env)
82 28ab0e2e bellard
{
83 1dce7c3c bellard
    /* Note: when using kqemu, it is more logical to return the host TSC
84 1dce7c3c bellard
       because kqemu does not trap the RDTSC instruction for
85 1dce7c3c bellard
       performance reasons */
86 eb38c52c blueswir1
#ifdef USE_KQEMU
87 1dce7c3c bellard
    if (env->kqemu_enabled) {
88 1dce7c3c bellard
        return cpu_get_real_ticks();
89 5fafdf24 ths
    } else
90 1dce7c3c bellard
#endif
91 1dce7c3c bellard
    {
92 1dce7c3c bellard
        return cpu_get_ticks();
93 1dce7c3c bellard
    }
94 28ab0e2e bellard
}
95 28ab0e2e bellard
96 a5954d5c bellard
/* SMM support */
97 a5954d5c bellard
void cpu_smm_update(CPUState *env)
98 a5954d5c bellard
{
99 a5954d5c bellard
    if (i440fx_state && env == first_cpu)
100 a5954d5c bellard
        i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
101 a5954d5c bellard
}
102 a5954d5c bellard
103 a5954d5c bellard
104 3de388f6 bellard
/* IRQ handling */
105 3de388f6 bellard
int cpu_get_pic_interrupt(CPUState *env)
106 3de388f6 bellard
{
107 3de388f6 bellard
    int intno;
108 3de388f6 bellard
109 3de388f6 bellard
    intno = apic_get_interrupt(env);
110 3de388f6 bellard
    if (intno >= 0) {
111 3de388f6 bellard
        /* set irq request if a PIC irq is still pending */
112 3de388f6 bellard
        /* XXX: improve that */
113 5fafdf24 ths
        pic_update_irq(isa_pic);
114 3de388f6 bellard
        return intno;
115 3de388f6 bellard
    }
116 3de388f6 bellard
    /* read the irq from the PIC */
117 0e21e12b ths
    if (!apic_accept_pic_intr(env))
118 0e21e12b ths
        return -1;
119 0e21e12b ths
120 3de388f6 bellard
    intno = pic_read_irq(isa_pic);
121 3de388f6 bellard
    return intno;
122 3de388f6 bellard
}
123 3de388f6 bellard
124 d537cf6c pbrook
static void pic_irq_request(void *opaque, int irq, int level)
125 3de388f6 bellard
{
126 a5b38b51 aurel32
    CPUState *env = first_cpu;
127 a5b38b51 aurel32
128 d5529471 aurel32
    if (env->apic_state) {
129 d5529471 aurel32
        while (env) {
130 d5529471 aurel32
            if (apic_accept_pic_intr(env))
131 1a7de94a aurel32
                apic_deliver_pic_intr(env, level);
132 d5529471 aurel32
            env = env->next_cpu;
133 d5529471 aurel32
        }
134 d5529471 aurel32
    } else {
135 b614106a aurel32
        if (level)
136 b614106a aurel32
            cpu_interrupt(env, CPU_INTERRUPT_HARD);
137 b614106a aurel32
        else
138 b614106a aurel32
            cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
139 a5b38b51 aurel32
    }
140 3de388f6 bellard
}
141 3de388f6 bellard
142 b0a21b53 bellard
/* PC cmos mappings */
143 b0a21b53 bellard
144 80cabfad bellard
#define REG_EQUIPMENT_BYTE          0x14
145 80cabfad bellard
146 777428f2 bellard
static int cmos_get_fd_drive_type(int fd0)
147 777428f2 bellard
{
148 777428f2 bellard
    int val;
149 777428f2 bellard
150 777428f2 bellard
    switch (fd0) {
151 777428f2 bellard
    case 0:
152 777428f2 bellard
        /* 1.44 Mb 3"5 drive */
153 777428f2 bellard
        val = 4;
154 777428f2 bellard
        break;
155 777428f2 bellard
    case 1:
156 777428f2 bellard
        /* 2.88 Mb 3"5 drive */
157 777428f2 bellard
        val = 5;
158 777428f2 bellard
        break;
159 777428f2 bellard
    case 2:
160 777428f2 bellard
        /* 1.2 Mb 5"5 drive */
161 777428f2 bellard
        val = 2;
162 777428f2 bellard
        break;
163 777428f2 bellard
    default:
164 777428f2 bellard
        val = 0;
165 777428f2 bellard
        break;
166 777428f2 bellard
    }
167 777428f2 bellard
    return val;
168 777428f2 bellard
}
169 777428f2 bellard
170 5fafdf24 ths
static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
171 ba6c2377 bellard
{
172 ba6c2377 bellard
    RTCState *s = rtc_state;
173 ba6c2377 bellard
    int cylinders, heads, sectors;
174 ba6c2377 bellard
    bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
175 ba6c2377 bellard
    rtc_set_memory(s, type_ofs, 47);
176 ba6c2377 bellard
    rtc_set_memory(s, info_ofs, cylinders);
177 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
178 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 2, heads);
179 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 3, 0xff);
180 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 4, 0xff);
181 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
182 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 6, cylinders);
183 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
184 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 8, sectors);
185 ba6c2377 bellard
}
186 ba6c2377 bellard
187 6ac0e82d balrog
/* convert boot_device letter to something recognizable by the bios */
188 6ac0e82d balrog
static int boot_device2nibble(char boot_device)
189 6ac0e82d balrog
{
190 6ac0e82d balrog
    switch(boot_device) {
191 6ac0e82d balrog
    case 'a':
192 6ac0e82d balrog
    case 'b':
193 6ac0e82d balrog
        return 0x01; /* floppy boot */
194 6ac0e82d balrog
    case 'c':
195 6ac0e82d balrog
        return 0x02; /* hard drive boot */
196 6ac0e82d balrog
    case 'd':
197 6ac0e82d balrog
        return 0x03; /* CD-ROM boot */
198 6ac0e82d balrog
    case 'n':
199 6ac0e82d balrog
        return 0x04; /* Network boot */
200 6ac0e82d balrog
    }
201 6ac0e82d balrog
    return 0;
202 6ac0e82d balrog
}
203 6ac0e82d balrog
204 0ecdffbb aurel32
/* copy/pasted from cmos_init, should be made a general function
205 0ecdffbb aurel32
 and used there as well */
206 3b4366de blueswir1
static int pc_boot_set(void *opaque, const char *boot_device)
207 0ecdffbb aurel32
{
208 376253ec aliguori
    Monitor *mon = cur_mon;
209 0ecdffbb aurel32
#define PC_MAX_BOOT_DEVICES 3
210 3b4366de blueswir1
    RTCState *s = (RTCState *)opaque;
211 0ecdffbb aurel32
    int nbds, bds[3] = { 0, };
212 0ecdffbb aurel32
    int i;
213 0ecdffbb aurel32
214 0ecdffbb aurel32
    nbds = strlen(boot_device);
215 0ecdffbb aurel32
    if (nbds > PC_MAX_BOOT_DEVICES) {
216 376253ec aliguori
        monitor_printf(mon, "Too many boot devices for PC\n");
217 0ecdffbb aurel32
        return(1);
218 0ecdffbb aurel32
    }
219 0ecdffbb aurel32
    for (i = 0; i < nbds; i++) {
220 0ecdffbb aurel32
        bds[i] = boot_device2nibble(boot_device[i]);
221 0ecdffbb aurel32
        if (bds[i] == 0) {
222 376253ec aliguori
            monitor_printf(mon, "Invalid boot device for PC: '%c'\n",
223 376253ec aliguori
                           boot_device[i]);
224 0ecdffbb aurel32
            return(1);
225 0ecdffbb aurel32
        }
226 0ecdffbb aurel32
    }
227 0ecdffbb aurel32
    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
228 0ecdffbb aurel32
    rtc_set_memory(s, 0x38, (bds[2] << 4));
229 0ecdffbb aurel32
    return(0);
230 0ecdffbb aurel32
}
231 0ecdffbb aurel32
232 ba6c2377 bellard
/* hd_table must contain 4 block drivers */
233 00f82b8a aurel32
static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
234 00f82b8a aurel32
                      const char *boot_device, BlockDriverState **hd_table)
235 80cabfad bellard
{
236 b0a21b53 bellard
    RTCState *s = rtc_state;
237 28c5af54 j_mayer
    int nbds, bds[3] = { 0, };
238 80cabfad bellard
    int val;
239 b41a2cd1 bellard
    int fd0, fd1, nb;
240 ba6c2377 bellard
    int i;
241 b0a21b53 bellard
242 b0a21b53 bellard
    /* various important CMOS locations needed by PC/Bochs bios */
243 80cabfad bellard
244 80cabfad bellard
    /* memory size */
245 333190eb bellard
    val = 640; /* base memory in K */
246 333190eb bellard
    rtc_set_memory(s, 0x15, val);
247 333190eb bellard
    rtc_set_memory(s, 0x16, val >> 8);
248 333190eb bellard
249 80cabfad bellard
    val = (ram_size / 1024) - 1024;
250 80cabfad bellard
    if (val > 65535)
251 80cabfad bellard
        val = 65535;
252 b0a21b53 bellard
    rtc_set_memory(s, 0x17, val);
253 b0a21b53 bellard
    rtc_set_memory(s, 0x18, val >> 8);
254 b0a21b53 bellard
    rtc_set_memory(s, 0x30, val);
255 b0a21b53 bellard
    rtc_set_memory(s, 0x31, val >> 8);
256 80cabfad bellard
257 00f82b8a aurel32
    if (above_4g_mem_size) {
258 00f82b8a aurel32
        rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
259 00f82b8a aurel32
        rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
260 00f82b8a aurel32
        rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
261 00f82b8a aurel32
    }
262 00f82b8a aurel32
263 9da98861 bellard
    if (ram_size > (16 * 1024 * 1024))
264 9da98861 bellard
        val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
265 9da98861 bellard
    else
266 9da98861 bellard
        val = 0;
267 80cabfad bellard
    if (val > 65535)
268 80cabfad bellard
        val = 65535;
269 b0a21b53 bellard
    rtc_set_memory(s, 0x34, val);
270 b0a21b53 bellard
    rtc_set_memory(s, 0x35, val >> 8);
271 3b46e624 ths
272 298e01b6 aurel32
    /* set the number of CPU */
273 298e01b6 aurel32
    rtc_set_memory(s, 0x5f, smp_cpus - 1);
274 298e01b6 aurel32
275 6ac0e82d balrog
    /* set boot devices, and disable floppy signature check if requested */
276 28c5af54 j_mayer
#define PC_MAX_BOOT_DEVICES 3
277 28c5af54 j_mayer
    nbds = strlen(boot_device);
278 28c5af54 j_mayer
    if (nbds > PC_MAX_BOOT_DEVICES) {
279 28c5af54 j_mayer
        fprintf(stderr, "Too many boot devices for PC\n");
280 28c5af54 j_mayer
        exit(1);
281 28c5af54 j_mayer
    }
282 28c5af54 j_mayer
    for (i = 0; i < nbds; i++) {
283 28c5af54 j_mayer
        bds[i] = boot_device2nibble(boot_device[i]);
284 28c5af54 j_mayer
        if (bds[i] == 0) {
285 28c5af54 j_mayer
            fprintf(stderr, "Invalid boot device for PC: '%c'\n",
286 28c5af54 j_mayer
                    boot_device[i]);
287 28c5af54 j_mayer
            exit(1);
288 28c5af54 j_mayer
        }
289 28c5af54 j_mayer
    }
290 28c5af54 j_mayer
    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
291 28c5af54 j_mayer
    rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ?  0x0 : 0x1));
292 80cabfad bellard
293 b41a2cd1 bellard
    /* floppy type */
294 b41a2cd1 bellard
295 baca51fa bellard
    fd0 = fdctrl_get_drive_type(floppy_controller, 0);
296 baca51fa bellard
    fd1 = fdctrl_get_drive_type(floppy_controller, 1);
297 80cabfad bellard
298 777428f2 bellard
    val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
299 b0a21b53 bellard
    rtc_set_memory(s, 0x10, val);
300 3b46e624 ths
301 b0a21b53 bellard
    val = 0;
302 b41a2cd1 bellard
    nb = 0;
303 80cabfad bellard
    if (fd0 < 3)
304 80cabfad bellard
        nb++;
305 80cabfad bellard
    if (fd1 < 3)
306 80cabfad bellard
        nb++;
307 80cabfad bellard
    switch (nb) {
308 80cabfad bellard
    case 0:
309 80cabfad bellard
        break;
310 80cabfad bellard
    case 1:
311 b0a21b53 bellard
        val |= 0x01; /* 1 drive, ready for boot */
312 80cabfad bellard
        break;
313 80cabfad bellard
    case 2:
314 b0a21b53 bellard
        val |= 0x41; /* 2 drives, ready for boot */
315 80cabfad bellard
        break;
316 80cabfad bellard
    }
317 b0a21b53 bellard
    val |= 0x02; /* FPU is there */
318 b0a21b53 bellard
    val |= 0x04; /* PS/2 mouse installed */
319 b0a21b53 bellard
    rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
320 b0a21b53 bellard
321 ba6c2377 bellard
    /* hard drives */
322 ba6c2377 bellard
323 ba6c2377 bellard
    rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
324 ba6c2377 bellard
    if (hd_table[0])
325 ba6c2377 bellard
        cmos_init_hd(0x19, 0x1b, hd_table[0]);
326 5fafdf24 ths
    if (hd_table[1])
327 ba6c2377 bellard
        cmos_init_hd(0x1a, 0x24, hd_table[1]);
328 ba6c2377 bellard
329 ba6c2377 bellard
    val = 0;
330 40b6ecc6 bellard
    for (i = 0; i < 4; i++) {
331 ba6c2377 bellard
        if (hd_table[i]) {
332 46d4767d bellard
            int cylinders, heads, sectors, translation;
333 46d4767d bellard
            /* NOTE: bdrv_get_geometry_hint() returns the physical
334 46d4767d bellard
                geometry.  It is always such that: 1 <= sects <= 63, 1
335 46d4767d bellard
                <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
336 46d4767d bellard
                geometry can be different if a translation is done. */
337 46d4767d bellard
            translation = bdrv_get_translation_hint(hd_table[i]);
338 46d4767d bellard
            if (translation == BIOS_ATA_TRANSLATION_AUTO) {
339 46d4767d bellard
                bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
340 46d4767d bellard
                if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
341 46d4767d bellard
                    /* No translation. */
342 46d4767d bellard
                    translation = 0;
343 46d4767d bellard
                } else {
344 46d4767d bellard
                    /* LBA translation. */
345 46d4767d bellard
                    translation = 1;
346 46d4767d bellard
                }
347 40b6ecc6 bellard
            } else {
348 46d4767d bellard
                translation--;
349 ba6c2377 bellard
            }
350 ba6c2377 bellard
            val |= translation << (i * 2);
351 ba6c2377 bellard
        }
352 40b6ecc6 bellard
    }
353 ba6c2377 bellard
    rtc_set_memory(s, 0x39, val);
354 80cabfad bellard
}
355 80cabfad bellard
356 59b8ad81 bellard
void ioport_set_a20(int enable)
357 59b8ad81 bellard
{
358 59b8ad81 bellard
    /* XXX: send to all CPUs ? */
359 59b8ad81 bellard
    cpu_x86_set_a20(first_cpu, enable);
360 59b8ad81 bellard
}
361 59b8ad81 bellard
362 59b8ad81 bellard
int ioport_get_a20(void)
363 59b8ad81 bellard
{
364 59b8ad81 bellard
    return ((first_cpu->a20_mask >> 20) & 1);
365 59b8ad81 bellard
}
366 59b8ad81 bellard
367 e1a23744 bellard
static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
368 e1a23744 bellard
{
369 59b8ad81 bellard
    ioport_set_a20((val >> 1) & 1);
370 e1a23744 bellard
    /* XXX: bit 0 is fast reset */
371 e1a23744 bellard
}
372 e1a23744 bellard
373 e1a23744 bellard
static uint32_t ioport92_read(void *opaque, uint32_t addr)
374 e1a23744 bellard
{
375 59b8ad81 bellard
    return ioport_get_a20() << 1;
376 e1a23744 bellard
}
377 e1a23744 bellard
378 80cabfad bellard
/***********************************************************/
379 80cabfad bellard
/* Bochs BIOS debug ports */
380 80cabfad bellard
381 9596ebb7 pbrook
static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
382 80cabfad bellard
{
383 a2f659ee bellard
    static const char shutdown_str[8] = "Shutdown";
384 a2f659ee bellard
    static int shutdown_index = 0;
385 3b46e624 ths
386 80cabfad bellard
    switch(addr) {
387 80cabfad bellard
        /* Bochs BIOS messages */
388 80cabfad bellard
    case 0x400:
389 80cabfad bellard
    case 0x401:
390 80cabfad bellard
        fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
391 80cabfad bellard
        exit(1);
392 80cabfad bellard
    case 0x402:
393 80cabfad bellard
    case 0x403:
394 80cabfad bellard
#ifdef DEBUG_BIOS
395 80cabfad bellard
        fprintf(stderr, "%c", val);
396 80cabfad bellard
#endif
397 80cabfad bellard
        break;
398 a2f659ee bellard
    case 0x8900:
399 a2f659ee bellard
        /* same as Bochs power off */
400 a2f659ee bellard
        if (val == shutdown_str[shutdown_index]) {
401 a2f659ee bellard
            shutdown_index++;
402 a2f659ee bellard
            if (shutdown_index == 8) {
403 a2f659ee bellard
                shutdown_index = 0;
404 a2f659ee bellard
                qemu_system_shutdown_request();
405 a2f659ee bellard
            }
406 a2f659ee bellard
        } else {
407 a2f659ee bellard
            shutdown_index = 0;
408 a2f659ee bellard
        }
409 a2f659ee bellard
        break;
410 80cabfad bellard
411 80cabfad bellard
        /* LGPL'ed VGA BIOS messages */
412 80cabfad bellard
    case 0x501:
413 80cabfad bellard
    case 0x502:
414 80cabfad bellard
        fprintf(stderr, "VGA BIOS panic, line %d\n", val);
415 80cabfad bellard
        exit(1);
416 80cabfad bellard
    case 0x500:
417 80cabfad bellard
    case 0x503:
418 80cabfad bellard
#ifdef DEBUG_BIOS
419 80cabfad bellard
        fprintf(stderr, "%c", val);
420 80cabfad bellard
#endif
421 80cabfad bellard
        break;
422 80cabfad bellard
    }
423 80cabfad bellard
}
424 80cabfad bellard
425 9596ebb7 pbrook
static void bochs_bios_init(void)
426 80cabfad bellard
{
427 3cce6243 blueswir1
    void *fw_cfg;
428 3cce6243 blueswir1
429 b41a2cd1 bellard
    register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
430 b41a2cd1 bellard
    register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
431 b41a2cd1 bellard
    register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
432 b41a2cd1 bellard
    register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
433 a2f659ee bellard
    register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
434 b41a2cd1 bellard
435 b41a2cd1 bellard
    register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
436 b41a2cd1 bellard
    register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
437 b41a2cd1 bellard
    register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
438 b41a2cd1 bellard
    register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
439 3cce6243 blueswir1
440 3cce6243 blueswir1
    fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
441 3cce6243 blueswir1
    fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
442 905fdcb5 blueswir1
    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
443 80deece2 blueswir1
    fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
444 80deece2 blueswir1
                     acpi_tables_len);
445 80cabfad bellard
}
446 80cabfad bellard
447 642a4f96 ths
/* Generate an initial boot sector which sets state and jump to
448 642a4f96 ths
   a specified vector */
449 7ffa4767 pbrook
static void generate_bootsect(target_phys_addr_t option_rom,
450 4fc9af53 aliguori
                              uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
451 642a4f96 ths
{
452 4fc9af53 aliguori
    uint8_t rom[512], *p, *reloc;
453 4fc9af53 aliguori
    uint8_t sum;
454 642a4f96 ths
    int i;
455 642a4f96 ths
456 4fc9af53 aliguori
    memset(rom, 0, sizeof(rom));
457 4fc9af53 aliguori
458 4fc9af53 aliguori
    p = rom;
459 4fc9af53 aliguori
    /* Make sure we have an option rom signature */
460 4fc9af53 aliguori
    *p++ = 0x55;
461 4fc9af53 aliguori
    *p++ = 0xaa;
462 642a4f96 ths
463 4fc9af53 aliguori
    /* ROM size in sectors*/
464 4fc9af53 aliguori
    *p++ = 1;
465 642a4f96 ths
466 4fc9af53 aliguori
    /* Hook int19 */
467 642a4f96 ths
468 4fc9af53 aliguori
    *p++ = 0x50;                /* push ax */
469 4fc9af53 aliguori
    *p++ = 0x1e;                /* push ds */
470 4fc9af53 aliguori
    *p++ = 0x31; *p++ = 0xc0;        /* xor ax, ax */
471 4fc9af53 aliguori
    *p++ = 0x8e; *p++ = 0xd8;        /* mov ax, ds */
472 642a4f96 ths
473 4fc9af53 aliguori
    *p++ = 0xc7; *p++ = 0x06;   /* movvw _start,0x64 */
474 4fc9af53 aliguori
    *p++ = 0x64; *p++ = 0x00;
475 4fc9af53 aliguori
    reloc = p;
476 4fc9af53 aliguori
    *p++ = 0x00; *p++ = 0x00;
477 4fc9af53 aliguori
478 4fc9af53 aliguori
    *p++ = 0x8c; *p++ = 0x0e;   /* mov cs,0x66 */
479 4fc9af53 aliguori
    *p++ = 0x66; *p++ = 0x00;
480 4fc9af53 aliguori
481 4fc9af53 aliguori
    *p++ = 0x1f;                /* pop ds */
482 4fc9af53 aliguori
    *p++ = 0x58;                /* pop ax */
483 4fc9af53 aliguori
    *p++ = 0xcb;                /* lret */
484 4fc9af53 aliguori
    
485 642a4f96 ths
    /* Actual code */
486 4fc9af53 aliguori
    *reloc = (p - rom);
487 4fc9af53 aliguori
488 642a4f96 ths
    *p++ = 0xfa;                /* CLI */
489 642a4f96 ths
    *p++ = 0xfc;                /* CLD */
490 642a4f96 ths
491 642a4f96 ths
    for (i = 0; i < 6; i++) {
492 642a4f96 ths
        if (i == 1)                /* Skip CS */
493 642a4f96 ths
            continue;
494 642a4f96 ths
495 642a4f96 ths
        *p++ = 0xb8;                /* MOV AX,imm16 */
496 642a4f96 ths
        *p++ = segs[i];
497 642a4f96 ths
        *p++ = segs[i] >> 8;
498 642a4f96 ths
        *p++ = 0x8e;                /* MOV <seg>,AX */
499 642a4f96 ths
        *p++ = 0xc0 + (i << 3);
500 642a4f96 ths
    }
501 642a4f96 ths
502 642a4f96 ths
    for (i = 0; i < 8; i++) {
503 642a4f96 ths
        *p++ = 0x66;                /* 32-bit operand size */
504 642a4f96 ths
        *p++ = 0xb8 + i;        /* MOV <reg>,imm32 */
505 642a4f96 ths
        *p++ = gpr[i];
506 642a4f96 ths
        *p++ = gpr[i] >> 8;
507 642a4f96 ths
        *p++ = gpr[i] >> 16;
508 642a4f96 ths
        *p++ = gpr[i] >> 24;
509 642a4f96 ths
    }
510 642a4f96 ths
511 642a4f96 ths
    *p++ = 0xea;                /* JMP FAR */
512 642a4f96 ths
    *p++ = ip;                        /* IP */
513 642a4f96 ths
    *p++ = ip >> 8;
514 642a4f96 ths
    *p++ = segs[1];                /* CS */
515 642a4f96 ths
    *p++ = segs[1] >> 8;
516 642a4f96 ths
517 4fc9af53 aliguori
    /* sign rom */
518 4fc9af53 aliguori
    sum = 0;
519 4fc9af53 aliguori
    for (i = 0; i < (sizeof(rom) - 1); i++)
520 4fc9af53 aliguori
        sum += rom[i];
521 4fc9af53 aliguori
    rom[sizeof(rom) - 1] = -sum;
522 4fc9af53 aliguori
523 7ffa4767 pbrook
    cpu_physical_memory_write_rom(option_rom, rom, sizeof(rom));
524 642a4f96 ths
}
525 80cabfad bellard
526 642a4f96 ths
static long get_file_size(FILE *f)
527 642a4f96 ths
{
528 642a4f96 ths
    long where, size;
529 642a4f96 ths
530 642a4f96 ths
    /* XXX: on Unix systems, using fstat() probably makes more sense */
531 642a4f96 ths
532 642a4f96 ths
    where = ftell(f);
533 642a4f96 ths
    fseek(f, 0, SEEK_END);
534 642a4f96 ths
    size = ftell(f);
535 642a4f96 ths
    fseek(f, where, SEEK_SET);
536 642a4f96 ths
537 642a4f96 ths
    return size;
538 642a4f96 ths
}
539 642a4f96 ths
540 7ffa4767 pbrook
static void load_linux(target_phys_addr_t option_rom,
541 4fc9af53 aliguori
                       const char *kernel_filename,
542 642a4f96 ths
                       const char *initrd_filename,
543 642a4f96 ths
                       const char *kernel_cmdline)
544 642a4f96 ths
{
545 642a4f96 ths
    uint16_t protocol;
546 642a4f96 ths
    uint32_t gpr[8];
547 642a4f96 ths
    uint16_t seg[6];
548 642a4f96 ths
    uint16_t real_seg;
549 642a4f96 ths
    int setup_size, kernel_size, initrd_size, cmdline_size;
550 642a4f96 ths
    uint32_t initrd_max;
551 642a4f96 ths
    uint8_t header[1024];
552 a37af289 blueswir1
    target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr;
553 642a4f96 ths
    FILE *f, *fi;
554 642a4f96 ths
555 642a4f96 ths
    /* Align to 16 bytes as a paranoia measure */
556 642a4f96 ths
    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
557 642a4f96 ths
558 642a4f96 ths
    /* load the kernel header */
559 642a4f96 ths
    f = fopen(kernel_filename, "rb");
560 642a4f96 ths
    if (!f || !(kernel_size = get_file_size(f)) ||
561 642a4f96 ths
        fread(header, 1, 1024, f) != 1024) {
562 642a4f96 ths
        fprintf(stderr, "qemu: could not load kernel '%s'\n",
563 642a4f96 ths
                kernel_filename);
564 642a4f96 ths
        exit(1);
565 642a4f96 ths
    }
566 642a4f96 ths
567 642a4f96 ths
    /* kernel protocol version */
568 bc4edd79 bellard
#if 0
569 642a4f96 ths
    fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
570 bc4edd79 bellard
#endif
571 642a4f96 ths
    if (ldl_p(header+0x202) == 0x53726448)
572 642a4f96 ths
        protocol = lduw_p(header+0x206);
573 642a4f96 ths
    else
574 642a4f96 ths
        protocol = 0;
575 642a4f96 ths
576 642a4f96 ths
    if (protocol < 0x200 || !(header[0x211] & 0x01)) {
577 642a4f96 ths
        /* Low kernel */
578 a37af289 blueswir1
        real_addr    = 0x90000;
579 a37af289 blueswir1
        cmdline_addr = 0x9a000 - cmdline_size;
580 a37af289 blueswir1
        prot_addr    = 0x10000;
581 642a4f96 ths
    } else if (protocol < 0x202) {
582 642a4f96 ths
        /* High but ancient kernel */
583 a37af289 blueswir1
        real_addr    = 0x90000;
584 a37af289 blueswir1
        cmdline_addr = 0x9a000 - cmdline_size;
585 a37af289 blueswir1
        prot_addr    = 0x100000;
586 642a4f96 ths
    } else {
587 642a4f96 ths
        /* High and recent kernel */
588 a37af289 blueswir1
        real_addr    = 0x10000;
589 a37af289 blueswir1
        cmdline_addr = 0x20000;
590 a37af289 blueswir1
        prot_addr    = 0x100000;
591 642a4f96 ths
    }
592 642a4f96 ths
593 bc4edd79 bellard
#if 0
594 642a4f96 ths
    fprintf(stderr,
595 526ccb7a balrog
            "qemu: real_addr     = 0x" TARGET_FMT_plx "\n"
596 526ccb7a balrog
            "qemu: cmdline_addr  = 0x" TARGET_FMT_plx "\n"
597 526ccb7a balrog
            "qemu: prot_addr     = 0x" TARGET_FMT_plx "\n",
598 a37af289 blueswir1
            real_addr,
599 a37af289 blueswir1
            cmdline_addr,
600 a37af289 blueswir1
            prot_addr);
601 bc4edd79 bellard
#endif
602 642a4f96 ths
603 642a4f96 ths
    /* highest address for loading the initrd */
604 642a4f96 ths
    if (protocol >= 0x203)
605 642a4f96 ths
        initrd_max = ldl_p(header+0x22c);
606 642a4f96 ths
    else
607 642a4f96 ths
        initrd_max = 0x37ffffff;
608 642a4f96 ths
609 642a4f96 ths
    if (initrd_max >= ram_size-ACPI_DATA_SIZE)
610 642a4f96 ths
        initrd_max = ram_size-ACPI_DATA_SIZE-1;
611 642a4f96 ths
612 642a4f96 ths
    /* kernel command line */
613 a37af289 blueswir1
    pstrcpy_targphys(cmdline_addr, 4096, kernel_cmdline);
614 642a4f96 ths
615 642a4f96 ths
    if (protocol >= 0x202) {
616 a37af289 blueswir1
        stl_p(header+0x228, cmdline_addr);
617 642a4f96 ths
    } else {
618 642a4f96 ths
        stw_p(header+0x20, 0xA33F);
619 642a4f96 ths
        stw_p(header+0x22, cmdline_addr-real_addr);
620 642a4f96 ths
    }
621 642a4f96 ths
622 642a4f96 ths
    /* loader type */
623 642a4f96 ths
    /* High nybble = B reserved for Qemu; low nybble is revision number.
624 642a4f96 ths
       If this code is substantially changed, you may want to consider
625 642a4f96 ths
       incrementing the revision. */
626 642a4f96 ths
    if (protocol >= 0x200)
627 642a4f96 ths
        header[0x210] = 0xB0;
628 642a4f96 ths
629 642a4f96 ths
    /* heap */
630 642a4f96 ths
    if (protocol >= 0x201) {
631 642a4f96 ths
        header[0x211] |= 0x80;        /* CAN_USE_HEAP */
632 642a4f96 ths
        stw_p(header+0x224, cmdline_addr-real_addr-0x200);
633 642a4f96 ths
    }
634 642a4f96 ths
635 642a4f96 ths
    /* load initrd */
636 642a4f96 ths
    if (initrd_filename) {
637 642a4f96 ths
        if (protocol < 0x200) {
638 642a4f96 ths
            fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
639 642a4f96 ths
            exit(1);
640 642a4f96 ths
        }
641 642a4f96 ths
642 642a4f96 ths
        fi = fopen(initrd_filename, "rb");
643 642a4f96 ths
        if (!fi) {
644 642a4f96 ths
            fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
645 642a4f96 ths
                    initrd_filename);
646 642a4f96 ths
            exit(1);
647 642a4f96 ths
        }
648 642a4f96 ths
649 642a4f96 ths
        initrd_size = get_file_size(fi);
650 a37af289 blueswir1
        initrd_addr = (initrd_max-initrd_size) & ~4095;
651 642a4f96 ths
652 526ccb7a balrog
        fprintf(stderr, "qemu: loading initrd (%#x bytes) at 0x" TARGET_FMT_plx
653 526ccb7a balrog
                "\n", initrd_size, initrd_addr);
654 642a4f96 ths
655 a37af289 blueswir1
        if (!fread_targphys_ok(initrd_addr, initrd_size, fi)) {
656 642a4f96 ths
            fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
657 642a4f96 ths
                    initrd_filename);
658 642a4f96 ths
            exit(1);
659 642a4f96 ths
        }
660 642a4f96 ths
        fclose(fi);
661 642a4f96 ths
662 a37af289 blueswir1
        stl_p(header+0x218, initrd_addr);
663 642a4f96 ths
        stl_p(header+0x21c, initrd_size);
664 642a4f96 ths
    }
665 642a4f96 ths
666 642a4f96 ths
    /* store the finalized header and load the rest of the kernel */
667 a37af289 blueswir1
    cpu_physical_memory_write(real_addr, header, 1024);
668 642a4f96 ths
669 642a4f96 ths
    setup_size = header[0x1f1];
670 642a4f96 ths
    if (setup_size == 0)
671 642a4f96 ths
        setup_size = 4;
672 642a4f96 ths
673 642a4f96 ths
    setup_size = (setup_size+1)*512;
674 642a4f96 ths
    kernel_size -= setup_size;        /* Size of protected-mode code */
675 642a4f96 ths
676 a37af289 blueswir1
    if (!fread_targphys_ok(real_addr+1024, setup_size-1024, f) ||
677 a37af289 blueswir1
        !fread_targphys_ok(prot_addr, kernel_size, f)) {
678 642a4f96 ths
        fprintf(stderr, "qemu: read error on kernel '%s'\n",
679 642a4f96 ths
                kernel_filename);
680 642a4f96 ths
        exit(1);
681 642a4f96 ths
    }
682 642a4f96 ths
    fclose(f);
683 642a4f96 ths
684 642a4f96 ths
    /* generate bootsector to set up the initial register state */
685 a37af289 blueswir1
    real_seg = real_addr >> 4;
686 642a4f96 ths
    seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg;
687 642a4f96 ths
    seg[1] = real_seg+0x20;        /* CS */
688 642a4f96 ths
    memset(gpr, 0, sizeof gpr);
689 642a4f96 ths
    gpr[4] = cmdline_addr-real_addr-16;        /* SP (-16 is paranoia) */
690 642a4f96 ths
691 4fc9af53 aliguori
    generate_bootsect(option_rom, gpr, seg, 0);
692 642a4f96 ths
}
693 642a4f96 ths
694 59b8ad81 bellard
static void main_cpu_reset(void *opaque)
695 59b8ad81 bellard
{
696 59b8ad81 bellard
    CPUState *env = opaque;
697 59b8ad81 bellard
    cpu_reset(env);
698 59b8ad81 bellard
}
699 59b8ad81 bellard
700 b41a2cd1 bellard
static const int ide_iobase[2] = { 0x1f0, 0x170 };
701 b41a2cd1 bellard
static const int ide_iobase2[2] = { 0x3f6, 0x376 };
702 b41a2cd1 bellard
static const int ide_irq[2] = { 14, 15 };
703 b41a2cd1 bellard
704 b41a2cd1 bellard
#define NE2000_NB_MAX 6
705 b41a2cd1 bellard
706 8d11df9e bellard
static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
707 b41a2cd1 bellard
static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
708 b41a2cd1 bellard
709 8d11df9e bellard
static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
710 8d11df9e bellard
static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
711 8d11df9e bellard
712 6508fe59 bellard
static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
713 6508fe59 bellard
static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
714 6508fe59 bellard
715 6a36d84e bellard
#ifdef HAS_AUDIO
716 d537cf6c pbrook
static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
717 6a36d84e bellard
{
718 6a36d84e bellard
    struct soundhw *c;
719 6a36d84e bellard
    int audio_enabled = 0;
720 6a36d84e bellard
721 6a36d84e bellard
    for (c = soundhw; !audio_enabled && c->name; ++c) {
722 6a36d84e bellard
        audio_enabled = c->enabled;
723 6a36d84e bellard
    }
724 6a36d84e bellard
725 6a36d84e bellard
    if (audio_enabled) {
726 6a36d84e bellard
        AudioState *s;
727 6a36d84e bellard
728 6a36d84e bellard
        s = AUD_init ();
729 6a36d84e bellard
        if (s) {
730 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
731 6a36d84e bellard
                if (c->enabled) {
732 6a36d84e bellard
                    if (c->isa) {
733 d537cf6c pbrook
                        c->init.init_isa (s, pic);
734 6a36d84e bellard
                    }
735 6a36d84e bellard
                    else {
736 6a36d84e bellard
                        if (pci_bus) {
737 6a36d84e bellard
                            c->init.init_pci (pci_bus, s);
738 6a36d84e bellard
                        }
739 6a36d84e bellard
                    }
740 6a36d84e bellard
                }
741 6a36d84e bellard
            }
742 6a36d84e bellard
        }
743 6a36d84e bellard
    }
744 6a36d84e bellard
}
745 6a36d84e bellard
#endif
746 6a36d84e bellard
747 d537cf6c pbrook
static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
748 a41b2ff2 pbrook
{
749 a41b2ff2 pbrook
    static int nb_ne2k = 0;
750 a41b2ff2 pbrook
751 a41b2ff2 pbrook
    if (nb_ne2k == NE2000_NB_MAX)
752 a41b2ff2 pbrook
        return;
753 d537cf6c pbrook
    isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd);
754 a41b2ff2 pbrook
    nb_ne2k++;
755 a41b2ff2 pbrook
}
756 a41b2ff2 pbrook
757 f753ff16 pbrook
static int load_option_rom(const char *oprom, target_phys_addr_t start,
758 f753ff16 pbrook
                           target_phys_addr_t end)
759 f753ff16 pbrook
{
760 f753ff16 pbrook
        int size;
761 f753ff16 pbrook
762 f753ff16 pbrook
        size = get_image_size(oprom);
763 f753ff16 pbrook
        if (size > 0 && start + size > end) {
764 f753ff16 pbrook
            fprintf(stderr, "Not enough space to load option rom '%s'\n",
765 f753ff16 pbrook
                    oprom);
766 f753ff16 pbrook
            exit(1);
767 f753ff16 pbrook
        }
768 f753ff16 pbrook
        size = load_image_targphys(oprom, start, end - start);
769 f753ff16 pbrook
        if (size < 0) {
770 f753ff16 pbrook
            fprintf(stderr, "Could not load option rom '%s'\n", oprom);
771 f753ff16 pbrook
            exit(1);
772 f753ff16 pbrook
        }
773 f753ff16 pbrook
        /* Round up optiom rom size to the next 2k boundary */
774 f753ff16 pbrook
        size = (size + 2047) & ~2047;
775 f753ff16 pbrook
        return size;
776 f753ff16 pbrook
}
777 f753ff16 pbrook
778 80cabfad bellard
/* PC hardware initialisation */
779 00f82b8a aurel32
static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
780 3023f332 aliguori
                     const char *boot_device,
781 b5ff2d6e bellard
                     const char *kernel_filename, const char *kernel_cmdline,
782 3dbbdc25 bellard
                     const char *initrd_filename,
783 a049de61 bellard
                     int pci_enabled, const char *cpu_model)
784 80cabfad bellard
{
785 80cabfad bellard
    char buf[1024];
786 642a4f96 ths
    int ret, linux_boot, i;
787 f753ff16 pbrook
    ram_addr_t ram_addr, vga_ram_addr, bios_offset, option_rom_offset;
788 00f82b8a aurel32
    ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
789 f753ff16 pbrook
    int bios_size, isa_bios_size, oprom_area_size;
790 46e50e9d bellard
    PCIBus *pci_bus;
791 5c3ff3a7 pbrook
    int piix3_devfn = -1;
792 59b8ad81 bellard
    CPUState *env;
793 d537cf6c pbrook
    qemu_irq *cpu_irq;
794 d537cf6c pbrook
    qemu_irq *i8259;
795 e4bcb14c ths
    int index;
796 e4bcb14c ths
    BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
797 e4bcb14c ths
    BlockDriverState *fd[MAX_FD];
798 34b39c2b aliguori
    int using_vga = cirrus_vga_enabled || std_vga_enabled || vmsvga_enabled;
799 d592d303 bellard
800 00f82b8a aurel32
    if (ram_size >= 0xe0000000 ) {
801 00f82b8a aurel32
        above_4g_mem_size = ram_size - 0xe0000000;
802 00f82b8a aurel32
        below_4g_mem_size = 0xe0000000;
803 00f82b8a aurel32
    } else {
804 00f82b8a aurel32
        below_4g_mem_size = ram_size;
805 00f82b8a aurel32
    }
806 00f82b8a aurel32
807 80cabfad bellard
    linux_boot = (kernel_filename != NULL);
808 80cabfad bellard
809 59b8ad81 bellard
    /* init CPUs */
810 a049de61 bellard
    if (cpu_model == NULL) {
811 a049de61 bellard
#ifdef TARGET_X86_64
812 a049de61 bellard
        cpu_model = "qemu64";
813 a049de61 bellard
#else
814 a049de61 bellard
        cpu_model = "qemu32";
815 a049de61 bellard
#endif
816 a049de61 bellard
    }
817 a049de61 bellard
    
818 59b8ad81 bellard
    for(i = 0; i < smp_cpus; i++) {
819 aaed909a bellard
        env = cpu_init(cpu_model);
820 aaed909a bellard
        if (!env) {
821 aaed909a bellard
            fprintf(stderr, "Unable to find x86 CPU definition\n");
822 aaed909a bellard
            exit(1);
823 aaed909a bellard
        }
824 59b8ad81 bellard
        if (i != 0)
825 ce5232c5 bellard
            env->halted = 1;
826 59b8ad81 bellard
        if (smp_cpus > 1) {
827 59b8ad81 bellard
            /* XXX: enable it in all cases */
828 59b8ad81 bellard
            env->cpuid_features |= CPUID_APIC;
829 59b8ad81 bellard
        }
830 59b8ad81 bellard
        qemu_register_reset(main_cpu_reset, env);
831 59b8ad81 bellard
        if (pci_enabled) {
832 59b8ad81 bellard
            apic_init(env);
833 59b8ad81 bellard
        }
834 59b8ad81 bellard
    }
835 59b8ad81 bellard
836 26fb5e48 aurel32
    vmport_init();
837 26fb5e48 aurel32
838 80cabfad bellard
    /* allocate RAM */
839 82b36dc3 aliguori
    ram_addr = qemu_ram_alloc(0xa0000);
840 82b36dc3 aliguori
    cpu_register_physical_memory(0, 0xa0000, ram_addr);
841 82b36dc3 aliguori
842 82b36dc3 aliguori
    /* Allocate, even though we won't register, so we don't break the
843 82b36dc3 aliguori
     * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
844 82b36dc3 aliguori
     * and some bios areas, which will be registered later
845 82b36dc3 aliguori
     */
846 82b36dc3 aliguori
    ram_addr = qemu_ram_alloc(0x100000 - 0xa0000);
847 82b36dc3 aliguori
    ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000);
848 82b36dc3 aliguori
    cpu_register_physical_memory(0x100000,
849 82b36dc3 aliguori
                 below_4g_mem_size - 0x100000,
850 82b36dc3 aliguori
                 ram_addr);
851 00f82b8a aurel32
852 00f82b8a aurel32
    /* above 4giga memory allocation */
853 00f82b8a aurel32
    if (above_4g_mem_size > 0) {
854 82b36dc3 aliguori
        ram_addr = qemu_ram_alloc(above_4g_mem_size);
855 82b36dc3 aliguori
        cpu_register_physical_memory(0x100000000ULL,
856 526ccb7a balrog
                                     above_4g_mem_size,
857 82b36dc3 aliguori
                                     ram_addr);
858 00f82b8a aurel32
    }
859 80cabfad bellard
860 82b36dc3 aliguori
861 970ac5a3 bellard
    /* allocate VGA RAM */
862 970ac5a3 bellard
    vga_ram_addr = qemu_ram_alloc(vga_ram_size);
863 7587cf44 bellard
864 970ac5a3 bellard
    /* BIOS load */
865 1192dad8 j_mayer
    if (bios_name == NULL)
866 1192dad8 j_mayer
        bios_name = BIOS_FILENAME;
867 1192dad8 j_mayer
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
868 7587cf44 bellard
    bios_size = get_image_size(buf);
869 5fafdf24 ths
    if (bios_size <= 0 ||
870 970ac5a3 bellard
        (bios_size % 65536) != 0) {
871 7587cf44 bellard
        goto bios_error;
872 7587cf44 bellard
    }
873 970ac5a3 bellard
    bios_offset = qemu_ram_alloc(bios_size);
874 7587cf44 bellard
    ret = load_image(buf, phys_ram_base + bios_offset);
875 7587cf44 bellard
    if (ret != bios_size) {
876 7587cf44 bellard
    bios_error:
877 970ac5a3 bellard
        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
878 80cabfad bellard
        exit(1);
879 80cabfad bellard
    }
880 7587cf44 bellard
    /* map the last 128KB of the BIOS in ISA space */
881 7587cf44 bellard
    isa_bios_size = bios_size;
882 7587cf44 bellard
    if (isa_bios_size > (128 * 1024))
883 7587cf44 bellard
        isa_bios_size = 128 * 1024;
884 5fafdf24 ths
    cpu_register_physical_memory(0x100000 - isa_bios_size,
885 5fafdf24 ths
                                 isa_bios_size,
886 7587cf44 bellard
                                 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
887 9ae02555 ths
888 4fc9af53 aliguori
889 f753ff16 pbrook
890 f753ff16 pbrook
    option_rom_offset = qemu_ram_alloc(0x20000);
891 f753ff16 pbrook
    oprom_area_size = 0;
892 f753ff16 pbrook
    cpu_register_physical_memory(0xc0000, 0x20000,
893 f753ff16 pbrook
                                 option_rom_offset | IO_MEM_ROM);
894 f753ff16 pbrook
895 f753ff16 pbrook
    if (using_vga) {
896 f753ff16 pbrook
        /* VGA BIOS load */
897 f753ff16 pbrook
        if (cirrus_vga_enabled) {
898 f753ff16 pbrook
            snprintf(buf, sizeof(buf), "%s/%s", bios_dir,
899 f753ff16 pbrook
                     VGABIOS_CIRRUS_FILENAME);
900 f753ff16 pbrook
        } else {
901 f753ff16 pbrook
            snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
902 970ac5a3 bellard
        }
903 f753ff16 pbrook
        oprom_area_size = load_option_rom(buf, 0xc0000, 0xe0000);
904 f753ff16 pbrook
    }
905 f753ff16 pbrook
    /* Although video roms can grow larger than 0x8000, the area between
906 f753ff16 pbrook
     * 0xc0000 - 0xc8000 is reserved for them. It means we won't be looking
907 f753ff16 pbrook
     * for any other kind of option rom inside this area */
908 f753ff16 pbrook
    if (oprom_area_size < 0x8000)
909 f753ff16 pbrook
        oprom_area_size = 0x8000;
910 f753ff16 pbrook
911 f753ff16 pbrook
    if (linux_boot) {
912 7ffa4767 pbrook
        load_linux(0xc0000 + oprom_area_size,
913 f753ff16 pbrook
                   kernel_filename, initrd_filename, kernel_cmdline);
914 f753ff16 pbrook
        oprom_area_size += 2048;
915 f753ff16 pbrook
    }
916 f753ff16 pbrook
917 f753ff16 pbrook
    for (i = 0; i < nb_option_roms; i++) {
918 f753ff16 pbrook
        oprom_area_size += load_option_rom(option_rom[i],
919 f753ff16 pbrook
                                           0xc0000 + oprom_area_size, 0xe0000);
920 9ae02555 ths
    }
921 9ae02555 ths
922 7587cf44 bellard
    /* map all the bios at the top of memory */
923 5fafdf24 ths
    cpu_register_physical_memory((uint32_t)(-bios_size),
924 7587cf44 bellard
                                 bios_size, bios_offset | IO_MEM_ROM);
925 3b46e624 ths
926 80cabfad bellard
    bochs_bios_init();
927 80cabfad bellard
928 a5b38b51 aurel32
    cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
929 d537cf6c pbrook
    i8259 = i8259_init(cpu_irq[0]);
930 d537cf6c pbrook
    ferr_irq = i8259[13];
931 d537cf6c pbrook
932 69b91039 bellard
    if (pci_enabled) {
933 d537cf6c pbrook
        pci_bus = i440fx_init(&i440fx_state, i8259);
934 8f1c91d8 ths
        piix3_devfn = piix3_init(pci_bus, -1);
935 46e50e9d bellard
    } else {
936 46e50e9d bellard
        pci_bus = NULL;
937 69b91039 bellard
    }
938 69b91039 bellard
939 80cabfad bellard
    /* init basic PC hardware */
940 b41a2cd1 bellard
    register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
941 80cabfad bellard
942 f929aad6 bellard
    register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
943 f929aad6 bellard
944 1f04275e bellard
    if (cirrus_vga_enabled) {
945 1f04275e bellard
        if (pci_enabled) {
946 5fafdf24 ths
            pci_cirrus_vga_init(pci_bus,
947 3023f332 aliguori
                                phys_ram_base + vga_ram_addr,
948 970ac5a3 bellard
                                vga_ram_addr, vga_ram_size);
949 1f04275e bellard
        } else {
950 3023f332 aliguori
            isa_cirrus_vga_init(phys_ram_base + vga_ram_addr,
951 970ac5a3 bellard
                                vga_ram_addr, vga_ram_size);
952 1f04275e bellard
        }
953 d34cab9f ths
    } else if (vmsvga_enabled) {
954 d34cab9f ths
        if (pci_enabled)
955 3023f332 aliguori
            pci_vmsvga_init(pci_bus, phys_ram_base + vga_ram_addr,
956 45e4522e balrog
                            vga_ram_addr, vga_ram_size);
957 d34cab9f ths
        else
958 d34cab9f ths
            fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
959 c2b3b41a aliguori
    } else if (std_vga_enabled) {
960 89b6b508 bellard
        if (pci_enabled) {
961 3023f332 aliguori
            pci_vga_init(pci_bus, phys_ram_base + vga_ram_addr,
962 970ac5a3 bellard
                         vga_ram_addr, vga_ram_size, 0, 0);
963 89b6b508 bellard
        } else {
964 3023f332 aliguori
            isa_vga_init(phys_ram_base + vga_ram_addr,
965 970ac5a3 bellard
                         vga_ram_addr, vga_ram_size);
966 89b6b508 bellard
        }
967 1f04275e bellard
    }
968 80cabfad bellard
969 42fc73a1 aurel32
    rtc_state = rtc_init(0x70, i8259[8], 2000);
970 80cabfad bellard
971 3b4366de blueswir1
    qemu_register_boot_set(pc_boot_set, rtc_state);
972 3b4366de blueswir1
973 e1a23744 bellard
    register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
974 e1a23744 bellard
    register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
975 e1a23744 bellard
976 d592d303 bellard
    if (pci_enabled) {
977 d592d303 bellard
        ioapic = ioapic_init();
978 d592d303 bellard
    }
979 d537cf6c pbrook
    pit = pit_init(0x40, i8259[0]);
980 fd06c375 bellard
    pcspk_init(pit);
981 16b29ae1 aliguori
    if (!no_hpet) {
982 16b29ae1 aliguori
        hpet_init(i8259);
983 16b29ae1 aliguori
    }
984 d592d303 bellard
    if (pci_enabled) {
985 d592d303 bellard
        pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
986 d592d303 bellard
    }
987 b41a2cd1 bellard
988 8d11df9e bellard
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
989 8d11df9e bellard
        if (serial_hds[i]) {
990 b6cd0ea1 aurel32
            serial_init(serial_io[i], i8259[serial_irq[i]], 115200,
991 b6cd0ea1 aurel32
                        serial_hds[i]);
992 8d11df9e bellard
        }
993 8d11df9e bellard
    }
994 b41a2cd1 bellard
995 6508fe59 bellard
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
996 6508fe59 bellard
        if (parallel_hds[i]) {
997 d537cf6c pbrook
            parallel_init(parallel_io[i], i8259[parallel_irq[i]],
998 d537cf6c pbrook
                          parallel_hds[i]);
999 6508fe59 bellard
        }
1000 6508fe59 bellard
    }
1001 6508fe59 bellard
1002 a41b2ff2 pbrook
    for(i = 0; i < nb_nics; i++) {
1003 cb457d76 aliguori
        NICInfo *nd = &nd_table[i];
1004 cb457d76 aliguori
1005 cb457d76 aliguori
        if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
1006 d537cf6c pbrook
            pc_init_ne2k_isa(nd, i8259);
1007 cb457d76 aliguori
        else
1008 cb457d76 aliguori
            pci_nic_init(pci_bus, nd, -1, "ne2k_pci");
1009 a41b2ff2 pbrook
    }
1010 b41a2cd1 bellard
1011 5e3cb534 aliguori
    qemu_system_hot_add_init();
1012 5e3cb534 aliguori
1013 e4bcb14c ths
    if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
1014 e4bcb14c ths
        fprintf(stderr, "qemu: too many IDE bus\n");
1015 e4bcb14c ths
        exit(1);
1016 e4bcb14c ths
    }
1017 e4bcb14c ths
1018 e4bcb14c ths
    for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
1019 e4bcb14c ths
        index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
1020 e4bcb14c ths
        if (index != -1)
1021 e4bcb14c ths
            hd[i] = drives_table[index].bdrv;
1022 e4bcb14c ths
        else
1023 e4bcb14c ths
            hd[i] = NULL;
1024 e4bcb14c ths
    }
1025 e4bcb14c ths
1026 a41b2ff2 pbrook
    if (pci_enabled) {
1027 e4bcb14c ths
        pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, i8259);
1028 a41b2ff2 pbrook
    } else {
1029 e4bcb14c ths
        for(i = 0; i < MAX_IDE_BUS; i++) {
1030 d537cf6c pbrook
            isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
1031 e4bcb14c ths
                         hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
1032 69b91039 bellard
        }
1033 b41a2cd1 bellard
    }
1034 69b91039 bellard
1035 d537cf6c pbrook
    i8042_init(i8259[1], i8259[12], 0x60);
1036 7c29d0c0 bellard
    DMA_init(0);
1037 6a36d84e bellard
#ifdef HAS_AUDIO
1038 d537cf6c pbrook
    audio_init(pci_enabled ? pci_bus : NULL, i8259);
1039 fb065187 bellard
#endif
1040 80cabfad bellard
1041 e4bcb14c ths
    for(i = 0; i < MAX_FD; i++) {
1042 e4bcb14c ths
        index = drive_get_index(IF_FLOPPY, 0, i);
1043 e4bcb14c ths
        if (index != -1)
1044 e4bcb14c ths
            fd[i] = drives_table[index].bdrv;
1045 e4bcb14c ths
        else
1046 e4bcb14c ths
            fd[i] = NULL;
1047 e4bcb14c ths
    }
1048 e4bcb14c ths
    floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
1049 b41a2cd1 bellard
1050 00f82b8a aurel32
    cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
1051 69b91039 bellard
1052 bb36d470 bellard
    if (pci_enabled && usb_enabled) {
1053 afcc3cdf ths
        usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
1054 bb36d470 bellard
    }
1055 bb36d470 bellard
1056 6515b203 bellard
    if (pci_enabled && acpi_enabled) {
1057 3fffc223 ths
        uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
1058 0ff596d0 pbrook
        i2c_bus *smbus;
1059 0ff596d0 pbrook
1060 0ff596d0 pbrook
        /* TODO: Populate SPD eeprom data.  */
1061 cf7a2fe2 aurel32
        smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]);
1062 3fffc223 ths
        for (i = 0; i < 8; i++) {
1063 0ff596d0 pbrook
            smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256));
1064 3fffc223 ths
        }
1065 6515b203 bellard
    }
1066 3b46e624 ths
1067 a5954d5c bellard
    if (i440fx_state) {
1068 a5954d5c bellard
        i440fx_init_memory_mappings(i440fx_state);
1069 a5954d5c bellard
    }
1070 e4bcb14c ths
1071 7d8406be pbrook
    if (pci_enabled) {
1072 e4bcb14c ths
        int max_bus;
1073 e4bcb14c ths
        int bus, unit;
1074 7d8406be pbrook
        void *scsi;
1075 96d30e48 ths
1076 e4bcb14c ths
        max_bus = drive_get_max_bus(IF_SCSI);
1077 e4bcb14c ths
1078 e4bcb14c ths
        for (bus = 0; bus <= max_bus; bus++) {
1079 e4bcb14c ths
            scsi = lsi_scsi_init(pci_bus, -1);
1080 e4bcb14c ths
            for (unit = 0; unit < LSI_MAX_DEVS; unit++) {
1081 e4bcb14c ths
                index = drive_get_index(IF_SCSI, bus, unit);
1082 e4bcb14c ths
                if (index == -1)
1083 e4bcb14c ths
                    continue;
1084 e4bcb14c ths
                lsi_scsi_attach(scsi, drives_table[index].bdrv, unit);
1085 e4bcb14c ths
            }
1086 e4bcb14c ths
        }
1087 7d8406be pbrook
    }
1088 6e02c38d aliguori
1089 6e02c38d aliguori
    /* Add virtio block devices */
1090 6e02c38d aliguori
    if (pci_enabled) {
1091 6e02c38d aliguori
        int index;
1092 6e02c38d aliguori
        int unit_id = 0;
1093 6e02c38d aliguori
1094 6e02c38d aliguori
        while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
1095 9b32d5a5 aliguori
            virtio_blk_init(pci_bus, drives_table[index].bdrv);
1096 6e02c38d aliguori
            unit_id++;
1097 6e02c38d aliguori
        }
1098 6e02c38d aliguori
    }
1099 bd322087 aliguori
1100 bd322087 aliguori
    /* Add virtio balloon device */
1101 bd322087 aliguori
    if (pci_enabled)
1102 bd322087 aliguori
        virtio_balloon_init(pci_bus);
1103 a2fa19f9 aliguori
1104 a2fa19f9 aliguori
    /* Add virtio console devices */
1105 a2fa19f9 aliguori
    if (pci_enabled) {
1106 a2fa19f9 aliguori
        for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
1107 a2fa19f9 aliguori
            if (virtcon_hds[i])
1108 a2fa19f9 aliguori
                virtio_console_init(pci_bus, virtcon_hds[i]);
1109 a2fa19f9 aliguori
        }
1110 a2fa19f9 aliguori
    }
1111 80cabfad bellard
}
1112 b5ff2d6e bellard
1113 00f82b8a aurel32
static void pc_init_pci(ram_addr_t ram_size, int vga_ram_size,
1114 3023f332 aliguori
                        const char *boot_device,
1115 5fafdf24 ths
                        const char *kernel_filename,
1116 3dbbdc25 bellard
                        const char *kernel_cmdline,
1117 94fc95cd j_mayer
                        const char *initrd_filename,
1118 94fc95cd j_mayer
                        const char *cpu_model)
1119 3dbbdc25 bellard
{
1120 3023f332 aliguori
    pc_init1(ram_size, vga_ram_size, boot_device,
1121 3dbbdc25 bellard
             kernel_filename, kernel_cmdline,
1122 a049de61 bellard
             initrd_filename, 1, cpu_model);
1123 3dbbdc25 bellard
}
1124 3dbbdc25 bellard
1125 00f82b8a aurel32
static void pc_init_isa(ram_addr_t ram_size, int vga_ram_size,
1126 3023f332 aliguori
                        const char *boot_device,
1127 5fafdf24 ths
                        const char *kernel_filename,
1128 3dbbdc25 bellard
                        const char *kernel_cmdline,
1129 94fc95cd j_mayer
                        const char *initrd_filename,
1130 94fc95cd j_mayer
                        const char *cpu_model)
1131 3dbbdc25 bellard
{
1132 3023f332 aliguori
    pc_init1(ram_size, vga_ram_size, boot_device,
1133 3dbbdc25 bellard
             kernel_filename, kernel_cmdline,
1134 a049de61 bellard
             initrd_filename, 0, cpu_model);
1135 3dbbdc25 bellard
}
1136 3dbbdc25 bellard
1137 0bacd130 aliguori
/* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
1138 0bacd130 aliguori
   BIOS will read it and start S3 resume at POST Entry */
1139 0bacd130 aliguori
void cmos_set_s3_resume(void)
1140 0bacd130 aliguori
{
1141 0bacd130 aliguori
    if (rtc_state)
1142 0bacd130 aliguori
        rtc_set_memory(rtc_state, 0xF, 0xFE);
1143 0bacd130 aliguori
}
1144 0bacd130 aliguori
1145 b5ff2d6e bellard
QEMUMachine pc_machine = {
1146 a245f2e7 aurel32
    .name = "pc",
1147 a245f2e7 aurel32
    .desc = "Standard PC",
1148 a245f2e7 aurel32
    .init = pc_init_pci,
1149 a245f2e7 aurel32
    .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE,
1150 b2097003 aliguori
    .max_cpus = 255,
1151 3dbbdc25 bellard
};
1152 3dbbdc25 bellard
1153 3dbbdc25 bellard
QEMUMachine isapc_machine = {
1154 a245f2e7 aurel32
    .name = "isapc",
1155 a245f2e7 aurel32
    .desc = "ISA-only PC",
1156 a245f2e7 aurel32
    .init = pc_init_isa,
1157 a245f2e7 aurel32
    .ram_require = VGA_RAM_SIZE + PC_MAX_BIOS_SIZE,
1158 b2097003 aliguori
    .max_cpus = 1,
1159 b5ff2d6e bellard
};