blob: bd3c34aa52e5d3d52542d61ca6ef8ab26c9b17e3 [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
6#include "linux/config.h"
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -07007#include "linux/sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include "linux/slab.h"
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -07009#include "linux/types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "asm/uaccess.h"
11#include "asm/ptrace.h"
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070012#include "asm/smp.h"
13#include "asm/ldt.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "choose-mode.h"
15#include "kern.h"
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070016#include "mode_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#ifdef CONFIG_MODE_TT
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
21
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070022static int do_modify_ldt_tt(int func, void *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 return modify_ldt(func, ptr, bytecount);
25}
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#endif
28
29#ifdef CONFIG_MODE_SKAS
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070031#include "skas.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "skas_ptrace.h"
33
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070034static int do_modify_ldt_skas(int func, void *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
36 struct ptrace_ldt ldt;
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070037 u32 cpu;
38 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070040 ldt = ((struct ptrace_ldt) { .func = func,
41 .ptr = ptr,
42 .bytecount = bytecount });
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070044 cpu = get_cpu();
45 res = ptrace(PTRACE_LDT, userspace_pid[cpu], 0, (unsigned long) &ldt);
46 put_cpu();
47
48 return res;
49}
50#endif
51
52int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
53{
54 struct user_desc info;
55 int res = 0;
56 void *buf = NULL;
57 void *p = NULL; /* What we pass to host. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 switch(func){
60 case 1:
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070061 case 0x11: /* write_ldt */
62 /* Do this check now to avoid overflows. */
63 if (bytecount != sizeof(struct user_desc)) {
64 res = -EINVAL;
65 goto out;
66 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070068 if(copy_from_user(&info, ptr, sizeof(info))) {
69 res = -EFAULT;
70 goto out;
71 }
72
73 p = &info;
74 break;
75 case 0:
76 case 2: /* read_ldt */
77
78 /* The use of info avoids kmalloc on the write case, not on the
79 * read one. */
80 buf = kmalloc(bytecount, GFP_KERNEL);
81 if (!buf) {
82 res = -ENOMEM;
83 goto out;
84 }
85 p = buf;
86 default:
87 res = -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 goto out;
89 }
90
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070091 res = CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func,
92 p, bytecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if(res < 0)
94 goto out;
95
96 switch(func){
97 case 0:
98 case 2:
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -070099 /* Modify_ldt was for reading and returned the number of read
100 * bytes.*/
101 if(copy_to_user(ptr, p, res))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 res = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 break;
104 }
105
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -0700106out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 kfree(buf);
Paolo 'Blaisorblade' Giarrusso2e5e5592005-07-14 00:33:37 -0700108 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}