(root)/
glibc-2.38/
sysdeps/
unix/
sysv/
linux/
ia64/
__sigstack_longjmp.c
       1  /* Copyright (C) 2004-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  /* The public __longjmp() implementation is limited to jumping within
      19     the same stack.  That is, in general it is not possible to use this
      20     __longjmp() implementation to cross from one stack to another.
      21     In contrast, the __sigstack_longjmp() implemented here allows
      22     crossing from the alternate signal stack to the normal stack
      23     as a special case.  */
      24  
      25  #include <assert.h>
      26  #include <setjmp.h>
      27  #include <signal.h>
      28  #include <stdint.h>
      29  #include <stdlib.h>
      30  
      31  #include <sysdep.h>
      32  #include <sys/rse.h>
      33  
      34  #define JB_SP	0
      35  #define JB_BSP	17
      36  
      37  struct rbs_flush_values
      38    {
      39      unsigned long bsp;
      40      unsigned long rsc;
      41      unsigned long rnat;
      42    };
      43  
      44  extern struct rbs_flush_values __ia64_flush_rbs (void);
      45  extern void __ia64_longjmp (__jmp_buf buf, int val, long rnat, long rsc)
      46       __attribute__ ((__noreturn__));
      47  
      48  static void
      49  copy_rbs (unsigned long *dst, unsigned long *dst_end, unsigned long dst_rnat,
      50  	  unsigned long *src, unsigned long *src_end,
      51  	  unsigned long current_rnat)
      52  {
      53    unsigned long dst_slot, src_rnat = 0, src_slot, *src_rnat_addr, nat_bit;
      54    int first_time = 1;
      55  
      56    while (dst < dst_end)
      57      {
      58        dst_slot = ia64_rse_slot_num (dst);
      59        if (dst_slot == 63)
      60  	{
      61  	  *dst++ = dst_rnat;
      62  	  dst_rnat = 0;
      63  	}
      64        else
      65  	{
      66  	  /* read source value, including NaT bit: */
      67  	  src_slot = ia64_rse_slot_num (src);
      68  	  if (src_slot == 63)
      69  	    {
      70  	      /* skip src RNaT slot */
      71  	      ++src;
      72  	      src_slot = 0;
      73  	    }
      74  	  if (first_time || src_slot == 0)
      75  	    {
      76  	      first_time = 0;
      77  	      src_rnat_addr = ia64_rse_rnat_addr (src);
      78  	      if (src_rnat_addr < src_end)
      79  		src_rnat = *src_rnat_addr;
      80  	      else
      81  		src_rnat = current_rnat;
      82  	    }
      83  	  nat_bit = (src_rnat >> src_slot) & 1;
      84  
      85  	  assert (src < src_end);
      86  
      87  	  *dst++ = *src++;
      88  	  if (nat_bit)
      89  	    dst_rnat |=  (1UL << dst_slot);
      90  	  else
      91  	    dst_rnat &= ~(1UL << dst_slot);
      92  	}
      93      }
      94    dst_slot = ia64_rse_slot_num (dst);
      95    if (dst_slot > 0)
      96      *ia64_rse_rnat_addr (dst) = dst_rnat;
      97  }
      98  
      99  void
     100  __sigstack_longjmp (__jmp_buf buf, int val)
     101  {
     102    unsigned long *rbs_base, *bsp, *bspstore, *jb_bsp, jb_sp, ss_sp;
     103    unsigned long ndirty, rnat, load_rnat, *jb_rnat_addr;
     104    struct sigcontext *sc;
     105    stack_t stk;
     106    struct rbs_flush_values c;
     107  
     108    /* put RSE into enforced-lazy mode and return current bsp/rsc/rnat: */
     109    c = __ia64_flush_rbs ();
     110  
     111    jb_sp  = ((unsigned long *)  buf)[JB_SP];
     112    jb_bsp = ((unsigned long **) buf)[JB_BSP];
     113  
     114    INTERNAL_SYSCALL_CALL (sigaltstack, NULL, &stk);
     115  
     116    ss_sp = (unsigned long) stk.ss_sp;
     117    jb_rnat_addr = ia64_rse_rnat_addr (jb_bsp);
     118  
     119    if ((stk.ss_flags & SS_ONSTACK) == 0 || jb_sp - ss_sp < stk.ss_size)
     120      /* Normal non-stack-crossing longjmp; if the RNaT slot for the bsp
     121         saved in the jump-buffer is the same as the one for the current
     122         BSP, use the current AR.RNAT value, otherwise, load it from the
     123         jump-buffer's RNaT-slot.  */
     124      load_rnat = (ia64_rse_rnat_addr ((unsigned long *) c.bsp) != jb_rnat_addr);
     125    else
     126      {
     127        /* If we are on the alternate signal-stack and the jump-buffer
     128  	 lies outside the signal-stack, we may need to copy back the
     129  	 dirty partition which was torn off and saved on the
     130  	 signal-stack when the signal was delivered.
     131  
     132  	 Caveat: we assume that the top of the alternate signal-stack
     133  		 stores the sigcontext structure of the signal that
     134  		 caused the switch to the signal-stack.	 This should
     135  		 be a fairly safe assumption but the kernel _could_
     136  		 do things differently.. */
     137        sc = ((struct sigcontext *) ((ss_sp + stk.ss_size) & -16) - 1);
     138  
     139        /* As a sanity-check, verify that the register-backing-store base
     140  	 of the alternate signal-stack is where we expect it.  */
     141        rbs_base = (unsigned long *)
     142  	((ss_sp + sizeof (long) - 1) & -sizeof (long));
     143  
     144        assert ((unsigned long) rbs_base == sc->sc_rbs_base);
     145  
     146        ndirty = ia64_rse_num_regs (rbs_base, rbs_base + (sc->sc_loadrs >> 19));
     147        bsp = (unsigned long *) sc->sc_ar_bsp;
     148        bspstore = ia64_rse_skip_regs (bsp, -ndirty);
     149  
     150        if (bspstore < jb_bsp)
     151  	/* AR.BSPSTORE at the time of the signal was below the value
     152  	   of AR.BSP saved in the jump-buffer => copy the missing
     153  	   portion from the torn off dirty partition which got saved
     154  	   on the alternate signal-stack.  */
     155  	copy_rbs (bspstore, jb_bsp, sc->sc_ar_rnat,
     156  		  rbs_base, (unsigned long *) c.bsp, c.rnat);
     157  
     158        load_rnat = 1;
     159      }
     160    if (load_rnat)
     161      rnat = *jb_rnat_addr;
     162    else
     163      rnat = c.rnat;
     164    __ia64_longjmp (buf, val, rnat, c.rsc);
     165  }