Index: Makefile
===================================================================
--- Makefile	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ Makefile	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -30,5 +30,8 @@
 #
 
-.PHONY: all config config_default distclean clean cscope
+CSCOPE = cscope
+STANSE = stanse
+
+.PHONY: all config config_default distclean clean cscope stanse
 
 all: Makefile.config config.h config.defs
@@ -36,4 +39,12 @@
 	$(MAKE) -C uspace
 	$(MAKE) -C boot
+
+stanse: Makefile.config config.h config.defs
+	$(MAKE) -C kernel clean
+	$(MAKE) -C kernel EXTRA_TOOL=stanse
+	$(STANSE) --checker ReachabilityChecker --jobfile kernel/kernel.job
+
+cscope:
+	find kernel boot uspace -regex '^.*\.[chsS]$$' | xargs $(CSCOPE) -b -k -u -f$(CSCOPE).out
 
 Makefile.config: config_default
@@ -50,13 +61,8 @@
 
 distclean: clean
-	rm -f Makefile.config config.h config.defs tools/*.pyc
+	rm -f $(CSCOPE).out Makefile.config config.h config.defs tools/*.pyc
 
 clean:
-	-$(MAKE) -C kernel clean
-	-$(MAKE) -C uspace clean
-	-$(MAKE) -C boot clean
-
-cscope:
-	find kernel boot uspace -regex '^.*\.[chsS]$$' -print > srclist
-	rm -f cscope.out
-	cscope -bi srclist
+	$(MAKE) -C kernel clean
+	$(MAKE) -C uspace clean
+	$(MAKE) -C boot clean
Index: boot/Makefile.common
===================================================================
--- boot/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ boot/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,80 @@
+#
+# Copyright (c) 2009 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.
+#
+
+TMP = distroot
+INITRD = initrd
+
+INIT_TASKS = \
+	$(USPACEDIR)/srv/ns/ns \
+	$(USPACEDIR)/srv/loader/loader \
+	$(USPACEDIR)/app/init/init \
+	$(USPACEDIR)/srv/devmap/devmap \
+	$(USPACEDIR)/srv/bd/rd/rd \
+	$(USPACEDIR)/srv/vfs/vfs
+ifeq ($(RDFMT),tmpfs)
+	INIT_TASKS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
+endif
+ifeq ($(RDFMT),fat)
+	INIT_TASKS += $(USPACEDIR)/srv/fs/fat/fat
+endif
+
+RD_SRVS = \
+	$(USPACEDIR)/srv/fb/fb \
+	$(USPACEDIR)/srv/kbd/kbd \
+	$(USPACEDIR)/srv/console/console \
+	$(USPACEDIR)/srv/clip/clip \
+	$(USPACEDIR)/srv/fs/devfs/devfs \
+	$(USPACEDIR)/srv/fs/tmpfs/tmpfs \
+	$(USPACEDIR)/srv/fs/fat/fat \
+	$(USPACEDIR)/srv/bd/file_bd/file_bd \
+	$(USPACEDIR)/srv/part/mbr_part/mbr_part
+
+RD_APPS = \
+	$(USPACEDIR)/app/edit/edit \
+	$(USPACEDIR)/app/getterm/getterm \
+	$(USPACEDIR)/app/redir/redir \
+	$(USPACEDIR)/app/tetris/tetris \
+	$(USPACEDIR)/app/tester/tester \
+	$(USPACEDIR)/app/trace/trace \
+	$(USPACEDIR)/app/klog/klog \
+	$(USPACEDIR)/app/bdsh/bdsh
+
+COMPONENTS = \
+	$(KERNELDIR)/kernel.bin \
+	$(USPACEDIR)/srv/ns/ns \
+	$(USPACEDIR)/app/init/init \
+	$(USPACEDIR)/srv/loader/loader \
+	$(USPACEDIR)/srv/devmap/devmap \
+	$(USPACEDIR)/srv/bd/rd/rd \
+	$(USPACEDIR)/srv/vfs/vfs
+ifeq ($(RDFMT),tmpfs)
+	COMPONENTS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
+endif
+ifeq ($(RDFMT),fat)
+	COMPONENTS += $(USPACEDIR)/srv/fs/fat/fat
+endif
Index: boot/arch/amd64/Makefile.inc
===================================================================
--- boot/arch/amd64/Makefile.inc	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/amd64/Makefile.inc	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -27,53 +27,31 @@
 #
 
-TMP = distroot
+include Makefile.common
 
-INIT_TASKS = \
-	$(USPACEDIR)/srv/ns/ns \
-	$(USPACEDIR)/srv/loader/loader \
-	$(USPACEDIR)/app/init/init \
-	$(USPACEDIR)/srv/devmap/devmap \
-	$(USPACEDIR)/srv/bd/rd/rd \
-	$(USPACEDIR)/srv/vfs/vfs
-ifeq ($(RDFMT),tmpfs)
-	INIT_TASKS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
-endif
-ifeq ($(RDFMT),fat)
-	INIT_TASKS += $(USPACEDIR)/srv/fs/fat/fat
-endif
+RD_SRVS += \
+	$(USPACEDIR)/srv/pci/pci \
+	$(USPACEDIR)/srv/bd/ata_bd/ata_bd
 
-RD_SRVS = \
-	$(USPACEDIR)/srv/pci/pci \
-	$(USPACEDIR)/srv/fb/fb \
-	$(USPACEDIR)/srv/kbd/kbd \
-	$(USPACEDIR)/srv/console/console \
-	$(USPACEDIR)/srv/fs/devfs/devfs \
-	$(USPACEDIR)/srv/fs/tmpfs/tmpfs \
-	$(USPACEDIR)/srv/fs/fat/fat \
-	$(USPACEDIR)/srv/bd/ata_bd/ata_bd \
-	$(USPACEDIR)/srv/bd/file_bd/file_bd \
-	$(USPACEDIR)/srv/part/mbr_part/mbr_part
-
-RD_APPS = \
-	$(USPACEDIR)/app/edit/edit \
-	$(USPACEDIR)/app/getvc/getvc \
-	$(USPACEDIR)/app/redir/redir \
-	$(USPACEDIR)/app/tetris/tetris \
-	$(USPACEDIR)/app/tester/tester \
-	$(USPACEDIR)/app/trace/trace \
-	$(USPACEDIR)/app/klog/klog \
-	$(USPACEDIR)/app/bdsh/bdsh
+MODULES := $(notdir $(COMPONENTS))
 
 build: $(BASE)/image.iso
 
-$(BASE)/image.iso: arch/$(BARCH)/grub/stage2_eltorito arch/$(BARCH)/grub/menu.lst $(KERNELDIR)/kernel.bin $(INIT_TASKS) $(RD_SRVS) $(RD_APPS)
+$(BASE)/image.iso: arch/$(BARCH)/grub/stage2_eltorito $(KERNELDIR)/kernel.bin $(INIT_TASKS) $(RD_SRVS) $(RD_APPS)
 	mkdir -p $(TMP)/boot/grub
 	cp arch/$(BARCH)/grub/stage2_eltorito $(TMP)/boot/grub/
-ifneq ($(RDFMT),tmpfs)
-	cat arch/$(BARCH)/grub/menu.lst | grep -v "tmpfs" > $(TMP)/boot/grub/menu.lst
-endif
-ifneq ($(RDFMT),fat)
-	cat arch/$(BARCH)/grub/menu.lst | grep -v "fat" > $(TMP)/boot/grub/menu.lst
-endif
+	
+	echo "default 0" > $(TMP)/boot/grub/menu.lst
+	echo "timeout 10" >> $(TMP)/boot/grub/menu.lst
+	echo "" >> $(TMP)/boot/grub/menu.lst
+	echo "title=HelenOS" >> $(TMP)/boot/grub/menu.lst
+	echo "	root (cd)" >> $(TMP)/boot/grub/menu.lst
+	for module in $(MODULES) $(INITRD).img ; do \
+		if [ $$module = kernel.bin ] ; then \
+			echo "	kernel /boot/$$module" >> $(TMP)/boot/grub/menu.lst ; \
+		else \
+			echo "	module /boot/$$module" >> $(TMP)/boot/grub/menu.lst ; \
+		fi \
+	done
+	
 	cp $(KERNELDIR)/kernel.bin $(TMP)/boot/
 	for task in $(INIT_TASKS) ; do \
@@ -86,12 +64,13 @@
 		cp $$file $(USPACEDIR)/dist/app/ ; \
 	done
+	
 ifeq ($(RDFMT),tmpfs)
-	$(BASE)/tools/mktmpfs.py $(USPACEDIR)/dist/ $(TMP)/boot/initrd.fs
+	$(BASE)/tools/mktmpfs.py $(USPACEDIR)/dist/ $(TMP)/boot/$(INITRD).fs
 endif
 ifeq ($(RDFMT),fat)
-	$(BASE)/tools/mkfat.py $(USPACEDIR)/dist/ $(TMP)/boot/initrd.fs
+	$(BASE)/tools/mkfat.py $(USPACEDIR)/dist/ $(TMP)/boot/$(INITRD).fs
 endif
-	$(BASE)/tools/mkhord.py 4096 $(TMP)/boot/initrd.fs $(TMP)/boot/initrd.img
-	rm $(TMP)/boot/initrd.fs
+	$(BASE)/tools/mkhord.py 4096 $(TMP)/boot/$(INITRD).fs $(TMP)/boot/$(INITRD).img
+	rm $(TMP)/boot/$(INITRD).fs
 	mkisofs -J -r -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o $@ $(TMP)/
 
Index: ot/arch/amd64/grub/menu.lst
===================================================================
--- boot/arch/amd64/grub/menu.lst	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,15 +1,0 @@
-default 0
-timeout 10
-
-title=HelenOS
-	root (cd)
-	kernel /boot/kernel.bin
-	module /boot/ns
-	module /boot/init
-	module /boot/devmap
-	module /boot/rd
-	module /boot/vfs
-	module /boot/tmpfs
-	module /boot/fat
-	module /boot/loader
-	module /boot/initrd.img
Index: boot/arch/arm32/loader/Makefile.build
===================================================================
--- boot/arch/arm32/loader/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/arm32/loader/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -51,19 +51,4 @@
 	../../../genarch/division.c
 
-COMPONENTS = \
-	$(KERNELDIR)/kernel.bin \
-	$(USPACEDIR)/srv/ns/ns \
-	$(USPACEDIR)/srv/loader/loader \
-	$(USPACEDIR)/app/init/init \
-	$(USPACEDIR)/srv/devmap/devmap \
-	$(USPACEDIR)/srv/bd/rd/rd \
-	$(USPACEDIR)/srv/vfs/vfs
-ifeq ($(RDFMT),tmpfs)
-	COMPONENTS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
-endif
-ifeq ($(RDFMT),fat)
-	COMPONENTS += $(USPACEDIR)/srv/fs/fat/fat
-endif
-
 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
 COMPONENT_OBJECTS := $(addsuffix .co,$(basename $(notdir $(COMPONENTS)))) $(INITRD).co
Index: boot/arch/arm32/loader/Makefile.common
===================================================================
--- boot/arch/arm32/loader/Makefile.common	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/arm32/loader/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -31,4 +31,6 @@
 #
 
+include ../../../Makefile.common
+
 DEPEND = Makefile.depend
 DEPEND_PREV = $(DEPEND).prev
@@ -37,27 +39,7 @@
 LINK = _link.ld
 COMPS = _components
-INITRD = initrd
 
 KERNELDIR = ../../../../kernel
 USPACEDIR = ../../../../uspace
 
-RD_SRVS = \
-	$(USPACEDIR)/srv/fb/fb \
-	$(USPACEDIR)/srv/kbd/kbd \
-	$(USPACEDIR)/srv/console/console \
-	$(USPACEDIR)/srv/fs/devfs/devfs \
-	$(USPACEDIR)/srv/fs/tmpfs/tmpfs \
-	$(USPACEDIR)/srv/fs/fat/fat \
-	$(USPACEDIR)/srv/bd/file_bd/file_bd \
-	$(USPACEDIR)/srv/part/mbr_part/mbr_part \
-	$(USPACEDIR)/srv/bd/gxe_bd/gxe_bd
-
-RD_APPS = \
-	$(USPACEDIR)/app/edit/edit \
-	$(USPACEDIR)/app/getvc/getvc \
-	$(USPACEDIR)/app/redir/redir \
-	$(USPACEDIR)/app/tetris/tetris \
-	$(USPACEDIR)/app/tester/tester \
-	$(USPACEDIR)/app/trace/trace \
-	$(USPACEDIR)/app/klog/klog \
-	$(USPACEDIR)/app/bdsh/bdsh
+RD_SRVS += $(USPACEDIR)/srv/bd/gxe_bd/gxe_bd
Index: boot/arch/ia64/loader/Makefile.build
===================================================================
--- boot/arch/ia64/loader/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/ia64/loader/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -49,19 +49,4 @@
 	boot.S
 
-COMPONENTS = \
-	$(KERNELDIR)/kernel.bin \
-	$(USPACEDIR)/srv/ns/ns \
-	$(USPACEDIR)/srv/loader/loader \
-	$(USPACEDIR)/app/init/init \
-	$(USPACEDIR)/srv/devmap/devmap \
-	$(USPACEDIR)/srv/bd/rd/rd \
-	$(USPACEDIR)/srv/vfs/vfs
-ifeq ($(RDFMT),tmpfs)
-	COMPONENTS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
-endif
-ifeq ($(RDFMT),fat)
-	COMPONENTS += $(USPACEDIR)/srv/fs/fat/fat
-endif
-
 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
 COMPONENT_OBJECTS := $(addsuffix .co,$(basename $(notdir $(COMPONENTS)))) $(INITRD).co
Index: boot/arch/ia64/loader/Makefile.common
===================================================================
--- boot/arch/ia64/loader/Makefile.common	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/ia64/loader/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -31,4 +31,6 @@
 #
 
+include ../../../Makefile.common
+
 DEPEND = Makefile.depend
 DEPEND_PREV = $(DEPEND).prev
@@ -38,26 +40,5 @@
 LINK = _link.ld
 COMPS = _components
-INITRD = initrd
 
 KERNELDIR = ../../../../kernel
 USPACEDIR = ../../../../uspace
-
-RD_SRVS = \
-	$(USPACEDIR)/srv/fb/fb \
-	$(USPACEDIR)/srv/kbd/kbd \
-	$(USPACEDIR)/srv/console/console \
-	$(USPACEDIR)/srv/fs/devfs/devfs \
-	$(USPACEDIR)/srv/fs/tmpfs/tmpfs \
-	$(USPACEDIR)/srv/fs/fat/fat \
-	$(USPACEDIR)/srv/bd/file_bd/file_bd \
-	$(USPACEDIR)/srv/part/mbr_part/mbr_part
-
-RD_APPS = \
-	$(USPACEDIR)/app/edit/edit \
-	$(USPACEDIR)/app/getvc/getvc \
-	$(USPACEDIR)/app/redir/redir \
-	$(USPACEDIR)/app/tetris/tetris \
-	$(USPACEDIR)/app/tester/tester \
-	$(USPACEDIR)/app/trace/trace \
-	$(USPACEDIR)/app/klog/klog \
-	$(USPACEDIR)/app/bdsh/bdsh
Index: boot/arch/mips32/loader/Makefile.build
===================================================================
--- boot/arch/mips32/loader/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/mips32/loader/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -49,19 +49,4 @@
 	boot.S
 
-COMPONENTS = \
-	$(KERNELDIR)/kernel.bin \
-	$(USPACEDIR)/srv/ns/ns \
-	$(USPACEDIR)/srv/loader/loader \
-	$(USPACEDIR)/app/init/init \
-	$(USPACEDIR)/srv/devmap/devmap \
-	$(USPACEDIR)/srv/bd/rd/rd \
-	$(USPACEDIR)/srv/vfs/vfs
-ifeq ($(RDFMT),tmpfs)
-	COMPONENTS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
-endif
-ifeq ($(RDFMT),fat)
-	COMPONENTS += $(USPACEDIR)/srv/fs/fat/fat
-endif
-
 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
 COMPONENT_OBJECTS := $(addsuffix .co,$(basename $(notdir $(COMPONENTS)))) $(INITRD).co
Index: boot/arch/mips32/loader/Makefile.common
===================================================================
--- boot/arch/mips32/loader/Makefile.common	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/mips32/loader/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -31,4 +31,6 @@
 #
 
+include ../../../Makefile.common
+
 DEPEND = Makefile.depend
 DEPEND_PREV = $(DEPEND).prev
@@ -38,27 +40,7 @@
 LINK = _link.ld
 COMPS = _components
-INITRD = initrd
 
 KERNELDIR = ../../../../kernel
 USPACEDIR = ../../../../uspace
 
-RD_SRVS = \
-	$(USPACEDIR)/srv/fb/fb \
-	$(USPACEDIR)/srv/kbd/kbd \
-	$(USPACEDIR)/srv/console/console \
-	$(USPACEDIR)/srv/fs/devfs/devfs \
-	$(USPACEDIR)/srv/fs/tmpfs/tmpfs \
-	$(USPACEDIR)/srv/fs/fat/fat \
-	$(USPACEDIR)/srv/bd/file_bd/file_bd \
-	$(USPACEDIR)/srv/part/mbr_part/mbr_part \
-	$(USPACEDIR)/srv/bd/gxe_bd/gxe_bd
-
-RD_APPS = \
-	$(USPACEDIR)/app/edit/edit \
-	$(USPACEDIR)/app/getvc/getvc \
-	$(USPACEDIR)/app/redir/redir \
-	$(USPACEDIR)/app/tetris/tetris \
-	$(USPACEDIR)/app/tester/tester \
-	$(USPACEDIR)/app/trace/trace \
-	$(USPACEDIR)/app/klog/klog \
-	$(USPACEDIR)/app/bdsh/bdsh
+RD_SRVS += $(USPACEDIR)/srv/bd/gxe_bd/gxe_bd
Index: boot/arch/ppc32/Makefile.inc
===================================================================
--- boot/arch/ppc32/Makefile.inc	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/ppc32/Makefile.inc	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -27,5 +27,5 @@
 #
 
-TMP = distroot
+include Makefile.common
 
 build: $(BASE)/image.iso
Index: boot/arch/ppc32/loader/Makefile.build
===================================================================
--- boot/arch/ppc32/loader/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/ppc32/loader/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -52,19 +52,4 @@
 	boot.S
 
-COMPONENTS = \
-	$(KERNELDIR)/kernel.bin \
-	$(USPACEDIR)/srv/ns/ns \
-	$(USPACEDIR)/app/init/init \
-	$(USPACEDIR)/srv/loader/loader \
-	$(USPACEDIR)/srv/devmap/devmap \
-	$(USPACEDIR)/srv/bd/rd/rd \
-	$(USPACEDIR)/srv/vfs/vfs
-ifeq ($(RDFMT),tmpfs)
-	COMPONENTS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
-endif
-ifeq ($(RDFMT),fat)
-	COMPONENTS += $(USPACEDIR)/srv/fs/fat/fat
-endif
-
 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
 COMPONENT_OBJECTS := $(addsuffix .co,$(basename $(notdir $(COMPONENTS)))) $(INITRD).co
Index: boot/arch/ppc32/loader/Makefile.common
===================================================================
--- boot/arch/ppc32/loader/Makefile.common	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/ppc32/loader/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -31,4 +31,6 @@
 #
 
+include ../../../Makefile.common
+
 DEPEND = Makefile.depend
 DEPEND_PREV = $(DEPEND).prev
@@ -37,26 +39,5 @@
 LINK = _link.ld
 COMPS = _components
-INITRD = initrd
 
 KERNELDIR = ../../../../kernel
 USPACEDIR = ../../../../uspace
-
-RD_SRVS = \
-	$(USPACEDIR)/srv/fb/fb \
-	$(USPACEDIR)/srv/kbd/kbd \
-	$(USPACEDIR)/srv/console/console \
-	$(USPACEDIR)/srv/fs/devfs/devfs \
-	$(USPACEDIR)/srv/fs/tmpfs/tmpfs \
-	$(USPACEDIR)/srv/fs/fat/fat \
-	$(USPACEDIR)/srv/bd/file_bd/file_bd \
-	$(USPACEDIR)/srv/part/mbr_part/mbr_part
-
-RD_APPS = \
-	$(USPACEDIR)/app/edit/edit \
-	$(USPACEDIR)/app/getvc/getvc \
-	$(USPACEDIR)/app/redir/redir \
-	$(USPACEDIR)/app/tetris/tetris \
-	$(USPACEDIR)/app/tester/tester \
-	$(USPACEDIR)/app/trace/trace \
-	$(USPACEDIR)/app/klog/klog \
-	$(USPACEDIR)/app/bdsh/bdsh
Index: boot/arch/sparc64/Makefile.inc
===================================================================
--- boot/arch/sparc64/Makefile.inc	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/sparc64/Makefile.inc	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -27,5 +27,5 @@
 #
 
-TMP = distroot
+include Makefile.common
 
 ifeq ($(CONFIG_AOUT_ISOFS_B),y)
Index: boot/arch/sparc64/loader/Makefile.build
===================================================================
--- boot/arch/sparc64/loader/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/sparc64/loader/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -52,19 +52,4 @@
 	boot.S
 
-COMPONENTS = \
-	$(KERNELDIR)/kernel.bin \
-	$(USPACEDIR)/srv/ns/ns \
-	$(USPACEDIR)/app/init/init \
-	$(USPACEDIR)/srv/loader/loader \
-	$(USPACEDIR)/srv/devmap/devmap \
-	$(USPACEDIR)/srv/bd/rd/rd \
-	$(USPACEDIR)/srv/vfs/vfs
-ifeq ($(RDFMT),tmpfs)
-	COMPONENTS += $(USPACEDIR)/srv/fs/tmpfs/tmpfs
-endif
-ifeq ($(RDFMT),fat)
-	COMPONENTS += $(USPACEDIR)/srv/fs/fat/fat
-endif
-
 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
 
Index: boot/arch/sparc64/loader/Makefile.common
===================================================================
--- boot/arch/sparc64/loader/Makefile.common	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ boot/arch/sparc64/loader/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -31,4 +31,6 @@
 #
 
+include ../../../Makefile.common
+
 DEPEND = Makefile.depend
 DEPEND_PREV = $(DEPEND).prev
@@ -37,26 +39,7 @@
 LINK = _link.ld
 COMPS = _components
-INITRD = initrd
 
 KERNELDIR = ../../../../kernel
 USPACEDIR = ../../../../uspace
-
-RD_SRVS = \
-	$(USPACEDIR)/srv/fb/fb \
-	$(USPACEDIR)/srv/kbd/kbd \
-	$(USPACEDIR)/srv/console/console \
-	$(USPACEDIR)/srv/fs/devfs/devfs \
-	$(USPACEDIR)/srv/fs/tmpfs/tmpfs \
-	$(USPACEDIR)/srv/bd/file_bd/file_bd \
-	$(USPACEDIR)/srv/part/mbr_part/mbr_part
-
-RD_APPS = \
-	$(USPACEDIR)/app/edit/edit \
-	$(USPACEDIR)/app/getvc/getvc \
-	$(USPACEDIR)/app/redir/redir \
-	$(USPACEDIR)/app/tetris/tetris \
-	$(USPACEDIR)/app/trace/trace \
-	$(USPACEDIR)/app/klog/klog \
-	$(USPACEDIR)/app/bdsh/bdsh
 
 RD_SRVS_GENERIC = \
Index: contrib/bazaar/mbprotect/__init__.py
===================================================================
--- contrib/bazaar/mbprotect/__init__.py	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ contrib/bazaar/mbprotect/__init__.py	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -52,10 +52,17 @@
 	return
 
-    # Look for old tip in new main branch.
+    # First permitted case is appending changesets to main branch.Look for
+    # old tip in new main branch.
     for revision_id in repo.iter_reverse_revision_history(params.new_revid):
 	if revision_id == params.old_revid:
 	    return	# Found old tip
 
-    # Old tip was not found. Reject the change.
+    # Another permitted case is backing out changesets. Look for new tip
+    # in old branch.
+    for revision_id in repo.iter_reverse_revision_history(params.old_revid):
+	if revision_id == params.new_revid:
+	    return	# Found new tip
+
+    # Trying to do something else. Reject the change.
     raise TipChangeRejected('Bad tip. Read http://trac.helenos.org/trac.fcgi/' +
 	'wiki/BazaarWorkflow')
Index: contrib/stanse/ThreadChecker.xml
===================================================================
--- contrib/stanse/ThreadChecker.xml	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ contrib/stanse/ThreadChecker.xml	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+
+ Copyright (c) 2009 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.
+
+-->
+
+<threadChecker>
+	<analyseType type="local" />
+	<patterns>
+		<pattern name="lockFunction">
+			<nested>
+				<functionCall>
+					<id>_mutex_lock_timeout</id>
+					<var name="mtx" />
+				</functionCall>
+			</nested>
+		</pattern>
+		<pattern name="unlockFunction">
+			<nested>
+				<functionCall>
+					<id>mutex_unlock</id>
+					<var name="mtx" />
+				</functionCall>
+			</nested>
+		</pattern>
+	</patterns>
+</threadChecker>
Index: kernel/Makefile
===================================================================
--- kernel/Makefile	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/Makefile	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -33,7 +33,8 @@
 all: ../version ../Makefile.config ../config.h ../config.defs
 	-[ -f $(DEPEND) ] && mv -f $(DEPEND) $(DEPEND_PREV)
-	$(MAKE) -f Makefile.build
+	$(MAKE) -f Makefile.build EXTRA_TOOL=$(EXTRA_TOOL)
 
 clean:
-	rm -f $(DEPEND) $(DEPEND_PREV) $(RAW) $(BIN) $(MAP) $(MAP_PREV) $(DISASM) $(DUMP) $(REAL_MAP).* $(ARCH_INCLUDE) $(GENARCH_INCLUDE) arch/*/_link.ld
+	rm -f $(DEPEND) $(DEPEND_PREV) $(RAW) $(BIN) $(MAP) $(JOB) $(MAP_PREV) $(DISASM) $(DUMP) $(REAL_MAP).* $(ARCH_INCLUDE) $(GENARCH_INCLUDE) arch/*/_link.ld
 	find generic/src/ arch/*/src/ genarch/src/ test/ -name '*.o' -follow -exec rm \{\} \;
+	find generic/src/ arch/*/src/ genarch/src/ test/ -name '*.o.preproc' -follow -exec rm \{\} \;
Index: kernel/Makefile.build
===================================================================
--- kernel/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -91,5 +91,5 @@
 ## Cross-platform assembly to start a symtab.data section
 #
-SYMTAB_SECTION=".section symtab.data, \"a\", $(ATSIGN)progbits;"
+SYMTAB_SECTION = ".section symtab.data, \"a\", $(ATSIGN)progbits;"
 
 ## Simple detection for the type of the host system
@@ -110,5 +110,5 @@
 ifeq ($(COMPILER),gcc_native)
 	CC = gcc
-	GCC = $(CC)
+	GCC = gcc
 	AS = $(BINUTILS_PREFIX)as
 	LD = $(BINUTILS_PREFIX)ld
@@ -168,4 +168,5 @@
 	DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
 endif
+
 
 ## Generic kernel sources
@@ -384,4 +385,7 @@
 test/fpu/%.o: test/fpu/%.c $(DEPEND)
 	$(CC) $(DEFS) $(CFLAGS) $(EXTRA_FLAGS) -c $< -o $@
+ifeq ($(EXTRA_TOOL),stanse)
+	../tools/jobfile.py $(JOB) $< $@ $(DEFS) $(CFLAGS) $(EXTRA_FLAGS)
+endif
 
 #
@@ -390,4 +394,7 @@
 %.o: %.c $(DEPEND)
 	$(CC) $(DEFS) $(CFLAGS) $(EXTRA_FLAGS) $(FPU_NO_CFLAGS) -c $< -o $@
+ifeq ($(EXTRA_TOOL),stanse)
+	../tools/jobfile.py $(JOB) $< $@ $(DEFS) $(CFLAGS) $(EXTRA_FLAGS) $(FPU_NO_CFLAGS)
+endif
 
 $(REAL_MAP).o: $(REAL_MAP).bin
Index: kernel/Makefile.common
===================================================================
--- kernel/Makefile.common	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,4 +36,5 @@
 BIN = kernel.bin
 MAP = kernel.map
+JOB = kernel.job
 MAP_PREV = $(MAP).prev
 DISASM = kernel.disasm
Index: kernel/arch/amd64/include/mm/page.h
===================================================================
--- kernel/arch/amd64/include/mm/page.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/amd64/include/mm/page.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -177,5 +177,23 @@
 #define PFERR_CODE_ID		(1 << 4)
 
-static inline int get_pt_flags(pte_t *pt, size_t i)
+/** Page Table Entry. */
+typedef struct {
+	unsigned present : 1;
+	unsigned writeable : 1;
+	unsigned uaccessible : 1;
+	unsigned page_write_through : 1;
+	unsigned page_cache_disable : 1;
+	unsigned accessed : 1;
+	unsigned dirty : 1;
+	unsigned unused: 1;
+	unsigned global : 1;
+	unsigned soft_valid : 1;		/**< Valid content even if present bit is cleared. */
+	unsigned avl : 2;
+	unsigned addr_12_31 : 30;
+	unsigned addr_32_51 : 21;
+	unsigned no_execute : 1;
+} __attribute__ ((packed)) pte_t;
+
+static inline unsigned int get_pt_flags(pte_t *pt, size_t i)
 {
 	pte_t *p = &pt[i];
Index: kernel/arch/amd64/include/types.h
===================================================================
--- kernel/arch/amd64/include/types.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/amd64/include/types.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -82,22 +82,4 @@
 #define PRIxn "llx"
 
-/** Page Table Entry. */
-typedef struct {
-	unsigned present : 1;
-	unsigned writeable : 1;
-	unsigned uaccessible : 1;
-	unsigned page_write_through : 1;
-	unsigned page_cache_disable : 1;
-	unsigned accessed : 1;
-	unsigned dirty : 1;
-	unsigned unused: 1;
-	unsigned global : 1;
-	unsigned soft_valid : 1;		/**< Valid content even if present bit is cleared. */
-	unsigned avl : 2;
-	unsigned addr_12_31 : 30;
-	unsigned addr_32_51 : 21;
-	unsigned no_execute : 1;
-} __attribute__ ((packed)) pte_t;
-
 #endif
 
Index: kernel/arch/amd64/src/amd64.c
===================================================================
--- kernel/arch/amd64/src/amd64.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/amd64/src/amd64.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -67,4 +67,5 @@
 #include <ddi/irq.h>
 #include <sysinfo/sysinfo.h>
+#include <memstr.h>
 
 /** Disable I/O on non-privileged levels
Index: kernel/arch/amd64/src/interrupt.c
===================================================================
--- kernel/arch/amd64/src/interrupt.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/amd64/src/interrupt.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -98,4 +98,11 @@
 }
 
+static void de_fault(int n, istate_t *istate)
+{
+	fault_if_from_uspace(istate, "Divide error.");
+	decode_istate(n, istate);
+	panic("Divide error.");
+}
+
 /** General Protection Fault. */
 static void gp_fault(int n, istate_t *istate)
@@ -200,4 +207,5 @@
 	}
 	
+	exc_register(0, "de_fault", (iroutine) de_fault);
 	exc_register(7, "nm_fault", (iroutine) nm_fault);
 	exc_register(12, "ss_fault", (iroutine) ss_fault);
Index: kernel/arch/arm32/Makefile.inc
===================================================================
--- kernel/arch/arm32/Makefile.inc	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/Makefile.inc	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -61,5 +61,6 @@
 	arch/$(KARCH)/src/mm/page.c \
 	arch/$(KARCH)/src/mm/tlb.c \
-	arch/$(KARCH)/src/mm/page_fault.c
+	arch/$(KARCH)/src/mm/page_fault.c \
+	arch/$(KARCH)/src/ras.c
 
 ifeq ($(MACHINE),testarm)
Index: kernel/arch/arm32/include/atomic.h
===================================================================
--- kernel/arch/arm32/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -37,4 +37,6 @@
 #define KERN_arm32_ATOMIC_H_
 
+#include <arch/asm.h>
+
 /** Atomic addition.
  *
@@ -47,19 +49,14 @@
 static inline long atomic_add(atomic_t *val, int i)
 {
-	int ret;
-	volatile long *mem = &(val->count);
-	
-	asm volatile (
-		"1:\n"
-			"ldr r2, [%[mem]]\n"
-			"add r3, r2, %[i]\n"
-			"str r3, %[ret]\n"
-			"swp r3, r3, [%[mem]]\n"
-			"cmp r3, r2\n"
-			"bne 1b\n"
-		: [ret] "=m" (ret)
-		: [mem] "r" (mem), [i] "r" (i)
-		: "r3", "r2"
-	);
+	long ret;
+
+	/*
+	 * This implementation is for UP pre-ARMv6 systems where we do not have
+	 * the LDREX and STREX instructions.
+	 */
+	ipl_t ipl = interrupts_disable();
+	val->count += i;
+	ret = val->count;
+	interrupts_restore(ipl);
 	
 	return ret;
Index: kernel/arch/arm32/include/mm/as.h
===================================================================
--- kernel/arch/arm32/include/mm/as.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/include/mm/as.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -54,5 +54,4 @@
 #define as_destructor_arch(as)			(as != as)
 #define as_create_arch(as, flags)		(as != as)
-#define as_install_arch(as)
 #define as_deinstall_arch(as)
 #define as_invalidate_translation_cache(as, page, cnt)
Index: kernel/arch/arm32/include/mm/page.h
===================================================================
--- kernel/arch/arm32/include/mm/page.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/include/mm/page.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -75,5 +75,5 @@
 /* Get PTE address accessors for each level. */
 #define GET_PTL1_ADDRESS_ARCH(ptl0, i) \
-	((pte_t *) ((((pte_level0_t *)(ptl0))[(i)]).coarse_table_addr << 10))
+	((pte_t *) ((((pte_t *)(ptl0))[(i)].l0).coarse_table_addr << 10))
 #define GET_PTL2_ADDRESS_ARCH(ptl1, i) \
 	(ptl1)
@@ -81,19 +81,19 @@
 	(ptl2)
 #define GET_FRAME_ADDRESS_ARCH(ptl3, i) \
-	((uintptr_t) ((((pte_level1_t *)(ptl3))[(i)]).frame_base_addr << 12))
+	((uintptr_t) ((((pte_t *)(ptl3))[(i)].l1).frame_base_addr << 12))
 
 /* Set PTE address accessors for each level. */
 #define SET_PTL0_ADDRESS_ARCH(ptl0) \
-	(set_ptl0_addr((pte_level0_t *) (ptl0)))
+	(set_ptl0_addr((pte_t *) (ptl0)))
 #define SET_PTL1_ADDRESS_ARCH(ptl0, i, a) \
-	(((pte_level0_t *) (ptl0))[(i)].coarse_table_addr = (a) >> 10)
+	(((pte_t *) (ptl0))[(i)].l0.coarse_table_addr = (a) >> 10)
 #define SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
 #define SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
 #define SET_FRAME_ADDRESS_ARCH(ptl3, i, a) \
-	(((pte_level1_t *) (ptl3))[(i)].frame_base_addr = (a) >> 12)
+	(((pte_t *) (ptl3))[(i)].l1.frame_base_addr = (a) >> 12)
 
 /* Get PTE flags accessors for each level. */
 #define GET_PTL1_FLAGS_ARCH(ptl0, i) \
-	get_pt_level0_flags((pte_level0_t *) (ptl0), (size_t) (i))
+	get_pt_level0_flags((pte_t *) (ptl0), (size_t) (i))
 #define GET_PTL2_FLAGS_ARCH(ptl1, i) \
 	PAGE_PRESENT
@@ -101,13 +101,13 @@
 	PAGE_PRESENT
 #define GET_FRAME_FLAGS_ARCH(ptl3, i) \
-	get_pt_level1_flags((pte_level1_t *) (ptl3), (size_t) (i))
+	get_pt_level1_flags((pte_t *) (ptl3), (size_t) (i))
 
 /* Set PTE flags accessors for each level. */
 #define SET_PTL1_FLAGS_ARCH(ptl0, i, x) \
-	set_pt_level0_flags((pte_level0_t *) (ptl0), (size_t) (i), (x))
+	set_pt_level0_flags((pte_t *) (ptl0), (size_t) (i), (x))
 #define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
 #define SET_PTL3_FLAGS_ARCH(ptl2, i, x)
 #define SET_FRAME_FLAGS_ARCH(ptl3, i, x) \
-	set_pt_level1_flags((pte_level1_t *) (ptl3), (size_t) (i), (x))
+	set_pt_level1_flags((pte_t *) (ptl3), (size_t) (i), (x))
 
 /* Macros for querying the last-level PTE entries. */
@@ -115,10 +115,9 @@
 	(*((uint32_t *) (pte)) != 0)
 #define PTE_PRESENT_ARCH(pte) \
-	(((pte_level0_t *) (pte))->descriptor_type != 0)
+	(((pte_t *) (pte))->l0.descriptor_type != 0)
 #define PTE_GET_FRAME_ARCH(pte) \
-	(((pte_level1_t *) (pte))->frame_base_addr << FRAME_WIDTH)
+	(((pte_t *) (pte))->l1.frame_base_addr << FRAME_WIDTH)
 #define PTE_WRITABLE_ARCH(pte) \
-	(((pte_level1_t *) (pte))->access_permission_0 == \
-	    PTE_AP_USER_RW_KERNEL_RW)
+	(((pte_t *) (pte))->l1.access_permission_0 == PTE_AP_USER_RW_KERNEL_RW)
 #define PTE_EXECUTABLE_ARCH(pte) \
 	1
@@ -159,4 +158,8 @@
 } ATTRIBUTE_PACKED pte_level1_t;
 
+typedef union {
+	pte_level0_t l0;
+	pte_level1_t l1;
+} pte_t;
 
 /* Level 1 page tables access permissions */
@@ -191,5 +194,5 @@
  * @param pt    Pointer to the page table to set.
  */   
