/*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018 Turing Robotic Industries Inc. * Copyright (c) 2000 Marcel Moolenaar * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #ifdef COMPAT_43TTY #include #endif /* COMPAT_43TTY */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* EM_PPC64 */ /* * Linux userland ABI identifier for ptrace(PTRACE_GET_SYSCALL_INFO), * taken from Linux's include/uapi/linux/audit.h. */ #define LINUX_AUDIT_ARCH_64BIT 0x80000000 #define LINUX_AUDIT_ARCH_LE 0x40000000 #if _BYTE_ORDER == _LITTLE_ENDIAN #define LINUX_ARCH_PPC64 (EM_PPC64 | LINUX_AUDIT_ARCH_64BIT | \ LINUX_AUDIT_ARCH_LE) #else #define LINUX_ARCH_PPC64 (EM_PPC64 | LINUX_AUDIT_ARCH_64BIT) #endif int linux_mmap(struct thread *td, struct linux_mmap_args *uap) { return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot, uap->flags, uap->fd, uap->pgoff)); } int linux_set_upcall(struct thread *td, register_t stack) { if (stack != 0) td->td_frame->fixreg[1] = stack; td->td_frame->fixreg[0] = td->td_frame->fixreg[1]; return (0); } int linux_set_cloned_tls(struct thread *td, void *desc) { if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) td->td_frame->fixreg[2] = (uintptr_t)desc; else td->td_frame->fixreg[13] = (uintptr_t)desc; return (0); } void bsd_to_linux_regset(const struct reg *b_reg, struct linux_pt_regset *l_regset) { memset(l_regset, 0, sizeof(*l_regset)); memcpy(l_regset->gpr, b_reg->fixreg, sizeof(l_regset->gpr)); l_regset->nip = b_reg->pc; l_regset->ctr = b_reg->ctr; l_regset->link = b_reg->lr; l_regset->xer = b_reg->xer; l_regset->ccr = b_reg->cr; /* * msr, trap, dar, dsisr, result live in the trapframe/PCB and * are not exposed by struct reg. linux_ptrace_getregs_machdep() * fills msr from the tracee's PCB; the rest stay zero. softe is * always 1 (soft-enabled) for a stopped userland thread. */ l_regset->softe = 1; } void linux_to_bsd_regset(struct reg *b_reg, const struct linux_pt_regset *l_regset) { memcpy(b_reg->fixreg, l_regset->gpr, sizeof(b_reg->fixreg)); b_reg->pc = l_regset->nip; b_reg->ctr = l_regset->ctr; b_reg->lr = l_regset->link; b_reg->xer = l_regset->xer; b_reg->cr = l_regset->ccr; /* msr and the remaining pt_regs tail are not settable via PT_SETREGS. */ } void linux_ptrace_get_syscall_info_machdep(const struct reg *reg, struct syscall_info *si) { si->arch = LINUX_ARCH_PPC64; si->instruction_pointer = reg->pc; si->stack_pointer = reg->fixreg[1]; } int linux_ptrace_getregs_machdep(struct thread *td __unused, pid_t pid __unused, struct linux_pt_regset *l_regset __unused) { /* * struct reg (already copied into l_regset by bsd_to_linux_regset) * does not carry MSR. Fetching it from the tracee needs proper * thread-lock discipline (thread_lock + trapframe reach), which is * left for a follow-up. For now, msr stays 0; most gdb use cases * do not depend on it. */ return (0); } int linux_pause(struct thread *td, struct linux_pause_args *args) { struct proc *p = td->td_proc; sigset_t sigmask; LINUX_CTR(pause); PROC_LOCK(p); sigmask = td->td_sigmask; PROC_UNLOCK(p); return (kern_sigsuspend(td, sigmask)); } int linux_ptrace_peekuser(struct thread *td, pid_t pid, void *addr, void *data) { LINUX_RATELIMIT_MSG_OPT1("PTRACE_PEEKUSER offset %ld not implemented; " "returning EINVAL", (uintptr_t)addr); return (EINVAL); } int linux_ptrace_pokeuser(struct thread *td, pid_t pid, void *addr, void *data) { LINUX_RATELIMIT_MSG_OPT1("PTRACE_POKEUSER offset %ld " "not implemented; returning EINVAL", (uintptr_t)addr); return (EINVAL); } static linux_ioctl_function_t linux_ioctl_ppc_handler; static struct linux_ioctl_handler linux_ioctls_ppc = { .func = linux_ioctl_ppc_handler, .low = FIOCLEX, .high = LINUX_IOCTL_TERMIO_PPC_MAX }; DATA_SET(linux_ioctl_handler_set, linux_ioctls_ppc); static int linux_ioctl_ppc_handler(struct thread *td, struct linux_ioctl_args *args) { switch (args->cmd) { case LINUX_TCGETS_PPC: args->cmd = LINUX_TCGETS; return (linux_ioctl_termio(td, args)); case LINUX_TCSETS_PPC: args->cmd = LINUX_TCSETS; return (linux_ioctl_termio(td, args)); case LINUX_TCSETSW_PPC: args->cmd = LINUX_TCSETSW; return (linux_ioctl_termio(td, args)); case LINUX_TCSETSF_PPC: args->cmd = LINUX_TCSETSF; return (linux_ioctl_termio(td, args)); case LINUX_TCGETA_PPC: args->cmd = LINUX_TCGETA; return (linux_ioctl_termio(td, args)); case LINUX_TCSETA_PPC: args->cmd = LINUX_TCSETA; return (linux_ioctl_termio(td, args)); case LINUX_TCSETAW_PPC: args->cmd = LINUX_TCSETAW; return (linux_ioctl_termio(td, args)); case LINUX_TCSETAF_PPC: args->cmd = LINUX_TCSETAF; return (linux_ioctl_termio(td, args)); case LINUX_TCSBRK_PPC: args->cmd = LINUX_TCSBRK; return (linux_ioctl_termio(td, args)); case LINUX_TCXONC_PPC: args->cmd = LINUX_TCXONC; return (linux_ioctl_termio(td, args)); case LINUX_TCFLSH_PPC: args->cmd = LINUX_TCFLSH; return (linux_ioctl_termio(td, args)); case LINUX_TIOCSWINSZ_PPC: args->cmd = LINUX_TIOCSWINSZ; return (linux_ioctl_termio(td, args)); case LINUX_TIOCGWINSZ_PPC: args->cmd = LINUX_TIOCGWINSZ; return (linux_ioctl_termio(td, args)); /* * The legacy PowerPC 't'-group TIOC ioctls (START/STOP/OUTQ and * [SG]LTC) share their encoding *and* struct layout with the * FreeBSD equivalents (see and ), * so hand them straight to sys_ioctl(). TIOCGLTC/TIOCSLTC only * exist in a kernel built with options COMPAT_43TTY; without it they * fall through to ENOTTY, which matches Linux's behavior on a modern * kernel. */ case LINUX_TIOCSTART_PPC: args->cmd = TIOCSTART; return (sys_ioctl(td, (struct ioctl_args *)args)); case LINUX_TIOCSTOP_PPC: args->cmd = TIOCSTOP; return (sys_ioctl(td, (struct ioctl_args *)args)); case LINUX_TIOCOUTQ_PPC: args->cmd = TIOCOUTQ; return (sys_ioctl(td, (struct ioctl_args *)args)); #ifdef COMPAT_43TTY case LINUX_TIOCGLTC_PPC: args->cmd = TIOCGLTC; return (sys_ioctl(td, (struct ioctl_args *)args)); case LINUX_TIOCSLTC_PPC: args->cmd = TIOCSLTC; return (sys_ioctl(td, (struct ioctl_args *)args)); #endif /* COMPAT_43TTY */ case LINUX_TIOCSPGRP_PPC: args->cmd = LINUX_TIOCSPGRP; return (linux_ioctl_termio(td, args)); case LINUX_TIOCGPGRP_PPC: args->cmd = LINUX_TIOCGPGRP; return (linux_ioctl_termio(td, args)); /* Cheat down below, the ioctls match the FreeBSD ones precisely */ case FIOCLEX: args->cmd = LINUX_FIOCLEX; return (linux_ioctl_termio(td, args)); case FIONCLEX: args->cmd = LINUX_FIONCLEX; return (linux_ioctl_termio(td, args)); case FIOASYNC: args->cmd = LINUX_FIOASYNC; return (linux_ioctl_termio(td, args)); case FIONBIO: args->cmd = LINUX_FIONBIO; return (linux_ioctl_termio(td, args)); case FIONREAD: args->cmd = LINUX_FIONREAD; return (linux_ioctl_termio(td, args)); default: return (ENOTTY); } return (ENOTTY); }