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