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