Statistics
| Branch: | Revision:

root / hw / i386 / pc.c @ 2c9b15ca

History | View | Annotate | Download (36.1 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 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
25 0d09e41a Paolo Bonzini
#include "hw/i386/pc.h"
26 0d09e41a Paolo Bonzini
#include "hw/char/serial.h"
27 0d09e41a Paolo Bonzini
#include "hw/i386/apic.h"
28 0d09e41a Paolo Bonzini
#include "hw/block/fdc.h"
29 83c9f4ca Paolo Bonzini
#include "hw/ide.h"
30 83c9f4ca Paolo Bonzini
#include "hw/pci/pci.h"
31 83c9089e Paolo Bonzini
#include "monitor/monitor.h"
32 0d09e41a Paolo Bonzini
#include "hw/nvram/fw_cfg.h"
33 0d09e41a Paolo Bonzini
#include "hw/timer/hpet.h"
34 0d09e41a Paolo Bonzini
#include "hw/i386/smbios.h"
35 83c9f4ca Paolo Bonzini
#include "hw/loader.h"
36 ca20cf32 Blue Swirl
#include "elf.h"
37 47b43a1f Paolo Bonzini
#include "multiboot.h"
38 0d09e41a Paolo Bonzini
#include "hw/timer/mc146818rtc.h"
39 0d09e41a Paolo Bonzini
#include "hw/timer/i8254.h"
40 0d09e41a Paolo Bonzini
#include "hw/audio/pcspk.h"
41 83c9f4ca Paolo Bonzini
#include "hw/pci/msi.h"
42 83c9f4ca Paolo Bonzini
#include "hw/sysbus.h"
43 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
44 9c17d615 Paolo Bonzini
#include "sysemu/kvm.h"
45 1d31f66b Peter Maydell
#include "kvm_i386.h"
46 0d09e41a Paolo Bonzini
#include "hw/xen/xen.h"
47 9c17d615 Paolo Bonzini
#include "sysemu/blockdev.h"
48 0d09e41a Paolo Bonzini
#include "hw/block/block.h"
49 a19cbfb3 Gerd Hoffmann
#include "ui/qemu-spice.h"
50 022c62cb Paolo Bonzini
#include "exec/memory.h"
51 022c62cb Paolo Bonzini
#include "exec/address-spaces.h"
52 9c17d615 Paolo Bonzini
#include "sysemu/arch_init.h"
53 1de7afc9 Paolo Bonzini
#include "qemu/bitmap.h"
54 0c764a9d Laszlo Ersek
#include "qemu/config-file.h"
55 0445259b Michael S. Tsirkin
#include "hw/acpi/acpi.h"
56 53a89e26 Igor Mammedov
#include "hw/cpu/icc_bus.h"
57 c649983b Igor Mammedov
#include "hw/boards.h"
58 80cabfad bellard
59 471fd342 Blue Swirl
/* debug PC/ISA interrupts */
60 471fd342 Blue Swirl
//#define DEBUG_IRQ
61 471fd342 Blue Swirl
62 471fd342 Blue Swirl
#ifdef DEBUG_IRQ
63 471fd342 Blue Swirl
#define DPRINTF(fmt, ...)                                       \
64 471fd342 Blue Swirl
    do { printf("CPUIRQ: " fmt , ## __VA_ARGS__); } while (0)
65 471fd342 Blue Swirl
#else
66 471fd342 Blue Swirl
#define DPRINTF(fmt, ...)
67 471fd342 Blue Swirl
#endif
68 471fd342 Blue Swirl
69 a80274c3 pbrook
/* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables.  */
70 a80274c3 pbrook
#define ACPI_DATA_SIZE       0x10000
71 3cce6243 blueswir1
#define BIOS_CFG_IOPORT 0x510
72 8a92ea2f aliguori
#define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
73 b6f6e3d3 aliguori
#define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
74 6b35e7bf Jes Sorensen
#define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
75 4c5b10b7 Jes Sorensen
#define FW_CFG_E820_TABLE (FW_CFG_ARCH_LOCAL + 3)
76 40ac17cd Gleb Natapov
#define FW_CFG_HPET (FW_CFG_ARCH_LOCAL + 4)
77 80cabfad bellard
78 3a4a4697 Laszlo Ersek
#define IO_APIC_DEFAULT_ADDRESS 0xfec00000
79 3a4a4697 Laszlo Ersek
80 4c5b10b7 Jes Sorensen
#define E820_NR_ENTRIES                16
81 4c5b10b7 Jes Sorensen
82 4c5b10b7 Jes Sorensen
struct e820_entry {
83 4c5b10b7 Jes Sorensen
    uint64_t address;
84 4c5b10b7 Jes Sorensen
    uint64_t length;
85 4c5b10b7 Jes Sorensen
    uint32_t type;
86 541dc0d4 Stefan Weil
} QEMU_PACKED __attribute((__aligned__(4)));
87 4c5b10b7 Jes Sorensen
88 4c5b10b7 Jes Sorensen
struct e820_table {
89 4c5b10b7 Jes Sorensen
    uint32_t count;
90 4c5b10b7 Jes Sorensen
    struct e820_entry entry[E820_NR_ENTRIES];
91 541dc0d4 Stefan Weil
} QEMU_PACKED __attribute((__aligned__(4)));
92 4c5b10b7 Jes Sorensen
93 4c5b10b7 Jes Sorensen
static struct e820_table e820_table;
94 dd703b99 Blue Swirl
struct hpet_fw_config hpet_cfg = {.count = UINT8_MAX};
95 4c5b10b7 Jes Sorensen
96 b881fbe9 Jan Kiszka
void gsi_handler(void *opaque, int n, int level)
97 1452411b Avi Kivity
{
98 b881fbe9 Jan Kiszka
    GSIState *s = opaque;
99 1452411b Avi Kivity
100 b881fbe9 Jan Kiszka
    DPRINTF("pc: %s GSI %d\n", level ? "raising" : "lowering", n);
101 b881fbe9 Jan Kiszka
    if (n < ISA_NUM_IRQS) {
102 b881fbe9 Jan Kiszka
        qemu_set_irq(s->i8259_irq[n], level);
103 1632dc6a Avi Kivity
    }
104 b881fbe9 Jan Kiszka
    qemu_set_irq(s->ioapic_irq[n], level);
105 2e9947d2 Jan Kiszka
}
106 1452411b Avi Kivity
107 258711c6 Julien Grall
static void ioport80_write(void *opaque, hwaddr addr, uint64_t data,
108 258711c6 Julien Grall
                           unsigned size)
109 80cabfad bellard
{
110 80cabfad bellard
}
111 80cabfad bellard
112 c02e1eac Julien Grall
static uint64_t ioport80_read(void *opaque, hwaddr addr, unsigned size)
113 c02e1eac Julien Grall
{
114 a6fc23e5 Julien Grall
    return 0xffffffffffffffffULL;
115 c02e1eac Julien Grall
}
116 c02e1eac Julien Grall
117 f929aad6 bellard
/* MSDOS compatibility mode FPU exception support */
118 d537cf6c pbrook
static qemu_irq ferr_irq;
119 8e78eb28 Isaku Yamahata
120 8e78eb28 Isaku Yamahata
void pc_register_ferr_irq(qemu_irq irq)
121 8e78eb28 Isaku Yamahata
{
122 8e78eb28 Isaku Yamahata
    ferr_irq = irq;
123 8e78eb28 Isaku Yamahata
}
124 8e78eb28 Isaku Yamahata
125 f929aad6 bellard
/* XXX: add IGNNE support */
126 f929aad6 bellard
void cpu_set_ferr(CPUX86State *s)
127 f929aad6 bellard
{
128 d537cf6c pbrook
    qemu_irq_raise(ferr_irq);
129 f929aad6 bellard
}
130 f929aad6 bellard
131 258711c6 Julien Grall
static void ioportF0_write(void *opaque, hwaddr addr, uint64_t data,
132 258711c6 Julien Grall
                           unsigned size)
133 f929aad6 bellard
{
134 d537cf6c pbrook
    qemu_irq_lower(ferr_irq);
135 f929aad6 bellard
}
136 f929aad6 bellard
137 c02e1eac Julien Grall
static uint64_t ioportF0_read(void *opaque, hwaddr addr, unsigned size)
138 c02e1eac Julien Grall
{
139 a6fc23e5 Julien Grall
    return 0xffffffffffffffffULL;
140 c02e1eac Julien Grall
}
141 c02e1eac Julien Grall
142 28ab0e2e bellard
/* TSC handling */
143 28ab0e2e bellard
uint64_t cpu_get_tsc(CPUX86State *env)
144 28ab0e2e bellard
{
145 4a1418e0 Anthony Liguori
    return cpu_get_ticks();
146 28ab0e2e bellard
}
147 28ab0e2e bellard
148 a5954d5c bellard
/* SMM support */
149 f885f1ea Isaku Yamahata
150 f885f1ea Isaku Yamahata
static cpu_set_smm_t smm_set;
151 f885f1ea Isaku Yamahata
static void *smm_arg;
152 f885f1ea Isaku Yamahata
153 f885f1ea Isaku Yamahata
void cpu_smm_register(cpu_set_smm_t callback, void *arg)
154 f885f1ea Isaku Yamahata
{
155 f885f1ea Isaku Yamahata
    assert(smm_set == NULL);
156 f885f1ea Isaku Yamahata
    assert(smm_arg == NULL);
157 f885f1ea Isaku Yamahata
    smm_set = callback;
158 f885f1ea Isaku Yamahata
    smm_arg = arg;
159 f885f1ea Isaku Yamahata
}
160 f885f1ea Isaku Yamahata
161 4a8fa5dc Andreas Färber
void cpu_smm_update(CPUX86State *env)
162 a5954d5c bellard
{
163 f885f1ea Isaku Yamahata
    if (smm_set && smm_arg && env == first_cpu)
164 f885f1ea Isaku Yamahata
        smm_set(!!(env->hflags & HF_SMM_MASK), smm_arg);
165 a5954d5c bellard
}
166 a5954d5c bellard
167 a5954d5c bellard
168 3de388f6 bellard
/* IRQ handling */
169 4a8fa5dc Andreas Färber
int cpu_get_pic_interrupt(CPUX86State *env)
170 3de388f6 bellard
{
171 3de388f6 bellard
    int intno;
172 3de388f6 bellard
173 cf6d64bf Blue Swirl
    intno = apic_get_interrupt(env->apic_state);
174 3de388f6 bellard
    if (intno >= 0) {
175 3de388f6 bellard
        return intno;
176 3de388f6 bellard
    }
177 3de388f6 bellard
    /* read the irq from the PIC */
178 cf6d64bf Blue Swirl
    if (!apic_accept_pic_intr(env->apic_state)) {
179 0e21e12b ths
        return -1;
180 cf6d64bf Blue Swirl
    }
181 0e21e12b ths
182 3de388f6 bellard
    intno = pic_read_irq(isa_pic);
183 3de388f6 bellard
    return intno;
184 3de388f6 bellard
}
185 3de388f6 bellard
186 d537cf6c pbrook
static void pic_irq_request(void *opaque, int irq, int level)
187 3de388f6 bellard
{
188 4a8fa5dc Andreas Färber
    CPUX86State *env = first_cpu;
189 a5b38b51 aurel32
190 471fd342 Blue Swirl
    DPRINTF("pic_irqs: %s irq %d\n", level? "raise" : "lower", irq);
191 d5529471 aurel32
    if (env->apic_state) {
192 d5529471 aurel32
        while (env) {
193 cf6d64bf Blue Swirl
            if (apic_accept_pic_intr(env->apic_state)) {
194 cf6d64bf Blue Swirl
                apic_deliver_pic_intr(env->apic_state, level);
195 cf6d64bf Blue Swirl
            }
196 d5529471 aurel32
            env = env->next_cpu;
197 d5529471 aurel32
        }
198 d5529471 aurel32
    } else {
199 d8ed887b Andreas Färber
        CPUState *cs = CPU(x86_env_get_cpu(env));
200 d8ed887b Andreas Färber
        if (level) {
201 c3affe56 Andreas Färber
            cpu_interrupt(cs, CPU_INTERRUPT_HARD);
202 d8ed887b Andreas Färber
        } else {
203 d8ed887b Andreas Färber
            cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
204 d8ed887b Andreas Färber
        }
205 a5b38b51 aurel32
    }
206 3de388f6 bellard
}
207 3de388f6 bellard
208 b0a21b53 bellard
/* PC cmos mappings */
209 b0a21b53 bellard
210 80cabfad bellard
#define REG_EQUIPMENT_BYTE          0x14
211 80cabfad bellard
212 d288c7ba Blue Swirl
static int cmos_get_fd_drive_type(FDriveType fd0)
213 777428f2 bellard
{
214 777428f2 bellard
    int val;
215 777428f2 bellard
216 777428f2 bellard
    switch (fd0) {
217 d288c7ba Blue Swirl
    case FDRIVE_DRV_144:
218 777428f2 bellard
        /* 1.44 Mb 3"5 drive */
219 777428f2 bellard
        val = 4;
220 777428f2 bellard
        break;
221 d288c7ba Blue Swirl
    case FDRIVE_DRV_288:
222 777428f2 bellard
        /* 2.88 Mb 3"5 drive */
223 777428f2 bellard
        val = 5;
224 777428f2 bellard
        break;
225 d288c7ba Blue Swirl
    case FDRIVE_DRV_120:
226 777428f2 bellard
        /* 1.2 Mb 5"5 drive */
227 777428f2 bellard
        val = 2;
228 777428f2 bellard
        break;
229 d288c7ba Blue Swirl
    case FDRIVE_DRV_NONE:
230 777428f2 bellard
    default:
231 777428f2 bellard
        val = 0;
232 777428f2 bellard
        break;
233 777428f2 bellard
    }
234 777428f2 bellard
    return val;
235 777428f2 bellard
}
236 777428f2 bellard
237 9139046c Markus Armbruster
static void cmos_init_hd(ISADevice *s, int type_ofs, int info_ofs,
238 9139046c Markus Armbruster
                         int16_t cylinders, int8_t heads, int8_t sectors)
239 ba6c2377 bellard
{
240 ba6c2377 bellard
    rtc_set_memory(s, type_ofs, 47);
241 ba6c2377 bellard
    rtc_set_memory(s, info_ofs, cylinders);
242 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
243 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 2, heads);
244 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 3, 0xff);
245 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 4, 0xff);
246 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
247 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 6, cylinders);
248 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
249 ba6c2377 bellard
    rtc_set_memory(s, info_ofs + 8, sectors);
