Home | History | Annotate | Line # | Download | only in freebsd
freebsd_misc.c revision 1.26
      1 /*	$NetBSD: freebsd_misc.c,v 1.26 2006/10/22 18:18:49 christos 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.26 2006/10/22 18:18:49 christos 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/sa.h>
     59 #include <sys/syscallargs.h>
     60 
     61 #include <compat/freebsd/freebsd_syscallargs.h>
     62 #include <compat/common/compat_util.h>
     63 #include <compat/freebsd/freebsd_rtprio.h>
     64 #include <compat/freebsd/freebsd_timex.h>
     65 #include <compat/freebsd/freebsd_signal.h>
     66 #include <compat/freebsd/freebsd_mman.h>
     67 
     68 int
     69 freebsd_sys_msync(l, v, retval)
     70 	struct lwp *l;
     71 	void *v;
     72 	register_t *retval;
     73 {
     74 	struct freebsd_sys_msync_args /* {
     75 		syscallarg(caddr_t) addr;
     76 		syscallarg(size_t) len;
     77 		syscallarg(int) flags;
     78 	} */ *uap = v;
     79 	struct sys___msync13_args bma;
     80 
     81 	/*
     82 	 * FreeBSD-2.0-RELEASE's msync(2) is compatible with NetBSD's.
     83 	 * FreeBSD-2.0.5-RELEASE's msync(2) has addtional argument `flags',
     84 	 * but syscall number is not changed. :-<
     85 	 */
     86 	SCARG(&bma, addr) = SCARG(uap, addr);
     87 	SCARG(&bma, len) = SCARG(uap, len);
     88 	SCARG(&bma, flags) = SCARG(uap, flags);
     89 	return sys___msync13(l, &bma, retval);
     90 }
     91 
     92 int
     93 freebsd_sys_mmap(l, v, retval)
     94 	struct lwp *l;
     95 	void *v;
     96 	register_t *retval;
     97 {
     98 	struct freebsd_sys_mmap_args /* {
     99 		syscallarg(caddr_t) addr;
    100 		syscallarg(size_t) len;
    101 		syscallarg(int) prot;
    102 		syscallarg(int) flags;
    103 		syscallarg(int) fd;
    104 		syscallarg(long) pad;
    105 		syscallarg(off_t) pos;
    106 	} */ *uap = v;
    107 	struct sys_mmap_args bma;
    108 	int flags, prot, fd;
    109 	off_t pos;
    110 
    111 	prot = SCARG(uap, prot);
    112 	flags = SCARG(uap, flags);
    113 	fd = SCARG(uap, fd);
    114 	pos = SCARG(uap, pos);
    115 
    116 	/*
    117 	 * If using MAP_STACK on FreeBSD:
    118 	 *
    119 	 * + fd has to be -1
    120 	 * + prot must have read and write
    121 	 * + MAP_STACK implies MAP_ANON
    122 	 * + MAP_STACK implies offset of 0
    123 	 */
    124 	if (flags & FREEBSD_MAP_STACK) {
    125 		if ((fd != -1)
    126 		    ||((prot & (PROT_READ|PROT_WRITE))!=(PROT_READ|PROT_WRITE)))
    127 			return (EINVAL);
    128 
    129 		flags |= (MAP_ANON | MAP_FIXED);
    130 		flags &= ~FREEBSD_MAP_STACK;
    131 
    132 		pos = 0;
    133 	}
    134 
    135 	SCARG(&bma, addr) = SCARG(uap, addr);
    136 	SCARG(&bma, len) = SCARG(uap, len);
    137 	SCARG(&bma, prot) = prot;
    138 	SCARG(&bma, flags) = flags;
    139 	SCARG(&bma, fd) = fd;
    140 	SCARG(&bma, pos) = pos;
    141 
    142 	return sys_mmap(l, &bma, retval);
    143 }
    144 
    145 /* just a place holder */
    146 
    147 int
    148 freebsd_sys_rtprio(struct lwp *l __unused, void *v __unused,
    149     register_t *retval __unused)
    150 {
    151 #ifdef notyet
    152 	struct freebsd_sys_rtprio_args /* {
    153 		syscallarg(int) function;
    154 		syscallarg(pid_t) pid;
    155 		syscallarg(struct freebsd_rtprio *) rtp;
    156 	} */ *uap = v;
    157 #endif
    158 
    159 	return ENOSYS;	/* XXX */
    160 }
    161 
    162 #ifdef NTP
    163 int
    164 freebsd_ntp_adjtime(struct lwp *l __unused, void *v __unused,
    165     register_t *retval __unused)
    166 {
    167 #ifdef notyet
    168 	struct freebsd_ntp_adjtime_args /* {
    169 		syscallarg(struct freebsd_timex *) tp;
    170 	} */ *uap = v;
    171 #endif
    172 
    173 	return ENOSYS;	/* XXX */
    174 }
    175 #endif
    176 
    177 int
    178 freebsd_sys_sigaction4(struct lwp *l, void *v, register_t *retval __unused)
    179 {
    180 	struct freebsd_sys_sigaction4_args /* {
    181 		syscallarg(int) signum;
    182 		syscallarg(const struct freebsd_sigaction4 *) nsa;
    183 		syscallarg(struct freebsd_sigaction4 *) osa;
    184 	} */ *uap = v;
    185 	struct proc *p = l->l_proc;
    186 	struct freebsd_sigaction4 nesa, oesa;
    187 	struct sigaction nbsa, obsa;
    188 	int error;
    189 
    190 	if (SCARG(uap, nsa)) {
    191 		error = copyin(SCARG(uap, nsa), &nesa, sizeof(nesa));
    192 		if (error)
    193 			return (error);
    194 		nbsa.sa_handler = nesa.freebsd_sa_handler;
    195 		nbsa.sa_mask    = nesa.freebsd_sa_mask;
    196 		nbsa.sa_flags   = nesa.freebsd_sa_flags;
    197 	}
    198 	error = sigaction1(p, SCARG(uap, signum),
    199 	    SCARG(uap, nsa) ? &nbsa : 0, SCARG(uap, osa) ? &obsa : 0,
    200 	    NULL, 0);
    201 	if (error)
    202 		return (error);
    203 	if (SCARG(uap, osa)) {
    204 		oesa.freebsd_sa_handler = obsa.sa_handler;
    205 		oesa.freebsd_sa_mask    = obsa.sa_mask;
    206 		oesa.freebsd_sa_flags   = obsa.sa_flags;
    207 		error = copyout(&oesa, SCARG(uap, osa), sizeof(oesa));
    208 		if (error)
    209 			return (error);
    210 	}
    211 	return (0);
    212 }
    213 
    214 int
    215 freebsd_sys_utrace(struct lwp *l, void *v, register_t *retval __unused)
    216 {
    217 #ifdef KTRACE
    218 	struct freebsd_sys_utrace_args /* {
    219 		syscallarg(void *) addr;
    220 		syscallarg(size_t) len;
    221 	} */ *uap = v;
    222 	struct proc *p = l->l_proc;
    223 
    224 	if (!KTRPOINT(p, KTR_USER))
    225 		return 0;
    226 
    227 	return ktruser(l, "FreeBSD utrace", SCARG(uap, addr), SCARG(uap, len),
    228 	    0);
    229 #else
    230 	return ENOSYS;
    231 #endif
    232 }
    233