fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / x86_64 / kernel / ldt-xen.c
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 #include <asm/pgalloc.h>
26
27 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
28 static void flush_ldt(void *null)
29 {
30         if (current->active_mm)
31                load_LDT(&current->active_mm->context);
32 }
33 #endif
34
35 static int alloc_ldt(mm_context_t *pc, unsigned mincount, int reload)
36 {
37         void *oldldt;
38         void *newldt;
39         unsigned oldsize;
40
41         if (mincount <= (unsigned)pc->size)
42                 return 0;
43         oldsize = pc->size;
44         mincount = (mincount+511)&(~511);
45         if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
46                 newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
47         else
48                 newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
49
50         if (!newldt)
51                 return -ENOMEM;
52
53         if (oldsize)
54                 memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
55         oldldt = pc->ldt;
56         memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
57         wmb();
58         pc->ldt = newldt;
59         wmb();
60         pc->size = mincount;
61         wmb();
62         if (reload) {
63 #ifdef CONFIG_SMP
64                 cpumask_t mask;
65
66                 preempt_disable();
67                 mask = cpumask_of_cpu(smp_processor_id());
68 #endif
69                 make_pages_readonly(
70                         pc->ldt,
71                         (pc->size * LDT_ENTRY_SIZE) / PAGE_SIZE,
72                         XENFEAT_writable_descriptor_tables);
73                 load_LDT(pc);
74 #ifdef CONFIG_SMP
75                 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
76                         smp_call_function(flush_ldt, NULL, 1, 1);
77                 preempt_enable();
78 #endif
79         }
80         if (oldsize) {
81                 make_pages_writable(
82                         oldldt,
83                         (oldsize * LDT_ENTRY_SIZE) / PAGE_SIZE,
84                         XENFEAT_writable_descriptor_tables);
85                 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
86                         vfree(oldldt);
87                 else
88                         kfree(oldldt);
89         }
90         return 0;
91 }
92
93 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
94 {
95         int err = alloc_ldt(new, old->size, 0);
96         if (err < 0)
97                 return err;
98         memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
99         make_pages_readonly(
100                 new->ldt,
101                 (new->size * LDT_ENTRY_SIZE) / PAGE_SIZE,
102                 XENFEAT_writable_descriptor_tables);
103         return 0;
104 }
105
106 /*
107  * we do not have to muck with descriptors here, that is
108  * done in switch_mm() as needed.
109  */
110 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
111 {
112         struct mm_struct * old_mm;
113         int retval = 0;
114
115         memset(&mm->context, 0, sizeof(mm->context));
116         init_MUTEX(&mm->context.sem);
117         mm->context.size = 0;
118         old_mm = current->mm;
119         if (old_mm && old_mm->context.size > 0) {
120                 down(&old_mm->context.sem);
121                 retval = copy_ldt(&mm->context, &old_mm->context);
122                 up(&old_mm->context.sem);
123         }
124         if (retval == 0) {
125                 spin_lock(&mm_unpinned_lock);
126                 list_add(&mm->context.unpinned, &mm_unpinned);
127                 spin_unlock(&mm_unpinned_lock);
128         }
129         return retval;
130 }
131
132 /*
133  * 
134  * Don't touch the LDT register - we're already in the next thread.
135  */
136 void destroy_context(struct mm_struct *mm)
137 {
138         if (mm->context.size) {
139                 if (mm == current->active_mm)
140                         clear_LDT();
141                 make_pages_writable(
142                         mm->context.ldt,
143                         (mm->context.size * LDT_ENTRY_SIZE) / PAGE_SIZE,
144                         XENFEAT_writable_descriptor_tables);
145                 if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
146                         vfree(mm->context.ldt);
147                 else
148                         kfree(mm->context.ldt);
149                 mm->context.size = 0;
150         }
151         if (!mm->context.pinned) {
152                 spin_lock(&mm_unpinned_lock);
153                 list_del(&mm->context.unpinned);
154                 spin_unlock(&mm_unpinned_lock);
155         }
156 }
157
158 static int read_ldt(void __user * ptr, unsigned long bytecount)
159 {
160         int err;
161         unsigned long size;
162         struct mm_struct * mm = current->mm;
163
164         if (!mm->context.size)
165                 return 0;
166         if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
167                 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
168
169         down(&mm->context.sem);
170         size = mm->context.size*LDT_ENTRY_SIZE;
171         if (size > bytecount)
172                 size = bytecount;
173
174         err = 0;
175         if (copy_to_user(ptr, mm->context.ldt, size))
176                 err = -EFAULT;
177         up(&mm->context.sem);
178         if (err < 0)
179                 goto error_return;
180         if (size != bytecount) {
181                 /* zero-fill the rest */
182                 if (clear_user(ptr+size, bytecount-size) != 0) {
183                         err = -EFAULT;
184                         goto error_return;
185                 }
186         }
187         return bytecount;
188 error_return:
189         return err;
190 }
191
192 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
193 {
194         /* Arbitrary number */ 
195         /* x86-64 default LDT is all zeros */
196         if (bytecount > 128) 
197                 bytecount = 128;        
198         if (clear_user(ptr, bytecount))
199                 return -EFAULT;
200         return bytecount; 
201 }
202
203 static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
204 {
205         struct task_struct *me = current;
206         struct mm_struct * mm = me->mm;
207         __u32 entry_1, entry_2, *lp;
208         unsigned long mach_lp;
209         int error;
210         struct user_desc ldt_info;
211
212         error = -EINVAL;
213
214         if (bytecount != sizeof(ldt_info))
215                 goto out;
216         error = -EFAULT;        
217         if (copy_from_user(&ldt_info, ptr, bytecount))
218                 goto out;
219
220         error = -EINVAL;
221         if (ldt_info.entry_number >= LDT_ENTRIES)
222                 goto out;
223         if (ldt_info.contents == 3) {
224                 if (oldmode)
225                         goto out;
226                 if (ldt_info.seg_not_present == 0)
227                         goto out;
228         }
229
230         down(&mm->context.sem);
231         if (ldt_info.entry_number >= (unsigned)mm->context.size) {
232                 error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
233                 if (error < 0)
234                         goto out_unlock;
235         }
236
237         lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
238         mach_lp = arbitrary_virt_to_machine(lp);
239
240         /* Allow LDTs to be cleared by the user. */
241         if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
242                 if (oldmode || LDT_empty(&ldt_info)) {
243                         entry_1 = 0;
244                         entry_2 = 0;
245                         goto install;
246                 }
247         }
248
249         entry_1 = LDT_entry_a(&ldt_info);
250         entry_2 = LDT_entry_b(&ldt_info);
251         if (oldmode)
252                 entry_2 &= ~(1 << 20);
253
254         /* Install the new entry ...  */
255 install:
256         error = HYPERVISOR_update_descriptor(mach_lp, (unsigned long)((entry_1 | (unsigned long) entry_2 << 32)));
257
258 out_unlock:
259         up(&mm->context.sem);
260 out:
261         return error;
262 }
263
264 asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
265 {
266         int ret = -ENOSYS;
267
268         switch (func) {
269         case 0:
270                 ret = read_ldt(ptr, bytecount);
271                 break;
272         case 1:
273                 ret = write_ldt(ptr, bytecount, 1);
274                 break;
275         case 2:
276                 ret = read_default_ldt(ptr, bytecount);
277                 break;
278         case 0x11:
279                 ret = write_ldt(ptr, bytecount, 0);
280                 break;
281         }
282         return ret;
283 }