2 #include <lunaix/exec.h>
4 #include <lunaix/load.h>
5 #include <lunaix/mm/mmap.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/mm/vmm.h>
8 #include <lunaix/process.h>
9 #include <lunaix/spike.h>
10 #include <lunaix/status.h>
11 #include <lunaix/syscall.h>
12 #include <lunaix/syscall_utils.h>
14 #include <klibc/string.h>
17 exec_container(struct exec_container* param,
18 struct proc_info* proc,
23 *param = (struct exec_container){ .proc = proc,
25 .exe = { .container = param },
32 exec_str_size(const char** str_arr, size_t* length)
39 const char* chr = *str_arr;
40 size_t sz = 0, len = 0;
50 return sz + sizeof(char*);
53 // externed from mm/dmm.c
55 create_heap(struct proc_mm* pvms, ptr_t addr);
58 exec_load(struct exec_container* container, struct v_file* executable)
62 const char **argv = container->argv, **envp = container->envp;
63 size_t argv_len, envp_len;
64 size_t sz_argv = exec_str_size(argv, &argv_len);
65 size_t sz_envp = exec_str_size(envp, &envp_len);
66 size_t var_sz = ROUNDUP(sz_envp, PG_SIZE);
67 char** argv_extra = container->argv_pp;
69 argv_extra[0] = executable->dnode->name.value;
71 if (var_sz / PG_SIZE > MAX_VAR_PAGES) {
76 if ((errno = load_executable(&container->exe, executable))) {
80 struct proc_mm* pvms = &container->proc->mm;
82 // A dedicated place for process variables (e.g. envp)
83 struct mmap_param map_vars = { .pvms = pvms,
84 .vms_mnt = container->vms_mnt,
85 .flags = MAP_ANON | MAP_PRIVATE | MAP_FIXED,
86 .type = REGION_TYPE_VARS,
88 .mlen = MAX_VAR_PAGES * PG_SIZE };
93 mem_unmap_region(container->vms_mnt, pvms->heap);
98 // If loading a statically linked file, then heap remapping we can do,
100 create_heap(&container->proc->mm, PG_ALIGN(container->exe.end));
103 if ((errno = mem_map(&mapped, NULL, UMMAP_END, NULL, &map_vars))) {
107 if (container->vms_mnt == VMS_SELF) {
108 // we are loading executable into current addr space
110 // make some handy infos available to user space
112 memcpy(mapped, (void*)envp, sz_envp);
114 void* ustack = (void*)USTACK_TOP;
117 ustack = (void*)((ptr_t)ustack - sz_argv);
118 memcpy(ustack, (void*)argv, sz_argv);
121 for (size_t i = 0; i < 2 && argv_extra[i]; i++, argv_len++) {
122 char* extra_arg = argv_extra[i];
123 size_t str_len = strlen(extra_arg);
125 ustack = (void*)((ptr_t)ustack - str_len);
126 memcpy(ustack, (void*)extra_arg, str_len);
129 // four args (arg{c|v}, env{c|p}) for main
130 struct uexec_param* exec_param = &((struct uexec_param*)ustack)[-1];
132 container->stack_top = (ptr_t)exec_param;
134 *exec_param = (struct uexec_param){
135 .argc = argv_len, .argv = ustack, .envc = envp_len, .envp = mapped
140 TODO need to find a way to inject argv and envp remotely
141 this is for the support of kernel level implementation of
145 1. Allocate a orphaned physical page (i.e., do not belong to any
147 2. Mounted to a temporary mount point in current VMA, (i.e.,
150 4. Unmount then mounted to the foreign VMA as the first stack
153 fail("not implemented");
161 exec_load_byname(struct exec_container* container, const char* filename)
164 struct v_dnode* dnode;
167 if ((errno = vfs_walk_proc(filename, &dnode, NULL, 0))) {
171 if ((errno = vfs_open(dnode, &file))) {
175 errno = exec_load(container, file);
182 exec_kexecve(const char* filename, const char* argv[], const char* envp[])
185 struct exec_container container;
186 exec_container(&container, __current, VMS_SELF, argv, envp);
188 errno = exec_load_byname(&container, filename);
194 ptr_t entry = container.exe.entry;
197 j_usr(container.stack_top, entry);
204 __DEFINE_LXSYSCALL3(int,
214 struct exec_container container;
215 exec_container(&container, __current, VMS_SELF, argv, envp);
217 if ((errno = exec_load_byname(&container, filename))) {
221 // we will jump to new entry point (_u_start) upon syscall's
222 // return so execve 'will not return' from the perspective of it's invoker
223 volatile struct exec_param* execp = __current->intr_ctx.execp;
224 execp->esp = container.stack_top;
225 execp->eip = container.exe.entry;
229 store_retval(DO_STATUS(errno));
231 // Always yield the process that want execve!
234 // this will never get executed!