xref: /onnv-gate/usr/src/cmd/rcap/rcapd/rcapd_mapping.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <assert.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include "rcapd_mapping.h"
33*0Sstevel@tonic-gate #include "utils.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate  * lmapping_t is a list of non-overlapping mappings, ordered by address.  These
37*0Sstevel@tonic-gate  * functions add, remove, and verify the existence of mappings in such a list.
38*0Sstevel@tonic-gate  * rcapd_scanner.c is a consumer.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate typedef struct lmapping_find_cb_arg {
42*0Sstevel@tonic-gate 	uintptr_t	lmfa_addr;
43*0Sstevel@tonic-gate 	size_t		lmfa_size;
44*0Sstevel@tonic-gate 	lmapping_t	*lmfa_prior;
45*0Sstevel@tonic-gate 	lmapping_t	*lmfa_ret;
46*0Sstevel@tonic-gate } lmapping_find_cb_arg_t;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #ifdef DEBUG
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * Verify a sublist is properly ordered.
51*0Sstevel@tonic-gate  */
52*0Sstevel@tonic-gate static void
lmapping_verify(lmapping_t * lm)53*0Sstevel@tonic-gate lmapping_verify(lmapping_t *lm)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	while (lm != NULL) {
56*0Sstevel@tonic-gate 		if (lm->lm_next != NULL)
57*0Sstevel@tonic-gate 			ASSERT(lm->lm_next->lm_addr > lm->lm_addr);
58*0Sstevel@tonic-gate 		lm = lm->lm_next;
59*0Sstevel@tonic-gate 	}
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate #else /* !DEBUG */
62*0Sstevel@tonic-gate #define	lmapping_verify(x) ((void)0)
63*0Sstevel@tonic-gate #endif /* DEBUG */
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * Determine the position of a mapping with the given address and size.  Upon
67*0Sstevel@tonic-gate  * return, lmfa_ret will be set to the actual mapping, if it exists, and
68*0Sstevel@tonic-gate  * lmfa_prior will be set to the mapping which does or would precede one with
69*0Sstevel@tonic-gate  * the given characteristics.
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate static int
lmapping_find_cb(lmapping_t * lm,void * arg)72*0Sstevel@tonic-gate lmapping_find_cb(lmapping_t *lm, void *arg)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	lmapping_find_cb_arg_t *lmfa = arg;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (lm->lm_addr >= lmfa->lmfa_addr) {
77*0Sstevel@tonic-gate 		if (lmfa->lmfa_addr == lm->lm_addr && lmfa->lmfa_size ==
78*0Sstevel@tonic-gate 		    lm->lm_size)
79*0Sstevel@tonic-gate 			lmfa->lmfa_ret = lm;
80*0Sstevel@tonic-gate 		return (1);
81*0Sstevel@tonic-gate 	} else
82*0Sstevel@tonic-gate 		lmfa->lmfa_prior = lm;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	return (0);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate static void
lmapping_walk(lmapping_t * lm,int (* lmapping_walk_cb)(lmapping_t *,void *),void * arg)88*0Sstevel@tonic-gate lmapping_walk(lmapping_t *lm, int(*lmapping_walk_cb)(lmapping_t *, void *),
89*0Sstevel@tonic-gate     void *arg)
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	lmapping_t *next;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	while (lm != NULL) {
94*0Sstevel@tonic-gate 		next = lm->lm_next;
95*0Sstevel@tonic-gate 		lmapping_verify(lm);
96*0Sstevel@tonic-gate 		if (lmapping_walk_cb(lm, arg) != 0) {
97*0Sstevel@tonic-gate 			lmapping_verify(lm);
98*0Sstevel@tonic-gate 			return;
99*0Sstevel@tonic-gate 		}
100*0Sstevel@tonic-gate 		lm = next;
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate int
lmapping_remove(lmapping_t ** lm,uintptr_t addr,size_t size)105*0Sstevel@tonic-gate lmapping_remove(lmapping_t **lm, uintptr_t addr, size_t size)
106*0Sstevel@tonic-gate {
107*0Sstevel@tonic-gate 	lmapping_find_cb_arg_t lmfa;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	lmfa.lmfa_addr = addr;
110*0Sstevel@tonic-gate 	lmfa.lmfa_size = size;
111*0Sstevel@tonic-gate 	lmfa.lmfa_prior = lmfa.lmfa_ret = NULL;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	lmapping_verify(*lm);
114*0Sstevel@tonic-gate 	lmapping_walk(*lm, lmapping_find_cb, &lmfa);
115*0Sstevel@tonic-gate 	if (lmfa.lmfa_ret == NULL)
116*0Sstevel@tonic-gate 		return (-1);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (lmfa.lmfa_prior != NULL)
119*0Sstevel@tonic-gate 		lmfa.lmfa_prior->lm_next = lmfa.lmfa_ret->lm_next;
120*0Sstevel@tonic-gate 	else if (*lm == lmfa.lmfa_ret)
121*0Sstevel@tonic-gate 		*lm = lmfa.lmfa_ret->lm_next;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	free(lmfa.lmfa_ret);
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	lmapping_verify(*lm);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	return (0);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate int
lmapping_insert(lmapping_t ** lm,uintptr_t addr,size_t size)131*0Sstevel@tonic-gate lmapping_insert(lmapping_t **lm, uintptr_t addr, size_t size)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	lmapping_find_cb_arg_t lmfa;
134*0Sstevel@tonic-gate 	lmapping_t *cur;
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	cur = malloc(sizeof (*cur));
137*0Sstevel@tonic-gate 	if (cur == NULL)
138*0Sstevel@tonic-gate 		return (-1);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	cur->lm_addr = addr;
141*0Sstevel@tonic-gate 	cur->lm_size = size;
142*0Sstevel@tonic-gate 	cur->lm_next = NULL;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	lmfa.lmfa_addr = addr;
145*0Sstevel@tonic-gate 	lmfa.lmfa_size = size;
146*0Sstevel@tonic-gate 	lmfa.lmfa_prior = lmfa.lmfa_ret = NULL;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	lmapping_verify(*lm);
149*0Sstevel@tonic-gate 	lmapping_walk(*lm, lmapping_find_cb, &lmfa);
150*0Sstevel@tonic-gate 	ASSERT(lmfa.lmfa_ret == NULL);
151*0Sstevel@tonic-gate 	if (lmfa.lmfa_prior != NULL) {
152*0Sstevel@tonic-gate 		cur->lm_next = lmfa.lmfa_prior->lm_next;
153*0Sstevel@tonic-gate 		lmfa.lmfa_prior->lm_next = cur;
154*0Sstevel@tonic-gate 	} else {
155*0Sstevel@tonic-gate 		cur->lm_next = *lm;
156*0Sstevel@tonic-gate 		*lm = cur;
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	lmapping_verify(*lm);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	return (0);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate int
lmapping_contains(lmapping_t * lm,uintptr_t addr,size_t size)165*0Sstevel@tonic-gate lmapping_contains(lmapping_t *lm, uintptr_t addr, size_t size)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	lmapping_find_cb_arg_t lmfa;
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	lmfa.lmfa_addr = addr;
170*0Sstevel@tonic-gate 	lmfa.lmfa_size = size;
171*0Sstevel@tonic-gate 	lmfa.lmfa_ret = NULL;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	lmapping_walk(lm, lmapping_find_cb, &lmfa);
174*0Sstevel@tonic-gate 	return (lmfa.lmfa_ret != NULL);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate /*ARGSUSED*/
178*0Sstevel@tonic-gate static int
lmapping_free_cb(lmapping_t * lm,void * arg)179*0Sstevel@tonic-gate lmapping_free_cb(lmapping_t *lm, void *arg)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate 	free(lm);
182*0Sstevel@tonic-gate 	return (0);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate void
lmapping_free(lmapping_t ** lm)186*0Sstevel@tonic-gate lmapping_free(lmapping_t **lm)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	lmapping_walk(*lm, lmapping_free_cb, NULL);
189*0Sstevel@tonic-gate 	*lm = NULL;
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate #ifdef DEBUG
193*0Sstevel@tonic-gate int
lmapping_dump_diff(lmapping_t * lm1,lmapping_t * lm2)194*0Sstevel@tonic-gate lmapping_dump_diff(lmapping_t *lm1, lmapping_t *lm2)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate 	lmapping_t **lmv;
197*0Sstevel@tonic-gate 	int res = 0;
198*0Sstevel@tonic-gate 	int ch = 0;
199*0Sstevel@tonic-gate 	int label_printed = 0;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate #define	OUTPUT_LABEL() \
202*0Sstevel@tonic-gate 	if (label_printed == 0) { \
203*0Sstevel@tonic-gate 		debug("changes in mappings:\n"); \
204*0Sstevel@tonic-gate 		label_printed++; \
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	while (lm1 != NULL && lm2 != NULL) {
208*0Sstevel@tonic-gate 		if ((lm1->lm_addr != lm2->lm_addr) || (lm1->lm_size !=
209*0Sstevel@tonic-gate 		    lm2->lm_size)) {
210*0Sstevel@tonic-gate 			res = -1;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 			if (lm1->lm_addr == lm2->lm_addr && lm1->lm_size <
213*0Sstevel@tonic-gate 			    lm2->lm_size || lm1->lm_addr < lm2->lm_addr) {
214*0Sstevel@tonic-gate 				lmv = &lm1;
215*0Sstevel@tonic-gate 				ch = '-';
216*0Sstevel@tonic-gate 			} else {
217*0Sstevel@tonic-gate 				lmv = &lm2;
218*0Sstevel@tonic-gate 				ch = '+';
219*0Sstevel@tonic-gate 			}
220*0Sstevel@tonic-gate 			OUTPUT_LABEL();
221*0Sstevel@tonic-gate 			debug("%c%p+0x%llx\n", ch, (void *)(*lmv)->lm_addr,
222*0Sstevel@tonic-gate 			    (long long)(*lmv)->lm_size);
223*0Sstevel@tonic-gate 			*lmv = (*lmv)->lm_next;
224*0Sstevel@tonic-gate 		} else {
225*0Sstevel@tonic-gate 			lm1 = lm1->lm_next;
226*0Sstevel@tonic-gate 			lm2 = lm2->lm_next;
227*0Sstevel@tonic-gate 		}
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 	while (lm1 != NULL) {
230*0Sstevel@tonic-gate 		OUTPUT_LABEL();
231*0Sstevel@tonic-gate 		debug("%c%p+0x%llx\n", '-', (void *)lm1->lm_addr,
232*0Sstevel@tonic-gate 		    (unsigned long long)lm1->lm_size);
233*0Sstevel@tonic-gate 		lm1 = lm1->lm_next;
234*0Sstevel@tonic-gate 		res = 1;
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 	while (lm2 != NULL) {
237*0Sstevel@tonic-gate 		OUTPUT_LABEL();
238*0Sstevel@tonic-gate 		debug("%c%p+0x%llx\n", '+', (void *)lm2->lm_addr,
239*0Sstevel@tonic-gate 		    (long long)lm2->lm_size);
240*0Sstevel@tonic-gate 		lm2 = lm2->lm_next;
241*0Sstevel@tonic-gate 		res = 1;
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	return (res);
245*0Sstevel@tonic-gate #undef OUTPUT_LABEL
246*0Sstevel@tonic-gate }
247*0Sstevel@tonic-gate #endif /* DEBUG */
248