summaryrefslogtreecommitdiff
path: root/IntelFrameworkModulePkg/Csm/LegacyBiosDxe/Ipf/Thunk.c
blob: e601bbd63bf295751fd096a2b50f5147945d97b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/** @file
  Call into 16-bit BIOS code

  BugBug: Thunker does A20 gate. Can we get rid of this code or
          put it into Legacy16 code.

Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR>

This program and the accompanying materials
are licensed and made available under the terms and conditions
of the BSD License which accompanies this distribution.  The
full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "LegacyBiosInterface.h"
#include "IpfThunk.h"

/**
  Gets the current flat GDT and IDT descriptors and  store them in
  Private->IntThunk.  These values are used by the Thunk code.
  This method must be called before every thunk in order to assure
  that the correct GDT and IDT are restored after the thunk.

  @param  Private            Private context for Legacy BIOS

  @retval EFI_SUCCESS        Should only pass.

**/
EFI_STATUS
LegacyBiosGetFlatDescs (
  IN  LEGACY_BIOS_INSTANCE    *Private
  )
{
  return EFI_SUCCESS;
}


/**
  BIOS interrupt call function.

  @param  BiosInt            Int number of BIOS call
  @param  Segment            Segment number
  @param  Offset             Offset in segment
  @param  Regs               IA32 Register set.
  @param  Stack              Base address of stack
  @param  StackSize          Size of stack

  @retval EFI_SUCCESS        BIOS interrupt call succeeds.

**/
EFI_STATUS
BiosIntCall (
  IN  UINT16                            BiosInt,
  IN  UINT16                            Segment,
  IN  UINT16                            Offset,
  IN  EFI_IA32_REGISTER_SET             *Regs,
  IN  VOID                              *Stack,
  IN  UINTN                             StackSize
  )
{
  IPF_DWORD_REGS  DwordRegs;
  UINT64          IntTypeVariable;

  IntTypeVariable = 0x8000000000000000;
  IntTypeVariable |= (UINT64)BiosInt;

  DwordRegs.Cs    = Segment;
  DwordRegs.Eip   = Offset;

  DwordRegs.Ds    = Regs->X.DS;
  DwordRegs.Es    = Regs->X.ES;
  DwordRegs.Fs    = Regs->X.ES;
  DwordRegs.Gs    = Regs->X.ES;
  DwordRegs.Ss    = 0xFFFF;

  DwordRegs.Eax   = Regs->X.AX;
  DwordRegs.Ebx   = Regs->X.BX;
  //
  // Sometimes, ECX is used to pass in 32 bit data. For example, INT 1Ah, AX = B10Dh is
  // "PCI BIOS v2.0c + Write Configuration DWORD" and ECX has the dword to write.
  //
  DwordRegs.Ecx   = Regs->E.ECX;
  DwordRegs.Edx   = Regs->X.DX;

  DwordRegs.Ebp   = Regs->X.BP;
  DwordRegs.Eflag = *((UINT16 *) &Regs->X.Flags);

  DwordRegs.Edi   = Regs->X.DI;
  DwordRegs.Esi   = Regs->X.SI;
  DwordRegs.Esp   = 0xFFFFFFFF;

  EfiIaEntryPoint (IntTypeVariable, &DwordRegs, ((UINTN) Stack + StackSize), StackSize);

  Regs->X.CS  = DwordRegs.Cs;

  Regs->X.DS  = (UINT16) DwordRegs.Ds;
  Regs->X.SS  = (UINT16) DwordRegs.Ss;

  Regs->E.EAX  = DwordRegs.Eax;
  Regs->E.EBX  = DwordRegs.Ebx;
  Regs->E.ECX  = DwordRegs.Ecx;
  Regs->E.EDX  = DwordRegs.Edx;

  Regs->E.EBP  = DwordRegs.Ebp;
  CopyMem (&Regs->X.Flags, &DwordRegs.Eflag, sizeof (EFI_FLAGS_REG));

  Regs->E.EDI  = DwordRegs.Edi;
  Regs->E.ESI  = DwordRegs.Esi;

  return EFI_SUCCESS;
}


/**
  Template of real mode code.

  @param  CodeStart          Start address of code.
  @param  CodeEnd            End address of code
  @param  ReverseThunkStart  Start of reverse thunk.
  @param  IntThunk           Low memory thunk.

**/
VOID
RealModeTemplate (
  OUT UINTN          *CodeStart,
  OUT UINTN          *CodeEnd,
  OUT UINTN          *ReverseThunkStart,
  LOW_MEMORY_THUNK   *IntThunk
  )
{
  SAL_RETURN_REGS SalStatus;

  SalStatus           = EsalGetReverseThunkAddress ();

  *CodeStart          = SalStatus.r9;
  *CodeEnd            = SalStatus.r10;
  *ReverseThunkStart  = SalStatus.r11;

}


