diff options
author | Vivek Goyal <vgoyal@in.ibm.com> | 2005-11-17 19:06:27 +0530 |
---|---|---|
committer | Eric W. Biederman <ebiederm@xmission.com> | 2006-07-27 10:33:22 -0600 |
commit | 38be08689724f4679e1a267eee671991056374f6 (patch) | |
tree | 0ed8dabc6672481b52f41b1081904236df917991 | |
parent | 8be19e1a35aff9290ed142fae38a53243176309e (diff) |
kexec-tools: x86_64 elf header generation
o This patch is to generate the elf-core headers for x86_64.
Not sure whether to retain the options --elf32 and --elf64
but as of now, we port them to x86_64 also.
o We conditionally fill up the vaddr in the phdr as we got
two linearly memory mapped regions in x86_64 unlike i386.
To get the virtual address, we make of use of __START_KERNEL_map
for the frist 0 to 40MB and for others we make use of PAGE_OFFSET.
o Support for per cpu dynamic allocation of crash notes.
Signed-off-by: Murali M Chakravarthy <muralim@in.ibm.com>
Signed-off-by: Vivek Goyal <vgoyal@in.ibm.com>
Signed-off-by: Maneesh Soni <maneesh@in.ibm.com>
-rw-r--r-- | kexec/arch/x86_64/Makefile | 2 | ||||
-rw-r--r-- | kexec/arch/x86_64/crashdump-x86_64.c | 283 | ||||
-rw-r--r-- | kexec/arch/x86_64/crashdump-x86_64.h | 24 | ||||
-rw-r--r-- | kexec/arch/x86_64/include/arch/options.h | 6 | ||||
-rw-r--r-- | kexec/arch/x86_64/kexec-x86_64.c | 11 |
5 files changed, 324 insertions, 2 deletions
diff --git a/kexec/arch/x86_64/Makefile b/kexec/arch/x86_64/Makefile index 18e1690..799e2a4 100644 --- a/kexec/arch/x86_64/Makefile +++ b/kexec/arch/x86_64/Makefile @@ -1,13 +1,13 @@ # # kexec x86_64 (linux booting linux) # -KEXEC_C_SRCS+= kexec/arch/i386/crashdump-x86.c KEXEC_C_SRCS+= kexec/arch/i386/kexec-elf-x86.c KEXEC_C_SRCS+= kexec/arch/i386/kexec-bzImage.c KEXEC_C_SRCS+= kexec/arch/i386/kexec-multiboot-x86.c KEXEC_C_SRCS+= kexec/arch/i386/kexec-beoboot-x86.c KEXEC_C_SRCS+= kexec/arch/i386/kexec-nbi.c KEXEC_C_SRCS+= kexec/arch/i386/x86-linux-setup.c +KEXEC_C_SRCS+= kexec/arch/x86_64/crashdump-x86_64.c KEXEC_C_SRCS+= kexec/arch/x86_64/kexec-x86_64.c KEXEC_C_SRCS+= kexec/arch/x86_64/kexec-elf-x86_64.c KEXEC_C_SRCS+= kexec/arch/x86_64/kexec-elf-rel-x86_64.c diff --git a/kexec/arch/x86_64/crashdump-x86_64.c b/kexec/arch/x86_64/crashdump-x86_64.c new file mode 100644 index 0000000..299d9d1 --- /dev/null +++ b/kexec/arch/x86_64/crashdump-x86_64.c @@ -0,0 +1,283 @@ +/* + * kexec: Linux boots Linux + * + * Created by: Murali M Chakravarthy (muralim@in.ibm.com) + * Copyright (C) IBM Corporation, 2005. All rights reserved + * Heavily borrowed from kexec/arch/i386/crashdump-x86.c + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <limits.h> +#include <elf.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include "../../kexec.h" +#include "../../kexec-elf.h" +#include "../../kexec-syscall.h" +#include "kexec-x86_64.h" +#include "crashdump-x86_64.h" +#include <x86/x86-linux.h> + +/* Returns the virtual address of start of crash notes buffer for a cpu. */ +static int get_crash_notes_section_addr(int cpu, unsigned long long *addr) +{ + +#define MAX_SYSFS_PATH_LEN 70 + char crash_notes[MAX_SYSFS_PATH_LEN]; + char line[MAX_LINE]; + FILE *fp; + struct stat cpu_stat; + + sprintf(crash_notes, "/sys/devices/system/cpu"); + if (stat(crash_notes, &cpu_stat)) { + die("Cannot stat %s: %s\nTry mounting sysfs\n", + crash_notes, strerror(errno)); + } + + sprintf(crash_notes, "/sys/devices/system/cpu/cpu%d/crash_notes", cpu); + fp = fopen(crash_notes, "r"); + if (!fp) { + /* CPU is not physically present.*/ + *addr = 0; + return -1; + } + + if (fgets(line, sizeof(line), fp) != 0) { + int count; + count = sscanf(line, "%Lx", addr); + if (count != 1) { + *addr = 0; + return -1; + } +#if 0 + printf("crash_notes addr = %Lx\n", *addr); +#endif + } + return 0; +} + +/* Prepares the crash memory elf64 headers and stores in supplied buffer. */ +static int prepare_crash_memory_elf64_headers(struct kexec_info *info, + void *buf, unsigned long size) +{ + Elf64_Ehdr *elf; + Elf64_Phdr *phdr; + int i; + char *bufp; + long int nr_cpus = 0; + unsigned long long notes_addr; + + bufp = (char*) buf; + + /* Setup ELF Header*/ + elf = (Elf64_Ehdr *) bufp; + bufp += sizeof(Elf64_Ehdr); + memcpy(elf->e_ident, ELFMAG, SELFMAG); + elf->e_ident[EI_CLASS] = ELFCLASS64; + elf->e_ident[EI_DATA] = ELFDATA2LSB; + elf->e_ident[EI_VERSION]= EV_CURRENT; + elf->e_ident[EI_OSABI] = ELFOSABI_NONE; + memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); + elf->e_type = ET_CORE; + elf->e_machine = EM_X86_64; + elf->e_version = EV_CURRENT; + elf->e_entry = 0; + elf->e_phoff = sizeof(Elf64_Ehdr); + elf->e_shoff = 0; + elf->e_flags = 0; + elf->e_ehsize = sizeof(Elf64_Ehdr); + elf->e_phentsize= sizeof(Elf64_Phdr); + elf->e_phnum = 0; + elf->e_shentsize= 0; + elf->e_shnum = 0; + elf->e_shstrndx = 0; + + /* PT_NOTE program headers. One per cpu*/ + nr_cpus = sysconf(_SC_NPROCESSORS_CONF); + if (nr_cpus < 0) { + return -1; + } + + /* Need to find a better way to determine per cpu notes section size. */ +#define MAX_NOTE_BYTES 1024 + for (i = 0; i < nr_cpus; i++) { + if (get_crash_notes_section_addr (i, ¬es_addr) < 0) { + /* This cpu is not present. Skip it. */ + continue; + } + + phdr = (Elf64_Phdr *) bufp; + bufp += sizeof(Elf64_Phdr); + phdr->p_type = PT_NOTE; + phdr->p_flags = 0; + phdr->p_offset = phdr->p_paddr = notes_addr; + phdr->p_vaddr = 0; + phdr->p_filesz = phdr->p_memsz = MAX_NOTE_BYTES; + /* Do we need any alignment of segments? */ + phdr->p_align = 0; + + /* Increment number of program headers. */ + (elf->e_phnum)++; + } + + /* Setup PT_LOAD type program header for every system RAM chunk. + * A seprate program header for Backup Region*/ + for (i = 0; i < CRASH_MAX_MEMORY_RANGES; i++) { + unsigned long long mstart, mend; + mstart = crash_memory_range[i].start; + mend = crash_memory_range[i].end; + if (!mstart && !mend) + break; + phdr = (Elf64_Phdr *) bufp; + bufp += sizeof(Elf64_Phdr); + phdr->p_type = PT_LOAD; + phdr->p_flags = PF_R|PF_W|PF_X; + if (mstart == BACKUP_START && mend == BACKUP_END) + phdr->p_offset = info->backup_start; + else + phdr->p_offset = mstart; + + /* Handle linearly mapped region.*/ + + /* Filling the vaddr conditionally as we have two linearly + * mapped regions here. One is __START_KERNEL_map 0 to 40 MB + * other one is PAGE_OFFSET */ + + if ((mend <= (MAXMEM - 1)) && mstart < KERNEL_TEXT_SIZE) + phdr->p_vaddr = mstart + __START_KERNEL_map; + else { + if (mend <= (MAXMEM - 1)) + phdr->p_vaddr = mstart + PAGE_OFFSET; + else + phdr->p_vaddr = -1ULL; + } + phdr->p_paddr = mstart; + phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; + /* Do we need any alignment of segments? */ + phdr->p_align = 0; + + /* Increment number of program headers. */ + (elf->e_phnum)++; + } + return 0; +} + +/* Prepares the crash memory elf32 headers and stores in supplied buffer. */ +static int prepare_crash_memory_elf32_headers(struct kexec_info *info, + void *buf, unsigned long size) +{ + Elf32_Ehdr *elf; + Elf32_Phdr *phdr; + int i; + char *bufp; + long int nr_cpus = 0; + unsigned long long notes_addr; + + bufp = (char*) buf; + + /* Setup ELF Header*/ + elf = (Elf32_Ehdr *) bufp; + bufp += sizeof(Elf32_Ehdr); + memcpy(elf->e_ident, ELFMAG, SELFMAG); + elf->e_ident[EI_CLASS] = ELFCLASS32; + elf->e_ident[EI_DATA] = ELFDATA2LSB; + elf->e_ident[EI_VERSION]= EV_CURRENT; + elf->e_ident[EI_OSABI] = ELFOSABI_NONE; + memset(elf->e_ident+EI_PAD, 0, EI_NIDENT-EI_PAD); + elf->e_type = ET_CORE; + elf->e_machine = EM_X86_64; + elf->e_version = EV_CURRENT; + elf->e_entry = 0; + elf->e_phoff = sizeof(Elf32_Ehdr); + elf->e_shoff = 0; + elf->e_flags = 0; + elf->e_ehsize = sizeof(Elf32_Ehdr); + elf->e_phentsize= sizeof(Elf32_Phdr); + elf->e_phnum = 0; + elf->e_shentsize= 0; + elf->e_shnum = 0; + elf->e_shstrndx = 0; + + /* PT_NOTE program headers. One per cpu*/ + nr_cpus = sysconf(_SC_NPROCESSORS_CONF); + if (nr_cpus < 0) { + return -1; + } + + /* Need to find a better way to determine per cpu notes section size. */ +#define MAX_NOTE_BYTES 1024 + for (i = 0; i < nr_cpus; i++) { + if (get_crash_notes_section_addr (i, ¬es_addr) < 0) { + /* This cpu is not present. Skip it. */ + return -1; + } + phdr = (Elf32_Phdr *) bufp; + bufp += sizeof(Elf32_Phdr); + phdr->p_type = PT_NOTE; + phdr->p_flags = 0; + phdr->p_offset = phdr->p_paddr = notes_addr; + phdr->p_vaddr = 0; + phdr->p_filesz = phdr->p_memsz = MAX_NOTE_BYTES; + /* Do we need any alignment of segments? */ + phdr->p_align = 0; + + /* Increment number of program headers. */ + (elf->e_phnum)++; + } + + /* Setup PT_LOAD type program header for every system RAM chunk. + * A seprate program header for Backup Region*/ + for (i = 0; i < CRASH_MAX_MEMORY_RANGES; i++) { + unsigned long long mstart, mend; + mstart = crash_memory_range[i].start; + mend = crash_memory_range[i].end; + if (!mstart && !mend) + break; + phdr = (Elf32_Phdr *) bufp; + bufp += sizeof(Elf32_Phdr); + phdr->p_type = PT_LOAD; + phdr->p_flags = PF_R|PF_W|PF_X; + if (mstart == BACKUP_START && mend == BACKUP_END) + phdr->p_offset = info->backup_start; + else + phdr->p_offset = mstart; + /* Handle linearly mapped region.*/ + + /* Filling the vaddr conditionally as we have two linearly + * mapped regions here. One is __START_KERNEL_map 0 to 40 MB + * other one is PAGE_OFFSET */ + + if (mend <= (MAXMEM - 1) && mstart < KERNEL_TEXT_SIZE) + phdr->p_vaddr = mstart + __START_KERNEL_map; + else { + if (mend <= (MAXMEM - 1)) + phdr->p_vaddr = mstart + PAGE_OFFSET; + else + phdr->p_vaddr = UINT_MAX; + } + phdr->p_paddr = mstart; + phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; + /* Do we need any alignment of segments? */ + phdr->p_align = 0; + /* Increment number of program headers. */ + (elf->e_phnum)++; + } + return 0; +} + diff --git a/kexec/arch/x86_64/crashdump-x86_64.h b/kexec/arch/x86_64/crashdump-x86_64.h new file mode 100644 index 0000000..2eb55db --- /dev/null +++ b/kexec/arch/x86_64/crashdump-x86_64.h @@ -0,0 +1,24 @@ +#ifndef CRASHDUMP_X86_64_H +#define CRASHDUMP_X86_64_H + +int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline, + unsigned long max_addr, unsigned long min_base); + +#define __START_KERNEL_map 0xffffffff80000000UL +#define PAGE_OFFSET 0xffff810000000000UL +#define __pa(x) (((unsigned long)(x)>=__START_KERNEL_map)?(unsigned long)(x) - (unsigned long)__START_KERNEL_map:(unsigned long)(x) - PAGE_OFFSET) + +#define MAXMEM 0x3fffffffffffUL + +/* Kernel text size */ +#define KERNEL_TEXT_SIZE (40UL*1024*1024) + +#define CRASH_MAX_MEMMAP_NR (KEXEC_MAX_SEGMENTS + 1) +#define CRASH_MAX_MEMORY_RANGES (MAX_MEMORY_RANGES + 2) + +/* Backup Region, First 640K of System RAM. */ +#define BACKUP_START 0x00000000 +#define BACKUP_END 0x0009ffff +#define BACKUP_SIZE (BACKUP_END - BACKUP_START + 1) + +#endif /* CRASHDUMP_X86_64_H */ diff --git a/kexec/arch/x86_64/include/arch/options.h b/kexec/arch/x86_64/include/arch/options.h index 75065b9..189ef46 100644 --- a/kexec/arch/x86_64/include/arch/options.h +++ b/kexec/arch/x86_64/include/arch/options.h @@ -6,7 +6,9 @@ #define OPT_SERIAL_BAUD (OPT_MAX+2) #define OPT_CONSOLE_VGA (OPT_MAX+3) #define OPT_CONSOLE_SERIAL (OPT_MAX+4) -#define OPT_ARCH_MAX (OPT_MAX+5) +#define OPT_ELF32_CORE (OPT_MAX+5) +#define OPT_ELF64_CORE (OPT_MAX+6) +#define OPT_ARCH_MAX (OPT_MAX+7) #define KEXEC_ARCH_OPTIONS \ KEXEC_OPTIONS \ @@ -15,6 +17,8 @@ { "serial-baud", 1, 0, OPT_SERIAL_BAUD }, \ { "console-vga", 0, 0, OPT_CONSOLE_VGA }, \ { "console-serial", 0, 0, OPT_CONSOLE_SERIAL }, \ + { "elf32-core-headers", 0, 0, OPT_ELF32_CORE }, \ + { "elf64-core-headers", 0, 0, OPT_ELF64_CORE }, \ #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR "" diff --git a/kexec/arch/x86_64/kexec-x86_64.c b/kexec/arch/x86_64/kexec-x86_64.c index 5c3349f..530adcf 100644 --- a/kexec/arch/x86_64/kexec-x86_64.c +++ b/kexec/arch/x86_64/kexec-x86_64.c @@ -30,6 +30,7 @@ #include "../../kexec-elf.h" #include "../../kexec-syscall.h" #include "kexec-x86_64.h" +#include "crashdump-x86_64.h" #include <arch/options.h> #define MAX_MEMORY_RANGES 64 @@ -136,6 +137,8 @@ void arch_usage(void) " --serial-baud=<buad_rate> Specify the serial port baud rate\n" " --console-vga Enable the vga console\n" " --console-serial Enable the serial console\n" + " --elf32-core-headers Prepare core headers in ELF32 format\n" + " --elf64-core-headers Prepare core headers in ELF64 format\n" ); } @@ -145,12 +148,14 @@ struct { uint32_t serial_baud; uint8_t console_vga; uint8_t console_serial; + int core_header_type; } arch_options = { .reset_vga = 0, .serial_base = 0x3f8, .serial_baud = 0, .console_vga = 0, .console_serial = 0, + .core_header_type = CORE_TYPE_ELF64, }; int arch_process_options(int argc, char **argv) @@ -214,6 +219,12 @@ int arch_process_options(int argc, char **argv) } arch_options.serial_baud = value; break; + case OPT_ELF32_CORE: + arch_options.core_header_type = CORE_TYPE_ELF32; + break; + case OPT_ELF64_CORE: + arch_options.core_header_type = CORE_TYPE_ELF64; + break; } } /* Reset getopt for the next pass; called in other source modules */ |