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 5*a574db85Sraf * Common Development and Distribution License (the "License"). 6*a574db85Sraf * 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 */ 21*a574db85Sraf 227c478bd9Sstevel@tonic-gate /* 23*a574db85Sraf * 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*a574db85Sraf #include <unistd.h> 307c478bd9Sstevel@tonic-gate #include <errno.h> 317c478bd9Sstevel@tonic-gate #include <sys/mman.h> 327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 337c478bd9Sstevel@tonic-gate #include "vmem_base.h" 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #define ALLOC_PROT PROT_READ | PROT_WRITE | PROT_EXEC 367c478bd9Sstevel@tonic-gate #define FREE_PROT PROT_NONE 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #define ALLOC_FLAGS MAP_PRIVATE | MAP_ANON 397c478bd9Sstevel@tonic-gate #define FREE_FLAGS MAP_PRIVATE | MAP_ANON | MAP_NORESERVE 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #define CHUNKSIZE (64*1024) /* 64 kilobytes */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static vmem_t *mmap_heap; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static void * 467c478bd9Sstevel@tonic-gate vmem_mmap_alloc(vmem_t *src, size_t size, int vmflags) 477c478bd9Sstevel@tonic-gate { 487c478bd9Sstevel@tonic-gate void *ret; 497c478bd9Sstevel@tonic-gate int old_errno = errno; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate ret = vmem_alloc(src, size, vmflags); 527c478bd9Sstevel@tonic-gate if (ret != NULL && 537c478bd9Sstevel@tonic-gate mmap(ret, size, ALLOC_PROT, ALLOC_FLAGS | MAP_FIXED, -1, 0) == 547c478bd9Sstevel@tonic-gate MAP_FAILED) { 557c478bd9Sstevel@tonic-gate vmem_free(src, ret, size); 567c478bd9Sstevel@tonic-gate vmem_reap(); 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP); 597c478bd9Sstevel@tonic-gate errno = old_errno; 607c478bd9Sstevel@tonic-gate return (NULL); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate errno = old_errno; 647c478bd9Sstevel@tonic-gate return (ret); 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate static void 687c478bd9Sstevel@tonic-gate vmem_mmap_free(vmem_t *src, void *addr, size_t size) 697c478bd9Sstevel@tonic-gate { 707c478bd9Sstevel@tonic-gate int old_errno = errno; 717c478bd9Sstevel@tonic-gate (void) mmap(addr, size, FREE_PROT, FREE_FLAGS | MAP_FIXED, -1, 0); 727c478bd9Sstevel@tonic-gate vmem_free(src, addr, size); 737c478bd9Sstevel@tonic-gate errno = old_errno; 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static void * 777c478bd9Sstevel@tonic-gate vmem_mmap_top_alloc(vmem_t *src, size_t size, int vmflags) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate void *ret; 807c478bd9Sstevel@tonic-gate void *buf; 817c478bd9Sstevel@tonic-gate int old_errno = errno; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate ret = vmem_alloc(src, size, VM_NOSLEEP); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (ret) { 867c478bd9Sstevel@tonic-gate errno = old_errno; 877c478bd9Sstevel@tonic-gate return (ret); 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * Need to grow the heap 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate buf = mmap((void *)CHUNKSIZE, size, FREE_PROT, FREE_FLAGS | MAP_ALIGN, 937c478bd9Sstevel@tonic-gate -1, 0); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (buf != MAP_FAILED) { 967c478bd9Sstevel@tonic-gate ret = _vmem_extend_alloc(src, buf, size, size, vmflags); 977c478bd9Sstevel@tonic-gate if (ret != NULL) 987c478bd9Sstevel@tonic-gate return (ret); 997c478bd9Sstevel@tonic-gate else { 1007c478bd9Sstevel@tonic-gate (void) munmap(buf, size); 1017c478bd9Sstevel@tonic-gate errno = old_errno; 1027c478bd9Sstevel@tonic-gate return (NULL); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate } else { 1057c478bd9Sstevel@tonic-gate /* 1067c478bd9Sstevel@tonic-gate * Growing the heap failed. The allocation above will 1077c478bd9Sstevel@tonic-gate * already have called umem_reap(). 1087c478bd9Sstevel@tonic-gate */ 1097c478bd9Sstevel@tonic-gate ASSERT((vmflags & VM_NOSLEEP) == VM_NOSLEEP); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate errno = old_errno; 1127c478bd9Sstevel@tonic-gate return (NULL); 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate vmem_t * 1177c478bd9Sstevel@tonic-gate vmem_mmap_arena(vmem_alloc_t **a_out, vmem_free_t **f_out) 1187c478bd9Sstevel@tonic-gate { 119*a574db85Sraf size_t pagesize = sysconf(_SC_PAGESIZE); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate if (mmap_heap == NULL) { 1227c478bd9Sstevel@tonic-gate mmap_heap = vmem_init("mmap_top", CHUNKSIZE, 1237c478bd9Sstevel@tonic-gate vmem_mmap_top_alloc, vmem_free, 1247c478bd9Sstevel@tonic-gate "mmap_heap", NULL, 0, pagesize, 1257c478bd9Sstevel@tonic-gate vmem_mmap_alloc, vmem_mmap_free); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if (a_out != NULL) 1297c478bd9Sstevel@tonic-gate *a_out = vmem_mmap_alloc; 1307c478bd9Sstevel@tonic-gate if (f_out != NULL) 1317c478bd9Sstevel@tonic-gate *f_out = vmem_mmap_free; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate return (mmap_heap); 1347c478bd9Sstevel@tonic-gate } 135