Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 1 | // Paravirtualization support. |
| 2 | // |
| 3 | // Copyright (C) 2009 Red Hat Inc. |
| 4 | // |
| 5 | // Authors: |
| 6 | // Gleb Natapov <gnatapov@redhat.com> |
| 7 | // |
| 8 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 9 | |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 10 | #include "config.h" // CONFIG_QEMU |
Kevin O'Connor | b306459 | 2012-08-14 21:20:10 -0400 | [diff] [blame] | 11 | #include "util.h" // dprintf |
| 12 | #include "byteorder.h" // be32_to_cpu |
Kevin O'Connor | 01a8520 | 2009-10-18 09:49:59 -0400 | [diff] [blame] | 13 | #include "ioport.h" // outw |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 14 | #include "paravirt.h" // qemu_cfg_preinit |
Kevin O'Connor | de9e705 | 2013-02-09 19:09:20 -0500 | [diff] [blame] | 15 | #include "smbios.h" // smbios_setup |
Kevin O'Connor | 8ed2e53 | 2013-01-21 02:32:48 -0500 | [diff] [blame] | 16 | #include "memmap.h" // add_e820 |
| 17 | #include "cmos.h" // CMOS_* |
| 18 | #include "acpi.h" // acpi_setup |
| 19 | #include "mptable.h" // mptable_setup |
| 20 | #include "pci.h" // create_pirtable |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 21 | |
Kevin O'Connor | 4edda08 | 2013-02-09 13:21:08 -0500 | [diff] [blame] | 22 | struct e820_reservation { |
| 23 | u64 address; |
| 24 | u64 length; |
| 25 | u32 type; |
| 26 | }; |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 27 | |
Kevin O'Connor | 7b5bc50 | 2013-02-09 13:07:23 -0500 | [diff] [blame] | 28 | /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It |
| 29 | * should be used to determine that a VM is running under KVM. |
| 30 | */ |
| 31 | #define KVM_CPUID_SIGNATURE 0x40000000 |
| 32 | |
| 33 | static void kvm_preinit(void) |
| 34 | { |
| 35 | if (!CONFIG_QEMU) |
| 36 | return; |
| 37 | unsigned int eax, ebx, ecx, edx; |
| 38 | char signature[13]; |
| 39 | |
| 40 | cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx); |
| 41 | memcpy(signature + 0, &ebx, 4); |
| 42 | memcpy(signature + 4, &ecx, 4); |
| 43 | memcpy(signature + 8, &edx, 4); |
| 44 | signature[12] = 0; |
| 45 | |
| 46 | if (strcmp(signature, "KVMKVMKVM") == 0) { |
| 47 | dprintf(1, "Running on KVM\n"); |
| 48 | PlatformRunningOn |= PF_KVM; |
| 49 | } |
| 50 | } |
| 51 | |
Kevin O'Connor | 8ed2e53 | 2013-01-21 02:32:48 -0500 | [diff] [blame] | 52 | void |
| 53 | qemu_ramsize_preinit(void) |
| 54 | { |
Kevin O'Connor | 02313b2 | 2013-02-07 22:42:25 -0500 | [diff] [blame] | 55 | if (!CONFIG_QEMU) |
| 56 | return; |
| 57 | |
| 58 | PlatformRunningOn = PF_QEMU; |
Kevin O'Connor | 7b5bc50 | 2013-02-09 13:07:23 -0500 | [diff] [blame] | 59 | kvm_preinit(); |
Kevin O'Connor | 02313b2 | 2013-02-07 22:42:25 -0500 | [diff] [blame] | 60 | |
Kevin O'Connor | 8ed2e53 | 2013-01-21 02:32:48 -0500 | [diff] [blame] | 61 | // On emulators, get memory size from nvram. |
| 62 | u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16) |
| 63 | | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24)); |
| 64 | if (rs) |
| 65 | rs += 16 * 1024 * 1024; |
| 66 | else |
| 67 | rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10) |
| 68 | | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18)) |
| 69 | + 1 * 1024 * 1024); |
| 70 | RamSize = rs; |
| 71 | add_e820(0, rs, E820_RAM); |
| 72 | |
| 73 | // Check for memory over 4Gig |
| 74 | u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16) |
| 75 | | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24) |
| 76 | | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32)); |
| 77 | RamSizeOver4G = high; |
| 78 | add_e820(0x100000000ull, high, E820_RAM); |
| 79 | |
| 80 | /* reserve 256KB BIOS area at the end of 4 GB */ |
| 81 | add_e820(0xfffc0000, 256*1024, E820_RESERVED); |
| 82 | |
| 83 | u32 count = qemu_cfg_e820_entries(); |
| 84 | if (count) { |
| 85 | struct e820_reservation entry; |
| 86 | int i; |
| 87 | |
| 88 | for (i = 0; i < count; i++) { |
| 89 | qemu_cfg_e820_load_next(&entry); |
| 90 | add_e820(entry.address, entry.length, entry.type); |
| 91 | } |
Kevin O'Connor | 7b5bc50 | 2013-02-09 13:07:23 -0500 | [diff] [blame] | 92 | } else if (runningOnKVM()) { |
Kevin O'Connor | 8ed2e53 | 2013-01-21 02:32:48 -0500 | [diff] [blame] | 93 | // Backwards compatibility - provide hard coded range. |
| 94 | // 4 pages before the bios, 3 pages for vmx tss pages, the |
| 95 | // other page for EPT real mode pagetable |
| 96 | add_e820(0xfffbc000, 4*4096, E820_RESERVED); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | qemu_biostable_setup(void) |
| 102 | { |
| 103 | pirtable_setup(); |
| 104 | mptable_setup(); |
| 105 | smbios_setup(); |
| 106 | acpi_setup(); |
| 107 | } |
| 108 | |
Kevin O'Connor | 4edda08 | 2013-02-09 13:21:08 -0500 | [diff] [blame] | 109 | |
| 110 | /**************************************************************** |
| 111 | * QEMU firmware config (fw_cfg) interface |
| 112 | ****************************************************************/ |
| 113 | |
| 114 | int qemu_cfg_present; |
| 115 | |
| 116 | #define QEMU_CFG_SIGNATURE 0x00 |
| 117 | #define QEMU_CFG_ID 0x01 |
| 118 | #define QEMU_CFG_UUID 0x02 |
| 119 | #define QEMU_CFG_NUMA 0x0d |
| 120 | #define QEMU_CFG_BOOT_MENU 0x0e |
| 121 | #define QEMU_CFG_MAX_CPUS 0x0f |
| 122 | #define QEMU_CFG_FILE_DIR 0x19 |
| 123 | #define QEMU_CFG_ARCH_LOCAL 0x8000 |
| 124 | #define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0) |
| 125 | #define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1) |
| 126 | #define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2) |
| 127 | #define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3) |
| 128 | |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 129 | static void |
| 130 | qemu_cfg_select(u16 f) |
| 131 | { |
| 132 | outw(f, PORT_QEMU_CFG_CTL); |
| 133 | } |
| 134 | |
| 135 | static void |
Kevin O'Connor | c40e3fa | 2013-02-09 14:55:30 -0500 | [diff] [blame] | 136 | qemu_cfg_read(void *buf, int len) |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 137 | { |
Kevin O'Connor | 6039fc5 | 2010-08-25 21:43:19 -0400 | [diff] [blame] | 138 | insb(PORT_QEMU_CFG_DATA, buf, len); |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void |
Kevin O'Connor | 4e4b410 | 2009-10-08 21:21:59 -0400 | [diff] [blame] | 142 | qemu_cfg_skip(int len) |
| 143 | { |
| 144 | while (len--) |
| 145 | inb(PORT_QEMU_CFG_DATA); |
| 146 | } |
| 147 | |
| 148 | static void |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 149 | qemu_cfg_read_entry(void *buf, int e, int len) |
| 150 | { |
| 151 | qemu_cfg_select(e); |
| 152 | qemu_cfg_read(buf, len); |
| 153 | } |
| 154 | |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 155 | void qemu_cfg_preinit(void) |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 156 | { |
| 157 | char *sig = "QEMU"; |
| 158 | int i; |
| 159 | |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 160 | if (!CONFIG_QEMU) |
Kevin O'Connor | e7cc764 | 2009-10-04 10:05:16 -0400 | [diff] [blame] | 161 | return; |
| 162 | |
| 163 | qemu_cfg_present = 1; |
| 164 | |
| 165 | qemu_cfg_select(QEMU_CFG_SIGNATURE); |
| 166 | |
| 167 | for (i = 0; i < 4; i++) |
| 168 | if (inb(PORT_QEMU_CFG_DATA) != sig[i]) { |
| 169 | qemu_cfg_present = 0; |
| 170 | break; |
| 171 | } |
| 172 | dprintf(4, "qemu_cfg_present=%d\n", qemu_cfg_present); |
| 173 | } |
| 174 | |
Jes Sorensen | 0360e8e | 2010-02-16 09:46:08 +0100 | [diff] [blame] | 175 | u32 qemu_cfg_e820_entries(void) |
| 176 | { |
| 177 | u32 cnt; |
| 178 | |
| 179 | if (!qemu_cfg_present) |
| 180 | return 0; |
| 181 | |
| 182 | qemu_cfg_read_entry(&cnt, QEMU_CFG_E820_TABLE, sizeof(cnt)); |
| 183 | return cnt; |
| 184 | } |
| 185 | |
| 186 | void* qemu_cfg_e820_load_next(void *addr) |
| 187 | { |
| 188 | qemu_cfg_read(addr, sizeof(struct e820_reservation)); |
| 189 | return addr; |
| 190 | } |
| 191 | |
Kevin O'Connor | c40e3fa | 2013-02-09 14:55:30 -0500 | [diff] [blame] | 192 | static int |
| 193 | qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen) |
Gerd Hoffmann | c4c9fae | 2009-12-18 12:16:04 +0100 | [diff] [blame] | 194 | { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 195 | if (file->size > maxlen) |
| 196 | return -1; |
Kevin O'Connor | c40e3fa | 2013-02-09 14:55:30 -0500 | [diff] [blame] | 197 | qemu_cfg_select(file->id); |
| 198 | qemu_cfg_skip(file->rawsize); |
| 199 | qemu_cfg_read(dst, file->size); |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 200 | return file->size; |
| 201 | } |
| 202 | |
Kevin O'Connor | c40e3fa | 2013-02-09 14:55:30 -0500 | [diff] [blame] | 203 | static void |
| 204 | qemu_romfile_add(char *name, int select, int skip, int size) |
| 205 | { |
| 206 | struct romfile_s *file = malloc_tmp(sizeof(*file)); |
| 207 | if (!file) { |
| 208 | warn_noalloc(); |
| 209 | return; |
| 210 | } |
| 211 | memset(file, 0, sizeof(*file)); |
| 212 | strtcpy(file->name, name, sizeof(file->name)); |
| 213 | file->id = select; |
| 214 | file->rawsize = skip; // Use rawsize to indicate skip length. |
| 215 | file->size = size; |
| 216 | file->copy = qemu_cfg_read_file; |
| 217 | romfile_add(file); |
| 218 | } |
| 219 | |
Kevin O'Connor | de9e705 | 2013-02-09 19:09:20 -0500 | [diff] [blame] | 220 | #define SMBIOS_FIELD_ENTRY 0 |
| 221 | #define SMBIOS_TABLE_ENTRY 1 |
| 222 | |
| 223 | struct qemu_smbios_header { |
| 224 | u16 length; |
| 225 | u8 headertype; |
| 226 | u8 tabletype; |
| 227 | u16 fieldoffset; |
| 228 | } PACKED; |
| 229 | |
Kevin O'Connor | 188d994 | 2013-02-09 15:24:08 -0500 | [diff] [blame] | 230 | // Populate romfile entries for legacy fw_cfg ports (that predate the |
| 231 | // "file" interface). |
| 232 | static void |
| 233 | qemu_cfg_legacy(void) |
| 234 | { |
Kevin O'Connor | 56c5089 | 2013-02-09 19:25:51 -0500 | [diff] [blame] | 235 | // Misc config items. |
| 236 | qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2); |
| 237 | qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1); |
| 238 | qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2); |
| 239 | |
Kevin O'Connor | f9e4e37 | 2013-02-09 19:45:45 -0500 | [diff] [blame^] | 240 | // NUMA data |
| 241 | u64 numacount; |
| 242 | qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount)); |
| 243 | numacount += romfile_loadint("etc/max-cpus", 0); |
| 244 | qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA, sizeof(numacount) |
| 245 | , numacount*sizeof(u64)); |
| 246 | |
Kevin O'Connor | 188d994 | 2013-02-09 15:24:08 -0500 | [diff] [blame] | 247 | // ACPI tables |
| 248 | char name[128]; |
| 249 | u16 cnt; |
| 250 | qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt)); |
| 251 | int i, offset = sizeof(cnt); |
| 252 | for (i = 0; i < cnt; i++) { |
| 253 | u16 len; |
| 254 | qemu_cfg_read(&len, sizeof(len)); |
| 255 | offset += sizeof(len); |
| 256 | snprintf(name, sizeof(name), "acpi/table%d", i); |
| 257 | qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len); |
| 258 | qemu_cfg_skip(len); |
| 259 | offset += len; |
| 260 | } |
Kevin O'Connor | de9e705 | 2013-02-09 19:09:20 -0500 | [diff] [blame] | 261 | |
| 262 | // SMBIOS info |
| 263 | qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt)); |
| 264 | offset = sizeof(cnt); |
| 265 | for (i = 0; i < cnt; i++) { |
| 266 | struct qemu_smbios_header header; |
| 267 | qemu_cfg_read(&header, sizeof(header)); |
| 268 | if (header.headertype == SMBIOS_FIELD_ENTRY) { |
| 269 | snprintf(name, sizeof(name), "smbios/field%d-%d" |
| 270 | , header.tabletype, header.fieldoffset); |
| 271 | qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES |
| 272 | , offset + sizeof(header) |
| 273 | , header.length - sizeof(header)); |
| 274 | } else { |
| 275 | snprintf(name, sizeof(name), "smbios/table%d-%d" |
| 276 | , header.tabletype, i); |
| 277 | qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES |
| 278 | , offset + 3, header.length - 3); |
| 279 | } |
| 280 | qemu_cfg_skip(header.length - sizeof(header)); |
| 281 | offset += header.length; |
| 282 | } |
Kevin O'Connor | 188d994 | 2013-02-09 15:24:08 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 285 | struct QemuCfgFile { |
| 286 | u32 size; /* file size */ |
| 287 | u16 select; /* write this to 0x510 to read it */ |
| 288 | u16 reserved; |
| 289 | char name[56]; |
| 290 | }; |
| 291 | |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 292 | void qemu_romfile_init(void) |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 293 | { |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 294 | if (!CONFIG_QEMU || !qemu_cfg_present) |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 295 | return; |
Gerd Hoffmann | c4c9fae | 2009-12-18 12:16:04 +0100 | [diff] [blame] | 296 | |
Kevin O'Connor | 188d994 | 2013-02-09 15:24:08 -0500 | [diff] [blame] | 297 | // Populate romfiles for legacy fw_cfg entries |
| 298 | qemu_cfg_legacy(); |
| 299 | |
| 300 | // Load files found in the fw_cfg file directory |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 301 | u32 count; |
Gerd Hoffmann | c4c9fae | 2009-12-18 12:16:04 +0100 | [diff] [blame] | 302 | qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count)); |
Kevin O'Connor | b306459 | 2012-08-14 21:20:10 -0400 | [diff] [blame] | 303 | count = be32_to_cpu(count); |
Kevin O'Connor | e230426 | 2010-06-13 16:05:17 -0400 | [diff] [blame] | 304 | u32 e; |
| 305 | for (e = 0; e < count; e++) { |
Kevin O'Connor | 59d6ca5 | 2012-05-31 00:20:55 -0400 | [diff] [blame] | 306 | struct QemuCfgFile qfile; |
Kevin O'Connor | c40e3fa | 2013-02-09 14:55:30 -0500 | [diff] [blame] | 307 | qemu_cfg_read(&qfile, sizeof(qfile)); |
| 308 | qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select) |
| 309 | , 0, be32_to_cpu(qfile.size)); |
Gerd Hoffmann | c4c9fae | 2009-12-18 12:16:04 +0100 | [diff] [blame] | 310 | } |
Kevin O'Connor | 8b56578 | 2011-07-05 20:32:44 -0400 | [diff] [blame] | 311 | } |