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
17 #include <linux/kernel.h>
18 #include <linux/syscalls.h>
19 #include <linux/sysctl.h>
20 #include <linux/sched.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>
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/syscalls.h>
38 #include <linux/sunrpc/svc.h>
39 #include <linux/nfsd/nfsd.h>
40 #include <linux/nfsd/cache.h>
41 #include <linux/nfsd/xdr.h>
42 #include <linux/nfsd/syscall.h>
43 #include <linux/poll.h>
44 #include <linux/eventpoll.h>
45 #include <linux/personality.h>
46 #include <linux/ptrace.h>
47 #include <linux/stat.h>
48 #include <linux/ipc.h>
49 #include <linux/capability.h>
50 #include <linux/compat.h>
51 #include <linux/vfs.h>
52 #include <linux/mman.h>
53 #include <linux/mutex.h>
55 #include <asm/intrinsics.h>
56 #include <asm/types.h>
57 #include <asm/uaccess.h>
58 #include <asm/unistd.h>
68 # define DBG(fmt...) printk(KERN_DEBUG fmt)
73 #define ROUND_UP(x,a) ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
75 #define OFFSET4K(a) ((a) & 0xfff)
76 #define PAGE_START(addr) ((addr) & PAGE_MASK)
77 #define MINSIGSTKSZ_IA32 2048
79 #define high2lowuid(uid) ((uid) > 65535 ? 65534 : (uid))
80 #define high2lowgid(gid) ((gid) > 65535 ? 65534 : (gid))
83 * Anything that modifies or inspects ia32 user virtual memory must hold this semaphore
86 /* XXX make per-mm: */
87 static DEFINE_MUTEX(ia32_mmap_mutex);
90 sys32_execve (char __user *name, compat_uptr_t __user *argv, compat_uptr_t __user *envp,
95 unsigned long old_map_base, old_task_size, tssd;
97 filename = getname(name);
98 error = PTR_ERR(filename);
102 old_map_base = current->thread.map_base;
103 old_task_size = current->thread.task_size;
104 tssd = ia64_get_kr(IA64_KR_TSSD);
106 /* we may be exec'ing a 64-bit process: reset map base, task-size, and io-base: */
107 current->thread.map_base = DEFAULT_MAP_BASE;
108 current->thread.task_size = DEFAULT_TASK_SIZE;
109 ia64_set_kr(IA64_KR_IO_BASE, current->thread.old_iob);
110 ia64_set_kr(IA64_KR_TSSD, current->thread.old_k1);
112 error = compat_do_execve(filename, argv, envp, regs);
116 /* oops, execve failed, switch back to old values... */
117 ia64_set_kr(IA64_KR_IO_BASE, IA32_IOBASE);
118 ia64_set_kr(IA64_KR_TSSD, tssd);
119 current->thread.map_base = old_map_base;
120 current->thread.task_size = old_task_size;
126 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
131 if ((u64) stat->size > MAX_NON_LFS ||
132 !old_valid_dev(stat->dev) ||
133 !old_valid_dev(stat->rdev))
137 if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
140 if (clear_user(ubuf, sizeof(*ubuf)))
143 err = __put_user(old_encode_dev(stat->dev), &ubuf->st_dev);
144 err |= __put_user(ino, &ubuf->st_ino);
145 err |= __put_user(stat->mode, &ubuf->st_mode);
146 err |= __put_user(stat->nlink, &ubuf->st_nlink);
147 err |= __put_user(high2lowuid(stat->uid), &ubuf->st_uid);
148 err |= __put_user(high2lowgid(stat->gid), &ubuf->st_gid);
149 err |= __put_user(old_encode_dev(stat->rdev), &ubuf->st_rdev);
150 err |= __put_user(stat->size, &ubuf->st_size);
151 err |= __put_user(stat->atime.tv_sec, &ubuf->st_atime);
152 err |= __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec);
153 err |= __put_user(stat->mtime.tv_sec, &ubuf->st_mtime);
154 err |= __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec);
155 err |= __put_user(stat->ctime.tv_sec, &ubuf->st_ctime);
156 err |= __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec);
157 err |= __put_user(stat->blksize, &ubuf->st_blksize);
158 err |= __put_user(stat->blocks, &ubuf->st_blocks);
162 #if PAGE_SHIFT > IA32_PAGE_SHIFT
166 get_page_prot (struct vm_area_struct *vma, unsigned long addr)
170 if (!vma || vma->vm_start > addr)
173 if (vma->vm_flags & VM_READ)
175 if (vma->vm_flags & VM_WRITE)
177 if (vma->vm_flags & VM_EXEC)
183 * Map a subpage by creating an anonymous page that contains the union of the old page and
187 mmap_subpage (struct file *file, unsigned long start, unsigned long end, int prot, int flags,
192 unsigned long ret = 0;
193 struct vm_area_struct *vma = find_vma(current->mm, start);
194 int old_prot = get_page_prot(vma, start);
196 DBG("mmap_subpage(file=%p,start=0x%lx,end=0x%lx,prot=%x,flags=%x,off=0x%llx)\n",
197 file, start, end, prot, flags, off);
200 /* Optimize the case where the old mmap and the new mmap are both anonymous */
201 if ((old_prot & PROT_WRITE) && (flags & MAP_ANONYMOUS) && !vma->vm_file) {
202 if (clear_user((void __user *) start, end - start)) {
209 page = (void *) get_zeroed_page(GFP_KERNEL);
214 copy_from_user(page, (void __user *) PAGE_START(start), PAGE_SIZE);
216 down_write(¤t->mm->mmap_sem);
218 ret = do_mmap(NULL, PAGE_START(start), PAGE_SIZE, prot | PROT_WRITE,
219 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
221 up_write(¤t->mm->mmap_sem);
223 if (IS_ERR((void *) ret))
227 /* copy back the old page contents. */
228 if (offset_in_page(start))
229 copy_to_user((void __user *) PAGE_START(start), page,
230 offset_in_page(start));
231 if (offset_in_page(end))
232 copy_to_user((void __user *) end, page + offset_in_page(end),
233 PAGE_SIZE - offset_in_page(end));
236 if (!(flags & MAP_ANONYMOUS)) {
237 /* read the file contents */
238 inode = file->f_path.dentry->d_inode;
239 if (!inode->i_fop || !file->f_op->read
240 || ((*file->f_op->read)(file, (char __user *) start, end - start, &off) < 0))
248 if (!(prot & PROT_WRITE))
249 ret = sys_mprotect(PAGE_START(start), PAGE_SIZE, prot | old_prot);
252 free_page((unsigned long) page);
256 /* SLAB cache for ia64_partial_page structures */
257 struct kmem_cache *ia64_partial_page_cachep;
260 * init ia64_partial_page_list.
261 * return 0 means kmalloc fail.
263 struct ia64_partial_page_list*
264 ia32_init_pp_list(void)
266 struct ia64_partial_page_list *p;
268 if ((p = kmalloc(sizeof(*p), GFP_KERNEL)) == NULL)
273 atomic_set(&p->pp_count, 1);
278 * Search for the partial page with @start in partial page list @ppl.
279 * If finds the partial page, return the found partial page.
280 * Else, return 0 and provide @pprev, @rb_link, @rb_parent to
281 * be used by later __ia32_insert_pp().
283 static struct ia64_partial_page *
284 __ia32_find_pp(struct ia64_partial_page_list *ppl, unsigned int start,
285 struct ia64_partial_page **pprev, struct rb_node ***rb_link,
286 struct rb_node **rb_parent)
288 struct ia64_partial_page *pp;
289 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
292 if (pp && pp->base == start)
295 __rb_link = &ppl->ppl_rb.rb_node;
296 rb_prev = __rb_parent = NULL;
299 __rb_parent = *__rb_link;
300 pp = rb_entry(__rb_parent, struct ia64_partial_page, pp_rb);
302 if (pp->base == start) {
305 } else if (pp->base < start) {
306 rb_prev = __rb_parent;
307 __rb_link = &__rb_parent->rb_right;
309 __rb_link = &__rb_parent->rb_left;
313 *rb_link = __rb_link;
314 *rb_parent = __rb_parent;
317 *pprev = rb_entry(rb_prev, struct ia64_partial_page, pp_rb);
322 * insert @pp into @ppl.
325 __ia32_insert_pp(struct ia64_partial_page_list *ppl,
326 struct ia64_partial_page *pp, struct ia64_partial_page *prev,
327 struct rb_node **rb_link, struct rb_node *rb_parent)
331 pp->next = prev->next;
336 pp->next = rb_entry(rb_parent,
337 struct ia64_partial_page, pp_rb);
343 rb_link_node(&pp->pp_rb, rb_parent, rb_link);
344 rb_insert_color(&pp->pp_rb, &ppl->ppl_rb);
350 * delete @pp from partial page list @ppl.
353 __ia32_delete_pp(struct ia64_partial_page_list *ppl,
354 struct ia64_partial_page *pp, struct ia64_partial_page *prev)
357 prev->next = pp->next;
358 if (ppl->pp_hint == pp)
361 ppl->pp_head = pp->next;
362 if (ppl->pp_hint == pp)
363 ppl->pp_hint = pp->next;
365 rb_erase(&pp->pp_rb, &ppl->ppl_rb);
366 kmem_cache_free(ia64_partial_page_cachep, pp);
369 static struct ia64_partial_page *
370 __pp_prev(struct ia64_partial_page *pp)
372 struct rb_node *prev = rb_prev(&pp->pp_rb);
374 return rb_entry(prev, struct ia64_partial_page, pp_rb);
380 * Delete partial pages with address between @start and @end.
381 * @start and @end are page aligned.
384 __ia32_delete_pp_range(unsigned int start, unsigned int end)
386 struct ia64_partial_page *pp, *prev;
387 struct rb_node **rb_link, *rb_parent;
392 pp = __ia32_find_pp(current->thread.ppl, start, &prev,
393 &rb_link, &rb_parent);
395 prev = __pp_prev(pp);
400 pp = current->thread.ppl->pp_head;
403 while (pp && pp->base < end) {
404 struct ia64_partial_page *tmp = pp->next;
405 __ia32_delete_pp(current->thread.ppl, pp, prev);
411 * Set the range between @start and @end in bitmap.
412 * @start and @end should be IA32 page aligned and in the same IA64 page.
415 __ia32_set_pp(unsigned int start, unsigned int end, int flags)
417 struct ia64_partial_page *pp, *prev;
418 struct rb_node ** rb_link, *rb_parent;
419 unsigned int pstart, start_bit, end_bit, i;
421 pstart = PAGE_START(start);
422 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
423 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
425 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
426 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
427 &rb_link, &rb_parent);
429 for (i = start_bit; i < end_bit; i++)
430 set_bit(i, &pp->bitmap);
432 * Check: if this partial page has been set to a full page,
435 if (find_first_zero_bit(&pp->bitmap, sizeof(pp->bitmap)*8) >=
436 PAGE_SIZE/IA32_PAGE_SIZE) {
437 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
443 * MAP_FIXED may lead to overlapping mmap.
444 * In this case, the requested mmap area may already mmaped as a full
445 * page. So check vma before adding a new partial page.
447 if (flags & MAP_FIXED) {
448 struct vm_area_struct *vma = find_vma(current->mm, pstart);
449 if (vma && vma->vm_start <= pstart)
453 /* new a ia64_partial_page */
454 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
459 for (i=start_bit; i<end_bit; i++)
460 set_bit(i, &(pp->bitmap));
462 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
467 * @start and @end should be IA32 page aligned, but don't need to be in the
468 * same IA64 page. Split @start and @end to make sure they're in the same IA64
469 * page, then call __ia32_set_pp().
472 ia32_set_pp(unsigned int start, unsigned int end, int flags)
474 down_write(¤t->mm->mmap_sem);
475 if (flags & MAP_FIXED) {
477 * MAP_FIXED may lead to overlapping mmap. When this happens,
478 * a series of complete IA64 pages results in deletion of
479 * old partial pages in that range.
481 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
484 if (end < PAGE_ALIGN(start)) {
485 __ia32_set_pp(start, end, flags);
487 if (offset_in_page(start))
488 __ia32_set_pp(start, PAGE_ALIGN(start), flags);
489 if (offset_in_page(end))
490 __ia32_set_pp(PAGE_START(end), end, flags);
492 up_write(¤t->mm->mmap_sem);
496 * Unset the range between @start and @end in bitmap.
497 * @start and @end should be IA32 page aligned and in the same IA64 page.
498 * After doing that, if the bitmap is 0, then free the page and return 1,
500 * If not find the partial page in the list, then
501 * If the vma exists, then the full page is set to a partial page;
502 * Else return -ENOMEM.
505 __ia32_unset_pp(unsigned int start, unsigned int end)
507 struct ia64_partial_page *pp, *prev;
508 struct rb_node ** rb_link, *rb_parent;
509 unsigned int pstart, start_bit, end_bit, i;
510 struct vm_area_struct *vma;
512 pstart = PAGE_START(start);
513 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
514 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
516 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
518 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
519 &rb_link, &rb_parent);
521 for (i = start_bit; i < end_bit; i++)
522 clear_bit(i, &pp->bitmap);
523 if (pp->bitmap == 0) {
524 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
530 vma = find_vma(current->mm, pstart);
531 if (!vma || vma->vm_start > pstart) {
535 /* new a ia64_partial_page */
536 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
541 for (i = 0; i < start_bit; i++)
542 set_bit(i, &(pp->bitmap));
543 for (i = end_bit; i < PAGE_SIZE / IA32_PAGE_SIZE; i++)
544 set_bit(i, &(pp->bitmap));
546 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
551 * Delete pp between PAGE_ALIGN(start) and PAGE_START(end) by calling
552 * __ia32_delete_pp_range(). Unset possible partial pages by calling
554 * The returned value see __ia32_unset_pp().
557 ia32_unset_pp(unsigned int *startp, unsigned int *endp)
559 unsigned int start = *startp, end = *endp;
562 down_write(¤t->mm->mmap_sem);
564 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
566 if (end < PAGE_ALIGN(start)) {
567 ret = __ia32_unset_pp(start, end);
569 *startp = PAGE_START(start);
570 *endp = PAGE_ALIGN(end);
573 /* to shortcut sys_munmap() in sys32_munmap() */
574 *startp = PAGE_START(start);
575 *endp = PAGE_START(end);
578 if (offset_in_page(start)) {
579 ret = __ia32_unset_pp(start, PAGE_ALIGN(start));
581 *startp = PAGE_START(start);
583 *startp = PAGE_ALIGN(start);
587 if (offset_in_page(end)) {
588 ret = __ia32_unset_pp(PAGE_START(end), end);
590 *endp = PAGE_ALIGN(end);
592 *endp = PAGE_START(end);
597 up_write(¤t->mm->mmap_sem);
602 * Compare the range between @start and @end with bitmap in partial page.
603 * @start and @end should be IA32 page aligned and in the same IA64 page.
606 __ia32_compare_pp(unsigned int start, unsigned int end)
608 struct ia64_partial_page *pp, *prev;
609 struct rb_node ** rb_link, *rb_parent;
610 unsigned int pstart, start_bit, end_bit, size;
611 unsigned int first_bit, next_zero_bit; /* the first range in bitmap */
613 pstart = PAGE_START(start);
615 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
616 &rb_link, &rb_parent);
620 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
621 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
622 size = sizeof(pp->bitmap) * 8;
623 first_bit = find_first_bit(&pp->bitmap, size);
624 next_zero_bit = find_next_zero_bit(&pp->bitmap, size, first_bit);
625 if ((start_bit < first_bit) || (end_bit > next_zero_bit)) {
626 /* exceeds the first range in bitmap */
628 } else if ((start_bit == first_bit) && (end_bit == next_zero_bit)) {
629 first_bit = find_next_bit(&pp->bitmap, size, next_zero_bit);
630 if ((next_zero_bit < first_bit) && (first_bit < size))
631 return 1; /* has next range */
633 return 0; /* no next range */
639 * @start and @end should be IA32 page aligned, but don't need to be in the
640 * same IA64 page. Split @start and @end to make sure they're in the same IA64
641 * page, then call __ia32_compare_pp().
643 * Take this as example: the range is the 1st and 2nd 4K page.
644 * Return 0 if they fit bitmap exactly, i.e. bitmap = 00000011;
645 * Return 1 if the range doesn't cover whole bitmap, e.g. bitmap = 00001111;
646 * Return -ENOMEM if the range exceeds the bitmap, e.g. bitmap = 00000001 or
650 ia32_compare_pp(unsigned int *startp, unsigned int *endp)
652 unsigned int start = *startp, end = *endp;
655 down_write(¤t->mm->mmap_sem);
657 if (end < PAGE_ALIGN(start)) {
658 retval = __ia32_compare_pp(start, end);
660 *startp = PAGE_START(start);
661 *endp = PAGE_ALIGN(end);
664 if (offset_in_page(start)) {
665 retval = __ia32_compare_pp(start,
668 *startp = PAGE_START(start);
672 if (offset_in_page(end)) {
673 retval = __ia32_compare_pp(PAGE_START(end), end);
675 *endp = PAGE_ALIGN(end);
680 up_write(¤t->mm->mmap_sem);
685 __ia32_drop_pp_list(struct ia64_partial_page_list *ppl)
687 struct ia64_partial_page *pp = ppl->pp_head;
690 struct ia64_partial_page *next = pp->next;
691 kmem_cache_free(ia64_partial_page_cachep, pp);
699 ia32_drop_ia64_partial_page_list(struct task_struct *task)
701 struct ia64_partial_page_list* ppl = task->thread.ppl;
703 if (ppl && atomic_dec_and_test(&ppl->pp_count))
704 __ia32_drop_pp_list(ppl);
708 * Copy current->thread.ppl to ppl (already initialized).
711 __ia32_copy_pp_list(struct ia64_partial_page_list *ppl)
713 struct ia64_partial_page *pp, *tmp, *prev;
714 struct rb_node **rb_link, *rb_parent;
718 ppl->ppl_rb = RB_ROOT;
719 rb_link = &ppl->ppl_rb.rb_node;
723 for (pp = current->thread.ppl->pp_head; pp; pp = pp->next) {
724 tmp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
728 __ia32_insert_pp(ppl, tmp, prev, rb_link, rb_parent);
730 rb_link = &tmp->pp_rb.rb_right;
731 rb_parent = &tmp->pp_rb;
737 ia32_copy_ia64_partial_page_list(struct task_struct *p,
738 unsigned long clone_flags)
742 if (clone_flags & CLONE_VM) {
743 atomic_inc(¤t->thread.ppl->pp_count);
744 p->thread.ppl = current->thread.ppl;
746 p->thread.ppl = ia32_init_pp_list();
749 down_write(¤t->mm->mmap_sem);
751 retval = __ia32_copy_pp_list(p->thread.ppl);
753 up_write(¤t->mm->mmap_sem);
760 emulate_mmap (struct file *file, unsigned long start, unsigned long len, int prot, int flags,
763 unsigned long tmp, end, pend, pstart, ret, is_congruent, fudge = 0;
768 pstart = PAGE_START(start);
769 pend = PAGE_ALIGN(end);
771 if (flags & MAP_FIXED) {
772 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
773 if (start > pstart) {
774 if (flags & MAP_SHARED)
776 "%s(%d): emulate_mmap() can't share head (addr=0x%lx)\n",
777 current->comm, current->pid, start);
778 ret = mmap_subpage(file, start, min(PAGE_ALIGN(start), end), prot, flags,
780 if (IS_ERR((void *) ret))
787 if (flags & MAP_SHARED)
789 "%s(%d): emulate_mmap() can't share tail (end=0x%lx)\n",
790 current->comm, current->pid, end);
791 ret = mmap_subpage(file, max(start, PAGE_START(end)), end, prot, flags,
792 (off + len) - offset_in_page(end));
793 if (IS_ERR((void *) ret))
801 * If a start address was specified, use it if the entire rounded out area
804 if (start && !pstart)
805 fudge = 1; /* handle case of mapping to range (0,PAGE_SIZE) */
806 tmp = arch_get_unmapped_area(file, pstart - fudge, pend - pstart, 0, flags);
809 start = pstart + offset_in_page(off); /* make start congruent with off */
811 pend = PAGE_ALIGN(end);
815 poff = off + (pstart - start); /* note: (pstart - start) may be negative */
816 is_congruent = (flags & MAP_ANONYMOUS) || (offset_in_page(poff) == 0);
818 if ((flags & MAP_SHARED) && !is_congruent)
819 printk(KERN_INFO "%s(%d): emulate_mmap() can't share contents of incongruent mmap "
820 "(addr=0x%lx,off=0x%llx)\n", current->comm, current->pid, start, off);
822 DBG("mmap_body: mapping [0x%lx-0x%lx) %s with poff 0x%llx\n", pstart, pend,
823 is_congruent ? "congruent" : "not congruent", poff);
825 down_write(¤t->mm->mmap_sem);
827 if (!(flags & MAP_ANONYMOUS) && is_congruent)
828 ret = do_mmap(file, pstart, pend - pstart, prot, flags | MAP_FIXED, poff);
830 ret = do_mmap(NULL, pstart, pend - pstart,
831 prot | ((flags & MAP_ANONYMOUS) ? 0 : PROT_WRITE),
832 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
834 up_write(¤t->mm->mmap_sem);
836 if (IS_ERR((void *) ret))
840 /* read the file contents */
841 inode = file->f_path.dentry->d_inode;
842 if (!inode->i_fop || !file->f_op->read
843 || ((*file->f_op->read)(file, (char __user *) pstart, pend - pstart, &poff)
846 sys_munmap(pstart, pend - pstart);
849 if (!(prot & PROT_WRITE) && sys_mprotect(pstart, pend - pstart, prot) < 0)
853 if (!(flags & MAP_FIXED))
854 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
859 #endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
861 static inline unsigned int
862 get_prot32 (unsigned int prot)
864 if (prot & PROT_WRITE)
865 /* on x86, PROT_WRITE implies PROT_READ which implies PROT_EEC */
866 prot |= PROT_READ | PROT_WRITE | PROT_EXEC;
867 else if (prot & (PROT_READ | PROT_EXEC))
868 /* on x86, there is no distinction between PROT_READ and PROT_EXEC */
869 prot |= (PROT_READ | PROT_EXEC);
875 ia32_do_mmap (struct file *file, unsigned long addr, unsigned long len, int prot, int flags,
878 DBG("ia32_do_mmap(file=%p,addr=0x%lx,len=0x%lx,prot=%x,flags=%x,offset=0x%llx)\n",
879 file, addr, len, prot, flags, offset);
881 if (file && (!file->f_op || !file->f_op->mmap))
884 len = IA32_PAGE_ALIGN(len);
888 if (len > IA32_PAGE_OFFSET || addr > IA32_PAGE_OFFSET - len)
890 if (flags & MAP_FIXED)
896 if (OFFSET4K(offset))
899 prot = get_prot32(prot);
901 #if PAGE_SHIFT > IA32_PAGE_SHIFT
902 mutex_lock(&ia32_mmap_mutex);
904 addr = emulate_mmap(file, addr, len, prot, flags, offset);
906 mutex_unlock(&ia32_mmap_mutex);
908 down_write(¤t->mm->mmap_sem);
910 addr = do_mmap(file, addr, len, prot, flags, offset);
912 up_write(¤t->mm->mmap_sem);
914 DBG("ia32_do_mmap: returning 0x%lx\n", addr);
919 * Linux/i386 didn't use to be able to handle more than 4 system call parameters, so these
920 * system calls used a memory block for parameter passing..
923 struct mmap_arg_struct {
933 sys32_mmap (struct mmap_arg_struct __user *arg)
935 struct mmap_arg_struct a;
936 struct file *file = NULL;
940 if (copy_from_user(&a, arg, sizeof(a)))
943 if (OFFSET4K(a.offset))
948 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
949 if (!(flags & MAP_ANONYMOUS)) {
955 addr = ia32_do_mmap(file, a.addr, a.len, a.prot, flags, a.offset);
963 sys32_mmap2 (unsigned int addr, unsigned int len, unsigned int prot, unsigned int flags,
964 unsigned int fd, unsigned int pgoff)
966 struct file *file = NULL;
967 unsigned long retval;
969 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
970 if (!(flags & MAP_ANONYMOUS)) {
976 retval = ia32_do_mmap(file, addr, len, prot, flags,
977 (unsigned long) pgoff << IA32_PAGE_SHIFT);
985 sys32_munmap (unsigned int start, unsigned int len)
987 unsigned int end = start + len;
990 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
991 ret = sys_munmap(start, end - start);
996 end = IA32_PAGE_ALIGN(end);
1000 ret = ia32_unset_pp(&start, &end);
1007 mutex_lock(&ia32_mmap_mutex);
1008 ret = sys_munmap(start, end - start);
1009 mutex_unlock(&ia32_mmap_mutex);
1014 #if PAGE_SHIFT > IA32_PAGE_SHIFT
1017 * When mprotect()ing a partial page, we set the permission to the union of the old
1018 * settings and the new settings. In other words, it's only possible to make access to a
1019 * partial page less restrictive.
1022 mprotect_subpage (unsigned long address, int new_prot)
1025 struct vm_area_struct *vma;
1027 if (new_prot == PROT_NONE)
1028 return 0; /* optimize case where nothing changes... */
1029 vma = find_vma(current->mm, address);
1030 old_prot = get_page_prot(vma, address);
1031 return sys_mprotect(address, PAGE_SIZE, new_prot | old_prot);
1034 #endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
1037 sys32_mprotect (unsigned int start, unsigned int len, int prot)
1039 unsigned int end = start + len;
1040 #if PAGE_SHIFT > IA32_PAGE_SHIFT
1044 prot = get_prot32(prot);
1046 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
1047 return sys_mprotect(start, end - start, prot);
1049 if (OFFSET4K(start))
1052 end = IA32_PAGE_ALIGN(end);
1056 retval = ia32_compare_pp(&start, &end);
1061 mutex_lock(&ia32_mmap_mutex);
1063 if (offset_in_page(start)) {
1064 /* start address is 4KB aligned but not page aligned. */
1065 retval = mprotect_subpage(PAGE_START(start), prot);
1069 start = PAGE_ALIGN(start);
1071 goto out; /* retval is already zero... */
1074 if (offset_in_page(end)) {
1075 /* end address is 4KB aligned but not page aligned. */
1076 retval = mprotect_subpage(PAGE_START(end), prot);
1080 end = PAGE_START(end);
1082 retval = sys_mprotect(start, end - start, prot);
1085 mutex_unlock(&ia32_mmap_mutex);
1091 sys32_mremap (unsigned int addr, unsigned int old_len, unsigned int new_len,
1092 unsigned int flags, unsigned int new_addr)
1096 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
1097 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1099 unsigned int old_end, new_end;
1104 old_len = IA32_PAGE_ALIGN(old_len);
1105 new_len = IA32_PAGE_ALIGN(new_len);
1106 old_end = addr + old_len;
1107 new_end = addr + new_len;
1112 if ((flags & MREMAP_FIXED) && (OFFSET4K(new_addr)))
1115 if (old_len >= new_len) {
1116 ret = sys32_munmap(addr + new_len, old_len - new_len);
1117 if (ret && old_len != new_len)
1120 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
1125 addr = PAGE_START(addr);
1126 old_len = PAGE_ALIGN(old_end) - addr;
1127 new_len = PAGE_ALIGN(new_end) - addr;
1129 mutex_lock(&ia32_mmap_mutex);
1130 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1131 mutex_unlock(&ia32_mmap_mutex);
1133 if ((ret >= 0) && (old_len < new_len)) {
1134 /* mremap expanded successfully */
1135 ia32_set_pp(old_end, new_end, flags);
1142 sys32_pipe (int __user *fd)
1147 retval = do_pipe(fds);
1150 if (copy_to_user(fd, fds, sizeof(fds)))
1157 get_tv32 (struct timeval *o, struct compat_timeval __user *i)
1159 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
1160 (__get_user(o->tv_sec, &i->tv_sec) | __get_user(o->tv_usec, &i->tv_usec)));
1164 put_tv32 (struct compat_timeval __user *o, struct timeval *i)
1166 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
1167 (__put_user(i->tv_sec, &o->tv_sec) | __put_user(i->tv_usec, &o->tv_usec)));
1170 asmlinkage unsigned long
1171 sys32_alarm (unsigned int seconds)
1173 return alarm_setitimer(seconds);
1176 /* Translations due to time_t size differences. Which affects all
1177 sorts of things, like timeval and itimerval. */
1179 extern struct timezone sys_tz;
1182 sys32_gettimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1186 do_gettimeofday(&ktv);
1187 if (put_tv32(tv, &ktv))
1191 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
1198 sys32_settimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1201 struct timespec kts;
1202 struct timezone ktz;
1205 if (get_tv32(&ktv, tv))
1207 kts.tv_sec = ktv.tv_sec;
1208 kts.tv_nsec = ktv.tv_usec * 1000;
1211 if (copy_from_user(&ktz, tz, sizeof(ktz)))
1215 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
1218 struct getdents32_callback {
1219 struct compat_dirent __user *current_dir;
1220 struct compat_dirent __user *previous;
1225 struct readdir32_callback {
1226 struct old_linux32_dirent __user * dirent;
1231 filldir32 (void *__buf, const char *name, int namlen, loff_t offset, u64 ino,
1232 unsigned int d_type)
1234 struct compat_dirent __user * dirent;
1235 struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
1236 int reclen = ROUND_UP(offsetof(struct compat_dirent, d_name) + namlen + 1, 4);
1239 buf->error = -EINVAL; /* only used if we fail.. */
1240 if (reclen > buf->count)
1243 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1245 buf->error = -EFAULT; /* only used if we fail.. */
1246 dirent = buf->previous;
1248 if (put_user(offset, &dirent->d_off))
1250 dirent = buf->current_dir;
1251 buf->previous = dirent;
1252 if (put_user(d_ino, &dirent->d_ino)
1253 || put_user(reclen, &dirent->d_reclen)
1254 || copy_to_user(dirent->d_name, name, namlen)
1255 || put_user(0, dirent->d_name + namlen))
1257 dirent = (struct compat_dirent __user *) ((char __user *) dirent + reclen);
1258 buf->current_dir = dirent;
1259 buf->count -= reclen;
1264 sys32_getdents (unsigned int fd, struct compat_dirent __user *dirent, unsigned int count)
1267 struct compat_dirent __user * lastdirent;
1268 struct getdents32_callback buf;
1272 if (!access_ok(VERIFY_WRITE, dirent, count))
1280 buf.current_dir = dirent;
1281 buf.previous = NULL;
1285 error = vfs_readdir(file, filldir32, &buf);
1289 lastdirent = buf.previous;
1291 if (put_user(file->f_pos, &lastdirent->d_off))
1294 error = count - buf.count;
1304 fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, u64 ino,
1305 unsigned int d_type)
1307 struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
1308 struct old_linux32_dirent __user * dirent;
1314 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1317 dirent = buf->dirent;
1318 if (put_user(d_ino, &dirent->d_ino)
1319 || put_user(offset, &dirent->d_offset)
1320 || put_user(namlen, &dirent->d_namlen)
1321 || copy_to_user(dirent->d_name, name, namlen)
1322 || put_user(0, dirent->d_name + namlen))
1328 sys32_readdir (unsigned int fd, void __user *dirent, unsigned int count)
1332 struct readdir32_callback buf;
1340 buf.dirent = dirent;
1342 error = vfs_readdir(file, fillonedir32, &buf);
1350 struct sel_arg_struct {
1359 sys32_old_select (struct sel_arg_struct __user *arg)
1361 struct sel_arg_struct a;
1363 if (copy_from_user(&a, arg, sizeof(a)))
1365 return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1366 compat_ptr(a.exp), compat_ptr(a.tvp));
1372 #define SEMTIMEDOP 4
1383 sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
1387 version = call >> 16; /* hack for backward compatibility */
1393 return compat_sys_semtimedop(first, compat_ptr(ptr),
1394 second, compat_ptr(fifth));
1395 /* else fall through for normal semop() */
1397 /* struct sembuf is the same on 32 and 64bit :)) */
1398 return sys_semtimedop(first, compat_ptr(ptr), second,
1401 return sys_semget(first, second, third);
1403 return compat_sys_semctl(first, second, third, compat_ptr(ptr));
1406 return compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
1408 return compat_sys_msgrcv(first, second, fifth, third, version, compat_ptr(ptr));
1410 return sys_msgget((key_t) first, second);
1412 return compat_sys_msgctl(first, second, compat_ptr(ptr));
1415 return compat_sys_shmat(first, second, third, version, compat_ptr(ptr));
1418 return sys_shmdt(compat_ptr(ptr));
1420 return sys_shmget(first, (unsigned)second, third);
1422 return compat_sys_shmctl(first, second, compat_ptr(ptr));
1431 compat_sys_wait4 (compat_pid_t pid, compat_uint_t * stat_addr, int options,
1432 struct compat_rusage *ru);
1435 sys32_waitpid (int pid, unsigned int *stat_addr, int options)
1437 return compat_sys_wait4(pid, stat_addr, options, NULL);
1441 ia32_peek (struct task_struct *child, unsigned long addr, unsigned int *val)
1446 copied = access_process_vm(child, addr, val, sizeof(*val), 0);
1447 return (copied != sizeof(ret)) ? -EIO : 0;
1451 ia32_poke (struct task_struct *child, unsigned long addr, unsigned int val)
1454 if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val))
1460 * The order in which registers are stored in the ptrace regs structure
1473 #define PT_ORIG_EAX 11
1481 getreg (struct task_struct *child, int regno)
1483 struct pt_regs *child_regs;
1485 child_regs = task_pt_regs(child);
1486 switch (regno / sizeof(int)) {
1487 case PT_EBX: return child_regs->r11;
1488 case PT_ECX: return child_regs->r9;
1489 case PT_EDX: return child_regs->r10;
1490 case PT_ESI: return child_regs->r14;
1491 case PT_EDI: return child_regs->r15;
1492 case PT_EBP: return child_regs->r13;
1493 case PT_EAX: return child_regs->r8;
1494 case PT_ORIG_EAX: return child_regs->r1; /* see dispatch_to_ia32_handler() */
1495 case PT_EIP: return child_regs->cr_iip;
1496 case PT_UESP: return child_regs->r12;
1497 case PT_EFL: return child->thread.eflag;
1498 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1500 case PT_CS: return __USER_CS;
1502 printk(KERN_ERR "ia32.getreg(): unknown register %d\n", regno);
1509 putreg (struct task_struct *child, int regno, unsigned int value)
1511 struct pt_regs *child_regs;
1513 child_regs = task_pt_regs(child);
1514 switch (regno / sizeof(int)) {
1515 case PT_EBX: child_regs->r11 = value; break;
1516 case PT_ECX: child_regs->r9 = value; break;
1517 case PT_EDX: child_regs->r10 = value; break;
1518 case PT_ESI: child_regs->r14 = value; break;
1519 case PT_EDI: child_regs->r15 = value; break;
1520 case PT_EBP: child_regs->r13 = value; break;
1521 case PT_EAX: child_regs->r8 = value; break;
1522 case PT_ORIG_EAX: child_regs->r1 = value; break;
1523 case PT_EIP: child_regs->cr_iip = value; break;
1524 case PT_UESP: child_regs->r12 = value; break;
1525 case PT_EFL: child->thread.eflag = value; break;
1526 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1527 if (value != __USER_DS)
1529 "ia32.putreg: attempt to set invalid segment register %d = %x\n",
1533 if (value != __USER_CS)
1535 "ia32.putreg: attempt to to set invalid segment register %d = %x\n",
1539 printk(KERN_ERR "ia32.putreg: unknown register %d\n", regno);
1545 put_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1546 struct switch_stack *swp, int tos)
1548 struct _fpreg_ia32 *f;
1551 f = (struct _fpreg_ia32 *)(((unsigned long)buf + 15) & ~15);
1552 if ((regno += tos) >= 8)
1556 ia64f2ia32f(f, &ptp->f8);
1559 ia64f2ia32f(f, &ptp->f9);
1562 ia64f2ia32f(f, &ptp->f10);
1565 ia64f2ia32f(f, &ptp->f11);
1571 ia64f2ia32f(f, &swp->f12 + (regno - 4));
1574 copy_to_user(reg, f, sizeof(*reg));
1578 get_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1579 struct switch_stack *swp, int tos)
1582 if ((regno += tos) >= 8)
1586 copy_from_user(&ptp->f8, reg, sizeof(*reg));
1589 copy_from_user(&ptp->f9, reg, sizeof(*reg));
1592 copy_from_user(&ptp->f10, reg, sizeof(*reg));
1595 copy_from_user(&ptp->f11, reg, sizeof(*reg));
1601 copy_from_user(&swp->f12 + (regno - 4), reg, sizeof(*reg));
1608 save_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1610 struct switch_stack *swp;
1611 struct pt_regs *ptp;
1614 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1617 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1618 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1619 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1620 __put_user(tsk->thread.fir, &save->fip);
1621 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1622 __put_user(tsk->thread.fdr, &save->foo);
1623 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1626 * Stack frames start with 16-bytes of temp space
1628 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1629 ptp = task_pt_regs(tsk);
1630 tos = (tsk->thread.fsr >> 11) & 7;
1631 for (i = 0; i < 8; i++)
1632 put_fpreg(i, &save->st_space[i], ptp, swp, tos);
1637 restore_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1639 struct switch_stack *swp;
1640 struct pt_regs *ptp;
1642 unsigned int fsrlo, fsrhi, num32;
1644 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1647 __get_user(num32, (unsigned int __user *)&save->cwd);
1648 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1649 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1650 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1651 num32 = (fsrhi << 16) | fsrlo;
1652 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1653 __get_user(num32, (unsigned int __user *)&save->fip);
1654 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1655 __get_user(num32, (unsigned int __user *)&save->foo);
1656 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1659 * Stack frames start with 16-bytes of temp space
1661 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1662 ptp = task_pt_regs(tsk);
1663 tos = (tsk->thread.fsr >> 11) & 7;
1664 for (i = 0; i < 8; i++)
1665 get_fpreg(i, &save->st_space[i], ptp, swp, tos);
1670 save_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1672 struct switch_stack *swp;
1673 struct pt_regs *ptp;
1675 unsigned long mxcsr=0;
1676 unsigned long num128[2];
1678 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1681 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1682 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1683 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1684 __put_user(tsk->thread.fir, &save->fip);
1685 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1686 __put_user(tsk->thread.fdr, &save->foo);
1687 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1690 * Stack frames start with 16-bytes of temp space
1692 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1693 ptp = task_pt_regs(tsk);
1694 tos = (tsk->thread.fsr >> 11) & 7;
1695 for (i = 0; i < 8; i++)
1696 put_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1698 mxcsr = ((tsk->thread.fcr>>32) & 0xff80) | ((tsk->thread.fsr>>32) & 0x3f);
1699 __put_user(mxcsr & 0xffff, &save->mxcsr);
1700 for (i = 0; i < 8; i++) {
1701 memcpy(&(num128[0]), &(swp->f16) + i*2, sizeof(unsigned long));
1702 memcpy(&(num128[1]), &(swp->f17) + i*2, sizeof(unsigned long));
1703 copy_to_user(&save->xmm_space[0] + 4*i, num128, sizeof(struct _xmmreg_ia32));
1709 restore_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1711 struct switch_stack *swp;
1712 struct pt_regs *ptp;
1714 unsigned int fsrlo, fsrhi, num32;
1716 unsigned long num64;
1717 unsigned long num128[2];
1719 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1722 __get_user(num32, (unsigned int __user *)&save->cwd);
1723 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1724 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1725 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1726 num32 = (fsrhi << 16) | fsrlo;
1727 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1728 __get_user(num32, (unsigned int __user *)&save->fip);
1729 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1730 __get_user(num32, (unsigned int __user *)&save->foo);
1731 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1734 * Stack frames start with 16-bytes of temp space
1736 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1737 ptp = task_pt_regs(tsk);
1738 tos = (tsk->thread.fsr >> 11) & 7;
1739 for (i = 0; i < 8; i++)
1740 get_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1742 __get_user(mxcsr, (unsigned int __user *)&save->mxcsr);
1743 num64 = mxcsr & 0xff10;
1744 tsk->thread.fcr = (tsk->thread.fcr & (~0xff1000000000UL)) | (num64<<32);
1745 num64 = mxcsr & 0x3f;
1746 tsk->thread.fsr = (tsk->thread.fsr & (~0x3f00000000UL)) | (num64<<32);
1748 for (i = 0; i < 8; i++) {
1749 copy_from_user(num128, &save->xmm_space[0] + 4*i, sizeof(struct _xmmreg_ia32));
1750 memcpy(&(swp->f16) + i*2, &(num128[0]), sizeof(unsigned long));
1751 memcpy(&(swp->f17) + i*2, &(num128[1]), sizeof(unsigned long));
1757 sys32_ptrace (int request, pid_t pid, unsigned int addr, unsigned int data)
1759 struct task_struct *child;
1760 unsigned int value, tmp;
1764 if (request == PTRACE_TRACEME) {
1765 ret = ptrace_traceme();
1769 child = ptrace_get_task_struct(pid);
1770 if (IS_ERR(child)) {
1771 ret = PTR_ERR(child);
1775 if (request == PTRACE_ATTACH) {
1776 ret = sys_ptrace(request, pid, addr, data);
1780 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1785 case PTRACE_PEEKTEXT:
1786 case PTRACE_PEEKDATA: /* read word at location addr */
1787 ret = ia32_peek(child, addr, &value);
1789 ret = put_user(value, (unsigned int __user *) compat_ptr(data));
1794 case PTRACE_POKETEXT:
1795 case PTRACE_POKEDATA: /* write the word at location addr */
1796 ret = ia32_poke(child, addr, data);
1799 case PTRACE_PEEKUSR: /* read word at addr in USER area */
1801 if ((addr & 3) || addr > 17*sizeof(int))
1804 tmp = getreg(child, addr);
1805 if (!put_user(tmp, (unsigned int __user *) compat_ptr(data)))
1809 case PTRACE_POKEUSR: /* write word at addr in USER area */
1811 if ((addr & 3) || addr > 17*sizeof(int))
1814 putreg(child, addr, data);
1818 case IA32_PTRACE_GETREGS:
1819 if (!access_ok(VERIFY_WRITE, compat_ptr(data), 17*sizeof(int))) {
1823 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1824 put_user(getreg(child, i), (unsigned int __user *) compat_ptr(data));
1825 data += sizeof(int);
1830 case IA32_PTRACE_SETREGS:
1831 if (!access_ok(VERIFY_READ, compat_ptr(data), 17*sizeof(int))) {
1835 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1836 get_user(tmp, (unsigned int __user *) compat_ptr(data));
1837 putreg(child, i, tmp);
1838 data += sizeof(int);
1843 case IA32_PTRACE_GETFPREGS:
1844 ret = save_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1848 case IA32_PTRACE_GETFPXREGS:
1849 ret = save_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1853 case IA32_PTRACE_SETFPREGS:
1854 ret = restore_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1858 case IA32_PTRACE_SETFPXREGS:
1859 ret = restore_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1863 case PTRACE_GETEVENTMSG:
1864 ret = put_user(child->ptrace_message, (unsigned int __user *) compat_ptr(data));
1867 case PTRACE_SYSCALL: /* continue, stop after next syscall */
1868 case PTRACE_CONT: /* restart after signal. */
1870 case PTRACE_SINGLESTEP: /* execute chile for one instruction */
1871 case PTRACE_DETACH: /* detach a process */
1872 ret = sys_ptrace(request, pid, addr, data);
1876 ret = ptrace_request(child, request, addr, data);
1881 put_task_struct(child);
1889 unsigned int ss_flags;
1890 unsigned int ss_size;
1894 sys32_sigaltstack (ia32_stack_t __user *uss32, ia32_stack_t __user *uoss32,
1895 long arg2, long arg3, long arg4, long arg5, long arg6,
1896 long arg7, struct pt_regs pt)
1901 mm_segment_t old_fs = get_fs();
1904 if (copy_from_user(&buf32, uss32, sizeof(ia32_stack_t)))
1906 uss.ss_sp = (void __user *) (long) buf32.ss_sp;
1907 uss.ss_flags = buf32.ss_flags;
1908 /* MINSIGSTKSZ is different for ia32 vs ia64. We lie here to pass the
1909 check and set it to the user requested value later */
1910 if ((buf32.ss_flags != SS_DISABLE) && (buf32.ss_size < MINSIGSTKSZ_IA32)) {
1914 uss.ss_size = MINSIGSTKSZ;
1917 ret = do_sigaltstack(uss32 ? (stack_t __user *) &uss : NULL,
1918 (stack_t __user *) &uoss, pt.r12);
1919 current->sas_ss_size = buf32.ss_size;
1925 buf32.ss_sp = (long __user) uoss.ss_sp;
1926 buf32.ss_flags = uoss.ss_flags;
1927 buf32.ss_size = uoss.ss_size;
1928 if (copy_to_user(uoss32, &buf32, sizeof(ia32_stack_t)))
1937 current->state = TASK_INTERRUPTIBLE;
1939 return -ERESTARTNOHAND;
1943 sys32_msync (unsigned int start, unsigned int len, int flags)
1947 if (OFFSET4K(start))
1949 addr = PAGE_START(start);
1950 return sys_msync(addr, len + (start - addr), flags);
1956 unsigned int oldval;
1957 unsigned int oldlenp;
1958 unsigned int newval;
1959 unsigned int newlen;
1960 unsigned int __unused[4];
1963 #ifdef CONFIG_SYSCTL_SYSCALL
1965 sys32_sysctl (struct sysctl32 __user *args)
1967 struct sysctl32 a32;
1968 mm_segment_t old_fs = get_fs ();
1969 void __user *oldvalp, *newvalp;
1974 if (copy_from_user(&a32, args, sizeof(a32)))
1978 * We need to pre-validate these because we have to disable address checking
1979 * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
1980 * user specifying bad addresses here. Well, since we're dealing with 32 bit
1981 * addresses, we KNOW that access_ok() will always succeed, so this is an
1982 * expensive NOP, but so what...
1984 namep = (int __user *) compat_ptr(a32.name);
1985 oldvalp = compat_ptr(a32.oldval);
1986 newvalp = compat_ptr(a32.newval);
1988 if ((oldvalp && get_user(oldlen, (int __user *) compat_ptr(a32.oldlenp)))
1989 || !access_ok(VERIFY_WRITE, namep, 0)
1990 || !access_ok(VERIFY_WRITE, oldvalp, 0)
1991 || !access_ok(VERIFY_WRITE, newvalp, 0))
1996 ret = do_sysctl(namep, a32.nlen, oldvalp, (size_t __user *) &oldlen,
1997 newvalp, (size_t) a32.newlen);
2001 if (oldvalp && put_user (oldlen, (int __user *) compat_ptr(a32.oldlenp)))
2009 sys32_newuname (struct new_utsname __user *name)
2011 int ret = sys_newuname(name);
2014 if (copy_to_user(name->machine, "i686\0\0\0", 8))
2020 sys32_getresuid16 (u16 __user *ruid, u16 __user *euid, u16 __user *suid)
2024 mm_segment_t old_fs = get_fs();
2027 ret = sys_getresuid((uid_t __user *) &a, (uid_t __user *) &b, (uid_t __user *) &c);
2030 if (put_user(a, ruid) || put_user(b, euid) || put_user(c, suid))
2036 sys32_getresgid16 (u16 __user *rgid, u16 __user *egid, u16 __user *sgid)
2040 mm_segment_t old_fs = get_fs();
2043 ret = sys_getresgid((gid_t __user *) &a, (gid_t __user *) &b, (gid_t __user *) &c);
2049 return put_user(a, rgid) | put_user(b, egid) | put_user(c, sgid);
2053 sys32_lseek (unsigned int fd, int offset, unsigned int whence)
2055 /* Sign-extension of "offset" is important here... */
2056 return sys_lseek(fd, offset, whence);
2060 groups16_to_user(short __user *grouplist, struct group_info *group_info)
2065 for (i = 0; i < group_info->ngroups; i++) {
2066 group = (short)GROUP_AT(group_info, i);
2067 if (put_user(group, grouplist+i))
2075 groups16_from_user(struct group_info *group_info, short __user *grouplist)
2080 for (i = 0; i < group_info->ngroups; i++) {
2081 if (get_user(group, grouplist+i))
2083 GROUP_AT(group_info, i) = (gid_t)group;
2090 sys32_getgroups16 (int gidsetsize, short __user *grouplist)
2097 get_group_info(current->group_info);
2098 i = current->group_info->ngroups;
2100 if (i > gidsetsize) {
2104 if (groups16_to_user(grouplist, current->group_info)) {
2110 put_group_info(current->group_info);
2115 sys32_setgroups16 (int gidsetsize, short __user *grouplist)
2117 struct group_info *group_info;
2120 if (!capable(CAP_SETGID))
2122 if ((unsigned)gidsetsize > NGROUPS_MAX)
2125 group_info = groups_alloc(gidsetsize);
2128 retval = groups16_from_user(group_info, grouplist);
2130 put_group_info(group_info);
2134 retval = set_current_groups(group_info);
2135 put_group_info(group_info);
2141 sys32_truncate64 (unsigned int path, unsigned int len_lo, unsigned int len_hi)
2143 return sys_truncate(compat_ptr(path), ((unsigned long) len_hi << 32) | len_lo);
2147 sys32_ftruncate64 (int fd, unsigned int len_lo, unsigned int len_hi)
2149 return sys_ftruncate(fd, ((unsigned long) len_hi << 32) | len_lo);
2153 putstat64 (struct stat64 __user *ubuf, struct kstat *kbuf)
2158 if (clear_user(ubuf, sizeof(*ubuf)))
2161 hdev = huge_encode_dev(kbuf->dev);
2162 err = __put_user(hdev, (u32 __user*)&ubuf->st_dev);
2163 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_dev) + 1);
2164 err |= __put_user(kbuf->ino, &ubuf->__st_ino);
2165 err |= __put_user(kbuf->ino, &ubuf->st_ino_lo);
2166 err |= __put_user(kbuf->ino >> 32, &ubuf->st_ino_hi);
2167 err |= __put_user(kbuf->mode, &ubuf->st_mode);
2168 err |= __put_user(kbuf->nlink, &ubuf->st_nlink);
2169 err |= __put_user(kbuf->uid, &ubuf->st_uid);
2170 err |= __put_user(kbuf->gid, &ubuf->st_gid);
2171 hdev = huge_encode_dev(kbuf->rdev);
2172 err = __put_user(hdev, (u32 __user*)&ubuf->st_rdev);
2173 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_rdev) + 1);
2174 err |= __put_user(kbuf->size, &ubuf->st_size_lo);
2175 err |= __put_user((kbuf->size >> 32), &ubuf->st_size_hi);
2176 err |= __put_user(kbuf->atime.tv_sec, &ubuf->st_atime);
2177 err |= __put_user(kbuf->atime.tv_nsec, &ubuf->st_atime_nsec);
2178 err |= __put_user(kbuf->mtime.tv_sec, &ubuf->st_mtime);
2179 err |= __put_user(kbuf->mtime.tv_nsec, &ubuf->st_mtime_nsec);
2180 err |= __put_user(kbuf->ctime.tv_sec, &ubuf->st_ctime);
2181 err |= __put_user(kbuf->ctime.tv_nsec, &ubuf->st_ctime_nsec);
2182 err |= __put_user(kbuf->blksize, &ubuf->st_blksize);
2183 err |= __put_user(kbuf->blocks, &ubuf->st_blocks);
2188 sys32_stat64 (char __user *filename, struct stat64 __user *statbuf)
2191 long ret = vfs_stat(filename, &s);
2193 ret = putstat64(statbuf, &s);
2198 sys32_lstat64 (char __user *filename, struct stat64 __user *statbuf)
2201 long ret = vfs_lstat(filename, &s);
2203 ret = putstat64(statbuf, &s);
2208 sys32_fstat64 (unsigned int fd, struct stat64 __user *statbuf)
2211 long ret = vfs_fstat(fd, &s);
2213 ret = putstat64(statbuf, &s);
2218 sys32_sched_rr_get_interval (pid_t pid, struct compat_timespec __user *interval)
2220 mm_segment_t old_fs = get_fs();
2225 ret = sys_sched_rr_get_interval(pid, (struct timespec __user *) &t);
2227 if (put_compat_timespec(&t, interval))
2233 sys32_pread (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2235 return sys_pread64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2239 sys32_pwrite (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2241 return sys_pwrite64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2245 sys32_sendfile (int out_fd, int in_fd, int __user *offset, unsigned int count)
2247 mm_segment_t old_fs = get_fs();
2251 if (offset && get_user(of, offset))
2255 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *) &of : NULL, count);
2258 if (offset && put_user(of, offset))
2265 sys32_personality (unsigned int personality)
2269 if (current->personality == PER_LINUX32 && personality == PER_LINUX)
2270 personality = PER_LINUX32;
2271 ret = sys_personality(personality);
2272 if (ret == PER_LINUX32)
2277 asmlinkage unsigned long
2278 sys32_brk (unsigned int brk)
2280 unsigned long ret, obrk;
2281 struct mm_struct *mm = current->mm;
2286 clear_user(compat_ptr(ret), PAGE_ALIGN(ret) - ret);
2290 /* Structure for ia32 emulation on ia64 */
2291 struct epoll_event32
2298 sys32_epoll_ctl(int epfd, int op, int fd, struct epoll_event32 __user *event)
2300 mm_segment_t old_fs = get_fs();
2301 struct epoll_event event64;
2305 if (!access_ok(VERIFY_READ, event, sizeof(struct epoll_event32)))
2308 __get_user(event64.events, &event->events);
2309 __get_user(data_halfword, &event->data[0]);
2310 event64.data = data_halfword;
2311 __get_user(data_halfword, &event->data[1]);
2312 event64.data |= (u64)data_halfword << 32;
2315 error = sys_epoll_ctl(epfd, op, fd, (struct epoll_event __user *) &event64);
2322 sys32_epoll_wait(int epfd, struct epoll_event32 __user * events, int maxevents,
2325 struct epoll_event *events64 = NULL;
2326 mm_segment_t old_fs = get_fs();
2327 int numevents, size;
2329 int do_free_pages = 0;
2331 if (maxevents <= 0) {
2335 /* Verify that the area passed by the user is writeable */
2336 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event32)))
2340 * Allocate space for the intermediate copy. If the space needed
2341 * is large enough to cause kmalloc to fail, then try again with
2344 size = maxevents * sizeof(struct epoll_event);
2345 events64 = kmalloc(size, GFP_KERNEL);
2346 if (events64 == NULL) {
2347 events64 = (struct epoll_event *)
2348 __get_free_pages(GFP_KERNEL, get_order(size));
2349 if (events64 == NULL)
2354 /* Do the system call */
2355 set_fs(KERNEL_DS); /* copy_to/from_user should work on kernel mem*/
2356 numevents = sys_epoll_wait(epfd, (struct epoll_event __user *) events64,
2357 maxevents, timeout);
2360 /* Don't modify userspace memory if we're returning an error */
2361 if (numevents > 0) {
2362 /* Translate the 64-bit structures back into the 32-bit
2364 for (evt_idx = 0; evt_idx < numevents; evt_idx++) {
2365 __put_user(events64[evt_idx].events,
2366 &events[evt_idx].events);
2367 __put_user((u32)events64[evt_idx].data,
2368 &events[evt_idx].data[0]);
2369 __put_user((u32)(events64[evt_idx].data >> 32),
2370 &events[evt_idx].data[1]);
2375 free_pages((unsigned long) events64, get_order(size));
2382 * Get a yet unused TLS descriptor index.
2387 struct thread_struct *t = ¤t->thread;
2390 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
2391 if (desc_empty(t->tls_array + idx))
2392 return idx + GDT_ENTRY_TLS_MIN;
2397 * Set a given TLS descriptor:
2400 sys32_set_thread_area (struct ia32_user_desc __user *u_info)
2402 struct thread_struct *t = ¤t->thread;
2403 struct ia32_user_desc info;
2404 struct desc_struct *desc;
2407 if (copy_from_user(&info, u_info, sizeof(info)))
2409 idx = info.entry_number;
2412 * index -1 means the kernel should try to find and allocate an empty descriptor:
2415 idx = get_free_idx();
2418 if (put_user(idx, &u_info->entry_number))
2422 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2425 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
2427 cpu = smp_processor_id();
2429 if (LDT_empty(&info)) {
2433 desc->a = LDT_entry_a(&info);
2434 desc->b = LDT_entry_b(&info);
2441 * Get the current Thread-Local Storage area:
2444 #define GET_BASE(desc) ( \
2445 (((desc)->a >> 16) & 0x0000ffff) | \
2446 (((desc)->b << 16) & 0x00ff0000) | \
2447 ( (desc)->b & 0xff000000) )
2449 #define GET_LIMIT(desc) ( \
2450 ((desc)->a & 0x0ffff) | \
2451 ((desc)->b & 0xf0000) )
2453 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
2454 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
2455 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
2456 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
2457 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
2458 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
2461 sys32_get_thread_area (struct ia32_user_desc __user *u_info)
2463 struct ia32_user_desc info;
2464 struct desc_struct *desc;
2467 if (get_user(idx, &u_info->entry_number))
2469 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2472 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
2474 info.entry_number = idx;
2475 info.base_addr = GET_BASE(desc);
2476 info.limit = GET_LIMIT(desc);
2477 info.seg_32bit = GET_32BIT(desc);
2478 info.contents = GET_CONTENTS(desc);
2479 info.read_exec_only = !GET_WRITABLE(desc);
2480 info.limit_in_pages = GET_LIMIT_PAGES(desc);
2481 info.seg_not_present = !GET_PRESENT(desc);
2482 info.useable = GET_USEABLE(desc);
2484 if (copy_to_user(u_info, &info, sizeof(info)))
2489 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
2490 __u32 len_low, __u32 len_high, int advice)
2492 return sys_fadvise64_64(fd,
2493 (((u64)offset_high)<<32) | offset_low,
2494 (((u64)len_high)<<32) | len_low,
2498 #ifdef NOTYET /* UNTESTED FOR IA64 FROM HERE DOWN */
2500 asmlinkage long sys32_setreuid(compat_uid_t ruid, compat_uid_t euid)
2504 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2505 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2506 return sys_setreuid(sruid, seuid);
2510 sys32_setresuid(compat_uid_t ruid, compat_uid_t euid,
2513 uid_t sruid, seuid, ssuid;
2515 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2516 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2517 ssuid = (suid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)suid);
2518 return sys_setresuid(sruid, seuid, ssuid);
2522 sys32_setregid(compat_gid_t rgid, compat_gid_t egid)
2526 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2527 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2528 return sys_setregid(srgid, segid);
2532 sys32_setresgid(compat_gid_t rgid, compat_gid_t egid,
2535 gid_t srgid, segid, ssgid;
2537 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2538 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2539 ssgid = (sgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)sgid);
2540 return sys_setresgid(srgid, segid, ssgid);