io_trapped.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Trapped io support
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Intercept io operations by trapping.
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/bitops.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <asm/mmu_context.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/io.h>
  21. #include <asm/io_trapped.h>
  22. #define TRAPPED_PAGES_MAX 16
  23. #ifdef CONFIG_HAS_IOPORT_MAP
  24. LIST_HEAD(trapped_io);
  25. EXPORT_SYMBOL_GPL(trapped_io);
  26. #endif
  27. #ifdef CONFIG_HAS_IOMEM
  28. LIST_HEAD(trapped_mem);
  29. EXPORT_SYMBOL_GPL(trapped_mem);
  30. #endif
  31. static DEFINE_SPINLOCK(trapped_lock);
  32. static int trapped_io_disable __read_mostly;
  33. static int __init trapped_io_setup(char *__unused)
  34. {
  35. trapped_io_disable = 1;
  36. return 1;
  37. }
  38. __setup("noiotrap", trapped_io_setup);
  39. int register_trapped_io(struct trapped_io *tiop)
  40. {
  41. struct resource *res;
  42. unsigned long len = 0, flags = 0;
  43. struct page *pages[TRAPPED_PAGES_MAX];
  44. int k, n;
  45. if (unlikely(trapped_io_disable))
  46. return 0;
  47. /* structure must be page aligned */
  48. if ((unsigned long)tiop & (PAGE_SIZE - 1))
  49. goto bad;
  50. for (k = 0; k < tiop->num_resources; k++) {
  51. res = tiop->resource + k;
  52. len += roundup(resource_size(res), PAGE_SIZE);
  53. flags |= res->flags;
  54. }
  55. /* support IORESOURCE_IO _or_ MEM, not both */
  56. if (hweight_long(flags) != 1)
  57. goto bad;
  58. n = len >> PAGE_SHIFT;
  59. if (n >= TRAPPED_PAGES_MAX)
  60. goto bad;
  61. for (k = 0; k < n; k++)
  62. pages[k] = virt_to_page(tiop);
  63. tiop->virt_base = vmap(pages, n, VM_MAP, PAGE_NONE);
  64. if (!tiop->virt_base)
  65. goto bad;
  66. len = 0;
  67. for (k = 0; k < tiop->num_resources; k++) {
  68. res = tiop->resource + k;
  69. pr_info("trapped io 0x%08lx overrides %s 0x%08lx\n",
  70. (unsigned long)(tiop->virt_base + len),
  71. res->flags & IORESOURCE_IO ? "io" : "mmio",
  72. (unsigned long)res->start);
  73. len += roundup(resource_size(res), PAGE_SIZE);
  74. }
  75. tiop->magic = IO_TRAPPED_MAGIC;
  76. INIT_LIST_HEAD(&tiop->list);
  77. spin_lock_irq(&trapped_lock);
  78. #ifdef CONFIG_HAS_IOPORT_MAP
  79. if (flags & IORESOURCE_IO)
  80. list_add(&tiop->list, &trapped_io);
  81. #endif
  82. #ifdef CONFIG_HAS_IOMEM
  83. if (flags & IORESOURCE_MEM)
  84. list_add(&tiop->list, &trapped_mem);
  85. #endif
  86. spin_unlock_irq(&trapped_lock);
  87. return 0;
  88. bad:
  89. pr_warning("unable to install trapped io filter\n");
  90. return -1;
  91. }
  92. EXPORT_SYMBOL_GPL(register_trapped_io);
  93. void __iomem *match_trapped_io_handler(struct list_head *list,
  94. unsigned long offset,
  95. unsigned long size)
  96. {
  97. unsigned long voffs;
  98. struct trapped_io *tiop;
  99. struct resource *res;
  100. int k, len;
  101. unsigned long flags;
  102. spin_lock_irqsave(&trapped_lock, flags);
  103. list_for_each_entry(tiop, list, list) {
  104. voffs = 0;
  105. for (k = 0; k < tiop->num_resources; k++) {
  106. res = tiop->resource + k;
  107. if (res->start == offset) {
  108. spin_unlock_irqrestore(&trapped_lock, flags);
  109. return tiop->virt_base + voffs;
  110. }
  111. len = resource_size(res);
  112. voffs += roundup(len, PAGE_SIZE);
  113. }
  114. }
  115. spin_unlock_irqrestore(&trapped_lock, flags);
  116. return NULL;
  117. }
  118. EXPORT_SYMBOL_GPL(match_trapped_io_handler);
  119. static struct trapped_io *lookup_tiop(unsigned long address)
  120. {
  121. pgd_t *pgd_k;
  122. pud_t *pud_k;
  123. pmd_t *pmd_k;
  124. pte_t *pte_k;
  125. pte_t entry;
  126. pgd_k = swapper_pg_dir + pgd_index(address);
  127. if (!pgd_present(*pgd_k))
  128. return NULL;
  129. pud_k = pud_offset(pgd_k, address);
  130. if (!pud_present(*pud_k))
  131. return NULL;
  132. pmd_k = pmd_offset(pud_k, address);
  133. if (!pmd_present(*pmd_k))
  134. return NULL;
  135. pte_k = pte_offset_kernel(pmd_k, address);
  136. entry = *pte_k;
  137. return pfn_to_kaddr(pte_pfn(entry));
  138. }
  139. static unsigned long lookup_address(struct trapped_io *tiop,
  140. unsigned long address)
  141. {
  142. struct resource *res;
  143. unsigned long vaddr = (unsigned long)tiop->virt_base;
  144. unsigned long len;
  145. int k;
  146. for (k = 0; k < tiop->num_resources; k++) {
  147. res = tiop->resource + k;
  148. len = roundup(resource_size(res), PAGE_SIZE);
  149. if (address < (vaddr + len))
  150. return res->start + (address - vaddr);
  151. vaddr += len;
  152. }
  153. return 0;
  154. }
  155. static unsigned long long copy_word(unsigned long src_addr, int src_len,
  156. unsigned long dst_addr, int dst_len)
  157. {
  158. unsigned long long tmp = 0;
  159. switch (src_len) {
  160. case 1:
  161. tmp = __raw_readb(src_addr);
  162. break;
  163. case 2:
  164. tmp = __raw_readw(src_addr);
  165. break;
  166. case 4:
  167. tmp = __raw_readl(src_addr);
  168. break;
  169. case 8:
  170. tmp = __raw_readq(src_addr);
  171. break;
  172. }
  173. switch (dst_len) {
  174. case 1:
  175. __raw_writeb(tmp, dst_addr);
  176. break;
  177. case 2:
  178. __raw_writew(tmp, dst_addr);
  179. break;
  180. case 4:
  181. __raw_writel(tmp, dst_addr);
  182. break;
  183. case 8:
  184. __raw_writeq(tmp, dst_addr);
  185. break;
  186. }
  187. return tmp;
  188. }
  189. static unsigned long from_device(void *dst, const void *src, unsigned long cnt)
  190. {
  191. struct trapped_io *tiop;
  192. unsigned long src_addr = (unsigned long)src;
  193. unsigned long long tmp;
  194. pr_debug("trapped io read 0x%08lx (%ld)\n", src_addr, cnt);
  195. tiop = lookup_tiop(src_addr);
  196. WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC));
  197. src_addr = lookup_address(tiop, src_addr);
  198. if (!src_addr)
  199. return cnt;
  200. tmp = copy_word(src_addr,
  201. max_t(unsigned long, cnt,
  202. (tiop->minimum_bus_width / 8)),
  203. (unsigned long)dst, cnt);
  204. pr_debug("trapped io read 0x%08lx -> 0x%08llx\n", src_addr, tmp);
  205. return 0;
  206. }
  207. static unsigned long to_device(void *dst, const void *src, unsigned long cnt)
  208. {
  209. struct trapped_io *tiop;
  210. unsigned long dst_addr = (unsigned long)dst;
  211. unsigned long long tmp;
  212. pr_debug("trapped io write 0x%08lx (%ld)\n", dst_addr, cnt);
  213. tiop = lookup_tiop(dst_addr);
  214. WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC));
  215. dst_addr = lookup_address(tiop, dst_addr);
  216. if (!dst_addr)
  217. return cnt;
  218. tmp = copy_word((unsigned long)src, cnt,
  219. dst_addr, max_t(unsigned long, cnt,
  220. (tiop->minimum_bus_width / 8)));
  221. pr_debug("trapped io write 0x%08lx -> 0x%08llx\n", dst_addr, tmp);
  222. return 0;
  223. }
  224. static struct mem_access trapped_io_access = {
  225. from_device,
  226. to_device,
  227. };
  228. int handle_trapped_io(struct pt_regs *regs, unsigned long address)
  229. {
  230. mm_segment_t oldfs;
  231. insn_size_t instruction;
  232. int tmp;
  233. if (trapped_io_disable)
  234. return 0;
  235. if (!lookup_tiop(address))
  236. return 0;
  237. WARN_ON(user_mode(regs));
  238. oldfs = get_fs();
  239. set_fs(KERNEL_DS);
  240. if (copy_from_user(&instruction, (void *)(regs->pc),
  241. sizeof(instruction))) {
  242. set_fs(oldfs);
  243. return 0;
  244. }
  245. tmp = handle_unaligned_access(instruction, regs,
  246. &trapped_io_access, 1, address);
  247. set_fs(oldfs);
  248. return tmp == 0;
  249. }