NetBSD Problem Report #55425

From www@netbsd.org  Sat Jun 27 10:46:36 2020
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id A337C1A9228
	for <gnats-bugs@gnats.NetBSD.org>; Sat, 27 Jun 2020 10:46:35 +0000 (UTC)
Message-Id: <20200627104634.747D31A9241@mollari.NetBSD.org>
Date: Sat, 27 Jun 2020 10:46:34 +0000 (UTC)
From: rokuyama.rk@gmail.com
Reply-To: rokuyama.rk@gmail.com
To: gnats-bugs@NetBSD.org
Subject: openssl fails on FPU emulation for powerpc
X-Send-Pr-Version: www-1.0

>Number:         55425
>Category:       port-powerpc
>Synopsis:       openssl fails on FPU emulation for powerpc
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    port-powerpc-maintainer
>State:          analyzed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Jun 27 10:50:00 +0000 2020
>Closed-Date:    
>Last-Modified:  Wed Jul 15 08:15:02 +0000 2020
>Originator:     Rin Okuyama
>Release:        9.99.68
>Organization:
Department of Physics, Meiji University
>Environment:
NetBSD obs266 9.99.68 NetBSD 9.99.68 (OBS266) #264: Sat Jun 27 13:39:00 JST 2020  rin@latipes:/usr/src/sys/arch/evbppc/compile/OBS266 evbppc
>Description:
tests/crypto/libcrypto/t_ciphers:evp fails on ibm4xx where FPU is
emulated by kernel:

tc-start: 1592971605.449921, evp
...
tc-se:    # Starting "Chacha20" tests at line 2455
tc-se:    # ERROR: (memory) 'expected == got' failed @ /usr/src/crypto/external/bsd/openssl/dist/test/evp_test.c:92
tc-se:    # --- expected
tc-se:    # +++ got
tc-se:    # 0000:-1ae10b594f09e26a 7e902ecbd0600691
tc-se:    # 0000:+153c72743c64d370 ecaeefcd68f9f41d
tc-se:    #       ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^

It works on macppc box with real FPU, or even on ibm4xx with
environmental OPENSSL_ppccap=0, which forcibly disables assembly codes
optimized for FPU.

Note that this is not a bug in code for Chacha20, but that for poly1305,
only where FPU is utilized.

This means that our FPU emulation is incomplete. I wonder whether we can
implement FPU emulation, in practice, at a level where results of integer
arithmetic are perfectly same as real hardwares.
>How-To-Repeat:
On ibm4xx or booke machines,

cd /usr/tests/crypto/libcrypto && atf-run t_ciphers
>Fix:
As a workaround, turn off FPU optimization on architectures without FPU.
The presence of FPU is automatically detected by openssl, but this test
passes even on machines without FPU because of emulation. Therefore, I'd
like to propose machdep.fpu_present sysctl variable like other ports, and
if it is zero, disable FPU codes in openssl:

http://www.netbsd.org/~rin/powerpc_machdep_fpu_present_20200627.patch
http://www.netbsd.org/~rin/openssl_powerpc_fpu_20200627.patch

Also note that this is meaningful even if we can fix FPU emulation;
codes optimized for ALU should be faster than that with FPU emulation.
So, I will commit the workaround above if there's no objection.

>Release-Note:

>Audit-Trail:
From: "Rin Okuyama" <rin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/55425 CVS commit: src/sys/arch/powerpc
Date: Tue, 7 Jul 2020 01:39:23 +0000

 Module Name:	src
 Committed By:	rin
 Date:		Tue Jul  7 01:39:23 UTC 2020

 Modified Files:
 	src/sys/arch/powerpc/include: cpu.h
 	src/sys/arch/powerpc/powerpc: powerpc_machdep.c

 Log Message:
 Part of PR port-powerpc/55425
 openssl fails on FPU emulation for powerpc

 Provide machdep.fpu_present sysctl variable like other ports.

 Userland can get informed that FPU is absent and emulated in software
 (and calculation results may not be correct in bit-to-bit precision).

 This variable should be useful even if we could fix FPU emulation;
 it is much faster to skip FPU arithmetic in general, rather than
 relying upon emulation by kernel via illegal instruction handler.


 To generate a diff of this commit:
 cvs rdiff -u -r1.113 -r1.114 src/sys/arch/powerpc/include/cpu.h
 cvs rdiff -u -r1.78 -r1.79 src/sys/arch/powerpc/powerpc/powerpc_machdep.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Rin Okuyama" <rin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/55425 CVS commit: src/crypto/external/bsd/openssl/dist/crypto
