summaryrefslogtreecommitdiff
path: root/nuttx/arch/z80
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2013-03-20 20:27:08 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2013-03-20 20:27:08 +0000
commitae6758d5518a0e92f10476592dc156af38f36244 (patch)
treef7e4cbe989ad3df45b6e062b8457e45c81556350 /nuttx/arch/z80
parentfce497aecaf6826d6fec3cfcb761abeac7fe72bf (diff)
downloadnuttx-ae6758d5518a0e92f10476592dc156af38f36244.tar.gz
nuttx-ae6758d5518a0e92f10476592dc156af38f36244.tar.bz2
nuttx-ae6758d5518a0e92f10476592dc156af38f36244.zip
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
Diffstat (limited to 'nuttx/arch/z80')
-rw-r--r--nuttx/arch/z80/src/common/up_createstack.c38
-rw-r--r--nuttx/arch/z80/src/common/up_releasestack.c24
-rw-r--r--nuttx/arch/z80/src/common/up_usestack.c10
3 files changed, 59 insertions, 13 deletions
diff --git a/nuttx/arch/z80/src/common/up_createstack.c b/nuttx/arch/z80/src/common/up_createstack.c
index 1e47b09bd..a36a8aa7b 100644
--- a/nuttx/arch/z80/src/common/up_createstack.c
+++ b/nuttx/arch/z80/src/common/up_createstack.c
@@ -101,30 +101,52 @@
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)
{
- /* Yes.. free it */
+ /* Yes.. Release the old stack */
- sched_ufree(tcb->stack_alloc_ptr);
- tcb->stack_alloc_ptr = NULL;
+ up_release_stack(tcb, ttype);
}
- /* Do we need to allocate a stack? */
-
+ /* Do we need to allocate a new stack? */
+
if (!tcb->stack_alloc_ptr)
{
/* Allocate the stack. If DEBUG is enabled (but not stack debug),
* 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)
- tcb->stack_alloc_ptr = (uint32_t *)kuzalloc(stack_size);
+ tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size);
#else
- tcb->stack_alloc_ptr = (uint32_t *)kumalloc(stack_size);
+ tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size);
#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
+ /* Was the allocation successful? */
+
if (!tcb->stack_alloc_ptr)
{
sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size);
diff --git a/nuttx/arch/z80/src/common/up_releasestack.c b/nuttx/arch/z80/src/common/up_releasestack.c
index beefc4744..92c7bbd0a 100644
--- a/nuttx/arch/z80/src/common/up_releasestack.c
+++ b/nuttx/arch/z80/src/common/up_releasestack.c
@@ -1,7 +1,7 @@
/****************************************************************************
* 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>
*
* 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)
{
+ /* Is there a stack allocated? */
+
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;
}
+ /* The size of the allocated stack is now zero */
+
dtcb->adj_stack_size = 0;
}
diff --git a/nuttx/arch/z80/src/common/up_usestack.c b/nuttx/arch/z80/src/common/up_usestack.c
index 8d322e705..095b4f5d5 100644
--- a/nuttx/arch/z80/src/common/up_usestack.c
+++ b/nuttx/arch/z80/src/common/up_usestack.c
@@ -1,7 +1,7 @@
/****************************************************************************
* 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>
*
* 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 size_of_stack;
+ /* Is there already a stack allocated? */
+
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;