Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1080916pxm; Wed, 23 Feb 2022 17:37:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJw1wGVtVZav6jBcqy3L6eQMeRBrzCieoZWpBVqbWAi6RKOIl7XILezPmLoApe5iX2jBZR2U X-Received: by 2002:a17:90a:990c:b0:1bc:3c9f:2abe with SMTP id b12-20020a17090a990c00b001bc3c9f2abemr383015pjp.220.1645666654863; Wed, 23 Feb 2022 17:37:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645666654; cv=none; d=google.com; s=arc-20160816; b=to98J393XQxYx1Oyc0SoxeWTMWv7737CqowrB08dPT/wV9Pb7w8SrE8ZPPlds2XJeV t8qznsyTvX553eYTfngxKO7ZR5SyAMPZyCEJVTwJQyI1M+AOKgnD2E8SFmtZeOymDWbQ HPKMVHBlktEINGMTTIFvEorpn4/3p0SP8s/jFxbECCAy2twKNqiHeMi32ZwlkzJFKQTT bDrgWH8ZX0m8uos+oOowkXef8oKe0ZpLNoFU6Q34/xv9nEhD/WdJMEqxZWNA/k3oWwZL f1YW+w6DrsFJIQOm/524bMz6avCTc98wCe0hK4q8jFjdZYLNdSG5AGbE9m+ajMVz/owd eRmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=3u8EEpMbig0KI546lbIIPD/d6CQvkSypU3nO2ozd8K0=; b=IvXj8DQDoPloKip9MgMvgEoVynuxH+eMlui255vJULCpZ80OSvr7qttHAbuBfvxcPK m0Iz4sasiDM7Dddiw53U6xgV347zEzJTg9NPgRtqZ87xuDyGj+mHSmL7URK2abGZAdL+ yTtP4e39MeFBDyrQTxCbZePVSJTzvAA1M0VuRU+KU6pswfVEv/jcYmtBymaWIsH+Gb1R mUr223tWkdQ/DY7fHv+pSYAd1325/0JqK5pQJj6n6jc7+lRfTUlpyGSCZ5+srJmtClRn aZbP7qv3i3yUH12wF0lNbjzTI7KkyoGqyS5vx66bXEVXfnrWmivh8thV9aVzrIswEsxF L8SA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=Vy0sBcWV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.de Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n9si1188107plh.384.2022.02.23.17.37.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Feb 2022 17:37:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmx.net header.s=badeba3b8450 header.b=Vy0sBcWV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmx.de Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0A7ED52E3B; Wed, 23 Feb 2022 17:15:45 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242936AbiBWVj4 (ORCPT + 99 others); Wed, 23 Feb 2022 16:39:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234362AbiBWVjz (ORCPT ); Wed, 23 Feb 2022 16:39:55 -0500 Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37DC645AC1; Wed, 23 Feb 2022 13:39:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1645652343; bh=+NQyixM3RzLbqPeag9mkx+AWwnpl0NSQKSfnDE07iys=; h=X-UI-Sender-Class:Subject:To:Cc:References:From:Date:In-Reply-To; b=Vy0sBcWVlu189IPaRYEUT3rePVUGoq3zjnuMH7OrAboW+KOJXTbKKyx1LLFOy/HYm k6X4R/TRnVvDwZ4h2G6PF5KUnLabTSWv+jHUrpdDkXqLnSsbrDFPIqFPtOkeU8h1L3 1/qa8NFwkVe3+vAqgf+GnncuXcl79cGJ9R86yFyg= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [141.30.226.129] ([141.30.226.129]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N95e9-1oGjHk0zzz-0169k8; Wed, 23 Feb 2022 22:39:03 +0100 Subject: Re: [PATCH v4] hwmon: (dell-smm) Improve assembly code To: David Laight , =?UTF-8?B?J1BhbGkgUm9ow6FyJw==?= , Guenter Roeck Cc: "jdelvare@suse.com" , "linux-hwmon@vger.kernel.org" , "linux-assembly@vger.kernel.org" , "linux-kernel@vger.kernel.org" References: <20220220190851.17965-1-W_Armin@gmx.de> <20220222165432.GA255373@roeck-us.net> <20220222175150.qs32v4outislnqj6@pali> From: Armin Wolf Message-ID: <4b5ca065-06a4-c1ff-ed16-3bbb038ee6e0@gmx.de> Date: Wed, 23 Feb 2022 22:39:00 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Provags-ID: V03:K1:7rIcpZjA2Su4QaoPr1aU1N7wlDbARz9HY5SPcA9huuY3vdxEft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1cjYHTFSe06Sp4i/WwM Ls9SmXMEleT8Btx3tcd6ZyQ7kIF1C/WhVZakHD62ThU8vgWIG4fYDK6qyH0Mu5wSQW0bKbUty 7u24Lqsvf0tL2Rvt1Nm1EFFhySFkCxjYcGqupK6s2f7bqQAzBsibWdhp6VjWDU75JzHZI6Hc3 fjJCEpLqgJCNJSgOnBbPzzgZStPDQt7d+5demtfcs/yj0EK8B8rqcGAHSpCmzH4fNvUct4gDL KhavmfxI0Ko6eM= X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Am 22.02.22 um 20:46 schrieb David Laight: > From: Pali Roh=C3=A1r >> Sent: 22 February 2022 17:52 >> >> On Tuesday 22 February 2022 08:54:32 Guenter Roeck wrote: >>> On Sun, Feb 20, 2022 at 08:08:51PM +0100, Armin Wolf wrote: >>>> The new assembly code works on both 32 bit and 64 bit >>>> cpus and allows for more compiler optimisations. >>>> Since clang runs out of registers on 32 bit x86 when >>>> using CC_OUT, we need to execute "setc" ourself. >>>> Also modify the debug message so we can still see >>>> the result (eax) when the carry flag was set. >>>> >>>> Tested with 32 bit and 64 bit kernels on a Dell Inspiron 3505. >>>> >>>> Signed-off-by: Armin Wolf >>> It would be great if I can get some Tested-by/Acked-by/Reviewed-by >>> tags for this patch. >> Well, I know about this driver asm code for a long time and it since >> beginning it was suspicious for me, why there is such huge code with >> stack and registers manipulation and why it cannot be implemented easil= y >> via just two "out" instructions. This patch is exactly doing it. >> But question reminds why it was written in this simple way since >> beginning. >> >> If this change is correct then I have no problem with it. >> >> But I would rather see review of this asm change by skilled x86 asm >> developer. We are dealing here with CPU 0, SMM x86 mode, I/O ports and >> stack manipulation in inline gcc asm which sounds like a trap. And I'm >> not feeling skilled for reviewing this change. >> >> May I ask somebody to review this change? Is there some linux x86 ML? > On the face of it the new asm code is basically the same as the old > asm code - and both are probably equivalent to the reverse engineered > windows code. > > The real problem isn't the new code, it is more 'WTF is actually going o= n'. > Somehow the pair of outb are generating a synchronous (or synchronous > enough) trap that the trap handler (in the smm code in the bios) > can change the registers before the code following the outb saves > them back to memory. > > SMM mode is a semi-hidden cpu mode usually entered by an interrupt. > It is horrid and nasty because it has access to the cpu memory and > registers, but is hidden and unknown (I think in the bios). > > Normal bios calls can (usually) be executed using a cpu emulator > (especially useful for doing real-mode call from 64bit mode) so > the kernel can limit what they can do. > > But SMM mode is nearly as bad as the ME processor on newer systems. > If you worry about security you really want it disabled - but you can't. > > As far as this patch goes I think I'd add a 'stc' (set carry) > instruction before the first 'outb'. > Then if the 'something magic happens here' doesn't happen (because > you aren't on the right kind of motherboard) the action fails. > > David We already check for such scenarios by checking if eax changed. I would not set the carry flag before doing a SMM call since im not sure if the firmware code always clears the carry flag when the call was successful. If for example the firmware code only sets the carry flag on error and otherwise ignores the carry flag, we might create some false negatives when a successful SMM call leaves the carry flag set. >>> Thanks, >>> Guenter >>> >>>> --- >>>> Changes in v4: >>>> - reword commit message >>>> >>>> Changes in v3: >>>> - make carry an unsigned char >>>> - use "+a", ... for output registers >>>> - drop "cc" from clobbered list >>>> >>>> Changes in v2: >>>> - fix clang running out of registers on 32 bit x86 >>>> - modify debug message >>>> --- >>>> drivers/hwmon/dell-smm-hwmon.c | 78 ++++++++-----------------------= --- >>>> 1 file changed, 18 insertions(+), 60 deletions(-) >>>> >>>> -- >>>> 2.30.2 >>>> >>>> diff --git a/drivers/hwmon/dell-smm-hwmon.c b/drivers/hwmon/dell-smm-= hwmon.c >>>> index c5939e68586d..38d23a8e83f2 100644 >>>> --- a/drivers/hwmon/dell-smm-hwmon.c >>>> +++ b/drivers/hwmon/dell-smm-hwmon.c >>>> @@ -119,7 +119,7 @@ struct smm_regs { >>>> unsigned int edx; >>>> unsigned int esi; >>>> unsigned int edi; >>>> -} __packed; >>>> +}; >>>> >>>> static const char * const temp_labels[] =3D { >>>> "CPU", >>>> @@ -164,74 +164,32 @@ static int i8k_smm_func(void *par) >>>> struct smm_regs *regs =3D par; >>>> int eax =3D regs->eax; >>>> int ebx =3D regs->ebx; >>>> + unsigned char carry; >>>> long long duration; >>>> - int rc; >>>> >>>> /* SMM requires CPU 0 */ >>>> if (smp_processor_id() !=3D 0) >>>> return -EBUSY; >>>> >>>> -#if defined(CONFIG_X86_64) >>>> - asm volatile("pushq %%rax\n\t" >>>> - "movl 0(%%rax),%%edx\n\t" >>>> - "pushq %%rdx\n\t" >>>> - "movl 4(%%rax),%%ebx\n\t" >>>> - "movl 8(%%rax),%%ecx\n\t" >>>> - "movl 12(%%rax),%%edx\n\t" >>>> - "movl 16(%%rax),%%esi\n\t" >>>> - "movl 20(%%rax),%%edi\n\t" >>>> - "popq %%rax\n\t" >>>> - "out %%al,$0xb2\n\t" >>>> - "out %%al,$0x84\n\t" >>>> - "xchgq %%rax,(%%rsp)\n\t" >>>> - "movl %%ebx,4(%%rax)\n\t" >>>> - "movl %%ecx,8(%%rax)\n\t" >>>> - "movl %%edx,12(%%rax)\n\t" >>>> - "movl %%esi,16(%%rax)\n\t" >>>> - "movl %%edi,20(%%rax)\n\t" >>>> - "popq %%rdx\n\t" >>>> - "movl %%edx,0(%%rax)\n\t" >>>> - "pushfq\n\t" >>>> - "popq %%rax\n\t" >>>> - "andl $1,%%eax\n" >>>> - : "=3Da"(rc) >>>> - : "a"(regs) >>>> - : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); >>>> -#else >>>> - asm volatile("pushl %%eax\n\t" >>>> - "movl 0(%%eax),%%edx\n\t" >>>> - "push %%edx\n\t" >>>> - "movl 4(%%eax),%%ebx\n\t" >>>> - "movl 8(%%eax),%%ecx\n\t" >>>> - "movl 12(%%eax),%%edx\n\t" >>>> - "movl 16(%%eax),%%esi\n\t" >>>> - "movl 20(%%eax),%%edi\n\t" >>>> - "popl %%eax\n\t" >>>> - "out %%al,$0xb2\n\t" >>>> - "out %%al,$0x84\n\t" >>>> - "xchgl %%eax,(%%esp)\n\t" >>>> - "movl %%ebx,4(%%eax)\n\t" >>>> - "movl %%ecx,8(%%eax)\n\t" >>>> - "movl %%edx,12(%%eax)\n\t" >>>> - "movl %%esi,16(%%eax)\n\t" >>>> - "movl %%edi,20(%%eax)\n\t" >>>> - "popl %%edx\n\t" >>>> - "movl %%edx,0(%%eax)\n\t" >>>> - "lahf\n\t" >>>> - "shrl $8,%%eax\n\t" >>>> - "andl $1,%%eax\n" >>>> - : "=3Da"(rc) >>>> - : "a"(regs) >>>> - : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); >>>> -#endif >>>> - if (rc !=3D 0 || (regs->eax & 0xffff) =3D=3D 0xffff || regs->eax = =3D=3D eax) >>>> - rc =3D -EINVAL; >>>> + asm volatile("out %%al,$0xb2\n\t" >>>> + "out %%al,$0x84\n\t" >>>> + "setc %0\n" >>>> + : "=3Dmr" (carry), >>>> + "+a" (regs->eax), >>>> + "+b" (regs->ebx), >>>> + "+c" (regs->ecx), >>>> + "+d" (regs->edx), >>>> + "+S" (regs->esi), >>>> + "+D" (regs->edi)); >>>> >>>> duration =3D ktime_us_delta(ktime_get(), calltime); >>>> - pr_debug("smm(0x%.4x 0x%.4x) =3D 0x%.4x (took %7lld usecs)\n", eax= , ebx, >>>> - (rc ? 0xffff : regs->eax & 0xffff), duration); >>>> + pr_debug("smm(0x%.4x 0x%.4x) =3D 0x%.4x carry: %d (took %7lld usecs= )\n", >>>> + eax, ebx, regs->eax & 0xffff, carry, duration); >>>> >>>> - return rc; >>>> + if (carry || (regs->eax & 0xffff) =3D=3D 0xffff || regs->eax =3D=3D= eax) >>>> + return -EINVAL; >>>> + >>>> + return 0; >>>> } >>>> >>>> /* > - > Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK= 1 1PT, UK > Registration No: 1397386 (Wales) >