vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / m_syswrap / syscall-x86-freebsd.S
blob1faa74c4d67d9a05738237670767b612b164e546
2 /*--------------------------------------------------------------------*/
3 /*--- Support for doing system calls.        syscall-x86-freebsd.S ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7   This file is part of Valgrind, a dynamic binary instrumentation
8   framework.
10   Copyright (C) 2000-2007 Julian Seward
11      jseward@acm.org
12    Copyright (C) 2018-2021 Paul Floyd
13       pjfloyd@wanadoo.fr
15   This program is free software; you can redistribute it and/or
16   modify it under the terms of the GNU General Public License as
17   published by the Free Software Foundation; either version 2 of the
18   License, or (at your option) any later version.
20   This program is distributed in the hope that it will be useful, but
21   WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   General Public License for more details.
25   You should have received a copy of the GNU General Public License
26   along with this program; if not, see <http://www.gnu.org/licenses/>.
28   The GNU General Public License is contained in the file COPYING.
31 #include "pub_core_basics_asm.h"
33 #if defined(VGP_x86_freebsd)
35 #include "pub_core_vkiscnums_asm.h"
36 #include "libvex_guest_offsets.h"
39 /*----------------------------------------------------------------*/
41    Perform a syscall for the client.  This will run a syscall
42    with the client's specific per-thread signal mask.
44    The structure of this function is such that, if the syscall is
45    interrupted by a signal, we can determine exactly what
46    execution state we were in with respect to the execution of
47    the syscall by examining the value of %eip in the signal
48    handler.  This means that we can always do the appropriate
49    thing to precisely emulate the kernel's signal/syscall
50    interactions.
52    The syscall number is taken from the argument, even though it
53    should also be in regs->m_eax.  The syscall result is written
54    back to regs->m_eax on completion.
56    Returns 0 if the syscall was successfully called (even if the
57    syscall itself failed), or a -ve error code if one of the
58    sigprocmasks failed (there's no way to determine which one
59    failed).
61    VG_(fixup_guest_state_after_syscall_interrupted) does the
62    thread state fixup in the case where we were interrupted by a
63    signal.
65         Prototype:
67    Int ML_(do_syscall_for_client_WRK)(
68            Int syscallno,                 // ebp+8
69            void* guest_state,             // ebp+12
70            const vki_sigset_t *sysmask,   // ebp+16
71            const vki_sigset_t *postmask,  // ebp+20
72            Int sigsetSzB)                 // ebp+24
74    Note that sigsetSzB is totally ignored (and irrelevant).
77 /* from vki-darwin.h, checked at startup by m_vki.c */
78 #define VKI_SIG_SETMASK 3
80 .globl ML_(do_syscall_for_client_WRK)
81 ML_(do_syscall_for_client_WRK):
82         /* establish stack frame */
83    push  %ebp
84    mov   %esp, %ebp
85    subl  $8, %esp       /* 16-byte align stack */
87 1:      /* Even though we can't take a signal until the
88       sigprocmask completes, start the range early.
89       If eip is in the range [1,2), the syscall hasn't been started yet */
91    /* Set the signal mask which should be current during the syscall. */
92    pushl 20(%ebp)
93    pushl 16(%ebp)
94    pushl $VKI_SIG_SETMASK
95    pushl $0xcafebabe    /* totally fake return address */
96    movl  $__NR_sigprocmask, %eax
97    int   $0x80
98    jc    7f  /* sigprocmask failed */
99    addl  $16,%esp
101    /* Copy syscall parameters to the stack - assume no more than 8
102     * plus the return address */
103    /* do_syscall8 */
104    /* stack is currently aligned assuming 8 parameters */
105    movl  12(%ebp), %edx
106    movl  OFFSET_x86_ESP(%edx), %edx     /* edx = simulated ESP */
107    movl  28+4(%edx), %eax
108    pushl %eax
109    movl  24+4(%edx), %eax
110    pushl %eax
111    movl  20+4(%edx), %eax
112    pushl %eax
113    movl  16+4(%edx), %eax
114    pushl %eax
115    movl  12+4(%edx), %eax
116    pushl %eax
117    movl  8+4(%edx), %eax
118    pushl %eax
119    movl  4+4(%edx), %eax
120    pushl %eax
121    movl  0+4(%edx), %eax
122    pushl %eax
123    /* return address */
124    movl  0(%edx), %eax
125    pushl %eax
127    /* Put syscall number in eax */
128    movl 8(%ebp), %eax
130    /* If eip==2, then the syscall was either just about to start,
131       or was interrupted and the kernel was restarting it. */
132 2:      int   $0x80             /* UNIX (GrP fixme should be sysenter?) */
134 3: /* In the range [3, 4), the syscall result is in %eax and %edx and C,
135       but hasn't been committed to the thread state. */
136    setc  0(%esp)                                /* stash returned carry flag */
137    movl  12(%ebp), %ecx
138    movl  %eax, OFFSET_x86_EAX(%ecx)     /* save EAX to vex */
139    movl  %edx, OFFSET_x86_EDX(%ecx)     /* save EDX to vex */
140    /* save carry flag to vex */
141    subl  $12, %esp
142    movl  %ecx, 4(%esp)
143    movl  $0, 0(%esp)
144    movb  12(%esp), %al
145    movb  %al, 0(%esp)
146    call  LibVEX_GuestX86_put_eflag_c
147    addl  $12, %esp
149 4: /* Re-block signals.  If eip is in [4,5), then the syscall is
150       complete and we needn't worry about it. */
151    /* Set up for __pthread_sigmask(SIG_SETMASK, postmask, NULL) */
152    pushl $0
153    pushl 20(%ebp)
154    pushl $VKI_SIG_SETMASK
155    pushl $0xcafef00d    /* totally fake return address */
156    movl  $__NR_sigprocmask, %eax
157    int   $0x80  /* should be sysenter? */
158    jc    7f  /* sigprocmask failed */
159    addl  $16,%esp
161 5: /* now safe from signals */
162    movl  $0, %eax       /* SUCCESS */
163    movl  %ebp, %esp
164    popl  %ebp
165    ret
167 7: /* failure: return 0x8000 | error code */
168    /* Note that we enter here with %esp being 16 too low
169       (4 extra words on the stack).  But because we're nuking
170       the stack frame now, that doesn't matter. */
171    andl  $0x7FFF, %eax
172    orl   $0x8000, %eax
173    movl  %ebp, %esp
174    popl  %ebp
175    ret
177 .section .rodata
178 /* export the ranges so that
179    VG_(fixup_guest_state_after_syscall_interrupted) can do the
180    right thing */
182 .globl ML_(blksys_setup)
183 .globl ML_(blksys_restart)
184 .globl ML_(blksys_complete)
185 .globl ML_(blksys_committed)
186 .globl ML_(blksys_finished)
187 ML_(blksys_setup):      .long 1b
188 ML_(blksys_restart):    .long 2b
189 ML_(blksys_complete):   .long 3b
190 ML_(blksys_committed):  .long 4b
191 ML_(blksys_finished):   .long 5b
192 .previous
194 #endif // defined(VGP_x86_freebsd)
196 /* Let the linker know we don't need an executable stack */
197 MARK_STACK_NO_EXEC
199 /*--------------------------------------------------------------------*/
200 /*--- end                                                          ---*/
201 /*--------------------------------------------------------------------*/