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/uaccess.h>
10 #include <asm/unistd.h>
13 #include <sysdep/tls.h>
15 extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
17 static long write_ldt_entry(struct mm_id *mm_idp, int func,
18 struct user_desc *desc, void **addr, int done)
22 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
23 (sizeof(*desc) + sizeof(long) - 1) &
27 unsigned long args[] = { func,
28 (unsigned long)stub_addr,
31 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
39 * In skas mode, we hold our own ldt data in UML.
40 * Thus, the code implementing sys_modify_ldt_skas
41 * is very similar to (and mostly stolen from) sys_modify_ldt
42 * for arch/i386/kernel/ldt.c
43 * The routines copied and modified in part are:
47 * - sys_modify_ldt_skas
50 static int read_ldt(void __user * ptr, unsigned long bytecount)
54 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
56 if (!ldt->entry_count)
58 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
59 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
62 mutex_lock(&ldt->lock);
63 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
64 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
67 if (copy_to_user(ptr, ldt->u.entries, size))
73 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
78 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
86 mutex_unlock(&ldt->lock);
88 if (bytecount == 0 || err == -EFAULT)
91 if (clear_user(ptr, bytecount))
98 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
102 if (bytecount > 5*LDT_ENTRY_SIZE)
103 bytecount = 5*LDT_ENTRY_SIZE;
107 * UML doesn't support lcall7 and lcall27.
108 * So, we don't really have a default ldt, but emulate
109 * an empty ldt of common host default ldt size.
111 if (clear_user(ptr, bytecount))
117 static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
119 uml_ldt_t *ldt = ¤t->mm->context.arch.ldt;
120 struct mm_id * mm_idp = ¤t->mm->context.id;
122 struct user_desc ldt_info;
123 struct ldt_entry entry0, *ldt_p;
127 if (bytecount != sizeof(ldt_info))
130 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
134 if (ldt_info.entry_number >= LDT_ENTRIES)
136 if (ldt_info.contents == 3) {
139 if (ldt_info.seg_not_present == 0)
143 mutex_lock(&ldt->lock);
145 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
149 if (ldt_info.entry_number >= ldt->entry_count &&
150 ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
151 for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
152 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
155 memcpy(&entry0, ldt->u.entries,
157 ldt->u.pages[i] = (struct ldt_entry *)
158 __get_free_page(GFP_KERNEL|__GFP_ZERO);
159 if (!ldt->u.pages[i]) {
161 /* Undo the change in host */
162 memset(&ldt_info, 0, sizeof(ldt_info));
163 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
167 memcpy(ldt->u.pages[0], &entry0,
169 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
170 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
172 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
175 if (ldt->entry_count <= ldt_info.entry_number)
176 ldt->entry_count = ldt_info.entry_number + 1;
178 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
179 ldt_p = ldt->u.entries + ldt_info.entry_number;
181 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
182 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
184 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
185 (func == 1 || LDT_empty(&ldt_info))) {
191 ldt_info.useable = 0;
192 ldt_p->a = LDT_entry_a(&ldt_info);
193 ldt_p->b = LDT_entry_b(&ldt_info);
198 mutex_unlock(&ldt->lock);
203 static long do_modify_ldt_skas(int func, void __user *ptr,
204 unsigned long bytecount)
210 ret = read_ldt(ptr, bytecount);
214 ret = write_ldt(ptr, bytecount, func);
217 ret = read_default_ldt(ptr, bytecount);
223 static DEFINE_SPINLOCK(host_ldt_lock);
224 static short dummy_list[9] = {0, -1};
225 static short * host_ldt_entries = NULL;
227 static void ldt_get_host_info(void)
230 struct ldt_entry * ldt;
232 int i, size, k, order;
234 spin_lock(&host_ldt_lock);
236 if (host_ldt_entries != NULL) {
237 spin_unlock(&host_ldt_lock);
240 host_ldt_entries = dummy_list+1;
242 spin_unlock(&host_ldt_lock);
244 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
247 ldt = (struct ldt_entry *)
248 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
250 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
255 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
257 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
261 /* default_ldt is active, simply write an empty entry 0 */
262 host_ldt_entries = dummy_list;
266 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
267 if (ldt[i].a != 0 || ldt[i].b != 0)
271 if (size < ARRAY_SIZE(dummy_list))
272 host_ldt_entries = dummy_list;
274 size = (size + 1) * sizeof(dummy_list[0]);
275 tmp = kmalloc(size, GFP_KERNEL);
277 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
281 host_ldt_entries = tmp;
284 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
285 if (ldt[i].a != 0 || ldt[i].b != 0)
286 host_ldt_entries[k++] = i;
288 host_ldt_entries[k] = -1;
291 free_pages((unsigned long)ldt, order);
294 long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
296 struct user_desc desc;
303 mutex_init(&new_mm->arch.ldt.lock);
306 memset(&desc, 0, sizeof(desc));
308 * Now we try to retrieve info about the ldt, we
309 * inherited from the host. All ldt-entries found
310 * will be reset in the following loop
313 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
314 desc.entry_number = *num_p;
315 err = write_ldt_entry(&new_mm->id, 1, &desc,
316 &addr, *(num_p + 1) == -1);
320 new_mm->arch.ldt.entry_count = 0;
326 * Our local LDT is used to supply the data for
327 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
328 * i.e., we have to use the stub for modify_ldt, which
329 * can't handle the big read buffer of up to 64kB.
331 mutex_lock(&from_mm->arch.ldt.lock);
332 if (from_mm->arch.ldt.entry_count <= LDT_DIRECT_ENTRIES)
333 memcpy(new_mm->arch.ldt.u.entries, from_mm->arch.ldt.u.entries,
334 sizeof(new_mm->arch.ldt.u.entries));
336 i = from_mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
338 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
343 new_mm->arch.ldt.u.pages[i] =
344 (struct ldt_entry *) page;
345 memcpy(new_mm->arch.ldt.u.pages[i],
346 from_mm->arch.ldt.u.pages[i], PAGE_SIZE);
349 new_mm->arch.ldt.entry_count = from_mm->arch.ldt.entry_count;
350 mutex_unlock(&from_mm->arch.ldt.lock);
357 void free_ldt(struct mm_context *mm)
361 if (mm->arch.ldt.entry_count > LDT_DIRECT_ENTRIES) {
362 i = mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
364 free_page((long) mm->arch.ldt.u.pages[i]);
366 mm->arch.ldt.entry_count = 0;
369 int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
371 return do_modify_ldt_skas(func, ptr, bytecount);