aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/ia64/kernel/efi.c7
-rw-r--r--arch/x86/Kconfig14
-rw-r--r--arch/x86/Kconfig.debug9
-rw-r--r--arch/x86/boot/Makefile2
-rw-r--r--arch/x86/boot/compressed/eboot.c1017
-rw-r--r--arch/x86/boot/compressed/eboot.h60
-rw-r--r--arch/x86/boot/compressed/efi_stub_64.S29
-rw-r--r--arch/x86/boot/compressed/head_32.S52
-rw-r--r--arch/x86/boot/compressed/head_64.S111
-rw-r--r--arch/x86/boot/header.S23
-rw-r--r--arch/x86/boot/tools/build.c77
-rw-r--r--arch/x86/include/asm/efi.h43
-rw-r--r--arch/x86/include/asm/pgtable.h3
-rw-r--r--arch/x86/include/asm/pgtable_types.h4
-rw-r--r--arch/x86/kernel/setup.c6
-rw-r--r--arch/x86/mm/dump_pagetables.c84
-rw-r--r--arch/x86/mm/fault.c7
-rw-r--r--arch/x86/mm/pageattr.c56
-rw-r--r--arch/x86/platform/efi/Makefile1
-rw-r--r--arch/x86/platform/efi/efi.c415
-rw-r--r--arch/x86/platform/efi/efi_32.c7
-rw-r--r--arch/x86/platform/efi/efi_64.c368
-rw-r--r--arch/x86/platform/efi/efi_stub_64.S166
-rw-r--r--arch/x86/platform/efi/efi_thunk_64.S65
-rw-r--r--drivers/firmware/efi/efi-stub-helper.c136
-rw-r--r--drivers/firmware/efi/efi.c5
-rw-r--r--drivers/firmware/efi/efivars.c2
-rw-r--r--fs/efivarfs/file.c13
-rw-r--r--include/linux/efi.h268
29 files changed, 2452 insertions, 598 deletions
diff --git a/arch/ia64/kernel/efi.c b/arch/ia64/kernel/efi.c
index da5b462e6de6..741b99c1a0b1 100644
--- a/arch/ia64/kernel/efi.c
+++ b/arch/ia64/kernel/efi.c
@@ -477,6 +477,9 @@ efi_init (void)
477 char *cp, vendor[100] = "unknown"; 477 char *cp, vendor[100] = "unknown";
478 int i; 478 int i;
479 479
480 set_bit(EFI_BOOT, &efi.flags);
481 set_bit(EFI_64BIT, &efi.flags);
482
480 /* 483 /*
481 * It's too early to be able to use the standard kernel command line 484 * It's too early to be able to use the standard kernel command line
482 * support... 485 * support...
@@ -529,6 +532,8 @@ efi_init (void)
529 efi.systab->hdr.revision >> 16, 532 efi.systab->hdr.revision >> 16,
530 efi.systab->hdr.revision & 0xffff, vendor); 533 efi.systab->hdr.revision & 0xffff, vendor);
531 534
535 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
536
532 palo_phys = EFI_INVALID_TABLE_ADDR; 537 palo_phys = EFI_INVALID_TABLE_ADDR;
533 538
534 if (efi_config_init(arch_tables) != 0) 539 if (efi_config_init(arch_tables) != 0)
@@ -657,6 +662,8 @@ efi_enter_virtual_mode (void)
657 return; 662 return;
658 } 663 }
659 664
665 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
666
660 /* 667 /*
661 * Now that EFI is in virtual mode, we call the EFI functions more 668 * Now that EFI is in virtual mode, we call the EFI functions more
662 * efficiently: 669 * efficiently:
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 0af5250d914f..8453fe1342ea 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1585,6 +1585,20 @@ config EFI_STUB
1585 1585
1586 See Documentation/efi-stub.txt for more information. 1586 See Documentation/efi-stub.txt for more information.
1587 1587
1588config EFI_MIXED
1589 bool "EFI mixed-mode support"
1590 depends on EFI_STUB && X86_64
1591 ---help---
1592 Enabling this feature allows a 64-bit kernel to be booted
1593 on a 32-bit firmware, provided that your CPU supports 64-bit
1594 mode.
1595
1596 Note that it is not possible to boot a mixed-mode enabled
1597 kernel via the EFI boot stub - a bootloader that supports
1598 the EFI handover protocol must be used.
1599
1600 If unsure, say N.
1601
1588config SECCOMP 1602config SECCOMP
1589 def_bool y 1603 def_bool y
1590 prompt "Enable seccomp to safely compute untrusted bytecode" 1604 prompt "Enable seccomp to safely compute untrusted bytecode"
diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug
index 321a52ccf63a..61bd2ad94281 100644
--- a/arch/x86/Kconfig.debug
+++ b/arch/x86/Kconfig.debug
@@ -81,6 +81,15 @@ config X86_PTDUMP
81 kernel. 81 kernel.
82 If in doubt, say "N" 82 If in doubt, say "N"
83 83
84config EFI_PGT_DUMP
85 bool "Dump the EFI pagetable"
86 depends on EFI && X86_PTDUMP
87 ---help---
88 Enable this if you want to dump the EFI page table before
89 enabling virtual mode. This can be used to debug miscellaneous
90 issues with the mapping of the EFI runtime regions into that
91 table.
92
84config DEBUG_RODATA 93config DEBUG_RODATA
85 bool "Write protect kernel read-only data structures" 94 bool "Write protect kernel read-only data structures"
86 default y 95 default y
diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile
index 878df7e88cd4..abb9eba61b50 100644
--- a/arch/x86/boot/Makefile
+++ b/arch/x86/boot/Makefile
@@ -80,7 +80,7 @@ targets += voffset.h
80$(obj)/voffset.h: vmlinux FORCE 80$(obj)/voffset.h: vmlinux FORCE
81 $(call if_changed,voffset) 81 $(call if_changed,voffset)
82 82
83sed-zoffset := -e 's/^\([0-9a-fA-F]*\) . \(startup_32\|startup_64\|efi_pe_entry\|efi_stub_entry\|input_data\|_end\|z_.*\)$$/\#define ZO_\2 0x\1/p' 83sed-zoffset := -e 's/^\([0-9a-fA-F]*\) . \(startup_32\|startup_64\|efi32_stub_entry\|efi64_stub_entry\|efi_pe_entry\|input_data\|_end\|z_.*\)$$/\#define ZO_\2 0x\1/p'
84 84
85quiet_cmd_zoffset = ZOFFSET $@ 85quiet_cmd_zoffset = ZOFFSET $@
86 cmd_zoffset = $(NM) $< | sed -n $(sed-zoffset) > $@ 86 cmd_zoffset = $(NM) $< | sed -n $(sed-zoffset) > $@
diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index a7677babf946..1e6146137f8e 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -19,10 +19,272 @@
19 19
20static efi_system_table_t *sys_table; 20static efi_system_table_t *sys_table;
21 21
22static struct efi_config *efi_early;
23
24#define efi_call_early(f, ...) \
25 efi_early->call(efi_early->f, __VA_ARGS__);
26
27#define BOOT_SERVICES(bits) \
28static void setup_boot_services##bits(struct efi_config *c) \
29{ \
30 efi_system_table_##bits##_t *table; \
31 efi_boot_services_##bits##_t *bt; \
32 \
33 table = (typeof(table))sys_table; \
34 \
35 c->text_output = table->con_out; \
36 \
37 bt = (typeof(bt))(unsigned long)(table->boottime); \
38 \
39 c->allocate_pool = bt->allocate_pool; \
40 c->allocate_pages = bt->allocate_pages; \
41 c->get_memory_map = bt->get_memory_map; \
42 c->free_pool = bt->free_pool; \
43 c->free_pages = bt->free_pages; \
44 c->locate_handle = bt->locate_handle; \
45 c->handle_protocol = bt->handle_protocol; \
46 c->exit_boot_services = bt->exit_boot_services; \
47}
48BOOT_SERVICES(32);
49BOOT_SERVICES(64);
22 50
23#include "../../../../drivers/firmware/efi/efi-stub-helper.c" 51static void efi_printk(efi_system_table_t *, char *);
52static void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
53
54static efi_status_t
55__file_size32(void *__fh, efi_char16_t *filename_16,
56 void **handle, u64 *file_sz)
57{
58 efi_file_handle_32_t *h, *fh = __fh;
59 efi_file_info_t *info;
60 efi_status_t status;
61 efi_guid_t info_guid = EFI_FILE_INFO_ID;
62 u32 info_sz;
63
64 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
65 EFI_FILE_MODE_READ, (u64)0);
66 if (status != EFI_SUCCESS) {
67 efi_printk(sys_table, "Failed to open file: ");
68 efi_char16_printk(sys_table, filename_16);
69 efi_printk(sys_table, "\n");
70 return status;
71 }
72
73 *handle = h;
74
75 info_sz = 0;
76 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
77 &info_sz, NULL);
78 if (status != EFI_BUFFER_TOO_SMALL) {
79 efi_printk(sys_table, "Failed to get file info size\n");
80 return status;
81 }
82
83grow:
84 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
85 info_sz, (void **)&info);
86 if (status != EFI_SUCCESS) {
87 efi_printk(sys_table, "Failed to alloc mem for file info\n");
88 return status;
89 }
90
91 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
92 &info_sz, info);
93 if (status == EFI_BUFFER_TOO_SMALL) {
94 efi_call_early(free_pool, info);
95 goto grow;
96 }
97
98 *file_sz = info->file_size;
99 efi_call_early(free_pool, info);
100
101 if (status != EFI_SUCCESS)
102 efi_printk(sys_table, "Failed to get initrd info\n");
103
104 return status;
105}
106
107static efi_status_t
108__file_size64(void *__fh, efi_char16_t *filename_16,
109 void **handle, u64 *file_sz)
110{
111 efi_file_handle_64_t *h, *fh = __fh;
112 efi_file_info_t *info;
113 efi_status_t status;
114 efi_guid_t info_guid = EFI_FILE_INFO_ID;
115 u32 info_sz;
24 116
117 status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
118 EFI_FILE_MODE_READ, (u64)0);
119 if (status != EFI_SUCCESS) {
120 efi_printk(sys_table, "Failed to open file: ");
121 efi_char16_printk(sys_table, filename_16);
122 efi_printk(sys_table, "\n");
123 return status;
124 }
25 125
126 *handle = h;
127
128 info_sz = 0;
129 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
130 &info_sz, NULL);
131 if (status != EFI_BUFFER_TOO_SMALL) {
132 efi_printk(sys_table, "Failed to get file info size\n");
133 return status;
134 }
135
136grow:
137 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
138 info_sz, (void **)&info);
139 if (status != EFI_SUCCESS) {
140 efi_printk(sys_table, "Failed to alloc mem for file info\n");
141 return status;
142 }
143
144 status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
145 &info_sz, info);
146 if (status == EFI_BUFFER_TOO_SMALL) {
147 efi_call_early(free_pool, info);
148 goto grow;
149 }
150
151 *file_sz = info->file_size;
152 efi_call_early(free_pool, info);
153
154 if (status != EFI_SUCCESS)
155 efi_printk(sys_table, "Failed to get initrd info\n");
156
157 return status;
158}
159static efi_status_t
160efi_file_size(efi_system_table_t *sys_table, void *__fh,
161 efi_char16_t *filename_16, void **handle, u64 *file_sz)
162{
163 if (efi_early->is64)
164 return __file_size64(__fh, filename_16, handle, file_sz);
165
166 return __file_size32(__fh, filename_16, handle, file_sz);
167}
168
169static inline efi_status_t
170efi_file_read(void *__fh, void *handle, unsigned long *size, void *addr)
171{
172 unsigned long func;
173
174 if (efi_early->is64) {
175 efi_file_handle_64_t *fh = __fh;
176
177 func = (unsigned long)fh->read;
178 return efi_early->call(func, handle, size, addr);
179 } else {
180 efi_file_handle_32_t *fh = __fh;
181
182 func = (unsigned long)fh->read;
183 return efi_early->call(func, handle, size, addr);
184 }
185}
186
187static inline efi_status_t efi_file_close(void *__fh, void *handle)
188{
189 if (efi_early->is64) {
190 efi_file_handle_64_t *fh = __fh;
191
192 return efi_early->call((unsigned long)fh->close, handle);
193 } else {
194 efi_file_handle_32_t *fh = __fh;
195
196 return efi_early->call((unsigned long)fh->close, handle);
197 }
198}
199
200static inline efi_status_t __open_volume32(void *__image, void **__fh)
201{
202 efi_file_io_interface_t *io;
203 efi_loaded_image_32_t *image = __image;
204 efi_file_handle_32_t *fh;
205 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
206 efi_status_t status;
207 void *handle = (void *)(unsigned long)image->device_handle;
208 unsigned long func;
209
210 status = efi_call_early(handle_protocol, handle,
211 &fs_proto, (void **)&io);
212 if (status != EFI_SUCCESS) {
213 efi_printk(sys_table, "Failed to handle fs_proto\n");
214 return status;
215 }
216
217 func = (unsigned long)io->open_volume;
218 status = efi_early->call(func, io, &fh);
219 if (status != EFI_SUCCESS)
220 efi_printk(sys_table, "Failed to open volume\n");
221
222 *__fh = fh;
223 return status;
224}
225
226static inline efi_status_t __open_volume64(void *__image, void **__fh)
227{
228 efi_file_io_interface_t *io;
229 efi_loaded_image_64_t *image = __image;
230 efi_file_handle_64_t *fh;
231 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
232 efi_status_t status;
233 void *handle = (void *)(unsigned long)image->device_handle;
234 unsigned long func;
235
236 status = efi_call_early(handle_protocol, handle,
237 &fs_proto, (void **)&io);
238 if (status != EFI_SUCCESS) {
239 efi_printk(sys_table, "Failed to handle fs_proto\n");
240 return status;
241 }
242
243 func = (unsigned long)io->open_volume;
244 status = efi_early->call(func, io, &fh);
245 if (status != EFI_SUCCESS)
246 efi_printk(sys_table, "Failed to open volume\n");
247
248 *__fh = fh;
249 return status;
250}
251
252static inline efi_status_t
253efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
254{
255 if (efi_early->is64)
256 return __open_volume64(__image, __fh);
257
258 return __open_volume32(__image, __fh);
259}
260
261static void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
262{
263 unsigned long output_string;
264 size_t offset;
265
266 if (efi_early->is64) {
267 struct efi_simple_text_output_protocol_64 *out;
268 u64 *func;
269
270 offset = offsetof(typeof(*out), output_string);
271 output_string = efi_early->text_output + offset;
272 func = (u64 *)output_string;
273
274 efi_early->call(*func, efi_early->text_output, str);
275 } else {
276 struct efi_simple_text_output_protocol_32 *out;
277 u32 *func;
278
279 offset = offsetof(typeof(*out), output_string);
280 output_string = efi_early->text_output + offset;
281 func = (u32 *)output_string;
282
283 efi_early->call(*func, efi_early->text_output, str);
284 }
285}
286
287#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
26 288
27static void find_bits(unsigned long mask, u8 *pos, u8 *size) 289static void find_bits(unsigned long mask, u8 *pos, u8 *size)
28{ 290{
@@ -47,105 +309,97 @@ static void find_bits(unsigned long mask, u8 *pos, u8 *size)
47 *size = len; 309 *size = len;
48} 310}
49 311
50static efi_status_t setup_efi_pci(struct boot_params *params) 312static efi_status_t
313__setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
51{ 314{
52 efi_pci_io_protocol *pci; 315 struct pci_setup_rom *rom = NULL;
53 efi_status_t status; 316 efi_status_t status;
54 void **pci_handle; 317 unsigned long size;
55 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; 318 uint64_t attributes;
56 unsigned long nr_pci, size = 0;
57 int i;
58 struct setup_data *data;
59 319
60 data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 320 status = efi_early->call(pci->attributes, pci,
321 EfiPciIoAttributeOperationGet, 0, 0,
322 &attributes);
323 if (status != EFI_SUCCESS)
324 return status;
61 325
62 while (data && data->next) 326 if (!pci->romimage || !pci->romsize)
63 data = (struct setup_data *)(unsigned long)data->next; 327 return EFI_INVALID_PARAMETER;
64 328
65 status = efi_call_phys5(sys_table->boottime->locate_handle, 329 size = pci->romsize + sizeof(*rom);
66 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
67 NULL, &size, pci_handle);
68 330
69 if (status == EFI_BUFFER_TOO_SMALL) { 331 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
70 status = efi_call_phys3(sys_table->boottime->allocate_pool, 332 if (status != EFI_SUCCESS)
71 EFI_LOADER_DATA, size, &pci_handle); 333 return status;
72 334
73 if (status != EFI_SUCCESS) 335 memset(rom, 0, sizeof(*rom));
74 return status;
75 336
76 status = efi_call_phys5(sys_table->boottime->locate_handle, 337 rom->data.type = SETUP_PCI;
77 EFI_LOCATE_BY_PROTOCOL, &pci_proto, 338 rom->data.len = size - sizeof(struct setup_data);
78 NULL, &size, pci_handle); 339 rom->data.next = 0;
79 } 340 rom->pcilen = pci->romsize;
341 *__rom = rom;
80 342
81 if (status != EFI_SUCCESS) 343 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
82 goto free_handle; 344 PCI_VENDOR_ID, 1, &(rom->vendor));
83
84 nr_pci = size / sizeof(void *);
85 for (i = 0; i < nr_pci; i++) {
86 void *h = pci_handle[i];
87 uint64_t attributes;
88 struct pci_setup_rom *rom;
89 345
90 status = efi_call_phys3(sys_table->boottime->handle_protocol, 346 if (status != EFI_SUCCESS)
91 h, &pci_proto, &pci); 347 goto free_struct;
92 348
93 if (status != EFI_SUCCESS) 349 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
94 continue; 350 PCI_DEVICE_ID, 1, &(rom->devid));
95 351
96 if (!pci) 352 if (status != EFI_SUCCESS)
97 continue; 353 goto free_struct;
98 354
99#ifdef CONFIG_X86_64 355 status = efi_early->call(pci->get_location, pci, &(rom->segment),
100 status = efi_call_phys4(pci->attributes, pci, 356 &(rom->bus), &(rom->device), &(rom->function));
101 EfiPciIoAttributeOperationGet, 0,
102 &attributes);
103#else
104 status = efi_call_phys5(pci->attributes, pci,
105 EfiPciIoAttributeOperationGet, 0, 0,
106 &attributes);
107#endif
108 if (status != EFI_SUCCESS)
109 continue;
110 357
111 if (!pci->romimage || !pci->romsize) 358 if (status != EFI_SUCCESS)
112 continue; 359 goto free_struct;
113 360
114 size = pci->romsize + sizeof(*rom); 361 memcpy(rom->romdata, pci->romimage, pci->romsize);
362 return status;
115 363
116 status = efi_call_phys3(sys_table->boottime->allocate_pool, 364free_struct:
117 EFI_LOADER_DATA, size, &rom); 365 efi_call_early(free_pool, rom);
366 return status;
367}
118 368
119 if (status != EFI_SUCCESS) 369static efi_status_t
120 continue; 370setup_efi_pci32(struct boot_params *params, void **pci_handle,
371 unsigned long size)
372{
373 efi_pci_io_protocol_32 *pci = NULL;
374 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
375 u32 *handles = (u32 *)(unsigned long)pci_handle;
376 efi_status_t status;
377 unsigned long nr_pci;
378 struct setup_data *data;
379 int i;
121 380
122 rom->data.type = SETUP_PCI; 381 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
123 rom->data.len = size - sizeof(struct setup_data);
124 rom->data.next = 0;
125 rom->pcilen = pci->romsize;
126 382
127 status = efi_call_phys5(pci->pci.read, pci, 383 while (data && data->next)
128 EfiPciIoWidthUint16, PCI_VENDOR_ID, 384 data = (struct setup_data *)(unsigned long)data->next;
129 1, &(rom->vendor));
130 385
131 if (status != EFI_SUCCESS) 386 nr_pci = size / sizeof(u32);
132 goto free_struct; 387 for (i = 0; i < nr_pci; i++) {
388 struct pci_setup_rom *rom = NULL;
389 u32 h = handles[i];
133 390
134 status = efi_call_phys5(pci->pci.read, pci, 391 status = efi_call_early(handle_protocol, h,
135 EfiPciIoWidthUint16, PCI_DEVICE_ID, 392 &pci_proto, (void **)&pci);
136 1, &(rom->devid));
137 393
138 if (status != EFI_SUCCESS) 394 if (status != EFI_SUCCESS)
139 goto free_struct; 395 continue;
140 396
141 status = efi_call_phys5(pci->get_location, pci, 397 if (!pci)
142 &(rom->segment), &(rom->bus), 398 continue;
143 &(rom->device), &(rom->function));
144 399
400 status = __setup_efi_pci32(pci, &rom);
145 if (status != EFI_SUCCESS) 401 if (status != EFI_SUCCESS)
146 goto free_struct; 402 continue;
147
148 memcpy(rom->romdata, pci->romimage, pci->romsize);
149 403
150 if (data) 404 if (data)
151 data->next = (unsigned long)rom; 405 data->next = (unsigned long)rom;
@@ -154,105 +408,155 @@ static efi_status_t setup_efi_pci(struct boot_params *params)
154 408
155 data = (struct setup_data *)rom; 409 data = (struct setup_data *)rom;
156 410
157 continue;
158 free_struct:
159 efi_call_phys1(sys_table->boottime->free_pool, rom);
160 } 411 }
161 412
162free_handle:
163 efi_call_phys1(sys_table->boottime->free_pool, pci_handle);
164 return status; 413 return status;
165} 414}
166 415
167/* 416static efi_status_t
168 * See if we have Graphics Output Protocol 417__setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
169 */
170static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
171 unsigned long size)
172{ 418{
173 struct efi_graphics_output_protocol *gop, *first_gop; 419 struct pci_setup_rom *rom;
174 struct efi_pixel_bitmask pixel_info;
175 unsigned long nr_gops;
176 efi_status_t status; 420 efi_status_t status;
177 void **gop_handle; 421 unsigned long size;
178 u16 width, height; 422 uint64_t attributes;
179 u32 fb_base, fb_size;
180 u32 pixels_per_scan_line;
181 int pixel_format;
182 int i;
183 423
184 status = efi_call_phys3(sys_table->boottime->allocate_pool, 424 status = efi_early->call(pci->attributes, pci,
185 EFI_LOADER_DATA, size, &gop_handle); 425 EfiPciIoAttributeOperationGet, 0,
426 &attributes);
186 if (status != EFI_SUCCESS) 427 if (status != EFI_SUCCESS)
187 return status; 428 return status;
188 429
189 status = efi_call_phys5(sys_table->boottime->locate_handle, 430 if (!pci->romimage || !pci->romsize)
190 EFI_LOCATE_BY_PROTOCOL, proto, 431 return EFI_INVALID_PARAMETER;
191 NULL, &size, gop_handle); 432
433 size = pci->romsize + sizeof(*rom);
434
435 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
192 if (status != EFI_SUCCESS) 436 if (status != EFI_SUCCESS)
193 goto free_handle; 437 return status;
194 438
195 first_gop = NULL; 439 rom->data.type = SETUP_PCI;
440 rom->data.len = size - sizeof(struct setup_data);
441 rom->data.next = 0;
442 rom->pcilen = pci->romsize;
443 *__rom = rom;
196 444
197 nr_gops = size / sizeof(void *); 445 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
198 for (i = 0; i < nr_gops; i++) { 446 PCI_VENDOR_ID, 1, &(rom->vendor));
199 struct efi_graphics_output_mode_info *info; 447
200 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID; 448 if (status != EFI_SUCCESS)
201 bool conout_found = false; 449 goto free_struct;
202 void *dummy; 450
203 void *h = gop_handle[i]; 451 status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
452 PCI_DEVICE_ID, 1, &(rom->devid));
453
454 if (status != EFI_SUCCESS)
455 goto free_struct;
456
457 status = efi_early->call(pci->get_location, pci, &(rom->segment),
458 &(rom->bus), &(rom->device), &(rom->function));
459
460 if (status != EFI_SUCCESS)
461 goto free_struct;
462
463 memcpy(rom->romdata, pci->romimage, pci->romsize);
464 return status;
465
466free_struct:
467 efi_call_early(free_pool, rom);
468 return status;
469
470}
471
472static efi_status_t
473setup_efi_pci64(struct boot_params *params, void **pci_handle,
474 unsigned long size)
475{
476 efi_pci_io_protocol_64 *pci = NULL;
477 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
478 u64 *handles = (u64 *)(unsigned long)pci_handle;
479 efi_status_t status;
480 unsigned long nr_pci;
481 struct setup_data *data;
482 int i;
483
484 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
485
486 while (data && data->next)
487 data = (struct setup_data *)(unsigned long)data->next;
488
489 nr_pci = size / sizeof(u64);
490 for (i = 0; i < nr_pci; i++) {
491 struct pci_setup_rom *rom = NULL;
492 u64 h = handles[i];
493
494 status = efi_call_early(handle_protocol, h,
495 &pci_proto, (void **)&pci);
204 496
205 status = efi_call_phys3(sys_table->boottime->handle_protocol,
206 h, proto, &gop);
207 if (status != EFI_SUCCESS) 497 if (status != EFI_SUCCESS)
208 continue; 498 continue;
209 499
210 status = efi_call_phys3(sys_table->boottime->handle_protocol, 500 if (!pci)
211 h, &conout_proto, &dummy); 501 continue;
212 502
213 if (status == EFI_SUCCESS) 503 status = __setup_efi_pci64(pci, &rom);
214 conout_found = true; 504 if (status != EFI_SUCCESS)
505 continue;
215 506
216 status = efi_call_phys4(gop->query_mode, gop, 507 if (data)
217 gop->mode->mode, &size, &info); 508 data->next = (unsigned long)rom;
218 if (status == EFI_SUCCESS && (!first_gop || conout_found)) { 509 else
219 /* 510 params->hdr.setup_data = (unsigned long)rom;
220 * Systems that use the UEFI Console Splitter may 511
221 * provide multiple GOP devices, not all of which are 512 data = (struct setup_data *)rom;
222 * backed by real hardware. The workaround is to search
223 * for a GOP implementing the ConOut protocol, and if
224 * one isn't found, to just fall back to the first GOP.
225 */
226 width = info->horizontal_resolution;
227 height = info->vertical_resolution;
228 fb_base = gop->mode->frame_buffer_base;
229 fb_size = gop->mode->frame_buffer_size;
230 pixel_format = info->pixel_format;
231 pixel_info = info->pixel_information;
232 pixels_per_scan_line = info->pixels_per_scan_line;
233 513
234 /*
235 * Once we've found a GOP supporting ConOut,
236 * don't bother looking any further.
237 */
238 first_gop = gop;
239 if (conout_found)
240 break;
241 }
242 } 514 }
243 515
244 /* Did we find any GOPs? */ 516 return status;
245 if (!first_gop) 517}
518
519static efi_status_t setup_efi_pci(struct boot_params *params)
520{
521 efi_status_t status;
522 void **pci_handle = NULL;
523 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
524 unsigned long size = 0;
525
526 status = efi_call_early(locate_handle,
527 EFI_LOCATE_BY_PROTOCOL,
528 &pci_proto, NULL, &size, pci_handle);
529
530 if (status == EFI_BUFFER_TOO_SMALL) {
531 status = efi_call_early(allocate_pool,
532 EFI_LOADER_DATA,
533 size, (void **)&pci_handle);
534
535 if (status != EFI_SUCCESS)
536 return status;
537
538 status = efi_call_early(locate_handle,
539 EFI_LOCATE_BY_PROTOCOL, &pci_proto,
540 NULL, &size, pci_handle);
541 }
542
543 if (status != EFI_SUCCESS)
246 goto free_handle; 544 goto free_handle;
247 545
248 /* EFI framebuffer */ 546 if (efi_early->is64)
249 si->orig_video_isVGA = VIDEO_TYPE_EFI; 547 status = setup_efi_pci64(params, pci_handle, size);
548 else
549 status = setup_efi_pci32(params, pci_handle, size);
250 550
251 si->lfb_width = width; 551free_handle:
252 si->lfb_height = height; 552 efi_call_early(free_pool, pci_handle);
253 si->lfb_base = fb_base; 553 return status;
254 si->pages = 1; 554}
255 555
556static void
557setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
558 struct efi_pixel_bitmask pixel_info, int pixel_format)
559{
256 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) { 560 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
257 si->lfb_depth = 32; 561 si->lfb_depth = 32;
258 si->lfb_linelength = pixels_per_scan_line * 4; 562 si->lfb_linelength = pixels_per_scan_line * 4;
@@ -297,62 +601,319 @@ static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
297 si->rsvd_size = 0; 601 si->rsvd_size = 0;
298 si->rsvd_pos = 0; 602 si->rsvd_pos = 0;
299 } 603 }
604}
605
606static efi_status_t
607__gop_query32(struct efi_graphics_output_protocol_32 *gop32,
608 struct efi_graphics_output_mode_info **info,
609 unsigned long *size, u32 *fb_base)
610{
611 struct efi_graphics_output_protocol_mode_32 *mode;
612 efi_status_t status;
613 unsigned long m;
614
615 m = gop32->mode;
616 mode = (struct efi_graphics_output_protocol_mode_32 *)m;
617
618 status = efi_early->call(gop32->query_mode, gop32,
619 mode->mode, size, info);
620 if (status != EFI_SUCCESS)
621 return status;
622
623 *fb_base = mode->frame_buffer_base;
624 return status;
625}
626
627static efi_status_t
628setup_gop32(struct screen_info *si, efi_guid_t *proto,
629 unsigned long size, void **gop_handle)
630{
631 struct efi_graphics_output_protocol_32 *gop32, *first_gop;
632 unsigned long nr_gops;
633 u16 width, height;
634 u32 pixels_per_scan_line;
635 u32 fb_base;
636 struct efi_pixel_bitmask pixel_info;
637 int pixel_format;
638 efi_status_t status;
639 u32 *handles = (u32 *)(unsigned long)gop_handle;
640 int i;
641
642 first_gop = NULL;
643 gop32 = NULL;
644
645 nr_gops = size / sizeof(u32);
646 for (i = 0; i < nr_gops; i++) {
647 struct efi_graphics_output_mode_info *info = NULL;
648 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
649 bool conout_found = false;
650 void *dummy = NULL;
651 u32 h = handles[i];
652
653 status = efi_call_early(handle_protocol, h,
654 proto, (void **)&gop32);
655 if (status != EFI_SUCCESS)
656 continue;
657
658 status = efi_call_early(handle_protocol, h,
659 &conout_proto, &dummy);
660 if (status == EFI_SUCCESS)
661 conout_found = true;
662
663 status = __gop_query32(gop32, &info, &size, &fb_base);
664 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
665 /*
666 * Systems that use the UEFI Console Splitter may
667 * provide multiple GOP devices, not all of which are
668 * backed by real hardware. The workaround is to search
669 * for a GOP implementing the ConOut protocol, and if
670 * one isn't found, to just fall back to the first GOP.
671 */
672 width = info->horizontal_resolution;
673 height = info->vertical_resolution;
674 pixel_format = info->pixel_format;
675 pixel_info = info->pixel_information;
676 pixels_per_scan_line = info->pixels_per_scan_line;
677
678 /*
679 * Once we've found a GOP supporting ConOut,
680 * don't bother looking any further.
681 */
682 first_gop = gop32;
683 if (conout_found)
684 break;
685 }
686 }
687
688 /* Did we find any GOPs? */
689 if (!first_gop)
690 goto out;
691
692 /* EFI framebuffer */
693 si->orig_video_isVGA = VIDEO_TYPE_EFI;
694
695 si->lfb_width = width;
696 si->lfb_height = height;
697 si->lfb_base = fb_base;
698 si->pages = 1;
699
700 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
300 701
301 si->lfb_size = si->lfb_linelength * si->lfb_height; 702 si->lfb_size = si->lfb_linelength * si->lfb_height;
302 703
303 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS; 704 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
705out:
706 return status;
707}
304 708
305free_handle: 709static efi_status_t
306 efi_call_phys1(sys_table->boottime->free_pool, gop_handle); 710__gop_query64(struct efi_graphics_output_protocol_64 *gop64,
711 struct efi_graphics_output_mode_info **info,
712 unsigned long *size, u32 *fb_base)
713{
714 struct efi_graphics_output_protocol_mode_64 *mode;
715 efi_status_t status;
716 unsigned long m;
717
718 m = gop64->mode;
719 mode = (struct efi_graphics_output_protocol_mode_64 *)m;
720
721 status = efi_early->call(gop64->query_mode, gop64,
722 mode->mode, size, info);
723 if (status != EFI_SUCCESS)
724 return status;
725
726 *fb_base = mode->frame_buffer_base;
727 return status;
728}
729
730static efi_status_t
731setup_gop64(struct screen_info *si, efi_guid_t *proto,
732 unsigned long size, void **gop_handle)
733{
734 struct efi_graphics_output_protocol_64 *gop64, *first_gop;
735 unsigned long nr_gops;
736 u16 width, height;
737 u32 pixels_per_scan_line;
738 u32 fb_base;
739 struct efi_pixel_bitmask pixel_info;
740 int pixel_format;
741 efi_status_t status;
742 u64 *handles = (u64 *)(unsigned long)gop_handle;
743 int i;
744
745 first_gop = NULL;
746 gop64 = NULL;
747
748 nr_gops = size / sizeof(u64);
749 for (i = 0; i < nr_gops; i++) {
750 struct efi_graphics_output_mode_info *info = NULL;
751 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
752 bool conout_found = false;
753 void *dummy = NULL;
754 u64 h = handles[i];
755
756 status = efi_call_early(handle_protocol, h,
757 proto, (void **)&gop64);
758 if (status != EFI_SUCCESS)
759 continue;
760
761 status = efi_call_early(handle_protocol, h,
762 &conout_proto, &dummy);
763 if (status == EFI_SUCCESS)
764 conout_found = true;
765
766 status = __gop_query64(gop64, &info, &size, &fb_base);
767 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
768 /*
769 * Systems that use the UEFI Console Splitter may
770 * provide multiple GOP devices, not all of which are
771 * backed by real hardware. The workaround is to search
772 * for a GOP implementing the ConOut protocol, and if
773 * one isn't found, to just fall back to the first GOP.
774 */
775 width = info->horizontal_resolution;
776 height = info->vertical_resolution;
777 pixel_format = info->pixel_format;
778 pixel_info = info->pixel_information;
779 pixels_per_scan_line = info->pixels_per_scan_line;
780
781 /*
782 * Once we've found a GOP supporting ConOut,
783 * don't bother looking any further.
784 */
785 first_gop = gop64;
786 if (conout_found)
787 break;
788 }
789 }
790
791 /* Did we find any GOPs? */
792 if (!first_gop)
793 goto out;
794
795 /* EFI framebuffer */
796 si->orig_video_isVGA = VIDEO_TYPE_EFI;
797
798 si->lfb_width = width;
799 si->lfb_height = height;
800 si->lfb_base = fb_base;
801 si->pages = 1;
802
803 setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
804
805 si->lfb_size = si->lfb_linelength * si->lfb_height;
806
807 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
808out:
307 return status; 809 return status;
308} 810}
309 811
310/* 812/*
311 * See if we have Universal Graphics Adapter (UGA) protocol 813 * See if we have Graphics Output Protocol
312 */ 814 */
313static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto, 815static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
314 unsigned long size) 816 unsigned long size)
315{ 817{
316 struct efi_uga_draw_protocol *uga, *first_uga;
317 unsigned long nr_ugas;
318 efi_status_t status; 818 efi_status_t status;
319 u32 width, height; 819 void **gop_handle = NULL;
320 void **uga_handle = NULL;
321 int i;
322 820
323 status = efi_call_phys3(sys_table->boottime->allocate_pool, 821 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
324 EFI_LOADER_DATA, size, &uga_handle); 822 size, (void **)&gop_handle);
325 if (status != EFI_SUCCESS) 823 if (status != EFI_SUCCESS)
326 return status; 824 return status;
327 825
328 status = efi_call_phys5(sys_table->boottime->locate_handle, 826 status = efi_call_early(locate_handle,
329 EFI_LOCATE_BY_PROTOCOL, uga_proto, 827 EFI_LOCATE_BY_PROTOCOL,
330 NULL, &size, uga_handle); 828 proto, NULL, &size, gop_handle);
331 if (status != EFI_SUCCESS) 829 if (status != EFI_SUCCESS)
332 goto free_handle; 830 goto free_handle;
333 831
832 if (efi_early->is64)
833 status = setup_gop64(si, proto, size, gop_handle);
834 else
835 status = setup_gop32(si, proto, size, gop_handle);
836
837free_handle:
838 efi_call_early(free_pool, gop_handle);
839 return status;
840}
841
842static efi_status_t
843setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
844{
845 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
846 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
847 unsigned long nr_ugas;
848 u32 *handles = (u32 *)uga_handle;;
849 efi_status_t status;
850 int i;
851
334 first_uga = NULL; 852 first_uga = NULL;
853 nr_ugas = size / sizeof(u32);
854 for (i = 0; i < nr_ugas; i++) {
855 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
856 u32 w, h, depth, refresh;
857 void *pciio;
858 u32 handle = handles[i];
859
860 status = efi_call_early(handle_protocol, handle,
861 &uga_proto, (void **)&uga);
862 if (status != EFI_SUCCESS)
863 continue;
864
865 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
866
867 status = efi_early->call((unsigned long)uga->get_mode, uga,
868 &w, &h, &depth, &refresh);
869 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
870 *width = w;
871 *height = h;
872
873 /*
874 * Once we've found a UGA supporting PCIIO,
875 * don't bother looking any further.
876 */
877 if (pciio)
878 break;
335 879
336 nr_ugas = size / sizeof(void *); 880 first_uga = uga;
881 }
882 }
883
884 return status;
885}
886
887static efi_status_t
888setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
889{
890 struct efi_uga_draw_protocol *uga = NULL, *first_uga;
891 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
892 unsigned long nr_ugas;
893 u64 *handles = (u64 *)uga_handle;;
894 efi_status_t status;
895 int i;
896
897 first_uga = NULL;
898 nr_ugas = size / sizeof(u64);
337 for (i = 0; i < nr_ugas; i++) { 899 for (i = 0; i < nr_ugas; i++) {
338 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; 900 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
339 void *handle = uga_handle[i];
340 u32 w, h, depth, refresh; 901 u32 w, h, depth, refresh;
341 void *pciio; 902 void *pciio;
903 u64 handle = handles[i];
342 904
343 status = efi_call_phys3(sys_table->boottime->handle_protocol, 905 status = efi_call_early(handle_protocol, handle,
344 handle, uga_proto, &uga); 906 &uga_proto, (void **)&uga);
345 if (status != EFI_SUCCESS) 907 if (status != EFI_SUCCESS)
346 continue; 908 continue;
347 909
348 efi_call_phys3(sys_table->boottime->handle_protocol, 910 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
349 handle, &pciio_proto, &pciio);
350 911
351 status = efi_call_phys5(uga->get_mode, uga, &w, &h, 912 status = efi_early->call((unsigned long)uga->get_mode, uga,
352 &depth, &refresh); 913 &w, &h, &depth, &refresh);
353 if (status == EFI_SUCCESS && (!first_uga || pciio)) { 914 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
354 width = w; 915 *width = w;
355 height = h; 916 *height = h;
356 917
357 /* 918 /*
358 * Once we've found a UGA supporting PCIIO, 919 * Once we've found a UGA supporting PCIIO,
@@ -365,7 +926,39 @@ static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
365 } 926 }
366 } 927 }
367 928
368 if (!first_uga) 929 return status;
930}
931
932/*
933 * See if we have Universal Graphics Adapter (UGA) protocol
934 */
935static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
936 unsigned long size)
937{
938 efi_status_t status;
939 u32 width, height;
940 void **uga_handle = NULL;
941
942 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
943 size, (void **)&uga_handle);
944 if (status != EFI_SUCCESS)
945 return status;
946
947 status = efi_call_early(locate_handle,
948 EFI_LOCATE_BY_PROTOCOL,
949 uga_proto, NULL, &size, uga_handle);
950 if (status != EFI_SUCCESS)
951 goto free_handle;
952
953 height = 0;
954 width = 0;
955
956 if (efi_early->is64)
957 status = setup_uga64(uga_handle, size, &width, &height);
958 else
959 status = setup_uga32(uga_handle, size, &width, &height);
960
961 if (!width && !height)
369 goto free_handle; 962 goto free_handle;
370 963
371 /* EFI framebuffer */ 964 /* EFI framebuffer */
@@ -384,9 +977,8 @@ static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
384 si->rsvd_size = 8; 977 si->rsvd_size = 8;
385 si->rsvd_pos = 24; 978 si->rsvd_pos = 24;
386 979
387
388free_handle: 980free_handle:
389 efi_call_phys1(sys_table->boottime->free_pool, uga_handle); 981 efi_call_early(free_pool, uga_handle);
390 return status; 982 return status;
391} 983}
392 984
@@ -404,29 +996,28 @@ void setup_graphics(struct boot_params *boot_params)
404 memset(si, 0, sizeof(*si)); 996 memset(si, 0, sizeof(*si));
405 997
406 size = 0; 998 size = 0;
407 status = efi_call_phys5(sys_table->boottime->locate_handle, 999 status = efi_call_early(locate_handle,
408 EFI_LOCATE_BY_PROTOCOL, &graphics_proto, 1000 EFI_LOCATE_BY_PROTOCOL,
409 NULL, &size, gop_handle); 1001 &graphics_proto, NULL, &size, gop_handle);
410 if (status == EFI_BUFFER_TOO_SMALL) 1002 if (status == EFI_BUFFER_TOO_SMALL)
411 status = setup_gop(si, &graphics_proto, size); 1003 status = setup_gop(si, &graphics_proto, size);
412 1004
413 if (status != EFI_SUCCESS) { 1005 if (status != EFI_SUCCESS) {
414 size = 0; 1006 size = 0;
415 status = efi_call_phys5(sys_table->boottime->locate_handle, 1007 status = efi_call_early(locate_handle,
416 EFI_LOCATE_BY_PROTOCOL, &uga_proto, 1008 EFI_LOCATE_BY_PROTOCOL,
417 NULL, &size, uga_handle); 1009 &uga_proto, NULL, &size, uga_handle);
418 if (status == EFI_BUFFER_TOO_SMALL) 1010 if (status == EFI_BUFFER_TOO_SMALL)
419 setup_uga(si, &uga_proto, size); 1011 setup_uga(si, &uga_proto, size);
420 } 1012 }
421} 1013}
422 1014
423
424/* 1015/*
425 * Because the x86 boot code expects to be passed a boot_params we 1016 * Because the x86 boot code expects to be passed a boot_params we
426 * need to create one ourselves (usually the bootloader would create 1017 * need to create one ourselves (usually the bootloader would create
427 * one for us). 1018 * one for us).
428 */ 1019 */
429struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table) 1020struct boot_params *make_boot_params(struct efi_config *c)
430{ 1021{
431 struct boot_params *boot_params; 1022 struct boot_params *boot_params;
432 struct sys_desc_table *sdt; 1023 struct sys_desc_table *sdt;
@@ -434,7 +1025,7 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
434 struct setup_header *hdr; 1025 struct setup_header *hdr;
435 struct efi_info *efi; 1026 struct efi_info *efi;
436 efi_loaded_image_t *image; 1027 efi_loaded_image_t *image;
437 void *options; 1028 void *options, *handle;
438 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; 1029 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
439 int options_size = 0; 1030 int options_size = 0;
440 efi_status_t status; 1031 efi_status_t status;
@@ -445,14 +1036,21 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
445 unsigned long ramdisk_addr; 1036 unsigned long ramdisk_addr;
446 unsigned long ramdisk_size; 1037 unsigned long ramdisk_size;
447 1038
448 sys_table = _table; 1039 efi_early = c;
1040 sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1041 handle = (void *)(unsigned long)efi_early->image_handle;
449 1042
450 /* Check if we were booted by the EFI firmware */ 1043 /* Check if we were booted by the EFI firmware */
451 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 1044 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
452 return NULL; 1045 return NULL;
453 1046
454 status = efi_call_phys3(sys_table->boottime->handle_protocol, 1047 if (efi_early->is64)
455 handle, &proto, (void *)&image); 1048 setup_boot_services64(efi_early);
1049 else
1050 setup_boot_services32(efi_early);
1051
1052 status = efi_call_early(handle_protocol, handle,
1053 &proto, (void *)&image);
456 if (status != EFI_SUCCESS) { 1054 if (status != EFI_SUCCESS) {
457 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); 1055 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
458 return NULL; 1056 return NULL;
@@ -641,14 +1239,13 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
641 sizeof(struct e820entry) * nr_desc; 1239 sizeof(struct e820entry) * nr_desc;
642 1240
643 if (*e820ext) { 1241 if (*e820ext) {
644 efi_call_phys1(sys_table->boottime->free_pool, *e820ext); 1242 efi_call_early(free_pool, *e820ext);
645 *e820ext = NULL; 1243 *e820ext = NULL;
646 *e820ext_size = 0; 1244 *e820ext_size = 0;
647 } 1245 }
648 1246
649 status = efi_call_phys3(sys_table->boottime->allocate_pool, 1247 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
650 EFI_LOADER_DATA, size, e820ext); 1248 size, (void **)e820ext);
651
652 if (status == EFI_SUCCESS) 1249 if (status == EFI_SUCCESS)
653 *e820ext_size = size; 1250 *e820ext_size = size;
654 1251
@@ -656,12 +1253,13 @@ static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
656} 1253}
657 1254
658static efi_status_t exit_boot(struct boot_params *boot_params, 1255static efi_status_t exit_boot(struct boot_params *boot_params,
659 void *handle) 1256 void *handle, bool is64)
660{ 1257{
661 struct efi_info *efi = &boot_params->efi_info; 1258 struct efi_info *efi = &boot_params->efi_info;
662 unsigned long map_sz, key, desc_size; 1259 unsigned long map_sz, key, desc_size;
663 efi_memory_desc_t *mem_map; 1260 efi_memory_desc_t *mem_map;
664 struct setup_data *e820ext; 1261 struct setup_data *e820ext;
1262 const char *signature;
665 __u32 e820ext_size; 1263 __u32 e820ext_size;
666 __u32 nr_desc, prev_nr_desc; 1264 __u32 nr_desc, prev_nr_desc;
667 efi_status_t status; 1265 efi_status_t status;
@@ -691,11 +1289,13 @@ get_map:
691 if (status != EFI_SUCCESS) 1289 if (status != EFI_SUCCESS)
692 goto free_mem_map; 1290 goto free_mem_map;
693 1291
694 efi_call_phys1(sys_table->boottime->free_pool, mem_map); 1292 efi_call_early(free_pool, mem_map);
695 goto get_map; /* Allocated memory, get map again */ 1293 goto get_map; /* Allocated memory, get map again */
696 } 1294 }
697 1295
698 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32)); 1296 signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1297 memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1298
699 efi->efi_systab = (unsigned long)sys_table; 1299 efi->efi_systab = (unsigned long)sys_table;
700 efi->efi_memdesc_size = desc_size; 1300 efi->efi_memdesc_size = desc_size;
701 efi->efi_memdesc_version = desc_version; 1301 efi->efi_memdesc_version = desc_version;
@@ -708,8 +1308,7 @@ get_map:
708#endif 1308#endif
709 1309
710 /* Might as well exit boot services now */ 1310 /* Might as well exit boot services now */
711 status = efi_call_phys2(sys_table->boottime->exit_boot_services, 1311 status = efi_call_early(exit_boot_services, handle, key);
712 handle, key);
713 if (status != EFI_SUCCESS) { 1312 if (status != EFI_SUCCESS) {
714 /* 1313 /*
715 * ExitBootServices() will fail if any of the event 1314 * ExitBootServices() will fail if any of the event
@@ -722,7 +1321,7 @@ get_map:
722 goto free_mem_map; 1321 goto free_mem_map;
723 1322
724 called_exit = true; 1323 called_exit = true;
725 efi_call_phys1(sys_table->boottime->free_pool, mem_map); 1324 efi_call_early(free_pool, mem_map);
726 goto get_map; 1325 goto get_map;
727 } 1326 }
728 1327
@@ -736,23 +1335,31 @@ get_map:
736 return EFI_SUCCESS; 1335 return EFI_SUCCESS;
737 1336
738free_mem_map: 1337free_mem_map:
739 efi_call_phys1(sys_table->boottime->free_pool, mem_map); 1338 efi_call_early(free_pool, mem_map);
740 return status; 1339 return status;
741} 1340}
742 1341
743
744/* 1342/*
745 * On success we return a pointer to a boot_params structure, and NULL 1343 * On success we return a pointer to a boot_params structure, and NULL
746 * on failure. 1344 * on failure.
747 */ 1345 */
748struct boot_params *efi_main(void *handle, efi_system_table_t *_table, 1346struct boot_params *efi_main(struct efi_config *c,
749 struct boot_params *boot_params) 1347 struct boot_params *boot_params)
750{ 1348{
751 struct desc_ptr *gdt; 1349 struct desc_ptr *gdt = NULL;
752 efi_loaded_image_t *image; 1350 efi_loaded_image_t *image;
753 struct setup_header *hdr = &boot_params->hdr; 1351 struct setup_header *hdr = &boot_params->hdr;
754 efi_status_t status; 1352 efi_status_t status;
755 struct desc_struct *desc; 1353 struct desc_struct *desc;
1354 void *handle;
1355 efi_system_table_t *_table;
1356 bool is64;
1357
1358 efi_early = c;
1359
1360 _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1361 handle = (void *)(unsigned long)efi_early->image_handle;
1362 is64 = efi_early->is64;
756 1363
757 sys_table = _table; 1364 sys_table = _table;
758 1365
@@ -760,13 +1367,17 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
760 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 1367 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
761 goto fail; 1368 goto fail;
762 1369
1370 if (is64)
1371 setup_boot_services64(efi_early);
1372 else
1373 setup_boot_services32(efi_early);
1374
763 setup_graphics(boot_params); 1375 setup_graphics(boot_params);
764 1376
765 setup_efi_pci(boot_params); 1377 setup_efi_pci(boot_params);
766 1378
767 status = efi_call_phys3(sys_table->boottime->allocate_pool, 1379 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
768 EFI_LOADER_DATA, sizeof(*gdt), 1380 sizeof(*gdt), (void **)&gdt);
769 (void **)&gdt);
770 if (status != EFI_SUCCESS) { 1381 if (status != EFI_SUCCESS) {
771 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n"); 1382 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
772 goto fail; 1383 goto fail;
@@ -797,7 +1408,7 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
797 hdr->code32_start = bzimage_addr; 1408 hdr->code32_start = bzimage_addr;
798 } 1409 }
799 1410
800 status = exit_boot(boot_params, handle); 1411 status = exit_boot(boot_params, handle, is64);
801 if (status != EFI_SUCCESS) 1412 if (status != EFI_SUCCESS)
802 goto fail; 1413 goto fail;
803 1414
diff --git a/arch/x86/boot/compressed/eboot.h b/arch/x86/boot/compressed/eboot.h
index 81b6b652b46a..c88c31ecad12 100644
--- a/arch/x86/boot/compressed/eboot.h
+++ b/arch/x86/boot/compressed/eboot.h
@@ -37,6 +37,24 @@ struct efi_graphics_output_mode_info {
37 u32 pixels_per_scan_line; 37 u32 pixels_per_scan_line;
38} __packed; 38} __packed;
39 39
40struct efi_graphics_output_protocol_mode_32 {
41 u32 max_mode;
42 u32 mode;
43 u32 info;
44 u32 size_of_info;
45 u64 frame_buffer_base;
46 u32 frame_buffer_size;
47} __packed;
48
49struct efi_graphics_output_protocol_mode_64 {
50 u32 max_mode;
51 u32 mode;
52 u64 info;
53 u64 size_of_info;
54 u64 frame_buffer_base;
55 u64 frame_buffer_size;
56} __packed;
57
40struct efi_graphics_output_protocol_mode { 58struct efi_graphics_output_protocol_mode {
41 u32 max_mode; 59 u32 max_mode;
42 u32 mode; 60 u32 mode;
@@ -46,6 +64,20 @@ struct efi_graphics_output_protocol_mode {
46 unsigned long frame_buffer_size; 64 unsigned long frame_buffer_size;
47} __packed; 65} __packed;
48 66
67struct efi_graphics_output_protocol_32 {
68 u32 query_mode;
69 u32 set_mode;
70 u32 blt;
71 u32 mode;
72};
73
74struct efi_graphics_output_protocol_64 {
75 u64 query_mode;
76 u64 set_mode;
77 u64 blt;
78 u64 mode;
79};
80
49struct efi_graphics_output_protocol { 81struct efi_graphics_output_protocol {
50 void *query_mode; 82 void *query_mode;
51 unsigned long set_mode; 83 unsigned long set_mode;
@@ -53,10 +85,38 @@ struct efi_graphics_output_protocol {
53 struct efi_graphics_output_protocol_mode *mode; 85 struct efi_graphics_output_protocol_mode *mode;
54}; 86};
55 87
88struct efi_uga_draw_protocol_32 {
89 u32 get_mode;
90 u32 set_mode;
91 u32 blt;
92};
93
94struct efi_uga_draw_protocol_64 {
95 u64 get_mode;
96 u64 set_mode;
97 u64 blt;
98};
99
56struct efi_uga_draw_protocol { 100struct efi_uga_draw_protocol {
57 void *get_mode; 101 void *get_mode;
58 void *set_mode; 102 void *set_mode;
59 void *blt; 103 void *blt;
60}; 104};
61 105
106struct efi_config {
107 u64 image_handle;
108 u64 table;
109 u64 allocate_pool;
110 u64 allocate_pages;
111 u64 get_memory_map;
112 u64 free_pool;
113 u64 free_pages;
114 u64 locate_handle;
115 u64 handle_protocol;
116 u64 exit_boot_services;
117 u64 text_output;
118 efi_status_t (*call)(unsigned long, ...);
119 bool is64;
120} __packed;
121
62#endif /* BOOT_COMPRESSED_EBOOT_H */ 122#endif /* BOOT_COMPRESSED_EBOOT_H */
diff --git a/arch/x86/boot/compressed/efi_stub_64.S b/arch/x86/boot/compressed/efi_stub_64.S
index cedc60de86eb..7ff3632806b1 100644
--- a/arch/x86/boot/compressed/efi_stub_64.S
+++ b/arch/x86/boot/compressed/efi_stub_64.S
@@ -1 +1,30 @@
1#include <asm/segment.h>
2#include <asm/msr.h>
3#include <asm/processor-flags.h>
4
1#include "../../platform/efi/efi_stub_64.S" 5#include "../../platform/efi/efi_stub_64.S"
6
7#ifdef CONFIG_EFI_MIXED
8 .code64
9 .text
10ENTRY(efi64_thunk)
11 push %rbp
12 push %rbx
13
14 subq $16, %rsp
15 leaq efi_exit32(%rip), %rax
16 movl %eax, 8(%rsp)
17 leaq efi_gdt64(%rip), %rax
18 movl %eax, 4(%rsp)
19 movl %eax, 2(%rax) /* Fixup the gdt base address */
20 leaq efi32_boot_gdt(%rip), %rax
21 movl %eax, (%rsp)
22
23 call __efi64_thunk
24
25 addq $16, %rsp
26 pop %rbx
27 pop %rbp
28 ret
29ENDPROC(efi64_thunk)
30#endif /* CONFIG_EFI_MIXED */
diff --git a/arch/x86/boot/compressed/head_32.S b/arch/x86/boot/compressed/head_32.S
index 9116aac232c7..de9d4200d305 100644
--- a/arch/x86/boot/compressed/head_32.S
+++ b/arch/x86/boot/compressed/head_32.S
@@ -42,26 +42,53 @@ ENTRY(startup_32)
42ENTRY(efi_pe_entry) 42ENTRY(efi_pe_entry)
43 add $0x4, %esp 43 add $0x4, %esp
44 44
45 call 1f
461: popl %esi
47 subl $1b, %esi
48
49 popl %ecx
50 movl %ecx, efi32_config(%esi) /* Handle */
51 popl %ecx
52 movl %ecx, efi32_config+8(%esi) /* EFI System table pointer */
53
54 /* Relocate efi_config->call() */
55 leal efi32_config(%esi), %eax
56 add %esi, 88(%eax)
57 pushl %eax
58
45 call make_boot_params 59 call make_boot_params
46 cmpl $0, %eax 60 cmpl $0, %eax
47 je 1f 61 je fail
48 movl 0x4(%esp), %esi 62 popl %ecx
49 movl (%esp), %ecx
50 pushl %eax 63 pushl %eax
51 pushl %esi
52 pushl %ecx 64 pushl %ecx
53 sub $0x4, %esp 65 jmp 2f /* Skip efi_config initialization */
54 66
55ENTRY(efi_stub_entry) 67ENTRY(efi32_stub_entry)
56 add $0x4, %esp 68 add $0x4, %esp
69 popl %ecx
70 popl %edx
71
72 call 1f
731: popl %esi
74 subl $1b, %esi
75
76 movl %ecx, efi32_config(%esi) /* Handle */
77 movl %edx, efi32_config+8(%esi) /* EFI System table pointer */
78
79 /* Relocate efi_config->call() */
80 leal efi32_config(%esi), %eax
81 add %esi, 88(%eax)
82 pushl %eax
832:
57 call efi_main 84 call efi_main
58 cmpl $0, %eax 85 cmpl $0, %eax
59 movl %eax, %esi 86 movl %eax, %esi
60 jne 2f 87 jne 2f
611: 88fail:
62 /* EFI init failed, so hang. */ 89 /* EFI init failed, so hang. */
63 hlt 90 hlt
64 jmp 1b 91 jmp fail
652: 922:
66 call 3f 93 call 3f
673: 943:
@@ -202,6 +229,15 @@ relocated:
202 xorl %ebx, %ebx 229 xorl %ebx, %ebx
203 jmp *%eax 230 jmp *%eax
204 231
232#ifdef CONFIG_EFI_STUB
233 .data
234efi32_config:
235 .fill 11,8,0
236 .long efi_call_phys
237 .long 0
238 .byte 0
239#endif
240
205/* 241/*
206 * Stack and heap for uncompression 242 * Stack and heap for uncompression
207 */ 243 */
diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S
index c5c1ae0997e7..57e58a5fa210 100644
--- a/arch/x86/boot/compressed/head_64.S
+++ b/arch/x86/boot/compressed/head_64.S
@@ -113,7 +113,8 @@ ENTRY(startup_32)
113 lgdt gdt(%ebp) 113 lgdt gdt(%ebp)
114 114
115 /* Enable PAE mode */ 115 /* Enable PAE mode */
116 movl $(X86_CR4_PAE), %eax 116 movl %cr4, %eax
117 orl $X86_CR4_PAE, %eax
117 movl %eax, %cr4 118 movl %eax, %cr4
118 119
119 /* 120 /*
@@ -178,6 +179,13 @@ ENTRY(startup_32)
178 */ 179 */
179 pushl $__KERNEL_CS 180 pushl $__KERNEL_CS
180 leal startup_64(%ebp), %eax 181 leal startup_64(%ebp), %eax
182#ifdef CONFIG_EFI_MIXED
183 movl efi32_config(%ebp), %ebx
184 cmp $0, %ebx
185 jz 1f
186 leal handover_entry(%ebp), %eax
1871:
188#endif
181 pushl %eax 189 pushl %eax
182 190
183 /* Enter paged protected Mode, activating Long Mode */ 191 /* Enter paged protected Mode, activating Long Mode */
@@ -188,6 +196,30 @@ ENTRY(startup_32)
188 lret 196 lret
189ENDPROC(startup_32) 197ENDPROC(startup_32)
190 198
199#ifdef CONFIG_EFI_MIXED
200 .org 0x190
201ENTRY(efi32_stub_entry)
202 add $0x4, %esp /* Discard return address */
203 popl %ecx
204 popl %edx
205 popl %esi
206
207 leal (BP_scratch+4)(%esi), %esp
208 call 1f
2091: pop %ebp
210 subl $1b, %ebp
211
212 movl %ecx, efi32_config(%ebp)
213 movl %edx, efi32_config+8(%ebp)
214 sgdtl efi32_boot_gdt(%ebp)
215
216 leal efi32_config(%ebp), %eax
217 movl %eax, efi_config(%ebp)
218
219 jmp startup_32
220ENDPROC(efi32_stub_entry)
221#endif
222
191 .code64 223 .code64
192 .org 0x200 224 .org 0x200
193ENTRY(startup_64) 225ENTRY(startup_64)
@@ -209,26 +241,48 @@ ENTRY(startup_64)
209 jmp preferred_addr 241 jmp preferred_addr
210 242
211ENTRY(efi_pe_entry) 243ENTRY(efi_pe_entry)
212 mov %rcx, %rdi 244 movq %rcx, efi64_config(%rip) /* Handle */
213 mov %rdx, %rsi 245 movq %rdx, efi64_config+8(%rip) /* EFI System table pointer */
214 pushq %rdi 246
215 pushq %rsi 247 leaq efi64_config(%rip), %rax
248 movq %rax, efi_config(%rip)
249
250 call 1f
2511: popq %rbp
252 subq $1b, %rbp
253
254 /*
255 * Relocate efi_config->call().
256 */
257 addq %rbp, efi64_config+88(%rip)
258
259 movq %rax, %rdi
216 call make_boot_params 260 call make_boot_params
217 cmpq $0,%rax 261 cmpq $0,%rax
218 je 1f 262 je fail
219 mov %rax, %rdx 263 mov %rax, %rsi
220 popq %rsi 264 jmp 2f /* Skip the relocation */
221 popq %rdi
222 265
223ENTRY(efi_stub_entry) 266handover_entry:
267 call 1f
2681: popq %rbp
269 subq $1b, %rbp
270
271 /*
272 * Relocate efi_config->call().
273 */
274 movq efi_config(%rip), %rax
275 addq %rbp, 88(%rax)
2762:
277 movq efi_config(%rip), %rdi
224 call efi_main 278 call efi_main
225 movq %rax,%rsi 279 movq %rax,%rsi
226 cmpq $0,%rax 280 cmpq $0,%rax
227 jne 2f 281 jne 2f
2281: 282fail:
229 /* EFI init failed, so hang. */ 283 /* EFI init failed, so hang. */
230 hlt 284 hlt
231 jmp 1b 285 jmp fail
2322: 2862:
233 call 3f 287 call 3f
2343: 2883:
@@ -307,6 +361,20 @@ preferred_addr:
307 leaq relocated(%rbx), %rax 361 leaq relocated(%rbx), %rax
308 jmp *%rax 362 jmp *%rax
309 363
364#ifdef CONFIG_EFI_STUB
365 .org 0x390
366ENTRY(efi64_stub_entry)
367 movq %rdi, efi64_config(%rip) /* Handle */
368 movq %rsi, efi64_config+8(%rip) /* EFI System table pointer */
369
370 leaq efi64_config(%rip), %rax
371 movq %rax, efi_config(%rip)
372
373 movq %rdx, %rsi
374 jmp handover_entry
375ENDPROC(efi64_stub_entry)
376#endif
377
310 .text 378 .text
311relocated: 379relocated:
312 380
@@ -372,6 +440,25 @@ gdt:
372 .quad 0x0000000000000000 /* TS continued */ 440 .quad 0x0000000000000000 /* TS continued */
373gdt_end: 441gdt_end:
374 442
443#ifdef CONFIG_EFI_STUB
444efi_config:
445 .quad 0
446
447#ifdef CONFIG_EFI_MIXED
448 .global efi32_config
449efi32_config:
450 .fill 11,8,0
451 .quad efi64_thunk
452 .byte 0
453#endif
454
455 .global efi64_config
456efi64_config:
457 .fill 11,8,0
458 .quad efi_call6
459 .byte 1
460#endif /* CONFIG_EFI_STUB */
461
375/* 462/*
376 * Stack and heap for uncompression 463 * Stack and heap for uncompression
377 */ 464 */
diff --git a/arch/x86/boot/header.S b/arch/x86/boot/header.S
index 46f5a220b0ad..0ca9a5c362bc 100644
--- a/arch/x86/boot/header.S
+++ b/arch/x86/boot/header.S
@@ -283,7 +283,7 @@ _start:
283 # Part 2 of the header, from the old setup.S 283 # Part 2 of the header, from the old setup.S
284 284
285 .ascii "HdrS" # header signature 285 .ascii "HdrS" # header signature
286 .word 0x020c # header version number (>= 0x0105) 286 .word 0x020d # header version number (>= 0x0105)
287 # or else old loadlin-1.5 will fail) 287 # or else old loadlin-1.5 will fail)
288 .globl realmode_swtch 288 .globl realmode_swtch
289realmode_swtch: .word 0, 0 # default_switch, SETUPSEG 289realmode_swtch: .word 0, 0 # default_switch, SETUPSEG
@@ -375,7 +375,8 @@ xloadflags:
375# define XLF0 0 375# define XLF0 0
376#endif 376#endif
377 377
378#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) 378#if defined(CONFIG_RELOCATABLE) && defined(CONFIG_X86_64) && \
379 !defined(CONFIG_EFI_MIXED)
379 /* kernel/boot_param/ramdisk could be loaded above 4g */ 380 /* kernel/boot_param/ramdisk could be loaded above 4g */
380# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G 381# define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G
381#else 382#else
@@ -383,10 +384,14 @@ xloadflags:
383#endif 384#endif
384 385
385#ifdef CONFIG_EFI_STUB 386#ifdef CONFIG_EFI_STUB
386# ifdef CONFIG_X86_64 387# ifdef CONFIG_EFI_MIXED
387# define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */ 388# define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_HANDOVER_64)
388# else 389# else
389# define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */ 390# ifdef CONFIG_X86_64
391# define XLF23 XLF_EFI_HANDOVER_64 /* 64-bit EFI handover ok */
392# else
393# define XLF23 XLF_EFI_HANDOVER_32 /* 32-bit EFI handover ok */
394# endif
390# endif 395# endif
391#else 396#else
392# define XLF23 0 397# define XLF23 0
@@ -426,13 +431,7 @@ pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr
426#define INIT_SIZE VO_INIT_SIZE 431#define INIT_SIZE VO_INIT_SIZE
427#endif 432#endif
428init_size: .long INIT_SIZE # kernel initialization size 433init_size: .long INIT_SIZE # kernel initialization size
429handover_offset: 434handover_offset: .long 0 # Filled in by build.c
430#ifdef CONFIG_EFI_STUB
431 .long 0x30 # offset to the handover
432 # protocol entry point
433#else
434 .long 0
435#endif
436 435
437# End of setup header ##################################################### 436# End of setup header #####################################################
438 437
diff --git a/arch/x86/boot/tools/build.c b/arch/x86/boot/tools/build.c
index 8e15b22391fc..1a2f2121cada 100644
--- a/arch/x86/boot/tools/build.c
+++ b/arch/x86/boot/tools/build.c
@@ -53,7 +53,8 @@ int is_big_kernel;
53 53
54#define PECOFF_RELOC_RESERVE 0x20 54#define PECOFF_RELOC_RESERVE 0x20
55 55
56unsigned long efi_stub_entry; 56unsigned long efi32_stub_entry;
57unsigned long efi64_stub_entry;
57unsigned long efi_pe_entry; 58unsigned long efi_pe_entry;
58unsigned long startup_64; 59unsigned long startup_64;
59 60
@@ -219,6 +220,52 @@ static void update_pecoff_text(unsigned int text_start, unsigned int file_sz)
219 update_pecoff_section_header(".text", text_start, text_sz); 220 update_pecoff_section_header(".text", text_start, text_sz);
220} 221}
221 222
223static int reserve_pecoff_reloc_section(int c)
224{
225 /* Reserve 0x20 bytes for .reloc section */
226 memset(buf+c, 0, PECOFF_RELOC_RESERVE);
227 return PECOFF_RELOC_RESERVE;
228}
229
230static void efi_stub_defaults(void)
231{
232 /* Defaults for old kernel */
233#ifdef CONFIG_X86_32
234 efi_pe_entry = 0x10;
235#else
236 efi_pe_entry = 0x210;
237 startup_64 = 0x200;
238#endif
239}
240
241static void efi_stub_entry_update(void)
242{
243 unsigned long addr = efi32_stub_entry;
244
245#ifdef CONFIG_X86_64
246 /* Yes, this is really how we defined it :( */
247 addr = efi64_stub_entry - 0x200;
248#endif
249
250#ifdef CONFIG_EFI_MIXED
251 if (efi32_stub_entry != addr)
252 die("32-bit and 64-bit EFI entry points do not match\n");
253#endif
254 put_unaligned_le32(addr, &buf[0x264]);
255}
256
257#else
258
259static inline void update_pecoff_setup_and_reloc(unsigned int size) {}
260static inline void update_pecoff_text(unsigned int text_start,
261 unsigned int file_sz) {}
262static inline void efi_stub_defaults(void) {}
263static inline void efi_stub_entry_update(void) {}
264
265static inline int reserve_pecoff_reloc_section(int c)
266{
267 return 0;
268}
222#endif /* CONFIG_EFI_STUB */ 269#endif /* CONFIG_EFI_STUB */
223 270
224 271
@@ -250,7 +297,8 @@ static void parse_zoffset(char *fname)
250 p = (char *)buf; 297 p = (char *)buf;
251 298
252 while (p && *p) { 299 while (p && *p) {
253 PARSE_ZOFS(p, efi_stub_entry); 300 PARSE_ZOFS(p, efi32_stub_entry);
301 PARSE_ZOFS(p, efi64_stub_entry);
254 PARSE_ZOFS(p, efi_pe_entry); 302 PARSE_ZOFS(p, efi_pe_entry);
255 PARSE_ZOFS(p, startup_64); 303 PARSE_ZOFS(p, startup_64);
256 304
@@ -271,15 +319,7 @@ int main(int argc, char ** argv)
271 void *kernel; 319 void *kernel;
272 u32 crc = 0xffffffffUL; 320 u32 crc = 0xffffffffUL;
273 321
274 /* Defaults for old kernel */ 322 efi_stub_defaults();
275#ifdef CONFIG_X86_32
276 efi_pe_entry = 0x10;
277 efi_stub_entry = 0x30;
278#else
279 efi_pe_entry = 0x210;
280 efi_stub_entry = 0x230;
281 startup_64 = 0x200;
282#endif
283 323
284 if (argc != 5) 324 if (argc != 5)
285 usage(); 325 usage();
@@ -302,11 +342,7 @@ int main(int argc, char ** argv)
302 die("Boot block hasn't got boot flag (0xAA55)"); 342 die("Boot block hasn't got boot flag (0xAA55)");
303 fclose(file); 343 fclose(file);
304 344
305#ifdef CONFIG_EFI_STUB 345 c += reserve_pecoff_reloc_section(c);
306 /* Reserve 0x20 bytes for .reloc section */
307 memset(buf+c, 0, PECOFF_RELOC_RESERVE);
308 c += PECOFF_RELOC_RESERVE;
309#endif
310 346
311 /* Pad unused space with zeros */ 347 /* Pad unused space with zeros */
312 setup_sectors = (c + 511) / 512; 348 setup_sectors = (c + 511) / 512;
@@ -315,9 +351,7 @@ int main(int argc, char ** argv)
315 i = setup_sectors*512; 351 i = setup_sectors*512;
316 memset(buf+c, 0, i-c); 352 memset(buf+c, 0, i-c);
317 353
318#ifdef CONFIG_EFI_STUB
319 update_pecoff_setup_and_reloc(i); 354 update_pecoff_setup_and_reloc(i);
320#endif
321 355
322 /* Set the default root device */ 356 /* Set the default root device */
323 put_unaligned_le16(DEFAULT_ROOT_DEV, &buf[508]); 357 put_unaligned_le16(DEFAULT_ROOT_DEV, &buf[508]);
@@ -342,14 +376,9 @@ int main(int argc, char ** argv)
342 buf[0x1f1] = setup_sectors-1; 376 buf[0x1f1] = setup_sectors-1;
343 put_unaligned_le32(sys_size, &buf[0x1f4]); 377 put_unaligned_le32(sys_size, &buf[0x1f4]);
344 378
345#ifdef CONFIG_EFI_STUB
346 update_pecoff_text(setup_sectors * 512, sz + i + ((sys_size * 16) - sz)); 379 update_pecoff_text(setup_sectors * 512, sz + i + ((sys_size * 16) - sz));
347 380
348#ifdef CONFIG_X86_64 /* Yes, this is really how we defined it :( */ 381 efi_stub_entry_update();
349 efi_stub_entry -= 0x200;
350#endif
351 put_unaligned_le32(efi_stub_entry, &buf[0x264]);
352#endif
353 382
354 crc = partial_crc32(buf, i, crc); 383 crc = partial_crc32(buf, i, crc);
355 if (fwrite(buf, 1, i, dest) != i) 384 if (fwrite(buf, 1, i, dest) != i)
diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h
index acd86c850414..0869434eaf72 100644
--- a/arch/x86/include/asm/efi.h
+++ b/arch/x86/include/asm/efi.h
@@ -19,9 +19,11 @@
19 */ 19 */
20#define EFI_OLD_MEMMAP EFI_ARCH_1 20#define EFI_OLD_MEMMAP EFI_ARCH_1
21 21
22#define EFI32_LOADER_SIGNATURE "EL32"
23#define EFI64_LOADER_SIGNATURE "EL64"
24
22#ifdef CONFIG_X86_32 25#ifdef CONFIG_X86_32
23 26
24#define EFI_LOADER_SIGNATURE "EL32"
25 27
26extern unsigned long asmlinkage efi_call_phys(void *, ...); 28extern unsigned long asmlinkage efi_call_phys(void *, ...);
27 29
@@ -57,8 +59,6 @@ extern unsigned long asmlinkage efi_call_phys(void *, ...);
57 59
58#else /* !CONFIG_X86_32 */ 60#else /* !CONFIG_X86_32 */
59 61
60#define EFI_LOADER_SIGNATURE "EL64"
61
62extern u64 efi_call0(void *fp); 62extern u64 efi_call0(void *fp);
63extern u64 efi_call1(void *fp, u64 arg1); 63extern u64 efi_call1(void *fp, u64 arg1);
64extern u64 efi_call2(void *fp, u64 arg1, u64 arg2); 64extern u64 efi_call2(void *fp, u64 arg1, u64 arg2);
@@ -119,7 +119,6 @@ extern void __iomem *efi_ioremap(unsigned long addr, unsigned long size,
119#endif /* CONFIG_X86_32 */ 119#endif /* CONFIG_X86_32 */
120 120
121extern int add_efi_memmap; 121extern int add_efi_memmap;
122extern unsigned long x86_efi_facility;
123extern struct efi_scratch efi_scratch; 122extern struct efi_scratch efi_scratch;
124extern void efi_set_executable(efi_memory_desc_t *md, bool executable); 123extern void efi_set_executable(efi_memory_desc_t *md, bool executable);
125extern int efi_memblock_x86_reserve_range(void); 124extern int efi_memblock_x86_reserve_range(void);
@@ -130,10 +129,12 @@ extern void efi_memory_uc(u64 addr, unsigned long size);
130extern void __init efi_map_region(efi_memory_desc_t *md); 129extern void __init efi_map_region(efi_memory_desc_t *md);
131extern void __init efi_map_region_fixed(efi_memory_desc_t *md); 130extern void __init efi_map_region_fixed(efi_memory_desc_t *md);
132extern void efi_sync_low_kernel_mappings(void); 131extern void efi_sync_low_kernel_mappings(void);
133extern void efi_setup_page_tables(void); 132extern int efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages);
133extern void efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages);
134extern void __init old_map_region(efi_memory_desc_t *md); 134extern void __init old_map_region(efi_memory_desc_t *md);
135extern void __init runtime_code_page_mkexec(void); 135extern void __init runtime_code_page_mkexec(void);
136extern void __init efi_runtime_mkexec(void); 136extern void __init efi_runtime_mkexec(void);
137extern void __init efi_dump_pagetable(void);
137extern void __init efi_apply_memmap_quirks(void); 138extern void __init efi_apply_memmap_quirks(void);
138 139
139struct efi_setup_data { 140struct efi_setup_data {
@@ -153,8 +154,40 @@ static inline bool efi_is_native(void)
153 return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT); 154 return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
154} 155}
155 156
157static inline bool efi_runtime_supported(void)
158{
159 if (efi_is_native())
160 return true;
161
162 if (IS_ENABLED(CONFIG_EFI_MIXED) && !efi_enabled(EFI_OLD_MEMMAP))
163 return true;
164
165 return false;
166}
167
156extern struct console early_efi_console; 168extern struct console early_efi_console;
157extern void parse_efi_setup(u64 phys_addr, u32 data_len); 169extern void parse_efi_setup(u64 phys_addr, u32 data_len);
170
171#ifdef CONFIG_EFI_MIXED
172extern void efi_thunk_runtime_setup(void);
173extern efi_status_t efi_thunk_set_virtual_address_map(
174 void *phys_set_virtual_address_map,
175 unsigned long memory_map_size,
176 unsigned long descriptor_size,
177 u32 descriptor_version,
178 efi_memory_desc_t *virtual_map);
179#else
180static inline void efi_thunk_runtime_setup(void) {}
181static inline efi_status_t efi_thunk_set_virtual_address_map(
182 void *phys_set_virtual_address_map,
183 unsigned long memory_map_size,
184 unsigned long descriptor_size,
185 u32 descriptor_version,
186 efi_memory_desc_t *virtual_map)
187{
188 return EFI_SUCCESS;
189}
190#endif /* CONFIG_EFI_MIXED */
158#else 191#else
159/* 192/*
160 * IF EFI is not configured, have the EFI calls return -ENOSYS. 193 * IF EFI is not configured, have the EFI calls return -ENOSYS.
diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index bbc8b12fa443..b459ddf27d64 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -15,9 +15,10 @@
15 : (prot)) 15 : (prot))
16 16
17#ifndef __ASSEMBLY__ 17#ifndef __ASSEMBLY__
18
19#include <asm/x86_init.h> 18#include <asm/x86_init.h>
20 19
20void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd);
21
21/* 22/*
22 * ZERO_PAGE is a global shared page that is always zero: used 23 * ZERO_PAGE is a global shared page that is always zero: used
23 * for zero-mapped memory areas etc.. 24 * for zero-mapped memory areas etc..
diff --git a/arch/x86/include/asm/pgtable_types.h b/arch/x86/include/asm/pgtable_types.h
index 1aa9ccd43223..708f19fb4fc7 100644
--- a/arch/x86/include/asm/pgtable_types.h
+++ b/arch/x86/include/asm/pgtable_types.h
@@ -382,9 +382,13 @@ static inline void update_page_count(int level, unsigned long pages) { }
382 * as a pte too. 382 * as a pte too.
383 */ 383 */
384extern pte_t *lookup_address(unsigned long address, unsigned int *level); 384extern pte_t *lookup_address(unsigned long address, unsigned int *level);
385extern pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
386 unsigned int *level);
385extern phys_addr_t slow_virt_to_phys(void *__address); 387extern phys_addr_t slow_virt_to_phys(void *__address);
386extern int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address, 388extern int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
387 unsigned numpages, unsigned long page_flags); 389 unsigned numpages, unsigned long page_flags);
390void kernel_unmap_pages_in_pgd(pgd_t *root, unsigned long address,
391 unsigned numpages);
388#endif /* !__ASSEMBLY__ */ 392#endif /* !__ASSEMBLY__ */
389 393
390#endif /* _ASM_X86_PGTABLE_DEFS_H */ 394#endif /* _ASM_X86_PGTABLE_DEFS_H */
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index ce72964b2f46..fa511acff7e6 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -926,11 +926,11 @@ void __init setup_arch(char **cmdline_p)
926#ifdef CONFIG_EFI 926#ifdef CONFIG_EFI
927 if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature, 927 if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
928 "EL32", 4)) { 928 "EL32", 4)) {
929 set_bit(EFI_BOOT, &x86_efi_facility); 929 set_bit(EFI_BOOT, &efi.flags);
930 } else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature, 930 } else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
931 "EL64", 4)) { 931 "EL64", 4)) {
932 set_bit(EFI_BOOT, &x86_efi_facility); 932 set_bit(EFI_BOOT, &efi.flags);
933 set_bit(EFI_64BIT, &x86_efi_facility); 933 set_bit(EFI_64BIT, &efi.flags);
934 } 934 }
935 935
936 if (efi_enabled(EFI_BOOT)) 936 if (efi_enabled(EFI_BOOT))
diff --git a/arch/x86/mm/dump_pagetables.c b/arch/x86/mm/dump_pagetables.c
index 0002a3a33081..20621d753d5f 100644
--- a/arch/x86/mm/dump_pagetables.c
+++ b/arch/x86/mm/dump_pagetables.c
@@ -30,6 +30,7 @@ struct pg_state {
30 unsigned long start_address; 30 unsigned long start_address;
31 unsigned long current_address; 31 unsigned long current_address;
32 const struct addr_marker *marker; 32 const struct addr_marker *marker;
33 bool to_dmesg;
33}; 34};
34 35
35struct addr_marker { 36struct addr_marker {
@@ -88,10 +89,28 @@ static struct addr_marker address_markers[] = {
88#define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT) 89#define PUD_LEVEL_MULT (PTRS_PER_PMD * PMD_LEVEL_MULT)
89#define PGD_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT) 90#define PGD_LEVEL_MULT (PTRS_PER_PUD * PUD_LEVEL_MULT)
90 91
92#define pt_dump_seq_printf(m, to_dmesg, fmt, args...) \
93({ \
94 if (to_dmesg) \
95 printk(KERN_INFO fmt, ##args); \
96 else \
97 if (m) \
98 seq_printf(m, fmt, ##args); \
99})
100
101#define pt_dump_cont_printf(m, to_dmesg, fmt, args...) \
102({ \
103 if (to_dmesg) \
104 printk(KERN_CONT fmt, ##args); \
105 else \
106 if (m) \
107 seq_printf(m, fmt, ##args); \
108})
109
91/* 110/*
92 * Print a readable form of a pgprot_t to the seq_file 111 * Print a readable form of a pgprot_t to the seq_file
93 */ 112 */
94static void printk_prot(struct seq_file *m, pgprot_t prot, int level) 113static void printk_prot(struct seq_file *m, pgprot_t prot, int level, bool dmsg)
95{ 114{
96 pgprotval_t pr = pgprot_val(prot); 115 pgprotval_t pr = pgprot_val(prot);
97 static const char * const level_name[] = 116 static const char * const level_name[] =
@@ -99,47 +118,47 @@ static void printk_prot(struct seq_file *m, pgprot_t prot, int level)
99 118
100 if (!pgprot_val(prot)) { 119 if (!pgprot_val(prot)) {
101 /* Not present */ 120 /* Not present */
102 seq_printf(m, " "); 121 pt_dump_cont_printf(m, dmsg, " ");
103 } else { 122 } else {
104 if (pr & _PAGE_USER) 123 if (pr & _PAGE_USER)
105 seq_printf(m, "USR "); 124 pt_dump_cont_printf(m, dmsg, "USR ");
106 else 125 else
107 seq_printf(m, " "); 126 pt_dump_cont_printf(m, dmsg, " ");
108 if (pr & _PAGE_RW) 127 if (pr & _PAGE_RW)
109 seq_printf(m, "RW "); 128 pt_dump_cont_printf(m, dmsg, "RW ");
110 else 129 else
111 seq_printf(m, "ro "); 130 pt_dump_cont_printf(m, dmsg, "ro ");
112 if (pr & _PAGE_PWT) 131 if (pr & _PAGE_PWT)
113 seq_printf(m, "PWT "); 132 pt_dump_cont_printf(m, dmsg, "PWT ");
114 else 133 else
115 seq_printf(m, " "); 134 pt_dump_cont_printf(m, dmsg, " ");
116 if (pr & _PAGE_PCD) 135 if (pr & _PAGE_PCD)
117 seq_printf(m, "PCD "); 136 pt_dump_cont_printf(m, dmsg, "PCD ");
118 else 137 else
119 seq_printf(m, " "); 138 pt_dump_cont_printf(m, dmsg, " ");
120 139
121 /* Bit 9 has a different meaning on level 3 vs 4 */ 140 /* Bit 9 has a different meaning on level 3 vs 4 */
122 if (level <= 3) { 141 if (level <= 3) {
123 if (pr & _PAGE_PSE) 142 if (pr & _PAGE_PSE)
124 seq_printf(m, "PSE "); 143 pt_dump_cont_printf(m, dmsg, "PSE ");
125 else 144 else
126 seq_printf(m, " "); 145 pt_dump_cont_printf(m, dmsg, " ");
127 } else { 146 } else {
128 if (pr & _PAGE_PAT) 147 if (pr & _PAGE_PAT)
129 seq_printf(m, "pat "); 148 pt_dump_cont_printf(m, dmsg, "pat ");
130 else 149 else
131 seq_printf(m, " "); 150 pt_dump_cont_printf(m, dmsg, " ");
132 } 151 }
133 if (pr & _PAGE_GLOBAL) 152 if (pr & _PAGE_GLOBAL)
134 seq_printf(m, "GLB "); 153 pt_dump_cont_printf(m, dmsg, "GLB ");
135 else 154 else
136 seq_printf(m, " "); 155 pt_dump_cont_printf(m, dmsg, " ");
137 if (pr & _PAGE_NX) 156 if (pr & _PAGE_NX)
138 seq_printf(m, "NX "); 157 pt_dump_cont_printf(m, dmsg, "NX ");
139 else 158 else
140 seq_printf(m, "x "); 159 pt_dump_cont_printf(m, dmsg, "x ");
141 } 160 }
142 seq_printf(m, "%s\n", level_name[level]); 161 pt_dump_cont_printf(m, dmsg, "%s\n", level_name[level]);
143} 162}
144 163
145/* 164/*
@@ -178,7 +197,8 @@ static void note_page(struct seq_file *m, struct pg_state *st,
178 st->current_prot = new_prot; 197 st->current_prot = new_prot;
179 st->level = level; 198 st->level = level;
180 st->marker = address_markers; 199 st->marker = address_markers;
181 seq_printf(m, "---[ %s ]---\n", st->marker->name); 200 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
201 st->marker->name);
182 } else if (prot != cur || level != st->level || 202 } else if (prot != cur || level != st->level ||
183 st->current_address >= st->marker[1].start_address) { 203 st->current_address >= st->marker[1].start_address) {
184 const char *unit = units; 204 const char *unit = units;
@@ -188,17 +208,17 @@ static void note_page(struct seq_file *m, struct pg_state *st,
188 /* 208 /*
189 * Now print the actual finished series 209 * Now print the actual finished series
190 */ 210 */
191 seq_printf(m, "0x%0*lx-0x%0*lx ", 211 pt_dump_seq_printf(m, st->to_dmesg, "0x%0*lx-0x%0*lx ",
192 width, st->start_address, 212 width, st->start_address,
193 width, st->current_address); 213 width, st->current_address);
194 214
195 delta = (st->current_address - st->start_address) >> 10; 215 delta = (st->current_address - st->start_address) >> 10;
196 while (!(delta & 1023) && unit[1]) { 216 while (!(delta & 1023) && unit[1]) {
197 delta >>= 10; 217 delta >>= 10;
198 unit++; 218 unit++;
199 } 219 }
200 seq_printf(m, "%9lu%c ", delta, *unit); 220 pt_dump_cont_printf(m, st->to_dmesg, "%9lu%c ", delta, *unit);
201 printk_prot(m, st->current_prot, st->level); 221 printk_prot(m, st->current_prot, st->level, st->to_dmesg);
202 222
203 /* 223 /*
204 * We print markers for special areas of address space, 224 * We print markers for special areas of address space,
@@ -207,7 +227,8 @@ static void note_page(struct seq_file *m, struct pg_state *st,
207 */ 227 */
208 if (st->current_address >= st->marker[1].start_address) { 228 if (st->current_address >= st->marker[1].start_address) {
209 st->marker++; 229 st->marker++;
210 seq_printf(m, "---[ %s ]---\n", st->marker->name); 230 pt_dump_seq_printf(m, st->to_dmesg, "---[ %s ]---\n",
231 st->marker->name);
211 } 232 }
212 233
213 st->start_address = st->current_address; 234 st->start_address = st->current_address;
@@ -296,7 +317,7 @@ static void walk_pud_level(struct seq_file *m, struct pg_state *st, pgd_t addr,
296#define pgd_none(a) pud_none(__pud(pgd_val(a))) 317#define pgd_none(a) pud_none(__pud(pgd_val(a)))
297#endif 318#endif
298 319
299static void walk_pgd_level(struct seq_file *m) 320void ptdump_walk_pgd_level(struct seq_file *m, pgd_t *pgd)
300{ 321{
301#ifdef CONFIG_X86_64 322#ifdef CONFIG_X86_64
302 pgd_t *start = (pgd_t *) &init_level4_pgt; 323 pgd_t *start = (pgd_t *) &init_level4_pgt;
@@ -304,9 +325,12 @@ static void walk_pgd_level(struct seq_file *m)
304 pgd_t *start = swapper_pg_dir; 325 pgd_t *start = swapper_pg_dir;
305#endif 326#endif
306 int i; 327 int i;
307 struct pg_state st; 328 struct pg_state st = {};
308 329
309 memset(&st, 0, sizeof(st)); 330 if (pgd) {
331 start = pgd;
332 st.to_dmesg = true;
333 }
310 334
311 for (i = 0; i < PTRS_PER_PGD; i++) { 335 for (i = 0; i < PTRS_PER_PGD; i++) {
312 st.current_address = normalize_addr(i * PGD_LEVEL_MULT); 336 st.current_address = normalize_addr(i * PGD_LEVEL_MULT);
@@ -331,7 +355,7 @@ static void walk_pgd_level(struct seq_file *m)
331 355
332static int ptdump_show(struct seq_file *m, void *v) 356static int ptdump_show(struct seq_file *m, void *v)
333{ 357{
334 walk_pgd_level(m); 358 ptdump_walk_pgd_level(m, NULL);
335 return 0; 359 return 0;
336} 360}
337 361
diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
index a10c8c792161..8e5722992677 100644
--- a/arch/x86/mm/fault.c
+++ b/arch/x86/mm/fault.c
@@ -584,8 +584,13 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code,
584 584
585 if (error_code & PF_INSTR) { 585 if (error_code & PF_INSTR) {
586 unsigned int level; 586 unsigned int level;
587 pgd_t *pgd;
588 pte_t *pte;
587 589
588 pte_t *pte = lookup_address(address, &level); 590 pgd = __va(read_cr3() & PHYSICAL_PAGE_MASK);
591 pgd += pgd_index(address);
592
593 pte = lookup_address_in_pgd(pgd, address, &level);
589 594
590 if (pte && pte_present(*pte) && !pte_exec(*pte)) 595 if (pte && pte_present(*pte) && !pte_exec(*pte))
591 printk(nx_warning, from_kuid(&init_user_ns, current_uid())); 596 printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
index cf125b301b69..ae242a7c11c7 100644
--- a/arch/x86/mm/pageattr.c
+++ b/arch/x86/mm/pageattr.c
@@ -323,8 +323,12 @@ static inline pgprot_t static_protections(pgprot_t prot, unsigned long address,
323 return prot; 323 return prot;
324} 324}
325 325
326static pte_t *__lookup_address_in_pgd(pgd_t *pgd, unsigned long address, 326/*
327 unsigned int *level) 327 * Lookup the page table entry for a virtual address in a specific pgd.
328 * Return a pointer to the entry and the level of the mapping.
329 */
330pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
331 unsigned int *level)
328{ 332{
329 pud_t *pud; 333 pud_t *pud;
330 pmd_t *pmd; 334 pmd_t *pmd;
@@ -365,7 +369,7 @@ static pte_t *__lookup_address_in_pgd(pgd_t *pgd, unsigned long address,
365 */ 369 */
366pte_t *lookup_address(unsigned long address, unsigned int *level) 370pte_t *lookup_address(unsigned long address, unsigned int *level)
367{ 371{
368 return __lookup_address_in_pgd(pgd_offset_k(address), address, level); 372 return lookup_address_in_pgd(pgd_offset_k(address), address, level);
369} 373}
370EXPORT_SYMBOL_GPL(lookup_address); 374EXPORT_SYMBOL_GPL(lookup_address);
371 375
@@ -373,7 +377,7 @@ static pte_t *_lookup_address_cpa(struct cpa_data *cpa, unsigned long address,
373 unsigned int *level) 377 unsigned int *level)
374{ 378{
375 if (cpa->pgd) 379 if (cpa->pgd)
376 return __lookup_address_in_pgd(cpa->pgd + pgd_index(address), 380 return lookup_address_in_pgd(cpa->pgd + pgd_index(address),
377 address, level); 381 address, level);
378 382
379 return lookup_address(address, level); 383 return lookup_address(address, level);
@@ -692,6 +696,18 @@ static bool try_to_free_pmd_page(pmd_t *pmd)
692 return true; 696 return true;
693} 697}
694 698
699static bool try_to_free_pud_page(pud_t *pud)
700{
701 int i;
702
703 for (i = 0; i < PTRS_PER_PUD; i++)
704 if (!pud_none(pud[i]))
705 return false;
706
707 free_page((unsigned long)pud);
708 return true;
709}
710
695static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end) 711static bool unmap_pte_range(pmd_t *pmd, unsigned long start, unsigned long end)
696{ 712{
697 pte_t *pte = pte_offset_kernel(pmd, start); 713 pte_t *pte = pte_offset_kernel(pmd, start);
@@ -805,6 +821,16 @@ static void unmap_pud_range(pgd_t *pgd, unsigned long start, unsigned long end)
805 */ 821 */
806} 822}
807 823
824static void unmap_pgd_range(pgd_t *root, unsigned long addr, unsigned long end)
825{
826 pgd_t *pgd_entry = root + pgd_index(addr);
827
828 unmap_pud_range(pgd_entry, addr, end);
829
830 if (try_to_free_pud_page((pud_t *)pgd_page_vaddr(*pgd_entry)))
831 pgd_clear(pgd_entry);
832}
833
808static int alloc_pte_page(pmd_t *pmd) 834static int alloc_pte_page(pmd_t *pmd)
809{ 835{
810 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK); 836 pte_t *pte = (pte_t *)get_zeroed_page(GFP_KERNEL | __GFP_NOTRACK);
@@ -999,9 +1025,8 @@ static int populate_pud(struct cpa_data *cpa, unsigned long start, pgd_t *pgd,
999static int populate_pgd(struct cpa_data *cpa, unsigned long addr) 1025static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1000{ 1026{
1001 pgprot_t pgprot = __pgprot(_KERNPG_TABLE); 1027 pgprot_t pgprot = __pgprot(_KERNPG_TABLE);
1002 bool allocd_pgd = false;
1003 pgd_t *pgd_entry;
1004 pud_t *pud = NULL; /* shut up gcc */ 1028 pud_t *pud = NULL; /* shut up gcc */
1029 pgd_t *pgd_entry;
1005 int ret; 1030 int ret;
1006 1031
1007 pgd_entry = cpa->pgd + pgd_index(addr); 1032 pgd_entry = cpa->pgd + pgd_index(addr);
@@ -1015,7 +1040,6 @@ static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1015 return -1; 1040 return -1;
1016 1041
1017 set_pgd(pgd_entry, __pgd(__pa(pud) | _KERNPG_TABLE)); 1042 set_pgd(pgd_entry, __pgd(__pa(pud) | _KERNPG_TABLE));
1018 allocd_pgd = true;
1019 } 1043 }
1020 1044
1021 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr); 1045 pgprot_val(pgprot) &= ~pgprot_val(cpa->mask_clr);
@@ -1023,19 +1047,11 @@ static int populate_pgd(struct cpa_data *cpa, unsigned long addr)
1023 1047
1024 ret = populate_pud(cpa, addr, pgd_entry, pgprot); 1048 ret = populate_pud(cpa, addr, pgd_entry, pgprot);
1025 if (ret < 0) { 1049 if (ret < 0) {
1026 unmap_pud_range(pgd_entry, addr, 1050 unmap_pgd_range(cpa->pgd, addr,
1027 addr + (cpa->numpages << PAGE_SHIFT)); 1051 addr + (cpa->numpages << PAGE_SHIFT));
1028
1029 if (allocd_pgd) {
1030 /*
1031 * If I allocated this PUD page, I can just as well
1032 * free it in this error path.
1033 */
1034 pgd_clear(pgd_entry);
1035 free_page((unsigned long)pud);
1036 }
1037 return ret; 1052 return ret;
1038 } 1053 }
1054
1039 cpa->numpages = ret; 1055 cpa->numpages = ret;
1040 return 0; 1056 return 0;
1041} 1057}
@@ -1861,6 +1877,12 @@ out:
1861 return retval; 1877 return retval;
1862} 1878}
1863 1879
1880void kernel_unmap_pages_in_pgd(pgd_t *root, unsigned long address,
1881 unsigned numpages)
1882{
1883 unmap_pgd_range(root, address, address + (numpages << PAGE_SHIFT));
1884}
1885
1864/* 1886/*
1865 * The testcases use internal knowledge of the implementation that shouldn't 1887 * The testcases use internal knowledge of the implementation that shouldn't
1866 * be exposed to the rest of the kernel. Include these directly here. 1888 * be exposed to the rest of the kernel. Include these directly here.
diff --git a/arch/x86/platform/efi/Makefile b/arch/x86/platform/efi/Makefile
index b7b0b35c1981..d51045afcaaf 100644
--- a/arch/x86/platform/efi/Makefile
+++ b/arch/x86/platform/efi/Makefile
@@ -1,3 +1,4 @@
1obj-$(CONFIG_EFI) += efi.o efi_$(BITS).o efi_stub_$(BITS).o 1obj-$(CONFIG_EFI) += efi.o efi_$(BITS).o efi_stub_$(BITS).o
2obj-$(CONFIG_ACPI_BGRT) += efi-bgrt.o 2obj-$(CONFIG_ACPI_BGRT) += efi-bgrt.o
3obj-$(CONFIG_EARLY_PRINTK_EFI) += early_printk.o 3obj-$(CONFIG_EARLY_PRINTK_EFI) += early_printk.o
4obj-$(CONFIG_EFI_MIXED) += efi_thunk_$(BITS).o
diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
index b97acecf3fd9..3781dd39e8bd 100644
--- a/arch/x86/platform/efi/efi.c
+++ b/arch/x86/platform/efi/efi.c
@@ -68,9 +68,7 @@ struct efi_memory_map memmap;
68static struct efi efi_phys __initdata; 68static struct efi efi_phys __initdata;
69static efi_system_table_t efi_systab __initdata; 69static efi_system_table_t efi_systab __initdata;
70 70
71unsigned long x86_efi_facility; 71static efi_config_table_type_t arch_tables[] __initdata = {
72
73static __initdata efi_config_table_type_t arch_tables[] = {
74#ifdef CONFIG_X86_UV 72#ifdef CONFIG_X86_UV
75 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab}, 73 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
76#endif 74#endif
@@ -79,16 +77,7 @@ static __initdata efi_config_table_type_t arch_tables[] = {
79 77
80u64 efi_setup; /* efi setup_data physical address */ 78u64 efi_setup; /* efi setup_data physical address */
81 79
82/* 80static bool disable_runtime __initdata = false;
83 * Returns 1 if 'facility' is enabled, 0 otherwise.
84 */
85int efi_enabled(int facility)
86{
87 return test_bit(facility, &x86_efi_facility) != 0;
88}
89EXPORT_SYMBOL(efi_enabled);
90
91static bool __initdata disable_runtime = false;
92static int __init setup_noefi(char *arg) 81static int __init setup_noefi(char *arg)
93{ 82{
94 disable_runtime = true; 83 disable_runtime = true;
@@ -257,27 +246,12 @@ static efi_status_t __init phys_efi_set_virtual_address_map(
257 return status; 246 return status;
258} 247}
259 248
260static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
261 efi_time_cap_t *tc)
262{
263 unsigned long flags;
264 efi_status_t status;
265
266 spin_lock_irqsave(&rtc_lock, flags);
267 efi_call_phys_prelog();
268 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
269 virt_to_phys(tc));
270 efi_call_phys_epilog();
271 spin_unlock_irqrestore(&rtc_lock, flags);
272 return status;
273}
274
275int efi_set_rtc_mmss(const struct timespec *now) 249int efi_set_rtc_mmss(const struct timespec *now)
276{ 250{
277 unsigned long nowtime = now->tv_sec; 251 unsigned long nowtime = now->tv_sec;
278 efi_status_t status; 252 efi_status_t status;
279 efi_time_t eft; 253 efi_time_t eft;
280 efi_time_cap_t cap; 254 efi_time_cap_t cap;
281 struct rtc_time tm; 255 struct rtc_time tm;
282 256
283 status = efi.get_time(&eft, &cap); 257 status = efi.get_time(&eft, &cap);
@@ -295,9 +269,8 @@ int efi_set_rtc_mmss(const struct timespec *now)
295 eft.second = tm.tm_sec; 269 eft.second = tm.tm_sec;
296 eft.nanosecond = 0; 270 eft.nanosecond = 0;
297 } else { 271 } else {
298 printk(KERN_ERR 272 pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
299 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n", 273 __func__, nowtime);
300 __FUNCTION__, nowtime);
301 return -1; 274 return -1;
302 } 275 }
303 276
@@ -413,8 +386,7 @@ static void __init print_efi_memmap(void)
413 p < memmap.map_end; 386 p < memmap.map_end;
414 p += memmap.desc_size, i++) { 387 p += memmap.desc_size, i++) {
415 md = p; 388 md = p;
416 pr_info("mem%02u: type=%u, attr=0x%llx, " 389 pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n",
417 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
418 i, md->type, md->attribute, md->phys_addr, 390 i, md->type, md->attribute, md->phys_addr,
419 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), 391 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
420 (md->num_pages >> (20 - EFI_PAGE_SHIFT))); 392 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
@@ -446,9 +418,8 @@ void __init efi_reserve_boot_services(void)
446 memblock_is_region_reserved(start, size)) { 418 memblock_is_region_reserved(start, size)) {
447 /* Could not reserve, skip it */ 419 /* Could not reserve, skip it */
448 md->num_pages = 0; 420 md->num_pages = 0;
449 memblock_dbg("Could not reserve boot range " 421 memblock_dbg("Could not reserve boot range [0x%010llx-0x%010llx]\n",
450 "[0x%010llx-0x%010llx]\n", 422 start, start+size-1);
451 start, start+size-1);
452 } else 423 } else
453 memblock_reserve(start, size); 424 memblock_reserve(start, size);
454 } 425 }
@@ -456,7 +427,7 @@ void __init efi_reserve_boot_services(void)
456 427
457void __init efi_unmap_memmap(void) 428void __init efi_unmap_memmap(void)
458{ 429{
459 clear_bit(EFI_MEMMAP, &x86_efi_facility); 430 clear_bit(EFI_MEMMAP, &efi.flags);
460 if (memmap.map) { 431 if (memmap.map) {
461 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); 432 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
462 memmap.map = NULL; 433 memmap.map = NULL;
@@ -467,9 +438,6 @@ void __init efi_free_boot_services(void)
467{ 438{
468 void *p; 439 void *p;
469 440
470 if (!efi_is_native())
471 return;
472
473 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { 441 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
474 efi_memory_desc_t *md = p; 442 efi_memory_desc_t *md = p;
475 unsigned long long start = md->phys_addr; 443 unsigned long long start = md->phys_addr;
@@ -584,45 +552,82 @@ static int __init efi_systab_init(void *phys)
584 return -EINVAL; 552 return -EINVAL;
585 } 553 }
586 if ((efi.systab->hdr.revision >> 16) == 0) 554 if ((efi.systab->hdr.revision >> 16) == 0)
587 pr_err("Warning: System table version " 555 pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n",
588 "%d.%02d, expected 1.00 or greater!\n",
589 efi.systab->hdr.revision >> 16, 556 efi.systab->hdr.revision >> 16,
590 efi.systab->hdr.revision & 0xffff); 557 efi.systab->hdr.revision & 0xffff);
591 558
559 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
560
592 return 0; 561 return 0;
593} 562}
594 563
595static int __init efi_runtime_init(void) 564static int __init efi_runtime_init32(void)
596{ 565{
597 efi_runtime_services_t *runtime; 566 efi_runtime_services_32_t *runtime;
567
568 runtime = early_ioremap((unsigned long)efi.systab->runtime,
569 sizeof(efi_runtime_services_32_t));
570 if (!runtime) {
571 pr_err("Could not map the runtime service table!\n");
572 return -ENOMEM;
573 }
598 574
599 /* 575 /*
600 * Check out the runtime services table. We need to map 576 * We will only need *early* access to the following two
601 * the runtime services table so that we can grab the physical 577 * EFI runtime services before set_virtual_address_map
602 * address of several of the EFI runtime functions, needed to 578 * is invoked.
603 * set the firmware into virtual mode.
604 */ 579 */
580 efi_phys.set_virtual_address_map =
581 (efi_set_virtual_address_map_t *)
582 (unsigned long)runtime->set_virtual_address_map;
583 early_iounmap(runtime, sizeof(efi_runtime_services_32_t));
584
585 return 0;
586}
587
588static int __init efi_runtime_init64(void)
589{
590 efi_runtime_services_64_t *runtime;
591
605 runtime = early_ioremap((unsigned long)efi.systab->runtime, 592 runtime = early_ioremap((unsigned long)efi.systab->runtime,
606 sizeof(efi_runtime_services_t)); 593 sizeof(efi_runtime_services_64_t));
607 if (!runtime) { 594 if (!runtime) {
608 pr_err("Could not map the runtime service table!\n"); 595 pr_err("Could not map the runtime service table!\n");
609 return -ENOMEM; 596 return -ENOMEM;
610 } 597 }
598
611 /* 599 /*
612 * We will only need *early* access to the following 600 * We will only need *early* access to the following two
613 * two EFI runtime services before set_virtual_address_map 601 * EFI runtime services before set_virtual_address_map
614 * is invoked. 602 * is invoked.
615 */ 603 */
616 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
617 efi_phys.set_virtual_address_map = 604 efi_phys.set_virtual_address_map =
618 (efi_set_virtual_address_map_t *) 605 (efi_set_virtual_address_map_t *)
619 runtime->set_virtual_address_map; 606 (unsigned long)runtime->set_virtual_address_map;
607 early_iounmap(runtime, sizeof(efi_runtime_services_64_t));
608
609 return 0;
610}
611
612static int __init efi_runtime_init(void)
613{
614 int rv;
615
620 /* 616 /*
621 * Make efi_get_time can be called before entering 617 * Check out the runtime services table. We need to map
622 * virtual mode. 618 * the runtime services table so that we can grab the physical
619 * address of several of the EFI runtime functions, needed to
620 * set the firmware into virtual mode.
623 */ 621 */
624 efi.get_time = phys_efi_get_time; 622 if (efi_enabled(EFI_64BIT))
625 early_iounmap(runtime, sizeof(efi_runtime_services_t)); 623 rv = efi_runtime_init64();
624 else
625 rv = efi_runtime_init32();
626
627 if (rv)
628 return rv;
629
630 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
626 631
627 return 0; 632 return 0;
628} 633}
@@ -641,6 +646,8 @@ static int __init efi_memmap_init(void)
641 if (add_efi_memmap) 646 if (add_efi_memmap)
642 do_add_efi_memmap(); 647 do_add_efi_memmap();
643 648
649 set_bit(EFI_MEMMAP, &efi.flags);
650
644 return 0; 651 return 0;
645} 652}
646 653
@@ -723,7 +730,7 @@ void __init efi_init(void)
723 if (efi_systab_init(efi_phys.systab)) 730 if (efi_systab_init(efi_phys.systab))
724 return; 731 return;
725 732
726 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility); 733 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
727 734
728 efi.config_table = (unsigned long)efi.systab->tables; 735 efi.config_table = (unsigned long)efi.systab->tables;
729 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor; 736 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
@@ -751,24 +758,21 @@ void __init efi_init(void)
751 if (efi_config_init(arch_tables)) 758 if (efi_config_init(arch_tables))
752 return; 759 return;
753 760
754 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
755
756 /* 761 /*
757 * Note: We currently don't support runtime services on an EFI 762 * Note: We currently don't support runtime services on an EFI
758 * that doesn't match the kernel 32/64-bit mode. 763 * that doesn't match the kernel 32/64-bit mode.
759 */ 764 */
760 765
761 if (!efi_is_native()) 766 if (!efi_runtime_supported())
762 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n"); 767 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
763 else { 768 else {
764 if (disable_runtime || efi_runtime_init()) 769 if (disable_runtime || efi_runtime_init())
765 return; 770 return;
766 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
767 } 771 }
768 if (efi_memmap_init()) 772 if (efi_memmap_init())
769 return; 773 return;
770 774
771 set_bit(EFI_MEMMAP, &x86_efi_facility); 775 set_bit(EFI_MEMMAP, &efi.flags);
772 776
773 print_efi_memmap(); 777 print_efi_memmap();
774} 778}
@@ -845,6 +849,22 @@ void __init old_map_region(efi_memory_desc_t *md)
845 (unsigned long long)md->phys_addr); 849 (unsigned long long)md->phys_addr);
846} 850}
847 851
852static void native_runtime_setup(void)
853{
854 efi.get_time = virt_efi_get_time;
855 efi.set_time = virt_efi_set_time;
856 efi.get_wakeup_time = virt_efi_get_wakeup_time;
857 efi.set_wakeup_time = virt_efi_set_wakeup_time;
858 efi.get_variable = virt_efi_get_variable;
859 efi.get_next_variable = virt_efi_get_next_variable;
860 efi.set_variable = virt_efi_set_variable;
861 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
862 efi.reset_system = virt_efi_reset_system;
863 efi.query_variable_info = virt_efi_query_variable_info;
864 efi.update_capsule = virt_efi_update_capsule;
865 efi.query_capsule_caps = virt_efi_query_capsule_caps;
866}
867
848/* Merge contiguous regions of the same type and attribute */ 868/* Merge contiguous regions of the same type and attribute */
849static void __init efi_merge_regions(void) 869static void __init efi_merge_regions(void)
850{ 870{
@@ -892,8 +912,9 @@ static void __init get_systab_virt_addr(efi_memory_desc_t *md)
892 } 912 }
893} 913}
894 914
895static int __init save_runtime_map(void) 915static void __init save_runtime_map(void)
896{ 916{
917#ifdef CONFIG_KEXEC
897 efi_memory_desc_t *md; 918 efi_memory_desc_t *md;
898 void *tmp, *p, *q = NULL; 919 void *tmp, *p, *q = NULL;
899 int count = 0; 920 int count = 0;
@@ -915,38 +936,44 @@ static int __init save_runtime_map(void)
915 } 936 }
916 937
917 efi_runtime_map_setup(q, count, memmap.desc_size); 938 efi_runtime_map_setup(q, count, memmap.desc_size);
939 return;
918 940
919 return 0;
920out: 941out:
921 kfree(q); 942 kfree(q);
922 return -ENOMEM; 943 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
944#endif
923} 945}
924 946
925/* 947static void *realloc_pages(void *old_memmap, int old_shift)
926 * Map efi regions which were passed via setup_data. The virt_addr is a fixed
927 * addr which was used in first kernel of a kexec boot.
928 */
929static void __init efi_map_regions_fixed(void)
930{ 948{
931 void *p; 949 void *ret;
932 efi_memory_desc_t *md;
933 950
934 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { 951 ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1);
935 md = p; 952 if (!ret)
936 efi_map_region_fixed(md); /* FIXME: add error handling */ 953 goto out;
937 get_systab_virt_addr(md); 954
938 } 955 /*
956 * A first-time allocation doesn't have anything to copy.
957 */
958 if (!old_memmap)
959 return ret;
939 960
961 memcpy(ret, old_memmap, PAGE_SIZE << old_shift);
962
963out:
964 free_pages((unsigned long)old_memmap, old_shift);
965 return ret;
940} 966}
941 967
942/* 968/*
943 * Map efi memory ranges for runtime serivce and update new_memmap with virtual 969 * Map the efi memory ranges of the runtime services and update new_mmap with
944 * addresses. 970 * virtual addresses.
945 */ 971 */
946static void * __init efi_map_regions(int *count) 972static void * __init efi_map_regions(int *count, int *pg_shift)
947{ 973{
974 void *p, *new_memmap = NULL;
975 unsigned long left = 0;
948 efi_memory_desc_t *md; 976 efi_memory_desc_t *md;
949 void *p, *tmp, *new_memmap = NULL;
950 977
951 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { 978 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
952 md = p; 979 md = p;
@@ -961,20 +988,80 @@ static void * __init efi_map_regions(int *count)
961 efi_map_region(md); 988 efi_map_region(md);
962 get_systab_virt_addr(md); 989 get_systab_virt_addr(md);
963 990
964 tmp = krealloc(new_memmap, (*count + 1) * memmap.desc_size, 991 if (left < memmap.desc_size) {
965 GFP_KERNEL); 992 new_memmap = realloc_pages(new_memmap, *pg_shift);
966 if (!tmp) 993 if (!new_memmap)
967 goto out; 994 return NULL;
968 new_memmap = tmp; 995
996 left += PAGE_SIZE << *pg_shift;
997 (*pg_shift)++;
998 }
999
969 memcpy(new_memmap + (*count * memmap.desc_size), md, 1000 memcpy(new_memmap + (*count * memmap.desc_size), md,
970 memmap.desc_size); 1001 memmap.desc_size);
1002
1003 left -= memmap.desc_size;
971 (*count)++; 1004 (*count)++;
972 } 1005 }
973 1006
974 return new_memmap; 1007 return new_memmap;
975out: 1008}
976 kfree(new_memmap); 1009
977 return NULL; 1010static void __init kexec_enter_virtual_mode(void)
1011{
1012#ifdef CONFIG_KEXEC
1013 efi_memory_desc_t *md;
1014 void *p;
1015
1016 efi.systab = NULL;
1017
1018 /*
1019 * We don't do virtual mode, since we don't do runtime services, on
1020 * non-native EFI
1021 */
1022 if (!efi_is_native()) {
1023 efi_unmap_memmap();
1024 return;
1025 }
1026
1027 /*
1028 * Map efi regions which were passed via setup_data. The virt_addr is a
1029 * fixed addr which was used in first kernel of a kexec boot.
1030 */
1031 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1032 md = p;
1033 efi_map_region_fixed(md); /* FIXME: add error handling */
1034 get_systab_virt_addr(md);
1035 }
1036
1037 save_runtime_map();
1038
1039 BUG_ON(!efi.systab);
1040
1041 efi_sync_low_kernel_mappings();
1042
1043 /*
1044 * Now that EFI is in virtual mode, update the function
1045 * pointers in the runtime service table to the new virtual addresses.
1046 *
1047 * Call EFI services through wrapper functions.
1048 */
1049 efi.runtime_version = efi_systab.hdr.revision;
1050
1051 native_runtime_setup();
1052
1053 efi.set_virtual_address_map = NULL;
1054
1055 if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX))
1056 runtime_code_page_mkexec();
1057
1058 /* clean DUMMY object */
1059 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
1060 EFI_VARIABLE_NON_VOLATILE |
1061 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1062 EFI_VARIABLE_RUNTIME_ACCESS,
1063 0, NULL);
1064#endif
978} 1065}
979 1066
980/* 1067/*
@@ -996,57 +1083,53 @@ out:
996 * 1083 *
997 * Specially for kexec boot, efi runtime maps in previous kernel should 1084 * Specially for kexec boot, efi runtime maps in previous kernel should
998 * be passed in via setup_data. In that case runtime ranges will be mapped 1085 * be passed in via setup_data. In that case runtime ranges will be mapped
999 * to the same virtual addresses as the first kernel. 1086 * to the same virtual addresses as the first kernel, see
1087 * kexec_enter_virtual_mode().
1000 */ 1088 */
1001void __init efi_enter_virtual_mode(void) 1089static void __init __efi_enter_virtual_mode(void)
1002{ 1090{
1003 efi_status_t status; 1091 int count = 0, pg_shift = 0;
1004 void *new_memmap = NULL; 1092 void *new_memmap = NULL;
1005 int err, count = 0; 1093 efi_status_t status;
1006 1094
1007 efi.systab = NULL; 1095 efi.systab = NULL;
1008 1096
1009 /* 1097 efi_merge_regions();
1010 * We don't do virtual mode, since we don't do runtime services, on 1098 new_memmap = efi_map_regions(&count, &pg_shift);
1011 * non-native EFI 1099 if (!new_memmap) {
1012 */ 1100 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1013 if (!efi_is_native()) {
1014 efi_unmap_memmap();
1015 return; 1101 return;
1016 } 1102 }
1017 1103
1018 if (efi_setup) { 1104 save_runtime_map();
1019 efi_map_regions_fixed();
1020 } else {
1021 efi_merge_regions();
1022 new_memmap = efi_map_regions(&count);
1023 if (!new_memmap) {
1024 pr_err("Error reallocating memory, EFI runtime non-functional!\n");
1025 return;
1026 }
1027 }
1028
1029 err = save_runtime_map();
1030 if (err)
1031 pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n");
1032 1105
1033 BUG_ON(!efi.systab); 1106 BUG_ON(!efi.systab);
1034 1107
1035 efi_setup_page_tables(); 1108 if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift))
1109 return;
1110
1036 efi_sync_low_kernel_mappings(); 1111 efi_sync_low_kernel_mappings();
1112 efi_dump_pagetable();
1037 1113
1038 if (!efi_setup) { 1114 if (efi_is_native()) {
1039 status = phys_efi_set_virtual_address_map( 1115 status = phys_efi_set_virtual_address_map(
1040 memmap.desc_size * count, 1116 memmap.desc_size * count,
1041 memmap.desc_size, 1117 memmap.desc_size,
1042 memmap.desc_version, 1118 memmap.desc_version,
1043 (efi_memory_desc_t *)__pa(new_memmap)); 1119 (efi_memory_desc_t *)__pa(new_memmap));
1044 1120 } else {
1045 if (status != EFI_SUCCESS) { 1121 status = efi_thunk_set_virtual_address_map(
1046 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n", 1122 efi_phys.set_virtual_address_map,
1047 status); 1123 memmap.desc_size * count,
1048 panic("EFI call to SetVirtualAddressMap() failed!"); 1124 memmap.desc_size,
1049 } 1125 memmap.desc_version,
1126 (efi_memory_desc_t *)__pa(new_memmap));
1127 }
1128
1129 if (status != EFI_SUCCESS) {
1130 pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
1131 status);
1132 panic("EFI call to SetVirtualAddressMap() failed!");
1050 } 1133 }
1051 1134
1052 /* 1135 /*
@@ -1056,23 +1139,43 @@ void __init efi_enter_virtual_mode(void)
1056 * Call EFI services through wrapper functions. 1139 * Call EFI services through wrapper functions.
1057 */ 1140 */
1058 efi.runtime_version = efi_systab.hdr.revision; 1141 efi.runtime_version = efi_systab.hdr.revision;
1059 efi.get_time = virt_efi_get_time; 1142
1060 efi.set_time = virt_efi_set_time; 1143 if (efi_is_native())
1061 efi.get_wakeup_time = virt_efi_get_wakeup_time; 1144 native_runtime_setup();
1062 efi.set_wakeup_time = virt_efi_set_wakeup_time; 1145 else
1063 efi.get_variable = virt_efi_get_variable; 1146 efi_thunk_runtime_setup();
1064 efi.get_next_variable = virt_efi_get_next_variable; 1147
1065 efi.set_variable = virt_efi_set_variable;
1066 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
1067 efi.reset_system = virt_efi_reset_system;
1068 efi.set_virtual_address_map = NULL; 1148 efi.set_virtual_address_map = NULL;
1069 efi.query_variable_info = virt_efi_query_variable_info;
1070 efi.update_capsule = virt_efi_update_capsule;
1071 efi.query_capsule_caps = virt_efi_query_capsule_caps;
1072 1149
1073 efi_runtime_mkexec(); 1150 efi_runtime_mkexec();
1074 1151
1075 kfree(new_memmap); 1152 /*
1153 * We mapped the descriptor array into the EFI pagetable above but we're
1154 * not unmapping it here. Here's why:
1155 *
1156 * We're copying select PGDs from the kernel page table to the EFI page
1157 * table and when we do so and make changes to those PGDs like unmapping
1158 * stuff from them, those changes appear in the kernel page table and we
1159 * go boom.
1160 *
1161 * From setup_real_mode():
1162 *
1163 * ...
1164 * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd;
1165 *
1166 * In this particular case, our allocation is in PGD 0 of the EFI page
1167 * table but we've copied that PGD from PGD[272] of the EFI page table:
1168 *
1169 * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272
1170 *
1171 * where the direct memory mapping in kernel space is.
1172 *
1173 * new_memmap's VA comes from that direct mapping and thus clearing it,
1174 * it would get cleared in the kernel page table too.
1175 *
1176 * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift);
1177 */
1178 free_pages((unsigned long)new_memmap, pg_shift);
1076 1179
1077 /* clean DUMMY object */ 1180 /* clean DUMMY object */
1078 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID, 1181 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
@@ -1082,6 +1185,14 @@ void __init efi_enter_virtual_mode(void)
1082 0, NULL); 1185 0, NULL);
1083} 1186}
1084 1187
1188void __init efi_enter_virtual_mode(void)
1189{
1190 if (efi_setup)
1191 kexec_enter_virtual_mode();
1192 else
1193 __efi_enter_virtual_mode();
1194}
1195
1085/* 1196/*
1086 * Convenience functions to obtain memory types and attributes 1197 * Convenience functions to obtain memory types and attributes
1087 */ 1198 */
@@ -1119,9 +1230,8 @@ u64 efi_mem_attributes(unsigned long phys_addr)
1119} 1230}
1120 1231
1121/* 1232/*
1122 * Some firmware has serious problems when using more than 50% of the EFI 1233 * Some firmware implementations refuse to boot if there's insufficient space
1123 * variable store, i.e. it triggers bugs that can brick machines. Ensure that 1234 * in the variable store. Ensure that we never use more than a safe limit.
1124 * we never use more than this safe limit.
1125 * 1235 *
1126 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable 1236 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
1127 * store. 1237 * store.
@@ -1140,10 +1250,9 @@ efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
1140 return status; 1250 return status;
1141 1251
1142 /* 1252 /*
1143 * Some firmware implementations refuse to boot if there's insufficient 1253 * We account for that by refusing the write if permitting it would
1144 * space in the variable store. We account for that by refusing the 1254 * reduce the available space to under 5KB. This figure was provided by
1145 * write if permitting it would reduce the available space to under 1255 * Samsung, so should be safe.
1146 * 5KB. This figure was provided by Samsung, so should be safe.
1147 */ 1256 */
1148 if ((remaining_size - size < EFI_MIN_RESERVE) && 1257 if ((remaining_size - size < EFI_MIN_RESERVE) &&
1149 !efi_no_storage_paranoia) { 1258 !efi_no_storage_paranoia) {
@@ -1206,7 +1315,7 @@ static int __init parse_efi_cmdline(char *str)
1206 str++; 1315 str++;
1207 1316
1208 if (!strncmp(str, "old_map", 7)) 1317 if (!strncmp(str, "old_map", 7))
1209 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility); 1318 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1210 1319
1211 return 0; 1320 return 0;
1212} 1321}
@@ -1219,7 +1328,7 @@ void __init efi_apply_memmap_quirks(void)
1219 * firmware/kernel architectures since there is no support for runtime 1328 * firmware/kernel architectures since there is no support for runtime
1220 * services. 1329 * services.
1221 */ 1330 */
1222 if (!efi_is_native()) { 1331 if (!efi_runtime_supported()) {
1223 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n"); 1332 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1224 efi_unmap_memmap(); 1333 efi_unmap_memmap();
1225 } 1334 }
@@ -1228,5 +1337,5 @@ void __init efi_apply_memmap_quirks(void)
1228 * UV doesn't support the new EFI pagetable mapping yet. 1337 * UV doesn't support the new EFI pagetable mapping yet.
1229 */ 1338 */
1230 if (is_uv_system()) 1339 if (is_uv_system())
1231 set_bit(EFI_OLD_MEMMAP, &x86_efi_facility); 1340 set_bit(EFI_OLD_MEMMAP, &efi.flags);
1232} 1341}
diff --git a/arch/x86/platform/efi/efi_32.c b/arch/x86/platform/efi/efi_32.c
index 0b74cdf7f816..9ee3491e31fb 100644
--- a/arch/x86/platform/efi/efi_32.c
+++ b/arch/x86/platform/efi/efi_32.c
@@ -40,7 +40,12 @@
40static unsigned long efi_rt_eflags; 40static unsigned long efi_rt_eflags;
41 41
42void efi_sync_low_kernel_mappings(void) {} 42void efi_sync_low_kernel_mappings(void) {}
43void efi_setup_page_tables(void) {} 43void __init efi_dump_pagetable(void) {}
44int efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
45{
46 return 0;
47}
48void efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages) {}
44 49
45void __init efi_map_region(efi_memory_desc_t *md) 50void __init efi_map_region(efi_memory_desc_t *md)
46{ 51{
diff --git a/arch/x86/platform/efi/efi_64.c b/arch/x86/platform/efi/efi_64.c
index 0c2a234fef1e..290d397e1dd9 100644
--- a/arch/x86/platform/efi/efi_64.c
+++ b/arch/x86/platform/efi/efi_64.c
@@ -39,6 +39,7 @@
39#include <asm/cacheflush.h> 39#include <asm/cacheflush.h>
40#include <asm/fixmap.h> 40#include <asm/fixmap.h>
41#include <asm/realmode.h> 41#include <asm/realmode.h>
42#include <asm/time.h>
42 43
43static pgd_t *save_pgd __initdata; 44static pgd_t *save_pgd __initdata;
44static unsigned long efi_flags __initdata; 45static unsigned long efi_flags __initdata;
@@ -58,7 +59,8 @@ struct efi_scratch {
58 u64 prev_cr3; 59 u64 prev_cr3;
59 pgd_t *efi_pgt; 60 pgd_t *efi_pgt;
60 bool use_pgd; 61 bool use_pgd;
61}; 62 u64 phys_stack;
63} __packed;
62 64
63static void __init early_code_mapping_set_exec(int executable) 65static void __init early_code_mapping_set_exec(int executable)
64{ 66{
@@ -137,12 +139,64 @@ void efi_sync_low_kernel_mappings(void)
137 sizeof(pgd_t) * num_pgds); 139 sizeof(pgd_t) * num_pgds);
138} 140}
139 141
140void efi_setup_page_tables(void) 142int efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
141{ 143{
144 unsigned long text;
145 struct page *page;
146 unsigned npages;
147 pgd_t *pgd;
148
149 if (efi_enabled(EFI_OLD_MEMMAP))
150 return 0;
151
142 efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd; 152 efi_scratch.efi_pgt = (pgd_t *)(unsigned long)real_mode_header->trampoline_pgd;
153 pgd = __va(efi_scratch.efi_pgt);
143 154
144 if (!efi_enabled(EFI_OLD_MEMMAP)) 155 /*
145 efi_scratch.use_pgd = true; 156 * It can happen that the physical address of new_memmap lands in memory
157 * which is not mapped in the EFI page table. Therefore we need to go
158 * and ident-map those pages containing the map before calling
159 * phys_efi_set_virtual_address_map().
160 */
161 if (kernel_map_pages_in_pgd(pgd, pa_memmap, pa_memmap, num_pages, _PAGE_NX)) {
162 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
163 return 1;
164 }
165
166 efi_scratch.use_pgd = true;
167
168 /*
169 * When making calls to the firmware everything needs to be 1:1
170 * mapped and addressable with 32-bit pointers. Map the kernel
171 * text and allocate a new stack because we can't rely on the
172 * stack pointer being < 4GB.
173 */
174 if (!IS_ENABLED(CONFIG_EFI_MIXED))
175 return 0;
176
177 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
178 if (!page)
179 panic("Unable to allocate EFI runtime stack < 4GB\n");
180
181 efi_scratch.phys_stack = virt_to_phys(page_address(page));
182 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
183
184 npages = (_end - _text) >> PAGE_SHIFT;
185 text = __pa(_text);
186
187 if (kernel_map_pages_in_pgd(pgd, text >> PAGE_SHIFT, text, npages, 0)) {
188 pr_err("Failed to map kernel text 1:1\n");
189 return 1;
190 }
191
192 return 0;
193}
194
195void efi_cleanup_page_tables(unsigned long pa_memmap, unsigned num_pages)
196{
197 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
198
199 kernel_unmap_pages_in_pgd(pgd, pa_memmap, num_pages);
146} 200}
147 201
148static void __init __map_region(efi_memory_desc_t *md, u64 va) 202static void __init __map_region(efi_memory_desc_t *md, u64 va)
@@ -173,6 +227,16 @@ void __init efi_map_region(efi_memory_desc_t *md)
173 */ 227 */
174 __map_region(md, md->phys_addr); 228 __map_region(md, md->phys_addr);
175 229
230 /*
231 * Enforce the 1:1 mapping as the default virtual address when
232 * booting in EFI mixed mode, because even though we may be
233 * running a 64-bit kernel, the firmware may only be 32-bit.
234 */
235 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
236 md->virt_addr = md->phys_addr;
237 return;
238 }
239
176 efi_va -= size; 240 efi_va -= size;
177 241
178 /* Is PA 2M-aligned? */ 242 /* Is PA 2M-aligned? */
@@ -242,3 +306,299 @@ void __init efi_runtime_mkexec(void)
242 if (__supported_pte_mask & _PAGE_NX) 306 if (__supported_pte_mask & _PAGE_NX)
243 runtime_code_page_mkexec(); 307 runtime_code_page_mkexec();
244} 308}
309
310void __init efi_dump_pagetable(void)
311{
312#ifdef CONFIG_EFI_PGT_DUMP
313 pgd_t *pgd = (pgd_t *)__va(real_mode_header->trampoline_pgd);
314
315 ptdump_walk_pgd_level(NULL, pgd);
316#endif
317}
318
319#ifdef CONFIG_EFI_MIXED
320extern efi_status_t efi64_thunk(u32, ...);
321
322#define runtime_service32(func) \
323({ \
324 u32 table = (u32)(unsigned long)efi.systab; \
325 u32 *rt, *___f; \
326 \
327 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
328 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
329 *___f; \
330})
331
332/*
333 * Switch to the EFI page tables early so that we can access the 1:1
334 * runtime services mappings which are not mapped in any other page
335 * tables. This function must be called before runtime_service32().
336 *
337 * Also, disable interrupts because the IDT points to 64-bit handlers,
338 * which aren't going to function correctly when we switch to 32-bit.
339 */
340#define efi_thunk(f, ...) \
341({ \
342 efi_status_t __s; \
343 unsigned long flags; \
344 u32 func; \
345 \
346 efi_sync_low_kernel_mappings(); \
347 local_irq_save(flags); \
348 \
349 efi_scratch.prev_cr3 = read_cr3(); \
350 write_cr3((unsigned long)efi_scratch.efi_pgt); \
351 __flush_tlb_all(); \
352 \
353 func = runtime_service32(f); \
354 __s = efi64_thunk(func, __VA_ARGS__); \
355 \
356 write_cr3(efi_scratch.prev_cr3); \
357 __flush_tlb_all(); \
358 local_irq_restore(flags); \
359 \
360 __s; \
361})
362
363efi_status_t efi_thunk_set_virtual_address_map(
364 void *phys_set_virtual_address_map,
365 unsigned long memory_map_size,
366 unsigned long descriptor_size,
367 u32 descriptor_version,
368 efi_memory_desc_t *virtual_map)
369{
370 efi_status_t status;
371 unsigned long flags;
372 u32 func;
373
374 efi_sync_low_kernel_mappings();
375 local_irq_save(flags);
376
377 efi_scratch.prev_cr3 = read_cr3();
378 write_cr3((unsigned long)efi_scratch.efi_pgt);
379 __flush_tlb_all();
380
381 func = (u32)(unsigned long)phys_set_virtual_address_map;
382 status = efi64_thunk(func, memory_map_size, descriptor_size,
383 descriptor_version, virtual_map);
384
385 write_cr3(efi_scratch.prev_cr3);
386 __flush_tlb_all();
387 local_irq_restore(flags);
388
389 return status;
390}
391
392static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
393{
394 efi_status_t status;
395 u32 phys_tm, phys_tc;
396
397 spin_lock(&rtc_lock);
398
399 phys_tm = virt_to_phys(tm);
400 phys_tc = virt_to_phys(tc);
401
402 status = efi_thunk(get_time, phys_tm, phys_tc);
403
404 spin_unlock(&rtc_lock);
405
406 return status;
407}
408
409static efi_status_t efi_thunk_set_time(efi_time_t *tm)
410{
411 efi_status_t status;
412 u32 phys_tm;
413
414 spin_lock(&rtc_lock);
415
416 phys_tm = virt_to_phys(tm);
417
418 status = efi_thunk(set_time, phys_tm);
419
420 spin_unlock(&rtc_lock);
421
422 return status;
423}
424
425static efi_status_t
426efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
427 efi_time_t *tm)
428{
429 efi_status_t status;
430 u32 phys_enabled, phys_pending, phys_tm;
431
432 spin_lock(&rtc_lock);
433
434 phys_enabled = virt_to_phys(enabled);
435 phys_pending = virt_to_phys(pending);
436 phys_tm = virt_to_phys(tm);
437
438 status = efi_thunk(get_wakeup_time, phys_enabled,
439 phys_pending, phys_tm);
440
441 spin_unlock(&rtc_lock);
442
443 return status;
444}
445
446static efi_status_t
447efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
448{
449 efi_status_t status;
450 u32 phys_tm;
451
452 spin_lock(&rtc_lock);
453
454 phys_tm = virt_to_phys(tm);
455
456 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
457
458 spin_unlock(&rtc_lock);
459
460 return status;
461}
462
463
464static efi_status_t
465efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
466 u32 *attr, unsigned long *data_size, void *data)
467{
468 efi_status_t status;
469 u32 phys_name, phys_vendor, phys_attr;
470 u32 phys_data_size, phys_data;
471
472 phys_data_size = virt_to_phys(data_size);
473 phys_vendor = virt_to_phys(vendor);
474 phys_name = virt_to_phys(name);
475 phys_attr = virt_to_phys(attr);
476 phys_data = virt_to_phys(data);
477
478 status = efi_thunk(get_variable, phys_name, phys_vendor,
479 phys_attr, phys_data_size, phys_data);
480
481 return status;
482}
483
484static efi_status_t
485efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
486 u32 attr, unsigned long data_size, void *data)
487{
488 u32 phys_name, phys_vendor, phys_data;
489 efi_status_t status;
490
491 phys_name = virt_to_phys(name);
492 phys_vendor = virt_to_phys(vendor);
493 phys_data = virt_to_phys(data);
494
495 /* If data_size is > sizeof(u32) we've got problems */
496 status = efi_thunk(set_variable, phys_name, phys_vendor,
497 attr, data_size, phys_data);
498
499 return status;
500}
501
502static efi_status_t
503efi_thunk_get_next_variable(unsigned long *name_size,
504 efi_char16_t *name,
505 efi_guid_t *vendor)
506{
507 efi_status_t status;
508 u32 phys_name_size, phys_name, phys_vendor;
509
510 phys_name_size = virt_to_phys(name_size);
511 phys_vendor = virt_to_phys(vendor);
512 phys_name = virt_to_phys(name);
513
514 status = efi_thunk(get_next_variable, phys_name_size,
515 phys_name, phys_vendor);
516
517 return status;
518}
519
520static efi_status_t
521efi_thunk_get_next_high_mono_count(u32 *count)
522{
523 efi_status_t status;
524 u32 phys_count;
525
526 phys_count = virt_to_phys(count);
527 status = efi_thunk(get_next_high_mono_count, phys_count);
528
529 return status;
530}
531
532static void
533efi_thunk_reset_system(int reset_type, efi_status_t status,
534 unsigned long data_size, efi_char16_t *data)
535{
536 u32 phys_data;
537
538 phys_data = virt_to_phys(data);
539
540 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
541}
542
543static efi_status_t
544efi_thunk_update_capsule(efi_capsule_header_t **capsules,
545 unsigned long count, unsigned long sg_list)
546{
547 /*
548 * To properly support this function we would need to repackage
549 * 'capsules' because the firmware doesn't understand 64-bit
550 * pointers.
551 */
552 return EFI_UNSUPPORTED;
553}
554
555static efi_status_t
556efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
557 u64 *remaining_space,
558 u64 *max_variable_size)
559{
560 efi_status_t status;
561 u32 phys_storage, phys_remaining, phys_max;
562
563 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
564 return EFI_UNSUPPORTED;
565
566 phys_storage = virt_to_phys(storage_space);
567 phys_remaining = virt_to_phys(remaining_space);
568 phys_max = virt_to_phys(max_variable_size);
569
570 status = efi_thunk(query_variable_info, attr, phys_storage,
571 phys_remaining, phys_max);
572
573 return status;
574}
575
576static efi_status_t
577efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
578 unsigned long count, u64 *max_size,
579 int *reset_type)
580{
581 /*
582 * To properly support this function we would need to repackage
583 * 'capsules' because the firmware doesn't understand 64-bit
584 * pointers.
585 */
586 return EFI_UNSUPPORTED;
587}
588
589void efi_thunk_runtime_setup(void)
590{
591 efi.get_time = efi_thunk_get_time;
592 efi.set_time = efi_thunk_set_time;
593 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
594 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
595 efi.get_variable = efi_thunk_get_variable;
596 efi.get_next_variable = efi_thunk_get_next_variable;
597 efi.set_variable = efi_thunk_set_variable;
598 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
599 efi.reset_system = efi_thunk_reset_system;
600 efi.query_variable_info = efi_thunk_query_variable_info;
601 efi.update_capsule = efi_thunk_update_capsule;
602 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
603}
604#endif /* CONFIG_EFI_MIXED */
diff --git a/arch/x86/platform/efi/efi_stub_64.S b/arch/x86/platform/efi/efi_stub_64.S
index 88073b140298..e0984ef0374b 100644
--- a/arch/x86/platform/efi/efi_stub_64.S
+++ b/arch/x86/platform/efi/efi_stub_64.S
@@ -7,6 +7,10 @@
7 */ 7 */
8 8
9#include <linux/linkage.h> 9#include <linux/linkage.h>
10#include <asm/segment.h>
11#include <asm/msr.h>
12#include <asm/processor-flags.h>
13#include <asm/page_types.h>
10 14
11#define SAVE_XMM \ 15#define SAVE_XMM \
12 mov %rsp, %rax; \ 16 mov %rsp, %rax; \
@@ -164,7 +168,169 @@ ENTRY(efi_call6)
164 ret 168 ret
165ENDPROC(efi_call6) 169ENDPROC(efi_call6)
166 170
171#ifdef CONFIG_EFI_MIXED
172
173/*
174 * We run this function from the 1:1 mapping.
175 *
176 * This function must be invoked with a 1:1 mapped stack.
177 */
178ENTRY(__efi64_thunk)
179 movl %ds, %eax
180 push %rax
181 movl %es, %eax
182 push %rax
183 movl %ss, %eax
184 push %rax
185
186 subq $32, %rsp
187 movl %esi, 0x0(%rsp)
188 movl %edx, 0x4(%rsp)
189 movl %ecx, 0x8(%rsp)
190 movq %r8, %rsi
191 movl %esi, 0xc(%rsp)
192 movq %r9, %rsi
193 movl %esi, 0x10(%rsp)
194
195 sgdt save_gdt(%rip)
196
197 leaq 1f(%rip), %rbx
198 movq %rbx, func_rt_ptr(%rip)
199
200 /* Switch to gdt with 32-bit segments */
201 movl 64(%rsp), %eax
202 lgdt (%rax)
203
204 leaq efi_enter32(%rip), %rax
205 pushq $__KERNEL_CS
206 pushq %rax
207 lretq
208
2091: addq $32, %rsp
210
211 lgdt save_gdt(%rip)
212
213 pop %rbx
214 movl %ebx, %ss
215 pop %rbx
216 movl %ebx, %es
217 pop %rbx
218 movl %ebx, %ds
219
220 /*
221 * Convert 32-bit status code into 64-bit.
222 */
223 test %rax, %rax
224 jz 1f
225 movl %eax, %ecx
226 andl $0x0fffffff, %ecx
227 andl $0xf0000000, %eax
228 shl $32, %rax
229 or %rcx, %rax
2301:
231 ret
232ENDPROC(__efi64_thunk)
233
234ENTRY(efi_exit32)
235 movq func_rt_ptr(%rip), %rax
236 push %rax
237 mov %rdi, %rax
238 ret
239ENDPROC(efi_exit32)
240
241 .code32
242/*
243 * EFI service pointer must be in %edi.
244 *
245 * The stack should represent the 32-bit calling convention.
246 */
247ENTRY(efi_enter32)
248 movl $__KERNEL_DS, %eax
249 movl %eax, %ds
250 movl %eax, %es
251 movl %eax, %ss
252
253 /* Reload pgtables */
254 movl %cr3, %eax
255 movl %eax, %cr3
256
257 /* Disable paging */
258 movl %cr0, %eax
259 btrl $X86_CR0_PG_BIT, %eax
260 movl %eax, %cr0
261
262 /* Disable long mode via EFER */
263 movl $MSR_EFER, %ecx
264 rdmsr
265 btrl $_EFER_LME, %eax
266 wrmsr
267
268 call *%edi
269
270 /* We must preserve return value */
271 movl %eax, %edi
272
273 /*
274 * Some firmware will return with interrupts enabled. Be sure to
275 * disable them before we switch GDTs.
276 */
277 cli
278
279 movl 68(%esp), %eax
280 movl %eax, 2(%eax)
281 lgdtl (%eax)
282
283 movl %cr4, %eax
284 btsl $(X86_CR4_PAE_BIT), %eax
285 movl %eax, %cr4
286
287 movl %cr3, %eax
288 movl %eax, %cr3
289
290 movl $MSR_EFER, %ecx
291 rdmsr
292 btsl $_EFER_LME, %eax
293 wrmsr
294
295 xorl %eax, %eax
296 lldt %ax
297
298 movl 72(%esp), %eax
299 pushl $__KERNEL_CS
300 pushl %eax
301
302 /* Enable paging */
303 movl %cr0, %eax
304 btsl $X86_CR0_PG_BIT, %eax
305 movl %eax, %cr0
306 lret
307ENDPROC(efi_enter32)
308
309 .data
310 .balign 8
311 .global efi32_boot_gdt
312efi32_boot_gdt: .word 0
313 .quad 0
314
315save_gdt: .word 0
316 .quad 0
317func_rt_ptr: .quad 0
318
319 .global efi_gdt64
320efi_gdt64:
321 .word efi_gdt64_end - efi_gdt64
322 .long 0 /* Filled out by user */
323 .word 0
324 .quad 0x0000000000000000 /* NULL descriptor */
325 .quad 0x00af9a000000ffff /* __KERNEL_CS */
326 .quad 0x00cf92000000ffff /* __KERNEL_DS */
327 .quad 0x0080890000000000 /* TS descriptor */
328 .quad 0x0000000000000000 /* TS continued */
329efi_gdt64_end:
330#endif /* CONFIG_EFI_MIXED */
331
167 .data 332 .data
168ENTRY(efi_scratch) 333ENTRY(efi_scratch)
169 .fill 3,8,0 334 .fill 3,8,0
170 .byte 0 335 .byte 0
336 .quad 0
diff --git a/arch/x86/platform/efi/efi_thunk_64.S b/arch/x86/platform/efi/efi_thunk_64.S
new file mode 100644
index 000000000000..8806fa73e6e6
--- /dev/null
+++ b/arch/x86/platform/efi/efi_thunk_64.S
@@ -0,0 +1,65 @@
1/*
2 * Copyright (C) 2014 Intel Corporation; author Matt Fleming
3 */
4
5#include <linux/linkage.h>
6#include <asm/page_types.h>
7
8 .text
9 .code64
10ENTRY(efi64_thunk)
11 push %rbp
12 push %rbx
13
14 /*
15 * Switch to 1:1 mapped 32-bit stack pointer.
16 */
17 movq %rsp, efi_saved_sp(%rip)
18 movq efi_scratch+25(%rip), %rsp
19
20 /*
21 * Calculate the physical address of the kernel text.
22 */
23 movq $__START_KERNEL_map, %rax
24 subq phys_base(%rip), %rax
25
26 /*
27 * Push some physical addresses onto the stack. This is easier
28 * to do now in a code64 section while the assembler can address
29 * 64-bit values. Note that all the addresses on the stack are
30 * 32-bit.
31 */
32 subq $16, %rsp
33 leaq efi_exit32(%rip), %rbx
34 subq %rax, %rbx
35 movl %ebx, 8(%rsp)
36 leaq efi_gdt64(%rip), %rbx
37 subq %rax, %rbx
38 movl %ebx, 2(%ebx)
39 movl %ebx, 4(%rsp)
40 leaq efi_gdt32(%rip), %rbx
41 subq %rax, %rbx
42 movl %ebx, 2(%ebx)
43 movl %ebx, (%rsp)
44
45 leaq __efi64_thunk(%rip), %rbx
46 subq %rax, %rbx
47 call *%rbx
48
49 movq efi_saved_sp(%rip), %rsp
50 pop %rbx
51 pop %rbp
52 retq
53ENDPROC(efi64_thunk)
54
55 .data
56efi_gdt32:
57 .word efi_gdt32_end - efi_gdt32
58 .long 0 /* Filled out above */
59 .word 0
60 .quad 0x0000000000000000 /* NULL descriptor */
61 .quad 0x00cf9a000000ffff /* __KERNEL_CS */
62 .quad 0x00cf93000000ffff /* __KERNEL_DS */
63efi_gdt32_end:
64
65efi_saved_sp: .quad 0
diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
index b6bffbfd3be7..ff50aeebf0d9 100644
--- a/drivers/firmware/efi/efi-stub-helper.c
+++ b/drivers/firmware/efi/efi-stub-helper.c
@@ -16,18 +16,6 @@ struct file_info {
16 u64 size; 16 u64 size;
17}; 17};
18 18
19
20
21
22static void efi_char16_printk(efi_system_table_t *sys_table_arg,
23 efi_char16_t *str)
24{
25 struct efi_simple_text_output_protocol *out;
26
27 out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
28 efi_call_phys2(out->output_string, out, str);
29}
30
31static void efi_printk(efi_system_table_t *sys_table_arg, char *str) 19static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
32{ 20{
33 char *s8; 21 char *s8;
@@ -65,20 +53,23 @@ again:
65 * allocation which may be in a new descriptor region. 53 * allocation which may be in a new descriptor region.
66 */ 54 */
67 *map_size += sizeof(*m); 55 *map_size += sizeof(*m);
68 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, 56 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
69 EFI_LOADER_DATA, *map_size, (void **)&m); 57 *map_size, (void **)&m);
70 if (status != EFI_SUCCESS) 58 if (status != EFI_SUCCESS)
71 goto fail; 59 goto fail;
72 60
73 status = efi_call_phys5(sys_table_arg->boottime->get_memory_map, 61 *desc_size = 0;
74 map_size, m, &key, desc_size, &desc_version); 62 key = 0;
63 status = efi_call_early(get_memory_map, map_size, m,
64 &key, desc_size, &desc_version);
75 if (status == EFI_BUFFER_TOO_SMALL) { 65 if (status == EFI_BUFFER_TOO_SMALL) {
76 efi_call_phys1(sys_table_arg->boottime->free_pool, m); 66 efi_call_early(free_pool, m);
77 goto again; 67 goto again;
78 } 68 }
79 69
80 if (status != EFI_SUCCESS) 70 if (status != EFI_SUCCESS)
81 efi_call_phys1(sys_table_arg->boottime->free_pool, m); 71 efi_call_early(free_pool, m);
72
82 if (key_ptr && status == EFI_SUCCESS) 73 if (key_ptr && status == EFI_SUCCESS)
83 *key_ptr = key; 74 *key_ptr = key;
84 if (desc_ver && status == EFI_SUCCESS) 75 if (desc_ver && status == EFI_SUCCESS)
@@ -158,7 +149,7 @@ again:
158 if (!max_addr) 149 if (!max_addr)
159 status = EFI_NOT_FOUND; 150 status = EFI_NOT_FOUND;
160 else { 151 else {
161 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, 152 status = efi_call_early(allocate_pages,
162 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, 153 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
163 nr_pages, &max_addr); 154 nr_pages, &max_addr);
164 if (status != EFI_SUCCESS) { 155 if (status != EFI_SUCCESS) {
@@ -170,8 +161,7 @@ again:
170 *addr = max_addr; 161 *addr = max_addr;
171 } 162 }
172 163
173 efi_call_phys1(sys_table_arg->boottime->free_pool, map); 164 efi_call_early(free_pool, map);
174
175fail: 165fail:
176 return status; 166 return status;
177} 167}
@@ -231,7 +221,7 @@ static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
231 if ((start + size) > end) 221 if ((start + size) > end)
232 continue; 222 continue;
233 223
234 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, 224 status = efi_call_early(allocate_pages,
235 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, 225 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
236 nr_pages, &start); 226 nr_pages, &start);
237 if (status == EFI_SUCCESS) { 227 if (status == EFI_SUCCESS) {
@@ -243,7 +233,7 @@ static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
243 if (i == map_size / desc_size) 233 if (i == map_size / desc_size)
244 status = EFI_NOT_FOUND; 234 status = EFI_NOT_FOUND;
245 235
246 efi_call_phys1(sys_table_arg->boottime->free_pool, map); 236 efi_call_early(free_pool, map);
247fail: 237fail:
248 return status; 238 return status;
249} 239}
@@ -257,7 +247,7 @@ static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
257 return; 247 return;
258 248
259 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; 249 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
260 efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages); 250 efi_call_early(free_pages, addr, nr_pages);
261} 251}
262 252
263 253
@@ -276,9 +266,7 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
276{ 266{
277 struct file_info *files; 267 struct file_info *files;
278 unsigned long file_addr; 268 unsigned long file_addr;
279 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
280 u64 file_size_total; 269 u64 file_size_total;
281 efi_file_io_interface_t *io;
282 efi_file_handle_t *fh; 270 efi_file_handle_t *fh;
283 efi_status_t status; 271 efi_status_t status;
284 int nr_files; 272 int nr_files;
@@ -319,10 +307,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
319 if (!nr_files) 307 if (!nr_files)
320 return EFI_SUCCESS; 308 return EFI_SUCCESS;
321 309
322 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool, 310 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
323 EFI_LOADER_DATA, 311 nr_files * sizeof(*files), (void **)&files);
324 nr_files * sizeof(*files),
325 (void **)&files);
326 if (status != EFI_SUCCESS) { 312 if (status != EFI_SUCCESS) {
327 efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n"); 313 efi_printk(sys_table_arg, "Failed to alloc mem for file handle list\n");
328 goto fail; 314 goto fail;
@@ -331,13 +317,8 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
331 str = cmd_line; 317 str = cmd_line;
332 for (i = 0; i < nr_files; i++) { 318 for (i = 0; i < nr_files; i++) {
333 struct file_info *file; 319 struct file_info *file;
334 efi_file_handle_t *h;
335 efi_file_info_t *info;
336 efi_char16_t filename_16[256]; 320 efi_char16_t filename_16[256];
337 unsigned long info_sz;
338 efi_guid_t info_guid = EFI_FILE_INFO_ID;
339 efi_char16_t *p; 321 efi_char16_t *p;
340 u64 file_sz;
341 322
342 str = strstr(str, option_string); 323 str = strstr(str, option_string);
343 if (!str) 324 if (!str)
@@ -368,71 +349,18 @@ static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
368 349
369 /* Only open the volume once. */ 350 /* Only open the volume once. */
370 if (!i) { 351 if (!i) {
371 efi_boot_services_t *boottime; 352 status = efi_open_volume(sys_table_arg, image,
372 353 (void **)&fh);
373 boottime = sys_table_arg->boottime; 354 if (status != EFI_SUCCESS)
374
375 status = efi_call_phys3(boottime->handle_protocol,
376 image->device_handle, &fs_proto,
377 (void **)&io);
378 if (status != EFI_SUCCESS) {
379 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
380 goto free_files;
381 }
382
383 status = efi_call_phys2(io->open_volume, io, &fh);
384 if (status != EFI_SUCCESS) {
385 efi_printk(sys_table_arg, "Failed to open volume\n");
386 goto free_files; 355 goto free_files;
387 }
388 } 356 }
389 357
390 status = efi_call_phys5(fh->open, fh, &h, filename_16, 358 status = efi_file_size(sys_table_arg, fh, filename_16,
391 EFI_FILE_MODE_READ, (u64)0); 359 (void **)&file->handle, &file->size);
392 if (status != EFI_SUCCESS) { 360 if (status != EFI_SUCCESS)
393 efi_printk(sys_table_arg, "Failed to open file: ");
394 efi_char16_printk(sys_table_arg, filename_16);
395 efi_printk(sys_table_arg, "\n");
396 goto close_handles; 361 goto close_handles;
397 }
398 362
399 file->handle = h; 363 file_size_total += file->size;
400
401 info_sz = 0;
402 status = efi_call_phys4(h->get_info, h, &info_guid,
403 &info_sz, NULL);
404 if (status != EFI_BUFFER_TOO_SMALL) {
405 efi_printk(sys_table_arg, "Failed to get file info size\n");
406 goto close_handles;
407 }
408
409grow:
410 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
411 EFI_LOADER_DATA, info_sz,
412 (void **)&info);
413 if (status != EFI_SUCCESS) {
414 efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
415 goto close_handles;
416 }
417
418 status = efi_call_phys4(h->get_info, h, &info_guid,
419 &info_sz, info);
420 if (status == EFI_BUFFER_TOO_SMALL) {
421 efi_call_phys1(sys_table_arg->boottime->free_pool,
422 info);
423 goto grow;
424 }
425
426 file_sz = info->file_size;
427 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
428
429 if (status != EFI_SUCCESS) {
430 efi_printk(sys_table_arg, "Failed to get file info\n");
431 goto close_handles;
432 }
433
434 file->size = file_sz;
435 file_size_total += file_sz;
436 } 364 }
437 365
438 if (file_size_total) { 366 if (file_size_total) {
@@ -468,10 +396,10 @@ grow:
468 chunksize = EFI_READ_CHUNK_SIZE; 396 chunksize = EFI_READ_CHUNK_SIZE;
469 else 397 else
470 chunksize = size; 398 chunksize = size;
471 status = efi_call_phys3(fh->read, 399
472 files[j].handle, 400 status = efi_file_read(fh, files[j].handle,
473 &chunksize, 401 &chunksize,
474 (void *)addr); 402 (void *)addr);
475 if (status != EFI_SUCCESS) { 403 if (status != EFI_SUCCESS) {
476 efi_printk(sys_table_arg, "Failed to read file\n"); 404 efi_printk(sys_table_arg, "Failed to read file\n");
477 goto free_file_total; 405 goto free_file_total;
@@ -480,12 +408,12 @@ grow:
480 size -= chunksize; 408 size -= chunksize;
481 } 409 }
482 410
483 efi_call_phys1(fh->close, files[j].handle); 411 efi_file_close(fh, files[j].handle);
484 } 412 }
485 413
486 } 414 }
487 415
488 efi_call_phys1(sys_table_arg->boottime->free_pool, files); 416 efi_call_early(free_pool, files);
489 417
490 *load_addr = file_addr; 418 *load_addr = file_addr;
491 *load_size = file_size_total; 419 *load_size = file_size_total;
@@ -497,9 +425,9 @@ free_file_total:
497 425
498close_handles: 426close_handles:
499 for (k = j; k < i; k++) 427 for (k = j; k < i; k++)
500 efi_call_phys1(fh->close, files[k].handle); 428 efi_file_close(fh, files[k].handle);
501free_files: 429free_files:
502 efi_call_phys1(sys_table_arg->boottime->free_pool, files); 430 efi_call_early(free_pool, files);
503fail: 431fail:
504 *load_addr = 0; 432 *load_addr = 0;
505 *load_size = 0; 433 *load_size = 0;
@@ -545,7 +473,7 @@ static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
545 * as possible while respecting the required alignment. 473 * as possible while respecting the required alignment.
546 */ 474 */
547 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE; 475 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
548 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages, 476 status = efi_call_early(allocate_pages,
549 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA, 477 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
550 nr_pages, &efi_addr); 478 nr_pages, &efi_addr);
551 new_addr = efi_addr; 479 new_addr = efi_addr;
diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
index 4753bac65279..af20f1712337 100644
--- a/drivers/firmware/efi/efi.c
+++ b/drivers/firmware/efi/efi.c
@@ -233,7 +233,7 @@ static __initdata efi_config_table_type_t common_tables[] = {
233 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab}, 233 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
234 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios}, 234 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
235 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga}, 235 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
236 {NULL_GUID, NULL, 0}, 236 {NULL_GUID, NULL, NULL},
237}; 237};
238 238
239static __init int match_config_table(efi_guid_t *guid, 239static __init int match_config_table(efi_guid_t *guid,
@@ -313,5 +313,8 @@ int __init efi_config_init(efi_config_table_type_t *arch_tables)
313 } 313 }
314 pr_cont("\n"); 314 pr_cont("\n");
315 early_iounmap(config_tables, efi.systab->nr_tables * sz); 315 early_iounmap(config_tables, efi.systab->nr_tables * sz);
316
317 set_bit(EFI_CONFIG_TABLES, &efi.flags);
318
316 return 0; 319 return 0;
317} 320}
diff --git a/drivers/firmware/efi/efivars.c b/drivers/firmware/efi/efivars.c
index 3dc248239197..50ea412a25e6 100644
--- a/drivers/firmware/efi/efivars.c
+++ b/drivers/firmware/efi/efivars.c
@@ -227,7 +227,7 @@ efivar_store_raw(struct efivar_entry *entry, const char *buf, size_t count)
227 memcpy(&entry->var, new_var, count); 227 memcpy(&entry->var, new_var, count);
228 228
229 err = efivar_entry_set(entry, new_var->Attributes, 229 err = efivar_entry_set(entry, new_var->Attributes,
230 new_var->DataSize, new_var->Data, false); 230 new_var->DataSize, new_var->Data, NULL);
231 if (err) { 231 if (err) {
232 printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err); 232 printk(KERN_WARNING "efivars: set_variable() failed: status=%d\n", err);
233 return -EIO; 233 return -EIO;
diff --git a/fs/efivarfs/file.c b/fs/efivarfs/file.c
index 8dd524f32284..cdb2971192a5 100644
--- a/fs/efivarfs/file.c
+++ b/fs/efivarfs/file.c
@@ -21,7 +21,7 @@ static ssize_t efivarfs_file_write(struct file *file,
21 u32 attributes; 21 u32 attributes;
22 struct inode *inode = file->f_mapping->host; 22 struct inode *inode = file->f_mapping->host;
23 unsigned long datasize = count - sizeof(attributes); 23 unsigned long datasize = count - sizeof(attributes);
24 ssize_t bytes = 0; 24 ssize_t bytes;
25 bool set = false; 25 bool set = false;
26 26
27 if (count < sizeof(attributes)) 27 if (count < sizeof(attributes))
@@ -33,14 +33,9 @@ static ssize_t efivarfs_file_write(struct file *file,
33 if (attributes & ~(EFI_VARIABLE_MASK)) 33 if (attributes & ~(EFI_VARIABLE_MASK))
34 return -EINVAL; 34 return -EINVAL;
35 35
36 data = kmalloc(datasize, GFP_KERNEL); 36 data = memdup_user(userbuf + sizeof(attributes), datasize);
37 if (!data) 37 if (IS_ERR(data))
38 return -ENOMEM; 38 return PTR_ERR(data);
39
40 if (copy_from_user(data, userbuf + sizeof(attributes), datasize)) {
41 bytes = -EFAULT;
42 goto out;
43 }
44 39
45 bytes = efivar_entry_set_get_size(var, attributes, &datasize, 40 bytes = efivar_entry_set_get_size(var, attributes, &datasize,
46 data, &set); 41 data, &set);
diff --git a/include/linux/efi.h b/include/linux/efi.h
index 0a819e7a60c9..6c100ff0cae4 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -153,6 +153,102 @@ typedef struct {
153 u8 sets_to_zero; 153 u8 sets_to_zero;
154} efi_time_cap_t; 154} efi_time_cap_t;
155 155
156typedef struct {
157 efi_table_hdr_t hdr;
158 u32 raise_tpl;
159 u32 restore_tpl;
160 u32 allocate_pages;
161 u32 free_pages;
162 u32 get_memory_map;
163 u32 allocate_pool;
164 u32 free_pool;
165 u32 create_event;
166 u32 set_timer;
167 u32 wait_for_event;
168 u32 signal_event;
169 u32 close_event;
170 u32 check_event;
171 u32 install_protocol_interface;
172 u32 reinstall_protocol_interface;
173 u32 uninstall_protocol_interface;
174 u32 handle_protocol;
175 u32 __reserved;
176 u32 register_protocol_notify;
177 u32 locate_handle;
178 u32 locate_device_path;
179 u32 install_configuration_table;
180 u32 load_image;
181 u32 start_image;
182 u32 exit;
183 u32 unload_image;
184 u32 exit_boot_services;
185 u32 get_next_monotonic_count;
186 u32 stall;
187 u32 set_watchdog_timer;
188 u32 connect_controller;
189 u32 disconnect_controller;
190 u32 open_protocol;
191 u32 close_protocol;
192 u32 open_protocol_information;
193 u32 protocols_per_handle;
194 u32 locate_handle_buffer;
195 u32 locate_protocol;
196 u32 install_multiple_protocol_interfaces;
197 u32 uninstall_multiple_protocol_interfaces;
198 u32 calculate_crc32;
199 u32 copy_mem;
200 u32 set_mem;
201 u32 create_event_ex;
202} __packed efi_boot_services_32_t;
203
204typedef struct {
205 efi_table_hdr_t hdr;
206 u64 raise_tpl;
207 u64 restore_tpl;
208 u64 allocate_pages;
209 u64 free_pages;
210 u64 get_memory_map;
211 u64 allocate_pool;
212 u64 free_pool;
213 u64 create_event;
214 u64 set_timer;
215 u64 wait_for_event;
216 u64 signal_event;
217 u64 close_event;
218 u64 check_event;
219 u64 install_protocol_interface;
220 u64 reinstall_protocol_interface;
221 u64 uninstall_protocol_interface;
222 u64 handle_protocol;
223 u64 __reserved;
224 u64 register_protocol_notify;
225 u64 locate_handle;
226 u64 locate_device_path;
227 u64 install_configuration_table;
228 u64 load_image;
229 u64 start_image;
230 u64 exit;
231 u64 unload_image;
232 u64 exit_boot_services;
233 u64 get_next_monotonic_count;
234 u64 stall;
235 u64 set_watchdog_timer;
236 u64 connect_controller;
237 u64 disconnect_controller;
238 u64 open_protocol;
239 u64 close_protocol;
240 u64 open_protocol_information;
241 u64 protocols_per_handle;
242 u64 locate_handle_buffer;
243 u64 locate_protocol;
244 u64 install_multiple_protocol_interfaces;
245 u64 uninstall_multiple_protocol_interfaces;
246 u64 calculate_crc32;
247 u64 copy_mem;
248 u64 set_mem;
249 u64 create_event_ex;
250} __packed efi_boot_services_64_t;
251
156/* 252/*
157 * EFI Boot Services table 253 * EFI Boot Services table
158 */ 254 */
@@ -231,6 +327,15 @@ typedef enum {
231 EfiPciIoAttributeOperationMaximum 327 EfiPciIoAttributeOperationMaximum
232} EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION; 328} EFI_PCI_IO_PROTOCOL_ATTRIBUTE_OPERATION;
233 329
330typedef struct {
331 u32 read;
332 u32 write;
333} efi_pci_io_protocol_access_32_t;
334
335typedef struct {
336 u64 read;
337 u64 write;
338} efi_pci_io_protocol_access_64_t;
234 339
235typedef struct { 340typedef struct {
236 void *read; 341 void *read;
@@ -238,6 +343,46 @@ typedef struct {
238} efi_pci_io_protocol_access_t; 343} efi_pci_io_protocol_access_t;
239 344
240typedef struct { 345typedef struct {
346 u32 poll_mem;
347 u32 poll_io;
348 efi_pci_io_protocol_access_32_t mem;
349 efi_pci_io_protocol_access_32_t io;
350 efi_pci_io_protocol_access_32_t pci;
351 u32 copy_mem;
352 u32 map;
353 u32 unmap;
354 u32 allocate_buffer;
355 u32 free_buffer;
356 u32 flush;
357 u32 get_location;
358 u32 attributes;
359 u32 get_bar_attributes;
360 u32 set_bar_attributes;
361 uint64_t romsize;
362 void *romimage;
363} efi_pci_io_protocol_32;
364
365typedef struct {
366 u64 poll_mem;
367 u64 poll_io;
368 efi_pci_io_protocol_access_64_t mem;
369 efi_pci_io_protocol_access_64_t io;
370 efi_pci_io_protocol_access_64_t pci;
371 u64 copy_mem;
372 u64 map;
373 u64 unmap;
374 u64 allocate_buffer;
375 u64 free_buffer;
376 u64 flush;
377 u64 get_location;
378 u64 attributes;
379 u64 get_bar_attributes;
380 u64 set_bar_attributes;
381 uint64_t romsize;
382 void *romimage;
383} efi_pci_io_protocol_64;
384
385typedef struct {
241 void *poll_mem; 386 void *poll_mem;
242 void *poll_io; 387 void *poll_io;
243 efi_pci_io_protocol_access_t mem; 388 efi_pci_io_protocol_access_t mem;
@@ -292,6 +437,42 @@ typedef struct {
292 437
293typedef struct { 438typedef struct {
294 efi_table_hdr_t hdr; 439 efi_table_hdr_t hdr;
440 u32 get_time;
441 u32 set_time;
442 u32 get_wakeup_time;
443 u32 set_wakeup_time;
444 u32 set_virtual_address_map;
445 u32 convert_pointer;
446 u32 get_variable;
447 u32 get_next_variable;
448 u32 set_variable;
449 u32 get_next_high_mono_count;
450 u32 reset_system;
451 u32 update_capsule;
452 u32 query_capsule_caps;
453 u32 query_variable_info;
454} efi_runtime_services_32_t;
455
456typedef struct {
457 efi_table_hdr_t hdr;
458 u64 get_time;
459 u64 set_time;
460 u64 get_wakeup_time;
461 u64 set_wakeup_time;
462 u64 set_virtual_address_map;
463 u64 convert_pointer;
464 u64 get_variable;
465 u64 get_next_variable;
466 u64 set_variable;
467 u64 get_next_high_mono_count;
468 u64 reset_system;
469 u64 update_capsule;
470 u64 query_capsule_caps;
471 u64 query_variable_info;
472} efi_runtime_services_64_t;
473
474typedef struct {
475 efi_table_hdr_t hdr;
295 void *get_time; 476 void *get_time;
296 void *set_time; 477 void *set_time;
297 void *get_wakeup_time; 478 void *get_wakeup_time;
@@ -485,6 +666,38 @@ struct efi_memory_map {
485 666
486typedef struct { 667typedef struct {
487 u32 revision; 668 u32 revision;
669 u32 parent_handle;
670 u32 system_table;
671 u32 device_handle;
672 u32 file_path;
673 u32 reserved;
674 u32 load_options_size;
675 u32 load_options;
676 u32 image_base;
677 __aligned_u64 image_size;
678 unsigned int image_code_type;
679 unsigned int image_data_type;
680 unsigned long unload;
681} efi_loaded_image_32_t;
682
683typedef struct {
684 u32 revision;
685 u64 parent_handle;
686 u64 system_table;
687 u64 device_handle;
688 u64 file_path;
689 u64 reserved;
690 u32 load_options_size;
691 u64 load_options;
692 u64 image_base;
693 __aligned_u64 image_size;
694 unsigned int image_code_type;
695 unsigned int image_data_type;
696 unsigned long unload;
697} efi_loaded_image_64_t;
698
699typedef struct {
700 u32 revision;
488 void *parent_handle; 701 void *parent_handle;
489 efi_system_table_t *system_table; 702 efi_system_table_t *system_table;
490 void *device_handle; 703 void *device_handle;
@@ -511,6 +724,34 @@ typedef struct {
511 efi_char16_t filename[1]; 724 efi_char16_t filename[1];
512} efi_file_info_t; 725} efi_file_info_t;
513 726
727typedef struct {
728 u64 revision;
729 u32 open;
730 u32 close;
731 u32 delete;
732 u32 read;
733 u32 write;
734 u32 get_position;
735 u32 set_position;
736 u32 get_info;
737 u32 set_info;
738 u32 flush;
739} efi_file_handle_32_t;
740
741typedef struct {
742 u64 revision;
743 u64 open;
744 u64 close;
745 u64 delete;
746 u64 read;
747 u64 write;
748 u64 get_position;
749 u64 set_position;
750 u64 get_info;
751 u64 set_info;
752 u64 flush;
753} efi_file_handle_64_t;
754
514typedef struct _efi_file_handle { 755typedef struct _efi_file_handle {
515 u64 revision; 756 u64 revision;
516 efi_status_t (*open)(struct _efi_file_handle *, 757 efi_status_t (*open)(struct _efi_file_handle *,
@@ -573,6 +814,7 @@ extern struct efi {
573 efi_reset_system_t *reset_system; 814 efi_reset_system_t *reset_system;
574 efi_set_virtual_address_map_t *set_virtual_address_map; 815 efi_set_virtual_address_map_t *set_virtual_address_map;
575 struct efi_memory_map *memmap; 816 struct efi_memory_map *memmap;
817 unsigned long flags;
576} efi; 818} efi;
577 819
578static inline int 820static inline int
@@ -659,18 +901,17 @@ extern int __init efi_setup_pcdp_console(char *);
659#define EFI_ARCH_1 6 /* First arch-specific bit */ 901#define EFI_ARCH_1 6 /* First arch-specific bit */
660 902
661#ifdef CONFIG_EFI 903#ifdef CONFIG_EFI
662# ifdef CONFIG_X86 904/*
663extern int efi_enabled(int facility); 905 * Test whether the above EFI_* bits are enabled.
664# else 906 */
665static inline int efi_enabled(int facility) 907static inline bool efi_enabled(int feature)
666{ 908{
667 return 1; 909 return test_bit(feature, &efi.flags) != 0;
668} 910}
669# endif
670#else 911#else
671static inline int efi_enabled(int facility) 912static inline bool efi_enabled(int feature)
672{ 913{
673 return 0; 914 return false;
674} 915}
675#endif 916#endif
676 917
@@ -809,6 +1050,17 @@ struct efivar_entry {
809 bool deleting; 1050 bool deleting;
810}; 1051};
811 1052
1053struct efi_simple_text_output_protocol_32 {
1054 u32 reset;
1055 u32 output_string;
1056 u32 test_string;
1057};
1058
1059struct efi_simple_text_output_protocol_64 {
1060 u64 reset;
1061 u64 output_string;
1062 u64 test_string;
1063};
812 1064
813struct efi_simple_text_output_protocol { 1065struct efi_simple_text_output_protocol {
814 void *reset; 1066 void *reset;