1  /* Machine-dependent details of interruptible RPC messaging.  x86_64 version.
       2     Copyright (C) 1995-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  
      20  /* Note that we must mark OPTION and TIMEOUT as outputs of this operation,
      21     to indicate that the signal thread might mutate them as part
      22     of sending us to a signal handler.  */
      23  
      24  #define INTR_MSG_TRAP(msg, option, send_size, rcv_size, rcv_name, timeout, notify, cancel_p, intr_port_p) \
      25  ({									      \
      26    error_t err;								      \
      27    register uintptr_t rcv_size_r10 asm ("r10") = rcv_size;		      \
      28    register uintptr_t rcv_name_r8 asm ("r8") = rcv_name;			      \
      29    register uintptr_t timeout_r9 asm ("r9") = timeout;			      \
      30    asm volatile (							      \
      31         ".globl _hurd_intr_rpc_msg_about_to\n"				      \
      32         ".globl _hurd_intr_rpc_msg_setup_done\n"				      \
      33         ".globl _hurd_intr_rpc_msg_in_trap\n"				      \
      34         /* Clear rax before we do the check for cancel below.  This is to
      35            detect rax being set to non-zero (actually MACH_SEND_INTERRUPTED)
      36            from the outside (namely, _hurdsig_abort_rpcs), which signals us
      37            to skip the trap we were about to enter.  */			      \
      38         "				xor %[err], %[err]\n"		      \
      39         "_hurd_intr_rpc_msg_about_to:"					      \
      40         /* We need to make a last check of cancel, in case we got interrupted
      41            right before _hurd_intr_rpc_msg_about_to.  */			      \
      42         "				cmpl $0, %[cancel]\n"		      \
      43         "				jz _hurd_intr_rpc_msg_do\n"	      \
      44         /* We got interrupted, note so and return EINTR.  */		      \
      45         "				movl $0, %[intr_port]\n"	      \
      46         "				movl %[eintr], %[err]\n"	      \
      47         "				jmp _hurd_intr_rpc_msg_sp_restored\n" \
      48         "_hurd_intr_rpc_msg_do:"						      \
      49         /* Ok, prepare the mach_msg_trap arguments.  We pass the first 6 args
      50            in registers, and push the last one and a fake return address.  */  \
      51         "				pushq %[notify]\n"		      \
      52         "				pushq $0\n"			      \
      53         "_hurd_intr_rpc_msg_setup_done:"					      \
      54         /* From here on, it is safe to make us jump over the syscall.  Now
      55            check if we have been told to skip the syscall while running
      56            the above.  */						      \
      57         "				test %[err], %[err]\n"		      \
      58         "				jnz _hurd_intr_rpc_msg_in_trap\n"     \
      59         /* Do the actual syscall.  */					      \
      60         "				movq $-25, %%rax\n"		      \
      61         "_hurd_intr_rpc_msg_do_trap:	syscall # status in %[err]\n"	      \
      62         "_hurd_intr_rpc_msg_in_trap:"					      \
      63         /* Clean the arguments.  */					      \
      64         "				addq $16, %%rsp\n"		      \
      65         "_hurd_intr_rpc_msg_sp_restored:"				      \
      66         : [err] "=&a" (err), "+S" (option), [intr_port] "=m" (*intr_port_p),   \
      67           "+r" (timeout_r9)						      \
      68         : "D" (msg), "d" (send_size), "r" (rcv_size_r10), "r" (rcv_name_r8),   \
      69           [notify] "rm" ((uintptr_t) notify), [cancel] "m" (*cancel_p),	      \
      70           [eintr] "i" (EINTR)						      \
      71         : "rcx", "r11");							      \
      72    timeout = timeout_r9;							      \
      73    err;									      \
      74  })
      75  
      76  #include "hurdfault.h"
      77  
      78  /* This cannot be an inline function because it calls setjmp.  */
      79  #define SYSCALL_EXAMINE(state, callno)					      \
      80  ({									      \
      81    struct { unsigned char c[2]; } *p = (void *) ((state)->rip - 2);	      \
      82    int result;								      \
      83    if (_hurdsig_catch_memory_fault (p))					      \
      84      return 0;								      \
      85    if (result = p->c[0] == 0xf && p->c[1] == 0x5)			      \
      86      /* The PC appears to be just after a `syscall' instruction.		      \
      87         This is a system call in progress; %rax holds the call number.  */     \
      88      *(callno) = (state)->rax;						      \
      89    _hurdsig_end_catch_fault ();						      \
      90    result;								      \
      91  })
      92  
      93  
      94  /* This cannot be an inline function because it calls setjmp.  */
      95  #define MSG_EXAMINE(state, msgid, rcvname, send_name, opt, tmout)	      \
      96  ({									      \
      97    int ret = 0;								      \
      98    const struct machine_thread_state *s = (state);			      \
      99    const mach_msg_header_t *msg = (const void *) s->rdi;			      \
     100    *(rcvname) = s->r8;							      \
     101    *(opt) = s->rsi;							      \
     102    *(tmout) = s->r9;							      \
     103    if (msg == 0)								      \
     104      {									      \
     105        *(send_name) = MACH_PORT_NULL;					      \
     106        *(msgid) = 0;							      \
     107      }									      \
     108    else									      \
     109      {									      \
     110        ret = _hurdsig_catch_memory_fault (msg) ? -1 : 0;			      \
     111        if (ret == 0)							      \
     112          {								      \
     113            *(send_name) = msg->msgh_remote_port;				      \
     114            *(msgid) = msg->msgh_id;					      \
     115            _hurdsig_end_catch_fault ();					      \
     116  	}								      \
     117      }									      \
     118    ret;									      \
     119  })