Index: arch/ia32/include/asm.h
===================================================================
--- arch/ia32/include/asm.h	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ arch/ia32/include/asm.h	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -162,4 +162,5 @@
  * Return the base address of the current stack.
  * The stack is assumed to be STACK_SIZE bytes long.
+ * The stack must start on page boundary.
  */
 static inline __address get_stack_base(void)
Index: arch/ia32/include/context.h
===================================================================
--- arch/ia32/include/context.h	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ arch/ia32/include/context.h	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -32,9 +32,13 @@
 #include <arch/types.h>
 
+#define STACK_ITEM_SIZE	4
+
 /*
  * Both context_save() and context_restore() eat two doublewords from the stack.
  * First for pop of the saved register, second during ret instruction.
+ *
+ * One item is put onto stack to support get_stack_base().
  */
-#define SP_DELTA	8
+#define SP_DELTA	(8+STACK_ITEM_SIZE)
 
 struct context {
Index: arch/ia32/include/cpu.h
===================================================================
--- arch/ia32/include/cpu.h	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ arch/ia32/include/cpu.h	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -30,6 +30,4 @@
 #define __ia32_CPU_H__
 
-#include <config.h>
-#include <proc/thread.h>
 #include <typedefs.h>
 #include <arch/pm.h>
Index: arch/ia64/include/asm.h
===================================================================
--- arch/ia64/include/asm.h	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ arch/ia64/include/asm.h	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -33,8 +33,17 @@
 #include <config.h>
 
-/* TODO: implement the real stuff */
+/** Return base address of current stack
+ *
+ * Return the base address of the current stack.
+ * The stack is assumed to be STACK_SIZE long.
+ * The stack must start on page boundary.
+ */
 static inline __address get_stack_base(void)
 {
-	return NULL;
+	__u64 v;
+
+	__asm__ volatile ("and %0 = %1, r12" : "=r" (v) : "r" (~(STACK_SIZE-1)));
+	
+	return v;
 }
 
Index: arch/ia64/include/context.h
===================================================================
--- arch/ia64/include/context.h	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ arch/ia64/include/context.h	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -32,9 +32,13 @@
 #include <arch/types.h>
 
+#define STACK_ITEM_SIZE	16
+
 /*
  * context_save() and context_restore() are both leaf procedures.
  * No need to allocate scratch area.
+ *
+ * One item is put onto the stack to support get_stack_base().
  */
-#define SP_DELTA	0
+#define SP_DELTA	(0+STACK_ITEM_SIZE)
 
 #ifdef context_set
Index: arch/mips/include/asm.h
===================================================================
--- arch/mips/include/asm.h	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ arch/mips/include/asm.h	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -39,4 +39,5 @@
  * Return the base address of the current stack.
  * The stack is assumed to be STACK_SIZE bytes long.
+ * The stack must start on page boundary.
  */
 static inline __address get_stack_base(void)
Index: arch/mips/include/context.h
===================================================================
--- arch/mips/include/context.h	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ arch/mips/include/context.h	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -32,5 +32,10 @@
 #include <arch/types.h>
 
-#define SP_DELTA	0
+#define STACK_ITEM_SIZE	4
+
+/*
+ * Put one item onto the stack to support get_stack_base().
+ */
+#define SP_DELTA	(0+STACK_ITEM_SIZE)
 
 
Index: src/main/main.c
===================================================================
--- src/main/main.c	(revision 4dd07044b37d2d4e6a367ace9f19235048ed6b52)
+++ src/main/main.c	(revision 1fbbcd6bcfe6d5e18e5d5fb5f9e78adde6415dc3)
@@ -74,4 +74,14 @@
 size_t hardcoded_kdata_size = 0;
 
+/*
+ * Size of memory in bytes taken by kernel and heap.
+ */
+static size_t kernel_size;
+
+/*
+ * Extra space on heap to make the stack start on page boundary.
+ */
+static size_t heap_delta;
+
 void main_bsp(void);
 void main_ap(void);
@@ -86,5 +96,4 @@
 static void main_ap_separated_stack(void);
 
-
 /** Bootstrap CPU main kernel routine
  *
@@ -98,26 +107,16 @@
 	config.cpu_count = 1;
 	config.cpu_active = 1;
-	size_t size, delta;
-
-	/*
-	 * Calculate 'size' that kernel and heap occupies in memory.
-	 */
-	size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE;	 
-	 
-	/*
-	 * We need the boot stack to start on page boundary.
-	 * That is why 'delta' is calculated.
-	 */
-	delta = PAGE_SIZE - ((hardcoded_load_address + size) % PAGE_SIZE);
-	delta = (delta == PAGE_SIZE) ? 0 : delta;
-	
-	size += delta;
+
+	kernel_size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE;	 
+	heap_delta = PAGE_SIZE - ((hardcoded_load_address + kernel_size) % PAGE_SIZE);
+	heap_delta = (heap_delta == PAGE_SIZE) ? 0 : heap_delta;
+	kernel_size += heap_delta;
 
 	config.base = hardcoded_load_address;
 	config.memory_size = get_memory_size();
-	config.kernel_size = size + CONFIG_STACK_SIZE;
+	config.kernel_size = kernel_size + CONFIG_STACK_SIZE;
 
 	context_save(&ctx);
-	context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + size, CONFIG_STACK_SIZE);
+	context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + kernel_size, CONFIG_STACK_SIZE);
 	context_restore(&ctx);
 	/* not reached */
@@ -136,7 +135,11 @@
 	thread_t *t;
 
+	THE->preemption_disabled = 0;
+	THE->cpu = NULL;
+	THE->thread = NULL;
+	THE->task = NULL;
+
 	arch_pre_mm_init();
-
-	heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE);
+	heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE + heap_delta);
 	frame_init();
 	page_init();
