Skip to content

Commit 6a2c20e

Browse files
rhvgoyaltorvalds
authored andcommitted
kexec: support kexec/kdump on EFI systems
This patch does two things. It passes EFI run time mappings to second kernel in bootparams efi_info. Second kernel parse this info and create new mappings in second kernel. That means mappings in first and second kernel will be same. This paves the way to enable EFI in kexec kernel. This patch also prepares and passes EFI setup data through bootparams. This contains bunch of information about various tables and their addresses. These information gathering and passing has been written along the lines of what current kexec-tools is doing to make kexec work with UEFI. [akpm@linux-foundation.org: s/get_efi/efi_get/g, per Matt] Signed-off-by: Vivek Goyal <vgoyal@redhat.com> Cc: Borislav Petkov <bp@suse.de> Cc: Michael Kerrisk <mtk.manpages@gmail.com> Cc: Yinghai Lu <yinghai@kernel.org> Cc: Eric Biederman <ebiederm@xmission.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Matthew Garrett <mjg59@srcf.ucam.org> Cc: Greg Kroah-Hartman <greg@kroah.com> Cc: Dave Young <dyoung@redhat.com> Cc: WANG Chao <chaowang@redhat.com> Cc: Baoquan He <bhe@redhat.com> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Matt Fleming <matt@console-pimps.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent dd5f726 commit 6a2c20e

3 files changed

Lines changed: 174 additions & 12 deletions

File tree

‎arch/x86/kernel/kexec-bzimage64.c‎

Lines changed: 134 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
#include <linux/kexec.h>
1919
#include <linux/kernel.h>
2020
#include <linux/mm.h>
21+
#include <linux/efi.h>
2122

2223
#include <asm/bootparam.h>
2324
#include <asm/setup.h>
2425
#include <asm/crash.h>
26+
#include <asm/efi.h>
2527

2628
#define MAX_ELFCOREHDR_STR_LEN 30 /* elfcorehdr=0x<64bit-value> */
2729

@@ -90,7 +92,7 @@ static int setup_cmdline(struct kimage *image, struct boot_params *params,
9092
return 0;
9193
}
9294

93-
static int setup_memory_map_entries(struct boot_params *params)
95+
static int setup_e820_entries(struct boot_params *params)
9496
{
9597
unsigned int nr_e820_entries;
9698

@@ -107,8 +109,93 @@ static int setup_memory_map_entries(struct boot_params *params)
107109
return 0;
108110
}
109111

