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