NetBSD Problem Report #51366

From www@NetBSD.org  Wed Jul 27 14:58:22 2016
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 "Postmaster NetBSD.org" (verified OK))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 27B3A7A26F
	for <gnats-bugs@gnats.NetBSD.org>; Wed, 27 Jul 2016 14:58:22 +0000 (UTC)
Message-Id: <20160727145820.BC1DA7A281@mollari.NetBSD.org>
Date: Wed, 27 Jul 2016 14:58:20 +0000 (UTC)
From: rokuyama@rk.phys.keio.ac.jp
Reply-To: rokuyama@rk.phys.keio.ac.jp
To: gnats-bugs@NetBSD.org
Subject: kernel for powerpc/ibm4xx miscompiled by gcc >= 4.8.5
X-Send-Pr-Version: www-1.0

>Number:         51366
>Category:       port-powerpc
>Synopsis:       kernel for powerpc/ibm4xx miscompiled by gcc >= 4.8.5
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    port-powerpc-maintainer
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Jul 27 15:00:00 +0000 2016
>Closed-Date:    Sun Dec 18 03:14:13 +0000 2016
>Last-Modified:  Sun Dec 18 03:14:13 +0000 2016
>Originator:     Rin Okuyama
>Release:        HEAD
>Organization:
Faculty of Science and Technology, Keio University
>Environment:
NetBSD obs266 7.99.34 NetBSD 7.99.34 (OPENBLOCKS266) #1: Wed Jul 27 22:09:26 JST 2016  rin@XXX:XXX evbppc
>Description:
Kernels for powerpc/ibm4xx (e.g., evbppc/OPENBLOCKS266) are miscompiled
by gcc both 5.4 and 4.8.5. As a result, copyout fails and system does not
boot:

====================
  NetBSD 7.99.34 (INSTALL_OPENBLOCKS266) #0: Thu Jul 21 20:05:55 JST 2016
          rin@XXX:XXX
  Model: OpenBlockS266 IBM PowerPC 405GPr Board
  total memory = 128 MB
  avail memory = 119 MB
  plb0 (root)
  ...snip...
  root file system type: ffs
  kern.module.path=/stand/powerpc-ibm4xx/7.99.34/modules
  init: copying out flags `-s' 3
  init: copying out path `/sbin/init' 11
  exec /sbin/init: error 13
  init: copying out flags `-s' 3
  init: copying out path `/sbin/oinit' 12
  exec /sbin/oinit: error 2
  ...snip...
====================

If kernel is compiled with gcc 4.8.4 distributed with NetBSD 7.0.1,
system boots up successfully, which suggests that this is a regression
introduced to gcc >= 4.8.5.
>How-To-Repeat:
Described above.
>Fix:
This is due to ".machine ppc" pseudo-op, which is passed to assembler by
gcc >= 4.8.5. It discards CPU type specified by "-mcpu=405" option. As a
result, inline aseembler codes are compiled into a wrong dialect of
machine language. The attached patch removes this pseudo-op. Then, kernel
is successfully compiled by gcc 5.4 and 4.8.5.

Note that ".machine" pseudo-op seems necessary for powerpc64, in which
the default CPU type is power4. Without it, gas cannot assemble codes
generated by gcc.

====================
--- src/external/gpl3/gcc/dist/gcc/config/rs6000/rs6000.c.orig	2016-07-26 20:35:20.391415369 +0900
+++ src/external/gpl3/gcc/dist/gcc/config/rs6000/rs6000.c	2016-07-26 21:07:17.973106045 +0900
@@ -5185,8 +5185,13 @@
     }

 #ifdef USING_ELFOS_H
