10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
56515Sraf * Common Development and Distribution License (the "License").
66515Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
216515Sraf
220Sstevel@tonic-gate /*
236515Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
29*6812Sraf #pragma weak _sbrk = sbrk
30*6812Sraf #pragma weak _brk = brk
310Sstevel@tonic-gate
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include <synch.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <sys/isa_defs.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <inttypes.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include "mtlib.h"
410Sstevel@tonic-gate #include "libc.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate extern int _end;
440Sstevel@tonic-gate void *_nd = &_end;
450Sstevel@tonic-gate mutex_t __sbrk_lock = DEFAULTMUTEX;
460Sstevel@tonic-gate
470Sstevel@tonic-gate extern int _brk_unlocked(void *);
48*6812Sraf extern void *_sbrk_unlocked(intptr_t);
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * The break must always be at least 8-byte aligned
520Sstevel@tonic-gate */
530Sstevel@tonic-gate #if (_MAX_ALIGNMENT < 8)
540Sstevel@tonic-gate #define ALIGNSZ 8
550Sstevel@tonic-gate #else
560Sstevel@tonic-gate #define ALIGNSZ _MAX_ALIGNMENT
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate
590Sstevel@tonic-gate #define BRKALIGN(x) (caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
600Sstevel@tonic-gate
610Sstevel@tonic-gate void *
sbrk(intptr_t addend)620Sstevel@tonic-gate sbrk(intptr_t addend)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate void *result;
650Sstevel@tonic-gate
666515Sraf if (!primary_link_map) {
676515Sraf errno = ENOTSUP;
686515Sraf return ((void *)-1);
696515Sraf }
700Sstevel@tonic-gate lmutex_lock(&__sbrk_lock);
710Sstevel@tonic-gate result = _sbrk_unlocked(addend);
720Sstevel@tonic-gate lmutex_unlock(&__sbrk_lock);
730Sstevel@tonic-gate
740Sstevel@tonic-gate return (result);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * _sbrk_unlocked() aligns the old break, adds the addend, aligns
790Sstevel@tonic-gate * the new break, and calls _brk_unlocked() to set the new break.
800Sstevel@tonic-gate * We must align the old break because _nd may begin life misaligned.
810Sstevel@tonic-gate * The addend can be either positive or negative, so there are two
820Sstevel@tonic-gate * overflow/underflow edge conditions to reject:
830Sstevel@tonic-gate *
840Sstevel@tonic-gate * - the addend is negative and brk + addend < 0.
850Sstevel@tonic-gate * - the addend is positive and brk + addend > ULONG_MAX
860Sstevel@tonic-gate */
87*6812Sraf void *
_sbrk_unlocked(intptr_t addend)880Sstevel@tonic-gate _sbrk_unlocked(intptr_t addend)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate char *old_brk = BRKALIGN(_nd);
910Sstevel@tonic-gate char *new_brk = BRKALIGN(old_brk + addend);
920Sstevel@tonic-gate
930Sstevel@tonic-gate if ((addend > 0 && new_brk < old_brk) ||
940Sstevel@tonic-gate (addend < 0 && new_brk > old_brk)) {
950Sstevel@tonic-gate errno = ENOMEM;
960Sstevel@tonic-gate return ((void *)-1);
970Sstevel@tonic-gate }
980Sstevel@tonic-gate if (_brk_unlocked(new_brk) != 0)
990Sstevel@tonic-gate return ((void *)-1);
1000Sstevel@tonic-gate _nd = new_brk;
1010Sstevel@tonic-gate return (old_brk);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * _sbrk_grow_aligned() aligns the old break to a low_align boundry,
1060Sstevel@tonic-gate * adds min_size, aligns to a high_align boundry, and calls _brk_unlocked()
1070Sstevel@tonic-gate * to set the new break. The low_aligned-aligned value is returned, and
1080Sstevel@tonic-gate * the actual space allocated is returned through actual_size.
1090Sstevel@tonic-gate *
1100Sstevel@tonic-gate * Unlike sbrk(2), _sbrk_grow_aligned takes an unsigned size, and does
1110Sstevel@tonic-gate * not allow shrinking the heap.
1120Sstevel@tonic-gate */
1130Sstevel@tonic-gate void *
_sbrk_grow_aligned(size_t min_size,size_t low_align,size_t high_align,size_t * actual_size)1140Sstevel@tonic-gate _sbrk_grow_aligned(size_t min_size, size_t low_align, size_t high_align,
1150Sstevel@tonic-gate size_t *actual_size)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate uintptr_t old_brk;
1180Sstevel@tonic-gate uintptr_t ret_brk;
1190Sstevel@tonic-gate uintptr_t high_brk;
1200Sstevel@tonic-gate uintptr_t new_brk;
1210Sstevel@tonic-gate int brk_result;
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate if (!primary_link_map) {
1240Sstevel@tonic-gate errno = ENOTSUP;
1250Sstevel@tonic-gate return ((void *)-1);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate if ((low_align & (low_align - 1)) != 0 ||
1280Sstevel@tonic-gate (high_align & (high_align - 1)) != 0) {
1290Sstevel@tonic-gate errno = EINVAL;
1300Sstevel@tonic-gate return ((void *)-1);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate low_align = MAX(low_align, ALIGNSZ);
1330Sstevel@tonic-gate high_align = MAX(high_align, ALIGNSZ);
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate lmutex_lock(&__sbrk_lock);
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate old_brk = (uintptr_t)BRKALIGN(_nd);
1380Sstevel@tonic-gate ret_brk = P2ROUNDUP(old_brk, low_align);
1390Sstevel@tonic-gate high_brk = ret_brk + min_size;
1400Sstevel@tonic-gate new_brk = P2ROUNDUP(high_brk, high_align);
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate * Check for overflow
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate if (ret_brk < old_brk || high_brk < ret_brk || new_brk < high_brk) {
1460Sstevel@tonic-gate lmutex_unlock(&__sbrk_lock);
1470Sstevel@tonic-gate errno = ENOMEM;
1480Sstevel@tonic-gate return ((void *)-1);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate if ((brk_result = _brk_unlocked((void *)new_brk)) == 0)
1520Sstevel@tonic-gate _nd = (void *)new_brk;
1530Sstevel@tonic-gate lmutex_unlock(&__sbrk_lock);
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (brk_result != 0)
1560Sstevel@tonic-gate return ((void *)-1);
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if (actual_size != NULL)
1590Sstevel@tonic-gate *actual_size = (new_brk - ret_brk);
1600Sstevel@tonic-gate return ((void *)ret_brk);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate int
brk(void * new_brk)1640Sstevel@tonic-gate brk(void *new_brk)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate int result;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate if (!primary_link_map) {
1690Sstevel@tonic-gate errno = ENOTSUP;
1700Sstevel@tonic-gate return (-1);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * Need to align this here; _brk_unlocked won't do it for us.
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate new_brk = BRKALIGN(new_brk);
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate lmutex_lock(&__sbrk_lock);
1780Sstevel@tonic-gate if ((result = _brk_unlocked(new_brk)) == 0)
1790Sstevel@tonic-gate _nd = new_brk;
1800Sstevel@tonic-gate lmutex_unlock(&__sbrk_lock);
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate return (result);
1830Sstevel@tonic-gate }
184