250 ba6c2377 bellard
}
251 ba6c2377 bellard
252 6ac0e82d balrog
/* convert boot_device letter to something recognizable by the bios */
253 6ac0e82d balrog
static int boot_device2nibble(char boot_device)
254 6ac0e82d balrog
{
255 6ac0e82d balrog
    switch(boot_device) {
256 6ac0e82d balrog
    case 'a':
257 6ac0e82d balrog
    case 'b':
258 6ac0e82d balrog
        return 0x01; /* floppy boot */
259 6ac0e82d balrog
    case 'c':
260 6ac0e82d balrog
        return 0x02; /* hard drive boot */
261 6ac0e82d balrog
    case 'd':
262 6ac0e82d balrog
        return 0x03; /* CD-ROM boot */
263 6ac0e82d balrog
    case 'n':
264 6ac0e82d balrog
        return 0x04; /* Network boot */
265 6ac0e82d balrog
    }
266 6ac0e82d balrog
    return 0;
267 6ac0e82d balrog
}
268 6ac0e82d balrog
269 e1123015 Markus Armbruster
static int set_boot_dev(ISADevice *s, const char *boot_device)
270 0ecdffbb aurel32
{
271 0ecdffbb aurel32
#define PC_MAX_BOOT_DEVICES 3
272 0ecdffbb aurel32
    int nbds, bds[3] = { 0, };
273 0ecdffbb aurel32
    int i;
274 0ecdffbb aurel32
275 0ecdffbb aurel32
    nbds = strlen(boot_device);
276 0ecdffbb aurel32
    if (nbds > PC_MAX_BOOT_DEVICES) {
277 1ecda02b Markus Armbruster
        error_report("Too many boot devices for PC");
278 0ecdffbb aurel32
        return(1);
279 0ecdffbb aurel32
    }
280 0ecdffbb aurel32
    for (i = 0; i < nbds; i++) {
281 0ecdffbb aurel32
        bds[i] = boot_device2nibble(boot_device[i]);
282 0ecdffbb aurel32
        if (bds[i] == 0) {
283 1ecda02b Markus Armbruster
            error_report("Invalid boot device for PC: '%c'",
284 1ecda02b Markus Armbruster
                         boot_device[i]);
285 0ecdffbb aurel32
            return(1);
286 0ecdffbb aurel32
        }
287 0ecdffbb aurel32
    }
288 0ecdffbb aurel32
    rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
289 d9346e81 Markus Armbruster
    rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
290 0ecdffbb aurel32
    return(0);
291 0ecdffbb aurel32
}
292 0ecdffbb aurel32
293 d9346e81 Markus Armbruster
static int pc_boot_set(void *opaque, const char *boot_device)
294 d9346e81 Markus Armbruster
{
295 e1123015 Markus Armbruster
    return set_boot_dev(opaque, boot_device);
296 d9346e81 Markus Armbruster
}
297 d9346e81 Markus Armbruster
298 c0897e0c Markus Armbruster
typedef struct pc_cmos_init_late_arg {
299 c0897e0c Markus Armbruster
    ISADevice *rtc_state;
300 9139046c Markus Armbruster
    BusState *idebus[2];
301 c0897e0c Markus Armbruster
} pc_cmos_init_late_arg;
302 c0897e0c Markus Armbruster
303 c0897e0c Markus Armbruster
static void pc_cmos_init_late(void *opaque)
304 c0897e0c Markus Armbruster
{
305 c0897e0c Markus Armbruster
    pc_cmos_init_late_arg *arg = opaque;
306 c0897e0c Markus Armbruster
    ISADevice *s = arg->rtc_state;
307 9139046c Markus Armbruster
    int16_t cylinders;
308 9139046c Markus Armbruster
    int8_t heads, sectors;
309 c0897e0c Markus Armbruster
    int val;
310 2adc99b2 Markus Armbruster
    int i, trans;
311 c0897e0c Markus Armbruster
312 9139046c Markus Armbruster
    val = 0;
313 9139046c Markus Armbruster
    if (ide_get_geometry(arg->idebus[0], 0,
314 9139046c Markus Armbruster
                         &cylinders, &heads, &sectors) >= 0) {
315 9139046c Markus Armbruster
        cmos_init_hd(s, 0x19, 0x1b, cylinders, heads, sectors);
316 9139046c Markus Armbruster
        val |= 0xf0;
317 9139046c Markus Armbruster
    }
318 9139046c Markus Armbruster
    if (ide_get_geometry(arg->idebus[0], 1,
319 9139046c Markus Armbruster
                         &cylinders, &heads, &sectors) >= 0) {
320 9139046c Markus Armbruster
        cmos_init_hd(s, 0x1a, 0x24, cylinders, heads, sectors);
321 9139046c Markus Armbruster
        val |= 0x0f;
322 9139046c Markus Armbruster
    }
323 9139046c Markus Armbruster
    rtc_set_memory(s, 0x12, val);
324 c0897e0c Markus Armbruster
325 c0897e0c Markus Armbruster
    val = 0;
326 c0897e0c Markus Armbruster
    for (i = 0; i < 4; i++) {
327 9139046c Markus Armbruster
        /* NOTE: ide_get_geometry() returns the physical
328 9139046c Markus Armbruster
           geometry.  It is always such that: 1 <= sects <= 63, 1
329 9139046c Markus Armbruster
           <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
330 9139046c Markus Armbruster
           geometry can be different if a translation is done. */
331 9139046c Markus Armbruster
        if (ide_get_geometry(arg->idebus[i / 2], i % 2,
332 9139046c Markus Armbruster
                             &cylinders, &heads, &sectors) >= 0) {
333 2adc99b2 Markus Armbruster
            trans = ide_get_bios_chs_trans(arg->idebus[i / 2], i % 2) - 1;
334 2adc99b2 Markus Armbruster
            assert((trans & ~3) == 0);
335 2adc99b2 Markus Armbruster
            val |= trans << (i * 2);
336 c0897e0c Markus Armbruster
        }
337 c0897e0c Markus Armbruster
    }
338 c0897e0c Markus Armbruster
    rtc_set_memory(s, 0x39, val);
339 c0897e0c Markus Armbruster
340 c0897e0c Markus Armbruster
    qemu_unregister_reset(pc_cmos_init_late, opaque);
341 c0897e0c Markus Armbruster
}
342 c0897e0c Markus Armbruster
343 b8b7456d Igor Mammedov
typedef struct RTCCPUHotplugArg {
344 b8b7456d Igor Mammedov
    Notifier cpu_added_notifier;
345 b8b7456d Igor Mammedov
    ISADevice *rtc_state;
346 b8b7456d Igor Mammedov
} RTCCPUHotplugArg;
347 b8b7456d Igor Mammedov
348 b8b7456d Igor Mammedov
static void rtc_notify_cpu_added(Notifier *notifier, void *data)
349 b8b7456d Igor Mammedov
{
350 b8b7456d Igor Mammedov
    RTCCPUHotplugArg *arg = container_of(notifier, RTCCPUHotplugArg,
351 b8b7456d Igor Mammedov
                                         cpu_added_notifier);
352 b8b7456d Igor Mammedov
    ISADevice *s = arg->rtc_state;
353 b8b7456d Igor Mammedov
354 b8b7456d Igor Mammedov
    /* increment the number of CPUs */
355 b8b7456d Igor Mammedov
    rtc_set_memory(s, 0x5f, rtc_get_memory(s, 0x5f) + 1);
356 b8b7456d Igor Mammedov
}
357 b8b7456d Igor Mammedov
358 845773ab Isaku Yamahata
void pc_cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
359 c0897e0c Markus Armbruster
                  const char *boot_device,
