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