blob: b1bf51fe97b47cc66b32cd889431c5ae9af1b3b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sys_ia32.c: Conversion between 32bit and 64bit native syscalls. Derived from sys_sparc32.c.
3 *
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>
12 *
13 * These routines maintain argument size conversion between 32bit and 64bit
14 * environment.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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>
KOSAKI Motohiro2d9b06c2008-03-04 15:45:42 -080035#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/quota.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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>
Randy Dunlapa9415642006-01-11 12:17:48 -080043#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/compat.h>
45#include <linux/vfs.h>
46#include <linux/mman.h>
Jes Sorensen92ff2ec2006-01-18 23:46:51 -080047#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/intrinsics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/types.h>
51#include <asm/uaccess.h>
52#include <asm/unistd.h>
53
54#include "ia32priv.h"
55
56#include <net/scm.h>
57#include <net/sock.h>
58
59#define DEBUG 0
60
61#if DEBUG
62# define DBG(fmt...) printk(KERN_DEBUG fmt)
63#else
64# define DBG(fmt...)
65#endif
66
67#define ROUND_UP(x,a) ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
68
69#define OFFSET4K(a) ((a) & 0xfff)
70#define PAGE_START(addr) ((addr) & PAGE_MASK)
71#define MINSIGSTKSZ_IA32 2048
72
73#define high2lowuid(uid) ((uid) > 65535 ? 65534 : (uid))
74#define high2lowgid(gid) ((gid) > 65535 ? 65534 : (gid))
75
76/*
77 * Anything that modifies or inspects ia32 user virtual memory must hold this semaphore
78 * while doing so.
79 */
80/* XXX make per-mm: */
Jes Sorensen92ff2ec2006-01-18 23:46:51 -080081static DEFINE_MUTEX(ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83asmlinkage long
84sys32_execve (char __user *name, compat_uptr_t __user *argv, compat_uptr_t __user *envp,
85 struct pt_regs *regs)
86{
87 long error;
88 char *filename;
89 unsigned long old_map_base, old_task_size, tssd;
90
91 filename = getname(name);
92 error = PTR_ERR(filename);
93 if (IS_ERR(filename))
94 return error;
95
96 old_map_base = current->thread.map_base;
97 old_task_size = current->thread.task_size;
98 tssd = ia64_get_kr(IA64_KR_TSSD);
99
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);
105
106 error = compat_do_execve(filename, argv, envp, regs);
107 putname(filename);
108
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;
115 }
116
117 return error;
118}
119
120int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
121{
David Howellsafefdbb2006-10-03 01:13:46 -0700122 compat_ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int err;
124
125 if ((u64) stat->size > MAX_NON_LFS ||
126 !old_valid_dev(stat->dev) ||
127 !old_valid_dev(stat->rdev))
128 return -EOVERFLOW;
129
David Howellsafefdbb2006-10-03 01:13:46 -0700130 ino = stat->ino;
131 if (sizeof(ino) < sizeof(stat->ino) && ino != stat->ino)
132 return -EOVERFLOW;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (clear_user(ubuf, sizeof(*ubuf)))
135 return -EFAULT;
136
137 err = __put_user(old_encode_dev(stat->dev), &ubuf->st_dev);
David Howellsafefdbb2006-10-03 01:13:46 -0700138 err |= __put_user(ino, &ubuf->st_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 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;
154}
155
156#if PAGE_SHIFT > IA32_PAGE_SHIFT
157
158
159static int
160get_page_prot (struct vm_area_struct *vma, unsigned long addr)
161{
162 int prot = 0;
163
164 if (!vma || vma->vm_start > addr)
165 return 0;
166
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;
174}
175
176/*
177 * Map a subpage by creating an anonymous page that contains the union of the old page and
178 * the subpage.
179 */
180static unsigned long
181mmap_subpage (struct file *file, unsigned long start, unsigned long end, int prot, int flags,
182 loff_t off)
183{
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);
189
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);
192
193
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;
199 }
200 goto skip_mmap;
201 }
202
203 page = (void *) get_zeroed_page(GFP_KERNEL);
204 if (!page)
205 return -ENOMEM;
206
207 if (old_prot)
208 copy_from_user(page, (void __user *) PAGE_START(start), PAGE_SIZE);
209
210 down_write(&current->mm->mmap_sem);
211 {
212 ret = do_mmap(NULL, PAGE_START(start), PAGE_SIZE, prot | PROT_WRITE,
213 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
214 }
215 up_write(&current->mm->mmap_sem);
216
217 if (IS_ERR((void *) ret))
218 goto out;
219
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));
228 }
229
230 if (!(flags & MAP_ANONYMOUS)) {
231 /* read the file contents */
Josef Sipekb66ffad2006-12-08 02:37:09 -0800232 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!inode->i_fop || !file->f_op->read
234 || ((*file->f_op->read)(file, (char __user *) start, end - start, &off) < 0))
235 {
236 ret = -EINVAL;
237 goto out;
238 }
239 }
240
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;
248}
249
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700250/* SLAB cache for ia64_partial_page structures */
251struct kmem_cache *ia64_partial_page_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/*
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700254 * init ia64_partial_page_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * return 0 means kmalloc fail.
256 */
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700257struct ia64_partial_page_list*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258ia32_init_pp_list(void)
259{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700260 struct ia64_partial_page_list *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
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;
269}
270
271/*
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().
276 */
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700277static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct rb_node **rb_parent)
281{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700282 struct ia64_partial_page *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
284
285 pp = ppl->pp_hint;
286 if (pp && pp->base == start)
287 return pp;
288
289 __rb_link = &ppl->ppl_rb.rb_node;
290 rb_prev = __rb_parent = NULL;
291
292 while (*__rb_link) {
293 __rb_parent = *__rb_link;
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700294 pp = rb_entry(__rb_parent, struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
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;
304 }
305 }
306
307 *rb_link = __rb_link;
308 *rb_parent = __rb_parent;
309 *pprev = NULL;
310 if (rb_prev)
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700311 *pprev = rb_entry(rb_prev, struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return NULL;
313}
314
315/*
316 * insert @pp into @ppl.
317 */
318static void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700319__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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
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,
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700331 struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 else
333 pp->next = NULL;
334 }
335
336 /* link rb */
337 rb_link_node(&pp->pp_rb, rb_parent, rb_link);
338 rb_insert_color(&pp->pp_rb, &ppl->ppl_rb);
339
340 ppl->pp_hint = pp;
341}
342
343/*
344 * delete @pp from partial page list @ppl.
345 */
346static void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700347__ia32_delete_pp(struct ia64_partial_page_list *ppl,
348 struct ia64_partial_page *pp, struct ia64_partial_page *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
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;
358 }
359 rb_erase(&pp->pp_rb, &ppl->ppl_rb);
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700360 kmem_cache_free(ia64_partial_page_cachep, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700363static struct ia64_partial_page *
364__pp_prev(struct ia64_partial_page *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct rb_node *prev = rb_prev(&pp->pp_rb);
367 if (prev)
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700368 return rb_entry(prev, struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 else
370 return NULL;
371}
372
373/*
374 * Delete partial pages with address between @start and @end.
375 * @start and @end are page aligned.
376 */
377static void
378__ia32_delete_pp_range(unsigned int start, unsigned int end)
379{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700380 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct rb_node **rb_link, *rb_parent;
382
383 if (start >= end)
384 return;
385
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;
395 }
396
397 while (pp && pp->base < end) {
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700398 struct ia64_partial_page *tmp = pp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 __ia32_delete_pp(current->thread.ppl, pp, prev);
400 pp = tmp;
401 }
402}
403
404/*
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.
407 */
408static int
409__ia32_set_pp(unsigned int start, unsigned int end, int flags)
410{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700411 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct rb_node ** rb_link, *rb_parent;
413 unsigned int pstart, start_bit, end_bit, i;
414
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);
425 /*
426 * Check: if this partial page has been set to a full page,
427 * then delete it.
428 */
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));
432 }
433 return 0;
434 }
435
436 /*
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.
440 */
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;
445 }
446
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700447 /* new a ia64_partial_page */
448 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 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;
458}
459
460/*
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().
464 */
465static void
466ia32_set_pp(unsigned int start, unsigned int end, int flags)
467{
468 down_write(&current->mm->mmap_sem);
469 if (flags & MAP_FIXED) {
470 /*
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.
474 */
475 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
476 }
477
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);
485 }
486 up_write(&current->mm->mmap_sem);
487}
488
489/*
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.
497 */
498static int
499__ia32_unset_pp(unsigned int start, unsigned int end)
500{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700501 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct rb_node ** rb_link, *rb_parent;
503 unsigned int pstart, start_bit, end_bit, i;
504 struct vm_area_struct *vma;
505
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;
511
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;
520 }
521 return 0;
522 }
523
524 vma = find_vma(current->mm, pstart);
525 if (!vma || vma->vm_start > pstart) {
526 return -ENOMEM;
527 }
528
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700529 /* new a ia64_partial_page */
530 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 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;
542}
543
544/*
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().
549 */
550static int
551ia32_unset_pp(unsigned int *startp, unsigned int *endp)
552{
553 unsigned int start = *startp, end = *endp;
554 int ret = 0;
555
556 down_write(&current->mm->mmap_sem);
557
558 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
559
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);
565 }
566 if (ret == 0) {
567 /* to shortcut sys_munmap() in sys32_munmap() */
568 *startp = PAGE_START(start);
569 *endp = PAGE_START(end);
570 }
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;
580 }
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);
587 }
588 }
589
590 out:
591 up_write(&current->mm->mmap_sem);
592 return ret;
593}
594
595/*
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.
598 */
599static int
600__ia32_compare_pp(unsigned int start, unsigned int end)
601{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700602 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 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 */
606
607 pstart = PAGE_START(start);
608
609 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
610 &rb_link, &rb_parent);
611 if (!pp)
612 return 1;
613
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;
630}
631
632/*
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().
636 *
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.
642 */
643static int
644ia32_compare_pp(unsigned int *startp, unsigned int *endp)
645{
646 unsigned int start = *startp, end = *endp;
647 int retval = 0;
648
649 down_write(&current->mm->mmap_sem);
650
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);
656 }
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;
665 }
666 if (offset_in_page(end)) {
667 retval = __ia32_compare_pp(PAGE_START(end), end);
668 if (retval == 0)
669 *endp = PAGE_ALIGN(end);
670 }
671 }
672
673 out:
674 up_write(&current->mm->mmap_sem);
675 return retval;
676}
677
678static void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700679__ia32_drop_pp_list(struct ia64_partial_page_list *ppl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700681 struct ia64_partial_page *pp = ppl->pp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 while (pp) {
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700684 struct ia64_partial_page *next = pp->next;
685 kmem_cache_free(ia64_partial_page_cachep, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 pp = next;
687 }
688
689 kfree(ppl);
690}
691
692void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700693ia32_drop_ia64_partial_page_list(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700695 struct ia64_partial_page_list* ppl = task->thread.ppl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (ppl && atomic_dec_and_test(&ppl->pp_count))
698 __ia32_drop_pp_list(ppl);
699}
700
701/*
702 * Copy current->thread.ppl to ppl (already initialized).
703 */
704static int
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700705__ia32_copy_pp_list(struct ia64_partial_page_list *ppl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700707 struct ia64_partial_page *pp, *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct rb_node **rb_link, *rb_parent;
709
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;
716
717 for (pp = current->thread.ppl->pp_head; pp; pp = pp->next) {
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700718 tmp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 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;
726 }
727 return 0;
728}
729
730int
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700731ia32_copy_ia64_partial_page_list(struct task_struct *p,
732 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 int retval = 0;
735
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);
744 {
745 retval = __ia32_copy_pp_list(p->thread.ppl);
746 }
747 up_write(&current->mm->mmap_sem);
748 }
749
750 return retval;
751}
752
753static unsigned long
754emulate_mmap (struct file *file, unsigned long start, unsigned long len, int prot, int flags,
755 loff_t off)
756{
757 unsigned long tmp, end, pend, pstart, ret, is_congruent, fudge = 0;
758 struct inode *inode;
759 loff_t poff;
760
761 end = start + len;
762 pstart = PAGE_START(start);
763 pend = PAGE_ALIGN(end);
764
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",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700771 current->comm, task_pid_nr(current), start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 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 */
779 }
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",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700784 current->comm, task_pid_nr(current), end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 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 */
792 }
793 } else {
794 /*
795 * If a start address was specified, use it if the entire rounded out area
796 * is available.
797 */
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);
806 }
807 }
808
809 poff = off + (pstart - start); /* note: (pstart - start) may be negative */
810 is_congruent = (flags & MAP_ANONYMOUS) || (offset_in_page(poff) == 0);
811
812 if ((flags & MAP_SHARED) && !is_congruent)
813 printk(KERN_INFO "%s(%d): emulate_mmap() can't share contents of incongruent mmap "
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700814 "(addr=0x%lx,off=0x%llx)\n", current->comm, task_pid_nr(current), start, off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 DBG("mmap_body: mapping [0x%lx-0x%lx) %s with poff 0x%llx\n", pstart, pend,
817 is_congruent ? "congruent" : "not congruent", poff);
818
819 down_write(&current->mm->mmap_sem);
820 {
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);
827 }
828 up_write(&current->mm->mmap_sem);
829
830 if (IS_ERR((void *) ret))
831 return ret;
832
833 if (!is_congruent) {
834 /* read the file contents */
Josef Sipekb66ffad2006-12-08 02:37:09 -0800835 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!inode->i_fop || !file->f_op->read
837 || ((*file->f_op->read)(file, (char __user *) pstart, pend - pstart, &poff)
838 < 0))
839 {
840 sys_munmap(pstart, pend - pstart);
841 return -EINVAL;
842 }
843 if (!(prot & PROT_WRITE) && sys_mprotect(pstart, pend - pstart, prot) < 0)
844 return -EINVAL;
845 }
846
847 if (!(flags & MAP_FIXED))
848 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
849out:
850 return start;
851}
852
853#endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
854
855static inline unsigned int
856get_prot32 (unsigned int prot)
857{
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);
864
865 return prot;
866}
867
868unsigned long
869ia32_do_mmap (struct file *file, unsigned long addr, unsigned long len, int prot, int flags,
870 loff_t offset)
871{
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);
874
875 if (file && (!file->f_op || !file->f_op->mmap))
876 return -ENODEV;
877
878 len = IA32_PAGE_ALIGN(len);
879 if (len == 0)
880 return addr;
881
882 if (len > IA32_PAGE_OFFSET || addr > IA32_PAGE_OFFSET - len)
883 {
884 if (flags & MAP_FIXED)
885 return -ENOMEM;
886 else
887 return -EINVAL;
888 }
889
890 if (OFFSET4K(offset))
891 return -EINVAL;
892
893 prot = get_prot32(prot);
894
895#if PAGE_SHIFT > IA32_PAGE_SHIFT
Jes Sorensen92ff2ec2006-01-18 23:46:51 -0800896 mutex_lock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 {
898 addr = emulate_mmap(file, addr, len, prot, flags, offset);
899 }
Jes Sorensen92ff2ec2006-01-18 23:46:51 -0800900 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#else
902 down_write(&current->mm->mmap_sem);
903 {
904 addr = do_mmap(file, addr, len, prot, flags, offset);
905 }
906 up_write(&current->mm->mmap_sem);
907#endif
908 DBG("ia32_do_mmap: returning 0x%lx\n", addr);
909 return addr;
910}
911
912/*
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..
915 */
916
917struct 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;
924};
925
926asmlinkage long
927sys32_mmap (struct mmap_arg_struct __user *arg)
928{
929 struct mmap_arg_struct a;
930 struct file *file = NULL;
931 unsigned long addr;
932 int flags;
933
934 if (copy_from_user(&a, arg, sizeof(a)))
935 return -EFAULT;
936
937 if (OFFSET4K(a.offset))
938 return -EINVAL;
939
940 flags = a.flags;
941
942 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
943 if (!(flags & MAP_ANONYMOUS)) {
944 file = fget(a.fd);
945 if (!file)
946 return -EBADF;
947 }
948
949 addr = ia32_do_mmap(file, a.addr, a.len, a.prot, flags, a.offset);
950
951 if (file)
952 fput(file);
953 return addr;
954}
955
956asmlinkage long
957sys32_mmap2 (unsigned int addr, unsigned int len, unsigned int prot, unsigned int flags,
958 unsigned int fd, unsigned int pgoff)
959{
960 struct file *file = NULL;
961 unsigned long retval;
962
963 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
964 if (!(flags & MAP_ANONYMOUS)) {
965 file = fget(fd);
966 if (!file)
967 return -EBADF;
968 }
969
970 retval = ia32_do_mmap(file, addr, len, prot, flags,
971 (unsigned long) pgoff << IA32_PAGE_SHIFT);
972
973 if (file)
974 fput(file);
975 return retval;
976}
977
978asmlinkage long
979sys32_munmap (unsigned int start, unsigned int len)
980{
981 unsigned int end = start + len;
982 long ret;
983
984#if PAGE_SHIFT <= IA32_PAGE_SHIFT
985 ret = sys_munmap(start, end - start);
986#else
987 if (OFFSET4K(start))
988 return -EINVAL;
989
990 end = IA32_PAGE_ALIGN(end);
991 if (start >= end)
992 return -EINVAL;
993
994 ret = ia32_unset_pp(&start, &end);
995 if (ret < 0)
996 return ret;
997
998 if (start >= end)
999 return 0;
1000
Jes Sorensen92ff2ec2006-01-18 23:46:51 -08001001 mutex_lock(&ia32_mmap_mutex);
1002 ret = sys_munmap(start, end - start);
1003 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004#endif
1005 return ret;
1006}
1007
1008#if PAGE_SHIFT > IA32_PAGE_SHIFT
1009
1010/*
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.
1014 */
1015static long
1016mprotect_subpage (unsigned long address, int new_prot)
1017{
1018 int old_prot;
1019 struct vm_area_struct *vma;
1020
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);
1026}
1027
1028#endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
1029
1030asmlinkage long
1031sys32_mprotect (unsigned int start, unsigned int len, int prot)
1032{
1033 unsigned int end = start + len;
1034#if PAGE_SHIFT > IA32_PAGE_SHIFT
1035 long retval = 0;
1036#endif
1037
1038 prot = get_prot32(prot);
1039
1040#if PAGE_SHIFT <= IA32_PAGE_SHIFT
1041 return sys_mprotect(start, end - start, prot);
1042#else
1043 if (OFFSET4K(start))
1044 return -EINVAL;
1045
1046 end = IA32_PAGE_ALIGN(end);
1047 if (end < start)
1048 return -EINVAL;
1049
1050 retval = ia32_compare_pp(&start, &end);
1051
1052 if (retval < 0)
1053 return retval;
1054
Jes Sorensen92ff2ec2006-01-18 23:46:51 -08001055 mutex_lock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 {
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;
1062
1063 start = PAGE_ALIGN(start);
1064 if (start >= end)
1065 goto out; /* retval is already zero... */
1066 }
1067
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;
1073
1074 end = PAGE_START(end);
1075 }
1076 retval = sys_mprotect(start, end - start, prot);
1077 }
1078 out:
Jes Sorensen92ff2ec2006-01-18 23:46:51 -08001079 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return retval;
1081#endif
1082}
1083
1084asmlinkage long
1085sys32_mremap (unsigned int addr, unsigned int old_len, unsigned int new_len,
1086 unsigned int flags, unsigned int new_addr)
1087{
1088 long ret;
1089
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;
1094
1095 if (OFFSET4K(addr))
1096 return -EINVAL;
1097
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;
1102
1103 if (!new_len)
1104 return -EINVAL;
1105
1106 if ((flags & MREMAP_FIXED) && (OFFSET4K(new_addr)))
1107 return -EINVAL;
1108
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;
1117 }
1118
1119 addr = PAGE_START(addr);
1120 old_len = PAGE_ALIGN(old_end) - addr;
1121 new_len = PAGE_ALIGN(new_end) - addr;
1122
Jes Sorensen92ff2ec2006-01-18 23:46:51 -08001123 mutex_lock(&ia32_mmap_mutex);
1124 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1125 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 if ((ret >= 0) && (old_len < new_len)) {
1128 /* mremap expanded successfully */
1129 ia32_set_pp(old_end, new_end, flags);
1130 }
1131#endif
1132 return ret;
1133}
1134
1135asmlinkage long
1136sys32_pipe (int __user *fd)
1137{
1138 int retval;
1139 int fds[2];
1140
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;
1148}
1149
1150static inline long
1151get_tv32 (struct timeval *o, struct compat_timeval __user *i)
1152{
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)));
1155}
1156
1157static inline long
1158put_tv32 (struct compat_timeval __user *o, struct timeval *i)
1159{
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)));
1162}
1163
1164asmlinkage unsigned long
1165sys32_alarm (unsigned int seconds)
1166{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001167 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/* Translations due to time_t size differences. Which affects all
1171 sorts of things, like timeval and itimerval. */
1172
1173extern struct timezone sys_tz;
1174
1175asmlinkage long
1176sys32_gettimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1177{
1178 if (tv) {
1179 struct timeval ktv;
1180 do_gettimeofday(&ktv);
1181 if (put_tv32(tv, &ktv))
1182 return -EFAULT;
1183 }
1184 if (tz) {
1185 if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
1186 return -EFAULT;
1187 }
1188 return 0;
1189}
1190
1191asmlinkage long
1192sys32_settimeofday (struct compat_timeval __user *tv, struct timezone __user *tz)
1193{
1194 struct timeval ktv;
1195 struct timespec kts;
1196 struct timezone ktz;
1197
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;
1203 }
1204 if (tz) {
1205 if (copy_from_user(&ktz, tz, sizeof(ktz)))
1206 return -EFAULT;
1207 }
1208
1209 return do_sys_settimeofday(tv ? &kts : NULL, tz ? &ktz : NULL);
1210}
1211
1212struct getdents32_callback {
1213 struct compat_dirent __user *current_dir;
1214 struct compat_dirent __user *previous;
1215 int count;
1216 int error;
1217};
1218
1219struct readdir32_callback {
1220 struct old_linux32_dirent __user * dirent;
1221 int count;
1222};
1223
1224static int
David Howellsafefdbb2006-10-03 01:13:46 -07001225filldir32 (void *__buf, const char *name, int namlen, loff_t offset, u64 ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 unsigned int d_type)
1227{
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);
David Howellsafefdbb2006-10-03 01:13:46 -07001231 u32 d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 buf->error = -EINVAL; /* only used if we fail.. */
1234 if (reclen > buf->count)
1235 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -07001236 d_ino = ino;
1237 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1238 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 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;
David Howellsafefdbb2006-10-03 01:13:46 -07001246 if (put_user(d_ino, &dirent->d_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 || 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;
1255}
1256
1257asmlinkage long
1258sys32_getdents (unsigned int fd, struct compat_dirent __user *dirent, unsigned int count)
1259{
1260 struct file * file;
1261 struct compat_dirent __user * lastdirent;
1262 struct getdents32_callback buf;
1263 int error;
1264
Alexandr Andreev50157b02007-03-06 02:34:24 -08001265 error = -EFAULT;
1266 if (!access_ok(VERIFY_WRITE, dirent, count))
1267 goto out;
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 error = -EBADF;
1270 file = fget(fd);
1271 if (!file)
1272 goto out;
1273
1274 buf.current_dir = dirent;
1275 buf.previous = NULL;
1276 buf.count = count;
1277 buf.error = 0;
1278
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (put_user(file->f_pos, &lastdirent->d_off))
Alexandr Andreev50157b02007-03-06 02:34:24 -08001286 error = -EFAULT;
1287 else
1288 error = count - buf.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291out_putf:
1292 fput(file);
1293out:
1294 return error;
1295}
1296
1297static int
David Howellsafefdbb2006-10-03 01:13:46 -07001298fillonedir32 (void * __buf, const char * name, int namlen, loff_t offset, u64 ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 unsigned int d_type)
1300{
1301 struct readdir32_callback * buf = (struct readdir32_callback *) __buf;
1302 struct old_linux32_dirent __user * dirent;
David Howellsafefdbb2006-10-03 01:13:46 -07001303 u32 d_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (buf->count)
1306 return -EINVAL;
David Howellsafefdbb2006-10-03 01:13:46 -07001307 d_ino = ino;
1308 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
1309 return -EOVERFLOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 buf->count++;
1311 dirent = buf->dirent;
David Howellsafefdbb2006-10-03 01:13:46 -07001312 if (put_user(d_ino, &dirent->d_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 || 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;
1319}
1320
1321asmlinkage long
1322sys32_readdir (unsigned int fd, void __user *dirent, unsigned int count)
1323{
1324 int error;
1325 struct file * file;
1326 struct readdir32_callback buf;
1327
1328 error = -EBADF;
1329 file = fget(fd);
1330 if (!file)
1331 goto out;
1332
1333 buf.count = 0;
1334 buf.dirent = dirent;
1335
1336 error = vfs_readdir(file, fillonedir32, &buf);
1337 if (error >= 0)
1338 error = buf.count;
1339 fput(file);
1340out:
1341 return error;
1342}
1343
1344struct sel_arg_struct {
1345 unsigned int n;
1346 unsigned int inp;
1347 unsigned int outp;
1348 unsigned int exp;
1349 unsigned int tvp;
1350};
1351
1352asmlinkage long
1353sys32_old_select (struct sel_arg_struct __user *arg)
1354{
1355 struct sel_arg_struct a;
1356
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));
1361}
1362
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
1375
1376asmlinkage long
1377sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
1378{
1379 int version;
1380
1381 version = call >> 16; /* hack for backward compatibility */
1382 call &= 0xffff;
1383
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));
1398
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));
1407
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));
1417
1418 default:
1419 return -ENOSYS;
1420 }
1421 return -EINVAL;
1422}
1423
1424asmlinkage long
1425compat_sys_wait4 (compat_pid_t pid, compat_uint_t * stat_addr, int options,
1426 struct compat_rusage *ru);
1427
1428asmlinkage long
1429sys32_waitpid (int pid, unsigned int *stat_addr, int options)
1430{
1431 return compat_sys_wait4(pid, stat_addr, options, NULL);
1432}
1433
1434static unsigned int
1435ia32_peek (struct task_struct *child, unsigned long addr, unsigned int *val)
1436{
1437 size_t copied;
1438 unsigned int ret;
1439
1440 copied = access_process_vm(child, addr, val, sizeof(*val), 0);
1441 return (copied != sizeof(ret)) ? -EIO : 0;
1442}
1443
1444static unsigned int
1445ia32_poke (struct task_struct *child, unsigned long addr, unsigned int val)
1446{
1447
1448 if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val))
1449 return -EIO;
1450 return 0;
1451}
1452
1453/*
1454 * The order in which registers are stored in the ptrace regs structure
1455 */
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
1473
1474static unsigned int
1475getreg (struct task_struct *child, int regno)
1476{
1477 struct pt_regs *child_regs;
1478
Al Viro64505782006-01-12 01:06:06 -08001479 child_regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 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;
1498 }
1499 return 0;
1500}
1501
1502static void
1503putreg (struct task_struct *child, int regno, unsigned int value)
1504{
1505 struct pt_regs *child_regs;
1506
Al Viro64505782006-01-12 01:06:06 -08001507 child_regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 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;
1535 }
1536}
1537
1538static void
1539put_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1540 struct switch_stack *swp, int tos)
1541{
1542 struct _fpreg_ia32 *f;
1543 char buf[32];
1544
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;
1567 }
1568 copy_to_user(reg, f, sizeof(*reg));
1569}
1570
1571static void
1572get_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1573 struct switch_stack *swp, int tos)
1574{
1575
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;
1597 }
1598 return;
1599}
1600
1601int
1602save_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1603{
1604 struct switch_stack *swp;
1605 struct pt_regs *ptp;
1606 int i, tos;
1607
1608 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1609 return -EFAULT;
1610
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);
1618
1619 /*
1620 * Stack frames start with 16-bytes of temp space
1621 */
1622 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001623 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 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;
1628}
1629
1630static int
1631restore_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1632{
1633 struct switch_stack *swp;
1634 struct pt_regs *ptp;
1635 int i, tos;
1636 unsigned int fsrlo, fsrhi, num32;
1637
1638 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1639 return(-EFAULT);
1640
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;
1651
1652 /*
1653 * Stack frames start with 16-bytes of temp space
1654 */
1655 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001656 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 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;
1661}
1662
1663int
1664save_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1665{
1666 struct switch_stack *swp;
1667 struct pt_regs *ptp;
1668 int i, tos;
1669 unsigned long mxcsr=0;
1670 unsigned long num128[2];
1671
1672 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1673 return -EFAULT;
1674
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);
1682
1683 /*
1684 * Stack frames start with 16-bytes of temp space
1685 */
1686 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001687 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 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);
1691
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));
1698 }
1699 return 0;
1700}
1701
1702static int
1703restore_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1704{
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];
1712
1713 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1714 return(-EFAULT);
1715
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;
1726
1727 /*
1728 * Stack frames start with 16-bytes of temp space
1729 */
1730 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001731 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 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);
1735
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);
1741
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));
1746 }
1747 return 0;
1748}
1749
1750asmlinkage long
1751sys32_ptrace (int request, pid_t pid, unsigned int addr, unsigned int data)
1752{
1753 struct task_struct *child;
1754 unsigned int value, tmp;
1755 long i, ret;
1756
1757 lock_kernel();
1758 if (request == PTRACE_TRACEME) {
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -08001759 ret = ptrace_traceme();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 goto out;
1761 }
1762
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -08001763 child = ptrace_get_task_struct(pid);
1764 if (IS_ERR(child)) {
1765 ret = PTR_ERR(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto out;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -08001767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 if (request == PTRACE_ATTACH) {
1770 ret = sys_ptrace(request, pid, addr, data);
1771 goto out_tsk;
1772 }
1773
1774 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1775 if (ret < 0)
1776 goto out_tsk;
1777
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;
1787
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;
1792
1793 case PTRACE_PEEKUSR: /* read word at addr in USER area */
1794 ret = -EIO;
1795 if ((addr & 3) || addr > 17*sizeof(int))
1796 break;
1797
1798 tmp = getreg(child, addr);
1799 if (!put_user(tmp, (unsigned int __user *) compat_ptr(data)))
1800 ret = 0;
1801 break;
1802
1803 case PTRACE_POKEUSR: /* write word at addr in USER area */
1804 ret = -EIO;
1805 if ((addr & 3) || addr > 17*sizeof(int))
1806 break;
1807
1808 putreg(child, addr, data);
1809 ret = 0;
1810 break;
1811
1812 case IA32_PTRACE_GETREGS:
1813 if (!access_ok(VERIFY_WRITE, compat_ptr(data), 17*sizeof(int))) {
1814 ret = -EIO;
1815 break;
1816 }
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);
1820 }
1821 ret = 0;
1822 break;
1823
1824 case IA32_PTRACE_SETREGS:
1825 if (!access_ok(VERIFY_READ, compat_ptr(data), 17*sizeof(int))) {
1826 ret = -EIO;
1827 break;
1828 }
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);
1833 }
1834 ret = 0;
1835 break;
1836
1837 case IA32_PTRACE_GETFPREGS:
1838 ret = save_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1839 compat_ptr(data));
1840 break;
1841
1842 case IA32_PTRACE_GETFPXREGS:
1843 ret = save_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1844 compat_ptr(data));
1845 break;
1846
1847 case IA32_PTRACE_SETFPREGS:
1848 ret = restore_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1849 compat_ptr(data));
1850 break;
1851
1852 case IA32_PTRACE_SETFPXREGS:
1853 ret = restore_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1854 compat_ptr(data));
1855 break;
1856
1857 case PTRACE_GETEVENTMSG:
1858 ret = put_user(child->ptrace_message, (unsigned int __user *) compat_ptr(data));
1859 break;
1860
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;
1868
1869 default:
1870 ret = ptrace_request(child, request, addr, data);
1871 break;
1872
1873 }
1874 out_tsk:
1875 put_task_struct(child);
1876 out:
1877 unlock_kernel();
1878 return ret;
1879}
1880
1881typedef struct {
1882 unsigned int ss_sp;
1883 unsigned int ss_flags;
1884 unsigned int ss_size;
1885} ia32_stack_t;
1886
1887asmlinkage long
1888sys32_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)
1891{
1892 stack_t uss, uoss;
1893 ia32_stack_t buf32;
1894 int ret;
1895 mm_segment_t old_fs = get_fs();
1896
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;
1907 }
1908 uss.ss_size = MINSIGSTKSZ;
1909 }
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);
1915out:
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;
1924 }
1925 return ret;
1926}
1927
1928asmlinkage int
1929sys32_pause (void)
1930{
1931 current->state = TASK_INTERRUPTIBLE;
1932 schedule();
1933 return -ERESTARTNOHAND;
1934}
1935
1936asmlinkage int
1937sys32_msync (unsigned int start, unsigned int len, int flags)
1938{
1939 unsigned int addr;
1940
1941 if (OFFSET4K(start))
1942 return -EINVAL;
1943 addr = PAGE_START(start);
1944 return sys_msync(addr, len + (start - addr), flags);
1945}
1946
1947struct 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];
1955};
1956
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001957#ifdef CONFIG_SYSCTL_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958asmlinkage long
1959sys32_sysctl (struct sysctl32 __user *args)
1960{
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;
1967
1968 if (copy_from_user(&a32, args, sizeof(a32)))
1969 return -EFAULT;
1970
1971 /*
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...
1977 */
1978 namep = (int __user *) compat_ptr(a32.name);
1979 oldvalp = compat_ptr(a32.oldval);
1980 newvalp = compat_ptr(a32.newval);
1981
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;
1987
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);
1994
1995 if (oldvalp && put_user (oldlen, (int __user *) compat_ptr(a32.oldlenp)))
1996 return -EFAULT;
1997
1998 return ret;
1999}
2000#endif
2001
2002asmlinkage long
2003sys32_newuname (struct new_utsname __user *name)
2004{
2005 int ret = sys_newuname(name);
2006
2007 if (!ret)
2008 if (copy_to_user(name->machine, "i686\0\0\0", 8))
2009 ret = -EFAULT;
2010 return ret;
2011}
2012
2013asmlinkage long
2014sys32_getresuid16 (u16 __user *ruid, u16 __user *euid, u16 __user *suid)
2015{
2016 uid_t a, b, c;
2017 int ret;
2018 mm_segment_t old_fs = get_fs();
2019
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);
2023
2024 if (put_user(a, ruid) || put_user(b, euid) || put_user(c, suid))
2025 return -EFAULT;
2026 return ret;
2027}
2028
2029asmlinkage long
2030sys32_getresgid16 (u16 __user *rgid, u16 __user *egid, u16 __user *sgid)
2031{
2032 gid_t a, b, c;
2033 int ret;
2034 mm_segment_t old_fs = get_fs();
2035
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);
2039
2040 if (ret)
2041 return ret;
2042
2043 return put_user(a, rgid) | put_user(b, egid) | put_user(c, sgid);
2044}
2045
2046asmlinkage long
2047sys32_lseek (unsigned int fd, int offset, unsigned int whence)
2048{
2049 /* Sign-extension of "offset" is important here... */
2050 return sys_lseek(fd, offset, whence);
2051}
2052
2053static int
2054groups16_to_user(short __user *grouplist, struct group_info *group_info)
2055{
2056 int i;
2057 short group;
2058
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;
2063 }
2064
2065 return 0;
2066}
2067
2068static int
2069groups16_from_user(struct group_info *group_info, short __user *grouplist)
2070{
2071 int i;
2072 short group;
2073
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;
2078 }
2079
2080 return 0;
2081}
2082
2083asmlinkage long
2084sys32_getgroups16 (int gidsetsize, short __user *grouplist)
2085{
2086 int i;
2087
2088 if (gidsetsize < 0)
2089 return -EINVAL;
2090
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;
2097 }
2098 if (groups16_to_user(grouplist, current->group_info)) {
2099 i = -EFAULT;
2100 goto out;
2101 }
2102 }
2103out:
2104 put_group_info(current->group_info);
2105 return i;
2106}
2107
2108asmlinkage long
2109sys32_setgroups16 (int gidsetsize, short __user *grouplist)
2110{
2111 struct group_info *group_info;
2112 int retval;
2113
2114 if (!capable(CAP_SETGID))
2115 return -EPERM;
2116 if ((unsigned)gidsetsize > NGROUPS_MAX)
2117 return -EINVAL;
2118
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;
2126 }
2127
2128 retval = set_current_groups(group_info);
2129 put_group_info(group_info);
2130
2131 return retval;
2132}
2133
2134asmlinkage long
2135sys32_truncate64 (unsigned int path, unsigned int len_lo, unsigned int len_hi)
2136{
2137 return sys_truncate(compat_ptr(path), ((unsigned long) len_hi << 32) | len_lo);
2138}
2139
2140asmlinkage long
2141sys32_ftruncate64 (int fd, unsigned int len_lo, unsigned int len_hi)
2142{
2143 return sys_ftruncate(fd, ((unsigned long) len_hi << 32) | len_lo);
2144}
2145
2146static int
2147putstat64 (struct stat64 __user *ubuf, struct kstat *kbuf)
2148{
2149 int err;
2150 u64 hdev;
2151
2152 if (clear_user(ubuf, sizeof(*ubuf)))
2153 return -EFAULT;
2154
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;
2179}
2180
2181asmlinkage long
2182sys32_stat64 (char __user *filename, struct stat64 __user *statbuf)
2183{
2184 struct kstat s;
2185 long ret = vfs_stat(filename, &s);
2186 if (!ret)
2187 ret = putstat64(statbuf, &s);
2188 return ret;
2189}
2190
2191asmlinkage long
2192sys32_lstat64 (char __user *filename, struct stat64 __user *statbuf)
2193{
2194 struct kstat s;
2195 long ret = vfs_lstat(filename, &s);
2196 if (!ret)
2197 ret = putstat64(statbuf, &s);
2198 return ret;
2199}
2200
2201asmlinkage long
2202sys32_fstat64 (unsigned int fd, struct stat64 __user *statbuf)
2203{
2204 struct kstat s;
2205 long ret = vfs_fstat(fd, &s);
2206 if (!ret)
2207 ret = putstat64(statbuf, &s);
2208 return ret;
2209}
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211asmlinkage long
2212sys32_sched_rr_get_interval (pid_t pid, struct compat_timespec __user *interval)
2213{
2214 mm_segment_t old_fs = get_fs();
2215 struct timespec t;
2216 long ret;
2217
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;
2224}
2225
2226asmlinkage long
2227sys32_pread (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2228{
2229 return sys_pread64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2230}
2231
2232asmlinkage long
2233sys32_pwrite (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
2234{
2235 return sys_pwrite64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
2236}
2237
2238asmlinkage long
2239sys32_sendfile (int out_fd, int in_fd, int __user *offset, unsigned int count)
2240{
2241 mm_segment_t old_fs = get_fs();
2242 long ret;
2243 off_t of;
2244
2245 if (offset && get_user(of, offset))
2246 return -EFAULT;
2247
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);
2251
Tsuneo.Yoshioka@f-secure.com83b942b2005-09-12 18:49:24 +02002252 if (offset && put_user(of, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return -EFAULT;
2254
2255 return ret;
2256}
2257
2258asmlinkage long
2259sys32_personality (unsigned int personality)
2260{
2261 long ret;
2262
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;
2269}
2270
2271asmlinkage unsigned long
2272sys32_brk (unsigned int brk)
2273{
2274 unsigned long ret, obrk;
2275 struct mm_struct *mm = current->mm;
2276
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;
2282}
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284/* Structure for ia32 emulation on ia64 */
2285struct epoll_event32
2286{
2287 u32 events;
2288 u32 data[2];
2289};
2290
2291asmlinkage long
2292sys32_epoll_ctl(int epfd, int op, int fd, struct epoll_event32 __user *event)
2293{
2294 mm_segment_t old_fs = get_fs();
2295 struct epoll_event event64;
2296 int error;
2297 u32 data_halfword;
2298
2299 if (!access_ok(VERIFY_READ, event, sizeof(struct epoll_event32)))
2300 return -EFAULT;
2301
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;
2307
2308 set_fs(KERNEL_DS);
2309 error = sys_epoll_ctl(epfd, op, fd, (struct epoll_event __user *) &event64);
2310 set_fs(old_fs);
2311
2312 return error;
2313}
2314
2315asmlinkage long
2316sys32_epoll_wait(int epfd, struct epoll_event32 __user * events, int maxevents,
2317 int timeout)
2318{
2319 struct epoll_event *events64 = NULL;
2320 mm_segment_t old_fs = get_fs();
Peter Chubbd8caebd2005-05-31 22:37:00 -07002321 int numevents, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 int evt_idx;
2323 int do_free_pages = 0;
2324
2325 if (maxevents <= 0) {
2326 return -EINVAL;
2327 }
2328
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;
2332
2333 /*
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.
2337 */
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;
2346 }
2347
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);
2353
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]);
2365 }
2366 }
2367
2368 if (do_free_pages)
2369 free_pages((unsigned long) events64, get_order(size));
2370 else
2371 kfree(events64);
2372 return numevents;
2373}
2374
2375/*
2376 * Get a yet unused TLS descriptor index.
2377 */
2378static int
2379get_free_idx (void)
2380{
2381 struct thread_struct *t = &current->thread;
2382 int idx;
2383
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;
2388}
2389
2390/*
2391 * Set a given TLS descriptor:
2392 */
2393asmlinkage int
2394sys32_set_thread_area (struct ia32_user_desc __user *u_info)
2395{
2396 struct thread_struct *t = &current->thread;
2397 struct ia32_user_desc info;
2398 struct desc_struct *desc;
2399 int cpu, idx;
2400
2401 if (copy_from_user(&info, u_info, sizeof(info)))
2402 return -EFAULT;
2403 idx = info.entry_number;
2404
2405 /*
2406 * index -1 means the kernel should try to find and allocate an empty descriptor:
2407 */
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;
2414 }
2415
2416 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2417 return -EINVAL;
2418
2419 desc = t->tls_array + idx - GDT_ENTRY_TLS_MIN;
2420
2421 cpu = smp_processor_id();
2422
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);
2429 }
2430 load_TLS(t, cpu);
2431 return 0;
2432}
2433
2434/*
2435 * Get the current Thread-Local Storage area:
2436 */
2437
2438#define GET_BASE(desc) ( \
2439 (((desc)->a >> 16) & 0x0000ffff) | \
2440 (((desc)->b << 16) & 0x00ff0000) | \
2441 ( (desc)->b & 0xff000000) )
2442
2443#define GET_LIMIT(desc) ( \
2444 ((desc)->a & 0x0ffff) | \
2445 ((desc)->b & 0xf0000) )
2446
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)
2453
2454asmlinkage int
2455sys32_get_thread_area (struct ia32_user_desc __user *u_info)
2456{
2457 struct ia32_user_desc info;
2458 struct desc_struct *desc;
2459 int idx;
2460
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;
2465
2466 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
2467
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);
2477
2478 if (copy_to_user(u_info, &info, sizeof(info)))
2479 return -EFAULT;
2480 return 0;
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
2484 __u32 len_low, __u32 len_high, int advice)
2485{
2486 return sys_fadvise64_64(fd,
2487 (((u64)offset_high)<<32) | offset_low,
2488 (((u64)len_high)<<32) | len_low,
2489 advice);
2490}
2491
2492#ifdef NOTYET /* UNTESTED FOR IA64 FROM HERE DOWN */
2493
2494asmlinkage long sys32_setreuid(compat_uid_t ruid, compat_uid_t euid)
2495{
2496 uid_t sruid, seuid;
2497
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);
2501}
2502
2503asmlinkage long
2504sys32_setresuid(compat_uid_t ruid, compat_uid_t euid,
2505 compat_uid_t suid)
2506{
2507 uid_t sruid, seuid, ssuid;
2508
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);
2513}
2514
2515asmlinkage long
2516sys32_setregid(compat_gid_t rgid, compat_gid_t egid)
2517{
2518 gid_t srgid, segid;
2519
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);
2523}
2524
2525asmlinkage long
2526sys32_setresgid(compat_gid_t rgid, compat_gid_t egid,
2527 compat_gid_t sgid)
2528{
2529 gid_t srgid, segid, ssgid;
2530
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);
2535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536#endif /* NOTYET */