-static inline void set_ptl0_addr(pte_level0_t *pt)
+static inline void set_ptl0_addr(pte_t *pt)
 {
 	asm volatile (
@@ -205,7 +208,7 @@
  *  @param i      Index of the entry to return.
  */
-static inline int get_pt_level0_flags(pte_level0_t *pt, size_t i)
-{
-	pte_level0_t *p = &pt[i];
+static inline int get_pt_level0_flags(pte_t *pt, size_t i)
+{
+	pte_level0_t *p = &pt[i].l0;
 	int np = (p->descriptor_type == PTE_DESCRIPTOR_NOT_PRESENT);
 
@@ -220,7 +223,7 @@
  *  @param i      Index of the entry to return.
  */
-static inline int get_pt_level1_flags(pte_level1_t *pt, size_t i)
-{
-	pte_level1_t *p = &pt[i];
+static inline int get_pt_level1_flags(pte_t *pt, size_t i)
+{
+	pte_level1_t *p = &pt[i].l1;
 
 	int dt = p->descriptor_type;
@@ -245,7 +248,7 @@
  *  @param flags  new flags
  */
-static inline void set_pt_level0_flags(pte_level0_t *pt, size_t i, int flags)
-{
-	pte_level0_t *p = &pt[i];
+static inline void set_pt_level0_flags(pte_t *pt, size_t i, int flags)
+{
+	pte_level0_t *p = &pt[i].l0;
 
 	if (flags & PAGE_NOT_PRESENT) {
@@ -273,7 +276,7 @@
  *  @param flags  New flags.
  */  
-static inline void set_pt_level1_flags(pte_level1_t *pt, size_t i, int flags)
-{
-	pte_level1_t *p = &pt[i];
+static inline void set_pt_level1_flags(pte_t *pt, size_t i, int flags)
+{
+	pte_level1_t *p = &pt[i].l1;
 	
 	if (flags & PAGE_NOT_PRESENT) {
Index: kernel/arch/arm32/include/ras.h
===================================================================
--- kernel/arch/arm32/include/ras.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ kernel/arch/arm32/include/ras.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2009 Jakub Jermar 
+ * 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 arm32
+ * @{
+ */
+/** @file
+ *  @brief Declarations related to Restartable Atomic Sequences.
+ */
+
+#ifndef KERN_arm32_RAS_H_
+#define KERN_arm32_RAS_H_
+
+#include <arch/exception.h>
+#include <arch/types.h>
+
+#define RAS_START	0
+#define RAS_END		1
+
+extern uintptr_t *ras_page;
+
+extern void ras_init(void);
+extern void ras_check(int, istate_t *);
+
+#endif
+
+/** @}
+ */
Index: kernel/arch/arm32/include/types.h
===================================================================
--- kernel/arch/arm32/include/types.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/include/types.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -87,13 +87,4 @@
 #define PRIxn "x"	/**< Format for hexadecimal (u)native_t. */
 
-/** Page table entry.
- *
- *  We have different structs for level 0 and level 1 page table entries.
- *  See page.h for definition of pte_level*_t.
- */
-typedef struct {
-	unsigned dummy : 32;
-} pte_t;
-
 #endif
 
Index: kernel/arch/arm32/src/arm32.c
===================================================================
--- kernel/arch/arm32/src/arm32.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/src/arm32.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -48,4 +48,5 @@
 #include <macros.h>
 #include <string.h>
+#include <arch/ras.h>
 
 #ifdef MACHINE_testarm
@@ -88,4 +89,7 @@
 	exception_init();
 	interrupt_init();
+
+	/* Initialize Restartable Atomic Sequences support. */
+	ras_init();
 	
 	machine_output_init();
@@ -136,5 +140,4 @@
 	uint8_t *stck;
 	
-	tlb_invalidate_all();
 	stck = &THREAD->kstack[THREAD_STACK_SIZE - SP_DELTA];
 	supervisor_sp = (uintptr_t) stck;
Index: kernel/arch/arm32/src/exc_handler.S
===================================================================
--- kernel/arch/arm32/src/exc_handler.S	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/src/exc_handler.S	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -148,5 +148,5 @@
 	mov r0, #0
 	mov r1, r13
-	bl exc_dispatch
+	bl ras_check 
 	LOAD_REGS_FROM_STACK
 
@@ -156,5 +156,5 @@
 	mov r0, #5
 	mov r1, r13
-	bl exc_dispatch
+	bl ras_check 
 	LOAD_REGS_FROM_STACK
 
@@ -164,5 +164,5 @@
 	mov r0, #6
 	mov r1, r13
-	bl exc_dispatch
+	bl ras_check 
 	LOAD_REGS_FROM_STACK
 
@@ -171,5 +171,5 @@
 	mov r0, #1
 	mov r1, r13
-	bl exc_dispatch
+	bl ras_check 
 	LOAD_REGS_FROM_STACK
 
@@ -179,5 +179,5 @@
 	mov r0, #3
 	mov r1, r13
-	bl exc_dispatch
+	bl ras_check 
 	LOAD_REGS_FROM_STACK
 
@@ -187,5 +187,5 @@
 	mov r0, #4
 	mov r1, r13
-	bl exc_dispatch
+	bl ras_check 
 	LOAD_REGS_FROM_STACK
 
@@ -195,5 +195,5 @@
 	mov r0, #2
 	mov r1, r13
-	bl exc_dispatch
+	bl ras_check
 	LOAD_REGS_FROM_STACK
 
Index: kernel/arch/arm32/src/mm/as.c
===================================================================
--- kernel/arch/arm32/src/mm/as.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/src/mm/as.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,6 +36,8 @@
 #include <arch/mm/as.h>
 #include <genarch/mm/as_pt.h>
+#include <genarch/mm/page_pt.h>
 #include <genarch/mm/asid_fifo.h>
 #include <mm/as.h>
+#include <mm/tlb.h>
 #include <arch.h>
 
@@ -49,4 +51,9 @@
 }
 
+void as_install_arch(as_t *as)
+{
+	tlb_invalidate_all();
+}
+
 /** @}
  */
Index: kernel/arch/arm32/src/ras.c
===================================================================
--- kernel/arch/arm32/src/ras.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ kernel/arch/arm32/src/ras.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2009 Jakub Jermar 
+ * 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 arm32
+ * @{
+ */
+/** @file
+ *  @brief Kernel part of Restartable Atomic Sequences support.
+ */
+
+#include <arch/ras.h>
+#include <mm/mm.h>
+#include <mm/frame.h>
+#include <mm/page.h>
+#include <mm/tlb.h>
+#include <mm/asid.h>
+#include <interrupt.h>
+#include <arch/exception.h>
+#include <arch.h>
+#include <memstr.h>
+#include <arch/types.h>
+
+uintptr_t *ras_page = NULL;
+
+void ras_init(void)
+{
+	ras_page = frame_alloc(ONE_FRAME, FRAME_KA);
+	memsetb(ras_page, FRAME_SIZE, 0); 
+	ras_page[RAS_START] = 0;
+	ras_page[RAS_END] = 0xffffffff;
+	/*
+	 * Userspace needs to be able to write to this page. The page is 
+	 * cached in TLB as PAGE_KERNEL. Purge it from TLB and map it
+	 * read/write PAGE_USER.
+	 */
+	tlb_invalidate_pages(ASID_KERNEL, (uintptr_t)ras_page, 1);
+	page_table_lock(AS, true);
+	page_mapping_insert(AS, (uintptr_t)ras_page, (uintptr_t)KA2PA(ras_page),
+	    PAGE_READ | PAGE_WRITE | PAGE_USER);
+	page_table_unlock(AS, true);
+}
+
+void ras_check(int n, istate_t *istate)
+{
+	uintptr_t rewrite_pc = istate->pc;
+
+	if (istate_from_uspace(istate)) {
+		if (ras_page[RAS_START]) {
+			if ((ras_page[RAS_START] < istate->pc) &&
+			    (ras_page[RAS_END] > istate->pc)) {
+				rewrite_pc = ras_page[RAS_START];
+			}
+			ras_page[RAS_START] = 0;
+			ras_page[RAS_END] = 0xffffffff;
+		}	
+	}
+
+	exc_dispatch(n, istate);
+
+	istate->pc = rewrite_pc;
+}
+
Index: kernel/arch/arm32/src/userspace.c
===================================================================
--- kernel/arch/arm32/src/userspace.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/arm32/src/userspace.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -35,4 +35,5 @@
 
 #include <userspace.h>
+#include <arch/ras.h>
 
 /** Struct for holding all general purpose registers.
@@ -74,8 +75,11 @@
 	ustate.r1 = 0;
 
+	/* pass the RAS page address in %r2 */
+	ustate.r2 = (uintptr_t) ras_page;
+
 	/* clear other registers */
-	ustate.r2 = ustate.r3  = ustate.r4  = ustate.r5 =
-	    ustate.r6  = ustate.r7  = ustate.r8  = ustate.r9 = ustate.r10 = 
-	    ustate.r11 = ustate.r12 = ustate.lr = 0;
+	ustate.r3  = ustate.r4  = ustate.r5 = ustate.r6 = ustate.r7 =
+	    ustate.r8 = ustate.r9 = ustate.r10 = ustate.r11 = ustate.r12 =
+	    ustate.lr = 0;
 
 	/* set user stack */
Index: kernel/arch/ia32/include/mm/page.h
===================================================================
--- kernel/arch/ia32/include/mm/page.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ia32/include/mm/page.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -146,5 +146,21 @@
 #define PFERR_CODE_RSVD		(1 << 3)	
 
-static inline int get_pt_flags(pte_t *pt, size_t i)
+/** Page Table Entry. */
+typedef struct {
+	unsigned present : 1;
+	unsigned writeable : 1;
+	unsigned uaccessible : 1;
+	unsigned page_write_through : 1;
+	unsigned page_cache_disable : 1;
+	unsigned accessed : 1;
+	unsigned dirty : 1;
+	unsigned pat : 1;
+	unsigned global : 1;
+	unsigned soft_valid : 1;	/**< Valid content even if the present bit is not set. */
+	unsigned avl : 2;
+	unsigned frame_address : 20;
+} __attribute__ ((packed)) pte_t;
+
+static inline unsigned int get_pt_flags(pte_t *pt, size_t i)
 {
 	pte_t *p = &pt[i];
Index: kernel/arch/ia32/include/types.h
===================================================================
--- kernel/arch/ia32/include/types.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ia32/include/types.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -80,20 +80,4 @@
 #define PRIxn "x"	/**< Format for hexadecimal (u)native_t. */
 
-/** Page Table Entry. */
-typedef struct {
-	unsigned present : 1;
-	unsigned writeable : 1;
-	unsigned uaccessible : 1;
-	unsigned page_write_through : 1;
-	unsigned page_cache_disable : 1;
-	unsigned accessed : 1;
-	unsigned dirty : 1;
-	unsigned pat : 1;
-	unsigned global : 1;
-	unsigned soft_valid : 1;	/**< Valid content even if the present bit is not set. */
-	unsigned avl : 2;
-	unsigned frame_address : 20;
-} __attribute__ ((packed)) pte_t;
-
 #endif
 
Index: kernel/arch/ia32/src/ia32.c
===================================================================
--- kernel/arch/ia32/src/ia32.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ia32/src/ia32.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -68,4 +68,5 @@
 #include <sysinfo/sysinfo.h>
 #include <arch/boot/boot.h>
+#include <memstr.h>
 
 #ifdef CONFIG_SMP
Index: kernel/arch/ia32/src/interrupt.c
===================================================================
--- kernel/arch/ia32/src/interrupt.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ia32/src/interrupt.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -98,4 +98,12 @@
 }
 
+static void de_fault(int n, istate_t *istate)
+{
+	fault_if_from_uspace(istate, "Divide error.");
+
+	decode_istate(istate);
+	panic("Divide error.");
+}
+
 /** General Protection Fault. */
 static void gp_fault(int n __attribute__((unused)), istate_t *istate)
@@ -215,4 +223,5 @@
 	}
 	
+	exc_register(0, "de_fault", (iroutine) de_fault);
 	exc_register(7, "nm_fault", (iroutine) nm_fault);
 	exc_register(12, "ss_fault", (iroutine) ss_fault);
Index: kernel/arch/ia64/src/cpu/cpu.c
===================================================================
--- kernel/arch/ia64/src/cpu/cpu.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ia64/src/cpu/cpu.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -37,4 +37,5 @@
 #include <arch/register.h>
 #include <print.h>
+#include <memstr.h>
 
 void cpu_arch_init(void)
Index: kernel/arch/mips32/include/mm/page.h
===================================================================
--- kernel/arch/mips32/include/mm/page.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/mips32/include/mm/page.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -141,5 +141,19 @@
 #include <arch/exception.h>
 
-static inline int get_pt_flags(pte_t *pt, size_t i)
+/** Page Table Entry. */
+typedef struct {
+	unsigned g : 1;			/**< Global bit. */
+	unsigned p : 1;			/**< Present bit. */
+	unsigned d : 1;			/**< Dirty bit. */
+	unsigned cacheable : 1;		/**< Cacheable bit. */
+	unsigned : 1;			/**< Unused. */
+	unsigned soft_valid : 1;	/**< Valid content even if not present. */
+	unsigned pfn : 24;		/**< Physical frame number. */
+	unsigned w : 1;			/**< Page writable bit. */
+	unsigned a : 1;			/**< Accessed bit. */
+} pte_t;
+
+
+static inline unsigned int get_pt_flags(pte_t *pt, size_t i)
 {
 	pte_t *p = &pt[i];
Index: kernel/arch/mips32/include/types.h
===================================================================
--- kernel/arch/mips32/include/types.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/mips32/include/types.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -80,17 +80,4 @@
 #define PRIxn "x"	/**< Format for hexadecimal (u)native_t. */
 
-/** Page Table Entry. */
-typedef struct {
-	unsigned g : 1;			/**< Global bit. */
-	unsigned p : 1;			/**< Present bit. */
-	unsigned d : 1;			/**< Dirty bit. */
-	unsigned cacheable : 1;		/**< Cacheable bit. */
-	unsigned : 1;			/**< Unused. */
-	unsigned soft_valid : 1;	/**< Valid content even if not present. */
-	unsigned pfn : 24;		/**< Physical frame number. */
-	unsigned w : 1;			/**< Page writable bit. */
-	unsigned a : 1;			/**< Accessed bit. */
-} pte_t;
-
 #endif
 
Index: kernel/arch/ppc32/include/mm/page.h
===================================================================
--- kernel/arch/ppc32/include/mm/page.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ppc32/include/mm/page.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -131,5 +131,16 @@
 #include <arch/interrupt.h>
 
-static inline int get_pt_flags(pte_t *pt, size_t i)
+/** Page Table Entry. */
+typedef struct {
+	unsigned present : 1;             /**< Present bit. */
+	unsigned page_write_through : 1;  /**< Write thought caching. */
+	unsigned page_cache_disable : 1;  /**< No caching. */
+	unsigned accessed : 1;            /**< Accessed bit. */
+	unsigned global : 1;              /**< Global bit. */
+	unsigned valid : 1;               /**< Valid content even if not present. */
+	unsigned pfn : 20;                /**< Physical frame number. */
+} pte_t;
+
+static inline unsigned int get_pt_flags(pte_t *pt, size_t i)
 {
 	pte_t *p = &pt[i];
Index: kernel/arch/ppc32/include/types.h
===================================================================
--- kernel/arch/ppc32/include/types.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ppc32/include/types.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -82,15 +82,4 @@
 #define PRIxn "x"
 
-/** Page Table Entry. */
-typedef struct {
-	unsigned present : 1;             /**< Present bit. */
-	unsigned page_write_through : 1;  /**< Write thought caching. */
-	unsigned page_cache_disable : 1;  /**< No caching. */
-	unsigned accessed : 1;            /**< Accessed bit. */
-	unsigned global : 1;              /**< Global bit. */
-	unsigned valid : 1;               /**< Valid content even if not present. */
-	unsigned pfn : 20;                /**< Physical frame number. */
-} pte_t;
-
 #endif
 
Index: kernel/arch/ppc32/src/mm/as.c
===================================================================
--- kernel/arch/ppc32/src/mm/as.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ppc32/src/mm/as.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -35,4 +35,5 @@
 #include <arch/mm/as.h>
 #include <genarch/mm/as_pt.h>
+#include <genarch/mm/page_pt.h>
 #include <genarch/mm/asid_fifo.h>
 #include <arch.h>
Index: kernel/arch/ppc32/src/mm/tlb.c
===================================================================
--- kernel/arch/ppc32/src/mm/tlb.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ppc32/src/mm/tlb.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -38,4 +38,5 @@
 #include <interrupt.h>
 #include <mm/as.h>
+#include <mm/page.h>
 #include <arch.h>
 #include <print.h>
Index: kernel/arch/ppc32/src/ppc32.c
===================================================================
--- kernel/arch/ppc32/src/ppc32.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/ppc32/src/ppc32.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -44,4 +44,5 @@
 #include <genarch/ofw/pci.h>
 #include <userspace.h>
+#include <mm/page.h>
 #include <proc/uarg.h>
 #include <console/console.h>
Index: kernel/arch/sparc64/src/mm/tlb.c
===================================================================
--- kernel/arch/sparc64/src/mm/tlb.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/arch/sparc64/src/mm/tlb.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -37,4 +37,5 @@
 #include <mm/as.h>
 #include <mm/asid.h>
+#include <genarch/mm/page_ht.h>
 #include <arch/mm/frame.h>
 #include <arch/mm/page.h>
Index: kernel/genarch/include/mm/as_pt.h
===================================================================
--- kernel/genarch/include/mm/as_pt.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/genarch/include/mm/as_pt.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,6 +36,5 @@
 #define KERN_AS_PT_H_
 
-#include <mm/mm.h>
-#include <arch/types.h>
+#include <arch/mm/page.h>
 
 #define AS_PAGE_TABLE
Index: kernel/genarch/include/mm/page_pt.h
===================================================================
--- kernel/genarch/include/mm/page_pt.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/genarch/include/mm/page_pt.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -44,7 +44,8 @@
 #define KERN_PAGE_PT_H_
 
-#include <arch/types.h>
 #include <mm/as.h>
 #include <mm/page.h>
+#include <arch/mm/page.h>
+#include <arch/types.h>
 
 /*
Index: kernel/genarch/src/drivers/via-cuda/cuda.c
===================================================================
--- kernel/genarch/src/drivers/via-cuda/cuda.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/genarch/src/drivers/via-cuda/cuda.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -41,4 +41,5 @@
 #include <ddi/device.h>
 #include <synch/spinlock.h>
+#include <memstr.h>
 
 static irq_ownership_t cuda_claim(irq_t *irq);
Index: kernel/genarch/src/fb/fb.c
===================================================================
--- kernel/genarch/src/fb/fb.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/genarch/src/fb/fb.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -41,4 +41,5 @@
 #include <console/console.h>
 #include <sysinfo/sysinfo.h>
+#include <mm/page.h>
 #include <mm/slab.h>
 #include <align.h>
Index: kernel/generic/include/arch.h
===================================================================
--- kernel/generic/include/arch.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/include/arch.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -39,4 +39,5 @@
 #include <proc/thread.h>
 #include <proc/task.h>
+#include <mm/as.h>
 
 #define DEFAULT_CONTEXT		0
Index: kernel/generic/include/ipc/ipc.h
===================================================================
--- kernel/generic/include/ipc/ipc.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/include/ipc/ipc.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -51,14 +51,12 @@
 /** This is answer to a call */
 #define IPC_CALL_ANSWERED	(1 << 0)
-/** This call will not be freed on error */
-#define IPC_CALL_STATIC_ALLOC	(1 << 1)
 /** Answer will not be passed to userspace, will be discarded */
-#define IPC_CALL_DISCARD_ANSWER	(1 << 2)
+#define IPC_CALL_DISCARD_ANSWER (1 << 1)
 /** Call was forwarded */
-#define IPC_CALL_FORWARDED	(1 << 3)
+#define IPC_CALL_FORWARDED	(1 << 2)
 /** Identify connect_me_to answer */
-#define IPC_CALL_CONN_ME_TO	(1 << 4)
+#define IPC_CALL_CONN_ME_TO	(1 << 3)
 /** Interrupt notification */
-#define IPC_CALL_NOTIF		(1 << 5)
+#define IPC_CALL_NOTIF		(1 << 4)
 
 /*
@@ -267,4 +265,7 @@
 	waitq_t wq;
 
+	/** Linkage for the list of task's synchronous answerboxes. */
+	link_t sync_box_link;
+
 	/** Phones connected to this answerbox. */
 	link_t connected_phones;
@@ -316,25 +317,31 @@
 } call_t;
 
+
+extern answerbox_t *ipc_phone_0;
+
+
 extern void ipc_init(void);
-extern call_t * ipc_wait_for_call(answerbox_t *, uint32_t, int);
-extern void ipc_answer(answerbox_t *, call_t *);
+
+extern call_t * ipc_call_alloc(int);
+extern void ipc_call_free(call_t *);
+
 extern int ipc_call(phone_t *, call_t *);
 extern int ipc_call_sync(phone_t *, call_t *);
+extern call_t * ipc_wait_for_call(answerbox_t *, uint32_t, int);
+extern int ipc_forward(call_t *, phone_t *, answerbox_t *, int);
+extern void ipc_answer(answerbox_t *, call_t *);
+
 extern void ipc_phone_init(phone_t *);
 extern void ipc_phone_connect(phone_t *, answerbox_t *);
-extern void ipc_call_free(call_t *);
-extern call_t * ipc_call_alloc(int);
+extern int ipc_phone_hangup(phone_t *);
+
 extern void ipc_answerbox_init(answerbox_t *, struct task *);
-extern void ipc_call_static_init(call_t *);
-extern void task_print_list(void);
-extern int ipc_forward(call_t *, phone_t *, answerbox_t *, int);
+
 extern void ipc_cleanup(void);
-extern int ipc_phone_hangup(phone_t *);
 extern void ipc_backsend_err(phone_t *, call_t *, unative_t);
-extern void ipc_print_task(task_id_t);
 extern void ipc_answerbox_slam_phones(answerbox_t *, bool);
 extern void ipc_cleanup_call_list(link_t *);
 
-extern answerbox_t *ipc_phone_0;
+extern void ipc_print_task(task_id_t);
 
 #endif
Index: kernel/generic/include/proc/task.h
===================================================================
--- kernel/generic/include/proc/task.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/include/proc/task.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -55,4 +55,5 @@
 #include <udebug/udebug.h>
 #include <ipc/kbox.h>
+#include <mm/as.h>
 
 #define TASK_NAME_BUFLEN	20
@@ -98,4 +99,6 @@
 	 */
 	atomic_t active_calls;
+	/** List of synchronous answerboxes. */
+	link_t sync_box_head;
 
 #ifdef CONFIG_UDEBUG
@@ -132,4 +135,5 @@
 extern int task_kill(task_id_t id);
 extern uint64_t task_get_accounting(task_t *t);
+extern void task_print_list(void);
 
 extern void cap_set(task_t *t, cap_t caps);
Index: kernel/generic/include/proc/thread.h
===================================================================
--- kernel/generic/include/proc/thread.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/include/proc/thread.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/include/string.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/include/synch/futex.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/include/syscall/syscall.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -43,4 +43,5 @@
 	SYS_THREAD_EXIT,
 	SYS_THREAD_GET_ID,
+	SYS_THREAD_USLEEP,
 	
 	SYS_TASK_GET_ID,
Index: kernel/generic/src/console/console.c
===================================================================
--- kernel/generic/src/console/console.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/console/console.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -319,15 +319,15 @@
 	
 	if (size > PAGE_SIZE)
-		return ELIMIT;
+		return (unative_t) ELIMIT;
 	
 	if (size > 0) {
 		data = (char *) malloc(size + 1, 0);
 		if (!data)
-			return ENOMEM;
+			return (unative_t) ENOMEM;
 		
 		rc = copy_from_uspace(data, buf, size);
 		if (rc) {
 			free(data);
-			return rc;
+			return (unative_t) rc;
 		}
 		data[size] = 0;
Index: kernel/generic/src/console/kconsole.c
===================================================================
--- kernel/generic/src/console/kconsole.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/console/kconsole.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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/ipc/ipc.c
===================================================================
--- kernel/generic/src/ipc/ipc.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/ipc/ipc.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -62,4 +62,5 @@
 
 static slab_cache_t *ipc_call_slab;
+static slab_cache_t *ipc_answerbox_slab;
 
 /** Initialize a call structure.
@@ -96,15 +97,4 @@
 }
 
-/** Initialize a statically allocated call structure.
- *
- * @param call		Statically allocated kernel call structure to be
- *			initialized.
- */
-void ipc_call_static_init(call_t *call)
-{
-	_ipc_call_init(call);
-	call->flags |= IPC_CALL_STATIC_ALLOC;
-}
-
 /** Deallocate a call structure.
  *
@@ -113,5 +103,4 @@
 void ipc_call_free(call_t *call)
 {
-	ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
 	/* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
 	if (call->buffer)
@@ -130,4 +119,5 @@
 	spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
 	waitq_initialize(&box->wq);
+	link_initialize(&box->sync_box_link);
 	list_initialize(&box->connected_phones);
 	list_initialize(&box->calls);
@@ -179,15 +169,41 @@
 int ipc_call_sync(phone_t *phone, call_t *request)
 {
-	answerbox_t sync_box; 
-
-	ipc_answerbox_init(&sync_box, TASK);
+	answerbox_t *sync_box; 
+	ipl_t ipl;
+
+	sync_box = slab_alloc(ipc_answerbox_slab, 0);
+	ipc_answerbox_init(sync_box, TASK);
+
+	/*
+	 * Put the answerbox on the TASK's list of synchronous answerboxes so
+	 * that it can be cleaned up if the call is interrupted.
+	 */
+	ipl = interrupts_disable();
+	spinlock_lock(&TASK->lock);
+	list_append(&sync_box->sync_box_link, &TASK->sync_box_head);
+	spinlock_unlock(&TASK->lock);
+	interrupts_restore(ipl);
 
 	/* We will receive data in a special box. */
-	request->callerbox = &sync_box;
+	request->callerbox = sync_box;
 
 	ipc_call(phone, request);
-	if (!ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT,
-	    SYNCH_FLAGS_INTERRUPTIBLE))
+	if (!ipc_wait_for_call(sync_box, SYNCH_NO_TIMEOUT,
+	    SYNCH_FLAGS_INTERRUPTIBLE)) {
+	    	/* The answerbox and the call will be freed by ipc_cleanup(). */
 		return EINTR;
+	}
+
+	/*
+	 * The answer arrived without interruption so we can remove the
+	 * answerbox from the TASK's list of synchronous answerboxes.
+	 */
+	(void) interrupts_disable();
+	spinlock_lock(&TASK->lock);
+	list_remove(&sync_box->sync_box_link);
+	spinlock_unlock(&TASK->lock);
+	interrupts_restore(ipl);
+
+	slab_free(ipc_answerbox_slab, sync_box);
 	return EOK;
 }
@@ -520,4 +536,5 @@
 	int i;
 	call_t *call;
+	ipl_t ipl;
 
 	/* Disconnect all our phones ('ipc_phone_hangup') */
@@ -545,5 +562,19 @@
 	spinlock_unlock(&TASK->answerbox.lock);
 	
-	/* Wait for all async answers to arrive */
+	/* Wait for all answers to interrupted synchronous calls to arrive */
+	ipl = interrupts_disable();
+	while (!list_empty(&TASK->sync_box_head)) {
+		answerbox_t *box = list_get_instance(TASK->sync_box_head.next,
+		    answerbox_t, sync_box_link);
+
+		list_remove(&box->sync_box_link);
+		call = ipc_wait_for_call(box, SYNCH_NO_TIMEOUT,
+		    SYNCH_FLAGS_NONE);
+		ipc_call_free(call);
+		slab_free(ipc_answerbox_slab, box);
+	}
+	interrupts_restore(ipl);
+
+	/* Wait for all answers to asynchronous calls to arrive */
 	while (1) {
 		/* Go through all phones, until all are FREE... */
@@ -552,6 +583,8 @@
 		for (i = 0; i < IPC_MAX_PHONES; i++) {
 			if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
-			    atomic_get(&TASK->phones[i].active_calls) == 0)
+			    atomic_get(&TASK->phones[i].active_calls) == 0) {
 				TASK->phones[i].state = IPC_PHONE_FREE;
+				TASK->phones[i].callee = NULL;
+			}
 			
 			/* Just for sure, we might have had some 
@@ -574,5 +607,4 @@
 		ASSERT((call->flags & IPC_CALL_ANSWERED) ||
 		    (call->flags & IPC_CALL_NOTIF));
-		ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
 		
 		/*
@@ -593,4 +625,6 @@
 	ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
 	    NULL, 0);
+	ipc_answerbox_slab = slab_cache_create("ipc_answerbox",
+	    sizeof(answerbox_t), 0, NULL, NULL, 0);
 }
 
Index: kernel/generic/src/ipc/irq.c
===================================================================
--- kernel/generic/src/ipc/irq.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/ipc/irq.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -418,5 +418,4 @@
 		case CMD_ACCEPT:
 			return IRQ_ACCEPT;
-			break;
 		case CMD_DECLINE:
 		default:
Index: kernel/generic/src/ipc/sysipc.c
===================================================================
--- kernel/generic/src/ipc/sysipc.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/ipc/sysipc.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -61,5 +61,5 @@
 { \
 	if (phoneid > IPC_MAX_PHONES) { \
-		err; \
+		err \
 	} \
 	phone = &TASK->phones[phoneid]; \
@@ -122,5 +122,4 @@
 	case IPC_M_DATA_READ:
 		return 1;
-		break;
 	default:
 		return 0;
@@ -376,5 +375,5 @@
 		phone_t *cloned_phone;
 		GET_CHECK_PHONE(cloned_phone, IPC_GET_ARG1(call->data),
-		    return ENOENT);
+		    return ENOENT;);
 		phones_lock(cloned_phone, phone);
 		if ((cloned_phone->state != IPC_PHONE_CONNECTED) ||
@@ -531,39 +530,42 @@
     unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
 {
-	call_t call;
+	call_t *call;
 	phone_t *phone;
 	int res;
 	int rc;
 	
-	GET_CHECK_PHONE(phone, phoneid, return ENOENT);
-
-	ipc_call_static_init(&call);
-	IPC_SET_METHOD(call.data, method);
-	IPC_SET_ARG1(call.data, arg1);
-	IPC_SET_ARG2(call.data, arg2);
-	IPC_SET_ARG3(call.data, arg3);
+	GET_CHECK_PHONE(phone, phoneid, return ENOENT;);
+
+	call = ipc_call_alloc(0);
+	IPC_SET_METHOD(call->data, method);
+	IPC_SET_ARG1(call->data, arg1);
+	IPC_SET_ARG2(call->data, arg2);
+	IPC_SET_ARG3(call->data, arg3);
 	/*
 	 * To achieve deterministic behavior, zero out arguments that are beyond
 	 * the limits of the fast version.
 	 */
-	IPC_SET_ARG4(call.data, 0);
-	IPC_SET_ARG5(call.data, 0);
-
-	if (!(res = request_preprocess(&call, phone))) {
+	IPC_SET_ARG4(call->data, 0);
+	IPC_SET_ARG5(call->data, 0);
+
+	if (!(res = request_preprocess(call, phone))) {
 #ifdef CONFIG_UDEBUG
 		udebug_stoppable_begin();
 #endif
-		rc = ipc_call_sync(phone, &call);
+		rc = ipc_call_sync(phone, call);
 #ifdef CONFIG_UDEBUG
 		udebug_stoppable_end();
 #endif
-		if (rc != EOK)
+		if (rc != EOK) {
+			/* The call will be freed by ipc_cleanup(). */
 			return rc;
-		process_answer(&call);
+		}
+		process_answer(call);
 
 	} else {
-		IPC_SET_RETVAL(call.data, res);
-	}
-	rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
+		IPC_SET_RETVAL(call->data, res);
+	}
+	rc = STRUCT_TO_USPACE(&data->args, &call->data.args);
+	ipc_call_free(call);
 	if (rc != 0)
 		return rc;
@@ -584,32 +586,38 @@
     ipc_data_t *reply)
 {
-	call_t call;
+	call_t *call;
 	phone_t *phone;
 	int res;
 	int rc;
 
-	ipc_call_static_init(&call);
-	rc = copy_from_uspace(&call.data.args, &question->args,
-	    sizeof(call.data.args));
-	if (rc != 0)
+	GET_CHECK_PHONE(phone, phoneid, return ENOENT;);
+
+	call = ipc_call_alloc(0);
+	rc = copy_from_uspace(&call->data.args, &question->args,
+	    sizeof(call->data.args));
+	if (rc != 0) {
+		ipc_call_free(call);
 		return (unative_t) rc;
-
-	GET_CHECK_PHONE(phone, phoneid, return ENOENT);
-
-	if (!(res = request_preprocess(&call, phone))) {
+	}
+
+
+	if (!(res = request_preprocess(call, phone))) {
 #ifdef CONFIG_UDEBUG
 		udebug_stoppable_begin();
 #endif
-		rc = ipc_call_sync(phone, &call);
+		rc = ipc_call_sync(phone, call);
 #ifdef CONFIG_UDEBUG
 		udebug_stoppable_end();
 #endif
-		if (rc != EOK)
+		if (rc != EOK) {
+			/* The call will be freed by ipc_cleanup(). */
 			return rc;
-		process_answer(&call);
+		}
+		process_answer(call);
 	} else 
-		IPC_SET_RETVAL(call.data, res);
-
-	rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
+		IPC_SET_RETVAL(call->data, res);
+
+	rc = STRUCT_TO_USPACE(&reply->args, &call->data.args);
+	ipc_call_free(call);
 	if (rc != 0)
 		return rc;
@@ -658,5 +666,5 @@
 		return IPC_CALLRET_TEMPORARY;
 
-	GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
+	GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL;);
 
 	call = ipc_call_alloc(0);
@@ -697,5 +705,5 @@
 		return IPC_CALLRET_TEMPORARY;
 
-	GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
+	GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL;);
 
 	call = ipc_call_alloc(0);
@@ -747,5 +755,5 @@
 	call->flags |= IPC_CALL_FORWARDED;
 
-	GET_CHECK_PHONE(phone, phoneid, { 
+	GET_CHECK_PHONE(phone, phoneid, {
 		IPC_SET_RETVAL(call->data, EFORWARD);
 		ipc_answer(&TASK->answerbox, call);
@@ -952,5 +960,5 @@
 	phone_t *phone;
 
-	GET_CHECK_PHONE(phone, phoneid, return ENOENT);
+	GET_CHECK_PHONE(phone, phoneid, return ENOENT;);
 
 	if (ipc_phone_hangup(phone))
@@ -991,6 +999,4 @@
 
 	if (call->flags & IPC_CALL_NOTIF) {
-		ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
-
 		/* Set in_phone_hash to the interrupt counter */
 		call->data.phone = (void *) call->priv;
@@ -1005,6 +1011,4 @@
 	if (call->flags & IPC_CALL_ANSWERED) {
 		process_answer(call);
-
-		ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
 
 		if (call->flags & IPC_CALL_DISCARD_ANSWER) {
Index: kernel/generic/src/lib/elf.c
===================================================================
--- kernel/generic/src/lib/elf.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/lib/elf.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -163,5 +163,4 @@
 	case PT_LOAD:
 		return load_segment(entry, elf, as);
-		break;
 	case PT_DYNAMIC:
 	case PT_INTERP:
@@ -182,5 +181,4 @@
 	default:
 		return EE_UNSUPPORTED;
-		break;
 	}
 	return EE_OK;
Index: kernel/generic/src/lib/string.c
===================================================================
--- kernel/generic/src/lib/string.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/lib/string.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/mm/backend_phys.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/proc/task.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -54,4 +54,5 @@
 #include <func.h>
 #include <string.h>
+#include <memstr.h>
 #include <syscall/copy.h>
 #include <macros.h>
@@ -75,6 +76,9 @@
 static task_id_t task_counter = 0;
 
+static slab_cache_t *task_slab;
+
 /* Forward declarations. */
 static void task_kill_internal(task_t *);
+static int tsk_constructor(void *, int);
 
 /** Initialize kernel tasks support. */
@@ -83,4 +87,6 @@
 	TASK = NULL;
 	avltree_create(&tasks_tree);
+	task_slab = slab_cache_create("task_slab", sizeof(task_t), 0,
+	    tsk_constructor, NULL, 0);
 }
 
@@ -128,4 +134,33 @@
 }
 
+int tsk_constructor(void *obj, int kmflags)
+{
+	task_t *ta = obj;
+	int i;
+
+	atomic_set(&ta->refcount, 0);
+	atomic_set(&ta->lifecount, 0);
+	atomic_set(&ta->active_calls, 0);
+
+	spinlock_initialize(&ta->lock, "task_ta_lock");
+	mutex_initialize(&ta->futexes_lock, MUTEX_PASSIVE);
+
+	list_initialize(&ta->th_head);
+	list_initialize(&ta->sync_box_head);
+
+	ipc_answerbox_init(&ta->answerbox, ta);
+	for (i = 0; i < IPC_MAX_PHONES; i++)
+		ipc_phone_init(&ta->phones[i]);
+
+#ifdef CONFIG_UDEBUG
+	/* Init kbox stuff */
+	ta->kb.thread = NULL;
+	ipc_answerbox_init(&ta->kb.box, ta);
+	mutex_initialize(&ta->kb.cleanup_lock, MUTEX_PASSIVE);
+#endif
+
+	return 0;
+}
+
 /** Create new task with no threads.
  *
@@ -140,21 +175,12 @@
 	ipl_t ipl;
 	task_t *ta;
-	int i;
-	
-	ta = (task_t *) malloc(sizeof(task_t), 0);
-
+	
+	ta = (task_t *) slab_alloc(task_slab, 0);
 	task_create_arch(ta);
-
-	spinlock_initialize(&ta->lock, "task_ta_lock");
-	list_initialize(&ta->th_head);
 	ta->as = as;
-
 	memcpy(ta->name, name, TASK_NAME_BUFLEN);
 	ta->name[TASK_NAME_BUFLEN - 1] = 0;
 
-	atomic_set(&ta->refcount, 0);
-	atomic_set(&ta->lifecount, 0);
 	ta->context = CONTEXT;
-
 	ta->capabilities = 0;
 	ta->cycles = 0;
@@ -165,28 +191,15 @@
 
 	/* Init kbox stuff */
-	ipc_answerbox_init(&ta->kb.box, ta);
-	ta->kb.thread = NULL;
-	mutex_initialize(&ta->kb.cleanup_lock, MUTEX_PASSIVE);
 	ta->kb.finished = false;
 #endif
 
-	ipc_answerbox_init(&ta->answerbox, ta);
-	for (i = 0; i < IPC_MAX_PHONES; i++)
-		ipc_phone_init(&ta->phones[i]);
-	if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
-	    ta->context)))
+	if ((ipc_phone_0) &&
+	    (context_check(ipc_phone_0->task->context, ta->context)))
 		ipc_phone_connect(&ta->phones[0], ipc_phone_0);
-	atomic_set(&ta->active_calls, 0);
-
-	mutex_initialize(&ta->futexes_lock, MUTEX_PASSIVE);
+
 	btree_create(&ta->futexes);
 	
 	ipl = interrupts_disable();
-
-	/*
-	 * Increment address space reference count.
-	 */
 	atomic_inc(&as->refcount);
-
 	spinlock_lock(&tasks_lock);
 	ta->taskid = ++task_counter;
@@ -229,5 +242,5 @@
 		as_destroy(t->as);
 	
-	free(t);
+	slab_free(task_slab, t);
 	TASK = NULL;
 }
Index: kernel/generic/src/proc/thread.c
===================================================================
--- kernel/generic/src/proc/thread.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/proc/thread.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/synch/futex.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/syscall/syscall.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -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 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ kernel/generic/src/udebug/udebug_ops.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -50,4 +50,5 @@
 #include <udebug/udebug.h>
 #include <udebug/udebug_ops.h>
+#include <memstr.h>
 
 /**
Index: tools/config.py
===================================================================
--- tools/config.py	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ tools/config.py	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -227,5 +227,8 @@
 	
 	timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
+	
+	sys.stderr.write("Fetching current revision identifier ... ")
 	version = subprocess.Popen(['bzr', 'version-info', '--custom', '--template={clean}:{revno}:{revision_id}'], stdout = subprocess.PIPE).communicate()[0].split(':')
+	sys.stderr.write("OK\n")
 	
 	if (len(version) == 3):
Index: tools/jobfile.py
===================================================================
--- tools/jobfile.py	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ tools/jobfile.py	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+#
+# Copyright (c) 2009 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.
+#
+"""
+Add a source/object file pair to a Stanse jobfile
+"""
+
+import sys
+import os
+import fcntl
+
+def usage(prname):
+	"Print usage syntax"
+	print prname + " <JOBFILE> <SOURCE> <OBJECT> [OPTIONS ...]"
+
+def main():
+	if (len(sys.argv) < 4):
+		usage(sys.argv[0])
+		return
+	
+	jobfname = sys.argv[1]
+	srcfname = sys.argv[2]
+	objfname = sys.argv[3]
+	cwd = os.getcwd()
+	options = " ".join(sys.argv[4:])
+	
+	jobfile = file(jobfname, "a")
+	fcntl.lockf(jobfile, fcntl.LOCK_EX)
+	jobfile.write("{%s},{%s},{%s},{%s}\n" % (srcfname, objfname, cwd, options))
+	fcntl.lockf(jobfile, fcntl.LOCK_UN)
+	jobfile.close()
+
+if __name__ == '__main__':
+	main()
Index: uspace/Makefile
===================================================================
--- uspace/Makefile	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/Makefile	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -48,4 +48,5 @@
 	srv/devmap \
 	srv/part/mbr_part \
+	srv/clip \
 	app/edit \
 	app/tetris \
@@ -54,5 +55,5 @@
 	app/klog \
 	app/init \
-	app/getvc \
+	app/getterm \
 	app/redir \
 	app/bdsh
Index: uspace/app/bdsh/cmds/modules/ls/ls.c
===================================================================
--- uspace/app/bdsh/cmds/modules/ls/ls.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/bdsh/cmds/modules/ls/ls.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -100,4 +100,6 @@
 	if (s.is_file)
 		printf("%-40s\t%llu\n", name, (long long) s.size);
+	else if (s.is_directory)
+		printf("%-40s\t<dir>\n", name);
 	else
 		printf("%-40s\n", name);
Index: uspace/app/bdsh/input.c
===================================================================
--- uspace/app/bdsh/input.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/bdsh/input.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,6 +36,10 @@
 #include <io/keycode.h>
 #include <io/style.h>
+#include <io/color.h>
 #include <vfs/vfs.h>
+#include <clipboard.h>
+#include <macros.h>
 #include <errno.h>
+#include <assert.h>
 #include <bool.h>
 
@@ -47,5 +51,51 @@
 #include "exec.h"
 
-static void read_line(char *, int);
+#define HISTORY_LEN 10
+
+/** Text input field. */
+typedef struct {
+	/** Buffer holding text currently being edited */
+	wchar_t buffer[INPUT_MAX + 1];
+	/** Screen coordinates of the top-left corner of the text field */
+	int col0, row0;
+	/** Screen dimensions */
+	int con_cols, con_rows;
+	/** Number of characters in @c buffer */
+	int nc;
+	/** Caret position within buffer */
+	int pos;
+	/** Selection mark position within buffer */
+	int sel_start;
+
+	/** History (dynamically allocated strings) */
+	char *history[1 + HISTORY_LEN];
+	/** Number of entries in @c history, not counting [0] */
+	int hnum;
+	/** Current position in history */
+	int hpos;
+	/** Exit flag */
+	bool done;
+} tinput_t;
+
+/** Seek direction */
+typedef enum {
+	seek_backward = -1,
+	seek_forward = 1
+} seek_dir_t;
+
+static tinput_t tinput;
+
+static char *tinput_read(tinput_t *ti);
+static void tinput_insert_string(tinput_t *ti, const char *str);
+static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb);
+static bool tinput_sel_active(tinput_t *ti);
+static void tinput_sel_all(tinput_t *ti);
+static void tinput_sel_delete(tinput_t *ti);
+static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev);
+static void tinput_key_shift(tinput_t *ti, console_event_t *ev);
+static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev);
+static void tinput_key_unmod(tinput_t *ti, console_event_t *ev);
+static void tinput_pre_seek(tinput_t *ti, bool shift_held);
+static void tinput_post_seek(tinput_t *ti, bool shift_held);
 
 /* Tokenizes input from console, sees if the first word is a built-in, if so
@@ -99,52 +149,623 @@
 }
 
-static void read_line(char *buffer, int n)
+static void tinput_display_tail(tinput_t *ti, int start, int pad)
+{
+	static wchar_t dbuf[INPUT_MAX + 1];
+	int sa, sb;
+	int i, p;
+
+	tinput_sel_get_bounds(ti, &sa, &sb);
+
+	console_goto(fphone(stdout), (ti->col0 + start) % ti->con_cols,
+	    ti->row0 + (ti->col0 + start) / ti->con_cols);
+	console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
+
+	p = start;
+	if (p < sa) {
+		memcpy(dbuf, ti->buffer + p, (sa - p) * sizeof(wchar_t));
+		dbuf[sa - p] = '\0';
+		printf("%ls", dbuf);
+		p = sa;
+	}
+
+	if (p < sb) {
+		fflush(stdout);
+		console_set_color(fphone(stdout), COLOR_BLACK, COLOR_RED, 0);
+		memcpy(dbuf, ti->buffer + p,
+		    (sb - p) * sizeof(wchar_t));
+		dbuf[sb - p] = '\0';
+		printf("%ls", dbuf);
+		p = sb;
+	}
+
+	fflush(stdout);
+	console_set_color(fphone(stdout), COLOR_BLACK, COLOR_WHITE, 0);
+
+	if (p < ti->nc) {
+		memcpy(dbuf, ti->buffer + p,
+		    (ti->nc - p) * sizeof(wchar_t));
+		dbuf[ti->nc - p] = '\0';
+		printf("%ls", dbuf);
+	}
+
+	for (i = 0; i < pad; ++i)
+		putchar(' ');
+	fflush(stdout);
+}
+
+static char *tinput_get_str(tinput_t *ti)
+{
+	return wstr_to_astr(ti->buffer);
+}
+
+static void tinput_position_caret(tinput_t *ti)
+{
+	console_goto(fphone(stdout), (ti->col0 + ti->pos) % ti->con_cols,
+	    ti->row0 + (ti->col0 + ti->pos) / ti->con_cols);
+}
+
+/** Update row0 in case the screen could have scrolled. */
+static void tinput_update_origin(tinput_t *ti)
+{
+	int width, rows;
+
+	width = ti->col0 + ti->nc;
+	rows = (width / ti->con_cols) + 1;
+
+	/* Update row0 if the screen scrolled. */
+	if (ti->row0 + rows > ti->con_rows)
+		ti->row0 = ti->con_rows - rows;	
+}
+
+static void tinput_insert_char(tinput_t *ti, wchar_t c)
+{
+	int i;
+	int new_width, new_height;
+
+	if (ti->nc == INPUT_MAX)
+		return;
+
+	new_width = ti->col0 + ti->nc + 1;
+	if (new_width % ti->con_cols == 0) {
+		/* Advancing to new line. */
+		new_height = (new_width / ti->con_cols) + 1;
+		if (new_height >= ti->con_rows)
+			return; /* Disallow text longer than 1 page for now. */
+	}
+
+	for (i = ti->nc; i > ti->pos; --i)
+		ti->buffer[i] = ti->buffer[i - 1];
+
+	ti->buffer[ti->pos] = c;
+	ti->pos += 1;
+	ti->nc += 1;
+	ti->buffer[ti->nc] = '\0';
+	ti->sel_start = ti->pos;
+
+	tinput_display_tail(ti, ti->pos - 1, 0);
+	tinput_update_origin(ti);
+	tinput_position_caret(ti);
+}
+
+static void tinput_insert_string(tinput_t *ti, const char *str)
+{
+	int i;
+	int new_width, new_height;
+	int ilen;
+	wchar_t c;
+	size_t off;
+
+	ilen = min((ssize_t) str_length(str), INPUT_MAX - ti->nc);
+	if (ilen == 0)
+		return;
+
+	new_width = ti->col0 + ti->nc + ilen;
+	new_height = (new_width / ti->con_cols) + 1;
+	if (new_height >= ti->con_rows)
+		return; /* Disallow text longer than 1 page for now. */
+
+	for (i = ti->nc - 1; i >= ti->pos; --i)
+		ti->buffer[i + ilen] = ti->buffer[i];
+
+	off = 0; i = 0;
+	while (i < ilen) {
+		c = str_decode(str, &off, STR_NO_LIMIT);
+		if (c == '\0')
+			break;
+
+		/* Filter out non-printable chars. */
+		if (c < 32)
+			c = 32;
+
+		ti->buffer[ti->pos + i] = c;
+		++i;
+	}
+
+	ti->pos += ilen;
+	ti->nc += ilen;
+	ti->buffer[ti->nc] = '\0';
+	ti->sel_start = ti->pos;
+
+	tinput_display_tail(ti, ti->pos - ilen, 0);
+	tinput_update_origin(ti);
+	tinput_position_caret(ti);
+}
+
+static void tinput_backspace(tinput_t *ti)
+{
+	int i;
+
+	if (tinput_sel_active(ti)) {
+		tinput_sel_delete(ti);
+		return;
+	}
+
+	if (ti->pos == 0)
+		return;
+
+	for (i = ti->pos; i < ti->nc; ++i)
+		ti->buffer[i - 1] = ti->buffer[i];
+	ti->pos -= 1;
+	ti->nc -= 1;
+	ti->buffer[ti->nc] = '\0';
+	ti->sel_start = ti->pos;
+
+	tinput_display_tail(ti, ti->pos, 1);
+	tinput_position_caret(ti);
+}
+
+static void tinput_delete(tinput_t *ti)
+{
+	if (tinput_sel_active(ti)) {
+		tinput_sel_delete(ti);
+		return;
+	}
+
+	if (ti->pos == ti->nc)
+		return;
+
+	ti->pos += 1;
+	ti->sel_start = ti->pos;
+
+	tinput_backspace(ti);
+}
+
+static void tinput_seek_cell(tinput_t *ti, seek_dir_t dir, bool shift_held)
+{
+	tinput_pre_seek(ti, shift_held);
+
+	if (dir == seek_forward) {
+		if (ti->pos < ti->nc)
+			ti->pos += 1;
+	} else {
+		if (ti->pos > 0)
+			ti->pos -= 1;
+	}
+
+	tinput_post_seek(ti, shift_held);
+}
+
+static void tinput_seek_word(tinput_t *ti, seek_dir_t dir, bool shift_held)
+{
+	tinput_pre_seek(ti, shift_held);
+
+	if (dir == seek_forward) {
+		if (ti->pos == ti->nc)
+			return;
+
+		while (1) {
+			ti->pos += 1;
+
+			if (ti->pos == ti->nc)
+				break;
+
+			if (ti->buffer[ti->pos - 1] == ' ' &&
+			    ti->buffer[ti->pos] != ' ')
+				break;
+		}
+	} else {
+		if (ti->pos == 0)
+			return;
+
+		while (1) {
+			ti->pos -= 1;
+
+			if (ti->pos == 0)
+				break;
+
+			if (ti->buffer[ti->pos - 1] == ' ' &&
+			    ti->buffer[ti->pos] != ' ')
+				break;
+		}
+
+	}
+
+	tinput_post_seek(ti, shift_held);
+}
+
+static void tinput_seek_vertical(tinput_t *ti, seek_dir_t dir, bool shift_held)
+{
+	tinput_pre_seek(ti, shift_held);
+
+	if (dir == seek_forward) {
+		if (ti->pos + ti->con_cols <= ti->nc)
+			ti->pos = ti->pos + ti->con_cols;
+	} else {
+		if (ti->pos - ti->con_cols >= 0)
+			ti->pos = ti->pos - ti->con_cols;
+	}
+
+	tinput_post_seek(ti, shift_held);
+}
+
+static void tinput_seek_max(tinput_t *ti, seek_dir_t dir, bool shift_held)
+{
+	tinput_pre_seek(ti, shift_held);
+
+	if (dir == seek_backward)
+		ti->pos = 0;
+	else
+		ti->pos = ti->nc;
+
+	tinput_post_seek(ti, shift_held);
+}
+
+static void tinput_pre_seek(tinput_t *ti, bool shift_held)
+{
+	if (tinput_sel_active(ti) && !shift_held) {
+		/* Unselect and redraw. */
+		ti->sel_start = ti->pos;
+		tinput_display_tail(ti, 0, 0);
+		tinput_position_caret(ti);
+	}
+}
+
+static void tinput_post_seek(tinput_t *ti, bool shift_held)
+{
+	if (shift_held) {
+		/* Selecting text. Need redraw. */
+		tinput_display_tail(ti, 0, 0);
+	} else {
+		/* Shift not held. Keep selection empty. */
+		ti->sel_start = ti->pos;
+	}
+	tinput_position_caret(ti);
+}
+
+static void tinput_history_insert(tinput_t *ti, char *str)
+{
+	int i;
+
+	if (ti->hnum < HISTORY_LEN) {
+		ti->hnum += 1;
+	} else {
+		if (ti->history[HISTORY_LEN] != NULL)
+			free(ti->history[HISTORY_LEN]);
+	}
+
+	for (i = ti->hnum; i > 1; --i)
+		ti->history[i] = ti->history[i - 1];
+
+	ti->history[1] = str_dup(str);
+
+	if (ti->history[0] != NULL) {
+		free(ti->history[0]);
+		ti->history[0] = NULL;
+	}
+}
+
+static void tinput_set_str(tinput_t *ti, char *str)
+{
+	str_to_wstr(ti->buffer, INPUT_MAX, str);
+	ti->nc = wstr_length(ti->buffer);
+	ti->pos = ti->nc;
+	ti->sel_start = ti->pos;
+}
+
+static void tinput_sel_get_bounds(tinput_t *ti, int *sa, int *sb)
+{
+	if (ti->sel_start < ti->pos) {
+		*sa = ti->sel_start;
+		*sb = ti->pos;
+	} else {
+		*sa = ti->pos;
+		*sb = ti->sel_start;
+	}
+}
+
+static bool tinput_sel_active(tinput_t *ti)
+{
+	return ti->sel_start != ti->pos;
+}
+
+static void tinput_sel_all(tinput_t *ti)
+{
+	ti->sel_start = 0;
+	ti->pos = ti->nc;
+	tinput_display_tail(ti, 0, 0);
+	tinput_position_caret(ti);
+}
+
+static void tinput_sel_delete(tinput_t *ti)
+{
+	int sa, sb;
+
+	tinput_sel_get_bounds(ti, &sa, &sb);
+	if (sa == sb)
+		return;
+
+	memmove(ti->buffer + sa, ti->buffer + sb,
+	    (ti->nc - sb) * sizeof(wchar_t));
+	ti->pos = ti->sel_start = sa;
+	ti->nc -= (sb - sa);
+	ti->buffer[ti->nc] = '\0';
+
+	tinput_display_tail(ti, sa, sb - sa);
+	tinput_position_caret(ti);
+}
+
+static void tinput_sel_copy_to_cb(tinput_t *ti)
+{
+	int sa, sb;
+	wchar_t tmp_c;
+	char *str;
+
+	tinput_sel_get_bounds(ti, &sa, &sb);
+
+	if (sb < ti->nc) {
+		tmp_c = ti->buffer[sb];
+		ti->buffer[sb] = '\0';
+	}
+
+	str = wstr_to_astr(ti->buffer + sa);
+
+	if (sb < ti->nc)
+		ti->buffer[sb] = tmp_c;
+
+	if (str == NULL)
+		goto error;
+
+	if (clipboard_put_str(str) != EOK)
+		goto error;
+
+	free(str);
+	return;
+error:
+	return;
+	/* TODO: Give the user some warning. */
+}
+
+static void tinput_paste_from_cb(tinput_t *ti)
+{
+	char *str;
+	int rc;
+
+	rc = clipboard_get_str(&str);
+	if (rc != EOK || str == NULL)
+		return; /* TODO: Give the user some warning. */
+
+	tinput_insert_string(ti, str);
+	free(str);
+}
+
+static void tinput_history_seek(tinput_t *ti, int offs)
+{
+	int pad;
+
+	if (ti->hpos + offs < 0 || ti->hpos + offs > ti->hnum)
+		return;
+
+	if (ti->history[ti->hpos] != NULL) {
+		free(ti->history[ti->hpos]);
+		ti->history[ti->hpos] = NULL;
+	}
+
+	ti->history[ti->hpos] = tinput_get_str(ti);
+	ti->hpos += offs;
+
+	pad = ti->nc - str_length(ti->history[ti->hpos]);
+	if (pad < 0) pad = 0;
+
+	tinput_set_str(ti, ti->history[ti->hpos]);
+	tinput_display_tail(ti, 0, pad);
+	tinput_update_origin(ti);
+	tinput_position_caret(ti);
+}
+
+/** Initialize text input field.
+ *
+ * Must be called before using the field. It clears the history.
+ */
+static void tinput_init(tinput_t *ti)
+{
+	ti->hnum = 0;
+	ti->hpos = 0;
+	ti->history[0] = NULL;
+}
+
+/** Read in one line of input. */
+static char *tinput_read(tinput_t *ti)
 {
 	console_event_t ev;
-	size_t offs, otmp;
-	wchar_t dec;
-
-	offs = 0;
-	while (true) {
+	char *str;
+
+	fflush(stdout);
+
+	if (console_get_size(fphone(stdin), &ti->con_cols, &ti->con_rows) != EOK)
+		return NULL;
+	if (console_get_pos(fphone(stdin), &ti->col0, &ti->row0) != EOK)
+		return NULL;
+
+	ti->pos = ti->sel_start = 0;
+	ti->nc = 0;
+	ti->buffer[0] = '\0';
+	ti->done = false;
+
+	while (!ti->done) {
 		fflush(stdout);
 		if (!console_get_event(fphone(stdin), &ev))
-			return;
-		
+			return NULL;
+
 		if (ev.type != KEY_PRESS)
 			continue;
-		
-		if (ev.key == KC_ENTER || ev.key == KC_NENTER)
-			break;
-		if (ev.key == KC_BACKSPACE) {
-			if (offs > 0) {
-				/*
-				 * Back up until we reach valid start of
-				 * character.
-				 */
-				while (offs > 0) {
-					--offs; otmp = offs;
-					dec = str_decode(buffer, &otmp, n);
-					if (dec != U_SPECIAL)
-						break;
-				}
-				putchar('\b');
-			}
-			continue;
+
+		if ((ev.mods & KM_CTRL) != 0 &&
+		    (ev.mods & (KM_ALT | KM_SHIFT)) == 0) {
+			tinput_key_ctrl(ti, &ev);
 		}
+
+		if ((ev.mods & KM_SHIFT) != 0 &&
+		    (ev.mods & (KM_CTRL | KM_ALT)) == 0) {
+			tinput_key_shift(ti, &ev);
+		}
+
+		if ((ev.mods & KM_CTRL) != 0 &&
+		    (ev.mods & KM_SHIFT) != 0 &&
+		    (ev.mods & KM_ALT) == 0) {
+			tinput_key_ctrl_shift(ti, &ev);
+		}
+
+		if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
+			tinput_key_unmod(ti, &ev);
+		}
+
 		if (ev.c >= ' ') {
-			if (chr_encode(ev.c, buffer, &offs, n - 1) == EOK)
-				putchar(ev.c);
+			tinput_sel_delete(ti);
+			tinput_insert_char(ti, ev.c);
 		}
 	}
+
+	ti->pos = ti->nc;
+	tinput_position_caret(ti);
 	putchar('\n');
-	buffer[offs] = '\0';
-}
-
-/* TODO:
- * Implement something like editline() / readline(), if even
- * just for command history and making arrows work. */
+
+	str = tinput_get_str(ti);
+	if (str_cmp(str, "") != 0)
+		tinput_history_insert(ti, str);
+
+	ti->hpos = 0;
+
+	return str;
+}
+
+static void tinput_key_ctrl(tinput_t *ti, console_event_t *ev)
+{
+	switch (ev->key) {
+	case KC_LEFT:
+		tinput_seek_word(ti, seek_backward, false);
+		break;
+	case KC_RIGHT:
+		tinput_seek_word(ti, seek_forward, false);
+		break;
+	case KC_UP:
+		tinput_seek_vertical(ti, seek_backward, false);
+		break;
+	case KC_DOWN:
+		tinput_seek_vertical(ti, seek_forward, false);
+		break;
+	case KC_X:
+		tinput_sel_copy_to_cb(ti);
+		tinput_sel_delete(ti);
+		break;
+	case KC_C:
+		tinput_sel_copy_to_cb(ti);
+		break;
+	case KC_V:
+		tinput_sel_delete(ti);
+		tinput_paste_from_cb(ti);
+		break;
+	case KC_A:
+		tinput_sel_all(ti);
+		break;
+	default:
+		break;
+	}
+}
+
+static void tinput_key_ctrl_shift(tinput_t *ti, console_event_t *ev)
+{
+	switch (ev->key) {
+	case KC_LEFT:
+		tinput_seek_word(ti, seek_backward, true);
+		break;
+	case KC_RIGHT:
+		tinput_seek_word(ti, seek_forward, true);
+		break;
+	case KC_UP:
+		tinput_seek_vertical(ti, seek_backward, true);
+		break;
+	case KC_DOWN:
+		tinput_seek_vertical(ti, seek_forward, true);
+		break;
+	default:
+		break;
+	}
+}
+
+static void tinput_key_shift(tinput_t *ti, console_event_t *ev)
+{
+	switch (ev->key) {
+	case KC_LEFT:
+		tinput_seek_cell(ti, seek_backward, true);
+		break;
+	case KC_RIGHT:
+		tinput_seek_cell(ti, seek_forward, true);
+		break;
+	case KC_UP:
+		tinput_seek_vertical(ti, seek_backward, true);
+		break;
+	case KC_DOWN:
+		tinput_seek_vertical(ti, seek_forward, true);
+		break;
+	case KC_HOME:
+		tinput_seek_max(ti, seek_backward, true);
+		break;
+	case KC_END:
+		tinput_seek_max(ti, seek_forward, true);
+		break;
+	default:
+		break;
+	}
+}
+
+static void tinput_key_unmod(tinput_t *ti, console_event_t *ev)
+{
+	switch (ev->key) {
+	case KC_ENTER:
+	case KC_NENTER:
+		ti->done = true;
+		break;
+	case KC_BACKSPACE:
+		tinput_backspace(ti);
+		break;
+	case KC_DELETE:
+		tinput_delete(ti);
+		break;
+	case KC_LEFT:
+		tinput_seek_cell(ti, seek_backward, false);
+		break;
+	case KC_RIGHT:
+		tinput_seek_cell(ti, seek_forward, false);
+		break;
+	case KC_HOME:
+		tinput_seek_max(ti, seek_backward, false);
+		break;
+	case KC_END:
+		tinput_seek_max(ti, seek_forward, false);
+		break;
+	case KC_UP:
+		tinput_history_seek(ti, +1);
+		break;
+	case KC_DOWN:
+		tinput_history_seek(ti, -1);
+		break;
+	default:
+		break;
+	}
+}
+
 void get_input(cliuser_t *usr)
 {
-	char line[INPUT_MAX];
+	char *str;
 
 	fflush(stdout);
@@ -154,11 +775,18 @@
 	console_set_style(fphone(stdout), STYLE_NORMAL);
 
-	read_line(line, INPUT_MAX);
-	/* Make sure we don't have rubbish or a C/R happy user */
-	if (str_cmp(line, "") == 0 || str_cmp(line, "\n") == 0)
-		return;
-	usr->line = str_dup(line);
-
+	str = tinput_read(&tinput);
+
+	/* Check for empty input. */
+	if (str_cmp(str, "") == 0) {
+		free(str);
+		return;
+	}
+
+	usr->line = str;
 	return;
 }
 
+void input_init(void)
+{
+	tinput_init(&tinput);
+}
Index: uspace/app/bdsh/input.h
===================================================================
--- uspace/app/bdsh/input.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/bdsh/input.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -8,4 +8,5 @@
 extern void get_input(cliuser_t *);
 extern int tok_input(cliuser_t *);
+extern void input_init(void);
 
 #endif
Index: uspace/app/bdsh/scli.c
===================================================================
--- uspace/app/bdsh/scli.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/bdsh/scli.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -64,4 +64,7 @@
 	usr->prompt = (char *) NULL;
 	usr->lasterr = 0;
+
+	input_init();
+
 	return (int) cli_set_prompt(usr);
 }
Index: uspace/app/edit/edit.c
===================================================================
--- uspace/app/edit/edit.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/edit/edit.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,4 +36,5 @@
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <vfs/vfs.h>
@@ -44,4 +45,5 @@
 #include <align.h>
 #include <macros.h>
+#include <clipboard.h>
 #include <bool.h>
 
@@ -73,4 +75,7 @@
 	tag_t caret_pos;
 
+	/** Start of selection */
+	tag_t sel_start;
+
 	/** 
 	 * Ideal column where the caret should try to get. This is used
@@ -101,10 +106,20 @@
 #define ED_INFTY 65536
 
+/** Maximum filename length that can be entered. */
+#define INFNAME_MAX_LEN 128
+
 static void key_handle_unmod(console_event_t const *ev);
 static void key_handle_ctrl(console_event_t const *ev);
+static void key_handle_shift(console_event_t const *ev);
+static void key_handle_movement(unsigned int key, bool shift);
+
 static int file_save(char const *fname);
+static void file_save_as(void);
 static int file_insert(char *fname);
 static int file_save_range(char const *fname, spt_t const *spos,
     spt_t const *epos);
+static char *filename_prompt(char const *prompt, char const *init_value);
+static char *range_get_str(spt_t const *spos, spt_t const *epos);
+
 static void pane_text_display(void);
 static void pane_row_display(void);
@@ -112,4 +127,5 @@
 static void pane_status_display(void);
 static void pane_caret_display(void);
+
 static void insert_char(wchar_t c);
 static void delete_char_before(void);
@@ -117,6 +133,18 @@
 static void caret_update(void);
 static void caret_move(int drow, int dcolumn, enum dir_spec align_dir);
+
+static bool selection_active(void);
+static void selection_sel_all(void);
+static void selection_get_points(spt_t *pa, spt_t *pb);
+static void selection_delete(void);
+static void selection_copy(void);
+static void insert_clipboard_data(void);
+
 static void pt_get_sof(spt_t *pt);
 static void pt_get_eof(spt_t *pt);
+static int tag_cmp(tag_t const *a, tag_t const *b);
+static int spt_cmp(spt_t const *a, spt_t const *b);
+static int coord_cmp(coord_t const *a, coord_t const *b);
+
 static void status_display(char const *str);
 
@@ -150,19 +178,23 @@
 
 	if (argc == 2) {
-		doc.file_name = argv[1];
+		doc.file_name = str_dup(argv[1]);
 	} else if (argc > 1) {
 		printf("Invalid arguments.\n");
 		return -2;
 	} else {
-		doc.file_name = "/edit.txt";
+		doc.file_name = NULL;
 	}
 
 	new_file = false;
 
-	if (file_insert(doc.file_name) != EOK)
+	if (doc.file_name == NULL || file_insert(doc.file_name) != EOK)
 		new_file = true;
 
 	/* Move to beginning of file. */
 	caret_move(-ED_INFTY, -ED_INFTY, dir_before);
+
+	/* Place selection start tag. */
+	tag_get_pt(&pane.caret_pos, &pt);
+	sheet_place_tag(&doc.sh, &pt, &pane.sel_start);
 
 	/* Initial display */
@@ -170,6 +202,6 @@
 	pane_text_display();
 	pane_status_display();
-	if (new_file)
-		status_display("File not found. Created empty file.");
+	if (new_file && doc.file_name != NULL)
+		status_display("File not found. Starting empty file.");
 	pane_caret_display();
 
@@ -184,7 +216,12 @@
 			/* Handle key press. */
 			if (((ev.mods & KM_ALT) == 0) &&
+			    ((ev.mods & KM_SHIFT) == 0) &&
 			     (ev.mods & KM_CTRL) != 0) {
 				key_handle_ctrl(&ev);
-			} else if ((ev.mods & (KM_CTRL | KM_ALT)) == 0) {
+			} else if (((ev.mods & KM_ALT) == 0) &&
+			    ((ev.mods & KM_CTRL) == 0) &&
+			     (ev.mods & KM_SHIFT) != 0) {
+				key_handle_shift(&ev);
+			} else if ((ev.mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) {
 				key_handle_unmod(&ev);
 			}
@@ -201,5 +238,4 @@
 		if (pane.rflags & REDRAW_CARET)
 			pane_caret_display();
-			
 	}
 
@@ -214,41 +250,35 @@
 	switch (ev->key) {
 	case KC_ENTER:
+		selection_delete();
 		insert_char('\n');
 		caret_update();
 		break;
 	case KC_LEFT:
-		caret_move(0, -1, dir_before);
-		break;
 	case KC_RIGHT:
-		caret_move(0, 0, dir_after);
-		break;
 	case KC_UP:
-		caret_move(-1, 0, dir_before);
-		break;
 	case KC_DOWN:
-		caret_move(+1, 0, dir_before);
-		break;
 	case KC_HOME:
-		caret_move(0, -ED_INFTY, dir_before);
-		break;
 	case KC_END:
-		caret_move(0, +ED_INFTY, dir_before);
-		break;
 	case KC_PAGE_UP:
-		caret_move(-pane.rows, 0, dir_before);
-		break;
 	case KC_PAGE_DOWN:
-		caret_move(+pane.rows, 0, dir_before);
+		key_handle_movement(ev->key, false);
 		break;
 	case KC_BACKSPACE:
-		delete_char_before();
+		if (selection_active())
+			selection_delete();
+		else
+			delete_char_before();
 		caret_update();
 		break;
 	case KC_DELETE:
-		delete_char_after();
+		if (selection_active())
+			selection_delete();
+		else
+			delete_char_after();
 		caret_update();
 		break;
 	default:
 		if (ev->c >= 32 || ev->c == '\t') {
+			selection_delete();
 			insert_char(ev->c);
 			caret_update();
@@ -258,4 +288,28 @@
 }
 
+/** Handle Shift-key combination. */
+static void key_handle_shift(console_event_t const *ev)
+{
+	switch (ev->key) {
+	case KC_LEFT:
+	case KC_RIGHT:
+	case KC_UP:
+	case KC_DOWN:
+	case KC_HOME:
+	case KC_END:
+	case KC_PAGE_UP:
+	case KC_PAGE_DOWN:
+		key_handle_movement(ev->key, true);
+		break;
+	default:
+		if (ev->c >= 32 || ev->c == '\t') {
+			selection_delete();
+			insert_char(ev->c);
+			caret_update();
+		}
+		break;
+	}
+}
+
 /** Handle Ctrl-key combination. */
 static void key_handle_ctrl(console_event_t const *ev)
@@ -266,5 +320,29 @@
 		break;
 	case KC_S:
-		(void) file_save(doc.file_name);
+		if (doc.file_name != NULL)
+			file_save(doc.file_name);
+		else
+			file_save_as();
+		break;
+	case KC_E:
+		file_save_as();
+		break;
+	case KC_C:
+		selection_copy();
+		break;
+	case KC_V:
+		selection_delete();
+		insert_clipboard_data();
+		pane.rflags |= REDRAW_TEXT;
+		caret_update();
+		break;
+	case KC_X:
+		selection_copy();
+		selection_delete();
+		pane.rflags |= REDRAW_TEXT;
+		caret_update();
+		break;
+	case KC_A:
+		selection_sel_all();
 		break;
 	default:
@@ -273,4 +351,67 @@
 }
 
+static void key_handle_movement(unsigned int key, bool select)
+{
+	spt_t pt;
+	spt_t caret_pt;
+	coord_t c_old, c_new;
+	bool had_sel;
+
+	/* Check if we had selection before. */
+	tag_get_pt(&pane.caret_pos, &caret_pt);
+	tag_get_pt(&pane.sel_start, &pt);
+	had_sel = !spt_equal(&caret_pt, &pt);
+
+	switch (key) {
+	case KC_LEFT:
+		caret_move(0, -1, dir_before);
+		break;
+	case KC_RIGHT:
+		caret_move(0, 0, dir_after);
+		break;
+	case KC_UP:
+		caret_move(-1, 0, dir_before);
+		break;
+	case KC_DOWN:
+		caret_move(+1, 0, dir_before);
+		break;
+	case KC_HOME:
+		caret_move(0, -ED_INFTY, dir_before);
+		break;
+	case KC_END:
+		caret_move(0, +ED_INFTY, dir_before);
+		break;
+	case KC_PAGE_UP:
+		caret_move(-pane.rows, 0, dir_before);
+		break;
+	case KC_PAGE_DOWN:
+		caret_move(+pane.rows, 0, dir_before);
+		break;
+	default:
+		break;
+	}
+
+	if (select == false) {
+		/* Move sel_start to the same point as caret. */
+		sheet_remove_tag(&doc.sh, &pane.sel_start);
+		tag_get_pt(&pane.caret_pos, &pt);
+		sheet_place_tag(&doc.sh, &pt, &pane.sel_start);
+	}
+
+	if (select) {
+		tag_get_pt(&pane.caret_pos, &pt);
+		spt_get_coord(&caret_pt, &c_old);
+		spt_get_coord(&pt, &c_new);
+
+		if (c_old.row == c_new.row)
+			pane.rflags |= REDRAW_ROW;
+		else
+			pane.rflags |= REDRAW_TEXT;
+
+	} else if (had_sel == true) {
+		/* Redraw because text was unselected. */
+		pane.rflags |= REDRAW_TEXT;
+	}
+}
 
 /** Save the document. */
@@ -285,7 +426,104 @@
 
 	rc = file_save_range(fname, &sp, &ep);
-	status_display("File saved.");
+
+	switch (rc) {
+	case EINVAL:
+		status_display("Error opening file!");
+		break;
+	case EIO:
+		status_display("Error writing data!");
+		break;
+	default:
+		status_display("File saved.");
+		break;
+	}
 
 	return rc;
+}
+
+/** Change document name and save. */
+static void file_save_as(void)
+{
+	char *old_fname, *fname;
+	int rc;
+
+	old_fname = (doc.file_name != NULL) ? doc.file_name : "";
+	fname = filename_prompt("Save As", old_fname);
+	if (fname == NULL) {
+		status_display("Save cancelled.");
+		return;
+	}
+
+	rc = file_save(fname);
+	if (rc != EOK)
+		return;
+
+	if (doc.file_name != NULL)
+		free(doc.file_name);
+	doc.file_name = fname;
+}
+
+/** Ask for a file name. */
+static char *filename_prompt(char const *prompt, char const *init_value)
+{
+	console_event_t ev;
+	char *str;
+	wchar_t buffer[INFNAME_MAX_LEN + 1];
+	int max_len;
+	int nc;
+	bool done;
+
+	asprintf(&str, "%s: %s", prompt, init_value);
+	status_display(str);
+	console_goto(con, 1 + str_length(str), scr_rows - 1);
+	free(str);
+
+	console_set_color(con, COLOR_WHITE, COLOR_BLACK, 0);
+
+	max_len = min(INFNAME_MAX_LEN, scr_columns - 4 - str_length(prompt));
+	str_to_wstr(buffer, max_len + 1, init_value);
+	nc = wstr_length(buffer);
+	done = false;
+
+	while (!done) {
+		console_get_event(con, &ev);
+
+		if (ev.type == KEY_PRESS) {
+			/* Handle key press. */
+			if (((ev.mods & KM_ALT) == 0) &&
+			     (ev.mods & KM_CTRL) != 0) {
+				;
+			} else if ((ev.mods & (KM_CTRL | KM_ALT)) == 0) {
+				switch (ev.key) {
+				case KC_ESCAPE:
+					return NULL;
+				case KC_BACKSPACE:
+					if (nc > 0) {
+						putchar('\b');
+						fflush(stdout);
+						--nc;
+					}
+					break;
+				case KC_ENTER:
+					done = true;
+					break;
+				default:
+					if (ev.c >= 32 && nc < max_len) {
+						putchar(ev.c);
+						fflush(stdout);
+						buffer[nc++] = ev.c;
+					}
+					break;
+				}
+			}
+		}
+	}
+
+	buffer[nc] = '\0';
+	str = wstr_to_astr(buffer);
+
+	console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0);
+
+	return str;
 }
 
@@ -359,7 +597,44 @@
 	} while (!spt_equal(&bep, epos));
 
-	fclose(f);
+	if (fclose(f) != EOK)
+		return EIO;
 
 	return EOK;
+}
+
+/** Return contents of range as a new string. */
+static char *range_get_str(spt_t const *spos, spt_t const *epos)
+{
+	char *buf;
+	spt_t sp, bep;
+	size_t bytes;
+	size_t buf_size, bpos;
+
+	buf_size = 1;
+
+	buf = malloc(buf_size);
+	if (buf == NULL)
+		return NULL;
+
+	bpos = 0;
+	sp = *spos;
+
+	while (true) {
+		sheet_copy_out(&doc.sh, &sp, epos, &buf[bpos], buf_size - bpos,
+		    &bep);
+		bytes = str_size(&buf[bpos]);
+		bpos += bytes;
+		sp = bep;
+
+		if (spt_equal(&bep, epos))
+			break;
+
+		buf_size *= 2;
+		buf = realloc(buf, buf_size);
+		if (buf == NULL)
+			return NULL;
+	}
+
+	return buf;
 }
 
@@ -408,5 +683,5 @@
 {
 	int i, j, fill;
-	spt_t rb, re, dep;
+	spt_t rb, re, dep, pt;
 	coord_t rbc, rec;
 	char row_buf[ROW_BUF_SIZE];
@@ -414,4 +689,19 @@
 	size_t pos, size;
 	unsigned s_column;
+	coord_t csel_start, csel_end, ctmp;
+
+	/* Determine selection start and end. */
+
+	tag_get_pt(&pane.sel_start, &pt);
+	spt_get_coord(&pt, &csel_start);
+
+	tag_get_pt(&pane.caret_pos, &pt);
+	spt_get_coord(&pt, &csel_end);
+
+	if (coord_cmp(&csel_start, &csel_end) > 0) {
+		ctmp = csel_start;
+		csel_start = csel_end;
+		csel_end = ctmp;
+	}
 
 	/* Draw rows from the sheet. */
@@ -434,9 +724,28 @@
 		/* Display text from the buffer. */
 
+		if (coord_cmp(&csel_start, &rbc) <= 0 &&
+		    coord_cmp(&rbc, &csel_end) < 0) {
+			fflush(stdout);
+			console_set_color(con, COLOR_BLACK, COLOR_RED, 0);
+			fflush(stdout);
+		}
+
 		console_goto(con, 0, i);
 		size = str_size(row_buf);
 		pos = 0;
-		s_column = 1;
+		s_column = pane.sh_column;
 		while (pos < size) {
+			if (csel_start.row == rbc.row && csel_start.column == s_column) {
+				fflush(stdout);
+				console_set_color(con, COLOR_BLACK, COLOR_RED, 0);
+				fflush(stdout);
+			}
+	
+			if (csel_end.row == rbc.row && csel_end.column == s_column) {
+				fflush(stdout);
+				console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0);
+				fflush(stdout);
+			}
+	
 			c = str_decode(row_buf, &pos, size);
 			if (c != '\t') {
@@ -453,4 +762,10 @@
 		}
 
+		if (csel_end.row == rbc.row && csel_end.column == s_column) {
+			fflush(stdout);
+			console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0);
+			fflush(stdout);
+		}
+
 		/* Fill until the end of display area. */
 
@@ -463,4 +778,5 @@
 			putchar(' ');
 		fflush(stdout);
+		console_set_color(con, COLOR_BLACK, COLOR_WHITE, 0);
 	}
 
@@ -473,4 +789,5 @@
 	spt_t caret_pt;
 	coord_t coord;
+	char *fname;
 	int n;
 
@@ -478,8 +795,10 @@
 	spt_get_coord(&caret_pt, &coord);
 
+	fname = (doc.file_name != NULL) ? doc.file_name : "<unnamed>";
+
 	console_goto(con, 0, scr_rows - 1);
 	console_set_color(con, COLOR_WHITE, COLOR_BLACK, 0);
-	n = printf(" %d, %d: File '%s'. Ctrl-S Save  Ctrl-Q Quit",
-	    coord.row, coord.column, doc.file_name);
+	n = printf(" %d, %d: File '%s'. Ctrl-Q Quit  Ctrl-S Save  "
+	    "Ctrl-E Save As", coord.row, coord.column, fname);
 	printf("%*s", scr_columns - 1 - n, "");
 	fflush(stdout);
@@ -648,4 +967,102 @@
 }
 
+/** Check for non-empty selection. */
+static bool selection_active(void)
+{
+	return (tag_cmp(&pane.caret_pos, &pane.sel_start) != 0);
+}
+
+static void selection_get_points(spt_t *pa, spt_t *pb)
+{
+	spt_t pt;
+
+	tag_get_pt(&pane.sel_start, pa);
+	tag_get_pt(&pane.caret_pos, pb);
+
+	if (spt_cmp(pa, pb) > 0) {
+		pt = *pa;
+		*pa = *pb;
+		*pb = pt;
+	}
+}
+
+/** Delete selected text. */
+static void selection_delete(void)
+{
+	spt_t pa, pb;
+	coord_t ca, cb;
+	int rel;
+
+	tag_get_pt(&pane.sel_start, &pa);
+	tag_get_pt(&pane.caret_pos, &pb);
+	spt_get_coord(&pa, &ca);
+	spt_get_coord(&pb, &cb);
+	rel = coord_cmp(&ca, &cb);
+
+	if (rel == 0)
+		return;
+
+	if (rel < 0)
+		sheet_delete(&doc.sh, &pa, &pb);
+	else
+		sheet_delete(&doc.sh, &pb, &pa);
+
+	if (ca.row == cb.row)
+		pane.rflags |= REDRAW_ROW;
+	else
+		pane.rflags |= REDRAW_TEXT;
+}
+
+static void selection_sel_all(void)
+{
+	spt_t spt, ept;
+
+	pt_get_sof(&spt);
+	pt_get_eof(&ept);
+	sheet_remove_tag(&doc.sh, &pane.sel_start);
+	sheet_place_tag(&doc.sh, &spt, &pane.sel_start);
+	sheet_remove_tag(&doc.sh, &pane.caret_pos);
+	sheet_place_tag(&doc.sh, &ept, &pane.caret_pos);
+
+	pane.rflags |= REDRAW_TEXT;
+	caret_update();
+}
+
+static void selection_copy(void)
+{
+	spt_t pa, pb;
+	char *str;
+
+	selection_get_points(&pa, &pb);
+	str = range_get_str(&pa, &pb);
+	if (str == NULL || clipboard_put_str(str) != EOK) {
+		status_display("Copying to clipboard failed!");
+	}
+	free(str);
+}
+
+static void insert_clipboard_data(void)
+{
+	char *str;
+	size_t off;
+	wchar_t c;
+	int rc;
+
+	rc = clipboard_get_str(&str);
+	if (rc != EOK || str == NULL)
+		return;
+
+	off = 0;
+
+	while (true) {
+		c = str_decode(str, &off, STR_NO_LIMIT);
+		if (c == '\0')
+			break;
+
+		insert_char(c);
+	}
+
+	free(str);
+}
 
 /** Get start-of-file s-point. */
@@ -669,4 +1086,35 @@
 
 	sheet_get_cell_pt(&doc.sh, &coord, dir_after, pt);
+}
+
+/** Compare tags. */
+static int tag_cmp(tag_t const *a, tag_t const *b)
+{
+	spt_t pa, pb;
+
+	tag_get_pt(a, &pa);
+	tag_get_pt(b, &pb);
+
+	return spt_cmp(&pa, &pb);
+}
+
+/** Compare s-points. */
+static int spt_cmp(spt_t const *a, spt_t const *b)
+{
+	coord_t ca, cb;
+
+	spt_get_coord(a, &ca);
+	spt_get_coord(b, &cb);
+
+	return coord_cmp(&ca, &cb);
+}
+
+/** Compare coordinats. */
+static int coord_cmp(coord_t const *a, coord_t const *b)
+{
+	if (a->row - b->row != 0)
+		return a->row - b->row;
+
+	return a->column - b->column;
 }
 
