ldt.c 6.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
 * 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
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/smp.h>
15
#include <linux/slab.h>
L
Linus Torvalds 已提交
16
#include <linux/vmalloc.h>
17
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
18 19 20

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

24
/* context.lock is held for us, so we don't need any locking. */
25
static void flush_ldt(void *current_mm)
L
Linus Torvalds 已提交
26
{
27 28 29 30 31 32 33
	mm_context_t *pc;

	if (current->active_mm != current_mm)
		return;

	pc = &current->active_mm->context;
	set_ldt(pc->ldt->entries, pc->ldt->size);
L
Linus Torvalds 已提交
34 35
}

36
/* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
37
static struct ldt_struct *alloc_ldt_struct(unsigned int size)
L
Linus Torvalds 已提交
38
{
39
	struct ldt_struct *new_ldt;
40
	unsigned int alloc_size;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

	if (size > LDT_ENTRIES)
		return NULL;

	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
	if (!new_ldt)
		return NULL;

	BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
	alloc_size = size * LDT_ENTRY_SIZE;

	/*
	 * Xen is very picky: it requires a page-aligned LDT that has no
	 * trailing nonzero bytes in any page that contains LDT descriptors.
	 * Keep it simple: zero the whole allocation and never allocate less
	 * than PAGE_SIZE.
	 */
	if (alloc_size > PAGE_SIZE)
		new_ldt->entries = vzalloc(alloc_size);
L
Linus Torvalds 已提交
60
	else
61
		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
L
Linus Torvalds 已提交
62

63 64 65 66
	if (!new_ldt->entries) {
		kfree(new_ldt);
		return NULL;
	}
67

68 69 70
	new_ldt->size = size;
	return new_ldt;
}
71

72 73 74 75
/* After calling this, the LDT is immutable. */
static void finalize_ldt_struct(struct ldt_struct *ldt)
{
	paravirt_alloc_ldt(ldt->entries, ldt->size);
L
Linus Torvalds 已提交
76 77
}

78 79 80
/* context.lock is held */
static void install_ldt(struct mm_struct *current_mm,
			struct ldt_struct *ldt)
L
Linus Torvalds 已提交
81
{
82 83 84 85 86 87
	/* Synchronizes with lockless_dereference in load_mm_ldt. */
	smp_store_release(&current_mm->context.ldt, ldt);

	/* Activate the LDT for all CPUs using current_mm. */
	on_each_cpu_mask(mm_cpumask(current_mm), flush_ldt, current_mm, true);
}
88

89 90 91 92
static void free_ldt_struct(struct ldt_struct *ldt)
{
	if (likely(!ldt))
		return;
93

94 95
	paravirt_free_ldt(ldt->entries, ldt->size);
	if (ldt->size * LDT_ENTRY_SIZE > PAGE_SIZE)
96
		vfree_atomic(ldt->entries);
97
	else
98
		free_page((unsigned long)ldt->entries);
99
	kfree(ldt);
L
Linus Torvalds 已提交
100 101 102 103 104 105
}

/*
 * we do not have to muck with descriptors here, that is
 * done in switch_mm() as needed.
 */
106
int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
L
Linus Torvalds 已提交
107
{
108
	struct ldt_struct *new_ldt;
109
	struct mm_struct *old_mm;
L
Linus Torvalds 已提交
110 111
	int retval = 0;

112
	mutex_init(&mm->context.lock);
L
Linus Torvalds 已提交
113
	old_mm = current->mm;
114 115 116
	if (!old_mm) {
		mm->context.ldt = NULL;
		return 0;
L
Linus Torvalds 已提交
117
	}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

	mutex_lock(&old_mm->context.lock);
	if (!old_mm->context.ldt) {
		mm->context.ldt = NULL;
		goto out_unlock;
	}

	new_ldt = alloc_ldt_struct(old_mm->context.ldt->size);
	if (!new_ldt) {
		retval = -ENOMEM;
		goto out_unlock;
	}

	memcpy(new_ldt->entries, old_mm->context.ldt->entries,
	       new_ldt->size * LDT_ENTRY_SIZE);
	finalize_ldt_struct(new_ldt);

	mm->context.ldt = new_ldt;

out_unlock:
	mutex_unlock(&old_mm->context.lock);
L
Linus Torvalds 已提交
139 140 141 142
	return retval;
}

