]> err.no Git - linux-2.6/blob - arch/x86/kernel/ldt_32.c
x86: clean up arch/x86/kernel/ldt_32/64.c
[linux-2.6] / arch / x86 / kernel / ldt_32.c
1 /*
2  * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4  */
5
6 #include <linux/errno.h>
7 #include <linux/sched.h>
8 #include <linux/string.h>
9 #include <linux/mm.h>
10 #include <linux/smp.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/ldt.h>
17 #include <asm/desc.h>
18 #include <asm/mmu_context.h>
19
20 #ifdef CONFIG_SMP
21 static void flush_ldt(void *null)
22 {
23         if (current->active_mm)
24                 load_LDT(&current->active_mm->context);
25 }
26 #endif
27
28 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
29 {
30         void *oldldt;
31         void *newldt;
32         int oldsize;
33
34         if (mincount <= pc->size)
35                 return 0;
36         oldsize = pc->size;
37         mincount = (mincount + 511) & (~511);
38         if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
39                 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
40         else
41                 newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL);
42
43         if (!newldt)
44                 return -ENOMEM;
45
46         if (oldsize)
47                 memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
48         oldldt = pc->ldt;
49         memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
50                (mincount - oldsize) * LDT_ENTRY_SIZE);
51         pc->ldt = newldt;
52         wmb();
53         pc->size = mincount;
54         wmb();
55
56         if (reload) {
57 #ifdef CONFIG_SMP
58                 cpumask_t mask;
59
60                 preempt_disable();
61                 load_LDT(pc);
62                 mask = cpumask_of_cpu(smp_processor_id());
63                 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
64                         smp_call_function(flush_ldt, NULL, 1, 1);
65                 preempt_enable();
66 #else
67                 load_LDT(pc);
68 #endif
69         }
70         if (oldsize) {
71                 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
72                         vfree(oldldt);
73                 else
74                         kfree(oldldt);
75         }
76         return 0;
77 }
78
79 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
80 {
81         int err = alloc_ldt(new, old->size, 0);
82
83         if (err < 0)
84                 return err;
85         memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
86         return 0;
87 }
88
89 /*
90  * we do not have to muck with descriptors here, that is
91  * done in switch_mm() as needed.
92  */
93 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
94 {
95         struct mm_struct *old_mm;
96         int retval = 0;
97
98         mutex_init(&mm->context.lock);
99         mm->context.size = 0;
100         old_mm = current->mm;
101         if (old_mm && old_mm->context.size > 0) {
102                 mutex_lock(&old_mm->context.lock);
103                 retval = copy_ldt(&mm->context, &old_mm->context);
104                 mutex_unlock(&old_mm->context.lock);
105         }
106         return retval;
107 }
108
109 /*
110  * No need to lock the MM as we are the last user
111  */
112 void destroy_context(struct mm_struct *mm)
113 {
114         if (mm->context.size) {
115                 if (mm == current->active_mm)
116                         clear_LDT();
117                 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
118                         vfree(mm->context.ldt);
119                 else
120                         kfree(mm->context.ldt);
121                 mm->context.size = 0;
122         }
123 }
124
125 static int read_ldt(void __user *ptr, unsigned long bytecount)
126 {
127         int err;
128         unsigned long size;
129         struct mm_struct *mm = current->mm;
130
131         if (!mm->context.size)
132                 return 0;
133         if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
134                 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
135
136         mutex_lock(&mm->context.lock);
137         size = mm->context.size * LDT_ENTRY_SIZE;
138         if (size > bytecount)
139                 size = bytecount;
140
141         err = 0;
142         if (copy_to_user(ptr, mm->context.ldt, size))
143                 err = -EFAULT;
144         mutex_unlock(&mm->context.lock);
145         if (err < 0)
146                 goto error_return;
147         if (size != bytecount) {
148                 /* zero-fill the rest */
149                 if (clear_user(ptr + size, bytecount - size) != 0) {
150                         err = -EFAULT;
151                         goto error_return;
152                 }
153         }
154         return bytecount;
155 error_return:
156         return err;
157 }
158
159 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
160 {
161         int err;
162         unsigned long size;
163
164         err = 0;
165         size = 5 * sizeof(struct desc_struct);
166         if (size > bytecount)
167                 size = bytecount;
168
169         err = size;
170         if (clear_user(ptr, size))
171                 err = -EFAULT;
172
173         return err;
174 }
175
176 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
177 {
178         struct mm_struct *mm = current->mm;
179         __u32 entry_1, entry_2;
180         int error;
181         struct user_desc ldt_info;
182
183         error = -EINVAL;
184         if (bytecount != sizeof(ldt_info))
185                 goto out;
186         error = -EFAULT;
187         if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
188                 goto out;
189
190         error = -EINVAL;
191         if (ldt_info.entry_number >= LDT_ENTRIES)
192                 goto out;
193         if (ldt_info.contents == 3) {
194                 if (oldmode)
195                         goto out;
196                 if (ldt_info.seg_not_present == 0)
197                         goto out;
198         }
199
200         mutex_lock(&mm->context.lock);
201         if (ldt_info.entry_number >= mm->context.size) {
202                 error = alloc_ldt(&current->mm->context,
203                                   ldt_info.entry_number + 1, 1);
204                 if (error < 0)
205                         goto out_unlock;
206         }
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 ...  */
223 install:
224         write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1,
225                         entry_2);
226         error = 0;
227
228 out_unlock:
229         mutex_unlock(&mm->context.lock);
230 out:
231         return error;
232 }
233
234 asmlinkage int sys_modify_ldt(int func, void __user *ptr,
235                               unsigned long bytecount)
236 {
237         int ret = -ENOSYS;
238
239         switch (func) {
240         case 0:
241                 ret = read_ldt(ptr, bytecount);
242                 break;
243         case 1:
244                 ret = write_ldt(ptr, bytecount, 1);
245                 break;
246         case 2:
247                 ret = read_default_ldt(ptr, bytecount);
248                 break;
249         case 0x11:
250                 ret = write_ldt(ptr, bytecount, 0);
251                 break;
252         }
253         return ret;
254 }