aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/ldt_64.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/kernel/ldt_64.c')
-rw-r--r--arch/x86/kernel/ldt_64.c252
1 files changed, 252 insertions, 0 deletions
diff --git a/arch/x86/kernel/ldt_64.c b/arch/x86/kernel/ldt_64.c
new file mode 100644
index 000000000000..bc9ffd5c19cc
--- /dev/null
+++ b/arch/x86/kernel/ldt_64.c
@@ -0,0 +1,252 @@
1/*
2 * linux/arch/x86_64/kernel/ldt.c
3 *
4 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
5 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
6 * Copyright (C) 2002 Andi Kleen
7 *
8 * This handles calls from both 32bit and 64bit mode.
9 */
10
11#include <linux/errno.h>
12#include <linux/sched.h>
13#include <linux/string.h>
14#include <linux/mm.h>
15#include <linux/smp.h>
16#include <linux/vmalloc.h>
17#include <linux/slab.h>
18
19#include <asm/uaccess.h>
20#include <asm/system.h>
21#include <asm/ldt.h>
22#include <asm/desc.h>
23#include <asm/proto.h>
24
25#ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
26static void flush_ldt(void *null)
27{
28 if (current->active_mm)
29 load_LDT(&current->active_mm->context);
30}
31#endif
32
33static int alloc_ldt(mm_context_t *pc, unsigned mincount, int reload)
34{
35 void *oldldt;
36 void *newldt;
37 unsigned oldsize;
38
39 if (mincount <= (unsigned)pc->size)
40 return 0;
41 oldsize = pc->size;
42 mincount = (mincount+511)&(~511);
43 if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
44 newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
45 else
46 newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
47
48 if (!newldt)
49 return -ENOMEM;
50
51 if (oldsize)
52 memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
53 oldldt = pc->ldt;
54 memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
55 wmb();
56 pc->ldt = newldt;
57 wmb();
58 pc->size = mincount;
59 wmb();
60 if (reload) {
61#ifdef CONFIG_SMP
62 cpumask_t mask;
63
64 preempt_disable();
65 mask = cpumask_of_cpu(smp_processor_id());
66 load_LDT(pc);
67 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
68 smp_call_function(flush_ldt, NULL, 1, 1);
69 preempt_enable();
70#else
71 load_LDT(pc);
72#endif
73 }
74 if (oldsize) {
75 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
76 vfree(oldldt);
77 else
78 kfree(oldldt);
79 }
80 return 0;
81}
82
83static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
84{
85 int err = alloc_ldt(new, old->size, 0);
86 if (err < 0)
87 return err;
88 memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
89 return 0;
90}
91
92/*
93 * we do not have to muck with descriptors here, that is
94 * done in switch_mm() as needed.
95 */
96int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
97{
98 struct mm_struct * old_mm;
99 int retval = 0;
100
101 init_MUTEX(&mm->context.sem);
102 mm->context.size = 0;
103 old_mm = current->mm;
104 if (old_mm && old_mm->context.size > 0) {
105 down(&old_mm->context.sem);
106 retval = copy_ldt(&mm->context, &old_mm->context);
107 up(&old_mm->context.sem);
108 }
109 return retval;
110}
111
112/*
113 *
114 * Don't touch the LDT register - we're already in the next thread.
115 */
116void destroy_context(struct mm_struct *mm)
117{
118 if (mm->context.size) {
119 if ((unsigned)mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
120 vfree(mm->context.ldt);
121 else
122 kfree(mm->context.ldt);
123 mm->context.size = 0;
124 }
125}
126
127static int read_ldt(void __user * ptr, unsigned long bytecount)
128{
129 int err;
130 unsigned long size;
131 struct mm_struct * mm = current->mm;
132
133 if (!mm->context.size)
134 return 0;
135 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
136 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
137
138 down(&mm->context.sem);
139 size = mm->context.size*LDT_ENTRY_SIZE;
140 if (size > bytecount)
141 size = bytecount;
142
143 err = 0;
144 if (copy_to_user(ptr, mm->context.ldt, size))
145 err = -EFAULT;
146 up(&mm->context.sem);
147 if (err < 0)
148 goto error_return;
149 if (size != bytecount) {
150 /* zero-fill the rest */
151 if (clear_user(ptr+size, bytecount-size) != 0) {
152 err = -EFAULT;
153 goto error_return;
154 }
155 }
156 return bytecount;
157error_return:
158 return err;
159}
160
161static int read_default_ldt(void __user * ptr, unsigned long bytecount)
162{
163 /* Arbitrary number */
164 /* x86-64 default LDT is all zeros */
165 if (bytecount > 128)
166 bytecount = 128;
167 if (clear_user(ptr, bytecount))
168 return -EFAULT;
169 return bytecount;
170}
171
172static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
173{
174 struct task_struct *me = current;
175 struct mm_struct * mm = me->mm;
176 __u32 entry_1, entry_2, *lp;
177 int error;
178 struct user_desc ldt_info;
179
180 error = -EINVAL;
181
182 if (bytecount != sizeof(ldt_info))
183 goto out;
184 error = -EFAULT;
185 if (copy_from_user(&ldt_info, ptr, bytecount))
186 goto out;
187
188 error = -EINVAL;
189 if (ldt_info.entry_number >= LDT_ENTRIES)
190 goto out;
191 if (ldt_info.contents == 3) {
192 if (oldmode)
193 goto out;
194 if (ldt_info.seg_not_present == 0)
195 goto out;
196 }
197
198 down(&mm->context.sem);
199 if (ldt_info.entry_number >= (unsigned)mm->context.size) {
200 error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
201 if (error < 0)
202 goto out_unlock;
203 }
204
205 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
206
207 /* Allow LDTs to be cleared by the user. */
208 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
209 if (oldmode || LDT_empty(&ldt_info)) {
210 entry_1 = 0;
211 entry_2 = 0;
212 goto install;
213 }
214 }
215
216 entry_1 = LDT_entry_a(&ldt_info);
217 entry_2 = LDT_entry_b(&ldt_info);
218 if (oldmode)
219 entry_2 &= ~(1 << 20);
220
221 /* Install the new entry ... */
222install:
223 *lp = entry_1;
224 *(lp+1) = entry_2;
225 error = 0;
226
227out_unlock:
228 up(&mm->context.sem);
229out:
230 return error;
231}
232
233asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
234{
235 int ret = -ENOSYS;
236
237 switch (func) {
238 case 0:
239 ret = read_ldt(ptr, bytecount);
240 break;
241 case 1:
242 ret = write_ldt(ptr, bytecount, 1);
243 break;
244 case 2:
245 ret = read_default_ldt(ptr, bytecount);
246 break;
247 case 0x11:
248 ret = write_ldt(ptr, bytecount, 0);
249 break;
250 }
251 return ret;
252}