14d1e669cSPeter Grehan /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni *
44d1e669cSPeter Grehan * Copyright (c) 2012 NetApp, Inc.
54d1e669cSPeter Grehan * All rights reserved.
64d1e669cSPeter Grehan *
74d1e669cSPeter Grehan * Redistribution and use in source and binary forms, with or without
84d1e669cSPeter Grehan * modification, are permitted provided that the following conditions
94d1e669cSPeter Grehan * are met:
104d1e669cSPeter Grehan * 1. Redistributions of source code must retain the above copyright
114d1e669cSPeter Grehan * notice, this list of conditions and the following disclaimer.
124d1e669cSPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright
134d1e669cSPeter Grehan * notice, this list of conditions and the following disclaimer in the
144d1e669cSPeter Grehan * documentation and/or other materials provided with the distribution.
154d1e669cSPeter Grehan *
164d1e669cSPeter Grehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
174d1e669cSPeter Grehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184d1e669cSPeter Grehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194d1e669cSPeter Grehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
204d1e669cSPeter Grehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214d1e669cSPeter Grehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224d1e669cSPeter Grehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234d1e669cSPeter Grehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244d1e669cSPeter Grehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254d1e669cSPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264d1e669cSPeter Grehan * SUCH DAMAGE.
274d1e669cSPeter Grehan */
284d1e669cSPeter Grehan
294d1e669cSPeter Grehan /*
304d1e669cSPeter Grehan * Memory ranges are represented with an RB tree. On insertion, the range
314d1e669cSPeter Grehan * is checked for overlaps. On lookup, the key has the same base and limit
324d1e669cSPeter Grehan * so it can be searched within the range.
334d1e669cSPeter Grehan */
344d1e669cSPeter Grehan
354d1e669cSPeter Grehan #include <sys/types.h>
364d1e669cSPeter Grehan #include <sys/errno.h>
37f2b5dc3aSMarcelo Araujo #include <sys/tree.h>
384d1e669cSPeter Grehan #include <machine/vmm.h>
39e813a873SNeel Natu #include <machine/vmm_instruction_emul.h>
404d1e669cSPeter Grehan
41f2b5dc3aSMarcelo Araujo #include <assert.h>
42f2b5dc3aSMarcelo Araujo #include <err.h>
43f2b5dc3aSMarcelo Araujo #include <pthread.h>
444d1e669cSPeter Grehan #include <stdio.h>
454d1e669cSPeter Grehan #include <stdlib.h>
467d9ef309SJohn Baldwin #include <vmmapi.h>
474d1e669cSPeter Grehan
484d1e669cSPeter Grehan #include "mem.h"
494d1e669cSPeter Grehan
504d1e669cSPeter Grehan struct mmio_rb_range {
514d1e669cSPeter Grehan RB_ENTRY(mmio_rb_range) mr_link; /* RB tree links */
524d1e669cSPeter Grehan struct mem_range mr_param;
534d1e669cSPeter Grehan uint64_t mr_base;
544d1e669cSPeter Grehan uint64_t mr_end;
554d1e669cSPeter Grehan };
564d1e669cSPeter Grehan
574d1e669cSPeter Grehan struct mmio_rb_tree;
584d1e669cSPeter Grehan RB_PROTOTYPE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
594d1e669cSPeter Grehan
6037045dfaSMark Johnston static RB_HEAD(mmio_rb_tree, mmio_rb_range) mmio_rb_root, mmio_rb_fallback;
614d1e669cSPeter Grehan
624d1e669cSPeter Grehan /*
634d1e669cSPeter Grehan * Per-vCPU cache. Since most accesses from a vCPU will be to
644d1e669cSPeter Grehan * consecutive addresses in a range, it makes sense to cache the
654d1e669cSPeter Grehan * result of a lookup.
664d1e669cSPeter Grehan */
67730510dcSJohn Baldwin static struct mmio_rb_range **mmio_hint;
68730510dcSJohn Baldwin static int mmio_ncpu;
694d1e669cSPeter Grehan
70ae551da6SNeel Natu static pthread_rwlock_t mmio_rwlock;
71028d9311SNeel Natu
724d1e669cSPeter Grehan static int
mmio_rb_range_compare(struct mmio_rb_range * a,struct mmio_rb_range * b)734d1e669cSPeter Grehan mmio_rb_range_compare(struct mmio_rb_range *a, struct mmio_rb_range *b)
744d1e669cSPeter Grehan {
754d1e669cSPeter Grehan if (a->mr_end < b->mr_base)
764d1e669cSPeter Grehan return (-1);
774d1e669cSPeter Grehan else if (a->mr_base > b->mr_end)
784d1e669cSPeter Grehan return (1);
794d1e669cSPeter Grehan return (0);
804d1e669cSPeter Grehan }
814d1e669cSPeter Grehan
824d1e669cSPeter Grehan static int
mmio_rb_lookup(struct mmio_rb_tree * rbt,uint64_t addr,struct mmio_rb_range ** entry)830ab13648SPeter Grehan mmio_rb_lookup(struct mmio_rb_tree *rbt, uint64_t addr,
840ab13648SPeter Grehan struct mmio_rb_range **entry)
854d1e669cSPeter Grehan {
864d1e669cSPeter Grehan struct mmio_rb_range find, *res;
874d1e669cSPeter Grehan
884d1e669cSPeter Grehan find.mr_base = find.mr_end = addr;
894d1e669cSPeter Grehan
900ab13648SPeter Grehan res = RB_FIND(mmio_rb_tree, rbt, &find);
914d1e669cSPeter Grehan
924d1e669cSPeter Grehan if (res != NULL) {
934d1e669cSPeter Grehan *entry = res;
944d1e669cSPeter Grehan return (0);
954d1e669cSPeter Grehan }
964d1e669cSPeter Grehan
974d1e669cSPeter Grehan return (ENOENT);
984d1e669cSPeter Grehan }
994d1e669cSPeter Grehan
1004d1e669cSPeter Grehan static int
mmio_rb_add(struct mmio_rb_tree * rbt,struct mmio_rb_range * new)1010ab13648SPeter Grehan mmio_rb_add(struct mmio_rb_tree *rbt, struct mmio_rb_range *new)
1024d1e669cSPeter Grehan {
1034d1e669cSPeter Grehan struct mmio_rb_range *overlap;
1044d1e669cSPeter Grehan
1050ab13648SPeter Grehan overlap = RB_INSERT(mmio_rb_tree, rbt, new);
1064d1e669cSPeter Grehan
1074d1e669cSPeter Grehan if (overlap != NULL) {
1084d1e669cSPeter Grehan #ifdef RB_DEBUG
109efec757bSRobert Crowston printf("overlap detected: new %lx:%lx, tree %lx:%lx, '%s' "
110efec757bSRobert Crowston "claims region already claimed for '%s'\n",
1114d1e669cSPeter Grehan new->mr_base, new->mr_end,
112efec757bSRobert Crowston overlap->mr_base, overlap->mr_end,
113efec757bSRobert Crowston new->mr_param.name, overlap->mr_param.name);
1144d1e669cSPeter Grehan #endif
1154d1e669cSPeter Grehan
1164d1e669cSPeter Grehan return (EEXIST);
1174d1e669cSPeter Grehan }
1184d1e669cSPeter Grehan
1194d1e669cSPeter Grehan return (0);
1204d1e669cSPeter Grehan }
1214d1e669cSPeter Grehan
1224d1e669cSPeter Grehan #if 0
1234d1e669cSPeter Grehan static void
1240ab13648SPeter Grehan mmio_rb_dump(struct mmio_rb_tree *rbt)
1254d1e669cSPeter Grehan {
1265f4c83abSMarcelo Araujo int perror;
1274d1e669cSPeter Grehan struct mmio_rb_range *np;
1284d1e669cSPeter Grehan
129ae551da6SNeel Natu pthread_rwlock_rdlock(&mmio_rwlock);
1300ab13648SPeter Grehan RB_FOREACH(np, mmio_rb_tree, rbt) {
1314d1e669cSPeter Grehan printf(" %lx:%lx, %s\n", np->mr_base, np->mr_end,
1324d1e669cSPeter Grehan np->mr_param.name);
1334d1e669cSPeter Grehan }
1345f4c83abSMarcelo Araujo perror = pthread_rwlock_unlock(&mmio_rwlock);
1355f4c83abSMarcelo Araujo assert(perror == 0);
1364d1e669cSPeter Grehan }
1374d1e669cSPeter Grehan #endif
1384d1e669cSPeter Grehan
1394d1e669cSPeter Grehan RB_GENERATE(mmio_rb_tree, mmio_rb_range, mr_link, mmio_rb_range_compare);
1404d1e669cSPeter Grehan
1417d9ef309SJohn Baldwin typedef int (mem_cb_t)(struct vcpu *vcpu, uint64_t gpa, struct mem_range *mr,
1427d9ef309SJohn Baldwin void *arg);
143cd377eb3SJohn Baldwin
144ba9b7bf7SNeel Natu static int
mem_read(struct vcpu * vcpu,uint64_t gpa,uint64_t * rval,int size,void * arg)1457d9ef309SJohn Baldwin mem_read(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size, void *arg)
146ba9b7bf7SNeel Natu {
147ba9b7bf7SNeel Natu int error;
148ba9b7bf7SNeel Natu struct mem_range *mr = arg;
149ba9b7bf7SNeel Natu
1507d9ef309SJohn Baldwin error = (*mr->handler)(vcpu, MEM_F_READ, gpa, size, rval, mr->arg1,
1517d9ef309SJohn Baldwin mr->arg2);
152ba9b7bf7SNeel Natu return (error);
153ba9b7bf7SNeel Natu }
154ba9b7bf7SNeel Natu
155ba9b7bf7SNeel Natu static int
mem_write(struct vcpu * vcpu,uint64_t gpa,uint64_t wval,int size,void * arg)1567d9ef309SJohn Baldwin mem_write(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size, void *arg)
157ba9b7bf7SNeel Natu {
158ba9b7bf7SNeel Natu int error;
159ba9b7bf7SNeel Natu struct mem_range *mr = arg;
160ba9b7bf7SNeel Natu
1617d9ef309SJohn Baldwin error = (*mr->handler)(vcpu, MEM_F_WRITE, gpa, size, &wval, mr->arg1,
1627d9ef309SJohn Baldwin mr->arg2);
163ba9b7bf7SNeel Natu return (error);
164ba9b7bf7SNeel Natu }
165ba9b7bf7SNeel Natu
166cd377eb3SJohn Baldwin static int
access_memory(struct vcpu * vcpu,uint64_t paddr,mem_cb_t * cb,void * arg)1677d9ef309SJohn Baldwin access_memory(struct vcpu *vcpu, uint64_t paddr, mem_cb_t *cb, void *arg)
1684d1e669cSPeter Grehan {
1694d1e669cSPeter Grehan struct mmio_rb_range *entry;
1707d9ef309SJohn Baldwin int err, perror, immutable, vcpuid;
1714d1e669cSPeter Grehan
1727d9ef309SJohn Baldwin vcpuid = vcpu_id(vcpu);
173ae551da6SNeel Natu pthread_rwlock_rdlock(&mmio_rwlock);
1744d1e669cSPeter Grehan /*
1754d1e669cSPeter Grehan * First check the per-vCPU cache
1764d1e669cSPeter Grehan */
1777d9ef309SJohn Baldwin if (mmio_hint[vcpuid] &&
1787d9ef309SJohn Baldwin paddr >= mmio_hint[vcpuid]->mr_base &&
1797d9ef309SJohn Baldwin paddr <= mmio_hint[vcpuid]->mr_end) {
1807d9ef309SJohn Baldwin entry = mmio_hint[vcpuid];
181ba9b7bf7SNeel Natu } else
182ba9b7bf7SNeel Natu entry = NULL;
183ba9b7bf7SNeel Natu
184ba9b7bf7SNeel Natu if (entry == NULL) {
185028d9311SNeel Natu if (mmio_rb_lookup(&mmio_rb_root, paddr, &entry) == 0) {
186ba9b7bf7SNeel Natu /* Update the per-vCPU cache */
1877d9ef309SJohn Baldwin mmio_hint[vcpuid] = entry;
1880ab13648SPeter Grehan } else if (mmio_rb_lookup(&mmio_rb_fallback, paddr, &entry)) {
1895f4c83abSMarcelo Araujo perror = pthread_rwlock_unlock(&mmio_rwlock);
1905f4c83abSMarcelo Araujo assert(perror == 0);
1910ab13648SPeter Grehan return (ESRCH);
1920ab13648SPeter Grehan }
1934d1e669cSPeter Grehan }
1944d1e669cSPeter Grehan
1950ab13648SPeter Grehan assert(entry != NULL);
19612a6eb99SNeel Natu
19712a6eb99SNeel Natu /*
19812a6eb99SNeel Natu * An 'immutable' memory range is guaranteed to be never removed
19912a6eb99SNeel Natu * so there is no need to hold 'mmio_rwlock' while calling the
20012a6eb99SNeel Natu * handler.
20112a6eb99SNeel Natu *
20212a6eb99SNeel Natu * XXX writes to the PCIR_COMMAND register can cause register_mem()
20312a6eb99SNeel Natu * to be called. If the guest is using PCI extended config space
20412a6eb99SNeel Natu * to modify the PCIR_COMMAND register then register_mem() can
20512a6eb99SNeel Natu * deadlock on 'mmio_rwlock'. However by registering the extended
20612a6eb99SNeel Natu * config space window as 'immutable' the deadlock can be avoided.
20712a6eb99SNeel Natu */
20812a6eb99SNeel Natu immutable = (entry->mr_param.flags & MEM_F_IMMUTABLE);
2095f4c83abSMarcelo Araujo if (immutable) {
2105f4c83abSMarcelo Araujo perror = pthread_rwlock_unlock(&mmio_rwlock);
2115f4c83abSMarcelo Araujo assert(perror == 0);
2125f4c83abSMarcelo Araujo }
21312a6eb99SNeel Natu
2147d9ef309SJohn Baldwin err = cb(vcpu, paddr, &entry->mr_param, arg);
21512a6eb99SNeel Natu
2165f4c83abSMarcelo Araujo if (!immutable) {
2175f4c83abSMarcelo Araujo perror = pthread_rwlock_unlock(&mmio_rwlock);
2185f4c83abSMarcelo Araujo assert(perror == 0);
2195f4c83abSMarcelo Araujo }
2205f4c83abSMarcelo Araujo
2214d1e669cSPeter Grehan return (err);
2224d1e669cSPeter Grehan }
2234d1e669cSPeter Grehan
224cd377eb3SJohn Baldwin struct emulate_mem_args {
225cd377eb3SJohn Baldwin struct vie *vie;
226cd377eb3SJohn Baldwin struct vm_guest_paging *paging;
227cd377eb3SJohn Baldwin };
228cd377eb3SJohn Baldwin
229cd377eb3SJohn Baldwin static int
emulate_mem_cb(struct vcpu * vcpu,uint64_t paddr,struct mem_range * mr,void * arg)2307d9ef309SJohn Baldwin emulate_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr,
231cd377eb3SJohn Baldwin void *arg)
232cd377eb3SJohn Baldwin {
233cd377eb3SJohn Baldwin struct emulate_mem_args *ema;
234cd377eb3SJohn Baldwin
235cd377eb3SJohn Baldwin ema = arg;
2367d9ef309SJohn Baldwin return (vmm_emulate_instruction(vcpu, paddr, ema->vie, ema->paging,
237cd377eb3SJohn Baldwin mem_read, mem_write, mr));
238cd377eb3SJohn Baldwin }
239cd377eb3SJohn Baldwin
240cd377eb3SJohn Baldwin int
emulate_mem(struct vcpu * vcpu,uint64_t paddr,struct vie * vie,struct vm_guest_paging * paging)2417d9ef309SJohn Baldwin emulate_mem(struct vcpu *vcpu, uint64_t paddr, struct vie *vie,
242cd377eb3SJohn Baldwin struct vm_guest_paging *paging)
243cd377eb3SJohn Baldwin {
244cd377eb3SJohn Baldwin struct emulate_mem_args ema;
245cd377eb3SJohn Baldwin
246cd377eb3SJohn Baldwin ema.vie = vie;
247cd377eb3SJohn Baldwin ema.paging = paging;
2487d9ef309SJohn Baldwin return (access_memory(vcpu, paddr, emulate_mem_cb, &ema));
249cd377eb3SJohn Baldwin }
250cd377eb3SJohn Baldwin
2511b52cd45SJohn Baldwin struct rw_mem_args {
2521b52cd45SJohn Baldwin uint64_t *val;
253cd377eb3SJohn Baldwin int size;
2541b52cd45SJohn Baldwin int operation;
255cd377eb3SJohn Baldwin };
256cd377eb3SJohn Baldwin
257cd377eb3SJohn Baldwin static int
rw_mem_cb(struct vcpu * vcpu,uint64_t paddr,struct mem_range * mr,void * arg)2587d9ef309SJohn Baldwin rw_mem_cb(struct vcpu *vcpu, uint64_t paddr, struct mem_range *mr, void *arg)
259cd377eb3SJohn Baldwin {
2601b52cd45SJohn Baldwin struct rw_mem_args *rma;
261cd377eb3SJohn Baldwin
262cd377eb3SJohn Baldwin rma = arg;
2637d9ef309SJohn Baldwin return (mr->handler(vcpu, rma->operation, paddr, rma->size,
2641b52cd45SJohn Baldwin rma->val, mr->arg1, mr->arg2));
265cd377eb3SJohn Baldwin }
266cd377eb3SJohn Baldwin
267cd377eb3SJohn Baldwin int
read_mem(struct vcpu * vcpu,uint64_t gpa,uint64_t * rval,int size)2687d9ef309SJohn Baldwin read_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t *rval, int size)
269cd377eb3SJohn Baldwin {
2701b52cd45SJohn Baldwin struct rw_mem_args rma;
271cd377eb3SJohn Baldwin
2721b52cd45SJohn Baldwin rma.val = rval;
273cd377eb3SJohn Baldwin rma.size = size;
2741b52cd45SJohn Baldwin rma.operation = MEM_F_READ;
2757d9ef309SJohn Baldwin return (access_memory(vcpu, gpa, rw_mem_cb, &rma));
2761b52cd45SJohn Baldwin }
2771b52cd45SJohn Baldwin
2781b52cd45SJohn Baldwin int
write_mem(struct vcpu * vcpu,uint64_t gpa,uint64_t wval,int size)2797d9ef309SJohn Baldwin write_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size)
2801b52cd45SJohn Baldwin {
2811b52cd45SJohn Baldwin struct rw_mem_args rma;
2821b52cd45SJohn Baldwin
2831b52cd45SJohn Baldwin rma.val = &wval;
2841b52cd45SJohn Baldwin rma.size = size;
2851b52cd45SJohn Baldwin rma.operation = MEM_F_WRITE;
2867d9ef309SJohn Baldwin return (access_memory(vcpu, gpa, rw_mem_cb, &rma));
287cd377eb3SJohn Baldwin }
288cd377eb3SJohn Baldwin
2890ab13648SPeter Grehan static int
register_mem_int(struct mmio_rb_tree * rbt,struct mem_range * memp)2900ab13648SPeter Grehan register_mem_int(struct mmio_rb_tree *rbt, struct mem_range *memp)
2914d1e669cSPeter Grehan {
292028d9311SNeel Natu struct mmio_rb_range *entry, *mrp;
2935f4c83abSMarcelo Araujo int err, perror;
2944d1e669cSPeter Grehan
2954d1e669cSPeter Grehan err = 0;
2964d1e669cSPeter Grehan
2974d1e669cSPeter Grehan mrp = malloc(sizeof(struct mmio_rb_range));
298f2b5dc3aSMarcelo Araujo if (mrp == NULL) {
299f2b5dc3aSMarcelo Araujo warn("%s: couldn't allocate memory for mrp\n",
300f2b5dc3aSMarcelo Araujo __func__);
301f2b5dc3aSMarcelo Araujo err = ENOMEM;
302f2b5dc3aSMarcelo Araujo } else {
3034d1e669cSPeter Grehan mrp->mr_param = *memp;
3044d1e669cSPeter Grehan mrp->mr_base = memp->base;
3054d1e669cSPeter Grehan mrp->mr_end = memp->base + memp->size - 1;
306ae551da6SNeel Natu pthread_rwlock_wrlock(&mmio_rwlock);
307028d9311SNeel Natu if (mmio_rb_lookup(rbt, memp->base, &entry) != 0)
3080ab13648SPeter Grehan err = mmio_rb_add(rbt, mrp);
3095f4c83abSMarcelo Araujo perror = pthread_rwlock_unlock(&mmio_rwlock);
3105f4c83abSMarcelo Araujo assert(perror == 0);
3114d1e669cSPeter Grehan if (err)
3124d1e669cSPeter Grehan free(mrp);
313f2b5dc3aSMarcelo Araujo }
3144d1e669cSPeter Grehan
3154d1e669cSPeter Grehan return (err);
3164d1e669cSPeter Grehan }
3174d1e669cSPeter Grehan
3180ab13648SPeter Grehan int
register_mem(struct mem_range * memp)3190ab13648SPeter Grehan register_mem(struct mem_range *memp)
3200ab13648SPeter Grehan {
3210ab13648SPeter Grehan
3220ab13648SPeter Grehan return (register_mem_int(&mmio_rb_root, memp));
3230ab13648SPeter Grehan }
3240ab13648SPeter Grehan
3250ab13648SPeter Grehan int
register_mem_fallback(struct mem_range * memp)3260ab13648SPeter Grehan register_mem_fallback(struct mem_range *memp)
3270ab13648SPeter Grehan {
3280ab13648SPeter Grehan
3290ab13648SPeter Grehan return (register_mem_int(&mmio_rb_fallback, memp));
3300ab13648SPeter Grehan }
3310ab13648SPeter Grehan
332028d9311SNeel Natu int
unregister_mem(struct mem_range * memp)333028d9311SNeel Natu unregister_mem(struct mem_range *memp)
334028d9311SNeel Natu {
335028d9311SNeel Natu struct mem_range *mr;
336028d9311SNeel Natu struct mmio_rb_range *entry = NULL;
3375f4c83abSMarcelo Araujo int err, perror, i;
338028d9311SNeel Natu
339ae551da6SNeel Natu pthread_rwlock_wrlock(&mmio_rwlock);
340028d9311SNeel Natu err = mmio_rb_lookup(&mmio_rb_root, memp->base, &entry);
341028d9311SNeel Natu if (err == 0) {
342028d9311SNeel Natu mr = &entry->mr_param;
343028d9311SNeel Natu assert(mr->name == memp->name);
344028d9311SNeel Natu assert(mr->base == memp->base && mr->size == memp->size);
34512a6eb99SNeel Natu assert((mr->flags & MEM_F_IMMUTABLE) == 0);
346028d9311SNeel Natu RB_REMOVE(mmio_rb_tree, &mmio_rb_root, entry);
347028d9311SNeel Natu
348028d9311SNeel Natu /* flush Per-vCPU cache */
349730510dcSJohn Baldwin for (i = 0; i < mmio_ncpu; i++) {
350028d9311SNeel Natu if (mmio_hint[i] == entry)
351028d9311SNeel Natu mmio_hint[i] = NULL;
352028d9311SNeel Natu }
353028d9311SNeel Natu }
3545f4c83abSMarcelo Araujo perror = pthread_rwlock_unlock(&mmio_rwlock);
3555f4c83abSMarcelo Araujo assert(perror == 0);
356028d9311SNeel Natu
357028d9311SNeel Natu if (entry)
358028d9311SNeel Natu free(entry);
359028d9311SNeel Natu
360028d9311SNeel Natu return (err);
361028d9311SNeel Natu }
362028d9311SNeel Natu
3634d1e669cSPeter Grehan void
init_mem(int ncpu)364730510dcSJohn Baldwin init_mem(int ncpu)
3654d1e669cSPeter Grehan {
3664d1e669cSPeter Grehan
367730510dcSJohn Baldwin mmio_ncpu = ncpu;
368730510dcSJohn Baldwin mmio_hint = calloc(ncpu, sizeof(*mmio_hint));
3690ab13648SPeter Grehan RB_INIT(&mmio_rb_root);
3700ab13648SPeter Grehan RB_INIT(&mmio_rb_fallback);
371ae551da6SNeel Natu pthread_rwlock_init(&mmio_rwlock, NULL);
3724d1e669cSPeter Grehan }
373