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