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