ldt.c 5.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
 * Copyright (C) 2002 Andi Kleen
5
 *
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * This handles calls from both 32bit and 64bit mode.
 */

#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>

#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/ldt.h>
#include <asm/desc.h>
21
#include <asm/mmu_context.h>
L
Linus Torvalds 已提交
22

23
#ifdef CONFIG_SMP
L
Linus Torvalds 已提交
24 25 26
static void flush_ldt(void *null)
{
	if (current->active_mm)
27
		load_LDT(&current->active_mm->context);
L
Linus Torvalds 已提交
28 29 30
}
#endif

31
static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
L
Linus Torvalds 已提交
32
{
33 34
	void *oldldt, *newldt;
	int oldsize;
L
Linus Torvalds 已提交
35

36
	if (mincount <= pc->size)
L
Linus Torvalds 已提交
37 38
		return 0;
	oldsize = pc->size;
39 40 41
	mincount = (mincount + 511) & (~511);
	if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
		newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
L
Linus Torvalds 已提交
42
	else
43
		newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL);
L
Linus Torvalds 已提交
44 45 46 47 48

	if (!newldt)
		return -ENOMEM;

	if (oldsize)
49
		memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
L
Linus Torvalds 已提交
50
	oldldt = pc->ldt;
51 52
	memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
	       (mincount - oldsize) * LDT_ENTRY_SIZE);
53 54 55

#ifdef CONFIG_X86_64
	/* CHECKME: Do we really need this ? */
L
Linus Torvalds 已提交
56
	wmb();
57
#endif
L
Linus Torvalds 已提交
58 59 60 61
	pc->ldt = newldt;
	wmb();
	pc->size = mincount;
	wmb();
62

L
Linus Torvalds 已提交
63 64 65 66 67 68
	if (reload) {
#ifdef CONFIG_SMP
		cpumask_t mask;

		preempt_disable();
		load_LDT(pc);
69
		mask = cpumask_of_cpu(smp_processor_id());
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77
		if (!cpus_equal(current->mm->cpu_vm_mask, mask))
			smp_call_function(flush_ldt, NULL, 1, 1);
		preempt_enable();
#else
		load_LDT(pc);
#endif
	}
	if (oldsize) {
78
		if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86 87 88
			vfree(oldldt);
		else
			kfree(oldldt);
	}
	return 0;
}

static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
{
	int err = alloc_ldt(new, old->size, 0);
89

L
Linus Torvalds 已提交
90 91
	if (err < 0)
		return err;
92
	memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
L
Linus Torvalds 已提交
93 94 95 96 97 98 99 100 101
	return 0;
}

/*
 * we do not have to muck with descriptors here, that is
 * done in switch_mm() as needed.
 */
int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
{
102
	struct mm_struct *old_mm;
L
Linus Torvalds 已提交
103 104
	int retval = 0;

105
	mutex_init(&mm->context.lock);
L
Linus Torvalds 已提交
106 107 108
	mm->context.size = 0;
	old_mm = current->mm;
	if (old_mm && old_mm->context.size > 0) {
109
		mutex_lock(&old_mm->context.lock);
L
Linus Torvalds 已提交
110
		retval = copy_ldt(&mm->context, &old_mm->context);
111
		mutex_unlock(&old_mm->context.lock);
L
Linus Torvalds 已提交
112 113 114 115 116
	}
	return retval;
}

/*
117 118 119
 * No need to lock the MM as we are the last user
 *
 * 64bit: Don't touch the LDT register - we're already in the next thread.
L
Linus Torvalds 已提交
120 121 122 123
 */
void destroy_context(struct mm_struct *mm)
{
	if (mm->context.size) {
124 125 126 127 128
#ifdef CONFIG_X86_32
		/* CHECKME: Can this ever happen ? */
		if (mm == current->active_mm)
			clear_LDT();
#endif
129
		if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
L
Linus Torvalds 已提交
130 131 132 133 134 135 136
			vfree(mm->context.ldt);
		else
			kfree(mm->context.ldt);
		mm->context.size = 0;
	}
}

