blob: 505ed5c9a68d56daf96ce7f3badf9a027daa8fb8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike8192ab42008-02-04 22:30:53 -08006#include <linux/mm.h>
7#include <linux/sched.h>
8#include <asm/unistd.h>
Bodo Stroesser12919aa2006-01-18 17:42:39 -08009#include "os.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070010#include "proc_mm.h"
Bodo Stroesser858259c2005-11-07 00:58:55 -080011#include "skas.h"
12#include "skas_ptrace.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070013#include "sysdep/tls.h"
14
15extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -080016
17long 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 Dikeba180fd2007-10-16 01:27:00 -070022 if (proc_mm) {
23 /*
24 * This is a special handling for the case, that the mm to
Bodo Stroesser858259c2005-11-07 00:58:55 -080025 * 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 Dike77bf4402007-10-16 01:26:58 -070028 * will be true. So no call to __switch_mm(mm_idp) is done.
Bodo Stroesser858259c2005-11-07 00:58:55 -080029 * 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' Giarrusso07f4e2c2006-02-24 13:03:55 -080034 * Note: I'm unsure: should interrupts be disabled here?
Bodo Stroesser858259c2005-11-07 00:58:55 -080035 */
Jeff Dikeba180fd2007-10-16 01:27:00 -070036 if (!current->active_mm || current->active_mm == &init_mm ||
Jeff Dike6c738ff2007-10-16 01:27:06 -070037 mm_idp != &current->active_mm->context.id)
Jeff Dike77bf4402007-10-16 01:26:58 -070038 __switch_mm(mm_idp);
Bodo Stroesser858259c2005-11-07 00:58:55 -080039 }
40
Jeff Dikeba180fd2007-10-16 01:27:00 -070041 if (ptrace_ldt) {
Bodo Stroesser858259c2005-11-07 00:58:55 -080042 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 Dikeba180fd2007-10-16 01:27:00 -070049 if (!proc_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -080050 pid = mm_idp->u.pid;
51 else {
52 cpu = get_cpu();
53 pid = userspace_pid[cpu];
54 }
55
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -080056 res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
Bodo Stroesser858259c2005-11-07 00:58:55 -080057
Jeff Dikeba180fd2007-10-16 01:27:00 -070058 if (proc_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -080059 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 Dikeba180fd2007-10-16 01:27:00 -070067 if (!res) {
Bodo Stroesser858259c2005-11-07 00:58:55 -080068 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 Dikeba180fd2007-10-16 01:27:00 -070077 if (proc_mm) {
78 /*
79 * This is the second part of special handling, that makes
Bodo Stroesser858259c2005-11-07 00:58:55 -080080 * PTRACE_LDT possible to implement.
81 */
Jeff Dikeba180fd2007-10-16 01:27:00 -070082 if (current->active_mm && current->active_mm != &init_mm &&
Jeff Dike6c738ff2007-10-16 01:27:06 -070083 mm_idp != &current->active_mm->context.id)
84 __switch_mm(&current->active_mm->context.id);
Bodo Stroesser858259c2005-11-07 00:58:55 -080085 }
86
87 return res;
88}
89
90static 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. Day5cbded52006-12-13 00:35:56 -080096 .ptr = kmalloc(bytecount, GFP_KERNEL)};
Bodo Stroesser858259c2005-11-07 00:58:55 -080097 u32 cpu;
98
Jeff Dikeba180fd2007-10-16 01:27:00 -070099 if (ptrace_ldt.ptr == NULL)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800100 return -ENOMEM;
101
Jeff Dikeba180fd2007-10-16 01:27:00 -0700102 /*
103 * This is called from sys_modify_ldt only, so userspace_pid gives
Bodo Stroesser858259c2005-11-07 00:58:55 -0800104 * us the right number
105 */
106
107 cpu = get_cpu();
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -0800108 res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800109 put_cpu();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700110 if (res < 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800111 goto out;
112
113 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700114 if (n != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800115 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
135static int read_ldt(void __user * ptr, unsigned long bytecount)
136{
137 int i, err = 0;
138 unsigned long size;
Jeff Dike6c738ff2007-10-16 01:27:06 -0700139 uml_ldt_t * ldt = &current->mm->context.ldt;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800140
Jeff Dikeba180fd2007-10-16 01:27:00 -0700141 if (!ldt->entry_count)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800142 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700143 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800144 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
145 err = bytecount;
146
Jeff Dikeba180fd2007-10-16 01:27:00 -0700147 if (ptrace_ldt)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800148 return read_ldt_from_host(ptr, bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800149
150 down(&ldt->semaphore);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700151 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800152 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700153 if (size > bytecount)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800154 size = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700155 if (copy_to_user(ptr, ldt->u.entries, size))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800156 err = -EFAULT;
157 bytecount -= size;
158 ptr += size;
159 }
160 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700161 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
162 i++) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800163 size = PAGE_SIZE;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700164 if (size > bytecount)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800165 size = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700166 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800167 err = -EFAULT;
168 break;
169 }
170 bytecount -= size;
171 ptr += size;
172 }
173 }
174 up(&ldt->semaphore);
175
Jeff Dikeba180fd2007-10-16 01:27:00 -0700176 if (bytecount == 0 || err == -EFAULT)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800177 goto out;
178
Jeff Dikeba180fd2007-10-16 01:27:00 -0700179 if (clear_user(ptr, bytecount))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800180 err = -EFAULT;
181
182out:
183 return err;
184}
185
186static int read_default_ldt(void __user * ptr, unsigned long bytecount)
187{
188 int err;
189
Jeff Dikeba180fd2007-10-16 01:27:00 -0700190 if (bytecount > 5*LDT_ENTRY_SIZE)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800191 bytecount = 5*LDT_ENTRY_SIZE;
192
193 err = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700194 /*
195 * UML doesn't support lcall7 and lcall27.
Bodo Stroesser858259c2005-11-07 00:58:55 -0800196 * So, we don't really have a default ldt, but emulate
197 * an empty ldt of common host default ldt size.
198 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700199 if (clear_user(ptr, bytecount))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800200 err = -EFAULT;
201
202 return err;
203}
204
205static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
206{
Jeff Dike6c738ff2007-10-16 01:27:06 -0700207 uml_ldt_t * ldt = &current->mm->context.ldt;
208 struct mm_id * mm_idp = &current->mm->context.id;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800209 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 Dikeba180fd2007-10-16 01:27:00 -0700215 if (bytecount != sizeof(ldt_info))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800216 goto out;
217 err = -EFAULT;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700218 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800219 goto out;
220
221 err = -EINVAL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700222 if (ldt_info.entry_number >= LDT_ENTRIES)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800223 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700224 if (ldt_info.contents == 3) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800225 if (func == 1)
226 goto out;
227 if (ldt_info.seg_not_present == 0)
228 goto out;
229 }
230
Jeff Dikeba180fd2007-10-16 01:27:00 -0700231 if (!ptrace_ldt)
232 down(&ldt->semaphore);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800233
234 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700235 if (err)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800236 goto out_unlock;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700237 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 Stroesser858259c2005-11-07 00:58:55 -0800242
Jeff Dikeba180fd2007-10-16 01:27:00 -0700243 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 Dikee23181d2005-11-21 21:32:08 -0800249 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 Dikeba180fd2007-10-16 01:27:00 -0700253 if (!ldt->u.pages[i]) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800254 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 Dikeba180fd2007-10-16 01:27:00 -0700260 if (i == 0) {
Jeff Dikee23181d2005-11-21 21:32:08 -0800261 memcpy(ldt->u.pages[0], &entry0,
262 sizeof(entry0));
263 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
Bodo Stroesser858259c2005-11-07 00:58:55 -0800264 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
265 }
266 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
267 }
268 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700269 if (ldt->entry_count <= ldt_info.entry_number)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800270 ldt->entry_count = ldt_info.entry_number + 1;
271
Jeff Dikeba180fd2007-10-16 01:27:00 -0700272 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
Jeff Dikee23181d2005-11-21 21:32:08 -0800273 ldt_p = ldt->u.entries + ldt_info.entry_number;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800274 else
Jeff Dikee23181d2005-11-21 21:32:08 -0800275 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
Bodo Stroesser858259c2005-11-07 00:58:55 -0800276 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
277
Jeff Dikeba180fd2007-10-16 01:27:00 -0700278 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
279 (func == 1 || LDT_empty(&ldt_info))) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800280 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
291out_unlock:
292 up(&ldt->semaphore);
293out:
294 return err;
295}
296
297static 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 Dikeaf727902007-02-28 20:13:06 -0800317static DEFINE_SPINLOCK(host_ldt_lock);
318static short dummy_list[9] = {0, -1};
319static short * host_ldt_entries = NULL;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800320
Jeff Dikeaf727902007-02-28 20:13:06 -0800321static void ldt_get_host_info(void)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800322{
323 long ret;
Jeff Dike622e6962007-03-29 01:20:32 -0700324 struct ldt_entry * ldt;
325 short *tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800326 int i, size, k, order;
327
Jeff Dikeaf727902007-02-28 20:13:06 -0800328 spin_lock(&host_ldt_lock);
329
Jeff Dikeba180fd2007-10-16 01:27:00 -0700330 if (host_ldt_entries != NULL) {
Jeff Dikeaf727902007-02-28 20:13:06 -0800331 spin_unlock(&host_ldt_lock);
332 return;
333 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800334 host_ldt_entries = dummy_list+1;
335
Jeff Dikeaf727902007-02-28 20:13:06 -0800336 spin_unlock(&host_ldt_lock);
337
Jeff Dikeba180fd2007-10-16 01:27:00 -0700338 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
339 ;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800340
341 ldt = (struct ldt_entry *)
342 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700343 if (ldt == NULL) {
344 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
345 "for host ldt\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800346 return;
347 }
348
349 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700350 if (ret < 0) {
351 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800352 goto out_free;
353 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700354 if (ret == 0) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800355 /* default_ldt is active, simply write an empty entry 0 */
356 host_ldt_entries = dummy_list;
357 goto out_free;
358 }
359
Jeff Dikeba180fd2007-10-16 01:27:00 -0700360 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
361 if (ldt[i].a != 0 || ldt[i].b != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800362 size++;
363 }
364
Jeff Dikeba180fd2007-10-16 01:27:00 -0700365 if (size < ARRAY_SIZE(dummy_list))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800366 host_ldt_entries = dummy_list;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800367 else {
368 size = (size + 1) * sizeof(dummy_list[0]);
Jeff Dikeaf727902007-02-28 20:13:06 -0800369 tmp = kmalloc(size, GFP_KERNEL);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700370 if (tmp == NULL) {
371 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
372 "host ldt list\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800373 goto out_free;
374 }
Jeff Dikeaf727902007-02-28 20:13:06 -0800375 host_ldt_entries = tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800376 }
377
Jeff Dikeba180fd2007-10-16 01:27:00 -0700378 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
379 if (ldt[i].a != 0 || ldt[i].b != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800380 host_ldt_entries[k++] = i;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800381 }
382 host_ldt_entries[k] = -1;
383
384out_free:
385 free_pages((unsigned long)ldt, order);
386}
387
Jeff Dike6c738ff2007-10-16 01:27:06 -0700388long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800389{
390 struct user_desc desc;
391 short * num_p;
392 int i;
393 long page, err=0;
394 void *addr = NULL;
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800395 struct proc_mm_op copy;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800396
Bodo Stroesser858259c2005-11-07 00:58:55 -0800397
Jeff Dikeba180fd2007-10-16 01:27:00 -0700398 if (!ptrace_ldt)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800399 init_MUTEX(&new_mm->ldt.semaphore);
400
Jeff Dikeba180fd2007-10-16 01:27:00 -0700401 if (!from_mm) {
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800402 memset(&desc, 0, sizeof(desc));
Bodo Stroesser858259c2005-11-07 00:58:55 -0800403 /*
404 * We have to initialize a clean ldt.
405 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700406 if (proc_mm) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800407 /*
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 Dikeba180fd2007-10-16 01:27:00 -0700414 err = write_ldt_entry(&new_mm->id, 1, &desc, &addr, 1);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800415 }
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 Dikeaf727902007-02-28 20:13:06 -0800422 ldt_get_host_info();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700423 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800424 desc.entry_number = *num_p;
425 err = write_ldt_entry(&new_mm->id, 1, &desc,
426 &addr, *(num_p + 1) == -1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700427 if (err)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800428 break;
429 }
430 }
431 new_mm->ldt.entry_count = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800432
433 goto out;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800434 }
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800435
Jeff Dikeba180fd2007-10-16 01:27:00 -0700436 if (proc_mm) {
437 /*
438 * We have a valid from_mm, so we now have to copy the LDT of
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800439 * 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 Dikea6ea4cc2007-05-06 14:51:43 -0700446 i = os_write_file(new_mm->id.u.mm_fd, &copy, sizeof(copy));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700447 if (i != sizeof(copy))
448 printk(KERN_ERR "new_mm : /proc/mm copy_segments "
449 "failed, err = %d\n", -i);
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800450 }
451
Jeff Dikeba180fd2007-10-16 01:27:00 -0700452 if (!ptrace_ldt) {
453 /*
454 * Our local LDT is used to supply the data for
Bodo Stroesser858259c2005-11-07 00:58:55 -0800455 * 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 Dikeba180fd2007-10-16 01:27:00 -0700460 if (from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES)
Jeff Dikee23181d2005-11-21 21:32:08 -0800461 memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
462 sizeof(new_mm->ldt.u.entries));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700463 else {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800464 i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700465 while (i-->0) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800466 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700467 if (!page) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800468 err = -ENOMEM;
469 break;
470 }
Jeff Dikee23181d2005-11-21 21:32:08 -0800471 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 Stroesser858259c2005-11-07 00:58:55 -0800475 }
476 }
477 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
478 up(&from_mm->ldt.semaphore);
479 }
480
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800481 out:
Bodo Stroesser858259c2005-11-07 00:58:55 -0800482 return err;
483}
484
485
Jeff Dike6c738ff2007-10-16 01:27:06 -0700486void free_ldt(struct mm_context *mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800487{
488 int i;
489
Jeff Dikeba180fd2007-10-16 01:27:00 -0700490 if (!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800491 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700492 while (i-- > 0)
493 free_page((long) mm->ldt.u.pages[i]);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800494 }
495 mm->ldt.entry_count = 0;
496}
Bodo Stroesser858259c2005-11-07 00:58:55 -0800497
498int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
499{
Jeff Dike6aa802c2007-10-16 01:26:56 -0700500 return do_modify_ldt_skas(func, ptr, bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800501}