Statistics
| Branch: | Revision:

root / hw / pc.c @ 94ad5b00

History | View | Annotate | Download (31.9 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 aa28b9bf Blue Swirl
#include "apic.h"
27 87ecb68b pbrook
#include "fdc.h"
28 c0897e0c Markus Armbruster
#include "ide.h"
29 87ecb68b pbrook
#include "pci.h"
30 18e08a55 Michael S. Tsirkin
#include "vmware_vga.h"
31 376253ec aliguori
#include "monitor.h"
32 3cce6243 blueswir1
#include "fw_cfg.h"
33 16b29ae1 aliguori
#include "hpet_emul.h"
34 b6f6e3d3 aliguori
#include "smbios.h"
35 ca20cf32 Blue Swirl
#include "loader.h"
36 ca20cf32 Blue Swirl
#include "elf.h"
37 52001445 Adam Lackorzynski
#include "multiboot.h"
38 1d914fa0 Isaku Yamahata
#include "mc146818rtc.h"
39 92a16d7a Blue Swirl
#include "msix.h"
40 822557eb Jan Kiszka
#include "sysbus.h"
41 666daa68 Markus Armbruster
#include "sysemu.h"
42 2446333c Blue Swirl
#include "blockdev.h"
43 a19cbfb3 Gerd Hoffmann
#include "ui/qemu-spice.h"
44 80cabfad bellard
45 b41a2cd1 bellard
/* output Bochs bios info messages */
46 b41a2cd1 bellard
//#define DEBUG_BIOS
47 b41a2cd1 bellard
48 471fd342 Blue Swirl
/* debug PC/ISA interrupts */
49 471fd342 Blue Swirl
//#define DEBUG_IRQ
50 471fd342 Blue Swirl
51 471fd342 Blue Swirl
#ifdef DEBUG_IRQ
52 471fd342 Blue Swirl
#define DPRINTF(fmt, ...)                                       \
53 471fd342 Blue Swirl
    do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
54 471fd342 Blue Swirl
#else
55 471fd342 Blue Swirl
#define DPRINTF(fmt, ...)
56 471fd342 Blue Swirl
#endif
57 471fd342 Blue Swirl
58 80cabfad bellard
#define BIOS_FILENAME "bios.bin"
59 80cabfad bellard
60 7fb4fdcf balrog
#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
61 7fb4fdcf balrog
62 a80274c3 pbrook
/* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
63 a80274c3 pbrook
#define ACPI_DATA_SIZE       0x10000
64 3cce6243 blueswir1
#define BIOS_CFG_IOPORT 0x510
65 8a92ea2f aliguori
#define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
66 b6f6e3d3 aliguori
#define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
67 6b35e7bf Jes Sorensen
#define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
68 4c5b10b7 Jes Sorensen
#define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
69 40ac17cd Gleb Natapov
#define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
70 80cabfad bellard
71 92a16d7a Blue Swirl
#define MSI_ADDR_BASE 0xfee00000
72 92a16d7a Blue Swirl
73 4c5b10b7 Jes Sorensen
#define E820_NR_ENTRIES                16
74 4c5b10b7 Jes Sorensen
75 4c5b10b7 Jes Sorensen
struct e820_entry {
76 4c5b10b7 Jes Sorensen
    uint64_t address;
77 4c5b10b7 Jes Sorensen
    uint64_t length;
78 4c5b10b7 Jes Sorensen
    uint32_t type;
79 67d4b0c1 Alex Williamson
} __attribute((__packed__, __aligned__(4)));
80 4c5b10b7 Jes Sorensen
81 4c5b10b7 Jes Sorensen
struct e820_table {
82 4c5b10b7 Jes Sorensen
    uint32_t count;
83 4c5b10b7 Jes Sorensen
    struct e820_entry entry[E820_NR_ENTRIES];
84 67d4b0c1 Alex Williamson
} __attribute((__packed__, __aligned__(4)));
85 4c5b10b7 Jes Sorensen
86 4c5b10b7 Jes Sorensen
static struct e820_table e820_table;
87 dd703b99 Blue Swirl
struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
88 4c5b10b7 Jes Sorensen
89 845773ab Isaku Yamahata
void isa_irq_handler(void *opaque, int n, int level)
90 1452411b Avi Kivity
{
91 1452411b Avi Kivity
    IsaIrqState *isa = (IsaIrqState *)opaque;
92 1452411b Avi Kivity
93 471fd342 Blue Swirl
    DPRINTF("isa_irqs: %s irq %d\n", level? "raise" : "lower", n);
94 1632dc6a Avi Kivity
    if (n < 16) {
95 1632dc6a Avi Kivity
        qemu_set_irq(isa->i8259[n], level);
96 1632dc6a Avi Kivity
    }
97 2c8d9340 Gerd Hoffmann
    if (isa->ioapic)
98 2c8d9340 Gerd Hoffmann
        qemu_set_irq(isa->ioapic[n], level);
99 1632dc6a Avi Kivity
};
100 1452411b Avi Kivity
101 b41a2cd1 bellard
static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
102 80cabfad bellard
{
103 80cabfad bellard
}
104 80cabfad bellard
105 f929aad6 bellard
/* MSDOS compatibility mode FPU exception support */
106 d537cf6c pbrook
static qemu_irq ferr_irq;
107 8e78eb28 Isaku Yamahata
108 8e78eb28 Isaku Yamahata
void pc_register_ferr_irq(qemu_irq irq)
109 8e78eb28 Isaku Yamahata
{
110 8e78eb28 Isaku Yamahata
    ferr_irq = irq;
111 8e78eb28 Isaku Yamahata
}
112 8e78eb28 Isaku Yamahata
113 f929aad6 bellard
/* XXX: add IGNNE support */
114 f929aad6 bellard
void cpu_set_ferr(CPUX86State *s)
115 f929aad6 bellard
{
116 d537cf6c pbrook
    qemu_irq_raise(ferr_irq);
117 f929aad6 bellard
}
118 f929aad6 bellard
119 f929aad6 bellard
static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
120 f929aad6 bellard
{
121 d537cf6c pbrook
    qemu_irq_lower(ferr_irq);
122 f929aad6 bellard
}
123 f929aad6 bellard
124 28ab0e2e bellard
/* TSC handling */
125 28ab0e2e bellard
uint64_t cpu_get_tsc(CPUX86State *env)
126 28ab0e2e bellard
{
127 4a1418e0 Anthony Liguori
    return cpu_get_ticks();
128 28ab0e2e bellard
}
129 28ab0e2e bellard
130 a5954d5c bellard
/* SMM support */
131 f885f1ea Isaku Yamahata
132 f885f1ea Isaku Yamahata
static cpu_set_smm_t smm_set;
133 f885f1ea Isaku Yamahata
static void *smm_arg;
134 f885f1ea Isaku Yamahata
135 f885f1ea Isaku Yamahata
void cpu_smm_register(cpu_set_smm_t callback, void *arg)
136 f885f1ea Isaku Yamahata
{
137 f885f1ea Isaku Yamahata
    assert(smm_set == NULL);
138 f885f1ea Isaku Yamahata
    assert(smm_arg == NULL);
139 f885f1ea Isaku Yamahata
    smm_set = callback;
140 f885f1ea Isaku Yamahata
    smm_arg = arg;
141 f885f1ea Isaku Yamahata
}
142 f885f1ea Isaku Yamahata
143 a5954d5c bellard
void cpu_smm_update(CPUState *env)
144 a5954d5c bellard
{
145 f885f1ea Isaku Yamahata
    if (smm_set && smm_arg && env == first_cpu)
146 f885f1ea Isaku Yamahata
        smm_set(!!(env->hflags & HF_SMM_MASK), smm_arg);
147 a5954d5c bellard
}
148 a5954d5c bellard
149 a5954d5c bellard
150 3de388f6 bellard
/* IRQ handling */
151 3de388f6 bellard
int cpu_get_pic_interrupt(CPUState *env)
152 3de388f6 bellard
{
153 3de388f6 bellard
    int intno;
154 3de388f6 bellard
155 cf6d64bf Blue Swirl
    intno = apic_get_interrupt(env->apic_state);
156 3de388f6 bellard
    if (intno >= 0) {
157 3de388f6 bellard
        /* set irq request if a PIC irq is still pending */
158 3de388f6 bellard
        /* XXX: improve that */
159 5fafdf24 ths
        pic_update_irq(isa_pic);
160 3de388f6 bellard
        return intno;
161 3de388f6 bellard
    }
162 3de388f6 bellard
    /* read the irq from the PIC */
163 cf6d64bf Blue Swirl
    if (!apic_accept_pic_intr(env->apic_state)) {
164 0e21e12b ths
        return -1;
165 cf6d64bf Blue Swirl
    }
166 0e21e12b ths
167 3de388f6 bellard
    intno = pic_read_irq(isa_pic);
168 3de388f6 bellard
    return intno;
169 3de388f6 bellard
}
170 3de388f6 bellard
171 d537cf6c pbrook
static void pic_irq_request(void *opaque, int irq, int level)
172 3de388f6 bellard
{
173 a5b38b51 aurel32
    CPUState *env = first_cpu;
174 a5b38b51 aurel32
175 471fd342 Blue Swirl
    DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
176 d5529471 aurel32
    if (env->apic_state) {
177 d5529471 aurel32
        while (env) {
178 cf6d64bf Blue Swirl
            if (apic_accept_pic_intr(env->apic_state)) {
179 cf6d64bf Blue Swirl
                apic_deliver_pic_intr(env->apic_state, level);
180 cf6d64bf Blue Swirl
            }
181 d5529471 aurel32
            env = env->next_cpu;
182 d5529471 aurel32
        }
183 d5529471 aurel32
    } else {
184 b614106a aurel32
        if (level)
185 b614106a aurel32
            cpu_interrupt(env, CPU_INTERRUPT_HARD);
186 b614106a aurel32
        else
187 b614106a aurel32
            cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
188 a5b38b51 aurel32
    }
189 3de388f6 bellard
}
190 3de388f6 bellard
191 b0a21b53 bellard
/* PC cmos mappings */
192 b0a21b53 bellard
193 80cabfad bellard
#define REG_EQUIPMENT_BYTE          0x14
194 80cabfad bellard
195 d288c7ba Blue Swirl
static int cmos_get_fd_drive_type(FDriveType fd0)
196 777428f2 bellard
{
197 777428f2 bellard
    int val;
198 777428f2 bellard
199 777428f2 bellard
    switch (fd0) {
200 d288c7ba Blue Swirl
    case FDRIVE_DRV_144:
201 777428f2 bellard
        /* 1.44 Mb 3"5 drive */
202 777428f2 bellard
        val = 4;
203 777428f2 bellard
        break;
204 d288c7ba Blue Swirl
    case FDRIVE_DRV_288:
205 777428f2 bellard
        /* 2.88 Mb 3"5 drive */
206 777428f2 bellard
        val = 5;
207 777428f2 bellard
        break;
208 d288c7ba Blue Swirl
    case FDRIVE_DRV_120:
209 777428f2 bellard
        /* 1.2 Mb 5"5 drive */
210 777428f2 bellard
        val = 2;
211 777428f2 bellard
        break;
212 d288c7ba Blue Swirl
    case FDRIVE_DRV_NONE:
213 777428f2 bellard
    default:
214 777428f2 bellard
        val = 0;
215 777428f2 bellard
        break;
216 777428f2 bellard
    }
217 777428f2 bellard
    return val;
218 777428f2 bellard
}
219 777428f2 bellard
220 ec2654fb Isaku Yamahata
static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd,
221 1d914fa0 Isaku Yamahata
                         ISADevice *s)
