Index: kernel/generic/include/arch.h
===================================================================
--- kernel/generic/include/arch.h	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/include/arch.h	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -39,4 +39,5 @@
 #include <proc/thread.h>
 #include <proc/task.h>
+#include <mm/as.h>
 
 #define DEFAULT_CONTEXT		0
Index: kernel/generic/include/proc/task.h
===================================================================
--- kernel/generic/include/proc/task.h	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/include/proc/task.h	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -55,4 +55,5 @@
 #include <udebug/udebug.h>
 #include <ipc/kbox.h>
+#include <mm/as.h>
 
 #define TASK_NAME_BUFLEN	20
Index: kernel/generic/include/proc/thread.h
===================================================================
--- kernel/generic/include/proc/thread.h	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/include/proc/thread.h	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -225,34 +225,33 @@
 
 extern void thread_init(void);
-extern thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
-    int flags, char *name, bool uncounted);
-extern void thread_attach(thread_t *t, task_t *task);
-extern void thread_ready(thread_t *t);
+extern thread_t *thread_create(void (*)(void *), void *, task_t *, int, char *,
+    bool);
+extern void thread_attach(thread_t *, task_t *);
+extern void thread_ready(thread_t *);
 extern void thread_exit(void) __attribute__((noreturn));
 
 #ifndef thread_create_arch
-extern void thread_create_arch(thread_t *t);
+extern void thread_create_arch(thread_t *);
 #endif
 #ifndef thr_constructor_arch
-extern void thr_constructor_arch(thread_t *t);
+extern void thr_constructor_arch(thread_t *);
 #endif
 #ifndef thr_destructor_arch
-extern void thr_destructor_arch(thread_t *t);
-#endif
-
-extern void thread_sleep(uint32_t sec);
-extern void thread_usleep(uint32_t usec);
+extern void thr_destructor_arch(thread_t *);
+#endif
+
+extern void thread_sleep(uint32_t);
+extern void thread_usleep(uint32_t);
 
 #define thread_join(t) \
 	thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
-extern int thread_join_timeout(thread_t *t, uint32_t usec, int flags);
-extern void thread_detach(thread_t *t);
-
-extern void thread_register_call_me(void (* call_me)(void *),
-    void *call_me_with);
+extern int thread_join_timeout(thread_t *, uint32_t, int);
+extern void thread_detach(thread_t *);
+
+extern void thread_register_call_me(void (*)(void *), void *);
 extern void thread_print_list(void);
-extern void thread_destroy(thread_t *t);
+extern void thread_destroy(thread_t *);
 extern void thread_update_accounting(void);
-extern bool thread_exists(thread_t *t);
+extern bool thread_exists(thread_t *);
 
 /** Fpu context slab cache. */
@@ -260,8 +259,9 @@
 
 /* Thread syscall prototypes. */
-extern unative_t sys_thread_create(uspace_arg_t *uspace_uarg,
-    char *uspace_name, size_t name_len, thread_id_t *uspace_thread_id);
-extern unative_t sys_thread_exit(int uspace_status);
-extern unative_t sys_thread_get_id(thread_id_t *uspace_thread_id);
+extern unative_t sys_thread_create(uspace_arg_t *, char *, size_t,
+    thread_id_t *);
+extern unative_t sys_thread_exit(int);
+extern unative_t sys_thread_get_id(thread_id_t *);
+extern unative_t sys_thread_usleep(uint32_t);
 
 #endif
Index: kernel/generic/include/string.h
===================================================================
--- kernel/generic/include/string.h	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/include/string.h	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -87,5 +87,5 @@
 extern void str_cpy(char *dest, size_t size, const char *src);
 extern void str_ncpy(char *dest, size_t size, const char *src, size_t n);
-extern void wstr_nstr(char *dst, const wchar_t *src, size_t size);
+extern void wstr_to_str(char *dest, size_t size, const wchar_t *src);
 
 extern char *str_chr(const char *str, wchar_t ch);
Index: kernel/generic/include/synch/futex.h
===================================================================
--- kernel/generic/include/synch/futex.h	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/include/synch/futex.h	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -38,6 +38,4 @@
 #include <arch/types.h>
 #include <synch/waitq.h>
-#include <genarch/mm/page_ht.h>
-#include <genarch/mm/page_pt.h>
 
 /** Kernel-side futex structure. */
@@ -54,7 +52,6 @@
 
 extern void futex_init(void);
-extern unative_t sys_futex_sleep_timeout(uintptr_t uaddr, uint32_t usec,
-    int flags);
-extern unative_t sys_futex_wakeup(uintptr_t uaddr);
+extern unative_t sys_futex_sleep(uintptr_t);
+extern unative_t sys_futex_wakeup(uintptr_t);
 
 extern void futex_cleanup(void);
Index: kernel/generic/include/syscall/syscall.h
===================================================================
--- kernel/generic/include/syscall/syscall.h	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/include/syscall/syscall.h	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -43,4 +43,5 @@
 	SYS_THREAD_EXIT,
 	SYS_THREAD_GET_ID,
