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

Last change on this file since 16 was 1, checked in by alain, 7 years ago

First import

File size: 6.2 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 consistant with enum in "vseg.h"
46////////////////////////////////////////////////////////////////////////////////////////
47
48const char* vseg_type_name[VSEG_TYPES_NR] =
49{
50        "CODE",
51        "DATA",
52    "HEAP",
53    "STACK",
54    "ANON",
55    "FILE",
56    "REMOTE",
57    "KCODE",
58    "KDATA",
59    "KDEV",
60};
61
62
63/////////////////////
64vseg_t * vseg_alloc()
65{
66    kmem_req_t   req;
67
68    req.type  = KMEM_VSEG;
69        req.size  = sizeof(vseg_t);
70        req.flags = AF_KERNEL;
71
72    return (vseg_t *)kmem_alloc( &req );
73}
74
75///////////////////////////////
76void vseg_free( vseg_t * vseg )
77{
78    kmem_req_t  req;
79
80        req.type = KMEM_VSEG;
81        req.ptr  = vseg;
82        kmem_free( &req );
83}
84
85///////////////////////////////////
86void vseg_init( vseg_t      * vseg,
87                    intptr_t      base, 
88                intptr_t      size,
89                vpn_t         vpn_base,
90                vpn_t         vpn_size,
91                        uint32_t      type,
92                cxy_t         cxy,
93                fdid_t        fdid,
94                        uint32_t      offset )
95{
96    vseg->type      = type;
97        vseg->min       = base;
98        vseg->max       = base + size;
99    vseg->vpn_base  = vpn_base;
100        vseg->vpn_size  = vpn_size;
101        vseg->mapper    = XPTR_NULL;
102        vseg->fdid      = fdid;
103        vseg->offset    = offset;
104    vseg->cxy       = cxy;
105
106    // set vseg flags depending on type
107        if     ( type == VSEG_TYPE_CODE ) 
108    {
109        vseg->flags = VSEG_USER    |
110                      VSEG_EXEC    | 
111                      VSEG_CACHE   |
112                      VSEG_PRIVATE ;
113    }
114    else if( type == VSEG_TYPE_STACK )
115    {
116        vseg->flags = VSEG_USER    |
117                      VSEG_WRITE   |
118                      VSEG_CACHE   |
119                      VSEG_PRIVATE ; 
120    }
121    else if( type == VSEG_TYPE_DATA ) 
122    {
123        vseg->flags = VSEG_USER    |
124                      VSEG_WRITE   |
125                      VSEG_CACHE   | 
126                      VSEG_DISTRIB ;
127    }
128    else if( type == VSEG_TYPE_HEAP ) 
129    {
130        vseg->flags = VSEG_USER    |
131                      VSEG_WRITE   |
132                      VSEG_CACHE   |
133                      VSEG_DISTRIB ;
134    }
135    else if( type == VSEG_TYPE_REMOTE ) 
136    {
137        vseg->flags = VSEG_USER    |
138                      VSEG_WRITE   | 
139                      VSEG_CACHE   ; 
140    }
141    else if( type == VSEG_TYPE_ANON ) 
142    {
143        vseg->flags = VSEG_USER    |
144                      VSEG_WRITE   |
145                      VSEG_CACHE   |
146                      VSEG_DISTRIB ;
147    }
148    else if( type == VSEG_TYPE_FILE ) 
149    {
150        vseg->flags = VSEG_USER    |
151                      VSEG_WRITE   |
152                      VSEG_CACHE   ;
153    }
154    else if( type == VSEG_TYPE_KCODE ) 
155    {
156        vseg->flags = VSEG_EXEC    |
157                      VSEG_CACHE   |
158                      VSEG_PRIVATE ;
159    }
160    else if( type == VSEG_TYPE_KDATA ) 
161    {
162        vseg->flags = VSEG_WRITE   |
163                      VSEG_CACHE   |
164                      VSEG_PRIVATE ;
165    }
166    else 
167    {
168            printk("\n[PANIC] in %s : illegal vseg type\n", __FUNCTION__);
169        hal_core_sleep();
170    } 
171}  // end vseg_init()
172
173//////////////////////////////////////////
174void vseg_init_from_ref( vseg_t    * vseg,
175                         xptr_t      ref )
176{
177    // get remote vseg cluster and pointer
178    cxy_t    cxy = (cxy_t   )GET_CXY( ref );
179    vseg_t * ptr = (vseg_t *)GET_PTR( ref );
180
181    // initialize vseg with remote_read access
182    vseg->type     =           hal_remote_lw ( XPTR( cxy , &ptr->type     ) );
183    vseg->min      = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min      ) );
184    vseg->max      = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max      ) );
185    vseg->vpn_base =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_base ) );
186    vseg->vpn_size =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_size ) );
187    vseg->flags    =           hal_remote_lw ( XPTR( cxy , &ptr->flags    ) );
188        vseg->mapper   = (xptr_t)  hal_remote_lwd( XPTR( cxy , &ptr->mapper   ) );
189
190    if( vseg->type == VSEG_TYPE_FILE )
191    {
192        vseg->fdid   = hal_remote_lw( XPTR( cxy , &ptr->fdid   ) );
193        vseg->offset = hal_remote_lw( XPTR( cxy , &ptr->offset ) );
194    }
195    else
196    {
197        vseg->fdid   = 0;
198        vseg->offset = 0;
199    }
200} // end vseg_init_from_ref()
201
202
203///////////////////////////////
204error_t vseg_attach( vmm_t  * vmm,
205                     vseg_t * vseg )
206{
207    // add vseg in radix-tree
208    error_t error = grdxt_insert( &vmm->grdxt , vseg->vpn_base , vseg );
209    if ( error ) return ENOMEM;
210
211    // update vseg descriptor
212    vseg->vmm = vmm;
213
214    // add vseg in vmm list
215    list_add_last( &vmm->vsegs_root , &vseg->list );
216
217    return 0;
218}
219
220///////////////////////////////
221void vseg_detach( vmm_t  * vmm,
222                  vseg_t * vseg )
223{
224    // remove vseg from radix-tree
225    grdxt_remove( &vmm->grdxt , vseg->vpn_base );
226
227    // update vseg descriptor
228    vseg->vmm = NULL;
229
230    // remove vseg from vmm list
231    list_unlink( &vseg->list );
232}
233
234
Note: See TracBrowser for help on using the repository browser.