xref: /titanic_44/usr/src/uts/common/zmod/zutil.c (revision a0b85df49408b3ff0ca3c0780974f13a7b485ae3)
17c478bd9Sstevel@tonic-gate /*
2*a0b85df4Sstevel  * CDDL HEADER START
3*a0b85df4Sstevel  *
4*a0b85df4Sstevel  * The contents of this file are subject to the terms of the
5*a0b85df4Sstevel  * Common Development and Distribution License, Version 1.0 only
6*a0b85df4Sstevel  * (the "License").  You may not use this file except in compliance
7*a0b85df4Sstevel  * with the License.
8*a0b85df4Sstevel  *
9*a0b85df4Sstevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*a0b85df4Sstevel  * or http://www.opensolaris.org/os/licensing.
11*a0b85df4Sstevel  * See the License for the specific language governing permissions
12*a0b85df4Sstevel  * and limitations under the License.
13*a0b85df4Sstevel  *
14*a0b85df4Sstevel  * When distributing Covered Code, include this CDDL HEADER in each
15*a0b85df4Sstevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*a0b85df4Sstevel  * If applicable, add the following below this CDDL HEADER, with the
17*a0b85df4Sstevel  * fields enclosed by brackets "[]" replaced with your own identifying
18*a0b85df4Sstevel  * information: Portions Copyright [yyyy] [name of copyright owner]
19*a0b85df4Sstevel  *
20*a0b85df4Sstevel  * CDDL HEADER END
21*a0b85df4Sstevel  */
22*a0b85df4Sstevel 
23*a0b85df4Sstevel /*
247c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
327c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
337c478bd9Sstevel@tonic-gate #include <sys/kobj_impl.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * This module is used both during the normal operation of the kernel (i.e.
377c478bd9Sstevel@tonic-gate  * after kmem has been initialized) and during boot (before unix`_start has
387c478bd9Sstevel@tonic-gate  * been called).  kobj_alloc is able to tell the difference between the two
397c478bd9Sstevel@tonic-gate  * cases, and as such must be used instead of kmem_alloc.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate void
437c478bd9Sstevel@tonic-gate zmemcpy(uchar_t *dest, const uchar_t *source, uint_t len)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	bcopy(source, dest, len);
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate struct zchdr {
497c478bd9Sstevel@tonic-gate 	uint_t zch_magic;
507c478bd9Sstevel@tonic-gate 	uint_t zch_size;
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	ZCH_MAGIC	0x3cc13cc1
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
567c478bd9Sstevel@tonic-gate void *
577c478bd9Sstevel@tonic-gate zcalloc(void *opaque, uint_t items, uint_t size)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	size_t nbytes = sizeof (struct zchdr) + items * size;
607c478bd9Sstevel@tonic-gate 	struct zchdr *z = kobj_zalloc(nbytes, KM_NOWAIT|KM_TMP);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (z == NULL)
637c478bd9Sstevel@tonic-gate 		return (NULL);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	z->zch_magic = ZCH_MAGIC;
667c478bd9Sstevel@tonic-gate 	z->zch_size = nbytes;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	return (z + 1);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
727c478bd9Sstevel@tonic-gate void
737c478bd9Sstevel@tonic-gate zcfree(void *opaque, void *ptr)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	struct zchdr *z = ((struct zchdr *)ptr) - 1;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (z->zch_magic != ZCH_MAGIC)
787c478bd9Sstevel@tonic-gate 		panic("zcfree region corrupt: hdr=%p ptr=%p", (void *)z, ptr);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	kobj_free(z, z->zch_size);
817c478bd9Sstevel@tonic-gate }
82