1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate * Standalone-specific vmem routines
31*0Sstevel@tonic-gate *
32*0Sstevel@tonic-gate * The standalone allocator operates on a pre-existing blob of memory, the
33*0Sstevel@tonic-gate * location and dimensions of which are set using vmem_stand_setsize(). We
34*0Sstevel@tonic-gate * then hand out CHUNKSIZE-sized pieces of this blob, until we run out.
35*0Sstevel@tonic-gate */
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate #define DEF_CHUNKSIZE (64 * 1024) /* 64K */
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate #define DEF_NREGIONS 2
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #include <errno.h>
42*0Sstevel@tonic-gate #include <limits.h>
43*0Sstevel@tonic-gate #include <sys/sysmacros.h>
44*0Sstevel@tonic-gate #include <sys/mman.h>
45*0Sstevel@tonic-gate #include <unistd.h>
46*0Sstevel@tonic-gate #include <strings.h>
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate #include "vmem_base.h"
49*0Sstevel@tonic-gate #include "misc.h"
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate static vmem_t *stand_heap;
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate static size_t stand_chunksize;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate typedef struct stand_region {
56*0Sstevel@tonic-gate caddr_t sr_base;
57*0Sstevel@tonic-gate caddr_t sr_curtop;
58*0Sstevel@tonic-gate size_t sr_left;
59*0Sstevel@tonic-gate } stand_region_t;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate static stand_region_t stand_regions[DEF_NREGIONS];
62*0Sstevel@tonic-gate static int stand_nregions;
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate extern void membar_producer(void);
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate void
vmem_stand_init(void)67*0Sstevel@tonic-gate vmem_stand_init(void)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate stand_chunksize = MAX(DEF_CHUNKSIZE, pagesize);
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate stand_nregions = 0;
72*0Sstevel@tonic-gate }
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate int
vmem_stand_add(caddr_t base,size_t len)75*0Sstevel@tonic-gate vmem_stand_add(caddr_t base, size_t len)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate stand_region_t *sr = &stand_regions[stand_nregions];
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate ASSERT(pagesize != 0);
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate if (stand_nregions == DEF_NREGIONS) {
82*0Sstevel@tonic-gate errno = ENOSPC;
83*0Sstevel@tonic-gate return (-1); /* we don't have room -- throw it back */
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate * We guarantee that only one call to `vmem_stand_add' will be
88*0Sstevel@tonic-gate * active at a time, but we can't ensure that the allocator won't be
89*0Sstevel@tonic-gate * in use while this function is being called. As such, we have to
90*0Sstevel@tonic-gate * ensure that sr is populated and visible to other processors before
91*0Sstevel@tonic-gate * allowing the allocator to access the new region.
92*0Sstevel@tonic-gate */
93*0Sstevel@tonic-gate sr->sr_base = base;
94*0Sstevel@tonic-gate sr->sr_curtop = (caddr_t)P2ROUNDUP((ulong_t)base, stand_chunksize);
95*0Sstevel@tonic-gate sr->sr_left = P2ALIGN(len - (size_t)(sr->sr_curtop - sr->sr_base),
96*0Sstevel@tonic-gate stand_chunksize);
97*0Sstevel@tonic-gate membar_producer();
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate stand_nregions++;
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate return (0);
102*0Sstevel@tonic-gate }
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate static void *
stand_parent_alloc(vmem_t * src,size_t size,int vmflags)105*0Sstevel@tonic-gate stand_parent_alloc(vmem_t *src, size_t size, int vmflags)
106*0Sstevel@tonic-gate {
107*0Sstevel@tonic-gate int old_errno = errno;
108*0Sstevel@tonic-gate stand_region_t *sr;
109*0Sstevel@tonic-gate size_t chksize;
110*0Sstevel@tonic-gate void *ret;
111*0Sstevel@tonic-gate int i;
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate if ((ret = vmem_alloc(src, size, VM_NOSLEEP)) != NULL) {
114*0Sstevel@tonic-gate errno = old_errno;
115*0Sstevel@tonic-gate return (ret);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate /* We need to allocate another chunk */
119*0Sstevel@tonic-gate chksize = roundup(size, stand_chunksize);
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate for (sr = stand_regions, i = 0; i < stand_nregions; i++, sr++) {
122*0Sstevel@tonic-gate if (sr->sr_left >= chksize)
123*0Sstevel@tonic-gate break;
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate if (i == stand_nregions) {
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate * We don't have enough in any of our regions to satisfy the
129*0Sstevel@tonic-gate * request.
130*0Sstevel@tonic-gate */
131*0Sstevel@tonic-gate errno = old_errno;
132*0Sstevel@tonic-gate return (NULL);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate if ((ret = _vmem_extend_alloc(src, sr->sr_curtop, chksize, size,
136*0Sstevel@tonic-gate vmflags)) == NULL) {
137*0Sstevel@tonic-gate errno = old_errno;
138*0Sstevel@tonic-gate return (NULL);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate bzero(sr->sr_curtop, chksize);
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate sr->sr_curtop += chksize;
144*0Sstevel@tonic-gate sr->sr_left -= chksize;
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate return (ret);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate vmem_t *
vmem_stand_arena(vmem_alloc_t ** a_out,vmem_free_t ** f_out)150*0Sstevel@tonic-gate vmem_stand_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
151*0Sstevel@tonic-gate {
152*0Sstevel@tonic-gate ASSERT(stand_nregions == 1);
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate stand_heap = vmem_init("stand_parent", stand_chunksize,
155*0Sstevel@tonic-gate stand_parent_alloc, vmem_free,
156*0Sstevel@tonic-gate "stand_heap", NULL, 0, pagesize, vmem_alloc, vmem_free);
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate if (a_out != NULL)
159*0Sstevel@tonic-gate *a_out = vmem_alloc;
160*0Sstevel@tonic-gate if (f_out != NULL)
161*0Sstevel@tonic-gate *f_out = vmem_free;
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate return (stand_heap);
164*0Sstevel@tonic-gate }
165