1130394Sdwmalone/*
2204977Simp * Copyright 1997 Sean Eric Fagan
3130394Sdwmalone *
4130394Sdwmalone * Redistribution and use in source and binary forms, with or without
5130394Sdwmalone * modification, are permitted provided that the following conditions
6130394Sdwmalone * are met:
7130394Sdwmalone * 1. Redistributions of source code must retain the above copyright
8130394Sdwmalone *    notice, this list of conditions and the following disclaimer.
9130394Sdwmalone * 2. Redistributions in binary form must reproduce the above copyright
10130394Sdwmalone *    notice, this list of conditions and the following disclaimer in the
11130394Sdwmalone *    documentation and/or other materials provided with the distribution.
12130394Sdwmalone * 3. All advertising materials mentioning features or use of this software
13130394Sdwmalone *    must display the following acknowledgement:
14130394Sdwmalone *	This product includes software developed by Sean Eric Fagan
15130394Sdwmalone * 4. Neither the name of the author may be used to endorse or promote
16130394Sdwmalone *    products derived from this software without specific prior written
17130394Sdwmalone *    permission.
18130394Sdwmalone *
19130394Sdwmalone * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20130394Sdwmalone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21130394Sdwmalone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22130394Sdwmalone * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23130394Sdwmalone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24130394Sdwmalone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25130394Sdwmalone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26130394Sdwmalone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27130394Sdwmalone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28130394Sdwmalone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29130394Sdwmalone * SUCH DAMAGE.
30130394Sdwmalone */
31130394Sdwmalone
32288424Sjhb#include <sys/cdefs.h>
33288424Sjhb__FBSDID("$FreeBSD: releng/11.0/usr.bin/truss/amd64-freebsd.c 295056 2016-01-30 01:00:54Z jhb $");
34130394Sdwmalone
35288424Sjhb/* FreeBSD/amd64-specific system call handling. */
36130394Sdwmalone
37168569Sdelphij#include <sys/ptrace.h>
38130394Sdwmalone#include <sys/syscall.h>
39130394Sdwmalone
40130394Sdwmalone#include <machine/reg.h>
41130394Sdwmalone#include <machine/psl.h>
42130394Sdwmalone
43130394Sdwmalone#include <stdio.h>
44294849Sjhb#include <sysdecode.h>
45130394Sdwmalone
46130394Sdwmalone#include "truss.h"
47130394Sdwmalone
48288424Sjhbstatic int
49288424Sjhbamd64_fetch_args(struct trussinfo *trussinfo, u_int narg)
50240562Szont{
51240005Szont	struct ptrace_io_desc iorequest;
52240005Szont	struct reg regs;
53288424Sjhb	struct current_syscall *cs;
54240562Szont	lwpid_t tid;
55288424Sjhb	u_int i, reg;
56130394Sdwmalone
57240562Szont	tid = trussinfo->curthread->tid;
58288424Sjhb	cs = &trussinfo->curthread->cs;
59240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
60240005Szont		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
61288424Sjhb		return (-1);
62240005Szont	}
63130394Sdwmalone
64240005Szont	/*
65288424Sjhb	 * FreeBSD has two special kinds of system call redirections --
66240005Szont	 * SYS_syscall, and SYS___syscall.  The former is the old syscall()
67240005Szont	 * routine, basically; the latter is for quad-aligned arguments.
68288424Sjhb	 *
69288424Sjhb	 * The system call argument count and code from ptrace() already
70288424Sjhb	 * account for these, but we need to skip over %rax if it contains
71288424Sjhb	 * either of these values.
72240005Szont	 */
73240005Szont	reg = 0;
74288424Sjhb	switch (regs.r_rax) {
75240005Szont	case SYS_syscall:
76240005Szont	case SYS___syscall:
77240005Szont		reg++;
78240005Szont		break;
79240005Szont	}
80130394Sdwmalone
81288424Sjhb	for (i = 0; i < narg && reg < 6; i++, reg++) {
82240005Szont		switch (reg) {
83288424Sjhb		case 0: cs->args[i] = regs.r_rdi; break;
84288424Sjhb		case 1: cs->args[i] = regs.r_rsi; break;
85288424Sjhb		case 2: cs->args[i] = regs.r_rdx; break;
86288424Sjhb		case 3: cs->args[i] = regs.r_rcx; break;
87288424Sjhb		case 4: cs->args[i] = regs.r_r8; break;
88288424Sjhb		case 5: cs->args[i] = regs.r_r9; break;
89240005Szont		}
90240005Szont	}
91288424Sjhb	if (narg > i) {
92240005Szont		iorequest.piod_op = PIOD_READ_D;
93240005Szont		iorequest.piod_offs = (void *)(regs.r_rsp + sizeof(register_t));
94288424Sjhb		iorequest.piod_addr = &cs->args[i];
95288424Sjhb		iorequest.piod_len = (narg - i) * sizeof(register_t);
96240562Szont		ptrace(PT_IO, tid, (caddr_t)&iorequest, 0);
97240005Szont		if (iorequest.piod_len == 0)
98288424Sjhb			return (-1);
99240005Szont	}
100240005Szont
101288424Sjhb	return (0);
102130394Sdwmalone}
103130394Sdwmalone
104288424Sjhbstatic int
105288424Sjhbamd64_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
106130394Sdwmalone{
107240005Szont	struct reg regs;
108240562Szont	lwpid_t tid;
109130394Sdwmalone
110240562Szont	tid = trussinfo->curthread->tid;
111240562Szont	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
112240005Szont		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
113240005Szont		return (-1);
114240005Szont	}
115130394Sdwmalone
116288424Sjhb	retval[0] = regs.r_rax;
117288424Sjhb	retval[1] = regs.r_rdx;
118288424Sjhb	*errorp = !!(regs.r_rflags & PSL_C);
119288424Sjhb	return (0);
120288424Sjhb}
121130394Sdwmalone
122289239Sbdrewerystatic struct procabi amd64_freebsd = {
123288424Sjhb	"FreeBSD ELF64",
124295056Sjhb	SYSDECODE_ABI_FREEBSD,
125288424Sjhb	amd64_fetch_args,
126288424Sjhb	amd64_fetch_retval
127288424Sjhb};
128130394Sdwmalone
129289239SbdreweryPROCABI(amd64_freebsd);
130