Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 526BAC2D0A3 for ; Tue, 3 Nov 2020 17:06:36 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id CAD9A20786 for ; Tue, 3 Nov 2020 17:06:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="MWgUvzf1" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CAD9A20786 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.18522.43542 (Exim 4.92) (envelope-from ) id 1kZzlD-0006cD-U8; Tue, 03 Nov 2020 17:06:19 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 18522.43542; Tue, 03 Nov 2020 17:06:19 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kZzlD-0006c6-Pl; Tue, 03 Nov 2020 17:06:19 +0000 Received: by outflank-mailman (input) for mailman id 18522; Tue, 03 Nov 2020 17:06:18 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kZzlC-0006c0-H3 for xen-devel@lists.xenproject.org; Tue, 03 Nov 2020 17:06:18 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id 1490ca39-3cc4-4b42-867e-607e4d040fe3; Tue, 03 Nov 2020 17:06:17 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 468C7AD29; Tue, 3 Nov 2020 17:06:17 +0000 (UTC) Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kZzlC-0006c0-H3 for xen-devel@lists.xenproject.org; Tue, 03 Nov 2020 17:06:18 +0000 X-Inumbo-ID: 1490ca39-3cc4-4b42-867e-607e4d040fe3 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTP id 1490ca39-3cc4-4b42-867e-607e4d040fe3; Tue, 03 Nov 2020 17:06:17 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1604423177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=5maChqSYeQFqWyEVEH7Ee6tx819ukNa7Fu6didAMlY4=; b=MWgUvzf1oeIyHMKevKKNLw6ABTuY/njEfhYejAYZOCsc9drrBBmWoKBTlsabvSBn39uBum ATUE1BttuJguF4KbtyLgP+1gH5n2NZuMq2uw1UTt6rTshFmLJojUeEXghQdqb2DvbsOjuu zQu+oW1JeL6MByT+hco1kQ6s2zyacn8= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 468C7AD29; Tue, 3 Nov 2020 17:06:17 +0000 (UTC) To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= From: Jan Beulich Subject: [PATCH] x86/PV: conditionally avoid raising #GP for early guest MSR accesses Message-ID: <7e69db81-cee7-3c7b-be64-4f5ff50fbe9c@suse.com> Date: Tue, 3 Nov 2020 18:06:17 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Prior to 4.15 Linux, when running in PV mode, did not install a #GP handler early enough to cover for example the rdmsrl_safe() of MSR_K8_TSEG_ADDR in bsp_init_amd() (not to speak of the unguarded read of MSR_K7_HWCR later in the same function). The respective change (42b3a4cb5609 "x86/xen: Support early interrupts in xen pv guests") was backported to 4.14, but no further - presumably since it wasn't really easy because of other dependencies. Therefore, to prevent our change in the handling of guest MSR accesses to render PV Linux 4.13 and older unusable on at least AMD systems, make the raising of #GP on these paths conditional upon the guest having installed a handler. Producing zero for reads and discarding writes isn't necessarily correct and may trip code trying to detect presence of MSRs early, but since such detection logic won't work without a #GP handler anyway, this ought to be a fair workaround. Signed-off-by: Jan Beulich --- a/xen/arch/x86/pv/emul-priv-op.c +++ b/xen/arch/x86/pv/emul-priv-op.c @@ -886,7 +886,7 @@ static int read_msr(unsigned int reg, ui if ( ret == X86EMUL_EXCEPTION ) x86_emul_hw_exception(TRAP_gp_fault, 0, ctxt); - return ret; + goto done; } switch ( reg ) @@ -999,7 +999,16 @@ static int read_msr(unsigned int reg, ui return X86EMUL_OKAY; } - return X86EMUL_UNHANDLEABLE; + done: + if ( ret != X86EMUL_OKAY && !curr->arch.pv.trap_ctxt[X86_EXC_GP].address ) + { + gprintk(XENLOG_WARNING, "faking RDMSR 0x%08x\n", reg); + *val = 0; + x86_emul_reset_event(ctxt); + ret = X86EMUL_OKAY; + } + + return ret; } static int write_msr(unsigned int reg, uint64_t val, @@ -1016,7 +1025,7 @@ static int write_msr(unsigned int reg, u if ( ret == X86EMUL_EXCEPTION ) x86_emul_hw_exception(TRAP_gp_fault, 0, ctxt); - return ret; + goto done; } switch ( reg ) @@ -1172,7 +1181,15 @@ static int write_msr(unsigned int reg, u return X86EMUL_OKAY; } - return X86EMUL_UNHANDLEABLE; + done: + if ( ret != X86EMUL_OKAY && !curr->arch.pv.trap_ctxt[X86_EXC_GP].address ) + { + gprintk(XENLOG_WARNING, "dropping WRMSR 0x%08x\n", reg); + x86_emul_reset_event(ctxt); + ret = X86EMUL_OKAY; + } + + return ret; } static int cache_op(enum x86emul_cache_op op, enum x86_segment seg,