mailing list of musl libc
 help / color / mirror / code / Atom feed
* musl-based toolchain for J2/SH-2
@ 2015-08-26 17:42 Rich Felker
  2015-08-26 18:08 ` musl-based toolchain for J2/SH-2 - kernel patches Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Rich Felker @ 2015-08-26 17:42 UTC (permalink / raw)
  To: 0pf; +Cc: musl, nommu

I've packaged up a small cross-compiler-toolchain build script that's
especially geared towards use with the Open Processor Foundation's J2
Core, which uses the SH-2 ISA, and in the future other NOMMU archs:

https://github.com/richfelker/musl-cross-make

Some special toolchain considerations are needed to support targets
without an MMU. Unlike the legacy uClinux approach of using a custom
binary format with its own encoding of relocations/fixups, musl's
approach to NOMMU uses the standard ELF binary format, and binaries
are forwards-compatible with cpu models that do have an MMU.

Since it's necessary for programs to be loadable at arbitrary
addresses (due to the lack of virtual memory), position-independent
executables (PIE) are mandatory. This toolchain is patched to produce
PIE by default (similar to what's done on various hardened Linux
distributions) and to make it possible to use static linking alongside
PIE.

Some of the PIE-related work is only complete for SH (including J2)
targets. I don't plan to spend more time on adding static-PIE support
for other archs to binutils 2.25.1 because I already have a proposed
patch for binutils git master that will hopefully make it into the
next binutils release; if it doesn't, at least that patch should
cleanly apply to the next binutils release. I do plan to add GCC-side
coverage for other archs with the aim of producing a patch suitable
for upstream GCC.

The FDPIC ABI variant, which would be needed to support shareable text
and execute-in-place, is not yet supported by musl, nor does upstream
GCC have support for SH FDPIC code generation. This will be added
later.

Rich


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: musl-based toolchain for J2/SH-2 - kernel patches
  2015-08-26 17:42 musl-based toolchain for J2/SH-2 Rich Felker
@ 2015-08-26 18:08 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2015-08-26 18:08 UTC (permalink / raw)
  To: 0pf; +Cc: musl, nommu

[-- Attachment #1: Type: text/plain, Size: 1127 bytes --]

On Wed, Aug 26, 2015 at 01:42:12PM -0400, Rich Felker wrote:
> I've packaged up a small cross-compiler-toolchain build script that's
> especially geared towards use with the Open Processor Foundation's J2
> Core, which uses the SH-2 ISA, and in the future other NOMMU archs:
> 
> https://github.com/richfelker/musl-cross-make

One more thing: A couple kernel-side patches are needed to support the
binaries produced by this toolchain on SH-2/J2 and other NOMMU
systems. One adds the unified syscall trap number for SH-2/3/4; the
other enables loading of ELF files on NOMMU. Early versions of both
were included in this email to the musl list:

http://www.openwall.com/lists/musl/2015/06/16/5

The version of the SH unified syscall trap patch that's been proposed
upstream is available here as well:

http://lkml.iu.edu/hypermail/linux/kernel/1508.3/01702.html

It touches some of the same code touched by the 0pf/J2 patches:

http://lkml.iu.edu/hypermail/linux/kernel/1506.2/02538.html

so some minor work to merge them is needed for using this on the real
J2 hardware.

I'm attaching both patches to this email as well.

Rich

[-- Attachment #2: sh-unified-syscall-v2.diff --]
[-- Type: text/plain, Size: 3459 bytes --]

diff -urp ../linux-4.2-rc6.orig/arch/sh/kernel/cpu/sh2/entry.S ./arch/sh/kernel/cpu/sh2/entry.S
--- ../linux-4.2-rc6.orig/arch/sh/kernel/cpu/sh2/entry.S	2015-08-09 19:54:30.000000000 +0000
+++ ./arch/sh/kernel/cpu/sh2/entry.S	2015-08-24 03:23:34.159387924 +0000
@@ -144,9 +144,9 @@ ENTRY(exception_handler)
 	mov	#64,r8
 	cmp/hs	r8,r9
 	bt	interrupt_entry	! vec >= 64 is interrupt
-	mov	#32,r8
+	mov	#31,r8
 	cmp/hs	r8,r9
-	bt	trap_entry	! 64 > vec >= 32  is trap
+	bt	trap_entry	! 64 > vec >= 31  is trap
 
 	mov.l	4f,r8
 	mov	r9,r4
@@ -178,9 +178,9 @@ interrupt_entry:
 
 trap_entry:
 	mov	#0x30,r8
-	cmp/ge	r8,r9		! vector 0x20-0x2f is systemcall
+	cmp/ge	r8,r9		! vector 0x1f-0x2f is systemcall
 	bt	1f
-	add	#-0x10,r9	! convert SH2 to SH3/4 ABI
+	mov     #0x1f,r9	! convert to unified SH2/3/4 trap number
 1:	
 	shll2	r9			! TRA
 	bra	system_call	! jump common systemcall entry
diff -urp ../linux-4.2-rc6.orig/arch/sh/kernel/cpu/sh2a/entry.S ./arch/sh/kernel/cpu/sh2a/entry.S
--- ../linux-4.2-rc6.orig/arch/sh/kernel/cpu/sh2a/entry.S	2015-08-09 19:54:30.000000000 +0000
+++ ./arch/sh/kernel/cpu/sh2a/entry.S	2015-08-24 03:23:58.849386418 +0000
@@ -109,9 +109,9 @@ ENTRY(exception_handler)
 	mov	#64,r8
 	cmp/hs	r8,r9
 	bt	interrupt_entry	! vec >= 64 is interrupt
-	mov	#32,r8
+	mov	#31,r8
 	cmp/hs	r8,r9
-	bt	trap_entry	! 64 > vec >= 32  is trap
+	bt	trap_entry	! 64 > vec >= 31  is trap
 
 	mov.l	4f,r8
 	mov	r9,r4
@@ -143,9 +143,9 @@ interrupt_entry:
 
 trap_entry:
 	mov	#0x30,r8
-	cmp/ge	r8,r9		! vector 0x20-0x2f is systemcall
+	cmp/ge	r8,r9		! vector 0x1f-0x2f is systemcall
 	bt	1f
-	add	#-0x10,r9	! convert SH2 to SH3/4 ABI
+	mov     #0x1f,r9	! convert to unified SH2/3/4 trap number
 1:	
 	shll2	r9			! TRA
 	bra	system_call	! jump common systemcall entry
diff -urp ../linux-4.2-rc6.orig/arch/sh/kernel/entry-common.S ./arch/sh/kernel/entry-common.S
--- ../linux-4.2-rc6.orig/arch/sh/kernel/entry-common.S	2015-08-09 19:54:30.000000000 +0000
+++ ./arch/sh/kernel/entry-common.S	2015-08-25 21:08:15.835463227 +0000
@@ -268,20 +268,29 @@ debug_trap:
  *	Syscall #: R3
  *	Arguments #0 to #3: R4--R7
  *	Arguments #4 to #6: R0, R1, R2
- *	TRA: (number of arguments + ABI revision) x 4
+ *	TRA: See following table.
  *
- * This code also handles delegating other traps to the BIOS/gdb stub
- * according to:
- *
- * Trap number
  * (TRA>>2)	Purpose
  * --------	-------
  * 0x00-0x0f	original SH-3/4 syscall ABI (not in general use).
  * 0x10-0x1f	general SH-3/4 syscall ABI.
- * 0x20-0x2f	syscall ABI for SH-2 parts.
+ *      0x1f	unified SH-2/3/4 syscall ABI (preferred).
+ * 0x20-0x2f	original SH-2 syscall ABI.
  * 0x30-0x3f	debug traps used by the kernel.
  * 0x40-0xff	Not supported by all parts, so left unhandled.
  *
+ * For making system calls, any trap number in the range for the
+ * given cpu model may be used, but the unified trap number 0x1f is
+ * preferred for compatibility with all models.
+ *
+ * The low bits of the trap number were once documented as matching
+ * the number of arguments, but they were never actually used as such
+ * by the kernel. SH-2 originally used its own separate trap range
+ * because several hardware exceptions fell in the range used for the
+ * SH-3/4 syscall ABI.
+ *
+ * This code also handles delegating other traps to the BIOS/gdb stub.
+ *
  * Note: When we're first called, the TRA value must be shifted
  * right 2 bits in order to get the value that was used as the "trapa"
  * argument.

[-- Attachment #3: fdpic_elf_loader.diff --]
[-- Type: text/plain, Size: 1969 bytes --]

--- fs/binfmt_elf_fdpic.c.orig
+++ fs/binfmt_elf_fdpic.c
@@ -103,14 +103,27 @@
 core_initcall(init_elf_fdpic_binfmt);
 module_exit(exit_elf_fdpic_binfmt);
 
+static int is_fdpic(struct elfhdr *hdr)
+{
+#ifdef CONFIG_MMU
+	return 1;
+#else
+	return elf_check_fdpic(hdr);
+#endif
+}
+
 static int is_elf_fdpic(struct elfhdr *hdr, struct file *file)
 {
 	if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0)
 		return 0;
 	if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN)
 		return 0;
-	if (!elf_check_arch(hdr) || !elf_check_fdpic(hdr))
+	if (!elf_check_arch(hdr))
 		return 0;
+#ifdef CONFIG_MMU
+	if (!elf_check_fdpic(hdr))
+		return 0;
+#endif
 	if (!file->f_op->mmap)
 		return 0;
 	return 1;
@@ -269,7 +282,7 @@
 
 	}
 
-	if (elf_check_const_displacement(&exec_params.hdr))
+	if (elf_check_const_displacement(&exec_params.hdr) || !is_fdpic(&exec_params.hdr))
 		exec_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 
 	/* perform insanity checks on the interpreter */
@@ -306,9 +319,9 @@
 
 	retval = -ENOEXEC;
 	if (stack_size == 0)
-		goto error;
+		stack_size = 131072;
 
-	if (elf_check_const_displacement(&interp_params.hdr))
+	if (elf_check_const_displacement(&interp_params.hdr) || !is_fdpic(&interp_params.hdr))
 		interp_params.flags |= ELF_FDPIC_FLAG_CONSTDISP;
 
 	/* flush all traces of the currently running executable */
@@ -319,7 +332,8 @@
 	/* there's now no turning back... the old userspace image is dead,
 	 * defunct, deceased, etc.
 	 */
-	set_personality(PER_LINUX_FDPIC);
+	if (is_fdpic(&exec_params.hdr))
+		set_personality(PER_LINUX_FDPIC);
 	if (elf_read_implies_exec(&exec_params.hdr, executable_stack))
 		current->personality |= READ_IMPLIES_EXEC;
 
@@ -400,8 +414,6 @@
 
 	current->mm->brk = current->mm->start_brk;
 	current->mm->context.end_brk = current->mm->start_brk;
-	current->mm->context.end_brk +=
-		(stack_size > PAGE_SIZE) ? (stack_size - PAGE_SIZE) : 0;
 	current->mm->start_stack = current->mm->start_brk + stack_size;
 #endif
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-08-26 18:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-26 17:42 musl-based toolchain for J2/SH-2 Rich Felker
2015-08-26 18:08 ` musl-based toolchain for J2/SH-2 - kernel patches Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).