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 | * Copyright (C) 2002 Andi Kleen |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * This handles calls from both 32bit and 64bit mode. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/errno.h> |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/string.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 21 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 23 | #ifdef CONFIG_SMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static void flush_ldt(void *null) |
| 25 | { |
| 26 | if (current->active_mm) |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 27 | load_LDT(¤t->active_mm->context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
| 29 | #endif |
| 30 | |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 31 | static int alloc_ldt(mm_context_t *pc, int mincount, int reload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 33 | void *oldldt, *newldt; |
| 34 | int oldsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 36 | if (mincount <= pc->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | return 0; |
| 38 | oldsize = pc->size; |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 39 | mincount = (mincount + 511) & (~511); |
| 40 | if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE) |
| 41 | newldt = vmalloc(mincount * LDT_ENTRY_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | else |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 43 | newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | if (!newldt) |
| 46 | return -ENOMEM; |
| 47 | |
| 48 | if (oldsize) |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 49 | memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | oldldt = pc->ldt; |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 51 | memset(newldt + oldsize * LDT_ENTRY_SIZE, 0, |
| 52 | (mincount - oldsize) * LDT_ENTRY_SIZE); |
Thomas Gleixner | 77e463d | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 53 | |
| 54 | #ifdef CONFIG_X86_64 |
| 55 | /* CHECKME: Do we really need this ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | wmb(); |
Thomas Gleixner | 77e463d | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 57 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | pc->ldt = newldt; |
| 59 | wmb(); |
| 60 | pc->size = mincount; |
| 61 | wmb(); |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | if (reload) { |
| 64 | #ifdef CONFIG_SMP |
| 65 | cpumask_t mask; |
| 66 | |
| 67 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | load_LDT(pc); |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 69 | mask = cpumask_of_cpu(smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (!cpus_equal(current->mm->cpu_vm_mask, mask)) |
| 71 | smp_call_function(flush_ldt, NULL, 1, 1); |
| 72 | preempt_enable(); |
| 73 | #else |
| 74 | load_LDT(pc); |
| 75 | #endif |
| 76 | } |
| 77 | if (oldsize) { |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 78 | if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | vfree(oldldt); |
| 80 | else |
| 81 | kfree(oldldt); |
| 82 | } |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static inline int copy_ldt(mm_context_t *new, mm_context_t *old) |
| 87 | { |
| 88 | int err = alloc_ldt(new, old->size, 0); |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (err < 0) |
| 91 | return err; |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 92 | memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * we do not have to muck with descriptors here, that is |
| 98 | * done in switch_mm() as needed. |
| 99 | */ |
| 100 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm) |
| 101 | { |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 102 | struct mm_struct *old_mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | int retval = 0; |
| 104 | |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 105 | mutex_init(&mm->context.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | mm->context.size = 0; |
| 107 | old_mm = current->mm; |
| 108 | if (old_mm && old_mm->context.size > 0) { |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 109 | mutex_lock(&old_mm->context.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | retval = copy_ldt(&mm->context, &old_mm->context); |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 111 | mutex_unlock(&old_mm->context.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | return retval; |
| 114 | } |
| 115 | |
| 116 | /* |
Thomas Gleixner | 77e463d | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 117 | * No need to lock the MM as we are the last user |
| 118 | * |
| 119 | * 64bit: Don't touch the LDT register - we're already in the next thread. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | */ |
| 121 | void destroy_context(struct mm_struct *mm) |
| 122 | { |
| 123 | if (mm->context.size) { |
Thomas Gleixner | 77e463d | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 124 | #ifdef CONFIG_X86_32 |
| 125 | /* CHECKME: Can this ever happen ? */ |
| 126 | if (mm == current->active_mm) |
| 127 | clear_LDT(); |
| 128 | #endif |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 129 | if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | vfree(mm->context.ldt); |
| 131 | else |
| 132 | kfree(mm->context.ldt); |
| 133 | mm->context.size = 0; |
| 134 | } |
| 135 | } |
| 136 | |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 137 | static int read_ldt(void __user *ptr, unsigned long bytecount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | int err; |
| 140 | unsigned long size; |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 141 | struct mm_struct *mm = current->mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | if (!mm->context.size) |
| 144 | return 0; |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 145 | if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES) |
| 146 | bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 148 | mutex_lock(&mm->context.lock); |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 149 | size = mm->context.size * LDT_ENTRY_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (size > bytecount) |
| 151 | size = bytecount; |
| 152 | |
| 153 | err = 0; |
| 154 | if (copy_to_user(ptr, mm->context.ldt, size)) |
| 155 | err = -EFAULT; |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 156 | mutex_unlock(&mm->context.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (err < 0) |
| 158 | goto error_return; |
| 159 | if (size != bytecount) { |
| 160 | /* zero-fill the rest */ |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 161 | if (clear_user(ptr + size, bytecount - size) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | err = -EFAULT; |
| 163 | goto error_return; |
| 164 | } |
| 165 | } |
| 166 | return bytecount; |
| 167 | error_return: |
| 168 | return err; |
| 169 | } |
| 170 | |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 171 | static int read_default_ldt(void __user *ptr, unsigned long bytecount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
Thomas Gleixner | 77e463d | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 173 | /* CHECKME: Can we use _one_ random number ? */ |
| 174 | #ifdef CONFIG_X86_32 |
| 175 | unsigned long size = 5 * sizeof(struct desc_struct); |
| 176 | #else |
| 177 | unsigned long size = 128; |
| 178 | #endif |
| 179 | if (bytecount > size) |
| 180 | bytecount = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (clear_user(ptr, bytecount)) |
| 182 | return -EFAULT; |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 183 | return bytecount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 186 | static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 188 | struct mm_struct *mm = current->mm; |
Glauber de Oliveira Costa | 5af7250 | 2008-01-30 13:31:13 +0100 | [diff] [blame^] | 189 | struct desc_struct ldt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | int error; |
| 191 | struct user_desc ldt_info; |
| 192 | |
| 193 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (bytecount != sizeof(ldt_info)) |
| 195 | goto out; |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 196 | error = -EFAULT; |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 197 | if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | goto out; |
| 199 | |
| 200 | error = -EINVAL; |
| 201 | if (ldt_info.entry_number >= LDT_ENTRIES) |
| 202 | goto out; |
| 203 | if (ldt_info.contents == 3) { |
| 204 | if (oldmode) |
| 205 | goto out; |
| 206 | if (ldt_info.seg_not_present == 0) |
| 207 | goto out; |
| 208 | } |
| 209 | |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 210 | mutex_lock(&mm->context.lock); |
Thomas Gleixner | 70f5088 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 211 | if (ldt_info.entry_number >= mm->context.size) { |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 212 | error = alloc_ldt(¤t->mm->context, |
| 213 | ldt_info.entry_number + 1, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (error < 0) |
| 215 | goto out_unlock; |
| 216 | } |
| 217 | |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 218 | /* Allow LDTs to be cleared by the user. */ |
| 219 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (oldmode || LDT_empty(&ldt_info)) { |
Glauber de Oliveira Costa | 5af7250 | 2008-01-30 13:31:13 +0100 | [diff] [blame^] | 221 | memset(&ldt, 0, sizeof(ldt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | goto install; |
| 223 | } |
| 224 | } |
| 225 | |
Glauber de Oliveira Costa | 5af7250 | 2008-01-30 13:31:13 +0100 | [diff] [blame^] | 226 | ldt.a = LDT_entry_a(&ldt_info); |
| 227 | ldt.b = LDT_entry_b(&ldt_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (oldmode) |
Glauber de Oliveira Costa | 5af7250 | 2008-01-30 13:31:13 +0100 | [diff] [blame^] | 229 | ldt.avl = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | /* Install the new entry ... */ |
| 232 | install: |
Glauber de Oliveira Costa | 5af7250 | 2008-01-30 13:31:13 +0100 | [diff] [blame^] | 233 | write_ldt_entry(mm->context.ldt, ldt_info.entry_number, |
| 234 | ldt.a, ldt.b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | error = 0; |
| 236 | |
| 237 | out_unlock: |
Luiz Fernando N. Capitulino | c7537ab2 | 2007-10-17 18:04:41 +0200 | [diff] [blame] | 238 | mutex_unlock(&mm->context.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | out: |
| 240 | return error; |
| 241 | } |
| 242 | |
Thomas Gleixner | 78aa1f6 | 2008-01-30 13:30:13 +0100 | [diff] [blame] | 243 | asmlinkage int sys_modify_ldt(int func, void __user *ptr, |
| 244 | unsigned long bytecount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
| 246 | int ret = -ENOSYS; |
| 247 | |
| 248 | switch (func) { |
| 249 | case 0: |
| 250 | ret = read_ldt(ptr, bytecount); |
| 251 | break; |
| 252 | case 1: |
| 253 | ret = write_ldt(ptr, bytecount, 1); |
| 254 | break; |
| 255 | case 2: |
| 256 | ret = read_default_ldt(ptr, bytecount); |
| 257 | break; |
| 258 | case 0x11: |
| 259 | ret = write_ldt(ptr, bytecount, 0); |
| 260 | break; |
| 261 | } |
| 262 | return ret; |
| 263 | } |