| [1] | 1 | /* | 
|---|
 | 2 |  * vseg.c - virtual segment (vseg) related operations | 
|---|
 | 3 |  * | 
|---|
 | 4 |  * Authors   Ghassan Almaless (2008,2009,2010,2011, 2012) | 
|---|
 | 5 |  *           Mohamed Lamine Karaoui (2015) | 
|---|
| [623] | 6 |  *           Alain Greiner (2016,2017,2018,2019) | 
|---|
| [1] | 7 |  * | 
|---|
 | 8 |  * Copyright (c) UPMC Sorbonne Universites | 
|---|
 | 9 |  * | 
|---|
 | 10 |  * This file is part of ALMOS-MKH. | 
|---|
 | 11 |  * | 
|---|
| [184] | 12 |  * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| [1] | 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 |  * | 
|---|
| [184] | 16 |  * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| [1] | 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 | 
|---|
| [184] | 22 |  * along with ALMOS-MKH; if not, write to the Free Software Foundation, | 
|---|
| [1] | 23 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
 | 24 |  */ | 
|---|
 | 25 |  | 
|---|
| [457] | 26 | #include <hal_kernel_types.h> | 
|---|
| [1] | 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 <vfs.h> | 
|---|
 | 38 | #include <page.h> | 
|---|
 | 39 | #include <vmm.h> | 
|---|
 | 40 | #include <kmem.h> | 
|---|
 | 41 | #include <vseg.h> | 
|---|
 | 42 |  | 
|---|
 | 43 | //////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| [18] | 44 | //   global variables for display / must be consistent with enum in "vseg.h" | 
|---|
| [1] | 45 | //////////////////////////////////////////////////////////////////////////////////////// | 
|---|
 | 46 |  | 
|---|
| [101] | 47 |  | 
|---|
 | 48 | ////////////////////////////////////////// | 
|---|
| [184] | 49 | char * vseg_type_str( uint32_t vseg_type ) | 
|---|
| [1] | 50 | { | 
|---|
| [101] | 51 |         if     ( vseg_type == VSEG_TYPE_CODE   ) return "CODE"; | 
|---|
 | 52 |         else if( vseg_type == VSEG_TYPE_DATA   ) return "DATA"; | 
|---|
| [407] | 53 |         else if( vseg_type == VSEG_TYPE_STACK  ) return "STAK"; | 
|---|
| [101] | 54 |         else if( vseg_type == VSEG_TYPE_ANON   ) return "ANON"; | 
|---|
 | 55 |         else if( vseg_type == VSEG_TYPE_FILE   ) return "FILE"; | 
|---|
| [407] | 56 |         else if( vseg_type == VSEG_TYPE_REMOTE ) return "REMO"; | 
|---|
| [623] | 57 |         else if( vseg_type == VSEG_TYPE_KCODE  ) return "KCOD"; | 
|---|
 | 58 |         else if( vseg_type == VSEG_TYPE_KDATA  ) return "KDAT"; | 
|---|
 | 59 |         else if( vseg_type == VSEG_TYPE_KDEV   ) return "KDEV"; | 
|---|
| [101] | 60 |     else                                     return "undefined"; | 
|---|
 | 61 | } | 
|---|
| [1] | 62 |  | 
|---|
| [625] | 63 | /////////////////////////// | 
|---|
| [503] | 64 | vseg_t * vseg_alloc( void ) | 
|---|
| [1] | 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 | /////////////////////////////// | 
|---|
 | 76 | void 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 | /////////////////////////////////// | 
