1
2/*--------------------------------------------------------------------*/
3/*--- Support for doing system calls.          syscall-x86-linux.S ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7  This file is part of Valgrind, a dynamic binary instrumentation
8  framework.
9
10  Copyright (C) 2000-2013 Julian Seward
11     jseward@acm.org
12
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.
17
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.
22
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26  02111-1307, USA.
27
28  The GNU General Public License is contained in the file COPYING.
29*/
30
31#if defined(VGP_x86_linux)
32
33#include "pub_core_basics_asm.h"
34#include "pub_core_vkiscnums_asm.h"
35#include "libvex_guest_offsets.h"
36
37
38/*----------------------------------------------------------------*/
39/*
40	Perform a syscall for the client.  This will run a syscall
41	with the client's specific per-thread signal mask.
42
43	The structure of this function is such that, if the syscall is
44	interrupted by a signal, we can determine exactly what
45	execution state we were in with respect to the execution of
46	the syscall by examining the value of %eip in the signal
47	handler.  This means that we can always do the appropriate
48	thing to precisely emulate the kernel's signal/syscall
49	interactions.
50
51	The syscall number is taken from the argument, even though it
52	should also be in regs->m_eax.  The syscall result is written
53	back to regs->m_eax on completion.
54
55	Returns 0 if the syscall was successfully called (even if the
56	syscall itself failed), or a nonzero error code in the lowest
57        8 bits if one of the sigprocmasks failed (there's no way to
58        determine which one failed).  And there's no obvious way to
59        recover from that either, but nevertheless we want to know.
60
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.
64
65	Prototype:
66
67	UWord ML_(do_syscall_for_client_WRK)(
68	                          Int syscallno,		// 0
69				  void* guest_state,		// 4
70				  const vki_sigset_t *sysmask,	// 8
71				  const vki_sigset_t *postmask,	// 12
72				  Int sigsetSzB)		// 16
73
74*/
75
76/* from vki_arch.h */
77#define VKI_SIG_SETMASK	2
78
79.globl ML_(do_syscall_for_client_WRK)
80ML_(do_syscall_for_client_WRK):
81	.cfi_startproc
82	/* save callee-saved regs */
83	push	%esi
84	.cfi_adjust_cfa_offset 4
85	.cfi_offset %esi, -8
86	push	%edi
87	.cfi_adjust_cfa_offset 4
88	.cfi_offset %esi, -12
89	push	%ebx
90	.cfi_adjust_cfa_offset 4
91	.cfi_offset %esi, -16
92	push	%ebp
93	.cfi_adjust_cfa_offset 4
94	.cfi_offset %esi, -20
95#define FSZ	((4+1)*4)	/* 4 args + ret addr */
96
971:	/* Even though we can't take a signal until the sigprocmask completes,
98	   start the range early.
99	   If eip is in the range [1,2), the syscall hasn't been started yet */
100
101	/* Set the signal mask which should be current during the syscall. */
102	movl	$ __NR_rt_sigprocmask, %eax
103	movl	$ VKI_SIG_SETMASK, %ebx
104	movl	8+FSZ(%esp), %ecx
105	movl	12+FSZ(%esp), %edx
106	movl	16+FSZ(%esp), %esi
107	int	$0x80
108	testl	%eax, %eax
109	js	7f	/* sigprocmask failed */
110
111	movl	 4+FSZ(%esp), %eax	/* eax == ThreadState * */
112
113	movl	 OFFSET_x86_EBX(%eax), %ebx
114	movl	 OFFSET_x86_ECX(%eax), %ecx
115	movl	 OFFSET_x86_EDX(%eax), %edx
116	movl	 OFFSET_x86_ESI(%eax), %esi
117	movl	 OFFSET_x86_EDI(%eax), %edi
118	movl	 OFFSET_x86_EBP(%eax), %ebp
119	movl	 0+FSZ(%esp), %eax	/* use syscallno argument rather than thread EAX */
120
121	/* If eip==2, then the syscall was either just about to start,
122	   or was interrupted and the kernel was restarting it. */
1232:	int	$0x80
1243:	/* In the range [3, 4), the syscall result is in %eax, but hasn't been
125	   committed to EAX. */
126	movl	4+FSZ(%esp), %ebx
127	movl	%eax, OFFSET_x86_EAX(%ebx)	/* save back to EAX */
128
1294:	/* Re-block signals.  If eip is in [4,5), then the syscall is complete and
130	   we needn't worry about it. */
131	movl	$ __NR_rt_sigprocmask, %eax
132	movl	$ VKI_SIG_SETMASK, %ebx
133	movl	12+FSZ(%esp), %ecx
134	xorl	%edx, %edx
135	movl	16+FSZ(%esp), %esi
136	int	$0x80
137	testl	%eax, %eax
138	js	7f	/* sigprocmask failed */
139
1405:	/* now safe from signals */
141	movl	$0, %eax	/* SUCCESS */
142	popl	%ebp
143	.cfi_adjust_cfa_offset -4
144	popl	%ebx
145	.cfi_adjust_cfa_offset -4
146	popl	%edi
147	.cfi_adjust_cfa_offset -4
148	popl	%esi
149	.cfi_adjust_cfa_offset -4
150	ret
151	.cfi_adjust_cfa_offset 4*4
152
1537:	/* failure: return 0x8000 | error code */
154	negl	%eax
155	andl	$0x7FFF, %eax
156	orl	$0x8000, %eax
157	popl	%ebp
158	.cfi_adjust_cfa_offset -4
159	popl	%ebx
160	.cfi_adjust_cfa_offset -4
161	popl	%edi
162	.cfi_adjust_cfa_offset -4
163	popl	%esi
164	.cfi_adjust_cfa_offset -4
165	ret
166	.cfi_endproc
167#undef FSZ
168
169
170.section .rodata
171/* export the ranges so that
172   VG_(fixup_guest_state_after_syscall_interrupted) can do the
173   right thing */
174
175.globl ML_(blksys_setup)
176.globl ML_(blksys_restart)
177.globl ML_(blksys_complete)
178.globl ML_(blksys_committed)
179.globl ML_(blksys_finished)
180ML_(blksys_setup):	.long 1b
181ML_(blksys_restart):	.long 2b
182ML_(blksys_complete):	.long 3b
183ML_(blksys_committed):	.long 4b
184ML_(blksys_finished):	.long 5b
185.previous
186
187/* Let the linker know we don't need an executable stack */
188.section .note.GNU-stack,"",@progbits
189
190#endif // defined(VGP_x86_linux)
191
192/*--------------------------------------------------------------------*/
193/*--- end                                                          ---*/
194/*--------------------------------------------------------------------*/
195