vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / m_syswrap / syscall-ppc64le-linux.S
blobe416688e37198ee447f264703c58df89e3e9188c
2 /*--------------------------------------------------------------------*/
3 /*--- Support for doing system calls.        syscall-ppc64-linux.S ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7   This file is part of Valgrind, a dynamic binary instrumentation
8   framework.
10   Copyright (C) 2005-2017 Paul Mackerras <paulus@samba.org>
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.
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.
22   You should have received a copy of the GNU General Public License
23   along with this program; if not, see <http://www.gnu.org/licenses/>.
25   The GNU General Public License is contained in the file COPYING.
28 #include "pub_core_basics_asm.h"
30 #if defined(VGP_ppc64le_linux)
32 #include "pub_core_vkiscnums_asm.h"
33 #include "libvex_guest_offsets.h"
34                 
36 /*----------------------------------------------------------------*/
38         Perform a syscall for the client.  This will run a syscall
39         with the client's specific per-thread signal mask.
41         The structure of this function is such that, if the syscall is
42         interrupted by a signal, we can determine exactly what
43         execution state we were in with respect to the execution of
44         the syscall by examining the value of NIP in the signal
45         handler.  This means that we can always do the appropriate
46         thing to precisely emulate the kernel's signal/syscall
47         interactions.
49         The syscall number is taken from the argument, even though it
50         should also be in regs->m_gpr[0].  The syscall result is written
51         back to regs->m_gpr[3]/m_xer/m_result on completion.
53         Returns 0 if the syscall was successfully called (even if the
54         syscall itself failed), or a nonzero error code in the lowest
55         8 bits if one of the sigprocmasks failed (there's no way to
56         determine which one failed).  And there's no obvious way to
57         recover from that either, but nevertheless we want to know.
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.
63         Prototype:
65         UWord ML_(do_syscall_for_client_WRK)(
66                                   Int syscallno,                // r3
67                                   void* guest_state,            // r4
68                                   const vki_sigset_t *sysmask,  // r5
69                                   const vki_sigset_t *postmask, // r6
70                                   Int sigsetSzB)                // r7
72 /* from vki_arch.h */
74 /* SCV flag defines.  Must be consistently defined here and in
75    coregrind/pub_core_syscall.h,in the do_syscall_WRK() assembly code in
76    coregrind/m_syscall.c and coregrind/m_syswrap/syscall-ppcle-linux.S code.
78 #define SC_FLAG  1
79 #define SCV_FLAG 2
81 #define VKI_SIG_SETMASK 2
83 .align 2
84 .globl ML_(do_syscall_for_client_WRK)
85 #if _CALL_ELF == 2
86 .type .ML_(do_syscall_for_client_WRK),@function
87 ML_(do_syscall_for_client_WRK):
88 0:      addis         2,12,.TOC.-0b@ha
89         addi          2,2,.TOC.-0b@l
90         .localentry   ML_(do_syscall_for_client_WRK), .-ML_(do_syscall_for_client_WRK)
91 #else
92 .section ".opd","aw"
93 .align 3
94 ML_(do_syscall_for_client_WRK):
95 .quad .ML_(do_syscall_for_client_WRK),.TOC.@tocbase,0
96 .previous
97 #endif
98 #if _CALL_ELF == 2
99 0:      addis        2,12,.TOC.-0b@ha
100         addi         2,2,.TOC.-0b@l
101         .localentry  ML_(do_syscall_for_client_WRK), .-ML_(do_syscall_for_client_WRK)
102 #endif
103 .type  .ML_(do_syscall_for_client_WRK),@function
104 .globl .ML_(do_syscall_for_client_WRK)
105 .ML_(do_syscall_for_client_WRK):
106         /* make a stack frame */
107         stdu    1,-80(1)
108         std     31,72(1)
109         std     30,64(1)
110         std     29,56(1)
111         std     28,48(1)
112         std     27,40(1)        /* save r27 to stack frame */
113         mflr    27              /* Get link register */
114         std     27,16(1)        /* Save link register */
115         std     9,32(1)         /* save gpr 9 */
116         mr      31,3            /* syscall number */
117         mr      30,4            /* guest_state */
118         mr      29,6            /* postmask */
119         mr      28,7            /* sigsetSzB */
121         /* set the signal mask for doing the system call */
122         /* set up for sigprocmask(SIG_SETMASK, sysmask, postmask) */
123 1:      li      0,__NR_rt_sigprocmask
124         li      3,VKI_SIG_SETMASK
125         mr      4,5
126         mr      5,6
127         mr      6,7
128         sc                      /* set the mask */
129         bso     7f              /* if the sigprocmask fails */
131         /* load up syscall args from the threadstate */
132         ld      3,OFFSET_ppc64_GPR3(30)
133         ld      4,OFFSET_ppc64_GPR4(30)
134         ld      5,OFFSET_ppc64_GPR5(30)
135         ld      6,OFFSET_ppc64_GPR6(30)
136         ld      7,OFFSET_ppc64_GPR7(30)
137         ld      8,OFFSET_ppc64_GPR8(30)
138         ld      9,OFFSET_ppc64_GPR9(30)   /* get flag for sc or scv inst */
139         mr      0,31            /* syscall number */
140         cmpdi   9,SC_FLAG
141         bne     8f              /* jump to scv call if gpr9 != 0 */
142         /* If you change the code between labels 2 and 3, you need to update
143            the corresponding ppc64le calculations for blksys_restart and
144            blksys_complete in function getSyscallArgsFromGuestState in file
145            syswrap-main.c.  */
146 2:      sc                      /* do the syscall */
147         b       3f              /* jump over scv call */
148 8:      scv     0
150         /* put the result back in the threadstate  */
151 3:      std     3,OFFSET_ppc64_GPR3(30)     /* gst->GPR3 = sc result */
153         /* copy cr0.so back to simulated state */
154         mfcr    5                           /* r5 = CR               */
155         rlwinm  5,5,4,31,31                 /* r5 = (CR >> 28) & 1   */
156         ld      9,OFFSET_ppc64_GPR9(30)
157         cmpdi   9,SC_FLAG
158         bne     4f                          /* scv does not change CR */
159         stb     5,OFFSET_ppc64_CR0_0(30)    /* gst->CR0.SO = cr0.so  */
161         /* block signals again */
162         /* set up for sigprocmask(SIG_SETMASK, postmask, NULL) */
163 4:      li      0,__NR_rt_sigprocmask
164         li      3,VKI_SIG_SETMASK
165         mr      4,29
166         li      5,0
167         mr      6,28
168         sc                      /* set the mask */
169         bso     7f              /* if the sigprocmask fails */
170         /* now safe from signals */
171         li      3,0             /* SUCCESS */
173         /* pop off stack frame */
174 5:      ld      28,48(1)
175         ld      29,56(1)
176         ld      30,64(1)
177         ld      31,72(1)
178         ld      9,32(1)
179         ld      27,16(1)        /* Fetch LR from frame */
180         mtlr    27              /* restore LR */
181         ld      27,40(1)        /* restore r27 from stack frame */
182         addi    1,1,80
183         blr
185         /* failure: return 0x8000 | error code */
186 7:      ori     3,3,0x8000      /* FAILURE -- ensure return value is nonzero */
187         b       5b
188 #if _CALL_ELF == 2
189         .size .ML_(do_syscall_for_client_WRK),.-.ML_(do_syscall_for_client_WRK)
190 #else
191         .size .ML_(do_syscall_for_client_WRK),.-.ML_(do_syscall_for_client_WRK)
192 #endif
193 .section .rodata
194 /* export the ranges so that
195    VG_(fixup_guest_state_after_syscall_interrupted) can do the
196    right thing */
198 .globl ML_(blksys_setup)
199 .globl ML_(blksys_restart)
200 .globl ML_(blksys_complete)
201 .globl ML_(blksys_committed)
202 .globl ML_(blksys_finished)
203 ML_(blksys_setup):     .quad 1b
204 ML_(blksys_restart):   .quad 2b
205 ML_(blksys_complete):  .quad 3b
206 ML_(blksys_committed): .quad 4b
207 ML_(blksys_finished):  .quad 5b
209 #endif // defined(VGP_ppc64le_linux)
211 /* Let the linker know we don't need an executable stack */
212 MARK_STACK_NO_EXEC
214 /*--------------------------------------------------------------------*/
215 /*--- end                                                          ---*/
216 /*--------------------------------------------------------------------*/