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 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * Returns kernel address for user virtual address. If the returned address is |
| 20 | * >= -4095 (IS_ERR_VALUE(x) returns true), a fault has occured and the address |
| 21 | * contains the (negative) exception code. |
| 22 | */ |
| 23 | static __always_inline unsigned long follow_table(struct mm_struct *mm, |
| 24 | unsigned long addr, int write) |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 25 | { |
| 26 | pgd_t *pgd; |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 27 | pud_t *pud; |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 28 | pmd_t *pmd; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 29 | pte_t *ptep; |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 30 | |
| 31 | pgd = pgd_offset(mm, addr); |
| 32 | if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd))) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 33 | return -0x3aUL; |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 34 | |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 35 | pud = pud_offset(pgd, addr); |
| 36 | if (pud_none(*pud) || unlikely(pud_bad(*pud))) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 37 | return -0x3bUL; |
Martin Schwidefsky | 190a1d7 | 2007-10-22 12:52:48 +0200 | [diff] [blame] | 38 | |
| 39 | pmd = pmd_offset(pud, addr); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 40 | if (pmd_none(*pmd)) |
| 41 | return -0x10UL; |
Gerald Schaefer | 156152f | 2012-10-25 17:24:12 +0200 | [diff] [blame] | 42 | if (pmd_large(*pmd)) { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 43 | if (write && (pmd_val(*pmd) & _SEGMENT_ENTRY_RO)) |
| 44 | return -0x04UL; |
| 45 | return (pmd_val(*pmd) & HPAGE_MASK) + (addr & ~HPAGE_MASK); |
| 46 | } |
| 47 | if (unlikely(pmd_bad(*pmd))) |
| 48 | return -0x10UL; |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 49 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 50 | ptep = pte_offset_map(pmd, addr); |
| 51 | if (!pte_present(*ptep)) |
| 52 | return -0x11UL; |
Martin Schwidefsky | abf09be | 2012-11-07 13:17:37 +0100 | [diff] [blame] | 53 | if (write && (!pte_write(*ptep) || !pte_dirty(*ptep))) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 54 | return -0x04UL; |
| 55 | |
| 56 | return (pte_val(*ptep) & PAGE_MASK) + (addr & ~PAGE_MASK); |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 59 | static __always_inline size_t __user_copy_pt(unsigned long uaddr, void *kptr, |
| 60 | size_t n, int write_user) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 61 | { |
| 62 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 63 | unsigned long offset, done, size, kaddr; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 64 | void *from, *to; |
| 65 | |
| 66 | done = 0; |
| 67 | retry: |
| 68 | spin_lock(&mm->page_table_lock); |
| 69 | do { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 70 | kaddr = follow_table(mm, uaddr, write_user); |
| 71 | if (IS_ERR_VALUE(kaddr)) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 72 | goto fault; |
| 73 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 74 | offset = uaddr & ~PAGE_MASK; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 75 | size = min(n - done, PAGE_SIZE - offset); |
| 76 | if (write_user) { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 77 | to = (void *) kaddr; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 78 | from = kptr + done; |
| 79 | } else { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 80 | from = (void *) kaddr; |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 81 | to = kptr + done; |
| 82 | } |
| 83 | memcpy(to, from, size); |
| 84 | done += size; |
| 85 | uaddr += size; |
| 86 | } while (done < n); |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 87 | spin_unlock(&mm->page_table_lock); |
| 88 | return n - done; |
| 89 | fault: |
| 90 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 91 | if (__handle_fault(uaddr, -kaddr, write_user)) |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 92 | return n - done; |
| 93 | goto retry; |
| 94 | } |
| 95 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 96 | /* |
| 97 | * Do DAT for user address by page table walk, return kernel address. |
| 98 | * This function needs to be called with current->mm->page_table_lock held. |
| 99 | */ |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 100 | static __always_inline unsigned long __dat_user_addr(unsigned long uaddr, |
| 101 | int write) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 102 | { |
| 103 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 104 | unsigned long kaddr; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 105 | int rc; |
| 106 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 107 | retry: |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 108 | kaddr = follow_table(mm, uaddr, write); |
| 109 | if (IS_ERR_VALUE(kaddr)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 110 | goto fault; |
| 111 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 112 | return kaddr; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 113 | fault: |
| 114 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 115 | rc = __handle_fault(uaddr, -kaddr, write); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 116 | spin_lock(&mm->page_table_lock); |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 117 | if (!rc) |
| 118 | goto retry; |
| 119 | return 0; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Gerald Schaefer | 59f35d5 | 2006-12-04 15:40:45 +0100 | [diff] [blame] | 122 | size_t copy_from_user_pt(size_t n, const void __user *from, void *to) |
| 123 | { |
| 124 | size_t rc; |
| 125 | |
| 126 | if (segment_eq(get_fs(), KERNEL_DS)) { |
| 127 | memcpy(to, (void __kernel __force *) from, n); |
| 128 | return 0; |
| 129 | } |
| 130 | rc = __user_copy_pt((unsigned long) from, to, n, 0); |
| 131 | if (unlikely(rc)) |
| 132 | memset(to + n - rc, 0, rc); |
| 133 | return rc; |
| 134 | } |
| 135 | |
| 136 | size_t copy_to_user_pt(size_t n, void __user *to, const void *from) |
| 137 | { |
| 138 | if (segment_eq(get_fs(), KERNEL_DS)) { |
| 139 | memcpy((void __kernel __force *) to, from, n); |
| 140 | return 0; |
| 141 | } |
| 142 | return __user_copy_pt((unsigned long) to, (void *) from, n, 1); |
| 143 | } |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 144 | |
| 145 | static size_t clear_user_pt(size_t n, void __user *to) |
| 146 | { |
| 147 | long done, size, ret; |
| 148 | |
| 149 | if (segment_eq(get_fs(), KERNEL_DS)) { |
| 150 | memset((void __kernel __force *) to, 0, n); |
| 151 | return 0; |
| 152 | } |
| 153 | done = 0; |
| 154 | do { |
| 155 | if (n - done > PAGE_SIZE) |
| 156 | size = PAGE_SIZE; |
| 157 | else |
| 158 | size = n - done; |
| 159 | ret = __user_copy_pt((unsigned long) to + done, |
| 160 | &empty_zero_page, size, 1); |
| 161 | done += size; |
| 162 | if (ret) |
| 163 | return ret + n - done; |
| 164 | } while (done < n); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static size_t strnlen_user_pt(size_t count, const char __user *src) |
| 169 | { |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 170 | unsigned long uaddr = (unsigned long) src; |
| 171 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 172 | unsigned long offset, done, len, kaddr; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 173 | size_t len_str; |
| 174 | |
| 175 | if (segment_eq(get_fs(), KERNEL_DS)) |
| 176 | return strnlen((const char __kernel __force *) src, count) + 1; |
| 177 | done = 0; |
| 178 | retry: |
| 179 | spin_lock(&mm->page_table_lock); |
| 180 | do { |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 181 | kaddr = follow_table(mm, uaddr, 0); |
| 182 | if (IS_ERR_VALUE(kaddr)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 183 | goto fault; |
| 184 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 185 | offset = uaddr & ~PAGE_MASK; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 186 | len = min(count - done, PAGE_SIZE - offset); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 187 | len_str = strnlen((char *) kaddr, len); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 188 | done += len_str; |
| 189 | uaddr += len_str; |
| 190 | } while ((len_str == len) && (done < count)); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 191 | spin_unlock(&mm->page_table_lock); |
| 192 | return done + 1; |
| 193 | fault: |
| 194 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 195 | if (__handle_fault(uaddr, -kaddr, 0)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 196 | return 0; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 197 | goto retry; |
| 198 | } |
| 199 | |
| 200 | static size_t strncpy_from_user_pt(size_t count, const char __user *src, |
| 201 | char *dst) |
| 202 | { |
| 203 | size_t n = strnlen_user_pt(count, src); |
| 204 | |
| 205 | if (!n) |
| 206 | return -EFAULT; |
| 207 | if (n > count) |
| 208 | n = count; |
| 209 | if (segment_eq(get_fs(), KERNEL_DS)) { |
| 210 | memcpy(dst, (const char __kernel __force *) src, n); |
| 211 | if (dst[n-1] == '\0') |
| 212 | return n-1; |
| 213 | else |
| 214 | return n; |
| 215 | } |
| 216 | if (__user_copy_pt((unsigned long) src, dst, n, 0)) |
| 217 | return -EFAULT; |
| 218 | if (dst[n-1] == '\0') |
| 219 | return n-1; |
| 220 | else |
| 221 | return n; |
| 222 | } |
| 223 | |
| 224 | static size_t copy_in_user_pt(size_t n, void __user *to, |
| 225 | const void __user *from) |
| 226 | { |
| 227 | struct mm_struct *mm = current->mm; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 228 | unsigned long offset_max, uaddr, done, size, error_code; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 229 | unsigned long uaddr_from = (unsigned long) from; |
| 230 | unsigned long uaddr_to = (unsigned long) to; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 231 | unsigned long kaddr_to, kaddr_from; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 232 | int write_user; |
| 233 | |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 234 | if (segment_eq(get_fs(), KERNEL_DS)) { |
| 235 | memcpy((void __force *) to, (void __force *) from, n); |
| 236 | return 0; |
| 237 | } |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 238 | done = 0; |
| 239 | retry: |
| 240 | spin_lock(&mm->page_table_lock); |
| 241 | do { |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 242 | write_user = 0; |
| 243 | uaddr = uaddr_from; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 244 | kaddr_from = follow_table(mm, uaddr_from, 0); |
| 245 | error_code = kaddr_from; |
| 246 | if (IS_ERR_VALUE(error_code)) |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 247 | goto fault; |
Martin Schwidefsky | e4aa402 | 2007-10-22 12:52:46 +0200 | [diff] [blame] | 248 | |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 249 | write_user = 1; |
| 250 | uaddr = uaddr_to; |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 251 | kaddr_to = follow_table(mm, uaddr_to, 1); |
| 252 | error_code = (unsigned long) kaddr_to; |
| 253 | if (IS_ERR_VALUE(error_code)) |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 254 | goto fault; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 255 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 256 | offset_max = max(uaddr_from & ~PAGE_MASK, |
| 257 | uaddr_to & ~PAGE_MASK); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 258 | size = min(n - done, PAGE_SIZE - offset_max); |
| 259 | |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 260 | memcpy((void *) kaddr_to, (void *) kaddr_from, size); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 261 | done += size; |
| 262 | uaddr_from += size; |
| 263 | uaddr_to += size; |
| 264 | } while (done < n); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 265 | spin_unlock(&mm->page_table_lock); |
| 266 | return n - done; |
| 267 | fault: |
| 268 | spin_unlock(&mm->page_table_lock); |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 269 | if (__handle_fault(uaddr, -error_code, write_user)) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 270 | return n - done; |
| 271 | goto retry; |
| 272 | } |
| 273 | |
| 274 | #define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \ |
| 275 | asm volatile("0: l %1,0(%6)\n" \ |
| 276 | "1: " insn \ |
| 277 | "2: cs %1,%2,0(%6)\n" \ |
| 278 | "3: jl 1b\n" \ |
| 279 | " lhi %0,0\n" \ |
| 280 | "4:\n" \ |
| 281 | EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b) \ |
| 282 | : "=d" (ret), "=&d" (oldval), "=&d" (newval), \ |
| 283 | "=m" (*uaddr) \ |
| 284 | : "0" (-EFAULT), "d" (oparg), "a" (uaddr), \ |
| 285 | "m" (*uaddr) : "cc" ); |
| 286 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 287 | 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] | 288 | { |
| 289 | int oldval = 0, newval, ret; |
| 290 | |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 291 | switch (op) { |
| 292 | case FUTEX_OP_SET: |
| 293 | __futex_atomic_op("lr %2,%5\n", |
| 294 | ret, oldval, newval, uaddr, oparg); |
| 295 | break; |
| 296 | case FUTEX_OP_ADD: |
| 297 | __futex_atomic_op("lr %2,%1\nar %2,%5\n", |
| 298 | ret, oldval, newval, uaddr, oparg); |
| 299 | break; |
| 300 | case FUTEX_OP_OR: |
| 301 | __futex_atomic_op("lr %2,%1\nor %2,%5\n", |
| 302 | ret, oldval, newval, uaddr, oparg); |
| 303 | break; |
| 304 | case FUTEX_OP_ANDN: |
| 305 | __futex_atomic_op("lr %2,%1\nnr %2,%5\n", |
| 306 | ret, oldval, newval, uaddr, oparg); |
| 307 | break; |
| 308 | case FUTEX_OP_XOR: |
| 309 | __futex_atomic_op("lr %2,%1\nxr %2,%5\n", |
| 310 | ret, oldval, newval, uaddr, oparg); |
| 311 | break; |
| 312 | default: |
| 313 | ret = -ENOSYS; |
| 314 | } |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 315 | if (ret == 0) |
| 316 | *old = oldval; |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 317 | return ret; |
| 318 | } |
| 319 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 320 | 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] | 321 | { |
| 322 | int ret; |
| 323 | |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 324 | if (segment_eq(get_fs(), KERNEL_DS)) |
| 325 | return __futex_atomic_op_pt(op, uaddr, oparg, old); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 326 | spin_lock(¤t->mm->page_table_lock); |
Martin Schwidefsky | 3c52e49 | 2011-10-30 15:17:15 +0100 | [diff] [blame] | 327 | uaddr = (u32 __force __user *) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 328 | __dat_user_addr((__force unsigned long) uaddr, 1); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 329 | if (!uaddr) { |
| 330 | spin_unlock(¤t->mm->page_table_lock); |
| 331 | return -EFAULT; |
| 332 | } |
| 333 | get_page(virt_to_page(uaddr)); |
| 334 | spin_unlock(¤t->mm->page_table_lock); |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 335 | ret = __futex_atomic_op_pt(op, uaddr, oparg, old); |
| 336 | put_page(virt_to_page(uaddr)); |
| 337 | return ret; |
| 338 | } |
| 339 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 340 | static int __futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr, |
| 341 | u32 oldval, u32 newval) |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 342 | { |
| 343 | int ret; |
| 344 | |
| 345 | asm volatile("0: cs %1,%4,0(%5)\n" |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 346 | "1: la %0,0\n" |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 347 | "2:\n" |
| 348 | EX_TABLE(0b,2b) EX_TABLE(1b,2b) |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 349 | : "=d" (ret), "+d" (oldval), "=m" (*uaddr) |
| 350 | : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr) |
| 351 | : "cc", "memory" ); |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 352 | *uval = oldval; |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 353 | return ret; |
| 354 | } |
| 355 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 356 | int futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr, |
| 357 | u32 oldval, u32 newval) |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 358 | { |
| 359 | int ret; |
| 360 | |
| 361 | if (segment_eq(get_fs(), KERNEL_DS)) |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 362 | return __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval); |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 363 | spin_lock(¤t->mm->page_table_lock); |
Martin Schwidefsky | 3c52e49 | 2011-10-30 15:17:15 +0100 | [diff] [blame] | 364 | uaddr = (u32 __force __user *) |
Gerald Schaefer | 4db84d4 | 2012-09-10 16:43:26 +0200 | [diff] [blame] | 365 | __dat_user_addr((__force unsigned long) uaddr, 1); |
Heiko Carstens | 3f12ebc | 2008-04-17 07:46:27 +0200 | [diff] [blame] | 366 | if (!uaddr) { |
| 367 | spin_unlock(¤t->mm->page_table_lock); |
| 368 | return -EFAULT; |
| 369 | } |
| 370 | get_page(virt_to_page(uaddr)); |
| 371 | spin_unlock(¤t->mm->page_table_lock); |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 372 | ret = __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval); |
Gerald Schaefer | c1821c2 | 2007-02-05 21:18:17 +0100 | [diff] [blame] | 373 | put_page(virt_to_page(uaddr)); |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | struct uaccess_ops uaccess_pt = { |
| 378 | .copy_from_user = copy_from_user_pt, |
| 379 | .copy_from_user_small = copy_from_user_pt, |
| 380 | .copy_to_user = copy_to_user_pt, |
| 381 | .copy_to_user_small = copy_to_user_pt, |
| 382 | .copy_in_user = copy_in_user_pt, |
| 383 | .clear_user = clear_user_pt, |
| 384 | .strnlen_user = strnlen_user_pt, |
| 385 | .strncpy_from_user = strncpy_from_user_pt, |
| 386 | .futex_atomic_op = futex_atomic_op_pt, |
| 387 | .futex_atomic_cmpxchg = futex_atomic_cmpxchg_pt, |
| 388 | }; |