[PATCH 3/7] add AVR32 support to libc

Hans-Christian Egtvedt hcegtvedt at atmel.com
Mon Nov 5 23:50:06 PST 2007


This patch adds AVR32 support to libc.

Signed-off-by: Hans-Christian Egtvedt <hcegtvedt at atmel.com>
--- 

diff --git a/libc/sysdeps/linux/avr32/Makefile b/libc/sysdeps/linux/avr32/Makefile
new file mode 100644
index 0000000..338abc0
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/Makefile
@@ -0,0 +1,25 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2003 Erik Andersen <andersen at uclibc.org>
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU Library General Public License as published by the Free
+# Software Foundation; either version 2 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Library General Public License
+# along with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+top_srcdir=../../../../
+top_builddir=../../../../
+all: objs
+
+include $(top_builddir)Rules.mak
+include Makefile.arch
+include $(top_srcdir)Makerules
diff --git a/libc/sysdeps/linux/avr32/Makefile.arch b/libc/sysdeps/linux/avr32/Makefile.arch
new file mode 100644
index 0000000..44fc01e
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/Makefile.arch
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen at uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+CSRC	:= brk.c clone.c mmap.c sigaction.c
+
+SSRC	:= __longjmp.S setjmp.S bsd-setjmp.S bsd-_setjmp.S	\
+		sigrestorer.S syscall.S vfork.S
+
+include $(top_srcdir)/libc/sysdeps/linux/Makefile.commonarch
diff --git a/libc/sysdeps/linux/avr32/__longjmp.S b/libc/sysdeps/linux/avr32/__longjmp.S
new file mode 100644
index 0000000..6154bb2
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/__longjmp.S
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+
+	.global	__longjmp
+	.type	__longjmp,"function"
+	.align	1
+__longjmp:
+	ldm	r12++, r0-r8,sp,lr
+	mustr	r8		/* restore status register (lower half) */
+	cp	r11, 0		/* can't return zero */
+	frs
+	moveq	r11, 1
+	retal	r11
+	.size	__longjmp, . - __longjmp
+
+libc_hidden_def(__longjmp)
diff --git a/libc/sysdeps/linux/avr32/brk.c b/libc/sysdeps/linux/avr32/brk.c
new file mode 100644
index 0000000..a54b49a
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/brk.c
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+#include <errno.h>
+#include <unistd.h>
+#include <sys/syscall.h>
+
+libc_hidden_proto(brk)
+
+void *__curbrk attribute_hidden = 0;
+
+int brk (void *addr)
+{
+	void *newbrk;
+
+	newbrk = (void *)INLINE_SYSCALL(brk, 1, addr);
+
+	__curbrk = newbrk;
+
+	if (newbrk < addr) {
+		__set_errno (ENOMEM);
+		return -1;
+	}
+
+	return 0;
+}
+libc_hidden_def(brk)
diff --git a/libc/sysdeps/linux/avr32/bsd-_setjmp.S b/libc/sysdeps/linux/avr32/bsd-_setjmp.S
new file mode 100644
index 0000000..be66a10
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/bsd-_setjmp.S
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+
+	/* This just does a tail-call to __sigsetjmp(env, 0) */
+	.global	_setjmp
+	.type	_setjmp,"function"
+	.align	1
+_setjmp:
+	mov	r11, 0
+	bral	__GI___sigsetjmp
+	.size	_setjmp, . - _setjmp
diff --git a/libc/sysdeps/linux/avr32/bsd-setjmp.S b/libc/sysdeps/linux/avr32/bsd-setjmp.S
new file mode 100644
index 0000000..4635eeb
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/bsd-setjmp.S
@@ -0,0 +1,16 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+
+	/* This just does a tail-call to __sigsetjmp(env, 1) */
+	.global	setjmp
+	.type	setjmp,"function"
+	.align	1
+setjmp:
+	mov	r11, 1
+	bral	__GI___sigsetjmp
+	.size	setjmp, . - setjmp
diff --git a/libc/sysdeps/linux/avr32/clone.c b/libc/sysdeps/linux/avr32/clone.c
new file mode 100644
index 0000000..e43b0f3
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/clone.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2004 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+#include <errno.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+/*
+ * I don't know if we can be absolutely certain that the fn and arg
+ * parameters are preserved when returning as the child. If the
+ * compiler stores them in registers (r0-r7), they should be.
+ */
+int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg)
+{
+	register int (*_fn)(void *arg) = fn;
+	register void *_arg = arg;
+	int err;
+
+	/* Sanity check the arguments */
+	err = -EINVAL;
+	if (!fn)
+		goto syscall_error;
+	if (!child_stack)
+		goto syscall_error;
+
+	err = INLINE_SYSCALL(clone, 2, flags, child_stack);
+	if (err < 0)
+		goto syscall_error;
+	else if (err != 0)
+		return err;
+
+	_exit(_fn(_arg));
+
+syscall_error:
+	__set_errno (-err);
+	return -1;
+}
diff --git a/libc/sysdeps/linux/avr32/crt1.S b/libc/sysdeps/linux/avr32/crt1.S
new file mode 100644
index 0000000..ca1fa7a
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/crt1.S
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ *
+ * When we enter _start, the stack looks like this:
+ *	argc		argument counter
+ *	argv[0]		pointer to program name
+ *	argv[1..argc-1]	pointers to program args
+ *	NULL
+ *	env[0..N]	pointers to environment variables
+ *	NULL
+ *
+ * r12 contains a function pointer to be registered with `atexit'.
+ * This is how the dynamic linker arranges to have DT_FINI functions
+ * called for shared libraries that have been loaded before this
+ * code runs.
+ *
+ * We're going to call the following function:
+ * __uClibc_main(int (*main)(int, char **, char **), int argc,
+ *		 char **argv, void (*app_init)(void), void (*app_fini)(void),
+ *		 void (*rtld_fini)(void), void *stack_end)
+ *
+ * So we need to set up things as follows:
+ *	r12 = address of main
+ *	r11 = argc
+ *	r10 = &argv[0]
+ *	r9  = address of _init
+ *	r8  = address of _fini
+ *	sp[0] = whatever we got passed in r12
+ */
+
+#include <features.h>
+
+	.text
+	.global _start
+	.type	_start, @function
+_start:
+	/* Clear the frame pointer and link register since this is the outermost frame.  */
+	mov	r7, 0
+	mov	lr, 0
+
+	ld.w	r11, sp++		/* argc		*/
+	mov	r10, sp			/* &argv[0]	*/
+
+	st.w	--sp, r10		/* stack_end */
+	st.w	--sp, r12		/* rtld_fini */
+
+#ifdef __PIC__
+	lddpc	r6, .L_GOT
+.L_RGOT:
+	rsub	r6, pc
+	lda.w	r9, _init
+	lda.w	r8, _fini
+	lda.w	r12, main
+
+	/* Ok, now run uClibc's main() -- should not return */
+	call	__uClibc_main
+
+	.align	2
+.L_GOT:
+	.long	.L_RGOT - _GLOBAL_OFFSET_TABLE_
+#else
+	lddpc	r9, __init_addr		/* app_init */
+	lddpc	r8, __fini_addr		/* app_fini */
+	lddpc	r12, __main_addr	/* main */
+
+	/* Ok, now run uClibc's main() -- should not return */
+	lddpc	pc, ___uClibc_main_addr
+
+	.align	2
+__init_addr:
+	.long	_init
+__fini_addr:
+	.long	_fini
+__main_addr:
+	.long	main
+___uClibc_main_addr:
+	.long	__uClibc_main
+#endif
+	.size	_start, . - _start
+
+	/*
+	 * The LSB says we need this.
+	 */
+	.section ".note.ABI-tag", "a"
+	.align	4
+	.long	2f - 1f		/* namesz */
+	.long	4f - 3f		/* descsz */
+	.long	1		/* type   */
+1:	.asciz	"GNU"		/* name */
+2:	.align	4
+3:	.long	0		/* Linux executable */
+	.long	2,6,0		/* Earliest compatible kernel */
+4:	.align	4
diff --git a/libc/sysdeps/linux/avr32/crti.S b/libc/sysdeps/linux/avr32/crti.S
new file mode 100644
index 0000000..660f47c
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/crti.S
@@ -0,0 +1,26 @@
+
+	.section .init
+	.align	2
+	.global	_init
+	.type	_init, @function
+_init:
+	stm	--sp, r6, lr
+	lddpc	r6, 2f
+1:	rsub	r6, pc
+	rjmp	3f
+	.align	2
+2:	.long	1b - _GLOBAL_OFFSET_TABLE_
+3:
+
+	.section .fini
+	.align	2
+	.global	_fini
+	.type	_fini, @function
+_fini:
+	stm	--sp, r6, lr
+	lddpc	r6, 2f
+1:	rsub	r6, pc
+	rjmp	3f
+	.align	2
+2:	.long	1b - _GLOBAL_OFFSET_TABLE_
+3:
diff --git a/libc/sysdeps/linux/avr32/crtn.S b/libc/sysdeps/linux/avr32/crtn.S
new file mode 100644
index 0000000..f7d1040
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/crtn.S
@@ -0,0 +1,14 @@
+
+	.section .init
+	.align	2
+	.global	_init
+	.type	_init, @function
+	ldm	sp++, r6, pc
+	.size	_init, . - _init
+
+	.section .fini
+	.align	2
+	.global _fini
+	.type	_fini, @function
+	ldm	sp++, r6, pc
+	.size	_fini, . - _fini
diff --git a/libc/sysdeps/linux/avr32/mmap.c b/libc/sysdeps/linux/avr32/mmap.c
new file mode 100644
index 0000000..2ee025a
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/mmap.c
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+
+#include <errno.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+
+libc_hidden_proto(mmap)
+
+static _syscall6(__ptr_t, mmap2, __ptr_t, addr, size_t, len, int, prot,
+		 int, flags, int, fd, __off_t, pgoff);
+
+__ptr_t mmap(__ptr_t addr, size_t len, int prot, int flags, int fd, __off_t offset)
+{
+	unsigned long page_size = sysconf(_SC_PAGESIZE);
+	unsigned long pgoff;
+
+	if (offset & (page_size - 1)) {
+		__set_errno(EINVAL);
+		return MAP_FAILED;
+	}
+
+	pgoff = (unsigned long)offset >> (31 - __builtin_clz(page_size));
+
+	return mmap2(addr, len, prot, flags, fd, pgoff);
+}
+libc_hidden_def(mmap)
diff --git a/libc/sysdeps/linux/avr32/setjmp.S b/libc/sysdeps/linux/avr32/setjmp.S
new file mode 100644
index 0000000..7d0354b
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/setjmp.S
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+#define _SETJMP_H
+#define _ASM
+#include <bits/setjmp.h>
+
+	.text
+
+	.global	__sigsetjmp
+	.type	__sigsetjmp,"function"
+
+	.align	1
+__sigsetjmp:
+	mustr	r8
+	stm	r12, r0,r1,r2,r3,r4,r5,r6,r7,r8,sp,lr
+
+	/*
+	 * Make a tail call to __sigjmp_save; it takes the same args
+	 * and is hidden so we don't need to mess around with the GOT.
+	 */
+	rjmp	__sigjmp_save
+	.size	__sigsetjmp, . - __sigsetjmp
+
+libc_hidden_def(__sigsetjmp)
diff --git a/libc/sysdeps/linux/avr32/sigaction.c b/libc/sysdeps/linux/avr32/sigaction.c
new file mode 100644
index 0000000..a97ff3d
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/sigaction.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+#include <errno.h>
+#include <signal.h>
+#include <string.h>
+#include <sys/syscall.h>
+#include <bits/kernel_sigaction.h>
+
+#define SA_RESTORER	0x04000000
+extern void __default_rt_sa_restorer(void);
+
+libc_hidden_proto(memcpy)
+
+/*
+ * If act is not NULL, change the action for sig to *act.
+ * If oact is not NULL, put the old action for sig in *oact.
+ */
+int __libc_sigaction(int signum, const struct sigaction *act,
+		     struct sigaction *oldact)
+{
+	struct kernel_sigaction kact, koact;
+	int result;
+
+	if (act) {
+		kact.k_sa_handler = act->sa_handler;
+		memcpy(&kact.sa_mask, &act->sa_mask, sizeof (kact.sa_mask));
+		kact.sa_flags = act->sa_flags;
+		if (kact.sa_flags & (SA_RESTORER | SA_ONSTACK))
+			kact.sa_restorer = act->sa_restorer;
+		else
+			kact.sa_restorer = __default_rt_sa_restorer;
+		kact.sa_flags |= SA_RESTORER;
+	}
+
+	result = __syscall_rt_sigaction(signum, act ? __ptrvalue(&kact) : NULL,
+					oldact ? __ptrvalue(&koact) : NULL,
+					_NSIG / 8);
+
+	if (oldact && result >= 0) {
+		oldact->sa_handler = koact.k_sa_handler;
+		memcpy(&oldact->sa_mask, &koact.sa_mask,
+		       sizeof(oldact->sa_mask));
+		oldact->sa_flags = koact.sa_flags;
+		oldact->sa_restorer = koact.sa_restorer;
+	}
+
+	return result;
+}
+
+#ifndef LIBC_SIGACTION
+libc_hidden_proto(sigaction)
+weak_alias(__libc_sigaction, sigaction)
+libc_hidden_weak(sigaction)
+#endif
diff --git a/libc/sysdeps/linux/avr32/sigrestorer.S b/libc/sysdeps/linux/avr32/sigrestorer.S
new file mode 100644
index 0000000..df6a1ba
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/sigrestorer.S
@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2004 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+#include <sys/syscall.h>
+
+	.global	__default_rt_sa_restorer
+	.type	__default_rt_sa_restorer,"function"
+	.align	1
+__default_rt_sa_restorer:
+	mov	r8, __NR_rt_sigreturn
+	scall
diff --git a/libc/sysdeps/linux/avr32/syscall.S b/libc/sysdeps/linux/avr32/syscall.S
new file mode 100644
index 0000000..55c1b1f
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/syscall.S
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2004-2007 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+#include <features.h>
+
+	.text
+
+	/*
+	 * long int syscall(long int sysno, ...)
+	 */
+	.global	syscall
+	.type	syscall, @function
+	.align	2
+syscall:
+	stm	--sp, r3,r5,r6,lr
+	sub	lr, sp, -16
+	mov	r8, r12
+	ldm	lr, r3,r5,r9-r12
+	scall
+	cp.w	r12, -4095
+	brlo	.Ldone
+
+#ifdef __PIC__
+	lddpc	r6, .Lgot
+.Lgotcalc:
+	rsub	r6, pc
+# ifdef __UCLIBC_HAS_THREADS__
+	rsub	r3, r12, 0
+	mcall	r6[__errno_location at got]
+	st.w	r12[0], r3
+# else
+	ld.w	r3, r6[errno at got]
+	neg	r12
+	st.w	r3[0], r12
+# endif
+#else
+# ifdef __UCLIBC_HAS_THREADS__
+	rsub	r3, r12, 0
+	mcall	.Lerrno_location
+	st.w	r12[0], r3
+# else
+	lddpc	r3, .Lerrno
+	neg	r12
+	st.w	r3[0], r12
+# endif
+#endif
+	mov	r12, -1
+
+.Ldone:
+	ldm	sp++, r3,r5,r6,pc
+
+	.align	2
+#ifdef __PIC__
+.Lgot:
+	.long	.Lgotcalc - _GLOBAL_OFFSET_TABLE_
+#else
+# ifdef __UCLIBC_HAS_THREADS__
+.Lerrno_location:
+	.long	__errno_location
+# else
+.Lerrno:
+	.long	errno
+# endif
+#endif
+
+
+	.size	syscall, . - syscall
diff --git a/libc/sysdeps/linux/avr32/vfork.S b/libc/sysdeps/linux/avr32/vfork.S
new file mode 100644
index 0000000..03ca99f
--- /dev/null
+++ b/libc/sysdeps/linux/avr32/vfork.S
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2005 Atmel Corporation
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser General
+ * Public License.  See the file "COPYING.LIB" in the main directory of this
+ * archive for more details.
+ */
+
+/*
+ * Clone the process without copying the address space.  The
+ * calling process is suspended until the child either exits
+ * or calls execve.
+ *
+ * This all means that we cannot rely on the stack to store
+ * away registers, since they will be overwritten by the child
+ * as soon as it makes another function call (e.g. execve()).
+ * Fortunately, the Linux kernel preserves LR across system calls.
+ */
+
+#include <features.h>
+#include <sys/syscall.h>
+
+	.global	__vfork
+	.type	__vfork, at function
+	.align	1
+__vfork:
+	mov	r8, __NR_vfork
+	scall
+	cp.w	r12, -4096
+	retls	r12
+
+	/* vfork failed, so we may use the stack freely */
+	pushm	r4-r7,lr
+#ifdef __PIC__
+	lddpc	r6, .L_GOT
+	rsub	r4, r12, 0
+.L_RGOT:
+	rsub	r6, pc
+	mcall	r6[__errno_location at got]
+#else
+	rsub	r4, r12, 0
+	mcall	.L__errno_location
+#endif
+	st.w	r12[0], r4
+	popm	r4-r7,pc,r12=-1
+
+	.align	2
+#ifdef __PIC__
+.L_GOT:
+	.long	.L_RGOT - _GLOBAL_OFFSET_TABLE_
+#else
+.L__errno_location:
+	.long	__errno_location
+#endif
+	.size	__vfork, . - __vfork
+
+weak_alias(__vfork,vfork)
+libc_hidden_weak(vfork)


More information about the uClibc mailing list