222 ba6c2377 bellard
{
223 ba6c2377 bellard
    int cylinders, heads, sectors;
224 ba6c2377 bellard
    bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
225 ba6c2377 bellard
    rtc_set_memory(s, type_ofs, 47);
226 ba6c2377 bellard
    rtc_set_memory(s, info_ofs, cylinders);
227 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
228 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 2, heads);
229 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 3, 0xff);
230 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 4, 0xff);
231 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
232 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 6, cylinders);
233 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
234 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 8, sectors);
235 ba6c2377 bellard
}
236 ba6c2377 bellard
237 6ac0e82d balrog
/* convert boot_device letter to something recognizable by the bios */
238 6ac0e82d balrog
static int boot_device2nibble(char boot_device)
239 6ac0e82d balrog
{
240 6ac0e82d balrog
    switch(boot_device) {
241 6ac0e82d balrog
    case 'a':
242 6ac0e82d balrog
    case 'b':
243 6ac0e82d balrog
        return 0x01; /* floppy boot */
244 6ac0e82d balrog
    case 'c':
245 6ac0e82d balrog
        return 0x02; /* hard drive boot */
246 6ac0e82d balrog
    case 'd':
247 6ac0e82d balrog
        return 0x03; /* CD-ROM boot */
248 6ac0e82d balrog
    case 'n':
249 6ac0e82d balrog
        return 0x04; /* Network boot */
250 6ac0e82d balrog
    }
251 6ac0e82d balrog
    return 0;
252 6ac0e82d balrog
}
253 6ac0e82d balrog
254 1d914fa0 Isaku Yamahata
static int set_boot_dev(ISADevice *s, const char *boot_device, int fd_bootchk)
255 0ecdffbb aurel32
{
256 0ecdffbb aurel32
#define PC_MAX_BOOT_DEVICES 3
257 0ecdffbb aurel32
    int nbds, bds[3] = { 0, };
258 0ecdffbb aurel32
    int i;
259 0ecdffbb aurel32
260 0ecdffbb aurel32
    nbds = strlen(boot_device);
261 0ecdffbb aurel32
    if (nbds > PC_MAX_BOOT_DEVICES) {
262 1ecda02b Markus Armbruster
        error_report("Too many boot devices for PC");
263 0ecdffbb aurel32
        return(1);
264 0ecdffbb aurel32
    }
265 0ecdffbb aurel32
    for (i = 0; i < nbds; i++) {
266 0ecdffbb aurel32
        bds[i] = boot_device2nibble(boot_device[i]);
267 0ecdffbb aurel32
        if (bds[i] == 0) {
268 1ecda02b Markus Armbruster
            error_report("Invalid boot device for PC: '%c'",
269 1ecda02b Markus Armbruster
                         boot_device[i]);
270 0ecdffbb aurel32
            return(1);
271 0ecdffbb aurel32
        }
272 0ecdffbb aurel32
    }
273 0ecdffbb aurel32
    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
274 d9346e81 Markus Armbruster
    rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
275 0ecdffbb aurel32
    return(0);
276 0ecdffbb aurel32
}
277 0ecdffbb aurel32
278 d9346e81 Markus Armbruster
static int pc_boot_set(void *opaque, const char *boot_device)
279 d9346e81 Markus Armbruster
{
280 d9346e81 Markus Armbruster
    return set_boot_dev(opaque, boot_device, 0);
281 d9346e81 Markus Armbruster
}
282 d9346e81 Markus Armbruster
283 c0897e0c Markus Armbruster
typedef struct pc_cmos_init_late_arg {
284 c0897e0c Markus Armbruster
    ISADevice *rtc_state;
285 c0897e0c Markus Armbruster
    BusState *idebus0, *idebus1;
286 c0897e0c Markus Armbruster
} pc_cmos_init_late_arg;
287 c0897e0c Markus Armbruster
288 c0897e0c Markus Armbruster
static void pc_cmos_init_late(void *opaque)
289 c0897e0c Markus Armbruster
{
290 c0897e0c Markus Armbruster
    pc_cmos_init_late_arg *arg = opaque;
291 c0897e0c Markus Armbruster
    ISADevice *s = arg->rtc_state;
292 c0897e0c Markus Armbruster
    int val;
293 c0897e0c Markus Armbruster
    BlockDriverState *hd_table[4];
294 c0897e0c Markus Armbruster
    int i;
295 c0897e0c Markus Armbruster
296 c0897e0c Markus Armbruster
    ide_get_bs(hd_table, arg->idebus0);
297 c0897e0c Markus Armbruster
    ide_get_bs(hd_table + 2, arg->idebus1);
298 c0897e0c Markus Armbruster
299 c0897e0c Markus Armbruster
    rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
300 c0897e0c Markus Armbruster
    if (hd_table[0])
301 c0897e0c Markus Armbruster
        cmos_init_hd(0x19, 0x1b, hd_table[0], s);
302 c0897e0c Markus Armbruster
    if (hd_table[1])
303 c0897e0c Markus Armbruster
        cmos_init_hd(0x1a, 0x24, hd_table[1], s);
304 c0897e0c Markus Armbruster
305 c0897e0c Markus Armbruster
    val = 0;
306 c0897e0c Markus Armbruster
    for (i = 0; i < 4; i++) {
307 c0897e0c Markus Armbruster
        if (hd_table[i]) {
308 c0897e0c Markus Armbruster
            int cylinders, heads, sectors, translation;
309 c0897e0c Markus Armbruster
            /* NOTE: bdrv_get_geometry_hint() returns the physical
310 c0897e0c Markus Armbruster
                geometry.  It is always such that: 1 <= sects <= 63, 1
311 c0897e0c Markus Armbruster
                <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
312 c0897e0c Markus Armbruster
                geometry can be different if a translation is done. */
313 c0897e0c Markus Armbruster
            translation = bdrv_get_translation_hint(hd_table[i]);
314 c0897e0c Markus Armbruster
            if (translation == BIOS_ATA_TRANSLATION_AUTO) {
315 c0897e0c Markus Armbruster
                bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
316 c0897e0c Markus Armbruster
                if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
317 c0897e0c Markus Armbruster
                    /* No translation. */
318 c0897e0c Markus Armbruster
                    translation = 0;
319 c0897e0c Markus Armbruster
                } else {
320 c0897e0c Markus Armbruster
                    /* LBA translation. */
321 c0897e0c Markus Armbruster
                    translation = 1;
322 c0897e0c Markus Armbruster
                }
323 c0897e0c Markus Armbruster
            } else {
324 c0897e0c Markus Armbruster
                translation--;
325 c0897e0c Markus Armbruster
            }
326 c0897e0c Markus Armbruster
            val |= translation << (i * 2);
327 c0897e0c Markus Armbruster
        }
328 c0897e0c Markus Armbruster
    }
