summaryrefslogtreecommitdiffstats
path: root/MdePkg/Library/DxeServicesLib/X64/Allocate.c
blob: 41c4cea391a93e319dec63cc195144d0c72f8c5d (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
/** @file
  DxeServicesLib memory allocation routines

  Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <PiDxe.h>
#include <Library/HobLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/DxeServicesLib.h>

/**
  Allocates one or more 4KB pages of a given type from a memory region that is
  accessible to PEI.

  Allocates the number of 4KB pages of type 'MemoryType' and returns a
  pointer to the allocated buffer.  The buffer returned is aligned on a 4KB
  boundary.  If Pages is 0, then NULL is returned.  If there is not enough
  memory remaining to satisfy the request, then NULL is returned.

  @param[in]  MemoryType            The memory type to allocate
  @param[in]  Pages                 The number of 4 KB pages to allocate.

  @return A pointer to the allocated buffer or NULL if allocation fails.

**/
VOID *
EFIAPI
AllocatePeiAccessiblePages (
  IN EFI_MEMORY_TYPE  MemoryType,
  IN UINTN            Pages
  )
{
  EFI_STATUS                  Status;
  EFI_ALLOCATE_TYPE           AllocType;
  EFI_PHYSICAL_ADDRESS        Memory;
  EFI_HOB_HANDOFF_INFO_TABLE  *PhitHob;

  if (Pages == 0) {
    return NULL;
  }

  AllocType = AllocateAnyPages;
  //
  // A X64 build of DXE may be combined with a 32-bit build of PEI, and so we
  // need to check the memory limit set by PEI, and allocate below 4 GB if the
  // limit is set to 4 GB or lower.
  //
  PhitHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList ();
  if (PhitHob->EfiFreeMemoryTop <= MAX_UINT32) {
    AllocType = AllocateMaxAddress;
    Memory = MAX_UINT32;
  }

  Status = gBS->AllocatePages (AllocType, MemoryType, Pages, &Memory);
  if (EFI_ERROR (Status)) {
    return NULL;
  }
  return (VOID *)(UINTN)Memory;
}