Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[wrt350n-kernel.git] / arch / ia64 / ia32 / sys_ia32.c
blobb1bf51fe97b47cc66b32cd889431c5ae9af1b3b3
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/socket.h>
36 #include <linux/quota.h>
37 #include <linux/poll.h>
38 #include <linux/eventpoll.h>
39 #include <linux/personality.h>
40 #include <linux/ptrace.h>
41 #include <linux/stat.h>
42 #include <linux/ipc.h>
43 #include <linux/capability.h>
44 #include <linux/compat.h>
45 #include <linux/vfs.h>
46 #include <linux/mman.h>
47 #include <linux/mutex.h>
49 #include <asm/intrinsics.h>
50 #include <asm/types.h>
51 #include <asm/uaccess.h>
52 #include <asm/unistd.h>
54 #include "ia32priv.h"
56 #include <net/scm.h>
57 #include <net/sock.h>
59 #define DEBUG 0
61 #if DEBUG
62 # define DBG(fmt...) printk(KERN_DEBUG fmt)
63 #else
64 # define DBG(fmt...)
65 #endif
67 #define ROUND_UP(x,a) ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
69 #define OFFSET4K(a) ((a) & 0xfff)
70 #define PAGE_START(addr) ((addr) & PAGE_MASK)
71 #define MINSIGSTKSZ_IA32 2048
73 #define high2lowuid(uid) ((uid) > 65535 ? 65534 : (uid))
74 #define high2lowgid(gid) ((gid) > 65535 ? 65534 : (gid))
77 * Anything that modifies or inspects ia32 user virtual memory must hold this semaphore
78 * while doing so.
80 /* XXX make per-mm: */
81 static DEFINE_MUTEX(ia32_mmap_mutex);
83 asmlinkage long
84 sys32_execve (char __user *name, compat_uptr_t __user *argv, compat_uptr_t __user *envp,
85 struct pt_regs *regs)
87 long error;
88 char *filename;
89 unsigned long old_map_base, old_task_size, tssd;
91 filename = getname(name);
92 error = PTR_ERR(filename);
93 if (IS_ERR(filename))
94 return error;
96 old_map_base = current->thread.map_base;
97 old_task_size = current->thread.task_size;
98 tssd = ia64_get_kr(IA64_KR_TSSD);
100 /* we may be exec'ing a 64-bit process: reset map base, task-size, and io-base: */
101 current->thread.map_base = DEFAULT_MAP_BASE;
102 current->thread.task_size = DEFAULT_TASK_SIZE;
103 ia64_set_kr(IA64_KR_IO_BASE, current->thread.old_iob);
104 ia64_set_kr(IA64_KR_TSSD, current->thread.old_k1);
106 error = compat_do_execve(filename, argv, envp, regs);
107 putname(filename);
109 if (error < 0) {
110 /* oops, execve failed, switch back to old values... */
111 ia64_set_kr(IA64_KR_IO_BASE, IA32_IOBASE);
112 ia64_set_kr(IA64_KR_TSSD, tssd);
113 current->thread.map_base = old_map_base;
114 current->thread.task_size = old_task_size;
117 return error;
120 int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
122 compat_ino_t ino;
123 int err;
125 if ((u64) stat->size > MAX_NON_LFS ||
126 !old_valid_dev(stat->dev) ||
127 !old_valid_dev(stat->rdev))
128 return -EOVERFLOW;
130 ino = stat->ino;
131 if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
132 return -EOVERFLOW;
134 if (clear_user(ubuf, sizeof(*ubuf)))
135 return -EFAULT;
137 err = __put_user(old_encode_dev(stat->dev), &ubuf->st_dev);
138 err |= __put_user(ino, &ubuf->st_ino);
139 err |= __put_user(stat->mode, &ubuf->st_mode);
140 err |= __put_user(stat->nlink, &ubuf->st_nlink);
141 err |= __put_user(high2lowuid(stat->uid), &ubuf->st_uid);
142 err |= __put_user(high2lowgid(stat->gid), &ubuf->st_gid);
143 err |= __put_user(old_encode_dev(stat->rdev), &ubuf->st_rdev);
144 err |= __put_user(stat->size, &ubuf->st_size);
145 err |= __put_user(stat->atime.tv_sec, &ubuf->st_atime);
146 err |= __put_user(stat->atime.tv_nsec, &ubuf->st_atime_nsec);
147 err |= __put_user(stat->mtime.tv_sec, &ubuf->st_mtime);
148 err |= __put_user(stat->mtime.tv_nsec, &ubuf->st_mtime_nsec);
149 err |= __put_user(stat->ctime.tv_sec, &ubuf->st_ctime);
150 err |= __put_user(stat->ctime.tv_nsec, &ubuf->st_ctime_nsec);
151 err |= __put_user(stat->blksize, &ubuf->st_blksize);
152 err |= __put_user(stat->blocks, &ubuf->st_blocks);
153 return err;
156 #if PAGE_SHIFT > IA32_PAGE_SHIFT
159 static int
160 get_page_prot (struct vm_area_struct *vma, unsigned long addr)
162 int prot = 0;
164 if (!vma || vma->vm_start > addr)
165 return 0;
167 if (vma->vm_flags & VM_READ)
168 prot |= PROT_READ;
169 if (vma->vm_flags & VM_WRITE)
170 prot |= PROT_WRITE;
171 if (vma->vm_flags & VM_EXEC)
172 prot |= PROT_EXEC;
173 return prot;
177 * Map a subpage by creating an anonymous page that contains the union of the old page and
178 * the subpage.
180 static unsigned long
181 mmap_subpage (struct file *file, unsigned long start, unsigned long end, int prot, int flags,
182 loff_t off)
184 void *page = NULL;
185 struct inode *inode;
186 unsigned long ret = 0;
187 struct vm_area_struct *vma = find_vma(current->mm, start);
188 int old_prot = get_page_prot(vma, start);
190 DBG("mmap_subpage(file=%p,start=0x%lx,end=0x%lx,prot=%x,flags=%x,off=0x%llx)\n",
191 file, start, end, prot, flags, off);
194 /* Optimize the case where the old mmap and the new mmap are both anonymous */
195 if ((old_prot & PROT_WRITE) && (flags & MAP_ANONYMOUS) && !vma->vm_file) {
196 if (clear_user((void __user *) start, end - start)) {
197 ret = -EFAULT;
198 goto out;
200 goto skip_mmap;
203 page = (void *) get_zeroed_page(GFP_KERNEL);
204 if (!page)
205 return -ENOMEM;
207 if (old_prot)
208 copy_from_user(page, (void __user *) PAGE_START(start), PAGE_SIZE);
210 down_write(&current->mm->mmap_sem);
212 ret = do_mmap(NULL, PAGE_START(start), PAGE_SIZE, prot | PROT_WRITE,
213 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
215 up_write(&current->mm->mmap_sem);
217 if (IS_ERR((void *) ret))
218 goto out;
220 if (old_prot) {
221 /* copy back the old page contents. */
222 if (offset_in_page(start))
223 copy_to_user((void __user *) PAGE_START(start), page,
224 offset_in_page(start));
225 if (offset_in_page(end))
226 copy_to_user((void __user *) end, page + offset_in_page(end),
227 PAGE_SIZE - offset_in_page(end));
230 if (!(flags & MAP_ANONYMOUS)) {
231 /* read the file contents */
232 inode = file->f_path.dentry->d_inode;
233 if (!inode->i_fop || !file->f_op->read
234 || ((*file->f_op->read)(file, (char __user *) start, end - start, &off) < 0))
236 ret = -EINVAL;
237 goto out;
241 skip_mmap:
242 if (!(prot & PROT_WRITE))
243 ret = sys_mprotect(PAGE_START(start), PAGE_SIZE, prot | old_prot);
244 out:
245 if (page)
246 free_page((unsigned long) page);
247 return ret;
250 /* SLAB cache for ia64_partial_page structures */
251 struct kmem_cache *ia64_partial_page_cachep;
254 * init ia64_partial_page_list.
255 * return 0 means kmalloc fail.
257 struct ia64_partial_page_list*
258 ia32_init_pp_list(void)
260 struct ia64_partial_page_list *p;
262 if ((p = kmalloc(sizeof(*p), GFP_KERNEL)) == NULL)
263 return p;
264 p->pp_head = NULL;
265 p->ppl_rb = RB_ROOT;
266 p->pp_hint = NULL;
267 atomic_set(&p->pp_count, 1);
268 return p;
272 * Search for the partial page with @start in partial page list @ppl.
273 * If finds the partial page, return the found partial page.
274 * Else, return 0 and provide @pprev, @rb_link, @rb_parent to
275 * be used by later __ia32_insert_pp().
277 static struct ia64_partial_page *
278 __ia32_find_pp(struct ia64_partial_page_list *ppl, unsigned int start,
279 struct ia64_partial_page **pprev, struct rb_node ***rb_link,
280 struct rb_node **rb_parent)
282 struct ia64_partial_page *pp;
283 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
285 pp = ppl->pp_hint;
286 if (pp && pp->base == start)
287 return pp;
289 __rb_link = &ppl->ppl_rb.rb_node;
290 rb_prev = __rb_parent = NULL;
292 while (*__rb_link) {
293 __rb_parent = *__rb_link;
294 pp = rb_entry(__rb_parent, struct ia64_partial_page, pp_rb);
296 if (pp->base == start) {
297 ppl->pp_hint = pp;
298 return pp;
299 } else if (pp->base < start) {
300 rb_prev = __rb_parent;
301 __rb_link = &__rb_parent->rb_right;
302 } else {
303 __rb_link = &__rb_parent->rb_left;
307 *rb_link = __rb_link;
308 *rb_parent = __rb_parent;
309 *pprev = NULL;
310 if (rb_prev)
311 *pprev = rb_entry(rb_prev, struct ia64_partial_page, pp_rb);
312 return NULL;
316 * insert @pp into @ppl.
318 static void
319 __ia32_insert_pp(struct ia64_partial_page_list *ppl,
320 struct ia64_partial_page *pp, struct ia64_partial_page *prev,
321 struct rb_node **rb_link, struct rb_node *rb_parent)
323 /* link list */
324 if (prev) {
325 pp->next = prev->next;
326 prev->next = pp;
327 } else {
328 ppl->pp_head = pp;
329 if (rb_parent)
330 pp->next = rb_entry(rb_parent,
331 struct ia64_partial_page, pp_rb);
332 else
333 pp->next = NULL;
336 /* link rb */
337 rb_link_node(&pp->pp_rb, rb_parent, rb_link);
338 rb_insert_color(&pp->pp_rb, &ppl->ppl_rb);
340 ppl->pp_hint = pp;
344 * delete @pp from partial page list @ppl.
346 static void
347 __ia32_delete_pp(struct ia64_partial_page_list *ppl,
348 struct ia64_partial_page *pp, struct ia64_partial_page *prev)
350 if (prev) {
351 prev->next = pp->next;
352 if (ppl->pp_hint == pp)
353 ppl->pp_hint = prev;
354 } else {
355 ppl->pp_head = pp->next;
356 if (ppl->pp_hint == pp)
357 ppl->pp_hint = pp->next;
359 rb_erase(&pp->pp_rb, &ppl->ppl_rb);
360 kmem_cache_free(ia64_partial_page_cachep, pp);
363 static struct ia64_partial_page *
364 __pp_prev(struct ia64_partial_page *pp)
366 struct rb_node *prev = rb_prev(&pp->pp_rb);
367 if (prev)
368 return rb_entry(prev, struct ia64_partial_page, pp_rb);
369 else
370 return NULL;
374 * Delete partial pages with address between @start and @end.
375 * @start and @end are page aligned.
377 static void
378 __ia32_delete_pp_range(unsigned int start, unsigned int end)
380 struct ia64_partial_page *pp, *prev;
381 struct rb_node **rb_link, *rb_parent;
383 if (start >= end)
384 return;
386 pp = __ia32_find_pp(current->thread.ppl, start, &prev,
387 &rb_link, &rb_parent);
388 if (pp)
389 prev = __pp_prev(pp);
390 else {
391 if (prev)
392 pp = prev->next;
393 else
394 pp = current->thread.ppl->pp_head;
397 while (pp && pp->base < end) {
398 struct ia64_partial_page *tmp = pp->next;
399 __ia32_delete_pp(current->thread.ppl, pp, prev);
400 pp = tmp;
405 * Set the range between @start and @end in bitmap.
406 * @start and @end should be IA32 page aligned and in the same IA64 page.
408 static int
409 __ia32_set_pp(unsigned int start, unsigned int end, int flags)
411 struct ia64_partial_page *pp, *prev;
412 struct rb_node ** rb_link, *rb_parent;
413 unsigned int pstart, start_bit, end_bit, i;
415 pstart = PAGE_START(start);
416 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
417 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
418 if (end_bit == 0)
419 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
420 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
421 &rb_link, &rb_parent);
422 if (pp) {
423 for (i = start_bit; i < end_bit; i++)
424 set_bit(i, &pp->bitmap);
426 * Check: if this partial page has been set to a full page,
427 * then delete it.
429 if (find_first_zero_bit(&pp->bitmap, sizeof(pp->bitmap)*8) >=
430 PAGE_SIZE/IA32_PAGE_SIZE) {
431 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
433 return 0;
437 * MAP_FIXED may lead to overlapping mmap.
438 * In this case, the requested mmap area may already mmaped as a full
439 * page. So check vma before adding a new partial page.
441 if (flags & MAP_FIXED) {
442 struct vm_area_struct *vma = find_vma(current->mm, pstart);
443 if (vma && vma->vm_start <= pstart)
444 return 0;
447 /* new a ia64_partial_page */
448 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
449 if (!pp)
450 return -ENOMEM;
451 pp->base = pstart;
452 pp->bitmap = 0;
453 for (i=start_bit; i<end_bit; i++)
454 set_bit(i, &(pp->bitmap));
455 pp->next = NULL;
456 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
457 return 0;
461 * @start and @end should be IA32 page aligned, but don't need to be in the
462 * same IA64 page. Split @start and @end to make sure they're in the same IA64
463 * page, then call __ia32_set_pp().
465 static void
466 ia32_set_pp(unsigned int start, unsigned int end, int flags)
468 down_write(&current->mm->mmap_sem);
469 if (flags & MAP_FIXED) {
471 * MAP_FIXED may lead to overlapping mmap. When this happens,
472 * a series of complete IA64 pages results in deletion of
473 * old partial pages in that range.
475 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
478 if (end < PAGE_ALIGN(start)) {
479 __ia32_set_pp(start, end, flags);
480 } else {
481 if (offset_in_page(start))
482 __ia32_set_pp(start, PAGE_ALIGN(start), flags);
483 if (offset_in_page(end))
484 __ia32_set_pp(PAGE_START(end), end, flags);
486 up_write(&current->mm->mmap_sem);
490 * Unset the range between @start and @end in bitmap.
491 * @start and @end should be IA32 page aligned and in the same IA64 page.
492 * After doing that, if the bitmap is 0, then free the page and return 1,
493 * else return 0;
494 * If not find the partial page in the list, then
495 * If the vma exists, then the full page is set to a partial page;
496 * Else return -ENOMEM.
498 static int
499 __ia32_unset_pp(unsigned int start, unsigned int end)
501 struct ia64_partial_page *pp, *prev;
502 struct rb_node ** rb_link, *rb_parent;
503 unsigned int pstart, start_bit, end_bit, i;
504 struct vm_area_struct *vma;
506 pstart = PAGE_START(start);
507 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
508 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
509 if (end_bit == 0)
510 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
512 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
513 &rb_link, &rb_parent);
514 if (pp) {
515 for (i = start_bit; i < end_bit; i++)
516 clear_bit(i, &pp->bitmap);
517 if (pp->bitmap == 0) {
518 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
519 return 1;
521 return 0;
524 vma = find_vma(current->mm, pstart);
525 if (!vma || vma->vm_start > pstart) {
526 return -ENOMEM;
529 /* new a ia64_partial_page */
530 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
531 if (!pp)
532 return -ENOMEM;
533 pp->base = pstart;
534 pp->bitmap = 0;
535 for (i = 0; i < start_bit; i++)
536 set_bit(i, &(pp->bitmap));
537 for (i = end_bit; i < PAGE_SIZE / IA32_PAGE_SIZE; i++)
538 set_bit(i, &(pp->bitmap));
539 pp->next = NULL;
540 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
541 return 0;
545 * Delete pp between PAGE_ALIGN(start) and PAGE_START(end) by calling
546 * __ia32_delete_pp_range(). Unset possible partial pages by calling
547 * __ia32_unset_pp().
548 * The returned value see __ia32_unset_pp().
550 static int
551 ia32_unset_pp(unsigned int *startp, unsigned int *endp)
553 unsigned int start = *startp, end = *endp;
554 int ret = 0;
556 down_write(&current->mm->mmap_sem);
558 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
560 if (end < PAGE_ALIGN(start)) {
561 ret = __ia32_unset_pp(start, end);
562 if (ret == 1) {
563 *startp = PAGE_START(start);
564 *endp = PAGE_ALIGN(end);
566 if (ret == 0) {
567 /* to shortcut sys_munmap() in sys32_munmap() */
568 *startp = PAGE_START(start);
569 *endp = PAGE_START(end);
571 } else {
572 if (offset_in_page(start)) {
573 ret = __ia32_unset_pp(start, PAGE_ALIGN(start));
574 if (ret == 1)
575 *startp = PAGE_START(start);
576 if (ret == 0)
577 *startp = PAGE_ALIGN(start);
578 if (ret < 0)
579 goto out;
581 if (offset_in_page(end)) {
582 ret = __ia32_unset_pp(PAGE_START(end), end);
583 if (ret == 1)
584 *endp = PAGE_ALIGN(end);
585 if (ret == 0)
586 *endp = PAGE_START(end);
590 out:
591 up_write(&current->mm->mmap_sem);
592 return ret;
596 * Compare the range between @start and @end with bitmap in partial page.
597 * @start and @end should be IA32 page aligned and in the same IA64 page.
599 static int
600 __ia32_compare_pp(unsigned int start, unsigned int end)
602 struct ia64_partial_page *pp, *prev;
603 struct rb_node ** rb_link, *rb_parent;
604 unsigned int pstart, start_bit, end_bit, size;
605 unsigned int first_bit, next_zero_bit; /* the first range in bitmap */
607 pstart = PAGE_START(start);
609 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
610 &rb_link, &rb_parent);
611 if (!pp)
612 return 1;
614 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
615 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
616 size = sizeof(pp->bitmap) * 8;
617 first_bit = find_first_bit(&pp->bitmap, size);
618 next_zero_bit = find_next_zero_bit(&pp->bitmap, size, first_bit);
619 if ((start_bit < first_bit) || (end_bit > next_zero_bit)) {
620 /* exceeds the first range in bitmap */
621 return -ENOMEM;
622 } else if ((start_bit == first_bit) && (end_bit == next_zero_bit)) {
623 first_bit = find_next_bit(&pp->bitmap, size, next_zero_bit);
624 if ((next_zero_bit < first_bit) && (first_bit < size))
625 return 1; /* has next range */
626 else
627 return 0; /* no next range */
628 } else
629 return 1;
633 * @start and @end should be IA32 page aligned, but don't need to be in the
634 * same IA64 page. Split @start and @end to make sure they're in the same IA64
635 * page, then call __ia32_compare_pp().
637 * Take this as example: the range is the 1st and 2nd 4K page.
638 * Return 0 if they fit bitmap exactly, i.e. bitmap = 00000011;
639 * Return 1 if the range doesn't cover whole bitmap, e.g. bitmap = 00001111;
640 * Return -ENOMEM if the range exceeds the bitmap, e.g. bitmap = 00000001 or
641 * bitmap = 00000101.
643 static int
644 ia32_compare_pp(unsigned int *startp, unsigned int *endp)
646 unsigned int start = *startp, end = *endp;
647 int retval = 0;
649 down_write(&current->mm->mmap_sem);
651 if (end < PAGE_ALIGN(start)) {
652 retval = __ia32_compare_pp(start, end);
653 if (retval == 0) {
654 *startp = PAGE_START(start);
655 *endp = PAGE_ALIGN(end);
657 } else {
658 if (offset_in_page(start)) {
659 retval = __ia32_compare_pp(start,
660 PAGE_ALIGN(start));
661 if (retval == 0)
662 *startp = PAGE_START(start);
663 if (retval < 0)
664 goto out;
666 if (offset_in_page(end)) {
667 retval = __ia32_compare_pp(PAGE_START(end), end);
668 if (retval == 0)
669 *endp = PAGE_ALIGN(end);
673 out:
674 up_write(&current->mm->mmap_sem);
675 return retval;
678 static void
679 __ia32_drop_pp_list(struct ia64_partial_page_list *ppl)
681 struct ia64_partial_page *pp = ppl->pp_head;
683 while (pp) {
684 struct ia64_partial_page *next = pp->next;
685 kmem_cache_free(ia64_partial_page_cachep, pp);
686 pp = next;
689 kfree(ppl);
692 void
693 ia32_drop_ia64_partial_page_list(struct task_struct *task)
695 struct ia64_partial_page_list* ppl = task->thread.ppl;
697 if (ppl && atomic_dec_and_test(&ppl->pp_count))
698 __ia32_drop_pp_list(ppl);
702 * Copy current->thread.ppl to ppl (already initialized).
704 static int
705 __ia32_copy_pp_list(struct ia64_partial_page_list *ppl)
707 struct ia64_partial_page *pp, *tmp, *prev;
708 struct rb_node **rb_link, *rb_parent;
710 ppl->pp_head = NULL;
711 ppl->pp_hint = NULL;
712 ppl->ppl_rb = RB_ROOT;
713 rb_link = &ppl->ppl_rb.rb_node;
714 rb_parent = NULL;
715 prev = NULL;
717 for (pp = current->thread.ppl->pp_head; pp; pp = pp->next) {
718 tmp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
719 if (!tmp)
720 return -ENOMEM;
721 *tmp = *pp;
722 __ia32_insert_pp(ppl, tmp, prev, rb_link, rb_parent);
723 prev = tmp;
724 rb_link = &tmp->pp_rb.rb_right;
725 rb_parent = &tmp->pp_rb;
727 return 0;
731 ia32_copy_ia64_partial_page_list(struct task_struct *p,
732 unsigned long clone_flags)
734 int retval = 0;
736 if (clone_flags & CLONE_VM) {
737 atomic_inc(&current->thread.ppl->pp_count);
738 p->thread.ppl = current->thread.ppl;
739 } else {
740 p->thread.ppl = ia32_init_pp_list();
741 if (!p->thread.ppl)
742 return -ENOMEM;
743 down_write(&current->mm->mmap_sem);
745 retval = __ia32_copy_pp_list(p->thread.ppl);
747 up_write(&current->mm->mmap_sem);
750 return retval;
753 static unsigned long
754 emulate_mmap (struct file *file, unsigned long start, unsigned long len, int prot, int flags,
755 loff_t off)
757 unsigned long tmp, end, pend, pstart, ret, is_congruent, fudge = 0;
758 struct inode *inode;
759 loff_t poff;
761 end = start + len;
762 pstart = PAGE_START(start);
763 pend = PAGE_ALIGN(end);
765 if (flags & MAP_FIXED) {
766 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
767 if (start > pstart) {
768 if (flags & MAP_SHARED)
769 printk(KERN_INFO
770 "%s(%d): emulate_mmap() can't share head (addr=0x%lx)\n",
771 current->comm, task_pid_nr(current), start);
772 ret = mmap_subpage(file, start, min(PAGE_ALIGN(start), end), prot, flags,
773 off);
774 if (IS_ERR((void *) ret))
775 return ret;
776 pstart += PAGE_SIZE;
777 if (pstart >= pend)
778 goto out; /* done */
780 if (end < pend) {
781 if (flags & MAP_SHARED)
782 printk(KERN_INFO
783 "%s(%d): emulate_mmap() can't share tail (end=0x%lx)\n",
784 current->comm, task_pid_nr(current), end);
785 ret = mmap_subpage(file, max(start, PAGE_START(end)), end, prot, flags,
786 (off + len) - offset_in_page(end));
787 if (IS_ERR((void *) ret))
788 return ret;
789 pend -= PAGE_SIZE;
790 if (pstart >= pend)
791 goto out; /* done */
793 } else {
795 * If a start address was specified, use it if the entire rounded out area
796 * is available.
798 if (start && !pstart)
799 fudge = 1; /* handle case of mapping to range (0,PAGE_SIZE) */
800 tmp = arch_get_unmapped_area(file, pstart - fudge, pend - pstart, 0, flags);
801 if (tmp != pstart) {
802 pstart = tmp;
803 start = pstart + offset_in_page(off); /* make start congruent with off */
804 end = start + len;
805 pend = PAGE_ALIGN(end);
809 poff = off + (pstart - start); /* note: (pstart - start) may be negative */
810 is_congruent = (flags & MAP_ANONYMOUS) || (offset_in_page(poff) == 0);
812 if ((flags & MAP_SHARED) && !is_congruent)
813 printk(KERN_INFO "%s(%d): emulate_mmap() can't share contents of incongruent mmap "
814 "(addr=0x%lx,off=0x%llx)\n", current->comm, task_pid_nr(current), start, off);
816 DBG("mmap_body: mapping [0x%lx-0x%lx) %s with poff 0x%llx\n", pstart, pend,
817 is_congruent ? "congruent" : "not congruent", poff);
819 down_write(&current->mm->mmap_sem);
821 if (!(flags & MAP_ANONYMOUS) && is_congruent)
822 ret = do_mmap(file, pstart, pend - pstart, prot, flags | MAP_FIXED, poff);
823 else
824 ret = do_mmap(NULL, pstart, pend - pstart,
825 prot | ((flags & MAP_ANONYMOUS) ? 0 : PROT_WRITE),
826 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
828 up_write(&current->mm->mmap_sem);
830 if (IS_ERR((void *) ret))
831 return ret;
833 if (!is_congruent) {
834 /* read the file contents */
835 inode = file->f_path.dentry->d_inode;
836 if (!inode->i_fop || !file->f_op->read
837 || ((*file->f_op->read)(file, (char __user *) pstart, pend - pstart, &poff)
838 < 0))
840 sys_munmap(pstart, pend - pstart);
841 return -EINVAL;
843 if (!(prot & PROT_WRITE) && sys_mprotect(pstart, pend - pstart, prot) < 0)
844 return -EINVAL;
847 if (!(flags & MAP_FIXED))
848 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
849 out:
850 return start;
853 #endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
855 static inline unsigned int
856 get_prot32 (unsigned int prot)
858 if (prot & PROT_WRITE)
859 /* on x86, PROT_WRITE implies PROT_READ which implies PROT_EEC */
860 prot |= PROT_READ | PROT_WRITE | PROT_EXEC;
861 else if (prot & (PROT_READ | PROT_EXEC))
862 /* on x86, there is no distinction between PROT_READ and PROT_EXEC */
863 prot |= (PROT_READ | PROT_EXEC);
865 return prot;
868 unsigned long
869 ia32_do_mmap (struct file *file, unsigned long addr, unsigned long len, int prot, int flags,
870 loff_t offset)
872 DBG("ia32_do_mmap(file=%p,addr=0x%lx,len=0x%lx,prot=%x,flags=%x,offset=0x%llx)\n",
873 file, addr, len, prot, flags, offset);
875 if (file && (!file->f_op || !file->f_op->mmap))
876 return -ENODEV;
878 len = IA32_PAGE_ALIGN(len);
879 if (len == 0)
880 return addr;
882 if (len > IA32_PAGE_OFFSET || addr > IA32_PAGE_OFFSET - len)
884 if (flags & MAP_FIXED)
885 return -ENOMEM;
886 else
887 return -EINVAL;
890 if (OFFSET4K(offset))
891 return -EINVAL;
893 prot = get_prot32(prot);
895 #if PAGE_SHIFT > IA32_PAGE_SHIFT
896 mutex_lock(&ia32_mmap_mutex);
898 addr = emulate_mmap(file, addr, len, prot, flags, offset);
900 mutex_unlock(&ia32_mmap_mutex);
901 #else
902 down_write(&current->mm->mmap_sem);
904 addr = do_mmap(file, addr, len, prot, flags, offset);
906 up_write(&current->mm->mmap_sem);
907 #endif
908 DBG("ia32_do_mmap: returning 0x%lx\n", addr);
909 return addr;
913 * Linux/i386 didn't use to be able to handle more than 4 system call parameters, so these
914 * system calls used a memory block for parameter passing..
917 struct mmap_arg_struct {
918 unsigned int addr;
919 unsigned int len;
920 unsigned int prot;
921 unsigned int flags;
922 unsigned int fd;
923 unsigned int offset;
926 asmlinkage long
927 sys32_mmap (struct mmap_arg_struct __user *arg)
929 struct mmap_arg_struct a;
930 struct file *file = NULL;
931 unsigned long addr;
932 int flags;
934 if (copy_from_user(&a, arg, sizeof(a)))
935 return -EFAULT;
937 if (OFFSET4K(a.offset))
938 return -EINVAL;
940 flags = a.flags;
942 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
943 if (!(flags & MAP_ANONYMOUS)) {
944 file = fget(a.fd);
945 if (!file)
946 return -EBADF;
949 addr = ia32_do_mmap(file, a.addr, a.len, a.prot, flags, a.offset);
951 if (file)
952 fput(file);
953 return addr;
956 asmlinkage long
957 sys32_mmap2 (unsigned int addr, unsigned int len, unsigned int prot, unsigned int flags,
958 unsigned int fd, unsigned int pgoff)
960 struct file *file = NULL;
961 unsigned long retval;
963 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
964 if (!(flags & MAP_ANONYMOUS)) {
965 file = fget(fd);
966 if (!file)
967 return -EBADF;
970 retval = ia32_do_mmap(file, addr, len, prot, flags,
971 (unsigned long) pgoff << IA32_PAGE_SHIFT);
973 if (file)
974 fput(file);
975 return retval;
978 asmlinkage long
979 sys32_munmap (unsigned int start, unsigned int len)
981 unsigned int end = start + len;
982 long ret;
984 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
985 ret = sys_munmap(start, end - start);
986 #else
987 if (OFFSET4K(start))
988 return -EINVAL;
990 end = IA32_PAGE_ALIGN(end);
991 if (start >= end)
992 return -EINVAL;
994 ret = ia32_unset_pp(&start, &end);
995 if (ret < 0)
996 return ret;
998 if (start >= end)
999 return 0;
1001 mutex_lock(&ia32_mmap_mutex);
1002 ret = sys_munmap(start, end - start);
1003 mutex_unlock(&ia32_mmap_mutex);
1004 #endif
1005 return ret;
1008 #if PAGE_SHIFT > IA32_PAGE_SHIFT
1011 * When mprotect()ing a partial page, we set the permission to the union of the old
1012 * settings and the new settings. In other words, it's only possible to make access to a
1013 * partial page less restrictive.
1015 static long
1016 mprotect_subpage (unsigned long address, int new_prot)
1018 int old_prot;
1019 struct vm_area_struct *vma;
1021 if (new_prot == PROT_NONE)
1022 return 0; /* optimize case where nothing changes... */
1023 vma = find_vma(current->mm, address);
1024 old_prot = get_page_prot(vma, address);
1025 return sys_mprotect(address, PAGE_SIZE, new_prot | old_prot);
1028 #endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
1030 asmlinkage long
1031 sys32_mprotect (unsigned int start, unsigned int len, int prot)
1033 unsigned int end = start + len;
1034 #if PAGE_SHIFT > IA32_PAGE_SHIFT
1035 long retval = 0;
1036 #endif
1038 prot = get_prot32(prot);
1040 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
1041 return sys_mprotect(start, end - start, prot);
1042 #else
1043 if (OFFSET4K(start))
1044 return -EINVAL;
1046 end = IA32_PAGE_ALIGN(end);
1047 if (end < start)
1048 return -EINVAL;
1050 retval = ia32_compare_pp(&start, &end);
1052 if (retval < 0)
1053 return retval;
1055 mutex_lock(&ia32_mmap_mutex);
1057 if (offset_in_page(start)) {
1058 /* start address is 4KB aligned but not page aligned. */
1059 retval = mprotect_subpage(PAGE_START(start), prot);
1060 if (retval < 0)
1061 goto out;
1063 start = PAGE_ALIGN(start);
1064 if (start >= end)
1065 goto out; /* retval is already zero... */
1068 if (offset_in_page(end)) {
1069 /* end address is 4KB aligned but not page aligned. */
1070 retval = mprotect_subpage(PAGE_START(end), prot);
1071 if (retval < 0)
1072 goto out;
1074 end = PAGE_START(end);
1076 retval = sys_mprotect(start, end - start, prot);
1078 out:
1079 mutex_unlock(&ia32_mmap_mutex);
1080 return retval;
1081 #endif
1084 asmlinkage long
1085 sys32_mremap (unsigned int addr, unsigned int old_len, unsigned int new_len,
1086 unsigned int flags, unsigned int new_addr)
1088 long ret;
1090 #if PAGE_SHIFT <= IA32_PAGE_SHIFT
1091 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1092 #else
1093 unsigned int old_end, new_end;
1095 if (OFFSET4K(addr))
1096 return -EINVAL;
1098 old_len = IA32_PAGE_ALIGN(old_len);
1099 new_len = IA32_PAGE_ALIGN(new_len);
1100 old_end = addr + old_len;
1101 new_end = addr + new_len;
1103 if (!new_len)
1104 return -EINVAL;
1106 if ((flags & MREMAP_FIXED) && (OFFSET4K(new_addr)))
1107 return -EINVAL;
1109 if (old_len >= new_len) {
1110 ret = sys32_munmap(addr + new_len, old_len - new_len);
1111 if (ret && old_len != new_len)
1112 return ret;
1113 ret = addr;
1114 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
1115 return ret;
1116 old_len = new_len;
1119 addr = PAGE_START(addr);
1120 old_len = PAGE_ALIGN(old_end) - addr;
1121 new_len = PAGE_ALIGN(new_end) - addr;
1123 mutex_lock(&ia32_mmap_mutex);
1124 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1125 mutex_unlock(&ia32_mmap_mutex);
1127 if ((ret >= 0) && (old_len < new_len)) {
1128 /* mremap expanded successfully */
1129 ia32_set_pp(old_end, new_end, flags);
1131 #endif
1132 return ret;
1135 asmlinkage long
1136 sys32_pipe (int __user *fd)
1138 int retval;
1139 int fds[2];
1141 retval = do_pipe(fds);
1142 if (retval)
1143 goto out;
1144 if (copy_to_user(fd, fds, sizeof(fds)))
1145 retval = -EFAULT;
1146 out:
1147 return retval;
1150 static inline long
1151 get_tv32 (struct timeval *o, struct compat_timeval __user *i)
1153 return (!access_ok(VERIFY_READ, i, sizeof(*i)) ||
1154 (__get_user(o->tv_sec, &i->tv_sec) | __get_user(o->tv_usec, &i->tv_usec)));
1157 static inline long
1158 put_tv32 (struct compat_timeval __user *o, struct timeval *i)
1160 return (!access_ok(VERIFY_WRITE, o, sizeof(*o)) ||
1161 (__put_user(i->tv_sec, &o->tv_sec) | __put_user(i->tv_usec, &o->tv_usec)));
1164 asmlinkage unsigned long
1165 sys32_alarm (unsigned int seconds)
1167 return alarm_setitimer(seconds);
1170 /* Translations due to time_t size differences. Which affects all
1171 sorts of things, like timeval and itimerval. */
1173 extern struct timezone sys_tz;
1175 asmlinkage long
1176 sys32_gettimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1178 if (tv) {
1179 struct timeval ktv;
1180 do_gettimeofday(&ktv);
1181 if (put_tv32(tv, &ktv))
1182 return -EFAULT;
1184 if (tz) {
1185 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
1186 return -EFAULT;
1188 return 0;
1191 asmlinkage long
1192 sys32_settimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1194 struct timeval ktv;
1195 struct timespec kts;
1196 struct timezone ktz;
1198 if (tv) {
1199 if (get_tv32(&ktv, tv))
1200 return -EFAULT;
1201 kts.tv_sec = ktv.tv_sec;
1202 kts.tv_nsec = ktv.tv_usec * 1000;
1204 if (tz) {
1205 if (copy_from_user(&ktz, tz, sizeof(ktz)))
1206 return -EFAULT;
1209 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
1212 struct getdents32_callback {
1213 struct compat_dirent __user *current_dir;
1214 struct compat_dirent __user *previous;
1215 int count;
1216 int error;
1219 struct readdir32_callback {
1220 struct old_linux32_dirent __user * dirent;
1221 int count;
1224 static int
1225 filldir32 (void *__buf, const char *name, int namlen, loff_t offset, u64 ino,
1226 unsigned int d_type)
1228 struct compat_dirent __user * dirent;
1229 struct getdents32_callback * buf = (struct getdents32_callback *) __buf;
1230 int reclen = ROUND_UP(offsetof(struct compat_dirent, d_name) + namlen + 1, 4);
1231 u32 d_ino;
1233 buf->error = -EINVAL; /* only used if we fail.. */
1234 if (reclen > buf->count)
1235 return -EINVAL;
1236 d_ino = ino;
1237 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1238 return -EOVERFLOW;
1239 buf->error = -EFAULT; /* only used if we fail.. */
1240 dirent = buf->previous;
1241 if (dirent)
1242 if (put_user(offset, &dirent->d_off))
1243 return -EFAULT;
1244 dirent = buf->current_dir;
1245 buf->previous = dirent;
1246 if (put_user(d_ino, &dirent->d_ino)
1247 || put_user(reclen, &dirent->d_reclen)
1248 || copy_to_user(dirent->d_name, name, namlen)
1249 || put_user(0, dirent->d_name + namlen))
1250 return -EFAULT;
1251 dirent = (struct compat_dirent __user *) ((char __user *) dirent + reclen);
1252 buf->current_dir = dirent;
1253 buf->count -= reclen;
1254 return 0;
1257 asmlinkage long
1258 sys32_getdents (unsigned int fd, struct compat_dirent __user *dirent, unsigned int count)
1260 struct file * file;
1261 struct compat_dirent __user * lastdirent;
1262 struct getdents32_callback buf;
1263 int error;
1265 error = -EFAULT;
1266 if (!access_ok(VERIFY_WRITE, dirent, count))
1267 goto out;
1269 error = -EBADF;
1270 file = fget(fd);
1271 if (!file)
1272 goto out;
1274 buf.current_dir = dirent;
1275 buf.previous = NULL;
1276 buf.count = count;
1277 buf.error = 0;
1279 error = vfs_readdir(file, filldir32, &buf);
1280 if (error < 0)
1281 goto out_putf;
1282 error = buf.error;
1283 lastdirent = buf.previous;
1284 if (lastdirent) {
1285 if (put_user(file->f_pos, &lastdirent->d_off))
1286 error = -EFAULT;
1287 else
1288 error = count - buf.count;
1291 out_putf:
1292 fput(file);
1293 out:
1294 return error;
1297 static int
1298 fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, u64 ino,
1299 unsigned int d_type)
1301 struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
1302 struct old_linux32_dirent __user * dirent;
1303 u32 d_ino;
1305 if (buf->count)
1306 return -EINVAL;
1307 d_ino = ino;
1308 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1309 return -EOVERFLOW;
1310 buf->count++;
1311 dirent = buf->dirent;
1312 if (put_user(d_ino, &dirent->d_ino)
1313 || put_user(offset, &dirent->d_offset)
1314 || put_user(namlen, &dirent->d_namlen)
1315 || copy_to_user(dirent->d_name, name, namlen)
1316 || put_user(0, dirent->d_name + namlen))
1317 return -EFAULT;
1318 return 0;
1321 asmlinkage long
1322 sys32_readdir (unsigned int fd, void __user *dirent, unsigned int count)
1324 int error;
1325 struct file * file;
1326 struct readdir32_callback buf;
1328 error = -EBADF;
1329 file = fget(fd);
1330 if (!file)
1331 goto out;
1333 buf.count = 0;
1334 buf.dirent = dirent;
1336 error = vfs_readdir(file, fillonedir32, &buf);
1337 if (error >= 0)
1338 error = buf.count;
1339 fput(file);
1340 out:
1341 return error;
1344 struct sel_arg_struct {
1345 unsigned int n;
1346 unsigned int inp;
1347 unsigned int outp;
1348 unsigned int exp;
1349 unsigned int tvp;
1352 asmlinkage long
1353 sys32_old_select (struct sel_arg_struct __user *arg)
1355 struct sel_arg_struct a;
1357 if (copy_from_user(&a, arg, sizeof(a)))
1358 return -EFAULT;
1359 return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1360 compat_ptr(a.exp), compat_ptr(a.tvp));
1363 #define SEMOP 1
1364 #define SEMGET 2
1365 #define SEMCTL 3
1366 #define SEMTIMEDOP 4
1367 #define MSGSND 11
1368 #define MSGRCV 12
1369 #define MSGGET 13
1370 #define MSGCTL 14
1371 #define SHMAT 21
1372 #define SHMDT 22
1373 #define SHMGET 23
1374 #define SHMCTL 24
1376 asmlinkage long
1377 sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
1379 int version;
1381 version = call >> 16; /* hack for backward compatibility */
1382 call &= 0xffff;
1384 switch (call) {
1385 case SEMTIMEDOP:
1386 if (fifth)
1387 return compat_sys_semtimedop(first, compat_ptr(ptr),
1388 second, compat_ptr(fifth));
1389 /* else fall through for normal semop() */
1390 case SEMOP:
1391 /* struct sembuf is the same on 32 and 64bit :)) */
1392 return sys_semtimedop(first, compat_ptr(ptr), second,
1393 NULL);
1394 case SEMGET:
1395 return sys_semget(first, second, third);
1396 case SEMCTL:
1397 return compat_sys_semctl(first, second, third, compat_ptr(ptr));
1399 case MSGSND:
1400 return compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
1401 case MSGRCV:
1402 return compat_sys_msgrcv(first, second, fifth, third, version, compat_ptr(ptr));
1403 case MSGGET:
1404 return sys_msgget((key_t) first, second);
1405 case MSGCTL:
1406 return compat_sys_msgctl(first, second, compat_ptr(ptr));
1408 case SHMAT:
1409 return compat_sys_shmat(first, second, third, version, compat_ptr(ptr));
1410 break;
1411 case SHMDT:
1412 return sys_shmdt(compat_ptr(ptr));
1413 case SHMGET:
1414 return sys_shmget(first, (unsigned)second, third);
1415 case SHMCTL:
1416 return compat_sys_shmctl(first, second, compat_ptr(ptr));
1418 default:
1419 return -ENOSYS;
1421 return -EINVAL;
1424 asmlinkage long
1425 compat_sys_wait4 (compat_pid_t pid, compat_uint_t * stat_addr, int options,
1426 struct compat_rusage *ru);
1428 asmlinkage long
1429 sys32_waitpid (int pid, unsigned int *stat_addr, int options)
1431 return compat_sys_wait4(pid, stat_addr, options, NULL);
1434 static unsigned int
1435 ia32_peek (struct task_struct *child, unsigned long addr, unsigned int *val)
1437 size_t copied;
1438 unsigned int ret;
1440 copied = access_process_vm(child, addr, val, sizeof(*val), 0);
1441 return (copied != sizeof(ret)) ? -EIO : 0;
1444 static unsigned int
1445 ia32_poke (struct task_struct *child, unsigned long addr, unsigned int val)
1448 if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val))
1449 return -EIO;
1450 return 0;
1454 * The order in which registers are stored in the ptrace regs structure
1456 #define PT_EBX 0
1457 #define PT_ECX 1
1458 #define PT_EDX 2
1459 #define PT_ESI 3
1460 #define PT_EDI 4
1461 #define PT_EBP 5
1462 #define PT_EAX 6
1463 #define PT_DS 7
1464 #define PT_ES 8
1465 #define PT_FS 9
1466 #define PT_GS 10
1467 #define PT_ORIG_EAX 11
1468 #define PT_EIP 12
1469 #define PT_CS 13
1470 #define PT_EFL 14
1471 #define PT_UESP 15
1472 #define PT_SS 16
1474 static unsigned int
1475 getreg (struct task_struct *child, int regno)
1477 struct pt_regs *child_regs;
1479 child_regs = task_pt_regs(child);
1480 switch (regno / sizeof(int)) {
1481 case PT_EBX: return child_regs->r11;
1482 case PT_ECX: return child_regs->r9;
1483 case PT_EDX: return child_regs->r10;
1484 case PT_ESI: return child_regs->r14;
1485 case PT_EDI: return child_regs->r15;
1486 case PT_EBP: return child_regs->r13;
1487 case PT_EAX: return child_regs->r8;
1488 case PT_ORIG_EAX: return child_regs->r1; /* see dispatch_to_ia32_handler() */
1489 case PT_EIP: return child_regs->cr_iip;
1490 case PT_UESP: return child_regs->r12;
1491 case PT_EFL: return child->thread.eflag;
1492 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1493 return __USER_DS;
1494 case PT_CS: return __USER_CS;
1495 default:
1496 printk(KERN_ERR "ia32.getreg(): unknown register %d\n", regno);
1497 break;
1499 return 0;
1502 static void
1503 putreg (struct task_struct *child, int regno, unsigned int value)
1505 struct pt_regs *child_regs;
1507 child_regs = task_pt_regs(child);
1508 switch (regno / sizeof(int)) {
1509 case PT_EBX: child_regs->r11 = value; break;
1510 case PT_ECX: child_regs->r9 = value; break;
1511 case PT_EDX: child_regs->r10 = value; break;
1512 case PT_ESI: child_regs->r14 = value; break;
1513 case PT_EDI: child_regs->r15 = value; break;
1514 case PT_EBP: child_regs->r13 = value; break;
1515 case PT_EAX: child_regs->r8 = value; break;
1516 case PT_ORIG_EAX: child_regs->r1 = value; break;
1517 case PT_EIP: child_regs->cr_iip = value; break;
1518 case PT_UESP: child_regs->r12 = value; break;
1519 case PT_EFL: child->thread.eflag = value; break;
1520 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1521 if (value != __USER_DS)
1522 printk(KERN_ERR
1523 "ia32.putreg: attempt to set invalid segment register %d = %x\n",
1524 regno, value);
1525 break;
1526 case PT_CS:
1527 if (value != __USER_CS)
1528 printk(KERN_ERR
1529 "ia32.putreg: attempt to to set invalid segment register %d = %x\n",
1530 regno, value);
1531 break;
1532 default:
1533 printk(KERN_ERR "ia32.putreg: unknown register %d\n", regno);
1534 break;
1538 static void
1539 put_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1540 struct switch_stack *swp, int tos)
1542 struct _fpreg_ia32 *f;
1543 char buf[32];
1545 f = (struct _fpreg_ia32 *)(((unsigned long)buf + 15) & ~15);
1546 if ((regno += tos) >= 8)
1547 regno -= 8;
1548 switch (regno) {
1549 case 0:
1550 ia64f2ia32f(f, &ptp->f8);
1551 break;
1552 case 1:
1553 ia64f2ia32f(f, &ptp->f9);
1554 break;
1555 case 2:
1556 ia64f2ia32f(f, &ptp->f10);
1557 break;
1558 case 3:
1559 ia64f2ia32f(f, &ptp->f11);
1560 break;
1561 case 4:
1562 case 5:
1563 case 6:
1564 case 7:
1565 ia64f2ia32f(f, &swp->f12 + (regno - 4));
1566 break;
1568 copy_to_user(reg, f, sizeof(*reg));
1571 static void
1572 get_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1573 struct switch_stack *swp, int tos)
1576 if ((regno += tos) >= 8)
1577 regno -= 8;
1578 switch (regno) {
1579 case 0:
1580 copy_from_user(&ptp->f8, reg, sizeof(*reg));
1581 break;
1582 case 1:
1583 copy_from_user(&ptp->f9, reg, sizeof(*reg));
1584 break;
1585 case 2:
1586 copy_from_user(&ptp->f10, reg, sizeof(*reg));
1587 break;
1588 case 3:
1589 copy_from_user(&ptp->f11, reg, sizeof(*reg));
1590 break;
1591 case 4:
1592 case 5:
1593 case 6:
1594 case 7:
1595 copy_from_user(&swp->f12 + (regno - 4), reg, sizeof(*reg));
1596 break;
1598 return;
1602 save_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1604 struct switch_stack *swp;
1605 struct pt_regs *ptp;
1606 int i, tos;
1608 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1609 return -EFAULT;
1611 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1612 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1613 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1614 __put_user(tsk->thread.fir, &save->fip);
1615 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1616 __put_user(tsk->thread.fdr, &save->foo);
1617 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1620 * Stack frames start with 16-bytes of temp space
1622 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1623 ptp = task_pt_regs(tsk);
1624 tos = (tsk->thread.fsr >> 11) & 7;
1625 for (i = 0; i < 8; i++)
1626 put_fpreg(i, &save->st_space[i], ptp, swp, tos);
1627 return 0;
1630 static int
1631 restore_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1633 struct switch_stack *swp;
1634 struct pt_regs *ptp;
1635 int i, tos;
1636 unsigned int fsrlo, fsrhi, num32;
1638 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1639 return(-EFAULT);
1641 __get_user(num32, (unsigned int __user *)&save->cwd);
1642 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1643 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1644 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1645 num32 = (fsrhi << 16) | fsrlo;
1646 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1647 __get_user(num32, (unsigned int __user *)&save->fip);
1648 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1649 __get_user(num32, (unsigned int __user *)&save->foo);
1650 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1653 * Stack frames start with 16-bytes of temp space
1655 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1656 ptp = task_pt_regs(tsk);
1657 tos = (tsk->thread.fsr >> 11) & 7;
1658 for (i = 0; i < 8; i++)
1659 get_fpreg(i, &save->st_space[i], ptp, swp, tos);
1660 return 0;
1664 save_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1666 struct switch_stack *swp;
1667 struct pt_regs *ptp;
1668 int i, tos;
1669 unsigned long mxcsr=0;
1670 unsigned long num128[2];
1672 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1673 return -EFAULT;
1675 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1676 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1677 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1678 __put_user(tsk->thread.fir, &save->fip);
1679 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1680 __put_user(tsk->thread.fdr, &save->foo);
1681 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1684 * Stack frames start with 16-bytes of temp space
1686 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1687 ptp = task_pt_regs(tsk);
1688 tos = (tsk->thread.fsr >> 11) & 7;
1689 for (i = 0; i < 8; i++)
1690 put_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1692 mxcsr = ((tsk->thread.fcr>>32) & 0xff80) | ((tsk->thread.fsr>>32) & 0x3f);
1693 __put_user(mxcsr & 0xffff, &save->mxcsr);
1694 for (i = 0; i < 8; i++) {
1695 memcpy(&(num128[0]), &(swp->f16) + i*2, sizeof(unsigned long));
1696 memcpy(&(num128[1]), &(swp->f17) + i*2, sizeof(unsigned long));
1697 copy_to_user(&save->xmm_space[0] + 4*i, num128, sizeof(struct _xmmreg_ia32));
1699 return 0;
1702 static int
1703 restore_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1705 struct switch_stack *swp;
1706 struct pt_regs *ptp;
1707 int i, tos;
1708 unsigned int fsrlo, fsrhi, num32;
1709 int mxcsr;
1710 unsigned long num64;
1711 unsigned long num128[2];
1713 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1714 return(-EFAULT);
1716 __get_user(num32, (unsigned int __user *)&save->cwd);
1717 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1718 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1719 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1720 num32 = (fsrhi << 16) | fsrlo;
1721 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1722 __get_user(num32, (unsigned int __user *)&save->fip);
1723 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1724 __get_user(num32, (unsigned int __user *)&save->foo);
1725 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1728 * Stack frames start with 16-bytes of temp space
1730 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
1731 ptp = task_pt_regs(tsk);
1732 tos = (tsk->thread.fsr >> 11) & 7;
1733 for (i = 0; i < 8; i++)
1734 get_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1736 __get_user(mxcsr, (unsigned int __user *)&save->mxcsr);
1737 num64 = mxcsr & 0xff10;
1738 tsk->thread.fcr = (tsk->thread.fcr & (~0xff1000000000UL)) | (num64<<32);
1739 num64 = mxcsr & 0x3f;
1740 tsk->thread.fsr = (tsk->thread.fsr & (~0x3f00000000UL)) | (num64<<32);
1742 for (i = 0; i < 8; i++) {
1743 copy_from_user(num128, &save->xmm_space[0] + 4*i, sizeof(struct _xmmreg_ia32));
1744 memcpy(&(swp->f16) + i*2, &(num128[0]), sizeof(unsigned long));
1745 memcpy(&(swp->f17) + i*2, &(num128[1]), sizeof(unsigned long));
1747 return 0;
1750 asmlinkage long
1751 sys32_ptrace (int request, pid_t pid, unsigned int addr, unsigned int data)
1753 struct task_struct *child;
1754 unsigned int value, tmp;
1755 long i, ret;
1757 lock_kernel();
1758 if (request == PTRACE_TRACEME) {
1759 ret = ptrace_traceme();
1760 goto out;
1763 child = ptrace_get_task_struct(pid);
1764 if (IS_ERR(child)) {
1765 ret = PTR_ERR(child);
1766 goto out;
1769 if (request == PTRACE_ATTACH) {
1770 ret = sys_ptrace(request, pid, addr, data);
1771 goto out_tsk;
1774 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1775 if (ret < 0)
1776 goto out_tsk;
1778 switch (request) {
1779 case PTRACE_PEEKTEXT:
1780 case PTRACE_PEEKDATA: /* read word at location addr */
1781 ret = ia32_peek(child, addr, &value);
1782 if (ret == 0)
1783 ret = put_user(value, (unsigned int __user *) compat_ptr(data));
1784 else
1785 ret = -EIO;
1786 goto out_tsk;
1788 case PTRACE_POKETEXT:
1789 case PTRACE_POKEDATA: /* write the word at location addr */
1790 ret = ia32_poke(child, addr, data);
1791 goto out_tsk;
1793 case PTRACE_PEEKUSR: /* read word at addr in USER area */
1794 ret = -EIO;
1795 if ((addr & 3) || addr > 17*sizeof(int))
1796 break;
1798 tmp = getreg(child, addr);
1799 if (!put_user(tmp, (unsigned int __user *) compat_ptr(data)))
1800 ret = 0;
1801 break;
1803 case PTRACE_POKEUSR: /* write word at addr in USER area */
1804 ret = -EIO;
1805 if ((addr & 3) || addr > 17*sizeof(int))
1806 break;
1808 putreg(child, addr, data);
1809 ret = 0;
1810 break;
1812 case IA32_PTRACE_GETREGS:
1813 if (!access_ok(VERIFY_WRITE, compat_ptr(data), 17*sizeof(int))) {
1814 ret = -EIO;
1815 break;
1817 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1818 put_user(getreg(child, i), (unsigned int __user *) compat_ptr(data));
1819 data += sizeof(int);
1821 ret = 0;
1822 break;
1824 case IA32_PTRACE_SETREGS:
1825 if (!access_ok(VERIFY_READ, compat_ptr(data), 17*sizeof(int))) {
1826 ret = -EIO;
1827 break;
1829 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1830 get_user(tmp, (unsigned int __user *) compat_ptr(data));
1831 putreg(child, i, tmp);
1832 data += sizeof(int);
1834 ret = 0;
1835 break;
1837 case IA32_PTRACE_GETFPREGS:
1838 ret = save_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1839 compat_ptr(data));
1840 break;
1842 case IA32_PTRACE_GETFPXREGS:
1843 ret = save_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1844 compat_ptr(data));
1845 break;
1847 case IA32_PTRACE_SETFPREGS:
1848 ret = restore_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1849 compat_ptr(data));
1850 break;
1852 case IA32_PTRACE_SETFPXREGS:
1853 ret = restore_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1854 compat_ptr(data));
1855 break;
1857 case PTRACE_GETEVENTMSG:
1858 ret = put_user(child->ptrace_message, (unsigned int __user *) compat_ptr(data));
1859 break;
1861 case PTRACE_SYSCALL: /* continue, stop after next syscall */
1862 case PTRACE_CONT: /* restart after signal. */
1863 case PTRACE_KILL:
1864 case PTRACE_SINGLESTEP: /* execute chile for one instruction */
1865 case PTRACE_DETACH: /* detach a process */
1866 ret = sys_ptrace(request, pid, addr, data);
1867 break;
1869 default:
1870 ret = ptrace_request(child, request, addr, data);
1871 break;
1874 out_tsk:
1875 put_task_struct(child);
1876 out:
1877 unlock_kernel();
1878 return ret;
1881 typedef struct {
1882 unsigned int ss_sp;
1883 unsigned int ss_flags;
1884 unsigned int ss_size;
1885 } ia32_stack_t;
1887 asmlinkage long
1888 sys32_sigaltstack (ia32_stack_t __user *uss32, ia32_stack_t __user *uoss32,
1889 long arg2, long arg3, long arg4, long arg5, long arg6,
1890 long arg7, struct pt_regs pt)
1892 stack_t uss, uoss;
1893 ia32_stack_t buf32;
1894 int ret;
1895 mm_segment_t old_fs = get_fs();
1897 if (uss32) {
1898 if (copy_from_user(&buf32, uss32, sizeof(ia32_stack_t)))
1899 return -EFAULT;
1900 uss.ss_sp = (void __user *) (long) buf32.ss_sp;
1901 uss.ss_flags = buf32.ss_flags;
1902 /* MINSIGSTKSZ is different for ia32 vs ia64. We lie here to pass the
1903 check and set it to the user requested value later */
1904 if ((buf32.ss_flags != SS_DISABLE) && (buf32.ss_size < MINSIGSTKSZ_IA32)) {
1905 ret = -ENOMEM;
1906 goto out;
1908 uss.ss_size = MINSIGSTKSZ;
1910 set_fs(KERNEL_DS);
1911 ret = do_sigaltstack(uss32 ? (stack_t __user *) &uss : NULL,
1912 (stack_t __user *) &uoss, pt.r12);
1913 current->sas_ss_size = buf32.ss_size;
1914 set_fs(old_fs);
1915 out:
1916 if (ret < 0)
1917 return(ret);
1918 if (uoss32) {
1919 buf32.ss_sp = (long __user) uoss.ss_sp;
1920 buf32.ss_flags = uoss.ss_flags;
1921 buf32.ss_size = uoss.ss_size;
1922 if (copy_to_user(uoss32, &buf32, sizeof(ia32_stack_t)))
1923 return -EFAULT;
1925 return ret;
1928 asmlinkage int
1929 sys32_pause (void)
1931 current->state = TASK_INTERRUPTIBLE;
1932 schedule();
1933 return -ERESTARTNOHAND;
1936 asmlinkage int
1937 sys32_msync (unsigned int start, unsigned int len, int flags)
1939 unsigned int addr;
1941 if (OFFSET4K(start))
1942 return -EINVAL;
1943 addr = PAGE_START(start);
1944 return sys_msync(addr, len + (start - addr), flags);
1947 struct sysctl32 {
1948 unsigned int name;
1949 int nlen;
1950 unsigned int oldval;
1951 unsigned int oldlenp;
1952 unsigned int newval;
1953 unsigned int newlen;
1954 unsigned int __unused[4];
1957 #ifdef CONFIG_SYSCTL_SYSCALL
1958 asmlinkage long
1959 sys32_sysctl (struct sysctl32 __user *args)
1961 struct sysctl32 a32;
1962 mm_segment_t old_fs = get_fs ();
1963 void __user *oldvalp, *newvalp;
1964 size_t oldlen;
1965 int __user *namep;
1966 long ret;
1968 if (copy_from_user(&a32, args, sizeof(a32)))
1969 return -EFAULT;
1972 * We need to pre-validate these because we have to disable address checking
1973 * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
1974 * user specifying bad addresses here. Well, since we're dealing with 32 bit
1975 * addresses, we KNOW that access_ok() will always succeed, so this is an
1976 * expensive NOP, but so what...
1978 namep = (int __user *) compat_ptr(a32.name);
1979 oldvalp = compat_ptr(a32.oldval);
1980 newvalp = compat_ptr(a32.newval);
1982 if ((oldvalp && get_user(oldlen, (int __user *) compat_ptr(a32.oldlenp)))
1983 || !access_ok(VERIFY_WRITE, namep, 0)
1984 || !access_ok(VERIFY_WRITE, oldvalp, 0)
1985 || !access_ok(VERIFY_WRITE, newvalp, 0))
1986 return -EFAULT;
1988 set_fs(KERNEL_DS);
1989 lock_kernel();
1990 ret = do_sysctl(namep, a32.nlen, oldvalp, (size_t __user *) &oldlen,
1991 newvalp, (size_t) a32.newlen);
1992 unlock_kernel();
1993 set_fs(old_fs);
1995 if (oldvalp && put_user (oldlen, (int __user *) compat_ptr(a32.oldlenp)))
1996 return -EFAULT;
1998 return ret;
2000 #endif
2002 asmlinkage long
2003 sys32_newuname (struct new_utsname __user *name)
2005 int ret = sys_newuname(name);
2007 if (!ret)
2008 if (copy_to_user(name->machine, "i686\0\0\0", 8))
2009 ret = -EFAULT;
2010 return ret;
2013 asmlinkage long
2014 sys32_getresuid16 (u16 __user *ruid, u16 __user *euid, u16 __user *suid)
2016 uid_t a, b, c;
2017 int ret;
2018 mm_segment_t old_fs = get_fs();
2020 set_fs(KERNEL_DS);
2021 ret = sys_getresuid((uid_t __user *) &a, (uid_t __user *) &b, (uid_t __user *) &c);
2022 set_fs(old_fs);
2024 if (put_user(a, ruid) || put_user(b, euid) || put_user(c, suid))
2025 return -EFAULT;
2026 return ret;
2029 asmlinkage long
2030 sys32_getresgid16 (u16 __user *rgid, u16 __user *egid, u16 __user *sgid)
2032 gid_t a, b, c;
2033 int ret;
2034 mm_segment_t old_fs = get_fs();
2036 set_fs(KERNEL_DS);
2037 ret = sys_getresgid((gid_t __user *) &a, (gid_t __user *) &b, (gid_t __user *) &c);
2038 set_fs(old_fs);
2040 if (ret)
2041 return ret;
2043 return put_user(a, rgid) | put_user(b, egid) | put_user(c, sgid);
2046 asmlinkage long
2047 sys32_lseek (unsigned int fd, int offset, unsigned int whence)
2049 /* Sign-extension of "offset" is important here... */
2050 return sys_lseek(fd, offset, whence);
2053 static int
2054 groups16_to_user(short __user *grouplist, struct group_info *group_info)
2056 int i;
2057 short group;
2059 for (i = 0; i < group_info->ngroups; i++) {
2060 group = (short)GROUP_AT(group_info, i);
2061 if (put_user(group, grouplist+i))
2062 return -EFAULT;
2065 return 0;
2068 static int
2069 groups16_from_user(struct group_info *group_info, short __user *grouplist)
2071 int i;
2072 short group;
2074 for (i = 0; i < group_info->ngroups; i++) {
2075 if (get_user(group, grouplist+i))
2076 return -EFAULT;
2077 GROUP_AT(group_info, i) = (gid_t)group;
2080 return 0;
2083 asmlinkage long
2084 sys32_getgroups16 (int gidsetsize, short __user *grouplist)
2086 int i;
2088 if (gidsetsize < 0)
2089 return -EINVAL;
2091 get_group_info(current->group_info);
2092 i = current->group_info->ngroups;
2093 if (gidsetsize) {
2094 if (i > gidsetsize) {
2095 i = -EINVAL;
2096 goto out;
2098 if (groups16_to_user(grouplist, current->group_info)) {
2099 i = -EFAULT;
2100 goto out;
2103 out:
2104 put_group_info(current->group_info);
2105 return i;
2108 asmlinkage long
2109 sys32_setgroups16 (int gidsetsize, short __user *grouplist)
2111 struct group_info *group_info;
2112 int retval;
2114 if (!capable(CAP_SETGID))
2115 return -EPERM;
2116 if ((unsigned)gidsetsize > NGROUPS_MAX)
2117 return -EINVAL;
2119 group_info = groups_alloc(gidsetsize);
2120 if (!group_info)
2121 return -ENOMEM;
2122 retval = groups16_from_user(group_info, grouplist);
2123 if (retval) {
2124 put_group_info(group_info);
2125 return retval;
2128 retval = set_current_groups(group_info);
2129 put_group_info(group_info);
2131 return retval;
2134 asmlinkage long
2135 sys32_truncate64 (unsigned int path, unsigned int len_lo, unsigned int len_hi)
2137 return sys_truncate(compat_ptr(path), ((unsigned long) len_hi << 32) | len_lo);
2140 asmlinkage long
2141 sys32_ftruncate64 (int fd, unsigned int len_lo, unsigned int len_hi)
2143 return sys_ftruncate(fd, ((unsigned long) len_hi << 32) | len_lo);
2146 static int
2147 putstat64 (struct stat64 __user *ubuf, struct kstat *kbuf)
2149 int err;
2150 u64 hdev;
2152 if (clear_user(ubuf, sizeof(*ubuf)))
2153 return -EFAULT;
2155 hdev = huge_encode_dev(kbuf->dev);
2156 err = __put_user(hdev, (u32 __user*)&ubuf->st_dev);
2157 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_dev) + 1);
2158 err |= __put_user(kbuf->ino, &ubuf->__st_ino);
2159 err |= __put_user(kbuf->ino, &ubuf->st_ino_lo);
2160 err |= __put_user(kbuf->ino >> 32, &ubuf->st_ino_hi);
2161 err |= __put_user(kbuf->mode, &ubuf->st_mode);
2162 err |= __put_user(kbuf->nlink, &ubuf->st_nlink);
2163 err |= __put_user(kbuf->uid, &ubuf->st_uid);
2164 err |= __put_user(kbuf->gid, &ubuf->st_gid);
2165 hdev = huge_encode_dev(kbuf->rdev);
2166 err = __put_user(hdev, (u32 __user*)&ubuf->st_rdev);
2167 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_rdev) + 1);
2168 err |= __put_user(kbuf->size, &ubuf->st_size_lo);
2169 err |= __put_user((kbuf->size >> 32), &ubuf->st_size_hi);
2170 err |= __put_user(kbuf->atime.tv_sec, &ubuf->st_atime);
2171 err |= __put_user(kbuf->atime.tv_nsec, &ubuf->st_atime_nsec);
2172 err |= __put_user(kbuf->mtime.tv_sec, &ubuf->st_mtime);
2173 err |= __put_user(kbuf->mtime.tv_nsec, &ubuf->st_mtime_nsec);
2174 err |= __put_user(kbuf->ctime.tv_sec, &ubuf->st_ctime);
2175 err |= __put_user(kbuf->ctime.tv_nsec, &ubuf->st_ctime_nsec);
2176 err |= __put_user(kbuf->blksize, &ubuf->st_blksize);
2177 err |= __put_user(kbuf->blocks, &ubuf->st_blocks);
2178 return err;
2181 asmlinkage long
2182 sys32_stat64 (char __user *filename, struct stat64 __user *statbuf)
2184 struct kstat s;
2185 long ret = vfs_stat(filename, &s);
2186 if (!ret)
2187 ret = putstat64(statbuf, &s);
2188 return ret;
2191 asmlinkage long
2192 sys32_lstat64 (char __user *filename, struct stat64 __user *statbuf)
2194 struct kstat s;
2195 long ret = vfs_lstat(filename, &s);
2196 if (!ret)
2197 ret = putstat64(statbuf, &s);
2198 return ret;
2201 asmlinkage long
2202 sys32_fstat64 (unsigned int fd, struct stat64 __user *statbuf)
2204 struct kstat s;
2205 long ret = vfs_fstat(fd, &s);
2206 if (!ret)
2207 ret = putstat64(statbuf, &s);
2208 return ret;
2211 asmlinkage long
2212 sys32_sched_rr_get_interval (pid_t pid, struct compat_timespec __user *interval)
2214 mm_segment_t old_fs = get_fs();
2215 struct timespec t;
2216 long ret;
2218 set_fs(KERNEL_DS);
2219 ret = sys_sched_rr_get_interval(pid, (struct timespec __user *) &t);
2220 set_fs(old_fs);
2221 if (put_compat_timespec(&t, interval))
2222 return -EFAULT;
2223 return ret;
2226 asmlinkage long
2227 sys32_pread (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2229 return sys_pread64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2232 asmlinkage long
2233 sys32_pwrite (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2235 return sys_pwrite64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2238 asmlinkage long
2239 sys32_sendfile (int out_fd, int in_fd, int __user *offset, unsigned int count)
2241 mm_segment_t old_fs = get_fs();
2242 long ret;
2243 off_t of;
2245 if (offset && get_user(of, offset))
2246 return -EFAULT;
2248 set_fs(KERNEL_DS);
2249 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *) &of : NULL, count);
2250 set_fs(old_fs);
2252 if (offset && put_user(of, offset))
2253 return -EFAULT;
2255 return ret;
2258 asmlinkage long
2259 sys32_personality (unsigned int personality)
2261 long ret;
2263 if (current->personality == PER_LINUX32 && personality == PER_LINUX)
2264 personality = PER_LINUX32;
2265 ret = sys_personality(personality);
2266 if (ret == PER_LINUX32)
2267 ret = PER_LINUX;
2268 return ret;
2271 asmlinkage unsigned long
2272 sys32_brk (unsigned int brk)
2274 unsigned long ret, obrk;
2275 struct mm_struct *mm = current->mm;
2277 obrk = mm->brk;
2278 ret = sys_brk(brk);
2279 if (ret < obrk)
2280 clear_user(compat_ptr(ret), PAGE_ALIGN(ret) - ret);
2281 return ret;
2284 /* Structure for ia32 emulation on ia64 */
2285 struct epoll_event32
2287 u32 events;
2288 u32 data[2];
2291 asmlinkage long
2292 sys32_epoll_ctl(int epfd, int op, int fd, struct epoll_event32 __user *event)
2294 mm_segment_t old_fs = get_fs();
2295 struct epoll_event event64;
2296 int error;
2297 u32 data_halfword;
2299 if (!access_ok(VERIFY_READ, event, sizeof(struct epoll_event32)))
2300 return -EFAULT;
2302 __get_user(event64.events, &event->events);
2303 __get_user(data_halfword, &event->data[0]);
2304 event64.data = data_halfword;
2305 __get_user(data_halfword, &event->data[1]);
2306 event64.data |= (u64)data_halfword << 32;
2308 set_fs(KERNEL_DS);
2309 error = sys_epoll_ctl(epfd, op, fd, (struct epoll_event __user *) &event64);
2310 set_fs(old_fs);
2312 return error;
2315 asmlinkage long
2316 sys32_epoll_wait(int epfd, struct epoll_event32 __user * events, int maxevents,
2317 int timeout)
2319 struct epoll_event *events64 = NULL;
2320 mm_segment_t old_fs = get_fs();
2321 int numevents, size;
2322 int evt_idx;
2323 int do_free_pages = 0;
2325 if (maxevents <= 0) {
2326 return -EINVAL;
2329 /* Verify that the area passed by the user is writeable */
2330 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event32)))
2331 return -EFAULT;
2334 * Allocate space for the intermediate copy. If the space needed
2335 * is large enough to cause kmalloc to fail, then try again with
2336 * __get_free_pages.
2338 size = maxevents * sizeof(struct epoll_event);
2339 events64 = kmalloc(size, GFP_KERNEL);
2340 if (events64 == NULL) {
2341 events64 = (struct epoll_event *)
2342 __get_free_pages(GFP_KERNEL, get_order(size));
2343 if (events64 == NULL)
2344 return -ENOMEM;
2345 do_free_pages = 1;
2348 /* Do the system call */
2349 set_fs(KERNEL_DS); /* copy_to/from_user should work on kernel mem*/
2350 numevents = sys_epoll_wait(epfd, (struct epoll_event __user *) events64,
2351 maxevents, timeout);
2352 set_fs(old_fs);
2354 /* Don't modify userspace memory if we're returning an error */
2355 if (numevents > 0) {
2356 /* Translate the 64-bit structures back into the 32-bit
2357 structures */
2358 for (evt_idx = 0; evt_idx < numevents; evt_idx++) {
2359 __put_user(events64[evt_idx].events,
2360 &events[evt_idx].events);
2361 __put_user((u32)events64[evt_idx].data,
2362 &events[evt_idx].data[0]);
2363 __put_user((u32)(events64[evt_idx].data >> 32),
2364 &events[evt_idx].data[1]);
2368 if (do_free_pages)
2369 free_pages((unsigned long) events64, get_order(size));
2370 else
2371 kfree(events64);
2372 return numevents;
2376 * Get a yet unused TLS descriptor index.
2378 static int
2379 get_free_idx (void)
2381 struct thread_struct *t = &current->thread;
2382 int idx;
2384 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
2385 if (desc_empty(t->tls_array + idx))
2386 return idx + GDT_ENTRY_TLS_MIN;
2387 return -ESRCH;
2391 * Set a given TLS descriptor:
2393 asmlinkage int
2394 sys32_set_thread_area (struct ia32_user_desc __user *u_info)
2396 struct thread_struct *t = &current->thread;
2397 struct ia32_user_desc info;
2398 struct desc_struct *desc;
2399 int cpu, idx;
2401 if (copy_from_user(&info, u_info, sizeof(info)))
2402 return -EFAULT;
2403 idx = info.entry_number;
2406 * index -1 means the kernel should try to find and allocate an empty descriptor:
2408 if (idx == -1) {
2409 idx = get_free_idx();
2410 if (idx < 0)
2411 return idx;
2412 if (put_user(idx, &u_info->entry_number))
2413 return -EFAULT;
2416 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2417 return -EINVAL;
2419 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
2421 cpu = smp_processor_id();
2423 if (LDT_empty(&info)) {
2424 desc->a = 0;
2425 desc->b = 0;
2426 } else {
2427 desc->a = LDT_entry_a(&info);
2428 desc->b = LDT_entry_b(&info);
2430 load_TLS(t, cpu);
2431 return 0;
2435 * Get the current Thread-Local Storage area:
2438 #define GET_BASE(desc) ( \
2439 (((desc)->a >> 16) & 0x0000ffff) | \
2440 (((desc)->b << 16) & 0x00ff0000) | \
2441 ( (desc)->b & 0xff000000) )
2443 #define GET_LIMIT(desc) ( \
2444 ((desc)->a & 0x0ffff) | \
2445 ((desc)->b & 0xf0000) )
2447 #define GET_32BIT(desc) (((desc)->b >> 22) & 1)
2448 #define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
2449 #define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
2450 #define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
2451 #define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
2452 #define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
2454 asmlinkage int
2455 sys32_get_thread_area (struct ia32_user_desc __user *u_info)
2457 struct ia32_user_desc info;
2458 struct desc_struct *desc;
2459 int idx;
2461 if (get_user(idx, &u_info->entry_number))
2462 return -EFAULT;
2463 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2464 return -EINVAL;
2466 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
2468 info.entry_number = idx;
2469 info.base_addr = GET_BASE(desc);
2470 info.limit = GET_LIMIT(desc);
2471 info.seg_32bit = GET_32BIT(desc);
2472 info.contents = GET_CONTENTS(desc);
2473 info.read_exec_only = !GET_WRITABLE(desc);
2474 info.limit_in_pages = GET_LIMIT_PAGES(desc);
2475 info.seg_not_present = !GET_PRESENT(desc);
2476 info.useable = GET_USEABLE(desc);
2478 if (copy_to_user(u_info, &info, sizeof(info)))
2479 return -EFAULT;
2480 return 0;
2483 long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
2484 __u32 len_low, __u32 len_high, int advice)
2486 return sys_fadvise64_64(fd,
2487 (((u64)offset_high)<<32) | offset_low,
2488 (((u64)len_high)<<32) | len_low,
2489 advice);
2492 #ifdef NOTYET /* UNTESTED FOR IA64 FROM HERE DOWN */
2494 asmlinkage long sys32_setreuid(compat_uid_t ruid, compat_uid_t euid)
2496 uid_t sruid, seuid;
2498 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2499 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2500 return sys_setreuid(sruid, seuid);
2503 asmlinkage long
2504 sys32_setresuid(compat_uid_t ruid, compat_uid_t euid,
2505 compat_uid_t suid)
2507 uid_t sruid, seuid, ssuid;
2509 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2510 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2511 ssuid = (suid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)suid);
2512 return sys_setresuid(sruid, seuid, ssuid);
2515 asmlinkage long
2516 sys32_setregid(compat_gid_t rgid, compat_gid_t egid)
2518 gid_t srgid, segid;
2520 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2521 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2522 return sys_setregid(srgid, segid);
2525 asmlinkage long
2526 sys32_setresgid(compat_gid_t rgid, compat_gid_t egid,
2527 compat_gid_t sgid)
2529 gid_t srgid, segid, ssgid;
2531 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2532 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2533 ssgid = (sgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)sgid);
2534 return sys_setresgid(srgid, segid, ssgid);
2536 #endif /* NOTYET */