ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/ldt.c @ 8401:8ebcbcda3d37

Add and use write_ldt_entry, as seen in later linux kernel versions.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author chris@ubuntu.eng.hq.xensource.com
date Thu Dec 15 16:17:13 2005 -0700 (2005-12-15)
parents 25599e222c33
children 4be553bcfb0f
line source
1 /*
2 * linux/kernel/ldt.c
3 *
4 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
5 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
6 */
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/string.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/vmalloc.h>
15 #include <linux/slab.h>
17 #include <asm/uaccess.h>
18 #include <asm/system.h>
19 #include <asm/ldt.h>
20 #include <asm/desc.h>
22 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
23 static void flush_ldt(void *null)
24 {
25 if (current->active_mm)
26 load_LDT(&current->active_mm->context);
27 }
28 #endif
30 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
31 {
32 void *oldldt;
33 void *newldt;
34 int oldsize;
36 if (mincount <= pc->size)
37 return 0;
38 oldsize = pc->size;
39 mincount = (mincount+511)&(~511);
40 if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
41 newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
42 else
43 newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
45 if (!newldt)
46 return -ENOMEM;
48 if (oldsize)
49 memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
50 oldldt = pc->ldt;
51 memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
52 pc->ldt = newldt;
53 wmb();
54 pc->size = mincount;
55 wmb();
57 if (reload) {
58 #ifdef CONFIG_SMP
59 cpumask_t mask;
60 preempt_disable();
61 #endif
62 make_pages_readonly(pc->ldt, (pc->size * LDT_ENTRY_SIZE) /
63 PAGE_SIZE);
64 load_LDT(pc);
65 #ifdef CONFIG_SMP
66 mask = cpumask_of_cpu(smp_processor_id());
67 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
68 smp_call_function(flush_ldt, NULL, 1, 1);
69 preempt_enable();
70 #endif
71 }
72 if (oldsize) {
73 make_pages_writable(oldldt, (oldsize * LDT_ENTRY_SIZE) /
74 PAGE_SIZE);
75 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
76 vfree(oldldt);
77 else
78 kfree(oldldt);
79 }
80 return 0;
81 }
83 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
84 {
85 int err = alloc_ldt(new, old->size, 0);
86 if (err < 0)
87 return err;
88 memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
89 make_pages_readonly(new->ldt, (new->size * LDT_ENTRY_SIZE) /
90 PAGE_SIZE);
91 return 0;
92 }
94 /*
95 * we do not have to muck with descriptors here, that is
96 * done in switch_mm() as needed.
97 */
98 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
99 {
100 struct mm_struct * old_mm;
101 int retval = 0;
103 init_MUTEX(&mm->context.sem);
104 mm->context.size = 0;
105 old_mm = current->mm;
106 if (old_mm && old_mm->context.size > 0) {
107 down(&old_mm->context.sem);
108 retval = copy_ldt(&mm->context, &old_mm->context);
109 up(&old_mm->context.sem);
110 }
111 return retval;
112 }
114 /*
115 * No need to lock the MM as we are the last user
116 */
117 void destroy_context(struct mm_struct *mm)
118 {
119 if (mm->context.size) {
120 if (mm == current->active_mm)
121 clear_LDT();
122 make_pages_writable(mm->context.ldt,
123 (mm->context.size * LDT_ENTRY_SIZE) /
124 PAGE_SIZE);
125 if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
126 vfree(mm->context.ldt);
127 else
128 kfree(mm->context.ldt);
129 mm->context.size = 0;
130 }
131 }
133 static int read_ldt(void __user * ptr, unsigned long bytecount)
134 {
135 int err;
136 unsigned long size;
137 struct mm_struct * mm = current->mm;
139 if (!mm->context.size)
140 return 0;
141 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
142 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
144 down(&mm->context.sem);
145 size = mm->context.size*LDT_ENTRY_SIZE;
146 if (size > bytecount)
147 size = bytecount;
149 err = 0;
150 if (copy_to_user(ptr, mm->context.ldt, size))
151 err = -EFAULT;
152 up(&mm->context.sem);
153 if (err < 0)
154 goto error_return;
155 if (size != bytecount) {
156 /* zero-fill the rest */
157 if (clear_user(ptr+size, bytecount-size) != 0) {
158 err = -EFAULT;
159 goto error_return;
160 }
161 }
162 return bytecount;
163 error_return:
164 return err;
165 }
167 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
168 {
169 int err;
170 unsigned long size;
171 void *address;
173 err = 0;
174 address = &default_ldt[0];
175 size = 5*sizeof(struct desc_struct);
176 if (size > bytecount)
177 size = bytecount;
179 err = size;
180 if (copy_to_user(ptr, address, size))
181 err = -EFAULT;
183 return err;
184 }
186 static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
187 {
188 struct mm_struct * mm = current->mm;
189 __u32 entry_1, entry_2;
190 int error;
191 struct user_desc ldt_info;
193 error = -EINVAL;
194 if (bytecount != sizeof(ldt_info))
195 goto out;
196 error = -EFAULT;
197 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
198 goto out;
200 error = -EINVAL;
201 if (ldt_info.entry_number >= LDT_ENTRIES)
202 goto out;
203 if (ldt_info.contents == 3) {
204 if (oldmode)
205 goto out;
206 if (ldt_info.seg_not_present == 0)
207 goto out;
208 }
210 down(&mm->context.sem);
211 if (ldt_info.entry_number >= mm->context.size) {
212 error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
213 if (error < 0)
214 goto out_unlock;
215 }
217 /* Allow LDTs to be cleared by the user. */
218 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
219 if (oldmode || LDT_empty(&ldt_info)) {
220 entry_1 = 0;
221 entry_2 = 0;
222 goto install;
223 }
224 }
226 entry_1 = LDT_entry_a(&ldt_info);
227 entry_2 = LDT_entry_b(&ldt_info);
228 if (oldmode)
229 entry_2 &= ~(1 << 20);
231 /* Install the new entry ... */
232 install:
233 error = write_ldt_entry(mm->context.ldt, ldt_info.entry_number,
234 entry_1, entry_2);
236 out_unlock:
237 up(&mm->context.sem);
238 out:
239 return error;
240 }
242 asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
243 {
244 int ret = -ENOSYS;
246 switch (func) {
247 case 0:
248 ret = read_ldt(ptr, bytecount);
249 break;
250 case 1:
251 ret = write_ldt(ptr, bytecount, 1);
252 break;
253 case 2:
254 ret = read_default_ldt(ptr, bytecount);
255 break;
256 case 0x11:
257 ret = write_ldt(ptr, bytecount, 0);
258 break;
259 }
260 return ret;
261 }