1/*
2 * This file is part of ltrace.
3 * Copyright (C) 2010,2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2011 Andreas Schwab
5 * Copyright (C) 2002,2004,2008,2009 Juan Cespedes
6 * Copyright (C) 2008 Luis Machado, IBM Corporation
7 * Copyright (C) 2006 Ian Wienand
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 */
24
25#include "config.h"
26
27#include <assert.h>
28#include <elf.h>
29#include <errno.h>
30#include <signal.h>
31#include <string.h>
32#include <sys/types.h>
33#include <sys/wait.h>
34
35#include "backend.h"
36#include "breakpoint.h"
37#include "common.h"
38#include "insn.h"
39#include "proc.h"
40#include "ptrace.h"
41#include "type.h"
42
43#if (!defined(PTRACE_PEEKUSER) && defined(PTRACE_PEEKUSR))
44# define PTRACE_PEEKUSER PTRACE_PEEKUSR
45#endif
46
47#if (!defined(PTRACE_POKEUSER) && defined(PTRACE_POKEUSR))
48# define PTRACE_POKEUSER PTRACE_POKEUSR
49#endif
50
51void
52get_arch_dep(struct process *proc)
53{
54#ifdef __powerpc64__
55	proc->mask_32bit = (proc->e_machine == EM_PPC);
56#endif
57}
58
59#define SYSCALL_INSN   0x44000002
60
61/* Returns 1 if syscall, 2 if sysret, 0 otherwise. */
62int
63syscall_p(struct process *proc, int status, int *sysnum)
64{
65	if (WIFSTOPPED(status)
66	    && WSTOPSIG(status) == (SIGTRAP | proc->tracesysgood)) {
67		long pc = (long)get_instruction_pointer(proc);
68		int insn =
69		    (int)ptrace(PTRACE_PEEKTEXT, proc->pid, pc - sizeof(long),
70				0);
71
72		if (insn == SYSCALL_INSN) {
73			*sysnum =
74			    (int)ptrace(PTRACE_PEEKUSER, proc->pid,
75					sizeof(long) * PT_R0, 0);
76			if (proc->callstack_depth > 0 &&
77					proc->callstack[proc->callstack_depth - 1].is_syscall &&
78					proc->callstack[proc->callstack_depth - 1].c_un.syscall == *sysnum) {
79				return 2;
80			}
81			return 1;
82		}
83	}
84	return 0;
85}
86
87/* The atomic skip code is mostly taken from GDB.  */
88
89enum sw_singlestep_status
90arch_sw_singlestep(struct process *proc, struct breakpoint *sbp,
91		   int (*add_cb)(arch_addr_t, struct sw_singlestep_data *),
92		   struct sw_singlestep_data *add_cb_data)
93{
94	arch_addr_t ip = get_instruction_pointer(proc);
95	struct breakpoint *other = address2bpstruct(proc->leader, ip);
96
97	debug(1, "arch_sw_singlestep pid=%d addr=%p %s(%p)",
98	      proc->pid, ip, breakpoint_name(sbp), sbp->addr);
99
100	/* If the original instruction was lwarx/ldarx, we can't
101	 * single-step over it, instead we have to execute the whole
102	 * atomic block at once.  */
103	union {
104		uint32_t insn;
105		char buf[BREAKPOINT_LENGTH];
106	} u;
107	if (other != NULL) {
108		memcpy(u.buf, sbp->orig_value, BREAKPOINT_LENGTH);
109	} else if (proc_read_32(proc, ip, &u.insn) < 0) {
110		fprintf(stderr, "couldn't read instruction at IP %p\n", ip);
111		/* Do the normal singlestep.  */
112		return SWS_HW;
113	}
114
115	if ((u.insn & LWARX_MASK) != LWARX_INSTRUCTION
116	    && (u.insn & LWARX_MASK) != LDARX_INSTRUCTION)
117		return SWS_HW;
118
119	debug(1, "singlestep over atomic block at %p", ip);
120
121	int insn_count;
122	arch_addr_t addr = ip;
123	for (insn_count = 0; ; ++insn_count) {
124		addr += 4;
125		unsigned long l = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0);
126		if (l == (unsigned long)-1 && errno)
127			return SWS_FAIL;
128		uint32_t insn;
129#ifdef __powerpc64__
130		insn = l >> 32;
131#else
132		insn = l;
133#endif
134
135		/* If a conditional branch is found, put a breakpoint
136		 * in its destination address.  */
137		if ((insn & BRANCH_MASK) == BC_INSN) {
138			arch_addr_t branch_addr = ppc_branch_dest(addr, insn);
139			debug(1, "pid=%d, branch in atomic block from %p to %p",
140			      proc->pid, addr, branch_addr);
141			if (add_cb(branch_addr, add_cb_data) < 0)
142				return SWS_FAIL;
143		}
144
145		/* Assume that the atomic sequence ends with a
146		 * stwcx/stdcx instruction.  */
147		if ((insn & STWCX_MASK) == STWCX_INSTRUCTION
148		    || (insn & STWCX_MASK) == STDCX_INSTRUCTION) {
149			debug(1, "pid=%d, found end of atomic block %p at %p",
150			      proc->pid, ip, addr);
151			break;
152		}
153
154		/* Arbitrary cut-off.  If we didn't find the
155		 * terminating instruction by now, just give up.  */
156		if (insn_count > 16) {
157			fprintf(stderr, "[%d] couldn't find end of atomic block"
158				" at %p\n", proc->pid, ip);
159			return SWS_FAIL;
160		}
161	}
162
163	/* Put the breakpoint to the next instruction.  */
164	addr += 4;
165	if (add_cb(addr, add_cb_data) < 0)
166		return SWS_FAIL;
167
168	debug(1, "PTRACE_CONT");
169	ptrace(PTRACE_CONT, proc->pid, 0, 0);
170	return SWS_OK;
171}
172
173size_t
174arch_type_sizeof(struct process *proc, struct arg_type_info *info)
175{
176	if (proc == NULL)
177		return (size_t)-2;
178
179	switch (info->type) {
180	case ARGTYPE_VOID:
181		return 0;
182
183	case ARGTYPE_CHAR:
184		return 1;
185
186	case ARGTYPE_SHORT:
187	case ARGTYPE_USHORT:
188		return 2;
189
190	case ARGTYPE_INT:
191	case ARGTYPE_UINT:
192		return 4;
193
194	case ARGTYPE_LONG:
195	case ARGTYPE_ULONG:
196	case ARGTYPE_POINTER:
197		return proc->e_machine == EM_PPC64 ? 8 : 4;
198
199	case ARGTYPE_FLOAT:
200		return 4;
201	case ARGTYPE_DOUBLE:
202		return 8;
203
204	case ARGTYPE_ARRAY:
205	case ARGTYPE_STRUCT:
206		/* Use default value.  */
207		return (size_t)-2;
208
209	default:
210		assert(info->type != info->type);
211		abort();
212		break;
213	}
214}
215
216size_t
217arch_type_alignof(struct process *proc, struct arg_type_info *info)
218{
219	if (proc == NULL)
220		return (size_t)-2;
221
222	switch (info->type) {
223	default:
224		assert(info->type != info->type);
225		abort();
226		break;
227
228	case ARGTYPE_CHAR:
229	case ARGTYPE_SHORT:
230	case ARGTYPE_USHORT:
231	case ARGTYPE_INT:
232	case ARGTYPE_UINT:
233	case ARGTYPE_LONG:
234	case ARGTYPE_ULONG:
235	case ARGTYPE_POINTER:
236	case ARGTYPE_FLOAT:
237	case ARGTYPE_DOUBLE:
238		/* On both PPC and PPC64, fundamental types have the
239		 * same alignment as size.  */
240		return arch_type_sizeof(proc, info);
241
242	case ARGTYPE_ARRAY:
243	case ARGTYPE_STRUCT:
244		/* Use default value.  */
245		return (size_t)-2;
246	}
247}
248