1  /* Machine-dependent details of interruptible RPC messaging.  i386 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    asm (".globl _hurd_intr_rpc_msg_about_to\n"				      \
      28         ".globl _hurd_intr_rpc_msg_setup_done\n"				      \
      29         ".globl _hurd_intr_rpc_msg_in_trap\n"				      \
      30         /* Clear eax before we do the check for cancel below.  This is to
      31            detect eax being set to non-zero (actually MACH_SEND_INTERRUPTED)
      32            from the outside (namely, _hurdsig_abort_rpcs), which signals us
      33            to skip the trap we were about to enter.  */			      \
      34         "				xorl %0, %0\n"			      \
      35         "_hurd_intr_rpc_msg_about_to:"					      \
      36         /* We need to make a last check of cancel, in case we got interrupted
      37            right before _hurd_intr_rpc_msg_about_to.  */			      \
      38         "				cmpl $0, %5\n"			      \
      39         "				jz _hurd_intr_rpc_msg_do\n"	      \
      40         /* We got interrupted, note so and return EINTR.  */		      \
      41         "				movl $0, %3\n"			      \
      42         "				movl %6, %0\n"			      \
      43         "				jmp _hurd_intr_rpc_msg_sp_restored\n" \
      44         "_hurd_intr_rpc_msg_do:"						      \
      45         /* Ok, push the mach_msg_trap arguments and a fake return address.  */ \
      46         "				pushl 24(%4)\n"			      \
      47         "				pushl %2\n"			      \
      48         "				pushl 16(%4)\n"			      \
      49         "				pushl 12(%4)\n"			      \
      50         "				pushl 8(%4)\n"			      \
      51         "				pushl %1\n"			      \
      52         "				pushl (%4)\n"			      \
      53         "				pushl $0\n"			      \
      54         "_hurd_intr_rpc_msg_setup_done:"					      \
      55         /* From here on, it is safe to make us jump over the syscall.  Now
      56            check if we have been told to skip the syscall while running
      57            the above.  */						      \
      58         "				test %0, %0\n"			      \
      59         "				jnz _hurd_intr_rpc_msg_in_trap\n"     \
      60         /* Do the actual syscall.  */					      \
      61         "				movl $-25, %%eax\n"		      \
      62         "_hurd_intr_rpc_msg_do_trap:	lcall $7, $0 # status in %0\n"	      \
      63         "_hurd_intr_rpc_msg_in_trap:"					      \
      64         /* Ok, clean the arguments and update OPTION and TIMEOUT.  */	      \
      65         "				addl $8, %%esp\n"		      \
      66         "				popl %1\n"			      \
      67         "				addl $12, %%esp\n"		      \
      68         "				popl %2\n"			      \
      69         "				addl $4, %%esp\n"		      \
      70         "_hurd_intr_rpc_msg_sp_restored:"				      \
      71         : "=&a" (err), "+r" (option), "+r" (timeout), "=m" (*intr_port_p)      \
      72         : "r" (&msg), "m" (*cancel_p), "i" (EINTR));			      \
      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 int c[2]; } *p = (void *) ((state)->eip - 7);	      \
      82    int result;								      \
      83    if (_hurdsig_catch_memory_fault (p))					      \
      84      return 0;								      \
      85    if (result = p->c[0] == 0x0000009a && (p->c[1] & 0x00ffffff) == 0x00000700) \
      86      /* The PC is just after an `lcall $7,$0' instruction.		      \
      87         This is a system call in progress; %eax holds the call number.  */     \
      88      *(callno) = (state)->eax;						      \
      89    _hurdsig_end_catch_fault ();						      \
      90    result;								      \
      91  })
      92  
      93  
      94  struct mach_msg_trap_args
      95    {
      96      void *retaddr;		/* Address mach_msg_trap will return to.  */
      97      /* This is the order of arguments to mach_msg_trap.  */
      98      mach_msg_header_t *msg;
      99      mach_msg_option_t option;
     100      mach_msg_size_t send_size;
     101      mach_msg_size_t rcv_size;
     102      mach_port_t rcv_name;
     103      mach_msg_timeout_t timeout;
     104      mach_port_t notify;
     105    };
     106  
     107  
     108  /* This cannot be an inline function because it calls setjmp.  */
     109  #define MSG_EXAMINE(state, msgid, rcvname, send_name, opt, tmout)	      \
     110  ({									      \
     111    const struct mach_msg_trap_args *args = (const void *) (state)->uesp;	      \
     112    mach_msg_header_t *msg;						      \
     113    _hurdsig_catch_memory_fault (args) ? -1 :				      \
     114      ({									      \
     115        msg = args->msg;							      \
     116        *(opt) = args->option;						      \
     117        *(tmout) = args->timeout;						      \
     118        *(rcvname) = args->rcv_name;					      \
     119        _hurdsig_end_catch_fault ();					      \
     120        if (msg == 0)							      \
     121  	{								      \
     122  	  *(send_name) = MACH_PORT_NULL;				      \
     123  	  *(msgid) = 0;							      \
     124  	}								      \
     125        else								      \
     126  	{								      \
     127  	  if (_hurdsig_catch_memory_fault (msg))			      \
     128  	    return -1;							      \
     129  	  *(send_name) = msg->msgh_remote_port;				      \
     130  	  *(msgid) = msg->msgh_id;					      \
     131  	  _hurdsig_end_catch_fault ();					      \
     132  	}								      \
     133        0;								      \
     134      });									      \
     135  })