treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / um / kernel / skas / uaccess.c
blobd617f8dc9c19d67abed02a125e4c5e1ef5e33c2f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 */
6 #include <linux/err.h>
7 #include <linux/highmem.h>
8 #include <linux/mm.h>
9 #include <linux/module.h>
10 #include <linux/sched.h>
11 #include <asm/current.h>
12 #include <asm/page.h>
13 #include <asm/pgtable.h>
14 #include <kern_util.h>
15 #include <os.h>
17 pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr)
19 pgd_t *pgd;
20 p4d_t *p4d;
21 pud_t *pud;
22 pmd_t *pmd;
24 if (mm == NULL)
25 return NULL;
27 pgd = pgd_offset(mm, addr);
28 if (!pgd_present(*pgd))
29 return NULL;
31 p4d = p4d_offset(pgd, addr);
32 if (!p4d_present(*p4d))
33 return NULL;
35 pud = pud_offset(p4d, addr);
36 if (!pud_present(*pud))
37 return NULL;
39 pmd = pmd_offset(pud, addr);
40 if (!pmd_present(*pmd))
41 return NULL;
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);
49 int err, dummy_code;
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);
54 if (err)
55 return NULL;
56 pte = virt_to_pte(current->mm, virt);
58 if (!pte_present(*pte))
59 pte = NULL;
61 return 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)
67 struct page *page;
68 pte_t *pte;
69 int n;
71 pte = maybe_map(addr, is_write);
72 if (pte == NULL)
73 return -1;
75 page = pte_page(*pte);
76 #ifdef CONFIG_64BIT
77 pagefault_disable();
78 addr = (unsigned long) page_address(page) +
79 (addr & ~PAGE_MASK);
80 #else
81 addr = (unsigned long) kmap_atomic(page) +
82 (addr & ~PAGE_MASK);
83 #endif
84 n = (*op)(addr, len, arg);
86 #ifdef CONFIG_64BIT
87 pagefault_enable();
88 #else
89 kunmap_atomic((void *)addr);
90 #endif
92 return n;
95 static long buffer_op(unsigned long addr, int len, int is_write,
96 int (*op)(unsigned long, int, void *), void *arg)
98 long size, remain, n;
100 size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
101 remain = len;
103 n = do_op_one_page(addr, size, is_write, op, arg);
104 if (n != 0) {
105 remain = (n < 0 ? remain : 0);
106 goto out;
109 addr += size;
110 remain -= size;
111 if (remain == 0)
112 goto out;
114 while (addr < ((addr + remain) & PAGE_MASK)) {
115 n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
116 if (n != 0) {
117 remain = (n < 0 ? remain : 0);
118 goto out;
121 addr += PAGE_SIZE;
122 remain -= PAGE_SIZE;
124 if (remain == 0)
125 goto out;
127 n = do_op_one_page(addr, remain, is_write, op, arg);
128 if (n != 0) {
129 remain = (n < 0 ? remain : 0);
130 goto out;
133 return 0;
134 out:
135 return remain;
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);
143 *to_ptr += len;
144 return 0;
147 unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
149 if (uaccess_kernel()) {
150 memcpy(to, (__force void*)from, n);
151 return 0;
154 return buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to);
156 EXPORT_SYMBOL(raw_copy_from_user);
158 static int copy_chunk_to_user(unsigned long to, int len, void *arg)
160 unsigned long *from_ptr = arg, from = *from_ptr;
162 memcpy((void *) to, (void *) from, len);
163 *from_ptr += len;
164 return 0;
167 unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
169 if (uaccess_kernel()) {
170 memcpy((__force void *) to, from, n);
171 return 0;
174 return buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from);
176 EXPORT_SYMBOL(raw_copy_to_user);
178 static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
180 char **to_ptr = arg, *to = *to_ptr;
181 int n;
183 strncpy(to, (void *) from, len);
184 n = strnlen(to, len);
185 *to_ptr += n;
187 if (n < len)
188 return 1;
189 return 0;
192 long __strncpy_from_user(char *dst, const char __user *src, long count)
194 long n;
195 char *ptr = dst;
197 if (uaccess_kernel()) {
198 strncpy(dst, (__force void *) src, count);
199 return strnlen(dst, count);
202 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
203 &ptr);
204 if (n != 0)
205 return -EFAULT;
206 return strnlen(dst, count);
208 EXPORT_SYMBOL(__strncpy_from_user);
210 static int clear_chunk(unsigned long addr, int len, void *unused)
212 memset((void *) addr, 0, len);
213 return 0;
216 unsigned long __clear_user(void __user *mem, unsigned long len)
218 if (uaccess_kernel()) {
219 memset((__force void*)mem, 0, len);
220 return 0;
223 return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
225 EXPORT_SYMBOL(__clear_user);
227 static int strnlen_chunk(unsigned long str, int len, void *arg)
229 int *len_ptr = arg, n;
231 n = strnlen((void *) str, len);
232 *len_ptr += n;
234 if (n < len)
235 return 1;
236 return 0;
239 long __strnlen_user(const void __user *str, long len)
241 int count = 0, n;
243 if (uaccess_kernel())
244 return strnlen((__force char*)str, len) + 1;
246 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
247 if (n == 0)
248 return count + 1;
249 return 0;
251 EXPORT_SYMBOL(__strnlen_user);