From nobody Tue Apr 18 23:22:57 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q1Khy22Ndz45qrD; Tue, 18 Apr 2023 23:22:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q1Khx6bnKz4W2w; Tue, 18 Apr 2023 23:22:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1681860178; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=30eL+RwY8wc5We9DPqPhqvHfIUfQZ3/tvLdB+jJBahc=; b=lqOos5NjO+OOHoBRpZlgWg2SJNN1ujg4UHO38INpYzZEtJFmnOeqVbVvuIb/BZgOTlMrwT MJJ9Pf6reVQg+934xQlU6kuasffyVfls9FYrBLZ77p1ic/5y78YwwQlVdFoz/X9xzjDSDu b88UWdCMHS0miGG6DGi2S+cwBu+DfDrYSD9kU5RAAImP7TVwsz17uA53fwawox0EM6wCCr fbmIuF/fFHlUlEX5V1VzyhgZfJ9SHlFXaxp//TXbofupsJNvuyfP0emHINcev5KKA1Wo5r relefdCfNZpWfNYBrDbWGO2XH1yg2IBSOUcXyuT19YVwBHlbH2wnrSkZ+faISw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1681860177; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=30eL+RwY8wc5We9DPqPhqvHfIUfQZ3/tvLdB+jJBahc=; b=jplXnY7clZpN3I/Uaprygz0U/H8ojrDAFW5HJ/QBOMHhaiLmHyBIIEWUst7gxuAoL4v2uH WAnYYDbf4sHwmy4mORwuPEqHxm/IM92eSkNxheSzffrQbBJ23fihZdCTRtyTw0Jha+tdtp /sQjFRgir5sNtMUPAjooYW6+iIDEU4VAxd8XQdTKXvcUNJI926MagQRrgbC5b4as8Z6Yns +WnA5fr6VQB4/5ZOTuCt1pdj2TKuIBUTRukGu5Y7gPqRrIWcQtiFrHrVWsN4AzBkTvsSTN UNI7a6SKWOxxtAVF20mY7ypp6H3UlNCUaKBmsmGx9j6WjILQo41kdNCksQYHFA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1681860177; a=rsa-sha256; cv=none; b=b+cRm7wqnOZRe261g5hTi6smLm5nkt5Cm0L+XEJYXrfzLazaAZgXeBtb1fCRnMIePyV22u KFsE8m70oVLK3+5Fgv8Ry0IWBi0f9QXmKbni4VIe8HOULd+ac2PQVF39kWVJqj2d36QM8s 0HwfNDIF7+HssSkEe/kog59GlKSuqucVxtVIPjooLzjOC4HVAPvlOUmVD8ul81jlHeUr/m K/ALmfGdEBkXZp5ztcxSzzb2eT2dRlfoxQNcHlwnVWZYUvuDqGtY+uO56iAMePGVhER69P a9Cb5yhOWq+O85vU4jyBEnCDZEsPHjQyypGizhxCCtQoryzXzAdFle9PZrFhAQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q1Khx3RVxzXCs; Tue, 18 Apr 2023 23:22:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33INMvnS011716; Tue, 18 Apr 2023 23:22:57 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33INMvuQ011715; Tue, 18 Apr 2023 23:22:57 GMT (envelope-from git) Date: Tue, 18 Apr 2023 23:22:57 GMT Message-Id: <202304182322.33INMvuQ011715@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 617a11eab633 - main - x86: initialize use_xsave once List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 617a11eab6337693eae9d160453adf1943ab6a37 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=617a11eab6337693eae9d160453adf1943ab6a37 commit 617a11eab6337693eae9d160453adf1943ab6a37 Author: Konstantin Belousov AuthorDate: 2023-04-18 15:50:26 +0000 Commit: Konstantin Belousov CommitDate: 2023-04-18 23:22:28 +0000 x86: initialize use_xsave once The explanation from https://reviews.freebsd.org/D39637 by stevek: The "use_xsave" variable is a global and that is only supposed to be initialized early before scheduling gets started. However, with the way the ifuncs for "fpusave" and "fpurestore" are implemented, the value could be changed at runtime when scheduling is active if "use_xsave" was set to 0 by the tunable. This leaves a window of opportunity where "use_xsave" gets re-initialized to 1 and a context switch could occur with a thread that was not set up to be able to use xsave functionality. This can lead to an "privileged instruction fault". The fix is to protect "use_xsave" from being initialized more than once. Reported and reviewed by: stevek Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D39660 --- sys/amd64/amd64/fpu.c | 16 ---------------- sys/amd64/amd64/machdep.c | 5 +++++ sys/i386/i386/machdep.c | 6 ++++++ sys/i386/i386/npx.c | 14 -------------- 4 files changed, 11 insertions(+), 30 deletions(-) diff --git a/sys/amd64/amd64/fpu.c b/sys/amd64/amd64/fpu.c index 64974a7210a9..a3421cf0d496 100644 --- a/sys/amd64/amd64/fpu.c +++ b/sys/amd64/amd64/fpu.c @@ -238,22 +238,8 @@ fpurestore_fxrstor(void *addr) fxrstor((char *)addr); } -static void -init_xsave(void) -{ - - if (use_xsave) - return; - if ((cpu_feature2 & CPUID2_XSAVE) == 0) - return; - use_xsave = 1; - TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave); -} - DEFINE_IFUNC(, void, fpusave, (void *)) { - - init_xsave(); if (!use_xsave) return (fpusave_fxsave); if ((cpu_stdext_feature & CPUID_EXTSTATE_XSAVEOPT) != 0) { @@ -266,8 +252,6 @@ DEFINE_IFUNC(, void, fpusave, (void *)) DEFINE_IFUNC(, void, fpurestore, (void *)) { - - init_xsave(); if (!use_xsave) return (fpurestore_fxrstor); return ((cpu_stdext_feature & CPUID_STDEXT_NFPUSG) != 0 ? diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c index 480db1ed2c31..29a0bd6f9db4 100644 --- a/sys/amd64/amd64/machdep.c +++ b/sys/amd64/amd64/machdep.c @@ -1345,6 +1345,11 @@ hammer_time(u_int64_t modulep, u_int64_t physfree) &pmap_pcid_invlpg_workaround_uena); cpu_init_small_core(); + if ((cpu_feature2 & CPUID2_XSAVE) != 0) { + use_xsave = 1; + TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave); + } + link_elf_ireloc(kmdp); /* diff --git a/sys/i386/i386/machdep.c b/sys/i386/i386/machdep.c index d59e95ed1a99..1003dbe539c2 100644 --- a/sys/i386/i386/machdep.c +++ b/sys/i386/i386/machdep.c @@ -1545,6 +1545,11 @@ init386(int first) i386_kdb_init(); } + if (cpu_fxsr && (cpu_feature2 & CPUID2_XSAVE) != 0) { + use_xsave = 1; + TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave); + } + kmdp = preload_search_by_type("elf kernel"); link_elf_ireloc(kmdp); @@ -1565,6 +1570,7 @@ init386(int first) msgbufinit(msgbufp, msgbufsize); npxinit(true); + /* * Set up thread0 pcb after npxinit calculated pcb + fpu save * area size. Zero out the extended state header in fpu save diff --git a/sys/i386/i386/npx.c b/sys/i386/i386/npx.c index 3d4f2f2a60c8..91967b4be843 100644 --- a/sys/i386/i386/npx.c +++ b/sys/i386/i386/npx.c @@ -320,22 +320,8 @@ fpusave_fnsave(union savefpu *addr) fnsave((char *)addr); } -static void -init_xsave(void) -{ - - if (use_xsave) - return; - if (!cpu_fxsr || (cpu_feature2 & CPUID2_XSAVE) == 0) - return; - use_xsave = 1; - TUNABLE_INT_FETCH("hw.use_xsave", &use_xsave); -} - DEFINE_IFUNC(, void, fpusave, (union savefpu *)) { - - init_xsave(); if (use_xsave) return ((cpu_stdext_feature & CPUID_EXTSTATE_XSAVEOPT) != 0 ? fpusave_xsaveopt : fpusave_xsave);