Received: by 10.223.185.116 with SMTP id b49csp2473830wrg; Mon, 5 Mar 2018 03:36:34 -0800 (PST) X-Google-Smtp-Source: AG47ELu1E3Gu1qp0zVPG7BKCKJy1WHqjTKqSPMvZuw8m30xAuTKFZ1Sb7QnaiZzyVlEFLuNm3NsJ X-Received: by 10.98.242.6 with SMTP id m6mr14946949pfh.230.1520249794615; Mon, 05 Mar 2018 03:36:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520249794; cv=none; d=google.com; s=arc-20160816; b=kAvCHFo4b0f4lFk7c8T8M/8hsEO4k5GxMYp41zJEXrVaRwJgighfH0SjuUGZXoO1Bx OKiAEsMxRindMs4DhZQlZUMSkjdxYNxZJbP0DYq7XUFAbAmqFgX8d0b2U6SNTv4r2qHK Ktqd4q9QFQyzFk81r28RQYqMvLCEqQWIfjRzYTv3zhXa1wVN2C91ULAk+PaTF1Bd/xs6 kTtJffHPtu2Zsb6N7Za192e2j0FgWouGN3XpaJy0NNtQgK64A+Qr/UbDNZhO0fatE7om lhbw8WMn6umHhdfkXvp8qdiUlrcDCSz4QuObWZhUPw/rL4pbGDwpjF80OO5HSjUQfOjM VRAg== 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=5GVwPDKsD+1K6vG3mdefs4/myzOy2uBlRsuw23QPAt0=; b=s5ChOzxnTX6SCvH/UbLilY//gf+sJMHtx6yjpwmzuFcC4M/UGO19KQ0Q5qbPpgSmec 19El7KNkHc2r8V0M8KN7ow/O0zxBABX3ndwumhFonsIQTR84ydUIkxdBouexqYaGqcMQ 4PDe3bfqHGyRzkIv+RE6JHdwjt4tEqI/jtM3bEBAX2jl3aaMXrmKAk7WUKaVFQ861eY2 4NXd5zv4EPq8xAMoZCwkCUdfVc4B2C2jhyipiJAL3eAgQsiK/DaOP/e4Fl9vvpwpgdKF 4kafYHeUIcdbnuERFgrab/NFWaHWDhS06PWwRyJZsoplz97fWAyTq7Mw1d9SPGpcqnbS BluQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=caH5IxUb; 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=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f11-v6si4690318plj.748.2018.03.05.03.36.20; Mon, 05 Mar 2018 03:36:34 -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=fail (test mode) header.i=@8bytes.org header.s=mail-1 header.b=caH5IxUb; 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=fail (p=NONE sp=NONE dis=NONE) header.from=8bytes.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934044AbeCEK22 (ORCPT + 99 others); Mon, 5 Mar 2018 05:28:28 -0500 Received: from 8bytes.org ([81.169.241.247]:54420 "EHLO theia.8bytes.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933819AbeCEK0W (ORCPT ); Mon, 5 Mar 2018 05:26:22 -0500 Received: by theia.8bytes.org (Postfix, from userid 1000) id 70B794246; Mon, 5 Mar 2018 11:26:10 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=8bytes.org; s=mail-1; t=1520245570; bh=Qx/1HWYNv8AQFzQ0MiHmuTzC8G5VFEXVR8wXhzVdEyA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=caH5IxUbfZMVVY8V5CDb0FcffjFwznY6ei6TBrsdVTppjfppbM8sx29Kc6KxUi7eb HIQ0FiZAQl8z4EQ8/lKxsmqJBKLtm0IV+8Zlwk4WUP1d2kbtyF4NnJJChquJRtudfg RxTe54OocMSulzDCAHfmpBl/puPnqv+NNSP+k8LYX4Cy1p/Cvp3O55vDqW5THdsP/Z 5Ls2pasqnm1UWx6nsb5VcbNlviIpeEacrEw25rdqqeUEw1zLF12eJAuZHImK6SEMcD hZfZL0D9p5zrIPp/8exPBo4BMU2LQxMmjeNEiq6XzXrptCvrFKZuWPRxmo0jPiRBCN ZU7d77NF3axGQ== From: Joerg Roedel To: Thomas Gleixner , Ingo Molnar , "H . Peter Anvin" Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Andy Lutomirski , Dave Hansen , Josh Poimboeuf , Juergen Gross , Peter Zijlstra , Borislav Petkov , Jiri Kosina , Boris Ostrovsky , Brian Gerst , David Laight , Denys Vlasenko , Eduardo Valentin , Greg KH , Will Deacon , aliguori@amazon.com, daniel.gruss@iaik.tugraz.at, hughd@google.com, keescook@google.com, Andrea Arcangeli , Waiman Long , Pavel Machek , jroedel@suse.de, joro@8bytes.org Subject: [PATCH 32/34] x86/ldt: Enable LDT user-mapping for PAE Date: Mon, 5 Mar 2018 11:26:01 +0100 Message-Id: <1520245563-8444-33-git-send-email-joro@8bytes.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1520245563-8444-1-git-send-email-joro@8bytes.org> References: <1520245563-8444-1-git-send-email-joro@8bytes.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joerg Roedel This adds the needed special case for PAE to get the LDT mapped into the user page-table when PTI is enabled. The big difference to the other paging modes is that we don't have a full top-level PGD entry available for the LDT, but only PMD entry. Signed-off-by: Joerg Roedel --- arch/x86/include/asm/mmu_context.h | 4 --- arch/x86/kernel/ldt.c | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index c931b88..af96cfb 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -70,11 +70,7 @@ struct ldt_struct { static inline void *ldt_slot_va(int slot) { -#ifdef CONFIG_X86_64 return (void *)(LDT_BASE_ADDR + LDT_SLOT_STRIDE * slot); -#else - BUG(); -#endif } /* diff --git a/arch/x86/kernel/ldt.c b/arch/x86/kernel/ldt.c index 8ab7df9..7787451 100644 --- a/arch/x86/kernel/ldt.c +++ b/arch/x86/kernel/ldt.c @@ -126,6 +126,57 @@ static void do_sanity_check(struct mm_struct *mm, } } +#ifdef CONFIG_X86_PAE + +static pmd_t *pgd_to_pmd_walk(pgd_t *pgd, unsigned long va) +{ + p4d_t *p4d; + pud_t *pud; + + if (pgd->pgd == 0) + return NULL; + + p4d = p4d_offset(pgd, va); + if (p4d_none(*p4d)) + return NULL; + + pud = pud_offset(p4d, va); + if (pud_none(*pud)) + return NULL; + + return pmd_offset(pud, va); +} + +static void map_ldt_struct_to_user(struct mm_struct *mm) +{ + pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR); + pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd); + pmd_t *k_pmd, *u_pmd; + + k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR); + u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR); + + if (static_cpu_has(X86_FEATURE_PTI) && !mm->context.ldt) + set_pmd(u_pmd, *k_pmd); +} + +static void sanity_check_ldt_mapping(struct mm_struct *mm) +{ + pgd_t *k_pgd = pgd_offset(mm, LDT_BASE_ADDR); + pgd_t *u_pgd = kernel_to_user_pgdp(k_pgd); + bool had_kernel, had_user; + pmd_t *k_pmd, *u_pmd; + + k_pmd = pgd_to_pmd_walk(k_pgd, LDT_BASE_ADDR); + u_pmd = pgd_to_pmd_walk(u_pgd, LDT_BASE_ADDR); + had_kernel = (k_pmd->pmd != 0); + had_user = (u_pmd->pmd != 0); + + do_sanity_check(mm, had_kernel, had_user); +} + +#else /* !CONFIG_X86_PAE */ + static void map_ldt_struct_to_user(struct mm_struct *mm) { pgd_t *pgd = pgd_offset(mm, LDT_BASE_ADDR); @@ -143,6 +194,8 @@ static void sanity_check_ldt_mapping(struct mm_struct *mm) do_sanity_check(mm, had_kernel, had_user); } +#endif /* CONFIG_X86_PAE */ + /* * If PTI is enabled, this maps the LDT into the kernelmode and * usermode tables for the given mm. -- 2.7.4