360 34d4260e Kevin Wolf
                  ISADevice *floppy, BusState *idebus0, BusState *idebus1,
361 63ffb564 Blue Swirl
                  ISADevice *s)
362 80cabfad bellard
{
363 61a8d649 Markus Armbruster
    int val, nb, i;
364 980bda8b Peter Maydell
    FDriveType fd_type[2] = { FDRIVE_DRV_NONE, FDRIVE_DRV_NONE };
365 c0897e0c Markus Armbruster
    static pc_cmos_init_late_arg arg;
366 b8b7456d Igor Mammedov
    static RTCCPUHotplugArg cpu_hotplug_cb;
367 b0a21b53 bellard
368 b0a21b53 bellard
    /* various important CMOS locations needed by PC/Bochs bios */
369 80cabfad bellard
370 80cabfad bellard
    /* memory size */
371 e89001f7 Markus Armbruster
    /* base memory (first MiB) */
372 e89001f7 Markus Armbruster
    val = MIN(ram_size / 1024, 640);
373 333190eb bellard
    rtc_set_memory(s, 0x15, val);
374 333190eb bellard
    rtc_set_memory(s, 0x16, val >> 8);
375 e89001f7 Markus Armbruster
    /* extended memory (next 64MiB) */
376 e89001f7 Markus Armbruster
    if (ram_size > 1024 * 1024) {
377 e89001f7 Markus Armbruster
        val = (ram_size - 1024 * 1024) / 1024;
378 e89001f7 Markus Armbruster
    } else {
379 e89001f7 Markus Armbruster
        val = 0;
380 e89001f7 Markus Armbruster
    }
381 80cabfad bellard
    if (val > 65535)
382 80cabfad bellard
        val = 65535;
383 b0a21b53 bellard
    rtc_set_memory(s, 0x17, val);
384 b0a21b53 bellard
    rtc_set_memory(s, 0x18, val >> 8);
385 b0a21b53 bellard
    rtc_set_memory(s, 0x30, val);
386 b0a21b53 bellard
    rtc_set_memory(s, 0x31, val >> 8);
387 e89001f7 Markus Armbruster
    /* memory between 16MiB and 4GiB */
388 e89001f7 Markus Armbruster
    if (ram_size > 16 * 1024 * 1024) {
389 e89001f7 Markus Armbruster
        val = (ram_size - 16 * 1024 * 1024) / 65536;
390 e89001f7 Markus Armbruster
    } else {
391 9da98861 bellard
        val = 0;
392 e89001f7 Markus Armbruster
    }
393 80cabfad bellard
    if (val > 65535)
394 80cabfad bellard
        val = 65535;
395 b0a21b53 bellard
    rtc_set_memory(s, 0x34, val);
396 b0a21b53 bellard
    rtc_set_memory(s, 0x35, val >> 8);
397 e89001f7 Markus Armbruster
    /* memory above 4GiB */
398 e89001f7 Markus Armbruster
    val = above_4g_mem_size / 65536;
399 e89001f7 Markus Armbruster
    rtc_set_memory(s, 0x5b, val);
400 e89001f7 Markus Armbruster
    rtc_set_memory(s, 0x5c, val >> 8);
401 e89001f7 Markus Armbruster
    rtc_set_memory(s, 0x5d, val >> 16);
402 3b46e624 ths
403 298e01b6 aurel32
    /* set the number of CPU */
404 298e01b6 aurel32
    rtc_set_memory(s, 0x5f, smp_cpus - 1);
405 b8b7456d Igor Mammedov
    /* init CPU hotplug notifier */
406 b8b7456d Igor Mammedov
    cpu_hotplug_cb.rtc_state = s;
407 b8b7456d Igor Mammedov
    cpu_hotplug_cb.cpu_added_notifier.notify = rtc_notify_cpu_added;
408 b8b7456d Igor Mammedov
    qemu_register_cpu_added_notifier(&cpu_hotplug_cb.cpu_added_notifier);
409 298e01b6 aurel32
410 e1123015 Markus Armbruster
    if (set_boot_dev(s, boot_device)) {
411 28c5af54 j_mayer
        exit(1);
412 28c5af54 j_mayer
    }
413 80cabfad bellard
414 b41a2cd1 bellard
    /* floppy type */
415 34d4260e Kevin Wolf
    if (floppy) {
416 34d4260e Kevin Wolf
        for (i = 0; i < 2; i++) {
417 61a8d649 Markus Armbruster
            fd_type[i] = isa_fdc_get_drive_type(floppy, i);
418 63ffb564 Blue Swirl
        }
419 63ffb564 Blue Swirl
    }
420 63ffb564 Blue Swirl
    val = (cmos_get_fd_drive_type(fd_type[0]) << 4) |
421 63ffb564 Blue Swirl
        cmos_get_fd_drive_type(fd_type[1]);
422 b0a21b53 bellard
    rtc_set_memory(s, 0x10, val);
423 3b46e624 ths
424 b0a21b53 bellard
    val = 0;
425 b41a2cd1 bellard
    nb = 0;
426 63ffb564 Blue Swirl
    if (fd_type[0] < FDRIVE_DRV_NONE) {
427 80cabfad bellard
        nb++;
428 d288c7ba Blue Swirl
    }
429 63ffb564 Blue Swirl
    if (fd_type[1] < FDRIVE_DRV_NONE) {
430 80cabfad bellard
        nb++;
431 d288c7ba Blue Swirl
    }
432 80cabfad bellard
    switch (nb) {
433 80cabfad bellard
    case 0:
434 80cabfad bellard
        break;
435 80cabfad bellard
    case 1:
436 b0a21b53 bellard
        val |= 0x01; /* 1 drive, ready for boot */
437 80cabfad bellard
        break;
438 80cabfad bellard
    case 2:
439 b0a21b53 bellard
        val |= 0x41; /* 2 drives, ready for boot */
440 80cabfad bellard
        break;
441 80cabfad bellard
    }
442 b0a21b53 bellard
    val |= 0x02; /* FPU is there */
443 b0a21b53 bellard
    val |= 0x04; /* PS/2 mouse installed */
444 b0a21b53 bellard
    rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
445 b0a21b53 bellard
446 ba6c2377 bellard
    /* hard drives */
447 c0897e0c Markus Armbruster
    arg.rtc_state = s;
448 9139046c Markus Armbruster
    arg.idebus[0] = idebus0;
449 9139046c Markus Armbruster
    arg.idebus[1] = idebus1;
450 c0897e0c Markus Armbruster
    qemu_register_reset(pc_cmos_init_late, &arg);
451 80cabfad bellard
}
452 80cabfad bellard
453 a0881c64 Andreas Färber
#define TYPE_PORT92 "port92"
454 a0881c64 Andreas Färber
#define PORT92(obj) OBJECT_CHECK(Port92State, (obj), TYPE_PORT92)
455 a0881c64 Andreas Färber
456 4b78a802 Blue Swirl
/* port 92 stuff: could be split off */
457 4b78a802 Blue Swirl
typedef struct Port92State {
458 a0881c64 Andreas Färber
    ISADevice parent_obj;
459 a0881c64 Andreas Färber
460 23af670e Richard Henderson
    MemoryRegion io;
461 4b78a802 Blue Swirl
    uint8_t outport;
462 4b78a802 Blue Swirl
    qemu_irq *a20_out;
463 4b78a802 Blue Swirl
} Port92State;
464 4b78a802 Blue Swirl
465 93ef4192 Alexander Graf
static void port92_write(void *opaque, hwaddr addr, uint64_t val,
466 93ef4192 Alexander Graf
                         unsigned size)
467 4b78a802 Blue Swirl
{
468 4b78a802 Blue Swirl
    Port92State *s = opaque;
469 4b78a802 Blue Swirl
470 4b78a802 Blue Swirl
    DPRINTF("port92: write 0x%02x\n", val);
471 4b78a802 Blue Swirl
    s->outport = val;
472 4b78a802 Blue Swirl
    qemu_set_irq(*s->a20_out, (val >> 1) & 1);
473 4b78a802 Blue Swirl
    if (val & 1) {
474 4b78a802 Blue Swirl
        qemu_system_reset_request();
475 4b78a802 Blue Swirl
    }
476 4b78a802 Blue Swirl
}
477 4b78a802 Blue Swirl
478 93ef4192 Alexander Graf
static uint64_t port92_read(void *opaque, hwaddr addr,
479 93ef4192 Alexander Graf
                            unsigned size)
