]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/ldt.c
Merge branch 'linus' into x86/xen
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / ldt.c
1 /*
2  * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4  * Copyright (C) 2002 Andi Kleen
5  *
6  * This handles calls from both 32bit and 64bit mode.
7  */
8
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/string.h>
12 #include <linux/mm.h>
13 #include <linux/smp.h>
14 #include <linux/vmalloc.h>
15
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <asm/ldt.h>
19 #include <asm/desc.h>
20 #include <asm/mmu_context.h>
21
22 #ifdef CONFIG_SMP
23 static void flush_ldt(void *current_mm)
24 {
25         if (current->active_mm == current_mm)
26                 load_LDT(&current->active_mm->context);
27 }
28 #endif
29
30 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
31 {
32         void *oldldt, *newldt;
33         int oldsize;
34
35         if (mincount <= pc->size)
36                 return 0;
37         oldsize = pc->size;
38         mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
39                         (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
40         if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
41                 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
42         else
43                 newldt = (void *)__get_free_page(GFP_KERNEL);
44
45         if (!newldt)
46                 return -ENOMEM;
47
48         if (oldsize)
49                 memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
50         oldldt = pc->ldt;
51         memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
52                (mincount - oldsize) * LDT_ENTRY_SIZE);
53
54         paravirt_alloc_ldt(newldt, mincount);
55
56 #ifdef CONFIG_X86_64
57         /* CHECKME: Do we really need this ? */
58         wmb();
59 #endif
60         pc->ldt = newldt;
61         wmb();
62         pc->size = mincount;
63         wmb();
64
65         if (reload) {
66 #ifdef CONFIG_SMP
67                 preempt_disable();
68                 load_LDT(pc);
69                 if (!cpus_equal(current->mm->cpu_vm_mask,
70                                 cpumask_of_cpu(smp_processor_id())))
71                         smp_call_function(flush_ldt, current->mm, 1);
72                 preempt_enable();
73 #else
74                 load_LDT(pc);
75 #endif
76         }
77         if (oldsize) {
78                 paravirt_free_ldt(oldldt, oldsize);
79                 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
80                         vfree(oldldt);
81                 else
82                         put_page(virt_to_page(oldldt));
83         }
84         return 0;
85 }
86
87 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
88 {
89         int err = alloc_ldt(new, old->size, 0);
90         int i;
91
92         if (err < 0)
93                 return err;
94
95         for(i = 0; i < old->size; i++)
96                 write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE);
97         return 0;
98 }
99
100 /*
101  * we do not have to muck with descriptors here, that is
102  * done in switch_mm() as needed.
103  */
104 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
105 {
106         struct mm_struct *old_mm;
107         int retval = 0;
108
109         mutex_init(&mm->context.lock);
110         mm->context.size = 0;
111         old_mm = current->mm;
112         if (old_mm && old_mm->context.size > 0) {
113                 mutex_lock(&old_mm->context.lock);
114                 retval = copy_ldt(&mm->context, &old_mm->context);
115                 mutex_unlock(&old_mm->context.lock);
116         }
117         return retval;
118 }
119
120 /*
121  * No need to lock the MM as we are the last user
122  *
123  * 64bit: Don't touch the LDT register - we're already in the next thread.
124  */
125 void destroy_context(struct mm_struct *mm)
126 {
127         if (mm->context.size) {
128 #ifdef CONFIG_X86_32
129                 /* CHECKME: Can this ever happen ? */
130                 if (mm == current->active_mm)
131                         clear_LDT();
132 #endif
133                 paravirt_free_ldt(mm->context.ldt, mm->context.size);
134                 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
135                         vfree(mm->context.ldt);
136                 else
137                         put_page(virt_to_page(mm->context.ldt));
138                 mm->context.size = 0;
139         }
140 }
141
142 static int read_ldt(void __user *ptr, unsigned long bytecount)
143 {
144         int err;
145         unsigned long size;
146         struct mm_struct *mm = current->mm;
147
148         if (!mm->context.size)
149                 return 0;
150         if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
151                 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
152
153         mutex_lock(&mm->context.lock);
154         size = mm->context.size * LDT_ENTRY_SIZE;
155         if (size > bytecount)
156                 size = bytecount;
157
158         err = 0;
159         if (copy_to_user(ptr, mm->context.ldt, size))
160                 err = -EFAULT;
161         mutex_unlock(&mm->context.lock);
162         if (err < 0)
163                 goto error_return;
164         if (size != bytecount) {
165                 /* zero-fill the rest */
166                 if (clear_user(ptr + size, bytecount - size) != 0) {
167                         err = -EFAULT;
168                         goto error_return;
169                 }
170         }
171         return bytecount;
172 error_return:
173         return err;
174 }
175
176 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
177 {
178         /* CHECKME: Can we use _one_ random number ? */
179 #ifdef CONFIG_X86_32
180         unsigned long size = 5 * sizeof(struct desc_struct);
181 #else
182         unsigned long size = 128;
183 #endif
184         if (bytecount > size)
185                 bytecount = size;
186         if (clear_user(ptr, bytecount))
187                 return -EFAULT;
188         return bytecount;
189 }
190
191 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
192 {
193         struct mm_struct *mm = current->mm;
194         struct desc_struct ldt;
195         int error;
196         struct user_desc ldt_info;
197
198         error = -EINVAL;
199         if (bytecount != sizeof(ldt_info))
200                 goto out;
201         error = -EFAULT;
202         if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
203                 goto out;
204
205         error = -EINVAL;
206         if (ldt_info.entry_number >= LDT_ENTRIES)
207                 goto out;
208         if (ldt_info.contents == 3) {
209                 if (oldmode)
210                         goto out;
211                 if (ldt_info.seg_not_present == 0)
212                         goto out;
213         }
214
215         mutex_lock(&mm->context.lock);
216         if (ldt_info.entry_number >= mm->context.size) {
217                 error = alloc_ldt(&current->mm->context,
218                                   ldt_info.entry_number + 1, 1);
219                 if (error < 0)
220                         goto out_unlock;
221         }
222
223         /* Allow LDTs to be cleared by the user. */
224         if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
225                 if (oldmode || LDT_empty(&ldt_info)) {
226                         memset(&ldt, 0, sizeof(ldt));
227                         goto install;
228                 }
229         }
230
231         fill_ldt(&ldt, &ldt_info);
232         if (oldmode)
233                 ldt.avl = 0;
234
235         /* Install the new entry ...  */
236 install:
237         write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
238         error = 0;
239
240 out_unlock:
241         mutex_unlock(&mm->context.lock);
242 out:
243         return error;
244 }
245
246 asmlinkage int sys_modify_ldt(int func, void __user *ptr,
247                               unsigned long bytecount)
248 {
249         int ret = -ENOSYS;
250
251         switch (func) {
252         case 0:
253                 ret = read_ldt(ptr, bytecount);
254                 break;
255         case 1:
256                 ret = write_ldt(ptr, bytecount, 1);
257                 break;
258         case 2:
259                 ret = read_default_ldt(ptr, bytecount);
260                 break;
261         case 0x11:
262                 ret = write_ldt(ptr, bytecount, 0);
263                 break;
264         }
265         return ret;
266 }