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

@@ -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;
}