blob: f76b47f3264147494c3d027fc2108b19714d9a77 [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
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'Connora2a86e22013-02-13 19:35:12 -050022#include "xen.h" // xen_biostable_setup
Kevin O'Connore7cc7642009-10-04 10:05:16 -040023
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050024/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
25 * should be used to determine that a VM is running under KVM.
26 */
27#define KVM_CPUID_SIGNATURE 0x40000000
28
29static void kvm_preinit(void)
30{
31 if (!CONFIG_QEMU)
32 return;
33 unsigned int eax, ebx, ecx, edx;
34 char signature[13];
35
36 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
37 memcpy(signature + 0, &ebx, 4);
38 memcpy(signature + 4, &ecx, 4);
39 memcpy(signature + 8, &edx, 4);
40 signature[12] = 0;
41
42 if (strcmp(signature, "KVMKVMKVM") == 0) {
43 dprintf(1, "Running on KVM\n");
44 PlatformRunningOn |= PF_KVM;
45 }
46}
47
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050048void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050049qemu_preinit(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050050{
Kevin O'Connor02313b22013-02-07 22:42:25 -050051 if (!CONFIG_QEMU)
52 return;
53
Kevin O'Connora2a86e22013-02-13 19:35:12 -050054 if (runningOnXen()) {
55 xen_ramsize_preinit();
56 return;
57 }
58
Kevin O'Connor02313b22013-02-07 22:42:25 -050059 PlatformRunningOn = PF_QEMU;
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050060 kvm_preinit();
Kevin O'Connor02313b22013-02-07 22:42:25 -050061
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050062 // On emulators, get memory size from nvram.
63 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
64 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
65 if (rs)
66 rs += 16 * 1024 * 1024;
67 else
68 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
69 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
70 + 1 * 1024 * 1024);
71 RamSize = rs;
72 add_e820(0, rs, E820_RAM);
73
74 // Check for memory over 4Gig
75 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
76 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
77 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
78 RamSizeOver4G = high;
79 add_e820(0x100000000ull, high, E820_RAM);
80
81 /* reserve 256KB BIOS area at the end of 4 GB */
82 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050083}
84
85void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050086qemu_platform_setup(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050087{
Kevin O'Connora2a86e22013-02-13 19:35:12 -050088 if (!CONFIG_QEMU)
89 return;
90
91 if (runningOnXen()) {
92 pci_probe_devices();
93 xen_hypercall_setup();
94 xen_biostable_setup();
95 return;
96 }
97
98 // Initialize pci
99 pci_setup();
100 smm_setup();
101
102 // Initialize mtrr and smp
103 mtrr_setup();
104 smp_setup();
105
106 // Create bios tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500107 pirtable_setup();
108 mptable_setup();
109 smbios_setup();
110 acpi_setup();
111}
112
Kevin O'Connor4edda082013-02-09 13:21:08 -0500113
114/****************************************************************
115 * QEMU firmware config (fw_cfg) interface
116 ****************************************************************/
117
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500118// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
119// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -0500120#define QEMU_CFG_SIGNATURE 0x00
121#define QEMU_CFG_ID 0x01
122#define QEMU_CFG_UUID 0x02
123#define QEMU_CFG_NUMA 0x0d
124#define QEMU_CFG_BOOT_MENU 0x0e
125#define QEMU_CFG_MAX_CPUS 0x0f
126#define QEMU_CFG_FILE_DIR 0x19
127#define QEMU_CFG_ARCH_LOCAL 0x8000
128#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
129#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
130#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
131#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
132
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400133static void
134qemu_cfg_select(u16 f)
135{
136 outw(f, PORT_QEMU_CFG_CTL);
137}
138
139static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500140qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400141{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400142 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400143}
144
145static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400146qemu_cfg_skip(int len)
147{
148 while (len--)
149 inb(PORT_QEMU_CFG_DATA);
150}
151
152static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400153qemu_cfg_read_entry(void *buf, int e, int len)
154{
155 qemu_cfg_select(e);
156 qemu_cfg_read(buf, len);
157}
158
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500159static int
160qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100161{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400162 if (file->size > maxlen)
163 return -1;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500164 qemu_cfg_select(file->id);
165 qemu_cfg_skip(file->rawsize);
166 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400167 return file->size;
168}
169
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500170static void
171qemu_romfile_add(char *name, int select, int skip, int size)
172{
173 struct romfile_s *file = malloc_tmp(sizeof(*file));
174 if (!file) {
175 warn_noalloc();
176 return;
177 }
178 memset(file, 0, sizeof(*file));
179 strtcpy(file->name, name, sizeof(file->name));
180 file->id = select;
181 file->rawsize = skip; // Use rawsize to indicate skip length.
182 file->size = size;
183 file->copy = qemu_cfg_read_file;
184 romfile_add(file);
185}
186
Kevin O'Connorfe090302013-02-09 20:00:06 -0500187struct e820_reservation {
188 u64 address;
189 u64 length;
190 u32 type;
191};
192
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500193#define SMBIOS_FIELD_ENTRY 0
194#define SMBIOS_TABLE_ENTRY 1
195
196struct qemu_smbios_header {
197 u16 length;
198 u8 headertype;
199 u8 tabletype;
200 u16 fieldoffset;
201} PACKED;
202
Kevin O'Connor188d9942013-02-09 15:24:08 -0500203// Populate romfile entries for legacy fw_cfg ports (that predate the
204// "file" interface).
205static void
206qemu_cfg_legacy(void)
207{
Kevin O'Connor56c50892013-02-09 19:25:51 -0500208 // Misc config items.
209 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
210 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
211 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
212
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500213 // NUMA data
214 u64 numacount;
215 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
216 numacount += romfile_loadint("etc/max-cpus", 0);
217 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA, sizeof(numacount)
218 , numacount*sizeof(u64));
219
Kevin O'Connorfe090302013-02-09 20:00:06 -0500220 // e820 data
221 u32 count32;
222 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
223 if (count32) {
224 struct e820_reservation entry;
225 int i;
226 for (i = 0; i < count32; i++) {
227 qemu_cfg_read(&entry, sizeof(entry));
228 add_e820(entry.address, entry.length, entry.type);
229 }
230 } else if (runningOnKVM()) {
231 // Backwards compatibility - provide hard coded range.
232 // 4 pages before the bios, 3 pages for vmx tss pages, the
233 // other page for EPT real mode pagetable
234 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
235 }
236
Kevin O'Connor188d9942013-02-09 15:24:08 -0500237 // ACPI tables
238 char name[128];
239 u16 cnt;
240 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
241 int i, offset = sizeof(cnt);
242 for (i = 0; i < cnt; i++) {
243 u16 len;
244 qemu_cfg_read(&len, sizeof(len));
245 offset += sizeof(len);
246 snprintf(name, sizeof(name), "acpi/table%d", i);
247 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
248 qemu_cfg_skip(len);
249 offset += len;
250 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500251
252 // SMBIOS info
253 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
254 offset = sizeof(cnt);
255 for (i = 0; i < cnt; i++) {
256 struct qemu_smbios_header header;
257 qemu_cfg_read(&header, sizeof(header));
258 if (header.headertype == SMBIOS_FIELD_ENTRY) {
259 snprintf(name, sizeof(name), "smbios/field%d-%d"
260 , header.tabletype, header.fieldoffset);
261 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
262 , offset + sizeof(header)
263 , header.length - sizeof(header));
264 } else {
265 snprintf(name, sizeof(name), "smbios/table%d-%d"
266 , header.tabletype, i);
267 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
268 , offset + 3, header.length - 3);
269 }
270 qemu_cfg_skip(header.length - sizeof(header));
271 offset += header.length;
272 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500273}
274
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400275struct QemuCfgFile {
276 u32 size; /* file size */
277 u16 select; /* write this to 0x510 to read it */
278 u16 reserved;
279 char name[56];
280};
281
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500282void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400283{
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500284 if (!CONFIG_QEMU)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400285 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100286
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500287 // Detect fw_cfg interface.
288 qemu_cfg_select(QEMU_CFG_SIGNATURE);
289 char *sig = "QEMU";
290 int i;
291 for (i = 0; i < 4; i++)
292 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
293 return;
294 dprintf(1, "Found QEMU fw_cfg\n");
295
Kevin O'Connor188d9942013-02-09 15:24:08 -0500296 // Populate romfiles for legacy fw_cfg entries
297 qemu_cfg_legacy();
298
299 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400300 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100301 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400302 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400303 u32 e;
304 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400305 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500306 qemu_cfg_read(&qfile, sizeof(qfile));
307 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
308 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100309 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400310}