Skip to content

Commit

Permalink
We never exec(2) with a kernel vmspace, so do not test for that, but …
Browse files Browse the repository at this point in the history
…instead

KASSERT() that we don't.
When calculating the load address for the interpreter (e.g. ld.elf_so),
we need to take into account wether the exec'd process will run with
topdown memory or bottom up. We can not use the current vmspace's flags
to test for that, as this happens too early. Luckily the execpack already
knows what the new state will be later, so instead of testing the current
vmspace, pass the info as additional argument to struct emul
e_vm_default_addr.
Fix all such functions and adopt all callers.
  • Loading branch information
martin committed Nov 26, 2015
1 parent 2583165 commit f765f5c
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 58 deletions.
9 changes: 5 additions & 4 deletions sys/arch/amd64/amd64/netbsd32_machdep.c
@@ -1,4 +1,4 @@
/* $NetBSD: netbsd32_machdep.c,v 1.94 2015/11/22 13:41:24 maxv Exp $ */
/* $NetBSD: netbsd32_machdep.c,v 1.95 2015/11/26 13:15:34 martin Exp $ */

/*
* Copyright (c) 2001 Wasabi Systems, Inc.
Expand Down Expand Up @@ -36,7 +36,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.94 2015/11/22 13:41:24 maxv Exp $");
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.95 2015/11/26 13:15:34 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
Expand Down Expand Up @@ -931,9 +931,10 @@ cpu_mcontext32_validate(struct lwp *l, const mcontext32_t *mcp)
}

vaddr_t
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t sz)
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t sz,
int topdown)
{
if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
if (topdown)
return VM_DEFAULT_ADDRESS32_TOPDOWN(base, sz);
else
return VM_DEFAULT_ADDRESS32_BOTTOMUP(base, sz);
Expand Down
7 changes: 4 additions & 3 deletions sys/arch/arm/arm32/netbsd32_machdep.c
Expand Up @@ -29,7 +29,7 @@

#include <sys/cdefs.h>

__KERNEL_RCSID(1, "$NetBSD: netbsd32_machdep.c,v 1.6 2014/05/05 17:45:24 skrll Exp $");
__KERNEL_RCSID(1, "$NetBSD: netbsd32_machdep.c,v 1.7 2015/11/26 13:15:34 martin Exp $");

#include "opt_compat_netbsd.h"

Expand Down Expand Up @@ -93,9 +93,10 @@ netbsd32_sysarch(struct lwp *l, const struct netbsd32_sysarch_args *uap,
}

vaddr_t
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t sz)
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t sz,
int topdown)
{
if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
if (topdown)
return VM_DEFAULT_ADDRESS_TOPDOWN(base, sz);
else
return VM_DEFAULT_ADDRESS_BOTTOMUP(base, sz);
Expand Down
9 changes: 5 additions & 4 deletions sys/arch/mips/mips/netbsd32_machdep.c
@@ -1,4 +1,4 @@
/* $NetBSD: netbsd32_machdep.c,v 1.12 2015/05/17 18:52:37 matt Exp $ */
/* $NetBSD: netbsd32_machdep.c,v 1.13 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -30,7 +30,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.12 2015/05/17 18:52:37 matt Exp $");
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.13 2015/11/26 13:15:34 martin Exp $");

#include "opt_compat_netbsd.h"
#include "opt_coredump.h"
Expand Down Expand Up @@ -143,9 +143,10 @@ compat_16_netbsd32___sigreturn14(struct lwp *l,
#endif

vaddr_t
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size)
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size,
int topdown)
{
if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
if (topdown)
return VM_DEFAULT_ADDRESS32_TOPDOWN(base, size);
else
return VM_DEFAULT_ADDRESS32_BOTTOMUP(base, size);
Expand Down
7 changes: 4 additions & 3 deletions sys/arch/riscv/riscv/netbsd32_machdep.c
Expand Up @@ -29,7 +29,7 @@

#include <sys/cdefs.h>

__RCSID("$NetBSD: netbsd32_machdep.c,v 1.1 2015/03/28 16:13:56 matt Exp $");
__RCSID("$NetBSD: netbsd32_machdep.c,v 1.2 2015/11/26 13:15:34 martin Exp $");

#include <sys/param.h>
#include <sys/ucontext.h>
Expand Down Expand Up @@ -177,9 +177,10 @@ netbsd32_sysarch(struct lwp *l, const struct netbsd32_sysarch_args *uap,
}

vaddr_t
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size)
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size,
int topdown)
{
if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
if (topdown)
return VM_DEFAULT_ADDRESS32_TOPDOWN(base, size);
else
return VM_DEFAULT_ADDRESS32_BOTTOMUP(base, size);
Expand Down
7 changes: 4 additions & 3 deletions sys/arch/sparc64/sparc64/netbsd32_machdep.c
@@ -1,4 +1,4 @@
/* $NetBSD: netbsd32_machdep.c,v 1.108 2015/11/22 11:24:08 martin Exp $ */
/* $NetBSD: netbsd32_machdep.c,v 1.109 2015/11/26 13:15:34 martin Exp $ */

