ldt.c 5.7 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
 * This handles calls from both 32bit and 64bit mode.
 */

#include <linux/errno.h>
10
#include <linux/gfp.h>
L
Linus Torvalds 已提交
11 12 13 14 15
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/vmalloc.h>
16
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
17 18 19

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

23
#ifdef CONFIG_SMP
24
static void flush_ldt(void *current_mm)
L
Linus Torvalds 已提交
25
{
26
	if (current->active_mm == current_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
	mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
			(~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
41 42
	if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
		newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
L
Linus Torvalds 已提交
43
	else
44
		newldt = (void *)__get_free_page(GFP_KERNEL);
L
Linus Torvalds 已提交
45 46 47 48 49

	if (!newldt)
		return -ENOMEM;

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

55 56
	paravirt_alloc_ldt(newldt, mincount);

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

L
Linus Torvalds 已提交
66 67 68 69
	if (reload) {
#ifdef CONFIG_SMP
		preempt_disable();
		load_LDT(pc);
70 71
		if (!cpumask_equal(mm_cpumask(current->mm),
				   cpumask_of(smp_processor_id())))
72
			smp_call_function(flush_ldt, current->mm, 1);
L
Linus Torvalds 已提交
73 74 75 76 77 78
		preempt_enable();
#else
		load_LDT(pc);
#endif
	}
	if (oldsize) {
79
		paravirt_free_ldt(oldldt, oldsize);
80
		if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
L
Linus Torvalds 已提交
81 82
			vfree(oldldt);
		else
83
			put_page(virt_to_page(oldldt));
L
Linus Torvalds 已提交
84 85 86 87 88 89 90
	}
	return 0;
}

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

L
Linus Torvalds 已提交
93 94
	if (err < 0)
		return err;
95

96
	for (i = 0; i < old->size; i++)
97
		write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE);
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105 106
	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)
{
107
	struct mm_struct *old_mm;
L
Linus Torvalds 已提交
108 109
	int retval = 0;

110
	mutex_init(&mm->context.lock);
L
Linus Torvalds 已提交
111 112 113
	mm->context.size = 0;
	old_mm = current->mm;
	if (old_mm && old_mm->context.size > 0) {
114
		mutex_lock(&old_mm->context.lock);
L
Linus Torvalds 已提交
115
		retval = copy_ldt(&mm->context, &old_mm->context);
116
		mutex_unlock(&old_mm->context.lock);
L
Linus Torvalds 已提交
117 118 119 120 121
	}
	return retval;
}

/*
122 123 124
 * 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 已提交
125 126 127 128
 */
void destroy_context(struct mm_struct *mm)
{
	if (mm->context.size) {
129 130 131 132 133
#ifdef CONFIG_X86_32
		/* CHECKME: Can this ever happen ? */
		if (mm == current->active_mm)
			clear_LDT();
#endif
134
		paravirt_free_ldt(mm->context.ldt, mm->context.size);
135
		if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
L
Linus Torvalds 已提交
136 137
			vfree(mm->context.ldt);
		else
138
			put_page(virt_to_page(mm->context.ldt));
L
Linus Torvalds 已提交
139 140 141 142
		mm->context.size = 0;
	}
}

143
static int read_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
144 145 146
{
	int err;
	unsigned long size;
147
	struct mm_struct *mm = current->mm;
L
Linus Torvalds 已提交
148 149 150

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

154
	mutex_lock(&mm->context.lock);
155
	size = mm->context.size * LDT_ENTRY_SIZE;
L
Linus Torvalds 已提交
156 157 158 159 160 161
	if (size > bytecount)
		size = bytecount;

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

177
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
178
{
179 180 181 182 183 184 185 186
	/* 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 已提交
187 188
	if (clear_user(ptr, bytecount))
		return -EFAULT;
189
	return bytecount;
L
Linus Torvalds 已提交
190 191
}

192
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
L
Linus Torvalds 已提交
193
{
194
	struct mm_struct *mm = current->mm;
195
	struct desc_struct ldt;
L
Linus Torvalds 已提交
196 197 198 199 200 201
	int error;
	struct user_desc ldt_info;

	error = -EINVAL;
	if (bytecount != sizeof(ldt_info))
		goto out;
202
	error = -EFAULT;
203
	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215
		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;
	}

216
	mutex_lock(&mm->context.lock);
217
	if (ldt_info.entry_number >= mm->context.size) {
218 219
		error = alloc_ldt(&current->mm->context,
				  ldt_info.entry_number + 1, 1);
L
Linus Torvalds 已提交
220 221 222 223
		if (error < 0)
			goto out_unlock;
	}

224 225
	/* Allow LDTs to be cleared by the user. */
	if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
L
Linus Torvalds 已提交
226
		if (oldmode || LDT_empty(&ldt_info)) {
227
			memset(&ldt, 0, sizeof(ldt));
L
Linus Torvalds 已提交
228 229 230 231
			goto install;
		}
	}

232
	fill_ldt(&ldt, &ldt_info);
L
Linus Torvalds 已提交
233
	if (oldmode)
234
		ldt.avl = 0;
L
Linus Torvalds 已提交
235 236 237

	/* Install the new entry ...  */
install:
238
	write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
L
Linus Torvalds 已提交
239 240 241
	error = 0;

out_unlock:
242
	mutex_unlock(&mm->context.lock);
L
Linus Torvalds 已提交
243 244 245 246
out:
	return error;
}

247 248
asmlinkage int sys_modify_ldt(int func, void __user *ptr,
			      unsigned long bytecount)
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
{
	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;
}