source: trunk/kernel/mm/mapper.c @ 140

Last change on this file since 140 was 53, checked in by alain, 7 years ago

Compilation OK pout TSAR

File size: 10.3 KB
Line 
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 <thread.h>
33#include <core.h>
34#include <process.h>
35#include <kmem.h>
36#include <kcm.h>
37#include <page.h>
38#include <cluster.h>
39#include <vfs.h>
40#include <mapper.h>
41
42//////////////////////////
43mapper_t * mapper_create()
44{
45    mapper_t * mapper;
46    kmem_req_t req;
47    error_t    error;
48
49    // allocate memory for associated mapper
50        req.type  = KMEM_MAPPER;
51        req.size  = sizeof(mapper_t);
52    req.flags = AF_KERNEL | AF_ZERO;
53        mapper    = (mapper_t *)kmem_alloc( &req );
54
55    if( mapper == NULL )
56    {
57        printk("\n[ERROR] in %s : no memory for mapper descriptor\n", __FUNCTION__ );
58        return NULL;
59    }
60
61    // initialize refcount & inode
62        mapper->refcount = 0;
63    mapper->inode    = NULL;
64
65    // initialize radix tree
66        error = grdxt_init( &mapper->radix,
67                        CONFIG_VMM_GRDXT_W1,
68                        CONFIG_VMM_GRDXT_W2,
69                        CONFIG_VMM_GRDXT_W3 );
70
71    if( error )
72    {
73        printk("\n[ERROR] in %s : cannot initialize radix tree\n", __FUNCTION__ );
74            req.type  = KMEM_MAPPER;
75        req.ptr   = mapper;
76        kmem_free( &req );
77        return NULL;
78    }
79
80    // initialize mapper lock
81        rwlock_init(  &mapper->lock );
82
83    // initialize waiting threads xlist (empty)
84        xlist_root_init( XPTR( local_cxy , &mapper->wait_root ) );
85
86    // initialize vsegs xlist (empty)
87        xlist_root_init( XPTR( local_cxy , &mapper->vsegs_root ) );
88
89    return mapper;
90
91} // end mapper_create()
92
93///////////////////////////////////////////
94error_t mapper_destroy( mapper_t * mapper )
95{
96    page_t   * page;
97    uint32_t   found_index = 0;
98    uint32_t   start_index = 0;
99    kmem_req_t req;
100    error_t    error;
101
102    // scan radix three and release all registered pages to PPM
103    do
104    {
105        // get page from radix tree
106        page = (page_t *)grdxt_get_first( &mapper->radix , start_index , &found_index );
107
108        if( page != NULL )
109        {
110            // remove page from mapper and release to PPM
111                error = mapper_release_page( mapper , page );
112
113            if ( error ) return error;
114
115            // update start_key value for next page
116            start_index = found_index;
117        }
118    }
119    while( page != NULL );
120
121    // release the memory allocated to radix-tree itself
122    grdxt_destroy( &mapper->radix );
123
124    // release memory for mapper descriptor
125    req.type = KMEM_MAPPER;
126    req.ptr  = mapper;
127    kmem_free( &req );
128
129    return 0;
130
131}  // end mapper_destroy()
132
133
134////////////////////////////////////////////
135page_t * mapper_get_page( mapper_t * mapper,
136                          uint32_t   index )
137{
138        kmem_req_t    req;
139        page_t      * page;
140        error_t       error;
141
142    thread_t * this = CURRENT_THREAD;
143
144    // take mapper lock in READ_MODE
145    rwlock_rd_lock( &mapper->lock );
146
147    // search page in radix tree
148    page = (page_t *)grdxt_lookup( &mapper->radix , index );
149
150    // test if page available in mapper
151    if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) )  // page not available
152    {
153        // release the lock in READ_MODE and take it in WRITE_MODE
154        rwlock_rd_unlock( &mapper->lock );
155        rwlock_wr_lock( &mapper->lock );
156
157        // second test on missing page because the page status can have been modified
158        // by another thread, when passing from READ_MODE to WRITE_MODE.
159        // from this point there is no concurrent accesses to mapper.
160
161        page = grdxt_lookup( &mapper->radix , index );
162
163        if ( page == NULL )   // missing page => load it from file system
164        {
165            // allocate one page from PPM
166            req.type  = KMEM_PAGE;
167            req.size  = 0;
168            req.flags = AF_NONE;
169            page = kmem_alloc( &req );
170
171            if( page == NULL )
172            {
173                printk("\n[ERROR] in %s : thread %x cannot allocate a page in cluster %x\n",
174                       __FUNCTION__ , this->trdid , local_cxy );
175                rwlock_wr_unlock( &mapper->lock );
176                return NULL;
177            }
178
179            // initialize the page descriptor
180            page_init( page );
181            page_set_flag( page , PG_INIT );
182            page_set_flag( page , PG_INLOAD );
183            page_refcount_up( page );
184            page->mapper = mapper;
185            page->index  = index;
186
187            // insert page in mapper radix tree
188            error = grdxt_insert( &mapper->radix, index , page );
189
190            // release mapper lock from WRITE_MODE
191            rwlock_wr_unlock( &mapper->lock );
192
193            if( error )
194            {
195                printk("\n[ERROR] in %s : thread %x cannot insert page in mapper\n",
196                       __FUNCTION__ , this->trdid );
197                mapper_release_page( mapper , page );
198                page_clear_flag( page , PG_ALL );
199                req.ptr  = page;
200                req.type = KMEM_PAGE;
201                kmem_free(&req);
202                return NULL;
203            }
204
205            // launch I/O operation to load page from file system
206            error = vfs_move_page_to_mapper( page );
207
208            if( error )
209            {
210                printk("\n[ERROR] in %s : thread %x cannot load page from device\n",
211                       __FUNCTION__ , this->trdid );
212                mapper_release_page( mapper , page );
213                page_clear_flag( page , PG_ALL );
214                req.ptr  = page;
215                req.type = KMEM_PAGE;
216                kmem_free( &req );
217                return NULL;
218            }
219
220            // update the mapper and index fields in page descriptor
221            page->mapper = mapper;
222            page->index  = index;
223
224            // reset the page INLOAD flag to make the page available to all readers
225            page_clear_flag( page , PG_INLOAD );
226
227        }
228        else if( page_is_flag( page , PG_INLOAD ) )   // page is loaded by another thread
229        {
230            // release mapper lock from WRITE_MODE
231            rwlock_wr_unlock( &mapper->lock );
232
233            // deschedule to wait load completion
234            while( 1 )
235            {
236                // exit waiting loop when loaded
237                if(  page_is_flag( page , PG_INLOAD ) ) break;
238
239                // deschedule
240                sched_yield();
241            }
242
243        }
244
245        return page;
246    }
247    else
248    {
249         // release lock from READ_MODE
250         rwlock_rd_unlock( &mapper->lock );
251
252         return page;
253    }
254}  // end mapper_get_page()
255
256///////////////////////////////////////////////
257error_t mapper_release_page( mapper_t * mapper,
258                             page_t   * page )
259{
260    error_t error;
261
262    // lauch IO operation to update page to file system
263    error = vfs_move_page_from_mapper( page );
264
265    if( error )
266    {
267        printk("\n[ERROR] in %s : cannot update file system\n", __FUNCTION__ );
268        return EIO;
269    }
270
271    // take mapper lock in WRITE_MODE
272    rwlock_wr_lock( &mapper->lock );
273
274    // remove physical page from radix tree
275        grdxt_remove( &mapper->radix , page->index );
276
277    // release mapper lock from WRITE_MODE
278    rwlock_wr_unlock( &mapper->lock );
279
280    // release page to PPM
281        kmem_req_t   req;
282        req.type  = KMEM_PAGE;
283    req.ptr   = page;
284    kmem_free( &req );
285
286    return 0;
287
288}  // end mapper_release_page()
289
290/////////////////////////////////////////
291error_t mapper_move( mapper_t  *  mapper,
292                     bool_t       to_buffer,
293                     uint32_t     file_offset,
294                     void      *  buffer,
295                     uint32_t     size )
296{
297    uint32_t   page_offset;    // first byte to move to/from a mapper page
298    uint32_t   page_count;     // number of bytes to move to/from a mapper page
299    uint32_t   index;          // current mapper page index
300    uint32_t   done;           // number of moved bytes
301    page_t   * page;           // current mapper page descriptor
302    uint8_t  * map_ptr;        // current mapper  address
303    uint8_t  * buf_ptr;        // current buffer  address
304
305    // compute offsets of first and last bytes in file
306    uint32_t min_byte = file_offset;
307    uint32_t max_byte = file_offset + size -1;
308
309    // compute indexes of pages for first and last byte in mapper
310    uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT;
311    uint32_t last  = max_byte >> CONFIG_PPM_PAGE_SHIFT;
312
313    done = 0;
314
315    // loop on pages in mapper
316    for( index = first ; index <= last ; index++ )
317    {
318        // compute page_offset
319        if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
320        else                 page_offset = 0;
321
322        // compute page_count
323        if      ( first == last  ) page_count = size;
324        else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset;
325        else if ( index == last  ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1;
326        else                       page_count = CONFIG_PPM_PAGE_SIZE;
327
328        // get page descriptor
329        page = mapper_get_page( mapper , index );
330
331        if ( page == NULL ) return EINVAL;
332
333        // compute pointer in mapper
334        map_ptr = (uint8_t *)ppm_page2vaddr( page ) + page_offset;
335
336        // compute pointer in buffer
337        buf_ptr = (uint8_t *)buffer + done;
338
339        // move fragment
340        if( to_buffer )
341        {
342            hal_copy_to_uspace( buf_ptr , map_ptr , page_count );
343        }
344        else
345        {
346            page_do_dirty( page );
347            hal_copy_from_uspace( map_ptr , buf_ptr , page_count );
348        }
349
350        done += page_count;
351    }
352
353    return 0;
354
355}  // end mapper_move()
356
357
358
Note: See TracBrowser for help on using the repository browser.