]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe/X64/SetIdtEntry.c
TempRam Done PPI definition in PI1.2.1
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / BootScriptExecutorDxe / X64 / SetIdtEntry.c
CommitLineData
be46cd5f 1/** @file\r
2 Set a IDT entry for debug purpose\r
3\r
4 Set a IDT entry for interrupt vector 3 for debug purpose for x64 platform\r
5\r
73f0127f 6Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r
be46cd5f 7\r
8This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17#include "ScriptExecute.h"\r
d0bf5623
JY
18\r
19#define IA32_PG_P BIT0\r
20#define IA32_PG_RW BIT1\r
21#define IA32_PG_PS BIT7\r
22\r
23UINT64 mPhyMask;\r
24BOOLEAN mPage1GSupport;\r
25VOID *mOriginalHandler;\r
26UINTN mS3NvsPageTableAddress;\r
27\r
54e27ada
JY
28/**\r
29 Page fault handler.\r
30\r
31**/\r
d0bf5623
JY
32VOID\r
33EFIAPI\r
34PageFaultHandlerHook (\r
35 VOID\r
36 );\r
37\r
54e27ada
JY
38/**\r
39 Hook IDT with our page fault handler so that the on-demand paging works on page fault.\r
40\r
41 @param IdtEntry a pointer to IDT entry\r
42\r
43**/\r
d0bf5623
JY
44VOID\r
45HookPageFaultHandler (\r
68cc1ba3 46 IN IA32_IDT_GATE_DESCRIPTOR *IdtEntry\r
d0bf5623
JY
47 )\r
48{\r
49 UINT32 RegEax;\r
50 UINT32 RegEdx;\r
73f0127f 51 UINTN PageFaultHandlerHookAddress;\r
d0bf5623
JY
52\r
53 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
54 mPhyMask = LShiftU64 (1, (UINT8)RegEax) - 1;\r
55 mPhyMask &= (1ull << 48) - SIZE_4KB;\r
56\r
57 mPage1GSupport = FALSE;\r
58 if (PcdGetBool(PcdUse1GPageTable)) {\r
59 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
60 if (RegEax >= 0x80000001) {\r
61 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
62 if ((RegEdx & BIT26) != 0) {\r
63 mPage1GSupport = TRUE;\r
64 }\r
65 }\r
66 }\r
67\r
68 //\r
69 // Set Page Fault entry to catch >4G access\r
70 //\r
73f0127f 71 PageFaultHandlerHookAddress = (UINTN)PageFaultHandlerHook;\r
68cc1ba3 72 mOriginalHandler = (VOID *)(UINTN)(LShiftU64 (IdtEntry->Bits.OffsetUpper, 32) + IdtEntry->Bits.OffsetLow + (IdtEntry->Bits.OffsetHigh << 16));\r
73f0127f 73 IdtEntry->Bits.OffsetLow = (UINT16)PageFaultHandlerHookAddress;\r
68cc1ba3
SZ
74 IdtEntry->Bits.Selector = (UINT16)AsmReadCs ();\r
75 IdtEntry->Bits.Reserved_0 = 0;\r
76 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;\r
73f0127f
SZ
77 IdtEntry->Bits.OffsetHigh = (UINT16)(PageFaultHandlerHookAddress >> 16);\r
78 IdtEntry->Bits.OffsetUpper = (UINT32)(PageFaultHandlerHookAddress >> 32);\r
68cc1ba3 79 IdtEntry->Bits.Reserved_1 = 0;\r
d0bf5623
JY
80\r
81 if (mPage1GSupport) {\r
82 mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE(2);\r
83 }else {\r
84 mS3NvsPageTableAddress = (UINTN)(AsmReadCr3 () & mPhyMask) + EFI_PAGES_TO_SIZE(6);\r
85 }\r
86}\r
87\r
be46cd5f 88/**\r
89 Set a IDT entry for interrupt vector 3 for debug purpose.\r
90\r
91 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT\r
92\r
93**/\r
94VOID\r
95SetIdtEntry (\r
96 IN ACPI_S3_CONTEXT *AcpiS3Context\r
97 )\r
98{\r
68cc1ba3 99 IA32_IDT_GATE_DESCRIPTOR *IdtEntry;\r
be46cd5f 100 IA32_DESCRIPTOR *IdtDescriptor;\r
101 UINTN S3DebugBuffer;\r
102\r
103 //\r
104 // Restore IDT for debug\r
105 //\r
106 IdtDescriptor = (IA32_DESCRIPTOR *) (UINTN) (AcpiS3Context->IdtrProfile);\r
1e172d6b 107 AsmWriteIdtr (IdtDescriptor);\r
108\r
109 //\r
110 // Setup the default CPU exception handlers\r
111 //\r
112 SetupCpuExceptionHandlers ();\r
113\r
f4a25e81 114 DEBUG_CODE (\r
115 //\r
116 // Update IDT entry INT3 if the instruction is valid in it\r
117 //\r
118 S3DebugBuffer = (UINTN) (AcpiS3Context->S3DebugBufferAddress);\r
119 if (*(UINTN *)S3DebugBuffer != (UINTN) -1) {\r
68cc1ba3
SZ
120 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (3 * sizeof (IA32_IDT_GATE_DESCRIPTOR)));\r
121 IdtEntry->Bits.OffsetLow = (UINT16)S3DebugBuffer;\r
122 IdtEntry->Bits.Selector = (UINT16)AsmReadCs ();\r
123 IdtEntry->Bits.Reserved_0 = 0;\r
124 IdtEntry->Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;\r
125 IdtEntry->Bits.OffsetHigh = (UINT16)(S3DebugBuffer >> 16);\r
126 IdtEntry->Bits.OffsetUpper = (UINT32)(S3DebugBuffer >> 32);\r
127 IdtEntry->Bits.Reserved_1 = 0;\r
f4a25e81 128 }\r
129 );\r
be46cd5f 130\r
68cc1ba3 131 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *)(IdtDescriptor->Base + (14 * sizeof (IA32_IDT_GATE_DESCRIPTOR)));\r
d0bf5623 132 HookPageFaultHandler (IdtEntry);\r
d0bf5623
JY
133}\r
134\r
54e27ada
JY
135/**\r
136 Get new page address.\r
137\r
138 @param PageNum new page number needed\r
139\r
140 @return new page address\r
141**/\r
d0bf5623
JY
142UINTN\r
143GetNewPage (\r
144 IN UINTN PageNum\r
145 )\r
146{\r
147 UINTN NewPage;\r
148 NewPage = mS3NvsPageTableAddress;\r
149 ZeroMem ((VOID *)NewPage, EFI_PAGES_TO_SIZE(PageNum));\r
150 mS3NvsPageTableAddress += EFI_PAGES_TO_SIZE(PageNum);\r
151 return NewPage;\r
be46cd5f 152}\r
153\r
54e27ada
JY
154/**\r
155 The page fault handler that on-demand read >4G memory/MMIO.\r
156 \r
157 @retval TRUE The page fault is correctly handled.\r
158 @retval FALSE The page fault is not handled and is passed through to original handler.\r
159\r
160**/\r
d0bf5623
JY
161BOOLEAN\r
162EFIAPI\r
163PageFaultHandler (\r
164 VOID\r
165 )\r
166{\r
167 UINT64 *PageTable;\r
168 UINT64 PFAddress;\r
169 UINTN PTIndex;\r
170\r
171 PFAddress = AsmReadCr2 ();\r
172 DEBUG ((EFI_D_ERROR, "BootScript - PageFaultHandler: Cr2 - %lx\n", PFAddress));\r
173\r
174 if (PFAddress >= mPhyMask + SIZE_4KB) {\r
175 return FALSE;\r
176 }\r
177 PFAddress &= mPhyMask;\r
178\r
179 PageTable = (UINT64*)(UINTN)(AsmReadCr3 () & mPhyMask);\r
180\r
181 PTIndex = BitFieldRead64 (PFAddress, 39, 47);\r
182 // PML4E\r
183 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {\r
184 PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;\r
185 }\r
186 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);\r
187 PTIndex = BitFieldRead64 (PFAddress, 30, 38);\r
188 // PDPTE\r
189 if (mPage1GSupport) {\r
190 PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;\r
191 } else {\r
192 if ((PageTable[PTIndex] & IA32_PG_P) == 0) {\r
193 PageTable[PTIndex] = GetNewPage (1) | IA32_PG_P | IA32_PG_RW;\r
194 }\r
195 PageTable = (UINT64*)(UINTN)(PageTable[PTIndex] & mPhyMask);\r
196 PTIndex = BitFieldRead64 (PFAddress, 21, 29);\r
197 // PD\r
198 PageTable[PTIndex] = PFAddress | IA32_PG_P | IA32_PG_RW | IA32_PG_PS;\r
199 }\r
200\r
201 return TRUE;\r
202}\r