From 0db0e9aaa24c5dbdac175c8e1ee2c9aa95ba4b22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 18 Nov 2004 18:15:39 +0000 Subject: [PATCH] KSTACK_SIZE, STACK_SIZE, and MAIN_THREAD_STACK_SIZE were all renamed. Fixed a bug in the team thread start: it did not set sizeLeft correctly before copying the arguments - shouldn't have had any consequences, though, as the size of the arguments is known before. Added a comment explaining the current layout of the main thread stack area. Now makes use of B_STACK_AREA and B_KERNEL_STACK_AREA. fill_thread_info() did not correctly set thread_info::stack_end. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10016 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/core/team.c | 29 ++++++++++++++++++++--------- src/kernel/core/thread.c | 28 ++++++++++++++++------------ 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/kernel/core/team.c b/src/kernel/core/team.c index b34c51d57d..fbee074316 100644 --- a/src/kernel/core/team.c +++ b/src/kernel/core/team.c @@ -1,10 +1,10 @@ /* -** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. -** Distributed under the terms of the Haiku License. -** -** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ + * Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + * + * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. + * Distributed under the terms of the NewOS License. + */ /* Team functions */ @@ -821,18 +821,27 @@ team_create_thread_start(void *args) // create an initial primary stack area + // Main stack area layout is currently as follows (starting from 0): + // + // size | usage + // -----------------------------+-------------------------------- + // USER_MAIN_THREAD_STACK_SIZE | actual stack + // TLS_SIZE | TLS data + // ENV_SIZE | environment variables + // arguments size | arguments passed to the team + // ToDo: make ENV_SIZE variable and put it on the heap? // ToDo: we could reserve the whole USER_STACK_REGION upfront... - sizeLeft = PAGE_ALIGN(MAIN_THREAD_STACK_SIZE + TLS_SIZE + ENV_SIZE + + sizeLeft = PAGE_ALIGN(USER_MAIN_THREAD_STACK_SIZE + TLS_SIZE + ENV_SIZE + get_arguments_data_size(teamArgs->args, teamArgs->arg_count)); t->user_stack_base = USER_STACK_REGION + USER_STACK_REGION_SIZE - sizeLeft; - t->user_stack_size = MAIN_THREAD_STACK_SIZE; + t->user_stack_size = USER_MAIN_THREAD_STACK_SIZE; // the exact location at the end of the user stack area sprintf(ustack_name, "%s_main_stack", team->name); t->user_stack_area = create_area_etc(team, ustack_name, (void **)&t->user_stack_base, - B_EXACT_ADDRESS, sizeLeft, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); + B_EXACT_ADDRESS, sizeLeft, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA | B_STACK_AREA); if (t->user_stack_area < 0) { dprintf("team_create_thread_start: could not create default user stack region\n"); return t->user_stack_area; @@ -851,6 +860,8 @@ team_create_thread_start(void *args) TRACE(("addr: stack base = 0x%lx, uargs = %p, udest = %p, sizeLeft = %lu\n", t->user_stack_base, uargs, udest, sizeLeft)); + sizeLeft = t->user_stack_base + sizeLeft - (addr_t)udest; + for (i = 0; i < argCount; i++) { ssize_t length = user_strlcpy(udest, teamArgs->args[i], sizeLeft); if (length < B_OK) { diff --git a/src/kernel/core/thread.c b/src/kernel/core/thread.c index 54c3eb7b0a..6968e1050c 100644 --- a/src/kernel/core/thread.c +++ b/src/kernel/core/thread.c @@ -1,10 +1,10 @@ /* -** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. -** Distributed under the terms of the Haiku License. -** -** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ + * Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + * + * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved. + * Distributed under the terms of the NewOS License. + */ /* Threading routines */ @@ -326,7 +326,8 @@ create_thread(const char *name, team_id teamID, thread_entry_func entry, snprintf(stack_name, B_OS_NAME_LENGTH, "%s_%lx_kstack", name, t->id); t->kernel_stack_area = create_area(stack_name, (void **)&t->kernel_stack_base, - B_ANY_KERNEL_ADDRESS, KSTACK_SIZE, B_FULL_LOCK, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); + B_ANY_KERNEL_ADDRESS, KERNEL_STACK_SIZE, B_FULL_LOCK, + B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_KERNEL_STACK_AREA); if (t->kernel_stack_area < 0) { // we're not yet part of a team, so we can just bail out @@ -382,12 +383,13 @@ create_thread(const char *name, team_id teamID, thread_entry_func entry, // the stack will be between USER_STACK_REGION and the main thread stack area // (the user stack of the main thread is created in team_create_team()) t->user_stack_base = USER_STACK_REGION; - t->user_stack_size = STACK_SIZE; + t->user_stack_size = USER_STACK_SIZE; snprintf(stack_name, B_OS_NAME_LENGTH, "%s_%lx_stack", name, t->id); t->user_stack_area = create_area_etc(team, stack_name, (void **)&t->user_stack_base, B_BASE_ADDRESS, - t->user_stack_size + TLS_SIZE, B_NO_LOCK, B_READ_AREA | B_WRITE_AREA); + t->user_stack_size + TLS_SIZE, B_NO_LOCK, + B_READ_AREA | B_WRITE_AREA | B_STACK_AREA); if (t->user_stack_area < 0) { // great, we have a fully running thread without a stack dprintf("create_thread: unable to create user stack!\n"); @@ -901,7 +903,8 @@ thread_exit(void) thread->kernel_stack_base = sDeathStacks[death_stack].address; // we will continue in thread_exit2(), on the new stack - arch_thread_switch_kstack_and_call(thread, thread->kernel_stack_base + KSTACK_SIZE, thread_exit2, &args); + arch_thread_switch_kstack_and_call(thread, thread->kernel_stack_base + KERNEL_STACK_SIZE, + thread_exit2, &args); } panic("never can get here\n"); @@ -1143,7 +1146,8 @@ thread_init(kernel_args *args) for (i = 0; i < sNumDeathStacks; i++) { sprintf(temp, "death_stack%d", i); sDeathStacks[i].area = create_area(temp, (void **)&sDeathStacks[i].address, - B_ANY_KERNEL_ADDRESS, KSTACK_SIZE, B_FULL_LOCK, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); + B_ANY_KERNEL_ADDRESS, KERNEL_STACK_SIZE, B_FULL_LOCK, + B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA | B_KERNEL_STACK_AREA); if (sDeathStacks[i].area < 0) { panic("error creating death stacks\n"); return sDeathStacks[i].area; @@ -1367,7 +1371,7 @@ fill_thread_info(struct thread *thread, thread_info *info, size_t size) info->user_time = thread->user_time; info->kernel_time = thread->kernel_time; info->stack_base = (void *)thread->user_stack_base; - info->stack_end = (void *)(thread->user_stack_base + STACK_SIZE); + info->stack_end = (void *)(thread->user_stack_base + thread->user_stack_size - 1); }