uaccess_pt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * arch/s390/lib/uaccess_pt.c
  3. *
  4. * User access functions based on page table walks for enhanced
  5. * system layout without hardware support.
  6. *
  7. * Copyright IBM Corp. 2006
  8. * Author(s): Gerald Schaefer (gerald.schaefer@de.ibm.com)
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/hardirq.h>
  12. #include <linux/mm.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/futex.h>
  15. #include "uaccess.h"
  16. static inline pte_t *follow_table(struct mm_struct *mm, unsigned long addr)
  17. {
  18. pgd_t *pgd;
  19. pud_t *pud;
  20. pmd_t *pmd;
  21. pgd = pgd_offset(mm, addr);
  22. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  23. return (pte_t *) 0x3a;
  24. pud = pud_offset(pgd, addr);
  25. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  26. return (pte_t *) 0x3b;
  27. pmd = pmd_offset(pud, addr);
  28. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  29. return (pte_t *) 0x10;
  30. return pte_offset_map(pmd, addr);
  31. }
  32. static __always_inline size_t __user_copy_pt(unsigned long uaddr, void *kptr,
  33. size_t n, int write_user)
  34. {
  35. struct mm_struct *mm = current->mm;
  36. unsigned long offset, pfn, done, size;
  37. pte_t *pte;
  38. void *from, *to;
  39. done = 0;
  40. retry:
  41. spin_lock(&mm->page_table_lock);
  42. do {
  43. pte = follow_table(mm, uaddr);
  44. if ((unsigned long) pte < 0x1000)
  45. goto fault;
  46. if (!pte_present(*pte)) {
  47. pte = (pte_t *) 0x11;
  48. goto fault;
  49. } else if (write_user && !pte_write(*pte)) {
  50. pte = (pte_t *) 0x04;
  51. goto fault;
  52. }
  53. pfn = pte_pfn(*pte);
  54. offset = uaddr & (PAGE_SIZE - 1);
  55. size = min(n - done, PAGE_SIZE - offset);
  56. if (write_user) {
  57. to = (void *)((pfn << PAGE_SHIFT) + offset);
  58. from = kptr + done;
  59. } else {
  60. from = (void *)((pfn << PAGE_SHIFT) + offset);
  61. to = kptr + done;
  62. }
  63. memcpy(to, from, size);
  64. done += size;
  65. uaddr += size;
  66. } while (done < n);
  67. spin_unlock(&mm->page_table_lock);
  68. return n - done;
  69. fault:
  70. spin_unlock(&mm->page_table_lock);
  71. if (__handle_fault(uaddr, (unsigned long) pte, write_user))
  72. return n - done;
  73. goto retry;
  74. }
  75. /*
  76. * Do DAT for user address by page table walk, return kernel address.
  77. * This function needs to be called with current->mm->page_table_lock held.
  78. */
  79. static __always_inline unsigned long __dat_user_addr(unsigned long uaddr)
  80. {
  81. struct mm_struct *mm = current->mm;
  82. unsigned long pfn;
  83. pte_t *pte;
  84. int rc;
  85. retry:
  86. pte = follow_table(mm, uaddr);
  87. if ((unsigned long) pte < 0x1000)
  88. goto fault;
  89. if (!pte_present(*pte)) {
  90. pte = (pte_t *) 0x11;
  91. goto fault;
  92. }
  93. pfn = pte_pfn(*pte);
  94. return (pfn << PAGE_SHIFT) + (uaddr & (PAGE_SIZE - 1));
  95. fault:
  96. spin_unlock(&mm->page_table_lock);
  97. rc = __handle_fault(uaddr, (unsigned long) pte, 0);
  98. spin_lock(&mm->page_table_lock);
  99. if (!rc)
  100. goto retry;
  101. return 0;
  102. }
  103. size_t copy_from_user_pt(size_t n, const void __user *from, void *to)
  104. {
  105. size_t rc;
  106. if (segment_eq(get_fs(), KERNEL_DS)) {
  107. memcpy(to, (void __kernel __force *) from, n);
  108. return 0;
  109. }
  110. rc = __user_copy_pt((unsigned long) from, to, n, 0);
  111. if (unlikely(rc))
  112. memset(to + n - rc, 0, rc);
  113. return rc;
  114. }
  115. size_t copy_to_user_pt(size_t n, void __user *to, const void *from)
  116. {
  117. if (segment_eq(get_fs(), KERNEL_DS)) {
  118. memcpy((void __kernel __force *) to, from, n);
  119. return 0;
  120. }
  121. return __user_copy_pt((unsigned long) to, (void *) from, n, 1);
  122. }
  123. static size_t clear_user_pt(size_t n, void __user *to)
  124. {
  125. long done, size, ret;
  126. if (segment_eq(get_fs(), KERNEL_DS)) {
  127. memset((void __kernel __force *) to, 0, n);
  128. return 0;
  129. }
  130. done = 0;
  131. do {
  132. if (n - done > PAGE_SIZE)
  133. size = PAGE_SIZE;
  134. else
  135. size = n - done;
  136. ret = __user_copy_pt((unsigned long) to + done,
  137. &empty_zero_page, size, 1);
  138. done += size;
  139. if (ret)
  140. return ret + n - done;
  141. } while (done < n);
  142. return 0;
  143. }
  144. static size_t strnlen_user_pt(size_t count, const char __user *src)
  145. {
  146. char *addr;
  147. unsigned long uaddr = (unsigned long) src;
  148. struct mm_struct *mm = current->mm;
  149. unsigned long offset, pfn, done, len;
  150. pte_t *pte;
  151. size_t len_str;
  152. if (segment_eq(get_fs(), KERNEL_DS))
  153. return strnlen((const char __kernel __force *) src, count) + 1;
  154. done = 0;
  155. retry:
  156. spin_lock(&mm->page_table_lock);
  157. do {
  158. pte = follow_table(mm, uaddr);
  159. if ((unsigned long) pte < 0x1000)
  160. goto fault;
  161. if (!pte_present(*pte)) {
  162. pte = (pte_t *) 0x11;
  163. goto fault;
  164. }
  165. pfn = pte_pfn(*pte);
  166. offset = uaddr & (PAGE_SIZE-1);
  167. addr = (char *)(pfn << PAGE_SHIFT) + offset;
  168. len = min(count - done, PAGE_SIZE - offset);
  169. len_str = strnlen(addr, len);
  170. done += len_str;
  171. uaddr += len_str;
  172. } while ((len_str == len) && (done < count));
  173. spin_unlock(&mm->page_table_lock);
  174. return done + 1;
  175. fault:
  176. spin_unlock(&mm->page_table_lock);
  177. if (__handle_fault(uaddr, (unsigned long) pte, 0))
  178. return 0;
  179. goto retry;
  180. }
  181. static size_t strncpy_from_user_pt(size_t count, const char __user *src,
  182. char *dst)
  183. {
  184. size_t n = strnlen_user_pt(count, src);
  185. if (!n)
  186. return -EFAULT;
  187. if (n > count)
  188. n = count;
  189. if (segment_eq(get_fs(), KERNEL_DS)) {
  190. memcpy(dst, (const char __kernel __force *) src, n);
  191. if (dst[n-1] == '\0')
  192. return n-1;
  193. else
  194. return n;
  195. }
  196. if (__user_copy_pt((unsigned long) src, dst, n, 0))
  197. return -EFAULT;
  198. if (dst[n-1] == '\0')
  199. return n-1;
  200. else
  201. return n;
  202. }
  203. static size_t copy_in_user_pt(size_t n, void __user *to,
  204. const void __user *from)
  205. {
  206. struct mm_struct *mm = current->mm;
  207. unsigned long offset_from, offset_to, offset_max, pfn_from, pfn_to,
  208. uaddr, done, size, error_code;
  209. unsigned long uaddr_from = (unsigned long) from;
  210. unsigned long uaddr_to = (unsigned long) to;
  211. pte_t *pte_from, *pte_to;
  212. int write_user;
  213. if (segment_eq(get_fs(), KERNEL_DS)) {
  214. memcpy((void __force *) to, (void __force *) from, n);
  215. return 0;
  216. }
  217. done = 0;
  218. retry:
  219. spin_lock(&mm->page_table_lock);
  220. do {
  221. write_user = 0;
  222. uaddr = uaddr_from;
  223. pte_from = follow_table(mm, uaddr_from);
  224. error_code = (unsigned long) pte_from;
  225. if (error_code < 0x1000)
  226. goto fault;
  227. if (!pte_present(*pte_from)) {
  228. error_code = 0x11;
  229. goto fault;
  230. }
  231. write_user = 1;
  232. uaddr = uaddr_to;
  233. pte_to = follow_table(mm, uaddr_to);
  234. error_code = (unsigned long) pte_to;
  235. if (error_code < 0x1000)
  236. goto fault;
  237. if (!pte_present(*pte_to)) {
  238. error_code = 0x11;
  239. goto fault;
  240. } else if (!pte_write(*pte_to)) {
  241. error_code = 0x04;
  242. goto fault;
  243. }
  244. pfn_from = pte_pfn(*pte_from);
  245. pfn_to = pte_pfn(*pte_to);
  246. offset_from = uaddr_from & (PAGE_SIZE-1);
  247. offset_to = uaddr_from & (PAGE_SIZE-1);
  248. offset_max = max(offset_from, offset_to);
  249. size = min(n - done, PAGE_SIZE - offset_max);
  250. memcpy((void *)(pfn_to << PAGE_SHIFT) + offset_to,
  251. (void *)(pfn_from << PAGE_SHIFT) + offset_from, size);
  252. done += size;
  253. uaddr_from += size;
  254. uaddr_to += size;
  255. } while (done < n);
  256. spin_unlock(&mm->page_table_lock);
  257. return n - done;
  258. fault:
  259. spin_unlock(&mm->page_table_lock);
  260. if (__handle_fault(uaddr, error_code, write_user))
  261. return n - done;
  262. goto retry;
  263. }
  264. #define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \
  265. asm volatile("0: l %1,0(%6)\n" \
  266. "1: " insn \
  267. "2: cs %1,%2,0(%6)\n" \
  268. "3: jl 1b\n" \
  269. " lhi %0,0\n" \
  270. "4:\n" \
  271. EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b) \
  272. : "=d" (ret), "=&d" (oldval), "=&d" (newval), \
  273. "=m" (*uaddr) \
  274. : "0" (-EFAULT), "d" (oparg), "a" (uaddr), \
  275. "m" (*uaddr) : "cc" );
  276. static int __futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old)
  277. {
  278. int oldval = 0, newval, ret;
  279. switch (op) {
  280. case FUTEX_OP_SET:
  281. __futex_atomic_op("lr %2,%5\n",
  282. ret, oldval, newval, uaddr, oparg);
  283. break;
  284. case FUTEX_OP_ADD:
  285. __futex_atomic_op("lr %2,%1\nar %2,%5\n",
  286. ret, oldval, newval, uaddr, oparg);
  287. break;
  288. case FUTEX_OP_OR:
  289. __futex_atomic_op("lr %2,%1\nor %2,%5\n",
  290. ret, oldval, newval, uaddr, oparg);
  291. break;
  292. case FUTEX_OP_ANDN:
  293. __futex_atomic_op("lr %2,%1\nnr %2,%5\n",
  294. ret, oldval, newval, uaddr, oparg);
  295. break;
  296. case FUTEX_OP_XOR:
  297. __futex_atomic_op("lr %2,%1\nxr %2,%5\n",
  298. ret, oldval, newval, uaddr, oparg);
  299. break;
  300. default:
  301. ret = -ENOSYS;
  302. }
  303. if (ret == 0)
  304. *old = oldval;
  305. return ret;
  306. }
  307. int futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old)
  308. {
  309. int ret;
  310. if (segment_eq(get_fs(), KERNEL_DS))
  311. return __futex_atomic_op_pt(op, uaddr, oparg, old);
  312. spin_lock(&current->mm->page_table_lock);
  313. uaddr = (int __user *) __dat_user_addr((unsigned long) uaddr);
  314. if (!uaddr) {
  315. spin_unlock(&current->mm->page_table_lock);
  316. return -EFAULT;
  317. }
  318. get_page(virt_to_page(uaddr));
  319. spin_unlock(&current->mm->page_table_lock);
  320. ret = __futex_atomic_op_pt(op, uaddr, oparg, old);
  321. put_page(virt_to_page(uaddr));
  322. return ret;
  323. }
  324. static int __futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr,
  325. u32 oldval, u32 newval)
  326. {
  327. int ret;
  328. asm volatile("0: cs %1,%4,0(%5)\n"
  329. "1: la %0,0\n"
  330. "2:\n"
  331. EX_TABLE(0b,2b) EX_TABLE(1b,2b)
  332. : "=d" (ret), "+d" (oldval), "=m" (*uaddr)
  333. : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
  334. : "cc", "memory" );
  335. *uval = oldval;
  336. return ret;
  337. }
  338. int futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr,
  339. u32 oldval, u32 newval)
  340. {
  341. int ret;
  342. if (segment_eq(get_fs(), KERNEL_DS))
  343. return __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
  344. spin_lock(&current->mm->page_table_lock);
  345. uaddr = (int __user *) __dat_user_addr((unsigned long) uaddr);
  346. if (!uaddr) {
  347. spin_unlock(&current->mm->page_table_lock);
  348. return -EFAULT;
  349. }
  350. get_page(virt_to_page(uaddr));
  351. spin_unlock(&current->mm->page_table_lock);
  352. ret = __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
  353. put_page(virt_to_page(uaddr));
  354. return ret;
  355. }
  356. struct uaccess_ops uaccess_pt = {
  357. .copy_from_user = copy_from_user_pt,
  358. .copy_from_user_small = copy_from_user_pt,
  359. .copy_to_user = copy_to_user_pt,
  360. .copy_to_user_small = copy_to_user_pt,
  361. .copy_in_user = copy_in_user_pt,
  362. .clear_user = clear_user_pt,
  363. .strnlen_user = strnlen_user_pt,
  364. .strncpy_from_user = strncpy_from_user_pt,
  365. .futex_atomic_op = futex_atomic_op_pt,
  366. .futex_atomic_cmpxchg = futex_atomic_cmpxchg_pt,
  367. };