/**
  Allocate memory < 1 MB and copy the thunker code into low memory. Se up
  all the descriptors.

  @param  Private            Private context for Legacy BIOS

  @retval EFI_SUCCESS        Should only pass.

**/
EFI_STATUS
LegacyBiosInitializeThunk (
  IN  LEGACY_BIOS_INSTANCE    *Private
  )
{
  GDT32               *CodeGdt;
  GDT32               *DataGdt;
  UINTN             CodeStart;
  UINTN             CodeEnd;
  UINTN             ReverseThunkStart;
  UINT32            Base;
  LOW_MEMORY_THUNK  *IntThunk;
  UINTN             TempData;

  ASSERT (Private);

  IntThunk = Private->IntThunk;

  //
  // Clear the reserved descriptor
  //
  ZeroMem (&(IntThunk->RealModeGdt[0]), sizeof (GDT32));

  //
  // Setup a descriptor for real-mode code
  //
  CodeGdt = &(IntThunk->RealModeGdt[1]);

  //
  // Fill in the descriptor with our real-mode segment value
  //
  CodeGdt->Type = 0xA;
  //
  // code/read
  //
  CodeGdt->System       = 1;
  CodeGdt->Dpl          = 0;
  CodeGdt->Present      = 1;
  CodeGdt->Software     = 0;
  CodeGdt->Reserved     = 0;
  CodeGdt->DefaultSize  = 0;
  //
  // 16 bit operands
  //
  CodeGdt->Granularity  = 0;

  CodeGdt->LimitHi      = 0;
  CodeGdt->LimitLo      = 0xffff;

  Base                  = (*((UINT32 *) &IntThunk->Code));
  CodeGdt->BaseHi       = (Base >> 24) & 0xFF;
  CodeGdt->BaseMid      = (Base >> 16) & 0xFF;
  CodeGdt->BaseLo       = Base & 0xFFFF;

  //
  // Setup a descriptor for read-mode data
  //
  DataGdt = &(IntThunk->RealModeGdt[2]);
  CopyMem (DataGdt, CodeGdt, sizeof (GDT32));

  DataGdt->Type = 0x2;
  //
  // read/write data
  //
  DataGdt->BaseHi = 0x0;
  //
  // Base = 0
  //
  DataGdt->BaseMid = 0x0;
  //
  DataGdt->BaseLo = 0x0;
  //
  DataGdt->LimitHi = 0x0F;
  //
  // Limit = 4Gb
  //
  DataGdt->LimitLo = 0xFFFF;
  //
  DataGdt->Granularity = 0x1;
  //
  //
  // Compute selector value
  //
  IntThunk->RealModeGdtDesc.Limit = (UINT16) (sizeof (IntThunk->RealModeGdt) - 1);
  CopyMem (&IntThunk->RealModeGdtDesc.Base, (UINT32 *) &IntThunk->RealModeGdt, sizeof (UINT32));
  //
  //  IntThunk->RealModeGdtDesc.Base = *((UINT32*) &IntThunk->RealModeGdt);
  //
  IntThunk->RealModeIdtDesc.Limit = 0xFFFF;
  IntThunk->RealModeIdtDesc.Base  = 0;
  IntThunk->LowCodeSelector       = (UINT32) ((UINTN) CodeGdt - IntThunk->RealModeGdtDesc.Base);
  IntThunk->LowDataSelector       = (UINT32) ((UINTN) DataGdt - IntThunk->RealModeGdtDesc.Base);

  //
  // Initialize low real-mode code thunk
  //
  RealModeTemplate (&CodeStart, &CodeEnd, &ReverseThunkStart, IntThunk);

  TempData                        = (UINTN) &(IntThunk->Code);
  IntThunk->LowReverseThunkStart  = ((UINT32) TempData + (UINT32) (ReverseThunkStart - CodeStart));

  EsalSetSalDataArea (TempData, (UINTN) IntThunk);
  CopyMem (IntThunk->Code, (VOID *) CodeStart, CodeEnd - CodeStart);

  IntThunk->EfiToLegacy16InitTable.ReverseThunkCallSegment = EFI_SEGMENT (*((UINT32 *) &IntThunk->LowReverseThunkStart));
  IntThunk->EfiToLegacy16InitTable.ReverseThunkCallOffset = EFI_OFFSET (*((UINT32 *) &IntThunk->LowReverseThunkStart));

  return EFI_SUCCESS;
}


