x86_64 support for klibc
PATCH 37/43 x86_64 support for klibc
The parts of klibc specific to the x86_64 architecture.
---
commit f889dd04bef1aed36ba18161c727af47338e167a
tree c25f184d2e3337b52dfe3abd191ec639d4d9543d
parent f30fa3db62972125afa68d3b53d03cdb843d3bbd
author H. Peter Anvin <[email protected]> Sun, 25 Jun 2006 16:58:53 -0700 committer H. Peter Anvin <[email protected]> Sun, 25 Jun 2006 16:58:53 -0700
usr/include/arch/x86_64/klibc/archconfig.h | 15 +++ usr/include/arch/x86_64/klibc/archsetjmp.h | 21 +++++ usr/include/arch/x86_64/klibc/archsignal.h | 18 ++++ usr/include/arch/x86_64/klibc/archstat.h | 28 ++++++ usr/include/arch/x86_64/klibc/archsys.h | 109 ++++++++++++++++++++++++ usr/include/arch/x86_64/sys/io.h | 127 ++++++++++++++++++++++++++++ usr/klibc/arch/x86_64/MCONFIG | 35 ++++++++ usr/klibc/arch/x86_64/Makefile.inc | 18 ++++ usr/klibc/arch/x86_64/crt0.S | 22 +++++ usr/klibc/arch/x86_64/setjmp.S | 54 ++++++++++++ usr/klibc/arch/x86_64/sigreturn.S | 15 +++ usr/klibc/arch/x86_64/syscall.S | 28 ++++++ usr/klibc/arch/x86_64/sysstub.ph | 23 +++++ usr/klibc/arch/x86_64/vfork.S | 26 ++++++ 14 files changed, 539 insertions(+), 0 deletions(-)
usr/include/arch/x86_64/klibc/archconfig.h new file +15 -0
@@ -0,0 +1,15 @@ +/* +* include/arch/x86_64/klibc/archconfig.h +* +* See include/klibc/sysconfig.h for the options that can be set in +* this file. +* +*/ + +#ifndef _KLIBC_ARCHCONFIG_H +#define _KLIBC_ARCHCONFIG_H + +/* x86-64 doesn't provide a default sigreturn. */ +#define _KLIBC_NEEDS_SA_RESTORER 1 + +#endif /* _KLIBC_ARCHCONFIG_H */
usr/include/arch/x86_64/klibc/archsetjmp.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* arch/x86_64/include/klibc/archsetjmp.h +*/ + +#ifndef _KLIBC_ARCHSETJMP_H +#define _KLIBC_ARCHSETJMP_H + +struct __jmp_buf { +unsigned long __rbx; +unsigned long __rsp; +unsigned long __rbp; +unsigned long __r12; +unsigned long __r13; +unsigned long __r14; +unsigned long __r15; +unsigned long __rip; +}; + +typedef struct __jmp_buf jmp_buf [1 ]; + +#endif /* _SETJMP_H */
usr/include/arch/x86_64/klibc/archsignal.h new file +18 -0
@@ -0,0 +1,18 @@ +/* +* arch/x86_64/include/klibc/archsignal.h +* +* Architecture-specific signal definitions +* +*/ + +#ifndef _KLIBC_ARCHSIGNAL_H +#define _KLIBC_ARCHSIGNAL_H + +#include <asm/signal.h> +/* The x86-64 headers defines NSIG 32, but it's actually 64 */ +#undef _NSIG +#undef NSIG +#define _NSIG 64 +#define NSIG _NSIG + +#endif
usr/include/arch/x86_64/klibc/archstat.h new file +28 -0
@@ -0,0 +1,28 @@ +#ifndef _KLIBC_ARCHSTAT_H +#define _KLIBC_ARCHSTAT_H + +#include <klibc/stathelp.h> + +#define _STATBUF_ST_NSEC + +struct stat { +__stdev64 (st_dev); +unsigned long st_ino; +unsigned long st_nlink; + +unsigned int st_mode; +unsigned int st_uid; +unsigned int st_gid; +unsigned int __pad0; +__stdev64 (st_rdev); +long st_size; +long st_blksize; +long st_blocks;/* Number 512-byte blocks allocated. */ + +struct timespec st_atim; +struct timespec st_mtim; +struct timespec st_ctim; +long __unused[3 ]; +}; + +#endif
usr/include/arch/x86_64/klibc/archsys.h new file +109 -0
109 lines not on this page · raw message
usr/include/arch/x86_64/sys/io.h new file +127 -0
127 lines not on this page · raw message
usr/klibc/arch/x86_64/MCONFIG new file +35 -0
@@ -0,0 +1,35 @@ +# -*- makefile -*- +# +# arch/x86-64/MCONFIG +# +# Special rules for this architecture. Note that this is actually +# included from the main Makefile, and that pathnames should be +# accordingly. +# +# Blatantly copied and modified from i386 version by Mats Petersson, AMD. +# + +# +# NOTE: -fno-asynchronous-unwind-tables produce significantly smaller +# binaries (20% smaller), but makes the code completely useless for +# debugging using gdb. +# +KLIBCARCHREQFLAGS = -m64 +ifeq ($(DEBUG),y) +KLIBCOPTFLAGS = -Os -fomit-frame-pointer \ + -falign-functions=1 -falign-jumps=1 -falign-loops=1 +else +KLIBCOPTFLAGS = -Os -fno-asynchronous-unwind-tables -fomit-frame-pointer \ + -falign-functions=1 -falign-jumps=1 -falign-loops=1 +endif +KLIBCBITSIZE = 64 +KLIBCLDFLAGS = -m elf_x86_64 + +# Extra linkflags when building the shared version of the library +# This address needs to be reachable using normal inter-module +# calls, and work on the memory models for this architecture +# 2 MB - normal binaries start at 4 MB +KLIBCSHAREDFLAGS = -Ttext 0x00200200 + +# Additional asm- directories needed during installation +ASMARCH = asm-i386
usr/klibc/arch/x86_64/Makefile.inc new file +18 -0
@@ -0,0 +1,18 @@ +# -*- makefile -*- +# +# arch/x86_64/Makefile.inc +# +# Special rules for this architecture. Note that this is actually +# included from the main Makefile, and that pathnames should be +# accordingly. +# + +KLIBCARCHOBJS = \ + arch/$(KLIBCARCH)/setjmp.o \ + arch/$(KLIBCARCH)/syscall.o \ + arch/$(KLIBCARCH)/sigreturn.o \ + arch/$(KLIBCARCH)/vfork.o + +KLIBCARCHSOOBJS = $(patsubst %.o,%.lo,$(KLIBCARCHOBJS)) + +archclean:
usr/klibc/arch/x86_64/crt0.S new file +22 -0
@@ -0,0 +1,22 @@ +# +# arch/x86_64/crt0.S +# +# Does arch-specific initialization and invokes __libc_init +# with the appropriate arguments. +# +# See __static_init.c or __shared_init.c for the expected +# arguments. +# + + .text + .align 4 + .type _start,@function + .globl _start +_start: + movq %rsp,%rdi # Offset of the ELF data structure + movq %rdx,%rsi # The atexit() pointer (if any) + call __libc_init + # We should never get here... + hlt + + .size _start,.-_start
usr/klibc/arch/x86_64/setjmp.S new file +54 -0
54 lines not on this page · raw message
usr/klibc/arch/x86_64/sigreturn.S new file +15 -0
@@ -0,0 +1,15 @@ +/* + * arch/x86_64/sigreturn.S + */ + +#include <asm/unistd.h> + + .text + .align 4 + .globl __sigreturn + .type __sigreturn,@function +__sigreturn: + movl $__NR_rt_sigreturn,%eax + syscall + + .size __sigreturn,.-__sigreturn
usr/klibc/arch/x86_64/syscall.S new file +28 -0
@@ -0,0 +1,28 @@ +/* + * arch/x86-64/syscall.S + * + * Common tail-handling code for system calls. + * + * The arguments are in the standard argument registers; the system + * call number in %eax. + */ + .text + .align 4 + .globl __syscall_common + .type __syscall_common,@function +__syscall_common: + movq %rcx,%r10 # The kernel uses %r10 istf %rcx + syscall + + cmpq $-4095,%rax + jnb 1f + ret + + # Error return, must set errno +1: + negl %eax + movl %eax,errno(%rip) # errno is type int, so 32 bits + orq $-1,%rax # orq $-1 smaller than movq $-1 + ret + + .size __syscall_common,.-__syscall_common
usr/klibc/arch/x86_64/sysstub.ph new file +23 -0
@@ -0,0 +1,23 @@ +# -*- perl -*- +# +# arch/x86_64/sysstub.ph +# +# Script to generate system call stubs +# + +sub make_sysstub($$$$$@) { + my($outputdir, $fname, $type, $sname, $stype, @args) = @_; + + open(OUT, '>', "${outputdir}/${fname}.S"); + print OUT "#include <asm/unistd.h>\n"; + print OUT "\n"; + print OUT "\t.type ${fname},\@function\n"; + print OUT "\t.globl ${fname}\n"; + print OUT "${fname}:\n"; + print OUT "\tmovl \$__NR_${sname},%eax\n"; # Zero-extends to 64 bits + print OUT "\tjmp __syscall_common\n"; + print OUT "\t.size ${fname},.-${fname}\n"; + close(OUT); +} + +1;
usr/klibc/arch/x86_64/vfork.S new file +26 -0
@@ -0,0 +1,26 @@ +# +# usr/klibc/arch/x86_64/vfork.S +# +# vfork is nasty - there must be nothing at all on the stack above +# the stack frame of the enclosing function. +# + +#include <asm/unistd.h> + + .text + .align 4 + .globl vfork + .type vfork, @function +vfork: + pop %rdx /* Return address */ + movl $__NR_vfork, %eax + syscall + push %rdx + cmpq $-4095, %rax + jae 1f + ret +1: + negl %eax + movl %eax, errno(%rip) + orq $-1, %rax + ret
usr/include/arch/x86_64/klibc/archsignal.h line 16
Quoted 7 lines · H. Peter Anvin
usr/include/arch/x86_64/klibc/archsignal.h new file +7
-0+ +#include <asm/signal.h> +/* The x86-64 headers defines NSIG 32, but it's actually 64 */ +#undef _NSIG +#undef NSIG +#define _NSIG 64 +#define NSIG _NSIG
If it's really wrong it should be fixed, not workarounded.
usr/include/arch/x86_64/klibc/archsys.h line 18
Quoted 10 lines
usr/include/arch/x86_64/klibc/archsys.h new file +10
-0+ +/* The x86-64 syscall headers are needlessly inefficient */ + +#undef _syscall0 +#undef _syscall1 +#undef _syscall2 +#undef _syscall3 +#undef _syscall4 +#undef _syscall5 +#undef _syscall6
What do you mean with that?
-Andi
Quoted 11 lines · Andi Kleen
usr/include/arch/x86_64/klibc/archsignal.h line 16
Quoted 7 lines · H. Peter Anvin
usr/include/arch/x86_64/klibc/archsignal.h new file +7
-0+ +#include <asm/signal.h> +/* The x86-64 headers defines NSIG 32, but it's actually 64 */ +#undef _NSIG +#undef NSIG +#define _NSIG 64 +#define NSIG _NSIGIf it's really wrong it should be fixed, not workarounded.
It is wrong... include/asm-x86_64/signal.h has cribbed a bit too much from i386:
ifdef __KERNEL__
/* Most things should be clean enough to redefine this at will, if care
is taken to make libc match. */
#define _NSIG 64 #define _NSIG_BPW 64 #define _NSIG_WORDS (_NSIG / _NSIG_BPW)
typedef unsigned long old_sigset_t; /* at least 32 bits */
typedef struct {
unsigned long sig[_NSIG_WORDS];
} sigset_t;struct pt_regs;
asmlinkage int do_signal(struct pt_regs *regs, sigset_t *oldset);
#else
/* Here we must cater to libcs that poke about in kernel headers. */
#define NSIG 32
typedef unsigned long sigset_t;
#endif /* __KERNEL__ */
#endif
I can't remove the workaround just yet, since I have active users of the out-of-tree version. However, I will send you a patch for include/asm-x86_64/signal.h.
Quoted 4 lines
Quoted 4 lines
usr/include/arch/x86_64/klibc/archsys.h new file +4
-0+ +/* The x86-64 syscall headers are needlessly inefficient */ + +#undef _syscall0
Quoted 9 lines
usr/include/arch/x86_64/klibc/archsys.h line 18
Quoted 6 lines
usr/include/arch/x86_64/klibc/archsys.h new file +6
-0+#undef _syscall1 +#undef _syscall2 +#undef _syscall3 +#undef _syscall4 +#undef _syscall5 +#undef _syscall6What do you mean with that?
At one point klibc was using the _syscall() macros, and I observed that the x86-64 ones generated extra instructions because of the "movq"s that were hard-coded in the patterns (using register variables, one can make gcc do any moves that may or may not be necessary.)
However, klibc no longer uses these except on ia64, so I removed <klibc/archsys.h> for all other architectures from the tree last night.
-hpa
Quoted 41 lines · H. Peter Anvin
Quoted 11 lines · Andi Kleen
usr/include/arch/x86_64/klibc/archsignal.h line 16
Quoted 7 lines · H. Peter Anvin
usr/include/arch/x86_64/klibc/archsignal.h new file +7
-0+ +#include <asm/signal.h> +/* The x86-64 headers defines NSIG 32, but it's actually 64 */ +#undef _NSIG +#undef NSIG +#define _NSIG 64 +#define NSIG _NSIGIf it's really wrong it should be fixed, not workarounded.
It is wrong...
include/asm-x86_64/signal.hhas cribbed a bit too much from i386:ifdef
__KERNEL__
/* Most things should be clean enough to redefine this at will, if care
is taken to make libc match. */#define _NSIG 64 #define _NSIG_BPW 64 #define _NSIG_WORDS (_NSIG / _NSIG_BPW)typedef unsigned long
old_sigset_t; /* at least 32 bits */typedef struct { unsigned long sig[_NSIG_WORDS]; } sigset_t;struct
pt_regs;
asmlinkage intdo_signal(struct pt_regs *regs, sigset_t *oldset);#else
/* Here we must cater to libcs that poke about in kernel headers. */#define NSIG 32
typedef unsigned longsigset_t;
Quoted 7 lines
#endif /*
__KERNEL__*/
#endifI can't remove the workaround just yet, since I have active users of the out-of-tree version. However, I will send you a patch for
include/asm-x86_64/signal.h.
Please do.
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
usr/include/arch/x86_64/klibc/archsys.h new file +4
-0+ +/* The x86-64 syscall headers are needlessly inefficient */ + +#undef _syscall0Quoted 9 lines
usr/include/arch/x86_64/klibc/archsys.h line 18
Quoted 6 lines
usr/include/arch/x86_64/klibc/archsys.h new file +6
-0+#undef _syscall1 +#undef _syscall2 +#undef _syscall3 +#undef _syscall4 +#undef _syscall5 +#undef _syscall6What do you mean with that?
Quoted 4 lines
At one point klibc was using the
_syscall()macros, and I observed that the x86-64 ones generated extra instructions because of the "movq"s that were hard-coded in the patterns (using register variables, one can make gcc do any moves that may or may not be necessary.)
No you can't. We tried that first, but it generated incorrect code on older gcc versions. gcc doesn't behave gratefully when an inline statement takes nearly all register classes (x86-64 has less register classes than real registers so it's a bit non obvious). I added the moves to hide some registers at the advice of gcc hackers.
Maybe you got lucky with newer gcc versions, but I would recommend to use the standard versions to be backwards compatible.
-Andi