1 | /** @file
|
---|
2 | Xen Hypercall Library implementation for Intel architecture
|
---|
3 |
|
---|
4 | Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 | #include <PiDxe.h>
|
---|
10 | #include <Library/BaseLib.h>
|
---|
11 | #include <Library/DebugLib.h>
|
---|
12 | #include <Library/CpuLib.h>
|
---|
13 |
|
---|
14 | static INTN mUseVmmCall = -1;
|
---|
15 | static BOOLEAN mHypercallAvail;
|
---|
16 |
|
---|
17 | //
|
---|
18 | // Interface exposed by the ASM implementation of the core hypercall
|
---|
19 | //
|
---|
20 | INTN
|
---|
21 | EFIAPI
|
---|
22 | __XenVmmcall2 (
|
---|
23 | IN INTN HypercallNum,
|
---|
24 | IN OUT INTN Arg1,
|
---|
25 | IN OUT INTN Arg2
|
---|
26 | );
|
---|
27 |
|
---|
28 | INTN
|
---|
29 | EFIAPI
|
---|
30 | __XenVmcall2 (
|
---|
31 | IN INTN HypercallNum,
|
---|
32 | IN OUT INTN Arg1,
|
---|
33 | IN OUT INTN Arg2
|
---|
34 | );
|
---|
35 |
|
---|
36 | /**
|
---|
37 | Check if the Xen Hypercall library is able to make calls to the Xen
|
---|
38 | hypervisor.
|
---|
39 |
|
---|
40 | Client code should call further functions in this library only if, and after,
|
---|
41 | this function returns TRUE.
|
---|
42 |
|
---|
43 | @retval TRUE Hypercalls are available.
|
---|
44 | @retval FALSE Hypercalls are not available.
|
---|
45 | **/
|
---|
46 | BOOLEAN
|
---|
47 | EFIAPI
|
---|
48 | XenHypercallIsAvailable (
|
---|
49 | VOID
|
---|
50 | )
|
---|
51 | {
|
---|
52 | return mHypercallAvail;
|
---|
53 | }
|
---|
54 |
|
---|
55 | STATIC
|
---|
56 | UINT32
|
---|
57 | XenCpuidLeaf (
|
---|
58 | VOID
|
---|
59 | )
|
---|
60 | {
|
---|
61 | UINT8 Signature[13];
|
---|
62 | UINT32 XenLeaf;
|
---|
63 |
|
---|
64 | Signature[12] = '\0';
|
---|
65 | for (XenLeaf = 0x40000000; XenLeaf < 0x40010000; XenLeaf += 0x100) {
|
---|
66 | AsmCpuid (
|
---|
67 | XenLeaf,
|
---|
68 | NULL,
|
---|
69 | (UINT32 *)&Signature[0],
|
---|
70 | (UINT32 *)&Signature[4],
|
---|
71 | (UINT32 *)&Signature[8]
|
---|
72 | );
|
---|
73 |
|
---|
74 | if (!AsciiStrCmp ((CHAR8 *)Signature, "XenVMMXenVMM")) {
|
---|
75 | return XenLeaf;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | Library constructor: Check for Xen leaf in CPUID
|
---|
84 | **/
|
---|
85 | RETURN_STATUS
|
---|
86 | EFIAPI
|
---|
87 | XenHypercallLibInit (
|
---|
88 | VOID
|
---|
89 | )
|
---|
90 | {
|
---|
91 | UINT32 XenLeaf;
|
---|
92 | CHAR8 sig[13];
|
---|
93 |
|
---|
94 | XenLeaf = XenCpuidLeaf ();
|
---|
95 | if (XenLeaf == 0) {
|
---|
96 | return RETURN_NOT_FOUND;
|
---|
97 | }
|
---|
98 |
|
---|
99 | sig[12] = '\0';
|
---|
100 | AsmCpuid (
|
---|
101 | 0,
|
---|
102 | NULL,
|
---|
103 | (UINT32 *)&sig[0],
|
---|
104 | (UINT32 *)&sig[8],
|
---|
105 | (UINT32 *)&sig[4]
|
---|
106 | );
|
---|
107 |
|
---|
108 | DEBUG ((DEBUG_INFO, "Detected CPU \"%12a\"\n", sig));
|
---|
109 |
|
---|
110 | if ((AsciiStrCmp ("AuthenticAMD", sig) == 0) ||
|
---|
111 | (AsciiStrCmp ("HygonGenuine", sig) == 0))
|
---|
112 | {
|
---|
113 | mUseVmmCall = TRUE;
|
---|
114 | } else if ((AsciiStrCmp ("GenuineIntel", sig) == 0) ||
|
---|
115 | (AsciiStrCmp ("CentaurHauls", sig) == 0) ||
|
---|
116 | (AsciiStrCmp (" Shanghai ", sig) == 0))
|
---|
117 | {
|
---|
118 | mUseVmmCall = FALSE;
|
---|
119 | } else {
|
---|
120 | DEBUG ((DEBUG_ERROR, "Unsupported CPU vendor\n"));
|
---|
121 | return RETURN_NOT_FOUND;
|
---|
122 | }
|
---|
123 |
|
---|
124 | mHypercallAvail = TRUE;
|
---|
125 |
|
---|
126 | return RETURN_SUCCESS;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | This function will put the two arguments in the right place (registers) and
|
---|
131 | invoke the hypercall identified by HypercallID.
|
---|
132 |
|
---|
133 | @param HypercallID The symbolic ID of the hypercall to be invoked
|
---|
134 | @param Arg1 First argument.
|
---|
135 | @param Arg2 Second argument.
|
---|
136 |
|
---|
137 | @return Return 0 if success otherwise it return an errno.
|
---|
138 | **/
|
---|
139 | INTN
|
---|
140 | EFIAPI
|
---|
141 | XenHypercall2 (
|
---|
142 | IN UINTN HypercallID,
|
---|
143 | IN OUT INTN Arg1,
|
---|
144 | IN OUT INTN Arg2
|
---|
145 | )
|
---|
146 | {
|
---|
147 | if (mUseVmmCall) {
|
---|
148 | return __XenVmmcall2 (HypercallID, Arg1, Arg2);
|
---|
149 | } else {
|
---|
150 | return __XenVmcall2 (HypercallID, Arg1, Arg2);
|
---|
151 | }
|
---|
152 | }
|
---|