1 | /* |
---|
2 | * mapper.c - Map memory, file or device in process virtual address space. |
---|
3 | * |
---|
4 | * Authors Mohamed Lamine Karaoui (2015) |
---|
5 | * Alain Greiner (2016) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #include <kernel_config.h> |
---|
26 | #include <hal_types.h> |
---|
27 | #include <hal_special.h> |
---|
28 | #include <hal_uspace.h> |
---|
29 | #include <grdxt.h> |
---|
30 | #include <rwlock.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <memcpy.h> |
---|
33 | #include <thread.h> |
---|
34 | #include <core.h> |
---|
35 | #include <process.h> |
---|
36 | #include <kmem.h> |
---|
37 | #include <kcm.h> |
---|
38 | #include <page.h> |
---|
39 | #include <cluster.h> |
---|
40 | #include <vfs.h> |
---|
41 | #include <mapper.h> |
---|
42 | |
---|
43 | ////////////////////////////////////////////// |
---|
44 | mapper_t * mapper_create( vfs_fs_type_t type ) |
---|
45 | { |
---|
46 | mapper_t * mapper; |
---|
47 | kmem_req_t req; |
---|
48 | error_t error; |
---|
49 | |
---|
50 | // allocate memory for associated mapper |
---|
51 | req.type = KMEM_MAPPER; |
---|
52 | req.size = sizeof(mapper_t); |
---|
53 | req.flags = AF_KERNEL | AF_ZERO; |
---|
54 | mapper = (mapper_t *)kmem_alloc( &req ); |
---|
55 | |
---|
56 | if( mapper == NULL ) |
---|
57 | { |
---|
58 | printk("\n[ERROR] in %s : no memory for mapper descriptor\n", __FUNCTION__ ); |
---|
59 | return NULL; |
---|
60 | } |
---|
61 | |
---|
62 | // initialize refcount & inode |
---|
63 | mapper->refcount = 0; |
---|
64 | mapper->inode = NULL; |
---|
65 | |
---|
66 | // initialize radix tree |
---|
67 | error = grdxt_init( &mapper->radix, |
---|
68 | CONFIG_VMM_GRDXT_W1, |
---|
69 | CONFIG_VMM_GRDXT_W2, |
---|
70 | CONFIG_VMM_GRDXT_W3 ); |
---|
71 | |
---|
72 | if( error ) |
---|
73 | { |
---|
74 | printk("\n[ERROR] in %s : cannot initialize radix tree\n", __FUNCTION__ ); |
---|
75 | req.type = KMEM_MAPPER; |
---|
76 | req.ptr = mapper; |
---|
77 | kmem_free( &req ); |
---|
78 | return NULL; |
---|
79 | } |
---|
80 | |
---|
81 | // initialize mapper type |
---|
82 | mapper->type = type; |
---|
83 | |
---|
84 | // initialize mapper lock |
---|
85 | rwlock_init( &mapper->lock ); |
---|
86 | |
---|
87 | // initialize waiting threads xlist (empty) |
---|
88 | xlist_root_init( XPTR( local_cxy , &mapper->wait_root ) ); |
---|
89 | |
---|
90 | // initialize vsegs xlist (empty) |
---|
91 | xlist_root_init( XPTR( local_cxy , &mapper->vsegs_root ) ); |
---|
92 | |
---|
93 | return mapper; |
---|
94 | |
---|
95 | } // end mapper_create() |
---|
96 | |
---|
97 | /////////////////////////////////////////// |
---|
98 | error_t mapper_destroy( mapper_t * mapper ) |
---|
99 | { |
---|
100 | page_t * page; |
---|
101 | uint32_t found_index = 0; |
---|
102 | uint32_t start_index = 0; |
---|
103 | kmem_req_t req; |
---|
104 | error_t error; |
---|
105 | |
---|
106 | // scan radix three and release all registered pages to PPM |
---|
107 | do |
---|
108 | { |
---|
109 | // get page from radix tree |
---|
110 | page = (page_t *)grdxt_get_first( &mapper->radix , start_index , &found_index ); |
---|
111 | |
---|
112 | if( page != NULL ) |
---|
113 | { |
---|
114 | // remove page from mapper and release to PPM |
---|
115 | error = mapper_release_page( mapper , page ); |
---|
116 | |
---|
117 | if ( error ) return error; |
---|
118 | |
---|
119 | // update start_key value for next page |
---|
120 | start_index = found_index; |
---|
121 | } |
---|
122 | } |
---|
123 | while( page != NULL ); |
---|
124 | |
---|
125 | // release the memory allocated to radix-tree itself |
---|
126 | grdxt_destroy( &mapper->radix ); |
---|
127 | |
---|
128 | // release memory for mapper descriptor |
---|
129 | req.type = KMEM_MAPPER; |
---|
130 | req.ptr = mapper; |
---|
131 | kmem_free( &req ); |
---|
132 | |
---|
133 | return 0; |
---|
134 | |
---|
135 | } // end mapper_destroy() |
---|
136 | |
---|
137 | //////////////////////////////////////////// |
---|
138 | page_t * mapper_get_page( mapper_t * mapper, |
---|
139 | uint32_t index ) |
---|
140 | { |
---|
141 | kmem_req_t req; |
---|
142 | page_t * page; |
---|
143 | error_t error; |
---|
144 | |
---|
145 | mapper_dmsg("\n[INFO] %s : enters for page %d in mapper %x\n", |
---|
146 | __FUNCTION__ , index , mapper ); |
---|
147 | |
---|
148 | thread_t * this = CURRENT_THREAD; |
---|
149 | |
---|
150 | // take mapper lock in READ_MODE |
---|
151 | rwlock_rd_lock( &mapper->lock ); |
---|
152 | |
---|
153 | // search page in radix tree |
---|
154 | page = (page_t *)grdxt_lookup( &mapper->radix , index ); |
---|
155 | |
---|
156 | // test if page available in mapper |
---|
157 | if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) ) // page not available |
---|
158 | { |
---|
159 | |
---|
160 | // release the lock in READ_MODE and take it in WRITE_MODE |
---|
161 | rwlock_rd_unlock( &mapper->lock ); |
---|
162 | rwlock_wr_lock( &mapper->lock ); |
---|
163 | |
---|
164 | // second test on missing page because the page status can have been modified |
---|
165 | // by another thread, when passing from READ_MODE to WRITE_MODE. |
---|
166 | // from this point there is no concurrent accesses to mapper. |
---|
167 | |
---|
168 | page = grdxt_lookup( &mapper->radix , index ); |
---|
169 | |
---|
170 | if ( page == NULL ) // missing page => create it and load it from file system |
---|
171 | { |
---|
172 | mapper_dmsg("\n[INFO] %s : missing page => load from device\n", __FUNCTION__ ); |
---|
173 | |
---|
174 | // allocate one page from PPM |
---|
175 | req.type = KMEM_PAGE; |
---|
176 | req.size = 0; |
---|
177 | req.flags = AF_NONE; |
---|
178 | page = kmem_alloc( &req ); |
---|
179 | |
---|
180 | if( page == NULL ) |
---|
181 | { |
---|
182 | printk("\n[ERROR] in %s : thread %x cannot allocate a page in cluster %x\n", |
---|
183 | __FUNCTION__ , this->trdid , local_cxy ); |
---|
184 | rwlock_wr_unlock( &mapper->lock ); |
---|
185 | return NULL; |
---|
186 | } |
---|
187 | |
---|
188 | // initialize the page descriptor |
---|
189 | page_init( page ); |
---|
190 | page_set_flag( page , PG_INIT ); |
---|
191 | page_set_flag( page , PG_INLOAD ); |
---|
192 | page_refcount_up( page ); |
---|
193 | page->mapper = mapper; |
---|
194 | page->index = index; |
---|
195 | |
---|
196 | // insert page in mapper radix tree |
---|
197 | error = grdxt_insert( &mapper->radix, index , page ); |
---|
198 | |
---|
199 | // release mapper lock from WRITE_MODE |
---|
200 | rwlock_wr_unlock( &mapper->lock ); |
---|
201 | |
---|
202 | if( error ) |
---|
203 | { |
---|
204 | printk("\n[ERROR] in %s : thread %x cannot insert page in mapper\n", |
---|
205 | __FUNCTION__ , this->trdid ); |
---|
206 | mapper_release_page( mapper , page ); |
---|
207 | page_clear_flag( page , PG_ALL ); |
---|
208 | req.ptr = page; |
---|
209 | req.type = KMEM_PAGE; |
---|
210 | kmem_free(&req); |
---|
211 | return NULL; |
---|
212 | } |
---|
213 | |
---|
214 | // update the mapper and index fields in page descriptor |
---|
215 | // required by the vfs_move_page_to_mapper() |
---|
216 | page->mapper = mapper; |
---|
217 | page->index = index; |
---|
218 | |
---|
219 | // launch I/O operation to load page from file system |
---|
220 | error = vfs_mapper_move_page( page , true ); // to mapper |
---|
221 | |
---|
222 | if( error ) |
---|
223 | { |
---|
224 | printk("\n[ERROR] in %s : thread %x cannot load page from device\n", |
---|
225 | __FUNCTION__ , this->trdid ); |
---|
226 | mapper_release_page( mapper , page ); |
---|
227 | page_clear_flag( page , PG_ALL ); |
---|
228 | req.ptr = page; |
---|
229 | req.type = KMEM_PAGE; |
---|
230 | kmem_free( &req ); |
---|
231 | return NULL; |
---|
232 | } |
---|
233 | |
---|
234 | // reset the page INLOAD flag to make the page available to all readers |
---|
235 | page_clear_flag( page , PG_INLOAD ); |
---|
236 | |
---|
237 | } |
---|
238 | else if( page_is_flag( page , PG_INLOAD ) ) // page is loaded by another thread |
---|
239 | { |
---|
240 | // release mapper lock from WRITE_MODE |
---|
241 | rwlock_wr_unlock( &mapper->lock ); |
---|
242 | |
---|
243 | // deschedule to wait load completion |
---|
244 | while( 1 ) |
---|
245 | { |
---|
246 | // exit waiting loop when loaded |
---|
247 | if( page_is_flag( page , PG_INLOAD ) ) break; |
---|
248 | |
---|
249 | // deschedule |
---|
250 | sched_yield( NULL ); |
---|
251 | } |
---|
252 | } |
---|
253 | } |
---|
254 | else // page available in mapper |
---|
255 | { |
---|
256 | |
---|
257 | rwlock_rd_unlock( &mapper->lock ); |
---|
258 | } |
---|
259 | |
---|
260 | mapper_dmsg("\n[INFO] %s : exit for page %d in mapper %x / page_desc = %x\n", |
---|
261 | __FUNCTION__ , index , mapper , page ); |
---|
262 | |
---|
263 | return page; |
---|
264 | |
---|
265 | } // end mapper_get_page() |
---|
266 | |
---|
267 | /////////////////////////////////////////////// |
---|
268 | error_t mapper_release_page( mapper_t * mapper, |
---|
269 | page_t * page ) |
---|
270 | { |
---|
271 | error_t error; |
---|
272 | |
---|
273 | // lauch IO operation to update page to file system |
---|
274 | error = vfs_mapper_move_page( page , false ); // from mapper |
---|
275 | |
---|
276 | if( error ) |
---|
277 | { |
---|
278 | printk("\n[ERROR] in %s : cannot update file system\n", __FUNCTION__ ); |
---|
279 | return EIO; |
---|
280 | } |
---|
281 | |
---|
282 | // take mapper lock in WRITE_MODE |
---|
283 | rwlock_wr_lock( &mapper->lock ); |
---|
284 | |
---|
285 | // remove physical page from radix tree |
---|
286 | grdxt_remove( &mapper->radix , page->index ); |
---|
287 | |
---|
288 | // release mapper lock from WRITE_MODE |
---|
289 | rwlock_wr_unlock( &mapper->lock ); |
---|
290 | |
---|
291 | // release page to PPM |
---|
292 | kmem_req_t req; |
---|
293 | req.type = KMEM_PAGE; |
---|
294 | req.ptr = page; |
---|
295 | kmem_free( &req ); |
---|
296 | |
---|
297 | return 0; |
---|
298 | |
---|
299 | } // end mapper_release_page() |
---|
300 | |
---|
301 | /////////////////////////////////////////////////// |
---|
302 | error_t mapper_move_user( mapper_t * mapper, |
---|
303 | bool_t to_buffer, |
---|
304 | uint32_t file_offset, |
---|
305 | void * buffer, |
---|
306 | uint32_t size ) |
---|
307 | { |
---|
308 | uint32_t page_offset; // first byte to move to/from a mapper page |
---|
309 | uint32_t page_count; // number of bytes to move to/from a mapper page |
---|
310 | uint32_t index; // current mapper page index |
---|
311 | uint32_t done; // number of moved bytes |
---|
312 | page_t * page; // current mapper page descriptor |
---|
313 | uint8_t * map_ptr; // current mapper address |
---|
314 | uint8_t * buf_ptr; // current buffer address |
---|
315 | |
---|
316 | mapper_dmsg("\n[INFO] %s : enters / to_buf = %d / buffer = %x\n", |
---|
317 | __FUNCTION__ , to_buffer , buffer ); |
---|
318 | |
---|
319 | // compute offsets of first and last bytes in file |
---|
320 | uint32_t min_byte = file_offset; |
---|
321 | uint32_t max_byte = file_offset + size -1; |
---|
322 | |
---|
323 | // compute indexes of pages for first and last byte in mapper |
---|
324 | uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
325 | uint32_t last = max_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
326 | |
---|
327 | done = 0; |
---|
328 | |
---|
329 | // loop on pages in mapper |
---|
330 | for( index = first ; index <= last ; index++ ) |
---|
331 | { |
---|
332 | // compute page_offset |
---|
333 | if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK; |
---|
334 | else page_offset = 0; |
---|
335 | |
---|
336 | // compute number of bytes in page |
---|
337 | if ( first == last ) page_count = size; |
---|
338 | else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset; |
---|
339 | else if ( index == last ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1; |
---|
340 | else page_count = CONFIG_PPM_PAGE_SIZE; |
---|
341 | |
---|
342 | mapper_dmsg("\n[INFO] %s : index = %d / offset = %d / count = %d\n", |
---|
343 | __FUNCTION__ , index , page_offset , page_count ); |
---|
344 | |
---|
345 | // get page descriptor |
---|
346 | page = mapper_get_page( mapper , index ); |
---|
347 | |
---|
348 | if ( page == NULL ) return EINVAL; |
---|
349 | |
---|
350 | // compute pointer in mapper |
---|
351 | xptr_t base_xp = ppm_page2base( XPTR( local_cxy, page ) ); |
---|
352 | map_ptr = (uint8_t *)GET_PTR( base_xp ) + page_offset; |
---|
353 | |
---|
354 | // compute pointer in buffer |
---|
355 | buf_ptr = (uint8_t *)buffer + done; |
---|
356 | |
---|
357 | mapper_dmsg("\n[INFO] %s : index = %d / buf_ptr = %x / map_ptr = %x\n", |
---|
358 | __FUNCTION__ , index , buf_ptr , map_ptr ); |
---|
359 | |
---|
360 | // move fragment |
---|
361 | if( to_buffer ) |
---|
362 | { |
---|
363 | hal_copy_to_uspace( buf_ptr , map_ptr , page_count ); |
---|
364 | } |
---|
365 | else |
---|
366 | { |
---|
367 | page_do_dirty( page ); |
---|
368 | hal_copy_from_uspace( map_ptr , buf_ptr , page_count ); |
---|
369 | } |
---|
370 | |
---|
371 | done += page_count; |
---|
372 | } |
---|
373 | |
---|
374 | mapper_dmsg("\n[INFO] %s : exit for buffer %x\n", |
---|
375 | __FUNCTION__, buffer ); |
---|
376 | |
---|
377 | return 0; |
---|
378 | |
---|
379 | } // end mapper_move_user() |
---|
380 | |
---|
381 | //////////////////////////////////////////////// |
---|
382 | error_t mapper_move_kernel( mapper_t * mapper, |
---|
383 | bool_t to_buffer, |
---|
384 | uint32_t file_offset, |
---|
385 | xptr_t buffer_xp, |
---|
386 | uint32_t size ) |
---|
387 | { |
---|
388 | uint32_t page_offset; // first byte to move to/from a mapper page |
---|
389 | uint32_t page_count; // number of bytes to move to/from a mapper page |
---|
390 | uint32_t index; // current mapper page index |
---|
391 | uint32_t done; // number of moved bytes |
---|
392 | page_t * page; // current mapper page descriptor |
---|
393 | |
---|
394 | uint8_t * src_ptr; // source buffer local pointer |
---|
395 | cxy_t src_cxy; // source cluster |
---|
396 | uint8_t * dst_ptr; // destination buffer local pointer |
---|
397 | cxy_t dst_cxy; // destination cluster |
---|
398 | |
---|
399 | mapper_dmsg("\n[INFO] %s : enters / to_buf = %d / buffer = %l / size = %x / cycle %d\n", |
---|
400 | __FUNCTION__ , to_buffer , buffer_xp , size , hal_time_stamp() ); |
---|
401 | |
---|
402 | // compute offsets of first and last bytes in file |
---|
403 | uint32_t min_byte = file_offset; |
---|
404 | uint32_t max_byte = file_offset + size -1; |
---|
405 | |
---|
406 | // compute indexes for first and last pages in mapper |
---|
407 | uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
408 | uint32_t last = max_byte >> CONFIG_PPM_PAGE_SHIFT; |
---|
409 | |
---|
410 | // get buffer cluster and local pointer |
---|
411 | cxy_t buffer_cxy = GET_CXY( buffer_xp ); |
---|
412 | uint8_t * buffer_ptr = (uint8_t *)GET_PTR( buffer_xp ); |
---|
413 | |
---|
414 | // compute source and destination clusters |
---|
415 | if( to_buffer ) |
---|
416 | { |
---|
417 | dst_cxy = buffer_cxy; |
---|
418 | src_cxy = local_cxy; |
---|
419 | } |
---|
420 | else |
---|
421 | { |
---|
422 | src_cxy = buffer_cxy; |
---|
423 | dst_cxy = local_cxy; |
---|
424 | } |
---|
425 | |
---|
426 | done = 0; |
---|
427 | |
---|
428 | // loop on pages in mapper |
---|
429 | for( index = first ; index <= last ; index++ ) |
---|
430 | { |
---|
431 | // compute page_offset |
---|
432 | if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK; |
---|
433 | else page_offset = 0; |
---|
434 | |
---|
435 | // compute number of bytes to move in page |
---|
436 | if ( first == last ) page_count = size; |
---|
437 | else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset; |
---|
438 | else if ( index == last ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1; |
---|
439 | else page_count = CONFIG_PPM_PAGE_SIZE; |
---|
440 | |
---|
441 | mapper_dmsg("\n[INFO] %s : page_index = %d / offset = %d / count = %d\n", |
---|
442 | __FUNCTION__ , index , page_offset , page_count ); |
---|
443 | |
---|
444 | // get page descriptor |
---|
445 | page = mapper_get_page( mapper , index ); |
---|
446 | |
---|
447 | if ( page == NULL ) return EINVAL; |
---|
448 | |
---|
449 | // get page base address |
---|
450 | uint8_t * base = (uint8_t *)GET_PTR( XPTR( local_cxy , page ) ); |
---|
451 | |
---|
452 | // compute source and destination pointers |
---|
453 | if( to_buffer ) |
---|
454 | { |
---|
455 | dst_ptr = buffer_ptr + done; |
---|
456 | src_ptr = base + page_offset; |
---|
457 | } |
---|
458 | else |
---|
459 | { |
---|
460 | src_ptr = buffer_ptr + done; |
---|
461 | dst_ptr = base + page_offset; |
---|
462 | |
---|
463 | page_do_dirty( page ); |
---|
464 | } |
---|
465 | |
---|
466 | mapper_dmsg("\n[INFO] %s : index = %d / buf_ptr = %x / map_ptr = %x\n", |
---|
467 | __FUNCTION__ , index , buf_ptr , map_ptr ); |
---|
468 | |
---|
469 | // move fragment |
---|
470 | hal_remote_memcpy( XPTR( dst_cxy , dst_ptr ), XPTR( src_cxy , src_ptr ), page_count ); |
---|
471 | |
---|
472 | done += page_count; |
---|
473 | } |
---|
474 | |
---|
475 | mapper_dmsg("\n[INFO] %s : exit for buffer %l / size = %x / cycle %d\n", |
---|
476 | __FUNCTION__ , buffer_xp , size , hal_time_stamp() ); |
---|
477 | |
---|
478 | return 0; |
---|
479 | |
---|
480 | } // end mapper_move_kernel_buffer() |
---|
481 | |
---|