From patchwork Mon Dec 19 07:13:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pratyush Anand X-Patchwork-Id: 9479771 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 785B660772 for ; Mon, 19 Dec 2016 07:16:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6ABCB28429 for ; Mon, 19 Dec 2016 07:16:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5F5A428451; Mon, 19 Dec 2016 07:16:32 +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=-3.7 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C8A4928429 for ; Mon, 19 Dec 2016 07:16:30 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1cIsA1-0004W9-Mw; Mon, 19 Dec 2016 07:15:01 +0000 Received: from mail-qt0-f173.google.com ([209.85.216.173]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1cIs9Q-0004Jl-1L for linux-arm-kernel@lists.infradead.org; Mon, 19 Dec 2016 07:14:29 +0000 Received: by mail-qt0-f173.google.com with SMTP id c47so139222083qtc.2 for ; Sun, 18 Dec 2016 23:14:01 -0800 (PST) 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=Rl/ol/cruh5fs0DL9BXYeZUD/B6UBoFgO8XFsosFD6w=; b=oTmVHIVaSOsvPDdJ/TOPc4ClVYGhbGbFy2vag/Y1am+Z05vyCca7quiDkwd77X65We eX1mMG9kBniBDO4BT4z/zqJb2VDvjF3oSyzSrdiV6/kZF4AStC6a6NcvIfgTYC8myngm z8LBNx595Ju7p6wZdNnKbFNX1K+X8jqYjlzsRPXHpuuWT/CtfcB04RFMl7+APuh0DnsK BKmV34vUSgGiTXiWu4HI0ZQsBWZoER4G1ZLt/qgW5vRJXFWNFYOId4vq4sneDDI6jZDM d8vK6c2BZyuO82b4o3Fdm5FJ88vcfIHf8/Z7hv6uxQNgJY8/a6Ox8A/32N+OSeW7qawW lQyA== X-Gm-Message-State: AIkVDXL4wvexKqPvz60CBN94hBSMk9qfCrftXlf95M5io1sxiabur+dX5lrEFFpy3n40O5ye X-Received: by 10.237.50.163 with SMTP id z32mr15115906qtd.71.1482131640720; Sun, 18 Dec 2016 23:14:00 -0800 (PST) Received: from localhost ([122.180.237.189]) by smtp.gmail.com with ESMTPSA id r15sm9880522qte.9.2016.12.18.23.13.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Dec 2016 23:13:59 -0800 (PST) From: Pratyush Anand To: kexec@lists.infradead.org, geoff@infradead.org, james.morse@arm.com Subject: [PATCH V2 1/2] kexec: arm64: create identity page table to be used in purgatory Date: Mon, 19 Dec 2016 12:43:46 +0530 Message-Id: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20161218_231424_382966_3DA682FE X-CRM114-Status: GOOD ( 21.36 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, Pratyush Anand , 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 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 | 152 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/kexec/arch/arm64/kexec-arm64.c b/kexec/arch/arm64/kexec-arm64.c index 04fd3968bb52..c2c8ff1b6940 100644 --- a/kexec/arch/arm64/kexec-arm64.c +++ b/kexec/arch/arm64/kexec-arm64.c @@ -24,6 +24,45 @@ #include "kexec-syscall.h" #include "arch/options.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 PGDIR_SHIFT 39 +#define PUD_SHIFT 30 +#define PMD_SHIFT 21 +#define PTRS_PER_PGD 0x1FF +#define PTRS_PER_PUD 0x1FF +#define PTRS_PER_PMD 0x1FF +#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 4 +#define PMD_FLAGS_NORMAL (PMD_TYPE_SECT | PMD_SECT_AF) +#define MMU_FLAGS_NORMAL (PMD_ATTRINDX(MT_NORMAL) | PMD_FLAGS_NORMAL) +#define SECTION_SIZE (2 * 1024 * 1024) +#define PAGE_SIZE (4 * 1024) +/* Since we are using 3 level of page tables, therefore minimum number of + * table will be 3. Most likely we will never need more than 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. We do not expect + * any memory segment to cross 512G boundary, however if we will ever wish + * to support uart debugging in purgatory then that might cross the + * boundary and therefore additional 2 more table space. Thus we will need + * maximum of 6 table space. + */ +#define MAX_PGTBLE_SZ (6 * 4096) +static int next_tbl_cnt = 1; + /* Global varables the core kexec routines expect. */ unsigned char reuse_initrd; @@ -316,6 +355,117 @@ unsigned long arm64_locate_kernel_segment(struct kexec_info *info) return hole; } +static unsigned long *create_table_entry(unsigned long *pgtbl_buf, + unsigned long pgtbl_mem, unsigned long *tbl, + unsigned long virt, int shift, + unsigned long ptrs) +{ + unsigned long index, desc, offset; + + index = (virt >> shift) & ptrs; + /* check if we have allocated a table already for this index */ + if (tbl[index]) { + /* + * table index will have entry as per purgatory page table + * memory. Find out corresponding buffer address of table. + */ + desc = tbl[index] & ~3UL; + offset = desc - pgtbl_mem; + return &pgtbl_buf[offset >> 3]; + } + + /* + * Always write page table content as per page table memory allocated + * for purgaory area, but return corresponding buffer area alloced + * in kexec + */ + if (next_tbl_cnt > 5) + die("%s: No more memory for page table\n", __func__); + + tbl[index] = (pgtbl_mem + PAGE_SIZE * next_tbl_cnt) | PMD_TYPE_TABLE; + + return &pgtbl_buf[(next_tbl_cnt++ * PAGE_SIZE) >> 3]; +} + +static void craete_block_entry(unsigned long *tbl, unsigned long flags, + unsigned long phys, unsigned long virt) +{ + unsigned long index; + unsigned long desc; + + index = (virt >> PMD_SHIFT) & PTRS_PER_PMD; + desc = (phys >> PMD_SHIFT) << PMD_SHIFT; + desc |= flags; + tbl[index] = desc; +} + +static void create_identity_entry(unsigned long *pgtbl_buf, + unsigned long pgtbl_mem, unsigned long virt, + unsigned long flags) +{ + unsigned long *tbl = pgtbl_buf; + + tbl = create_table_entry(pgtbl_buf, pgtbl_mem, tbl, virt, + PGDIR_SHIFT, PTRS_PER_PGD); + tbl = create_table_entry(pgtbl_buf, pgtbl_mem, tbl, virt, + PUD_SHIFT, PTRS_PER_PUD); + craete_block_entry(tbl, flags, virt, 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. + * Additionaly, 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) +{ + unsigned long *pgtbl_buf; + int i; + unsigned long mstart, mend, pgtbl_mem; + 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 dcache flush by VAs is recomeneded, 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 additionaly. + */ + 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. */ @@ -423,6 +573,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; }