BlSupportSmm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /** @file
  2. This driver is used for SMM S3 support for the bootloader that
  3. doesn't support SMM.
  4. The payload would save SMM rebase info to SMM communication area.
  5. The bootloader is expected to rebase the SMM and trigger SMI by
  6. writting 0xB2 port with given value from SMM communication area.
  7. The paylaod SMM handler got chance to restore regs in S3 path.
  8. Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
  9. SPDX-License-Identifier: BSD-2-Clause-Patent
  10. **/
  11. #include <BlSupportSmm.h>
  12. PLD_S3_COMMUNICATION mPldS3Hob;
  13. EFI_SMRAM_HOB_DESCRIPTOR_BLOCK *mSmramHob = NULL;
  14. PLD_SMM_REGISTERS *mSmmRegisterHob = NULL;
  15. UINT64 mSmmFeatureControl = 0;
  16. /**
  17. Save SMM rebase and SMI handler information to SMM communication area
  18. The function detects SMM communication region for boot loader, if it is detected, it
  19. will save SMM rebase information and S3 SMI handler information to SMM communication
  20. region. Bootloader should consume these information in S3 path to restore smm base,
  21. and write the 0xB2 port to trigger SMI so that payload could resume S3 registers.
  22. @param[in] BlSwSmiHandlerInput Value written to 0xB2 to trigger SMI handler.
  23. @retval EFI_SUCCESS Save SMM info success.
  24. @retval Others Failed to save SMM info.
  25. **/
  26. EFI_STATUS
  27. SaveSmmInfoForS3 (
  28. IN UINT8 BlSwSmiHandlerInput
  29. )
  30. {
  31. EFI_STATUS Status;
  32. EFI_PROCESSOR_INFORMATION ProcessorInfo;
  33. EFI_MP_SERVICES_PROTOCOL *MpService;
  34. CPU_SMMBASE *SmmBaseInfo;
  35. PLD_TO_BL_SMM_INFO *PldSmmInfo;
  36. UINTN Index;
  37. PldSmmInfo = (PLD_TO_BL_SMM_INFO *)(UINTN)mPldS3Hob.CommBuffer.PhysicalStart;
  38. PldSmmInfo->Header.Header.HobLength = (UINT16)(sizeof (PLD_TO_BL_SMM_INFO) + gSmst->NumberOfCpus * sizeof (CPU_SMMBASE));
  39. for (Index = 0; Index < mSmramHob->NumberOfSmmReservedRegions; Index++) {
  40. if ((mPldS3Hob.CommBuffer.PhysicalStart >= mSmramHob->Descriptor[Index].PhysicalStart) &&
  41. (mPldS3Hob.CommBuffer.PhysicalStart < mSmramHob->Descriptor[Index].PhysicalStart + mSmramHob->Descriptor[Index].PhysicalSize))
  42. {
  43. break;
  44. }
  45. }
  46. if (Index == mSmramHob->NumberOfSmmReservedRegions) {
  47. return EFI_NOT_FOUND;
  48. }
  49. //
  50. // Make sure the dedicated region for SMM info communication whose attribute is "allocated" (i.e., excluded from SMM memory service)
  51. //
  52. if ((mSmramHob->Descriptor[Index].RegionState & EFI_ALLOCATED) == 0) {
  53. DEBUG ((DEBUG_ERROR, "SMM communication region not set to EFI_ALLOCATED\n"));
  54. return EFI_INVALID_PARAMETER;
  55. }
  56. if (((UINTN)PldSmmInfo + PldSmmInfo->Header.Header.HobLength) > (mSmramHob->Descriptor[Index].PhysicalStart + mSmramHob->Descriptor[Index].PhysicalSize)) {
  57. DEBUG ((DEBUG_ERROR, "SMM communication buffer (0x%x) is too small.\n", (UINTN)PldSmmInfo + PldSmmInfo->Header.Header.HobLength));
  58. return EFI_BUFFER_TOO_SMALL;
  59. }
  60. CopyGuid (&PldSmmInfo->Header.Name, &gS3CommunicationGuid);
  61. PldSmmInfo->Header.Header.HobType = EFI_HOB_TYPE_GUID_EXTENSION;
  62. PldSmmInfo->S3Info.SwSmiTriggerValue = BlSwSmiHandlerInput;
  63. //
  64. // Save APIC ID and SMM base
  65. //
  66. Status = gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpService);
  67. if (EFI_ERROR (Status)) {
  68. return Status;
  69. }
  70. PldSmmInfo->S3Info.CpuCount = (UINT32)gSmst->NumberOfCpus;
  71. SmmBaseInfo = &PldSmmInfo->S3Info.SmmBase[0];
  72. for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {
  73. Status = MpService->GetProcessorInfo (MpService, Index, &ProcessorInfo);
  74. if (EFI_ERROR (Status)) {
  75. return Status;
  76. }
  77. SmmBaseInfo->ApicId = (UINT32)(UINTN)ProcessorInfo.ProcessorId;
  78. SmmBaseInfo->SmmBase = (UINT32)(UINTN)gSmst->CpuSaveState[Index] - SMRAM_SAVE_STATE_MAP_OFFSET;
  79. DEBUG ((DEBUG_INFO, "CPU%d ID:%02X Base: %08X\n", Index, SmmBaseInfo->ApicId, SmmBaseInfo->SmmBase));
  80. SmmBaseInfo++;
  81. }
  82. return EFI_SUCCESS;
  83. }
  84. /**
  85. Get specified SMI register based on given register ID
  86. @param[in] Id The register ID to get.
  87. @retval NULL The register is not found
  88. @return smi register
  89. **/
  90. PLD_GENERIC_REGISTER *
  91. GetRegisterById (
  92. UINT64 Id
  93. )
  94. {
  95. UINT32 Index;
  96. for (Index = 0; Index < mSmmRegisterHob->Count; Index++) {
  97. if (mSmmRegisterHob->Registers[Index].Id == Id) {
  98. return &mSmmRegisterHob->Registers[Index];
  99. }
  100. }
  101. return NULL;
  102. }
  103. /**
  104. Set SMM SMI Global enable lock
  105. **/
  106. VOID
  107. LockSmiGlobalEn (
  108. VOID
  109. )
  110. {
  111. PLD_GENERIC_REGISTER *SmiLockReg;
  112. DEBUG ((DEBUG_ERROR, "LockSmiGlobalEn .....\n"));
  113. SmiLockReg = GetRegisterById (REGISTER_ID_SMI_GBL_EN_LOCK);
  114. if (SmiLockReg == NULL) {
  115. DEBUG ((DEBUG_ERROR, "SMI global enable lock reg not found.\n"));
  116. return;
  117. }
  118. //
  119. // Set SMM SMI lock in S3 path
  120. //
  121. if ((SmiLockReg->Address.AccessSize == EFI_ACPI_3_0_DWORD) &&
  122. (SmiLockReg->Address.Address != 0) &&
  123. (SmiLockReg->Address.RegisterBitWidth == 1) &&
  124. (SmiLockReg->Address.AddressSpaceId == EFI_ACPI_3_0_SYSTEM_MEMORY) &&
  125. (SmiLockReg->Value == 1))
  126. {
  127. DEBUG ((DEBUG_ERROR, "LockSmiGlobalEn ....is locked\n"));
  128. MmioOr32 ((UINT32)SmiLockReg->Address.Address, 1 << SmiLockReg->Address.RegisterBitOffset);
  129. } else {
  130. DEBUG ((DEBUG_ERROR, "Unexpected SMM SMI lock register, need enhancement here.\n"));
  131. }
  132. }
  133. /**
  134. Check and set SMM feature lock bit and code check enable bit
  135. in S3 path.
  136. **/
  137. VOID
  138. SmmFeatureLockOnS3 (
  139. VOID
  140. )
  141. {
  142. if (mSmmFeatureControl != 0) {
  143. return;
  144. }
  145. mSmmFeatureControl = AsmReadMsr64 (MSR_SMM_FEATURE_CONTROL);
  146. if ((mSmmFeatureControl & 0x5) != 0x5) {
  147. //
  148. // Set Lock bit [BIT0] for this register and SMM code check enable bit [BIT2]
  149. //
  150. AsmWriteMsr64 (MSR_SMM_FEATURE_CONTROL, mSmmFeatureControl | 0x5);
  151. }
  152. mSmmFeatureControl = AsmReadMsr64 (MSR_SMM_FEATURE_CONTROL);
  153. }
  154. /**
  155. Function to program SMRR base and mask.
  156. @param[in] ProcedureArgument Pointer to SMRR_BASE_MASK structure.
  157. **/
  158. VOID
  159. EFIAPI
  160. SetSmrr (
  161. IN VOID *ProcedureArgument
  162. )
  163. {
  164. if (ProcedureArgument != NULL) {
  165. AsmWriteMsr64 (MSR_IA32_SMRR_PHYSBASE, ((SMRR_BASE_MASK *)ProcedureArgument)->Base);
  166. AsmWriteMsr64 (MSR_IA32_SMRR_PHYSMASK, ((SMRR_BASE_MASK *)ProcedureArgument)->Mask);
  167. }
  168. }
  169. /**
  170. Set SMRR in S3 path.
  171. **/
  172. VOID
  173. SetSmrrOnS3 (
  174. VOID
  175. )
  176. {
  177. EFI_STATUS Status;
  178. SMRR_BASE_MASK Arguments;
  179. UINTN Index;
  180. UINT32 SmmBase;
  181. UINT32 SmmSize;
  182. if ((AsmReadMsr64 (MSR_IA32_SMRR_PHYSBASE) != 0) && ((AsmReadMsr64 (MSR_IA32_SMRR_PHYSMASK) & BIT11) != 0)) {
  183. return;
  184. }
  185. SmmBase = (UINT32)(UINTN)mSmramHob->Descriptor[0].PhysicalStart;
  186. SmmSize = (UINT32)(UINTN)mSmramHob->Descriptor[0].PhysicalSize;
  187. if ((mSmramHob->NumberOfSmmReservedRegions > 2) || (mSmramHob->NumberOfSmmReservedRegions == 0)) {
  188. DEBUG ((DEBUG_ERROR, "%d SMM ranges are not supported.\n", mSmramHob->NumberOfSmmReservedRegions));
  189. return;
  190. } else if (mSmramHob->NumberOfSmmReservedRegions == 2) {
  191. if ((mSmramHob->Descriptor[1].PhysicalStart + mSmramHob->Descriptor[1].PhysicalSize) == SmmBase) {
  192. SmmBase = (UINT32)(UINTN)mSmramHob->Descriptor[1].PhysicalStart;
  193. } else if (mSmramHob->Descriptor[1].PhysicalStart != (SmmBase + SmmSize)) {
  194. DEBUG ((DEBUG_ERROR, "Two SMM regions are not continous.\n"));
  195. return;
  196. }
  197. SmmSize += (UINT32)(UINTN)mSmramHob->Descriptor[1].PhysicalSize;
  198. }
  199. if ((SmmBase == 0) || (SmmSize < SIZE_4KB)) {
  200. DEBUG ((DEBUG_ERROR, "Invalid SMM range.\n"));
  201. return;
  202. }
  203. //
  204. // SMRR size must be of length 2^n
  205. // SMRR base alignment cannot be less than SMRR length
  206. //
  207. if ((SmmSize != GetPowerOfTwo32 (SmmSize)) || ((SmmBase & ~(SmmSize - 1)) != SmmBase)) {
  208. DEBUG ((DEBUG_ERROR, " Invalid SMM range.\n"));
  209. return;
  210. }
  211. //
  212. // Calculate smrr base, mask and pass them as arguments.
  213. //
  214. Arguments.Base = (SmmSize | MTRR_CACHE_WRITE_BACK);
  215. Arguments.Mask = (~(SmmSize - 1) & EFI_MSR_SMRR_MASK);
  216. //
  217. // Set SMRR valid bit
  218. //
  219. Arguments.Mask |= BIT11;
  220. //
  221. // Program smrr base and mask on BSP first and then on APs
  222. //
  223. SetSmrr (&Arguments);
  224. for (Index = 0; Index < gSmst->NumberOfCpus; Index++) {
  225. if (Index != gSmst->CurrentlyExecutingCpu) {
  226. Status = gSmst->SmmStartupThisAp (SetSmrr, Index, (VOID *)&Arguments);
  227. if (EFI_ERROR (Status)) {
  228. DEBUG ((DEBUG_ERROR, "Programming SMRR on AP# %d status: %r\n", Index, Status));
  229. }
  230. }
  231. }
  232. }
  233. /**
  234. Software SMI callback for restoring SMRR base and mask in S3 path.
  235. @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
  236. @param[in] Context Points to an optional handler context which was specified when the
  237. handler was registered.
  238. @param[in, out] CommBuffer A pointer to a collection of data in memory that will
  239. be conveyed from a non-SMM environment into an SMM environment.
  240. @param[in, out] CommBufferSize The size of the CommBuffer.
  241. @retval EFI_SUCCESS The interrupt was handled successfully.
  242. **/
  243. EFI_STATUS
  244. EFIAPI
  245. BlSwSmiHandler (
  246. IN EFI_HANDLE DispatchHandle,
  247. IN CONST VOID *Context,
  248. IN OUT VOID *CommBuffer,
  249. IN OUT UINTN *CommBufferSize
  250. )
  251. {
  252. SetSmrrOnS3 ();
  253. SmmFeatureLockOnS3 ();
  254. LockSmiGlobalEn ();
  255. return EFI_SUCCESS;
  256. }
  257. /**
  258. Lock SMI in this SMM ready to lock event.
  259. @param Protocol Points to the protocol's unique identifier
  260. @param Interface Points to the interface instance
  261. @param Handle The handle on which the interface was installed
  262. @retval EFI_SUCCESS SmmEventCallback runs successfully
  263. @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
  264. **/
  265. EFI_STATUS
  266. EFIAPI
  267. BlSupportSmmReadyToLockCallback (
  268. IN CONST EFI_GUID *Protocol,
  269. IN VOID *Interface,
  270. IN EFI_HANDLE Handle
  271. )
  272. {
  273. //
  274. // Set SMM SMI lock
  275. //
  276. LockSmiGlobalEn ();
  277. return EFI_SUCCESS;
  278. }
  279. /**
  280. The driver's entry point.
  281. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  282. @param[in] SystemTable A pointer to the EFI System Table.
  283. @retval EFI_SUCCESS The entry point is executed successfully.
  284. @retval Others Some error occurs when executing this entry point.
  285. **/
  286. EFI_STATUS
  287. EFIAPI
  288. BlSupportSmm (
  289. IN EFI_HANDLE ImageHandle,
  290. IN EFI_SYSTEM_TABLE *SystemTable
  291. )
  292. {
  293. EFI_STATUS Status;
  294. EFI_SMM_SW_DISPATCH2_PROTOCOL *SwDispatch;
  295. EFI_SMM_SW_REGISTER_CONTEXT SwContext;
  296. EFI_HANDLE SwHandle;
  297. EFI_HOB_GUID_TYPE *GuidHob;
  298. VOID *SmmHob;
  299. VOID *Registration;
  300. //
  301. // Get SMM S3 communication hob and save it
  302. //
  303. GuidHob = GetFirstGuidHob (&gS3CommunicationGuid);
  304. if (GuidHob != NULL) {
  305. SmmHob = (VOID *)(GET_GUID_HOB_DATA (GuidHob));
  306. CopyMem (&mPldS3Hob, SmmHob, GET_GUID_HOB_DATA_SIZE (GuidHob));
  307. } else {
  308. return EFI_NOT_FOUND;
  309. }
  310. if (mPldS3Hob.PldAcpiS3Enable) {
  311. // Other drivers will take care of S3.
  312. return EFI_SUCCESS;
  313. }
  314. //
  315. // Get smram hob and save it
  316. //
  317. GuidHob = GetFirstGuidHob (&gEfiSmmSmramMemoryGuid);
  318. if (GuidHob != NULL) {
  319. SmmHob = (VOID *)(GET_GUID_HOB_DATA (GuidHob));
  320. mSmramHob = AllocatePool (GET_GUID_HOB_DATA_SIZE (GuidHob));
  321. if (mSmramHob == NULL) {
  322. return EFI_OUT_OF_RESOURCES;
  323. }
  324. CopyMem (mSmramHob, SmmHob, GET_GUID_HOB_DATA_SIZE (GuidHob));
  325. } else {
  326. return EFI_NOT_FOUND;
  327. }
  328. //
  329. // Get SMM register hob and save it
  330. //
  331. GuidHob = GetFirstGuidHob (&gSmmRegisterInfoGuid);
  332. if (GuidHob != NULL) {
  333. SmmHob = (VOID *)(GET_GUID_HOB_DATA (GuidHob));
  334. mSmmRegisterHob = AllocatePool (GET_GUID_HOB_DATA_SIZE (GuidHob));
  335. if (mSmmRegisterHob == NULL) {
  336. return EFI_OUT_OF_RESOURCES;
  337. }
  338. CopyMem (mSmmRegisterHob, SmmHob, GET_GUID_HOB_DATA_SIZE (GuidHob));
  339. } else {
  340. return EFI_NOT_FOUND;
  341. }
  342. //
  343. // Get the Sw dispatch protocol and register SMI handler.
  344. //
  345. Status = gSmst->SmmLocateProtocol (&gEfiSmmSwDispatch2ProtocolGuid, NULL, (VOID **)&SwDispatch);
  346. if (EFI_ERROR (Status)) {
  347. return Status;
  348. }
  349. SwContext.SwSmiInputValue = (UINTN)-1;
  350. Status = SwDispatch->Register (SwDispatch, BlSwSmiHandler, &SwContext, &SwHandle);
  351. if (EFI_ERROR (Status)) {
  352. DEBUG ((DEBUG_ERROR, "Registering S3 smi handler failed: %r\n", Status));
  353. return Status;
  354. }
  355. //
  356. // Register SMM ready to lock callback
  357. //
  358. Status = gSmst->SmmRegisterProtocolNotify (
  359. &gEfiSmmReadyToLockProtocolGuid,
  360. BlSupportSmmReadyToLockCallback,
  361. &Registration
  362. );
  363. ASSERT_EFI_ERROR (Status);
  364. SaveSmmInfoForS3 ((UINT8)SwContext.SwSmiInputValue);
  365. return EFI_SUCCESS;
  366. }