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 "hw/pci.h" // create_pirtable
14 #include "hw/pci_regs.h" // PCI_DEVICE_ID
15 #include "hw/rtc.h" // CMOS_*
16 #include "malloc.h" // malloc_tmp
17 #include "memmap.h" // add_e820
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
27 // Amount of continuous ram under 4Gig
29 // Amount of continuous ram >4Gig
31 // Type of emulator platform.
32 int PlatformRunningOn VARFSEG;
34 /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
35 * should be used to determine that a VM is running under KVM.
37 #define KVM_CPUID_SIGNATURE 0x40000000
39 static void kvm_detect(void)
41 unsigned int eax, ebx, ecx, edx;
44 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
45 memcpy(signature + 0, &ebx, 4);
46 memcpy(signature + 4, &ecx, 4);
47 memcpy(signature + 8, &edx, 4);
50 if (strcmp(signature, "KVMKVMKVM") == 0) {
51 dprintf(1, "Running on KVM\n");
52 PlatformRunningOn |= PF_KVM;
56 static void qemu_detect(void)
58 if (!CONFIG_QEMU_HARDWARE)
61 // check northbridge @ 00:00.0
62 u16 v = pci_config_readw(0, PCI_VENDOR_ID);
63 if (v == 0x0000 || v == 0xffff)
65 u16 d = pci_config_readw(0, PCI_DEVICE_ID);
66 u16 sv = pci_config_readw(0, PCI_SUBSYSTEM_VENDOR_ID);
67 u16 sd = pci_config_readw(0, PCI_SUBSYSTEM_ID);
69 if (sv != 0x1af4 || /* Red Hat, Inc */
70 sd != 0x1100) /* Qemu virtual machine */
73 PlatformRunningOn |= PF_QEMU;
76 dprintf(1, "Running on QEMU (i440fx)\n");
79 dprintf(1, "Running on QEMU (q35)\n");
82 dprintf(1, "Running on QEMU (unknown nb: %04x:%04x)\n", v, d);
97 xen_ramsize_preinit();
101 if (!runningOnQEMU()) {
102 dprintf(1, "Warning: No QEMU Northbridge found (isapc?)\n");
103 PlatformRunningOn |= PF_QEMU;
107 // On emulators, get memory size from nvram.
108 u32 rs = ((rtc_read(CMOS_MEM_EXTMEM2_LOW) << 16)
109 | (rtc_read(CMOS_MEM_EXTMEM2_HIGH) << 24));
111 rs += 16 * 1024 * 1024;
113 rs = (((rtc_read(CMOS_MEM_EXTMEM_LOW) << 10)
114 | (rtc_read(CMOS_MEM_EXTMEM_HIGH) << 18))
117 add_e820(0, rs, E820_RAM);
119 /* reserve 256KB BIOS area at the end of 4 GB */
120 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
122 dprintf(1, "RamSize: 0x%08x [cmos]\n", RamSize);
126 qemu_platform_setup(void)
131 if (runningOnXen()) {
133 xen_hypercall_setup();
134 xen_biostable_setup();
143 // Initialize mtrr and smp
147 // Create bios tables
152 if (CONFIG_FW_ROMFILE_LOAD) {
155 dprintf(3, "load ACPI tables\n");
157 loader_err = romfile_loader_execute("etc/table-loader");
159 RsdpAddr = find_acpi_rsdp();
164 /* If present, loader should have installed an RSDP.
165 * Not installed? We might still be able to continue
166 * using the builtin RSDP.
169 warn_internalerror();
176 /****************************************************************
177 * QEMU firmware config (fw_cfg) interface
178 ****************************************************************/
180 // List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
181 // should be passed via the fw_cfg "file" interface.)
182 #define QEMU_CFG_SIGNATURE 0x00
183 #define QEMU_CFG_ID 0x01
184 #define QEMU_CFG_UUID 0x02
185 #define QEMU_CFG_NUMA 0x0d
186 #define QEMU_CFG_BOOT_MENU 0x0e
187 #define QEMU_CFG_MAX_CPUS 0x0f
188 #define QEMU_CFG_FILE_DIR 0x19
189 #define QEMU_CFG_ARCH_LOCAL 0x8000
190 #define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
191 #define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
192 #define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
193 #define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
196 qemu_cfg_select(u16 f)
198 outw(f, PORT_QEMU_CFG_CTL);
202 qemu_cfg_read(void *buf, int len)
204 insb(PORT_QEMU_CFG_DATA, buf, len);
208 qemu_cfg_skip(int len)
211 inb(PORT_QEMU_CFG_DATA);
215 qemu_cfg_read_entry(void *buf, int e, int len)
218 qemu_cfg_read(buf, len);
221 struct qemu_romfile_s {
222 struct romfile_s file;
227 qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
229 if (file->size > maxlen)
231 struct qemu_romfile_s *qfile;
232 qfile = container_of(file, struct qemu_romfile_s, file);
233 qemu_cfg_select(qfile->select);
234 qemu_cfg_skip(qfile->skip);
235 qemu_cfg_read(dst, file->size);
240 qemu_romfile_add(char *name, int select, int skip, int size)
242 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
247 memset(qfile, 0, sizeof(*qfile));
248 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
249 qfile->file.size = size;
250 qfile->select = select;
252 qfile->file.copy = qemu_cfg_read_file;
253 romfile_add(&qfile->file);
256 struct e820_reservation {
262 #define SMBIOS_FIELD_ENTRY 0
263 #define SMBIOS_TABLE_ENTRY 1
265 struct qemu_smbios_header {
275 struct e820_reservation *table;
281 // "etc/e820" has both ram and reservations
282 table = romfile_loadfile("etc/e820", &size);
284 for (i = 0; i < size / sizeof(struct e820_reservation); i++) {
285 switch (table[i].type) {
287 dprintf(1, "RamBlock: addr 0x%016llx len 0x%016llx [e820]\n",
288 table[i].address, table[i].length);
289 if (table[i].address < RamSize)
290 // ignore, preinit got it from cmos already and
291 // adding this again would ruin any reservations
294 if (table[i].address < 0x100000000LL) {
295 // below 4g -- adjust RamSize to mark highest lowram addr
296 if (RamSize < table[i].address + table[i].length)
297 RamSize = table[i].address + table[i].length;
299 // above 4g -- adjust RamSizeOver4G to mark highest ram addr
300 if (0x100000000LL + RamSizeOver4G < table[i].address + table[i].length)
301 RamSizeOver4G = table[i].address + table[i].length - 0x100000000LL;
305 add_e820(table[i].address, table[i].length, table[i].type);
309 * Qemu 1.7 uses RAM + RESERVED only. Ignore
310 * everything else, so we have the option to
311 * extend this in the future without breakage.
319 // QEMU_CFG_E820_TABLE has reservations only
321 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
323 struct e820_reservation entry;
325 for (i = 0; i < count32; i++) {
326 qemu_cfg_read(&entry, sizeof(entry));
327 add_e820(entry.address, entry.length, entry.type);
329 } else if (runningOnKVM()) {
330 // Backwards compatibility - provide hard coded range.
331 // 4 pages before the bios, 3 pages for vmx tss pages, the
332 // other page for EPT real mode pagetable
333 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
336 // Check for memory over 4Gig in cmos
337 u64 high = ((rtc_read(CMOS_MEM_HIGHMEM_LOW) << 16)
338 | ((u32)rtc_read(CMOS_MEM_HIGHMEM_MID) << 24)
339 | ((u64)rtc_read(CMOS_MEM_HIGHMEM_HIGH) << 32));
340 RamSizeOver4G = high;
341 add_e820(0x100000000ull, high, E820_RAM);
342 dprintf(1, "RamSizeOver4G: 0x%016llx [cmos]\n", RamSizeOver4G);
345 // Populate romfile entries for legacy fw_cfg ports (that predate the
346 // "file" interface).
348 qemu_cfg_legacy(void)
353 // Misc config items.
354 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
355 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
356 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
360 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
361 int max_cpu = romfile_loadint("etc/max-cpus", 0);
362 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
363 , max_cpu*sizeof(u64));
364 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
365 , sizeof(numacount) + max_cpu*sizeof(u64)
366 , numacount*sizeof(u64));
371 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
372 int i, offset = sizeof(cnt);
373 for (i = 0; i < cnt; i++) {
375 qemu_cfg_read(&len, sizeof(len));
376 offset += sizeof(len);
377 snprintf(name, sizeof(name), "acpi/table%d", i);
378 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
384 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
385 offset = sizeof(cnt);
386 for (i = 0; i < cnt; i++) {
387 struct qemu_smbios_header header;
388 qemu_cfg_read(&header, sizeof(header));
389 if (header.headertype == SMBIOS_FIELD_ENTRY) {
390 snprintf(name, sizeof(name), "smbios/field%d-%d"
391 , header.tabletype, header.fieldoffset);
392 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
393 , offset + sizeof(header)
394 , header.length - sizeof(header));
396 snprintf(name, sizeof(name), "smbios/table%d-%d"
397 , header.tabletype, i);
398 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
399 , offset + 3, header.length - 3);
401 qemu_cfg_skip(header.length - sizeof(header));
402 offset += header.length;
407 u32 size; /* file size */
408 u16 select; /* write this to 0x510 to read it */
413 void qemu_cfg_init(void)
415 if (!runningOnQEMU())
418 // Detect fw_cfg interface.
419 qemu_cfg_select(QEMU_CFG_SIGNATURE);
422 for (i = 0; i < 4; i++)
423 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
425 dprintf(1, "Found QEMU fw_cfg\n");
427 // Populate romfiles for legacy fw_cfg entries
430 // Load files found in the fw_cfg file directory
432 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
433 count = be32_to_cpu(count);
435 for (e = 0; e < count; e++) {
436 struct QemuCfgFile qfile;
437 qemu_cfg_read(&qfile, sizeof(qfile));
438 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
439 , 0, be32_to_cpu(qfile.size));
444 if (romfile_find("etc/table-loader")) {
445 acpi_pm_base = 0x0600;
446 dprintf(1, "Moving pm_base to 0x%x\n", acpi_pm_base);