1 /* $NetBSD: syscallemu_x86.c,v 1.1 2012/01/06 13:16:20 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Reinoud Zandijk <reinoud@NetBSD.org>
5 * Copyright (c) 2012 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_x86.c,v 1.1 2012/01/06 13:16:20 jmcneill Exp $");
32
33 #include <sys/param.h>
34 #include <sys/proc.h>
35 #include <sys/systm.h>
36
37 #include <machine/userret.h>
38
39 #include "syscallemu.h"
40
41 /*
42 * If syscallemu specific data is present for the process, verify that the
43 * caller is allowed to execute system calls. If not, deliver a SIGILL to
44 * the process. When syscallemu specific data is not present, simply defer
45 * to the original syscall handler.
46 */
47 static void
x86_syscall_emu(struct trapframe * frame)48 x86_syscall_emu(struct trapframe *frame)
49 {
50 void (*md_syscall)(struct trapframe *) = NULL;
51 struct syscallemu_data *sce;
52 register_t rip_call;
53 struct proc *p;
54 ksiginfo_t ksi;
55 lwp_t *l;
56
57 l = curlwp;
58 p = l->l_proc;
59
60 rip_call = X86_TF_RIP(frame) - frame->tf_err;
61
62 /* Determine if we need to emulate the system call */
63 sce = syscallemu_getsce(p);
64 if (sce) {
65 if ((rip_call >= sce->sce_user_start &&
66 rip_call < sce->sce_user_end) ||
67 (rip_call + frame->tf_err >= sce->sce_user_start &&
68 rip_call + frame->tf_err < 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 X86_TF_RIP(frame) = rip_call;
80 KSI_INIT_TRAP(&ksi);
81 ksi.ksi_signo = SIGILL;
82 ksi.ksi_code = ILL_ILLTRP;
83 ksi.ksi_addr = (void *)X86_TF_RIP(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);
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 = x86_syscall_emu;
105
106 return osyscall;
107 }
108