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