1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* 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. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/types.h> 41 #include <sys/tree.h> 42 #include <sys/errno.h> 43 #include <machine/vmm.h> 44 #include <machine/vmm_instruction_emul.h> 45 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <assert.h> 49 #include <pthread.h> 50 51 #include "mem.h" 52 53 struct mmio_rb_range { 54 RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */ 55 struct mem_range mr_param; 56 uint64_t mr_base; 57 uint64_t mr_end; 58 }; 59 60 struct mmio_rb_tree; 61 RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare); 62 63 RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback; 64 65 /* 66 * Per-vCPU cache. Since most accesses from a vCPU will be to 67 * consecutive addresses in a range, it makes sense to cache the 68 * result of a lookup. 69 */ 70 static struct mmio_rb_range *mmio_hint[VM_MAXCPU]; 71 72 static pthread_rwlock_t mmio_rwlock; 73 74 static int 75 mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b) 76 { 77 if (a->mr_end < b->mr_base) 78 return (-1); 79 else if (a->mr_base > b->mr_end) 80 return (1); 81 return (0); 82 } 83 84 static int 85 mmio_rb_lookup(struct mmio_rb_tree *rbt, uint64_t addr, 86 struct mmio_rb_range **entry) 87 { 88 struct mmio_rb_range find, *res; 89 90 find.mr_base = find.mr_end = addr; 91 92 res = RB_FIND(mmio_rb_tree, rbt, &find); 93 94 if (res != NULL) { 95 *entry = res; 96 return (0); 97 } 98 99 return (ENOENT); 100 } 101 102 static int 103 mmio_rb_add(struct mmio_rb_tree *rbt, struct mmio_rb_range *new) 104 { 105 struct mmio_rb_range *overlap; 106 107 overlap = RB_INSERT(mmio_rb_tree, rbt, new); 108 109 if (overlap != NULL) { 110 #ifdef RB_DEBUG 111 printf("overlap detected: new %lx:%lx, tree %lx:%lx\n", 112 new->mr_base, new->mr_end, 113 overlap->mr_base, overlap->mr_end); 114 #endif 115 116 return (EEXIST); 117 } 118 119 return (0); 120 } 121 122 #if 0 123 static void 124 mmio_rb_dump(struct mmio_rb_tree *rbt) 125 { 126 int perror; 127 struct mmio_rb_range *np; 128 129 pthread_rwlock_rdlock(&mmio_rwlock); 130 RB_FOREACH(np, mmio_rb_tree, rbt) { 131 printf(" %lx:%lx, %s\n", np->mr_base, np->mr_end, 132 np->mr_param.name); 133 } 134 perror = pthread_rwlock_unlock(&mmio_rwlock); 135 assert(perror == 0); 136 } 137 #endif 138 139 RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare); 140 141 typedef int (mem_cb_t)(struct vmctx *ctx, int vcpu, uint64_t gpa, 142 struct mem_range *mr, void *arg); 143 144 static int 145 mem_read(void *ctx, int vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg) 146 { 147 int error; 148 struct mem_range *mr = arg; 149 150 error = (*mr->handler)(ctx, vcpu, MEM_F_READ, gpa, size, 151 rval, mr->arg1, mr->arg2); 152 return (error); 153 } 154 155 static int 156 mem_write(void *ctx, int vcpu, uint64_t gpa, uint64_t wval, int size, void *arg) 157 { 158 int error; 159 struct mem_range *mr = arg; 160 161 error = (*mr->handler)(ctx, vcpu, MEM_F_WRITE, gpa, size, 162 &wval, mr->arg1, mr->arg2); 163 return (error); 164 } 165 166 static int 167 access_memory(struct vmctx *ctx, int vcpu, uint64_t paddr, mem_cb_t *cb, 168 void *arg) 169 { 170 struct mmio_rb_range *entry; 171 int err, perror, immutable; 172 173 pthread_rwlock_rdlock(&mmio_rwlock); 174 /* 175 * First check the per-vCPU cache 176 */ 177 if (mmio_hint[vcpu] && 178 paddr >= mmio_hint[vcpu]->mr_base && 179 paddr <= mmio_hint[vcpu]->mr_end) { 180 entry = mmio_hint[vcpu]; 181 } else 182 entry = NULL; 183 184 if (entry == NULL) { 185 if (mmio_rb_lookup(&mmio_rb_root, paddr, &entry) == 0) { 186 /* Update the per-vCPU cache */ 187 mmio_hint[vcpu] = entry; 188 } else if (mmio_rb_lookup(&mmio_rb_fallback, paddr, &entry)) { 189 perror = pthread_rwlock_unlock(&mmio_rwlock); 190 assert(perror == 0); 191 return (ESRCH); 192 } 193 } 194 195 assert(entry != NULL); 196 197 /* 198 * An 'immutable' memory range is guaranteed to be never removed 199 * so there is no need to hold 'mmio_rwlock' while calling the 200 * handler. 201 * 202 * XXX writes to the PCIR_COMMAND register can cause register_mem() 203 * to be called. If the guest is using PCI extended config space 204 * to modify the PCIR_COMMAND register then register_mem() can 205 * deadlock on 'mmio_rwlock'. However by registering the extended 206 * config space window as 'immutable' the deadlock can be avoided. 207 */ 208 immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE); 209 if (immutable) { 210 perror = pthread_rwlock_unlock(&mmio_rwlock); 211 assert(perror == 0); 212 } 213 214 err = cb(ctx, vcpu, paddr, &entry->mr_param, arg); 215 216 if (!immutable) { 217 perror = pthread_rwlock_unlock(&mmio_rwlock); 218 assert(perror == 0); 219 } 220 221 222 return (err); 223 } 224 225 struct emulate_mem_args { 226 struct vie *vie; 227 struct vm_guest_paging *paging; 228 }; 229 230 static int 231 emulate_mem_cb(struct vmctx *ctx, int vcpu, uint64_t paddr, struct mem_range *mr, 232 void *arg) 233 { 234 struct emulate_mem_args *ema; 235 236 ema = arg; 237 return (vmm_emulate_instruction(ctx, vcpu, paddr, ema->vie, ema->paging, 238 mem_read, mem_write, mr)); 239 } 240 241 int 242 emulate_mem(struct vmctx *ctx, int vcpu, uint64_t paddr, struct vie *vie, 243 struct vm_guest_paging *paging) 244 245 { 246 struct emulate_mem_args ema; 247 248 ema.vie = vie; 249 ema.paging = paging; 250 return (access_memory(ctx, vcpu, paddr, emulate_mem_cb, &ema)); 251 } 252 253 struct read_mem_args { 254 uint64_t *rval; 255 int size; 256 }; 257 258 static int 259 read_mem_cb(struct vmctx *ctx, int vcpu, uint64_t paddr, struct mem_range *mr, 260 void *arg) 261 { 262 struct read_mem_args *rma; 263 264 rma = arg; 265 return (mr->handler(ctx, vcpu, MEM_F_READ, paddr, rma->size, 266 rma->rval, mr->arg1, mr->arg2)); 267 } 268 269 int 270 read_mem(struct vmctx *ctx, int vcpu, uint64_t gpa, uint64_t *rval, int size) 271 { 272 struct read_mem_args rma; 273 274 rma.rval = rval; 275 rma.size = size; 276 return (access_memory(ctx, vcpu, gpa, read_mem_cb, &rma)); 277 } 278 279 static int 280 register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp) 281 { 282 struct mmio_rb_range *entry, *mrp; 283 int err, perror; 284 285 err = 0; 286 287 mrp = malloc(sizeof(struct mmio_rb_range)); 288 289 if (mrp != NULL) { 290 mrp->mr_param = *memp; 291 mrp->mr_base = memp->base; 292 mrp->mr_end = memp->base + memp->size - 1; 293 pthread_rwlock_wrlock(&mmio_rwlock); 294 if (mmio_rb_lookup(rbt, memp->base, &entry) != 0) 295 err = mmio_rb_add(rbt, mrp); 296 perror = pthread_rwlock_unlock(&mmio_rwlock); 297 assert(perror == 0); 298 if (err) 299 free(mrp); 300 } else 301 err = ENOMEM; 302 303 return (err); 304 } 305 306 int 307 register_mem(struct mem_range *memp) 308 { 309 310 return (register_mem_int(&mmio_rb_root, memp)); 311 } 312 313 int 314 register_mem_fallback(struct mem_range *memp) 315 { 316 317 return (register_mem_int(&mmio_rb_fallback, memp)); 318 } 319 320 int 321 unregister_mem(struct mem_range *memp) 322 { 323 struct mem_range *mr; 324 struct mmio_rb_range *entry = NULL; 325 int err, perror, i; 326 327 pthread_rwlock_wrlock(&mmio_rwlock); 328 err = mmio_rb_lookup(&mmio_rb_root, memp->base, &entry); 329 if (err == 0) { 330 mr = &entry->mr_param; 331 assert(mr->name == memp->name); 332 assert(mr->base == memp->base && mr->size == memp->size); 333 assert((mr->flags & MEM_F_IMMUTABLE) == 0); 334 RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry); 335 336 /* flush Per-vCPU cache */ 337 for (i=0; i < VM_MAXCPU; i++) { 338 if (mmio_hint[i] == entry) 339 mmio_hint[i] = NULL; 340 } 341 } 342 perror = pthread_rwlock_unlock(&mmio_rwlock); 343 assert(perror == 0); 344 345 if (entry) 346 free(entry); 347 348 return (err); 349 } 350 351 void 352 init_mem(void) 353 { 354 355 RB_INIT(&mmio_rb_root); 356 RB_INIT(&mmio_rb_fallback); 357 pthread_rwlock_init(&mmio_rwlock, NULL); 358 } 359