1 | /* |
---|
2 | * kmem.c - kernel memory allocator implementation. |
---|
3 | * |
---|
4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
5 | * Mohamed Lamine Karaoui (2015) |
---|
6 | * Alain Greiner (2016) |
---|
7 | * |
---|
8 | * Copyright (c) UPMC Sorbonne Universites |
---|
9 | * |
---|
10 | * This file is part of ALMOS-MKH. |
---|
11 | * |
---|
12 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
13 | * under the terms of the GNU General Public License as published by |
---|
14 | * the Free Software Foundation; version 2.0 of the License. |
---|
15 | * |
---|
16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | * General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU General Public License |
---|
22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include <kernel_config.h> |
---|
27 | #include <hal_types.h> |
---|
28 | #include <hal_special.h> |
---|
29 | #include <printk.h> |
---|
30 | #include <spinlock.h> |
---|
31 | #include <readlock.h> |
---|
32 | #include <memcpy.h> |
---|
33 | #include <khm.h> |
---|
34 | #include <ppm.h> |
---|
35 | #include <page.h> |
---|
36 | #include <cluster.h> |
---|
37 | #include <thread.h> |
---|
38 | #include <process.h> |
---|
39 | #include <chdev.h> |
---|
40 | #include <mapper.h> |
---|
41 | #include <vfs.h> |
---|
42 | #include <fatfs.h> |
---|
43 | #include <ramfs.h> |
---|
44 | #include <remote_sem.h> |
---|
45 | #include <remote_barrier.h> |
---|
46 | #include <remote_mutex.h> |
---|
47 | #include <remote_condvar.h> |
---|
48 | #include <mapper.h> |
---|
49 | #include <grdxt.h> |
---|
50 | #include <vseg.h> |
---|
51 | #include <kmem.h> |
---|
52 | |
---|
53 | /////////////////////////// |
---|
54 | void kmem_print_kcm_table() |
---|
55 | { |
---|
56 | uint32_t index; |
---|
57 | kcm_t * kcm; |
---|
58 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
59 | |
---|
60 | printk("\n *** KCM Pointers Table ***\n"); |
---|
61 | |
---|
62 | for( index = 0 ; index < KMEM_TYPES_NR ; index++ ) |
---|
63 | { |
---|
64 | kcm = cluster->kcm_tbl[index]; |
---|
65 | if( kcm != NULL ) |
---|
66 | { |
---|
67 | if( index == kcm->type ) |
---|
68 | { |
---|
69 | printk(" - KCM[%s] (at address %x) is OK\n", |
---|
70 | kmem_type_str( index ) , (intptr_t)kcm ); |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | printk(" - KCM[%s] (at address %x) is KO : has type %s\n", |
---|
75 | kmem_type_str( index ) , (intptr_t)kcm , kmem_type_str( kcm->type ) ); |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | ///////////////////////////////////////// |
---|
82 | uint32_t kmem_type_size( uint32_t type ) |
---|
83 | { |
---|
84 | if ( type == KMEM_PAGE ) return CONFIG_PPM_PAGE_SIZE; |
---|
85 | else if( type == KMEM_GENERIC ) return 0; |
---|
86 | else if( type == KMEM_KCM ) return sizeof( kcm_t ); |
---|
87 | else if( type == KMEM_VSEG ) return sizeof( vseg_t ); |
---|
88 | else if( type == KMEM_DEVICE ) return sizeof( chdev_t ); |
---|
89 | else if( type == KMEM_MAPPER ) return sizeof( mapper_t ); |
---|
90 | else if( type == KMEM_PROCESS ) return sizeof( process_t ); |
---|
91 | else if( type == KMEM_CPU_CTX ) return CONFIG_CPU_CTX_SIZE; |
---|
92 | else if( type == KMEM_FPU_CTX ) return CONFIG_FPU_CTX_SIZE; |
---|
93 | else if( type == KMEM_BARRIER ) return sizeof( remote_barrier_t ); |
---|
94 | |
---|
95 | else if( type == KMEM_DEVFS_CTX ) return sizeof( fatfs_ctx_t ); |
---|
96 | else if( type == KMEM_FATFS_CTX ) return sizeof( fatfs_ctx_t ); |
---|
97 | else if( type == KMEM_VFS_CTX ) return sizeof( vfs_ctx_t ); |
---|
98 | else if( type == KMEM_VFS_INODE ) return sizeof( vfs_inode_t ); |
---|
99 | else if( type == KMEM_VFS_DENTRY ) return sizeof( vfs_dentry_t ); |
---|
100 | else if( type == KMEM_VFS_FILE ) return sizeof( vfs_file_t ); |
---|
101 | else if( type == KMEM_SEM ) return sizeof( remote_sem_t ); |
---|
102 | else if( type == KMEM_CONDVAR ) return sizeof( remote_condvar_t ); |
---|
103 | else if( type == KMEM_MUTEX ) return sizeof( remote_mutex_t ); |
---|
104 | else if( type == KMEM_512_BYTES ) return 512; |
---|
105 | |
---|
106 | else return 0; |
---|
107 | } |
---|
108 | |
---|
109 | ///////////////////////////////////// |
---|
110 | char * kmem_type_str( uint32_t type ) |
---|
111 | { |
---|
112 | if ( type == KMEM_PAGE ) return "KMEM_PAGE"; |
---|
113 | else if( type == KMEM_GENERIC ) return "KMEM_GENERIC"; |
---|
114 | else if( type == KMEM_KCM ) return "KMEM_KCM"; |
---|
115 | else if( type == KMEM_VSEG ) return "KMEM_VSEG"; |
---|
116 | else if( type == KMEM_DEVICE ) return "KMEM_DEVICE"; |
---|
117 | else if( type == KMEM_MAPPER ) return "KMEM_MAPPER"; |
---|
118 | else if( type == KMEM_PROCESS ) return "KMEM_PROCESS"; |
---|
119 | else if( type == KMEM_CPU_CTX ) return "KMEM_CPU_CTX"; |
---|
120 | else if( type == KMEM_FPU_CTX ) return "KMEM_FPU_CTX"; |
---|
121 | else if( type == KMEM_BARRIER ) return "KMEM_BARRIER"; |
---|
122 | |
---|
123 | else if( type == KMEM_DEVFS_CTX ) return "KMEM_DEVFS_CTX"; |
---|
124 | else if( type == KMEM_FATFS_CTX ) return "KMEM_FATFS_CTX"; |
---|
125 | else if( type == KMEM_VFS_CTX ) return "KMEM_VFS_CTX"; |
---|
126 | else if( type == KMEM_VFS_INODE ) return "KMEM_VFS_INODE"; |
---|
127 | else if( type == KMEM_VFS_DENTRY ) return "KMEM_VFS_DENTRY"; |
---|
128 | else if( type == KMEM_VFS_FILE ) return "KMEM_VFS_FILE"; |
---|
129 | else if( type == KMEM_SEM ) return "KMEM_SEM"; |
---|
130 | else if( type == KMEM_CONDVAR ) return "KMEM_CONDVAR"; |
---|
131 | else if( type == KMEM_MUTEX ) return "KMEM_MUTEX"; |
---|
132 | else if( type == KMEM_512_BYTES ) return "KMEM_512_BYTES"; |
---|
133 | |
---|
134 | else return "undefined"; |
---|
135 | } |
---|
136 | |
---|
137 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
138 | // This static function dynamically allocates and initializes a specific KCM allocator. |
---|
139 | // It uses the KCM allocator embedded in cluster manager, initialized by cluster_init(). |
---|
140 | ///////////////////////////////////////////////////////////////////////////////////////////// |
---|
141 | static error_t kmem_create_kcm( uint32_t type ) |
---|
142 | { |
---|
143 | kcm_t * kcm; |
---|
144 | |
---|
145 | assert( ((type > 1) && (type < KMEM_TYPES_NR) ) , __FUNCTION__ , "illegal KCM type" ); |
---|
146 | |
---|
147 | kmem_dmsg("\n[DBG] %s : enters / KCM type %s missing in cluster %x\n", |
---|
148 | __FUNCTION__ , kmem_type_str( type ) , local_cxy ); |
---|
149 | |
---|
150 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
151 | |
---|
152 | // allocate memory for the requested KCM allocator |
---|
153 | // from the KCM allocator embedded in cluster descriptor |
---|
154 | kcm = kcm_alloc( &cluster->kcm ); |
---|
155 | |
---|
156 | if( kcm == NULL ) |
---|
157 | { |
---|
158 | printk("\n[ERROR] in %s : failed to create KCM type %d in cluster %x\n", |
---|
159 | __FUNCTION__ , type , local_cxy ); |
---|
160 | return ENOMEM; |
---|
161 | } |
---|
162 | |
---|
163 | // initialize the new KCM allocator |
---|
164 | kcm_init( kcm , type ); |
---|
165 | |
---|
166 | // register it if the KCM pointers Table |
---|
167 | cluster->kcm_tbl[type] = kcm; |
---|
168 | |
---|
169 | hal_fence(); |
---|
170 | |
---|
171 | kmem_dmsg("\n[DBG] %s : exit / KCM type %s created in cluster %x\n", |
---|
172 | __FUNCTION__ , kmem_type_str( type ) , local_cxy ); |
---|
173 | |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | ///////////////////////////////////// |
---|
178 | void * kmem_alloc( kmem_req_t * req ) |
---|
179 | { |
---|
180 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
181 | |
---|
182 | uint32_t type; |
---|
183 | uint32_t flags; |
---|
184 | uint32_t size; // ln( pages ) if PPM / bytes if KHM / unused if KCM |
---|
185 | void * ptr; // memory buffer if KHM or KCM / page descriptor if PPM |
---|
186 | |
---|
187 | |
---|
188 | type = req->type; |
---|
189 | size = req->size; |
---|
190 | flags = req->flags; |
---|
191 | |
---|
192 | assert( (type < KMEM_TYPES_NR) , __FUNCTION__ , "illegal KMEM request type" ); |
---|
193 | |
---|
194 | kmem_dmsg("\n[DBG] %s : enters in cluster %x for type %s\n", |
---|
195 | __FUNCTION__ , local_cxy , kmem_type_str( type ) ); |
---|
196 | |
---|
197 | // analyse request type |
---|
198 | if( type == KMEM_PAGE ) // PPM allocator |
---|
199 | { |
---|
200 | |
---|
201 | #if CONFIG_DEBUG_KMEM_ALLOC |
---|
202 | if( CONFIG_DEBUG_KMEM_ALLOC < (uint32_t)hal_get_cycles() ) |
---|
203 | printk("\n[DBG] in %s : thread %x enter for %d page(s)\n", |
---|
204 | __FUNCTION__ , CURRENT_THREAD , 1<<size ); |
---|
205 | #endif |
---|
206 | |
---|
207 | // allocate the number of requested pages |
---|
208 | ptr = (void *)ppm_alloc_pages( size ); |
---|
209 | if( ptr == NULL ) |
---|
210 | { |
---|
211 | printk("\n[ERROR] in %s : failed for type %d / size %d in cluster %x\n", |
---|
212 | __FUNCTION__ , type , size , local_cxy ); |
---|
213 | return NULL; |
---|
214 | } |
---|
215 | |
---|
216 | // reset page if requested |
---|
217 | if( flags & AF_ZERO ) page_zero( (page_t *)ptr ); |
---|
218 | |
---|
219 | kmem_dmsg("\n[DBG] %s : exit in cluster %x for type %s / page = %x / base = %x\n", |
---|
220 | __FUNCTION__, local_cxy , kmem_type_str( type ) , |
---|
221 | (intptr_t)ptr , (intptr_t)ppm_page2base( ptr ) ); |
---|
222 | |
---|
223 | #if CONFIG_DEBUG_KMEM_ALLOC |
---|
224 | if( CONFIG_DEBUG_KMEM_ALLOC < (uint32_t)hal_get_cycles() ) |
---|
225 | printk("\n[DBG] in %s : thread %x exit / %d page(s) allocated / ppn = %x\n", |
---|
226 | __FUNCTION__ , CURRENT_THREAD , 1<<size , ppm_page2ppn( XPTR( local_cxy , ptr ) ) ); |
---|
227 | #endif |
---|
228 | |
---|
229 | } |
---|
230 | else if( type == KMEM_GENERIC ) // KHM allocator |
---|
231 | { |
---|
232 | // allocate memory from KHM |
---|
233 | ptr = khm_alloc( &cluster->khm , size ); |
---|
234 | if( ptr == NULL ) |
---|
235 | { |
---|
236 | printk("\n[ERROR] in %s : failed for type %d / size %d in cluster %x\n", |
---|
237 | __FUNCTION__ , type , size , local_cxy ); |
---|
238 | return NULL; |
---|
239 | } |
---|
240 | |
---|
241 | // reset memory if requested |
---|
242 | if( flags & AF_ZERO ) memset( ptr , 0 , size ); |
---|
243 | |
---|
244 | kmem_dmsg("\n[DBG] %s : exit in cluster %x for type %s / base = %x / size = %d\n", |
---|
245 | __FUNCTION__, local_cxy , kmem_type_str( type ) , |
---|
246 | (intptr_t)ptr , req->size ); |
---|
247 | } |
---|
248 | else // KCM allocator |
---|
249 | { |
---|
250 | // initialize the KCM allocator if not already done |
---|
251 | if( cluster->kcm_tbl[type] == NULL ) |
---|
252 | { |
---|
253 | spinlock_lock( &cluster->kcm_lock ); |
---|
254 | error_t error = kmem_create_kcm( type ); |
---|
255 | spinlock_unlock( &cluster->kcm_lock ); |
---|
256 | if ( error ) return NULL; |
---|
257 | } |
---|
258 | |
---|
259 | // allocate memory from KCM |
---|
260 | ptr = kcm_alloc( cluster->kcm_tbl[type] ); |
---|
261 | if( ptr == NULL ) |
---|
262 | { |
---|
263 | printk("\n[ERROR] in %s : failed for type %d / size %d in cluster %x\n", |
---|
264 | __FUNCTION__ , type , size , local_cxy ); |
---|
265 | return NULL; |
---|
266 | } |
---|
267 | |
---|
268 | // reset memory if requested |
---|
269 | if( flags & AF_ZERO ) memset( ptr , 0 , kmem_type_size( type ) ); |
---|
270 | |
---|
271 | kmem_dmsg("\n[DBG] %s : exit in cluster %x for type %s / base = %x / size = %d\n", |
---|
272 | __FUNCTION__, local_cxy , kmem_type_str( type ) , |
---|
273 | (intptr_t)ptr , kmem_type_size( type ) ); |
---|
274 | } |
---|
275 | |
---|
276 | return ptr; |
---|
277 | } |
---|
278 | |
---|
279 | ////////////////////////////////// |
---|
280 | void kmem_free( kmem_req_t * req ) |
---|
281 | { |
---|
282 | if( req->type >= KMEM_TYPES_NR ) |
---|
283 | { |
---|
284 | assert( false , __FUNCTION__ , "illegal request type\n" ); |
---|
285 | } |
---|
286 | |
---|
287 | switch(req->type) |
---|
288 | { |
---|
289 | case KMEM_PAGE: |
---|
290 | ppm_free_pages( (page_t*)req->ptr ); |
---|
291 | return; |
---|
292 | |
---|
293 | case KMEM_GENERIC: |
---|
294 | khm_free( req->ptr ); |
---|
295 | return; |
---|
296 | |
---|
297 | default: |
---|
298 | kcm_free( req->ptr ); |
---|
299 | return; |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|