/*
* Copyright (c) 1998, 2001 Matthew R. Green
Expand Down Expand Up @@ -27,7 +27,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.108 2015/11/22 11:24:08 martin Exp $");
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.109 2015/11/26 13:15:34 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_compat_netbsd.h"
Expand Down Expand Up @@ -1338,7 +1338,8 @@ startlwp32(void *arg)
}

vaddr_t
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size)
netbsd32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size,
int topdown)
{
return round_page((vaddr_t)(base) + (vsize_t)MAXDSIZ32);
}
7 changes: 4 additions & 3 deletions sys/arch/sparc64/sparc64/svr4_32_machdep.c
@@ -1,4 +1,4 @@
/* $NetBSD: svr4_32_machdep.c,v 1.38 2011/03/04 22:25:29 joerg Exp $ */
/* $NetBSD: svr4_32_machdep.c,v 1.39 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 1994 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -30,7 +30,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: svr4_32_machdep.c,v 1.38 2011/03/04 22:25:29 joerg Exp $");
__KERNEL_RCSID(0, "$NetBSD: svr4_32_machdep.c,v 1.39 2015/11/26 13:15:34 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_ddb.h"
Expand Down Expand Up @@ -673,7 +673,8 @@ svr4_32_sys_sysarch(struct lwp *l, const struct svr4_32_sys_sysarch_args *uap, r
}

vaddr_t
svr4_32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size)
svr4_32_vm_default_addr(struct proc *p, vaddr_t base, vsize_t size,
int topdown)
{
return round_page((vaddr_t)(base) + (vsize_t)MAXDSIZ32);
}
4 changes: 2 additions & 2 deletions sys/compat/netbsd32/netbsd32.h
@@ -1,4 +1,4 @@
/* $NetBSD: netbsd32.h,v 1.108 2015/08/10 04:48:53 mrg Exp $ */
/* $NetBSD: netbsd32.h,v 1.109 2015/11/26 13:15:34 martin Exp $ */

