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