xref: /freebsd/usr.sbin/bhyve/mem.c (revision 190cef3d52236565eb22e18b33e9e865ec634aa3)
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 
52 #include "mem.h"
53 
54 struct mmio_rb_range {
55 	RB_ENTRY(mmio_rb_range)	mr_link;	/* RB tree links */
56 	struct mem_range	mr_param;
57 	uint64_t                mr_base;
58 	uint64_t                mr_end;
59 };
60 
61 struct mmio_rb_tree;
62 RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
63 
64 RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback;
65 
66 /*
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
69  * result of a lookup.
70  */
71 static struct mmio_rb_range	*mmio_hint[VM_MAXCPU];
72 
73 static pthread_rwlock_t mmio_rwlock;
74 
75 static int
76 mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b)
77 {
78 	if (a->mr_end < b->mr_base)
79 		return (-1);
80 	else if (a->mr_base > b->mr_end)
81 		return (1);
82 	return (0);
83 }
84 
85 static int
86 mmio_rb_lookup(struct mmio_rb_tree *rbt, uint64_t addr,
87     struct mmio_rb_range **entry)
88 {
89 	struct mmio_rb_range find, *res;
90 
91 	find.mr_base = find.mr_end = addr;
92 
93 	res = RB_FIND(mmio_rb_tree, rbt, &find);
94 
95 	if (res != NULL) {
96 		*entry = res;
97 		return (0);
98 	}
99 
100 	return (ENOENT);
101 }
102 
103 static int
104 mmio_rb_add(struct mmio_rb_tree *rbt, struct mmio_rb_range *new)
105 {
106 	struct mmio_rb_range *overlap;
107 
108 	overlap = RB_INSERT(mmio_rb_tree, rbt, new);
109 
110 	if (overlap != NULL) {
111 #ifdef RB_DEBUG
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);
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 vmctx *ctx, int vcpu, uint64_t gpa,
143     struct mem_range *mr, void *arg);
144 
145 static int
146 mem_read(void *ctx, int 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)(ctx, vcpu, MEM_F_READ, gpa, size,
152 			       rval, mr->arg1, mr->arg2);
153 	return (error);
154 }
155 
156 static int
157 mem_write(void *ctx, int 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)(ctx, vcpu, MEM_F_WRITE, gpa, size,
163 			       &wval, mr->arg1, mr->arg2);
164 	return (error);
165 }
166 
167 static int
168 access_memory(struct vmctx *ctx, int vcpu, uint64_t paddr, mem_cb_t *cb,
169     void *arg)
170 {
171 	struct mmio_rb_range *entry;
172 	int err, perror, immutable;
173 
174 	pthread_rwlock_rdlock(&mmio_rwlock);
175 	/*
176 	 * First check the per-vCPU cache
177 	 */
178 	if (mmio_hint[vcpu] &&
179 	    paddr >= mmio_hint[vcpu]->mr_base &&
180 	    paddr <= mmio_hint[vcpu]->mr_end) {
181 		entry = mmio_hint[vcpu];
182 	} else
183 		entry = NULL;
184 
185 	if (entry == NULL) {
186 		if (mmio_rb_lookup(&mmio_rb_root, paddr, &entry) == 0) {
187 			/* Update the per-vCPU cache */
188 			mmio_hint[vcpu] = entry;
189 		} else if (mmio_rb_lookup(&mmio_rb_fallback, paddr, &entry)) {
190 			perror = pthread_rwlock_unlock(&mmio_rwlock);
191 			assert(perror == 0);
192 			return (ESRCH);
193 		}
194 	}
195 
196 	assert(entry != NULL);
197 
198 	/*
199 	 * An 'immutable' memory range is guaranteed to be never removed
200 	 * so there is no need to hold 'mmio_rwlock' while calling the
201 	 * handler.
202 	 *
203 	 * XXX writes to the PCIR_COMMAND register can cause register_mem()
204 	 * to be called. If the guest is using PCI extended config space
205 	 * to modify the PCIR_COMMAND register then register_mem() can
206 	 * deadlock on 'mmio_rwlock'. However by registering the extended
207 	 * config space window as 'immutable' the deadlock can be avoided.
208 	 */
209 	immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE);
210 	if (immutable) {
211 		perror = pthread_rwlock_unlock(&mmio_rwlock);
212 		assert(perror == 0);
213 	}
214 
215 	err = cb(ctx, vcpu, paddr, &entry->mr_param, arg);
216 
217 	if (!immutable) {
218 		perror = pthread_rwlock_unlock(&mmio_rwlock);
219 		assert(perror == 0);
220 	}
221 
222 
223 	return (err);
224 }
225 
226 struct emulate_mem_args {
227 	struct vie *vie;
228 	struct vm_guest_paging *paging;
229 };
230 
231 static int
232 emulate_mem_cb(struct vmctx *ctx, int vcpu, uint64_t paddr, struct mem_range *mr,
233     void *arg)
234 {
235 	struct emulate_mem_args *ema;
236 
237 	ema = arg;
238 	return (vmm_emulate_instruction(ctx, vcpu, paddr, ema->vie, ema->paging,
239 	    mem_read, mem_write, mr));
240 }
241 
242 int
243 emulate_mem(struct vmctx *ctx, int vcpu, uint64_t paddr, struct vie *vie,
244     struct vm_guest_paging *paging)
245 
246 {
247 	struct emulate_mem_args ema;
248 
249 	ema.vie = vie;
250 	ema.paging = paging;
251 	return (access_memory(ctx, vcpu, paddr, emulate_mem_cb, &ema));
252 }
253 
254 struct read_mem_args {
255 	uint64_t *rval;
256 	int size;
257 };
258 
259 static int
260 read_mem_cb(struct vmctx *ctx, int vcpu, uint64_t paddr, struct mem_range *mr,
261     void *arg)
262 {
263 	struct read_mem_args *rma;
264 
265 	rma = arg;
266 	return (mr->handler(ctx, vcpu, MEM_F_READ, paddr, rma->size,
267 	    rma->rval, mr->arg1, mr->arg2));
268 }
269 
270 int
271 read_mem(struct vmctx *ctx, int vcpu, uint64_t gpa, uint64_t *rval, int size)
272 {
273 	struct read_mem_args rma;
274 
275 	rma.rval = rval;
276 	rma.size = size;
277 	return (access_memory(ctx, vcpu, gpa, read_mem_cb, &rma));
278 }
279 
280 static int
281 register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp)
282 {
283 	struct mmio_rb_range *entry, *mrp;
284 	int err, perror;
285 
286 	err = 0;
287 
288 	mrp = malloc(sizeof(struct mmio_rb_range));
289 	if (mrp == NULL) {
290 		warn("%s: couldn't allocate memory for mrp\n",
291 		     __func__);
292 		err = ENOMEM;
293 	} else {
294 		mrp->mr_param = *memp;
295 		mrp->mr_base = memp->base;
296 		mrp->mr_end = memp->base + memp->size - 1;
297 		pthread_rwlock_wrlock(&mmio_rwlock);
298 		if (mmio_rb_lookup(rbt, memp->base, &entry) != 0)
299 			err = mmio_rb_add(rbt, mrp);
300 		perror = pthread_rwlock_unlock(&mmio_rwlock);
301 		assert(perror == 0);
302 		if (err)
303 			free(mrp);
304 	}
305 
306 	return (err);
307 }
308 
309 int
310 register_mem(struct mem_range *memp)
311 {
312 
313 	return (register_mem_int(&mmio_rb_root, memp));
314 }
315 
316 int
317 register_mem_fallback(struct mem_range *memp)
318 {
319 
320 	return (register_mem_int(&mmio_rb_fallback, memp));
321 }
322 
323 int
324 unregister_mem(struct mem_range *memp)
325 {
326 	struct mem_range *mr;
327 	struct mmio_rb_range *entry = NULL;
328 	int err, perror, i;
329 
330 	pthread_rwlock_wrlock(&mmio_rwlock);
331 	err = mmio_rb_lookup(&mmio_rb_root, memp->base, &entry);
332 	if (err == 0) {
333 		mr = &entry->mr_param;
334 		assert(mr->name == memp->name);
335 		assert(mr->base == memp->base && mr->size == memp->size);
336 		assert((mr->flags & MEM_F_IMMUTABLE) == 0);
337 		RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry);
338 
339 		/* flush Per-vCPU cache */
340 		for (i=0; i < VM_MAXCPU; i++) {
341 			if (mmio_hint[i] == entry)
342 				mmio_hint[i] = NULL;
343 		}
344 	}
345 	perror = pthread_rwlock_unlock(&mmio_rwlock);
346 	assert(perror == 0);
347 
348 	if (entry)
349 		free(entry);
350 
351 	return (err);
352 }
353 
354 void
355 init_mem(void)
356 {
357 
358 	RB_INIT(&mmio_rb_root);
359 	RB_INIT(&mmio_rb_fallback);
360 	pthread_rwlock_init(&mmio_rwlock, NULL);
361 }
362