blob: 3f2bf208d884ec6973d550f84a9f5d0625ae2e5f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Jeff Dike8192ab42008-02-04 22:30:53 -08009#include <asm/unistd.h>
Bodo Stroesser12919aa2006-01-18 17:42:39 -080010#include "os.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070011#include "proc_mm.h"
Bodo Stroesser858259c2005-11-07 00:58:55 -080012#include "skas.h"
13#include "skas_ptrace.h"
Jeff Dikeba180fd2007-10-16 01:27:00 -070014#include "sysdep/tls.h"
15
16extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -080017
WANG Cong99764fa2008-07-23 21:28:49 -070018static long write_ldt_entry(struct mm_id *mm_idp, int func,
19 struct user_desc *desc, void **addr, int done)
Bodo Stroesser858259c2005-11-07 00:58:55 -080020{
21 long res;
22
Jeff Dikeba180fd2007-10-16 01:27:00 -070023 if (proc_mm) {
24 /*
25 * This is a special handling for the case, that the mm to
Bodo Stroesser858259c2005-11-07 00:58:55 -080026 * modify isn't current->active_mm.
27 * If this is called directly by modify_ldt,
28 * (current->active_mm->context.skas.u == mm_idp)
Jeff Dike77bf4402007-10-16 01:26:58 -070029 * will be true. So no call to __switch_mm(mm_idp) is done.
Bodo Stroesser858259c2005-11-07 00:58:55 -080030 * If this is called in case of init_new_ldt or PTRACE_LDT,
31 * mm_idp won't belong to current->active_mm, but child->mm.
32 * So we need to switch child's mm into our userspace, then
33 * later switch back.
34 *
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -080035 * Note: I'm unsure: should interrupts be disabled here?
Bodo Stroesser858259c2005-11-07 00:58:55 -080036 */
Jeff Dikeba180fd2007-10-16 01:27:00 -070037 if (!current->active_mm || current->active_mm == &init_mm ||
Jeff Dike6c738ff2007-10-16 01:27:06 -070038 mm_idp != &current->active_mm->context.id)
Jeff Dike77bf4402007-10-16 01:26:58 -070039 __switch_mm(mm_idp);
Bodo Stroesser858259c2005-11-07 00:58:55 -080040 }
41
Jeff Dikeba180fd2007-10-16 01:27:00 -070042 if (ptrace_ldt) {
Bodo Stroesser858259c2005-11-07 00:58:55 -080043 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
44 .func = func,
45 .ptr = desc,
46 .bytecount = sizeof(*desc)};
47 u32 cpu;
48 int pid;
49
Jeff Dikeba180fd2007-10-16 01:27:00 -070050 if (!proc_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -080051 pid = mm_idp->u.pid;
52 else {
53 cpu = get_cpu();
54 pid = userspace_pid[cpu];
55 }
56
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -080057 res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
Bodo Stroesser858259c2005-11-07 00:58:55 -080058
Jeff Dikeba180fd2007-10-16 01:27:00 -070059 if (proc_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -080060 put_cpu();
61 }
62 else {
63 void *stub_addr;
64 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
65 (sizeof(*desc) + sizeof(long) - 1) &
66 ~(sizeof(long) - 1),
67 addr, &stub_addr);
Jeff Dikeba180fd2007-10-16 01:27:00 -070068 if (!res) {
Bodo Stroesser858259c2005-11-07 00:58:55 -080069 unsigned long args[] = { func,
70 (unsigned long)stub_addr,
71 sizeof(*desc),
72 0, 0, 0 };
73 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
74 0, addr, done);
75 }
76 }
77
Jeff Dikeba180fd2007-10-16 01:27:00 -070078 if (proc_mm) {
79 /*
80 * This is the second part of special handling, that makes
Bodo Stroesser858259c2005-11-07 00:58:55 -080081 * PTRACE_LDT possible to implement.
82 */
Jeff Dikeba180fd2007-10-16 01:27:00 -070083 if (current->active_mm && current->active_mm != &init_mm &&
Jeff Dike6c738ff2007-10-16 01:27:06 -070084 mm_idp != &current->active_mm->context.id)
85 __switch_mm(&current->active_mm->context.id);
Bodo Stroesser858259c2005-11-07 00:58:55 -080086 }
87
88 return res;
89}
90
91static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
92{
93 int res, n;
94 struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
95 .func = 0,
96 .bytecount = bytecount,
Robert P. J. Day5cbded52006-12-13 00:35:56 -080097 .ptr = kmalloc(bytecount, GFP_KERNEL)};
Bodo Stroesser858259c2005-11-07 00:58:55 -080098 u32 cpu;
99
Jeff Dikeba180fd2007-10-16 01:27:00 -0700100 if (ptrace_ldt.ptr == NULL)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800101 return -ENOMEM;
102
Jeff Dikeba180fd2007-10-16 01:27:00 -0700103 /*
104 * This is called from sys_modify_ldt only, so userspace_pid gives
Bodo Stroesser858259c2005-11-07 00:58:55 -0800105 * us the right number
106 */
107
108 cpu = get_cpu();
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -0800109 res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800110 put_cpu();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700111 if (res < 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800112 goto out;
113
114 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700115 if (n != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800116 res = -EFAULT;
117
118 out:
119 kfree(ptrace_ldt.ptr);
120
121 return res;
122}
123
124/*
125 * In skas mode, we hold our own ldt data in UML.
126 * Thus, the code implementing sys_modify_ldt_skas
127 * is very similar to (and mostly stolen from) sys_modify_ldt
128 * for arch/i386/kernel/ldt.c
129 * The routines copied and modified in part are:
130 * - read_ldt
131 * - read_default_ldt
132 * - write_ldt
133 * - sys_modify_ldt_skas
134 */
135
136static int read_ldt(void __user * ptr, unsigned long bytecount)
137{
138 int i, err = 0;
139 unsigned long size;
Jeff Dike6c738ff2007-10-16 01:27:06 -0700140 uml_ldt_t * ldt = &current->mm->context.ldt;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800141
Jeff Dikeba180fd2007-10-16 01:27:00 -0700142 if (!ldt->entry_count)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800143 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700144 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800145 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
146 err = bytecount;
147
Jeff Dikeba180fd2007-10-16 01:27:00 -0700148 if (ptrace_ldt)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800149 return read_ldt_from_host(ptr, bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800150
Daniel Walker01ac8352008-02-04 22:31:26 -0800151 mutex_lock(&ldt->lock);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700152 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800153 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700154 if (size > bytecount)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800155 size = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700156 if (copy_to_user(ptr, ldt->u.entries, size))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800157 err = -EFAULT;
158 bytecount -= size;
159 ptr += size;
160 }
161 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -0700162 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
163 i++) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800164 size = PAGE_SIZE;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700165 if (size > bytecount)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800166 size = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700167 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800168 err = -EFAULT;
169 break;
170 }
171 bytecount -= size;
172 ptr += size;
173 }
174 }
Daniel Walker01ac8352008-02-04 22:31:26 -0800175 mutex_unlock(&ldt->lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800176
Jeff Dikeba180fd2007-10-16 01:27:00 -0700177 if (bytecount == 0 || err == -EFAULT)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800178 goto out;
179
Jeff Dikeba180fd2007-10-16 01:27:00 -0700180 if (clear_user(ptr, bytecount))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800181 err = -EFAULT;
182
183out:
184 return err;
185}
186
187static int read_default_ldt(void __user * ptr, unsigned long bytecount)
188{
189 int err;
190
Jeff Dikeba180fd2007-10-16 01:27:00 -0700191 if (bytecount > 5*LDT_ENTRY_SIZE)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800192 bytecount = 5*LDT_ENTRY_SIZE;
193
194 err = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700195 /*
196 * UML doesn't support lcall7 and lcall27.
Bodo Stroesser858259c2005-11-07 00:58:55 -0800197 * So, we don't really have a default ldt, but emulate
198 * an empty ldt of common host default ldt size.
199 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700200 if (clear_user(ptr, bytecount))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800201 err = -EFAULT;
202
203 return err;
204}
205
206static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
207{
Jeff Dike6c738ff2007-10-16 01:27:06 -0700208 uml_ldt_t * ldt = &current->mm->context.ldt;
209 struct mm_id * mm_idp = &current->mm->context.id;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800210 int i, err;
211 struct user_desc ldt_info;
212 struct ldt_entry entry0, *ldt_p;
213 void *addr = NULL;
214
215 err = -EINVAL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700216 if (bytecount != sizeof(ldt_info))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800217 goto out;
218 err = -EFAULT;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700219 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800220 goto out;
221
222 err = -EINVAL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700223 if (ldt_info.entry_number >= LDT_ENTRIES)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800224 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700225 if (ldt_info.contents == 3) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800226 if (func == 1)
227 goto out;
228 if (ldt_info.seg_not_present == 0)
229 goto out;
230 }
231
Jeff Dikeba180fd2007-10-16 01:27:00 -0700232 if (!ptrace_ldt)
Daniel Walker01ac8352008-02-04 22:31:26 -0800233 mutex_lock(&ldt->lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800234
235 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700236 if (err)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800237 goto out_unlock;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700238 else if (ptrace_ldt) {
239 /* With PTRACE_LDT available, this is used as a flag only */
240 ldt->entry_count = 1;
241 goto out;
242 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800243
Jeff Dikeba180fd2007-10-16 01:27:00 -0700244 if (ldt_info.entry_number >= ldt->entry_count &&
245 ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
246 for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
247 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
248 i++) {
249 if (i == 0)
Jeff Dikee23181d2005-11-21 21:32:08 -0800250 memcpy(&entry0, ldt->u.entries,
251 sizeof(entry0));
252 ldt->u.pages[i] = (struct ldt_entry *)
253 __get_free_page(GFP_KERNEL|__GFP_ZERO);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700254 if (!ldt->u.pages[i]) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800255 err = -ENOMEM;
256 /* Undo the change in host */
257 memset(&ldt_info, 0, sizeof(ldt_info));
258 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
259 goto out_unlock;
260 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700261 if (i == 0) {
Jeff Dikee23181d2005-11-21 21:32:08 -0800262 memcpy(ldt->u.pages[0], &entry0,
263 sizeof(entry0));
264 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
Bodo Stroesser858259c2005-11-07 00:58:55 -0800265 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
266 }
267 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
268 }
269 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700270 if (ldt->entry_count <= ldt_info.entry_number)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800271 ldt->entry_count = ldt_info.entry_number + 1;
272
Jeff Dikeba180fd2007-10-16 01:27:00 -0700273 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
Jeff Dikee23181d2005-11-21 21:32:08 -0800274 ldt_p = ldt->u.entries + ldt_info.entry_number;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800275 else
Jeff Dikee23181d2005-11-21 21:32:08 -0800276 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
Bodo Stroesser858259c2005-11-07 00:58:55 -0800277 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
278
Jeff Dikeba180fd2007-10-16 01:27:00 -0700279 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
280 (func == 1 || LDT_empty(&ldt_info))) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800281 ldt_p->a = 0;
282 ldt_p->b = 0;
283 }
284 else{
285 if (func == 1)
286 ldt_info.useable = 0;
287 ldt_p->a = LDT_entry_a(&ldt_info);
288 ldt_p->b = LDT_entry_b(&ldt_info);
289 }
290 err = 0;
291
292out_unlock:
Daniel Walker01ac8352008-02-04 22:31:26 -0800293 mutex_unlock(&ldt->lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800294out:
295 return err;
296}
297
298static long do_modify_ldt_skas(int func, void __user *ptr,
299 unsigned long bytecount)
300{
301 int ret = -ENOSYS;
302
303 switch (func) {
304 case 0:
305 ret = read_ldt(ptr, bytecount);
306 break;
307 case 1:
308 case 0x11:
309 ret = write_ldt(ptr, bytecount, func);
310 break;
311 case 2:
312 ret = read_default_ldt(ptr, bytecount);
313 break;
314 }
315 return ret;
316}
317
Jeff Dikeaf727902007-02-28 20:13:06 -0800318static DEFINE_SPINLOCK(host_ldt_lock);
319static short dummy_list[9] = {0, -1};
320static short * host_ldt_entries = NULL;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800321
Jeff Dikeaf727902007-02-28 20:13:06 -0800322static void ldt_get_host_info(void)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800323{
324 long ret;
Jeff Dike622e6962007-03-29 01:20:32 -0700325 struct ldt_entry * ldt;
326 short *tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800327 int i, size, k, order;
328
Jeff Dikeaf727902007-02-28 20:13:06 -0800329 spin_lock(&host_ldt_lock);
330
Jeff Dikeba180fd2007-10-16 01:27:00 -0700331 if (host_ldt_entries != NULL) {
Jeff Dikeaf727902007-02-28 20:13:06 -0800332 spin_unlock(&host_ldt_lock);
333 return;
334 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800335 host_ldt_entries = dummy_list+1;
336
Jeff Dikeaf727902007-02-28 20:13:06 -0800337 spin_unlock(&host_ldt_lock);
338
Jeff Dikeba180fd2007-10-16 01:27:00 -0700339 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
340 ;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800341
342 ldt = (struct ldt_entry *)
343 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700344 if (ldt == NULL) {
345 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
346 "for host ldt\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800347 return;
348 }
349
350 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700351 if (ret < 0) {
352 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800353 goto out_free;
354 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700355 if (ret == 0) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800356 /* default_ldt is active, simply write an empty entry 0 */
357 host_ldt_entries = dummy_list;
358 goto out_free;
359 }
360
Jeff Dikeba180fd2007-10-16 01:27:00 -0700361 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
362 if (ldt[i].a != 0 || ldt[i].b != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800363 size++;
364 }
365
Jeff Dikeba180fd2007-10-16 01:27:00 -0700366 if (size < ARRAY_SIZE(dummy_list))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800367 host_ldt_entries = dummy_list;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800368 else {
369 size = (size + 1) * sizeof(dummy_list[0]);
Jeff Dikeaf727902007-02-28 20:13:06 -0800370 tmp = kmalloc(size, GFP_KERNEL);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700371 if (tmp == NULL) {
372 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
373 "host ldt list\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800374 goto out_free;
375 }
Jeff Dikeaf727902007-02-28 20:13:06 -0800376 host_ldt_entries = tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800377 }
378
Jeff Dikeba180fd2007-10-16 01:27:00 -0700379 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
380 if (ldt[i].a != 0 || ldt[i].b != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800381 host_ldt_entries[k++] = i;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800382 }
383 host_ldt_entries[k] = -1;
384
385out_free:
386 free_pages((unsigned long)ldt, order);
387}
388
Jeff Dike6c738ff2007-10-16 01:27:06 -0700389long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800390{
391 struct user_desc desc;
392 short * num_p;
393 int i;
394 long page, err=0;
395 void *addr = NULL;
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800396 struct proc_mm_op copy;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800397
Bodo Stroesser858259c2005-11-07 00:58:55 -0800398
Jeff Dikeba180fd2007-10-16 01:27:00 -0700399 if (!ptrace_ldt)
Daniel Walker01ac8352008-02-04 22:31:26 -0800400 mutex_init(&new_mm->ldt.lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800401
Jeff Dikeba180fd2007-10-16 01:27:00 -0700402 if (!from_mm) {
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800403 memset(&desc, 0, sizeof(desc));
Bodo Stroesser858259c2005-11-07 00:58:55 -0800404 /*
405 * We have to initialize a clean ldt.
406 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700407 if (proc_mm) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800408 /*
409 * If the new mm was created using proc_mm, host's
410 * default-ldt currently is assigned, which normally
411 * contains the call-gates for lcall7 and lcall27.
412 * To remove these gates, we simply write an empty
413 * entry as number 0 to the host.
414 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700415 err = write_ldt_entry(&new_mm->id, 1, &desc, &addr, 1);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800416 }
417 else{
418 /*
419 * Now we try to retrieve info about the ldt, we
420 * inherited from the host. All ldt-entries found
421 * will be reset in the following loop
422 */
Jeff Dikeaf727902007-02-28 20:13:06 -0800423 ldt_get_host_info();
Jeff Dikeba180fd2007-10-16 01:27:00 -0700424 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800425 desc.entry_number = *num_p;
426 err = write_ldt_entry(&new_mm->id, 1, &desc,
427 &addr, *(num_p + 1) == -1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700428 if (err)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800429 break;
430 }
431 }
432 new_mm->ldt.entry_count = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800433
434 goto out;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800435 }
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800436
Jeff Dikeba180fd2007-10-16 01:27:00 -0700437 if (proc_mm) {
438 /*
439 * We have a valid from_mm, so we now have to copy the LDT of
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800440 * from_mm to new_mm, because using proc_mm an new mm with
441 * an empty/default LDT was created in new_mm()
442 */
443 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
444 .u =
445 { .copy_segments =
446 from_mm->id.u.mm_fd } } );
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700447 i = os_write_file(new_mm->id.u.mm_fd, &copy, sizeof(copy));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700448 if (i != sizeof(copy))
449 printk(KERN_ERR "new_mm : /proc/mm copy_segments "
450 "failed, err = %d\n", -i);
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800451 }
452
Jeff Dikeba180fd2007-10-16 01:27:00 -0700453 if (!ptrace_ldt) {
454 /*
455 * Our local LDT is used to supply the data for
Bodo Stroesser858259c2005-11-07 00:58:55 -0800456 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
457 * i.e., we have to use the stub for modify_ldt, which
458 * can't handle the big read buffer of up to 64kB.
459 */
Daniel Walker01ac8352008-02-04 22:31:26 -0800460 mutex_lock(&from_mm->ldt.lock);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700461 if (from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES)
Jeff Dikee23181d2005-11-21 21:32:08 -0800462 memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
463 sizeof(new_mm->ldt.u.entries));
Jeff Dikeba180fd2007-10-16 01:27:00 -0700464 else {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800465 i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700466 while (i-->0) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800467 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700468 if (!page) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800469 err = -ENOMEM;
470 break;
471 }
Jeff Dikee23181d2005-11-21 21:32:08 -0800472 new_mm->ldt.u.pages[i] =
473 (struct ldt_entry *) page;
474 memcpy(new_mm->ldt.u.pages[i],
475 from_mm->ldt.u.pages[i], PAGE_SIZE);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800476 }
477 }
478 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
Daniel Walker01ac8352008-02-04 22:31:26 -0800479 mutex_unlock(&from_mm->ldt.lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800480 }
481
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800482 out:
Bodo Stroesser858259c2005-11-07 00:58:55 -0800483 return err;
484}
485
486
Jeff Dike6c738ff2007-10-16 01:27:06 -0700487void free_ldt(struct mm_context *mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800488{
489 int i;
490
Jeff Dikeba180fd2007-10-16 01:27:00 -0700491 if (!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800492 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700493 while (i-- > 0)
494 free_page((long) mm->ldt.u.pages[i]);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800495 }
496 mm->ldt.entry_count = 0;
497}
Bodo Stroesser858259c2005-11-07 00:58:55 -0800498
499int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
500{
Jeff Dike6aa802c2007-10-16 01:26:56 -0700501 return do_modify_ldt_skas(func, ptr, bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800502}