powerpc64-freebsd.c revision 295056
1154047Sgrehan/*
2154047Sgrehan * Copyright 2006 Peter Grehan <grehan@freebsd.org>
3204977Simp * Copyright 2005 Orlando Bassotto <orlando@break.net>
4204977Simp * Copyright 1998 Sean Eric Fagan
5154047Sgrehan *
6154047Sgrehan * Redistribution and use in source and binary forms, with or without
7154047Sgrehan * modification, are permitted provided that the following conditions
8154047Sgrehan * are met:
9154047Sgrehan * 1. Redistributions of source code must retain the above copyright
10154047Sgrehan *    notice, this list of conditions and the following disclaimer.
11154047Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
12154047Sgrehan *    notice, this list of conditions and the following disclaimer in the
13154047Sgrehan *    documentation and/or other materials provided with the distribution.
14154047Sgrehan *
15154047Sgrehan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16154047Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17154047Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18154047Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19154047Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20154047Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21154047Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22154047Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23154047Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24154047Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25154047Sgrehan * SUCH DAMAGE.
26154047Sgrehan */
27154047Sgrehan
28288424Sjhb#include <sys/cdefs.h>
29288424Sjhb__FBSDID("$FreeBSD: head/usr.bin/truss/powerpc64-freebsd.c 295056 2016-01-30 01:00:54Z jhb $");
30154047Sgrehan
31288424Sjhb/* FreeBSD/powerpc64-specific system call handling. */
32154047Sgrehan
33168569Sdelphij#include <sys/ptrace.h>
34154047Sgrehan#include <sys/syscall.h>
35154047Sgrehan
36154047Sgrehan#include <machine/reg.h>
37154047Sgrehan#include <machine/frame.h>
38154047Sgrehan
39154047Sgrehan#include <stdio.h>
40294849Sjhb#include <sysdecode.h>
41154047Sgrehan
42154047Sgrehan#include "truss.h"
43154047Sgrehan
44288424Sjhbstatic int
45288424Sjhbpowerpc64_fetch_args(struct trussinfo *trussinfo, u_int narg)
46240562Szont{
47240005Szont	struct ptrace_io_desc iorequest;
48240005Szont	struct reg regs;
49288424Sjhb	struct current_syscall *cs;
50240562Szont	lwpid_t tid;
51288424Sjhb	u_int i, reg;
52154047Sgrehan
53240562Szont	tid = trussinfo->curthread->tid;
54288424Sjhb	cs = &trussinfo->curthread->cs;
55240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
56240005Szont		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
57288424Sjhb		return (-1);
58240005Szont	}
59154047Sgrehan
60240005Szont	/*
61288424Sjhb	 * FreeBSD has two special kinds of system call redirections --
62240005Szont	 * SYS_syscall, and SYS___syscall.  The former is the old syscall()
63240005Szont	 * routine, basically; the latter is for quad-aligned arguments.
64288424Sjhb	 *
65288424Sjhb	 * The system call argument count and code from ptrace() already
66288424Sjhb	 * account for these, but we need to skip over the first argument.
67240005Szont	 */
68288424Sjhb	reg = 0;
69288424Sjhb	switch (regs.fixreg[0]) {
70288424Sjhb	case SYS_syscall:
71288424Sjhb	case SYS___syscall:
72288424Sjhb		reg += 1;
73288424Sjhb		break;
74240005Szont	}
75154047Sgrehan
76288424Sjhb	for (i = 0; i < narg && reg < NARGREG; i++, reg++)
77288424Sjhb		cs->args[i] = regs.fixreg[FIRSTARG + reg];
78288424Sjhb	if (narg > i) {
79240005Szont		iorequest.piod_op = PIOD_READ_D;
80240005Szont		iorequest.piod_offs = (void *)(regs.fixreg[1] + 48);
81288424Sjhb		iorequest.piod_addr = &cs->args[i];
82288424Sjhb		iorequest.piod_len = (narg - i) * sizeof(cs->args[0]);
83240562Szont		ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
84240005Szont		if (iorequest.piod_len == 0)
85288424Sjhb			return (-1);
86240005Szont	}
87154047Sgrehan
88288424Sjhb	return (0);
89154047Sgrehan}
90154047Sgrehan
91288424Sjhbstatic int
92288424Sjhbpowerpc64_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
93154047Sgrehan{
94240005Szont	struct reg regs;
95240562Szont	lwpid_t tid;
96154047Sgrehan
97240562Szont	tid = trussinfo->curthread->tid;
98240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
99288424Sjhb		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
100240005Szont		return (-1);
101240005Szont	}
102154047Sgrehan
103288424Sjhb	retval[0] = regs.fixreg[3];
104288424Sjhb	retval[1] = regs.fixreg[4];
105288424Sjhb	*errorp = !!(regs.cr & 0x10000000);
106288424Sjhb	return (0);
107288424Sjhb}
108154047Sgrehan
109289239Sbdrewerystatic struct procabi powerpc64_freebsd = {
110288424Sjhb	"FreeBSD ELF64",
111295056Sjhb	SYSDECODE_ABI_FREEBSD,
112288424Sjhb	powerpc64_fetch_args,
113288424Sjhb	powerpc64_fetch_retval
114288424Sjhb};
115154047Sgrehan
116289239SbdreweryPROCABI(powerpc64_freebsd);
117