329 c0897e0c Markus Armbruster
    rtc_set_memory(s, 0x39, val);
330 c0897e0c Markus Armbruster
331 c0897e0c Markus Armbruster
    qemu_unregister_reset(pc_cmos_init_late, opaque);
332 c0897e0c Markus Armbruster
}
333 c0897e0c Markus Armbruster
334 845773ab Isaku Yamahata
void pc_cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
335 c0897e0c Markus Armbruster
                  const char *boot_device,
336 c0897e0c Markus Armbruster
                  BusState *idebus0, BusState *idebus1,
337 63ffb564 Blue Swirl
                  ISADevice *s)
338 80cabfad bellard
{
339 63ffb564 Blue Swirl
    int val, nb, nb_heads, max_track, last_sect, i;
340 63ffb564 Blue Swirl
    FDriveType fd_type[2];
341 63ffb564 Blue Swirl
    DriveInfo *fd[2];
342 c0897e0c Markus Armbruster
    static pc_cmos_init_late_arg arg;
343 b0a21b53 bellard
344 b0a21b53 bellard
    /* various important CMOS locations needed by PC/Bochs bios */
345 80cabfad bellard
346 80cabfad bellard
    /* memory size */
347 333190eb bellard
    val = 640; /* base memory in K */
348 333190eb bellard
    rtc_set_memory(s, 0x15, val);
349 333190eb bellard
    rtc_set_memory(s, 0x16, val >> 8);
350 333190eb bellard
351 80cabfad bellard
    val = (ram_size / 1024) - 1024;
352 80cabfad bellard
    if (val > 65535)
353 80cabfad bellard
        val = 65535;
354 b0a21b53 bellard
    rtc_set_memory(s, 0x17, val);
355 b0a21b53 bellard
    rtc_set_memory(s, 0x18, val >> 8);
356 b0a21b53 bellard
    rtc_set_memory(s, 0x30, val);
357 b0a21b53 bellard
    rtc_set_memory(s, 0x31, val >> 8);
358 80cabfad bellard
359 00f82b8a aurel32
    if (above_4g_mem_size) {
360 00f82b8a aurel32
        rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
361 00f82b8a aurel32
        rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
362 00f82b8a aurel32
        rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
363 00f82b8a aurel32
    }
364 00f82b8a aurel32
365 9da98861 bellard
    if (ram_size > (16 * 1024 * 1024))
366 9da98861 bellard
        val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
367 9da98861 bellard
    else
368 9da98861 bellard
        val = 0;
369 80cabfad bellard
    if (val > 65535)
370 80cabfad bellard
        val = 65535;
371 b0a21b53 bellard
    rtc_set_memory(s, 0x34, val);
372 b0a21b53 bellard
    rtc_set_memory(s, 0x35, val >> 8);
373 3b46e624 ths
374 298e01b6 aurel32
    /* set the number of CPU */
375 298e01b6 aurel32
    rtc_set_memory(s, 0x5f, smp_cpus - 1);
376 298e01b6 aurel32
377 6ac0e82d balrog
    /* set boot devices, and disable floppy signature check if requested */
378 d9346e81 Markus Armbruster
    if (set_boot_dev(s, boot_device, fd_bootchk)) {
379 28c5af54 j_mayer
        exit(1);
380 28c5af54 j_mayer
    }
381 80cabfad bellard
382 b41a2cd1 bellard
    /* floppy type */
383 63ffb564 Blue Swirl
    for (i = 0; i < 2; i++) {
384 63ffb564 Blue Swirl
        fd[i] = drive_get(IF_FLOPPY, 0, i);
385 e14c8062 Blue Swirl
        if (fd[i] && bdrv_is_inserted(fd[i]->bdrv)) {
386 63ffb564 Blue Swirl
            bdrv_get_floppy_geometry_hint(fd[i]->bdrv, &nb_heads, &max_track,
387 63ffb564 Blue Swirl
                                          &last_sect, FDRIVE_DRV_NONE,
388 63ffb564 Blue Swirl
                                          &fd_type[i]);
389 63ffb564 Blue Swirl
        } else {
390 63ffb564 Blue Swirl
            fd_type[i] = FDRIVE_DRV_NONE;
391 63ffb564 Blue Swirl
        }
392 63ffb564 Blue Swirl
    }
393 63ffb564 Blue Swirl
    val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
394 63ffb564 Blue Swirl
        cmos_get_fd_drive_type(fd_type[1]);
395 b0a21b53 bellard
    rtc_set_memory(s, 0x10, val);
396 3b46e624 ths
397 b0a21b53 bellard
    val = 0;
398 b41a2cd1 bellard
    nb = 0;
399 63ffb564 Blue Swirl
    if (fd_type[0] < FDRIVE_DRV_NONE) {
400 80cabfad bellard
        nb++;
401 d288c7ba Blue Swirl
    }
402 63ffb564 Blue Swirl
    if (fd_type[1] < FDRIVE_DRV_NONE) {
403 80cabfad bellard
        nb++;
404 d288c7ba Blue Swirl
    }
405 80cabfad bellard
    switch (nb) {
406 80cabfad bellard
    case 0:
407 80cabfad bellard
        break;
408 80cabfad bellard
    case 1:
409 b0a21b53 bellard
        val |= 0x01; /* 1 drive, ready for boot */
410 80cabfad bellard
        break;
411 80cabfad bellard
    case 2:
412 b0a21b53 bellard
        val |= 0x41; /* 2 drives, ready for boot */
413 80cabfad bellard
        break;
414 80cabfad bellard
    }
415 b0a21b53 bellard
    val |= 0x02; /* FPU is there */
416 b0a21b53 bellard
    val |= 0x04; /* PS/2 mouse installed */
417 b0a21b53 bellard
    rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
418 b0a21b53 bellard
419 ba6c2377 bellard
    /* hard drives */
420 c0897e0c Markus Armbruster
    arg.rtc_state = s;
421 c0897e0c Markus Armbruster
    arg.idebus0 = idebus0;
422 c0897e0c Markus Armbruster
    arg.idebus1 = idebus1;
423 c0897e0c Markus Armbruster
    qemu_register_reset(pc_cmos_init_late, &arg);
424 80cabfad bellard
}
425 80cabfad bellard
426 4b78a802 Blue Swirl
/* port 92 stuff: could be split off */
427 4b78a802 Blue Swirl
typedef struct Port92State {
428 4b78a802 Blue Swirl
    ISADevice dev;
429 4b78a802 Blue Swirl
    uint8_t outport;
430 4b78a802 Blue Swirl
    qemu_irq *a20_out;
431 4b78a802 Blue Swirl
} Port92State;
432 4b78a802 Blue Swirl
433 4b78a802 Blue Swirl
static void port92_write(void *opaque, uint32_t addr, uint32_t val)
434 4b78a802 Blue Swirl
{
435 4b78a802 Blue Swirl
    Port92State *s = opaque;
436 4b78a802 Blue Swirl
437 4b78a802 Blue Swirl
    DPRINTF("port92: write 0x%02x\n", val);
438 4b78a802 Blue Swirl
    s->outport = val;
439 4b78a802 Blue Swirl
    qemu_set_irq(*s->a20_out, (val >> 1) & 1);
440 4b78a802 Blue Swirl
    if (val & 1) {
441 4b78a802 Blue Swirl
        qemu_system_reset_request();
442 4b78a802 Blue Swirl
    }
443 4b78a802 Blue Swirl
}
444 4b78a802 Blue Swirl
445 4b78a802 Blue Swirl
static uint32_t port92_read(void *opaque, uint32_t addr)
446 4b78a802 Blue Swirl
{
447 4b78a802 Blue Swirl
    Port92State *s = opaque;
448 4b78a802 Blue Swirl
    uint32_t ret;
449 4b78a802 Blue Swirl
450 4b78a802 Blue Swirl
    ret = s->outport;
451 4b78a802 Blue Swirl
    DPRINTF("port92: read 0x%02x\n", ret);
452 4b78a802 Blue Swirl
    return ret;
453 4b78a802 Blue Swirl
}
454 4b78a802 Blue Swirl
455 4b78a802 Blue Swirl
static void port92_init(ISADevice *dev, qemu_irq *a20_out)
456 4b78a802 Blue Swirl
{
457 4b78a802 Blue Swirl
    Port92State *s = DO_UPCAST(Port92State, dev, dev);
458 4b78a802 Blue Swirl
459 4b78a802 Blue Swirl
    s->a20_out = a20_out;
460 4b78a802 Blue Swirl
}
461 4b78a802 Blue Swirl
462 4b78a802 Blue Swirl
static const VMStateDescription vmstate_port92_isa = {
463 4b78a802 Blue Swirl
    .name = "port92",
464 4b78a802 Blue Swirl
    .version_id = 1,
465 4b78a802 Blue Swirl
    .minimum_version_id = 1,
466 4b78a802 Blue Swirl
    .minimum_version_id_old = 1,
467 4b78a802 Blue Swirl
    .fields      = (VMStateField []) {
468 4b78a802 Blue Swirl
        VMSTATE_UINT8(outport, Port92State),
469 4b78a802 Blue Swirl
        VMSTATE_END_OF_LIST()
470 4b78a802 Blue Swirl
    }
471 4b78a802 Blue Swirl
};
472 4b78a802 Blue Swirl
473 4b78a802 Blue Swirl
static void port92_reset(DeviceState *d)
474 4b78a802 Blue Swirl
{
475 4b78a802 Blue Swirl
    Port92State *s = container_of(d, Port92State, dev.qdev);
476 4b78a802 Blue Swirl
477 4b78a802 Blue Swirl
    s->outport &= ~1;
478 4b78a802 Blue Swirl
}
479 4b78a802 Blue Swirl
480 4b78a802 Blue Swirl
static int port92_initfn(ISADevice *dev)
481 4b78a802 Blue Swirl
{
482 4b78a802 Blue Swirl
    Port92State *s = DO_UPCAST(Port92State, dev, dev);
483 4b78a802 Blue Swirl
484 4b78a802 Blue Swirl
    register_ioport_read(0x92, 1, 1, port92_read, s);
485 4b78a802 Blue Swirl
    register_ioport_write(0x92, 1, 1, port92_write, s);
486 4b78a802 Blue Swirl
    isa_init_ioport(dev, 0x92);
487 4b78a802 Blue Swirl
    s->outport = 0;
488 4b78a802 Blue Swirl
    return 0;
489 4b78a802 Blue Swirl
}
490 4b78a802 Blue Swirl
491 4b78a802 Blue Swirl
static ISADeviceInfo port92_info = {
492 4b78a802 Blue Swirl
    .qdev.name     = "port92",
493 4b78a802 Blue Swirl
    .qdev.size     = sizeof(Port92State),
494 4b78a802 Blue Swirl
    .qdev.vmsd     = &vmstate_port92_isa,
495 4b78a802 Blue Swirl
    .qdev.no_user  = 1,
496 4b78a802 Blue Swirl
    .qdev.reset    = port92_reset,
497 4b78a802 Blue Swirl
    .init          = port92_initfn,
498 4b78a802 Blue Swirl
};
499 4b78a802 Blue Swirl
500 4b78a802 Blue Swirl
static void port92_register(void)
501 4b78a802 Blue Swirl
{
502 4b78a802 Blue Swirl
    isa_qdev_register(&port92_info);
503 4b78a802 Blue Swirl
}
504 4b78a802 Blue Swirl
device_init(port92_register)
505 4b78a802 Blue Swirl
506 956a3e6b Blue Swirl
static void handle_a20_line_change(void *opaque, int irq, int level)
507 59b8ad81 bellard
{
508 956a3e6b Blue Swirl
    CPUState *cpu = opaque;
509 e1a23744 bellard
510 956a3e6b Blue Swirl
    /* XXX: send to all CPUs ? */
511 4b78a802 Blue Swirl
    /* XXX: add logic to handle multiple A20 line sources */
512 956a3e6b Blue Swirl
    cpu_x86_set_a20(cpu, level);
513 e1a23744 bellard
}
514 e1a23744 bellard
515 80cabfad bellard
/***********************************************************/
516 80cabfad bellard
/* Bochs BIOS debug ports */
517 80cabfad bellard
518 9596ebb7 pbrook
static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
519 80cabfad bellard
{
520 a2f659ee bellard
    static const char shutdown_str[8] = "Shutdown";
521 a2f659ee bellard
    static int shutdown_index = 0;
522 3b46e624 ths
523 80cabfad bellard
    switch(addr) {
524 80cabfad bellard
        /* Bochs BIOS messages */
525 80cabfad bellard
    case 0x400:
526 80cabfad bellard
    case 0x401:
527 0550f9c1 Bernhard Kohl
        /* used to be panic, now unused */
528 0550f9c1 Bernhard Kohl
        break;
529 80cabfad bellard
    case 0x402:
530 80cabfad bellard
    case 0x403:
531 80cabfad bellard
#ifdef DEBUG_BIOS
532 80cabfad bellard
        fprintf(stderr, "%c", val);
533 80cabfad bellard
#endif
534 80cabfad bellard
        break;
535 a2f659ee bellard
    case 0x8900:
536 a2f659ee bellard
        /* same as Bochs power off */
537 a2f659ee bellard
        if (val == shutdown_str[shutdown_index]) {
538 a2f659ee bellard
            shutdown_index++;
539 a2f659ee bellard
            if (shutdown_index == 8) {
540 a2f659ee bellard
                shutdown_index = 0;
541 a2f659ee bellard
                qemu_system_shutdown_request();
542 a2f659ee bellard
            }
543 a2f659ee bellard
        } else {
544 a2f659ee bellard
            shutdown_index = 0;
545 a2f659ee bellard
        }
546 a2f659ee bellard
        break;
547 80cabfad bellard
548 80cabfad bellard
        /* LGPL'ed VGA BIOS messages */
549 80cabfad bellard
    case 0x501:
550 80cabfad bellard
    case 0x502:
551 80cabfad bellard
        fprintf(stderr, "VGA BIOS panic, line %d\n", val);
552 80cabfad bellard
        exit(1);
553 80cabfad bellard
    case 0x500:
554 80cabfad bellard
    case 0x503:
555 80cabfad bellard
#ifdef DEBUG_BIOS
556 80cabfad bellard
        fprintf(stderr, "%c", val);
557 80cabfad bellard
#endif
558 80cabfad bellard
        break;
559 80cabfad bellard
    }
560 80cabfad bellard
}
561 80cabfad bellard
562 4c5b10b7 Jes Sorensen
int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
563 4c5b10b7 Jes Sorensen
{
564 8ca209ad Alex Williamson
    int index = le32_to_cpu(e820_table.count);
565 4c5b10b7 Jes Sorensen
    struct e820_entry *entry;
566 4c5b10b7 Jes Sorensen
567 4c5b10b7 Jes Sorensen
    if (index >= E820_NR_ENTRIES)
568 4c5b10b7 Jes Sorensen
        return -EBUSY;
569 8ca209ad Alex Williamson
    entry = &e820_table.entry[index++];
570 4c5b10b7 Jes Sorensen
571 8ca209ad Alex Williamson
    entry->address = cpu_to_le64(address);
572 8ca209ad Alex Williamson
    entry->length = cpu_to_le64(length);
573 8ca209ad Alex Williamson
    entry->type = cpu_to_le32(type);
574 4c5b10b7 Jes Sorensen
575 8ca209ad Alex Williamson
    e820_table.count = cpu_to_le32(index);
576 8ca209ad Alex Williamson
    return index;
577 4c5b10b7 Jes Sorensen
}
578 4c5b10b7 Jes Sorensen
579 bf483392 Alexander Graf
static void *bochs_bios_init(void)
580 80cabfad bellard
{
581 3cce6243 blueswir1
    void *fw_cfg;
582 b6f6e3d3 aliguori
    uint8_t *smbios_table;
583 b6f6e3d3 aliguori
    size_t smbios_len;
584 11c2fd3e aliguori
    uint64_t *numa_fw_cfg;
585 11c2fd3e aliguori
    int i, j;
586 3cce6243 blueswir1
587 b41a2cd1 bellard
    register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
588 b41a2cd1 bellard
    register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
589 b41a2cd1 bellard
    register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
590 b41a2cd1 bellard
    register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
591 a2f659ee bellard
    register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
592 b41a2cd1 bellard
593 b41a2cd1 bellard
    register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
594 b41a2cd1 bellard
    register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
595 b41a2cd1 bellard
    register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
596 b41a2cd1 bellard
    register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
597 3cce6243 blueswir1
598 3cce6243 blueswir1
    fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
599 bf483392 Alexander Graf
600 3cce6243 blueswir1
    fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
601 905fdcb5 blueswir1
    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
602 80deece2 blueswir1
    fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
603 80deece2 blueswir1
                     acpi_tables_len);
