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
28290052Sjhb#include <sys/cdefs.h>
29290052Sjhb__FBSDID("$FreeBSD$");
30154047Sgrehan
31290052Sjhb/* 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>
40154047Sgrehan
41154047Sgrehan#include "truss.h"
42154047Sgrehan
43154047Sgrehan#include "syscalls.h"
44154047Sgrehan
45290052Sjhbstatic int
46290052Sjhbpowerpc64_fetch_args(struct trussinfo *trussinfo, u_int narg)
47240562Szont{
48240005Szont	struct ptrace_io_desc iorequest;
49240005Szont	struct reg regs;
50290052Sjhb	struct current_syscall *cs;
51240562Szont	lwpid_t tid;
52290052Sjhb	u_int i, reg;
53154047Sgrehan
54240562Szont	tid = trussinfo->curthread->tid;
55290052Sjhb	cs = &trussinfo->curthread->cs;
56240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
57240005Szont		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
58290052Sjhb		return (-1);
59240005Szont	}
60154047Sgrehan
61240005Szont	/*
62290052Sjhb	 * 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.
65290052Sjhb	 *
66290052Sjhb	 * The system call argument count and code from ptrace() already
67290052Sjhb	 * account for these, but we need to skip over the first argument.
68240005Szont	 */
69290052Sjhb	reg = 0;
70290052Sjhb	switch (regs.fixreg[0]) {
71290052Sjhb	case SYS_syscall:
72290052Sjhb	case SYS___syscall:
73290052Sjhb		reg += 1;
74290052Sjhb		break;
75240005Szont	}
76154047Sgrehan
77290052Sjhb	for (i = 0; i < narg && reg < NARGREG; i++, reg++)
78290052Sjhb		cs->args[i] = regs.fixreg[FIRSTARG + reg];
79290052Sjhb	if (narg > i) {
80240005Szont		iorequest.piod_op = PIOD_READ_D;
81240005Szont		iorequest.piod_offs = (void *)(regs.fixreg[1] + 48);
82290052Sjhb		iorequest.piod_addr = &cs->args[i];
83290052Sjhb		iorequest.piod_len = (narg - i) * sizeof(cs->args[0]);
84240562Szont		ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
85240005Szont		if (iorequest.piod_len == 0)
86290052Sjhb			return (-1);
87240005Szont	}
88154047Sgrehan
89290052Sjhb	return (0);
90154047Sgrehan}
91154047Sgrehan
92290052Sjhbstatic int
93290052Sjhbpowerpc64_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
94154047Sgrehan{
95240005Szont	struct reg regs;
96240562Szont	lwpid_t tid;
97154047Sgrehan
98240562Szont	tid = trussinfo->curthread->tid;
99240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
100290052Sjhb		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
101240005Szont		return (-1);
102240005Szont	}
103154047Sgrehan
104290052Sjhb	retval[0] = regs.fixreg[3];
105290052Sjhb	retval[1] = regs.fixreg[4];
106290052Sjhb	*errorp = !!(regs.cr & 0x10000000);
107290052Sjhb	return (0);
108290052Sjhb}
109154047Sgrehan
110290052Sjhbstatic struct procabi powerpc64_fbsd = {
111290052Sjhb	"FreeBSD ELF64",
112290052Sjhb	syscallnames,
113290052Sjhb	nitems(syscallnames),
114290052Sjhb	powerpc64_fetch_args,
115290052Sjhb	powerpc64_fetch_retval
116290052Sjhb};
117154047Sgrehan
118290052SjhbPROCABI(powerpc64_fbsd);
119