[PATCH] Fix atomicity of TIF update in flush_thread() for x86_64
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86_64 / kernel / ldt.c
blobd7e5d0cf4285f0ce4326c139e526536a00af1ef6
1 /*
2 * linux/arch/x86_64/kernel/ldt.c
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 */
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>
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>
26 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
27 static void flush_ldt(void *null)
29 if (current->active_mm)
30 load_LDT(&current->active_mm->context);
32 #endif
34 static int alloc_ldt(mm_context_t *pc, unsigned mincount, int reload)
36 void *oldldt;
37 void *newldt;
38 unsigned oldsize;
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);
49 if (!newldt)
50 return -ENOMEM;
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;
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
75 if (oldsize) {
76 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
77 vfree(oldldt);
78 else
79 kfree(oldldt);
81 return 0;
84 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
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;
94 * we do not have to muck with descriptors here, that is
95 * done in switch_mm() as needed.
97 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
99 struct mm_struct * old_mm;
100 int retval = 0;
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);
110 return retval;
115 * Don't touch the LDT register - we're already in the next thread.
117 void destroy_context(struct mm_struct *mm)
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;
128 static int read_ldt(void __user * ptr, unsigned long bytecount)
130 int err;
131 unsigned long size;
132 struct mm_struct * mm = current->mm;
134 if (!mm->context.size)
135 return 0;
136 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
137 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
139 down(&mm->context.sem);
140 size = mm->context.size*LDT_ENTRY_SIZE;
141 if (size > bytecount)
142 size = bytecount;
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;
157 return bytecount;
158 error_return:
159 return err;
162 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
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;
173 static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
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;
181 error = -EINVAL;
183 if (bytecount != sizeof(ldt_info))
184 goto out;
185 error = -EFAULT;
186 if (copy_from_user(&ldt_info, ptr, bytecount))
187 goto out;
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;
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;
206 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
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;
217 entry_1 = LDT_entry_a(&ldt_info);
218 entry_2 = LDT_entry_b(&ldt_info);
219 if (oldmode)
220 entry_2 &= ~(1 << 20);
222 /* Install the new entry ... */
223 install:
224 *lp = entry_1;
225 *(lp+1) = entry_2;
226 error = 0;
228 out_unlock:
229 up(&mm->context.sem);
230 out:
231 return error;
234 asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
236 int ret = -ENOSYS;
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;
252 return ret;