1/*
2 * Copyright (c) 2015 The FreeBSD Foundation
3 *
4 * Portions of this software were developed by Konstantin Belousov
5 * under sponsorship from the FreeBSD Foundation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/11/usr.bin/truss/aarch64-freebsd.c 312084 2017-01-13 21:30:18Z jhb $");
31
32/* FreeBSD/arm64-specific system call handling. */
33
34#include <sys/ptrace.h>
35#include <sys/syscall.h>
36
37#include <machine/reg.h>
38#include <machine/armreg.h>
39#include <machine/ucontext.h>
40
41#include <stdbool.h>
42#include <stdio.h>
43#include <sysdecode.h>
44
45#include "truss.h"
46
47static int
48aarch64_fetch_args(struct trussinfo *trussinfo, u_int narg)
49{
50	struct reg regs;
51	struct current_syscall *cs;
52	lwpid_t tid;
53	u_int i, reg, syscall_num;
54
55	tid = trussinfo->curthread->tid;
56	cs = &trussinfo->curthread->cs;
57	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
58		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
59		return (-1);
60	}
61
62	/*
63	 * FreeBSD has two special kinds of system call redirections --
64	 * SYS_syscall, and SYS___syscall.  The former is the old syscall()
65	 * routine, basically; the latter is for quad-aligned arguments.
66	 *
67	 * The system call argument count and code from ptrace() already
68	 * account for these, but we need to skip over the first argument.
69	 */
70	syscall_num = regs.x[8];
71	if (syscall_num == SYS_syscall || syscall_num == SYS___syscall) {
72		reg = 1;
73		syscall_num = regs.x[0];
74	} else {
75		reg = 0;
76	}
77
78	for (i = 0; i < narg && reg < 8; i++, reg++)
79		cs->args[i] = regs.x[reg];
80	return (0);
81}
82
83static int
84aarch64_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp)
85{
86	struct reg regs;
87	lwpid_t tid;
88
89	tid = trussinfo->curthread->tid;
90	if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) < 0) {
91		fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
92		return (-1);
93	}
94
95	retval[0] = regs.x[0];
96	retval[1] = regs.x[1];
97	*errorp = !!(regs.spsr & PSR_C);
98	return (0);
99}
100
101static struct procabi aarch64_freebsd = {
102	"FreeBSD ELF64",
103	SYSDECODE_ABI_FREEBSD,
104	aarch64_fetch_args,
105	aarch64_fetch_retval,
106	STAILQ_HEAD_INITIALIZER(aarch64_freebsd.extra_syscalls),
107	{ NULL }
108};
109
110PROCABI(aarch64_freebsd);
111