blob: 95903938e7ad4e37a626311b652f15eb4a5514a5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4 * Copyright (C) 2002 Andi Kleen
Thomas Gleixner78aa1f62008-01-30 13:30:13 +01005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * This handles calls from both 32bit and 64bit mode.
7 */
8
9#include <linux/errno.h>
10#include <linux/sched.h>
11#include <linux/string.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/vmalloc.h>
15#include <linux/slab.h>
16
17#include <asm/uaccess.h>
18#include <asm/system.h>
19#include <asm/ldt.h>
20#include <asm/desc.h>
Thomas Gleixner70f50882008-01-30 13:30:13 +010021#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010023#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static void flush_ldt(void *null)
25{
26 if (current->active_mm)
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010027 load_LDT(&current->active_mm->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29#endif
30
Thomas Gleixner70f50882008-01-30 13:30:13 +010031static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Thomas Gleixner70f50882008-01-30 13:30:13 +010033 void *oldldt, *newldt;
34 int oldsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Thomas Gleixner70f50882008-01-30 13:30:13 +010036 if (mincount <= pc->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return 0;
38 oldsize = pc->size;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010039 mincount = (mincount + 511) & (~511);
40 if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
41 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 else
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010043 newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (!newldt)
46 return -ENOMEM;
47
48 if (oldsize)
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010049 memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 oldldt = pc->ldt;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010051 memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
52 (mincount - oldsize) * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 wmb();
54 pc->ldt = newldt;
55 wmb();
56 pc->size = mincount;
57 wmb();
Thomas Gleixner70f50882008-01-30 13:30:13 +010058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (reload) {
60#ifdef CONFIG_SMP
61 cpumask_t mask;
62
63 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 load_LDT(pc);
Thomas Gleixner70f50882008-01-30 13:30:13 +010065 mask = cpumask_of_cpu(smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
67 smp_call_function(flush_ldt, NULL, 1, 1);
68 preempt_enable();
69#else
70 load_LDT(pc);
71#endif
72 }
73 if (oldsize) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010074 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 vfree(oldldt);
76 else
77 kfree(oldldt);
78 }
79 return 0;
80}
81
82static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
83{
84 int err = alloc_ldt(new, old->size, 0);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (err < 0)
87 return err;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010088 memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return 0;
90}
91
92/*
93 * we do not have to muck with descriptors here, that is
94 * done in switch_mm() as needed.
95 */
96int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
97{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010098 struct mm_struct *old_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int retval = 0;
100
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200101 mutex_init(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 mm->context.size = 0;
103 old_mm = current->mm;
104 if (old_mm && old_mm->context.size > 0) {
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200105 mutex_lock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 retval = copy_ldt(&mm->context, &old_mm->context);
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200107 mutex_unlock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 return retval;
110}
111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Don't touch the LDT register - we're already in the next thread.
114 */
115void destroy_context(struct mm_struct *mm)
116{
117 if (mm->context.size) {
Thomas Gleixner70f50882008-01-30 13:30:13 +0100118 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 vfree(mm->context.ldt);
120 else
121 kfree(mm->context.ldt);
122 mm->context.size = 0;
123 }
124}
125
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100126static int read_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int err;
129 unsigned long size;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100130 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (!mm->context.size)
133 return 0;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100134 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
135 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200137 mutex_lock(&mm->context.lock);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100138 size = mm->context.size * LDT_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (size > bytecount)
140 size = bytecount;
141
142 err = 0;
143 if (copy_to_user(ptr, mm->context.ldt, size))
144 err = -EFAULT;
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200145 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (err < 0)
147 goto error_return;
148 if (size != bytecount) {
149 /* zero-fill the rest */
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100150 if (clear_user(ptr + size, bytecount - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 err = -EFAULT;
152 goto error_return;
153 }
154 }
155 return bytecount;
156error_return:
157 return err;
158}
159
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100160static int read_default_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100162 /* Arbitrary number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* x86-64 default LDT is all zeros */
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100164 if (bytecount > 128)
165 bytecount = 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (clear_user(ptr, bytecount))
167 return -EFAULT;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100168 return bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100171static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Thomas Gleixner70f50882008-01-30 13:30:13 +0100173 struct mm_struct *mm = current->mm;
Thomas Gleixnerfc2d6252008-01-30 13:30:13 +0100174 __u32 entry_1, entry_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int error;
176 struct user_desc ldt_info;
177
178 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (bytecount != sizeof(ldt_info))
180 goto out;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100181 error = -EFAULT;
Thomas Gleixner70f50882008-01-30 13:30:13 +0100182 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto out;
184
185 error = -EINVAL;
186 if (ldt_info.entry_number >= LDT_ENTRIES)
187 goto out;
188 if (ldt_info.contents == 3) {
189 if (oldmode)
190 goto out;
191 if (ldt_info.seg_not_present == 0)
192 goto out;
193 }
194
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200195 mutex_lock(&mm->context.lock);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100196 if (ldt_info.entry_number >= mm->context.size) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100197 error = alloc_ldt(&current->mm->context,
198 ldt_info.entry_number + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (error < 0)
200 goto out_unlock;
201 }
202
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100203 /* Allow LDTs to be cleared by the user. */
204 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (oldmode || LDT_empty(&ldt_info)) {
206 entry_1 = 0;
207 entry_2 = 0;
208 goto install;
209 }
210 }
211
212 entry_1 = LDT_entry_a(&ldt_info);
213 entry_2 = LDT_entry_b(&ldt_info);
214 if (oldmode)
215 entry_2 &= ~(1 << 20);
216
217 /* Install the new entry ... */
218install:
Thomas Gleixnerfc2d6252008-01-30 13:30:13 +0100219 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1,
220 entry_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 error = 0;
222
223out_unlock:
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200224 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225out:
226 return error;
227}
228
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100229asmlinkage int sys_modify_ldt(int func, void __user *ptr,
230 unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int ret = -ENOSYS;
233
234 switch (func) {
235 case 0:
236 ret = read_ldt(ptr, bytecount);
237 break;
238 case 1:
239 ret = write_ldt(ptr, bytecount, 1);
240 break;
241 case 2:
242 ret = read_default_ldt(ptr, bytecount);
243 break;
244 case 0x11:
245 ret = write_ldt(ptr, bytecount, 0);
246 break;
247 }
248 return ret;
249}