Subject: stack growth
To: None <tech-kern@netbsd.org>
From: Matt Fredette <fredette@theory.lcs.mit.edu>
List: tech-kern
Date: 02/03/2002 10:50:13
One of the challenges of the HP PA-RISC architecture is that
a stack grows up instead of down.

So far, I've found relatively few places in the kernel that
assume that it grows down.  The worst offender is sys_execve().

This is a proposal for a set of macros that deal with stack
growth direction.  I'm not necessarily happy with them, but I
have been using them for the last month or so.

STACK_GROW and STACK_SHRINK adjust a stack pointer by some size,
no questions asked.  STACK_ALIGN aligns a stack pointer.

#ifdef MACHINE_STACK_GROWS_UP
#define	STACK_GROW(sp, sz)		(((caddr_t)(sp)) + (sz))
#define	STACK_SHRINK(sp, sz)		(((caddr_t)(sp)) - (sz))
#define	STACK_ALIGN(sp, bytes)  \
	((caddr_t)((((unsigned long)(sp)) + (bytes)) & ~(bytes)))
#else
#define	STACK_GROW(sp, sz)		(((caddr_t)(sp)) - (sz))
#define	STACK_SHRINK(sp, sz)		(((caddr_t)(sp)) + (sz))
#define	STACK_ALIGN(sp, bytes)  \
	((caddr_t)(((unsigned long)(sp)) & ~(bytes)))
#endif

STACK_ALLOC returns a pointer to allocated stack space of some 
size.  Since the pointer it returns may not be the "next" stack 
pointer, STACK_MAX takes such a pointer and the size allocated
and gives the maximum (in the "maxsaddr" sense) stack pointer
of the allocated memory.

#ifdef MACHINE_STACK_GROWS_UP
#define	STACK_ALLOC(sp, sz)		((caddr_t)(sp))
#define	STACK_MAX(p, sz)		(((caddr_t)(p)) + (sz))
#else
#define	STACK_ALLOC(sp, sz)		(((caddr_t)(sp)) - (sz))
#define	STACK_MAX(p, sz)		((caddr_t)(p))
#endif

Comments?  Thanks,

Matt

--
Matt Fredette