blob: aa4a42166993af0298ecc083bba68ebc1cfcddb1 [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'Connore7cc7642009-10-04 10:05:16 -040022
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050023/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
24 * should be used to determine that a VM is running under KVM.
25 */
26#define KVM_CPUID_SIGNATURE 0x40000000
27
28static void kvm_preinit(void)
29{
30 if (!CONFIG_QEMU)
31 return;
32 unsigned int eax, ebx, ecx, edx;
33 char signature[13];
34
35 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
36 memcpy(signature + 0, &ebx, 4);
37 memcpy(signature + 4, &ecx, 4);
38 memcpy(signature + 8, &edx, 4);
39 signature[12] = 0;
40
41 if (strcmp(signature, "KVMKVMKVM") == 0) {
42 dprintf(1, "Running on KVM\n");
43 PlatformRunningOn |= PF_KVM;
44 }
45}
46
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050047void
48qemu_ramsize_preinit(void)
49{
Kevin O'Connor02313b22013-02-07 22:42:25 -050050 if (!CONFIG_QEMU)
51 return;
52
53 PlatformRunningOn = PF_QEMU;
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050054 kvm_preinit();
Kevin O'Connor02313b22013-02-07 22:42:25 -050055
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050056 // On emulators, get memory size from nvram.
57 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
58 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
59 if (rs)
60 rs += 16 * 1024 * 1024;
61 else
62 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
63 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
64 + 1 * 1024 * 1024);
65 RamSize = rs;
66 add_e820(0, rs, E820_RAM);
67
68 // Check for memory over 4Gig
69 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
70 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
71 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
72 RamSizeOver4G = high;
73 add_e820(0x100000000ull, high, E820_RAM);
74
75 /* reserve 256KB BIOS area at the end of 4 GB */
76 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050077}
78
79void
80qemu_biostable_setup(void)
81{
82 pirtable_setup();
83 mptable_setup();
84 smbios_setup();
85 acpi_setup();
86}
87
Kevin O'Connor4edda082013-02-09 13:21:08 -050088
89/****************************************************************
90 * QEMU firmware config (fw_cfg) interface
91 ****************************************************************/
92
Kevin O'Connorb840ba92013-02-09 20:09:22 -050093// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
94// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -050095#define QEMU_CFG_SIGNATURE 0x00
96#define QEMU_CFG_ID 0x01
97#define QEMU_CFG_UUID 0x02
98#define QEMU_CFG_NUMA 0x0d
99#define QEMU_CFG_BOOT_MENU 0x0e
100#define QEMU_CFG_MAX_CPUS 0x0f
101#define QEMU_CFG_FILE_DIR 0x19
102#define QEMU_CFG_ARCH_LOCAL 0x8000
103#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
104#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
105#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
106#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
107
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400108static void
109qemu_cfg_select(u16 f)
110{
111 outw(f, PORT_QEMU_CFG_CTL);
112}
113
114static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500115qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400116{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400117 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400118}
119
120static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400121qemu_cfg_skip(int len)
122{
123 while (len--)
124 inb(PORT_QEMU_CFG_DATA);
125}
126
127static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400128qemu_cfg_read_entry(void *buf, int e, int len)
129{
130 qemu_cfg_select(e);
131 qemu_cfg_read(buf, len);
132}
133
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500134static int
135qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100136{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400137 if (file->size > maxlen)
138 return -1;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500139 qemu_cfg_select(file->id);
140 qemu_cfg_skip(file->rawsize);
141 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400142 return file->size;
143}
144
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500145static void
146qemu_romfile_add(char *name, int select, int skip, int size)
147{
148 struct romfile_s *file = malloc_tmp(sizeof(*file));
149 if (!file) {
150 warn_noalloc();
151 return;
152 }
153 memset(file, 0, sizeof(*file));
154 strtcpy(file->name, name, sizeof(file->name));
155 file->id = select;
156 file->rawsize = skip; // Use rawsize to indicate skip length.
157 file->size = size;
158 file->copy = qemu_cfg_read_file;
159 romfile_add(file);
160}
161
Kevin O'Connorfe090302013-02-09 20:00:06 -0500162struct e820_reservation {
163 u64 address;
164 u64 length;
165 u32 type;
166};
167
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500168#define SMBIOS_FIELD_ENTRY 0
169#define SMBIOS_TABLE_ENTRY 1
170
171struct qemu_smbios_header {
172 u16 length;
173 u8 headertype;
174 u8 tabletype;
175 u16 fieldoffset;
176} PACKED;
177
Kevin O'Connor188d9942013-02-09 15:24:08 -0500178// Populate romfile entries for legacy fw_cfg ports (that predate the
179// "file" interface).
180static void
181qemu_cfg_legacy(void)
182{
Kevin O'Connor56c50892013-02-09 19:25:51 -0500183 // Misc config items.
184 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
185 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
186 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
187
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500188 // NUMA data
189 u64 numacount;
190 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
191 numacount += romfile_loadint("etc/max-cpus", 0);
192 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA, sizeof(numacount)
193 , numacount*sizeof(u64));
194
Kevin O'Connorfe090302013-02-09 20:00:06 -0500195 // e820 data
196 u32 count32;
197 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
198 if (count32) {
199 struct e820_reservation entry;
200 int i;
201 for (i = 0; i < count32; i++) {
202 qemu_cfg_read(&entry, sizeof(entry));
203 add_e820(entry.address, entry.length, entry.type);
204 }
205 } else if (runningOnKVM()) {
206 // Backwards compatibility - provide hard coded range.
207 // 4 pages before the bios, 3 pages for vmx tss pages, the
208 // other page for EPT real mode pagetable
209 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
210 }
211
Kevin O'Connor188d9942013-02-09 15:24:08 -0500212 // ACPI tables
213 char name[128];
214 u16 cnt;
215 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
216 int i, offset = sizeof(cnt);
217 for (i = 0; i < cnt; i++) {
218 u16 len;
219 qemu_cfg_read(&len, sizeof(len));
220 offset += sizeof(len);
221 snprintf(name, sizeof(name), "acpi/table%d", i);
222 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
223 qemu_cfg_skip(len);
224 offset += len;
225 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500226
227 // SMBIOS info
228 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
229 offset = sizeof(cnt);
230 for (i = 0; i < cnt; i++) {
231 struct qemu_smbios_header header;
232 qemu_cfg_read(&header, sizeof(header));
233 if (header.headertype == SMBIOS_FIELD_ENTRY) {
234 snprintf(name, sizeof(name), "smbios/field%d-%d"
235 , header.tabletype, header.fieldoffset);
236 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
237 , offset + sizeof(header)
238 , header.length - sizeof(header));
239 } else {
240 snprintf(name, sizeof(name), "smbios/table%d-%d"
241 , header.tabletype, i);
242 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
243 , offset + 3, header.length - 3);
244 }
245 qemu_cfg_skip(header.length - sizeof(header));
246 offset += header.length;
247 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500248}
249
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400250struct QemuCfgFile {
251 u32 size; /* file size */
252 u16 select; /* write this to 0x510 to read it */
253 u16 reserved;
254 char name[56];
255};
256
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500257void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400258{
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500259 if (!CONFIG_QEMU)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400260 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100261
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500262 // Detect fw_cfg interface.
263 qemu_cfg_select(QEMU_CFG_SIGNATURE);
264 char *sig = "QEMU";
265 int i;
266 for (i = 0; i < 4; i++)
267 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
268 return;
269 dprintf(1, "Found QEMU fw_cfg\n");
270
Kevin O'Connor188d9942013-02-09 15:24:08 -0500271 // Populate romfiles for legacy fw_cfg entries
272 qemu_cfg_legacy();
273
274 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400275 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100276 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400277 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400278 u32 e;
279 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400280 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500281 qemu_cfg_read(&qfile, sizeof(qfile));
282 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
283 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100284 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400285}