/**
  Thunk to 16-bit real mode and execute a software interrupt with a vector
  of BiosInt. Regs will contain the 16-bit register context on entry and
  exit.

  @param  This               Protocol instance pointer.
  @param  BiosInt            Processor interrupt vector to invoke
  @param  Regs               Register contexted passed into (and returned) from
                             thunk to  16-bit mode

  @retval FALSE              Thunk completed, and there were no BIOS errors in the
                             target code. See Regs for status.
  @retval TRUE               There was a BIOS erro in the target code.

**/
BOOLEAN
EFIAPI
LegacyBiosInt86 (
  IN EFI_LEGACY_BIOS_PROTOCOL           *This,
  IN  UINT8                             BiosInt,
  IN  EFI_IA32_REGISTER_SET             *Regs
  )
{
  EFI_STATUS            Status;
  LEGACY_BIOS_INSTANCE  *Private;
  LOW_MEMORY_THUNK      *IntThunk;
  UINT16                *Stack16;
  EFI_TPL               OriginalTpl;
  UINTN                 IaSegment;
  UINTN                 IaOffset;
  UINTN                 *Address;
  UINTN                 TempData;

  Private   = LEGACY_BIOS_INSTANCE_FROM_THIS (This);
  IntThunk  = Private->IntThunk;

  //
  // Get the current flat GDT, IDT, and SS and store them in Private->IntThunk.
  //
  Status = LegacyBiosGetFlatDescs (Private);
  ASSERT_EFI_ERROR (Status);

  Regs->X.Flags.Reserved1 = 1;
  Regs->X.Flags.Reserved2 = 0;
  Regs->X.Flags.Reserved3 = 0;
  Regs->X.Flags.Reserved4 = 0;
  Regs->X.Flags.IOPL      = 3;
  Regs->X.Flags.NT        = 0;
  Regs->X.Flags.IF        = 1;
  Regs->X.Flags.TF        = 0;
  Regs->X.Flags.CF        = 0;
  //
  // Clear the error flag; thunk code may set it.
  //
  Stack16 = (UINT16 *) (IntThunk->Stack + LOW_STACK_SIZE);

  //
  // Copy regs to low memory stack
  //
  Stack16 -= sizeof (EFI_IA32_REGISTER_SET) / sizeof (UINT16);
  CopyMem (Stack16, Regs, sizeof (EFI_IA32_REGISTER_SET));

  //
  // Provide low stack esp
  //
  TempData            = ((UINTN) Stack16) - ((UINTN) IntThunk);
  IntThunk->LowStack  = *((UINT32 *) &TempData);

  //
  // Stack for reverse thunk flat mode.
  //    It must point to top of stack (end of stack space).
  //
  TempData                = ((UINTN) IntThunk->RevThunkStack) + LOW_STACK_SIZE;
  IntThunk->RevFlatStack  = *((UINT32 *) &TempData);

  //
  // The call to Legacy16 is a critical section to EFI
  //
  OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);

  //
  // Set Legacy16 state. 0x08, 0x70 is legacy 8259 vector bases.
  //
  Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259LegacyMode, NULL, NULL);
  ASSERT_EFI_ERROR (Status);

  //
  // Call the real mode thunk code
  //
  TempData  = BiosInt * 4;
  Address   = (UINTN *) TempData;
  IaOffset  = 0xFFFF & (*Address);
  IaSegment = 0xFFFF & ((*Address) >> 16);

  Status = BiosIntCall (
            BiosInt,
            (UINT16) IaSegment,
            (UINT16) IaOffset,
            (EFI_IA32_REGISTER_SET *) Stack16,
            IntThunk,
            IntThunk->LowStack
            );

  //
  // Check for errors with the thunk
  //
  switch (Status) {
  case THUNK_OK:
    break;

  case THUNK_ERR_A20_UNSUP:
  case THUNK_ERR_A20_FAILED:
  default:
    //
    // For all errors, set EFLAGS.CF (used by legacy BIOS to indicate error).
    //
    Regs->X.Flags.CF = 1;
    break;
  }

  Status  = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259ProtectedMode, NULL, NULL);
  ASSERT_EFI_ERROR (Status);

  //
  // End critical section
  //
  gBS->RestoreTPL (OriginalTpl);

  //
  // Return the resulting registers
  //
  CopyMem (Regs, Stack16, sizeof (EFI_IA32_REGISTER_SET));

  return (BOOLEAN) (Regs->X.Flags.CF != 0);
}


