xref: /minix/minix/servers/rs/exec.c (revision 0a6a1f1d)
1 #include "inc.h"
2 #include <assert.h>
3 #include <sys/exec.h>
4 #include <libexec.h>
5 #include <machine/vmparam.h>
6 
7 static int do_exec(int proc_e, char *exec, size_t exec_len, char *progname,
8 	char *frame, int frame_len, vir_bytes ps_str);
9 static int exec_restart(int proc_e, int result, vir_bytes pc, vir_bytes ps_str);
10 static int read_seg(struct exec_info *execi, off_t off,
11         vir_bytes seg_addr, size_t seg_bytes);
12 
13 /* Array of loaders for different object formats */
14 static struct exec_loaders {
15 	libexec_exec_loadfunc_t load_object;
16 } const exec_loaders[] = {
17 	{ libexec_load_elf },
18 	{ NULL }
19 };
20 
21 int srv_execve(int proc_e, char *exec, size_t exec_len, char **argv,
22 	char **envp)
23 {
24 	size_t frame_size = 0;	/* Size of the new initial stack. */
25 	int argc = 0;		/* Argument count. */
26 	int envc = 0;		/* Environment count */
27 	char overflow = 0;	/* No overflow yet. */
28 	char *frame;
29 	struct ps_strings *psp;
30 	int vsp = 0;	/* (virtual) Stack pointer in new address space. */
31 
32 	char *progname;
33 	int r;
34 
35 	minix_stack_params(argv[0], argv, envp, &frame_size, &overflow,
36 		&argc, &envc);
37 
38 	/* The party is off if there is an overflow. */
39 	if (overflow) {
40 		errno = E2BIG;
41 		return -1;
42 	}
43 
44 	/* Allocate space for the stack frame. */
45 	if ((frame = (char *) sbrk(frame_size)) == (char *) -1) {
46 		errno = E2BIG;
47 		return -1;
48 	}
49 
50 	minix_stack_fill(argv[0], argc, argv, envc, envp, frame_size, frame,
51 		&vsp, &psp);
52 
53 	(progname=strrchr(argv[0], '/')) ? progname++ : (progname=argv[0]);
54 
55 	r = do_exec(proc_e, exec, exec_len, progname, frame, frame_size,
56 		vsp + ((char *)psp - frame));
57 
58 	/* Failure, return the memory used for the frame and exit. */
59 	(void) sbrk(-frame_size);
60 
61 	return r;
62 }
63 
64 
65 static int do_exec(int proc_e, char *exec, size_t exec_len, char *progname,
66 	char *frame, int frame_len, vir_bytes ps_str)
67 {
68 	int r;
69 	vir_bytes vsp;
70 	struct exec_info execi;
71 	int i;
72 
73 	memset(&execi, 0, sizeof(execi));
74 
75 	execi.stack_high = minix_get_user_sp();
76 	execi.stack_size = DEFAULT_STACK_LIMIT;
77 	execi.proc_e = proc_e;
78 	execi.hdr = exec;
79 	execi.filesize = execi.hdr_len = exec_len;
80 	strncpy(execi.progname, progname, PROC_NAME_LEN-1);
81 	execi.progname[PROC_NAME_LEN-1] = '\0';
82 	execi.frame_len = frame_len;
83 
84 	/* callback functions and data */
85 	execi.copymem = read_seg;
86 	execi.clearproc = libexec_clearproc_vm_procctl;
87 	execi.clearmem = libexec_clear_sys_memset;
88 	execi.allocmem_prealloc_cleared = libexec_alloc_mmap_prealloc_cleared;
89 	execi.allocmem_prealloc_junk = libexec_alloc_mmap_prealloc_junk;
90 	execi.allocmem_ondemand = libexec_alloc_mmap_ondemand;
91 
92 	for(i = 0; exec_loaders[i].load_object != NULL; i++) {
93 	    r = (*exec_loaders[i].load_object)(&execi);
94 	    /* Loaded successfully, so no need to try other loaders */
95 	    if (r == OK) break;
96 	}
97 
98 	/* No exec loader could load the object */
99 	if (r != OK) {
100 	    printf("RS: do_exec: loading error %d\n", r);
101 	    return r;
102 	}
103 
104 	/* Inform PM */
105         if((r = libexec_pm_newexec(execi.proc_e, &execi)) != OK)
106 		return r;
107 
108 	/* Patch up stack and copy it from RS to new core image. */
109 	vsp = execi.stack_high - frame_len;
110 	r = sys_datacopy(SELF, (vir_bytes) frame,
111 		proc_e, (vir_bytes) vsp, (phys_bytes)frame_len);
112 	if (r != OK) {
113 		printf("do_exec: copying out new stack failed: %d\n", r);
114 		exec_restart(proc_e, r, execi.pc, ps_str);
115 		return r;
116 	}
117 
118 	return exec_restart(proc_e, OK, execi.pc, ps_str);
119 }
120 
121 /*===========================================================================*
122  *				exec_restart				     *
123  *===========================================================================*/
124 static int exec_restart(int proc_e, int result, vir_bytes pc, vir_bytes ps_str)
125 {
126 	int r;
127 	message m;
128 
129 	memset(&m, 0, sizeof(m));
130 	m.m_type = PM_EXEC_RESTART;
131 	m.m_rs_pm_exec_restart.endpt = proc_e;
132 	m.m_rs_pm_exec_restart.result = result;
133 	m.m_rs_pm_exec_restart.pc = pc;
134 	m.m_rs_pm_exec_restart.ps_str = ps_str;
135 
136 	r = ipc_sendrec(PM_PROC_NR, &m);
137 	if (r != OK)
138 		return r;
139 
140 	return m.m_type;
141 }
142 
143 /*===========================================================================*
144  *                             read_seg                                     *
145  *===========================================================================*/
146 static int read_seg(
147 struct exec_info *execi,       /* various data needed for exec */
148 off_t off,                     /* offset in file */
149 vir_bytes seg_addr,            /* address to load segment */
150 size_t seg_bytes           /* how much is to be transferred? */
151 )
152 {
153 /*
154  * The byte count on read is usually smaller than the segment count, because
155  * a segment is padded out to a click multiple, and the data segment is only
156  * partially initialized.
157  */
158 
159   int r;
160 
161   if (off+seg_bytes > execi->hdr_len) return ENOEXEC;
162   if((r= sys_datacopy(SELF, ((vir_bytes)execi->hdr)+off,
163   	execi->proc_e, seg_addr, seg_bytes)) != OK) {
164 	printf("RS: exec read_seg: copy 0x%x bytes into %i at 0x%08lx failed: %i\n",
165 		(int) seg_bytes, execi->proc_e, seg_addr, r);
166   }
167   return r;
168 }
169