Blame sysdeps/unix/sysv/linux/ia64/__sigstack_longjmp.c

Packit 6c4009
/* Copyright (C) 2004-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* The public __longjmp() implementation is limited to jumping within
Packit 6c4009
   the same stack.  That is, in general it is not possible to use this
Packit 6c4009
   __longjmp() implementation to cross from one stack to another.
Packit 6c4009
   In contrast, the __sigstack_longjmp() implemented here allows
Packit 6c4009
   crossing from the alternate signal stack to the normal stack
Packit 6c4009
   as a special case.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <setjmp.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <sys/rse.h>
Packit 6c4009
Packit 6c4009
#define JB_SP	0
Packit 6c4009
#define JB_BSP	17
Packit 6c4009
Packit 6c4009
struct rbs_flush_values
Packit 6c4009
  {
Packit 6c4009
    unsigned long bsp;
Packit 6c4009
    unsigned long rsc;
Packit 6c4009
    unsigned long rnat;
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
extern struct rbs_flush_values __ia64_flush_rbs (void);
Packit 6c4009
extern void __ia64_longjmp (__jmp_buf buf, int val, long rnat, long rsc)
Packit 6c4009
     __attribute__ ((__noreturn__));
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
copy_rbs (unsigned long *dst, unsigned long *dst_end, unsigned long dst_rnat,
Packit 6c4009
	  unsigned long *src, unsigned long *src_end,
Packit 6c4009
	  unsigned long current_rnat)
Packit 6c4009
{
Packit 6c4009
  unsigned long dst_slot, src_rnat = 0, src_slot, *src_rnat_addr, nat_bit;
Packit 6c4009
  int first_time = 1;
Packit 6c4009
Packit 6c4009
  while (dst < dst_end)
Packit 6c4009
    {
Packit 6c4009
      dst_slot = ia64_rse_slot_num (dst);
Packit 6c4009
      if (dst_slot == 63)
Packit 6c4009
	{
Packit 6c4009
	  *dst++ = dst_rnat;
Packit 6c4009
	  dst_rnat = 0;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* read source value, including NaT bit: */
Packit 6c4009
	  src_slot = ia64_rse_slot_num (src);
Packit 6c4009
	  if (src_slot == 63)
Packit 6c4009
	    {
Packit 6c4009
	      /* skip src RNaT slot */
Packit 6c4009
	      ++src;
Packit 6c4009
	      src_slot = 0;
Packit 6c4009
	    }
Packit 6c4009
	  if (first_time || src_slot == 0)
Packit 6c4009
	    {
Packit 6c4009
	      first_time = 0;
Packit 6c4009
	      src_rnat_addr = ia64_rse_rnat_addr (src);
Packit 6c4009
	      if (src_rnat_addr < src_end)
Packit 6c4009
		src_rnat = *src_rnat_addr;
Packit 6c4009
	      else
Packit 6c4009
		src_rnat = current_rnat;
Packit 6c4009
	    }
Packit 6c4009
	  nat_bit = (src_rnat >> src_slot) & 1;
Packit 6c4009
Packit 6c4009
	  assert (src < src_end);
Packit 6c4009
Packit 6c4009
	  *dst++ = *src++;
Packit 6c4009
	  if (nat_bit)
Packit 6c4009
	    dst_rnat |=  (1UL << dst_slot);
Packit 6c4009
	  else
Packit 6c4009
	    dst_rnat &= ~(1UL << dst_slot);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  dst_slot = ia64_rse_slot_num (dst);
Packit 6c4009
  if (dst_slot > 0)
Packit 6c4009
    *ia64_rse_rnat_addr (dst) = dst_rnat;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