+	SYS_THREAD_USLEEP,
 	
 	SYS_TASK_GET_ID,
Index: kernel/generic/src/console/kconsole.c
===================================================================
--- kernel/generic/src/console/kconsole.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/console/kconsole.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -289,5 +289,5 @@
 			
 			char tmp[STR_BOUNDS(MAX_CMDLINE)];
-			wstr_nstr(tmp, current + beg, position - beg + 1);
+			wstr_to_str(tmp, position - beg + 1, current + beg);
 			
 			int found;
@@ -665,5 +665,5 @@
 		
 		char cmdline[STR_BOUNDS(MAX_CMDLINE)];
-		wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE));
+		wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
 		
 		if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
Index: kernel/generic/src/lib/string.c
===================================================================
--- kernel/generic/src/lib/string.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/lib/string.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -537,5 +537,5 @@
  * null-terminated and containing only complete characters.
  *
- * @param dst   Destination buffer.
+ * @param dest   Destination buffer.
  * @param count Size of the destination buffer (must be > 0).
  * @param src   Source string.
@@ -571,5 +571,5 @@
  * have to be null-terminated.
  *
- * @param dst   Destination buffer.
+ * @param dest   Destination buffer.
  * @param count Size of the destination buffer (must be > 0).
  * @param src   Source string.
@@ -596,36 +596,32 @@
 }
 
