Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2009 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #ifndef __included_svm_common_h__ |
| 19 | #define __included_svm_common_h__ |
| 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <pthread.h> |
Nathan Moos | cf5ba26 | 2021-01-15 14:03:01 -0800 | [diff] [blame] | 23 | #include <sys/user.h> |
Benoît Ganne | 6531cf5 | 2022-09-30 17:13:33 +0200 | [diff] [blame^] | 24 | #include <vppinfra/clib.h> |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 25 | #include <vppinfra/types.h> |
| 26 | |
| 27 | #define SVM_VERSION ((1<<16) | 1) /* set to declare region ready. */ |
| 28 | |
| 29 | #define SVM_FLAGS_MHEAP (1<<0) /* region contains an mheap */ |
| 30 | #define SVM_FLAGS_FILE (1<<1) /* region backed by one or more files */ |
| 31 | #define SVM_FLAGS_NODATA (1<<2) /* region will be further subdivided */ |
| 32 | #define SVM_FLAGS_NEED_DATA_INIT (1<<3) |
| 33 | |
| 34 | #define SVM_PVT_MHEAP_SIZE (128<<10) /* region's private mheap (128k) */ |
| 35 | |
| 36 | typedef struct svm_region_ |
| 37 | { |
| 38 | volatile uword version; |
| 39 | pthread_mutex_t mutex; |
| 40 | pthread_cond_t condvar; |
| 41 | int mutex_owner_pid; /* in case of trouble */ |
| 42 | int mutex_owner_tag; |
| 43 | uword flags; |
| 44 | uword virtual_base; /* base of the region object */ |
| 45 | uword virtual_size; |
| 46 | void *region_heap; |
| 47 | void *data_base; /* data portion base address */ |
| 48 | void *data_heap; /* data heap, if any */ |
| 49 | volatile void *user_ctx; /* user context pointer */ |
| 50 | /* stuff allocated in the region's heap */ |
| 51 | uword bitmap_size; /* nbits in virtual alloc bitmap */ |
| 52 | uword *bitmap; /* the bitmap */ |
| 53 | char *region_name; |
| 54 | char *backing_file; |
| 55 | char **filenames; |
| 56 | uword *client_pids; |
| 57 | /* pad */ |
| 58 | |
| 59 | /* next page: |
| 60 | * (64K) clib heap for the region itself |
| 61 | * |
| 62 | * data_base -> whatever is in this region |
| 63 | */ |
| 64 | |
| 65 | } svm_region_t; |
| 66 | |
| 67 | typedef struct svm_map_region_args_ |
| 68 | { |
| 69 | const char *root_path; /* NULL means use the truly global arena */ |
| 70 | const char *name; |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 71 | uword baseva; |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 72 | u64 size; |
| 73 | u64 pvt_heap_size; |
| 74 | uword flags; |
| 75 | char *backing_file; |
| 76 | uword backing_mmap_size; |
| 77 | /* uid, gid to own the svm region(s) */ |
| 78 | int uid; |
| 79 | int gid; |
| 80 | } svm_map_region_args_t; |
| 81 | |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 82 | /* |
| 83 | * Memory mapped to high addresses for session/vppcom/vcl/etc... |
| 84 | */ |
| 85 | #if __WORDSIZE == 64 |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 86 | #ifdef CLIB_SANITIZE_ADDR |
Benoît Ganne | e3e9d8a | 2020-11-17 17:58:59 +0100 | [diff] [blame] | 87 | #define HIGH_SEGMENT_BASEVA 0x300000000000 /* DO NOT CHANGE THIS: YOU'LL BREAK ASAN */ |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 88 | #else /* CLIB_SANITIZE_ADDR */ |
Nathan Skrzypczak | f260eb9 | 2020-11-10 16:57:49 +0100 | [diff] [blame] | 89 | #define HIGH_SEGMENT_BASEVA (128ULL << 30) /* 128GB */ |
Benoît Ganne | 9fb6d40 | 2019-04-15 15:28:21 +0200 | [diff] [blame] | 90 | #endif /* CLIB_SANITIZE_ADDR */ |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 91 | #elif __WORDSIZE == 32 |
| 92 | #define HIGH_SEGMENT_BASEVA (3584UL << 20) /* 3.5GB */ |
| 93 | #else |
| 94 | #error "unknown __WORDSIZE" |
| 95 | #endif |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Memory shared across all router instances. Packet buffers, etc |
| 99 | * Base should be "out of the way," and size should be big enough to |
| 100 | * cover everything we plan to put here. |
| 101 | */ |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 102 | #define SVM_GLOBAL_REGION_SIZE (64<<20) |
| 103 | #define SVM_GLOBAL_REGION_NAME "/global_vm" |
Damjan Marion | aec8f89 | 2018-01-08 16:35:35 +0100 | [diff] [blame] | 104 | u64 svm_get_global_region_base_va (); |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * Memory shared across individual router instances. |
| 108 | */ |
| 109 | #define SVM_OVERLAY_REGION_BASEVA \ |
| 110 | (SVM_GLOBAL_REGION_BASEVA + SVM_GLOBAL_REGION_SIZE) |
| 111 | #define SVM_OVERLAY_REGION_SIZE (1<<20) |
| 112 | #define SVM_OVERLAY_REGION_BASENAME "/overlay_vm" |
| 113 | |
| 114 | typedef struct |
| 115 | { |
| 116 | u8 *subregion_name; |
| 117 | } svm_subregion_t; |
| 118 | |
| 119 | typedef struct |
| 120 | { |
| 121 | svm_subregion_t *subregions; /* subregion pool */ |
| 122 | uword *name_hash; |
| 123 | u8 *root_path; |
Dave Wallace | 1929611 | 2017-08-31 15:54:11 -0400 | [diff] [blame] | 124 | int uid; |
| 125 | int gid; |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 126 | } svm_main_region_t; |
| 127 | |
| 128 | |
| 129 | void *svm_region_find_or_create (svm_map_region_args_t * a); |
| 130 | void svm_region_init (void); |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 131 | void svm_region_init_mapped_region (svm_map_region_args_t * a, |
| 132 | svm_region_t * rp); |
Ole Troan | 3cdc25f | 2017-08-17 11:07:33 +0200 | [diff] [blame] | 133 | int svm_region_init_chroot (const char *root_path); |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 134 | void svm_region_init_chroot_uid_gid (const char *root_path, int uid, int gid); |
| 135 | void svm_region_init_args (svm_map_region_args_t * a); |
| 136 | void svm_region_exit (void); |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 137 | void svm_region_exit_client (void); |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 138 | void svm_region_unmap (void *rp_arg); |
Florin Coras | d6c30d9 | 2018-01-29 05:11:24 -0800 | [diff] [blame] | 139 | void svm_region_unmap_client (void *rp_arg); |
Klement Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 140 | void svm_client_scan (const char *root_path); |
| 141 | void svm_client_scan_this_region_nolock (svm_region_t * rp); |
| 142 | u8 *shm_name_from_svm_map_region_args (svm_map_region_args_t * a); |
| 143 | u8 *format_svm_region (u8 * s, va_list * args); |
| 144 | |
| 145 | svm_region_t *svm_get_root_rp (void); |
| 146 | |
| 147 | #endif /* __included_svm_common_h__ */ |
| 148 | |
| 149 | /* |
| 150 | * fd.io coding-style-patch-verification: ON |
| 151 | * |
| 152 | * Local Variables: |
| 153 | * eval: (c-set-style "gnu") |
| 154 | * End: |
| 155 | */ |