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 /*
28*7c478bd9Sstevel@tonic-gate * Standalone-specific vmem routines
29*7c478bd9Sstevel@tonic-gate *
30*7c478bd9Sstevel@tonic-gate * The standalone allocator operates on a pre-existing blob of memory, the
31*7c478bd9Sstevel@tonic-gate * location and dimensions of which are set using vmem_stand_setsize(). We
32*7c478bd9Sstevel@tonic-gate * then hand out CHUNKSIZE-sized pieces of this blob, until we run out.
33*7c478bd9Sstevel@tonic-gate */
34*7c478bd9Sstevel@tonic-gate
35*7c478bd9Sstevel@tonic-gate #define DEF_CHUNKSIZE (64 * 1024) /* 64K */
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate #define DEF_NREGIONS 2
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <limits.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/mman.h>
43*7c478bd9Sstevel@tonic-gate #include <unistd.h>
44*7c478bd9Sstevel@tonic-gate #include <strings.h>
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #include "vmem_base.h"
47*7c478bd9Sstevel@tonic-gate #include "misc.h"
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate static vmem_t *stand_heap;
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate static size_t stand_chunksize;
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate typedef struct stand_region {
54*7c478bd9Sstevel@tonic-gate caddr_t sr_base;
55*7c478bd9Sstevel@tonic-gate caddr_t sr_curtop;
56*7c478bd9Sstevel@tonic-gate size_t sr_left;
57*7c478bd9Sstevel@tonic-gate } stand_region_t;
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate static stand_region_t stand_regions[DEF_NREGIONS];
60*7c478bd9Sstevel@tonic-gate static int stand_nregions;
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate extern void membar_producer(void);
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate void
vmem_stand_init(void)65*7c478bd9Sstevel@tonic-gate vmem_stand_init(void)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate stand_chunksize = MAX(DEF_CHUNKSIZE, pagesize);
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate stand_nregions = 0;
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate int
vmem_stand_add(caddr_t base,size_t len)73*7c478bd9Sstevel@tonic-gate vmem_stand_add(caddr_t base, size_t len)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate stand_region_t *sr = &stand_regions[stand_nregions];
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate ASSERT(pagesize != 0);
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate if (stand_nregions == DEF_NREGIONS) {
80*7c478bd9Sstevel@tonic-gate errno = ENOSPC;
81*7c478bd9Sstevel@tonic-gate return (-1); /* we don't have room -- throw it back */
82*7c478bd9Sstevel@tonic-gate }
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate /*
85*7c478bd9Sstevel@tonic-gate * We guarantee that only one call to `vmem_stand_add' will be
86*7c478bd9Sstevel@tonic-gate * active at a time, but we can't ensure that the allocator won't be
87*7c478bd9Sstevel@tonic-gate * in use while this function is being called. As such, we have to
88*7c478bd9Sstevel@tonic-gate * ensure that sr is populated and visible to other processors before
89*7c478bd9Sstevel@tonic-gate * allowing the allocator to access the new region.
90*7c478bd9Sstevel@tonic-gate */
91*7c478bd9Sstevel@tonic-gate sr->sr_base = base;
92*7c478bd9Sstevel@tonic-gate sr->sr_curtop = (caddr_t)P2ROUNDUP((ulong_t)base, stand_chunksize);
93*7c478bd9Sstevel@tonic-gate sr->sr_left = P2ALIGN(len - (size_t)(sr->sr_curtop - sr->sr_base),
94*7c478bd9Sstevel@tonic-gate stand_chunksize);
95*7c478bd9Sstevel@tonic-gate membar_producer();
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate stand_nregions++;
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate return (0);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate static void *
stand_parent_alloc(vmem_t * src,size_t size,int vmflags)103*7c478bd9Sstevel@tonic-gate stand_parent_alloc(vmem_t *src, size_t size, int vmflags)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate int old_errno = errno;
106*7c478bd9Sstevel@tonic-gate stand_region_t *sr;
107*7c478bd9Sstevel@tonic-gate size_t chksize;
108*7c478bd9Sstevel@tonic-gate void *ret;
109*7c478bd9Sstevel@tonic-gate int i;
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate if ((ret = vmem_alloc(src, size, VM_NOSLEEP)) != NULL) {
112*7c478bd9Sstevel@tonic-gate errno = old_errno;
113*7c478bd9Sstevel@tonic-gate return (ret);
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate /* We need to allocate another chunk */
117*7c478bd9Sstevel@tonic-gate chksize = roundup(size, stand_chunksize);
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate for (sr = stand_regions, i = 0; i < stand_nregions; i++, sr++) {
120*7c478bd9Sstevel@tonic-gate if (sr->sr_left >= chksize)
121*7c478bd9Sstevel@tonic-gate break;
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate if (i == stand_nregions) {
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate * We don't have enough in any of our regions to satisfy the
127*7c478bd9Sstevel@tonic-gate * request.
128*7c478bd9Sstevel@tonic-gate */
129*7c478bd9Sstevel@tonic-gate errno = old_errno;
130*7c478bd9Sstevel@tonic-gate return (NULL);
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate if ((ret = _vmem_extend_alloc(src, sr->sr_curtop, chksize, size,
134*7c478bd9Sstevel@tonic-gate vmflags)) == NULL) {
135*7c478bd9Sstevel@tonic-gate errno = old_errno;
136*7c478bd9Sstevel@tonic-gate return (NULL);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate
139*7c478bd9Sstevel@tonic-gate bzero(sr->sr_curtop, chksize);
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate sr->sr_curtop += chksize;
142*7c478bd9Sstevel@tonic-gate sr->sr_left -= chksize;
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate return (ret);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate vmem_t *
vmem_stand_arena(vmem_alloc_t ** a_out,vmem_free_t ** f_out)148*7c478bd9Sstevel@tonic-gate vmem_stand_arena(vmem_alloc_t **a_out, vmem_free_t **f_out)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate ASSERT(stand_nregions == 1);
151*7c478bd9Sstevel@tonic-gate
152*7c478bd9Sstevel@tonic-gate stand_heap = vmem_init("stand_parent", stand_chunksize,
153*7c478bd9Sstevel@tonic-gate stand_parent_alloc, vmem_free,
154*7c478bd9Sstevel@tonic-gate "stand_heap", NULL, 0, pagesize, vmem_alloc, vmem_free);
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate if (a_out != NULL)
157*7c478bd9Sstevel@tonic-gate *a_out = vmem_alloc;
158*7c478bd9Sstevel@tonic-gate if (f_out != NULL)
159*7c478bd9Sstevel@tonic-gate *f_out = vmem_free;
160*7c478bd9Sstevel@tonic-gate
161*7c478bd9Sstevel@tonic-gate return (stand_heap);
162*7c478bd9Sstevel@tonic-gate }
163