Repository navigation
Expand file tree
/
Copy pathmemory.c
More file actions
149 lines (127 loc) · 4.85 KB
/
Copy pathmemory.c
File metadata and controls
149 lines (127 loc) · 4.85 KB
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
/* SPDX-License-Identifier: BSD-3-Clause */
#include <atv1sm.h>
/**
Allocates one or more 4KB pages of a certain memory type.
Allocates the number of 4KB pages of a certain memory type and returns a pointer
to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
@param Type The type of allocation to perform.
@param MemoryType The type of memory to allocate.
@param Pages The number of 4 KB pages to allocate.
@param Memory The pointer to a physical address. On input, the
way in which the address is used depends on the
value of Type.
@retval EFI_SUCCESS The requested pages were allocated.
@retval EFI_OUT_OF_RESOURCES The pages could not be allocated.
@retval EFI_NOT_FOUND The requested pages could not be found.
**/
EFI_STATUS
EFIAPI
AllocatePagesEx (
IN EFI_ALLOCATE_TYPE Type,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages,
IN OUT EFI_PHYSICAL_ADDRESS *Memory
)
{
return gBS->AllocatePages (Type, MemoryType, Pages, Memory);
}
/**
Allocates one or more 4KB pages of a certain memory type at a specified alignment.
Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
specified by Alignment.
If Alignment is not a power of two and Alignment is not zero, then ASSERT().
If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
@param Type The type of allocation to perform.
@param MemoryType The type of memory to allocate.
@param Pages The number of 4 KB pages to allocate.
@param Alignment The requested alignment of the allocation. Must be a power of two.
If Alignment is zero, then byte alignment is used.
@param Memory The pointer to a physical address. On input, the
way in which the address is used depends on the
value of Type.
@retval EFI_SUCCESS The requested pages were allocated.
@retval EFI_OUT_OF_RESOURCES The pages could not be allocated.
@retval EFI_NOT_FOUND The requested pages could not be found.
**/
EFI_STATUS
EFIAPI
AllocateAlignedPagesEx (
IN EFI_ALLOCATE_TYPE Type,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN Pages,
IN UINTN Alignment,
IN OUT EFI_PHYSICAL_ADDRESS *Memory
)
{
EFI_STATUS Status;
EFI_PHYSICAL_ADDRESS Address;
EFI_PHYSICAL_ADDRESS AlignedMemory;
UINTN AlignmentMask;
UINTN UnalignedPages;
UINTN RealPages;
//
// Alignment must be a power of two or zero.
//
ASSERT ((Alignment & (Alignment - 1)) == 0);
//
// Reserving a specific address does not require guaranteeing alignment
// constraints.
//
ASSERT (Type != AllocateAddress);
if (Pages == 0) {
return EFI_INVALID_PARAMETER;
}
if (Alignment > EFI_PAGE_SIZE) {
//
// Calculate the total number of pages since alignment is larger than page size.
//
AlignmentMask = Alignment - 1;
RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);
//
// Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
//
ASSERT (RealPages > Pages);
Status = AllocatePagesEx (Type, MemoryType, RealPages, Memory);
if (EFI_ERROR (Status)) {
return Status;
}
Address = *Memory;
AlignedMemory = (Address + AlignmentMask) & ~(EFI_PHYSICAL_ADDRESS)AlignmentMask;
UnalignedPages = EFI_SIZE_TO_PAGES ((UINTN)(AlignedMemory - Address));
if (UnalignedPages > 0) {
//
// Free first unaligned page(s).
//
Status = gBS->FreePages (Address, UnalignedPages);
//ASSERT_EFI_ERROR (Status);
}
Address = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);
UnalignedPages = RealPages - Pages - UnalignedPages;
if (UnalignedPages > 0) {
//
// Free last unaligned page(s).
//
Status = gBS->FreePages (Address, UnalignedPages);
//ASSERT_EFI_ERROR (Status);
}
*Memory = AlignedMemory;
} else {
//
// Do not over-allocate pages in this case.
//
Status = AllocatePagesEx (Type, MemoryType, Pages, Memory);
if (EFI_ERROR (Status)) {
return Status;
}
}
return EFI_SUCCESS;
}
VOID
EFIAPI
FreeAlignedPages (
IN VOID *Buffer,
IN UINTN Pages
)
{
gBS->FreePages((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);
}