]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/MpInitLib/X64/CreatePageTable.c
UefiCpuPkg: Put APs in 64 bit mode before handoff to OS.
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / X64 / CreatePageTable.c
1 /** @file
2 Function to create page talbe.
3 Only create page table for x64, and leave the CreatePageTable empty for Ia32.
4 Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 **/
7 #include <Library/CpuPageTableLib.h>
8 #include <Library/MemoryAllocationLib.h>
9 #include <Base.h>
10 #include <Library/BaseMemoryLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/BaseLib.h>
13
14 /**
15 Create 1:1 mapping page table in reserved memory to map the specified address range.
16 @param[in] LinearAddress The start of the linear address range.
17 @param[in] Length The length of the linear address range.
18 @return The page table to be created.
19 **/
20 UINTN
21 CreatePageTable (
22 IN UINTN Address,
23 IN UINTN Length
24 )
25 {
26 EFI_STATUS Status;
27 VOID *PageTableBuffer;
28 UINTN PageTableBufferSize;
29 UINTN PageTable;
30 PAGING_MODE PagingMode;
31 IA32_CR4 Cr4;
32
33 IA32_MAP_ATTRIBUTE MapAttribute;
34 IA32_MAP_ATTRIBUTE MapMask;
35
36 MapAttribute.Uint64 = Address;
37 MapAttribute.Bits.Present = 1;
38 MapAttribute.Bits.ReadWrite = 1;
39
40 MapMask.Bits.PageTableBaseAddress = 1;
41 MapMask.Bits.Present = 1;
42 MapMask.Bits.ReadWrite = 1;
43
44 PageTable = 0;
45 PageTableBufferSize = 0;
46
47 Cr4.UintN = AsmReadCr4 ();
48
49 if (Cr4.Bits.LA57 == 1) {
50 PagingMode = Paging5Level;
51 } else {
52 PagingMode = Paging4Level;
53 }
54
55 Status = PageTableMap (
56 &PageTable,
57 PagingMode,
58 NULL,
59 &PageTableBufferSize,
60 Address,
61 Length,
62 &MapAttribute,
63 &MapMask
64 );
65 ASSERT (Status == EFI_BUFFER_TOO_SMALL);
66 DEBUG ((DEBUG_INFO, "AP Page Table Buffer Size = %x\n", PageTableBufferSize));
67
68 PageTableBuffer = AllocateReservedPages (EFI_SIZE_TO_PAGES (PageTableBufferSize));
69 ASSERT (PageTableBuffer != NULL);
70 Status = PageTableMap (
71 &PageTable,
72 PagingMode,
73 PageTableBuffer,
74 &PageTableBufferSize,
75 Address,
76 Length,
77 &MapAttribute,
78 &MapMask
79 );
80 ASSERT_EFI_ERROR (Status);
81 return PageTable;
82 }