xref: /freebsd/usr.sbin/bhyve/mem.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/types.h>
39 #include <sys/errno.h>
40 #include <sys/tree.h>
41 #include <machine/vmm.h>
42 #include <machine/vmm_instruction_emul.h>
43 
44 #include <assert.h>
45 #include <err.h>
46 #include <pthread.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <vmmapi.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 static 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;
71 static int mmio_ncpu;
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, '%s' "
113 		       "claims region already claimed for '%s'\n",
114 		       new->mr_base, new->mr_end,
115 		       overlap->mr_base, overlap->mr_end,
116 		       new->mr_param.name, overlap->mr_param.name);
117 #endif
118 
119 		return (EEXIST);
120 	}
121 
122 	return (0);
123 }
124 
125 #if 0
126 static void
127 mmio_rb_dump(struct mmio_rb_tree *rbt)
128 {
129 	int perror;
130 	struct mmio_rb_range *np;
131 
132 	pthread_rwlock_rdlock(&mmio_rwlock);
133 	RB_FOREACH(np, mmio_rb_tree, rbt) {
134 		printf(" %lx:%lx, %s\n", np->mr_base, np->mr_end,
135 		       np->mr_param.name);
136 	}
137 	perror = pthread_rwlock_unlock(&mmio_rwlock);
138 	assert(perror == 0);
139 }
140 #endif
141 
142 RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
143 
144 typedef int (mem_cb_t)(struct vcpu *vcpu, uint64_t gpa, struct mem_range *mr,
145     void *arg);
146 
147 static int
148 mem_read(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg)
149 {
150 	int error;
151 	struct mem_range *mr = arg;
152 
153 	error = (*mr->handler)(vcpu, MEM_F_READ, gpa, size, rval, mr->arg1,
154 	    mr->arg2);
155 	return (error);
156 }
157 
158 static int
159 mem_write(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size, void *arg)
160 {
161 	int error;
162 	struct mem_range *mr = arg;
163 
164 	error = (*mr->handler)(vcpu, MEM_F_WRITE, gpa, size, &wval, mr->arg1,
165 	    mr->arg2);
166 	return (error);
167 }
168 
169 static int
170 access_memory(struct vcpu *vcpu, uint64_t paddr, mem_cb_t *cb, void *arg)
171 {
172 	struct mmio_rb_range *entry;
173 	int err, perror, immutable, vcpuid;
174 
175 	vcpuid = vcpu_id(vcpu);
176 	pthread_rwlock_rdlock(&mmio_rwlock);
177 	/*
178 	 * First check the per-vCPU cache
179 	 */
180 	if (mmio_hint[vcpuid] &&
181 	    paddr >= mmio_hint[vcpuid]->mr_base &&
182 	    paddr <= mmio_hint[vcpuid]->mr_end) {
183 		entry = mmio_hint[vcpuid];
184 	} else
185 		entry = NULL;
186 
187 	if (entry == NULL) {
188 		if (mmio_rb_lookup(&mmio_rb_root, paddr, &entry) == 0) {
189 			/* Update the per-vCPU cache */
190 			mmio_hint[vcpuid] = entry;
191 		} else if (mmio_rb_lookup(&mmio_rb_fallback, paddr, &entry)) {
192 			perror = pthread_rwlock_unlock(&mmio_rwlock);
193 			assert(perror == 0);
194 			return (ESRCH);
195 		}
196 	}
197 
198 	assert(entry != NULL);
199 
200 	/*
201 	 * An 'immutable' memory range is guaranteed to be never removed
202 	 * so there is no need to hold 'mmio_rwlock' while calling the
203 	 * handler.
204 	 *
205 	 * XXX writes to the PCIR_COMMAND register can cause register_mem()
206 	 * to be called. If the guest is using PCI extended config space
207 	 * to modify the PCIR_COMMAND register then register_mem() can
208 	 * deadlock on 'mmio_rwlock'. However by registering the extended
209 	 * config space window as 'immutable' the deadlock can be avoided.
210 	 */
211 	immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE);
212 	if (immutable) {
213 		perror = pthread_rwlock_unlock(&mmio_rwlock);
214 		assert(perror == 0);
215 	}
216 
217 	err = cb(vcpu, paddr, &entry->mr_param, arg);
218 
219 	if (!immutable) {
220 		perror = pthread_rwlock_unlock(&mmio_rwlock);
221 		assert(perror == 0);
222 	}
223 
224 	return (err);
225 }
226 
227 struct emulate_mem_args {
228 	struct vie *vie;
229 	struct vm_guest_paging *paging;
230 };
231 
232 static int
233 emulate_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr,
234     void *arg)
235 {
236 	struct emulate_mem_args *ema;
237 
238 	ema = arg;
239 	return (vmm_emulate_instruction(vcpu, paddr, ema->vie, ema->paging,
240 	    mem_read, mem_write, mr));
241 }
242 
243 int
244 emulate_mem(struct vcpu *vcpu, uint64_t paddr, struct vie *vie,
245     struct vm_guest_paging *paging)
246 {
247 	struct emulate_mem_args ema;
248 
249 	ema.vie = vie;
250 	ema.paging = paging;
251 	return (access_memory(vcpu, paddr, emulate_mem_cb, &ema));
252 }
253 
254 struct rw_mem_args {
255 	uint64_t *val;
256 	int size;
257 	int operation;
258 };
259 
260 static int
261 rw_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr, void *arg)
262 {
263 	struct rw_mem_args *rma;
264 
265 	rma = arg;
266 	return (mr->handler(vcpu, rma->operation, paddr, rma->size,
267 	    rma->val, mr->arg1, mr->arg2));
268 }
269 
270 int
271 read_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size)
272 {
273 	struct rw_mem_args rma;
274 
275 	rma.val = rval;
276 	rma.size = size;
277 	rma.operation = MEM_F_READ;
278 	return (access_memory(vcpu, gpa, rw_mem_cb, &rma));
279 }
280 
281 int
282 write_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size)
283 {
284 	struct rw_mem_args rma;
285 
286 	rma.val = &wval;
287 	rma.size = size;
288 	rma.operation = MEM_F_WRITE;
289 	return (access_memory(vcpu, gpa, rw_mem_cb, &rma));
290 }
291 
292 static int
293 register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp)
294 {
295 	struct mmio_rb_range *entry, *mrp;
296 	int err, perror;
297 
298 	err = 0;
299 
300 	mrp = malloc(sizeof(struct mmio_rb_range));
301 	if (mrp == NULL) {
302 		warn("%s: couldn't allocate memory for mrp\n",
303 		     __func__);
304 		err = ENOMEM;
305 	} else {
306 		mrp->mr_param = *memp;
307 		mrp->mr_base = memp->base;
308 		mrp->mr_end = memp->base + memp->size - 1;
309 		pthread_rwlock_wrlock(&mmio_rwlock);
310 		if (mmio_rb_lookup(rbt, memp->base, &entry) != 0)
311 			err = mmio_rb_add(rbt, mrp);
312 		perror = pthread_rwlock_unlock(&mmio_rwlock);
313 		assert(perror == 0);
314 		if (err)
315 			free(mrp);
316 	}
317 
318 	return (err);
319 }
320 
321 int
322 register_mem(struct mem_range *memp)
323 {
324 
325 	return (register_mem_int(&mmio_rb_root, memp));
326 }
327 
328 int
329 register_mem_fallback(struct mem_range *memp)
330 {
331 
332 	return (register_mem_int(&mmio_rb_fallback, memp));
333 }
334 
335 int
336 unregister_mem(struct mem_range *memp)
337 {
338 	struct mem_range *mr;
339 	struct mmio_rb_range *entry = NULL;
340 	int err, perror, i;
341 
342 	pthread_rwlock_wrlock(&mmio_rwlock);
343 	err = mmio_rb_lookup(&mmio_rb_root, memp->base, &entry);
344 	if (err == 0) {
345 		mr = &entry->mr_param;
346 		assert(mr->name == memp->name);
347 		assert(mr->base == memp->base && mr->size == memp->size);
348 		assert((mr->flags & MEM_F_IMMUTABLE) == 0);
349 		RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry);
350 
351 		/* flush Per-vCPU cache */
352 		for (i = 0; i < mmio_ncpu; i++) {
353 			if (mmio_hint[i] == entry)
354 				mmio_hint[i] = NULL;
355 		}
356 	}
357 	perror = pthread_rwlock_unlock(&mmio_rwlock);
358 	assert(perror == 0);
359 
360 	if (entry)
361 		free(entry);
362 
363 	return (err);
364 }
365 
366 void
367 init_mem(int ncpu)
368 {
369 
370 	mmio_ncpu = ncpu;
371 	mmio_hint = calloc(ncpu, sizeof(*mmio_hint));
372 	RB_INIT(&mmio_rb_root);
373 	RB_INIT(&mmio_rb_fallback);
374 	pthread_rwlock_init(&mmio_rwlock, NULL);
375 }
376