source: trunk/kernel/mm/kmem.c @ 666

Last change on this file since 666 was 657, checked in by alain, 5 years ago

Introduce remote_buf.c/.h & socket.c/.h files.
Update dev_nic.c/.h files.

File size: 7.4 KB
Line 
1/*
2 * kmem.c - kernel memory allocator implementation.
3 *
4 * Authors  Alain Greiner (2016,2017,2018,2019,2020)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_special.h>
27#include <printk.h>
28#include <cluster.h>
29#include <memcpy.h>
30#include <khm.h>
31#include <ppm.h>
32#include <kcm.h>
33#include <page.h>
34#include <kmem.h>
35
36/////////////////////////////////////
37void * kmem_alloc( kmem_req_t * req )
38{
39        uint32_t    type;    // KMEM_PPM / KMEM_KCM / KMEM_KHM
40        uint32_t    flags;   // AF_NONE / AF_ZERO / AF_KERNEL
41        uint32_t    order;   // PPM: ln(pages) / KCM: ln(bytes) / KHM: bytes
42
43        type  = req->type;
44        order = req->order;
45        flags = req->flags;
46
47    //////////////////////
48        if( type == KMEM_PPM )
49        {
50                // allocate the number of requested pages
51                page_t * page_ptr = (void *)ppm_alloc_pages( order );
52
53                if( page_ptr == NULL )
54                {
55                        printk("\n[ERROR] in %s : PPM failed / order %d / cluster %x\n",
56                        __FUNCTION__ , order , local_cxy );
57                        return NULL;
58                }
59
60        xptr_t page_xp = XPTR( local_cxy , page_ptr );
61
62                // reset page if requested
63                if( flags & AF_ZERO ) page_zero( page_ptr );
64
65        // get pointer on buffer from the page descriptor
66        void * ptr = GET_PTR( ppm_page2base( page_xp ) );
67
68#if DEBUG_KMEM
69thread_t * this  = CURRENT_THREAD;
70uint32_t   cycle = (uint32_t)hal_get_cycles();
71if( DEBUG_KMEM < cycle )
72printk("\n[%s] thread[%x,%x] from PPM / %d page(s) / ppn %x / cxy %x / cycle %d\n",
73__FUNCTION__, this->process->pid, this->trdid,
741<<order, ppm_page2ppn(XPTR(local_cxy,ptr)), local_cxy, cycle );
75#endif
76        return ptr;
77        }
78    ///////////////////////////
79        else if( type == KMEM_KCM )
80        {
81                // allocate memory from KCM
82                void * ptr = kcm_alloc( order );
83
84                if( ptr == NULL )
85                {
86                        printk("\n[ERROR] in %s : KCM failed / order %d / cluster %x\n",
87                    __FUNCTION__ , order , local_cxy );
88                        return NULL;
89                }
90
91                // reset memory if requested
92                if( flags & AF_ZERO ) memset( ptr , 0 , 1<<order );
93
94#if DEBUG_KMEM
95thread_t * this  = CURRENT_THREAD;
96uint32_t   cycle = (uint32_t)hal_get_cycles();
97if( DEBUG_KMEM < cycle )
98printk("\n[%s] thread [%x,%x] from KCM / %d bytes / base %x / cxy %x / cycle %d\n",
99__FUNCTION__, this->process->pid, this->trdid,
1001<<order, ptr, local_cxy, cycle ); 
101#endif
102        return ptr;
103        }
104    ///////////////////////////
105        else if( type == KMEM_KHM )
106        {
107                // allocate memory from KHM
108                void * ptr = khm_alloc( &LOCAL_CLUSTER->khm , order );
109
110                if( ptr == NULL )
111                {
112                        printk("\n[ERROR] in %s : KHM failed / order %d / cluster %x\n",
113                        __FUNCTION__ , order , local_cxy );
114                        return NULL;
115                }
116
117                // reset memory if requested
118                if( flags & AF_ZERO ) memset( ptr , 0 , order );
119
120#if DEBUG_KMEM
121thread_t * this  = CURRENT_THREAD;
122uint32_t   cycle = (uint32_t)hal_get_cycles();
123if( DEBUG_KMEM < cycle )
124printk("\n[%s] thread[%x,%x] from KHM / %d bytes / base %x / cxy %x / cycle %d\n",
125__FUNCTION__, this->process->pid, this->trdid, 
126order, ptr, local_cxy, cycle );
127#endif
128        return ptr;
129        }
130    else
131    {
132        printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__);
133        return NULL;
134    }
135}  // end kmem_alloc()
136
137//////////////////////////////////
138void kmem_free( kmem_req_t * req )
139{
140    uint32_t type = req->type;
141
142    //////////////////////
143        if( type == KMEM_PPM )
144        {
145        page_t * page = GET_PTR( ppm_base2page( XPTR( local_cxy , req->ptr ) ) );
146
147        ppm_free_pages( page );
148    }
149    ///////////////////////////
150    else if( type == KMEM_KCM )
151    {
152        kcm_free( req->ptr );
153        }
154    ///////////////////////////
155    else if( type == KMEM_KHM )
156    {
157        khm_free( req->ptr );
158    }
159    else
160    {
161        printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__);
162    }
163}  // end kmem_free()
164
165///////////////////////////////////////////
166void * kmem_remote_alloc( cxy_t        cxy,
167                          kmem_req_t * req )
168{
169        uint32_t    type;    // KMEM_PPM / KMEM_KCM / KMEM_KHM
170        uint32_t    flags;   // AF_ZERO / AF_KERNEL / AF_NONE
171        uint32_t    order;   // PPM: ln(pages) / KCM: ln(bytes) / KHM: bytes
172
173        type  = req->type;
174        order = req->order;
175        flags = req->flags;
176
177        //////////////////////
178        if( type == KMEM_PPM )
179        {
180                // allocate the number of requested pages from remote cluster
181                xptr_t page_xp = ppm_remote_alloc_pages( cxy , order );
182
183                if( page_xp == XPTR_NULL )
184                {
185                        printk("\n[ERROR] in %s : failed for PPM / order %d in cluster %x\n",
186                        __FUNCTION__ , order , cxy );
187                        return NULL;
188                }
189
190        // get extended pointer on remote buffer
191        xptr_t base_xp = ppm_page2base( page_xp );
192
193                // reset page if requested
194                if( flags & AF_ZERO ) hal_remote_memset( base_xp , 0 , CONFIG_PPM_PAGE_SIZE );
195
196
197#if DEBUG_KMEM_REMOTE
198thread_t * this = CURRENT_THREAD;
199uint32_t   cycle = (uint32_t)hal_get_cycles();
200if( DEBUG_KMEM_REMOTE < cycle )
201printk("\n[%s] thread[%x,%x] from PPM / %d page(s) / ppn %x / cxy %x / cycle %d\n",
202__FUNCTION__, this->process->pid, this->trdid,
2031<<order, ppm_page2ppn( page_xp ), cxy, cycle );
204#endif
205        return GET_PTR( base_xp );
206        }
207    ///////////////////////////
208        else if( type == KMEM_KCM ) 
209        {
210                // allocate memory from KCM
211                void * ptr = kcm_remote_alloc( cxy , order );
212
213                if( ptr == NULL )
214                {
215                        printk("\n[ERROR] in %s : failed for KCM / order %d in cluster %x\n",
216                    __FUNCTION__ , order , cxy );
217                        return NULL;
218                }
219
220                // reset memory if requested
221                if( flags & AF_ZERO )  hal_remote_memset( XPTR( cxy , ptr ) , 0 , 1<<order );
222
223#if DEBUG_KMEM_REMOTE
224thread_t * this = CURRENT_THREAD;
225uint32_t   cycle = (uint32_t)hal_get_cycles();
226if( DEBUG_KMEM_REMOTE < cycle )
227printk("\n[%s] thread [%x,%x] from KCM / %d bytes / base %x / cxy %x / cycle %d\n",
228__FUNCTION__, this->process->pid, this->trdid,
2291<<order, ptr, cxy, cycle );
230#endif
231        return ptr;
232        }
233        ///////////////////////////
234        else if( type == KMEM_KHM )               
235        {
236        printk("\n[ERROR] in %s : remote access not supported for KHM\n", __FUNCTION__  );
237                return NULL;
238        }
239    else
240    {
241        printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__);
242        return NULL;
243    }
244}  // kmem_remote_malloc()
245
246////////////////////////////////////////
247void kmem_remote_free( cxy_t        cxy,
248                       kmem_req_t * req )
249{
250    uint32_t type = req->type;
251
252    //////////////////////
253        if( type == KMEM_PPM )
254        {
255        page_t * page = GET_PTR( ppm_base2page( XPTR( cxy , req->ptr ) ) );
256
257        ppm_remote_free_pages( cxy , page );
258    }
259    ///////////////////////////
260    else if( type == KMEM_KCM )
261    {
262        kcm_remote_free( cxy , req->ptr );
263        }
264    ///////////////////////////
265    else if( type == KMEM_KHM )
266    {
267        printk("\n[ERROR] in %s : remote access not supported for KHM\n", __FUNCTION__ );
268    }
269    else
270    {
271        printk("\n[ERROR] in %s : illegal allocator type\n", __FUNCTION__);
272    }
273}  // end kmem_remote_free()
274
275
276
Note: See TracBrowser for help on using the repository browser.