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