/*
143 144 145
 * 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 已提交
146
 */
147
void destroy_context_ldt(struct mm_struct *mm)
L
Linus Torvalds 已提交
148
{
149 150
	free_ldt_struct(mm->context.ldt);
	mm->context.ldt = NULL;
L
Linus Torvalds 已提交
151 152
}

153
static int read_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
154
{
155
	int retval;
L
Linus Torvalds 已提交
156
	unsigned long size;
157
	struct mm_struct *mm = current->mm;
L
Linus Torvalds 已提交
158

159 160 161 162 163 164 165
	mutex_lock(&mm->context.lock);

	if (!mm->context.ldt) {
		retval = 0;
		goto out_unlock;
	}

166 167
	if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
		bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
L
Linus Torvalds 已提交
168

169
	size = mm->context.ldt->size * LDT_ENTRY_SIZE;
L
Linus Torvalds 已提交
170 171 172
	if (size > bytecount)
		size = bytecount;

173 174 175 176 177
	if (copy_to_user(ptr, mm->context.ldt->entries, size)) {
		retval = -EFAULT;
		goto out_unlock;
	}

L
Linus Torvalds 已提交
178
	if (size != bytecount) {
179 180 181 182
		/* Zero-fill the rest and pretend we read bytecount bytes. */
		if (clear_user(ptr + size, bytecount - size)) {
			retval = -EFAULT;
			goto out_unlock;
L
Linus Torvalds 已提交
183 184
		}
	}
185 186 187 188 189
	retval = bytecount;

out_unlock:
	mutex_unlock(&mm->context.lock);
	return retval;
L
Linus Torvalds 已提交
190 191
}

192
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
193
{
194 195 196 197 198 199 200 201
	/* 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 已提交
202 203
	if (clear_user(ptr, bytecount))
		return -EFAULT;
204
	return bytecount;
L
Linus Torvalds 已提交
205 206
}

207
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
L
Linus Torvalds 已提交
208
{
209
	struct mm_struct *mm = current->mm;
210 211 212
	struct ldt_struct *new_ldt, *old_ldt;
	unsigned int oldsize, newsize;
	struct user_desc ldt_info;
213
	struct desc_struct ldt;
L
Linus Torvalds 已提交
214 215 216 217 218
	int error;

	error = -EINVAL;
	if (bytecount != sizeof(ldt_info))
		goto out;
219
	error = -EFAULT;
220
	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
L
Linus Torvalds 已提交
221 222 223 224 225 226 227 228 229 230 231 232
		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;
	}

233 234 235 236 237 238 239 240
	if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
	    LDT_empty(&ldt_info)) {
		/* The user wants to clear the entry. */
		memset(&ldt, 0, sizeof(ldt));
	} else {
		if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
			error = -EINVAL;
			goto out;
L
Linus Torvalds 已提交
241
		}
242 243 244 245

		fill_ldt(&ldt, &ldt_info);
		if (oldmode)
			ldt.avl = 0;
L
Linus Torvalds 已提交
246 247
	}

248 249 250 251
	mutex_lock(&mm->context.lock);

	old_ldt = mm->context.ldt;
	oldsize = old_ldt ? old_ldt->size : 0;
252
	newsize = max(ldt_info.entry_number + 1, oldsize);
253 254 255 256

	error = -ENOMEM;
	new_ldt = alloc_ldt_struct(newsize);
	if (!new_ldt)
257 258
		goto out_unlock;

259 260 261 262
	if (old_ldt)
		memcpy(new_ldt->entries, old_ldt->entries, oldsize * LDT_ENTRY_SIZE);
	new_ldt->entries[ldt_info.entry_number] = ldt;
	finalize_ldt_struct(new_ldt);
L
Linus Torvalds 已提交
263

264 265
	install_ldt(mm, new_ldt);
	free_ldt_struct(old_ldt);
L
Linus Torvalds 已提交
266 267 268
	error = 0;

out_unlock:
269
	mutex_unlock(&mm->context.lock);
L
Linus Torvalds 已提交
270 271 272 273
out:
	return error;
}

274 275
asmlinkage int sys_modify_ldt(int func, void __user *ptr,
			      unsigned long bytecount)
L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
{
	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;
}