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 | |
---|
24 | #include <kernel_config.h> |
---|
25 | #include <hal_types.h> |
---|
26 | #include <hal_special.h> |
---|
27 | #include <hal_uspace.h> |
---|
28 | #include <printk.h> |
---|
29 | #include <process.h> |
---|
30 | #include <thread.h> |
---|
31 | #include <mapper.h> |
---|
32 | #include <vseg.h> |
---|
33 | #include <kmem.h> |
---|
34 | #include <vfs.h> |
---|
35 | #include <elf.h> |
---|
36 | #include <syscalls.h> |
---|
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 | /////////////////////////////////////////////////////////////////// |
---|
43 | static bool_t elf_isValidHeader(Elf_Ehdr *header) |
---|
44 | { |
---|
45 | if((header->e_ident[EI_CLASS] == ELFCLASS) |
---|
46 | && (header->e_ident[EI_DATA] == ELFDATA2LSB) |
---|
47 | && (header->e_ident[EI_VERSION] == EV_CURRENT) |
---|
48 | /* |
---|
49 | && (header->e_ident[EI_OSABI] == ELFOSABI_NONE) |
---|
50 | */ |
---|
51 | && ((header->e_machine == EM_MIPS) || |
---|
52 | (header->e_machine == EM_MIPS_RS3_LE) || |
---|
53 | (header->e_machine == EM_X86_64)) |
---|
54 | && (header->e_type == ET_EXEC)) |
---|
55 | return true; |
---|
56 | |
---|
57 | if( header->e_ident[EI_CLASS] != ELFCLASS ) |
---|
58 | printk("\n[ERROR] in %s : Elf is not 32/64-Binary\n", __FUNCTION__ ); |
---|
59 | |
---|
60 | if( header->e_ident[EI_DATA] != ELFDATA2LSB ) |
---|
61 | printk("\n[ERROR] in %s : Elf is not 2's complement, little endian\n", __FUNCTION__ ); |
---|
62 | |
---|
63 | if( header->e_ident[EI_VERSION] != EV_CURRENT ) |
---|
64 | printk("\n[ERROR] in %s : Elf is not in Current Version\n", __FUNCTION__); |
---|
65 | /* |
---|
66 | if( header->e_ident[EI_OSABI] != ELFOSABI_NONE ) |
---|
67 | printk("\n[ERROR] in %s : Unexpected Elf ABI, need UNIX System V ABI\n", __FUNCTION__ ); |
---|
68 | */ |
---|
69 | if( (header->e_machine != EM_MIPS) && |
---|
70 | (header->e_machine != EM_MIPS_RS3_LE) && |
---|
71 | (header->e_machine != EM_X86_64) ) |
---|
72 | printk("\n[ERROR] in %s : unexpected core / accept only MIPS or x86_64\n", __FUNCTION__ ); |
---|
73 | |
---|
74 | if( header->e_type != ET_EXEC ) |
---|
75 | printk("\n[ERROR] in %s : Elf is not executable binary\n", __FUNCTION__ ); |
---|
76 | |
---|
77 | return false; |
---|
78 | } |
---|
79 | |
---|
80 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
81 | // This function loads the .elf header in the buffer allocated by the caller. |
---|
82 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
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 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
87 | static error_t elf_header_load( xptr_t file_xp, |
---|
88 | void * buffer, |
---|
89 | uint32_t size ) |
---|
90 | { |
---|
91 | error_t error; |
---|
92 | xptr_t buf_xp; |
---|
93 | |
---|
94 | buf_xp = XPTR( local_cxy , buffer ); |
---|
95 | |
---|
96 | // load .elf header |
---|
97 | error = vfs_kernel_move( true, // to_buffer |
---|
98 | file_xp, |
---|
99 | buf_xp, |
---|
100 | size ); |
---|
101 | |
---|
102 | if( error ) |
---|
103 | { |
---|
104 | printk("\n[ERROR] in %s : cannot read ELF header size : %d\n", |
---|
105 | __FUNCTION__ , size ); |
---|
106 | return -1; |
---|
107 | } |
---|
108 | |
---|
109 | Elf_Ehdr * header = (Elf_Ehdr *)buffer; |
---|
110 | |
---|
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 | { |
---|
116 | printk("\n[ERROR] in %s : file not in ELF format\n", __FUNCTION__ ); |
---|
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 | |
---|
127 | } // end elf_header_load() |
---|
128 | |
---|
129 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
130 | // This function registers in the process VMM the CODE and DATA segments. |
---|
131 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
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 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
137 | static error_t elf_segments_register( xptr_t file_xp, |
---|
138 | void * segs_base, |
---|
139 | uint32_t nb_segs, |
---|
140 | process_t * process ) |
---|
141 | { |
---|
142 | uint32_t index; |
---|
143 | intptr_t file_size; |
---|
144 | intptr_t mem_size; |
---|
145 | intptr_t file_offset; |
---|
146 | intptr_t vbase; |
---|
147 | uint32_t type; |
---|
148 | uint32_t flags; |
---|
149 | vseg_t * vseg; |
---|
150 | |
---|
151 | Elf_Phdr * seg_ptr = (Elf_Phdr *)segs_base; |
---|
152 | |
---|
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 |
---|
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; |
---|
165 | |
---|
166 | if( flags & PF_X ) // found CODE segment |
---|
167 | { |
---|
168 | type = VSEG_TYPE_CODE; |
---|
169 | process->vmm.code_vpn_base = vbase >> CONFIG_PPM_PAGE_SHIFT; |
---|
170 | } |
---|
171 | else // found DATA segment |
---|
172 | { |
---|
173 | type = VSEG_TYPE_DATA; |
---|
174 | process->vmm.data_vpn_base = vbase >> CONFIG_PPM_PAGE_SHIFT; |
---|
175 | } |
---|
176 | |
---|
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 ) ); |
---|
184 | // register vseg in VMM |
---|
185 | vseg = (vseg_t *)vmm_create_vseg( process, |
---|
186 | type, |
---|
187 | vbase, |
---|
188 | mem_size, |
---|
189 | file_offset, |
---|
190 | file_size, |
---|
191 | XPTR( file_cxy , mapper_ptr ), |
---|
192 | local_cxy ); |
---|
193 | if( vseg == NULL ) |
---|
194 | { |
---|
195 | printk("\n[ERROR] in %s : cannot map segment / base = %x / size = %x\n", |
---|
196 | __FUNCTION__ , vbase , mem_size ); |
---|
197 | return -1; |
---|
198 | } |
---|
199 | |
---|
200 | // update reference counter in file descriptor |
---|
201 | vfs_file_count_up( file_xp ); |
---|
202 | |
---|
203 | #if CONFIG_DEBUG_ELF_LOAD |
---|
204 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
205 | if( CONFIG_DEBUG_ELF_LOAD < cycle ) |
---|
206 | printk("\n[DBG] %s : found %s vseg / base %x / size %x\n" |
---|
207 | " file_size %x / file_offset %x / mapper_xp %l / cycle %d\n", |
---|
208 | __FUNCTION__ , vseg_type_str(vseg->type) , vseg->min , vseg->max - vseg->min , |
---|
209 | vseg->file_size , vseg->file_offset , vseg->mapper_xp ); |
---|
210 | #endif |
---|
211 | |
---|
212 | } |
---|
213 | |
---|
214 | return 0; |
---|
215 | |
---|
216 | } // end elf_segments_register() |
---|
217 | |
---|
218 | /////////////////////////////////////////////// |
---|
219 | error_t elf_load_process( char * pathname, |
---|
220 | process_t * process) |
---|
221 | { |
---|
222 | kmem_req_t req; // kmem request for program header |
---|
223 | Elf_Ehdr header; // local buffer for .elf header |
---|
224 | void * segs_base; // pointer on buffer for segment descriptors array |
---|
225 | uint32_t segs_size; // size of buffer for segment descriptors array |
---|
226 | xptr_t file_xp; // extended pointer on created file descriptor |
---|
227 | uint32_t file_id; // file descriptor index (unused) |
---|
228 | error_t error; |
---|
229 | |
---|
230 | #if CONFIG_DEBUG_ELF_LOAD |
---|
231 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
---|
232 | if( CONFIG_DEBUG_ELF_LOAD < cycle ) |
---|
233 | printk("\n[DBG] %s : thread %d enter for <%s> / cycle %d\n", |
---|
234 | __FUNCTION__, CURRENT_THREAD, pathname, cycle ); |
---|
235 | #endif |
---|
236 | |
---|
237 | // avoid GCC warning |
---|
238 | file_xp = XPTR_NULL; |
---|
239 | file_id = -1; |
---|
240 | |
---|
241 | // open file |
---|
242 | error = vfs_open( process, |
---|
243 | pathname, |
---|
244 | O_RDONLY, |
---|
245 | 0, |
---|
246 | &file_xp, |
---|
247 | &file_id ); |
---|
248 | if( error ) |
---|
249 | { |
---|
250 | printk("\n[ERROR] in %s : failed to open file %s\n", __FUNCTION__ , pathname ); |
---|
251 | return -1; |
---|
252 | } |
---|
253 | |
---|
254 | #if (CONFIG_DEBUG_ELF_LOAD & 1) |
---|
255 | if( CONFIG_DEBUG_ELF_LOAD < cycle ) |
---|
256 | printk("\n[DBG] %s : open file <%s>\n", __FUNCTION__, pathname ); |
---|
257 | #endif |
---|
258 | |
---|
259 | // load header in local buffer |
---|
260 | error = elf_header_load( file_xp , |
---|
261 | &header, |
---|
262 | sizeof(Elf_Ehdr) ); |
---|
263 | if( error ) |
---|
264 | { |
---|
265 | printk("\n[ERROR] in %s : cannot get header file %s\n", __FUNCTION__ , pathname ); |
---|
266 | vfs_close( file_xp , file_id ); |
---|
267 | return -1; |
---|
268 | } |
---|
269 | |
---|
270 | #if (CONFIG_DEBUG_ELF_LOAD & 1) |
---|
271 | if( CONFIG_DEBUG_ELF_LOAD < cycle ) |
---|
272 | printk("\n[DBG] %s : loaded elf header for %s\n", __FUNCTION__ , pathname ); |
---|
273 | #endif |
---|
274 | |
---|
275 | if( header.e_phnum == 0 ) |
---|
276 | { |
---|
277 | printk("\n[ERROR] in %s : no segments found\n", __FUNCTION__ ); |
---|
278 | vfs_close( file_xp , file_id ); |
---|
279 | return -1; |
---|
280 | } |
---|
281 | |
---|
282 | // compute buffer size for segment descriptors array |
---|
283 | segs_size = sizeof(Elf_Phdr) * header.e_phnum; |
---|
284 | |
---|
285 | // allocate memory for segment descriptors array |
---|
286 | req.type = KMEM_GENERIC; |
---|
287 | req.size = segs_size; |
---|
288 | req.flags = AF_KERNEL; |
---|
289 | segs_base = kmem_alloc( &req ); |
---|
290 | |
---|
291 | if( segs_base == NULL ) |
---|
292 | { |
---|
293 | printk("\n[ERROR] in %s : no memory for segment descriptors\n", __FUNCTION__ ); |
---|
294 | vfs_close( file_xp , file_id ); |
---|
295 | return -1; |
---|
296 | } |
---|
297 | |
---|
298 | // set seek pointer in file descriptor to access segment descriptors array |
---|
299 | error = vfs_lseek( file_xp , header.e_phoff, SEEK_SET , NULL ); |
---|
300 | |
---|
301 | if( error ) |
---|
302 | { |
---|
303 | printk("\n[ERROR] in %s : cannot seek for descriptors array\n", __FUNCTION__ ); |
---|
304 | vfs_close( file_xp , file_id ); |
---|
305 | req.ptr = segs_base; |
---|
306 | kmem_free( &req ); |
---|
307 | return -1; |
---|
308 | } |
---|
309 | |
---|
310 | #if (CONFIG_DEBUG_ELF_LOAD & 1) |
---|
311 | if( CONFIG_DEBUG_ELF_LOAD < cycle ) |
---|
312 | printk("\n[DBG] %s : segments array allocated for %s\n", __FUNCTION__ , pathname ); |
---|
313 | #endif |
---|
314 | |
---|
315 | // load seg descriptors array to local buffer |
---|
316 | error = vfs_kernel_move( true, // to_buffer |
---|
317 | file_xp, |
---|
318 | XPTR( local_cxy , segs_base ), |
---|
319 | segs_size ); |
---|
320 | |
---|
321 | if( error ) |
---|
322 | { |
---|
323 | printk("\n[ERROR] in %s : cannot read segments descriptors\n", __FUNCTION__ ); |
---|
324 | vfs_close( file_xp , file_id ); |
---|
325 | req.ptr = segs_base; |
---|
326 | kmem_free( &req ); |
---|
327 | return -1; |
---|
328 | } |
---|
329 | |
---|
330 | #if (CONFIG_DEBUG_ELF_LOAD & 1) |
---|
331 | if( CONFIG_DEBUG_ELF_LOAD < cycle ) |
---|
332 | printk("\n[DBG] %s loaded segments descriptors for %s \n", __FUNCTION__ , pathname ); |
---|
333 | #endif |
---|
334 | |
---|
335 | // register loadable segments in process VMM |
---|
336 | error = elf_segments_register( file_xp, |
---|
337 | segs_base, |
---|
338 | header.e_phnum, |
---|
339 | process ); |
---|
340 | if( error ) |
---|
341 | { |
---|
342 | vfs_close( file_xp , file_id ); |
---|
343 | req.ptr = segs_base; |
---|
344 | kmem_free( &req ); |
---|
345 | return -1; |
---|
346 | } |
---|
347 | |
---|
348 | // register process entry point in VMM |
---|
349 | process->vmm.entry_point = (intptr_t)header.e_entry; |
---|
350 | |
---|
351 | // register extended pointer on .elf file descriptor |
---|
352 | process->vfs_bin_xp = file_xp; |
---|
353 | |
---|
354 | // release allocated memory for program header |
---|
355 | req.ptr = segs_base; |
---|
356 | kmem_free(&req); |
---|
357 | |
---|
358 | #if CONFIG_DEBUG_ELF_LOAD |
---|
359 | cycle = (uint32_t)hal_get_cycles(); |
---|
360 | if( CONFIG_DEBUG_ELF_LOAD < cycle ) |
---|
361 | printk("\n[DBG] %s : thread %d exit for <%s> / entry_point %x / cycle %d\n", |
---|
362 | __FUNCTION__, CURRENT_THREAD, pathname, header.e_entry, cycle ); |
---|
363 | #endif |
---|
364 | |
---|
365 | return 0; |
---|
366 | |
---|
367 | } // end elf_load_process() |
---|
368 | |
---|