blob: b76c1dbf36b21b661236cea867f223442bf5433e [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'Connor8ed2e532013-01-21 02:32:48 -050011#include "acpi.h" // acpi_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "byteorder.h" // be32_to_cpu
13#include "config.h" // CONFIG_QEMU
14#include "hw/cmos.h" // CMOS_*
Kevin O'Connor5d369d82013-09-02 20:48:46 -040015#include "hw/pci.h" // create_pirtable
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "ioport.h" // outw
Kevin O'Connor9dea5902013-09-14 20:23:54 -040017#include "malloc.h" // malloc_tmp
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040018#include "memmap.h" // add_e820
19#include "mptable.h" // mptable_setup
20#include "output.h" // dprintf
21#include "paravirt.h" // qemu_cfg_preinit
Kevin O'Connor41639f82013-09-14 19:37:36 -040022#include "romfile.h" // romfile_loadint
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040023#include "smbios.h" // smbios_setup
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040024#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040025#include "util.h" // pci_setup
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040026#include "x86.h" // cpuid
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040027#include "xen.h" // xen_biostable_setup
Kevin O'Connore7cc7642009-10-04 10:05:16 -040028
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050029// Amount of continuous ram under 4Gig
30u32 RamSize;
31// Amount of continuous ram >4Gig
32u64 RamSizeOver4G;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050033// Type of emulator platform.
34int PlatformRunningOn VARFSEG;
35
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050036/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
37 * should be used to determine that a VM is running under KVM.
38 */
39#define KVM_CPUID_SIGNATURE 0x40000000
40
41static void kvm_preinit(void)
42{
43 if (!CONFIG_QEMU)
44 return;
45 unsigned int eax, ebx, ecx, edx;
46 char signature[13];
47
48 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
49 memcpy(signature + 0, &ebx, 4);
50 memcpy(signature + 4, &ecx, 4);
51 memcpy(signature + 8, &edx, 4);
52 signature[12] = 0;
53
54 if (strcmp(signature, "KVMKVMKVM") == 0) {
55 dprintf(1, "Running on KVM\n");
56 PlatformRunningOn |= PF_KVM;
57 }
58}
59
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050060void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050061qemu_preinit(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050062{
Kevin O'Connor02313b22013-02-07 22:42:25 -050063 if (!CONFIG_QEMU)
64 return;
65
Kevin O'Connora2a86e22013-02-13 19:35:12 -050066 if (runningOnXen()) {
67 xen_ramsize_preinit();
68 return;
69 }
70
Kevin O'Connor02313b22013-02-07 22:42:25 -050071 PlatformRunningOn = PF_QEMU;
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050072 kvm_preinit();
Kevin O'Connor02313b22013-02-07 22:42:25 -050073
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050074 // On emulators, get memory size from nvram.
75 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
76 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
77 if (rs)
78 rs += 16 * 1024 * 1024;
79 else
80 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
81 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
82 + 1 * 1024 * 1024);
83 RamSize = rs;
84 add_e820(0, rs, E820_RAM);
85
86 // Check for memory over 4Gig
87 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
88 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
89 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
90 RamSizeOver4G = high;
91 add_e820(0x100000000ull, high, E820_RAM);
92
93 /* reserve 256KB BIOS area at the end of 4 GB */
94 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050095
96 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050097}
98
99void
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500100qemu_platform_setup(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500101{
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500102 if (!CONFIG_QEMU)
103 return;
104
105 if (runningOnXen()) {
106 pci_probe_devices();
107 xen_hypercall_setup();
108 xen_biostable_setup();
109 return;
110 }
111
112 // Initialize pci
113 pci_setup();
Kevin O'Connorcdbac7f2013-03-08 19:33:39 -0500114 smm_device_setup();
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500115 smm_setup();
116
117 // Initialize mtrr and smp
118 mtrr_setup();
119 smp_setup();
120
121 // Create bios tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500122 pirtable_setup();
123 mptable_setup();
124 smbios_setup();
125 acpi_setup();
126}
127
Kevin O'Connor4edda082013-02-09 13:21:08 -0500128
129/****************************************************************
130 * QEMU firmware config (fw_cfg) interface
131 ****************************************************************/
132
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500133// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
134// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -0500135#define QEMU_CFG_SIGNATURE 0x00
136#define QEMU_CFG_ID 0x01
137#define QEMU_CFG_UUID 0x02
138#define QEMU_CFG_NUMA 0x0d
139#define QEMU_CFG_BOOT_MENU 0x0e
140#define QEMU_CFG_MAX_CPUS 0x0f
141#define QEMU_CFG_FILE_DIR 0x19
142#define QEMU_CFG_ARCH_LOCAL 0x8000
143#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
144#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
145#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
146#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
147
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400148static void
149qemu_cfg_select(u16 f)
150{
151 outw(f, PORT_QEMU_CFG_CTL);
152}
153
154static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500155qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400156{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400157 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400158}
159
160static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400161qemu_cfg_skip(int len)
162{
163 while (len--)
164 inb(PORT_QEMU_CFG_DATA);
165}
166
167static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400168qemu_cfg_read_entry(void *buf, int e, int len)
169{
170 qemu_cfg_select(e);
171 qemu_cfg_read(buf, len);
172}
173
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400174struct qemu_romfile_s {
175 struct romfile_s file;
176 int select, skip;
177};
178
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500179static int
180qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100181{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400182 if (file->size > maxlen)
183 return -1;
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400184 struct qemu_romfile_s *qfile;
185 qfile = container_of(file, struct qemu_romfile_s, file);
186 qemu_cfg_select(qfile->select);
187 qemu_cfg_skip(qfile->skip);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500188 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400189 return file->size;
190}
191
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500192static void
193qemu_romfile_add(char *name, int select, int skip, int size)
194{
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400195 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
196 if (!qfile) {
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500197 warn_noalloc();
198 return;
199 }
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400200 memset(qfile, 0, sizeof(*qfile));
201 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
202 qfile->file.size = size;
203 qfile->select = select;
204 qfile->skip = skip;
205 qfile->file.copy = qemu_cfg_read_file;
206 romfile_add(&qfile->file);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500207}
208
Kevin O'Connorfe090302013-02-09 20:00:06 -0500209struct e820_reservation {
210 u64 address;
211 u64 length;
212 u32 type;
213};
214
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500215#define SMBIOS_FIELD_ENTRY 0
216#define SMBIOS_TABLE_ENTRY 1
217
218struct qemu_smbios_header {
219 u16 length;
220 u8 headertype;
221 u8 tabletype;
222 u16 fieldoffset;
223} PACKED;
224
Kevin O'Connor188d9942013-02-09 15:24:08 -0500225// Populate romfile entries for legacy fw_cfg ports (that predate the
226// "file" interface).
227static void
228qemu_cfg_legacy(void)
229{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400230 if (!CONFIG_QEMU)
231 return;
232
Kevin O'Connor56c50892013-02-09 19:25:51 -0500233 // Misc config items.
234 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
235 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
236 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
237
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500238 // NUMA data
239 u64 numacount;
240 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
Kevin O'Connorfb76cff2013-03-23 11:38:45 -0400241 int max_cpu = romfile_loadint("etc/max-cpus", 0);
242 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
243 , max_cpu*sizeof(u64));
244 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
245 , sizeof(numacount) + max_cpu*sizeof(u64)
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500246 , numacount*sizeof(u64));
247
Kevin O'Connorfe090302013-02-09 20:00:06 -0500248 // e820 data
249 u32 count32;
250 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
251 if (count32) {
252 struct e820_reservation entry;
253 int i;
254 for (i = 0; i < count32; i++) {
255 qemu_cfg_read(&entry, sizeof(entry));
256 add_e820(entry.address, entry.length, entry.type);
257 }
258 } else if (runningOnKVM()) {
259 // Backwards compatibility - provide hard coded range.
260 // 4 pages before the bios, 3 pages for vmx tss pages, the
261 // other page for EPT real mode pagetable
262 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
263 }
264
Kevin O'Connor188d9942013-02-09 15:24:08 -0500265 // ACPI tables
266 char name[128];
267 u16 cnt;
268 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
269 int i, offset = sizeof(cnt);
270 for (i = 0; i < cnt; i++) {
271 u16 len;
272 qemu_cfg_read(&len, sizeof(len));
273 offset += sizeof(len);
274 snprintf(name, sizeof(name), "acpi/table%d", i);
275 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
276 qemu_cfg_skip(len);
277 offset += len;
278 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500279
280 // SMBIOS info
281 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
282 offset = sizeof(cnt);
283 for (i = 0; i < cnt; i++) {
284 struct qemu_smbios_header header;
285 qemu_cfg_read(&header, sizeof(header));
286 if (header.headertype == SMBIOS_FIELD_ENTRY) {
287 snprintf(name, sizeof(name), "smbios/field%d-%d"
288 , header.tabletype, header.fieldoffset);
289 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
290 , offset + sizeof(header)
291 , header.length - sizeof(header));
292 } else {
293 snprintf(name, sizeof(name), "smbios/table%d-%d"
294 , header.tabletype, i);
295 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
296 , offset + 3, header.length - 3);
297 }
298 qemu_cfg_skip(header.length - sizeof(header));
299 offset += header.length;
300 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500301}
302
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400303struct QemuCfgFile {
304 u32 size; /* file size */
305 u16 select; /* write this to 0x510 to read it */
306 u16 reserved;
307 char name[56];
308};
309
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500310void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400311{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400312 if (!runningOnQEMU())
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400313 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100314
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500315 // Detect fw_cfg interface.
316 qemu_cfg_select(QEMU_CFG_SIGNATURE);
317 char *sig = "QEMU";
318 int i;
319 for (i = 0; i < 4; i++)
320 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
321 return;
322 dprintf(1, "Found QEMU fw_cfg\n");
323
Kevin O'Connor188d9942013-02-09 15:24:08 -0500324 // Populate romfiles for legacy fw_cfg entries
325 qemu_cfg_legacy();
326
327 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400328 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100329 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400330 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400331 u32 e;
332 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400333 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500334 qemu_cfg_read(&qfile, sizeof(qfile));
335 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
336 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100337 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400338}