-  if (rs6000_default_cpu == 0 || rs6000_default_cpu[0] == '\0'
-      || !global_options_set.x_rs6000_cpu_index)
+/*
+ * XXX .machine overrides CPU type specified by -mcpu or -Wa,-m options, which
+ * can result in corrupted output with inline assembler for ppc32.
+ */
+  if ((rs6000_default_cpu == 0 || rs6000_default_cpu[0] == '\0'
+	|| !global_options_set.x_rs6000_cpu_index)
+      && (rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
     {
       fputs ("\t.machine ", asm_out_file);
       if ((rs6000_isa_flags & OPTION_MASK_DIRECT_MOVE) != 0)
@@ -5199,10 +5204,8 @@
 	fputs ("power5\n", asm_out_file);
       else if ((rs6000_isa_flags & OPTION_MASK_MFCRF) != 0)
 	fputs ("power4\n", asm_out_file);
-      else if ((rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
-	fputs ("ppc64\n", asm_out_file);
       else
-	fputs ("ppc\n", asm_out_file);
+	fputs ("ppc64\n", asm_out_file);
     }
 #endif

--- src/external/gpl3/gcc.old/dist/gcc/config/rs6000/rs6000.c.orig	2016-07-26 20:35:21.497737069 +0900
+++ src/external/gpl3/gcc.old/dist/gcc/config/rs6000/rs6000.c	2016-07-26 21:09:00.819227210 +0900
@@ -4972,8 +4972,13 @@
     }

 #ifdef USING_ELFOS_H
-  if (rs6000_default_cpu == 0 || rs6000_default_cpu[0] == '\0'
-      || !global_options_set.x_rs6000_cpu_index)
+/*
+ * XXX .machine overrides CPU type specified by -mcpu or -Wa,-m options, which
+ * can result in corrupted output with inline assembler for ppc32.
+ */
+  if ((rs6000_default_cpu == 0 || rs6000_default_cpu[0] == '\0'
+	|| !global_options_set.x_rs6000_cpu_index)
+      && (rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
     {
       fputs ("\t.machine ", asm_out_file);
       if ((rs6000_isa_flags & OPTION_MASK_DIRECT_MOVE) != 0)
@@ -4986,10 +4991,8 @@
 	fputs ("power5\n", asm_out_file);
       else if ((rs6000_isa_flags & OPTION_MASK_MFCRF) != 0)
 	fputs ("power4\n", asm_out_file);
-      else if ((rs6000_isa_flags & OPTION_MASK_POWERPC64) != 0)
-	fputs ("ppc64\n", asm_out_file);
       else
-	fputs ("ppc\n", asm_out_file);
+	fputs ("ppc64\n", asm_out_file);
     }
 #endif
 }
====================

>Release-Note:

>Audit-Trail:
From: coypu@SDF.ORG
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: port-powerpc/51366: kernel for powerpc/ibm4xx miscompiled by gcc
 >= 4.8.5
Date: Sat, 17 Sep 2016 21:05:01 +0000

 hi,
 sorry, I'm neither a gcc nor a ppc expert, but nobody seems to be
 responding.

 what you seem to be suggesting: set the default for RS6000 to ppc64, as
 the kernel is miscompiled when ppc is chosen.
 aside from seeming like a very dangerous change, it's very confusing to
 me. the documentation I have[1] says that ppc405 is 32bit.

 Is it not?

 [1] - http://www2.informatik.hu-berlin.de/~fwinkler/psvfpga/amirix/405_um.pdf

From: Rin Okuyama <rokuyama@rk.phys.keio.ac.jp>
To: gnats-bugs@NetBSD.org, port-powerpc-maintainer@netbsd.org,
 gnats-admin@netbsd.org, netbsd-bugs@netbsd.org
Cc: 
Subject: Re: port-powerpc/51366: kernel for powerpc/ibm4xx miscompiled by gcc
 >= 4.8.5
Date: Tue, 20 Sep 2016 21:24:16 +0900

 On 2016/09/18 6:10, coypu@SDF.ORG wrote:
 >  what you seem to be suggesting: set the default for RS6000 to ppc64, as
 >  the kernel is miscompiled when ppc is chosen.
 >  aside from seeming like a very dangerous change, it's very confusing to
 >  me. the documentation I have[1] says that ppc405 is 32bit.

 My patch does nothing for 64-bit machines. It merely stops gcc issuing a
 bogus pseudo-op for 32-bit machines. gcc >= 4.8.5 generates ".machine ppc"
 pseudo-op for 32-bit environment, which discards CPU type specified by
 -mcpu or -Wa,-m option. This results in miscompiling kernel codes for,
 at least, 405.

From: Rin Okuyama <rokuyama@rk.phys.keio.ac.jp>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: port-powerpc/51366 kernel for powerpc/ibm4xx miscompiled by gcc
 >= 4.8.5
Date: Sun, 11 Dec 2016 02:09:50 +0900

 I've found the cause of this failure. The index of sprg for pid is
 different between generic ppc and IBM 405; 48 and 945, respectively.
 Therefore, for example, "mfpid N" is assembled into "mfspr rN, 48"
 for generic ppc, but it should be "mfspr rN, 945" for IBM 405.

 Since GCC >= 4.8.5 passes ".machine ppc" pseudo-op to assembler,
 "m[ft]pid" instructions in inline assembler codes are misassembled
 for IBM 405, even if -mcpu=405 or -Wa,-m405 options are specified.

From: "Rin Okuyama" <rin@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/51366 CVS commit: src/external/gpl3/gcc/dist/gcc/config/rs6000
Date: Sun, 18 Dec 2016 03:00:58 +0000

 Module Name:	src
 Committed By:	rin
 Date:		Sun Dec 18 03:00:58 UTC 2016

 Modified Files:
 	src/external/gpl3/gcc/dist/gcc/config/rs6000: rs6000.c

 Log Message:
 PR port-powerpc/51366 kernel for powerpc/ibm4xx miscompiled by gcc >= 4.8.5
 backport fix from upstream:

 URL: https://gcc.gnu.org/viewcvs?rev=238789&root=gcc&view=rev
 Log:
 rs6000: Fix logic for when to emit .machine (PR71216)

 The current logic determining whether to use .machine in the generated
 asm code puts it there if the compiler is not configured with a default
 target cpu, _or_ no -mcpu= was given on the command line.  It should
 be "and" instead.

 	PR target/71216
 	* config/rs6000/rs6000.c (rs6000_file_start): Fix condition for
 	when to emit a ".machine" pseudo-op.

 Modified:
     branches/gcc-5-branch/gcc/ChangeLog
     branches/gcc-5-branch/gcc/config/rs6000/rs6000.c


 To generate a diff of this commit:
 cvs rdiff -u -r1.15 -r1.16 \
     src/external/gpl3/gcc/dist/gcc/config/rs6000/rs6000.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->closed
State-Changed-By: rin@NetBSD.org
State-Changed-When: Sun, 18 Dec 2016 03:14:13 +0000
State-Changed-Why:
Fix committed. It seems too early to pull up to netbsd-7; we need to
fix port-powerpc/51367 at least to get ibm4xx working again.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2014 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.