]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Csm/LegacyBiosDxe/Ipf/Thunk.c
EmbeddedPkg: Removing ipf which is no longer supported from edk2.
[mirror_edk2.git] / IntelFrameworkModulePkg / Csm / LegacyBiosDxe / Ipf / Thunk.c
CommitLineData
bcecde14 1/** @file\r
2 Call into 16-bit BIOS code\r
3\r
4 BugBug: Thunker does A20 gate. Can we get rid of this code or\r
5 put it into Legacy16 code.\r
6\r
1a45b15e 7Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR>\r
bcecde14 8\r
9This program and the accompanying materials\r
10are licensed and made available under the terms and conditions\r
11of the BSD License which accompanies this distribution. The\r
12full text of the license may be found at\r
13http://opensource.org/licenses/bsd-license.php\r
14\r
15THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
16WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
18**/\r
19\r
20#include "LegacyBiosInterface.h"\r
21#include "IpfThunk.h"\r
22\r
23/**\r
24 Gets the current flat GDT and IDT descriptors and store them in\r
25 Private->IntThunk. These values are used by the Thunk code.\r
26 This method must be called before every thunk in order to assure\r
27 that the correct GDT and IDT are restored after the thunk.\r
28\r
29 @param Private Private context for Legacy BIOS\r
30\r
31 @retval EFI_SUCCESS Should only pass.\r
32\r
33**/\r
34EFI_STATUS\r
35LegacyBiosGetFlatDescs (\r
36 IN LEGACY_BIOS_INSTANCE *Private\r
37 )\r
38{\r
39 return EFI_SUCCESS;\r
40}\r
41\r
42\r
43/**\r
44 BIOS interrupt call function.\r
45\r
46 @param BiosInt Int number of BIOS call\r
47 @param Segment Segment number\r
48 @param Offset Offset in segment\r
49 @param Regs IA32 Register set.\r
50 @param Stack Base address of stack\r
51 @param StackSize Size of stack\r
52\r
53 @retval EFI_SUCCESS BIOS interrupt call succeeds.\r
54\r
55**/\r
56EFI_STATUS\r
57BiosIntCall (\r
58 IN UINT16 BiosInt,\r
59 IN UINT16 Segment,\r
60 IN UINT16 Offset,\r
61 IN EFI_IA32_REGISTER_SET *Regs,\r
62 IN VOID *Stack,\r
63 IN UINTN StackSize\r
64 )\r
65{\r
66 IPF_DWORD_REGS DwordRegs;\r
67 UINT64 IntTypeVariable;\r
68\r
69 IntTypeVariable = 0x8000000000000000;\r
1a45b15e 70 IntTypeVariable |= (UINT64)BiosInt;\r
bcecde14 71\r
72 DwordRegs.Cs = Segment;\r
73 DwordRegs.Eip = Offset;\r
74\r
75 DwordRegs.Ds = Regs->X.DS;\r
76 DwordRegs.Es = Regs->X.ES;\r
77 DwordRegs.Fs = Regs->X.ES;\r
78 DwordRegs.Gs = Regs->X.ES;\r
79 DwordRegs.Ss = 0xFFFF;\r
80\r
81 DwordRegs.Eax = Regs->X.AX;\r
82 DwordRegs.Ebx = Regs->X.BX;\r
83 //\r
84 // Sometimes, ECX is used to pass in 32 bit data. For example, INT 1Ah, AX = B10Dh is\r
85 // "PCI BIOS v2.0c + Write Configuration DWORD" and ECX has the dword to write.\r
86 //\r
87 DwordRegs.Ecx = Regs->E.ECX;\r
88 DwordRegs.Edx = Regs->X.DX;\r
89\r
90 DwordRegs.Ebp = Regs->X.BP;\r
91 DwordRegs.Eflag = *((UINT16 *) &Regs->X.Flags);\r
92\r
93 DwordRegs.Edi = Regs->X.DI;\r
94 DwordRegs.Esi = Regs->X.SI;\r
95 DwordRegs.Esp = 0xFFFFFFFF;\r
96\r
97 EfiIaEntryPoint (IntTypeVariable, &DwordRegs, ((UINTN) Stack + StackSize), StackSize);\r
98\r
99 Regs->X.CS = DwordRegs.Cs;\r
100\r
101 Regs->X.DS = (UINT16) DwordRegs.Ds;\r
102 Regs->X.SS = (UINT16) DwordRegs.Ss;\r
103\r
104 Regs->E.EAX = DwordRegs.Eax;\r
105 Regs->E.EBX = DwordRegs.Ebx;\r
106 Regs->E.ECX = DwordRegs.Ecx;\r
107 Regs->E.EDX = DwordRegs.Edx;\r
108\r
109 Regs->E.EBP = DwordRegs.Ebp;\r
110 CopyMem (&Regs->X.Flags, &DwordRegs.Eflag, sizeof (EFI_FLAGS_REG));\r
111\r
112 Regs->E.EDI = DwordRegs.Edi;\r
113 Regs->E.ESI = DwordRegs.Esi;\r
114\r
115 return EFI_SUCCESS;\r
116}\r
117\r
118\r
119/**\r
120 Template of real mode code.\r
121\r
122 @param CodeStart Start address of code.\r
123 @param CodeEnd End address of code\r
124 @param ReverseThunkStart Start of reverse thunk.\r
125 @param IntThunk Low memory thunk.\r
126\r
127**/\r
128VOID\r
129RealModeTemplate (\r
130 OUT UINTN *CodeStart,\r
131 OUT UINTN *CodeEnd,\r
132 OUT UINTN *ReverseThunkStart,\r
133 LOW_MEMORY_THUNK *IntThunk\r
134 )\r
135{\r
136 SAL_RETURN_REGS SalStatus;\r
137\r
138 SalStatus = EsalGetReverseThunkAddress ();\r
139\r
140 *CodeStart = SalStatus.r9;\r
141 *CodeEnd = SalStatus.r10;\r
142 *ReverseThunkStart = SalStatus.r11;\r
143\r
144}\r
145\r
146\r
147/**\r
148 Allocate memory < 1 MB and copy the thunker code into low memory. Se up\r
149 all the descriptors.\r
150\r
151 @param Private Private context for Legacy BIOS\r
152\r
153 @retval EFI_SUCCESS Should only pass.\r
154\r
155**/\r
156EFI_STATUS\r
157LegacyBiosInitializeThunk (\r
158 IN LEGACY_BIOS_INSTANCE *Private\r
159 )\r
160{\r
161 GDT32 *CodeGdt;\r
162 GDT32 *DataGdt;\r
163 UINTN CodeStart;\r
164 UINTN CodeEnd;\r
165 UINTN ReverseThunkStart;\r
166 UINT32 Base;\r
167 LOW_MEMORY_THUNK *IntThunk;\r
168 UINTN TempData;\r
169\r
170 ASSERT (Private);\r
171\r
172 IntThunk = Private->IntThunk;\r
173\r
174 //\r
175 // Clear the reserved descriptor\r
176 //\r
177 ZeroMem (&(IntThunk->RealModeGdt[0]), sizeof (GDT32));\r
178\r
179 //\r
180 // Setup a descriptor for real-mode code\r
181 //\r
182 CodeGdt = &(IntThunk->RealModeGdt[1]);\r
183\r
184 //\r
185 // Fill in the descriptor with our real-mode segment value\r
186 //\r
187 CodeGdt->Type = 0xA;\r
188 //\r
189 // code/read\r
190 //\r
191 CodeGdt->System = 1;\r
192 CodeGdt->Dpl = 0;\r
193 CodeGdt->Present = 1;\r
194 CodeGdt->Software = 0;\r
195 CodeGdt->Reserved = 0;\r
196 CodeGdt->DefaultSize = 0;\r
197 //\r
198 // 16 bit operands\r
199 //\r
200 CodeGdt->Granularity = 0;\r
201\r
202 CodeGdt->LimitHi = 0;\r
203 CodeGdt->LimitLo = 0xffff;\r
204\r
205 Base = (*((UINT32 *) &IntThunk->Code));\r
206 CodeGdt->BaseHi = (Base >> 24) & 0xFF;\r
207 CodeGdt->BaseMid = (Base >> 16) & 0xFF;\r
208 CodeGdt->BaseLo = Base & 0xFFFF;\r
209\r
210 //\r
211 // Setup a descriptor for read-mode data\r
212 //\r
213 DataGdt = &(IntThunk->RealModeGdt[2]);\r
214 CopyMem (DataGdt, CodeGdt, sizeof (GDT32));\r
215\r
216 DataGdt->Type = 0x2;\r
217 //\r
218 // read/write data\r
219 //\r
220 DataGdt->BaseHi = 0x0;\r
221 //\r
222 // Base = 0\r
223 //\r
224 DataGdt->BaseMid = 0x0;\r
225 //\r
226 DataGdt->BaseLo = 0x0;\r
227 //\r
228 DataGdt->LimitHi = 0x0F;\r
229 //\r
230 // Limit = 4Gb\r
231 //\r
232 DataGdt->LimitLo = 0xFFFF;\r
233 //\r
234 DataGdt->Granularity = 0x1;\r
235 //\r
236 //\r
237 // Compute selector value\r
238 //\r
239 IntThunk->RealModeGdtDesc.Limit = (UINT16) (sizeof (IntThunk->RealModeGdt) - 1);\r
240 CopyMem (&IntThunk->RealModeGdtDesc.Base, (UINT32 *) &IntThunk->RealModeGdt, sizeof (UINT32));\r
241 //\r
242 // IntThunk->RealModeGdtDesc.Base = *((UINT32*) &IntThunk->RealModeGdt);\r
243 //\r
244 IntThunk->RealModeIdtDesc.Limit = 0xFFFF;\r
245 IntThunk->RealModeIdtDesc.Base = 0;\r
246 IntThunk->LowCodeSelector = (UINT32) ((UINTN) CodeGdt - IntThunk->RealModeGdtDesc.Base);\r
247 IntThunk->LowDataSelector = (UINT32) ((UINTN) DataGdt - IntThunk->RealModeGdtDesc.Base);\r
248\r
249 //\r
250 // Initialize low real-mode code thunk\r
251 //\r
252 RealModeTemplate (&CodeStart, &CodeEnd, &ReverseThunkStart, IntThunk);\r
253\r
254 TempData = (UINTN) &(IntThunk->Code);\r
255 IntThunk->LowReverseThunkStart = ((UINT32) TempData + (UINT32) (ReverseThunkStart - CodeStart));\r
256\r
257 EsalSetSalDataArea (TempData, (UINTN) IntThunk);\r
258 CopyMem (IntThunk->Code, (VOID *) CodeStart, CodeEnd - CodeStart);\r
259\r
260 IntThunk->EfiToLegacy16InitTable.ReverseThunkCallSegment = EFI_SEGMENT (*((UINT32 *) &IntThunk->LowReverseThunkStart));\r
261 IntThunk->EfiToLegacy16InitTable.ReverseThunkCallOffset = EFI_OFFSET (*((UINT32 *) &IntThunk->LowReverseThunkStart));\r
262\r
263 return EFI_SUCCESS;\r
264}\r
265\r
266\r
267/**\r
268 Thunk to 16-bit real mode and execute a software interrupt with a vector\r
269 of BiosInt. Regs will contain the 16-bit register context on entry and\r
270 exit.\r
271\r
272 @param This Protocol instance pointer.\r
273 @param BiosInt Processor interrupt vector to invoke\r
274 @param Regs Register contexted passed into (and returned) from\r
275 thunk to 16-bit mode\r
276\r
277 @retval FALSE Thunk completed, and there were no BIOS errors in the\r
278 target code. See Regs for status.\r
279 @retval TRUE There was a BIOS erro in the target code.\r
280\r
281**/\r
282BOOLEAN\r
283EFIAPI\r
284LegacyBiosInt86 (\r
285 IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
286 IN UINT8 BiosInt,\r
287 IN EFI_IA32_REGISTER_SET *Regs\r
288 )\r
289{\r
290 EFI_STATUS Status;\r
291 LEGACY_BIOS_INSTANCE *Private;\r
292 LOW_MEMORY_THUNK *IntThunk;\r
293 UINT16 *Stack16;\r
294 EFI_TPL OriginalTpl;\r
295 UINTN IaSegment;\r
296 UINTN IaOffset;\r
297 UINTN *Address;\r
298 UINTN TempData;\r
299\r
300 Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
301 IntThunk = Private->IntThunk;\r
302\r
303 //\r
304 // Get the current flat GDT, IDT, and SS and store them in Private->IntThunk.\r
305 //\r
306 Status = LegacyBiosGetFlatDescs (Private);\r
307 ASSERT_EFI_ERROR (Status);\r
308\r
309 Regs->X.Flags.Reserved1 = 1;\r
310 Regs->X.Flags.Reserved2 = 0;\r
311 Regs->X.Flags.Reserved3 = 0;\r
312 Regs->X.Flags.Reserved4 = 0;\r
313 Regs->X.Flags.IOPL = 3;\r
314 Regs->X.Flags.NT = 0;\r
315 Regs->X.Flags.IF = 1;\r
316 Regs->X.Flags.TF = 0;\r
317 Regs->X.Flags.CF = 0;\r
318 //\r
319 // Clear the error flag; thunk code may set it.\r
320 //\r
321 Stack16 = (UINT16 *) (IntThunk->Stack + LOW_STACK_SIZE);\r
322\r
323 //\r
324 // Copy regs to low memory stack\r
325 //\r
326 Stack16 -= sizeof (EFI_IA32_REGISTER_SET) / sizeof (UINT16);\r
327 CopyMem (Stack16, Regs, sizeof (EFI_IA32_REGISTER_SET));\r
328\r
329 //\r
330 // Provide low stack esp\r
331 //\r
332 TempData = ((UINTN) Stack16) - ((UINTN) IntThunk);\r
333 IntThunk->LowStack = *((UINT32 *) &TempData);\r
334\r
335 //\r
336 // Stack for reverse thunk flat mode.\r
337 // It must point to top of stack (end of stack space).\r
338 //\r
339 TempData = ((UINTN) IntThunk->RevThunkStack) + LOW_STACK_SIZE;\r
340 IntThunk->RevFlatStack = *((UINT32 *) &TempData);\r
341\r
342 //\r
343 // The call to Legacy16 is a critical section to EFI\r
344 //\r
345 OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
346\r
347 //\r
348 // Set Legacy16 state. 0x08, 0x70 is legacy 8259 vector bases.\r
349 //\r
350 Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259LegacyMode, NULL, NULL);\r
351 ASSERT_EFI_ERROR (Status);\r
352\r
353 //\r
354 // Call the real mode thunk code\r
355 //\r
356 TempData = BiosInt * 4;\r
357 Address = (UINTN *) TempData;\r
358 IaOffset = 0xFFFF & (*Address);\r
359 IaSegment = 0xFFFF & ((*Address) >> 16);\r
360\r
361 Status = BiosIntCall (\r
362 BiosInt,\r
363 (UINT16) IaSegment,\r
364 (UINT16) IaOffset,\r
365 (EFI_IA32_REGISTER_SET *) Stack16,\r
366 IntThunk,\r
367 IntThunk->LowStack\r
368 );\r
369\r
370 //\r
371 // Check for errors with the thunk\r
372 //\r
373 switch (Status) {\r
374 case THUNK_OK:\r
375 break;\r
376\r
377 case THUNK_ERR_A20_UNSUP:\r
378 case THUNK_ERR_A20_FAILED:\r
379 default:\r
380 //\r
381 // For all errors, set EFLAGS.CF (used by legacy BIOS to indicate error).\r
382 //\r
383 Regs->X.Flags.CF = 1;\r
384 break;\r
385 }\r
386\r
387 Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259ProtectedMode, NULL, NULL);\r
388 ASSERT_EFI_ERROR (Status);\r
389\r
390 //\r
391 // End critical section\r
392 //\r
393 gBS->RestoreTPL (OriginalTpl);\r
394\r
395 //\r
396 // Return the resulting registers\r
397 //\r
398 CopyMem (Regs, Stack16, sizeof (EFI_IA32_REGISTER_SET));\r
399\r
400 return (BOOLEAN) (Regs->X.Flags.CF != 0);\r
401}\r
402\r
403\r
404/**\r
405 Thunk to 16-bit real mode and call Segment:Offset. Regs will contain the\r
406 16-bit register context on entry and exit. Arguments can be passed on\r
407 the Stack argument\r
408\r
409 @param This Protocol instance pointer.\r
410 @param Segment Segemnt of 16-bit mode call\r
411 @param Offset Offset of 16-bit mdoe call\r
412 @param Regs Register contexted passed into (and returned) from\r
413 thunk to 16-bit mode\r
414 @param Stack Caller allocated stack used to pass arguments\r
415 @param StackSize Size of Stack in bytes\r
416\r
417 @retval FALSE Thunk completed, and there were no BIOS errors in the\r
418 target code. See Regs for status.\r
419 @retval TRUE There was a BIOS erro in the target code.\r
420\r
421**/\r
422BOOLEAN\r
423EFIAPI\r
424LegacyBiosFarCall86 (\r
425 IN EFI_LEGACY_BIOS_PROTOCOL *This,\r
426 IN UINT16 Segment,\r
427 IN UINT16 Offset,\r
428 IN EFI_IA32_REGISTER_SET *Regs,\r
429 IN VOID *Stack,\r
430 IN UINTN StackSize\r
431 )\r
432{\r
433 EFI_STATUS Status;\r
434 LEGACY_BIOS_INSTANCE *Private;\r
435 LOW_MEMORY_THUNK *IntThunk;\r
436 UINT16 *Stack16;\r
437 EFI_TPL OriginalTpl;\r
438 UINTN IaSegment;\r
439 UINTN IaOffset;\r
440 UINTN TempData;\r
441\r
442 Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);\r
443 IntThunk = Private->IntThunk;\r
444 IaSegment = Segment;\r
445 IaOffset = Offset;\r
446\r
447 //\r
448 // Get the current flat GDT and IDT and store them in Private->IntThunk.\r
449 //\r
450 Status = LegacyBiosGetFlatDescs (Private);\r
451 ASSERT_EFI_ERROR (Status);\r
452\r
453 Regs->X.Flags.Reserved1 = 1;\r
454 Regs->X.Flags.Reserved2 = 0;\r
455 Regs->X.Flags.Reserved3 = 0;\r
456 Regs->X.Flags.Reserved4 = 0;\r
457 Regs->X.Flags.IOPL = 3;\r
458 Regs->X.Flags.NT = 0;\r
459 Regs->X.Flags.IF = 1;\r
460 Regs->X.Flags.TF = 0;\r
461 Regs->X.Flags.CF = 0;\r
462 //\r
463 // Clear the error flag; thunk code may set it.\r
464 //\r
465 Stack16 = (UINT16 *) (IntThunk->Stack + LOW_STACK_SIZE);\r
466 if (Stack != NULL && StackSize != 0) {\r
467 //\r
468 // Copy Stack to low memory stack\r
469 //\r
470 Stack16 -= StackSize / sizeof (UINT16);\r
471 CopyMem (Stack16, Stack, StackSize);\r
472 }\r
473 //\r
474 // Copy regs to low memory stack\r
475 //\r
476 Stack16 -= sizeof (EFI_IA32_REGISTER_SET) / sizeof (UINT16);\r
477 CopyMem (Stack16, Regs, sizeof (EFI_IA32_REGISTER_SET));\r
478\r
479 //\r
480 // Provide low stack esp\r
481 //\r
482 TempData = ((UINTN) Stack16) - ((UINTN) IntThunk);\r
483 IntThunk->LowStack = *((UINT32 *) &TempData);\r
484\r
485 //\r
486 // The call to Legacy16 is a critical section to EFI\r
487 //\r
488 OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
489\r
490 //\r
491 // Set Legacy16 state. 0x08, 0x70 is legacy 8259 vector bases.\r
492 //\r
493 Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259LegacyMode, NULL, NULL);\r
494 ASSERT_EFI_ERROR (Status);\r
495\r
496 //\r
497 // Call the real mode thunk code\r
498 //\r
499 Status = BiosIntCall (\r
500 0x100,\r
501 (UINT16) IaSegment,\r
502 (UINT16) IaOffset,\r
503 (EFI_IA32_REGISTER_SET *) Stack16,\r
504 IntThunk,\r
505 IntThunk->LowStack\r
506 );\r
507\r
508 //\r
509 // Check for errors with the thunk\r
510 //\r
511 switch (Status) {\r
512 case THUNK_OK:\r
513 break;\r
514\r
515 case THUNK_ERR_A20_UNSUP:\r
516 case THUNK_ERR_A20_FAILED:\r
517 default:\r
518 //\r
519 // For all errors, set EFLAGS.CF (used by legacy BIOS to indicate error).\r
520 //\r
521 Regs->X.Flags.CF = 1;\r
522 break;\r
523 }\r
524 //\r
525 // Restore protected mode interrupt state\r
526 //\r
527 Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259ProtectedMode, NULL, NULL);\r
528 ASSERT_EFI_ERROR (Status);\r
529\r
530 //\r
531 // End critical section\r
532 //\r
533 gBS->RestoreTPL (OriginalTpl);\r
534\r
535 //\r
536 // Return the resulting registers\r
537 //\r
538 CopyMem (Regs, Stack16, sizeof (EFI_IA32_REGISTER_SET));\r
539 Stack16 += sizeof (EFI_IA32_REGISTER_SET) / sizeof (UINT16);\r
540\r
541 if (Stack != NULL && StackSize != 0) {\r
542 //\r
543 // Copy low memory stack to Stack\r
544 //\r
545 CopyMem (Stack, Stack16, StackSize);\r
546 Stack16 += StackSize / sizeof (UINT16);\r
547 }\r
548\r
549 return (BOOLEAN) (Regs->X.Flags.CF != 0);\r
550}\r