From patchwork Thu Feb 22 11:17:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129204 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481221lja; Thu, 22 Feb 2018 03:19:02 -0800 (PST) X-Google-Smtp-Source: AH8x224lXyr4tFZQ3xa1eJJ6VSZkOcgt5ZVuA4CihOZdluRK4XIqFFIa1Tb9p/6AG+h++x3u4ECI X-Received: by 2002:a17:902:2bc5:: with SMTP id l63-v6mr6361707plb.108.1519298342005; Thu, 22 Feb 2018 03:19:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298341; cv=none; d=google.com; s=arc-20160816; b=eCZi+LtVjM4aIxUyPtUrBi12hnKDBApll3v/gIKp21vgwlOv1ISaVBnLtNMVoPoMny IXZIm8dNKuMXK4hviJ8y/++zWEgoPDUNctGOzFWqEJHJEhj3nJDcpfL/DXa3N0uhZQk4 PRtntYt2Ek+UZn3VTocP8fgf2oXw7OAD99MmDHfZeMEPOTW16ersGYSxQbhUe4B736hv rEfcxPS4JzznCgkOCes5dk+4vEBer2AT4GkYyxxixBjka1wrjNkrN/XWt/VgUvUJMyDL Y1IdOA7Np8PYVhcxuagn/NxSLZXX5Ma8TAmtfLdU6zucd7aQXrqEi6S2Zlj90S/YgCMV 7B9A== 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=7A4jfFU8caPfA3i6HwoNwb39+7mY+fpdaRh+dIM26SA=; b=sHna3nLwmLNSXBvbmyTajqQPwgIhkDY5usP9zwWB9qUW/GmFBegU1x7ZIGqNaSopSB EAWOywS20DsDSr6ChyYmi435j11FdPbHTe1viwMiK0pCMvy2D6U0/VXfo4oK8nzAzIB4 qwiVHdEmV3oBpkoGDdTj/niDRbtEIvxjDAJKKi31wUfz0uRzdvH8mhIUygjjMymRKjAm RwdCgolLgzWhxzBhnZnR50TkMoZBr0f4eHYOLWjZ0/oOqxGR8wQGSMdIT4n01KVPPtt+ bZ7cIclhbPVuAjRDSGEskc9SnAOSlHusK1wma+BHCAiQdMH/rRk9s3g/ZykmDzQavoLr wEEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bsOk7Z96; 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 e17si7343232pgo.189.2018.02.22.03.19.01; Thu, 22 Feb 2018 03:19:01 -0800 (PST) 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=bsOk7Z96; 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 S1753741AbeBVLS6 (ORCPT + 28 others); Thu, 22 Feb 2018 06:18:58 -0500 Received: from mail-it0-f65.google.com ([209.85.214.65]:54940 "EHLO mail-it0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753531AbeBVLS4 (ORCPT ); Thu, 22 Feb 2018 06:18:56 -0500 Received: by mail-it0-f65.google.com with SMTP id p204so5842380itc.4 for ; Thu, 22 Feb 2018 03:18:56 -0800 (PST) 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=7A4jfFU8caPfA3i6HwoNwb39+7mY+fpdaRh+dIM26SA=; b=bsOk7Z96vGfOWYpv0peGIIWdiLR7zmtZcYqg2t2o8PJDyqaTz98JrH+V0lO+l9vpXU uj/c288M6NSvLMG3/g2ydpCZMVcmBkz+U9W42a1UqV2XDKItPQzdn/oidTw9gheE5tKh VwC1YGO+4LwNHu5SnwNY92ea3JCzIwYfdQcos= 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=7A4jfFU8caPfA3i6HwoNwb39+7mY+fpdaRh+dIM26SA=; b=b6zSaWMzu/m/SJgJz9tgChJLh3PQREEVRovdqIZwDyFfUD2XHcHBGYWydRvjLkeOZm hBtj2IwgopDvoKNQ5XhHHCUWuKHyhkbHXw7/d8UBQ7q8Kb0Liap7oPLCm656B5FrrGr3 fmEyvliHRItXlqzxXs2maQKOBX39cCp5WMmzSJB5wLF24b/qXJQ7wqIG/FI/HDQXWF/4 SL+K7s0mpNu1o5PWhwS3fNXEwTx6ua2VmXbeajSXyx8B986RhZPFF7lEux81HhlpUZmE 0JePZ6Qf6RctF8QskMwroLVRThM+cneTo9g/nHAER4D7xQntzYs3xqzoVGQjDXJGz99T G44A== X-Gm-Message-State: APf1xPD3JxoASCO4LdTRU+Nrj0puaPB9TsN/Nr/ekVIZxOBIZQFO9Bst gqlPJKFPhCxGmaFoL0fVPXIiEg== X-Received: by 10.36.147.133 with SMTP id y127mr7130430itd.12.1519298336341; Thu, 22 Feb 2018 03:18:56 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id y6sm27550818iod.7.2018.02.22.03.18.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:18:55 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro , Linus Torvalds Subject: [PATCH v8 01/13] resource: add walk_system_ram_res_rev() Date: Thu, 22 Feb 2018 20:17:20 +0900 Message-Id: <20180222111732.23051-2-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This function, being a variant of walk_system_ram_res() introduced in commit 8c86e70acead ("resource: provide new functions to walk through resources"), walks through a list of all the resources of System RAM in reversed order, i.e., from higher to lower. It will be used in kexec_file implementation on arm64. Signed-off-by: AKASHI Takahiro Cc: Vivek Goyal Cc: Andrew Morton Cc: Linus Torvalds --- include/linux/ioport.h | 3 +++ kernel/resource.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) -- 2.16.2 diff --git a/include/linux/ioport.h b/include/linux/ioport.h index da0ebaec25f0..f12d95fe038b 100644 --- a/include/linux/ioport.h +++ b/include/linux/ioport.h @@ -277,6 +277,9 @@ extern int walk_system_ram_res(u64 start, u64 end, void *arg, int (*func)(struct resource *, void *)); extern int +walk_system_ram_res_rev(u64 start, u64 end, void *arg, + int (*func)(struct resource *, void *)); +extern int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end, void *arg, int (*func)(struct resource *, void *)); diff --git a/kernel/resource.c b/kernel/resource.c index e270b5048988..bdaa93407f4c 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include @@ -486,6 +488,61 @@ int walk_mem_res(u64 start, u64 end, void *arg, arg, func); } +int walk_system_ram_res_rev(u64 start, u64 end, void *arg, + int (*func)(struct resource *, void *)) +{ + struct resource res, *rams; + int rams_size = 16, i; + int ret = -1; + + /* create a list */ + rams = vmalloc(sizeof(struct resource) * rams_size); + if (!rams) + return ret; + + res.start = start; + res.end = end; + res.flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; + i = 0; + while ((res.start < res.end) && + (!find_next_iomem_res(&res, IORES_DESC_NONE, true))) { + if (i >= rams_size) { + /* re-alloc */ + struct resource *rams_new; + int rams_new_size; + + rams_new_size = rams_size + 16; + rams_new = vmalloc(sizeof(struct resource) + * rams_new_size); + if (!rams_new) + goto out; + + memcpy(rams_new, rams, + sizeof(struct resource) * rams_size); + vfree(rams); + rams = rams_new; + rams_size = rams_new_size; + } + + rams[i].start = res.start; + rams[i++].end = res.end; + + res.start = res.end + 1; + res.end = end; + } + + /* go reverse */ + for (i--; i >= 0; i--) { + ret = (*func)(&rams[i], arg); + if (ret) + break; + } + +out: + vfree(rams); + return ret; +} + #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY) /* From patchwork Thu Feb 22 11:17:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129205 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481403lja; Thu, 22 Feb 2018 03:19:12 -0800 (PST) X-Google-Smtp-Source: AH8x225XOgKmwaixlW/NljwTGqDvoI01TcAvaXoDPfg8tpVvCH3xhIwMRDvro9qPXAOlnVP4CIGj X-Received: by 10.98.87.68 with SMTP id l65mr6523399pfb.72.1519298352664; Thu, 22 Feb 2018 03:19:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298352; cv=none; d=google.com; s=arc-20160816; b=oBh2ljUBajCdEIF4/ONAIjQM+G9+gxlQprN43cciJfUCUntkRL+tuYVAjVwWcLIgw7 6Hm8J3e7Zym61E1Buw/wiqWgodrHNJt4+s49CAJEIK0wpBMeXCYeXDCjFz6Wr8MiLHth ST+zydlO2k5Qg/vgubGl2zibB+d9bq/qCw45ACcWGyUTujfL4i7NATKCejZAt6TnMval pdWfO0h5KhDNiQ8PmCsIUK2rT+rjRbNkAr9MpoEEV/r5dXyukITFjN+mTw77HJ7T5+bF yJy9dCZx65MEGYGxWzh6tdyi7oOlJ2L2tN0aPMblpWX5S0N9rlWdBAx16bM+ZgLhX9Gd vwQg== 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=hBCJG4EDW23Kq3k1dQBmwbwyd2oPgAlLdwNxaE9ZwLs=; b=OCpC5ljDnSxoX++SmGtCdOvHM55hQjLiAfergBmmMPmG31In/i7NHHPBfeHFlzFsg9 O6VbB/V5iLpBKhMkGWH8BHjLiJ5Os/NYYPNHTTapGiJ1FvenioEJMsswBHwjGkahfDXZ RHm6df248RCzeo4ZBUnv7VGuHnNg2AS9QJWxBKlvSsIKfk0d2V3ODF0kIDKS+ECQ6Ro+ 80FzsfC3BtuGx3dTQoE7sl+ynEuKVqFBgZqcqqlBWX0Zsshh/7kcn4IUw3VaVpd/kI4v Ye4AlaaML0hJg9hd+oN2fZxvK3xIszcxuWbZwS2T0X+DQ0CIcNME8GulvYIve6pCppbm +Izw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gWoHB67g; 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 34-v6si950366plc.368.2018.02.22.03.19.12; Thu, 22 Feb 2018 03:19:12 -0800 (PST) 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=gWoHB67g; 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 S932078AbeBVLTH (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:07 -0500 Received: from mail-pf0-f193.google.com ([209.85.192.193]:44849 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753746AbeBVLTD (ORCPT ); Thu, 22 Feb 2018 06:19:03 -0500 Received: by mail-pf0-f193.google.com with SMTP id 17so1943341pfw.11 for ; Thu, 22 Feb 2018 03:19:03 -0800 (PST) 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=hBCJG4EDW23Kq3k1dQBmwbwyd2oPgAlLdwNxaE9ZwLs=; b=gWoHB67g1SU0tLXdMmIfiQAcGH0NP9/ZyP4y3JCGgtK1a5LZbLE/jan3ph/+Y/69tO MAlyf6wUCY6wlrAhNcHwHhWYSXmRshBxc4ROoqz/er95toCmBzfZPAQlif4vvwxbCuQ0 aSwV8xj8LC82ODKxLxZuQvIsRMmBoHqKu7k9c= 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=hBCJG4EDW23Kq3k1dQBmwbwyd2oPgAlLdwNxaE9ZwLs=; b=fb7fYc1NgL6XUq55FTXuXjSYRQl2ppWwKAa4uVKQzo+6ffpv9B6ubRuAw2XLdUGLuK Bs9+42op2KKgrpJXspsrAzC4CV+E9H+20wCd6YugrmubH2o2ems5jj1kEdE+m4rbUuqe ygwXqsPc6vrNRfZixkAW9Kr9rYZg1jyb+klSPmlFdxlwhgD9N28WQYYcgcPu72WJ7ec+ Zy2R1Je+Cwm69x/f91vzFsdfebRX0tGQ541t/nXwcAgVbD8Z1bbz5hmQWrbVDiord4Xz 1G7rMOBnEfnxeB/KEmErvantioBLBNK0/uTuegGHMBBXEYr7orW05fgJjUuxF1WfvKbD 4Ltw== X-Gm-Message-State: APf1xPCCu9hUsB5q+aJQP6en8mu0oc40M0cQY9vokjJGJBdXegRHIkF7 OaHo/w/qbyE2FNSGv00OFypsSQ== X-Received: by 10.98.63.147 with SMTP id z19mr6459784pfj.221.1519298343128; Thu, 22 Feb 2018 03:19:03 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id g67sm2473581pgc.60.2018.02.22.03.19.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:02 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 02/13] kexec_file: make an use of purgatory optional Date: Thu, 22 Feb 2018 20:17:21 +0900 Message-Id: <20180222111732.23051-3-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On arm64, no trampline code between old kernel and new kernel will be required in kexec_file implementation. This patch introduces a new configuration, ARCH_HAS_KEXEC_PURGATORY, and allows related code to be compiled in only if necessary. Signed-off-by: AKASHI Takahiro Cc: Dave Young Cc: Vivek Goyal Cc: Baoquan He --- arch/powerpc/Kconfig | 3 +++ arch/x86/Kconfig | 3 +++ kernel/kexec_file.c | 6 ++++++ 3 files changed, 12 insertions(+) -- 2.16.2 diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index 73ce5dd07642..c32a181a7cbb 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -552,6 +552,9 @@ config KEXEC_FILE for kernel and initramfs as opposed to a list of segments as is the case for the older kexec call. +config ARCH_HAS_KEXEC_PURGATORY + def_bool KEXEC_FILE + config RELOCATABLE bool "Build a relocatable kernel" depends on PPC64 || (FLATMEM && (44x || FSL_BOOKE)) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index c1236b187824..f031c3efe47e 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2019,6 +2019,9 @@ config KEXEC_FILE for kernel and initramfs as opposed to list of segments as accepted by previous system call. +config ARCH_HAS_KEXEC_PURGATORY + def_bool KEXEC_FILE + config KEXEC_VERIFY_SIG bool "Verify kernel signature during kexec_file_load() syscall" depends on KEXEC_FILE diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index e5bcd94c1efb..990adae52151 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -26,7 +26,11 @@ #include #include "kexec_internal.h" +#ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY static int kexec_calculate_store_digests(struct kimage *image); +#else +static int kexec_calculate_store_digests(struct kimage *image) { return 0; }; +#endif /* Architectures can provide this probe function */ int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, @@ -520,6 +524,7 @@ int kexec_add_buffer(struct kexec_buf *kbuf) return 0; } +#ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY /* Calculate and store the digest of segments */ static int kexec_calculate_store_digests(struct kimage *image) { @@ -1022,3 +1027,4 @@ int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, return 0; } +#endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */ From patchwork Thu Feb 22 11:17:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129206 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481428lja; Thu, 22 Feb 2018 03:19:14 -0800 (PST) X-Google-Smtp-Source: AH8x2240xdhbUSDNMtiNLMUEPYiV83Wt6ItgNbvrlMykD4LOv3xaGHGCx4ZhsOVlHlRgrcCXwcuS X-Received: by 2002:a17:902:7486:: with SMTP id h6-v6mr6203108pll.216.1519298354227; Thu, 22 Feb 2018 03:19:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298354; cv=none; d=google.com; s=arc-20160816; b=dE+ySieH62atAK2YIA80fL5/AdCeIl2kUlvMTbzgyFjJ7FIzbZ6ipIQMoJytneMEMk bqmreshNfAU9fYUngQqV/a7IqB7B9b10CaVoPqCw/grGP0kTddq877BGJ27ou4efNvur 63VCIMLH7Okn7FsHWIhL1iWX/nDAEQtv2vwVnAfqpz0w8co8DJqs/8IxKiL9zPbR7UJE BSJaYLCsD76ZIbRG1bKGtxxwSWy+HTAyozL3B+5LOJqaAW7Ug6TUi/Fyg+7x3R31DLCJ 7H7Oq60XYkz+bAAPb/xEV92L4p2dcg1Jle/6eOA8ggKRDPIv9i5A/TTLEJnfnbvPQIVH QfzQ== 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=Il0NY7dx9L75+0Qy+Cm567MZczyZ9floK17NAH1/Ogc=; b=vmxyYWurVQwM1YIIHJuUQFKR+lrpkRDjOr88NoHt/uJ3cEuBJNEh5EfobFaKiKZLfU ZXkCnY80z2veZsUdlHCeL/bvdmi7YHY/YyS+dfU43MCnzKJvy4d3e8dhuSz9rkzbHA3a uZNAigvkQVSkII+ECDqnqImWyQJRJ/OK5781YeQ+xS4taO94bjgDsypH74cSkjhd88SA ECL/0V9C2In35ORLxpR3lDRz41xyd/Mh+OOo/bMpDsTwUcXoms65yAYgr2aNc1TZ+4Sy ZhZEVOfXkpuH1ytAbe7kSmg4mNP3zzNf7VbibUtG5bhkYaaRTAHPI7vm9hSDX5IkEd+E kDEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=IfiTt6E9; 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 34-v6si950366plc.368.2018.02.22.03.19.13; Thu, 22 Feb 2018 03:19:14 -0800 (PST) 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=IfiTt6E9; 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 S932099AbeBVLTK (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:10 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:45891 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753399AbeBVLTI (ORCPT ); Thu, 22 Feb 2018 06:19:08 -0500 Received: by mail-pf0-f195.google.com with SMTP id j24so1945235pff.12 for ; Thu, 22 Feb 2018 03:19:08 -0800 (PST) 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=Il0NY7dx9L75+0Qy+Cm567MZczyZ9floK17NAH1/Ogc=; b=IfiTt6E99wEJnYwuys9PI+5W6JzG6CYWJ2iRNkkRtyJK+mD7DhtVKBvryDfSnSYPpb DDWjc2Kj57A39wOjKk74JYp8RAcaJ4NsbX5+dD6o+GNImaH0Qd73wXsaSr45A80PacRX rOE89zh74seMPLjaf3CZpN/JjFp8fBdFg4TQ0= 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=Il0NY7dx9L75+0Qy+Cm567MZczyZ9floK17NAH1/Ogc=; b=JNK2pD/Z92RlSXPdeL58UuubHKYXYJBy2LeRpR165E/l1S2t7lGINQZQ6ZWEhwvdmH U5xivHUvZ4tXmfcdYAYBQvCMuqeUvTMVNetRenG2dh3u/WLcn2Jv9+NXR1lf5W4SSRYd CQVvbcmTOD43qvoKloMCC8qqU6I/RZposO3MHe92HZzFmRoU3SdwGRob5vrS1UvhG+uX /l5+l4jTSUHbOAZSQ9l+gsW2LdGUFRedGbWR5XeY4DnYA/SIBpiC4uJr+lMxmPh+yZtc o8KBg9BAmklTFWbAPQru79rPR+WFp++v26KX0pcMHUFD4qWd9nbJMp4hHiEk9yOt10wN 06/A== X-Gm-Message-State: APf1xPBcQfdszpts3IKQRFTCcQR3edMtS8QrWYqnlZEgVuYwsHEPmz2n AKwbYX+t4zDVKh/aMekSUTU6LdKT9CE= X-Received: by 10.98.41.194 with SMTP id p185mr6664962pfp.38.1519298347861; Thu, 22 Feb 2018 03:19:07 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id v8sm19280826pff.38.2018.02.22.03.19.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:07 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 03/13] kexec_file, x86, powerpc: factor out kexec_file_ops functions Date: Thu, 22 Feb 2018 20:17:22 +0900 Message-Id: <20180222111732.23051-4-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As arch_kexec_kernel_*_{probe,load}(), arch_kimage_file_post_load_cleanup() and arch_kexec_kernel_verify_sg can be parameterized with a kexec_file_ops array and now duplicated among some architectures, let's factor them out. Signed-off-by: AKASHI Takahiro Cc: Dave Young Cc: Vivek Goyal Cc: Baoquan He Cc: Michael Ellerman Cc: Thiago Jung Bauermann --- arch/powerpc/include/asm/kexec.h | 2 +- arch/powerpc/kernel/kexec_elf_64.c | 2 +- arch/powerpc/kernel/machine_kexec_file_64.c | 39 ++------------------ arch/x86/include/asm/kexec-bzimage64.h | 2 +- arch/x86/kernel/kexec-bzimage64.c | 2 +- arch/x86/kernel/machine_kexec_64.c | 45 +---------------------- include/linux/kexec.h | 15 ++++---- kernel/kexec_file.c | 57 +++++++++++++++++++++++++++-- 8 files changed, 70 insertions(+), 94 deletions(-) -- 2.16.2 diff --git a/arch/powerpc/include/asm/kexec.h b/arch/powerpc/include/asm/kexec.h index d8b1e8e7e035..4a585cba1787 100644 --- a/arch/powerpc/include/asm/kexec.h +++ b/arch/powerpc/include/asm/kexec.h @@ -95,7 +95,7 @@ static inline bool kdump_in_progress(void) } #ifdef CONFIG_KEXEC_FILE -extern struct kexec_file_ops kexec_elf64_ops; +extern const struct kexec_file_ops kexec_elf64_ops; #ifdef CONFIG_IMA_KEXEC #define ARCH_HAS_KIMAGE_ARCH diff --git a/arch/powerpc/kernel/kexec_elf_64.c b/arch/powerpc/kernel/kexec_elf_64.c index 9a42309b091a..6c78c11c7faf 100644 --- a/arch/powerpc/kernel/kexec_elf_64.c +++ b/arch/powerpc/kernel/kexec_elf_64.c @@ -657,7 +657,7 @@ static void *elf64_load(struct kimage *image, char *kernel_buf, return ret ? ERR_PTR(ret) : fdt; } -struct kexec_file_ops kexec_elf64_ops = { +const struct kexec_file_ops kexec_elf64_ops = { .probe = elf64_probe, .load = elf64_load, }; diff --git a/arch/powerpc/kernel/machine_kexec_file_64.c b/arch/powerpc/kernel/machine_kexec_file_64.c index e4395f937d63..a27ec647350c 100644 --- a/arch/powerpc/kernel/machine_kexec_file_64.c +++ b/arch/powerpc/kernel/machine_kexec_file_64.c @@ -31,52 +31,19 @@ #define SLAVE_CODE_SIZE 256 -static struct kexec_file_ops *kexec_file_loaders[] = { +const struct kexec_file_ops * const kexec_file_loaders[] = { &kexec_elf64_ops, + NULL }; int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len) { - int i, ret = -ENOEXEC; - struct kexec_file_ops *fops; - /* We don't support crash kernels yet. */ if (image->type == KEXEC_TYPE_CRASH) return -ENOTSUPP; - for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) { - fops = kexec_file_loaders[i]; - if (!fops || !fops->probe) - continue; - - ret = fops->probe(buf, buf_len); - if (!ret) { - image->fops = fops; - return ret; - } - } - - return ret; -} - -void *arch_kexec_kernel_image_load(struct kimage *image) -{ - if (!image->fops || !image->fops->load) - return ERR_PTR(-ENOEXEC); - - return image->fops->load(image, image->kernel_buf, - image->kernel_buf_len, image->initrd_buf, - image->initrd_buf_len, image->cmdline_buf, - image->cmdline_buf_len); -} - -int arch_kimage_file_post_load_cleanup(struct kimage *image) -{ - if (!image->fops || !image->fops->cleanup) - return 0; - - return image->fops->cleanup(image->image_loader_data); + return _kexec_kernel_image_probe(image, buf, buf_len); } /** diff --git a/arch/x86/include/asm/kexec-bzimage64.h b/arch/x86/include/asm/kexec-bzimage64.h index 9f07cff43705..df89ee7d3e9e 100644 --- a/arch/x86/include/asm/kexec-bzimage64.h +++ b/arch/x86/include/asm/kexec-bzimage64.h @@ -2,6 +2,6 @@ #ifndef _ASM_KEXEC_BZIMAGE64_H #define _ASM_KEXEC_BZIMAGE64_H -extern struct kexec_file_ops kexec_bzImage64_ops; +extern const struct kexec_file_ops kexec_bzImage64_ops; #endif /* _ASM_KEXE_BZIMAGE64_H */ diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c index fb095ba0c02f..705654776c0c 100644 --- a/arch/x86/kernel/kexec-bzimage64.c +++ b/arch/x86/kernel/kexec-bzimage64.c @@ -538,7 +538,7 @@ static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len) } #endif -struct kexec_file_ops kexec_bzImage64_ops = { +const struct kexec_file_ops kexec_bzImage64_ops = { .probe = bzImage64_probe, .load = bzImage64_load, .cleanup = bzImage64_cleanup, diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c index 1f790cf9d38f..2cdd29d64181 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -30,8 +30,9 @@ #include #ifdef CONFIG_KEXEC_FILE -static struct kexec_file_ops *kexec_file_loaders[] = { +const struct kexec_file_ops * const kexec_file_loaders[] = { &kexec_bzImage64_ops, + NULL }; #endif @@ -363,27 +364,6 @@ void arch_crash_save_vmcoreinfo(void) /* arch-dependent functionality related to kexec file-based syscall */ #ifdef CONFIG_KEXEC_FILE -int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, - unsigned long buf_len) -{ - int i, ret = -ENOEXEC; - struct kexec_file_ops *fops; - - for (i = 0; i < ARRAY_SIZE(kexec_file_loaders); i++) { - fops = kexec_file_loaders[i]; - if (!fops || !fops->probe) - continue; - - ret = fops->probe(buf, buf_len); - if (!ret) { - image->fops = fops; - return ret; - } - } - - return ret; -} - void *arch_kexec_kernel_image_load(struct kimage *image) { vfree(image->arch.elf_headers); @@ -398,27 +378,6 @@ void *arch_kexec_kernel_image_load(struct kimage *image) image->cmdline_buf_len); } -int arch_kimage_file_post_load_cleanup(struct kimage *image) -{ - if (!image->fops || !image->fops->cleanup) - return 0; - - return image->fops->cleanup(image->image_loader_data); -} - -#ifdef CONFIG_KEXEC_VERIFY_SIG -int arch_kexec_kernel_verify_sig(struct kimage *image, void *kernel, - unsigned long kernel_len) -{ - if (!image->fops || !image->fops->verify_sig) { - pr_debug("kernel loader does not support signature verification."); - return -EKEYREJECTED; - } - - return image->fops->verify_sig(kernel, kernel_len); -} -#endif - /* * Apply purgatory relocations. * diff --git a/include/linux/kexec.h b/include/linux/kexec.h index f16f6ceb3875..325980537125 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -209,7 +209,7 @@ struct kimage { unsigned long cmdline_buf_len; /* File operations provided by image loader */ - struct kexec_file_ops *fops; + const struct kexec_file_ops *fops; /* Image loader handling the kernel can store a pointer here */ void *image_loader_data; @@ -277,12 +277,13 @@ int crash_shrink_memory(unsigned long new_size); size_t crash_get_memory_size(void); void crash_free_reserved_phys_range(unsigned long begin, unsigned long end); -int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, - unsigned long buf_len); -void * __weak arch_kexec_kernel_image_load(struct kimage *image); -int __weak arch_kimage_file_post_load_cleanup(struct kimage *image); -int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf, - unsigned long buf_len); +int _kexec_kernel_image_probe(struct kimage *image, void *buf, + unsigned long buf_len); +void *_kexec_kernel_image_load(struct kimage *image); +int _kimage_file_post_load_cleanup(struct kimage *image); +int _kexec_kernel_verify_sig(struct kimage *image, void *buf, + unsigned long buf_len); + int __weak arch_kexec_apply_relocations_add(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, unsigned int relsec); int __weak arch_kexec_apply_relocations(const Elf_Ehdr *ehdr, Elf_Shdr *sechdrs, diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index 990adae52151..a6d14a768b3e 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -26,34 +26,83 @@ #include #include "kexec_internal.h" +const __weak struct kexec_file_ops * const kexec_file_loaders[] = {NULL}; + #ifdef CONFIG_ARCH_HAS_KEXEC_PURGATORY static int kexec_calculate_store_digests(struct kimage *image); #else static int kexec_calculate_store_digests(struct kimage *image) { return 0; }; #endif +int _kexec_kernel_image_probe(struct kimage *image, void *buf, + unsigned long buf_len) +{ + const struct kexec_file_ops * const *fops; + int ret = -ENOEXEC; + + for (fops = &kexec_file_loaders[0]; *fops && (*fops)->probe; ++fops) { + ret = (*fops)->probe(buf, buf_len); + if (!ret) { + image->fops = *fops; + return ret; + } + } + + return ret; +} + /* Architectures can provide this probe function */ int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf, unsigned long buf_len) { - return -ENOEXEC; + return _kexec_kernel_image_probe(image, buf, buf_len); +} + +void *_kexec_kernel_image_load(struct kimage *image) +{ + if (!image->fops || !image->fops->load) + return ERR_PTR(-ENOEXEC); + + return image->fops->load(image, image->kernel_buf, + image->kernel_buf_len, image->initrd_buf, + image->initrd_buf_len, image->cmdline_buf, + image->cmdline_buf_len); } void * __weak arch_kexec_kernel_image_load(struct kimage *image) { - return ERR_PTR(-ENOEXEC); + return _kexec_kernel_image_load(image); +} + +int _kimage_file_post_load_cleanup(struct kimage *image) +{ + if (!image->fops || !image->fops->cleanup) + return 0; + + return image->fops->cleanup(image->image_loader_data); } int __weak arch_kimage_file_post_load_cleanup(struct kimage *image) { - return -EINVAL; + return _kimage_file_post_load_cleanup(image); } #ifdef CONFIG_KEXEC_VERIFY_SIG +int _kexec_kernel_verify_sig(struct kimage *image, void *buf, + unsigned long buf_len) +{ + if (!image->fops || !image->fops->verify_sig) { + pr_debug("kernel loader does not support signature verification.\n"); + return -EKEYREJECTED; + } + + return image->fops->verify_sig(buf, buf_len); +} + int __weak arch_kexec_kernel_verify_sig(struct kimage *image, void *buf, unsigned long buf_len) { - return -EKEYREJECTED; + return _kexec_kernel_verify_sig(image, buf, buf_len); } #endif From patchwork Thu Feb 22 11:17:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129207 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481532lja; Thu, 22 Feb 2018 03:19:20 -0800 (PST) X-Google-Smtp-Source: AH8x225+WyRdf+Qdnd663CE8zIZwbBx6cXzzg1Lg9r6Qt3sWfCeEDauPAsf9A1EJqzAvQG4ppH/E X-Received: by 2002:a17:902:bf01:: with SMTP id bi1-v6mr6165280plb.254.1519298360812; Thu, 22 Feb 2018 03:19:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298360; cv=none; d=google.com; s=arc-20160816; b=KD80CGho5aAf8CHZ730IaCyTREPy1nzPUxP4luZ1khdheI0aNDK2nxMdZH6xql2kUt l7aOE2SIWeblqMJlPweUnhjODOkVXPW1UkYB5D4dtlviACSgNGDyQc2OLYfJCZh1C1Ve ceUYT6wfMXFKXbzKZteCvposDs5aTsLzA8O2YFVjm5b9oLsfv7EIIWwfTdhNbKx6VJX0 1JocItUGZkweAyD47KfDo1GDihdk4fGt9z0vRbYhN0If8+DgNYm1nVFYSZfrRluvjFmk wZXLkusGwbc8/c0KjTsqYgfXHm40Bl2UOS8wUnuf/3DlPj7gxZrCjhHrNAMA/r8ylFLV 8Vmg== 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=efuAuXsFpNdP9wcJP/B0+E5eWfdQZ0BUy+RaCTL3Zts=; b=F/W41bTtB1ukyEjVVTV0iShNKCef7Odt7fSp1igSUBi3ZIxu6CZMrxNsBpsb0Mq2Qs +nnSDGIpZ1XrwPRinh6LyFuf/1W26a3PFRlpvKO6+TbKszC33E290MCNaK45Odfx1pEF +6Z+nomLe3g+Hc/qCTCcYb0dpMxPylORXjhtN+LS6AffHhrtWvuKXvu2GN6ftv2VIh5+ 9EYUKN5HW0TaYUOS/koz3yAAVcnV+pYljrJ25Yh9cxIW/NzGVqPDEAoRNu/0uzdAjzko /BkP5F/SKzbLh23iVj48uANC7stDiHANNHjwyEVjyVU5BSEaPkid6hnEL4ZfzmuJP35D QBdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=BHeYd9m4; 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 b1si1388016pgq.773.2018.02.22.03.19.20; Thu, 22 Feb 2018 03:19:20 -0800 (PST) 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=BHeYd9m4; 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 S932128AbeBVLTR (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:17 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:36311 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932102AbeBVLTN (ORCPT ); Thu, 22 Feb 2018 06:19:13 -0500 Received: by mail-pl0-f67.google.com with SMTP id v3so2710296plg.3 for ; Thu, 22 Feb 2018 03:19:12 -0800 (PST) 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=efuAuXsFpNdP9wcJP/B0+E5eWfdQZ0BUy+RaCTL3Zts=; b=BHeYd9m40og6csL+3RFwOUTPPexVoND2D1xmEJgdZsafrtvy0khhx77VduHGc/Ea9Z KTUpXyeGVWEyCE/q8sK5oHmW6BIMJJfMmJAiUPtyXuNneRYTpc5ACgGie3ejEH6bCiTR Tx32UqRizmAlh85+7qGTylM6OBhi9UhvnpJvI= 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=efuAuXsFpNdP9wcJP/B0+E5eWfdQZ0BUy+RaCTL3Zts=; b=sEIHmkusZE6yG9JYfLDc+bzs3ZdJrVHyYuA95ioJqfAyhJbRrhmJCEltCAecQ53qst upgeaw+2bswVz2jn21lrZSDL8eHBLoNLrHsSw0IxB63D/9qQbCDEATVm5uHvhfeihYaU Tn/zpZwllkeOQaRZxfYjxsjKQXMnZJoGUq+/O7c/IZbWQPjEuV6ETYOaaS9J5p6t8IZR OwU5tM1wYTnMkG+yjCTUjwsR3HB1YrQN5sq43Y20i1J6IxsgXFghHirW7IwxTPIysxh/ OhOeTlVzu869KO+i+uVTsdOKcFJQitP6+bs8Y0c4IEcYhf3Hc4/yR3LgeKmgLiDaL7cI RmhA== X-Gm-Message-State: APf1xPAv307ugpo3Uk4B3F6xTqHCzs88qAcu+L5FJS5AOdizOuoQf+HI lmk/kWbExE6Huv1qbqnoN4DByA== X-Received: by 2002:a17:902:1683:: with SMTP id h3-v6mr6312604plh.433.1519298352508; Thu, 22 Feb 2018 03:19:12 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id a22sm34512898pff.119.2018.02.22.03.19.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:11 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 04/13] x86: kexec_file: factor out elf core header related functions Date: Thu, 22 Feb 2018 20:17:23 +0900 Message-Id: <20180222111732.23051-5-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org exclude_mem_range() and prepare_elf64_headers() can be re-used on other architectures, including arm64, as well. So let them factored out so as to move them to generic side in the next patch. fill_up_crash_elf_data() can potentially be commonalized for most architectures who want to go through io resources (/proc/iomem) for a list of "System RAM", but leave it private for now. Signed-off-by: AKASHI Takahiro Cc: Dave Young Cc: Vivek Goyal Cc: Baoquan He --- arch/x86/kernel/crash.c | 235 +++++++++++++++++++++--------------------------- 1 file changed, 103 insertions(+), 132 deletions(-) -- 2.16.2 diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index 10e74d4778a1..5c19cfbf3b85 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -41,32 +41,14 @@ /* Alignment required for elf header segment */ #define ELF_CORE_HEADER_ALIGN 4096 -/* This primarily represents number of split ranges due to exclusion */ -#define CRASH_MAX_RANGES 16 - struct crash_mem_range { u64 start, end; }; struct crash_mem { - unsigned int nr_ranges; - struct crash_mem_range ranges[CRASH_MAX_RANGES]; -}; - -/* Misc data about ram ranges needed to prepare elf headers */ -struct crash_elf_data { - struct kimage *image; - /* - * Total number of ram ranges we have after various adjustments for - * crash reserved region, etc. - */ unsigned int max_nr_ranges; - - /* Pointer to elf header */ - void *ehdr; - /* Pointer to next phdr */ - void *bufp; - struct crash_mem mem; + unsigned int nr_ranges; + struct crash_mem_range ranges[0]; }; /* Used while preparing memory map entries for second kernel */ @@ -217,29 +199,32 @@ static int get_nr_ram_ranges_callback(struct resource *res, void *arg) return 0; } - /* Gather all the required information to prepare elf headers for ram regions */ -static void fill_up_crash_elf_data(struct crash_elf_data *ced, - struct kimage *image) +static struct crash_mem *fill_up_crash_elf_data(void) { unsigned int nr_ranges = 0; - - ced->image = image; + struct crash_mem *cmem; walk_system_ram_res(0, -1, &nr_ranges, get_nr_ram_ranges_callback); - ced->max_nr_ranges = nr_ranges; + /* + * Exclusion of crash region and/or crashk_low_res may cause + * another range split. So add extra two slots here. + */ + nr_ranges += 2; + cmem = vmalloc(sizeof(struct crash_mem) + + sizeof(struct crash_mem_range) * nr_ranges); + if (!cmem) + return NULL; - /* Exclusion of crash region could split memory ranges */ - ced->max_nr_ranges++; + cmem->max_nr_ranges = nr_ranges; + cmem->nr_ranges = 0; - /* If crashk_low_res is not 0, another range split possible */ - if (crashk_low_res.end) - ced->max_nr_ranges++; + return cmem; } -static int exclude_mem_range(struct crash_mem *mem, +static int crash_exclude_mem_range(struct crash_mem *mem, unsigned long long mstart, unsigned long long mend) { int i, j; @@ -293,10 +278,8 @@ static int exclude_mem_range(struct crash_mem *mem, return 0; /* Split happened */ - if (i == CRASH_MAX_RANGES - 1) { - pr_err("Too many crash ranges after split\n"); + if (i == mem->max_nr_ranges - 1) return -ENOMEM; - } /* Location where new range should go */ j = i + 1; @@ -314,27 +297,20 @@ static int exclude_mem_range(struct crash_mem *mem, /* * Look for any unwanted ranges between mstart, mend and remove them. This - * might lead to split and split ranges are put in ced->mem.ranges[] array + * might lead to split and split ranges are put in cmem->ranges[] array */ -static int elf_header_exclude_ranges(struct crash_elf_data *ced, - unsigned long long mstart, unsigned long long mend) +static int elf_header_exclude_ranges(struct crash_mem *cmem) { - struct crash_mem *cmem = &ced->mem; int ret = 0; - memset(cmem->ranges, 0, sizeof(cmem->ranges)); - - cmem->ranges[0].start = mstart; - cmem->ranges[0].end = mend; - cmem->nr_ranges = 1; - /* Exclude crashkernel region */ - ret = exclude_mem_range(cmem, crashk_res.start, crashk_res.end); + ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end); if (ret) return ret; if (crashk_low_res.end) { - ret = exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end); + ret = crash_exclude_mem_range(cmem, crashk_low_res.start, + crashk_low_res.end); if (ret) return ret; } @@ -344,70 +320,29 @@ static int elf_header_exclude_ranges(struct crash_elf_data *ced, static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg) { - struct crash_elf_data *ced = arg; - Elf64_Ehdr *ehdr; - Elf64_Phdr *phdr; - unsigned long mstart, mend; - struct kimage *image = ced->image; - struct crash_mem *cmem; - int ret, i; + struct crash_mem *cmem = arg; - ehdr = ced->ehdr; - - /* Exclude unwanted mem ranges */ - ret = elf_header_exclude_ranges(ced, res->start, res->end); - if (ret) - return ret; - - /* Go through all the ranges in ced->mem.ranges[] and prepare phdr */ - cmem = &ced->mem; - - for (i = 0; i < cmem->nr_ranges; i++) { - mstart = cmem->ranges[i].start; - mend = cmem->ranges[i].end; - - phdr = ced->bufp; - ced->bufp += sizeof(Elf64_Phdr); - - phdr->p_type = PT_LOAD; - phdr->p_flags = PF_R|PF_W|PF_X; - phdr->p_offset = mstart; - - /* - * If a range matches backup region, adjust offset to backup - * segment. - */ - if (mstart == image->arch.backup_src_start && - (mend - mstart + 1) == image->arch.backup_src_sz) - phdr->p_offset = image->arch.backup_load_addr; - - phdr->p_paddr = mstart; - phdr->p_vaddr = (unsigned long long) __va(mstart); - phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; - phdr->p_align = 0; - ehdr->e_phnum++; - pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", - phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, - ehdr->e_phnum, phdr->p_offset); - } + cmem->ranges[cmem->nr_ranges].start = res->start; + cmem->ranges[cmem->nr_ranges].end = res->end; + cmem->nr_ranges++; - return ret; + return 0; } -static int prepare_elf64_headers(struct crash_elf_data *ced, - void **addr, unsigned long *sz) +static int crash_prepare_elf64_headers(struct crash_mem *cmem, int kernel_map, + void **addr, unsigned long *sz) { Elf64_Ehdr *ehdr; Elf64_Phdr *phdr; unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; - unsigned char *buf, *bufp; - unsigned int cpu; + unsigned char *buf; + unsigned int cpu, i; unsigned long long notes_addr; - int ret; + unsigned long mstart, mend; /* extra phdr for vmcoreinfo elf note */ nr_phdr = nr_cpus + 1; - nr_phdr += ced->max_nr_ranges; + nr_phdr += cmem->nr_ranges; /* * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping @@ -425,9 +360,8 @@ static int prepare_elf64_headers(struct crash_elf_data *ced, if (!buf) return -ENOMEM; - bufp = buf; - ehdr = (Elf64_Ehdr *)bufp; - bufp += sizeof(Elf64_Ehdr); + ehdr = (Elf64_Ehdr *)buf; + phdr = (Elf64_Phdr *)(ehdr + 1); memcpy(ehdr->e_ident, ELFMAG, SELFMAG); ehdr->e_ident[EI_CLASS] = ELFCLASS64; ehdr->e_ident[EI_DATA] = ELFDATA2LSB; @@ -443,42 +377,51 @@ static int prepare_elf64_headers(struct crash_elf_data *ced, /* Prepare one phdr of type PT_NOTE for each present cpu */ for_each_present_cpu(cpu) { - phdr = (Elf64_Phdr *)bufp; - bufp += sizeof(Elf64_Phdr); phdr->p_type = PT_NOTE; notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); phdr->p_offset = phdr->p_paddr = notes_addr; phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); (ehdr->e_phnum)++; + phdr++; } /* Prepare one PT_NOTE header for vmcoreinfo */ - phdr = (Elf64_Phdr *)bufp; - bufp += sizeof(Elf64_Phdr); phdr->p_type = PT_NOTE; phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; (ehdr->e_phnum)++; + phdr++; -#ifdef CONFIG_X86_64 /* Prepare PT_LOAD type program header for kernel text region */ - phdr = (Elf64_Phdr *)bufp; - bufp += sizeof(Elf64_Phdr); - phdr->p_type = PT_LOAD; - phdr->p_flags = PF_R|PF_W|PF_X; - phdr->p_vaddr = (Elf64_Addr)_text; - phdr->p_filesz = phdr->p_memsz = _end - _text; - phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); - (ehdr->e_phnum)++; -#endif + if (kernel_map) { + phdr->p_type = PT_LOAD; + phdr->p_flags = PF_R|PF_W|PF_X; + phdr->p_vaddr = (Elf64_Addr)_text; + phdr->p_filesz = phdr->p_memsz = _end - _text; + phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); + ehdr->e_phnum++; + phdr++; + } - /* Prepare PT_LOAD headers for system ram chunks. */ - ced->ehdr = ehdr; - ced->bufp = bufp; - ret = walk_system_ram_res(0, -1, ced, - prepare_elf64_ram_headers_callback); - if (ret < 0) - return ret; + /* Go through all the ranges in cmem->ranges[] and prepare phdr */ + for (i = 0; i < cmem->nr_ranges; i++) { + mstart = cmem->ranges[i].start; + mend = cmem->ranges[i].end; + + phdr->p_type = PT_LOAD; + phdr->p_flags = PF_R|PF_W|PF_X; + phdr->p_offset = mstart; + + phdr->p_paddr = mstart; + phdr->p_vaddr = (unsigned long long) __va(mstart); + phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; + phdr->p_align = 0; + ehdr->e_phnum++; + phdr++; + pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", + phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, + ehdr->e_phnum, phdr->p_offset); + } *addr = buf; *sz = elf_sz; @@ -489,18 +432,46 @@ static int prepare_elf64_headers(struct crash_elf_data *ced, static int prepare_elf_headers(struct kimage *image, void **addr, unsigned long *sz) { - struct crash_elf_data *ced; - int ret; + struct crash_mem *cmem; + Elf64_Ehdr *ehdr; + Elf64_Phdr *phdr; + int ret, i; - ced = kzalloc(sizeof(*ced), GFP_KERNEL); - if (!ced) + cmem = fill_up_crash_elf_data(); + if (!cmem) return -ENOMEM; - fill_up_crash_elf_data(ced, image); + ret = walk_system_ram_res(0, -1, cmem, + prepare_elf64_ram_headers_callback); + if (ret) + goto out; + + /* Exclude unwanted mem ranges */ + ret = elf_header_exclude_ranges(cmem); + if (ret) + goto out; /* By default prepare 64bit headers */ - ret = prepare_elf64_headers(ced, addr, sz); - kfree(ced); + ret = crash_prepare_elf64_headers(cmem, + (int)IS_ENABLED(CONFIG_X86_64), addr, sz); + if (ret) + goto out; + + /* + * If a range matches backup region, adjust offset to backup + * segment. + */ + ehdr = (Elf64_Ehdr *)*addr; + phdr = (Elf64_Phdr *)(ehdr + 1); + for (i = 0; i < ehdr->e_phnum; phdr++, i++) + if (phdr->p_type == PT_LOAD && + phdr->p_paddr == image->arch.backup_src_start && + phdr->p_memsz == image->arch.backup_src_sz) { + phdr->p_offset = image->arch.backup_load_addr; + break; + } +out: + vfree(cmem); return ret; } @@ -546,14 +517,14 @@ static int memmap_exclude_ranges(struct kimage *image, struct crash_mem *cmem, /* Exclude Backup region */ start = image->arch.backup_load_addr; end = start + image->arch.backup_src_sz - 1; - ret = exclude_mem_range(cmem, start, end); + ret = crash_exclude_mem_range(cmem, start, end); if (ret) return ret; /* Exclude elf header region */ start = image->arch.elf_load_addr; end = start + image->arch.elf_headers_sz - 1; - return exclude_mem_range(cmem, start, end); + return crash_exclude_mem_range(cmem, start, end); } /* Prepare memory map for crash dump kernel */ From patchwork Thu Feb 22 11:17:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129208 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481590lja; Thu, 22 Feb 2018 03:19:24 -0800 (PST) X-Google-Smtp-Source: AH8x2241zlT8hQrTkyYYQ+ERqrpVwMS2Lz3v/+pu0WykZiMJdpmS/xxe8SjERwgwqLkow1vndl7Z X-Received: by 10.167.131.135 with SMTP id u7mr6590176pfm.50.1519298364161; Thu, 22 Feb 2018 03:19:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298364; cv=none; d=google.com; s=arc-20160816; b=F5KWZENtkGYunOrdgzxmjWExA827t78sWaYJjx0Xbh/LGv2Qh+BUE7hUbI9AuDSfJK gEruS+tOrCIRTPtfHn2cSNfxFKFjkLakqe3B1vjlk5Yjpcfdl2QD92J4RtUCySW+fY4s mLPk+8+EpxbWzm9418txca879r61vGlpwYuoKdKOTQGb0gGMtFvk7O4NgGAzl7vy/2D6 +iFpxAwSn/ni4D4w6TgWImb//CWjSNDou/j7SQUHqPFRnY3YkTBImmQP50ZfrIZZxJb5 ND+Zw11844/NeDx1SocyKNTxnfJ/kir+BKrp8nMo501JfTSvy7O5zaMpL5zdGkPqJEXu X9Lg== 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=4uxJvJDwq/vzPe3tHcp4blp7WKxBo5MSb9jVXarxZjY=; b=1FQLLS1HwCca1X+bGgQ+KUShkKAeY2PFFClUy9N0tG3EP4VxeDOG6K1qe+uc9+S9ek ewg6i3SDXfu2DsCWDC4P3VnfkMp3vXLspBkxx7JjKDfTbZGG2bNoCIKbqGuedkQobJ3g zi5lyfRWTHsfHdiDPAVV87AVG3tN7hZE1VopIyWg45zvpfC/H9IPZcg6GIpk16Z/XE9Z IwYCAdz+BUxjXa6ooEGRrpXQ2DK15o18GMWv2ewKGvxhd/+uaO6h2U+9zdJTw5w77nVT KTqvfOE07qwsVwkTf25M8tKP66CBhA+FhEoQCa3yXmTOeQbGwEF87wEFaJGmAPHPMVFN n11Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=keV8MkXY; 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 b1si1388016pgq.773.2018.02.22.03.19.23; Thu, 22 Feb 2018 03:19:24 -0800 (PST) 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=keV8MkXY; 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 S932141AbeBVLTW (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:22 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:34173 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932102AbeBVLTT (ORCPT ); Thu, 22 Feb 2018 06:19:19 -0500 Received: by mail-pl0-f67.google.com with SMTP id u13so2715084plq.1 for ; Thu, 22 Feb 2018 03:19:18 -0800 (PST) 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=4uxJvJDwq/vzPe3tHcp4blp7WKxBo5MSb9jVXarxZjY=; b=keV8MkXYkYtrXNAUYbuU8FhjnPgarx3ga+2tiFzUlyEGZl2ZF4aTswk1Ccboo5zzjK 0BpavArR5KLvC9UBBlaQIfF36/dzlAbp6uhMK7ysstc1MFh+qCnyVQQ+9THlpkCFHXYx +rbXFj8yiDbWpMSPLAeCaxhaqyH2OpQC5BHCI= 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=4uxJvJDwq/vzPe3tHcp4blp7WKxBo5MSb9jVXarxZjY=; b=S1JJxht8vezSePwfn4u9Ha0hdXk6hAqSyjlLsD3iQBoJoHUFsRw5pnCvQeCFc7QI2y xq1oqQcJnusAtsSgkdzL2AxsgBeMusB3jrDBOpOD1jXW6wWSDl+WZETm+OJfPND7sL1u oANwHZRf4Q9m/SkF6E/wi0gEozXcJGh3uM3NaDDsartQ8CJMZHDAMzETD6OdFaNQrwoh 1mAvCpRw4NgW8QCWLaFwX/p5t+/7BeMwsu5iR6H5Z1KaaDyuBtdu1sic4YAlAj/9foED rGT52Q9m/a5r/d6UK1hoPWPA3rJGlR1BXxeaKoPhbKn3X5kofz2OaM6k7RZMxy/0fiOD gdYg== X-Gm-Message-State: APf1xPCHrDZ6GQaeB9UVdFAzDDB9307jl4JbWWlBMNEm5MASOaPe38Q0 IicxjN6EdvmnWzXoW6ArSR7vYw== X-Received: by 2002:a17:902:32a2:: with SMTP id z31-v6mr6385527plb.32.1519298358566; Thu, 22 Feb 2018 03:19:18 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id 66sm12436944pfh.96.2018.02.22.03.19.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:17 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 05/13] kexec_file, x86: move re-factored code to generic side Date: Thu, 22 Feb 2018 20:17:24 +0900 Message-Id: <20180222111732.23051-6-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the previous patch, commonly-used routines were carved out. Now place them in kexec common code. Signed-off-by: AKASHI Takahiro Cc: Dave Young Cc: Vivek Goyal Cc: Baoquan He --- arch/x86/kernel/crash.c | 183 ------------------------------------------------ include/linux/kexec.h | 19 +++++ kernel/kexec_file.c | 175 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 183 deletions(-) -- 2.16.2 diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c index 5c19cfbf3b85..3e4f3980688d 100644 --- a/arch/x86/kernel/crash.c +++ b/arch/x86/kernel/crash.c @@ -38,19 +38,6 @@ #include #include -/* Alignment required for elf header segment */ -#define ELF_CORE_HEADER_ALIGN 4096 - -struct crash_mem_range { - u64 start, end; -}; - -struct crash_mem { - unsigned int max_nr_ranges; - unsigned int nr_ranges; - struct crash_mem_range ranges[0]; -}; - /* Used while preparing memory map entries for second kernel */ struct crash_memmap_data { struct boot_params *params; @@ -224,77 +211,6 @@ static struct crash_mem *fill_up_crash_elf_data(void) return cmem; } -static int crash_exclude_mem_range(struct crash_mem *mem, - unsigned long long mstart, unsigned long long mend) -{ - int i, j; - unsigned long long start, end; - struct crash_mem_range temp_range = {0, 0}; - - for (i = 0; i < mem->nr_ranges; i++) { - start = mem->ranges[i].start; - end = mem->ranges[i].end; - - if (mstart > end || mend < start) - continue; - - /* Truncate any area outside of range */ - if (mstart < start) - mstart = start; - if (mend > end) - mend = end; - - /* Found completely overlapping range */ - if (mstart == start && mend == end) { - mem->ranges[i].start = 0; - mem->ranges[i].end = 0; - if (i < mem->nr_ranges - 1) { - /* Shift rest of the ranges to left */ - for (j = i; j < mem->nr_ranges - 1; j++) { - mem->ranges[j].start = - mem->ranges[j+1].start; - mem->ranges[j].end = - mem->ranges[j+1].end; - } - } - mem->nr_ranges--; - return 0; - } - - if (mstart > start && mend < end) { - /* Split original range */ - mem->ranges[i].end = mstart - 1; - temp_range.start = mend + 1; - temp_range.end = end; - } else if (mstart != start) - mem->ranges[i].end = mstart - 1; - else - mem->ranges[i].start = mend + 1; - break; - } - - /* If a split happend, add the split to array */ - if (!temp_range.end) - return 0; - - /* Split happened */ - if (i == mem->max_nr_ranges - 1) - return -ENOMEM; - - /* Location where new range should go */ - j = i + 1; - if (j < mem->nr_ranges) { - /* Move over all ranges one slot towards the end */ - for (i = mem->nr_ranges - 1; i >= j; i--) - mem->ranges[i + 1] = mem->ranges[i]; - } - - mem->ranges[j].start = temp_range.start; - mem->ranges[j].end = temp_range.end; - mem->nr_ranges++; - return 0; -} - /* * Look for any unwanted ranges between mstart, mend and remove them. This * might lead to split and split ranges are put in cmem->ranges[] array @@ -329,105 +245,6 @@ static int prepare_elf64_ram_headers_callback(struct resource *res, void *arg) return 0; } -static int crash_prepare_elf64_headers(struct crash_mem *cmem, int kernel_map, - void **addr, unsigned long *sz) -{ - Elf64_Ehdr *ehdr; - Elf64_Phdr *phdr; - unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; - unsigned char *buf; - unsigned int cpu, i; - unsigned long long notes_addr; - unsigned long mstart, mend; - - /* extra phdr for vmcoreinfo elf note */ - nr_phdr = nr_cpus + 1; - nr_phdr += cmem->nr_ranges; - - /* - * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping - * area on x86_64 (ffffffff80000000 - ffffffffa0000000). - * I think this is required by tools like gdb. So same physical - * memory will be mapped in two elf headers. One will contain kernel - * text virtual addresses and other will have __va(physical) addresses. - */ - - nr_phdr++; - elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); - elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); - - buf = vzalloc(elf_sz); - if (!buf) - return -ENOMEM; - - ehdr = (Elf64_Ehdr *)buf; - phdr = (Elf64_Phdr *)(ehdr + 1); - memcpy(ehdr->e_ident, ELFMAG, SELFMAG); - ehdr->e_ident[EI_CLASS] = ELFCLASS64; - ehdr->e_ident[EI_DATA] = ELFDATA2LSB; - ehdr->e_ident[EI_VERSION] = EV_CURRENT; - ehdr->e_ident[EI_OSABI] = ELF_OSABI; - memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); - ehdr->e_type = ET_CORE; - ehdr->e_machine = ELF_ARCH; - ehdr->e_version = EV_CURRENT; - ehdr->e_phoff = sizeof(Elf64_Ehdr); - ehdr->e_ehsize = sizeof(Elf64_Ehdr); - ehdr->e_phentsize = sizeof(Elf64_Phdr); - - /* Prepare one phdr of type PT_NOTE for each present cpu */ - for_each_present_cpu(cpu) { - phdr->p_type = PT_NOTE; - notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); - phdr->p_offset = phdr->p_paddr = notes_addr; - phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); - (ehdr->e_phnum)++; - phdr++; - } - - /* Prepare one PT_NOTE header for vmcoreinfo */ - phdr->p_type = PT_NOTE; - phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); - phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; - (ehdr->e_phnum)++; - phdr++; - - /* Prepare PT_LOAD type program header for kernel text region */ - if (kernel_map) { - phdr->p_type = PT_LOAD; - phdr->p_flags = PF_R|PF_W|PF_X; - phdr->p_vaddr = (Elf64_Addr)_text; - phdr->p_filesz = phdr->p_memsz = _end - _text; - phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); - ehdr->e_phnum++; - phdr++; - } - - /* Go through all the ranges in cmem->ranges[] and prepare phdr */ - for (i = 0; i < cmem->nr_ranges; i++) { - mstart = cmem->ranges[i].start; - mend = cmem->ranges[i].end; - - phdr->p_type = PT_LOAD; - phdr->p_flags = PF_R|PF_W|PF_X; - phdr->p_offset = mstart; - - phdr->p_paddr = mstart; - phdr->p_vaddr = (unsigned long long) __va(mstart); - phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; - phdr->p_align = 0; - ehdr->e_phnum++; - phdr++; - pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", - phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, - ehdr->e_phnum, phdr->p_offset); - } - - *addr = buf; - *sz = elf_sz; - return 0; -} - /* Prepare elf headers. Return addr and size */ static int prepare_elf_headers(struct kimage *image, void **addr, unsigned long *sz) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 325980537125..0b10c7f7cca8 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -163,6 +163,25 @@ int __weak arch_kexec_walk_mem(struct kexec_buf *kbuf, int (*func)(struct resource *, void *)); extern int kexec_add_buffer(struct kexec_buf *kbuf); int kexec_locate_mem_hole(struct kexec_buf *kbuf); + +/* Alignment required for elf header segment */ +#define ELF_CORE_HEADER_ALIGN 4096 + +struct crash_mem_range { + u64 start, end; +}; + +struct crash_mem { + unsigned int max_nr_ranges; + unsigned int nr_ranges; + struct crash_mem_range ranges[0]; +}; + +extern int crash_exclude_mem_range(struct crash_mem *mem, + unsigned long long mstart, + unsigned long long mend); +extern int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map, + void **addr, unsigned long *sz); #endif /* CONFIG_KEXEC_FILE */ struct kimage { diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index a6d14a768b3e..3f506774f32e 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -22,6 +22,11 @@ #include #include #include +#include +#include +#include +#include +#include #include #include #include "kexec_internal.h" @@ -1077,3 +1082,173 @@ int kexec_purgatory_get_set_symbol(struct kimage *image, const char *name, return 0; } #endif /* CONFIG_ARCH_HAS_KEXEC_PURGATORY */ + +int crash_exclude_mem_range(struct crash_mem *mem, + unsigned long long mstart, unsigned long long mend) +{ + int i, j; + unsigned long long start, end; + struct crash_mem_range temp_range = {0, 0}; + + for (i = 0; i < mem->nr_ranges; i++) { + start = mem->ranges[i].start; + end = mem->ranges[i].end; + + if (mstart > end || mend < start) + continue; + + /* Truncate any area outside of range */ + if (mstart < start) + mstart = start; + if (mend > end) + mend = end; + + /* Found completely overlapping range */ + if (mstart == start && mend == end) { + mem->ranges[i].start = 0; + mem->ranges[i].end = 0; + if (i < mem->nr_ranges - 1) { + /* Shift rest of the ranges to left */ + for (j = i; j < mem->nr_ranges - 1; j++) { + mem->ranges[j].start = + mem->ranges[j+1].start; + mem->ranges[j].end = + mem->ranges[j+1].end; + } + } + mem->nr_ranges--; + return 0; + } + + if (mstart > start && mend < end) { + /* Split original range */ + mem->ranges[i].end = mstart - 1; + temp_range.start = mend + 1; + temp_range.end = end; + } else if (mstart != start) + mem->ranges[i].end = mstart - 1; + else + mem->ranges[i].start = mend + 1; + break; + } + + /* If a split happened, add the split to array */ + if (!temp_range.end) + return 0; + + /* Split happened */ + if (i == mem->max_nr_ranges - 1) + return -ENOMEM; + + /* Location where new range should go */ + j = i + 1; + if (j < mem->nr_ranges) { + /* Move over all ranges one slot towards the end */ + for (i = mem->nr_ranges - 1; i >= j; i--) + mem->ranges[i + 1] = mem->ranges[i]; + } + + mem->ranges[j].start = temp_range.start; + mem->ranges[j].end = temp_range.end; + mem->nr_ranges++; + return 0; +} + +int crash_prepare_elf64_headers(struct crash_mem *mem, int kernel_map, + void **addr, unsigned long *sz) +{ + Elf64_Ehdr *ehdr; + Elf64_Phdr *phdr; + unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz; + unsigned char *buf; + unsigned int cpu, i; + unsigned long long notes_addr; + unsigned long mstart, mend; + + /* extra phdr for vmcoreinfo elf note */ + nr_phdr = nr_cpus + 1; + nr_phdr += mem->nr_ranges; + + /* + * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping + * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64). + * I think this is required by tools like gdb. So same physical + * memory will be mapped in two elf headers. One will contain kernel + * text virtual addresses and other will have __va(physical) addresses. + */ + + nr_phdr++; + elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr); + elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN); + + buf = vzalloc(elf_sz); + if (!buf) + return -ENOMEM; + + ehdr = (Elf64_Ehdr *)buf; + phdr = (Elf64_Phdr *)(ehdr + 1); + memcpy(ehdr->e_ident, ELFMAG, SELFMAG); + ehdr->e_ident[EI_CLASS] = ELFCLASS64; + ehdr->e_ident[EI_DATA] = ELFDATA2LSB; + ehdr->e_ident[EI_VERSION] = EV_CURRENT; + ehdr->e_ident[EI_OSABI] = ELF_OSABI; + memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); + ehdr->e_type = ET_CORE; + ehdr->e_machine = ELF_ARCH; + ehdr->e_version = EV_CURRENT; + ehdr->e_phoff = sizeof(Elf64_Ehdr); + ehdr->e_ehsize = sizeof(Elf64_Ehdr); + ehdr->e_phentsize = sizeof(Elf64_Phdr); + + /* Prepare one phdr of type PT_NOTE for each present cpu */ + for_each_present_cpu(cpu) { + phdr->p_type = PT_NOTE; + notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu)); + phdr->p_offset = phdr->p_paddr = notes_addr; + phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t); + (ehdr->e_phnum)++; + phdr++; + } + + /* Prepare one PT_NOTE header for vmcoreinfo */ + phdr->p_type = PT_NOTE; + phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note(); + phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE; + (ehdr->e_phnum)++; + phdr++; + + /* Prepare PT_LOAD type program header for kernel text region */ + if (kernel_map) { + phdr->p_type = PT_LOAD; + phdr->p_flags = PF_R|PF_W|PF_X; + phdr->p_vaddr = (Elf64_Addr)_text; + phdr->p_filesz = phdr->p_memsz = _end - _text; + phdr->p_offset = phdr->p_paddr = __pa_symbol(_text); + ehdr->e_phnum++; + phdr++; + } + + /* Go through all the ranges in mem->ranges[] and prepare phdr */ + for (i = 0; i < mem->nr_ranges; i++) { + mstart = mem->ranges[i].start; + mend = mem->ranges[i].end; + + phdr->p_type = PT_LOAD; + phdr->p_flags = PF_R|PF_W|PF_X; + phdr->p_offset = mstart; + + phdr->p_paddr = mstart; + phdr->p_vaddr = (unsigned long long) __va(mstart); + phdr->p_filesz = phdr->p_memsz = mend - mstart + 1; + phdr->p_align = 0; + ehdr->e_phnum++; + phdr++; + pr_debug("Crash PT_LOAD elf header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n", + phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz, + ehdr->e_phnum, phdr->p_offset); + } + + *addr = buf; + *sz = elf_sz; + return 0; +} From patchwork Thu Feb 22 11:17:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129209 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481675lja; Thu, 22 Feb 2018 03:19:31 -0800 (PST) X-Google-Smtp-Source: AH8x224iCpx3FXPJQ5hRfoEytNfegShN4zdK1C8YP+wkma6bqewofakquCzZcGoqA0fUTmdNm23G X-Received: by 2002:a17:902:26a:: with SMTP id 97-v6mr6282074plc.3.1519298371354; Thu, 22 Feb 2018 03:19:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298371; cv=none; d=google.com; s=arc-20160816; b=jzfvVuKOrqZH4uHLdaBm4LkPTMUEi37nbsYpuLv2RwFJcix8W6kF8vPKwqIWMhMMNF biIz5fMIFEu4UdSNiYxOuvZEomwsjrX+Idw5vKwLo7wOzEiRo+Df5Z+xMCoc/j1aMBi7 HX54KnzFWsM04ffMQozf3UHHO/ErB54qnf3hqcUSP7nzWZ93Q3MF4blAvGzCXapro++9 CsCPkk/uH3d1YZUuR6rLlT6JQAqrQh6gfge89yk3bnYCT42oRE/yp/XXjbYUw1ukvKXp eZFRXDOnb8+N/pe0WZ3j96epdjJoUQhHta6hrTXr4CcOL1pnrHfe2tDffC4f7cq3ERHA RkAQ== 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=ODOcCvpdSx6peztqFXdleOHiebtdBOEIkJi6v/RgTc4=; b=SsexBKckc4zt4glioYOOxjQqbuRBz+Tz7zwQLoMC5Hsy8vLNCvJ4GktdudJT+WYWOa gxgEPq/9iGqajltXl1IVTj1dux4VHE9u7Rjm10zcLzYhavxius2OnNz98Z1hsKGxgl+S juBJmTCezMph425+DlHsGzBlNuG77Bk8jDIvEdaehtOTRH7SeT9jfB04rTVeyNhHd/vx MuUEpEgxczB3QYudDhLBQRxvfbUGfDMLnFftBmsBsr4wQyyjhZOLxSszXKdibP9wZ0Q9 M2eF+pvKxbzAer24jX+Fno+X3vgSp5Ab0KwyjalvZTnzQvm4jUWmUe7JbRW+XsQvmw28 myhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JYxstcQ4; 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 e18si12359355pfi.130.2018.02.22.03.19.31; Thu, 22 Feb 2018 03:19:31 -0800 (PST) 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=JYxstcQ4; 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 S932160AbeBVLT2 (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:28 -0500 Received: from mail-pl0-f67.google.com ([209.85.160.67]:35124 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932147AbeBVLTZ (ORCPT ); Thu, 22 Feb 2018 06:19:25 -0500 Received: by mail-pl0-f67.google.com with SMTP id bb3so2712496plb.2 for ; Thu, 22 Feb 2018 03:19:25 -0800 (PST) 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=ODOcCvpdSx6peztqFXdleOHiebtdBOEIkJi6v/RgTc4=; b=JYxstcQ4r6JfERDlC5r91Gq23kZ5b/MYc/pg3xYNsjcdM0S5r2s/O27+4E06nR5uEe NgJp115KanxHv90vtriCqAVHR3qFFIxZGMk/uPvE3wUSuHeMq/QcJVn35t7qgUwawT9c zL1ay/I+KXB5i0X7JTzQQvVZJ775ib4Eww98M= 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=ODOcCvpdSx6peztqFXdleOHiebtdBOEIkJi6v/RgTc4=; b=Po5ytrmCDthkEB98XKSN+vBetumhIBpEJqLw2bOl7Mojq3aShbTVl8bnTYRgi6WTZr aLCtzGVxwe2S3bAnTxBUS9OQ26y2v9nkEzYnKXq5ZTV9CwjCG/lI4f/kweiOs+ZamhU/ bqHDNB26Xb/PHsf8507aL/pHBSd/RWq+e/VqUE7uFXDfk3i0fhNrHQbH1FZjMRXDyrIi WteK8TTKWgUAZR2B1oMx/eK8/RJLlg+YDJzwxZ6P/i4PNJ5engnIlkidt9mWrbHL2HIu o5NqnsaPbAtt8iKTiP4Wj/64woCL4Q+kKPoh/P/KpWGdx6HmCuptut2H9KyzjXl0Aypd E/bg== X-Gm-Message-State: APf1xPD2yB5lbiM6BukLMYhDBxDE119B62GOPICakRubvsp/XE7lDX/u irqkUerw0B7Db4aK9WfmbMk0xQ== X-Received: by 2002:a17:902:a5c5:: with SMTP id t5-v6mr6429879plq.160.1519298364619; Thu, 22 Feb 2018 03:19:24 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id 7sm34319010pfk.72.2018.02.22.03.19.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:23 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 06/13] asm-generic: add kexec_file_load system call to unistd.h Date: Thu, 22 Feb 2018 20:17:25 +0900 Message-Id: <20180222111732.23051-7-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The initial user of this system call number is arm64. Signed-off-by: AKASHI Takahiro Acked-by: Arnd Bergmann --- include/uapi/asm-generic/unistd.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) -- 2.16.2 diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index 8b87de067bc7..33761525ed2f 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -732,9 +732,11 @@ __SYSCALL(__NR_pkey_alloc, sys_pkey_alloc) __SYSCALL(__NR_pkey_free, sys_pkey_free) #define __NR_statx 291 __SYSCALL(__NR_statx, sys_statx) +#define __NR_kexec_file_load 292 +__SYSCALL(__NR_kexec_file_load, sys_kexec_file_load) #undef __NR_syscalls -#define __NR_syscalls 292 +#define __NR_syscalls 293 /* * All syscalls below here should go away really, From patchwork Thu Feb 22 11:17:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129210 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481781lja; Thu, 22 Feb 2018 03:19:39 -0800 (PST) X-Google-Smtp-Source: AH8x226vPBQWt2Vzj+Ft5FiHgnj0IOHtXK1N9HOsEMF4cUakn9wKGSro3nyPvCYD9Ha2frcbyFca X-Received: by 10.98.224.208 with SMTP id d77mr6638109pfm.194.1519298379154; Thu, 22 Feb 2018 03:19:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298379; cv=none; d=google.com; s=arc-20160816; b=NUDXQafK6iqg/hBDOJlIbAhM//F4rLffktawrj4riEN/DW98ck0v6zTsAWFEjsjPda 7vXTXiNWvw3Ucc5xwuaOlmbwtMOi0JA6UWU+u/lXv6n4gN7oeFhyOh/kywSGf7c909sU YZ8TSG0umPd9GX31kF2XXYeyx7gypPTr8JYpfKaEOIZUxoIuYSactXLgU6fPd37CGAPy K6QLABSElWbp8MXQ1PrEhJsrC/vR0KA4AwuUpkxhBiLryZLyFyC94wQzrX4CLLjkLgqX Z21HvWFJJaUZaRz9cflp7J2Yh7laHJGpRa6wQS/6MI8jMtE/nOkTFxRuyJ7rd+lMsdXr UW8g== 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=QF60Fq3LXgQ/Pd/Rp8F+Wxzz522rhmYJZ34ba4IgsUA=; b=RCVFXV8OwkFTfrkenCH/vJ2MUY3rAdAiuRMm2i7YeHg42UctSNAQhvZS1TmEVzWscl NE00Ne8c1efLhlnwrLbXLWTpYgjdxgABv55Eq4PCHLF4aI2D4SCalS3xRXLeFjaNvyr+ K88bUggAKmlp7apjJ4n7DUwljMWM1cILIdLBxg1uUhLS0lPdkeMUjN09yAk1uyEw8pi3 qv2aUd7v13Z9i8RheazGoZQNeyqVHWKPxhiqWSot9tBEYDkFfl9+6H68SMi3I3dP6Mlz vNoW6If2AmYvfiaXR361sS5dw/mHa6B8mHBVt+Jp711BnFgr65oIRGmNiw3Bpb2cpTcs IcbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q6ktHvwz; 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 e18si12359355pfi.130.2018.02.22.03.19.38; Thu, 22 Feb 2018 03:19:39 -0800 (PST) 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=Q6ktHvwz; 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 S932177AbeBVLTg (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:36 -0500 Received: from mail-it0-f67.google.com ([209.85.214.67]:38913 "EHLO mail-it0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932080AbeBVLTb (ORCPT ); Thu, 22 Feb 2018 06:19:31 -0500 Received: by mail-it0-f67.google.com with SMTP id l187so6067743ith.4 for ; Thu, 22 Feb 2018 03:19:31 -0800 (PST) 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=QF60Fq3LXgQ/Pd/Rp8F+Wxzz522rhmYJZ34ba4IgsUA=; b=Q6ktHvwzgTxkesbjdLksjP6ZQYlj6arusHeRFijc1eWX+kpvaUDiW9puGMWUFYq+RX MElfzjBUlibLERBBX/WNdzHIHYxw0H3PA78pWC9ECcskj3dqanWIjQtxWs0NA/YIwekT t2xJb/AukRPU99+8YMkV0TOiRjU3usRlDcowk= 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=QF60Fq3LXgQ/Pd/Rp8F+Wxzz522rhmYJZ34ba4IgsUA=; b=F1A4HwkhEm8mJ+8ygvUI6su/5NGqNgprhpm22CH4w37eB/9gGYEOmEYC6C/Et4km1m 6c7s40jwJw5nUGjq6ZAvO1pCn6aff9XaNVnZLp5T4/pMTaVkc3TgzC7ouwbx/BJ+bKUE E/Axe1Lby27TEP98Hl0FDipk0qFQYfkaMfv0Xwf/GEXMSzgIoirpYTa7t+eVsevzJrGc uFhFkjVWVT3drLxbEwExLpZN64lQO9KirqNLe/EJ043EDHpc8Kton2gUfqshaKy2MWhZ Q9Jwkq7k2ENHzaaUzE8jUlaTMNm0+0/FH1j+v66Wg99amY+ZaWqRVI1BHmyRYIvvtoiY bW3A== X-Gm-Message-State: APf1xPD8CeRFOm1cLb9dAa7wNuSI1OvRpJqwPGgc8LcRAYfL2RzVuvO4 vg4OJ28D2F3bQcbuDGK5YCvjXQ== X-Received: by 10.36.94.197 with SMTP id h188mr7474236itb.141.1519298371260; Thu, 22 Feb 2018 03:19:31 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id b185sm211689itb.20.2018.02.22.03.19.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:30 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 07/13] arm64: kexec_file: invoke the kernel without purgatory Date: Thu, 22 Feb 2018 20:17:26 +0900 Message-Id: <20180222111732.23051-8-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On arm64, purugatory would do almosty nothing. So just invoke the second kernel by jumping into the entry code directly. While, in this case, cpu_soft_restart() must be called in a specific way, it still stays compatible with kexec as far as the fifth argument is null. Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/kernel/cpu-reset.S | 6 +++--- arch/arm64/kernel/machine_kexec.c | 11 +++++++++-- arch/arm64/kernel/relocate_kernel.S | 3 ++- 3 files changed, 14 insertions(+), 6 deletions(-) -- 2.16.2 diff --git a/arch/arm64/kernel/cpu-reset.S b/arch/arm64/kernel/cpu-reset.S index 8021b46c9743..46fd9ea66ae8 100644 --- a/arch/arm64/kernel/cpu-reset.S +++ b/arch/arm64/kernel/cpu-reset.S @@ -24,9 +24,9 @@ * * @el2_switch: Flag to indicate a swich to EL2 is needed. * @entry: Location to jump to for soft reset. - * arg0: First argument passed to @entry. - * arg1: Second argument passed to @entry. - * arg2: Third argument passed to @entry. + * arg0: First argument passed to @entry. (rellocator's address) + * arg1: Second argument passed to @entry.(physcal kernel entry) + * arg2: Third argument passed to @entry. (physical dtb address) * * Put the CPU into the same state as it would be if it had been reset, and * branch to what would be the reset vector. It must be executed with the diff --git a/arch/arm64/kernel/machine_kexec.c b/arch/arm64/kernel/machine_kexec.c index f76ea92dff91..f7dbba00be10 100644 --- a/arch/arm64/kernel/machine_kexec.c +++ b/arch/arm64/kernel/machine_kexec.c @@ -205,10 +205,17 @@ void machine_kexec(struct kimage *kimage) * uses physical addressing to relocate the new image to its final * position and transfers control to the image entry point when the * relocation is complete. + * In case of kexec_file_load syscall, we directly start the kernel, + * skipping purgatory. */ - cpu_soft_restart(kimage != kexec_crash_image, - reboot_code_buffer_phys, kimage->head, kimage->start, 0); + reboot_code_buffer_phys, kimage->head, kimage->start, +#ifdef CONFIG_KEXEC_FILE + kimage->purgatory_info.purgatory_buf ? + 0 : kimage->arch.dtb_mem); +#else + 0); +#endif BUG(); /* Should never get here. */ } diff --git a/arch/arm64/kernel/relocate_kernel.S b/arch/arm64/kernel/relocate_kernel.S index f407e422a720..95fd94209aae 100644 --- a/arch/arm64/kernel/relocate_kernel.S +++ b/arch/arm64/kernel/relocate_kernel.S @@ -32,6 +32,7 @@ ENTRY(arm64_relocate_new_kernel) /* Setup the list loop variables. */ + mov x18, x2 /* x18 = dtb address */ mov x17, x1 /* x17 = kimage_start */ mov x16, x0 /* x16 = kimage_head */ raw_dcache_line_size x15, x0 /* x15 = dcache line size */ @@ -107,7 +108,7 @@ ENTRY(arm64_relocate_new_kernel) isb /* Start new image. */ - mov x0, xzr + mov x0, x18 mov x1, xzr mov x2, xzr mov x3, xzr From patchwork Thu Feb 22 11:17:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129211 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481870lja; Thu, 22 Feb 2018 03:19:44 -0800 (PST) X-Google-Smtp-Source: AH8x2273ew5ZKsqfda3JLaNDw2vPSnpMmY686WGkOqmVxipHRpb8xDhaeVwniAqER5niwA46EGaB X-Received: by 2002:a17:902:e81:: with SMTP id 1-v6mr6353528plx.169.1519298384567; Thu, 22 Feb 2018 03:19:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298384; cv=none; d=google.com; s=arc-20160816; b=TkV7baHdloAGm4X7xsnwlQioWcUkBdlare3ye/TIY866oPOhIjXhqqtuDJFQQsXWpY 7DKrqn8GvBuK0ZM1VqIz/fTaA9kqXG/JIN/HIOuLZ47t0ZpDjsczfbJVgYeoN2cde2Xx qXESxme5HFOx5JstkErFO4zf3jgqrvKcXpWeC0eDWfF1Hml4wYEqnCZwrsaLk8hph4Ak t1Y7jFYh5NUqKLF9SAip5FvKu5iYclRjkveZBIO+jyCopWxRGwVpoNTYOSu0+KEYSi7B H3jkGP4xJFAUqmYineVDg65c/aA87piuNtI9Iy8hbIqWH6EwNvdxXuxwtsh3pD/Y+qIT T0YA== 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=FeXRCBf7919s4saKne3LzG0WmXBDJHFjqarQilEeFj4=; b=DNTRlE+hIZlk6BneNl+wgm4jsCZg1AcLqCMim/ecmYlzF2ckxk8TUdPmZDftLopK4m LcKvbYiHFlKAeKU9iabbLk3bQOivYducrrGVjg8cac11+yQJDNlpQtsiWCZYkMQstUuc J5NbPUGKWJLawEqAP7Fni6ZPqB2g4Ohs8F87zV8ui6WJkve6IxyuqW4Oag6I4KB71H7d fLfp/4Pn2ov4rblrZfwRAoEfBFX6tgFZ3dJBp5f6hPHHNJ3LX2ufabC5EWpkboI85EB2 khGy5S/nsgCfPJonHP0q4CgPjTU8k1bOt3ypnKL1VikEiICnqYHuuJK9WX0AbnVQYjWX wWhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fAzjWS4M; 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 z7si2783815pgv.473.2018.02.22.03.19.44; Thu, 22 Feb 2018 03:19:44 -0800 (PST) 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=fAzjWS4M; 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 S932196AbeBVLTk (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:40 -0500 Received: from mail-it0-f66.google.com ([209.85.214.66]:55957 "EHLO mail-it0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932080AbeBVLTg (ORCPT ); Thu, 22 Feb 2018 06:19:36 -0500 Received: by mail-it0-f66.google.com with SMTP id n7so5834787ita.5 for ; Thu, 22 Feb 2018 03:19:36 -0800 (PST) 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=FeXRCBf7919s4saKne3LzG0WmXBDJHFjqarQilEeFj4=; b=fAzjWS4MFgWYk7VHEPgCJgTw/lDwVG34FtYbliYducyFHGPAAHyqpIiA5PaV5/3jgr da2Hv4hGnvtI0piy81NbYZ815eOB4Oeqo/sVoiNxvvWo9Kdt1vt3ULlEqKZm4YgVooHU /ndls4a14F6ShUmKBTUxrEs+kdLlI8toufwNI= 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=FeXRCBf7919s4saKne3LzG0WmXBDJHFjqarQilEeFj4=; b=gQo2UWnCpkrH72f1aZwYlN3Bfq/mRlIBfEyuc1LolaLUYtYoRQO/EpcglE2ETEB/nv o2VMCRgM+NzEc3UwKljqUYA5xWw//e6G6e0Rge7Qon41lsEnl5wrWDXwITx/3hKQCKoZ FCKBFb0lhF9JGtXAyVGl6OyF7e7k4/I4uzlSXmkYOSppE9Dlj7dAwEPAZqt5vtsAYhlK 9GMNWBXi0dSX9L2ZbVwfCtVvsglUOePpo6MVoklA2RNtmQtWoUOHr9sHxgsWKFWilTp6 LUReEDHQ1h4t/sPMji39JCNrNY7z2dvep6zS8iCiza/WXr5AWWICC15Im0EX5XnijuCM aUkQ== X-Gm-Message-State: APf1xPCBuJMOlpzTvvq0WJoKP1Y0LoGnUoQuOW9mS2TJExqrGYXNGHn9 VmI5iBXxK2UITyoCrUSZXFRetw== X-Received: by 10.36.103.148 with SMTP id u142mr7324126itc.83.1519298376114; Thu, 22 Feb 2018 03:19:36 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id p7sm16455662iof.79.2018.02.22.03.19.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:35 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 08/13] arm64: kexec_file: load initrd and device-tree Date: Thu, 22 Feb 2018 20:17:27 +0900 Message-Id: <20180222111732.23051-9-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-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_other_segments() sets up and adds all the memory segments necessary other than kernel, including initrd and device-tree blob. Most of the code was borrowed from kexec-tools' counterpart. arch_kimage_kernel_post_load_cleanup() is meant to free arm64-specific data allocated in load_other_segments(). Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/include/asm/kexec.h | 19 ++++ arch/arm64/kernel/Makefile | 3 +- arch/arm64/kernel/machine_kexec_file.c | 189 +++++++++++++++++++++++++++++++++ 3 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kernel/machine_kexec_file.c -- 2.16.2 diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index e17f0529a882..fc562db22d46 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -93,6 +93,25 @@ static inline void crash_prepare_suspend(void) {} static inline void crash_post_resume(void) {} #endif +#ifdef CONFIG_KEXEC_FILE +#define ARCH_HAS_KIMAGE_ARCH + +struct kimage_arch { + phys_addr_t dtb_mem; + void *dtb_buf; +}; + +struct kimage; + +#define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup +extern int arch_kimage_file_post_load_cleanup(struct kimage *image); + +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); +#endif + #endif /* __ASSEMBLY__ */ #endif diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index b87541360f43..151dc890737c 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -47,8 +47,9 @@ arm64-obj-$(CONFIG_ARM64_ACPI_PARKING_PROTOCOL) += acpi_parking_protocol.o arm64-obj-$(CONFIG_PARAVIRT) += paravirt.o arm64-obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o arm64-obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o -arm64-obj-$(CONFIG_KEXEC) += machine_kexec.o relocate_kernel.o \ +arm64-obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \ cpu-reset.o +arm64-obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o arm64-obj-$(CONFIG_ARM64_RELOC_TEST) += arm64-reloc-test.o arm64-reloc-test-y := reloc_test_core.o reloc_test_syms.o arm64-obj-$(CONFIG_CRASH_DUMP) += crash_dump.o diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c new file mode 100644 index 000000000000..12012f247501 --- /dev/null +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -0,0 +1,189 @@ +/* + * kexec_file for arm64 + * + * Copyright (C) 2018 Linaro Limited + * Author: AKASHI Takahiro + * + * Most code is derived from arm64 port of kexec-tools + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#define pr_fmt(fmt) "kexec_file: " fmt + +#include +#include +#include +#include +#include +#include + +static int __dt_root_addr_cells; +static int __dt_root_size_cells; + +const struct kexec_file_ops * const kexec_file_loaders[] = { + NULL +}; + +int arch_kimage_file_post_load_cleanup(struct kimage *image) +{ + vfree(image->arch.dtb_buf); + image->arch.dtb_buf = NULL; + + return _kimage_file_post_load_cleanup(image); +} + +int arch_kexec_walk_mem(struct kexec_buf *kbuf, + int (*func)(struct resource *, 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 if (kbuf->top_down) + return walk_system_ram_res_rev(0, ULONG_MAX, kbuf, func); + else + return walk_system_ram_res(0, ULONG_MAX, kbuf, func); +} + +static int setup_dtb(struct kimage *image, + unsigned long initrd_load_addr, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len, + char **dtb_buf, size_t *dtb_buf_len) +{ + char *buf = NULL; + size_t buf_size; + int nodeoffset; + u64 value; + int range_len; + int ret; + + /* duplicate dt blob */ + buf_size = fdt_totalsize(initial_boot_params); + range_len = (__dt_root_addr_cells + __dt_root_size_cells) * sizeof(u32); + + if (initrd_load_addr) + buf_size += fdt_prop_len("initrd-start", sizeof(u64)) + + fdt_prop_len("initrd-end", sizeof(u64)); + + if (cmdline) + buf_size += fdt_prop_len("bootargs", cmdline_len + 1); + + buf = vmalloc(buf_size); + if (!buf) { + ret = -ENOMEM; + goto out_err; + } + + ret = fdt_open_into(initial_boot_params, buf, buf_size); + if (ret) + goto out_err; + + nodeoffset = fdt_path_offset(buf, "/chosen"); + if (nodeoffset < 0) + goto out_err; + + /* add bootargs */ + if (cmdline) { + ret = fdt_setprop(buf, nodeoffset, "bootargs", + cmdline, cmdline_len + 1); + if (ret) + goto out_err; + } + + /* add initrd-* */ + if (initrd_load_addr) { + value = cpu_to_fdt64(initrd_load_addr); + ret = fdt_setprop(buf, nodeoffset, "initrd-start", + &value, sizeof(value)); + if (ret) + goto out_err; + + value = cpu_to_fdt64(initrd_load_addr + initrd_len); + ret = fdt_setprop(buf, nodeoffset, "initrd-end", + &value, sizeof(value)); + if (ret) + goto out_err; + } + + /* trim a buffer */ + fdt_pack(buf); + *dtb_buf = buf; + *dtb_buf_len = fdt_totalsize(buf); + + return 0; + +out_err: + vfree(buf); + return ret; +} + +int load_other_segments(struct kimage *image, unsigned long kernel_load_addr, + char *initrd, unsigned long initrd_len, + char *cmdline, unsigned long cmdline_len) +{ + struct kexec_buf kbuf; + unsigned long initrd_load_addr = 0; + char *dtb = NULL; + unsigned long dtb_len = 0; + int ret = 0; + + kbuf.image = image; + /* not allocate anything below the kernel */ + kbuf.buf_min = kernel_load_addr; + + /* Load initrd */ + if (initrd) { + kbuf.buffer = initrd; + kbuf.bufsz = initrd_len; + kbuf.memsz = initrd_len; + kbuf.buf_align = PAGE_SIZE; + /* within 1GB-aligned window of up to 32GB in size */ + kbuf.buf_max = round_down(kernel_load_addr, SZ_1G) + + (unsigned long)SZ_1G * 31; + kbuf.top_down = 0; + + ret = kexec_add_buffer(&kbuf); + if (ret) + goto out_err; + initrd_load_addr = kbuf.mem; + + pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + initrd_load_addr, initrd_len, initrd_len); + } + + /* Load dtb blob */ + ret = setup_dtb(image, initrd_load_addr, initrd_len, + cmdline, cmdline_len, &dtb, &dtb_len); + if (ret) { + pr_err("Preparing for new dtb failed\n"); + goto out_err; + } + + kbuf.buffer = dtb; + kbuf.bufsz = dtb_len; + kbuf.memsz = dtb_len; + /* not across 2MB boundary */ + kbuf.buf_align = SZ_2M; + kbuf.buf_max = ULONG_MAX; + kbuf.top_down = 1; + + ret = kexec_add_buffer(&kbuf); + if (ret) + goto out_err; + image->arch.dtb_mem = kbuf.mem; + image->arch.dtb_buf = dtb; + + pr_debug("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + kbuf.mem, dtb_len, dtb_len); + + return 0; + +out_err: + vfree(dtb); + image->arch.dtb_buf = NULL; + return ret; +} From patchwork Thu Feb 22 11:17:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129212 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp481957lja; Thu, 22 Feb 2018 03:19:50 -0800 (PST) X-Google-Smtp-Source: AH8x224qJAu7clekjNGBYA6C5RH/rUVWHpY/gF0hG8j+NCiemwTnE/wY9jFVOCzbOBScZPWpG7oc X-Received: by 10.98.157.18 with SMTP id i18mr6611520pfd.62.1519298389904; Thu, 22 Feb 2018 03:19:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298389; cv=none; d=google.com; s=arc-20160816; b=nnkZbj4VtcwpBqy43mJVE2U3tiQCgMHcmdBJPYTwuYf3PZu2Joh8ciDGjfaXDgseYq iYrJTazRmhr6FTUIdqpyAFhs3Bkd4sImGeLTxsZLo9BLedwFU81pmMV0uAaKWKNj9NT5 rAZucqbztiaoeuK8tBSFvpm7Wv+xcsAFIQSWC5S7chjoclcfbgV8DOWZZXeeRV+ha484 /lfLvISWyTtnUs2o3Q/ADiWiNjLxgHt6Y/tqgeUY7U8yR5PhcOgF+L+qgKSsS6VmjAYn p2h2MlGiNkOEWxZygXlaLupyc9XHZOG5YdI2BLhzcJe1pnLtmheJPNK6i3gzoged8Ksl J0FA== 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=pag8aDw5NOH+WORa/0KmWZWZmG7E16se2nJyZIom+e4=; b=Uxcy6PMXzqnSRj1R/alTIv/yc7wxyQ7iNYktgNQ/2d/w3c+QRL2dVhxbavFbWQJnN5 TBnUB90njZwW18UyJDqS0FBRL8ul2vS/Ox3ZQTjGZvo4BVmQR6TVYFXhEpPig1x3SIMS Syic2vajDr0FgjHmlA7xrZJmVqjOKECn/5Dm/QMkaPTQ2xeW3tMPK9af4sn+IiwEgsQ1 zt5mTNl5457EqlAGz122gdTBLDIy4ZRvq7O+xgfpDuYvanu9m3oZb77iszcK1Y9HALJ/ hogKZr22hkOLNlwDoY5VyzY2oP/quKT1jTpDKoxaF9t8aupZVYEwgzCiVSSXmguPzjSM 47TA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=P/fP3lIS; 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 z7si2783815pgv.473.2018.02.22.03.19.49; Thu, 22 Feb 2018 03:19:49 -0800 (PST) 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=P/fP3lIS; 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 S932206AbeBVLTq (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:46 -0500 Received: from mail-io0-f194.google.com ([209.85.223.194]:42035 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932080AbeBVLTm (ORCPT ); Thu, 22 Feb 2018 06:19:42 -0500 Received: by mail-io0-f194.google.com with SMTP id u84so5625916iod.9 for ; Thu, 22 Feb 2018 03:19:42 -0800 (PST) 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=pag8aDw5NOH+WORa/0KmWZWZmG7E16se2nJyZIom+e4=; b=P/fP3lISg6ngB341mfK+wVPsuNbOFqBUVAiinulpfZhrGUG9YzYa/p6coN4Gg144vw 0lRcVj0nt8Teh3lDK7bV/blkzAVN+K7luEtKW/QtC3hV8o62pCjWETLDIfsuawJJ7VXF LJHD19CokvNtVQ1vXI1VGERcOXNqs/HJwWGZE= 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=pag8aDw5NOH+WORa/0KmWZWZmG7E16se2nJyZIom+e4=; b=lOe4arOXSQx4t9bJGAVmL+7VuyjAvsDcZRzExSnqSKzJO302u8CIUSd3MjqpWs2rZT XGfI90ZuZ2p75pwO0xDpG521Z27+qqKde96+xxZbIlpER4LSnaL6A+K+qyxgiGuvKOaG i4tXO1FuFy16QfFrNw20MAl1H/9fovUYth1E630g82DfYY+vhyzwIEexr+5gPDQSejsI z8ZJfFYF2KPv++q1botRnlDcx9SxsHmPapULKGzJmhhlY7oBZB+gUyPZjfrqJ21QFG5Z a+ok0s/CuPk0qJ5mKQlEMh5PYbuQhluFYQEks23v1t/aRAo+df0WhMVkMgoN9FkFjymJ VjZw== X-Gm-Message-State: APf1xPAl5vAvXmemMNcDfSJ6OSuLAi9ovbo2uUo67T0vomISF+L5vmOJ Bhmf8bZ+Y75+Ge3D+TxETApUWA== X-Received: by 10.107.148.142 with SMTP id w136mr8364748iod.65.1519298381832; Thu, 22 Feb 2018 03:19:41 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id z1sm190685itg.37.2018.02.22.03.19.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:40 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 09/13] arm64: kexec_file: add crash dump support Date: Thu, 22 Feb 2018 20:17:28 +0900 Message-Id: <20180222111732.23051-10-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To enable crash dump (kdump), we need to * prepare the contents of ELF header of /proc/vmcore through load_crashdump_segments(), and * set up two device tree properties, "linux,usable-memory-range" and "linux,elfcorehdr", which repsectively represent a memory range to be used on crash dump kernel and a region of ELF core header (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 | 211 +++++++++++++++++++++++++++++++++ 2 files changed, 216 insertions(+) -- 2.16.2 diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index fc562db22d46..d7427d510e1b 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -99,6 +99,10 @@ static inline void crash_post_resume(void) {} struct kimage_arch { phys_addr_t dtb_mem; void *dtb_buf; + /* Core ELF header buffer */ + void *elf_headers; + unsigned long elf_headers_sz; + unsigned long elf_load_addr; }; struct kimage; @@ -110,6 +114,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 12012f247501..fc132047c8cd 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -19,6 +19,7 @@ #include #include #include +#include static int __dt_root_addr_cells; static int __dt_root_size_cells; @@ -32,6 +33,10 @@ int arch_kimage_file_post_load_cleanup(struct kimage *image) vfree(image->arch.dtb_buf); image->arch.dtb_buf = NULL; + vfree(image->arch.elf_headers); + image->arch.elf_headers = NULL; + image->arch.elf_headers_sz = 0; + return _kimage_file_post_load_cleanup(image); } @@ -49,6 +54,78 @@ int arch_kexec_walk_mem(struct kexec_buf *kbuf, 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; + + if (cells == 1) { + val32 = cpu_to_fdt32((u32)val64); + memcpy(buf, &val32, sizeof(val32)); + } else { + memset(buf, 0, cells * sizeof(u32) - sizeof(u64)); + buf += cells * sizeof(u32) - sizeof(u64); + + 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) +{ + void *buf, *prop; + size_t buf_size; + int result; + + buf_size = (__dt_root_addr_cells + __dt_root_size_cells) * sizeof(u32); + prop = buf = vmalloc(buf_size); + if (!buf) + return -ENOMEM; + + fill_property(prop, addr, __dt_root_addr_cells); + prop += __dt_root_addr_cells * sizeof(u32); + + fill_property(prop, size, __dt_root_size_cells); + + result = fdt_setprop(fdt, nodeoffset, name, buf, buf_size); + + vfree(buf); + + return result; +} + static int setup_dtb(struct kimage *image, unsigned long initrd_load_addr, unsigned long initrd_len, char *cmdline, unsigned long cmdline_len, @@ -61,10 +138,26 @@ static 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)); @@ -86,6 +179,23 @@ static 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", @@ -187,3 +297,104 @@ int load_other_segments(struct kimage *image, unsigned long kernel_load_addr, image->arch.dtb_buf = NULL; return ret; } + +static int get_nr_ranges_callback(struct resource *res, void *arg) +{ + unsigned int *nr_ranges = arg; + + (*nr_ranges)++; + return 0; +} + +static int add_mem_range_callback(struct resource *res, void *arg) +{ + struct crash_mem *cmem = arg; + + cmem->ranges[cmem->nr_ranges].start = res->start; + cmem->ranges[cmem->nr_ranges].end = res->end; + cmem->nr_ranges++; + + return 0; +} + +static struct crash_mem *get_crash_memory_ranges(void) +{ + unsigned int nr_ranges; + struct crash_mem *cmem; + + nr_ranges = 1; /* for exclusion of crashkernel region */ + walk_system_ram_res(0, -1, &nr_ranges, get_nr_ranges_callback); + + cmem = vmalloc(sizeof(struct crash_mem) + + sizeof(struct crash_mem_range) * nr_ranges); + if (!cmem) + return NULL; + + cmem->max_nr_ranges = nr_ranges; + cmem->nr_ranges = 0; + walk_system_ram_res(0, -1, cmem, add_mem_range_callback); + + /* Exclude crashkernel region */ + if (crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end)) { + vfree(cmem); + return NULL; + } + + return cmem; +} + +static int prepare_elf_core_header(void **addr, unsigned long *sz) +{ + struct crash_mem *cmem; + int ret = 0; + + cmem = get_crash_memory_ranges(); + if (!cmem) + return -ENOMEM; + + /* 1: add segment for kernel map */ + ret = crash_prepare_elf64_headers(cmem, 1, addr, sz); + + vfree(cmem); + 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; + + ret = prepare_elf_core_header(&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; +} From patchwork Thu Feb 22 11:17:29 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129213 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp482069lja; Thu, 22 Feb 2018 03:19:58 -0800 (PST) X-Google-Smtp-Source: AH8x2273UGrIoG2Youi21RzKwBS6vRdyfOPWPJ1kHVZzOHi1uk1TP5KZWiDAEwPqQqz2Rn6SqH/M X-Received: by 2002:a17:902:8d81:: with SMTP id v1-v6mr6207851plo.430.1519298398211; Thu, 22 Feb 2018 03:19:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298398; cv=none; d=google.com; s=arc-20160816; b=teUahQYe0WW/iI6foIQBmj7TfD1q3VkGo5KZm8gUUZ9HPGdtwDR42wxcbm2pOTTnWP 3N6PcnsRgFylnWYXuBQubt73xUEkW4O5oppodyveVHAQ3OZjCiS+1ZPpYRIaT8ohuNaw tQu6W+Rl7IxG3MgfaiCRhpwk/KVEn1ojRbzHmFjgfYVhuXGyVz3h5x0P7zPG4rspJVcu e0Ap+A3DToeeIfsr/neC0/0yEC2ikiVtRJuUNB/WPS5AoigWuForofoyE0zzewf9jH5o jAGw+0zH/pn/1cqBgq1hr11cwml56h2Y3fEZap2TeKt5H4TXNrg2JuGiunhHL2K0lnL9 VgLQ== 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=4ojgkLUeMnzShFW7B1qBplIWMSH7iuYhyTsUehR83J0=; b=oGyVBEOm/7KjbRgJRZ+UxQNhPVWn3gaMY1IHY3nZZOl3GDoIY07xxDlTGvtJsRl8Wo aM49FD/wLwqOPmaJU/Cj+GcXZOVWpSc3pkAtJYcBMGDoDsR9/AeuU9b4x+2x+E6XbkX3 BY90JDORG12ThfxNUC3exbrV0WB2zQg+RYMWlo63LPZOuZFdtdm7c/24uteHmFiPiC/l fhKJWIVadfm7TDJFl6Va46jGzUNd1PHx+EtaU+D/WQ2KN54oni8QY0V3piVFh/BRkDdQ yqtb3FMe7/a/vyLU58BQuSg4YZRtdvlIt1x3SBXkoxrTfq4RRAJ/ICRo8rtWXTfIf7LT SRYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=eQNfphRE; 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 k8si64866pgc.472.2018.02.22.03.19.57; Thu, 22 Feb 2018 03:19:58 -0800 (PST) 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=eQNfphRE; 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 S932218AbeBVLTz (ORCPT + 28 others); Thu, 22 Feb 2018 06:19:55 -0500 Received: from mail-io0-f193.google.com ([209.85.223.193]:39038 "EHLO mail-io0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753643AbeBVLTt (ORCPT ); Thu, 22 Feb 2018 06:19:49 -0500 Received: by mail-io0-f193.google.com with SMTP id b34so5634234ioj.6 for ; Thu, 22 Feb 2018 03:19:49 -0800 (PST) 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=4ojgkLUeMnzShFW7B1qBplIWMSH7iuYhyTsUehR83J0=; b=eQNfphRER/oIY6fM7BDAAV42GjzwzRY11YHPjux4nibmf98asFtcosecoRXie+kpkp HTT/ic0lubBxIWSbJFJPxP1yKVTrdNDaCeYw60YhABzMKXrCSdqlv3wvR4Z6uL3hPEzD y7k+xuaFX6u+7eFbY6tclYF7sWVSvegecaug8= 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=4ojgkLUeMnzShFW7B1qBplIWMSH7iuYhyTsUehR83J0=; b=ZONZN8lj7jEWWHMGuXAGQuXA43g9auehLi+AT0RPjiXEZ1LqljOww2mQGXq0wmsz3Q DSqs+OUrVijworL6Z64IcuiR1cIWTsvgWJzTSnhs4Gcq6JhxhSVQEhF5DvgR1jCj7m86 qcFBZ5FhHmze0+MhbXQsMbmbVxbCAfJ2ZYbVW9yAm33D2GvLirR4PfGpEdj1AVj7vsY1 ZtA4+uoC10NDnDF4czjuwasrmjY2zyVfYPBBeEY9GNILq3z1aQ5jmJudk8XG0tha9IZR B/SuSJklqO3RoVGfXcKpTu511WWSsnCxcOKPBLPZgyBg6pDighagjw+gEPRGnGCs1w3y oekg== X-Gm-Message-State: APf1xPD8ild47OPJCP6s8Vf6aFOy5ISpuGOtBxhRfHLve00GbzhIlZzI ZB5B+b6gvcdZNTX6+h+NqEsmqg== X-Received: by 10.107.50.17 with SMTP id y17mr7797691ioy.223.1519298388639; Thu, 22 Feb 2018 03:19:48 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id l16sm6206503ioc.80.2018.02.22.03.19.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:47 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 10/13] arm64: kexec_file: add Image format support Date: Thu, 22 Feb 2018 20:17:29 +0900 Message-Id: <20180222111732.23051-11-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch provides kexec_file_ops for "Image"-format kernel. Please note that a binary is always loaded at an offset based on its text_offset field. While this patch doesn't contains CONFIG_KEXEC_VERIFY_SIG support, that is to be added in a later patch in this series, file-attribute-based kernel verification can now be materialised by enabling IMA security subsystem. See more details about IMA here: https://sourceforge.net/p/linux-ima/wiki/Home/ You can sign(label) a kernel image on target filesystem to be kexec-ed with: $ evmctl ima_sign --key /path/to/private_key.pem Image On live system, you must have IMA enforced with, at least, the following security policy: "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig" Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/include/asm/kexec.h | 50 +++++++++++++++++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/kexec_image.c | 90 ++++++++++++++++++++++++++++++++++ arch/arm64/kernel/machine_kexec_file.c | 1 + 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/kernel/kexec_image.c -- 2.16.2 diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index d7427d510e1b..592890085aae 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -105,6 +105,56 @@ struct kimage_arch { unsigned long elf_load_addr; }; +/** + * struct arm64_image_header - arm64 kernel image header + * + * @pe_sig: Optional PE format 'MZ' signature + * @branch_code: Instruction to branch to stext + * @text_offset: Image load offset, little endian + * @image_size: Effective image size, little endian + * @flags: + * Bit 0: Kernel endianness. 0=little endian, 1=big endian + * @reserved: Reserved + * @magic: Magic number, "ARM\x64" + * @pe_header: Optional offset to a PE format header + **/ + +struct arm64_image_header { + u8 pe_sig[2]; + u8 pad[2]; + u32 branch_code; + u64 text_offset; + u64 image_size; + u64 flags; + u64 reserved[3]; + u8 magic[4]; + u32 pe_header; +}; + +static const u8 arm64_image_magic[4] = {'A', 'R', 'M', 0x64U}; + +/** + * arm64_header_check_magic - Helper to check the arm64 image header. + * + * Returns non-zero if header is OK. + */ + +static inline int arm64_header_check_magic(const struct arm64_image_header *h) +{ + if (!h) + return 0; + + if (!h->text_offset) + return 0; + + return (h->magic[0] == arm64_image_magic[0] + && h->magic[1] == arm64_image_magic[1] + && h->magic[2] == arm64_image_magic[2] + && h->magic[3] == arm64_image_magic[3]); +} + +extern const struct kexec_file_ops kexec_image_ops; + struct kimage; #define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index 151dc890737c..454b2735603a 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -49,7 +49,7 @@ arm64-obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o arm64-obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o arm64-obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \ cpu-reset.o -arm64-obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o +arm64-obj-$(CONFIG_KEXEC_FILE) += machine_kexec_file.o kexec_image.o arm64-obj-$(CONFIG_ARM64_RELOC_TEST) += arm64-reloc-test.o arm64-reloc-test-y := reloc_test_core.o reloc_test_syms.o arm64-obj-$(CONFIG_CRASH_DUMP) += crash_dump.o diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c new file mode 100644 index 000000000000..de62def63dd6 --- /dev/null +++ b/arch/arm64/kernel/kexec_image.c @@ -0,0 +1,90 @@ +/* + * Kexec image loader + + * Copyright (C) 2018 Linaro Limited + * Author: AKASHI Takahiro + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#define pr_fmt(fmt) "kexec_file(Image): " fmt + +#include +#include +#include +#include +#include +#include + +static int image_probe(const char *kernel_buf, unsigned long kernel_len) +{ + const struct arm64_image_header *h; + + h = (const struct arm64_image_header *)(kernel_buf); + + if ((kernel_len < sizeof(*h)) || !arm64_header_check_magic(h)) + return -EINVAL; + + return 0; +} + +static void *image_load(struct kimage *image, char *kernel, + unsigned long kernel_len, char *initrd, + unsigned long initrd_len, char *cmdline, + unsigned long cmdline_len) +{ + struct kexec_buf kbuf; + struct arm64_image_header *h = (struct arm64_image_header *)kernel; + unsigned long text_offset; + int ret; + + /* Create elf core header segment */ + ret = load_crashdump_segments(image); + if (ret) + goto out; + + /* Load the kernel */ + kbuf.image = image; + if (image->type == KEXEC_TYPE_CRASH) { + kbuf.buf_min = crashk_res.start; + kbuf.buf_max = crashk_res.end + 1; + } else { + kbuf.buf_min = 0; + kbuf.buf_max = ULONG_MAX; + } + kbuf.top_down = 0; + + kbuf.buffer = kernel; + kbuf.bufsz = kernel_len; + kbuf.memsz = le64_to_cpu(h->image_size); + text_offset = le64_to_cpu(h->text_offset); + kbuf.buf_align = SZ_2M; + + /* Adjust kernel segment with TEXT_OFFSET */ + kbuf.memsz += text_offset; + + ret = kexec_add_buffer(&kbuf); + if (ret) + goto out; + + image->segment[image->nr_segments - 1].mem += text_offset; + image->segment[image->nr_segments - 1].memsz -= text_offset; + image->start = kbuf.mem + text_offset; + + pr_debug("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", + image->start, kbuf.bufsz, kbuf.memsz); + + /* Load additional data */ + ret = load_other_segments(image, image->start, + initrd, initrd_len, cmdline, cmdline_len); + +out: + return ERR_PTR(ret); +} + +const struct kexec_file_ops kexec_image_ops = { + .probe = image_probe, + .load = image_load, +}; diff --git a/arch/arm64/kernel/machine_kexec_file.c b/arch/arm64/kernel/machine_kexec_file.c index fc132047c8cd..384146583f8d 100644 --- a/arch/arm64/kernel/machine_kexec_file.c +++ b/arch/arm64/kernel/machine_kexec_file.c @@ -25,6 +25,7 @@ static int __dt_root_addr_cells; static int __dt_root_size_cells; const struct kexec_file_ops * const kexec_file_loaders[] = { + &kexec_image_ops, NULL }; From patchwork Thu Feb 22 11:17:30 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129214 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp482148lja; Thu, 22 Feb 2018 03:20:04 -0800 (PST) X-Google-Smtp-Source: AH8x227VB9hOa1kbfrWOPcNMlJEV/n8e6WHD74jz92TFMKZhvuFhv8U7HPi1nlE7glNJ7Vcssw4x X-Received: by 10.101.81.204 with SMTP id i12mr5382654pgq.206.1519298404386; Thu, 22 Feb 2018 03:20:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298404; cv=none; d=google.com; s=arc-20160816; b=DXOgJdLl/LTPWAbz2tqG7O9ZYQuEDXwg6Hc3lCW5jG5LnfcvfQc7PN25uhmX53wnCM UX/YvEjtTq4AZLWGQDJ0psBDZS2dlpsqN1FrbxQ+su2hRGE8zaRaNnAxyGNCXQ2PMLuC 0BRgxNxpf06pJpvhYAAA6SLfXwqzwEFha+Rh3j0sG60BngGL2QQXXp14pTE8wewwPVv4 RPqztx+2kqbpC2pgpaxA5yzkbbvkx4YxgVsFpOw9QTJzUjZCgJSI2pYimSXGALftO4R4 kg1eUF3bTGyOX49Inh38rm3w7DedH4gnWj/HGQD0t5CeGSvpO9AMVcFaz5/RGWgvI2fH H8fA== 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=EDWf381AC+81kWWce75bws6AddvzmzUGCExwZPnd4zU=; b=HuwHmG8SdXxz8I/YMnIi9I0tJOP4eihu79bd54oYturzSMIFPPl0mUzIGKaWIuO5gD t+vQSPE8q8Zner7AdfEPnKrPELmdvAUrSvjQorzhDe3ukinXUr39Cwhd5uYCzZ/wfQbn KJdKBjwum/gEqYwHSuUJ5vuY/GtaUwKUvGLQ9p9qmYk3kwAO8WQa2EzQ9Z6u9dVreqwI 4liBKyFYRs75WIjhwB6IFiY8PXosfny4kS9D1c9V6jHeBhRCtYDlNXy8GC4OrdorXYY7 jdUZ+csrD9K8mCD9xK1ooxSq9xqopD9CV8eCjE1MbY/P9kkWJs6j0ZdUPa/Qf9lv4bGy k2WA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QpIZO/OX; 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 k8si64866pgc.472.2018.02.22.03.20.04; Thu, 22 Feb 2018 03:20:04 -0800 (PST) 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=QpIZO/OX; 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 S932234AbeBVLUA (ORCPT + 28 others); Thu, 22 Feb 2018 06:20:00 -0500 Received: from mail-it0-f67.google.com ([209.85.214.67]:40216 "EHLO mail-it0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932080AbeBVLTy (ORCPT ); Thu, 22 Feb 2018 06:19:54 -0500 Received: by mail-it0-f67.google.com with SMTP id v186so6065553itc.5 for ; Thu, 22 Feb 2018 03:19:54 -0800 (PST) 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=EDWf381AC+81kWWce75bws6AddvzmzUGCExwZPnd4zU=; b=QpIZO/OXyjsvC54/fNZkihNXp1v0PX5U1lm/jBlhZhG5jSXROy6hiCswDMkMhlTGFb ilu9QIJIzoqjEVfYhAZUuQW7DXlSW0RPfW9Hvkvi7w7wrVzCyM34oZXpUhxgmlBAdCFo 6eq9I8fukYoak5K0CIdJvE/hAYHq/66xQ6Cys= 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=EDWf381AC+81kWWce75bws6AddvzmzUGCExwZPnd4zU=; b=XmJ5WrQGrgVS1x8hZVsc6LGp2JU8DHfKL4eTSFisUjZ1UB8BvYat287ghzQPhtzVpI grJwar3DA6Q77V6N628UJJoanZrlLt4lydYx9+6hCsWoefp39/ZQT5iG3mm8ytKT2mtk 5/uV6kGtnDoH37chjcNzW36KLiXfrq2IsDxmMAVE/Pf2OTuI+t13G0fKw00amR4O0DfS hq8N8+lCaRUfxTlVPSzjorZtMnO/HFz/pRIyJbXurDt8lPgSLjXnbWKqL1j25H6ObbuL IGvhm27PLGOrH6hA4qrfZAdnymWX+NHYWGJEMxPRX8Ea0ZS6llzUL3C9A0mLb8SrAsge OW0w== X-Gm-Message-State: APf1xPAJz0e7FbIx3P2zHXhFe/JraYju4AyrKbNe/2CVE72NeO5KrbLp gJx8dLHLTtKObrY8QMFj0U0FQw== X-Received: by 10.36.69.131 with SMTP id c3mr7348425itd.126.1519298394239; Thu, 22 Feb 2018 03:19:54 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id h2sm11028813ioa.77.2018.02.22.03.19.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:53 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 11/13] arm64: kexec_file: enable KEXEC_FILE config Date: Thu, 22 Feb 2018 20:17:30 +0900 Message-Id: <20180222111732.23051-12-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Modify arm64/Kconfig to enable kexec_file_load support. Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/Kconfig | 10 ++++++++++ 1 file changed, 10 insertions(+) -- 2.16.2 diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7381eeb7ef8e..79ee27b8d2a0 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -829,6 +829,16 @@ config KEXEC but it is independent of the system firmware. And like a reboot you can start any kernel with it, not just Linux. +config KEXEC_FILE + bool "kexec file based system call" + select KEXEC_CORE + select BUILD_BIN2C + ---help--- + This is new version of kexec system call. This system call is + file based and takes file descriptors as system call argument + for kernel and initramfs as opposed to list of segments as + accepted by previous system call. + config CRASH_DUMP bool "Build kdump crash kernel" help From patchwork Thu Feb 22 11:17:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129215 Delivered-To: patch@linaro.org Received: by 10.80.172.228 with SMTP id x91csp1881347edc; Thu, 22 Feb 2018 03:20:33 -0800 (PST) X-Google-Smtp-Source: AH8x226DIluONmUkL0LB2Del+Tr+Y3uNGdqL/sF0Fk1XiqyWaWwj4AA5YQ2M8Kez8IlH1kdt8FlC X-Received: by 10.98.61.133 with SMTP id x5mr3602188pfj.181.1519298433403; Thu, 22 Feb 2018 03:20:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298433; cv=none; d=google.com; s=arc-20160816; b=olZErqb7y9NnNj14FjvFtX8QCpa/M9WjcvfhRuDwsTZH5hD1be29ogrr/2x15sXipn U8yyhHyiouIbOXV8zbW4laMpRaWcnsyS9KkqRr1WlUjY3yHXTXkXqYpPkHKHwohpmeIj ed/ZT0C2kBIs5FHaIkcnYJBadBAbBYfeUcJsfmZrVZRsKl1+deYklOwSIwupnA2MhEjY ytrljEC88Ntoi5vEt8SWkIpdx/P9qJuD5yVEa+r17PdjHFSnSXuxUmtTJCZ19irP8kuz 6aktrPMCm4W5T/VThBiiMswjxppYo9RpgGthxXKPJBuvxfkiUOr8NRk8vnDIxEu7ftIs 1+kA== 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=uaS4aTMgyvhoEwZbjgAZtndudIYAMy0APlmirhXzKaU=; b=T6vLzcMJjXLhZwXDSLdPAPtMx41lR/JW058nyeiV+rD4wxi7LSOFknn2KTwti9ZKk2 dp/VmSNkyLgNjxZ+7HKDoTm+GPky2xZjzaaostLrXaZCoiS03I27asaYtW1loSXak0YV /4dCJ60bnM6/yFchqIcfFrXAIVC14nCU93Aedbya5Mnaa8wed9i5wwI6ilL8iBVmCyHn kYBgvQylG3H9BjwT76FQ4LAu5wqHkGIFKCByIT64fa51Y3CkF3FqdPpxZDM2iWFp+qba sA56YIaVk61TNQdnazOKRzzwqbUaaeXhAGa3AQpNCXRuZbpIwzG0CECmjvr9kjy/7pn9 vJLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YnifjGKt; 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 k8si64866pgc.472.2018.02.22.03.20.33; Thu, 22 Feb 2018 03:20:33 -0800 (PST) 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=YnifjGKt; 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 S932260AbeBVLU3 (ORCPT + 28 others); Thu, 22 Feb 2018 06:20:29 -0500 Received: from mail-io0-f196.google.com ([209.85.223.196]:41310 "EHLO mail-io0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753633AbeBVLT7 (ORCPT ); Thu, 22 Feb 2018 06:19:59 -0500 Received: by mail-io0-f196.google.com with SMTP id e4so5617578iob.8 for ; Thu, 22 Feb 2018 03:19:59 -0800 (PST) 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=uaS4aTMgyvhoEwZbjgAZtndudIYAMy0APlmirhXzKaU=; b=YnifjGKtLe2c6uw8HD5Z9CrTs3OG4pdc8LQJp6MkHgrXt/d5hd/IARei5/ETlWlxce meuuZgosjy9HQsWJRVU68u7ZqmS9lC08N1SO2XbEVh612igyvh2s+DYmEco9rY7X6/HE QzrrcoEGthS4kWj6MetR0fmaP2htb9W4sTWbM= 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=uaS4aTMgyvhoEwZbjgAZtndudIYAMy0APlmirhXzKaU=; b=LM9wd2Ft5w9qZKOu9aJY6GhHGjJ3uMmlJ/zGiXk+Fc9PGNmO/s8U6e2L6mcffEKW2T vKx2D6eDL7vWGnVcwK+D/8byGVICa532wzYJKu95QDuSreH5Zy1AnfZR0BlId7MZOt8H HAdKEMC6kqmbmh5JHJ8+mmr60Wp5WL9GpFiBMBuUoT0DAtr5nHRG7lDyONhtPlOyUZGH BlK+wiDI7x3s3hn6qea3jVp6IeXyeLLCMZO8jkaUILKdBM+IqTr3bghHFn5zEol5Lo7g eeI3MgzRhU94hyRxQ9LwIzJhDKn8Zix1N/e970S95gJcSp2h8DZ9d49cexbav+Hnp0qa UF3w== X-Gm-Message-State: APf1xPCo7kgGveBiBLT1+RhoHfe5u8lq8jblaXQHZftPe20zH0FSWbOg dgv8NLcqhbyLHCE13/uVxlTb3g== X-Received: by 10.107.139.77 with SMTP id n74mr8275896iod.109.1519298398857; Thu, 22 Feb 2018 03:19:58 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id x72sm218804ite.26.2018.02.22.03.19.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:19:58 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 12/13] include: pe.h: remove message[] from mz header definition Date: Thu, 22 Feb 2018 20:17:31 +0900 Message-Id: <20180222111732.23051-13-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org message[] field won't be part of the definition of mz header. This change is crucial for enabling kexec_file_load on arm64 because arm64's "Image" binary, as in PE format, doesn't have any data for it and accordingly the following check in pefile_parse_binary() will fail: chkaddr(cursor, mz->peaddr, sizeof(*pe)); Signed-off-by: AKASHI Takahiro Reviewed-by: Ard Biesheuvel Cc: David Howells Cc: Vivek Goyal Cc: Herbert Xu Cc: David S. Miller --- include/linux/pe.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- 2.16.2 diff --git a/include/linux/pe.h b/include/linux/pe.h index 143ce75be5f0..3482b18a48b5 100644 --- a/include/linux/pe.h +++ b/include/linux/pe.h @@ -166,7 +166,7 @@ struct mz_hdr { uint16_t oem_info; /* oem specific */ uint16_t reserved1[10]; /* reserved */ uint32_t peaddr; /* address of pe header */ - char message[64]; /* message to print */ + char message[]; /* message to print */ }; struct mz_reloc { From patchwork Thu Feb 22 11:17:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 129216 Delivered-To: patch@linaro.org Received: by 10.80.172.228 with SMTP id x91csp1882370edc; Thu, 22 Feb 2018 03:21:33 -0800 (PST) X-Google-Smtp-Source: AH8x224f3CI250FzQJxVppHNyscb+5tAgqLsKl//74l4ZbCKLhDNFNtoCpU7EW+3ANBzRf3HXcfA X-Received: by 10.101.78.143 with SMTP id b15mr5431517pgs.229.1519298410921; Thu, 22 Feb 2018 03:20:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519298410; cv=none; d=google.com; s=arc-20160816; b=sYdLs1qjmCyuKIikzUSHspZXZa4UBzPO2J42gZbbkKP73w8enm4EJlXyBUIw90qF4Y PC/fyVgclu841N3EYxOP7aG+U/OdJ95pM58DwDhZS4HrX029/uphU7j9CwARjgKf8nYN ie7x/GCERecw7DClh/s3CaFJUj7KIPqkMK4ZmOE5fI9+vazSsc0GQsyiY5BmrkSSgUtG WlFWJxtiWxn3hw9vWoEJmk8M93jXinoqClo+8/kd+JRU7KhzkleFu+2UtLw8XWa65UA9 g4DCaG3V+SLokED6aXWPC5PpTFiO+z7DbKRsATBLaomC8gFUQ+6wY9qjjGS66GeWfBlf lioA== 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=jsSE1wBGdj+4kK9YoZHE5wwBzlPAEkUMsMeDdB0bAvY=; b=XIDKyjsUIsb+1c8S6wEJeQgaT5EZ3FZIvDWB0shOD4v/gif5b+aS5Ty9V91RDuahCg z9sxZnfBTjVzrsh3K1EMIX18mzQ7TkgKPHZr84hB/UuktwoKIWQUJSbCG1U3lAUvtpaY x990DJCEnjEBanxhRZRlIN8NWdWsDZvga+EmqyT+Z0hJjpXfGInI+1ceV/emRZqArwdp qljAhLsQh/BHGEONGF9XpdkPC5EL8wrCLOEyP36EsUhfM+5+5FRDPqY7S2euMCV2YiJm IuNZwRASgUEeRtVUXo915hkBJgiKPLixU2NqtReERcLL4j7/KEfD1bvESQrQJdbiNuxO 2Bpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=A45/ZI6k; 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 k8si64866pgc.472.2018.02.22.03.20.10; Thu, 22 Feb 2018 03:20:10 -0800 (PST) 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=A45/ZI6k; 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 S932248AbeBVLUH (ORCPT + 28 others); Thu, 22 Feb 2018 06:20:07 -0500 Received: from mail-io0-f194.google.com ([209.85.223.194]:39066 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932080AbeBVLUE (ORCPT ); Thu, 22 Feb 2018 06:20:04 -0500 Received: by mail-io0-f194.google.com with SMTP id b34so5634951ioj.6 for ; Thu, 22 Feb 2018 03:20:04 -0800 (PST) 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=jsSE1wBGdj+4kK9YoZHE5wwBzlPAEkUMsMeDdB0bAvY=; b=A45/ZI6ksv7IFSWodYMWlt2ROlGlh6xw7p+baWy63g6Y5acLXiIR7eXnYhh+lsa+LL 9y37uXm1kpmb/jJqYOxtK24tndIZtxnL8OT8YYaE8W48mNzfRZT1B1kymcXclJ6d1Y+4 itvbBmEm6wY6I/b1M5oOmvb4ISdNnYzKPYAlU= 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=jsSE1wBGdj+4kK9YoZHE5wwBzlPAEkUMsMeDdB0bAvY=; b=GCGHeF3dAVIGVNY+yb+k/fbo8ZbHhNHE23gNInhqEhpd8LgFENlytx77FT3z+8Kr43 C8lgj4Y4wR51gNjAkX4+qYB4bqADpITOjwSOiJGf1apFcwTJUveynk8i89ITijBs621O tbrPA9jQ8kZ1M2h7Ncb3tt4bpF7XekdMA+CTkFZG7Q9csFqISFgxngpNyU5mQNqsrX2F 4F5sWOYVizRumTdULGy9Yu428jLauoOyq9C8AeAR5dd/WLLyp1YfI83ai3nuYshuZuRr TOkfjd/DxbX3fihTvvg7kxr3XSwwa1ldIXsXeqAQBF4y4heUCZlqE7ZVGpnTHkBCS8KY hTxA== X-Gm-Message-State: APf1xPA4qMgEE1hU2JJlJ+mEl5X0SzUEl/FxSuQtPdXZUoBhn+wQqe68 0sMYxg9pgfq+LZ4/TnOAFSKbOQ== X-Received: by 10.107.138.35 with SMTP id m35mr8402008iod.81.1519298403844; Thu, 22 Feb 2018 03:20:03 -0800 (PST) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id v140sm188171itb.42.2018.02.22.03.20.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 03:20:03 -0800 (PST) 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, julien.thierry@arm.com Cc: kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, AKASHI Takahiro Subject: [PATCH v8 13/13] arm64: kexec_file: enable KEXEC_VERIFY_SIG for Image Date: Thu, 22 Feb 2018 20:17:32 +0900 Message-Id: <20180222111732.23051-14-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180222111732.23051-1-takahiro.akashi@linaro.org> References: <20180222111732.23051-1-takahiro.akashi@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With this patch, kernel verification can be done without IMA security subsystem enabled. Turn on CONFIG_KEXEC_VERIFY_SIG instead. On x86, a signature is embedded into a PE file (Microsoft's format) header of binary. Since arm64's "Image" can also be seen as a PE file as far as CONFIG_EFI is enabled, we adopt this format for kernel signing. You can create a signed kernel image with: $ sbsign --key ${KEY} --cert ${CERT} Image Signed-off-by: AKASHI Takahiro Cc: Catalin Marinas Cc: Will Deacon --- arch/arm64/Kconfig | 24 ++++++++++++++++++++++++ arch/arm64/include/asm/kexec.h | 16 ++++++++++++++++ arch/arm64/kernel/kexec_image.c | 15 +++++++++++++++ 3 files changed, 55 insertions(+) -- 2.16.2 diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 79ee27b8d2a0..e400edc291d4 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -839,6 +839,30 @@ config KEXEC_FILE for kernel and initramfs as opposed to list of segments as accepted by previous system call. +config KEXEC_VERIFY_SIG + bool "Verify kernel signature during kexec_file_load() syscall" + depends on KEXEC_FILE + ---help--- + Select this option to verify a signature with loaded kernel + image. If configured, any attempt of loading a image without + valid signature will fail. + + In addition to that option, you need to enable signature + verification for the corresponding kernel image type being + loaded in order for this to work. + +config KEXEC_IMAGE_VERIFY_SIG + bool "Enable Image signature verification support" + default y + depends on KEXEC_VERIFY_SIG + depends on EFI && SIGNED_PE_FILE_VERIFICATION + ---help--- + Enable Image signature verification support. + +comment "Image signature verification is missing yet" + depends on KEXEC_VERIFY_SIG + depends on !EFI || !SIGNED_PE_FILE_VERIFICATION + config CRASH_DUMP bool "Build kdump crash kernel" help diff --git a/arch/arm64/include/asm/kexec.h b/arch/arm64/include/asm/kexec.h index 592890085aae..85f6913f868f 100644 --- a/arch/arm64/include/asm/kexec.h +++ b/arch/arm64/include/asm/kexec.h @@ -132,6 +132,7 @@ struct arm64_image_header { }; static const u8 arm64_image_magic[4] = {'A', 'R', 'M', 0x64U}; +static const u8 arm64_image_pe_sig[2] = {'M', 'Z'}; /** * arm64_header_check_magic - Helper to check the arm64 image header. @@ -153,6 +154,21 @@ static inline int arm64_header_check_magic(const struct arm64_image_header *h) && h->magic[3] == arm64_image_magic[3]); } +/** + * arm64_header_check_pe_sig - Helper to check the arm64 image header. + * + * Returns non-zero if 'MZ' signature is found. + */ + +static inline int arm64_header_check_pe_sig(const struct arm64_image_header *h) +{ + if (!h) + return 0; + + return (h->pe_sig[0] == arm64_image_pe_sig[0] + && h->pe_sig[1] == arm64_image_pe_sig[1]); +} + extern const struct kexec_file_ops kexec_image_ops; struct kimage; diff --git a/arch/arm64/kernel/kexec_image.c b/arch/arm64/kernel/kexec_image.c index de62def63dd6..816d5faf491d 100644 --- a/arch/arm64/kernel/kexec_image.c +++ b/arch/arm64/kernel/kexec_image.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -27,6 +28,9 @@ static int image_probe(const char *kernel_buf, unsigned long kernel_len) if ((kernel_len < sizeof(*h)) || !arm64_header_check_magic(h)) return -EINVAL; + pr_debug("PE format: %s\n", + (arm64_header_check_pe_sig(h) ? "yes" : "no")); + return 0; } @@ -84,7 +88,18 @@ static void *image_load(struct kimage *image, char *kernel, return ERR_PTR(ret); } +#ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG +static int image_verify_sig(const char *kernel, unsigned long kernel_len) +{ + return verify_pefile_signature(kernel, kernel_len, NULL, + VERIFYING_KEXEC_PE_SIGNATURE); +} +#endif + const struct kexec_file_ops kexec_image_ops = { .probe = image_probe, .load = image_load, +#ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG + .verify_sig = image_verify_sig, +#endif };