2 /*--------------------------------------------------------------------*/
3 /*--- Support for doing system calls. syscall-x86-freebsd.S ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2007 Julian Seward
12 Copyright (C) 2018-2021 Paul Floyd
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
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
61 VG_(fixup_guest_state_after_syscall_interrupted) does the
62 thread state fixup in the case where we were interrupted by a
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 */
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. */
94 pushl $VKI_SIG_SETMASK
95 pushl $0xcafebabe /* totally fake return address */
96 movl $__NR_sigprocmask, %eax
98 jc 7f /* sigprocmask failed */
101 /* Copy syscall parameters to the stack - assume no more than 8
102 * plus the return address */
104 /* stack is currently aligned assuming 8 parameters */
106 movl OFFSET_x86_ESP(%edx), %edx /* edx = simulated ESP */
107 movl 28+4(%edx), %eax
109 movl 24+4(%edx), %eax
111 movl 20+4(%edx), %eax
113 movl 16+4(%edx), %eax
115 movl 12+4(%edx), %eax
127 /* Put syscall number in 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 */
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 */
146 call LibVEX_GuestX86_put_eflag_c
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) */
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 */
161 5: /* now safe from signals */
162 movl $0, %eax /* SUCCESS */
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. */
178 /* export the ranges so that
179 VG_(fixup_guest_state_after_syscall_interrupted) can do the
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
194 #endif // defined(VGP_x86_freebsd)
196 /* Let the linker know we don't need an executable stack */
199 /*--------------------------------------------------------------------*/
201 /*--------------------------------------------------------------------*/