vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / m_syswrap / syscall-amd64-darwin.S
blobed93a851ebfabded3ff5fae2503f41a9efc4009e
2 /*--------------------------------------------------------------------*/
3 /*--- Support for doing system calls.       syscall-amd64-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_amd64_darwin)
33 #include "pub_core_vkiscnums_asm.h"
34 #include "libvex_guest_offsets.h"
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 guest_state->guest_RAX.  The syscall result
52         is written back to guest_state->guest_RAX 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,                // rdi
67                                   void* guest_state,            // rsi
68                                   const vki_sigset_t *sysmask,  // rdx
69                                   const vki_sigset_t *postmask, // rcx
70                                   Int sigsetSzB)                // r8
72         Note that sigsetSzB is totally ignored (and irrelevant).
75 /* from vki_arch.h */   
76 #define VKI_SIG_SETMASK 3
78 /* DO_SYSCALL MACH|MDEP|UNIX */
79 #define MACH 1
80 #define MDEP 2
81 #define UNIX 3
83 .macro DO_SYSCALL
84         /* save callee-saved regs */
85         pushq   %rbp
86         movq    %rsp, %rbp
87         // stack is now aligned
88         pushq   %rdi  // -8(%rbp)   syscallno
89         pushq   %rsi  // -16(%rbp)  guest_state
90         pushq   %rdx  // -24(%rbp)  sysmask
91         pushq   %rcx  // -32(%rbp)  postmask
92         pushq   %r8   // -40(%rbp)  sigsetSzB
93         // stack is now aligned
94         
95 L_$0_1: /* Even though we can't take a signal until the sigprocmask completes,
96            start the range early.
97            If rip is in the range [1,2), the syscall hasn't been started yet */
99         /* Set the signal mask which should be current during the syscall. */
100         /* GrP fixme signals
101            DDD: JRS fixme: use __NR___pthread_sigmask, not __NR_rt_sigprocmask
102         movq    $__NR_rt_sigprocmask, %rax      // syscall #
103         movq    $VKI_SIG_SETMASK, %rdi          // how
104         movq    -24(%rbp), %rsi                 // sysmask
105         movq    -32(%rbp), %rdx                 // postmask
106         movq    -40(%rbp), %r10                 // sigsetSzB in r10 not rcx
107         DDD: fixme return address
108         syscall
110         jnc     7f      // sigprocmask failed
111         */
112         
113         /* OK, that worked.  Now do the syscall proper. */
115         /* 6 register parameters */
116         movq    -16(%rbp), %r11 /* r11 = VexGuestAMD64State * */
117         movq    OFFSET_amd64_RDI(%r11), %rdi
118         movq    OFFSET_amd64_RSI(%r11), %rsi
119         movq    OFFSET_amd64_RDX(%r11), %rdx
120         movq    OFFSET_amd64_RCX(%r11), %r10 /* rcx is passed in r10 instead */
121         movq    OFFSET_amd64_R8(%r11), %r8
122         movq    OFFSET_amd64_R9(%r11), %r9
123         /* 2 stack parameters plus return address (ignored by syscall) */
124         movq    OFFSET_amd64_RSP(%r11), %r11 /* r11 = simulated RSP */
125         movq    16(%r11), %rax
126         pushq   %rax
127         movq    8(%r11), %rax
128         pushq   %rax
129         /* stack is currently aligned - return address misaligns */
130         movq    0(%r11), %rax
131         pushq   %rax
132         /* syscallno */
133         movq    -8(%rbp), %rax
134         
135         /* If rip==2, then the syscall was either just about
136            to start, or was interrupted and the kernel was 
137            restarting it. */
138 L_$0_2: syscall
139 L_$0_3: /* In the range [3, 4), the syscall result is in %rax, 
140            but hasn't been committed to RAX. */
142         /* stack contents: 3 words for syscall above, plus our prologue */
143         setc    0(%rsp)         /* stash returned carry flag */
145         movq    -16(%rbp), %r11 /* r11 = VexGuestAMD64State * */
146         movq    %rax, OFFSET_amd64_RAX(%r11)    /* save back to RAX */
147         movq    %rdx, OFFSET_amd64_RDX(%r11)    /* save back to RDX */
149 .if $0 == UNIX
150         /* save carry flag to VEX */
151         xor     %rax, %rax
152         movb    0(%rsp), %al
153         movq    %rax, %rdi      /* arg1 = new flag */
154         movq    %r11, %rsi      /* arg2 = vex state */
155         addq    $$24, %rsp      /* remove syscall parameters */
156         call    _LibVEX_GuestAMD64_put_rflag_c
157 .else
158         addq    $$24, %rsp      /* remove syscall parameters*/
159 .endif
161 L_$0_4: /* Re-block signals.  If eip is in [4,5), then the syscall 
162            is complete and we needn't worry about it. */
163         /* GrP fixme signals
164            DDD: JRS fixme: use __NR___pthread_sigmask, not __NR_rt_sigprocmask
165         PUSH_di_si_dx_cx_8
167         movq    $__NR_rt_sigprocmask, %rax      // syscall #
168         movq    $VKI_SIG_SETMASK, %rdi          // how
169         movq    %rcx, %rsi                      // postmask
170         xorq    %rdx, %rdx                      // NULL
171         movq    %r8, %r10                       // sigsetSzB
172         DDD: fixme return address
173         syscall
175         POP_di_si_dx_cx_8
177         jnc     7f      // sigprocmask failed
178         */
179 L_$0_5: /* now safe from signals */
180         movq    $$0, %rax       /* SUCCESS */
181         movq    %rbp, %rsp
182         popq    %rbp
183         ret
185 /* GrP fixme signals
186 L_$0_7: // failure:      return 0x8000 | error code
187         DDD: fixme return value
188         movq    %rbp, %rsp
189         popq    %rbp
190         ret
193 .endmacro
195         
196 .globl ML_(do_syscall_for_client_unix_WRK)
197 ML_(do_syscall_for_client_unix_WRK):
198         DO_SYSCALL UNIX
200 .globl ML_(do_syscall_for_client_mach_WRK)
201 ML_(do_syscall_for_client_mach_WRK):
202         DO_SYSCALL MACH
203         
204 .globl ML_(do_syscall_for_client_mdep_WRK)
205 ML_(do_syscall_for_client_mdep_WRK):
206         DO_SYSCALL MDEP
208 .data
209 /* export the ranges so that
210    VG_(fixup_guest_state_after_syscall_interrupted) can do the
211    right thing */
213 /* eg MK_L_SCLASS_N(UNIX,99) produces L_3_99
214    since UNIX is #defined to 3 at the top of this file */
215 #define FOO(scclass,labelno) L_##scclass##_##labelno
216 #define MK_L_SCCLASS_N(scclass,labelno) FOO(scclass,labelno)
218 .globl ML_(blksys_setup_MACH)
219 .globl ML_(blksys_restart_MACH)
220 .globl ML_(blksys_complete_MACH)
221 .globl ML_(blksys_committed_MACH)
222 .globl ML_(blksys_finished_MACH)
223 ML_(blksys_setup_MACH): .quad MK_L_SCCLASS_N(MACH,1)
224 ML_(blksys_restart_MACH):       .quad MK_L_SCCLASS_N(MACH,2)
225 ML_(blksys_complete_MACH):      .quad MK_L_SCCLASS_N(MACH,3)
226 ML_(blksys_committed_MACH):     .quad MK_L_SCCLASS_N(MACH,4)
227 ML_(blksys_finished_MACH):      .quad MK_L_SCCLASS_N(MACH,5)
229 .globl ML_(blksys_setup_MDEP)
230 .globl ML_(blksys_restart_MDEP)
231 .globl ML_(blksys_complete_MDEP)
232 .globl ML_(blksys_committed_MDEP)
233 .globl ML_(blksys_finished_MDEP)
234 ML_(blksys_setup_MDEP): .quad MK_L_SCCLASS_N(MDEP,1)
235 ML_(blksys_restart_MDEP):       .quad MK_L_SCCLASS_N(MDEP,2)
236 ML_(blksys_complete_MDEP):      .quad MK_L_SCCLASS_N(MDEP,3)
237 ML_(blksys_committed_MDEP):     .quad MK_L_SCCLASS_N(MDEP,4)
238 ML_(blksys_finished_MDEP):      .quad MK_L_SCCLASS_N(MDEP,5)
240 .globl ML_(blksys_setup_UNIX)
241 .globl ML_(blksys_restart_UNIX)
242 .globl ML_(blksys_complete_UNIX)
243 .globl ML_(blksys_committed_UNIX)
244 .globl ML_(blksys_finished_UNIX)
245 ML_(blksys_setup_UNIX): .quad MK_L_SCCLASS_N(UNIX,1)
246 ML_(blksys_restart_UNIX):       .quad MK_L_SCCLASS_N(UNIX,2)
247 ML_(blksys_complete_UNIX):      .quad MK_L_SCCLASS_N(UNIX,3)
248 ML_(blksys_committed_UNIX):     .quad MK_L_SCCLASS_N(UNIX,4)
249 ML_(blksys_finished_UNIX):      .quad MK_L_SCCLASS_N(UNIX,5)
251 #endif // defined(VGP_amd64_darwin)
253 /* Let the linker know we don't need an executable stack */
254 MARK_STACK_NO_EXEC
256 /*--------------------------------------------------------------------*/
257 /*--- end                                                          ---*/
258 /*--------------------------------------------------------------------*/