4 * Copyright (C) 2008 Magnus Damm
6 * Intercept io operations by trapping.
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
12 #include <linux/kernel.h>
14 #include <linux/bitops.h>
15 #include <linux/vmalloc.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <asm/system.h>
19 #include <asm/mmu_context.h>
20 #include <asm/uaccess.h>
22 #include <asm/io_trapped.h>
24 #define TRAPPED_PAGES_MAX 16
26 #ifdef CONFIG_HAS_IOPORT
27 LIST_HEAD(trapped_io
);
28 EXPORT_SYMBOL_GPL(trapped_io
);
30 #ifdef CONFIG_HAS_IOMEM
31 LIST_HEAD(trapped_mem
);
32 EXPORT_SYMBOL_GPL(trapped_mem
);
34 static DEFINE_SPINLOCK(trapped_lock
);
36 static int trapped_io_disable __read_mostly
;
38 static int __init
trapped_io_setup(char *__unused
)
40 trapped_io_disable
= 1;
43 __setup("noiotrap", trapped_io_setup
);
45 int register_trapped_io(struct trapped_io
*tiop
)
48 unsigned long len
= 0, flags
= 0;
49 struct page
*pages
[TRAPPED_PAGES_MAX
];
52 if (unlikely(trapped_io_disable
))
55 /* structure must be page aligned */
56 if ((unsigned long)tiop
& (PAGE_SIZE
- 1))
59 for (k
= 0; k
< tiop
->num_resources
; k
++) {
60 res
= tiop
->resource
+ k
;
61 len
+= roundup((res
->end
- res
->start
) + 1, PAGE_SIZE
);
65 /* support IORESOURCE_IO _or_ MEM, not both */
66 if (hweight_long(flags
) != 1)
69 n
= len
>> PAGE_SHIFT
;
71 if (n
>= TRAPPED_PAGES_MAX
)
74 for (k
= 0; k
< n
; k
++)
75 pages
[k
] = virt_to_page(tiop
);
77 tiop
->virt_base
= vmap(pages
, n
, VM_MAP
, PAGE_NONE
);
82 for (k
= 0; k
< tiop
->num_resources
; k
++) {
83 res
= tiop
->resource
+ k
;
84 pr_info("trapped io 0x%08lx overrides %s 0x%08lx\n",
85 (unsigned long)(tiop
->virt_base
+ len
),
86 res
->flags
& IORESOURCE_IO
? "io" : "mmio",
87 (unsigned long)res
->start
);
88 len
+= roundup((res
->end
- res
->start
) + 1, PAGE_SIZE
);
91 tiop
->magic
= IO_TRAPPED_MAGIC
;
92 INIT_LIST_HEAD(&tiop
->list
);
93 spin_lock_irq(&trapped_lock
);
94 #ifdef CONFIG_HAS_IOPORT
95 if (flags
& IORESOURCE_IO
)
96 list_add(&tiop
->list
, &trapped_io
);
98 #ifdef CONFIG_HAS_IOMEM
99 if (flags
& IORESOURCE_MEM
)
100 list_add(&tiop
->list
, &trapped_mem
);
102 spin_unlock_irq(&trapped_lock
);
106 pr_warning("unable to install trapped io filter\n");
109 EXPORT_SYMBOL_GPL(register_trapped_io
);
111 void __iomem
*match_trapped_io_handler(struct list_head
*list
,
112 unsigned long offset
,
116 struct trapped_io
*tiop
;
117 struct resource
*res
;
121 spin_lock_irqsave(&trapped_lock
, flags
);
122 list_for_each_entry(tiop
, list
, list
) {
124 for (k
= 0; k
< tiop
->num_resources
; k
++) {
125 res
= tiop
->resource
+ k
;
126 if (res
->start
== offset
) {
127 spin_unlock_irqrestore(&trapped_lock
, flags
);
128 return tiop
->virt_base
+ voffs
;
131 len
= (res
->end
- res
->start
) + 1;
132 voffs
+= roundup(len
, PAGE_SIZE
);
135 spin_unlock_irqrestore(&trapped_lock
, flags
);
138 EXPORT_SYMBOL_GPL(match_trapped_io_handler
);
140 static struct trapped_io
*lookup_tiop(unsigned long address
)
148 pgd_k
= swapper_pg_dir
+ pgd_index(address
);
149 if (!pgd_present(*pgd_k
))
152 pud_k
= pud_offset(pgd_k
, address
);
153 if (!pud_present(*pud_k
))
156 pmd_k
= pmd_offset(pud_k
, address
);
157 if (!pmd_present(*pmd_k
))
160 pte_k
= pte_offset_kernel(pmd_k
, address
);
163 return pfn_to_kaddr(pte_pfn(entry
));
166 static unsigned long lookup_address(struct trapped_io
*tiop
,
167 unsigned long address
)
169 struct resource
*res
;
170 unsigned long vaddr
= (unsigned long)tiop
->virt_base
;
174 for (k
= 0; k
< tiop
->num_resources
; k
++) {
175 res
= tiop
->resource
+ k
;
176 len
= roundup((res
->end
- res
->start
) + 1, PAGE_SIZE
);
177 if (address
< (vaddr
+ len
))
178 return res
->start
+ (address
- vaddr
);
184 static unsigned long long copy_word(unsigned long src_addr
, int src_len
,
185 unsigned long dst_addr
, int dst_len
)
187 unsigned long long tmp
= 0;
191 tmp
= __raw_readb(src_addr
);
194 tmp
= __raw_readw(src_addr
);
197 tmp
= __raw_readl(src_addr
);
200 tmp
= __raw_readq(src_addr
);
206 __raw_writeb(tmp
, dst_addr
);
209 __raw_writew(tmp
, dst_addr
);
212 __raw_writel(tmp
, dst_addr
);
215 __raw_writeq(tmp
, dst_addr
);
222 static unsigned long from_device(void *dst
, const void *src
, unsigned long cnt
)
224 struct trapped_io
*tiop
;
225 unsigned long src_addr
= (unsigned long)src
;
226 unsigned long long tmp
;
228 pr_debug("trapped io read 0x%08lx (%ld)\n", src_addr
, cnt
);
229 tiop
= lookup_tiop(src_addr
);
230 WARN_ON(!tiop
|| (tiop
->magic
!= IO_TRAPPED_MAGIC
));
232 src_addr
= lookup_address(tiop
, src_addr
);
236 tmp
= copy_word(src_addr
,
237 max_t(unsigned long, cnt
,
238 (tiop
->minimum_bus_width
/ 8)),
239 (unsigned long)dst
, cnt
);
241 pr_debug("trapped io read 0x%08lx -> 0x%08llx\n", src_addr
, tmp
);
245 static unsigned long to_device(void *dst
, const void *src
, unsigned long cnt
)
247 struct trapped_io
*tiop
;
248 unsigned long dst_addr
= (unsigned long)dst
;
249 unsigned long long tmp
;
251 pr_debug("trapped io write 0x%08lx (%ld)\n", dst_addr
, cnt
);
252 tiop
= lookup_tiop(dst_addr
);
253 WARN_ON(!tiop
|| (tiop
->magic
!= IO_TRAPPED_MAGIC
));
255 dst_addr
= lookup_address(tiop
, dst_addr
);
259 tmp
= copy_word((unsigned long)src
, cnt
,
260 dst_addr
, max_t(unsigned long, cnt
,
261 (tiop
->minimum_bus_width
/ 8)));
263 pr_debug("trapped io write 0x%08lx -> 0x%08llx\n", dst_addr
, tmp
);
267 static struct mem_access trapped_io_access
= {
272 int handle_trapped_io(struct pt_regs
*regs
, unsigned long address
)
275 insn_size_t instruction
;
278 if (trapped_io_disable
)
280 if (!lookup_tiop(address
))
283 WARN_ON(user_mode(regs
));
287 if (copy_from_user(&instruction
, (void *)(regs
->pc
),
288 sizeof(instruction
))) {
293 tmp
= handle_unaligned_access(instruction
, regs
,
294 &trapped_io_access
, 1, address
);