Index: uspace/app/getterm/Makefile
===================================================================
--- uspace/app/getterm/Makefile	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/app/getterm/Makefile	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,40 @@
+#
+# Copyright (c) 2005 Martin Decky
+# Copyright (c) 2007 Jakub Jermar
+# 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.
+#
+
+include Makefile.common
+
+.PHONY: all clean
+
+all: $(LIBC_PREFIX)/../../../version $(LIBC_PREFIX)/../../../Makefile.config $(LIBC_PREFIX)/../../../config.h $(LIBC_PREFIX)/../../../config.defs $(LIBS)
+	-[ -f $(DEPEND) ] && mv -f $(DEPEND) $(DEPEND_PREV)
+	$(MAKE) -f Makefile.build
+
+clean:
+	rm -f $(DEPEND) $(DEPEND_PREV) $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm
+	find . -name '*.o' -follow -exec rm \{\} \;
Index: uspace/app/getterm/Makefile.build
===================================================================
--- uspace/app/getterm/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/app/getterm/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,65 @@
+#
+# Copyright (c) 2005 Martin Decky
+# Copyright (c) 2007 Jakub Jermar
+# 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.
+#
+
+## Setup toolchain
+#
+
+include Makefile.common
+include $(LIBC_PREFIX)/Makefile.toolchain
+include ../../../version
+
+DEFS += -DRELEASE=$(RELEASE) "-DNAME=$(NAME)"
+
+## Sources
+#
+
+SOURCES = \
+	getterm.c \
+	version.c
+
+OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
+
+.PHONY: all
+
+all: $(OUTPUT) $(OUTPUT).disasm
+
+-include $(DEPEND)
+
+$(OUTPUT).disasm: $(OUTPUT)
+	$(OBJDUMP) -d $< > $@
+
+$(OUTPUT): $(OBJECTS) $(LIBS)
+	$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
+
+%.o: %.c $(DEPEND)
+	$(CC) $(DEFS) $(CFLAGS) -c $< -o $@
+
+$(DEPEND):
+	makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
+	-[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
Index: uspace/app/getterm/Makefile.common
===================================================================
--- uspace/app/getterm/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/app/getterm/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,39 @@
+#
+# Copyright (c) 2005 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.
+#
+
+
+## Common names
+#
+
+LIBC_PREFIX = ../../lib/libc
+SOFTINT_PREFIX = ../../lib/softint
+LIBS = $(LIBC_PREFIX)/libc.a
+
+DEPEND = Makefile.depend
+DEPEND_PREV = $(DEPEND).prev
+OUTPUT = getterm
Index: uspace/app/getterm/getterm.c
===================================================================
--- uspace/app/getterm/getterm.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/app/getterm/getterm.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2009 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 getterm GetTerm
+ * @brief Console initialization task.
+ * @{
+ */
+/**
+ * @file
+ */
+
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <task.h>
+#include "version.h"
+
+static void usage(void)
+{
+	printf("Usage: getterm <terminal> <path>\n");
+}
+
+static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
+{
+	if (fclose(*stream))
+		return;
+	
+	*stream = NULL;
+	
+	int oldfd = open(path, flags);
+	if (oldfd < 0)
+		return;
+	
+	if (oldfd != fd) {
+		if (dup2(oldfd, fd) != fd)
+			return;
+		
+		if (close(oldfd))
+			return;
+	}
+	
+	*stream = fdopen(fd, mode);
+}
+
+static task_id_t spawn(char *fname)
+{
+	char *args[2];
+	
+	args[0] = fname;
+	args[1] = NULL;
+	
+	task_id_t id = task_spawn(fname, args);
+	
+	if (id == 0)
+		printf("Error spawning %s\n", fname);
+	
+	return id;
+}
+
+int main(int argc, char *argv[])
+{
+	if (argc < 3) {
+		usage();
+		return -1;
+	}
+	
+	reopen(&stdin, 0, argv[1], O_RDONLY, "r");
+	reopen(&stdout, 1, argv[1], O_WRONLY, "w");
+	reopen(&stderr, 2, argv[1], O_WRONLY, "w");
+	
+	/*
+	 * FIXME: fdopen() should actually detect that we are opening a console
+	 * and it should set line-buffering mode automatically.
+	 */
+	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
+	
+	if (stdin == NULL)
+		return -2;
+	
+	if (stdout == NULL)
+		return -3;
+	
+	if (stderr == NULL)
+		return -4;
+	
+	version_print(argv[1]);
+	task_id_t id = spawn(argv[2]);
+	
+	if (id != 0) {
+		task_exit_t texit;
+		int retval;
+		task_wait(id, &texit, &retval);
+		
+		return 0;
+	}
+	
+	return -5;
+}
+
+/** @}
+ */
Index: uspace/app/getterm/getterm.h
===================================================================
--- uspace/app/getterm/getterm.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/app/getterm/getterm.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2009 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 getterm
+ * @{
+ */
+/**
+ * @file
+ */
+
+#ifndef GETTERM_H__
+#define GETTERM_H__
+
+#endif
+
+/** @}
+ */
Index: uspace/app/getterm/version.c
===================================================================
--- uspace/app/getterm/version.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/app/getterm/version.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2009 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 getterm
+ * @{
+ */
+/**
+ * @file
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <macros.h>
+#include "getterm.h"
+#include "version.h"
+
+static char *release = STRING(RELEASE);
+static char *name = STRING(NAME);
+static char *arch = STRING(UARCH);
+
+#ifdef REVISION
+	static char *revision = ", revision " STRING(REVISION);
+#else
+	static char *revision = "";
+#endif
+
+#ifdef TIMESTAMP
+	static char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
+#else
+	static char *timestamp = "";
+#endif
+
+/** Print version information. */
+void version_print(const char *term)
+{
+	printf("HelenOS release %s (%s)%s%s\n", release, name, revision, timestamp);
+	printf("Running on %s (%s)\n", arch, term);
+	printf("Copyright (c) 2001-2009 HelenOS project\n\n");
+}
+
+/** @}
+ */
Index: uspace/app/getterm/version.h
===================================================================
--- uspace/app/getterm/version.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/app/getterm/version.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2009 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 getterm
+ * @{
+ */
+/**
+ * @file
+ */
+
+#ifndef VERSION_H__
+#define VERSION_H__
+
+extern void version_print(const char *term);
+
+#endif
+
+/** @}
+ */
Index: pace/app/getvc/Makefile
===================================================================
--- uspace/app/getvc/Makefile	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,40 +1,0 @@
-#
-# Copyright (c) 2005 Martin Decky
-# Copyright (c) 2007 Jakub Jermar
-# 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.
-#
-
-include Makefile.common
-
-.PHONY: all clean
-
-all: $(LIBC_PREFIX)/../../../version $(LIBC_PREFIX)/../../../Makefile.config $(LIBC_PREFIX)/../../../config.h $(LIBC_PREFIX)/../../../config.defs $(LIBS)
-	-[ -f $(DEPEND) ] && mv -f $(DEPEND) $(DEPEND_PREV)
-	$(MAKE) -f Makefile.build
-
-clean:
-	rm -f $(DEPEND) $(DEPEND_PREV) $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm
-	find . -name '*.o' -follow -exec rm \{\} \;
Index: pace/app/getvc/Makefile.build
===================================================================
--- uspace/app/getvc/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,65 +1,0 @@
-#
-# Copyright (c) 2005 Martin Decky
-# Copyright (c) 2007 Jakub Jermar
-# 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.
-#
-
-## Setup toolchain
-#
-
-include Makefile.common
-include $(LIBC_PREFIX)/Makefile.toolchain
-include ../../../version
-
-DEFS += -DRELEASE=$(RELEASE) "-DNAME=$(NAME)"
-
-## Sources
-#
-
-SOURCES = \
-	getvc.c \
-	version.c
-
-OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
-
-.PHONY: all
-
-all: $(OUTPUT) $(OUTPUT).disasm
-
--include $(DEPEND)
-
-$(OUTPUT).disasm: $(OUTPUT)
-	$(OBJDUMP) -d $< > $@
-
-$(OUTPUT): $(OBJECTS) $(LIBS)
-	$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
-
-%.o: %.c $(DEPEND)
-	$(CC) $(DEFS) $(CFLAGS) -c $< -o $@
-
-$(DEPEND):
-	makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
-	-[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
Index: pace/app/getvc/Makefile.common
===================================================================
--- uspace/app/getvc/Makefile.common	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,39 +1,0 @@
-#
-# Copyright (c) 2005 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.
-#
-
-
-## Common names
-#
-
-LIBC_PREFIX = ../../lib/libc
-SOFTINT_PREFIX = ../../lib/softint
-LIBS = $(LIBC_PREFIX)/libc.a
-
-DEPEND = Makefile.depend
-DEPEND_PREV = $(DEPEND).prev
-OUTPUT = getvc
Index: pace/app/getvc/getvc.c
===================================================================
--- uspace/app/getvc/getvc.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,127 +1,0 @@
-/*
- * Copyright (c) 2009 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 getvc GetVC
- * @brief Console initialization task.
- * @{
- */
-/**
- * @file
- */
-
-#include <sys/types.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <task.h>
-#include "version.h"
-
-static void usage(void)
-{
-	printf("Usage: getvc <device> <path>\n");
-}
-
-static void reopen(FILE **stream, int fd, const char *path, int flags, const char *mode)
-{
-	if (fclose(*stream))
-		return;
-	
-	*stream = NULL;
-	
-	int oldfd = open(path, flags);
-	if (oldfd < 0)
-		return;
-	
-	if (oldfd != fd) {
-		if (dup2(oldfd, fd) != fd)
-			return;
-		
-		if (close(oldfd))
-			return;
-	}
-	
-	*stream = fdopen(fd, mode);
-}
-
-static task_id_t spawn(char *fname)
-{
-	char *args[2];
-	
-	args[0] = fname;
-	args[1] = NULL;
-	
-	task_id_t id = task_spawn(fname, args);
-	
-	if (id == 0)
-		printf("Error spawning %s\n", fname);
-	
-	return id;
-}
-
-int main(int argc, char *argv[])
-{
-	if (argc < 3) {
-		usage();
-		return -1;
-	}
-	
-	reopen(&stdin, 0, argv[1], O_RDONLY, "r");
-	reopen(&stdout, 1, argv[1], O_WRONLY, "w");
-	reopen(&stderr, 2, argv[1], O_WRONLY, "w");
-	
-	/*
-	 * FIXME: fdopen() should actually detect that we are opening a console
-	 * and it should set line-buffering mode automatically.
-	 */
-	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
-	
-	if (stdin == NULL)
-		return -2;
-	
-	if (stdout == NULL)
-		return -3;
-	
-	if (stderr == NULL)
-		return -4;
-	
-	version_print(argv[1]);
-	task_id_t id = spawn(argv[2]);
-	
-	if (id != 0) {
-		task_exit_t texit;
-		int retval;
-		task_wait(id, &texit, &retval);
-		
-		return 0;
-	}
-	
-	return -5;
-}
-
-/** @}
- */
Index: pace/app/getvc/getvc.h
===================================================================
--- uspace/app/getvc/getvc.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,42 +1,0 @@
-/*
- * Copyright (c) 2009 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 getvc
- * @{
- */
-/**
- * @file
- */
-
-#ifndef GETVC_H__
-#define GETVC_H__
-
-#endif
-
-/** @}
- */
Index: pace/app/getvc/version.c
===================================================================
--- uspace/app/getvc/version.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,67 +1,0 @@
-/*
- * Copyright (c) 2009 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 getvc
- * @{
- */
-/**
- * @file
- */
-
-#include <unistd.h>
-#include <stdio.h>
-#include <macros.h>
-#include "getvc.h"
-#include "version.h"
-
-static char *release = STRING(RELEASE);
-static char *name = STRING(NAME);
-static char *arch = STRING(UARCH);
-
-#ifdef REVISION
-	static char *revision = ", revision " STRING(REVISION);
-#else
-	static char *revision = "";
-#endif
-
-#ifdef TIMESTAMP
-	static char *timestamp = "\nBuilt on " STRING(TIMESTAMP);
-#else
-	static char *timestamp = "";
-#endif
-
-/** Print version information. */
-void version_print(const char *vc)
-{
-	printf("HelenOS release %s (%s)%s%s\n", release, name, revision, timestamp);
-	printf("Running on %s (%s)\n", arch, vc);
-	printf("Copyright (c) 2001-2009 HelenOS project\n\n");
-}
-
-/** @}
- */
Index: pace/app/getvc/version.h
===================================================================
--- uspace/app/getvc/version.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,44 +1,0 @@
-/*
- * Copyright (c) 2009 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 getvc
- * @{
- */
-/**
- * @file
- */
-
-#ifndef VERSION_H__
-#define VERSION_H__
-
-extern void version_print(const char *vc);
-
-#endif
-
-/** @}
- */
Index: uspace/app/init/init.c
===================================================================
--- uspace/app/init/init.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/init/init.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -50,4 +50,9 @@
 #include "init.h"
 
+#define DEVFS_MOUNT_POINT  "/dev"
+
+#define SRV_CONSOLE  "/srv/console"
+#define APP_GETTERM  "/app/getterm"
+
 static void info_print(void)
 {
@@ -58,5 +63,5 @@
 {
 	char *opts = "";
-	const char *root_dev = "initrd";
+	const char *root_dev = "bd/initrd";
 	
 	if (str_cmp(fstype, "tmpfs") == 0)
@@ -97,6 +102,6 @@
 	}
 	
-	snprintf(null, MAX_DEVICE_NAME, "null%d", null_id);
-	int rc = mount("devfs", "/dev", null, "", IPC_FLAG_BLOCKING);
+	snprintf(null, MAX_DEVICE_NAME, "null/%d", null_id);
+	int rc = mount("devfs", DEVFS_MOUNT_POINT, null, "", IPC_FLAG_BLOCKING);
 	
 	switch (rc) {
@@ -170,5 +175,5 @@
 	}
 
-	if (texit != TASK_EXIT_NORMAL || retval != 0) {
+	if ((texit != TASK_EXIT_NORMAL) || (retval != 0)) {
 		printf(NAME ": Server %s failed to start (returned %d)\n",
 			fname, retval);
@@ -176,28 +181,54 @@
 }
 
-static void getvc(char *dev, char *app)
-{
-	char *argv[4];
-	char vc[MAX_DEVICE_NAME];
+static void console(char *dev)
+{
+	char *argv[3];
+	char hid_in[MAX_DEVICE_NAME];
 	int rc;
 	
-	snprintf(vc, MAX_DEVICE_NAME, "/dev/%s", dev);
-	
-	printf(NAME ": Spawning getvc on %s\n", vc);
-	
+	snprintf(hid_in, MAX_DEVICE_NAME, "%s/%s", DEVFS_MOUNT_POINT, dev);
+	
+	printf(NAME ": Spawning %s with %s\n", SRV_CONSOLE, hid_in);
+	
+	/* Wait for the input device to be ready */
 	dev_handle_t handle;
 	rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
 	
 	if (rc == EOK) {
-		argv[0] = "/app/getvc";
-		argv[1] = vc;
+		argv[0] = SRV_CONSOLE;
+		argv[1] = hid_in;
+		argv[2] = NULL;
+		
+		if (!task_spawn(SRV_CONSOLE, argv))
+			printf(NAME ": Error spawning %s with %s\n", SRV_CONSOLE, hid_in);
+	} else
+		printf(NAME ": Error waiting on %s\n", hid_in);
+}
+
+static void getterm(char *dev, char *app)
+{
+	char *argv[4];
+	char term[MAX_DEVICE_NAME];
+	int rc;
+	
+	snprintf(term, MAX_DEVICE_NAME, "%s/%s", DEVFS_MOUNT_POINT, dev);
+	
+	printf(NAME ": Spawning %s with %s %s\n", APP_GETTERM, term, app);
+	
+	/* Wait for the terminal device to be ready */
+	dev_handle_t handle;
+	rc = devmap_device_get_handle(dev, &handle, IPC_FLAG_BLOCKING);
+	
+	if (rc == EOK) {
+		argv[0] = APP_GETTERM;
+		argv[1] = term;
 		argv[2] = app;
 		argv[3] = NULL;
 		
-		if (!task_spawn("/app/getvc", argv))
-			printf(NAME ": Error spawning getvc on %s\n", vc);
-	} else {
-		printf(NAME ": Error waiting on %s\n", vc);
-	}
+		if (!task_spawn(APP_GETTERM, argv))
+			printf(NAME ": Error spawning %s with %s %s\n", APP_GETTERM,
+			    term, app);
+	} else
+		printf(NAME ": Error waiting on %s\n", term);
 }
 
@@ -206,8 +237,8 @@
 	int rc;
 
-	printf("Trying to mount disk0 on /data... ");
+	printf("Trying to mount bd/disk0 on /data... ");
 	fflush(stdout);
 
-	rc = mount("fat", "/data", "disk0", "wtcache", 0);
+	rc = mount("fat", "/data", "bd/disk0", "wtcache", 0);
 	if (rc == EOK)
 		printf("OK\n");
@@ -234,5 +265,7 @@
 	spawn("/srv/fb");
 	spawn("/srv/kbd");
-	spawn("/srv/console");
+	console("hid_in/kbd");
+	
+	spawn("/srv/clip");
 	spawn("/srv/fhc");
 	spawn("/srv/obio");
@@ -255,11 +288,11 @@
 #endif
 
-	getvc("vc0", "/app/bdsh");
-	getvc("vc1", "/app/bdsh");
-	getvc("vc2", "/app/bdsh");
-	getvc("vc3", "/app/bdsh");
-	getvc("vc4", "/app/bdsh");
-	getvc("vc5", "/app/bdsh");
-	getvc("vc6", "/app/klog");
+	getterm("term/vc0", "/app/bdsh");
+	getterm("term/vc1", "/app/bdsh");
+	getterm("term/vc2", "/app/bdsh");
+	getterm("term/vc3", "/app/bdsh");
+	getterm("term/vc4", "/app/bdsh");
+	getterm("term/vc5", "/app/bdsh");
+	getterm("term/vc6", "/app/klog");
 	
 	return 0;
Index: uspace/app/tester/vfs/vfs1.c
===================================================================
--- uspace/app/tester/vfs/vfs1.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/tester/vfs/vfs1.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -85,5 +85,5 @@
 		return "Unable to create null device";
 	
-	snprintf(null, MAX_DEVICE_NAME, "null%d", null_id);
+	snprintf(null, MAX_DEVICE_NAME, "null/%d", null_id);
 	int rc = mount(FS_TYPE, MOUNT_POINT, null, OPTIONS, FLAGS);
 	switch (rc) {
Index: uspace/app/trace/trace.c
===================================================================
--- uspace/app/trace/trace.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/app/trace/trace.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -48,5 +48,5 @@
 #include <io/console.h>
 #include <io/keycode.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 
 #include <libc.h>
Index: uspace/lib/libblock/libblock.c
===================================================================
--- uspace/lib/libblock/libblock.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libblock/libblock.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -47,5 +47,5 @@
 #include <as.h>
 #include <assert.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <adt/list.h>
 #include <adt/hash_table.h>
Index: uspace/lib/libblock/libblock.h
===================================================================
--- uspace/lib/libblock/libblock.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libblock/libblock.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,9 +36,9 @@
 
 #ifndef LIBBLOCK_LIBBLOCK_H_
-#define	LIBBLOCK_LIBBLOCK_H_ 
+#define LIBBLOCK_LIBBLOCK_H_
 
 #include <stdint.h>
 #include "../../srv/vfs/vfs.h"
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <adt/hash_table.h>
 #include <adt/list.h>
Index: uspace/lib/libc/Makefile.build
===================================================================
--- uspace/lib/libc/Makefile.build	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -50,4 +50,5 @@
 	generic/as.c \
 	generic/cap.c \
+	generic/clipboard.c \
 	generic/devmap.c \
 	generic/event.c \
@@ -56,5 +57,5 @@
 	generic/string.c \
 	generic/fibril.c \
-	generic/fibril_sync.c \
+	generic/fibril_synch.c \
 	generic/pcb.c \
 	generic/smc.c \
Index: uspace/lib/libc/arch/amd64/include/atomic.h
===================================================================
--- uspace/lib/libc/arch/amd64/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/amd64/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -37,4 +37,8 @@
 #ifndef LIBC_amd64_ATOMIC_H_
 #define LIBC_amd64_ATOMIC_H_
+
+#define LIBC_ARCH_ATOMIC_H_
+
+#include <atomicdflt.h>
 
 static inline void atomic_inc(atomic_t *val) {
Index: uspace/lib/libc/arch/arm32/include/atomic.h
===================================================================
--- uspace/lib/libc/arch/arm32/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/arm32/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -37,4 +37,50 @@
 #define LIBC_arm32_ATOMIC_H_
 
+#define LIBC_ARCH_ATOMIC_H_
+#define CAS 
+
+#include <atomicdflt.h>
+#include <bool.h>
+#include <sys/types.h>
+
+extern uintptr_t *ras_page;
+
+static inline bool cas(atomic_t *val, long ov, long nv)
+{
+	long ret = 0;
+
+	/*
+	 * The following instructions between labels 1 and 2 constitute a
+	 * Restartable Atomic Seqeunce. Should the sequence be non-atomic,
+	 * the kernel will restart it.
+	 */
+	asm volatile (
+		"1:\n"
+		"	adr %[ret], 1b\n"
+		"	str %[ret], %[rp0]\n"
+		"	adr %[ret], 2f\n"
+		"	str %[ret], %[rp1]\n"
+		"	ldr %[ret], %[addr]\n"
+		"	cmp %[ret], %[ov]\n"
+		"	streq %[nv], %[addr]\n"
+		"2:\n"
+		"	moveq %[ret], #1\n"
+		"	movne %[ret], #0\n"
+		: [ret] "+&r" (ret),
+		  [rp0] "=m" (ras_page[0]),
+		  [rp1] "=m" (ras_page[1]),
+		  [addr] "+m" (val->count)
+		: [ov] "r" (ov),
+		  [nv] "r" (nv)
+		: "memory"
+	);
+
+	ras_page[0] = 0;
+	asm volatile ("" ::: "memory");	
+	ras_page[1] = 0xffffffff;
+
+	return (bool) ret;
+}
+
 /** Atomic addition.
  *
@@ -46,20 +92,31 @@
 static inline long atomic_add(atomic_t *val, int i)
 {
-	int ret;
-	volatile long * mem = &(val->count);
+	long ret = 0;
 
+	/*
+	 * The following instructions between labels 1 and 2 constitute a
+	 * Restartable Atomic Seqeunce. Should the sequence be non-atomic,
+	 * the kernel will restart it.
+	 */
 	asm volatile (
-	"1:\n"
-		"ldr r2, [%1]\n"
-		"add r3, r2, %2\n"
-		"str r3, %0\n"
-		"swp r3, r3, [%1]\n"
-		"cmp r3, r2\n"
-		"bne 1b\n"
+		"1:\n"
+		"	adr %[ret], 1b\n"
+		"	str %[ret], %[rp0]\n"
+		"	adr %[ret], 2f\n"
+		"	str %[ret], %[rp1]\n"
+		"	ldr %[ret], %[addr]\n"
+		"	add %[ret], %[ret], %[imm]\n"
+		"	str %[ret], %[addr]\n"
+		"2:\n"
+		: [ret] "+&r" (ret),
+		  [rp0] "=m" (ras_page[0]),
+		  [rp1] "=m" (ras_page[1]),
+		  [addr] "+m" (val->count)
+		: [imm] "r" (i)
+	);
 
-		: "=m" (ret)
-		: "r" (mem), "r" (i)
-		: "r3", "r2"
-	);
+	ras_page[0] = 0;
+	asm volatile ("" ::: "memory");	
+	ras_page[1] = 0xffffffff;
 
 	return ret;
Index: uspace/lib/libc/arch/arm32/src/entry.s
===================================================================
--- uspace/lib/libc/arch/arm32/src/entry.s	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/arm32/src/entry.s	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,6 +36,11 @@
 #
 # r1 contains the PCB pointer
+# r2 contains the RAS page address
 #
 __entry:
+	# Store the RAS page address into the ras_page variable
+	ldr r0, =ras_page
+	str r2, [r0]
+
 	# Pass pcb_ptr to __main as the first argument (in r0)
 	mov r0, r1
@@ -43,2 +48,9 @@
 
 	bl __exit
+
+.data
+
+.global ras_page
+ras_page:
+	.long 0
+
Index: uspace/lib/libc/arch/arm32/src/syscall.c
===================================================================
--- uspace/lib/libc/arch/arm32/src/syscall.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/arm32/src/syscall.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -60,6 +60,7 @@
 	register sysarg_t __arm_reg_r5 asm("r5") = p6;
 	register sysarg_t __arm_reg_r6 asm("r6") = id;
-
-	asm volatile ( "swi"
+	
+	asm volatile (
+		"swi 0"
 		: "=r" (__arm_reg_r0)
 		: "r" (__arm_reg_r0),
@@ -71,5 +72,5 @@
 		  "r" (__arm_reg_r6)
 	);
-
+	
 	return __arm_reg_r0;
 }
Index: uspace/lib/libc/arch/ia32/Makefile.inc
===================================================================
--- uspace/lib/libc/arch/ia32/Makefile.inc	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/ia32/Makefile.inc	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -39,4 +39,5 @@
 	arch/$(UARCH)/src/setjmp.S
 
+GCC_CFLAGS += -march=pentium
 LFLAGS += -N
 
Index: uspace/lib/libc/arch/ia32/include/atomic.h
===================================================================
--- uspace/lib/libc/arch/ia32/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/ia32/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -35,4 +35,8 @@
 #ifndef LIBC_ia32_ATOMIC_H_
 #define LIBC_ia32_ATOMIC_H_
+
+#define LIBC_ARCH_ATOMIC_H_
+
+#include <atomicdflt.h>
 
 static inline void atomic_inc(atomic_t *val) {
Index: uspace/lib/libc/arch/ia64/include/atomic.h
===================================================================
--- uspace/lib/libc/arch/ia64/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/ia64/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -35,4 +35,8 @@
 #ifndef LIBC_ia64_ATOMIC_H_
 #define LIBC_ia64_ATOMIC_H_
+
+#define LIBC_ARCH_ATOMIC_H_
+
+#include <atomicdflt.h>
 
 static inline void atomic_inc(atomic_t *val)
Index: uspace/lib/libc/arch/mips32/include/atomic.h
===================================================================
--- uspace/lib/libc/arch/mips32/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/mips32/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,4 +36,8 @@
 #ifndef LIBC_mips32_ATOMIC_H_
 #define LIBC_mips32_ATOMIC_H_
+
+#define LIBC_ARCH_ATOMIC_H_
+
+#include <atomicdflt.h>
 
 #define atomic_inc(x)	((void) atomic_add(x, 1))
Index: uspace/lib/libc/arch/ppc32/include/atomic.h
===================================================================
--- uspace/lib/libc/arch/ppc32/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/ppc32/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -35,4 +35,8 @@
 #ifndef LIBC_ppc32_ATOMIC_H_
 #define LIBC_ppc32_ATOMIC_H_
+
+#define LIBC_ARCH_ATOMIC_H_
+
+#include <atomicdflt.h>
 
 static inline void atomic_inc(atomic_t *val)
Index: uspace/lib/libc/arch/sparc64/include/atomic.h
===================================================================
--- uspace/lib/libc/arch/sparc64/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/arch/sparc64/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,4 +36,7 @@
 #define LIBC_sparc64_ATOMIC_H_
 
+#define LIBC_ARCH_ATOMIC_H_
+
+#include <atomicdflt.h>
 #include <sys/types.h>
 
Index: uspace/lib/libc/generic/async.c
===================================================================
--- uspace/lib/libc/generic/async.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/async.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -392,4 +392,18 @@
 	/* If nothing in queue, wait until something arrives */
 	while (list_empty(&conn->msg_queue)) {
+		if (conn->close_callid) {
+			/*
+			 * Handle the case when the connection was already
+			 * closed by the client but the server did not notice
+			 * the first IPC_M_PHONE_HUNGUP call and continues to
+			 * call async_get_call_timeout(). Repeat
+			 * IPC_M_PHONE_HUNGUP until the caller notices. 
+			 */
+			memset(call, 0, sizeof(ipc_call_t));
+			IPC_SET_METHOD(*call, IPC_M_PHONE_HUNGUP);
+			futex_up(&async_futex);
+			return conn->close_callid;
+		}
+
 		if (usecs)
 			async_insert_timeout(&conn->wdata);
@@ -528,5 +542,5 @@
 	list_initialize(&conn->msg_queue);
 	conn->callid = callid;
-	conn->close_callid = false;
+	conn->close_callid = 0;
 	
 	if (call)
@@ -1331,4 +1345,97 @@
 }
 
+/** Wrapper for receiving blobs via the async_data_write_*
+ *
+ * This wrapper only makes it more comfortable to use async_data_write_*
+ * functions to receive blobs.
+ *
+ * @param blob     Pointer to data pointer (which should be later disposed
+ *                 by free()). If the operation fails, the pointer is not
+ *                 touched.
+ * @param max_size Maximum size (in bytes) of the blob to receive. 0 means
+ *                 no limit.
+ * @param received If not NULL, the size of the received data is stored here.
+ *
+ * @return Zero on success or a value from @ref errno.h on failure.
+ *
+ */
+int async_data_blob_receive(char **blob, const size_t max_size, size_t *received)
+{
+	ipc_callid_t callid;
+	size_t size;
+	if (!async_data_write_receive(&callid, &size)) {
+		ipc_answer_0(callid, EINVAL);
+		return EINVAL;
+	}
+	
+	if ((max_size > 0) && (size > max_size)) {
+		ipc_answer_0(callid, EINVAL);
+		return EINVAL;
+	}
+	
+	char *data = (char *) malloc(size);
+	if (data == NULL) {
+		ipc_answer_0(callid, ENOMEM);
+		return ENOMEM;
+	}
+	
+	int rc = async_data_write_finalize(callid, data, size);
+	if (rc != EOK) {
+		free(data);
+		return rc;
+	}
+	
+	*blob = data;
+	if (received != NULL)
+		*received = size;
+	
+	return EOK;
+}
+
+/** Wrapper for receiving strings via the async_data_write_*
+ *
+ * This wrapper only makes it more comfortable to use async_data_write_*
+ * functions to receive strings.
+ *
+ * @param str      Pointer to string pointer (which should be later disposed
+ *                 by free()). If the operation fails, the pointer is not
+ *                 touched.
+ * @param max_size Maximum size (in bytes) of the string to receive. 0 means
+ *                 no limit.
+ *
+ * @return Zero on success or a value from @ref errno.h on failure.
+ *
+ */
+int async_data_string_receive(char **str, const size_t max_size)
+{
+	ipc_callid_t callid;
+	size_t size;
+	if (!async_data_write_receive(&callid, &size)) {
+		ipc_answer_0(callid, EINVAL);
+		return EINVAL;
+	}
+	
+	if ((max_size > 0) && (size > max_size)) {
+		ipc_answer_0(callid, EINVAL);
+		return EINVAL;
+	}
+	
+	char *data = (char *) malloc(size + 1);
+	if (data == NULL) {
+		ipc_answer_0(callid, ENOMEM);
+		return ENOMEM;
+	}
+	
+	int rc = async_data_write_finalize(callid, data, size);
+	if (rc != EOK) {
+		free(data);
+		return rc;
+	}
+	
+	data[size] = 0;
+	*str = data;
+	return EOK;
+}
+
 /** @}
  */
Index: uspace/lib/libc/generic/clipboard.c
===================================================================
--- uspace/lib/libc/generic/clipboard.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/lib/libc/generic/clipboard.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,185 @@
+/*
+ * Copyright (c) 2009 Jiri Svoboda
+ * 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 libc
+ * @{
+ */
+/** @file
+ * @brief System clipboard API.
+ *
+ * The clipboard data is managed by the clipboard service and it is shared by
+ * the entire system.
+ *
+ */
+
+#include <clipboard.h>
+#include <ipc/services.h>
+#include <ipc/clipboard.h>
+#include <async.h>
+#include <string.h>
+#include <errno.h>
+#include <malloc.h>
+
+static int clip_phone = -1;
+
+/** Connect to clipboard server
+ *
+ */
+static void clip_connect(void)
+{
+	while (clip_phone < 0)
+		clip_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_CLIPBOARD, 0, 0);
+}
+
+/** Copy string to clipboard.
+ *
+ * Sets the clipboard contents to @a str. Passing an empty string or NULL
+ * makes the clipboard empty.
+ *
+ * @param str String to put to clipboard or NULL.
+ *
+ * @return Zero on success or negative error code.
+ *
+ */
+int clipboard_put_str(const char *str)
+{
+	size_t size = str_size(str);
+	
+	if (size == 0) {
+		async_serialize_start();
+		clip_connect();
+		
+		ipcarg_t rc = async_req_1_0(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_NONE);
+		
+		async_serialize_end();
+		
+		return (int) rc;
+	} else {
+		async_serialize_start();
+		clip_connect();
+		
+		aid_t req = async_send_1(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_BLOB, NULL);
+		ipcarg_t rc = async_data_write_start(clip_phone, (void *) str, size);
+		if (rc != EOK) {
+			ipcarg_t rc_orig;
+			async_wait_for(req, &rc_orig);
+			async_serialize_end();
+			if (rc_orig == EOK)
+				return (int) rc;
+			else
+				return (int) rc_orig;
+		}
+		
+		async_wait_for(req, &rc);
+		async_serialize_end();
+		
+		return (int) rc;
+	}
+}
+
+/** Get a copy of clipboard contents.
+ *
+ * Returns a new string that can be deallocated with free().
+ *
+ * @param str Here pointer to the newly allocated string is stored.
+ *
+ * @return Zero on success or negative error code.
+ *
+ */
+int clipboard_get_str(char **str)
+{
+	/* Loop until clipboard read succesful */
+	while (true) {
+		async_serialize_start();
+		clip_connect();
+		
+		ipcarg_t size;
+		ipcarg_t tag;
+		ipcarg_t rc = async_req_0_2(clip_phone, CLIPBOARD_CONTENT, &size, &tag);
+		
+		async_serialize_end();
+		
+		if (rc != EOK)
+			return (int) rc;
+		
+		char *sbuf;
+		
+		switch (tag) {
+		case CLIPBOARD_TAG_NONE:
+			sbuf = malloc(1);
+			if (sbuf == NULL)
+				return ENOMEM;
+			
+			sbuf[0] = 0;
+			*str = sbuf;
+			return EOK;
+		case CLIPBOARD_TAG_BLOB:
+			sbuf = malloc(size + 1);
+			if (sbuf == NULL)
+				return ENOMEM;
+			
+			async_serialize_start();
+			
+			aid_t req = async_send_1(clip_phone, CLIPBOARD_GET_DATA, tag, NULL);
+			rc = async_data_read_start(clip_phone, (void *) sbuf, size);
+			if (rc == EOVERFLOW) {
+				/*
+				 * The data in the clipboard has changed since
+				 * the last call of CLIPBOARD_CONTENT
+				 */
+				async_serialize_end();
+				break;
+			}
+			
+			if (rc != EOK) {
+				ipcarg_t rc_orig;
+				async_wait_for(req, &rc_orig);
+				async_serialize_end();
+				if (rc_orig == EOK)
+					return (int) rc;
+				else
+					return (int) rc_orig;
+			}
+			
+			async_wait_for(req, &rc);
+			async_serialize_end();
+			
+			if (rc == EOK) {
+				sbuf[size] = 0;
+				*str = sbuf;
+			}
+			
+			return rc;
+		default:
+			return EINVAL;
+		}
+	}
+}
+
+/** @}
+ */
Index: uspace/lib/libc/generic/devmap.c
===================================================================
--- uspace/lib/libc/generic/devmap.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/devmap.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -35,4 +35,6 @@
 #include <async.h>
 #include <errno.h>
+#include <malloc.h>
+#include <bool.h>
 
 static int devmap_phone_driver = -1;
@@ -105,6 +107,5 @@
 	aid_t req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
 	
-	ipcarg_t retval = async_data_write_start(phone, name, str_size(name) + 1);
-	
+	ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
 	if (retval != EOK) {
 		async_wait_for(req, NULL);
@@ -126,9 +127,10 @@
 /** Register new device.
  *
- * @param name   Device name.
- * @param handle Output: Handle to the created instance of device.
+ * @param namespace Namespace name.
+ * @param fqdn      Fully qualified device name.
+ * @param handle    Output: Handle to the created instance of device.
  *
  */
-int devmap_device_register(const char *name, dev_handle_t *handle)
+int devmap_device_register(const char *fqdn, dev_handle_t *handle)
 {
 	int phone = devmap_get_phone(DEVMAP_DRIVER, IPC_FLAG_BLOCKING);
@@ -143,6 +145,5 @@
 	    &answer);
 	
-	ipcarg_t retval = async_data_write_start(phone, name, str_size(name) + 1);
-	
+	ipcarg_t retval = async_data_write_start(phone, fqdn, str_size(fqdn));
 	if (retval != EOK) {
 		async_wait_for(req, NULL);
@@ -167,5 +168,5 @@
 }
 
-int devmap_device_get_handle(const char *name, dev_handle_t *handle, unsigned int flags)
+int devmap_device_get_handle(const char *fqdn, dev_handle_t *handle, unsigned int flags)
 {
 	int phone = devmap_get_phone(DEVMAP_CLIENT, flags);
@@ -180,6 +181,5 @@
 	    &answer);
 	
-	ipcarg_t retval = async_data_write_start(phone, name, str_size(name) + 1);
-	
+	ipcarg_t retval = async_data_write_start(phone, fqdn, str_size(fqdn));
 	if (retval != EOK) {
 		async_wait_for(req, NULL);
@@ -202,4 +202,55 @@
 	
 	return retval;
+}
+
+int devmap_namespace_get_handle(const char *name, dev_handle_t *handle, unsigned int flags)
+{
+	int phone = devmap_get_phone(DEVMAP_CLIENT, flags);
+	
+	if (phone < 0)
+		return phone;
+	
+	async_serialize_start();
+	
+	ipc_call_t answer;
+	aid_t req = async_send_2(phone, DEVMAP_NAMESPACE_GET_HANDLE, flags, 0,
+	    &answer);
+	
+	ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
+	if (retval != EOK) {
+		async_wait_for(req, NULL);
+		async_serialize_end();
+		return retval;
+	}
+	
+	async_wait_for(req, &retval);
+	
+	async_serialize_end();
+	
+	if (retval != EOK) {
+		if (handle != NULL)
+			*handle = (dev_handle_t) -1;
+		return retval;
+	}
+	
+	if (handle != NULL)
+		*handle = (dev_handle_t) IPC_GET_ARG1(answer);
+	
+	return retval;
+}
+
+devmap_handle_type_t devmap_handle_probe(dev_handle_t handle)
+{
+	int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
+	
+	if (phone < 0)
+		return phone;
+	
+	ipcarg_t type;
+	int retval = async_req_1_1(phone, DEVMAP_HANDLE_PROBE, handle, &type);
+	if (retval != EOK)
+		return DEV_HANDLE_NONE;
+	
+	return (devmap_handle_type_t) type;
 }
 
@@ -227,5 +278,5 @@
 	
 	ipcarg_t null_id;
-	int retval = async_req_0_1(phone, DEVMAP_DEVICE_NULL_CREATE, &null_id);
+	int retval = async_req_0_1(phone, DEVMAP_NULL_CREATE, &null_id);
 	if (retval != EOK)
 		return -1;
@@ -241,16 +292,11 @@
 		return;
 	
-	async_req_1_0(phone, DEVMAP_DEVICE_NULL_DESTROY, (ipcarg_t) null_id);
-}
-
-ipcarg_t devmap_device_get_count(void)
-{
-	int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
-	
-	if (phone < 0)
-		return 0;
-	
+	async_req_1_0(phone, DEVMAP_NULL_DESTROY, (ipcarg_t) null_id);
+}
+
+static size_t devmap_count_namespaces_internal(int phone)
+{
 	ipcarg_t count;
-	int retval = async_req_0_1(phone, DEVMAP_DEVICE_GET_COUNT, &count);
+	int retval = async_req_0_1(phone, DEVMAP_GET_NAMESPACE_COUNT, &count);
 	if (retval != EOK)
 		return 0;
@@ -259,31 +305,135 @@
 }
 
-ipcarg_t devmap_device_get_devices(ipcarg_t count, dev_desc_t *data)
-{
-	int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
-	
-	if (phone < 0)
-		return 0;
-	
-	async_serialize_start();
-	
-	ipc_call_t answer;
-	aid_t req = async_send_0(phone, DEVMAP_DEVICE_GET_DEVICES, &answer);
-	
-	ipcarg_t retval = async_data_read_start(phone, data, count * sizeof(dev_desc_t));
-	
-	if (retval != EOK) {
-		async_wait_for(req, NULL);
-		async_serialize_end();
-		return 0;
-	}
-	
-	async_wait_for(req, &retval);
-	
-	async_serialize_end();
-	
+static size_t devmap_count_devices_internal(int phone, dev_handle_t ns_handle)
+{
+	ipcarg_t count;
+	int retval = async_req_1_1(phone, DEVMAP_GET_DEVICE_COUNT, ns_handle, &count);
 	if (retval != EOK)
 		return 0;
 	
-	return IPC_GET_ARG1(answer);
-}
+	return count;
+}
+
+size_t devmap_count_namespaces(void)
+{
+	int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
+	
+	if (phone < 0)
+		return 0;
+	
+	return devmap_count_namespaces_internal(phone);
+}
+
+size_t devmap_count_devices(dev_handle_t ns_handle)
+{
+	int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
+	
+	if (phone < 0)
+		return 0;
+	
+	return devmap_count_devices_internal(phone, ns_handle);
+}
+
+size_t devmap_get_namespaces(dev_desc_t **data)
+{
+	int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
+	
+	if (phone < 0)
+		return 0;
+	
+	/* Loop until namespaces read succesful */
+	while (true) {
+		size_t count = devmap_count_namespaces_internal(phone);
+		if (count == 0)
+			return 0;
+		
+		dev_desc_t *devs = (dev_desc_t *) calloc(count, sizeof(dev_desc_t));
+		if (devs == NULL)
+			return 0;
+		
+		async_serialize_start();
+		
+		ipc_call_t answer;
+		aid_t req = async_send_0(phone, DEVMAP_GET_NAMESPACES, &answer);
+		
+		int rc = async_data_read_start(phone, devs, count * sizeof(dev_desc_t));
+		if (rc == EOVERFLOW) {
+			/*
+			 * Number of namespaces has changed since
+			 * the last call of DEVMAP_DEVICE_GET_NAMESPACE_COUNT
+			 */
+			async_serialize_end();
+			free(devs);
+			continue;
+		}
+		
+		if (rc != EOK) {
+			async_wait_for(req, NULL);
+			async_serialize_end();
+			free(devs);
+			return 0;
+		}
+		
+		ipcarg_t retval;
+		async_wait_for(req, &retval);
+		async_serialize_end();
+		
+		if (retval != EOK)
+			return 0;
+		
+		*data = devs;
+		return count;
+	}
+}
+
+size_t devmap_get_devices(dev_handle_t ns_handle, dev_desc_t **data)
+{
+	int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
+	
+	if (phone < 0)
+		return 0;
+	
+	/* Loop until namespaces read succesful */
+	while (true) {
+		size_t count = devmap_count_devices_internal(phone, ns_handle);
+		if (count == 0)
+			return 0;
+		
+		dev_desc_t *devs = (dev_desc_t *) calloc(count, sizeof(dev_desc_t));
+		if (devs == NULL)
+			return 0;
+		
+		async_serialize_start();
+		
+		ipc_call_t answer;
+		aid_t req = async_send_1(phone, DEVMAP_GET_DEVICES, ns_handle, &answer);
+		
+		int rc = async_data_read_start(phone, devs, count * sizeof(dev_desc_t));
+		if (rc == EOVERFLOW) {
+			/*
+			 * Number of devices has changed since
+			 * the last call of DEVMAP_DEVICE_GET_DEVICE_COUNT
+			 */
+			async_serialize_end();
+			free(devs);
+			continue;
+		}
+		
+		if (rc != EOK) {
+			async_wait_for(req, NULL);
+			async_serialize_end();
+			free(devs);
+			return 0;
+		}
+		
+		ipcarg_t retval;
+		async_wait_for(req, &retval);
+		async_serialize_end();
+		
+		if (retval != EOK)
+			return 0;
+		
+		*data = devs;
+		return count;
+	}
+}
Index: pace/lib/libc/generic/fibril_sync.c
===================================================================
--- uspace/lib/libc/generic/fibril_sync.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,308 +1,0 @@
-/*
- * Copyright (c) 2009 Jakub Jermar 
- * 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 libc
- * @{
- */
-/** @file
- */
-
-#include <fibril_sync.h>
-#include <fibril.h>
-#include <async.h>
-#include <async_priv.h>
-#include <adt/list.h>
-#include <futex.h>
-#include <sys/time.h>
-#include <errno.h>
-#include <assert.h>
-
-static void optimize_execution_power(void)
-{
-	/*
-	 * When waking up a worker fibril previously blocked in fibril
-	 * synchronization, chances are that there is an idle manager fibril
-	 * waiting for IPC, that could start executing the awakened worker
-	 * fibril right away. We try to detect this and bring the manager
-	 * fibril back to fruitful work.
-	 */
-	if (atomic_get(&threads_in_ipc_wait) > 0)
-		ipc_poke();
-}
-
-void fibril_mutex_initialize(fibril_mutex_t *fm)
-{
-	fm->counter = 1;
-	list_initialize(&fm->waiters);
-}
-
-void fibril_mutex_lock(fibril_mutex_t *fm)
-{
-	futex_down(&async_futex);
-	if (fm->counter-- <= 0) {
-		awaiter_t wdata;
-
-		wdata.fid = fibril_get_id();
-		wdata.active = false;
-		wdata.wu_event.inlist = true;
-		link_initialize(&wdata.wu_event.link);
-		list_append(&wdata.wu_event.link, &fm->waiters);
-		fibril_switch(FIBRIL_TO_MANAGER);
-	} else {
-		futex_up(&async_futex);
-	}
-}
-
-bool fibril_mutex_trylock(fibril_mutex_t *fm)
-{
-	bool locked = false;
-	
-	futex_down(&async_futex);
-	if (fm->counter > 0) {
-		fm->counter--;
-		locked = true;
-	}
-	futex_up(&async_futex);
-	
-	return locked;
-}
-
-static void _fibril_mutex_unlock_unsafe(fibril_mutex_t *fm)
-{
-	assert(fm->counter <= 0);
-	if (fm->counter++ < 0) {
-		link_t *tmp;
-		awaiter_t *wdp;
-	
-		assert(!list_empty(&fm->waiters));
-		tmp = fm->waiters.next;
-		wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
-		wdp->active = true;
-		wdp->wu_event.inlist = false;
-		list_remove(&wdp->wu_event.link);
-		fibril_add_ready(wdp->fid);
-		optimize_execution_power();
-	}
-}
-
-void fibril_mutex_unlock(fibril_mutex_t *fm)
-{
-	futex_down(&async_futex);
-	_fibril_mutex_unlock_unsafe(fm);
-	futex_up(&async_futex);
-}
-
-void fibril_rwlock_initialize(fibril_rwlock_t *frw)
-{
-	frw->writers = 0;
-	frw->readers = 0;
-	list_initialize(&frw->waiters);
-}
-
-void fibril_rwlock_read_lock(fibril_rwlock_t *frw)
-{
-	futex_down(&async_futex);
-	if (frw->writers) {
-		fibril_t *f = (fibril_t *) fibril_get_id();
-		awaiter_t wdata;
-
-		wdata.fid = (fid_t) f;
-		wdata.active = false;
-		wdata.wu_event.inlist = true;
-		link_initialize(&wdata.wu_event.link);
-		f->flags &= ~FIBRIL_WRITER;
-		list_append(&wdata.wu_event.link, &frw->waiters);
-		fibril_switch(FIBRIL_TO_MANAGER);
-	} else {
-		frw->readers++;
-		futex_up(&async_futex);
-	}
-}
-
-void fibril_rwlock_write_lock(fibril_rwlock_t *frw)
-{
-	futex_down(&async_futex);
-	if (frw->writers || frw->readers) {
-		fibril_t *f = (fibril_t *) fibril_get_id();
-		awaiter_t wdata;
-
-		wdata.fid = (fid_t) f;
-		wdata.active = false;
-		wdata.wu_event.inlist = true;
-		link_initialize(&wdata.wu_event.link);
-		f->flags |= FIBRIL_WRITER;
-		list_append(&wdata.wu_event.link, &frw->waiters);
-		fibril_switch(FIBRIL_TO_MANAGER);
-	} else {
-		frw->writers++;
-		futex_up(&async_futex);
-	}
-}
-
-static void _fibril_rwlock_common_unlock(fibril_rwlock_t *frw)
-{
-	futex_down(&async_futex);
-	assert(frw->readers || (frw->writers == 1));
-	if (frw->readers) {
-		if (--frw->readers)
-			goto out;
-	} else {
-		frw->writers--;
-	}
-	
-	assert(!frw->readers && !frw->writers);
-	
-	while (!list_empty(&frw->waiters)) {
-		link_t *tmp = frw->waiters.next;
-		awaiter_t *wdp;
-		fibril_t *f;
-		
-		wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
-		f = (fibril_t *) wdp->fid;
-		
-		if (f->flags & FIBRIL_WRITER) {
-			if (frw->readers)
-				break;
-			wdp->active = true;
-			wdp->wu_event.inlist = false;
-			list_remove(&wdp->wu_event.link);
-			fibril_add_ready(wdp->fid);
-			frw->writers++;
-			optimize_execution_power();
-			break;
-		} else {
-			wdp->active = true;
-			wdp->wu_event.inlist = false;
-			list_remove(&wdp->wu_event.link);
-			fibril_add_ready(wdp->fid);
-			frw->readers++;
-			optimize_execution_power();
-		}
-	}
-out:
-	futex_up(&async_futex);
-}
-
-void fibril_rwlock_read_unlock(fibril_rwlock_t *frw)
-{
-	_fibril_rwlock_common_unlock(frw);
-}
-
-void fibril_rwlock_write_unlock(fibril_rwlock_t *frw)
-{
-	_fibril_rwlock_common_unlock(frw);
-}
-
-void fibril_condvar_initialize(fibril_condvar_t *fcv)
-{
-	list_initialize(&fcv->waiters);
-}
-
-int
-fibril_condvar_wait_timeout(fibril_condvar_t *fcv, fibril_mutex_t *fm,
-    suseconds_t timeout)
-{
-	awaiter_t wdata;
-
-	if (timeout < 0)
-		return ETIMEOUT;
-
-	wdata.fid = fibril_get_id();
-	wdata.active = false;
-	
-	wdata.to_event.inlist = timeout > 0;
-	wdata.to_event.occurred = false;
-	link_initialize(&wdata.to_event.link);
-
-	wdata.wu_event.inlist = true;
-	link_initialize(&wdata.wu_event.link);
-
-	futex_down(&async_futex);
-	if (timeout) {
-		gettimeofday(&wdata.to_event.expires, NULL);
-		tv_add(&wdata.to_event.expires, timeout);
-		async_insert_timeout(&wdata);
-	}
-	list_append(&wdata.wu_event.link, &fcv->waiters);
-	_fibril_mutex_unlock_unsafe(fm);
-	fibril_switch(FIBRIL_TO_MANAGER);
-	fibril_mutex_lock(fm);
-
-	/* async_futex not held after fibril_switch() */
-	futex_down(&async_futex);
-	if (wdata.to_event.inlist)
-		list_remove(&wdata.to_event.link);
-	if (wdata.wu_event.inlist)
-		list_remove(&wdata.wu_event.link);
-	futex_up(&async_futex);
-	
-	return wdata.to_event.occurred ? ETIMEOUT : EOK;
-}
-
-void fibril_condvar_wait(fibril_condvar_t *fcv, fibril_mutex_t *fm)
-{
-	int rc;
-
-	rc = fibril_condvar_wait_timeout(fcv, fm, 0);
-	assert(rc == EOK);
-}
-
-static void _fibril_condvar_wakeup_common(fibril_condvar_t *fcv, bool once)
-{
-	link_t *tmp;
-	awaiter_t *wdp;
-
-	futex_down(&async_futex);
-	while (!list_empty(&fcv->waiters)) {
-		tmp = fcv->waiters.next;
-		wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
-		list_remove(&wdp->wu_event.link);
-		wdp->wu_event.inlist = false;
-		if (!wdp->active) {
-			wdp->active = true;
-			fibril_add_ready(wdp->fid);
-			optimize_execution_power();
-			if (once)
-				break;
-		}
-	}
-	futex_up(&async_futex);
-}
-
-void fibril_condvar_signal(fibril_condvar_t *fcv)
-{
-	_fibril_condvar_wakeup_common(fcv, true);
-}
-
-void fibril_condvar_broadcast(fibril_condvar_t *fcv)
-{
-	_fibril_condvar_wakeup_common(fcv, false);
-}
-
-/** @}
- */
Index: uspace/lib/libc/generic/fibril_synch.c
===================================================================
--- uspace/lib/libc/generic/fibril_synch.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/lib/libc/generic/fibril_synch.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,308 @@
+/*
+ * Copyright (c) 2009 Jakub Jermar 
+ * 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 libc
+ * @{
+ */
+/** @file
+ */
+
+#include <fibril_synch.h>
+#include <fibril.h>
+#include <async.h>
+#include <async_priv.h>
+#include <adt/list.h>
+#include <futex.h>
+#include <sys/time.h>
+#include <errno.h>
+#include <assert.h>
+
+static void optimize_execution_power(void)
+{
+	/*
+	 * When waking up a worker fibril previously blocked in fibril
+	 * synchronization, chances are that there is an idle manager fibril
+	 * waiting for IPC, that could start executing the awakened worker
+	 * fibril right away. We try to detect this and bring the manager
+	 * fibril back to fruitful work.
+	 */
+	if (atomic_get(&threads_in_ipc_wait) > 0)
+		ipc_poke();
+}
+
+void fibril_mutex_initialize(fibril_mutex_t *fm)
+{
+	fm->counter = 1;
+	list_initialize(&fm->waiters);
+}
+
+void fibril_mutex_lock(fibril_mutex_t *fm)
+{
+	futex_down(&async_futex);
+	if (fm->counter-- <= 0) {
+		awaiter_t wdata;
+
+		wdata.fid = fibril_get_id();
+		wdata.active = false;
+		wdata.wu_event.inlist = true;
+		link_initialize(&wdata.wu_event.link);
+		list_append(&wdata.wu_event.link, &fm->waiters);
+		fibril_switch(FIBRIL_TO_MANAGER);
+	} else {
+		futex_up(&async_futex);
+	}
+}
+
+bool fibril_mutex_trylock(fibril_mutex_t *fm)
+{
+	bool locked = false;
+	
+	futex_down(&async_futex);
+	if (fm->counter > 0) {
+		fm->counter--;
+		locked = true;
+	}
+	futex_up(&async_futex);
+	
+	return locked;
+}
+
+static void _fibril_mutex_unlock_unsafe(fibril_mutex_t *fm)
+{
+	assert(fm->counter <= 0);
+	if (fm->counter++ < 0) {
+		link_t *tmp;
+		awaiter_t *wdp;
+	
+		assert(!list_empty(&fm->waiters));
+		tmp = fm->waiters.next;
+		wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
+		wdp->active = true;
+		wdp->wu_event.inlist = false;
+		list_remove(&wdp->wu_event.link);
+		fibril_add_ready(wdp->fid);
+		optimize_execution_power();
+	}
+}
+
+void fibril_mutex_unlock(fibril_mutex_t *fm)
+{
+	futex_down(&async_futex);
+	_fibril_mutex_unlock_unsafe(fm);
+	futex_up(&async_futex);
+}
+
+void fibril_rwlock_initialize(fibril_rwlock_t *frw)
+{
+	frw->writers = 0;
+	frw->readers = 0;
+	list_initialize(&frw->waiters);
+}
+
+void fibril_rwlock_read_lock(fibril_rwlock_t *frw)
+{
+	futex_down(&async_futex);
+	if (frw->writers) {
+		fibril_t *f = (fibril_t *) fibril_get_id();
+		awaiter_t wdata;
+
+		wdata.fid = (fid_t) f;
+		wdata.active = false;
+		wdata.wu_event.inlist = true;
+		link_initialize(&wdata.wu_event.link);
+		f->flags &= ~FIBRIL_WRITER;
+		list_append(&wdata.wu_event.link, &frw->waiters);
+		fibril_switch(FIBRIL_TO_MANAGER);
+	} else {
+		frw->readers++;
+		futex_up(&async_futex);
+	}
+}
+
+void fibril_rwlock_write_lock(fibril_rwlock_t *frw)
+{
+	futex_down(&async_futex);
+	if (frw->writers || frw->readers) {
+		fibril_t *f = (fibril_t *) fibril_get_id();
+		awaiter_t wdata;
+
+		wdata.fid = (fid_t) f;
+		wdata.active = false;
+		wdata.wu_event.inlist = true;
+		link_initialize(&wdata.wu_event.link);
+		f->flags |= FIBRIL_WRITER;
+		list_append(&wdata.wu_event.link, &frw->waiters);
+		fibril_switch(FIBRIL_TO_MANAGER);
+	} else {
+		frw->writers++;
+		futex_up(&async_futex);
+	}
+}
+
+static void _fibril_rwlock_common_unlock(fibril_rwlock_t *frw)
+{
+	futex_down(&async_futex);
+	assert(frw->readers || (frw->writers == 1));
+	if (frw->readers) {
+		if (--frw->readers)
+			goto out;
+	} else {
+		frw->writers--;
+	}
+	
+	assert(!frw->readers && !frw->writers);
+	
+	while (!list_empty(&frw->waiters)) {
+		link_t *tmp = frw->waiters.next;
+		awaiter_t *wdp;
+		fibril_t *f;
+		
+		wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
+		f = (fibril_t *) wdp->fid;
+		
+		if (f->flags & FIBRIL_WRITER) {
+			if (frw->readers)
+				break;
+			wdp->active = true;
+			wdp->wu_event.inlist = false;
+			list_remove(&wdp->wu_event.link);
+			fibril_add_ready(wdp->fid);
+			frw->writers++;
+			optimize_execution_power();
+			break;
+		} else {
+			wdp->active = true;
+			wdp->wu_event.inlist = false;
+			list_remove(&wdp->wu_event.link);
+			fibril_add_ready(wdp->fid);
+			frw->readers++;
+			optimize_execution_power();
+		}
+	}
+out:
+	futex_up(&async_futex);
+}
+
+void fibril_rwlock_read_unlock(fibril_rwlock_t *frw)
+{
+	_fibril_rwlock_common_unlock(frw);
+}
+
+void fibril_rwlock_write_unlock(fibril_rwlock_t *frw)
+{
+	_fibril_rwlock_common_unlock(frw);
+}
+
+void fibril_condvar_initialize(fibril_condvar_t *fcv)
+{
+	list_initialize(&fcv->waiters);
+}
+
+int
+fibril_condvar_wait_timeout(fibril_condvar_t *fcv, fibril_mutex_t *fm,
+    suseconds_t timeout)
+{
+	awaiter_t wdata;
+
+	if (timeout < 0)
+		return ETIMEOUT;
+
+	wdata.fid = fibril_get_id();
+	wdata.active = false;
+	
+	wdata.to_event.inlist = timeout > 0;
+	wdata.to_event.occurred = false;
+	link_initialize(&wdata.to_event.link);
+
+	wdata.wu_event.inlist = true;
+	link_initialize(&wdata.wu_event.link);
+
+	futex_down(&async_futex);
+	if (timeout) {
+		gettimeofday(&wdata.to_event.expires, NULL);
+		tv_add(&wdata.to_event.expires, timeout);
+		async_insert_timeout(&wdata);
+	}
+	list_append(&wdata.wu_event.link, &fcv->waiters);
+	_fibril_mutex_unlock_unsafe(fm);
+	fibril_switch(FIBRIL_TO_MANAGER);
+	fibril_mutex_lock(fm);
+
+	/* async_futex not held after fibril_switch() */
+	futex_down(&async_futex);
+	if (wdata.to_event.inlist)
+		list_remove(&wdata.to_event.link);
+	if (wdata.wu_event.inlist)
+		list_remove(&wdata.wu_event.link);
+	futex_up(&async_futex);
+	
+	return wdata.to_event.occurred ? ETIMEOUT : EOK;
+}
+
+void fibril_condvar_wait(fibril_condvar_t *fcv, fibril_mutex_t *fm)
+{
+	int rc;
+
+	rc = fibril_condvar_wait_timeout(fcv, fm, 0);
+	assert(rc == EOK);
+}
+
+static void _fibril_condvar_wakeup_common(fibril_condvar_t *fcv, bool once)
+{
+	link_t *tmp;
+	awaiter_t *wdp;
+
+	futex_down(&async_futex);
+	while (!list_empty(&fcv->waiters)) {
+		tmp = fcv->waiters.next;
+		wdp = list_get_instance(tmp, awaiter_t, wu_event.link);
+		list_remove(&wdp->wu_event.link);
+		wdp->wu_event.inlist = false;
+		if (!wdp->active) {
+			wdp->active = true;
+			fibril_add_ready(wdp->fid);
+			optimize_execution_power();
+			if (once)
+				break;
+		}
+	}
+	futex_up(&async_futex);
+}
+
+void fibril_condvar_signal(fibril_condvar_t *fcv)
+{
+	_fibril_condvar_wakeup_common(fcv, true);
+}
+
+void fibril_condvar_broadcast(fibril_condvar_t *fcv)
+{
+	_fibril_condvar_wakeup_common(fcv, false);
+}
+
+/** @}
+ */
Index: uspace/lib/libc/generic/futex.c
===================================================================
--- uspace/lib/libc/generic/futex.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/futex.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,49 +36,5 @@
 #include <atomic.h>
 #include <libc.h>
-#include <stdio.h>
 #include <sys/types.h>
-#include <kernel/synch/synch.h>
-
-/*
- * Note about race conditions.
- * Because of non-atomic nature of operations performed sequentially on the
- * futex counter and the futex wait queue, there is a race condition:
- *
- * (wq->missed_wakeups == 1) && (futex->count = 1)
- *
- * Scenario 1 (wait queue timeout vs. futex_up()):
- * 1. assume wq->missed_wakeups == 0 && futex->count == -1
- *    (ie. thread A sleeping, thread B in the critical section)
- * 2. A receives timeout and gets removed from the wait queue
- * 3. B wants to leave the critical section and calls futex_up()
- * 4. B thus changes futex->count from -1 to 0
- * 5. B has to call SYS_FUTEX_WAKEUP syscall to wake up the sleeping thread
- * 6. B finds the wait queue empty and changes wq->missed_wakeups from 0 to 1
- * 7. A fixes futex->count (i.e. the number of waiting threads) by changing it
- *    from 0 to 1
- *
- * Scenario 2 (conditional down operation vs. futex_up)
- * 1. assume wq->missed_wakeups == 0 && futex->count == 0
- *    (i.e. thread A is in the critical section)
- * 2. thread B performs futex_trydown() operation and changes futex->count from
- *    0 to -1
- *    B is now obliged to call SYS_FUTEX_SLEEP syscall
- * 3. A wants to leave the critical section and does futex_up()
- * 4. A thus changes futex->count from -1 to 0 and must call SYS_FUTEX_WAKEUP
- *    syscall
- * 5. B finds the wait queue empty and immediatelly aborts the conditional sleep
- * 6. No thread is queueing in the wait queue so wq->missed_wakeups changes from
- *    0 to 1
- * 6. B fixes futex->count (i.e. the number of waiting threads) by changing it
- *    from 0 to 1
- *
- * Both scenarios allow two threads to be in the critical section
- * simultaneously. One without kernel intervention and the other through
- * wq->missed_wakeups being 1.
- *
- * To mitigate this problem, futex_down_timeout() detects that the syscall
- * didn't sleep in the wait queue, fixes the futex counter and RETRIES the
- * whole operation again.
- */
 
 /** Initialize futex counter.
@@ -92,81 +48,28 @@
 }
 
-int futex_down(futex_t *futex)
-{
-	return futex_down_timeout(futex, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
-}
-
-int futex_trydown(futex_t *futex)
-{
-	return futex_down_timeout(futex, SYNCH_NO_TIMEOUT,
-	    SYNCH_FLAGS_NON_BLOCKING);
-}
-
 /** Try to down the futex.
  *
  * @param futex		Futex.
- * @param usec		Microseconds to wait. Zero value means sleep without
- * 			timeout.
- * @param flags		Select mode of operation. See comment for
- * 			waitq_sleep_timeout(). 
+ * @return		Non-zero if the futex was acquired.
+ * @return		Zero if the futex was not acquired.
+ */
+int futex_trydown(futex_t *futex)
+{
+	return cas(futex, 1, 0);
+}
+
+/** Down the futex.
  *
- * @return		ENOENT if there is no such virtual address. One of
- * 			ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED on success or
- * 			ESYNCH_TIMEOUT if the lock was not acquired because of
- * 			a timeout or ESYNCH_WOULD_BLOCK if the operation could
- * 			not be carried out atomically (if requested so).
+ * @param futex		Futex.
+ * @return		ENOENT if there is no such virtual address.
+ * @return		Zero in the uncontended case. 
+ * @return		Otherwise one of ESYNCH_OK_ATOMIC or ESYNCH_OK_BLOCKED.
  */
-int futex_down_timeout(futex_t *futex, uint32_t usec, int flags)
+int futex_down(futex_t *futex)
 {
-	int rc;
-	
-	while (atomic_predec(futex) < 0) {
-		rc = __SYSCALL3(SYS_FUTEX_SLEEP, (sysarg_t) &futex->count,
-		    (sysarg_t) usec, (sysarg_t) flags);
-		
-		switch (rc) {
-		case ESYNCH_OK_ATOMIC:
-			/*
-			 * Because of a race condition between timeout and
-			 * futex_up() and between conditional
-			 * futex_down_timeout() and futex_up(), we have to give
-			 * up and try again in this special case.
-			 */
-			atomic_inc(futex);
-			break;
+	if (atomic_predec(futex) < 0)
+		return __SYSCALL1(SYS_FUTEX_SLEEP, (sysarg_t) &futex->count);
 
-		case ESYNCH_TIMEOUT:
-			atomic_inc(futex);
-			return ESYNCH_TIMEOUT;
-			break;
-
-		case ESYNCH_WOULD_BLOCK:
-			/*
-			 * The conditional down operation should be implemented
-			 * this way. The userspace-only variant tends to
-			 * accumulate missed wakeups in the kernel futex wait
-			 * queue.
-			 */
-			atomic_inc(futex);
-			return ESYNCH_WOULD_BLOCK;
-			break;
-
-		case ESYNCH_OK_BLOCKED:
-			/*
-			 * Enter the critical section.
-			 * The futex counter has already been incremented for
-			 * us.
-			 */
-			return ESYNCH_OK_BLOCKED;
-			break;
-		default:
-			return rc;
-		}
-	}
-
-	/*
-	 * Enter the critical section.
-	 */
-	return ESYNCH_OK_ATOMIC;
+	return 0;
 }
 
@@ -174,14 +77,10 @@
  *
  * @param futex		Futex.
- *
- * @return		ENOENT if there is no such virtual address. Otherwise
- * 			zero.
+ * @return		ENOENT if there is no such virtual address.
+ * @return		Zero in the uncontended case.
  */
 int futex_up(futex_t *futex)
 {
-	long val;
-	
-	val = atomic_postinc(futex);
-	if (val < 0)
+	if (atomic_postinc(futex) < 0)
 		return __SYSCALL1(SYS_FUTEX_WAKEUP, (sysarg_t) &futex->count);
 		
Index: uspace/lib/libc/generic/io/console.c
===================================================================
--- uspace/lib/libc/generic/io/console.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/io/console.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -94,4 +94,17 @@
 }
 
+int console_get_pos(int phone, int *col, int *row)
+{
+	ipcarg_t col_v;
+	ipcarg_t row_v;
+	int rc;
+
+	rc = async_req_0_2(phone, CONSOLE_GET_POS, &col_v, &row_v);
+
+	*col = (int) col_v;
+	*row = (int) row_v;
+	return rc;
+}
+
 void console_goto(int phone, int col, int row)
 {
Index: uspace/lib/libc/generic/io/io.c
===================================================================
--- uspace/lib/libc/generic/io/io.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/io/io.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -341,9 +341,14 @@
 size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
 {
-	size_t left = size * nmemb;
-	size_t done = 0;
-	
+	size_t left, done;
+
+	if (size == 0 || nmemb == 0)
+		return 0;
+
 	/* Make sure no data is pending write. */
 	_fflushbuf(stream);
+
+	left = size * nmemb;
+	done = 0;
 	
 	while ((left > 0) && (!stream->error) && (!stream->eof)) {
@@ -365,7 +370,13 @@
 static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
 {
-	size_t left = size * nmemb;
-	size_t done = 0;
-	
+	size_t left;
+	size_t done;
+
+	if (size == 0 || nmemb == 0)
+		return 0;
+
+	left = size * nmemb;
+	done = 0;
+
 	while ((left > 0) && (!stream->error)) {
 		ssize_t wr;
@@ -421,5 +432,8 @@
 	uint8_t b;
 	bool need_flush;
-	
+
+	if (size == 0 || nmemb == 0)
+		return 0;
+
 	/* If not buffered stream, write out directly. */
 	if (stream->btype == _IONBF) {
@@ -480,5 +494,5 @@
 	
 	if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
-		size_t wr = fwrite(buf, sz, 1, stream);
+		size_t wr = fwrite(buf, 1, sz, stream);
 		
 		if (wr < sz)
Index: uspace/lib/libc/generic/io/klog.c
===================================================================
--- uspace/lib/libc/generic/io/klog.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/io/klog.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -42,5 +42,10 @@
 size_t klog_write(const void *buf, size_t size)
 {
-	return (size_t) __SYSCALL3(SYS_KLOG, 1, (sysarg_t) buf, size);
+	ssize_t ret = (ssize_t) __SYSCALL3(SYS_KLOG, 1, (sysarg_t) buf, size);
+	
+	if (ret >= 0)
+		return (size_t) ret;
+	
+	return 0;
 }
 
Index: uspace/lib/libc/generic/string.c
===================================================================
--- uspace/lib/libc/generic/string.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/string.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -471,5 +471,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.
@@ -505,5 +505,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.
@@ -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.
  * @param src   Source string.
@@ -549,36 +549,113 @@
 }
 
-/** 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';
+}
+
+/** Convert wide string to new string.
+ *
+ * Convert wide string @a src to string. Space for the new string is allocated
+ * on the heap.
+ *
+ * @param src	Source wide string.
+ * @return	New string.
+ */
+char *wstr_to_astr(const wchar_t *src)
+{
+	char dbuf[STR_BOUNDS(1)];
+	char *str;
+	wchar_t ch;
+
+	size_t src_idx;
+	size_t dest_off;
+	size_t dest_size;
+
+	/* Compute size of encoded string. */
+
+	src_idx = 0;
+	dest_size = 0;
+
+	while ((ch = src[src_idx++]) != 0) {
+		dest_off = 0;
+		if (chr_encode(ch, dbuf, &dest_off, STR_BOUNDS(1)) != EOK)
+			break;
+		dest_size += dest_off;
+	}
+
+	str = malloc(dest_size + 1);
+	if (str == NULL)
+		return NULL;
+
+	/* Encode string. */
+
+	src_idx = 0;
+	dest_off = 0;
+
+	while ((ch = src[src_idx++]) != 0) {
+		if (chr_encode(ch, str, &dest_off, dest_size) != EOK)
+			break;
+	}
+
+	str[dest_size] = '\0';
+	return str;
+}
+
+
+/** Convert string to wide string.
+ *
+ * Convert string @a src to wide string. The output is written to the
+ * buffer specified by @a dest and @a dlen. @a dlen must be non-zero
+ * and the wide string written will always be null-terminated.
+ *
+ * @param dest	Destination buffer.
+ * @param dlen	Length of destination buffer (number of wchars).
+ * @param src	Source string.
+ */
+void str_to_wstr(wchar_t *dest, size_t dlen, const char *src)
+{
+	size_t offset;
+	size_t di;
+	wchar_t c;
+
+	assert(dlen > 0);
+
+	offset = 0;
+	di = 0;
+
+	do {
+		if (di >= dlen - 1)
+			break;
+
+		c = str_decode(src, &offset, STR_NO_LIMIT);
+		dest[di++] = c;
+	} while (c != '\0');
+
+	dest[dlen - 1] = '\0';
 }
 
@@ -819,4 +896,31 @@
 }
 
+char *str_dup(const char *src)
+{
+	size_t size = str_size(src);
+	void *dest = malloc(size + 1);
+	
+	if (dest == NULL)
+		return (char *) NULL;
+	
+	return (char *) memcpy(dest, src, size + 1);
+}
+
+char *str_ndup(const char *src, size_t max_size)
+{
+	size_t size = str_size(src);
+	if (size > max_size)
+		size = max_size;
+	
+	char *dest = (char *) malloc(size + 1);
+	
+	if (dest == NULL)
+		return (char *) NULL;
+	
+	memcpy(dest, src, size);
+	dest[size] = 0;
+	return dest;
+}
+
 
 /** Convert initial part of string to unsigned long according to given base.
@@ -843,15 +947,4 @@
 }
 
-char *str_dup(const char *src)
-{
-	size_t size = str_size(src);
-	void *dest = malloc(size + 1);
-
-	if (dest == NULL)
-		return (char *) NULL;
-
-	return (char *) memcpy(dest, src, size + 1);
-}
-
 char *strtok(char *s, const char *delim)
 {
Index: uspace/lib/libc/generic/time.c
===================================================================
--- uspace/lib/libc/generic/time.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/time.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -31,5 +31,5 @@
  */
 /** @file
- */ 
+ */
 
 #include <sys/time.h>
@@ -40,7 +40,7 @@
 #include <unistd.h>
 #include <atomic.h>
-#include <futex.h>
 #include <sysinfo.h>
 #include <ipc/services.h>
+#include <libc.h>
 
 #include <sysinfo.h>
@@ -189,27 +189,20 @@
 
 /** Wait unconditionally for specified number of microseconds */
-int usleep(unsigned long usec)
-{
-	atomic_t futex = FUTEX_INITIALIZER;
-
-	futex_initialize(&futex, 0);
-	futex_down_timeout(&futex, usec, 0);
+int usleep(useconds_t usec)
+{
+	(void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
 	return 0;
 }
 
 /** Wait unconditionally for specified number of seconds */
-unsigned int sleep(unsigned int seconds)
-{
-	atomic_t futex = FUTEX_INITIALIZER;
-
-	futex_initialize(&futex, 0);
-	
+unsigned int sleep(unsigned int sec)
+{
 	/* Sleep in 1000 second steps to support
 	   full argument range */
-	while (seconds > 0) {
-		unsigned int period = (seconds > 1000) ? 1000 : seconds;
+	while (sec > 0) {
+		unsigned int period = (sec > 1000) ? 1000 : sec;
 	
-		futex_down_timeout(&futex, period * 1000000, 0);
-		seconds -= period;
+		usleep(period * 1000000);
+		sec -= period;
 	}
 	return 0;
Index: uspace/lib/libc/generic/vfs/canonify.c
===================================================================
--- uspace/lib/libc/generic/vfs/canonify.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/vfs/canonify.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -142,5 +142,5 @@
 	t->start[-1] = '\0';
 }
-/** Eat the extra '/'..
+/** Eat the extra '/'.
  *
  * @param t		The current TK_SLASH token.
@@ -288,9 +288,15 @@
  *
  * A file system path is canonical, if the following holds:
- * 1) the path is absolute (i.e. a/b/c is not canonical)
- * 2) there is no trailing slash in the path (i.e. /a/b/c is not canonical)
- * 3) there is no extra slash in the path (i.e. /a//b/c is not canonical)
- * 4) there is no '.' component in the path (i.e. /a/./b/c is not canonical)
- * 5) there is no '..' component in the path (i.e. /a/b/../c is not canonical) 
+ *
+ * 1) the path is absolute
+ *    (i.e. a/b/c is not canonical)
+ * 2) there is no trailing slash in the path if it has components
+ *    (i.e. /a/b/c/ is not canonical)
+ * 3) there is no extra slash in the path
+ *    (i.e. /a//b/c is not canonical)
+ * 4) there is no '.' component in the path
+ *    (i.e. /a/./b/c is not canonical)
+ * 5) there is no '..' component in the path
+ *    (i.e. /a/b/../c is not canonical)
  *
  * This function makes a potentially non-canonical file system path canonical.
Index: uspace/lib/libc/generic/vfs/vfs.c
===================================================================
--- uspace/lib/libc/generic/vfs/vfs.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/generic/vfs/vfs.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -117,5 +117,5 @@
 }
 
-int mount(const char *fs_name, const char *mp, const char *dev,
+int mount(const char *fs_name, const char *mp, const char *fqdn,
     const char *opts, unsigned int flags)
 {
@@ -126,5 +126,5 @@
 	dev_handle_t dev_handle;
 	
-	res = devmap_device_get_handle(dev, &dev_handle, flags);
+	res = devmap_device_get_handle(fqdn, &dev_handle, flags);
 	if (res != EOK)
 		return res;
@@ -703,8 +703,8 @@
 	rc = fstat(fildes, &stat);
 
-	if (!stat.devfs_stat.device)
+	if (!stat.device)
 		return -1;
 	
-	return devmap_device_connect(stat.devfs_stat.device, 0);
+	return devmap_device_connect(stat.device, 0);
 }
 
Index: uspace/lib/libc/include/async.h
===================================================================
--- uspace/lib/libc/include/async.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/async.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -284,4 +284,7 @@
 extern int async_data_write_finalize(ipc_callid_t, void *, size_t);
 
+extern int async_data_blob_receive(char **, const size_t, size_t *);
+extern int async_data_string_receive(char **, const size_t);
+
 #endif
 
Index: uspace/lib/libc/include/atomic.h
===================================================================
--- uspace/lib/libc/include/atomic.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/atomic.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2006 Jakub Jermar
+ * Copyright (c) 2009 Jakub Jermar
  * All rights reserved.
  *
@@ -36,19 +36,5 @@
 #define LIBC_ATOMIC_H_
 
-typedef struct atomic {
-	volatile long count;
-} atomic_t;
-
 #include <libarch/atomic.h>
-
-static inline void atomic_set(atomic_t *val, long i)
-{
-        val->count = i;
-}
-
-static inline long atomic_get(atomic_t *val)
-{
-        return val->count;
-}
 
 #endif
Index: uspace/lib/libc/include/atomicdflt.h
===================================================================
--- uspace/lib/libc/include/atomicdflt.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/lib/libc/include/atomicdflt.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2006 Jakub Jermar
+ * 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 libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_ATOMICDFLT_H_
+#define LIBC_ATOMICDFLT_H_
+
+#ifndef LIBC_ARCH_ATOMIC_H_
+#error This file cannot be included directly, include atomic.h instead.
+#endif
+
+#include <bool.h>
+
+typedef struct atomic {
+	volatile long count;
+} atomic_t;
+
+static inline void atomic_set(atomic_t *val, long i)
+{
+        val->count = i;
+}
+
+static inline long atomic_get(atomic_t *val)
+{
+        return val->count;
+}
+
+#ifndef CAS 
+static inline bool cas(atomic_t *val, long ov, long nv)
+{
+	return __sync_bool_compare_and_swap(&val->count, ov, nv);
+}
+#endif
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/libc/include/clipboard.h
===================================================================
--- uspace/lib/libc/include/clipboard.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/lib/libc/include/clipboard.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2009 Jiri Svoboda
+ * 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 libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_CLIPBOARD_H_
+#define LIBC_CLIPBOARD_H_
+
+extern int clipboard_put_str(const char *);
+extern int clipboard_get_str(char **);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/libc/include/devmap.h
===================================================================
--- uspace/lib/libc/include/devmap.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/devmap.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -38,4 +38,5 @@
 #include <ipc/devmap.h>
 #include <async.h>
+#include <bool.h>
 
 extern int devmap_get_phone(devmap_interface_t, unsigned int);
@@ -46,4 +47,7 @@
 
 extern int devmap_device_get_handle(const char *, dev_handle_t *, unsigned int);
+extern int devmap_namespace_get_handle(const char *, dev_handle_t *, unsigned int);
+extern devmap_handle_type_t devmap_handle_probe(dev_handle_t);
+
 extern int devmap_device_connect(dev_handle_t, unsigned int);
 
@@ -51,6 +55,9 @@
 extern void devmap_null_destroy(int);
 
-extern ipcarg_t devmap_device_get_count(void);
-extern ipcarg_t devmap_device_get_devices(ipcarg_t, dev_desc_t *);
+extern size_t devmap_count_namespaces(void);
+extern size_t devmap_count_devices(dev_handle_t);
+
+extern size_t devmap_get_namespaces(dev_desc_t **);
+extern size_t devmap_get_devices(dev_handle_t, dev_desc_t **);
 
 #endif
Index: pace/lib/libc/include/fibril_sync.h
===================================================================
--- uspace/lib/libc/include/fibril_sync.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ 	(revision )
@@ -1,107 +1,0 @@
-/*
- * Copyright (c) 2009 Jakub Jermar 
- * 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 libc
- * @{
- */
-/** @file
- */
-
-#ifndef LIBC_FIBRIL_SYNC_H_
-#define LIBC_FIBRIL_SYNC_H_
-
-#include <async.h>
-#include <fibril.h>
-#include <adt/list.h>
-#include <libarch/tls.h>
-#include <sys/time.h>
-
-typedef struct {
-	int counter;
-	link_t waiters;
-} fibril_mutex_t;
-
-#define FIBRIL_MUTEX_INITIALIZE(name) \
-	fibril_mutex_t name = {	\
-		.counter = 1, \
-		.waiters = { \
-			.prev = &name.waiters, \
-			.next = &name.waiters, \
-		} \
-	}
-
-typedef struct {
-	unsigned writers;
-	unsigned readers;
-	link_t waiters;
-} fibril_rwlock_t;
-
-#define FIBRIL_RWLOCK_INITIALIZE(name) \
-	fibril_rwlock_t name = { \
-		.readers = 0, \
-		.writers = 0, \
-		.waiters = { \
-			.prev = &name.waiters, \
-			.next = &name.waiters, \
-		} \
-	}
-
-typedef struct {
-	link_t waiters;
-} fibril_condvar_t;
-
-#define FIBRIL_CONDVAR_INITIALIZE(name) \
-	fibril_condvar_t name = { \
-		.waiters = { \
-			.next = &name.waiters, \
-			.prev = &name.waiters, \
-		} \
-	}
-
-extern void fibril_mutex_initialize(fibril_mutex_t *);
-extern void fibril_mutex_lock(fibril_mutex_t *);
-extern bool fibril_mutex_trylock(fibril_mutex_t *);
-extern void fibril_mutex_unlock(fibril_mutex_t *);
-
-extern void fibril_rwlock_initialize(fibril_rwlock_t *);
-extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
-extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
-extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
-extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
-
-extern void fibril_condvar_initialize(fibril_condvar_t *);
-extern int fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
-    suseconds_t);
-extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
-extern void fibril_condvar_signal(fibril_condvar_t *);
-extern void fibril_condvar_broadcast(fibril_condvar_t *);
-
-#endif
-
-/** @}
- */
Index: uspace/lib/libc/include/fibril_synch.h
===================================================================
--- uspace/lib/libc/include/fibril_synch.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/lib/libc/include/fibril_synch.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2009 Jakub Jermar 
+ * 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 libc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_FIBRIL_SYNCH_H_
+#define LIBC_FIBRIL_SYNCH_H_
+
+#include <async.h>
+#include <fibril.h>
+#include <adt/list.h>
+#include <libarch/tls.h>
+#include <sys/time.h>
+
+typedef struct {
+	int counter;
+	link_t waiters;
+} fibril_mutex_t;
+
+#define FIBRIL_MUTEX_INITIALIZE(name) \
+	fibril_mutex_t name = {	\
+		.counter = 1, \
+		.waiters = { \
+			.prev = &name.waiters, \
+			.next = &name.waiters, \
+		} \
+	}
+
+typedef struct {
+	unsigned writers;
+	unsigned readers;
+	link_t waiters;
+} fibril_rwlock_t;
+
+#define FIBRIL_RWLOCK_INITIALIZE(name) \
+	fibril_rwlock_t name = { \
+		.readers = 0, \
+		.writers = 0, \
+		.waiters = { \
+			.prev = &name.waiters, \
+			.next = &name.waiters, \
+		} \
+	}
+
+typedef struct {
+	link_t waiters;
+} fibril_condvar_t;
+
+#define FIBRIL_CONDVAR_INITIALIZE(name) \
+	fibril_condvar_t name = { \
+		.waiters = { \
+			.next = &name.waiters, \
+			.prev = &name.waiters, \
+		} \
+	}
+
+extern void fibril_mutex_initialize(fibril_mutex_t *);
+extern void fibril_mutex_lock(fibril_mutex_t *);
+extern bool fibril_mutex_trylock(fibril_mutex_t *);
+extern void fibril_mutex_unlock(fibril_mutex_t *);
+
+extern void fibril_rwlock_initialize(fibril_rwlock_t *);
+extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
+extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
+extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
+extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
+
+extern void fibril_condvar_initialize(fibril_condvar_t *);
+extern int fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
+    suseconds_t);
+extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
+extern void fibril_condvar_signal(fibril_condvar_t *);
+extern void fibril_condvar_broadcast(fibril_condvar_t *);
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/libc/include/futex.h
===================================================================
--- uspace/lib/libc/include/futex.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/futex.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -46,5 +46,4 @@
 extern int futex_down(futex_t *futex);
 extern int futex_trydown(futex_t *futex);
-extern int futex_down_timeout(futex_t *futex, uint32_t usec, int flags);
 extern int futex_up(futex_t *futex);
 
Index: uspace/lib/libc/include/io/console.h
===================================================================
--- uspace/lib/libc/include/io/console.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/io/console.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -69,4 +69,5 @@
 
 extern int console_get_size(int phone, int *cols, int *rows);
+extern int console_get_pos(int phone, int *col, int *row);
 extern void console_goto(int phone, int col, int row);
 
Index: uspace/lib/libc/include/ipc/clipboard.h
===================================================================
--- uspace/lib/libc/include/ipc/clipboard.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/lib/libc/include/ipc/clipboard.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2009 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 libcipc
+ * @{
+ */
+/** @file
+ */
+
+#ifndef LIBC_IPC_CLIPBOARD_H_
+#define LIBC_IPC_CLIPBOARD_H_
+
+#include <ipc/ipc.h>
+
+typedef enum {
+	CLIPBOARD_PUT_DATA = IPC_FIRST_USER_METHOD,
+	CLIPBOARD_GET_DATA,
+	CLIPBOARD_CONTENT
+} clipboard_request_t;
+
+typedef enum {
+	CLIPBOARD_TAG_NONE,
+	CLIPBOARD_TAG_BLOB
+} clipboard_tag_t;
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/libc/include/ipc/console.h
===================================================================
--- uspace/lib/libc/include/ipc/console.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/ipc/console.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -43,4 +43,5 @@
 	CONSOLE_GET_COLOR_CAP,
 	CONSOLE_GET_EVENT,
+	CONSOLE_GET_POS,
 	CONSOLE_GOTO,
 	CONSOLE_CLEAR,
Index: uspace/lib/libc/include/ipc/devmap.h
===================================================================
--- uspace/lib/libc/include/ipc/devmap.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/ipc/devmap.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -43,14 +43,23 @@
 
 typedef enum {
+	DEV_HANDLE_NONE,
+	DEV_HANDLE_NAMESPACE,
+	DEV_HANDLE_DEVICE
+} devmap_handle_type_t;
+
+typedef enum {
 	DEVMAP_DRIVER_REGISTER = IPC_FIRST_USER_METHOD,
 	DEVMAP_DRIVER_UNREGISTER,
 	DEVMAP_DEVICE_REGISTER,
 	DEVMAP_DEVICE_UNREGISTER,
-	DEVMAP_DEVICE_GET_NAME,
 	DEVMAP_DEVICE_GET_HANDLE,
-	DEVMAP_DEVICE_NULL_CREATE,
-	DEVMAP_DEVICE_NULL_DESTROY,
-	DEVMAP_DEVICE_GET_COUNT,
-	DEVMAP_DEVICE_GET_DEVICES
+	DEVMAP_NAMESPACE_GET_HANDLE,
+	DEVMAP_HANDLE_PROBE,
+	DEVMAP_NULL_CREATE,
+	DEVMAP_NULL_DESTROY,
+	DEVMAP_GET_NAMESPACE_COUNT,
+	DEVMAP_GET_DEVICE_COUNT,
+	DEVMAP_GET_NAMESPACES,
+	DEVMAP_GET_DEVICES
 } devmap_request_t;
 
Index: uspace/lib/libc/include/ipc/services.h
===================================================================
--- uspace/lib/libc/include/ipc/services.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/ipc/services.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -41,5 +41,4 @@
 	SERVICE_LOAD = 1,
 	SERVICE_PCI,
-	SERVICE_KEYBOARD,
 	SERVICE_VIDEO,
 	SERVICE_CONSOLE,
@@ -47,5 +46,6 @@
 	SERVICE_DEVMAP,
 	SERVICE_FHC,
-	SERVICE_OBIO
+	SERVICE_OBIO,
+	SERVICE_CLIPBOARD
 } services_t;
 
Index: uspace/lib/libc/include/string.h
===================================================================
--- uspace/lib/libc/include/string.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/string.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -73,5 +73,7 @@
 extern void str_append(char *dest, size_t size, const char *src);
 
-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 *wstr_to_astr(const wchar_t *src);
+extern void str_to_wstr(wchar_t *dest, size_t dlen, const char *src);
 
 extern char *str_chr(const char *str, wchar_t ch);
@@ -82,4 +84,5 @@
 
 extern char *str_dup(const char *);
+extern char *str_ndup(const char *, size_t max_size);
 
 /*
Index: uspace/lib/libc/include/sys/stat.h
===================================================================
--- uspace/lib/libc/include/sys/stat.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/sys/stat.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -42,15 +42,12 @@
 
 struct stat {
-	fs_handle_t	fs_handle;
-	dev_handle_t	dev_handle;
-	fs_index_t	index;
-	unsigned	lnkcnt;
-	bool		is_file;
-	off_t		size;
-	union {
-		struct {
-			dev_handle_t	device;
-		} devfs_stat;
-	};
+	fs_handle_t fs_handle;
+	dev_handle_t dev_handle;
+	fs_index_t index;
+	unsigned int lnkcnt;
+	bool is_file;
+	bool is_directory;
+	off_t size;
+	dev_handle_t device;
 };
 
Index: uspace/lib/libc/include/unistd.h
===================================================================
--- uspace/lib/libc/include/unistd.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libc/include/unistd.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -51,4 +51,6 @@
 #endif
 
+typedef uint32_t useconds_t;
+
 extern int dup2(int oldfd, int newfd);
 
@@ -68,6 +70,6 @@
 
 extern void _exit(int status) __attribute__ ((noreturn));
-extern int usleep(unsigned long usec);
-extern unsigned int sleep(unsigned int seconds);
+extern int usleep(useconds_t uses);
+extern unsigned int sleep(unsigned int se);
 
 #endif
Index: uspace/lib/libfs/libfs.c
===================================================================
--- uspace/lib/libfs/libfs.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libfs/libfs.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -1,4 +1,4 @@
 /*
- * Copyright (c) 2009 Jakub Jermar 
+ * Copyright (c) 2009 Jakub Jermar
  * All rights reserved.
  *
@@ -27,13 +27,13 @@
  */
 
-/** @addtogroup libfs 
+/** @addtogroup libfs
  * @{
- */ 
+ */
 /**
  * @file
- * Glue code which is commonod to all FS implementations. 
- */
-
-#include "libfs.h" 
+ * Glue code which is common to all FS implementations.
+ */
+
+#include "libfs.h"
 #include "../../srv/vfs/vfs.h"
 #include <errno.h>
@@ -67,13 +67,14 @@
  * code.
  *
- * @param vfs_phone	Open phone for communication with VFS.
- * @param reg		File system registration structure. It will be
- * 			initialized by this function.
- * @param info		VFS info structure supplied by the file system
- *			implementation.
- * @param conn		Connection fibril for handling all calls originating in
- *			VFS.
- *
- * @return		EOK on success or a non-zero error code on errror.
+ * @param vfs_phone Open phone for communication with VFS.
+ * @param reg       File system registration structure. It will be
+ *                  initialized by this function.
+ * @param info      VFS info structure supplied by the file system
+ *                  implementation.
+ * @param conn      Connection fibril for handling all calls originating in
+ *                  VFS.
+ *
+ * @return EOK on success or a non-zero error code on errror.
+ *
  */
 int fs_register(int vfs_phone, fs_reg_t *reg, vfs_info_t *info,
@@ -87,5 +88,5 @@
 	ipc_call_t answer;
 	aid_t req = async_send_0(vfs_phone, VFS_IN_REGISTER, &answer);
-
+	
 	/*
 	 * Send our VFS info structure to VFS.
@@ -96,10 +97,10 @@
 		return rc;
 	}
-
+	
 	/*
 	 * Ask VFS for callback connection.
 	 */
 	ipc_connect_to_me(vfs_phone, 0, 0, 0, &reg->vfs_phonehash);
-
+	
 	/*
 	 * Allocate piece of address space for PLB.
@@ -110,5 +111,5 @@
 		return ENOMEM;
 	}
-
+	
 	/*
 	 * Request sharing the Path Lookup Buffer with VFS.
@@ -136,5 +137,5 @@
 	 */
 	async_set_client_connection(conn);
-
+	
 	return IPC_GET_RETVAL(answer);
 }
@@ -154,18 +155,20 @@
 	int res;
 	ipcarg_t rc;
-
+	
 	ipc_call_t call;
 	ipc_callid_t callid;
-
-	/* accept the phone */
+	
+	/* Accept the phone */
 	callid = async_get_call(&call);
 	int mountee_phone = (int)IPC_GET_ARG1(call);
 	if ((IPC_GET_METHOD(call) != IPC_M_CONNECTION_CLONE) ||
-	    mountee_phone < 0) {
+	    (mountee_phone < 0)) {
 		ipc_answer_0(callid, EINVAL);
 		ipc_answer_0(rid, EINVAL);
 		return;
 	}
-	ipc_answer_0(callid, EOK);	/* acknowledge the mountee_phone */
+	
+	/* Acknowledge the mountee_phone */
+	ipc_answer_0(callid, EOK);
 	
 	res = async_data_write_receive(&callid, NULL);
@@ -176,8 +179,8 @@
 		return;
 	}
-
+	
 	fs_node_t *fn;
 	res = ops->node_get(&fn, mp_dev_handle, mp_fs_index);
-	if (res != EOK || !fn) {
+	if ((res != EOK) || (!fn)) {
 		ipc_hangup(mountee_phone);
 		ipc_answer_0(callid, combine_rc(res, ENOENT));
@@ -185,5 +188,5 @@
 		return;
 	}
-
+	
 	if (fn->mp_data.mp_active) {
 		ipc_hangup(mountee_phone);
@@ -193,5 +196,5 @@
 		return;
 	}
-
+	
 	rc = async_req_0_0(mountee_phone, IPC_M_CONNECT_ME);
 	if (rc != EOK) {
@@ -215,4 +218,5 @@
 		fn->mp_data.phone = mountee_phone;
 	}
+	
 	/*
 	 * Do not release the FS node so that it stays in memory.
@@ -238,24 +242,24 @@
     ipc_call_t *request)
 {
-	unsigned first = IPC_GET_ARG1(*request);
-	unsigned last = IPC_GET_ARG2(*request);
-	unsigned next = first;
+	unsigned int first = IPC_GET_ARG1(*request);
+	unsigned int last = IPC_GET_ARG2(*request);
+	unsigned int next = first;
 	dev_handle_t dev_handle = IPC_GET_ARG3(*request);
 	int lflag = IPC_GET_ARG4(*request);
-	fs_index_t index = IPC_GET_ARG5(*request); /* when L_LINK specified */
+	fs_index_t index = IPC_GET_ARG5(*request);
 	char component[NAME_MAX + 1];
 	int len;
 	int rc;
-
+	
 	if (last < next)
 		last += PLB_SIZE;
-
+	
 	fs_node_t *par = NULL;
 	fs_node_t *cur = NULL;
 	fs_node_t *tmp = NULL;
-
+	
 	rc = ops->root_get(&cur, dev_handle);
 	on_error(rc, goto out_with_answer);
-
+	
 	if (cur->mp_data.mp_active) {
 		ipc_forward_slow(rid, cur->mp_data.phone, VFS_OUT_LOOKUP,
@@ -265,42 +269,45 @@
 		return;
 	}
-
+	
+	/* Eat slash */
 	if (ops->plb_get_char(next) == '/')
-		next++;		/* eat slash */
+		next++;
 	
 	while (next <= last) {
 		bool has_children;
-
+		
 		rc = ops->has_children(&has_children, cur);
 		on_error(rc, goto out_with_answer);
 		if (!has_children)
 			break;
-
-		/* collect the component */
+		
+		/* Collect the component */
 		len = 0;
-		while ((next <= last) &&  (ops->plb_get_char(next) != '/')) {
+		while ((next <= last) && (ops->plb_get_char(next) != '/')) {
 			if (len + 1 == NAME_MAX) {
-				/* component length overflow */
+				/* Component length overflow */
 				ipc_answer_0(rid, ENAMETOOLONG);
 				goto out;
 			}
 			component[len++] = ops->plb_get_char(next);
-			next++;	/* process next character */
+			/* Process next character */
+			next++;
 		}
-
+		
 		assert(len);
 		component[len] = '\0';
-		next++;		/* eat slash */
-
-		/* match the component */
+		/* Eat slash */
+		next++;
+		
+		/* Match the component */
 		rc = ops->match(&tmp, cur, component);
 		on_error(rc, goto out_with_answer);
-
-		if (tmp && tmp->mp_data.mp_active) {
+		
+		if ((tmp) && (tmp->mp_data.mp_active)) {
 			if (next > last)
 				next = last = first;
 			else
 				next--;
-				
+			
 			ipc_forward_slow(rid, tmp->mp_data.phone,
 			    VFS_OUT_LOOKUP, next, last, tmp->mp_data.dev_handle,
@@ -312,19 +319,21 @@
 			return;
 		}
-
-		/* handle miss: match amongst siblings */
+		
+		/* Handle miss: match amongst siblings */
 		if (!tmp) {
 			if (next <= last) {
-				/* there are unprocessed components */
+				/* There are unprocessed components */
 				ipc_answer_0(rid, ENOENT);
 				goto out;
 			}
-			/* miss in the last component */
-			if (lflag & (L_CREATE | L_LINK)) { 
-				/* request to create a new link */
+			
+			/* Miss in the last component */
+			if (lflag & (L_CREATE | L_LINK)) {
+				/* Request to create a new link */
 				if (!ops->is_directory(cur)) {
 					ipc_answer_0(rid, ENOTDIR);
 					goto out;
 				}
+				
 				fs_node_t *fn;
 				if (lflag & L_CREATE)
@@ -335,4 +344,5 @@
 					    index);
 				on_error(rc, goto out_with_answer);
+				
 				if (fn) {
 					rc = ops->link(cur, fn, component);
@@ -349,33 +359,34 @@
 						(void) ops->node_put(fn);
 					}
-				} else {
+				} else
 					ipc_answer_0(rid, ENOSPC);
-				}
+				
 				goto out;
-			} 
+			}
+			
 			ipc_answer_0(rid, ENOENT);
 			goto out;
 		}
-
+		
 		if (par) {
 			rc = ops->node_put(par);
 			on_error(rc, goto out_with_answer);
 		}
-
-		/* descend one level */
+		
+		/* Descend one level */
 		par = cur;
 		cur = tmp;
 		tmp = NULL;
 	}
-
-	/* handle miss: excessive components */
+	
+	/* Handle miss: excessive components */
 	if (next <= last) {
 		bool has_children;
-
 		rc = ops->has_children(&has_children, cur);
 		on_error(rc, goto out_with_answer);
+		
 		if (has_children)
 			goto skip_miss;
-
+		
 		if (lflag & (L_CREATE | L_LINK)) {
 			if (!ops->is_directory(cur)) {
@@ -383,24 +394,28 @@
 				goto out;
 			}
-
-			/* collect next component */
+			
+			/* Collect next component */
 			len = 0;
 			while (next <= last) {
 				if (ops->plb_get_char(next) == '/') {
-					/* more than one component */
+					/* More than one component */
 					ipc_answer_0(rid, ENOENT);
 					goto out;
 				}
+				
 				if (len + 1 == NAME_MAX) {
-					/* component length overflow */
+					/* Component length overflow */
 					ipc_answer_0(rid, ENAMETOOLONG);
 					goto out;
 				}
+				
 				component[len++] = ops->plb_get_char(next);
-				next++;	/* process next character */
+				/* Process next character */
+				next++;
 			}
+			
 			assert(len);
 			component[len] = '\0';
-				
+			
 			fs_node_t *fn;
 			if (lflag & L_CREATE)
@@ -409,4 +424,5 @@
 				rc = ops->node_get(&fn, dev_handle, index);
 			on_error(rc, goto out_with_answer);
+			
 			if (fn) {
 				rc = ops->link(cur, fn, component);
@@ -423,22 +439,25 @@
 					(void) ops->node_put(fn);
 				}
-			} else {
+			} else
 				ipc_answer_0(rid, ENOSPC);
-			}
+			
 			goto out;
 		}
+		
 		ipc_answer_0(rid, ENOENT);
 		goto out;
 	}
+	
 skip_miss:
-
-	/* handle hit */
+	
+	/* Handle hit */
 	if (lflag & L_UNLINK) {
-		unsigned old_lnkcnt = ops->lnkcnt_get(cur);
+		unsigned int old_lnkcnt = ops->lnkcnt_get(cur);
 		rc = ops->unlink(par, cur, component);
-		ipc_answer_5(rid, (ipcarg_t)rc, fs_handle, dev_handle,
+		ipc_answer_5(rid, (ipcarg_t) rc, fs_handle, dev_handle,
 		    ops->index_get(cur), ops->size_get(cur), old_lnkcnt);
 		goto out;
 	}
+	
 	if (((lflag & (L_CREATE | L_EXCLUSIVE)) == (L_CREATE | L_EXCLUSIVE)) ||
 	    (lflag & L_LINK)) {
@@ -446,27 +465,35 @@
 		goto out;
 	}
+	
 	if ((lflag & L_FILE) && (ops->is_directory(cur))) {
 		ipc_answer_0(rid, EISDIR);
 		goto out;
 	}
+	
 	if ((lflag & L_DIRECTORY) && (ops->is_file(cur))) {
 		ipc_answer_0(rid, ENOTDIR);
 		goto out;
 	}
-
+	
 out_with_answer:
+	
 	if (rc == EOK) {
-		ipc_answer_5(rid, EOK, fs_handle, dev_handle,
+		if (lflag & L_OPEN)
+			rc = ops->node_open(cur);
+		
+		ipc_answer_5(rid, rc, fs_handle, dev_handle,
 		    ops->index_get(cur), ops->size_get(cur),
 		    ops->lnkcnt_get(cur));
-	} else {
+	} else
 		ipc_answer_0(rid, rc);
-	}
-
+	
 out:
+	
 	if (par)
 		(void) ops->node_put(par);
+	
 	if (cur)
 		(void) ops->node_put(cur);
+	
 	if (tmp)
 		(void) ops->node_put(tmp);
@@ -478,19 +505,19 @@
 	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
 	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
+	
 	fs_node_t *fn;
-	int rc;
-
-	rc = ops->node_get(&fn, dev_handle, index);
+	int rc = ops->node_get(&fn, dev_handle, index);
 	on_error(rc, answer_and_return(rid, rc));
-
+	
 	ipc_callid_t callid;
 	size_t size;
-	if (!async_data_read_receive(&callid, &size) ||
-	    size != sizeof(struct stat)) {
+	if ((!async_data_read_receive(&callid, &size)) ||
+	    (size != sizeof(struct stat))) {
+		ops->node_put(fn);
 		ipc_answer_0(callid, EINVAL);
 		ipc_answer_0(rid, EINVAL);
 		return;
 	}
-
+	
 	struct stat stat;
 	memset(&stat, 0, sizeof(struct stat));
@@ -499,8 +526,12 @@
 	stat.dev_handle = dev_handle;
 	stat.index = index;
-	stat.lnkcnt = ops->lnkcnt_get(fn); 
+	stat.lnkcnt = ops->lnkcnt_get(fn);
 	stat.is_file = ops->is_file(fn);
+	stat.is_directory = ops->is_directory(fn);
 	stat.size = ops->size_get(fn);
-
+	stat.device = ops->device_get(fn);
+	
+	ops->node_put(fn);
+	
 	async_data_read_finalize(callid, &stat, sizeof(struct stat));
 	ipc_answer_0(rid, EOK);
@@ -509,8 +540,8 @@
 /** Open VFS triplet.
  *
- * @param ops       libfs operations structure with function pointers to
- *                  file system implementation
- * @param rid       Request ID of the VFS_OUT_OPEN_NODE request.
- * @param request   VFS_OUT_OPEN_NODE request data itself.
+ * @param ops     libfs operations structure with function pointers to
+ *                file system implementation
+ * @param rid     Request ID of the VFS_OUT_OPEN_NODE request.
+ * @param request VFS_OUT_OPEN_NODE request data itself.
  *
  */
@@ -531,5 +562,6 @@
 	}
 	
-	ipc_answer_3(rid, EOK, ops->size_get(fn), ops->lnkcnt_get(fn),
+	rc = ops->node_open(fn);
+	ipc_answer_3(rid, rc, ops->size_get(fn), ops->lnkcnt_get(fn),
 	    (ops->is_file(fn) ? L_FILE : 0) | (ops->is_directory(fn) ? L_DIRECTORY : 0));
 	
Index: uspace/lib/libfs/libfs.h
===================================================================
--- uspace/lib/libfs/libfs.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/lib/libfs/libfs.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -64,4 +64,5 @@
 	int (* match)(fs_node_t **, fs_node_t *, const char *);
 	int (* node_get)(fs_node_t **, dev_handle_t, fs_index_t);
+	int (* node_open)(fs_node_t *);
 	int (* node_put)(fs_node_t *);
 	int (* create)(fs_node_t **, dev_handle_t, int);
@@ -76,8 +77,9 @@
 	fs_index_t (* index_get)(fs_node_t *);
 	size_t (* size_get)(fs_node_t *);
-	unsigned (* lnkcnt_get)(fs_node_t *);
+	unsigned int (* lnkcnt_get)(fs_node_t *);
 	char (* plb_get_char)(unsigned pos);
 	bool (* is_directory)(fs_node_t *);
 	bool (* is_file)(fs_node_t *);
+	dev_handle_t (* device_get)(fs_node_t *);
 } libfs_ops_t;
 
Index: uspace/srv/bd/ata_bd/ata_bd.c
===================================================================
--- uspace/srv/bd/ata_bd/ata_bd.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/bd/ata_bd/ata_bd.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -55,5 +55,5 @@
 #include <async.h>
 #include <as.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <string.h>
 #include <devmap.h>
@@ -66,5 +66,6 @@
 #include "ata_bd.h"
 
-#define NAME "ata_bd"
+#define NAME       "ata_bd"
+#define NAMESPACE  "bd"
 
 /** Physical block size. Should be always 512. */
@@ -135,11 +136,10 @@
 		if (disk[i].present == false)
 			continue;
-
-		snprintf(name, 16, "disk%d", i);
+		
+		snprintf(name, 16, "%s/disk%d", NAMESPACE, i);
 		rc = devmap_device_register(name, &disk[i].dev_handle);
 		if (rc != EOK) {
 			devmap_hangup_phone(DEVMAP_DRIVER);
-			printf(NAME ": Unable to register device %s.\n",
-				name);
+			printf(NAME ": Unable to register device %s.\n", name);
 			return rc;
 		}
Index: uspace/srv/bd/ata_bd/ata_bd.h
===================================================================
--- uspace/srv/bd/ata_bd/ata_bd.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/bd/ata_bd/ata_bd.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -37,14 +37,14 @@
 
 #include <sys/types.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <string.h>
 
 enum {
-	CTL_READ_START	= 0,
+	CTL_READ_START  = 0,
 	CTL_WRITE_START = 1,
 };
 
 enum {
-	STATUS_FAILURE	= 0
+	STATUS_FAILURE = 0
 };
 
Index: uspace/srv/bd/file_bd/file_bd.c
===================================================================
--- uspace/srv/bd/file_bd/file_bd.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/bd/file_bd/file_bd.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -45,5 +45,5 @@
 #include <async.h>
 #include <as.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <devmap.h>
 #include <sys/types.h>
Index: uspace/srv/bd/gxe_bd/gxe_bd.c
===================================================================
--- uspace/srv/bd/gxe_bd/gxe_bd.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/bd/gxe_bd/gxe_bd.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -43,5 +43,5 @@
 #include <async.h>
 #include <as.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <devmap.h>
 #include <sys/types.h>
@@ -50,5 +50,6 @@
 #include <task.h>
 
-#define NAME "gxe_bd"
+#define NAME       "gxe_bd"
+#define NAMESPACE  "bd"
 
 enum {
@@ -141,10 +142,9 @@
 
 	for (i = 0; i < MAX_DISKS; i++) {
-		snprintf(name, 16, "disk%d", i);
+		snprintf(name, 16, "%s/disk%d", NAMESPACE, i);
 		rc = devmap_device_register(name, &dev_handle[i]);
 		if (rc != EOK) {
 			devmap_hangup_phone(DEVMAP_DRIVER);
-			printf(NAME ": Unable to register device %s.\n",
-				name);
+			printf(NAME ": Unable to register device %s.\n", name);
 			return rc;
 		}
Index: uspace/srv/bd/rd/rd.c
===================================================================
--- uspace/srv/bd/rd/rd.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/bd/rd/rd.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -51,5 +51,5 @@
 #include <align.h>
 #include <async.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <stdio.h>
 #include <devmap.h>
@@ -228,5 +228,5 @@
 	
 	dev_handle_t dev_handle;
-	if (devmap_device_register("initrd", &dev_handle) != EOK) {
+	if (devmap_device_register("bd/initrd", &dev_handle) != EOK) {
 		devmap_hangup_phone(DEVMAP_DRIVER);
 		printf(NAME ": Unable to register device\n");
Index: uspace/srv/clip/Makefile
===================================================================
--- uspace/srv/clip/Makefile	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/srv/clip/Makefile	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,39 @@
+#
+# Copyright (c) 2009 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.
+#
+
+include Makefile.common
+
+.PHONY: all clean
+
+all: $(LIBC_PREFIX)/../../../Makefile.config $(LIBC_PREFIX)/../../../config.h $(LIBC_PREFIX)/../../../config.defs $(LIBS)
+	-[ -f $(DEPEND) ] && mv -f $(DEPEND) $(DEPEND_PREV)
+	$(MAKE) -f Makefile.build
+
+clean:
+	rm -f $(DEPEND) $(DEPEND_PREV) $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm
+	find . -name '*.o' -follow -exec rm \{\} \;
Index: uspace/srv/clip/Makefile.build
===================================================================
--- uspace/srv/clip/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/srv/clip/Makefile.build	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,62 @@
+#
+# Copyright (c) 2009 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.
+#
+
+## Setup toolchain
+#
+
+include Makefile.common
+include $(LIBC_PREFIX)/Makefile.toolchain
+
+## Sources
+#
+
+SOURCES = \
+	clip.c
+
+CFLAGS += -D$(UARCH)
+
+OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
+
+.PHONY: all
+
+all: $(OUTPUT) $(OUTPUT).disasm
+
+-include $(DEPEND)
+
+$(OUTPUT).disasm: $(OUTPUT)
+	$(OBJDUMP) -d $< > $@
+
+$(OUTPUT): $(OBJECTS) $(LIBS)
+	$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map
+
+%.o: %.c $(DEPEND)
+	$(CC) $(DEFS) $(CFLAGS) -c $< -o $@
+
+$(DEPEND):
+	makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
+	-[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
Index: uspace/srv/clip/Makefile.common
===================================================================
--- uspace/srv/clip/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/srv/clip/Makefile.common	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,39 @@
+#
+# Copyright (c) 2009 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.
+#
+
+
+## Common names
+#
+
+LIBC_PREFIX = ../../lib/libc
+SOFTINT_PREFIX = ../../lib/softint
+LIBS = $(LIBC_PREFIX)/libc.a
+
+DEPEND = Makefile.depend
+DEPEND_PREV = $(DEPEND).prev
+OUTPUT = clip
Index: uspace/srv/clip/clip.c
===================================================================
--- uspace/srv/clip/clip.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/srv/clip/clip.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2009 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.
+ */
+
+#include <stdio.h>
+#include <bool.h>
+#include <ipc/ipc.h>
+#include <async.h>
+#include <ipc/services.h>
+#include <ipc/clipboard.h>
+#include <malloc.h>
+#include <fibril_synch.h>
+#include <errno.h>
+
+#define NAME  "clip"
+
+static char *clip_data = NULL;
+static size_t clip_size = 0;
+static clipboard_tag_t clip_tag = CLIPBOARD_TAG_NONE;
+static FIBRIL_MUTEX_INITIALIZE(clip_mtx);
+
+static void clip_put_data(ipc_callid_t rid, ipc_call_t *request)
+{
+	char *data;
+	int rc;
+	size_t size;
+	
+	switch (IPC_GET_ARG1(*request)) {
+	case CLIPBOARD_TAG_NONE:
+		fibril_mutex_lock(&clip_mtx);
+		
+		if (clip_data)
+			free(clip_data);
+		
+		clip_data = NULL;
+		clip_size = 0;
+		clip_tag = CLIPBOARD_TAG_NONE;
+		
+		fibril_mutex_unlock(&clip_mtx);
+		ipc_answer_0(rid, EOK);
+		break;
+	case CLIPBOARD_TAG_BLOB:
+		rc = async_data_blob_receive(&data, 0, &size);
+		if (rc != EOK) {
+			ipc_answer_0(rid, rc);
+			break;
+		}
+		
+		fibril_mutex_lock(&clip_mtx);
+		
+		if (clip_data)
+			free(clip_data);
+		
+		clip_data = data;
+		clip_size = size;
+		clip_tag = CLIPBOARD_TAG_BLOB;
+		
+		fibril_mutex_unlock(&clip_mtx);
+		ipc_answer_0(rid, EOK);
+		break;
+	default:
+		ipc_answer_0(rid, EINVAL);
+	}
+}
+
+static void clip_get_data(ipc_callid_t rid, ipc_call_t *request)
+{
+	fibril_mutex_lock(&clip_mtx);
+	
+	ipc_callid_t callid;
+	size_t size;
+	
+	/* Check for clipboard data tag compatibility */
+	switch (IPC_GET_ARG1(*request)) {
+	case CLIPBOARD_TAG_BLOB:
+		if (!async_data_read_receive(&callid, &size)) {
+			ipc_answer_0(callid, EINVAL);
+			ipc_answer_0(rid, EINVAL);
+			break;
+		}
+		
+		if (clip_tag != CLIPBOARD_TAG_BLOB) {
+			/* So far we only understand BLOB */
+			ipc_answer_0(callid, EOVERFLOW);
+			ipc_answer_0(rid, EOVERFLOW);
+			break;
+		}
+		
+		if (clip_size != size) {
+			/* The client expects different size of data */
+			ipc_answer_0(callid, EOVERFLOW);
+			ipc_answer_0(rid, EOVERFLOW);
+			break;
+		}
+		
+		ipcarg_t retval = async_data_read_finalize(callid, clip_data, size);
+		if (retval != EOK) {
+			ipc_answer_0(rid, retval);
+			break;
+		}
+		
+		ipc_answer_0(rid, EOK);
+	default:
+		/*
+		 * Sorry, we don't know how to get unknown or NONE
+		 * data from the clipbard
+		 */
+		ipc_answer_0(rid, EINVAL);
+		break;
+	}
+	
+	fibril_mutex_unlock(&clip_mtx);
+}
+
+static void clip_content(ipc_callid_t rid, ipc_call_t *request)
+{
+	fibril_mutex_lock(&clip_mtx);
+	
+	size_t size = clip_size;
+	clipboard_tag_t tag = clip_tag;
+	
+	fibril_mutex_unlock(&clip_mtx);
+	ipc_answer_2(rid, EOK, (ipcarg_t) size, (ipcarg_t) clip_tag);
+}
+
+static void clip_connection(ipc_callid_t iid, ipc_call_t *icall)
+{
+	/* Accept connection */
+	ipc_answer_0(iid, EOK);
+	
+	bool cont = true;
+	while (cont) {
+		ipc_call_t call;
+		ipc_callid_t callid = async_get_call(&call);
+		
+		switch (IPC_GET_METHOD(call)) {
+		case IPC_M_PHONE_HUNGUP:
+			cont = false;
+			continue;
+		case CLIPBOARD_PUT_DATA:
+			clip_put_data(callid, &call);
+			break;
+		case CLIPBOARD_GET_DATA:
+			clip_get_data(callid, &call);
+			break;
+		case CLIPBOARD_CONTENT:
+			clip_content(callid, &call);
+			break;
+		default:
+			if (!(callid & IPC_CALLID_NOTIFICATION))
+				ipc_answer_0(callid, ENOENT);
+		}
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	printf(NAME ": HelenOS clipboard service\n");
+	
+	async_set_client_connection(clip_connection);
+	
+	ipcarg_t phonead;
+	if (ipc_connect_to_me(PHONE_NS, SERVICE_CLIPBOARD, 0, 0, &phonead) != 0) 
+		return -1;
+	
+	printf(NAME ": Accepting connections\n");
+	async_manager();
+	
+	/* Never reached */
+	return 0;
+}
Index: uspace/srv/clip/clip.h
===================================================================
--- uspace/srv/clip/clip.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
+++ uspace/srv/clip/clip.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2009 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.
+ */
+
+#ifndef CLIP_MAIN_H_
+#define CLIP_MAIN_H_
+
+#endif
Index: uspace/srv/console/console.c
===================================================================
--- uspace/srv/console/console.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/console/console.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -50,5 +50,7 @@
 #include <event.h>
 #include <devmap.h>
-#include <fibril_sync.h>
+#include <fcntl.h>
+#include <vfs/vfs.h>
+#include <fibril_synch.h>
 
 #include "console.h"
@@ -57,7 +59,6 @@
 #include "screenbuffer.h"
 
-#define NAME  "console"
-
-#define MAX_DEVICE_NAME  32
+#define NAME       "console"
+#define NAMESPACE  "term"
 
 /** Phone to the keyboard driver. */
@@ -69,5 +70,5 @@
 	ipcarg_t cols;  /**< Framebuffer columns */
 	ipcarg_t rows;  /**< Framebuffer rows */
-	int color_cap;	/**< Color capabilities (FB_CCAP_xxx) */
+	int color_cap;  /**< Color capabilities (FB_CCAP_xxx) */
 } fb_info;
 
@@ -601,4 +602,8 @@
 				    IPC_GET_ARG2(call));
 			break;
+		case CONSOLE_GET_POS:
+			arg1 = cons->scr.position_x;
+			arg2 = cons->scr.position_y;
+			break;
 		case CONSOLE_GET_SIZE:
 			arg1 = fb_info.cols;
@@ -662,18 +667,23 @@
 }
 
-static bool console_init(void)
-{
-	ipcarg_t color_cap;
-
-	/* Connect to keyboard driver */
-	kbd_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_KEYBOARD, 0, 0);
+static bool console_init(char *input)
+{
+	/* Connect to input device */
+	int input_fd = open(input, O_RDONLY);
+	if (input_fd < 0) {
+		printf(NAME ": Failed opening %s\n", input);
+		return false;
+	}
+	
+	kbd_phone = fd_phone(input_fd);
 	if (kbd_phone < 0) {
-		printf(NAME ": Failed to connect to keyboard service\n");
+		printf(NAME ": Failed to connect to input device\n");
 		return false;
 	}
 	
+	/* NB: The callback connection is slotted for removal */
 	ipcarg_t phonehash;
 	if (ipc_connect_to_me(kbd_phone, SERVICE_CONSOLE, 0, 0, &phonehash) != 0) {
-		printf(NAME ": Failed to create callback from keyboard service\n");
+		printf(NAME ": Failed to create callback from input device\n");
 		return false;
 	}
@@ -699,4 +709,5 @@
 	
 	/* Synchronize, the gcons could put something in queue */
+	ipcarg_t color_cap;
 	async_req_0_0(fb_info.phone, FB_FLUSH);
 	async_req_0_2(fb_info.phone, FB_GET_CSIZE, &fb_info.cols, &fb_info.rows);
@@ -736,6 +747,6 @@
 			consoles[i].refcount = 0;
 			
-			char vc[MAX_DEVICE_NAME];
-			snprintf(vc, MAX_DEVICE_NAME, "vc%u", i);
+			char vc[DEVMAP_NAME_MAXLEN + 1];
+			snprintf(vc, DEVMAP_NAME_MAXLEN, "%s/vc%u", NAMESPACE, i);
 			
 			if (devmap_device_register(vc, &consoles[i].dev_handle) != EOK) {
@@ -768,9 +779,19 @@
 }
 
+static void usage(void)
+{
+	printf("Usage: console <input>\n");
+}
+
 int main(int argc, char *argv[])
 {
+	if (argc < 2) {
+		usage();
+		return -1;
+	}
+	
 	printf(NAME ": HelenOS Console service\n");
 	
-	if (!console_init())
+	if (!console_init(argv[1]))
 		return -1;
 	
Index: uspace/srv/devmap/devmap.c
===================================================================
--- uspace/srv/devmap/devmap.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/devmap/devmap.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -42,5 +42,5 @@
 #include <errno.h>
 #include <bool.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <stdlib.h>
 #include <string.h>
@@ -68,4 +68,18 @@
 } devmap_driver_t;
 
+/** Info about registered namespaces
+ *
+ */
+typedef struct {
+	/** Pointer to the previous and next device in the list of all namespaces */
+	link_t namespaces;
+	/** Unique namespace identifier */
+	dev_handle_t handle;
+	/** Namespace name */
+	char *name;
+	/** Reference count */
+	size_t refcnt;
+} devmap_namespace_t;
+
 /** Info about registered device
  *
@@ -77,6 +91,8 @@
 	    owned by one driver */
 	link_t driver_devices;
-	/** Unique device identifier  */
+	/** Unique device identifier */
 	dev_handle_t handle;
+	/** Device namespace */
+	devmap_namespace_t *namespace;
 	/** Device name */
 	char *name;
@@ -86,4 +102,5 @@
 
 LIST_INITIALIZE(devices_list);
+LIST_INITIALIZE(namespaces_list);
 LIST_INITIALIZE(drivers_list);
 
@@ -117,70 +134,240 @@
 }
 
+/** Convert fully qualified device name to namespace and device name.
+ *
+ * A fully qualified device name can be either a plain device name
+ * (then the namespace is considered to be an empty string) or consist
+ * of two components separated by a slash. No more than one slash
+ * is allowed.
+ *
+ */
+static bool devmap_fqdn_split(const char *fqdn, char **ns_name, char **name)
+{
+	size_t cnt = 0;
+	size_t slash_offset = 0;
+	size_t slash_after = 0;
+	
+	size_t offset = 0;
+	size_t offset_prev = 0;
+	wchar_t c;
+	
+	while ((c = str_decode(fqdn, &offset, STR_NO_LIMIT)) != 0) {
+		if (c == '/') {
+			cnt++;
+			slash_offset = offset_prev;
+			slash_after = offset;
+		}
+		offset_prev = offset;
+	}
+	
+	/* More than one slash */
+	if (cnt > 1)
+		return false;
+	
+	/* No slash -> namespace is empty */
+	if (cnt == 0) {
+		*ns_name = str_dup("");
+		if (*ns_name == NULL)
+			return false;
+		
+		*name = str_dup(fqdn);
+		if ((*name == NULL) || (str_cmp(*name, "") == 0)) {
+			free(*ns_name);
+			return false;
+		}
+		
+		return true;
+	}
+	
+	/* Exactly one slash */
+	*ns_name = str_ndup(fqdn, slash_offset);
+	if (*ns_name == NULL)
+		return false;
+	
+	*name = str_dup(fqdn + slash_after);
+	if ((*name == NULL) || (str_cmp(*name, "") == 0)) {
+		free(*ns_name);
+		return false;
+	}
+	
+	return true;
+}
+
+/** Find namespace with given name.
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ */
+static devmap_namespace_t *devmap_namespace_find_name(const char *name)
+{
+	link_t *item = namespaces_list.next;
+	
+	while (item != &namespaces_list) {
+		devmap_namespace_t *namespace = list_get_instance(item, devmap_namespace_t, namespaces);
+		if (str_cmp(namespace->name, name) == 0)
+			return namespace;
+		item = item->next;
+	}
+	
+	return NULL;
+}
+
+/** Find namespace with given handle.
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ * @todo: use hash table
+ *
+ */
+static devmap_namespace_t *devmap_namespace_find_handle(dev_handle_t handle)
+{
+	link_t *item = namespaces_list.next;
+	
+	while (item != &namespaces_list) {
+		devmap_namespace_t *namespace = list_get_instance(item, devmap_namespace_t, namespaces);
+		if (namespace->handle == handle)
+			return namespace;
+		
+		item = item->next;
+	}
+	
+	return NULL;
+}
+
 /** Find device with given name.
  *
- */
-static devmap_device_t *devmap_device_find_name(const char *name)
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ */
+static devmap_device_t *devmap_device_find_name(const char *ns_name,
+    const char *name)
 {
 	link_t *item = devices_list.next;
-	devmap_device_t *device = NULL;
 	
 	while (item != &devices_list) {
-		device = list_get_instance(item, devmap_device_t, devices);
-		if (str_cmp(device->name, name) == 0)
-			break;
+		devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
+		if ((str_cmp(device->namespace->name, ns_name) == 0) && (str_cmp(device->name, name) == 0))
+			return device;
 		item = item->next;
 	}
 	
-	if (item == &devices_list)
+	return NULL;
+}
+
+/** Find device with given handle.
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ * @todo: use hash table
+ *
+ */
+static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
+{
+	link_t *item = devices_list.next;
+	
+	while (item != &devices_list) {
+		devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
+		if (device->handle == handle)
+			return device;
+		
+		item = item->next;
+	}
+	
+	return NULL;
+}
+
+/** Create a namespace (if not already present)
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ */
+static devmap_namespace_t *devmap_namespace_create(const char *ns_name)
+{
+	devmap_namespace_t *namespace = devmap_namespace_find_name(ns_name);
+	if (namespace != NULL)
+		return namespace;
+	
+	namespace = (devmap_namespace_t *) malloc(sizeof(devmap_namespace_t));
+	if (namespace == NULL)
 		return NULL;
 	
-	device = list_get_instance(item, devmap_device_t, devices);
-	return device;
-}
-
-/** Find device with given handle.
- *
- * @todo: use hash table
- *
- */
-static devmap_device_t *devmap_device_find_handle(dev_handle_t handle)
-{
-	fibril_mutex_lock(&devices_list_mutex);
-	
-	link_t *item = (&devices_list)->next;
-	devmap_device_t *device = NULL;
-	
-	while (item != &devices_list) {
-		device = list_get_instance(item, devmap_device_t, devices);
-		if (device->handle == handle)
-			break;
-		item = item->next;
-	}
-	
-	if (item == &devices_list) {
-		fibril_mutex_unlock(&devices_list_mutex);
+	namespace->name = str_dup(ns_name);
+	if (namespace->name == NULL) {
+		free(namespace);
 		return NULL;
 	}
 	
-	device = list_get_instance(item, devmap_device_t, devices);
-	
-	fibril_mutex_unlock(&devices_list_mutex);
-	
-	return device;
-}
-
-/**
- * Unregister device and free it. It's assumed that driver's device list is
- * already locked.
- */
-static int devmap_device_unregister_core(devmap_device_t *device)
-{
+	namespace->handle = devmap_create_handle();
+	namespace->refcnt = 0;
+	
+	/*
+	 * Insert new namespace into list of registered namespaces
+	 */
+	list_append(&(namespace->namespaces), &namespaces_list);
+	
+	return namespace;
+}
+
+/** Destroy a namespace (if it is no longer needed)
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ */
+static void devmap_namespace_destroy(devmap_namespace_t *namespace)
+{
+	if (namespace->refcnt == 0) {
+		list_remove(&(namespace->namespaces));
+		
+		free(namespace->name);
+		free(namespace);
+	}
+}
+
+/** Increase namespace reference count by including device
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ */
+static void devmap_namespace_addref(devmap_namespace_t *namespace,
+    devmap_device_t *device)
+{
+	device->namespace = namespace;
+	namespace->refcnt++;
+}
+
+/** Decrease namespace reference count
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ */
+static void devmap_namespace_delref(devmap_namespace_t *namespace)
+{
+	namespace->refcnt--;
+	devmap_namespace_destroy(namespace);
+}
+
+/** Unregister device and free it
+ *
+ * The devices_list_mutex should be already held when
+ * calling this function.
+ *
+ */
+static void devmap_device_unregister_core(devmap_device_t *device)
+{
+	devmap_namespace_delref(device->namespace);
 	list_remove(&(device->devices));
 	list_remove(&(device->driver_devices));
 	
+	free(device->namespace);
 	free(device->name);
 	free(device);
-	
-	return EOK;
 }
 
@@ -189,8 +376,6 @@
  * drivers.
  */
-static void devmap_driver_register(devmap_driver_t **odriver)
-{
-	*odriver = NULL;
-	
+static devmap_driver_t *devmap_driver_register(void)
+{
 	ipc_call_t icall;
 	ipc_callid_t iid = async_get_call(&icall);
@@ -198,5 +383,5 @@
 	if (IPC_GET_METHOD(icall) != DEVMAP_DRIVER_REGISTER) {
 		ipc_answer_0(iid, EREFUSED);
-		return;
+		return NULL;
 	}
 	
@@ -205,5 +390,5 @@
 	if (driver == NULL) {
 		ipc_answer_0(iid, ENOMEM);
-		return;
+		return NULL;
 	}
 	
@@ -211,42 +396,10 @@
 	 * Get driver name
 	 */
-	ipc_callid_t callid;
-	size_t name_size;
-	if (!async_data_write_receive(&callid, &name_size)) {
+	int rc = async_data_string_receive(&driver->name, DEVMAP_NAME_MAXLEN);
+	if (rc != EOK) {
 		free(driver);
-		ipc_answer_0(callid, EREFUSED);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	if (name_size > DEVMAP_NAME_MAXLEN) {
-		free(driver);
-		ipc_answer_0(callid, EINVAL);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	/*
-	 * Allocate buffer for device name.
-	 */
-	driver->name = (char *) malloc(name_size + 1);
-	if (driver->name == NULL) {
-		free(driver);
-		ipc_answer_0(callid, ENOMEM);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	/*
-	 * Send confirmation to sender and get data into buffer.
-	 */
-	if (async_data_write_finalize(callid, driver->name, name_size) != EOK) {
-		free(driver->name);
-		free(driver);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	driver->name[name_size] = 0;
+		ipc_answer_0(iid, rc);
+		return NULL;
+	}
 	
 	/* Initialize mutex for list of devices owned by this driver */
@@ -262,5 +415,5 @@
 	 */
 	ipc_call_t call;
-	callid = async_get_call(&call);
+	ipc_callid_t callid = async_get_call(&call);
 	
 	if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
@@ -270,5 +423,5 @@
 		free(driver);
 		ipc_answer_0(iid, ENOTSUP);
-		return;
+		return NULL;
 	}
 	
@@ -293,5 +446,5 @@
 	ipc_answer_0(iid, EOK);
 	
-	*odriver = driver;
+	return driver;
 }
 
@@ -355,42 +508,43 @@
 	}
 	
-	/* Get device name */
-	ipc_callid_t callid;
-	size_t size;
-	if (!async_data_write_receive(&callid, &size)) {
+	/* Get fqdn */
+	char *fqdn;
+	int rc = async_data_string_receive(&fqdn, DEVMAP_NAME_MAXLEN);
+	if (rc != EOK) {
 		free(device);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	if (size > DEVMAP_NAME_MAXLEN) {
+		ipc_answer_0(iid, rc);
+		return;
+	}
+	
+	char *ns_name;
+	if (!devmap_fqdn_split(fqdn, &ns_name, &device->name)) {
+		free(fqdn);
 		free(device);
-		ipc_answer_0(callid, EINVAL);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	/* +1 for terminating \0 */
-	device->name = (char *) malloc(size + 1);
-	
-	if (device->name == NULL) {
+		ipc_answer_0(iid, EINVAL);
+		return;
+	}
+	
+	free(fqdn);
+	
+	fibril_mutex_lock(&devices_list_mutex);
+	
+	devmap_namespace_t *namespace = devmap_namespace_create(ns_name);
+	free(ns_name);
+	if (!namespace) {
+		fibril_mutex_unlock(&devices_list_mutex);
 		free(device);
-		ipc_answer_0(callid, ENOMEM);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	async_data_write_finalize(callid, device->name, size);
-	device->name[size] = 0;
+		ipc_answer_0(iid, ENOMEM);
+		return;
+	}
 	
 	list_initialize(&(device->devices));
 	list_initialize(&(device->driver_devices));
 	
-	fibril_mutex_lock(&devices_list_mutex);
-	
-	/* Check that device with such name is not already registered */
-	if (NULL != devmap_device_find_name(device->name)) {
-		printf(NAME ": Device '%s' already registered\n", device->name);
+	/* Check that device is not already registered */
+	if (devmap_device_find_name(namespace->name, device->name) != NULL) {
+		printf(NAME ": Device '%s/%s' already registered\n", device->namespace, device->name);
+		devmap_namespace_destroy(namespace);
 		fibril_mutex_unlock(&devices_list_mutex);
+		free(device->namespace);
 		free(device->name);
 		free(device);
@@ -402,4 +556,5 @@
 	device->handle = devmap_create_handle();
 	
+	devmap_namespace_addref(namespace, device);
 	device->driver = driver;
 	
@@ -437,4 +592,6 @@
 static void devmap_forward(ipc_callid_t callid, ipc_call_t *call)
 {
+	fibril_mutex_lock(&devices_list_mutex);
+	
 	/*
 	 * Get handle from request
@@ -450,4 +607,6 @@
 	ipc_forward_fast(callid, dev->driver->phone, dev->handle,
 	    IPC_GET_ARG3(*call), 0, IPC_FF_NONE);
+	
+	fibril_mutex_unlock(&devices_list_mutex);
 }
 
@@ -458,53 +617,34 @@
  *
  */
-static void devmap_get_handle(ipc_callid_t iid, ipc_call_t *icall)
-{
-	/*
-	 * Wait for incoming message with device name (but do not
-	 * read the name itself until the buffer is allocated).
-	 */
-	ipc_callid_t callid;
-	size_t size;
-	if (!async_data_write_receive(&callid, &size)) {
-		ipc_answer_0(callid, EREFUSED);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	if ((size < 1) || (size > DEVMAP_NAME_MAXLEN)) {
-		ipc_answer_0(callid, EINVAL);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	/*
-	 * Allocate buffer for device name.
-	 */
-	char *name = (char *) malloc(size + 1);
-	if (name == NULL) {
-		ipc_answer_0(callid, ENOMEM);
-		ipc_answer_0(iid, EREFUSED);
-		return;
-	}
-	
-	/*
-	 * Send confirmation to sender and get data into buffer.
-	 */
-	ipcarg_t retval = async_data_write_finalize(callid, name, size);
-	if (retval != EOK) {
-		ipc_answer_0(iid, EREFUSED);
-		free(name);
-		return;
-	}
-	name[size] = '\0';
+static void devmap_device_get_handle(ipc_callid_t iid, ipc_call_t *icall)
+{
+	char *fqdn;
+	
+	/* Get fqdn */
+	int rc = async_data_string_receive(&fqdn, DEVMAP_NAME_MAXLEN);
+	if (rc != EOK) {
+		ipc_answer_0(iid, rc);
+		return;
+	}
+	
+	char *ns_name;
+	char *name;
+	if (!devmap_fqdn_split(fqdn, &ns_name, &name)) {
+		free(fqdn);
+		ipc_answer_0(iid, EINVAL);
+		return;
+	}
+	
+	free(fqdn);
 	
 	fibril_mutex_lock(&devices_list_mutex);
 	const devmap_device_t *dev;
+	
 recheck:
-
+	
 	/*
 	 * Find device name in the list of known devices.
 	 */
-	dev = devmap_device_find_name(name);
+	dev = devmap_device_find_name(ns_name, name);
 	
 	/*
@@ -520,4 +660,5 @@
 		
 		ipc_answer_0(iid, ENOENT);
+		free(ns_name);
 		free(name);
 		fibril_mutex_unlock(&devices_list_mutex);
@@ -527,51 +668,96 @@
 	
 	ipc_answer_1(iid, EOK, dev->handle);
+	free(ns_name);
 	free(name);
 }
 
-/** Find name of device identified by id and send it to caller.
- *
- */
-static void devmap_get_name(ipc_callid_t iid, ipc_call_t *icall)
-{
-	const devmap_device_t *device = devmap_device_find_handle(IPC_GET_ARG1(*icall));
+/** Find handle for namespace identified by name.
+ *
+ * In answer will be send EOK and device handle in arg1 or a error
+ * code from errno.h.
+ *
+ */
+static void devmap_namespace_get_handle(ipc_callid_t iid, ipc_call_t *icall)
+{
+	char *name;
+	
+	/* Get device name */
+	int rc = async_data_string_receive(&name, DEVMAP_NAME_MAXLEN);
+	if (rc != EOK) {
+		ipc_answer_0(iid, rc);
+		return;
+	}
+	
+	fibril_mutex_lock(&devices_list_mutex);
+	const devmap_namespace_t *namespace;
+	
+recheck:
 	
 	/*
-	 * Device not found.
+	 * Find namespace name in the list of known namespaces.
 	 */
-	if (device == NULL) {
+	namespace = devmap_namespace_find_name(name);
+	
+	/*
+	 * Namespace was not found.
+	 */
+	if (namespace == NULL) {
+		if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
+			/* Blocking lookup */
+			fibril_condvar_wait(&devices_list_cv,
+			    &devices_list_mutex);
+			goto recheck;
+		}
+		
 		ipc_answer_0(iid, ENOENT);
-		return;
-	}
-	
-	ipc_answer_0(iid, EOK);
-	
-	/* FIXME:
-	 * We have no channel from DEVMAP to client, therefore
-	 * sending must be initiated by client.
-	 *
-	 * size_t name_size = str_size(device->name);
-	 *
-	 * int rc = async_data_write_send(phone, device->name, name_size);
-	 * if (rc != EOK) {
-	 *     async_wait_for(req, NULL);
-	 *     return rc;
-	 * }
-	 */
-	
-	/* TODO: send name in response */
-}
-
-static void devmap_get_count(ipc_callid_t iid, ipc_call_t *icall)
+		free(name);
+		fibril_mutex_unlock(&devices_list_mutex);
+		return;
+	}
+	fibril_mutex_unlock(&devices_list_mutex);
+	
+	ipc_answer_1(iid, EOK, namespace->handle);
+	free(name);
+}
+
+static void devmap_handle_probe(ipc_callid_t iid, ipc_call_t *icall)
 {
 	fibril_mutex_lock(&devices_list_mutex);
-	ipc_answer_1(iid, EOK, list_count(&devices_list));
+	
+	devmap_namespace_t *namespace = devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
+	if (namespace == NULL) {
+		devmap_device_t *dev = devmap_device_find_handle(IPC_GET_ARG1(*icall));
+		if (dev == NULL)
+			ipc_answer_1(iid, EOK, DEV_HANDLE_NONE);
+		else
+			ipc_answer_1(iid, EOK, DEV_HANDLE_DEVICE);
+	} else
+		ipc_answer_1(iid, EOK, DEV_HANDLE_NAMESPACE);
+	
 	fibril_mutex_unlock(&devices_list_mutex);
 }
 
-static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
+static void devmap_get_namespace_count(ipc_callid_t iid, ipc_call_t *icall)
 {
 	fibril_mutex_lock(&devices_list_mutex);
-	
+	ipc_answer_1(iid, EOK, list_count(&namespaces_list));
+	fibril_mutex_unlock(&devices_list_mutex);
+}
+
+static void devmap_get_device_count(ipc_callid_t iid, ipc_call_t *icall)
+{
+	fibril_mutex_lock(&devices_list_mutex);
+	
+	devmap_namespace_t *namespace = devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
+	if (namespace == NULL)
+		ipc_answer_0(iid, EEXISTS);
+	else
+		ipc_answer_1(iid, EOK, namespace->refcnt);
+	
+	fibril_mutex_unlock(&devices_list_mutex);
+}
+
+static void devmap_get_namespaces(ipc_callid_t iid, ipc_call_t *icall)
+{
 	ipc_callid_t callid;
 	size_t size;
@@ -584,9 +770,80 @@
 	if ((size % sizeof(dev_desc_t)) != 0) {
 		ipc_answer_0(callid, EINVAL);
+		ipc_answer_0(iid, EINVAL);
+		return;
+	}
+	
+	fibril_mutex_lock(&devices_list_mutex);
+	
+	size_t count = size / sizeof(dev_desc_t);
+	if (count != list_count(&namespaces_list)) {
+		ipc_answer_0(callid, EOVERFLOW);
+		ipc_answer_0(iid, EOVERFLOW);
+		return;
+	}
+	
+	dev_desc_t *desc = (dev_desc_t *) malloc(size);
+	if (desc == NULL) {
+		ipc_answer_0(callid, ENOMEM);
+		ipc_answer_0(iid, ENOMEM);
+		return;
+	}
+	
+	link_t *item = namespaces_list.next;
+	size_t pos = 0;
+	while (item != &namespaces_list) {
+		devmap_namespace_t *namespace = list_get_instance(item, devmap_namespace_t, namespaces);
+		
+		desc[pos].handle = namespace->handle;
+		str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, namespace->name);
+		pos++;
+		
+		item = item->next;
+	}
+	
+	ipcarg_t retval = async_data_read_finalize(callid, desc, size);
+	
+	free(desc);
+	fibril_mutex_unlock(&devices_list_mutex);
+	
+	ipc_answer_0(iid, retval);
+}
+
+static void devmap_get_devices(ipc_callid_t iid, ipc_call_t *icall)
+{
+	/* FIXME: Use faster algorithm which can make better use
+	   of namespaces */
+	
+	ipc_callid_t callid;
+	size_t size;
+	if (!async_data_read_receive(&callid, &size)) {
+		ipc_answer_0(callid, EREFUSED);
 		ipc_answer_0(iid, EREFUSED);
 		return;
 	}
 	
+	if ((size % sizeof(dev_desc_t)) != 0) {
+		ipc_answer_0(callid, EINVAL);
+		ipc_answer_0(iid, EINVAL);
+		return;
+	}
+	
+	fibril_mutex_lock(&devices_list_mutex);
+	
+	devmap_namespace_t *namespace = devmap_namespace_find_handle(IPC_GET_ARG1(*icall));
+	if (namespace == NULL) {
+		fibril_mutex_unlock(&devices_list_mutex);
+		ipc_answer_0(callid, ENOENT);
+		ipc_answer_0(iid, ENOENT);
+		return;
+	}
+	
 	size_t count = size / sizeof(dev_desc_t);
+	if (count != namespace->refcnt) {
+		ipc_answer_0(callid, EOVERFLOW);
+		ipc_answer_0(iid, EOVERFLOW);
+		return;
+	}
+	
 	dev_desc_t *desc = (dev_desc_t *) malloc(size);
 	if (desc == NULL) {
@@ -596,28 +853,24 @@
 	}
 	
+	link_t *item = devices_list.next;
 	size_t pos = 0;
-	link_t *item = devices_list.next;
-	
-	while ((item != &devices_list) && (pos < count)) {
+	while (item != &devices_list) {
 		devmap_device_t *device = list_get_instance(item, devmap_device_t, devices);
 		
-		desc[pos].handle = device->handle;
-		str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name);
-		pos++;
+		if (device->namespace == namespace) {
+			desc[pos].handle = device->handle;
+			str_cpy(desc[pos].name, DEVMAP_NAME_MAXLEN, device->name);
+			pos++;
+		}
+		
 		item = item->next;
 	}
 	
-	ipcarg_t retval = async_data_read_finalize(callid, desc, pos * sizeof(dev_desc_t));
-	if (retval != EOK) {
-		ipc_answer_0(iid, EREFUSED);
-		free(desc);
-		return;
-	}
+	ipcarg_t retval = async_data_read_finalize(callid, desc, size);
 	
 	free(desc);
-	
 	fibril_mutex_unlock(&devices_list_mutex);
 	
-	ipc_answer_1(iid, EOK, pos);
+	ipc_answer_0(iid, retval);
 }
 
@@ -642,5 +895,14 @@
 	}
 	
-	/* Create NULL device entry */
+	char null[DEVMAP_NAME_MAXLEN];
+	snprintf(null, DEVMAP_NAME_MAXLEN, "%u", i);
+	
+	char *dev_name = str_dup(null);
+	if (dev_name == NULL) {
+		fibril_mutex_unlock(&null_devices_mutex);
+		ipc_answer_0(iid, ENOMEM);
+		return;
+	}
+	
 	devmap_device_t *device = (devmap_device_t *) malloc(sizeof(devmap_device_t));
 	if (device == NULL) {
@@ -650,11 +912,10 @@
 	}
 	
-	char null[DEVMAP_NAME_MAXLEN];
-	snprintf(null, DEVMAP_NAME_MAXLEN, "null%u", i);
-	
-	device->name = str_dup(null);
-	if (device->name == NULL) {
+	fibril_mutex_lock(&devices_list_mutex);
+	
+	devmap_namespace_t *namespace = devmap_namespace_create("null");
+	if (!namespace) {
+		fibril_mutex_lock(&devices_list_mutex);
 		fibril_mutex_unlock(&null_devices_mutex);
-		free(device);
 		ipc_answer_0(iid, ENOMEM);
 		return;
@@ -663,10 +924,11 @@
 	list_initialize(&(device->devices));
 	list_initialize(&(device->driver_devices));
-	
-	fibril_mutex_lock(&devices_list_mutex);
 	
 	/* Get unique device handle */
 	device->handle = devmap_create_handle();
 	device->driver = NULL;
+	
+	devmap_namespace_addref(namespace, device);
+	device->name = dev_name;
 	
 	/* Insert device into list of all devices
@@ -692,5 +954,8 @@
 	}
 	
+	fibril_mutex_lock(&devices_list_mutex);
 	devmap_device_unregister_core(null_devices[i]);
+	fibril_mutex_unlock(&devices_list_mutex);
+	
 	null_devices[i] = NULL;
 	
@@ -725,8 +990,6 @@
 	ipc_answer_0(iid, EOK);
 	
-	devmap_driver_t *driver = NULL;
-	devmap_driver_register(&driver);
-	
-	if (NULL == driver)
+	devmap_driver_t *driver = devmap_driver_register();
+	if (driver == NULL)
 		return;
 	
@@ -755,8 +1018,8 @@
 			break;
 		case DEVMAP_DEVICE_GET_HANDLE:
-			devmap_get_handle(callid, &call);
-			break;
-		case DEVMAP_DEVICE_GET_NAME:
-			devmap_get_name(callid, &call);
+			devmap_device_get_handle(callid, &call);
+			break;
+		case DEVMAP_NAMESPACE_GET_HANDLE:
+			devmap_namespace_get_handle(callid, &call);
 			break;
 		default:
@@ -793,19 +1056,28 @@
 			continue;
 		case DEVMAP_DEVICE_GET_HANDLE:
-			devmap_get_handle(callid, &call);
-			break;
-		case DEVMAP_DEVICE_GET_NAME:
-			devmap_get_name(callid, &call);
-			break;
-		case DEVMAP_DEVICE_NULL_CREATE:
+			devmap_device_get_handle(callid, &call);
+			break;
+		case DEVMAP_NAMESPACE_GET_HANDLE:
+			devmap_namespace_get_handle(callid, &call);
+			break;
+		case DEVMAP_HANDLE_PROBE:
+			devmap_handle_probe(callid, &call);
+			break;
+		case DEVMAP_NULL_CREATE:
 			devmap_null_create(callid, &call);
 			break;
-		case DEVMAP_DEVICE_NULL_DESTROY:
+		case DEVMAP_NULL_DESTROY:
 			devmap_null_destroy(callid, &call);
 			break;
-		case DEVMAP_DEVICE_GET_COUNT:
-			devmap_get_count(callid, &call);
-			break;
-		case DEVMAP_DEVICE_GET_DEVICES:
+		case DEVMAP_GET_NAMESPACE_COUNT:
+			devmap_get_namespace_count(callid, &call);
+			break;
+		case DEVMAP_GET_DEVICE_COUNT:
+			devmap_get_device_count(callid, &call);
+			break;
+		case DEVMAP_GET_NAMESPACES:
+			devmap_get_namespaces(callid, &call);
+			break;
+		case DEVMAP_GET_DEVICES:
 			devmap_get_devices(callid, &call);
 			break;
@@ -867,5 +1139,5 @@
 }
 
-/** 
+/**
  * @}
  */
Index: uspace/srv/fs/devfs/devfs_ops.c
===================================================================
--- uspace/srv/fs/devfs/devfs_ops.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/fs/devfs/devfs_ops.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -42,11 +42,17 @@
 #include <string.h>
 #include <libfs.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <adt/hash_table.h>
+#include <ipc/devmap.h>
 #include <sys/stat.h>
+#include <libfs.h>
+#include <assert.h>
 #include "devfs.h"
 #include "devfs_ops.h"
 
-#define PLB_GET_CHAR(pos)  (devfs_reg.plb_ro[pos % PLB_SIZE])
+typedef struct {
+	devmap_handle_type_t type;
+	dev_handle_t handle;
+} devfs_node_t;
 
 /** Opened devices structure */
@@ -91,4 +97,309 @@
 };
 
+static int devfs_node_get_internal(fs_node_t **rfn, devmap_handle_type_t type,
+    dev_handle_t handle)
+{
+	devfs_node_t *node = (devfs_node_t *) malloc(sizeof(devfs_node_t));
+	if (node == NULL) {
+		*rfn = NULL;
+		return ENOMEM;
+	}
+	
+	*rfn = (fs_node_t *) malloc(sizeof(fs_node_t));
+	if (*rfn == NULL) {
+		free(node);
+		*rfn = NULL;
+		return ENOMEM;
+	}
+	
+	fs_node_initialize(*rfn);
+	node->type = type;
+	node->handle = handle;
+	
+	(*rfn)->data = node;
+	return EOK;
+}
+
+static int devfs_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
+{
+	return devfs_node_get_internal(rfn, DEV_HANDLE_NONE, 0);
+}
+
+static int devfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
+{
+	devfs_node_t *node = (devfs_node_t *) pfn->data;
+	
+	if (node->handle == 0) {
+		/* Root directory */
+		
+		dev_desc_t *devs;
+		size_t count = devmap_get_namespaces(&devs);
+		
+		if (count > 0) {
+			size_t pos;
+			for (pos = 0; pos < count; pos++) {
+				/* Ignore root namespace */
+				if (str_cmp(devs[pos].name, "") == 0)
+					continue;
+				
+				if (str_cmp(devs[pos].name, component) == 0) {
+					free(devs);
+					return devfs_node_get_internal(rfn, DEV_HANDLE_NAMESPACE, devs[pos].handle);
+				}
+			}
+			
+			free(devs);
+		}
+		
+		/* Search root namespace */
+		dev_handle_t namespace;
+		if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
+			count = devmap_get_devices(namespace, &devs);
+			
+			if (count > 0) {
+				size_t pos;
+				for (pos = 0; pos < count; pos++) {
+					if (str_cmp(devs[pos].name, component) == 0) {
+						free(devs);
+						return devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
+					}
+				}
+				
+				free(devs);
+			}
+		}
+		
+		*rfn = NULL;
+		return EOK;
+	}
+	
+	if (node->type == DEV_HANDLE_NAMESPACE) {
+		/* Namespace directory */
+		
+		dev_desc_t *devs;
+		size_t count = devmap_get_devices(node->handle, &devs);
+		if (count > 0) {
+			size_t pos;
+			for (pos = 0; pos < count; pos++) {
+				if (str_cmp(devs[pos].name, component) == 0) {
+					free(devs);
+					return devfs_node_get_internal(rfn, DEV_HANDLE_DEVICE, devs[pos].handle);
+				}
+			}
+			
+			free(devs);
+		}
+		
+		*rfn = NULL;
+		return EOK;
+	}
+	
+	*rfn = NULL;
+	return EOK;
+}
+
+static int devfs_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
+{
+	return devfs_node_get_internal(rfn, devmap_handle_probe(index), index);
+}
+
+static int devfs_node_open(fs_node_t *fn)
+{
+	devfs_node_t *node = (devfs_node_t *) fn->data;
+	
+	if (node->handle == 0) {
+		/* Root directory */
+		return EOK;
+	}
+	
+	devmap_handle_type_t type = devmap_handle_probe(node->handle);
+	
+	if (type == DEV_HANDLE_NAMESPACE) {
+		/* Namespace directory */
+		return EOK;
+	}
+	
+	if (type == DEV_HANDLE_DEVICE) {
+		/* Device node */
+		
+		unsigned long key[] = {
+			[DEVICES_KEY_HANDLE] = (unsigned long) node->handle
+		};
+		
+		fibril_mutex_lock(&devices_mutex);
+		link_t *lnk = hash_table_find(&devices, key);
+		if (lnk == NULL) {
+			device_t *dev = (device_t *) malloc(sizeof(device_t));
+			if (dev == NULL) {
+				fibril_mutex_unlock(&devices_mutex);
+				return ENOMEM;
+			}
+			
+			int phone = devmap_device_connect(node->handle, 0);
+			if (phone < 0) {
+				fibril_mutex_unlock(&devices_mutex);
+				free(dev);
+				return ENOENT;
+			}
+			
+			dev->handle = node->handle;
+			dev->phone = phone;
+			dev->refcount = 1;
+			
+			hash_table_insert(&devices, key, &dev->link);
+		} else {
+			device_t *dev = hash_table_get_instance(lnk, device_t, link);
+			dev->refcount++;
+		}
+		
+		fibril_mutex_unlock(&devices_mutex);
+		
+		return EOK;
+	}
+	
+	return ENOENT;
+}
+
+static int devfs_node_put(fs_node_t *fn)
+{
+	free(fn->data);
+	free(fn);
+	return EOK;
+}
+
+static int devfs_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int lflag)
+{
+	assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
+	
+	*rfn = NULL;
+	return ENOTSUP;
+}
+
+static int devfs_destroy_node(fs_node_t *fn)
+{
+	return ENOTSUP;
+}
+
+static int devfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
+{
+	return ENOTSUP;
+}
+
+static int devfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
+{
+	return ENOTSUP;
+}
+
+static int devfs_has_children(bool *has_children, fs_node_t *fn)
+{
+	devfs_node_t *node = (devfs_node_t *) fn->data;
+	
+	if (node->handle == 0) {
+		size_t count = devmap_count_namespaces();
+		if (count > 0) {
+			*has_children = true;
+			return EOK;
+		}
+		
+		/* Root namespace */
+		dev_handle_t namespace;
+		if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
+			count = devmap_count_devices(namespace);
+			if (count > 0) {
+				*has_children = true;
+				return EOK;
+			}
+		}
+		
+		*has_children = false;
+		return EOK;
+	}
+	
+	if (node->type == DEV_HANDLE_NAMESPACE) {
+		size_t count = devmap_count_devices(node->handle);
+		if (count > 0) {
+			*has_children = true;
+			return EOK;
+		}
+		
+		*has_children = false;
+		return EOK;
+	}
+	
+	*has_children = false;
+	return EOK;
+}
+
+static fs_index_t devfs_index_get(fs_node_t *fn)
+{
+	devfs_node_t *node = (devfs_node_t *) fn->data;
+	return node->handle;
+}
+
+static size_t devfs_size_get(fs_node_t *fn)
+{
+	return 0;
+}
+
+static unsigned int devfs_lnkcnt_get(fs_node_t *fn)
+{
+	devfs_node_t *node = (devfs_node_t *) fn->data;
+	
+	if (node->handle == 0)
+		return 0;
+	
+	return 1;
+}
+
+static char devfs_plb_get_char(unsigned pos)
+{
+	return devfs_reg.plb_ro[pos % PLB_SIZE];
+}
+
+static bool devfs_is_directory(fs_node_t *fn)
+{
+	devfs_node_t *node = (devfs_node_t *) fn->data;
+	
+	return ((node->type == DEV_HANDLE_NONE) || (node->type == DEV_HANDLE_NAMESPACE));
+}
+
+static bool devfs_is_file(fs_node_t *fn)
+{
+	devfs_node_t *node = (devfs_node_t *) fn->data;
+	
+	return (node->type == DEV_HANDLE_DEVICE);
+}
+
+static dev_handle_t devfs_device_get(fs_node_t *fn)
+{
+	devfs_node_t *node = (devfs_node_t *) fn->data;
+	
+	if (node->type == DEV_HANDLE_DEVICE)
+		return node->handle;
+	
+	return 0;
+}
+
+/** libfs operations */
+libfs_ops_t devfs_libfs_ops = {
+	.root_get = devfs_root_get,
+	.match = devfs_match,
+	.node_get = devfs_node_get,
+	.node_open = devfs_node_open,
+	.node_put = devfs_node_put,
+	.create = devfs_create_node,
+	.destroy = devfs_destroy_node,
+	.link = devfs_link_node,
+	.unlink = devfs_unlink_node,
+	.has_children = devfs_has_children,
+	.index_get = devfs_index_get,
+	.size_get = devfs_size_get,
+	.lnkcnt_get = devfs_lnkcnt_get,
+	.plb_get_char = devfs_plb_get_char,
+	.is_directory = devfs_is_directory,
+	.is_file = devfs_is_file,
+	.device_get = devfs_device_get
+};
+
 bool devfs_init(void)
 {
@@ -105,29 +416,14 @@
 void devfs_mounted(ipc_callid_t rid, ipc_call_t *request)
 {
+	char *opts;
+	
 	/* Accept the mount options */
-	ipc_callid_t callid;
-	size_t size;
-	if (!async_data_write_receive(&callid, &size)) {
-		ipc_answer_0(callid, EINVAL);
-		ipc_answer_0(rid, EINVAL);
-		return;
-	}
-	
-	char *opts = malloc(size + 1);
-	if (!opts) {
-		ipc_answer_0(callid, ENOMEM);
-		ipc_answer_0(rid, ENOMEM);
-		return;
-	}
-	
-	ipcarg_t retval = async_data_write_finalize(callid, opts, size);
+	ipcarg_t retval = async_data_string_receive(&opts, 0);
 	if (retval != EOK) {
 		ipc_answer_0(rid, retval);
-		free(opts);
 		return;
 	}
 	
 	free(opts);
-	
 	ipc_answer_3(rid, EOK, 0, 0, 0);
 }
@@ -135,194 +431,111 @@
 void devfs_mount(ipc_callid_t rid, ipc_call_t *request)
 {
-	ipc_answer_0(rid, ENOTSUP);
+	libfs_mount(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
 }
 
 void devfs_lookup(ipc_callid_t rid, ipc_call_t *request)
 {
-	ipcarg_t first = IPC_GET_ARG1(*request);
-	ipcarg_t last = IPC_GET_ARG2(*request);
-	dev_handle_t dev_handle = IPC_GET_ARG3(*request);
-	ipcarg_t lflag = IPC_GET_ARG4(*request);
-	fs_index_t index = IPC_GET_ARG5(*request);
-	
-	/* Hierarchy is flat, no altroot is supported */
-	if (index != 0) {
-		ipc_answer_0(rid, ENOENT);
-		return;
-	}
-	
-	if ((lflag & L_LINK) || (lflag & L_UNLINK)) {
-		ipc_answer_0(rid, ENOTSUP);
-		return;
-	}
-	
-	/* Eat slash */
-	if (PLB_GET_CHAR(first) == '/') {
-		first++;
-		first %= PLB_SIZE;
-	}
-	
-	if (first >= last) {
-		/* Root entry */
-		if (!(lflag & L_FILE))
-			ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0);
-		else
-			ipc_answer_0(rid, ENOENT);
-	} else {
-		if (!(lflag & L_DIRECTORY)) {
-			size_t len;
-			if (last >= first)
-				len = last - first + 1;
-			else
-				len = first + PLB_SIZE - last + 1;
+	libfs_lookup(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
+}
+
+void devfs_open_node(ipc_callid_t rid, ipc_call_t *request)
+{
+	libfs_open_node(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
+}
+
+void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
+{
+	libfs_stat(&devfs_libfs_ops, devfs_reg.fs_handle, rid, request);
+}
+
+void devfs_read(ipc_callid_t rid, ipc_call_t *request)
+{
+	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
+	off_t pos = (off_t) IPC_GET_ARG3(*request);
+	
+	if (index == 0) {
+		ipc_callid_t callid;
+		size_t size;
+		if (!async_data_read_receive(&callid, &size)) {
+			ipc_answer_0(callid, EINVAL);
+			ipc_answer_0(rid, EINVAL);
+			return;
+		}
+		
+		dev_desc_t *desc;
+		size_t count = devmap_get_namespaces(&desc);
+		
+		/* Get rid of root namespace */
+		size_t i;
+		for (i = 0; i < count; i++) {
+			if (str_cmp(desc[i].name, "") == 0) {
+				if (pos >= i)
+					pos++;
+				
+				break;
+			}
+		}
+		
+		if (pos < count) {
+			async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
+			free(desc);
+			ipc_answer_1(rid, EOK, 1);
+			return;
+		}
+		
+		free(desc);
+		pos -= count;
+		
+		/* Search root namespace */
+		dev_handle_t namespace;
+		if (devmap_namespace_get_handle("", &namespace, 0) == EOK) {
+			count = devmap_get_devices(namespace, &desc);
 			
-			char *name = (char *) malloc(len + 1);
-			if (name == NULL) {
-				ipc_answer_0(rid, ENOMEM);
+			if (pos < count) {
+				async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
+				free(desc);
+				ipc_answer_1(rid, EOK, 1);
 				return;
 			}
 			
-			size_t i;
-			for (i = 0; i < len; i++)
-				name[i] = PLB_GET_CHAR(first + i);
-			
-			name[len] = 0;
-			
-			dev_handle_t handle;
-			if (devmap_device_get_handle(name, &handle, 0) != EOK) {
-				free(name);
-				ipc_answer_0(rid, ENOENT);
-				return;
-			}
-			
-			if (lflag & L_OPEN) {
-				unsigned long key[] = {
-					[DEVICES_KEY_HANDLE] = (unsigned long) handle
-				};
-				
-				fibril_mutex_lock(&devices_mutex);
-				link_t *lnk = hash_table_find(&devices, key);
-				if (lnk == NULL) {
-					int phone = devmap_device_connect(handle, 0);
-					if (phone < 0) {
-						fibril_mutex_unlock(&devices_mutex);
-						free(name);
-						ipc_answer_0(rid, ENOENT);
-						return;
-					}
-					
-					device_t *dev = (device_t *) malloc(sizeof(device_t));
-					if (dev == NULL) {
-						fibril_mutex_unlock(&devices_mutex);
-						free(name);
-						ipc_answer_0(rid, ENOMEM);
-						return;
-					}
-					
-					dev->handle = handle;
-					dev->phone = phone;
-					dev->refcount = 1;
-					
-					hash_table_insert(&devices, key, &dev->link);
-				} else {
-					device_t *dev = hash_table_get_instance(lnk, device_t, link);
-					dev->refcount++;
-				}
-				fibril_mutex_unlock(&devices_mutex);
-			}
-			
-			free(name);
-			
-			ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1);
-		} else
-			ipc_answer_0(rid, ENOENT);
-	}
-}
-
-void devfs_open_node(ipc_callid_t rid, ipc_call_t *request)
-{
-	dev_handle_t handle = IPC_GET_ARG2(*request);
-	
-	unsigned long key[] = {
-		[DEVICES_KEY_HANDLE] = (unsigned long) handle
-	};
-	
-	fibril_mutex_lock(&devices_mutex);
-	link_t *lnk = hash_table_find(&devices, key);
-	if (lnk == NULL) {
-		int phone = devmap_device_connect(handle, 0);
-		if (phone < 0) {
-			fibril_mutex_unlock(&devices_mutex);
-			ipc_answer_0(rid, ENOENT);
-			return;
-		}
-		
-		device_t *dev = (device_t *) malloc(sizeof(device_t));
-		if (dev == NULL) {
-			fibril_mutex_unlock(&devices_mutex);
-			ipc_answer_0(rid, ENOMEM);
-			return;
-		}
-		
-		dev->handle = handle;
-		dev->phone = phone;
-		dev->refcount = 1;
-		
-		hash_table_insert(&devices, key, &dev->link);
-	} else {
-		device_t *dev = hash_table_get_instance(lnk, device_t, link);
-		dev->refcount++;
-	}
-	fibril_mutex_unlock(&devices_mutex);
-	
-	ipc_answer_3(rid, EOK, 0, 1, L_FILE);
-}
-
-void devfs_stat(ipc_callid_t rid, ipc_call_t *request)
-{
-	dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
-	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
-	
-	ipc_callid_t callid;
-	size_t size;
-	if (!async_data_read_receive(&callid, &size) ||
-	    size != sizeof(struct stat)) {
-		ipc_answer_0(callid, EINVAL);
-		ipc_answer_0(rid, EINVAL);
-		return;
-	}
-
-	struct stat stat;
-	memset(&stat, 0, sizeof(struct stat));
-
-	stat.fs_handle = devfs_reg.fs_handle;
-	stat.dev_handle = dev_handle;
-	stat.index = index;
-	stat.lnkcnt = 1;
-	stat.is_file = (index != 0);
-	stat.size = 0;
-	
-	if (index != 0) {
-		unsigned long key[] = {
-			[DEVICES_KEY_HANDLE] = (unsigned long) index
-		};
-		
-		fibril_mutex_lock(&devices_mutex);
-		link_t *lnk = hash_table_find(&devices, key);
-		if (lnk != NULL) 
-			stat.devfs_stat.device = (dev_handle_t)index;
-		fibril_mutex_unlock(&devices_mutex);
-	}
-
-	async_data_read_finalize(callid, &stat, sizeof(struct stat));
-	ipc_answer_0(rid, EOK);
-}
-
-void devfs_read(ipc_callid_t rid, ipc_call_t *request)
-{
-	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
-	off_t pos = (off_t) IPC_GET_ARG3(*request);
-	
-	if (index != 0) {
+			free(desc);
+		}
+		
+		ipc_answer_0(callid, ENOENT);
+		ipc_answer_1(rid, ENOENT, 0);
+		return;
+	}
+	
+	devmap_handle_type_t type = devmap_handle_probe(index);
+	
+	if (type == DEV_HANDLE_NAMESPACE) {
+		/* Namespace directory */
+		ipc_callid_t callid;
+		size_t size;
+		if (!async_data_read_receive(&callid, &size)) {
+			ipc_answer_0(callid, EINVAL);
+			ipc_answer_0(rid, EINVAL);
+			return;
+		}
+		
+		dev_desc_t *desc;
+		size_t count = devmap_get_devices(index, &desc);
+		
+		if (pos < count) {
+			async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
+			free(desc);
+			ipc_answer_1(rid, EOK, 1);
+			return;
+		}
+		
+		free(desc);
+		ipc_answer_0(callid, ENOENT);
+		ipc_answer_1(rid, ENOENT, 0);
+		return;
+	}
+	
+	if (type == DEV_HANDLE_DEVICE) {
+		/* Device node */
+		
 		unsigned long key[] = {
 			[DEVICES_KEY_HANDLE] = (unsigned long) index
@@ -364,35 +577,8 @@
 		/* Driver reply is the final result of the whole operation */
 		ipc_answer_1(rid, rc, bytes);
-	} else {
-		ipc_callid_t callid;
-		size_t size;
-		if (!async_data_read_receive(&callid, &size)) {
-			ipc_answer_0(callid, EINVAL);
-			ipc_answer_0(rid, EINVAL);
-			return;
-		}
-		
-		size_t count = devmap_device_get_count();
-		dev_desc_t *desc = malloc(count * sizeof(dev_desc_t));
-		if (desc == NULL) {
-			ipc_answer_0(callid, ENOMEM);
-			ipc_answer_1(rid, ENOMEM, 0);
-			return;
-		}
-		
-		size_t max = devmap_device_get_devices(count, desc);
-		
-		if (pos < max) {
-			async_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1);
-		} else {
-			ipc_answer_0(callid, ENOENT);
-			ipc_answer_1(rid, ENOENT, 0);
-			return;
-		}
-		
-		free(desc);
-		
-		ipc_answer_1(rid, EOK, 1);
-	}
+		return;
+	}
+	
+	ipc_answer_0(rid, ENOENT);
 }
 
@@ -402,5 +588,19 @@
 	off_t pos = (off_t) IPC_GET_ARG3(*request);
 	
-	if (index != 0) {
+	if (index == 0) {
+		ipc_answer_0(rid, ENOTSUP);
+		return;
+	}
+	
+	devmap_handle_type_t type = devmap_handle_probe(index);
+	
+	if (type == DEV_HANDLE_NAMESPACE) {
+		/* Namespace directory */
+		ipc_answer_0(rid, ENOTSUP);
+		return;
+	}
+	
+	if (type == DEV_HANDLE_DEVICE) {
+		/* Device node */
 		unsigned long key[] = {
 			[DEVICES_KEY_HANDLE] = (unsigned long) index
@@ -443,8 +643,8 @@
 		/* Driver reply is the final result of the whole operation */
 		ipc_answer_1(rid, rc, bytes);
-	} else {
-		/* Read-only filesystem */
-		ipc_answer_0(rid, ENOTSUP);
-	}
+		return;
+	}
+	
+	ipc_answer_0(rid, ENOENT);
 }
 
@@ -458,5 +658,18 @@
 	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
 	
-	if (index != 0) {
+	if (index == 0) {
+		ipc_answer_0(rid, EOK);
+		return;
+	}
+	
+	devmap_handle_type_t type = devmap_handle_probe(index);
+	
+	if (type == DEV_HANDLE_NAMESPACE) {
+		/* Namespace directory */
+		ipc_answer_0(rid, EOK);
+		return;
+	}
+	
+	if (type == DEV_HANDLE_DEVICE) {
 		unsigned long key[] = {
 			[DEVICES_KEY_HANDLE] = (unsigned long) index
@@ -482,6 +695,8 @@
 		
 		ipc_answer_0(rid, EOK);
-	} else
-		ipc_answer_0(rid, ENOTSUP);
+		return;
+	}
+	
+	ipc_answer_0(rid, ENOENT);
 }
 
@@ -490,5 +705,18 @@
 	fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
 	
-	if (index != 0) {
+	if (index == 0) {
+		ipc_answer_0(rid, EOK);
+		return;
+	}
+	
+	devmap_handle_type_t type = devmap_handle_probe(index);
+	
+	if (type == DEV_HANDLE_NAMESPACE) {
+		/* Namespace directory */
+		ipc_answer_0(rid, EOK);
+		return;
+	}
+	
+	if (type == DEV_HANDLE_DEVICE) {
 		unsigned long key[] = {
 			[DEVICES_KEY_HANDLE] = (unsigned long) index
@@ -518,6 +746,8 @@
 		/* Driver reply is the final result of the whole operation */
 		ipc_answer_0(rid, rc);
-	} else
-		ipc_answer_0(rid, ENOTSUP);
+		return;
+	}
+	
+	ipc_answer_0(rid, ENOENT);
 }
 
Index: uspace/srv/fs/fat/fat.h
===================================================================
--- uspace/srv/fs/fat/fat.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/fs/fat/fat.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,5 +36,5 @@
 #include "fat_fat.h"
 #include <ipc/ipc.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <libfs.h>
 #include <atomic.h>
Index: uspace/srv/fs/fat/fat_fat.c
===================================================================
--- uspace/srv/fs/fat/fat_fat.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/fs/fat/fat_fat.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -46,5 +46,5 @@
 #include <align.h>
 #include <assert.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <mem.h>
 
Index: uspace/srv/fs/fat/fat_idx.c
===================================================================
--- uspace/srv/fs/fat/fat_idx.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/fs/fat/fat_idx.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -43,5 +43,5 @@
 #include <adt/list.h>
 #include <assert.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 
 /** Each instance of this type describes one interval of freed VFS indices. */
Index: uspace/srv/fs/fat/fat_ops.c
===================================================================
--- uspace/srv/fs/fat/fat_ops.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/fs/fat/fat_ops.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -52,5 +52,5 @@
 #include <adt/list.h>
 #include <assert.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <sys/mman.h>
 #include <align.h>
@@ -71,4 +71,5 @@
 static int fat_match(fs_node_t **, fs_node_t *, const char *);
 static int fat_node_get(fs_node_t **, dev_handle_t, fs_index_t);
+static int fat_node_open(fs_node_t *);
 static int fat_node_put(fs_node_t *);
 static int fat_create_node(fs_node_t **, dev_handle_t, int);
@@ -83,4 +84,5 @@
 static bool fat_is_directory(fs_node_t *);
 static bool fat_is_file(fs_node_t *node);
+static dev_handle_t fat_device_get(fs_node_t *node);
 
 /*
@@ -407,4 +409,13 @@
 }
 
+int fat_node_open(fs_node_t *fn)
+{
+	/*
+	 * Opening a file is stateless, nothing
+	 * to be done here.
+	 */
+	return EOK;
+}
+
 int fat_node_put(fs_node_t *fn)
 {
@@ -867,4 +878,9 @@
 }
 
+dev_handle_t fat_device_get(fs_node_t *node)
+{
+	return 0;
+}
+
 /** libfs operations */
 libfs_ops_t fat_libfs_ops = {
@@ -872,4 +888,5 @@
 	.match = fat_match,
 	.node_get = fat_node_get,
+	.node_open = fat_node_open,
 	.node_put = fat_node_put,
 	.create = fat_create_node,
@@ -881,7 +898,8 @@
 	.size_get = fat_size_get,
 	.lnkcnt_get = fat_lnkcnt_get,
-	.plb_get_char =	fat_plb_get_char,
+	.plb_get_char = fat_plb_get_char,
 	.is_directory = fat_is_directory,
-	.is_file = fat_is_file
+	.is_file = fat_is_file,
+	.device_get = fat_device_get
 };
 
Index: uspace/srv/fs/tmpfs/tmpfs_ops.c
===================================================================
--- uspace/srv/fs/tmpfs/tmpfs_ops.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/fs/tmpfs/tmpfs_ops.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -69,4 +69,5 @@
 static int tmpfs_match(fs_node_t **, fs_node_t *, const char *);
 static int tmpfs_node_get(fs_node_t **, dev_handle_t, fs_index_t);
+static int tmpfs_node_open(fs_node_t *);
 static int tmpfs_node_put(fs_node_t *);
 static int tmpfs_create_node(fs_node_t **, dev_handle_t, int);
@@ -117,4 +118,9 @@
 }
 
+static dev_handle_t tmpfs_device_get(fs_node_t *fn)
+{
+	return 0;
+}
+
 /** libfs operations */
 libfs_ops_t tmpfs_libfs_ops = {
@@ -122,4 +128,5 @@
 	.match = tmpfs_match,
 	.node_get = tmpfs_node_get,
+	.node_open = tmpfs_node_open,
 	.node_put = tmpfs_node_put,
 	.create = tmpfs_create_node,
@@ -133,5 +140,6 @@
 	.plb_get_char = tmpfs_plb_get_char,
 	.is_directory = tmpfs_is_directory,
-	.is_file = tmpfs_is_file
+	.is_file = tmpfs_is_file,
+	.device_get = tmpfs_device_get
 };
 
@@ -241,4 +249,10 @@
 	}
 	return EOK;	
+}
+
+int tmpfs_node_open(fs_node_t *fn)
+{
+	/* nothing to do */
+	return EOK;
 }
 
Index: uspace/srv/kbd/generic/kbd.c
===================================================================
--- uspace/srv/kbd/generic/kbd.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/kbd/generic/kbd.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -50,4 +50,5 @@
 #include <io/console.h>
 #include <io/keycode.h>
+#include <devmap.h>
 
 #include <kbd.h>
@@ -56,7 +57,7 @@
 #include <layout.h>
 
-#define NAME "kbd"
-
-int cons_connected = 0;
+#define NAME       "kbd"
+#define NAMESPACE  "hid_in"
+
 int phone2cons = -1;
 
@@ -172,9 +173,4 @@
 	int retval;
 
-	if (cons_connected) {
-		ipc_answer_0(iid, ELIMIT);
-		return;
-	}
-	cons_connected = 1;
 	ipc_answer_0(iid, EOK);
 
@@ -183,7 +179,9 @@
 		switch (IPC_GET_METHOD(call)) {
 		case IPC_M_PHONE_HUNGUP:
-			cons_connected = 0;
-			ipc_hangup(phone2cons);
-			phone2cons = -1;
+			if (phone2cons != -1) {
+				ipc_hangup(phone2cons);
+				phone2cons = -1;
+			}
+			
 			ipc_answer_0(callid, EOK);
 			return;
@@ -216,6 +214,4 @@
 	printf(NAME ": HelenOS Keyboard service\n");
 	
-	ipcarg_t phonead;
-	
 	if (sysinfo_value("kbd.cir.fhc") == 1)
 		cir_service = SERVICE_FHC;
@@ -241,10 +237,20 @@
 	layout[active_layout]->reset();
 	
-	async_set_client_connection(console_connection);
-
-	/* Register service at nameserver. */
-	if (ipc_connect_to_me(PHONE_NS, SERVICE_KEYBOARD, 0, 0, &phonead) != 0)
-		return -1;
-	
+	/* Register driver */
+	int rc = devmap_driver_register(NAME, console_connection);
+	if (rc < 0) {
+		printf(NAME ": Unable to register driver (%d)\n", rc);
+		return -1;
+	}
+	
+	char kbd[DEVMAP_NAME_MAXLEN + 1];
+	snprintf(kbd, DEVMAP_NAME_MAXLEN, "%s/%s", NAMESPACE, NAME);
+	
+	dev_handle_t dev_handle;
+	if (devmap_device_register(kbd, &dev_handle) != EOK) {
+		printf(NAME ": Unable to register device %s\n", kbd);
+		return -1;
+	}
+
 	printf(NAME ": Accepting connections\n");
 	async_manager();
Index: uspace/srv/loader/arch/arm32/arm32.s
===================================================================
--- uspace/srv/loader/arch/arm32/arm32.s	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/loader/arch/arm32/arm32.s	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,4 +36,7 @@
 # Jump to a program entry point
 program_run:
+	# load ras_page address to r2
+	ldr r2, =ras_page
+	ldr r2, [r2]
 	# pcb is passed to the entry point in r1 (where it already is)
 	mov r15, r0
Index: uspace/srv/part/mbr_part/mbr_part.c
===================================================================
--- uspace/srv/part/mbr_part/mbr_part.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/part/mbr_part/mbr_part.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -61,5 +61,5 @@
 #include <async.h>
 #include <as.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <devmap.h>
 #include <sys/types.h>
Index: uspace/srv/vfs/vfs.h
===================================================================
--- uspace/srv/vfs/vfs.h	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/vfs/vfs.h	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -36,5 +36,5 @@
 #include <ipc/ipc.h>
 #include <adt/list.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <sys/types.h>
 #include <devmap.h>
Index: uspace/srv/vfs/vfs_file.c
===================================================================
--- uspace/srv/vfs/vfs_file.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/vfs/vfs_file.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -42,5 +42,5 @@
 #include <bool.h>
 #include <fibril.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include "vfs.h"
 
@@ -89,5 +89,5 @@
 	unsigned int i;
 	if (desc)
-		i = MAX_OPEN_FILES;
+		i = MAX_OPEN_FILES - 1;
 	else
 		i = 0;
@@ -111,5 +111,5 @@
 			i--;
 		} else {
-			if (i == MAX_OPEN_FILES)
+			if (i == MAX_OPEN_FILES - 1)
 				break;
 			
Index: uspace/srv/vfs/vfs_lookup.c
===================================================================
--- uspace/srv/vfs/vfs_lookup.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/vfs/vfs_lookup.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -43,5 +43,5 @@
 #include <stdarg.h>
 #include <bool.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <adt/list.h>
 #include <vfs/canonify.h>
Index: uspace/srv/vfs/vfs_node.c
===================================================================
--- uspace/srv/vfs/vfs_node.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/vfs/vfs_node.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -39,5 +39,5 @@
 #include <stdlib.h>
 #include <string.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <adt/hash_table.h>
 #include <assert.h>
Index: uspace/srv/vfs/vfs_ops.c
===================================================================
--- uspace/srv/vfs/vfs_ops.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/vfs/vfs_ops.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -44,5 +44,5 @@
 #include <string.h>
 #include <bool.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <adt/list.h>
 #include <unistd.h>
Index: uspace/srv/vfs/vfs_register.c
===================================================================
--- uspace/srv/vfs/vfs_register.c	(revision 245e8399f3d58ad45fc1730f6b948b33ae3ab8df)
+++ uspace/srv/vfs/vfs_register.c	(revision eacb957f6c30d6c807f9a5ca0a3e535e59262c52)
@@ -46,5 +46,5 @@
 #include <ctype.h>
 #include <bool.h>
-#include <fibril_sync.h>
+#include <fibril_synch.h>
 #include <adt/list.h>
 #include <as.h>