480 4b78a802 Blue Swirl
{
481 4b78a802 Blue Swirl
    Port92State *s = opaque;
482 4b78a802 Blue Swirl
    uint32_t ret;
483 4b78a802 Blue Swirl
484 4b78a802 Blue Swirl
    ret = s->outport;
485 4b78a802 Blue Swirl
    DPRINTF("port92: read 0x%02x\n", ret);
486 4b78a802 Blue Swirl
    return ret;
487 4b78a802 Blue Swirl
}
488 4b78a802 Blue Swirl
489 4b78a802 Blue Swirl
static void port92_init(ISADevice *dev, qemu_irq *a20_out)
490 4b78a802 Blue Swirl
{
491 a0881c64 Andreas Färber
    Port92State *s = PORT92(dev);
492 4b78a802 Blue Swirl
493 4b78a802 Blue Swirl
    s->a20_out = a20_out;
494 4b78a802 Blue Swirl
}
495 4b78a802 Blue Swirl
496 4b78a802 Blue Swirl
static const VMStateDescription vmstate_port92_isa = {
497 4b78a802 Blue Swirl
    .name = "port92",
498 4b78a802 Blue Swirl
    .version_id = 1,
499 4b78a802 Blue Swirl
    .minimum_version_id = 1,
500 4b78a802 Blue Swirl
    .minimum_version_id_old = 1,
501 4b78a802 Blue Swirl
    .fields      = (VMStateField []) {
502 4b78a802 Blue Swirl
        VMSTATE_UINT8(outport, Port92State),
503 4b78a802 Blue Swirl
        VMSTATE_END_OF_LIST()
504 4b78a802 Blue Swirl
    }
505 4b78a802 Blue Swirl
};
506 4b78a802 Blue Swirl
507 4b78a802 Blue Swirl
static void port92_reset(DeviceState *d)
508 4b78a802 Blue Swirl
{
509 a0881c64 Andreas Färber
    Port92State *s = PORT92(d);
510 4b78a802 Blue Swirl
511 4b78a802 Blue Swirl
    s->outport &= ~1;
512 4b78a802 Blue Swirl
}
513 4b78a802 Blue Swirl
514 23af670e Richard Henderson
static const MemoryRegionOps port92_ops = {
515 93ef4192 Alexander Graf
    .read = port92_read,
516 93ef4192 Alexander Graf
    .write = port92_write,
517 93ef4192 Alexander Graf
    .impl = {
518 93ef4192 Alexander Graf
        .min_access_size = 1,
519 93ef4192 Alexander Graf
        .max_access_size = 1,
520 93ef4192 Alexander Graf
    },
521 93ef4192 Alexander Graf
    .endianness = DEVICE_LITTLE_ENDIAN,
522 23af670e Richard Henderson
};
523 23af670e Richard Henderson
524 db895a1e Andreas Färber
static void port92_initfn(Object *obj)
525 4b78a802 Blue Swirl
{
526 db895a1e Andreas Färber
    Port92State *s = PORT92(obj);
527 4b78a802 Blue Swirl
528 2c9b15ca Paolo Bonzini
    memory_region_init_io(&s->io, NULL, &port92_ops, s, "port92", 1);
529 23af670e Richard Henderson
530 4b78a802 Blue Swirl
    s->outport = 0;
531 db895a1e Andreas Färber
}
532 db895a1e Andreas Färber
533 db895a1e Andreas Färber
static void port92_realizefn(DeviceState *dev, Error **errp)
534 db895a1e Andreas Färber
{
535 db895a1e Andreas Färber
    ISADevice *isadev = ISA_DEVICE(dev);
536 db895a1e Andreas Färber
    Port92State *s = PORT92(dev);
537 db895a1e Andreas Färber
538 db895a1e Andreas Färber
    isa_register_ioport(isadev, &s->io, 0x92);
539 4b78a802 Blue Swirl
}
540 4b78a802 Blue Swirl
541 8f04ee08 Anthony Liguori
static void port92_class_initfn(ObjectClass *klass, void *data)
542 8f04ee08 Anthony Liguori
{
543 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
544 db895a1e Andreas Färber
545 39bffca2 Anthony Liguori
    dc->no_user = 1;
546 db895a1e Andreas Färber
    dc->realize = port92_realizefn;
547 39bffca2 Anthony Liguori
    dc->reset = port92_reset;
548 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_port92_isa;
549 8f04ee08 Anthony Liguori
}
550 8f04ee08 Anthony Liguori
551 8c43a6f0 Andreas Färber
static const TypeInfo port92_info = {
552 a0881c64 Andreas Färber
    .name          = TYPE_PORT92,
553 39bffca2 Anthony Liguori
    .parent        = TYPE_ISA_DEVICE,
554 39bffca2 Anthony Liguori
    .instance_size = sizeof(Port92State),
555 db895a1e Andreas Färber
    .instance_init = port92_initfn,
556 39bffca2 Anthony Liguori
    .class_init    = port92_class_initfn,
557 4b78a802 Blue Swirl
};
558 4b78a802 Blue Swirl
559 83f7d43a Andreas Färber
static void port92_register_types(void)
560 4b78a802 Blue Swirl
{
561 39bffca2 Anthony Liguori
    type_register_static(&port92_info);
562 4b78a802 Blue Swirl
}
563 83f7d43a Andreas Färber
564 83f7d43a Andreas Färber
type_init(port92_register_types)
565 4b78a802 Blue Swirl
566 956a3e6b Blue Swirl
static void handle_a20_line_change(void *opaque, int irq, int level)
567 59b8ad81 bellard
{
568 cc36a7a2 Andreas Färber
    X86CPU *cpu = opaque;
569 e1a23744 bellard
570 956a3e6b Blue Swirl
    /* XXX: send to all CPUs ? */
571 4b78a802 Blue Swirl
    /* XXX: add logic to handle multiple A20 line sources */
572 cc36a7a2 Andreas Färber
    x86_cpu_set_a20(cpu, level);
573 e1a23744 bellard
}
574 e1a23744 bellard
575 4c5b10b7 Jes Sorensen
int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
576 4c5b10b7 Jes Sorensen
{
577 8ca209ad Alex Williamson
    int index = le32_to_cpu(e820_table.count);
578 4c5b10b7 Jes Sorensen
    struct e820_entry *entry;
579 4c5b10b7 Jes Sorensen
580 4c5b10b7 Jes Sorensen
    if (index >= E820_NR_ENTRIES)
581 4c5b10b7 Jes Sorensen
        return -EBUSY;
582 8ca209ad Alex Williamson
    entry = &e820_table.entry[index++];
583 4c5b10b7 Jes Sorensen
584 8ca209ad Alex Williamson
    entry->address = cpu_to_le64(address);
585 8ca209ad Alex Williamson
    entry->length = cpu_to_le64(length);
586 8ca209ad Alex Williamson
    entry->type = cpu_to_le32(type);
587 4c5b10b7 Jes Sorensen
588 8ca209ad Alex Williamson
    e820_table.count = cpu_to_le32(index);
589 8ca209ad Alex Williamson
    return index;
590 4c5b10b7 Jes Sorensen
}
591 4c5b10b7 Jes Sorensen
592 1d934e89 Eduardo Habkost
/* Calculates the limit to CPU APIC ID values
593 1d934e89 Eduardo Habkost
 *
594 1d934e89 Eduardo Habkost
 * This function returns the limit for the APIC ID value, so that all
595 1d934e89 Eduardo Habkost
 * CPU APIC IDs are < pc_apic_id_limit().
596 1d934e89 Eduardo Habkost
 *
597 1d934e89 Eduardo Habkost
 * This is used for FW_CFG_MAX_CPUS. See comments on bochs_bios_init().
598 1d934e89 Eduardo Habkost
 */
599 1d934e89 Eduardo Habkost
static unsigned int pc_apic_id_limit(unsigned int max_cpus)
600 1d934e89 Eduardo Habkost
{
601 1d934e89 Eduardo Habkost
    return x86_cpu_apic_id_from_index(max_cpus - 1) + 1;
602 1d934e89 Eduardo Habkost
}
603 1d934e89 Eduardo Habkost
604 a88b362c Laszlo Ersek
static FWCfgState *bochs_bios_init(void)
605 80cabfad bellard
{
606 a88b362c Laszlo Ersek
    FWCfgState *fw_cfg;
607 b6f6e3d3 aliguori
    uint8_t *smbios_table;
608 b6f6e3d3 aliguori
    size_t smbios_len;
609 11c2fd3e aliguori
    uint64_t *numa_fw_cfg;
610 11c2fd3e aliguori
    int i, j;
611 1d934e89 Eduardo Habkost
    unsigned int apic_id_limit = pc_apic_id_limit(max_cpus);
612 3cce6243 blueswir1
613 3cce6243 blueswir1
    fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
614 1d934e89 Eduardo Habkost
    /* FW_CFG_MAX_CPUS is a bit confusing/problematic on x86:
615 1d934e89 Eduardo Habkost
     *
616 1d934e89 Eduardo Habkost
     * SeaBIOS needs FW_CFG_MAX_CPUS for CPU hotplug, but the CPU hotplug
617 1d934e89 Eduardo Habkost
     * QEMU<->SeaBIOS interface is not based on the "CPU index", but on the APIC
618 1d934e89 Eduardo Habkost
     * ID of hotplugged CPUs[1]. This means that FW_CFG_MAX_CPUS is not the
619 1d934e89 Eduardo Habkost
     * "maximum number of CPUs", but the "limit to the APIC ID values SeaBIOS
620 1d934e89 Eduardo Habkost
     * may see".
621 1d934e89 Eduardo Habkost
     *
622 1d934e89 Eduardo Habkost
     * So, this means we must not use max_cpus, here, but the maximum possible
623 1d934e89 Eduardo Habkost
     * APIC ID value, plus one.
624 1d934e89 Eduardo Habkost
     *
625 1d934e89 Eduardo Habkost
     * [1] The only kind of "CPU identifier" used between SeaBIOS and QEMU is
626 1d934e89 Eduardo Habkost
     *     the APIC ID, not the "CPU index"
627 1d934e89 Eduardo Habkost
     */
628 1d934e89 Eduardo Habkost
    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)apic_id_limit);
629 3cce6243 blueswir1
    fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
630 905fdcb5 blueswir1
    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
631 089da572 Markus Armbruster
    fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES,
632 089da572 Markus Armbruster
                     acpi_tables, acpi_tables_len);
633 9b5b76d4 Jan Kiszka
    fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, kvm_allows_irq0_override());
634 b6f6e3d3 aliguori
635 b6f6e3d3 aliguori
    smbios_table = smbios_get_table(&smbios_len);
636 b6f6e3d3 aliguori
    if (smbios_table)
637 b6f6e3d3 aliguori
        fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
638 b6f6e3d3 aliguori
                         smbios_table, smbios_len);
639 089da572 Markus Armbruster
    fw_cfg_add_bytes(fw_cfg, FW_CFG_E820_TABLE,
640 089da572 Markus Armbruster
                     &e820_table, sizeof(e820_table));
641 11c2fd3e aliguori
642 089da572 Markus Armbruster
    fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg));
643 11c2fd3e aliguori
    /* allocate memory for the NUMA channel: one (64bit) word for the number
644 11c2fd3e aliguori
     * of nodes, one word for each VCPU->node and one word for each node to
645 11c2fd3e aliguori
     * hold the amount of memory.
646 11c2fd3e aliguori
     */
647 1d934e89 Eduardo Habkost
    numa_fw_cfg = g_new0(uint64_t, 1 + apic_id_limit + nb_numa_nodes);
648 11c2fd3e aliguori
    numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
649 991dfefd Vasilis Liaskovitis
    for (i = 0; i < max_cpus; i++) {
650 1d934e89 Eduardo Habkost
        unsigned int apic_id = x86_cpu_apic_id_from_index(i);
651 1d934e89 Eduardo Habkost
        assert(apic_id < apic_id_limit);
652 11c2fd3e aliguori
        for (j = 0; j < nb_numa_nodes; j++) {
653 ee785fed Chegu Vinod
            if (test_bit(i, node_cpumask[j])) {
654 1d934e89 Eduardo Habkost
                numa_fw_cfg[apic_id + 1] = cpu_to_le64(j);
655 11c2fd3e aliguori
                break;
656 11c2fd3e aliguori
            }
657 11c2fd3e aliguori
        }
658 11c2fd3e aliguori
    }
659 11c2fd3e aliguori
    for (i = 0; i < nb_numa_nodes; i++) {
660 1d934e89 Eduardo Habkost
        numa_fw_cfg[apic_id_limit + 1 + i] = cpu_to_le64(node_mem[i]);
661 11c2fd3e aliguori
    }
662 089da572 Markus Armbruster
    fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, numa_fw_cfg,
663 1d934e89 Eduardo Habkost
                     (1 + apic_id_limit + nb_numa_nodes) *
664 1d934e89 Eduardo Habkost
                     sizeof(*numa_fw_cfg));
665 bf483392 Alexander Graf
666 bf483392 Alexander Graf
    return fw_cfg;
