3 // Copyright (C) 2011 Citrix Systems.
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
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
18 #define INFO_PHYSICAL_ADDRESS 0x00001000
20 u32 xen_cpuid_base = 0;
21 unsigned long xen_hypercall_page = 0;
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 */
28 * Physical address of an array of tables_nr elements.
30 * Each element is a 32 bit value contianing the physical address
36 * Physical address of the e820 table, contains e820_nr entries.
42 static void validate_info(struct xen_seabios_info *t)
44 if ( memcmp(t->signature, "XenHVMSeaBIOS", 14) )
45 panic("Bad Xen info signature\n");
47 if ( t->length < sizeof(struct xen_seabios_info) )
48 panic("Bad Xen info length\n");
50 if (checksum(t, t->length) != 0)
51 panic("Bad Xen info checksum\n");
54 void xen_preinit(void)
56 u32 base, eax, ebx, ecx, edx;
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);
69 dprintf(9, "Found hypervisor signature \"%s\" at %x\n",
71 if (strcmp(signature, "XenVMMXenVMM") == 0) {
72 /* Set debug_io_port first, so the following messages work. */
73 DebugOutputPort = 0xe9;
75 dprintf(1, "\nFound Xen hypervisor signature at %x\n", base);
77 panic("Insufficient Xen cpuid leaves. eax=%x at base %x\n",
79 xen_cpuid_base = base;
83 if (!xen_cpuid_base) {
84 dprintf(1, "No Xen hypervisor found.\n");
87 PlatformRunningOn = PF_QEMU|PF_XEN;
90 static int hypercall_xen_version( int cmd, void *arg)
92 return _hypercall2(int, xen_version, cmd, arg);
95 /* Fill in hypercall transfer pages. */
96 void xen_hypercall_setup(void)
98 u32 eax, ebx, ecx, edx;
99 xen_extraversion_t extraversion;
105 cpuid(xen_cpuid_base + 2, &eax, &ebx, &ecx, &edx);
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");
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);
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);
121 void xen_biostable_setup(void)
123 struct xen_seabios_info *info = (void *)INFO_PHYSICAL_ADDRESS;
124 void **tables = (void*)info->tables;
127 dprintf(1, "xen: copy BIOS tables...\n");
128 for (i=0; i<info->tables_nr; i++)
129 copy_table(tables[i]);
131 find_acpi_features();
134 void xen_ramsize_preinit(void)
137 struct xen_seabios_info *info = (void *)INFO_PHYSICAL_ADDRESS;
138 struct e820entry *e820 = (struct e820entry *)info->e820;
141 dprintf(1, "xen: copy e820...\n");
143 for (i = 0; i < info->e820_nr; i++) {
144 struct e820entry *e = &e820[i];
145 add_e820(e->start, e->size, e->type);