Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 11 Aug 2002 05:40:58 +0900 (JST)
From:      NIIMI Satoshi <sa2c@sa2c.net>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   i386/41528: better stack alignment patch for lib/csu/i386-elf/
Message-ID:  <20020810204058.33B9231D@berkeley.sa2c.net>

next in thread | raw e-mail | index | archive | help

>Number:         41528
>Category:       i386
>Synopsis:       better stack alignment patch for lib/csu/i386-elf/
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat Aug 10 13:50:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     NIIMI Satoshi
>Release:        FreeBSD 4.6.1-RELEASE-p10 i386
>Organization:
>Environment:
System: FreeBSD berkeley.sa2c.net 4.6.1-RELEASE-p10 FreeBSD 4.6.1-RELEASE-p10 #5: Fri Aug 9 16:33:26 JST 2002 sa2c@berkeley.sa2c.net:/usr/obj/usr/src/sys/SA2C_NET i386


	
>Description:

Although system C compiler, GCC, maintains stack pointer to keep
aligned to 2**preferred-stack-boundary byte, C startup routine does
not care about this.

This causes a big performance penalty for floating point operations
with variables in stack frame because IA32 CPUs are optimized to
operate with aligned data.

	
>How-To-Repeat:
With code: foo.c
#include <stdio.h>
main()
{
	double x;
	printf("%p\n", &x);
}
% cc foo.c
% ./a.out
0xbfbff730	<- aligned to 8-byte boundary.
% ./a.out a
0xbfbff72c	<- not aligned if command line arguments are passed.
	
>Fix:

(gcc 3.1 masks %esp by himself, so this patch might not be required
for -current.)

--- diff begins here ---
Index: stable/lib/csu/i386-elf/crt1.c
===================================================================
RCS file: /home/ncvs/src/lib/csu/i386-elf/crt1.c,v
retrieving revision 1.4.2.1
diff -u -r1.4.2.1 crt1.c
--- stable/lib/csu/i386-elf/crt1.c	30 Oct 2000 20:32:24 -0000	1.4.2.1
+++ stable/lib/csu/i386-elf/crt1.c	10 Aug 2002 19:40:54 -0000
@@ -93,7 +93,33 @@
     monstartup(&eprol, &etext);
 #endif
     _init();
+#if 0
     exit( main(argc, argv, env) );
+#else
+    /*
+     * GCC expects stack frame to be aligned like following figure.
+     *
+     *  +--------------+
+     *  |%ebp (if any) |
+     *  +--------------+
+     *  |return address|
+     *  +--------------+ --- aligned by PREFERRED_STACK_BOUNDARY
+     *  |  arguments   |
+     *  |      :       |
+     *  |      :       |
+     */
+    __asm__ ("
+	subl	$12, %%esp		# create stack frame for arguments
+	andl	$~0xf, %%esp		# align stack to 16-byte boundary
+	movl	%0, 0(%%esp)
+	movl	%1, 4(%%esp)
+	movl	%2, 8(%%esp)
+	call	main
+	movl	%%eax, 0(%%esp)
+	call	exit
+	hlt				# do not return
+    " : : "r"(argc), "r"(argv), "r"(env));
+#endif
 }
 
 #ifdef GCRT
Index: current/lib/csu/i386-elf/crt1.c
===================================================================
RCS file: /home/ncvs/src/lib/csu/i386-elf/crt1.c,v
retrieving revision 1.9
diff -u -r1.9 crt1.c
--- current/lib/csu/i386-elf/crt1.c	16 Jul 2002 12:28:49 -0000	1.9
+++ current/lib/csu/i386-elf/crt1.c	10 Aug 2002 19:46:42 -0000
@@ -100,7 +100,33 @@
 	monstartup(&eprol, &etext);
 #endif
 	_init();
+#if 0
 	exit( main(argc, argv, env) );
+#else
+	/*
+	 * GCC expects stack frame to be aligned like following figure.
+	 *
+	 *  +--------------+
+	 *  |%ebp (if any) |
+	 *  +--------------+
+	 *  |return address|
+	 *  +--------------+ --- aligned by PREFERRED_STACK_BOUNDARY
+	 *  |  arguments   |
+	 *  |      :       |
+	 *  |      :       |
+	 */
+	__asm__ ("
+		subl	$12, %%esp	# create stack frame for arguments
+		andl	$~0xf, %%esp	# align stack to 16-byte boundary
+		movl	%0, 0(%%esp)
+		movl	%1, 4(%%esp)
+		movl	%2, 8(%%esp)
+		call	main
+		movl	%%eax, 0(%%esp)
+		call	exit
+		hlt			# do not return
+	" : : "r"(argc), "r"(argv), "r"(env));
+#endif
 }
 
 #ifdef GCRT
--- diff ends here ---

	
>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020810204058.33B9231D>