Statistics
| Branch: | Revision:

root / hw / pc.c @ 3871481c

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