blob: c49ff2dc8f83d2ad5f27a9659df55ab8a392d33b [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>
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
55#ifdef CONFIG_X86_64
56 /* CHECKME: Do we really need this ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 wmb();
Thomas Gleixner77e463d2008-01-30 13:30:14 +010058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 pc->ldt = newldt;
60 wmb();
61 pc->size = mincount;
62 wmb();
Thomas Gleixner70f50882008-01-30 13:30:13 +010063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (reload) {
65#ifdef CONFIG_SMP
66 cpumask_t mask;
67
68 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 load_LDT(pc);
Thomas Gleixner70f50882008-01-30 13:30:13 +010070 mask = cpumask_of_cpu(smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
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) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010079 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 vfree(oldldt);
81 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +010082 put_page(virt_to_page(oldldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84 return 0;
85}
86
87static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
88{
89 int err = alloc_ldt(new, old->size, 0);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (err < 0)
92 return err;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010093 memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return 0;
95}
96
97/*
98 * we do not have to muck with descriptors here, that is
99 * done in switch_mm() as needed.
100 */
101int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
102{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100103 struct mm_struct *old_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int retval = 0;
105
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200106 mutex_init(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 mm->context.size = 0;
108 old_mm = current->mm;
109 if (old_mm && old_mm->context.size > 0) {
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200110 mutex_lock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 retval = copy_ldt(&mm->context, &old_mm->context);
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200112 mutex_unlock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114 return retval;
115}
116
117/*
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100118 * No need to lock the MM as we are the last user
119 *
120 * 64bit: Don't touch the LDT register - we're already in the next thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122void destroy_context(struct mm_struct *mm)
123{
124 if (mm->context.size) {
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100125#ifdef CONFIG_X86_32
126 /* CHECKME: Can this ever happen ? */
127 if (mm == current->active_mm)
128 clear_LDT();
129#endif
Thomas Gleixner70f50882008-01-30 13:30:13 +0100130 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 vfree(mm->context.ldt);
132 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100133 put_page(virt_to_page(mm->context.ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 mm->context.size = 0;
135 }
136}
137
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100138static int read_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int err;
141 unsigned long size;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100142 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (!mm->context.size)
145 return 0;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100146 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
147 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200149 mutex_lock(&mm->context.lock);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100150 size = mm->context.size * LDT_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (size > bytecount)
152 size = bytecount;
153
154 err = 0;
155 if (copy_to_user(ptr, mm->context.ldt, size))
156 err = -EFAULT;
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200157 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (err < 0)
159 goto error_return;
160 if (size != bytecount) {
161 /* zero-fill the rest */
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100162 if (clear_user(ptr + size, bytecount - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 err = -EFAULT;
164 goto error_return;
165 }
166 }
167 return bytecount;
168error_return:
169 return err;
170}
171
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100172static int read_default_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100174 /* CHECKME: Can we use _one_ random number ? */
175#ifdef CONFIG_X86_32
176 unsigned long size = 5 * sizeof(struct desc_struct);
177#else
178 unsigned long size = 128;
179#endif
180 if (bytecount > size)
181 bytecount = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (clear_user(ptr, bytecount))
183 return -EFAULT;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100184 return bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100187static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Thomas Gleixner70f50882008-01-30 13:30:13 +0100189 struct mm_struct *mm = current->mm;
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100190 struct desc_struct ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int error;
192 struct user_desc ldt_info;
193
194 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (bytecount != sizeof(ldt_info))
196 goto out;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100197 error = -EFAULT;
Thomas Gleixner70f50882008-01-30 13:30:13 +0100198 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto out;
200
201 error = -EINVAL;
202 if (ldt_info.entry_number >= LDT_ENTRIES)
203 goto out;
204 if (ldt_info.contents == 3) {
205 if (oldmode)
206 goto out;
207 if (ldt_info.seg_not_present == 0)
208 goto out;
209 }
210
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200211 mutex_lock(&mm->context.lock);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100212 if (ldt_info.entry_number >= mm->context.size) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100213 error = alloc_ldt(&current->mm->context,
214 ldt_info.entry_number + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (error < 0)
216 goto out_unlock;
217 }
218
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100219 /* Allow LDTs to be cleared by the user. */
220 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (oldmode || LDT_empty(&ldt_info)) {
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100222 memset(&ldt, 0, sizeof(ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 goto install;
224 }
225 }
226
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100227 fill_ldt(&ldt, &ldt_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (oldmode)
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100229 ldt.avl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* Install the new entry ... */
232install:
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100233 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 error = 0;
235
236out_unlock:
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200237 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238out:
239 return error;
240}
241
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100242asmlinkage int sys_modify_ldt(int func, void __user *ptr,
243 unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int ret = -ENOSYS;
246
247 switch (func) {
248 case 0:
249 ret = read_ldt(ptr, bytecount);
250 break;
251 case 1:
252 ret = write_ldt(ptr, bytecount, 1);
253 break;
254 case 2:
255 ret = read_default_ldt(ptr, bytecount);
256 break;
257 case 0x11:
258 ret = write_ldt(ptr, bytecount, 0);
259 break;
260 }
261 return ret;
262}