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