blob: 906c2a4e72794b1490904939aa28abbd4617ab53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
5
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -07006#include "linux/sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/slab.h"
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -07008#include "linux/types.h"
Bodo Stroesser858259c2005-11-07 00:58:55 -08009#include "linux/errno.h"
Jeff Dikeaf727902007-02-28 20:13:06 -080010#include "linux/spinlock.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "asm/uaccess.h"
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070012#include "asm/smp.h"
13#include "asm/ldt.h"
Bodo Stroesser858259c2005-11-07 00:58:55 -080014#include "asm/unistd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "kern.h"
Bodo Stroesser12919aa2006-01-18 17:42:39 -080016#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
19
Bodo Stroesser858259c2005-11-07 00:58:55 -080020#include "skas.h"
21#include "skas_ptrace.h"
22#include "asm/mmu_context.h"
Pekka Enberge8730eab2006-02-01 03:05:06 -080023#include "proc_mm.h"
Bodo Stroesser858259c2005-11-07 00:58:55 -080024
25long write_ldt_entry(struct mm_id * mm_idp, int func, struct user_desc * desc,
26 void **addr, int done)
27{
28 long res;
29
30 if(proc_mm){
31 /* This is a special handling for the case, that the mm to
32 * modify isn't current->active_mm.
33 * If this is called directly by modify_ldt,
34 * (current->active_mm->context.skas.u == mm_idp)
Jeff Dike77bf4402007-10-16 01:26:58 -070035 * will be true. So no call to __switch_mm(mm_idp) is done.
Bodo Stroesser858259c2005-11-07 00:58:55 -080036 * If this is called in case of init_new_ldt or PTRACE_LDT,
37 * mm_idp won't belong to current->active_mm, but child->mm.
38 * So we need to switch child's mm into our userspace, then
39 * later switch back.
40 *
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -080041 * Note: I'm unsure: should interrupts be disabled here?
Bodo Stroesser858259c2005-11-07 00:58:55 -080042 */
43 if(!current->active_mm || current->active_mm == &init_mm ||
44 mm_idp != &current->active_mm->context.skas.id)
Jeff Dike77bf4402007-10-16 01:26:58 -070045 __switch_mm(mm_idp);
Bodo Stroesser858259c2005-11-07 00:58:55 -080046 }
47
48 if(ptrace_ldt) {
49 struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
50 .func = func,
51 .ptr = desc,
52 .bytecount = sizeof(*desc)};
53 u32 cpu;
54 int pid;
55
56 if(!proc_mm)
57 pid = mm_idp->u.pid;
58 else {
59 cpu = get_cpu();
60 pid = userspace_pid[cpu];
61 }
62
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -080063 res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
Bodo Stroesser858259c2005-11-07 00:58:55 -080064
65 if(proc_mm)
66 put_cpu();
67 }
68 else {
69 void *stub_addr;
70 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
71 (sizeof(*desc) + sizeof(long) - 1) &
72 ~(sizeof(long) - 1),
73 addr, &stub_addr);
74 if(!res){
75 unsigned long args[] = { func,
76 (unsigned long)stub_addr,
77 sizeof(*desc),
78 0, 0, 0 };
79 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
80 0, addr, done);
81 }
82 }
83
84 if(proc_mm){
85 /* This is the second part of special handling, that makes
86 * PTRACE_LDT possible to implement.
87 */
88 if(current->active_mm && current->active_mm != &init_mm &&
89 mm_idp != &current->active_mm->context.skas.id)
Jeff Dike77bf4402007-10-16 01:26:58 -070090 __switch_mm(&current->active_mm->context.skas.id);
Bodo Stroesser858259c2005-11-07 00:58:55 -080091 }
92
93 return res;
94}
95
96static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
97{
98 int res, n;
99 struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
100 .func = 0,
101 .bytecount = bytecount,
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800102 .ptr = kmalloc(bytecount, GFP_KERNEL)};
Bodo Stroesser858259c2005-11-07 00:58:55 -0800103 u32 cpu;
104
105 if(ptrace_ldt.ptr == NULL)
106 return -ENOMEM;
107
108 /* This is called from sys_modify_ldt only, so userspace_pid gives
109 * us the right number
110 */
111
112 cpu = get_cpu();
Paolo 'Blaisorblade' Giarrusso07f4e2c2006-02-24 13:03:55 -0800113 res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800114 put_cpu();
115 if(res < 0)
116 goto out;
117
118 n = copy_to_user(ptr, ptrace_ldt.ptr, res);
119 if(n != 0)
120 res = -EFAULT;
121
122 out:
123 kfree(ptrace_ldt.ptr);
124
125 return res;
126}
127
128/*
129 * In skas mode, we hold our own ldt data in UML.
130 * Thus, the code implementing sys_modify_ldt_skas
131 * is very similar to (and mostly stolen from) sys_modify_ldt
132 * for arch/i386/kernel/ldt.c
133 * The routines copied and modified in part are:
134 * - read_ldt
135 * - read_default_ldt
136 * - write_ldt
137 * - sys_modify_ldt_skas
138 */
139
140static int read_ldt(void __user * ptr, unsigned long bytecount)
141{
142 int i, err = 0;
143 unsigned long size;
144 uml_ldt_t * ldt = &current->mm->context.skas.ldt;
145
146 if(!ldt->entry_count)
147 goto out;
148 if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
149 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
150 err = bytecount;
151
152 if(ptrace_ldt){
153 return read_ldt_from_host(ptr, bytecount);
154 }
155
156 down(&ldt->semaphore);
157 if(ldt->entry_count <= LDT_DIRECT_ENTRIES){
158 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
159 if(size > bytecount)
160 size = bytecount;
Jeff Dikee23181d2005-11-21 21:32:08 -0800161 if(copy_to_user(ptr, ldt->u.entries, size))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800162 err = -EFAULT;
163 bytecount -= size;
164 ptr += size;
165 }
166 else {
167 for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
168 i++){
169 size = PAGE_SIZE;
170 if(size > bytecount)
171 size = bytecount;
Jeff Dikee23181d2005-11-21 21:32:08 -0800172 if(copy_to_user(ptr, ldt->u.pages[i], size)){
Bodo Stroesser858259c2005-11-07 00:58:55 -0800173 err = -EFAULT;
174 break;
175 }
176 bytecount -= size;
177 ptr += size;
178 }
179 }
180 up(&ldt->semaphore);
181
182 if(bytecount == 0 || err == -EFAULT)
183 goto out;
184
185 if(clear_user(ptr, bytecount))
186 err = -EFAULT;
187
188out:
189 return err;
190}
191
192static int read_default_ldt(void __user * ptr, unsigned long bytecount)
193{
194 int err;
195
196 if(bytecount > 5*LDT_ENTRY_SIZE)
197 bytecount = 5*LDT_ENTRY_SIZE;
198
199 err = bytecount;
200 /* UML doesn't support lcall7 and lcall27.
201 * So, we don't really have a default ldt, but emulate
202 * an empty ldt of common host default ldt size.
203 */
204 if(clear_user(ptr, bytecount))
205 err = -EFAULT;
206
207 return err;
208}
209
210static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
211{
212 uml_ldt_t * ldt = &current->mm->context.skas.ldt;
213 struct mm_id * mm_idp = &current->mm->context.skas.id;
214 int i, err;
215 struct user_desc ldt_info;
216 struct ldt_entry entry0, *ldt_p;
217 void *addr = NULL;
218
219 err = -EINVAL;
220 if(bytecount != sizeof(ldt_info))
221 goto out;
222 err = -EFAULT;
223 if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
224 goto out;
225
226 err = -EINVAL;
227 if(ldt_info.entry_number >= LDT_ENTRIES)
228 goto out;
229 if(ldt_info.contents == 3){
230 if (func == 1)
231 goto out;
232 if (ldt_info.seg_not_present == 0)
233 goto out;
234 }
235
236 if(!ptrace_ldt)
237 down(&ldt->semaphore);
238
239 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
240 if(err)
241 goto out_unlock;
242 else if(ptrace_ldt) {
243 /* With PTRACE_LDT available, this is used as a flag only */
244 ldt->entry_count = 1;
245 goto out;
246 }
247
248 if(ldt_info.entry_number >= ldt->entry_count &&
249 ldt_info.entry_number >= LDT_DIRECT_ENTRIES){
250 for(i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
251 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
252 i++){
253 if(i == 0)
Jeff Dikee23181d2005-11-21 21:32:08 -0800254 memcpy(&entry0, ldt->u.entries,
255 sizeof(entry0));
256 ldt->u.pages[i] = (struct ldt_entry *)
257 __get_free_page(GFP_KERNEL|__GFP_ZERO);
258 if(!ldt->u.pages[i]){
Bodo Stroesser858259c2005-11-07 00:58:55 -0800259 err = -ENOMEM;
260 /* Undo the change in host */
261 memset(&ldt_info, 0, sizeof(ldt_info));
262 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
263 goto out_unlock;
264 }
265 if(i == 0) {
Jeff Dikee23181d2005-11-21 21:32:08 -0800266 memcpy(ldt->u.pages[0], &entry0,
267 sizeof(entry0));
268 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
Bodo Stroesser858259c2005-11-07 00:58:55 -0800269 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
270 }
271 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
272 }
273 }
274 if(ldt->entry_count <= ldt_info.entry_number)
275 ldt->entry_count = ldt_info.entry_number + 1;
276
277 if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
Jeff Dikee23181d2005-11-21 21:32:08 -0800278 ldt_p = ldt->u.entries + ldt_info.entry_number;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800279 else
Jeff Dikee23181d2005-11-21 21:32:08 -0800280 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
Bodo Stroesser858259c2005-11-07 00:58:55 -0800281 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
282
283 if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
284 (func == 1 || LDT_empty(&ldt_info))){
285 ldt_p->a = 0;
286 ldt_p->b = 0;
287 }
288 else{
289 if (func == 1)
290 ldt_info.useable = 0;
291 ldt_p->a = LDT_entry_a(&ldt_info);
292 ldt_p->b = LDT_entry_b(&ldt_info);
293 }
294 err = 0;
295
296out_unlock:
297 up(&ldt->semaphore);
298out:
299 return err;
300}
301
302static long do_modify_ldt_skas(int func, void __user *ptr,
303 unsigned long bytecount)
304{
305 int ret = -ENOSYS;
306
307 switch (func) {
308 case 0:
309 ret = read_ldt(ptr, bytecount);
310 break;
311 case 1:
312 case 0x11:
313 ret = write_ldt(ptr, bytecount, func);
314 break;
315 case 2:
316 ret = read_default_ldt(ptr, bytecount);
317 break;
318 }
319 return ret;
320}
321
Jeff Dikeaf727902007-02-28 20:13:06 -0800322static DEFINE_SPINLOCK(host_ldt_lock);
323static short dummy_list[9] = {0, -1};
324static short * host_ldt_entries = NULL;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800325
Jeff Dikeaf727902007-02-28 20:13:06 -0800326static void ldt_get_host_info(void)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800327{
328 long ret;
Jeff Dike622e6962007-03-29 01:20:32 -0700329 struct ldt_entry * ldt;
330 short *tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800331 int i, size, k, order;
332
Jeff Dikeaf727902007-02-28 20:13:06 -0800333 spin_lock(&host_ldt_lock);
334
335 if(host_ldt_entries != NULL){
336 spin_unlock(&host_ldt_lock);
337 return;
338 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800339 host_ldt_entries = dummy_list+1;
340
Jeff Dikeaf727902007-02-28 20:13:06 -0800341 spin_unlock(&host_ldt_lock);
342
Bodo Stroesser858259c2005-11-07 00:58:55 -0800343 for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
344
345 ldt = (struct ldt_entry *)
346 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
347 if(ldt == NULL) {
Jeff Dikeaf727902007-02-28 20:13:06 -0800348 printk("ldt_get_host_info: couldn't allocate buffer for host "
349 "ldt\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800350 return;
351 }
352
353 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
354 if(ret < 0) {
355 printk("ldt_get_host_info: couldn't read host ldt\n");
356 goto out_free;
357 }
358 if(ret == 0) {
359 /* default_ldt is active, simply write an empty entry 0 */
360 host_ldt_entries = dummy_list;
361 goto out_free;
362 }
363
364 for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
365 if(ldt[i].a != 0 || ldt[i].b != 0)
366 size++;
367 }
368
Jeff Dike91b165c2006-09-25 23:33:00 -0700369 if(size < ARRAY_SIZE(dummy_list))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800370 host_ldt_entries = dummy_list;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800371 else {
372 size = (size + 1) * sizeof(dummy_list[0]);
Jeff Dikeaf727902007-02-28 20:13:06 -0800373 tmp = kmalloc(size, GFP_KERNEL);
374 if(tmp == NULL) {
375 printk("ldt_get_host_info: couldn't allocate host ldt "
376 "list\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800377 goto out_free;
378 }
Jeff Dikeaf727902007-02-28 20:13:06 -0800379 host_ldt_entries = tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800380 }
381
382 for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){
383 if(ldt[i].a != 0 || ldt[i].b != 0) {
384 host_ldt_entries[k++] = i;
385 }
386 }
387 host_ldt_entries[k] = -1;
388
389out_free:
390 free_pages((unsigned long)ldt, order);
391}
392
393long init_new_ldt(struct mmu_context_skas * new_mm,
394 struct mmu_context_skas * from_mm)
395{
396 struct user_desc desc;
397 short * num_p;
398 int i;
399 long page, err=0;
400 void *addr = NULL;
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800401 struct proc_mm_op copy;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800402
Bodo Stroesser858259c2005-11-07 00:58:55 -0800403
404 if(!ptrace_ldt)
405 init_MUTEX(&new_mm->ldt.semaphore);
406
407 if(!from_mm){
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800408 memset(&desc, 0, sizeof(desc));
Bodo Stroesser858259c2005-11-07 00:58:55 -0800409 /*
410 * We have to initialize a clean ldt.
411 */
412 if(proc_mm) {
413 /*
414 * If the new mm was created using proc_mm, host's
415 * default-ldt currently is assigned, which normally
416 * contains the call-gates for lcall7 and lcall27.
417 * To remove these gates, we simply write an empty
418 * entry as number 0 to the host.
419 */
420 err = write_ldt_entry(&new_mm->id, 1, &desc,
421 &addr, 1);
422 }
423 else{
424 /*
425 * Now we try to retrieve info about the ldt, we
426 * inherited from the host. All ldt-entries found
427 * will be reset in the following loop
428 */
Jeff Dikeaf727902007-02-28 20:13:06 -0800429 ldt_get_host_info();
Bodo Stroesser858259c2005-11-07 00:58:55 -0800430 for(num_p=host_ldt_entries; *num_p != -1; num_p++){
431 desc.entry_number = *num_p;
432 err = write_ldt_entry(&new_mm->id, 1, &desc,
433 &addr, *(num_p + 1) == -1);
434 if(err)
435 break;
436 }
437 }
438 new_mm->ldt.entry_count = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800439
440 goto out;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800441 }
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800442
443 if(proc_mm){
444 /* We have a valid from_mm, so we now have to copy the LDT of
445 * from_mm to new_mm, because using proc_mm an new mm with
446 * an empty/default LDT was created in new_mm()
447 */
448 copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
449 .u =
450 { .copy_segments =
451 from_mm->id.u.mm_fd } } );
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700452 i = os_write_file(new_mm->id.u.mm_fd, &copy, sizeof(copy));
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800453 if(i != sizeof(copy))
454 printk("new_mm : /proc/mm copy_segments failed, "
455 "err = %d\n", -i);
456 }
457
458 if(!ptrace_ldt) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800459 /* Our local LDT is used to supply the data for
460 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
461 * i.e., we have to use the stub for modify_ldt, which
462 * can't handle the big read buffer of up to 64kB.
463 */
464 down(&from_mm->ldt.semaphore);
465 if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){
Jeff Dikee23181d2005-11-21 21:32:08 -0800466 memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
467 sizeof(new_mm->ldt.u.entries));
Bodo Stroesser858259c2005-11-07 00:58:55 -0800468 }
469 else{
470 i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
471 while(i-->0){
472 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
473 if (!page){
474 err = -ENOMEM;
475 break;
476 }
Jeff Dikee23181d2005-11-21 21:32:08 -0800477 new_mm->ldt.u.pages[i] =
478 (struct ldt_entry *) page;
479 memcpy(new_mm->ldt.u.pages[i],
480 from_mm->ldt.u.pages[i], PAGE_SIZE);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800481 }
482 }
483 new_mm->ldt.entry_count = from_mm->ldt.entry_count;
484 up(&from_mm->ldt.semaphore);
485 }
486
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800487 out:
Bodo Stroesser858259c2005-11-07 00:58:55 -0800488 return err;
489}
490
491
492void free_ldt(struct mmu_context_skas * mm)
493{
494 int i;
495
496 if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
497 i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
498 while(i-- > 0){
Jeff Dikee23181d2005-11-21 21:32:08 -0800499 free_page((long )mm->ldt.u.pages[i]);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800500 }
501 }
502 mm->ldt.entry_count = 0;
503}
Bodo Stroesser858259c2005-11-07 00:58:55 -0800504
505int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
506{
Jeff Dike6aa802c2007-10-16 01:26:56 -0700507 return do_modify_ldt_skas(func, ptr, bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800508}