blob: b68e21f06f4f84615baf2fbc47179fd437bea321 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/uaccess.h>
17#include <asm/system.h>
18#include <asm/ldt.h>
19#include <asm/desc.h>
Thomas Gleixner70f50882008-01-30 13:30:13 +010020#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010022#ifdef CONFIG_SMP
Jan Beulichb4784582008-05-12 15:44:39 +020023static void flush_ldt(void *current_mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Jan Beulichb4784582008-05-12 15:44:39 +020025 if (current->active_mm == current_mm)
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010026 load_LDT(&current->active_mm->context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28#endif
29
Thomas Gleixner70f50882008-01-30 13:30:13 +010030static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Thomas Gleixner70f50882008-01-30 13:30:13 +010032 void *oldldt, *newldt;
33 int oldsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Gleixner70f50882008-01-30 13:30:13 +010035 if (mincount <= pc->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return 0;
37 oldsize = pc->size;
Cyrill Gorcunovfa0c8642008-02-04 16:48:03 +010038 mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
39 (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010040 if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
41 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +010043 newldt = (void *)__get_free_page(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);
Thomas Gleixner77e463d2008-01-30 13:30:14 +010053
54#ifdef CONFIG_X86_64
55 /* CHECKME: Do we really need this ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 wmb();
Thomas Gleixner77e463d2008-01-30 13:30:14 +010057#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 pc->ldt = newldt;
59 wmb();
60 pc->size = mincount;
61 wmb();
Thomas Gleixner70f50882008-01-30 13:30:13 +010062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (reload) {
64#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 load_LDT(pc);
Mike Travis0bc3cc02008-07-24 18:21:31 -070067 if (!cpus_equal(current->mm->cpu_vm_mask,
68 cpumask_of_cpu(smp_processor_id())))
Ingo Molnar1a781a72008-07-15 21:55:59 +020069 smp_call_function(flush_ldt, current->mm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 preempt_enable();
71#else
72 load_LDT(pc);
73#endif
74 }
75 if (oldsize) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010076 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 vfree(oldldt);
78 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +010079 put_page(virt_to_page(oldldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81 return 0;
82}
83
84static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
85{
86 int err = alloc_ldt(new, old->size, 0);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (err < 0)
89 return err;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010090 memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92}
93
94/*
95 * we do not have to muck with descriptors here, that is
96 * done in switch_mm() as needed.
97 */
98int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
99{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100100 struct mm_struct *old_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int retval = 0;
102
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200103 mutex_init(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mm->context.size = 0;
105 old_mm = current->mm;
106 if (old_mm && old_mm->context.size > 0) {
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200107 mutex_lock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 retval = copy_ldt(&mm->context, &old_mm->context);
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200109 mutex_unlock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 return retval;
112}
113
114/*
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100115 * No need to lock the MM as we are the last user
116 *
117 * 64bit: Don't touch the LDT register - we're already in the next thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119void destroy_context(struct mm_struct *mm)
120{
121 if (mm->context.size) {
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100122#ifdef CONFIG_X86_32
123 /* CHECKME: Can this ever happen ? */
124 if (mm == current->active_mm)
125 clear_LDT();
126#endif
Thomas Gleixner70f50882008-01-30 13:30:13 +0100127 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 vfree(mm->context.ldt);
129 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100130 put_page(virt_to_page(mm->context.ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mm->context.size = 0;
132 }
133}
134
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100135static int read_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 int err;
138 unsigned long size;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100139 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (!mm->context.size)
142 return 0;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100143 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
144 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200146 mutex_lock(&mm->context.lock);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100147 size = mm->context.size * LDT_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (size > bytecount)
149 size = bytecount;
150
151 err = 0;
152 if (copy_to_user(ptr, mm->context.ldt, size))
153 err = -EFAULT;
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200154 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (err < 0)
156 goto error_return;
157 if (size != bytecount) {
158 /* zero-fill the rest */
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100159 if (clear_user(ptr + size, bytecount - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 err = -EFAULT;
161 goto error_return;
162 }
163 }
164 return bytecount;
165error_return:
166 return err;
167}
168
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100169static int read_default_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100171 /* CHECKME: Can we use _one_ random number ? */
172#ifdef CONFIG_X86_32
173 unsigned long size = 5 * sizeof(struct desc_struct);
174#else
175 unsigned long size = 128;
176#endif
177 if (bytecount > size)
178 bytecount = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (clear_user(ptr, bytecount))
180 return -EFAULT;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100181 return bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100184static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Thomas Gleixner70f50882008-01-30 13:30:13 +0100186 struct mm_struct *mm = current->mm;
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100187 struct desc_struct ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int error;
189 struct user_desc ldt_info;
190
191 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (bytecount != sizeof(ldt_info))
193 goto out;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100194 error = -EFAULT;
Thomas Gleixner70f50882008-01-30 13:30:13 +0100195 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto out;
197
198 error = -EINVAL;
199 if (ldt_info.entry_number >= LDT_ENTRIES)
200 goto out;
201 if (ldt_info.contents == 3) {
202 if (oldmode)
203 goto out;
204 if (ldt_info.seg_not_present == 0)
205 goto out;
206 }
207
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200208 mutex_lock(&mm->context.lock);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100209 if (ldt_info.entry_number >= mm->context.size) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100210 error = alloc_ldt(&current->mm->context,
211 ldt_info.entry_number + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (error < 0)
213 goto out_unlock;
214 }
215
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100216 /* Allow LDTs to be cleared by the user. */
217 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (oldmode || LDT_empty(&ldt_info)) {
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100219 memset(&ldt, 0, sizeof(ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto install;
221 }
222 }
223
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100224 fill_ldt(&ldt, &ldt_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (oldmode)
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100226 ldt.avl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* Install the new entry ... */
229install:
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100230 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 error = 0;
232
233out_unlock:
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200234 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235out:
236 return error;
237}
238
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100239asmlinkage int sys_modify_ldt(int func, void __user *ptr,
240 unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int ret = -ENOSYS;
243
244 switch (func) {
245 case 0:
246 ret = read_ldt(ptr, bytecount);
247 break;
248 case 1:
249 ret = write_ldt(ptr, bytecount, 1);
250 break;
251 case 2:
252 ret = read_default_ldt(ptr, bytecount);
253 break;
254 case 0x11:
255 ret = write_ldt(ptr, bytecount, 0);
256 break;
257 }
258 return ret;
259}