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/errno.h> 42 #include <sys/tree.h> 43 #include <machine/vmm.h> 44 #include <machine/vmm_instruction_emul.h> 45 46 #include <assert.h> 47 #include <err.h> 48 #include <pthread.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <vmmapi.h> 52 53 #include "mem.h" 54 55 struct mmio_rb_range { 56 RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */ 57 struct mem_range mr_param; 58 uint64_t mr_base; 59 uint64_t mr_end; 60 }; 61 62 struct mmio_rb_tree; 63 RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare); 64 65 static RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback; 66 67 /* 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 70 * result of a lookup. 71 */ 72 static struct mmio_rb_range **mmio_hint; 73 static int mmio_ncpu; 74 75 static pthread_rwlock_t mmio_rwlock; 76 77 static int 78 mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b) 79 { 80 if (a->mr_end < b->mr_base) 81 return (-1); 82 else if (a->mr_base > b->mr_end) 83 return (1); 84 return (0); 85 } 86 87 static int 88 mmio_rb_lookup(struct mmio_rb_tree *rbt, uint64_t addr, 89 struct mmio_rb_range **entry) 90 { 91 struct mmio_rb_range find, *res; 92 93 find.mr_base = find.mr_end = addr; 94 95 res = RB_FIND(mmio_rb_tree, rbt, &find); 96 97 if (res != NULL) { 98 *entry = res; 99 return (0); 100 } 101 102 return (ENOENT); 103 } 104 105 static int 106 mmio_rb_add(struct mmio_rb_tree *rbt, struct mmio_rb_range *new) 107 { 108 struct mmio_rb_range *overlap; 109 110 overlap = RB_INSERT(mmio_rb_tree, rbt, new); 111 112 if (overlap != NULL) { 113 #ifdef RB_DEBUG 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); 119 #endif 120 121 return (EEXIST); 122 } 123 124 return (0); 125 } 126 127 #if 0 128 static void 129 mmio_rb_dump(struct mmio_rb_tree *rbt) 130 { 131 int perror; 132 struct mmio_rb_range *np; 133 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, 137 np->mr_param.name); 138 } 139 perror = pthread_rwlock_unlock(&mmio_rwlock); 140 assert(perror == 0); 141 } 142 #endif 143 144 RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare); 145 146 typedef int (mem_cb_t)(struct vcpu *vcpu, uint64_t gpa, struct mem_range *mr, 147 void *arg); 148 149 static int 150 mem_read(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg) 151 { 152 int error; 153 struct mem_range *mr = arg; 154 155 error = (*mr->handler)(vcpu, MEM_F_READ, gpa, size, rval, mr->arg1, 156 mr->arg2); 157 return (error); 158 } 159 160 static int 161 mem_write(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size, void *arg) 162 { 163 int error; 164 struct mem_range *mr = arg; 165 166 error = (*mr->handler)(vcpu, MEM_F_WRITE, gpa, size, &wval, mr->arg1, 167 mr->arg2); 168 return (error); 169 } 170 171 static int 172 access_memory(struct vcpu *vcpu, uint64_t paddr, mem_cb_t *cb, void *arg) 173 { 174 struct mmio_rb_range *entry; 175 int err, perror, immutable, vcpuid; 176 177 vcpuid = vcpu_id(vcpu); 178 pthread_rwlock_rdlock(&mmio_rwlock); 179 /* 180 * First check the per-vCPU cache 181 */ 182 if (mmio_hint[vcpuid] && 183 paddr >= mmio_hint[vcpuid]->mr_base && 184 paddr <= mmio_hint[vcpuid]->mr_end) { 185 entry = mmio_hint[vcpuid]; 186 } else 187 entry = NULL; 188 189 if (entry == NULL) { 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); 195 assert(perror == 0); 196 return (ESRCH); 197 } 198 } 199 200 assert(entry != NULL); 201 202 /* 203 * An 'immutable' memory range is guaranteed to be never removed 204 * so there is no need to hold 'mmio_rwlock' while calling the 205 * handler. 206 * 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. 212 */ 213 immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE); 214 if (immutable) { 215 perror = pthread_rwlock_unlock(&mmio_rwlock); 216 assert(perror == 0); 217 } 218 219 err = cb(vcpu, paddr, &entry->mr_param, arg); 220 221 if (!immutable) { 222 perror = pthread_rwlock_unlock(&mmio_rwlock); 223 assert(perror == 0); 224 } 225 226 return (err); 227 } 228 229 struct emulate_mem_args { 230 struct vie *vie; 231 struct vm_guest_paging *paging; 232 }; 233 234 static int 235 emulate_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr, 236 void *arg) 237 { 238 struct emulate_mem_args *ema; 239 240 ema = arg; 241 return (vmm_emulate_instruction(vcpu, paddr, ema->vie, ema->paging, 242 mem_read, mem_write, mr)); 243 } 244 245 int 246 emulate_mem(struct vcpu *vcpu, uint64_t paddr, struct vie *vie, 247 struct vm_guest_paging *paging) 248 { 249 struct emulate_mem_args ema; 250 251 ema.vie = vie; 252 ema.paging = paging; 253 return (access_memory(vcpu, paddr, emulate_mem_cb, &ema)); 254 } 255 256 struct rw_mem_args { 257 uint64_t *val; 258 int size; 259 int operation; 260 }; 261 262 static int 263 rw_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr, void *arg) 264 { 265 struct rw_mem_args *rma; 266 267 rma = arg; 268 return (mr->handler(vcpu, rma->operation, paddr, rma->size, 269 rma->val, mr->arg1, mr->arg2)); 270 } 271 272 int 273 read_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size) 274 { 275 struct rw_mem_args rma; 276 277 rma.val = rval; 278 rma.size = size; 279 rma.operation = MEM_F_READ; 280 return (access_memory(vcpu, gpa, rw_mem_cb, &rma)); 281 } 282 283 int 284 write_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size) 285 { 286 struct rw_mem_args rma; 287 288 rma.val = &wval; 289 rma.size = size; 290 rma.operation = MEM_F_WRITE; 291 return (access_memory(vcpu, gpa, rw_mem_cb, &rma)); 292 } 293 294 static int 295 register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp) 296 { 297 struct mmio_rb_range *entry, *mrp; 298 int err, perror; 299 300 err = 0; 301 302 mrp = malloc(sizeof(struct mmio_rb_range)); 303 if (mrp == NULL) { 304 warn("%s: couldn't allocate memory for mrp\n", 305 __func__); 306 err = ENOMEM; 307 } else { 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); 315 assert(perror == 0); 316 if (err) 317 free(mrp); 318 } 319 320 return (err); 321 } 322 323 int 324 register_mem(struct mem_range *memp) 325 { 326 327 return (register_mem_int(&mmio_rb_root, memp)); 328 } 329 330 int 331 register_mem_fallback(struct mem_range *memp) 332 { 333 334 return (register_mem_int(&mmio_rb_fallback, memp)); 335 } 336 337 int 338 unregister_mem(struct mem_range *memp) 339 { 340 struct mem_range *mr; 341 struct mmio_rb_range *entry = NULL; 342 int err, perror, i; 343 344 pthread_rwlock_wrlock(&mmio_rwlock); 345 err = mmio_rb_lookup(&mmio_rb_root, memp->base, &entry); 346 if (err == 0) { 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); 352 353 /* flush Per-vCPU cache */ 354 for (i = 0; i < mmio_ncpu; i++) { 355 if (mmio_hint[i] == entry) 356 mmio_hint[i] = NULL; 357 } 358 } 359 perror = pthread_rwlock_unlock(&mmio_rwlock); 360 assert(perror == 0); 361 362 if (entry) 363 free(entry); 364 365 return (err); 366 } 367 368 void 369 init_mem(int ncpu) 370 { 371 372 mmio_ncpu = ncpu; 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); 377 } 378