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