xref: /onnv-gate/usr/src/uts/common/krtld/kobj_subr.c (revision 5648:161f8007cab9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52267Sdp  * Common Development and Distribution License (the "License").
62267Sdp  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5648Ssetje  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
302267Sdp #include <sys/systm.h>
310Sstevel@tonic-gate 
32*5648Ssetje #include <sys/bootconf.h>
33*5648Ssetje #include <sys/kobj_impl.h>
34*5648Ssetje #include <sys/cmn_err.h>
35*5648Ssetje 
360Sstevel@tonic-gate /*
37*5648Ssetje  * Standalone utility functions for use within krtld.
38*5648Ssetje  * Many platforms implement optimized platmod versions of
39*5648Ssetje  * utilities such as bcopy and any such are not yet available
40*5648Ssetje  * until the kernel is more completely stitched together.
41*5648Ssetje  * These standalones are referenced through vectors
42*5648Ssetje  * kobj_bzero, etc.  Throughout krtld, the usual utility
43*5648Ssetje  * is redefined to reference through the corresponding
44*5648Ssetje  * vector so that krtld may simply refer to bzero etc.
45*5648Ssetje  * as usual.  See kobj_impl.h.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
48*5648Ssetje /*ARGSUSED*/
49*5648Ssetje static void
kprintf(void * op,const char * fmt,...)50*5648Ssetje kprintf(void *op, const char *fmt, ...)
510Sstevel@tonic-gate {
52*5648Ssetje 	va_list adx;
530Sstevel@tonic-gate 
54*5648Ssetje 	va_start(adx, fmt);
55*5648Ssetje 	vprintf(fmt, adx);
56*5648Ssetje 	va_end(adx);
570Sstevel@tonic-gate }
580Sstevel@tonic-gate 
59*5648Ssetje static void
stand_bzero(void * p_arg,size_t count)60*5648Ssetje stand_bzero(void *p_arg, size_t count)
610Sstevel@tonic-gate {
62*5648Ssetje 	char zero = 0;
63*5648Ssetje 	caddr_t p = p_arg;
640Sstevel@tonic-gate 
65*5648Ssetje 	while (count != 0)
66*5648Ssetje 		*p++ = zero, count--;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
69*5648Ssetje static void
stand_bcopy(const void * src_arg,void * dest_arg,size_t count)70*5648Ssetje stand_bcopy(const void *src_arg, void *dest_arg, size_t count)
710Sstevel@tonic-gate {
72*5648Ssetje 	caddr_t src = (caddr_t)src_arg;
73*5648Ssetje 	caddr_t dest = dest_arg;
740Sstevel@tonic-gate 
75*5648Ssetje 	if (src < dest && (src + count) > dest) {
76*5648Ssetje 		/* overlap copy */
77*5648Ssetje 		while (--count != -1)
78*5648Ssetje 			*(dest + count) = *(src + count);
79*5648Ssetje 	} else {
80*5648Ssetje 		while (--count != -1)
81*5648Ssetje 			*dest++ = *src++;
82*5648Ssetje 	}
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
85*5648Ssetje static size_t
stand_strlcat(char * dst,const char * src,size_t dstsize)86*5648Ssetje stand_strlcat(char *dst, const char *src, size_t dstsize)
872267Sdp {
882267Sdp 	char *df = dst;
892267Sdp 	size_t left = dstsize;
902267Sdp 	size_t l1;
912267Sdp 	size_t l2 = strlen(src);
922267Sdp 	size_t copied;
932267Sdp 
942267Sdp 	while (left-- != 0 && *df != '\0')
952267Sdp 		df++;
962267Sdp 	l1 = df - dst;
972267Sdp 	if (dstsize == l1)
982267Sdp 		return (l1 + l2);
992267Sdp 
1002267Sdp 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
1012267Sdp 	bcopy(src, dst + l1, copied);
1022267Sdp 	dst[l1+copied] = '\0';
1032267Sdp 	return (l1 + l2);
1042267Sdp }
1052267Sdp 
106*5648Ssetje /*
107*5648Ssetje  * Set up the krtld standalone utilty vectors
108*5648Ssetje  */
109*5648Ssetje void
kobj_setup_standalone_vectors()110*5648Ssetje kobj_setup_standalone_vectors()
1110Sstevel@tonic-gate {
112*5648Ssetje 	_kobj_printf = (void (*)(void *, const char *, ...))bop_printf;
113*5648Ssetje 	kobj_bcopy = stand_bcopy;
114*5648Ssetje 	kobj_bzero = stand_bzero;
115*5648Ssetje 	kobj_strlcat = stand_strlcat;
116*5648Ssetje }
1170Sstevel@tonic-gate 
118*5648Ssetje /*
119*5648Ssetje  * Restore the kprintf/bcopy/bzero kobj vectors.
120*5648Ssetje  * We need to undefine the override macros to
121*5648Ssetje  * accomplish this.
122*5648Ssetje  *
123*5648Ssetje  * Do NOT add new code after the point or at least
124*5648Ssetje  * certainly not code using bcopy or bzero which would
125*5648Ssetje  * need to be vectored to the krtld equivalents.
126*5648Ssetje  */
127*5648Ssetje #undef	bcopy
128*5648Ssetje #undef	bzero
129*5648Ssetje #undef	strlcat
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate void
kobj_restore_vectors()132*5648Ssetje kobj_restore_vectors()
1330Sstevel@tonic-gate {
134*5648Ssetje 	_kobj_printf = kprintf;
135*5648Ssetje 	kobj_bcopy = bcopy;
136*5648Ssetje 	kobj_bzero = bzero;
137*5648Ssetje 	kobj_strlcat = strlcat;
1380Sstevel@tonic-gate }
139