Home | History | Annotate | Line # | Download | only in freebsd
freebsd_syscall.c revision 1.2
      1  1.2  maxv /*	$NetBSD: freebsd_syscall.c,v 1.2 2017/08/08 08:04:06 maxv Exp $	*/
      2  1.1  maxv 
      3  1.1  maxv /*-
      4  1.1  maxv  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  1.1  maxv  * All rights reserved.
      6  1.1  maxv  *
      7  1.1  maxv  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  maxv  * by Charles M. Hannum.
      9  1.1  maxv  *
     10  1.1  maxv  * Redistribution and use in source and binary forms, with or without
     11  1.1  maxv  * modification, are permitted provided that the following conditions
     12  1.1  maxv  * are met:
     13  1.1  maxv  * 1. Redistributions of source code must retain the above copyright
     14  1.1  maxv  *    notice, this list of conditions and the following disclaimer.
     15  1.1  maxv  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  maxv  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  maxv  *    documentation and/or other materials provided with the distribution.
     18  1.1  maxv  *
     19  1.1  maxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  maxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  maxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  maxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  maxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  maxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  maxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  maxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  maxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  maxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  maxv  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  maxv  */
     31  1.1  maxv 
     32  1.1  maxv #include <sys/cdefs.h>
     33  1.2  maxv __KERNEL_RCSID(0, "$NetBSD: freebsd_syscall.c,v 1.2 2017/08/08 08:04:06 maxv Exp $");
     34  1.1  maxv 
     35  1.1  maxv #include <sys/param.h>
     36  1.1  maxv #include <sys/systm.h>
     37  1.1  maxv #include <sys/proc.h>
     38  1.1  maxv #include <sys/signal.h>
     39  1.1  maxv #include <sys/syscall.h>
     40  1.1  maxv #include <sys/syscallvar.h>
     41  1.1  maxv 
     42  1.1  maxv #include <uvm/uvm_extern.h>
     43  1.1  maxv 
     44  1.1  maxv #include <machine/cpu.h>
     45  1.1  maxv #include <machine/psl.h>
     46  1.1  maxv #include <machine/userret.h>
     47  1.1  maxv 
     48  1.1  maxv #include <compat/sys/signal.h>
     49  1.1  maxv 
     50  1.2  maxv #include <compat/freebsd/freebsd_machdep.h>
     51  1.1  maxv #include <compat/freebsd/freebsd_syscall.h>
     52  1.1  maxv 
     53  1.1  maxv void freebsd_syscall(struct trapframe *);
     54  1.1  maxv 
     55  1.1  maxv void
     56  1.1  maxv freebsd_syscall_intern(struct proc *p)
     57  1.1  maxv {
     58  1.1  maxv 
     59  1.1  maxv 	p->p_md.md_syscall = freebsd_syscall;
     60  1.1  maxv }
     61  1.1  maxv 
     62  1.1  maxv /*
     63  1.1  maxv  * syscall(frame):
     64  1.1  maxv  *	System call request from POSIX system call gate interface to kernel.
     65  1.1  maxv  * Like trap(), argument is call by reference.
     66  1.1  maxv  */
     67  1.1  maxv void
     68  1.1  maxv freebsd_syscall(struct trapframe *frame)
     69  1.1  maxv {
     70  1.1  maxv 	char *params;
     71  1.1  maxv 	const struct sysent *callp;
     72  1.1  maxv 	struct lwp *l;
     73  1.1  maxv 	struct proc *p;
     74  1.1  maxv 	int error;
     75  1.1  maxv 	size_t argsize;
     76  1.1  maxv 	register_t code, args[8], rval[2];
     77  1.1  maxv 
     78  1.1  maxv 	l = curlwp;
     79  1.1  maxv 	p = l->l_proc;
     80  1.1  maxv 	LWP_CACHE_CREDS(l, p);
     81  1.1  maxv 
     82  1.1  maxv 	code = frame->tf_eax;
     83  1.1  maxv 	callp = p->p_emul->e_sysent;
     84  1.1  maxv 	params = (char *)frame->tf_esp + sizeof(int);
     85  1.1  maxv 
     86  1.1  maxv 	switch (code) {
     87  1.1  maxv 	case SYS_syscall:
     88  1.1  maxv 		/*
     89  1.1  maxv 		 * Code is first argument, followed by actual args.
     90  1.1  maxv 		 */
     91  1.1  maxv 		code = fuword(params);
     92  1.1  maxv 		params += sizeof(int);
     93  1.1  maxv 		break;
     94  1.1  maxv 	case SYS___syscall:
     95  1.1  maxv 		/*
     96  1.1  maxv 		 * Like syscall, but code is a quad, so as to maintain
     97  1.1  maxv 		 * quad alignment for the rest of the arguments.
     98  1.1  maxv 		 */
     99  1.1  maxv 		code = fuword(params + _QUAD_LOWWORD * sizeof(int));
    100  1.1  maxv 		params += sizeof(quad_t);
    101  1.1  maxv 		break;
    102  1.1  maxv 	default:
    103  1.1  maxv 		break;
    104  1.1  maxv 	}
    105  1.1  maxv 
    106  1.1  maxv 	code &= (SYS_NSYSENT - 1);
    107  1.1  maxv 	callp += code;
    108  1.1  maxv 	argsize = callp->sy_argsize;
    109  1.1  maxv 	if (argsize) {
    110  1.1  maxv 		error = copyin(params, (void *)args, argsize);
    111  1.1  maxv 		if (error)
    112  1.1  maxv 			goto bad;
    113  1.1  maxv 	}
    114  1.1  maxv 
    115  1.1  maxv 	if (!__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_entry))
    116  1.1  maxv 	    || (error = trace_enter(code, callp, args)) == 0) {
    117  1.1  maxv 		rval[0] = 0;
    118  1.1  maxv 		rval[1] = frame->tf_edx; /* need to keep edx for shared FreeBSD bins */
    119  1.1  maxv 		error = sy_call(callp, l, args, rval);
    120  1.1  maxv 	}
    121  1.1  maxv 
    122  1.1  maxv 	switch (error) {
    123  1.1  maxv 	case 0:
    124  1.1  maxv 		frame->tf_eax = rval[0];
    125  1.1  maxv 		frame->tf_edx = rval[1];
    126  1.1  maxv 		frame->tf_eflags &= ~PSL_C;	/* carry bit */
    127  1.1  maxv 		break;
    128  1.1  maxv 	case ERESTART:
    129  1.1  maxv 		/*
    130  1.1  maxv 		 * The offset to adjust the PC by depends on whether we entered
    131  1.1  maxv 		 * the kernel through the trap or call gate.  We pushed the
    132  1.1  maxv 		 * size of the instruction into tf_err on entry.
    133  1.1  maxv 		 */
    134  1.1  maxv 		frame->tf_eip -= frame->tf_err;
    135  1.1  maxv 		break;
    136  1.1  maxv 	case EJUSTRETURN:
    137  1.1  maxv 		/* nothing to do */
    138  1.1  maxv 		break;
    139  1.1  maxv 	default:
    140  1.1  maxv 	bad:
    141  1.1  maxv 		frame->tf_eax = error;
    142  1.1  maxv 		frame->tf_eflags |= PSL_C;	/* carry bit */
    143  1.1  maxv 		break;
    144  1.1  maxv 	}
    145  1.1  maxv 
    146  1.1  maxv 	if (__predict_false(p->p_trace_enabled || KDTRACE_ENTRY(callp->sy_return)))
    147  1.1  maxv 		trace_exit(code, callp, args, rval, error);
    148  1.1  maxv 
    149  1.1  maxv 	userret(l);
    150  1.1  maxv }
    151