14d330ceeSTimothy E Baldwin/* 24d330ceeSTimothy E Baldwin * safe-syscall.inc.S : host-specific assembly fragment 34d330ceeSTimothy E Baldwin * to handle signals occurring at the same time as system calls. 44d330ceeSTimothy E Baldwin * This is intended to be included by linux-user/safe-syscall.S 54d330ceeSTimothy E Baldwin * 64d330ceeSTimothy E Baldwin * Copyright (C) 2015 Timothy Edward Baldwin <T.E.Baldwin99@members.leeds.ac.uk> 74d330ceeSTimothy E Baldwin * 84d330ceeSTimothy E Baldwin * This work is licensed under the terms of the GNU GPL, version 2 or later. 94d330ceeSTimothy E Baldwin * See the COPYING file in the top-level directory. 104d330ceeSTimothy E Baldwin */ 114d330ceeSTimothy E Baldwin 124d330ceeSTimothy E Baldwin .global safe_syscall_base 134d330ceeSTimothy E Baldwin .global safe_syscall_start 144d330ceeSTimothy E Baldwin .global safe_syscall_end 154d330ceeSTimothy E Baldwin .type safe_syscall_base, @function 164d330ceeSTimothy E Baldwin 174d330ceeSTimothy E Baldwin /* This is the entry point for making a system call. The calling 184d330ceeSTimothy E Baldwin * convention here is that of a C varargs function with the 194d330ceeSTimothy E Baldwin * first argument an 'int *' to the signal_pending flag, the 204d330ceeSTimothy E Baldwin * second one the system call number (as a 'long'), and all further 214d330ceeSTimothy E Baldwin * arguments being syscall arguments (also 'long'). 224d330ceeSTimothy E Baldwin */ 234d330ceeSTimothy E Baldwinsafe_syscall_base: 249e024732SPeter Maydell .cfi_startproc 254d330ceeSTimothy E Baldwin /* This saves a frame pointer and aligns the stack for the syscall. 264d330ceeSTimothy E Baldwin * (It's unclear if the syscall ABI has the same stack alignment 274d330ceeSTimothy E Baldwin * requirements as the userspace function call ABI, but better safe than 284d330ceeSTimothy E Baldwin * sorry. Appendix A2 of http://www.x86-64.org/documentation/abi.pdf 294d330ceeSTimothy E Baldwin * does not list any ABI differences regarding stack alignment.) 304d330ceeSTimothy E Baldwin */ 314d330ceeSTimothy E Baldwin push %rbp 329e024732SPeter Maydell .cfi_adjust_cfa_offset 8 339e024732SPeter Maydell .cfi_rel_offset rbp, 0 344d330ceeSTimothy E Baldwin 35a3310c03SRichard Henderson /* 36a3310c03SRichard Henderson * The syscall calling convention isn't the same as the C one: 37a3310c03SRichard Henderson * we enter with rdi == &signal_pending 384d330ceeSTimothy E Baldwin * rsi == syscall number 394d330ceeSTimothy E Baldwin * rdx, rcx, r8, r9, (stack), (stack) == syscall arguments 404d330ceeSTimothy E Baldwin * and return the result in rax 414d330ceeSTimothy E Baldwin * and the syscall instruction needs 424d330ceeSTimothy E Baldwin * rax == syscall number 434d330ceeSTimothy E Baldwin * rdi, rsi, rdx, r10, r8, r9 == syscall arguments 444d330ceeSTimothy E Baldwin * and returns the result in rax 454d330ceeSTimothy E Baldwin * Shuffle everything around appropriately. 464d330ceeSTimothy E Baldwin * Note that syscall will trash rcx and r11. 474d330ceeSTimothy E Baldwin */ 484d330ceeSTimothy E Baldwin mov %rsi, %rax /* syscall number */ 494d330ceeSTimothy E Baldwin mov %rdi, %rbp /* signal_pending pointer */ 504d330ceeSTimothy E Baldwin /* and the syscall arguments */ 514d330ceeSTimothy E Baldwin mov %rdx, %rdi 524d330ceeSTimothy E Baldwin mov %rcx, %rsi 534d330ceeSTimothy E Baldwin mov %r8, %rdx 544d330ceeSTimothy E Baldwin mov %r9, %r10 554d330ceeSTimothy E Baldwin mov 16(%rsp), %r8 564d330ceeSTimothy E Baldwin mov 24(%rsp), %r9 574d330ceeSTimothy E Baldwin 584d330ceeSTimothy E Baldwin /* This next sequence of code works in conjunction with the 594d330ceeSTimothy E Baldwin * rewind_if_safe_syscall_function(). If a signal is taken 604d330ceeSTimothy E Baldwin * and the interrupted PC is anywhere between 'safe_syscall_start' 614d330ceeSTimothy E Baldwin * and 'safe_syscall_end' then we rewind it to 'safe_syscall_start'. 624d330ceeSTimothy E Baldwin * The code sequence must therefore be able to cope with this, and 634d330ceeSTimothy E Baldwin * the syscall instruction must be the final one in the sequence. 644d330ceeSTimothy E Baldwin */ 654d330ceeSTimothy E Baldwinsafe_syscall_start: 664d330ceeSTimothy E Baldwin /* if signal_pending is non-zero, don't do the call */ 674eed9990SRichard Henderson cmpl $0, (%rbp) 68a3310c03SRichard Henderson jnz 2f 694d330ceeSTimothy E Baldwin syscall 704d330ceeSTimothy E Baldwinsafe_syscall_end: 714d330ceeSTimothy E Baldwin /* code path for having successfully executed the syscall */ 72a3310c03SRichard Henderson cmp $-4095, %rax 73a3310c03SRichard Henderson jae 0f 744d330ceeSTimothy E Baldwin pop %rbp 759e024732SPeter Maydell .cfi_remember_state 769e024732SPeter Maydell .cfi_def_cfa_offset 8 779e024732SPeter Maydell .cfi_restore rbp 784d330ceeSTimothy E Baldwin ret 799e024732SPeter Maydell .cfi_restore_state 80a3310c03SRichard Henderson 81a3310c03SRichard Henderson0: neg %eax 82a3310c03SRichard Henderson jmp 1f 83a3310c03SRichard Henderson 84a3310c03SRichard Henderson /* code path when we didn't execute the syscall */ 85*af254a27SRichard Henderson2: mov $QEMU_ERESTARTSYS, %eax 86a3310c03SRichard Henderson 87a3310c03SRichard Henderson /* code path setting errno */ 88a3310c03SRichard Henderson1: pop %rbp 899e024732SPeter Maydell .cfi_def_cfa_offset 8 909e024732SPeter Maydell .cfi_restore rbp 91a3310c03SRichard Henderson jmp safe_syscall_set_errno_tail 929e024732SPeter Maydell .cfi_endproc 934d330ceeSTimothy E Baldwin 944d330ceeSTimothy E Baldwin .size safe_syscall_base, .-safe_syscall_base 95