regs.c revision f9d93c50bd246ea7fd42e0c8ad24aa01467e76ac
1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 1998,2002,2004,2008,2009 Juan Cespedes
5 * Copyright (C) 2009 Juan Cespedes
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include "config.h"
24
25#include <sys/types.h>
26#include <sys/ptrace.h>
27#include <asm/ptrace.h>
28#include <errno.h>
29
30#include "proc.h"
31#include "common.h"
32#include "regs.h"
33
34#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
35# define PTRACE_PEEKUSER PTRACE_PEEKUSR
36#endif
37
38#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
39# define PTRACE_POKEUSER PTRACE_POKEUSR
40#endif
41
42#define off_pc ((void *)60)
43#define off_sp ((void *)52)
44
45int
46arm_get_register(struct process *proc, enum arm_register reg, uint32_t *lp)
47{
48	errno = 0;
49	long l = ptrace(PTRACE_PEEKUSER, proc->pid, (void *)(reg * 4L), 0);
50	if (l == -1 && errno != 0)
51		return -1;
52	*lp = (uint32_t)l;
53	return 0;
54}
55
56int
57arm_get_register_offpc(struct process *proc, enum arm_register reg,
58		       uint32_t *lp)
59{
60	if (arm_get_register(proc, reg, lp) < 0)
61		return -1;
62	if (reg == ARM_REG_PC)
63		*lp += 8;
64	return 0;
65}
66
67int
68arm_get_shifted_register(struct process *proc, uint32_t inst, int carry,
69			 arch_addr_t pc_val, uint32_t *lp)
70{
71	enum arm_register rm = BITS(inst, 0, 3);
72	unsigned long shifttype = BITS(inst, 5, 6);
73
74	uint32_t shift;
75	if (BIT(inst, 4)) {
76		if (arm_get_register_offpc(proc, BITS(inst, 8, 11), &shift) < 0)
77			return -1;
78		shift &= 0xff;
79	} else {
80		shift = BITS(inst, 7, 11);
81	}
82
83	uint32_t res;
84	if (rm == ARM_REG_PC)
85		/* xxx double cast */
86		res = (uintptr_t)pc_val + (BIT(inst, 4) ? 12 : 8);
87	else if (arm_get_register(proc, rm, &res) < 0)
88		return -1;
89
90	switch (shifttype) {
91	case 0:			/* LSL */
92		res = shift >= 32 ? 0 : res << shift;
93		break;
94
95	case 1:			/* LSR */
96		res = shift >= 32 ? 0 : res >> shift;
97		break;
98
99	case 2:			/* ASR */
100		if (shift >= 32)
101			shift = 31;
102		res = ((res & 0x80000000L)
103		       ? ~((~res) >> shift) : res >> shift);
104		break;
105
106	case 3:			/* ROR/RRX */
107		shift &= 31;
108		if (shift == 0)
109			res = (res >> 1) | (carry ? 0x80000000L : 0);
110		else
111			res = (res >> shift) | (res << (32 - shift));
112		break;
113	}
114
115	*lp = res & 0xffffffff;
116	return 0;
117}
118
119arch_addr_t
120get_instruction_pointer(struct process *proc)
121{
122	uint32_t reg;
123	if (arm_get_register(proc, ARM_REG_PC, &reg) < 0)
124		/* XXX double cast. */
125		return (arch_addr_t)-1;
126	/* XXX double cast.  */
127	return (arch_addr_t)(uintptr_t)reg;
128}
129
130void
131set_instruction_pointer(struct process *proc, void *addr)
132{
133	ptrace(PTRACE_POKEUSER, proc->pid, off_pc, addr);
134}
135
136void *
137get_stack_pointer(struct process *proc)
138{
139	return (void *)ptrace(PTRACE_PEEKUSER, proc->pid, off_sp, 0);
140}
141
142arch_addr_t
143get_return_addr(struct process *proc, arch_addr_t stack_pointer)
144{
145	uint32_t reg;
146	if (arm_get_register(proc, ARM_REG_LR, &reg) < 0)
147		/* XXX double cast. */
148		return (arch_addr_t)-1;
149	/* XXX double cast.  */
150	return (arch_addr_t)(uintptr_t)reg;
151}
152