Home | History | Annotate | Line # | Download | only in freebsd
freebsd_misc.c revision 1.24.20.3
      1 /*	$NetBSD: freebsd_misc.c,v 1.24.20.3 2007/01/30 13:51:31 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Frank van der Linden
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project
     18  *      by Frank van der Linden
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * FreeBSD compatibility module. Try to deal with various FreeBSD system calls.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: freebsd_misc.c,v 1.24.20.3 2007/01/30 13:51:31 ad Exp $");
     40 
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_ntp.h"
     43 #include "opt_ktrace.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/proc.h>
     49 #include <sys/mount.h>
     50 #include <sys/signal.h>
     51 #include <sys/signalvar.h>
     52 #include <sys/malloc.h>
     53 #include <sys/mman.h>
     54 #ifdef KTRACE
     55 #include <sys/ktrace.h>
     56 #endif
     57 
     58 #include <sys/syscallargs.h>
     59 
     60 #include <compat/freebsd/freebsd_syscallargs.h>
     61 #include <compat/common/compat_util.h>
     62 #include <compat/freebsd/freebsd_rtprio.h>
     63 #include <compat/freebsd/freebsd_timex.h>
     64 #include <compat/freebsd/freebsd_signal.h>
     65 #include <compat/freebsd/freebsd_mman.h>
     66 
     67 int
     68 freebsd_sys_msync(l, v, retval)
     69 	struct lwp *l;
     70 	void *v;
     71 	register_t *retval;
     72 {
     73 	struct freebsd_sys_msync_args /* {
     74 		syscallarg(caddr_t) addr;
     75 		syscallarg(size_t) len;
     76 		syscallarg(int) flags;
     77 	} */ *uap = v;
     78 	struct sys___msync13_args bma;
     79 
     80 	/*
     81 	 * FreeBSD-2.0-RELEASE's msync(2) is compatible with NetBSD's.
     82 	 * FreeBSD-2.0.5-RELEASE's msync(2) has addtional argument `flags',
     83 	 * but syscall number is not changed. :-<
     84 	 */
     85 	SCARG(&bma, addr) = SCARG(uap, addr);
     86 	SCARG(&bma, len) = SCARG(uap, len);
     87 	SCARG(&bma, flags) = SCARG(uap, flags);
     88 	return sys___msync13(l, &bma, retval);
     89 }
     90 
     91 int
     92 freebsd_sys_mmap(l, v, retval)
     93 	struct lwp *l;
     94 	void *v;
     95 	register_t *retval;
     96 {
     97 	struct freebsd_sys_mmap_args /* {
     98 		syscallarg(caddr_t) addr;
     99 		syscallarg(size_t) len;
    100 		syscallarg(int) prot;
    101 		syscallarg(int) flags;
    102 		syscallarg(int) fd;
    103 		syscallarg(long) pad;
    104 		syscallarg(off_t) pos;
    105 	} */ *uap = v;
    106 	struct sys_mmap_args bma;
    107 	int flags, prot, fd;
    108 	off_t pos;
    109 
    110 	prot = SCARG(uap, prot);
    111 	flags = SCARG(uap, flags);
    112 	fd = SCARG(uap, fd);
    113 	pos = SCARG(uap, pos);
    114 
    115 	/*
    116 	 * If using MAP_STACK on FreeBSD:
    117 	 *
    118 	 * + fd has to be -1
    119 	 * + prot must have read and write
    120 	 * + MAP_STACK implies MAP_ANON
    121 	 * + MAP_STACK implies offset of 0
    122 	 */
    123 	if (flags & FREEBSD_MAP_STACK) {
    124 		if ((fd != -1)
    125 		    ||((prot & (PROT_READ|PROT_WRITE))!=(PROT_READ|PROT_WRITE)))
    126 			return (EINVAL);
    127 
    128 		flags |= (MAP_ANON | MAP_FIXED);
    129 		flags &= ~FREEBSD_MAP_STACK;
    130 
    131 		pos = 0;
    132 	}
    133 
    134 	SCARG(&bma, addr) = SCARG(uap, addr);
    135 	SCARG(&bma, len) = SCARG(uap, len);
    136 	SCARG(&bma, prot) = prot;
    137 	SCARG(&bma, flags) = flags;
    138 	SCARG(&bma, fd) = fd;
    139 	SCARG(&bma, pos) = pos;
    140 
    141 	return sys_mmap(l, &bma, retval);
    142 }
    143 
    144 /* just a place holder */
    145 
    146 int
    147 freebsd_sys_rtprio(struct lwp *l, void *v,
    148     register_t *retval)
    149 {
    150 #ifdef notyet
    151 	struct freebsd_sys_rtprio_args /* {
    152 		syscallarg(int) function;
    153 		syscallarg(pid_t) pid;
    154 		syscallarg(struct freebsd_rtprio *) rtp;
    155 	} */ *uap = v;
    156 #endif
    157 
    158 	return ENOSYS;	/* XXX */
    159 }
    160 
    161 #ifdef NTP
    162 int
    163 freebsd_ntp_adjtime(struct lwp *l, void *v,
    164     register_t *retval)
    165 {
    166 #ifdef notyet
    167 	struct freebsd_ntp_adjtime_args /* {
    168 		syscallarg(struct freebsd_timex *) tp;
    169 	} */ *uap = v;
    170 #endif
    171 
    172 	return ENOSYS;	/* XXX */
    173 }
    174 #endif
    175 
    176 int
    177 freebsd_sys_sigaction4(struct lwp *l, void *v, register_t *retval)
    178 {
    179 	struct freebsd_sys_sigaction4_args /* {
    180 		syscallarg(int) signum;
    181 		syscallarg(const struct freebsd_sigaction4 *) nsa;
    182 		syscallarg(struct freebsd_sigaction4 *) osa;
    183 	} */ *uap = v;
    184 	struct freebsd_sigaction4 nesa, oesa;
    185 	struct sigaction nbsa, obsa;
    186 	int error;
    187 
    188 	if (SCARG(uap, nsa)) {
    189 		error = copyin(SCARG(uap, nsa), &nesa, sizeof(nesa));
    190 		if (error)
    191 			return (error);
    192 		nbsa.sa_handler = nesa.freebsd_sa_handler;
    193 		nbsa.sa_mask    = nesa.freebsd_sa_mask;
    194 		nbsa.sa_flags   = nesa.freebsd_sa_flags;
    195 	}
    196 	error = sigaction1(l, SCARG(uap, signum),
    197 	    SCARG(uap, nsa) ? &nbsa : 0, SCARG(uap, osa) ? &obsa : 0,
    198 	    NULL, 0);
    199 	if (error)
    200 		return (error);
    201 	if (SCARG(uap, osa)) {
    202 		oesa.freebsd_sa_handler = obsa.sa_handler;
    203 		oesa.freebsd_sa_mask    = obsa.sa_mask;
    204 		oesa.freebsd_sa_flags   = obsa.sa_flags;
    205 		error = copyout(&oesa, SCARG(uap, osa), sizeof(oesa));
    206 		if (error)
    207 			return (error);
    208 	}
    209 	return (0);
    210 }
    211 
    212 int
    213 freebsd_sys_utrace(struct lwp *l, void *v, register_t *retval)
    214 {
    215 #ifdef KTRACE
    216 	struct freebsd_sys_utrace_args /* {
    217 		syscallarg(void *) addr;
    218 		syscallarg(size_t) len;
    219 	} */ *uap = v;
    220 	struct proc *p = l->l_proc;
    221 
    222 	if (!KTRPOINT(p, KTR_USER))
    223 		return 0;
    224 
    225 	return ktruser(l, "FreeBSD utrace", SCARG(uap, addr), SCARG(uap, len),
    226 	    0);
    227 #else
    228 	return ENOSYS;
    229 #endif
    230 }
    231