From patchwork Thu Dec 6 22:50:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Capper X-Patchwork-Id: 10717153 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 041B8109C for ; Thu, 6 Dec 2018 22:51:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E2A832F14F for ; Thu, 6 Dec 2018 22:51:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BD2822F276; Thu, 6 Dec 2018 22:51:22 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D08532F00A for ; Thu, 6 Dec 2018 22:51:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D8A26B7CCD; Thu, 6 Dec 2018 17:51:10 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0385B6B7CCE; Thu, 6 Dec 2018 17:51:09 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF6A76B7CCF; Thu, 6 Dec 2018 17:51:09 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ot1-f72.google.com (mail-ot1-f72.google.com [209.85.210.72]) by kanga.kvack.org (Postfix) with ESMTP id AA4256B7CCD for ; Thu, 6 Dec 2018 17:51:09 -0500 (EST) Received: by mail-ot1-f72.google.com with SMTP id q23so936850otn.3 for ; Thu, 06 Dec 2018 14:51:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=AXQW4xLiBAUaksY06gwnByuIvewn8cFUSe8k6M/4Oao=; b=hhOzYpUWLNkFTL8aTA1ZGSlYF6b1xGnw3yw5QhNBeXWsFu9lI2TalcOVyO3087C69V mYNStQ2mAJpZbXoy9/b64TBZqogIgBQ7JvjIi81+BaRmAiOXbiXDnzlZZyMBc6RlU0qp VAqG5BXOMVwQ1iKfIV0bia8adDRUQy+9vqqwDkKM85HFTyCJSS01Z4vj+Gjs5nyEBOSk DNzkH3rmOLRjPYIvuStdD1p1C05UX+rB4JnmKTbpBh0zVdLJTSPA0g4jrLxx1JTVlRpa GDBKPFqNMDvCzLOSPtGgFEeVSXLXueS90ruXMhU29hhrjNlsmp4kZSvQw2ZDsCNLKTCL kBCA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of steve.capper@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=steve.capper@arm.com X-Gm-Message-State: AA+aEWZW+XJaUUwvoS+ltyYoiDl/+8JRdHTur6A4k6w9454T6x43dAVW 2kjdLFGeIgTemRi2h6s/H5nt0U4HjiFUM8zusy9tgPQp0pka9tqfrrh5NeKTiyfqoR0f3JcG96O /1kr0iIiMPWk+F2K05LU1ZNAK4NK16Y4SIek0ENyxC05f64AzeE0tgn2u0I36AmMmAA== X-Received: by 2002:a9d:7685:: with SMTP id j5mr19569737otl.150.1544136669463; Thu, 06 Dec 2018 14:51:09 -0800 (PST) X-Google-Smtp-Source: AFSGD/UioIEPY65FrWrhF58xhaXjPyhkTn5d+q3ibx59ySO7u8/8riM6A0I9mrhKaFUqOtQ6ZWK/ X-Received: by 2002:a9d:7685:: with SMTP id j5mr19569714otl.150.1544136668611; Thu, 06 Dec 2018 14:51:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544136668; cv=none; d=google.com; s=arc-20160816; b=itlvOzEVWGI4bCVuMiEvYUi6HubAFVFkW17uktKpKbIQ+3VI0+xzqTW3rlZT83vX0+ DdsTN4pvj3cgf1bYkGzTWVv2vuHc8DqGb7Fn7olAjjGA3YYDDvheFJOAfW7WJK/4AzRj OV9lbVKIKuMIdJAg/NWX2SExqH0GGyJiMZKnbusc8Y0Bvq8Bs7l5xxnR1wE6Vw66+zXp anjoCQc5GAWrAfQpxb0Xr764gXO8WUyeWh7IHbzxUuzTFpIrxiZ8HL2nT0isyyy3et3H 39leCoL2cdCUISrGpekrShp6OdxTIKWypVvPdpWtHj5InW8P3Ypb+9K1R3FNmGiX0sTG ubhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=AXQW4xLiBAUaksY06gwnByuIvewn8cFUSe8k6M/4Oao=; b=e7+fDpYE6Gpm1oKCes4GprgDEMGVQzYGI8WnX2Mkgb6RsNfJ9PJoxE6MFH3gnaRfkl PRrcnGogv/1392mO+vqHuewL3ps/Rol16F45ONfXdqTaSik8PK8+vglgb7dC8bLzyKBi 7EA/vlfABUihSCYfxo2Nf9Q4VqHVDZuPqVrvVWHF8pdYPYdWihzTdP1v7+tLLkYKNrKE 2HrIcNbWo1fg6rLvwUPqVvVP/j15fbBS1Q1r6VdAXy0LwOwbA7ZNHyGu1CaWgz9uZ67s NPwQschDYsovbKccoil2SF47pzAmJlOPEbxqLMpakenw/oSv8xQkNhmQPoWrG4uTVfd4 KQWg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of steve.capper@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=steve.capper@arm.com Received: from foss.arm.com (foss.arm.com. [217.140.101.70]) by mx.google.com with ESMTP id z25si608091oto.195.2018.12.06.14.51.08 for ; Thu, 06 Dec 2018 14:51:08 -0800 (PST) Received-SPF: pass (google.com: domain of steve.capper@arm.com designates 217.140.101.70 as permitted sender) client-ip=217.140.101.70; Authentication-Results: mx.google.com; spf=pass (google.com: domain of steve.capper@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=steve.capper@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0621A1682; Thu, 6 Dec 2018 14:51:08 -0800 (PST) Received: from capper-debian.arm.com (unknown [10.37.12.92]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 7593F3F5AF; Thu, 6 Dec 2018 14:51:06 -0800 (PST) From: Steve Capper To: linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org Cc: catalin.marinas@arm.com, will.deacon@arm.com, ard.biesheuvel@linaro.org, suzuki.poulose@arm.com, jcm@redhat.com, Steve Capper Subject: [PATCH V5 4/7] arm64: mm: Offset TTBR1 to allow 52-bit PTRS_PER_PGD Date: Thu, 6 Dec 2018 22:50:39 +0000 Message-Id: <20181206225042.11548-5-steve.capper@arm.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181206225042.11548-1-steve.capper@arm.com> References: <20181206225042.11548-1-steve.capper@arm.com> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Enabling 52-bit VAs on arm64 requires that the PGD table expands from 64 entries (for the 48-bit case) to 1024 entries. This quantity, PTRS_PER_PGD is used as follows to compute which PGD entry corresponds to a given virtual address, addr: pgd_index(addr) -> (addr >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1) Userspace addresses are prefixed by 0's, so for a 48-bit userspace address, uva, the following is true: (uva >> PGDIR_SHIFT) & (1024 - 1) == (uva >> PGDIR_SHIFT) & (64 - 1) In other words, a 48-bit userspace address will have the same pgd_index when using PTRS_PER_PGD = 64 and 1024. Kernel addresses are prefixed by 1's so, given a 48-bit kernel address, kva, we have the following inequality: (kva >> PGDIR_SHIFT) & (1024 - 1) != (kva >> PGDIR_SHIFT) & (64 - 1) In other words a 48-bit kernel virtual address will have a different pgd_index when using PTRS_PER_PGD = 64 and 1024. If, however, we note that: kva = 0xFFFF << 48 + lower (where lower[63:48] == 0b) and, PGDIR_SHIFT = 42 (as we are dealing with 64KB PAGE_SIZE) We can consider: (kva >> PGDIR_SHIFT) & (1024 - 1) - (kva >> PGDIR_SHIFT) & (64 - 1) = (0xFFFF << 6) & 0x3FF - (0xFFFF << 6) & 0x3F // "lower" cancels out = 0x3C0 In other words, one can switch PTRS_PER_PGD to the 52-bit value globally provided that they increment ttbr1_el1 by 0x3C0 * 8 = 0x1E00 bytes when running with 48-bit kernel VAs (TCR_EL1.T1SZ = 16). For kernel configuration where 52-bit userspace VAs are possible, this patch offsets ttbr1_el1 and sets PTRS_PER_PGD corresponding to the 52-bit value. Suggested-by: Catalin Marinas Signed-off-by: Steve Capper Reviewed-by: Catalin Marinas Reviewed-by: Suzuki K Poulose --- Changed in V5, removed ttbr1 save/restore logic for software PAN as hardware PAN is a mandatory ARMv8.1 feature anyway. The logic to enable 52-bit VAs has also been changed to depend on ARM64_PAN || !ARM64_SW_TTBR0_PAN (in a later patch) This patch is new in V4 of the series --- arch/arm64/include/asm/assembler.h | 23 +++++++++++++++++++++++ arch/arm64/include/asm/pgtable-hwdef.h | 9 +++++++++ arch/arm64/kernel/head.S | 1 + arch/arm64/kernel/hibernate-asm.S | 1 + arch/arm64/mm/proc.S | 4 ++++ 5 files changed, 38 insertions(+) diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h index 6142402c2eb4..e2fe378d2a63 100644 --- a/arch/arm64/include/asm/assembler.h +++ b/arch/arm64/include/asm/assembler.h @@ -515,6 +515,29 @@ USER(\label, ic ivau, \tmp2) // invalidate I line PoU mrs \rd, sp_el0 .endm +/* + * Offset ttbr1 to allow for 48-bit kernel VAs set with 52-bit PTRS_PER_PGD. + * orr is used as it can cover the immediate value (and is idempotent). + * In future this may be nop'ed out when dealing with 52-bit kernel VAs. + * ttbr: Value of ttbr to set, modified. + */ + .macro offset_ttbr1, ttbr +#ifdef CONFIG_ARM64_52BIT_VA + orr \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET +#endif + .endm + +/* + * Perform the reverse of offset_ttbr1. + * bic is used as it can cover the immediate value and, in future, won't need + * to be nop'ed out when dealing with 52-bit kernel VAs. + */ + .macro restore_ttbr1, ttbr +#ifdef CONFIG_ARM64_52BIT_VA + bic \ttbr, \ttbr, #TTBR1_BADDR_4852_OFFSET +#endif + .endm + /* * Arrange a physical address in a TTBR register, taking care of 52-bit * addresses. diff --git a/arch/arm64/include/asm/pgtable-hwdef.h b/arch/arm64/include/asm/pgtable-hwdef.h index 1d7d8da2ef9b..4a29c7e03ae4 100644 --- a/arch/arm64/include/asm/pgtable-hwdef.h +++ b/arch/arm64/include/asm/pgtable-hwdef.h @@ -80,7 +80,11 @@ #define PGDIR_SHIFT ARM64_HW_PGTABLE_LEVEL_SHIFT(4 - CONFIG_PGTABLE_LEVELS) #define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT) #define PGDIR_MASK (~(PGDIR_SIZE-1)) +#ifdef CONFIG_ARM64_52BIT_VA +#define PTRS_PER_PGD (1 << (52 - PGDIR_SHIFT)) +#else #define PTRS_PER_PGD (1 << (VA_BITS - PGDIR_SHIFT)) +#endif /* * Section address mask and size definitions. @@ -306,4 +310,9 @@ #define TTBR_BADDR_MASK_52 (((UL(1) << 46) - 1) << 2) #endif +#ifdef CONFIG_ARM64_52BIT_VA +#define TTBR1_BADDR_4852_OFFSET (((UL(1) << (52 - PGDIR_SHIFT)) - \ + (UL(1) << (48 - PGDIR_SHIFT))) * 8) +#endif + #endif diff --git a/arch/arm64/kernel/head.S b/arch/arm64/kernel/head.S index 4471f570a295..f60081be9a1b 100644 --- a/arch/arm64/kernel/head.S +++ b/arch/arm64/kernel/head.S @@ -769,6 +769,7 @@ ENTRY(__enable_mmu) phys_to_ttbr x1, x1 phys_to_ttbr x2, x2 msr ttbr0_el1, x2 // load TTBR0 + offset_ttbr1 x1 msr ttbr1_el1, x1 // load TTBR1 isb msr sctlr_el1, x0 diff --git a/arch/arm64/kernel/hibernate-asm.S b/arch/arm64/kernel/hibernate-asm.S index dd14ab8c9f72..fe36d85c60bd 100644 --- a/arch/arm64/kernel/hibernate-asm.S +++ b/arch/arm64/kernel/hibernate-asm.S @@ -40,6 +40,7 @@ tlbi vmalle1 dsb nsh phys_to_ttbr \tmp, \page_table + offset_ttbr1 \tmp msr ttbr1_el1, \tmp isb .endm diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S index 2c75b0b903ae..2db1c491d45d 100644 --- a/arch/arm64/mm/proc.S +++ b/arch/arm64/mm/proc.S @@ -182,6 +182,7 @@ ENDPROC(cpu_do_switch_mm) .macro __idmap_cpu_set_reserved_ttbr1, tmp1, tmp2 adrp \tmp1, empty_zero_page phys_to_ttbr \tmp2, \tmp1 + offset_ttbr1 \tmp2 msr ttbr1_el1, \tmp2 isb tlbi vmalle1 @@ -200,6 +201,7 @@ ENTRY(idmap_cpu_replace_ttbr1) __idmap_cpu_set_reserved_ttbr1 x1, x3 + offset_ttbr1 x0 msr ttbr1_el1, x0 isb @@ -254,6 +256,7 @@ ENTRY(idmap_kpti_install_ng_mappings) pte .req x16 mrs swapper_ttb, ttbr1_el1 + restore_ttbr1 swapper_ttb adr flag_ptr, __idmap_kpti_flag cbnz cpu, __idmap_kpti_secondary @@ -373,6 +376,7 @@ __idmap_kpti_secondary: cbnz w18, 1b /* All done, act like nothing happened */ + offset_ttbr1 swapper_ttb msr ttbr1_el1, swapper_ttb isb ret