ldt.c 7.0 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
static void refresh_ldt_segments(void)
{
#ifdef CONFIG_X86_64
	unsigned short sel;

	/*
	 * Make sure that the cached DS and ES descriptors match the updated
	 * LDT.
	 */
	savesegment(ds, sel);
	if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
		loadsegment(ds, sel);

	savesegment(es, sel);
	if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
		loadsegment(es, sel);
#endif
}

43
/* context.lock is held for us, so we don't need any locking. */
44
static void flush_ldt(void *__mm)
L
Linus Torvalds 已提交
45
{
46
	struct mm_struct *mm = __mm;
47 48
	mm_context_t *pc;

49
	if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
50 51
		return;

52
	pc = &mm->context;
53
	set_ldt(pc->ldt->entries, pc->ldt->nr_entries);
54 55

	refresh_ldt_segments();
L
Linus Torvalds 已提交
56 57
}

58
/* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
59
static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
L
Linus Torvalds 已提交
60
{
61
	struct ldt_struct *new_ldt;
62
	unsigned int alloc_size;
63

64
	if (num_entries > LDT_ENTRIES)
65 66 67 68 69 70 71
		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));
72
	alloc_size = num_entries * LDT_ENTRY_SIZE;
73 74 75 76 77 78 79 80 81

	/*
	 * 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 已提交
82
	else
83
		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
L
Linus Torvalds 已提交
84

85 86 87 88
	if (!new_ldt->entries) {
		kfree(new_ldt);
		return NULL;
	}
89

90
	new_ldt->nr_entries = num_entries;
91 92
	return new_ldt;
}
93

94 95 96
/* After calling this, the LDT is immutable. */
static void finalize_ldt_struct(struct ldt_struct *ldt)
{
97
	paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
L
Linus Torvalds 已提交
98 99
}

100 101 102
/* context.lock is held */
static void install_ldt(struct mm_struct *current_mm,
			struct ldt_struct *ldt)
L
Linus Torvalds 已提交
103
{
104 105 106 107 108 109
	/* 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);
}
110

111 112 113 114
static void free_ldt_struct(struct ldt_struct *ldt)
{
	if (likely(!ldt))
		return;
115

116 117
	paravirt_free_ldt(ldt->entries, ldt->nr_entries);
	if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
118
		vfree_atomic(ldt->entries);
119
	else
120
		free_page((unsigned long)ldt->entries);
121
	kfree(ldt);
L
Linus Torvalds 已提交
122 123 124 125 126 127
}

/*
 * we do not have to muck with descriptors here, that is
 * done in switch_mm() as needed.
 */
128
int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
L
Linus Torvalds 已提交
129
{
130
	struct ldt_struct *new_ldt;
131
	struct mm_struct *old_mm;
L
Linus Torvalds 已提交
132 133
	int retval = 0;

134
	mutex_init(&mm->context.lock);
L
Linus Torvalds 已提交
135
	old_mm = current->mm;
136 137 138
	if (!old_mm) {
		mm->context.ldt = NULL;
		return 0;
L
Linus Torvalds 已提交
139
	}
140 141 142 143 144 145 146

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

147
	new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
148 149 150 151 152 153
	if (!new_ldt) {
		retval = -ENOMEM;
		goto out_unlock;
	}

	memcpy(new_ldt->entries, old_mm->context.ldt->entries,
154
	       new_ldt->nr_entries * LDT_ENTRY_SIZE);
155 156 157 158 159 160
	finalize_ldt_struct(new_ldt);

	mm->context.ldt = new_ldt;

out_unlock:
	mutex_unlock(&old_mm->context.lock);
L
Linus Torvalds 已提交
161 162 163 164
	return retval;
}

/*
165 166 167
 * 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 已提交
168
 */
169
void destroy_context_ldt(struct mm_struct *mm)
L
Linus Torvalds 已提交
170
{
171 172
	free_ldt_struct(mm->context.ldt);
	mm->context.ldt = NULL;
L
Linus Torvalds 已提交
173 174
}

175
static int read_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
176
{
177
	struct mm_struct *mm = current->mm;
178 179
	unsigned long entries_size;
	int retval;
L
Linus Torvalds 已提交
180

181 182 183 184 185 186 187
	mutex_lock(&mm->context.lock);

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

188 189
	if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
		bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
L
Linus Torvalds 已提交
190

191 192 193
	entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
	if (entries_size > bytecount)
		entries_size = bytecount;
L
Linus Torvalds 已提交
194

195
	if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
196 197 198 199
		retval = -EFAULT;
		goto out_unlock;
	}

200
	if (entries_size != bytecount) {
201
		/* Zero-fill the rest and pretend we read bytecount bytes. */
202
		if (clear_user(ptr + entries_size, bytecount - entries_size)) {
203 204
			retval = -EFAULT;
			goto out_unlock;
L
Linus Torvalds 已提交
205 206
		}
	}
207 208 209 210 211
	retval = bytecount;

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

214
static int read_default_ldt(void __user *ptr, unsigned long bytecount)
L
Linus Torvalds 已提交
215
{
216 217 218 219 220 221 222 223
	/* 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 已提交
224 225
	if (clear_user(ptr, bytecount))
		return -EFAULT;
226
	return bytecount;
L
Linus Torvalds 已提交
227 228
}

229
static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
L
Linus Torvalds 已提交
230
{
231
	struct mm_struct *mm = current->mm;
232
	struct ldt_struct *new_ldt, *old_ldt;
233
	unsigned int old_nr_entries, new_nr_entries;
234
	struct user_desc ldt_info;
235
	struct desc_struct ldt;
L
Linus Torvalds 已提交
236 237 238 239 240
	int error;

	error = -EINVAL;
	if (bytecount != sizeof(ldt_info))
		goto out;
241
	error = -EFAULT;
242
	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251 252 253 254
		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;
	}

255 256 257 258 259 260 261 262
	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 已提交
263
		}
264 265 266 267

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

270 271
	mutex_lock(&mm->context.lock);

272 273 274
	old_ldt       = mm->context.ldt;
	old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
	new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
275 276

	error = -ENOMEM;
277
	new_ldt = alloc_ldt_struct(new_nr_entries);
278
	if (!new_ldt)
279 280
		goto out_unlock;

281
	if (old_ldt)
282 283
		memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);

284 285
	new_ldt->entries[ldt_info.entry_number] = ldt;
	finalize_ldt_struct(new_ldt);
L
Linus Torvalds 已提交
286

287 288
	install_ldt(mm, new_ldt);
	free_ldt_struct(old_ldt);
L
Linus Torvalds 已提交
289 290 291
	error = 0;

out_unlock:
292
	mutex_unlock(&mm->context.lock);
L
Linus Torvalds 已提交
293 294 295 296
out:
	return error;
}

297 298
asmlinkage int sys_modify_ldt(int func, void __user *ptr,
			      unsigned long bytecount)
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
	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;
}