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/sched.h>
11 #include <linux/string.h>
13 #include <linux/smp.h>
14 #include <linux/vmalloc.h>
15 #include <linux/slab.h>
17 #include <asm/uaccess.h>
18 #include <asm/system.h>
21 #include <asm/mmu_context.h>
24 static void flush_ldt(void *null)
26 if (current->active_mm)
27 load_LDT(¤t->active_mm->context);
31 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
33 void *oldldt, *newldt;
36 if (mincount <= pc->size)
39 mincount = (mincount + 511) & (~511);
40 if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
41 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
43 newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL);
49 memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
51 memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
52 (mincount - oldsize) * LDT_ENTRY_SIZE);
65 mask = cpumask_of_cpu(smp_processor_id());
66 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
67 smp_call_function(flush_ldt, NULL, 1, 1);
74 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
82 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
84 int err = alloc_ldt(new, old->size, 0);
88 memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
93 * we do not have to muck with descriptors here, that is
94 * done in switch_mm() as needed.
96 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
98 struct mm_struct *old_mm;
101 mutex_init(&mm->context.lock);
102 mm->context.size = 0;
103 old_mm = current->mm;
104 if (old_mm && old_mm->context.size > 0) {
105 mutex_lock(&old_mm->context.lock);
106 retval = copy_ldt(&mm->context, &old_mm->context);
107 mutex_unlock(&old_mm->context.lock);
113 * Don't touch the LDT register - we're already in the next thread.
115 void destroy_context(struct mm_struct *mm)
117 if (mm->context.size) {
118 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
119 vfree(mm->context.ldt);
121 kfree(mm->context.ldt);
122 mm->context.size = 0;
126 static int read_ldt(void __user *ptr, unsigned long bytecount)
130 struct mm_struct *mm = current->mm;
132 if (!mm->context.size)
134 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
135 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
137 mutex_lock(&mm->context.lock);
138 size = mm->context.size * LDT_ENTRY_SIZE;
139 if (size > bytecount)
143 if (copy_to_user(ptr, mm->context.ldt, size))
145 mutex_unlock(&mm->context.lock);
148 if (size != bytecount) {
149 /* zero-fill the rest */
150 if (clear_user(ptr + size, bytecount - size) != 0) {
160 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
162 /* Arbitrary number */
163 /* x86-64 default LDT is all zeros */
166 if (clear_user(ptr, bytecount))
171 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
173 struct mm_struct *mm = current->mm;
174 __u32 entry_1, entry_2;
176 struct user_desc ldt_info;
179 if (bytecount != sizeof(ldt_info))
182 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
186 if (ldt_info.entry_number >= LDT_ENTRIES)
188 if (ldt_info.contents == 3) {
191 if (ldt_info.seg_not_present == 0)
195 mutex_lock(&mm->context.lock);
196 if (ldt_info.entry_number >= mm->context.size) {
197 error = alloc_ldt(¤t->mm->context,
198 ldt_info.entry_number + 1, 1);
203 /* Allow LDTs to be cleared by the user. */
204 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
205 if (oldmode || LDT_empty(&ldt_info)) {
212 entry_1 = LDT_entry_a(&ldt_info);
213 entry_2 = LDT_entry_b(&ldt_info);
215 entry_2 &= ~(1 << 20);
217 /* Install the new entry ... */
219 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1,
224 mutex_unlock(&mm->context.lock);
229 asmlinkage int sys_modify_ldt(int func, void __user *ptr,
230 unsigned long bytecount)
236 ret = read_ldt(ptr, bytecount);
239 ret = write_ldt(ptr, bytecount, 1);
242 ret = read_default_ldt(ptr, bytecount);
245 ret = write_ldt(ptr, bytecount, 0);