667 80cabfad bellard
}
668 80cabfad bellard
669 642a4f96 ths
static long get_file_size(FILE *f)
670 642a4f96 ths
{
671 642a4f96 ths
    long where, size;
672 642a4f96 ths
673 642a4f96 ths
    /* XXX: on Unix systems, using fstat() probably makes more sense */
674 642a4f96 ths
675 642a4f96 ths
    where = ftell(f);
676 642a4f96 ths
    fseek(f, 0, SEEK_END);
677 642a4f96 ths
    size = ftell(f);
678 642a4f96 ths
    fseek(f, where, SEEK_SET);
679 642a4f96 ths
680 642a4f96 ths
    return size;
681 642a4f96 ths
}
682 642a4f96 ths
683 a88b362c Laszlo Ersek
static void load_linux(FWCfgState *fw_cfg,
684 4fc9af53 aliguori
                       const char *kernel_filename,
685 0f9d76e5 liguang
                       const char *initrd_filename,
686 0f9d76e5 liguang
                       const char *kernel_cmdline,
687 a8170e5e Avi Kivity
                       hwaddr max_ram_size)
688 642a4f96 ths
{
689 642a4f96 ths
    uint16_t protocol;
690 5cea8590 Paul Brook
    int setup_size, kernel_size, initrd_size = 0, cmdline_size;
691 642a4f96 ths
    uint32_t initrd_max;
692 57a46d05 Alexander Graf
    uint8_t header[8192], *setup, *kernel, *initrd_data;
693 a8170e5e Avi Kivity
    hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
694 45a50b16 Gerd Hoffmann
    FILE *f;
695 bf4e5d92 Pascal Terjan
    char *vmode;
696 642a4f96 ths
697 642a4f96 ths
    /* Align to 16 bytes as a paranoia measure */
698 642a4f96 ths
    cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
699 642a4f96 ths
700 642a4f96 ths
    /* load the kernel header */
701 642a4f96 ths
    f = fopen(kernel_filename, "rb");
702 642a4f96 ths
    if (!f || !(kernel_size = get_file_size(f)) ||
703 0f9d76e5 liguang
        fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
704 0f9d76e5 liguang
        MIN(ARRAY_SIZE(header), kernel_size)) {
705 0f9d76e5 liguang
        fprintf(stderr, "qemu: could not load kernel '%s': %s\n",
706 0f9d76e5 liguang
                kernel_filename, strerror(errno));
707 0f9d76e5 liguang
        exit(1);
708 642a4f96 ths
    }
709 642a4f96 ths
710 642a4f96 ths
    /* kernel protocol version */
711 bc4edd79 bellard
#if 0
712 642a4f96 ths
    fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
713 bc4edd79 bellard
#endif
714 0f9d76e5 liguang
    if (ldl_p(header+0x202) == 0x53726448) {
715 0f9d76e5 liguang
        protocol = lduw_p(header+0x206);
716 0f9d76e5 liguang
    } else {
717 0f9d76e5 liguang
        /* This looks like a multiboot kernel. If it is, let's stop
718 0f9d76e5 liguang
           treating it like a Linux kernel. */
719 52001445 Adam Lackorzynski
        if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
720 0f9d76e5 liguang
                           kernel_cmdline, kernel_size, header)) {
721 82663ee2 Blue Swirl
            return;
722 0f9d76e5 liguang
        }
723 0f9d76e5 liguang
        protocol = 0;
724 f16408df Alexander Graf
    }
725 642a4f96 ths
726 642a4f96 ths
    if (protocol < 0x200 || !(header[0x211] & 0x01)) {
727 0f9d76e5 liguang
        /* Low kernel */
728 0f9d76e5 liguang
        real_addr    = 0x90000;
729 0f9d76e5 liguang
        cmdline_addr = 0x9a000 - cmdline_size;
730 0f9d76e5 liguang
        prot_addr    = 0x10000;
731 642a4f96 ths
    } else if (protocol < 0x202) {
732 0f9d76e5 liguang
        /* High but ancient kernel */
733 0f9d76e5 liguang
        real_addr    = 0x90000;
734 0f9d76e5 liguang
        cmdline_addr = 0x9a000 - cmdline_size;
735 0f9d76e5 liguang
        prot_addr    = 0x100000;
736 642a4f96 ths
    } else {
737 0f9d76e5 liguang
        /* High and recent kernel */
738 0f9d76e5 liguang
        real_addr    = 0x10000;
739 0f9d76e5 liguang
        cmdline_addr = 0x20000;
740 0f9d76e5 liguang
        prot_addr    = 0x100000;
741 642a4f96 ths
    }
742 642a4f96 ths
743 bc4edd79 bellard
#if 0
744 642a4f96 ths
    fprintf(stderr,
745 0f9d76e5 liguang
            "qemu: real_addr     = 0x" TARGET_FMT_plx "\n"
746 0f9d76e5 liguang
            "qemu: cmdline_addr  = 0x" TARGET_FMT_plx "\n"
747 0f9d76e5 liguang
            "qemu: prot_addr     = 0x" TARGET_FMT_plx "\n",
748 0f9d76e5 liguang
            real_addr,
749 0f9d76e5 liguang
            cmdline_addr,
750 0f9d76e5 liguang
            prot_addr);
751 bc4edd79 bellard
#endif
752 642a4f96 ths
753 642a4f96 ths
    /* highest address for loading the initrd */
754 0f9d76e5 liguang
    if (protocol >= 0x203) {
755 0f9d76e5 liguang
        initrd_max = ldl_p(header+0x22c);
756 0f9d76e5 liguang
    } else {
757 0f9d76e5 liguang
        initrd_max = 0x37ffffff;
758 0f9d76e5 liguang
    }
759 642a4f96 ths
760 e6ade764 Glauber Costa
    if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
761 e6ade764 Glauber Costa
            initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
762 642a4f96 ths
763 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
764 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline)+1);
765 96f80586 Markus Armbruster
    fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
766 642a4f96 ths
767 642a4f96 ths
    if (protocol >= 0x202) {
768 0f9d76e5 liguang
        stl_p(header+0x228, cmdline_addr);
769 642a4f96 ths
    } else {
770 0f9d76e5 liguang
        stw_p(header+0x20, 0xA33F);
771 0f9d76e5 liguang
        stw_p(header+0x22, cmdline_addr-real_addr);
772 642a4f96 ths
    }
773 642a4f96 ths
774 bf4e5d92 Pascal Terjan
    /* handle vga= parameter */
775 bf4e5d92 Pascal Terjan
    vmode = strstr(kernel_cmdline, "vga=");
776 bf4e5d92 Pascal Terjan
    if (vmode) {
777 bf4e5d92 Pascal Terjan
        unsigned int video_mode;
778 bf4e5d92 Pascal Terjan
        /* skip "vga=" */
779 bf4e5d92 Pascal Terjan
        vmode += 4;
780 bf4e5d92 Pascal Terjan
        if (!strncmp(vmode, "normal", 6)) {
781 bf4e5d92 Pascal Terjan
            video_mode = 0xffff;
782 bf4e5d92 Pascal Terjan
        } else if (!strncmp(vmode, "ext", 3)) {
783 bf4e5d92 Pascal Terjan
            video_mode = 0xfffe;
784 bf4e5d92 Pascal Terjan
        } else if (!strncmp(vmode, "ask", 3)) {
785 bf4e5d92 Pascal Terjan
            video_mode = 0xfffd;
786 bf4e5d92 Pascal Terjan
        } else {
787 bf4e5d92 Pascal Terjan
            video_mode = strtol(vmode, NULL, 0);
788 bf4e5d92 Pascal Terjan
        }
789 bf4e5d92 Pascal Terjan
        stw_p(header+0x1fa, video_mode);
790 bf4e5d92 Pascal Terjan
    }
791 bf4e5d92 Pascal Terjan
792 642a4f96 ths
    /* loader type */
793 5cbdb3a3 Stefan Weil
    /* High nybble = B reserved for QEMU; low nybble is revision number.
794 642a4f96 ths
       If this code is substantially changed, you may want to consider
795 642a4f96 ths
       incrementing the revision. */
796 0f9d76e5 liguang
    if (protocol >= 0x200) {
797 0f9d76e5 liguang
        header[0x210] = 0xB0;
798 0f9d76e5 liguang
    }
799 642a4f96 ths
    /* heap */
800 642a4f96 ths
    if (protocol >= 0x201) {
801 0f9d76e5 liguang
        header[0x211] |= 0x80;        /* CAN_USE_HEAP */
802 0f9d76e5 liguang
        stw_p(header+0x224, cmdline_addr-real_addr-0x200);
803 642a4f96 ths
    }
804 642a4f96 ths
805 642a4f96 ths
    /* load initrd */
806 642a4f96 ths
    if (initrd_filename) {
807 0f9d76e5 liguang
        if (protocol < 0x200) {
808 0f9d76e5 liguang
            fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
809 0f9d76e5 liguang
            exit(1);
810 0f9d76e5 liguang
        }
811 642a4f96 ths
812 0f9d76e5 liguang
        initrd_size = get_image_size(initrd_filename);
813 d6fa4b77 M. Mohan Kumar
        if (initrd_size < 0) {
814 d6fa4b77 M. Mohan Kumar
            fprintf(stderr, "qemu: error reading initrd %s\n",
815 d6fa4b77 M. Mohan Kumar
                    initrd_filename);
816 d6fa4b77 M. Mohan Kumar
            exit(1);
817 d6fa4b77 M. Mohan Kumar
        }
818 d6fa4b77 M. Mohan Kumar
819 45a50b16 Gerd Hoffmann
        initrd_addr = (initrd_max-initrd_size) & ~4095;
820 57a46d05 Alexander Graf
821 7267c094 Anthony Liguori
        initrd_data = g_malloc(initrd_size);
822 57a46d05 Alexander Graf
        load_image(initrd_filename, initrd_data);
823 57a46d05 Alexander Graf
824 57a46d05 Alexander Graf
        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
825 57a46d05 Alexander Graf
        fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
826 57a46d05 Alexander Graf
        fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
827 642a4f96 ths
828 0f9d76e5 liguang
        stl_p(header+0x218, initrd_addr);
829 0f9d76e5 liguang
        stl_p(header+0x21c, initrd_size);
830 642a4f96 ths
    }
831 642a4f96 ths
832 45a50b16 Gerd Hoffmann
    /* load kernel and setup */
833 642a4f96 ths
    setup_size = header[0x1f1];
834 0f9d76e5 liguang
    if (setup_size == 0) {
835 0f9d76e5 liguang
        setup_size = 4;
836 0f9d76e5 liguang
    }
837 642a4f96 ths
    setup_size = (setup_size+1)*512;
838 45a50b16 Gerd Hoffmann
    kernel_size -= setup_size;
839 642a4f96 ths
840 7267c094 Anthony Liguori
    setup  = g_malloc(setup_size);
841 7267c094 Anthony Liguori
    kernel = g_malloc(kernel_size);
842 45a50b16 Gerd Hoffmann
    fseek(f, 0, SEEK_SET);
