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