4 #define BLOCK_SIZE 1024
6 static void do_exec(int proc_e
, char *exec
, size_t exec_len
, char *progname
,
7 char *frame
, int frame_len
);
8 FORWARD
_PROTOTYPE( int read_header
, (char *exec
, size_t exec_len
, int *sep_id
,
9 vir_bytes
*text_bytes
, vir_bytes
*data_bytes
,
10 vir_bytes
*bss_bytes
, phys_bytes
*tot_bytes
, vir_bytes
*pc
,
12 FORWARD
_PROTOTYPE( int exec_newmem
, (int proc_e
, vir_bytes text_bytes
,
13 vir_bytes data_bytes
, vir_bytes bss_bytes
, vir_bytes tot_bytes
,
14 vir_bytes frame_len
, int sep_id
,
15 Dev_t st_dev
, ino_t st_ino
, time_t st_ctime
, char *progname
,
16 int new_uid
, int new_gid
,
17 vir_bytes
*stack_topp
, int *load_textp
, int *allow_setuidp
) );
18 FORWARD
_PROTOTYPE( int exec_restart
, (int proc_e
, int result
) );
19 FORWARD
_PROTOTYPE( void patch_ptr
, (char stack
[ARG_MAX
],
21 FORWARD
_PROTOTYPE( int read_seg
, (char *exec
, size_t exec_len
, off_t off
,
22 int proc_e
, int seg
, phys_bytes seg_bytes
) );
24 static int self_e
= NONE
;
26 int dev_execve(int proc_e
, char *exec
, size_t exec_len
, char **argv
,
41 /* Assumptions: size_t and char *, it's all the same thing. */
43 /* Create a stack image that only needs to be patched up slightly
44 * by the kernel to be used for the process to be executed.
47 ov
= 0; /* No overflow yet. */
48 frame_size
= 0; /* Size of the new initial stack. */
49 string_off
= 0; /* Offset to start of the strings. */
50 argc
= 0; /* Argument count. */
52 for (ap
= argv
; *ap
!= NULL
; ap
++) {
53 n
= sizeof(*ap
) + strlen(*ap
) + 1;
55 if (frame_size
< n
) ov
= 1;
56 string_off
+= sizeof(*ap
);
61 printf("here: %s, %d\n", __FILE__
, __LINE__
);
62 for (ep
= envp
; *ep
!= NULL
; ep
++) {
63 n
= sizeof(*ep
) + strlen(*ep
) + 1;
65 if (frame_size
< n
) ov
= 1;
66 string_off
+= sizeof(*ap
);
70 /* Add an argument count and two terminating nulls. */
71 frame_size
+= sizeof(argc
) + sizeof(*ap
) + sizeof(*ep
);
72 string_off
+= sizeof(argc
) + sizeof(*ap
) + sizeof(*ep
);
75 frame_size
= (frame_size
+ sizeof(char *) - 1) & ~(sizeof(char *) - 1);
77 /* The party is off if there is an overflow. */
78 if (ov
|| frame_size
< 3 * sizeof(char *)) {
83 /* Allocate space for the stack frame. */
84 if ((frame
= (char *) sbrk(frame_size
)) == (char *) -1) {
89 /* Set arg count, init pointers to vector and string tables. */
90 * (size_t *) frame
= argc
;
91 vp
= (char **) (frame
+ sizeof(argc
));
92 sp
= frame
+ string_off
;
94 /* Load the argument vector and strings. */
95 for (ap
= argv
; *ap
!= NULL
; ap
++) {
96 *vp
++= (char *) (sp
- frame
);
104 /* Load the environment vector and strings. */
105 for (ep
= envp
; *ep
!= NULL
; ep
++) {
106 *vp
++= (char *) (sp
- frame
);
115 while (sp
< frame
+ frame_size
) *sp
++= 0;
117 (progname
=strrchr(argv
[0], '/')) ? progname
++ : (progname
=argv
[0]);
118 do_exec(proc_e
, exec
, exec_len
, progname
, frame
, frame_size
);
120 /* Failure, return the memory used for the frame and exit. */
121 (void) sbrk(-frame_size
);
125 static void do_exec(int proc_e
, char *exec
, size_t exec_len
, char *progname
,
126 char *frame
, int frame_len
)
129 int hdrlen
, sep_id
, load_text
, allow_setuid
;
130 int need_restart
, error
;
131 vir_bytes stack_top
, vsp
;
132 vir_bytes text_bytes
, data_bytes
, bss_bytes
, pc
;
133 phys_bytes tot_bytes
;
141 self_e
= getnprocnr(getpid());
143 /* Read the file header and extract the segment sizes. */
144 r
= read_header(exec
, exec_len
, &sep_id
,
145 &text_bytes
, &data_bytes
, &bss_bytes
,
146 &tot_bytes
, &pc
, &hdrlen
);
149 printf("do_exec: read_header failed\n");
156 /* XXX what should we use to identify the executable? */
157 r
= exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
158 frame_len
, sep_id
, 0 /*dev*/, proc_e
/*inum*/, 0 /*ctime*/,
159 progname
, new_uid
, new_gid
, &stack_top
, &load_text
,
163 printf("do_exec: exec_newmap failed: %d\n", r
);
168 /* Patch up stack and copy it from FS to new core image. */
171 patch_ptr(frame
, vsp
);
172 r
= sys_datacopy(SELF
, (vir_bytes
) frame
,
173 proc_e
, (vir_bytes
) vsp
, (phys_bytes
)frame_len
);
174 if (r
!= OK
) panic(__FILE__
,"pm_exec stack copy err on", proc_e
);
178 /* Read in text and data segments. */
180 r
= read_seg(exec
, exec_len
, off
, proc_e
, T
, text_bytes
);
183 printf("do_exec: read_seg failed: %d\n", r
);
189 printf("do_exec: not loading text segment\n");
192 r
= read_seg(exec
, exec_len
, off
, proc_e
, D
, data_bytes
);
195 printf("do_exec: read_seg failed: %d\n", r
);
200 exec_restart(proc_e
, OK
);
205 printf("do_exec(fail): error = %d\n", error
);
207 exec_restart(proc_e
, error
);
210 /*===========================================================================*
212 *===========================================================================*/
213 PRIVATE
int exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
214 frame_len
, sep_id
, st_dev
, st_ino
, st_ctime
, progname
,
215 new_uid
, new_gid
, stack_topp
, load_textp
, allow_setuidp
)
217 vir_bytes text_bytes
;
218 vir_bytes data_bytes
;
229 vir_bytes
*stack_topp
;
234 struct exec_newmem e
;
237 e
.text_bytes
= text_bytes
;
238 e
.data_bytes
= data_bytes
;
239 e
.bss_bytes
= bss_bytes
;
240 e
.tot_bytes
= tot_bytes
;
241 e
.args_bytes
= frame_len
;
245 e
.st_ctime
= st_ctime
;
248 strncpy(e
.progname
, progname
, sizeof(e
.progname
)-1);
249 e
.progname
[sizeof(e
.progname
)-1]= '\0';
251 m
.m_type
= EXEC_NEWMEM
;
252 m
.EXC_NM_PROC
= proc_e
;
253 m
.EXC_NM_PTR
= (char *)&e
;
254 r
= sendrec(PM_PROC_NR
, &m
);
258 printf("exec_newmem: r = %d, m_type = %d\n", r
, m
.m_type
);
260 *stack_topp
= m
.m1_i1
;
261 *load_textp
= !!(m
.m1_i2
& EXC_NM_RF_LOAD_TEXT
);
262 *allow_setuidp
= !!(m
.m1_i2
& EXC_NM_RF_ALLOW_SETUID
);
264 printf("exec_newmem: stack_top = 0x%x\n", *stack_topp
);
265 printf("exec_newmem: load_text = %d\n", *load_textp
);
271 /*===========================================================================*
273 *===========================================================================*/
274 PRIVATE
int exec_restart(proc_e
, result
)
281 m
.m_type
= EXEC_RESTART
;
282 m
.EXC_RS_PROC
= proc_e
;
283 m
.EXC_RS_RESULT
= result
;
284 r
= sendrec(PM_PROC_NR
, &m
);
291 /*===========================================================================*
293 *===========================================================================*/
294 PRIVATE
int read_header(exec
, exec_len
, sep_id
, text_bytes
, data_bytes
,
295 bss_bytes
, tot_bytes
, pc
, hdrlenp
)
296 char *exec
; /* executable image */
297 size_t exec_len
; /* size of the image */
298 int *sep_id
; /* true iff sep I&D */
299 vir_bytes
*text_bytes
; /* place to return text size */
300 vir_bytes
*data_bytes
; /* place to return initialized data size */
301 vir_bytes
*bss_bytes
; /* place to return bss size */
302 phys_bytes
*tot_bytes
; /* place to return total size */
303 vir_bytes
*pc
; /* program entry point (initial PC) */
306 /* Read the header and extract the text, data, bss and total sizes from it. */
309 struct exec hdr
; /* a.out header is read in here */
311 /* Read the header and check the magic number. The standard MINIX header
312 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
313 * Then come 4 more longs that are not used here.
314 * Byte 0: magic number 0x01
315 * Byte 1: magic number 0x03
316 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
317 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
318 * Motorola = 0x0B, Sun SPARC = 0x17
319 * Byte 4: Header length = 0x20
320 * Bytes 5-7 are not used.
322 * Now come the 6 longs
323 * Bytes 8-11: size of text segments in bytes
324 * Bytes 12-15: size of initialized data segment in bytes
325 * Bytes 16-19: size of bss in bytes
326 * Bytes 20-23: program entry point
327 * Bytes 24-27: total memory allocated to program (text, data + stack)
328 * Bytes 28-31: size of symbol table in bytes
329 * The longs are represented in a machine dependent order,
330 * little-endian on the 8088, big-endian on the 68000.
331 * The header is followed directly by the text and data segments, and the
332 * symbol table (if any). The sizes are given in the header. Only the
333 * text and data segments are copied into memory by exec. The header is
334 * used here only. The symbol table is for the benefit of a debugger and
339 pos
= 0; /* Read from the start of the file */
341 if (exec_len
< sizeof(hdr
)) return(ENOEXEC
);
343 memcpy(&hdr
, exec
, sizeof(hdr
));
345 /* Check magic number, cpu type, and flags. */
346 if (BADMAG(hdr
)) return(ENOEXEC
);
347 #if (CHIP == INTEL && _WORD_SIZE == 2)
348 if (hdr
.a_cpu
!= A_I8086
) return(ENOEXEC
);
350 #if (CHIP == INTEL && _WORD_SIZE == 4)
351 if (hdr
.a_cpu
!= A_I80386
) return(ENOEXEC
);
353 if ((hdr
.a_flags
& ~(A_NSYM
| A_EXEC
| A_SEP
)) != 0) return(ENOEXEC
);
355 *sep_id
= !!(hdr
.a_flags
& A_SEP
); /* separate I & D or not */
357 /* Get text and data sizes. */
358 *text_bytes
= (vir_bytes
) hdr
.a_text
; /* text size in bytes */
359 *data_bytes
= (vir_bytes
) hdr
.a_data
; /* data size in bytes */
360 *bss_bytes
= (vir_bytes
) hdr
.a_bss
; /* bss size in bytes */
361 *tot_bytes
= hdr
.a_total
; /* total bytes to allocate for prog */
362 if (*tot_bytes
== 0) return(ENOEXEC
);
365 /* If I & D space is not separated, it is all considered data. Text=0*/
366 *data_bytes
+= *text_bytes
;
369 *pc
= hdr
.a_entry
; /* initial address to start execution */
370 *hdrlenp
= hdr
.a_hdrlen
& BYTE
; /* header length */
375 /*===========================================================================*
377 *===========================================================================*/
378 PRIVATE
void patch_ptr(stack
, base
)
379 char stack
[ARG_MAX
]; /* pointer to stack image within PM */
380 vir_bytes base
; /* virtual address of stack base inside user */
382 /* When doing an exec(name, argv, envp) call, the user builds up a stack
383 * image with arg and env pointers relative to the start of the stack. Now
384 * these pointers must be relocated, since the stack is not positioned at
385 * address 0 in the user's address space.
391 flag
= 0; /* counts number of 0-pointers seen */
392 ap
= (char **) stack
; /* points initially to 'nargs' */
393 ap
++; /* now points to argv[0] */
395 if (ap
>= (char **) &stack
[ARG_MAX
]) return; /* too bad */
397 v
= (vir_bytes
) *ap
; /* v is relative pointer */
398 v
+= base
; /* relocate it */
399 *ap
= (char *) v
; /* put it back */
407 /*===========================================================================*
409 *===========================================================================*/
410 PRIVATE
int read_seg(exec
, exec_len
, off
, proc_e
, seg
, seg_bytes
)
411 char *exec
; /* executable image */
412 size_t exec_len
; /* size of the image */
413 off_t off
; /* offset in file */
414 int proc_e
; /* process number (endpoint) */
415 int seg
; /* T, D, or S */
416 phys_bytes seg_bytes
; /* how much is to be transferred? */
419 * The byte count on read is usually smaller than the segment count, because
420 * a segment is padded out to a click multiple, and the data segment is only
421 * partially initialized.
425 off_t n
, o
, b_off
, seg_off
;
427 if (off
+seg_bytes
> exec_len
) return ENOEXEC
;
428 r
= sys_vircopy(SELF
, D
, (vir_bytes
)exec
+off
, proc_e
, seg
, 0, seg_bytes
);