Removing CSU patches

* Removed startup code patches in lib/csu regarding kernel to userland
   ABI.

 * Aligned stack layout on NetBSD stack layout.

 * Generate valid stack pointers instead of offsets by taking into account
   _minix_kerninfo->kinfo->user_sp.

 * Refactored stack generation, by moving part of execve in two
   functions {minix_stack_params(), minix_stack_fill()} and using them
   in execve(), rs and vm.

 * Changed load offset of rtld (ld.so) to:
      execi.args.stack_high - execi.args.stack_size - 0xa00000
   which is 10MB below the main executable stack.

Change-Id: I839daf3de43321cded44105634102d419cb36cec
This commit is contained in:
2013-09-13 20:12:22 +02:00
parent 5d04b92e06
commit cfd3379bb1
29 changed files with 608 additions and 485 deletions

View File

@@ -36,33 +36,16 @@ RCSID("$NetBSD: crt0.S,v 1.1 2012/08/13 02:49:04 matt Exp $")
STRONG_ALIAS(_start,__start)
_ENTRY(__start)
#ifdef __minix
mov r3, r2 /* cleanup */
mov r4, r1 /* obj_main */
and r5, r5, #0 /* ps_strings, always NULL on MINIX */
/* Get argc, argv, and envp from stack */
ldr r0, [sp, #0] /* get argc */
add r1, sp, #4 /* argv = sp + 4 */
add r2, r1, r0, lsl #2 /* envp = argv + argc*4 */
add r2, r2, #4 /* skip NULL terminator */
#else
/*
* We need to swap ps_strings and cleanup
*/
mov ip, r0 /* ps_strings -> tmp */
mov r0, r2 /* cleanup -> ps_strings */
mov r2, ip /* tmp -> ps_strings */
#endif /* __minix */
/* Ensure the stack is properly aligned before calling C code. */
bic sp, sp, #7
#ifdef __minix
/* Store obj and ps_strings on the stack */
sub sp, sp, #8
str r5, [sp, #4]
str r4, [sp, #0]
#endif
/*
* void ___start(void (*cleanup)(void),
* const Obj_Entry *obj,

View File

@@ -46,12 +46,4 @@ _ENTRY(__start)
pushl %ebx
pushl %ecx
pushl %edx
#ifdef __minix
movl 12(%esp),%eax
leal 16(%esp),%edx
leal 20(%esp,%eax,4),%ecx
pushl %ecx
pushl %edx
pushl %eax
#endif /* __minix */
call ___start

View File

@@ -80,15 +80,12 @@ struct ps_strings *__ps_strings = 0;
static char empty_string[] = "";
char *__progname = empty_string;
#ifndef __minix
__dead __dso_hidden void ___start(void (*)(void), const Obj_Entry *,
struct ps_strings *);
#ifndef __minix
#define write(fd, s, n) __syscall(SYS_write, (fd), (s), (n))
#else
__dead __dso_hidden void ___start(int, char **, char **, void (*)(void),
const Obj_Entry *, struct ps_strings *);
#define write(fd, s, n) /* NO write() from here on minix */
#endif
@@ -129,30 +126,11 @@ _fini(void)
#endif /* HAVE_INITFINI_ARRAY */
void
#ifdef __minix
___start(int argc, char **argv, char **envp,
void (*cleanup)(void), /* from shared loader */
#else
___start(void (*cleanup)(void), /* from shared loader */
#endif /* __minix */
const Obj_Entry *obj, /* from shared loader */
struct ps_strings *ps_strings)
{
#ifdef __minix
/* LSC: We have not yet updated the way we pass arguments to
the userspace, so here some code to adapt this to the new
ways. */
struct ps_strings minix_ps_strings;
if (ps_strings == NULL) {
memset(&minix_ps_strings, 0, sizeof(minix_ps_strings));
minix_ps_strings.ps_envstr = envp;
minix_ps_strings.ps_argvstr = argv;
minix_ps_strings.ps_nargvstr = argc;
ps_strings = &minix_ps_strings;
}
#endif /* __minix */
if (ps_strings == NULL)
_FATAL("ps_strings missing\n");
__ps_strings = ps_strings;

View File

@@ -23,6 +23,6 @@ SRCS+= accept.c access.c adjtime.c bind.c brk.c sbrk.c m_closefrom.c getsid.c \
getrusage.c
# Minix specific syscalls.
SRCS+= cprofile.c lseek64.c sprofile.c _mcontext.c
SRCS+= cprofile.c lseek64.c sprofile.c stack_utils.c _mcontext.c
.include "${ARCHDIR}/sys-minix/Makefile.inc"

View File

@@ -1,6 +1,4 @@
/* execve() - basic program execution call Author: Kees J. Bot
* 21 Jan 1994
*/
/* execve() - basic program execution call */
#include <sys/cdefs.h>
#include "namespace.h"
@@ -9,7 +7,9 @@
#include <unistd.h>
#include <string.h>
#include <stddef.h>
#include <minix/param.h>
#include <sys/exec_elf.h>
#include <sys/exec.h>
#ifdef __weak_alias
__weak_alias(execve, _execve)
@@ -17,62 +17,21 @@ __weak_alias(execve, _execve)
int execve(const char *path, char * const *argv, char * const *envp)
{
char * const *ap;
char * const *ep;
char *frame;
char **vp;
char *sp;
size_t argc;
int extra;
int vectors;
size_t frame_size;
size_t string_off;
size_t n;
int ov;
message m;
size_t frame_size = 0; /* Size of the new initial stack. */
int argc = 0; /* Argument count. */
int envc = 0; /* Environment count */
char overflow = 0; /* No overflow yet. */
char *frame;
struct ps_strings *psp;
int vsp = 0; /* (virtual) Stack pointer in new address space. */
/* Assumptions: size_t and char *, it's all the same thing. */
/* Create a stack image that only needs to be patched up slightly
* by the kernel to be used for the process to be executed.
*/
ov= 0; /* No overflow yet. */
frame_size= 0; /* Size of the new initial stack. */
string_off= 0; /* Offset to start of the strings. */
argc= 0; /* Argument count. */
for (ap= argv; *ap != NULL; ap++) {
n = sizeof(*ap) + strlen(*ap) + 1;
frame_size+= n;
if (frame_size < n) ov= 1;
string_off+= sizeof(*ap);
argc++;
}
for (ep= envp; *ep != NULL; ep++) {
n = sizeof(*ep) + strlen(*ep) + 1;
frame_size+= n;
if (frame_size < n) ov= 1;
string_off+= sizeof(*ap);
}
/* Add an argument count, two terminating nulls and
* space for the ELF aux vectors, that must come before
* (i.e. at a higher address) then the strings.
*/
vectors = sizeof(argc) + sizeof(*ap) + sizeof(*ep) +
sizeof(AuxInfo) * PMEF_AUXVECTORS;
extra = vectors + PMEF_EXECNAMELEN1;
frame_size+= extra;
string_off+= extra;
/* Align. */
frame_size= (frame_size + sizeof(char *) - 1) & ~(sizeof(char *) - 1);
minix_stack_params(path, argv, envp, &frame_size, &overflow,
&argc, &envc);
/* The party is off if there is an overflow. */
if (ov || frame_size < 3 * sizeof(char *)) {
errno= E2BIG;
if (overflow) {
errno = E2BIG;
return -1;
}
@@ -82,31 +41,8 @@ int execve(const char *path, char * const *argv, char * const *envp)
return -1;
}
/* Set arg count, init pointers to vector and string tables. */
* (size_t *) frame = argc;
vp = (char **) (frame + sizeof(argc));
sp = frame + string_off;
/* Load the argument vector and strings. */
for (ap= argv; *ap != NULL; ap++) {
*vp++= (char *) (sp - frame);
n= strlen(*ap) + 1;
memcpy(sp, *ap, n);
sp+= n;
}
*vp++= NULL;
/* Load the environment vector and strings. */
for (ep= envp; *ep != NULL; ep++) {
*vp++= (char *) (sp - frame);
n= strlen(*ep) + 1;
memcpy(sp, *ep, n);
sp+= n;
}
*vp++= NULL;
/* Padding. */
while (sp < frame + frame_size) *sp++= 0;
minix_stack_fill(path, argc, argv, envc, envp, frame_size, frame,
&vsp, &psp);
/* Clear unused message fields */
memset(&m, 0, sizeof(m));
@@ -116,15 +52,12 @@ int execve(const char *path, char * const *argv, char * const *envp)
m.m1_i2 = frame_size;
m.m1_p1 = (char *) __UNCONST(path);
m.m1_p2 = frame;
/* Tell PM/VFS we have left space for the aux vectors
* and executable name
*/
m.PMEXEC_FLAGS = PMEF_AUXVECTORSPACE | PMEF_EXECNAMESPACE1;
m.m1_p4 = (char *)(vsp + ((char *)psp - frame));
(void) _syscall(PM_PROC_NR, EXEC, &m);
/* Failure, return the memory used for the frame and exit. */
(void) sbrk(-frame_size);
return -1;
}

View File

@@ -0,0 +1,177 @@
/* Utilities to generate a proper C stack.
*
* Author: Lionel A. Sambuc.
*/
#include <sys/cdefs.h>
#include "namespace.h"
#include <lib.h>
#include <unistd.h>
#include <string.h>
#include <stddef.h>
#include <minix/param.h>
#include <sys/exec_elf.h>
#include <sys/exec.h>
#ifdef __weak_alias
__weak_alias(execve, _execve)
#endif
extern struct minix_kerninfo *_minix_kerninfo;
/* Create a stack image that only needs to be patched up slightly by
* the kernel to be used for the process to be executed.
*
* Every pointers are stored here as offset from the frame base, and
* will be adapted as required for the new process address space.
*
* The following parameters are passed by register to either __start
* for static binaries, or _rtld_start for dynamic ones:
* *fct, *ObjEntry, *ps_string
*
* The following stack layout is expected by _rtld():
*
* | XXXXXXXXXX | 0x0000_00000
* | ... |
* | ... | Top of the stack
* | argc |
* | *argv1 | points to the first char of the argv1
* | ... |
* | *argvN |
* | NULL |
* | *env1 |
* | ... |
* | *envN |
* | NULL |
* | ElfAuxV1 |
* | ... |
* | ElfAuxVX |
* | AuxExecName| fully resolve executable name, as an ASCIIZ string,
* at most PMEF_EXECNAMELEN1 long.
*
* Here we put first the strings, then word-align, then ps_strings, to
* comply with the expected layout of NetBSD. This seems to matter for
* the NetBSD ps command, so let's make sure we are compatible...
*
* | strings | Maybe followed by some padding to word-align.
* | **argv | \
* | argc | +---> ps_string structure content.
* | **env | |
* | envc | /
* | sigcode | On NetBSD, there may be a compatibility stub here,
* +------------+ for native code, it is not present.
* Stack Base , 0xF000_0000, descending stack.
*/
/* The minimum size of the frame is composed of:
* argc, the NULL terminator for argv as well as one for
* environ, the ELF Aux vectors, executable name and the
* ps_strings struct. */
#define STACK_MIN_SZ \
( \
sizeof(int) + sizeof(void *) * 2 + \
sizeof(AuxInfo) * PMEF_AUXVECTORS + PMEF_EXECNAMELEN1 + \
sizeof(struct ps_strings) \
)
/*****************************************************************************
* Computes stack size, argc, envc, for a given set of path, argv, envp. *
*****************************************************************************/
void minix_stack_params(const char *path, char * const *argv, char * const *envp,
size_t *stack_size, char *overflow, int *argc, int *envc)
{
char * const *p;
size_t const min_size = STACK_MIN_SZ;
*stack_size = min_size; /* Size of the new initial stack. */
*overflow = 0; /* No overflow yet. */
*argc = 0; /* Argument count. */
*envc = 0; /* Environment count */
/* Compute and add the size required to store argv and env. */
for (p = argv; *p != NULL; p++) {
size_t const n = sizeof(*p) + strlen(*p) + 1;
*stack_size += n;
if (*stack_size < n) {
*overflow = 1;
}
(*argc)++;
}
for (p = envp; *p != NULL; p++) {
size_t const n = sizeof(*p) + strlen(*p) + 1;
*stack_size += n;
if (*stack_size < n) {
*overflow = 1;
}
(*envc)++;
}
/* Compute the aligned frame size. */
*stack_size = (*stack_size + sizeof(void *) - 1) &
~(sizeof(void *) - 1);
if (*stack_size < min_size) {
/* This is possible only in case of overflow. */
*overflow = 1;
}
}
/*****************************************************************************
* Generate a stack in the buffer frame, ready to be used. *
*****************************************************************************/
void minix_stack_fill(const char *path, int argc, char * const *argv,
int envc, char * const *envp, size_t stack_size, char *frame,
int *vsp, struct ps_strings **psp)
{
char * const *p;
/* Frame pointers (a.k.a stack pointer within the buffer in current
* address space.) */
char *fp; /* byte aligned */
char **fpw; /* word aligned */
size_t const min_size = STACK_MIN_SZ;
/* Virtual address of the stack pointer, in new memory space. */
*vsp = _minix_kerninfo->kinfo->user_sp - stack_size;
/* Fill in the frame now. */
fpw = (char **) frame;
*fpw++ = (char *) argc;
/* The strings themselves are stored after the aux vectors,
* cf. top comment. */
fp = frame + (min_size - sizeof(struct ps_strings)) +
(envc + argc) * sizeof(char *);
/* Fill in argv and the environment, as well as copy the strings
* themselves. */
for (p = argv; *p != NULL; p++) {
size_t const n = strlen(*p) + 1;
*fpw++= (char *)(*vsp + (fp - frame));
memcpy(fp, *p, n);
fp += n;
}
*fpw++ = NULL;
for (p = envp; *p != NULL; p++) {
size_t const n = strlen(*p) + 1;
*fpw++= (char *)(*vsp + (fp - frame));
memcpy(fp, *p, n);
fp += n;
}
*fpw++ = NULL;
/* Padding, because of the stack alignement. */
while ((size_t)fp % sizeof(void *)) *fp++= 0;
/* Fill in the ps_string struct*/
*psp = (struct ps_strings *) fp;
(*psp)->ps_argvstr = (char **)(*vsp + sizeof(argc));
(*psp)->ps_nargvstr = argc;
(*psp)->ps_envstr = (*psp)->ps_argvstr + argc + 1;
(*psp)->ps_nenvstr = envc;
}

View File

@@ -75,33 +75,6 @@ int libexec_clear_memset(struct exec_info *execi, vir_bytes vaddr, size_t len)
return OK;
}
void libexec_patch_ptr(char stack[ARG_MAX], vir_bytes base)
{
/* When doing an exec(name, argv, envp) call, the user builds up a stack
* image with arg and env pointers relative to the start of the stack. Now
* these pointers must be relocated, since the stack is not positioned at
* address 0 in the user's address space.
*/
char **ap, flag;
vir_bytes v;
flag = 0; /* counts number of 0-pointers seen */
ap = (char **) stack; /* points initially to 'nargs' */
ap++; /* now points to argv[0] */
while (flag < 2) {
if (ap >= (char **) &stack[ARG_MAX]) return; /* too bad */
if (*ap != NULL) {
v = (vir_bytes) *ap; /* v is relative pointer */
v += base; /* relocate it */
*ap = (char *) v; /* put it back */
} else {
flag++;
}
ap++;
}
}
int libexec_pm_newexec(endpoint_t proc_e, struct exec_info *e)
{
int r;

View File

@@ -55,7 +55,6 @@ struct exec_info {
int elf_has_interpreter(char *exec_hdr, int hdr_len, char *interp, int maxsz);
int elf_phdr(char *exec_hdr, int hdr_len, vir_bytes *phdr);
void libexec_patch_ptr(char stack[ARG_MAX], vir_bytes base);
int libexec_pm_newexec(endpoint_t proc_e, struct exec_info *execi);
typedef int (*libexec_exec_loadfunc_t)(struct exec_info *execi);

View File

@@ -146,8 +146,8 @@ CPPFLAGS.${i}+= -I${LIBCDIR}/locale
.for i in access.c brk.c close.c environ.c execve.c fork.c \
getgid.c getpid.c geteuid.c getuid.c gettimeofday.c getvfsstat.c \
link.c loadname.c _mcontext.c mknod.c mmap.c nanosleep.c open.c \
read.c reboot.c sbrk.c select.c setuid.c sigprocmask.c stat.c \
stime.c syscall.c _ucontext.c umask.c unlink.c waitpid.c \
read.c reboot.c sbrk.c select.c setuid.c sigprocmask.c stack_utils.c \
stat.c stime.c syscall.c _ucontext.c umask.c unlink.c waitpid.c \
brksize.S _ipc.S _senda.S ucontext.S mmap.c init.c
.PATH.c: ${LIBCDIR}/sys-minix
.PATH.S: ${ARCHDIR}/sys-minix

View File

@@ -1,18 +1,17 @@
#include "syslib.h"
int sys_exec(proc_ep, ptr, prog_name, initpc)
endpoint_t proc_ep; /* process that did exec */
char *ptr; /* new stack pointer */
char *prog_name; /* name of the new program */
vir_bytes initpc;
int sys_exec(endpoint_t proc_ep, char *stack_ptr, char *progname,
vir_bytes pc, vir_bytes ps_str)
{
/* A process has exec'd. Tell the kernel. */
message m;
message m;
m.PR_ENDPT = proc_ep;
m.PR_STACK_PTR = ptr;
m.PR_NAME_PTR = prog_name;
m.PR_IP_PTR = (char *)initpc;
return(_kernel_call(SYS_EXEC, &m));
m.PR_ENDPT = proc_ep;
m.PR_STACK_PTR = stack_ptr;
m.PR_NAME_PTR = progname;
m.PR_IP_PTR = (char *)pc;
m.PR_PS_STR_PTR = (char *)ps_str;
return _kernel_call(SYS_EXEC, &m);
}