xref: /netbsd-src/sys/compat/linux32/common/linux32_misc.c (revision 62a8debe1dc62962e18a1c918def78666141273b)
1 /*	$NetBSD: linux32_misc.c,v 1.18 2009/11/24 10:42:44 njoly 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.18 2009/11/24 10:42:44 njoly 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_signal.h>
57 #include <compat/linux/common/linux_misc.h>
58 #include <compat/linux/common/linux_statfs.h>
59 #include <compat/linux/common/linux_ipc.h>
60 #include <compat/linux/common/linux_sem.h>
61 #include <compat/linux/linux_syscallargs.h>
62 
63 extern const struct linux_mnttypes linux_fstypes[];
64 extern const int linux_fstypes_cnt;
65 
66 
67 /*
68  * Implement the fs stat functions. Straightforward.
69  */
70 int
71 linux32_sys_statfs(struct lwp *l, const struct linux32_sys_statfs_args *uap, register_t *retval)
72 {
73 	/* {
74 		syscallarg(const netbsd32_charp char) path;
75 		syscallarg(linux32_statfsp) sp;
76 	} */
77 	struct statvfs *sb;
78 	struct linux_statfs ltmp;
79 	int error;
80 
81 	sb = STATVFSBUF_GET();
82 	error = do_sys_pstatvfs(l, SCARG_P32(uap, path), ST_WAIT, sb);
83 	if (error == 0) {
84 		bsd_to_linux_statfs(sb, &ltmp);
85 		error = copyout(&ltmp, SCARG_P32(uap, sp), sizeof ltmp);
86 	}
87 
88 	STATVFSBUF_PUT(sb);
89 	return error;
90 }
91 
92 int
93 linux32_sys_fstatfs(struct lwp *l, const struct linux32_sys_fstatfs_args *uap, register_t *retval)
94 {
95 	/* {
96 		syscallarg(int) fd;
97 		syscallarg(linux32_statfsp) sp;
98 	} */
99 	struct statvfs *sb;
100 	struct linux_statfs ltmp;
101 	int error;
102 
103 	sb = STATVFSBUF_GET();
104 	error = do_sys_fstatvfs(l, SCARG(uap, fd), ST_WAIT, sb);
105 	if (error == 0) {
106 		bsd_to_linux_statfs(sb, &ltmp);
107 		error = copyout(&ltmp, SCARG_P32(uap, sp), sizeof ltmp);
108 	}
109 	STATVFSBUF_PUT(sb);
110 
111 	return error;
112 }
113 
114 extern const int linux_ptrace_request_map[];
115 
116 int
117 linux32_sys_ptrace(struct lwp *l, const struct linux32_sys_ptrace_args *uap, register_t *retval)
118 {
119 	/* {
120 		i386, m68k, powerpc: T=int
121 		alpha, amd64: T=long
122 		syscallarg(T) request;
123 		syscallarg(T) pid;
124 		syscallarg(T) addr;
125 		syscallarg(T) data;
126 	} */
127 	const int *ptr;
128 	int request;
129 	int error;
130 
131 	ptr = linux_ptrace_request_map;
132 	request = SCARG(uap, request);
133 	while (*ptr != -1)
134 		if (*ptr++ == request) {
135 			struct sys_ptrace_args pta;
136 
137 			SCARG(&pta, req) = *ptr;
138 			SCARG(&pta, pid) = SCARG(uap, pid);
139 			SCARG(&pta, addr) = NETBSD32IPTR64(SCARG(uap, addr));
140 			SCARG(&pta, data) = SCARG(uap, data);
141 
142 			/*
143 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
144 			 * to continue where the process left off previously.
145 			 * The same thing is achieved by addr == (void *) 1
146 			 * on NetBSD, so rewrite 'addr' appropriately.
147 			 */
148 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
149 				SCARG(&pta, addr) = (void *) 1;
150 
151 			error = sysent[SYS_ptrace].sy_call(l, &pta, retval);
152 			if (error)
153 				return error;
154 			switch (request) {
155 			case LINUX_PTRACE_PEEKTEXT:
156 			case LINUX_PTRACE_PEEKDATA:
157 				error = copyout (retval,
158 				    NETBSD32IPTR64(SCARG(uap, data)),
159 				    sizeof *retval);
160 				*retval = SCARG(uap, data);
161 				break;
162 			default:
163 				break;
164 			}
165 			return error;
166 		}
167 		else
168 			ptr++;
169 
170 	return EIO;
171 }
172 
173 int
174 linux32_sys_personality(struct lwp *l, const struct linux32_sys_personality_args *uap, register_t *retval)
175 {
176 	/* {
177 		syscallarg(netbsd32_u_long) per;
178 	} */
179 
180 	switch (SCARG(uap, per)) {
181 	case LINUX_PER_LINUX:
182 	case LINUX_PER_LINUX32:
183 	case LINUX_PER_QUERY:
184 		break;
185 	default:
186 		return EINVAL;
187 	}
188 
189 	retval[0] = LINUX_PER_LINUX;
190 	return 0;
191 }
192