ia64/xen-unstable

view linux-2.6.7-xen-sparse/arch/xen/i386/kernel/ldt.c @ 2074:5c45177abff2

bitkeeper revision 1.1139.1.1 (4110b72fhtHFIf4Ul13KpCx5pOkJ7w)

Make pages used for LDT writable when they get freed.
Fixes ltp thread creation test failure.
author cl349@freefall.cl.cam.ac.uk
date Wed Aug 04 10:15:11 2004 +0000 (2004-08-04)
parents 0366608703aa
children a3e585a0c6ca
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 flush_page_update_queue();
66 #ifdef CONFIG_SMP
67 mask = cpumask_of_cpu(smp_processor_id());
68 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
69 smp_call_function(flush_ldt, 0, 1, 1);
70 preempt_enable();
71 #endif
72 }
73 if (oldsize) {
74 make_pages_writeable(oldldt, (oldsize * LDT_ENTRY_SIZE) /
75 PAGE_SIZE);
76 flush_page_update_queue();
77 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
78 vfree(oldldt);
79 else
80 kfree(oldldt);
81 }
82 return 0;
83 }
85 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
86 {
87 int err = alloc_ldt(new, old->size, 0);
88 if (err < 0)
89 return err;
90 memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
91 make_pages_readonly(new->ldt, (new->size * LDT_ENTRY_SIZE) /
92 PAGE_SIZE);
93 return 0;
94 }
96 /*
97 * we do not have to muck with descriptors here, that is
98 * done in switch_mm() as needed.
99 */
100 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
101 {
102 struct mm_struct * old_mm;
103 int retval = 0;
105 init_MUTEX(&mm->context.sem);
106 mm->context.size = 0;
107 old_mm = current->mm;
108 if (old_mm && old_mm->context.size > 0) {
109 down(&old_mm->context.sem);
110 retval = copy_ldt(&mm->context, &old_mm->context);
111 up(&old_mm->context.sem);
112 }
113 return retval;
114 }
116 /*
117 * No need to lock the MM as we are the last user
118 */
119 void destroy_context(struct mm_struct *mm)
120 {
121 if (mm->context.size) {
122 if (mm == current->active_mm)
123 clear_LDT();
124 make_pages_writeable(mm->context.ldt,
125 (mm->context.size * LDT_ENTRY_SIZE) /
126 PAGE_SIZE);
127 flush_page_update_queue();
128 if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
129 vfree(mm->context.ldt);
130 else
131 kfree(mm->context.ldt);
132 mm->context.size = 0;
133 }
134 }
136 static int read_ldt(void __user * ptr, unsigned long bytecount)
137 {
138 int err;
139 unsigned long size;
140 struct mm_struct * mm = current->mm;
142 if (!mm->context.size)
143 return 0;
144 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
145 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
147 down(&mm->context.sem);
148 size = mm->context.size*LDT_ENTRY_SIZE;
149 if (size > bytecount)
150 size = bytecount;
152 err = 0;
153 if (copy_to_user(ptr, mm->context.ldt, size))
154 err = -EFAULT;
155 up(&mm->context.sem);
156 if (err < 0)
157 return err;
158 if (size != bytecount) {
159 /* zero-fill the rest */
160 clear_user(ptr+size, bytecount-size);
161 }
162 return bytecount;
163 }
165 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
166 {
167 int err;
168 unsigned long size;
169 void *address;
171 err = 0;
172 address = &default_ldt[0];
173 size = 5*sizeof(struct desc_struct);
174 if (size > bytecount)
175 size = bytecount;
177 err = size;
178 if (copy_to_user(ptr, address, size))
179 err = -EFAULT;
181 return err;
182 }
184 static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
185 {
186 struct mm_struct * mm = current->mm;
187 __u32 entry_1, entry_2, *lp;
188 unsigned long phys_lp;
189 int error;
190 struct user_desc ldt_info;
192 error = -EINVAL;
193 if (bytecount != sizeof(ldt_info))
194 goto out;
195 error = -EFAULT;
196 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
197 goto out;
199 error = -EINVAL;
200 if (ldt_info.entry_number >= LDT_ENTRIES)
201 goto out;
202 if (ldt_info.contents == 3) {
203 if (oldmode)
204 goto out;
205 if (ldt_info.seg_not_present == 0)
206 goto out;
207 }
209 down(&mm->context.sem);
210 if (ldt_info.entry_number >= mm->context.size) {
211 error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
212 if (error < 0)
213 goto out_unlock;
214 }
216 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
217 phys_lp = arbitrary_virt_to_phys(lp);
219 /* Allow LDTs to be cleared by the user. */
220 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
221 if (oldmode || LDT_empty(&ldt_info)) {
222 entry_1 = 0;
223 entry_2 = 0;
224 goto install;
225 }
226 }
228 entry_1 = LDT_entry_a(&ldt_info);
229 entry_2 = LDT_entry_b(&ldt_info);
230 if (oldmode)
231 entry_2 &= ~(1 << 20);
233 /* Install the new entry ... */
234 install:
235 error = HYPERVISOR_update_descriptor(phys_lp, entry_1, entry_2);
237 out_unlock:
238 up(&mm->context.sem);
239 out:
240 return error;
241 }
243 asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
244 {
245 int ret = -ENOSYS;
247 switch (func) {
248 case 0:
249 ret = read_ldt(ptr, bytecount);
250 break;
251 case 1:
252 ret = write_ldt(ptr, bytecount, 1);
253 break;
254 case 2:
255 ret = read_default_ldt(ptr, bytecount);
256 break;
257 case 0x11:
258 ret = write_ldt(ptr, bytecount, 0);
259 break;
260 }
261 return ret;
262 }