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. 4bbf15aafSRichard Henderson * This is intended to be included by common-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: 715bfd125eSRichard Henderson 724d330ceeSTimothy E Baldwin /* code path for having successfully executed the syscall */ 735bfd125eSRichard Henderson#if defined(__linux__) 745bfd125eSRichard Henderson /* Linux kernel returns (small) negative errno. */ 75a3310c03SRichard Henderson cmp $-4095, %rax 76a3310c03SRichard Henderson jae 0f 775bfd125eSRichard Henderson#elif defined(__FreeBSD__) 785bfd125eSRichard Henderson /* FreeBSD kernel returns positive errno and C bit set. */ 795bfd125eSRichard Henderson jc 1f 805bfd125eSRichard Henderson#else 815bfd125eSRichard Henderson#error "unsupported os" 825bfd125eSRichard Henderson#endif 834d330ceeSTimothy E Baldwin pop %rbp 849e024732SPeter Maydell .cfi_remember_state 859e024732SPeter Maydell .cfi_def_cfa_offset 8 869e024732SPeter Maydell .cfi_restore rbp 874d330ceeSTimothy E Baldwin ret 889e024732SPeter Maydell .cfi_restore_state 89a3310c03SRichard Henderson 905bfd125eSRichard Henderson#if defined(__linux__) 91a3310c03SRichard Henderson0: neg %eax 92a3310c03SRichard Henderson jmp 1f 935bfd125eSRichard Henderson#endif 94a3310c03SRichard Henderson 95a3310c03SRichard Henderson /* code path when we didn't execute the syscall */ 96af254a27SRichard Henderson2: mov $QEMU_ERESTARTSYS, %eax 97a3310c03SRichard Henderson 98a3310c03SRichard Henderson /* code path setting errno */ 99a3310c03SRichard Henderson1: pop %rbp 1009e024732SPeter Maydell .cfi_def_cfa_offset 8 1019e024732SPeter Maydell .cfi_restore rbp 102*d7478d42SRichard Henderson mov %eax, %edi 103a3310c03SRichard Henderson jmp safe_syscall_set_errno_tail 1049e024732SPeter Maydell .cfi_endproc 1054d330ceeSTimothy E Baldwin 1064d330ceeSTimothy E Baldwin .size safe_syscall_base, .-safe_syscall_base 107