From e0cbe28711e1c92d87724f425304dfe3d436bc5a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 23 Feb 2024 18:41:39 -0800 Subject: [PATCH] Add the generic syscall function --- native/src/crt0/Android.mk | 1 + native/src/crt0/nolibc.c | 5 + native/src/crt0/syscall/private/bionic_asm.h | 115 ++++++++++++++++++ .../src/crt0/syscall/private/bionic_asm_arm.h | 48 ++++++++ .../crt0/syscall/private/bionic_asm_arm64.h | 79 ++++++++++++ .../crt0/syscall/private/bionic_asm_riscv64.h | 43 +++++++ .../src/crt0/syscall/private/bionic_asm_x86.h | 51 ++++++++ .../crt0/syscall/private/bionic_asm_x86_64.h | 42 +++++++ native/src/crt0/syscall/syscall-arm.S | 51 ++++++++ native/src/crt0/syscall/syscall-arm64.S | 51 ++++++++ native/src/crt0/syscall/syscall-x86.S | 73 +++++++++++ native/src/crt0/syscall/syscall-x86_64.S | 63 ++++++++++ 12 files changed, 622 insertions(+) create mode 100644 native/src/crt0/syscall/private/bionic_asm.h create mode 100644 native/src/crt0/syscall/private/bionic_asm_arm.h create mode 100644 native/src/crt0/syscall/private/bionic_asm_arm64.h create mode 100644 native/src/crt0/syscall/private/bionic_asm_riscv64.h create mode 100644 native/src/crt0/syscall/private/bionic_asm_x86.h create mode 100644 native/src/crt0/syscall/private/bionic_asm_x86_64.h create mode 100644 native/src/crt0/syscall/syscall-arm.S create mode 100644 native/src/crt0/syscall/syscall-arm64.S create mode 100644 native/src/crt0/syscall/syscall-x86.S create mode 100644 native/src/crt0/syscall/syscall-x86_64.S diff --git a/native/src/crt0/Android.mk b/native/src/crt0/Android.mk index b64d8f747..3aeccda0a 100644 --- a/native/src/crt0/Android.mk +++ b/native/src/crt0/Android.mk @@ -13,6 +13,7 @@ LOCAL_SRC_FILES := \ nolibc.c \ stdio.c \ syscall.c \ + syscall/syscall-$(TARGET_ARCH).S \ tinystdio/tinystdio.c include $(BUILD_STATIC_LIBRARY) diff --git a/native/src/crt0/nolibc.c b/native/src/crt0/nolibc.c index a4d8c5622..d220227ca 100644 --- a/native/src/crt0/nolibc.c +++ b/native/src/crt0/nolibc.c @@ -9,3 +9,8 @@ static int g_errno = 0; int *__errno(void) { return &g_errno; } + +long __set_errno_internal(int n) { + g_errno = n; + return -1; +} diff --git a/native/src/crt0/syscall/private/bionic_asm.h b/native/src/crt0/syscall/private/bionic_asm.h new file mode 100644 index 000000000..333f4fa3c --- /dev/null +++ b/native/src/crt0/syscall/private/bionic_asm.h @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#pragma once + +#define __ASSEMBLY__ + +/* https://github.com/android/ndk/issues/1422 */ +#include + +#include /* For system call numbers. */ +#define MAX_ERRNO 4095 /* For recognizing system call error returns. */ + +#define __bionic_asm_custom_entry(f) +#define __bionic_asm_custom_end(f) +#define __bionic_asm_function_type @function +#define __bionic_asm_custom_note_gnu_section() + +#if defined(__aarch64__) +#include "bionic_asm_arm64.h" +#elif defined(__arm__) +#include "bionic_asm_arm.h" +#elif defined(__i386__) +#include "bionic_asm_x86.h" +#elif defined(__riscv) +#include "bionic_asm_riscv64.h" +#elif defined(__x86_64__) +#include "bionic_asm_x86_64.h" +#endif + +// Starts a normal assembler routine. +#define ENTRY(__f) __ENTRY_WITH_BINDING(__f, .globl) + +// Starts an assembler routine with hidden visibility. +#define ENTRY_PRIVATE(__f) \ + __ENTRY_WITH_BINDING(__f, .globl); \ + .hidden __f; + +// Starts an assembler routine that's weak so native bridges can override it. +#define ENTRY_WEAK_FOR_NATIVE_BRIDGE(__f) __ENTRY_WITH_BINDING(__f, .weak) + +// Starts an assembler routine with hidden visibility and no DWARF information. +// Only used for internal functions passed via sa_restorer. +// TODO: can't we just delete all those and let the kernel do its thing? +#define ENTRY_NO_DWARF_PRIVATE(__f) \ + __ENTRY_NO_DWARF(__f, .globl); \ + .hidden __f; + +// (Implementation detail.) +#define __ENTRY_NO_DWARF(__f, __binding) \ + .text; \ + __binding __f; \ + .balign __bionic_asm_align; \ + .type __f, __bionic_asm_function_type; \ + __f: \ + __bionic_asm_custom_entry(__f); + +// (Implementation detail.) +#define __ENTRY_WITH_BINDING(__f, __binding) \ + __ENTRY_NO_DWARF(__f, __binding); \ + .cfi_startproc; + +// Ends a normal assembler routine. +#define END(__f) \ + .cfi_endproc; \ + END_NO_DWARF(__f) + +// Ends an assembler routine with no DWARF information. +// Only used for internal functions passed via sa_restorer. +// TODO: can't we just delete all those and let the kernel do its thing? +#define END_NO_DWARF(__f) \ + .size __f, .- __f; \ + __bionic_asm_custom_end(__f) + +// Creates an alias `alias` for the symbol `original`. +#define ALIAS_SYMBOL(alias, original) \ + .globl alias; \ + .equ alias, original + +// Creates an alias `alias` for the symbol `original` that's weak so it can be +// separately overridden by native bridges. +#define ALIAS_SYMBOL_WEAK_FOR_NATIVE_BRIDGE(alias, original) \ + .weak alias; \ + .equ alias, original + +// Adds a GNU property ELF note. Important on arm64 to declare PAC/BTI support. +#define NOTE_GNU_PROPERTY() __bionic_asm_custom_note_gnu_section() + +// Gives local labels a more convenient and readable syntax. +#define L(__label) .L##__label diff --git a/native/src/crt0/syscall/private/bionic_asm_arm.h b/native/src/crt0/syscall/private/bionic_asm_arm.h new file mode 100644 index 000000000..d8381d32b --- /dev/null +++ b/native/src/crt0/syscall/private/bionic_asm_arm.h @@ -0,0 +1,48 @@ +/* $OpenBSD: asm.h,v 1.1 2004/02/01 05:09:49 drahn Exp $ */ +/* $NetBSD: asm.h,v 1.4 2001/07/16 05:43:32 matt Exp $ */ + +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)asm.h 5.5 (Berkeley) 5/7/91 + */ + +#pragma once + +#define __bionic_asm_align 0 + +#undef __bionic_asm_custom_entry +#undef __bionic_asm_custom_end +#define __bionic_asm_custom_entry(f) .fnstart +#define __bionic_asm_custom_end(f) .fnend + +#undef __bionic_asm_function_type +#define __bionic_asm_function_type #function diff --git a/native/src/crt0/syscall/private/bionic_asm_arm64.h b/native/src/crt0/syscall/private/bionic_asm_arm64.h new file mode 100644 index 000000000..ffc718144 --- /dev/null +++ b/native/src/crt0/syscall/private/bionic_asm_arm64.h @@ -0,0 +1,79 @@ +/* $OpenBSD: asm.h,v 1.1 2004/02/01 05:09:49 drahn Exp $ */ +/* $NetBSD: asm.h,v 1.4 2001/07/16 05:43:32 matt Exp $ */ + +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)asm.h 5.5 (Berkeley) 5/7/91 + */ + +#pragma once + +#define __bionic_asm_align 16 + +#undef __bionic_asm_function_type +#define __bionic_asm_function_type %function + +#if defined(__ARM_FEATURE_BTI_DEFAULT) +#define __bionic_asm_aarch64_feature_bti (1 << 0) +#undef __bionic_asm_custom_entry +#define __bionic_asm_custom_entry(f) bti c +#else +#define __bionic_asm_aarch64_feature_bti 0 +#endif + +#if defined(__ARM_FEATURE_PAC_DEFAULT) +#define __bionic_asm_aarch64_feature_pac (1 << 1) +#else +#define __bionic_asm_aarch64_feature_pac 0 +#endif + +#undef __bionic_asm_custom_note_gnu_section +#define __bionic_asm_custom_note_gnu_section() \ + .pushsection .note.gnu.property, "a"; \ + .balign 8; \ + .long 4; \ + .long 0x10; \ + .long 0x5; /* NT_GNU_PROPERTY_TYPE_0 */ \ + .asciz "GNU"; \ + .long 0xc0000000; /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */ \ + .long 4; \ + .long (__bionic_asm_aarch64_feature_pac | \ + __bionic_asm_aarch64_feature_bti); \ + .long 0; \ + .popsection; + +#define NT_MEMTAG_LEVEL_MASK 3 +#define NT_MEMTAG_LEVEL_NONE 0 +#define NT_MEMTAG_LEVEL_ASYNC 1 +#define NT_MEMTAG_LEVEL_SYNC 2 +#define NT_MEMTAG_HEAP 4 +#define NT_MEMTAG_STACK 8 diff --git a/native/src/crt0/syscall/private/bionic_asm_riscv64.h b/native/src/crt0/syscall/private/bionic_asm_riscv64.h new file mode 100644 index 000000000..463ca312a --- /dev/null +++ b/native/src/crt0/syscall/private/bionic_asm_riscv64.h @@ -0,0 +1,43 @@ +/* $OpenBSD: asm.h,v 1.1 2004/02/01 05:09:49 drahn Exp $ */ +/* $NetBSD: asm.h,v 1.4 2001/07/16 05:43:32 matt Exp $ */ + +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * from: @(#)asm.h 5.5 (Berkeley) 5/7/91 + */ + +#pragma once + +#define __bionic_asm_align 16 + +#undef __bionic_asm_function_type +#define __bionic_asm_function_type %function diff --git a/native/src/crt0/syscall/private/bionic_asm_x86.h b/native/src/crt0/syscall/private/bionic_asm_x86.h new file mode 100644 index 000000000..fcec40b5e --- /dev/null +++ b/native/src/crt0/syscall/private/bionic_asm_x86.h @@ -0,0 +1,51 @@ +/* $NetBSD: asm.h,v 1.40 2011/06/16 13:16:20 joerg Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)asm.h 5.5 (Berkeley) 5/7/91 + */ + +#pragma once + +#define PIC_PROLOGUE \ + pushl %ebx; \ + call 666f; \ +666: \ + popl %ebx; \ + addl $_GLOBAL_OFFSET_TABLE_+[.-666b], %ebx +#define PIC_EPILOGUE \ + popl %ebx +#define PIC_PLT(x) x@PLT +#define PIC_GOT(x) x@GOT(%ebx) +#define PIC_GOTOFF(x) x@GOTOFF(%ebx) + +#define __bionic_asm_align 16 diff --git a/native/src/crt0/syscall/private/bionic_asm_x86_64.h b/native/src/crt0/syscall/private/bionic_asm_x86_64.h new file mode 100644 index 000000000..c553b0c9c --- /dev/null +++ b/native/src/crt0/syscall/private/bionic_asm_x86_64.h @@ -0,0 +1,42 @@ +/* $NetBSD: asm.h,v 1.18 2013/09/12 15:36:17 joerg Exp $ */ + +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * William Jolitz. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)asm.h 5.5 (Berkeley) 5/7/91 + */ + +#pragma once + +#define PIC_PLT(x) x@PLT +#define PIC_GOT(x) x@GOTPCREL(%rip) + +#define __bionic_asm_align 16 diff --git a/native/src/crt0/syscall/syscall-arm.S b/native/src/crt0/syscall/syscall-arm.S new file mode 100644 index 000000000..8b603835a --- /dev/null +++ b/native/src/crt0/syscall/syscall-arm.S @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "private/bionic_asm.h" + +ENTRY(syscall) + mov ip, sp + stmfd sp!, {r4, r5, r6, r7} + .cfi_def_cfa_offset 16 + .cfi_rel_offset r4, 0 + .cfi_rel_offset r5, 4 + .cfi_rel_offset r6, 8 + .cfi_rel_offset r7, 12 + mov r7, r0 + mov r0, r1 + mov r1, r2 + mov r2, r3 + ldmfd ip, {r3, r4, r5, r6} + swi #0 + ldmfd sp!, {r4, r5, r6, r7} + .cfi_def_cfa_offset 0 + cmn r0, #(MAX_ERRNO + 1) + bxls lr + neg r0, r0 + b __set_errno_internal +END(syscall) diff --git a/native/src/crt0/syscall/syscall-arm64.S b/native/src/crt0/syscall/syscall-arm64.S new file mode 100644 index 000000000..acd2b4205 --- /dev/null +++ b/native/src/crt0/syscall/syscall-arm64.S @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "private/bionic_asm.h" + +ENTRY(syscall) + /* Move syscall No. from x0 to x8 */ + mov x8, x0 + /* Move syscall parameters from x1 thru x6 to x0 thru x5 */ + mov x0, x1 + mov x1, x2 + mov x2, x3 + mov x3, x4 + mov x4, x5 + mov x5, x6 + svc #0 + + /* check if syscall returned successfully */ + cmn x0, #(MAX_ERRNO + 1) + cneg x0, x0, hi + b.hi __set_errno_internal + + ret +END(syscall) + +NOTE_GNU_PROPERTY() diff --git a/native/src/crt0/syscall/syscall-x86.S b/native/src/crt0/syscall/syscall-x86.S new file mode 100644 index 000000000..bc3fb2a73 --- /dev/null +++ b/native/src/crt0/syscall/syscall-x86.S @@ -0,0 +1,73 @@ +/* + * Generic syscall call. + * Upon entry: + * %eax: system call number - caller save + * %ebx: arg0 to system call - callee save + * %ecx: arg1 - caller save + * %edx: arg2 - caller save + * %esi: arg3 - callee save + * %edi: arg4 - callee save + * %ebp: arg5 - callee save + */ + +#include "private/bionic_asm.h" + +ENTRY(syscall) + # Push the callee save registers. + push %ebx + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset ebx, 0 + push %esi + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset esi, 0 + push %edi + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset edi, 0 + push %ebp + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset ebp, 0 + + # Get and save the system call entry address. + call __kernel_syscall + push %eax + .cfi_adjust_cfa_offset 4 + .cfi_rel_offset eax, 0 + + # Load all the arguments from the calling frame. + # (Not all will be valid, depending on the syscall.) + mov 24(%esp),%eax + mov 28(%esp),%ebx + mov 32(%esp),%ecx + mov 36(%esp),%edx + mov 40(%esp),%esi + mov 44(%esp),%edi + mov 48(%esp),%ebp + + # Make the system call. + call *(%esp) + addl $4, %esp + + # Error? + cmpl $-MAX_ERRNO, %eax + jb 1f + # Yes, so set errno. + negl %eax + pushl %eax + call __set_errno_internal + addl $4, %esp +1: + # Restore the callee save registers. + pop %ebp + .cfi_adjust_cfa_offset -4 + .cfi_restore ebp + pop %edi + .cfi_adjust_cfa_offset -4 + .cfi_restore edi + pop %esi + .cfi_adjust_cfa_offset -4 + .cfi_restore esi + pop %ebx + .cfi_adjust_cfa_offset -4 + .cfi_restore ebx + ret +END(syscall) diff --git a/native/src/crt0/syscall/syscall-x86_64.S b/native/src/crt0/syscall/syscall-x86_64.S new file mode 100644 index 000000000..7a6f4104b --- /dev/null +++ b/native/src/crt0/syscall/syscall-x86_64.S @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Generic syscall call. + * Upon entry: + * %rax: system call number + * %rdi: arg0 to system call + * %rsi: arg1 + * %rdx: arg2 + * %rcx: arg3 - syscall expects it at %r10 + * %r8: arg4 + * %r9: arg5 + */ + +#include "private/bionic_asm.h" + +ENTRY(syscall) + # All arguments are passed via registers. + # (Not all will be valid, depending on the syscall.) + mov %edi, %eax + mov %rsi, %rdi + mov %rdx, %rsi + mov %rcx, %rdx + mov %r8, %r10 + mov %r9, %r8 + mov 8(%rsp), %r9 + + # Make the system call. + syscall + cmpq $-MAX_ERRNO, %rax + jb 1f + negl %eax + movl %eax, %edi + call __set_errno_internal +1: + ret +END(syscall)