xref: /titanic_50/usr/src/uts/common/krtld/kobj_subr.c (revision cc31cba96d9862a1075a8ec47d6149e04c7ad62d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 
32 #include <sys/bootconf.h>
33 #include <sys/kobj_impl.h>
34 #include <sys/cmn_err.h>
35 
36 /*
37  * Standalone utility functions for use within krtld.
38  * Many platforms implement optimized platmod versions of
39  * utilities such as bcopy and any such are not yet available
40  * until the kernel is more completely stitched together.
41  * These standalones are referenced through vectors
42  * kobj_bzero, etc.  Throughout krtld, the usual utility
43  * is redefined to reference through the corresponding
44  * vector so that krtld may simply refer to bzero etc.
45  * as usual.  See kobj_impl.h.
46  */
47 
48 /*ARGSUSED*/
49 static void
50 kprintf(void *op, const char *fmt, ...)
51 {
52 	va_list adx;
53 
54 	va_start(adx, fmt);
55 	vprintf(fmt, adx);
56 	va_end(adx);
57 }
58 
59 static void
60 stand_bzero(void *p_arg, size_t count)
61 {
62 	char zero = 0;
63 	caddr_t p = p_arg;
64 
65 	while (count != 0)
66 		*p++ = zero, count--;
67 }
68 
69 static void
70 stand_bcopy(const void *src_arg, void *dest_arg, size_t count)
71 {
72 	caddr_t src = (caddr_t)src_arg;
73 	caddr_t dest = dest_arg;
74 
75 	if (src < dest && (src + count) > dest) {
76 		/* overlap copy */
77 		while (--count != -1)
78 			*(dest + count) = *(src + count);
79 	} else {
80 		while (--count != -1)
81 			*dest++ = *src++;
82 	}
83 }
84 
85 static size_t
86 stand_strlcat(char *dst, const char *src, size_t dstsize)
87 {
88 	char *df = dst;
89 	size_t left = dstsize;
90 	size_t l1;
91 	size_t l2 = strlen(src);
92 	size_t copied;
93 
94 	while (left-- != 0 && *df != '\0')
95 		df++;
96 	l1 = df - dst;
97 	if (dstsize == l1)
98 		return (l1 + l2);
99 
100 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
101 	bcopy(src, dst + l1, copied);
102 	dst[l1+copied] = '\0';
103 	return (l1 + l2);
104 }
105 
106 /*
107  * Set up the krtld standalone utilty vectors
108  */
109 void
110 kobj_setup_standalone_vectors()
111 {
112 	_kobj_printf = (void (*)(void *, const char *, ...))bop_printf;
113 	kobj_bcopy = stand_bcopy;
114 	kobj_bzero = stand_bzero;
115 	kobj_strlcat = stand_strlcat;
116 }
117 
118 /*
119  * Restore the kprintf/bcopy/bzero kobj vectors.
120  * We need to undefine the override macros to
121  * accomplish this.
122  *
123  * Do NOT add new code after the point or at least
124  * certainly not code using bcopy or bzero which would
125  * need to be vectored to the krtld equivalents.
126  */
127 #undef	bcopy
128 #undef	bzero
129 #undef	strlcat
130 
131 void
132 kobj_restore_vectors()
133 {
134 	_kobj_printf = kprintf;
135 	kobj_bcopy = bcopy;
136 	kobj_bzero = bzero;
137 	kobj_strlcat = strlcat;
138 }
139