Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1pmaQa-0003nr-Q6 for mharc-grub-devel@gnu.org; Wed, 12 Apr 2023 09:22:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmWxx-00033S-Gh for grub-devel@gnu.org; Wed, 12 Apr 2023 05:40:37 -0400 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmWxs-0000Bm-VL for grub-devel@gnu.org; Wed, 12 Apr 2023 05:40:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1681292434; x=1712828434; h=from:to:cc:subject:date:message-id: content-transfer-encoding:mime-version; bh=3IKzi0qkKTho7tcQjpq0sufliDHbbaD3nJgvPyGZUVw=; b=ueAIz2MdGzu1nniZ1f9POkeq8wJIdS/Zs7bp+rBy4GfT76TjmMP2E7T7 x9eIkrUxqjp6ZFh7FpD5MCIIyAp8B+9wwAt8DYTw/Pn5TDCUpgkKdphO/ ATIzJ8f03cvTYpmCmNOouCDGDsFcwcASUixXyhysO7i5/2hOwgU1Z+nN2 U=; X-IronPort-AV: E=Sophos;i="5.98,338,1673913600"; d="scan'208";a="328661817" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-pdx-2c-m6i4x-8c5b1df3.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2023 09:40:25 +0000 Received: from EX19MTAUWA002.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-pdx-2c-m6i4x-8c5b1df3.us-west-2.amazon.com (Postfix) with ESMTPS id 045AB414D5 for ; Wed, 12 Apr 2023 09:40:23 +0000 (UTC) Received: from EX19D021UWA001.ant.amazon.com (10.13.139.24) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Wed, 12 Apr 2023 09:40:23 +0000 Received: from EX19D016UWA003.ant.amazon.com (10.13.139.104) by EX19D021UWA001.ant.amazon.com (10.13.139.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Wed, 12 Apr 2023 09:40:23 +0000 Received: from EX19D016UWA003.ant.amazon.com ([fe80::d0e4:ef0e:b10a:c566]) by EX19D016UWA003.ant.amazon.com ([fe80::d0e4:ef0e:b10a:c566%6]) with mapi id 15.02.1118.026; Wed, 12 Apr 2023 09:40:23 +0000 From: "Paterson, Harley" To: "grub-devel@gnu.org" CC: "Herrenschmidt, Benjamin" Subject: [PATCH] Feature: Expose x86 CPU FMS from cpuid module Thread-Topic: [PATCH] Feature: Expose x86 CPU FMS from cpuid module Thread-Index: AdltIV30Tve+3/y0Rty34cLBJtTRWw== Date: Wed, 12 Apr 2023 09:40:23 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.106.82.18] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Received-SPF: pass client-ip=207.171.184.29; envelope-from=prvs=459423129=harleyip@amazon.com; helo=smtp-fw-9102.amazon.com X-Spam_score_int: -102 X-Spam_score: -10.3 X-Spam_bar: ---------- X-Spam_report: (-10.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Wed, 12 Apr 2023 09:22:23 -0400 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 12 Apr 2023 09:40:37 -0000 This patch exposes an x86 CPU's model, family, and stepping via flag in the `cpuid` command. Invoking `cpuid -f` will set the Grub environment variables `CPUID_FAMILY`, `CPUID_MODEL`, and `CPUID_STEP` for consumption by Grub scripts. Accessing the CPU FMS data requires a `cpuid(0x1)` call on both x86 and x64 systems. Calling `cpuid(0x1)` first requires checking `cpuid` can be called on the processor, and then checking the maximum parameter accepted by the CPU's `cpuid` implementation is greater than or equal to one. The x86 implementation of this code already has these checks (and calls `cpuid(0x1)`) to discover the presence of long mode and PAE. These features are currently assumed to be present on x64. This patch runs the tests on both x86 and x86_64. This existing shortcut saves trivial time, but we would need the same code paths for the FMS anyway - better always run the PAE tests too, rather than duplicate the code minus these fast tests (bitshifts and xors). The CPU FMS format is common across both AMD and Intel devices. The long mode flags bit is undefined in x64. The long-mode check is disabled on x64 and assumed true, consistent with existing behaviour. See: * https://www.amd.com/system/files/TechDocs/25481.pdf * https://www.scss.tcd.ie/~jones/CS4021/processor-identification-cpuid-inst= ruction-note.pdf --- grub-core/commands/i386/cpuid.c | 73 ++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/grub-core/commands/i386/cpuid.c b/grub-core/commands/i386/cpui= d.c index 42b9841..90d857b 100644 --- a/grub-core/commands/i386/cpuid.c +++ b/grub-core/commands/i386/cpuid.c @@ -35,13 +35,15 @@ static const struct grub_arg_option options[] =3D no argument is specified. */ {"long-mode", 'l', 0, N_("Check if CPU supports 64-bit (long) mode (de= fault)."), 0, 0}, {"pae", 'p', 0, N_("Check if CPU supports Physical Address Extension."= ), 0, 0}, + {"fms", 'f', 0, "Set CPUID_FAMILY, CPUID_MODEL, and CPUID_STEP variabl= es.", 0, 0}, {0, 0, 0, 0, 0, 0} }; =20 enum { MODE_LM =3D 0, - MODE_PAE =3D 1 + MODE_PAE =3D 1, + MODE_FMS =3D 2 }; =20 enum @@ -52,8 +54,45 @@ enum { bit_LM =3D (1 << 29) }; +enum + { + /* 8 bit number (3 base 10 digits) plus terminator. */ + FMS_BUF_LEN =3D 4, + }; + +unsigned char grub_cpuid_has_longmode =3D 0; +unsigned char grub_cpuid_has_pae =3D 0; +int grub_cpuid_family =3D -1; +int grub_cpuid_model =3D -1; +int grub_cpuid_step =3D -1; =20 -unsigned char grub_cpuid_has_longmode =3D 0, grub_cpuid_has_pae =3D 0; + +static void +set_cpuid_env(void) +{ + char fms[FMS_BUF_LEN] =3D {0}; + if (grub_cpuid_family !=3D -1) + { + grub_snprintf(fms, FMS_BUF_LEN - 1, "%d", grub_cpuid_family); + grub_env_set("CPUID_FAMILY", fms); + } + else + grub_env_set("CPUID_FAMILY", "unknown"); + if (grub_cpuid_model !=3D -1) + { + grub_snprintf(fms, FMS_BUF_LEN - 1, "%d", grub_cpuid_model); + grub_env_set("CPUID_MODEL", fms); + } + else + grub_env_set("CPUID_MODEL", "unknown"); + if (grub_cpuid_step !=3D -1) + { + grub_snprintf(fms, FMS_BUF_LEN - 1, "%d", grub_cpuid_step); + grub_env_set("CPUID_STEP", fms); + } + else + grub_env_set("CPUID_STEP", "unknown"); +} =20 static grub_err_t grub_cmd_cpuid (grub_extcmd_context_t ctxt, @@ -61,7 +100,12 @@ grub_cmd_cpuid (grub_extcmd_context_t ctxt, char **args __attribute__ ((unused))) { int val =3D 0; - if (ctxt->state[MODE_PAE].set) + if (ctxt->state[MODE_FMS].set) + { + set_cpuid_env(); + return GRUB_ERR_NONE; + } + else if (ctxt->state[MODE_PAE].set) val =3D grub_cpuid_has_pae; else val =3D grub_cpuid_has_longmode; @@ -75,20 +119,24 @@ static grub_extcmd_t cmd; =20 GRUB_MOD_INIT(cpuid) { -#ifdef __x86_64__ - /* grub-emu */ - grub_cpuid_has_longmode =3D 1; - grub_cpuid_has_pae =3D 1; -#else - unsigned int eax, ebx, ecx, edx; unsigned int max_level; unsigned int ext_level; =20 /* See if we can use cpuid. */ +#ifdef __x86_64__ + unsigned long eax, ebx, ecx, edx; + grub_cpuid_has_longmode =3D 1; + asm volatile ("pushf; pushf; pop %0; mov %0,%1; xor %2,%0;" + "push %0; popf; pushf; pop %0; popf" + : "=3D&r" (eax), "=3D&r" (ebx) + : "i" (0x00200000)); +#else + unsigned int eax, ebx, ecx, edx; asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;" "pushl %0; popfl; pushfl; popl %0; popfl" : "=3D&r" (eax), "=3D&r" (ebx) : "i" (0x00200000)); +#endif if (((eax ^ ebx) & 0x00200000) =3D=3D 0) goto done; =20 @@ -103,8 +151,12 @@ GRUB_MOD_INIT(cpuid) { grub_cpuid (1, eax, ebx, ecx, edx); grub_cpuid_has_pae =3D !!(edx & bit_PAE); + grub_cpuid_step =3D eax & 0xf; + grub_cpuid_model =3D ((eax >> 4) & 0xf) + (((eax >> 16) & 0xf) << 4)= ; + grub_cpuid_family =3D ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff); } =20 +#ifndef __x86_64__ grub_cpuid (0x80000000, eax, ebx, ecx, edx); ext_level =3D eax; if (ext_level < 0x80000000) @@ -112,9 +164,8 @@ GRUB_MOD_INIT(cpuid) =20 grub_cpuid (0x80000001, eax, ebx, ecx, edx); grub_cpuid_has_longmode =3D !!(edx & bit_LM); -done: #endif - +done: cmd =3D grub_register_extcmd ("cpuid", grub_cmd_cpuid, 0, "[-l]", N_("Check for CPU features."), options); } --=20 2.39.2