2 * Copyright (c) 2012 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Memory ranges are represented with an RB tree. On insertion, the range
31 * is checked for overlaps. On lookup, the key has the same base and limit
32 * so it can be searched within the range.
34 * It is assumed that all setup of ranges takes place in single-threaded
35 * mode before vCPUs have been started. As such, no locks are used on the
36 * RB tree. If this is no longer the case, then a r/w lock could be used,
37 * with readers on the lookup and a writer if the tree needs to be changed
38 * (and per vCPU caches flushed)
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
46 #include <sys/errno.h>
47 #include <machine/vmm.h>
55 struct mmio_rb_range {
56 RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */
57 struct mem_range mr_param;
63 RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
65 RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rbroot;
68 * Per-vCPU cache. Since most accesses from a vCPU will be to
69 * consecutive addresses in a range, it makes sense to cache the
72 static struct mmio_rb_range *mmio_hint[VM_MAXCPU];
75 mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b)
77 if (a->mr_end < b->mr_base)
79 else if (a->mr_base > b->mr_end)
85 mmio_rb_lookup(uint64_t addr, struct mmio_rb_range **entry)
87 struct mmio_rb_range find, *res;
89 find.mr_base = find.mr_end = addr;
91 res = RB_FIND(mmio_rb_tree, &mmio_rbroot, &find);
102 mmio_rb_add(struct mmio_rb_range *new)
104 struct mmio_rb_range *overlap;
106 overlap = RB_INSERT(mmio_rb_tree, &mmio_rbroot, new);
108 if (overlap != NULL) {
110 printf("overlap detected: new %lx:%lx, tree %lx:%lx\n",
111 new->mr_base, new->mr_end,
112 overlap->mr_base, overlap->mr_end);
125 struct mmio_rb_range *np;
127 RB_FOREACH(np, mmio_rb_tree, &mmio_rbroot) {
128 printf(" %lx:%lx, %s\n", np->mr_base, np->mr_end,
134 RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
137 mem_read(void *ctx, int vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg)
140 struct mem_range *mr = arg;
142 error = (*mr->handler)(ctx, vcpu, MEM_F_READ, gpa, size,
143 rval, mr->arg1, mr->arg2);
148 mem_write(void *ctx, int vcpu, uint64_t gpa, uint64_t wval, int size, void *arg)
151 struct mem_range *mr = arg;
153 error = (*mr->handler)(ctx, vcpu, MEM_F_WRITE, gpa, size,
154 &wval, mr->arg1, mr->arg2);
159 emulate_mem(struct vmctx *ctx, int vcpu, uint64_t paddr, struct vie *vie)
161 struct mmio_rb_range *entry;
165 * First check the per-vCPU cache
167 if (mmio_hint[vcpu] &&
168 paddr >= mmio_hint[vcpu]->mr_base &&
169 paddr <= mmio_hint[vcpu]->mr_end) {
170 entry = mmio_hint[vcpu];
175 if (mmio_rb_lookup(paddr, &entry))
178 /* Update the per-vCPU cache */
179 mmio_hint[vcpu] = entry;
182 assert(entry != NULL && entry == mmio_hint[vcpu]);
184 err = vmm_emulate_instruction(ctx, vcpu, paddr, vie,
185 mem_read, mem_write, &entry->mr_param);
190 register_mem(struct mem_range *memp)
192 struct mmio_rb_range *mrp;
197 mrp = malloc(sizeof(struct mmio_rb_range));
200 mrp->mr_param = *memp;
201 mrp->mr_base = memp->base;
202 mrp->mr_end = memp->base + memp->size - 1;
204 err = mmio_rb_add(mrp);
217 RB_INIT(&mmio_rbroot);