direct-io.hg

view tools/ioemu/hw/pc.c @ 9672:5765497cf75e

Sync PIT device model with latest qemu and move it to hypervisor.
Signed-off-by: Edwin Zhai <edwin.zhai@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Apr 19 18:38:14 2006 +0100 (2006-04-19)
parents b29954d81e6d
children 922f95ab88e4
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 #define KERNEL_LOAD_ADDR 0x00100000
35 #define INITRD_LOAD_ADDR 0x00400000
36 #define KERNEL_PARAMS_ADDR 0x00090000
37 #define KERNEL_CMDLINE_ADDR 0x00099000
39 int speaker_data_on;
40 int dummy_refresh_clock;
41 static fdctrl_t *floppy_controller;
42 static RTCState *rtc_state;
44 static void ioport80_write(void *opaque, uint32_t addr, uint32_t data)
45 {
46 }
48 /* MSDOS compatibility mode FPU exception support */
49 /* XXX: add IGNNE support */
50 void cpu_set_ferr(CPUX86State *s)
51 {
52 pic_set_irq(13, 1);
53 }
55 static void ioportF0_write(void *opaque, uint32_t addr, uint32_t data)
56 {
57 pic_set_irq(13, 0);
58 }
60 /* TSC handling */
62 uint64_t cpu_get_tsc(CPUX86State *env)
63 {
64 return qemu_get_clock(vm_clock);
65 }
67 /* PC cmos mappings */
69 #define REG_EQUIPMENT_BYTE 0x14
70 #define REG_IBM_CENTURY_BYTE 0x32
71 #define REG_IBM_PS2_CENTURY_BYTE 0x37
74 static inline int to_bcd(RTCState *s, int a)
75 {
76 return ((a / 10) << 4) | (a % 10);
77 }
79 static int cmos_get_fd_drive_type(int fd0)
80 {
81 int val;
83 switch (fd0) {
84 case 0:
85 /* 1.44 Mb 3"5 drive */
86 val = 4;
87 break;
88 case 1:
89 /* 2.88 Mb 3"5 drive */
90 val = 5;
91 break;
92 case 2:
93 /* 1.2 Mb 5"5 drive */
94 val = 2;
95 break;
96 default:
97 val = 0;
98 break;
99 }
100 return val;
101 }
103 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
104 {
105 RTCState *s = rtc_state;
106 int cylinders, heads, sectors;
107 bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
108 rtc_set_memory(s, type_ofs, 47);
109 rtc_set_memory(s, info_ofs, cylinders);
110 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
111 rtc_set_memory(s, info_ofs + 2, heads);
112 rtc_set_memory(s, info_ofs + 3, 0xff);
113 rtc_set_memory(s, info_ofs + 4, 0xff);
114 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
115 rtc_set_memory(s, info_ofs + 6, cylinders);
116 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
117 rtc_set_memory(s, info_ofs + 8, sectors);
118 }
120 /* hd_table must contain 4 block drivers */
121 static void cmos_init(uint64_t ram_size, int boot_device, BlockDriverState **hd_table)
122 {
123 RTCState *s = rtc_state;
124 int val;
125 int fd0, fd1, nb;
126 time_t ti;
127 struct tm *tm;
128 int i;
130 /* set the CMOS date */
131 time(&ti);
132 if (rtc_utc)
133 tm = gmtime(&ti);
134 else
135 tm = localtime(&ti);
136 rtc_set_date(s, tm);
138 val = to_bcd(s, (tm->tm_year / 100) + 19);
139 rtc_set_memory(s, REG_IBM_CENTURY_BYTE, val);
140 rtc_set_memory(s, REG_IBM_PS2_CENTURY_BYTE, val);
142 /* various important CMOS locations needed by PC/Bochs bios */
144 /* memory size */
145 val = 640; /* base memory in K */
146 rtc_set_memory(s, 0x15, val);
147 rtc_set_memory(s, 0x16, val >> 8);
149 val = (ram_size / 1024) - 1024;
150 if (val > 65535)
151 val = 65535;
152 rtc_set_memory(s, 0x17, val);
153 rtc_set_memory(s, 0x18, val >> 8);
154 rtc_set_memory(s, 0x30, val);
155 rtc_set_memory(s, 0x31, val >> 8);
157 if (ram_size > (16 * 1024 * 1024))
158 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
159 else
160 val = 0;
161 if (val > 65535)
162 val = 65535;
163 rtc_set_memory(s, 0x34, val);
164 rtc_set_memory(s, 0x35, val >> 8);
166 switch(boot_device) {
167 case 'a':
168 case 'b':
169 rtc_set_memory(s, 0x3d, 0x01); /* floppy boot */
170 break;
171 default:
172 case 'c':
173 rtc_set_memory(s, 0x3d, 0x02); /* hard drive boot */
174 break;
175 case 'd':
176 rtc_set_memory(s, 0x3d, 0x03); /* CD-ROM boot */
177 break;
178 }
180 /* floppy type */
182 fd0 = fdctrl_get_drive_type(floppy_controller, 0);
183 fd1 = fdctrl_get_drive_type(floppy_controller, 1);
185 val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
186 rtc_set_memory(s, 0x10, val);
188 val = 0;
189 nb = 0;
190 if (fd0 < 3)
191 nb++;
192 if (fd1 < 3)
193 nb++;
194 switch (nb) {
195 case 0:
196 break;
197 case 1:
198 val |= 0x01; /* 1 drive, ready for boot */
199 break;
200 case 2:
201 val |= 0x41; /* 2 drives, ready for boot */
202 break;
203 }
204 val |= 0x02; /* FPU is there */
205 val |= 0x04; /* PS/2 mouse installed */
206 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
208 /* hard drives */
210 rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
211 if (hd_table[0])
212 cmos_init_hd(0x19, 0x1b, hd_table[0]);
213 if (hd_table[1])
214 cmos_init_hd(0x1a, 0x24, hd_table[1]);
216 val = 0;
217 for (i = 0; i < 4; i++) {
218 if (hd_table[i]) {
219 int cylinders, heads, sectors;
220 uint8_t translation;
221 /* NOTE: bdrv_get_geometry_hint() returns the geometry
222 that the hard disk returns. It is always such that: 1 <=
223 sects <= 63, 1 <= heads <= 16, 1 <= cylinders <=
224 16383. The BIOS geometry can be different. */
225 bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
226 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
227 /* No translation. */
228 translation = 0;
229 } else {
230 /* LBA translation. */
231 translation = 1;
232 }
233 val |= translation << (i * 2);
234 }
235 }
236 rtc_set_memory(s, 0x39, val);
238 /* Disable check of 0x55AA signature on the last two bytes of
239 first sector of disk. XXX: make it the default ? */
240 // rtc_set_memory(s, 0x38, 1);
241 }
243 static void speaker_ioport_write(void *opaque, uint32_t addr, uint32_t val)
244 {
245 fprintf(stderr, "speaker port should not be handled in DM!\n");
246 }
248 static uint32_t speaker_ioport_read(void *opaque, uint32_t addr)
249 {
250 fprintf(stderr, "speaker port should not be handled in DM!\n");
251 return 0;
252 }
254 static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
255 {
256 cpu_x86_set_a20(cpu_single_env, (val >> 1) & 1);
257 /* XXX: bit 0 is fast reset */
258 }
260 static uint32_t ioport92_read(void *opaque, uint32_t addr)
261 {
262 return ((cpu_single_env->a20_mask >> 20) & 1) << 1;
263 }
265 /***********************************************************/
266 /* Bochs BIOS debug ports */
268 void bochs_bios_write(void *opaque, uint32_t addr, uint32_t val)
269 {
270 static const char shutdown_str[8] = "Shutdown";
271 static int shutdown_index = 0;
273 switch(addr) {
274 /* Bochs BIOS messages */
275 case 0x400:
276 case 0x401:
277 fprintf(stderr, "BIOS panic at rombios.c, line %d\n", val);
278 exit(1);
279 case 0x402:
280 case 0x403:
281 #ifdef DEBUG_BIOS
282 fprintf(stderr, "%c", val);
283 #endif
284 break;
285 case 0x8900:
286 /* same as Bochs power off */
287 if (val == shutdown_str[shutdown_index]) {
288 shutdown_index++;
289 if (shutdown_index == 8) {
290 shutdown_index = 0;
291 qemu_system_shutdown_request();
292 }
293 } else {
294 shutdown_index = 0;
295 }
296 break;
298 /* LGPL'ed VGA BIOS messages */
299 case 0x501:
300 case 0x502:
301 fprintf(stderr, "VGA BIOS panic, line %d\n", val);
302 exit(1);
303 case 0x500:
304 case 0x503:
305 #ifdef DEBUG_BIOS
306 fprintf(stderr, "%c", val);
307 #endif
308 break;
309 }
310 }
312 void bochs_bios_init(void)
313 {
314 register_ioport_write(0x400, 1, 2, bochs_bios_write, NULL);
315 register_ioport_write(0x401, 1, 2, bochs_bios_write, NULL);
316 register_ioport_write(0x402, 1, 1, bochs_bios_write, NULL);
317 register_ioport_write(0x403, 1, 1, bochs_bios_write, NULL);
318 register_ioport_write(0x8900, 1, 1, bochs_bios_write, NULL);
320 register_ioport_write(0x501, 1, 2, bochs_bios_write, NULL);
321 register_ioport_write(0x502, 1, 2, bochs_bios_write, NULL);
322 register_ioport_write(0x500, 1, 1, bochs_bios_write, NULL);
323 register_ioport_write(0x503, 1, 1, bochs_bios_write, NULL);
324 }
327 int load_kernel(const char *filename, uint8_t *addr,
328 uint8_t *real_addr)
329 {
330 int fd, size;
331 int setup_sects;
333 fd = open(filename, O_RDONLY | O_BINARY);
334 if (fd < 0)
335 return -1;
337 /* load 16 bit code */
338 if (read(fd, real_addr, 512) != 512)
339 goto fail;
340 setup_sects = real_addr[0x1F1];
341 if (!setup_sects)
342 setup_sects = 4;
343 if (read(fd, real_addr + 512, setup_sects * 512) !=
344 setup_sects * 512)
345 goto fail;
347 /* load 32 bit code */
348 size = read(fd, addr, 16 * 1024 * 1024);
349 if (size < 0)
350 goto fail;
351 close(fd);
352 return size;
353 fail:
354 close(fd);
355 return -1;
356 }
358 static const int ide_iobase[2] = { 0x1f0, 0x170 };
359 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
360 static const int ide_irq[2] = { 14, 15 };
362 #define NE2000_NB_MAX 6
364 static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
365 static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
367 static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
368 static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
370 #define NOBIOS 1
372 /* PC hardware initialisation */
373 void pc_init(uint64_t ram_size, int vga_ram_size, int boot_device,
374 DisplayState *ds, const char **fd_filename, int snapshot,
375 const char *kernel_filename, const char *kernel_cmdline,
376 const char *initrd_filename)
377 {
378 char buf[1024];
379 int ret, linux_boot, initrd_size, i, nb_nics1;
380 PCIBus *pci_bus;
381 extern void * shared_vram;
383 linux_boot = (kernel_filename != NULL);
385 /* allocate RAM */
386 // cpu_register_physical_memory(0, ram_size, 0);
388 #ifndef NOBIOS
389 /* BIOS load */
390 bios_offset = ram_size + vga_ram_size;
391 vga_bios_offset = bios_offset + 256 * 1024;
393 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
394 bios_size = get_image_size(buf);
395 if (bios_size <= 0 ||
396 (bios_size % 65536) != 0 ||
397 bios_size > (256 * 1024)) {
398 goto bios_error;
399 }
400 ret = load_image(buf, phys_ram_base + bios_offset);
401 if (ret != bios_size) {
402 bios_error:
403 fprintf(stderr, "qemu: could not load PC bios '%s'\n", buf);
404 exit(1);
405 }
407 /* VGA BIOS load */
408 if (cirrus_vga_enabled) {
409 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_CIRRUS_FILENAME);
410 } else {
411 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
412 }
413 ret = load_image(buf, phys_ram_base + vga_bios_offset);
414 #endif
416 #ifndef NOBIOS
417 /* setup basic memory access */
418 cpu_register_physical_memory(0xc0000, 0x10000,
419 vga_bios_offset | IO_MEM_ROM);
421 /* map the last 128KB of the BIOS in ISA space */
422 isa_bios_size = bios_size;
423 if (isa_bios_size > (128 * 1024))
424 isa_bios_size = 128 * 1024;
425 cpu_register_physical_memory(0xd0000, (192 * 1024) - isa_bios_size,
426 IO_MEM_UNASSIGNED);
427 cpu_register_physical_memory(0x100000 - isa_bios_size,
428 isa_bios_size,
429 (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
430 /* map all the bios at the top of memory */
431 cpu_register_physical_memory((uint32_t)(-bios_size),
432 bios_size, bios_offset | IO_MEM_ROM);
433 #endif
435 bochs_bios_init();
437 if (linux_boot) {
438 uint8_t bootsect[512];
439 uint8_t old_bootsect[512];
441 if (bs_table[0] == NULL) {
442 fprintf(stderr, "A disk image must be given for 'hda' when booting a Linux kernel\n");
443 exit(1);
444 }
445 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, LINUX_BOOT_FILENAME);
446 ret = load_image(buf, bootsect);
447 if (ret != sizeof(bootsect)) {
448 fprintf(stderr, "qemu: could not load linux boot sector '%s'\n",
449 buf);
450 exit(1);
451 }
453 if (bdrv_read(bs_table[0], 0, old_bootsect, 1) >= 0) {
454 /* copy the MSDOS partition table */
455 memcpy(bootsect + 0x1be, old_bootsect + 0x1be, 0x40);
456 }
458 bdrv_set_boot_sector(bs_table[0], bootsect, sizeof(bootsect));
460 /* now we can load the kernel */
461 ret = load_kernel(kernel_filename,
462 phys_ram_base + KERNEL_LOAD_ADDR,
463 phys_ram_base + KERNEL_PARAMS_ADDR);
464 if (ret < 0) {
465 fprintf(stderr, "qemu: could not load kernel '%s'\n",
466 kernel_filename);
467 exit(1);
468 }
470 /* load initrd */
471 initrd_size = 0;
472 if (initrd_filename) {
473 initrd_size = load_image(initrd_filename, phys_ram_base + INITRD_LOAD_ADDR);
474 if (initrd_size < 0) {
475 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
476 initrd_filename);
477 exit(1);
478 }
479 }
480 if (initrd_size > 0) {
481 stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x218, INITRD_LOAD_ADDR);
482 stl_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x21c, initrd_size);
483 }
484 pstrcpy(phys_ram_base + KERNEL_CMDLINE_ADDR, 4096,
485 kernel_cmdline);
486 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x20, 0xA33F);
487 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x22,
488 KERNEL_CMDLINE_ADDR - KERNEL_PARAMS_ADDR);
489 /* loader type */
490 stw_raw(phys_ram_base + KERNEL_PARAMS_ADDR + 0x210, 0x01);
491 }
493 if (pci_enabled) {
494 pci_bus = i440fx_init();
495 piix3_init(pci_bus);
496 } else {
497 pci_bus = NULL;
498 }
500 /* init basic PC hardware */
501 register_ioport_write(0x80, 1, 1, ioport80_write, NULL);
503 register_ioport_write(0xf0, 1, 1, ioportF0_write, NULL);
505 if (cirrus_vga_enabled) {
506 if (pci_enabled) {
507 pci_cirrus_vga_init(pci_bus,
508 ds, shared_vram, ram_size,
509 vga_ram_size);
510 } else {
511 isa_cirrus_vga_init(ds, shared_vram, ram_size,
512 vga_ram_size);
513 }
514 } else {
515 vga_initialize(pci_bus, ds, shared_vram, ram_size,
516 vga_ram_size);
517 }
519 rtc_state = rtc_init(0x70, 8);
520 register_ioport_read(0x61, 1, 1, speaker_ioport_read, NULL);
521 register_ioport_write(0x61, 1, 1, speaker_ioport_write, NULL);
523 register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
524 register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
526 pic_init();
528 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
529 if (serial_hds[i]) {
530 serial_init(serial_io[i], serial_irq[i], serial_hds[i]);
531 }
532 }
534 if (pci_enabled) {
535 for(i = 0; i < nb_nics; i++) {
536 if (nic_ne2000)
537 pci_ne2000_init(pci_bus, &nd_table[i]);
538 else
539 pci_pcnet_init(pci_bus, &nd_table[i]);
540 }
541 pci_piix3_ide_init(pci_bus, bs_table);
542 } else {
543 nb_nics1 = nb_nics;
544 if (nb_nics1 > NE2000_NB_MAX)
545 nb_nics1 = NE2000_NB_MAX;
546 for(i = 0; i < nb_nics1; i++) {
547 isa_ne2000_init(ne2000_io[i], ne2000_irq[i], &nd_table[i]);
548 }
550 for(i = 0; i < 2; i++) {
551 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
552 bs_table[2 * i], bs_table[2 * i + 1]);
553 }
554 }
556 kbd_init();
557 DMA_init(0);
559 if (audio_enabled) {
560 AUD_init();
561 #ifdef USE_SB16
562 if (sb16_enabled)
563 SB16_init();
564 #endif
565 }
568 floppy_controller = fdctrl_init(6, 2, 0, 0x3f0, fd_table);
570 cmos_init(ram_size, boot_device, bs_table);
572 /* must be done after all PCI devices are instanciated */
573 /* XXX: should be done in the Bochs BIOS */
574 if (pci_enabled) {
575 pci_bios_init();
576 }
577 port_e9_init();
578 }