uaccess.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/err.h>
  6. #include <linux/highmem.h>
  7. #include <linux/mm.h>
  8. #include <linux/module.h>
  9. #include <linux/sched.h>
  10. #include <asm/current.h>
  11. #include <asm/page.h>
  12. #include <asm/pgtable.h>
  13. #include <kern_util.h>
  14. #include <os.h>
  15. pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr)
  16. {
  17. pgd_t *pgd;
  18. pud_t *pud;
  19. pmd_t *pmd;
  20. if (mm == NULL)
  21. return NULL;
  22. pgd = pgd_offset(mm, addr);
  23. if (!pgd_present(*pgd))
  24. return NULL;
  25. pud = pud_offset(pgd, addr);
  26. if (!pud_present(*pud))
  27. return NULL;
  28. pmd = pmd_offset(pud, addr);
  29. if (!pmd_present(*pmd))
  30. return NULL;
  31. return pte_offset_kernel(pmd, addr);
  32. }
  33. static pte_t *maybe_map(unsigned long virt, int is_write)
  34. {
  35. pte_t *pte = virt_to_pte(current->mm, virt);
  36. int err, dummy_code;
  37. if ((pte == NULL) || !pte_present(*pte) ||
  38. (is_write && !pte_write(*pte))) {
  39. err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
  40. if (err)
  41. return NULL;
  42. pte = virt_to_pte(current->mm, virt);
  43. }
  44. if (!pte_present(*pte))
  45. pte = NULL;
  46. return pte;
  47. }
  48. static int do_op_one_page(unsigned long addr, int len, int is_write,
  49. int (*op)(unsigned long addr, int len, void *arg), void *arg)
  50. {
  51. jmp_buf buf;
  52. struct page *page;
  53. pte_t *pte;
  54. int n, faulted;
  55. pte = maybe_map(addr, is_write);
  56. if (pte == NULL)
  57. return -1;
  58. page = pte_page(*pte);
  59. addr = (unsigned long) kmap_atomic(page) +
  60. (addr & ~PAGE_MASK);
  61. current->thread.fault_catcher = &buf;
  62. faulted = UML_SETJMP(&buf);
  63. if (faulted == 0)
  64. n = (*op)(addr, len, arg);
  65. else
  66. n = -1;
  67. current->thread.fault_catcher = NULL;
  68. kunmap_atomic((void *)addr);
  69. return n;
  70. }
  71. static long buffer_op(unsigned long addr, int len, int is_write,
  72. int (*op)(unsigned long, int, void *), void *arg)
  73. {
  74. long size, remain, n;
  75. size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
  76. remain = len;
  77. n = do_op_one_page(addr, size, is_write, op, arg);
  78. if (n != 0) {
  79. remain = (n < 0 ? remain : 0);
  80. goto out;
  81. }
  82. addr += size;
  83. remain -= size;
  84. if (remain == 0)
  85. goto out;
  86. while (addr < ((addr + remain) & PAGE_MASK)) {
  87. n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
  88. if (n != 0) {
  89. remain = (n < 0 ? remain : 0);
  90. goto out;
  91. }
  92. addr += PAGE_SIZE;
  93. remain -= PAGE_SIZE;
  94. }
  95. if (remain == 0)
  96. goto out;
  97. n = do_op_one_page(addr, remain, is_write, op, arg);
  98. if (n != 0) {
  99. remain = (n < 0 ? remain : 0);
  100. goto out;
  101. }
  102. return 0;
  103. out:
  104. return remain;
  105. }
  106. static int copy_chunk_from_user(unsigned long from, int len, void *arg)
  107. {
  108. unsigned long *to_ptr = arg, to = *to_ptr;
  109. memcpy((void *) to, (void *) from, len);
  110. *to_ptr += len;
  111. return 0;
  112. }
  113. long __copy_from_user(void *to, const void __user *from, unsigned long n)
  114. {
  115. if (segment_eq(get_fs(), KERNEL_DS)) {
  116. memcpy(to, (__force void*)from, n);
  117. return 0;
  118. }
  119. return buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to);
  120. }
  121. EXPORT_SYMBOL(__copy_from_user);
  122. static int copy_chunk_to_user(unsigned long to, int len, void *arg)
  123. {
  124. unsigned long *from_ptr = arg, from = *from_ptr;
  125. memcpy((void *) to, (void *) from, len);
  126. *from_ptr += len;
  127. return 0;
  128. }
  129. long __copy_to_user(void __user *to, const void *from, unsigned long n)
  130. {
  131. if (segment_eq(get_fs(), KERNEL_DS)) {
  132. memcpy((__force void *) to, from, n);
  133. return 0;
  134. }
  135. return buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from);
  136. }
  137. EXPORT_SYMBOL(__copy_to_user);
  138. static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
  139. {
  140. char **to_ptr = arg, *to = *to_ptr;
  141. int n;
  142. strncpy(to, (void *) from, len);
  143. n = strnlen(to, len);
  144. *to_ptr += n;
  145. if (n < len)
  146. return 1;
  147. return 0;
  148. }
  149. long __strncpy_from_user(char *dst, const char __user *src, long count)
  150. {
  151. long n;
  152. char *ptr = dst;
  153. if (segment_eq(get_fs(), KERNEL_DS)) {
  154. strncpy(dst, (__force void *) src, count);
  155. return strnlen(dst, count);
  156. }
  157. n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
  158. &ptr);
  159. if (n != 0)
  160. return -EFAULT;
  161. return strnlen(dst, count);
  162. }
  163. EXPORT_SYMBOL(__strncpy_from_user);
  164. static int clear_chunk(unsigned long addr, int len, void *unused)
  165. {
  166. memset((void *) addr, 0, len);
  167. return 0;
  168. }
  169. unsigned long __clear_user(void __user *mem, unsigned long len)
  170. {
  171. if (segment_eq(get_fs(), KERNEL_DS)) {
  172. memset((__force void*)mem, 0, len);
  173. return 0;
  174. }
  175. return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
  176. }
  177. EXPORT_SYMBOL(__clear_user);
  178. static int strnlen_chunk(unsigned long str, int len, void *arg)
  179. {
  180. int *len_ptr = arg, n;
  181. n = strnlen((void *) str, len);
  182. *len_ptr += n;
  183. if (n < len)
  184. return 1;
  185. return 0;
  186. }
  187. long __strnlen_user(const void __user *str, long len)
  188. {
  189. int count = 0, n;
  190. if (segment_eq(get_fs(), KERNEL_DS))
  191. return strnlen((__force char*)str, len) + 1;
  192. n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
  193. if (n == 0)
  194. return count + 1;
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(__strnlen_user);