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 <asm/system.h>
17 #include <asm/mmu_context.h>
18 #include <asm/uaccess.h>
20 #include <asm/io_trapped.h>
22 #define TRAPPED_PAGES_MAX 16
24 #ifdef CONFIG_HAS_IOPORT
25 LIST_HEAD(trapped_io);
27 #ifdef CONFIG_HAS_IOMEM
28 LIST_HEAD(trapped_mem);
30 static DEFINE_SPINLOCK(trapped_lock);
32 int __init register_trapped_io(struct trapped_io *tiop)
35 unsigned long len = 0, flags = 0;
36 struct page *pages[TRAPPED_PAGES_MAX];
39 /* structure must be page aligned */
40 if ((unsigned long)tiop & (PAGE_SIZE - 1))
43 for (k = 0; k < tiop->num_resources; k++) {
44 res = tiop->resource + k;
45 len += roundup((res->end - res->start) + 1, PAGE_SIZE);
49 /* support IORESOURCE_IO _or_ MEM, not both */
50 if (hweight_long(flags) != 1)
53 n = len >> PAGE_SHIFT;
55 if (n >= TRAPPED_PAGES_MAX)
58 for (k = 0; k < n; k++)
59 pages[k] = virt_to_page(tiop);
61 tiop->virt_base = vmap(pages, n, VM_MAP, PAGE_NONE);
66 for (k = 0; k < tiop->num_resources; k++) {
67 res = tiop->resource + k;
68 pr_info("trapped io 0x%08lx overrides %s 0x%08lx\n",
69 (unsigned long)(tiop->virt_base + len),
70 res->flags & IORESOURCE_IO ? "io" : "mmio",
71 (unsigned long)res->start);
72 len += roundup((res->end - res->start) + 1, PAGE_SIZE);
75 tiop->magic = IO_TRAPPED_MAGIC;
76 INIT_LIST_HEAD(&tiop->list);
77 spin_lock_irq(&trapped_lock);
78 if (flags & IORESOURCE_IO)
79 list_add(&tiop->list, &trapped_io);
80 if (flags & IORESOURCE_MEM)
81 list_add(&tiop->list, &trapped_mem);
82 spin_unlock_irq(&trapped_lock);
86 pr_warning("unable to install trapped io filter\n");
90 void __iomem *match_trapped_io_handler(struct list_head *list,
95 struct trapped_io *tiop;
99 spin_lock_irq(&trapped_lock);
100 list_for_each_entry(tiop, list, list) {
102 for (k = 0; k < tiop->num_resources; k++) {
103 res = tiop->resource + k;
104 if (res->start == offset) {
105 spin_unlock_irq(&trapped_lock);
106 return tiop->virt_base + voffs;
109 len = (res->end - res->start) + 1;
110 voffs += roundup(len, PAGE_SIZE);
113 spin_unlock_irq(&trapped_lock);
117 static struct trapped_io *lookup_tiop(unsigned long address)
125 pgd_k = swapper_pg_dir + pgd_index(address);
126 if (!pgd_present(*pgd_k))
129 pud_k = pud_offset(pgd_k, address);
130 if (!pud_present(*pud_k))
133 pmd_k = pmd_offset(pud_k, address);
134 if (!pmd_present(*pmd_k))
137 pte_k = pte_offset_kernel(pmd_k, address);
140 return pfn_to_kaddr(pte_pfn(entry));
143 static unsigned long lookup_address(struct trapped_io *tiop,
144 unsigned long address)
146 struct resource *res;
147 unsigned long vaddr = (unsigned long)tiop->virt_base;
151 for (k = 0; k < tiop->num_resources; k++) {
152 res = tiop->resource + k;
153 len = roundup((res->end - res->start) + 1, PAGE_SIZE);
154 if (address < (vaddr + len))
155 return res->start + (address - vaddr);
161 static unsigned long long copy_word(unsigned long src_addr, int src_len,
162 unsigned long dst_addr, int dst_len)
164 unsigned long long tmp = 0;
168 tmp = ctrl_inb(src_addr);
171 tmp = ctrl_inw(src_addr);
174 tmp = ctrl_inl(src_addr);
177 tmp = ctrl_inq(src_addr);
183 ctrl_outb(tmp, dst_addr);
186 ctrl_outw(tmp, dst_addr);
189 ctrl_outl(tmp, dst_addr);
192 ctrl_outq(tmp, dst_addr);
199 static unsigned long from_device(void *dst, const void *src, unsigned long cnt)
201 struct trapped_io *tiop;
202 unsigned long src_addr = (unsigned long)src;
203 unsigned long long tmp;
205 pr_debug("trapped io read 0x%08lx (%ld)\n", src_addr, cnt);
206 tiop = lookup_tiop(src_addr);
207 WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC));
209 src_addr = lookup_address(tiop, src_addr);
213 tmp = copy_word(src_addr,
214 max_t(unsigned long, cnt,
215 (tiop->minimum_bus_width / 8)),
216 (unsigned long)dst, cnt);
218 pr_debug("trapped io read 0x%08lx -> 0x%08llx\n", src_addr, tmp);
222 static unsigned long to_device(void *dst, const void *src, unsigned long cnt)
224 struct trapped_io *tiop;
225 unsigned long dst_addr = (unsigned long)dst;
226 unsigned long long tmp;
228 pr_debug("trapped io write 0x%08lx (%ld)\n", dst_addr, cnt);
229 tiop = lookup_tiop(dst_addr);
230 WARN_ON(!tiop || (tiop->magic != IO_TRAPPED_MAGIC));
232 dst_addr = lookup_address(tiop, dst_addr);
236 tmp = copy_word((unsigned long)src, cnt,
237 dst_addr, max_t(unsigned long, cnt,
238 (tiop->minimum_bus_width / 8)));
240 pr_debug("trapped io write 0x%08lx -> 0x%08llx\n", dst_addr, tmp);
244 static struct mem_access trapped_io_access = {
249 int handle_trapped_io(struct pt_regs *regs, unsigned long address)
252 opcode_t instruction;
255 if (!lookup_tiop(address))
258 WARN_ON(user_mode(regs));
262 if (copy_from_user(&instruction, (void *)(regs->pc),
263 sizeof(instruction))) {
268 tmp = handle_unaligned_access(instruction, regs, &trapped_io_access);