1 // Paravirtualization support.
3 // Copyright (C) 2013 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2009 Red Hat Inc.
7 // Gleb Natapov <gnatapov@redhat.com>
9 // This file may be distributed under the terms of the GNU LGPLv3 license.
11 #include "byteorder.h" // be32_to_cpu
12 #include "config.h" // CONFIG_QEMU
13 #include "e820map.h" // e820_add
14 #include "hw/pci.h" // create_pirtable
15 #include "hw/pci_regs.h" // PCI_DEVICE_ID
16 #include "hw/rtc.h" // CMOS_*
17 #include "malloc.h" // malloc_tmp
18 #include "output.h" // dprintf
19 #include "paravirt.h" // qemu_cfg_preinit
20 #include "romfile.h" // romfile_loadint
21 #include "romfile_loader.h" // romfile_loader_execute
22 #include "string.h" // memset
23 #include "util.h" // pci_setup
24 #include "x86.h" // cpuid
25 #include "xen.h" // xen_biostable_setup
26 #include "stacks.h" // yield
28 // Amount of continuous ram under 4Gig
30 // Amount of continuous ram >4Gig
32 // Type of emulator platform.
33 int PlatformRunningOn VARFSEG;
35 int cfg_dma_enabled = 0;
37 inline int qemu_cfg_dma_enabled(void)
39 return cfg_dma_enabled;
42 /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
43 * should be used to determine that a VM is running under KVM.
45 #define KVM_CPUID_SIGNATURE 0x40000000
47 static void kvm_detect(void)
49 unsigned int eax, ebx, ecx, edx;
52 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
53 memcpy(signature + 0, &ebx, 4);
54 memcpy(signature + 4, &ecx, 4);
55 memcpy(signature + 8, &edx, 4);
58 if (strcmp(signature, "KVMKVMKVM") == 0) {
59 dprintf(1, "Running on KVM\n");
60 PlatformRunningOn |= PF_KVM;
64 static void qemu_detect(void)
66 if (!CONFIG_QEMU_HARDWARE)
69 // check northbridge @ 00:00.0
70 u16 v = pci_config_readw(0, PCI_VENDOR_ID);
71 if (v == 0x0000 || v == 0xffff)
73 u16 d = pci_config_readw(0, PCI_DEVICE_ID);
74 u16 sv = pci_config_readw(0, PCI_SUBSYSTEM_VENDOR_ID);
75 u16 sd = pci_config_readw(0, PCI_SUBSYSTEM_ID);
77 if (sv != 0x1af4 || /* Red Hat, Inc */
78 sd != 0x1100) /* Qemu virtual machine */
81 PlatformRunningOn |= PF_QEMU;
84 dprintf(1, "Running on QEMU (i440fx)\n");
87 dprintf(1, "Running on QEMU (q35)\n");
90 dprintf(1, "Running on QEMU (unknown nb: %04x:%04x)\n", v, d);
104 if (runningOnXen()) {
105 xen_ramsize_preinit();
109 if (!runningOnQEMU()) {
110 dprintf(1, "Warning: No QEMU Northbridge found (isapc?)\n");
111 PlatformRunningOn |= PF_QEMU;
115 // On emulators, get memory size from nvram.
116 u32 rs = ((rtc_read(CMOS_MEM_EXTMEM2_LOW) << 16)
117 | (rtc_read(CMOS_MEM_EXTMEM2_HIGH) << 24));
119 rs += 16 * 1024 * 1024;
121 rs = (((rtc_read(CMOS_MEM_EXTMEM_LOW) << 10)
122 | (rtc_read(CMOS_MEM_EXTMEM_HIGH) << 18))
125 e820_add(0, rs, E820_RAM);
127 /* reserve 256KB BIOS area at the end of 4 GB */
128 e820_add(0xfffc0000, 256*1024, E820_RESERVED);
130 dprintf(1, "RamSize: 0x%08x [cmos]\n", RamSize);
134 qemu_platform_setup(void)
139 if (runningOnXen()) {
141 xen_hypercall_setup();
142 xen_biostable_setup();
151 // Initialize mtrr and smp
155 // Create bios tables
160 if (CONFIG_FW_ROMFILE_LOAD) {
163 dprintf(3, "load ACPI tables\n");
165 loader_err = romfile_loader_execute("etc/table-loader");
167 RsdpAddr = find_acpi_rsdp();
172 /* If present, loader should have installed an RSDP.
173 * Not installed? We might still be able to continue
174 * using the builtin RSDP.
177 warn_internalerror();
184 /****************************************************************
185 * QEMU firmware config (fw_cfg) interface
186 ****************************************************************/
188 // List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
189 // should be passed via the fw_cfg "file" interface.)
190 #define QEMU_CFG_SIGNATURE 0x00
191 #define QEMU_CFG_ID 0x01
192 #define QEMU_CFG_UUID 0x02
193 #define QEMU_CFG_NUMA 0x0d
194 #define QEMU_CFG_BOOT_MENU 0x0e
195 #define QEMU_CFG_MAX_CPUS 0x0f
196 #define QEMU_CFG_FILE_DIR 0x19
197 #define QEMU_CFG_ARCH_LOCAL 0x8000
198 #define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
199 #define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
200 #define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
201 #define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
204 qemu_cfg_select(u16 f)
206 outw(f, PORT_QEMU_CFG_CTL);
210 qemu_cfg_dma_transfer(void *address, u32 length, u32 control)
212 QemuCfgDmaAccess access;
214 access.address = cpu_to_be64((u64)(u32)address);
215 access.length = cpu_to_be32(length);
216 access.control = cpu_to_be32(control);
220 outl(cpu_to_be32((u32)&access), PORT_QEMU_CFG_DMA_ADDR_LOW);
222 while(be32_to_cpu(access.control) & ~QEMU_CFG_DMA_CTL_ERROR) {
228 qemu_cfg_read(void *buf, int len)
234 if (qemu_cfg_dma_enabled()) {
235 qemu_cfg_dma_transfer(buf, len, QEMU_CFG_DMA_CTL_READ);
237 insb(PORT_QEMU_CFG_DATA, buf, len);
242 qemu_cfg_skip(int len)
248 if (qemu_cfg_dma_enabled()) {
249 qemu_cfg_dma_transfer(0, len, QEMU_CFG_DMA_CTL_SKIP);
252 inb(PORT_QEMU_CFG_DATA);
257 qemu_cfg_read_entry(void *buf, int e, int len)
259 if (qemu_cfg_dma_enabled()) {
260 u32 control = (e << 16) | QEMU_CFG_DMA_CTL_SELECT
261 | QEMU_CFG_DMA_CTL_READ;
262 qemu_cfg_dma_transfer(buf, len, control);
265 qemu_cfg_read(buf, len);
269 struct qemu_romfile_s {
270 struct romfile_s file;
275 qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
277 if (file->size > maxlen)
279 struct qemu_romfile_s *qfile;
280 qfile = container_of(file, struct qemu_romfile_s, file);
281 if (qfile->skip == 0) {
282 /* Do it in one transfer */
283 qemu_cfg_read_entry(dst, qfile->select, file->size);
285 qemu_cfg_select(qfile->select);
286 qemu_cfg_skip(qfile->skip);
287 qemu_cfg_read(dst, file->size);
293 qemu_romfile_add(char *name, int select, int skip, int size)
295 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
300 memset(qfile, 0, sizeof(*qfile));
301 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
302 qfile->file.size = size;
303 qfile->select = select;
305 qfile->file.copy = qemu_cfg_read_file;
306 romfile_add(&qfile->file);
309 struct e820_reservation {
315 #define SMBIOS_FIELD_ENTRY 0
316 #define SMBIOS_TABLE_ENTRY 1
318 struct qemu_smbios_header {
328 struct e820_reservation *table;
334 // "etc/e820" has both ram and reservations
335 table = romfile_loadfile("etc/e820", &size);
337 for (i = 0; i < size / sizeof(struct e820_reservation); i++) {
338 switch (table[i].type) {
340 dprintf(1, "RamBlock: addr 0x%016llx len 0x%016llx [e820]\n",
341 table[i].address, table[i].length);
342 if (table[i].address < RamSize)
343 // ignore, preinit got it from cmos already and
344 // adding this again would ruin any reservations
347 if (table[i].address < 0x100000000LL) {
348 // below 4g -- adjust RamSize to mark highest lowram addr
349 if (RamSize < table[i].address + table[i].length)
350 RamSize = table[i].address + table[i].length;
352 // above 4g -- adjust RamSizeOver4G to mark highest ram addr
353 if (0x100000000LL + RamSizeOver4G < table[i].address + table[i].length)
354 RamSizeOver4G = table[i].address + table[i].length - 0x100000000LL;
358 e820_add(table[i].address, table[i].length, table[i].type);
362 * Qemu 1.7 uses RAM + RESERVED only. Ignore
363 * everything else, so we have the option to
364 * extend this in the future without breakage.
372 // QEMU_CFG_E820_TABLE has reservations only
374 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
376 struct e820_reservation entry;
378 for (i = 0; i < count32; i++) {
379 qemu_cfg_read(&entry, sizeof(entry));
380 e820_add(entry.address, entry.length, entry.type);
382 } else if (runningOnKVM()) {
383 // Backwards compatibility - provide hard coded range.
384 // 4 pages before the bios, 3 pages for vmx tss pages, the
385 // other page for EPT real mode pagetable
386 e820_add(0xfffbc000, 4*4096, E820_RESERVED);
389 // Check for memory over 4Gig in cmos
390 u64 high = ((rtc_read(CMOS_MEM_HIGHMEM_LOW) << 16)
391 | ((u32)rtc_read(CMOS_MEM_HIGHMEM_MID) << 24)
392 | ((u64)rtc_read(CMOS_MEM_HIGHMEM_HIGH) << 32));
393 RamSizeOver4G = high;
394 e820_add(0x100000000ull, high, E820_RAM);
395 dprintf(1, "RamSizeOver4G: 0x%016llx [cmos]\n", RamSizeOver4G);
398 // Populate romfile entries for legacy fw_cfg ports (that predate the
399 // "file" interface).
401 qemu_cfg_legacy(void)
406 // Misc config items.
407 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
408 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
409 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
413 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
414 int max_cpu = romfile_loadint("etc/max-cpus", 0);
415 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
416 , max_cpu*sizeof(u64));
417 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
418 , sizeof(numacount) + max_cpu*sizeof(u64)
419 , numacount*sizeof(u64));
424 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
425 int i, offset = sizeof(cnt);
426 for (i = 0; i < cnt; i++) {
428 qemu_cfg_read(&len, sizeof(len));
429 offset += sizeof(len);
430 snprintf(name, sizeof(name), "acpi/table%d", i);
431 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
437 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
438 offset = sizeof(cnt);
439 for (i = 0; i < cnt; i++) {
440 struct qemu_smbios_header header;
441 qemu_cfg_read(&header, sizeof(header));
442 if (header.headertype == SMBIOS_FIELD_ENTRY) {
443 snprintf(name, sizeof(name), "smbios/field%d-%d"
444 , header.tabletype, header.fieldoffset);
445 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
446 , offset + sizeof(header)
447 , header.length - sizeof(header));
449 snprintf(name, sizeof(name), "smbios/table%d-%d"
450 , header.tabletype, i);
451 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
452 , offset + 3, header.length - 3);
454 qemu_cfg_skip(header.length - sizeof(header));
455 offset += header.length;
460 u32 size; /* file size */
461 u16 select; /* write this to 0x510 to read it */
466 void qemu_cfg_init(void)
468 if (!runningOnQEMU())
471 // Detect fw_cfg interface.
472 qemu_cfg_select(QEMU_CFG_SIGNATURE);
475 for (i = 0; i < 4; i++)
476 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
479 dprintf(1, "Found QEMU fw_cfg\n");
481 // Detect DMA interface.
483 qemu_cfg_read_entry(&id, QEMU_CFG_ID, sizeof(id));
485 if (id & QEMU_CFG_VERSION_DMA) {
486 dprintf(1, "QEMU fw_cfg DMA interface supported\n");
490 // Populate romfiles for legacy fw_cfg entries
493 // Load files found in the fw_cfg file directory
495 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
496 count = be32_to_cpu(count);
498 for (e = 0; e < count; e++) {
499 struct QemuCfgFile qfile;
500 qemu_cfg_read(&qfile, sizeof(qfile));
501 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
502 , 0, be32_to_cpu(qfile.size));
507 if (romfile_find("etc/table-loader")) {
508 acpi_pm_base = 0x0600;
509 dprintf(1, "Moving pm_base to 0x%x\n", acpi_pm_base);