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