xref: /titanic_41/usr/src/lib/libc/port/sys/sbrk.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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
58cd45542Sraf  * Common Development and Distribution License (the "License").
68cd45542Sraf  * 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  */
218cd45542Sraf 
227c478bd9Sstevel@tonic-gate /*
238cd45542Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29*7257d1b4Sraf #pragma weak _sbrk = sbrk
30*7257d1b4Sraf #pragma weak _brk = brk
317c478bd9Sstevel@tonic-gate 
32*7257d1b4Sraf #include "lint.h"
337c478bd9Sstevel@tonic-gate #include <synch.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <inttypes.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include "mtlib.h"
417c478bd9Sstevel@tonic-gate #include "libc.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate extern int _end;
447c478bd9Sstevel@tonic-gate void *_nd = &_end;
457c478bd9Sstevel@tonic-gate mutex_t __sbrk_lock = DEFAULTMUTEX;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern int _brk_unlocked(void *);
48*7257d1b4Sraf extern void *_sbrk_unlocked(intptr_t);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * The break must always be at least 8-byte aligned
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate #if (_MAX_ALIGNMENT < 8)
547c478bd9Sstevel@tonic-gate #define	ALIGNSZ		8
557c478bd9Sstevel@tonic-gate #else
567c478bd9Sstevel@tonic-gate #define	ALIGNSZ		_MAX_ALIGNMENT
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	BRKALIGN(x)	(caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate void *
sbrk(intptr_t addend)627c478bd9Sstevel@tonic-gate sbrk(intptr_t addend)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	void *result;
657c478bd9Sstevel@tonic-gate 
668cd45542Sraf 	if (!primary_link_map) {
678cd45542Sraf 		errno = ENOTSUP;
688cd45542Sraf 		return ((void *)-1);
698cd45542Sraf 	}
707c478bd9Sstevel@tonic-gate 	lmutex_lock(&__sbrk_lock);
717c478bd9Sstevel@tonic-gate 	result = _sbrk_unlocked(addend);
727c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__sbrk_lock);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	return (result);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * _sbrk_unlocked() aligns the old break, adds the addend, aligns
797c478bd9Sstevel@tonic-gate  * the new break, and calls _brk_unlocked() to set the new break.
807c478bd9Sstevel@tonic-gate  * We must align the old break because _nd may begin life misaligned.
817c478bd9Sstevel@tonic-gate  * The addend can be either positive or negative, so there are two
827c478bd9Sstevel@tonic-gate  * overflow/underflow edge conditions to reject:
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  *   - the addend is negative and brk + addend < 0.
857c478bd9Sstevel@tonic-gate  *   - the addend is positive and brk + addend > ULONG_MAX
867c478bd9Sstevel@tonic-gate  */
87*7257d1b4Sraf void *
_sbrk_unlocked(intptr_t addend)887c478bd9Sstevel@tonic-gate _sbrk_unlocked(intptr_t addend)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	char *old_brk = BRKALIGN(_nd);
917c478bd9Sstevel@tonic-gate 	char *new_brk = BRKALIGN(old_brk + addend);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if ((addend > 0 && new_brk < old_brk) ||
947c478bd9Sstevel@tonic-gate 	    (addend < 0 && new_brk > old_brk)) {
957c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
967c478bd9Sstevel@tonic-gate 		return ((void *)-1);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	if (_brk_unlocked(new_brk) != 0)
997c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1007c478bd9Sstevel@tonic-gate 	_nd = new_brk;
1017c478bd9Sstevel@tonic-gate 	return (old_brk);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * _sbrk_grow_aligned() aligns the old break to a low_align boundry,
1067c478bd9Sstevel@tonic-gate  * adds min_size, aligns to a high_align boundry, and calls _brk_unlocked()
1077c478bd9Sstevel@tonic-gate  * to set the new break.  The low_aligned-aligned value is returned, and
1087c478bd9Sstevel@tonic-gate  * the actual space allocated is returned through actual_size.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * Unlike sbrk(2), _sbrk_grow_aligned takes an unsigned size, and does
1117c478bd9Sstevel@tonic-gate  * not allow shrinking the heap.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate void *
_sbrk_grow_aligned(size_t min_size,size_t low_align,size_t high_align,size_t * actual_size)1147c478bd9Sstevel@tonic-gate _sbrk_grow_aligned(size_t min_size, size_t low_align, size_t high_align,
1157c478bd9Sstevel@tonic-gate     size_t *actual_size)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	uintptr_t old_brk;
1187c478bd9Sstevel@tonic-gate 	uintptr_t ret_brk;
1197c478bd9Sstevel@tonic-gate 	uintptr_t high_brk;
1207c478bd9Sstevel@tonic-gate 	uintptr_t new_brk;
1217c478bd9Sstevel@tonic-gate 	int brk_result;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (!primary_link_map) {
1247c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
1257c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	if ((low_align & (low_align - 1)) != 0 ||
1287c478bd9Sstevel@tonic-gate 	    (high_align & (high_align - 1)) != 0) {
1297c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1307c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	low_align = MAX(low_align, ALIGNSZ);
1337c478bd9Sstevel@tonic-gate 	high_align = MAX(high_align, ALIGNSZ);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	lmutex_lock(&__sbrk_lock);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	old_brk = (uintptr_t)BRKALIGN(_nd);
1387c478bd9Sstevel@tonic-gate 	ret_brk = P2ROUNDUP(old_brk, low_align);
1397c478bd9Sstevel@tonic-gate 	high_brk = ret_brk + min_size;
1407c478bd9Sstevel@tonic-gate 	new_brk = P2ROUNDUP(high_brk, high_align);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * Check for overflow
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	if (ret_brk < old_brk || high_brk < ret_brk || new_brk < high_brk) {
1467c478bd9Sstevel@tonic-gate 		lmutex_unlock(&__sbrk_lock);
1477c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1487c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if ((brk_result = _brk_unlocked((void *)new_brk)) == 0)
1527c478bd9Sstevel@tonic-gate 		_nd = (void *)new_brk;
1537c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__sbrk_lock);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	if (brk_result != 0)
1567c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (actual_size != NULL)
1597c478bd9Sstevel@tonic-gate 		*actual_size = (new_brk - ret_brk);
1607c478bd9Sstevel@tonic-gate 	return ((void *)ret_brk);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate int
brk(void * new_brk)1647c478bd9Sstevel@tonic-gate brk(void *new_brk)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	int result;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (!primary_link_map) {
1697c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
1707c478bd9Sstevel@tonic-gate 		return (-1);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Need to align this here;  _brk_unlocked won't do it for us.
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	new_brk = BRKALIGN(new_brk);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	lmutex_lock(&__sbrk_lock);
1787c478bd9Sstevel@tonic-gate 	if ((result = _brk_unlocked(new_brk)) == 0)
1797c478bd9Sstevel@tonic-gate 		_nd = new_brk;
1807c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__sbrk_lock);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	return (result);
1837c478bd9Sstevel@tonic-gate }
184