2 * SPDX-License-Identifier: BSD-2-Clause
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>
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 static RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback;
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;
75 static pthread_rwlock_t mmio_rwlock;
78 mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b)
80 if (a->mr_end < b->mr_base)
82 else if (a->mr_base > b->mr_end)
88 mmio_rb_lookup(struct mmio_rb_tree *rbt, uint64_t addr,
89 struct mmio_rb_range **entry)
91 struct mmio_rb_range find, *res;
93 find.mr_base = find.mr_end = addr;
95 res = RB_FIND(mmio_rb_tree, rbt, &find);
106 mmio_rb_add(struct mmio_rb_tree *rbt, struct mmio_rb_range *new)
108 struct mmio_rb_range *overlap;
110 overlap = RB_INSERT(mmio_rb_tree, rbt, new);
112 if (overlap != NULL) {
114 printf("overlap detected: new %lx:%lx, tree %lx:%lx, '%s' "
115 "claims region already claimed for '%s'\n",
116 new->mr_base, new->mr_end,
117 overlap->mr_base, overlap->mr_end,
118 new->mr_param.name, overlap->mr_param.name);
129 mmio_rb_dump(struct mmio_rb_tree *rbt)
132 struct mmio_rb_range *np;
134 pthread_rwlock_rdlock(&mmio_rwlock);
135 RB_FOREACH(np, mmio_rb_tree, rbt) {
136 printf(" %lx:%lx, %s\n", np->mr_base, np->mr_end,
139 perror = pthread_rwlock_unlock(&mmio_rwlock);
144 RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
146 typedef int (mem_cb_t)(struct vcpu *vcpu, uint64_t gpa, struct mem_range *mr,
150 mem_read(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg)
153 struct mem_range *mr = arg;
155 error = (*mr->handler)(vcpu, MEM_F_READ, gpa, size, rval, mr->arg1,
161 mem_write(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size, void *arg)
164 struct mem_range *mr = arg;
166 error = (*mr->handler)(vcpu, MEM_F_WRITE, gpa, size, &wval, mr->arg1,
172 access_memory(struct vcpu *vcpu, uint64_t paddr, mem_cb_t *cb, void *arg)
174 struct mmio_rb_range *entry;
175 int err, perror, immutable, vcpuid;
177 vcpuid = vcpu_id(vcpu);
178 pthread_rwlock_rdlock(&mmio_rwlock);
180 * First check the per-vCPU cache
182 if (mmio_hint[vcpuid] &&
183 paddr >= mmio_hint[vcpuid]->mr_base &&
184 paddr <= mmio_hint[vcpuid]->mr_end) {
185 entry = mmio_hint[vcpuid];
190 if (mmio_rb_lookup(&mmio_rb_root, paddr, &entry) == 0) {
191 /* Update the per-vCPU cache */
192 mmio_hint[vcpuid] = entry;
193 } else if (mmio_rb_lookup(&mmio_rb_fallback, paddr, &entry)) {
194 perror = pthread_rwlock_unlock(&mmio_rwlock);
200 assert(entry != NULL);
203 * An 'immutable' memory range is guaranteed to be never removed
204 * so there is no need to hold 'mmio_rwlock' while calling the
207 * XXX writes to the PCIR_COMMAND register can cause register_mem()
208 * to be called. If the guest is using PCI extended config space
209 * to modify the PCIR_COMMAND register then register_mem() can
210 * deadlock on 'mmio_rwlock'. However by registering the extended
211 * config space window as 'immutable' the deadlock can be avoided.
213 immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE);
215 perror = pthread_rwlock_unlock(&mmio_rwlock);
219 err = cb(vcpu, paddr, &entry->mr_param, arg);
222 perror = pthread_rwlock_unlock(&mmio_rwlock);
229 struct emulate_mem_args {
231 struct vm_guest_paging *paging;
235 emulate_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr,
238 struct emulate_mem_args *ema;
241 return (vmm_emulate_instruction(vcpu, paddr, ema->vie, ema->paging,
242 mem_read, mem_write, mr));
246 emulate_mem(struct vcpu *vcpu, uint64_t paddr, struct vie *vie,
247 struct vm_guest_paging *paging)
249 struct emulate_mem_args ema;
253 return (access_memory(vcpu, paddr, emulate_mem_cb, &ema));
263 rw_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr, void *arg)
265 struct rw_mem_args *rma;
268 return (mr->handler(vcpu, rma->operation, paddr, rma->size,
269 rma->val, mr->arg1, mr->arg2));
273 read_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size)
275 struct rw_mem_args rma;
279 rma.operation = MEM_F_READ;
280 return (access_memory(vcpu, gpa, rw_mem_cb, &rma));
284 write_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size)
286 struct rw_mem_args rma;
290 rma.operation = MEM_F_WRITE;
291 return (access_memory(vcpu, gpa, rw_mem_cb, &rma));
295 register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp)
297 struct mmio_rb_range *entry, *mrp;
302 mrp = malloc(sizeof(struct mmio_rb_range));
304 warn("%s: couldn't allocate memory for mrp\n",
308 mrp->mr_param = *memp;
309 mrp->mr_base = memp->base;
310 mrp->mr_end = memp->base + memp->size - 1;
311 pthread_rwlock_wrlock(&mmio_rwlock);
312 if (mmio_rb_lookup(rbt, memp->base, &entry) != 0)
313 err = mmio_rb_add(rbt, mrp);
314 perror = pthread_rwlock_unlock(&mmio_rwlock);
324 register_mem(struct mem_range *memp)
327 return (register_mem_int(&mmio_rb_root, memp));
331 register_mem_fallback(struct mem_range *memp)
334 return (register_mem_int(&mmio_rb_fallback, memp));
338 unregister_mem(struct mem_range *memp)
340 struct mem_range *mr;
341 struct mmio_rb_range *entry = NULL;
344 pthread_rwlock_wrlock(&mmio_rwlock);
345 err = mmio_rb_lookup(&mmio_rb_root, memp->base, &entry);
347 mr = &entry->mr_param;
348 assert(mr->name == memp->name);
349 assert(mr->base == memp->base && mr->size == memp->size);
350 assert((mr->flags & MEM_F_IMMUTABLE) == 0);
351 RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry);
353 /* flush Per-vCPU cache */
354 for (i = 0; i < mmio_ncpu; i++) {
355 if (mmio_hint[i] == entry)
359 perror = pthread_rwlock_unlock(&mmio_rwlock);
373 mmio_hint = calloc(ncpu, sizeof(*mmio_hint));
374 RB_INIT(&mmio_rb_root);
375 RB_INIT(&mmio_rb_fallback);
376 pthread_rwlock_init(&mmio_rwlock, NULL);