source: trunk/kernel/mm/vseg.c @ 266

Last change on this file since 266 was 184, checked in by max@…, 7 years ago

style

File size: 6.7 KB
Line 
1/*
2 * vseg.c - virtual segment (vseg) related operations
3 *
4 * Authors   Ghassan Almaless (2008,2009,2010,2011, 2012)
5 *           Mohamed Lamine Karaoui (2015)
6 *           Alain Greiner (2016)
7 *
8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH.
11 *
12 * ALMOS-MKH is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <hal_types.h>
27#include <hal_special.h>
28#include <hal_remote.h>
29#include <list.h>
30#include <errno.h>
31#include <printk.h>
32#include <bits.h>
33#include <thread.h>
34#include <process.h>
35#include <ppm.h>
36#include <mapper.h>
37#include <spinlock.h>
38#include <vfs.h>
39#include <page.h>
40#include <vmm.h>
41#include <kmem.h>
42#include <vseg.h>
43
44////////////////////////////////////////////////////////////////////////////////////////
45//   global variables for display / must be consistent with enum in "vseg.h"
46////////////////////////////////////////////////////////////////////////////////////////
47
48
49//////////////////////////////////////////
50char * vseg_type_str( uint32_t vseg_type )
51{
52        if     ( vseg_type == VSEG_TYPE_CODE   ) return "CODE";
53        else if( vseg_type == VSEG_TYPE_DATA   ) return "DATA";
54        else if( vseg_type == VSEG_TYPE_HEAP   ) return "HEAP";
55        else if( vseg_type == VSEG_TYPE_STACK  ) return "STACK";
56        else if( vseg_type == VSEG_TYPE_ANON   ) return "ANON";
57        else if( vseg_type == VSEG_TYPE_FILE   ) return "FILE";
58        else if( vseg_type == VSEG_TYPE_REMOTE ) return "REMOTE";
59        else if( vseg_type == VSEG_TYPE_KCODE  ) return "KCODE";
60        else if( vseg_type == VSEG_TYPE_KDATA  ) return "KDATA";
61        else if( vseg_type == VSEG_TYPE_KDEV   ) return "KDEV";
62    else                                     return "undefined";
63}
64
65/////////////////////
66vseg_t * vseg_alloc()
67{
68    kmem_req_t   req;
69
70    req.type  = KMEM_VSEG;
71        req.size  = sizeof(vseg_t);
72        req.flags = AF_KERNEL;
73
74    return (vseg_t *)kmem_alloc( &req );
75}
76
77///////////////////////////////
78void vseg_free( vseg_t * vseg )
79{
80    kmem_req_t  req;
81
82        req.type = KMEM_VSEG;
83        req.ptr  = vseg;
84        kmem_free( &req );
85}
86
87///////////////////////////////////
88void vseg_init( vseg_t      * vseg,
89                    intptr_t      base,
90                intptr_t      size,
91                vpn_t         vpn_base,
92                vpn_t         vpn_size,
93                        uint32_t      type,
94                cxy_t         cxy,
95                fdid_t        fdid,
96                        uint32_t      offset )
97{
98    vseg->type      = type;
99        vseg->min       = base;
100        vseg->max       = base + size;
101    vseg->vpn_base  = vpn_base;
102        vseg->vpn_size  = vpn_size;
103        vseg->mapper    = XPTR_NULL;
104        vseg->fdid      = fdid;
105        vseg->offset    = offset;
106    vseg->cxy       = cxy;
107
108    // set vseg flags depending on type
109        if     ( type == VSEG_TYPE_CODE )
110    {
111        vseg->flags = VSEG_USER    |
112                      VSEG_EXEC    |
113                      VSEG_CACHE   |
114                      VSEG_PRIVATE ;
115    }
116    else if( type == VSEG_TYPE_STACK )
117    {
118        vseg->flags = VSEG_USER    |
119                      VSEG_WRITE   |
120                      VSEG_CACHE   |
121                      VSEG_PRIVATE ;
122    }
123    else if( type == VSEG_TYPE_DATA )
124    {
125        vseg->flags = VSEG_USER    |
126                      VSEG_WRITE   |
127                      VSEG_CACHE   |
128                      VSEG_DISTRIB ;
129    }
130    else if( type == VSEG_TYPE_HEAP )
131    {
132        vseg->flags = VSEG_USER    |
133                      VSEG_WRITE   |
134                      VSEG_CACHE   |
135                      VSEG_DISTRIB ;
136    }
137    else if( type == VSEG_TYPE_REMOTE )
138    {
139        vseg->flags = VSEG_USER    |
140                      VSEG_WRITE   |
141                      VSEG_CACHE   ;
142    }
143    else if( type == VSEG_TYPE_ANON )
144    {
145        vseg->flags = VSEG_USER    |
146                      VSEG_WRITE   |
147                      VSEG_CACHE   |
148                      VSEG_DISTRIB ;
149    }
150    else if( type == VSEG_TYPE_FILE )
151    {
152        vseg->flags = VSEG_USER    |
153                      VSEG_WRITE   |
154                      VSEG_CACHE   ;
155    }
156    else if( type == VSEG_TYPE_KCODE )
157    {
158        vseg->flags = VSEG_EXEC    |
159                      VSEG_CACHE   |
160                      VSEG_PRIVATE ;
161    }
162    else if( type == VSEG_TYPE_KDATA )
163    {
164        vseg->flags = VSEG_WRITE   |
165                      VSEG_CACHE   |
166                      VSEG_PRIVATE ;
167    }
168    else
169    {
170            printk("\n[PANIC] in %s : illegal vseg type\n", __FUNCTION__);
171        hal_core_sleep();
172    }
173}
174
175//////////////////////////////////////////
176void vseg_init_from_ref( vseg_t    * vseg,
177                         xptr_t      ref )
178{
179    // get remote vseg cluster and pointer
180    cxy_t    cxy = (cxy_t   )GET_CXY( ref );
181    vseg_t * ptr = (vseg_t *)GET_PTR( ref );
182
183    // initialize vseg with remote_read access
184    vseg->type     =           hal_remote_lw ( XPTR( cxy , &ptr->type     ) );
185    vseg->min      = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min      ) );
186    vseg->max      = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max      ) );
187    vseg->vpn_base =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_base ) );
188    vseg->vpn_size =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_size ) );
189    vseg->flags    =           hal_remote_lw ( XPTR( cxy , &ptr->flags    ) );
190        vseg->mapper   = (xptr_t)  hal_remote_lwd( XPTR( cxy , &ptr->mapper   ) );
191
192    if( vseg->type == VSEG_TYPE_FILE )
193    {
194        vseg->fdid   = hal_remote_lw( XPTR( cxy , &ptr->fdid   ) );
195        vseg->offset = hal_remote_lw( XPTR( cxy , &ptr->offset ) );
196    }
197    else
198    {
199        vseg->fdid   = 0;
200        vseg->offset = 0;
201    }
202}
203
204///////////////////////////////
205error_t vseg_attach( vmm_t  * vmm,
206                     vseg_t * vseg )
207{
208    // add vseg in radix-tree
209    error_t error = grdxt_insert( &vmm->grdxt , vseg->vpn_base , vseg );
210    if ( error ) return ENOMEM;
211
212    // update vseg descriptor
213    vseg->vmm = vmm;
214
215    // add vseg in vmm list
216    list_add_last( &vmm->vsegs_root , &vseg->list );
217
218    return 0;
219}
220
221///////////////////////////////
222void vseg_detach( vmm_t  * vmm,
223                  vseg_t * vseg )
224{
225    // remove vseg from radix-tree
226    grdxt_remove( &vmm->grdxt , vseg->vpn_base );
227
228    // update vseg descriptor
229    vseg->vmm = NULL;
230
231    // remove vseg from vmm list
232    list_unlink( &vseg->list );
233}
234
Note: See TracBrowser for help on using the repository browser.