ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/kernel/ldt.c @ 6294:1a0723cd37f1

Fix many uses of machine addresses in XenLinux. Primarily
this fixes users of virt_to_machine/machine_to_virt to
use virt_to_mfn/mfn_to_virt where that is more appropriate.

This should be a big step to improved PAE stability.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Aug 19 16:06:43 2005 +0000 (2005-08-19)
parents 56a63f9f378f
children f51fe43c5d1c 5f4724c13040 3a8f27c6d56c
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>
21 #include <asm/mmu_context.h>
23 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
24 static void flush_ldt(void *null)
25 {
26 if (current->active_mm)
27 load_LDT(&current->active_mm->context);
28 }
29 #endif
31 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
32 {
33 void *oldldt;
34 void *newldt;
35 int oldsize;
37 if (mincount <= pc->size)
38 return 0;
39 oldsize = pc->size;
40 mincount = (mincount+511)&(~511);
41 if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
42 newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
43 else
44 newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
46 if (!newldt)
47 return -ENOMEM;
49 if (oldsize)
50 memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
51 oldldt = pc->ldt;
52 memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
53 pc->ldt = newldt;
54 wmb();
55 pc->size = mincount;
56 wmb();
58 if (reload) {
59 #ifdef CONFIG_SMP
60 cpumask_t mask;
61 preempt_disable();
62 #endif
63 make_pages_readonly(pc->ldt, (pc->size * LDT_ENTRY_SIZE) /
64 PAGE_SIZE);
65 load_LDT(pc);
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, NULL, 1, 1);
70 preempt_enable();
71 #endif
72 }
73 if (oldsize) {
74 make_pages_writable(oldldt, (oldsize * LDT_ENTRY_SIZE) /
75 PAGE_SIZE);
76 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
77 vfree(oldldt);
78 else
79 kfree(oldldt);
80 }
81 return 0;
82 }
84 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
85 {
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 make_pages_readonly(new->ldt, (new->size * LDT_ENTRY_SIZE) /
91 PAGE_SIZE);
92 return 0;
93 }
95 /*
96 * we do not have to muck with descriptors here, that is
97 * done in switch_mm() as needed.
98 */
99 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
100 {
101 struct mm_struct * old_mm;
102 int retval = 0;
104 memset(&mm->context, 0, sizeof(mm->context));
105 init_MUTEX(&mm->context.sem);
106 old_mm = current->mm;
107 if (old_mm && old_mm->context.size > 0) {
108 down(&old_mm->context.sem);
109 retval = copy_ldt(&mm->context, &old_mm->context);
110 up(&old_mm->context.sem);
111 }
112 if (retval == 0) {
113 spin_lock(&mm_unpinned_lock);
114 list_add(&mm->context.unpinned, &mm_unpinned);
115 spin_unlock(&mm_unpinned_lock);
116 }
117 return retval;
118 }
120 /*
121 * No need to lock the MM as we are the last user
122 */
123 void destroy_context(struct mm_struct *mm)
124 {
125 if (mm->context.size) {
126 if (mm == current->active_mm)
127 clear_LDT();
128 make_pages_writable(mm->context.ldt,
129 (mm->context.size * LDT_ENTRY_SIZE) /
130 PAGE_SIZE);
131 if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
132 vfree(mm->context.ldt);
133 else
134 kfree(mm->context.ldt);
135 mm->context.size = 0;
136 }
137 if (!mm->context.pinned) {
138 spin_lock(&mm_unpinned_lock);
139 list_del(&mm->context.unpinned);
140 spin_unlock(&mm_unpinned_lock);
141 }
142 }
144 static int read_ldt(void __user * ptr, unsigned long bytecount)
145 {
146 int err;
147 unsigned long size;
148 struct mm_struct * mm = current->mm;
150 if (!mm->context.size)
151 return 0;
152 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
153 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
155 down(&mm->context.sem);
156 size = mm->context.size*LDT_ENTRY_SIZE;
157 if (size > bytecount)
158 size = bytecount;
160 err = 0;
161 if (copy_to_user(ptr, mm->context.ldt, size))
162 err = -EFAULT;
163 up(&mm->context.sem);
164 if (err < 0)
165 goto error_return;
166 if (size != bytecount) {
167 /* zero-fill the rest */
168 if (clear_user(ptr+size, bytecount-size) != 0) {
169 err = -EFAULT;
170 goto error_return;
171 }
172 }
173 return bytecount;
174 error_return:
175 return err;
176 }
178 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
179 {
180 int err;
181 unsigned long size;
182 void *address;
184 err = 0;
185 address = &default_ldt[0];
186 size = 5*sizeof(struct desc_struct);
187 if (size > bytecount)
188 size = bytecount;
190 err = size;
191 if (copy_to_user(ptr, address, size))
192 err = -EFAULT;
194 return err;
195 }
197 static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
198 {
199 struct mm_struct * mm = current->mm;
200 __u32 entry_1, entry_2, *lp;
201 maddr_t mach_lp;
202 int error;
203 struct user_desc ldt_info;
205 error = -EINVAL;
206 if (bytecount != sizeof(ldt_info))
207 goto out;
208 error = -EFAULT;
209 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
210 goto out;
212 error = -EINVAL;
213 if (ldt_info.entry_number >= LDT_ENTRIES)
214 goto out;
215 if (ldt_info.contents == 3) {
216 if (oldmode)
217 goto out;
218 if (ldt_info.seg_not_present == 0)
219 goto out;
220 }
222 down(&mm->context.sem);
223 if (ldt_info.entry_number >= mm->context.size) {
224 error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
225 if (error < 0)
226 goto out_unlock;
227 }
229 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
230 mach_lp = arbitrary_virt_to_machine(lp);
232 /* Allow LDTs to be cleared by the user. */
233 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
234 if (oldmode || LDT_empty(&ldt_info)) {
235 entry_1 = 0;
236 entry_2 = 0;
237 goto install;
238 }
239 }
241 entry_1 = LDT_entry_a(&ldt_info);
242 entry_2 = LDT_entry_b(&ldt_info);
243 if (oldmode)
244 entry_2 &= ~(1 << 20);
246 /* Install the new entry ... */
247 install:
248 error = HYPERVISOR_update_descriptor(
249 mach_lp, (u64)entry_1 | ((u64)entry_2<<32));
251 out_unlock:
252 up(&mm->context.sem);
253 out:
254 return error;
255 }
257 asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
258 {
259 int ret = -ENOSYS;
261 switch (func) {
262 case 0:
263 ret = read_ldt(ptr, bytecount);
264 break;
265 case 1:
266 ret = write_ldt(ptr, bytecount, 1);
267 break;
268 case 2:
269 ret = read_default_ldt(ptr, bytecount);
270 break;
271 case 0x11:
272 ret = write_ldt(ptr, bytecount, 0);
273 break;
274 }
275 return ret;
276 }