2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Memory ranges are represented with an RB tree. On insertion, the range
33 * is checked for overlaps. On lookup, the key has the same base and limit
34 * so it can be searched within the range.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
41 #include <sys/errno.h>
43 #include <machine/vmm.h>
44 #include <machine/vmm_instruction_emul.h>
54 struct mmio_rb_range {
55 RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */
56 struct mem_range mr_param;
62 RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
64 RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback;
67 * Per-vCPU cache. Since most accesses from a vCPU will be to
68 * consecutive addresses in a range, it makes sense to cache the
71 static struct mmio_rb_range *mmio_hint[VM_MAXCPU];
73 static pthread_rwlock_t mmio_rwlock;
76 mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b)
78 if (a->mr_end < b->mr_base)
80 else if (a->mr_base > b->mr_end)
86 mmio_rb_lookup(struct mmio_rb_tree *rbt, uint64_t addr,
87 struct mmio_rb_range **entry)
89 struct mmio_rb_range find, *res;
91 find.mr_base = find.mr_end = addr;
93 res = RB_FIND(mmio_rb_tree, rbt, &find);
104 mmio_rb_add(struct mmio_rb_tree *rbt, struct mmio_rb_range *new)
106 struct mmio_rb_range *overlap;
108 overlap = RB_INSERT(mmio_rb_tree, rbt, new);
110 if (overlap != NULL) {
112 printf("overlap detected: new %lx:%lx, tree %lx:%lx\n",
113 new->mr_base, new->mr_end,
114 overlap->mr_base, overlap->mr_end);
125 mmio_rb_dump(struct mmio_rb_tree *rbt)
128 struct mmio_rb_range *np;
130 pthread_rwlock_rdlock(&mmio_rwlock);
131 RB_FOREACH(np, mmio_rb_tree, rbt) {
132 printf(" %lx:%lx, %s\n", np->mr_base, np->mr_end,
135 perror = pthread_rwlock_unlock(&mmio_rwlock);
140 RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
143 mem_read(void *ctx, int vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg)
146 struct mem_range *mr = arg;
148 error = (*mr->handler)(ctx, vcpu, MEM_F_READ, gpa, size,
149 rval, mr->arg1, mr->arg2);
154 mem_write(void *ctx, int vcpu, uint64_t gpa, uint64_t wval, int size, void *arg)
157 struct mem_range *mr = arg;
159 error = (*mr->handler)(ctx, vcpu, MEM_F_WRITE, gpa, size,
160 &wval, mr->arg1, mr->arg2);
165 emulate_mem(struct vmctx *ctx, int vcpu, uint64_t paddr, struct vie *vie,
166 struct vm_guest_paging *paging)
169 struct mmio_rb_range *entry;
170 int err, perror, immutable;
172 pthread_rwlock_rdlock(&mmio_rwlock);
174 * First check the per-vCPU cache
176 if (mmio_hint[vcpu] &&
177 paddr >= mmio_hint[vcpu]->mr_base &&
178 paddr <= mmio_hint[vcpu]->mr_end) {
179 entry = mmio_hint[vcpu];
184 if (mmio_rb_lookup(&mmio_rb_root, paddr, &entry) == 0) {
185 /* Update the per-vCPU cache */
186 mmio_hint[vcpu] = entry;
187 } else if (mmio_rb_lookup(&mmio_rb_fallback, paddr, &entry)) {
188 perror = pthread_rwlock_unlock(&mmio_rwlock);
194 assert(entry != NULL);
197 * An 'immutable' memory range is guaranteed to be never removed
198 * so there is no need to hold 'mmio_rwlock' while calling the
201 * XXX writes to the PCIR_COMMAND register can cause register_mem()
202 * to be called. If the guest is using PCI extended config space
203 * to modify the PCIR_COMMAND register then register_mem() can
204 * deadlock on 'mmio_rwlock'. However by registering the extended
205 * config space window as 'immutable' the deadlock can be avoided.
207 immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE);
209 perror = pthread_rwlock_unlock(&mmio_rwlock);
213 err = vmm_emulate_instruction(ctx, vcpu, paddr, vie, paging,
214 mem_read, mem_write, &entry->mr_param);
217 perror = pthread_rwlock_unlock(&mmio_rwlock);
226 register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp)
228 struct mmio_rb_range *entry, *mrp;
233 mrp = malloc(sizeof(struct mmio_rb_range));
235 warn("%s: couldn't allocate memory for mrp\n",
239 mrp->mr_param = *memp;
240 mrp->mr_base = memp->base;
241 mrp->mr_end = memp->base + memp->size - 1;
242 pthread_rwlock_wrlock(&mmio_rwlock);
243 if (mmio_rb_lookup(rbt, memp->base, &entry) != 0)
244 err = mmio_rb_add(rbt, mrp);
245 perror = pthread_rwlock_unlock(&mmio_rwlock);
255 register_mem(struct mem_range *memp)
258 return (register_mem_int(&mmio_rb_root, memp));
262 register_mem_fallback(struct mem_range *memp)
265 return (register_mem_int(&mmio_rb_fallback, memp));
269 unregister_mem(struct mem_range *memp)
271 struct mem_range *mr;
272 struct mmio_rb_range *entry = NULL;
275 pthread_rwlock_wrlock(&mmio_rwlock);
276 err = mmio_rb_lookup(&mmio_rb_root, memp->base, &entry);
278 mr = &entry->mr_param;
279 assert(mr->name == memp->name);
280 assert(mr->base == memp->base && mr->size == memp->size);
281 assert((mr->flags & MEM_F_IMMUTABLE) == 0);
282 RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry);
284 /* flush Per-vCPU cache */
285 for (i=0; i < VM_MAXCPU; i++) {
286 if (mmio_hint[i] == entry)
290 perror = pthread_rwlock_unlock(&mmio_rwlock);
303 RB_INIT(&mmio_rb_root);
304 RB_INIT(&mmio_rb_fallback);
305 pthread_rwlock_init(&mmio_rwlock, NULL);