From patchwork Tue May 23 05:02:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pratyush Anand X-Patchwork-Id: 9741915 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8BB956034C for ; Tue, 23 May 2017 05:12:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8133528794 for ; Tue, 23 May 2017 05:12:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7549128797; Tue, 23 May 2017 05:12:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CB96C28794 for ; Tue, 23 May 2017 05:12:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=zwMhRgBm9GkqP83wfzfHfF5aq7ksi86AINPZTI9DPgQ=; b=C0jxs4mH0VTOryzOBJf+SmuT2J 40AUDpOGTerf1DRMAb6xndQ2II5xUxpJxcx6mqbrM3hcHO84MIumKTKYpAbZcxCPAoVEeBf9k1X4m SPs5I7UCng7pKtQDiO7QDtIrqNE/lPIK0JWdnZSz0HCoSeZj7dYgMFBtEftZUiRup5UbnsgHcH78C HWHvbIGgiEItH70zba6KiDA08Or0ns3rOSlRp7ATRUg/9geSnd13yu8xGPbrPup2rJXuDfkyiDIDf xI45s7rxRN1srhwbfLbbMaPYIN/GbLODbi9+QXg0+vKmEdG46utcOdC8SnAROu6PS56bqfj3VM8uD RiPow9Yg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1dD27P-0006k0-MU; Tue, 23 May 2017 05:12:27 +0000 Received: from mail-pf0-f180.google.com ([209.85.192.180]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1dD1yK-0000nD-3D for linux-arm-kernel@lists.infradead.org; Tue, 23 May 2017 05:03:06 +0000 Received: by mail-pf0-f180.google.com with SMTP id e193so102651464pfh.0 for ; Mon, 22 May 2017 22:02:43 -0700 (PDT) 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:in-reply-to:references; bh=3bnvnOD/x51hDHdkZ+DCRs41mw50xxPWYy8ry4lgdZI=; b=RfFvh89fEwZJQNz2FvW29kCr1mC8Lgwyd5WAI4cOxuxhqQ5bIlFHCOd+deyPOWUVQY 6JKY6bWUd3YdDIVdkCRjldhogjeB0i6iqDGDujCaGJ8w/C7bcvDnPtbUPubbH7bHSYm7 wIrvJaaDFwFyuTpWlQXGdCuZcvfd5pN9LSVK6HtTHS1/+DLEYNiFKnK2UCKIa7Ss1dr7 w4/StgFXjjf6d4GWxXsrsNXvq6OmfppKcjfGzBOEcePniqH23CEdOh+16VzZd77eTQB3 LQVujJT85MIsH6oEejNCPncA8bR9Xj4I2foXJDfqt6ncH+pil2StAdOBU9y9Q74bthnY NNRg== X-Gm-Message-State: AODbwcCkVbSwNO8kGOqGeEJCiySWi/km5WdPbiRS/lSw3V4uvnMRuRaC WjTsU7KPTjoQ0/+j X-Received: by 10.84.135.101 with SMTP id 92mr9691669pli.114.1495515763168; Mon, 22 May 2017 22:02:43 -0700 (PDT) Received: from localhost ([122.162.132.203]) by smtp.gmail.com with ESMTPSA id e124sm33471744pfc.64.2017.05.22.22.02.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 22 May 2017 22:02:42 -0700 (PDT) From: Pratyush Anand To: horms@verge.net.au, james.morse@arm.com Subject: [PATCH v3 1/2] kexec: arm64: create identity page table to be used in purgatory Date: Tue, 23 May 2017 10:32:08 +0530 Message-Id: <3355da35facd39ed02e66c0d7d50f86692d565a1.1495514291.git.panand@redhat.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: References: In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170522_220304_214143_2C62DA09 X-CRM114-Status: GOOD ( 27.31 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, Pratyush Anand , bhe@redhat.com, kexec@lists.infradead.org, dyoung@redhat.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Purgatory sha verification is very slow when D-cache is not enabled there. We need to enable MMU as well to enable D-Cache.Therefore,we need to have an identity mapped page table in purgatory. Since debugging is very difficult in purgatory therefore we prefer to do as much work as possible in kexec. This patch prepares page table for purgatory in advance. We support only 4K page table,because it will be available on most of the platform. This page table is passed to the purgatory as a new segment. VA bit is fixed as 48, page table level is 3 where 3rd level page table contains 2M block entries. Signed-off-by: Pratyush Anand --- kexec/arch/arm64/kexec-arm64.c | 119 +++++++++++++++++++++++++++++++++++++++++ kexec/arch/arm64/kexec-mmu.h | 58 ++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 kexec/arch/arm64/kexec-mmu.h diff --git a/kexec/arch/arm64/kexec-arm64.c b/kexec/arch/arm64/kexec-arm64.c index 62f37585b788..af5dab331e97 100644 --- a/kexec/arch/arm64/kexec-arm64.c +++ b/kexec/arch/arm64/kexec-arm64.c @@ -17,6 +17,7 @@ #include "kexec.h" #include "kexec-arm64.h" +#include "kexec-mmu.h" #include "crashdump.h" #include "crashdump-arm64.h" #include "dt-ops.h" @@ -33,6 +34,10 @@ #define PROP_ELFCOREHDR "linux,elfcorehdr" #define PROP_USABLE_MEM_RANGE "linux,usable-memory-range" +typedef unsigned long guest_addr_t; +typedef unsigned long host_addr_t; +static int next_tbl_cnt = 1; + /* Global varables the core kexec routines expect. */ unsigned char reuse_initrd; @@ -519,6 +524,118 @@ unsigned long arm64_locate_kernel_segment(struct kexec_info *info) return hole; } +static host_addr_t *create_table_entry(host_addr_t *pgtbl_buf, + guest_addr_t pgtbl_mem, host_addr_t *tbl, + guest_addr_t virt, int shift, + unsigned long ptrs) +{ + unsigned long index, desc, offset; + + index = (virt >> shift) & (ptrs - 1); + /* check if we have allocated a table already for this index */ + if (tbl[index]) { + /* + * table index will have entry as per purgatory (guest)page + * table memory. Find out corresponding buffer address of + * table (host). + */ + desc = tbl[index] & ~3UL; + offset = desc - pgtbl_mem; + return &pgtbl_buf[offset >> 3]; + } + + if (next_tbl_cnt >= MAX_PGTBLE_SZ / PAGE_SIZE) + die("%s:No more memory for page table\n", __func__); + /* + * Always write page table content as per page table memory + * allocated for purgatory(guest) area, but return corresponding + * buffer area allocated in kexec (host). + */ + + tbl[index] = (pgtbl_mem + PAGE_SIZE * next_tbl_cnt) | PMD_TYPE_TABLE; + + return &pgtbl_buf[(next_tbl_cnt++ * PAGE_SIZE) >> 3]; +} + +static void create_block_entry(host_addr_t *tbl, unsigned long flags, + guest_addr_t virt) +{ + unsigned long index; + unsigned long desc; + + index = pmd_index(virt); + desc = virt & PMD_MASK; + desc |= flags; + tbl[index] = desc; +} + +static void create_identity_entry(host_addr_t *pgtbl_buf, + guest_addr_t pgtbl_mem, guest_addr_t virt, + unsigned long flags) +{ + host_addr_t *tbl = pgtbl_buf; + + tbl = create_table_entry(pgtbl_buf, pgtbl_mem, tbl, virt, + EXTRA_SHIFT, EXTRA_PTRS); + tbl = create_table_entry(pgtbl_buf, pgtbl_mem, tbl, virt, + IDENTITY_SHIFT, PTRS_PER_PTE); + create_block_entry(tbl, flags, virt); +} + +/** + * arm64_create_pgtbl_segment - Create page table segments to be used by + * purgatory. Page table will have entries to access memory area of all + * those segments which becomes part of sha verification in purgatory. + * Additionally, we also create page table for purgatory segment as well. + */ + +static int arm64_create_pgtbl_segment(struct kexec_info *info, + unsigned long hole_min, unsigned long hole_max) +{ + host_addr_t *pgtbl_buf; + guest_addr_t pgtbl_mem, mstart, mend; + int i; + unsigned long purgatory_base, purgatory_len; + + pgtbl_buf = xmalloc(MAX_PGTBLE_SZ); + memset(pgtbl_buf, 0, MAX_PGTBLE_SZ); + pgtbl_mem = add_buffer_phys_virt(info, pgtbl_buf, MAX_PGTBLE_SZ, + MAX_PGTBLE_SZ, PAGE_SIZE, hole_min, hole_max, 1, 0); + for (i = 0; i < info->nr_segments; i++) { + if (info->segment[i].mem == (void *)info->rhdr.rel_addr) { + purgatory_base = (unsigned long)info->segment[i].mem; + purgatory_len = info->segment[i].memsz; + } + mstart = (unsigned long)info->segment[i].mem; + mend = mstart + info->segment[i].memsz; + mstart &= ~(SECTION_SIZE - 1); + while (mstart < mend) { + create_identity_entry(pgtbl_buf, pgtbl_mem, + mstart, MMU_FLAGS_NORMAL); + mstart += SECTION_SIZE; + } + } + + /* we will need pgtble_base in purgatory for enabling d-cache */ + elf_rel_set_symbol(&info->rhdr, "pgtble_base", &pgtbl_mem, + sizeof(pgtbl_mem)); + /* + * We need to disable d-cache before we exit from purgatory. + * Since, only d-cache flush by VAs is recommended, therefore we + * will also need memory location of all those area which will be + * accessed in purgatory with enabled d-cache. sha256_regions + * already have start and length for all the segments except + * purgatory. Therefore, we will need to pass start and length of + * purgatory additionally. + */ + elf_rel_set_symbol(&info->rhdr, "purgatory_base", &purgatory_base, + sizeof(purgatory_base)); + elf_rel_set_symbol(&info->rhdr, "purgatory_len", &purgatory_len, + sizeof(purgatory_len)); + + return 0; +} + /** * arm64_load_other_segments - Prepare the dtb, initrd and purgatory segments. */ @@ -630,6 +747,8 @@ int arm64_load_other_segments(struct kexec_info *info, elf_rel_set_symbol(&info->rhdr, "arm64_dtb_addr", &dtb_base, sizeof(dtb_base)); + arm64_create_pgtbl_segment(info, hole_min, hole_max); + return 0; } diff --git a/kexec/arch/arm64/kexec-mmu.h b/kexec/arch/arm64/kexec-mmu.h new file mode 100644 index 000000000000..55354b5e3002 --- /dev/null +++ b/kexec/arch/arm64/kexec-mmu.h @@ -0,0 +1,58 @@ +#if !defined(KEXEC_MMU_H) +#define KEXEC_MMU_H +/* + * kexec creates identity page table to be used in purgatory so that + * dcache verification becomes faster. + * + * These are the definitions to be used by page table creation routine. + * + * Only 4K page table, 3 level, 2M block mapping, 48bit VA is supported + */ +#define PAGE_SHIFT 12 +#define PGTABLE_LEVELS 3 +#define ARM64_HW_PGTABLE_LEVEL_SHIFT(n) ((PAGE_SHIFT - 3) * (4 - (n)) + 3) +#define PGDIR_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(4 - PGTABLE_LEVELS) +#define EXTRA_SHIFT (PGDIR_SHIFT + PAGE_SHIFT - 3) +#define EXTRA_PTRS (1 << (48 - EXTRA_SHIFT)) +#define PUD_SHIFT PGDIR_SHIFT +#define PMD_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(2) +#define PMD_SIZE (1UL << PMD_SHIFT) +#define PMD_MASK (~(PMD_SIZE-1)) +#define IDENTITY_SHIFT PUD_SHIFT +#define PTRS_PER_PTE (1 << (PAGE_SHIFT - 3)) +#define PTRS_PER_PMD PTRS_PER_PTE +#define pmd_index(addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1)) +#define PMD_TYPE_TABLE (3UL << 0) +#define PMD_TYPE_SECT (1UL << 0) +#define PMD_SECT_AF (1UL << 10) +#define PMD_ATTRINDX(t) ((unsigned long)(t) << 2) +#define MT_NORMAL 0 +#define PMD_FLAGS_NORMAL (PMD_TYPE_SECT | PMD_SECT_AF) +#define MMU_FLAGS_NORMAL (PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS_NORMAL) +#define SECTION_SHIFT PMD_SHIFT +#define SECTION_SIZE (1UL << SECTION_SHIFT) +#define PAGE_SIZE (1 << PAGE_SHIFT) +/* + * Since we are using 3 level of page tables, therefore minimum number of + * table will be 3. Each entry in level 3 page table can map 2MB memory + * area. Thus a level 3 page table indexed by bit 29:21 can map a total of + * 1G memory area. Therefore, if any segment crosses 1G boundary, then we + * will need one more level 3 table. Similarly, level 2 page table indexed + * by bit 38:30 can map a total of 512G memory area. If segment addresses + * are more than 512G apart then we will need two more table for each such + * block. + * Lets consider 2G as the maximum size of crashkernel. This 2G memory + * location might not be allocated at 1G aligned boundary, so in that case + * we need to have 5 table size resrved to map any location of the crash + * kernel region. + * + * If we will ever wish to support uart debugging in purgatory then that + * might cross the boundary and therefore additional 2 more table space. In + * that case, we will need a total of 7 table space. + * + * As of now keep it fixed at 5. Increase it if any platform either + * supports uart or more than 2G of crash kernel size. + */ +#define MAX_PGTBLE_SZ (5 * PAGE_SIZE) + +#endif