604 6b35e7bf Jes Sorensen
    fw_cfg_add_bytes(fw_cfg, FW_CFG_IRQ0_OVERRIDE, &irq0override, 1);
605 b6f6e3d3 aliguori
606 b6f6e3d3 aliguori
    smbios_table = smbios_get_table(&smbios_len);
607 b6f6e3d3 aliguori
    if (smbios_table)
608 b6f6e3d3 aliguori
        fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
609 b6f6e3d3 aliguori
                         smbios_table, smbios_len);
610 4c5b10b7 Jes Sorensen
    fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE, (uint8_t *)&e820_table,
611 4c5b10b7 Jes Sorensen
                     sizeof(struct e820_table));
612 11c2fd3e aliguori
613 40ac17cd Gleb Natapov
    fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, (uint8_t *)&hpet_cfg,
614 40ac17cd Gleb Natapov
                     sizeof(struct hpet_fw_config));
615 11c2fd3e aliguori
    /* allocate memory for the NUMA channel: one (64bit) word for the number
616 11c2fd3e aliguori
     * of nodes, one word for each VCPU->node and one word for each node to
617 11c2fd3e aliguori
     * hold the amount of memory.
618 11c2fd3e aliguori
     */
619 11c2fd3e aliguori
    numa_fw_cfg = qemu_mallocz((1 + smp_cpus + nb_numa_nodes) * 8);
