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

Last change on this file since 440 was 440, checked in by alain, 6 years ago

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File size: 5.8 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_STACK  ) return "STAK";
55        else if( vseg_type == VSEG_TYPE_ANON   ) return "ANON";
56        else if( vseg_type == VSEG_TYPE_FILE   ) return "FILE";
57        else if( vseg_type == VSEG_TYPE_REMOTE ) return "REMO";
58    else                                     return "undefined";
59}
60
61/////////////////////
62vseg_t * vseg_alloc()
63{
64    kmem_req_t   req;
65
66    req.type  = KMEM_VSEG;
67        req.size  = sizeof(vseg_t);
68        req.flags = AF_KERNEL;
69
70    return (vseg_t *)kmem_alloc( &req );
71}
72
73///////////////////////////////
74void vseg_free( vseg_t * vseg )
75{
76    kmem_req_t  req;
77
78        req.type = KMEM_VSEG;
79        req.ptr  = vseg;
80        kmem_free( &req );
81}
82
83///////////////////////////////////
84void vseg_init( vseg_t      * vseg,
85                vseg_type_t   type,
86                    intptr_t      base,
87                uint32_t      size,
88                vpn_t         vpn_base,
89                vpn_t         vpn_size,
90                        uint32_t      file_offset,
91                uint32_t      file_size,
92                xptr_t        mapper_xp,
93                cxy_t         cxy )
94{
95    vseg->type        = type;
96        vseg->min         = base;
97        vseg->max         = base + size;
98    vseg->vpn_base    = vpn_base;
99        vseg->vpn_size    = vpn_size;
100    vseg->file_offset = file_offset;
101    vseg->file_size   = file_size;
102        vseg->mapper_xp   = mapper_xp;
103    vseg->cxy         = cxy;
104
105    // set vseg flags depending on type
106        if     ( type == VSEG_TYPE_CODE )
107    {
108        vseg->flags = VSEG_USER    |
109                      VSEG_EXEC    |
110                      VSEG_CACHE   |
111                      VSEG_PRIVATE ;
112    }
113    else if( type == VSEG_TYPE_STACK )
114    {
115        vseg->flags = VSEG_USER    |
116                      VSEG_WRITE   |
117                      VSEG_CACHE   |
118                      VSEG_PRIVATE ;
119    }
120    else if( type == VSEG_TYPE_DATA )
121    {
122        vseg->flags = VSEG_USER    |
123                      VSEG_WRITE   |
124                      VSEG_CACHE   |
125                      VSEG_DISTRIB ;
126    }
127    else if( type == VSEG_TYPE_REMOTE )
128    {
129        vseg->flags = VSEG_USER    |
130                      VSEG_WRITE   |
131                      VSEG_CACHE   ;
132    }
133    else if( type == VSEG_TYPE_ANON )
134    {
135        vseg->flags = VSEG_USER    |
136                      VSEG_WRITE   |
137                      VSEG_CACHE; 
138    }
139    else if( type == VSEG_TYPE_FILE )
140    {
141        vseg->flags = VSEG_USER    |
142                      VSEG_WRITE   |
143                      VSEG_CACHE   ;
144    }
145    else
146    {
147            assert( false , __FUNCTION__ , "illegal vseg type\n" );
148    }
149
150}  // end vseg_init()
151
152//////////////////////////////////////////
153void vseg_init_from_ref( vseg_t    * vseg,
154                         xptr_t      ref_xp )
155{
156    // get remote vseg cluster and pointer
157    cxy_t    cxy = (cxy_t   )GET_CXY( ref_xp );
158    vseg_t * ptr = (vseg_t *)GET_PTR( ref_xp );
159
160    // initialize vseg with remote_read access
161    vseg->type        =           hal_remote_lw ( XPTR( cxy , &ptr->type        ) );
162    vseg->min         = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min         ) );
163    vseg->max         = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max         ) );
164    vseg->vpn_base    =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_base    ) );
165    vseg->vpn_size    =           hal_remote_lw ( XPTR( cxy , &ptr->vpn_size    ) );
166    vseg->flags       =           hal_remote_lw ( XPTR( cxy , &ptr->flags       ) );
167    vseg->file_offset =           hal_remote_lw ( XPTR( cxy , &ptr->file_offset ) );
168    vseg->file_size   =           hal_remote_lw ( XPTR( cxy , &ptr->file_size   ) );
169        vseg->mapper_xp   = (xptr_t)  hal_remote_lwd( XPTR( cxy , &ptr->mapper_xp   ) );
170}
171
172///////////////////////////////
173void vseg_attach( vmm_t  * vmm,
174                  vseg_t * vseg )
175{
176    // update vseg descriptor
177    vseg->vmm = vmm;
178
179    // add vseg in vmm list
180    xlist_add_last( XPTR( local_cxy , &vmm->vsegs_root ),
181                    XPTR( local_cxy , &vseg->xlist ) );
182}
183
184///////////////////////////////
185void vseg_detach( vmm_t  * vmm,
186                  vseg_t * vseg )
187{
188    // update vseg descriptor
189    vseg->vmm = NULL;
190
191    // remove vseg from vmm list
192    xlist_unlink( XPTR( local_cxy , &vseg->xlist ) );
193}
194
Note: See TracBrowser for help on using the repository browser.