Home | History | Annotate | Line # | Download | only in common
linux32_misc.c revision 1.20
      1 /*	$NetBSD: linux32_misc.c,v 1.20 2010/11/02 18:14:06 chs Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
      9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center;
     10  * by Edgar Fu\ss, Mathematisches Institut der Uni Bonn.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: linux32_misc.c,v 1.20 2010/11/02 18:14:06 chs Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/proc.h>
     39 #include <sys/time.h>
     40 #include <sys/types.h>
     41 #include <sys/malloc.h>
     42 #include <sys/fstypes.h>
     43 #include <sys/vfs_syscalls.h>
     44 #include <sys/ptrace.h>
     45 #include <sys/syscall.h>
     46 
     47 #include <compat/netbsd32/netbsd32.h>
     48 #include <compat/netbsd32/netbsd32_syscallargs.h>
     49 
     50 #include <compat/linux32/common/linux32_types.h>
     51 #include <compat/linux32/common/linux32_signal.h>
     52 #include <compat/linux32/linux32_syscallargs.h>
     53 
     54 #include <compat/linux/common/linux_ptrace.h>
     55 #include <compat/linux/common/linux_types.h>
     56 #include <compat/linux/common/linux_emuldata.h>
     57 #include <compat/linux/common/linux_signal.h>
     58 #include <compat/linux/common/linux_misc.h>
     59 #include <compat/linux/common/linux_statfs.h>
     60 #include <compat/linux/common/linux_ipc.h>
     61 #include <compat/linux/common/linux_sem.h>
     62 #include <compat/linux/common/linux_futex.h>
     63 #include <compat/linux/linux_syscallargs.h>
     64 
     65 extern const struct linux_mnttypes linux_fstypes[];
     66 extern const int linux_fstypes_cnt;
     67 
     68 void linux32_to_native_timespec(struct timespec *, struct linux32_timespec *);
     69 
     70 /*
     71  * Implement the fs stat functions. Straightforward.
     72  */
     73 int
     74 linux32_sys_statfs(struct lwp *l, const struct linux32_sys_statfs_args *uap, register_t *retval)
     75 {
     76 	/* {
     77 		syscallarg(const netbsd32_charp char) path;
     78 		syscallarg(linux32_statfsp) sp;
     79 	} */
     80 	struct statvfs *sb;
     81 	struct linux_statfs ltmp;
     82 	int error;
     83 
     84 	sb = STATVFSBUF_GET();
     85 	error = do_sys_pstatvfs(l, SCARG_P32(uap, path), ST_WAIT, sb);
     86 	if (error == 0) {
     87 		bsd_to_linux_statfs(sb, &ltmp);
     88 		error = copyout(&ltmp, SCARG_P32(uap, sp), sizeof ltmp);
     89 	}
     90 
     91 	STATVFSBUF_PUT(sb);
     92 	return error;
     93 }
     94 
     95 int
     96 linux32_sys_fstatfs(struct lwp *l, const struct linux32_sys_fstatfs_args *uap, register_t *retval)
     97 {
     98 	/* {
     99 		syscallarg(int) fd;
    100 		syscallarg(linux32_statfsp) sp;
    101 	} */
    102 	struct statvfs *sb;
    103 	struct linux_statfs ltmp;
    104 	int error;
    105 
    106 	sb = STATVFSBUF_GET();
    107 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    108 	if (error == 0) {
    109 		bsd_to_linux_statfs(sb, &ltmp);
    110 		error = copyout(&ltmp, SCARG_P32(uap, sp), sizeof ltmp);
    111 	}
    112 	STATVFSBUF_PUT(sb);
    113 
    114 	return error;
    115 }
    116 
    117 int
    118 linux32_sys_statfs64(struct lwp *l, const struct linux32_sys_statfs64_args *uap, register_t *retval)
    119 {
    120 	/* {
    121 		syscallarg(const netbsd32_charp char) path;
    122 		syscallarg(linux32_statfs64p) sp;
    123 	} */
    124 	struct statvfs *sb;
    125 	struct linux_statfs64 ltmp;
    126 	int error;
    127 
    128 	sb = STATVFSBUF_GET();
    129 	error = do_sys_pstatvfs(l, SCARG_P32(uap, path), ST_WAIT, sb);
    130 	if (error == 0) {
    131 		bsd_to_linux_statfs64(sb, &ltmp);
    132 		error = copyout(&ltmp, SCARG_P32(uap, sp), sizeof ltmp);
    133 	}
    134 
    135 	STATVFSBUF_PUT(sb);
    136 	return error;
    137 }
    138 
    139 int
    140 linux32_sys_fstatfs64(struct lwp *l, const struct linux32_sys_fstatfs64_args *uap, register_t *retval)
    141 {
    142 	/* {
    143 		syscallarg(int) fd;
    144 		syscallarg(linux32_statfs64p) sp;
    145 	} */
    146 	struct statvfs *sb;
    147 	struct linux_statfs64 ltmp;
    148 	int error;
    149 
    150 	sb = STATVFSBUF_GET();
    151 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    152 	if (error == 0) {
    153 		bsd_to_linux_statfs64(sb, &ltmp);
    154 		error = copyout(&ltmp, SCARG_P32(uap, sp), sizeof ltmp);
    155 	}
    156 	STATVFSBUF_PUT(sb);
    157 
    158 	return error;
    159 }
    160 
    161 extern const int linux_ptrace_request_map[];
    162 
    163 int
    164 linux32_sys_ptrace(struct lwp *l, const struct linux32_sys_ptrace_args *uap, register_t *retval)
    165 {
    166 	/* {
    167 		i386, m68k, powerpc: T=int
    168 		alpha, amd64: T=long
    169 		syscallarg(T) request;
    170 		syscallarg(T) pid;
    171 		syscallarg(T) addr;
    172 		syscallarg(T) data;
    173 	} */
    174 	const int *ptr;
    175 	int request;
    176 	int error;
    177 
    178 	ptr = linux_ptrace_request_map;
    179 	request = SCARG(uap, request);
    180 	while (*ptr != -1)
    181 		if (*ptr++ == request) {
    182 			struct sys_ptrace_args pta;
    183 
    184 			SCARG(&pta, req) = *ptr;
    185 			SCARG(&pta, pid) = SCARG(uap, pid);
    186 			SCARG(&pta, addr) = NETBSD32IPTR64(SCARG(uap, addr));
    187 			SCARG(&pta, data) = SCARG(uap, data);
    188 
    189 			/*
    190 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
    191 			 * to continue where the process left off previously.
    192 			 * The same thing is achieved by addr == (void *) 1
    193 			 * on NetBSD, so rewrite 'addr' appropriately.
    194 			 */
    195 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
    196 				SCARG(&pta, addr) = (void *) 1;
    197 
    198 			error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
    199 			if (error)
    200 				return error;
    201 			switch (request) {
    202 			case LINUX_PTRACE_PEEKTEXT:
    203 			case LINUX_PTRACE_PEEKDATA:
    204 				error = copyout (retval,
    205 				    NETBSD32IPTR64(SCARG(uap, data)),
    206 				    sizeof *retval);
    207 				*retval = SCARG(uap, data);
    208 				break;
    209 			default:
    210 				break;
    211 			}
    212 			return error;
    213 		}
    214 		else
    215 			ptr++;
    216 
    217 	return EIO;
    218 }
    219 
    220 int
    221 linux32_sys_personality(struct lwp *l, const struct linux32_sys_personality_args *uap, register_t *retval)
    222 {
    223 	/* {
    224 		syscallarg(netbsd32_u_long) per;
    225 	} */
    226 
    227 	switch (SCARG(uap, per)) {
    228 	case LINUX_PER_LINUX:
    229 	case LINUX_PER_LINUX32:
    230 	case LINUX_PER_QUERY:
    231 		break;
    232 	default:
    233 		return EINVAL;
    234 	}
    235 
    236 	retval[0] = LINUX_PER_LINUX;
    237 	return 0;
    238 }
    239 
    240 int
    241 linux32_sys_futex(struct lwp *l,
    242     const struct linux32_sys_futex_args *uap, register_t *retval)
    243 {
    244 	/* {
    245 		syscallarg(linux32_intp_t) uaddr;
    246 		syscallarg(int) op;
    247 		syscallarg(int) val;
    248 		syscallarg(linux32_timespecp_t) timeout;
    249 		syscallarg(linux32_intp_t) uaddr2;
    250 		syscallarg(int) val3;
    251 	} */
    252 	struct linux_sys_futex_args ua;
    253 	struct linux32_timespec lts;
    254 	struct timespec ts = { 0, 0 };
    255 	int error;
    256 
    257 	NETBSD32TOP_UAP(uaddr, int);
    258 	NETBSD32TO64_UAP(op);
    259 	NETBSD32TO64_UAP(val);
    260 	NETBSD32TOP_UAP(timeout, struct linux_timespec);
    261 	NETBSD32TOP_UAP(uaddr2, int);
    262 	NETBSD32TO64_UAP(val3);
    263 	if ((SCARG(uap, op) & ~LINUX_FUTEX_PRIVATE_FLAG) == LINUX_FUTEX_WAIT &&
    264 	    SCARG_P32(uap, timeout) != NULL) {
    265 		if ((error = copyin((void *)SCARG_P32(uap, timeout),
    266 		    &lts, sizeof(lts))) != 0) {
    267 			return error;
    268 		}
    269 		linux32_to_native_timespec(&ts, &lts);
    270 	}
    271 	return linux_do_futex(l, &ua, retval, &ts);
    272 }
    273 
    274 int
    275 linux32_sys_set_robust_list(struct lwp *l,
    276     const struct linux32_sys_set_robust_list_args *uap, register_t *retval)
    277 {
    278 	/* {
    279 		syscallarg(linux32_robust_list_headp_t) head;
    280 		syscallarg(linux32_size_t) len;
    281 	} */
    282 	struct linux_sys_set_robust_list_args ua;
    283 	struct linux_emuldata *led;
    284 
    285 	if (SCARG(uap, len) != 12)
    286 		return EINVAL;
    287 
    288 	NETBSD32TOP_UAP(head, struct robust_list_head);
    289 	NETBSD32TOX64_UAP(len, size_t);
    290 
    291 	led = l->l_emuldata;
    292 	led->led_robust_head = SCARG(&ua, head);
    293 	*retval = 0;
    294 	return 0;
    295 }
    296 
    297 int
    298 linux32_sys_get_robust_list(struct lwp *l,
    299     const struct linux32_sys_get_robust_list_args *uap, register_t *retval)
    300 {
    301 	/* {
    302 		syscallarg(linux32_robust_list_headpp_t) head;
    303 		syscallarg(linux32_sizep_t) len;
    304 	} */
    305 	struct linux_sys_get_robust_list_args ua;
    306 
    307 	NETBSD32TOP_UAP(head, struct robust_list_head *);
    308 	NETBSD32TOP_UAP(len, size_t *);
    309 	return linux_sys_get_robust_list(l, &ua, retval);
    310 }
    311 
    312 int
    313 linux32_sys_truncate64(struct lwp *l, const struct linux32_sys_truncate64_args *uap, register_t *retval)
    314 {
    315 	/* {
    316 		syscallarg(netbsd32_charp) path;
    317 		syscallarg(off_t) length;
    318 	} */
    319 	struct sys_truncate_args ua;
    320 
    321 	/* Linux doesn't have the 'pad' pseudo-parameter */
    322 	NETBSD32TOP_UAP(path, const char *);
    323 	SCARG(&ua, PAD) = 0;
    324 	SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
    325 	return sys_truncate(l, &ua, retval);
    326 }
    327 
    328 int
    329 linux32_sys_ftruncate64(struct lwp *l, const struct linux32_sys_ftruncate64_args *uap, register_t *retval)
    330 {
    331 	/* {
    332 		syscallarg(unsigned int) fd;
    333 		syscallarg(off_t) length;
    334 	} */
    335 	struct sys_ftruncate_args ua;
    336 
    337 	/* Linux doesn't have the 'pad' pseudo-parameter */
    338 	NETBSD32TO64_UAP(fd);
    339 	SCARG(&ua, PAD) = 0;
    340 	SCARG(&ua, length) = ((off_t)SCARG(uap, lenhi) << 32) + SCARG(uap, lenlo);
    341 	return sys_ftruncate(l, &ua, retval);
    342 }
    343 
    344 int
    345 linux32_sys_setdomainname(struct lwp *l, const struct linux32_sys_setdomainname_args *uap, register_t *retval)
    346 {
    347 	/* {
    348 		syscallarg(netbsd32_charp) domainname;
    349 		syscallarg(int) len;
    350 	} */
    351 	struct linux_sys_setdomainname_args ua;
    352 
    353 	NETBSD32TOP_UAP(domainname, char);
    354 	NETBSD32TO64_UAP(len);
    355 	return linux_sys_setdomainname(l, &ua, retval);
    356 }
    357