2 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/syscalls.h>
10 #include <linux/uaccess.h>
11 #include <asm/unistd.h>
14 #include <sysdep/tls.h>
16 static inline int modify_ldt (int func, void *ptr, unsigned long bytecount)
18 return syscall(__NR_modify_ldt, func, ptr, bytecount);
21 static long write_ldt_entry(struct mm_id *mm_idp, int func,
22 struct user_desc *desc, void **addr, int done)
27 BUILD_BUG_ON(sizeof(*desc) % sizeof(long));
29 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
30 sizeof(*desc) / sizeof(long),
33 unsigned long args[] = { func,
34 (unsigned long)stub_addr,
37 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
45 * In skas mode, we hold our own ldt data in UML.
46 * Thus, the code implementing sys_modify_ldt_skas
47 * is very similar to (and mostly stolen from) sys_modify_ldt
48 * for arch/i386/kernel/ldt.c
49 * The routines copied and modified in part are:
53 * - sys_modify_ldt_skas
56 static int read_ldt(void __user * ptr, unsigned long bytecount)
60 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
62 if (!ldt->entry_count)
64 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
65 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
68 mutex_lock(&ldt->lock);
69 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
70 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
73 if (copy_to_user(ptr, ldt->u.entries, size))
79 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
84 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
92 mutex_unlock(&ldt->lock);
94 if (bytecount == 0 || err == -EFAULT)
97 if (clear_user(ptr, bytecount))
104 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
108 if (bytecount > 5*LDT_ENTRY_SIZE)
109 bytecount = 5*LDT_ENTRY_SIZE;
113 * UML doesn't support lcall7 and lcall27.
114 * So, we don't really have a default ldt, but emulate
115 * an empty ldt of common host default ldt size.
117 if (clear_user(ptr, bytecount))
123 static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
125 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
126 struct mm_id * mm_idp = ¤t->mm->context.id;
128 struct user_desc ldt_info;
129 struct ldt_entry entry0, *ldt_p;
133 if (bytecount != sizeof(ldt_info))
136 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
140 if (ldt_info.entry_number >= LDT_ENTRIES)
142 if (ldt_info.contents == 3) {
145 if (ldt_info.seg_not_present == 0)
149 mutex_lock(&ldt->lock);
151 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
155 if (ldt_info.entry_number >= ldt->entry_count &&
156 ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
157 for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
158 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
161 memcpy(&entry0, ldt->u.entries,
163 ldt->u.pages[i] = (struct ldt_entry *)
164 __get_free_page(GFP_KERNEL|__GFP_ZERO);
165 if (!ldt->u.pages[i]) {
167 /* Undo the change in host */
168 memset(&ldt_info, 0, sizeof(ldt_info));
169 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
173 memcpy(ldt->u.pages[0], &entry0,
175 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
176 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
178 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
181 if (ldt->entry_count <= ldt_info.entry_number)
182 ldt->entry_count = ldt_info.entry_number + 1;
184 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
185 ldt_p = ldt->u.entries + ldt_info.entry_number;
187 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
188 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
190 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
191 (func == 1 || LDT_empty(&ldt_info))) {
197 ldt_info.useable = 0;
198 ldt_p->a = LDT_entry_a(&ldt_info);
199 ldt_p->b = LDT_entry_b(&ldt_info);
204 mutex_unlock(&ldt->lock);
209 static long do_modify_ldt_skas(int func, void __user *ptr,
210 unsigned long bytecount)
216 ret = read_ldt(ptr, bytecount);
220 ret = write_ldt(ptr, bytecount, func);
223 ret = read_default_ldt(ptr, bytecount);
229 static DEFINE_SPINLOCK(host_ldt_lock);
230 static short dummy_list[9] = {0, -1};
231 static short * host_ldt_entries = NULL;
233 static void ldt_get_host_info(void)
236 struct ldt_entry * ldt;
238 int i, size, k, order;
240 spin_lock(&host_ldt_lock);
242 if (host_ldt_entries != NULL) {
243 spin_unlock(&host_ldt_lock);
246 host_ldt_entries = dummy_list+1;
248 spin_unlock(&host_ldt_lock);
250 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
253 ldt = (struct ldt_entry *)
254 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
256 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
261 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
263 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
267 /* default_ldt is active, simply write an empty entry 0 */
268 host_ldt_entries = dummy_list;
272 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
273 if (ldt[i].a != 0 || ldt[i].b != 0)
277 if (size < ARRAY_SIZE(dummy_list))
278 host_ldt_entries = dummy_list;
280 size = (size + 1) * sizeof(dummy_list[0]);
281 tmp = kmalloc(size, GFP_KERNEL);
283 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
287 host_ldt_entries = tmp;
290 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
291 if (ldt[i].a != 0 || ldt[i].b != 0)
292 host_ldt_entries[k++] = i;
294 host_ldt_entries[k] = -1;
297 free_pages((unsigned long)ldt, order);
300 long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
302 struct user_desc desc;
309 mutex_init(&new_mm->arch.ldt.lock);
312 memset(&desc, 0, sizeof(desc));
314 * Now we try to retrieve info about the ldt, we
315 * inherited from the host. All ldt-entries found
316 * will be reset in the following loop
319 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
320 desc.entry_number = *num_p;
321 err = write_ldt_entry(&new_mm->id, 1, &desc,
322 &addr, *(num_p + 1) == -1);
326 new_mm->arch.ldt.entry_count = 0;
332 * Our local LDT is used to supply the data for
333 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
334 * i.e., we have to use the stub for modify_ldt, which
335 * can't handle the big read buffer of up to 64kB.
337 mutex_lock(&from_mm->arch.ldt.lock);
338 if (from_mm->arch.ldt.entry_count <= LDT_DIRECT_ENTRIES)
339 memcpy(new_mm->arch.ldt.u.entries, from_mm->arch.ldt.u.entries,
340 sizeof(new_mm->arch.ldt.u.entries));
342 i = from_mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
344 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
349 new_mm->arch.ldt.u.pages[i] =
350 (struct ldt_entry *) page;
351 memcpy(new_mm->arch.ldt.u.pages[i],
352 from_mm->arch.ldt.u.pages[i], PAGE_SIZE);
355 new_mm->arch.ldt.entry_count = from_mm->arch.ldt.entry_count;
356 mutex_unlock(&from_mm->arch.ldt.lock);
363 void free_ldt(struct mm_context *mm)
367 if (mm->arch.ldt.entry_count > LDT_DIRECT_ENTRIES) {
368 i = mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
370 free_page((long) mm->arch.ldt.u.pages[i]);
372 mm->arch.ldt.entry_count = 0;
375 SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
376 unsigned long , bytecount)
378 /* See non-um modify_ldt() for why we do this cast */
379 return (unsigned int)do_modify_ldt_skas(func, ptr, bytecount);