[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> |
---|
| 26 | #include <hal_uspace.h> |
---|
| 27 | #include <printk.h> |
---|
| 28 | #include <process.h> |
---|
| 29 | #include <vseg.h> |
---|
| 30 | #include <kmem.h> |
---|
| 31 | #include <vfs.h> |
---|
| 32 | #include <elf.h> |
---|
[23] | 33 | #include <syscalls.h> |
---|
[1] | 34 | |
---|
| 35 | /////////////////////////////////////////////////////////////////// |
---|
| 36 | // This static function checks the .elf header. |
---|
| 37 | // - return true if legal header. |
---|
| 38 | // - return false with an error message if illegal header. |
---|
| 39 | /////////////////////////////////////////////////////////////////// |
---|
[227] | 40 | static bool_t elf_isValidHeader(Elf_Ehdr *header) |
---|
[1] | 41 | { |
---|
[270] | 42 | if((header->e_ident[EI_CLASS] == ELFCLASS) |
---|
[156] | 43 | && (header->e_ident[EI_DATA] == ELFDATA2LSB) |
---|
[1] | 44 | && (header->e_ident[EI_VERSION] == EV_CURRENT) |
---|
[270] | 45 | /* |
---|
[1] | 46 | && (header->e_ident[EI_OSABI] == ELFOSABI_NONE) |
---|
[270] | 47 | */ |
---|
[156] | 48 | && ((header->e_machine == EM_MIPS) || |
---|
[1] | 49 | (header->e_machine == EM_MIPS_RS3_LE) || |
---|
[157] | 50 | (header->e_machine == EM_X86_64)) |
---|
[156] | 51 | && (header->e_type == ET_EXEC)) |
---|
| 52 | return true; |
---|
| 53 | |
---|
[270] | 54 | if( header->e_ident[EI_CLASS] != ELFCLASS ) |
---|
| 55 | printk("\n[ERROR] in %s : Elf is not 32/64-Binary\n", __FUNCTION__ ); |
---|
[1] | 56 | |
---|
| 57 | if( header->e_ident[EI_DATA] != ELFDATA2LSB ) |
---|
[156] | 58 | printk("\n[ERROR] in %s : Elf is not 2's complement, little endian\n", __FUNCTION__ ); |
---|
[1] | 59 | |
---|
[156] | 60 | if( header->e_ident[EI_VERSION] != EV_CURRENT ) |
---|
| 61 | printk("\n[ERROR] in %s : Elf is not in Current Version\n", __FUNCTION__); |
---|
[270] | 62 | /* |
---|
[1] | 63 | if( header->e_ident[EI_OSABI] != ELFOSABI_NONE ) |
---|
[156] | 64 | printk("\n[ERROR] in %s : Unexpected Elf ABI, need UNIX System V ABI\n", __FUNCTION__ ); |
---|
[270] | 65 | */ |
---|
| 66 | if( (header->e_machine != EM_MIPS) && |
---|
| 67 | (header->e_machine != EM_MIPS_RS3_LE) && |
---|
| 68 | (header->e_machine != EM_X86_64) ) |
---|
[157] | 69 | printk("\n[ERROR] in %s : unexpected core / accept only MIPS or x86_64\n", __FUNCTION__ ); |
---|
[1] | 70 | |
---|
[270] | 71 | if( header->e_type != ET_EXEC ) |
---|
[156] | 72 | printk("\n[ERROR] in %s : Elf is not executable binary\n", __FUNCTION__ ); |
---|
[1] | 73 | |
---|
| 74 | return false; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[156] | 78 | // This function loads the .elf header in the buffer allocated by the caller. |
---|
[204] | 79 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 80 | // @ file : extended pointer on the remote file descriptor. |
---|
| 81 | // @ buffer : pointer on buffer allocated by the caller. |
---|
| 82 | // @ size : number of bytes to read. |
---|
| 83 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[204] | 84 | static error_t elf_header_load( xptr_t file_xp, |
---|
[1] | 85 | void * buffer, |
---|
| 86 | uint32_t size ) |
---|
[156] | 87 | { |
---|
[332] | 88 | error_t error; |
---|
[328] | 89 | xptr_t buf_xp; |
---|
[1] | 90 | |
---|
[337] | 91 | buf_xp = XPTR( local_cxy , buffer ); |
---|
[328] | 92 | |
---|
[156] | 93 | // load .elf header |
---|
[332] | 94 | error = vfs_kernel_move( true, // to_buffer |
---|
[328] | 95 | file_xp, |
---|
| 96 | buf_xp, |
---|
| 97 | size ); |
---|
[1] | 98 | |
---|
[332] | 99 | if( error ) |
---|
[1] | 100 | { |
---|
[332] | 101 | printk("\n[ERROR] in %s : cannot read ELF header size : %d\n", |
---|
| 102 | __FUNCTION__ , size ); |
---|
[1] | 103 | return -1; |
---|
| 104 | } |
---|
| 105 | |
---|
[227] | 106 | Elf_Ehdr * header = (Elf_Ehdr *)buffer; |
---|
[156] | 107 | |
---|
[1] | 108 | if( (header->e_ident[EI_MAG0] != ELFMAG0) || |
---|
| 109 | (header->e_ident[EI_MAG1] != ELFMAG1) || |
---|
| 110 | (header->e_ident[EI_MAG2] != ELFMAG2) || |
---|
| 111 | (header->e_ident[EI_MAG3] != ELFMAG3) ) |
---|
| 112 | { |
---|
[332] | 113 | printk("\n[ERROR] in %s : file not in ELF format\n", __FUNCTION__ ); |
---|
[1] | 114 | return -1; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | if( !(elf_isValidHeader( header ) ) ) |
---|
| 118 | { |
---|
| 119 | printk("\n[ERROR] in %s : not supported Elf\n", __FUNCTION__ ); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | return 0; |
---|
| 123 | |
---|
[204] | 124 | } // end elf_header_load() |
---|
| 125 | |
---|
[1] | 126 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
| 127 | // This function registers in the process VMM the CODE and DATA segments. |
---|
[204] | 128 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[1] | 129 | // @ file : extended pointer on the remote file descriptor. |
---|
| 130 | // @ segs_base : local pointer on buffer containing the segments descriptors array |
---|
| 131 | // @ segs_nr : number of segments in segment descriptors array. |
---|
| 132 | // @ process : local pointer on process descriptor. |
---|
| 133 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
[313] | 134 | static error_t elf_segments_register( xptr_t file_xp, |
---|
| 135 | void * segs_base, |
---|
| 136 | uint32_t nb_segs, |
---|
| 137 | process_t * process ) |
---|
[1] | 138 | { |
---|
| 139 | uint32_t index; |
---|
[313] | 140 | intptr_t file_size; |
---|
| 141 | intptr_t mem_size; |
---|
| 142 | intptr_t file_offset; |
---|
| 143 | intptr_t vbase; |
---|
[1] | 144 | uint32_t type; |
---|
| 145 | uint32_t flags; |
---|
[156] | 146 | vseg_t * vseg; |
---|
[1] | 147 | |
---|
[227] | 148 | Elf_Phdr * seg_ptr = (Elf_Phdr *)segs_base; |
---|
[1] | 149 | |
---|
[156] | 150 | // loop on segments |
---|
| 151 | for( index = 0 ; index < nb_segs ; index++ , seg_ptr++ ) |
---|
| 152 | { |
---|
| 153 | if( seg_ptr->p_type != PT_LOAD) |
---|
| 154 | continue; |
---|
| 155 | |
---|
| 156 | // get segment attributes |
---|
[313] | 157 | vbase = seg_ptr->p_vaddr; // vseg base vaddr |
---|
| 158 | mem_size = seg_ptr->p_memsz; // actual vseg size |
---|
| 159 | file_offset = seg_ptr->p_offset; // vseg offset in .elf file |
---|
| 160 | file_size = seg_ptr->p_filesz; // vseg size in .elf file |
---|
| 161 | flags = seg_ptr->p_flags; |
---|
[1] | 162 | |
---|
| 163 | if( flags & PF_X ) // found CODE segment |
---|
| 164 | { |
---|
[156] | 165 | type = VSEG_TYPE_CODE; |
---|
[315] | 166 | process->vmm.code_vpn_base = vbase >> CONFIG_PPM_PAGE_SHIFT; |
---|
[1] | 167 | |
---|
[279] | 168 | elf_dmsg("\n[INFO] %s : found CODE vseg / base = %x / size = %x\n", |
---|
[315] | 169 | __FUNCTION__ , vbase , mem_size ); |
---|
[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 | |
---|
[279] | 176 | elf_dmsg("\n[INFO] %s : found DATA vseg / base = %x / size = %x\n", |
---|
[315] | 177 | __FUNCTION__, vbase , mem_size ); |
---|
[1] | 178 | } |
---|
| 179 | |
---|
[156] | 180 | // register vseg in VMM |
---|
| 181 | vseg = (vseg_t *)vmm_create_vseg( process, |
---|
[313] | 182 | vbase, |
---|
[156] | 183 | mem_size, |
---|
| 184 | type ); |
---|
[1] | 185 | if( vseg == NULL ) |
---|
| 186 | { |
---|
| 187 | printk("\n[ERROR] in %s : cannot map segment / base = %x / size = %x\n", |
---|
[313] | 188 | __FUNCTION__ , vbase , mem_size ); |
---|
[1] | 189 | return -1; |
---|
| 190 | } |
---|
| 191 | |
---|
[313] | 192 | // get .elf file descriptor cluster and local pointer |
---|
| 193 | cxy_t file_cxy = GET_CXY( file_xp ); |
---|
| 194 | vfs_file_t * file_ptr = (vfs_file_t *)GET_PTR( file_xp ); |
---|
| 195 | |
---|
| 196 | // initialize "file_mapper", "file_offset", "file_size" fields in vseg |
---|
[315] | 197 | vseg->mapper_xp = (xptr_t)hal_remote_lwd( XPTR( file_cxy , &file_ptr->mapper ) ); |
---|
[313] | 198 | vseg->file_offset = file_offset; |
---|
| 199 | vseg->file_size = file_size; |
---|
| 200 | |
---|
| 201 | // update reference counter in file descriptor |
---|
[1] | 202 | vfs_file_count_up( file_xp ); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | return 0; |
---|
| 206 | |
---|
[313] | 207 | } // end elf_segments_register() |
---|
[204] | 208 | |
---|
[1] | 209 | /////////////////////////////////////////////// |
---|
| 210 | error_t elf_load_process( char * pathname, |
---|
| 211 | process_t * process) |
---|
| 212 | { |
---|
| 213 | kmem_req_t req; // kmem request for program header |
---|
[227] | 214 | Elf_Ehdr header; // local buffer for .elf header |
---|
[1] | 215 | void * segs_base; // pointer on buffer for segment descriptors array |
---|
[156] | 216 | uint32_t segs_size; // size of buffer for segment descriptors array |
---|
[1] | 217 | xptr_t file_xp; // extended pointer on created file descriptor |
---|
[156] | 218 | uint32_t file_id; // file descriptor index (unused) |
---|
[1] | 219 | error_t error; |
---|
| 220 | |
---|
[265] | 221 | elf_dmsg("\n[INFO] %s : enters for <%s>\n", __FUNCTION__ , pathname ); |
---|
[1] | 222 | |
---|
[204] | 223 | // avoid GCC warning |
---|
| 224 | file_xp = XPTR_NULL; |
---|
| 225 | file_id = -1; |
---|
[1] | 226 | |
---|
[156] | 227 | // open file |
---|
| 228 | error = vfs_open( process->vfs_cwd_xp, |
---|
[204] | 229 | pathname, |
---|
[156] | 230 | O_RDONLY, |
---|
| 231 | 0, |
---|
| 232 | &file_xp, |
---|
| 233 | &file_id ); |
---|
[1] | 234 | if( error ) |
---|
| 235 | { |
---|
[204] | 236 | printk("\n[ERROR] in %s : failed to open file %s\n", __FUNCTION__ , pathname ); |
---|
[1] | 237 | return -1; |
---|
| 238 | } |
---|
| 239 | |
---|
[265] | 240 | elf_dmsg("\n[INFO] %s : file <%s> open\n", __FUNCTION__ , pathname ); |
---|
[204] | 241 | |
---|
[156] | 242 | // load header in local buffer |
---|
[204] | 243 | error = elf_header_load( file_xp , |
---|
[156] | 244 | &header, |
---|
[227] | 245 | sizeof(Elf_Ehdr) ); |
---|
[156] | 246 | if( error ) |
---|
| 247 | { |
---|
[204] | 248 | printk("\n[ERROR] in %s : cannot get header file %s\n", __FUNCTION__ , pathname ); |
---|
[156] | 249 | vfs_close( file_xp , file_id ); |
---|
| 250 | return -1; |
---|
| 251 | } |
---|
[1] | 252 | |
---|
[204] | 253 | elf_dmsg("\n[INFO] %s : loaded elf header for %s\n", __FUNCTION__ , pathname ); |
---|
[1] | 254 | |
---|
| 255 | if( header.e_phnum == 0 ) |
---|
| 256 | { |
---|
| 257 | printk("\n[ERROR] in %s : no segments found\n", __FUNCTION__ ); |
---|
[156] | 258 | vfs_close( file_xp , file_id ); |
---|
[1] | 259 | return -1; |
---|
| 260 | } |
---|
| 261 | |
---|
[156] | 262 | // compute buffer size for segment descriptors array |
---|
[227] | 263 | segs_size = sizeof(Elf_Phdr) * header.e_phnum; |
---|
[156] | 264 | |
---|
| 265 | // allocate memory for segment descriptors array |
---|
[1] | 266 | req.type = KMEM_GENERIC; |
---|
| 267 | req.size = segs_size; |
---|
| 268 | req.flags = AF_KERNEL; |
---|
| 269 | segs_base = kmem_alloc( &req ); |
---|
| 270 | |
---|
| 271 | if( segs_base == NULL ) |
---|
[156] | 272 | { |
---|
[1] | 273 | printk("\n[ERROR] in %s : no memory for segment descriptors\n", __FUNCTION__ ); |
---|
[156] | 274 | vfs_close( file_xp , file_id ); |
---|
| 275 | return -1; |
---|
| 276 | } |
---|
[1] | 277 | |
---|
[156] | 278 | // set seek pointer in file descriptor to access segment descriptors array |
---|
[23] | 279 | error = vfs_lseek( file_xp , header.e_phoff, SEEK_SET , NULL ); |
---|
[1] | 280 | |
---|
| 281 | if( error ) |
---|
| 282 | { |
---|
| 283 | printk("\n[ERROR] in %s : cannot seek for descriptors array\n", __FUNCTION__ ); |
---|
[156] | 284 | vfs_close( file_xp , file_id ); |
---|
| 285 | req.ptr = segs_base; |
---|
| 286 | kmem_free( &req ); |
---|
[1] | 287 | return -1; |
---|
| 288 | } |
---|
| 289 | |
---|
[156] | 290 | // load seg descriptors array to local buffer |
---|
[333] | 291 | error = vfs_user_move( true, // to_buffer |
---|
[315] | 292 | file_xp, |
---|
| 293 | segs_base, |
---|
| 294 | segs_size ); |
---|
[1] | 295 | |
---|
[333] | 296 | if( error ) |
---|
[1] | 297 | { |
---|
| 298 | printk("\n[ERROR] in %s : cannot read segments descriptors\n", __FUNCTION__ ); |
---|
[156] | 299 | vfs_close( file_xp , file_id ); |
---|
| 300 | req.ptr = segs_base; |
---|
| 301 | kmem_free( &req ); |
---|
[1] | 302 | return -1; |
---|
| 303 | } |
---|
| 304 | |
---|
[204] | 305 | elf_dmsg("\n[INFO] %s loaded segments descriptors for %s \n", __FUNCTION__ , pathname ); |
---|
[1] | 306 | |
---|
[156] | 307 | // register loadable segments in process VMM |
---|
[313] | 308 | error = elf_segments_register( file_xp, |
---|
| 309 | segs_base, |
---|
| 310 | header.e_phnum, |
---|
| 311 | process ); |
---|
[1] | 312 | if( error ) |
---|
[156] | 313 | { |
---|
| 314 | vfs_close( file_xp , file_id ); |
---|
| 315 | req.ptr = segs_base; |
---|
| 316 | kmem_free( &req ); |
---|
[1] | 317 | return -1; |
---|
[156] | 318 | } |
---|
[1] | 319 | |
---|
[156] | 320 | // register process entry point in VMM |
---|
[1] | 321 | process->vmm.entry_point = (intptr_t)header.e_entry; |
---|
| 322 | |
---|
[156] | 323 | // register extended pointer on .elf file descriptor |
---|
| 324 | process->vfs_bin_xp = file_xp; |
---|
[1] | 325 | |
---|
[156] | 326 | // release allocated memory for program header |
---|
[1] | 327 | req.ptr = segs_base; |
---|
| 328 | kmem_free(&req); |
---|
| 329 | |
---|
[156] | 330 | elf_dmsg("\n[INFO] %s successfully completed / entry point = %x for %s]\n", |
---|
[204] | 331 | __FUNCTION__, (uint32_t) header.e_entry , pathname ); |
---|
[1] | 332 | |
---|
| 333 | return 0; |
---|
| 334 | |
---|
[204] | 335 | } // end elf_load_process() |
---|
| 336 | |
---|