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