vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / m_syswrap / syscall-x86-darwin.S
blob903242268501060da090b830596fda21ca23ddc9
2 /*--------------------------------------------------------------------*/
3 /*--- Support for doing system calls.         syscall-x86-darwin.S ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7   This file is part of Valgrind, a dynamic binary instrumentation
8   framework.
10   Copyright (C) 2000-2017 Julian Seward 
11      jseward@acm.org
13   This program is free software; you can redistribute it and/or
14   modify it under the terms of the GNU General Public License as
15   published by the Free Software Foundation; either version 2 of the
16   License, or (at your option) any later version.
18   This program is distributed in the hope that it will be useful, but
19   WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   General Public License for more details.
23   You should have received a copy of the GNU General Public License
24   along with this program; if not, see <http://www.gnu.org/licenses/>.
26   The GNU General Public License is contained in the file COPYING.
29 #include "pub_core_basics_asm.h"
31 #if defined(VGP_x86_darwin)
33 #include "pub_core_vkiscnums_asm.h"
34 #include "libvex_guest_offsets.h"
35                 
36         
37 /*----------------------------------------------------------------*/
39         Perform a syscall for the client.  This will run a syscall
40         with the client's specific per-thread signal mask.
41         
42         The structure of this function is such that, if the syscall is
43         interrupted by a signal, we can determine exactly what
44         execution state we were in with respect to the execution of
45         the syscall by examining the value of %eip in the signal
46         handler.  This means that we can always do the appropriate
47         thing to precisely emulate the kernel's signal/syscall
48         interactions.
50         The syscall number is taken from the argument, even though it
51         should also be in regs->m_eax.  The syscall result is written
52         back to regs->m_eax on completion.
53         
54         Returns 0 if the syscall was successfully called (even if the
55         syscall itself failed), or a -ve error code if one of the
56         sigprocmasks failed (there's no way to determine which one
57         failed).
59         VG_(fixup_guest_state_after_syscall_interrupted) does the
60         thread state fixup in the case where we were interrupted by a
61         signal.
62         
63         Prototype:
65         Int ML_(do_syscall_for_client_WRK)(
66                                   Int syscallno,                // ebp+8
67                                   void* guest_state,            // ebp+12
68                                   const vki_sigset_t *sysmask,  // ebp+16
69                                   const vki_sigset_t *postmask, // ebp+20
70                                   Int sigsetSzB)                // ebp+24
72         Note that sigsetSzB is totally ignored (and irrelevant).
75 /* from vki-darwin.h, checked at startup by m_vki.c */  
76 #define VKI_SIG_SETMASK 3
78 /* DO_SYSCALL MACH|MDEP|UNIX */
79 #define MACH 1
80 #define MDEP 2
81 #define UNIX 3
82         
83 .macro DO_SYSCALL
84         /* establish stack frame */
85         push    %ebp
86         mov     %esp, %ebp
87         subl    $$8, %esp       /* 16-byte align stack */
88         
89 L_$0_1: /* Even though we can't take a signal until the
90            __pthread_sigmask completes, start the range early.
91            If eip is in the range [1,2), the syscall hasn't been started yet */
93         /* Set the signal mask which should be current during the syscall. */
94         /* Set up for __pthread_sigmask(SIG_SETMASK, sysmask, postmask) */
95         pushl   20(%ebp)
96         pushl   16(%ebp)
97         pushl   $$VKI_SIG_SETMASK
98         pushl   $$0xcafebabe    /* totally fake return address */
99         movl    $$__NR___pthread_sigmask, %eax
100         int     $$0x80  /* should be sysenter? */
101         jc      L_$0_7  /* __pthread_sigmask failed */
102         addl    $$16,%esp
104         /* Copy syscall parameters to the stack - assume no more than 8 
105          * plus the return address */
106         /* do_syscall8 */
107         /* stack is currently aligned assuming 8 parameters */
108         movl    12(%ebp), %edx                  
109         movl    OFFSET_x86_ESP(%edx), %edx      /* edx = simulated ESP */
110         movl    28+4(%edx), %eax
111         pushl   %eax
112         movl    24+4(%edx), %eax
113         pushl   %eax
114         movl    20+4(%edx), %eax
115         pushl   %eax
116         movl    16+4(%edx), %eax
117         pushl   %eax
118         movl    12+4(%edx), %eax
119         pushl   %eax
120         movl    8+4(%edx), %eax
121         pushl   %eax
122         movl    4+4(%edx), %eax
123         pushl   %eax
124         movl    0+4(%edx), %eax
125         pushl   %eax
126         /* return address */
127         movl    0(%edx), %eax
128         pushl   %eax
130         /* Put syscall number in eax */
131         movl    8(%ebp), %eax
133         /* If eip==2, then the syscall was either just about to start, 
134            or was interrupted and the kernel was restarting it. */
135 L_$0_2:
136 .if $0 == UNIX
137         int     $$0x80          /* UNIX (GrP fixme should be sysenter?) */
138 .elseif $0 == MACH
139         int     $$0x81
140 .elseif $0 == MDEP
141         int     $$0x82
142 .else
143         error$0 x
144 .endif
146 L_$0_3: /* In the range [3, 4), the syscall result is in %eax and %edx and C,
147            but hasn't been committed to the thread state. */
148         setc    0(%esp)                         /* stash returned carry flag */
149         movl    12(%ebp), %ecx
150         movl    %eax, OFFSET_x86_EAX(%ecx)      /* save EAX to vex */
151         movl    %edx, OFFSET_x86_EDX(%ecx)      /* save EDX to vex */
152 .if $0 == UNIX
153         /* UNIX: save carry flag to vex */
154         subl    $$12, %esp
155         movl    %ecx, 4(%esp)
156         movl    $$0, 0(%esp)
157         movb    12(%esp), %al
158         movb    %al, 0(%esp)
159         call    _LibVEX_GuestX86_put_eflag_c
160         addl    $$12, %esp
161 .endif
163 L_$0_4: /* Re-block signals.  If eip is in [4,5), then the syscall is
164            complete and we needn't worry about it. */
165         /* Set up for __pthread_sigmask(SIG_SETMASK, postmask, NULL) */
166         pushl   $$0
167         pushl   20(%ebp)
168         pushl   $$VKI_SIG_SETMASK
169         pushl   $$0xcafef00d    /* totally fake return address */
170         movl    $$__NR___pthread_sigmask, %eax
171         int     $$0x80  /* should be sysenter? */
172         jc      L_$0_7  /* __pthread_sigmask failed */
173         addl    $$16,%esp
175 L_$0_5: /* now safe from signals */
176         movl    $$0, %eax       /* SUCCESS */
177         movl    %ebp, %esp
178         popl    %ebp
179         ret
181 L_$0_7: /* failure: return 0x8000 | error code */
182         /* Note that we enter here with %esp being 16 too low
183            (4 extra words on the stack).  But because we're nuking
184            the stack frame now, that doesn't matter. */
185         andl    $$0x7FFF, %eax
186         orl     $$0x8000, %eax
187         movl    %ebp, %esp
188         popl    %ebp
189         ret
191 .endmacro
192         
194 .globl ML_(do_syscall_for_client_unix_WRK)
195 ML_(do_syscall_for_client_unix_WRK):
196         DO_SYSCALL UNIX
198 .globl ML_(do_syscall_for_client_mach_WRK)
199 ML_(do_syscall_for_client_mach_WRK):
200         DO_SYSCALL MACH
201         
202 .globl ML_(do_syscall_for_client_mdep_WRK)
203 ML_(do_syscall_for_client_mdep_WRK):
204         DO_SYSCALL MDEP
206 .data
207 /* export the ranges so that
208    VG_(fixup_guest_state_after_syscall_interrupted) can do the
209    right thing */
211 /* eg MK_L_SCLASS_N(UNIX,99) produces L_3_99
212    since UNIX is #defined to 3 at the top of this file */
213 #define FOO(scclass,labelno) L_##scclass##_##labelno
214 #define MK_L_SCCLASS_N(scclass,labelno) FOO(scclass,labelno)
216 .globl ML_(blksys_setup_MACH)
217 .globl ML_(blksys_restart_MACH)
218 .globl ML_(blksys_complete_MACH)
219 .globl ML_(blksys_committed_MACH)
220 .globl ML_(blksys_finished_MACH)
221 ML_(blksys_setup_MACH): .long MK_L_SCCLASS_N(MACH,1)
222 ML_(blksys_restart_MACH):       .long MK_L_SCCLASS_N(MACH,2)
223 ML_(blksys_complete_MACH):      .long MK_L_SCCLASS_N(MACH,3)
224 ML_(blksys_committed_MACH):     .long MK_L_SCCLASS_N(MACH,4)
225 ML_(blksys_finished_MACH):      .long MK_L_SCCLASS_N(MACH,5)
227 .globl ML_(blksys_setup_MDEP)
228 .globl ML_(blksys_restart_MDEP)
229 .globl ML_(blksys_complete_MDEP)
230 .globl ML_(blksys_committed_MDEP)
231 .globl ML_(blksys_finished_MDEP)
232 ML_(blksys_setup_MDEP): .long MK_L_SCCLASS_N(MDEP,1)
233 ML_(blksys_restart_MDEP):       .long MK_L_SCCLASS_N(MDEP,2)
234 ML_(blksys_complete_MDEP):      .long MK_L_SCCLASS_N(MDEP,3)
235 ML_(blksys_committed_MDEP):     .long MK_L_SCCLASS_N(MDEP,4)
236 ML_(blksys_finished_MDEP):      .long MK_L_SCCLASS_N(MDEP,5)
238 .globl ML_(blksys_setup_UNIX)
239 .globl ML_(blksys_restart_UNIX)
240 .globl ML_(blksys_complete_UNIX)
241 .globl ML_(blksys_committed_UNIX)
242 .globl ML_(blksys_finished_UNIX)
243 ML_(blksys_setup_UNIX): .long MK_L_SCCLASS_N(UNIX,1)
244 ML_(blksys_restart_UNIX):       .long MK_L_SCCLASS_N(UNIX,2)
245 ML_(blksys_complete_UNIX):      .long MK_L_SCCLASS_N(UNIX,3)
246 ML_(blksys_committed_UNIX):     .long MK_L_SCCLASS_N(UNIX,4)
247 ML_(blksys_finished_UNIX):      .long MK_L_SCCLASS_N(UNIX,5)
249 #endif // defined(VGP_x86_darwin)
251 /* Let the linker know we don't need an executable stack */
252 MARK_STACK_NO_EXEC
254 /*--------------------------------------------------------------------*/
255 /*--- end                                                          ---*/
256 /*--------------------------------------------------------------------*/