620 11c2fd3e aliguori
    numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
621 11c2fd3e aliguori
    for (i = 0; i < smp_cpus; i++) {
622 11c2fd3e aliguori
        for (j = 0; j < nb_numa_nodes; j++) {
623 11c2fd3e aliguori
            if (node_cpumask[j] & (1 << i)) {
624 11c2fd3e aliguori
                numa_fw_cfg[i + 1] = cpu_to_le64(j);
625 11c2fd3e aliguori
                break;
626 11c2fd3e aliguori
            }
627 11c2fd3e aliguori
        }
628 11c2fd3e aliguori
    }
629 11c2fd3e aliguori
    for (i = 0; i < nb_numa_nodes; i++) {
630 11c2fd3e aliguori
        numa_fw_cfg[smp_cpus + 1 + i] = cpu_to_le64(node_mem[i]);
631 11c2fd3e aliguori
    }
632 11c2fd3e aliguori
    fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, (uint8_t *)numa_fw_cfg,
633 11c2fd3e aliguori
                     (1 + smp_cpus + nb_numa_nodes) * 8);
634 bf483392 Alexander Graf
635 bf483392 Alexander Graf
    return fw_cfg;
636 80cabfad bellard
}
637 80cabfad bellard
638 642a4f96 ths
static long get_file_size(FILE *f)
639 642a4f96 ths
{
640 642a4f96 ths
    long where, size;
641 642a4f96 ths
642 642a4f96 ths
    /* XXX: on Unix systems, using fstat() probably makes more sense */
643 642a4f96 ths
644 642a4f96 ths
    where = ftell(f);
645 642a4f96 ths
    fseek(f, 0, SEEK_END);
646 642a4f96 ths
    size = ftell(f);
647 642a4f96 ths
    fseek(f, where, SEEK_SET);
648 642a4f96 ths
649 642a4f96 ths
    return size;
650 642a4f96 ths
}
651 642a4f96 ths
652 f16408df Alexander Graf
static void load_linux(void *fw_cfg,
653 4fc9af53 aliguori
                       const char *kernel_filename,
654 642a4f96 ths
                       const char *initrd_filename,
655 e6ade764 Glauber Costa
                       const char *kernel_cmdline,
656 45a50b16 Gerd Hoffmann
                       target_phys_addr_t max_ram_size)
657 642a4f96 ths
{
658 642a4f96 ths
    uint16_t protocol;
659 5cea8590 Paul Brook
    int setup_size, kernel_size, initrd_size = 0, cmdline_size;
660 642a4f96 ths
    uint32_t initrd_max;
661 57a46d05 Alexander Graf
    uint8_t header[8192], *setup, *kernel, *initrd_data;
662 c227f099 Anthony Liguori
    target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
663 45a50b16 Gerd Hoffmann
    FILE *f;
664 bf4e5d92 Pascal Terjan
    char *vmode;
665 642a4f96 ths
666 642a4f96 ths
    /* Align to 16 bytes as a paranoia measure */
667 642a4f96 ths
    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
668 642a4f96 ths
669 642a4f96 ths
    /* load the kernel header */
670 642a4f96 ths
    f = fopen(kernel_filename, "rb");
671 642a4f96 ths
    if (!f || !(kernel_size = get_file_size(f)) ||
672 f16408df Alexander Graf
        fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
673 f16408df Alexander Graf
        MIN(ARRAY_SIZE(header), kernel_size)) {
674 850810d0 Justin M. Forbes
        fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
675 850810d0 Justin M. Forbes
                kernel_filename, strerror(errno));
676 642a4f96 ths
        exit(1);
677 642a4f96 ths
    }
678 642a4f96 ths
679 642a4f96 ths
    /* kernel protocol version */
680 bc4edd79 bellard
#if 0
681 642a4f96 ths
    fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
682 bc4edd79 bellard
#endif
683 642a4f96 ths
    if (ldl_p(header+0x202) == 0x53726448)
684 642a4f96 ths
        protocol = lduw_p(header+0x206);
685 f16408df Alexander Graf
    else {
686 f16408df Alexander Graf
        /* This looks like a multiboot kernel. If it is, let's stop
687 f16408df Alexander Graf
           treating it like a Linux kernel. */
688 52001445 Adam Lackorzynski
        if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
689 52001445 Adam Lackorzynski
                           kernel_cmdline, kernel_size, header))
690 82663ee2 Blue Swirl
            return;
691 642a4f96 ths
        protocol = 0;
692 f16408df Alexander Graf
    }
693 642a4f96 ths
694 642a4f96 ths
    if (protocol < 0x200 || !(header[0x211] & 0x01)) {
695 642a4f96 ths
        /* Low kernel */
696 a37af289 blueswir1
        real_addr    = 0x90000;
697 a37af289 blueswir1
        cmdline_addr = 0x9a000 - cmdline_size;
698 a37af289 blueswir1
        prot_addr    = 0x10000;
699 642a4f96 ths
    } else if (protocol < 0x202) {
700 642a4f96 ths
        /* High but ancient kernel */
701 a37af289 blueswir1
        real_addr    = 0x90000;
702 a37af289 blueswir1
        cmdline_addr = 0x9a000 - cmdline_size;
703 a37af289 blueswir1
        prot_addr    = 0x100000;
704 642a4f96 ths
    } else {
705 642a4f96 ths
        /* High and recent kernel */
706 a37af289 blueswir1
        real_addr    = 0x10000;
707 a37af289 blueswir1
        cmdline_addr = 0x20000;
708 a37af289 blueswir1
        prot_addr    = 0x100000;
709 642a4f96 ths
    }
710 642a4f96 ths
711 bc4edd79 bellard
#if 0
712 642a4f96 ths
    fprintf(stderr,
713 526ccb7a balrog
            "qemu: real_addr     = 0x" TARGET_FMT_plx "\n"
714 526ccb7a balrog
            "qemu: cmdline_addr  = 0x" TARGET_FMT_plx "\n"
715 526ccb7a balrog
            "qemu: prot_addr     = 0x" TARGET_FMT_plx "\n",
716 a37af289 blueswir1
            real_addr,
717 a37af289 blueswir1
            cmdline_addr,
718 a37af289 blueswir1
            prot_addr);
719 bc4edd79 bellard
#endif
720 642a4f96 ths
721 642a4f96 ths
    /* highest address for loading the initrd */
722 642a4f96 ths
    if (protocol >= 0x203)
723 642a4f96 ths
        initrd_max = ldl_p(header+0x22c);
724 642a4f96 ths
    else
725 642a4f96 ths
        initrd_max = 0x37ffffff;
726 642a4f96 ths
727 e6ade764 Glauber Costa
    if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
728 e6ade764 Glauber Costa
            initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
729 642a4f96 ths
730 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
731 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
732 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_CMDLINE_DATA,
733 57a46d05 Alexander Graf
                     (uint8_t*)strdup(kernel_cmdline),
734 57a46d05 Alexander Graf
                     strlen(kernel_cmdline)+1);
735 642a4f96 ths
736 642a4f96 ths
    if (protocol >= 0x202) {
737 a37af289 blueswir1
        stl_p(header+0x228, cmdline_addr);
738 642a4f96 ths
    } else {
739 642a4f96 ths
        stw_p(header+0x20, 0xA33F);
740 642a4f96 ths
        stw_p(header+0x22, cmdline_addr-real_addr);
741 642a4f96 ths
    }
742 642a4f96 ths
743 bf4e5d92 Pascal Terjan
    /* handle vga= parameter */
744 bf4e5d92 Pascal Terjan
    vmode = strstr(kernel_cmdline, "vga=");
745 bf4e5d92 Pascal Terjan
    if (vmode) {
746 bf4e5d92 Pascal Terjan
        unsigned int video_mode;
747 bf4e5d92 Pascal Terjan
        /* skip "vga=" */
748 bf4e5d92 Pascal Terjan
        vmode += 4;
749 bf4e5d92 Pascal Terjan
        if (!strncmp(vmode, "normal", 6)) {
750 bf4e5d92 Pascal Terjan
            video_mode = 0xffff;
751 bf4e5d92 Pascal Terjan
        } else if (!strncmp(vmode, "ext", 3)) {
752 bf4e5d92 Pascal Terjan
            video_mode = 0xfffe;
753 bf4e5d92 Pascal Terjan
        } else if (!strncmp(vmode, "ask", 3)) {
754 bf4e5d92 Pascal Terjan
            video_mode = 0xfffd;
755 bf4e5d92 Pascal Terjan
        } else {
756 bf4e5d92 Pascal Terjan
            video_mode = strtol(vmode, NULL, 0);
757 bf4e5d92 Pascal Terjan
        }
758 bf4e5d92 Pascal Terjan
        stw_p(header+0x1fa, video_mode);
759 bf4e5d92 Pascal Terjan
    }
760 bf4e5d92 Pascal Terjan
761 642a4f96 ths
    /* loader type */
762 642a4f96 ths
    /* High nybble = B reserved for Qemu; low nybble is revision number.
763 642a4f96 ths
       If this code is substantially changed, you may want to consider
764 642a4f96 ths
       incrementing the revision. */
