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