/*
* Copyright (c) 1998, 2001, 2008, 2015 Matthew R. Green
Expand Down Expand Up @@ -1100,7 +1100,7 @@ int coredump_netbsd32(struct lwp *, struct coredump_iostate *);
#include <compat/common/compat_util.h>
#include <compat/sys/siginfo.h>

vaddr_t netbsd32_vm_default_addr(struct proc *, vaddr_t, vsize_t);
vaddr_t netbsd32_vm_default_addr(struct proc *, vaddr_t, vsize_t, int);
void netbsd32_adjust_limits(struct proc *);

void netbsd32_si_to_si32(siginfo32_t *, const siginfo_t *);
Expand Down
4 changes: 2 additions & 2 deletions sys/compat/svr4_32/svr4_32_exec.h
@@ -1,4 +1,4 @@
/* $NetBSD: svr4_32_exec.h,v 1.14 2009/12/10 14:13:54 matt Exp $ */
/* $NetBSD: svr4_32_exec.h,v 1.15 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 1994 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -63,7 +63,7 @@ int svr4_32_copyargs(struct lwp *, struct exec_package *, struct ps_strings *,
extern struct emul emul_svr4_32;

void svr4_32_setregs(struct lwp *, struct exec_package *, vaddr_t);
vaddr_t svr4_32_vm_default_addr(struct proc *, vaddr_t, vsize_t);
vaddr_t svr4_32_vm_default_addr(struct proc *, vaddr_t, vsize_t, int);
int svr4_32_elf32_probe(struct lwp *, struct exec_package *, void *,
char *, vaddr_t *);

Expand Down
3 changes: 2 additions & 1 deletion sys/external/bsd/drm2/dist/drm/i915/i915_gem.c
Expand Up @@ -1772,7 +1772,8 @@ i915_gem_mmap_ioctl(struct drm_device *dev, void *data,

#ifdef __NetBSD__
addr = (*curproc->p_emul->e_vm_default_addr)(curproc,
(vaddr_t)curproc->p_vmspace->vm_daddr, args->size);
(vaddr_t)curproc->p_vmspace->vm_daddr, args->size,
curproc->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
/* XXX errno NetBSD->Linux */
ret = -uvm_map(&curproc->p_vmspace->vm_map, &addr, args->size,
obj->gemo_shm_uao, args->offset, 0,
Expand Down
20 changes: 10 additions & 10 deletions sys/kern/exec_elf.c
@@ -1,4 +1,4 @@
/* $NetBSD: exec_elf.c,v 1.80 2015/11/01 17:44:41 christos Exp $ */
/* $NetBSD: exec_elf.c,v 1.81 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 1994, 2000, 2005, 2015 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -57,7 +57,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.80 2015/11/01 17:44:41 christos Exp $");
__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.81 2015/11/26 13:15:34 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_pax.h"
Expand Down Expand Up @@ -413,15 +413,13 @@ elf_load_interp(struct lwp *l, struct exec_package *epp, char *path,
p = l->l_proc;

KASSERT(p->p_vmspace);
if (__predict_true(p->p_vmspace != proc0.p_vmspace)) {
use_topdown = p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN;
} else {
KASSERT(p->p_vmspace != proc0.p_vmspace);

#ifdef __USE_TOPDOWN_VM
use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
#else
use_topdown = false;
use_topdown = false;
#endif
}

/*
* 1. open file
Expand Down Expand Up @@ -531,9 +529,11 @@ elf_load_interp(struct lwp *l, struct exec_package *epp, char *path,
*/
addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
epp->ep_daddr,
round_page(limit) - trunc_page(base_ph->p_vaddr));
} else
round_page(limit) - trunc_page(base_ph->p_vaddr),
use_topdown);
} else {
addr = *last; /* may be ELF_LINK_ADDR */
}

/*
* Load all the necessary sections
Expand Down
6 changes: 3 additions & 3 deletions sys/kern/kern_exec.c
@@ -1,4 +1,4 @@
/* $NetBSD: kern_exec.c,v 1.421 2015/10/22 11:48:02 maxv Exp $ */
/* $NetBSD: kern_exec.c,v 1.422 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -59,7 +59,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.421 2015/10/22 11:48:02 maxv Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.422 2015/11/26 13:15:34 martin Exp $");

#include "opt_exec.h"
#include "opt_execfmt.h"
Expand Down Expand Up @@ -1919,7 +1919,7 @@ exec_sigcode_map(struct proc *p, const struct emul *e)

/* Just a hint to uvm_map where to put it. */
va = e->e_vm_default_addr(p, (vaddr_t)p->p_vmspace->vm_daddr,
round_page(sz));
round_page(sz), p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);