765 642a4f96 ths
    if (protocol >= 0x200)
766 642a4f96 ths
        header[0x210] = 0xB0;
767 642a4f96 ths
768 642a4f96 ths
    /* heap */
769 642a4f96 ths
    if (protocol >= 0x201) {
770 642a4f96 ths
        header[0x211] |= 0x80;        /* CAN_USE_HEAP */
771 642a4f96 ths
        stw_p(header+0x224, cmdline_addr-real_addr-0x200);
772 642a4f96 ths
    }
773 642a4f96 ths
774 642a4f96 ths
    /* load initrd */
775 642a4f96 ths
    if (initrd_filename) {
776 642a4f96 ths
        if (protocol < 0x200) {
777 642a4f96 ths
            fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
778 642a4f96 ths
            exit(1);
779 642a4f96 ths
        }
780 642a4f96 ths
781 45a50b16 Gerd Hoffmann
        initrd_size = get_image_size(initrd_filename);
782 d6fa4b77 M. Mohan Kumar
        if (initrd_size < 0) {
783 d6fa4b77 M. Mohan Kumar
            fprintf(stderr, "qemu: error reading initrd %s\n",
784 d6fa4b77 M. Mohan Kumar
                    initrd_filename);
785 d6fa4b77 M. Mohan Kumar
            exit(1);
786 d6fa4b77 M. Mohan Kumar
        }
787 d6fa4b77 M. Mohan Kumar
788 45a50b16 Gerd Hoffmann
        initrd_addr = (initrd_max-initrd_size) & ~4095;
789 57a46d05 Alexander Graf
790 57a46d05 Alexander Graf
        initrd_data = qemu_malloc(initrd_size);
791 57a46d05 Alexander Graf
        load_image(initrd_filename, initrd_data);
792 57a46d05 Alexander Graf
793 57a46d05 Alexander Graf
        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
794 57a46d05 Alexander Graf
        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
795 57a46d05 Alexander Graf
        fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
796 642a4f96 ths
797 a37af289 blueswir1
        stl_p(header+0x218, initrd_addr);
798 642a4f96 ths
        stl_p(header+0x21c, initrd_size);
799 642a4f96 ths
    }
800 642a4f96 ths
801 45a50b16 Gerd Hoffmann
    /* load kernel and setup */
802 642a4f96 ths
    setup_size = header[0x1f1];
803 642a4f96 ths
    if (setup_size == 0)
804 642a4f96 ths
        setup_size = 4;
805 642a4f96 ths
    setup_size = (setup_size+1)*512;
806 45a50b16 Gerd Hoffmann
    kernel_size -= setup_size;
807 642a4f96 ths
808 45a50b16 Gerd Hoffmann
    setup  = qemu_malloc(setup_size);
809 45a50b16 Gerd Hoffmann
    kernel = qemu_malloc(kernel_size);
810 45a50b16 Gerd Hoffmann
    fseek(f, 0, SEEK_SET);
811 5a41ecc5 Kirill A. Shutemov
    if (fread(setup, 1, setup_size, f) != setup_size) {
812 5a41ecc5 Kirill A. Shutemov
        fprintf(stderr, "fread() failed\n");
813 5a41ecc5 Kirill A. Shutemov
        exit(1);
814 5a41ecc5 Kirill A. Shutemov
    }
815 5a41ecc5 Kirill A. Shutemov
    if (fread(kernel, 1, kernel_size, f) != kernel_size) {
816 5a41ecc5 Kirill A. Shutemov
        fprintf(stderr, "fread() failed\n");
817 5a41ecc5 Kirill A. Shutemov
        exit(1);
818 5a41ecc5 Kirill A. Shutemov
    }
819 642a4f96 ths
    fclose(f);
820 45a50b16 Gerd Hoffmann
    memcpy(setup, header, MIN(sizeof(header), setup_size));
821 57a46d05 Alexander Graf
822 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
823 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
824 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
825 57a46d05 Alexander Graf
826 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
827 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
828 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
829 57a46d05 Alexander Graf
830 2e55e842 Gleb Natapov
    option_rom[nb_option_roms].name = "linuxboot.bin";
831 2e55e842 Gleb Natapov
    option_rom[nb_option_roms].bootindex = 0;
832 57a46d05 Alexander Graf
    nb_option_roms++;
833 642a4f96 ths
}
834 642a4f96 ths
835 b41a2cd1 bellard
#define NE2000_NB_MAX 6
836 b41a2cd1 bellard
837 675d6f82 Blue Swirl
static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
838 675d6f82 Blue Swirl
                                              0x280, 0x380 };
839 675d6f82 Blue Swirl
static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
840 b41a2cd1 bellard
841 675d6f82 Blue Swirl
static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
842 675d6f82 Blue Swirl
static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
843 6508fe59 bellard
844 845773ab Isaku Yamahata
void pc_init_ne2k_isa(NICInfo *nd)
845 a41b2ff2 pbrook
{
846 a41b2ff2 pbrook
    static int nb_ne2k = 0;
847 a41b2ff2 pbrook
848 a41b2ff2 pbrook
    if (nb_ne2k == NE2000_NB_MAX)
849 a41b2ff2 pbrook
        return;
850 3a38d437 Jes Sorensen
    isa_ne2000_init(ne2000_io[nb_ne2k],
851 9453c5bc Gerd Hoffmann
                    ne2000_irq[nb_ne2k], nd);
852 a41b2ff2 pbrook
    nb_ne2k++;
853 a41b2ff2 pbrook
}
854 a41b2ff2 pbrook
855 678e12cc Gleb Natapov
int cpu_is_bsp(CPUState *env)
856 678e12cc Gleb Natapov
{
857 6cb2996c Jan Kiszka
    /* We hard-wire the BSP to the first CPU. */
858 6cb2996c Jan Kiszka
    return env->cpu_index == 0;
859 678e12cc Gleb Natapov
}
860 678e12cc Gleb Natapov
861 92a16d7a Blue Swirl
DeviceState *cpu_get_current_apic(void)
862 0e26b7b8 Blue Swirl
{
863 0e26b7b8 Blue Swirl
    if (cpu_single_env) {
864 0e26b7b8 Blue Swirl
        return cpu_single_env->apic_state;
865 0e26b7b8 Blue Swirl
    } else {
866 0e26b7b8 Blue Swirl
        return NULL;
867 0e26b7b8 Blue Swirl
    }
868 0e26b7b8 Blue Swirl
}
869 0e26b7b8 Blue Swirl
870 92a16d7a Blue Swirl
static DeviceState *apic_init(void *env, uint8_t apic_id)
871 92a16d7a Blue Swirl
{
872 92a16d7a Blue Swirl
    DeviceState *dev;
873 92a16d7a Blue Swirl
    SysBusDevice *d;
874 92a16d7a Blue Swirl
    static int apic_mapped;
875 92a16d7a Blue Swirl
876 92a16d7a Blue Swirl
    dev = qdev_create(NULL, "apic");
877 92a16d7a Blue Swirl
    qdev_prop_set_uint8(dev, "id", apic_id);
878 92a16d7a Blue Swirl
    qdev_prop_set_ptr(dev, "cpu_env", env);
879 92a16d7a Blue Swirl
    qdev_init_nofail(dev);
880 92a16d7a Blue Swirl
    d = sysbus_from_qdev(dev);
881 92a16d7a Blue Swirl
882 92a16d7a Blue Swirl
    /* XXX: mapping more APICs at the same memory location */
883 92a16d7a Blue Swirl
    if (apic_mapped == 0) {
884 92a16d7a Blue Swirl
        /* NOTE: the APIC is directly connected to the CPU - it is not
885 92a16d7a Blue Swirl
           on the global memory bus. */
886 92a16d7a Blue Swirl
        /* XXX: what if the base changes? */
887 92a16d7a Blue Swirl
        sysbus_mmio_map(d, 0, MSI_ADDR_BASE);
888 92a16d7a Blue Swirl
        apic_mapped = 1;
889 92a16d7a Blue Swirl
    }
890 92a16d7a Blue Swirl
891 92a16d7a Blue Swirl
    msix_supported = 1;
892 92a16d7a Blue Swirl
893 92a16d7a Blue Swirl
    return dev;
894 92a16d7a Blue Swirl
}
895 92a16d7a Blue Swirl
896 53b67b30 Blue Swirl
/* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
897 53b67b30 Blue Swirl
   BIOS will read it and start S3 resume at POST Entry */
