From patchwork Thu Aug 25 18:18:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 9300743 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AC1CE60459 for ; Fri, 26 Aug 2016 00:58:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E5F029384 for ; Fri, 26 Aug 2016 00:58:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 82DFE29406; Fri, 26 Aug 2016 00:58:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E613829384 for ; Fri, 26 Aug 2016 00:58:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752425AbcHZA6l (ORCPT ); Thu, 25 Aug 2016 20:58:41 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:58937 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751902AbcHZA6l (ORCPT ); Thu, 25 Aug 2016 20:58:41 -0400 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id u7PIGbfD027110 for ; Thu, 25 Aug 2016 14:18:51 -0400 Received: from e24smtp05.br.ibm.com (e24smtp05.br.ibm.com [32.104.18.26]) by mx0a-001b2d01.pphosted.com with ESMTP id 251w7wk1ta-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 25 Aug 2016 14:18:51 -0400 Received: from localhost by e24smtp05.br.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 25 Aug 2016 15:18:49 -0300 Received: from d24dlp02.br.ibm.com (9.18.248.206) by e24smtp05.br.ibm.com (10.172.0.141) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 25 Aug 2016 15:18:45 -0300 X-IBM-Helo: d24dlp02.br.ibm.com X-IBM-MailFrom: bauerman@linux.vnet.ibm.com X-IBM-RcptTo: linux-kernel@vger.kernel.org; linux-security-module@vger.kernel.org Received: from d24relay03.br.ibm.com (d24relay03.br.ibm.com [9.13.184.25]) by d24dlp02.br.ibm.com (Postfix) with ESMTP id D82751DC0054; Thu, 25 Aug 2016 14:18:35 -0400 (EDT) Received: from d24av01.br.ibm.com (d24av01.br.ibm.com [9.8.31.91]) by d24relay03.br.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u7PIIiqS6095144; Thu, 25 Aug 2016 15:18:44 -0300 Received: from d24av01.br.ibm.com (localhost [127.0.0.1]) by d24av01.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u7PIIh4n017631; Thu, 25 Aug 2016 15:18:44 -0300 Received: from hactar.ibm.com (ismaeljr.br.ibm.com [9.18.203.95] (may be forged)) by d24av01.br.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u7PIIdFh017437; Thu, 25 Aug 2016 15:18:42 -0300 From: Thiago Jung Bauermann To: kexec@lists.infradead.org Cc: linux-security-module@vger.kernel.org, linux-ima-devel@lists.sourceforge.net, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, Eric Biederman , Dave Young , Vivek Goyal , Baoquan He , Michael Ellerman , Stewart Smith , Mimi Zohar , Eric Richter , Andrew Morton , Balbir Singh , Thiago Jung Bauermann Subject: [PATCH v3 2/5] powerpc: kexec_file: Add buffer hand-over support for the next kernel Date: Thu, 25 Aug 2016 15:18:28 -0300 X-Mailer: git-send-email 1.9.1 In-Reply-To: <1472149111-30598-1-git-send-email-bauerman@linux.vnet.ibm.com> References: <1472149111-30598-1-git-send-email-bauerman@linux.vnet.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16082518-0032-0000-0000-0000027A5197 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16082518-0033-0000-0000-00000EC43401 Message-Id: <1472149111-30598-3-git-send-email-bauerman@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-08-25_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1608250209 Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The buffer hand-over mechanism allows the currently running kernel to pass data to kernel that will be kexec'd via a kexec segment. The second kernel can check whether the previous kernel sent data and retrieve it. This is the architecture-specific part. Signed-off-by: Thiago Jung Bauermann --- arch/powerpc/include/asm/kexec.h | 12 +++- arch/powerpc/kernel/kexec_elf_64.c | 2 +- arch/powerpc/kernel/machine_kexec_64.c | 114 +++++++++++++++++++++++++++++++-- 3 files changed, 120 insertions(+), 8 deletions(-) diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h index 09813f3ea3c2..cfc702f60726 100644 --- a/arch/powerpc/include/asm/kexec.h +++ b/arch/powerpc/include/asm/kexec.h @@ -92,12 +92,20 @@ static inline bool kdump_in_progress(void) } #ifdef CONFIG_KEXEC_FILE +#define ARCH_HAS_KIMAGE_ARCH + +struct kimage_arch { + phys_addr_t handover_buffer_addr; + unsigned long handover_buffer_size; +}; + 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); +int setup_new_fdt(const struct kimage *image, void *fdt, + unsigned long initrd_load_addr, unsigned long initrd_len, + const char *cmdline); bool find_debug_console(const void *fdt); #endif /* CONFIG_KEXEC_FILE */ diff --git a/arch/powerpc/kernel/kexec_elf_64.c b/arch/powerpc/kernel/kexec_elf_64.c index a2b8ddc44b74..6127a495a774 100644 --- a/arch/powerpc/kernel/kexec_elf_64.c +++ b/arch/powerpc/kernel/kexec_elf_64.c @@ -209,7 +209,7 @@ void *elf64_load(struct kimage *image, char *kernel_buf, goto out; } - ret = setup_new_fdt(fdt, initrd_load_addr, initrd_len, cmdline); + ret = setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline); if (ret) goto out; diff --git a/arch/powerpc/kernel/machine_kexec_64.c b/arch/powerpc/kernel/machine_kexec_64.c index 5d59ccdc39f5..59f1e5d4b6c4 100644 --- a/arch/powerpc/kernel/machine_kexec_64.c +++ b/arch/powerpc/kernel/machine_kexec_64.c @@ -489,6 +489,60 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) return image->fops->cleanup(image->image_loader_data); } +bool kexec_can_hand_over_buffer(void) +{ + return true; +} + +int arch_kexec_add_handover_buffer(struct kimage *image, + unsigned long load_addr, unsigned long size) +{ + image->arch.handover_buffer_addr = load_addr; + image->arch.handover_buffer_size = size; + + return 0; +} + +int kexec_get_handover_buffer(void **addr, unsigned long *size) +{ + int ret; + u64 start_addr, end_addr; + + ret = of_property_read_u64(of_chosen, + "linux,kexec-handover-buffer-start", + &start_addr); + if (ret == -EINVAL) + return -ENOENT; + else if (ret) + return -EINVAL; + + ret = of_property_read_u64(of_chosen, "linux,kexec-handover-buffer-end", + &end_addr); + if (ret == -EINVAL) + return -ENOENT; + else if (ret) + return -EINVAL; + + *addr = __va(start_addr); + /* -end is the first address after the buffer. */ + *size = end_addr - start_addr; + + return 0; +} + +int kexec_free_handover_buffer(void) +{ + int ret; + void *addr; + unsigned long size; + + ret = kexec_get_handover_buffer(&addr, &size); + if (ret) + return ret; + + return memblock_free((phys_addr_t) addr, size); +} + /** * arch_kexec_walk_mem() - call func(data) for each unreserved memory block * @kbuf: Context info for the search. Also passed to @func. @@ -686,9 +740,52 @@ int setup_purgatory(struct kimage *image, const void *slave_code, return 0; } -/* - * setup_new_fdt() - modify /chosen and memory reservation for the next kernel - * @fdt: +/** + * setup_handover_buffer() - add properties and reservation for the handover buffer + * @image: kexec image being loaded. + * @fdt: Flattened device tree for the next kernel. + * @chosen_node: Offset to the chosen node. + * + * Return: 0 on success, negative errno on error. + */ +static int setup_handover_buffer(const struct kimage *image, void *fdt, + int chosen_node) +{ + int ret; + + if (image->arch.handover_buffer_addr == 0) + return 0; + + ret = fdt_setprop_u64(fdt, chosen_node, + "linux,kexec-handover-buffer-start", + image->arch.handover_buffer_addr); + if (ret < 0) + return -EINVAL; + + /* -end is the first address after the buffer. */ + ret = fdt_setprop_u64(fdt, chosen_node, + "linux,kexec-handover-buffer-end", + image->arch.handover_buffer_addr + + image->arch.handover_buffer_size); + if (ret < 0) + return -EINVAL; + + ret = fdt_add_mem_rsv(fdt, image->arch.handover_buffer_addr, + image->arch.handover_buffer_size); + if (ret) + return -EINVAL; + + pr_debug("kexec handover buffer at 0x%llx, size = 0x%lx\n", + image->arch.handover_buffer_addr, + image->arch.handover_buffer_size); + + return 0; +} + +/** + * setup_new_fdt() - modify /chosen and memory reservations for the next kernel + * @image: kexec image being loaded. + * @fdt: Flattened device tree for the next kernel. * @initrd_load_addr: Address where the next initrd will be loaded. * @initrd_len: Size of the next initrd, or 0 if there will be none. * @cmdline: Command line for the next kernel, or NULL if there will @@ -696,8 +793,9 @@ int setup_purgatory(struct kimage *image, const void *slave_code, * * Return: 0 on success, or negative errno on error. */ -int setup_new_fdt(void *fdt, unsigned long initrd_load_addr, - unsigned long initrd_len, const char *cmdline) +int setup_new_fdt(const struct kimage *image, void *fdt, + unsigned long initrd_load_addr, unsigned long initrd_len, + const char *cmdline) { uint64_t oldfdt_addr; int i, ret, chosen_node; @@ -839,6 +937,12 @@ int setup_new_fdt(void *fdt, unsigned long initrd_load_addr, } } + ret = setup_handover_buffer(image, fdt, chosen_node); + if (ret) { + pr_err("Error setting up the new device tree.\n"); + return ret; + } + ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0); if (ret) { pr_err("Error setting up the new device tree.\n");