|---|
 | 86 | void vseg_init( vseg_t      * vseg, | 
|---|
| [407] | 87 |                 vseg_type_t   type, | 
|---|
| [18] | 88 |                     intptr_t      base, | 
|---|
| [407] | 89 |                 uint32_t      size, | 
|---|
| [1] | 90 |                 vpn_t         vpn_base, | 
|---|
 | 91 |                 vpn_t         vpn_size, | 
|---|
| [407] | 92 |                         uint32_t      file_offset, | 
|---|
 | 93 |                 uint32_t      file_size, | 
|---|
 | 94 |                 xptr_t        mapper_xp, | 
|---|
| [315] | 95 |                 cxy_t         cxy ) | 
|---|
| [1] | 96 | { | 
|---|
| [407] | 97 |     vseg->type        = type; | 
|---|
 | 98 |         vseg->min         = base; | 
|---|
 | 99 |         vseg->max         = base + size; | 
|---|
 | 100 |     vseg->vpn_base    = vpn_base; | 
|---|
 | 101 |         vseg->vpn_size    = vpn_size; | 
|---|
 | 102 |     vseg->file_offset = file_offset; | 
|---|
 | 103 |     vseg->file_size   = file_size; | 
|---|
 | 104 |         vseg->mapper_xp   = mapper_xp; | 
|---|
 | 105 |     vseg->cxy         = cxy; | 
|---|
| [1] | 106 |  | 
|---|
 | 107 |     // set vseg flags depending on type | 
|---|
| [18] | 108 |         if     ( type == VSEG_TYPE_CODE ) | 
|---|
| [1] | 109 |     { | 
|---|
 | 110 |         vseg->flags = VSEG_USER    | | 
|---|
| [18] | 111 |                       VSEG_EXEC    | | 
|---|
| [1] | 112 |                       VSEG_CACHE   | | 
|---|
 | 113 |                       VSEG_PRIVATE ; | 
|---|
 | 114 |     } | 
|---|
 | 115 |     else if( type == VSEG_TYPE_STACK ) | 
|---|
 | 116 |     { | 
|---|
 | 117 |         vseg->flags = VSEG_USER    | | 
|---|
 | 118 |                       VSEG_WRITE   | | 
|---|
 | 119 |                       VSEG_CACHE   | | 
|---|
| [18] | 120 |                       VSEG_PRIVATE ; | 
|---|
| [1] | 121 |     } | 
|---|
| [18] | 122 |     else if( type == VSEG_TYPE_DATA ) | 
|---|
| [1] | 123 |     { | 
|---|
 | 124 |         vseg->flags = VSEG_USER    | | 
|---|
 | 125 |                       VSEG_WRITE   | | 
|---|
| [18] | 126 |                       VSEG_CACHE   | | 
|---|
| [1] | 127 |                       VSEG_DISTRIB ; | 
|---|
 | 128 |     } | 
|---|
| [18] | 129 |     else if( type == VSEG_TYPE_REMOTE ) | 
|---|
| [1] | 130 |     { | 
|---|
 | 131 |         vseg->flags = VSEG_USER    | | 
|---|
| [18] | 132 |                       VSEG_WRITE   | | 
|---|
 | 133 |                       VSEG_CACHE   ; | 
|---|
| [1] | 134 |     } | 
|---|
| [18] | 135 |     else if( type == VSEG_TYPE_ANON ) | 
|---|
| [1] | 136 |     { | 
|---|
 | 137 |         vseg->flags = VSEG_USER    | | 
|---|
 | 138 |                       VSEG_WRITE   | | 
|---|
| [407] | 139 |                       VSEG_CACHE;  | 
|---|
| [1] | 140 |     } | 
|---|
| [18] | 141 |     else if( type == VSEG_TYPE_FILE ) | 
|---|
| [1] | 142 |     { | 
|---|
 | 143 |         vseg->flags = VSEG_USER    | | 
|---|
 | 144 |                       VSEG_WRITE   | | 
|---|
 | 145 |                       VSEG_CACHE   ; | 
|---|
 | 146 |     } | 
|---|
| [623] | 147 |     else if( type == VSEG_TYPE_KCODE ) | 
|---|
 | 148 |     { | 
|---|
 | 149 |         vseg->flags = VSEG_EXEC    | | 
|---|
 | 150 |                       VSEG_CACHE   | | 
|---|
 | 151 |                       VSEG_PRIVATE ; | 
|---|
 | 152 |     } | 
|---|
 | 153 |     else if( type == VSEG_TYPE_KDATA ) | 
|---|
 | 154 |     { | 
|---|
 | 155 |         vseg->flags = VSEG_CACHE   | | 
|---|
 | 156 |                       VSEG_WRITE   ; | 
|---|
 | 157 |     } | 
|---|
 | 158 |     else if( type == VSEG_TYPE_KDEV ) | 
|---|
 | 159 |     { | 
|---|
 | 160 |         vseg->flags = VSEG_WRITE   ; | 
|---|
 | 161 |     } | 
|---|
| [18] | 162 |     else | 
|---|
| [1] | 163 |     { | 
|---|
| [492] | 164 |             assert( false , "illegal vseg type\n" ); | 
|---|
| [18] | 165 |     } | 
|---|
| [1] | 166 |  | 
|---|
| [315] | 167 | }  // end vseg_init() | 
|---|
 | 168 |  | 