898 845773ab Isaku Yamahata
void pc_cmos_set_s3_resume(void *opaque, int irq, int level)
899 53b67b30 Blue Swirl
{
900 1d914fa0 Isaku Yamahata
    ISADevice *s = opaque;
901 53b67b30 Blue Swirl
902 53b67b30 Blue Swirl
    if (level) {
903 53b67b30 Blue Swirl
        rtc_set_memory(s, 0xF, 0xFE);
904 53b67b30 Blue Swirl
    }
905 53b67b30 Blue Swirl
}
906 53b67b30 Blue Swirl
907 845773ab Isaku Yamahata
void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
908 53b67b30 Blue Swirl
{
909 53b67b30 Blue Swirl
    CPUState *s = opaque;
910 53b67b30 Blue Swirl
911 53b67b30 Blue Swirl
    if (level) {
912 53b67b30 Blue Swirl
        cpu_interrupt(s, CPU_INTERRUPT_SMI);
913 53b67b30 Blue Swirl
    }
914 53b67b30 Blue Swirl
}
915 53b67b30 Blue Swirl
916 427bd8d6 Jan Kiszka
static void pc_cpu_reset(void *opaque)
917 0e26b7b8 Blue Swirl
{
918 0e26b7b8 Blue Swirl
    CPUState *env = opaque;
919 0e26b7b8 Blue Swirl
920 0e26b7b8 Blue Swirl
    cpu_reset(env);
921 427bd8d6 Jan Kiszka
    env->halted = !cpu_is_bsp(env);
922 0e26b7b8 Blue Swirl
}
923 0e26b7b8 Blue Swirl
924 3a31f36a Jan Kiszka
static CPUState *pc_new_cpu(const char *cpu_model)
925 3a31f36a Jan Kiszka
{
926 3a31f36a Jan Kiszka
    CPUState *env;
927 3a31f36a Jan Kiszka
928 3a31f36a Jan Kiszka
    env = cpu_init(cpu_model);
929 3a31f36a Jan Kiszka
    if (!env) {
930 3a31f36a Jan Kiszka
        fprintf(stderr, "Unable to find x86 CPU definition\n");
931 3a31f36a Jan Kiszka
        exit(1);
932 3a31f36a Jan Kiszka
    }
933 3a31f36a Jan Kiszka
    if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) {
934 3a31f36a Jan Kiszka
        env->cpuid_apic_id = env->cpu_index;
935 0e26b7b8 Blue Swirl
        env->apic_state = apic_init(env, env->cpuid_apic_id);
936 0e26b7b8 Blue Swirl
    }
937 427bd8d6 Jan Kiszka
    qemu_register_reset(pc_cpu_reset, env);
938 427bd8d6 Jan Kiszka
    pc_cpu_reset(env);
939 3a31f36a Jan Kiszka
    return env;
940 3a31f36a Jan Kiszka
}
941 3a31f36a Jan Kiszka
942 845773ab Isaku Yamahata
void pc_cpus_init(const char *cpu_model)
943 70166477 Isaku Yamahata
{
944 70166477 Isaku Yamahata
    int i;
945 70166477 Isaku Yamahata
946 70166477 Isaku Yamahata
    /* init CPUs */
947 70166477 Isaku Yamahata
    if (cpu_model == NULL) {
948 70166477 Isaku Yamahata
#ifdef TARGET_X86_64
949 70166477 Isaku Yamahata
        cpu_model = "qemu64";
950 70166477 Isaku Yamahata
#else
951 70166477 Isaku Yamahata
        cpu_model = "qemu32";
952 70166477 Isaku Yamahata
#endif
953 70166477 Isaku Yamahata
    }
954 70166477 Isaku Yamahata
955 70166477 Isaku Yamahata
    for(i = 0; i < smp_cpus; i++) {
956 70166477 Isaku Yamahata
        pc_new_cpu(cpu_model);
957 70166477 Isaku Yamahata
    }
958 70166477 Isaku Yamahata
}
959 70166477 Isaku Yamahata
960 845773ab Isaku Yamahata
void pc_memory_init(ram_addr_t ram_size,
961 845773ab Isaku Yamahata
                    const char *kernel_filename,
962 845773ab Isaku Yamahata
                    const char *kernel_cmdline,
963 845773ab Isaku Yamahata
                    const char *initrd_filename,
964 845773ab Isaku Yamahata
                    ram_addr_t *below_4g_mem_size_p,
965 845773ab Isaku Yamahata
                    ram_addr_t *above_4g_mem_size_p)
