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>
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
20 #include <asm/mmu_context.h>
23 static void flush_ldt(void *current_mm)
25 if (current->active_mm == current_mm)
26 load_LDT(¤t->active_mm->context);
30 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
32 void *oldldt, *newldt;
35 if (mincount <= pc->size)
38 mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
39 (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
40 if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
41 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
43 newldt = (void *)__get_free_page(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);
54 paravirt_alloc_ldt(newldt, mincount);
57 /* CHECKME: Do we really need this ? */
69 if (!cpus_equal(current->mm->cpu_vm_mask,
70 cpumask_of_cpu(smp_processor_id())))
71 smp_call_function(flush_ldt, current->mm, 1);
78 paravirt_free_ldt(oldldt, oldsize);
79 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
82 put_page(virt_to_page(oldldt));
87 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
89 int err = alloc_ldt(new, old->size, 0);
95 for(i = 0; i < old->size; i++)
96 write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE);
101 * we do not have to muck with descriptors here, that is
102 * done in switch_mm() as needed.
104 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
106 struct mm_struct *old_mm;
109 mutex_init(&mm->context.lock);
110 mm->context.size = 0;
111 old_mm = current->mm;
112 if (old_mm && old_mm->context.size > 0) {
113 mutex_lock(&old_mm->context.lock);
114 retval = copy_ldt(&mm->context, &old_mm->context);
115 mutex_unlock(&old_mm->context.lock);
121 * No need to lock the MM as we are the last user
123 * 64bit: Don't touch the LDT register - we're already in the next thread.
125 void destroy_context(struct mm_struct *mm)
127 if (mm->context.size) {
129 /* CHECKME: Can this ever happen ? */
130 if (mm == current->active_mm)
133 paravirt_free_ldt(mm->context.ldt, mm->context.size);
134 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
135 vfree(mm->context.ldt);
137 put_page(virt_to_page(mm->context.ldt));
138 mm->context.size = 0;
142 static int read_ldt(void __user *ptr, unsigned long bytecount)
146 struct mm_struct *mm = current->mm;
148 if (!mm->context.size)
150 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
151 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
153 mutex_lock(&mm->context.lock);
154 size = mm->context.size * LDT_ENTRY_SIZE;
155 if (size > bytecount)
159 if (copy_to_user(ptr, mm->context.ldt, size))
161 mutex_unlock(&mm->context.lock);
164 if (size != bytecount) {
165 /* zero-fill the rest */
166 if (clear_user(ptr + size, bytecount - size) != 0) {
176 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
178 /* CHECKME: Can we use _one_ random number ? */
180 unsigned long size = 5 * sizeof(struct desc_struct);
182 unsigned long size = 128;
184 if (bytecount > size)
186 if (clear_user(ptr, bytecount))
191 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
193 struct mm_struct *mm = current->mm;
194 struct desc_struct ldt;
196 struct user_desc ldt_info;
199 if (bytecount != sizeof(ldt_info))
202 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
206 if (ldt_info.entry_number >= LDT_ENTRIES)
208 if (ldt_info.contents == 3) {
211 if (ldt_info.seg_not_present == 0)
215 mutex_lock(&mm->context.lock);
216 if (ldt_info.entry_number >= mm->context.size) {
217 error = alloc_ldt(¤t->mm->context,
218 ldt_info.entry_number + 1, 1);
223 /* Allow LDTs to be cleared by the user. */
224 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
225 if (oldmode || LDT_empty(&ldt_info)) {
226 memset(&ldt, 0, sizeof(ldt));
231 fill_ldt(&ldt, &ldt_info);
235 /* Install the new entry ... */
237 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
241 mutex_unlock(&mm->context.lock);
246 asmlinkage int sys_modify_ldt(int func, void __user *ptr,
247 unsigned long bytecount)
253 ret = read_ldt(ptr, bytecount);
256 ret = write_ldt(ptr, bytecount, 1);
259 ret = read_default_ldt(ptr, bytecount);
262 ret = write_ldt(ptr, bytecount, 0);