]> git.proxmox.com Git - qemu.git/blame - hw/pc.c
Route PC irqs to ISA bus instead of i8259 directly
[qemu.git] / hw / pc.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU PC System Emulator
5fafdf24 3 *
80cabfad 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
80cabfad
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
87ecb68b
PB
24#include "hw.h"
25#include "pc.h"
26#include "fdc.h"
27#include "pci.h"
28#include "block.h"
29#include "sysemu.h"
30#include "audio/audio.h"
31#include "net.h"
32#include "smbus.h"
33#include "boards.h"
376253ec 34#include "monitor.h"
3cce6243 35#include "fw_cfg.h"
16b29ae1 36#include "hpet_emul.h"
9dd986cc 37#include "watchdog.h"
b6f6e3d3 38#include "smbios.h"
80cabfad 39
b41a2cd1
FB
40/* output Bochs bios info messages */
41//#define DEBUG_BIOS
42
f16408df
AG
43/* Show multiboot debug output */
44//#define DEBUG_MULTIBOOT
45
80cabfad
FB
46#define BIOS_FILENAME "bios.bin"
47#define VGABIOS_FILENAME "vgabios.bin"
de9258a8 48#define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
80cabfad 49
7fb4fdcf
AZ
50#define PC_MAX_BIOS_SIZE (4 * 1024 * 1024)
51
a80274c3
PB
52/* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
53#define ACPI_DATA_SIZE 0x10000
3cce6243 54#define BIOS_CFG_IOPORT 0x510
8a92ea2f 55#define FW_CFG_ACPI_TABLES (FW_CFG_ARCH_LOCAL + 0)
b6f6e3d3 56#define FW_CFG_SMBIOS_ENTRIES (FW_CFG_ARCH_LOCAL + 1)
6b35e7bf 57#define FW_CFG_IRQ0_OVERRIDE (FW_CFG_ARCH_LOCAL + 2)
80cabfad 58
e4bcb14c
TS
59#define MAX_IDE_BUS 2
60
baca51fa 61static fdctrl_t *floppy_controller;
b0a21b53 62static RTCState *rtc_state;
ec844b96 63static PITState *pit;
d592d303 64static IOAPICState *ioapic;
a5954d5c 65static PCIDevice *i440fx_state;
80cabfad 66
e28f9884
GC
67typedef struct rom_reset_data {
68 uint8_t *data;
69 target_phys_addr_t addr;
70 unsigned size;
71} RomResetData;
72
73static void option_rom_reset(void *_rrd)
74{
75 RomResetData *rrd = _rrd;
76
77 cpu_physical_memory_write_rom(rrd->addr, rrd->data, rrd->size);
78}
79
80static void option_rom_setup_reset(target_phys_addr_t addr, unsigned size)
81{
82 RomResetData *rrd = qemu_malloc(sizeof *rrd);
83
84 rrd->data = qemu_malloc(size);
85 cpu_physical_memory_read(addr, rrd->data, size);
86 rrd->addr = addr;
87 rrd->size = size;
a08d4367 88 qemu_register_reset(option_rom_reset, rrd);
e28f9884
GC
89}
90
1452411b
AK
91typedef struct isa_irq_state {
92 qemu_irq *i8259;
93} IsaIrqState;
94
95static void isa_irq_handler(void *opaque, int n, int level)
96{
97 IsaIrqState *isa = (IsaIrqState *)opaque;
98
99 qemu_set_irq(isa->i8259[n], level);
100}
101
b41a2cd1 102static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
80cabfad
FB
103{
104}
105
f929aad6 106/* MSDOS compatibility mode FPU exception support */
d537cf6c 107static qemu_irq ferr_irq;
f929aad6
FB
108/* XXX: add IGNNE support */
109void cpu_set_ferr(CPUX86State *s)
110{
d537cf6c 111 qemu_irq_raise(ferr_irq);
f929aad6
FB
112}
113
114static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
115{
d537cf6c 116 qemu_irq_lower(ferr_irq);
f929aad6
FB
117}
118
28ab0e2e 119/* TSC handling */
28ab0e2e
FB
120uint64_t cpu_get_tsc(CPUX86State *env)
121{
1dce7c3c
FB
122 /* Note: when using kqemu, it is more logical to return the host TSC
123 because kqemu does not trap the RDTSC instruction for
124 performance reasons */
640f42e4 125#ifdef CONFIG_KQEMU
1dce7c3c
FB
126 if (env->kqemu_enabled) {
127 return cpu_get_real_ticks();
5fafdf24 128 } else
1dce7c3c
FB
129#endif
130 {
131 return cpu_get_ticks();
132 }
28ab0e2e
FB
133}
134
a5954d5c
FB
135/* SMM support */
136void cpu_smm_update(CPUState *env)
137{
138 if (i440fx_state && env == first_cpu)
139 i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
140}
141
142
3de388f6
FB
143/* IRQ handling */
144int cpu_get_pic_interrupt(CPUState *env)
145{
146 int intno;
147
3de388f6
FB
148 intno = apic_get_interrupt(env);
149 if (intno >= 0) {
150 /* set irq request if a PIC irq is still pending */
151 /* XXX: improve that */
5fafdf24 152 pic_update_irq(isa_pic);
3de388f6
FB
153 return intno;
154 }
3de388f6 155 /* read the irq from the PIC */
0e21e12b
TS
156 if (!apic_accept_pic_intr(env))
157 return -1;
158
3de388f6
FB
159 intno = pic_read_irq(isa_pic);
160 return intno;
161}
162
d537cf6c 163static void pic_irq_request(void *opaque, int irq, int level)
3de388f6 164{
a5b38b51
AJ
165 CPUState *env = first_cpu;
166
d5529471
AJ
167 if (env->apic_state) {
168 while (env) {
169 if (apic_accept_pic_intr(env))
1a7de94a 170 apic_deliver_pic_intr(env, level);
d5529471
AJ
171 env = env->next_cpu;
172 }
173 } else {
b614106a
AJ
174 if (level)
175 cpu_interrupt(env, CPU_INTERRUPT_HARD);
176 else
177 cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
a5b38b51 178 }
3de388f6
FB
179}
180
b0a21b53
FB
181/* PC cmos mappings */
182
80cabfad
FB
183#define REG_EQUIPMENT_BYTE 0x14
184
777428f2
FB
185static int cmos_get_fd_drive_type(int fd0)
186{
187 int val;
188
189 switch (fd0) {
190 case 0:
191 /* 1.44 Mb 3"5 drive */
192 val = 4;
193 break;
194 case 1:
195 /* 2.88 Mb 3"5 drive */
196 val = 5;
197 break;
198 case 2:
199 /* 1.2 Mb 5"5 drive */
200 val = 2;
201 break;
202 default:
203 val = 0;
204 break;
205 }
206 return val;
207}
208
5fafdf24 209static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
ba6c2377
FB
210{
211 RTCState *s = rtc_state;
212 int cylinders, heads, sectors;
213 bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
214 rtc_set_memory(s, type_ofs, 47);
215 rtc_set_memory(s, info_ofs, cylinders);
216 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
217 rtc_set_memory(s, info_ofs + 2, heads);
218 rtc_set_memory(s, info_ofs + 3, 0xff);
219 rtc_set_memory(s, info_ofs + 4, 0xff);
220 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
221 rtc_set_memory(s, info_ofs + 6, cylinders);
222 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
223 rtc_set_memory(s, info_ofs + 8, sectors);
224}
225
6ac0e82d
AZ
226/* convert boot_device letter to something recognizable by the bios */
227static int boot_device2nibble(char boot_device)
228{
229 switch(boot_device) {
230 case 'a':
231 case 'b':
232 return 0x01; /* floppy boot */
233 case 'c':
234 return 0x02; /* hard drive boot */
235 case 'd':
236 return 0x03; /* CD-ROM boot */
237 case 'n':
238 return 0x04; /* Network boot */
239 }
240 return 0;
241}
242
0ecdffbb
AJ
243/* copy/pasted from cmos_init, should be made a general function
244 and used there as well */
3b4366de 245static int pc_boot_set(void *opaque, const char *boot_device)
0ecdffbb 246{
376253ec 247 Monitor *mon = cur_mon;
0ecdffbb 248#define PC_MAX_BOOT_DEVICES 3
3b4366de 249 RTCState *s = (RTCState *)opaque;
0ecdffbb
AJ
250 int nbds, bds[3] = { 0, };
251 int i;
252
253 nbds = strlen(boot_device);
254 if (nbds > PC_MAX_BOOT_DEVICES) {
376253ec 255 monitor_printf(mon, "Too many boot devices for PC\n");
0ecdffbb
AJ
256 return(1);
257 }
258 for (i = 0; i < nbds; i++) {
259 bds[i] = boot_device2nibble(boot_device[i]);
260 if (bds[i] == 0) {
376253ec
AL
261 monitor_printf(mon, "Invalid boot device for PC: '%c'\n",
262 boot_device[i]);
0ecdffbb
AJ
263 return(1);
264 }
265 }
266 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
267 rtc_set_memory(s, 0x38, (bds[2] << 4));
268 return(0);
269}
270
ba6c2377 271/* hd_table must contain 4 block drivers */
00f82b8a
AJ
272static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
273 const char *boot_device, BlockDriverState **hd_table)
80cabfad 274{
b0a21b53 275 RTCState *s = rtc_state;
28c5af54 276 int nbds, bds[3] = { 0, };
80cabfad 277 int val;
b41a2cd1 278 int fd0, fd1, nb;
ba6c2377 279 int i;
b0a21b53 280
b0a21b53 281 /* various important CMOS locations needed by PC/Bochs bios */
80cabfad
FB
282
283 /* memory size */
333190eb
FB
284 val = 640; /* base memory in K */
285 rtc_set_memory(s, 0x15, val);
286 rtc_set_memory(s, 0x16, val >> 8);
287
80cabfad
FB
288 val = (ram_size / 1024) - 1024;
289 if (val > 65535)
290 val = 65535;
b0a21b53
FB
291 rtc_set_memory(s, 0x17, val);
292 rtc_set_memory(s, 0x18, val >> 8);
293 rtc_set_memory(s, 0x30, val);
294 rtc_set_memory(s, 0x31, val >> 8);
80cabfad 295
00f82b8a
AJ
296 if (above_4g_mem_size) {
297 rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
298 rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
299 rtc_set_memory(s, 0x5d, (uint64_t)above_4g_mem_size >> 32);
300 }
301
9da98861
FB
302 if (ram_size > (16 * 1024 * 1024))
303 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
304 else
305 val = 0;
80cabfad
FB
306 if (val > 65535)
307 val = 65535;
b0a21b53
FB
308 rtc_set_memory(s, 0x34, val);
309 rtc_set_memory(s, 0x35, val >> 8);
3b46e624 310
298e01b6
AJ
311 /* set the number of CPU */
312 rtc_set_memory(s, 0x5f, smp_cpus - 1);
313
6ac0e82d 314 /* set boot devices, and disable floppy signature check if requested */
28c5af54
JM
315#define PC_MAX_BOOT_DEVICES 3
316 nbds = strlen(boot_device);
317 if (nbds > PC_MAX_BOOT_DEVICES) {
318 fprintf(stderr, "Too many boot devices for PC\n");
319 exit(1);
320 }
321 for (i = 0; i < nbds; i++) {
322 bds[i] = boot_device2nibble(boot_device[i]);
323 if (bds[i] == 0) {
324 fprintf(stderr, "Invalid boot device for PC: '%c'\n",
325 boot_device[i]);
326 exit(1);
327 }
328 }
329 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
330 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
80cabfad 331
b41a2cd1
FB
332 /* floppy type */
333
baca51fa
FB
334 fd0 = fdctrl_get_drive_type(floppy_controller, 0);
335 fd1 = fdctrl_get_drive_type(floppy_controller, 1);
80cabfad 336
777428f2 337 val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
b0a21b53 338 rtc_set_memory(s, 0x10, val);
3b46e624 339
b0a21b53 340 val = 0;
b41a2cd1 341 nb = 0;
80cabfad
FB
342 if (fd0 < 3)
343 nb++;
344 if (fd1 < 3)
345 nb++;
346 switch (nb) {
347 case 0:
348 break;
349 case 1:
b0a21b53 350 val |= 0x01; /* 1 drive, ready for boot */
80cabfad
FB
351 break;
352 case 2:
b0a21b53 353 val |= 0x41; /* 2 drives, ready for boot */
80cabfad
FB
354 break;
355 }
b0a21b53
FB
356 val |= 0x02; /* FPU is there */
357 val |= 0x04; /* PS/2 mouse installed */
358 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
359
ba6c2377
FB
360 /* hard drives */
361
362 rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
363 if (hd_table[0])
364 cmos_init_hd(0x19, 0x1b, hd_table[0]);
5fafdf24 365 if (hd_table[1])
ba6c2377
FB
366 cmos_init_hd(0x1a, 0x24, hd_table[1]);
367
368 val = 0;
40b6ecc6 369 for (i = 0; i < 4; i++) {
ba6c2377 370 if (hd_table[i]) {
46d4767d
FB
371 int cylinders, heads, sectors, translation;
372 /* NOTE: bdrv_get_geometry_hint() returns the physical
373 geometry. It is always such that: 1 <= sects <= 63, 1
374 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
375 geometry can be different if a translation is done. */
376 translation = bdrv_get_translation_hint(hd_table[i]);
377 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
378 bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
379 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
380 /* No translation. */
381 translation = 0;
382 } else {
383 /* LBA translation. */
384 translation = 1;
385 }
40b6ecc6 386 } else {
46d4767d 387 translation--;
ba6c2377 388 }
ba6c2377
FB
389 val |= translation << (i * 2);
390 }
40b6ecc6 391 }
ba6c2377 392 rtc_set_memory(s, 0x39, val);
80cabfad
FB
393}
394
59b8ad81
FB
395void ioport_set_a20(int enable)
396{
397 /* XXX: send to all CPUs ? */
398 cpu_x86_set_a20(first_cpu, enable);
399}
400
401int ioport_get_a20(void)
402{
403 return ((first_cpu->a20_mask >> 20) & 1);
404}
405
e1a23744
FB
406static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
407{
59b8ad81 408 ioport_set_a20((val >> 1) & 1);
e1a23744
FB
409 /* XXX: bit 0 is fast reset */
410}
411
412static uint32_t ioport92_read(void *opaque, uint32_t addr)
413{
59b8ad81 414 return ioport_get_a20() << 1;
e1a23744
FB
415}
416
80cabfad
FB
417/***********************************************************/
418/* Bochs BIOS debug ports */
419
9596ebb7 420static void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
80cabfad 421{
a2f659ee
FB
422 static const char shutdown_str[8] = "Shutdown";
423 static int shutdown_index = 0;
3b46e624 424
80cabfad
FB
425 switch(addr) {
426 /* Bochs BIOS messages */
427 case 0x400:
428 case 0x401:
429 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
430 exit(1);
431 case 0x402:
432 case 0x403:
433#ifdef DEBUG_BIOS
434 fprintf(stderr, "%c", val);
435#endif
436 break;
a2f659ee
FB
437 case 0x8900:
438 /* same as Bochs power off */
439 if (val == shutdown_str[shutdown_index]) {
440 shutdown_index++;
441 if (shutdown_index == 8) {
442 shutdown_index = 0;
443 qemu_system_shutdown_request();
444 }
445 } else {
446 shutdown_index = 0;
447 }
448 break;
80cabfad
FB
449
450 /* LGPL'ed VGA BIOS messages */
451 case 0x501:
452 case 0x502:
453 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
454 exit(1);
455 case 0x500:
456 case 0x503:
457#ifdef DEBUG_BIOS
458 fprintf(stderr, "%c", val);
459#endif
460 break;
461 }
462}
463
11c2fd3e
AL
464extern uint64_t node_cpumask[MAX_NODES];
465
bf483392 466static void *bochs_bios_init(void)
80cabfad 467{
3cce6243 468 void *fw_cfg;
b6f6e3d3
AL
469 uint8_t *smbios_table;
470 size_t smbios_len;
11c2fd3e
AL
471 uint64_t *numa_fw_cfg;
472 int i, j;
3cce6243 473
b41a2cd1
FB
474 register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
475 register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
476 register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
477 register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
a2f659ee 478 register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
b41a2cd1
FB
479
480 register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
481 register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
482 register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
483 register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
3cce6243
BS
484
485 fw_cfg = fw_cfg_init(BIOS_CFG_IOPORT, BIOS_CFG_IOPORT + 1, 0, 0);
bf483392 486
3cce6243 487 fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
905fdcb5 488 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
80deece2
BS
489 fw_cfg_add_bytes(fw_cfg, FW_CFG_ACPI_TABLES, (uint8_t *)acpi_tables,
490 acpi_tables_len);
6b35e7bf 491 fw_cfg_add_bytes(fw_cfg, FW_CFG_IRQ0_OVERRIDE, &irq0override, 1);
b6f6e3d3
AL
492
493 smbios_table = smbios_get_table(&smbios_len);
494 if (smbios_table)
495 fw_cfg_add_bytes(fw_cfg, FW_CFG_SMBIOS_ENTRIES,
496 smbios_table, smbios_len);
11c2fd3e
AL
497
498 /* allocate memory for the NUMA channel: one (64bit) word for the number
499 * of nodes, one word for each VCPU->node and one word for each node to
500 * hold the amount of memory.
501 */
502 numa_fw_cfg = qemu_mallocz((1 + smp_cpus + nb_numa_nodes) * 8);
503 numa_fw_cfg[0] = cpu_to_le64(nb_numa_nodes);
504 for (i = 0; i < smp_cpus; i++) {
505 for (j = 0; j < nb_numa_nodes; j++) {
506 if (node_cpumask[j] & (1 << i)) {
507 numa_fw_cfg[i + 1] = cpu_to_le64(j);
508 break;
509 }
510 }
511 }
512 for (i = 0; i < nb_numa_nodes; i++) {
513 numa_fw_cfg[smp_cpus + 1 + i] = cpu_to_le64(node_mem[i]);
514 }
515 fw_cfg_add_bytes(fw_cfg, FW_CFG_NUMA, (uint8_t *)numa_fw_cfg,
516 (1 + smp_cpus + nb_numa_nodes) * 8);
bf483392
AG
517
518 return fw_cfg;
80cabfad
FB
519}
520
642a4f96
TS
521/* Generate an initial boot sector which sets state and jump to
522 a specified vector */
7ffa4767 523static void generate_bootsect(target_phys_addr_t option_rom,
4fc9af53 524 uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
642a4f96 525{
4fc9af53
AL
526 uint8_t rom[512], *p, *reloc;
527 uint8_t sum;
642a4f96
TS
528 int i;
529
4fc9af53
AL
530 memset(rom, 0, sizeof(rom));
531
532 p = rom;
533 /* Make sure we have an option rom signature */
534 *p++ = 0x55;
535 *p++ = 0xaa;
642a4f96 536
4fc9af53
AL
537 /* ROM size in sectors*/
538 *p++ = 1;
642a4f96 539
4fc9af53 540 /* Hook int19 */
642a4f96 541
4fc9af53
AL
542 *p++ = 0x50; /* push ax */
543 *p++ = 0x1e; /* push ds */
544 *p++ = 0x31; *p++ = 0xc0; /* xor ax, ax */
545 *p++ = 0x8e; *p++ = 0xd8; /* mov ax, ds */
642a4f96 546
4fc9af53
AL
547 *p++ = 0xc7; *p++ = 0x06; /* movvw _start,0x64 */
548 *p++ = 0x64; *p++ = 0x00;
549 reloc = p;
550 *p++ = 0x00; *p++ = 0x00;
551
552 *p++ = 0x8c; *p++ = 0x0e; /* mov cs,0x66 */
553 *p++ = 0x66; *p++ = 0x00;
554
555 *p++ = 0x1f; /* pop ds */
556 *p++ = 0x58; /* pop ax */
557 *p++ = 0xcb; /* lret */
558
642a4f96 559 /* Actual code */
4fc9af53
AL
560 *reloc = (p - rom);
561
642a4f96
TS
562 *p++ = 0xfa; /* CLI */
563 *p++ = 0xfc; /* CLD */
564
565 for (i = 0; i < 6; i++) {
566 if (i == 1) /* Skip CS */
567 continue;
568
569 *p++ = 0xb8; /* MOV AX,imm16 */
570 *p++ = segs[i];
571 *p++ = segs[i] >> 8;
572 *p++ = 0x8e; /* MOV <seg>,AX */
573 *p++ = 0xc0 + (i << 3);
574 }
575
576 for (i = 0; i < 8; i++) {
577 *p++ = 0x66; /* 32-bit operand size */
578 *p++ = 0xb8 + i; /* MOV <reg>,imm32 */
579 *p++ = gpr[i];
580 *p++ = gpr[i] >> 8;
581 *p++ = gpr[i] >> 16;
582 *p++ = gpr[i] >> 24;
583 }
584
585 *p++ = 0xea; /* JMP FAR */
586 *p++ = ip; /* IP */
587 *p++ = ip >> 8;
588 *p++ = segs[1]; /* CS */
589 *p++ = segs[1] >> 8;
590
4fc9af53
AL
591 /* sign rom */
592 sum = 0;
593 for (i = 0; i < (sizeof(rom) - 1); i++)
594 sum += rom[i];
595 rom[sizeof(rom) - 1] = -sum;
596
7ffa4767 597 cpu_physical_memory_write_rom(option_rom, rom, sizeof(rom));
d6ecb036 598 option_rom_setup_reset(option_rom, sizeof (rom));
642a4f96 599}
80cabfad 600
642a4f96
TS
601static long get_file_size(FILE *f)
602{
603 long where, size;
604
605 /* XXX: on Unix systems, using fstat() probably makes more sense */
606
607 where = ftell(f);
608 fseek(f, 0, SEEK_END);
609 size = ftell(f);
610 fseek(f, where, SEEK_SET);
611
612 return size;
613}
614
f16408df
AG
615#define MULTIBOOT_STRUCT_ADDR 0x9000
616
617#if MULTIBOOT_STRUCT_ADDR > 0xf0000
618#error multiboot struct needs to fit in 16 bit real mode
619#endif
620
621static int load_multiboot(void *fw_cfg,
622 FILE *f,
623 const char *kernel_filename,
624 const char *initrd_filename,
625 const char *kernel_cmdline,
626 uint8_t *header)
627{
628 int i, t, is_multiboot = 0;
629 uint32_t flags = 0;
630 uint32_t mh_entry_addr;
631 uint32_t mh_load_addr;
632 uint32_t mb_kernel_size;
633 uint32_t mmap_addr = MULTIBOOT_STRUCT_ADDR;
634 uint32_t mb_bootinfo = MULTIBOOT_STRUCT_ADDR + 0x500;
635 uint32_t mb_cmdline = mb_bootinfo + 0x200;
636 uint32_t mb_mod_end;
637
638 /* Ok, let's see if it is a multiboot image.
639 The header is 12x32bit long, so the latest entry may be 8192 - 48. */
640 for (i = 0; i < (8192 - 48); i += 4) {
641 if (ldl_p(header+i) == 0x1BADB002) {
642 uint32_t checksum = ldl_p(header+i+8);
643 flags = ldl_p(header+i+4);
644 checksum += flags;
645 checksum += (uint32_t)0x1BADB002;
646 if (!checksum) {
647 is_multiboot = 1;
648 break;
649 }
650 }
651 }
652
653 if (!is_multiboot)
654 return 0; /* no multiboot */
655
656#ifdef DEBUG_MULTIBOOT
657 fprintf(stderr, "qemu: I believe we found a multiboot image!\n");
658#endif
659
660 if (flags & 0x00000004) { /* MULTIBOOT_HEADER_HAS_VBE */
661 fprintf(stderr, "qemu: multiboot knows VBE. we don't.\n");
662 }
663 if (!(flags & 0x00010000)) { /* MULTIBOOT_HEADER_HAS_ADDR */
664 uint64_t elf_entry;
665 int kernel_size;
666 fclose(f);
667 kernel_size = load_elf(kernel_filename, 0, &elf_entry, NULL, NULL);
668 if (kernel_size < 0) {
669 fprintf(stderr, "Error while loading elf kernel\n");
670 exit(1);
671 }
672 mh_load_addr = mh_entry_addr = elf_entry;
673 mb_kernel_size = kernel_size;
674
675#ifdef DEBUG_MULTIBOOT
676 fprintf(stderr, "qemu: loading multiboot-elf kernel (%#x bytes) with entry %#zx\n",
677 mb_kernel_size, (size_t)mh_entry_addr);
678#endif
679 } else {
680 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_ADDR. */
681 uint32_t mh_header_addr = ldl_p(header+i+12);
682 mh_load_addr = ldl_p(header+i+16);
683#ifdef DEBUG_MULTIBOOT
684 uint32_t mh_load_end_addr = ldl_p(header+i+20);
685 uint32_t mh_bss_end_addr = ldl_p(header+i+24);
686#endif
687 uint32_t mb_kernel_text_offset = i - (mh_header_addr - mh_load_addr);
688
689 mh_entry_addr = ldl_p(header+i+28);
690 mb_kernel_size = get_file_size(f) - mb_kernel_text_offset;
691
692 /* Valid if mh_flags sets MULTIBOOT_HEADER_HAS_VBE.
693 uint32_t mh_mode_type = ldl_p(header+i+32);
694 uint32_t mh_width = ldl_p(header+i+36);
695 uint32_t mh_height = ldl_p(header+i+40);
696 uint32_t mh_depth = ldl_p(header+i+44); */
697
698#ifdef DEBUG_MULTIBOOT
699 fprintf(stderr, "multiboot: mh_header_addr = %#x\n", mh_header_addr);
700 fprintf(stderr, "multiboot: mh_load_addr = %#x\n", mh_load_addr);
701 fprintf(stderr, "multiboot: mh_load_end_addr = %#x\n", mh_load_end_addr);
702 fprintf(stderr, "multiboot: mh_bss_end_addr = %#x\n", mh_bss_end_addr);
703#endif
704
705 fseek(f, mb_kernel_text_offset, SEEK_SET);
706
707#ifdef DEBUG_MULTIBOOT
708 fprintf(stderr, "qemu: loading multiboot kernel (%#x bytes) at %#x\n",
709 mb_kernel_size, mh_load_addr);
710#endif
711
712 if (!fread_targphys_ok(mh_load_addr, mb_kernel_size, f)) {
713 fprintf(stderr, "qemu: read error on multiboot kernel '%s' (%#x)\n",
714 kernel_filename, mb_kernel_size);
715 exit(1);
716 }
717 fclose(f);
718 }
719
720 /* blob size is only the kernel for now */
721 mb_mod_end = mh_load_addr + mb_kernel_size;
722
723 /* load modules */
724 stl_phys(mb_bootinfo + 20, 0x0); /* mods_count */
725 if (initrd_filename) {
726 uint32_t mb_mod_info = mb_bootinfo + 0x100;
727 uint32_t mb_mod_cmdline = mb_bootinfo + 0x300;
728 uint32_t mb_mod_start = mh_load_addr;
729 uint32_t mb_mod_length = mb_kernel_size;
730 char *next_initrd;
731 char *next_space;
732 int mb_mod_count = 0;
733
734 do {
735 next_initrd = strchr(initrd_filename, ',');
736 if (next_initrd)
737 *next_initrd = '\0';
738 /* if a space comes after the module filename, treat everything
739 after that as parameters */
740 cpu_physical_memory_write(mb_mod_cmdline, (uint8_t*)initrd_filename,
741 strlen(initrd_filename) + 1);
742 stl_phys(mb_mod_info + 8, mb_mod_cmdline); /* string */
743 mb_mod_cmdline += strlen(initrd_filename) + 1;
744 if ((next_space = strchr(initrd_filename, ' ')))
745 *next_space = '\0';
746#ifdef DEBUG_MULTIBOOT
747 printf("multiboot loading module: %s\n", initrd_filename);
748#endif
749 f = fopen(initrd_filename, "rb");
750 if (f) {
751 mb_mod_start = (mb_mod_start + mb_mod_length + (TARGET_PAGE_SIZE - 1))
752 & (TARGET_PAGE_MASK);
753 mb_mod_length = get_file_size(f);
754 mb_mod_end = mb_mod_start + mb_mod_length;
755
756 if (!fread_targphys_ok(mb_mod_start, mb_mod_length, f)) {
757 fprintf(stderr, "qemu: read error on multiboot module '%s' (%#x)\n",
758 initrd_filename, mb_mod_length);
759 exit(1);
760 }
761
762 mb_mod_count++;
763 stl_phys(mb_mod_info + 0, mb_mod_start);
764 stl_phys(mb_mod_info + 4, mb_mod_start + mb_mod_length);
765#ifdef DEBUG_MULTIBOOT
766 printf("mod_start: %#x\nmod_end: %#x\n", mb_mod_start,
767 mb_mod_start + mb_mod_length);
768#endif
769 stl_phys(mb_mod_info + 12, 0x0); /* reserved */
770 }
771 initrd_filename = next_initrd+1;
772 mb_mod_info += 16;
773 } while (next_initrd);
774 stl_phys(mb_bootinfo + 20, mb_mod_count); /* mods_count */
775 stl_phys(mb_bootinfo + 24, mb_bootinfo + 0x100); /* mods_addr */
776 }
777
778 /* Make sure we're getting kernel + modules back after reset */
779 option_rom_setup_reset(mh_load_addr, mb_mod_end - mh_load_addr);
780
781 /* Commandline support */
782 stl_phys(mb_bootinfo + 16, mb_cmdline);
783 t = strlen(kernel_filename);
784 cpu_physical_memory_write(mb_cmdline, (uint8_t*)kernel_filename, t);
785 mb_cmdline += t;
786 stb_phys(mb_cmdline++, ' ');
787 t = strlen(kernel_cmdline) + 1;
788 cpu_physical_memory_write(mb_cmdline, (uint8_t*)kernel_cmdline, t);
789
790 /* the kernel is where we want it to be now */
791
792#define MULTIBOOT_FLAGS_MEMORY (1 << 0)
793#define MULTIBOOT_FLAGS_BOOT_DEVICE (1 << 1)
794#define MULTIBOOT_FLAGS_CMDLINE (1 << 2)
795#define MULTIBOOT_FLAGS_MODULES (1 << 3)
796#define MULTIBOOT_FLAGS_MMAP (1 << 6)
797 stl_phys(mb_bootinfo, MULTIBOOT_FLAGS_MEMORY
798 | MULTIBOOT_FLAGS_BOOT_DEVICE
799 | MULTIBOOT_FLAGS_CMDLINE
800 | MULTIBOOT_FLAGS_MODULES
801 | MULTIBOOT_FLAGS_MMAP);
802 stl_phys(mb_bootinfo + 4, 640); /* mem_lower */
803 stl_phys(mb_bootinfo + 8, ram_size / 1024); /* mem_upper */
804 stl_phys(mb_bootinfo + 12, 0x8001ffff); /* XXX: use the -boot switch? */
805 stl_phys(mb_bootinfo + 48, mmap_addr); /* mmap_addr */
806
807#ifdef DEBUG_MULTIBOOT
808 fprintf(stderr, "multiboot: mh_entry_addr = %#x\n", mh_entry_addr);
809#endif
810
811 /* Pass variables to option rom */
812 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, mh_entry_addr);
813 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, mb_bootinfo);
814 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, mmap_addr);
815
816 /* Make sure we're getting the config space back after reset */
817 option_rom_setup_reset(mb_bootinfo, 0x500);
818
819 option_rom[nb_option_roms] = "multiboot.bin";
820 nb_option_roms++;
821
822 return 1; /* yes, we are multiboot */
823}
824
825static void load_linux(void *fw_cfg,
826 target_phys_addr_t option_rom,
4fc9af53 827 const char *kernel_filename,
642a4f96 828 const char *initrd_filename,
e6ade764
GC
829 const char *kernel_cmdline,
830 target_phys_addr_t max_ram_size)
642a4f96
TS
831{
832 uint16_t protocol;
833 uint32_t gpr[8];
834 uint16_t seg[6];
835 uint16_t real_seg;
5cea8590 836 int setup_size, kernel_size, initrd_size = 0, cmdline_size;
642a4f96 837 uint32_t initrd_max;
f16408df 838 uint8_t header[8192];
5cea8590 839 target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
642a4f96 840 FILE *f, *fi;
bf4e5d92 841 char *vmode;
642a4f96
TS
842
843 /* Align to 16 bytes as a paranoia measure */
844 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
845
846 /* load the kernel header */
847 f = fopen(kernel_filename, "rb");
848 if (!f || !(kernel_size = get_file_size(f)) ||
f16408df
AG
849 fread(header, 1, MIN(ARRAY_SIZE(header), kernel_size), f) !=
850 MIN(ARRAY_SIZE(header), kernel_size)) {
642a4f96
TS
851 fprintf(stderr, "qemu: could not load kernel '%s'\n",
852 kernel_filename);
853 exit(1);
854 }
855
856 /* kernel protocol version */
bc4edd79 857#if 0
642a4f96 858 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
bc4edd79 859#endif
642a4f96
TS
860 if (ldl_p(header+0x202) == 0x53726448)
861 protocol = lduw_p(header+0x206);
f16408df
AG
862 else {
863 /* This looks like a multiboot kernel. If it is, let's stop
864 treating it like a Linux kernel. */
865 if (load_multiboot(fw_cfg, f, kernel_filename,
866 initrd_filename, kernel_cmdline, header))
867 return;
642a4f96 868 protocol = 0;
f16408df 869 }
642a4f96
TS
870
871 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
872 /* Low kernel */
a37af289
BS
873 real_addr = 0x90000;
874 cmdline_addr = 0x9a000 - cmdline_size;
875 prot_addr = 0x10000;
642a4f96
TS
876 } else if (protocol < 0x202) {
877 /* High but ancient kernel */
a37af289
BS
878 real_addr = 0x90000;
879 cmdline_addr = 0x9a000 - cmdline_size;
880 prot_addr = 0x100000;
642a4f96
TS
881 } else {
882 /* High and recent kernel */
a37af289
BS
883 real_addr = 0x10000;
884 cmdline_addr = 0x20000;
885 prot_addr = 0x100000;
642a4f96
TS
886 }
887
bc4edd79 888#if 0
642a4f96 889 fprintf(stderr,
526ccb7a
AZ
890 "qemu: real_addr = 0x" TARGET_FMT_plx "\n"
891 "qemu: cmdline_addr = 0x" TARGET_FMT_plx "\n"
892 "qemu: prot_addr = 0x" TARGET_FMT_plx "\n",
a37af289
BS
893 real_addr,
894 cmdline_addr,
895 prot_addr);
bc4edd79 896#endif
642a4f96
TS
897
898 /* highest address for loading the initrd */
899 if (protocol >= 0x203)
900 initrd_max = ldl_p(header+0x22c);
901 else
902 initrd_max = 0x37ffffff;
903
e6ade764
GC
904 if (initrd_max >= max_ram_size-ACPI_DATA_SIZE)
905 initrd_max = max_ram_size-ACPI_DATA_SIZE-1;
642a4f96
TS
906
907 /* kernel command line */
a37af289 908 pstrcpy_targphys(cmdline_addr, 4096, kernel_cmdline);
642a4f96
TS
909
910 if (protocol >= 0x202) {
a37af289 911 stl_p(header+0x228, cmdline_addr);
642a4f96
TS
912 } else {
913 stw_p(header+0x20, 0xA33F);
914 stw_p(header+0x22, cmdline_addr-real_addr);
915 }
916
bf4e5d92
PT
917 /* handle vga= parameter */
918 vmode = strstr(kernel_cmdline, "vga=");
919 if (vmode) {
920 unsigned int video_mode;
921 /* skip "vga=" */
922 vmode += 4;
923 if (!strncmp(vmode, "normal", 6)) {
924 video_mode = 0xffff;
925 } else if (!strncmp(vmode, "ext", 3)) {
926 video_mode = 0xfffe;
927 } else if (!strncmp(vmode, "ask", 3)) {
928 video_mode = 0xfffd;
929 } else {
930 video_mode = strtol(vmode, NULL, 0);
931 }
932 stw_p(header+0x1fa, video_mode);
933 }
934
642a4f96
TS
935 /* loader type */
936 /* High nybble = B reserved for Qemu; low nybble is revision number.
937 If this code is substantially changed, you may want to consider
938 incrementing the revision. */
939 if (protocol >= 0x200)
940 header[0x210] = 0xB0;
941
942 /* heap */
943 if (protocol >= 0x201) {
944 header[0x211] |= 0x80; /* CAN_USE_HEAP */
945 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
946 }
947
948 /* load initrd */
949 if (initrd_filename) {
950 if (protocol < 0x200) {
951 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
952 exit(1);
953 }
954
955 fi = fopen(initrd_filename, "rb");
956 if (!fi) {
957 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
958 initrd_filename);
959 exit(1);
960 }
961
962 initrd_size = get_file_size(fi);
a37af289 963 initrd_addr = (initrd_max-initrd_size) & ~4095;
642a4f96 964
a37af289 965 if (!fread_targphys_ok(initrd_addr, initrd_size, fi)) {
642a4f96
TS
966 fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
967 initrd_filename);
968 exit(1);
969 }
970 fclose(fi);
971
a37af289 972 stl_p(header+0x218, initrd_addr);
642a4f96
TS
973 stl_p(header+0x21c, initrd_size);
974 }
975
976 /* store the finalized header and load the rest of the kernel */
f16408df 977 cpu_physical_memory_write(real_addr, header, ARRAY_SIZE(header));
642a4f96
TS
978
979 setup_size = header[0x1f1];
980 if (setup_size == 0)
981 setup_size = 4;
982
983 setup_size = (setup_size+1)*512;
f16408df
AG
984 /* Size of protected-mode code */
985 kernel_size -= (setup_size > ARRAY_SIZE(header)) ? setup_size : ARRAY_SIZE(header);
986
987 /* In case we have read too much already, copy that over */
988 if (setup_size < ARRAY_SIZE(header)) {
989 cpu_physical_memory_write(prot_addr, header + setup_size, ARRAY_SIZE(header) - setup_size);
990 prot_addr += (ARRAY_SIZE(header) - setup_size);
991 setup_size = ARRAY_SIZE(header);
992 }
642a4f96 993
f16408df
AG
994 if (!fread_targphys_ok(real_addr + ARRAY_SIZE(header),
995 setup_size - ARRAY_SIZE(header), f) ||
a37af289 996 !fread_targphys_ok(prot_addr, kernel_size, f)) {
642a4f96
TS
997 fprintf(stderr, "qemu: read error on kernel '%s'\n",
998 kernel_filename);
999 exit(1);
1000 }
1001 fclose(f);
1002
1003 /* generate bootsector to set up the initial register state */
a37af289 1004 real_seg = real_addr >> 4;
642a4f96
TS
1005 seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg;
1006 seg[1] = real_seg+0x20; /* CS */
1007 memset(gpr, 0, sizeof gpr);
1008 gpr[4] = cmdline_addr-real_addr-16; /* SP (-16 is paranoia) */
1009
d6ecb036
GC
1010 option_rom_setup_reset(real_addr, setup_size);
1011 option_rom_setup_reset(prot_addr, kernel_size);
1012 option_rom_setup_reset(cmdline_addr, cmdline_size);
1013 if (initrd_filename)
1014 option_rom_setup_reset(initrd_addr, initrd_size);
1015
4fc9af53 1016 generate_bootsect(option_rom, gpr, seg, 0);
642a4f96
TS
1017}
1018
b41a2cd1
FB
1019static const int ide_iobase[2] = { 0x1f0, 0x170 };
1020static const int ide_iobase2[2] = { 0x3f6, 0x376 };
1021static const int ide_irq[2] = { 14, 15 };
1022
1023#define NE2000_NB_MAX 6
1024
8d11df9e 1025static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
b41a2cd1
FB
1026static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
1027
8d11df9e
FB
1028static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
1029static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
1030
6508fe59
FB
1031static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
1032static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
1033
6a36d84e 1034#ifdef HAS_AUDIO
d537cf6c 1035static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
6a36d84e
FB
1036{
1037 struct soundhw *c;
6a36d84e 1038
3a8bae3e 1039 for (c = soundhw; c->name; ++c) {
1040 if (c->enabled) {
1041 if (c->isa) {
1042 c->init.init_isa(pic);
1043 } else {
1044 if (pci_bus) {
1045 c->init.init_pci(pci_bus);
6a36d84e
FB
1046 }
1047 }
1048 }
1049 }
1050}
1051#endif
1052
d537cf6c 1053static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
a41b2ff2
PB
1054{
1055 static int nb_ne2k = 0;
1056
1057 if (nb_ne2k == NE2000_NB_MAX)
1058 return;
d537cf6c 1059 isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd);
a41b2ff2
PB
1060 nb_ne2k++;
1061}
1062
f753ff16
PB
1063static int load_option_rom(const char *oprom, target_phys_addr_t start,
1064 target_phys_addr_t end)
1065{
1066 int size;
5cea8590
PB
1067 char *filename;
1068
1069 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, oprom);
1070 if (filename) {
1071 size = get_image_size(filename);
1072 if (size > 0 && start + size > end) {
1073 fprintf(stderr, "Not enough space to load option rom '%s'\n",
1074 oprom);
1075 exit(1);
1076 }
1077 size = load_image_targphys(filename, start, end - start);
1078 qemu_free(filename);
1079 } else {
1080 size = -1;
f753ff16 1081 }
f753ff16
PB
1082 if (size < 0) {
1083 fprintf(stderr, "Could not load option rom '%s'\n", oprom);
1084 exit(1);
1085 }
1086 /* Round up optiom rom size to the next 2k boundary */
1087 size = (size + 2047) & ~2047;
e28f9884 1088 option_rom_setup_reset(start, size);
f753ff16
PB
1089 return size;
1090}
1091
678e12cc
GN
1092int cpu_is_bsp(CPUState *env)
1093{
1094 return env->cpuid_apic_id == 0;
1095}
1096
3a31f36a
JK
1097static CPUState *pc_new_cpu(const char *cpu_model)
1098{
1099 CPUState *env;
1100
1101 env = cpu_init(cpu_model);
1102 if (!env) {
1103 fprintf(stderr, "Unable to find x86 CPU definition\n");
1104 exit(1);
1105 }
1106 if ((env->cpuid_features & CPUID_APIC) || smp_cpus > 1) {
1107 env->cpuid_apic_id = env->cpu_index;
1108 /* APIC reset callback resets cpu */
1109 apic_init(env);
1110 } else {
1111 qemu_register_reset((QEMUResetHandler*)cpu_reset, env);
1112 }
1113 return env;
1114}
1115
80cabfad 1116/* PC hardware initialisation */
fbe1b595 1117static void pc_init1(ram_addr_t ram_size,
3023f332 1118 const char *boot_device,
e8b2a1c6
MM
1119 const char *kernel_filename,
1120 const char *kernel_cmdline,
3dbbdc25 1121 const char *initrd_filename,
e8b2a1c6 1122 const char *cpu_model,
caea79a9 1123 int pci_enabled)
80cabfad 1124{
5cea8590 1125 char *filename;
642a4f96 1126 int ret, linux_boot, i;
b584726d 1127 ram_addr_t ram_addr, bios_offset, option_rom_offset;
00f82b8a 1128 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
f753ff16 1129 int bios_size, isa_bios_size, oprom_area_size;
46e50e9d 1130 PCIBus *pci_bus;
c2cc47a4 1131 PCIDevice *pci_dev;
b3999638 1132 ISADevice *isa_dev;
5c3ff3a7 1133 int piix3_devfn = -1;
59b8ad81 1134 CPUState *env;
d537cf6c 1135 qemu_irq *cpu_irq;
1452411b 1136 qemu_irq *isa_irq;
d537cf6c 1137 qemu_irq *i8259;
1452411b 1138 IsaIrqState *isa_irq_state;
751c6a17 1139 DriveInfo *dinfo;
e4bcb14c
TS
1140 BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
1141 BlockDriverState *fd[MAX_FD];
34b39c2b 1142 int using_vga = cirrus_vga_enabled || std_vga_enabled || vmsvga_enabled;
bf483392 1143 void *fw_cfg;
d592d303 1144
00f82b8a
AJ
1145 if (ram_size >= 0xe0000000 ) {
1146 above_4g_mem_size = ram_size - 0xe0000000;
1147 below_4g_mem_size = 0xe0000000;
1148 } else {
1149 below_4g_mem_size = ram_size;
1150 }
1151
80cabfad
FB
1152 linux_boot = (kernel_filename != NULL);
1153
59b8ad81 1154 /* init CPUs */
a049de61
FB
1155 if (cpu_model == NULL) {
1156#ifdef TARGET_X86_64
1157 cpu_model = "qemu64";
1158#else
1159 cpu_model = "qemu32";
1160#endif
1161 }
3a31f36a
JK
1162
1163 for (i = 0; i < smp_cpus; i++) {
1164 env = pc_new_cpu(cpu_model);
59b8ad81
FB
1165 }
1166
26fb5e48
AJ
1167 vmport_init();
1168
80cabfad 1169 /* allocate RAM */
82b36dc3
AL
1170 ram_addr = qemu_ram_alloc(0xa0000);
1171 cpu_register_physical_memory(0, 0xa0000, ram_addr);
1172
1173 /* Allocate, even though we won't register, so we don't break the
1174 * phys_ram_base + PA assumption. This range includes vga (0xa0000 - 0xc0000),
1175 * and some bios areas, which will be registered later
1176 */
1177 ram_addr = qemu_ram_alloc(0x100000 - 0xa0000);
1178 ram_addr = qemu_ram_alloc(below_4g_mem_size - 0x100000);
1179 cpu_register_physical_memory(0x100000,
1180 below_4g_mem_size - 0x100000,
1181 ram_addr);
00f82b8a
AJ
1182
1183 /* above 4giga memory allocation */
1184 if (above_4g_mem_size > 0) {
8a637d44
PB
1185#if TARGET_PHYS_ADDR_BITS == 32
1186 hw_error("To much RAM for 32-bit physical address");
1187#else
82b36dc3
AL
1188 ram_addr = qemu_ram_alloc(above_4g_mem_size);
1189 cpu_register_physical_memory(0x100000000ULL,
526ccb7a 1190 above_4g_mem_size,
82b36dc3 1191 ram_addr);
8a637d44 1192#endif
00f82b8a 1193 }
80cabfad 1194
82b36dc3 1195
970ac5a3 1196 /* BIOS load */
1192dad8
JM
1197 if (bios_name == NULL)
1198 bios_name = BIOS_FILENAME;
5cea8590
PB
1199 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
1200 if (filename) {
1201 bios_size = get_image_size(filename);
1202 } else {
1203 bios_size = -1;
1204 }
5fafdf24 1205 if (bios_size <= 0 ||
970ac5a3 1206 (bios_size % 65536) != 0) {
7587cf44
FB
1207 goto bios_error;
1208 }
970ac5a3 1209 bios_offset = qemu_ram_alloc(bios_size);
5cea8590 1210 ret = load_image(filename, qemu_get_ram_ptr(bios_offset));
7587cf44
FB
1211 if (ret != bios_size) {
1212 bios_error:
5cea8590 1213 fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
80cabfad
FB
1214 exit(1);
1215 }
5cea8590
PB
1216 if (filename) {
1217 qemu_free(filename);
1218 }
7587cf44
FB
1219 /* map the last 128KB of the BIOS in ISA space */
1220 isa_bios_size = bios_size;
1221 if (isa_bios_size > (128 * 1024))
1222 isa_bios_size = 128 * 1024;
5fafdf24
TS
1223 cpu_register_physical_memory(0x100000 - isa_bios_size,
1224 isa_bios_size,
7587cf44 1225 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
9ae02555 1226
4fc9af53 1227
f753ff16
PB
1228
1229 option_rom_offset = qemu_ram_alloc(0x20000);
1230 oprom_area_size = 0;
49669fc5 1231 cpu_register_physical_memory(0xc0000, 0x20000, option_rom_offset);
f753ff16
PB
1232
1233 if (using_vga) {
5cea8590 1234 const char *vgabios_filename;
f753ff16
PB
1235 /* VGA BIOS load */
1236 if (cirrus_vga_enabled) {
5cea8590 1237 vgabios_filename = VGABIOS_CIRRUS_FILENAME;
f753ff16 1238 } else {
5cea8590 1239 vgabios_filename = VGABIOS_FILENAME;
970ac5a3 1240 }
5cea8590 1241 oprom_area_size = load_option_rom(vgabios_filename, 0xc0000, 0xe0000);
f753ff16
PB
1242 }
1243 /* Although video roms can grow larger than 0x8000, the area between
1244 * 0xc0000 - 0xc8000 is reserved for them. It means we won't be looking
1245 * for any other kind of option rom inside this area */
1246 if (oprom_area_size < 0x8000)
1247 oprom_area_size = 0x8000;
1248
1d108d97
AG
1249 /* map all the bios at the top of memory */
1250 cpu_register_physical_memory((uint32_t)(-bios_size),
1251 bios_size, bios_offset | IO_MEM_ROM);
1252
bf483392 1253 fw_cfg = bochs_bios_init();
1d108d97 1254
f753ff16 1255 if (linux_boot) {
f16408df 1256 load_linux(fw_cfg, 0xc0000 + oprom_area_size,
e6ade764 1257 kernel_filename, initrd_filename, kernel_cmdline, below_4g_mem_size);
f753ff16
PB
1258 oprom_area_size += 2048;
1259 }
1260
1261 for (i = 0; i < nb_option_roms; i++) {
406c8df3
GC
1262 oprom_area_size += load_option_rom(option_rom[i], 0xc0000 + oprom_area_size,
1263 0xe0000);
1264 }
1265
1266 for (i = 0; i < nb_nics; i++) {
1267 char nic_oprom[1024];
1268 const char *model = nd_table[i].model;
1269
1270 if (!nd_table[i].bootable)
1271 continue;
1272
1273 if (model == NULL)
1274 model = "ne2k_pci";
1275 snprintf(nic_oprom, sizeof(nic_oprom), "pxe-%s.bin", model);
1276
1277 oprom_area_size += load_option_rom(nic_oprom, 0xc0000 + oprom_area_size,
1278 0xe0000);
9ae02555
TS
1279 }
1280
a5b38b51 1281 cpu_irq = qemu_allocate_irqs(pic_irq_request, NULL, 1);
d537cf6c 1282 i8259 = i8259_init(cpu_irq[0]);
1452411b
AK
1283 isa_irq_state = qemu_mallocz(sizeof(*isa_irq_state));
1284 isa_irq_state->i8259 = i8259;
1285 isa_irq = qemu_allocate_irqs(isa_irq_handler, isa_irq_state, 16);
1286 ferr_irq = isa_irq[13];
d537cf6c 1287
69b91039 1288 if (pci_enabled) {
1452411b 1289 pci_bus = i440fx_init(&i440fx_state, isa_irq);
8f1c91d8 1290 piix3_devfn = piix3_init(pci_bus, -1);
46e50e9d
FB
1291 } else {
1292 pci_bus = NULL;
69b91039
FB
1293 }
1294
80cabfad 1295 /* init basic PC hardware */
b41a2cd1 1296 register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
80cabfad 1297
f929aad6
FB
1298 register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
1299
1f04275e
FB
1300 if (cirrus_vga_enabled) {
1301 if (pci_enabled) {
fbe1b595 1302 pci_cirrus_vga_init(pci_bus);
1f04275e 1303 } else {
fbe1b595 1304 isa_cirrus_vga_init();
1f04275e 1305 }
d34cab9f
TS
1306 } else if (vmsvga_enabled) {
1307 if (pci_enabled)
fbe1b595 1308 pci_vmsvga_init(pci_bus);
d34cab9f
TS
1309 else
1310 fprintf(stderr, "%s: vmware_vga: no PCI bus\n", __FUNCTION__);
c2b3b41a 1311 } else if (std_vga_enabled) {
89b6b508 1312 if (pci_enabled) {
fbe1b595 1313 pci_vga_init(pci_bus, 0, 0);
89b6b508 1314 } else {
fbe1b595 1315 isa_vga_init();
89b6b508 1316 }
1f04275e 1317 }
80cabfad 1318
1452411b 1319 rtc_state = rtc_init(0x70, isa_irq[8], 2000);
80cabfad 1320
3b4366de
BS
1321 qemu_register_boot_set(pc_boot_set, rtc_state);
1322
e1a23744
FB
1323 register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
1324 register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
1325
d592d303 1326 if (pci_enabled) {
d592d303
FB
1327 ioapic = ioapic_init();
1328 }
1452411b 1329 pit = pit_init(0x40, isa_irq[0]);
fd06c375 1330 pcspk_init(pit);
16b29ae1 1331 if (!no_hpet) {
1452411b 1332 hpet_init(isa_irq);
16b29ae1 1333 }
d592d303
FB
1334 if (pci_enabled) {
1335 pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
1336 }
b41a2cd1 1337
8d11df9e
FB
1338 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
1339 if (serial_hds[i]) {
1452411b 1340 serial_init(serial_io[i], isa_irq[serial_irq[i]], 115200,
b6cd0ea1 1341 serial_hds[i]);
8d11df9e
FB
1342 }
1343 }
b41a2cd1 1344
6508fe59
FB
1345 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
1346 if (parallel_hds[i]) {
1452411b 1347 parallel_init(parallel_io[i], isa_irq[parallel_irq[i]],
d537cf6c 1348 parallel_hds[i]);
6508fe59
FB
1349 }
1350 }
1351
9dd986cc
RJ
1352 watchdog_pc_init(pci_bus);
1353
a41b2ff2 1354 for(i = 0; i < nb_nics; i++) {
cb457d76
AL
1355 NICInfo *nd = &nd_table[i];
1356
1357 if (!pci_enabled || (nd->model && strcmp(nd->model, "ne2k_isa") == 0))
1452411b 1358 pc_init_ne2k_isa(nd, isa_irq);
cb457d76 1359 else
5607c388 1360 pci_nic_init(nd, "ne2k_pci", NULL);
a41b2ff2 1361 }
b41a2cd1 1362
9d5e77a2 1363 piix4_acpi_system_hot_add_init();
5e3cb534 1364
e4bcb14c
TS
1365 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
1366 fprintf(stderr, "qemu: too many IDE bus\n");
1367 exit(1);
1368 }
1369
1370 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
751c6a17
GH
1371 dinfo = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
1372 hd[i] = dinfo ? dinfo->bdrv : NULL;
e4bcb14c
TS
1373 }
1374
a41b2ff2 1375 if (pci_enabled) {
1452411b 1376 pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, isa_irq);
a41b2ff2 1377 } else {
e4bcb14c 1378 for(i = 0; i < MAX_IDE_BUS; i++) {
1452411b 1379 isa_ide_init(ide_iobase[i], ide_iobase2[i], isa_irq[ide_irq[i]],
e4bcb14c 1380 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
69b91039 1381 }
b41a2cd1 1382 }
69b91039 1383
b3999638 1384 isa_dev = isa_create_simple("i8042", 0x60, 0x64);
1452411b
AK
1385 isa_connect_irq(isa_dev, 0, isa_irq[1]);
1386 isa_connect_irq(isa_dev, 1, isa_irq[12]);
7c29d0c0 1387 DMA_init(0);
6a36d84e 1388#ifdef HAS_AUDIO
1452411b 1389 audio_init(pci_enabled ? pci_bus : NULL, isa_irq);
fb065187 1390#endif
80cabfad 1391
e4bcb14c 1392 for(i = 0; i < MAX_FD; i++) {
751c6a17
GH
1393 dinfo = drive_get(IF_FLOPPY, 0, i);
1394 fd[i] = dinfo ? dinfo->bdrv : NULL;
e4bcb14c 1395 }
1452411b 1396 floppy_controller = fdctrl_init(isa_irq[6], 2, 0, 0x3f0, fd);
b41a2cd1 1397
00f82b8a 1398 cmos_init(below_4g_mem_size, above_4g_mem_size, boot_device, hd);
69b91039 1399
bb36d470 1400 if (pci_enabled && usb_enabled) {
afcc3cdf 1401 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
bb36d470
FB
1402 }
1403
6515b203 1404 if (pci_enabled && acpi_enabled) {
3fffc223 1405 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
0ff596d0
PB
1406 i2c_bus *smbus;
1407
1408 /* TODO: Populate SPD eeprom data. */
1452411b 1409 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, isa_irq[9]);
3fffc223 1410 for (i = 0; i < 8; i++) {
1ea96673 1411 DeviceState *eeprom;
02e2da45 1412 eeprom = qdev_create((BusState *)smbus, "smbus-eeprom");
ee6847d1
GH
1413 qdev_prop_set_uint32(eeprom, "address", 0x50 + i);
1414 qdev_prop_set_ptr(eeprom, "data", eeprom_buf + (i * 256));
1ea96673 1415 qdev_init(eeprom);
3fffc223 1416 }
6515b203 1417 }
3b46e624 1418
a5954d5c
FB
1419 if (i440fx_state) {
1420 i440fx_init_memory_mappings(i440fx_state);
1421 }
e4bcb14c 1422
7d8406be 1423 if (pci_enabled) {
e4bcb14c 1424 int max_bus;
9be5dafe 1425 int bus;
96d30e48 1426
e4bcb14c 1427 max_bus = drive_get_max_bus(IF_SCSI);
e4bcb14c 1428 for (bus = 0; bus <= max_bus; bus++) {
9be5dafe 1429 pci_create_simple(pci_bus, -1, "lsi53c895a");
e4bcb14c 1430 }
7d8406be 1431 }
6e02c38d 1432
bd322087 1433 /* Add virtio balloon device */
7d4c3d53
MA
1434 if (pci_enabled && virtio_balloon) {
1435 pci_dev = pci_create("virtio-balloon-pci", virtio_balloon_devaddr);
1436 qdev_init(&pci_dev->qdev);
2d72c572 1437 }
a2fa19f9
AL
1438
1439 /* Add virtio console devices */
1440 if (pci_enabled) {
1441 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
0e058a8a 1442 if (virtcon_hds[i]) {
caea79a9 1443 pci_create_simple(pci_bus, -1, "virtio-console-pci");
0e058a8a 1444 }
a2fa19f9
AL
1445 }
1446 }
80cabfad 1447}
b5ff2d6e 1448
fbe1b595 1449static void pc_init_pci(ram_addr_t ram_size,
3023f332 1450 const char *boot_device,
5fafdf24 1451 const char *kernel_filename,
3dbbdc25 1452 const char *kernel_cmdline,
94fc95cd
JM
1453 const char *initrd_filename,
1454 const char *cpu_model)
3dbbdc25 1455{
fbe1b595 1456 pc_init1(ram_size, boot_device,
3dbbdc25 1457 kernel_filename, kernel_cmdline,
caea79a9 1458 initrd_filename, cpu_model, 1);
3dbbdc25
FB
1459}
1460
fbe1b595 1461static void pc_init_isa(ram_addr_t ram_size,
3023f332 1462 const char *boot_device,
5fafdf24 1463 const char *kernel_filename,
3dbbdc25 1464 const char *kernel_cmdline,
94fc95cd
JM
1465 const char *initrd_filename,
1466 const char *cpu_model)
3dbbdc25 1467{
fbe1b595 1468 pc_init1(ram_size, boot_device,
3dbbdc25 1469 kernel_filename, kernel_cmdline,
caea79a9 1470 initrd_filename, cpu_model, 0);
3dbbdc25
FB
1471}
1472
0bacd130
AL
1473/* set CMOS shutdown status register (index 0xF) as S3_resume(0xFE)
1474 BIOS will read it and start S3 resume at POST Entry */
1475void cmos_set_s3_resume(void)
1476{
1477 if (rtc_state)
1478 rtc_set_memory(rtc_state, 0xF, 0xFE);
1479}
1480
f80f9ec9 1481static QEMUMachine pc_machine = {
95747581
MM
1482 .name = "pc-0.11",
1483 .alias = "pc",
a245f2e7
AJ
1484 .desc = "Standard PC",
1485 .init = pc_init_pci,
b2097003 1486 .max_cpus = 255,
0c257437 1487 .is_default = 1,
3dbbdc25
FB
1488};
1489
96cc1810
GH
1490static QEMUMachine pc_machine_v0_10 = {
1491 .name = "pc-0.10",
1492 .desc = "Standard PC, qemu 0.10",
1493 .init = pc_init_pci,
1494 .max_cpus = 255,
1495 .compat_props = (CompatProperty[]) {
ab73ff29
GH
1496 {
1497 .driver = "virtio-blk-pci",
1498 .property = "class",
1499 .value = stringify(PCI_CLASS_STORAGE_OTHER),
d6beee99
GH
1500 },{
1501 .driver = "virtio-console-pci",
1502 .property = "class",
1503 .value = stringify(PCI_CLASS_DISPLAY_OTHER),
a1e0fea5
GH
1504 },{
1505 .driver = "virtio-net-pci",
1506 .property = "vectors",
1507 .value = stringify(0),
ab73ff29 1508 },
96cc1810
GH
1509 { /* end of list */ }
1510 },
1511};
1512
f80f9ec9 1513static QEMUMachine isapc_machine = {
a245f2e7
AJ
1514 .name = "isapc",
1515 .desc = "ISA-only PC",
1516 .init = pc_init_isa,
b2097003 1517 .max_cpus = 1,
b5ff2d6e 1518};
f80f9ec9
AL
1519
1520static void pc_machine_init(void)
1521{
1522 qemu_register_machine(&pc_machine);
96cc1810 1523 qemu_register_machine(&pc_machine_v0_10);
f80f9ec9
AL
1524 qemu_register_machine(&isapc_machine);
1525}
1526
1527machine_init(pc_machine_init);