Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 6 | #include "linux/sched.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include "linux/slab.h" |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 8 | #include "linux/types.h" |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 9 | #include "linux/errno.h" |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 10 | #include "linux/spinlock.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "asm/uaccess.h" |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 12 | #include "asm/smp.h" |
| 13 | #include "asm/ldt.h" |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 14 | #include "asm/unistd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "kern.h" |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 16 | #include "mode_kern.h" |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 17 | #include "os.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | extern int modify_ldt(int func, void *ptr, unsigned long bytecount); |
| 20 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 21 | #include "skas.h" |
| 22 | #include "skas_ptrace.h" |
| 23 | #include "asm/mmu_context.h" |
Pekka Enberg | e8730eab | 2006-02-01 03:05:06 -0800 | [diff] [blame] | 24 | #include "proc_mm.h" |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 25 | |
| 26 | long write_ldt_entry(struct mm_id * mm_idp, int func, struct user_desc * desc, |
| 27 | void **addr, int done) |
| 28 | { |
| 29 | long res; |
| 30 | |
| 31 | if(proc_mm){ |
| 32 | /* This is a special handling for the case, that the mm to |
| 33 | * modify isn't current->active_mm. |
| 34 | * If this is called directly by modify_ldt, |
| 35 | * (current->active_mm->context.skas.u == mm_idp) |
| 36 | * will be true. So no call to switch_mm_skas(mm_idp) is done. |
| 37 | * If this is called in case of init_new_ldt or PTRACE_LDT, |
| 38 | * mm_idp won't belong to current->active_mm, but child->mm. |
| 39 | * So we need to switch child's mm into our userspace, then |
| 40 | * later switch back. |
| 41 | * |
Paolo 'Blaisorblade' Giarrusso | 07f4e2c | 2006-02-24 13:03:55 -0800 | [diff] [blame] | 42 | * Note: I'm unsure: should interrupts be disabled here? |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 43 | */ |
| 44 | if(!current->active_mm || current->active_mm == &init_mm || |
| 45 | mm_idp != ¤t->active_mm->context.skas.id) |
| 46 | switch_mm_skas(mm_idp); |
| 47 | } |
| 48 | |
| 49 | if(ptrace_ldt) { |
| 50 | struct ptrace_ldt ldt_op = (struct ptrace_ldt) { |
| 51 | .func = func, |
| 52 | .ptr = desc, |
| 53 | .bytecount = sizeof(*desc)}; |
| 54 | u32 cpu; |
| 55 | int pid; |
| 56 | |
| 57 | if(!proc_mm) |
| 58 | pid = mm_idp->u.pid; |
| 59 | else { |
| 60 | cpu = get_cpu(); |
| 61 | pid = userspace_pid[cpu]; |
| 62 | } |
| 63 | |
Paolo 'Blaisorblade' Giarrusso | 07f4e2c | 2006-02-24 13:03:55 -0800 | [diff] [blame] | 64 | res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 65 | |
| 66 | if(proc_mm) |
| 67 | put_cpu(); |
| 68 | } |
| 69 | else { |
| 70 | void *stub_addr; |
| 71 | res = syscall_stub_data(mm_idp, (unsigned long *)desc, |
| 72 | (sizeof(*desc) + sizeof(long) - 1) & |
| 73 | ~(sizeof(long) - 1), |
| 74 | addr, &stub_addr); |
| 75 | if(!res){ |
| 76 | unsigned long args[] = { func, |
| 77 | (unsigned long)stub_addr, |
| 78 | sizeof(*desc), |
| 79 | 0, 0, 0 }; |
| 80 | res = run_syscall_stub(mm_idp, __NR_modify_ldt, args, |
| 81 | 0, addr, done); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if(proc_mm){ |
| 86 | /* This is the second part of special handling, that makes |
| 87 | * PTRACE_LDT possible to implement. |
| 88 | */ |
| 89 | if(current->active_mm && current->active_mm != &init_mm && |
| 90 | mm_idp != ¤t->active_mm->context.skas.id) |
| 91 | switch_mm_skas(¤t->active_mm->context.skas.id); |
| 92 | } |
| 93 | |
| 94 | return res; |
| 95 | } |
| 96 | |
| 97 | static long read_ldt_from_host(void __user * ptr, unsigned long bytecount) |
| 98 | { |
| 99 | int res, n; |
| 100 | struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) { |
| 101 | .func = 0, |
| 102 | .bytecount = bytecount, |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 103 | .ptr = kmalloc(bytecount, GFP_KERNEL)}; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 104 | u32 cpu; |
| 105 | |
| 106 | if(ptrace_ldt.ptr == NULL) |
| 107 | return -ENOMEM; |
| 108 | |
| 109 | /* This is called from sys_modify_ldt only, so userspace_pid gives |
| 110 | * us the right number |
| 111 | */ |
| 112 | |
| 113 | cpu = get_cpu(); |
Paolo 'Blaisorblade' Giarrusso | 07f4e2c | 2006-02-24 13:03:55 -0800 | [diff] [blame] | 114 | res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 115 | put_cpu(); |
| 116 | if(res < 0) |
| 117 | goto out; |
| 118 | |
| 119 | n = copy_to_user(ptr, ptrace_ldt.ptr, res); |
| 120 | if(n != 0) |
| 121 | res = -EFAULT; |
| 122 | |
| 123 | out: |
| 124 | kfree(ptrace_ldt.ptr); |
| 125 | |
| 126 | return res; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * In skas mode, we hold our own ldt data in UML. |
| 131 | * Thus, the code implementing sys_modify_ldt_skas |
| 132 | * is very similar to (and mostly stolen from) sys_modify_ldt |
| 133 | * for arch/i386/kernel/ldt.c |
| 134 | * The routines copied and modified in part are: |
| 135 | * - read_ldt |
| 136 | * - read_default_ldt |
| 137 | * - write_ldt |
| 138 | * - sys_modify_ldt_skas |
| 139 | */ |
| 140 | |
| 141 | static int read_ldt(void __user * ptr, unsigned long bytecount) |
| 142 | { |
| 143 | int i, err = 0; |
| 144 | unsigned long size; |
| 145 | uml_ldt_t * ldt = ¤t->mm->context.skas.ldt; |
| 146 | |
| 147 | if(!ldt->entry_count) |
| 148 | goto out; |
| 149 | if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES) |
| 150 | bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES; |
| 151 | err = bytecount; |
| 152 | |
| 153 | if(ptrace_ldt){ |
| 154 | return read_ldt_from_host(ptr, bytecount); |
| 155 | } |
| 156 | |
| 157 | down(&ldt->semaphore); |
| 158 | if(ldt->entry_count <= LDT_DIRECT_ENTRIES){ |
| 159 | size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES; |
| 160 | if(size > bytecount) |
| 161 | size = bytecount; |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 162 | if(copy_to_user(ptr, ldt->u.entries, size)) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 163 | err = -EFAULT; |
| 164 | bytecount -= size; |
| 165 | ptr += size; |
| 166 | } |
| 167 | else { |
| 168 | for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount; |
| 169 | i++){ |
| 170 | size = PAGE_SIZE; |
| 171 | if(size > bytecount) |
| 172 | size = bytecount; |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 173 | if(copy_to_user(ptr, ldt->u.pages[i], size)){ |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 174 | err = -EFAULT; |
| 175 | break; |
| 176 | } |
| 177 | bytecount -= size; |
| 178 | ptr += size; |
| 179 | } |
| 180 | } |
| 181 | up(&ldt->semaphore); |
| 182 | |
| 183 | if(bytecount == 0 || err == -EFAULT) |
| 184 | goto out; |
| 185 | |
| 186 | if(clear_user(ptr, bytecount)) |
| 187 | err = -EFAULT; |
| 188 | |
| 189 | out: |
| 190 | return err; |
| 191 | } |
| 192 | |
| 193 | static int read_default_ldt(void __user * ptr, unsigned long bytecount) |
| 194 | { |
| 195 | int err; |
| 196 | |
| 197 | if(bytecount > 5*LDT_ENTRY_SIZE) |
| 198 | bytecount = 5*LDT_ENTRY_SIZE; |
| 199 | |
| 200 | err = bytecount; |
| 201 | /* UML doesn't support lcall7 and lcall27. |
| 202 | * So, we don't really have a default ldt, but emulate |
| 203 | * an empty ldt of common host default ldt size. |
| 204 | */ |
| 205 | if(clear_user(ptr, bytecount)) |
| 206 | err = -EFAULT; |
| 207 | |
| 208 | return err; |
| 209 | } |
| 210 | |
| 211 | static int write_ldt(void __user * ptr, unsigned long bytecount, int func) |
| 212 | { |
| 213 | uml_ldt_t * ldt = ¤t->mm->context.skas.ldt; |
| 214 | struct mm_id * mm_idp = ¤t->mm->context.skas.id; |
| 215 | int i, err; |
| 216 | struct user_desc ldt_info; |
| 217 | struct ldt_entry entry0, *ldt_p; |
| 218 | void *addr = NULL; |
| 219 | |
| 220 | err = -EINVAL; |
| 221 | if(bytecount != sizeof(ldt_info)) |
| 222 | goto out; |
| 223 | err = -EFAULT; |
| 224 | if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info))) |
| 225 | goto out; |
| 226 | |
| 227 | err = -EINVAL; |
| 228 | if(ldt_info.entry_number >= LDT_ENTRIES) |
| 229 | goto out; |
| 230 | if(ldt_info.contents == 3){ |
| 231 | if (func == 1) |
| 232 | goto out; |
| 233 | if (ldt_info.seg_not_present == 0) |
| 234 | goto out; |
| 235 | } |
| 236 | |
| 237 | if(!ptrace_ldt) |
| 238 | down(&ldt->semaphore); |
| 239 | |
| 240 | err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1); |
| 241 | if(err) |
| 242 | goto out_unlock; |
| 243 | else if(ptrace_ldt) { |
| 244 | /* With PTRACE_LDT available, this is used as a flag only */ |
| 245 | ldt->entry_count = 1; |
| 246 | goto out; |
| 247 | } |
| 248 | |
| 249 | if(ldt_info.entry_number >= ldt->entry_count && |
| 250 | ldt_info.entry_number >= LDT_DIRECT_ENTRIES){ |
| 251 | for(i=ldt->entry_count/LDT_ENTRIES_PER_PAGE; |
| 252 | i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number; |
| 253 | i++){ |
| 254 | if(i == 0) |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 255 | memcpy(&entry0, ldt->u.entries, |
| 256 | sizeof(entry0)); |
| 257 | ldt->u.pages[i] = (struct ldt_entry *) |
| 258 | __get_free_page(GFP_KERNEL|__GFP_ZERO); |
| 259 | if(!ldt->u.pages[i]){ |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 260 | err = -ENOMEM; |
| 261 | /* Undo the change in host */ |
| 262 | memset(&ldt_info, 0, sizeof(ldt_info)); |
| 263 | write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1); |
| 264 | goto out_unlock; |
| 265 | } |
| 266 | if(i == 0) { |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 267 | memcpy(ldt->u.pages[0], &entry0, |
| 268 | sizeof(entry0)); |
| 269 | memcpy(ldt->u.pages[0]+1, ldt->u.entries+1, |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 270 | sizeof(entry0)*(LDT_DIRECT_ENTRIES-1)); |
| 271 | } |
| 272 | ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE; |
| 273 | } |
| 274 | } |
| 275 | if(ldt->entry_count <= ldt_info.entry_number) |
| 276 | ldt->entry_count = ldt_info.entry_number + 1; |
| 277 | |
| 278 | if(ldt->entry_count <= LDT_DIRECT_ENTRIES) |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 279 | ldt_p = ldt->u.entries + ldt_info.entry_number; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 280 | else |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 281 | ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] + |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 282 | ldt_info.entry_number%LDT_ENTRIES_PER_PAGE; |
| 283 | |
| 284 | if(ldt_info.base_addr == 0 && ldt_info.limit == 0 && |
| 285 | (func == 1 || LDT_empty(&ldt_info))){ |
| 286 | ldt_p->a = 0; |
| 287 | ldt_p->b = 0; |
| 288 | } |
| 289 | else{ |
| 290 | if (func == 1) |
| 291 | ldt_info.useable = 0; |
| 292 | ldt_p->a = LDT_entry_a(&ldt_info); |
| 293 | ldt_p->b = LDT_entry_b(&ldt_info); |
| 294 | } |
| 295 | err = 0; |
| 296 | |
| 297 | out_unlock: |
| 298 | up(&ldt->semaphore); |
| 299 | out: |
| 300 | return err; |
| 301 | } |
| 302 | |
| 303 | static long do_modify_ldt_skas(int func, void __user *ptr, |
| 304 | unsigned long bytecount) |
| 305 | { |
| 306 | int ret = -ENOSYS; |
| 307 | |
| 308 | switch (func) { |
| 309 | case 0: |
| 310 | ret = read_ldt(ptr, bytecount); |
| 311 | break; |
| 312 | case 1: |
| 313 | case 0x11: |
| 314 | ret = write_ldt(ptr, bytecount, func); |
| 315 | break; |
| 316 | case 2: |
| 317 | ret = read_default_ldt(ptr, bytecount); |
| 318 | break; |
| 319 | } |
| 320 | return ret; |
| 321 | } |
| 322 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 323 | static DEFINE_SPINLOCK(host_ldt_lock); |
| 324 | static short dummy_list[9] = {0, -1}; |
| 325 | static short * host_ldt_entries = NULL; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 326 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 327 | static void ldt_get_host_info(void) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 328 | { |
| 329 | long ret; |
Jeff Dike | 622e696 | 2007-03-29 01:20:32 -0700 | [diff] [blame] | 330 | struct ldt_entry * ldt; |
| 331 | short *tmp; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 332 | int i, size, k, order; |
| 333 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 334 | spin_lock(&host_ldt_lock); |
| 335 | |
| 336 | if(host_ldt_entries != NULL){ |
| 337 | spin_unlock(&host_ldt_lock); |
| 338 | return; |
| 339 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 340 | host_ldt_entries = dummy_list+1; |
| 341 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 342 | spin_unlock(&host_ldt_lock); |
| 343 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 344 | for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++); |
| 345 | |
| 346 | ldt = (struct ldt_entry *) |
| 347 | __get_free_pages(GFP_KERNEL|__GFP_ZERO, order); |
| 348 | if(ldt == NULL) { |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 349 | printk("ldt_get_host_info: couldn't allocate buffer for host " |
| 350 | "ldt\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
| 354 | ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE); |
| 355 | if(ret < 0) { |
| 356 | printk("ldt_get_host_info: couldn't read host ldt\n"); |
| 357 | goto out_free; |
| 358 | } |
| 359 | if(ret == 0) { |
| 360 | /* default_ldt is active, simply write an empty entry 0 */ |
| 361 | host_ldt_entries = dummy_list; |
| 362 | goto out_free; |
| 363 | } |
| 364 | |
| 365 | for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){ |
| 366 | if(ldt[i].a != 0 || ldt[i].b != 0) |
| 367 | size++; |
| 368 | } |
| 369 | |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 370 | if(size < ARRAY_SIZE(dummy_list)) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 371 | host_ldt_entries = dummy_list; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 372 | else { |
| 373 | size = (size + 1) * sizeof(dummy_list[0]); |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 374 | tmp = kmalloc(size, GFP_KERNEL); |
| 375 | if(tmp == NULL) { |
| 376 | printk("ldt_get_host_info: couldn't allocate host ldt " |
| 377 | "list\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 378 | goto out_free; |
| 379 | } |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 380 | host_ldt_entries = tmp; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){ |
| 384 | if(ldt[i].a != 0 || ldt[i].b != 0) { |
| 385 | host_ldt_entries[k++] = i; |
| 386 | } |
| 387 | } |
| 388 | host_ldt_entries[k] = -1; |
| 389 | |
| 390 | out_free: |
| 391 | free_pages((unsigned long)ldt, order); |
| 392 | } |
| 393 | |
| 394 | long init_new_ldt(struct mmu_context_skas * new_mm, |
| 395 | struct mmu_context_skas * from_mm) |
| 396 | { |
| 397 | struct user_desc desc; |
| 398 | short * num_p; |
| 399 | int i; |
| 400 | long page, err=0; |
| 401 | void *addr = NULL; |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 402 | struct proc_mm_op copy; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 403 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 404 | |
| 405 | if(!ptrace_ldt) |
| 406 | init_MUTEX(&new_mm->ldt.semaphore); |
| 407 | |
| 408 | if(!from_mm){ |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 409 | memset(&desc, 0, sizeof(desc)); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 410 | /* |
| 411 | * We have to initialize a clean ldt. |
| 412 | */ |
| 413 | if(proc_mm) { |
| 414 | /* |
| 415 | * If the new mm was created using proc_mm, host's |
| 416 | * default-ldt currently is assigned, which normally |
| 417 | * contains the call-gates for lcall7 and lcall27. |
| 418 | * To remove these gates, we simply write an empty |
| 419 | * entry as number 0 to the host. |
| 420 | */ |
| 421 | err = write_ldt_entry(&new_mm->id, 1, &desc, |
| 422 | &addr, 1); |
| 423 | } |
| 424 | else{ |
| 425 | /* |
| 426 | * Now we try to retrieve info about the ldt, we |
| 427 | * inherited from the host. All ldt-entries found |
| 428 | * will be reset in the following loop |
| 429 | */ |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 430 | ldt_get_host_info(); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 431 | for(num_p=host_ldt_entries; *num_p != -1; num_p++){ |
| 432 | desc.entry_number = *num_p; |
| 433 | err = write_ldt_entry(&new_mm->id, 1, &desc, |
| 434 | &addr, *(num_p + 1) == -1); |
| 435 | if(err) |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | new_mm->ldt.entry_count = 0; |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 440 | |
| 441 | goto out; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 442 | } |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 443 | |
| 444 | if(proc_mm){ |
| 445 | /* We have a valid from_mm, so we now have to copy the LDT of |
| 446 | * from_mm to new_mm, because using proc_mm an new mm with |
| 447 | * an empty/default LDT was created in new_mm() |
| 448 | */ |
| 449 | copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS, |
| 450 | .u = |
| 451 | { .copy_segments = |
| 452 | from_mm->id.u.mm_fd } } ); |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 453 | i = os_write_file(new_mm->id.u.mm_fd, ©, sizeof(copy)); |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 454 | if(i != sizeof(copy)) |
| 455 | printk("new_mm : /proc/mm copy_segments failed, " |
| 456 | "err = %d\n", -i); |
| 457 | } |
| 458 | |
| 459 | if(!ptrace_ldt) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 460 | /* Our local LDT is used to supply the data for |
| 461 | * modify_ldt(READLDT), if PTRACE_LDT isn't available, |
| 462 | * i.e., we have to use the stub for modify_ldt, which |
| 463 | * can't handle the big read buffer of up to 64kB. |
| 464 | */ |
| 465 | down(&from_mm->ldt.semaphore); |
| 466 | if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){ |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 467 | memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries, |
| 468 | sizeof(new_mm->ldt.u.entries)); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 469 | } |
| 470 | else{ |
| 471 | i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE; |
| 472 | while(i-->0){ |
| 473 | page = __get_free_page(GFP_KERNEL|__GFP_ZERO); |
| 474 | if (!page){ |
| 475 | err = -ENOMEM; |
| 476 | break; |
| 477 | } |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 478 | new_mm->ldt.u.pages[i] = |
| 479 | (struct ldt_entry *) page; |
| 480 | memcpy(new_mm->ldt.u.pages[i], |
| 481 | from_mm->ldt.u.pages[i], PAGE_SIZE); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | new_mm->ldt.entry_count = from_mm->ldt.entry_count; |
| 485 | up(&from_mm->ldt.semaphore); |
| 486 | } |
| 487 | |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 488 | out: |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 489 | return err; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void free_ldt(struct mmu_context_skas * mm) |
| 494 | { |
| 495 | int i; |
| 496 | |
| 497 | if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){ |
| 498 | i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE; |
| 499 | while(i-- > 0){ |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 500 | free_page((long )mm->ldt.u.pages[i]); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | mm->ldt.entry_count = 0; |
| 504 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 505 | |
| 506 | int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount) |
| 507 | { |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame^] | 508 | return do_modify_ldt_skas(func, ptr, bytecount); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 509 | } |