Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 6 | #include <linux/mm.h> |
| 7 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/slab.h> |
Richard Weinberger | 1d04c8d | 2015-05-12 00:14:39 +0200 | [diff] [blame] | 9 | #include <linux/uaccess.h> |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 10 | #include <asm/unistd.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 11 | #include <os.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 12 | #include <skas.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 13 | #include <sysdep/tls.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 14 | |
Hans-Werner Hilse | 37e81a0 | 2015-06-29 11:50:32 +0200 | [diff] [blame] | 15 | static inline int modify_ldt (int func, void *ptr, unsigned long bytecount) |
| 16 | { |
| 17 | return syscall(__NR_modify_ldt, func, ptr, bytecount); |
| 18 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 19 | |
WANG Cong | 99764fa | 2008-07-23 21:28:49 -0700 | [diff] [blame] | 20 | static long write_ldt_entry(struct mm_id *mm_idp, int func, |
| 21 | struct user_desc *desc, void **addr, int done) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 22 | { |
| 23 | long res; |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 24 | void *stub_addr; |
| 25 | res = syscall_stub_data(mm_idp, (unsigned long *)desc, |
| 26 | (sizeof(*desc) + sizeof(long) - 1) & |
| 27 | ~(sizeof(long) - 1), |
| 28 | addr, &stub_addr); |
| 29 | if (!res) { |
| 30 | unsigned long args[] = { func, |
| 31 | (unsigned long)stub_addr, |
| 32 | sizeof(*desc), |
| 33 | 0, 0, 0 }; |
| 34 | res = run_syscall_stub(mm_idp, __NR_modify_ldt, args, |
| 35 | 0, addr, done); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 38 | return res; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * In skas mode, we hold our own ldt data in UML. |
| 43 | * Thus, the code implementing sys_modify_ldt_skas |
| 44 | * is very similar to (and mostly stolen from) sys_modify_ldt |
| 45 | * for arch/i386/kernel/ldt.c |
| 46 | * The routines copied and modified in part are: |
| 47 | * - read_ldt |
| 48 | * - read_default_ldt |
| 49 | * - write_ldt |
| 50 | * - sys_modify_ldt_skas |
| 51 | */ |
| 52 | |
| 53 | static int read_ldt(void __user * ptr, unsigned long bytecount) |
| 54 | { |
| 55 | int i, err = 0; |
| 56 | unsigned long size; |
Al Viro | b3ee571 | 2011-08-18 20:10:49 +0100 | [diff] [blame] | 57 | uml_ldt_t *ldt = ¤t->mm->context.arch.ldt; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 58 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 59 | if (!ldt->entry_count) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 60 | goto out; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 61 | if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 62 | bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES; |
| 63 | err = bytecount; |
| 64 | |
Daniel Walker | 01ac835 | 2008-02-04 22:31:26 -0800 | [diff] [blame] | 65 | mutex_lock(&ldt->lock); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 66 | if (ldt->entry_count <= LDT_DIRECT_ENTRIES) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 67 | size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 68 | if (size > bytecount) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 69 | size = bytecount; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 70 | if (copy_to_user(ptr, ldt->u.entries, size)) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 71 | err = -EFAULT; |
| 72 | bytecount -= size; |
| 73 | ptr += size; |
| 74 | } |
| 75 | else { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 76 | for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount; |
| 77 | i++) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 78 | size = PAGE_SIZE; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 79 | if (size > bytecount) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 80 | size = bytecount; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 81 | if (copy_to_user(ptr, ldt->u.pages[i], size)) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 82 | err = -EFAULT; |
| 83 | break; |
| 84 | } |
| 85 | bytecount -= size; |
| 86 | ptr += size; |
| 87 | } |
| 88 | } |
Daniel Walker | 01ac835 | 2008-02-04 22:31:26 -0800 | [diff] [blame] | 89 | mutex_unlock(&ldt->lock); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 90 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 91 | if (bytecount == 0 || err == -EFAULT) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 92 | goto out; |
| 93 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 94 | if (clear_user(ptr, bytecount)) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 95 | err = -EFAULT; |
| 96 | |
| 97 | out: |
| 98 | return err; |
| 99 | } |
| 100 | |
| 101 | static int read_default_ldt(void __user * ptr, unsigned long bytecount) |
| 102 | { |
| 103 | int err; |
| 104 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 105 | if (bytecount > 5*LDT_ENTRY_SIZE) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 106 | bytecount = 5*LDT_ENTRY_SIZE; |
| 107 | |
| 108 | err = bytecount; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 109 | /* |
| 110 | * UML doesn't support lcall7 and lcall27. |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 111 | * So, we don't really have a default ldt, but emulate |
| 112 | * an empty ldt of common host default ldt size. |
| 113 | */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 114 | if (clear_user(ptr, bytecount)) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 115 | err = -EFAULT; |
| 116 | |
| 117 | return err; |
| 118 | } |
| 119 | |
| 120 | static int write_ldt(void __user * ptr, unsigned long bytecount, int func) |
| 121 | { |
Al Viro | b3ee571 | 2011-08-18 20:10:49 +0100 | [diff] [blame] | 122 | uml_ldt_t *ldt = ¤t->mm->context.arch.ldt; |
Jeff Dike | 6c738ff | 2007-10-16 01:27:06 -0700 | [diff] [blame] | 123 | struct mm_id * mm_idp = ¤t->mm->context.id; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 124 | int i, err; |
| 125 | struct user_desc ldt_info; |
| 126 | struct ldt_entry entry0, *ldt_p; |
| 127 | void *addr = NULL; |
| 128 | |
| 129 | err = -EINVAL; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 130 | if (bytecount != sizeof(ldt_info)) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 131 | goto out; |
| 132 | err = -EFAULT; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 133 | if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info))) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 134 | goto out; |
| 135 | |
| 136 | err = -EINVAL; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 137 | if (ldt_info.entry_number >= LDT_ENTRIES) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 138 | goto out; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 139 | if (ldt_info.contents == 3) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 140 | if (func == 1) |
| 141 | goto out; |
| 142 | if (ldt_info.seg_not_present == 0) |
| 143 | goto out; |
| 144 | } |
| 145 | |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 146 | mutex_lock(&ldt->lock); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 147 | |
| 148 | err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 149 | if (err) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 150 | goto out_unlock; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 151 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 152 | if (ldt_info.entry_number >= ldt->entry_count && |
| 153 | ldt_info.entry_number >= LDT_DIRECT_ENTRIES) { |
| 154 | for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE; |
| 155 | i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number; |
| 156 | i++) { |
| 157 | if (i == 0) |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 158 | memcpy(&entry0, ldt->u.entries, |
| 159 | sizeof(entry0)); |
| 160 | ldt->u.pages[i] = (struct ldt_entry *) |
| 161 | __get_free_page(GFP_KERNEL|__GFP_ZERO); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 162 | if (!ldt->u.pages[i]) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 163 | err = -ENOMEM; |
| 164 | /* Undo the change in host */ |
| 165 | memset(&ldt_info, 0, sizeof(ldt_info)); |
| 166 | write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1); |
| 167 | goto out_unlock; |
| 168 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 169 | if (i == 0) { |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 170 | memcpy(ldt->u.pages[0], &entry0, |
| 171 | sizeof(entry0)); |
| 172 | memcpy(ldt->u.pages[0]+1, ldt->u.entries+1, |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 173 | sizeof(entry0)*(LDT_DIRECT_ENTRIES-1)); |
| 174 | } |
| 175 | ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE; |
| 176 | } |
| 177 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 178 | if (ldt->entry_count <= ldt_info.entry_number) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 179 | ldt->entry_count = ldt_info.entry_number + 1; |
| 180 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 181 | if (ldt->entry_count <= LDT_DIRECT_ENTRIES) |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 182 | ldt_p = ldt->u.entries + ldt_info.entry_number; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 183 | else |
Jeff Dike | e23181d | 2005-11-21 21:32:08 -0800 | [diff] [blame] | 184 | ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] + |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 185 | ldt_info.entry_number%LDT_ENTRIES_PER_PAGE; |
| 186 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 187 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0 && |
| 188 | (func == 1 || LDT_empty(&ldt_info))) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 189 | ldt_p->a = 0; |
| 190 | ldt_p->b = 0; |
| 191 | } |
| 192 | else{ |
| 193 | if (func == 1) |
| 194 | ldt_info.useable = 0; |
| 195 | ldt_p->a = LDT_entry_a(&ldt_info); |
| 196 | ldt_p->b = LDT_entry_b(&ldt_info); |
| 197 | } |
| 198 | err = 0; |
| 199 | |
| 200 | out_unlock: |
Daniel Walker | 01ac835 | 2008-02-04 22:31:26 -0800 | [diff] [blame] | 201 | mutex_unlock(&ldt->lock); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 202 | out: |
| 203 | return err; |
| 204 | } |
| 205 | |
| 206 | static long do_modify_ldt_skas(int func, void __user *ptr, |
| 207 | unsigned long bytecount) |
| 208 | { |
| 209 | int ret = -ENOSYS; |
| 210 | |
| 211 | switch (func) { |
| 212 | case 0: |
| 213 | ret = read_ldt(ptr, bytecount); |
| 214 | break; |
| 215 | case 1: |
| 216 | case 0x11: |
| 217 | ret = write_ldt(ptr, bytecount, func); |
| 218 | break; |
| 219 | case 2: |
| 220 | ret = read_default_ldt(ptr, bytecount); |
| 221 | break; |
| 222 | } |
| 223 | return ret; |
| 224 | } |
| 225 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 226 | static DEFINE_SPINLOCK(host_ldt_lock); |
| 227 | static short dummy_list[9] = {0, -1}; |
| 228 | static short * host_ldt_entries = NULL; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 229 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 230 | static void ldt_get_host_info(void) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 231 | { |
| 232 | long ret; |
Jeff Dike | 622e696 | 2007-03-29 01:20:32 -0700 | [diff] [blame] | 233 | struct ldt_entry * ldt; |
| 234 | short *tmp; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 235 | int i, size, k, order; |
| 236 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 237 | spin_lock(&host_ldt_lock); |
| 238 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 239 | if (host_ldt_entries != NULL) { |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 240 | spin_unlock(&host_ldt_lock); |
| 241 | return; |
| 242 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 243 | host_ldt_entries = dummy_list+1; |
| 244 | |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 245 | spin_unlock(&host_ldt_lock); |
| 246 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 247 | for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++) |
| 248 | ; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 249 | |
| 250 | ldt = (struct ldt_entry *) |
| 251 | __get_free_pages(GFP_KERNEL|__GFP_ZERO, order); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 252 | if (ldt == NULL) { |
| 253 | printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer " |
| 254 | "for host ldt\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 255 | return; |
| 256 | } |
| 257 | |
| 258 | ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 259 | if (ret < 0) { |
| 260 | printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 261 | goto out_free; |
| 262 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 263 | if (ret == 0) { |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 264 | /* default_ldt is active, simply write an empty entry 0 */ |
| 265 | host_ldt_entries = dummy_list; |
| 266 | goto out_free; |
| 267 | } |
| 268 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 269 | for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) { |
| 270 | if (ldt[i].a != 0 || ldt[i].b != 0) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 271 | size++; |
| 272 | } |
| 273 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 274 | if (size < ARRAY_SIZE(dummy_list)) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 275 | host_ldt_entries = dummy_list; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 276 | else { |
| 277 | size = (size + 1) * sizeof(dummy_list[0]); |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 278 | tmp = kmalloc(size, GFP_KERNEL); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 279 | if (tmp == NULL) { |
| 280 | printk(KERN_ERR "ldt_get_host_info: couldn't allocate " |
| 281 | "host ldt list\n"); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 282 | goto out_free; |
| 283 | } |
Jeff Dike | af72790 | 2007-02-28 20:13:06 -0800 | [diff] [blame] | 284 | host_ldt_entries = tmp; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 287 | for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) { |
| 288 | if (ldt[i].a != 0 || ldt[i].b != 0) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 289 | host_ldt_entries[k++] = i; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 290 | } |
| 291 | host_ldt_entries[k] = -1; |
| 292 | |
| 293 | out_free: |
| 294 | free_pages((unsigned long)ldt, order); |
| 295 | } |
| 296 | |
Jeff Dike | 6c738ff | 2007-10-16 01:27:06 -0700 | [diff] [blame] | 297 | long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 298 | { |
| 299 | struct user_desc desc; |
| 300 | short * num_p; |
| 301 | int i; |
| 302 | long page, err=0; |
| 303 | void *addr = NULL; |
| 304 | |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 305 | |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 306 | mutex_init(&new_mm->arch.ldt.lock); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 307 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 308 | if (!from_mm) { |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 309 | memset(&desc, 0, sizeof(desc)); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 310 | /* |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 311 | * Now we try to retrieve info about the ldt, we |
| 312 | * inherited from the host. All ldt-entries found |
| 313 | * will be reset in the following loop |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 314 | */ |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 315 | ldt_get_host_info(); |
| 316 | for (num_p=host_ldt_entries; *num_p != -1; num_p++) { |
| 317 | desc.entry_number = *num_p; |
| 318 | err = write_ldt_entry(&new_mm->id, 1, &desc, |
| 319 | &addr, *(num_p + 1) == -1); |
| 320 | if (err) |
| 321 | break; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 322 | } |
Al Viro | b3ee571 | 2011-08-18 20:10:49 +0100 | [diff] [blame] | 323 | new_mm->arch.ldt.entry_count = 0; |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 324 | |
| 325 | goto out; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 326 | } |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 327 | |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 328 | /* |
| 329 | * Our local LDT is used to supply the data for |
| 330 | * modify_ldt(READLDT), if PTRACE_LDT isn't available, |
| 331 | * i.e., we have to use the stub for modify_ldt, which |
| 332 | * can't handle the big read buffer of up to 64kB. |
| 333 | */ |
| 334 | mutex_lock(&from_mm->arch.ldt.lock); |
| 335 | if (from_mm->arch.ldt.entry_count <= LDT_DIRECT_ENTRIES) |
| 336 | memcpy(new_mm->arch.ldt.u.entries, from_mm->arch.ldt.u.entries, |
| 337 | sizeof(new_mm->arch.ldt.u.entries)); |
| 338 | else { |
| 339 | i = from_mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE; |
| 340 | while (i-->0) { |
| 341 | page = __get_free_page(GFP_KERNEL|__GFP_ZERO); |
| 342 | if (!page) { |
| 343 | err = -ENOMEM; |
| 344 | break; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 345 | } |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 346 | new_mm->arch.ldt.u.pages[i] = |
| 347 | (struct ldt_entry *) page; |
| 348 | memcpy(new_mm->arch.ldt.u.pages[i], |
| 349 | from_mm->arch.ldt.u.pages[i], PAGE_SIZE); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 350 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 351 | } |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 352 | new_mm->arch.ldt.entry_count = from_mm->arch.ldt.entry_count; |
| 353 | mutex_unlock(&from_mm->arch.ldt.lock); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 354 | |
Bodo Stroesser | 12919aa | 2006-01-18 17:42:39 -0800 | [diff] [blame] | 355 | out: |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 356 | return err; |
| 357 | } |
| 358 | |
| 359 | |
Jeff Dike | 6c738ff | 2007-10-16 01:27:06 -0700 | [diff] [blame] | 360 | void free_ldt(struct mm_context *mm) |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 361 | { |
| 362 | int i; |
| 363 | |
Richard Weinberger | d0b5e15 | 2015-03-18 21:31:27 +0100 | [diff] [blame] | 364 | if (mm->arch.ldt.entry_count > LDT_DIRECT_ENTRIES) { |
Al Viro | b3ee571 | 2011-08-18 20:10:49 +0100 | [diff] [blame] | 365 | i = mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 366 | while (i-- > 0) |
Al Viro | b3ee571 | 2011-08-18 20:10:49 +0100 | [diff] [blame] | 367 | free_page((long) mm->arch.ldt.u.pages[i]); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 368 | } |
Al Viro | b3ee571 | 2011-08-18 20:10:49 +0100 | [diff] [blame] | 369 | mm->arch.ldt.entry_count = 0; |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 370 | } |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 371 | |
| 372 | int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount) |
| 373 | { |
Jeff Dike | 6aa802c | 2007-10-16 01:26:56 -0700 | [diff] [blame] | 374 | return do_modify_ldt_skas(func, ptr, bytecount); |
Bodo Stroesser | 858259c | 2005-11-07 00:58:55 -0800 | [diff] [blame] | 375 | } |