Index: kernel/generic/include/mm/frame.h
===================================================================
--- kernel/generic/include/mm/frame.h	(revision f47fd19f9885f194c0b816b7814357789c9565e2)
+++ kernel/generic/include/mm/frame.h	(revision 32fffef00aa36a7913892cc0d7ff6eff828b2e7e)
@@ -28,5 +28,5 @@
  */
 
- /** @addtogroup genericmm
+/** @addtogroup genericmm
  * @{
  */
@@ -34,6 +34,6 @@
  */
 
-#ifndef __FRAME_H__
-#define __FRAME_H__
+#ifndef KERN_FRAME_H_
+#define KERN_FRAME_H_
 
 #include <arch/types.h>
@@ -62,8 +62,4 @@
 #define FRAME_NO_RECLAIM        0x4     /* do not start reclaiming when no free memory */
 
-#define FRAME_OK		0	/* frame_alloc return status */
-#define FRAME_NO_MEMORY		1	/* frame_alloc return status */
-#define FRAME_ERROR		2	/* frame_alloc return status */
-
 static inline uintptr_t PFN2ADDR(pfn_t frame)
 {
@@ -89,5 +85,5 @@
 #define IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame)	(((frame_index_abs((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1)
 
-#define frame_alloc(order, flags)				frame_alloc_generic(order, flags, NULL)
+#define frame_alloc(order, flags)		frame_alloc_generic(order, flags, NULL)
 
 extern void frame_init(void);
@@ -112,5 +108,4 @@
 #endif
 
- /** @}
+/** @}
  */
-
Index: kernel/generic/include/proc/thread.h
===================================================================
--- kernel/generic/include/proc/thread.h	(revision f47fd19f9885f194c0b816b7814357789c9565e2)
+++ kernel/generic/include/proc/thread.h	(revision 32fffef00aa36a7913892cc0d7ff6eff828b2e7e)
@@ -53,5 +53,5 @@
 #define THREAD_STACK_SIZE	STACK_SIZE
 
-/**< Thread states. */
+/** Thread states. */
 enum state {
 	Invalid,	/**< It is an error, if thread is found in this state. */
@@ -66,5 +66,5 @@
 extern char *thread_states[];
 
-/**< Join types. */
+/** Join types. */
 typedef enum {
 	None,
@@ -73,6 +73,8 @@
 } thread_join_type_t;
 
-#define X_WIRED		(1<<0)
-#define X_STOLEN	(1<<1)
+/* Thread flags */
+#define THREAD_FLAG_WIRED	(1<<0)	/**< Thread cannot be migrated to another CPU. */
+#define THREAD_FLAG_STOLEN	(1<<1)	/**< Thread was migrated to another CPU and has not run yet. */
+#define THREAD_FLAG_USPACE	(1<<2)	/**< Thread executes in userspace. */
 
 #define THREAD_NAME_BUFLEN	20
@@ -128,5 +130,5 @@
 	 * Defined only if thread doesn't run.
 	 * It means that fpu context is in CPU that last time executes this thread.
-	 * This disables migration
+	 * This disables migration.
 	 */
 	int fpu_context_engaged;
@@ -150,5 +152,5 @@
 	thread_arch_t arch;			/**< Architecture-specific data. */
 
-	uint8_t *kstack;				/**< Thread's kernel stack. */
+	uint8_t *kstack;			/**< Thread's kernel stack. */
 };
 
@@ -171,4 +173,10 @@
 extern void thread_create_arch(thread_t *t);
 #endif
+#ifndef thr_constructor_arch
+extern void thr_constructor_arch(thread_t *t);
+#endif
+#ifndef thr_destructor_arch
+extern void thr_destructor_arch(thread_t *t);
+#endif
 
 extern void thread_sleep(uint32_t sec);
Index: kernel/generic/src/main/kinit.c
===================================================================
--- kernel/generic/src/main/kinit.c	(revision f47fd19f9885f194c0b816b7814357789c9565e2)
+++ kernel/generic/src/main/kinit.c	(revision 32fffef00aa36a7913892cc0d7ff6eff828b2e7e)
@@ -101,7 +101,6 @@
 		 * Just a beautification.
 		 */
-		if ((t = thread_create(kmp, NULL, TASK, 0, "kmp"))) {
+		if ((t = thread_create(kmp, NULL, TASK, THREAD_FLAG_WIRED, "kmp"))) {
 			spinlock_lock(&t->lock);
-			t->flags |= X_WIRED;
 			t->cpu = &cpus[0];
 			spinlock_unlock(&t->lock);
@@ -127,7 +126,6 @@
 		for (i = 0; i < config.cpu_count; i++) {
 
-			if ((t = thread_create(kcpulb, NULL, TASK, 0, "kcpulb"))) {
+			if ((t = thread_create(kcpulb, NULL, TASK, THREAD_FLAG_WIRED, "kcpulb"))) {
 				spinlock_lock(&t->lock);			
-				t->flags |= X_WIRED;
 				t->cpu = &cpus[i];
 				spinlock_unlock(&t->lock);
Index: kernel/generic/src/mm/frame.c
===================================================================
--- kernel/generic/src/mm/frame.c	(revision f47fd19f9885f194c0b816b7814357789c9565e2)
+++ kernel/generic/src/mm/frame.c	(revision 32fffef00aa36a7913892cc0d7ff6eff828b2e7e)
@@ -929,5 +929,4 @@
  * @param order  Allocate exactly 2^order frames.
  * @param flags  Flags for host zone selection and address processing.
- * @param status Allocation status (FRAME_OK on success), unused if NULL.
  * @param pzone  Preferred zone
  *
@@ -988,5 +987,5 @@
 /** Free a frame.
  *
- * Find respective frame structure for supplied PFN.
+ * Find respective frame structure for supplied physical frame address.
  * Decrement frame reference count.
  * If it drops to zero, move the frame structure to free list.
Index: kernel/generic/src/proc/scheduler.c
===================================================================
--- kernel/generic/src/proc/scheduler.c	(revision f47fd19f9885f194c0b816b7814357789c9565e2)
+++ kernel/generic/src/proc/scheduler.c	(revision 32fffef00aa36a7913892cc0d7ff6eff828b2e7e)
@@ -142,6 +142,5 @@
 			spinlock_unlock(&THREAD->lock);
 			spinlock_unlock(&CPU->lock);
-			THREAD->saved_fpu_context = slab_alloc(fpu_context_slab,
-							       0);
+			THREAD->saved_fpu_context = slab_alloc(fpu_context_slab, 0);
 			/* We may have switched CPUs during slab_alloc */
 			goto restart; 
@@ -236,7 +235,8 @@
 
 		/*
-		 * Clear the X_STOLEN flag so that t can be migrated when load balancing needs emerge.
+		 * Clear the THREAD_FLAG_STOLEN flag so that t can be migrated
+		 * when load balancing needs emerge.
 		 */
-		t->flags &= ~X_STOLEN;
+		t->flags &= ~THREAD_FLAG_STOLEN;
 		spinlock_unlock(&t->lock);
 
@@ -350,5 +350,6 @@
 	 */
 	context_save(&CPU->saved_context);
-	context_set(&CPU->saved_context, FADDR(scheduler_separated_stack), (uintptr_t) CPU->stack, CPU_STACK_SIZE);
+	context_set(&CPU->saved_context, FADDR(scheduler_separated_stack),
+		(uintptr_t) CPU->stack, CPU_STACK_SIZE);
 	context_restore(&CPU->saved_context);
 	/* not reached */
@@ -484,5 +485,6 @@
 
 #ifdef SCHEDULER_VERBOSE
-	printf("cpu%d: tid %d (priority=%d,ticks=%lld,nrdy=%ld)\n", CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, atomic_get(&CPU->nrdy));
+	printf("cpu%d: tid %d (priority=%d,ticks=%lld,nrdy=%ld)\n",
+		CPU->id, THREAD->tid, THREAD->priority, THREAD->ticks, atomic_get(&CPU->nrdy));
 #endif	
 
@@ -557,5 +559,5 @@
 			/*
 			 * Not interested in ourselves.
-			 * Doesn't require interrupt disabling for kcpulb is X_WIRED.
+			 * Doesn't require interrupt disabling for kcpulb has THREAD_FLAG_WIRED.
 			 */
 			if (CPU == cpu)
@@ -578,10 +580,12 @@
 				t = list_get_instance(l, thread_t, rq_link);
 				/*
-				 * We don't want to steal CPU-wired threads neither threads already stolen.
-				 * The latter prevents threads from migrating between CPU's without ever being run.
-				 * We don't want to steal threads whose FPU context is still in CPU.
+				 * We don't want to steal CPU-wired threads neither threads already
+				 * stolen. The latter prevents threads from migrating between CPU's
+				 * without ever being run. We don't want to steal threads whose FPU
+				 * context is still in CPU.
 				 */
 				spinlock_lock(&t->lock);
-				if ( (!(t->flags & (X_WIRED | X_STOLEN))) && (!(t->fpu_context_engaged)) ) {
+				if ((!(t->flags & (THREAD_FLAG_WIRED | THREAD_FLAG_STOLEN))) &&
+					(!(t->fpu_context_engaged)) ) {
 					/*
 					 * Remove t from r.
@@ -609,7 +613,9 @@
 				spinlock_lock(&t->lock);
 #ifdef KCPULB_VERBOSE
-				printf("kcpulb%d: TID %d -> cpu%d, nrdy=%ld, avg=%nd\n", CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy), atomic_get(&nrdy) / config.cpu_active);
+				printf("kcpulb%d: TID %d -> cpu%d, nrdy=%ld, avg=%nd\n",
+					CPU->id, t->tid, CPU->id, atomic_get(&CPU->nrdy),
+					atomic_get(&nrdy) / config.cpu_active);
 #endif
-				t->flags |= X_STOLEN;
+				t->flags |= THREAD_FLAG_STOLEN;
 				t->state = Entering;
 				spinlock_unlock(&t->lock);
Index: kernel/generic/src/proc/thread.c
===================================================================
--- kernel/generic/src/proc/thread.c	(revision f47fd19f9885f194c0b816b7814357789c9565e2)
+++ kernel/generic/src/proc/thread.c	(revision 32fffef00aa36a7913892cc0d7ff6eff828b2e7e)
@@ -130,4 +130,7 @@
 	link_initialize(&t->wq_link);
 	link_initialize(&t->th_link);
+
+	/* call the architecture-specific part of the constructor */
+	thr_constructor_arch(t);
 	
 #ifdef ARCH_HAS_FPU
@@ -157,4 +160,7 @@
 {
 	thread_t *t = (thread_t *) obj;
+
+	/* call the architecture-specific part of the destructor */
+	thr_destructor_arch(t);
 
 	frame_free(KA2PA(t->kstack));
@@ -211,5 +217,5 @@
 	
 	cpu = CPU;
-	if (t->flags & X_WIRED) {
+	if (t->flags & THREAD_FLAG_WIRED) {
 		cpu = t->cpu;
 	}
@@ -296,6 +302,4 @@
 	if (!t)
 		return NULL;
-
-	thread_create_arch(t);
 	
 	/* Not needed, but good for debugging */
@@ -324,5 +328,5 @@
 	t->priority = -1;		/* start in rq[0] */
 	t->cpu = NULL;
-	t->flags = 0;
+	t->flags = flags;
 	t->state = Entering;
 	t->call_me = NULL;
@@ -348,4 +352,6 @@
 	t->fpu_context_exists = 0;
 	t->fpu_context_engaged = 0;
+
+	thread_create_arch(t);		/* might depend on previous initialization */
 	
 	/*
@@ -590,5 +596,5 @@
 	}
 
-	if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) {
+	if ((t = thread_create(uinit, kernel_uarg, TASK, THREAD_FLAG_USPACE, namebuf))) {
 		tid = t->tid;
 		thread_ready(t);