843 5a41ecc5 Kirill A. Shutemov
    if (fread(setup, 1, setup_size, f) != setup_size) {
844 5a41ecc5 Kirill A. Shutemov
        fprintf(stderr, "fread() failed\n");
845 5a41ecc5 Kirill A. Shutemov
        exit(1);
846 5a41ecc5 Kirill A. Shutemov
    }
847 5a41ecc5 Kirill A. Shutemov
    if (fread(kernel, 1, kernel_size, f) != kernel_size) {
848 5a41ecc5 Kirill A. Shutemov
        fprintf(stderr, "fread() failed\n");
849 5a41ecc5 Kirill A. Shutemov
        exit(1);
850 5a41ecc5 Kirill A. Shutemov
    }
851 642a4f96 ths
    fclose(f);
852 45a50b16 Gerd Hoffmann
    memcpy(setup, header, MIN(sizeof(header), setup_size));
853 57a46d05 Alexander Graf
854 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
855 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
856 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
857 57a46d05 Alexander Graf
858 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
859 57a46d05 Alexander Graf
    fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
860 57a46d05 Alexander Graf
    fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
861 57a46d05 Alexander Graf
862 2e55e842 Gleb Natapov
    option_rom[nb_option_roms].name = "linuxboot.bin";
863 2e55e842 Gleb Natapov
    option_rom[nb_option_roms].bootindex = 0;
864 57a46d05 Alexander Graf
    nb_option_roms++;
865 642a4f96 ths
}
866 642a4f96 ths
867 b41a2cd1 bellard
#define NE2000_NB_MAX 6
868 b41a2cd1 bellard
869 675d6f82 Blue Swirl
static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360,
870 675d6f82 Blue Swirl
                                              0x280, 0x380 };
871 675d6f82 Blue Swirl
static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
872 b41a2cd1 bellard
873 675d6f82 Blue Swirl
static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
874 675d6f82 Blue Swirl
static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
875 6508fe59 bellard
876 48a18b3c Hervé Poussineau
void pc_init_ne2k_isa(ISABus *bus, NICInfo *nd)
877 a41b2ff2 pbrook
{
878 a41b2ff2 pbrook
    static int nb_ne2k = 0;
879 a41b2ff2 pbrook
880 a41b2ff2 pbrook
    if (nb_ne2k == NE2000_NB_MAX)
881 a41b2ff2 pbrook
        return;
882 48a18b3c Hervé Poussineau
    isa_ne2000_init(bus, ne2000_io[nb_ne2k],
883 9453c5bc Gerd Hoffmann
                    ne2000_irq[nb_ne2k], nd);
884 a41b2ff2 pbrook
    nb_ne2k++;
885 a41b2ff2 pbrook
}
886 a41b2ff2 pbrook
887 92a16d7a Blue Swirl
DeviceState *cpu_get_current_apic(void)
888 0e26b7b8 Blue Swirl
{
889 0e26b7b8 Blue Swirl
    if (cpu_single_env) {
890 0e26b7b8 Blue Swirl
        return cpu_single_env->apic_state;
891 0e26b7b8 Blue Swirl
    } else {
892 0e26b7b8 Blue Swirl
        return NULL;
893 0e26b7b8 Blue Swirl
    }
894 0e26b7b8 Blue Swirl
}
895 0e26b7b8 Blue Swirl
896 845773ab Isaku Yamahata
void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
897 53b67b30 Blue Swirl
{
898 c3affe56 Andreas Färber
    X86CPU *cpu = opaque;
899 53b67b30 Blue Swirl
900 53b67b30 Blue Swirl
    if (level) {
901 c3affe56 Andreas Färber
        cpu_interrupt(CPU(cpu), CPU_INTERRUPT_SMI);
902 53b67b30 Blue Swirl
    }
903 53b67b30 Blue Swirl
}
904 53b67b30 Blue Swirl
905 62fc403f Igor Mammedov
static X86CPU *pc_new_cpu(const char *cpu_model, int64_t apic_id,
906 62fc403f Igor Mammedov
                          DeviceState *icc_bridge, Error **errp)
907 31050930 Igor Mammedov
{
908 31050930 Igor Mammedov
    X86CPU *cpu;
909 31050930 Igor Mammedov
    Error *local_err = NULL;
910 31050930 Igor Mammedov
911 62fc403f Igor Mammedov
    cpu = cpu_x86_create(cpu_model, icc_bridge, errp);
912 31050930 Igor Mammedov
    if (!cpu) {
913 31050930 Igor Mammedov
        return cpu;
914 31050930 Igor Mammedov
    }
915 31050930 Igor Mammedov
916 31050930 Igor Mammedov
    object_property_set_int(OBJECT(cpu), apic_id, "apic-id", &local_err);
917 31050930 Igor Mammedov
    object_property_set_bool(OBJECT(cpu), true, "realized", &local_err);
918 31050930 Igor Mammedov
919 31050930 Igor Mammedov
    if (local_err) {
920 31050930 Igor Mammedov
        if (cpu != NULL) {
921 31050930 Igor Mammedov
            object_unref(OBJECT(cpu));
922 31050930 Igor Mammedov
            cpu = NULL;
923 31050930 Igor Mammedov
        }
924 31050930 Igor Mammedov
        error_propagate(errp, local_err);
925 31050930 Igor Mammedov
    }
926 31050930 Igor Mammedov
    return cpu;
927 31050930 Igor Mammedov
}
928 31050930 Igor Mammedov
929 c649983b Igor Mammedov
static const char *current_cpu_model;
930 c649983b Igor Mammedov
931 c649983b Igor Mammedov
void pc_hot_add_cpu(const int64_t id, Error **errp)
932 c649983b Igor Mammedov
{
933 c649983b Igor Mammedov
    DeviceState *icc_bridge;
934 c649983b Igor Mammedov
    int64_t apic_id = x86_cpu_apic_id_from_index(id);
935 c649983b Igor Mammedov
936 8de433cb Igor Mammedov
    if (id < 0) {
937 8de433cb Igor Mammedov
        error_setg(errp, "Invalid CPU id: %" PRIi64, id);
938 8de433cb Igor Mammedov
        return;
939 8de433cb Igor Mammedov
    }
940 8de433cb Igor Mammedov
941 c649983b Igor Mammedov
    if (cpu_exists(apic_id)) {
942 c649983b Igor Mammedov
        error_setg(errp, "Unable to add CPU: %" PRIi64
943 c649983b Igor Mammedov
                   ", it already exists", id);
944 c649983b Igor Mammedov
        return;
945 c649983b Igor Mammedov
    }
946 c649983b Igor Mammedov
947 c649983b Igor Mammedov
    if (id >= max_cpus) {
948 c649983b Igor Mammedov
        error_setg(errp, "Unable to add CPU: %" PRIi64
949 c649983b Igor Mammedov
                   ", max allowed: %d", id, max_cpus - 1);
950 c649983b Igor Mammedov
        return;
951 c649983b Igor Mammedov
    }
952 c649983b Igor Mammedov
953 c649983b Igor Mammedov
    icc_bridge = DEVICE(object_resolve_path_type("icc-bridge",
954 c649983b Igor Mammedov
                                                 TYPE_ICC_BRIDGE, NULL));
955 c649983b Igor Mammedov
    pc_new_cpu(current_cpu_model, apic_id, icc_bridge, errp);
956 c649983b Igor Mammedov
}
957 c649983b Igor Mammedov
958 62fc403f Igor Mammedov
void pc_cpus_init(const char *cpu_model, DeviceState *icc_bridge)
959 70166477 Isaku Yamahata
{
960 70166477 Isaku Yamahata
    int i;
961 53a89e26 Igor Mammedov
    X86CPU *cpu = NULL;
962 31050930 Igor Mammedov
    Error *error = NULL;
963 70166477 Isaku Yamahata
964 70166477 Isaku Yamahata
    /* init CPUs */
965 70166477 Isaku Yamahata
    if (cpu_model == NULL) {
966 70166477 Isaku Yamahata
#ifdef TARGET_X86_64
967 70166477 Isaku Yamahata
        cpu_model = "qemu64";
968 70166477 Isaku Yamahata
#else
969 70166477 Isaku Yamahata
        cpu_model = "qemu32";
970 70166477 Isaku Yamahata
#endif
971 70166477 Isaku Yamahata
    }
972 c649983b Igor Mammedov
    current_cpu_model = cpu_model;
973 70166477 Isaku Yamahata
974 bdeec802 Igor Mammedov
    for (i = 0; i < smp_cpus; i++) {
975 53a89e26 Igor Mammedov
        cpu = pc_new_cpu(cpu_model, x86_cpu_apic_id_from_index(i),
976 53a89e26 Igor Mammedov
                         icc_bridge, &error);
977 31050930 Igor Mammedov
        if (error) {
978 31050930 Igor Mammedov
            fprintf(stderr, "%s\n", error_get_pretty(error));
979 31050930 Igor Mammedov
            error_free(error);
980 bdeec802 Igor Mammedov
            exit(1);
981 bdeec802 Igor Mammedov
        }
982 70166477 Isaku Yamahata
    }
983 53a89e26 Igor Mammedov
984 53a89e26 Igor Mammedov
    /* map APIC MMIO area if CPU has APIC */
985 53a89e26 Igor Mammedov
    if (cpu && cpu->env.apic_state) {
986 53a89e26 Igor Mammedov
        /* XXX: what if the base changes? */
987 53a89e26 Igor Mammedov
        sysbus_mmio_map_overlap(SYS_BUS_DEVICE(icc_bridge), 0,
988 53a89e26 Igor Mammedov
                                APIC_DEFAULT_ADDRESS, 0x1000);
989 53a89e26 Igor Mammedov
    }
990 70166477 Isaku Yamahata
}
991 70166477 Isaku Yamahata
992 f7e4dd6c Gerd Hoffmann
void pc_acpi_init(const char *default_dsdt)
993 f7e4dd6c Gerd Hoffmann
{
994 c5a98cf3 Laszlo Ersek
    char *filename;
995 f7e4dd6c Gerd Hoffmann
996 f7e4dd6c Gerd Hoffmann
    if (acpi_tables != NULL) {
997 f7e4dd6c Gerd Hoffmann
        /* manually set via -acpitable, leave it alone */
998 f7e4dd6c Gerd Hoffmann
        return;
999 f7e4dd6c Gerd Hoffmann
    }
1000 f7e4dd6c Gerd Hoffmann
1001 f7e4dd6c Gerd Hoffmann
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, default_dsdt);
1002 f7e4dd6c Gerd Hoffmann
    if (filename == NULL) {
1003 f7e4dd6c Gerd Hoffmann
        fprintf(stderr, "WARNING: failed to find %s\n", default_dsdt);
1004 c5a98cf3 Laszlo Ersek
    } else {
1005 c5a98cf3 Laszlo Ersek
        char *arg;
1006 c5a98cf3 Laszlo Ersek
        QemuOpts *opts;
1007 c5a98cf3 Laszlo Ersek
        Error *err = NULL;
1008 f7e4dd6c Gerd Hoffmann
1009 c5a98cf3 Laszlo Ersek
        arg = g_strdup_printf("file=%s", filename);
1010 0c764a9d Laszlo Ersek
1011 c5a98cf3 Laszlo Ersek
        /* creates a deep copy of "arg" */
1012 c5a98cf3 Laszlo Ersek
        opts = qemu_opts_parse(qemu_find_opts("acpi"), arg, 0);
1013 c5a98cf3 Laszlo Ersek
        g_assert(opts != NULL);
1014 0c764a9d Laszlo Ersek
1015 c5a98cf3 Laszlo Ersek
        acpi_table_add(opts, &err);
1016 c5a98cf3 Laszlo Ersek
        if (err) {
1017 c5a98cf3 Laszlo Ersek
            fprintf(stderr, "WARNING: failed to load %s: %s\n", filename,
1018 c5a98cf3 Laszlo Ersek
                    error_get_pretty(err));
1019 c5a98cf3 Laszlo Ersek
            error_free(err);
1020 c5a98cf3 Laszlo Ersek
        }
1021 c5a98cf3 Laszlo Ersek
        g_free(arg);
1022 c5a98cf3 Laszlo Ersek
        g_free(filename);
1023 f7e4dd6c Gerd Hoffmann
    }
