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: stable/11/usr.bin/truss/powerpc-freebsd.c 312084 2017-01-13 21:30:18Z jhb $");
30154047Sgrehan
31288424Sjhb/* FreeBSD/powerpc-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
39311999Sjhb#include <stdbool.h>
40154047Sgrehan#include <stdio.h>
41294849Sjhb#include <sysdecode.h>
42154047Sgrehan
43154047Sgrehan#include "truss.h"
44154047Sgrehan
45288424Sjhbstatic int
46288424Sjhbpowerpc_fetch_args(struct trussinfo *trussinfo, u_int narg)
47240562Szont{
48240005Szont	struct ptrace_io_desc iorequest;
49240005Szont	struct reg regs;
50288424Sjhb	struct current_syscall *cs;
51240562Szont	lwpid_t tid;
52288424Sjhb	u_int i, reg;
53154047Sgrehan
54240562Szont	tid = trussinfo->curthread->tid;
55288424Sjhb	cs = &trussinfo->curthread->cs;
56240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
57240005Szont		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
58288424Sjhb		return (-1);
59240005Szont	}
60154047Sgrehan
61240005Szont	/*
62288424Sjhb	 * FreeBSD has two special kinds of system call redirections --
63240005Szont	 * SYS_syscall, and SYS___syscall.  The former is the old syscall()
64240005Szont	 * routine, basically; the latter is for quad-aligned arguments.
65288424Sjhb	 *
66288424Sjhb	 * The system call argument count and code from ptrace() already
67288424Sjhb	 * account for these, but we need to skip over the first argument.
68240005Szont	 */
69288424Sjhb	reg = 0;
70288424Sjhb	switch (regs.fixreg[0]) {
71288424Sjhb	case SYS_syscall:
72288424Sjhb		reg += 1;
73288424Sjhb		break;
74288424Sjhb	case SYS___syscall:
75288424Sjhb		reg += 2;
76288424Sjhb		break;
77240005Szont	}
78154047Sgrehan
79288424Sjhb	for (i = 0; i < narg && reg < NARGREG; i++, reg++) {
80288424Sjhb		cs->args[i] = regs.fixreg[FIRSTARG + reg];
81240005Szont	}
82288424Sjhb	if (narg > i) {
83240005Szont		iorequest.piod_op = PIOD_READ_D;
84240005Szont		iorequest.piod_offs = (void *)(regs.fixreg[1] + 8);
85288424Sjhb		iorequest.piod_addr = &cs->args[i];
86288424Sjhb		iorequest.piod_len = (narg - i) * sizeof(cs->args[0]);
87240562Szont		ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
88240005Szont		if (iorequest.piod_len == 0)
89288424Sjhb			return (-1);
90240005Szont	}
91154047Sgrehan
92288424Sjhb	return (0);
93154047Sgrehan}
94154047Sgrehan
95288424Sjhbstatic int
96288424Sjhbpowerpc_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
97154047Sgrehan{
98240005Szont	struct reg regs;
99240562Szont	lwpid_t tid;
100154047Sgrehan
101240562Szont	tid = trussinfo->curthread->tid;
102240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
103288424Sjhb		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
104240005Szont		return (-1);
105240005Szont	}
106154047Sgrehan
107288424Sjhb	/* XXX: Does not have fixup for __syscall(). */
108288424Sjhb	retval[0] = regs.fixreg[3];
109288424Sjhb	retval[1] = regs.fixreg[4];
110288424Sjhb	*errorp = !!(regs.cr & 0x10000000);
111288424Sjhb	return (0);
112288424Sjhb}
113154047Sgrehan
114289239Sbdrewerystatic struct procabi powerpc_freebsd = {
115288424Sjhb	"FreeBSD ELF32",
116295056Sjhb	SYSDECODE_ABI_FREEBSD,
117288424Sjhb	powerpc_fetch_args,
118312084Sjhb	powerpc_fetch_retval,
119312084Sjhb	STAILQ_HEAD_INITIALIZER(powerpc_freebsd.extra_syscalls),
120312084Sjhb	{ NULL }
121288424Sjhb};
122154047Sgrehan
123289239SbdreweryPROCABI(powerpc_freebsd);
124