debuggers.hg

view tools/ioemu/hw/pc.c @ 16997:17cce0554151

Direct Linux boot: Support booting relocatable Linux kernels.

This patch introduces the basic infrastructure for direct kernel
boot in the ioemu copy of QEMU. The current #ifdef disabled
code is actually obsolete wrt to upstream QEMU code. So this
is removed entirely. In its place I have imported the latest
upstream QEMU code. The QEMU code assumes that the guest RAM
is directly mapped into the QEMU process, so there were some
changes neccessary. Instead of strcpy/memcpy'ing the args
and kernel header into guest RAM, cpu_physical_memory_rw is
used. Intead of fread() the initrd and kernel into guest RAM
a helper function is used fread2guest which reads into a small
buffer and then uses cpu_physical_memory_rw.

NB in reading the following, Documentation/i386/boot.txt is
a useful reference for what's going on.

Next, instead of loading the kernel at 0x100000, this code
loads it at 0x200000. This is far enough away that there's
no risk of it overlapping with the HVM firmware image. If the
Linux kernel boot protocol is 0x205 or later, and the flag
at offset 0x234 in the kernel header is 1, then the guest
kernel was built with CONFIG_RELOCATABLE=y.

In this scenario we merely need to tell the kernel what address
it has been relocated to by writing 0x200000 into the kernel
header at offset 0x214. When switching from real mode into
protected mode the kernel will immediately start executing at
0x200000 and be happy with life. This should work for 2.6.20 or
later on i386, and 2.6.22 or later on x86_64.