__sigstack_longjmp (__jmp_buf buf, int val)
Packit 6c4009
{
Packit 6c4009
  unsigned long *rbs_base, *bsp, *bspstore, *jb_bsp, jb_sp, ss_sp;
Packit 6c4009
  unsigned long ndirty, rnat, load_rnat, *jb_rnat_addr;
Packit 6c4009
  struct sigcontext *sc;
Packit 6c4009
  stack_t stk;
Packit 6c4009
  struct rbs_flush_values c;
Packit 6c4009
Packit 6c4009
  /* put RSE into enforced-lazy mode and return current bsp/rsc/rnat: */
Packit 6c4009
  c = __ia64_flush_rbs ();
Packit 6c4009
Packit 6c4009
  jb_sp  = ((unsigned long *)  buf)[JB_SP];
Packit 6c4009
  jb_bsp = ((unsigned long **) buf)[JB_BSP];
Packit 6c4009
Packit 6c4009
  INTERNAL_SYSCALL_DECL (err);
Packit 6c4009
  (void) INTERNAL_SYSCALL (sigaltstack, err, 2, NULL, &stk;;
Packit 6c4009
Packit 6c4009
  ss_sp = (unsigned long) stk.ss_sp;
Packit 6c4009
  jb_rnat_addr = ia64_rse_rnat_addr (jb_bsp);
Packit 6c4009
Packit 6c4009
  if ((stk.ss_flags & SS_ONSTACK) == 0 || jb_sp - ss_sp < stk.ss_size)
Packit 6c4009
    /* Normal non-stack-crossing longjmp; if the RNaT slot for the bsp
Packit 6c4009
       saved in the jump-buffer is the same as the one for the current
Packit 6c4009
       BSP, use the current AR.RNAT value, otherwise, load it from the
Packit 6c4009
       jump-buffer's RNaT-slot.  */
Packit 6c4009
    load_rnat = (ia64_rse_rnat_addr ((unsigned long *) c.bsp) != jb_rnat_addr);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* If we are on the alternate signal-stack and the jump-buffer
Packit 6c4009
	 lies outside the signal-stack, we may need to copy back the
Packit 6c4009
	 dirty partition which was torn off and saved on the
Packit 6c4009
	 signal-stack when the signal was delivered.
Packit 6c4009
Packit 6c4009
	 Caveat: we assume that the top of the alternate signal-stack
Packit 6c4009
		 stores the sigcontext structure of the signal that
Packit 6c4009
		 caused the switch to the signal-stack.	 This should
Packit 6c4009
		 be a fairly safe assumption but the kernel _could_
Packit 6c4009
		 do things differently.. */
Packit 6c4009
      sc = ((struct sigcontext *) ((ss_sp + stk.ss_size) & -16) - 1);
Packit 6c4009
Packit 6c4009
      /* As a sanity-check, verify that the register-backing-store base
Packit 6c4009
	 of the alternate signal-stack is where we expect it.  */
Packit 6c4009
      rbs_base = (unsigned long *)
Packit 6c4009
	((ss_sp + sizeof (long) - 1) & -sizeof (long));
Packit 6c4009
Packit 6c4009
      assert ((unsigned long) rbs_base == sc->sc_rbs_base);
Packit 6c4009
Packit 6c4009
      ndirty = ia64_rse_num_regs (rbs_base, rbs_base + (sc->sc_loadrs >> 19));
Packit 6c4009
      bsp = (unsigned long *) sc->sc_ar_bsp;
Packit 6c4009
      bspstore = ia64_rse_skip_regs (bsp, -ndirty);
Packit 6c4009
Packit 6c4009
      if (bspstore < jb_bsp)
Packit 6c4009
	/* AR.BSPSTORE at the time of the signal was below the value
Packit 6c4009
	   of AR.BSP saved in the jump-buffer => copy the missing
Packit 6c4009
	   portion from the torn off dirty partition which got saved
Packit 6c4009
	   on the alternate signal-stack.  */
Packit 6c4009
	copy_rbs (bspstore, jb_bsp, sc->sc_ar_rnat,
Packit 6c4009
		  rbs_base, (unsigned long *) c.bsp, c.rnat);
Packit 6c4009
Packit 6c4009
      load_rnat = 1;
Packit 6c4009
    }
Packit 6c4009
  if (load_rnat)
Packit 6c4009
    rnat = *jb_rnat_addr;
Packit 6c4009
  else
Packit 6c4009
    rnat = c.rnat;
Packit 6c4009
  __ia64_longjmp (buf, val, rnat, c.rsc);
Packit 6c4009
}