103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes/* Get previous frame state for an existing frame state. 203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Copyright (C) 2013 Red Hat, Inc. 303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes This file is part of elfutils. 403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes This file is free software; you can redistribute it and/or modify 603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes it under the terms of either 703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes * the GNU Lesser General Public License as published by the Free 903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Software Foundation; either version 3 of the License, or (at 1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes your option) any later version 1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes or 1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes * the GNU General Public License as published by the Free 1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Software Foundation; either version 2 of the License, or (at 1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes your option) any later version 1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes or both in parallel, as here. 1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elfutils is distributed in the hope that it will be useful, but 2103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes WITHOUT ANY WARRANTY; without even the implied warranty of 2203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 2303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes General Public License for more details. 2403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 2503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes You should have received copies of the GNU General Public License and 2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes the GNU Lesser General Public License along with this program. If 2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes not, see <http://www.gnu.org/licenses/>. */ 2803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 2903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#ifdef HAVE_CONFIG_H 3003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# include <config.h> 3103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#endif 3203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <stdlib.h> 3403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include <assert.h> 3503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#define BACKEND s390_ 3703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include "libebl_CPU.h" 3803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes/* s390/s390x do not annotate signal handler frame by CFI. It would be also 4003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes difficult as PC points into a stub built on stack. Function below is called 4103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes only if unwinder could not find CFI. Function then verifies the register 4203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes state for this frame really belongs to a signal frame. In such case it 4303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes fetches original registers saved by the signal frame. */ 4403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 4503333823c75a1c1887e923828113a1b0fd12020cElliott Hughesbool 4603333823c75a1c1887e923828113a1b0fd12020cElliott Hughess390_unwind (Ebl *ebl, Dwarf_Addr pc, ebl_tid_registers_t *setfunc, 4703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes ebl_tid_registers_get_t *getfunc, ebl_pid_memory_read_t *readfunc, 4803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes void *arg, bool *signal_framep) 4903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{ 5003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Caller already assumed caller adjustment but S390 instructions are 4 bytes 5103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes long. Undo it. */ 5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if ((pc & 0x3) != 0x3) 5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes pc++; 5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* We can assume big-endian read here. */ 5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Word instr; 5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! readfunc (pc, &instr, arg)) 5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Fetch only the very first two bytes. */ 6003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes instr = (instr >> (ebl->class == ELFCLASS64 ? 48 : 16)) & 0xffff; 6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* See GDB s390_sigtramp_frame_sniffer. */ 6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Check for 'svc' as the first instruction. */ 6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (((instr >> 8) & 0xff) != 0x0a) 6403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 6503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Check for 'sigreturn' or 'rt_sigreturn' as the second instruction. */ 6603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if ((instr & 0xff) != 119 && (instr & 0xff) != 173) 6703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* See GDB s390_sigtramp_frame_unwind_cache. */ 6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Word this_sp; 7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! getfunc (0 + 15, 1, &this_sp, arg)) 7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 7203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes unsigned word_size = ebl->class == ELFCLASS64 ? 8 : 4; 7303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Addr next_cfa = this_sp + 16 * word_size + 32; 7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* "New-style RT frame" is not supported, 7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes assuming "Old-style RT frame and all non-RT frames". 7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Pointer to the array of saved registers is at NEXT_CFA + 8. */ 7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Word sigreg_ptr; 7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! readfunc (next_cfa + 8, &sigreg_ptr, arg)) 7903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 8003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Skip PSW mask. */ 8103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += word_size; 8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Read PSW address. */ 8303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Word val; 8403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! readfunc (sigreg_ptr, &val, arg)) 8503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 8603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! setfunc (-1, 1, &val, arg)) 8703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 8803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += word_size; 8903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Then the GPRs. */ 9003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Word gprs[16]; 9103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (int i = 0; i < 16; i++) 9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! readfunc (sigreg_ptr, &gprs[i], arg)) 9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += word_size; 9603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 9703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Then the ACRs. Skip them, they are not used in CFI. */ 9803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (int i = 0; i < 16; i++) 9903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += 4; 10003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* The floating-point control word. */ 10103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += 8; 10203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* And finally the FPRs. */ 10303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Word fprs[16]; 10403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (int i = 0; i < 16; i++) 10503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 10603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! readfunc (sigreg_ptr, &val, arg)) 10703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 10803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (ebl->class == ELFCLASS32) 10903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 11003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Addr val_low; 11103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! readfunc (sigreg_ptr + 4, &val_low, arg)) 11203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 11303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes val = (val << 32) | val_low; 11403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 11503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes fprs[i] = val; 11603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += 8; 11703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 11803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* If we have them, the GPR upper halves are appended at the end. */ 11903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (ebl->class == ELFCLASS32) 12003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 12103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Skip signal number. */ 12203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += 4; 12303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes for (int i = 0; i < 16; i++) 12403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 12503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! readfunc (sigreg_ptr, &val, arg)) 12603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 12703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Dwarf_Word val_low = gprs[i]; 12803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes val = (val << 32) | val_low; 12903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes gprs[i] = val; 13003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes sigreg_ptr += 4; 13103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 13203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 13303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! setfunc (0, 16, gprs, arg)) 13403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 13503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (! setfunc (16, 16, fprs, arg)) 13603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return false; 13703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes *signal_framep = true; 13803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return true; 13903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes} 140