#ifdef __alpha__
/*
Expand Down
7 changes: 4 additions & 3 deletions sys/kern/kern_lwp.c
@@ -1,4 +1,4 @@
/* $NetBSD: kern_lwp.c,v 1.181 2015/11/12 10:47:30 skrll Exp $ */
/* $NetBSD: kern_lwp.c,v 1.182 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 2001, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -211,7 +211,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.181 2015/11/12 10:47:30 skrll Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.182 2015/11/26 13:15:34 martin Exp $");

#include "opt_ddb.h"
#include "opt_lockdebug.h"
Expand Down Expand Up @@ -1743,7 +1743,8 @@ lwp_ctl_alloc(vaddr_t *uaddr)
lp->lp_cur = 0;
lp->lp_max = LWPCTL_UAREA_SZ;
lp->lp_uva = p->p_emul->e_vm_default_addr(p,
(vaddr_t)p->p_vmspace->vm_daddr, LWPCTL_UAREA_SZ);
(vaddr_t)p->p_vmspace->vm_daddr, LWPCTL_UAREA_SZ,
p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
error = uvm_map(&p->p_vmspace->vm_map, &lp->lp_uva,
LWPCTL_UAREA_SZ, lp->lp_uao, 0, 0, UVM_MAPFLAG(UVM_PROT_RW,
UVM_PROT_RW, UVM_INH_NONE, UVM_ADV_NORMAL, 0));
Expand Down
7 changes: 4 additions & 3 deletions sys/kern/sysv_shm.c
@@ -1,4 +1,4 @@
/* $NetBSD: sysv_shm.c,v 1.130 2015/11/06 02:26:42 pgoyette Exp $ */
/* $NetBSD: sysv_shm.c,v 1.131 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -61,7 +61,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.130 2015/11/06 02:26:42 pgoyette Exp $");
__KERNEL_RCSID(0, "$NetBSD: sysv_shm.c,v 1.131 2015/11/26 13:15:34 martin Exp $");

#ifdef _KERNEL_OPT
#include "opt_sysv.h"
Expand Down Expand Up @@ -436,7 +436,8 @@ sys_shmat(struct lwp *l, const struct sys_shmat_args *uap, register_t *retval)
} else {
/* This is just a hint to uvm_map() about where to put it. */
attach_va = p->p_emul->e_vm_default_addr(p,
(vaddr_t)vm->vm_daddr, size);
(vaddr_t)vm->vm_daddr, size,
p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
}

/*
Expand Down
5 changes: 3 additions & 2 deletions sys/sys/proc.h
@@ -1,4 +1,4 @@
/* $NetBSD: proc.h,v 1.323 2015/09/24 14:33:31 christos Exp $ */
/* $NetBSD: proc.h,v 1.324 2015/11/26 13:15:34 martin Exp $ */

/*-
* Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -170,7 +170,8 @@ struct emul {
struct sysctlnode *e_sysctlovly;
int (*e_fault)(struct proc *, vaddr_t, int);

vaddr_t (*e_vm_default_addr)(struct proc *, vaddr_t, vsize_t);
vaddr_t (*e_vm_default_addr)(struct proc *, vaddr_t, vsize_t,
int);

/* Emulation-specific hook for userspace page faults */
int (*e_usertrap)(struct lwp *, vaddr_t, void *);
Expand Down
5 changes: 3 additions & 2 deletions sys/uvm/uvm_extern.h
@@ -1,4 +1,4 @@
/* $NetBSD: uvm_extern.h,v 1.194 2015/03/20 15:41:43 riastradh Exp $ */
/* $NetBSD: uvm_extern.h,v 1.195 2015/11/26 13:15:34 martin Exp $ */

/*
* Copyright (c) 1997 Charles D. Cranor and Washington University.
Expand Down Expand Up @@ -675,7 +675,8 @@ int uvm_pctparam_createsysctlnode(struct uvm_pctparam *,
int uvm_mmap_dev(struct proc *, void **, size_t, dev_t,
off_t);
int uvm_mmap_anon(struct proc *, void **, size_t);
vaddr_t uvm_default_mapaddr(struct proc *, vaddr_t, vsize_t);
vaddr_t uvm_default_mapaddr(struct proc *, vaddr_t, vsize_t,
int);

/* uvm_mremap.c */
int uvm_mremap(struct vm_map *, vaddr_t, vsize_t,
Expand Down

0 comments on commit f765f5c

Please sign in to comment.