]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFspWrapperPkg/Library/BaseFspApiLib/X64/DispatchExecute.c
IntelFspWrapperPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFspWrapperPkg / Library / BaseFspApiLib / X64 / DispatchExecute.c
CommitLineData
a33a2f62
JY
1/** @file\r
2 Execute 32-bit code in Long Mode.\r
3 Provide a thunk function to transition from long mode to compatibility mode to execute 32-bit code and then transit\r
4 back to long mode.\r
5\r
d8043ce9 6 Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>\r
19486360 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
a33a2f62
JY
8\r
9**/\r
10\r
11#include <Uefi.h>\r
12#include <Library/BaseLib.h>\r
13#include <FspApi.h>\r
14\r
15#pragma pack(1)\r
16typedef union {\r
17 struct {\r
18 UINT32 LimitLow : 16;\r
19 UINT32 BaseLow : 16;\r
20 UINT32 BaseMid : 8;\r
21 UINT32 Type : 4;\r
22 UINT32 System : 1;\r
23 UINT32 Dpl : 2;\r
24 UINT32 Present : 1;\r
25 UINT32 LimitHigh : 4;\r
26 UINT32 Software : 1;\r
27 UINT32 Reserved : 1;\r
28 UINT32 DefaultSize : 1;\r
29 UINT32 Granularity : 1;\r
30 UINT32 BaseHigh : 8;\r
31 } Bits;\r
32 UINT64 Uint64;\r
33} IA32_GDT;\r
34#pragma pack()\r
35\r
36GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT mGdtEntries[] = {\r
37 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 0x0: reserve */\r
38 {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}}, /* 0x8: compatibility mode */\r
39 {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 1, 0, 1, 0}}, /* 0x10: for long mode */\r
40 {{0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}}, /* 0x18: data */\r
41 {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, /* 0x20: reserve */\r
42};\r
43\r
44//\r
45// IA32 Gdt register\r
46//\r
47GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR mGdt = {\r
48 sizeof (mGdtEntries) - 1,\r
49 (UINTN) mGdtEntries\r
50 };\r
51\r
52/**\r
53 Assembly function to transition from long mode to compatibility mode to execute 32-bit code and then transit back to\r
54 long mode.\r
55\r
56 @param[in] Function The 32bit code entry to be executed.\r
57 @param[in] Param1 The first parameter to pass to 32bit code\r
58 @param[in] Param2 The second parameter to pass to 32bit code\r
59 @param[in] InternalGdtr The GDT and GDT descriptor used by this library\r
60\r
61 @return status.\r
62**/\r
63UINT32\r
64AsmExecute32BitCode (\r
65 IN UINT64 Function,\r
66 IN UINT64 Param1,\r
67 IN UINT64 Param2,\r
68 IN IA32_DESCRIPTOR *InternalGdtr\r
69 );\r
70\r
71/**\r
72 Wrapper for a thunk to transition from long mode to compatibility mode to execute 32-bit code and then transit back to\r
73 long mode.\r
74\r
75 @param[in] Function The 32bit code entry to be executed.\r
76 @param[in] Param1 The first parameter to pass to 32bit code.\r
77\r
d8043ce9 78 @return EFI_STATUS.\r
a33a2f62 79**/\r
d8043ce9 80EFI_STATUS\r
a33a2f62
JY
81Execute32BitCode (\r
82 IN UINT64 Function,\r
83 IN UINT64 Param1\r
84 )\r
85{\r
3b567f08
JY
86 EFI_STATUS Status;\r
87 IA32_DESCRIPTOR Idtr;\r
88\r
89 //\r
90 // Idtr might be changed inside of FSP. 32bit FSP only knows the <4G address.\r
91 // If IDTR.Base is >4G, FSP can not handle. So we need save/restore IDTR here for X64 only.\r
92 // Interrupt is already disabled here, so it is safety to update IDTR.\r
93 //\r
94 AsmReadIdtr (&Idtr);\r
95 Status = AsmExecute32BitCode (Function, Param1, 0, &mGdt);\r
96 AsmWriteIdtr (&Idtr);\r
97\r
98 return Status;\r
a33a2f62
JY
99}\r
100\r