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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// SPDX-License-Identifier: GPL-2.0+
/*
* efi_selftest_set_virtual_address_map.c
*
* Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
*
* This test checks the notification of EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
* and the following services: SetVirtualAddressMap, ConvertPointer.
*/
#include <efi_selftest.h>
static const struct efi_boot_services *boottime;
static const struct efi_runtime_services *runtime;
static struct efi_event *event;
static struct efi_mem_desc *memory_map;
static efi_uintn_t map_size;
static efi_uintn_t desc_size;
static u32 desc_version;
static u64 page1;
static u64 page2;
static u32 notify_call_count;
static bool convert_pointer_failed;
/**
* notify () - notification function
*
* This function is called when the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event
* occurs. The correct output of ConvertPointer() is checked.
*
* @event notified event
* @context pointer to the notification count
*/
static void EFIAPI notify(struct efi_event *event, void *context)
{
void *addr;
efi_status_t ret;
++notify_call_count;
addr = (void *)(uintptr_t)page1;
ret = runtime->convert_pointer(0, &addr);
if (ret != EFI_SUCCESS) {
efi_st_error("ConvertPointer failed\n");
convert_pointer_failed = true;
return;
}
if ((uintptr_t)addr != page1 + EFI_PAGE_SIZE) {
efi_st_error("ConvertPointer wrong address\n");
convert_pointer_failed = true;
return;
}
addr = (void *)(uintptr_t)page2;
ret = runtime->convert_pointer(0, &addr);
if (ret != EFI_SUCCESS) {
efi_st_error("ConvertPointer failed\n");
convert_pointer_failed = true;
return;
}
if ((uintptr_t)addr != page2 + 2 * EFI_PAGE_SIZE) {
efi_st_error("ConvertPointer wrong address\n");
convert_pointer_failed = true;
}
}
/**
* setup() - setup unit test
*
* The memory map is read. Boottime only entries are deleted. Two entries for
* newly allocated pages are added. For these virtual addresses deviating from
* the physical addresses are set.
*
* @handle: handle of the loaded image
* @systable: system table
* @return: EFI_ST_SUCCESS for success
*/
static int setup(const efi_handle_t handle,
const struct efi_system_table *systable)
{
efi_uintn_t map_key;
efi_status_t ret;
struct efi_mem_desc *end, *pos1, *pos2;
boottime = systable->boottime;
runtime = systable->runtime;
ret = boottime->create_event(EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,
TPL_CALLBACK, notify, NULL,
&event);
if (ret != EFI_SUCCESS) {
efi_st_error("could not create event\n");
return EFI_ST_FAILURE;
}
ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size,
&desc_version);
if (ret != EFI_BUFFER_TOO_SMALL) {
efi_st_error(
"GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n");
return EFI_ST_FAILURE;
}
/* Allocate extra space for newly allocated memory */
map_size += 3 * sizeof(struct efi_mem_desc);
ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size,
(void **)&memory_map);
if (ret != EFI_SUCCESS) {
efi_st_error("AllocatePool failed\n");
return EFI_ST_FAILURE;
}
ret = boottime->get_memory_map(&map_size, memory_map, &map_key,
&desc_size, &desc_version);
if (ret != EFI_SUCCESS) {
efi_st_error("GetMemoryMap failed\n");
return EFI_ST_FAILURE;
}
ret = boottime->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
EFI_BOOT_SERVICES_DATA, 2, &page1);
if (ret != EFI_SUCCESS) {
efi_st_error("AllocatePages failed\n");
return EFI_ST_FAILURE;
}
ret = boottime->allocate_pages(EFI_ALLOCATE_ANY_PAGES,
EFI_BOOT_SERVICES_DATA, 3, &page2);
if (ret != EFI_SUCCESS) {
efi_st_error("AllocatePages failed\n");
return EFI_ST_FAILURE;
}
/* Remove entries not relevant for runtime from map */
end = (struct efi_mem_desc *)((u8 *)memory_map + map_size);
for (pos1 = memory_map, pos2 = memory_map;
pos2 < end; ++pos2) {
switch (pos2->type) {
case EFI_LOADER_CODE:
case EFI_LOADER_DATA:
case EFI_BOOT_SERVICES_CODE:
case EFI_BOOT_SERVICES_DATA:
case EFI_CONVENTIONAL_MEMORY:
continue;
}
memcpy(pos1, pos2, desc_size);
++pos1;
}
/*
* Add entries with virtual addresses deviating from the physical
* addresses. By choosing virtual address ranges within the allocated
* physical pages address space collisions are avoided.
*/
pos1->type = EFI_RUNTIME_SERVICES_DATA;
pos1->reserved = 0;
pos1->physical_start = page1;
pos1->virtual_start = page1 + EFI_PAGE_SIZE;
pos1->num_pages = 1;
pos1->attribute = EFI_MEMORY_RUNTIME;
++pos1;
pos1->type = EFI_RUNTIME_SERVICES_DATA;
pos1->reserved = 0;
pos1->physical_start = page2;
pos1->virtual_start = page2 + 2 * EFI_PAGE_SIZE;
pos1->num_pages = 1;
pos1->attribute = EFI_MEMORY_RUNTIME;
++pos1;
map_size = (u8 *)pos1 - (u8 *)memory_map;
return EFI_ST_SUCCESS;
}
/**
* execute() - execute unit test
*
* SetVirtualAddressMap() is called with the memory map prepared in setup().
*
* The triggering of the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event is checked via
* the call count of the notification function.
*
* @return: EFI_ST_SUCCESS for success
*/
static int execute(void)
{
efi_status_t ret;
ret = runtime->set_virtual_address_map(map_size, desc_size,
desc_version, memory_map);
if (ret != EFI_SUCCESS) {
efi_st_error("SetVirtualAddressMap failed\n");
return EFI_ST_FAILURE;
}
if (notify_call_count != 1) {
efi_st_error("EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE triggered %d times\n",
notify_call_count);
return EFI_ST_FAILURE;
}
if (convert_pointer_failed)
return EFI_ST_FAILURE;
return EFI_ST_SUCCESS;
}
EFI_UNIT_TEST(virtaddrmap) = {
.name = "virtual address map",
.phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
.setup = setup,
.execute = execute,
};
|