xref: /onnv-gate/usr/src/uts/common/zmod/zutil.c (revision 420:45e68e7aede3)
1*420Sstevel /*
2*420Sstevel  * CDDL HEADER START
3*420Sstevel  *
4*420Sstevel  * The contents of this file are subject to the terms of the
5*420Sstevel  * Common Development and Distribution License, Version 1.0 only
6*420Sstevel  * (the "License").  You may not use this file except in compliance
7*420Sstevel  * with the License.
8*420Sstevel  *
9*420Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*420Sstevel  * or http://www.opensolaris.org/os/licensing.
11*420Sstevel  * See the License for the specific language governing permissions
12*420Sstevel  * and limitations under the License.
13*420Sstevel  *
14*420Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
15*420Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*420Sstevel  * If applicable, add the following below this CDDL HEADER, with the
17*420Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
18*420Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
19*420Sstevel  *
20*420Sstevel  * CDDL HEADER END
21*420Sstevel  */
22*420Sstevel 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/kobj.h>
330Sstevel@tonic-gate #include <sys/kobj_impl.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * This module is used both during the normal operation of the kernel (i.e.
370Sstevel@tonic-gate  * after kmem has been initialized) and during boot (before unix`_start has
380Sstevel@tonic-gate  * been called).  kobj_alloc is able to tell the difference between the two
390Sstevel@tonic-gate  * cases, and as such must be used instead of kmem_alloc.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate 
420Sstevel@tonic-gate void
430Sstevel@tonic-gate zmemcpy(uchar_t *dest, const uchar_t *source, uint_t len)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	bcopy(source, dest, len);
460Sstevel@tonic-gate }
470Sstevel@tonic-gate 
480Sstevel@tonic-gate struct zchdr {
490Sstevel@tonic-gate 	uint_t zch_magic;
500Sstevel@tonic-gate 	uint_t zch_size;
510Sstevel@tonic-gate };
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #define	ZCH_MAGIC	0x3cc13cc1
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*ARGSUSED*/
560Sstevel@tonic-gate void *
570Sstevel@tonic-gate zcalloc(void *opaque, uint_t items, uint_t size)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	size_t nbytes = sizeof (struct zchdr) + items * size;
600Sstevel@tonic-gate 	struct zchdr *z = kobj_zalloc(nbytes, KM_NOWAIT|KM_TMP);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (z == NULL)
630Sstevel@tonic-gate 		return (NULL);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	z->zch_magic = ZCH_MAGIC;
660Sstevel@tonic-gate 	z->zch_size = nbytes;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	return (z + 1);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*ARGSUSED*/
720Sstevel@tonic-gate void
730Sstevel@tonic-gate zcfree(void *opaque, void *ptr)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate 	struct zchdr *z = ((struct zchdr *)ptr) - 1;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	if (z->zch_magic != ZCH_MAGIC)
780Sstevel@tonic-gate 		panic("zcfree region corrupt: hdr=%p ptr=%p", (void *)z, ptr);
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	kobj_free(z, z->zch_size);
810Sstevel@tonic-gate }
82