1024 f7e4dd6c Gerd Hoffmann
}
1025 f7e4dd6c Gerd Hoffmann
1026 a88b362c Laszlo Ersek
FWCfgState *pc_memory_init(MemoryRegion *system_memory,
1027 a88b362c Laszlo Ersek
                           const char *kernel_filename,
1028 a88b362c Laszlo Ersek
                           const char *kernel_cmdline,
1029 a88b362c Laszlo Ersek
                           const char *initrd_filename,
1030 a88b362c Laszlo Ersek
                           ram_addr_t below_4g_mem_size,
1031 a88b362c Laszlo Ersek
                           ram_addr_t above_4g_mem_size,
1032 a88b362c Laszlo Ersek
                           MemoryRegion *rom_memory,
1033 a88b362c Laszlo Ersek
                           MemoryRegion **ram_memory)
1034 80cabfad bellard
{
1035 cbc5b5f3 Jordan Justen
    int linux_boot, i;
1036 cbc5b5f3 Jordan Justen
    MemoryRegion *ram, *option_rom_mr;
1037 00cb2a99 Avi Kivity
    MemoryRegion *ram_below_4g, *ram_above_4g;
1038 a88b362c Laszlo Ersek
    FWCfgState *fw_cfg;
1039 d592d303 bellard
1040 80cabfad bellard
    linux_boot = (kernel_filename != NULL);
1041 80cabfad bellard
1042 00cb2a99 Avi Kivity
    /* Allocate RAM.  We allocate it as a single memory region and use
1043 66a0a2cb Dong Xu Wang
     * aliases to address portions of it, mostly for backwards compatibility
1044 00cb2a99 Avi Kivity
     * with older qemus that used qemu_ram_alloc().
1045 00cb2a99 Avi Kivity
     */
1046 7267c094 Anthony Liguori
    ram = g_malloc(sizeof(*ram));
1047 2c9b15ca Paolo Bonzini
    memory_region_init_ram(ram, NULL, "pc.ram",
1048 00cb2a99 Avi Kivity
                           below_4g_mem_size + above_4g_mem_size);
1049 c5705a77 Avi Kivity
    vmstate_register_ram_global(ram);
1050 ae0a5466 Avi Kivity
    *ram_memory = ram;
1051 7267c094 Anthony Liguori
    ram_below_4g = g_malloc(sizeof(*ram_below_4g));
1052 2c9b15ca Paolo Bonzini
    memory_region_init_alias(ram_below_4g, NULL, "ram-below-4g", ram,
1053 00cb2a99 Avi Kivity
                             0, below_4g_mem_size);
1054 00cb2a99 Avi Kivity
    memory_region_add_subregion(system_memory, 0, ram_below_4g);
1055 bbe80adf Alex Williamson
    if (above_4g_mem_size > 0) {
1056 7267c094 Anthony Liguori
        ram_above_4g = g_malloc(sizeof(*ram_above_4g));
1057 2c9b15ca Paolo Bonzini
        memory_region_init_alias(ram_above_4g, NULL, "ram-above-4g", ram,
1058 00cb2a99 Avi Kivity
                                 below_4g_mem_size, above_4g_mem_size);
1059 00cb2a99 Avi Kivity
        memory_region_add_subregion(system_memory, 0x100000000ULL,
1060 00cb2a99 Avi Kivity
                                    ram_above_4g);
1061 bbe80adf Alex Williamson
    }
1062 82b36dc3 aliguori
1063 cbc5b5f3 Jordan Justen
1064 cbc5b5f3 Jordan Justen
    /* Initialize PC system firmware */
1065 cbc5b5f3 Jordan Justen
    pc_system_firmware_init(rom_memory);
1066 00cb2a99 Avi Kivity
1067 7267c094 Anthony Liguori
    option_rom_mr = g_malloc(sizeof(*option_rom_mr));
1068 2c9b15ca Paolo Bonzini
    memory_region_init_ram(option_rom_mr, NULL, "pc.rom", PC_ROM_SIZE);
1069 c5705a77 Avi Kivity
    vmstate_register_ram_global(option_rom_mr);
1070 4463aee6 Jan Kiszka
    memory_region_add_subregion_overlap(rom_memory,
1071 00cb2a99 Avi Kivity
                                        PC_ROM_MIN_VGA,
1072 00cb2a99 Avi Kivity
                                        option_rom_mr,
1073 00cb2a99 Avi Kivity
                                        1);
1074 f753ff16 pbrook
1075 bf483392 Alexander Graf
    fw_cfg = bochs_bios_init();
1076 8832cb80 Gerd Hoffmann
    rom_set_fw(fw_cfg);
1077 1d108d97 Alexander Graf
1078 f753ff16 pbrook
    if (linux_boot) {
1079 81a204e4 Eduard - Gabriel Munteanu
        load_linux(fw_cfg, kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
1080 f753ff16 pbrook
    }
1081 f753ff16 pbrook
1082 f753ff16 pbrook
    for (i = 0; i < nb_option_roms; i++) {
1083 2e55e842 Gleb Natapov
        rom_add_option(option_rom[i].name, option_rom[i].bootindex);
1084 406c8df3 Glauber Costa
    }
1085 459ae5ea Gleb Natapov
    return fw_cfg;
1086 3d53f5c3 Isaku Yamahata
}
1087 3d53f5c3 Isaku Yamahata
1088 845773ab Isaku Yamahata
qemu_irq *pc_allocate_cpu_irq(void)
1089 845773ab Isaku Yamahata
{
1090 845773ab Isaku Yamahata
    return qemu_allocate_irqs(pic_irq_request, NULL, 1);
1091 845773ab Isaku Yamahata
}
1092 845773ab Isaku Yamahata
1093 48a18b3c Hervé Poussineau
DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus)
1094 765d7908 Isaku Yamahata
{
1095 ad6d45fa Anthony Liguori
    DeviceState *dev = NULL;
1096 ad6d45fa Anthony Liguori
1097 16094b75 Aurelien Jarno
    if (pci_bus) {
1098 16094b75 Aurelien Jarno
        PCIDevice *pcidev = pci_vga_init(pci_bus);
1099 16094b75 Aurelien Jarno
        dev = pcidev ? &pcidev->qdev : NULL;
1100 16094b75 Aurelien Jarno
    } else if (isa_bus) {
1101 16094b75 Aurelien Jarno
        ISADevice *isadev = isa_vga_init(isa_bus);
1102 4a17cc4f Andreas Färber
        dev = isadev ? DEVICE(isadev) : NULL;
1103 765d7908 Isaku Yamahata
    }
1104 ad6d45fa Anthony Liguori
    return dev;
1105 765d7908 Isaku Yamahata
}
1106 765d7908 Isaku Yamahata
1107 4556bd8b Blue Swirl
static void cpu_request_exit(void *opaque, int irq, int level)
1108 4556bd8b Blue Swirl
{
1109 4a8fa5dc Andreas Färber
    CPUX86State *env = cpu_single_env;
1110 4556bd8b Blue Swirl
1111 4556bd8b Blue Swirl
    if (env && level) {
1112 60a3e17a Andreas Färber
        cpu_exit(CPU(x86_env_get_cpu(env)));
1113 4556bd8b Blue Swirl
    }
1114 4556bd8b Blue Swirl
}
1115 4556bd8b Blue Swirl
1116 258711c6 Julien Grall
static const MemoryRegionOps ioport80_io_ops = {
1117 258711c6 Julien Grall
    .write = ioport80_write,
1118 c02e1eac Julien Grall
    .read = ioport80_read,
1119 258711c6 Julien Grall
    .endianness = DEVICE_NATIVE_ENDIAN,
1120 258711c6 Julien Grall
    .impl = {
1121 258711c6 Julien Grall
        .min_access_size = 1,
1122 258711c6 Julien Grall
        .max_access_size = 1,
1123 258711c6 Julien Grall
    },
1124 258711c6 Julien Grall
};
1125 258711c6 Julien Grall
1126 258711c6 Julien Grall
static const MemoryRegionOps ioportF0_io_ops = {
1127 258711c6 Julien Grall
    .write = ioportF0_write,
1128 c02e1eac Julien Grall
    .read = ioportF0_read,
1129 258711c6 Julien Grall
    .endianness = DEVICE_NATIVE_ENDIAN,
1130 258711c6 Julien Grall
    .impl = {
1131 258711c6 Julien Grall
        .min_access_size = 1,
1132 258711c6 Julien Grall
        .max_access_size = 1,
1133 258711c6 Julien Grall
    },
1134 258711c6 Julien Grall
};
1135 258711c6 Julien Grall
1136 48a18b3c Hervé Poussineau
void pc_basic_device_init(ISABus *isa_bus, qemu_irq *gsi,
1137 1611977c Anthony PERARD
                          ISADevice **rtc_state,
1138 34d4260e Kevin Wolf
                          ISADevice **floppy,
1139 1611977c Anthony PERARD
                          bool no_vmport)
