Initial commit
[wrt350n-kernel.git] / arch / ia64 / ia32 / sys_ia32.c
blobd025a22eb225f6bf455ec5a76b47434dc09a0977
1 /*
2 * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Derived from sys_sparc32.c.
4 * Copyright (C) 2000 VA Linux Co
5 * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
6 * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
7 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
9 * Copyright (C) 2000-2003, 2005 Hewlett-Packard Co
10 * David Mosberger-Tang <davidm@hpl.hp.com>
11 * Copyright (C) 2004 Gordon Jin <gordon.jin@intel.com>
13 * These routines maintain argument size conversion between 32bit and 64bit
14 * environment.
17 #include <linux/kernel.h>
18 #include <linux/syscalls.h>
19 #include <linux/sysctl.h>
20 #include <linux/sched.h>
21 #include <linux/fs.h>
22 #include <linux/file.h>
23 #include <linux/signal.h>
24 #include <linux/resource.h>
25 #include <linux/times.h>
26 #include <linux/utsname.h>
27 #include <linux/smp.h>
28 #include <linux/smp_lock.h>
29 #include <linux/sem.h>
30 #include <linux/msg.h>
31 #include <linux/mm.h>
32 #include <linux/shm.h>
33 #include <linux/slab.h>
34 #include <linux/uio.h>
35 #include <linux/nfs_fs.h>
36 #include <linux/quota.h>
37 #include <linux/sunrpc/svc.h>
38 #include <linux/nfsd/nfsd.h>
39 #include <linux/nfsd/cache.h>
40 #include <linux/nfsd/xdr.h>
41 #include <linux/nfsd/syscall.h>
42 #include <linux/poll.h>
43 #include <linux/eventpoll.h>
44 #include <linux/personality.h>
45 #include <linux/ptrace.h>
46 #include <linux/stat.h>
47 #include <linux/ipc.h>
48 #include <linux/capability.h>
49 #include <linux/compat.h>
50 #include <linux/vfs.h>
51 #include <linux/mman.h>
52 #include <linux/mutex.h>
54 #include <asm/intrinsics.h>
55 #include <asm/types.h>
56 #include <asm/uaccess.h>
57 #include <asm/unistd.h>
59 #include "ia32priv.h"
61 #include <net/scm.h>
62 #include <net/sock.h>
64 #define DEBUG 0
66 #if DEBUG
67 # define DBG(fmt...) printk(KERN_DEBUG fmt)
68 #else
69 # define DBG(fmt...)
70 #endif
72 #define ROUND_UP(x,a) ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
74 #define OFFSET4K(a) ((a) & 0xfff)
75 #define PAGE_START(addr) ((addr) & PAGE_MASK)
76 #define MINSIGSTKSZ_IA32 2048
78 #define high2lowuid(uid) ((uid) > 65535 ? 65534 : (uid))
79 #define high2lowgid(gid) ((gid) > 65535 ? 65534 : (gid))
82 * Anything that modifies or inspects ia32 user virtual memory must hold this semaphore
83 * while doing so.
85 /* XXX make per-mm: */
86 static DEFINE_MUTEX(ia32_mmap_mutex);
88 asmlinkage long
89 sys32_execve (char __user *name, compat_uptr_t __user *argv, compat_uptr_t __user *envp,
90 struct pt_regs *regs)
92 long error;
93 char *filename;
94 unsigned long old_map_base, old_task_size, tssd;
96 filename = getname(name);
97 error = PTR_ERR(filename);
98 if (IS_ERR(filename))
99 return error;
101 old_map_base = current->thread.map_base;
102 old_task_size = current->thread.task_size;
103 tssd = ia64_get_kr(IA64_KR_TSSD);
105 /* we may be exec'ing a 64-bit process: reset map base, task-size, and io-base: */
106 current->thread.map_base = DEFAULT_MAP_BASE;
107 current->thread.task_size = DEFAULT_TASK_SIZE;
108 ia64_set_kr(IA64_KR_IO_BASE, current->thread.old_iob);
109 ia64_set_kr(IA64_KR_TSSD, current->thread.old_k1);
111 error = compat_do_execve(filename, argv, envp, regs);
112 putname(filename);
114 if (error < 0) {
115 /* oops, execve failed, switch back to old values... */
116 ia64_set_kr(IA64_KR_IO_BASE, IA32_IOBASE);
117 ia64_set_kr(IA64_KR_TSSD, tssd);
118 current->thread.map_base = old_map_base;
119 current->thread.task_size = old_task_size;
122 return error;
125 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
127 compat_ino_t ino;
128 int err;
130 if ((u64) stat->size > MAX_NON_LFS ||
131 !old_valid_dev(stat->dev) ||
132 !old_valid_dev(stat->rdev))
133 return -EOVERFLOW;
135 ino = stat->ino;
136 if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
137 return -EOVERFLOW;
139 if (clear_user(ubuf, sizeof(*ubuf)))
140 return -EFAULT;
142 err = __put_user(old_encode_dev(stat->dev), &ubuf->st_dev);
143 err |= __put_user(ino, &ubuf->st_ino);
144 err |= __put_user(stat->mode, &ubuf->st_mode);
145 err |= __put_user(stat->nlink, &ubuf->st_nlink);
146 err |= __put_user(high2lowuid(stat->uid), &ubuf->st_uid);
147 err |= __put_user(high2lowgid(stat->gid), &ubuf->st_gid);
148 err |= __put_user(old_encode_dev(stat->rdev), &ubuf->st_rdev);
149 err |= __put_user(stat->size, &ubuf->st_size);
150 err |= __put_user(stat->atime.tv_sec, &ubuf->st_atime);
151 err |= __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec);
152 err |= __put_user(stat->mtime.tv_sec, &ubuf->st_mtime);
153 err |= __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec);
154 err |= __put_user(stat->ctime.tv_sec, &ubuf->st_ctime);
155 err |= __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec);
156 err |= __put_user(stat->blksize, &ubuf->st_blksize);
157 err |= __put_user(stat->blocks, &ubuf->st_blocks);
158 return err;
161 #if PAGE_SHIFT > IA32_PAGE_SHIFT
164 static int
165 get_page_prot (struct vm_area_struct *vma, unsigned long addr)
167 int prot = 0;
169 if (!vma || vma->vm_start > addr)
170 return 0;
172 if (vma->vm_flags & VM_READ)
173 prot |= PROT_READ;
174 if (vma->vm_flags & VM_WRITE)
175 prot |= PROT_WRITE;
176 if (vma->vm_flags & VM_EXEC)
177 prot |= PROT_EXEC;
178 return prot;
182 * Map a subpage by creating an anonymous page that contains the union of the old page and
183 * the subpage.
185 static unsigned long
186 mmap_subpage (struct file *file, unsigned long start, unsigned long end, int prot, int flags,
187 loff_t off)
189 void *page = NULL;
190 struct inode *inode;
191 unsigned long ret = 0;
192 struct vm_area_struct *vma = find_vma(current->mm, start);
193 int old_prot = get_page_prot(vma, start);
195 DBG("mmap_subpage(file=%p,start=0x%lx,end=0x%lx,prot=%x,flags=%x,off=0x%llx)\n",
196 file, start, end, prot, flags, off);
199 /* Optimize the case where the old mmap and the new mmap are both anonymous */
200 if ((old_prot & PROT_WRITE) && (flags & MAP_ANONYMOUS) && !vma->vm_file) {
201 if (clear_user((void __user *) start, end - start)) {
202 ret = -EFAULT;
203 goto out;
205 goto skip_mmap;
208 page = (void *) get_zeroed_page(GFP_KERNEL);
209 if (!page)
210 return -ENOMEM;
212 if (old_prot)
213 copy_from_user(page, (void __user *) PAGE_START(start), PAGE_SIZE);
215 down_write(&current->mm->mmap_sem);
217 ret = do_mmap(NULL, PAGE_START(start), PAGE_SIZE, prot | PROT_WRITE,
218 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
220 up_write(&current->mm->mmap_sem);
222 if (IS_ERR((void *) ret))
223 goto out;
225 if (old_prot) {
226 /* copy back the old page contents. */
227 if (offset_in_page(start))
228 copy_to_user((void __user *) PAGE_START(start), page,
229 offset_in_page(start));
230 if (offset_in_page(end))
231 copy_to_user((void __user *) end, page + offset_in_page(end),
232 PAGE_SIZE - offset_in_page(end));
235 if (!(flags & MAP_ANONYMOUS)) {
236 /* read the file contents */
237 inode = file->f_path.dentry->d_inode;
238 if (!inode->i_fop || !file->f_op->read
239 || ((*file->f_op->read)(file, (char __user *) start, end - start, &off) < 0))
241 ret = -EINVAL;
242 goto out;
246 skip_mmap:
247 if (!(prot & PROT_WRITE))
248 ret = sys_mprotect(PAGE_START(start), PAGE_SIZE, prot | old_prot);
249 out:
250 if (page)
251 free_page((unsigned long) page);
252 return ret;
255 /* SLAB cache for ia64_partial_page structures */
256 struct kmem_cache *ia64_partial_page_cachep;
259 * init ia64_partial_page_list.
260 * return 0 means kmalloc fail.
262 struct ia64_partial_page_list*
263 ia32_init_pp_list(void)
265 struct ia64_partial_page_list *p;
267 if ((p = kmalloc(sizeof(*p), GFP_KERNEL)) == NULL)
268 return p;
269 p->pp_head = NULL;
270 p->ppl_rb = RB_ROOT;
271 p->pp_hint = NULL;
272 atomic_set(&p->pp_count, 1);
273 return p;
277 * Search for the partial page with @start in partial page list @ppl.
278 * If finds the partial page, return the found partial page.
279 * Else, return 0 and provide @pprev, @rb_link, @rb_parent to
280 * be used by later __ia32_insert_pp().
282 static struct ia64_partial_page *
283 __ia32_find_pp(struct ia64_partial_page_list *ppl, unsigned int start,
284 struct ia64_partial_page **pprev, struct rb_node ***rb_link,
285 struct rb_node **rb_parent)
287 struct ia64_partial_page *pp;
288 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
290 pp = ppl->pp_hint;
291 if (pp && pp->base == start)
292 return pp;
294 __rb_link = &ppl->ppl_rb.rb_node;
295 rb_prev = __rb_parent = NULL;
297 while (*__rb_link) {
298 __rb_parent = *__rb_link;
299 pp = rb_entry(__rb_parent, struct ia64_partial_page, pp_rb);
301 if (pp->base == start) {
302 ppl->pp_hint = pp;
303 return pp;
304 } else if (pp->base < start) {
305 rb_prev = __rb_parent;
306 __rb_link = &__rb_parent->rb_right;
307 } else {
308 __rb_link = &__rb_parent->rb_left;
312 *rb_link = __rb_link;
313 *rb_parent = __rb_parent;
314 *pprev = NULL;
315 if (rb_prev)
316 *pprev = rb_entry(rb_prev, struct ia64_partial_page, pp_rb);
317 return NULL;
321 * insert @pp into @ppl.
323 static void
324 __ia32_insert_pp(struct ia64_partial_page_list *ppl,
325 struct ia64_partial_page *pp, struct ia64_partial_page *prev,
326 struct rb_node **rb_link, struct rb_node *rb_parent)
328 /* link list */
329 if (prev) {
330 pp->next = prev->next;
331 prev->next = pp;
332 } else {
333 ppl->pp_head = pp;
334 if (rb_parent)
335 pp->next = rb_entry(rb_parent,
336 struct ia64_partial_page, pp_rb);
337 else
338 pp->next = NULL;
341 /* link rb */
342 rb_link_node(&pp->pp_rb, rb_parent, rb_link);
343 rb_insert_color(&pp->pp_rb, &ppl->ppl_rb);
345 ppl->pp_hint = pp;
349 * delete @pp from partial page list @ppl.
351 static void
352 __ia32_delete_pp(struct ia64_partial_page_list *ppl,
353 struct ia64_partial_page *pp, struct ia64_partial_page *prev)
355 if (prev) {
356 prev->next = pp->next;
357 if (ppl->pp_hint == pp)
358 ppl->pp_hint = prev;
359 } else {
360 ppl->pp_head = pp->next;
361 if (ppl->pp_hint == pp)
362 ppl->pp_hint = pp->next;
364 rb_erase(&pp->pp_rb, &ppl->ppl_rb);
365 kmem_cache_free(ia64_partial_page_cachep, pp);
368 static struct ia64_partial_page *
369 __pp_prev(struct ia64_partial_page *pp)
371 struct rb_node *prev = rb_prev(&pp->pp_rb);
372 if (prev)
373 return rb_entry(prev, struct ia64_partial_page, pp_rb);
374 else
375 return NULL;
379 * Delete partial pages with address between @start and @end.
380 * @start and @end are page aligned.
382 static void
383 __ia32_delete_pp_range(unsigned int start, unsigned int end)
385 struct ia64_partial_page *pp, *prev;
386 struct rb_node **rb_link, *rb_parent;
388 if (start >= end)
389 return;
391 pp = __ia32_find_pp(current->thread.ppl, start, &prev,
392 &rb_link, &rb_parent);
393 if (pp)
394 prev = __pp_prev(pp);
395 else {
396 if (prev)
397 pp = prev->next;
398 else
399 pp = current->thread.ppl->pp_head;
402 while (pp && pp->base < end) {
403 struct ia64_partial_page *tmp = pp->next;
404 __ia32_delete_pp(current->thread.ppl, pp, prev);
405 pp = tmp;
410 * Set the range between @start and @end in bitmap.
411 * @start and @end should be IA32 page aligned and in the same IA64 page.
413 static int
414 __ia32_set_pp(unsigned int start, unsigned int end, int flags)
416 struct ia64_partial_page *pp, *prev;
417 struct rb_node ** rb_link, *rb_parent;
418 unsigned int pstart, start_bit, end_bit, i;
420 pstart = PAGE_START(start);
421 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
422 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
423 if (end_bit == 0)
424 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
425 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
426 &rb_link, &rb_parent);
427 if (pp) {
428 for (i = start_bit; i < end_bit; i++)
429 set_bit(i, &pp->bitmap);
431 * Check: if this partial page has been set to a full page,
432 * then delete it.
434 if (find_first_zero_bit(&pp->bitmap, sizeof(pp->bitmap)*8) >=
435 PAGE_SIZE/IA32_PAGE_SIZE) {
436 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
438 return 0;
442 * MAP_FIXED may lead to overlapping mmap.
443 * In this case, the requested mmap area may already mmaped as a full
444 * page. So check vma before adding a new partial page.
446 if (flags & MAP_FIXED) {
447 struct vm_area_struct *vma = find_vma(current->mm, pstart);
448 if (vma && vma->vm_start <= pstart)
449 return 0;
452 /* new a ia64_partial_page */
453 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
454 if (!pp)
455 return -ENOMEM;
456 pp->base = pstart;
457 pp->bitmap = 0;
458 for (i=start_bit; i<end_bit; i++)
459 set_bit(i, &(pp->bitmap));
460 pp->next = NULL;
461 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
462 return 0;
466 * @start and @end should be IA32 page aligned, but don't need to be in the
467 * same IA64 page. Split @start and @end to make sure they're in the same IA64
468 * page, then call __ia32_set_pp().
470 static void
471 ia32_set_pp(unsigned int start, unsigned int end, int flags)
473 down_write(&current->mm->mmap_sem);
474 if (flags & MAP_FIXED) {
476 * MAP_FIXED may lead to overlapping mmap. When this happens,
477 * a series of complete IA64 pages results in deletion of
478 * old partial pages in that range.
480 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
483 if (end < PAGE_ALIGN(start)) {
484 __ia32_set_pp(start, end, flags);
485 } else {
486 if (offset_in_page(start))
487 __ia32_set_pp(start, PAGE_ALIGN(start), flags);
488 if (offset_in_page(end))
489 __ia32_set_pp(PAGE_START(end), end, flags);
491 up_write(&current->mm->mmap_sem);
495 * Unset the range between @start and @end in bitmap.
496 * @start and @end should be IA32 page aligned and in the same IA64 page.
497 * After doing that, if the bitmap is 0, then free the page and return 1,
498 * else return 0;
499 * If not find the partial page in the list, then
500 * If the vma exists, then the full page is set to a partial page;
501 * Else return -ENOMEM.
503 static int
504 __ia32_unset_pp(unsigned int start, unsigned int end)
506 struct ia64_partial_page *pp, *prev;
507 struct rb_node ** rb_link, *rb_parent;
508 unsigned int pstart, start_bit, end_bit, i;
509 struct vm_area_struct *vma;
511 pstart = PAGE_START(start);
512 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
513 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
514 if (end_bit == 0)
515 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
517 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
518 &rb_link, &rb_parent);
519 if (pp) {
520 for (i = start_bit; i < end_bit; i++)
521 clear_bit(i, &pp->bitmap);
522 if (pp->bitmap == 0) {
523 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
524 return 1;
526 return 0;
529 vma = find_vma(current->mm, pstart);
530 if (!vma || vma->vm_start > pstart) {
531 return -ENOMEM;
534 /* new a ia64_partial_page */
535 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
536 if (!pp)
537 return -ENOMEM;
538 pp->base = pstart;
539 pp->bitmap = 0;
540 for (i = 0; i < start_bit; i++)
541 set_bit(i, &(pp->bitmap));
542 for (i = end_bit; i < PAGE_SIZE / IA32_PAGE_SIZE; i++)
543 set_bit(i, &(pp->bitmap));
544 pp->next = NULL;
545 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
546 return 0;
550 * Delete pp between PAGE_ALIGN(start) and PAGE_START(end) by calling
551 * __ia32_delete_pp_range(). Unset possible partial pages by calling
552 * __ia32_unset_pp().
553 * The returned value see __ia32_unset_pp().
555 static int
556 ia32_unset_pp(unsigned int *startp, unsigned int *endp)
558 unsigned int start = *startp, end = *endp;
559 int ret = 0;
561 down_write(&current->mm->mmap_sem);
563 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
565 if (end < PAGE_ALIGN(start)) {
566 ret = __ia32_unset_pp(start, end);
567 if (ret == 1) {
568 *startp = PAGE_START(start);
569 *endp = PAGE_ALIGN(end);
571 if (ret == 0) {
572 /* to shortcut sys_munmap() in sys32_munmap() */
573 *startp = PAGE_START(start);
574 *endp = PAGE_START(end);
576 } else {
577 if (offset_in_page(start)) {
578 ret = __ia32_unset_pp(start, PAGE_ALIGN(start));
579 if (ret == 1)
580 *startp = PAGE_START(start);
581 if (ret == 0)
582 *startp = PAGE_ALIGN(start);
583 if (ret < 0)
584 goto out;
586 if (offset_in_page(end)) {
587 ret = __ia32_unset_pp(PAGE_START(end), end);
588 if (ret == 1)
589 *endp = PAGE_ALIGN(end);
590 if (ret == 0)
591 *endp = PAGE_START(end);
595 out:
596 up_write(&current->mm->mmap_sem);
597 return ret;
601 * Compare the range between @start and @end with bitmap in partial page.
602 * @start and @end should be IA32 page aligned and in the same IA64 page.
604 static int
605 __ia32_compare_pp(unsigned int start, unsigned int end)
607 struct ia64_partial_page *pp, *prev;
608 struct rb_node ** rb_link, *rb_parent;
609 unsigned int pstart, start_bit, end_bit, size;
610 unsigned int first_bit, next_zero_bit; /* the first range in bitmap */
612 pstart = PAGE_START(start);
614 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
615 &rb_link, &rb_parent);
616 if (!pp)
617 return 1;
619 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
620 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
621 size = sizeof(pp->bitmap) * 8;
622 first_bit = find_first_bit(&pp->bitmap, size);
623 next_zero_bit = find_next_zero_bit(&pp->bitmap, size, first_bit);
624 if ((start_bit < first_bit) || (end_bit > next_zero_bit)) {
625 /* exceeds the first range in bitmap */
626 return -ENOMEM;
627 } else if ((start_bit == first_bit) && (end_bit == next_zero_bit)) {
628 first_bit = find_next_bit(&pp->bitmap, size, next_zero_bit);
629 if ((next_zero_bit < first_bit) && (first_bit < size))
630 return 1; /* has next range */
631 else
632 return 0; /* no next range */
633 } else
634 return 1;
638 * @start and @end should be IA32 page aligned, but don't need to be in the
639 * same IA64 page. Split @start and @end to make sure they're in the same IA64
640 * page, then call __ia32_compare_pp().
642 * Take this as example: the range is the 1st and 2nd 4K page.
643 * Return 0 if they fit bitmap exactly, i.e. bitmap = 00000011;
644 * Return 1 if the range doesn't cover whole bitmap, e.g. bitmap = 00001111;
645 * Return -ENOMEM if the range exceeds the bitmap, e.g. bitmap = 00000001 or
646 * bitmap = 00000101.
648 static int
649 ia32_compare_pp(unsigned int *startp, unsigned int *endp)
651 unsigned int start = *startp, end = *endp;
652 int retval = 0;
654 down_write(&current->mm->mmap_sem);
656 if (end < PAGE_ALIGN(start)) {
657 retval = __ia32_compare_pp(start, end);
658 if (retval == 0) {
659 *startp = PAGE_START(start);
660 *endp = PAGE_ALIGN(end);
662 } else {
663 if (offset_in_page(start)) {
664 retval = __ia32_compare_pp(start,
665 PAGE_ALIGN(start));
666 if (retval == 0)
667 *startp = PAGE_START(start);
668 if (retval < 0)
669 goto out;
671 if (offset_in_page(end)) {
672 retval = __ia32_compare_pp(PAGE_START(end), end);
673 if (retval == 0)
674 *endp = PAGE_ALIGN(end);
678 out:
679 up_write(&current->mm->mmap_sem);
680 return retval;
683 static void
684 __ia32_drop_pp_list(struct ia64_partial_page_list *ppl)
686 struct ia64_partial_page *pp = ppl->pp_head;
688 while (pp) {
689 struct ia64_partial_page *next = pp->next;
690 kmem_cache_free(ia64_partial_page_cachep, pp);
691 pp = next;
694 kfree(ppl);
697 void
698 ia32_drop_ia64_partial_page_list(struct task_struct *task)
700 struct ia64_partial_page_list* ppl = task->thread.ppl;
702 if (ppl && atomic_dec_and_test(&ppl->pp_count))
703 __ia32_drop_pp_list(ppl);
707 * Copy current->thread.ppl to ppl (already initialized).
709 static int
710 __ia32_copy_pp_list(struct ia64_partial_page_list *ppl)
712 struct ia64_partial_page *pp, *tmp, *prev;
713 struct rb_node **rb_link, *rb_parent;
715 ppl->pp_head = NULL;
716 ppl->pp_hint = NULL;
717 ppl->ppl_rb = RB_ROOT;
718 rb_link = &ppl->ppl_rb.rb_node;
719 rb_parent = NULL;
720 prev = NULL;
722 for (pp = current->thread.ppl->pp_head; pp; pp = pp->next) {
723 tmp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
724 if (!tmp)
725 return -ENOMEM;
726 *tmp = *pp;
727 __ia32_insert_pp(ppl, tmp, prev, rb_link, rb_parent);
728 prev = tmp;
729 rb_link = &tmp->pp_rb.rb_right;
730 rb_parent = &tmp->pp_rb;
732 return 0;
736 ia32_copy_ia64_partial_page_list(struct task_struct *p,
737 unsigned long clone_flags)
739 int retval = 0;
741 if (clone_flags & CLONE_VM) {
742 atomic_inc(&current->thread.ppl->pp_count);
743 p->thread.ppl = current->thread.ppl;
744 } else {
745 p->thread.ppl = ia32_init_pp_list();
746 if (!p->thread.ppl)
747 return -ENOMEM;
748 down_write(&current->mm->mmap_sem);
750 retval = __ia32_copy_pp_list(p->thread.ppl);
752 up_write(&current->mm->mmap_sem);
755 return retval;
758 static unsigned long
759 emulate_mmap (struct file *file, unsigned long start, unsigned long len, int prot, int flags,
760 loff_t off)
762 unsigned long tmp, end, pend, pstart, ret, is_congruent, fudge = 0;
763 struct inode *inode;
764 loff_t poff;
766 end = start + len;
767 pstart = PAGE_START(start);
768 pend = PAGE_ALIGN(end);
770 if (flags & MAP_FIXED) {
771 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
772 if (start > pstart) {
773 if (flags & MAP_SHARED)
774 printk(KERN_INFO
775 "%s(%d): emulate_mmap() can't share head (addr=0x%lx)\n",
776 current->comm, task_pid_nr(current), start);
777 ret = mmap_subpage(file, start, min(PAGE_ALIGN(start), end), prot, flags,
778 off);
779 if (IS_ERR((void *) ret))
780 return ret;
781 pstart += PAGE_SIZE;
782 if (pstart >= pend)
783 goto out; /* done */
785 if (end < pend) {
786 if (flags & MAP_SHARED)
787 printk(KERN_INFO
788 "%s(%d): emulate_mmap() can't share tail (end=0x%lx)\n",
789 current->comm, task_pid_nr(current), end);
790 ret = mmap_subpage(file, max(start, PAGE_START(end)), end, prot, flags,
791 (off + len) - offset_in_page(end));
792 if (IS_ERR((void *) ret))
793 return ret;
794 pend -= PAGE_SIZE;
795 if (pstart >= pend)
796 goto out; /* done */
798 } else {
800 * If a start address was specified, use it if the entire rounded out area
801 * is available.
803 if (start && !pstart)
804 fudge = 1; /* handle case of mapping to range (0,PAGE_SIZE) */
805 tmp = arch_get_unmapped_area(file, pstart - fudge, pend - pstart, 0, flags);
806 if (tmp != pstart) {
807 pstart = tmp;
808 start = pstart + offset_in_page(off); /* make start congruent with off */
809 end = start + len;
810 pend = PAGE_ALIGN(end);
814 poff = off + (pstart - start); /* note: (pstart - start) may be negative */
815 is_congruent = (flags & MAP_ANONYMOUS) || (offset_in_page(poff) == 0);
817 if ((flags & MAP_SHARED) && !is_congruent)
818 printk(KERN_INFO "%s(%d): emulate_mmap() can't share contents of incongruent mmap "
819 "(addr=0x%lx,off=0x%llx)\n", current->comm, task_pid_nr(current), start, off);
821 DBG("mmap_body: mapping [0x%lx-0x%lx) %s with poff 0x%llx\n", pstart, pend,
822 is_congruent ? "congruent" : "not congruent", poff);
824 down_write(&current->mm->mmap_sem);
826 if (!(flags & MAP_ANONYMOUS) && is_congruent)
827 ret = do_mmap(file, pstart, pend - pstart, prot, flags | MAP_FIXED, poff);
828 else
829 ret = do_mmap(NULL, pstart, pend - pstart,
830 prot | ((flags & MAP_ANONYMOUS) ? 0 : PROT_WRITE),
831 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
833 up_write(&current->mm->mmap_sem);
835 if (IS_ERR((void *) ret))
836 return ret;
838 if (!is_congruent) {
839 /* read the file contents */
840 inode = file->f_path.dentry->d_inode;
841 if (!inode->i_fop || !file->f_op->read
842 || ((*file->f_op->read)(file, (char __user *) pstart, pend - pstart, &poff)
843 < 0))
845 sys_munmap(pstart, pend - pstart);
846 return -EINVAL;
848 if (!(prot & PROT_WRITE) && sys_mprotect(pstart, pend - pstart, prot) < 0)
849 return -EINVAL;
852 if (!(flags & MAP_FIXED))
853 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
854 out:
855 return start;
858 #endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
860 static inline unsigned int
861 get_prot32 (unsigned int prot)
863 if (prot & PROT_WRITE)
864 /* on x86, PROT_WRITE implies PROT_READ which implies PROT_EEC */
865 prot |= PROT_READ | PROT_WRITE | PROT_EXEC;
866 else if (prot & (PROT_READ | PROT_EXEC))
867 /* on x86, there is no distinction between PROT_READ and PROT_EXEC */
868 prot |= (PROT_READ | PROT_EXEC);
870 return prot;
873 unsigned long
874 ia32_do_mmap (struct file *file, unsigned long addr, unsigned long len, int prot, int flags,
875 loff_t offset)
877 DBG("ia32_do_mmap(file=%p,addr=0x%lx,len=0x%lx,prot=%x,flags=%x,offset=0x%llx)\n",
878 file, addr, len, prot, flags, offset);
880 if (file && (!file->f_op || !file->f_op->mmap))
881 return -ENODEV;
883 len = IA32_PAGE_ALIGN(len);
884 if (len == 0)
885 return addr;
887 if (len > IA32_PAGE_OFFSET || addr > IA32_PAGE_OFFSET - len)
889 if (flags & MAP_FIXED)
890 return -ENOMEM;
891 else
892 return -EINVAL;
895 if (OFFSET4K(offset))
896 return -EINVAL;
898 prot = get_prot32(prot);
900 #if PAGE_SHIFT > IA32_PAGE_SHIFT
901 mutex_lock(&ia32_mmap_mutex);
903 addr = emulate_mmap(file, addr, len, prot, flags, offset);
905 mutex_unlock(&ia32_mmap_mutex);
906 #else
907 down_write(&current->mm->mmap_sem);
909 addr = do_mmap(file, addr, len, prot, flags, offset);
911 up_write(&current->mm->mmap_sem);
912 #endif
913 DBG("ia32_do_mmap: returning 0x%lx\n", addr);
914 return addr;
918 * Linux/i386 didn't use to be able to handle more than 4 system call parameters, so these
919 * system calls used a memory block for parameter passing..
922 struct mmap_arg_struct {
923 unsigned int addr;
924 unsigned int len;
925 unsigned int prot;
926 unsigned int flags;
927 unsigned int fd;
928 unsigned int offset;
931 asmlinkage long
932 sys32_mmap (struct mmap_arg_struct __user *arg)
934 struct mmap_arg_struct a;
935 struct file *file = NULL;
936 unsigned long addr;
937 int flags;
939 if (copy_from_user(&a, arg, sizeof(a)))
940 return -EFAULT;
942 if (OFFSET4K(a.offset))
943 return -EINVAL;
945 flags = a.flags;
947 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
948 if (!(flags & MAP_ANONYMOUS)) {
949 file = fget(a.fd);
950 if (!file)
951 return -EBADF;
954 addr = ia32_do_mmap(file, a.addr, a.len, a.prot, flags, a.offset);
956 if (file)
957 fput(file);
958 return addr;
961 asmlinkage long
962 sys32_mmap2 (unsigned int addr, unsigned int len, unsigned int prot, unsigned int flags,
963 unsigned int fd, unsigned int pgoff)
965 struct file *file = NULL;
966 unsigned long retval;
968 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
969 if (!(flags & MAP_ANONYMOUS)) {
970 file = fget(fd);
971 if (!file)
972 return -EBADF;
975 retval = ia32_do_mmap(file, addr, len, prot, flags,
976 (unsigned long) pgoff << IA32_PAGE_SHIFT);
978 if (file)
979 fput(file);
980 return retval;
983 asmlinkage long
984 sys32_munmap (unsigned int start, unsigned int len)
986 unsigned int end = start + len;
987 long ret;
989 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
990 ret = sys_munmap(start, end - start);
991 #else
992 if (OFFSET4K(start))
993 return -EINVAL;
995 end = IA32_PAGE_ALIGN(end);
996 if (start >= end)
997 return -EINVAL;
999 ret = ia32_unset_pp(&start, &end);
1000 if (ret < 0)
1001 return ret;
1003 if (start >= end)
1004 return 0;
1006 mutex_lock(&ia32_mmap_mutex);
1007 ret = sys_munmap(start, end - start);
1008 mutex_unlock(&ia32_mmap_mutex);
1009 #endif
1010 return ret;
1013 #if PAGE_SHIFT > IA32_PAGE_SHIFT
1016 * When mprotect()ing a partial page, we set the permission to the union of the old
1017 * settings and the new settings. In other words, it's only possible to make access to a
1018 * partial page less restrictive.
1020 static long
1021 mprotect_subpage (unsigned long address, int new_prot)
1023 int old_prot;
1024 struct vm_area_struct *vma;
1026 if (new_prot == PROT_NONE)
1027 return 0; /* optimize case where nothing changes... */
1028 vma = find_vma(current->mm, address);
1029 old_prot = get_page_prot(vma, address);
1030 return sys_mprotect(address, PAGE_SIZE, new_prot | old_prot);
1033 #endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
1035 asmlinkage long
1036 sys32_mprotect (unsigned int start, unsigned int len, int prot)
1038 unsigned int end = start + len;
1039 #if PAGE_SHIFT > IA32_PAGE_SHIFT
1040 long retval = 0;
1041 #endif
1043 prot = get_prot32(prot);
1045 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
1046 return sys_mprotect(start, end - start, prot);
1047 #else
1048 if (OFFSET4K(start))
1049 return -EINVAL;
1051 end = IA32_PAGE_ALIGN(end);
1052 if (end < start)
1053 return -EINVAL;
1055 retval = ia32_compare_pp(&start, &end);
1057 if (retval < 0)
1058 return retval;
1060 mutex_lock(&ia32_mmap_mutex);
1062 if (offset_in_page(start)) {
1063 /* start address is 4KB aligned but not page aligned. */
1064 retval = mprotect_subpage(PAGE_START(start), prot);
1065 if (retval < 0)
1066 goto out;
1068 start = PAGE_ALIGN(start);
1069 if (start >= end)
1070 goto out; /* retval is already zero... */
1073 if (offset_in_page(end)) {
1074 /* end address is 4KB aligned but not page aligned. */
1075 retval = mprotect_subpage(PAGE_START(end), prot);
1076 if (retval < 0)
1077 goto out;
1079 end = PAGE_START(end);
1081 retval = sys_mprotect(start, end - start, prot);
1083 out:
1084 mutex_unlock(&ia32_mmap_mutex);
1085 return retval;
1086 #endif
1089 asmlinkage long
1090 sys32_mremap (unsigned int addr, unsigned int old_len, unsigned int new_len,
1091 unsigned int flags, unsigned int new_addr)
1093 long ret;
1095 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
1096 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1097 #else
1098 unsigned int old_end, new_end;
1100 if (OFFSET4K(addr))
1101 return -EINVAL;
1103 old_len = IA32_PAGE_ALIGN(old_len);
1104 new_len = IA32_PAGE_ALIGN(new_len);
1105 old_end = addr + old_len;
1106 new_end = addr + new_len;
1108 if (!new_len)
1109 return -EINVAL;
1111 if ((flags & MREMAP_FIXED) && (OFFSET4K(new_addr)))
1112 return -EINVAL;
1114 if (old_len >= new_len) {
1115 ret = sys32_munmap(addr + new_len, old_len - new_len);
1116 if (ret && old_len != new_len)
1117 return ret;
1118 ret = addr;
1119 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
1120 return ret;
1121 old_len = new_len;
1124 addr = PAGE_START(addr);
1125 old_len = PAGE_ALIGN(old_end) - addr;
1126 new_len = PAGE_ALIGN(new_end) - addr;
1128 mutex_lock(&ia32_mmap_mutex);
1129 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1130 mutex_unlock(&ia32_mmap_mutex);
1132 if ((ret >= 0) && (old_len < new_len)) {
1133 /* mremap expanded successfully */
1134 ia32_set_pp(old_end, new_end, flags);
1136 #endif
1137 return ret;
1140 asmlinkage long
1141 sys32_pipe (int __user *fd)
1143 int retval;
1144 int fds[2];
1146 retval = do_pipe(fds);
1147 if (retval)
1148 goto out;
1149 if (copy_to_user(fd, fds, sizeof(fds)))
1150 retval = -EFAULT;
1151 out:
1152 return retval;
1155 static inline long
1156 get_tv32 (struct timeval *o, struct compat_timeval __user *i)
1158 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
1159 (__get_user(o->tv_sec, &i->tv_sec) | __get_user(o->tv_usec, &i->tv_usec)));
1162 static inline long
1163 put_tv32 (struct compat_timeval __user *o, struct timeval *i)
1165 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
1166 (__put_user(i->tv_sec, &o->tv_sec) | __put_user(i->tv_usec, &o->tv_usec)));
1169 asmlinkage unsigned long
1170 sys32_alarm (unsigned int seconds)
1172 return alarm_setitimer(seconds);
1175 /* Translations due to time_t size differences. Which affects all
1176 sorts of things, like timeval and itimerval. */
1178 extern struct timezone sys_tz;
1180 asmlinkage long
1181 sys32_gettimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1183 if (tv) {
1184 struct timeval ktv;
1185 do_gettimeofday(&ktv);
1186 if (put_tv32(tv, &ktv))
1187 return -EFAULT;
1189 if (tz) {
1190 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
1191 return -EFAULT;
1193 return 0;
1196 asmlinkage long
1197 sys32_settimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1199 struct timeval ktv;
1200 struct timespec kts;
1201 struct timezone ktz;
1203 if (tv) {
1204 if (get_tv32(&ktv, tv))
1205 return -EFAULT;
1206 kts.tv_sec = ktv.tv_sec;
1207 kts.tv_nsec = ktv.tv_usec * 1000;
1209 if (tz) {
1210 if (copy_from_user(&ktz, tz, sizeof(ktz)))
1211 return -EFAULT;
1214 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
1217 struct getdents32_callback {
1218 struct compat_dirent __user *current_dir;
1219 struct compat_dirent __user *previous;
1220 int count;
1221 int error;
1224 struct readdir32_callback {
1225 struct old_linux32_dirent __user * dirent;
1226 int count;
1229 static int
1230 filldir32 (void *__buf, const char *name, int namlen, loff_t offset, u64 ino,
1231 unsigned int d_type)
1233 struct compat_dirent __user * dirent;
1234 struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
1235 int reclen = ROUND_UP(offsetof(struct compat_dirent, d_name) + namlen + 1, 4);
1236 u32 d_ino;
1238 buf->error = -EINVAL; /* only used if we fail.. */
1239 if (reclen > buf->count)
1240 return -EINVAL;
1241 d_ino = ino;
1242 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1243 return -EOVERFLOW;
1244 buf->error = -EFAULT; /* only used if we fail.. */
1245 dirent = buf->previous;
1246 if (dirent)
1247 if (put_user(offset, &dirent->d_off))
1248 return -EFAULT;
1249 dirent = buf->current_dir;
1250 buf->previous = dirent;
1251 if (put_user(d_ino, &dirent->d_ino)
1252 || put_user(reclen, &dirent->d_reclen)
1253 || copy_to_user(dirent->d_name, name, namlen)
1254 || put_user(0, dirent->d_name + namlen))
1255 return -EFAULT;
1256 dirent = (struct compat_dirent __user *) ((char __user *) dirent + reclen);
1257 buf->current_dir = dirent;
1258 buf->count -= reclen;
1259 return 0;
1262 asmlinkage long
1263 sys32_getdents (unsigned int fd, struct compat_dirent __user *dirent, unsigned int count)
1265 struct file * file;
1266 struct compat_dirent __user * lastdirent;
1267 struct getdents32_callback buf;
1268 int error;
1270 error = -EFAULT;
1271 if (!access_ok(VERIFY_WRITE, dirent, count))
1272 goto out;
1274 error = -EBADF;
1275 file = fget(fd);
1276 if (!file)
1277 goto out;
1279 buf.current_dir = dirent;
1280 buf.previous = NULL;
1281 buf.count = count;
1282 buf.error = 0;
1284 error = vfs_readdir(file, filldir32, &buf);
1285 if (error < 0)
1286 goto out_putf;
1287 error = buf.error;
1288 lastdirent = buf.previous;
1289 if (lastdirent) {
1290 if (put_user(file->f_pos, &lastdirent->d_off))
1291 error = -EFAULT;
1292 else
1293 error = count - buf.count;
1296 out_putf:
1297 fput(file);
1298 out:
1299 return error;
1302 static int
1303 fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, u64 ino,
1304 unsigned int d_type)
1306 struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
1307 struct old_linux32_dirent __user * dirent;
1308 u32 d_ino;
1310 if (buf->count)
1311 return -EINVAL;
1312 d_ino = ino;
1313 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1314 return -EOVERFLOW;
1315 buf->count++;
1316 dirent = buf->dirent;
1317 if (put_user(d_ino, &dirent->d_ino)
1318 || put_user(offset, &dirent->d_offset)
1319 || put_user(namlen, &dirent->d_namlen)
1320 || copy_to_user(dirent->d_name, name, namlen)
1321 || put_user(0, dirent->d_name + namlen))
1322 return -EFAULT;
1323 return 0;
1326 asmlinkage long
1327 sys32_readdir (unsigned int fd, void __user *dirent, unsigned int count)
1329 int error;
1330 struct file * file;
1331 struct readdir32_callback buf;
1333 error = -EBADF;
1334 file = fget(fd);
1335 if (!file)
1336 goto out;
1338 buf.count = 0;
1339 buf.dirent = dirent;
1341 error = vfs_readdir(file, fillonedir32, &buf);
1342 if (error >= 0)
1343 error = buf.count;
1344 fput(file);
1345 out:
1346 return error;
1349 struct sel_arg_struct {
1350 unsigned int n;
1351 unsigned int inp;
1352 unsigned int outp;
1353 unsigned int exp;
1354 unsigned int tvp;
1357 asmlinkage long
1358 sys32_old_select (struct sel_arg_struct __user *arg)
1360 struct sel_arg_struct a;
1362 if (copy_from_user(&a, arg, sizeof(a)))
1363 return -EFAULT;
1364 return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1365 compat_ptr(a.exp), compat_ptr(a.tvp));
1368 #define SEMOP 1
1369 #define SEMGET 2
1370 #define SEMCTL 3
1371 #define SEMTIMEDOP 4
1372 #define MSGSND 11
1373 #define MSGRCV 12
1374 #define MSGGET 13
1375 #define MSGCTL 14
1376 #define SHMAT 21
1377 #define SHMDT 22
1378 #define SHMGET 23
1379 #define SHMCTL 24
1381 asmlinkage long
1382 sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
1384 int version;
1386 version = call >> 16; /* hack for backward compatibility */
1387 call &= 0xffff;
1389 switch (call) {
1390 case SEMTIMEDOP:
1391 if (fifth)
1392 return compat_sys_semtimedop(first, compat_ptr(ptr),
1393 second, compat_ptr(fifth));
1394 /* else fall through for normal semop() */
1395 case SEMOP:
1396 /* struct sembuf is the same on 32 and 64bit :)) */
1397 return sys_semtimedop(first, compat_ptr(ptr), second,
1398 NULL);
1399 case SEMGET:
1400 return sys_semget(first, second, third);
1401 case SEMCTL:
1402 return compat_sys_semctl(first, second, third, compat_ptr(ptr));
1404 case MSGSND:
1405 return compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
1406 case MSGRCV:
1407 return compat_sys_msgrcv(first, second, fifth, third, version, compat_ptr(ptr));
1408 case MSGGET:
1409 return sys_msgget((key_t) first, second);
1410 case MSGCTL:
1411 return compat_sys_msgctl(first, second, compat_ptr(ptr));
1413 case SHMAT:
1414 return compat_sys_shmat(first, second, third, version, compat_ptr(ptr));
1415 break;
1416 case SHMDT:
1417 return sys_shmdt(compat_ptr(ptr));
1418 case SHMGET:
1419 return sys_shmget(first, (unsigned)second, third);
1420 case SHMCTL:
1421 return compat_sys_shmctl(first, second, compat_ptr(ptr));
1423 default:
1424 return -ENOSYS;
1426 return -EINVAL;
1429 asmlinkage long
1430 compat_sys_wait4 (compat_pid_t pid, compat_uint_t * stat_addr, int options,
1431 struct compat_rusage *ru);
1433 asmlinkage long
1434 sys32_waitpid (int pid, unsigned int *stat_addr, int options)
1436 return compat_sys_wait4(pid, stat_addr, options, NULL);
1439 static unsigned int
1440 ia32_peek (struct task_struct *child, unsigned long addr, unsigned int *val)
1442 size_t copied;
1443 unsigned int ret;
1445 copied = access_process_vm(child, addr, val, sizeof(*val), 0);
1446 return (copied != sizeof(ret)) ? -EIO : 0;
1449 static unsigned int
1450 ia32_poke (struct task_struct *child, unsigned long addr, unsigned int val)
1453 if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val))
1454 return -EIO;
1455 return 0;
1459 * The order in which registers are stored in the ptrace regs structure
1461 #define PT_EBX 0
1462 #define PT_ECX 1
1463 #define PT_EDX 2
1464 #define PT_ESI 3
1465 #define PT_EDI 4
1466 #define PT_EBP 5
1467 #define PT_EAX 6
1468 #define PT_DS 7
1469 #define PT_ES 8
1470 #define PT_FS 9
1471 #define PT_GS 10
1472 #define PT_ORIG_EAX 11
1473 #define PT_EIP 12
1474 #define PT_CS 13
1475 #define PT_EFL 14
1476 #define PT_UESP 15
1477 #define PT_SS 16
1479 static unsigned int
1480 getreg (struct task_struct *child, int regno)
1482 struct pt_regs *child_regs;
1484 child_regs = task_pt_regs(child);
1485 switch (regno / sizeof(int)) {
1486 case PT_EBX: return child_regs->r11;
1487 case PT_ECX: return child_regs->r9;
1488 case PT_EDX: return child_regs->r10;
1489 case PT_ESI: return child_regs->r14;
1490 case PT_EDI: return child_regs->r15;
1491 case PT_EBP: return child_regs->r13;
1492 case PT_EAX: return child_regs->r8;
1493 case PT_ORIG_EAX: return child_regs->r1; /* see dispatch_to_ia32_handler() */
1494 case PT_EIP: return child_regs->cr_iip;
1495 case PT_UESP: return child_regs->r12;
1496 case PT_EFL: return child->thread.eflag;
1497 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1498 return __USER_DS;
1499 case PT_CS: return __USER_CS;
1500 default:
1501 printk(KERN_ERR "ia32.getreg(): unknown register %d\n", regno);
1502 break;
1504 return 0;
1507 static void
1508 putreg (struct task_struct *child, int regno, unsigned int value)
1510 struct pt_regs *child_regs;
1512 child_regs = task_pt_regs(child);
1513 switch (regno / sizeof(int)) {
1514 case PT_EBX: child_regs->r11 = value; break;
1515 case PT_ECX: child_regs->r9 = value; break;
1516 case PT_EDX: child_regs->r10 = value; break;
1517 case PT_ESI: child_regs->r14 = value; break;
1518 case PT_EDI: child_regs->r15 = value; break;
1519 case PT_EBP: child_regs->r13 = value; break;
1520 case PT_EAX: child_regs->r8 = value; break;
1521 case PT_ORIG_EAX: child_regs->r1 = value; break;
1522 case PT_EIP: child_regs->cr_iip = value; break;
1523 case PT_UESP: child_regs->r12 = value; break;
1524 case PT_EFL: child->thread.eflag = value; break;
1525 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1526 if (value != __USER_DS)
1527 printk(KERN_ERR
1528 "ia32.putreg: attempt to set invalid segment register %d = %x\n",
1529 regno, value);
1530 break;
1531 case PT_CS:
1532 if (value != __USER_CS)
1533 printk(KERN_ERR
1534 "ia32.putreg: attempt to to set invalid segment register %d = %x\n",
1535 regno, value);
1536 break;
1537 default:
1538 printk(KERN_ERR "ia32.putreg: unknown register %d\n", regno);
1539 break;
1543 static void
1544 put_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1545 struct switch_stack *swp, int tos)
1547 struct _fpreg_ia32 *f;
1548 char buf[32];
1550 f = (struct _fpreg_ia32 *)(((unsigned long)buf + 15) & ~15);
1551 if ((regno += tos) >= 8)
1552 regno -= 8;
1553 switch (regno) {
1554 case 0:
1555 ia64f2ia32f(f, &ptp->f8);
1556 break;
1557 case 1:
1558 ia64f2ia32f(f, &ptp->f9);
1559 break;
1560 case 2:
1561 ia64f2ia32f(f, &ptp->f10);
1562 break;
1563 case 3:
1564 ia64f2ia32f(f, &ptp->f11);
1565 break;
1566 case 4:
1567 case 5:
1568 case 6:
1569 case 7:
1570 ia64f2ia32f(f, &swp->f12 + (regno - 4));
1571 break;
1573 copy_to_user(reg, f, sizeof(*reg));
1576 static void
1577 get_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1578 struct switch_stack *swp, int tos)
1581 if ((regno += tos) >= 8)
1582 regno -= 8;
1583 switch (regno) {
1584 case 0:
1585 copy_from_user(&ptp->f8, reg, sizeof(*reg));
1586 break;
1587 case 1:
1588 copy_from_user(&ptp->f9, reg, sizeof(*reg));
1589 break;
1590 case 2:
1591 copy_from_user(&ptp->f10, reg, sizeof(*reg));
1592 break;
1593 case 3:
1594 copy_from_user(&ptp->f11, reg, sizeof(*reg));
1595 break;
1596 case 4:
1597 case 5:
1598 case 6:
1599 case 7:
1600 copy_from_user(&swp->f12 + (regno - 4), reg, sizeof(*reg));
1601 break;
1603 return;
1607 save_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1609 struct switch_stack *swp;
1610 struct pt_regs *ptp;
1611 int i, tos;
1613 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1614 return -EFAULT;
1616 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1617 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1618 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1619 __put_user(tsk->thread.fir, &save->fip);
1620 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1621 __put_user(tsk->thread.fdr, &save->foo);
1622 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1625 * Stack frames start with 16-bytes of temp space
1627 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1628 ptp = task_pt_regs(tsk);
1629 tos = (tsk->thread.fsr >> 11) & 7;
1630 for (i = 0; i < 8; i++)
1631 put_fpreg(i, &save->st_space[i], ptp, swp, tos);
1632 return 0;
1635 static int
1636 restore_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1638 struct switch_stack *swp;
1639 struct pt_regs *ptp;
1640 int i, tos;
1641 unsigned int fsrlo, fsrhi, num32;
1643 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1644 return(-EFAULT);
1646 __get_user(num32, (unsigned int __user *)&save->cwd);
1647 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1648 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1649 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1650 num32 = (fsrhi << 16) | fsrlo;
1651 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1652 __get_user(num32, (unsigned int __user *)&save->fip);
1653 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1654 __get_user(num32, (unsigned int __user *)&save->foo);
1655 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1658 * Stack frames start with 16-bytes of temp space
1660 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1661 ptp = task_pt_regs(tsk);
1662 tos = (tsk->thread.fsr >> 11) & 7;
1663 for (i = 0; i < 8; i++)
1664 get_fpreg(i, &save->st_space[i], ptp, swp, tos);
1665 return 0;
1669 save_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1671 struct switch_stack *swp;
1672 struct pt_regs *ptp;
1673 int i, tos;
1674 unsigned long mxcsr=0;
1675 unsigned long num128[2];
1677 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1678 return -EFAULT;
1680 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1681 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1682 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1683 __put_user(tsk->thread.fir, &save->fip);
1684 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1685 __put_user(tsk->thread.fdr, &save->foo);
1686 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1689 * Stack frames start with 16-bytes of temp space
1691 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1692 ptp = task_pt_regs(tsk);
1693 tos = (tsk->thread.fsr >> 11) & 7;
1694 for (i = 0; i < 8; i++)
1695 put_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1697 mxcsr = ((tsk->thread.fcr>>32) & 0xff80) | ((tsk->thread.fsr>>32) & 0x3f);
1698 __put_user(mxcsr & 0xffff, &save->mxcsr);
1699 for (i = 0; i < 8; i++) {
1700 memcpy(&(num128[0]), &(swp->f16) + i*2, sizeof(unsigned long));
1701 memcpy(&(num128[1]), &(swp->f17) + i*2, sizeof(unsigned long));
1702 copy_to_user(&save->xmm_space[0] + 4*i, num128, sizeof(struct _xmmreg_ia32));
1704 return 0;
1707 static int
1708 restore_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1710 struct switch_stack *swp;
1711 struct pt_regs *ptp;
1712 int i, tos;
1713 unsigned int fsrlo, fsrhi, num32;
1714 int mxcsr;
1715 unsigned long num64;
1716 unsigned long num128[2];
1718 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1719 return(-EFAULT);
1721 __get_user(num32, (unsigned int __user *)&save->cwd);
1722 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1723 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1724 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1725 num32 = (fsrhi << 16) | fsrlo;
1726 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1727 __get_user(num32, (unsigned int __user *)&save->fip);
1728 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1729 __get_user(num32, (unsigned int __user *)&save->foo);
1730 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1733 * Stack frames start with 16-bytes of temp space
1735 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1736 ptp = task_pt_regs(tsk);
1737 tos = (tsk->thread.fsr >> 11) & 7;
1738 for (i = 0; i < 8; i++)
1739 get_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1741 __get_user(mxcsr, (unsigned int __user *)&save->mxcsr);
1742 num64 = mxcsr & 0xff10;
1743 tsk->thread.fcr = (tsk->thread.fcr & (~0xff1000000000UL)) | (num64<<32);
1744 num64 = mxcsr & 0x3f;
1745 tsk->thread.fsr = (tsk->thread.fsr & (~0x3f00000000UL)) | (num64<<32);
1747 for (i = 0; i < 8; i++) {
1748 copy_from_user(num128, &save->xmm_space[0] + 4*i, sizeof(struct _xmmreg_ia32));
1749 memcpy(&(swp->f16) + i*2, &(num128[0]), sizeof(unsigned long));
1750 memcpy(&(swp->f17) + i*2, &(num128[1]), sizeof(unsigned long));
1752 return 0;
1755 asmlinkage long
1756 sys32_ptrace (int request, pid_t pid, unsigned int addr, unsigned int data)
1758 struct task_struct *child;
1759 unsigned int value, tmp;
1760 long i, ret;
1762 lock_kernel();
1763 if (request == PTRACE_TRACEME) {
1764 ret = ptrace_traceme();
1765 goto out;
1768 child = ptrace_get_task_struct(pid);
1769 if (IS_ERR(child)) {
1770 ret = PTR_ERR(child);
1771 goto out;
1774 if (request == PTRACE_ATTACH) {
1775 ret = sys_ptrace(request, pid, addr, data);
1776 goto out_tsk;
1779 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1780 if (ret < 0)
1781 goto out_tsk;
1783 switch (request) {
1784 case PTRACE_PEEKTEXT:
1785 case PTRACE_PEEKDATA: /* read word at location addr */
1786 ret = ia32_peek(child, addr, &value);
1787 if (ret == 0)
1788 ret = put_user(value, (unsigned int __user *) compat_ptr(data));
1789 else
1790 ret = -EIO;
1791 goto out_tsk;
1793 case PTRACE_POKETEXT:
1794 case PTRACE_POKEDATA: /* write the word at location addr */
1795 ret = ia32_poke(child, addr, data);
1796 goto out_tsk;
1798 case PTRACE_PEEKUSR: /* read word at addr in USER area */
1799 ret = -EIO;
1800 if ((addr & 3) || addr > 17*sizeof(int))
1801 break;
1803 tmp = getreg(child, addr);
1804 if (!put_user(tmp, (unsigned int __user *) compat_ptr(data)))
1805 ret = 0;
1806 break;
1808 case PTRACE_POKEUSR: /* write word at addr in USER area */
1809 ret = -EIO;
1810 if ((addr & 3) || addr > 17*sizeof(int))
1811 break;
1813 putreg(child, addr, data);
1814 ret = 0;
1815 break;
1817 case IA32_PTRACE_GETREGS:
1818 if (!access_ok(VERIFY_WRITE, compat_ptr(data), 17*sizeof(int))) {
1819 ret = -EIO;
1820 break;
1822 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1823 put_user(getreg(child, i), (unsigned int __user *) compat_ptr(data));
1824 data += sizeof(int);
1826 ret = 0;
1827 break;
1829 case IA32_PTRACE_SETREGS:
1830 if (!access_ok(VERIFY_READ, compat_ptr(data), 17*sizeof(int))) {
1831 ret = -EIO;
1832 break;
1834 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1835 get_user(tmp, (unsigned int __user *) compat_ptr(data));
1836 putreg(child, i, tmp);
1837 data += sizeof(int);
1839 ret = 0;
1840 break;
1842 case IA32_PTRACE_GETFPREGS:
1843 ret = save_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1844 compat_ptr(data));
1845 break;
1847 case IA32_PTRACE_GETFPXREGS:
1848 ret = save_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1849 compat_ptr(data));
1850 break;
1852 case IA32_PTRACE_SETFPREGS:
1853 ret = restore_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1854 compat_ptr(data));
1855 break;
1857 case IA32_PTRACE_SETFPXREGS:
1858 ret = restore_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1859 compat_ptr(data));
1860 break;
1862 case PTRACE_GETEVENTMSG:
1863 ret = put_user(child->ptrace_message, (unsigned int __user *) compat_ptr(data));
1864 break;
1866 case PTRACE_SYSCALL: /* continue, stop after next syscall */
1867 case PTRACE_CONT: /* restart after signal. */
1868 case PTRACE_KILL:
1869 case PTRACE_SINGLESTEP: /* execute chile for one instruction */
1870 case PTRACE_DETACH: /* detach a process */
1871 ret = sys_ptrace(request, pid, addr, data);
1872 break;
1874 default:
1875 ret = ptrace_request(child, request, addr, data);
1876 break;
1879 out_tsk:
1880 put_task_struct(child);
1881 out:
1882 unlock_kernel();
1883 return ret;
1886 typedef struct {
1887 unsigned int ss_sp;
1888 unsigned int ss_flags;
1889 unsigned int ss_size;
1890 } ia32_stack_t;
1892 asmlinkage long
1893 sys32_sigaltstack (ia32_stack_t __user *uss32, ia32_stack_t __user *uoss32,
1894 long arg2, long arg3, long arg4, long arg5, long arg6,
1895 long arg7, struct pt_regs pt)
1897 stack_t uss, uoss;
1898 ia32_stack_t buf32;
1899 int ret;
1900 mm_segment_t old_fs = get_fs();
1902 if (uss32) {
1903 if (copy_from_user(&buf32, uss32, sizeof(ia32_stack_t)))
1904 return -EFAULT;
1905 uss.ss_sp = (void __user *) (long) buf32.ss_sp;
1906 uss.ss_flags = buf32.ss_flags;
1907 /* MINSIGSTKSZ is different for ia32 vs ia64. We lie here to pass the
1908 check and set it to the user requested value later */
1909 if ((buf32.ss_flags != SS_DISABLE) && (buf32.ss_size < MINSIGSTKSZ_IA32)) {
1910 ret = -ENOMEM;
1911 goto out;
1913 uss.ss_size = MINSIGSTKSZ;
1915 set_fs(KERNEL_DS);
1916 ret = do_sigaltstack(uss32 ? (stack_t __user *) &uss : NULL,
1917 (stack_t __user *) &uoss, pt.r12);
1918 current->sas_ss_size = buf32.ss_size;
1919 set_fs(old_fs);
1920 out:
1921 if (ret < 0)
1922 return(ret);
1923 if (uoss32) {
1924 buf32.ss_sp = (long __user) uoss.ss_sp;
1925 buf32.ss_flags = uoss.ss_flags;
1926 buf32.ss_size = uoss.ss_size;
1927 if (copy_to_user(uoss32, &buf32, sizeof(ia32_stack_t)))
1928 return -EFAULT;
1930 return ret;
1933 asmlinkage int
1934 sys32_pause (void)
1936 current->state = TASK_INTERRUPTIBLE;
1937 schedule();
1938 return -ERESTARTNOHAND;
1941 asmlinkage int
1942 sys32_msync (unsigned int start, unsigned int len, int flags)
1944 unsigned int addr;
1946 if (OFFSET4K(start))
1947 return -EINVAL;
1948 addr = PAGE_START(start);
1949 return sys_msync(addr, len + (start - addr), flags);
1952 struct sysctl32 {
1953 unsigned int name;
1954 int nlen;
1955 unsigned int oldval;
1956 unsigned int oldlenp;
1957 unsigned int newval;
1958 unsigned int newlen;
1959 unsigned int __unused[4];
1962 #ifdef CONFIG_SYSCTL_SYSCALL
1963 asmlinkage long
1964 sys32_sysctl (struct sysctl32 __user *args)
1966 struct sysctl32 a32;
1967 mm_segment_t old_fs = get_fs ();
1968 void __user *oldvalp, *newvalp;
1969 size_t oldlen;
1970 int __user *namep;
1971 long ret;
1973 if (copy_from_user(&a32, args, sizeof(a32)))
1974 return -EFAULT;
1977 * We need to pre-validate these because we have to disable address checking
1978 * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
1979 * user specifying bad addresses here. Well, since we're dealing with 32 bit
1980 * addresses, we KNOW that access_ok() will always succeed, so this is an
1981 * expensive NOP, but so what...
1983 namep = (int __user *) compat_ptr(a32.name);
1984 oldvalp = compat_ptr(a32.oldval);
1985 newvalp = compat_ptr(a32.newval);
1987 if ((oldvalp && get_user(oldlen, (int __user *) compat_ptr(a32.oldlenp)))
1988 || !access_ok(VERIFY_WRITE, namep, 0)
1989 || !access_ok(VERIFY_WRITE, oldvalp, 0)
1990 || !access_ok(VERIFY_WRITE, newvalp, 0))
1991 return -EFAULT;
1993 set_fs(KERNEL_DS);
1994 lock_kernel();
1995 ret = do_sysctl(namep, a32.nlen, oldvalp, (size_t __user *) &oldlen,
1996 newvalp, (size_t) a32.newlen);
1997 unlock_kernel();
1998 set_fs(old_fs);
2000 if (oldvalp && put_user (oldlen, (int __user *) compat_ptr(a32.oldlenp)))
2001 return -EFAULT;
2003 return ret;
2005 #endif
2007 asmlinkage long
2008 sys32_newuname (struct new_utsname __user *name)
2010 int ret = sys_newuname(name);
2012 if (!ret)
2013 if (copy_to_user(name->machine, "i686\0\0\0", 8))
2014 ret = -EFAULT;
2015 return ret;
2018 asmlinkage long
2019 sys32_getresuid16 (u16 __user *ruid, u16 __user *euid, u16 __user *suid)
2021 uid_t a, b, c;
2022 int ret;
2023 mm_segment_t old_fs = get_fs();
2025 set_fs(KERNEL_DS);
2026 ret = sys_getresuid((uid_t __user *) &a, (uid_t __user *) &b, (uid_t __user *) &c);
2027 set_fs(old_fs);
2029 if (put_user(a, ruid) || put_user(b, euid) || put_user(c, suid))
2030 return -EFAULT;
2031 return ret;
2034 asmlinkage long
2035 sys32_getresgid16 (u16 __user *rgid, u16 __user *egid, u16 __user *sgid)
2037 gid_t a, b, c;
2038 int ret;
2039 mm_segment_t old_fs = get_fs();
2041 set_fs(KERNEL_DS);
2042 ret = sys_getresgid((gid_t __user *) &a, (gid_t __user *) &b, (gid_t __user *) &c);
2043 set_fs(old_fs);
2045 if (ret)
2046 return ret;
2048 return put_user(a, rgid) | put_user(b, egid) | put_user(c, sgid);
2051 asmlinkage long
2052 sys32_lseek (unsigned int fd, int offset, unsigned int whence)
2054 /* Sign-extension of "offset" is important here... */
2055 return sys_lseek(fd, offset, whence);
2058 static int
2059 groups16_to_user(short __user *grouplist, struct group_info *group_info)
2061 int i;
2062 short group;
2064 for (i = 0; i < group_info->ngroups; i++) {
2065 group = (short)GROUP_AT(group_info, i);
2066 if (put_user(group, grouplist+i))
2067 return -EFAULT;
2070 return 0;
2073 static int
2074 groups16_from_user(struct group_info *group_info, short __user *grouplist)
2076 int i;
2077 short group;
2079 for (i = 0; i < group_info->ngroups; i++) {
2080 if (get_user(group, grouplist+i))
2081 return -EFAULT;
2082 GROUP_AT(group_info, i) = (gid_t)group;
2085 return 0;
2088 asmlinkage long
2089 sys32_getgroups16 (int gidsetsize, short __user *grouplist)
2091 int i;
2093 if (gidsetsize < 0)
2094 return -EINVAL;
2096 get_group_info(current->group_info);
2097 i = current->group_info->ngroups;
2098 if (gidsetsize) {
2099 if (i > gidsetsize) {
2100 i = -EINVAL;
2101 goto out;
2103 if (groups16_to_user(grouplist, current->group_info)) {
2104 i = -EFAULT;
2105 goto out;
2108 out:
2109 put_group_info(current->group_info);
2110 return i;
2113 asmlinkage long
2114 sys32_setgroups16 (int gidsetsize, short __user *grouplist)
2116 struct group_info *group_info;
2117 int retval;
2119 if (!capable(CAP_SETGID))
2120 return -EPERM;
2121 if ((unsigned)gidsetsize > NGROUPS_MAX)
2122 return -EINVAL;
2124 group_info = groups_alloc(gidsetsize);
2125 if (!group_info)
2126 return -ENOMEM;
2127 retval = groups16_from_user(group_info, grouplist);
2128 if (retval) {
2129 put_group_info(group_info);
2130 return retval;
2133 retval = set_current_groups(group_info);
2134 put_group_info(group_info);
2136 return retval;
2139 asmlinkage long
2140 sys32_truncate64 (unsigned int path, unsigned int len_lo, unsigned int len_hi)
2142 return sys_truncate(compat_ptr(path), ((unsigned long) len_hi << 32) | len_lo);
2145 asmlinkage long
2146 sys32_ftruncate64 (int fd, unsigned int len_lo, unsigned int len_hi)
2148 return sys_ftruncate(fd, ((unsigned long) len_hi << 32) | len_lo);
2151 static int
2152 putstat64 (struct stat64 __user *ubuf, struct kstat *kbuf)
2154 int err;
2155 u64 hdev;
2157 if (clear_user(ubuf, sizeof(*ubuf)))
2158 return -EFAULT;
2160 hdev = huge_encode_dev(kbuf->dev);
2161 err = __put_user(hdev, (u32 __user*)&ubuf->st_dev);
2162 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_dev) + 1);
2163 err |= __put_user(kbuf->ino, &ubuf->__st_ino);
2164 err |= __put_user(kbuf->ino, &ubuf->st_ino_lo);
2165 err |= __put_user(kbuf->ino >> 32, &ubuf->st_ino_hi);
2166 err |= __put_user(kbuf->mode, &ubuf->st_mode);
2167 err |= __put_user(kbuf->nlink, &ubuf->st_nlink);
2168 err |= __put_user(kbuf->uid, &ubuf->st_uid);
2169 err |= __put_user(kbuf->gid, &ubuf->st_gid);
2170 hdev = huge_encode_dev(kbuf->rdev);
2171 err = __put_user(hdev, (u32 __user*)&ubuf->st_rdev);
2172 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_rdev) + 1);
2173 err |= __put_user(kbuf->size, &ubuf->st_size_lo);
2174 err |= __put_user((kbuf->size >> 32), &ubuf->st_size_hi);
2175 err |= __put_user(kbuf->atime.tv_sec, &ubuf->st_atime);
2176 err |= __put_user(kbuf->atime.tv_nsec, &ubuf->st_atime_nsec);
2177 err |= __put_user(kbuf->mtime.tv_sec, &ubuf->st_mtime);
2178 err |= __put_user(kbuf->mtime.tv_nsec, &ubuf->st_mtime_nsec);
2179 err |= __put_user(kbuf->ctime.tv_sec, &ubuf->st_ctime);
2180 err |= __put_user(kbuf->ctime.tv_nsec, &ubuf->st_ctime_nsec);
2181 err |= __put_user(kbuf->blksize, &ubuf->st_blksize);
2182 err |= __put_user(kbuf->blocks, &ubuf->st_blocks);
2183 return err;
2186 asmlinkage long
2187 sys32_stat64 (char __user *filename, struct stat64 __user *statbuf)
2189 struct kstat s;
2190 long ret = vfs_stat(filename, &s);
2191 if (!ret)
2192 ret = putstat64(statbuf, &s);
2193 return ret;
2196 asmlinkage long
2197 sys32_lstat64 (char __user *filename, struct stat64 __user *statbuf)
2199 struct kstat s;
2200 long ret = vfs_lstat(filename, &s);
2201 if (!ret)
2202 ret = putstat64(statbuf, &s);
2203 return ret;
2206 asmlinkage long
2207 sys32_fstat64 (unsigned int fd, struct stat64 __user *statbuf)
2209 struct kstat s;
2210 long ret = vfs_fstat(fd, &s);
2211 if (!ret)
2212 ret = putstat64(statbuf, &s);
2213 return ret;
2216 asmlinkage long
2217 sys32_sched_rr_get_interval (pid_t pid, struct compat_timespec __user *interval)
2219 mm_segment_t old_fs = get_fs();
2220 struct timespec t;
2221 long ret;
2223 set_fs(KERNEL_DS);
2224 ret = sys_sched_rr_get_interval(pid, (struct timespec __user *) &t);
2225 set_fs(old_fs);
2226 if (put_compat_timespec(&t, interval))
2227 return -EFAULT;
2228 return ret;
2231 asmlinkage long
2232 sys32_pread (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2234 return sys_pread64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2237 asmlinkage long
2238 sys32_pwrite (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2240 return sys_pwrite64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2243 asmlinkage long
2244 sys32_sendfile (int out_fd, int in_fd, int __user *offset, unsigned int count)
2246 mm_segment_t old_fs = get_fs();
2247 long ret;
2248 off_t of;
2250 if (offset && get_user(of, offset))
2251 return -EFAULT;
2253 set_fs(KERNEL_DS);
2254 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *) &of : NULL, count);
2255 set_fs(old_fs);
2257 if (offset && put_user(of, offset))
2258 return -EFAULT;
2260 return ret;
2263 asmlinkage long
2264 sys32_personality (unsigned int personality)
2266 long ret;
2268 if (current->personality == PER_LINUX32 && personality == PER_LINUX)
2269 personality = PER_LINUX32;
2270 ret = sys_personality(personality);
2271 if (ret == PER_LINUX32)
2272 ret = PER_LINUX;
2273 return ret;
2276 asmlinkage unsigned long
2277 sys32_brk (unsigned int brk)
2279 unsigned long ret, obrk;
2280 struct mm_struct *mm = current->mm;
2282 obrk = mm->brk;
2283 ret = sys_brk(brk);
2284 if (ret < obrk)
2285 clear_user(compat_ptr(ret), PAGE_ALIGN(ret) - ret);
2286 return ret;
2289 /* Structure for ia32 emulation on ia64 */
2290 struct epoll_event32
2292 u32 events;
2293 u32 data[2];
2296 asmlinkage long
2297 sys32_epoll_ctl(int epfd, int op, int fd, struct epoll_event32 __user *event)
2299 mm_segment_t old_fs = get_fs();
2300 struct epoll_event event64;
2301 int error;
2302 u32 data_halfword;
2304 if (!access_ok(VERIFY_READ, event, sizeof(struct epoll_event32)))
2305 return -EFAULT;
2307 __get_user(event64.events, &event->events);
2308 __get_user(data_halfword, &event->data[0]);
2309 event64.data = data_halfword;
2310 __get_user(data_halfword, &event->data[1]);
2311 event64.data |= (u64)data_halfword << 32;
2313 set_fs(KERNEL_DS);
2314 error = sys_epoll_ctl(epfd, op, fd, (struct epoll_event __user *) &event64);
2315 set_fs(old_fs);
2317 return error;
2320 asmlinkage long
2321 sys32_epoll_wait(int epfd, struct epoll_event32 __user * events, int maxevents,
2322 int timeout)
2324 struct epoll_event *events64 = NULL;
2325 mm_segment_t old_fs = get_fs();
2326 int numevents, size;
2327 int evt_idx;
2328 int do_free_pages = 0;
2330 if (maxevents <= 0) {
2331 return -EINVAL;
2334 /* Verify that the area passed by the user is writeable */
2335 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event32)))
2336 return -EFAULT;
2339 * Allocate space for the intermediate copy. If the space needed
2340 * is large enough to cause kmalloc to fail, then try again with
2341 * __get_free_pages.
2343 size = maxevents * sizeof(struct epoll_event);
2344 events64 = kmalloc(size, GFP_KERNEL);
2345 if (events64 == NULL) {
2346 events64 = (struct epoll_event *)
2347 __get_free_pages(GFP_KERNEL, get_order(size));
2348 if (events64 == NULL)
2349 return -ENOMEM;
2350 do_free_pages = 1;
2353 /* Do the system call */
2354 set_fs(KERNEL_DS); /* copy_to/from_user should work on kernel mem*/
2355 numevents = sys_epoll_wait(epfd, (struct epoll_event __user *) events64,
2356 maxevents, timeout);
2357 set_fs(old_fs);
2359 /* Don't modify userspace memory if we're returning an error */
2360 if (numevents > 0) {
2361 /* Translate the 64-bit structures back into the 32-bit
2362 structures */
2363 for (evt_idx = 0; evt_idx < numevents; evt_idx++) {
2364 __put_user(events64[evt_idx].events,
2365 &events[evt_idx].events);
2366 __put_user((u32)events64[evt_idx].data,
2367 &events[evt_idx].data[0]);
2368 __put_user((u32)(events64[evt_idx].data >> 32),
2369 &events[evt_idx].data[1]);
2373 if (do_free_pages)
2374 free_pages((unsigned long) events64, get_order(size));
2375 else
2376 kfree(events64);
2377 return numevents;
2381 * Get a yet unused TLS descriptor index.
2383 static int
2384 get_free_idx (void)
2386 struct thread_struct *t = &current->thread;
2387 int idx;
2389 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
2390 if (desc_empty(t->tls_array + idx))
2391 return idx + GDT_ENTRY_TLS_MIN;
2392 return -ESRCH;
2396 * Set a given TLS descriptor:
2398 asmlinkage int
2399 sys32_set_thread_area (struct ia32_user_desc __user *u_info)
2401 struct thread_struct *t = &current->thread;
2402 struct ia32_user_desc info;
2403 struct desc_struct *desc;
2404 int cpu, idx;
2406 if (copy_from_user(&info, u_info, sizeof(info)))
2407 return -EFAULT;
2408 idx = info.entry_number;
2411 * index -1 means the kernel should try to find and allocate an empty descriptor:
2413 if (idx == -1) {
2414 idx = get_free_idx();
2415 if (idx < 0)
2416 return idx;
2417 if (put_user(idx, &u_info->entry_number))
2418 return -EFAULT;
2421 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2422 return -EINVAL;
2424 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
2426 cpu = smp_processor_id();
2428 if (LDT_empty(&info)) {
2429 desc->a = 0;
2430 desc->b = 0;
2431 } else {
2432 desc->a = LDT_entry_a(&info);
2433 desc->b = LDT_entry_b(&info);
2435 load_TLS(t, cpu);
2436 return 0;
2440 * Get the current Thread-Local Storage area:
2443 #define GET_BASE(desc) ( \
2444 (((desc)->a >> 16) & 0x0000ffff) | \
2445 (((desc)->b << 16) & 0x00ff0000) | \
2446 ( (desc)->b & 0xff000000) )
2448 #define GET_LIMIT(desc) ( \
2449 ((desc)->a & 0x0ffff) | \
2450 ((desc)->b & 0xf0000) )
2452 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
2453 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
2454 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
2455 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
2456 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
2457 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
2459 asmlinkage int
2460 sys32_get_thread_area (struct ia32_user_desc __user *u_info)
2462 struct ia32_user_desc info;
2463 struct desc_struct *desc;
2464 int idx;
2466 if (get_user(idx, &u_info->entry_number))
2467 return -EFAULT;
2468 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2469 return -EINVAL;
2471 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
2473 info.entry_number = idx;
2474 info.base_addr = GET_BASE(desc);
2475 info.limit = GET_LIMIT(desc);
2476 info.seg_32bit = GET_32BIT(desc);
2477 info.contents = GET_CONTENTS(desc);
2478 info.read_exec_only = !GET_WRITABLE(desc);
2479 info.limit_in_pages = GET_LIMIT_PAGES(desc);
2480 info.seg_not_present = !GET_PRESENT(desc);
2481 info.useable = GET_USEABLE(desc);
2483 if (copy_to_user(u_info, &info, sizeof(info)))
2484 return -EFAULT;
2485 return 0;
2488 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
2489 __u32 len_low, __u32 len_high, int advice)
2491 return sys_fadvise64_64(fd,
2492 (((u64)offset_high)<<32) | offset_low,
2493 (((u64)len_high)<<32) | len_low,
2494 advice);
2497 #ifdef NOTYET /* UNTESTED FOR IA64 FROM HERE DOWN */
2499 asmlinkage long sys32_setreuid(compat_uid_t ruid, compat_uid_t euid)
2501 uid_t sruid, seuid;
2503 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2504 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2505 return sys_setreuid(sruid, seuid);
2508 asmlinkage long
2509 sys32_setresuid(compat_uid_t ruid, compat_uid_t euid,
2510 compat_uid_t suid)
2512 uid_t sruid, seuid, ssuid;
2514 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2515 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2516 ssuid = (suid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)suid);
2517 return sys_setresuid(sruid, seuid, ssuid);
2520 asmlinkage long
2521 sys32_setregid(compat_gid_t rgid, compat_gid_t egid)
2523 gid_t srgid, segid;
2525 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2526 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2527 return sys_setregid(srgid, segid);
2530 asmlinkage long
2531 sys32_setresgid(compat_gid_t rgid, compat_gid_t egid,
2532 compat_gid_t sgid)
2534 gid_t srgid, segid, ssgid;
2536 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2537 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2538 ssgid = (sgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)sgid);
2539 return sys_setresgid(srgid, segid, ssgid);
2541 #endif /* NOTYET */