-/** Copy NULL-terminated wide string to string
- *
- * Copy source wide string @a src to destination buffer @a dst.
- * No more than @a size bytes are written. NULL-terminator is always
- * written after the last succesfully copied character (i.e. if the
- * destination buffer is has at least 1 byte, it will be always
- * NULL-terminated).
- *
- * @param src   Source wide string.
- * @param dst   Destination buffer.
- * @param count Size of the destination buffer.
- *
- */
-void wstr_nstr(char *dst, const wchar_t *src, size_t size)
-{
-	/* No space for the NULL-terminator in the buffer */
-	if (size == 0)
-		return;
-	
+/** Convert wide string to string.
+ *
+ * Convert wide string @a src to string. The output is written to the buffer
+ * specified by @a dest and @a size. @a size must be non-zero and the string
+ * written will always be well-formed.
+ *
+ * @param dest	Destination buffer.
+ * @param size	Size of the destination buffer.
+ * @param src	Source wide string.
+ */
+void wstr_to_str(char *dest, size_t size, const wchar_t *src)
+{
 	wchar_t ch;
-	size_t src_idx = 0;
-	size_t dst_off = 0;
+	size_t src_idx;
+	size_t dest_off;
+
+	/* There must be space for a null terminator in the buffer. */
+	ASSERT(size > 0);
+
+	src_idx = 0;
+	dest_off = 0;
 	
 	while ((ch = src[src_idx++]) != 0) {
-		if (chr_encode(ch, dst, &dst_off, size) != EOK)
+		if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
 			break;
 	}
-	
-	if (dst_off >= size)
-		dst[size - 1] = 0;
-	else
-		dst[dst_off] = 0;
+
+	dest[dest_off] = '\0';
 }
 
Index: kernel/generic/src/mm/backend_phys.c
===================================================================
--- kernel/generic/src/mm/backend_phys.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/mm/backend_phys.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -40,4 +40,5 @@
 #include <arch/types.h>
 #include <mm/as.h>
+#include <mm/page.h>
 #include <mm/frame.h>
 #include <mm/slab.h>
Index: kernel/generic/src/proc/task.c
===================================================================
--- kernel/generic/src/proc/task.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/proc/task.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -54,4 +54,5 @@
 #include <func.h>
 #include <string.h>
+#include <memstr.h>
 #include <syscall/copy.h>
 #include <macros.h>
Index: kernel/generic/src/proc/thread.c
===================================================================
--- kernel/generic/src/proc/thread.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/proc/thread.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -501,5 +501,12 @@
 void thread_sleep(uint32_t sec)
 {
-	thread_usleep(sec * 1000000);
+	/* Sleep in 1000 second steps to support
+	   full argument range */
+	while (sec > 0) {
+		uint32_t period = (sec > 1000) ? 1000 : sec;
+	
+		thread_usleep(period * 1000000);
+		sec -= period;
+	}
 }
 
@@ -575,7 +582,7 @@
 {
 	waitq_t wq;
-				  
+	
 	waitq_initialize(&wq);
-
+	
 	(void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
 }
@@ -812,4 +819,11 @@
 }
 
+/** Syscall wrapper for sleeping. */
+unative_t sys_thread_usleep(uint32_t usec)
+{
+	thread_usleep(usec);
+	return 0;
+}
+
 /** @}
  */
Index: kernel/generic/src/synch/futex.c
===================================================================
--- kernel/generic/src/synch/futex.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/synch/futex.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -90,5 +90,5 @@
 /** Initialize kernel futex structure.
  *
- * @param futex Kernel futex structure.
+ * @param futex		Kernel futex structure.
  */
 void futex_initialize(futex_t *futex)
@@ -102,13 +102,11 @@
 /** Sleep in futex wait queue.
  *
- * @param uaddr Userspace address of the futex counter.
- * @param usec If non-zero, number of microseconds this thread is willing to
- *     sleep.
- * @param flags Select mode of operation.
- *
- * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See
- *     synch.h. If there is no physical mapping for uaddr ENOENT is returned.
- */
-unative_t sys_futex_sleep_timeout(uintptr_t uaddr, uint32_t usec, int flags)
+ * @param uaddr		Userspace address of the futex counter.
+ *
+ * @return		If there is no physical mapping for uaddr ENOENT is
+ *			returned. Otherwise returns a wait result as defined in
+ *			synch.h.
+ */
+unative_t sys_futex_sleep(uintptr_t uaddr)
 {
 	futex_t *futex;
@@ -140,7 +138,5 @@
 	udebug_stoppable_begin();
 #endif
-	rc = waitq_sleep_timeout(&futex->wq, usec, flags |
-	    SYNCH_FLAGS_INTERRUPTIBLE);
-
+	rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE); 
 #ifdef CONFIG_UDEBUG
 	udebug_stoppable_end();
@@ -151,7 +147,7 @@
 /** Wakeup one thread waiting in futex wait queue.
  *
- * @param uaddr Userspace address of the futex counter.
- *
- * @return ENOENT if there is no physical mapping for uaddr.
+ * @param uaddr		Userspace address of the futex counter.
+ *
+ * @return		ENOENT if there is no physical mapping for uaddr.
  */
 unative_t sys_futex_wakeup(uintptr_t uaddr)
@@ -190,7 +186,7 @@
  * If the structure does not exist already, a new one is created.
  *
- * @param paddr Physical address of the userspace futex counter.
- *
- * @return Address of the kernel futex structure.
+ * @param paddr		Physical address of the userspace futex counter.
+ *
+ * @return		Address of the kernel futex structure.
  */
 futex_t *futex_find(uintptr_t paddr)
@@ -284,8 +280,8 @@
 /** Compute hash index into futex hash table.
  *
- * @param key Address where the key (i.e. physical address of futex counter) is
- *     stored.
- *
- * @return Index into futex hash table.
+ * @param key		Address where the key (i.e. physical address of futex
+ *			counter) is stored.
+ *
+ * @return		Index into futex hash table.
  */
 size_t futex_ht_hash(unative_t *key)
@@ -296,8 +292,8 @@
 /** Compare futex hash table item with a key.
  *
- * @param key Address where the key (i.e. physical address of futex counter) is
- *     stored.
- *
- * @return True if the item matches the key. False otherwise.
+ * @param key		Address where the key (i.e. physical address of futex
+ *			counter) is stored.
+ *
+ * @return		True if the item matches the key. False otherwise.
  */
 bool futex_ht_compare(unative_t *key, size_t keys, link_t *item)
@@ -313,5 +309,5 @@
 /** Callback for removal items from futex hash table.
  *
- * @param item Item removed from the hash table.
+ * @param item		Item removed from the hash table.
  */
 void futex_ht_remove_callback(link_t *item)
Index: kernel/generic/src/syscall/syscall.c
===================================================================
--- kernel/generic/src/syscall/syscall.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/syscall/syscall.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -62,13 +62,9 @@
 
 #ifdef CONFIG_UDEBUG
-	bool debug;
-
 	/*
 	 * Early check for undebugged tasks. We do not lock anything as this
-	 * test need not be precise in either way.
+	 * test need not be precise in either direction.
 	 */
-	debug = THREAD->udebug.active;
-	
-	if (debug) {
+	if (THREAD->udebug.active) {
 		udebug_syscall_event(a1, a2, a3, a4, a5, a6, id, 0, false);
 	}
@@ -87,5 +83,5 @@
 	
 #ifdef CONFIG_UDEBUG
-	if (debug) {
+	if (THREAD->udebug.active) {
 		udebug_syscall_event(a1, a2, a3, a4, a5, a6, id, rc, true);
 	
@@ -111,4 +107,5 @@
 	(syshandler_t) sys_thread_exit,
 	(syshandler_t) sys_thread_get_id,
+	(syshandler_t) sys_thread_usleep,
 	
 	(syshandler_t) sys_task_get_id,
@@ -117,5 +114,5 @@
 	
 	/* Synchronization related syscalls. */
-	(syshandler_t) sys_futex_sleep_timeout,
+	(syshandler_t) sys_futex_sleep,
 	(syshandler_t) sys_futex_wakeup,
 	(syshandler_t) sys_smc_coherence,
Index: kernel/generic/src/udebug/udebug_ops.c
===================================================================
--- kernel/generic/src/udebug/udebug_ops.c	(revision ba50a34216214cc680c5b1c6995600cc6e1828e8)
+++ kernel/generic/src/udebug/udebug_ops.c	(revision 387416b0bf7b246ca6a9df2aea472d75776a5b48)
@@ -50,4 +50,5 @@
 #include <udebug/udebug.h>
 #include <udebug/udebug_ops.h>
+#include <memstr.h>
 
 /**
