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