blob: 800a7174a5cb07d848d31906f26739c43e02b57e [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>
Shaohua Li75529212008-02-28 16:09:33 +080041#include <linux/regset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/stat.h>
43#include <linux/ipc.h>
Randy Dunlapa9415642006-01-11 12:17:48 -080044#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/compat.h>
46#include <linux/vfs.h>
47#include <linux/mman.h>
Jes Sorensen92ff2ec2006-01-18 23:46:51 -080048#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/intrinsics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/types.h>
52#include <asm/uaccess.h>
53#include <asm/unistd.h>
54
55#include "ia32priv.h"
56
57#include <net/scm.h>
58#include <net/sock.h>
59
60#define DEBUG 0
61
62#if DEBUG
63# define DBG(fmt...) printk(KERN_DEBUG fmt)
64#else
65# define DBG(fmt...)
66#endif
67
68#define ROUND_UP(x,a) ((__typeof__(x))(((unsigned long)(x) + ((a) - 1)) & ~((a) - 1)))
69
70#define OFFSET4K(a) ((a) & 0xfff)
71#define PAGE_START(addr) ((addr) & PAGE_MASK)
72#define MINSIGSTKSZ_IA32 2048
73
74#define high2lowuid(uid) ((uid) > 65535 ? 65534 : (uid))
75#define high2lowgid(gid) ((gid) > 65535 ? 65534 : (gid))
76
77/*
78 * Anything that modifies or inspects ia32 user virtual memory must hold this semaphore
79 * while doing so.
80 */
81/* XXX make per-mm: */
Jes Sorensen92ff2ec2006-01-18 23:46:51 -080082static DEFINE_MUTEX(ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84asmlinkage long
85sys32_execve (char __user *name, compat_uptr_t __user *argv, compat_uptr_t __user *envp,
86 struct pt_regs *regs)
87{
88 long error;
89 char *filename;
90 unsigned long old_map_base, old_task_size, tssd;
91
92 filename = getname(name);
93 error = PTR_ERR(filename);
94 if (IS_ERR(filename))
95 return error;
96
97 old_map_base = current->thread.map_base;
98 old_task_size = current->thread.task_size;
99 tssd = ia64_get_kr(IA64_KR_TSSD);
100
101 /* we may be exec'ing a 64-bit process: reset map base, task-size, and io-base: */
102 current->thread.map_base = DEFAULT_MAP_BASE;
103 current->thread.task_size = DEFAULT_TASK_SIZE;
104 ia64_set_kr(IA64_KR_IO_BASE, current->thread.old_iob);
105 ia64_set_kr(IA64_KR_TSSD, current->thread.old_k1);
106
107 error = compat_do_execve(filename, argv, envp, regs);
108 putname(filename);
109
110 if (error < 0) {
111 /* oops, execve failed, switch back to old values... */
112 ia64_set_kr(IA64_KR_IO_BASE, IA32_IOBASE);
113 ia64_set_kr(IA64_KR_TSSD, tssd);
114 current->thread.map_base = old_map_base;
115 current->thread.task_size = old_task_size;
116 }
117
118 return error;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122#if PAGE_SHIFT > IA32_PAGE_SHIFT
123
124
125static int
126get_page_prot (struct vm_area_struct *vma, unsigned long addr)
127{
128 int prot = 0;
129
130 if (!vma || vma->vm_start > addr)
131 return 0;
132
133 if (vma->vm_flags & VM_READ)
134 prot |= PROT_READ;
135 if (vma->vm_flags & VM_WRITE)
136 prot |= PROT_WRITE;
137 if (vma->vm_flags & VM_EXEC)
138 prot |= PROT_EXEC;
139 return prot;
140}
141
142/*
143 * Map a subpage by creating an anonymous page that contains the union of the old page and
144 * the subpage.
145 */
146static unsigned long
147mmap_subpage (struct file *file, unsigned long start, unsigned long end, int prot, int flags,
148 loff_t off)
149{
150 void *page = NULL;
151 struct inode *inode;
152 unsigned long ret = 0;
153 struct vm_area_struct *vma = find_vma(current->mm, start);
154 int old_prot = get_page_prot(vma, start);
155
156 DBG("mmap_subpage(file=%p,start=0x%lx,end=0x%lx,prot=%x,flags=%x,off=0x%llx)\n",
157 file, start, end, prot, flags, off);
158
159
160 /* Optimize the case where the old mmap and the new mmap are both anonymous */
161 if ((old_prot & PROT_WRITE) && (flags & MAP_ANONYMOUS) && !vma->vm_file) {
162 if (clear_user((void __user *) start, end - start)) {
163 ret = -EFAULT;
164 goto out;
165 }
166 goto skip_mmap;
167 }
168
169 page = (void *) get_zeroed_page(GFP_KERNEL);
170 if (!page)
171 return -ENOMEM;
172
173 if (old_prot)
174 copy_from_user(page, (void __user *) PAGE_START(start), PAGE_SIZE);
175
176 down_write(&current->mm->mmap_sem);
177 {
178 ret = do_mmap(NULL, PAGE_START(start), PAGE_SIZE, prot | PROT_WRITE,
179 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
180 }
181 up_write(&current->mm->mmap_sem);
182
183 if (IS_ERR((void *) ret))
184 goto out;
185
186 if (old_prot) {
187 /* copy back the old page contents. */
188 if (offset_in_page(start))
189 copy_to_user((void __user *) PAGE_START(start), page,
190 offset_in_page(start));
191 if (offset_in_page(end))
192 copy_to_user((void __user *) end, page + offset_in_page(end),
193 PAGE_SIZE - offset_in_page(end));
194 }
195
196 if (!(flags & MAP_ANONYMOUS)) {
197 /* read the file contents */
Josef Sipekb66ffad2006-12-08 02:37:09 -0800198 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!inode->i_fop || !file->f_op->read
200 || ((*file->f_op->read)(file, (char __user *) start, end - start, &off) < 0))
201 {
202 ret = -EINVAL;
203 goto out;
204 }
205 }
206
207 skip_mmap:
208 if (!(prot & PROT_WRITE))
209 ret = sys_mprotect(PAGE_START(start), PAGE_SIZE, prot | old_prot);
210 out:
211 if (page)
212 free_page((unsigned long) page);
213 return ret;
214}
215
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700216/* SLAB cache for ia64_partial_page structures */
217struct kmem_cache *ia64_partial_page_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/*
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700220 * init ia64_partial_page_list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * return 0 means kmalloc fail.
222 */
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700223struct ia64_partial_page_list*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224ia32_init_pp_list(void)
225{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700226 struct ia64_partial_page_list *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if ((p = kmalloc(sizeof(*p), GFP_KERNEL)) == NULL)
229 return p;
230 p->pp_head = NULL;
231 p->ppl_rb = RB_ROOT;
232 p->pp_hint = NULL;
233 atomic_set(&p->pp_count, 1);
234 return p;
235}
236
237/*
238 * Search for the partial page with @start in partial page list @ppl.
239 * If finds the partial page, return the found partial page.
240 * Else, return 0 and provide @pprev, @rb_link, @rb_parent to
241 * be used by later __ia32_insert_pp().
242 */
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700243static struct ia64_partial_page *
244__ia32_find_pp(struct ia64_partial_page_list *ppl, unsigned int start,
245 struct ia64_partial_page **pprev, struct rb_node ***rb_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct rb_node **rb_parent)
247{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700248 struct ia64_partial_page *pp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct rb_node **__rb_link, *__rb_parent, *rb_prev;
250
251 pp = ppl->pp_hint;
252 if (pp && pp->base == start)
253 return pp;
254
255 __rb_link = &ppl->ppl_rb.rb_node;
256 rb_prev = __rb_parent = NULL;
257
258 while (*__rb_link) {
259 __rb_parent = *__rb_link;
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700260 pp = rb_entry(__rb_parent, struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (pp->base == start) {
263 ppl->pp_hint = pp;
264 return pp;
265 } else if (pp->base < start) {
266 rb_prev = __rb_parent;
267 __rb_link = &__rb_parent->rb_right;
268 } else {
269 __rb_link = &__rb_parent->rb_left;
270 }
271 }
272
273 *rb_link = __rb_link;
274 *rb_parent = __rb_parent;
275 *pprev = NULL;
276 if (rb_prev)
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700277 *pprev = rb_entry(rb_prev, struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return NULL;
279}
280
281/*
282 * insert @pp into @ppl.
283 */
284static void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700285__ia32_insert_pp(struct ia64_partial_page_list *ppl,
286 struct ia64_partial_page *pp, struct ia64_partial_page *prev,
287 struct rb_node **rb_link, struct rb_node *rb_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 /* link list */
290 if (prev) {
291 pp->next = prev->next;
292 prev->next = pp;
293 } else {
294 ppl->pp_head = pp;
295 if (rb_parent)
296 pp->next = rb_entry(rb_parent,
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700297 struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 else
299 pp->next = NULL;
300 }
301
302 /* link rb */
303 rb_link_node(&pp->pp_rb, rb_parent, rb_link);
304 rb_insert_color(&pp->pp_rb, &ppl->ppl_rb);
305
306 ppl->pp_hint = pp;
307}
308
309/*
310 * delete @pp from partial page list @ppl.
311 */
312static void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700313__ia32_delete_pp(struct ia64_partial_page_list *ppl,
314 struct ia64_partial_page *pp, struct ia64_partial_page *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 if (prev) {
317 prev->next = pp->next;
318 if (ppl->pp_hint == pp)
319 ppl->pp_hint = prev;
320 } else {
321 ppl->pp_head = pp->next;
322 if (ppl->pp_hint == pp)
323 ppl->pp_hint = pp->next;
324 }
325 rb_erase(&pp->pp_rb, &ppl->ppl_rb);
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700326 kmem_cache_free(ia64_partial_page_cachep, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700329static struct ia64_partial_page *
330__pp_prev(struct ia64_partial_page *pp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct rb_node *prev = rb_prev(&pp->pp_rb);
333 if (prev)
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700334 return rb_entry(prev, struct ia64_partial_page, pp_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 else
336 return NULL;
337}
338
339/*
340 * Delete partial pages with address between @start and @end.
341 * @start and @end are page aligned.
342 */
343static void
344__ia32_delete_pp_range(unsigned int start, unsigned int end)
345{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700346 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct rb_node **rb_link, *rb_parent;
348
349 if (start >= end)
350 return;
351
352 pp = __ia32_find_pp(current->thread.ppl, start, &prev,
353 &rb_link, &rb_parent);
354 if (pp)
355 prev = __pp_prev(pp);
356 else {
357 if (prev)
358 pp = prev->next;
359 else
360 pp = current->thread.ppl->pp_head;
361 }
362
363 while (pp && pp->base < end) {
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700364 struct ia64_partial_page *tmp = pp->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 __ia32_delete_pp(current->thread.ppl, pp, prev);
366 pp = tmp;
367 }
368}
369
370/*
371 * Set the range between @start and @end in bitmap.
372 * @start and @end should be IA32 page aligned and in the same IA64 page.
373 */
374static int
375__ia32_set_pp(unsigned int start, unsigned int end, int flags)
376{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700377 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct rb_node ** rb_link, *rb_parent;
379 unsigned int pstart, start_bit, end_bit, i;
380
381 pstart = PAGE_START(start);
382 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
383 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
384 if (end_bit == 0)
385 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
386 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
387 &rb_link, &rb_parent);
388 if (pp) {
389 for (i = start_bit; i < end_bit; i++)
390 set_bit(i, &pp->bitmap);
391 /*
392 * Check: if this partial page has been set to a full page,
393 * then delete it.
394 */
395 if (find_first_zero_bit(&pp->bitmap, sizeof(pp->bitmap)*8) >=
396 PAGE_SIZE/IA32_PAGE_SIZE) {
397 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
398 }
399 return 0;
400 }
401
402 /*
403 * MAP_FIXED may lead to overlapping mmap.
404 * In this case, the requested mmap area may already mmaped as a full
405 * page. So check vma before adding a new partial page.
406 */
407 if (flags & MAP_FIXED) {
408 struct vm_area_struct *vma = find_vma(current->mm, pstart);
409 if (vma && vma->vm_start <= pstart)
410 return 0;
411 }
412
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700413 /* new a ia64_partial_page */
414 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!pp)
416 return -ENOMEM;
417 pp->base = pstart;
418 pp->bitmap = 0;
419 for (i=start_bit; i<end_bit; i++)
420 set_bit(i, &(pp->bitmap));
421 pp->next = NULL;
422 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
423 return 0;
424}
425
426/*
427 * @start and @end should be IA32 page aligned, but don't need to be in the
428 * same IA64 page. Split @start and @end to make sure they're in the same IA64
429 * page, then call __ia32_set_pp().
430 */
431static void
432ia32_set_pp(unsigned int start, unsigned int end, int flags)
433{
434 down_write(&current->mm->mmap_sem);
435 if (flags & MAP_FIXED) {
436 /*
437 * MAP_FIXED may lead to overlapping mmap. When this happens,
438 * a series of complete IA64 pages results in deletion of
439 * old partial pages in that range.
440 */
441 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
442 }
443
444 if (end < PAGE_ALIGN(start)) {
445 __ia32_set_pp(start, end, flags);
446 } else {
447 if (offset_in_page(start))
448 __ia32_set_pp(start, PAGE_ALIGN(start), flags);
449 if (offset_in_page(end))
450 __ia32_set_pp(PAGE_START(end), end, flags);
451 }
452 up_write(&current->mm->mmap_sem);
453}
454
455/*
456 * Unset the range between @start and @end in bitmap.
457 * @start and @end should be IA32 page aligned and in the same IA64 page.
458 * After doing that, if the bitmap is 0, then free the page and return 1,
459 * else return 0;
460 * If not find the partial page in the list, then
461 * If the vma exists, then the full page is set to a partial page;
462 * Else return -ENOMEM.
463 */
464static int
465__ia32_unset_pp(unsigned int start, unsigned int end)
466{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700467 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct rb_node ** rb_link, *rb_parent;
469 unsigned int pstart, start_bit, end_bit, i;
470 struct vm_area_struct *vma;
471
472 pstart = PAGE_START(start);
473 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
474 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
475 if (end_bit == 0)
476 end_bit = PAGE_SIZE / IA32_PAGE_SIZE;
477
478 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
479 &rb_link, &rb_parent);
480 if (pp) {
481 for (i = start_bit; i < end_bit; i++)
482 clear_bit(i, &pp->bitmap);
483 if (pp->bitmap == 0) {
484 __ia32_delete_pp(current->thread.ppl, pp, __pp_prev(pp));
485 return 1;
486 }
487 return 0;
488 }
489
490 vma = find_vma(current->mm, pstart);
491 if (!vma || vma->vm_start > pstart) {
492 return -ENOMEM;
493 }
494
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700495 /* new a ia64_partial_page */
496 pp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!pp)
498 return -ENOMEM;
499 pp->base = pstart;
500 pp->bitmap = 0;
501 for (i = 0; i < start_bit; i++)
502 set_bit(i, &(pp->bitmap));
503 for (i = end_bit; i < PAGE_SIZE / IA32_PAGE_SIZE; i++)
504 set_bit(i, &(pp->bitmap));
505 pp->next = NULL;
506 __ia32_insert_pp(current->thread.ppl, pp, prev, rb_link, rb_parent);
507 return 0;
508}
509
510/*
511 * Delete pp between PAGE_ALIGN(start) and PAGE_START(end) by calling
512 * __ia32_delete_pp_range(). Unset possible partial pages by calling
513 * __ia32_unset_pp().
514 * The returned value see __ia32_unset_pp().
515 */
516static int
517ia32_unset_pp(unsigned int *startp, unsigned int *endp)
518{
519 unsigned int start = *startp, end = *endp;
520 int ret = 0;
521
522 down_write(&current->mm->mmap_sem);
523
524 __ia32_delete_pp_range(PAGE_ALIGN(start), PAGE_START(end));
525
526 if (end < PAGE_ALIGN(start)) {
527 ret = __ia32_unset_pp(start, end);
528 if (ret == 1) {
529 *startp = PAGE_START(start);
530 *endp = PAGE_ALIGN(end);
531 }
532 if (ret == 0) {
533 /* to shortcut sys_munmap() in sys32_munmap() */
534 *startp = PAGE_START(start);
535 *endp = PAGE_START(end);
536 }
537 } else {
538 if (offset_in_page(start)) {
539 ret = __ia32_unset_pp(start, PAGE_ALIGN(start));
540 if (ret == 1)
541 *startp = PAGE_START(start);
542 if (ret == 0)
543 *startp = PAGE_ALIGN(start);
544 if (ret < 0)
545 goto out;
546 }
547 if (offset_in_page(end)) {
548 ret = __ia32_unset_pp(PAGE_START(end), end);
549 if (ret == 1)
550 *endp = PAGE_ALIGN(end);
551 if (ret == 0)
552 *endp = PAGE_START(end);
553 }
554 }
555
556 out:
557 up_write(&current->mm->mmap_sem);
558 return ret;
559}
560
561/*
562 * Compare the range between @start and @end with bitmap in partial page.
563 * @start and @end should be IA32 page aligned and in the same IA64 page.
564 */
565static int
566__ia32_compare_pp(unsigned int start, unsigned int end)
567{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700568 struct ia64_partial_page *pp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct rb_node ** rb_link, *rb_parent;
570 unsigned int pstart, start_bit, end_bit, size;
571 unsigned int first_bit, next_zero_bit; /* the first range in bitmap */
572
573 pstart = PAGE_START(start);
574
575 pp = __ia32_find_pp(current->thread.ppl, pstart, &prev,
576 &rb_link, &rb_parent);
577 if (!pp)
578 return 1;
579
580 start_bit = (start % PAGE_SIZE) / IA32_PAGE_SIZE;
581 end_bit = (end % PAGE_SIZE) / IA32_PAGE_SIZE;
582 size = sizeof(pp->bitmap) * 8;
583 first_bit = find_first_bit(&pp->bitmap, size);
584 next_zero_bit = find_next_zero_bit(&pp->bitmap, size, first_bit);
585 if ((start_bit < first_bit) || (end_bit > next_zero_bit)) {
586 /* exceeds the first range in bitmap */
587 return -ENOMEM;
588 } else if ((start_bit == first_bit) && (end_bit == next_zero_bit)) {
589 first_bit = find_next_bit(&pp->bitmap, size, next_zero_bit);
590 if ((next_zero_bit < first_bit) && (first_bit < size))
591 return 1; /* has next range */
592 else
593 return 0; /* no next range */
594 } else
595 return 1;
596}
597
598/*
599 * @start and @end should be IA32 page aligned, but don't need to be in the
600 * same IA64 page. Split @start and @end to make sure they're in the same IA64
601 * page, then call __ia32_compare_pp().
602 *
603 * Take this as example: the range is the 1st and 2nd 4K page.
604 * Return 0 if they fit bitmap exactly, i.e. bitmap = 00000011;
605 * Return 1 if the range doesn't cover whole bitmap, e.g. bitmap = 00001111;
606 * Return -ENOMEM if the range exceeds the bitmap, e.g. bitmap = 00000001 or
607 * bitmap = 00000101.
608 */
609static int
610ia32_compare_pp(unsigned int *startp, unsigned int *endp)
611{
612 unsigned int start = *startp, end = *endp;
613 int retval = 0;
614
615 down_write(&current->mm->mmap_sem);
616
617 if (end < PAGE_ALIGN(start)) {
618 retval = __ia32_compare_pp(start, end);
619 if (retval == 0) {
620 *startp = PAGE_START(start);
621 *endp = PAGE_ALIGN(end);
622 }
623 } else {
624 if (offset_in_page(start)) {
625 retval = __ia32_compare_pp(start,
626 PAGE_ALIGN(start));
627 if (retval == 0)
628 *startp = PAGE_START(start);
629 if (retval < 0)
630 goto out;
631 }
632 if (offset_in_page(end)) {
633 retval = __ia32_compare_pp(PAGE_START(end), end);
634 if (retval == 0)
635 *endp = PAGE_ALIGN(end);
636 }
637 }
638
639 out:
640 up_write(&current->mm->mmap_sem);
641 return retval;
642}
643
644static void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700645__ia32_drop_pp_list(struct ia64_partial_page_list *ppl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700647 struct ia64_partial_page *pp = ppl->pp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 while (pp) {
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700650 struct ia64_partial_page *next = pp->next;
651 kmem_cache_free(ia64_partial_page_cachep, pp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 pp = next;
653 }
654
655 kfree(ppl);
656}
657
658void
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700659ia32_drop_ia64_partial_page_list(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700661 struct ia64_partial_page_list* ppl = task->thread.ppl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (ppl && atomic_dec_and_test(&ppl->pp_count))
664 __ia32_drop_pp_list(ppl);
665}
666
667/*
668 * Copy current->thread.ppl to ppl (already initialized).
669 */
670static int
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700671__ia32_copy_pp_list(struct ia64_partial_page_list *ppl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700673 struct ia64_partial_page *pp, *tmp, *prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 struct rb_node **rb_link, *rb_parent;
675
676 ppl->pp_head = NULL;
677 ppl->pp_hint = NULL;
678 ppl->ppl_rb = RB_ROOT;
679 rb_link = &ppl->ppl_rb.rb_node;
680 rb_parent = NULL;
681 prev = NULL;
682
683 for (pp = current->thread.ppl->pp_head; pp; pp = pp->next) {
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700684 tmp = kmem_cache_alloc(ia64_partial_page_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!tmp)
686 return -ENOMEM;
687 *tmp = *pp;
688 __ia32_insert_pp(ppl, tmp, prev, rb_link, rb_parent);
689 prev = tmp;
690 rb_link = &tmp->pp_rb.rb_right;
691 rb_parent = &tmp->pp_rb;
692 }
693 return 0;
694}
695
696int
akpm@linux-foundation.org3b74d182007-07-24 19:44:55 -0700697ia32_copy_ia64_partial_page_list(struct task_struct *p,
698 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 int retval = 0;
701
702 if (clone_flags & CLONE_VM) {
703 atomic_inc(&current->thread.ppl->pp_count);
704 p->thread.ppl = current->thread.ppl;
705 } else {
706 p->thread.ppl = ia32_init_pp_list();
707 if (!p->thread.ppl)
708 return -ENOMEM;
709 down_write(&current->mm->mmap_sem);
710 {
711 retval = __ia32_copy_pp_list(p->thread.ppl);
712 }
713 up_write(&current->mm->mmap_sem);
714 }
715
716 return retval;
717}
718
719static unsigned long
720emulate_mmap (struct file *file, unsigned long start, unsigned long len, int prot, int flags,
721 loff_t off)
722{
723 unsigned long tmp, end, pend, pstart, ret, is_congruent, fudge = 0;
724 struct inode *inode;
725 loff_t poff;
726
727 end = start + len;
728 pstart = PAGE_START(start);
729 pend = PAGE_ALIGN(end);
730
731 if (flags & MAP_FIXED) {
732 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
733 if (start > pstart) {
734 if (flags & MAP_SHARED)
735 printk(KERN_INFO
736 "%s(%d): emulate_mmap() can't share head (addr=0x%lx)\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700737 current->comm, task_pid_nr(current), start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ret = mmap_subpage(file, start, min(PAGE_ALIGN(start), end), prot, flags,
739 off);
740 if (IS_ERR((void *) ret))
741 return ret;
742 pstart += PAGE_SIZE;
743 if (pstart >= pend)
744 goto out; /* done */
745 }
746 if (end < pend) {
747 if (flags & MAP_SHARED)
748 printk(KERN_INFO
749 "%s(%d): emulate_mmap() can't share tail (end=0x%lx)\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700750 current->comm, task_pid_nr(current), end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ret = mmap_subpage(file, max(start, PAGE_START(end)), end, prot, flags,
752 (off + len) - offset_in_page(end));
753 if (IS_ERR((void *) ret))
754 return ret;
755 pend -= PAGE_SIZE;
756 if (pstart >= pend)
757 goto out; /* done */
758 }
759 } else {
760 /*
761 * If a start address was specified, use it if the entire rounded out area
762 * is available.
763 */
764 if (start && !pstart)
765 fudge = 1; /* handle case of mapping to range (0,PAGE_SIZE) */
766 tmp = arch_get_unmapped_area(file, pstart - fudge, pend - pstart, 0, flags);
767 if (tmp != pstart) {
768 pstart = tmp;
769 start = pstart + offset_in_page(off); /* make start congruent with off */
770 end = start + len;
771 pend = PAGE_ALIGN(end);
772 }
773 }
774
775 poff = off + (pstart - start); /* note: (pstart - start) may be negative */
776 is_congruent = (flags & MAP_ANONYMOUS) || (offset_in_page(poff) == 0);
777
778 if ((flags & MAP_SHARED) && !is_congruent)
779 printk(KERN_INFO "%s(%d): emulate_mmap() can't share contents of incongruent mmap "
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700780 "(addr=0x%lx,off=0x%llx)\n", current->comm, task_pid_nr(current), start, off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 DBG("mmap_body: mapping [0x%lx-0x%lx) %s with poff 0x%llx\n", pstart, pend,
783 is_congruent ? "congruent" : "not congruent", poff);
784
785 down_write(&current->mm->mmap_sem);
786 {
787 if (!(flags & MAP_ANONYMOUS) && is_congruent)
788 ret = do_mmap(file, pstart, pend - pstart, prot, flags | MAP_FIXED, poff);
789 else
790 ret = do_mmap(NULL, pstart, pend - pstart,
791 prot | ((flags & MAP_ANONYMOUS) ? 0 : PROT_WRITE),
792 flags | MAP_FIXED | MAP_ANONYMOUS, 0);
793 }
794 up_write(&current->mm->mmap_sem);
795
796 if (IS_ERR((void *) ret))
797 return ret;
798
799 if (!is_congruent) {
800 /* read the file contents */
Josef Sipekb66ffad2006-12-08 02:37:09 -0800801 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!inode->i_fop || !file->f_op->read
803 || ((*file->f_op->read)(file, (char __user *) pstart, pend - pstart, &poff)
804 < 0))
805 {
806 sys_munmap(pstart, pend - pstart);
807 return -EINVAL;
808 }
809 if (!(prot & PROT_WRITE) && sys_mprotect(pstart, pend - pstart, prot) < 0)
810 return -EINVAL;
811 }
812
813 if (!(flags & MAP_FIXED))
814 ia32_set_pp((unsigned int)start, (unsigned int)end, flags);
815out:
816 return start;
817}
818
819#endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
820
821static inline unsigned int
822get_prot32 (unsigned int prot)
823{
824 if (prot & PROT_WRITE)
825 /* on x86, PROT_WRITE implies PROT_READ which implies PROT_EEC */
826 prot |= PROT_READ | PROT_WRITE | PROT_EXEC;
827 else if (prot & (PROT_READ | PROT_EXEC))
828 /* on x86, there is no distinction between PROT_READ and PROT_EXEC */
829 prot |= (PROT_READ | PROT_EXEC);
830
831 return prot;
832}
833
834unsigned long
835ia32_do_mmap (struct file *file, unsigned long addr, unsigned long len, int prot, int flags,
836 loff_t offset)
837{
838 DBG("ia32_do_mmap(file=%p,addr=0x%lx,len=0x%lx,prot=%x,flags=%x,offset=0x%llx)\n",
839 file, addr, len, prot, flags, offset);
840
841 if (file && (!file->f_op || !file->f_op->mmap))
842 return -ENODEV;
843
844 len = IA32_PAGE_ALIGN(len);
845 if (len == 0)
846 return addr;
847
848 if (len > IA32_PAGE_OFFSET || addr > IA32_PAGE_OFFSET - len)
849 {
850 if (flags & MAP_FIXED)
851 return -ENOMEM;
852 else
853 return -EINVAL;
854 }
855
856 if (OFFSET4K(offset))
857 return -EINVAL;
858
859 prot = get_prot32(prot);
860
861#if PAGE_SHIFT > IA32_PAGE_SHIFT
Jes Sorensen92ff2ec2006-01-18 23:46:51 -0800862 mutex_lock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 {
864 addr = emulate_mmap(file, addr, len, prot, flags, offset);
865 }
Jes Sorensen92ff2ec2006-01-18 23:46:51 -0800866 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867#else
868 down_write(&current->mm->mmap_sem);
869 {
870 addr = do_mmap(file, addr, len, prot, flags, offset);
871 }
872 up_write(&current->mm->mmap_sem);
873#endif
874 DBG("ia32_do_mmap: returning 0x%lx\n", addr);
875 return addr;
876}
877
878/*
879 * Linux/i386 didn't use to be able to handle more than 4 system call parameters, so these
880 * system calls used a memory block for parameter passing..
881 */
882
883struct mmap_arg_struct {
884 unsigned int addr;
885 unsigned int len;
886 unsigned int prot;
887 unsigned int flags;
888 unsigned int fd;
889 unsigned int offset;
890};
891
892asmlinkage long
893sys32_mmap (struct mmap_arg_struct __user *arg)
894{
895 struct mmap_arg_struct a;
896 struct file *file = NULL;
897 unsigned long addr;
898 int flags;
899
900 if (copy_from_user(&a, arg, sizeof(a)))
901 return -EFAULT;
902
903 if (OFFSET4K(a.offset))
904 return -EINVAL;
905
906 flags = a.flags;
907
908 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
909 if (!(flags & MAP_ANONYMOUS)) {
910 file = fget(a.fd);
911 if (!file)
912 return -EBADF;
913 }
914
915 addr = ia32_do_mmap(file, a.addr, a.len, a.prot, flags, a.offset);
916
917 if (file)
918 fput(file);
919 return addr;
920}
921
922asmlinkage long
923sys32_mmap2 (unsigned int addr, unsigned int len, unsigned int prot, unsigned int flags,
924 unsigned int fd, unsigned int pgoff)
925{
926 struct file *file = NULL;
927 unsigned long retval;
928
929 flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
930 if (!(flags & MAP_ANONYMOUS)) {
931 file = fget(fd);
932 if (!file)
933 return -EBADF;
934 }
935
936 retval = ia32_do_mmap(file, addr, len, prot, flags,
937 (unsigned long) pgoff << IA32_PAGE_SHIFT);
938
939 if (file)
940 fput(file);
941 return retval;
942}
943
944asmlinkage long
945sys32_munmap (unsigned int start, unsigned int len)
946{
947 unsigned int end = start + len;
948 long ret;
949
950#if PAGE_SHIFT <= IA32_PAGE_SHIFT
951 ret = sys_munmap(start, end - start);
952#else
953 if (OFFSET4K(start))
954 return -EINVAL;
955
956 end = IA32_PAGE_ALIGN(end);
957 if (start >= end)
958 return -EINVAL;
959
960 ret = ia32_unset_pp(&start, &end);
961 if (ret < 0)
962 return ret;
963
964 if (start >= end)
965 return 0;
966
Jes Sorensen92ff2ec2006-01-18 23:46:51 -0800967 mutex_lock(&ia32_mmap_mutex);
968 ret = sys_munmap(start, end - start);
969 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970#endif
971 return ret;
972}
973
974#if PAGE_SHIFT > IA32_PAGE_SHIFT
975
976/*
977 * When mprotect()ing a partial page, we set the permission to the union of the old
978 * settings and the new settings. In other words, it's only possible to make access to a
979 * partial page less restrictive.
980 */
981static long
982mprotect_subpage (unsigned long address, int new_prot)
983{
984 int old_prot;
985 struct vm_area_struct *vma;
986
987 if (new_prot == PROT_NONE)
988 return 0; /* optimize case where nothing changes... */
989 vma = find_vma(current->mm, address);
990 old_prot = get_page_prot(vma, address);
991 return sys_mprotect(address, PAGE_SIZE, new_prot | old_prot);
992}
993
994#endif /* PAGE_SHIFT > IA32_PAGE_SHIFT */
995
996asmlinkage long
997sys32_mprotect (unsigned int start, unsigned int len, int prot)
998{
999 unsigned int end = start + len;
1000#if PAGE_SHIFT > IA32_PAGE_SHIFT
1001 long retval = 0;
1002#endif
1003
1004 prot = get_prot32(prot);
1005
1006#if PAGE_SHIFT <= IA32_PAGE_SHIFT
1007 return sys_mprotect(start, end - start, prot);
1008#else
1009 if (OFFSET4K(start))
1010 return -EINVAL;
1011
1012 end = IA32_PAGE_ALIGN(end);
1013 if (end < start)
1014 return -EINVAL;
1015
1016 retval = ia32_compare_pp(&start, &end);
1017
1018 if (retval < 0)
1019 return retval;
1020
Jes Sorensen92ff2ec2006-01-18 23:46:51 -08001021 mutex_lock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 {
1023 if (offset_in_page(start)) {
1024 /* start address is 4KB aligned but not page aligned. */
1025 retval = mprotect_subpage(PAGE_START(start), prot);
1026 if (retval < 0)
1027 goto out;
1028
1029 start = PAGE_ALIGN(start);
1030 if (start >= end)
1031 goto out; /* retval is already zero... */
1032 }
1033
1034 if (offset_in_page(end)) {
1035 /* end address is 4KB aligned but not page aligned. */
1036 retval = mprotect_subpage(PAGE_START(end), prot);
1037 if (retval < 0)
1038 goto out;
1039
1040 end = PAGE_START(end);
1041 }
1042 retval = sys_mprotect(start, end - start, prot);
1043 }
1044 out:
Jes Sorensen92ff2ec2006-01-18 23:46:51 -08001045 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return retval;
1047#endif
1048}
1049
1050asmlinkage long
1051sys32_mremap (unsigned int addr, unsigned int old_len, unsigned int new_len,
1052 unsigned int flags, unsigned int new_addr)
1053{
1054 long ret;
1055
1056#if PAGE_SHIFT <= IA32_PAGE_SHIFT
1057 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1058#else
1059 unsigned int old_end, new_end;
1060
1061 if (OFFSET4K(addr))
1062 return -EINVAL;
1063
1064 old_len = IA32_PAGE_ALIGN(old_len);
1065 new_len = IA32_PAGE_ALIGN(new_len);
1066 old_end = addr + old_len;
1067 new_end = addr + new_len;
1068
1069 if (!new_len)
1070 return -EINVAL;
1071
1072 if ((flags & MREMAP_FIXED) && (OFFSET4K(new_addr)))
1073 return -EINVAL;
1074
1075 if (old_len >= new_len) {
1076 ret = sys32_munmap(addr + new_len, old_len - new_len);
1077 if (ret && old_len != new_len)
1078 return ret;
1079 ret = addr;
1080 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
1081 return ret;
1082 old_len = new_len;
1083 }
1084
1085 addr = PAGE_START(addr);
1086 old_len = PAGE_ALIGN(old_end) - addr;
1087 new_len = PAGE_ALIGN(new_end) - addr;
1088
Jes Sorensen92ff2ec2006-01-18 23:46:51 -08001089 mutex_lock(&ia32_mmap_mutex);
1090 ret = sys_mremap(addr, old_len, new_len, flags, new_addr);
1091 mutex_unlock(&ia32_mmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if ((ret >= 0) && (old_len < new_len)) {
1094 /* mremap expanded successfully */
1095 ia32_set_pp(old_end, new_end, flags);
1096 }
1097#endif
1098 return ret;
1099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101asmlinkage unsigned long
1102sys32_alarm (unsigned int seconds)
1103{
Thomas Gleixnerc08b8a42006-03-25 03:06:33 -08001104 return alarm_setitimer(seconds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107struct sel_arg_struct {
1108 unsigned int n;
1109 unsigned int inp;
1110 unsigned int outp;
1111 unsigned int exp;
1112 unsigned int tvp;
1113};
1114
1115asmlinkage long
1116sys32_old_select (struct sel_arg_struct __user *arg)
1117{
1118 struct sel_arg_struct a;
1119
1120 if (copy_from_user(&a, arg, sizeof(a)))
1121 return -EFAULT;
1122 return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1123 compat_ptr(a.exp), compat_ptr(a.tvp));
1124}
1125
1126#define SEMOP 1
1127#define SEMGET 2
1128#define SEMCTL 3
1129#define SEMTIMEDOP 4
1130#define MSGSND 11
1131#define MSGRCV 12
1132#define MSGGET 13
1133#define MSGCTL 14
1134#define SHMAT 21
1135#define SHMDT 22
1136#define SHMGET 23
1137#define SHMCTL 24
1138
1139asmlinkage long
1140sys32_ipc(u32 call, int first, int second, int third, u32 ptr, u32 fifth)
1141{
1142 int version;
1143
1144 version = call >> 16; /* hack for backward compatibility */
1145 call &= 0xffff;
1146
1147 switch (call) {
1148 case SEMTIMEDOP:
1149 if (fifth)
1150 return compat_sys_semtimedop(first, compat_ptr(ptr),
1151 second, compat_ptr(fifth));
1152 /* else fall through for normal semop() */
1153 case SEMOP:
1154 /* struct sembuf is the same on 32 and 64bit :)) */
1155 return sys_semtimedop(first, compat_ptr(ptr), second,
1156 NULL);
1157 case SEMGET:
1158 return sys_semget(first, second, third);
1159 case SEMCTL:
1160 return compat_sys_semctl(first, second, third, compat_ptr(ptr));
1161
1162 case MSGSND:
1163 return compat_sys_msgsnd(first, second, third, compat_ptr(ptr));
1164 case MSGRCV:
1165 return compat_sys_msgrcv(first, second, fifth, third, version, compat_ptr(ptr));
1166 case MSGGET:
1167 return sys_msgget((key_t) first, second);
1168 case MSGCTL:
1169 return compat_sys_msgctl(first, second, compat_ptr(ptr));
1170
1171 case SHMAT:
1172 return compat_sys_shmat(first, second, third, version, compat_ptr(ptr));
1173 break;
1174 case SHMDT:
1175 return sys_shmdt(compat_ptr(ptr));
1176 case SHMGET:
1177 return sys_shmget(first, (unsigned)second, third);
1178 case SHMCTL:
1179 return compat_sys_shmctl(first, second, compat_ptr(ptr));
1180
1181 default:
1182 return -ENOSYS;
1183 }
1184 return -EINVAL;
1185}
1186
1187asmlinkage long
1188compat_sys_wait4 (compat_pid_t pid, compat_uint_t * stat_addr, int options,
1189 struct compat_rusage *ru);
1190
1191asmlinkage long
1192sys32_waitpid (int pid, unsigned int *stat_addr, int options)
1193{
1194 return compat_sys_wait4(pid, stat_addr, options, NULL);
1195}
1196
1197static unsigned int
1198ia32_peek (struct task_struct *child, unsigned long addr, unsigned int *val)
1199{
1200 size_t copied;
1201 unsigned int ret;
1202
1203 copied = access_process_vm(child, addr, val, sizeof(*val), 0);
1204 return (copied != sizeof(ret)) ? -EIO : 0;
1205}
1206
1207static unsigned int
1208ia32_poke (struct task_struct *child, unsigned long addr, unsigned int val)
1209{
1210
1211 if (access_process_vm(child, addr, &val, sizeof(val), 1) != sizeof(val))
1212 return -EIO;
1213 return 0;
1214}
1215
1216/*
1217 * The order in which registers are stored in the ptrace regs structure
1218 */
1219#define PT_EBX 0
1220#define PT_ECX 1
1221#define PT_EDX 2
1222#define PT_ESI 3
1223#define PT_EDI 4
1224#define PT_EBP 5
1225#define PT_EAX 6
1226#define PT_DS 7
1227#define PT_ES 8
1228#define PT_FS 9
1229#define PT_GS 10
1230#define PT_ORIG_EAX 11
1231#define PT_EIP 12
1232#define PT_CS 13
1233#define PT_EFL 14
1234#define PT_UESP 15
1235#define PT_SS 16
1236
1237static unsigned int
1238getreg (struct task_struct *child, int regno)
1239{
1240 struct pt_regs *child_regs;
1241
Al Viro64505782006-01-12 01:06:06 -08001242 child_regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 switch (regno / sizeof(int)) {
1244 case PT_EBX: return child_regs->r11;
1245 case PT_ECX: return child_regs->r9;
1246 case PT_EDX: return child_regs->r10;
1247 case PT_ESI: return child_regs->r14;
1248 case PT_EDI: return child_regs->r15;
1249 case PT_EBP: return child_regs->r13;
1250 case PT_EAX: return child_regs->r8;
1251 case PT_ORIG_EAX: return child_regs->r1; /* see dispatch_to_ia32_handler() */
1252 case PT_EIP: return child_regs->cr_iip;
1253 case PT_UESP: return child_regs->r12;
1254 case PT_EFL: return child->thread.eflag;
1255 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1256 return __USER_DS;
1257 case PT_CS: return __USER_CS;
1258 default:
1259 printk(KERN_ERR "ia32.getreg(): unknown register %d\n", regno);
1260 break;
1261 }
1262 return 0;
1263}
1264
1265static void
1266putreg (struct task_struct *child, int regno, unsigned int value)
1267{
1268 struct pt_regs *child_regs;
1269
Al Viro64505782006-01-12 01:06:06 -08001270 child_regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 switch (regno / sizeof(int)) {
1272 case PT_EBX: child_regs->r11 = value; break;
1273 case PT_ECX: child_regs->r9 = value; break;
1274 case PT_EDX: child_regs->r10 = value; break;
1275 case PT_ESI: child_regs->r14 = value; break;
1276 case PT_EDI: child_regs->r15 = value; break;
1277 case PT_EBP: child_regs->r13 = value; break;
1278 case PT_EAX: child_regs->r8 = value; break;
1279 case PT_ORIG_EAX: child_regs->r1 = value; break;
1280 case PT_EIP: child_regs->cr_iip = value; break;
1281 case PT_UESP: child_regs->r12 = value; break;
1282 case PT_EFL: child->thread.eflag = value; break;
1283 case PT_DS: case PT_ES: case PT_FS: case PT_GS: case PT_SS:
1284 if (value != __USER_DS)
1285 printk(KERN_ERR
1286 "ia32.putreg: attempt to set invalid segment register %d = %x\n",
1287 regno, value);
1288 break;
1289 case PT_CS:
1290 if (value != __USER_CS)
1291 printk(KERN_ERR
1292 "ia32.putreg: attempt to to set invalid segment register %d = %x\n",
1293 regno, value);
1294 break;
1295 default:
1296 printk(KERN_ERR "ia32.putreg: unknown register %d\n", regno);
1297 break;
1298 }
1299}
1300
1301static void
1302put_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1303 struct switch_stack *swp, int tos)
1304{
1305 struct _fpreg_ia32 *f;
1306 char buf[32];
1307
1308 f = (struct _fpreg_ia32 *)(((unsigned long)buf + 15) & ~15);
1309 if ((regno += tos) >= 8)
1310 regno -= 8;
1311 switch (regno) {
1312 case 0:
1313 ia64f2ia32f(f, &ptp->f8);
1314 break;
1315 case 1:
1316 ia64f2ia32f(f, &ptp->f9);
1317 break;
1318 case 2:
1319 ia64f2ia32f(f, &ptp->f10);
1320 break;
1321 case 3:
1322 ia64f2ia32f(f, &ptp->f11);
1323 break;
1324 case 4:
1325 case 5:
1326 case 6:
1327 case 7:
1328 ia64f2ia32f(f, &swp->f12 + (regno - 4));
1329 break;
1330 }
1331 copy_to_user(reg, f, sizeof(*reg));
1332}
1333
1334static void
1335get_fpreg (int regno, struct _fpreg_ia32 __user *reg, struct pt_regs *ptp,
1336 struct switch_stack *swp, int tos)
1337{
1338
1339 if ((regno += tos) >= 8)
1340 regno -= 8;
1341 switch (regno) {
1342 case 0:
1343 copy_from_user(&ptp->f8, reg, sizeof(*reg));
1344 break;
1345 case 1:
1346 copy_from_user(&ptp->f9, reg, sizeof(*reg));
1347 break;
1348 case 2:
1349 copy_from_user(&ptp->f10, reg, sizeof(*reg));
1350 break;
1351 case 3:
1352 copy_from_user(&ptp->f11, reg, sizeof(*reg));
1353 break;
1354 case 4:
1355 case 5:
1356 case 6:
1357 case 7:
1358 copy_from_user(&swp->f12 + (regno - 4), reg, sizeof(*reg));
1359 break;
1360 }
1361 return;
1362}
1363
1364int
1365save_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1366{
1367 struct switch_stack *swp;
1368 struct pt_regs *ptp;
1369 int i, tos;
1370
1371 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1372 return -EFAULT;
1373
1374 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1375 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1376 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1377 __put_user(tsk->thread.fir, &save->fip);
1378 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1379 __put_user(tsk->thread.fdr, &save->foo);
1380 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1381
1382 /*
1383 * Stack frames start with 16-bytes of temp space
1384 */
1385 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001386 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 tos = (tsk->thread.fsr >> 11) & 7;
1388 for (i = 0; i < 8; i++)
1389 put_fpreg(i, &save->st_space[i], ptp, swp, tos);
1390 return 0;
1391}
1392
1393static int
1394restore_ia32_fpstate (struct task_struct *tsk, struct ia32_user_i387_struct __user *save)
1395{
1396 struct switch_stack *swp;
1397 struct pt_regs *ptp;
1398 int i, tos;
1399 unsigned int fsrlo, fsrhi, num32;
1400
1401 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1402 return(-EFAULT);
1403
1404 __get_user(num32, (unsigned int __user *)&save->cwd);
1405 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1406 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1407 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1408 num32 = (fsrhi << 16) | fsrlo;
1409 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1410 __get_user(num32, (unsigned int __user *)&save->fip);
1411 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1412 __get_user(num32, (unsigned int __user *)&save->foo);
1413 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1414
1415 /*
1416 * Stack frames start with 16-bytes of temp space
1417 */
1418 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001419 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 tos = (tsk->thread.fsr >> 11) & 7;
1421 for (i = 0; i < 8; i++)
1422 get_fpreg(i, &save->st_space[i], ptp, swp, tos);
1423 return 0;
1424}
1425
1426int
1427save_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1428{
1429 struct switch_stack *swp;
1430 struct pt_regs *ptp;
1431 int i, tos;
1432 unsigned long mxcsr=0;
1433 unsigned long num128[2];
1434
1435 if (!access_ok(VERIFY_WRITE, save, sizeof(*save)))
1436 return -EFAULT;
1437
1438 __put_user(tsk->thread.fcr & 0xffff, &save->cwd);
1439 __put_user(tsk->thread.fsr & 0xffff, &save->swd);
1440 __put_user((tsk->thread.fsr>>16) & 0xffff, &save->twd);
1441 __put_user(tsk->thread.fir, &save->fip);
1442 __put_user((tsk->thread.fir>>32) & 0xffff, &save->fcs);
1443 __put_user(tsk->thread.fdr, &save->foo);
1444 __put_user((tsk->thread.fdr>>32) & 0xffff, &save->fos);
1445
1446 /*
1447 * Stack frames start with 16-bytes of temp space
1448 */
1449 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001450 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 tos = (tsk->thread.fsr >> 11) & 7;
1452 for (i = 0; i < 8; i++)
1453 put_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1454
1455 mxcsr = ((tsk->thread.fcr>>32) & 0xff80) | ((tsk->thread.fsr>>32) & 0x3f);
1456 __put_user(mxcsr & 0xffff, &save->mxcsr);
1457 for (i = 0; i < 8; i++) {
1458 memcpy(&(num128[0]), &(swp->f16) + i*2, sizeof(unsigned long));
1459 memcpy(&(num128[1]), &(swp->f17) + i*2, sizeof(unsigned long));
1460 copy_to_user(&save->xmm_space[0] + 4*i, num128, sizeof(struct _xmmreg_ia32));
1461 }
1462 return 0;
1463}
1464
1465static int
1466restore_ia32_fpxstate (struct task_struct *tsk, struct ia32_user_fxsr_struct __user *save)
1467{
1468 struct switch_stack *swp;
1469 struct pt_regs *ptp;
1470 int i, tos;
1471 unsigned int fsrlo, fsrhi, num32;
1472 int mxcsr;
1473 unsigned long num64;
1474 unsigned long num128[2];
1475
1476 if (!access_ok(VERIFY_READ, save, sizeof(*save)))
1477 return(-EFAULT);
1478
1479 __get_user(num32, (unsigned int __user *)&save->cwd);
1480 tsk->thread.fcr = (tsk->thread.fcr & (~0x1f3f)) | (num32 & 0x1f3f);
1481 __get_user(fsrlo, (unsigned int __user *)&save->swd);
1482 __get_user(fsrhi, (unsigned int __user *)&save->twd);
1483 num32 = (fsrhi << 16) | fsrlo;
1484 tsk->thread.fsr = (tsk->thread.fsr & (~0xffffffff)) | num32;
1485 __get_user(num32, (unsigned int __user *)&save->fip);
1486 tsk->thread.fir = (tsk->thread.fir & (~0xffffffff)) | num32;
1487 __get_user(num32, (unsigned int __user *)&save->foo);
1488 tsk->thread.fdr = (tsk->thread.fdr & (~0xffffffff)) | num32;
1489
1490 /*
1491 * Stack frames start with 16-bytes of temp space
1492 */
1493 swp = (struct switch_stack *)(tsk->thread.ksp + 16);
Al Viro64505782006-01-12 01:06:06 -08001494 ptp = task_pt_regs(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 tos = (tsk->thread.fsr >> 11) & 7;
1496 for (i = 0; i < 8; i++)
1497 get_fpreg(i, (struct _fpreg_ia32 __user *)&save->st_space[4*i], ptp, swp, tos);
1498
1499 __get_user(mxcsr, (unsigned int __user *)&save->mxcsr);
1500 num64 = mxcsr & 0xff10;
1501 tsk->thread.fcr = (tsk->thread.fcr & (~0xff1000000000UL)) | (num64<<32);
1502 num64 = mxcsr & 0x3f;
1503 tsk->thread.fsr = (tsk->thread.fsr & (~0x3f00000000UL)) | (num64<<32);
1504
1505 for (i = 0; i < 8; i++) {
1506 copy_from_user(num128, &save->xmm_space[0] + 4*i, sizeof(struct _xmmreg_ia32));
1507 memcpy(&(swp->f16) + i*2, &(num128[0]), sizeof(unsigned long));
1508 memcpy(&(swp->f17) + i*2, &(num128[1]), sizeof(unsigned long));
1509 }
1510 return 0;
1511}
1512
1513asmlinkage long
1514sys32_ptrace (int request, pid_t pid, unsigned int addr, unsigned int data)
1515{
1516 struct task_struct *child;
1517 unsigned int value, tmp;
1518 long i, ret;
1519
1520 lock_kernel();
1521 if (request == PTRACE_TRACEME) {
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -08001522 ret = ptrace_traceme();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto out;
1524 }
1525
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -08001526 child = ptrace_get_task_struct(pid);
1527 if (IS_ERR(child)) {
1528 ret = PTR_ERR(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 goto out;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -08001530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 if (request == PTRACE_ATTACH) {
1533 ret = sys_ptrace(request, pid, addr, data);
1534 goto out_tsk;
1535 }
1536
1537 ret = ptrace_check_attach(child, request == PTRACE_KILL);
1538 if (ret < 0)
1539 goto out_tsk;
1540
1541 switch (request) {
1542 case PTRACE_PEEKTEXT:
1543 case PTRACE_PEEKDATA: /* read word at location addr */
1544 ret = ia32_peek(child, addr, &value);
1545 if (ret == 0)
1546 ret = put_user(value, (unsigned int __user *) compat_ptr(data));
1547 else
1548 ret = -EIO;
1549 goto out_tsk;
1550
1551 case PTRACE_POKETEXT:
1552 case PTRACE_POKEDATA: /* write the word at location addr */
1553 ret = ia32_poke(child, addr, data);
1554 goto out_tsk;
1555
1556 case PTRACE_PEEKUSR: /* read word at addr in USER area */
1557 ret = -EIO;
1558 if ((addr & 3) || addr > 17*sizeof(int))
1559 break;
1560
1561 tmp = getreg(child, addr);
1562 if (!put_user(tmp, (unsigned int __user *) compat_ptr(data)))
1563 ret = 0;
1564 break;
1565
1566 case PTRACE_POKEUSR: /* write word at addr in USER area */
1567 ret = -EIO;
1568 if ((addr & 3) || addr > 17*sizeof(int))
1569 break;
1570
1571 putreg(child, addr, data);
1572 ret = 0;
1573 break;
1574
1575 case IA32_PTRACE_GETREGS:
1576 if (!access_ok(VERIFY_WRITE, compat_ptr(data), 17*sizeof(int))) {
1577 ret = -EIO;
1578 break;
1579 }
1580 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1581 put_user(getreg(child, i), (unsigned int __user *) compat_ptr(data));
1582 data += sizeof(int);
1583 }
1584 ret = 0;
1585 break;
1586
1587 case IA32_PTRACE_SETREGS:
1588 if (!access_ok(VERIFY_READ, compat_ptr(data), 17*sizeof(int))) {
1589 ret = -EIO;
1590 break;
1591 }
1592 for (i = 0; i < (int) (17*sizeof(int)); i += sizeof(int) ) {
1593 get_user(tmp, (unsigned int __user *) compat_ptr(data));
1594 putreg(child, i, tmp);
1595 data += sizeof(int);
1596 }
1597 ret = 0;
1598 break;
1599
1600 case IA32_PTRACE_GETFPREGS:
1601 ret = save_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1602 compat_ptr(data));
1603 break;
1604
1605 case IA32_PTRACE_GETFPXREGS:
1606 ret = save_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1607 compat_ptr(data));
1608 break;
1609
1610 case IA32_PTRACE_SETFPREGS:
1611 ret = restore_ia32_fpstate(child, (struct ia32_user_i387_struct __user *)
1612 compat_ptr(data));
1613 break;
1614
1615 case IA32_PTRACE_SETFPXREGS:
1616 ret = restore_ia32_fpxstate(child, (struct ia32_user_fxsr_struct __user *)
1617 compat_ptr(data));
1618 break;
1619
1620 case PTRACE_GETEVENTMSG:
1621 ret = put_user(child->ptrace_message, (unsigned int __user *) compat_ptr(data));
1622 break;
1623
1624 case PTRACE_SYSCALL: /* continue, stop after next syscall */
1625 case PTRACE_CONT: /* restart after signal. */
1626 case PTRACE_KILL:
1627 case PTRACE_SINGLESTEP: /* execute chile for one instruction */
1628 case PTRACE_DETACH: /* detach a process */
1629 ret = sys_ptrace(request, pid, addr, data);
1630 break;
1631
1632 default:
1633 ret = ptrace_request(child, request, addr, data);
1634 break;
1635
1636 }
1637 out_tsk:
1638 put_task_struct(child);
1639 out:
1640 unlock_kernel();
1641 return ret;
1642}
1643
1644typedef struct {
1645 unsigned int ss_sp;
1646 unsigned int ss_flags;
1647 unsigned int ss_size;
1648} ia32_stack_t;
1649
1650asmlinkage long
1651sys32_sigaltstack (ia32_stack_t __user *uss32, ia32_stack_t __user *uoss32,
1652 long arg2, long arg3, long arg4, long arg5, long arg6,
1653 long arg7, struct pt_regs pt)
1654{
1655 stack_t uss, uoss;
1656 ia32_stack_t buf32;
1657 int ret;
1658 mm_segment_t old_fs = get_fs();
1659
1660 if (uss32) {
1661 if (copy_from_user(&buf32, uss32, sizeof(ia32_stack_t)))
1662 return -EFAULT;
1663 uss.ss_sp = (void __user *) (long) buf32.ss_sp;
1664 uss.ss_flags = buf32.ss_flags;
1665 /* MINSIGSTKSZ is different for ia32 vs ia64. We lie here to pass the
1666 check and set it to the user requested value later */
1667 if ((buf32.ss_flags != SS_DISABLE) && (buf32.ss_size < MINSIGSTKSZ_IA32)) {
1668 ret = -ENOMEM;
1669 goto out;
1670 }
1671 uss.ss_size = MINSIGSTKSZ;
1672 }
1673 set_fs(KERNEL_DS);
1674 ret = do_sigaltstack(uss32 ? (stack_t __user *) &uss : NULL,
1675 (stack_t __user *) &uoss, pt.r12);
1676 current->sas_ss_size = buf32.ss_size;
1677 set_fs(old_fs);
1678out:
1679 if (ret < 0)
1680 return(ret);
1681 if (uoss32) {
1682 buf32.ss_sp = (long __user) uoss.ss_sp;
1683 buf32.ss_flags = uoss.ss_flags;
1684 buf32.ss_size = uoss.ss_size;
1685 if (copy_to_user(uoss32, &buf32, sizeof(ia32_stack_t)))
1686 return -EFAULT;
1687 }
1688 return ret;
1689}
1690
1691asmlinkage int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692sys32_msync (unsigned int start, unsigned int len, int flags)
1693{
1694 unsigned int addr;
1695
1696 if (OFFSET4K(start))
1697 return -EINVAL;
1698 addr = PAGE_START(start);
1699 return sys_msync(addr, len + (start - addr), flags);
1700}
1701
1702struct sysctl32 {
1703 unsigned int name;
1704 int nlen;
1705 unsigned int oldval;
1706 unsigned int oldlenp;
1707 unsigned int newval;
1708 unsigned int newlen;
1709 unsigned int __unused[4];
1710};
1711
Eric W. Biedermanb89a8172006-09-27 01:51:04 -07001712#ifdef CONFIG_SYSCTL_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713asmlinkage long
1714sys32_sysctl (struct sysctl32 __user *args)
1715{
1716 struct sysctl32 a32;
1717 mm_segment_t old_fs = get_fs ();
1718 void __user *oldvalp, *newvalp;
1719 size_t oldlen;
1720 int __user *namep;
1721 long ret;
1722
1723 if (copy_from_user(&a32, args, sizeof(a32)))
1724 return -EFAULT;
1725
1726 /*
1727 * We need to pre-validate these because we have to disable address checking
1728 * before calling do_sysctl() because of OLDLEN but we can't run the risk of the
1729 * user specifying bad addresses here. Well, since we're dealing with 32 bit
1730 * addresses, we KNOW that access_ok() will always succeed, so this is an
1731 * expensive NOP, but so what...
1732 */
1733 namep = (int __user *) compat_ptr(a32.name);
1734 oldvalp = compat_ptr(a32.oldval);
1735 newvalp = compat_ptr(a32.newval);
1736
1737 if ((oldvalp && get_user(oldlen, (int __user *) compat_ptr(a32.oldlenp)))
1738 || !access_ok(VERIFY_WRITE, namep, 0)
1739 || !access_ok(VERIFY_WRITE, oldvalp, 0)
1740 || !access_ok(VERIFY_WRITE, newvalp, 0))
1741 return -EFAULT;
1742
1743 set_fs(KERNEL_DS);
1744 lock_kernel();
1745 ret = do_sysctl(namep, a32.nlen, oldvalp, (size_t __user *) &oldlen,
1746 newvalp, (size_t) a32.newlen);
1747 unlock_kernel();
1748 set_fs(old_fs);
1749
1750 if (oldvalp && put_user (oldlen, (int __user *) compat_ptr(a32.oldlenp)))
1751 return -EFAULT;
1752
1753 return ret;
1754}
1755#endif
1756
1757asmlinkage long
1758sys32_newuname (struct new_utsname __user *name)
1759{
1760 int ret = sys_newuname(name);
1761
1762 if (!ret)
1763 if (copy_to_user(name->machine, "i686\0\0\0", 8))
1764 ret = -EFAULT;
1765 return ret;
1766}
1767
1768asmlinkage long
1769sys32_getresuid16 (u16 __user *ruid, u16 __user *euid, u16 __user *suid)
1770{
1771 uid_t a, b, c;
1772 int ret;
1773 mm_segment_t old_fs = get_fs();
1774
1775 set_fs(KERNEL_DS);
1776 ret = sys_getresuid((uid_t __user *) &a, (uid_t __user *) &b, (uid_t __user *) &c);
1777 set_fs(old_fs);
1778
1779 if (put_user(a, ruid) || put_user(b, euid) || put_user(c, suid))
1780 return -EFAULT;
1781 return ret;
1782}
1783
1784asmlinkage long
1785sys32_getresgid16 (u16 __user *rgid, u16 __user *egid, u16 __user *sgid)
1786{
1787 gid_t a, b, c;
1788 int ret;
1789 mm_segment_t old_fs = get_fs();
1790
1791 set_fs(KERNEL_DS);
1792 ret = sys_getresgid((gid_t __user *) &a, (gid_t __user *) &b, (gid_t __user *) &c);
1793 set_fs(old_fs);
1794
1795 if (ret)
1796 return ret;
1797
1798 return put_user(a, rgid) | put_user(b, egid) | put_user(c, sgid);
1799}
1800
1801asmlinkage long
1802sys32_lseek (unsigned int fd, int offset, unsigned int whence)
1803{
1804 /* Sign-extension of "offset" is important here... */
1805 return sys_lseek(fd, offset, whence);
1806}
1807
1808static int
1809groups16_to_user(short __user *grouplist, struct group_info *group_info)
1810{
1811 int i;
1812 short group;
1813
1814 for (i = 0; i < group_info->ngroups; i++) {
1815 group = (short)GROUP_AT(group_info, i);
1816 if (put_user(group, grouplist+i))
1817 return -EFAULT;
1818 }
1819
1820 return 0;
1821}
1822
1823static int
1824groups16_from_user(struct group_info *group_info, short __user *grouplist)
1825{
1826 int i;
1827 short group;
1828
1829 for (i = 0; i < group_info->ngroups; i++) {
1830 if (get_user(group, grouplist+i))
1831 return -EFAULT;
1832 GROUP_AT(group_info, i) = (gid_t)group;
1833 }
1834
1835 return 0;
1836}
1837
1838asmlinkage long
1839sys32_getgroups16 (int gidsetsize, short __user *grouplist)
1840{
1841 int i;
1842
1843 if (gidsetsize < 0)
1844 return -EINVAL;
1845
1846 get_group_info(current->group_info);
1847 i = current->group_info->ngroups;
1848 if (gidsetsize) {
1849 if (i > gidsetsize) {
1850 i = -EINVAL;
1851 goto out;
1852 }
1853 if (groups16_to_user(grouplist, current->group_info)) {
1854 i = -EFAULT;
1855 goto out;
1856 }
1857 }
1858out:
1859 put_group_info(current->group_info);
1860 return i;
1861}
1862
1863asmlinkage long
1864sys32_setgroups16 (int gidsetsize, short __user *grouplist)
1865{
1866 struct group_info *group_info;
1867 int retval;
1868
1869 if (!capable(CAP_SETGID))
1870 return -EPERM;
1871 if ((unsigned)gidsetsize > NGROUPS_MAX)
1872 return -EINVAL;
1873
1874 group_info = groups_alloc(gidsetsize);
1875 if (!group_info)
1876 return -ENOMEM;
1877 retval = groups16_from_user(group_info, grouplist);
1878 if (retval) {
1879 put_group_info(group_info);
1880 return retval;
1881 }
1882
1883 retval = set_current_groups(group_info);
1884 put_group_info(group_info);
1885
1886 return retval;
1887}
1888
1889asmlinkage long
1890sys32_truncate64 (unsigned int path, unsigned int len_lo, unsigned int len_hi)
1891{
1892 return sys_truncate(compat_ptr(path), ((unsigned long) len_hi << 32) | len_lo);
1893}
1894
1895asmlinkage long
1896sys32_ftruncate64 (int fd, unsigned int len_lo, unsigned int len_hi)
1897{
1898 return sys_ftruncate(fd, ((unsigned long) len_hi << 32) | len_lo);
1899}
1900
1901static int
1902putstat64 (struct stat64 __user *ubuf, struct kstat *kbuf)
1903{
1904 int err;
1905 u64 hdev;
1906
1907 if (clear_user(ubuf, sizeof(*ubuf)))
1908 return -EFAULT;
1909
1910 hdev = huge_encode_dev(kbuf->dev);
1911 err = __put_user(hdev, (u32 __user*)&ubuf->st_dev);
1912 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_dev) + 1);
1913 err |= __put_user(kbuf->ino, &ubuf->__st_ino);
1914 err |= __put_user(kbuf->ino, &ubuf->st_ino_lo);
1915 err |= __put_user(kbuf->ino >> 32, &ubuf->st_ino_hi);
1916 err |= __put_user(kbuf->mode, &ubuf->st_mode);
1917 err |= __put_user(kbuf->nlink, &ubuf->st_nlink);
1918 err |= __put_user(kbuf->uid, &ubuf->st_uid);
1919 err |= __put_user(kbuf->gid, &ubuf->st_gid);
1920 hdev = huge_encode_dev(kbuf->rdev);
1921 err = __put_user(hdev, (u32 __user*)&ubuf->st_rdev);
1922 err |= __put_user(hdev >> 32, ((u32 __user*)&ubuf->st_rdev) + 1);
1923 err |= __put_user(kbuf->size, &ubuf->st_size_lo);
1924 err |= __put_user((kbuf->size >> 32), &ubuf->st_size_hi);
1925 err |= __put_user(kbuf->atime.tv_sec, &ubuf->st_atime);
1926 err |= __put_user(kbuf->atime.tv_nsec, &ubuf->st_atime_nsec);
1927 err |= __put_user(kbuf->mtime.tv_sec, &ubuf->st_mtime);
1928 err |= __put_user(kbuf->mtime.tv_nsec, &ubuf->st_mtime_nsec);
1929 err |= __put_user(kbuf->ctime.tv_sec, &ubuf->st_ctime);
1930 err |= __put_user(kbuf->ctime.tv_nsec, &ubuf->st_ctime_nsec);
1931 err |= __put_user(kbuf->blksize, &ubuf->st_blksize);
1932 err |= __put_user(kbuf->blocks, &ubuf->st_blocks);
1933 return err;
1934}
1935
1936asmlinkage long
1937sys32_stat64 (char __user *filename, struct stat64 __user *statbuf)
1938{
1939 struct kstat s;
1940 long ret = vfs_stat(filename, &s);
1941 if (!ret)
1942 ret = putstat64(statbuf, &s);
1943 return ret;
1944}
1945
1946asmlinkage long
1947sys32_lstat64 (char __user *filename, struct stat64 __user *statbuf)
1948{
1949 struct kstat s;
1950 long ret = vfs_lstat(filename, &s);
1951 if (!ret)
1952 ret = putstat64(statbuf, &s);
1953 return ret;
1954}
1955
1956asmlinkage long
1957sys32_fstat64 (unsigned int fd, struct stat64 __user *statbuf)
1958{
1959 struct kstat s;
1960 long ret = vfs_fstat(fd, &s);
1961 if (!ret)
1962 ret = putstat64(statbuf, &s);
1963 return ret;
1964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966asmlinkage long
1967sys32_sched_rr_get_interval (pid_t pid, struct compat_timespec __user *interval)
1968{
1969 mm_segment_t old_fs = get_fs();
1970 struct timespec t;
1971 long ret;
1972
1973 set_fs(KERNEL_DS);
1974 ret = sys_sched_rr_get_interval(pid, (struct timespec __user *) &t);
1975 set_fs(old_fs);
1976 if (put_compat_timespec(&t, interval))
1977 return -EFAULT;
1978 return ret;
1979}
1980
1981asmlinkage long
1982sys32_pread (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
1983{
1984 return sys_pread64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
1985}
1986
1987asmlinkage long
1988sys32_pwrite (unsigned int fd, void __user *buf, unsigned int count, u32 pos_lo, u32 pos_hi)
1989{
1990 return sys_pwrite64(fd, buf, count, ((unsigned long) pos_hi << 32) | pos_lo);
1991}
1992
1993asmlinkage long
1994sys32_sendfile (int out_fd, int in_fd, int __user *offset, unsigned int count)
1995{
1996 mm_segment_t old_fs = get_fs();
1997 long ret;
1998 off_t of;
1999
2000 if (offset && get_user(of, offset))
2001 return -EFAULT;
2002
2003 set_fs(KERNEL_DS);
2004 ret = sys_sendfile(out_fd, in_fd, offset ? (off_t __user *) &of : NULL, count);
2005 set_fs(old_fs);
2006
Tsuneo.Yoshioka@f-secure.com83b942b2005-09-12 18:49:24 +02002007 if (offset && put_user(of, offset))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -EFAULT;
2009
2010 return ret;
2011}
2012
2013asmlinkage long
2014sys32_personality (unsigned int personality)
2015{
2016 long ret;
2017
2018 if (current->personality == PER_LINUX32 && personality == PER_LINUX)
2019 personality = PER_LINUX32;
2020 ret = sys_personality(personality);
2021 if (ret == PER_LINUX32)
2022 ret = PER_LINUX;
2023 return ret;
2024}
2025
2026asmlinkage unsigned long
2027sys32_brk (unsigned int brk)
2028{
2029 unsigned long ret, obrk;
2030 struct mm_struct *mm = current->mm;
2031
2032 obrk = mm->brk;
2033 ret = sys_brk(brk);
2034 if (ret < obrk)
2035 clear_user(compat_ptr(ret), PAGE_ALIGN(ret) - ret);
2036 return ret;
2037}
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/* Structure for ia32 emulation on ia64 */
2040struct epoll_event32
2041{
2042 u32 events;
2043 u32 data[2];
2044};
2045
2046asmlinkage long
2047sys32_epoll_ctl(int epfd, int op, int fd, struct epoll_event32 __user *event)
2048{
2049 mm_segment_t old_fs = get_fs();
2050 struct epoll_event event64;
2051 int error;
2052 u32 data_halfword;
2053
2054 if (!access_ok(VERIFY_READ, event, sizeof(struct epoll_event32)))
2055 return -EFAULT;
2056
2057 __get_user(event64.events, &event->events);
2058 __get_user(data_halfword, &event->data[0]);
2059 event64.data = data_halfword;
2060 __get_user(data_halfword, &event->data[1]);
2061 event64.data |= (u64)data_halfword << 32;
2062
2063 set_fs(KERNEL_DS);
2064 error = sys_epoll_ctl(epfd, op, fd, (struct epoll_event __user *) &event64);
2065 set_fs(old_fs);
2066
2067 return error;
2068}
2069
2070asmlinkage long
2071sys32_epoll_wait(int epfd, struct epoll_event32 __user * events, int maxevents,
2072 int timeout)
2073{
2074 struct epoll_event *events64 = NULL;
2075 mm_segment_t old_fs = get_fs();
Peter Chubbd8caebd2005-05-31 22:37:00 -07002076 int numevents, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 int evt_idx;
2078 int do_free_pages = 0;
2079
2080 if (maxevents <= 0) {
2081 return -EINVAL;
2082 }
2083
2084 /* Verify that the area passed by the user is writeable */
2085 if (!access_ok(VERIFY_WRITE, events, maxevents * sizeof(struct epoll_event32)))
2086 return -EFAULT;
2087
2088 /*
2089 * Allocate space for the intermediate copy. If the space needed
2090 * is large enough to cause kmalloc to fail, then try again with
2091 * __get_free_pages.
2092 */
2093 size = maxevents * sizeof(struct epoll_event);
2094 events64 = kmalloc(size, GFP_KERNEL);
2095 if (events64 == NULL) {
2096 events64 = (struct epoll_event *)
2097 __get_free_pages(GFP_KERNEL, get_order(size));
2098 if (events64 == NULL)
2099 return -ENOMEM;
2100 do_free_pages = 1;
2101 }
2102
2103 /* Do the system call */
2104 set_fs(KERNEL_DS); /* copy_to/from_user should work on kernel mem*/
2105 numevents = sys_epoll_wait(epfd, (struct epoll_event __user *) events64,
2106 maxevents, timeout);
2107 set_fs(old_fs);
2108
2109 /* Don't modify userspace memory if we're returning an error */
2110 if (numevents > 0) {
2111 /* Translate the 64-bit structures back into the 32-bit
2112 structures */
2113 for (evt_idx = 0; evt_idx < numevents; evt_idx++) {
2114 __put_user(events64[evt_idx].events,
2115 &events[evt_idx].events);
2116 __put_user((u32)events64[evt_idx].data,
2117 &events[evt_idx].data[0]);
2118 __put_user((u32)(events64[evt_idx].data >> 32),
2119 &events[evt_idx].data[1]);
2120 }
2121 }
2122
2123 if (do_free_pages)
2124 free_pages((unsigned long) events64, get_order(size));
2125 else
2126 kfree(events64);
2127 return numevents;
2128}
2129
2130/*
2131 * Get a yet unused TLS descriptor index.
2132 */
2133static int
2134get_free_idx (void)
2135{
2136 struct thread_struct *t = &current->thread;
2137 int idx;
2138
2139 for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
2140 if (desc_empty(t->tls_array + idx))
2141 return idx + GDT_ENTRY_TLS_MIN;
2142 return -ESRCH;
2143}
2144
Shaohua Li75529212008-02-28 16:09:33 +08002145static void set_tls_desc(struct task_struct *p, int idx,
2146 const struct ia32_user_desc *info, int n)
2147{
2148 struct thread_struct *t = &p->thread;
2149 struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
2150 int cpu;
2151
2152 /*
2153 * We must not get preempted while modifying the TLS.
2154 */
2155 cpu = get_cpu();
2156
2157 while (n-- > 0) {
2158 if (LDT_empty(info)) {
2159 desc->a = 0;
2160 desc->b = 0;
2161 } else {
2162 desc->a = LDT_entry_a(info);
2163 desc->b = LDT_entry_b(info);
2164 }
2165
2166 ++info;
2167 ++desc;
2168 }
2169
2170 if (t == &current->thread)
2171 load_TLS(t, cpu);
2172
2173 put_cpu();
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176/*
2177 * Set a given TLS descriptor:
2178 */
2179asmlinkage int
2180sys32_set_thread_area (struct ia32_user_desc __user *u_info)
2181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct ia32_user_desc info;
Shaohua Li75529212008-02-28 16:09:33 +08002183 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 if (copy_from_user(&info, u_info, sizeof(info)))
2186 return -EFAULT;
2187 idx = info.entry_number;
2188
2189 /*
2190 * index -1 means the kernel should try to find and allocate an empty descriptor:
2191 */
2192 if (idx == -1) {
2193 idx = get_free_idx();
2194 if (idx < 0)
2195 return idx;
2196 if (put_user(idx, &u_info->entry_number))
2197 return -EFAULT;
2198 }
2199
2200 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2201 return -EINVAL;
2202
Shaohua Li75529212008-02-28 16:09:33 +08002203 set_tls_desc(current, idx, &info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return 0;
2205}
2206
2207/*
2208 * Get the current Thread-Local Storage area:
2209 */
2210
2211#define GET_BASE(desc) ( \
2212 (((desc)->a >> 16) & 0x0000ffff) | \
2213 (((desc)->b << 16) & 0x00ff0000) | \
2214 ( (desc)->b & 0xff000000) )
2215
2216#define GET_LIMIT(desc) ( \
2217 ((desc)->a & 0x0ffff) | \
2218 ((desc)->b & 0xf0000) )
2219
2220#define GET_32BIT(desc) (((desc)->b >> 22) & 1)
2221#define GET_CONTENTS(desc) (((desc)->b >> 10) & 3)
2222#define GET_WRITABLE(desc) (((desc)->b >> 9) & 1)
2223#define GET_LIMIT_PAGES(desc) (((desc)->b >> 23) & 1)
2224#define GET_PRESENT(desc) (((desc)->b >> 15) & 1)
2225#define GET_USEABLE(desc) (((desc)->b >> 20) & 1)
2226
Shaohua Li75529212008-02-28 16:09:33 +08002227static void fill_user_desc(struct ia32_user_desc *info, int idx,
2228 const struct desc_struct *desc)
2229{
2230 info->entry_number = idx;
2231 info->base_addr = GET_BASE(desc);
2232 info->limit = GET_LIMIT(desc);
2233 info->seg_32bit = GET_32BIT(desc);
2234 info->contents = GET_CONTENTS(desc);
2235 info->read_exec_only = !GET_WRITABLE(desc);
2236 info->limit_in_pages = GET_LIMIT_PAGES(desc);
2237 info->seg_not_present = !GET_PRESENT(desc);
2238 info->useable = GET_USEABLE(desc);
2239}
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241asmlinkage int
2242sys32_get_thread_area (struct ia32_user_desc __user *u_info)
2243{
2244 struct ia32_user_desc info;
2245 struct desc_struct *desc;
2246 int idx;
2247
2248 if (get_user(idx, &u_info->entry_number))
2249 return -EFAULT;
2250 if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
2251 return -EINVAL;
2252
2253 desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
Shaohua Li75529212008-02-28 16:09:33 +08002254 fill_user_desc(&info, idx, desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 if (copy_to_user(u_info, &info, sizeof(info)))
2257 return -EFAULT;
2258 return 0;
2259}
2260
Shaohua Li75529212008-02-28 16:09:33 +08002261struct regset_get {
2262 void *kbuf;
2263 void __user *ubuf;
2264};
2265
2266struct regset_set {
2267 const void *kbuf;
2268 const void __user *ubuf;
2269};
2270
2271struct regset_getset {
2272 struct task_struct *target;
2273 const struct user_regset *regset;
2274 union {
2275 struct regset_get get;
2276 struct regset_set set;
2277 } u;
2278 unsigned int pos;
2279 unsigned int count;
2280 int ret;
2281};
2282
2283static void getfpreg(struct task_struct *task, int regno, int *val)
2284{
2285 switch (regno / sizeof(int)) {
2286 case 0:
2287 *val = task->thread.fcr & 0xffff;
2288 break;
2289 case 1:
2290 *val = task->thread.fsr & 0xffff;
2291 break;
2292 case 2:
2293 *val = (task->thread.fsr>>16) & 0xffff;
2294 break;
2295 case 3:
2296 *val = task->thread.fir;
2297 break;
2298 case 4:
2299 *val = (task->thread.fir>>32) & 0xffff;
2300 break;
2301 case 5:
2302 *val = task->thread.fdr;
2303 break;
2304 case 6:
2305 *val = (task->thread.fdr >> 32) & 0xffff;
2306 break;
2307 }
2308}
2309
2310static void setfpreg(struct task_struct *task, int regno, int val)
2311{
2312 switch (regno / sizeof(int)) {
2313 case 0:
2314 task->thread.fcr = (task->thread.fcr & (~0x1f3f))
2315 | (val & 0x1f3f);
2316 break;
2317 case 1:
2318 task->thread.fsr = (task->thread.fsr & (~0xffff)) | val;
2319 break;
2320 case 2:
2321 task->thread.fsr = (task->thread.fsr & (~0xffff0000))
2322 | (val << 16);
2323 break;
2324 case 3:
2325 task->thread.fir = (task->thread.fir & (~0xffffffff)) | val;
2326 break;
2327 case 5:
2328 task->thread.fdr = (task->thread.fdr & (~0xffffffff)) | val;
2329 break;
2330 }
2331}
2332
2333static void access_fpreg_ia32(int regno, void *reg,
2334 struct pt_regs *pt, struct switch_stack *sw,
2335 int tos, int write)
2336{
2337 void *f;
2338
2339 if ((regno += tos) >= 8)
2340 regno -= 8;
2341 if (regno < 4)
2342 f = &pt->f8 + regno;
2343 else if (regno <= 7)
2344 f = &sw->f12 + (regno - 4);
2345 else {
2346 printk(KERN_ERR "regno must be less than 7 \n");
2347 return;
2348 }
2349
2350 if (write)
2351 memcpy(f, reg, sizeof(struct _fpreg_ia32));
2352 else
2353 memcpy(reg, f, sizeof(struct _fpreg_ia32));
2354}
2355
2356static void do_fpregs_get(struct unw_frame_info *info, void *arg)
2357{
2358 struct regset_getset *dst = arg;
2359 struct task_struct *task = dst->target;
2360 struct pt_regs *pt;
2361 int start, end, tos;
2362 char buf[80];
2363
2364 if (dst->count == 0 || unw_unwind_to_user(info) < 0)
2365 return;
2366 if (dst->pos < 7 * sizeof(int)) {
2367 end = min((dst->pos + dst->count),
2368 (unsigned int)(7 * sizeof(int)));
2369 for (start = dst->pos; start < end; start += sizeof(int))
2370 getfpreg(task, start, (int *)(buf + start));
2371 dst->ret = user_regset_copyout(&dst->pos, &dst->count,
2372 &dst->u.get.kbuf, &dst->u.get.ubuf, buf,
2373 0, 7 * sizeof(int));
2374 if (dst->ret || dst->count == 0)
2375 return;
2376 }
2377 if (dst->pos < sizeof(struct ia32_user_i387_struct)) {
2378 pt = task_pt_regs(task);
2379 tos = (task->thread.fsr >> 11) & 7;
2380 end = min(dst->pos + dst->count,
2381 (unsigned int)(sizeof(struct ia32_user_i387_struct)));
2382 start = (dst->pos - 7 * sizeof(int)) /
2383 sizeof(struct _fpreg_ia32);
2384 end = (end - 7 * sizeof(int)) / sizeof(struct _fpreg_ia32);
2385 for (; start < end; start++)
2386 access_fpreg_ia32(start,
2387 (struct _fpreg_ia32 *)buf + start,
2388 pt, info->sw, tos, 0);
2389 dst->ret = user_regset_copyout(&dst->pos, &dst->count,
2390 &dst->u.get.kbuf, &dst->u.get.ubuf,
2391 buf, 7 * sizeof(int),
2392 sizeof(struct ia32_user_i387_struct));
2393 if (dst->ret || dst->count == 0)
2394 return;
2395 }
2396}
2397
2398static void do_fpregs_set(struct unw_frame_info *info, void *arg)
2399{
2400 struct regset_getset *dst = arg;
2401 struct task_struct *task = dst->target;
2402 struct pt_regs *pt;
2403 char buf[80];
2404 int end, start, tos;
2405
2406 if (dst->count == 0 || unw_unwind_to_user(info) < 0)
2407 return;
2408
2409 if (dst->pos < 7 * sizeof(int)) {
2410 start = dst->pos;
2411 dst->ret = user_regset_copyin(&dst->pos, &dst->count,
2412 &dst->u.set.kbuf, &dst->u.set.ubuf, buf,
2413 0, 7 * sizeof(int));
2414 if (dst->ret)
2415 return;
2416 for (; start < dst->pos; start += sizeof(int))
2417 setfpreg(task, start, *((int *)(buf + start)));
2418 if (dst->count == 0)
2419 return;
2420 }
2421 if (dst->pos < sizeof(struct ia32_user_i387_struct)) {
2422 start = (dst->pos - 7 * sizeof(int)) /
2423 sizeof(struct _fpreg_ia32);
2424 dst->ret = user_regset_copyin(&dst->pos, &dst->count,
2425 &dst->u.set.kbuf, &dst->u.set.ubuf,
2426 buf, 7 * sizeof(int),
2427 sizeof(struct ia32_user_i387_struct));
2428 if (dst->ret)
2429 return;
2430 pt = task_pt_regs(task);
2431 tos = (task->thread.fsr >> 11) & 7;
2432 end = (dst->pos - 7 * sizeof(int)) / sizeof(struct _fpreg_ia32);
2433 for (; start < end; start++)
2434 access_fpreg_ia32(start,
2435 (struct _fpreg_ia32 *)buf + start,
2436 pt, info->sw, tos, 1);
2437 if (dst->count == 0)
2438 return;
2439 }
2440}
2441
2442#define OFFSET(member) ((int)(offsetof(struct ia32_user_fxsr_struct, member)))
2443static void getfpxreg(struct task_struct *task, int start, int end, char *buf)
2444{
2445 int min_val;
2446
2447 min_val = min(end, OFFSET(fop));
2448 while (start < min_val) {
2449 if (start == OFFSET(cwd))
2450 *((short *)buf) = task->thread.fcr & 0xffff;
2451 else if (start == OFFSET(swd))
2452 *((short *)buf) = task->thread.fsr & 0xffff;
2453 else if (start == OFFSET(twd))
2454 *((short *)buf) = (task->thread.fsr>>16) & 0xffff;
2455 buf += 2;
2456 start += 2;
2457 }
2458 /* skip fop element */
2459 if (start == OFFSET(fop)) {
2460 start += 2;
2461 buf += 2;
2462 }
2463 while (start < end) {
2464 if (start == OFFSET(fip))
2465 *((int *)buf) = task->thread.fir;
2466 else if (start == OFFSET(fcs))
2467 *((int *)buf) = (task->thread.fir>>32) & 0xffff;
2468 else if (start == OFFSET(foo))
2469 *((int *)buf) = task->thread.fdr;
2470 else if (start == OFFSET(fos))
2471 *((int *)buf) = (task->thread.fdr>>32) & 0xffff;
2472 else if (start == OFFSET(mxcsr))
2473 *((int *)buf) = ((task->thread.fcr>>32) & 0xff80)
2474 | ((task->thread.fsr>>32) & 0x3f);
2475 buf += 4;
2476 start += 4;
2477 }
2478}
2479
2480static void setfpxreg(struct task_struct *task, int start, int end, char *buf)
2481{
2482 int min_val, num32;
2483 short num;
2484 unsigned long num64;
2485
2486 min_val = min(end, OFFSET(fop));
2487 while (start < min_val) {
2488 num = *((short *)buf);
2489 if (start == OFFSET(cwd)) {
2490 task->thread.fcr = (task->thread.fcr & (~0x1f3f))
2491 | (num & 0x1f3f);
2492 } else if (start == OFFSET(swd)) {
2493 task->thread.fsr = (task->thread.fsr & (~0xffff)) | num;
2494 } else if (start == OFFSET(twd)) {
2495 task->thread.fsr = (task->thread.fsr & (~0xffff0000))
2496 | (((int)num) << 16);
2497 }
2498 buf += 2;
2499 start += 2;
2500 }
2501 /* skip fop element */
2502 if (start == OFFSET(fop)) {
2503 start += 2;
2504 buf += 2;
2505 }
2506 while (start < end) {
2507 num32 = *((int *)buf);
2508 if (start == OFFSET(fip))
2509 task->thread.fir = (task->thread.fir & (~0xffffffff))
2510 | num32;
2511 else if (start == OFFSET(foo))
2512 task->thread.fdr = (task->thread.fdr & (~0xffffffff))
2513 | num32;
2514 else if (start == OFFSET(mxcsr)) {
2515 num64 = num32 & 0xff10;
2516 task->thread.fcr = (task->thread.fcr &
2517 (~0xff1000000000UL)) | (num64<<32);
2518 num64 = num32 & 0x3f;
2519 task->thread.fsr = (task->thread.fsr &
2520 (~0x3f00000000UL)) | (num64<<32);
2521 }
2522 buf += 4;
2523 start += 4;
2524 }
2525}
2526
2527static void do_fpxregs_get(struct unw_frame_info *info, void *arg)
2528{
2529 struct regset_getset *dst = arg;
2530 struct task_struct *task = dst->target;
2531 struct pt_regs *pt;
2532 char buf[128];
2533 int start, end, tos;
2534
2535 if (dst->count == 0 || unw_unwind_to_user(info) < 0)
2536 return;
2537 if (dst->pos < OFFSET(st_space[0])) {
2538 end = min(dst->pos + dst->count, (unsigned int)32);
2539 getfpxreg(task, dst->pos, end, buf);
2540 dst->ret = user_regset_copyout(&dst->pos, &dst->count,
2541 &dst->u.get.kbuf, &dst->u.get.ubuf, buf,
2542 0, OFFSET(st_space[0]));
2543 if (dst->ret || dst->count == 0)
2544 return;
2545 }
2546 if (dst->pos < OFFSET(xmm_space[0])) {
2547 pt = task_pt_regs(task);
2548 tos = (task->thread.fsr >> 11) & 7;
2549 end = min(dst->pos + dst->count,
2550 (unsigned int)OFFSET(xmm_space[0]));
2551 start = (dst->pos - OFFSET(st_space[0])) / 16;
2552 end = (end - OFFSET(st_space[0])) / 16;
2553 for (; start < end; start++)
2554 access_fpreg_ia32(start, buf + 16 * start, pt,
2555 info->sw, tos, 0);
2556 dst->ret = user_regset_copyout(&dst->pos, &dst->count,
2557 &dst->u.get.kbuf, &dst->u.get.ubuf,
2558 buf, OFFSET(st_space[0]), OFFSET(xmm_space[0]));
2559 if (dst->ret || dst->count == 0)
2560 return;
2561 }
2562 if (dst->pos < OFFSET(padding[0]))
2563 dst->ret = user_regset_copyout(&dst->pos, &dst->count,
2564 &dst->u.get.kbuf, &dst->u.get.ubuf,
2565 &info->sw->f16, OFFSET(xmm_space[0]),
2566 OFFSET(padding[0]));
2567}
2568
2569static void do_fpxregs_set(struct unw_frame_info *info, void *arg)
2570{
2571 struct regset_getset *dst = arg;
2572 struct task_struct *task = dst->target;
2573 char buf[128];
2574 int start, end;
2575
2576 if (dst->count == 0 || unw_unwind_to_user(info) < 0)
2577 return;
2578
2579 if (dst->pos < OFFSET(st_space[0])) {
2580 start = dst->pos;
2581 dst->ret = user_regset_copyin(&dst->pos, &dst->count,
2582 &dst->u.set.kbuf, &dst->u.set.ubuf,
2583 buf, 0, OFFSET(st_space[0]));
2584 if (dst->ret)
2585 return;
2586 setfpxreg(task, start, dst->pos, buf);
2587 if (dst->count == 0)
2588 return;
2589 }
2590 if (dst->pos < OFFSET(xmm_space[0])) {
2591 struct pt_regs *pt;
2592 int tos;
2593 pt = task_pt_regs(task);
2594 tos = (task->thread.fsr >> 11) & 7;
2595 start = (dst->pos - OFFSET(st_space[0])) / 16;
2596 dst->ret = user_regset_copyin(&dst->pos, &dst->count,
2597 &dst->u.set.kbuf, &dst->u.set.ubuf,
2598 buf, OFFSET(st_space[0]), OFFSET(xmm_space[0]));
2599 if (dst->ret)
2600 return;
2601 end = (dst->pos - OFFSET(st_space[0])) / 16;
2602 for (; start < end; start++)
2603 access_fpreg_ia32(start, buf + 16 * start, pt, info->sw,
2604 tos, 1);
2605 if (dst->count == 0)
2606 return;
2607 }
2608 if (dst->pos < OFFSET(padding[0]))
2609 dst->ret = user_regset_copyin(&dst->pos, &dst->count,
2610 &dst->u.set.kbuf, &dst->u.set.ubuf,
2611 &info->sw->f16, OFFSET(xmm_space[0]),
2612 OFFSET(padding[0]));
2613}
2614#undef OFFSET
2615
2616static int do_regset_call(void (*call)(struct unw_frame_info *, void *),
2617 struct task_struct *target,
2618 const struct user_regset *regset,
2619 unsigned int pos, unsigned int count,
2620 const void *kbuf, const void __user *ubuf)
2621{
2622 struct regset_getset info = { .target = target, .regset = regset,
2623 .pos = pos, .count = count,
2624 .u.set = { .kbuf = kbuf, .ubuf = ubuf },
2625 .ret = 0 };
2626
2627 if (target == current)
2628 unw_init_running(call, &info);
2629 else {
2630 struct unw_frame_info ufi;
2631 memset(&ufi, 0, sizeof(ufi));
2632 unw_init_from_blocked_task(&ufi, target);
2633 (*call)(&ufi, &info);
2634 }
2635
2636 return info.ret;
2637}
2638
2639static int ia32_fpregs_get(struct task_struct *target,
2640 const struct user_regset *regset,
2641 unsigned int pos, unsigned int count,
2642 void *kbuf, void __user *ubuf)
2643{
2644 return do_regset_call(do_fpregs_get, target, regset, pos, count,
2645 kbuf, ubuf);
2646}
2647
2648static int ia32_fpregs_set(struct task_struct *target,
2649 const struct user_regset *regset,
2650 unsigned int pos, unsigned int count,
2651 const void *kbuf, const void __user *ubuf)
2652{
2653 return do_regset_call(do_fpregs_set, target, regset, pos, count,
2654 kbuf, ubuf);
2655}
2656
2657static int ia32_fpxregs_get(struct task_struct *target,
2658 const struct user_regset *regset,
2659 unsigned int pos, unsigned int count,
2660 void *kbuf, void __user *ubuf)
2661{
2662 return do_regset_call(do_fpxregs_get, target, regset, pos, count,
2663 kbuf, ubuf);
2664}
2665
2666static int ia32_fpxregs_set(struct task_struct *target,
2667 const struct user_regset *regset,
2668 unsigned int pos, unsigned int count,
2669 const void *kbuf, const void __user *ubuf)
2670{
2671 return do_regset_call(do_fpxregs_set, target, regset, pos, count,
2672 kbuf, ubuf);
2673}
2674
2675static int ia32_genregs_get(struct task_struct *target,
2676 const struct user_regset *regset,
2677 unsigned int pos, unsigned int count,
2678 void *kbuf, void __user *ubuf)
2679{
2680 if (kbuf) {
2681 u32 *kp = kbuf;
2682 while (count > 0) {
2683 *kp++ = getreg(target, pos);
2684 pos += 4;
2685 count -= 4;
2686 }
2687 } else {
2688 u32 __user *up = ubuf;
2689 while (count > 0) {
2690 if (__put_user(getreg(target, pos), up++))
2691 return -EFAULT;
2692 pos += 4;
2693 count -= 4;
2694 }
2695 }
2696 return 0;
2697}
2698
2699static int ia32_genregs_set(struct task_struct *target,
2700 const struct user_regset *regset,
2701 unsigned int pos, unsigned int count,
2702 const void *kbuf, const void __user *ubuf)
2703{
2704 int ret = 0;
2705
2706 if (kbuf) {
2707 const u32 *kp = kbuf;
2708 while (!ret && count > 0) {
2709 putreg(target, pos, *kp++);
2710 pos += 4;
2711 count -= 4;
2712 }
2713 } else {
2714 const u32 __user *up = ubuf;
2715 u32 val;
2716 while (!ret && count > 0) {
2717 ret = __get_user(val, up++);
2718 if (!ret)
2719 putreg(target, pos, val);
2720 pos += 4;
2721 count -= 4;
2722 }
2723 }
2724 return ret;
2725}
2726
2727static int ia32_tls_active(struct task_struct *target,
2728 const struct user_regset *regset)
2729{
2730 struct thread_struct *t = &target->thread;
2731 int n = GDT_ENTRY_TLS_ENTRIES;
2732 while (n > 0 && desc_empty(&t->tls_array[n -1]))
2733 --n;
2734 return n;
2735}
2736
2737static int ia32_tls_get(struct task_struct *target,
2738 const struct user_regset *regset, unsigned int pos,
2739 unsigned int count, void *kbuf, void __user *ubuf)
2740{
2741 const struct desc_struct *tls;
2742
2743 if (pos > GDT_ENTRY_TLS_ENTRIES * sizeof(struct ia32_user_desc) ||
2744 (pos % sizeof(struct ia32_user_desc)) != 0 ||
2745 (count % sizeof(struct ia32_user_desc)) != 0)
2746 return -EINVAL;
2747
2748 pos /= sizeof(struct ia32_user_desc);
2749 count /= sizeof(struct ia32_user_desc);
2750
2751 tls = &target->thread.tls_array[pos];
2752
2753 if (kbuf) {
2754 struct ia32_user_desc *info = kbuf;
2755 while (count-- > 0)
2756 fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
2757 tls++);
2758 } else {
2759 struct ia32_user_desc __user *u_info = ubuf;
2760 while (count-- > 0) {
2761 struct ia32_user_desc info;
2762 fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
2763 if (__copy_to_user(u_info++, &info, sizeof(info)))
2764 return -EFAULT;
2765 }
2766 }
2767
2768 return 0;
2769}
2770
2771static int ia32_tls_set(struct task_struct *target,
2772 const struct user_regset *regset, unsigned int pos,
2773 unsigned int count, const void *kbuf, const void __user *ubuf)
2774{
2775 struct ia32_user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
2776 const struct ia32_user_desc *info;
2777
2778 if (pos > GDT_ENTRY_TLS_ENTRIES * sizeof(struct ia32_user_desc) ||
2779 (pos % sizeof(struct ia32_user_desc)) != 0 ||
2780 (count % sizeof(struct ia32_user_desc)) != 0)
2781 return -EINVAL;
2782
2783 if (kbuf)
2784 info = kbuf;
2785 else if (__copy_from_user(infobuf, ubuf, count))
2786 return -EFAULT;
2787 else
2788 info = infobuf;
2789
2790 set_tls_desc(target,
2791 GDT_ENTRY_TLS_MIN + (pos / sizeof(struct ia32_user_desc)),
2792 info, count / sizeof(struct ia32_user_desc));
2793
2794 return 0;
2795}
2796
2797/*
2798 * This should match arch/i386/kernel/ptrace.c:native_regsets.
2799 * XXX ioperm? vm86?
2800 */
2801static const struct user_regset ia32_regsets[] = {
2802 {
2803 .core_note_type = NT_PRSTATUS,
2804 .n = sizeof(struct user_regs_struct32)/4,
2805 .size = 4, .align = 4,
2806 .get = ia32_genregs_get, .set = ia32_genregs_set
2807 },
2808 {
2809 .core_note_type = NT_PRFPREG,
2810 .n = sizeof(struct ia32_user_i387_struct) / 4,
2811 .size = 4, .align = 4,
2812 .get = ia32_fpregs_get, .set = ia32_fpregs_set
2813 },
2814 {
2815 .core_note_type = NT_PRXFPREG,
2816 .n = sizeof(struct ia32_user_fxsr_struct) / 4,
2817 .size = 4, .align = 4,
2818 .get = ia32_fpxregs_get, .set = ia32_fpxregs_set
2819 },
2820 {
2821 .core_note_type = NT_386_TLS,
2822 .n = GDT_ENTRY_TLS_ENTRIES,
2823 .bias = GDT_ENTRY_TLS_MIN,
2824 .size = sizeof(struct ia32_user_desc),
2825 .align = sizeof(struct ia32_user_desc),
2826 .active = ia32_tls_active,
2827 .get = ia32_tls_get, .set = ia32_tls_set,
2828 },
2829};
2830
2831const struct user_regset_view user_ia32_view = {
2832 .name = "i386", .e_machine = EM_386,
2833 .regsets = ia32_regsets, .n = ARRAY_SIZE(ia32_regsets)
2834};
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836long sys32_fadvise64_64(int fd, __u32 offset_low, __u32 offset_high,
2837 __u32 len_low, __u32 len_high, int advice)
2838{
2839 return sys_fadvise64_64(fd,
2840 (((u64)offset_high)<<32) | offset_low,
2841 (((u64)len_high)<<32) | len_low,
2842 advice);
2843}
2844
2845#ifdef NOTYET /* UNTESTED FOR IA64 FROM HERE DOWN */
2846
2847asmlinkage long sys32_setreuid(compat_uid_t ruid, compat_uid_t euid)
2848{
2849 uid_t sruid, seuid;
2850
2851 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2852 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2853 return sys_setreuid(sruid, seuid);
2854}
2855
2856asmlinkage long
2857sys32_setresuid(compat_uid_t ruid, compat_uid_t euid,
2858 compat_uid_t suid)
2859{
2860 uid_t sruid, seuid, ssuid;
2861
2862 sruid = (ruid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)ruid);
2863 seuid = (euid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)euid);
2864 ssuid = (suid == (compat_uid_t)-1) ? ((uid_t)-1) : ((uid_t)suid);
2865 return sys_setresuid(sruid, seuid, ssuid);
2866}
2867
2868asmlinkage long
2869sys32_setregid(compat_gid_t rgid, compat_gid_t egid)
2870{
2871 gid_t srgid, segid;
2872
2873 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2874 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2875 return sys_setregid(srgid, segid);
2876}
2877
2878asmlinkage long
2879sys32_setresgid(compat_gid_t rgid, compat_gid_t egid,
2880 compat_gid_t sgid)
2881{
2882 gid_t srgid, segid, ssgid;
2883
2884 srgid = (rgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)rgid);
2885 segid = (egid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)egid);
2886 ssgid = (sgid == (compat_gid_t)-1) ? ((gid_t)-1) : ((gid_t)sgid);
2887 return sys_setresgid(srgid, segid, ssgid);
2888}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889#endif /* NOTYET */