|---|
| [1] | 169 | ////////////////////////////////////////// | 
|---|
 | 170 | void vseg_init_from_ref( vseg_t    * vseg, | 
|---|
| [407] | 171 |                          xptr_t      ref_xp ) | 
|---|
| [1] | 172 | { | 
|---|
 | 173 |     // get remote vseg cluster and pointer | 
|---|
| [407] | 174 |     cxy_t    cxy = (cxy_t   )GET_CXY( ref_xp ); | 
|---|
 | 175 |     vseg_t * ptr = (vseg_t *)GET_PTR( ref_xp ); | 
|---|
| [1] | 176 |  | 
|---|
 | 177 |     // initialize vseg with remote_read access | 
|---|
| [623] | 178 |     vseg->type        =           hal_remote_l32( XPTR( cxy , &ptr->type        ) ); | 
|---|
| [407] | 179 |     vseg->min         = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min         ) ); | 
|---|
 | 180 |     vseg->max         = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max         ) ); | 
|---|
| [623] | 181 |     vseg->vpn_base    =           hal_remote_l32( XPTR( cxy , &ptr->vpn_base    ) ); | 
|---|
 | 182 |     vseg->vpn_size    =           hal_remote_l32( XPTR( cxy , &ptr->vpn_size    ) ); | 
|---|
 | 183 |     vseg->flags       =           hal_remote_l32( XPTR( cxy , &ptr->flags       ) ); | 
|---|
 | 184 |     vseg->file_offset =           hal_remote_l32( XPTR( cxy , &ptr->file_offset ) ); | 
|---|
 | 185 |     vseg->file_size   =           hal_remote_l32( XPTR( cxy , &ptr->file_size   ) ); | 
|---|
| [567] | 186 |         vseg->mapper_xp   = (xptr_t)  hal_remote_l64( XPTR( cxy , &ptr->mapper_xp   ) ); | 
|---|
| [453] | 187 |  | 
|---|
 | 188 |     switch (vseg->type) | 
|---|
 | 189 |     { | 
|---|
| [623] | 190 |         case VSEG_TYPE_DATA:      // unused  | 
|---|
| [457] | 191 |         { | 
|---|
| [453] | 192 |             vseg->cxy = 0xffff; | 
|---|
 | 193 |             break; | 
|---|
 | 194 |         } | 
|---|
| [623] | 195 |         case VSEG_TYPE_CODE:      // always local | 
|---|
| [457] | 196 |         case VSEG_TYPE_STACK:  | 
|---|
| [623] | 197 |         case VSEG_TYPE_KCODE: | 
|---|
| [457] | 198 |         { | 
|---|
| [453] | 199 |             vseg->cxy = local_cxy; | 
|---|
 | 200 |             break; | 
|---|
 | 201 |         } | 
|---|
| [623] | 202 |         case VSEG_TYPE_ANON:      // intrinsic | 
|---|
| [453] | 203 |         case VSEG_TYPE_FILE: | 
|---|
| [457] | 204 |         case VSEG_TYPE_REMOTE:  | 
|---|
| [623] | 205 |         case VSEG_TYPE_KDEV: | 
|---|
 | 206 |         case VSEG_TYPE_KDATA: | 
|---|
| [457] | 207 |         { | 
|---|
| [567] | 208 |             vseg->cxy = (cxy_t) hal_remote_l32( XPTR(cxy, &ptr->cxy) ); | 
|---|
| [453] | 209 |             break; | 
|---|
 | 210 |         } | 
|---|
| [457] | 211 |         default:  | 
|---|
 | 212 |         { | 
|---|
| [492] | 213 |             assert( false, "Illegal vseg type" ); | 
|---|
| [453] | 214 |             break; | 
|---|
 | 215 |         } | 
|---|
 | 216 |     } | 
|---|
| [184] | 217 | } | 
|---|
| [1] | 218 |  | 
|---|
 | 219 |  | 
|---|