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