tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

exec and VM_MAP_TOPDOWN - chicken & egg?

I have a small (mostly conceptional) issue with sys/kern/exec_elf.c.
In my view the exec operation is kind of contstructor op for a vmspace,
but on the other hand exec needs to know where to put the interpreter,
which slightly differs if we are about to arrange for topdown VM layout.

My concrete issue popped up when I try to exec in a proc that has no
p_vmspace at all yet - so it crashes when checking for VM_MAP_TOPDOWN
in the vmspace flags.

This is easily worked around by this patch:

Index: exec_elf.c
RCS file: /cvsroot/src/sys/kern/exec_elf.c,v
retrieving revision 1.30
diff -c -u -p -r1.30 exec_elf.c
--- exec_elf.c  19 Jul 2011 19:45:36 -0000      1.30
+++ exec_elf.c  31 Jul 2011 18:01:22 -0000
@@ -84,6 +84,7 @@ __KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v
 #include <compat/common/compat_util.h>
 #include <sys/pax.h>
+#include <uvm/uvm_param.h>
 extern struct emul emul_netbsd;
@@ -406,9 +407,19 @@ elf_load_file(struct lwp *l, struct exec
        u_long phsize;
        Elf_Addr addr = *last;
        struct proc *p;
+       bool use_topdown;
        p = l->l_proc;
+       if (p->p_vmspace)
+               use_topdown = p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN;
+       else
+               use_topdown = true;
+               use_topdown = false;
         * 1. open file
         * 2. read filehdr
@@ -552,7 +563,7 @@ elf_load_file(struct lwp *l, struct exec
                                flags = VMCMD_BASE;
                                if (addr == ELF_LINK_ADDR)
                                        addr = ph0->p_vaddr;
-                               if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
+                               if (use_topdown)
                                        addr = ELF_TRUNC(addr, ph0->p_align);
                                        addr = ELF_ROUND(addr, ph0->p_align);

Obviously this is a hack. Thinking about what happens in the normal case:
we are about to create the new vmspace, but the check tests the flags for
the old vmspace. The new vmspace will not inherit the flags, but will
have the same default as the use_topdown variable I added in the patch.

I would have expected that emulations would care, but I can't find traces
of it. And the only exec format that cares is elf. Wouldn't it be conceptually
cleaner if the "we would like to arrange for topdown VM, if possible" flag
would be part of struct exec_pack and explicitly set upfront (maybbe by
just copying it from the current procs vmspace flags?

Object loaders and emulations could override it, and the vmpspace flag
could later be set accordingly.

Am I missing something?


Home | Main Index | Thread Index | Old Index