Index: kernel/generic/include/config.h
===================================================================
--- kernel/generic/include/config.h	(revision c06eb06bfa73f0453ea411eebf5fa21013e9f555)
+++ kernel/generic/include/config.h	(revision deaa22f01020fc8d432df92b990024d02d453083)
@@ -63,4 +63,7 @@
 	size_t memory_size;		/**< Size of detected memory in bytes. */
 	size_t kernel_size;		/**< Size of memory in bytes taken by kernel and stack */
+	
+	uintptr_t stack_base;	/**< Base adddress of initial stack */
+	size_t stack_size;		/**< Size of initial stack */
 } config_t;
 
Index: kernel/generic/include/main/main.h
===================================================================
--- kernel/generic/include/main/main.h	(revision deaa22f01020fc8d432df92b990024d02d453083)
+++ kernel/generic/include/main/main.h	(revision deaa22f01020fc8d432df92b990024d02d453083)
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2006 Martin Decky
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup main
+ * @{
+ */
+/** @file
+ */
+ 
+#ifndef __MAIN_H__
+#define __MAIN_H__
+
+#include <typedefs.h>
+
+extern uintptr_t stack_safe;
+
+#endif
+
+/** @}
+ */
Index: kernel/generic/src/main/main.c
===================================================================
--- kernel/generic/src/main/main.c	(revision c06eb06bfa73f0453ea411eebf5fa21013e9f555)
+++ kernel/generic/src/main/main.c	(revision deaa22f01020fc8d432df92b990024d02d453083)
@@ -106,4 +106,6 @@
 size_t hardcoded_kdata_size = 0;	/**< Size of the kernel data in bytes. */
 
+uintptr_t stack_safe = 0;	/**< Lowest safe stack virtual address */
+
 void main_bsp(void);
 void main_ap(void);
@@ -133,6 +135,4 @@
 void main_bsp(void)
 {
-	uintptr_t stackaddr;
-
 	config.cpu_count = 1;
 	config.cpu_active = 1;
@@ -142,21 +142,21 @@
 	
 	config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE);
-	stackaddr = config.base + config.kernel_size;
-	
-	/* Avoid placing kernel on top of init */
+	config.stack_size = CONFIG_STACK_SIZE;
+	
+	/* Initialy the stack is placed just after the kernel */
+	config.stack_base = config.base + config.kernel_size;
+	
+	/* Avoid placing stack on top of init */
 	count_t i;
-	bool overlap = false;
-	for (i = 0; i < init.cnt; i++)
-		if (PA_overlaps(stackaddr, CONFIG_STACK_SIZE, init.tasks[i].addr, init.tasks[i].size)) {
-			stackaddr = ALIGN_UP(init.tasks[i].addr + init.tasks[i].size, CONFIG_STACK_SIZE);
-			init.tasks[i].size = ALIGN_UP(init.tasks[i].size, CONFIG_STACK_SIZE) + CONFIG_STACK_SIZE;
-			overlap = true;
-		}
-	
-	if (!overlap)
-		config.kernel_size += CONFIG_STACK_SIZE;
+	for (i = 0; i < init.cnt; i++) {
+		if (PA_overlaps(config.stack_base, config.stack_size, init.tasks[i].addr, init.tasks[i].size))
+			config.stack_base = ALIGN_UP(init.tasks[i].addr + init.tasks[i].size, config.stack_size);
+	}
+	
+	if (config.stack_base < stack_safe)
+		config.stack_base = ALIGN_UP(stack_safe, PAGE_SIZE);
 	
 	context_save(&ctx);
-	context_set(&ctx, FADDR(main_bsp_separated_stack), stackaddr, THREAD_STACK_SIZE);
+	context_set(&ctx, FADDR(main_bsp_separated_stack), config.stack_base, THREAD_STACK_SIZE);
 	context_restore(&ctx);
 	/* not reached */
@@ -203,5 +203,6 @@
 
 	version_print();
-	printf("%.*p: hardcoded_ktext_size=%zdK, hardcoded_kdata_size=%zdK\n", sizeof(uintptr_t) * 2, config.base, hardcoded_ktext_size >> 10, hardcoded_kdata_size >> 10);
+	printf("kernel: %.*p hardcoded_ktext_size=%zdK, hardcoded_kdata_size=%zdK\n", sizeof(uintptr_t) * 2, config.base, hardcoded_ktext_size >> 10, hardcoded_kdata_size >> 10);
+	printf("stack:  %.*p size=%zdK\n", sizeof(uintptr_t) * 2, config.stack_base, config.stack_size >> 10);
 
 	arch_pre_smp_init();
Index: kernel/generic/src/mm/frame.c
===================================================================
--- kernel/generic/src/mm/frame.c	(revision c06eb06bfa73f0453ea411eebf5fa21013e9f555)
+++ kernel/generic/src/mm/frame.c	(revision deaa22f01020fc8d432df92b990024d02d453083)
@@ -864,4 +864,7 @@
 			addr = PFN2ADDR(confframe);
 			if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.base), config.kernel_size))
+				continue;
+			
+			if (overlaps(addr, PFN2ADDR(confcount), KA2PA(config.stack_base), config.stack_size))
 				continue;
 			
@@ -1069,7 +1072,6 @@
 	frame_arch_init();
 	if (config.cpu_active == 1) {
-		pfn_t firstframe = ADDR2PFN(KA2PA(config.base));
-		pfn_t lastframe = ADDR2PFN(KA2PA(config.base+config.kernel_size));
-		frame_mark_unavailable(firstframe,lastframe-firstframe+1);
+		frame_mark_unavailable(ADDR2PFN(KA2PA(config.base)), SIZE2FRAMES(config.kernel_size));
+		frame_mark_unavailable(ADDR2PFN(KA2PA(config.stack_base)), SIZE2FRAMES(config.stack_size));
 		
 		count_t i;
