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