2 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4 * Copyright (C) 2002 Andi Kleen
6 * This handles calls from both 32bit and 64bit mode.
9 #include <linux/errno.h>
10 #include <linux/gfp.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
14 #include <linux/smp.h>
15 #include <linux/vmalloc.h>
16 #include <linux/uaccess.h>
20 #include <asm/mmu_context.h>
21 #include <asm/syscalls.h>
26 static void flush_ldt(void *current_mm)
28 if (current->active_mm == current_mm)
29 load_LDT(¤t->active_mm->context);
33 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
35 void *oldldt, *newldt;
38 if (mincount <= pc->size)
41 mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
42 (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
43 if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
44 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
46 newldt = (void *)__get_free_page(GFP_KERNEL);
52 memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
54 memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
55 (mincount - oldsize) * LDT_ENTRY_SIZE);
57 paravirt_alloc_ldt(newldt, mincount);
60 /* CHECKME: Do we really need this ? */
72 if (!cpumask_equal(mm_cpumask(current->mm),
73 cpumask_of(smp_processor_id())))
74 smp_call_function(flush_ldt, current->mm, 1);
81 paravirt_free_ldt(oldldt, oldsize);
82 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
85 put_page(virt_to_page(oldldt));
90 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
92 int err = alloc_ldt(new, old->size, 0);
98 for (i = 0; i < old->size; i++)
99 write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE);
104 * we do not have to muck with descriptors here, that is
105 * done in switch_mm() as needed.
107 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
109 struct mm_struct *old_mm;
112 mutex_init(&mm->context.lock);
113 mm->context.size = 0;
114 old_mm = current->mm;
115 if (old_mm && old_mm->context.size > 0) {
116 mutex_lock(&old_mm->context.lock);
117 retval = copy_ldt(&mm->context, &old_mm->context);
118 mutex_unlock(&old_mm->context.lock);
124 * No need to lock the MM as we are the last user
126 * 64bit: Don't touch the LDT register - we're already in the next thread.
128 void destroy_context(struct mm_struct *mm)
130 if (mm->context.size) {
132 /* CHECKME: Can this ever happen ? */
133 if (mm == current->active_mm)
136 paravirt_free_ldt(mm->context.ldt, mm->context.size);
137 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
138 vfree(mm->context.ldt);
140 put_page(virt_to_page(mm->context.ldt));
141 mm->context.size = 0;
145 static int read_ldt(void __user *ptr, unsigned long bytecount)
149 struct mm_struct *mm = current->mm;
151 if (!mm->context.size)
153 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
154 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
156 mutex_lock(&mm->context.lock);
157 size = mm->context.size * LDT_ENTRY_SIZE;
158 if (size > bytecount)
162 if (copy_to_user(ptr, mm->context.ldt, size))
164 mutex_unlock(&mm->context.lock);
167 if (size != bytecount) {
168 /* zero-fill the rest */
169 if (clear_user(ptr + size, bytecount - size) != 0) {
179 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
181 /* CHECKME: Can we use _one_ random number ? */
183 unsigned long size = 5 * sizeof(struct desc_struct);
185 unsigned long size = 128;
187 if (bytecount > size)
189 if (clear_user(ptr, bytecount))
194 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
196 struct mm_struct *mm = current->mm;
197 struct desc_struct ldt;
199 struct user_desc ldt_info;
202 if (bytecount != sizeof(ldt_info))
205 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
209 if (ldt_info.entry_number >= LDT_ENTRIES)
211 if (ldt_info.contents == 3) {
214 if (ldt_info.seg_not_present == 0)
218 mutex_lock(&mm->context.lock);
219 if (ldt_info.entry_number >= mm->context.size) {
220 error = alloc_ldt(¤t->mm->context,
221 ldt_info.entry_number + 1, 1);
226 /* Allow LDTs to be cleared by the user. */
227 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
228 if (oldmode || LDT_empty(&ldt_info)) {
229 memset(&ldt, 0, sizeof(ldt));
235 * On x86-64 we do not support 16-bit segments due to
236 * IRET leaking the high bits of the kernel stack address.
239 if (!ldt_info.seg_32bit && !sysctl_ldt16) {
245 fill_ldt(&ldt, &ldt_info);
249 /* Install the new entry ... */
251 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
255 mutex_unlock(&mm->context.lock);
260 asmlinkage int sys_modify_ldt(int func, void __user *ptr,
261 unsigned long bytecount)
267 ret = read_ldt(ptr, bytecount);
270 ret = write_ldt(ptr, bytecount, 1);
273 ret = read_default_ldt(ptr, bytecount);
276 ret = write_ldt(ptr, bytecount, 0);