Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / fw / xen.c
1 // 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 "hw/serialio.h" // DebugOutputPort
9 #include "malloc.h" // memalign_high
10 #include "memmap.h" // add_e820
11 #include "output.h" // dprintf
12 #include "paravirt.h" // PlatformRunningOn
13 #include "string.h" // memcpy
14 #include "util.h" // copy_acpi_rsdp
15 #include "x86.h" // cpuid
16 #include "xen.h"
17
18 #define INFO_PHYSICAL_ADDRESS 0x00001000
19
20 u32 xen_cpuid_base = 0;
21 unsigned long xen_hypercall_page = 0;
22
23 struct xen_seabios_info {
24     char signature[14]; /* XenHVMSeaBIOS\0 */
25     u8 length;     /* Length of this struct */
26     u8 checksum;   /* Set such that the sum over bytes 0..length == 0 */
27     /*
28      * Physical address of an array of tables_nr elements.
29      *
30      * Each element is a 32 bit value contianing the physical address
31      * of a BIOS table.
32      */
33     u32 tables;
34     u32 tables_nr;
35     /*
36      * Physical address of the e820 table, contains e820_nr entries.
37      */
38     u32 e820;
39     u32 e820_nr;
40 } PACKED;
41
42 static void validate_info(struct xen_seabios_info *t)
43 {
44     if ( memcmp(t->signature, "XenHVMSeaBIOS", 14) )
45         panic("Bad Xen info signature\n");
46
47     if ( t->length < sizeof(struct xen_seabios_info) )
48         panic("Bad Xen info length\n");
49
50     if (checksum(t, t->length) != 0)
51         panic("Bad Xen info checksum\n");
52 }
53
54 void xen_preinit(void)
55 {
56     u32 base, eax, ebx, ecx, edx;
57     char signature[13];
58
59     if (!CONFIG_XEN)
60         return;
61
62     for (base = 0x40000000; base < 0x40010000; base += 0x100) {
63         cpuid(base, &eax, &ebx, &ecx, &edx);
64         memcpy(signature + 0, &ebx, 4);
65         memcpy(signature + 4, &ecx, 4);
66         memcpy(signature + 8, &edx, 4);
67         signature[12] = 0;
68
69         dprintf(9, "Found hypervisor signature \"%s\" at %x\n",
70                 signature, base);
71         if (strcmp(signature, "XenVMMXenVMM") == 0) {
72             /* Set debug_io_port first, so the following messages work. */
73             DebugOutputPort = 0xe9;
74             debug_banner();
75             dprintf(1, "\nFound Xen hypervisor signature at %x\n", base);
76             if ((eax - base) < 2)
77                 panic("Insufficient Xen cpuid leaves. eax=%x at base %x\n",
78                       eax, base);
79             xen_cpuid_base = base;
80             break;
81         }
82     }
83     if (!xen_cpuid_base) {
84         dprintf(1, "No Xen hypervisor found.\n");
85         return;
86     }
87     PlatformRunningOn = PF_QEMU|PF_XEN;
88 }
89
90 static int hypercall_xen_version( int cmd, void *arg)
91 {
92     return _hypercall2(int, xen_version, cmd, arg);
93 }
94
95 /* Fill in hypercall transfer pages. */
96 void xen_hypercall_setup(void)
97 {
98     u32 eax, ebx, ecx, edx;
99     xen_extraversion_t extraversion;
100     unsigned long i;
101
102     if (!runningOnXen())
103         return;
104
105     cpuid(xen_cpuid_base + 2, &eax, &ebx, &ecx, &edx);
106
107     xen_hypercall_page = (unsigned long)memalign_high(PAGE_SIZE, eax*PAGE_SIZE);
108     if (!xen_hypercall_page)
109         panic("unable to allocate Xen hypercall page\n");
110
111     dprintf(1, "Allocated Xen hypercall page at %lx\n", xen_hypercall_page);
112     for ( i = 0; i < eax; i++ )
113         wrmsr(ebx, xen_hypercall_page + (i << 12) + i);
114
115     /* Print version information. */
116     cpuid(xen_cpuid_base + 1, &eax, &ebx, &ecx, &edx);
117     hypercall_xen_version(XENVER_extraversion, extraversion);
118     dprintf(1, "Detected Xen v%u.%u%s\n", eax >> 16, eax & 0xffff, extraversion);
119 }
120
121 void xen_biostable_setup(void)
122 {
123     struct xen_seabios_info *info = (void *)INFO_PHYSICAL_ADDRESS;
124     void **tables = (void*)info->tables;
125     int i;
126
127     dprintf(1, "xen: copy BIOS tables...\n");
128     for (i=0; i<info->tables_nr; i++)
129         copy_table(tables[i]);
130
131     find_acpi_features();
132 }
133
134 void xen_ramsize_preinit(void)
135 {
136     int i;
137     struct xen_seabios_info *info = (void *)INFO_PHYSICAL_ADDRESS;
138     struct e820entry *e820 = (struct e820entry *)info->e820;
139     validate_info(info);
140
141     dprintf(1, "xen: copy e820...\n");
142
143     for (i = 0; i < info->e820_nr; i++) {
144         struct e820entry *e = &e820[i];
145         add_e820(e->start, e->size, e->type);
146     }
147 }