Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1762060rwb; Wed, 5 Oct 2022 04:34:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4h//whkYm6rk6e3JzxiQu2ho9yZQL6NA0sjd4tnpLRYVuRMPMTDR8WKRxrjtuDskVtrEPv X-Received: by 2002:a05:6a00:2485:b0:561:c0a5:88aa with SMTP id c5-20020a056a00248500b00561c0a588aamr8451977pfv.51.1664969651077; Wed, 05 Oct 2022 04:34:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664969651; cv=none; d=google.com; s=arc-20160816; b=lv2pWb6p+oGe6ugd5Y22Ly6FjJO8k+wXzotQ1ocJtvhMSzXBR/emzn1//tbjnrkPCK WrlUsKE1jid04IOy6OsTTrfAWD/v0xQxDOSESd7elI+0ln4Mth+IbqgTXCjPqA3tOPOb u8s1kBAG33ndepKr22h80EfEa5TF4gTsNYw7GL3KxsTF0jwwyIRB32VHmqEkh1Q0DHTY bJ42QBLTQOL3pC9RbXJSGNTA9OY1rmiJf8shBfdRtoEq3phqlDvYGVND3h8vdk1Uw6kv cUcu9MG75SMrVC3c0fX7eb/ilw9mCbl0pJW+Ixqx7YMj7EZ1zTZrEPAtB2pF8FrpE9jE cSUg== 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:cc:to:from :dkim-signature; bh=BOKz5nEhUv9hbN0dMlTKN7tDv2LiEndbpY8j9esotMI=; b=ZrFNvAZy17SoCg9rmY3UL1OQ1fkBgeXg2JFz9CEsH2oe5qTauMTU7XTtzf+4HKZAV9 MRO+18Wg7uC8f7RprTQLZNv4tEVJtazSmqRQlN5ttlNRJfTNuoZ0J1IfKAea/nIT1quX zRKhfRkB2TzVbk/EImzYjFy2DjN0UMg26mmqDLHVQe2OxFlnycR5F4b99g0GqiZisbkl 5bz8N/h5NURC6gwFeaaYl/PDK3CpBsnMQsVcoAsyKMhpld2/v5yLln/8IZ2dSp956Eds 61+ewCBak/5yV5laXD/9dZ2ome0gDuQtkbXrGb04q0UwBKHKMBRCnQpW7ZY1dhiTuYZF vvzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=ZNE2YXNF; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l186-20020a6388c3000000b00458b40c8ee6si1539473pgd.861.2022.10.05.04.33.59; Wed, 05 Oct 2022 04:34:11 -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=@suse.com header.s=susede1 header.b=ZNE2YXNF; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229785AbiJELDk (ORCPT + 99 others); Wed, 5 Oct 2022 07:03:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229683AbiJELD2 (ORCPT ); Wed, 5 Oct 2022 07:03:28 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C6556CF4F for ; Wed, 5 Oct 2022 04:03:24 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 3C810218FA; Wed, 5 Oct 2022 11:03:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1664967802; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BOKz5nEhUv9hbN0dMlTKN7tDv2LiEndbpY8j9esotMI=; b=ZNE2YXNFRD9sG12P8pUw+NoUe3PFpWe/7qNalsGr7Q4gOUklUhjzssZikAoVr+j13p2i53 JsuyaXYG/l/sjwE4ig7IqIiPEG1TNNqH+VfmTymakAX7KdjRQ841KCgyIN+8apcB6+cqGy EBPpVFAOPr3doh+S81iaW88/mpjsPW4= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id E225B13345; Wed, 5 Oct 2022 11:03:21 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id bAv1NXlkPWP9QgAAMHmgww (envelope-from ); Wed, 05 Oct 2022 11:03:21 +0000 From: Juergen Gross To: linux-kernel@vger.kernel.org, x86@kernel.org Cc: Juergen Gross , Boris Ostrovsky , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , xen-devel@lists.xenproject.org, Jan Beulich Subject: [PATCH v3 3/4] xen/pv: refactor msr access functions to support safe and unsafe accesses Date: Wed, 5 Oct 2022 13:03:01 +0200 Message-Id: <20221005110302.13455-4-jgross@suse.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20221005110302.13455-1-jgross@suse.com> References: <20221005110302.13455-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS 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 Refactor and rename xen_read_msr_safe() and xen_write_msr_safe() to support both cases of MSR accesses, safe ones and potentially GP-fault generating ones. This will prepare to no longer swallow GPs silently in xen_read_msr() and xen_write_msr(). Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V2: - init val in xen_do_read_msr() to 0 (Jan Beulich) --- arch/x86/xen/enlighten_pv.c | 75 +++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c index 9b1a58dda935..d5b0844a1b7c 100644 --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -917,14 +917,18 @@ static void xen_write_cr4(unsigned long cr4) native_write_cr4(cr4); } -static u64 xen_read_msr_safe(unsigned int msr, int *err) +static u64 xen_do_read_msr(unsigned int msr, int *err) { - u64 val; + u64 val = 0; /* Avoid uninitialized value for safe variant. */ if (pmu_msr_read(msr, &val, err)) return val; - val = native_read_msr_safe(msr, err); + if (err) + val = native_read_msr_safe(msr, err); + else + val = native_read_msr(msr); + switch (msr) { case MSR_IA32_APICBASE: val &= ~X2APIC_ENABLE; @@ -933,23 +937,39 @@ static u64 xen_read_msr_safe(unsigned int msr, int *err) return val; } -static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high) +static void set_seg(unsigned int which, unsigned int low, unsigned int high, + int *err) { - int ret; - unsigned int which; - u64 base; + u64 base = ((u64)high << 32) | low; + + if (HYPERVISOR_set_segment_base(which, base) == 0) + return; - ret = 0; + if (err) + *err = -EIO; + else + WARN(1, "Xen set_segment_base(%u, %llx) failed\n", which, base); +} +/* + * Support write_msr_safe() and write_msr() semantics. + * With err == NULL write_msr() semantics are selected. + * Supplying an err pointer requires err to be pre-initialized with 0. + */ +static void xen_do_write_msr(unsigned int msr, unsigned int low, + unsigned int high, int *err) +{ switch (msr) { - case MSR_FS_BASE: which = SEGBASE_FS; goto set; - case MSR_KERNEL_GS_BASE: which = SEGBASE_GS_USER; goto set; - case MSR_GS_BASE: which = SEGBASE_GS_KERNEL; goto set; - - set: - base = ((u64)high << 32) | low; - if (HYPERVISOR_set_segment_base(which, base) != 0) - ret = -EIO; + case MSR_FS_BASE: + set_seg(SEGBASE_FS, low, high, err); + break; + + case MSR_KERNEL_GS_BASE: + set_seg(SEGBASE_GS_USER, low, high, err); + break; + + case MSR_GS_BASE: + set_seg(SEGBASE_GS_KERNEL, low, high, err); break; case MSR_STAR: @@ -965,11 +985,28 @@ static int xen_write_msr_safe(unsigned int msr, unsigned low, unsigned high) break; default: - if (!pmu_msr_write(msr, low, high, &ret)) - ret = native_write_msr_safe(msr, low, high); + if (!pmu_msr_write(msr, low, high, err)) { + if (err) + *err = native_write_msr_safe(msr, low, high); + else + native_write_msr(msr, low, high); + } } +} + +static u64 xen_read_msr_safe(unsigned int msr, int *err) +{ + return xen_do_read_msr(msr, err); +} + +static int xen_write_msr_safe(unsigned int msr, unsigned int low, + unsigned int high) +{ + int err = 0; + + xen_do_write_msr(msr, low, high, &err); - return ret; + return err; } static u64 xen_read_msr(unsigned int msr) -- 2.35.3