blob: 4895e0634d22bf83b1937777a6a15dbf4c93900d [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
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070054 paravirt_alloc_ldt(newldt, mincount);
55
Thomas Gleixner77e463d2008-01-30 13:30:14 +010056#ifdef CONFIG_X86_64
57 /* CHECKME: Do we really need this ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 wmb();
Thomas Gleixner77e463d2008-01-30 13:30:14 +010059#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 pc->ldt = newldt;
61 wmb();
62 pc->size = mincount;
63 wmb();
Thomas Gleixner70f50882008-01-30 13:30:13 +010064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (reload) {
66#ifdef CONFIG_SMP
Mike Travisc42f4f42008-07-15 14:14:32 -070067 cpumask_of_cpu_ptr_declare(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 load_LDT(pc);
Mike Travisc42f4f42008-07-15 14:14:32 -070071 cpumask_of_cpu_ptr_next(mask, smp_processor_id());
72 if (!cpus_equal(current->mm->cpu_vm_mask, *mask))
Ingo Molnar1a781a72008-07-15 21:55:59 +020073 smp_call_function(flush_ldt, current->mm, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 preempt_enable();
75#else
76 load_LDT(pc);
77#endif
78 }
79 if (oldsize) {
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070080 paravirt_free_ldt(oldldt, oldsize);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010081 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 vfree(oldldt);
83 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +010084 put_page(virt_to_page(oldldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
86 return 0;
87}
88
89static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
90{
91 int err = alloc_ldt(new, old->size, 0);
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070092 int i;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +010093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (err < 0)
95 return err;
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -070096
97 for(i = 0; i < old->size; i++)
98 write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100}
101
102/*
103 * we do not have to muck with descriptors here, that is
104 * done in switch_mm() as needed.
105 */
106int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
107{
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100108 struct mm_struct *old_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int retval = 0;
110
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200111 mutex_init(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 mm->context.size = 0;
113 old_mm = current->mm;
114 if (old_mm && old_mm->context.size > 0) {
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200115 mutex_lock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 retval = copy_ldt(&mm->context, &old_mm->context);
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200117 mutex_unlock(&old_mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 return retval;
120}
121
122/*
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100123 * No need to lock the MM as we are the last user
124 *
125 * 64bit: Don't touch the LDT register - we're already in the next thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127void destroy_context(struct mm_struct *mm)
128{
129 if (mm->context.size) {
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100130#ifdef CONFIG_X86_32
131 /* CHECKME: Can this ever happen ? */
132 if (mm == current->active_mm)
133 clear_LDT();
134#endif
Jeremy Fitzhardinge38ffbe62008-07-23 14:21:18 -0700135 paravirt_free_ldt(mm->context.ldt, mm->context.size);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100136 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 vfree(mm->context.ldt);
138 else
Jan Beulich4dbf7af2008-01-30 13:33:14 +0100139 put_page(virt_to_page(mm->context.ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 mm->context.size = 0;
141 }
142}
143
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100144static int read_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int err;
147 unsigned long size;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100148 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if (!mm->context.size)
151 return 0;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100152 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
153 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200155 mutex_lock(&mm->context.lock);
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100156 size = mm->context.size * LDT_ENTRY_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (size > bytecount)
158 size = bytecount;
159
160 err = 0;
161 if (copy_to_user(ptr, mm->context.ldt, size))
162 err = -EFAULT;
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200163 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (err < 0)
165 goto error_return;
166 if (size != bytecount) {
167 /* zero-fill the rest */
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100168 if (clear_user(ptr + size, bytecount - size) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 err = -EFAULT;
170 goto error_return;
171 }
172 }
173 return bytecount;
174error_return:
175 return err;
176}
177
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100178static int read_default_ldt(void __user *ptr, unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Thomas Gleixner77e463d2008-01-30 13:30:14 +0100180 /* CHECKME: Can we use _one_ random number ? */
181#ifdef CONFIG_X86_32
182 unsigned long size = 5 * sizeof(struct desc_struct);
183#else
184 unsigned long size = 128;
185#endif
186 if (bytecount > size)
187 bytecount = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (clear_user(ptr, bytecount))
189 return -EFAULT;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100190 return bytecount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100193static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Thomas Gleixner70f50882008-01-30 13:30:13 +0100195 struct mm_struct *mm = current->mm;
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100196 struct desc_struct ldt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int error;
198 struct user_desc ldt_info;
199
200 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (bytecount != sizeof(ldt_info))
202 goto out;
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100203 error = -EFAULT;
Thomas Gleixner70f50882008-01-30 13:30:13 +0100204 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out;
206
207 error = -EINVAL;
208 if (ldt_info.entry_number >= LDT_ENTRIES)
209 goto out;
210 if (ldt_info.contents == 3) {
211 if (oldmode)
212 goto out;
213 if (ldt_info.seg_not_present == 0)
214 goto out;
215 }
216
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200217 mutex_lock(&mm->context.lock);
Thomas Gleixner70f50882008-01-30 13:30:13 +0100218 if (ldt_info.entry_number >= mm->context.size) {
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100219 error = alloc_ldt(&current->mm->context,
220 ldt_info.entry_number + 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (error < 0)
222 goto out_unlock;
223 }
224
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100225 /* Allow LDTs to be cleared by the user. */
226 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (oldmode || LDT_empty(&ldt_info)) {
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100228 memset(&ldt, 0, sizeof(ldt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto install;
230 }
231 }
232
Glauber de Oliveira Costa80fbb692008-01-30 13:31:13 +0100233 fill_ldt(&ldt, &ldt_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (oldmode)
Glauber de Oliveira Costa5af72502008-01-30 13:31:13 +0100235 ldt.avl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* Install the new entry ... */
238install:
Glauber de Oliveira Costa75b8bb32008-01-30 13:31:13 +0100239 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 error = 0;
241
242out_unlock:
Luiz Fernando N. Capitulinoc7537ab22007-10-17 18:04:41 +0200243 mutex_unlock(&mm->context.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244out:
245 return error;
246}
247
Thomas Gleixner78aa1f62008-01-30 13:30:13 +0100248asmlinkage int sys_modify_ldt(int func, void __user *ptr,
249 unsigned long bytecount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 int ret = -ENOSYS;
252
253 switch (func) {
254 case 0:
255 ret = read_ldt(ptr, bytecount);
256 break;
257 case 1:
258 ret = write_ldt(ptr, bytecount, 1);
259 break;
260 case 2:
261 ret = read_default_ldt(ptr, bytecount);
262 break;
263 case 0x11:
264 ret = write_ldt(ptr, bytecount, 0);
265 break;
266 }
267 return ret;
268}