17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53f2f09c1Sdp * Common Development and Distribution License (the "License").
63f2f09c1Sdp * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*986fd29aSsetje * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
303f2f09c1Sdp #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate
32*986fd29aSsetje #include <sys/bootconf.h>
33*986fd29aSsetje #include <sys/kobj_impl.h>
34*986fd29aSsetje #include <sys/cmn_err.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
37*986fd29aSsetje * Standalone utility functions for use within krtld.
38*986fd29aSsetje * Many platforms implement optimized platmod versions of
39*986fd29aSsetje * utilities such as bcopy and any such are not yet available
40*986fd29aSsetje * until the kernel is more completely stitched together.
41*986fd29aSsetje * These standalones are referenced through vectors
42*986fd29aSsetje * kobj_bzero, etc. Throughout krtld, the usual utility
43*986fd29aSsetje * is redefined to reference through the corresponding
44*986fd29aSsetje * vector so that krtld may simply refer to bzero etc.
45*986fd29aSsetje * as usual. See kobj_impl.h.
467c478bd9Sstevel@tonic-gate */
47*986fd29aSsetje
48*986fd29aSsetje /*ARGSUSED*/
49*986fd29aSsetje static void
kprintf(void * op,const char * fmt,...)50*986fd29aSsetje kprintf(void *op, const char *fmt, ...)
517c478bd9Sstevel@tonic-gate {
52*986fd29aSsetje va_list adx;
53*986fd29aSsetje
54*986fd29aSsetje va_start(adx, fmt);
55*986fd29aSsetje vprintf(fmt, adx);
56*986fd29aSsetje va_end(adx);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
59*986fd29aSsetje static void
stand_bzero(void * p_arg,size_t count)60*986fd29aSsetje stand_bzero(void *p_arg, size_t count)
617c478bd9Sstevel@tonic-gate {
62*986fd29aSsetje char zero = 0;
63*986fd29aSsetje caddr_t p = p_arg;
647c478bd9Sstevel@tonic-gate
65*986fd29aSsetje while (count != 0)
66*986fd29aSsetje *p++ = zero, count--;
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate
69*986fd29aSsetje static void
stand_bcopy(const void * src_arg,void * dest_arg,size_t count)70*986fd29aSsetje stand_bcopy(const void *src_arg, void *dest_arg, size_t count)
717c478bd9Sstevel@tonic-gate {
72*986fd29aSsetje caddr_t src = (caddr_t)src_arg;
73*986fd29aSsetje caddr_t dest = dest_arg;
747c478bd9Sstevel@tonic-gate
75*986fd29aSsetje if (src < dest && (src + count) > dest) {
76*986fd29aSsetje /* overlap copy */
77*986fd29aSsetje while (--count != -1)
78*986fd29aSsetje *(dest + count) = *(src + count);
79*986fd29aSsetje } else {
80*986fd29aSsetje while (--count != -1)
81*986fd29aSsetje *dest++ = *src++;
82*986fd29aSsetje }
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
85*986fd29aSsetje static size_t
stand_strlcat(char * dst,const char * src,size_t dstsize)86*986fd29aSsetje stand_strlcat(char *dst, const char *src, size_t dstsize)
873f2f09c1Sdp {
883f2f09c1Sdp char *df = dst;
893f2f09c1Sdp size_t left = dstsize;
903f2f09c1Sdp size_t l1;
913f2f09c1Sdp size_t l2 = strlen(src);
923f2f09c1Sdp size_t copied;
933f2f09c1Sdp
943f2f09c1Sdp while (left-- != 0 && *df != '\0')
953f2f09c1Sdp df++;
963f2f09c1Sdp l1 = df - dst;
973f2f09c1Sdp if (dstsize == l1)
983f2f09c1Sdp return (l1 + l2);
993f2f09c1Sdp
1003f2f09c1Sdp copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
1013f2f09c1Sdp bcopy(src, dst + l1, copied);
1023f2f09c1Sdp dst[l1+copied] = '\0';
1033f2f09c1Sdp return (l1 + l2);
1043f2f09c1Sdp }
1053f2f09c1Sdp
106*986fd29aSsetje /*
107*986fd29aSsetje * Set up the krtld standalone utilty vectors
108*986fd29aSsetje */
109*986fd29aSsetje void
kobj_setup_standalone_vectors()110*986fd29aSsetje kobj_setup_standalone_vectors()
1117c478bd9Sstevel@tonic-gate {
112*986fd29aSsetje _kobj_printf = (void (*)(void *, const char *, ...))bop_printf;
113*986fd29aSsetje kobj_bcopy = stand_bcopy;
114*986fd29aSsetje kobj_bzero = stand_bzero;
115*986fd29aSsetje kobj_strlcat = stand_strlcat;
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate
118*986fd29aSsetje /*
119*986fd29aSsetje * Restore the kprintf/bcopy/bzero kobj vectors.
120*986fd29aSsetje * We need to undefine the override macros to
121*986fd29aSsetje * accomplish this.
122*986fd29aSsetje *
123*986fd29aSsetje * Do NOT add new code after the point or at least
124*986fd29aSsetje * certainly not code using bcopy or bzero which would
125*986fd29aSsetje * need to be vectored to the krtld equivalents.
126*986fd29aSsetje */
127*986fd29aSsetje #undef bcopy
128*986fd29aSsetje #undef bzero
129*986fd29aSsetje #undef strlcat
130*986fd29aSsetje
1317c478bd9Sstevel@tonic-gate void
kobj_restore_vectors()132*986fd29aSsetje kobj_restore_vectors()
1337c478bd9Sstevel@tonic-gate {
134*986fd29aSsetje _kobj_printf = kprintf;
135*986fd29aSsetje kobj_bcopy = bcopy;
136*986fd29aSsetje kobj_bzero = bzero;
137*986fd29aSsetje kobj_strlcat = strlcat;
1387c478bd9Sstevel@tonic-gate }
139