From owner-svn-src-all@freebsd.org  Wed Oct 23 16:41:32 2019
Return-Path: <owner-svn-src-all@freebsd.org>
Delivered-To: svn-src-all@mailman.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.nyi.freebsd.org (Postfix) with ESMTP id 8FC811579A3;
 Wed, 23 Oct 2019 16:41:32 +0000 (UTC) (envelope-from jhb@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)
 server-signature RSA-PSS (4096 bits)
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 46yx3J3Fn0z4L6P;
 Wed, 23 Oct 2019 16:41:32 +0000 (UTC) (envelope-from jhb@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 53F16275FE;
 Wed, 23 Oct 2019 16:41:32 +0000 (UTC) (envelope-from jhb@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x9NGfWen031408;
 Wed, 23 Oct 2019 16:41:32 GMT (envelope-from jhb@FreeBSD.org)
Received: (from jhb@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id x9NGfWeU031407;
 Wed, 23 Oct 2019 16:41:32 GMT (envelope-from jhb@FreeBSD.org)
Message-Id: <201910231641.x9NGfWeU031407@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org
 using -f
From: John Baldwin <jhb@FreeBSD.org>
Date: Wed, 23 Oct 2019 16:41:32 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-all@freebsd.org,
 svn-src-head@freebsd.org
Subject: svn commit: r353931 - head/sys/riscv/include
X-SVN-Group: head
X-SVN-Commit-Author: jhb
X-SVN-Commit-Paths: head/sys/riscv/include
X-SVN-Commit-Revision: 353931
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-all@freebsd.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "SVN commit messages for the entire src tree \(except for &quot;
 user&quot; and &quot; projects&quot; \)" <svn-src-all.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-all/>
List-Post: <mailto:svn-src-all@freebsd.org>
List-Help: <mailto:svn-src-all-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Wed, 23 Oct 2019 16:41:32 -0000

Author: jhb
Date: Wed Oct 23 16:41:31 2019
New Revision: 353931
URL: https://svnweb.freebsd.org/changeset/base/353931

Log:
  Fix atomic_*cmpset32 on riscv64 with clang.
  
  The lr.w instruction used to read the value from memory sign-extends
  the value read from memory.  GCC sign-extends the 32-bit comparison
  value passed in whereas clang currently does not.  As a result, if the
  value being compared has the MSB set, the comparison fails for
  matching 32-bit values when compiled with clang.
  
  Use a cast to explicitly sign-extend the unsigned comparison value.
  This works with both GCC and clang.
  
  There is commentary in the RISC-V spec that suggests that GCC's
  approach is more correct, but it is not clear if the commentary in the
  RISC-V spec is binding.
  
  Reviewed by:	mhorne
  Obtained from:	Axiado
  MFC after:	2 weeks
  Sponsored by:	DARPA
  Differential Revision:	https://reviews.freebsd.org/D22084

Modified:
  head/sys/riscv/include/atomic.h

Modified: head/sys/riscv/include/atomic.h
==============================================================================
--- head/sys/riscv/include/atomic.h	Wed Oct 23 16:06:47 2019	(r353930)
+++ head/sys/riscv/include/atomic.h	Wed Oct 23 16:41:31 2019	(r353931)
@@ -182,7 +182,7 @@ atomic_cmpset_32(volatile uint32_t *p, uint32_t cmpval
 			"bnez %1, 0b\n"
 		"1:"
 			: "=&r" (tmp), "=&r" (res), "+A" (*p)
-			: "rJ" (cmpval), "rJ" (newval)
+			: "rJ" ((long)(int32_t)cmpval), "rJ" (newval)
 			: "memory");
 
 	return (!res);
@@ -207,7 +207,7 @@ atomic_fcmpset_32(volatile uint32_t *p, uint32_t *cmpv
 			"sw   %0, %3\n"		/* Save old value */
 		"2:"
 			: "=&r" (tmp), "=&r" (res), "+A" (*p), "+A" (*cmpval)
-			: "rJ" (*cmpval), "rJ" (newval)
+			: "rJ" ((long)(int32_t)*cmpval), "rJ" (newval)
 			: "memory");
 
 	return (!res);