2 *------------------------------------------------------------------
3 * svm.h - shared VM allocation, mmap(...MAP_FIXED...)
6 * Copyright (c) 2009 Cisco and/or its affiliates.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *------------------------------------------------------------------
21 #ifndef __included_svm_h__
22 #define __included_svm_h__
25 #include <vppinfra/clib.h>
26 #include <vppinfra/mem.h>
28 #define MMAP_PAGESIZE (clib_mem_get_page_size())
30 #define SVM_VERSION ((1<<16) | 1) /* set to declare region ready. */
32 #define SVM_FLAGS_MHEAP (1<<0) /* region contains an mheap */
33 #define SVM_FLAGS_FILE (1<<1) /* region backed by one or more files */
34 #define SVM_FLAGS_NODATA (1<<2) /* region will be further subdivided */
35 #define SVM_FLAGS_NEED_DATA_INIT (1<<3)
37 #define SVM_PVT_MHEAP_SIZE (128<<10) /* region's private mheap (128k) */
39 typedef struct svm_region_ {
40 volatile uword version;
41 pthread_mutex_t mutex;
42 pthread_cond_t condvar;
43 int mutex_owner_pid; /* in case of trouble */
46 uword virtual_base; /* base of the region object */
49 void *data_base; /* data portion base address */
50 void *data_heap; /* data heap, if any */
51 volatile void *user_ctx; /* user context pointer */
52 /* stuff allocated in the region's heap */
53 uword bitmap_size; /* nbits in virtual alloc bitmap */
54 uword *bitmap; /* the bitmap */
62 * (64K) clib heap for the region itself
64 * data_base -> whatever is in this region
69 typedef struct svm_map_region_args_ {
70 char *root_path; /* NULL means use the truly global arena */
76 uword backing_mmap_size;
77 /* uid, gid to own the svm region(s) */
80 } svm_map_region_args_t;
84 * Memory shared across all router instances. Packet buffers, etc
85 * Base should be "out of the way," and size should be big enough to
86 * cover everything we plan to put here.
88 #define SVM_GLOBAL_REGION_BASEVA 0x30000000
89 #define SVM_GLOBAL_REGION_SIZE (64<<20)
90 #define SVM_GLOBAL_REGION_NAME "/global_vm"
93 * Memory shared across individual router instances.
95 #define SVM_OVERLAY_REGION_BASEVA \
96 (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE)
97 #define SVM_OVERLAY_REGION_SIZE (1<<20)
98 #define SVM_OVERLAY_REGION_BASENAME "/overlay_vm"
105 svm_subregion_t *subregions; /* subregion pool */
111 void *svm_region_find_or_create (svm_map_region_args_t *a);
112 void svm_region_init(void);
113 void svm_region_init_chroot(char *root_path);
114 void svm_region_init_chroot_uid_gid(char *root_path, int uid, int gid);
115 void svm_region_exit (void);
116 void svm_region_unmap(void *rp_arg);
117 void svm_client_scan (char *root_path);
118 void svm_client_scan_this_region_nolock (svm_region_t *rp);
119 u8 *shm_name_from_svm_map_region_args (svm_map_region_args_t *a);
121 static inline void *svm_mem_alloc (svm_region_t *rp, uword size)
124 ASSERT(rp->flags & SVM_FLAGS_MHEAP);
127 pthread_mutex_lock(&rp->mutex);
128 oldheap = clib_mem_set_heap(rp->data_heap);
129 rv = clib_mem_alloc (size);
130 clib_mem_set_heap(oldheap);
131 pthread_mutex_unlock(&rp->mutex);
135 static inline void *svm_mem_alloc_aligned_at_offset (svm_region_t *rp,
141 ASSERT(rp->flags & SVM_FLAGS_MHEAP);
144 pthread_mutex_lock(&rp->mutex);
145 oldheap = clib_mem_set_heap(rp->data_heap);
146 rv = clib_mem_alloc_aligned_at_offset (size, align, offset);
147 clib_mem_set_heap(oldheap);
148 pthread_mutex_unlock(&rp->mutex);
152 static inline void svm_mem_free (svm_region_t *rp, void *ptr)
155 ASSERT(rp->flags & SVM_FLAGS_MHEAP);
157 pthread_mutex_lock(&rp->mutex);
158 oldheap = clib_mem_set_heap(rp->data_heap);
160 clib_mem_set_heap(oldheap);
161 pthread_mutex_unlock(&rp->mutex);
165 static inline void *svm_push_pvt_heap (svm_region_t *rp)
168 oldheap = clib_mem_set_heap(rp->region_heap);
169 return ((void *) oldheap);
172 static inline void *svm_push_data_heap (svm_region_t *rp)
175 oldheap = clib_mem_set_heap(rp->data_heap);
176 return ((void *) oldheap);
179 static inline void svm_pop_heap (void *oldheap)
181 clib_mem_set_heap(oldheap);
184 u8 * format_svm_region (u8 * s, va_list * args);
186 svm_region_t *svm_get_root_rp (void);
188 #endif /* __included_svm_h__ */