xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_regset.c (revision 538354481ef7dbcd76ebc7334512a9d60994b84e)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
66ff6d951SJohn Birrell  * (the "License").  You may not use this file except in compliance
76ff6d951SJohn Birrell  * with the License.
86ff6d951SJohn Birrell  *
96ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
106ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
116ff6d951SJohn Birrell  * See the License for the specific language governing permissions
126ff6d951SJohn Birrell  * and limitations under the License.
136ff6d951SJohn Birrell  *
146ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
156ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
166ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
176ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
186ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
196ff6d951SJohn Birrell  *
206ff6d951SJohn Birrell  * CDDL HEADER END
216ff6d951SJohn Birrell  */
22ba6cafe2SMark Johnston 
236ff6d951SJohn Birrell /*
246ff6d951SJohn Birrell  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
256ff6d951SJohn Birrell  * Use is subject to license terms.
266ff6d951SJohn Birrell  */
276ff6d951SJohn Birrell 
28ba6cafe2SMark Johnston /*
29ba6cafe2SMark Johnston  * Copyright (c) 2012 by Delphix. All rights reserved.
30*273df429SPedro F. Giffuni  * Copyright (c) 2016 Pedro Giffuni.  All rights reserved.
31ba6cafe2SMark Johnston  */
326ff6d951SJohn Birrell 
336ff6d951SJohn Birrell #include <sys/types.h>
346ff6d951SJohn Birrell #include <sys/bitmap.h>
356ff6d951SJohn Birrell #include <assert.h>
366ff6d951SJohn Birrell #include <strings.h>
376ff6d951SJohn Birrell #include <stdlib.h>
386ff6d951SJohn Birrell 
396ff6d951SJohn Birrell #include <dt_regset.h>
40ba6cafe2SMark Johnston #include <dt_impl.h>
416ff6d951SJohn Birrell 
426ff6d951SJohn Birrell dt_regset_t *
dt_regset_create(ulong_t nregs)43ba6cafe2SMark Johnston dt_regset_create(ulong_t nregs)
446ff6d951SJohn Birrell {
45ba6cafe2SMark Johnston 	ulong_t n = BT_BITOUL(nregs);
466ff6d951SJohn Birrell 	dt_regset_t *drp = malloc(sizeof (dt_regset_t));
476ff6d951SJohn Birrell 
486ff6d951SJohn Birrell 	if (drp == NULL)
496ff6d951SJohn Birrell 		return (NULL);
506ff6d951SJohn Birrell 
51*273df429SPedro F. Giffuni 	drp->dr_bitmap = calloc(n, sizeof (ulong_t));
526ff6d951SJohn Birrell 
536ff6d951SJohn Birrell 	if (drp->dr_bitmap == NULL) {
546ff6d951SJohn Birrell 		dt_regset_destroy(drp);
556ff6d951SJohn Birrell 		return (NULL);
566ff6d951SJohn Birrell 	}
576ff6d951SJohn Birrell 
58*273df429SPedro F. Giffuni 	drp->dr_size = nregs;
59*273df429SPedro F. Giffuni 
606ff6d951SJohn Birrell 	return (drp);
616ff6d951SJohn Birrell }
626ff6d951SJohn Birrell 
636ff6d951SJohn Birrell void
dt_regset_destroy(dt_regset_t * drp)646ff6d951SJohn Birrell dt_regset_destroy(dt_regset_t *drp)
656ff6d951SJohn Birrell {
666ff6d951SJohn Birrell 	free(drp->dr_bitmap);
676ff6d951SJohn Birrell 	free(drp);
686ff6d951SJohn Birrell }
696ff6d951SJohn Birrell 
706ff6d951SJohn Birrell void
dt_regset_reset(dt_regset_t * drp)716ff6d951SJohn Birrell dt_regset_reset(dt_regset_t *drp)
726ff6d951SJohn Birrell {
736ff6d951SJohn Birrell 	bzero(drp->dr_bitmap, sizeof (ulong_t) * BT_BITOUL(drp->dr_size));
746ff6d951SJohn Birrell }
756ff6d951SJohn Birrell 
76ba6cafe2SMark Johnston void
dt_regset_assert_free(dt_regset_t * drp)77ba6cafe2SMark Johnston dt_regset_assert_free(dt_regset_t *drp)
78ba6cafe2SMark Johnston {
79ba6cafe2SMark Johnston 	int reg;
80ba6cafe2SMark Johnston 	boolean_t fail = B_FALSE;
81ba6cafe2SMark Johnston 	for (reg = 0; reg < drp->dr_size; reg++) {
82ba6cafe2SMark Johnston 		if (BT_TEST(drp->dr_bitmap, reg) != 0)  {
83ba6cafe2SMark Johnston 			dt_dprintf("%%r%d was left allocated\n", reg);
84ba6cafe2SMark Johnston 			fail = B_TRUE;
85ba6cafe2SMark Johnston 		}
86ba6cafe2SMark Johnston 	}
87ba6cafe2SMark Johnston 
88ba6cafe2SMark Johnston 	/*
89ba6cafe2SMark Johnston 	 * We set this during dtest runs to check for register leaks.
90ba6cafe2SMark Johnston 	 */
91ba6cafe2SMark Johnston 	if (fail && getenv("DTRACE_DEBUG_REGSET") != NULL)
92ba6cafe2SMark Johnston 		abort();
93ba6cafe2SMark Johnston }
94ba6cafe2SMark Johnston 
956ff6d951SJohn Birrell int
dt_regset_alloc(dt_regset_t * drp)966ff6d951SJohn Birrell dt_regset_alloc(dt_regset_t *drp)
976ff6d951SJohn Birrell {
986ff6d951SJohn Birrell 	ulong_t nbits = drp->dr_size - 1;
996ff6d951SJohn Birrell 	ulong_t maxw = nbits >> BT_ULSHIFT;
1006ff6d951SJohn Birrell 	ulong_t wx;
1016ff6d951SJohn Birrell 
1026ff6d951SJohn Birrell 	for (wx = 0; wx <= maxw; wx++) {
1036ff6d951SJohn Birrell 		if (drp->dr_bitmap[wx] != ~0UL)
1046ff6d951SJohn Birrell 			break;
1056ff6d951SJohn Birrell 	}
1066ff6d951SJohn Birrell 
1076ff6d951SJohn Birrell 	if (wx <= maxw) {
1086ff6d951SJohn Birrell 		ulong_t maxb = (wx == maxw) ? nbits & BT_ULMASK : BT_NBIPUL - 1;
1096ff6d951SJohn Birrell 		ulong_t word = drp->dr_bitmap[wx];
1106ff6d951SJohn Birrell 		ulong_t bit, bx;
1116ff6d951SJohn Birrell 		int reg;
1126ff6d951SJohn Birrell 
1136ff6d951SJohn Birrell 		for (bit = 1, bx = 0; bx <= maxb; bx++, bit <<= 1) {
1146ff6d951SJohn Birrell 			if ((word & bit) == 0) {
1156ff6d951SJohn Birrell 				reg = (int)((wx << BT_ULSHIFT) | bx);
1166ff6d951SJohn Birrell 				BT_SET(drp->dr_bitmap, reg);
1176ff6d951SJohn Birrell 				return (reg);
1186ff6d951SJohn Birrell 			}
1196ff6d951SJohn Birrell 		}
1206ff6d951SJohn Birrell 	}
1216ff6d951SJohn Birrell 
122ba6cafe2SMark Johnston 	xyerror(D_NOREG, "Insufficient registers to generate code");
123ba6cafe2SMark Johnston 	/*NOTREACHED*/
124ba6cafe2SMark Johnston 	return (-1);
1256ff6d951SJohn Birrell }
1266ff6d951SJohn Birrell 
1276ff6d951SJohn Birrell void
dt_regset_free(dt_regset_t * drp,int reg)1286ff6d951SJohn Birrell dt_regset_free(dt_regset_t *drp, int reg)
1296ff6d951SJohn Birrell {
130ba6cafe2SMark Johnston 	assert(reg >= 0 && reg < drp->dr_size);
1316ff6d951SJohn Birrell 	assert(BT_TEST(drp->dr_bitmap, reg) != 0);
1326ff6d951SJohn Birrell 	BT_CLEAR(drp->dr_bitmap, reg);
1336ff6d951SJohn Birrell }
134