]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PlatformPei/Xen.c
OvmfPkg: introduce XenMemMapInitialization
[mirror_edk2.git] / OvmfPkg / PlatformPei / Xen.c
1 /**@file
2 Xen Platform PEI support
3
4 Copyright (c) 2006 - 2013, 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 EFI_XEN_INFO mXenInfo;
35
36 /**
37 Returns E820 map provided by Xen
38
39 @param Entries Pointer to E820 map
40 @param Count Number of entries
41
42 @return EFI_STATUS
43 **/
44 EFI_STATUS
45 XenGetE820Map (
46 EFI_E820_ENTRY64 **Entries,
47 UINT32 *Count
48 )
49 {
50 EFI_XEN_OVMF_INFO *Info =
51 (EFI_XEN_OVMF_INFO *)(UINTN) OVMF_INFO_PHYSICAL_ADDRESS;
52
53 if (AsciiStrCmp ((CHAR8 *) Info->Signature, "XenHVMOVMF")) {
54 return EFI_NOT_FOUND;
55 }
56
57 ASSERT (Info->E820 < MAX_ADDRESS);
58 *Entries = (EFI_E820_ENTRY64 *)(UINTN) Info->E820;
59 *Count = Info->E820EntriesCount;
60
61 return EFI_SUCCESS;
62 }
63
64 /**
65 Connects to the Hypervisor.
66
67 @param XenLeaf CPUID index used to connect.
68
69 @return EFI_STATUS
70
71 **/
72 EFI_STATUS
73 XenConnect (
74 UINT32 XenLeaf
75 )
76 {
77 UINT32 Index;
78 UINT32 TransferReg;
79 UINT32 TransferPages;
80 UINT32 XenVersion;
81
82 AsmCpuid (XenLeaf + 2, &TransferPages, &TransferReg, NULL, NULL);
83 mXenInfo.HyperPages = AllocatePages (TransferPages);
84 if (!mXenInfo.HyperPages) {
85 return EFI_OUT_OF_RESOURCES;
86 }
87
88 for (Index = 0; Index < TransferPages; Index++) {
89 AsmWriteMsr64 (TransferReg,
90 (UINTN) mXenInfo.HyperPages +
91 (Index << EFI_PAGE_SHIFT) + Index);
92 }
93
94 AsmCpuid (XenLeaf + 1, &XenVersion, NULL, NULL, NULL);
95 DEBUG ((EFI_D_ERROR, "Detected Xen version %d.%d\n",
96 XenVersion >> 16, XenVersion & 0xFFFF));
97 mXenInfo.VersionMajor = (UINT16)(XenVersion >> 16);
98 mXenInfo.VersionMinor = (UINT16)(XenVersion & 0xFFFF);
99
100 /* TBD: Locate hvm_info and reserve it away. */
101 mXenInfo.HvmInfo = NULL;
102
103 BuildGuidDataHob (
104 &gEfiXenInfoGuid,
105 &mXenInfo,
106 sizeof(mXenInfo)
107 );
108
109 return EFI_SUCCESS;
110 }
111
112 /**
113 Figures out if we are running inside Xen HVM.
114
115 @return UINT32 CPUID index used to connect to HV.
116
117 **/
118 UINT32
119 XenDetect (
120 VOID
121 )
122 {
123
124 UINT32 XenLeaf;
125 UINT8 Signature[13];
126
127 for (XenLeaf = 0x40000000; XenLeaf < 0x40010000; XenLeaf += 0x100) {
128 AsmCpuid (XenLeaf, NULL, (UINT32 *) &Signature[0],
129 (UINT32 *) &Signature[4],
130 (UINT32 *) &Signature[8]);
131 Signature[12] = '\0';
132
133 if (!AsciiStrCmp ((CHAR8 *) Signature, "XenVMMXenVMM")) {
134 return XenLeaf;
135 }
136 }
137
138 return 0;
139 }
140
141 /**
142 Perform Xen PEI initialization.
143
144 @return EFI_SUCCESS Xen initialized successfully
145 @return EFI_NOT_FOUND Not running under Xen
146
147 **/
148 EFI_STATUS
149 InitializeXen (
150 UINT32 XenLeaf
151 )
152 {
153 XenConnect (XenLeaf);
154
155 //
156 // Reserve away HVMLOADER reserved memory [0xFC000000,0xFD000000).
157 // This needs to match HVMLOADER RESERVED_MEMBASE/RESERVED_MEMSIZE.
158 //
159 AddReservedMemoryBaseSizeHob (0xFC000000, 0x1000000);
160
161 return EFI_SUCCESS;
162 }