In the kernel build, allocate the stacks for kernel threads from the kernel heap so that they are protected from medddling by the applications

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5766 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2013-03-20 20:27:08 +00:00
parent 020cc2248e
commit c86f502f2e
29 changed files with 711 additions and 265 deletions

View File

@ -146,17 +146,19 @@ static void *memset32(void *s, uint32_t c, size_t n)
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
@ -164,12 +166,32 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);
@ -218,7 +240,7 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
*/ */
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_STACK)
memset32(tcb->stack_alloc_ptr, 0xDEADBEEF, tcb->adj_stack_size/4); memset32(tcb->stack_alloc_ptr, 0xdeadbeef, tcb->adj_stack_size/4);
#endif #endif
up_ledon(LED_STACKCREATED); up_ledon(LED_STACKCREATED);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/arm/src/common/up_releasestack.c * arch/arm/src/common/up_releasestack.c
* *
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -91,11 +91,31 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -121,12 +121,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes... Release the old stack allocation */
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;

View File

@ -105,30 +105,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (FAR void *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (FAR void *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/avr/src/avr/up_usestack.c * arch/avr/src/avr/up_usestack.c
* *
* Copyright (C) 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -98,12 +98,12 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack allocation */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;

View File

@ -104,30 +104,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (FAR void *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (FAR void *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/avr/src/avr32/up_usestack.c * arch/avr/src/avr32/up_usestack.c
* *
* Copyright (C) 2010 Gregory Nutt. All rights reserved. * Copyright (C) 2010, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -94,12 +94,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes.. Release the old stack allocation */
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/avr/src/common/up_releasestack.c * arch/avr/src/common/up_releasestack.c
* *
* Copyright (C) 2010 Gregory Nutt. All rights reserved. * Copyright (C) 2010, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -91,12 +91,32 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -101,30 +101,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/hc/src/common/up_releasestack.c * arch/hc/src/common/up_releasestack.c
* *
* Copyright (C) 2009 Gregory Nutt. All rights reserved. * Copyright (C) 2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -92,11 +92,31 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/hc/src/common/up_usestack.c * arch/hc/src/common/up_usestack.c
* *
* Copyright (C) 2009 Gregory Nutt. All rights reserved. * Copyright (C) 2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -93,13 +93,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes.. Release the old stack allocation */
tcb->stack_alloc_ptr = NULL;
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;

View File

@ -122,30 +122,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/mips/src/common/up_releasestack.c * arch/mips/src/common/up_releasestack.c
* *
* Copyright (C) 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -91,11 +91,31 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/mips/src/common/up_usestack.c * arch/mips/src/common/up_usestack.c
* *
* Copyright (C) 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -94,12 +94,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes.. Release the old stack allocation */
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;

View File

@ -66,11 +66,11 @@ static inline void up_switchcontext(struct tcb_s *ctcb, struct tcb_s *ntcb)
{ {
// do nothing if two tasks are the same // do nothing if two tasks are the same
if (ctcb == ntcb) if (ctcb == ntcb)
return; return;
// this function can not be called in interrupt // this function can not be called in interrupt
if (up_interrupt_context()) { if (up_interrupt_context()) {
panic("%s: try to switch context in interrupt\n", __func__); panic("%s: try to switch context in interrupt\n", __func__);
} }
// start switch // start switch
@ -81,19 +81,19 @@ static inline void up_switchcontext(struct tcb_s *ctcb, struct tcb_s *ntcb)
void up_initialize(void) void up_initialize(void)
{ {
extern pidhash_t g_pidhash[]; extern pidhash_t g_pidhash[];
extern void vdev_init(void); extern void vdev_init(void);
extern void nuttx_arch_init(void); extern void nuttx_arch_init(void);
// intialize the current_task to g_idletcb // intialize the current_task to g_idletcb
current_task = g_pidhash[PIDHASH(0)].tcb; current_task = g_pidhash[PIDHASH(0)].tcb;
// OS memory alloc system is ready // OS memory alloc system is ready
use_os_kmalloc = 1; use_os_kmalloc = 1;
// rgmp vdev init // rgmp vdev init
vdev_init(); vdev_init();
nuttx_arch_init(); nuttx_arch_init();
// enable interrupt // enable interrupt
local_irq_enable(); local_irq_enable();
@ -106,13 +106,14 @@ void up_idle(void)
void up_allocate_heap(void **heap_start, size_t *heap_size) void up_allocate_heap(void **heap_start, size_t *heap_size)
{ {
void *boot_freemem = boot_alloc(0, sizeof(int)); void *boot_freemem = boot_alloc(0, sizeof(int));
*heap_start = boot_freemem; *heap_start = boot_freemem;
*heap_size = KERNBASE + kmem_size - (uint32_t)boot_freemem; *heap_size = KERNBASE + kmem_size - (uint32_t)boot_freemem;
} }
int up_create_stack(struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
uint32_t *stack_alloc_ptr;
int ret = ERROR; int ret = ERROR;
size_t *adj_stack_ptr; size_t *adj_stack_ptr;
@ -123,18 +124,27 @@ int up_create_stack(struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
/* Allocate the memory for the stack */ /* Allocate the memory for the stack */
uint32_t *stack_alloc_ptr = (uint32_t*)kumalloc(adj_stack_size); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) {
stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
} else
#endif
{
stack_alloc_ptr = (uint32_t*)kumalloc(adj_stack_size);
}
if (stack_alloc_ptr) { if (stack_alloc_ptr) {
/* This is the address of the last word in the allocation */ /* This is the address of the last word in the allocation */
adj_stack_ptr = &stack_alloc_ptr[adj_stack_words - 1]; adj_stack_ptr = &stack_alloc_ptr[adj_stack_words - 1];
/* Save the values in the TCB */ /* Save the values in the TCB */
tcb->adj_stack_size = adj_stack_size; tcb->adj_stack_size = adj_stack_size;
tcb->stack_alloc_ptr = stack_alloc_ptr; tcb->stack_alloc_ptr = stack_alloc_ptr;
tcb->adj_stack_ptr = (void *)((unsigned int)adj_stack_ptr & ~7); tcb->adj_stack_ptr = (void *)((unsigned int)adj_stack_ptr & ~7);
ret = OK; ret = OK;
} }
return ret; return ret;
} }
@ -160,13 +170,28 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
void up_release_stack(struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(struct tcb_s *dtcb, uint8_t ttype)
{ {
if (dtcb->stack_alloc_ptr) { /* Is there a stack allocated? */
kufree(dtcb->stack_alloc_ptr);
}
dtcb->stack_alloc_ptr = NULL; if (dtcb->stack_alloc_ptr) {
dtcb->adj_stack_size = 0; #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
dtcb->adj_stack_ptr = NULL; /* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL) {
kfree(dtcb->stack_alloc_ptr);
} else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
kufree(dtcb->stack_alloc_ptr);
}
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL;
dtcb->adj_stack_size = 0;
dtcb->adj_stack_ptr = NULL;
} }
/**************************************************************************** /****************************************************************************
@ -196,43 +221,43 @@ void up_block_task(struct tcb_s *tcb, tstate_t task_state)
{ {
/* Verify that the context switch can be performed */ /* Verify that the context switch can be performed */
if ((tcb->task_state < FIRST_READY_TO_RUN_STATE) || if ((tcb->task_state < FIRST_READY_TO_RUN_STATE) ||
(tcb->task_state > LAST_READY_TO_RUN_STATE)) { (tcb->task_state > LAST_READY_TO_RUN_STATE)) {
warn("%s: task sched error\n", __func__); warn("%s: task sched error\n", __func__);
return; return;
} }
else { else {
struct tcb_s *rtcb = current_task; struct tcb_s *rtcb = current_task;
bool switch_needed; bool switch_needed;
/* Remove the tcb task from the ready-to-run list. If we /* Remove the tcb task from the ready-to-run list. If we
* are blocking the task at the head of the task list (the * are blocking the task at the head of the task list (the
* most likely case), then a context switch to the next * most likely case), then a context switch to the next
* ready-to-run task is needed. In this case, it should * ready-to-run task is needed. In this case, it should
* also be true that rtcb == tcb. * also be true that rtcb == tcb.
*/ */
switch_needed = sched_removereadytorun(tcb); switch_needed = sched_removereadytorun(tcb);
/* Add the task to the specified blocked task list */ /* Add the task to the specified blocked task list */
sched_addblocked(tcb, (tstate_t)task_state); sched_addblocked(tcb, (tstate_t)task_state);
/* Now, perform the context switch if one is needed */ /* Now, perform the context switch if one is needed */
if (switch_needed) { if (switch_needed) {
struct tcb_s *nexttcb; struct tcb_s *nexttcb;
// this part should not be executed in interrupt context // this part should not be executed in interrupt context
if (up_interrupt_context()) { if (up_interrupt_context()) {
panic("%s: %d\n", __func__, __LINE__); panic("%s: %d\n", __func__, __LINE__);
} }
// If there are any pending tasks, then add them to the g_readytorun // If there are any pending tasks, then add them to the g_readytorun
// task list now. It should be the up_realease_pending() called from // task list now. It should be the up_realease_pending() called from
// sched_unlock() to do this for disable preemption. But it block // sched_unlock() to do this for disable preemption. But it block
// itself, so it's OK. // itself, so it's OK.
if (g_pendingtasks.head) { if (g_pendingtasks.head) {
warn("Disable preemption failed for task block itself\n"); warn("Disable preemption failed for task block itself\n");
sched_mergepending(); sched_mergepending();
} }
nexttcb = (struct tcb_s*)g_readytorun.head; nexttcb = (struct tcb_s*)g_readytorun.head;
// context switch // context switch
up_switchcontext(rtcb, nexttcb); up_switchcontext(rtcb, nexttcb);
} }
} }
} }
@ -256,31 +281,31 @@ void up_unblock_task(struct tcb_s *tcb)
{ {
/* Verify that the context switch can be performed */ /* Verify that the context switch can be performed */
if ((tcb->task_state < FIRST_BLOCKED_STATE) || if ((tcb->task_state < FIRST_BLOCKED_STATE) ||
(tcb->task_state > LAST_BLOCKED_STATE)) { (tcb->task_state > LAST_BLOCKED_STATE)) {
warn("%s: task sched error\n", __func__); warn("%s: task sched error\n", __func__);
return; return;
} }
else { else {
struct tcb_s *rtcb = current_task; struct tcb_s *rtcb = current_task;
/* Remove the task from the blocked task list */ /* Remove the task from the blocked task list */
sched_removeblocked(tcb); sched_removeblocked(tcb);
/* Reset its timeslice. This is only meaningful for round /* Reset its timeslice. This is only meaningful for round
* robin tasks but it doesn't here to do it for everything * robin tasks but it doesn't here to do it for everything
*/ */
#if CONFIG_RR_INTERVAL > 0 #if CONFIG_RR_INTERVAL > 0
tcb->timeslice = CONFIG_RR_INTERVAL / MSEC_PER_TICK; tcb->timeslice = CONFIG_RR_INTERVAL / MSEC_PER_TICK;
#endif #endif
// Add the task in the correct location in the prioritized // Add the task in the correct location in the prioritized
// g_readytorun task list. // g_readytorun task list.
if (sched_addreadytorun(tcb) && !up_interrupt_context()) { if (sched_addreadytorun(tcb) && !up_interrupt_context()) {
/* The currently active task has changed! */ /* The currently active task has changed! */
struct tcb_s *nexttcb = (struct tcb_s*)g_readytorun.head; struct tcb_s *nexttcb = (struct tcb_s*)g_readytorun.head;
// context switch // context switch
up_switchcontext(rtcb, nexttcb); up_switchcontext(rtcb, nexttcb);
} }
} }
} }
@ -295,11 +320,11 @@ void up_release_pending(void)
/* Merge the g_pendingtasks list into the g_readytorun task list */ /* Merge the g_pendingtasks list into the g_readytorun task list */
if (sched_mergepending()) { if (sched_mergepending()) {
/* The currently active task has changed! */ /* The currently active task has changed! */
struct tcb_s *nexttcb = (struct tcb_s*)g_readytorun.head; struct tcb_s *nexttcb = (struct tcb_s*)g_readytorun.head;
// context switch // context switch
up_switchcontext(rtcb, nexttcb); up_switchcontext(rtcb, nexttcb);
} }
} }
@ -308,54 +333,54 @@ void up_reprioritize_rtr(struct tcb_s *tcb, uint8_t priority)
/* Verify that the caller is sane */ /* Verify that the caller is sane */
if (tcb->task_state < FIRST_READY_TO_RUN_STATE || if (tcb->task_state < FIRST_READY_TO_RUN_STATE ||
tcb->task_state > LAST_READY_TO_RUN_STATE tcb->task_state > LAST_READY_TO_RUN_STATE
#if SCHED_PRIORITY_MIN > UINT8_MIN #if SCHED_PRIORITY_MIN > UINT8_MIN
|| priority < SCHED_PRIORITY_MIN || priority < SCHED_PRIORITY_MIN
#endif #endif
#if SCHED_PRIORITY_MAX < UINT8_MAX #if SCHED_PRIORITY_MAX < UINT8_MAX
|| priority > SCHED_PRIORITY_MAX || priority > SCHED_PRIORITY_MAX
#endif #endif
) { ) {
warn("%s: task sched error\n", __func__); warn("%s: task sched error\n", __func__);
return; return;
} }
else { else {
struct tcb_s *rtcb = current_task; struct tcb_s *rtcb = current_task;
bool switch_needed; bool switch_needed;
/* Remove the tcb task from the ready-to-run list. /* Remove the tcb task from the ready-to-run list.
* sched_removereadytorun will return true if we just * sched_removereadytorun will return true if we just
* remove the head of the ready to run list. * remove the head of the ready to run list.
*/ */
switch_needed = sched_removereadytorun(tcb); switch_needed = sched_removereadytorun(tcb);
/* Setup up the new task priority */ /* Setup up the new task priority */
tcb->sched_priority = (uint8_t)priority; tcb->sched_priority = (uint8_t)priority;
/* Return the task to the specified blocked task list. /* Return the task to the specified blocked task list.
* sched_addreadytorun will return true if the task was * sched_addreadytorun will return true if the task was
* added to the new list. We will need to perform a context * added to the new list. We will need to perform a context
* switch only if the EXCLUSIVE or of the two calls is non-zero * switch only if the EXCLUSIVE or of the two calls is non-zero
* (i.e., one and only one the calls changes the head of the * (i.e., one and only one the calls changes the head of the
* ready-to-run list). * ready-to-run list).
*/ */
switch_needed ^= sched_addreadytorun(tcb); switch_needed ^= sched_addreadytorun(tcb);
/* Now, perform the context switch if one is needed */ /* Now, perform the context switch if one is needed */
if (switch_needed && !up_interrupt_context()) { if (switch_needed && !up_interrupt_context()) {
struct tcb_s *nexttcb; struct tcb_s *nexttcb;
// If there are any pending tasks, then add them to the g_readytorun // If there are any pending tasks, then add them to the g_readytorun
// task list now. It should be the up_realease_pending() called from // task list now. It should be the up_realease_pending() called from
// sched_unlock() to do this for disable preemption. But it block // sched_unlock() to do this for disable preemption. But it block
// itself, so it's OK. // itself, so it's OK.
if (g_pendingtasks.head) { if (g_pendingtasks.head) {
warn("Disable preemption failed for reprioritize task\n"); warn("Disable preemption failed for reprioritize task\n");
sched_mergepending(); sched_mergepending();
} }
nexttcb = (struct tcb_s*)g_readytorun.head; nexttcb = (struct tcb_s*)g_readytorun.head;
// context switch // context switch
up_switchcontext(rtcb, nexttcb); up_switchcontext(rtcb, nexttcb);
} }
} }
} }
@ -387,26 +412,26 @@ void up_assert(const uint8_t *filename, int line)
// which will stop the OS // which will stop the OS
// if in user space just terminate the task // if in user space just terminate the task
if (up_interrupt_context() || current_task->pid == 0) { if (up_interrupt_context() || current_task->pid == 0) {
panic("%s: %d\n", __func__, __LINE__); panic("%s: %d\n", __func__, __LINE__);
} }
else { else {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
void up_assert_code(const uint8_t *filename, int line, int code) void up_assert_code(const uint8_t *filename, int line, int code)
{ {
fprintf(stderr, "Assertion failed at file:%s line: %d error code: %d\n", fprintf(stderr, "Assertion failed at file:%s line: %d error code: %d\n",
filename, line, code); filename, line, code);
// in interrupt context or idle task means kernel error // in interrupt context or idle task means kernel error
// which will stop the OS // which will stop the OS
// if in user space just terminate the task // if in user space just terminate the task
if (up_interrupt_context() || current_task->pid == 0) { if (up_interrupt_context() || current_task->pid == 0) {
panic("%s: %d\n", __func__, __LINE__); panic("%s: %d\n", __func__, __LINE__);
} }
else { else {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -417,38 +442,38 @@ void up_schedule_sigaction(struct tcb_s *tcb, sig_deliver_t sigdeliver)
{ {
/* Refuse to handle nested signal actions */ /* Refuse to handle nested signal actions */
if (!tcb->xcp.sigdeliver) { if (!tcb->xcp.sigdeliver) {
int flags; int flags;
/* Make sure that interrupts are disabled */ /* Make sure that interrupts are disabled */
local_irq_save(flags); local_irq_save(flags);
// First, handle some special cases when the signal is // First, handle some special cases when the signal is
// being delivered to the currently executing task. // being delivered to the currently executing task.
if (tcb == current_task) { if (tcb == current_task) {
// CASE 1: We are not in an interrupt handler and // CASE 1: We are not in an interrupt handler and
// a task is signalling itself for some reason. // a task is signalling itself for some reason.
if (!up_interrupt_context()) { if (!up_interrupt_context()) {
// In this case just deliver the signal now. // In this case just deliver the signal now.
sigdeliver(tcb); sigdeliver(tcb);
} }
// CASE 2: We are in an interrupt handler AND the // CASE 2: We are in an interrupt handler AND the
// interrupted task is the same as the one that // interrupted task is the same as the one that
// must receive the signal. // must receive the signal.
else { else {
tcb->xcp.sigdeliver = sigdeliver; tcb->xcp.sigdeliver = sigdeliver;
} }
} }
// Otherwise, we are (1) signaling a task is not running // Otherwise, we are (1) signaling a task is not running
// from an interrupt handler or (2) we are not in an // from an interrupt handler or (2) we are not in an
// interrupt handler and the running task is signalling // interrupt handler and the running task is signalling
// some non-running task. // some non-running task.
else { else {
tcb->xcp.sigdeliver = sigdeliver; tcb->xcp.sigdeliver = sigdeliver;
push_xcptcontext(&tcb->xcp); push_xcptcontext(&tcb->xcp);
} }
local_irq_restore(flags); local_irq_restore(flags);
} }
} }
@ -458,7 +483,7 @@ void up_schedule_sigaction(struct tcb_s *tcb, sig_deliver_t sigdeliver)
bool up_interrupt_context(void) bool up_interrupt_context(void)
{ {
if (nest_irq) if (nest_irq)
return true; return true;
return false; return false;
} }
@ -497,7 +522,7 @@ void up_sigdeliver(struct Trapframe *tf)
void up_cxxinitialize(void) void up_cxxinitialize(void)
{ {
rgmp_cxx_init(); rgmp_cxx_init();
} }
#endif #endif

View File

@ -101,30 +101,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/sh/src/common/up_releasestack.c * arch/sh/src/common/up_releasestack.c
* *
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -91,11 +91,31 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/sh/src/common/up_usestack.c * arch/sh/src/common/up_usestack.c
* *
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -94,13 +94,17 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes.. Release the old stack allocation */
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;
/* The Arm7Tdmi uses a push-down stack: the stack grows /* The Arm7Tdmi uses a push-down stack: the stack grows

View File

@ -102,6 +102,7 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
uint32_t *stack_alloc_ptr;
int ret = ERROR; int ret = ERROR;
/* Move up to next even word boundary if necessary */ /* Move up to next even word boundary if necessary */
@ -111,7 +112,21 @@ int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
/* Allocate the memory for the stack */ /* Allocate the memory for the stack */
uint32_t *stack_alloc_ptr = (uint32_t*)kumalloc(adj_stack_size); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
}
else
#endif
{
stack_alloc_ptr = (uint32_t*)kumalloc(adj_stack_size);
}
/* Was the allocation successful? */
if (stack_alloc_ptr) if (stack_alloc_ptr)
{ {
/* This is the address of the last word in the allocation */ /* This is the address of the last word in the allocation */

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* up_releasestack.c * up_releasestack.c
* *
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -93,11 +93,28 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
free(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
} }
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
dtcb->adj_stack_ptr = NULL; dtcb->adj_stack_ptr = NULL;

View File

@ -103,30 +103,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/x86/src/common/up_releasestack.c * arch/x86/src/common/up_releasestack.c
* *
* Copyright (C) 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -91,11 +91,31 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/x86/src/common/up_usestack.c * arch/x86/src/common/up_usestack.c
* *
* Copyright (C) 2011 Gregory Nutt. All rights reserved. * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -94,12 +94,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes.. Release the old stack allocation */
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;

View File

@ -102,30 +102,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* common/up_releasestack.c * common/up_releasestack.c
* *
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -91,11 +91,31 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/z16/common/up_usestack.c * arch/z16/common/up_usestack.c
* *
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -94,12 +94,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes.. Release the old stack allocation */
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;

View File

@ -101,30 +101,52 @@
int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype) int up_create_stack(FAR struct tcb_s *tcb, size_t stack_size, uint8_t ttype)
{ {
/* Is there already a stack allocated of a different size? */ /* Is there already a stack allocated of a different size? Because of
* alignment issues, stack_size might erroneously appear to be of a
* different size. Fortunately, this is not a critical operation.
*/
if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size)
{ {
/* Yes.. free it */ /* Yes.. Release the old stack */
sched_ufree(tcb->stack_alloc_ptr); up_release_stack(tcb, ttype);
tcb->stack_alloc_ptr = NULL;
} }
/* Do we need to allocate a stack? */ /* Do we need to allocate a new stack? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
/* Allocate the stack. If DEBUG is enabled (but not stack debug), /* Allocate the stack. If DEBUG is enabled (but not stack debug),
* then create a zeroed stack to make stack dumps easier to trace. * then create a zeroed stack to make stack dumps easier to trace.
*/ */
#if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) #if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else #else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size); tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#endif #endif
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK)
tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
#else
tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
#endif
}
#ifdef CONFIG_DEBUG #ifdef CONFIG_DEBUG
/* Was the allocation successful? */
if (!tcb->stack_alloc_ptr) if (!tcb->stack_alloc_ptr)
{ {
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* common/up_releasestack.c * common/up_releasestack.c
* *
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -91,11 +91,31 @@
void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype) void up_release_stack(FAR struct tcb_s *dtcb, uint8_t ttype)
{ {
/* Is there a stack allocated? */
if (dtcb->stack_alloc_ptr) if (dtcb->stack_alloc_ptr)
{ {
sched_ufree(dtcb->stack_alloc_ptr); #if defined(CONFIG_NUTTX_KERNEL) && defined(CONFIG_MM_KERNEL_HEAP)
/* Use the kernel allocator if this is a kernel thread */
if (ttype == TCB_FLAG_TTYPE_KERNEL)
{
sched_kfree(dtcb->stack_alloc_ptr);
}
else
#endif
{
/* Use the user-space allocator if this is a task or pthread */
sched_ufree(dtcb->stack_alloc_ptr);
}
/* Mark the stack freed */
dtcb->stack_alloc_ptr = NULL; dtcb->stack_alloc_ptr = NULL;
} }
/* The size of the allocated stack is now zero */
dtcb->adj_stack_size = 0; dtcb->adj_stack_size = 0;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* arch/z80/src/common/up_usestack.c * arch/z80/src/common/up_usestack.c
* *
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -93,12 +93,16 @@ int up_use_stack(struct tcb_s *tcb, void *stack, size_t stack_size)
size_t top_of_stack; size_t top_of_stack;
size_t size_of_stack; size_t size_of_stack;
/* Is there already a stack allocated? */
if (tcb->stack_alloc_ptr) if (tcb->stack_alloc_ptr)
{ {
sched_ufree(tcb->stack_alloc_ptr); /* Yes.. Release the old stack allocation */
up_release_stack(tcb, tcb->flags & TCB_FLAG_TTYPE_MASK);
} }
/* Save the stack allocation */ /* Save the new stack allocation */
tcb->stack_alloc_ptr = stack; tcb->stack_alloc_ptr = stack;