kernel: overhaul stack specification

The core kernel computes the initial stack pointer
for a thread, properly aligning it and subtracting out
any random offsets or thread-local storage areas.
arch_new_thread() no longer needs to make any calculations,
an initial stack frame may be placed at the bounds of
the new 'stack_ptr' parameter passed in. This parameter
replaces 'stack_size'.

thread->stack_info is now set before arch_new_thread()
is invoked, z_new_thread_init() has been removed.
The values populated may need to be adjusted on arches
which carve-out MPU guard space from the actual stack
buffer.

thread->stack_info now has a new member 'delta' which
indicates any offset applied for TLS or random offset.
It's used so the calculations don't need to be repeated
if the thread later drops to user mode.

CONFIG_INIT_STACKS logic is now performed inside
z_setup_new_thread(), before arch_new_thread() is called.

thread->stack_info is now defined as the canonical
user-accessible area within the stack object, including
random offsets and TLS. It will never include any
carved-out memory for MPU guards and must be updated at
runtime if guards are removed.

Available stack space is now optimized. Some arches may
need to significantly round up the buffer size to account
for page-level granularity or MPU power-of-two requirements.
This space is now accounted for and used by virtue of
the Z_THREAD_STACK_SIZE_ADJUST() call in z_setup_new_thread.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
Andrew Boie 2020-04-23 13:55:56 -07:00 committed by Anas Nashif
commit b0c155f3ca
27 changed files with 570 additions and 764 deletions

View file

@ -13,23 +13,17 @@ void z_thread_entry_wrapper(k_thread_entry_t thread,
void *arg3);
void arch_new_thread(struct k_thread *thread, k_thread_stack_t *stack,
size_t stack_size, k_thread_entry_t entry,
char *stack_ptr, k_thread_entry_t entry,
void *p1, void *p2, void *p3)
{
char *stack_memory = Z_THREAD_STACK_BUFFER(stack);
struct __esf *stack_init;
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE
const struct soc_esf soc_esf_init = {SOC_ESF_INIT};
#endif
z_new_thread_init(thread, stack_memory, stack_size);
/* Initial stack frame for thread */
stack_init = (struct __esf *)
Z_STACK_PTR_ALIGN(stack_memory +
stack_size - sizeof(struct __esf));
stack_init = Z_STACK_PTR_TO_FRAME(struct __esf, stack_ptr);
/* Setup the initial stack frame */
stack_init->a0 = (ulong_t)entry;