| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds | 
 | 3 |  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com> | 
 | 4 |  */ | 
 | 5 |  | 
 | 6 | #include <linux/errno.h> | 
 | 7 | #include <linux/sched.h> | 
 | 8 | #include <linux/string.h> | 
 | 9 | #include <linux/mm.h> | 
 | 10 | #include <linux/smp.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/vmalloc.h> | 
 | 12 | #include <linux/slab.h> | 
 | 13 |  | 
 | 14 | #include <asm/uaccess.h> | 
 | 15 | #include <asm/system.h> | 
 | 16 | #include <asm/ldt.h> | 
 | 17 | #include <asm/desc.h> | 
| Adrian Bunk | 8d1ed63 | 2005-11-07 00:58:35 -0800 | [diff] [blame] | 18 | #include <asm/mmu_context.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 |  | 
 | 20 | #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */ | 
 | 21 | static void flush_ldt(void *null) | 
 | 22 | { | 
 | 23 | 	if (current->active_mm) | 
 | 24 | 		load_LDT(¤t->active_mm->context); | 
 | 25 | } | 
 | 26 | #endif | 
 | 27 |  | 
 | 28 | static int alloc_ldt(mm_context_t *pc, int mincount, int reload) | 
 | 29 | { | 
 | 30 | 	void *oldldt; | 
 | 31 | 	void *newldt; | 
 | 32 | 	int oldsize; | 
 | 33 |  | 
 | 34 | 	if (mincount <= pc->size) | 
 | 35 | 		return 0; | 
 | 36 | 	oldsize = pc->size; | 
 | 37 | 	mincount = (mincount+511)&(~511); | 
 | 38 | 	if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE) | 
 | 39 | 		newldt = vmalloc(mincount*LDT_ENTRY_SIZE); | 
 | 40 | 	else | 
 | 41 | 		newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL); | 
 | 42 |  | 
 | 43 | 	if (!newldt) | 
 | 44 | 		return -ENOMEM; | 
 | 45 |  | 
 | 46 | 	if (oldsize) | 
 | 47 | 		memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE); | 
 | 48 | 	oldldt = pc->ldt; | 
 | 49 | 	memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE); | 
 | 50 | 	pc->ldt = newldt; | 
 | 51 | 	wmb(); | 
 | 52 | 	pc->size = mincount; | 
 | 53 | 	wmb(); | 
 | 54 |  | 
 | 55 | 	if (reload) { | 
 | 56 | #ifdef CONFIG_SMP | 
 | 57 | 		cpumask_t mask; | 
 | 58 | 		preempt_disable(); | 
 | 59 | 		load_LDT(pc); | 
 | 60 | 		mask = cpumask_of_cpu(smp_processor_id()); | 
 | 61 | 		if (!cpus_equal(current->mm->cpu_vm_mask, mask)) | 
 | 62 | 			smp_call_function(flush_ldt, NULL, 1, 1); | 
 | 63 | 		preempt_enable(); | 
 | 64 | #else | 
 | 65 | 		load_LDT(pc); | 
 | 66 | #endif | 
 | 67 | 	} | 
 | 68 | 	if (oldsize) { | 
 | 69 | 		if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE) | 
 | 70 | 			vfree(oldldt); | 
 | 71 | 		else | 
 | 72 | 			kfree(oldldt); | 
 | 73 | 	} | 
 | 74 | 	return 0; | 
 | 75 | } | 
 | 76 |  | 
 | 77 | static inline int copy_ldt(mm_context_t *new, mm_context_t *old) | 
 | 78 | { | 
 | 79 | 	int err = alloc_ldt(new, old->size, 0); | 
 | 80 | 	if (err < 0) | 
 | 81 | 		return err; | 
 | 82 | 	memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE); | 
 | 83 | 	return 0; | 
 | 84 | } | 
 | 85 |  | 
 | 86 | /* | 
 | 87 |  * we do not have to muck with descriptors here, that is | 
 | 88 |  * done in switch_mm() as needed. | 
 | 89 |  */ | 
 | 90 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm) | 
 | 91 | { | 
 | 92 | 	struct mm_struct * old_mm; | 
 | 93 | 	int retval = 0; | 
 | 94 |  | 
 | 95 | 	init_MUTEX(&mm->context.sem); | 
 | 96 | 	mm->context.size = 0; | 
 | 97 | 	old_mm = current->mm; | 
 | 98 | 	if (old_mm && old_mm->context.size > 0) { | 
 | 99 | 		down(&old_mm->context.sem); | 
 | 100 | 		retval = copy_ldt(&mm->context, &old_mm->context); | 
 | 101 | 		up(&old_mm->context.sem); | 
 | 102 | 	} | 
 | 103 | 	return retval; | 
 | 104 | } | 
 | 105 |  | 
 | 106 | /* | 
 | 107 |  * No need to lock the MM as we are the last user | 
 | 108 |  */ | 
 | 109 | void destroy_context(struct mm_struct *mm) | 
 | 110 | { | 
 | 111 | 	if (mm->context.size) { | 
 | 112 | 		if (mm == current->active_mm) | 
 | 113 | 			clear_LDT(); | 
 | 114 | 		if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE) | 
 | 115 | 			vfree(mm->context.ldt); | 
 | 116 | 		else | 
 | 117 | 			kfree(mm->context.ldt); | 
 | 118 | 		mm->context.size = 0; | 
 | 119 | 	} | 
 | 120 | } | 
 | 121 |  | 
 | 122 | static int read_ldt(void __user * ptr, unsigned long bytecount) | 
 | 123 | { | 
 | 124 | 	int err; | 
 | 125 | 	unsigned long size; | 
 | 126 | 	struct mm_struct * mm = current->mm; | 
 | 127 |  | 
 | 128 | 	if (!mm->context.size) | 
 | 129 | 		return 0; | 
 | 130 | 	if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES) | 
 | 131 | 		bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES; | 
 | 132 |  | 
 | 133 | 	down(&mm->context.sem); | 
 | 134 | 	size = mm->context.size*LDT_ENTRY_SIZE; | 
 | 135 | 	if (size > bytecount) | 
 | 136 | 		size = bytecount; | 
 | 137 |  | 
 | 138 | 	err = 0; | 
 | 139 | 	if (copy_to_user(ptr, mm->context.ldt, size)) | 
 | 140 | 		err = -EFAULT; | 
 | 141 | 	up(&mm->context.sem); | 
 | 142 | 	if (err < 0) | 
 | 143 | 		goto error_return; | 
 | 144 | 	if (size != bytecount) { | 
 | 145 | 		/* zero-fill the rest */ | 
 | 146 | 		if (clear_user(ptr+size, bytecount-size) != 0) { | 
 | 147 | 			err = -EFAULT; | 
 | 148 | 			goto error_return; | 
 | 149 | 		} | 
 | 150 | 	} | 
 | 151 | 	return bytecount; | 
 | 152 | error_return: | 
 | 153 | 	return err; | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static int read_default_ldt(void __user * ptr, unsigned long bytecount) | 
 | 157 | { | 
 | 158 | 	int err; | 
 | 159 | 	unsigned long size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 |  | 
 | 161 | 	err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | 	size = 5*sizeof(struct desc_struct); | 
 | 163 | 	if (size > bytecount) | 
 | 164 | 		size = bytecount; | 
 | 165 |  | 
 | 166 | 	err = size; | 
| Jeremy Fitzhardinge | e5e3a04 | 2006-12-07 02:14:01 +0100 | [diff] [blame] | 167 | 	if (clear_user(ptr, size)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | 		err = -EFAULT; | 
 | 169 |  | 
 | 170 | 	return err; | 
 | 171 | } | 
 | 172 |  | 
 | 173 | static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode) | 
 | 174 | { | 
 | 175 | 	struct mm_struct * mm = current->mm; | 
| Zachary Amsden | f2f30eb | 2005-09-03 15:56:47 -0700 | [diff] [blame] | 176 | 	__u32 entry_1, entry_2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | 	int error; | 
 | 178 | 	struct user_desc ldt_info; | 
 | 179 |  | 
 | 180 | 	error = -EINVAL; | 
 | 181 | 	if (bytecount != sizeof(ldt_info)) | 
 | 182 | 		goto out; | 
 | 183 | 	error = -EFAULT; 	 | 
 | 184 | 	if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info))) | 
 | 185 | 		goto out; | 
 | 186 |  | 
 | 187 | 	error = -EINVAL; | 
 | 188 | 	if (ldt_info.entry_number >= LDT_ENTRIES) | 
 | 189 | 		goto out; | 
 | 190 | 	if (ldt_info.contents == 3) { | 
 | 191 | 		if (oldmode) | 
 | 192 | 			goto out; | 
 | 193 | 		if (ldt_info.seg_not_present == 0) | 
 | 194 | 			goto out; | 
 | 195 | 	} | 
 | 196 |  | 
 | 197 | 	down(&mm->context.sem); | 
 | 198 | 	if (ldt_info.entry_number >= mm->context.size) { | 
 | 199 | 		error = alloc_ldt(¤t->mm->context, ldt_info.entry_number+1, 1); | 
 | 200 | 		if (error < 0) | 
 | 201 | 			goto out_unlock; | 
 | 202 | 	} | 
 | 203 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 |    	/* Allow LDTs to be cleared by the user. */ | 
 | 205 |    	if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { | 
 | 206 | 		if (oldmode || LDT_empty(&ldt_info)) { | 
 | 207 | 			entry_1 = 0; | 
 | 208 | 			entry_2 = 0; | 
 | 209 | 			goto install; | 
 | 210 | 		} | 
 | 211 | 	} | 
 | 212 |  | 
 | 213 | 	entry_1 = LDT_entry_a(&ldt_info); | 
 | 214 | 	entry_2 = LDT_entry_b(&ldt_info); | 
 | 215 | 	if (oldmode) | 
 | 216 | 		entry_2 &= ~(1 << 20); | 
 | 217 |  | 
 | 218 | 	/* Install the new entry ...  */ | 
 | 219 | install: | 
| Zachary Amsden | f2f30eb | 2005-09-03 15:56:47 -0700 | [diff] [blame] | 220 | 	write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1, entry_2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | 	error = 0; | 
 | 222 |  | 
 | 223 | out_unlock: | 
 | 224 | 	up(&mm->context.sem); | 
 | 225 | out: | 
 | 226 | 	return error; | 
 | 227 | } | 
 | 228 |  | 
 | 229 | asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount) | 
 | 230 | { | 
 | 231 | 	int ret = -ENOSYS; | 
 | 232 |  | 
 | 233 | 	switch (func) { | 
 | 234 | 	case 0: | 
 | 235 | 		ret = read_ldt(ptr, bytecount); | 
 | 236 | 		break; | 
 | 237 | 	case 1: | 
 | 238 | 		ret = write_ldt(ptr, bytecount, 1); | 
 | 239 | 		break; | 
 | 240 | 	case 2: | 
 | 241 | 		ret = read_default_ldt(ptr, bytecount); | 
 | 242 | 		break; | 
 | 243 | 	case 0x11: | 
 | 244 | 		ret = write_ldt(ptr, bytecount, 0); | 
 | 245 | 		break; | 
 | 246 | 	} | 
 | 247 | 	return ret; | 
 | 248 | } |