blob: 3fee2aa50f3f63d594304611090338d36f3fcc01 [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
Mike Travisc42f4f42008-07-15 14:14:32 -070065 cpumask_of_cpu_ptr_declare(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 load_LDT(pc);
Mike Travisc42f4f42008-07-15 14:14:32 -070069 cpumask_of_cpu_ptr_next(mask, smp_processor_id());
70 if (!cpus_equal(current->mm->cpu_vm_mask, *mask))
Ingo Molnar1a781a72008-07-15 21:55:59 +020071 smp_call_function(flush_ldt, current->mm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 preempt_enable();
73#else
74 load_LDT(pc);
75#endif
76 }
77 if (oldsize) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010078 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 vfree(oldldt);
80 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +010081 put_page(virt_to_page(oldldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83 return 0;
84}
85
86static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
87{
88 int err = alloc_ldt(new, old->size, 0);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (err < 0)
91 return err;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010092 memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return 0;
94}
95
96/*
97 * we do not have to muck with descriptors here, that is
98 * done in switch_mm() as needed.
99 */
100int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
101{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100102 struct mm_struct *old_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int retval = 0;
104
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200105 mutex_init(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 mm->context.size = 0;
107 old_mm = current->mm;
108 if (old_mm && old_mm->context.size > 0) {
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200109 mutex_lock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 retval = copy_ldt(&mm->context, &old_mm->context);
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200111 mutex_unlock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 return retval;
114}
115
116/*
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100117 * No need to lock the MM as we are the last user
118 *
119 * 64bit: Don't touch the LDT register - we're already in the next thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121void destroy_context(struct mm_struct *mm)
122{
123 if (mm->context.size) {
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100124#ifdef CONFIG_X86_32
125 /* CHECKME: Can this ever happen ? */
126 if (mm == current->active_mm)
127 clear_LDT();
128#endif
Thomas Gleixner70f50882008-01-30 13:30:13 +0100129 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 vfree(mm->context.ldt);
131 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100132 put_page(virt_to_page(mm->context.ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 mm->context.size = 0;
134 }
135}
136
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100137static int read_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int err;
140 unsigned long size;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100141 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if (!mm->context.size)
144 return 0;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100145 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
146 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200148 mutex_lock(&mm->context.lock);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100149 size = mm->context.size * LDT_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (size > bytecount)
151 size = bytecount;
152
153 err = 0;
154 if (copy_to_user(ptr, mm->context.ldt, size))
155 err = -EFAULT;
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200156 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (err < 0)
158 goto error_return;
159 if (size != bytecount) {
160 /* zero-fill the rest */
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100161 if (clear_user(ptr + size, bytecount - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 err = -EFAULT;
163 goto error_return;
164 }
165 }
166 return bytecount;
167error_return:
168 return err;
169}
170
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100171static int read_default_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100173 /* CHECKME: Can we use _one_ random number ? */
174#ifdef CONFIG_X86_32
175 unsigned long size = 5 * sizeof(struct desc_struct);
176#else
177 unsigned long size = 128;
178#endif
179 if (bytecount > size)
180 bytecount = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (clear_user(ptr, bytecount))
182 return -EFAULT;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100183 return bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100186static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Thomas Gleixner70f50882008-01-30 13:30:13 +0100188 struct mm_struct *mm = current->mm;
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100189 struct desc_struct ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int error;
191 struct user_desc ldt_info;
192
193 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (bytecount != sizeof(ldt_info))
195 goto out;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100196 error = -EFAULT;
Thomas Gleixner70f50882008-01-30 13:30:13 +0100197 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto out;
199
200 error = -EINVAL;
201 if (ldt_info.entry_number >= LDT_ENTRIES)
202 goto out;
203 if (ldt_info.contents == 3) {
204 if (oldmode)
205 goto out;
206 if (ldt_info.seg_not_present == 0)
207 goto out;
208 }
209
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200210 mutex_lock(&mm->context.lock);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100211 if (ldt_info.entry_number >= mm->context.size) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100212 error = alloc_ldt(&current->mm->context,
213 ldt_info.entry_number + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (error < 0)
215 goto out_unlock;
216 }
217
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100218 /* Allow LDTs to be cleared by the user. */
219 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (oldmode || LDT_empty(&ldt_info)) {
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100221 memset(&ldt, 0, sizeof(ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 goto install;
223 }
224 }
225
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100226 fill_ldt(&ldt, &ldt_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (oldmode)
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100228 ldt.avl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* Install the new entry ... */
231install:
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100232 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 error = 0;
234
235out_unlock:
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200236 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237out:
238 return error;
239}
240
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100241asmlinkage int sys_modify_ldt(int func, void __user *ptr,
242 unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int ret = -ENOSYS;
245
246 switch (func) {
247 case 0:
248 ret = read_ldt(ptr, bytecount);
249 break;
250 case 1:
251 ret = write_ldt(ptr, bytecount, 1);
252 break;
253 case 2:
254 ret = read_default_ldt(ptr, bytecount);
255 break;
256 case 0x11:
257 ret = write_ldt(ptr, bytecount, 0);
258 break;
259 }
260 return ret;
261}