[1] | 1 | /* |
---|
| 2 | * elf.c - elf parser: find and map process CODE and DATA segments |
---|
| 3 | * |
---|
| 4 | * Authors Alain Greiner (2016) |
---|
| 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[14] | 24 | #include <kernel_config.h> |
---|
[1] | 25 | #include <hal_types.h> |
---|
[406] | 26 | #include <hal_special.h> |
---|
[1] | 27 | #include <hal_uspace.h> |
---|
| 28 | #include <printk.h> |
---|
| 29 | #include <process.h> |
---|
[406] | 30 | #include <thread.h> |
---|
| 31 | #include <mapper.h> |
---|
[1] | 32 | #include <vseg.h> |
---|
| 33 | #include <kmem.h> |
---|
| 34 | #include <vfs.h> |
---|
| 35 | #include <elf.h> |
---|
[23] | 36 | #include <syscalls.h> |
---|
[1] | 37 | |
---|
| 38 | /////////////////////////////////////////////////////////////////// |
---|
| 39 | // This static function checks the .elf header. |
---|
| 40 | // - return true if legal header. |
---|
| 41 | // - return false with an error message if illegal header. |
---|
| 42 | /////////////////////////////////////////////////////////////////// |
---|
[227] | 43 | static bool_t elf_isValidHeader(Elf_Ehdr *header) |
---|
[1] | 44 | { |
---|
[270] | 45 | if((header->e_ident[EI_CLASS] == ELFCLASS) |
---|
[156] | 46 | && (header->e_ident[EI_DATA] == ELFDATA2LSB) |
---|
[1] | 47 | && (header->e_ident[EI_VERSION] == EV_CURRENT) |
---|
[270] | 48 | /* |
---|
[1] | 49 | && (header->e_ident[EI_OSABI] == ELFOSABI_NONE) |
---|
[270] | 50 | */ |
---|
[156] | 51 | && ((header->e_machine == EM_MIPS) || |
---|
[1] | 52 | (header->e_machine == EM_MIPS_RS3_LE) || |
---|
[157] | 53 | (header->e_machine == EM_X86_64)) |
---|
[156] | 54 | && (header->e_type == ET_EXEC)) |
---|
| 55 | return true; |
---|
| 56 | |
---|
[270] | 57 | if( header->e_ident[EI_CLASS] != ELFCLASS ) |
---|
| 58 | printk("\n[ERROR] in %s : Elf is not 32/64-Binary\n", __FUNCTION__ ); |
---|
[1] | 59 | |
---|
| 60 | if( header->e_ident[EI_DATA] != ELFDATA2LSB ) |
---|
[156] | 61 | printk("\n[ERROR] in %s : Elf is not 2's complement, little endian\n", __FUNCTION__ ); |
---|
[1] | 62 | |
---|
[156] | 63 | if( header->e_ident[EI_VERSION] != EV_CURRENT ) |
---|
| 64 | printk("\n[ERROR] in %s : Elf is not in Current Version\n", __FUNCTION__); |
---|
[270] | 65 | /* |
---|
[1] | 66 | if( header->e_ident[EI_OSABI] != ELFOSABI_NONE ) |
---|
[156] | 67 | printk("\n[ERROR] in %s : Unexpected Elf ABI, need UNIX System V ABI\n", __FUNCTION__ ); |
---|
[270] | 68 | */ |
---|
| 69 | if( (header->e_machine != EM_MIPS) && |
---|
| 70 | (header->e_machine != EM_MIPS_RS3_LE) && |
---|
| 71 | (header->e_machine != EM_X86_64) ) |
---|
[157] | 72 | printk("\n[ERROR] in %s : unexpected core / accept only MIPS or x86_64\n", __FUNCTION__ ); |
---|
[1] | 73 | |
---|
[270] | 74 | if( header->e_type != ET_EXEC ) |
---|
[156] | 75 | printk("\n[ERROR] in %s : Elf is not executable binary\n", __FUNCTION__ ); |
---|
[1] | 76 | |
---|
| 77 | return false; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[156] | 81 | // This function loads the .elf header in the buffer allocated by the caller. |
---|
[204] | 82 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 83 | // @ file : extended pointer on the remote file descriptor. |
---|
| 84 | // @ buffer : pointer on buffer allocated by the caller. |
---|
| 85 | // @ size : number of bytes to read. |
---|
| 86 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[204] | 87 | static error_t elf_header_load( xptr_t file_xp, |
---|
[1] | 88 | void * buffer, |
---|
| 89 | uint32_t size ) |
---|
[156] | 90 | { |
---|
[332] | 91 | error_t error; |
---|
[328] | 92 | xptr_t buf_xp; |
---|
[1] | 93 | |
---|
[337] | 94 | buf_xp = XPTR( local_cxy , buffer ); |
---|
[328] | 95 | |
---|
[156] | 96 | // load .elf header |
---|
[332] | 97 | error = vfs_kernel_move( true, // to_buffer |
---|
[328] | 98 | file_xp, |
---|
| 99 | buf_xp, |
---|
| 100 | size ); |
---|
[1] | 101 | |
---|
[332] | 102 | if( error ) |
---|
[1] | 103 | { |
---|
[332] | 104 | printk("\n[ERROR] in %s : cannot read ELF header size : %d\n", |
---|
| 105 | __FUNCTION__ , size ); |
---|
[1] | 106 | return -1; |
---|
| 107 | } |
---|
| 108 | |
---|
[227] | 109 | Elf_Ehdr * header = (Elf_Ehdr *)buffer; |
---|
[156] | 110 | |
---|
[1] | 111 | if( (header->e_ident[EI_MAG0] != ELFMAG0) || |
---|
| 112 | (header->e_ident[EI_MAG1] != ELFMAG1) || |
---|
| 113 | (header->e_ident[EI_MAG2] != ELFMAG2) || |
---|
| 114 | (header->e_ident[EI_MAG3] != ELFMAG3) ) |
---|
| 115 | { |
---|
[332] | 116 | printk("\n[ERROR] in %s : file not in ELF format\n", __FUNCTION__ ); |
---|
[1] | 117 | return -1; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | if( !(elf_isValidHeader( header ) ) ) |
---|
| 121 | { |
---|
| 122 | printk("\n[ERROR] in %s : not supported Elf\n", __FUNCTION__ ); |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
| 125 | return 0; |
---|
| 126 | |
---|
[204] | 127 | } // end elf_header_load() |
---|
| 128 | |
---|
[1] | 129 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 130 | // This function registers in the process VMM the CODE and DATA segments. |
---|
[204] | 131 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 132 | // @ file : extended pointer on the remote file descriptor. |
---|
| 133 | // @ segs_base : local pointer on buffer containing the segments descriptors array |
---|
| 134 | // @ segs_nr : number of segments in segment descriptors array. |
---|
| 135 | // @ process : local pointer on process descriptor. |
---|
| 136 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[313] | 137 | static error_t elf_segments_register( xptr_t file_xp, |
---|
| 138 | void * segs_base, |
---|
| 139 | uint32_t nb_segs, |
---|
| 140 | process_t * process ) |
---|
[1] | 141 | { |
---|
| 142 | uint32_t index; |
---|
[313] | 143 | intptr_t file_size; |
---|
| 144 | intptr_t mem_size; |
---|
| 145 | intptr_t file_offset; |
---|
| 146 | intptr_t vbase; |
---|
[1] | 147 | uint32_t type; |
---|
| 148 | uint32_t flags; |
---|
[156] | 149 | vseg_t * vseg; |
---|
[1] | 150 | |
---|
[227] | 151 | Elf_Phdr * seg_ptr = (Elf_Phdr *)segs_base; |
---|
[1] | 152 | |
---|
[156] | 153 | // loop on segments |
---|
| 154 | for( index = 0 ; index < nb_segs ; index++ , seg_ptr++ ) |
---|
| 155 | { |
---|
| 156 | if( seg_ptr->p_type != PT_LOAD) |
---|
| 157 | continue; |
---|
| 158 | |
---|
| 159 | // get segment attributes |
---|
[313] | 160 | vbase = seg_ptr->p_vaddr; // vseg base vaddr |
---|
| 161 | mem_size = seg_ptr->p_memsz; // actual vseg size |
---|
| 162 | file_offset = seg_ptr->p_offset; // vseg offset in .elf file |
---|
| 163 | file_size = seg_ptr->p_filesz; // vseg size in .elf file |
---|
| 164 | flags = seg_ptr->p_flags; |
---|
[1] | 165 | |
---|
| 166 | if( flags & PF_X ) // found CODE segment |
---|
| 167 | { |
---|
[156] | 168 | type = VSEG_TYPE_CODE; |
---|
[315] | 169 | process->vmm.code_vpn_base = vbase >> CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 170 | } |
---|
[156] | 171 | else // found DATA segment |
---|
[1] | 172 | { |
---|
[156] | 173 | type = VSEG_TYPE_DATA; |
---|
[315] | 174 | process->vmm.data_vpn_base = vbase >> CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 175 | } |
---|
| 176 | |
---|
[407] | 177 | // get .elf file descriptor cluster and local pointer |
---|
| 178 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 179 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 180 | |
---|
| 181 | // get local pointer on .elf file mapper |
---|
| 182 | mapper_t * mapper_ptr = (mapper_t *)hal_remote_lpt( XPTR( file_cxy , |
---|
| 183 | &file_ptr->mapper ) ); |
---|
[156] | 184 | // register vseg in VMM |
---|
| 185 | vseg = (vseg_t *)vmm_create_vseg( process, |
---|
[407] | 186 | type, |
---|
[313] | 187 | vbase, |
---|
[156] | 188 | mem_size, |
---|
[407] | 189 | file_offset, |
---|
| 190 | file_size, |
---|
| 191 | XPTR( file_cxy , mapper_ptr ), |
---|
| 192 | local_cxy ); |
---|
[1] | 193 | if( vseg == NULL ) |
---|
| 194 | { |
---|
| 195 | printk("\n[ERROR] in %s : cannot map segment / base = %x / size = %x\n", |
---|
[313] | 196 | __FUNCTION__ , vbase , mem_size ); |
---|
[1] | 197 | return -1; |
---|
| 198 | } |
---|
| 199 | |
---|
[313] | 200 | // update reference counter in file descriptor |
---|
[1] | 201 | vfs_file_count_up( file_xp ); |
---|
[406] | 202 | |
---|
[407] | 203 | elf_dmsg("\n[DBG] %s : found %s vseg / base = %x / size = %x\n" |
---|
[406] | 204 | " file_size = %x / file_offset = %x / mapper_xp = %l\n", |
---|
| 205 | __FUNCTION__ , vseg_type_str(vseg->type) , vseg->min , vseg->max - vseg->min , |
---|
| 206 | vseg->file_size , vseg->file_offset , vseg->mapper_xp ); |
---|
[1] | 207 | } |
---|
| 208 | |
---|
| 209 | return 0; |
---|
| 210 | |
---|
[313] | 211 | } // end elf_segments_register() |
---|
[204] | 212 | |
---|
[1] | 213 | /////////////////////////////////////////////// |
---|
| 214 | error_t elf_load_process( char * pathname, |
---|
| 215 | process_t * process) |
---|
| 216 | { |
---|
| 217 | kmem_req_t req; // kmem request for program header |
---|
[227] | 218 | Elf_Ehdr header; // local buffer for .elf header |
---|
[1] | 219 | void * segs_base; // pointer on buffer for segment descriptors array |
---|
[156] | 220 | uint32_t segs_size; // size of buffer for segment descriptors array |
---|
[1] | 221 | xptr_t file_xp; // extended pointer on created file descriptor |
---|
[156] | 222 | uint32_t file_id; // file descriptor index (unused) |
---|
[1] | 223 | error_t error; |
---|
| 224 | |
---|
[407] | 225 | elf_dmsg("\n[DBG] %s : core[%x,%d] enter for <%s>\n", |
---|
[401] | 226 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , pathname ); |
---|
[1] | 227 | |
---|
[204] | 228 | // avoid GCC warning |
---|
| 229 | file_xp = XPTR_NULL; |
---|
| 230 | file_id = -1; |
---|
[1] | 231 | |
---|
[156] | 232 | // open file |
---|
[407] | 233 | error = vfs_open( process, |
---|
[204] | 234 | pathname, |
---|
[156] | 235 | O_RDONLY, |
---|
| 236 | 0, |
---|
| 237 | &file_xp, |
---|
| 238 | &file_id ); |
---|
[1] | 239 | if( error ) |
---|
| 240 | { |
---|
[204] | 241 | printk("\n[ERROR] in %s : failed to open file %s\n", __FUNCTION__ , pathname ); |
---|
[1] | 242 | return -1; |
---|
| 243 | } |
---|
| 244 | |
---|
[407] | 245 | elf_dmsg("\n[DBG] %s : open file <%s>\n", __FUNCTION__ , pathname ); |
---|
[204] | 246 | |
---|
[156] | 247 | // load header in local buffer |
---|
[204] | 248 | error = elf_header_load( file_xp , |
---|
[156] | 249 | &header, |
---|
[227] | 250 | sizeof(Elf_Ehdr) ); |
---|
[156] | 251 | if( error ) |
---|
| 252 | { |
---|
[204] | 253 | printk("\n[ERROR] in %s : cannot get header file %s\n", __FUNCTION__ , pathname ); |
---|
[156] | 254 | vfs_close( file_xp , file_id ); |
---|
| 255 | return -1; |
---|
| 256 | } |
---|
[1] | 257 | |
---|
[407] | 258 | elf_dmsg("\n[DBG] %s : loaded elf header for %s\n", __FUNCTION__ , pathname ); |
---|
[1] | 259 | |
---|
| 260 | if( header.e_phnum == 0 ) |
---|
| 261 | { |
---|
| 262 | printk("\n[ERROR] in %s : no segments found\n", __FUNCTION__ ); |
---|
[156] | 263 | vfs_close( file_xp , file_id ); |
---|
[1] | 264 | return -1; |
---|
| 265 | } |
---|
| 266 | |
---|
[156] | 267 | // compute buffer size for segment descriptors array |
---|
[227] | 268 | segs_size = sizeof(Elf_Phdr) * header.e_phnum; |
---|
[156] | 269 | |
---|
| 270 | // allocate memory for segment descriptors array |
---|
[1] | 271 | req.type = KMEM_GENERIC; |
---|
| 272 | req.size = segs_size; |
---|
| 273 | req.flags = AF_KERNEL; |
---|
| 274 | segs_base = kmem_alloc( &req ); |
---|
| 275 | |
---|
| 276 | if( segs_base == NULL ) |
---|
[156] | 277 | { |
---|
[1] | 278 | printk("\n[ERROR] in %s : no memory for segment descriptors\n", __FUNCTION__ ); |
---|
[156] | 279 | vfs_close( file_xp , file_id ); |
---|
| 280 | return -1; |
---|
| 281 | } |
---|
[1] | 282 | |
---|
[156] | 283 | // set seek pointer in file descriptor to access segment descriptors array |
---|
[23] | 284 | error = vfs_lseek( file_xp , header.e_phoff, SEEK_SET , NULL ); |
---|
[1] | 285 | |
---|
| 286 | if( error ) |
---|
| 287 | { |
---|
| 288 | printk("\n[ERROR] in %s : cannot seek for descriptors array\n", __FUNCTION__ ); |
---|
[156] | 289 | vfs_close( file_xp , file_id ); |
---|
| 290 | req.ptr = segs_base; |
---|
| 291 | kmem_free( &req ); |
---|
[1] | 292 | return -1; |
---|
| 293 | } |
---|
| 294 | |
---|
[407] | 295 | elf_dmsg("\n[DBG] %s : segments array allocated for %s\n", __FUNCTION__ , pathname ); |
---|
[367] | 296 | |
---|
[156] | 297 | // load seg descriptors array to local buffer |
---|
[367] | 298 | error = vfs_kernel_move( true, // to_buffer |
---|
| 299 | file_xp, |
---|
| 300 | XPTR( local_cxy , segs_base ), |
---|
| 301 | segs_size ); |
---|
[1] | 302 | |
---|
[333] | 303 | if( error ) |
---|
[1] | 304 | { |
---|
| 305 | printk("\n[ERROR] in %s : cannot read segments descriptors\n", __FUNCTION__ ); |
---|
[156] | 306 | vfs_close( file_xp , file_id ); |
---|
| 307 | req.ptr = segs_base; |
---|
| 308 | kmem_free( &req ); |
---|
[1] | 309 | return -1; |
---|
| 310 | } |
---|
| 311 | |
---|
[407] | 312 | elf_dmsg("\n[DBG] %s loaded segments descriptors for %s \n", __FUNCTION__ , pathname ); |
---|
[1] | 313 | |
---|
[156] | 314 | // register loadable segments in process VMM |
---|
[313] | 315 | error = elf_segments_register( file_xp, |
---|
| 316 | segs_base, |
---|
| 317 | header.e_phnum, |
---|
| 318 | process ); |
---|
[1] | 319 | if( error ) |
---|
[156] | 320 | { |
---|
| 321 | vfs_close( file_xp , file_id ); |
---|
| 322 | req.ptr = segs_base; |
---|
| 323 | kmem_free( &req ); |
---|
[1] | 324 | return -1; |
---|
[156] | 325 | } |
---|
[1] | 326 | |
---|
[156] | 327 | // register process entry point in VMM |
---|
[1] | 328 | process->vmm.entry_point = (intptr_t)header.e_entry; |
---|
| 329 | |
---|
[156] | 330 | // register extended pointer on .elf file descriptor |
---|
| 331 | process->vfs_bin_xp = file_xp; |
---|
[1] | 332 | |
---|
[156] | 333 | // release allocated memory for program header |
---|
[1] | 334 | req.ptr = segs_base; |
---|
| 335 | kmem_free(&req); |
---|
| 336 | |
---|
[407] | 337 | elf_dmsg("\n[DBG] %s : core[%x,%d] exit for <%s> / entry_point = %x\n", |
---|
[401] | 338 | __FUNCTION__ , local_cxy , CURRENT_THREAD->core->lid , pathname , header.e_entry ); |
---|
[1] | 339 | |
---|
| 340 | return 0; |
---|
| 341 | |
---|
[204] | 342 | } // end elf_load_process() |
---|
| 343 | |
---|