1305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata/*
2305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * This file is part of ltrace.
3305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * Copyright (C) 2012 Petr Machata, Red Hat Inc.
4305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata *
5305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * This program is free software; you can redistribute it and/or
6305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * modify it under the terms of the GNU General Public License as
7305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * published by the Free Software Foundation; either version 2 of the
8305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * License, or (at your option) any later version.
9305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata *
10305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * This program is distributed in the hope that it will be useful, but
11305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * WITHOUT ANY WARRANTY; without even the implied warranty of
12305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * General Public License for more details.
14305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata *
15305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * You should have received a copy of the GNU General Public License
16305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * along with this program; if not, write to the Free Software
17305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * 02110-1301 USA
19305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata */
20305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata
21305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata/* Instruction masks used during single-stepping of atomic
22305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata * sequences.  This was lifted from GDB.  */
23305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define LWARX_MASK 0xfc0007fe
24305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define LWARX_INSTRUCTION 0x7c000028
25305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define LDARX_INSTRUCTION 0x7c0000A8
26305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define STWCX_MASK 0xfc0007ff
27305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define STWCX_INSTRUCTION 0x7c00012d
28305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define STDCX_INSTRUCTION 0x7c0001ad
29305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define BRANCH_MASK 0xfc000000
30305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define BC_MASK 0xfc000000
31305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define BC_INSN 0x40000000
32305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata#define B_INSN 0x48000000
33305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata
34305945b2523ea87907952eefd7300cfb9f1a0235Petr Machatastatic inline arch_addr_t
35305945b2523ea87907952eefd7300cfb9f1a0235Petr Machatappc_branch_dest(arch_addr_t addr, uint32_t insn)
36305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata{
37305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata	int immediate = ((insn & 0xfffc) ^ 0x8000) - 0x8000;
38305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata	int absolute = insn & 2;
39305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata
40305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata	/* XXX drop the following double casts.  */
41305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata	if (absolute)
42305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata		return (arch_addr_t)(uintptr_t)immediate;
43305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata	else
44305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata		return addr + (uintptr_t)immediate;
45305945b2523ea87907952eefd7300cfb9f1a0235Petr Machata}
46