2 /*--------------------------------------------------------------------*/
3 /*--- Support for doing system calls. syscall-arm64-freebsd.S ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2024 Paul Floyd
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.
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.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
29 #include "pub_core_basics_asm.h"
31 #if defined(VGP_arm64_freebsd)
33 #include "pub_core_vkiscnums_asm.h"
34 #include "libvex_guest_offsets.h"
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 IP in the signal
45 handler. This means that we can always do the appropriate
46 thing to precisely emulate the kernel's signal/syscall
49 The syscall number is taken from the argument, even though it
50 should also be in guest_state->guest_X8. The syscall result
51 is written back to guest_state->guest_X0 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
65 UWord ML_(do_syscall_for_client_WRK)(
67 void* guest_state, // x1
68 const vki_sigset_t *sysmask, // x2
69 const vki_sigset_t *postmask, // x3
72 /* from vki-arm64-freebsd.h */
73 #define VKI_SIG_SETMASK 3
75 .globl ML_(do_syscall_for_client_WRK)
76 ML_(do_syscall_for_client_WRK):
78 /* Stash callee-saves and our args on the stack */
79 stp x29, x30, [sp, #-16]!
80 stp x27, x28, [sp, #-16]!
81 stp x25, x26, [sp, #-16]!
82 stp x23, x24, [sp, #-16]!
83 stp x21, x22, [sp, #-16]!
84 stp x19, x20, [sp, #-16]!
85 stp x4, x5, [sp, #-16]!
86 stp x2, x3, [sp, #-16]!
87 stp x0, x1, [sp, #-16]!
91 mov x8, #__NR_sigprocmask
92 mov x0, #VKI_SIG_SETMASK
93 mov x1, x2 /* sysmask */
94 mov x2, x3 /* postmask */
95 mov x3, x4 /* nsigwords */
98 ldr x9, [sp, #8] /* saved x1 == guest_state */
100 ldr x8, [sp, #0] /* saved x0 == syscall# */
101 ldr x0, [x9, #OFFSET_arm64_X0]
102 ldr x1, [x9, #OFFSET_arm64_X1]
103 ldr x2, [x9, #OFFSET_arm64_X2]
104 ldr x3, [x9, #OFFSET_arm64_X3]
105 ldr x4, [x9, #OFFSET_arm64_X4]
106 ldr x5, [x9, #OFFSET_arm64_X5]
107 ldr x6, [x9, #OFFSET_arm64_X6]
108 ldr x7, [x9, #OFFSET_arm64_X7]
113 /* stash returned carry flag */
117 ldr x5, [sp, #8] /* saved x1 == guest_state */
118 str x0, [x5, #OFFSET_arm64_X0]
119 str x1, [x5, #OFFSET_arm64_X1]
121 /* save carry flag to VEX */
122 mov x0, x4 /* arg1 = new flag */
123 ldr x1, [sp, #8] /* arg2 = vex state */
125 str x20, [x1, #OFFSET_arm64_SETC]
126 bl LibVEX_GuestARM64_put_nzcv_c
127 ldr x1, [sp, #8] /* arg2 = vex state */
129 str x20, [x1, #OFFSET_arm64_SETC]
132 mov x8, #__NR_sigprocmask
133 mov x0, #VKI_SIG_SETMASK
134 ldr x1, [sp, #24] /* saved x3 == postmask */
136 ldr x3, [sp, #32] /* saved x4 == nsigwords */
137 cmn xzr, xzr /* clear the carry flag */
142 5: /* Success: return zero */
144 ldp xzr, x1, [sp], #16
145 ldp x2, x3, [sp], #16
146 ldp x4, x5, [sp], #16
147 ldp x19, x20, [sp], #16
148 ldp x21, x22, [sp], #16
149 ldp x23, x24, [sp], #16
150 ldp x25, x26, [sp], #16
151 ldp x27, x28, [sp], #16
152 ldp x29, x30, [sp], #16
155 7: /* Failure: return 0x8000 | error code */
157 ldp xzr, x1, [sp], #16
158 ldp x2, x3, [sp], #16
159 ldp x4, x5, [sp], #16
160 ldp x19, x20, [sp], #16
161 ldp x21, x22, [sp], #16
162 ldp x23, x24, [sp], #16
163 ldp x25, x26, [sp], #16
164 ldp x27, x28, [sp], #16
165 ldp x29, x30, [sp], #16
169 /* export the ranges so that
170 VG_(fixup_guest_state_after_syscall_interrupted) can do the
174 .globl ML_(blksys_setup)
175 .globl ML_(blksys_restart)
176 .globl ML_(blksys_complete)
177 .globl ML_(blksys_committed)
178 .globl ML_(blksys_finished)
179 ML_(blksys_setup): .quad 1b
180 ML_(blksys_restart): .quad 2b
181 ML_(blksys_complete): .quad 3b
182 ML_(blksys_committed): .quad 4b
183 ML_(blksys_finished): .quad 5b
185 #endif // defined(VGP_arm_freebsd)
187 /* Let the linker know we don't need an executable stack */
190 /*--------------------------------------------------------------------*/
192 /*--------------------------------------------------------------------*/