]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PlatformPei/Xen.c
OvmfPkg/PlatformPei: Hide Xen Leaf details
[mirror_edk2.git] / OvmfPkg / PlatformPei / Xen.c
1 /**@file
2 Xen Platform PEI support
3
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 //
18 // The package level header files this module uses
19 //
20 #include <PiPei.h>
21
22 //
23 // The Library classes this module consumes
24 //
25 #include <Library/DebugLib.h>
26 #include <Library/HobLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/PcdLib.h>
29 #include <Guid/XenInfo.h>
30
31 #include "Platform.h"
32 #include "Xen.h"
33
34 BOOLEAN mXen = FALSE;
35
36 STATIC UINT32 mXenLeaf = 0;
37
38 EFI_XEN_INFO mXenInfo;
39
40 /**
41 Returns E820 map provided by Xen
42
43 @param Entries Pointer to E820 map
44 @param Count Number of entries
45
46 @return EFI_STATUS
47 **/
48 EFI_STATUS
49 XenGetE820Map (
50 EFI_E820_ENTRY64 **Entries,
51 UINT32 *Count
52 )
53 {
54 EFI_XEN_OVMF_INFO *Info =
55 (EFI_XEN_OVMF_INFO *)(UINTN) OVMF_INFO_PHYSICAL_ADDRESS;
56
57 if (AsciiStrCmp ((CHAR8 *) Info->Signature, "XenHVMOVMF")) {
58 return EFI_NOT_FOUND;
59 }
60
61 ASSERT (Info->E820 < MAX_ADDRESS);
62 *Entries = (EFI_E820_ENTRY64 *)(UINTN) Info->E820;
63 *Count = Info->E820EntriesCount;
64
65 return EFI_SUCCESS;
66 }
67
68 /**
69 Connects to the Hypervisor.
70
71 @param XenLeaf CPUID index used to connect.
72
73 @return EFI_STATUS
74
75 **/
76 EFI_STATUS
77 XenConnect (
78 UINT32 XenLeaf
79 )
80 {
81 UINT32 Index;
82 UINT32 TransferReg;
83 UINT32 TransferPages;
84 UINT32 XenVersion;
85
86 AsmCpuid (XenLeaf + 2, &TransferPages, &TransferReg, NULL, NULL);
87 mXenInfo.HyperPages = AllocatePages (TransferPages);
88 if (!mXenInfo.HyperPages) {
89 return EFI_OUT_OF_RESOURCES;
90 }
91
92 for (Index = 0; Index < TransferPages; Index++) {
93 AsmWriteMsr64 (TransferReg,
94 (UINTN) mXenInfo.HyperPages +
95 (Index << EFI_PAGE_SHIFT) + Index);
96 }
97
98 AsmCpuid (XenLeaf + 1, &XenVersion, NULL, NULL, NULL);
99 DEBUG ((EFI_D_ERROR, "Detected Xen version %d.%d\n",
100 XenVersion >> 16, XenVersion & 0xFFFF));
101 mXenInfo.VersionMajor = (UINT16)(XenVersion >> 16);
102 mXenInfo.VersionMinor = (UINT16)(XenVersion & 0xFFFF);
103
104 /* TBD: Locate hvm_info and reserve it away. */
105 mXenInfo.HvmInfo = NULL;
106
107 BuildGuidDataHob (
108 &gEfiXenInfoGuid,
109 &mXenInfo,
110 sizeof(mXenInfo)
111 );
112
113 return EFI_SUCCESS;
114 }
115
116 /**
117 Figures out if we are running inside Xen HVM.
118
119 @retval TRUE Xen was detected
120 @retval FALSE Xen was not detected
121
122 **/
123 BOOLEAN
124 XenDetect (
125 VOID
126 )
127 {
128 UINT8 Signature[13];
129
130 if (mXenLeaf != 0) {
131 return TRUE;
132 }
133
134 Signature[12] = '\0';
135 for (mXenLeaf = 0x40000000; mXenLeaf < 0x40010000; mXenLeaf += 0x100) {
136 AsmCpuid (mXenLeaf,
137 NULL,
138 (UINT32 *) &Signature[0],
139 (UINT32 *) &Signature[4],
140 (UINT32 *) &Signature[8]);
141
142 if (!AsciiStrCmp ((CHAR8 *) Signature, "XenVMMXenVMM")) {
143 mXen = TRUE;
144 return TRUE;
145 }
146 }
147
148 mXenLeaf = 0;
149 return FALSE;
150 }
151
152 /**
153 Perform Xen PEI initialization.
154
155 @return EFI_SUCCESS Xen initialized successfully
156 @return EFI_NOT_FOUND Not running under Xen
157
158 **/
159 EFI_STATUS
160 InitializeXen (
161 VOID
162 )
163 {
164 if (mXenLeaf == 0) {
165 return EFI_NOT_FOUND;
166 }
167
168 XenConnect (mXenLeaf);
169
170 //
171 // Reserve away HVMLOADER reserved memory [0xFC000000,0xFD000000).
172 // This needs to match HVMLOADER RESERVED_MEMBASE/RESERVED_MEMSIZE.
173 //
174 AddReservedMemoryBaseSizeHob (0xFC000000, 0x1000000);
175
176 return EFI_SUCCESS;
177 }