110-
static int setup_boot_parameters(struct kimage *image,
111-
struct boot_params *params)
112+
#ifdef CONFIG_EFI
113+
static int setup_efi_info_memmap(struct boot_params *params,
114+
unsigned long params_load_addr,
115+
unsigned int efi_map_offset,
116+
unsigned int efi_map_sz)
117+
{
118+
void *efi_map = (void *)params + efi_map_offset;
119+
unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
120+
struct efi_info *ei = &params->efi_info;
121+
122+
if (!efi_map_sz)
123+
return 0;
124+
125+
efi_runtime_map_copy(efi_map, efi_map_sz);
126+
127+
ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
128+
ei->efi_memmap_hi = efi_map_phys_addr >> 32;
129+
ei->efi_memmap_size = efi_map_sz;
130+
131+
return 0;
132+
}
133+
134+
static int
135+
prepare_add_efi_setup_data(struct boot_params *params,
136+
unsigned long params_load_addr,
137+
unsigned int efi_setup_data_offset)
138+
{
139+
unsigned long setup_data_phys;
140+
struct setup_data *sd = (void *)params + efi_setup_data_offset;
141+
struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
142+
143+
esd->fw_vendor = efi.fw_vendor;
144+
esd->runtime = efi.runtime;
145+
esd->tables = efi.config_table;
146+
esd->smbios = efi.smbios;
147+
148+
sd->type = SETUP_EFI;
149+
sd->len = sizeof(struct efi_setup_data);
150+
151+
/* Add setup data */
152+
setup_data_phys = params_load_addr + efi_setup_data_offset;
153+
sd->next = params->hdr.setup_data;
154+
params->hdr.setup_data = setup_data_phys;
155+
156+
return 0;
157+
}
158+
159+
static int
160+
setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
161+
unsigned int efi_map_offset, unsigned int efi_map_sz,
162+
unsigned int efi_setup_data_offset)
163+
{
164+
struct efi_info *current_ei = &boot_params.efi_info;
165+
struct efi_info *ei = &params->efi_info;
166+
167+
if (!current_ei->efi_memmap_size)
168+
return 0;
169+
170+
/*
171+
* If 1:1 mapping is not enabled, second kernel can not setup EFI
172+
* and use EFI run time services. User space will have to pass
173+
* acpi_rsdp=<addr> on kernel command line to make second kernel boot
174+
* without efi.
175+
*/
176+
if (efi_enabled(EFI_OLD_MEMMAP))
177+
return 0;
178+
179+
ei->efi_loader_signature = current_ei->efi_loader_signature;
180+
ei->efi_systab = current_ei->efi_systab;
181+
ei->efi_systab_hi = current_ei->efi_systab_hi;
182+
183+
ei->efi_memdesc_version = current_ei->efi_memdesc_version;
184+
ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
185+
186+
setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
187+
efi_map_sz);
188+
prepare_add_efi_setup_data(params, params_load_addr,
189+
efi_setup_data_offset);
190+
return 0;
191+
}
192+
#endif /* CONFIG_EFI */
193+
194+
static int
195+
setup_boot_parameters(struct kimage *image, struct boot_params *params,
196+
unsigned long params_load_addr,
197+
unsigned int efi_map_offset, unsigned int efi_map_sz,
198+
unsigned int efi_setup_data_offset)
112199
{
113200
unsigned int nr_e820_entries;
114201
unsigned long long mem_k, start, end;
@@ -140,7 +227,7 @@ static int setup_boot_parameters(struct kimage *image,
140227
if (ret)
141228
return ret;
142229
} else
143-
setup_memory_map_entries(params);
230+
setup_e820_entries(params);
144231

145232
nr_e820_entries = params->e820_entries;
146233

@@ -161,6 +248,12 @@ static int setup_boot_parameters(struct kimage *image,
161248
}
162249
}
163250

251+
#ifdef CONFIG_EFI
252+
/* Setup EFI state */
253+
setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
254+
efi_setup_data_offset);
255+
#endif
256+
164257
/* Setup EDD info */
165258
memcpy(params->eddbuf, boot_params.eddbuf,
166259
EDDMAXNR * sizeof(struct edd_info));
@@ -214,6 +307,15 @@ int bzImage64_probe(const char *buf, unsigned long len)
214307
return ret;
215308
}
216309

