blob: c37886d759ccac2736c36b357cc0f399786f2fb3 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/sched.h>
12#include <linux/string.h>
13#include <linux/mm.h>
14#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/vmalloc.h>
Jaswinder Singh Rajput423a5402008-12-31 16:42:20 +053016#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/ldt.h>
19#include <asm/desc.h>
Thomas Gleixner70f50882008-01-30 13:30:13 +010020#include <asm/mmu_context.h>
Jaswinder Singhbbc1f692008-07-21 21:34:13 +053021#include <asm/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010023#ifdef CONFIG_SMP
Jan Beulichb4784582008-05-12 15:44:39 +020024static void flush_ldt(void *current_mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Jan Beulichb4784582008-05-12 15:44:39 +020026 if (current->active_mm == current_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;
Cyrill Gorcunovfa0c8642008-02-04 16:48:03 +010039 mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
40 (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010041 if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
42 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +010044 newldt = (void *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 if (!newldt)
47 return -ENOMEM;
48
49 if (oldsize)
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010050 memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 oldldt = pc->ldt;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010052 memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
53 (mincount - oldsize) * LDT_ENTRY_SIZE);
Thomas Gleixner77e463d2008-01-30 13:30:14 +010054
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070055 paravirt_alloc_ldt(newldt, mincount);
56
Thomas Gleixner77e463d2008-01-30 13:30:14 +010057#ifdef CONFIG_X86_64
58 /* CHECKME: Do we really need this ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 wmb();
Thomas Gleixner77e463d2008-01-30 13:30:14 +010060#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 pc->ldt = newldt;
62 wmb();
63 pc->size = mincount;
64 wmb();
Thomas Gleixner70f50882008-01-30 13:30:13 +010065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (reload) {
67#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 load_LDT(pc);
Rusty Russell78f1c4d2009-09-24 09:34:51 -060070 if (!cpumask_equal(mm_cpumask(current->mm),
71 cpumask_of(smp_processor_id())))
Ingo Molnar1a781a72008-07-15 21:55:59 +020072 smp_call_function(flush_ldt, current->mm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 preempt_enable();
74#else
75 load_LDT(pc);
76#endif
77 }
78 if (oldsize) {
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070079 paravirt_free_ldt(oldldt, oldsize);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010080 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 vfree(oldldt);
82 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +010083 put_page(virt_to_page(oldldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85 return 0;
86}
87
88static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
89{
90 int err = alloc_ldt(new, old->size, 0);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070091 int i;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (err < 0)
94 return err;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070095
Jaswinder Singh Rajput423a5402008-12-31 16:42:20 +053096 for (i = 0; i < old->size; i++)
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070097 write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
101/*
102 * we do not have to muck with descriptors here, that is
103 * done in switch_mm() as needed.
104 */
105int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
106{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100107 struct mm_struct *old_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int retval = 0;
109
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200110 mutex_init(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mm->context.size = 0;
112 old_mm = current->mm;
113 if (old_mm && old_mm->context.size > 0) {
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200114 mutex_lock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 retval = copy_ldt(&mm->context, &old_mm->context);
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200116 mutex_unlock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 return retval;
119}
120
121/*
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100122 * No need to lock the MM as we are the last user
123 *
124 * 64bit: Don't touch the LDT register - we're already in the next thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126void destroy_context(struct mm_struct *mm)
127{
128 if (mm->context.size) {
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100129#ifdef CONFIG_X86_32
130 /* CHECKME: Can this ever happen ? */
131 if (mm == current->active_mm)
132 clear_LDT();
133#endif
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700134 paravirt_free_ldt(mm->context.ldt, mm->context.size);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100135 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 vfree(mm->context.ldt);
137 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100138 put_page(virt_to_page(mm->context.ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 mm->context.size = 0;
140 }
141}
142
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100143static int read_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int err;
146 unsigned long size;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100147 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (!mm->context.size)
150 return 0;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100151 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
152 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200154 mutex_lock(&mm->context.lock);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100155 size = mm->context.size * LDT_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (size > bytecount)
157 size = bytecount;
158
159 err = 0;
160 if (copy_to_user(ptr, mm->context.ldt, size))
161 err = -EFAULT;
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200162 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (err < 0)
164 goto error_return;
165 if (size != bytecount) {
166 /* zero-fill the rest */
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100167 if (clear_user(ptr + size, bytecount - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 err = -EFAULT;
169 goto error_return;
170 }
171 }
172 return bytecount;
173error_return:
174 return err;
175}
176
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100177static int read_default_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100179 /* CHECKME: Can we use _one_ random number ? */
180#ifdef CONFIG_X86_32
181 unsigned long size = 5 * sizeof(struct desc_struct);
182#else
183 unsigned long size = 128;
184#endif
185 if (bytecount > size)
186 bytecount = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (clear_user(ptr, bytecount))
188 return -EFAULT;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100189 return bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100192static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Thomas Gleixner70f50882008-01-30 13:30:13 +0100194 struct mm_struct *mm = current->mm;
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100195 struct desc_struct ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int error;
197 struct user_desc ldt_info;
198
199 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (bytecount != sizeof(ldt_info))
201 goto out;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100202 error = -EFAULT;
Thomas Gleixner70f50882008-01-30 13:30:13 +0100203 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 goto out;
205
206 error = -EINVAL;
207 if (ldt_info.entry_number >= LDT_ENTRIES)
208 goto out;
209 if (ldt_info.contents == 3) {
210 if (oldmode)
211 goto out;
212 if (ldt_info.seg_not_present == 0)
213 goto out;
214 }
215
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200216 mutex_lock(&mm->context.lock);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100217 if (ldt_info.entry_number >= mm->context.size) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100218 error = alloc_ldt(&current->mm->context,
219 ldt_info.entry_number + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (error < 0)
221 goto out_unlock;
222 }
223
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100224 /* Allow LDTs to be cleared by the user. */
225 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (oldmode || LDT_empty(&ldt_info)) {
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100227 memset(&ldt, 0, sizeof(ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 goto install;
229 }
230 }
231
H. Peter Anvin34273f42014-05-04 10:36:22 -0700232 if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
233 error = -EINVAL;
234 goto out_unlock;
235 }
236
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100237 fill_ldt(&ldt, &ldt_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (oldmode)
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100239 ldt.avl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* Install the new entry ... */
242install:
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100243 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 error = 0;
245
246out_unlock:
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200247 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248out:
249 return error;
250}
251
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100252asmlinkage int sys_modify_ldt(int func, void __user *ptr,
253 unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int ret = -ENOSYS;
256
257 switch (func) {
258 case 0:
259 ret = read_ldt(ptr, bytecount);
260 break;
261 case 1:
262 ret = write_ldt(ptr, bytecount, 1);
263 break;
264 case 2:
265 ret = read_default_ldt(ptr, bytecount);
266 break;
267 case 0x11:
268 ret = write_ldt(ptr, bytecount, 0);
269 break;
270 }
271 return ret;
272}