966 80cabfad bellard
{
967 5cea8590 Paul Brook
    char *filename;
968 642a4f96 ths
    int ret, linux_boot, i;
969 c227f099 Anthony Liguori
    ram_addr_t ram_addr, bios_offset, option_rom_offset;
970 c227f099 Anthony Liguori
    ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
971 45a50b16 Gerd Hoffmann
    int bios_size, isa_bios_size;
972 81a204e4 Eduard - Gabriel Munteanu
    void *fw_cfg;
973 d592d303 bellard
974 00f82b8a aurel32
    if (ram_size >= 0xe0000000 ) {
975 00f82b8a aurel32
        above_4g_mem_size = ram_size - 0xe0000000;
976 00f82b8a aurel32
        below_4g_mem_size = 0xe0000000;
977 00f82b8a aurel32
    } else {
978 00f82b8a aurel32
        below_4g_mem_size = ram_size;
979 00f82b8a aurel32
    }
980 3d53f5c3 Isaku Yamahata
    *above_4g_mem_size_p = above_4g_mem_size;
981 3d53f5c3 Isaku Yamahata
    *below_4g_mem_size_p = below_4g_mem_size;
982 00f82b8a aurel32
983 80cabfad bellard
    linux_boot = (kernel_filename != NULL);
984 80cabfad bellard
985 80cabfad bellard
    /* allocate RAM */
986 1724f049 Alex Williamson
    ram_addr = qemu_ram_alloc(NULL, "pc.ram",
987 1724f049 Alex Williamson
                              below_4g_mem_size + above_4g_mem_size);
988 82b36dc3 aliguori
    cpu_register_physical_memory(0, 0xa0000, ram_addr);
989 82b36dc3 aliguori
    cpu_register_physical_memory(0x100000,
990 82b36dc3 aliguori
                 below_4g_mem_size - 0x100000,
991 60e4c631 Avi Kivity
                 ram_addr + 0x100000);
992 bbe80adf Alex Williamson
    if (above_4g_mem_size > 0) {
993 bbe80adf Alex Williamson
        cpu_register_physical_memory(0x100000000ULL, above_4g_mem_size,
994 bbe80adf Alex Williamson
                                     ram_addr + below_4g_mem_size);
995 bbe80adf Alex Williamson
    }
996 82b36dc3 aliguori
997 970ac5a3 bellard
    /* BIOS load */
998 1192dad8 j_mayer
    if (bios_name == NULL)
999 1192dad8 j_mayer
        bios_name = BIOS_FILENAME;
1000 5cea8590 Paul Brook
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1001 5cea8590 Paul Brook
    if (filename) {
1002 5cea8590 Paul Brook
        bios_size = get_image_size(filename);
1003 5cea8590 Paul Brook
    } else {
1004 5cea8590 Paul Brook
        bios_size = -1;
1005 5cea8590 Paul Brook
    }
1006 5fafdf24 ths
    if (bios_size <= 0 ||
1007 970ac5a3 bellard
        (bios_size % 65536) != 0) {
1008 7587cf44 bellard
        goto bios_error;
1009 7587cf44 bellard
    }
1010 1724f049 Alex Williamson
    bios_offset = qemu_ram_alloc(NULL, "pc.bios", bios_size);
1011 2e55e842 Gleb Natapov
    ret = rom_add_file_fixed(bios_name, (uint32_t)(-bios_size), -1);
1012 51edd4e6 Gerd Hoffmann
    if (ret != 0) {
1013 7587cf44 bellard
    bios_error:
1014 5cea8590 Paul Brook
        fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
1015 80cabfad bellard
        exit(1);
1016 80cabfad bellard
    }
1017 5cea8590 Paul Brook
    if (filename) {
1018 5cea8590 Paul Brook
        qemu_free(filename);
1019 5cea8590 Paul Brook
    }
1020 7587cf44 bellard
    /* map the last 128KB of the BIOS in ISA space */
1021 7587cf44 bellard
    isa_bios_size = bios_size;
1022 7587cf44 bellard
    if (isa_bios_size > (128 * 1024))
1023 7587cf44 bellard
        isa_bios_size = 128 * 1024;
1024 5fafdf24 ths
    cpu_register_physical_memory(0x100000 - isa_bios_size,
1025 5fafdf24 ths
                                 isa_bios_size,
1026 7587cf44 bellard
                                 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
1027 9ae02555 ths
1028 1724f049 Alex Williamson
    option_rom_offset = qemu_ram_alloc(NULL, "pc.rom", PC_ROM_SIZE);
1029 45a50b16 Gerd Hoffmann
    cpu_register_physical_memory(PC_ROM_MIN_VGA, PC_ROM_SIZE, option_rom_offset);
1030 f753ff16 pbrook
1031 1d108d97 Alexander Graf
    /* map all the bios at the top of memory */
1032 1d108d97 Alexander Graf
    cpu_register_physical_memory((uint32_t)(-bios_size),
1033 1d108d97 Alexander Graf
                                 bios_size, bios_offset | IO_MEM_ROM);
1034 1d108d97 Alexander Graf
1035 bf483392 Alexander Graf
    fw_cfg = bochs_bios_init();
1036 8832cb80 Gerd Hoffmann
    rom_set_fw(fw_cfg);
1037 1d108d97 Alexander Graf
1038 f753ff16 pbrook
    if (linux_boot) {
1039 81a204e4 Eduard - Gabriel Munteanu
        load_linux(fw_cfg, kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
1040 f753ff16 pbrook
    }
1041 f753ff16 pbrook
1042 f753ff16 pbrook
    for (i = 0; i < nb_option_roms; i++) {
1043 2e55e842 Gleb Natapov
        rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1044 406c8df3 Glauber Costa
    }
1045 3d53f5c3 Isaku Yamahata
}
1046 3d53f5c3 Isaku Yamahata
1047 845773ab Isaku Yamahata
qemu_irq *pc_allocate_cpu_irq(void)
1048 845773ab Isaku Yamahata
{
1049 845773ab Isaku Yamahata
    return qemu_allocate_irqs(pic_irq_request, NULL, 1);
1050 845773ab Isaku Yamahata
}
1051 845773ab Isaku Yamahata
1052 845773ab Isaku Yamahata
void pc_vga_init(PCIBus *pci_bus)
1053 765d7908 Isaku Yamahata
{
1054 765d7908 Isaku Yamahata
    if (cirrus_vga_enabled) {
1055 765d7908 Isaku Yamahata
        if (pci_bus) {
1056 765d7908 Isaku Yamahata
            pci_cirrus_vga_init(pci_bus);
1057 765d7908 Isaku Yamahata
        } else {
1058 765d7908 Isaku Yamahata
            isa_cirrus_vga_init();
1059 765d7908 Isaku Yamahata
        }
1060 765d7908 Isaku Yamahata
    } else if (vmsvga_enabled) {
1061 7ba7e49e Blue Swirl
        if (pci_bus) {
1062 7ba7e49e Blue Swirl
            if (!pci_vmsvga_init(pci_bus)) {
1063 7ba7e49e Blue Swirl
                fprintf(stderr, "Warning: vmware_vga not available,"
1064 7ba7e49e Blue Swirl
                        " using standard VGA instead\n");
1065 7ba7e49e Blue Swirl
                pci_vga_init(pci_bus);
1066 7ba7e49e Blue Swirl
            }
1067 7ba7e49e Blue Swirl
        } else {
1068 765d7908 Isaku Yamahata
            fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
1069 7ba7e49e Blue Swirl
        }
1070 a19cbfb3 Gerd Hoffmann
#ifdef CONFIG_SPICE
1071 a19cbfb3 Gerd Hoffmann
    } else if (qxl_enabled) {
1072 a19cbfb3 Gerd Hoffmann
        if (pci_bus)
1073 a19cbfb3 Gerd Hoffmann
            pci_create_simple(pci_bus, -1, "qxl-vga");
1074 a19cbfb3 Gerd Hoffmann
        else
1075 a19cbfb3 Gerd Hoffmann
            fprintf(stderr, "%s: qxl: no PCI bus\n", __FUNCTION__);
1076 a19cbfb3 Gerd Hoffmann
#endif
1077 765d7908 Isaku Yamahata
    } else if (std_vga_enabled) {
1078 765d7908 Isaku Yamahata
        if (pci_bus) {
1079 78895427 Gerd Hoffmann
            pci_vga_init(pci_bus);
1080 765d7908 Isaku Yamahata
        } else {
1081 765d7908 Isaku Yamahata
            isa_vga_init();
1082 765d7908 Isaku Yamahata
        }
1083 765d7908 Isaku Yamahata
    }
1084 765d7908 Isaku Yamahata
}
1085 765d7908 Isaku Yamahata
1086 4556bd8b Blue Swirl
static void cpu_request_exit(void *opaque, int irq, int level)
1087 4556bd8b Blue Swirl
{
1088 4556bd8b Blue Swirl
    CPUState *env = cpu_single_env;
1089 4556bd8b Blue Swirl
1090 4556bd8b Blue Swirl
    if (env && level) {
1091 4556bd8b Blue Swirl
        cpu_exit(env);
1092 4556bd8b Blue Swirl
    }
1093 4556bd8b Blue Swirl
}
1094 4556bd8b Blue Swirl
1095 845773ab Isaku Yamahata
void pc_basic_device_init(qemu_irq *isa_irq,
1096 1d914fa0 Isaku Yamahata
                          ISADevice **rtc_state)
1097 ffe513da Isaku Yamahata
{
1098 ffe513da Isaku Yamahata
    int i;
1099 ffe513da Isaku Yamahata
    DriveInfo *fd[MAX_FD];
1100 7d932dfd Jan Kiszka
    qemu_irq rtc_irq = NULL;
1101 956a3e6b Blue Swirl
    qemu_irq *a20_line;
1102 64d7e9a4 Blue Swirl
    ISADevice *i8042, *port92, *vmmouse, *pit;
1103 4556bd8b Blue Swirl
    qemu_irq *cpu_exit_irq;
1104 ffe513da Isaku Yamahata
1105 ffe513da Isaku Yamahata
    register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
1106 ffe513da Isaku Yamahata
1107 ffe513da Isaku Yamahata
    register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
1108 ffe513da Isaku Yamahata
1109 ffe513da Isaku Yamahata
    if (!no_hpet) {
1110 dd703b99 Blue Swirl
        DeviceState *hpet = sysbus_try_create_simple("hpet", HPET_BASE, NULL);
1111 822557eb Jan Kiszka
1112 dd703b99 Blue Swirl
        if (hpet) {
1113 dd703b99 Blue Swirl
            for (i = 0; i < 24; i++) {
1114 dd703b99 Blue Swirl
                sysbus_connect_irq(sysbus_from_qdev(hpet), i, isa_irq[i]);
1115 dd703b99 Blue Swirl
            }
1116 dd703b99 Blue Swirl
            rtc_irq = qdev_get_gpio_in(hpet, 0);
1117 822557eb Jan Kiszka
        }
1118 ffe513da Isaku Yamahata
    }
1119 7d932dfd Jan Kiszka
    *rtc_state = rtc_init(2000, rtc_irq);
1120 7d932dfd Jan Kiszka
1121 7d932dfd Jan Kiszka
    qemu_register_boot_set(pc_boot_set, *rtc_state);
1122 7d932dfd Jan Kiszka
1123 64d7e9a4 Blue Swirl
    pit = pit_init(0x40, 0);
1124 7d932dfd Jan Kiszka
    pcspk_init(pit);
1125 ffe513da Isaku Yamahata
1126 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
1127 ffe513da Isaku Yamahata
        if (serial_hds[i]) {
1128 ffe513da Isaku Yamahata
            serial_isa_init(i, serial_hds[i]);
1129 ffe513da Isaku Yamahata
        }
1130 ffe513da Isaku Yamahata
    }
1131 ffe513da Isaku Yamahata
1132 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
1133 ffe513da Isaku Yamahata
        if (parallel_hds[i]) {
1134 ffe513da Isaku Yamahata
            parallel_init(i, parallel_hds[i]);
1135 ffe513da Isaku Yamahata
        }
1136 ffe513da Isaku Yamahata
    }
1137 ffe513da Isaku Yamahata
1138 4b78a802 Blue Swirl
    a20_line = qemu_allocate_irqs(handle_a20_line_change, first_cpu, 2);
1139 956a3e6b Blue Swirl
    i8042 = isa_create_simple("i8042");
1140 4b78a802 Blue Swirl
    i8042_setup_a20_line(i8042, &a20_line[0]);
1141 6872ef61 Blue Swirl
    vmport_init();
1142 86d86414 Blue Swirl
    vmmouse = isa_try_create("vmmouse");
1143 86d86414 Blue Swirl
    if (vmmouse) {
1144 86d86414 Blue Swirl
        qdev_prop_set_ptr(&vmmouse->qdev, "ps2_mouse", i8042);
1145 86d86414 Blue Swirl
    }
1146 4b78a802 Blue Swirl
    port92 = isa_create_simple("port92");
1147 4b78a802 Blue Swirl
    port92_init(port92, &a20_line[1]);
1148 956a3e6b Blue Swirl
1149 4556bd8b Blue Swirl
    cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
1150 4556bd8b Blue Swirl
    DMA_init(0, cpu_exit_irq);
1151 ffe513da Isaku Yamahata
1152 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_FD; i++) {
1153 ffe513da Isaku Yamahata
        fd[i] = drive_get(IF_FLOPPY, 0, i);
1154 ffe513da Isaku Yamahata
    }
1155 63ffb564 Blue Swirl
    fdctrl_init_isa(fd);
1156 ffe513da Isaku Yamahata
}
1157 ffe513da Isaku Yamahata
1158 845773ab Isaku Yamahata
void pc_pci_device_init(PCIBus *pci_bus)
1159 e3a5cf42 Isaku Yamahata
{
1160 e3a5cf42 Isaku Yamahata
    int max_bus;
1161 e3a5cf42 Isaku Yamahata
    int bus;
1162 e3a5cf42 Isaku Yamahata
1163 e3a5cf42 Isaku Yamahata
    max_bus = drive_get_max_bus(IF_SCSI);
1164 e3a5cf42 Isaku Yamahata
    for (bus = 0; bus <= max_bus; bus++) {
1165 e3a5cf42 Isaku Yamahata
        pci_create_simple(pci_bus, -1, "lsi53c895a");
1166 e3a5cf42 Isaku Yamahata
    }
1167 e3a5cf42 Isaku Yamahata
}