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

Last change on this file since 379 was 315, checked in by alain, 7 years ago

Redefine the fuctions ppm_base2page() / ppm_page2base() / ppm_page2ppn() / ppm_ppn2page() / ppm_base2ppn() / ppm_ppn2base(),
to use explicitely extended pointers.

File size: 6.4 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{
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_xp = XPTR_NULL;
102    vseg->cxy       = cxy;
103
104    // set vseg flags depending on type
105        if     ( type == VSEG_TYPE_CODE )
106    {
107        vseg->flags = VSEG_USER    |
108                      VSEG_EXEC    |
109                      VSEG_CACHE   |
110                      VSEG_PRIVATE ;
111    }
112    else if( type == VSEG_TYPE_STACK )
113    {
114        vseg->flags = VSEG_USER    |
115                      VSEG_WRITE   |
116                      VSEG_CACHE   |
117                      VSEG_PRIVATE ;
118    }
119    else if( type == VSEG_TYPE_DATA )
120    {
121        vseg->flags = VSEG_USER    |
122                      VSEG_WRITE   |
123                      VSEG_CACHE   |
124                      VSEG_DISTRIB ;
125    }
126    else if( type == VSEG_TYPE_HEAP )
127    {
128        vseg->flags = VSEG_USER    |
129                      VSEG_WRITE   |
130                      VSEG_CACHE   |
131                      VSEG_DISTRIB ;
132    }
133    else if( type == VSEG_TYPE_REMOTE )
134    {
135        vseg->flags = VSEG_USER    |
136                      VSEG_WRITE   |
137                      VSEG_CACHE   ;
138    }
139    else if( type == VSEG_TYPE_ANON )
140    {
141        vseg->flags = VSEG_USER    |
142                      VSEG_WRITE   |
143                      VSEG_CACHE   |
144                      VSEG_DISTRIB ;
145    }
146    else if( type == VSEG_TYPE_FILE )
147    {
148        vseg->flags = VSEG_USER    |
149                      VSEG_WRITE   |
150                      VSEG_CACHE   ;
151    }
152    else if( type == VSEG_TYPE_KCODE )
153    {
154        vseg->flags = VSEG_EXEC    |
155                      VSEG_CACHE   |
156                      VSEG_PRIVATE ;
157    }
158    else if( type == VSEG_TYPE_KDATA )
159    {
160        vseg->flags = VSEG_WRITE   |
161                      VSEG_CACHE   |
162                      VSEG_PRIVATE ;
163    }
164    else
165    {
166            printk("\n[PANIC] in %s : illegal vseg type\n", __FUNCTION__);
167        hal_core_sleep();
168    }
169
170}  // end vseg_init()
171
172//////////////////////////////////////////
173void vseg_init_from_ref( vseg_t    * vseg,
174                         xptr_t      ref )
175{
176    // get remote vseg cluster and pointer
177    cxy_t    cxy = (cxy_t   )GET_CXY( ref );
178    vseg_t * ptr = (vseg_t *)GET_PTR( ref );
179
180    // initialize vseg with remote_read access
181    vseg->type       =           hal_remote_lw ( XPTR( cxy , &ptr->type      ) );
182    vseg->min        = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min       ) );
183    vseg->max        = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max       ) );
184    vseg->vpn_base   =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_base  ) );
185    vseg->vpn_size   =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_size  ) );
186    vseg->flags      =           hal_remote_lw ( XPTR( cxy , &ptr->flags     ) );
187        vseg->mapper_xp  = (xptr_t)  hal_remote_lwd( XPTR( cxy , &ptr->mapper_xp ) );
188}
189
190///////////////////////////////
191error_t vseg_attach( vmm_t  * vmm,
192                     vseg_t * vseg )
193{
194    // add vseg in radix-tree
195    error_t error = grdxt_insert( &vmm->grdxt , vseg->vpn_base , vseg );
196    if ( error ) return ENOMEM;
197
198    // update vseg descriptor
199    vseg->vmm = vmm;
200
201    // add vseg in vmm list
202    list_add_last( &vmm->vsegs_root , &vseg->list );
203
204    return 0;
205}
206
207///////////////////////////////
208void vseg_detach( vmm_t  * vmm,
209                  vseg_t * vseg )
210{
211    // remove vseg from radix-tree
212    grdxt_remove( &vmm->grdxt , vseg->vpn_base );
213
214    // update vseg descriptor
215    vseg->vmm = NULL;
216
217    // remove vseg from vmm list
218    list_unlink( &vseg->list );
219}
220
Note: See TracBrowser for help on using the repository browser.