1 // Paravirtualization support.
2 //
3 // Copyright (C) 2013  Kevin O'Connor <kevin@koconnor.net>
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 
11 #include "byteorder.h" // be32_to_cpu
12 #include "config.h" // CONFIG_QEMU
13 #include "e820map.h" // e820_add
14 #include "hw/pci.h" // pci_config_readw
15 #include "hw/pcidevice.h" // pci_probe_devices
16 #include "hw/pci_regs.h" // PCI_DEVICE_ID
17 #include "hw/serialio.h" // PORT_SERIAL1
18 #include "hw/rtc.h" // CMOS_*
19 #include "malloc.h" // malloc_tmp
20 #include "output.h" // dprintf
21 #include "paravirt.h" // qemu_cfg_preinit
22 #include "romfile.h" // romfile_loadint
23 #include "romfile_loader.h" // romfile_loader_execute
24 #include "string.h" // memset
25 #include "util.h" // pci_setup
26 #include "x86.h" // cpuid
27 #include "xen.h" // xen_biostable_setup
28 #include "stacks.h" // yield
29 
30 // Amount of continuous ram under 4Gig
31 u32 RamSize;
32 // Amount of continuous ram >4Gig
33 u64 RamSizeOver4G;
34 // Type of emulator platform.
35 int PlatformRunningOn VARFSEG;
36 // cfg enabled
37 int cfg_enabled = 0;
38 // cfg_dma enabled
39 int cfg_dma_enabled = 0;
40 
qemu_cfg_enabled(void)41 inline int qemu_cfg_enabled(void)
42 {
43     return cfg_enabled;
44 }
45 
qemu_cfg_dma_enabled(void)46 inline int qemu_cfg_dma_enabled(void)
47 {
48     return cfg_dma_enabled;
49 }
50 
51 /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx.  It
52  * should be used to determine that a VM is running under KVM.
53  */
54 #define KVM_CPUID_SIGNATURE     0x40000000
55 
kvm_detect(void)56 static void kvm_detect(void)
57 {
58     unsigned int eax, ebx, ecx, edx;
59     char signature[13];
60 
61     cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
62     memcpy(signature + 0, &ebx, 4);
63     memcpy(signature + 4, &ecx, 4);
64     memcpy(signature + 8, &edx, 4);
65     signature[12] = 0;
66 
67     if (strcmp(signature, "KVMKVMKVM") == 0) {
68         dprintf(1, "Running on KVM\n");
69         PlatformRunningOn |= PF_KVM;
70     }
71 }
72 
qemu_detect(void)73 static void qemu_detect(void)
74 {
75     if (!CONFIG_QEMU_HARDWARE)
76         return;
77 
78     // Setup QEMU debug output port
79     qemu_debug_preinit();
80 
81     // check northbridge @ 00:00.0
82     u16 v = pci_config_readw(0, PCI_VENDOR_ID);
83     if (v == 0x0000 || v == 0xffff)
84         return;
85     u16 d = pci_config_readw(0, PCI_DEVICE_ID);
86     u16 sv = pci_config_readw(0, PCI_SUBSYSTEM_VENDOR_ID);
87     u16 sd = pci_config_readw(0, PCI_SUBSYSTEM_ID);
88 
89     if (sv != 0x1af4 || /* Red Hat, Inc */
90         sd != 0x1100)   /* Qemu virtual machine */
91         return;
92 
93     PlatformRunningOn |= PF_QEMU;
94     switch (d) {
95     case 0x1237:
96         dprintf(1, "Running on QEMU (i440fx)\n");
97         break;
98     case 0x29c0:
99         dprintf(1, "Running on QEMU (q35)\n");
100         break;
101     default:
102         dprintf(1, "Running on QEMU (unknown nb: %04x:%04x)\n", v, d);
103         break;
104     }
105     kvm_detect();
106 }
107 
108 void
qemu_preinit(void)109 qemu_preinit(void)
110 {
111     qemu_detect();
112 
113     if (!CONFIG_QEMU)
114         return;
115 
116     if (runningOnXen()) {
117         xen_ramsize_preinit();
118         return;
119     }
120 
121     if (!runningOnQEMU()) {
122         dprintf(1, "Warning: No QEMU Northbridge found (isapc?)\n");
123         PlatformRunningOn |= PF_QEMU;
124         kvm_detect();
125     }
126 
127     // On emulators, get memory size from nvram.
128     u32 rs = ((rtc_read(CMOS_MEM_EXTMEM2_LOW) << 16)
129               | (rtc_read(CMOS_MEM_EXTMEM2_HIGH) << 24));
130     if (rs)
131         rs += 16 * 1024 * 1024;
132     else
133         rs = (((rtc_read(CMOS_MEM_EXTMEM_LOW) << 10)
134                | (rtc_read(CMOS_MEM_EXTMEM_HIGH) << 18))
135               + 1 * 1024 * 1024);
136     if (CONFIG_X86)
137         RamSize = rs;
138     e820_add(0, rs, E820_RAM);
139 
140     /* reserve 256KB BIOS area at the end of 4 GB */
141     e820_add(0xfffc0000, 256*1024, E820_RESERVED);
142 
143     dprintf(1, "RamSize: 0x%08x [cmos]\n", RamSize);
144 }
145 
146 #define MSR_IA32_FEATURE_CONTROL 0x0000003a
147 
msr_feature_control_setup(void)148 static void msr_feature_control_setup(void)
149 {
150     u64 feature_control_bits = romfile_loadint("etc/msr_feature_control", 0);
151     if (feature_control_bits)
152         wrmsr_smp(MSR_IA32_FEATURE_CONTROL, feature_control_bits);
153 }
154 
155 void
qemu_platform_setup(void)156 qemu_platform_setup(void)
157 {
158     if (!CONFIG_QEMU)
159         return;
160 
161     if (runningOnXen()) {
162         pci_probe_devices();
163         xen_hypercall_setup();
164         xen_biostable_setup();
165         return;
166     }
167 
168     // Initialize pci
169     pci_setup();
170     smm_device_setup();
171     smm_setup();
172 
173     // Initialize mtrr, msr_feature_control and smp
174     mtrr_setup();
175     msr_feature_control_setup();
176     smp_setup();
177 
178     // Create bios tables
179     if (MaxCountCPUs <= 255) {
180         pirtable_setup();
181         mptable_setup();
182     }
183     smbios_setup();
184 
185     if (CONFIG_FW_ROMFILE_LOAD) {
186         int loader_err;
187 
188         dprintf(3, "load ACPI tables\n");
189 
190         loader_err = romfile_loader_execute("etc/table-loader");
191 
192         RsdpAddr = find_acpi_rsdp();
193 
194         if (RsdpAddr)
195             return;
196 
197         /* If present, loader should have installed an RSDP.
198          * Not installed? We might still be able to continue
199          * using the builtin RSDP.
200          */
201         if (!loader_err)
202             warn_internalerror();
203     }
204 
205     acpi_setup();
206 }
207 
208 
209 /****************************************************************
210  * QEMU firmware config (fw_cfg) interface
211  ****************************************************************/
212 
213 // List of QEMU fw_cfg entries.  DO NOT ADD MORE.  (All new content
214 // should be passed via the fw_cfg "file" interface.)
215 #define QEMU_CFG_SIGNATURE              0x00
216 #define QEMU_CFG_ID                     0x01
217 #define QEMU_CFG_UUID                   0x02
218 #define QEMU_CFG_NOGRAPHIC              0x04
219 #define QEMU_CFG_NUMA                   0x0d
220 #define QEMU_CFG_BOOT_MENU              0x0e
221 #define QEMU_CFG_NB_CPUS                0x05
222 #define QEMU_CFG_MAX_CPUS               0x0f
223 #define QEMU_CFG_FILE_DIR               0x19
224 #define QEMU_CFG_ARCH_LOCAL             0x8000
225 #define QEMU_CFG_ACPI_TABLES            (QEMU_CFG_ARCH_LOCAL + 0)
226 #define QEMU_CFG_SMBIOS_ENTRIES         (QEMU_CFG_ARCH_LOCAL + 1)
227 #define QEMU_CFG_IRQ0_OVERRIDE          (QEMU_CFG_ARCH_LOCAL + 2)
228 #define QEMU_CFG_E820_TABLE             (QEMU_CFG_ARCH_LOCAL + 3)
229 
230 static void
qemu_cfg_select(u16 f)231 qemu_cfg_select(u16 f)
232 {
233     outw(f, PORT_QEMU_CFG_CTL);
234 }
235 
236 static void
qemu_cfg_dma_transfer(void * address,u32 length,u32 control)237 qemu_cfg_dma_transfer(void *address, u32 length, u32 control)
238 {
239     QemuCfgDmaAccess access;
240 
241     access.address = cpu_to_be64((u64)(u32)address);
242     access.length = cpu_to_be32(length);
243     access.control = cpu_to_be32(control);
244 
245     barrier();
246 
247     outl(cpu_to_be32((u32)&access), PORT_QEMU_CFG_DMA_ADDR_LOW);
248 
249     while(be32_to_cpu(access.control) & ~QEMU_CFG_DMA_CTL_ERROR) {
250         yield();
251     }
252 }
253 
254 static void
qemu_cfg_read(void * buf,int len)255 qemu_cfg_read(void *buf, int len)
256 {
257     if (len == 0) {
258         return;
259     }
260 
261     if (qemu_cfg_dma_enabled()) {
262         qemu_cfg_dma_transfer(buf, len, QEMU_CFG_DMA_CTL_READ);
263     } else {
264         insb(PORT_QEMU_CFG_DATA, buf, len);
265     }
266 }
267 
268 static void
qemu_cfg_write(void * buf,int len)269 qemu_cfg_write(void *buf, int len)
270 {
271     if (len == 0) {
272         return;
273     }
274 
275     if (qemu_cfg_dma_enabled()) {
276         qemu_cfg_dma_transfer(buf, len, QEMU_CFG_DMA_CTL_WRITE);
277     } else {
278         warn_internalerror();
279     }
280 }
281 
282 static void
qemu_cfg_skip(int len)283 qemu_cfg_skip(int len)
284 {
285     if (len == 0) {
286         return;
287     }
288 
289     if (qemu_cfg_dma_enabled()) {
290         qemu_cfg_dma_transfer(0, len, QEMU_CFG_DMA_CTL_SKIP);
291     } else {
292         while (len--)
293             inb(PORT_QEMU_CFG_DATA);
294     }
295 }
296 
297 static void
qemu_cfg_read_entry(void * buf,int e,int len)298 qemu_cfg_read_entry(void *buf, int e, int len)
299 {
300     if (qemu_cfg_dma_enabled()) {
301         u32 control = (e << 16) | QEMU_CFG_DMA_CTL_SELECT
302                         | QEMU_CFG_DMA_CTL_READ;
303         qemu_cfg_dma_transfer(buf, len, control);
304     } else {
305         qemu_cfg_select(e);
306         qemu_cfg_read(buf, len);
307     }
308 }
309 
310 static void
qemu_cfg_write_entry(void * buf,int e,int len)311 qemu_cfg_write_entry(void *buf, int e, int len)
312 {
313     if (qemu_cfg_dma_enabled()) {
314         u32 control = (e << 16) | QEMU_CFG_DMA_CTL_SELECT
315                         | QEMU_CFG_DMA_CTL_WRITE;
316         qemu_cfg_dma_transfer(buf, len, control);
317     } else {
318         warn_internalerror();
319     }
320 }
321 
322 struct qemu_romfile_s {
323     struct romfile_s file;
324     int select, skip;
325 };
326 
327 static int
qemu_cfg_read_file(struct romfile_s * file,void * dst,u32 maxlen)328 qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
329 {
330     if (file->size > maxlen)
331         return -1;
332     struct qemu_romfile_s *qfile;
333     qfile = container_of(file, struct qemu_romfile_s, file);
334     if (qfile->skip == 0) {
335         /* Do it in one transfer */
336         qemu_cfg_read_entry(dst, qfile->select, file->size);
337     } else {
338         qemu_cfg_select(qfile->select);
339         qemu_cfg_skip(qfile->skip);
340         qemu_cfg_read(dst, file->size);
341     }
342     return file->size;
343 }
344 
345 // Bare-bones function for writing a file knowing only its unique
346 // identifying key (select)
347 int
qemu_cfg_write_file_simple(void * src,u16 key,u32 offset,u32 len)348 qemu_cfg_write_file_simple(void *src, u16 key, u32 offset, u32 len)
349 {
350     if (offset == 0) {
351         /* Do it in one transfer */
352         qemu_cfg_write_entry(src, key, len);
353     } else {
354         qemu_cfg_select(key);
355         qemu_cfg_skip(offset);
356         qemu_cfg_write(src, len);
357     }
358     return len;
359 }
360 
361 int
qemu_cfg_write_file(void * src,struct romfile_s * file,u32 offset,u32 len)362 qemu_cfg_write_file(void *src, struct romfile_s *file, u32 offset, u32 len)
363 {
364     if ((offset + len) > file->size)
365         return -1;
366 
367     if (!qemu_cfg_dma_enabled() || (file->copy != qemu_cfg_read_file)) {
368         warn_internalerror();
369         return -1;
370     }
371     return qemu_cfg_write_file_simple(src, qemu_get_romfile_key(file),
372                                       offset, len);
373 }
374 
375 static void
qemu_romfile_add(char * name,int select,int skip,int size)376 qemu_romfile_add(char *name, int select, int skip, int size)
377 {
378     struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
379     if (!qfile) {
380         warn_noalloc();
381         return;
382     }
383     memset(qfile, 0, sizeof(*qfile));
384     strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
385     qfile->file.size = size;
386     qfile->select = select;
387     qfile->skip = skip;
388     qfile->file.copy = qemu_cfg_read_file;
389     romfile_add(&qfile->file);
390 }
391 
392 u16
qemu_get_romfile_key(struct romfile_s * file)393 qemu_get_romfile_key(struct romfile_s *file)
394 {
395     struct qemu_romfile_s *qfile;
396     if (file->copy != qemu_cfg_read_file) {
397         warn_internalerror();
398         return 0;
399     }
400     qfile = container_of(file, struct qemu_romfile_s, file);
401     return qfile->select;
402 }
403 
404 u16
qemu_get_present_cpus_count(void)405 qemu_get_present_cpus_count(void)
406 {
407     u16 smp_count = 0;
408     if (qemu_cfg_enabled()) {
409         qemu_cfg_read_entry(&smp_count, QEMU_CFG_NB_CPUS, sizeof(smp_count));
410     }
411     u16 cmos_cpu_count = rtc_read(CMOS_BIOS_SMP_COUNT) + 1;
412     if (smp_count < cmos_cpu_count) {
413         smp_count = cmos_cpu_count;
414     }
415     return smp_count;
416 }
417 
418 struct e820_reservation {
419     u64 address;
420     u64 length;
421     u32 type;
422 };
423 
424 #define SMBIOS_FIELD_ENTRY 0
425 #define SMBIOS_TABLE_ENTRY 1
426 
427 struct qemu_smbios_header {
428     u16 length;
429     u8 headertype;
430     u8 tabletype;
431     u16 fieldoffset;
432 } PACKED;
433 
434 static void
qemu_cfg_e820(void)435 qemu_cfg_e820(void)
436 {
437     struct e820_reservation *table;
438     int i, size;
439 
440     if (!CONFIG_QEMU)
441         return;
442 
443     // "etc/e820" has both ram and reservations
444     table = romfile_loadfile("etc/e820", &size);
445     if (table) {
446         for (i = 0; i < size / sizeof(struct e820_reservation); i++) {
447             switch (table[i].type) {
448             case E820_RAM:
449                 dprintf(1, "RamBlock: addr 0x%016llx len 0x%016llx [e820]\n",
450                         table[i].address, table[i].length);
451                 if (table[i].address < RamSize)
452                     // ignore, preinit got it from cmos already and
453                     // adding this again would ruin any reservations
454                     // done so far
455                     continue;
456                 if (table[i].address < 0x100000000LL) {
457                     // below 4g -- adjust RamSize to mark highest lowram addr
458                     if (RamSize < table[i].address + table[i].length)
459                         RamSize = table[i].address + table[i].length;
460                 } else {
461                     // above 4g -- adjust RamSizeOver4G to mark highest ram addr
462                     if (0x100000000LL + RamSizeOver4G < table[i].address + table[i].length)
463                         RamSizeOver4G = table[i].address + table[i].length - 0x100000000LL;
464                 }
465                 /* fall through */
466             case E820_RESERVED:
467                 e820_add(table[i].address, table[i].length, table[i].type);
468                 break;
469             default:
470                 /*
471                  * Qemu 1.7 uses RAM + RESERVED only.  Ignore
472                  * everything else, so we have the option to
473                  * extend this in the future without breakage.
474                  */
475                 break;
476             }
477         }
478         return;
479     }
480 
481     // QEMU_CFG_E820_TABLE has reservations only
482     u32 count32;
483     qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
484     if (count32) {
485         struct e820_reservation entry;
486         int i;
487         for (i = 0; i < count32; i++) {
488             qemu_cfg_read(&entry, sizeof(entry));
489             e820_add(entry.address, entry.length, entry.type);
490         }
491     } else if (runningOnKVM()) {
492         // Backwards compatibility - provide hard coded range.
493         // 4 pages before the bios, 3 pages for vmx tss pages, the
494         // other page for EPT real mode pagetable
495         e820_add(0xfffbc000, 4*4096, E820_RESERVED);
496     }
497 
498     // Check for memory over 4Gig in cmos
499     u64 high = ((rtc_read(CMOS_MEM_HIGHMEM_LOW) << 16)
500                 | ((u32)rtc_read(CMOS_MEM_HIGHMEM_MID) << 24)
501                 | ((u64)rtc_read(CMOS_MEM_HIGHMEM_HIGH) << 32));
502     RamSizeOver4G = high;
503     e820_add(0x100000000ull, high, E820_RAM);
504     dprintf(1, "RamSizeOver4G: 0x%016llx [cmos]\n", RamSizeOver4G);
505 }
506 
507 // Populate romfile entries for legacy fw_cfg ports (that predate the
508 // "file" interface).
509 static void
qemu_cfg_legacy(void)510 qemu_cfg_legacy(void)
511 {
512     if (!CONFIG_QEMU)
513         return;
514 
515     // Misc config items.
516     qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
517     qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
518     qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
519 
520     // NUMA data
521     u64 numacount;
522     qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
523     int max_cpu = romfile_loadint("etc/max-cpus", 0);
524     qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
525                      , max_cpu*sizeof(u64));
526     qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
527                      , sizeof(numacount) + max_cpu*sizeof(u64)
528                      , numacount*sizeof(u64));
529 
530     // ACPI tables
531     char name[128];
532     u16 cnt;
533     qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
534     int i, offset = sizeof(cnt);
535     for (i = 0; i < cnt; i++) {
536         u16 len;
537         qemu_cfg_read(&len, sizeof(len));
538         offset += sizeof(len);
539         snprintf(name, sizeof(name), "acpi/table%d", i);
540         qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
541         qemu_cfg_skip(len);
542         offset += len;
543     }
544 
545     // SMBIOS info
546     qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
547     offset = sizeof(cnt);
548     for (i = 0; i < cnt; i++) {
549         struct qemu_smbios_header header;
550         qemu_cfg_read(&header, sizeof(header));
551         if (header.headertype == SMBIOS_FIELD_ENTRY) {
552             snprintf(name, sizeof(name), "smbios/field%d-%d"
553                      , header.tabletype, header.fieldoffset);
554             qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
555                              , offset + sizeof(header)
556                              , header.length - sizeof(header));
557         } else {
558             snprintf(name, sizeof(name), "smbios/table%d-%d"
559                      , header.tabletype, i);
560             qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
561                              , offset + 3, header.length - 3);
562         }
563         qemu_cfg_skip(header.length - sizeof(header));
564         offset += header.length;
565     }
566 }
567 
568 struct QemuCfgFile {
569     u32  size;        /* file size */
570     u16  select;      /* write this to 0x510 to read it */
571     u16  reserved;
572     char name[56];
573 };
574 
qemu_cfg_init(void)575 void qemu_cfg_init(void)
576 {
577     if (!runningOnQEMU())
578         return;
579 
580     // Detect fw_cfg interface.
581     qemu_cfg_select(QEMU_CFG_SIGNATURE);
582     char *sig = "QEMU";
583     int i;
584     for (i = 0; i < 4; i++)
585         if (inb(PORT_QEMU_CFG_DATA) != sig[i])
586             return;
587 
588     dprintf(1, "Found QEMU fw_cfg\n");
589     cfg_enabled = 1;
590 
591     // Detect DMA interface.
592     u32 id;
593     qemu_cfg_read_entry(&id, QEMU_CFG_ID, sizeof(id));
594 
595     if (id & QEMU_CFG_VERSION_DMA) {
596         dprintf(1, "QEMU fw_cfg DMA interface supported\n");
597         cfg_dma_enabled = 1;
598     }
599 
600     // Populate romfiles for legacy fw_cfg entries
601     qemu_cfg_legacy();
602 
603     // Load files found in the fw_cfg file directory
604     u32 count;
605     qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
606     count = be32_to_cpu(count);
607     u32 e;
608     for (e = 0; e < count; e++) {
609         struct QemuCfgFile qfile;
610         qemu_cfg_read(&qfile, sizeof(qfile));
611         qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
612                          , 0, be32_to_cpu(qfile.size));
613     }
614 
615     qemu_cfg_e820();
616 
617     if (romfile_find("etc/table-loader")) {
618         acpi_pm_base = 0x0600;
619         dprintf(1, "Moving pm_base to 0x%x\n", acpi_pm_base);
620     }
621 
622     // serial console
623     u16 nogfx = 0;
624     qemu_cfg_read_entry(&nogfx, QEMU_CFG_NOGRAPHIC, sizeof(nogfx));
625     if (nogfx && !romfile_find("etc/sercon-port")
626         && !romfile_find("vgaroms/sgabios.bin"))
627         const_romfile_add_int("etc/sercon-port", PORT_SERIAL1);
628 }
629