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/powerpc64-freebsd32.c 312084 2017-01-13 21:30:18Z jhb $");
30154047Sgrehan
31289239Sbdrewery/* FreeBSD/powerpc64-freebsd32-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
46289239Sbdrewerypowerpc64_freebsd32_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] & 0xffffffff;
81240005Szont	}
82288424Sjhb	if (narg > i) {
83288424Sjhb		uint32_t args32[narg - i];
84288424Sjhb		u_int j;
85154047Sgrehan
86240005Szont		iorequest.piod_op = PIOD_READ_D;
87240005Szont		iorequest.piod_offs = (void *)(regs.fixreg[1] + 8);
88288424Sjhb		iorequest.piod_addr = args32;
89288424Sjhb		iorequest.piod_len = sizeof(args32);
90240562Szont		ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
91240005Szont		if (iorequest.piod_len == 0)
92288424Sjhb			return (-1);
93288424Sjhb		for (j = 0; j < narg - i; j++)
94288424Sjhb			cs->args[i + j] = args32[j];
95240005Szont	}
96154047Sgrehan
97288424Sjhb	return (0);
98154047Sgrehan}
99154047Sgrehan
100288424Sjhbstatic int
101289239Sbdrewerypowerpc64_freebsd32_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
102154047Sgrehan{
103240005Szont	struct reg regs;
104240562Szont	lwpid_t tid;
105154047Sgrehan
106240562Szont	tid = trussinfo->curthread->tid;
107240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
108288424Sjhb		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
109240005Szont		return (-1);
110240005Szont	}
111154047Sgrehan
112288424Sjhb	/* XXX: Does not have fixup for __syscall(). */
113288424Sjhb	retval[0] = regs.fixreg[3] & 0xffffffff;
114288424Sjhb	retval[1] = regs.fixreg[4] & 0xffffffff;
115288424Sjhb	*errorp = !!(regs.cr & 0x10000000);
116288424Sjhb	return (0);
117288424Sjhb}
118154047Sgrehan
119289239Sbdrewerystatic struct procabi powerpc64_freebsd32 = {
120288424Sjhb	"FreeBSD ELF32",
121295056Sjhb	SYSDECODE_ABI_FREEBSD32,
122289239Sbdrewery	powerpc64_freebsd32_fetch_args,
123312084Sjhb	powerpc64_freebsd32_fetch_retval,
124312084Sjhb	STAILQ_HEAD_INITIALIZER(powerpc64_freebsd32.extra_syscalls),
125312084Sjhb	{ NULL }
126288424Sjhb};
127154047Sgrehan
128289239SbdreweryPROCABI(powerpc64_freebsd32);
129