1
2/*--------------------------------------------------------------------*/
3/*--- Support for doing system calls.        syscall-ppc64-linux.S ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7  This file is part of Valgrind, a dynamic binary instrumentation
8  framework.
9
10  Copyright (C) 2005-2015 Paul Mackerras <paulus@samba.org>
11
12  This program is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 2 of the
15  License, or (at your option) any later version.
16
17  This program is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25  02111-1307, USA.
26
27  The GNU General Public License is contained in the file COPYING.
28*/
29
30#include "pub_core_basics_asm.h"
31
32#if defined(VGP_ppc64le_linux)
33
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 NIP 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_gpr[0].  The syscall result is written
53        back to regs->m_gpr[3]/m_xer/m_result 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,		// r3
69				  void* guest_state,		// r4
70				  const vki_sigset_t *sysmask,	// r5
71				  const vki_sigset_t *postmask,	// r6
72				  Int sigsetSzB)		// r7
73*/
74/* from vki_arch.h */
75#define VKI_SIG_SETMASK 2
76
77.align 2
78.globl ML_(do_syscall_for_client_WRK)
79#if _CALL_ELF == 2
80.type .ML_(do_syscall_for_client_WRK),@function
81ML_(do_syscall_for_client_WRK):
820:      addis         2,12,.TOC.-0b@ha
83        addi          2,2,.TOC.-0b@l
84        .localentry   ML_(do_syscall_for_client_WRK), .-ML_(do_syscall_for_client_WRK)
85#else
86.section ".opd","aw"
87.align 3
88ML_(do_syscall_for_client_WRK):
89.quad .ML_(do_syscall_for_client_WRK),.TOC.@tocbase,0
90.previous
91#endif
92#if _CALL_ELF == 2
930:      addis        2,12,.TOC.-0b@ha
94        addi         2,2,.TOC.-0b@l
95        .localentry  ML_(do_syscall_for_client_WRK), .-ML_(do_syscall_for_client_WRK)
96#endif
97.type  .ML_(do_syscall_for_client_WRK),@function
98.globl .ML_(do_syscall_for_client_WRK)
99.ML_(do_syscall_for_client_WRK):
100        /* make a stack frame */
101        stdu    1,-80(1)
102        std     31,72(1)
103        std     30,64(1)
104        std     29,56(1)
105        std     28,48(1)
106        mr      31,3            /* syscall number */
107        mr      30,4            /* guest_state */
108        mr      29,6            /* postmask */
109        mr      28,7            /* sigsetSzB */
110
111        /* set the signal mask for doing the system call */
112        /* set up for sigprocmask(SIG_SETMASK, sysmask, postmask) */
1131:      li      0,__NR_rt_sigprocmask
114        li      3,VKI_SIG_SETMASK
115        mr      4,5
116        mr      5,6
117	mr	6,7
118        sc                      /* set the mask */
119        bso     7f              /* if the sigprocmask fails */
120
121        /* load up syscall args from the threadstate */
122        ld      3,OFFSET_ppc64_GPR3(30)
123        ld      4,OFFSET_ppc64_GPR4(30)
124        ld      5,OFFSET_ppc64_GPR5(30)
125        ld      6,OFFSET_ppc64_GPR6(30)
126        ld      7,OFFSET_ppc64_GPR7(30)
127        ld      8,OFFSET_ppc64_GPR8(30)
128        mr      0,31            /* syscall number */
1292:      sc                      /* do the syscall */
130
131        /* put the result back in the threadstate  */
1323:	std     3,OFFSET_ppc64_GPR3(30)     /* gst->GPR3 = sc result */
133	/* copy cr0.so back to simulated state */
134	mfcr    5                           /* r5 = CR               */
135	rlwinm	5,5,4,31,31                 /* r5 = (CR >> 28) & 1   */
136        stb     5,OFFSET_ppc64_CR0_0(30)    /* gst->CR0.SO = cr0.so  */
137
138        /* block signals again */
139	/* set up for sigprocmask(SIG_SETMASK, postmask, NULL) */
1404:      li      0,__NR_rt_sigprocmask
141        li      3,VKI_SIG_SETMASK
142        mr      4,29
143        li      5,0
144        mr      6,28
145        sc                      /* set the mask */
146        bso     7f              /* if the sigprocmask fails */
147        /* now safe from signals */
148	li	3,0		/* SUCCESS */
149
150        /* pop off stack frame */
1515:      ld      28,48(1)
152        ld      29,56(1)
153        ld      30,64(1)
154        ld      31,72(1)
155        addi    1,1,80
156        blr
157
158	/* failure: return 0x8000 | error code */
1597:	ori	3,3,0x8000	/* FAILURE -- ensure return value is nonzero */
160        b       5b
161#if _CALL_ELF == 2
162        .size .ML_(do_syscall_for_client_WRK),.-.ML_(do_syscall_for_client_WRK)
163#else
164        .size .ML_(do_syscall_for_client_WRK),.-.ML_(do_syscall_for_client_WRK)
165#endif
166.section .rodata
167/* export the ranges so that
168   VG_(fixup_guest_state_after_syscall_interrupted) can do the
169   right thing */
170
171.globl ML_(blksys_setup)
172.globl ML_(blksys_restart)
173.globl ML_(blksys_complete)
174.globl ML_(blksys_committed)
175.globl ML_(blksys_finished)
176ML_(blksys_setup):     .quad 1b
177ML_(blksys_restart):   .quad 2b
178ML_(blksys_complete):  .quad 3b
179ML_(blksys_committed): .quad 4b
180ML_(blksys_finished):  .quad 5b
181
182#endif // defined(VGP_ppc64le_linux)
183
184/* Let the linker know we don't need an executable stack */
185MARK_STACK_NO_EXEC
186
187/*--------------------------------------------------------------------*/
188/*--- end                                                          ---*/
189/*--------------------------------------------------------------------*/
190