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