Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 1 | /* |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 2 | * User access functions based on page table walks for enhanced |
| 3 | * system layout without hardware support. |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 4 | * |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2006, 2012 |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 6 | * Author(s): Gerald Schaefer (gerald.schaefer@de.ibm.com) |
| 7 | */ |
| 8 | |
| 9 | #include <linux/errno.h> |
Heiko Carstens | d8ad075 | 2007-01-09 10:18:50 +0100 | [diff] [blame] | 10 | #include <linux/hardirq.h> |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 11 | #include <linux/mm.h> |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 12 | #include <linux/hugetlb.h> |
Heiko Carstens | 2215591 | 2006-12-08 15:53:49 +0100 | [diff] [blame] | 13 | #include <asm/uaccess.h> |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 14 | #include <asm/futex.h> |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 15 | #include "uaccess.h" |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 16 | |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 17 | #ifndef CONFIG_64BIT |
| 18 | #define AHI "ahi" |
| 19 | #define SLR "slr" |
| 20 | #else |
| 21 | #define AHI "aghi" |
| 22 | #define SLR "slgr" |
| 23 | #endif |
| 24 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 25 | static unsigned long strnlen_kernel(const char __user *src, unsigned long count) |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 26 | { |
| 27 | register unsigned long reg0 asm("0") = 0UL; |
| 28 | unsigned long tmp1, tmp2; |
| 29 | |
| 30 | asm volatile( |
| 31 | " la %2,0(%1)\n" |
| 32 | " la %3,0(%0,%1)\n" |
| 33 | " "SLR" %0,%0\n" |
| 34 | "0: srst %3,%2\n" |
| 35 | " jo 0b\n" |
| 36 | " la %0,1(%3)\n" /* strnlen_kernel results includes \0 */ |
| 37 | " "SLR" %0,%1\n" |
| 38 | "1:\n" |
| 39 | EX_TABLE(0b,1b) |
| 40 | : "+a" (count), "+a" (src), "=a" (tmp1), "=a" (tmp2) |
| 41 | : "d" (reg0) : "cc", "memory"); |
| 42 | return count; |
| 43 | } |
| 44 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 45 | static unsigned long copy_in_kernel(void __user *to, const void __user *from, |
| 46 | unsigned long count) |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 47 | { |
| 48 | unsigned long tmp1; |
| 49 | |
| 50 | asm volatile( |
| 51 | " "AHI" %0,-1\n" |
| 52 | " jo 5f\n" |
| 53 | " bras %3,3f\n" |
| 54 | "0:"AHI" %0,257\n" |
| 55 | "1: mvc 0(1,%1),0(%2)\n" |
| 56 | " la %1,1(%1)\n" |
| 57 | " la %2,1(%2)\n" |
| 58 | " "AHI" %0,-1\n" |
| 59 | " jnz 1b\n" |
| 60 | " j 5f\n" |
| 61 | "2: mvc 0(256,%1),0(%2)\n" |
| 62 | " la %1,256(%1)\n" |
| 63 | " la %2,256(%2)\n" |
| 64 | "3:"AHI" %0,-256\n" |
| 65 | " jnm 2b\n" |
| 66 | "4: ex %0,1b-0b(%3)\n" |
| 67 | "5:"SLR" %0,%0\n" |
| 68 | "6:\n" |
| 69 | EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b) |
| 70 | : "+a" (count), "+a" (to), "+a" (from), "=a" (tmp1) |
| 71 | : : "cc", "memory"); |
| 72 | return count; |
| 73 | } |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Returns kernel address for user virtual address. If the returned address is |
Hendrik Brueckner | b4a9601 | 2013-12-13 12:53:42 +0100 | [diff] [blame] | 77 | * >= -4095 (IS_ERR_VALUE(x) returns true), a fault has occurred and the |
| 78 | * address contains the (negative) exception code. |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 79 | */ |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 80 | #ifdef CONFIG_64BIT |
Heiko Carstens | 71a86ef | 2013-11-21 16:22:17 +0100 | [diff] [blame] | 81 | |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 82 | static unsigned long follow_table(struct mm_struct *mm, |
| 83 | unsigned long address, int write) |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 84 | { |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 85 | unsigned long *table = (unsigned long *)__pa(mm->pgd); |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 86 | |
Heiko Carstens | 71a86ef | 2013-11-21 16:22:17 +0100 | [diff] [blame] | 87 | if (unlikely(address > mm->context.asce_limit - 1)) |
| 88 | return -0x38UL; |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 89 | switch (mm->context.asce_bits & _ASCE_TYPE_MASK) { |
| 90 | case _ASCE_TYPE_REGION1: |
| 91 | table = table + ((address >> 53) & 0x7ff); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 92 | if (unlikely(*table & _REGION_ENTRY_INVALID)) |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 93 | return -0x39UL; |
| 94 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
Heiko Carstens | 12d8471 | 2013-04-29 10:58:56 +0200 | [diff] [blame] | 95 | /* fallthrough */ |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 96 | case _ASCE_TYPE_REGION2: |
| 97 | table = table + ((address >> 42) & 0x7ff); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 98 | if (unlikely(*table & _REGION_ENTRY_INVALID)) |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 99 | return -0x3aUL; |
| 100 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
Heiko Carstens | 12d8471 | 2013-04-29 10:58:56 +0200 | [diff] [blame] | 101 | /* fallthrough */ |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 102 | case _ASCE_TYPE_REGION3: |
| 103 | table = table + ((address >> 31) & 0x7ff); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 104 | if (unlikely(*table & _REGION_ENTRY_INVALID)) |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 105 | return -0x3bUL; |
| 106 | table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN); |
Heiko Carstens | 12d8471 | 2013-04-29 10:58:56 +0200 | [diff] [blame] | 107 | /* fallthrough */ |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 108 | case _ASCE_TYPE_SEGMENT: |
| 109 | table = table + ((address >> 20) & 0x7ff); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 110 | if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 111 | return -0x10UL; |
| 112 | if (unlikely(*table & _SEGMENT_ENTRY_LARGE)) { |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 113 | if (write && (*table & _SEGMENT_ENTRY_PROTECT)) |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 114 | return -0x04UL; |
| 115 | return (*table & _SEGMENT_ENTRY_ORIGIN_LARGE) + |
| 116 | (address & ~_SEGMENT_ENTRY_ORIGIN_LARGE); |
| 117 | } |
| 118 | table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 119 | } |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 120 | table = table + ((address >> 12) & 0xff); |
| 121 | if (unlikely(*table & _PAGE_INVALID)) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 122 | return -0x11UL; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 123 | if (write && (*table & _PAGE_PROTECT)) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 124 | return -0x04UL; |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 125 | return (*table & PAGE_MASK) + (address & ~PAGE_MASK); |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 128 | #else /* CONFIG_64BIT */ |
| 129 | |
| 130 | static unsigned long follow_table(struct mm_struct *mm, |
| 131 | unsigned long address, int write) |
| 132 | { |
| 133 | unsigned long *table = (unsigned long *)__pa(mm->pgd); |
| 134 | |
| 135 | table = table + ((address >> 20) & 0x7ff); |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 136 | if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 137 | return -0x10UL; |
| 138 | table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN); |
| 139 | table = table + ((address >> 12) & 0xff); |
| 140 | if (unlikely(*table & _PAGE_INVALID)) |
| 141 | return -0x11UL; |
Martin Schwidefsky | e509861 | 2013-07-23 20:57:57 +0200 | [diff] [blame] | 142 | if (write && (*table & _PAGE_PROTECT)) |
Heiko Carstens | ea81531 | 2013-03-21 12:50:39 +0100 | [diff] [blame] | 143 | return -0x04UL; |
| 144 | return (*table & PAGE_MASK) + (address & ~PAGE_MASK); |
| 145 | } |
| 146 | |
| 147 | #endif /* CONFIG_64BIT */ |
| 148 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 149 | static inline unsigned long __user_copy_pt(unsigned long uaddr, void *kptr, |
| 150 | unsigned long n, int write_user) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 151 | { |
| 152 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 153 | unsigned long offset, done, size, kaddr; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 154 | void *from, *to; |
| 155 | |
Heiko Carstens | b03b467 | 2014-01-17 13:12:34 +0100 | [diff] [blame] | 156 | if (!mm) |
| 157 | return n; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 158 | done = 0; |
| 159 | retry: |
| 160 | spin_lock(&mm->page_table_lock); |
| 161 | do { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 162 | kaddr = follow_table(mm, uaddr, write_user); |
| 163 | if (IS_ERR_VALUE(kaddr)) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 164 | goto fault; |
| 165 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 166 | offset = uaddr & ~PAGE_MASK; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 167 | size = min(n - done, PAGE_SIZE - offset); |
| 168 | if (write_user) { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 169 | to = (void *) kaddr; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 170 | from = kptr + done; |
| 171 | } else { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 172 | from = (void *) kaddr; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 173 | to = kptr + done; |
| 174 | } |
| 175 | memcpy(to, from, size); |
| 176 | done += size; |
| 177 | uaddr += size; |
| 178 | } while (done < n); |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 179 | spin_unlock(&mm->page_table_lock); |
| 180 | return n - done; |
| 181 | fault: |
| 182 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 183 | if (__handle_fault(uaddr, -kaddr, write_user)) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 184 | return n - done; |
| 185 | goto retry; |
| 186 | } |
| 187 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 188 | /* |
| 189 | * Do DAT for user address by page table walk, return kernel address. |
| 190 | * This function needs to be called with current->mm->page_table_lock held. |
| 191 | */ |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 192 | static inline unsigned long __dat_user_addr(unsigned long uaddr, int write) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 193 | { |
| 194 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 195 | unsigned long kaddr; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 196 | int rc; |
| 197 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 198 | retry: |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 199 | kaddr = follow_table(mm, uaddr, write); |
| 200 | if (IS_ERR_VALUE(kaddr)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 201 | goto fault; |
| 202 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 203 | return kaddr; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 204 | fault: |
| 205 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 206 | rc = __handle_fault(uaddr, -kaddr, write); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 207 | spin_lock(&mm->page_table_lock); |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 208 | if (!rc) |
| 209 | goto retry; |
| 210 | return 0; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 213 | unsigned long copy_from_user_pt(void *to, const void __user *from, unsigned long n) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 214 | { |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 215 | unsigned long rc; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 216 | |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 217 | if (segment_eq(get_fs(), KERNEL_DS)) |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 218 | return copy_in_kernel((void __user *) to, from, n); |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 219 | rc = __user_copy_pt((unsigned long) from, to, n, 0); |
| 220 | if (unlikely(rc)) |
| 221 | memset(to + n - rc, 0, rc); |
| 222 | return rc; |
| 223 | } |
| 224 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 225 | unsigned long copy_to_user_pt(void __user *to, const void *from, unsigned long n) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 226 | { |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 227 | if (segment_eq(get_fs(), KERNEL_DS)) |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 228 | return copy_in_kernel(to, (void __user *) from, n); |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 229 | return __user_copy_pt((unsigned long) to, (void *) from, n, 1); |
| 230 | } |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 231 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 232 | unsigned long clear_user_pt(void __user *to, unsigned long n) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 233 | { |
Heiko Carstens | b7fef2d | 2013-03-21 08:24:11 +0100 | [diff] [blame] | 234 | void *zpage = (void *) empty_zero_page; |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 235 | unsigned long done, size, ret; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 236 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 237 | done = 0; |
| 238 | do { |
| 239 | if (n - done > PAGE_SIZE) |
| 240 | size = PAGE_SIZE; |
| 241 | else |
| 242 | size = n - done; |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 243 | if (segment_eq(get_fs(), KERNEL_DS)) |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 244 | ret = copy_in_kernel(to, (void __user *) zpage, n); |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 245 | else |
| 246 | ret = __user_copy_pt((unsigned long) to, zpage, size, 1); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 247 | done += size; |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 248 | to += size; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 249 | if (ret) |
| 250 | return ret + n - done; |
| 251 | } while (done < n); |
| 252 | return 0; |
| 253 | } |
| 254 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 255 | unsigned long strnlen_user_pt(const char __user *src, unsigned long count) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 256 | { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 257 | unsigned long uaddr = (unsigned long) src; |
| 258 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 259 | unsigned long offset, done, len, kaddr; |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 260 | unsigned long len_str; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 261 | |
Heiko Carstens | f45655f | 2013-02-21 13:30:42 +0100 | [diff] [blame] | 262 | if (unlikely(!count)) |
| 263 | return 0; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 264 | if (segment_eq(get_fs(), KERNEL_DS)) |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 265 | return strnlen_kernel(src, count); |
Heiko Carstens | b03b467 | 2014-01-17 13:12:34 +0100 | [diff] [blame] | 266 | if (!mm) |
| 267 | return 0; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 268 | done = 0; |
| 269 | retry: |
| 270 | spin_lock(&mm->page_table_lock); |
| 271 | do { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 272 | kaddr = follow_table(mm, uaddr, 0); |
| 273 | if (IS_ERR_VALUE(kaddr)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 274 | goto fault; |
| 275 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 276 | offset = uaddr & ~PAGE_MASK; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 277 | len = min(count - done, PAGE_SIZE - offset); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 278 | len_str = strnlen((char *) kaddr, len); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 279 | done += len_str; |
| 280 | uaddr += len_str; |
| 281 | } while ((len_str == len) && (done < count)); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 282 | spin_unlock(&mm->page_table_lock); |
| 283 | return done + 1; |
| 284 | fault: |
| 285 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 286 | if (__handle_fault(uaddr, -kaddr, 0)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 287 | return 0; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 288 | goto retry; |
| 289 | } |
| 290 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 291 | long strncpy_from_user_pt(char *dst, const char __user *src, long count) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 292 | { |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 293 | unsigned long done, len, offset, len_str; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 294 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 295 | if (unlikely(count <= 0)) |
Heiko Carstens | f45655f | 2013-02-21 13:30:42 +0100 | [diff] [blame] | 296 | return 0; |
Heiko Carstens | 225cf8d | 2013-02-25 07:24:20 +0100 | [diff] [blame] | 297 | done = 0; |
| 298 | do { |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 299 | offset = (unsigned long)src & ~PAGE_MASK; |
Heiko Carstens | 225cf8d | 2013-02-25 07:24:20 +0100 | [diff] [blame] | 300 | len = min(count - done, PAGE_SIZE - offset); |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 301 | if (segment_eq(get_fs(), KERNEL_DS)) { |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 302 | if (copy_in_kernel((void __user *) dst, src, len)) |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 303 | return -EFAULT; |
| 304 | } else { |
| 305 | if (__user_copy_pt((unsigned long) src, dst, len, 0)) |
| 306 | return -EFAULT; |
| 307 | } |
Heiko Carstens | 225cf8d | 2013-02-25 07:24:20 +0100 | [diff] [blame] | 308 | len_str = strnlen(dst, len); |
| 309 | done += len_str; |
| 310 | src += len_str; |
| 311 | dst += len_str; |
| 312 | } while ((len_str == len) && (done < count)); |
| 313 | return done; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 316 | unsigned long copy_in_user_pt(void __user *to, const void __user *from, |
| 317 | unsigned long n) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 318 | { |
| 319 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 320 | unsigned long offset_max, uaddr, done, size, error_code; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 321 | unsigned long uaddr_from = (unsigned long) from; |
| 322 | unsigned long uaddr_to = (unsigned long) to; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 323 | unsigned long kaddr_to, kaddr_from; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 324 | int write_user; |
| 325 | |
Heiko Carstens | 066c437 | 2013-02-25 09:10:23 +0100 | [diff] [blame] | 326 | if (segment_eq(get_fs(), KERNEL_DS)) |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 327 | return copy_in_kernel(to, from, n); |
Heiko Carstens | b03b467 | 2014-01-17 13:12:34 +0100 | [diff] [blame] | 328 | if (!mm) |
| 329 | return n; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 330 | done = 0; |
| 331 | retry: |
| 332 | spin_lock(&mm->page_table_lock); |
| 333 | do { |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 334 | write_user = 0; |
| 335 | uaddr = uaddr_from; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 336 | kaddr_from = follow_table(mm, uaddr_from, 0); |
| 337 | error_code = kaddr_from; |
| 338 | if (IS_ERR_VALUE(error_code)) |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 339 | goto fault; |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 340 | |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 341 | write_user = 1; |
| 342 | uaddr = uaddr_to; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 343 | kaddr_to = follow_table(mm, uaddr_to, 1); |
| 344 | error_code = (unsigned long) kaddr_to; |
| 345 | if (IS_ERR_VALUE(error_code)) |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 346 | goto fault; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 347 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 348 | offset_max = max(uaddr_from & ~PAGE_MASK, |
| 349 | uaddr_to & ~PAGE_MASK); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 350 | size = min(n - done, PAGE_SIZE - offset_max); |
| 351 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 352 | memcpy((void *) kaddr_to, (void *) kaddr_from, size); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 353 | done += size; |
| 354 | uaddr_from += size; |
| 355 | uaddr_to += size; |
| 356 | } while (done < n); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 357 | spin_unlock(&mm->page_table_lock); |
| 358 | return n - done; |
| 359 | fault: |
| 360 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 361 | if (__handle_fault(uaddr, -error_code, write_user)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 362 | return n - done; |
| 363 | goto retry; |
| 364 | } |
| 365 | |
| 366 | #define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \ |
| 367 | asm volatile("0: l %1,0(%6)\n" \ |
| 368 | "1: " insn \ |
| 369 | "2: cs %1,%2,0(%6)\n" \ |
| 370 | "3: jl 1b\n" \ |
| 371 | " lhi %0,0\n" \ |
| 372 | "4:\n" \ |
| 373 | EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b) \ |
| 374 | : "=d" (ret), "=&d" (oldval), "=&d" (newval), \ |
| 375 | "=m" (*uaddr) \ |
| 376 | : "0" (-EFAULT), "d" (oparg), "a" (uaddr), \ |
| 377 | "m" (*uaddr) : "cc" ); |
| 378 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 379 | static int __futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 380 | { |
| 381 | int oldval = 0, newval, ret; |
| 382 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 383 | switch (op) { |
| 384 | case FUTEX_OP_SET: |
| 385 | __futex_atomic_op("lr %2,%5\n", |
| 386 | ret, oldval, newval, uaddr, oparg); |
| 387 | break; |
| 388 | case FUTEX_OP_ADD: |
| 389 | __futex_atomic_op("lr %2,%1\nar %2,%5\n", |
| 390 | ret, oldval, newval, uaddr, oparg); |
| 391 | break; |
| 392 | case FUTEX_OP_OR: |
| 393 | __futex_atomic_op("lr %2,%1\nor %2,%5\n", |
| 394 | ret, oldval, newval, uaddr, oparg); |
| 395 | break; |
| 396 | case FUTEX_OP_ANDN: |
| 397 | __futex_atomic_op("lr %2,%1\nnr %2,%5\n", |
| 398 | ret, oldval, newval, uaddr, oparg); |
| 399 | break; |
| 400 | case FUTEX_OP_XOR: |
| 401 | __futex_atomic_op("lr %2,%1\nxr %2,%5\n", |
| 402 | ret, oldval, newval, uaddr, oparg); |
| 403 | break; |
| 404 | default: |
| 405 | ret = -ENOSYS; |
| 406 | } |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 407 | if (ret == 0) |
| 408 | *old = oldval; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 409 | return ret; |
| 410 | } |
| 411 | |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 412 | int __futex_atomic_op_inuser(int op, u32 __user *uaddr, int oparg, int *old) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 413 | { |
| 414 | int ret; |
| 415 | |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 416 | if (segment_eq(get_fs(), KERNEL_DS)) |
| 417 | return __futex_atomic_op_pt(op, uaddr, oparg, old); |
Heiko Carstens | b03b467 | 2014-01-17 13:12:34 +0100 | [diff] [blame] | 418 | if (unlikely(!current->mm)) |
| 419 | return -EFAULT; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 420 | spin_lock(¤t->mm->page_table_lock); |
Martin Schwidefsky | 3c52e49 | 2011-10-30 15:17:15 +0100 | [diff] [blame] | 421 | uaddr = (u32 __force __user *) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 422 | __dat_user_addr((__force unsigned long) uaddr, 1); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 423 | if (!uaddr) { |
| 424 | spin_unlock(¤t->mm->page_table_lock); |
| 425 | return -EFAULT; |
| 426 | } |
| 427 | get_page(virt_to_page(uaddr)); |
| 428 | spin_unlock(¤t->mm->page_table_lock); |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 429 | ret = __futex_atomic_op_pt(op, uaddr, oparg, old); |
| 430 | put_page(virt_to_page(uaddr)); |
| 431 | return ret; |
| 432 | } |
| 433 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 434 | static int __futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr, |
| 435 | u32 oldval, u32 newval) |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 436 | { |
| 437 | int ret; |
| 438 | |
| 439 | asm volatile("0: cs %1,%4,0(%5)\n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 440 | "1: la %0,0\n" |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 441 | "2:\n" |
| 442 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 443 | : "=d" (ret), "+d" (oldval), "=m" (*uaddr) |
| 444 | : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr) |
| 445 | : "cc", "memory" ); |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 446 | *uval = oldval; |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 447 | return ret; |
| 448 | } |
| 449 | |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 450 | int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 451 | u32 oldval, u32 newval) |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 452 | { |
| 453 | int ret; |
| 454 | |
| 455 | if (segment_eq(get_fs(), KERNEL_DS)) |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 456 | return __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval); |
Heiko Carstens | b03b467 | 2014-01-17 13:12:34 +0100 | [diff] [blame] | 457 | if (unlikely(!current->mm)) |
| 458 | return -EFAULT; |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 459 | spin_lock(¤t->mm->page_table_lock); |
Martin Schwidefsky | 3c52e49 | 2011-10-30 15:17:15 +0100 | [diff] [blame] | 460 | uaddr = (u32 __force __user *) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 461 | __dat_user_addr((__force unsigned long) uaddr, 1); |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 462 | if (!uaddr) { |
| 463 | spin_unlock(¤t->mm->page_table_lock); |
| 464 | return -EFAULT; |
| 465 | } |
| 466 | get_page(virt_to_page(uaddr)); |
| 467 | spin_unlock(¤t->mm->page_table_lock); |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 468 | ret = __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 469 | put_page(virt_to_page(uaddr)); |
| 470 | return ret; |
| 471 | } |