This has been verified with Fedora 7 and Fedora 8 bare metal kernels
on i386 and x86_64 from the $TREE/images/pxeboot of the install trees.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Feb 01 11:14:53 2008 +0000 (2008-02-01)
parents acfa9290746f
children 67ca9c37ef02
line source
1 /*
2 * QEMU PC System Emulator
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
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 */
24 #include "vl.h"
26 /* output Bochs bios info messages */
27 //#define DEBUG_BIOS
29 #define BIOS_FILENAME "bios.bin"
30 #define VGABIOS_FILENAME "vgabios.bin"
31 #define VGABIOS_CIRRUS_FILENAME "vgabios-cirrus.bin"
32 #define LINUX_BOOT_FILENAME "linux_boot.bin"
34 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
35 #define ACPI_DATA_SIZE 0x10000
37 static fdctrl_t *floppy_controller;
38 static RTCState *rtc_state;
39 #ifndef CONFIG_DM
40 static PITState *pit;
41 #endif /* !CONFIG_DM */
42 #ifndef CONFIG_DM
43 static IOAPICState *ioapic;
44 #endif /* !CONFIG_DM */
45 static PCIDevice *i440fx_state;
47 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
48 {
49 }
51 /* MSDOS compatibility mode FPU exception support */
52 /* XXX: add IGNNE support */
53 void cpu_set_ferr(CPUX86State *s)
54 {
55 pic_set_irq(13, 1);
56 }
58 static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
59 {
60 pic_set_irq(13, 0);
61 }
63 /* TSC handling */
64 uint64_t cpu_get_tsc(CPUX86State *env)
65 {
66 /* Note: when using kqemu, it is more logical to return the host TSC
67 because kqemu does not trap the RDTSC instruction for
68 performance reasons */
69 #if USE_KQEMU
70 if (env->kqemu_enabled) {
71 return cpu_get_real_ticks();
72 } else
73 #endif
74 {
75 return cpu_get_ticks();
76 }
77 }
79 #ifndef CONFIG_DM
80 /* SMM support */
81 void cpu_smm_update(CPUState *env)
82 {
83 if (i440fx_state && env == first_cpu)
84 i440fx_set_smm(i440fx_state, (env->hflags >> HF_SMM_SHIFT) & 1);
85 }
88 /* IRQ handling */
89 int cpu_get_pic_interrupt(CPUState *env)
90 {
91 int intno;
93 intno = apic_get_interrupt(env);
94 if (intno >= 0) {
95 /* set irq request if a PIC irq is still pending */
96 /* XXX: improve that */
97 pic_update_irq(isa_pic);
98 return intno;
99 }
100 /* read the irq from the PIC */
101 intno = pic_read_irq(isa_pic);
102 return intno;
103 }
104 #endif /* CONFIG_DM */
106 static void pic_irq_request(void *opaque, int level)
107 {
108 CPUState *env = opaque;
109 if (level)
110 cpu_interrupt(env, CPU_INTERRUPT_HARD);
111 else
112 cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
113 }
115 /* PC cmos mappings */
117 #define REG_EQUIPMENT_BYTE 0x14
119 static int cmos_get_fd_drive_type(int fd0)
120 {
121 int val;
123 switch (fd0) {
124 case 0:
125 /* 1.44 Mb 3"5 drive */
126 val = 4;
127 break;
128 case 1:
129 /* 2.88 Mb 3"5 drive */
130 val = 5;
131 break;
132 case 2:
133 /* 1.2 Mb 5"5 drive */
134 val = 2;
135 break;
136 default:
137 val = 0;
138 break;
139 }
140 return val;
141 }
143 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
144 {
145 RTCState *s = rtc_state;
146 int cylinders, heads, sectors;
147 bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
148 rtc_set_memory(s, type_ofs, 47);
149 rtc_set_memory(s, info_ofs, cylinders);
150 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
151 rtc_set_memory(s, info_ofs + 2, heads);
152 rtc_set_memory(s, info_ofs + 3, 0xff);
153 rtc_set_memory(s, info_ofs + 4, 0xff);
154 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
155 rtc_set_memory(s, info_ofs + 6, cylinders);
156 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
157 rtc_set_memory(s, info_ofs + 8, sectors);
158 }
160 static int get_bios_disk(char *boot_device, int index) {
162 if (index < strlen(boot_device)) {
163 switch (boot_device[index]) {
164 case 'a':
165 return 0x01; /* floppy */
166 case 'c':
167 return 0x02; /* hard drive */
168 case 'd':
169 return 0x03; /* cdrom */
170 case 'n':
171 return 0x04; /* network */
172 }
173 }
174 return 0x00; /* no device */
175 }
177 /* hd_table must contain 4 block drivers */
178 static void cmos_init(uint64_t ram_size, char *boot_device, BlockDriverState **hd_table)
179 {
180 RTCState *s = rtc_state;
181 int val;
182 int fd0, fd1, nb;
183 int i;
185 /* various important CMOS locations needed by PC/Bochs bios */
187 /* memory size */
188 val = 640; /* base memory in K */
189 rtc_set_memory(s, 0x15, val);
190 rtc_set_memory(s, 0x16, val >> 8);
192 val = (ram_size / 1024) - 1024;
193 if (val > 65535)
194 val = 65535;
195 rtc_set_memory(s, 0x17, val);
196 rtc_set_memory(s, 0x18, val >> 8);
197 rtc_set_memory(s, 0x30, val);
198 rtc_set_memory(s, 0x31, val >> 8);
200 if (ram_size > (16 * 1024 * 1024))
201 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
202 else
203 val = 0;
204 if (val > 65535)
205 val = 65535;
206 rtc_set_memory(s, 0x34, val);
207 rtc_set_memory(s, 0x35, val >> 8);
209 if (boot_device == NULL) {
210 /* default to hd, then cd, then floppy. */
211 boot_device = "cda";
212 }
213 rtc_set_memory(s, 0x3d, get_bios_disk(boot_device, 0) |
214 (get_bios_disk(boot_device, 1) << 4));
215 rtc_set_memory(s, 0x38, (get_bios_disk(boot_device, 2) << 4) |
216 (!fd_bootchk ? 0x01 : 0x00));
218 /* floppy type */
220 fd0 = fdctrl_get_drive_type(floppy_controller, 0);
221 fd1 = fdctrl_get_drive_type(floppy_controller, 1);
223 val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
224 rtc_set_memory(s, 0x10, val);
226 val = 0;
227 nb = 0;
228 if (fd0 < 3)
229 nb++;
230 if (fd1 < 3)
231 nb++;
232 switch (nb) {
233 case 0:
234 break;
235 case 1:
236 val |= 0x01; /* 1 drive, ready for boot */
237 break;
238 case 2:
239 val |= 0x41; /* 2 drives, ready for boot */
240 break;
241 }
242 val |= 0x02; /* FPU is there */
243 val |= 0x04; /* PS/2 mouse installed */
244 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
246 /* hard drives */
248 rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
249 if (hd_table[0])
250 cmos_init_hd(0x19, 0x1b, hd_table[0]);
251 if (hd_table[1])
252 cmos_init_hd(0x1a, 0x24, hd_table[1]);
254 val = 0;
255 for (i = 0; i < 4; i++) {
256 if (hd_table[i]) {
257 int cylinders, heads, sectors, translation;
258 /* NOTE: bdrv_get_geometry_hint() returns the physical
259 geometry. It is always such that: 1 <= sects <= 63, 1
260 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
261 geometry can be different if a translation is done. */
262 translation = bdrv_get_translation_hint(hd_table[i]);
263 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
264 bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
265 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
266 /* No translation. */
267 translation = 0;
268 } else {
269 /* LBA translation. */
270 translation = 1;
271 }
272 } else {
273 translation--;
274 }
275 val |= translation << (i * 2);
276 }
277 }
278 rtc_set_memory(s, 0x39, val);
279 }
281 void ioport_set_a20(int enable)
282 {
283 /* XXX: send to all CPUs ? */
284 cpu_x86_set_a20(first_cpu, enable);
285 }
287 int ioport_get_a20(void)
288 {
289 return ((first_cpu->a20_mask >> 20) & 1);
290 }
292 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
293 {
294 ioport_set_a20((val >> 1) & 1);
295 /* XXX: bit 0 is fast reset */
296 }
298 static uint32_t ioport92_read(void *opaque, uint32_t addr)
299 {
300 return ioport_get_a20() << 1;
301 }
303 /***********************************************************/
304 /* Bochs BIOS debug ports */
306 void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
307 {
308 static const char shutdown_str[8] = "Shutdown";
309 static int shutdown_index = 0;
311 switch(addr) {
312 /* Bochs BIOS messages */
313 case 0x400:
314 case 0x401:
315 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
316 exit(1);
317 case 0x402:
318 case 0x403:
319 #ifdef DEBUG_BIOS
320 fprintf(stderr, "%c", val);
321 #endif
322 break;
323 case 0x8900:
324 /* same as Bochs power off */
325 if (val == shutdown_str[shutdown_index]) {
326 shutdown_index++;
327 if (shutdown_index == 8) {
328 shutdown_index = 0;
329 qemu_system_shutdown_request();
330 }
331 } else {
332 shutdown_index = 0;
333 }
334 break;
336 /* LGPL'ed VGA BIOS messages */
337 case 0x501:
338 case 0x502:
339 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
340 exit(1);
341 case 0x500:
342 case 0x503:
343 #ifdef DEBUG_BIOS
344 fprintf(stderr, "%c", val);
345 #endif
346 break;
347 }
348 }
350 void bochs_bios_init(void)
351 {
352 register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
353 register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
354 register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
355 register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
356 register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
358 register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
359 register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
360 register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
361 register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
362 }
364 /* Generate an initial boot sector which sets state and jump to
365 a specified vector */
366 static void generate_bootsect(uint32_t gpr[8], uint16_t segs[6], uint16_t ip)
367 {
368 uint8_t bootsect[512], *p;
369 int i;
371 if (bs_table[0] == NULL) {
372 fprintf(stderr, "A disk image must be given for 'hda' when booting "
373 "a Linux kernel\n");
374 exit(1);
375 }
377 memset(bootsect, 0, sizeof(bootsect));
379 /* Copy the MSDOS partition table if possible */
380 bdrv_read(bs_table[0], 0, bootsect, 1);
382 /* Make sure we have a partition signature */
383 bootsect[510] = 0x55;
384 bootsect[511] = 0xaa;
386 /* Actual code */
387 p = bootsect;
388 *p++ = 0xfa; /* CLI */
389 *p++ = 0xfc; /* CLD */
391 for (i = 0; i < 6; i++) {
392 if (i == 1) /* Skip CS */
393 continue;
395 *p++ = 0xb8; /* MOV AX,imm16 */
396 *p++ = segs[i];
397 *p++ = segs[i] >> 8;
398 *p++ = 0x8e; /* MOV <seg>,AX */
399 *p++ = 0xc0 + (i << 3);
400 }
402 for (i = 0; i < 8; i++) {
403 *p++ = 0x66; /* 32-bit operand size */
404 *p++ = 0xb8 + i; /* MOV <reg>,imm32 */
405 *p++ = gpr[i];
406 *p++ = gpr[i] >> 8;
407 *p++ = gpr[i] >> 16;
408 *p++ = gpr[i] >> 24;
409 }
411 *p++ = 0xea; /* JMP FAR */
412 *p++ = ip; /* IP */
413 *p++ = ip >> 8;
414 *p++ = segs[1]; /* CS */
415 *p++ = segs[1] >> 8;
417 bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect));
418 }
421 static long get_file_size(FILE *f)
422 {
423 long where, size;
425 /* XXX: on Unix systems, using fstat() probably makes more sense */
427 where = ftell(f);
428 fseek(f, 0, SEEK_END);
429 size = ftell(f);
430 fseek(f, where, SEEK_SET);
432 return size;
433 }
435 static int fread2guest(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
436 {
437 size_t offset = 0;
438 while (nbytes) {
439 uint8_t buf[4096];
440 size_t count = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
441 if (fread(buf, 1, count, f) != count)
442 return -1;
444 cpu_physical_memory_rw(dst_addr+offset, buf, count, 1);
445 offset += count;
446 nbytes -= count;
447 }
448 return 0;
449 }
451 static void load_linux(const char *kernel_filename,
452 const char *initrd_filename,
453 const char *kernel_cmdline)
454 {
455 uint16_t protocol;
456 uint32_t gpr[8];
457 uint16_t seg[6];
458 uint16_t real_seg;
459 int setup_size, kernel_size, initrd_size, cmdline_size;
460 uint32_t initrd_max;
461 uint8_t header[1024];
462 target_phys_addr_t real_addr, reloc_prot_addr, prot_addr, cmdline_addr, initrd_addr;
463 size_t ncmdline;
464 FILE *f, *fi;
466 /* Align to 16 bytes as a paranoia measure */
467 cmdline_size = (strlen(kernel_cmdline)+16) & ~15;
469 /* load the kernel header */
470 f = fopen(kernel_filename, "rb");
471 if (!f || !(kernel_size = get_file_size(f)) ||
472 fread(header, 1, 1024, f) != 1024) {
473 fprintf(stderr, "qemu: could not load kernel '%s'\n",
474 kernel_filename);
475 exit(1);
476 }
478 /* kernel protocol version */
479 fprintf(stderr, "header magic: %#x\n", ldl_p(header+0x202));
480 if (ldl_p(header+0x202) == 0x53726448)
481 protocol = lduw_p(header+0x206);
482 else
483 protocol = 0;
484 fprintf(stderr, "header protocol: %x\n", protocol);
485 if (protocol < 0x200 || !(header[0x211] & 0x01)) {
486 /* Low kernel */
487 real_addr = 0x90000;
488 cmdline_addr = 0x9a000 - cmdline_size;
489 prot_addr = 0x10000;
490 reloc_prot_addr = prot_addr;
491 } else if (protocol < 0x202) {
492 /* High but ancient kernel */
493 real_addr = 0x90000;
494 cmdline_addr = 0x9a000 - cmdline_size;
495 prot_addr = 0x100000;
496 reloc_prot_addr = 0x200000;
497 } else {
498 /* High and recent kernel */
499 real_addr = 0x10000;
500 cmdline_addr = 0x20000;
501 prot_addr = 0x100000;
502 reloc_prot_addr = 0x200000;
503 }
505 fprintf(stderr,
506 "qemu: real_addr = %#zx\n"
507 "qemu: cmdline_addr = %#zx\n"
508 "qemu: prot_addr = %#zx\n",
509 real_addr,
510 cmdline_addr,
511 prot_addr);
513 /* highest address for loading the initrd */
514 if (protocol >= 0x203)
515 initrd_max = ldl_p(header+0x22c);
516 else
517 initrd_max = 0x37ffffff;
519 if (initrd_max >= ram_size-ACPI_DATA_SIZE)
520 initrd_max = ram_size-ACPI_DATA_SIZE-1;
523 /* kernel command line */
524 ncmdline = strlen(kernel_cmdline);
525 if (ncmdline > 4095) {
526 ncmdline = 4095;
527 ((uint8_t*)kernel_cmdline)[4095] = '\0';
528 }
529 fprintf(stderr, "qemu: kernel_cmdline: %#zx ('%s')\n", ncmdline, kernel_cmdline);
530 cpu_physical_memory_rw(cmdline_addr, (uint8_t*)kernel_cmdline, ncmdline+1, 1);
532 if (protocol >= 0x202) {
533 stl_p(header+0x228, cmdline_addr);
534 } else {
535 stw_p(header+0x20, 0xA33F);
536 stw_p(header+0x22, cmdline_addr-real_addr);
537 }
539 /* loader type */
540 /* High nybble = B reserved for Qemu; low nybble is revision number.
541 If this code is substantially changed, you may want to consider
542 incrementing the revision. */
543 if (protocol >= 0x200)
544 header[0x210] = 0xB0;
546 /* heap */
547 if (protocol >= 0x201) {
548 header[0x211] |= 0x80; /* CAN_USE_HEAP */
549 stw_p(header+0x224, cmdline_addr-real_addr-0x200);
550 }
552 /* load initrd */
553 if (initrd_filename) {
554 if (protocol < 0x200) {
555 fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
556 exit(1);
557 }
559 fi = fopen(initrd_filename, "rb");
560 if (!fi) {
561 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
562 initrd_filename);
563 exit(1);
564 }
566 initrd_size = get_file_size(fi);
567 initrd_addr = ((initrd_max-initrd_size) & ~4095);
569 fprintf(stderr, "qemu: loading initrd (%#x bytes) at %#zx\n",
570 initrd_size, initrd_addr);
572 if (fread2guest(initrd_addr, initrd_size, fi) < 0) {
573 fprintf(stderr, "qemu: read error on initial ram disk '%s'\n",
574 initrd_filename);
575 exit(1);
576 }
577 fclose(fi);
579 stl_p(header+0x218, initrd_addr);
580 stl_p(header+0x21c, initrd_size);
581 }
584 setup_size = header[0x1f1];
585 if (setup_size == 0)
586 setup_size = 4;
588 setup_size = (setup_size+1)*512;
589 kernel_size -= setup_size; /* Size of protected-mode code */
591 /* Urgh, Xen's HVM firmware lives at 0x100000, but that's also the
592 * address Linux wants to start life at prior to relocatable support
593 */
594 if (prot_addr != reloc_prot_addr) {
595 if (protocol >= 0x205 && (header[0x234] & 1)) {
596 /* Relocatable automatically */
597 stl_p(header+0x214, reloc_prot_addr);
598 fprintf(stderr, "qemu: kernel is relocatable\n");
599 } else {
600 fprintf(stderr, "qemu: unable to load non-relocatable kernel\n");
601 exit(1);
602 }
603 }
606 fprintf(stderr, "qemu: loading kernel real mode (%#x bytes) at %#zx\n",
607 setup_size-1024, real_addr);
608 fprintf(stderr, "qemu: loading kernel protected mode (%#x bytes) at %#zx\n",
609 kernel_size, reloc_prot_addr);
611 /* store the finalized header and load the rest of the kernel */
612 cpu_physical_memory_rw(real_addr, header, 1024, 1);
613 if (fread2guest(real_addr+1024, setup_size-1024, f) < 0 ||
614 fread2guest(reloc_prot_addr, kernel_size, f) < 0) {
615 fprintf(stderr, "qemu: loading kernel protected mode (%#x bytes) at %#zx\n",
616 kernel_size, reloc_prot_addr);
617 exit(1);
618 }
619 fclose(f);
621 /* generate bootsector to set up the initial register state */
622 real_seg = (real_addr) >> 4;
623 seg[0] = seg[2] = seg[3] = seg[4] = seg[4] = real_seg;
624 seg[1] = real_seg+0x20; /* CS */
625 memset(gpr, 0, sizeof gpr);
626 gpr[4] = cmdline_addr-real_addr-16; /* SP (-16 is paranoia) */
628 generate_bootsect(gpr, seg, 0);
629 }
631 static void main_cpu_reset(void *opaque)
632 {
633 CPUState *env = opaque;
634 cpu_reset(env);
635 }
637 static const int ide_iobase[2] = { 0x1f0, 0x170 };
638 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
639 static const int ide_irq[2] = { 14, 15 };
641 #define NE2000_NB_MAX 6
643 static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
644 static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
646 static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
647 static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
649 static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
650 static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
652 #ifdef HAS_AUDIO
653 static void audio_init (PCIBus *pci_bus)
654 {
655 struct soundhw *c;
656 int audio_enabled = 0;
658 for (c = soundhw; !audio_enabled && c->name; ++c) {
659 audio_enabled = c->enabled;
660 }
662 if (audio_enabled) {
663 AudioState *s;
665 s = AUD_init ();
666 if (s) {
667 for (c = soundhw; c->name; ++c) {
668 if (c->enabled) {
669 if (c->isa) {
670 c->init.init_isa (s);
671 }
672 else {
673 if (pci_bus) {
674 c->init.init_pci (pci_bus, s);
675 }
676 }
677 }
678 }
679 }
680 }
681 }
682 #endif
684 static void pc_init_ne2k_isa(NICInfo *nd)
685 {
686 static int nb_ne2k = 0;
688 if (nb_ne2k == NE2000_NB_MAX)
689 return;
690 isa_ne2000_init(ne2000_io[nb_ne2k], ne2000_irq[nb_ne2k], nd);
691 nb_ne2k++;
692 }
694 #define NOBIOS 1
696 /* PC hardware initialisation */
697 static void pc_init1(uint64_t ram_size, int vga_ram_size, char *boot_device,
698 DisplayState *ds, const char **fd_filename, int snapshot,
699 const char *kernel_filename, const char *kernel_cmdline,
700 const char *initrd_filename,
701 int pci_enabled, const char *direct_pci)
702 {
703 #ifndef NOBIOS
704 char buf[1024];
705 int ret, initrd_size;
706 #endif /* !NOBIOS */
707 int linux_boot, i;
708 #ifndef NOBIOS
709 unsigned long bios_offset, vga_bios_offset, option_rom_offset;
710 int bios_size, isa_bios_size;
711 #endif /* !NOBIOS */
712 PCIBus *pci_bus;
713 int piix3_devfn = -1;
714 CPUState *env;
715 NICInfo *nd;
716 int rc;
718 linux_boot = (kernel_filename != NULL);
720 /* init CPUs */
721 for(i = 0; i < smp_cpus; i++) {
722 env = cpu_init();
723 #ifndef CONFIG_DM
724 if (i != 0)
725 env->hflags |= HF_HALTED_MASK;
726 if (smp_cpus > 1) {
727 /* XXX: enable it in all cases */
728 env->cpuid_features |= CPUID_APIC;
729 }
730 #endif /* !CONFIG_DM */
731 register_savevm("cpu", i, 4, cpu_save, cpu_load, env);
732 qemu_register_reset(main_cpu_reset, env);
733 #ifndef CONFIG_DM
734 if (pci_enabled) {
735 apic_init(env);
736 }
737 #endif /* !CONFIG_DM */
738 }
740 /* allocate RAM */
741 #ifndef CONFIG_DM /* HVM domain owns memory */
742 cpu_register_physical_memory(0, ram_size, 0);
743 #endif
745 #ifndef NOBIOS
746 /* BIOS load */
747 bios_offset = ram_size + vga_ram_size;
748 vga_bios_offset = bios_offset + 256 * 1024;
750 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
751 bios_size = get_image_size(buf);
752 if (bios_size <= 0 ||
753 (bios_size % 65536) != 0 ||
754 bios_size > (256 * 1024)) {
755 goto bios_error;
756 }
757 ret = load_image(buf, phys_ram_base + bios_offset);
758 if (ret != bios_size) {
759 bios_error:
760 fprintf(stderr, "qemu: could not load PC bios '%s'\n", buf);
761 exit(1);
762 }
764 /* VGA BIOS load */
765 if (cirrus_vga_enabled) {
766 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
767 } else {
768 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
769 }
770 ret = load_image(buf, phys_ram_base + vga_bios_offset);
771 #endif /* !NOBIOS */
773 /* setup basic memory access */
774 #ifndef CONFIG_DM /* HVM domain owns memory */
775 cpu_register_physical_memory(0xc0000, 0x10000,
776 vga_bios_offset | IO_MEM_ROM);
777 #endif
779 #ifndef NOBIOS
780 /* map the last 128KB of the BIOS in ISA space */
781 isa_bios_size = bios_size;
782 if (isa_bios_size > (128 * 1024))
783 isa_bios_size = 128 * 1024;
784 cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size,
785 IO_MEM_UNASSIGNED);
786 cpu_register_physical_memory(0x100000 - isa_bios_size,
787 isa_bios_size,
788 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
790 option_rom_offset = 0;
791 for (i = 0; i < nb_option_roms; i++) {
792 int offset = bios_offset + bios_size + option_rom_offset;
793 int size;
795 size = load_image(option_rom[i], phys_ram_base + offset);
796 if ((size + option_rom_offset) > 0x10000) {
797 fprintf(stderr, "Too many option ROMS\n");
798 exit(1);
799 }
800 cpu_register_physical_memory(0xd0000 + option_rom_offset,
801 size, offset | IO_MEM_ROM);
802 option_rom_offset += size + 2047;
803 option_rom_offset -= (option_rom_offset % 2048);
804 }
806 /* map all the bios at the top of memory */
807 cpu_register_physical_memory((uint32_t)(-bios_size),
808 bios_size, bios_offset | IO_MEM_ROM);
809 #endif
811 bochs_bios_init();
813 if (linux_boot)
814 load_linux(kernel_filename, initrd_filename, kernel_cmdline);
816 if (pci_enabled) {
817 pci_bus = i440fx_init(&i440fx_state);
818 piix3_devfn = piix3_init(pci_bus, -1);
819 } else {
820 pci_bus = NULL;
821 }
823 /* init basic PC hardware */
824 register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
826 register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
828 if (cirrus_vga_enabled) {
829 if (pci_enabled) {
830 pci_cirrus_vga_init(pci_bus,
831 ds, NULL, ram_size,
832 vga_ram_size);
833 } else {
834 isa_cirrus_vga_init(ds, NULL, ram_size,
835 vga_ram_size);
836 }
837 } else {
838 if (pci_enabled) {
839 pci_vga_init(pci_bus, ds, NULL, ram_size,
840 vga_ram_size, 0, 0);
841 } else {
842 isa_vga_init(ds, NULL, ram_size,
843 vga_ram_size);
844 }
845 }
847 #ifdef CONFIG_PASSTHROUGH
848 /* Pass-through Initialization */
849 if ( pci_enabled && direct_pci )
850 {
851 rc = pt_init(pci_bus, direct_pci);
852 if ( rc < 0 )
853 {
854 fprintf(logfile, "Error: Initialization failed for pass-through devices\n");
855 exit(1);
856 }
857 }
858 #endif
860 rtc_state = rtc_init(0x70, 8);
862 register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
863 register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
865 #ifndef CONFIG_DM
866 if (pci_enabled) {
867 ioapic = ioapic_init();
868 }
869 #endif /* !CONFIG_DM */
870 isa_pic = pic_init(pic_irq_request, first_cpu);
871 #ifndef CONFIG_DM
872 pit = pit_init(0x40, 0);
873 pcspk_init(pit);
874 #endif /* !CONFIG_DM */
875 #ifndef CONFIG_DM
876 if (pci_enabled) {
877 pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
878 }
879 #endif /* !CONFIG_DM */
881 if (pci_enabled)
882 pci_xen_platform_init(pci_bus);
884 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
885 if (serial_hds[i]) {
886 serial_init(&pic_set_irq_new, isa_pic,
887 serial_io[i], serial_irq[i], serial_hds[i]);
888 }
889 }
891 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
892 if (parallel_hds[i]) {
893 parallel_init(parallel_io[i], parallel_irq[i], parallel_hds[i]);
894 }
895 }
897 for(i = 0; i < nb_nics; i++) {
898 nd = &nd_table[i];
899 if (!nd->model) {
900 if (pci_enabled) {
901 nd->model = "ne2k_pci";
902 } else {
903 nd->model = "ne2k_isa";
904 }
905 }
906 if (strcmp(nd->model, "ne2k_isa") == 0) {
907 pc_init_ne2k_isa(nd);
908 } else if (pci_enabled) {
909 pci_nic_init(pci_bus, nd, -1);
910 } else {
911 fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
912 exit(1);
913 }
914 }
916 if (pci_enabled) {
917 pci_piix3_ide_init(pci_bus, bs_table, piix3_devfn + 1);
918 } else {
919 for(i = 0; i < 2; i++) {
920 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
921 bs_table[2 * i], bs_table[2 * i + 1]);
922 }
923 }
925 if (has_tpm_device())
926 tpm_tis_init(&pic_set_irq_new, isa_pic, 11);
928 kbd_init();
929 DMA_init(0);
930 #ifdef HAS_AUDIO
931 audio_init(pci_enabled ? pci_bus : NULL);
932 #endif
934 floppy_controller = fdctrl_init(6, 2, 0, 0x3f0, fd_table);
936 cmos_init(ram_size, boot_device, bs_table);
938 /* using PIIX4 acpi model */
939 if (pci_enabled && acpi_enabled)
940 pci_piix4_acpi_init(pci_bus, piix3_devfn + 2);
942 if (pci_enabled && usb_enabled) {
943 usb_uhci_init(pci_bus, piix3_devfn + (acpi_enabled ? 3 : 2));
944 }
946 #ifndef CONFIG_DM
947 if (pci_enabled && acpi_enabled) {
948 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
949 piix4_pm_init(pci_bus, piix3_devfn + 3);
950 for (i = 0; i < 8; i++) {
951 SMBusDevice *eeprom = smbus_eeprom_device_init(0x50 + i,
952 eeprom_buf + (i * 256));
953 piix4_smbus_register_device(eeprom, 0x50 + i);
954 }
955 }
957 if (i440fx_state) {
958 i440fx_init_memory_mappings(i440fx_state);
959 }
960 #if 0
961 /* ??? Need to figure out some way for the user to
962 specify SCSI devices. */
963 if (pci_enabled) {
964 void *scsi;
965 BlockDriverState *bdrv;
967 scsi = lsi_scsi_init(pci_bus, -1);
968 bdrv = bdrv_new("scsidisk");
969 bdrv_open(bdrv, "scsi_disk.img", 0);
970 lsi_scsi_attach(scsi, bdrv, -1);
971 bdrv = bdrv_new("scsicd");
972 bdrv_open(bdrv, "scsi_cd.iso", 0);
973 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
974 lsi_scsi_attach(scsi, bdrv, -1);
975 }
976 #endif
977 #else
978 if (pci_enabled) {
979 void *scsi = NULL;
980 for (i = 0; i < MAX_SCSI_DISKS ; i++) {
981 if (!bs_table[i + MAX_DISKS])
982 continue;
983 if (!scsi)
984 scsi = lsi_scsi_init(pci_bus, -1);
985 lsi_scsi_attach(scsi, bs_table[i + MAX_DISKS], -1);
986 }
987 }
988 #endif /* !CONFIG_DM */
989 }
991 static void pc_init_pci(uint64_t ram_size, int vga_ram_size, char *boot_device,
992 DisplayState *ds, const char **fd_filename,
993 int snapshot,
994 const char *kernel_filename,
995 const char *kernel_cmdline,
996 const char *initrd_filename,
997 const char *direct_pci)
998 {
999 pc_init1(ram_size, vga_ram_size, boot_device,
1000 ds, fd_filename, snapshot,
1001 kernel_filename, kernel_cmdline,
1002 initrd_filename, 1,
1003 direct_pci);
1006 static void pc_init_isa(uint64_t ram_size, int vga_ram_size, char *boot_device,
1007 DisplayState *ds, const char **fd_filename,
1008 int snapshot,
1009 const char *kernel_filename,
1010 const char *kernel_cmdline,
1011 const char *initrd_filename,
1012 const char *unused)
1014 pc_init1(ram_size, vga_ram_size, boot_device,
1015 ds, fd_filename, snapshot,
1016 kernel_filename, kernel_cmdline,
1017 initrd_filename, 0, NULL);
1020 QEMUMachine pc_machine = {
1021 "pc",
1022 "Standard PC",
1023 pc_init_pci,
1024 };
1026 QEMUMachine isapc_machine = {
1027 "isapc",
1028 "ISA-only PC",
1029 pc_init_isa,
1030 };