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

Last change on this file since 212 was 204, checked in by alain, 7 years ago

Bug fix in kernel_init
-This line, and those below, will be ignored--

M params.mk
M kernel_config.h
M Makefile
M hdd/virt_hdd.dmg
M tools/bootloader_tsar/boot.c
M kernel/libk/bits.h
M kernel/libk/elf.c
M kernel/libk/xhtab.c
M kernel/libk/elf.h
M kernel/libk/xhtab.h
M kernel/devices/dev_pic.c
M kernel/mm/vmm.c
M kernel/mm/mapper.c
M kernel/mm/mapper.h
M kernel/vfs/devfs.h
M kernel/vfs/vfs.c
M kernel/vfs/vfs.h
M kernel/vfs/devfs.c
M kernel/kern/chdev.h
M kernel/kern/kernel_init.c
M kernel/kern/process.c
M kernel/kern/process.h
M hal/tsar_mips32/core/hal_remote.c
M hal/tsar_mips32/drivers/soclib_pic.c

File size: 10.9 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////////////////////////////////////////////
134page_t * mapper_get_page( mapper_t * mapper,
135                          uint32_t   index )
136{
137    kmem_req_t    req;
138    page_t      * page;
139    error_t       error;
140
141    mapper_dmsg("\n[INFO] %s : enter for page %d / mapper = %x\n",
142                __FUNCTION__ , index , mapper );
143
144    thread_t * this = CURRENT_THREAD;
145
146    // take mapper lock in READ_MODE
147    rwlock_rd_lock( &mapper->lock );
148
149    // search page in radix tree
150    page = (page_t *)grdxt_lookup( &mapper->radix , index );
151
152    // test if page available in mapper
153    if( ( page == NULL) || page_is_flag( page , PG_INLOAD ) )  // page not available
154    {
155
156        // release the lock in READ_MODE and take it in WRITE_MODE
157        rwlock_rd_unlock( &mapper->lock );
158        rwlock_wr_lock( &mapper->lock );
159
160        // second test on missing page because the page status can have been modified
161        // by another thread, when passing from READ_MODE to WRITE_MODE.
162        // from this point there is no concurrent accesses to mapper.
163
164        page = grdxt_lookup( &mapper->radix , index );
165
166        if ( page == NULL )   // missing page => load it from file system
167        {
168            mapper_dmsg("\n[INFO] %s : missing page => load from FS\n", __FUNCTION__ );
169
170            // allocate one page from PPM
171            req.type  = KMEM_PAGE;
172            req.size  = 0;
173            req.flags = AF_NONE;
174            page = kmem_alloc( &req );
175
176            if( page == NULL )
177            {
178                printk("\n[ERROR] in %s : thread %x cannot allocate a page in cluster %x\n",
179                       __FUNCTION__ , this->trdid , local_cxy );
180                rwlock_wr_unlock( &mapper->lock );
181                return NULL;
182            }
183
184            // initialize the page descriptor
185            page_init( page );
186            page_set_flag( page , PG_INIT );
187            page_set_flag( page , PG_INLOAD );
188            page_refcount_up( page );
189            page->mapper = mapper;
190            page->index  = index;
191
192            // insert page in mapper radix tree
193            error = grdxt_insert( &mapper->radix, index , page );
194
195            // release mapper lock from WRITE_MODE
196            rwlock_wr_unlock( &mapper->lock );
197
198            if( error )
199            {
200                printk("\n[ERROR] in %s : thread %x cannot insert page in mapper\n",
201                       __FUNCTION__ , this->trdid );
202                mapper_release_page( mapper , page );
203                page_clear_flag( page , PG_ALL );
204                req.ptr  = page;
205                req.type = KMEM_PAGE;
206                kmem_free(&req);
207                return NULL;
208            }
209
210            // launch I/O operation to load page from file system
211            error = vfs_move_page_to_mapper( page );
212
213            if( error )
214            {
215                printk("\n[ERROR] in %s : thread %x cannot load page from device\n",
216                       __FUNCTION__ , this->trdid );
217                mapper_release_page( mapper , page );
218                page_clear_flag( page , PG_ALL );
219                req.ptr  = page;
220                req.type = KMEM_PAGE;
221                kmem_free( &req );
222                return NULL;
223            }
224
225            // update the mapper and index fields in page descriptor
226            page->mapper = mapper;
227            page->index  = index;
228
229            // reset the page INLOAD flag to make the page available to all readers
230            page_clear_flag( page , PG_INLOAD );
231
232        }
233        else if( page_is_flag( page , PG_INLOAD ) )   // page is loaded by another thread
234        {
235            // release mapper lock from WRITE_MODE
236            rwlock_wr_unlock( &mapper->lock );
237
238            // deschedule to wait load completion
239            while( 1 )
240            {
241                // exit waiting loop when loaded
242                if(  page_is_flag( page , PG_INLOAD ) ) break;
243
244                // deschedule
245                sched_yield();
246            }
247        }
248    }
249    else                          // page available in mapper
250    {
251
252        rwlock_rd_unlock( &mapper->lock );
253    }
254
255    mapper_dmsg("\n[INFO] %s : exit for page %d / page desc = %x\n",
256                __FUNCTION__ , index , page );
257
258    return page;
259
260}  // end mapper_get_page()
261
262///////////////////////////////////////////////
263error_t mapper_release_page( mapper_t * mapper,
264                             page_t   * page )
265{
266    error_t error;
267
268    // lauch IO operation to update page to file system
269    error = vfs_move_page_from_mapper( page );
270
271    if( error )
272    {
273        printk("\n[ERROR] in %s : cannot update file system\n", __FUNCTION__ );
274        return EIO;
275    }
276
277    // take mapper lock in WRITE_MODE
278    rwlock_wr_lock( &mapper->lock );
279
280    // remove physical page from radix tree
281    grdxt_remove( &mapper->radix , page->index );
282
283    // release mapper lock from WRITE_MODE
284    rwlock_wr_unlock( &mapper->lock );
285
286    // release page to PPM
287    kmem_req_t   req;
288    req.type  = KMEM_PAGE;
289    req.ptr   = page;
290    kmem_free( &req );
291
292    return 0;
293
294}  // end mapper_release_page()
295
296/////////////////////////////////////////
297error_t mapper_move( mapper_t  *  mapper,
298                     bool_t       to_buffer,
299                     uint32_t     file_offset,
300                     void      *  buffer,
301                     uint32_t     size )
302{
303    uint32_t   page_offset;    // first byte to move to/from a mapper page
304    uint32_t   page_count;     // number of bytes to move to/from a mapper page
305    uint32_t   index;          // current mapper page index
306    uint32_t   done;           // number of moved bytes
307    page_t   * page;           // current mapper page descriptor
308    uint8_t  * map_ptr;        // current mapper  address
309    uint8_t  * buf_ptr;        // current buffer  address
310 
311    mapper_dmsg("\n[INFO] %s : enter / to_buf = %d / buffer = %x\n",
312                __FUNCTION__ , to_buffer , buffer );
313
314    // compute offsets of first and last bytes in file
315    uint32_t min_byte = file_offset;
316    uint32_t max_byte = file_offset + size -1;
317
318    // compute indexes of pages for first and last byte in mapper
319    uint32_t first = min_byte >> CONFIG_PPM_PAGE_SHIFT;
320    uint32_t last  = max_byte >> CONFIG_PPM_PAGE_SHIFT;
321
322    done = 0;
323
324    // loop on pages in mapper
325    for( index = first ; index <= last ; index++ )
326    {
327        // compute page_offset
328        if( index == first ) page_offset = min_byte & CONFIG_PPM_PAGE_MASK;
329        else                 page_offset = 0;
330
331        // compute page_count
332        if      ( first == last  ) page_count = size;
333        else if ( index == first ) page_count = CONFIG_PPM_PAGE_SIZE - page_offset;
334        else if ( index == last  ) page_count = (max_byte & CONFIG_PPM_PAGE_MASK) + 1;
335        else                       page_count = CONFIG_PPM_PAGE_SIZE;
336
337        // get page descriptor
338        page = mapper_get_page( mapper , index );
339
340        if ( page == NULL ) return EINVAL;
341
342        // compute pointer in mapper
343        map_ptr = (uint8_t *)ppm_page2vaddr( page ) + page_offset;
344
345        // compute pointer in buffer
346        buf_ptr = (uint8_t *)buffer + done;
347
348        // move fragment
349        if( to_buffer )
350        {
351            hal_copy_to_uspace( buf_ptr , map_ptr , page_count );
352        }
353        else
354        {
355            page_do_dirty( page );
356            hal_copy_from_uspace( map_ptr , buf_ptr , page_count );
357        }
358
359        done += page_count;
360    }
361
362    mapper_dmsg("\n[INFO] %s : exit for buffer %x\n",
363                __FUNCTION__, buffer );
364
365    return 0;
366
367}  // end mapper_move()
368
Note: See TracBrowser for help on using the repository browser.