From patchwork Thu Aug 24 08:18:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 110889 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp5321326qge; Thu, 24 Aug 2017 01:25:24 -0700 (PDT) X-Received: by 10.84.232.137 with SMTP id i9mr6245342plk.240.1503563124589; Thu, 24 Aug 2017 01:25:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503563124; cv=none; d=google.com; s=arc-20160816; b=pi2N3LMxiQDmTdxwYqI0jvJdoOp4S+c1z+0I64lCFuB1X92AYfJdRRsVpp9rvh8mMK b+kNJSNDK6dr5h1JlDdxdpuiISaO6TF/fsJ3Io9ZrxZPs+QCkHGOtzcmljq58lds75a3 +BzbOCNtwr4U0tIejciaR1RNhxqMvNzZV0W/oVjCr+7j+lPK3TIUlA3P8N0mu4KPHwkI gkqVO1A+KOAf8hvZcLCH5LvRsObxnXgXnhO/ya/RVYV9W69PNyFbf9H9/g6RUQFkVYtN HqHKRvRjUcE6OXuQSgEe1gtw9QKQUlSrz8OiT5VTj84iN6Juj/zwjim3cJc0hFuq4Hze ET1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=5ObHuYfLHgQhQMhvEhRdQESvV4pllmIw9h/uQrBNm+o=; b=NifQP5nI90NH5FJkPi0g/jp0zX3rmMQnmruJsqNTcjakut8RqwW9lTG0/HV0sBkf+K U3ZzWJZ3bFH/p7OacBDTj4ucT+jf6093QYod1caHaiyyOVATXFzg9i1TlV/723uZ9xUR e9TmJZ7XZH4qxwUmwQ2+dU8X1ZFuVOWHxq7piC967DSsgogMtBqyp0JvPkFrnoR4JGTa oaUAkzniOBHCn3sJLkbBtPNwz19cl7rJkj3IvsRYD4DTzjOGlCOvkirt6q8qsW1NKxdg W+r3BEfUZwZiBD5Na2J+GCBF1D6VXruuDz0lEP849xcK6vpjYfJ8R02pJpDRIg05VzNH oM5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=im8Qioyh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b14si2271006pga.562.2017.08.24.01.25.24; Thu, 24 Aug 2017 01:25:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=im8Qioyh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752443AbdHXIZU (ORCPT + 26 others); Thu, 24 Aug 2017 04:25:20 -0400 Received: from mail-pg0-f44.google.com ([74.125.83.44]:36030 "EHLO mail-pg0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751484AbdHXITE (ORCPT ); Thu, 24 Aug 2017 04:19:04 -0400 Received: by mail-pg0-f44.google.com with SMTP id 83so13455830pgb.3 for ; Thu, 24 Aug 2017 01:19:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=5ObHuYfLHgQhQMhvEhRdQESvV4pllmIw9h/uQrBNm+o=; b=im8Qioyhfr8JYgzVEV0n0vSkBLL+jpmuLUhiyfJ4KTOnDEqY+x3A1ayuw0mHVR7ZT9 x5bZeKRYV6V5eNCNqRhMGivk7fmzprFiPdYaQAJZNDVHb/M3bss1t7MVHYk/7uMHf4C3 i/ZT5RJzjWlRtXU8DbBHd5bL5DQ1XitxYoUag= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=5ObHuYfLHgQhQMhvEhRdQESvV4pllmIw9h/uQrBNm+o=; b=qUh+f22QUFe8k5liE8908LZAKEMFIGvwgHoQJ61d3e74DPiohg/4vdfjOrciPw/QJU gP0dFWbMvDhzFzF1l5eK9YQ+pS0tIEbZuU4j1BUkWed1GIekUXu1hiX3BPhDxRznv2G9 fVG1bPMm6nkFGAUXiBuCsv63pCjGbi912DulnGEOGzXH6Vrzm3WikPi++YHbZYo/AWX4 VmWKRIbqoNZ3Rpvu29unCxeUWbf7H+UAlNkLDc4NyG50GFF/CjNeQcqM3ZwXfyOe3CQi JlmgCoGIzEGuHREpgSKvcy3j+7AtGAxxpPxzlu0bMcROtYYSkVLo8/a1KoMwAZyWtuRY 7nqw== X-Gm-Message-State: AHYfb5gLDZ23OPm6lF+h+f0mNiZ82ig7i/Ufzdw/9ISFVSisnaaDFBjp gXUZsgW7JTGjRb1T X-Received: by 10.101.76.140 with SMTP id m12mr5414687pgt.160.1503562743533; Thu, 24 Aug 2017 01:19:03 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id b9sm7323929pfl.13.2017.08.24.01.19.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Aug 2017 01:19:03 -0700 (PDT) From: AKASHI Takahiro To: catalin.marinas@arm.com, will.deacon@arm.com, bauerman@linux.vnet.ibm.com, dhowells@redhat.com, vgoyal@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, akpm@linux-foundation.org, mpe@ellerman.id.au, dyoung@redhat.com, bhe@redhat.com, arnd@arndb.de, ard.biesheuvel@linaro.org Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH 11/14] arm64: kexec_file: set up for crash dump adding elf core header Date: Thu, 24 Aug 2017 17:18:08 +0900 Message-Id: <20170824081811.19299-12-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170824081811.19299-1-takahiro.akashi@linaro.org> References: <20170824081811.19299-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org load_crashdump_segments() creates and loads a memory segment of elf core header for crash dump. "linux,usable-memory-range" and "linux,elfcorehdr" will add to the 2nd kernel's device-tree blob. The logic of this cod is also from kexec-tools. Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/include/asm/kexec.h | 5 ++ arch/arm64/kernel/machine_kexec_file.c | 145 +++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) -- 2.14.1 diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index ebc4aaa707ae..46b21512efbe 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -98,6 +98,10 @@ static inline void crash_post_resume(void) {} struct kimage_arch { void *dtb_buf; + /* Core ELF header buffer */ + void *elf_headers; + unsigned long elf_headers_sz; + unsigned long elf_load_addr; }; struct kimage; @@ -109,6 +113,7 @@ extern int load_other_segments(struct kimage *image, unsigned long kernel_load_addr, char *initrd, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len); +extern int load_crashdump_segments(struct kimage *image); #endif #endif /* __ASSEMBLY__ */ diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c index cd12e451e474..012063307001 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -281,6 +281,77 @@ int arch_kexec_walk_mem(struct kexec_buf *kbuf, int (*func)(u64, u64, void *)) return walk_system_ram_res(0, ULONG_MAX, kbuf, func); } +static int __init arch_kexec_file_init(void) +{ + /* Those values are used later on loading the kernel */ + __dt_root_addr_cells = dt_root_addr_cells; + __dt_root_size_cells = dt_root_size_cells; + + return 0; +} +late_initcall(arch_kexec_file_init); + +#define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) +#define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE)) + +static int fdt_prop_len(const char *prop_name, int len) +{ + return (strlen(prop_name) + 1) + + sizeof(struct fdt_property) + + FDT_TAGALIGN(len); +} + +static bool cells_size_fitted(unsigned long base, unsigned long size) +{ + /* if *_cells >= 2, cells can hold 64-bit values anyway */ + if ((__dt_root_addr_cells == 1) && (base >= (1ULL << 32))) + return false; + + if ((__dt_root_size_cells == 1) && (size >= (1ULL << 32))) + return false; + + return true; +} + +static void fill_property(void *buf, u64 val64, int cells) +{ + u32 val32; + int i; + + if (cells == 1) { + val32 = cpu_to_fdt32((u32)val64); + memcpy(buf, &val32, sizeof(val32)); + } else { + for (i = 0; i < (cells * sizeof(u32) - sizeof(u64)); i++) + *(char *)buf++ = 0; + + val64 = cpu_to_fdt64(val64); + memcpy(buf, &val64, sizeof(val64)); + } +} + +static int fdt_setprop_range(void *fdt, int nodeoffset, const char *name, + unsigned long addr, unsigned long size) +{ + u64 range[2]; + void *prop; + size_t buf_size; + int result; + + prop = range; + buf_size = (__dt_root_addr_cells + __dt_root_size_cells) * sizeof(u32); + + fill_property(prop, addr, __dt_root_addr_cells); + prop += __dt_root_addr_cells * sizeof(u32); + + fill_property(prop, size, __dt_root_size_cells); + prop += __dt_root_size_cells * sizeof(u32); + + result = fdt_setprop(fdt, nodeoffset, name, range, buf_size); + + return result; +} + int setup_dtb(struct kimage *image, unsigned long initrd_load_addr, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len, @@ -293,10 +364,26 @@ int setup_dtb(struct kimage *image, int range_len; int ret; + /* check ranges against root's #address-cells and #size-cells */ + if (image->type == KEXEC_TYPE_CRASH && + (!cells_size_fitted(image->arch.elf_load_addr, + image->arch.elf_headers_sz) || + !cells_size_fitted(crashk_res.start, + crashk_res.end - crashk_res.start + 1))) { + pr_err("Crash memory region doesn't fit into DT's root cell sizes.\n"); + ret = -EINVAL; + goto out_err; + } + /* duplicate dt blob */ buf_size = fdt_totalsize(initial_boot_params); range_len = (__dt_root_addr_cells + __dt_root_size_cells) * sizeof(u32); + if (image->type == KEXEC_TYPE_CRASH) + buf_size += fdt_prop_len("linux,elfcorehdr", range_len) + + fdt_prop_len("linux,usable-memory-range", + range_len); + if (initrd_load_addr) buf_size += fdt_prop_len("initrd-start", sizeof(u64)) + fdt_prop_len("initrd-end", sizeof(u64)); @@ -318,6 +405,23 @@ int setup_dtb(struct kimage *image, if (nodeoffset < 0) goto out_err; + if (image->type == KEXEC_TYPE_CRASH) { + /* add linux,elfcorehdr */ + ret = fdt_setprop_range(buf, nodeoffset, "linux,elfcorehdr", + image->arch.elf_load_addr, + image->arch.elf_headers_sz); + if (ret) + goto out_err; + + /* add linux,usable-memory-range */ + ret = fdt_setprop_range(buf, nodeoffset, + "linux,usable-memory-range", + crashk_res.start, + crashk_res.end - crashk_res.start + 1); + if (ret) + goto out_err; + } + /* add bootargs */ if (cmdline) { ret = fdt_setprop(buf, nodeoffset, "bootargs", @@ -452,3 +556,44 @@ int load_other_segments(struct kimage *image, unsigned long kernel_load_addr, image->arch.dtb_buf = NULL; return ret; } + +int load_crashdump_segments(struct kimage *image) +{ + void *elf_addr; + unsigned long elf_sz; + struct kexec_buf kbuf; + int ret; + + if (image->type != KEXEC_TYPE_CRASH) + return 0; + + /* Prepare elf headers and add a segment */ + ret = prepare_elf_headers(image, &elf_addr, &elf_sz); + if (ret) { + pr_err("Preparing elf core header failed\n"); + return ret; + } + + kbuf.image = image; + kbuf.buffer = elf_addr; + kbuf.bufsz = elf_sz; + kbuf.memsz = elf_sz; + kbuf.buf_align = PAGE_SIZE; + kbuf.buf_min = crashk_res.start; + kbuf.buf_max = crashk_res.end + 1; + kbuf.top_down = 1; + + ret = kexec_add_buffer(&kbuf); + if (ret) { + vfree(elf_addr); + return ret; + } + image->arch.elf_headers = elf_addr; + image->arch.elf_headers_sz = elf_sz; + image->arch.elf_load_addr = kbuf.mem; + + pr_debug("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + image->arch.elf_load_addr, elf_sz, elf_sz); + + return ret; +}