| 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 | ////////////////////////////////////////// |
|---|
| 50 | char * 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 | ///////////////////// |
|---|
| 62 | vseg_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 | /////////////////////////////// |
|---|
| 74 | void 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 | /////////////////////////////////// |
|---|
| 84 | void 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 if( type == VSEG_TYPE_KCODE ) |
|---|
| 146 | { |
|---|
| 147 | vseg->flags = VSEG_EXEC | |
|---|
| 148 | VSEG_CACHE | |
|---|
| 149 | VSEG_PRIVATE ; |
|---|
| 150 | } |
|---|
| 151 | else if( type == VSEG_TYPE_KDATA ) |
|---|
| 152 | { |
|---|
| 153 | vseg->flags = VSEG_WRITE | |
|---|
| 154 | VSEG_CACHE | |
|---|
| 155 | VSEG_PRIVATE ; |
|---|
| 156 | } |
|---|
| 157 | else if( type == VSEG_TYPE_KDEV ) |
|---|
| 158 | { |
|---|
| 159 | vseg->flags = VSEG_WRITE ; |
|---|
| 160 | } |
|---|
| 161 | else |
|---|
| 162 | { |
|---|
| 163 | assert( false , __FUNCTION__ , "illegal vseg type\n" ); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | } // end vseg_init() |
|---|
| 167 | |
|---|
| 168 | ////////////////////////////////////////// |
|---|
| 169 | void vseg_init_from_ref( vseg_t * vseg, |
|---|
| 170 | xptr_t ref_xp ) |
|---|
| 171 | { |
|---|
| 172 | // get remote vseg cluster and pointer |
|---|
| 173 | cxy_t cxy = (cxy_t )GET_CXY( ref_xp ); |
|---|
| 174 | vseg_t * ptr = (vseg_t *)GET_PTR( ref_xp ); |
|---|
| 175 | |
|---|
| 176 | // initialize vseg with remote_read access |
|---|
| 177 | vseg->type = hal_remote_lw ( XPTR( cxy , &ptr->type ) ); |
|---|
| 178 | vseg->min = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->min ) ); |
|---|
| 179 | vseg->max = (intptr_t)hal_remote_lpt( XPTR( cxy , &ptr->max ) ); |
|---|
| 180 | vseg->vpn_base = hal_remote_lw ( XPTR( cxy , &ptr->vpn_base ) ); |
|---|
| 181 | vseg->vpn_size = hal_remote_lw ( XPTR( cxy , &ptr->vpn_size ) ); |
|---|
| 182 | vseg->flags = hal_remote_lw ( XPTR( cxy , &ptr->flags ) ); |
|---|
| 183 | vseg->file_offset = hal_remote_lw ( XPTR( cxy , &ptr->file_offset ) ); |
|---|
| 184 | vseg->file_size = hal_remote_lw ( XPTR( cxy , &ptr->file_size ) ); |
|---|
| 185 | vseg->mapper_xp = (xptr_t) hal_remote_lwd( XPTR( cxy , &ptr->mapper_xp ) ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /////////////////////////////// |
|---|
| 189 | void vseg_attach( vmm_t * vmm, |
|---|
| 190 | vseg_t * vseg ) |
|---|
| 191 | { |
|---|
| 192 | // update vseg descriptor |
|---|
| 193 | vseg->vmm = vmm; |
|---|
| 194 | |
|---|
| 195 | // add vseg in vmm list |
|---|
| 196 | xlist_add_last( XPTR( local_cxy , &vmm->vsegs_root ), |
|---|
| 197 | XPTR( local_cxy , &vseg->xlist ) ); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /////////////////////////////// |
|---|
| 201 | void vseg_detach( vmm_t * vmm, |
|---|
| 202 | vseg_t * vseg ) |
|---|
| 203 | { |
|---|
| 204 | // update vseg descriptor |
|---|
| 205 | vseg->vmm = NULL; |
|---|
| 206 | |
|---|
| 207 | // remove vseg from vmm list |
|---|
| 208 | xlist_unlink( XPTR( local_cxy , &vseg->xlist ) ); |
|---|
| 209 | } |
|---|
| 210 | |
|---|