1 /*
2  * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Based on
3  *             sys_sparc32
4  *
5  * Copyright (C) 2000		VA Linux Co
6  * Copyright (C) 2000		Don Dugger <n0ano@valinux.com>
7  * Copyright (C) 1999 		Arun Sharma <arun.sharma@intel.com>
8  * Copyright (C) 1997,1998 	Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9  * Copyright (C) 1997 		David S. Miller (davem@caip.rutgers.edu)
10  * Copyright (C) 2000		Hewlett-Packard Co.
11  * Copyright (C) 2000		David Mosberger-Tang <davidm@hpl.hp.com>
12  * Copyright (C) 2000,2001,2002	Andi Kleen, SuSE Labs (x86-64 port)
13  *
14  * These routines maintain argument size conversion between 32bit and 64bit
15  * environment. In 2.5 most of this should be moved to a generic directory.
16  *
17  * This file assumes that there is a hole at the end of user address space.
18  *
19  * $Id: sys_ia32.c,v 1.70 2004/03/03 23:36:43 ak Exp $
20  */
21 
22 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/fs.h>
26 #include <linux/file.h>
27 #include <linux/signal.h>
28 #include <linux/utime.h>
29 #include <linux/resource.h>
30 #include <linux/times.h>
31 #include <linux/utsname.h>
32 #include <linux/timex.h>
33 #include <linux/smp.h>
34 #include <linux/smp_lock.h>
35 #include <linux/sem.h>
36 #include <linux/msg.h>
37 #include <linux/mm.h>
38 #include <linux/shm.h>
39 #include <linux/slab.h>
40 #include <linux/uio.h>
41 #include <linux/nfs_fs.h>
42 #include <linux/smb_fs.h>
43 #include <linux/smb_mount.h>
44 #include <linux/ncp_fs.h>
45 #include <linux/quota.h>
46 #include <linux/module.h>
47 #include <linux/sunrpc/svc.h>
48 #include <linux/nfsd/nfsd.h>
49 #include <linux/nfsd/cache.h>
50 #include <linux/nfsd/xdr.h>
51 #include <linux/nfsd/syscall.h>
52 #include <linux/poll.h>
53 #include <linux/personality.h>
54 #include <linux/stat.h>
55 #include <linux/ipc.h>
56 #include <linux/rwsem.h>
57 #include <linux/binfmts.h>
58 #include <linux/init.h>
59 #include <linux/highuid.h>
60 #include <asm/mman.h>
61 #include <asm/types.h>
62 #include <asm/uaccess.h>
63 #include <asm/semaphore.h>
64 #include <asm/ipc.h>
65 #include <asm/atomic.h>
66 #include <asm/ldt.h>
67 
68 #include <net/scm.h>
69 #include <net/sock.h>
70 #include <asm/ia32.h>
71 
72 #define A(__x)		((unsigned long)(__x))
73 #define AA(__x)		((unsigned long)(__x))
74 #define ROUND_UP(x,a)	((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
75 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
76 
77 #undef high2lowuid
78 #undef high2lowgid
79 #undef low2highuid
80 #undef low2highgid
81 
82 #define high2lowuid(uid) ((uid) > 65535) ? (u16)overflowuid : (u16)(uid)
83 #define high2lowgid(gid) ((gid) > 65535) ? (u16)overflowgid : (u16)(gid)
84 #define low2highuid(uid) ((uid) == (u16)-1) ? (uid_t)-1 : (uid_t)(uid)
85 #define low2highgid(gid) ((gid) == (u16)-1) ? (gid_t)-1 : (gid_t)(gid)
86 extern int overflowuid,overflowgid;
87 
88 typedef u16 old_uid_t;
89 typedef u16 old_gid_t;
90 
91 #include "../../../kernel/uid16.c"
92 
93 static int
putstat(struct stat32 * ubuf,struct stat * kbuf)94 putstat(struct stat32 *ubuf, struct stat *kbuf)
95 {
96 	if (kbuf->st_size > 0x7fffffff)
97 		return -EOVERFLOW;
98 	if (verify_area(VERIFY_WRITE, ubuf, sizeof(struct stat32)) ||
99 	    __put_user (kbuf->st_dev, &ubuf->st_dev) ||
100 	    __put_user (kbuf->st_ino, &ubuf->st_ino) ||
101 	    __put_user (kbuf->st_mode, &ubuf->st_mode) ||
102 	    __put_user (kbuf->st_nlink, &ubuf->st_nlink) ||
103 	    __put_user (high2lowuid(kbuf->st_uid), &ubuf->st_uid) ||
104 	    __put_user (high2lowgid(kbuf->st_gid), &ubuf->st_gid) ||
105 	    __put_user (kbuf->st_rdev, &ubuf->st_rdev) ||
106 	    __put_user (kbuf->st_size, &ubuf->st_size) ||
107 	    __put_user (kbuf->st_atime, &ubuf->st_atime) ||
108 	    __put_user (kbuf->st_mtime, &ubuf->st_mtime) ||
109 	    __put_user (kbuf->st_ctime, &ubuf->st_ctime) ||
110 	    __put_user (kbuf->st_blksize, &ubuf->st_blksize) ||
111 	    __put_user (kbuf->st_blocks, &ubuf->st_blocks))
112 		return -EFAULT;
113 	return 0;
114 }
115 
116 extern asmlinkage long sys_newstat(char * filename, struct stat * statbuf);
117 
118 asmlinkage long
sys32_newstat(char * filename,struct stat32 * statbuf)119 sys32_newstat(char * filename, struct stat32 *statbuf)
120 {
121 	char *name;
122 	int ret;
123 	struct stat s;
124 	mm_segment_t old_fs = get_fs();
125 
126 	name = getname(filename);
127 	if (IS_ERR(name))
128 		return PTR_ERR(name);
129 	set_fs (KERNEL_DS);
130 	ret = sys_newstat(name, &s);
131 	set_fs (old_fs);
132 	putname(name);
133 	if (ret)
134 	return ret;
135 	return putstat(statbuf, &s);
136 }
137 
138 extern asmlinkage long sys_newlstat(char * filename, struct stat * statbuf);
139 
140 asmlinkage long
sys32_newlstat(char * filename,struct stat32 * statbuf)141 sys32_newlstat(char * filename, struct stat32 *statbuf)
142 {
143 	char *name;
144 	int ret;
145 	struct stat s;
146 	mm_segment_t old_fs = get_fs();
147 
148 	name = getname(filename);
149 	if (IS_ERR(name))
150 		return PTR_ERR(name);
151 	set_fs (KERNEL_DS);
152 	ret = sys_newlstat(name, &s);
153 	set_fs (old_fs);
154 	putname(name);
155 	if (ret)
156 	return ret;
157 	return putstat(statbuf, &s);
158 }
159 
160 extern asmlinkage long sys_newfstat(unsigned int fd, struct stat * statbuf);
161 
162 asmlinkage long
sys32_newfstat(unsigned int fd,struct stat32 * statbuf)163 sys32_newfstat(unsigned int fd, struct stat32 *statbuf)
164 {
165 	int ret;
166 	struct stat s;
167 	mm_segment_t old_fs = get_fs();
168 
169 	set_fs (KERNEL_DS);
170 	ret = sys_newfstat(fd, &s);
171 	set_fs (old_fs);
172 	if (ret)
173 	return ret;
174 	return putstat(statbuf, &s);
175 }
176 
177 extern long sys_truncate(char *, loff_t);
178 
179 asmlinkage long
sys32_truncate64(char * filename,unsigned long offset_low,unsigned long offset_high)180 sys32_truncate64(char * filename, unsigned long offset_low, unsigned long offset_high)
181 {
182        return sys_truncate(filename, ((loff_t) offset_high << 32) | offset_low);
183 }
184 
185 extern long sys_ftruncate(int, loff_t);
186 
187 asmlinkage long
sys32_ftruncate64(unsigned int fd,unsigned long offset_low,unsigned long offset_high)188 sys32_ftruncate64(unsigned int fd, unsigned long offset_low, unsigned long offset_high)
189 {
190        return sys_ftruncate(fd, ((loff_t) offset_high << 32) | offset_low);
191 }
192 
193 /* Another set for IA32/LFS -- x86_64 struct stat is different due to
194    support for 64bit inode numbers. */
195 
196 static int
putstat64(struct stat64 * ubuf,struct stat * kbuf)197 putstat64(struct stat64 *ubuf, struct stat *kbuf)
198 {
199 	if (verify_area(VERIFY_WRITE, ubuf, sizeof(struct stat64)) ||
200 	    __put_user (kbuf->st_dev, &ubuf->st_dev) ||
201 	    __put_user (kbuf->st_ino, &ubuf->__st_ino) ||
202 	    __put_user (kbuf->st_ino, &ubuf->st_ino) ||
203 	    __put_user (kbuf->st_mode, &ubuf->st_mode) ||
204 	    __put_user (kbuf->st_nlink, &ubuf->st_nlink) ||
205 	    __put_user (kbuf->st_uid, &ubuf->st_uid) ||
206 	    __put_user (kbuf->st_gid, &ubuf->st_gid) ||
207 	    __put_user (kbuf->st_rdev, &ubuf->st_rdev) ||
208 	    __put_user (kbuf->st_size, &ubuf->st_size) ||
209 	    __put_user (kbuf->st_atime, &ubuf->st_atime) ||
210 	    __put_user (kbuf->st_mtime, &ubuf->st_mtime) ||
211 	    __put_user (kbuf->st_ctime, &ubuf->st_ctime) ||
212 	    __put_user (kbuf->st_blksize, &ubuf->st_blksize) ||
213 	    __put_user (kbuf->st_blocks, &ubuf->st_blocks))
214 		return -EFAULT;
215 	return 0;
216 }
217 
218 asmlinkage long
sys32_stat64(char * filename,struct stat64 * statbuf)219 sys32_stat64(char * filename, struct stat64 *statbuf)
220 {
221 	char *name;
222 	int ret;
223 	struct stat s;
224 	mm_segment_t old_fs = get_fs();
225 
226 	name = getname(filename);
227 	if (IS_ERR(name))
228 		return PTR_ERR(name);
229 	set_fs (KERNEL_DS);
230 	ret = sys_newstat(name, &s);
231 	set_fs (old_fs);
232 	putname(name);
233 	if (ret)
234 	return ret;
235 	return putstat64(statbuf, &s);
236 }
237 
238 asmlinkage long
sys32_lstat64(char * filename,struct stat64 * statbuf)239 sys32_lstat64(char * filename, struct stat64 *statbuf)
240 {
241 	char *name;
242 	int ret;
243 	struct stat s;
244 	mm_segment_t old_fs = get_fs();
245 
246 	name = getname(filename);
247 	if (IS_ERR(name))
248 		return PTR_ERR(name);
249 	set_fs (KERNEL_DS);
250 	ret = sys_newlstat(name, &s);
251 	set_fs (old_fs);
252 	putname(name);
253 	if (ret)
254 	return ret;
255 	return putstat64(statbuf, &s);
256 }
257 
258 asmlinkage long
sys32_fstat64(unsigned int fd,struct stat64 * statbuf)259 sys32_fstat64(unsigned int fd, struct stat64 *statbuf)
260 {
261 	int ret;
262 	struct stat s;
263 	mm_segment_t old_fs = get_fs();
264 
265 	set_fs (KERNEL_DS);
266 	ret = sys_newfstat(fd, &s);
267 	set_fs (old_fs);
268 	if (ret)
269 	return ret;
270 	return putstat64(statbuf, &s);
271 }
272 
273 /* Don't set O_LARGEFILE implicitely. */
sys32_open(const char * filename,int flags,int mode)274 asmlinkage long sys32_open(const char * filename, int flags, int mode)
275 {
276 	char * tmp;
277 	int fd, error;
278 
279 	tmp = getname(filename);
280 	fd = PTR_ERR(tmp);
281 	if (!IS_ERR(tmp)) {
282 		fd = get_unused_fd();
283 		if (fd >= 0) {
284 			struct file *f = filp_open(tmp, flags, mode);
285 			error = PTR_ERR(f);
286 			if (IS_ERR(f))
287 				goto out_error;
288 			fd_install(fd, f);
289 		}
290 out:
291 		putname(tmp);
292 	}
293 	return fd;
294 
295 out_error:
296 	put_unused_fd(fd);
297 	fd = error;
298 	goto out;
299 }
300 
301 /*
302  * Linux/i386 didn't use to be able to handle more than
303  * 4 system call parameters, so these system calls used a memory
304  * block for parameter passing..
305  */
306 
307 struct mmap_arg_struct {
308 	unsigned int addr;
309 	unsigned int len;
310 	unsigned int prot;
311 	unsigned int flags;
312 	unsigned int fd;
313 	unsigned int offset;
314 };
315 
316 asmlinkage long
sys32_mmap(struct mmap_arg_struct * arg)317 sys32_mmap(struct mmap_arg_struct *arg)
318 {
319 	struct mmap_arg_struct a;
320 	struct file *file = NULL;
321 	unsigned long retval;
322 	struct mm_struct *mm ;
323 
324 	if (copy_from_user(&a, arg, sizeof(a)))
325 		return -EFAULT;
326 
327 	if (a.offset & ~PAGE_MASK)
328 		return -EINVAL;
329 
330 	if (!(a.flags & MAP_ANONYMOUS)) {
331 		file = fget(a.fd);
332 		if (!file)
333 			return -EBADF;
334 	}
335 
336 	if (a.prot & PROT_READ)
337 		a.prot |= PROT_EXEC;
338 
339 	mm = current->mm;
340 	down_write(&mm->mmap_sem);
341 	retval = do_mmap_pgoff(file, a.addr, a.len, a.prot, a.flags, a.offset>>PAGE_SHIFT);
342 	if (file)
343 		fput(file);
344 
345 	up_write(&mm->mmap_sem);
346 
347 	return retval;
348 }
349 
350 extern asmlinkage long sys_mprotect(unsigned long start,size_t len,unsigned long prot);
351 
sys32_mprotect(unsigned long start,size_t len,unsigned long prot)352 asmlinkage long sys32_mprotect(unsigned long start, size_t len, unsigned long prot)
353 {
354 	if (prot & PROT_READ)
355 		prot |= PROT_EXEC;
356 	return sys_mprotect(start,len,prot);
357 }
358 
359 asmlinkage long
sys32_pipe(int * fd)360 sys32_pipe(int *fd)
361 {
362 	int retval;
363 	int fds[2];
364 
365 	retval = do_pipe(fds);
366 	if (retval)
367 		goto out;
368 	if (copy_to_user(fd, fds, sizeof(fds)))
369 		retval = -EFAULT;
370   out:
371 	return retval;
372 }
373 
374 asmlinkage long
sys32_rt_sigaction(int sig,struct sigaction32 * act,struct sigaction32 * oact,unsigned int sigsetsize)375 sys32_rt_sigaction(int sig, struct sigaction32 *act,
376 		   struct sigaction32 *oact,  unsigned int sigsetsize)
377 {
378 	struct k_sigaction new_ka, old_ka;
379 	int ret;
380 	sigset32_t set32;
381 
382 	/* XXX: Don't preclude handling different sized sigset_t's.  */
383 	if (sigsetsize != sizeof(sigset32_t))
384 		return -EINVAL;
385 
386 	if (act) {
387 		u32 handler, restorer;
388 
389 		if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
390 		    __get_user(handler, &act->sa_handler) ||
391 		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
392 		    __get_user(restorer, &act->sa_restorer)||
393 		    __copy_from_user(&set32, &act->sa_mask, sizeof(sigset32_t)))
394 			return -EFAULT;
395 		new_ka.sa.sa_handler = (void*)(u64)handler;
396 		new_ka.sa.sa_restorer = (void*)(u64)restorer;
397 
398 		/* FIXME: here we rely on _IA32_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
399 		switch (_NSIG_WORDS) {
400 		case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
401 				| (((long)set32.sig[7]) << 32);
402 		case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
403 				| (((long)set32.sig[5]) << 32);
404 		case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
405 				| (((long)set32.sig[3]) << 32);
406 		case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
407 				| (((long)set32.sig[1]) << 32);
408 		}
409 	}
410 
411 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
412 
413 	if (!ret && oact) {
414 		/* FIXME: here we rely on _IA32_NSIG_WORS to be >= than _NSIG_WORDS << 1 */
415 		switch (_NSIG_WORDS) {
416 		case 4:
417 			set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
418 			set32.sig[6] = old_ka.sa.sa_mask.sig[3];
419 		case 3:
420 			set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
421 			set32.sig[4] = old_ka.sa.sa_mask.sig[2];
422 		case 2:
423 			set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
424 			set32.sig[2] = old_ka.sa.sa_mask.sig[1];
425 		case 1:
426 			set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
427 			set32.sig[0] = old_ka.sa.sa_mask.sig[0];
428 		}
429 		if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
430 		    __put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
431 		    __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
432 		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
433 		    __copy_to_user(&oact->sa_mask, &set32, sizeof(sigset32_t)))
434 			return -EFAULT;
435 	}
436 
437 	return ret;
438 }
439 
440 asmlinkage long
sys32_sigaction(int sig,struct old_sigaction32 * act,struct old_sigaction32 * oact)441 sys32_sigaction (int sig, struct old_sigaction32 *act, struct old_sigaction32 *oact)
442 {
443         struct k_sigaction new_ka, old_ka;
444         int ret;
445 
446         if (act) {
447 		old_sigset32_t mask;
448 		u32 handler, restorer;
449 
450 		if (verify_area(VERIFY_READ, act, sizeof(*act)) ||
451 		    __get_user(handler, &act->sa_handler) ||
452 		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
453 		    __get_user(restorer, &act->sa_restorer) ||
454 		    __get_user(mask, &act->sa_mask))
455 			return -EFAULT;
456 		new_ka.sa.sa_handler = (void*)(u64)handler;
457 		new_ka.sa.sa_restorer = (void*)(u64)restorer;
458 		siginitset(&new_ka.sa.sa_mask, mask);
459         }
460 
461         ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
462 
463 	if (!ret && oact) {
464 		if (verify_area(VERIFY_WRITE, oact, sizeof(*oact)) ||
465 		    __put_user((long)old_ka.sa.sa_handler, &oact->sa_handler) ||
466 		    __put_user((long)old_ka.sa.sa_restorer, &oact->sa_restorer) ||
467 		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
468 		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
469 			return -EFAULT;
470         }
471 
472 	return ret;
473 }
474 
475 extern asmlinkage long sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset,
476 					  size_t sigsetsize);
477 
478 asmlinkage long
sys32_rt_sigprocmask(int how,sigset32_t * set,sigset32_t * oset,unsigned int sigsetsize)479 sys32_rt_sigprocmask(int how, sigset32_t *set, sigset32_t *oset,
480 		     unsigned int sigsetsize)
481 {
482 	sigset_t s;
483 	sigset32_t s32;
484 	int ret;
485 	mm_segment_t old_fs = get_fs();
486 
487 	if (set) {
488 		if (copy_from_user (&s32, set, sizeof(sigset32_t)))
489 			return -EFAULT;
490 		switch (_NSIG_WORDS) {
491 		case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
492 		case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
493 		case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
494 		case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
495 		}
496 	}
497 	set_fs (KERNEL_DS);
498 	ret = sys_rt_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL,
499 				 sigsetsize);
500 	set_fs (old_fs);
501 	if (ret) return ret;
502 	if (oset) {
503 		switch (_NSIG_WORDS) {
504 		case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
505 		case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
506 		case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
507 		case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
508 		}
509 		if (copy_to_user (oset, &s32, sizeof(sigset32_t)))
510 			return -EFAULT;
511 	}
512 	return 0;
513 }
514 
515 static int
put_statfs(struct statfs32 * ubuf,struct statfs * kbuf)516 put_statfs (struct statfs32 *ubuf, struct statfs *kbuf)
517 {
518 	if (verify_area(VERIFY_WRITE, ubuf, sizeof(struct statfs32)) ||
519 	    __put_user (kbuf->f_type, &ubuf->f_type) ||
520 	    __put_user (kbuf->f_bsize, &ubuf->f_bsize) ||
521 	    __put_user (kbuf->f_blocks, &ubuf->f_blocks) ||
522 	    __put_user (kbuf->f_bfree, &ubuf->f_bfree) ||
523 	    __put_user (kbuf->f_bavail, &ubuf->f_bavail) ||
524 	    __put_user (kbuf->f_files, &ubuf->f_files) ||
525 	    __put_user (kbuf->f_ffree, &ubuf->f_ffree) ||
526 	    __put_user (kbuf->f_namelen, &ubuf->f_namelen) ||
527 	    __put_user (kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
528 	    __put_user (kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
529 	    __put_user (0, &ubuf->f_spare[0]) ||
530 	    __put_user (0, &ubuf->f_spare[1]) ||
531 	    __put_user (0, &ubuf->f_spare[2]) ||
532 	    __put_user (0, &ubuf->f_spare[3]) ||
533 	    __put_user (0, &ubuf->f_spare[4]) ||
534 	    __put_user (0, &ubuf->f_spare[5]))
535 		return -EFAULT;
536 	return 0;
537 }
538 
539 extern asmlinkage long sys_statfs(const char * path, struct statfs * buf);
540 
541 asmlinkage long
sys32_statfs(const char * path,struct statfs32 * buf)542 sys32_statfs(const char * path, struct statfs32 *buf)
543 {
544 	int ret;
545 	struct statfs s;
546 	mm_segment_t old_fs = get_fs();
547 	const char *name;
548 
549 	name = getname(path);
550 	if (IS_ERR(name))
551 		return PTR_ERR(name);
552 	set_fs (KERNEL_DS);
553 	ret = sys_statfs(name, &s);
554 	set_fs (old_fs);
555 	putname(name);
556 	if (put_statfs(buf, &s))
557 		return -EFAULT;
558 	return ret;
559 }
560 
561 extern asmlinkage long sys_fstatfs(unsigned int fd, struct statfs * buf);
562 
563 asmlinkage long
sys32_fstatfs(unsigned int fd,struct statfs32 * buf)564 sys32_fstatfs(unsigned int fd, struct statfs32 *buf)
565 {
566 	int ret;
567 	struct statfs s;
568 	mm_segment_t old_fs = get_fs();
569 
570 	set_fs (KERNEL_DS);
571 	ret = sys_fstatfs(fd, &s);
572 	set_fs (old_fs);
573 	if (put_statfs(buf, &s))
574 		return -EFAULT;
575 	return ret;
576 }
577 
578 struct timeval32
579 {
580     int tv_sec, tv_usec;
581 };
582 
583 struct itimerval32
584 {
585     struct timeval32 it_interval;
586     struct timeval32 it_value;
587 };
588 
589 static long
get_tv32(struct timeval * o,struct timeval32 * i)590 get_tv32(struct timeval *o, struct timeval32 *i)
591 {
592 	int err = -EFAULT;
593 	if (access_ok(VERIFY_READ, i, sizeof(*i))) {
594 		err = __get_user(o->tv_sec, &i->tv_sec);
595 		err |= __get_user(o->tv_usec, &i->tv_usec);
596 	}
597 	return err;
598 }
599 
600 static long
put_tv32(struct timeval32 * o,struct timeval * i)601 put_tv32(struct timeval32 *o, struct timeval *i)
602 {
603 	int err = -EFAULT;
604 	if (access_ok(VERIFY_WRITE, o, sizeof(*o))) {
605 		err = __put_user(i->tv_sec, &o->tv_sec);
606 		err |= __put_user(i->tv_usec, &o->tv_usec);
607 	}
608 	return err;
609 }
610 
611 static long
get_it32(struct itimerval * o,struct itimerval32 * i)612 get_it32(struct itimerval *o, struct itimerval32 *i)
613 {
614 	int err = -EFAULT;
615 	if (access_ok(VERIFY_READ, i, sizeof(*i))) {
616 		err = __get_user(o->it_interval.tv_sec, &i->it_interval.tv_sec);
617 		err |= __get_user(o->it_interval.tv_usec, &i->it_interval.tv_usec);
618 		err |= __get_user(o->it_value.tv_sec, &i->it_value.tv_sec);
619 		err |= __get_user(o->it_value.tv_usec, &i->it_value.tv_usec);
620 	}
621 	return err;
622 }
623 
624 static long
put_it32(struct itimerval32 * o,struct itimerval * i)625 put_it32(struct itimerval32 *o, struct itimerval *i)
626 {
627 	int err = -EFAULT;
628 	if (access_ok(VERIFY_WRITE, o, sizeof(*o))) {
629 		err = __put_user(i->it_interval.tv_sec, &o->it_interval.tv_sec);
630 		err |= __put_user(i->it_interval.tv_usec, &o->it_interval.tv_usec);
631 		err |= __put_user(i->it_value.tv_sec, &o->it_value.tv_sec);
632 		err |= __put_user(i->it_value.tv_usec, &o->it_value.tv_usec);
633 	}
634 	return err;
635 }
636 
637 extern int do_getitimer(int which, struct itimerval *value);
638 
639 asmlinkage long
sys32_getitimer(int which,struct itimerval32 * it)640 sys32_getitimer(int which, struct itimerval32 *it)
641 {
642 	struct itimerval kit;
643 	int error;
644 
645 	error = do_getitimer(which, &kit);
646 	if (!error && put_it32(it, &kit))
647 		error = -EFAULT;
648 
649 	return error;
650 }
651 
652 extern int do_setitimer(int which, struct itimerval *, struct itimerval *);
653 
654 asmlinkage long
sys32_setitimer(int which,struct itimerval32 * in,struct itimerval32 * out)655 sys32_setitimer(int which, struct itimerval32 *in, struct itimerval32 *out)
656 {
657 	struct itimerval kin, kout;
658 	int error;
659 
660 	if (in) {
661 		if (get_it32(&kin, in))
662 			return -EFAULT;
663 	} else
664 		memset(&kin, 0, sizeof(kin));
665 
666 	error = do_setitimer(which, &kin, out ? &kout : NULL);
667 	if (error || !out)
668 		return error;
669 	if (put_it32(out, &kout))
670 		return -EFAULT;
671 
672 	return 0;
673 
674 }
675 
676 asmlinkage long
sys32_alarm(unsigned int seconds)677 sys32_alarm(unsigned int seconds)
678 {
679 	struct itimerval it_new, it_old;
680 	unsigned int oldalarm;
681 
682 	it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
683 	it_new.it_value.tv_sec = seconds;
684 	it_new.it_value.tv_usec = 0;
685 	do_setitimer(ITIMER_REAL, &it_new, &it_old);
686 	oldalarm = it_old.it_value.tv_sec;
687 	/* ehhh.. We can't return 0 if we have an alarm pending.. */
688 	/* And we'd better return too much than too little anyway */
689 	if (it_old.it_value.tv_usec)
690 		oldalarm++;
691 	return oldalarm;
692 }
693 
694 /* Translations due to time_t size differences.  Which affects all
695    sorts of things, like timeval and itimerval.  */
696 
697 struct utimbuf_32 {
698 	int	atime;
699 	int	mtime;
700 };
701 
702 extern asmlinkage long sys_utimes(char * filename, struct timeval * utimes);
703 extern asmlinkage long sys_gettimeofday (struct timeval *tv, struct timezone *tz);
704 
705 extern struct timezone sys_tz;
706 extern int do_sys_settimeofday(struct timeval *tv, struct timezone *tz);
707 
708 asmlinkage long
sys32_gettimeofday(struct timeval32 * tv,struct timezone * tz)709 sys32_gettimeofday(struct timeval32 *tv, struct timezone *tz)
710 {
711 	if (tv) {
712 		struct timeval ktv;
713 		do_gettimeofday(&ktv);
714 		if (put_tv32(tv, &ktv))
715 			return -EFAULT;
716 	}
717 	if (tz) {
718 		if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
719 			return -EFAULT;
720 	}
721 	return 0;
722 }
723 
724 asmlinkage long
sys32_settimeofday(struct timeval32 * tv,struct timezone * tz)725 sys32_settimeofday(struct timeval32 *tv, struct timezone *tz)
726 {
727 	struct timeval ktv;
728 	struct timezone ktz;
729 
730  	if (tv) {
731 		if (get_tv32(&ktv, tv))
732 			return -EFAULT;
733 	}
734 	if (tz) {
735 		if (copy_from_user(&ktz, tz, sizeof(ktz)))
736 			return -EFAULT;
737 	}
738 
739 	return do_sys_settimeofday(tv ? &ktv : NULL, tz ? &ktz : NULL);
740 }
741 
742 struct linux32_dirent {
743 	u32	d_ino;
744 	u32	d_off;
745 	u16	d_reclen;
746 	char	d_name[1];
747 };
748 
749 struct old_linux32_dirent {
750 	u32	d_ino;
751 	u32	d_offset;
752 	u16	d_namlen;
753 	char	d_name[1];
754 };
755 
756 struct getdents32_callback {
757 	struct linux32_dirent * current_dir;
758 	struct linux32_dirent * previous;
759 	int count;
760 	int error;
761 };
762 
763 struct readdir32_callback {
764 	struct old_linux32_dirent * dirent;
765 	int count;
766 };
767 
768 static int
filldir32(void * __buf,const char * name,int namlen,loff_t offset,ino_t ino,unsigned int d_type)769 filldir32 (void *__buf, const char *name, int namlen, loff_t offset, ino_t ino,
770 	   unsigned int d_type)
771 {
772 	struct linux32_dirent * dirent;
773 	struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
774 	int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1, 4);
775 
776 	buf->error = -EINVAL;	/* only used if we fail.. */
777 	if (reclen > buf->count)
778 		return -EINVAL;
779 	dirent = buf->previous;
780 	if (dirent)
781 		put_user(offset, &dirent->d_off);
782 	dirent = buf->current_dir;
783 	buf->previous = dirent;
784 	put_user(ino, &dirent->d_ino);
785 	put_user(reclen, &dirent->d_reclen);
786 	copy_to_user(dirent->d_name, name, namlen);
787 	put_user(0, dirent->d_name + namlen);
788 	dirent = ((void *)dirent) + reclen;
789 	buf->current_dir = dirent;
790 	buf->count -= reclen;
791 	return 0;
792 }
793 
794 asmlinkage long
sys32_getdents(unsigned int fd,void * dirent,unsigned int count)795 sys32_getdents (unsigned int fd, void * dirent, unsigned int count)
796 {
797 	struct file * file;
798 	struct linux32_dirent * lastdirent;
799 	struct getdents32_callback buf;
800 	int error;
801 
802 	error = -EBADF;
803 	file = fget(fd);
804 	if (!file)
805 		goto out;
806 
807 	buf.current_dir = (struct linux32_dirent *) dirent;
808 	buf.previous = NULL;
809 	buf.count = count;
810 	buf.error = 0;
811 
812 	error = vfs_readdir(file, filldir32, &buf);
813 	if (error < 0)
814 		goto out_putf;
815 	error = buf.error;
816 	lastdirent = buf.previous;
817 	if (lastdirent) {
818 		put_user(file->f_pos, &lastdirent->d_off);
819 		error = count - buf.count;
820 	}
821 
822 out_putf:
823 	fput(file);
824 out:
825 	return error;
826 }
827 
828 static int
fillonedir32(void * __buf,const char * name,int namlen,loff_t offset,ino_t ino,unsigned d_type)829 fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, ino_t ino, unsigned d_type)
830 {
831 	struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
832 	struct old_linux32_dirent * dirent;
833 
834 	if (buf->count)
835 		return -EINVAL;
836 	buf->count++;
837 	dirent = buf->dirent;
838 	put_user(ino, &dirent->d_ino);
839 	put_user(offset, &dirent->d_offset);
840 	put_user(namlen, &dirent->d_namlen);
841 	copy_to_user(dirent->d_name, name, namlen);
842 	put_user(0, dirent->d_name + namlen);
843 	return 0;
844 }
845 
846 asmlinkage long
sys32_oldreaddir(unsigned int fd,void * dirent,unsigned int count)847 sys32_oldreaddir (unsigned int fd, void * dirent, unsigned int count)
848 {
849 	int error;
850 	struct file * file;
851 	struct readdir32_callback buf;
852 
853 	error = -EBADF;
854 	file = fget(fd);
855 	if (!file)
856 		goto out;
857 
858 	buf.count = 0;
859 	buf.dirent = dirent;
860 
861 	error = vfs_readdir(file, fillonedir32, &buf);
862 	if (error >= 0)
863 		error = buf.count;
864 	fput(file);
865 out:
866 	return error;
867 }
868 
869 /*
870  * We can actually return ERESTARTSYS instead of EINTR, but I'd
871  * like to be certain this leads to no problems. So I return
872  * EINTR just for safety.
873  *
874  * Update: ERESTARTSYS breaks at least the xview clock binary, so
875  * I'm trying ERESTARTNOHAND which restart only when you want to.
876  */
877 #define MAX_SELECT_SECONDS \
878 	((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
879 #define ROUND_UP_TIME(x,y) (((x)+(y)-1)/(y))
880 
881 asmlinkage long
sys32_select(int n,fd_set * inp,fd_set * outp,fd_set * exp,struct timeval32 * tvp32)882 sys32_select(int n, fd_set *inp, fd_set *outp, fd_set *exp, struct timeval32 *tvp32)
883 {
884 	fd_set_bits fds;
885 	char *bits;
886 	long timeout;
887 	int ret, size;
888 
889 	timeout = MAX_SCHEDULE_TIMEOUT;
890 	if (tvp32) {
891 		time_t sec, usec;
892 
893 		get_user(sec, &tvp32->tv_sec);
894 		get_user(usec, &tvp32->tv_usec);
895 
896 		ret = -EINVAL;
897 		if (sec < 0 || usec < 0)
898 			goto out_nofds;
899 
900 		if ((unsigned long) sec < MAX_SELECT_SECONDS) {
901 			timeout = ROUND_UP_TIME(usec, 1000000/HZ);
902 			timeout += sec * (unsigned long) HZ;
903 		}
904 	}
905 
906 	ret = -EINVAL;
907 	if (n < 0)
908 		goto out_nofds;
909 	size = FDS_BYTES(n);
910 
911 	if (n > current->files->max_fdset)
912 		n = current->files->max_fdset;
913 
914 	/*
915 	 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
916 	 * since we used fdset we need to allocate memory in units of
917 	 * long-words.
918 	 */
919 	ret = -ENOMEM;
920 	bits = kmalloc(6 * size, GFP_KERNEL);
921 	if (!bits)
922 		goto out_nofds;
923 	fds.in      = (unsigned long *)  bits;
924 	fds.out     = (unsigned long *) (bits +   size);
925 	fds.ex      = (unsigned long *) (bits + 2*size);
926 	fds.res_in  = (unsigned long *) (bits + 3*size);
927 	fds.res_out = (unsigned long *) (bits + 4*size);
928 	fds.res_ex  = (unsigned long *) (bits + 5*size);
929 
930 	if ((ret = get_fd_set(n, inp, fds.in)) ||
931 	    (ret = get_fd_set(n, outp, fds.out)) ||
932 	    (ret = get_fd_set(n, exp, fds.ex)))
933 		goto out;
934 	zero_fd_set(n, fds.res_in);
935 	zero_fd_set(n, fds.res_out);
936 	zero_fd_set(n, fds.res_ex);
937 
938 	ret = do_select(n, &fds, &timeout);
939 
940 	if (tvp32 && !(current->personality & STICKY_TIMEOUTS)) {
941 		time_t sec = 0, usec = 0;
942 		if (timeout) {
943 			sec = timeout / HZ;
944 			usec = timeout % HZ;
945 			usec *= (1000000/HZ);
946 		}
947 		put_user(sec, (int *)&tvp32->tv_sec);
948 		put_user(usec, (int *)&tvp32->tv_usec);
949 	}
950 
951 	if (ret < 0)
952 		goto out;
953 	if (!ret) {
954 		ret = -ERESTARTNOHAND;
955 		if (signal_pending(current))
956 			goto out;
957 		ret = 0;
958 	}
959 
960 	set_fd_set(n, inp, fds.res_in);
961 	set_fd_set(n, outp, fds.res_out);
962 	set_fd_set(n, exp, fds.res_ex);
963 
964 out:
965 	kfree(bits);
966 out_nofds:
967 	return ret;
968 }
969 
970 struct sel_arg_struct {
971 	unsigned int n;
972 	unsigned int inp;
973 	unsigned int outp;
974 	unsigned int exp;
975 	unsigned int tvp;
976 };
977 
978 asmlinkage long
sys32_old_select(struct sel_arg_struct * arg)979 sys32_old_select(struct sel_arg_struct *arg)
980 {
981 	struct sel_arg_struct a;
982 
983 	if (copy_from_user(&a, arg, sizeof(a)))
984 		return -EFAULT;
985 	return sys32_select(a.n, (fd_set *)A(a.inp), (fd_set *)A(a.outp), (fd_set *)A(a.exp),
986 			    (struct timeval32 *)A(a.tvp));
987 }
988 
989 extern asmlinkage long sys_nanosleep(struct timespec *rqtp, struct timespec *rmtp);
990 
991 asmlinkage long
sys32_nanosleep(struct timespec32 * rqtp,struct timespec32 * rmtp)992 sys32_nanosleep(struct timespec32 *rqtp, struct timespec32 *rmtp)
993 {
994 	struct timespec t, tout;
995 	int ret;
996 	mm_segment_t old_fs = get_fs ();
997 
998 	if (rqtp) {
999 	if (verify_area(VERIFY_READ, rqtp, sizeof(struct timespec32)) ||
1000 	    __get_user (t.tv_sec, &rqtp->tv_sec) ||
1001 	    __get_user (t.tv_nsec, &rqtp->tv_nsec))
1002 		return -EFAULT;
1003 	}
1004 	set_fs (KERNEL_DS);
1005 	ret = sys_nanosleep(rqtp ? &t : NULL, rmtp ? &tout : NULL);
1006 	set_fs (old_fs);
1007 	if (rmtp && ret == -EINTR) {
1008 		if (verify_area(VERIFY_WRITE, rmtp, sizeof(struct timespec32)) ||
1009 		    __put_user (tout.tv_sec, &rmtp->tv_sec) ||
1010 	    	    __put_user (tout.tv_nsec, &rmtp->tv_nsec))
1011 			return -EFAULT;
1012 	}
1013 	return ret;
1014 }
1015 
1016 asmlinkage ssize_t sys_readv(unsigned long,const struct iovec *,unsigned long);
1017 asmlinkage ssize_t sys_writev(unsigned long,const struct iovec *,unsigned long);
1018 
1019 static struct iovec *
get_iovec32(struct iovec32 * iov32,struct iovec * iov_buf,u32 * count,int type,int * errp)1020 get_iovec32(struct iovec32 *iov32, struct iovec *iov_buf, u32 *count, int type, int *errp)
1021 {
1022 	int i;
1023 	u32 buf, len;
1024 	struct iovec *ivp, *iov;
1025 	unsigned long totlen;
1026 
1027 	/* Get the "struct iovec" from user memory */
1028 
1029 	*errp = 0;
1030 	if (!*count)
1031 		return 0;
1032 	*errp = -EINVAL;
1033 	if (*count > UIO_MAXIOV)
1034 		return(struct iovec *)0;
1035 	*errp = -EFAULT;
1036 	if(verify_area(VERIFY_READ, iov32, sizeof(struct iovec32)**count))
1037 		return(struct iovec *)0;
1038 	if (*count > UIO_FASTIOV) {
1039 		*errp = -ENOMEM;
1040 		iov = kmalloc(*count*sizeof(struct iovec), GFP_KERNEL);
1041 		if (!iov)
1042 			return((struct iovec *)0);
1043 	} else
1044 		iov = iov_buf;
1045 
1046 	ivp = iov;
1047 	totlen = 0;
1048 	for (i = 0; i < *count; i++) {
1049 		*errp = __get_user(len, &iov32->iov_len) |
1050 		  	__get_user(buf, &iov32->iov_base);
1051 		if (*errp)
1052 			goto error;
1053 		*errp = verify_area(type, (void *)A(buf), len);
1054 		if (*errp) {
1055 			if (i > 0) {
1056 				*count = i;
1057 				break;
1058 			}
1059 			goto error;
1060 		}
1061 		/* SuS checks: */
1062 		*errp = -EINVAL;
1063 		if ((int)len < 0)
1064 			goto error;
1065 		if ((totlen += len) >= 0x7fffffff)
1066 			goto error;
1067 		ivp->iov_base = (void *)A(buf);
1068 		ivp->iov_len = (__kernel_size_t)len;
1069 		iov32++;
1070 		ivp++;
1071 	}
1072 	*errp = 0;
1073 	return(iov);
1074 
1075 error:
1076 	if (iov != iov_buf)
1077 		kfree(iov);
1078 	return NULL;
1079 }
1080 
1081 asmlinkage long
sys32_readv(int fd,struct iovec32 * vector,u32 count)1082 sys32_readv(int fd, struct iovec32 *vector, u32 count)
1083 {
1084 	struct iovec iovstack[UIO_FASTIOV];
1085 	struct iovec *iov;
1086 	int ret;
1087 	mm_segment_t old_fs = get_fs();
1088 
1089 	if ((iov = get_iovec32(vector, iovstack, &count, VERIFY_WRITE, &ret)) == NULL)
1090 		return ret;
1091 	set_fs(KERNEL_DS);
1092 	ret = sys_readv(fd, iov, count);
1093 	set_fs(old_fs);
1094 	if (iov != iovstack)
1095 		kfree(iov);
1096 	return ret;
1097 }
1098 
1099 asmlinkage long
sys32_writev(int fd,struct iovec32 * vector,u32 count)1100 sys32_writev(int fd, struct iovec32 *vector, u32 count)
1101 {
1102 	struct iovec iovstack[UIO_FASTIOV];
1103 	struct iovec *iov;
1104 	int ret;
1105 	mm_segment_t old_fs = get_fs();
1106 
1107 	if ((iov = get_iovec32(vector, iovstack, &count, VERIFY_READ, &ret)) == NULL)
1108 		return ret;
1109 	set_fs(KERNEL_DS);
1110 	ret = sys_writev(fd, iov, count);
1111 	set_fs(old_fs);
1112 	if (iov != iovstack)
1113 		kfree(iov);
1114 	return ret;
1115 }
1116 
1117 #define RLIM_INFINITY32	0xffffffff
1118 #define RESOURCE32(x) ((x > RLIM_INFINITY32) ? RLIM_INFINITY32 : x)
1119 
1120 struct rlimit32 {
1121 	unsigned	rlim_cur;
1122 	unsigned	rlim_max;
1123 };
1124 
1125 extern asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit *rlim);
1126 
1127 asmlinkage long
sys32_getrlimit(unsigned int resource,struct rlimit32 * rlim)1128 sys32_getrlimit(unsigned int resource, struct rlimit32 *rlim)
1129 {
1130 	struct rlimit r;
1131 	int ret;
1132 	mm_segment_t old_fs;
1133 
1134 	old_fs = get_fs();
1135 	set_fs(KERNEL_DS);
1136 	ret = sys_getrlimit(resource, &r);
1137 	set_fs(old_fs);
1138 	if (!ret) {
1139 		if (r.rlim_cur >= 0xffffffff)
1140 			r.rlim_cur = RLIM_INFINITY32;
1141 		if (r.rlim_max >= 0xffffffff)
1142 			r.rlim_max = RLIM_INFINITY32;
1143 		if (verify_area(VERIFY_WRITE, rlim, sizeof(struct rlimit32)) ||
1144 		    __put_user(RESOURCE32(r.rlim_cur), &rlim->rlim_cur) ||
1145 		    __put_user(RESOURCE32(r.rlim_max), &rlim->rlim_max))
1146 			ret = -EFAULT;
1147 	}
1148 	return ret;
1149 }
1150 
1151 extern asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit *rlim);
1152 
1153 asmlinkage long
sys32_old_getrlimit(unsigned int resource,struct rlimit32 * rlim)1154 sys32_old_getrlimit(unsigned int resource, struct rlimit32 *rlim)
1155 {
1156 	struct rlimit r;
1157 	int ret;
1158 	mm_segment_t old_fs;
1159 
1160 	old_fs = get_fs();
1161 	set_fs(KERNEL_DS);
1162 	ret = sys_getrlimit(resource, &r);
1163 	set_fs(old_fs);
1164 	if (!ret) {
1165 		if (r.rlim_cur >= 0x7fffffff)
1166 			r.rlim_cur = RLIM_INFINITY32;
1167 		if (r.rlim_max >= 0x7fffffff)
1168 			r.rlim_max = RLIM_INFINITY32;
1169 		if (verify_area(VERIFY_WRITE, rlim, sizeof(struct rlimit32)) ||
1170 		    __put_user(r.rlim_cur, &rlim->rlim_cur) ||
1171 		    __put_user(r.rlim_max, &rlim->rlim_max))
1172 			ret = -EFAULT;
1173 	}
1174 	return ret;
1175 }
1176 
1177 extern asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit *rlim);
1178 
1179 asmlinkage long
sys32_setrlimit(unsigned int resource,struct rlimit32 * rlim)1180 sys32_setrlimit(unsigned int resource, struct rlimit32 *rlim)
1181 {
1182 	struct rlimit r;
1183 	int ret;
1184 	mm_segment_t old_fs = get_fs ();
1185 
1186 	if (resource >= RLIM_NLIMITS) return -EINVAL;
1187 	if (verify_area(VERIFY_READ, rlim, sizeof(struct rlimit32)) ||
1188 	    __get_user (r.rlim_cur, &rlim->rlim_cur) ||
1189 	    __get_user (r.rlim_max, &rlim->rlim_max))
1190 		return -EFAULT;
1191 	if (r.rlim_cur == RLIM_INFINITY32)
1192 		r.rlim_cur = RLIM_INFINITY;
1193 	if (r.rlim_max == RLIM_INFINITY32)
1194 		r.rlim_max = RLIM_INFINITY;
1195 	set_fs (KERNEL_DS);
1196 	ret = sys_setrlimit(resource, &r);
1197 	set_fs (old_fs);
1198 	return ret;
1199 }
1200 
1201 /*
1202  * sys_time() can be implemented in user-level using
1203  * sys_gettimeofday().  x86-64 did this but i386 Linux did not
1204  * so we have to implement this system call here.
1205  */
sys32_time(int * tloc)1206 asmlinkage long sys32_time(int * tloc)
1207 {
1208 	int i;
1209 
1210 	/* SMP: This is fairly trivial. We grab CURRENT_TIME and
1211 	   stuff it to user space. No side effects */
1212 	i = CURRENT_TIME;
1213 	if (tloc) {
1214 		if (put_user(i,tloc))
1215 			i = -EFAULT;
1216 	}
1217 	return i;
1218 }
1219 
1220 struct rusage32 {
1221         struct timeval32 ru_utime;
1222         struct timeval32 ru_stime;
1223         int    ru_maxrss;
1224         int    ru_ixrss;
1225         int    ru_idrss;
1226         int    ru_isrss;
1227         int    ru_minflt;
1228         int    ru_majflt;
1229         int    ru_nswap;
1230         int    ru_inblock;
1231         int    ru_oublock;
1232         int    ru_msgsnd;
1233         int    ru_msgrcv;
1234         int    ru_nsignals;
1235         int    ru_nvcsw;
1236         int    ru_nivcsw;
1237 };
1238 
1239 static int
put_rusage(struct rusage32 * ru,struct rusage * r)1240 put_rusage (struct rusage32 *ru, struct rusage *r)
1241 {
1242 	if (verify_area(VERIFY_WRITE, ru, sizeof(struct rusage32)) ||
1243 	    __put_user (r->ru_utime.tv_sec, &ru->ru_utime.tv_sec) ||
1244 	    __put_user (r->ru_utime.tv_usec, &ru->ru_utime.tv_usec) ||
1245 	    __put_user (r->ru_stime.tv_sec, &ru->ru_stime.tv_sec) ||
1246 	    __put_user (r->ru_stime.tv_usec, &ru->ru_stime.tv_usec) ||
1247 	    __put_user (r->ru_maxrss, &ru->ru_maxrss) ||
1248 	    __put_user (r->ru_ixrss, &ru->ru_ixrss) ||
1249 	    __put_user (r->ru_idrss, &ru->ru_idrss) ||
1250 	    __put_user (r->ru_isrss, &ru->ru_isrss) ||
1251 	    __put_user (r->ru_minflt, &ru->ru_minflt) ||
1252 	    __put_user (r->ru_majflt, &ru->ru_majflt) ||
1253 	    __put_user (r->ru_nswap, &ru->ru_nswap) ||
1254 	    __put_user (r->ru_inblock, &ru->ru_inblock) ||
1255 	    __put_user (r->ru_oublock, &ru->ru_oublock) ||
1256 	    __put_user (r->ru_msgsnd, &ru->ru_msgsnd) ||
1257 	    __put_user (r->ru_msgrcv, &ru->ru_msgrcv) ||
1258 	    __put_user (r->ru_nsignals, &ru->ru_nsignals) ||
1259 	    __put_user (r->ru_nvcsw, &ru->ru_nvcsw) ||
1260 	    __put_user (r->ru_nivcsw, &ru->ru_nivcsw))
1261 		return -EFAULT;
1262 	return 0;
1263 }
1264 
1265 extern asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr,
1266 				int options, struct rusage * ru);
1267 
1268 asmlinkage long
sys32_wait4(__kernel_pid_t32 pid,unsigned int * stat_addr,int options,struct rusage32 * ru)1269 sys32_wait4(__kernel_pid_t32 pid, unsigned int *stat_addr, int options,
1270 	    struct rusage32 *ru)
1271 {
1272 	if (!ru)
1273 		return sys_wait4(pid, stat_addr, options, NULL);
1274 	else {
1275 		struct rusage r;
1276 		int ret;
1277 		unsigned int status;
1278 		mm_segment_t old_fs = get_fs();
1279 
1280 		set_fs (KERNEL_DS);
1281 		ret = sys_wait4(pid, stat_addr ? &status : NULL, options, &r);
1282 		set_fs (old_fs);
1283 		if (put_rusage (ru, &r)) return -EFAULT;
1284 		if (stat_addr && put_user (status, stat_addr))
1285 			return -EFAULT;
1286 		return ret;
1287 	}
1288 }
1289 
1290 asmlinkage long
sys32_waitpid(__kernel_pid_t32 pid,unsigned int * stat_addr,int options)1291 sys32_waitpid(__kernel_pid_t32 pid, unsigned int *stat_addr, int options)
1292 {
1293 	return sys32_wait4(pid, stat_addr, options, NULL);
1294 }
1295 
1296 extern asmlinkage long
1297 sys_getrusage(int who, struct rusage *ru);
1298 
1299 asmlinkage long
sys32_getrusage(int who,struct rusage32 * ru)1300 sys32_getrusage(int who, struct rusage32 *ru)
1301 {
1302 	struct rusage r;
1303 	int ret;
1304 	mm_segment_t old_fs = get_fs();
1305 
1306 	set_fs (KERNEL_DS);
1307 	ret = sys_getrusage(who, &r);
1308 	set_fs (old_fs);
1309 	if (put_rusage (ru, &r)) return -EFAULT;
1310 	return ret;
1311 }
1312 
1313 struct tms32 {
1314 	__kernel_clock_t32 tms_utime;
1315 	__kernel_clock_t32 tms_stime;
1316 	__kernel_clock_t32 tms_cutime;
1317 	__kernel_clock_t32 tms_cstime;
1318 };
1319 
1320 extern int sys_times(struct tms *);
1321 
1322 asmlinkage long
sys32_times(struct tms32 * tbuf)1323 sys32_times(struct tms32 *tbuf)
1324 {
1325 	struct tms t;
1326 	long ret;
1327 	mm_segment_t old_fs = get_fs ();
1328 
1329 	set_fs (KERNEL_DS);
1330 	ret = sys_times(tbuf ? &t : NULL);
1331 	set_fs (old_fs);
1332 	if (tbuf) {
1333 		if (verify_area(VERIFY_WRITE, tbuf, sizeof(struct tms32)) ||
1334 		    __put_user (t.tms_utime, &tbuf->tms_utime) ||
1335 		    __put_user (t.tms_stime, &tbuf->tms_stime) ||
1336 		    __put_user (t.tms_cutime, &tbuf->tms_cutime) ||
1337 		    __put_user (t.tms_cstime, &tbuf->tms_cstime))
1338 			return -EFAULT;
1339 	}
1340 	return ret;
1341 }
1342 
1343 
get_flock(struct flock * kfl,struct flock32 * ufl)1344 static inline int get_flock(struct flock *kfl, struct flock32 *ufl)
1345 {
1346 	int err;
1347 
1348 	err = get_user(kfl->l_type, &ufl->l_type);
1349 	err |= __get_user(kfl->l_whence, &ufl->l_whence);
1350 	err |= __get_user(kfl->l_start, &ufl->l_start);
1351 	err |= __get_user(kfl->l_len, &ufl->l_len);
1352 	err |= __get_user(kfl->l_pid, &ufl->l_pid);
1353 	return err;
1354 }
1355 
put_flock(struct flock * kfl,struct flock32 * ufl)1356 static inline int put_flock(struct flock *kfl, struct flock32 *ufl)
1357 {
1358 	int err;
1359 
1360 	err = __put_user(kfl->l_type, &ufl->l_type);
1361 	err |= __put_user(kfl->l_whence, &ufl->l_whence);
1362 	err |= __put_user(kfl->l_start, &ufl->l_start);
1363 	err |= __put_user(kfl->l_len, &ufl->l_len);
1364 	err |= __put_user(kfl->l_pid, &ufl->l_pid);
1365 	return err;
1366 }
1367 
1368 extern asmlinkage long sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg);
1369 asmlinkage long sys32_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg);
1370 
1371 
sys32_fcntl(unsigned int fd,unsigned int cmd,unsigned long arg)1372 asmlinkage long sys32_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
1373 {
1374 	switch (cmd) {
1375 	case F_GETLK:
1376 	case F_SETLK:
1377 	case F_SETLKW:
1378 		{
1379 			struct flock f;
1380 			mm_segment_t old_fs;
1381 			long ret;
1382 
1383 			if (get_flock(&f, (struct flock32 *)arg))
1384 				return -EFAULT;
1385 			old_fs = get_fs(); set_fs (KERNEL_DS);
1386 			ret = sys_fcntl(fd, cmd, (unsigned long)&f);
1387 			set_fs (old_fs);
1388 			if (ret) return ret;
1389 			if (put_flock(&f, (struct flock32 *)arg))
1390 				return -EFAULT;
1391 			return 0;
1392 		}
1393 	case F_GETLK64:
1394 	case F_SETLK64:
1395 	case F_SETLKW64:
1396 		return sys32_fcntl64(fd,cmd,arg);
1397 
1398 	default:
1399 		return sys_fcntl(fd, cmd, (unsigned long)arg);
1400 	}
1401 }
1402 
get_flock64(struct ia32_flock64 * fl32,struct flock * fl64)1403 static inline int get_flock64(struct ia32_flock64 *fl32, struct flock *fl64)
1404 {
1405 	if (access_ok(fl32, sizeof(struct ia32_flock64), VERIFY_WRITE)) {
1406 		int ret = __get_user(fl64->l_type, &fl32->l_type);
1407 		ret |= __get_user(fl64->l_whence, &fl32->l_whence);
1408 		ret |= __get_user(fl64->l_start, &fl32->l_start);
1409 		ret |= __get_user(fl64->l_len, &fl32->l_len);
1410 		ret |= __get_user(fl64->l_pid, &fl32->l_pid);
1411 		return ret;
1412 	}
1413 	return -EFAULT;
1414 }
1415 
put_flock64(struct ia32_flock64 * fl32,struct flock * fl64)1416 static inline int put_flock64(struct ia32_flock64 *fl32, struct flock *fl64)
1417 {
1418 	if (access_ok(fl32, sizeof(struct ia32_flock64), VERIFY_WRITE)) {
1419 		int ret = __put_user(fl64->l_type, &fl32->l_type);
1420 		ret |= __put_user(fl64->l_whence, &fl32->l_whence);
1421 		ret |= __put_user(fl64->l_start, &fl32->l_start);
1422 		ret |= __put_user(fl64->l_len, &fl32->l_len);
1423 		ret |= __put_user(fl64->l_pid, &fl32->l_pid);
1424 		return ret;
1425 	}
1426 	return -EFAULT;
1427 }
1428 
sys32_fcntl64(unsigned int fd,unsigned int cmd,unsigned long arg)1429 asmlinkage long sys32_fcntl64(unsigned int fd, unsigned int cmd, unsigned long arg)
1430 {
1431 	struct flock fl64;
1432 	mm_segment_t oldfs = get_fs();
1433 	int ret = 0;
1434 	int oldcmd = cmd;
1435 	unsigned long oldarg = arg;
1436 
1437 	switch (cmd) {
1438 	case F_GETLK64:
1439 		cmd = F_GETLK;
1440 		goto cnv;
1441 	case F_SETLK64:
1442 		cmd = F_SETLK;
1443 		goto cnv;
1444 	case F_SETLKW64:
1445 		cmd = F_SETLKW;
1446 	cnv:
1447 		ret = get_flock64((struct ia32_flock64 *)arg, &fl64);
1448 		arg = (unsigned long)&fl64;
1449 		set_fs(KERNEL_DS);
1450 		break;
1451 	case F_GETLK:
1452 	case F_SETLK:
1453 	case F_SETLKW:
1454 		return sys32_fcntl(fd,cmd,arg);
1455 	}
1456 	if (!ret)
1457 		ret = sys_fcntl(fd, cmd, arg);
1458 	set_fs(oldfs);
1459 	if (oldcmd == F_GETLK64 && !ret)
1460 		ret = put_flock64((struct ia32_flock64 *)oldarg, &fl64);
1461 	return ret;
1462 }
1463 
sys32_ni_syscall(int call)1464 asmlinkage long sys32_ni_syscall(int call)
1465 {
1466 	/* Disable for now because the emulation should be pretty complete
1467 	   and we miss some syscalls from 2.6. */
1468 #if 0
1469 	printk(KERN_INFO "IA32 syscall %d from %s not implemented\n", call,
1470 	       current->comm);
1471 #endif
1472 	return -ENOSYS;
1473 }
1474 
1475 /* 32-bit timeval and related flotsam.  */
1476 
1477 extern asmlinkage long sys_utime(char * filename, struct utimbuf * times);
1478 
1479 struct utimbuf32 {
1480 	__kernel_time_t32 actime, modtime;
1481 };
1482 
1483 asmlinkage long
sys32_utime(char * filename,struct utimbuf32 * times)1484 sys32_utime(char * filename, struct utimbuf32 *times)
1485 {
1486 	struct utimbuf t;
1487 	mm_segment_t old_fs;
1488 	int ret;
1489 	char *filenam;
1490 
1491 	if (!times)
1492 		return sys_utime(filename, NULL);
1493 	if (verify_area(VERIFY_READ, times, sizeof(struct utimbuf32)) ||
1494 	    __get_user (t.actime, &times->actime) ||
1495 	    __get_user (t.modtime, &times->modtime))
1496 		return -EFAULT;
1497 	filenam = getname (filename);
1498 	ret = PTR_ERR(filenam);
1499 	if (!IS_ERR(filenam)) {
1500 		old_fs = get_fs();
1501 		set_fs (KERNEL_DS);
1502 		ret = sys_utime(filenam, &t);
1503 		set_fs (old_fs);
1504 		putname(filenam);
1505 	}
1506 	return ret;
1507 }
1508 
1509 extern asmlinkage long sys_sysfs(int option, unsigned long arg1,
1510 				unsigned long arg2);
1511 
1512 asmlinkage long
sys32_sysfs(int option,u32 arg1,u32 arg2)1513 sys32_sysfs(int option, u32 arg1, u32 arg2)
1514 {
1515 	return sys_sysfs(option, arg1, arg2);
1516 }
1517 
1518 extern asmlinkage long sys_mount(char * dev_name, char * dir_name, char * type,
1519 				unsigned long new_flags, void *data);
1520 
1521 static char *badfs[] = {
1522 	"smbfs", "ncpfs", NULL
1523 };
1524 
checktype(char * user_type)1525 static int checktype(char *user_type)
1526 {
1527 	int err = 0;
1528 	char **s,*kernel_type = getname(user_type);
1529 	if (!kernel_type || IS_ERR(kernel_type))
1530 		return -EFAULT;
1531 	for (s = badfs; *s; ++s)
1532 		if (!strcmp(kernel_type, *s)) {
1533 			printk(KERN_ERR "mount32: unsupported fs `%s' -- use 64bit mount\n", *s);
1534 			err = -EINVAL;
1535 			break;
1536 		}
1537 	putname(user_type);
1538 	return err;
1539 }
1540 
1541 asmlinkage long
sys32_mount(char * dev_name,char * dir_name,char * type,unsigned long new_flags,u32 data)1542 sys32_mount(char *dev_name, char *dir_name, char *type,
1543 	    unsigned long new_flags, u32 data)
1544 {
1545 	int err;
1546 	if(!capable(CAP_SYS_ADMIN))
1547 		return -EPERM;
1548 	err = checktype(type);
1549 	if (err)
1550 		return err;
1551 	return sys_mount(dev_name, dir_name, type, new_flags, (void *)AA(data));
1552 }
1553 
1554 struct sysinfo32 {
1555         s32 uptime;
1556         u32 loads[3];
1557         u32 totalram;
1558         u32 freeram;
1559         u32 sharedram;
1560         u32 bufferram;
1561         u32 totalswap;
1562         u32 freeswap;
1563         unsigned short procs;
1564 	unsigned short pad;
1565         u32 totalhigh;
1566         u32 freehigh;
1567         u32 mem_unit;
1568         char _f[20-2*sizeof(u32)-sizeof(int)];
1569 };
1570 
1571 extern asmlinkage long sys_sysinfo(struct sysinfo *info);
1572 
1573 asmlinkage long
sys32_sysinfo(struct sysinfo32 * info)1574 sys32_sysinfo(struct sysinfo32 *info)
1575 {
1576 	struct sysinfo s;
1577 	int ret;
1578 	mm_segment_t old_fs = get_fs ();
1579 	int bitcount = 0;
1580 
1581 	set_fs (KERNEL_DS);
1582 	ret = sys_sysinfo(&s);
1583 	set_fs (old_fs);
1584 
1585         /* Check to see if any memory value is too large for 32-bit and scale
1586 	 *  down if needed
1587 	 */
1588 	if ((s.totalram >> 32) || (s.totalswap >> 32)) {
1589 		while (s.mem_unit < PAGE_SIZE) {
1590 			s.mem_unit <<= 1;
1591 			bitcount++;
1592 		}
1593 		s.totalram >>= bitcount;
1594 		s.freeram >>= bitcount;
1595 		s.sharedram >>= bitcount;
1596 		s.bufferram >>= bitcount;
1597 		s.totalswap >>= bitcount;
1598 		s.freeswap >>= bitcount;
1599 		s.totalhigh >>= bitcount;
1600 		s.freehigh >>= bitcount;
1601 	}
1602 
1603 	if (verify_area(VERIFY_WRITE, info, sizeof(struct sysinfo32)) ||
1604 	    __put_user (s.uptime, &info->uptime) ||
1605 	    __put_user (s.loads[0], &info->loads[0]) ||
1606 	    __put_user (s.loads[1], &info->loads[1]) ||
1607 	    __put_user (s.loads[2], &info->loads[2]) ||
1608 	    __put_user (s.totalram, &info->totalram) ||
1609 	    __put_user (s.freeram, &info->freeram) ||
1610 	    __put_user (s.sharedram, &info->sharedram) ||
1611 	    __put_user (s.bufferram, &info->bufferram) ||
1612 	    __put_user (s.totalswap, &info->totalswap) ||
1613 	    __put_user (s.freeswap, &info->freeswap) ||
1614 	    __put_user (s.procs, &info->procs) ||
1615 	    __put_user (s.totalhigh, &info->totalhigh) ||
1616 	    __put_user (s.freehigh, &info->freehigh) ||
1617 	    __put_user (s.mem_unit, &info->mem_unit))
1618 		return -EFAULT;
1619 	return 0;
1620 }
1621 
1622 extern asmlinkage long sys_sched_rr_get_interval(pid_t pid,
1623 						struct timespec *interval);
1624 
1625 asmlinkage long
sys32_sched_rr_get_interval(__kernel_pid_t32 pid,struct timespec32 * interval)1626 sys32_sched_rr_get_interval(__kernel_pid_t32 pid, struct timespec32 *interval)
1627 {
1628 	struct timespec t;
1629 	int ret;
1630 	mm_segment_t old_fs = get_fs ();
1631 
1632 	set_fs (KERNEL_DS);
1633 	ret = sys_sched_rr_get_interval(pid, &t);
1634 	set_fs (old_fs);
1635 	if (verify_area(VERIFY_WRITE, interval, sizeof(struct timespec32)) ||
1636 	    __put_user (t.tv_sec, &interval->tv_sec) ||
1637 	    __put_user (t.tv_nsec, &interval->tv_nsec))
1638 		return -EFAULT;
1639 	return ret;
1640 }
1641 
1642 extern asmlinkage long sys_sigprocmask(int how, old_sigset_t *set,
1643 				      old_sigset_t *oset);
1644 
1645 asmlinkage long
sys32_sigprocmask(int how,old_sigset32_t * set,old_sigset32_t * oset)1646 sys32_sigprocmask(int how, old_sigset32_t *set, old_sigset32_t *oset)
1647 {
1648 	old_sigset_t s;
1649 	int ret;
1650 	mm_segment_t old_fs = get_fs();
1651 
1652 	if (set && get_user (s, set)) return -EFAULT;
1653 	set_fs (KERNEL_DS);
1654 	ret = sys_sigprocmask(how, set ? &s : NULL, oset ? &s : NULL);
1655 	set_fs (old_fs);
1656 	if (ret) return ret;
1657 	if (oset && put_user (s, oset)) return -EFAULT;
1658 	return 0;
1659 }
1660 
1661 extern asmlinkage long sys_sigpending(old_sigset_t *set);
1662 
1663 asmlinkage long
sys32_sigpending(old_sigset32_t * set)1664 sys32_sigpending(old_sigset32_t *set)
1665 {
1666 	old_sigset_t s;
1667 	int ret;
1668 	mm_segment_t old_fs = get_fs();
1669 
1670 	set_fs (KERNEL_DS);
1671 	ret = sys_sigpending(&s);
1672 	set_fs (old_fs);
1673 	if (put_user (s, set)) return -EFAULT;
1674 	return ret;
1675 }
1676 
1677 extern asmlinkage long sys_rt_sigpending(sigset_t *set, size_t sigsetsize);
1678 
1679 asmlinkage long
sys32_rt_sigpending(sigset32_t * set,__kernel_size_t32 sigsetsize)1680 sys32_rt_sigpending(sigset32_t *set, __kernel_size_t32 sigsetsize)
1681 {
1682 	sigset_t s;
1683 	sigset32_t s32;
1684 	int ret;
1685 	mm_segment_t old_fs = get_fs();
1686 
1687 	set_fs (KERNEL_DS);
1688 	ret = sys_rt_sigpending(&s, sigsetsize);
1689 	set_fs (old_fs);
1690 	if (!ret) {
1691 		switch (_NSIG_WORDS) {
1692 		case 4: s32.sig[7] = (s.sig[3] >> 32); s32.sig[6] = s.sig[3];
1693 		case 3: s32.sig[5] = (s.sig[2] >> 32); s32.sig[4] = s.sig[2];
1694 		case 2: s32.sig[3] = (s.sig[1] >> 32); s32.sig[2] = s.sig[1];
1695 		case 1: s32.sig[1] = (s.sig[0] >> 32); s32.sig[0] = s.sig[0];
1696 		}
1697 		if (copy_to_user (set, &s32, sizeof(sigset32_t)))
1698 			return -EFAULT;
1699 	}
1700 	return ret;
1701 }
1702 
1703 siginfo_t32 *
siginfo64to32(siginfo_t32 * d,siginfo_t * s)1704 siginfo64to32(siginfo_t32 *d, siginfo_t *s)
1705 {
1706 	memset (d, 0, sizeof(siginfo_t32));
1707 	d->si_signo = s->si_signo;
1708 	d->si_errno = s->si_errno;
1709 	d->si_code = s->si_code;
1710 	if (s->si_signo >= SIGRTMIN) {
1711 		d->si_pid = s->si_pid;
1712 		d->si_uid = s->si_uid;
1713 		memcpy(&d->si_int, &s->si_int,
1714 		       sizeof(siginfo_t) - offsetof(siginfo_t,si_int));
1715 	} else switch (s->si_signo) {
1716 	/* XXX: What about POSIX1.b timers */
1717 	case SIGCHLD:
1718 		d->si_pid = s->si_pid;
1719 		d->si_status = s->si_status;
1720 		d->si_utime = s->si_utime;
1721 		d->si_stime = s->si_stime;
1722 		break;
1723 	case SIGSEGV:
1724 	case SIGBUS:
1725 	case SIGFPE:
1726 	case SIGILL:
1727 		d->si_addr = (long)(s->si_addr);
1728 //		d->si_trapno = s->si_trapno;
1729 		break;
1730 	case SIGPOLL:
1731 		d->si_band = s->si_band;
1732 		d->si_fd = s->si_fd;
1733 		break;
1734 	default:
1735 		d->si_pid = s->si_pid;
1736 		d->si_uid = s->si_uid;
1737 		break;
1738 	}
1739 	return d;
1740 }
1741 
1742 siginfo_t *
siginfo32to64(siginfo_t * d,siginfo_t32 * s)1743 siginfo32to64(siginfo_t *d, siginfo_t32 *s)
1744 {
1745 	d->si_signo = s->si_signo;
1746 	d->si_errno = s->si_errno;
1747 	d->si_code = s->si_code;
1748 	if (s->si_signo >= SIGRTMIN) {
1749 		d->si_pid = s->si_pid;
1750 		d->si_uid = s->si_uid;
1751 		memcpy(&d->si_int,
1752 		       &s->si_int,
1753 		       sizeof(siginfo_t) - offsetof(siginfo_t, si_int));
1754 	} else switch (s->si_signo) {
1755 	/* XXX: What about POSIX1.b timers */
1756 	case SIGCHLD:
1757 		d->si_pid = s->si_pid;
1758 		d->si_status = s->si_status;
1759 		d->si_utime = s->si_utime;
1760 		d->si_stime = s->si_stime;
1761 		break;
1762 	case SIGSEGV:
1763 	case SIGBUS:
1764 	case SIGFPE:
1765 	case SIGILL:
1766 		d->si_addr = (void *)A(s->si_addr);
1767 //		d->si_trapno = s->si_trapno;
1768 		break;
1769 	case SIGPOLL:
1770 		d->si_band = s->si_band;
1771 		d->si_fd = s->si_fd;
1772 		break;
1773 	default:
1774 		d->si_pid = s->si_pid;
1775 		d->si_uid = s->si_uid;
1776 		break;
1777 	}
1778 	return d;
1779 }
1780 
1781 extern asmlinkage long
1782 sys_rt_sigtimedwait(const sigset_t *uthese, siginfo_t *uinfo,
1783 		    const struct timespec *uts, size_t sigsetsize);
1784 
1785 asmlinkage long
sys32_rt_sigtimedwait(sigset32_t * uthese,siginfo_t32 * uinfo,struct timespec32 * uts,__kernel_size_t32 sigsetsize)1786 sys32_rt_sigtimedwait(sigset32_t *uthese, siginfo_t32 *uinfo,
1787 		      struct timespec32 *uts, __kernel_size_t32 sigsetsize)
1788 {
1789 	sigset_t s;
1790 	sigset32_t s32;
1791 	struct timespec t;
1792 	int ret;
1793 	mm_segment_t old_fs = get_fs();
1794 	siginfo_t info;
1795 	siginfo_t32 info32;
1796 
1797 	if (copy_from_user (&s32, uthese, sizeof(sigset32_t)))
1798 		return -EFAULT;
1799 	switch (_NSIG_WORDS) {
1800 	case 4: s.sig[3] = s32.sig[6] | (((long)s32.sig[7]) << 32);
1801 	case 3: s.sig[2] = s32.sig[4] | (((long)s32.sig[5]) << 32);
1802 	case 2: s.sig[1] = s32.sig[2] | (((long)s32.sig[3]) << 32);
1803 	case 1: s.sig[0] = s32.sig[0] | (((long)s32.sig[1]) << 32);
1804 	}
1805 	if (uts) {
1806 		if (verify_area(VERIFY_READ, uts, sizeof(struct timespec32)) ||
1807 		    __get_user (t.tv_sec, &uts->tv_sec) ||
1808 		    __get_user (t.tv_nsec, &uts->tv_nsec))
1809 			return -EFAULT;
1810 	}
1811 	set_fs (KERNEL_DS);
1812 	ret = sys_rt_sigtimedwait(&s, &info, uts ? &t : NULL, sigsetsize);
1813 	set_fs (old_fs);
1814 	if (ret >= 0 && uinfo) {
1815 		if (copy_to_user (uinfo, siginfo64to32(&info32, &info),
1816 				  sizeof(siginfo_t32)))
1817 			return -EFAULT;
1818 	}
1819 	return ret;
1820 }
1821 
1822 extern asmlinkage long
1823 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo);
1824 
1825 asmlinkage long
sys32_rt_sigqueueinfo(int pid,int sig,siginfo_t32 * uinfo)1826 sys32_rt_sigqueueinfo(int pid, int sig, siginfo_t32 *uinfo)
1827 {
1828 	siginfo_t info;
1829 	siginfo_t32 info32;
1830 	int ret;
1831 	mm_segment_t old_fs = get_fs();
1832 
1833 	if (copy_from_user (&info32, uinfo, sizeof(siginfo_t32)))
1834 		return -EFAULT;
1835 	/* XXX: Is this correct? */
1836 	siginfo32to64(&info, &info32);
1837 	set_fs (KERNEL_DS);
1838 	ret = sys_rt_sigqueueinfo(pid, sig, &info);
1839 	set_fs (old_fs);
1840 	return ret;
1841 }
1842 
1843 extern void check_pending(int signum);
1844 
1845 asmlinkage long sys_utimes(char *, struct timeval *);
1846 
1847 asmlinkage long
sys32_utimes(char * filename,struct timeval32 * tvs)1848 sys32_utimes(char *filename, struct timeval32 *tvs)
1849 {
1850 	char *kfilename;
1851 	struct timeval ktvs[2];
1852 	mm_segment_t old_fs;
1853 	int ret;
1854 
1855 	kfilename = getname(filename);
1856 	ret = PTR_ERR(kfilename);
1857 	if (!IS_ERR(kfilename)) {
1858 		if (tvs) {
1859 			if (get_tv32(&ktvs[0], tvs) ||
1860 			    get_tv32(&ktvs[1], 1+tvs))
1861 				return -EFAULT;
1862 		}
1863 
1864 		old_fs = get_fs();
1865 		set_fs(KERNEL_DS);
1866 		ret = sys_utimes(kfilename, &ktvs[0]);
1867 		set_fs(old_fs);
1868 
1869 		putname(kfilename);
1870 	}
1871 	return ret;
1872 }
1873 
1874 /* These are here just in case some old ia32 binary calls it. */
1875 asmlinkage long
sys32_pause(void)1876 sys32_pause(void)
1877 {
1878 	current->state = TASK_INTERRUPTIBLE;
1879 	schedule();
1880 	return -ERESTARTNOHAND;
1881 }
1882 
1883 
1884 struct sysctl_ia32 {
1885 	unsigned int	name;
1886 	int		nlen;
1887 	unsigned int	oldval;
1888 	unsigned int	oldlenp;
1889 	unsigned int	newval;
1890 	unsigned int	newlen;
1891 	unsigned int	__unused[4];
1892 };
1893 
1894 
1895 asmlinkage long
sys32_sysctl(struct sysctl_ia32 * args32)1896 sys32_sysctl(struct sysctl_ia32 *args32)
1897 {
1898 #ifndef CONFIG_SYSCTL
1899 	return -ENOSYS;
1900 #else
1901 	struct sysctl_ia32 a32;
1902 	mm_segment_t old_fs = get_fs ();
1903 	void *oldvalp, *newvalp;
1904 	size_t oldlen;
1905 	int *namep;
1906 	long ret;
1907 	extern int do_sysctl(int *name, int nlen, void *oldval, size_t *oldlenp,
1908 		     void *newval, size_t newlen);
1909 
1910 
1911 	if (copy_from_user(&a32, args32, sizeof (a32)))
1912 		return -EFAULT;
1913 
1914 	/*
1915 	 * We need to pre-validate these because we have to disable address checking
1916 	 * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
1917 	 * user specifying bad addresses here.  Well, since we're dealing with 32 bit
1918 	 * addresses, we KNOW that access_ok() will always succeed, so this is an
1919 	 * expensive NOP, but so what...
1920 	 */
1921 	namep = (int *) A(a32.name);
1922 	oldvalp = (void *) A(a32.oldval);
1923 	newvalp = (void *) A(a32.newval);
1924 
1925 	if ((oldvalp && get_user(oldlen, (int *) A(a32.oldlenp)))
1926 	    || !access_ok(VERIFY_WRITE, namep, 0)
1927 	    || !access_ok(VERIFY_WRITE, oldvalp, 0)
1928 	    || !access_ok(VERIFY_WRITE, newvalp, 0))
1929 		return -EFAULT;
1930 
1931 	set_fs(KERNEL_DS);
1932 	lock_kernel();
1933 	ret = do_sysctl(namep, a32.nlen, oldvalp, &oldlen, newvalp, (size_t) a32.newlen);
1934 	unlock_kernel();
1935 	set_fs(old_fs);
1936 
1937 	if (oldvalp && put_user (oldlen, (int *) A(a32.oldlenp)))
1938 		return -EFAULT;
1939 
1940 	return ret;
1941 #endif
1942 }
1943 
1944 extern asmlinkage ssize_t sys_pread(unsigned int fd, char * buf,
1945 				    size_t count, loff_t pos);
1946 
1947 extern asmlinkage ssize_t sys_pwrite(unsigned int fd, const char * buf,
1948 				     size_t count, loff_t pos);
1949 
1950 typedef __kernel_ssize_t32 ssize_t32;
1951 
1952 
1953 /* warning: next two assume little endian */
1954 asmlinkage long
sys32_pread(unsigned int fd,char * ubuf,__kernel_size_t32 count,u32 poslo,u32 poshi)1955 sys32_pread(unsigned int fd, char *ubuf, __kernel_size_t32 count,
1956 	    u32 poslo, u32 poshi)
1957 {
1958 	return sys_pread(fd, ubuf, count,
1959 			 ((loff_t)AA(poshi) << 32) | AA(poslo));
1960 }
1961 
1962 asmlinkage long
sys32_pwrite(unsigned int fd,char * ubuf,__kernel_size_t32 count,u32 poslo,u32 poshi)1963 sys32_pwrite(unsigned int fd, char *ubuf, __kernel_size_t32 count,
1964 	     u32 poslo, u32 poshi)
1965 {
1966 	return sys_pwrite(fd, ubuf, count,
1967 			  ((loff_t)AA(poshi) << 32) | AA(poslo));
1968 }
1969 
1970 
1971 extern asmlinkage long sys_personality(unsigned long);
1972 
1973 asmlinkage long
sys32_personality(unsigned long personality)1974 sys32_personality(unsigned long personality)
1975 {
1976 	int ret;
1977 	if (personality(current->personality) == PER_LINUX32 &&
1978 		personality == PER_LINUX)
1979 		personality = PER_LINUX32;
1980 	ret = sys_personality(personality);
1981 	if (ret == PER_LINUX32)
1982 		ret = PER_LINUX;
1983 	return ret;
1984 }
1985 
1986 extern asmlinkage ssize_t sys_sendfile(int out_fd, int in_fd, off_t *offset,
1987 				       size_t count);
1988 
1989 asmlinkage long
sys32_sendfile(int out_fd,int in_fd,__kernel_off_t32 * offset,s32 count)1990 sys32_sendfile(int out_fd, int in_fd, __kernel_off_t32 *offset, s32 count)
1991 {
1992 	mm_segment_t old_fs = get_fs();
1993 	int ret;
1994 	off_t of;
1995 
1996 	if (offset && get_user(of, offset))
1997 		return -EFAULT;
1998 
1999 	set_fs(KERNEL_DS);
2000 	ret = sys_sendfile(out_fd, in_fd, offset ? &of : NULL, count);
2001 	set_fs(old_fs);
2002 
2003 	if (!ret && offset && put_user(of, offset))
2004 		return -EFAULT;
2005 
2006 	return ret;
2007 }
2008 
2009 extern long sys_modify_ldt(int,void*,unsigned long);
2010 
sys32_modify_ldt(int func,void * ptr,unsigned long bytecount)2011 asmlinkage long sys32_modify_ldt(int func, void *ptr, unsigned long bytecount)
2012 {
2013         long ret;
2014         if (func == 0x1 || func == 0x11) {
2015 				struct modify_ldt_ldt_s info;
2016                 mm_segment_t old_fs = get_fs();
2017                 if (bytecount != sizeof(struct modify_ldt_ldt_s))
2018                         return -EINVAL;
2019                 if (copy_from_user(&info, ptr, sizeof(struct modify_ldt_ldt_s)))
2020                         return -EFAULT;
2021                 /* lm bit was undefined in the 32bit ABI and programs
2022                    give it random values. Force it to zero here. */
2023                 info.lm = 0;
2024                 set_fs(KERNEL_DS);
2025                 ret = sys_modify_ldt(func, &info, bytecount);
2026                 set_fs(old_fs);
2027         }  else {
2028                 ret = sys_modify_ldt(func, ptr, bytecount);
2029         }
2030         return ret;
2031 }
2032 
2033 /* Handle adjtimex compatability. */
2034 
2035 struct timex32 {
2036 	u32 modes;
2037 	s32 offset, freq, maxerror, esterror;
2038 	s32 status, constant, precision, tolerance;
2039 	struct timeval32 time;
2040 	s32 tick;
2041 	s32 ppsfreq, jitter, shift, stabil;
2042 	s32 jitcnt, calcnt, errcnt, stbcnt;
2043 	s32  :32; s32  :32; s32  :32; s32  :32;
2044 	s32  :32; s32  :32; s32  :32; s32  :32;
2045 	s32  :32; s32  :32; s32  :32; s32  :32;
2046 };
2047 
2048 extern int do_adjtimex(struct timex *);
2049 
2050 asmlinkage long
sys32_adjtimex(struct timex32 * utp)2051 sys32_adjtimex(struct timex32 *utp)
2052 {
2053 	struct timex txc;
2054 	int ret;
2055 
2056 	memset(&txc, 0, sizeof(struct timex));
2057 
2058 	if(verify_area(VERIFY_READ, utp, sizeof(struct timex32)) ||
2059 	   __get_user(txc.modes, &utp->modes) ||
2060 	   __get_user(txc.offset, &utp->offset) ||
2061 	   __get_user(txc.freq, &utp->freq) ||
2062 	   __get_user(txc.maxerror, &utp->maxerror) ||
2063 	   __get_user(txc.esterror, &utp->esterror) ||
2064 	   __get_user(txc.status, &utp->status) ||
2065 	   __get_user(txc.constant, &utp->constant) ||
2066 	   __get_user(txc.precision, &utp->precision) ||
2067 	   __get_user(txc.tolerance, &utp->tolerance) ||
2068 	   __get_user(txc.time.tv_sec, &utp->time.tv_sec) ||
2069 	   __get_user(txc.time.tv_usec, &utp->time.tv_usec) ||
2070 	   __get_user(txc.tick, &utp->tick) ||
2071 	   __get_user(txc.ppsfreq, &utp->ppsfreq) ||
2072 	   __get_user(txc.jitter, &utp->jitter) ||
2073 	   __get_user(txc.shift, &utp->shift) ||
2074 	   __get_user(txc.stabil, &utp->stabil) ||
2075 	   __get_user(txc.jitcnt, &utp->jitcnt) ||
2076 	   __get_user(txc.calcnt, &utp->calcnt) ||
2077 	   __get_user(txc.errcnt, &utp->errcnt) ||
2078 	   __get_user(txc.stbcnt, &utp->stbcnt))
2079 		return -EFAULT;
2080 
2081 	ret = do_adjtimex(&txc);
2082 
2083 	if(verify_area(VERIFY_WRITE, utp, sizeof(struct timex32)) ||
2084 	   __put_user(txc.modes, &utp->modes) ||
2085 	   __put_user(txc.offset, &utp->offset) ||
2086 	   __put_user(txc.freq, &utp->freq) ||
2087 	   __put_user(txc.maxerror, &utp->maxerror) ||
2088 	   __put_user(txc.esterror, &utp->esterror) ||
2089 	   __put_user(txc.status, &utp->status) ||
2090 	   __put_user(txc.constant, &utp->constant) ||
2091 	   __put_user(txc.precision, &utp->precision) ||
2092 	   __put_user(txc.tolerance, &utp->tolerance) ||
2093 	   __put_user(txc.time.tv_sec, &utp->time.tv_sec) ||
2094 	   __put_user(txc.time.tv_usec, &utp->time.tv_usec) ||
2095 	   __put_user(txc.tick, &utp->tick) ||
2096 	   __put_user(txc.ppsfreq, &utp->ppsfreq) ||
2097 	   __put_user(txc.jitter, &utp->jitter) ||
2098 	   __put_user(txc.shift, &utp->shift) ||
2099 	   __put_user(txc.stabil, &utp->stabil) ||
2100 	   __put_user(txc.jitcnt, &utp->jitcnt) ||
2101 	   __put_user(txc.calcnt, &utp->calcnt) ||
2102 	   __put_user(txc.errcnt, &utp->errcnt) ||
2103 	   __put_user(txc.stbcnt, &utp->stbcnt))
2104 		ret = -EFAULT;
2105 
2106 	return ret;
2107 }
2108 
sys32_mmap2(unsigned long addr,unsigned long len,unsigned long prot,unsigned long flags,unsigned long fd,unsigned long pgoff)2109 asmlinkage long sys32_mmap2(unsigned long addr, unsigned long len,
2110 	unsigned long prot, unsigned long flags,
2111 	unsigned long fd, unsigned long pgoff)
2112 {
2113 	struct mm_struct *mm = current->mm;
2114 	unsigned long error;
2115 	struct file * file = NULL;
2116 
2117 	flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
2118 	if (!(flags & MAP_ANONYMOUS)) {
2119 		file = fget(fd);
2120 		if (!file)
2121 			return -EBADF;
2122 	}
2123 
2124 	if (prot & PROT_READ)
2125 		prot |= PROT_EXEC;
2126 
2127 	down_write(&mm->mmap_sem);
2128 	error = do_mmap_pgoff(file, addr, len, prot, flags|MAP_32BIT, pgoff);
2129 	up_write(&mm->mmap_sem);
2130 
2131 	if (file)
2132 		fput(file);
2133 	return error;
2134 }
2135 
sys32_olduname(struct oldold_utsname * name)2136 asmlinkage long sys32_olduname(struct oldold_utsname * name)
2137 {
2138 	int error;
2139 
2140 	if (!name)
2141 		return -EFAULT;
2142 	if (!access_ok(VERIFY_WRITE,name,sizeof(struct oldold_utsname)))
2143 		return -EFAULT;
2144 
2145   	down_read(&uts_sem);
2146 
2147 	error = __copy_to_user(&name->sysname,&system_utsname.sysname,__OLD_UTS_LEN);
2148 	 __put_user(0,name->sysname+__OLD_UTS_LEN);
2149 	 __copy_to_user(&name->nodename,&system_utsname.nodename,__OLD_UTS_LEN);
2150 	 __put_user(0,name->nodename+__OLD_UTS_LEN);
2151 	 __copy_to_user(&name->release,&system_utsname.release,__OLD_UTS_LEN);
2152 	 __put_user(0,name->release+__OLD_UTS_LEN);
2153 	 __copy_to_user(&name->version,&system_utsname.version,__OLD_UTS_LEN);
2154 	 __put_user(0,name->version+__OLD_UTS_LEN);
2155 	 {
2156 		 char *arch = (personality(current->personality) == PER_LINUX32)
2157 			 ? "i686" : "x86_64";
2158 
2159 		 __copy_to_user(&name->machine,arch,strlen(arch)+1);
2160 	 }
2161 
2162 	 up_read(&uts_sem);
2163 
2164 	 error = error ? -EFAULT : 0;
2165 
2166 	 return error;
2167 }
2168 
sys32_uname(struct old_utsname * name)2169 asmlinkage long sys32_uname(struct old_utsname * name)
2170 {
2171 	int err;
2172 	down_read(&uts_sem);
2173 	err=copy_to_user(name, &system_utsname, sizeof (*name));
2174 	up_read(&uts_sem);
2175 	if (personality(current->personality) == PER_LINUX32)
2176 		err = copy_to_user(name->machine, "i686", 5);
2177 	return err?-EFAULT:0;
2178 }
2179 
2180 extern int sys_ustat(dev_t, struct ustat *);
2181 
sys32_ustat(dev_t dev,struct ustat32 * u32p)2182 asmlinkage long sys32_ustat(dev_t dev, struct ustat32 *u32p)
2183 {
2184 	struct ustat u;
2185 	mm_segment_t seg;
2186 	int ret;
2187 
2188 	seg = get_fs();
2189 	set_fs(KERNEL_DS);
2190 	ret = sys_ustat(dev,&u);
2191 	set_fs(seg);
2192 	if (ret >= 0) {
2193 		if (!access_ok(VERIFY_WRITE,u32p,sizeof(struct ustat32)) ||
2194 		    __put_user((__u32) u.f_tfree, &u32p->f_tfree) ||
2195 		    __put_user((__u32) u.f_tinode, &u32p->f_tfree) ||
2196 		    __copy_to_user(&u32p->f_fname, u.f_fname, sizeof(u.f_fname)) ||
2197 		    __copy_to_user(&u32p->f_fpack, u.f_fpack, sizeof(u.f_fpack)))
2198 			ret = -EFAULT;
2199 	}
2200 	return ret;
2201 }
2202 
nargs(u32 src,char ** dst,int max)2203 static int nargs(u32 src, char **dst, int max)
2204 {
2205 	int cnt;
2206 	u32 val;
2207 
2208 	cnt = 0;
2209 	do {
2210 		int ret = get_user(val, (__u32 *)(u64)src);
2211 		if (ret)
2212 			return ret;
2213 		if (cnt > max)
2214 			return -E2BIG;
2215 		if (dst)
2216 			dst[cnt] = (char *)(u64)val;
2217 		cnt++;
2218 		src += 4;
2219 		} while(val);
2220 	if (dst)
2221 		dst[cnt-1] = 0;
2222 	return cnt;
2223 }
2224 
sys32_execve(char * name,u32 argv,u32 envp,struct pt_regs regs)2225 asmlinkage long sys32_execve(char *name, u32 argv, u32 envp, struct pt_regs regs)
2226 {
2227 	mm_segment_t oldseg;
2228 	char **buf = NULL;
2229 	int na = 0,ne = 0;
2230 	int ret;
2231 	unsigned sz = 0;
2232 
2233 	/* Can actually allocate 2*MAX_ARG_PAGES */
2234 	if (argv) {
2235 	na = nargs(argv, NULL, (MAX_ARG_PAGES * PAGE_SIZE)/sizeof(char*) - 1);
2236 	if (na < 0)
2237 		return -EFAULT;
2238 	}
2239 	if (envp) {
2240 	ne = nargs(envp, NULL, (MAX_ARG_PAGES * PAGE_SIZE)/sizeof(char*) - 1);
2241 	if (ne < 0)
2242 		return -EFAULT;
2243 	}
2244 
2245 	if (argv || envp) {
2246 	sz = (na+ne)*sizeof(void *);
2247 	if (sz > PAGE_SIZE)
2248 		buf = vmalloc(sz);
2249 	else
2250 		buf = kmalloc(sz, GFP_KERNEL);
2251 	if (!buf)
2252 		return -ENOMEM;
2253 	}
2254 
2255 	if (argv) {
2256 	ret = nargs(argv, buf, na);
2257 	if (ret < 0)
2258 		goto free;
2259 	}
2260 
2261 	if (envp) {
2262 	ret = nargs(envp, buf + na, ne);
2263 	if (ret < 0)
2264 		goto free;
2265 	}
2266 
2267 	name = getname(name);
2268 	ret = PTR_ERR(name);
2269 	if (IS_ERR(name))
2270 		goto free;
2271 
2272 	oldseg = get_fs();
2273 	set_fs(KERNEL_DS);
2274 	ret = do_execve(name, argv ? buf : NULL, envp ? buf+na : NULL, &regs);
2275 	set_fs(oldseg);
2276 
2277 	if (ret == 0)
2278 		current->ptrace &= ~PT_DTRACE;
2279 
2280 	putname(name);
2281 
2282 free:
2283 	if (argv || envp) {
2284 	if (sz > PAGE_SIZE)
2285 		vfree(buf);
2286 	else
2287 		kfree(buf);
2288 	}
2289 	return ret;
2290 }
2291 
sys32_fork(struct pt_regs regs)2292 asmlinkage long sys32_fork(struct pt_regs regs)
2293 {
2294 	return do_fork(SIGCHLD, regs.rsp, &regs, 0);
2295 }
2296 
sys32_clone(unsigned int clone_flags,unsigned int newsp,struct pt_regs regs)2297 asmlinkage long sys32_clone(unsigned int clone_flags, unsigned int newsp, struct pt_regs regs)
2298 {
2299 	if (!newsp)
2300 		newsp = regs.rsp;
2301 	return do_fork(clone_flags, newsp, &regs, 0);
2302 }
2303 
2304 /*
2305  * This is trivial, and on the face of it looks like it
2306  * could equally well be done in user mode.
2307  *
2308  * Not so, for quite unobvious reasons - register pressure.
2309  * In user mode vfork() cannot have a stack frame, and if
2310  * done by calling the "clone()" system call directly, you
2311  * do not have enough call-clobbered registers to hold all
2312  * the information you need.
2313  */
sys32_vfork(struct pt_regs regs)2314 asmlinkage long sys32_vfork(struct pt_regs regs)
2315 {
2316 	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.rsp, &regs, 0);
2317 }
2318 
2319 /*
2320  * Some system calls that need sign extended arguments. This could be done by a generic wrapper.
2321  */
2322 
2323 extern off_t sys_lseek (unsigned int fd, off_t offset, unsigned int origin);
2324 
sys32_lseek(unsigned int fd,int offset,unsigned int whence)2325 asmlinkage long sys32_lseek (unsigned int fd, int offset, unsigned int whence)
2326 {
2327 	return sys_lseek(fd, offset, whence);
2328 }
2329 
2330 extern int sys_kill(pid_t pid, int sig);
2331 
sys32_kill(int pid,int sig)2332 asmlinkage long sys32_kill(int pid, int sig)
2333 {
2334 	return sys_kill(pid, sig);
2335 }
2336 
2337 
2338 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
2339 /* Stuff for NFS server syscalls... */
2340 struct nfsctl_svc32 {
2341 	u16			svc32_port;
2342 	s32			svc32_nthreads;
2343 };
2344 
2345 struct nfsctl_client32 {
2346 	s8			cl32_ident[NFSCLNT_IDMAX+1];
2347 	s32			cl32_naddr;
2348 	struct in_addr		cl32_addrlist[NFSCLNT_ADDRMAX];
2349 	s32			cl32_fhkeytype;
2350 	s32			cl32_fhkeylen;
2351 	u8			cl32_fhkey[NFSCLNT_KEYMAX];
2352 };
2353 
2354 struct nfsctl_export32 {
2355 	s8			ex32_client[NFSCLNT_IDMAX+1];
2356 	s8			ex32_path[NFS_MAXPATHLEN+1];
2357 	__kernel_dev_t32	ex32_dev;
2358 	__kernel_ino_t32	ex32_ino;
2359 	s32			ex32_flags;
2360 	__kernel_uid_t32	ex32_anon_uid;
2361 	__kernel_gid_t32	ex32_anon_gid;
2362 };
2363 
2364 struct nfsctl_uidmap32 {
2365 	u32			ug32_ident;   /* char * */
2366 	__kernel_uid_t32	ug32_uidbase;
2367 	s32			ug32_uidlen;
2368 	u32			ug32_udimap;  /* uid_t * */
2369 	__kernel_uid_t32	ug32_gidbase;
2370 	s32			ug32_gidlen;
2371 	u32			ug32_gdimap;  /* gid_t * */
2372 };
2373 
2374 struct nfsctl_fhparm32 {
2375 	struct sockaddr		gf32_addr;
2376 	__kernel_dev_t32	gf32_dev;
2377 	__kernel_ino_t32	gf32_ino;
2378 	s32			gf32_version;
2379 };
2380 
2381 struct nfsctl_fdparm32 {
2382 	struct sockaddr		gd32_addr;
2383 	s8			gd32_path[NFS_MAXPATHLEN+1];
2384 	s32			gd32_version;
2385 };
2386 
2387 struct nfsctl_fsparm32 {
2388 	struct sockaddr		gd32_addr;
2389 	s8			gd32_path[NFS_MAXPATHLEN+1];
2390 	s32			gd32_maxlen;
2391 };
2392 
2393 struct nfsctl_arg32 {
2394 	s32			ca32_version;	/* safeguard */
2395 	union {
2396 		struct nfsctl_svc32	u32_svc;
2397 		struct nfsctl_client32	u32_client;
2398 		struct nfsctl_export32	u32_export;
2399 		struct nfsctl_uidmap32	u32_umap;
2400 		struct nfsctl_fhparm32	u32_getfh;
2401 		struct nfsctl_fdparm32	u32_getfd;
2402 		struct nfsctl_fsparm32	u32_getfs;
2403 	} u;
2404 #define ca32_svc	u.u32_svc
2405 #define ca32_client	u.u32_client
2406 #define ca32_export	u.u32_export
2407 #define ca32_umap	u.u32_umap
2408 #define ca32_getfh	u.u32_getfh
2409 #define ca32_getfd	u.u32_getfd
2410 #define ca32_getfs	u.u32_getfs
2411 #define ca32_authd	u.u32_authd
2412 };
2413 
2414 union nfsctl_res32 {
2415 	__u8			cr32_getfh[NFS_FHSIZE];
2416 	struct knfsd_fh		cr32_getfs;
2417 };
2418 
nfs_svc32_trans(struct nfsctl_arg * karg,struct nfsctl_arg32 * arg32)2419 static int nfs_svc32_trans(struct nfsctl_arg *karg, struct nfsctl_arg32 *arg32)
2420 {
2421 	int err;
2422 
2423 	err = get_user(karg->ca_version, &arg32->ca32_version);
2424 	err |= __get_user(karg->ca_svc.svc_port, &arg32->ca32_svc.svc32_port);
2425 	err |= __get_user(karg->ca_svc.svc_nthreads, &arg32->ca32_svc.svc32_nthreads);
2426 	return err;
2427 }
2428 
nfs_clnt32_trans(struct nfsctl_arg * karg,struct nfsctl_arg32 * arg32)2429 static int nfs_clnt32_trans(struct nfsctl_arg *karg, struct nfsctl_arg32 *arg32)
2430 {
2431 	int err;
2432 
2433 	err = get_user(karg->ca_version, &arg32->ca32_version);
2434 	err |= copy_from_user(&karg->ca_client.cl_ident[0],
2435 			  &arg32->ca32_client.cl32_ident[0],
2436 			  NFSCLNT_IDMAX);
2437 	err |= __get_user(karg->ca_client.cl_naddr, &arg32->ca32_client.cl32_naddr);
2438 	err |= copy_from_user(&karg->ca_client.cl_addrlist[0],
2439 			  &arg32->ca32_client.cl32_addrlist[0],
2440 			  (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
2441 	err |= __get_user(karg->ca_client.cl_fhkeytype,
2442 		      &arg32->ca32_client.cl32_fhkeytype);
2443 	err |= __get_user(karg->ca_client.cl_fhkeylen,
2444 		      &arg32->ca32_client.cl32_fhkeylen);
2445 	err |= copy_from_user(&karg->ca_client.cl_fhkey[0],
2446 			  &arg32->ca32_client.cl32_fhkey[0],
2447 			  NFSCLNT_KEYMAX);
2448 	return err;
2449 }
2450 
nfs_exp32_trans(struct nfsctl_arg * karg,struct nfsctl_arg32 * arg32)2451 static int nfs_exp32_trans(struct nfsctl_arg *karg, struct nfsctl_arg32 *arg32)
2452 {
2453 	int err;
2454 
2455 	err = get_user(karg->ca_version, &arg32->ca32_version);
2456 	err |= copy_from_user(&karg->ca_export.ex_client[0],
2457 			  &arg32->ca32_export.ex32_client[0],
2458 			  NFSCLNT_IDMAX);
2459 	err |= copy_from_user(&karg->ca_export.ex_path[0],
2460 			  &arg32->ca32_export.ex32_path[0],
2461 			  NFS_MAXPATHLEN);
2462 	err |= __get_user(karg->ca_export.ex_dev,
2463 		      &arg32->ca32_export.ex32_dev);
2464 	err |= __get_user(karg->ca_export.ex_ino,
2465 		      &arg32->ca32_export.ex32_ino);
2466 	err |= __get_user(karg->ca_export.ex_flags,
2467 		      &arg32->ca32_export.ex32_flags);
2468 	err |= __get_user(karg->ca_export.ex_anon_uid,
2469 		      &arg32->ca32_export.ex32_anon_uid);
2470 	err |= __get_user(karg->ca_export.ex_anon_gid,
2471 		      &arg32->ca32_export.ex32_anon_gid);
2472 	karg->ca_export.ex_anon_uid = high2lowuid(karg->ca_export.ex_anon_uid);
2473 	karg->ca_export.ex_anon_gid = high2lowgid(karg->ca_export.ex_anon_gid);
2474 	return err;
2475 }
2476 
nfs_getfh32_trans(struct nfsctl_arg * karg,struct nfsctl_arg32 * arg32)2477 static int nfs_getfh32_trans(struct nfsctl_arg *karg, struct nfsctl_arg32 *arg32)
2478 {
2479 	int err;
2480 
2481 	err = get_user(karg->ca_version, &arg32->ca32_version);
2482 	err |= copy_from_user(&karg->ca_getfh.gf_addr,
2483 			  &arg32->ca32_getfh.gf32_addr,
2484 			  (sizeof(struct sockaddr)));
2485 	err |= __get_user(karg->ca_getfh.gf_dev,
2486 		      &arg32->ca32_getfh.gf32_dev);
2487 	err |= __get_user(karg->ca_getfh.gf_ino,
2488 		      &arg32->ca32_getfh.gf32_ino);
2489 	err |= __get_user(karg->ca_getfh.gf_version,
2490 		      &arg32->ca32_getfh.gf32_version);
2491 	return err;
2492 }
2493 
nfs_getfd32_trans(struct nfsctl_arg * karg,struct nfsctl_arg32 * arg32)2494 static int nfs_getfd32_trans(struct nfsctl_arg *karg, struct nfsctl_arg32 *arg32)
2495 {
2496 	int err;
2497 
2498 	err = get_user(karg->ca_version, &arg32->ca32_version);
2499 	err |= copy_from_user(&karg->ca_getfd.gd_addr,
2500 			  &arg32->ca32_getfd.gd32_addr,
2501 			  (sizeof(struct sockaddr)));
2502 	err |= copy_from_user(&karg->ca_getfd.gd_path,
2503 			  &arg32->ca32_getfd.gd32_path,
2504 			  (NFS_MAXPATHLEN+1));
2505 	err |= get_user(karg->ca_getfd.gd_version,
2506 		      &arg32->ca32_getfd.gd32_version);
2507 	return err;
2508 }
2509 
nfs_getfs32_trans(struct nfsctl_arg * karg,struct nfsctl_arg32 * arg32)2510 static int nfs_getfs32_trans(struct nfsctl_arg *karg, struct nfsctl_arg32 *arg32)
2511 {
2512 	int err;
2513 
2514 	err = get_user(karg->ca_version, &arg32->ca32_version);
2515 	err |= copy_from_user(&karg->ca_getfs.gd_addr,
2516 			  &arg32->ca32_getfs.gd32_addr,
2517 			  (sizeof(struct sockaddr)));
2518 	err |= copy_from_user(&karg->ca_getfs.gd_path,
2519 			  &arg32->ca32_getfs.gd32_path,
2520 			  (NFS_MAXPATHLEN+1));
2521 	err |= get_user(karg->ca_getfs.gd_maxlen,
2522 		      &arg32->ca32_getfs.gd32_maxlen);
2523 	return err;
2524 }
2525 
2526 /* This really doesn't need translations, we are only passing
2527  * back a union which contains opaque nfs file handle data.
2528  */
nfs_getfh32_res_trans(union nfsctl_res * kres,union nfsctl_res32 * res32)2529 static int nfs_getfh32_res_trans(union nfsctl_res *kres, union nfsctl_res32 *res32)
2530 {
2531 	return copy_to_user(res32, kres, sizeof(*res32));
2532 }
2533 
sys32_nfsservctl(int cmd,struct nfsctl_arg32 * arg32,union nfsctl_res32 * res32)2534 long asmlinkage sys32_nfsservctl(int cmd, struct nfsctl_arg32 *arg32, union nfsctl_res32 *res32)
2535 {
2536 	struct nfsctl_arg *karg = NULL;
2537 	union nfsctl_res *kres = NULL;
2538 	mm_segment_t oldfs;
2539 	int err;
2540 
2541 	karg = kmalloc(sizeof(*karg), GFP_USER);
2542 	if(!karg)
2543 		return -ENOMEM;
2544 	if(res32) {
2545 		kres = kmalloc(sizeof(*kres), GFP_USER);
2546 		if(!kres) {
2547 			kfree(karg);
2548 			return -ENOMEM;
2549 		}
2550 	}
2551 	switch(cmd) {
2552 	case NFSCTL_SVC:
2553 		err = nfs_svc32_trans(karg, arg32);
2554 		break;
2555 	case NFSCTL_ADDCLIENT:
2556 		err = nfs_clnt32_trans(karg, arg32);
2557 		break;
2558 	case NFSCTL_DELCLIENT:
2559 		err = nfs_clnt32_trans(karg, arg32);
2560 		break;
2561 	case NFSCTL_EXPORT:
2562 	case NFSCTL_UNEXPORT:
2563 		err = nfs_exp32_trans(karg, arg32);
2564 		break;
2565 	case NFSCTL_GETFH:
2566 		err = nfs_getfh32_trans(karg, arg32);
2567 		break;
2568 	case NFSCTL_GETFD:
2569 		err = nfs_getfd32_trans(karg, arg32);
2570 		break;
2571 	case NFSCTL_GETFS:
2572 		err = nfs_getfs32_trans(karg, arg32);
2573 		break;
2574 	default:
2575 		err = -EINVAL;
2576 		break;
2577 	}
2578 	if(err)
2579 		goto done;
2580 	oldfs = get_fs();
2581 	set_fs(KERNEL_DS);
2582 	err = sys_nfsservctl(cmd, karg, kres);
2583 	set_fs(oldfs);
2584 
2585 	if (err)
2586 		goto done;
2587 
2588 	if((cmd == NFSCTL_GETFH) ||
2589 	   (cmd == NFSCTL_GETFD) ||
2590 	   (cmd == NFSCTL_GETFS))
2591 		err = nfs_getfh32_res_trans(kres, res32);
2592 
2593 done:
2594 	if(karg) {
2595 		if(cmd == NFSCTL_UGIDUPDATE) {
2596 			if(karg->ca_umap.ug_ident)
2597 				kfree(karg->ca_umap.ug_ident);
2598 			if(karg->ca_umap.ug_udimap)
2599 				kfree(karg->ca_umap.ug_udimap);
2600 			if(karg->ca_umap.ug_gdimap)
2601 				kfree(karg->ca_umap.ug_gdimap);
2602 		}
2603 		kfree(karg);
2604 	}
2605 	if(kres)
2606 		kfree(kres);
2607 	return err;
2608 }
2609 #else /* !NFSD */
2610 extern asmlinkage long sys_ni_syscall(void);
sys32_nfsservctl(int cmd,void * notused,void * notused2)2611 long asmlinkage sys32_nfsservctl(int cmd, void *notused, void *notused2)
2612 {
2613 	return sys_ni_syscall();
2614 }
2615 #endif
2616 
2617 #ifdef CONFIG_MODULES
2618 
2619 extern asmlinkage unsigned long sys_create_module(const char *name_user, size_t size);
2620 
sys32_create_module(const char * name_user,__kernel_size_t32 size)2621 asmlinkage unsigned long sys32_create_module(const char *name_user, __kernel_size_t32 size)
2622 {
2623 	return sys_create_module(name_user, (size_t)size);
2624 }
2625 
2626 extern asmlinkage int sys_init_module(const char *name_user, struct module *mod_user);
2627 
2628 /* Hey, when you're trying to init module, take time and prepare us a nice 64bit
2629  * module structure, even if from 32bit modutils... Why to pollute kernel... :))
2630  */
sys32_init_module(const char * name_user,struct module * mod_user)2631 asmlinkage int sys32_init_module(const char *name_user, struct module *mod_user)
2632 {
2633 	return sys_init_module(name_user, mod_user);
2634 }
2635 
2636 extern asmlinkage int sys_delete_module(const char *name_user);
2637 
sys32_delete_module(const char * name_user)2638 asmlinkage int sys32_delete_module(const char *name_user)
2639 {
2640 	return sys_delete_module(name_user);
2641 }
2642 
2643 struct module_info32 {
2644 	u32 addr;
2645 	u32 size;
2646 	u32 flags;
2647 	s32 usecount;
2648 };
2649 
2650 /* Query various bits about modules.  */
2651 
2652 static inline long
get_mod_name(const char * user_name,char ** buf)2653 get_mod_name(const char *user_name, char **buf)
2654 {
2655 	unsigned long page;
2656 	long retval;
2657 
2658 	if ((unsigned long)user_name >= TASK_SIZE
2659 	    && !segment_eq(get_fs (), KERNEL_DS))
2660 		return -EFAULT;
2661 
2662 	page = __get_free_page(GFP_KERNEL);
2663 	if (!page)
2664 		return -ENOMEM;
2665 
2666 	retval = strncpy_from_user((char *)page, user_name, PAGE_SIZE);
2667 	if (retval > 0) {
2668 		if (retval < PAGE_SIZE) {
2669 			*buf = (char *)page;
2670 			return retval;
2671 	}
2672 		retval = -ENAMETOOLONG;
2673 	} else if (!retval)
2674 		retval = -EINVAL;
2675 
2676 	free_page(page);
2677 	return retval;
2678 }
2679 
2680 static inline void
put_mod_name(char * buf)2681 put_mod_name(char *buf)
2682 {
2683 	free_page((unsigned long)buf);
2684 }
2685 
find_module(const char * name)2686 static __inline__ struct module *find_module(const char *name)
2687 {
2688 	struct module *mod;
2689 
2690 	for (mod = module_list; mod ; mod = mod->next) {
2691 		if (mod->flags & MOD_DELETED)
2692 			continue;
2693 		if (!strcmp(mod->name, name))
2694 			break;
2695 	}
2696 
2697 	return mod;
2698 }
2699 
2700 static int
qm_modules(char * buf,size_t bufsize,__kernel_size_t32 * ret)2701 qm_modules(char *buf, size_t bufsize, __kernel_size_t32 *ret)
2702 {
2703 	struct module *mod;
2704 	size_t nmod, space, len;
2705 
2706 	nmod = space = 0;
2707 
2708 	for (mod = module_list; mod->next != NULL; mod = mod->next, ++nmod) {
2709 		len = strlen(mod->name)+1;
2710 		if (len > bufsize)
2711 			goto calc_space_needed;
2712 		if (copy_to_user(buf, mod->name, len))
2713 			return -EFAULT;
2714 		buf += len;
2715 		bufsize -= len;
2716 		space += len;
2717 	}
2718 
2719 	if (put_user(nmod, ret))
2720 		return -EFAULT;
2721 	else
2722 		return 0;
2723 
2724 calc_space_needed:
2725 	space += len;
2726 	while ((mod = mod->next)->next != NULL)
2727 		space += strlen(mod->name)+1;
2728 
2729 	if (put_user(space, ret))
2730 		return -EFAULT;
2731 	else
2732 		return -ENOSPC;
2733 }
2734 
2735 static int
qm_deps(struct module * mod,char * buf,size_t bufsize,__kernel_size_t32 * ret)2736 qm_deps(struct module *mod, char *buf, size_t bufsize, __kernel_size_t32 *ret)
2737 {
2738 	size_t i, space, len;
2739 
2740 	if (mod->next == NULL)
2741 		return -EINVAL;
2742 	if (!MOD_CAN_QUERY(mod))
2743 		return put_user(0, ret);
2744 
2745 	space = 0;
2746 	for (i = 0; i < mod->ndeps; ++i) {
2747 		const char *dep_name = mod->deps[i].dep->name;
2748 
2749 		len = strlen(dep_name)+1;
2750 		if (len > bufsize)
2751 			goto calc_space_needed;
2752 		if (copy_to_user(buf, dep_name, len))
2753 			return -EFAULT;
2754 		buf += len;
2755 		bufsize -= len;
2756 		space += len;
2757 	}
2758 
2759 	return put_user(i, ret);
2760 
2761 calc_space_needed:
2762 	space += len;
2763 	while (++i < mod->ndeps)
2764 		space += strlen(mod->deps[i].dep->name)+1;
2765 
2766 	if (put_user(space, ret))
2767 		return -EFAULT;
2768 	else
2769 		return -ENOSPC;
2770 }
2771 
2772 static int
qm_refs(struct module * mod,char * buf,size_t bufsize,__kernel_size_t32 * ret)2773 qm_refs(struct module *mod, char *buf, size_t bufsize, __kernel_size_t32 *ret)
2774 {
2775 	size_t nrefs, space, len;
2776 	struct module_ref *ref;
2777 
2778 	if (mod->next == NULL)
2779 		return -EINVAL;
2780 	if (!MOD_CAN_QUERY(mod))
2781 		if (put_user(0, ret))
2782 			return -EFAULT;
2783 		else
2784 			return 0;
2785 
2786 	space = 0;
2787 	for (nrefs = 0, ref = mod->refs; ref ; ++nrefs, ref = ref->next_ref) {
2788 		const char *ref_name = ref->ref->name;
2789 
2790 		len = strlen(ref_name)+1;
2791 		if (len > bufsize)
2792 			goto calc_space_needed;
2793 		if (copy_to_user(buf, ref_name, len))
2794 			return -EFAULT;
2795 		buf += len;
2796 		bufsize -= len;
2797 		space += len;
2798 	}
2799 
2800 	if (put_user(nrefs, ret))
2801 		return -EFAULT;
2802 	else
2803 		return 0;
2804 
2805 calc_space_needed:
2806 	space += len;
2807 	while ((ref = ref->next_ref) != NULL)
2808 		space += strlen(ref->ref->name)+1;
2809 
2810 	if (put_user(space, ret))
2811 		return -EFAULT;
2812 	else
2813 		return -ENOSPC;
2814 }
2815 
2816 static inline int
qm_symbols(struct module * mod,char * buf,size_t bufsize,__kernel_size_t32 * ret)2817 qm_symbols(struct module *mod, char *buf, size_t bufsize, __kernel_size_t32 *ret)
2818 {
2819 	size_t i, space, len;
2820 	struct module_symbol *s;
2821 	char *strings;
2822 	unsigned *vals;
2823 
2824 	if (!MOD_CAN_QUERY(mod))
2825 		if (put_user(0, ret))
2826 			return -EFAULT;
2827 		else
2828 			return 0;
2829 
2830 	space = mod->nsyms * 2*sizeof(u32);
2831 
2832 	i = len = 0;
2833 	s = mod->syms;
2834 
2835 	if (space > bufsize)
2836 		goto calc_space_needed;
2837 
2838 	if (!access_ok(VERIFY_WRITE, buf, space))
2839 		return -EFAULT;
2840 
2841 	bufsize -= space;
2842 	vals = (unsigned *)buf;
2843 	strings = buf+space;
2844 
2845 	for (; i < mod->nsyms ; ++i, ++s, vals += 2) {
2846 		len = strlen(s->name)+1;
2847 		if (len > bufsize)
2848 			goto calc_space_needed;
2849 
2850 		if (copy_to_user(strings, s->name, len)
2851 		    || __put_user(s->value, vals+0)
2852 		    || __put_user(space, vals+1))
2853 			return -EFAULT;
2854 
2855 		strings += len;
2856 		bufsize -= len;
2857 		space += len;
2858 	}
2859 
2860 	if (put_user(i, ret))
2861 		return -EFAULT;
2862 	else
2863 		return 0;
2864 
2865 calc_space_needed:
2866 	for (; i < mod->nsyms; ++i, ++s)
2867 		space += strlen(s->name)+1;
2868 
2869 	if (put_user(space, ret))
2870 		return -EFAULT;
2871 	else
2872 		return -ENOSPC;
2873 }
2874 
2875 static inline int
qm_info(struct module * mod,char * buf,size_t bufsize,__kernel_size_t32 * ret)2876 qm_info(struct module *mod, char *buf, size_t bufsize, __kernel_size_t32 *ret)
2877 {
2878 	int error = 0;
2879 
2880 	if (mod->next == NULL)
2881 		return -EINVAL;
2882 
2883 	if (sizeof(struct module_info32) <= bufsize) {
2884 		struct module_info32 info;
2885 		info.addr = (unsigned long)mod;
2886 		info.size = mod->size;
2887 		info.flags = mod->flags;
2888 		info.usecount =
2889 			((mod_member_present(mod, can_unload)
2890 			  && mod->can_unload)
2891 			 ? -1 : atomic_read(&mod->uc.usecount));
2892 
2893 		if (copy_to_user(buf, &info, sizeof(struct module_info32)))
2894 			return -EFAULT;
2895 	} else
2896 		error = -ENOSPC;
2897 
2898 	if (put_user(sizeof(struct module_info32), ret))
2899 		return -EFAULT;
2900 
2901 	return error;
2902 }
2903 
sys32_query_module(char * name_user,int which,char * buf,__kernel_size_t32 bufsize,u32 ret)2904 asmlinkage int sys32_query_module(char *name_user, int which, char *buf, __kernel_size_t32 bufsize, u32 ret)
2905 {
2906 	struct module *mod;
2907 	int err;
2908 
2909 	lock_kernel();
2910 	if (name_user == 0) {
2911 		/* This finds "kernel_module" which is not exported. */
2912 		for(mod = module_list; mod->next != NULL; mod = mod->next)
2913 			;
2914 	} else {
2915 		long namelen;
2916 		char *name;
2917 
2918 		if ((namelen = get_mod_name(name_user, &name)) < 0) {
2919 			err = namelen;
2920 			goto out;
2921 		}
2922 		err = -ENOENT;
2923 		if (namelen == 0) {
2924 			/* This finds "kernel_module" which is not exported. */
2925 			for(mod = module_list; mod->next != NULL; mod = mod->next)
2926 				;
2927 		} else if ((mod = find_module(name)) == NULL) {
2928 			put_mod_name(name);
2929 			goto out;
2930 		}
2931 		put_mod_name(name);
2932 	}
2933 
2934 	switch (which)
2935 	{
2936 	case 0:
2937 		err = 0;
2938 		break;
2939 	case QM_MODULES:
2940 		err = qm_modules(buf, bufsize, (__kernel_size_t32 *)AA(ret));
2941 		break;
2942 	case QM_DEPS:
2943 		err = qm_deps(mod, buf, bufsize, (__kernel_size_t32 *)AA(ret));
2944 		break;
2945 	case QM_REFS:
2946 		err = qm_refs(mod, buf, bufsize, (__kernel_size_t32 *)AA(ret));
2947 		break;
2948 	case QM_SYMBOLS:
2949 		err = qm_symbols(mod, buf, bufsize, (__kernel_size_t32 *)AA(ret));
2950 		break;
2951 	case QM_INFO:
2952 		err = qm_info(mod, buf, bufsize, (__kernel_size_t32 *)AA(ret));
2953 		break;
2954 	default:
2955 		err = -EINVAL;
2956 		break;
2957 	}
2958 out:
2959 	unlock_kernel();
2960 	return err;
2961 }
2962 
2963 struct kernel_sym32 {
2964 	u32 value;
2965 	char name[60];
2966 };
2967 
2968 extern asmlinkage int sys_get_kernel_syms(struct kernel_sym *table);
2969 
sys32_get_kernel_syms(struct kernel_sym32 * table)2970 asmlinkage int sys32_get_kernel_syms(struct kernel_sym32 *table)
2971 {
2972 	int len, i;
2973 	struct kernel_sym *tbl;
2974 	mm_segment_t old_fs;
2975 
2976 	len = sys_get_kernel_syms(NULL);
2977 	if (!table) return len;
2978 	tbl = kmalloc (len * sizeof (struct kernel_sym), GFP_KERNEL);
2979 	if (!tbl) return -ENOMEM;
2980 	old_fs = get_fs();
2981 	set_fs (KERNEL_DS);
2982 	sys_get_kernel_syms(tbl);
2983 	set_fs (old_fs);
2984 	for (i = 0; i < len; i++, table++) {
2985 		if (put_user (tbl[i].value, &table->value) ||
2986 		    copy_to_user (table->name, tbl[i].name, 60))
2987 			break;
2988 	}
2989 	kfree (tbl);
2990 	return i;
2991 }
2992 
2993 #else /* CONFIG_MODULES */
2994 
2995 asmlinkage unsigned long
sys32_create_module(const char * name_user,size_t size)2996 sys32_create_module(const char *name_user, size_t size)
2997 {
2998 	return -ENOSYS;
2999 }
3000 
3001 asmlinkage int
sys32_init_module(const char * name_user,struct module * mod_user)3002 sys32_init_module(const char *name_user, struct module *mod_user)
3003 {
3004 	return -ENOSYS;
3005 }
3006 
3007 asmlinkage int
sys32_delete_module(const char * name_user)3008 sys32_delete_module(const char *name_user)
3009 {
3010 	return -ENOSYS;
3011 }
3012 
3013 asmlinkage int
sys32_query_module(const char * name_user,int which,char * buf,size_t bufsize,size_t * ret)3014 sys32_query_module(const char *name_user, int which, char *buf, size_t bufsize,
3015 		 size_t *ret)
3016 {
3017 	/* Let the program know about the new interface.  Not that
3018 	   it'll do them much good.  */
3019 	if (which == 0)
3020 		return 0;
3021 
3022 	return -ENOSYS;
3023 }
3024 
3025 asmlinkage int
sys32_get_kernel_syms(struct kernel_sym * table)3026 sys32_get_kernel_syms(struct kernel_sym *table)
3027 {
3028 	return -ENOSYS;
3029 }
3030 
3031 #endif  /* CONFIG_MODULES */
3032 
sys32_vm86_warning(void)3033 long sys32_vm86_warning(void)
3034 {
3035 	static long warn_time = -(60*HZ);
3036 	if (time_before(warn_time + 60*HZ,jiffies)) {
3037 		printk(KERN_INFO "%s: vm86 mode not supported on 64 bit kernel\n",
3038 		       current->comm);
3039 		warn_time = jiffies;
3040 	}
3041 	return -ENOSYS ;
3042 }
3043 
3044 /* This only triggers an i686 uname */
3045 struct exec_domain ia32_exec_domain = {
3046 	name: "linux/uname-i686",
3047 	pers_low: PER_LINUX32,
3048 	pers_high: PER_LINUX32,
3049 };
3050 
ia32_init(void)3051 static int __init ia32_init (void)
3052 {
3053 	printk("IA32 emulation $Id: sys_ia32.c,v 1.70 2004/03/03 23:36:43 ak Exp $\n");
3054 	ia32_exec_domain.signal_map = default_exec_domain.signal_map;
3055 	ia32_exec_domain.signal_invmap = default_exec_domain.signal_invmap;
3056 	register_exec_domain(&ia32_exec_domain);
3057 	return 0;
3058 }
3059 
3060 __initcall(ia32_init);
3061