Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4379777rwd; Tue, 30 May 2023 04:49:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7pSfW5ESL95GNGEYqfdzJ/ydfQzB9rchZkS2GBrUohyJHAu2rY2ViG6SixCOrIJO/0VWgW X-Received: by 2002:a17:90a:304e:b0:256:dd38:6567 with SMTP id q14-20020a17090a304e00b00256dd386567mr217040pjl.2.1685447354983; Tue, 30 May 2023 04:49:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685447354; cv=none; d=google.com; s=arc-20160816; b=SZ3ySfP32QDRFmoowNfrnpheBjBRjZu7RUbAmlzienzDrBpfeKa72YHgw6wTkrxoyA RcHP70CMJdcinleuNqjrZOjS/2lXRunqTpYqlnay5lcg5NcW+9sSWzfv+8iZ9xodx0es dU+hcp3init1v1uAVFqVFoACCST7loDR6X2szo0cXtTW1os7ioRjjjueY3X4AnjTh6Bv fduHL9GWUoLFQo642zoJ7hVrSvn34HCG0+NH4sCek+Cqit6exG4ujfb3huAHTBrtgKkD glY4Aek4ZLQRbHWYyt+OXADlzWS4JejANFnwhGMjcmtHAiuTZOSdJoiRynJfuRjj9mzu qZLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=bsYKDU7MPiEidgTaRXLDv8eau4e1tuEI9QhXTkMGoak=; b=LWgRVfd360RkFAjSxxQXFgmbZ12NARd0s5p8d/UlIAW9zKgwJ5KDxofzSiD2d5Uo6/ T4I9eGds4TdgQHINTDNb1igFZELvzrwhiu8HgCbbGsPqbaOQ0Ne7g9Yu4CEPtBPegpLG m6BqemKZeU8l8SyEKiIx9Uy0yEu8b34HpDOPSerdyAIJGZhZuKyRoJO2oUG4WOcaq7kN 8Vgp52GR7J1nz4+ATjjZ5vWU9sM/huFbXrtPQ1opGwXoqk/ybxCaC1HOJ2Ku1N535IUd a6q6JI97wrRbFfoxvpF3BXKeOQlwKHXcpeHrz42eXhhxioM6/fhFHUiDIbfB5aQCjhbV uGAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NqHZk1Ym; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i184-20020a6387c1000000b00513af1fa0b5si10520729pge.797.2023.05.30.04.49.02; Tue, 30 May 2023 04:49:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NqHZk1Ym; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232067AbjE3LoH (ORCPT + 99 others); Tue, 30 May 2023 07:44:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232059AbjE3Lnp (ORCPT ); Tue, 30 May 2023 07:43:45 -0400 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5419A114 for ; Tue, 30 May 2023 04:43:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685447016; x=1716983016; h=from:to:subject:date:message-id:in-reply-to:references: mime-version:content-transfer-encoding; bh=sshNR5ZFYTaYbsJoxoozahRmzMiWyXTZmDglNvBU3PU=; b=NqHZk1Ymz87jDfNL4TO92+C7cuNHFBkzauSvmvQgW4fopasI7Bx6WNux xnZWnLnKzzqRcICibGziL9qDs5qWbfI1OrQbicknTrbWdzYTdrPMMuJt9 qgCS2Fep/QFo0wzdjltsplpcusou2kUcpLV/9iy6XOgnlB+Kn4nO3gYbW jm3rpWgY04XeJL6lKeiYhUd0Brji/ytssfvhL+zLpJm0ffJyIIpApjw4D F7C/gdMVMn67yoeXtZA903EIe0ZSqTm1lCMDPNPMiQY2Cl6Ml47oR8v10 p97K2JKr/xvR2MCc41WP3Mht24G46iFXCZEgd2mlwmMw7DStVDvSqbSz0 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="383145372" X-IronPort-AV: E=Sophos;i="6.00,204,1681196400"; d="scan'208";a="383145372" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 May 2023 04:43:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="700588721" X-IronPort-AV: E=Sophos;i="6.00,204,1681196400"; d="scan'208";a="700588721" Received: from black.fi.intel.com (HELO black.fi.intel.com.) ([10.237.72.28]) by orsmga007.jf.intel.com with ESMTP; 30 May 2023 04:43:10 -0700 From: Alexander Shishkin To: linux-kernel@vger.kernel.org, x86@kernel.org, Andy Lutomirski , Dave Hansen , Ravi Shankar , Tony Luck , Sohil Mehta , Paul Lai Subject: [PATCH v2 06/12] x86/vsyscall: Reorganize the #PF emulation code Date: Tue, 30 May 2023 14:42:41 +0300 Message-Id: <20230530114247.21821-7-alexander.shishkin@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230530114247.21821-1-alexander.shishkin@linux.intel.com> References: <20230530114247.21821-1-alexander.shishkin@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sohil Mehta Separate out the actual vsyscall emulation from the page fault specific handling in preparation for the upcoming #GP fault emulation. Export is_vsyscall_vaddr() so that it can be reused later. No functional change intended. Signed-off-by: Sohil Mehta --- arch/x86/entry/vsyscall/vsyscall_64.c | 51 +++++++++++++++++---------- arch/x86/include/asm/vsyscall.h | 10 +++--- arch/x86/mm/fault.c | 13 ++----- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/arch/x86/entry/vsyscall/vsyscall_64.c b/arch/x86/entry/vsyscall/vsyscall_64.c index e0ca8120aea8..dd112e538992 100644 --- a/arch/x86/entry/vsyscall/vsyscall_64.c +++ b/arch/x86/entry/vsyscall/vsyscall_64.c @@ -82,6 +82,15 @@ static void warn_bad_vsyscall(const char *level, struct pt_regs *regs, regs->sp, regs->ax, regs->si, regs->di); } +/* + * The (legacy) vsyscall page is the long page in the kernel portion + * of the address space that has user-accessible permissions. + */ +bool is_vsyscall_vaddr(unsigned long vaddr) +{ + return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR); +} + static int addr_to_vsyscall_nr(unsigned long addr) { int nr; @@ -117,8 +126,7 @@ static bool write_ok_or_segv(unsigned long ptr, size_t size) } } -bool emulate_vsyscall(unsigned long error_code, - struct pt_regs *regs, unsigned long address) +static bool __emulate_vsyscall(struct pt_regs *regs, unsigned long address) { struct task_struct *tsk; unsigned long caller; @@ -127,22 +135,6 @@ bool emulate_vsyscall(unsigned long error_code, long ret; unsigned long orig_dx; - /* Write faults or kernel-privilege faults never get fixed up. */ - if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER) - return false; - - if (!(error_code & X86_PF_INSTR)) { - /* Failed vsyscall read */ - if (vsyscall_mode == EMULATE) - return false; - - /* - * User code tried and failed to read the vsyscall page. - */ - warn_bad_vsyscall(KERN_INFO, regs, "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround"); - return false; - } - /* * No point in checking CS -- the only way to get here is a user mode * trap to a high address, which means that we're in 64-bit user code. @@ -294,6 +286,29 @@ bool emulate_vsyscall(unsigned long error_code, return true; } +bool emulate_vsyscall_pf(unsigned long error_code, struct pt_regs *regs, + unsigned long address) +{ + /* Write faults or kernel-privilege faults never get fixed up. */ + if ((error_code & (X86_PF_WRITE | X86_PF_USER)) != X86_PF_USER) + return false; + + if (!(error_code & X86_PF_INSTR)) { + /* Failed vsyscall read */ + if (vsyscall_mode == EMULATE) + return false; + + /* + * User code tried and failed to read the vsyscall page. + */ + warn_bad_vsyscall(KERN_INFO, regs, + "vsyscall read attempt denied -- look up the vsyscall kernel parameter if you need a workaround"); + return false; + } + + return __emulate_vsyscall(regs, address); +} + /* * A pseudo VMA to allow ptrace access for the vsyscall page. This only * covers the 64bit vsyscall page now. 32bit has a real VMA now and does diff --git a/arch/x86/include/asm/vsyscall.h b/arch/x86/include/asm/vsyscall.h index ab60a71a8dcb..667b280afc1a 100644 --- a/arch/x86/include/asm/vsyscall.h +++ b/arch/x86/include/asm/vsyscall.h @@ -5,6 +5,8 @@ #include #include +extern bool is_vsyscall_vaddr(unsigned long vaddr); + #ifdef CONFIG_X86_VSYSCALL_EMULATION extern void map_vsyscall(void); extern void set_vsyscall_pgtable_user_bits(pgd_t *root); @@ -13,12 +15,12 @@ extern void set_vsyscall_pgtable_user_bits(pgd_t *root); * Called on instruction fetch fault in vsyscall page. * Returns true if handled. */ -extern bool emulate_vsyscall(unsigned long error_code, - struct pt_regs *regs, unsigned long address); +extern bool emulate_vsyscall_pf(unsigned long error_code, + struct pt_regs *regs, unsigned long address); #else static inline void map_vsyscall(void) {} -static inline bool emulate_vsyscall(unsigned long error_code, - struct pt_regs *regs, unsigned long address) +static inline bool emulate_vsyscall_pf(unsigned long error_code, + struct pt_regs *regs, unsigned long address) { return false; } diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index e4399983c50c..645eb3323f34 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -730,7 +730,7 @@ kernelmode_fixup_or_oops(struct pt_regs *regs, unsigned long error_code, * Per the above we're !in_interrupt(), aka. task context. * * In this case we need to make sure we're not recursively - * faulting through the emulate_vsyscall() logic. + * faulting through the emulate_vsyscall_pf() logic. */ if (current->thread.sig_on_uaccess_err && signal) { sanitize_error_code(address, &error_code); @@ -798,15 +798,6 @@ show_signal_msg(struct pt_regs *regs, unsigned long error_code, show_opcodes(regs, loglvl); } -/* - * The (legacy) vsyscall page is the long page in the kernel portion - * of the address space that has user-accessible permissions. - */ -static bool is_vsyscall_vaddr(unsigned long vaddr) -{ - return unlikely((vaddr & PAGE_MASK) == VSYSCALL_ADDR); -} - static void __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, unsigned long address, u32 pkey, int si_code) @@ -1329,7 +1320,7 @@ void do_user_addr_fault(struct pt_regs *regs, * to consider the PF_PK bit. */ if (is_vsyscall_vaddr(address)) { - if (emulate_vsyscall(error_code, regs, address)) + if (emulate_vsyscall_pf(error_code, regs, address)) return; } #endif -- 2.39.2