1/* 2 * safe-syscall.inc.S : host-specific assembly fragment 3 * to handle signals occurring at the same time as system calls. 4 * This is intended to be included by common-user/safe-syscall.S 5 * 6 * Written by Richard Henderson <rth@twiddle.net> 7 * Copyright (C) 2016 Red Hat, Inc. 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 .global safe_syscall_base 14 .global safe_syscall_start 15 .global safe_syscall_end 16 .type safe_syscall_base, @function 17 18 /* This is the entry point for making a system call. The calling 19 * convention here is that of a C varargs function with the 20 * first argument an 'int *' to the signal_pending flag, the 21 * second one the system call number (as a 'long'), and all further 22 * arguments being syscall arguments (also 'long'). 23 */ 24safe_syscall_base: 25 .cfi_startproc 26 push %ebp 27 .cfi_adjust_cfa_offset 4 28 .cfi_rel_offset ebp, 0 29 push %esi 30 .cfi_adjust_cfa_offset 4 31 .cfi_rel_offset esi, 0 32 push %edi 33 .cfi_adjust_cfa_offset 4 34 .cfi_rel_offset edi, 0 35 push %ebx 36 .cfi_adjust_cfa_offset 4 37 .cfi_rel_offset ebx, 0 38 39 /* The syscall calling convention isn't the same as the C one: 40 * we enter with 0(%esp) == return address 41 * 4(%esp) == &signal_pending 42 * 8(%esp) == syscall number 43 * 12(%esp) ... 32(%esp) == syscall arguments 44 * and return the result in eax 45 * and the syscall instruction needs 46 * eax == syscall number 47 * ebx, ecx, edx, esi, edi, ebp == syscall arguments 48 * and returns the result in eax 49 * Shuffle everything around appropriately. 50 * Note the 16 bytes that we pushed to save registers. 51 */ 52 mov 12+16(%esp), %ebx /* the syscall arguments */ 53 mov 16+16(%esp), %ecx 54 mov 20+16(%esp), %edx 55 mov 24+16(%esp), %esi 56 mov 28+16(%esp), %edi 57 mov 32+16(%esp), %ebp 58 59 /* This next sequence of code works in conjunction with the 60 * rewind_if_safe_syscall_function(). If a signal is taken 61 * and the interrupted PC is anywhere between 'safe_syscall_start' 62 * and 'safe_syscall_end' then we rewind it to 'safe_syscall_start'. 63 * The code sequence must therefore be able to cope with this, and 64 * the syscall instruction must be the final one in the sequence. 65 */ 66safe_syscall_start: 67 /* if signal_pending is non-zero, don't do the call */ 68 mov 4+16(%esp), %eax /* signal_pending */ 69 cmpl $0, (%eax) 70 jnz 2f 71 mov 8+16(%esp), %eax /* syscall number */ 72 int $0x80 73safe_syscall_end: 74 75 /* code path for having successfully executed the syscall */ 76#if defined(__linux__) 77 /* Linux kernel returns (small) negative errno. */ 78 cmp $-4095, %eax 79 jae 0f 80#elif defined(__FreeBSD__) 81 /* FreeBSD kernel returns positive errno and C bit set. */ 82 jc 1f 83#else 84#error "unsupported os" 85#endif 86 pop %ebx 87 .cfi_remember_state 88 .cfi_adjust_cfa_offset -4 89 .cfi_restore ebx 90 pop %edi 91 .cfi_adjust_cfa_offset -4 92 .cfi_restore edi 93 pop %esi 94 .cfi_adjust_cfa_offset -4 95 .cfi_restore esi 96 pop %ebp 97 .cfi_adjust_cfa_offset -4 98 .cfi_restore ebp 99 ret 100 .cfi_restore_state 101 102#if defined(__linux__) 1030: neg %eax 104 jmp 1f 105#endif 106 107 /* code path when we didn't execute the syscall */ 1082: mov $QEMU_ERESTARTSYS, %eax 109 110 /* code path setting errno */ 1111: pop %ebx 112 .cfi_adjust_cfa_offset -4 113 .cfi_restore ebx 114 pop %edi 115 .cfi_adjust_cfa_offset -4 116 .cfi_restore edi 117 pop %esi 118 .cfi_adjust_cfa_offset -4 119 .cfi_restore esi 120 pop %ebp 121 .cfi_adjust_cfa_offset -4 122 .cfi_restore ebp 123 mov %eax, 4(%esp) 124 jmp safe_syscall_set_errno_tail 125 126 .cfi_endproc 127 .size safe_syscall_base, .-safe_syscall_base 128