4 #define BLOCK_SIZE 1024
6 static int 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 int dev_execve(int proc_e
, char *exec
, size_t exec_len
, char **argv
,
40 /* Assumptions: size_t and char *, it's all the same thing. */
42 /* Create a stack image that only needs to be patched up slightly
43 * by the kernel to be used for the process to be executed.
46 ov
= 0; /* No overflow yet. */
47 frame_size
= 0; /* Size of the new initial stack. */
48 string_off
= 0; /* Offset to start of the strings. */
49 argc
= 0; /* Argument count. */
51 for (ap
= argv
; *ap
!= NULL
; ap
++) {
52 n
= sizeof(*ap
) + strlen(*ap
) + 1;
54 if (frame_size
< n
) ov
= 1;
55 string_off
+= sizeof(*ap
);
60 printf("here: %s, %d\n", __FILE__
, __LINE__
);
61 for (ep
= envp
; *ep
!= NULL
; ep
++) {
62 n
= sizeof(*ep
) + strlen(*ep
) + 1;
64 if (frame_size
< n
) ov
= 1;
65 string_off
+= sizeof(*ap
);
69 /* Add an argument count and two terminating nulls. */
70 frame_size
+= sizeof(argc
) + sizeof(*ap
) + sizeof(*ep
);
71 string_off
+= sizeof(argc
) + sizeof(*ap
) + sizeof(*ep
);
74 frame_size
= (frame_size
+ sizeof(char *) - 1) & ~(sizeof(char *) - 1);
76 /* The party is off if there is an overflow. */
77 if (ov
|| frame_size
< 3 * sizeof(char *)) {
82 /* Allocate space for the stack frame. */
83 if ((frame
= (char *) sbrk(frame_size
)) == (char *) -1) {
88 /* Set arg count, init pointers to vector and string tables. */
89 * (size_t *) frame
= argc
;
90 vp
= (char **) (frame
+ sizeof(argc
));
91 sp
= frame
+ string_off
;
93 /* Load the argument vector and strings. */
94 for (ap
= argv
; *ap
!= NULL
; ap
++) {
95 *vp
++= (char *) (sp
- frame
);
103 /* Load the environment vector and strings. */
104 for (ep
= envp
; *ep
!= NULL
; ep
++) {
105 *vp
++= (char *) (sp
- frame
);
114 while (sp
< frame
+ frame_size
) *sp
++= 0;
116 (progname
=strrchr(argv
[0], '/')) ? progname
++ : (progname
=argv
[0]);
117 r
= do_exec(proc_e
, exec
, exec_len
, progname
, frame
, frame_size
);
119 /* Return the memory used for the frame and exit. */
120 (void) sbrk(-frame_size
);
124 static int do_exec(int proc_e
, char *exec
, size_t exec_len
, char *progname
,
125 char *frame
, int frame_len
)
128 int hdrlen
, sep_id
, load_text
, allow_setuid
;
129 int need_restart
, error
;
130 vir_bytes stack_top
, vsp
;
131 vir_bytes text_bytes
, data_bytes
, bss_bytes
, pc
;
132 phys_bytes tot_bytes
;
140 /* Read the file header and extract the segment sizes. */
141 r
= read_header(exec
, exec_len
, &sep_id
,
142 &text_bytes
, &data_bytes
, &bss_bytes
,
143 &tot_bytes
, &pc
, &hdrlen
);
146 printf("do_exec: read_header failed\n");
153 /* XXX what should we use to identify the executable? */
154 r
= exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
155 frame_len
, sep_id
, 0 /*dev*/, proc_e
/*inum*/, 0 /*ctime*/,
156 progname
, new_uid
, new_gid
, &stack_top
, &load_text
,
160 printf("do_exec: exec_newmap failed: %d\n", r
);
165 /* Patch up stack and copy it from RS to new core image. */
168 patch_ptr(frame
, vsp
);
169 r
= sys_datacopy(SELF
, (vir_bytes
) frame
,
170 proc_e
, (vir_bytes
) vsp
, (phys_bytes
)frame_len
);
172 printf("RS: stack_top is 0x%lx; tried to copy to 0x%lx in %d\n",
174 printf("do_exec: copying out new stack failed: %d\n", r
);
181 /* Read in text and data segments. */
183 r
= read_seg(exec
, exec_len
, off
, proc_e
, T
, text_bytes
);
186 printf("do_exec: read_seg failed: %d\n", r
);
192 printf("do_exec: not loading text segment\n");
195 r
= read_seg(exec
, exec_len
, off
, proc_e
, D
, data_bytes
);
198 printf("do_exec: read_seg failed: %d\n", r
);
203 return exec_restart(proc_e
, OK
);
206 printf("do_exec(fail): error = %d\n", error
);
208 exec_restart(proc_e
, error
);
213 /*===========================================================================*
215 *===========================================================================*/
216 PRIVATE
int exec_newmem(proc_e
, text_bytes
, data_bytes
, bss_bytes
, tot_bytes
,
217 frame_len
, sep_id
, st_dev
, st_ino
, st_ctime
, progname
,
218 new_uid
, new_gid
, stack_topp
, load_textp
, allow_setuidp
)
220 vir_bytes text_bytes
;
221 vir_bytes data_bytes
;
232 vir_bytes
*stack_topp
;
237 struct exec_newmem e
;
240 e
.text_bytes
= text_bytes
;
241 e
.data_bytes
= data_bytes
;
242 e
.bss_bytes
= bss_bytes
;
243 e
.tot_bytes
= tot_bytes
;
244 e
.args_bytes
= frame_len
;
248 e
.st_ctime
= st_ctime
;
251 strncpy(e
.progname
, progname
, sizeof(e
.progname
)-1);
252 e
.progname
[sizeof(e
.progname
)-1]= '\0';
254 m
.m_type
= EXEC_NEWMEM
;
255 m
.EXC_NM_PROC
= proc_e
;
256 m
.EXC_NM_PTR
= (char *)&e
;
257 r
= sendrec(PM_PROC_NR
, &m
);
261 printf("exec_newmem: r = %d, m_type = %d\n", r
, m
.m_type
);
263 *stack_topp
= m
.m1_i1
;
264 *load_textp
= !!(m
.m1_i2
& EXC_NM_RF_LOAD_TEXT
);
265 *allow_setuidp
= !!(m
.m1_i2
& EXC_NM_RF_ALLOW_SETUID
);
267 printf("RS: exec_newmem: stack_top = 0x%x\n", *stack_topp
);
268 printf("RS: exec_newmem: load_text = %d\n", *load_textp
);
274 /*===========================================================================*
276 *===========================================================================*/
277 PRIVATE
int exec_restart(proc_e
, result
)
284 m
.m_type
= EXEC_RESTART
;
285 m
.EXC_RS_PROC
= proc_e
;
286 m
.EXC_RS_RESULT
= result
;
287 r
= sendrec(PM_PROC_NR
, &m
);
294 /*===========================================================================*
296 *===========================================================================*/
297 PRIVATE
int read_header(exec
, exec_len
, sep_id
, text_bytes
, data_bytes
,
298 bss_bytes
, tot_bytes
, pc
, hdrlenp
)
299 char *exec
; /* executable image */
300 size_t exec_len
; /* size of the image */
301 int *sep_id
; /* true iff sep I&D */
302 vir_bytes
*text_bytes
; /* place to return text size */
303 vir_bytes
*data_bytes
; /* place to return initialized data size */
304 vir_bytes
*bss_bytes
; /* place to return bss size */
305 phys_bytes
*tot_bytes
; /* place to return total size */
306 vir_bytes
*pc
; /* program entry point (initial PC) */
309 /* Read the header and extract the text, data, bss and total sizes from it. */
312 struct exec hdr
; /* a.out header is read in here */
314 /* Read the header and check the magic number. The standard MINIX header
315 * is defined in <a.out.h>. It consists of 8 chars followed by 6 longs.
316 * Then come 4 more longs that are not used here.
317 * Byte 0: magic number 0x01
318 * Byte 1: magic number 0x03
319 * Byte 2: normal = 0x10 (not checked, 0 is OK), separate I/D = 0x20
320 * Byte 3: CPU type, Intel 16 bit = 0x04, Intel 32 bit = 0x10,
321 * Motorola = 0x0B, Sun SPARC = 0x17
322 * Byte 4: Header length = 0x20
323 * Bytes 5-7 are not used.
325 * Now come the 6 longs
326 * Bytes 8-11: size of text segments in bytes
327 * Bytes 12-15: size of initialized data segment in bytes
328 * Bytes 16-19: size of bss in bytes
329 * Bytes 20-23: program entry point
330 * Bytes 24-27: total memory allocated to program (text, data + stack)
331 * Bytes 28-31: size of symbol table in bytes
332 * The longs are represented in a machine dependent order,
333 * little-endian on the 8088, big-endian on the 68000.
334 * The header is followed directly by the text and data segments, and the
335 * symbol table (if any). The sizes are given in the header. Only the
336 * text and data segments are copied into memory by exec. The header is
337 * used here only. The symbol table is for the benefit of a debugger and
342 pos
= 0; /* Read from the start of the file */
344 if (exec_len
< sizeof(hdr
)) return(ENOEXEC
);
346 memcpy(&hdr
, exec
, sizeof(hdr
));
348 /* Check magic number, cpu type, and flags. */
349 if (BADMAG(hdr
)) return(ENOEXEC
);
350 #if (CHIP == INTEL && _WORD_SIZE == 2)
351 if (hdr
.a_cpu
!= A_I8086
) return(ENOEXEC
);
353 #if (CHIP == INTEL && _WORD_SIZE == 4)
354 if (hdr
.a_cpu
!= A_I80386
) return(ENOEXEC
);
356 if ((hdr
.a_flags
& ~(A_NSYM
| A_EXEC
| A_SEP
)) != 0) return(ENOEXEC
);
358 *sep_id
= !!(hdr
.a_flags
& A_SEP
); /* separate I & D or not */
360 /* Get text and data sizes. */
361 *text_bytes
= (vir_bytes
) hdr
.a_text
; /* text size in bytes */
362 *data_bytes
= (vir_bytes
) hdr
.a_data
; /* data size in bytes */
363 *bss_bytes
= (vir_bytes
) hdr
.a_bss
; /* bss size in bytes */
364 *tot_bytes
= hdr
.a_total
; /* total bytes to allocate for prog */
365 if (*tot_bytes
== 0) return(ENOEXEC
);
368 /* If I & D space is not separated, it is all considered data. Text=0*/
369 *data_bytes
+= *text_bytes
;
372 *pc
= hdr
.a_entry
; /* initial address to start execution */
373 *hdrlenp
= hdr
.a_hdrlen
& BYTE
; /* header length */
378 /*===========================================================================*
380 *===========================================================================*/
381 PRIVATE
void patch_ptr(stack
, base
)
382 char stack
[ARG_MAX
]; /* pointer to stack image within PM */
383 vir_bytes base
; /* virtual address of stack base inside user */
385 /* When doing an exec(name, argv, envp) call, the user builds up a stack
386 * image with arg and env pointers relative to the start of the stack. Now
387 * these pointers must be relocated, since the stack is not positioned at
388 * address 0 in the user's address space.
394 flag
= 0; /* counts number of 0-pointers seen */
395 ap
= (char **) stack
; /* points initially to 'nargs' */
396 ap
++; /* now points to argv[0] */
398 if (ap
>= (char **) &stack
[ARG_MAX
]) return; /* too bad */
400 v
= (vir_bytes
) *ap
; /* v is relative pointer */
401 v
+= base
; /* relocate it */
402 *ap
= (char *) v
; /* put it back */
410 /*===========================================================================*
412 *===========================================================================*/
413 PRIVATE
int read_seg(exec
, exec_len
, off
, proc_e
, seg
, seg_bytes
)
414 char *exec
; /* executable image */
415 size_t exec_len
; /* size of the image */
416 off_t off
; /* offset in file */
417 int proc_e
; /* process number (endpoint) */
418 int seg
; /* T, D, or S */
419 phys_bytes seg_bytes
; /* how much is to be transferred? */
422 * The byte count on read is usually smaller than the segment count, because
423 * a segment is padded out to a click multiple, and the data segment is only
424 * partially initialized.
428 off_t n
, o
, b_off
, seg_off
;
430 if (off
+seg_bytes
> exec_len
) return ENOEXEC
;
431 r
= sys_vircopy(SELF
, D
, (vir_bytes
)exec
+off
, proc_e
, seg
, 0, seg_bytes
);