summaryrefslogtreecommitdiffstats
path: root/src/kernel
diff options
context:
space:
mode:
authorPatrick Williams <iawillia@us.ibm.com>2011-08-22 16:20:11 -0500
committerA. Patrick Williams III <iawillia@us.ibm.com>2011-08-31 14:24:59 -0500
commit0ebac914541254c4b9ee2a271f26cd67fc2b94a0 (patch)
tree872be77d5870ea788513d8cb044f837904ddf8cc /src/kernel
parentf7b7b56dea28dd69a44a877f7b7073c4496ced9e (diff)
downloadtalos-hostboot-0ebac914541254c4b9ee2a271f26cd67fc2b94a0.tar.gz
talos-hostboot-0ebac914541254c4b9ee2a271f26cd67fc2b94a0.zip
Dynamic stack support.
- Create stack segment. - Allocate stack blocks on stack create. Change-Id: Ida90055afb68f208c479b5fdc19d3d931d026105 Reviewed-on: http://gfw160.austin.ibm.com:8080/gerrit/271 Tested-by: Jenkins Server Reviewed-by: A. Patrick Williams III <iawillia@us.ibm.com>
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/block.C61
-rw-r--r--src/kernel/makefile2
-rw-r--r--src/kernel/stacksegment.C163
-rw-r--r--src/kernel/syscall.C8
-rw-r--r--src/kernel/taskmgr.C10
-rw-r--r--src/kernel/vmmmgr.C4
6 files changed, 234 insertions, 14 deletions
diff --git a/src/kernel/block.C b/src/kernel/block.C
index 68db26a95..1402bd60a 100644
--- a/src/kernel/block.C
+++ b/src/kernel/block.C
@@ -98,7 +98,7 @@ bool Block::handlePageFault(task_t* i_task, uint64_t i_addr)
//Done(waiting for response)
return true;
}
- else if (pte->allocate_from_zero)
+ else if (pte->isAllocateFromZero())
{
void* l_page = PageManager::allocatePage();
memset(l_page, '\0', PAGESIZE);
@@ -144,8 +144,21 @@ void Block::setPhysicalPage(uint64_t i_vAddr, uint64_t i_pAddr,
// Create virtual to physical mapping.
ShadowPTE* pte = getPTE(i_vAddr);
- pte->setPageAddr(i_pAddr);
- pte->setPresent(true);
+ if (i_pAddr != 0)
+ {
+ pte->setPageAddr(i_pAddr);
+ pte->setPresent(true);
+
+ // Modified an SPTE, clear the HPTE.
+ PageTableManager::delEntry(i_vAddr);
+ }
+ // If the page is already present, we might be changing permissions, so
+ // clear the HPTE.
+ else if (pte->isPresent())
+ {
+ PageTableManager::delEntry(i_vAddr);
+ }
+
switch(i_access)
{
case VmmManager::READ_O_ACCESS:
@@ -209,3 +222,45 @@ uint64_t Block::findPhysicalAddress(uint64_t i_vaddr) const
return paddr;
}
+
+void Block::setPageAllocateFromZero(uint64_t i_vAddr)
+{
+ // Check containment, call down chain if address isn't in this block.
+ if (!isContained(i_vAddr))
+ {
+ if (iv_nextBlock)
+ {
+ iv_nextBlock->setPageAllocateFromZero(i_vAddr);
+ }
+ else
+ {
+ // No block owns this address. Code bug.
+ kassert(iv_nextBlock);
+ }
+ return;
+ }
+
+ // Set page to allocate-from-zero.
+ ShadowPTE* pte = getPTE(i_vAddr);
+ pte->setAllocateFromZero(true);
+}
+
+void Block::releaseAllPages()
+{
+ // Release all pages from page table.
+ PageTableManager::delRangeVA(iv_baseAddr, iv_baseAddr + iv_size);
+
+ // Free all pages back to page manager.
+ for(uint64_t page = iv_baseAddr;
+ page < (iv_baseAddr + iv_size);
+ page += PAGESIZE)
+ {
+ ShadowPTE* pte = getPTE(page);
+ if (pte->isPresent() && (0 != pte->getPageAddr()))
+ {
+ PageManager::freePage(reinterpret_cast<void*>(pte->getPageAddr()));
+ pte->setPresent(false);
+ pte->setPageAddr(NULL);
+ }
+ }
+}
diff --git a/src/kernel/makefile b/src/kernel/makefile
index 24052a695..a0f1e2d7f 100644
--- a/src/kernel/makefile
+++ b/src/kernel/makefile
@@ -25,7 +25,7 @@ ROOTPATH = ../..
OBJS = start.o kernel.o console.o pagemgr.o heapmgr.o taskmgr.o cpumgr.o
OBJS += syscall.o scheduler.o spinlock.o exception.o vmmmgr.o timemgr.o
OBJS += futexmgr.o ptmgr.o segmentmgr.o devicesegment.o basesegment.o
-OBJS += block.o cpuid.o misc.o msghandler.o blockmsghdlr.o
+OBJS += block.o cpuid.o misc.o msghandler.o blockmsghdlr.o stacksegment.o
include ${ROOTPATH}/config.mk
diff --git a/src/kernel/stacksegment.C b/src/kernel/stacksegment.C
new file mode 100644
index 000000000..91b16e6a9
--- /dev/null
+++ b/src/kernel/stacksegment.C
@@ -0,0 +1,163 @@
+// IBM_PROLOG_BEGIN_TAG
+// This is an automatically generated prolog.
+//
+// $Source: src/kernel/stacksegment.C $
+//
+// IBM CONFIDENTIAL
+//
+// COPYRIGHT International Business Machines Corp. 2011
+//
+// p1
+//
+// Object Code Only (OCO) source materials
+// Licensed Internal Code Source Materials
+// IBM HostBoot Licensed Internal Code
+//
+// The source code for this program is not published or other-
+// wise divested of its trade secrets, irrespective of what has
+// been deposited with the U.S. Copyright Office.
+//
+// Origin: 30
+//
+// IBM_PROLOG_END
+
+#include <assert.h>
+#include <util/singleton.H>
+
+#include <kernel/stacksegment.H>
+#include <kernel/segmentmgr.H>
+#include <kernel/block.H>
+#include <errno.h>
+
+void StackSegment::init()
+{
+ Singleton<StackSegment>::instance()._init();
+}
+
+void* StackSegment::createStack(tid_t i_task)
+{
+ return Singleton<StackSegment>::instance()._createStack(i_task);
+}
+
+void StackSegment::deleteStack(tid_t i_task)
+{
+ Singleton<StackSegment>::instance()._deleteStack(i_task);
+}
+
+StackSegment::~StackSegment()
+{
+ // Release all blocks and associated pages.
+ StackBlockNode* l_node = NULL;
+ do
+ {
+ l_node = iv_blockList.remove();
+ if (NULL != l_node)
+ {
+ l_node->block->releaseAllPages();
+ delete l_node->block;
+ delete l_node;
+ }
+ } while (l_node != NULL);
+}
+
+bool StackSegment::handlePageFault(task_t* i_task, uint64_t i_addr)
+{
+ uint64_t l_addr_8mb = i_addr & ~((EIGHT_MEGABYTE) - 1);
+
+ StackBlockNode* l_node = iv_blockList.find(l_addr_8mb);
+
+ return (NULL == l_node ?
+ false :
+ l_node->block->handlePageFault(i_task, i_addr));
+}
+
+uint64_t StackSegment::findPhysicalAddress(uint64_t i_vaddr) const
+{
+ uint64_t l_addr_8mb = i_vaddr & ~((EIGHT_MEGABYTE) - 1);
+
+ StackBlockNode* l_node = iv_blockList.find(l_addr_8mb);
+
+ return (NULL == l_node ?
+ -EFAULT :
+ l_node->block->findPhysicalAddress(i_vaddr));
+}
+
+void StackSegment::_init()
+{
+ // Assign segment to segment manager.
+ SegmentManager::addSegment(this, SegmentManager::STACK_SEGMENT_ID);
+}
+
+void* StackSegment::_createStack(tid_t i_task)
+{
+ /* The segment is broken out into 8MB blocks so we need to place the
+ * stack somewhere within an 8MB range. The constrants are ensuring
+ * we have adequate protection and that the hashed page table does not
+ * have a large number of collisions. If we were to place all of the
+ * stacks at (8MB - 64k) there would be a large amount of contention on
+ * the same PTEG in the hashed page table.
+ *
+ * Design:
+ * - Provide 64k of protection minimum at the top and bottom of the
+ * stack.
+ * - Allow stack sizes up to 256k.
+ * - Expect typical (well performing) stacks of under 128k.
+ *
+ * Therefore, place stacks at:
+ * Bottom = 64k + 128k * (tid % 61).
+ * Top = Bottom + 256k - 8.
+ *
+ * This provides a possible range of 64k to (8MB - 64k), giving 64k of
+ * protection at each end. It also cycles the stacks through the 8MB
+ * range, and therefore the hashed page table, at 128k blocks. Finally,
+ * it provides for stack sizes up to 256k.
+ *
+ * Any attempt to grow the stack above 256k can be caught by killing the
+ * task (so we can re-write the offending code to not waste so much stack
+ * space).
+ */
+
+ uint64_t l_addr_8mb = i_task * EIGHT_MEGABYTE + ONE_TERABYTE;
+ // Ensure block doesn't already exist.
+ kassert(NULL == iv_blockList.find(l_addr_8mb));
+
+ // Calculate offset bounds of stack.
+ uint64_t l_offset_bottom = (64 + 128 * (i_task % 61)) * 1024;
+ uint64_t l_offset_top = l_offset_bottom + (256 * 1024) - 8;
+
+ uint64_t l_addr_bottom = l_addr_8mb + l_offset_bottom;
+ uint64_t l_addr_top = l_addr_8mb + l_offset_top;
+
+ // Create block.
+ Block* l_block = new Block(l_addr_bottom, 256 * 1024);
+ // Set pages to be allocate-from-zero.
+ for(uint64_t i = l_addr_bottom; i <= l_addr_top; i += PAGE_SIZE)
+ {
+ l_block->setPhysicalPage(i, 0, VmmManager::NORMAL_ACCESS);
+ l_block->setPageAllocateFromZero(i);
+ }
+
+ // Insert block to list.
+ StackBlockNode* l_node = new StackBlockNode();
+ l_node->key = l_addr_8mb;
+ l_node->block = l_block;
+ iv_blockList.insert(l_node);
+
+ // Return pointer to top of stack, since stacks grow down.
+ return reinterpret_cast<void*>(l_addr_top);
+}
+
+void StackSegment::_deleteStack(tid_t i_task)
+{
+ uint64_t l_addr_8mb = i_task * EIGHT_MEGABYTE + ONE_TERABYTE;
+
+ StackBlockNode* l_node = iv_blockList.find(l_addr_8mb);
+ kassert(NULL != l_node);
+ iv_blockList.erase(l_node);
+
+ l_node->block->releaseAllPages();
+ delete l_node->block;
+ delete l_node;
+
+ return;
+}
diff --git a/src/kernel/syscall.C b/src/kernel/syscall.C
index 8f69e934e..6faf47681 100644
--- a/src/kernel/syscall.C
+++ b/src/kernel/syscall.C
@@ -37,6 +37,7 @@
#include <kernel/misc.H>
#include <kernel/msghandler.H>
#include <kernel/vmmmgr.H>
+#include <kernel/stacksegment.H>
extern "C"
void kernel_execute_decrementer()
@@ -164,7 +165,7 @@ namespace Systemcalls
// TODO: Deal with join.
// Clean up task memory.
- PageManager::freePage(t->context.stack_ptr, TASK_DEFAULT_STACK_SIZE);
+ StackSegment::deleteStack(t->tid);
delete t;
}
@@ -218,7 +219,7 @@ namespace Systemcalls
if (m->type >= MSG_FIRST_SYS_TYPE)
{
- printkd("MsgSend> type=%d\n", m->type);
+ printkd("Invalid type for msg_send, type=%d.\n", m->type);
TASK_SETRTN(t, -EINVAL);
return;
}
@@ -252,7 +253,8 @@ namespace Systemcalls
if (m->type >= MSG_FIRST_SYS_TYPE)
{
- printkd("MsgSendRecv> type=%d\n", m->type);
+ printkd("Invalid message type for msg_sendrecv, type=%d.\n",
+ m->type);
TASK_SETRTN(t, -EINVAL);
return;
}
diff --git a/src/kernel/taskmgr.C b/src/kernel/taskmgr.C
index 00bb1d2c9..3fe2cff6c 100644
--- a/src/kernel/taskmgr.C
+++ b/src/kernel/taskmgr.C
@@ -25,6 +25,7 @@
#include <kernel/task.H>
#include <kernel/pagemgr.H>
#include <kernel/cpumgr.H>
+#include <kernel/stacksegment.H>
#include <sys/task.h>
#include <arch/ppc.H>
#include <string.h>
@@ -99,12 +100,9 @@ task_t* TaskManager::_createTask(TaskManager::task_fn_t t,
// Setup stack.
if (withStack)
{
- task->context.stack_ptr =
- PageManager::allocatePage(TASK_DEFAULT_STACK_SIZE);
- memset(task->context.stack_ptr, '\0',
- TASK_DEFAULT_STACK_SIZE * PAGESIZE);
- task->context.gprs[1] = ((uint64_t)task->context.stack_ptr) +
- TASK_DEFAULT_STACK_SIZE * PAGESIZE - 8;
+ task->context.stack_ptr = StackSegment::createStack(task->tid);
+ task->context.gprs[1] =
+ reinterpret_cast<uint64_t>(task->context.stack_ptr);
}
else
{
diff --git a/src/kernel/vmmmgr.C b/src/kernel/vmmmgr.C
index ba5b795f3..14a08da2c 100644
--- a/src/kernel/vmmmgr.C
+++ b/src/kernel/vmmmgr.C
@@ -27,8 +27,9 @@
#include <arch/ppc.H>
#include <kernel/ptmgr.H>
#include <kernel/segmentmgr.H>
-#include <kernel/devicesegment.H>
#include <kernel/basesegment.H>
+#include <kernel/stacksegment.H>
+#include <kernel/devicesegment.H>
extern void* data_load_address;
@@ -43,6 +44,7 @@ void VmmManager::init()
VmmManager& v = Singleton<VmmManager>::instance();
BaseSegment::init();
+ StackSegment::init();
DeviceSegment::init();
SegmentManager::initSLB();
OpenPOWER on IntegriCloud