kexec_file_load implementation for PowerPC
Subsystems (5)
v5 cover kexec_file_load implementation for PowerPC
[ Andrew, since this series touches generic code, x86 and powerpc,
Michael Ellerman and Dave Young think it should go via your tree. ]
The main differences in this version are (more detailed changelog at the end of this email):
- The code which is not specific to loading ELF format kernels were moved from kexec_elf_64.c to machine_kexec_64.c.
- There is a new patch implementing support for receiving a device tree blob from userspace, checking it against a whitelist of allowed nodes and properties and copying it into the device tree for the next kernel. This is the only patch that depends on the "extend kexec_file_load system call" series. Everything else can be upstreamed independently of that series.
- Also, I realised that the patch "Add support for loading ELF kernels with kexec_file_load." was too big, so I moved some changes to other patches to facilitate review. Details of what went where are in the changelog.
Original cover letter:
This patch series implements the kexec_file_load system call on PowerPC.
This system call moves the reading of the kernel, initrd and the device tree from the userspace kexec tool to the kernel. This is needed if you want to do one or both of the following:
1. only allow loading of signed kernels.
2. "measure" (i.e., record the hashes of) the kernel, initrd, kernel command line and other boot inputs for the Integrity Measurement Architecture subsystem.
The above are the functions kexec already has built into kexec_file_load. Yesterday I posted a set of patches which allows a third feature:
3. have IMA pass-on its event log (where integrity measurements are registered) accross kexec to the second kernel, so that the event history is preserved.
Because OpenPower uses an intermediary Linux instance as a boot loader (skiroot), feature 1 is needed to implement secure boot for the platform, while features 2 and 3 are needed to implement trusted boot.
This patch series starts by removing an x86 assumption from kexec_file:kexec_add_buffer uses iomem to find reserved memory ranges, but PowerPC uses the memblock subsystem. A hook is added so that each arch can specify how memory ranges can be found.
Also, the memory-walking logic in kexec_add_buffer is useful in this implementation to find a free area for the purgatory's stack, so the next patch moves that logic to kexec_locate_mem_hole.
The kexec_file_load system call needs to apply relocations to the purgatory but adding code for that would duplicate functionality with the module loading mechanism, which also needs to apply relocations to the kernel modules. Therefore, this patch series factors out the module relocation code so that it can be shared.
One thing that is still missing is crashkernel support, which I intend to submit shortly. For now, arch_kexec_kernel_image_probe rejects crash kernels.
This code is based on kexec-tools, but with many modifications to adapt it to the kernel environment and facilities. Except the purgatory, which only has minimal changes.
Changes for v5:
- Rebased series on v4.8-rc1 + the extend kexec_file_load series.
- Patch "powerpc: Adapt elf64_apply_relocate_add for kexec_file_load."
- New patch. These changes were previously in patch 10.
The code itself is unchanged from v4.
- Patch "powerpc: Implement kexec_file_load."
- Moved arch_kexec_walk_mem, arch_kexec_apply_relocations_add and setup_purgatory from patch 10 to this patch.
- arch_kexec_apply_relocations_add is unchanged from v4.
- Fixed off-by-one error in arch_kexec_walk_mem when passing range to func.
- Moved setup_purgatory from kexec_elf_64.c to machine_kexec_64.c, and changed it to receive a pointer to the slave code directly rather than a struct elf_info and getting the pointer from there.
- Patch "powerpc: Add code to work with device trees in kexec_file_load."
- New patch. These changes were previously in patch 10.
- find_debug_console moved from kexec_elf_64.c to machine_kexec_64.c. The code is unchanged from v4.
- setup_new_fdt is a new function factored out of elf64_load. The only code change from v4 is to create /chosen if it doesn't exist yet.
- Patch "powerpc: Add support for loading ELF kernels with kexec_file_load."
- This patch was too big, so moved some of its changes to other patches to facilitate review.
- Allow loading ELF file type ET_DYN, which is what the BE kernel uses.
- The code adapting the device tree for booting the new kernel was moved out of elf64_load to setup_new_fdt.
- Patch "powerpc: Allow userspace to set device tree properties in kexec_file_load"
- New patch.
- The code in this patch didn't exist in v4.
- This is the only patch that depends on the extend kexec_file_load series.
- Patch "powerpc: Enable CONFIG_KEXEC_FILE in powerpc server defconfigs."
- New patch.
Changes for v4:
- Rebased series on today's powerpc/next.
- Patch "kexec_file: Remove unused members from struct kexec_buf.":
- Dropped from the series.
- Patch "kexec_file: Allow arch-specific memory walking function for kexec_add_buffer":
- Changed subject line to be more specific. Was: "kexec_file: Generalize kexec_add_buffer.".
- Changed description to refer to x86 arch instead of Intel arch.
- Moved documentation comments for struct kexec_buf to this patch.
- Patch "kexec_file: Change kexec_add_buffer to take kexec_buf as argument.":
- New patch.
- Patch "kexec_file: Factor out kexec_locate_mem_hole from kexec_add_buffer.":
- Changed kexec_locate_mem_hole to take kexec_buf as argument.
- Improved description of kexec_locate_mem_hole in documentation comment.
Changes for v3:
- Rebased series on today's powerpc/next.
- Patch "kexec_file: Generalize kexec_add_buffer.":
- Removed most arguments from arch_kexec_walk_mem and pass kbuf explicitly.
- Patch "powerpc: Add functions to read ELF files of any endianness.":
- Fixed whitespace issues found by checkpatch.pl.
- Patch "powerpc: Factor out relocation code from module_64.c to elf_util_64.c.":
- Changed to use the new PPC64_ELF_ABI_v2 macro.
- Patch "powerpc: Add support for loading ELF kernels with kexec_file_load.":
- Adapted arch_kexec_walk_mem implementation to changes in its argument list.
- Fixed whitespace and GPL header issues found by checkpatch.pl.
- Patch "powerpc: Add purgatory for kexec_file_load implementation.":
- Fixed whitespace and GPL header issues found by checkpatch.pl.
- Changed to use the new PPC64_ELF_ABI_v2 macro.
Changes for v2:
- All patches: forgot to add Signed-off-by lines in v1, so added them now.
- Patch "kexec_file: Generalize kexec_add_buffer.": broke in two, one adding arch_kexec_walk_mem and the other adding kexec_locate_mem_hole.
- Patch "powerpc: Implement kexec_file_load.":
- Moved relocation changes and the arch_kexec_walk_mem implementation to the next patch in the series.
- Removed pr_fmt from machine_kexec_64.c, since the patch doesn't add any call to pr_debug in that file.
- Changed arch_kexec_kernel_image_probe to reject crash kernels.
Thiago Jung Bauermann (13):
kexec_file: Allow arch-specific memory walking for kexec_add_buffer
kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
kexec_file: Factor out kexec_locate_mem_hole from kexec_add_buffer.
powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
powerpc: Generalize elf64_apply_relocate_add.
powerpc: Adapt elf64_apply_relocate_add for kexec_file_load.
powerpc: Add functions to read ELF files of any endianness.
powerpc: Implement kexec_file_load.
powerpc: Add code to work with device trees in kexec_file_load.
powerpc: Add support for loading ELF kernels with kexec_file_load.
powerpc: Allow userspace to set device tree properties in
kexec_file_load
powerpc: Add purgatory for kexec_file_load implementation.
powerpc: Enable CONFIG_KEXEC_FILE in powerpc server defconfigs.
arch/powerpc/Kconfig | 13 + arch/powerpc/Makefile | 4 + arch/powerpc/configs/powernv_defconfig | 2 + arch/powerpc/configs/ppc64_defconfig | 2 + arch/powerpc/configs/pseries_defconfig | 2 + arch/powerpc/include/asm/elf_util.h | 92 ++++ arch/powerpc/include/asm/kexec.h | 11 + arch/powerpc/include/asm/kexec_elf_64.h | 10 + arch/powerpc/include/asm/module.h | 14 +- arch/powerpc/include/asm/systbl.h | 1 + arch/powerpc/include/asm/unistd.h | 2 +- arch/powerpc/include/uapi/asm/unistd.h | 1 + arch/powerpc/kernel/Makefile | 7 + arch/powerpc/kernel/elf_util.c | 476 ++++++++++++++++++++ arch/powerpc/kernel/elf_util_64.c | 374 ++++++++++++++++ arch/powerpc/kernel/kexec_elf_64.c | 291 ++++++++++++ arch/powerpc/kernel/machine_kexec_64.c | 719 ++++++++++++++++++++++++++++++ arch/powerpc/kernel/module_64.c | 328 ++------------ arch/powerpc/purgatory/.gitignore | 2 + arch/powerpc/purgatory/Makefile | 36 ++ arch/powerpc/purgatory/console-ppc64.c | 38 ++ arch/powerpc/purgatory/crashdump-ppc64.h | 42 ++ arch/powerpc/purgatory/crashdump_backup.c | 36 ++ arch/powerpc/purgatory/crtsavres.S | 5 + arch/powerpc/purgatory/hvCall.S | 27 ++ arch/powerpc/purgatory/hvCall.h | 8 + arch/powerpc/purgatory/kexec-sha256.h | 11 + arch/powerpc/purgatory/ppc64_asm.h | 20 + arch/powerpc/purgatory/printf.c | 164 +++++++ arch/powerpc/purgatory/purgatory-ppc64.c | 41 ++ arch/powerpc/purgatory/purgatory-ppc64.h | 6 + arch/powerpc/purgatory/purgatory.c | 62 +++ arch/powerpc/purgatory/purgatory.h | 11 + arch/powerpc/purgatory/sha256.c | 6 + arch/powerpc/purgatory/sha256.h | 1 + arch/powerpc/purgatory/string.S | 1 + arch/powerpc/purgatory/v2wrap.S | 134 ++++++ arch/x86/kernel/crash.c | 37 +- arch/x86/kernel/kexec-bzimage64.c | 48 +- include/linux/kexec.h | 33 +- kernel/kexec_file.c | 141 +++--- kernel/kexec_internal.h | 16 - 42 files changed, 2869 insertions(+), 406 deletions(-) create mode 100644 arch/powerpc/include/asm/elf_util.h create mode 100644 arch/powerpc/include/asm/kexec_elf_64.h create mode 100644 arch/powerpc/kernel/elf_util.c create mode 100644 arch/powerpc/kernel/elf_util_64.c create mode 100644 arch/powerpc/kernel/kexec_elf_64.c create mode 100644 arch/powerpc/purgatory/.gitignore create mode 100644 arch/powerpc/purgatory/Makefile create mode 100644 arch/powerpc/purgatory/console-ppc64.c create mode 100644 arch/powerpc/purgatory/crashdump-ppc64.h create mode 100644 arch/powerpc/purgatory/crashdump_backup.c create mode 100644 arch/powerpc/purgatory/crtsavres.S create mode 100644 arch/powerpc/purgatory/hvCall.S create mode 100644 arch/powerpc/purgatory/hvCall.h create mode 100644 arch/powerpc/purgatory/kexec-sha256.h create mode 100644 arch/powerpc/purgatory/ppc64_asm.h create mode 100644 arch/powerpc/purgatory/printf.c create mode 100644 arch/powerpc/purgatory/purgatory-ppc64.c create mode 100644 arch/powerpc/purgatory/purgatory-ppc64.h create mode 100644 arch/powerpc/purgatory/purgatory.c create mode 100644 arch/powerpc/purgatory/purgatory.h create mode 100644 arch/powerpc/purgatory/sha256.c create mode 100644 arch/powerpc/purgatory/sha256.h create mode 100644 arch/powerpc/purgatory/string.S create mode 100644 arch/powerpc/purgatory/v2wrap.S
Signature · 1 line
1.9.1
PATCH v5 1/13 kexec_file: Allow arch-specific memory walking for kexec_add_buffer
Allow architectures to specify a different memory walking function for kexec_add_buffer. x86 uses iomem to track reserved memory ranges, but PowerPC uses the memblock subsystem.
--- include/linux/kexec.h | 26 ++++++++++++++++++++++++++ kernel/kexec_file.c | 30 ++++++++++++++++++++++-------- kernel/kexec_internal.h | 16 ---------------- 3 files changed, 48 insertions(+), 24 deletions(-)
include/linux/kexec.h +26 -0
@@ -149,6 +149,32 @@ struct kexec_file_ops { #endif }; +/** +* struct kexec_buf - parameters for finding a place for a buffer in memory +* @image: kexec image in which memory to search. +* @buffer: Contents which will be copied to the allocated memory. +* @bufsz: Size of @buffer. +* @mem: On return will have address of the buffer in memory. +* @memsz: Size for the buffer in memory. +* @buf_align: Minimum alignment needed. +* @buf_min: The buffer can't be placed below this address. +* @buf_max: The buffer can't be placed above this address. +* @top_down: Allocate from top of memory. +*/ +struct kexec_buf { +struct kimage *image; +char *buffer; +unsigned long bufsz; +unsigned long mem; +unsigned long memsz; +unsigned long buf_align; +unsigned long buf_min; +unsigned long buf_max; +bool top_down; +}; + +int __weak arch_kexec_walk_mem (struct kexec_buf *kbuf, +int (*func)(u64 ,u64 ,void *));int __weak arch_kexec_verify_buffer (enum kexec_file_type type,const void *buf,unsigned long size);#endif /* CONFIG_KEXEC_FILE */
kernel/kexec_file.c +22 -8
@@ -502,6 +502,27 @@ static int locate_mem_hole_callback(u64 start, u64 end, void *arg)return locate_mem_hole_bottom_up (start, end, kbuf); } +/** +* arch_kexec_walk_mem - call func(data) on free memory regions +* @kbuf: Context info for the search. Also passed to @func. +* @func: Function to call for each memory region. +* +* Return: The memory walk will stop when func returns a non-zero value +* and that value will be returned. If all free regions are visited without +* func returning non-zero, then zero will be returned. +*/ +int __weak arch_kexec_walk_mem (struct kexec_buf *kbuf, +int (*func)(u64 ,u64 ,void *)) +{ +if (kbuf->image->type ==KEXEC_TYPE_CRASH ) +return walk_iomem_res_desc (crashk_res.desc, +IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY , + crashk_res.start, crashk_res.end, + kbuf, func); +else +return walk_system_ram_res (0 ,ULONG_MAX , kbuf, func); +} +/* * Helper function for placing a buffer in a kexec segment. This assumes * that kexec_mutex is held.
@@ -548,14 +569,7 @@ int kexec_add_buffer(struct kimage *image, char *buffer, unsigned long bufsz, kbuf->top_down = top_down;/* Walk the RAM ranges and allocate a suitable range for the buffer */ -if (image->type ==KEXEC_TYPE_CRASH ) - ret =walk_iomem_res_desc (crashk_res.desc, -IORESOURCE_SYSTEM_RAM |IORESOURCE_BUSY , - crashk_res.start, crashk_res.end, kbuf, - locate_mem_hole_callback); -else - ret =walk_system_ram_res (0 ,-1 , kbuf, - locate_mem_hole_callback); + ret =arch_kexec_walk_mem (kbuf, locate_mem_hole_callback);if (ret !=1 ) {/* A suitable memory range could not be found for buffer */ return -EADDRNOTAVAIL ;
kernel/kexec_internal.h +0 -16
@@ -20,22 +20,6 @@ struct kexec_sha_region { unsigned long len; }; -/* -* Keeps track of buffer parameters as provided by caller for requesting -* memory placement of buffer. -*/ -struct kexec_buf { -struct kimage *image; -char *buffer; -unsigned long bufsz; -unsigned long mem; -unsigned long memsz; -unsigned long buf_align; -unsigned long buf_min; -unsigned long buf_max; -bool top_down;/* allocate from top of memory hole */ -}; -void kimage_file_post_load_cleanup (struct kimage *image);#else /* CONFIG_KEXEC_FILE */ static inline void kimage_file_post_load_cleanup (struct kimage *image) { }
Signature · 1 line
1.9.1
PATCH v5 3/13 kexec_file: Factor out kexec_locate_mem_hole from kexec_add_buffer.
In reply to Thiago Jung Bauermannkexec_locate_mem_hole will be used by the PowerPC kexec_file_load implementation to find free memory for the purgatory stack.
--- include/linux/kexec.h | 1 + kernel/kexec_file.c | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-)
include/linux/kexec.h +1 -0
@@ -176,6 +176,7 @@ struct kexec_buf {int __weak arch_kexec_walk_mem (struct kexec_buf *kbuf,int (*func)(u64 ,u64 ,void *));extern int kexec_add_buffer (struct kexec_buf *kbuf); +int kexec_locate_mem_hole (struct kexec_buf *kbuf);int __weak arch_kexec_verify_buffer (enum kexec_file_type type,const void *buf,unsigned long size);#endif /* CONFIG_KEXEC_FILE */
kernel/kexec_file.c +20 -5
@@ -524,6 +524,23 @@ int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf, }/** +* kexec_locate_mem_hole - find free memory for the purgatory or the next kernel +* @kbuf: Parameters for the memory search. +* +* On success, kbuf->mem will have the start address of the memory region found. +* +* Return: 0 on success, negative errno on error. +*/ +int kexec_locate_mem_hole (struct kexec_buf *kbuf) +{ +int ret; + + ret =arch_kexec_walk_mem (kbuf, locate_mem_hole_callback); + +return ret ==1 ?0 : -EADDRNOTAVAIL ; +} + +/** * kexec_add_buffer - place a buffer in a kexec segment * @kbuf: Buffer contents and memory parameters. *
@@ -563,11 +580,9 @@ int kexec_add_buffer(struct kexec_buf *kbuf) kbuf->buf_align =max (kbuf->buf_align,PAGE_SIZE );/* Walk the RAM ranges and allocate a suitable range for the buffer */ - ret =arch_kexec_walk_mem (kbuf, locate_mem_hole_callback); -if (ret !=1 ) { -/* A suitable memory range could not be found for buffer */ -return -EADDRNOTAVAIL ; - } + ret =kexec_locate_mem_hole (kbuf); +if (ret) +return ret;/* Found a suitable memory range */ ksegment = &kbuf->image->segment[kbuf->image->nr_segments];
Signature · 1 line
1.9.1
PATCH v5 2/13 kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
In reply to Thiago Jung BauermannAdapt all callers to the new function prototype.
In addition, change the type of kexec_buf.buffer from char * to void *. There is no particular reason for it to be a char *, and the change allows us to get rid of 3 existing casts to char * in the code.
--- arch/x86/kernel/crash.c | 37 ++++++++-------- arch/x86/kernel/kexec-bzimage64.c | 48 +++++++++++---------- include/linux/kexec.h | 8 +--- kernel/kexec_file.c | 88 ++++++++++++++++++--------------------- 4 files changed, 87 insertions(+), 94 deletions(-)
arch/x86/kernel/crash.c +19 -18
@@ -615,9 +615,9 @@ static int determine_backup_region(u64 start, u64 end, void *arg)int crash_load_segments (struct kimage *image) { -unsigned long src_start, src_sz, elf_sz; -void *elf_addr;int ret; +struct kexec_buf kbuf = { .image = image, .buf_min =0 , + .buf_max =ULONG_MAX , .top_down = false };/* * Determine and load a segment for backup area. First 640K RAM
@@ -631,43 +631,44 @@ int crash_load_segments(struct kimage *image)if (ret <0 )return ret; - src_start = image->arch.backup_src_start; - src_sz = image->arch.backup_src_sz; -/* Add backup segment. */ -if (src_sz) { +if (image->arch.backup_src_sz) { + kbuf.buffer = &crash_zero_bytes; + kbuf.bufsz =sizeof (crash_zero_bytes); + kbuf.memsz = image->arch.backup_src_sz; + kbuf.buf_align =PAGE_SIZE ;/* * Ideally there is no source for backup segment. This is * copied in purgatory after crash. Just add a zero filled * segment for now to make sure checksum logic works fine. */ - ret =kexec_add_buffer (image, (char *)&crash_zero_bytes, -sizeof (crash_zero_bytes), src_sz, -PAGE_SIZE ,0 ,-1 ,0 , - &image->arch.backup_load_addr); + ret =kexec_add_buffer (&kbuf);if (ret)return ret; + image->arch.backup_load_addr = kbuf.mem;pr_debug ("Loaded backup region at 0x%lx backup_start=0x%lx memsz=0x%lx\n" , - image->arch.backup_load_addr, src_start, src_sz); + image->arch.backup_load_addr, + image->arch.backup_src_start, kbuf.memsz); }/* Prepare elf headers and add a segment */ - ret =prepare_elf_headers (image, &elf_addr, &elf_sz); + ret =prepare_elf_headers (image, &kbuf.buffer, &kbuf.bufsz);if (ret)return ret; - image->arch.elf_headers = elf_addr; - image->arch.elf_headers_sz = elf_sz; + image->arch.elf_headers = kbuf.buffer; + image->arch.elf_headers_sz = kbuf.bufsz; - ret =kexec_add_buffer (image, (char *)elf_addr, elf_sz, elf_sz, -ELF_CORE_HEADER_ALIGN ,0 ,-1 ,0 , - &image->arch.elf_load_addr); + kbuf.memsz = kbuf.bufsz; + kbuf.buf_align =ELF_CORE_HEADER_ALIGN ; + ret =kexec_add_buffer (&kbuf);if (ret) {vfree ((void *)image->arch.elf_headers);return ret; } + image->arch.elf_load_addr = kbuf.mem;pr_debug ("Loaded ELF headers at 0x%lx bufsz=0x%lx memsz=0x%lx\n" , - image->arch.elf_load_addr, elf_sz, elf_sz); + image->arch.elf_load_addr, kbuf.bufsz, kbuf.bufsz);return ret; }
arch/x86/kernel/kexec-bzimage64.c +26 -22
91 lines not on this page · raw message
include/linux/kexec.h +2 -6
@@ -163,7 +163,7 @@ struct kexec_file_ops {*/ struct kexec_buf {struct kimage *image; -char *buffer; +void *buffer;unsigned long bufsz;unsigned long mem;unsigned long memsz;
@@ -175,6 +175,7 @@ struct kexec_buf {int __weak arch_kexec_walk_mem (struct kexec_buf *kbuf,int (*func)(u64 ,u64 ,void *)); +extern int kexec_add_buffer (struct kexec_buf *kbuf);int __weak arch_kexec_verify_buffer (enum kexec_file_type type,const void *buf,unsigned long size);#endif /* CONFIG_KEXEC_FILE */
@@ -244,11 +245,6 @@ extern asmlinkage long sys_kexec_load(unsigned long entry,struct kexec_segment __user *segments, unsigned long flags);extern int kernel_kexec (void ); -extern int kexec_add_buffer (struct kimage *image,char *buffer, -unsigned long bufsz,unsigned long memsz, -unsigned long buf_align,unsigned long buf_min, -unsigned long buf_max,bool top_down, -unsigned long *load_addr);extern struct page *kimage_alloc_control_pages (struct kimage *image,unsigned int order); extern int kexec_load_purgatory(struct kimage *image, unsigned long min,
kernel/kexec_file.c +40 -48
175 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 4/13 powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
In reply to Thiago Jung BauermannThe kexec_file_load system call needs to relocate the purgatory, so factor out the module relocation code so that it can be shared.
This patch's purpose is to move the ELF relocation logic from apply_relocate_add to elf_util_64.c with as few changes as possible. The following changes were needed:
To avoid having module-specific code in a general purpose utility function, struct elf_info was created to contain the information needed for ELF binaries manipulation.
my_r2, stub_for_addr and create_stub were changed to use it instead of having to receive a struct module, since they are called from elf64_apply_relocate_add.
local_entry_offset and squash_toc_save_inst were only used by apply_rellocate_add, so they were moved to elf_util_64.c as well.
--- arch/powerpc/include/asm/elf_util.h | 70 ++++++++ arch/powerpc/include/asm/module.h | 14 +- arch/powerpc/kernel/Makefile | 4 + arch/powerpc/kernel/elf_util_64.c | 269 +++++++++++++++++++++++++++++++ arch/powerpc/kernel/module_64.c | 312 ++++-------------------------------- 5 files changed, 386 insertions(+), 283 deletions(-)
arch/powerpc/include/asm/elf_util.h new file +70 -0
@@ -0,0 +1,70 @@ +/* +* Utility functions to work with ELF files. +* +* Copyright (C) 2016, IBM Corporation +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef _ASM_POWERPC_ELF_UTIL_H +#define _ASM_POWERPC_ELF_UTIL_H + +#include <linux/elf.h> + +struct elf_info { +struct elf_shdr *sechdrs; + +/* Index of stubs section. */ +unsigned int stubs_section; +/* Index of TOC section. */ +unsigned int toc_section; +}; + +#ifdef __powerpc64__ +#ifdef PPC64_ELF_ABI_v2 + +/* An address is simply the address of the function. */ +typedef unsigned long func_desc_t ; +#else + +/* An address is address of the OPD entry, which contains address of fn. */ +typedef struct ppc64_opd_entry func_desc_t ; +#endif /* PPC64_ELF_ABI_v2 */ + +/* Like PPC32, we need little trampolines to do > 24-bit jumps (into +the kernel itself). But on PPC64, these need to be used for every +jump, actually, to reset r2 (TOC+0x8000). */ +struct ppc64_stub_entry +{ +/* 28 byte jump instruction sequence (7 instructions). We only +* need 6 instructions on ABIv2 but we always allocate 7 so +* so we don't have to modify the trampoline load instruction. */ +u32 jump[7 ]; +/* Used by ftrace to identify stubs */ +u32 magic; +/* Data for the above code */ +func_desc_t funcdata; +}; +#endif + +/* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this +gives the value maximum span in an instruction which uses a signed +offset) */ +static inline unsigned long my_r2 (const struct elf_info *elf_info) +{ +return elf_info->sechdrs[elf_info->toc_section].sh_addr +0x8000 ; +} + +int elf64_apply_relocate_add (const struct elf_info *elf_info, +const char *strtab,unsigned int symindex, +unsigned int relsec,const char *obj_name); + +#endif /* _ASM_POWERPC_ELF_UTIL_H */
arch/powerpc/include/asm/module.h +12 -2
@@ -12,7 +12,14 @@#include <linux/list.h> #include <asm/bug.h> #include <asm-generic/module.h> +#include <asm/elf_util.h> +/* Both low and high 16 bits are added as SIGNED additions, so if low +16 bits has high bit set, high 16 bits must be adjusted. These +macros do that (stolen from binutils). */ +#define PPC_LO (v) ((v) & 0xffff) +#define PPC_HI (v) (((v) >> 16) & 0xffff) +#define PPC_HA (v) PPC_HI ((v) + 0x8000)#ifndef __powerpc64__/*
@@ -33,8 +40,7 @@ struct ppc_plt_entry {struct mod_arch_specific {#ifdef __powerpc64__ -unsigned int stubs_section;/* Index of stubs section in module */ -unsigned int toc_section;/* What section is the TOC? */ +struct elf_info elf_info;bool toc_fixed;/* Have we fixed up .TOC.? */ #ifdef CONFIG_DYNAMIC_FTRACE unsigned long toc;
@@ -90,6 +96,10 @@ static inline int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sec }#endif +unsigned long stub_for_addr (const struct elf_info *elf_info,unsigned long addr, +const char *obj_name); +int restore_r2 (u32 *instruction,const char *obj_name); +struct exception_table_entry ;void sort_ex_table (struct exception_table_entry *start,struct exception_table_entry *finish);
arch/powerpc/kernel/Makefile +4 -0
@@ -123,6 +123,10 @@ ifneq ($(CONFIG_PPC_INDIRECT_PIO),y) obj-y += iomap.o endif +ifeq ($(CONFIG_MODULES)$(CONFIG_WORD_SIZE),y64) +obj-y += elf_util_64.o +endif + obj64-$(CONFIG_PPC_TRANSACTIONAL_MEM) += tm.o obj-$(CONFIG_PPC64) += $(obj64-y)
arch/powerpc/kernel/elf_util_64.c new file +269 -0
269 lines not on this page · raw message
arch/powerpc/kernel/module_64.c +31 -281
448 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 5/13 powerpc: Generalize elf64_apply_relocate_add.
In reply to Thiago Jung BauermannWhen apply_relocate_add is called, modules are already loaded at their final location in memory so Elf64_Shdr.sh_addr can be used for accessing the section contents as well as the base address for relocations.
This is not the case for kexec's purgatory, because it will only be copied to its final location right before being executed. Therefore, it needs to be relocated while it is still in a temporary buffer. In this case, Elf64_Shdr.sh_addr can't be used to access the sections' contents.
This patch allows elf64_apply_relocate_add to be used when the ELF binary is not yet at its final location by adding an addr_base argument to specify the address at which the section will be loaded, and rela, loc_base and syms_base to point to the sections' contents.
--- arch/powerpc/include/asm/elf_util.h | 6 ++-- arch/powerpc/kernel/elf_util_64.c | 63 +++++++++++++++++++++++++------------ arch/powerpc/kernel/module_64.c | 17 ++++++++-- 3 files changed, 61 insertions(+), 25 deletions(-)
arch/powerpc/include/asm/elf_util.h +4 -2
@@ -64,7 +64,9 @@ static inline unsigned long my_r2(const struct elf_info *elf_info) }int elf64_apply_relocate_add (const struct elf_info *elf_info, -const char *strtab,unsigned int symindex, -unsigned int relsec,const char *obj_name); +const char *strtab,const Elf64_Rela *rela, +unsigned int num_rela,void *syms_base, +void *loc_base,Elf64_Addr addr_base, +const char *obj_name);#endif /* _ASM_POWERPC_ELF_UTIL_H */
arch/powerpc/kernel/elf_util_64.c +43 -20
111 lines not on this page · raw message
arch/powerpc/kernel/module_64.c +14 -3
@@ -439,16 +439,26 @@ int restore_r2(u32 *instruction, const char *obj_name)return 1 ; } +/* +* When this function is called, the module is already at its final location in +* memory, so Elf64_Shdr.sh_addr can be used for accessing the section +* contents as well as the base address for relocations. +*/ int apply_relocate_add (Elf64_Shdr *sechdrs,const char *strtab,unsigned int symindex,unsigned int relsec,struct module *me) { +Elf64_Shdr *rel_section = &sechdrs[relsec]; +void *syms_base = (void *) sechdrs[symindex].sh_addr; +Elf64_Addr addr_base = sechdrs[rel_section->sh_info].sh_addr; +const Elf64_Rela *rela = (const Elf64_Rela *) rel_section->sh_addr; +unsigned int num_rela = rel_section->sh_size /sizeof (Elf64_Rela);Elf64_Sym *sym;pr_debug ("Applying ADD relocate section %u to %u\n" , relsec, - sechdrs[relsec].sh_info); + rel_section->sh_info);/* First time we're called, we can fix up .TOC. */ if (!me->arch.toc_fixed) {
@@ -460,8 +470,9 @@ int apply_relocate_add(Elf64_Shdr *sechdrs, me->arch.toc_fixed = true; } -return elf64_apply_relocate_add (&me->arch.elf_info, strtab, symindex, - relsec, me->name); +return elf64_apply_relocate_add (&me->arch.elf_info, strtab, rela, + num_rela, syms_base, (void *) addr_base, + addr_base, me->name); }#ifdef CONFIG_DYNAMIC_FTRACE
Signature · 1 line
1.9.1
PATCH v5 6/13 powerpc: Adapt elf64_apply_relocate_add for kexec_file_load.
In reply to Thiago Jung BauermannExtend elf64_apply_relocate_add to support relative symbols. This is necessary because there is a difference between how the module loading mechanism and the kexec purgatory loading code use Elf64_Sym.st_value at relocation time: the former changes st_value to point to the absolute memory address before relocating the module, while the latter does that adjustment during relocation of the purgatory.
Also, add a check_symbols argument so that the kexec code can be stricter about undefined symbols.
Finally, add relocation types used by the purgatory.
--- arch/powerpc/include/asm/elf_util.h | 1 + arch/powerpc/kernel/elf_util_64.c | 84 ++++++++++++++++++++++++++++++++++++- arch/powerpc/kernel/module_64.c | 5 ++- 3 files changed, 88 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/elf_util.h +1 -0
@@ -67,6 +67,7 @@ int elf64_apply_relocate_add(const struct elf_info *elf_info, const char *strtab, const Elf64_Rela *rela, unsigned int num_rela, void *syms_base, void *loc_base, Elf64_Addr addr_base, + bool relative_symbols, bool check_symbols, const char *obj_name);#endif /* _ASM_POWERPC_ELF_UTIL_H */
arch/powerpc/kernel/elf_util_64.c +83 -1
132 lines not on this page · raw message
arch/powerpc/kernel/module_64.c +4 -1
@@ -443,6 +443,9 @@ int restore_r2(u32 *instruction, const char *obj_name)* When this function is called, the module is already at its final location in * memory, so Elf64_Shdr.sh_addr can be used for accessing the section * contents as well as the base address for relocations. +* +* Also, simplify_symbols already changed all symbols' st_value members +* to absolute addresses. */ int apply_relocate_add(Elf64_Shdr *sechdrs, const char *strtab,
@@ -472,7 +475,7 @@ int apply_relocate_add(Elf64_Shdr *sechdrs,return elf64_apply_relocate_add (&me->arch.elf_info, strtab, rela, num_rela, syms_base, (void *) addr_base, - addr_base, me->name); + addr_base, false, false, me->name); }#ifdef CONFIG_DYNAMIC_FTRACE
Signature · 1 line
1.9.1
PATCH v5 7/13 powerpc: Add functions to read ELF files of any endianness.
In reply to Thiago Jung BauermannA little endian kernel might need to kexec a big endian kernel (the opposite is less likely but could happen as well), so we can't just cast the buffer with the binary to ELF structs and use them as is done elsewhere.
This patch adds functions which do byte-swapping as necessary when populating the ELF structs. These functions will be used in the next patch in the series.
--- arch/powerpc/include/asm/elf_util.h | 19 ++ arch/powerpc/kernel/Makefile | 2 +- arch/powerpc/kernel/elf_util.c | 476 ++++++++++++++++++++++++++++++++++++ 3 files changed, 496 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/elf_util.h +19 -0
@@ -20,6 +20,14 @@#include <linux/elf.h> struct elf_info { +/* +* Where the ELF binary contents are kept. +* Memory managed by the user of the struct. +*/ +const char *buffer; + +const struct elfhdr *ehdr; +const struct elf_phdr *proghdrs;struct elf_shdr *sechdrs;/* Index of stubs section. */
@@ -63,6 +71,17 @@ static inline unsigned long my_r2(const struct elf_info *elf_info)return elf_info->sechdrs[elf_info->toc_section].sh_addr +0x8000 ; } +static inline bool elf_is_elf_file (const struct elfhdr *ehdr) +{ +return memcmp (ehdr->e_ident,ELFMAG ,SELFMAG ) ==0 ; +} + +int elf_read_from_buffer (const char *buf,size_t len,struct elfhdr *ehdr, +struct elf_info *elf_info); +void elf_init_elf_info (const struct elfhdr *ehdr,struct elf_shdr *sechdrs, +struct elf_info *elf_info); +void elf_free_info (struct elf_info *elf_info); + int elf64_apply_relocate_add(const struct elf_info *elf_info, const char *strtab, const Elf64_Rela *rela, unsigned int num_rela, void *syms_base,
arch/powerpc/kernel/Makefile +1 -1
@@ -124,7 +124,7 @@ obj-y += iomap.o endif ifeq ($(CONFIG_MODULES)$(CONFIG_WORD_SIZE),y64) -obj-y += elf_util_64.o +obj-y += elf_util.o elf_util_64.o endif obj64-$(CONFIG_PPC_TRANSACTIONAL_MEM) += tm.o
arch/powerpc/kernel/elf_util.c new file +476 -0
476 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 8/13 powerpc: Implement kexec_file_load.
In reply to Thiago Jung Bauermannarch_kexec_walk_mem and arch_kexec_apply_relocations_add are used by generic kexec code, while setup_purgatory is powerpc-specific and sets runtime variables needed by the powerpc purgatory implementation.
--- arch/powerpc/Kconfig | 13 ++ arch/powerpc/include/asm/kexec.h | 7 + arch/powerpc/include/asm/systbl.h | 1 + arch/powerpc/include/asm/unistd.h | 2 +- arch/powerpc/include/uapi/asm/unistd.h | 1 + arch/powerpc/kernel/Makefile | 4 +- arch/powerpc/kernel/machine_kexec_64.c | 252 +++++++++++++++++++++++++++++++++ 7 files changed, 278 insertions(+), 2 deletions(-)
arch/powerpc/Kconfig +13 -0
@@ -459,6 +459,19 @@ config KEXEC interface is strongly in flux, so no good recommendation can be made. +config KEXEC_FILE + bool "kexec file based system call" + select KEXEC_CORE + select BUILD_BIN2C + depends on PPC64 + depends on CRYPTO=y + depends on CRYPTO_SHA256=y + help + This is a new version of the kexec system call. This call is + file based and takes in file descriptors as system call arguments + for kernel and initramfs as opposed to a list of segments as is the + case for the older kexec call. + config RELOCATABLE bool "Build a relocatable kernel" depends on (PPC64 && !COMPILE_TEST) || (FLATMEM && (44x || FSL_BOOKE))
arch/powerpc/include/asm/kexec.h +7 -0
@@ -91,6 +91,13 @@ static inline bool kdump_in_progress(void) return crashing_cpu >= 0; } +#ifdef CONFIG_KEXEC_FILE +int setup_purgatory (struct kimage *image,const void *slave_code, +const void *fdt,unsigned long kernel_load_addr, +unsigned long fdt_load_addr,unsigned long stack_top, +int debug); +#endif /* CONFIG_KEXEC_FILE */ +#else /* !CONFIG_KEXEC */ static inline void crash_kexec_secondary (struct pt_regs *regs) { }
arch/powerpc/include/asm/systbl.h +1 -0
@@ -386,3 +386,4 @@ SYSCALL(mlock2) SYSCALL(copy_file_range) COMPAT_SYS_SPU(preadv2) COMPAT_SYS_SPU(pwritev2) +SYSCALL(kexec_file_load)
arch/powerpc/include/asm/unistd.h +1 -1
@@ -12,7 +12,7 @@#include <uapi/asm/unistd.h> -#define NR_syscalls 382 +#define NR_syscalls 383#define __NR__exit __NR_exit
arch/powerpc/include/uapi/asm/unistd.h +1 -0
@@ -392,5 +392,6 @@#define __NR_copy_file_range 379#define __NR_preadv2 380#define __NR_pwritev2 381 +#define __NR_kexec_file_load 382#endif /* _UAPI_ASM_POWERPC_UNISTD_H_ */
arch/powerpc/kernel/Makefile +3 -1
@@ -123,9 +123,11 @@ ifneq ($(CONFIG_PPC_INDIRECT_PIO),y) obj-y += iomap.o endif -ifeq ($(CONFIG_MODULES)$(CONFIG_WORD_SIZE),y64) +ifneq ($(CONFIG_MODULES)$(CONFIG_KEXEC_FILE),) +ifeq ($(CONFIG_WORD_SIZE),64) obj-y += elf_util.o elf_util_64.o endif +endif obj64-$(CONFIG_PPC_TRANSACTIONAL_MEM) += tm.o
arch/powerpc/kernel/machine_kexec_64.c +252 -0
267 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 9/13 powerpc: Add code to work with device trees in kexec_file_load.
In reply to Thiago Jung Bauermannkexec_file_load needs to set up the device tree that will be used by the next kernel and check whether it provides a console that can be used by the purgatory.
--- arch/powerpc/include/asm/kexec.h | 3 + arch/powerpc/kernel/machine_kexec_64.c | 222 +++++++++++++++++++++++++++++++++ 2 files changed, 225 insertions(+)
arch/powerpc/include/asm/kexec.h +3 -0
@@ -96,6 +96,9 @@ int setup_purgatory(struct kimage *image, const void *slave_code, const void *fdt, unsigned long kernel_load_addr, unsigned long fdt_load_addr, unsigned long stack_top, int debug); +int setup_new_fdt (void *fdt,unsigned long initrd_load_addr, +unsigned long initrd_len,const char *cmdline); +bool find_debug_console (const void *fdt,int chosen_node);#endif /* CONFIG_KEXEC_FILE */ #else /* !CONFIG_KEXEC */
arch/powerpc/kernel/machine_kexec_64.c +222 -0
226 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 10/13 powerpc: Add support for loading ELF kernels with kexec_file_load.
In reply to Thiago Jung BauermannThis uses all the infrastructure built up by the previous patches in the series to load an ELF vmlinux file and an initrd. It uses the flattened device tree at initial_boot_params as a base and adjusts memory reservations and its /chosen node for the next kernel.
--- arch/powerpc/include/asm/kexec_elf_64.h | 10 ++ arch/powerpc/kernel/Makefile | 1 + arch/powerpc/kernel/kexec_elf_64.c | 284 ++++++++++++++++++++++++++++++++ arch/powerpc/kernel/machine_kexec_64.c | 5 +- 4 files changed, 299 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kexec_elf_64.h new file +10 -0
@@ -0,0 +1,10 @@ +#ifndef __POWERPC_KEXEC_ELF_64_H__ +#define __POWERPC_KEXEC_ELF_64_H__ + +#ifdef CONFIG_KEXEC_FILE + +extern struct kexec_file_ops kexec_elf64_ops; + +#endif /* CONFIG_KEXEC_FILE */ + +#endif /* __POWERPC_KEXEC_ELF_64_H__ */
arch/powerpc/kernel/Makefile +1 -0
@@ -109,6 +109,7 @@ obj-$(CONFIG_PCI) += pci_$(CONFIG_WORD_SIZE).o $(pci64-y) \ obj-$(CONFIG_PCI_MSI) += msi.o obj-$(CONFIG_KEXEC) += machine_kexec.o crash.o \ machine_kexec_$(CONFIG_WORD_SIZE).o +obj-$(CONFIG_KEXEC_FILE) += kexec_elf_$(CONFIG_WORD_SIZE).o obj-$(CONFIG_AUDIT) += audit.o obj64-$(CONFIG_AUDIT) += compat_audit.o
arch/powerpc/kernel/kexec_elf_64.c new file +284 -0
284 lines not on this page · raw message
arch/powerpc/kernel/machine_kexec_64.c +4 -1
@@ -32,11 +32,14 @@#include <asm/smp.h> #include <asm/hw_breakpoint.h> #include <asm/asm-prototypes.h> +#include <asm/kexec_elf_64.h> #define SLAVE_CODE_SIZE 256#ifdef CONFIG_KEXEC_FILE -static struct kexec_file_ops *kexec_file_loaders[] = { }; +static struct kexec_file_ops *kexec_file_loaders[] = { + &kexec_elf64_ops, +};#endif #ifdef CONFIG_PPC_BOOK3E
Signature · 1 line
1.9.1
PATCH v5 11/13 powerpc: Allow userspace to set device tree properties in kexec_file_load
In reply to Thiago Jung BauermannImplement the arch_kexec_verify_buffer hook to verify that a device tree blob passed by userspace via kexec_file_load contains only nodes and properties from a whitelist.
In elf64_load we merge those properties into the device tree that will be passed to the next kernel.
--- arch/powerpc/include/asm/kexec.h | 1 + arch/powerpc/kernel/kexec_elf_64.c | 9 ++ arch/powerpc/kernel/machine_kexec_64.c | 242 +++++++++++++++++++++++++++++++++ 3 files changed, 252 insertions(+)
arch/powerpc/include/asm/kexec.h +1 -0
@@ -99,6 +99,7 @@ int setup_purgatory(struct kimage *image, const void *slave_code,int setup_new_fdt (void *fdt,unsigned long initrd_load_addr,unsigned long initrd_len,const char *cmdline);bool find_debug_console (const void *fdt,int chosen_node); +int merge_partial_dtb (void *to,const void *from);#endif /* CONFIG_KEXEC_FILE */ #else /* !CONFIG_KEXEC */
arch/powerpc/kernel/kexec_elf_64.c +9 -0
@@ -210,6 +210,15 @@ void *elf64_load(struct kimage *image, char *kernel_buf, goto out; } +/* Add nodes and properties from the DTB passed by userspace. */ +if (image->dtb_buf) { + ret =merge_partial_dtb (fdt, image->dtb_buf); +if (ret) { +pr_err ("Error merging partial device tree.\n" ); + goto out; + } + } + ret =setup_new_fdt (fdt, initrd_load_addr, initrd_len, cmdline);if (ret) goto out;
arch/powerpc/kernel/machine_kexec_64.c +242 -0
252 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 12/13 powerpc: Add purgatory for kexec_file_load implementation.
In reply to Thiago Jung BauermannThis purgatory implementation comes from kexec-tools, almost unchanged.
The only changes were that the sha256_regions global variable was renamed to sha_regions to match what kexec_file_load expects, and to use the sha256.c file from x86's purgatory to avoid adding yet another SHA-256 implementation.
Also, some formatting warnings found by checkpatch.pl were fixed.
--- arch/powerpc/Makefile | 4 + arch/powerpc/purgatory/.gitignore | 2 + arch/powerpc/purgatory/Makefile | 36 +++++++ arch/powerpc/purgatory/console-ppc64.c | 38 +++++++ arch/powerpc/purgatory/crashdump-ppc64.h | 42 ++++++++ arch/powerpc/purgatory/crashdump_backup.c | 36 +++++++ arch/powerpc/purgatory/crtsavres.S | 5 + arch/powerpc/purgatory/hvCall.S | 27 +++++ arch/powerpc/purgatory/hvCall.h | 8 ++ arch/powerpc/purgatory/kexec-sha256.h | 11 ++ arch/powerpc/purgatory/ppc64_asm.h | 20 ++++ arch/powerpc/purgatory/printf.c | 164 ++++++++++++++++++++++++++++++ arch/powerpc/purgatory/purgatory-ppc64.c | 41 ++++++++ arch/powerpc/purgatory/purgatory-ppc64.h | 6 ++ arch/powerpc/purgatory/purgatory.c | 62 +++++++++++ arch/powerpc/purgatory/purgatory.h | 11 ++ arch/powerpc/purgatory/sha256.c | 6 ++ arch/powerpc/purgatory/sha256.h | 1 + arch/powerpc/purgatory/string.S | 1 + arch/powerpc/purgatory/v2wrap.S | 134 ++++++++++++++++++++++++ 20 files changed, 655 insertions(+)
arch/powerpc/Makefile +4 -0
@@ -254,6 +254,7 @@ core-y += arch/powerpc/kernel/ \ core-$(CONFIG_XMON) += arch/powerpc/xmon/ core-$(CONFIG_KVM) += arch/powerpc/kvm/ core-$(CONFIG_PERF_EVENTS) += arch/powerpc/perf/ +core-$(CONFIG_KEXEC_FILE) += arch/powerpc/purgatory/ drivers-$(CONFIG_OPROFILE) += arch/powerpc/oprofile/
@@ -375,6 +376,9 @@ archclean: $(Q)$(MAKE) $(clean)=$(boot) archprepare: checkbin +ifeq ($(CONFIG_KEXEC_FILE),y) + $(Q)$(MAKE) $(build)=arch/powerpc/purgatory arch/powerpc/purgatory/kexec-purgatory.c +endif # Use the file '.tmp_gas_check' for binutils tests, as gas won't output # to stdout and these checks are run even on install targets.
arch/powerpc/purgatory/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +kexec-purgatory.c +purgatory.ro
arch/powerpc/purgatory/Makefile new file +36 -0
@@ -0,0 +1,36 @@ +purgatory-y := purgatory.o printf.o string.o v2wrap.o hvCall.o \ + purgatory-ppc64.o console-ppc64.o crashdump_backup.o \ + crtsavres.o sha256.o + +targets += $(purgatory-y) +PURGATORY_OBJS = $(addprefix $(obj)/,$(purgatory-y)) + +LDFLAGS_purgatory.ro := -e purgatory_start -r --no-undefined -nostartfiles \ + -nostdlib -nodefaultlibs +targets += purgatory.ro + +# Default KBUILD_CFLAGS can have -pg option set when FTRACE is enabled. That +# in turn leaves some undefined symbols like __fentry__ in purgatory and not +# sure how to relocate those. Like kexec-tools, use custom flags. + +KBUILD_CFLAGS := -Wall -Wstrict-prototypes -fno-strict-aliasing \ + -fno-zero-initialized-in-bss -fno-builtin -ffreestanding \ + -fno-PIC -fno-PIE -fno-stack-protector -fno-exceptions \ + -msoft-float -MD -Os +KBUILD_CFLAGS += -m$(CONFIG_WORD_SIZE) + +$(obj)/purgatory.ro: $(PURGATORY_OBJS) FORCE + $(call if_changed,ld) + +targets += kexec-purgatory.c + +CMD_BIN2C = $(objtree)/scripts/basic/bin2c +quiet_cmd_bin2c = BIN2C $@ + cmd_bin2c = $(CMD_BIN2C) kexec_purgatory < $< > $@ + +$(obj)/kexec-purgatory.c: $(obj)/purgatory.ro FORCE + $(call if_changed,bin2c) + @: + + +obj-$(CONFIG_KEXEC_FILE) += kexec-purgatory.o
arch/powerpc/purgatory/console-ppc64.c new file +38 -0
@@ -0,0 +1,38 @@ +/* +* kexec: Linux boots Linux +* +* Created by: Mohan Kumar M ([email protected]) +* +* Copyright (C) IBM Corporation, 2005. All rights reserved +* +* Code taken from kexec-tools. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation (version 2 of the License). +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include "hvCall.h" +#include <asm/byteorder.h> + +extern int debug; + +void putchar (int c) +{ +char buff[8 ]; +unsigned long *lbuf = (unsigned long *)buff; + +if (!debug)/* running on non pseries */ +return ; + +if (c =='\n' ) +putchar ('\r' ); + + buff[0 ] = c; +plpar_hcall_norets (H_PUT_TERM_CHAR ,0 ,1 ,__cpu_to_be64 (*lbuf),0 ); +}
arch/powerpc/purgatory/crashdump-ppc64.h new file +42 -0
42 lines not on this page · raw message
arch/powerpc/purgatory/crashdump_backup.c new file +36 -0
@@ -0,0 +1,36 @@ +/* +* kexec: Linux boots Linux +* +* Created by: Mohan Kumar M ([email protected]) +* +* Copyright (C) IBM Corporation, 2005. All rights reserved +* +* Code taken from kexec-tools. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation (version 2 of the License). +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include "../boot/string.h" +#include "crashdump-ppc64.h" + +extern unsigned long backup_start; + +/* Backup first 32KB of memory to backup region reserved by kexec */ +void crashdump_backup_memory (void ) +{ +void *dest, *src; + + src = (void *)BACKUP_SRC_START ; + +if (backup_start) { + dest = (void *)(backup_start); +memcpy (dest, src,BACKUP_SRC_SIZE ); + } +}
arch/powerpc/purgatory/crtsavres.S new file +5 -0
@@ -0,0 +1,5 @@ +#ifndef CONFIG_CC_OPTIMIZE_FOR_SIZE +#define CONFIG_CC_OPTIMIZE_FOR_SIZE 1 +#endif + +#include "../lib/crtsavres.S"
arch/powerpc/purgatory/hvCall.S new file +27 -0
@@ -0,0 +1,27 @@ +/* + * This file contains the generic function to perform a call to the + * pSeries LPAR hypervisor. + * + * Taken from linux/arch/powerpc/platforms/pseries/hvCall.S + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + */ +#include "ppc64_asm.h" + +#define HVSC .long 0x44000022 +.text + .machine ppc64 +.globl DOTSYM(plpar_hcall_norets) +DOTSYM(plpar_hcall_norets): + or 6,6,6 # medium low priority + mfcr 0 + stw 0,8(1) + + HVSC /* invoke the hypervisor */ + + lwz 0,8(1) + mtcrf 0xff,0 + blr /* return r3 = status */
arch/powerpc/purgatory/hvCall.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef HVCALL_H +#define HVCALL_H + +#define H_PUT_TERM_CHAR 0x58 + +long plpar_hcall_norets (unsigned long opcode, ...); + +#endif
arch/powerpc/purgatory/kexec-sha256.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef KEXEC_SHA256_H +#define KEXEC_SHA256_H + +struct kexec_sha_region { +unsigned long start; +unsigned long len; +}; + +#define SHA256_REGIONS 16 + +#endif /* KEXEC_SHA256_H */
arch/powerpc/purgatory/ppc64_asm.h new file +20 -0
@@ -0,0 +1,20 @@ +/* +* ppc64_asm.h - common defines for PPC64 assembly parts +* +* Code taken from kexec-tools. +* +* This source code is licensed under the GNU General Public License, +* Version 2. See the file COPYING for more details. +*/ + +#include <asm/types.h> + +/* +* ABIv1 requires dot symbol while ABIv2 does not. +*/ +#ifdef PPC64_ELF_ABI_v2 +#define DOTSYM (a) a +#else +#define GLUE (a, b) a##b +#define DOTSYM (a) GLUE(., a) +#endif
arch/powerpc/purgatory/printf.c new file +164 -0
164 lines not on this page · raw message
arch/powerpc/purgatory/purgatory-ppc64.c new file +41 -0
41 lines not on this page · raw message
arch/powerpc/purgatory/purgatory-ppc64.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef PURGATORY_PPC64_H +#define PURGATORY_PPC64_H + +void crashdump_backup_memory (void ); + +#endif /* PURGATORY_PPC64_H */
arch/powerpc/purgatory/purgatory.c new file +62 -0
62 lines not on this page · raw message
arch/powerpc/purgatory/purgatory.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef PURGATORY_H +#define PURGATORY_H + +void putchar (int ch); +void sprintf (char *buffer,const char *fmt, ...) + __attribute__ ((format (printf,2 ,3 ))); +void printf (const char *fmt, ...) __attribute__ ((format (printf,1 ,2 ))); +void setup_arch (void ); +void post_verification_setup_arch (void ); + +#endif /* PURGATORY_H */
arch/powerpc/purgatory/sha256.c new file +6 -0
@@ -0,0 +1,6 @@ +#include "../boot/string.h" + +/* Avoid including x86's boot/string.h in sha256.c. */ +#define BOOT_STRING_H + +#include "../../x86/purgatory/sha256.c"
arch/powerpc/purgatory/sha256.h new file +1 -0
@@ -0,0 +1 @@ +#include "../../x86/purgatory/sha256.h"
arch/powerpc/purgatory/string.S new file +1 -0
@@ -0,0 +1 @@ +#include "../boot/string.S"
arch/powerpc/purgatory/v2wrap.S new file +134 -0
134 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v5 13/13 powerpc: Enable CONFIG_KEXEC_FILE in powerpc server defconfigs.
In reply to Thiago Jung BauermannEnable CONFIG_KEXEC_FILE in powernv_defconfig, ppc64_defconfig and pseries_defconfig.
It depends on CONFIG_CRYPTO_SHA256=y, so add that as well.
--- arch/powerpc/configs/powernv_defconfig | 2 ++ arch/powerpc/configs/ppc64_defconfig | 2 ++ arch/powerpc/configs/pseries_defconfig | 2 ++ 3 files changed, 6 insertions(+)
arch/powerpc/configs/powernv_defconfig +2 -0
@@ -47,6 +47,7 @@ CONFIG_BINFMT_MISC=m CONFIG_PPC_TRANSACTIONAL_MEM=y CONFIG_HOTPLUG_CPU=y CONFIG_KEXEC=y +CONFIG_KEXEC_FILE=y CONFIG_IRQ_ALL_CPUS=y CONFIG_NUMA=y CONFIG_MEMORY_HOTPLUG=y
@@ -298,6 +299,7 @@ CONFIG_CRYPTO_CCM=m CONFIG_CRYPTO_PCBC=m CONFIG_CRYPTO_HMAC=y CONFIG_CRYPTO_MICHAEL_MIC=m +CONFIG_CRYPTO_SHA256=y CONFIG_CRYPTO_TGR192=m CONFIG_CRYPTO_WP512=m CONFIG_CRYPTO_ANUBIS=m
arch/powerpc/configs/ppc64_defconfig +2 -0
@@ -44,6 +44,7 @@ CONFIG_HZ_100=y CONFIG_BINFMT_MISC=m CONFIG_PPC_TRANSACTIONAL_MEM=y CONFIG_KEXEC=y +CONFIG_KEXEC_FILE=y CONFIG_CRASH_DUMP=y CONFIG_IRQ_ALL_CPUS=y CONFIG_MEMORY_HOTREMOVE=y
@@ -333,6 +334,7 @@ CONFIG_CRYPTO_TEST=m CONFIG_CRYPTO_PCBC=m CONFIG_CRYPTO_HMAC=y CONFIG_CRYPTO_MICHAEL_MIC=m +CONFIG_CRYPTO_SHA256=y CONFIG_CRYPTO_TGR192=m CONFIG_CRYPTO_WP512=m CONFIG_CRYPTO_ANUBIS=m
arch/powerpc/configs/pseries_defconfig +2 -0
@@ -50,6 +50,7 @@ CONFIG_HZ_100=y CONFIG_BINFMT_MISC=m CONFIG_PPC_TRANSACTIONAL_MEM=y CONFIG_KEXEC=y +CONFIG_KEXEC_FILE=y CONFIG_IRQ_ALL_CPUS=y CONFIG_MEMORY_HOTPLUG=y CONFIG_MEMORY_HOTREMOVE=y
@@ -300,6 +301,7 @@ CONFIG_CRYPTO_TEST=m CONFIG_CRYPTO_PCBC=m CONFIG_CRYPTO_HMAC=y CONFIG_CRYPTO_MICHAEL_MIC=m +CONFIG_CRYPTO_SHA256=y CONFIG_CRYPTO_TGR192=m CONFIG_CRYPTO_WP512=m CONFIG_CRYPTO_ANUBIS=m
Signature · 1 line
1.9.1
Re: [PATCH v5 11/13] powerpc: Allow userspace to set device tree properties in kexec_file_load
In reply to Thiago Jung BauermannQuoted 93 lines · Thiago Jung Bauermann
Implement the
arch_kexec_verify_bufferhook to verify that a device tree blob passed by userspace viakexec_file_loadcontains only nodes and properties from a whitelist.In
elf64_loadwe merge those properties into the device tree that will be passed to the next kernel.Suggested-by Michael Ellerman <[email protected]>Signed-off-by Thiago Jung Bauermann <[email protected]>---
arch/powerpc/include/asm/kexec.h | 1 + arch/powerpc/kernel/kexec_elf_64.c | 9 ++arch/powerpc/kernel/machine_kexec_64.c| 242 +++++++++++++++++++++++++++++++++3 files changed, 252 insertions(+)arch/powerpc/include/asm/kexec.h +1
-0@@ -99,6 +99,7 @@ int setup_purgatory(struct kimage *image, const void *slave_code, int setup_new_fdt(void *fdt, unsigned long initrd_load_addr, unsigned long initrd_len, const char *cmdline); bool find_debug_console(const void *fdt, int chosen_node); +int merge_partial_dtb (void *to,const void *from); #endif /* CONFIG_KEXEC_FILE */
#else /* !CONFIG_KEXEC */arch/powerpc/kernel/kexec_elf_64.c +0
-0@@ -210,6 +210,15 @@ void *elf64_load(struct kimage *image, char *kernel_buf, goto out; }
+ /* Add nodes and properties from the DTB passed by userspace. */ + if (image->dtb_buf) {
+ ret =merge_partial_dtb(fdt, image->dtb_buf); + if (ret) {
+ pr_err("Error merging partial device tree.\n"); + goto out;
+ }
+ }
+
ret =setup_new_fdt(fdt, initrd_load_addr, initrd_len, cmdline); if (ret)
goto out;arch/powerpc/kernel/machine_kexec_64.c +0
-0@@ -35,6 +35,7 @@ #include <asm/kexec_elf_64.h>
#define SLAVE_CODE_SIZE 256 +#define MAX_DT_PATH 512
#ifdefCONFIG_KEXEC_FILE
static structkexec_file_ops*kexec_file_loaders[] = {arch/powerpc/kernel/machine_kexec_64.c +0
-0@@ -908,4 +909,245 @@ bool find_debug_console(const void *fdt, int chosen_node) return false; }arch/powerpc/kernel/machine_kexec_64.c +30
-0+/** +* struct allowed_node - a node in the whitelist and its allowed properties. +* @name: node name or full node path +* @properties: NULL-terminated array of names or name=value pairs +* +* If name starts with /, then the node has to be at the specified path in +* the device tree (including unit addresses for all nodes in the path). +* If it doesn't, then the node can be anywhere in the device tree. +* +* An entry in properties can specify a string value that the property must +* have by using the "name=value" format. If the entry ends with =, it means +* that the property must be empty. +*/ +static struct allowed_node { + const char *name; + const char *properties[9]; +} allowed_nodes[] = { + { + .name = "/chosen", + .properties = { + "stdout-path", + "linux,stdout-path", + NULL, + } + }, + { + .name = "vga", + .properties = { + "device_type=display", + "assigned-addresses",
arch/powerpc/kernel/machine_kexec_64.c bool find_debug_console(const void *fdt, int chosen_node) · line 951
Quoted 10 lines
arch/powerpc/kernel/machine_kexec_64.c +10
-0+ "width", + "height", + "depth", + "little-endian=", + "linux,opened=", + "linux,boot-display=",ss + NULL, + } + }, +};
Hi Thiago,
As much as this solves problems for *me*, I suspect adding 'vga' here might be the subject of some discussion. Having /chosen whitelisted makes sense on it's own, but 'vga' and its properties are very specific without much explanation.
If everyone's happy to have it there, cool! If not, I have the majority of a patch that handles the original reason for these property updates separately in the kernel rather than from userspace. If needed I'll clean it up and we can handle it that way.
Cheers,
Sam
Re: [PATCH v5 11/13] powerpc: Allow userspace to set device tree properties in kexec_file_load
Hello Sam,
Thanks for the quick response.
Am Freitag, 12 August 2016, 10:45:00 schrieb Samuel Mendoza-Jonas:
Quoted 53 lines · Thiago Jung Bauermann
Quoted 34 lines
arch/powerpc/kernel/machine_kexec_64.c +2
-0@@ -908,4 +909,245 @@ bool find_debug_console(const void *fdt, int chosen_node) return false; } +/** +* struct allowed_node - a node in the whitelist and its allowed properties. + * @name: node name or full node path
+ * @properties: NULL-terminated array of names or
name=value pairs + *
+ * If name starts with /, then the node has to be at the specified path in + * the device tree (including unit addresses for all nodes in the path). + * If it doesn't, then the node can be anywhere in the device tree. + *
+ * An entry in properties can specify a string value that the property must + * have by using the "name=value" format. If the entry ends with =, it means + * that the property must be empty.
+ */arch/powerpc/kernel/machine_kexec_64.c +17
-0+static struct allowed_node { +const char *name; +const char *properties[9 ]; +} allowed_nodes[] = { + { + .name ="/chosen" , + .properties = { +"stdout-path" , +"linux,stdout-path" , +NULL , + } + }, + { + .name ="vga" , + .properties = { +"device_type=display" , +"assigned-addresses" ,arch/powerpc/kernel/machine_kexec_64.c bool find_debug_console(const void *fdt, int chosen_node) · line 951
Quoted 10 lines
arch/powerpc/kernel/machine_kexec_64.c +10
-0+ "width", + "height", + "depth", + "little-endian=", + "linux,opened=", + "linux,boot-display=",ss + NULL, + } + }, +};Hi Thiago,
As much as this solves problems for *me*, I suspect adding 'vga' here might be the subject of some discussion. Having /chosen whitelisted makes sense on it's own, but 'vga' and its properties are very specific without much explanation.
Quoted 4 lines
If everyone's happy to have it there, cool! If not, I have the majority of a patch that handles the original reason for these property updates separately in the kernel rather than from userspace. If needed I'll clean it up and we can handle it that way.
Ok, that's good to know. I'm fine with it either way. In any case, 'vga' in this patch also serves a good real-life example of a non-trivial binding outside of /chosen that we might want to whitelist in the future.
Signature · 3 lines
[]'s Thiago Jung Bauermann IBM Linux Technology Center
Re: [PATCH v5 01/13] kexec_file: Allow arch-specific memory walking for kexec_add_buffer
In reply to Thiago Jung BauermannQuoted 7 lines · Thiago Jung Bauermann
Allow architectures to specify a different memory walking function for
kexec_add_buffer. x86 uses iomem to track reserved memory ranges, but PowerPC uses the memblock subsystem.Signed-off-by Thiago Jung Bauermann <[email protected]>Acked-by Dave Young <[email protected]>---
Re: [PATCH v5 02/13] kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
In reply to Thiago Jung BauermannQuoted 2 lines · Thiago Jung Bauermann
Adapt all callers to the new function prototype.
Could you please expand on this?
Quoted 7 lines
In addition, change the type of kexec_buf.buffer from char * to void *. There is no particular reason for it to be a char *, and the change allows us to get rid of 3 existing casts to char * in the code.
Signed-off-by Thiago Jung Bauermann <[email protected]>Acked-by Dave Young <[email protected]>---
Looks good otherwise
Re: [PATCH v5 04/13] powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
In reply to Thiago Jung BauermannQuoted 240 lines · Thiago Jung Bauermann
The
kexec_file_loadsystem call needs to relocate the purgatory, so factor out the module relocation code so that it can be shared.This patch's purpose is to move the ELF relocation logic from
apply_relocate_addtoelf_util_64.cwith as few changes as possible. The following changes were needed:To avoid having module-specific code in a general purpose utility function, struct
elf_infowas created to contain the information needed for ELF binaries manipulation.
my_r2,stub_for_addrandcreate_stubwere changed to use it instead of having to receive a struct module, since they are called fromelf64_apply_relocate_add.
local_entry_offsetandsquash_toc_save_instwere only used byapply_rellocate_add, so they were moved toelf_util_64.cas well.Signed-off-by Thiago Jung Bauermann <[email protected]>--- arch/powerpc/include/asm/elf_util.h | 70 ++++++++ arch/powerpc/include/asm/module.h | 14 +- arch/powerpc/kernel/Makefile | 4 + arch/powerpc/kernel/elf_util_64.c | 269 +++++++++++++++++++++++++++++++ arch/powerpc/kernel/module_64.c | 312 ++++-------------------------------- 5 files changed, 386 insertions(+), 283 deletions(-)arch/powerpc/include/asm/elf_util.h new file +70
-0@@ -0,0 +1,70 @@ +/* +* Utility functions to work with ELF files. +* +* Copyright (C) 2016, IBM Corporation +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#ifndef _ASM_POWERPC_ELF_UTIL_H +#define _ASM_POWERPC_ELF_UTIL_H + +#include <linux/elf.h> + +struct elf_info { +struct elf_shdr *sechdrs; + +/* Index of stubs section. */ +unsigned int stubs_section; +/* Index of TOC section. */ +unsigned int toc_section; +}; + +#ifdef __powerpc64__ +#ifdef PPC64_ELF_ABI_v2 + +/* An address is simply the address of the function. */ +typedef unsigned long func_desc_t ; +#else + +/* An address is address of the OPD entry, which contains address of fn. */ +typedef struct ppc64_opd_entry func_desc_t ; +#endif /* PPC64_ELF_ABI_v2 */ + +/* Like PPC32, we need little trampolines to do > 24-bit jumps (into +the kernel itself). But on PPC64, these need to be used for every +jump, actually, to reset r2 (TOC+0x8000). */ +struct ppc64_stub_entry +{ +/* 28 byte jump instruction sequence (7 instructions). We only +* need 6 instructions on ABIv2 but we always allocate 7 so +* so we don't have to modify the trampoline load instruction. */ +u32 jump[7 ]; +/* Used by ftrace to identify stubs */ +u32 magic; +/* Data for the above code */ +func_desc_t funcdata; +}; +#endif + +/* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this +gives the value maximum span in an instruction which uses a signed +offset) */ +static inline unsigned long my_r2 (const struct elf_info *elf_info) +{ +return elf_info->sechdrs[elf_info->toc_section].sh_addr +0x8000 ; +} + +int elf64_apply_relocate_add (const struct elf_info *elf_info, +const char *strtab,unsigned int symindex, +unsigned int relsec,const char *obj_name); + +#endif /* _ASM_POWERPC_ELF_UTIL_H */ arch/powerpc/include/asm/module.h +12
-2@@ -12,7 +12,14 @@#include <linux/list.h> #include <asm/bug.h> #include <asm-generic/module.h> +#include <asm/elf_util.h> +/* Both low and high 16 bits are added as SIGNED additions, so if low +16 bits has high bit set, high 16 bits must be adjusted. These +macros do that (stolen from binutils). */ +#define PPC_LO (v) ((v) & 0xffff) +#define PPC_HI (v) (((v) >> 16) & 0xffff) +#define PPC_HA (v) PPC_HI ((v) + 0x8000)#ifndef __powerpc64__/* @@ -33,8 +40,7 @@ struct ppc_plt_entry {struct mod_arch_specific {#ifdef __powerpc64__ -unsigned int stubs_section;/* Index of stubs section in module */ -unsigned int toc_section;/* What section is the TOC? */ +struct elf_info elf_info;bool toc_fixed;/* Have we fixed up .TOC.? */ #ifdef CONFIG_DYNAMIC_FTRACE unsigned long toc;@@ -90,6 +96,10 @@ static inline int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sec }#endif +unsigned long stub_for_addr (const struct elf_info *elf_info,unsigned long addr, +const char *obj_name); +int restore_r2 (u32 *instruction,const char *obj_name); +struct exception_table_entry ;void sort_ex_table (struct exception_table_entry *start,struct exception_table_entry *finish);arch/powerpc/kernel/Makefile +4
-0@@ -123,6 +123,10 @@ ifneq ($(CONFIG_PPC_INDIRECT_PIO),y) obj-y += iomap.o endif +ifeq ($(CONFIG_MODULES)$(CONFIG_WORD_SIZE),y64) +obj-y += elf_util_64.o +endif + obj64-$(CONFIG_PPC_TRANSACTIONAL_MEM) += tm.o obj-$(CONFIG_PPC64) += $(obj64-y)arch/powerpc/kernel/elf_util_64.c new file +76
-0@@ -0,0 +1,269 @@ +/* +* Utility functions to work with ELF files. +* +* Copyright (C) 2016, IBM Corporation +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2, or (at your option) +* any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +*/ + +#include <asm/ppc-opcode.h> +#include <asm/elf_util.h> + +/* +* We just need to use the functions defined in <asm/module.h>, so just declare +* struct module here and avoid having to import <linux/module.h>. +*/ +struct module ; +#include <asm/module.h> + +#ifdef PPC64_ELF_ABI_v2 +/* PowerPC64 specific values for the Elf64_Sym st_other field. */ +#define STO_PPC64_LOCAL_BIT 5 +#define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT) +#define PPC64_LOCAL_ENTRY_OFFSET (other) \ + (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2) + +static unsigned int local_entry_offset (const Elf64_Sym *sym) +{ +/* sym->st_other indicates offset to local entry point +* (otherwise it will assume r12 is the address of the start +* of function and try to derive r2 from it). */ +return PPC64_LOCAL_ENTRY_OFFSET (sym->st_other); +} +#else +static unsigned int local_entry_offset (const Elf64_Sym *sym) +{ +return 0 ; +} +#endif + +#ifdef CC_USING_MPROFILE_KERNEL +/* +* In case of _mcount calls, do not save the current callee's TOC (in r2) into +* the original caller's stack frame. If we did we would clobber the saved TOC +* value of the original caller. +*/ +static void squash_toc_save_inst (const char *name,unsigned long addr) +{ +struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr; + +/* Only for calls to _mcount */ +if (strcmp ("_mcount" , name) !=0 ) +return ; + + stub->jump[2 ] =PPC_INST_NOP ; +} +#else +static void squash_toc_save_inst (const char *name,unsigned long addr) { } +#endif + +/** +* elf64_apply_relocate_add - apply 64 bit RELA relocations +* @elf_info: Support information for the ELF binary being relocated. +* @strtab: String table for the associated symbol table. +* @symindex: Section header index for the associated symbol table. +* @relsec: Section header index for the relocations to apply. +* @obj_name: The name of the ELF binary, for information messages. +*/ +int elf64_apply_relocate_add(const struct elf_info *elf_info,
arch/powerpc/kernel/elf_util_64.c line 86
Quoted 10 lines
arch/powerpc/kernel/elf_util_64.c new file +10
-0+ const char *strtab, unsigned int symindex, + unsigned int relsec, const char *obj_name) +{ + unsigned int i; +Elf64_Shdr *sechdrs = elf_info->sechdrs; +Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; +Elf64_Sym *sym; +unsigned long *location; +unsigned long value; +
For the relocatable kernel we expect only
R_PPC64_RELATIVER_PPC64_NONER_PPC64_ADDR64
In the future we can use this to check/assert the usage of this for the core kernel (vmlinux) when loaded.
Did we check elf64_apply_relocate_add with zImage and vmlinux?
Balbir Singh
Re: [PATCH v5 02/13] kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
In reply to Balbir SinghAm Montag, 15 August 2016, 17:30:49 schrieb Balbir Singh:
Quoted 4 lines · Thiago Jung Bauermann
Quoted 1 line
Adapt all callers to the new function prototype.
Could you please expand on this?
Is the following better?
Adapt all callers to set up a kexec_buf to pass to kexec_add_buffer.
Quoted 3 lines
Looks good otherwise
Acked-by Balbir Singh <[email protected]>
Thank you for reviewing this series!
Signature · 3 lines
[]'s Thiago Jung Bauermann IBM Linux Technology Center
Re: [PATCH v5 04/13] powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
In reply to Balbir SinghAm Montag, 15 August 2016, 17:46:34 schrieb Balbir Singh:
Quoted 4 lines · Thiago Jung Bauermann
Quoted 3 lines
+/**
+ *elf64_apply_relocate_add- apply 64 bit RELA relocations
+ * @elf_info: Support information for the ELF binary being
relocated.
Quoted 1 line
Quoted 1 line
+ * @strtab: String table for the associated symbol
table.
Quoted 1 line
Quoted 1 line
+ * @symindex: Section header index for the associated
symbol table.
Quoted 1 line
Quoted 1 line
+ * @relsec: Section header index for the relocations to
apply.
Quoted 1 line
Quoted 1 line
+ * @obj_name: The name of the ELF binary, for information
messages.
Quoted 15 lines
arch/powerpc/kernel/elf_util_64.c line 86
Quoted 12 lines
+ */
arch/powerpc/kernel/elf_util_64.c new file +11
-0+int elf64_apply_relocate_add (const struct elf_info *elf_info, +const char *strtab,unsigned int symindex, +unsigned int relsec,const char *obj_name) +{ +unsigned int i; +Elf64_Shdr *sechdrs = elf_info->sechdrs; +Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; +Elf64_Sym *sym; +unsigned long *location; +unsigned long value; +For the relocatable kernel we expect only
Quoted 8 lines
R_PPC64_RELATIVER_PPC64_NONER_PPC64_ADDR64In the future we can use this to check/assert the usage of this for the core kernel (vmlinux) when loaded.
Did we check
elf64_apply_relocate_addwith zImage and vmlinux?
kexec_file_load doesn't call call elf64_apply_relocate_add on the kernel image, it only uses it to relocate the purgatory. So whether it is loading a zImage or a vmlinux file, the function will work in the same way since the purgatory binary is the same regardless of the kernel image format.
For the same reason, as it currently stands kexec_file_load can't check the relocation types used in the kernel image. But it is possible to add such a check/assertion in kexec_elf_64.c:build_elf_exec_info if we want.
I tested kexec_file_load on both relocatable and non-relocatable vmlinux and it works correctly.
I hadn't tested with zImage yet. I just did, and I had two problems:
1. For some reason, it has an INTERP segment. This patch series doesn't support loading program interpreters for ELF binaries, so kexec_elf_64.c:build_elf_exec_info refuses to load them.
2. If I disable the check for the INTERP segment, the zImage file loads correctly, but then I get an exception during reboot when loading the kexec image, right before jumping into the purgatory. I suspect this is because the LOAD segment has a virtual address of 0, and the first kernel is not coping well with that. But I still have to debug it further.
Is there a reason for the zImage ELF header to request an interpreter and to have a virtual address of 0?
Signature · 3 lines
[]'s Thiago Jung Bauermann IBM Linux Technology Center
Re: [PATCH v5 02/13] kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
In reply to Thiago Jung BauermannQuoted 9 lines · Thiago Jung Bauermann
Am Montag, 15 August 2016, 17:30:49 schrieb Balbir Singh:
Quoted 4 lines · Thiago Jung Bauermann
Quoted 1 line
Adapt all callers to the new function prototype.
Could you please expand on this?
Is the following better?
Adapt all callers to set up a
kexec_bufto pass tokexec_add_buffer.
Yes and the reason for doing so? Consolidation/clarity of implementation?
Quoted 7 lines
Quoted 4 lines
Looks good otherwise
Acked-by Balbir Singh <[email protected]>Thank you for reviewing this series!
Re: [PATCH v5 04/13] powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
In reply to Thiago Jung BauermannQuoted 36 lines · Thiago Jung Bauermann
Am Montag, 15 August 2016, 17:46:34 schrieb Balbir Singh:
Quoted 4 lines · Thiago Jung Bauermann
Quoted 3 lines
+/**
+ *elf64_apply_relocate_add- apply 64 bit RELA relocations
+ * @elf_info: Support information for the ELF binary beingrelocated.
Quoted 1 line
Quoted 1 line
+ * @strtab: String table for the associated symbol
table.
Quoted 1 line
Quoted 1 line
+ * @symindex: Section header index for the associated
symbol table.
Quoted 1 line
Quoted 1 line
+ * @relsec: Section header index for the relocations to
apply.
Quoted 1 line
Quoted 1 line
+ * @obj_name: The name of the ELF binary, for information
messages.
Quoted 22 lines
arch/powerpc/kernel/elf_util_64.c line 86
Quoted 12 lines
+ */
arch/powerpc/kernel/elf_util_64.c new file +11
-0+int elf64_apply_relocate_add (const struct elf_info *elf_info, +const char *strtab,unsigned int symindex, +unsigned int relsec,const char *obj_name) +{ +unsigned int i; +Elf64_Shdr *sechdrs = elf_info->sechdrs; +Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; +Elf64_Sym *sym; +unsigned long *location; +unsigned long value; +For the relocatable kernel we expect only
R_PPC64_RELATIVER_PPC64_NONER_PPC64_ADDR64In the future we can use this to check/assert the usage of this for the core kernel (vmlinux) when loaded.
Quoted 6 lines
Quoted 1 line
Did we check
elf64_apply_relocate_addwith zImage and vmlinux?
kexec_file_loaddoesn't call callelf64_apply_relocate_addon the kernel image, it only uses it to relocate the purgatory. So whether it is loading a zImage or a vmlinux file, the function will work in the same way since the purgatory binary is the same regardless of the kernel image format.
Thanks for clarifying.
Quoted 14 lines
For the same reason, as it currently stands
kexec_file_loadcan't check the relocation types used in the kernel image. But it is possible to add such a check/assertion in kexec_elf_64.c:build_elf_exec_info if we want.I tested
kexec_file_loadon both relocatable and non-relocatable vmlinux and it works correctly.I hadn't tested with zImage yet. I just did, and I had two problems:
1. For some reason, it has an INTERP segment. This patch series doesn't support loading program interpreters for ELF binaries, so kexec_elf_64.c:build_elf_exec_info refuses to load them.
Quoted 9 lines
2. If I disable the check for the INTERP segment, the zImage file loads correctly, but then I get an exception during reboot when loading the kexec image, right before jumping into the purgatory. I suspect this is because the LOAD segment has a virtual address of 0, and the first kernel is not coping well with that. But I still have to debug it further.
Is there a reason for the zImage ELF header to request an interpreter and to have a virtual address of 0?
Not that I am aware of.
Balbir Singh
Re: [PATCH v5 02/13] kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
In reply to Balbir SinghAm Dienstag, 16 August 2016, 16:15:55 schrieb Balbir Singh:
Quoted 5 lines · Thiago Jung Bauermann
Quoted 4 lines
Am Montag, 15 August 2016, 17:30:49 schrieb Balbir Singh:
Quoted 3 lines · Thiago Jung Bauermann
Quoted 1 line
Adapt all callers to the new function prototype.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Could you please expand on this?
Is the following better?
Adapt all callers to set up a
kexec_bufto pass tokexec_add_buffer.Yes and the reason for doing so? Consolidation/clarity of implementation?
Indeed. What about this commit message?
Subject: [PATCH v5 02/13] kexec_file: Change kexec_add_buffer to take
kexec_buf as argument.
This is done to simplify the kexec_add_buffer argument list. Adapt all callers to set up a kexec_buf to pass to kexec_add_buffer.
In addition, change the type of kexec_buf.buffer from char * to void *. There is no particular reason for it to be a char *, and the change allows us to get rid of 3 existing casts to char * in the code.
Signature · 3 lines
[]'s Thiago Jung Bauermann IBM Linux Technology Center
Re: [PATCH v5 02/13] kexec_file: Change kexec_add_buffer to take kexec_buf as argument.
Quoted 22 lines · Thiago Jung Bauermann
Am Dienstag, 16 August 2016, 16:15:55 schrieb Balbir Singh:
Quoted 5 lines · Thiago Jung Bauermann
Quoted 4 lines
Am Montag, 15 August 2016, 17:30:49 schrieb Balbir Singh:
Quoted 3 lines · Thiago Jung Bauermann
Quoted 1 line
Adapt all callers to the new function prototype.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Could you please expand on this?
Is the following better?
Adapt all callers to set up a
kexec_bufto pass tokexec_add_buffer.Yes and the reason for doing so? Consolidation/clarity of implementation?
Indeed. What about this commit message?
Subject: [PATCH v5 02/13]
kexec_file: Changekexec_add_bufferto take
kexec_bufas argument.This is done to simplify the
kexec_add_bufferargument list. Adapt all callers to set up akexec_bufto pass tokexec_add_buffer.
Quoted 8 lines
In addition, change the type of kexec_buf.buffer from char * to void *. There is no particular reason for it to be a char *, and the change allows us to get rid of 3 existing casts to char * in the code.
Signed-off-by Thiago Jung Bauermann <[email protected]>Acked-by Dave Young <[email protected]>Acked-by Balbir Singh <[email protected]>
Looks good
Balbir
Re: [PATCH v5 04/13] powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
In reply to Balbir SinghAm Dienstag, 16 August 2016, 16:52:54 schrieb Balbir Singh:
Quoted 77 lines · Thiago Jung Bauermann
Quoted 46 lines
Am Montag, 15 August 2016, 17:46:34 schrieb Balbir Singh:
Quoted 4 lines · Thiago Jung Bauermann
Quoted 3 lines
+/**
+ *elf64_apply_relocate_add- apply 64 bit RELA relocations
+ * @elf_info: Support information for the ELF binary beingrelocated.
Quoted 1 line
Quoted 1 line
+ * @strtab: String table for the associated symbol
table.
Quoted 1 line
Quoted 1 line
+ * @symindex: Section header index for the associated
symbol table.
Quoted 1 line
Quoted 1 line
+ * @relsec: Section header index for the relocations to
apply.
Quoted 1 line
Quoted 1 line
+ * @obj_name: The name of the ELF binary, for information
messages.
Quoted 22 lines
arch/powerpc/kernel/elf_util_64.c line 86
Quoted 12 lines
+ */
arch/powerpc/kernel/elf_util_64.c new file +11
-0+int elf64_apply_relocate_add (const struct elf_info *elf_info, +const char *strtab,unsigned int symindex, +unsigned int relsec,const char *obj_name) +{ +unsigned int i; +Elf64_Shdr *sechdrs = elf_info->sechdrs; +Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; +Elf64_Sym *sym; +unsigned long *location; +unsigned long value; +For the relocatable kernel we expect only
R_PPC64_RELATIVER_PPC64_NONER_PPC64_ADDR64In the future we can use this to check/assert the usage of this for the core kernel (vmlinux) when loaded.
Quoted 7 lines
Quoted 1 line
Did we check
elf64_apply_relocate_addwith zImage and vmlinux?
kexec_file_loaddoesn't call callelf64_apply_relocate_addon the kernel image, it only uses it to relocate the purgatory. So whether it is loading a zImage or a vmlinux file, the function will work in the same way since the purgatory binary is the same regardless of the kernel image format.Thanks for clarifying.
Quoted 21 lines
For the same reason, as it currently stands
kexec_file_loadcan't check the relocation types used in the kernel image. But it is possible to add such a check/assertion in kexec_elf_64.c:build_elf_exec_info if we want.I tested
kexec_file_loadon both relocatable and non-relocatable vmlinux and it works correctly.I hadn't tested with zImage yet. I just did, and I had two problems:
1. For some reason, it has an INTERP segment. This patch series doesn't support loading program interpreters for ELF binaries, so kexec_elf_64.c:build_elf_exec_info refuses to load them.
2. If I disable the check for the INTERP segment, the zImage file loads correctly, but then I get an exception during reboot when loading the kexec image, right before jumping into the purgatory. I suspect this is because the LOAD segment has a virtual address of 0, and the first kernel is not coping well with that. But I still have to debug it further.
Quoted 4 lines
Quoted 2 lines
Is there a reason for the zImage ELF header to request an interpreter and to have a virtual address of 0?
Not that I am aware of.
zImage on ppc64 BE is an ELF32 file. This patch set only supports loading ELF files of the same class as the kernel, so a 64 bit kernel can't load an ELF32 file. It would be possible to add such support, but it would be a new feature.
The distros I was able to check on ppc64 LE and BE all use vmlinux. kexec-tools with kexec_load also doesn't support zImage. Do you think it is important to support zImage?
Signature · 3 lines
[]'s Thiago Jung Bauermann IBM Linux Technology Center
Re: [PATCH v5 04/13] powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
Quoted 10 lines
zImage on ppc64 BE is an ELF32 file. This patch set only supports loading ELF files of the same class as the kernel, so a 64 bit kernel can't load an ELF32 file. It would be possible to add such support, but it would be a new feature.
The distros I was able to check on ppc64 LE and BE all use vmlinux. kexec-tools with
kexec_loadalso doesn't support zImage. Do you think it is important to support zImage?
Well if it didn't work already, I think its low priority. Michael should be able to confirm this. Oliver's been trying to cleanup the zImage to get rid the old zImage limitation, cc'ing him
Balbir Singh
Re: [PATCH v5 04/13] powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
Quoted 6 lines · Balbir Singh
Quoted 6 lines
zImage on ppc64 BE is an ELF32 file. This patch set only supports loading ELF files of the same class as the kernel, so a 64 bit kernel can't load an ELF32 file. It would be possible to add such support, but it would be a new feature.
Quoted 7 lines
Quoted 3 lines
The distros I was able to check on ppc64 LE and BE all use vmlinux. kexec-tools with
kexec_loadalso doesn't support zImage. Do you think it is important to support zImage?Well if it didn't work already, I think its low priority. Michael should be able to confirm this. Oliver's been trying to cleanup the zImage to get rid the old zImage limitation, cc'ing him
I don't think it's ever worked so I wouldn't worry too much about supporting it. Fixing kexec-into-zImage and fixing the 32bit wrapper on 64bit BE kernel problem has been on my TODO list for a while, but it's not a priority.
oliver
Re: [PATCH v5 04/13] powerpc: Factor out relocation code from module_64.c to elf_util_64.c.
Am Mittwoch, 24 August 2016, 10:50:26 schrieb Oliver O'Halloran:
Quoted 1 line
On Tue, Aug 23, 2016 at 1:21 PM, Balbir Singh <[email protected]>
Quoted 10 lines · wrote:
Quoted 10 lines
Quoted 9 lines
zImage on ppc64 BE is an ELF32 file. This patch set only supports loading
ELF files of the same class as the kernel, so a 64 bit kernel can't load an ELF32 file. It would be possible to add such support, but it would be a new feature.The distros I was able to check on ppc64 LE and BE all use vmlinux. kexec-tools with
kexec_loadalso doesn't support zImage. Do you think it is important to support zImage?
Quoted 8 lines
Quoted 3 lines
Well if it didn't work already, I think its low priority. Michael should be able to confirm this. Oliver's been trying to cleanup the zImage to get rid the old zImage limitation, cc'ing him
I don't think it's ever worked so I wouldn't worry too much about supporting it. Fixing kexec-into-zImage and fixing the 32bit wrapper on 64bit BE kernel problem has been on my TODO list for a while, but it's not a priority.
Ok, thanks for your input.
Signature · 3 lines
[]'s Thiago Jung Bauermann IBM Linux Technology Center