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