1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
7 #include <linux/highmem.h>
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <asm/current.h>
13 #include <kern_util.h>
14 #include <asm/futex.h>
17 pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr)
27 pgd = pgd_offset(mm, addr);
28 if (!pgd_present(*pgd))
31 p4d = p4d_offset(pgd, addr);
32 if (!p4d_present(*p4d))
35 pud = pud_offset(p4d, addr);
36 if (!pud_present(*pud))
39 pmd = pmd_offset(pud, addr);
40 if (!pmd_present(*pmd))
43 return pte_offset_kernel(pmd, addr);
46 static pte_t *maybe_map(unsigned long virt, int is_write)
48 pte_t *pte = virt_to_pte(current->mm, virt);
51 if ((pte == NULL) || !pte_present(*pte) ||
52 (is_write && !pte_write(*pte))) {
53 err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
56 pte = virt_to_pte(current->mm, virt);
58 if (!pte_present(*pte))
64 static int do_op_one_page(unsigned long addr, int len, int is_write,
65 int (*op)(unsigned long addr, int len, void *arg), void *arg)
71 pte = maybe_map(addr, is_write);
75 page = pte_page(*pte);
78 addr = (unsigned long) page_address(page) +
81 addr = (unsigned long) kmap_atomic(page) +
84 n = (*op)(addr, len, arg);
89 kunmap_atomic((void *)addr);
95 static long buffer_op(unsigned long addr, int len, int is_write,
96 int (*op)(unsigned long, int, void *), void *arg)
100 size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
103 n = do_op_one_page(addr, size, is_write, op, arg);
105 remain = (n < 0 ? remain : 0);
114 while (addr < ((addr + remain) & PAGE_MASK)) {
115 n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
117 remain = (n < 0 ? remain : 0);
127 n = do_op_one_page(addr, remain, is_write, op, arg);
129 remain = (n < 0 ? remain : 0);
138 static int copy_chunk_from_user(unsigned long from, int len, void *arg)
140 unsigned long *to_ptr = arg, to = *to_ptr;
142 memcpy((void *) to, (void *) from, len);
147 unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
149 return buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to);
151 EXPORT_SYMBOL(raw_copy_from_user);
153 static int copy_chunk_to_user(unsigned long to, int len, void *arg)
155 unsigned long *from_ptr = arg, from = *from_ptr;
157 memcpy((void *) to, (void *) from, len);
162 unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
164 return buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from);
166 EXPORT_SYMBOL(raw_copy_to_user);
168 static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
170 char **to_ptr = arg, *to = *to_ptr;
173 strncpy(to, (void *) from, len);
174 n = strnlen(to, len);
182 long strncpy_from_user(char *dst, const char __user *src, long count)
187 if (!access_ok(src, 1))
189 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
193 return strnlen(dst, count);
195 EXPORT_SYMBOL(strncpy_from_user);
197 static int clear_chunk(unsigned long addr, int len, void *unused)
199 memset((void *) addr, 0, len);
203 unsigned long __clear_user(void __user *mem, unsigned long len)
205 return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
207 EXPORT_SYMBOL(__clear_user);
209 static int strnlen_chunk(unsigned long str, int len, void *arg)
211 int *len_ptr = arg, n;
213 n = strnlen((void *) str, len);
221 long strnlen_user(const char __user *str, long len)
225 if (!access_ok(str, 1))
227 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
232 EXPORT_SYMBOL(strnlen_user);
235 * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant
236 * argument and comparison of the previous
237 * futex value with another constant.
239 * @encoded_op: encoded operation to execute
240 * @uaddr: pointer to user space address
244 * -EFAULT - User access resulted in a page fault
245 * -EAGAIN - Atomic operation was unable to complete due to contention
246 * -ENOSYS - Operation not supported
249 int arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
253 unsigned long addr = (unsigned long) uaddr;
257 if (!access_ok(uaddr, sizeof(*uaddr)))
260 pte = maybe_map(addr, 1);
264 page = pte_page(*pte);
267 addr = (unsigned long) page_address(page) +
268 (((unsigned long) addr) & ~PAGE_MASK);
270 addr = (unsigned long) kmap_atomic(page) +
271 ((unsigned long) addr & ~PAGE_MASK);
273 uaddr = (u32 *) addr;
300 kunmap_atomic((void *)addr);
311 EXPORT_SYMBOL(arch_futex_atomic_op_inuser);
314 * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
315 * uaddr with newval if the current value is
317 * @uval: pointer to store content of @uaddr
318 * @uaddr: pointer to user space address
320 * @newval: new value to store to @uaddr
324 * -EFAULT - User access resulted in a page fault
325 * -EAGAIN - Atomic operation was unable to complete due to contention
328 int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
329 u32 oldval, u32 newval)
335 if (!access_ok(uaddr, sizeof(*uaddr)))
339 pte = maybe_map((unsigned long) uaddr, 1);
343 page = pte_page(*pte);
346 uaddr = page_address(page) + (((unsigned long) uaddr) & ~PAGE_MASK);
348 uaddr = kmap_atomic(page) + ((unsigned long) uaddr & ~PAGE_MASK);
353 ret = cmpxchg(uaddr, oldval, newval);
358 kunmap_atomic(uaddr);
366 EXPORT_SYMBOL(futex_atomic_cmpxchg_inatomic);