source: trunk/kernel/mm/kcm.c @ 628

Last change on this file since 628 was 619, checked in by alain, 5 years ago

1) Fix a bug in KSH : after the "load" command,

the [ksh] prompt is now printed after completion
of the loaded application.

2) Fix a bug in vmm_handle_cow() : the copy-on-write

use now a hal_remote_memcpy() to replicate the page content.


File size: 10.4 KB
Line 
1/*
2 * kcm.c - Per cluster Kernel Cache Manager implementation.
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner    (2016,2017,2018,2019)
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_kernel_types.h>
27#include <hal_special.h>
28#include <busylock.h>
29#include <list.h>
30#include <printk.h>
31#include <bits.h>
32#include <ppm.h>
33#include <thread.h>
34#include <page.h>
35#include <cluster.h>
36#include <kmem.h>
37#include <kcm.h>
38
39
40//////////////////////////////////////////////////////////////////////////////////////
41// This static function returns pointer on an allocated block from an active page.
42// It returns NULL if no block available in selected page.
43// It changes the page status if required.
44//////////////////////////////////////////////////////////////////////////////////////
45// @ kcm      : pointer on kcm allocator.
46// @ kcm_page : pointer on active kcm page to use.
47/////////////////////////////////////////////////////////////////////////////////////
48static void * kcm_get_block( kcm_t      * kcm,
49                             kcm_page_t * kcm_page )
50{
51
52#if DEBUG_KCM
53thread_t * this = CURRENT_THREAD;
54uint32_t cycle = (uint32_t)hal_get_cycles();
55if( DEBUG_KCM < cycle )
56printk("\n[%s] thread[%x,%x] enters for %s / page %x / count %d / active %d\n",
57__FUNCTION__, this->process->pid, this->trdid, kmem_type_str(kcm->type),
58(intptr_t)kcm_page , kcm_page->count , kcm_page->active );
59#endif
60
61assert( kcm_page->active , "kcm_page should be active" );
62
63        // get first block available
64        int32_t index = bitmap_ffs( kcm_page->bitmap , kcm->blocks_nr );
65
66assert( (index != -1) , "kcm_page should not be full" );
67
68        // allocate block
69        bitmap_clear( kcm_page->bitmap , index );
70
71        // increase kcm_page count
72        kcm_page->count ++;
73
74        // change the kcm_page to busy if no more free block in page
75        if( kcm_page->count >= kcm->blocks_nr )
76        {
77                kcm_page->active = 0;
78                list_unlink( &kcm_page->list);
79                kcm->active_pages_nr --;
80
81                list_add_first( &kcm->busy_root , &kcm_page->list);
82                kcm->busy_pages_nr ++;
83                kcm_page->busy = 1;
84        }
85
86        // compute return pointer
87        void * ptr = (void *)((intptr_t)kcm_page + CONFIG_KCM_SLOT_SIZE
88                     + (index * kcm->block_size) );
89
90#if DEBUG_KCM
91cycle = (uint32_t)hal_get_cycles();
92if( DEBUG_KCM < cycle )
93printk("\n[%s] thread[%x,%x] exit for %s / ptr %x / page %x / count %d\n",
94__FUNCTION__, this->process->pid, this->trdid, kmem_type_str(kcm->type),
95(intptr_t)ptr, (intptr_t)kcm_page, kcm_page->count );
96#endif
97
98        return ptr;
99}
100
101/////////////////////////////////////////////////////////////////////////////////////
102// This static function releases a previously allocated block.
103// It changes the kcm_page status if required.
104/////////////////////////////////////////////////////////////////////////////////////
105// @ kcm      : pointer on kcm allocator.
106// @ kcm_page : pointer on kcm_page.
107// @ ptr      : pointer on block to be released.
108/////////////////////////////////////////////////////////////////////////////////////
109static void kcm_put_block ( kcm_t      * kcm,
110                            kcm_page_t * kcm_page,
111                            void       * ptr )
112{
113        uint32_t     index;
114
115        // compute block index from block pointer
116        index = ((uint8_t *)ptr - (uint8_t *)kcm_page - CONFIG_KCM_SLOT_SIZE) / kcm->block_size;
117
118assert( !bitmap_state( kcm_page->bitmap , index ) , "page already freed" );
119
120assert( (kcm_page->count > 0) , "count already zero" );
121
122        bitmap_set( kcm_page->bitmap , index );
123        kcm_page->count --;
124
125        // change the page to active if it was busy
126        if( kcm_page->busy )
127        {
128                kcm_page->busy = 0;
129                list_unlink( &kcm_page->list );
130                kcm->busy_pages_nr --;
131
132                list_add_last( &kcm->active_root, &kcm_page->list );
133                kcm->active_pages_nr ++;
134                kcm_page->active = 1;
135        }
136
137        // change the kcm_page to free if last block in active page
138        if( (kcm_page->active) && (kcm_page->count == 0) )
139        {
140                kcm_page->active = 0;
141                list_unlink( &kcm_page->list);
142                kcm->active_pages_nr --;
143
144                list_add_first( &kcm->free_root , &kcm_page->list);
145                kcm->free_pages_nr ++;
146        }
147}
148
149/////////////////////////////////////////////////////////////////////////////////////
150// This static function allocates one page from PPM. It initializes
151// the kcm_page descriptor, and introduces the new kcm_page into freelist.
152/////////////////////////////////////////////////////////////////////////////////////
153static error_t freelist_populate( kcm_t * kcm )
154{
155        page_t     * page;
156        kcm_page_t * kcm_page;
157        kmem_req_t   req;
158
159        // get one page from local PPM
160        req.type  = KMEM_PAGE;
161        req.size  = 0;
162        req.flags = AF_KERNEL;
163        page = kmem_alloc( &req );
164
165        if( page == NULL )
166        {
167                printk("\n[ERROR] in %s : failed to allocate page in cluster %x\n",
168            __FUNCTION__ , local_cxy );
169                return ENOMEM;
170        }
171
172        // get page base address
173        xptr_t base_xp = ppm_page2base( XPTR( local_cxy , page ) );
174        kcm_page = (kcm_page_t *)GET_PTR( base_xp );
175
176        // initialize KCM-page descriptor
177        bitmap_set_range( kcm_page->bitmap , 0 , kcm->blocks_nr );
178
179        kcm_page->busy          = 0;
180        kcm_page->active        = 0;
181        kcm_page->count      = 0;
182        kcm_page->kcm           = kcm;
183        kcm_page->page          = page;
184
185        // introduce new page in free-list
186        list_add_first( &kcm->free_root , &kcm_page->list );
187        kcm->free_pages_nr ++;
188
189        return 0;
190}
191
192/////////////////////////////////////////////////////////////////////////////////////
193// This private function gets one KCM page from the KCM freelist.
194// It populates the freelist if required.
195/////////////////////////////////////////////////////////////////////////////////////
196static kcm_page_t * freelist_get( kcm_t * kcm )
197{
198        error_t      error;
199        kcm_page_t * kcm_page;
200
201        // get a new page from PPM if freelist empty
202        if( kcm->free_pages_nr == 0 )
203        {
204                error = freelist_populate( kcm );
205                if( error ) return NULL;
206        }
207
208        // get first KCM page from freelist and unlink it
209        kcm_page = LIST_FIRST( &kcm->free_root, kcm_page_t , list );
210        list_unlink( &kcm_page->list );
211        kcm->free_pages_nr --;
212
213        return kcm_page;
214}
215
216//////////////////////////////
217void kcm_init( kcm_t    * kcm,
218                   uint32_t   type )
219{
220
221// the kcm_page descriptor must fit in the KCM slot
222assert( (sizeof(kcm_page_t) <= CONFIG_KCM_SLOT_SIZE) , "KCM slot too small\n" );
223
224// the allocated object must fit in one single page
225assert( (kmem_type_size(type) <= (CONFIG_PPM_PAGE_SIZE - CONFIG_KCM_SLOT_SIZE)),
226"allocated object requires more than one single page\n" );
227
228        // initialize lock
229        busylock_init( &kcm->lock , LOCK_KCM_STATE );
230
231        // initialize KCM type
232        kcm->type = type;
233
234        // initialize KCM page lists
235        kcm->free_pages_nr   = 0;
236        kcm->busy_pages_nr   = 0;
237        kcm->active_pages_nr = 0;
238        list_root_init( &kcm->free_root );
239        list_root_init( &kcm->busy_root );
240        list_root_init( &kcm->active_root );
241
242        // initialize block size
243        uint32_t block_size = ARROUND_UP( kmem_type_size( type ) , CONFIG_KCM_SLOT_SIZE );
244        kcm->block_size = block_size;
245
246        // initialize number of blocks per page
247        uint32_t  blocks_nr = (CONFIG_PPM_PAGE_SIZE - CONFIG_KCM_SLOT_SIZE) / block_size;
248        kcm->blocks_nr = blocks_nr;
249
250#if DEBUG_KCM
251thread_t * this  = CURRENT_THREAD;
252uint32_t   cycle = (uint32_t)hal_get_cycles();
253if( DEBUG_KCM < cycle )
254printk("\n[%s] thread[%x,%x] initialised KCM %s : block_size %d / blocks_nr %d\n",
255__FUNCTION__, this->process->pid, this->trdid,
256kmem_type_str( kcm->type ), block_size, blocks_nr );
257#endif
258
259}
260
261///////////////////////////////
262void kcm_destroy( kcm_t * kcm )
263{
264        kcm_page_t   * kcm_page;
265        list_entry_t * iter;
266
267        // get KCM lock
268        busylock_acquire( &kcm->lock );
269
270        // release all free pages
271        LIST_FOREACH( &kcm->free_root , iter )
272        {
273                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
274                list_unlink( iter );
275                kcm->free_pages_nr --;
276                ppm_free_pages( kcm_page->page );
277        }
278
279        // release all active pages
280        LIST_FOREACH( &kcm->active_root , iter )
281        {
282                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
283                list_unlink( iter );
284                kcm->free_pages_nr --;
285                ppm_free_pages( kcm_page->page );
286        }
287
288        // release all busy pages
289        LIST_FOREACH( &kcm->busy_root , iter )
290        {
291                kcm_page = (kcm_page_t *)LIST_ELEMENT( iter , kcm_page_t , list );
292                list_unlink( iter );
293                kcm->free_pages_nr --;
294                ppm_free_pages( kcm_page->page );
295        }
296
297        // release KCM lock
298        busylock_release( &kcm->lock );
299}
300
301///////////////////////////////
302void * kcm_alloc( kcm_t * kcm )
303{
304        kcm_page_t * kcm_page;
305        void       * ptr = NULL;   // pointer on block
306
307        // get lock
308        busylock_acquire( &kcm->lock );
309
310        // get an active page
311        if( list_is_empty( &kcm->active_root ) )  // no active page => get one
312        {
313                // get a page from free list
314                kcm_page = freelist_get( kcm );
315
316                if( kcm_page == NULL )
317                {
318                        busylock_release( &kcm->lock );
319                        return NULL;
320                }
321
322                // insert page in active list
323                list_add_first( &kcm->active_root , &kcm_page->list );
324                kcm->active_pages_nr ++;
325                kcm_page->active = 1;
326        }
327        else                                    // get first page from active list
328        {
329                // get page pointer from active list
330                kcm_page = (kcm_page_t *)LIST_FIRST( &kcm->active_root , kcm_page_t , list );
331        }
332
333        // get a block from selected active page
334        // cannot fail, as an active page cannot be full...
335        ptr  = kcm_get_block( kcm , kcm_page );
336
337        // release lock
338        busylock_release( &kcm->lock );
339
340        return ptr;
341}
342
343///////////////////////////
344void kcm_free( void * ptr )
345{
346        kcm_page_t * kcm_page;
347        kcm_t      * kcm;
348
349// check argument
350assert( (ptr != NULL) , "pointer cannot be NULL" );
351
352        kcm_page = (kcm_page_t *)((intptr_t)ptr & ~CONFIG_PPM_PAGE_MASK);
353        kcm      = kcm_page->kcm;
354
355        // get lock
356        busylock_acquire( &kcm->lock );
357
358        // release block
359        kcm_put_block( kcm , kcm_page , ptr );
360
361        // release lock
362        busylock_release( &kcm->lock );
363}
364
365////////////////////////////
366void kcm_print (kcm_t * kcm)
367{
368        printk("*** KCM type = %s / free_pages = %d / busy_pages = %d / active_pages = %d\n",
369               kmem_type_str( kcm->type ) ,
370               kcm->free_pages_nr ,
371               kcm->busy_pages_nr ,
372               kcm->active_pages_nr );
373}
Note: See TracBrowser for help on using the repository browser.