xref: /qemu/include/user/safe-syscall.h (revision 7a21bee2aa52fc95b25e38372678986ee94f05f1)
1a57e0c36SPeter Maydell /*
2a57e0c36SPeter Maydell  * safe-syscall.h: prototypes for linux-user signal-race-safe syscalls
3a57e0c36SPeter Maydell  *
4a57e0c36SPeter Maydell  *  This program is free software; you can redistribute it and/or modify
5a57e0c36SPeter Maydell  *  it under the terms of the GNU General Public License as published by
6a57e0c36SPeter Maydell  *  the Free Software Foundation; either version 2 of the License, or
7a57e0c36SPeter Maydell  *  (at your option) any later version.
8a57e0c36SPeter Maydell  *
9a57e0c36SPeter Maydell  *  This program is distributed in the hope that it will be useful,
10a57e0c36SPeter Maydell  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11a57e0c36SPeter Maydell  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12a57e0c36SPeter Maydell  *  GNU General Public License for more details.
13a57e0c36SPeter Maydell  *
14a57e0c36SPeter Maydell  *  You should have received a copy of the GNU General Public License
15a57e0c36SPeter Maydell  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16a57e0c36SPeter Maydell  */
17a57e0c36SPeter Maydell 
18a57e0c36SPeter Maydell #ifndef LINUX_USER_SAFE_SYSCALL_H
19a57e0c36SPeter Maydell #define LINUX_USER_SAFE_SYSCALL_H
20a57e0c36SPeter Maydell 
21a57e0c36SPeter Maydell /**
22a57e0c36SPeter Maydell  * safe_syscall:
23a57e0c36SPeter Maydell  * @int number: number of system call to make
24a57e0c36SPeter Maydell  * ...: arguments to the system call
25a57e0c36SPeter Maydell  *
26a57e0c36SPeter Maydell  * Call a system call if guest signal not pending.
27a57e0c36SPeter Maydell  * This has the same API as the libc syscall() function, except that it
28af254a27SRichard Henderson  * may return -1 with errno == QEMU_ERESTARTSYS if a signal was pending.
29a57e0c36SPeter Maydell  *
30a57e0c36SPeter Maydell  * Returns: the system call result, or -1 with an error code in errno
31af254a27SRichard Henderson  * (Errnos are host errnos; we rely on QEMU_ERESTARTSYS not clashing
32a57e0c36SPeter Maydell  * with any of the host errno values.)
33a57e0c36SPeter Maydell  */
34a57e0c36SPeter Maydell 
35a57e0c36SPeter Maydell /*
36a57e0c36SPeter Maydell  * A guide to using safe_syscall() to handle interactions between guest
37a57e0c36SPeter Maydell  * syscalls and guest signals:
38a57e0c36SPeter Maydell  *
39a57e0c36SPeter Maydell  * Guest syscalls come in two flavours:
40a57e0c36SPeter Maydell  *
41a57e0c36SPeter Maydell  * (1) Non-interruptible syscalls
42a57e0c36SPeter Maydell  *
43a57e0c36SPeter Maydell  * These are guest syscalls that never get interrupted by signals and
44a57e0c36SPeter Maydell  * so never return EINTR. They can be implemented straightforwardly in
45a57e0c36SPeter Maydell  * QEMU: just make sure that if the implementation code has to make any
46a57e0c36SPeter Maydell  * blocking calls that those calls are retried if they return EINTR.
47a57e0c36SPeter Maydell  * It's also OK to implement these with safe_syscall, though it will be
48a57e0c36SPeter Maydell  * a little less efficient if a signal is delivered at the 'wrong' moment.
49a57e0c36SPeter Maydell  *
50a57e0c36SPeter Maydell  * Some non-interruptible syscalls need to be handled using block_signals()
51a57e0c36SPeter Maydell  * to block signals for the duration of the syscall. This mainly applies
52a57e0c36SPeter Maydell  * to code which needs to modify the data structures used by the
53a57e0c36SPeter Maydell  * host_signal_handler() function and the functions it calls, including
54a57e0c36SPeter Maydell  * all syscalls which change the thread's signal mask.
55a57e0c36SPeter Maydell  *
56a57e0c36SPeter Maydell  * (2) Interruptible syscalls
57a57e0c36SPeter Maydell  *
58a57e0c36SPeter Maydell  * These are guest syscalls that can be interrupted by signals and
59a57e0c36SPeter Maydell  * for which we need to either return EINTR or arrange for the guest
60a57e0c36SPeter Maydell  * syscall to be restarted. This category includes both syscalls which
61a57e0c36SPeter Maydell  * always restart (and in the kernel return -ERESTARTNOINTR), ones
62a57e0c36SPeter Maydell  * which only restart if there is no handler (kernel returns -ERESTARTNOHAND
63a57e0c36SPeter Maydell  * or -ERESTART_RESTARTBLOCK), and the most common kind which restart
64a57e0c36SPeter Maydell  * if the handler was registered with SA_RESTART (kernel returns
65a57e0c36SPeter Maydell  * -ERESTARTSYS). System calls which are only interruptible in some
66a57e0c36SPeter Maydell  * situations (like 'open') also need to be handled this way.
67a57e0c36SPeter Maydell  *
68a57e0c36SPeter Maydell  * Here it is important that the host syscall is made
69a57e0c36SPeter Maydell  * via this safe_syscall() function, and *not* via the host libc.
70a57e0c36SPeter Maydell  * If the host libc is used then the implementation will appear to work
71a57e0c36SPeter Maydell  * most of the time, but there will be a race condition where a
72a57e0c36SPeter Maydell  * signal could arrive just before we make the host syscall inside libc,
73*7a21bee2SDaniel P. Berrangé  * and then the guest syscall will not correctly be interrupted.
74a57e0c36SPeter Maydell  * Instead the implementation of the guest syscall can use the safe_syscall
75a57e0c36SPeter Maydell  * function but otherwise just return the result or errno in the usual
76a57e0c36SPeter Maydell  * way; the main loop code will take care of restarting the syscall
77a57e0c36SPeter Maydell  * if appropriate.
78a57e0c36SPeter Maydell  *
79a57e0c36SPeter Maydell  * (If the implementation needs to make multiple host syscalls this is
80a57e0c36SPeter Maydell  * OK; any which might really block must be via safe_syscall(); for those
81a57e0c36SPeter Maydell  * which are only technically blocking (ie which we know in practice won't
82a57e0c36SPeter Maydell  * stay in the host kernel indefinitely) it's OK to use libc if necessary.
83a57e0c36SPeter Maydell  * You must be able to cope with backing out correctly if some safe_syscall
84af254a27SRichard Henderson  * you make in the implementation returns either -QEMU_ERESTARTSYS or
85a57e0c36SPeter Maydell  * EINTR though.)
86a57e0c36SPeter Maydell  *
87a57e0c36SPeter Maydell  * block_signals() cannot be used for interruptible syscalls.
88a57e0c36SPeter Maydell  *
89a57e0c36SPeter Maydell  *
90a57e0c36SPeter Maydell  * How and why the safe_syscall implementation works:
91a57e0c36SPeter Maydell  *
92a57e0c36SPeter Maydell  * The basic setup is that we make the host syscall via a known
93a57e0c36SPeter Maydell  * section of host native assembly. If a signal occurs, our signal
94a57e0c36SPeter Maydell  * handler checks the interrupted host PC against the addresse of that
95a57e0c36SPeter Maydell  * known section. If the PC is before or at the address of the syscall
96a57e0c36SPeter Maydell  * instruction then we change the PC to point at a "return
97af254a27SRichard Henderson  * -QEMU_ERESTARTSYS" code path instead, and then exit the signal handler
98a57e0c36SPeter Maydell  * (causing the safe_syscall() call to immediately return that value).
99a57e0c36SPeter Maydell  * Then in the main.c loop if we see this magic return value we adjust
100a57e0c36SPeter Maydell  * the guest PC to wind it back to before the system call, and invoke
101a57e0c36SPeter Maydell  * the guest signal handler as usual.
102a57e0c36SPeter Maydell  *
103a57e0c36SPeter Maydell  * This winding-back will happen in two cases:
104a57e0c36SPeter Maydell  * (1) signal came in just before we took the host syscall (a race);
105a57e0c36SPeter Maydell  *   in this case we'll take the guest signal and have another go
106a57e0c36SPeter Maydell  *   at the syscall afterwards, and this is indistinguishable for the
107a57e0c36SPeter Maydell  *   guest from the timing having been different such that the guest
108a57e0c36SPeter Maydell  *   signal really did win the race
109a57e0c36SPeter Maydell  * (2) signal came in while the host syscall was blocking, and the
110a57e0c36SPeter Maydell  *   host kernel decided the syscall should be restarted;
111a57e0c36SPeter Maydell  *   in this case we want to restart the guest syscall also, and so
112a57e0c36SPeter Maydell  *   rewinding is the right thing. (Note that "restart" semantics mean
113a57e0c36SPeter Maydell  *   "first call the signal handler, then reattempt the syscall".)
114a57e0c36SPeter Maydell  * The other situation to consider is when a signal came in while the
115a57e0c36SPeter Maydell  * host syscall was blocking, and the host kernel decided that the syscall
116a57e0c36SPeter Maydell  * should not be restarted; in this case QEMU's host signal handler will
117a57e0c36SPeter Maydell  * be invoked with the PC pointing just after the syscall instruction,
118a57e0c36SPeter Maydell  * with registers indicating an EINTR return; the special code in the
119a57e0c36SPeter Maydell  * handler will not kick in, and we will return EINTR to the guest as
120a57e0c36SPeter Maydell  * we should.
121a57e0c36SPeter Maydell  *
122a57e0c36SPeter Maydell  * Notice that we can leave the host kernel to make the decision for
123a57e0c36SPeter Maydell  * us about whether to do a restart of the syscall or not; we do not
124a57e0c36SPeter Maydell  * need to check SA_RESTART flags in QEMU or distinguish the various
125a57e0c36SPeter Maydell  * kinds of restartability.
126a57e0c36SPeter Maydell  */
1270a7e0190SRichard Henderson 
128a57e0c36SPeter Maydell /* The core part of this function is implemented in assembly */
129a57e0c36SPeter Maydell extern long safe_syscall_base(int *pending, long number, ...);
130a3310c03SRichard Henderson extern long safe_syscall_set_errno_tail(int value);
131a3310c03SRichard Henderson 
13207637888SWarner Losh /* These are defined by the safe-syscall.inc.S file */
13307637888SWarner Losh extern char safe_syscall_start[];
13407637888SWarner Losh extern char safe_syscall_end[];
135a57e0c36SPeter Maydell 
136a57e0c36SPeter Maydell #define safe_syscall(...)                                                 \
137a3310c03SRichard Henderson     safe_syscall_base(&((TaskState *)thread_cpu->opaque)->signal_pending, \
138a3310c03SRichard Henderson                       __VA_ARGS__)
139a57e0c36SPeter Maydell 
140a57e0c36SPeter Maydell #endif
141