Blame backends/s390_unwind.c

Packit 032894
/* Get previous frame state for an existing frame state.
Packit 032894
   Copyright (C) 2013 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <stdlib.h>
Packit 032894
#include <assert.h>
Packit 032894
Packit 032894
#define BACKEND s390_
Packit 032894
#include "libebl_CPU.h"
Packit 032894
Packit 032894
/* s390/s390x do not annotate signal handler frame by CFI.  It would be also
Packit 032894
   difficult as PC points into a stub built on stack.  Function below is called
Packit 032894
   only if unwinder could not find CFI.  Function then verifies the register
Packit 032894
   state for this frame really belongs to a signal frame.  In such case it
Packit 032894
   fetches original registers saved by the signal frame.  */
Packit 032894
Packit 032894
bool
Packit 032894
s390_unwind (Ebl *ebl, Dwarf_Addr pc, ebl_tid_registers_t *setfunc,
Packit 032894
	     ebl_tid_registers_get_t *getfunc, ebl_pid_memory_read_t *readfunc,
Packit 032894
	     void *arg, bool *signal_framep)
Packit 032894
{
Packit 032894
  /* Caller already assumed caller adjustment but S390 instructions are 4 bytes
Packit 032894
     long.  Undo it.  */
Packit 032894
  if ((pc & 0x3) != 0x3)
Packit 032894
    return false;
Packit 032894
  pc++;
Packit 032894
  /* We can assume big-endian read here.  */
Packit 032894
  Dwarf_Word instr;
Packit 032894
  if (! readfunc (pc, &instr, arg))
Packit 032894
    return false;
Packit 032894
  /* Fetch only the very first two bytes.  */
Packit 032894
  instr = (instr >> (ebl->class == ELFCLASS64 ? 48 : 16)) & 0xffff;
Packit 032894
  /* See GDB s390_sigtramp_frame_sniffer.  */
Packit 032894
  /* Check for 'svc' as the first instruction.  */
Packit 032894
  if (((instr >> 8) & 0xff) != 0x0a)
Packit 032894
    return false;
Packit 032894
  /* Check for 'sigreturn' or 'rt_sigreturn' as the second instruction.  */
Packit 032894
  if ((instr & 0xff) != 119 && (instr & 0xff) != 173)
Packit 032894
    return false;
Packit 032894
  /* See GDB s390_sigtramp_frame_unwind_cache.  */
Packit 032894
  Dwarf_Word this_sp;
Packit 032894
  if (! getfunc (0 + 15, 1, &this_sp, arg))
Packit 032894
    return false;
Packit 032894
  unsigned word_size = ebl->class == ELFCLASS64 ? 8 : 4;
Packit 032894
  Dwarf_Addr next_cfa = this_sp + 16 * word_size + 32;
Packit 032894
  /* "New-style RT frame" is not supported,
Packit 032894
     assuming "Old-style RT frame and all non-RT frames".
Packit 032894
     Pointer to the array of saved registers is at NEXT_CFA + 8.  */
Packit 032894
  Dwarf_Word sigreg_ptr;
Packit 032894
  if (! readfunc (next_cfa + 8, &sigreg_ptr, arg))
Packit 032894
    return false;
Packit 032894
  /* Skip PSW mask.  */
Packit 032894
  sigreg_ptr += word_size;
Packit 032894
  /* Read PSW address.  */
Packit 032894
  Dwarf_Word val;
Packit 032894
  if (! readfunc (sigreg_ptr, &val, arg))
Packit 032894
    return false;
Packit 032894
  if (! setfunc (-1, 1, &val, arg))
Packit 032894
    return false;
Packit 032894
  sigreg_ptr += word_size;
Packit 032894
  /* Then the GPRs.  */
Packit 032894
  Dwarf_Word gprs[16];
Packit 032894
  for (int i = 0; i < 16; i++)
Packit 032894
    {
Packit 032894
      if (! readfunc (sigreg_ptr, &gprs[i], arg))
Packit 032894
	return false;
Packit 032894
      sigreg_ptr += word_size;
Packit 032894
    }
Packit 032894
  /* Then the ACRs.  Skip them, they are not used in CFI.  */
Packit 032894
  for (int i = 0; i < 16; i++)
Packit 032894
    sigreg_ptr += 4;
Packit 032894
  /* The floating-point control word.  */
Packit 032894
  sigreg_ptr += 8;
Packit 032894
  /* And finally the FPRs.  */
Packit 032894
  Dwarf_Word fprs[16];
Packit 032894
  for (int i = 0; i < 16; i++)
Packit 032894
    {
Packit 032894
      if (! readfunc (sigreg_ptr, &val, arg))
Packit 032894
	return false;
Packit 032894
      if (ebl->class == ELFCLASS32)
Packit 032894
	{
Packit 032894
	  Dwarf_Addr val_low;
Packit 032894
	  if (! readfunc (sigreg_ptr + 4, &val_low, arg))
Packit 032894
	    return false;
Packit 032894
	  val = (val << 32) | val_low;
Packit 032894
	}
Packit 032894
      fprs[i] = val;
Packit 032894
      sigreg_ptr += 8;
Packit 032894
    }
Packit 032894
  /* If we have them, the GPR upper halves are appended at the end.  */
Packit 032894
  if (ebl->class == ELFCLASS32)
Packit 032894
    {
Packit 032894
      /* Skip signal number.  */
Packit 032894
      sigreg_ptr += 4;
Packit 032894
      for (int i = 0; i < 16; i++)
Packit 032894
	{
Packit 032894
	  if (! readfunc (sigreg_ptr, &val, arg))
Packit 032894
	    return false;
Packit 032894
	  Dwarf_Word val_low = gprs[i];
Packit 032894
	  val = (val << 32) | val_low;
Packit 032894
	  gprs[i] = val;
Packit 032894
	  sigreg_ptr += 4;
Packit 032894
	}
Packit 032894
    }
Packit 032894
  if (! setfunc (0, 16, gprs, arg))
Packit 032894
    return false;
Packit 032894
  if (! setfunc (16, 16, fprs, arg))
Packit 032894
    return false;
Packit 032894
  *signal_framep = true;
Packit 032894
  return true;
Packit 032894
}