Home | History | Annotate | Line # | Download | only in common
linux32_misc.c revision 1.9.10.5
      1  1.9.10.5   yamt /*	$NetBSD: linux32_misc.c,v 1.9.10.5 2010/08/11 22:53:09 yamt 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.9.10.5   yamt __KERNEL_RCSID(0, "$NetBSD: linux32_misc.c,v 1.9.10.5 2010/08/11 22:53:09 yamt 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.9.10.2   yamt #include <sys/ptrace.h>
     45  1.9.10.2   yamt #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.9.10.2   yamt #include <compat/linux/common/linux_ptrace.h>
     55       1.1   manu #include <compat/linux/common/linux_types.h>
     56  1.9.10.5   yamt #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.9.10.2   yamt #include <compat/linux/common/linux_ipc.h>
     61  1.9.10.2   yamt #include <compat/linux/common/linux_sem.h>
     62  1.9.10.5   yamt #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.9.10.5   yamt 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.9.10.2   yamt 
     95  1.9.10.3   yamt int
     96  1.9.10.3   yamt linux32_sys_fstatfs(struct lwp *l, const struct linux32_sys_fstatfs_args *uap, register_t *retval)
     97  1.9.10.3   yamt {
     98  1.9.10.3   yamt 	/* {
     99  1.9.10.3   yamt 		syscallarg(int) fd;
    100  1.9.10.3   yamt 		syscallarg(linux32_statfsp) sp;
    101  1.9.10.3   yamt 	} */
    102  1.9.10.3   yamt 	struct statvfs *sb;
    103  1.9.10.3   yamt 	struct linux_statfs ltmp;
    104  1.9.10.3   yamt 	int error;
    105  1.9.10.3   yamt 
    106  1.9.10.3   yamt 	sb = STATVFSBUF_GET();
    107  1.9.10.3   yamt 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
    108  1.9.10.3   yamt 	if (error == 0) {
    109  1.9.10.3   yamt 		bsd_to_linux_statfs(sb, &ltmp);
    110  1.9.10.3   yamt 		error = copyout(&ltmp, SCARG_P32(uap, sp), sizeof ltmp);
    111  1.9.10.3   yamt 	}
    112  1.9.10.3   yamt 	STATVFSBUF_PUT(sb);
    113  1.9.10.3   yamt 
    114  1.9.10.3   yamt 	return error;
    115  1.9.10.3   yamt }
    116  1.9.10.3   yamt 
    117  1.9.10.2   yamt extern const int linux_ptrace_request_map[];
    118  1.9.10.2   yamt 
    119  1.9.10.2   yamt int
    120  1.9.10.2   yamt linux32_sys_ptrace(struct lwp *l, const struct linux32_sys_ptrace_args *uap, register_t *retval)
    121  1.9.10.2   yamt {
    122  1.9.10.2   yamt 	/* {
    123  1.9.10.2   yamt 		i386, m68k, powerpc: T=int
    124  1.9.10.2   yamt 		alpha, amd64: T=long
    125  1.9.10.2   yamt 		syscallarg(T) request;
    126  1.9.10.2   yamt 		syscallarg(T) pid;
    127  1.9.10.2   yamt 		syscallarg(T) addr;
    128  1.9.10.2   yamt 		syscallarg(T) data;
    129  1.9.10.2   yamt 	} */
    130  1.9.10.2   yamt 	const int *ptr;
    131  1.9.10.2   yamt 	int request;
    132  1.9.10.2   yamt 	int error;
    133  1.9.10.2   yamt 
    134  1.9.10.2   yamt 	ptr = linux_ptrace_request_map;
    135  1.9.10.2   yamt 	request = SCARG(uap, request);
    136  1.9.10.2   yamt 	while (*ptr != -1)
    137  1.9.10.2   yamt 		if (*ptr++ == request) {
    138  1.9.10.2   yamt 			struct sys_ptrace_args pta;
    139  1.9.10.2   yamt 
    140  1.9.10.2   yamt 			SCARG(&pta, req) = *ptr;
    141  1.9.10.2   yamt 			SCARG(&pta, pid) = SCARG(uap, pid);
    142  1.9.10.2   yamt 			SCARG(&pta, addr) = NETBSD32IPTR64(SCARG(uap, addr));
    143  1.9.10.2   yamt 			SCARG(&pta, data) = SCARG(uap, data);
    144  1.9.10.2   yamt 
    145  1.9.10.2   yamt 			/*
    146  1.9.10.2   yamt 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
    147  1.9.10.2   yamt 			 * to continue where the process left off previously.
    148  1.9.10.2   yamt 			 * The same thing is achieved by addr == (void *) 1
    149  1.9.10.2   yamt 			 * on NetBSD, so rewrite 'addr' appropriately.
    150  1.9.10.2   yamt 			 */
    151  1.9.10.2   yamt 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
    152  1.9.10.2   yamt 				SCARG(&pta, addr) = (void *) 1;
    153  1.9.10.2   yamt 
    154  1.9.10.2   yamt 			error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
    155  1.9.10.2   yamt 			if (error)
    156  1.9.10.2   yamt 				return error;
    157  1.9.10.2   yamt 			switch (request) {
    158  1.9.10.2   yamt 			case LINUX_PTRACE_PEEKTEXT:
    159  1.9.10.2   yamt 			case LINUX_PTRACE_PEEKDATA:
    160  1.9.10.2   yamt 				error = copyout (retval,
    161  1.9.10.2   yamt 				    NETBSD32IPTR64(SCARG(uap, data)),
    162  1.9.10.2   yamt 				    sizeof *retval);
    163  1.9.10.2   yamt 				*retval = SCARG(uap, data);
    164  1.9.10.2   yamt 				break;
    165  1.9.10.2   yamt 			default:
    166  1.9.10.2   yamt 				break;
    167  1.9.10.2   yamt 			}
    168  1.9.10.2   yamt 			return error;
    169  1.9.10.2   yamt 		}
    170  1.9.10.2   yamt 		else
    171  1.9.10.2   yamt 			ptr++;
    172  1.9.10.2   yamt 
    173  1.9.10.2   yamt 	return EIO;
    174  1.9.10.2   yamt }
    175  1.9.10.2   yamt 
    176  1.9.10.2   yamt int
    177  1.9.10.2   yamt linux32_sys_personality(struct lwp *l, const struct linux32_sys_personality_args *uap, register_t *retval)
    178  1.9.10.2   yamt {
    179  1.9.10.2   yamt 	/* {
    180  1.9.10.4   yamt 		syscallarg(netbsd32_u_long) per;
    181  1.9.10.2   yamt 	} */
    182  1.9.10.2   yamt 
    183  1.9.10.2   yamt 	switch (SCARG(uap, per)) {
    184  1.9.10.2   yamt 	case LINUX_PER_LINUX:
    185  1.9.10.2   yamt 	case LINUX_PER_LINUX32:
    186  1.9.10.2   yamt 	case LINUX_PER_QUERY:
    187  1.9.10.2   yamt 		break;
    188  1.9.10.2   yamt 	default:
    189  1.9.10.2   yamt 		return EINVAL;
    190  1.9.10.2   yamt 	}
    191  1.9.10.2   yamt 
    192  1.9.10.2   yamt 	retval[0] = LINUX_PER_LINUX;
    193  1.9.10.2   yamt 	return 0;
    194  1.9.10.2   yamt }
    195  1.9.10.5   yamt 
    196  1.9.10.5   yamt int
    197  1.9.10.5   yamt linux32_sys_futex(struct lwp *l,
    198  1.9.10.5   yamt     const struct linux32_sys_futex_args *uap, register_t *retval)
    199  1.9.10.5   yamt {
    200  1.9.10.5   yamt 	/* {
    201  1.9.10.5   yamt 		syscallarg(linux32_intp_t) uaddr;
    202  1.9.10.5   yamt 		syscallarg(int) op;
    203  1.9.10.5   yamt 		syscallarg(int) val;
    204  1.9.10.5   yamt 		syscallarg(linux32_timespecp_t) timeout;
    205  1.9.10.5   yamt 		syscallarg(linux32_intp_t) uaddr2;
    206  1.9.10.5   yamt 		syscallarg(int) val3;
    207  1.9.10.5   yamt 	} */
    208  1.9.10.5   yamt 	struct linux_sys_futex_args ua;
    209  1.9.10.5   yamt 	struct linux32_timespec lts;
    210  1.9.10.5   yamt 	struct timespec ts = { 0, 0 };
    211  1.9.10.5   yamt 	int error;
    212  1.9.10.5   yamt 
    213  1.9.10.5   yamt 	NETBSD32TOP_UAP(uaddr, int);
    214  1.9.10.5   yamt 	NETBSD32TO64_UAP(op);
    215  1.9.10.5   yamt 	NETBSD32TO64_UAP(val);
    216  1.9.10.5   yamt 	NETBSD32TOP_UAP(timeout, struct linux_timespec);
    217  1.9.10.5   yamt 	NETBSD32TOP_UAP(uaddr2, int);
    218  1.9.10.5   yamt 	NETBSD32TO64_UAP(val3);
    219  1.9.10.5   yamt 	if ((SCARG(uap, op) & ~LINUX_FUTEX_PRIVATE_FLAG) == LINUX_FUTEX_WAIT &&
    220  1.9.10.5   yamt 	    SCARG_P32(uap, timeout) != NULL) {
    221  1.9.10.5   yamt 		if ((error = copyin((void *)SCARG_P32(uap, timeout),
    222  1.9.10.5   yamt 		    &lts, sizeof(lts))) != 0) {
    223  1.9.10.5   yamt 			return error;
    224  1.9.10.5   yamt 		}
    225  1.9.10.5   yamt 		linux32_to_native_timespec(&ts, &lts);
    226  1.9.10.5   yamt 	}
    227  1.9.10.5   yamt 	return linux_do_futex(l, &ua, retval, &ts);
    228  1.9.10.5   yamt }
    229  1.9.10.5   yamt 
    230  1.9.10.5   yamt int
    231  1.9.10.5   yamt linux32_sys_set_robust_list(struct lwp *l,
    232  1.9.10.5   yamt     const struct linux32_sys_set_robust_list_args *uap, register_t *retval)
    233  1.9.10.5   yamt {
    234  1.9.10.5   yamt 	/* {
    235  1.9.10.5   yamt 		syscallarg(linux32_robust_list_headp_t) head;
    236  1.9.10.5   yamt 		syscallarg(linux32_size_t) len;
    237  1.9.10.5   yamt 	} */
    238  1.9.10.5   yamt 	struct linux_sys_set_robust_list_args ua;
    239  1.9.10.5   yamt 	struct linux_emuldata *led;
    240  1.9.10.5   yamt 
    241  1.9.10.5   yamt 	if (SCARG(uap, len) != 12)
    242  1.9.10.5   yamt 		return EINVAL;
    243  1.9.10.5   yamt 
    244  1.9.10.5   yamt 	NETBSD32TOP_UAP(head, struct robust_list_head);
    245  1.9.10.5   yamt 	NETBSD32TOX64_UAP(len, size_t);
    246  1.9.10.5   yamt 
    247  1.9.10.5   yamt 	led = l->l_emuldata;
    248  1.9.10.5   yamt 	led->led_robust_head = SCARG(&ua, head);
    249  1.9.10.5   yamt 	*retval = 0;
    250  1.9.10.5   yamt 	return 0;
    251  1.9.10.5   yamt }
    252  1.9.10.5   yamt 
    253  1.9.10.5   yamt int
    254  1.9.10.5   yamt linux32_sys_get_robust_list(struct lwp *l,
    255  1.9.10.5   yamt     const struct linux32_sys_get_robust_list_args *uap, register_t *retval)
    256  1.9.10.5   yamt {
    257  1.9.10.5   yamt 	/* {
    258  1.9.10.5   yamt 		syscallarg(linux32_robust_list_headpp_t) head;
    259  1.9.10.5   yamt 		syscallarg(linux32_sizep_t) len;
    260  1.9.10.5   yamt 	} */
    261  1.9.10.5   yamt 	struct linux_sys_get_robust_list_args ua;
    262  1.9.10.5   yamt 
    263  1.9.10.5   yamt 	NETBSD32TOP_UAP(head, struct robust_list_head *);
    264  1.9.10.5   yamt 	NETBSD32TOP_UAP(head, size_t *);
    265  1.9.10.5   yamt 	return linux_sys_get_robust_list(l, &ua, retval);
    266  1.9.10.5   yamt }
    267