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
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * These routines simply provide wrappers around malloc(3C) and free(3C)
297c478bd9Sstevel@tonic-gate * for now. In the future we hope to provide a userland equivalent to
307c478bd9Sstevel@tonic-gate * the kmem allocator, including cache allocators.
317c478bd9Sstevel@tonic-gate */
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <poll.h>
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #ifdef _KMDB
387c478bd9Sstevel@tonic-gate #include <kmdb/kmdb_fault.h>
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #include <mdb/mdb_debug.h>
417c478bd9Sstevel@tonic-gate #include <mdb/mdb_stdlib.h>
427c478bd9Sstevel@tonic-gate #include <mdb/mdb_frame.h>
437c478bd9Sstevel@tonic-gate #include <mdb/mdb_umem.h>
447c478bd9Sstevel@tonic-gate #include <mdb/mdb_err.h>
457c478bd9Sstevel@tonic-gate #include <mdb/mdb.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define UMF_DEBUG 0x1
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #ifdef DEBUG
507c478bd9Sstevel@tonic-gate int mdb_umem_flags = UMF_DEBUG;
517c478bd9Sstevel@tonic-gate #else
527c478bd9Sstevel@tonic-gate int mdb_umem_flags = 0;
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate struct mdb_mblk {
567c478bd9Sstevel@tonic-gate void *blk_addr; /* address of allocated block */
577c478bd9Sstevel@tonic-gate size_t blk_size; /* size of block in bytes */
587c478bd9Sstevel@tonic-gate struct mdb_mblk *blk_next; /* link to next block */
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
627c478bd9Sstevel@tonic-gate static void *
mdb_umem_handler(size_t nbytes,size_t align,uint_t flags)637c478bd9Sstevel@tonic-gate mdb_umem_handler(size_t nbytes, size_t align, uint_t flags)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate #ifdef _KMDB
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * kmdb has a fixed, dedicated VA range in which to play. This range
697c478bd9Sstevel@tonic-gate * won't change size while the debugger is running, regardless of how
707c478bd9Sstevel@tonic-gate * long we wait. As a result, the only sensible course of action is
717c478bd9Sstevel@tonic-gate * to fail the request. If we're here, however, the request was made
727c478bd9Sstevel@tonic-gate * with UM_SLEEP. The caller is thus not expecting a NULL back. We'll
737c478bd9Sstevel@tonic-gate * have to fail the current dcmd set.
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate if (mdb.m_depth > 0) {
767c478bd9Sstevel@tonic-gate warn("failed to allocate %lu bytes -- recovering\n",
777c478bd9Sstevel@tonic-gate (ulong_t)nbytes);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate kmdb_print_stack();
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate longjmp(mdb.m_frame->f_pcb, MDB_ERR_NOMEM);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate #else
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * mdb, on the other hand, can afford to wait, as someone may actually
887c478bd9Sstevel@tonic-gate * free something.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate if (errno == EAGAIN) {
917c478bd9Sstevel@tonic-gate void *ptr = NULL;
927c478bd9Sstevel@tonic-gate char buf[64];
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate (void) mdb_iob_snprintf(buf, sizeof (buf),
957c478bd9Sstevel@tonic-gate "[ sleeping for %lu bytes of free memory ... ]",
967c478bd9Sstevel@tonic-gate (ulong_t)nbytes);
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate (void) mdb_iob_puts(mdb.m_err, buf);
997c478bd9Sstevel@tonic-gate (void) mdb_iob_flush(mdb.m_err);
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate do {
1027c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, 1000);
1037c478bd9Sstevel@tonic-gate if (align != 0)
1047c478bd9Sstevel@tonic-gate ptr = memalign(align, nbytes);
1057c478bd9Sstevel@tonic-gate else
1067c478bd9Sstevel@tonic-gate ptr = malloc(nbytes);
1077c478bd9Sstevel@tonic-gate } while (ptr == NULL && errno == EAGAIN);
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (ptr != NULL)
1107c478bd9Sstevel@tonic-gate return (ptr);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate (void) memset(buf, '\b', strlen(buf));
1137c478bd9Sstevel@tonic-gate (void) mdb_iob_puts(mdb.m_err, buf);
1147c478bd9Sstevel@tonic-gate (void) mdb_iob_flush(mdb.m_err);
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate (void) memset(buf, ' ', strlen(buf));
1177c478bd9Sstevel@tonic-gate (void) mdb_iob_puts(mdb.m_err, buf);
1187c478bd9Sstevel@tonic-gate (void) mdb_iob_flush(mdb.m_err);
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate (void) memset(buf, '\b', strlen(buf));
1217c478bd9Sstevel@tonic-gate (void) mdb_iob_puts(mdb.m_err, buf);
1227c478bd9Sstevel@tonic-gate (void) mdb_iob_flush(mdb.m_err);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate #endif
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate die("failed to allocate %lu bytes -- terminating\n", (ulong_t)nbytes);
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*NOTREACHED*/
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate return (NULL);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate static void
mdb_umem_gc_enter(void * ptr,size_t nbytes)1347c478bd9Sstevel@tonic-gate mdb_umem_gc_enter(void *ptr, size_t nbytes)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate mdb_mblk_t *blkp = mdb_alloc(sizeof (mdb_mblk_t), UM_SLEEP);
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate blkp->blk_addr = ptr;
1397c478bd9Sstevel@tonic-gate blkp->blk_size = nbytes;
1407c478bd9Sstevel@tonic-gate blkp->blk_next = mdb.m_frame->f_mblks;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate mdb.m_frame->f_mblks = blkp;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate * If we're compiled in debug mode, we use this function (gratuitously
1477c478bd9Sstevel@tonic-gate * stolen from kmem.c) to set uninitialized and freed regions to
1487c478bd9Sstevel@tonic-gate * special bit patterns.
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate static void
mdb_umem_copy_pattern(uint32_t pattern,void * buf_arg,size_t size)1517c478bd9Sstevel@tonic-gate mdb_umem_copy_pattern(uint32_t pattern, void *buf_arg, size_t size)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate /* LINTED - alignment of bufend */
1547c478bd9Sstevel@tonic-gate uint32_t *bufend = (uint32_t *)((char *)buf_arg + size);
1557c478bd9Sstevel@tonic-gate uint32_t *buf = buf_arg;
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate while (buf < bufend - 3) {
1587c478bd9Sstevel@tonic-gate buf[3] = buf[2] = buf[1] = buf[0] = pattern;
1597c478bd9Sstevel@tonic-gate buf += 4;
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate while (buf < bufend)
1637c478bd9Sstevel@tonic-gate *buf++ = pattern;
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate void *
mdb_alloc_align(size_t nbytes,size_t align,uint_t flags)1677c478bd9Sstevel@tonic-gate mdb_alloc_align(size_t nbytes, size_t align, uint_t flags)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate void *ptr;
170*258e8624SRobert Mustacchi size_t obytes = nbytes;
1717c478bd9Sstevel@tonic-gate
172*258e8624SRobert Mustacchi if (nbytes == 0 || nbytes > MDB_ALLOC_MAX)
1737c478bd9Sstevel@tonic-gate return (NULL);
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate nbytes = (nbytes + sizeof (uint32_t) - 1) & ~(sizeof (uint32_t) - 1);
176*258e8624SRobert Mustacchi if (nbytes < obytes || nbytes == 0)
177*258e8624SRobert Mustacchi return (NULL);
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate if (align != 0)
1807c478bd9Sstevel@tonic-gate ptr = memalign(align, nbytes);
1817c478bd9Sstevel@tonic-gate else
1827c478bd9Sstevel@tonic-gate ptr = malloc(nbytes);
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate if (flags & UM_SLEEP) {
1857c478bd9Sstevel@tonic-gate while (ptr == NULL)
1867c478bd9Sstevel@tonic-gate ptr = mdb_umem_handler(nbytes, align, flags);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate if (ptr != NULL && (mdb_umem_flags & UMF_DEBUG) != 0)
1907c478bd9Sstevel@tonic-gate mdb_umem_copy_pattern(UMEM_UNINITIALIZED_PATTERN, ptr, nbytes);
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate if (flags & UM_GC)
1937c478bd9Sstevel@tonic-gate mdb_umem_gc_enter(ptr, nbytes);
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate return (ptr);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate void *
mdb_alloc(size_t nbytes,uint_t flags)1997c478bd9Sstevel@tonic-gate mdb_alloc(size_t nbytes, uint_t flags)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate return (mdb_alloc_align(nbytes, 0, flags));
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate void *
mdb_zalloc(size_t nbytes,uint_t flags)2057c478bd9Sstevel@tonic-gate mdb_zalloc(size_t nbytes, uint_t flags)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate void *ptr = mdb_alloc(nbytes, flags);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if (ptr != NULL)
2107c478bd9Sstevel@tonic-gate bzero(ptr, nbytes);
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate return (ptr);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate void
mdb_free(void * ptr,size_t nbytes)2167c478bd9Sstevel@tonic-gate mdb_free(void *ptr, size_t nbytes)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate ASSERT(ptr != NULL || nbytes == 0);
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate nbytes = (nbytes + sizeof (uint32_t) - 1) & ~(sizeof (uint32_t) - 1);
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate if (ptr != NULL) {
2237c478bd9Sstevel@tonic-gate if (mdb_umem_flags & UMF_DEBUG)
2247c478bd9Sstevel@tonic-gate mdb_umem_copy_pattern(UMEM_FREE_PATTERN, ptr, nbytes);
2257c478bd9Sstevel@tonic-gate free(ptr);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate void
mdb_free_align(void * ptr,size_t nbytes)2307c478bd9Sstevel@tonic-gate mdb_free_align(void *ptr, size_t nbytes)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate mdb_free(ptr, nbytes);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate void
mdb_recycle(mdb_mblk_t ** blkpp)2367c478bd9Sstevel@tonic-gate mdb_recycle(mdb_mblk_t **blkpp)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate mdb_mblk_t *blkp, *nblkp;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate for (blkp = *blkpp; blkp != NULL; blkp = nblkp) {
2417c478bd9Sstevel@tonic-gate mdb_dprintf(MDB_DBG_UMEM,
2427c478bd9Sstevel@tonic-gate "garbage collect %p size %lu bytes\n", blkp->blk_addr,
2437c478bd9Sstevel@tonic-gate (ulong_t)blkp->blk_size);
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate nblkp = blkp->blk_next;
2467c478bd9Sstevel@tonic-gate mdb_free(blkp->blk_addr, blkp->blk_size);
2477c478bd9Sstevel@tonic-gate mdb_free(blkp, sizeof (mdb_mblk_t));
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate *blkpp = NULL;
2517c478bd9Sstevel@tonic-gate }
252