1210409Skib/* 2210409Skib * Copyright 1997 Sean Eric Fagan 3210409Skib * 4275732Sjmg * Redistribution and use in source and binary forms, with or without 5210409Skib * modification, are permitted provided that the following conditions 6210409Skib * are met: 7275732Sjmg * 1. Redistributions of source code must retain the above copyright 8275732Sjmg * notice, this list of conditions and the following disclaimer. 9275732Sjmg * 2. Redistributions in binary form must reproduce the above copyright 10275732Sjmg * notice, this list of conditions and the following disclaimer in the 11210409Skib * documentation and/or other materials provided with the distribution. 12210409Skib * 3. All advertising materials mentioning features or use of this software 13210409Skib * must display the following acknowledgement: 14210409Skib * This product includes software developed by Sean Eric Fagan 15210409Skib * 4. Neither the name of the author may be used to endorse or promote 16210409Skib * products derived from this software without specific prior written 17210409Skib * permission. 18210409Skib * 19210409Skib * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20210409Skib * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21210409Skib * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22210409Skib * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23210409Skib * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24210409Skib * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25210409Skib * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26210409Skib * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27210409Skib * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28210409Skib * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29210409Skib * SUCH DAMAGE. 30210409Skib */ 31210409Skib 32210409Skib#include <sys/cdefs.h> 33210409Skib__FBSDID("$FreeBSD: stable/11/usr.bin/truss/amd64-freebsd.c 312084 2017-01-13 21:30:18Z jhb $"); 34210409Skib 35210409Skib/* FreeBSD/amd64-specific system call handling. */ 36210409Skib 37210409Skib#include <sys/ptrace.h> 38210409Skib#include <sys/syscall.h> 39210409Skib 40210409Skib#include <machine/reg.h> 41210409Skib#include <machine/psl.h> 42210409Skib 43210409Skib#include <stdbool.h> 44210409Skib#include <stdio.h> 45210409Skib#include <sysdecode.h> 46210409Skib 47275732Sjmg#include "truss.h" 48210409Skib 49255187Sjmgstatic int 50275732Sjmgamd64_fetch_args(struct trussinfo *trussinfo, u_int narg) 51210409Skib{ 52210409Skib struct ptrace_io_desc iorequest; 53210409Skib struct reg regs; 54210409Skib struct current_syscall *cs; 55210409Skib lwpid_t tid; 56210409Skib u_int i, reg; 57210409Skib 58210409Skib tid = trussinfo->curthread->tid; 59210409Skib cs = &trussinfo->curthread->cs; 60210409Skib if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { 61210409Skib fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); 62210409Skib return (-1); 63267815Skib } 64267815Skib 65267815Skib /* 66275732Sjmg * FreeBSD has two special kinds of system call redirections -- 67210409Skib * SYS_syscall, and SYS___syscall. The former is the old syscall() 68210409Skib * routine, basically; the latter is for quad-aligned arguments. 69210409Skib * 70210409Skib * The system call argument count and code from ptrace() already 71210409Skib * account for these, but we need to skip over %rax if it contains 72210409Skib * either of these values. 73210409Skib */ 74210409Skib reg = 0; 75210409Skib switch (regs.r_rax) { 76210409Skib case SYS_syscall: 77210409Skib case SYS___syscall: 78210409Skib reg++; 79210409Skib break; 80210409Skib } 81210409Skib 82210409Skib for (i = 0; i < narg && reg < 6; i++, reg++) { 83210409Skib switch (reg) { 84210409Skib case 0: cs->args[i] = regs.r_rdi; break; 85210409Skib case 1: cs->args[i] = regs.r_rsi; break; 86210409Skib case 2: cs->args[i] = regs.r_rdx; break; 87210409Skib case 3: cs->args[i] = regs.r_rcx; break; 88255187Sjmg case 4: cs->args[i] = regs.r_r8; break; 89275732Sjmg case 5: cs->args[i] = regs.r_r9; break; 90275732Sjmg } 91255187Sjmg } 92255187Sjmg if (narg > i) { 93255187Sjmg iorequest.piod_op = PIOD_READ_D; 94275732Sjmg iorequest.piod_offs = (void *)(regs.r_rsp + sizeof(register_t)); 95210409Skib iorequest.piod_addr = &cs->args[i]; 96210409Skib iorequest.piod_len = (narg - i) * sizeof(register_t); 97210409Skib ptrace(PT_IO, tid, (caddr_t)&iorequest, 0); 98210409Skib if (iorequest.piod_len == 0) 99210409Skib return (-1); 100210409Skib } 101210409Skib 102210409Skib return (0); 103210409Skib} 104210409Skib 105210409Skibstatic int 106258492Sjmgamd64_fetch_retval(struct trussinfo *trussinfo, long *retval, int *errorp) 107258492Sjmg{ 108210409Skib struct reg regs; 109210409Skib lwpid_t tid; 110210409Skib 111210409Skib tid = trussinfo->curthread->tid; 112210409Skib if (ptrace(PT_GETREGS, tid, (caddr_t)®s, 0) < 0) { 113210409Skib fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n"); 114210409Skib return (-1); 115275732Sjmg } 116275732Sjmg 117275732Sjmg retval[0] = regs.r_rax; 118275732Sjmg retval[1] = regs.r_rdx; 119275732Sjmg *errorp = !!(regs.r_rflags & PSL_C); 120213069Spjd return (0); 121210409Skib} 122210409Skib 123210409Skibstatic struct procabi amd64_freebsd = { 124210409Skib "FreeBSD ELF64", 125210409Skib SYSDECODE_ABI_FREEBSD, 126210409Skib amd64_fetch_args, 127210409Skib amd64_fetch_retval, 128210409Skib STAILQ_HEAD_INITIALIZER(amd64_freebsd.extra_syscalls), 129210409Skib { NULL } 130210409Skib}; 131210409Skib 132210409SkibPROCABI(amd64_freebsd); 133210409Skib