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