| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * This file defines various POSIX-like functions directly using Linux | 8 * This file defines various POSIX-like functions directly using Linux |
| 9 * syscalls. This is analogous to src/untrusted/nacl/sys_private.c, which | 9 * syscalls. This is analogous to src/untrusted/nacl/sys_private.c, which |
| 10 * defines functions using NaCl syscalls directly. | 10 * defines functions using NaCl syscalls directly. |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 | 628 |
| 629 int linux_sigprocmask(int how, | 629 int linux_sigprocmask(int how, |
| 630 const linux_sigset_t *set, | 630 const linux_sigset_t *set, |
| 631 linux_sigset_t *oset) { | 631 linux_sigset_t *oset) { |
| 632 return errno_value_call( | 632 return errno_value_call( |
| 633 linux_syscall4(__NR_rt_sigprocmask, how, | 633 linux_syscall4(__NR_rt_sigprocmask, how, |
| 634 (uintptr_t) set, (uintptr_t) oset, | 634 (uintptr_t) set, (uintptr_t) oset, |
| 635 sizeof(*set))); | 635 sizeof(*set))); |
| 636 } | 636 } |
| 637 | 637 |
| 638 int linux_tgkill(int tgid, int tid, int sig) { | |
| 639 return errno_value_call( | |
| 640 linux_syscall3(__NR_tgkill, tgid, tid, sig)); | |
| 641 } | |
| 642 | |
| 643 /* | 638 /* |
| 644 * Obtain Linux signal number from portable signal number. | 639 * Obtain Linux signal number from portable signal number. |
| 645 */ | 640 */ |
| 646 static int nacl_signum_to_linux_signum(int signum) { | 641 static int nacl_signum_to_linux_signum(int signum) { |
| 647 /* SIGSTKFLT is not defined in newlib, hence no mapping. */ | 642 /* SIGSTKFLT is not defined in newlib, hence no mapping. */ |
| 648 #define HANDLE_SIGNUM(SIGNUM) case SIGNUM: return LINUX_##SIGNUM; | 643 #define HANDLE_SIGNUM(SIGNUM) case SIGNUM: return LINUX_##SIGNUM; |
| 649 switch(signum) { | 644 switch(signum) { |
| 650 HANDLE_SIGNUM(SIGHUP); | 645 HANDLE_SIGNUM(SIGHUP); |
| 651 HANDLE_SIGNUM(SIGINT); | 646 HANDLE_SIGNUM(SIGINT); |
| 652 HANDLE_SIGNUM(SIGQUIT); | 647 HANDLE_SIGNUM(SIGQUIT); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 /* | 763 /* |
| 769 * This function is called only from clone() below or | 764 * This function is called only from clone() below or |
| 770 * nacl_irt_thread_create() defined in linux_pthread_private.c. | 765 * nacl_irt_thread_create() defined in linux_pthread_private.c. |
| 771 * In both cases, |child_stack| will never be NULL, although it is allowed | 766 * In both cases, |child_stack| will never be NULL, although it is allowed |
| 772 * for direct clone() syscall. So, we skip that case's implementation for | 767 * for direct clone() syscall. So, we skip that case's implementation for |
| 773 * simplicity here. | 768 * simplicity here. |
| 774 * | 769 * |
| 775 * Here we reserve 6 * 4 bytes for three purposes described below: | 770 * Here we reserve 6 * 4 bytes for three purposes described below: |
| 776 * 1) At the beginning of the child process, we call fn(arg). To pass | 771 * 1) At the beginning of the child process, we call fn(arg). To pass |
| 777 * the function pointer and arguments, we use |stack| for |arg|, | 772 * the function pointer and arguments, we use |stack| for |arg|, |
| 778 * |stack + 4| for |fn|. | 773 * |stack - 4| for |fn|. Here, we need 4-byte extra memory on top of |
| 774 * stack for |arg|. |
| 779 * 2) Our syscall() implementation reads six 4-byte arguments regardless | 775 * 2) Our syscall() implementation reads six 4-byte arguments regardless |
| 780 * of its actual arguments. | 776 * of its actual arguments. |
| 781 * 3) Similar to 2), our clone() implementation reads three 4-byte arguments | 777 * 3) Similar to 2), our clone() implementation reads three 4-byte arguments |
| 782 * regardless of its actual arguments. | 778 * regardless of its actual arguments. |
| 783 * So, here we need max size of those three cases (= 6 * 4 bytes) on top of | 779 * So, here we need max size of those three cases (= 6 * 4 bytes) on top of |
| 784 * the stack, with 16-byte alignment. | 780 * the stack, with 16-byte alignment. |
| 785 */ | 781 */ |
| 786 static const int kStackAlignmentMask = ~15; | 782 static const int kStackAlignmentMask = ~15; |
| 787 void *stack = (void *) (((uintptr_t) child_stack - sizeof(uintptr_t) * 6) & | 783 void *stack = (void *) (((uintptr_t) child_stack - sizeof(uintptr_t) * 6) & |
| 788 kStackAlignmentMask); | 784 kStackAlignmentMask); |
| 789 /* Put |fn| and |arg| on child process's stack. */ | 785 /* Put |fn| and |arg| on child process's stack. */ |
| 786 ((uintptr_t *) stack)[-1] = fn; |
| 790 ((uintptr_t *) stack)[0] = arg; | 787 ((uintptr_t *) stack)[0] = arg; |
| 791 ((uintptr_t *) stack)[1] = fn; | |
| 792 | 788 |
| 793 #if defined(__i386__) | 789 #if defined(__i386__) |
| 794 uint32_t result; | 790 uint32_t result; |
| 795 __asm__ __volatile__("int $0x80\n" | 791 __asm__ __volatile__("int $0x80\n" |
| 796 /* | 792 /* |
| 797 * If the return value of clone is non-zero, we are | 793 * If the return value of clone is non-zero, we are |
| 798 * in the parent thread of clone. | 794 * in the parent thread of clone. |
| 799 */ | 795 */ |
| 800 "cmp $0, %%eax\n" | 796 "cmp $0, %%eax\n" |
| 801 "jne 0f\n" | 797 "jne 0f\n" |
| 802 /* | 798 /* |
| 803 * In child thread. Clear the frame pointer to | 799 * In child thread. Clear the frame pointer to |
| 804 * prevent debuggers from unwinding beyond this. | 800 * prevent debuggers from unwinding beyond this. |
| 805 */ | 801 */ |
| 806 "mov $0, %%ebp\n" | 802 "mov $0, %%ebp\n" |
| 807 /* | 803 /* |
| 808 * Call fn(arg). Note that |arg| is already ready on top | 804 * Call fn(arg). Note that |arg| is already ready on top |
| 809 * of the stack, here. | 805 * of the stack, here. |
| 810 */ | 806 */ |
| 811 "call *4(%%esp)\n" | 807 "call *-4(%%esp)\n" |
| 812 /* Then call _exit(2) with the return value. */ | 808 /* Then call _exit(2) with the return value. */ |
| 813 "mov %%eax, %%ebx\n" | 809 "mov %%eax, %%ebx\n" |
| 814 "mov %[exit_sysno], %%eax\n" | 810 "mov %[exit_sysno], %%eax\n" |
| 815 "int $0x80\n" | 811 "int $0x80\n" |
| 816 /* _exit(2) will never return. */ | 812 /* _exit(2) will never return. */ |
| 817 "hlt\n" | 813 "hlt\n" |
| 818 "0:\n" | 814 "0:\n" |
| 819 : "=a"(result) | 815 : "=a"(result) |
| 820 : "a"(__NR_clone), "b"(flags), "c"(stack), | 816 : "a"(__NR_clone), "b"(flags), "c"(stack), |
| 821 "d"(ptid), "S"(&desc), "D"(ctid), | 817 "d"(ptid), "S"(&desc), "D"(ctid), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 837 "cmp r0, #0\n" | 833 "cmp r0, #0\n" |
| 838 "bne 0f\n" | 834 "bne 0f\n" |
| 839 /* | 835 /* |
| 840 * In child thread. Clear the frame pointer to | 836 * In child thread. Clear the frame pointer to |
| 841 * prevent debuggers from unwinding beyond this, | 837 * prevent debuggers from unwinding beyond this, |
| 842 * load start_func from the stack and call it. | 838 * load start_func from the stack and call it. |
| 843 */ | 839 */ |
| 844 "mov fp, #0\n" | 840 "mov fp, #0\n" |
| 845 /* Load |arg| to r0 register, then call |fn|. */ | 841 /* Load |arg| to r0 register, then call |fn|. */ |
| 846 "ldr r0, [sp]\n" | 842 "ldr r0, [sp]\n" |
| 847 "ldr r1, [sp, #4]\n" | 843 "ldr r1, [sp, #-4]\n" |
| 848 "blx r1\n" | 844 "blx r1\n" |
| 849 /* | 845 /* |
| 850 * Then, call _exit(2) with the returned value. | 846 * Then, call _exit(2) with the returned value. |
| 851 * r0 keeps the return value of |fn(arg)|. | 847 * r0 keeps the return value of |fn(arg)|. |
| 852 */ | 848 */ |
| 853 "mov r7, %[exit_sysno]\n" | 849 "mov r7, %[exit_sysno]\n" |
| 854 "svc #0\n" | 850 "svc #0\n" |
| 855 /* _exit(2) will never return. */ | 851 /* _exit(2) will never return. */ |
| 856 "bkpt #0\n" | 852 "bkpt #0\n" |
| 857 "0:\n" | 853 "0:\n" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 880 va_list ap; | 876 va_list ap; |
| 881 va_start(ap, arg); | 877 va_start(ap, arg); |
| 882 void *ptid = va_arg(ap, void *); | 878 void *ptid = va_arg(ap, void *); |
| 883 void *tls = va_arg(ap, void *); | 879 void *tls = va_arg(ap, void *); |
| 884 void *ctid = va_arg(ap, void *); | 880 void *ctid = va_arg(ap, void *); |
| 885 va_end(ap); | 881 va_end(ap); |
| 886 | 882 |
| 887 return errno_value_call(linux_clone_wrapper( | 883 return errno_value_call(linux_clone_wrapper( |
| 888 (uintptr_t) fn, (uintptr_t) arg, flags, child_stack, ptid, tls, ctid)); | 884 (uintptr_t) fn, (uintptr_t) arg, flags, child_stack, ptid, tls, ctid)); |
| 889 } | 885 } |
| OLD | NEW |