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