310+
/*
311+
* Can't handle 32bit EFI as it does not allow loading kernel
312+
* above 4G. This should be handled by 32bit bzImage loader
313+
*/
314+
if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
315+
pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
316+
return ret;
317+
}
318+
217319
/* I've got a bzImage */
218320
pr_debug("It's a relocatable bzImage64\n");
219321
ret = 0;
@@ -229,7 +331,7 @@ void *bzImage64_load(struct kimage *image, char *kernel,
229331

230332
struct setup_header *header;
231333
int setup_sects, kern16_size, ret = 0;
232-
unsigned long setup_header_size, params_cmdline_sz;
334+
unsigned long setup_header_size, params_cmdline_sz, params_misc_sz;
233335
struct boot_params *params;
234336
unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
235337
unsigned long purgatory_load_addr;
@@ -239,6 +341,7 @@ void *bzImage64_load(struct kimage *image, char *kernel,
239341
struct kexec_entry64_regs regs64;
240342
void *stack;
241343
unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
344+
unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
242345

243346
header = (struct setup_header *)(kernel + setup_hdr_offset);
244347
setup_sects = header->setup_sects;
@@ -285,26 +388,43 @@ void *bzImage64_load(struct kimage *image, char *kernel,
285388

286389
pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr);
287390

288-
/* Load Bootparams and cmdline */
391+
392+
/*
393+
* Load Bootparams and cmdline and space for efi stuff.
394+
*
395+
* Allocate memory together for multiple data structures so
396+
* that they all can go in single area/segment and we don't
397+
* have to create separate segment for each. Keeps things
398+
* little bit simple
399+
*/
400+
efi_map_sz = efi_get_runtime_map_size();
401+
efi_map_sz = ALIGN(efi_map_sz, 16);
289402
params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
290403
MAX_ELFCOREHDR_STR_LEN;
291-
params = kzalloc(params_cmdline_sz, GFP_KERNEL);
404+
params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
405+
params_misc_sz = params_cmdline_sz + efi_map_sz +
406+
sizeof(struct setup_data) +
407+
sizeof(struct efi_setup_data);
408+
409+
params = kzalloc(params_misc_sz, GFP_KERNEL);
292410
if (!params)
293411
return ERR_PTR(-ENOMEM);
412+
efi_map_offset = params_cmdline_sz;
413+
efi_setup_data_offset = efi_map_offset + efi_map_sz;
294414

295415
/* Copy setup header onto bootparams. Documentation/x86/boot.txt */
296416
setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
297417

298418
/* Is there a limit on setup header size? */
299419
memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
300420

301-
ret = kexec_add_buffer(image, (char *)params, params_cmdline_sz,
302-
params_cmdline_sz, 16, MIN_BOOTPARAM_ADDR,
421+
ret = kexec_add_buffer(image, (char *)params, params_misc_sz,
422+
params_misc_sz, 16, MIN_BOOTPARAM_ADDR,
303423
ULONG_MAX, 1, &bootparam_load_addr);
304424
if (ret)
305425
goto out_free_params;
306-
pr_debug("Loaded boot_param and command line at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
307-
bootparam_load_addr, params_cmdline_sz, params_cmdline_sz);
426+
pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
427+
bootparam_load_addr, params_misc_sz, params_misc_sz);
308428

309429
/* Load kernel */
310430
kernel_buf = kernel + kern16_size;
@@ -365,7 +485,9 @@ void *bzImage64_load(struct kimage *image, char *kernel,
365485
if (ret)
366486
goto out_free_params;
367487

368-
ret = setup_boot_parameters(image, params);
488+
ret = setup_boot_parameters(image, params, bootparam_load_addr,
489+
efi_map_offset, efi_map_sz,
490+
efi_setup_data_offset);
369491
if (ret)
370492
goto out_free_params;
371493

‎drivers/firmware/efi/runtime-map.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,27 @@ add_sysfs_runtime_map_entry(struct kobject *kobj, int nr)
138138
return entry;
139139
}
140140

141+
int efi_get_runtime_map_size(void)
142+
{
143+
return nr_efi_runtime_map * efi_memdesc_size;
144+
}
145+
146+
int efi_get_runtime_map_desc_size(void)
147+
{
148+
return efi_memdesc_size;
149+
}
150+
151+
int efi_runtime_map_copy(void *buf, size_t bufsz)
152+
{
153+
size_t sz = efi_get_runtime_map_size();
154+
155+
if (sz > bufsz)
156+
sz = bufsz;
157+
158+
memcpy(buf, efi_runtime_map, sz);
159+
return 0;
160+
}
161+
141162
void efi_runtime_map_setup(void *map, int nr_entries, u32 desc_size)
142163
{
143164
efi_runtime_map = map;

‎include/linux/efi.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,9 @@ int efivars_sysfs_init(void);
11561156
#ifdef CONFIG_EFI_RUNTIME_MAP
11571157
int efi_runtime_map_init(struct kobject *);
11581158
void efi_runtime_map_setup(void *, int, u32);
1159+
int efi_get_runtime_map_size(void);
1160+
int efi_get_runtime_map_desc_size(void);
1161+
int efi_runtime_map_copy(void *buf, size_t bufsz);
11591162
#else
11601163
static inline int efi_runtime_map_init(struct kobject *kobj)
11611164
{
@@ -1164,6 +1167,22 @@ static inline int efi_runtime_map_init(struct kobject *kobj)
11641167

11651168
static inline void
11661169
efi_runtime_map_setup(void *map, int nr_entries, u32 desc_size) {}
1170+
1171+
static inline int efi_get_runtime_map_size(void)
1172+
{
1173+
return 0;
1174+
}
1175+
1176+
static inline int efi_get_runtime_map_desc_size(void)
1177+
{
1178+
return 0;
1179+
}
1180+
1181+
static inline int efi_runtime_map_copy(void *buf, size_t bufsz)
1182+
{
1183+
return 0;
1184+
}
1185+
11671186
#endif
11681187

11691188
/* prototypes shared between arch specific and generic stub code */

0 commit comments

Comments
 (0)