137
static int read_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
138 139 140
{
	int err;
	unsigned long size;
141
	struct mm_struct *mm = current->mm;
L
Linus Torvalds 已提交
142 143 144

	if (!mm->context.size)
		return 0;
145 146
	if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
		bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
L
Linus Torvalds 已提交
147

148
	mutex_lock(&mm->context.lock);
149
	size = mm->context.size * LDT_ENTRY_SIZE;
L
Linus Torvalds 已提交
150 151 152 153 154 155
	if (size > bytecount)
		size = bytecount;

	err = 0;
	if (copy_to_user(ptr, mm->context.ldt, size))
		err = -EFAULT;
156
	mutex_unlock(&mm->context.lock);
L
Linus Torvalds 已提交
157 158 159 160
	if (err < 0)
		goto error_return;
	if (size != bytecount) {
		/* zero-fill the rest */
161
		if (clear_user(ptr + size, bytecount - size) != 0) {
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170
			err = -EFAULT;
			goto error_return;
		}
	}
	return bytecount;
error_return:
	return err;
}

171
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
172
{
173 174 175 176 177 178 179 180
	/* CHECKME: Can we use _one_ random number ? */
#ifdef CONFIG_X86_32
	unsigned long size = 5 * sizeof(struct desc_struct);
#else
	unsigned long size = 128;
#endif
	if (bytecount > size)
		bytecount = size;
L
Linus Torvalds 已提交
181 182
	if (clear_user(ptr, bytecount))
		return -EFAULT;
183
	return bytecount;
L
Linus Torvalds 已提交
184 185
}

186
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
L
Linus Torvalds 已提交
187
{
188
	struct mm_struct *mm = current->mm;
189
	struct desc_struct ldt;
L
Linus Torvalds 已提交
190 191 192 193 194 195
	int error;
	struct user_desc ldt_info;

	error = -EINVAL;
	if (bytecount != sizeof(ldt_info))
		goto out;
196
	error = -EFAULT;
197
	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208 209
		goto out;

	error = -EINVAL;
	if (ldt_info.entry_number >= LDT_ENTRIES)
		goto out;
	if (ldt_info.contents == 3) {
		if (oldmode)
			goto out;
		if (ldt_info.seg_not_present == 0)
			goto out;
	}

210
	mutex_lock(&mm->context.lock);
211
	if (ldt_info.entry_number >= mm->context.size) {
212 213
		error = alloc_ldt(&current->mm->context,
				  ldt_info.entry_number + 1, 1);
L
Linus Torvalds 已提交
214 215 216 217
		if (error < 0)
			goto out_unlock;
	}

218 219
	/* Allow LDTs to be cleared by the user. */
	if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
L
Linus Torvalds 已提交
220
		if (oldmode || LDT_empty(&ldt_info)) {
221
			memset(&ldt, 0, sizeof(ldt));
L
Linus Torvalds 已提交
222 223 224 225
			goto install;
		}
	}

226
	fill_ldt(&ldt, &ldt_info);
L
Linus Torvalds 已提交
227
	if (oldmode)
228
		ldt.avl = 0;
L
Linus Torvalds 已提交
229 230 231

	/* Install the new entry ...  */
install:
232 233
	write_ldt_entry(mm->context.ldt, ldt_info.entry_number,
			ldt.a, ldt.b);
L
Linus Torvalds 已提交
234 235 236
	error = 0;

out_unlock:
237
	mutex_unlock(&mm->context.lock);
L
Linus Torvalds 已提交
238 239 240 241
out:
	return error;
}

242 243
asmlinkage int sys_modify_ldt(int func, void __user *ptr,
			      unsigned long bytecount)
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
{
	int ret = -ENOSYS;

	switch (func) {
	case 0:
		ret = read_ldt(ptr, bytecount);
		break;
	case 1:
		ret = write_ldt(ptr, bytecount, 1);
		break;
	case 2:
		ret = read_default_ldt(ptr, bytecount);
		break;
	case 0x11:
		ret = write_ldt(ptr, bytecount, 0);
		break;
	}
	return ret;
}