Date: Tue, 7 Jul 2020 01:47:47 +0000

 Module Name:	src
 Committed By:	rin
 Date:		Tue Jul  7 01:47:47 UTC 2020

 Modified Files:
 	src/crypto/external/bsd/openssl/dist/crypto: ppccap.c

 Log Message:
 Part of PR port-powerpc/55425
 openssl fails on FPU emulation for powerpc

 When machdep.fpu_present sysctl variable can be retrieved, and
 its value is zero, avoid using FPU arithmetic.

 FPU is absent and emulated by kernel in that case, and calculation
 results are not correct in bit-to-bit precision.

 This behavior should be useful even if we could fix FPU emulation;
 it is much faster to skip FPU arithmetic in general, rather than
 relying upon emulation by kernel via illegal instruction handler.


 To generate a diff of this commit:
 cvs rdiff -u -r1.15 -r1.16 \
     src/crypto/external/bsd/openssl/dist/crypto/ppccap.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

State-Changed-From-To: open->analyzed
State-Changed-By: rin@NetBSD.org
State-Changed-When: Tue, 07 Jul 2020 01:51:34 +0000
State-Changed-Why:
Workaround committed.

It is best to fix FPU emulation in kernel. However, I doubt whether we
could implement FPU emulation, in practice, at a level where calculation
results are same as real hardware in bit-to-bit precision.


From: Taylor R Campbell <riastradh@NetBSD.org>
To: gnats-bugs@netbsd.org
Cc: port-powerpc-maintainer@netbsd.org, netbsd-bugs@netbsd.org,
	gnats-admin@netbsd.org, rin@NetBSD.org, rokuyama.rk@gmail.com
Subject: Re: port-powerpc/55425 (openssl fails on FPU emulation for powerpc)
Date: Tue, 7 Jul 2020 02:57:10 +0000

 > Date: Tue,  7 Jul 2020 01:51:35 +0000 (UTC)
 > From: rin@NetBSD.org
 > 
 > It is best to fix FPU emulation in kernel. However, I doubt whether we
 > could implement FPU emulation, in practice, at a level where calculation
 > results are same as real hardware in bit-to-bit precision.

 Why do you doubt this?  We already do this for many platforms with
 sys/lib/libkern/softfloat.c, don't we?  Is it broken on powerpc, or
 does powerpc do something different, or what?

From: Rin Okuyama <rokuyama.rk@gmail.com>
To: Taylor R Campbell <riastradh@NetBSD.org>,
 "gnats-bugs@NetBSD.org" <gnats-bugs@NetBSD.org>
Cc: 
Subject: Re: port-powerpc/55425 (openssl fails on FPU emulation for powerpc)
Date: Tue, 7 Jul 2020 12:04:15 +0900

 On 2020/07/07 11:57, Taylor R Campbell wrote:
 >> Date: Tue,  7 Jul 2020 01:51:35 +0000 (UTC)
 >> From: rin@NetBSD.org
 >>
 >> It is best to fix FPU emulation in kernel. However, I doubt whether we
 >> could implement FPU emulation, in practice, at a level where calculation
 >> results are same as real hardware in bit-to-bit precision.
 > 
 > Why do you doubt this?  We already do this for many platforms with
 > sys/lib/libkern/softfloat.c, don't we?  Is it broken on powerpc, or
 > does powerpc do something different, or what?

 For FPU emulation on powerpc, sys/lib/libkern/softfloat.c is not used.
 Instead, we use its own codes in sys/arch/powerpc/fpu.

 Well, it may be better to switch to sys/lib/libkern/softfloat.c, rather
 than fixing codes in sys/arch/powerpc/fpu. However, it is still a not
 trivial work anyway...

 Thanks,
 rin

From: "Rin Okuyama" <rin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/55425 CVS commit: src/sys/arch/powerpc/fpu
Date: Wed, 15 Jul 2020 08:10:42 +0000

 Module Name:	src
 Committed By:	rin
 Date:		Wed Jul 15 08:10:41 UTC 2020

 Modified Files:
 	src/sys/arch/powerpc/fpu: fpu_emu.c

 Log Message:
 PR port-powerpc/55425

 Fix emulation for mtfsf; source register is frB here.

 Now, userland processes successfully change rounding mode, by which
 FPU-optimized code in OpenSSL works just fine as far as I can see.


 To generate a diff of this commit:
 cvs rdiff -u -r1.27 -r1.28 src/sys/arch/powerpc/fpu/fpu_emu.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Rin Okuyama" <rin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/55425 CVS commit: src/crypto/external/bsd/openssl/dist/crypto
Date: Wed, 15 Jul 2020 08:14:41 +0000

 Module Name:	src
 Committed By:	rin
 Date:		Wed Jul 15 08:14:41 UTC 2020

 Modified Files:
 	src/crypto/external/bsd/openssl/dist/crypto: ppccap.c

 Log Message:
 PR port-powerpc/55425

 Update comment; FPU emulation seems to work just fine now. However,
 FPU-optimized code should still be avoided for better performance,
 if FPU is not present.


 To generate a diff of this commit:
 cvs rdiff -u -r1.16 -r1.17 \
     src/crypto/external/bsd/openssl/dist/crypto/ppccap.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.