1140 ffe513da Isaku Yamahata
{
1141 ffe513da Isaku Yamahata
    int i;
1142 ffe513da Isaku Yamahata
    DriveInfo *fd[MAX_FD];
1143 ce967e2f Jan Kiszka
    DeviceState *hpet = NULL;
1144 ce967e2f Jan Kiszka
    int pit_isa_irq = 0;
1145 ce967e2f Jan Kiszka
    qemu_irq pit_alt_irq = NULL;
1146 7d932dfd Jan Kiszka
    qemu_irq rtc_irq = NULL;
1147 956a3e6b Blue Swirl
    qemu_irq *a20_line;
1148 c2d8d311 Stefano Stabellini
    ISADevice *i8042, *port92, *vmmouse, *pit = NULL;
1149 4556bd8b Blue Swirl
    qemu_irq *cpu_exit_irq;
1150 258711c6 Julien Grall
    MemoryRegion *ioport80_io = g_new(MemoryRegion, 1);
1151 258711c6 Julien Grall
    MemoryRegion *ioportF0_io = g_new(MemoryRegion, 1);
1152 ffe513da Isaku Yamahata
1153 2c9b15ca Paolo Bonzini
    memory_region_init_io(ioport80_io, NULL, &ioport80_io_ops, NULL, "ioport80", 1);
1154 258711c6 Julien Grall
    memory_region_add_subregion(isa_bus->address_space_io, 0x80, ioport80_io);
1155 ffe513da Isaku Yamahata
1156 2c9b15ca Paolo Bonzini
    memory_region_init_io(ioportF0_io, NULL, &ioportF0_io_ops, NULL, "ioportF0", 1);
1157 258711c6 Julien Grall
    memory_region_add_subregion(isa_bus->address_space_io, 0xf0, ioportF0_io);
1158 ffe513da Isaku Yamahata
1159 5d17c0d2 Jan Kiszka
    /*
1160 5d17c0d2 Jan Kiszka
     * Check if an HPET shall be created.
1161 5d17c0d2 Jan Kiszka
     *
1162 5d17c0d2 Jan Kiszka
     * Without KVM_CAP_PIT_STATE2, we cannot switch off the in-kernel PIT
1163 5d17c0d2 Jan Kiszka
     * when the HPET wants to take over. Thus we have to disable the latter.
1164 5d17c0d2 Jan Kiszka
     */
1165 5d17c0d2 Jan Kiszka
    if (!no_hpet && (!kvm_irqchip_in_kernel() || kvm_has_pit_state2())) {
1166 ce967e2f Jan Kiszka
        hpet = sysbus_try_create_simple("hpet", HPET_BASE, NULL);
1167 822557eb Jan Kiszka
1168 dd703b99 Blue Swirl
        if (hpet) {
1169 b881fbe9 Jan Kiszka
            for (i = 0; i < GSI_NUM_PINS; i++) {
1170 1356b98d Andreas Färber
                sysbus_connect_irq(SYS_BUS_DEVICE(hpet), i, gsi[i]);
1171 dd703b99 Blue Swirl
            }
1172 ce967e2f Jan Kiszka
            pit_isa_irq = -1;
1173 ce967e2f Jan Kiszka
            pit_alt_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_PIT_INT);
1174 ce967e2f Jan Kiszka
            rtc_irq = qdev_get_gpio_in(hpet, HPET_LEGACY_RTC_INT);
1175 822557eb Jan Kiszka
        }
1176 ffe513da Isaku Yamahata
    }
1177 48a18b3c Hervé Poussineau
    *rtc_state = rtc_init(isa_bus, 2000, rtc_irq);
1178 7d932dfd Jan Kiszka
1179 7d932dfd Jan Kiszka
    qemu_register_boot_set(pc_boot_set, *rtc_state);
1180 7d932dfd Jan Kiszka
1181 c2d8d311 Stefano Stabellini
    if (!xen_enabled()) {
1182 c2d8d311 Stefano Stabellini
        if (kvm_irqchip_in_kernel()) {
1183 c2d8d311 Stefano Stabellini
            pit = kvm_pit_init(isa_bus, 0x40);
1184 c2d8d311 Stefano Stabellini
        } else {
1185 c2d8d311 Stefano Stabellini
            pit = pit_init(isa_bus, 0x40, pit_isa_irq, pit_alt_irq);
1186 c2d8d311 Stefano Stabellini
        }
1187 c2d8d311 Stefano Stabellini
        if (hpet) {
1188 c2d8d311 Stefano Stabellini
            /* connect PIT to output control line of the HPET */
1189 4a17cc4f Andreas Färber
            qdev_connect_gpio_out(hpet, 0, qdev_get_gpio_in(DEVICE(pit), 0));
1190 c2d8d311 Stefano Stabellini
        }
1191 c2d8d311 Stefano Stabellini
        pcspk_init(isa_bus, pit);
1192 ce967e2f Jan Kiszka
    }
1193 ffe513da Isaku Yamahata
1194 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
1195 ffe513da Isaku Yamahata
        if (serial_hds[i]) {
1196 48a18b3c Hervé Poussineau
            serial_isa_init(isa_bus, i, serial_hds[i]);
1197 ffe513da Isaku Yamahata
        }
1198 ffe513da Isaku Yamahata
    }
1199 ffe513da Isaku Yamahata
1200 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
1201 ffe513da Isaku Yamahata
        if (parallel_hds[i]) {
1202 48a18b3c Hervé Poussineau
            parallel_init(isa_bus, i, parallel_hds[i]);
1203 ffe513da Isaku Yamahata
        }
1204 ffe513da Isaku Yamahata
    }
1205 ffe513da Isaku Yamahata
1206 cc36a7a2 Andreas Färber
    a20_line = qemu_allocate_irqs(handle_a20_line_change,
1207 cc36a7a2 Andreas Färber
                                  x86_env_get_cpu(first_cpu), 2);
1208 48a18b3c Hervé Poussineau
    i8042 = isa_create_simple(isa_bus, "i8042");
1209 4b78a802 Blue Swirl
    i8042_setup_a20_line(i8042, &a20_line[0]);
1210 1611977c Anthony PERARD
    if (!no_vmport) {
1211 48a18b3c Hervé Poussineau
        vmport_init(isa_bus);
1212 48a18b3c Hervé Poussineau
        vmmouse = isa_try_create(isa_bus, "vmmouse");
1213 1611977c Anthony PERARD
    } else {
1214 1611977c Anthony PERARD
        vmmouse = NULL;
1215 1611977c Anthony PERARD
    }
1216 86d86414 Blue Swirl
    if (vmmouse) {
1217 4a17cc4f Andreas Färber
        DeviceState *dev = DEVICE(vmmouse);
1218 4a17cc4f Andreas Färber
        qdev_prop_set_ptr(dev, "ps2_mouse", i8042);
1219 4a17cc4f Andreas Färber
        qdev_init_nofail(dev);
1220 86d86414 Blue Swirl
    }
1221 48a18b3c Hervé Poussineau
    port92 = isa_create_simple(isa_bus, "port92");
1222 4b78a802 Blue Swirl
    port92_init(port92, &a20_line[1]);
1223 956a3e6b Blue Swirl
1224 4556bd8b Blue Swirl
    cpu_exit_irq = qemu_allocate_irqs(cpu_request_exit, NULL, 1);
1225 4556bd8b Blue Swirl
    DMA_init(0, cpu_exit_irq);
1226 ffe513da Isaku Yamahata
1227 ffe513da Isaku Yamahata
    for(i = 0; i < MAX_FD; i++) {
1228 ffe513da Isaku Yamahata
        fd[i] = drive_get(IF_FLOPPY, 0, i);
1229 ffe513da Isaku Yamahata
    }
1230 48a18b3c Hervé Poussineau
    *floppy = fdctrl_init_isa(isa_bus, fd);
1231 ffe513da Isaku Yamahata
}
1232 ffe513da Isaku Yamahata
1233 9011a1a7 Isaku Yamahata
void pc_nic_init(ISABus *isa_bus, PCIBus *pci_bus)
1234 9011a1a7 Isaku Yamahata
{
1235 9011a1a7 Isaku Yamahata
    int i;
1236 9011a1a7 Isaku Yamahata
1237 9011a1a7 Isaku Yamahata
    for (i = 0; i < nb_nics; i++) {
1238 9011a1a7 Isaku Yamahata
        NICInfo *nd = &nd_table[i];
1239 9011a1a7 Isaku Yamahata
1240 9011a1a7 Isaku Yamahata
        if (!pci_bus || (nd->model && strcmp(nd->model, "ne2k_isa") == 0)) {
1241 9011a1a7 Isaku Yamahata
            pc_init_ne2k_isa(isa_bus, nd);
1242 9011a1a7 Isaku Yamahata
        } else {
1243 9011a1a7 Isaku Yamahata
            pci_nic_init_nofail(nd, "e1000", NULL);
1244 9011a1a7 Isaku Yamahata
        }
1245 9011a1a7 Isaku Yamahata
    }
1246 9011a1a7 Isaku Yamahata
}
1247 9011a1a7 Isaku Yamahata
1248 845773ab Isaku Yamahata
void pc_pci_device_init(PCIBus *pci_bus)
1249 e3a5cf42 Isaku Yamahata
{
1250 e3a5cf42 Isaku Yamahata
    int max_bus;
1251 e3a5cf42 Isaku Yamahata
    int bus;
1252 e3a5cf42 Isaku Yamahata
1253 e3a5cf42 Isaku Yamahata
    max_bus = drive_get_max_bus(IF_SCSI);
1254 e3a5cf42 Isaku Yamahata
    for (bus = 0; bus <= max_bus; bus++) {
1255 e3a5cf42 Isaku Yamahata
        pci_create_simple(pci_bus, -1, "lsi53c895a");
1256 e3a5cf42 Isaku Yamahata
    }
1257 e3a5cf42 Isaku Yamahata
}
1258 a39e3564 Jason Baron
1259 a39e3564 Jason Baron
void ioapic_init_gsi(GSIState *gsi_state, const char *parent_name)
1260 a39e3564 Jason Baron
{
1261 a39e3564 Jason Baron
    DeviceState *dev;
1262 a39e3564 Jason Baron
    SysBusDevice *d;
1263 a39e3564 Jason Baron
    unsigned int i;
1264 a39e3564 Jason Baron
1265 a39e3564 Jason Baron
    if (kvm_irqchip_in_kernel()) {
1266 a39e3564 Jason Baron
        dev = qdev_create(NULL, "kvm-ioapic");
1267 a39e3564 Jason Baron
    } else {
1268 a39e3564 Jason Baron
        dev = qdev_create(NULL, "ioapic");
1269 a39e3564 Jason Baron
    }
1270 a39e3564 Jason Baron
    if (parent_name) {
1271 a39e3564 Jason Baron
        object_property_add_child(object_resolve_path(parent_name, NULL),
1272 a39e3564 Jason Baron
                                  "ioapic", OBJECT(dev), NULL);
1273 a39e3564 Jason Baron
    }
1274 a39e3564 Jason Baron
    qdev_init_nofail(dev);
1275 1356b98d Andreas Färber
    d = SYS_BUS_DEVICE(dev);
1276 3a4a4697 Laszlo Ersek
    sysbus_mmio_map(d, 0, IO_APIC_DEFAULT_ADDRESS);
1277 a39e3564 Jason Baron
1278 a39e3564 Jason Baron
    for (i = 0; i < IOAPIC_NUM_PINS; i++) {
1279 a39e3564 Jason Baron
        gsi_state->ioapic_irq[i] = qdev_get_gpio_in(dev, i);
1280 a39e3564 Jason Baron
    }
1281 a39e3564 Jason Baron
}