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
);
175 printf("RS: stack_top is 0x%lx; tried to copy to 0x%lx in %d\n",
177 panic("RS", "do_exec: stack copy err on", proc_e
);
182 /* Read in text and data segments. */
184 r
= read_seg(exec
, exec_len
, off
, proc_e
, T
, text_bytes
);
187 printf("do_exec: read_seg failed: %d\n", r
);
193 printf("do_exec: not loading text segment\n");
196 r
= read_seg(exec
, exec_len
, off
, proc_e
, D
, data_bytes
);
199 printf("do_exec: read_seg failed: %d\n", r
);
204 exec_restart(proc_e
, OK
);
209 printf("do_exec(fail): error = %d\n", error
);
211 exec_restart(proc_e
, error
);
214 /*===========================================================================*
216 *===========================================================================*/
217 PRIVATE
int exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
218 frame_len
, sep_id
, st_dev
, st_ino
, st_ctime
, progname
,
219 new_uid
, new_gid
, stack_topp
, load_textp
, allow_setuidp
)
221 vir_bytes text_bytes
;
222 vir_bytes data_bytes
;
233 vir_bytes
*stack_topp
;
238 struct exec_newmem e
;
241 e
.text_bytes
= text_bytes
;
242 e
.data_bytes
= data_bytes
;
243 e
.bss_bytes
= bss_bytes
;
244 e
.tot_bytes
= tot_bytes
;
245 e
.args_bytes
= frame_len
;
249 e
.st_ctime
= st_ctime
;
252 strncpy(e
.progname
, progname
, sizeof(e
.progname
)-1);
253 e
.progname
[sizeof(e
.progname
)-1]= '\0';
255 m
.m_type
= EXEC_NEWMEM
;
256 m
.EXC_NM_PROC
= proc_e
;
257 m
.EXC_NM_PTR
= (char *)&e
;
258 r
= sendrec(PM_PROC_NR
, &m
);
262 printf("exec_newmem: r = %d, m_type = %d\n", r
, m
.m_type
);
264 *stack_topp
= m
.m1_i1
;
265 *load_textp
= !!(m
.m1_i2
& EXC_NM_RF_LOAD_TEXT
);
266 *allow_setuidp
= !!(m
.m1_i2
& EXC_NM_RF_ALLOW_SETUID
);
268 printf("RS: exec_newmem: stack_top = 0x%x\n", *stack_topp
);
269 printf("RS: exec_newmem: load_text = %d\n", *load_textp
);
275 /*===========================================================================*
277 *===========================================================================*/
278 PRIVATE
int exec_restart(proc_e
, result
)
285 m
.m_type
= EXEC_RESTART
;
286 m
.EXC_RS_PROC
= proc_e
;
287 m
.EXC_RS_RESULT
= result
;
288 r
= sendrec(PM_PROC_NR
, &m
);
295 /*===========================================================================*
297 *===========================================================================*/
298 PRIVATE
int read_header(exec
, exec_len
, sep_id
, text_bytes
, data_bytes
,
299 bss_bytes
, tot_bytes
, pc
, hdrlenp
)
300 char *exec
; /* executable image */
301 size_t exec_len
; /* size of the image */
302 int *sep_id
; /* true iff sep I&D */
303 vir_bytes
*text_bytes
; /* place to return text size */
304 vir_bytes
*data_bytes
; /* place to return initialized data size */
305 vir_bytes
*bss_bytes
; /* place to return bss size */
306 phys_bytes
*tot_bytes
; /* place to return total size */
307 vir_bytes
*pc
; /* program entry point (initial PC) */
310 /* Read the header and extract the text, data, bss and total sizes from it. */
313 struct exec hdr
; /* a.out header is read in here */
315 /* Read the header and check the magic number. The standard MINIX header
316 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
317 * Then come 4 more longs that are not used here.
318 * Byte 0: magic number 0x01
319 * Byte 1: magic number 0x03
320 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
321 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
322 * Motorola = 0x0B, Sun SPARC = 0x17
323 * Byte 4: Header length = 0x20
324 * Bytes 5-7 are not used.
326 * Now come the 6 longs
327 * Bytes 8-11: size of text segments in bytes
328 * Bytes 12-15: size of initialized data segment in bytes
329 * Bytes 16-19: size of bss in bytes
330 * Bytes 20-23: program entry point
331 * Bytes 24-27: total memory allocated to program (text, data + stack)
332 * Bytes 28-31: size of symbol table in bytes
333 * The longs are represented in a machine dependent order,
334 * little-endian on the 8088, big-endian on the 68000.
335 * The header is followed directly by the text and data segments, and the
336 * symbol table (if any). The sizes are given in the header. Only the
337 * text and data segments are copied into memory by exec. The header is
338 * used here only. The symbol table is for the benefit of a debugger and
343 pos
= 0; /* Read from the start of the file */
345 if (exec_len
< sizeof(hdr
)) return(ENOEXEC
);
347 memcpy(&hdr
, exec
, sizeof(hdr
));
349 /* Check magic number, cpu type, and flags. */
350 if (BADMAG(hdr
)) return(ENOEXEC
);
351 #if (CHIP == INTEL && _WORD_SIZE == 2)
352 if (hdr
.a_cpu
!= A_I8086
) return(ENOEXEC
);
354 #if (CHIP == INTEL && _WORD_SIZE == 4)
355 if (hdr
.a_cpu
!= A_I80386
) return(ENOEXEC
);
357 if ((hdr
.a_flags
& ~(A_NSYM
| A_EXEC
| A_SEP
)) != 0) return(ENOEXEC
);
359 *sep_id
= !!(hdr
.a_flags
& A_SEP
); /* separate I & D or not */
361 /* Get text and data sizes. */
362 *text_bytes
= (vir_bytes
) hdr
.a_text
; /* text size in bytes */
363 *data_bytes
= (vir_bytes
) hdr
.a_data
; /* data size in bytes */
364 *bss_bytes
= (vir_bytes
) hdr
.a_bss
; /* bss size in bytes */
365 *tot_bytes
= hdr
.a_total
; /* total bytes to allocate for prog */
366 if (*tot_bytes
== 0) return(ENOEXEC
);
369 /* If I & D space is not separated, it is all considered data. Text=0*/
370 *data_bytes
+= *text_bytes
;
373 *pc
= hdr
.a_entry
; /* initial address to start execution */
374 *hdrlenp
= hdr
.a_hdrlen
& BYTE
; /* header length */
379 /*===========================================================================*
381 *===========================================================================*/
382 PRIVATE
void patch_ptr(stack
, base
)
383 char stack
[ARG_MAX
]; /* pointer to stack image within PM */
384 vir_bytes base
; /* virtual address of stack base inside user */
386 /* When doing an exec(name, argv, envp) call, the user builds up a stack
387 * image with arg and env pointers relative to the start of the stack. Now
388 * these pointers must be relocated, since the stack is not positioned at
389 * address 0 in the user's address space.
395 flag
= 0; /* counts number of 0-pointers seen */
396 ap
= (char **) stack
; /* points initially to 'nargs' */
397 ap
++; /* now points to argv[0] */
399 if (ap
>= (char **) &stack
[ARG_MAX
]) return; /* too bad */
401 v
= (vir_bytes
) *ap
; /* v is relative pointer */
402 v
+= base
; /* relocate it */
403 *ap
= (char *) v
; /* put it back */
411 /*===========================================================================*
413 *===========================================================================*/
414 PRIVATE
int read_seg(exec
, exec_len
, off
, proc_e
, seg
, seg_bytes
)
415 char *exec
; /* executable image */
416 size_t exec_len
; /* size of the image */
417 off_t off
; /* offset in file */
418 int proc_e
; /* process number (endpoint) */
419 int seg
; /* T, D, or S */
420 phys_bytes seg_bytes
; /* how much is to be transferred? */
423 * The byte count on read is usually smaller than the segment count, because
424 * a segment is padded out to a click multiple, and the data segment is only
425 * partially initialized.
429 off_t n
, o
, b_off
, seg_off
;
431 if (off
+seg_bytes
> exec_len
) return ENOEXEC
;
432 r
= sys_vircopy(SELF
, D
, (vir_bytes
)exec
+off
, proc_e
, seg
, 0, seg_bytes
);