1 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19 
20 #include <tls.h>
21 #include <pt-machine.h>
22 #ifndef __ASSEMBLER__
23 # include <internals.h>
24 #endif
25 
26 #if !defined NOT_IN_libc || defined IS_IN_libpthread || defined IS_IN_librt
27 
28 # undef PSEUDO
29 # define PSEUDO(name, syscall_name, args)				      \
30   .text;								      \
31   ENTRY (name)								      \
32     SINGLE_THREAD_P;							      \
33     jne L(pseudo_cancel);						      \
34     DO_CALL (syscall_name, args);					      \
35     cmpq $-4095, %rax;							      \
36     jae SYSCALL_ERROR_LABEL;						      \
37     ret;								      \
38   L(pseudo_cancel):							      \
39     /* Save registers that might get destroyed.  */			      \
40     SAVESTK_##args							      \
41     PUSHARGS_##args							      \
42     CENABLE								      \
43     /* Restore registers.  */						      \
44     POPARGS_##args							      \
45     /* The return value from CENABLE is argument for CDISABLE.  */	      \
46     movq %rax, (%rsp);							      \
47     movl $SYS_ify (syscall_name), %eax;					      \
48     syscall;								      \
49     movq (%rsp), %rdi;							      \
50     /* Save %rax since it's the error code from the syscall.  */	      \
51     movq %rax, 8(%rsp);							      \
52     CDISABLE								      \
53     movq 8(%rsp), %rax;							      \
54     RESTSTK_##args							      \
55     cmpq $-4095, %rax;							      \
56     jae SYSCALL_ERROR_LABEL;						      \
57   L(pseudo_end):
58 
59 # define PUSHARGS_0	/* Nothing.  */
60 # define PUSHARGS_1	PUSHARGS_0 movq %rdi, 8(%rsp);
61 # define PUSHARGS_2	PUSHARGS_1 movq %rsi, 16(%rsp);
62 # define PUSHARGS_3	PUSHARGS_2 movq %rdx, 24(%rsp);
63 # define PUSHARGS_4	PUSHARGS_3 movq %rcx, 32(%rsp);
64 # define PUSHARGS_5	PUSHARGS_4 movq %r8, 40(%rsp);
65 # define PUSHARGS_6	PUSHARGS_5 movq %r9, 48(%rsp);
66 
67 # define POPARGS_0	/* Nothing.  */
68 # define POPARGS_1	POPARGS_0 movq 8(%rsp), %rdi;
69 # define POPARGS_2	POPARGS_1 movq 16(%rsp), %rsi;
70 # define POPARGS_3	POPARGS_2 movq 24(%rsp), %rdx;
71 # define POPARGS_4	POPARGS_3 movq 32(%rsp), %r10;
72 # define POPARGS_5	POPARGS_4 movq 40(%rsp), %r8;
73 # define POPARGS_6	POPARGS_5 movq 48(%rsp), %r9;
74 
75 /* We always have to align the stack before calling a function.  */
76 # define SAVESTK_0	subq $24, %rsp;cfi_adjust_cfa_offset(24);
77 # define SAVESTK_1	SAVESTK_0
78 # define SAVESTK_2	SAVESTK_1
79 # define SAVESTK_3	subq $40, %rsp;cfi_adjust_cfa_offset(40);
80 # define SAVESTK_4	SAVESTK_3
81 # define SAVESTK_5	subq $56, %rsp;cfi_adjust_cfa_offset(56);
82 # define SAVESTK_6	SAVESTK_5
83 
84 # define RESTSTK_0	addq $24,%rsp;cfi_adjust_cfa_offset(-24);
85 # define RESTSTK_1	RESTSTK_0
86 # define RESTSTK_2	RESTSTK_1
87 # define RESTSTK_3	addq $40, %rsp;cfi_adjust_cfa_offset(-40);
88 # define RESTSTK_4	RESTSTK_3
89 # define RESTSTK_5	addq $56, %rsp;cfi_adjust_cfa_offset(-56);
90 # define RESTSTK_6	RESTSTK_5
91 
92 # ifdef IS_IN_libpthread
93 #  define CENABLE	call __pthread_enable_asynccancel;
94 #  define CDISABLE	call __pthread_disable_asynccancel;
95 #  define __local_multiple_threads __pthread_multiple_threads
96 # elif !defined NOT_IN_libc
97 #  define CENABLE	call __libc_enable_asynccancel;
98 #  define CDISABLE	call __libc_disable_asynccancel;
99 #  define __local_multiple_threads __libc_multiple_threads
100 # else
101 #  define CENABLE	call __librt_enable_asynccancel@plt;
102 #  define CDISABLE	call __librt_disable_asynccancel@plt;
103 # endif
104 
105 # if defined IS_IN_libpthread || !defined NOT_IN_libc
106 #  ifndef __ASSEMBLER__
107 extern int __local_multiple_threads attribute_hidden;
108 #   define SINGLE_THREAD_P \
109   __builtin_expect (__local_multiple_threads == 0, 1)
110 #  else
111 #   define SINGLE_THREAD_P cmpl $0, __local_multiple_threads(%rip)
112 #  endif
113 
114 # else
115 
116 #  ifndef __ASSEMBLER__
117 #   define SINGLE_THREAD_P \
118   __builtin_expect (THREAD_GETMEM (THREAD_SELF, \
119 				   p_header.data.multiple_threads) == 0, 1)
120 #  else
121 #   define SINGLE_THREAD_P cmpl $0, %fs:MULTIPLE_THREADS_OFFSET
122 #  endif
123 
124 # endif
125 
126 #elif !defined __ASSEMBLER__
127 
128 /* This code should never be used but we define it anyhow.  */
129 # define SINGLE_THREAD_P (1)
130 
131 #endif
132