| 1 | /* $NetBSD: netbsd32_syscall.c,v 1.33 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 | #if defined(_KERNEL) && defined(_KERNEL_OPT) |
| 33 | #include "opt_dtrace.h" |
| 34 | #endif |
| 35 | |
| 36 | #include <sys/cdefs.h> |
| 37 | __KERNEL_RCSID(0, "$NetBSD: netbsd32_syscall.c,v 1.33 2015/03/07 18:41:40 christos Exp $" ); |
| 38 | |
| 39 | #include <sys/param.h> |
| 40 | #include <sys/systm.h> |
| 41 | #include <sys/proc.h> |
| 42 | #include <sys/signal.h> |
| 43 | /* XXX this file ought to include the netbsd32 version of these 2 headers */ |
| 44 | #include <sys/syscall.h> |
| 45 | #include <sys/syscallvar.h> |
| 46 | #include <sys/syscallargs.h> |
| 47 | #include <sys/syscall_stats.h> |
| 48 | |
| 49 | #include <machine/cpu.h> |
| 50 | #include <machine/psl.h> |
| 51 | #include <machine/userret.h> |
| 52 | |
| 53 | void netbsd32_syscall_intern(struct proc *); |
| 54 | static void netbsd32_syscall(struct trapframe *); |
| 55 | |
| 56 | void |
| 57 | netbsd32_syscall_intern(struct proc *p) |
| 58 | { |
| 59 | |
| 60 | p->p_md.md_syscall = netbsd32_syscall; |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | netbsd32_syscall(struct trapframe *frame) |
| 65 | { |
| 66 | char *params; |
| 67 | const struct sysent *callp; |
| 68 | struct proc *p; |
| 69 | struct lwp *l; |
| 70 | int error; |
| 71 | int i; |
| 72 | register32_t code, args[2 + SYS_MAXSYSARGS]; |
| 73 | register_t rval[2]; |
| 74 | register_t args64[SYS_MAXSYSARGS]; |
| 75 | |
| 76 | l = curlwp; |
| 77 | p = l->l_proc; |
| 78 | |
| 79 | code = frame->tf_rax & (SYS_NSYSENT - 1); |
| 80 | callp = p->p_emul->e_sysent + code; |
| 81 | |
| 82 | LWP_CACHE_CREDS(l, p); |
| 83 | |
| 84 | SYSCALL_COUNT(syscall_counts, code); |
| 85 | SYSCALL_TIME_SYS_ENTRY(l, syscall_times, code); |
| 86 | |
| 87 | params = (char *)frame->tf_rsp + sizeof(int); |
| 88 | |
| 89 | if (callp->sy_argsize) { |
| 90 | error = copyin(params, args, callp->sy_argsize); |
| 91 | if (__predict_false(error != 0)) |
| 92 | goto bad; |
| 93 | } |
| 94 | |
| 95 | if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry)) |
| 96 | && !__predict_false(callp->sy_flags & SYCALL_INDIRECT)) { |
| 97 | int narg = callp->sy_argsize >> 2; |
| 98 | for (i = 0; i < narg; i++) |
| 99 | args64[i] = args[i]; |
| 100 | error = trace_enter(code, callp, args64); |
| 101 | if (__predict_false(error != 0)) |
| 102 | goto out; |
| 103 | } |
| 104 | |
| 105 | rval[0] = 0; |
| 106 | rval[1] = 0; |
| 107 | error = sy_call(callp, l, args, rval); |
| 108 | |
| 109 | out: |
| 110 | if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)) |
| 111 | && !__predict_false(callp->sy_flags & SYCALL_INDIRECT)) { |
| 112 | int narg = callp->sy_argsize >> 2; |
| 113 | for (i = 0; i < narg; i++) |
| 114 | args64[i] = args[i]; |
| 115 | trace_exit(code, callp, args64, rval, error); |
| 116 | } |
| 117 | |
| 118 | if (__predict_true(error == 0)) { |
| 119 | frame->tf_rax = rval[0]; |
| 120 | frame->tf_rdx = rval[1]; |
| 121 | frame->tf_rflags &= ~PSL_C; /* carry bit */ |
| 122 | } else { |
| 123 | switch (error) { |
| 124 | case ERESTART: |
| 125 | /* |
| 126 | * The offset to adjust the PC by depends on whether we |
| 127 | * entered the kernel through the trap or call gate. |
| 128 | * We saved the instruction size in tf_err on entry. |
| 129 | */ |
| 130 | frame->tf_rip -= frame->tf_err; |
| 131 | break; |
| 132 | case EJUSTRETURN: |
| 133 | /* nothing to do */ |
| 134 | break; |
| 135 | default: |
| 136 | bad: |
| 137 | frame->tf_rax = error; |
| 138 | frame->tf_rflags |= PSL_C; /* carry bit */ |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | SYSCALL_TIME_SYS_EXIT(l); |
| 144 | userret(l); |
| 145 | } |
| 146 | |