1 1.1 jmcneill /* $NetBSD: syscallemu_arm.c,v 1.1 2013/11/10 19:58:38 jmcneill Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2011 Reinoud Zandijk <reinoud (at) NetBSD.org> 5 1.1 jmcneill * Copyright (c) 2012-2013 Jared D. McNeill <jmcneill (at) invisible.ca> 6 1.1 jmcneill * All rights reserved. 7 1.1 jmcneill * 8 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 9 1.1 jmcneill * modification, are permitted provided that the following conditions 10 1.1 jmcneill * are met: 11 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 12 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 13 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 15 1.1 jmcneill * documentation and/or other materials provided with the distribution. 16 1.1 jmcneill * 17 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 28 1.1 jmcneill */ 29 1.1 jmcneill 30 1.1 jmcneill #include <sys/cdefs.h> 31 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: syscallemu_arm.c,v 1.1 2013/11/10 19:58:38 jmcneill Exp $"); 32 1.1 jmcneill 33 1.1 jmcneill #include <sys/param.h> 34 1.1 jmcneill #include <sys/proc.h> 35 1.1 jmcneill #include <sys/systm.h> 36 1.1 jmcneill 37 1.1 jmcneill #include <arch/arm/include/locore.h> 38 1.1 jmcneill 39 1.1 jmcneill #include "syscallemu.h" 40 1.1 jmcneill 41 1.1 jmcneill #define ARM_TF_PC(frame) ((frame)->tf_pc) 42 1.1 jmcneill 43 1.1 jmcneill /* 44 1.1 jmcneill * If syscallemu specific data is present for the process, verify that the 45 1.1 jmcneill * caller is allowed to execute system calls. If not, deliver a SIGILL to 46 1.1 jmcneill * the process. When syscallemu specific data is not present, simply defer 47 1.1 jmcneill * to the original syscall handler. 48 1.1 jmcneill */ 49 1.1 jmcneill static void 50 1.1 jmcneill arm_syscall_emu(struct trapframe *frame, struct lwp *l, uint32_t insn) 51 1.1 jmcneill { 52 1.1 jmcneill void (*md_syscall)(struct trapframe *, struct lwp *, uint32_t) = NULL; 53 1.1 jmcneill struct syscallemu_data *sce; 54 1.1 jmcneill register_t pc_call; 55 1.1 jmcneill struct proc *p; 56 1.1 jmcneill ksiginfo_t ksi; 57 1.1 jmcneill 58 1.1 jmcneill p = l->l_proc; 59 1.1 jmcneill 60 1.1 jmcneill pc_call = ARM_TF_PC(frame) - INSN_SIZE; 61 1.1 jmcneill 62 1.1 jmcneill /* Determine if we need to emulate the system call */ 63 1.1 jmcneill sce = syscallemu_getsce(p); 64 1.1 jmcneill if (sce) { 65 1.1 jmcneill if ((pc_call >= sce->sce_user_start && 66 1.1 jmcneill pc_call < sce->sce_user_end) || 67 1.1 jmcneill (pc_call + INSN_SIZE >= sce->sce_user_start && 68 1.1 jmcneill pc_call + INSN_SIZE < sce->sce_user_end)) { 69 1.1 jmcneill md_syscall = NULL; 70 1.1 jmcneill } else { 71 1.1 jmcneill md_syscall = sce->sce_md_syscall; 72 1.1 jmcneill } 73 1.1 jmcneill } else { 74 1.1 jmcneill md_syscall = p->p_md.md_syscall; 75 1.1 jmcneill } 76 1.1 jmcneill 77 1.1 jmcneill if (md_syscall == NULL) { 78 1.1 jmcneill /* If emulating, deliver SIGILL to process */ 79 1.1 jmcneill ARM_TF_PC(frame) = pc_call; 80 1.1 jmcneill KSI_INIT_TRAP(&ksi); 81 1.1 jmcneill ksi.ksi_signo = SIGILL; 82 1.1 jmcneill ksi.ksi_code = ILL_ILLTRP; 83 1.1 jmcneill ksi.ksi_addr = (void *)ARM_TF_PC(frame); 84 1.1 jmcneill ksi.ksi_trap = 0; 85 1.1 jmcneill trapsignal(l, &ksi); 86 1.1 jmcneill userret(l); 87 1.1 jmcneill } else { 88 1.1 jmcneill /* Not emulating, so treat as a normal syscall */ 89 1.1 jmcneill KASSERT(md_syscall != NULL); 90 1.1 jmcneill md_syscall(frame, l, insn); 91 1.1 jmcneill } 92 1.1 jmcneill } 93 1.1 jmcneill 94 1.1 jmcneill /* 95 1.1 jmcneill * Set p_md.md_syscall to our syscall filter, and return a pointer to the 96 1.1 jmcneill * original syscall handler. 97 1.1 jmcneill */ 98 1.1 jmcneill void * 99 1.1 jmcneill md_syscallemu(struct proc *p) 100 1.1 jmcneill { 101 1.1 jmcneill void *osyscall; 102 1.1 jmcneill 103 1.1 jmcneill osyscall = p->p_md.md_syscall; 104 1.1 jmcneill p->p_md.md_syscall = arm_syscall_emu; 105 1.1 jmcneill 106 1.1 jmcneill return osyscall; 107 1.1 jmcneill } 108