1/* $NetBSD: linux_syscall.c,v 1.32 2015/03/07 18:41:40 christos Exp $ */
2
3/*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: linux_syscall.c,v 1.32 2015/03/07 18:41:40 christos Exp $");
34
35#if defined(_KERNEL_OPT)
36#include "opt_compat_linux.h"
37#endif
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/proc.h>
42#include <sys/signal.h>
43#include <sys/syscall.h>
44#include <sys/syscallvar.h>
45
46#include <machine/cpu.h>
47#include <machine/psl.h>
48#include <machine/userret.h>
49
50#include <compat/linux/linux_syscall.h>
51#include <compat/linux/common/linux_types.h>
52#include <compat/linux/common/linux_errno.h>
53#include <compat/linux/common/linux_signal.h>
54#include <compat/linux/common/linux_siginfo.h>
55#include <compat/linux/arch/amd64/linux_siginfo.h>
56#include <compat/linux/arch/amd64/linux_machdep.h>
57
58void linux_syscall_intern(struct proc *);
59static void linux_syscall(struct trapframe *);
60
61void
62linux_syscall_intern(struct proc *p)
63{
64
65 p->p_md.md_syscall = linux_syscall;
66}
67
68/*
69 * syscall(frame):
70 * System call request from POSIX system call gate interface to kernel.
71 * Like trap(), argument is call by reference.
72 */
73static void
74linux_syscall(struct trapframe *frame)
75{
76 const struct sysent *callp;
77 struct proc *p;
78 struct lwp *l;
79 int error;
80 register_t code, rval[2];
81 #define args (&frame->tf_rdi)
82
83 l = curlwp;
84 p = l->l_proc;
85
86 code = frame->tf_rax;
87
88 LWP_CACHE_CREDS(l, p);
89
90 callp = p->p_emul->e_sysent;
91
92 code &= (LINUX_SYS_NSYSENT - 1);
93 callp += code;
94
95 /*
96 * Linux system calls have a maximum of 6 arguments, they are
97 * already adjacent in the syscall trapframe.
98 */
99
100 if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))
101 && (error = trace_enter(code, callp, args)) != 0)
102 goto out;
103
104 rval[0] = 0;
105 rval[1] = 0;
106 error = sy_call(callp, l, args, rval);
107out:
108 switch (error) {
109 case 0:
110 frame->tf_rax = rval[0];
111 break;
112 case ERESTART:
113 /*
114 * The offset to adjust the PC by depends on whether we entered
115 * the kernel through the trap or call gate. We pushed the
116 * size of the instruction into tf_err on entry.
117 */
118 frame->tf_rip -= frame->tf_err;
119 break;
120 case EJUSTRETURN:
121 /* nothing to do */
122 break;
123 default:
124 error = native_to_linux_errno[error];
125 frame->tf_rax = error;
126 break;
127 }
128
129 if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)))
130 trace_exit(code, callp, args, rval, error);
131
132 userret(l);
133}
134