blob: fd581840bdf0c93bd1838a377fc13719b3283078 [file] [log] [blame]
Kevin O'Connore7cc7642009-10-04 10:05:16 -04001// Paravirtualization support.
2//
Kevin O'Connorb840ba92013-02-09 20:09:22 -05003// Copyright (C) 2013 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connore7cc7642009-10-04 10:05:16 -04004// 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'Connor897fb112013-02-07 23:32:48 -050011#include "config.h" // CONFIG_QEMU
Kevin O'Connorb3064592012-08-14 21:20:10 -040012#include "util.h" // dprintf
13#include "byteorder.h" // be32_to_cpu
Kevin O'Connor01a85202009-10-18 09:49:59 -040014#include "ioport.h" // outw
Kevin O'Connord83c87b2013-01-21 01:14:12 -050015#include "paravirt.h" // qemu_cfg_preinit
Kevin O'Connorde9e7052013-02-09 19:09:20 -050016#include "smbios.h" // smbios_setup
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050017#include "memmap.h" // add_e820
Kevin O'Connor5d369d82013-09-02 20:48:46 -040018#include "hw/cmos.h" // CMOS_*
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050019#include "acpi.h" // acpi_setup
20#include "mptable.h" // mptable_setup
Kevin O'Connor5d369d82013-09-02 20:48:46 -040021#include "hw/pci.h" // create_pirtable
Kevin O'Connora2a86e22013-02-13 19:35:12 -050022#include "xen.h" // xen_biostable_setup
Kevin O'Connor41639f82013-09-14 19:37:36 -040023#include "romfile.h" // romfile_loadint
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040024#include "string.h" // memset
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040025#include "x86.h" // cpuid
Kevin O'Connore7cc7642009-10-04 10:05:16 -040026
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050027// Amount of continuous ram under 4Gig
28u32 RamSize;
29// Amount of continuous ram >4Gig
30u64 RamSizeOver4G;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050031// Type of emulator platform.
32int PlatformRunningOn VARFSEG;
33
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050034/* 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.
36 */
37#define KVM_CPUID_SIGNATURE 0x40000000
38
39static void kvm_preinit(void)
40{
41 if (!CONFIG_QEMU)
42 return;
43 unsigned int eax, ebx, ecx, edx;
44 char signature[13];
45
46 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
47 memcpy(signature + 0, &ebx, 4);
48 memcpy(signature + 4, &ecx, 4);
49 memcpy(signature + 8, &edx, 4);
50 signature[12] = 0;
51
52 if (strcmp(signature, "KVMKVMKVM") == 0) {
53 dprintf(1, "Running on KVM\n");
54 PlatformRunningOn |= PF_KVM;
55 }
56}
57
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050058void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050059qemu_preinit(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050060{
Kevin O'Connor02313b22013-02-07 22:42:25 -050061 if (!CONFIG_QEMU)
62 return;
63
Kevin O'Connora2a86e22013-02-13 19:35:12 -050064 if (runningOnXen()) {
65 xen_ramsize_preinit();
66 return;
67 }
68
Kevin O'Connor02313b22013-02-07 22:42:25 -050069 PlatformRunningOn = PF_QEMU;
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050070 kvm_preinit();
Kevin O'Connor02313b22013-02-07 22:42:25 -050071
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050072 // On emulators, get memory size from nvram.
73 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
74 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
75 if (rs)
76 rs += 16 * 1024 * 1024;
77 else
78 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
79 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
80 + 1 * 1024 * 1024);
81 RamSize = rs;
82 add_e820(0, rs, E820_RAM);
83
84 // Check for memory over 4Gig
85 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
86 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
87 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
88 RamSizeOver4G = high;
89 add_e820(0x100000000ull, high, E820_RAM);
90
91 /* reserve 256KB BIOS area at the end of 4 GB */
92 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050093
94 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050095}
96
97void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050098qemu_platform_setup(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050099{
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500100 if (!CONFIG_QEMU)
101 return;
102
103 if (runningOnXen()) {
104 pci_probe_devices();
105 xen_hypercall_setup();
106 xen_biostable_setup();
107 return;
108 }
109
110 // Initialize pci
111 pci_setup();
Kevin O'Connorcdbac7f2013-03-08 19:33:39 -0500112 smm_device_setup();
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500113 smm_setup();
114
115 // Initialize mtrr and smp
116 mtrr_setup();
117 smp_setup();
118
119 // Create bios tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500120 pirtable_setup();
121 mptable_setup();
122 smbios_setup();
123 acpi_setup();
124}
125
Kevin O'Connor4edda082013-02-09 13:21:08 -0500126
127/****************************************************************
128 * QEMU firmware config (fw_cfg) interface
129 ****************************************************************/
130
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500131// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
132// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -0500133#define QEMU_CFG_SIGNATURE 0x00
134#define QEMU_CFG_ID 0x01
135#define QEMU_CFG_UUID 0x02
136#define QEMU_CFG_NUMA 0x0d
137#define QEMU_CFG_BOOT_MENU 0x0e
138#define QEMU_CFG_MAX_CPUS 0x0f
139#define QEMU_CFG_FILE_DIR 0x19
140#define QEMU_CFG_ARCH_LOCAL 0x8000
141#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
142#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
143#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
144#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
145
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400146static void
147qemu_cfg_select(u16 f)
148{
149 outw(f, PORT_QEMU_CFG_CTL);
150}
151
152static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500153qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400154{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400155 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400156}
157
158static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400159qemu_cfg_skip(int len)
160{
161 while (len--)
162 inb(PORT_QEMU_CFG_DATA);
163}
164
165static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400166qemu_cfg_read_entry(void *buf, int e, int len)
167{
168 qemu_cfg_select(e);
169 qemu_cfg_read(buf, len);
170}
171
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400172struct qemu_romfile_s {
173 struct romfile_s file;
174 int select, skip;
175};
176
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500177static int
178qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100179{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400180 if (file->size > maxlen)
181 return -1;
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400182 struct qemu_romfile_s *qfile;
183 qfile = container_of(file, struct qemu_romfile_s, file);
184 qemu_cfg_select(qfile->select);
185 qemu_cfg_skip(qfile->skip);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500186 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400187 return file->size;
188}
189
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500190static void
191qemu_romfile_add(char *name, int select, int skip, int size)
192{
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400193 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
194 if (!qfile) {
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500195 warn_noalloc();
196 return;
197 }
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400198 memset(qfile, 0, sizeof(*qfile));
199 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
200 qfile->file.size = size;
201 qfile->select = select;
202 qfile->skip = skip;
203 qfile->file.copy = qemu_cfg_read_file;
204 romfile_add(&qfile->file);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500205}
206
Kevin O'Connorfe090302013-02-09 20:00:06 -0500207struct e820_reservation {
208 u64 address;
209 u64 length;
210 u32 type;
211};
212
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500213#define SMBIOS_FIELD_ENTRY 0
214#define SMBIOS_TABLE_ENTRY 1
215
216struct qemu_smbios_header {
217 u16 length;
218 u8 headertype;
219 u8 tabletype;
220 u16 fieldoffset;
221} PACKED;
222
Kevin O'Connor188d9942013-02-09 15:24:08 -0500223// Populate romfile entries for legacy fw_cfg ports (that predate the
224// "file" interface).
225static void
226qemu_cfg_legacy(void)
227{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400228 if (!CONFIG_QEMU)
229 return;
230
Kevin O'Connor56c50892013-02-09 19:25:51 -0500231 // Misc config items.
232 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
233 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
234 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
235
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500236 // NUMA data
237 u64 numacount;
238 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
Kevin O'Connorfb76cff2013-03-23 11:38:45 -0400239 int max_cpu = romfile_loadint("etc/max-cpus", 0);
240 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
241 , max_cpu*sizeof(u64));
242 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
243 , sizeof(numacount) + max_cpu*sizeof(u64)
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500244 , numacount*sizeof(u64));
245
Kevin O'Connorfe090302013-02-09 20:00:06 -0500246 // e820 data
247 u32 count32;
248 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
249 if (count32) {
250 struct e820_reservation entry;
251 int i;
252 for (i = 0; i < count32; i++) {
253 qemu_cfg_read(&entry, sizeof(entry));
254 add_e820(entry.address, entry.length, entry.type);
255 }
256 } else if (runningOnKVM()) {
257 // Backwards compatibility - provide hard coded range.
258 // 4 pages before the bios, 3 pages for vmx tss pages, the
259 // other page for EPT real mode pagetable
260 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
261 }
262
Kevin O'Connor188d9942013-02-09 15:24:08 -0500263 // ACPI tables
264 char name[128];
265 u16 cnt;
266 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
267 int i, offset = sizeof(cnt);
268 for (i = 0; i < cnt; i++) {
269 u16 len;
270 qemu_cfg_read(&len, sizeof(len));
271 offset += sizeof(len);
272 snprintf(name, sizeof(name), "acpi/table%d", i);
273 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
274 qemu_cfg_skip(len);
275 offset += len;
276 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500277
278 // SMBIOS info
279 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
280 offset = sizeof(cnt);
281 for (i = 0; i < cnt; i++) {
282 struct qemu_smbios_header header;
283 qemu_cfg_read(&header, sizeof(header));
284 if (header.headertype == SMBIOS_FIELD_ENTRY) {
285 snprintf(name, sizeof(name), "smbios/field%d-%d"
286 , header.tabletype, header.fieldoffset);
287 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
288 , offset + sizeof(header)
289 , header.length - sizeof(header));
290 } else {
291 snprintf(name, sizeof(name), "smbios/table%d-%d"
292 , header.tabletype, i);
293 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
294 , offset + 3, header.length - 3);
295 }
296 qemu_cfg_skip(header.length - sizeof(header));
297 offset += header.length;
298 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500299}
300
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400301struct QemuCfgFile {
302 u32 size; /* file size */
303 u16 select; /* write this to 0x510 to read it */
304 u16 reserved;
305 char name[56];
306};
307
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500308void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400309{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400310 if (!runningOnQEMU())
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400311 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100312
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500313 // Detect fw_cfg interface.
314 qemu_cfg_select(QEMU_CFG_SIGNATURE);
315 char *sig = "QEMU";
316 int i;
317 for (i = 0; i < 4; i++)
318 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
319 return;
320 dprintf(1, "Found QEMU fw_cfg\n");
321
Kevin O'Connor188d9942013-02-09 15:24:08 -0500322 // Populate romfiles for legacy fw_cfg entries
323 qemu_cfg_legacy();
324
325 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400326 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100327 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400328 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400329 u32 e;
330 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400331 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500332 qemu_cfg_read(&qfile, sizeof(qfile));
333 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
334 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100335 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400336}