blob: 2be428110e4f2236a14961e6674539b2527c1db4 [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'Connorfa9c66a2013-09-14 19:10:40 -040023#include "string.h" // memset
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040024#include "x86.h" // cpuid
Kevin O'Connore7cc7642009-10-04 10:05:16 -040025
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050026// Amount of continuous ram under 4Gig
27u32 RamSize;
28// Amount of continuous ram >4Gig
29u64 RamSizeOver4G;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050030// Type of emulator platform.
31int PlatformRunningOn VARFSEG;
32
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050033/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
34 * should be used to determine that a VM is running under KVM.
35 */
36#define KVM_CPUID_SIGNATURE 0x40000000
37
38static void kvm_preinit(void)
39{
40 if (!CONFIG_QEMU)
41 return;
42 unsigned int eax, ebx, ecx, edx;
43 char signature[13];
44
45 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
46 memcpy(signature + 0, &ebx, 4);
47 memcpy(signature + 4, &ecx, 4);
48 memcpy(signature + 8, &edx, 4);
49 signature[12] = 0;
50
51 if (strcmp(signature, "KVMKVMKVM") == 0) {
52 dprintf(1, "Running on KVM\n");
53 PlatformRunningOn |= PF_KVM;
54 }
55}
56
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050057void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050058qemu_preinit(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050059{
Kevin O'Connor02313b22013-02-07 22:42:25 -050060 if (!CONFIG_QEMU)
61 return;
62
Kevin O'Connora2a86e22013-02-13 19:35:12 -050063 if (runningOnXen()) {
64 xen_ramsize_preinit();
65 return;
66 }
67
Kevin O'Connor02313b22013-02-07 22:42:25 -050068 PlatformRunningOn = PF_QEMU;
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050069 kvm_preinit();
Kevin O'Connor02313b22013-02-07 22:42:25 -050070
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050071 // On emulators, get memory size from nvram.
72 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
73 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
74 if (rs)
75 rs += 16 * 1024 * 1024;
76 else
77 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
78 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
79 + 1 * 1024 * 1024);
80 RamSize = rs;
81 add_e820(0, rs, E820_RAM);
82
83 // Check for memory over 4Gig
84 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
85 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
86 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
87 RamSizeOver4G = high;
88 add_e820(0x100000000ull, high, E820_RAM);
89
90 /* reserve 256KB BIOS area at the end of 4 GB */
91 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050092
93 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050094}
95
96void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050097qemu_platform_setup(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050098{
Kevin O'Connora2a86e22013-02-13 19:35:12 -050099 if (!CONFIG_QEMU)
100 return;
101
102 if (runningOnXen()) {
103 pci_probe_devices();
104 xen_hypercall_setup();
105 xen_biostable_setup();
106 return;
107 }
108
109 // Initialize pci
110 pci_setup();
Kevin O'Connorcdbac7f2013-03-08 19:33:39 -0500111 smm_device_setup();
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500112 smm_setup();
113
114 // Initialize mtrr and smp
115 mtrr_setup();
116 smp_setup();
117
118 // Create bios tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500119 pirtable_setup();
120 mptable_setup();
121 smbios_setup();
122 acpi_setup();
123}
124
Kevin O'Connor4edda082013-02-09 13:21:08 -0500125
126/****************************************************************
127 * QEMU firmware config (fw_cfg) interface
128 ****************************************************************/
129
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500130// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
131// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -0500132#define QEMU_CFG_SIGNATURE 0x00
133#define QEMU_CFG_ID 0x01
134#define QEMU_CFG_UUID 0x02
135#define QEMU_CFG_NUMA 0x0d
136#define QEMU_CFG_BOOT_MENU 0x0e
137#define QEMU_CFG_MAX_CPUS 0x0f
138#define QEMU_CFG_FILE_DIR 0x19
139#define QEMU_CFG_ARCH_LOCAL 0x8000
140#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
141#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
142#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
143#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
144
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400145static void
146qemu_cfg_select(u16 f)
147{
148 outw(f, PORT_QEMU_CFG_CTL);
149}
150
151static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500152qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400153{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400154 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400155}
156
157static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400158qemu_cfg_skip(int len)
159{
160 while (len--)
161 inb(PORT_QEMU_CFG_DATA);
162}
163
164static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400165qemu_cfg_read_entry(void *buf, int e, int len)
166{
167 qemu_cfg_select(e);
168 qemu_cfg_read(buf, len);
169}
170
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400171struct qemu_romfile_s {
172 struct romfile_s file;
173 int select, skip;
174};
175
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500176static int
177qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100178{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400179 if (file->size > maxlen)
180 return -1;
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400181 struct qemu_romfile_s *qfile;
182 qfile = container_of(file, struct qemu_romfile_s, file);
183 qemu_cfg_select(qfile->select);
184 qemu_cfg_skip(qfile->skip);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500185 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400186 return file->size;
187}
188
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500189static void
190qemu_romfile_add(char *name, int select, int skip, int size)
191{
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400192 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
193 if (!qfile) {
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500194 warn_noalloc();
195 return;
196 }
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400197 memset(qfile, 0, sizeof(*qfile));
198 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
199 qfile->file.size = size;
200 qfile->select = select;
201 qfile->skip = skip;
202 qfile->file.copy = qemu_cfg_read_file;
203 romfile_add(&qfile->file);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500204}
205
Kevin O'Connorfe090302013-02-09 20:00:06 -0500206struct e820_reservation {
207 u64 address;
208 u64 length;
209 u32 type;
210};
211
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500212#define SMBIOS_FIELD_ENTRY 0
213#define SMBIOS_TABLE_ENTRY 1
214
215struct qemu_smbios_header {
216 u16 length;
217 u8 headertype;
218 u8 tabletype;
219 u16 fieldoffset;
220} PACKED;
221
Kevin O'Connor188d9942013-02-09 15:24:08 -0500222// Populate romfile entries for legacy fw_cfg ports (that predate the
223// "file" interface).
224static void
225qemu_cfg_legacy(void)
226{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400227 if (!CONFIG_QEMU)
228 return;
229
Kevin O'Connor56c50892013-02-09 19:25:51 -0500230 // Misc config items.
231 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
232 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
233 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
234
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500235 // NUMA data
236 u64 numacount;
237 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
Kevin O'Connorfb76cff2013-03-23 11:38:45 -0400238 int max_cpu = romfile_loadint("etc/max-cpus", 0);
239 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
240 , max_cpu*sizeof(u64));
241 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
242 , sizeof(numacount) + max_cpu*sizeof(u64)
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500243 , numacount*sizeof(u64));
244
Kevin O'Connorfe090302013-02-09 20:00:06 -0500245 // e820 data
246 u32 count32;
247 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
248 if (count32) {
249 struct e820_reservation entry;
250 int i;
251 for (i = 0; i < count32; i++) {
252 qemu_cfg_read(&entry, sizeof(entry));
253 add_e820(entry.address, entry.length, entry.type);
254 }
255 } else if (runningOnKVM()) {
256 // Backwards compatibility - provide hard coded range.
257 // 4 pages before the bios, 3 pages for vmx tss pages, the
258 // other page for EPT real mode pagetable
259 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
260 }
261
Kevin O'Connor188d9942013-02-09 15:24:08 -0500262 // ACPI tables
263 char name[128];
264 u16 cnt;
265 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
266 int i, offset = sizeof(cnt);
267 for (i = 0; i < cnt; i++) {
268 u16 len;
269 qemu_cfg_read(&len, sizeof(len));
270 offset += sizeof(len);
271 snprintf(name, sizeof(name), "acpi/table%d", i);
272 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
273 qemu_cfg_skip(len);
274 offset += len;
275 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500276
277 // SMBIOS info
278 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
279 offset = sizeof(cnt);
280 for (i = 0; i < cnt; i++) {
281 struct qemu_smbios_header header;
282 qemu_cfg_read(&header, sizeof(header));
283 if (header.headertype == SMBIOS_FIELD_ENTRY) {
284 snprintf(name, sizeof(name), "smbios/field%d-%d"
285 , header.tabletype, header.fieldoffset);
286 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
287 , offset + sizeof(header)
288 , header.length - sizeof(header));
289 } else {
290 snprintf(name, sizeof(name), "smbios/table%d-%d"
291 , header.tabletype, i);
292 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
293 , offset + 3, header.length - 3);
294 }
295 qemu_cfg_skip(header.length - sizeof(header));
296 offset += header.length;
297 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500298}
299
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400300struct QemuCfgFile {
301 u32 size; /* file size */
302 u16 select; /* write this to 0x510 to read it */
303 u16 reserved;
304 char name[56];
305};
306
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500307void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400308{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400309 if (!runningOnQEMU())
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400310 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100311
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500312 // Detect fw_cfg interface.
313 qemu_cfg_select(QEMU_CFG_SIGNATURE);
314 char *sig = "QEMU";
315 int i;
316 for (i = 0; i < 4; i++)
317 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
318 return;
319 dprintf(1, "Found QEMU fw_cfg\n");
320
Kevin O'Connor188d9942013-02-09 15:24:08 -0500321 // Populate romfiles for legacy fw_cfg entries
322 qemu_cfg_legacy();
323
324 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400325 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100326 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400327 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400328 u32 e;
329 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400330 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500331 qemu_cfg_read(&qfile, sizeof(qfile));
332 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
333 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100334 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400335}