]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFspWrapperPkg/Library/BaseFspApiLib/X64/DispatchExecute.c
DynamicTablesPkg: GTDT updates for ACPI 6.3
[mirror_edk2.git] / IntelFspWrapperPkg / Library / BaseFspApiLib / 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 - 2015, 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 <FspApi.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 AsmExecute32BitCode (
65 IN UINT64 Function,
66 IN UINT64 Param1,
67 IN UINT64 Param2,
68 IN IA32_DESCRIPTOR *InternalGdtr
69 );
70
71 /**
72 Wrapper for a thunk to transition from long mode to compatibility mode to execute 32-bit code and then transit back to
73 long mode.
74
75 @param[in] Function The 32bit code entry to be executed.
76 @param[in] Param1 The first parameter to pass to 32bit code.
77
78 @return EFI_STATUS.
79 **/
80 EFI_STATUS
81 Execute32BitCode (
82 IN UINT64 Function,
83 IN UINT64 Param1
84 )
85 {
86 EFI_STATUS Status;
87 IA32_DESCRIPTOR Idtr;
88
89 //
90 // Idtr might be changed inside of FSP. 32bit FSP only knows the <4G address.
91 // If IDTR.Base is >4G, FSP can not handle. So we need save/restore IDTR here for X64 only.
92 // Interrupt is already disabled here, so it is safety to update IDTR.
93 //
94 AsmReadIdtr (&Idtr);
95 Status = AsmExecute32BitCode (Function, Param1, 0, &mGdt);
96 AsmWriteIdtr (&Idtr);
97
98 return Status;
99 }
100