blob: f181e5ff4ab8f567336e60e67ddc5ac0f980527f [file] [log] [blame]
Ian Campbell74c78782011-06-01 11:00:29 +01001// Xen HVM support
2//
3// Copyright (C) 2011 Citrix Systems.
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "config.h"
8#include "xen.h"
Kevin O'Connor02313b22013-02-07 22:42:25 -05009#include "paravirt.h" // PlatformRunningOn
Ian Campbell74c78782011-06-01 11:00:29 +010010#include "memmap.h" // add_e820
11#include "types.h" // ASM32FLAT
12#include "util.h" // copy_acpi_rsdp
David Woodhoused304fe42013-02-23 00:24:48 +000013#include "acpi.h" // find_acpi_features
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040014#include "string.h" // memcpy
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040015#include "x86.h" // cpuid
Ian Campbell74c78782011-06-01 11:00:29 +010016
17#define INFO_PHYSICAL_ADDRESS 0x00001000
18
19u32 xen_cpuid_base = 0;
Ian Campbell9166c4a2012-06-27 12:08:49 +010020unsigned long xen_hypercall_page = 0;
Ian Campbell74c78782011-06-01 11:00:29 +010021
22struct xen_seabios_info {
23 char signature[14]; /* XenHVMSeaBIOS\0 */
24 u8 length; /* Length of this struct */
25 u8 checksum; /* Set such that the sum over bytes 0..length == 0 */
26 /*
27 * Physical address of an array of tables_nr elements.
28 *
29 * Each element is a 32 bit value contianing the physical address
30 * of a BIOS table.
31 */
32 u32 tables;
33 u32 tables_nr;
34 /*
35 * Physical address of the e820 table, contains e820_nr entries.
36 */
37 u32 e820;
38 u32 e820_nr;
39} PACKED;
40
41static void validate_info(struct xen_seabios_info *t)
42{
43 if ( memcmp(t->signature, "XenHVMSeaBIOS", 14) )
44 panic("Bad Xen info signature\n");
45
46 if ( t->length < sizeof(struct xen_seabios_info) )
47 panic("Bad Xen info length\n");
48
49 if (checksum(t, t->length) != 0)
50 panic("Bad Xen info checksum\n");
51}
52
Kevin O'Connord83c87b2013-01-21 01:14:12 -050053void xen_preinit(void)
Ian Campbell74c78782011-06-01 11:00:29 +010054{
55 u32 base, eax, ebx, ecx, edx;
56 char signature[13];
57
58 if (!CONFIG_XEN)
59 return;
60
61 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
62 cpuid(base, &eax, &ebx, &ecx, &edx);
63 memcpy(signature + 0, &ebx, 4);
64 memcpy(signature + 4, &ecx, 4);
65 memcpy(signature + 8, &edx, 4);
66 signature[12] = 0;
67
Kevin O'Connor36289332012-07-21 13:41:16 -040068 dprintf(9, "Found hypervisor signature \"%s\" at %x\n",
Ian Campbell74c78782011-06-01 11:00:29 +010069 signature, base);
70 if (strcmp(signature, "XenVMMXenVMM") == 0) {
Ian Campbell54f3b252012-06-28 11:08:32 +010071 /* Set debug_io_port first, so the following messages work. */
72 DebugOutputPort = 0xe9;
73 dprintf(1, "SeaBIOS (version %s)\n\n", VERSION);
74 dprintf(1, "Found Xen hypervisor signature at %x\n", base);
Ian Campbell74c78782011-06-01 11:00:29 +010075 if ((eax - base) < 2)
76 panic("Insufficient Xen cpuid leaves. eax=%x at base %x\n",
77 eax, base);
78 xen_cpuid_base = base;
79 break;
80 }
81 }
Kevin O'Connor02313b22013-02-07 22:42:25 -050082 if (!xen_cpuid_base) {
Ian Campbell54f3b252012-06-28 11:08:32 +010083 dprintf(1, "No Xen hypervisor found.\n");
Kevin O'Connor02313b22013-02-07 22:42:25 -050084 return;
85 }
86 PlatformRunningOn = PF_QEMU|PF_XEN;
Ian Campbell74c78782011-06-01 11:00:29 +010087}
88
89static int hypercall_xen_version( int cmd, void *arg)
90{
91 return _hypercall2(int, xen_version, cmd, arg);
92}
93
94/* Fill in hypercall transfer pages. */
Kevin O'Connord83c87b2013-01-21 01:14:12 -050095void xen_hypercall_setup(void)
Ian Campbell74c78782011-06-01 11:00:29 +010096{
97 u32 eax, ebx, ecx, edx;
98 xen_extraversion_t extraversion;
99 unsigned long i;
100
Kevin O'Connor897fb112013-02-07 23:32:48 -0500101 if (!runningOnXen())
Ian Campbell74c78782011-06-01 11:00:29 +0100102 return;
103
104 cpuid(xen_cpuid_base + 2, &eax, &ebx, &ecx, &edx);
105
106 xen_hypercall_page = (unsigned long)memalign_high(PAGE_SIZE, eax*PAGE_SIZE);
107 if (!xen_hypercall_page)
108 panic("unable to allocate Xen hypercall page\n");
109
110 dprintf(1, "Allocated Xen hypercall page at %lx\n", xen_hypercall_page);
111 for ( i = 0; i < eax; i++ )
112 wrmsr(ebx, xen_hypercall_page + (i << 12) + i);
113
114 /* Print version information. */
115 cpuid(xen_cpuid_base + 1, &eax, &ebx, &ecx, &edx);
116 hypercall_xen_version(XENVER_extraversion, extraversion);
117 dprintf(1, "Detected Xen v%u.%u%s\n", eax >> 16, eax & 0xffff, extraversion);
118}
119
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500120void xen_biostable_setup(void)
Ian Campbell74c78782011-06-01 11:00:29 +0100121{
122 struct xen_seabios_info *info = (void *)INFO_PHYSICAL_ADDRESS;
Kevin O'Connor4d053eb2012-06-09 13:08:02 -0400123 void **tables = (void*)info->tables;
Ian Campbell74c78782011-06-01 11:00:29 +0100124 int i;
125
126 dprintf(1, "xen: copy BIOS tables...\n");
Kevin O'Connor4d053eb2012-06-09 13:08:02 -0400127 for (i=0; i<info->tables_nr; i++)
128 copy_table(tables[i]);
David Woodhousef34f0692013-02-05 16:14:06 +0000129
David Woodhoused304fe42013-02-23 00:24:48 +0000130 find_acpi_features();
Ian Campbell74c78782011-06-01 11:00:29 +0100131}
132
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500133void xen_ramsize_preinit(void)
Ian Campbell74c78782011-06-01 11:00:29 +0100134{
Ian Campbell74c78782011-06-01 11:00:29 +0100135 int i;
136 struct xen_seabios_info *info = (void *)INFO_PHYSICAL_ADDRESS;
137 struct e820entry *e820 = (struct e820entry *)info->e820;
138 validate_info(info);
139
140 dprintf(1, "xen: copy e820...\n");
141
142 for (i = 0; i < info->e820_nr; i++) {
143 struct e820entry *e = &e820[i];
Ian Campbell74c78782011-06-01 11:00:29 +0100144 add_e820(e->start, e->size, e->type);
145 }
Ian Campbell74c78782011-06-01 11:00:29 +0100146}