/**
  Thunk to 16-bit real mode and call Segment:Offset. Regs will contain the
  16-bit register context on entry and exit. Arguments can be passed on
  the Stack argument

  @param  This               Protocol instance pointer.
  @param  Segment            Segemnt of 16-bit mode call
  @param  Offset             Offset of 16-bit mdoe call
  @param  Regs               Register contexted passed into (and returned) from
                             thunk to  16-bit mode
  @param  Stack              Caller allocated stack used to pass arguments
  @param  StackSize          Size of Stack in bytes

  @retval FALSE              Thunk completed, and there were no BIOS errors in the
                             target code. See Regs for status.
  @retval TRUE               There was a BIOS erro in the target code.

**/
BOOLEAN
EFIAPI
LegacyBiosFarCall86 (
  IN EFI_LEGACY_BIOS_PROTOCOL           *This,
  IN  UINT16                            Segment,
  IN  UINT16                            Offset,
  IN  EFI_IA32_REGISTER_SET             *Regs,
  IN  VOID                              *Stack,
  IN  UINTN                             StackSize
  )
{
  EFI_STATUS            Status;
  LEGACY_BIOS_INSTANCE  *Private;
  LOW_MEMORY_THUNK      *IntThunk;
  UINT16                *Stack16;
  EFI_TPL               OriginalTpl;
  UINTN                 IaSegment;
  UINTN                 IaOffset;
  UINTN                 TempData;

  Private   = LEGACY_BIOS_INSTANCE_FROM_THIS (This);
  IntThunk  = Private->IntThunk;
  IaSegment = Segment;
  IaOffset  = Offset;

  //
  // Get the current flat GDT and IDT and store them in Private->IntThunk.
  //
  Status = LegacyBiosGetFlatDescs (Private);
  ASSERT_EFI_ERROR (Status);

  Regs->X.Flags.Reserved1 = 1;
  Regs->X.Flags.Reserved2 = 0;
  Regs->X.Flags.Reserved3 = 0;
  Regs->X.Flags.Reserved4 = 0;
  Regs->X.Flags.IOPL      = 3;
  Regs->X.Flags.NT        = 0;
  Regs->X.Flags.IF        = 1;
  Regs->X.Flags.TF        = 0;
  Regs->X.Flags.CF        = 0;
  //
  // Clear the error flag; thunk code may set it.
  //
  Stack16 = (UINT16 *) (IntThunk->Stack + LOW_STACK_SIZE);
  if (Stack != NULL && StackSize != 0) {
    //
    // Copy Stack to low memory stack
    //
    Stack16 -= StackSize / sizeof (UINT16);
    CopyMem (Stack16, Stack, StackSize);
  }
  //
  // Copy regs to low memory stack
  //
  Stack16 -= sizeof (EFI_IA32_REGISTER_SET) / sizeof (UINT16);
  CopyMem (Stack16, Regs, sizeof (EFI_IA32_REGISTER_SET));

  //
  // Provide low stack esp
  //
  TempData            = ((UINTN) Stack16) - ((UINTN) IntThunk);
  IntThunk->LowStack  = *((UINT32 *) &TempData);

  //
  // The call to Legacy16 is a critical section to EFI
  //
  OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);

  //
  // Set Legacy16 state. 0x08, 0x70 is legacy 8259 vector bases.
  //
  Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259LegacyMode, NULL, NULL);
  ASSERT_EFI_ERROR (Status);

  //
  // Call the real mode thunk code
  //
  Status = BiosIntCall (
            0x100,
            (UINT16) IaSegment,
            (UINT16) IaOffset,
            (EFI_IA32_REGISTER_SET *) Stack16,
            IntThunk,
            IntThunk->LowStack
            );

  //
  // Check for errors with the thunk
  //
  switch (Status) {
  case THUNK_OK:
    break;

  case THUNK_ERR_A20_UNSUP:
  case THUNK_ERR_A20_FAILED:
  default:
    //
    // For all errors, set EFLAGS.CF (used by legacy BIOS to indicate error).
    //
    Regs->X.Flags.CF = 1;
    break;
  }
  //
  // Restore protected mode interrupt state
  //
  Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259ProtectedMode, NULL, NULL);
  ASSERT_EFI_ERROR (Status);

  //
  // End critical section
  //
  gBS->RestoreTPL (OriginalTpl);

  //
  // Return the resulting registers
  //
  CopyMem (Regs, Stack16, sizeof (EFI_IA32_REGISTER_SET));
  Stack16 += sizeof (EFI_IA32_REGISTER_SET) / sizeof (UINT16);

  if (Stack != NULL && StackSize != 0) {
    //
    // Copy low memory stack to Stack
    //
    CopyMem (Stack, Stack16, StackSize);
    Stack16 += StackSize / sizeof (UINT16);
  }

  return (BOOLEAN) (Regs->X.Flags.CF != 0);
}