1 /** @file
2   SMM STM support functions
3 
4   Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include <PiSmm.h>
10 #include <Library/DebugLib.h>
11 
12 #include "SmmStm.h"
13 
14 ///
15 /// Page Table Entry
16 ///
17 #define IA32_PG_P                   BIT0
18 #define IA32_PG_RW                  BIT1
19 #define IA32_PG_PS                  BIT7
20 
21 /**
22 
23   Create 4G page table for STM.
24   2M PAE page table in X64 version.
25 
26   @param PageTableBase        The page table base in MSEG
27 
28 **/
29 VOID
StmGen4GPageTable(IN UINTN PageTableBase)30 StmGen4GPageTable (
31   IN UINTN              PageTableBase
32   )
33 {
34   UINTN                             Index;
35   UINTN                             SubIndex;
36   UINT64                            *Pde;
37   UINT64                            *Pte;
38   UINT64                            *Pml4;
39 
40   Pml4 = (UINT64*)(UINTN)PageTableBase;
41   PageTableBase += SIZE_4KB;
42   *Pml4 = PageTableBase | IA32_PG_RW | IA32_PG_P;
43 
44   Pde = (UINT64*)(UINTN)PageTableBase;
45   PageTableBase += SIZE_4KB;
46   Pte = (UINT64 *)(UINTN)PageTableBase;
47 
48   for (Index = 0; Index < 4; Index++) {
49     *Pde = PageTableBase | IA32_PG_RW | IA32_PG_P;
50     Pde++;
51     PageTableBase += SIZE_4KB;
52 
53     for (SubIndex = 0; SubIndex < SIZE_4KB / sizeof (*Pte); SubIndex++) {
54       *Pte = (((Index << 9) + SubIndex) << 21) | IA32_PG_PS | IA32_PG_RW | IA32_PG_P;
55       Pte++;
56     }
57   }
58 }
59 
60 /**
61   This is SMM exception handle.
62   Consumed by STM when exception happen.
63 
64   @param Context  STM protection exception stack frame
65 
66   @return the EBX value for STM reference.
67           EBX = 0: resume SMM guest using register state found on exception stack.
68           EBX = 1 to 0x0F: EBX contains a BIOS error code which the STM must record in the
69                            TXT.ERRORCODE register and subsequently reset the system via
70                            TXT.CMD.SYS_RESET. The value of the TXT.ERRORCODE register is calculated as
71                            follows: TXT.ERRORCODE = (EBX & 0x0F) | STM_CRASH_BIOS_PANIC
72           EBX = 0x10 to 0xFFFFFFFF - reserved, do not use.
73 
74 **/
75 UINT32
76 EFIAPI
SmmStmExceptionHandler(IN OUT STM_PROTECTION_EXCEPTION_STACK_FRAME Context)77 SmmStmExceptionHandler (
78   IN OUT STM_PROTECTION_EXCEPTION_STACK_FRAME Context
79   )
80 {
81   // TBD - SmmStmExceptionHandler, record information
82   DEBUG ((DEBUG_ERROR, "SmmStmExceptionHandler ...\n"));
83   //
84   // Skip this instruction and continue;
85   //
86   Context.X64StackFrame->Rip += Context.X64StackFrame->VmcsExitInstructionLength;
87 
88   return 0;
89 }
90