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