Index: uspace/app/init/Makefile
===================================================================
--- uspace/app/init/Makefile	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/init/Makefile	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -30,4 +30,5 @@
 USPACE_PREFIX = ../..
 BINARY = init
+STATIC_NEEDED = y
 
 SOURCES = \
Index: uspace/app/klog/klog.c
===================================================================
--- uspace/app/klog/klog.c	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/klog/klog.c	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -44,8 +44,20 @@
 #include <io/klog.h>
 #include <sysinfo.h>
+#include <malloc.h>
 #include <fibril_synch.h>
+#include <adt/list.h>
+#include <adt/prodcons.h>
 
 #define NAME       "klog"
 #define LOG_FNAME  "/log/klog"
+
+/* Producer/consumer buffers */
+typedef struct {
+	link_t link;
+	size_t length;
+	wchar_t *data;
+} item_t;
+
+static prodcons_t pc;
 
 /* Pointer to klog area */
@@ -53,34 +65,120 @@
 static size_t klog_length;
 
-static FILE *log;
-
-/* Serialize the output a bit. This will not avoid messed-up log completely
-   but chances for are pretty high (experimentally confirmed). */
-static FIBRIL_MUTEX_INITIALIZE(log_mutex);
-
-static void interrupt_received(ipc_callid_t callid, ipc_call_t *call)
-{
-	fibril_mutex_lock(&log_mutex);
+/* Notification mutex */
+static FIBRIL_MUTEX_INITIALIZE(mtx);
+
+/** Klog producer
+ *
+ * Copies the contents of a character buffer to local
+ * producer/consumer queue.
+ *
+ * @param length Number of characters to copy.
+ * @param data   Pointer to the kernel klog buffer.
+ *
+ */
+static void producer(size_t length, wchar_t *data)
+{
+	item_t *item = (item_t *) malloc(sizeof(item_t));
+	if (item == NULL)
+		return;
+	
+	size_t sz = sizeof(wchar_t) * length;
+	wchar_t *buf = (wchar_t *) malloc(sz);
+	if (data == NULL) {
+		free(item);
+		return;
+	}
+	
+	memcpy(buf, data, sz);
+	
+	link_initialize(&item->link);
+	item->length = length;
+	item->data = buf;
+	prodcons_produce(&pc, &item->link);
+}
+
+/** Klog consumer
+ *
+ * Waits in an infinite loop for the character data created by
+ * the producer and outputs them to stdout and optionally into
+ * a file.
+ *
+ * @param data Unused.
+ *
+ * @return Always EOK (unreachable).
+ *
+ */
+static int consumer(void *data)
+{
+	FILE *log = fopen(LOG_FNAME, "a");
+	if (log == NULL)
+		printf("%s: Unable to create log file %s (%s)\n", NAME, LOG_FNAME,
+		    str_error(errno));
+	
+	while (true) {
+		link_t *link = prodcons_consume(&pc);
+		item_t *item = list_get_instance(link, item_t, link);
+		
+		for (size_t i = 0; i < item->length; i++)
+			putchar(item->data[i]);
+		
+		if (log != NULL) {
+			for (size_t i = 0; i < item->length; i++)
+				fputc(item->data[i], log);
+			
+			fflush(log);
+			fsync(fileno(log));
+		}
+		
+		free(item->data);
+		free(item);
+	}
+	
+	fclose(log);
+	return EOK;
+}
+
+/** Kernel notification handler
+ *
+ * Receives kernel klog notifications.
+ *
+ * @param callid IPC call ID.
+ * @param call   IPC call structure.
+ *
+ */
+static void notification_received(ipc_callid_t callid, ipc_call_t *call)
+{
+	/*
+	 * Make sure we process only a single notification
+	 * at any time to limit the chance of the consumer
+	 * starving.
+	 *
+	 * Note: Usually the automatic masking of the klog
+	 * notifications on the kernel side does the trick
+	 * of limiting the chance of accidentally copying
+	 * the same data multiple times. However, due to
+	 * the non-blocking architecture of klog notifications,
+	 * this possibility cannot be generally avoided.
+	 */
+	
+	fibril_mutex_lock(&mtx);
 	
 	size_t klog_start = (size_t) IPC_GET_ARG1(*call);
 	size_t klog_len = (size_t) IPC_GET_ARG2(*call);
 	size_t klog_stored = (size_t) IPC_GET_ARG3(*call);
-	size_t i;
-	
-	for (i = klog_len - klog_stored; i < klog_len; i++) {
-		wchar_t ch = klog[(klog_start + i) % klog_length];
-		
-		putchar(ch);
-		
-		if (log != NULL)
-			fputc(ch, log);
-	}
-	
-	if (log != NULL) {
-		fflush(log);
-		fsync(fileno(log));
-	}
-	
-	fibril_mutex_unlock(&log_mutex);
+	
+	size_t offset = (klog_start + klog_len - klog_stored) % klog_length;
+	
+	/* Copy data from the ring buffer */
+	if (offset + klog_stored >= klog_length) {
+		size_t split = klog_length - offset;
+		
+		producer(split, klog + offset);
+		producer(klog_stored - split, klog);
+	} else
+		producer(klog_stored, klog + offset);
+	
+	event_unmask(EVENT_KLOG);
+	fibril_mutex_unlock(&mtx);
 }
 
@@ -120,4 +218,6 @@
 	}
 	
+	prodcons_initialize(&pc);
+	async_set_interrupt_received(notification_received);
 	rc = event_subscribe(EVENT_KLOG, 0);
 	if (rc != EOK) {
@@ -127,15 +227,16 @@
 	}
 	
-	/*
-	 * Mode "a" would be definitively much better here, but it is
-	 * not well supported by the FAT driver.
-	 */
-	log = fopen(LOG_FNAME, "w");
-	if (log == NULL)
-		printf("%s: Unable to create log file %s (%s)\n", NAME, LOG_FNAME,
-		    str_error(errno));
-	
-	async_set_interrupt_received(interrupt_received);
+	fid_t fid = fibril_create(consumer, NULL);
+	if (!fid) {
+		fprintf(stderr, "%s: Unable to create consumer fibril\n",
+		    NAME);
+		return ENOMEM;
+	}
+	
+	fibril_add_ready(fid);
+	event_unmask(EVENT_KLOG);
 	klog_update();
+	
+	task_retval(0);
 	async_manager();
 	
Index: uspace/app/stats/stats.c
===================================================================
--- uspace/app/stats/stats.c	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/stats/stats.c	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -69,13 +69,19 @@
 	size_t i;
 	for (i = 0; i < count; i++) {
-		uint64_t resmem, virtmem, ucycles, kcycles;
-		char resmem_suffix, virtmem_suffix, usuffix, ksuffix;
-		
-		order_suffix(stats_tasks[i].resmem, &resmem, &resmem_suffix);
-		order_suffix(stats_tasks[i].virtmem, &virtmem, &virtmem_suffix);
+		uint64_t resmem;
+		uint64_t virtmem;
+		uint64_t ucycles;
+		uint64_t kcycles;
+		const char *resmem_suffix;
+		const char *virtmem_suffix;
+		char usuffix;
+		char ksuffix;
+		
+		bin_order_suffix(stats_tasks[i].resmem, &resmem, &resmem_suffix, true);
+		bin_order_suffix(stats_tasks[i].virtmem, &virtmem, &virtmem_suffix, true);
 		order_suffix(stats_tasks[i].ucycles, &ucycles, &usuffix);
 		order_suffix(stats_tasks[i].kcycles, &kcycles, &ksuffix);
 		
-		printf("%-8" PRIu64 " %7zu %9" PRIu64 "%c %8" PRIu64 "%c"
+		printf("%-8" PRIu64 " %7zu %7" PRIu64 "%s %6" PRIu64 "%s"
 		    " %8" PRIu64 "%c %8" PRIu64 "%c %s\n",
 		    stats_tasks[i].task_id, stats_tasks[i].threads,
Index: uspace/app/tester/Makefile
===================================================================
--- uspace/app/tester/Makefile	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/tester/Makefile	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -49,4 +49,5 @@
 	loop/loop1.c \
 	mm/malloc1.c \
+	mm/malloc2.c \
 	mm/mapping1.c \
 	devs/devman1.c \
Index: uspace/app/tester/mm/malloc2.c
===================================================================
--- uspace/app/tester/mm/malloc2.c	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
+++ uspace/app/tester/mm/malloc2.c	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2011 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 <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <malloc.h>
+#include "../tester.h"
+
+const char *test_malloc2(void)
+{
+	int cnt = 0;
+	char *p;
+
+	TPRINTF("Provoking the kernel into overcommitting memory to us...\n");
+	while ((p = malloc(1024 * 1024))) {
+		TPRINTF("%dM ", ++cnt);
+		*p = 'A';
+	}
+	TPRINTF("\nWas refused more memory as expected.\n");
+
+	return NULL;
+}
Index: uspace/app/tester/mm/malloc2.def
===================================================================
--- uspace/app/tester/mm/malloc2.def	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
+++ uspace/app/tester/mm/malloc2.def	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -0,0 +1,6 @@
+{
+	"malloc2",
+	"Memory reservation feature test",
+	&test_malloc2,
+	false	
+},
Index: uspace/app/tester/tester.c
===================================================================
--- uspace/app/tester/tester.c	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/tester/tester.c	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -62,4 +62,5 @@
 #include "loop/loop1.def"
 #include "mm/malloc1.def"
+#include "mm/malloc2.def"
 #include "mm/mapping1.def"
 #include "hw/serial/serial1.def"
Index: uspace/app/tester/tester.h
===================================================================
--- uspace/app/tester/tester.h	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/tester/tester.h	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -78,4 +78,5 @@
 extern const char *test_loop1(void);
 extern const char *test_malloc1(void);
+extern const char *test_malloc2(void);
 extern const char *test_mapping1(void);
 extern const char *test_serial1(void);
Index: uspace/app/top/screen.c
===================================================================
--- uspace/app/top/screen.c	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/top/screen.c	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -254,16 +254,16 @@
 	uint64_t used;
 	uint64_t free;
-	char total_suffix;
-	char unavail_suffix;
-	char used_suffix;
-	char free_suffix;
-	
-	order_suffix(data->physmem->total, &total, &total_suffix);
-	order_suffix(data->physmem->unavail, &unavail, &unavail_suffix);
-	order_suffix(data->physmem->used, &used, &used_suffix);
-	order_suffix(data->physmem->free, &free, &free_suffix);
-	
-	printf("memory: %" PRIu64 "%c total, %" PRIu64 "%c unavail, %"
-	    PRIu64 "%c used, %" PRIu64 "%c free", total, total_suffix,
+	const char *total_suffix;
+	const char *unavail_suffix;
+	const char *used_suffix;
+	const char *free_suffix;
+	
+	bin_order_suffix(data->physmem->total, &total, &total_suffix, false);
+	bin_order_suffix(data->physmem->unavail, &unavail, &unavail_suffix, false);
+	bin_order_suffix(data->physmem->used, &used, &used_suffix, false);
+	bin_order_suffix(data->physmem->free, &free, &free_suffix, false);
+	
+	printf("memory: %" PRIu64 "%s total, %" PRIu64 "%s unavail, %"
+	    PRIu64 "%s used, %" PRIu64 "%s free", total, total_suffix,
 	    unavail, unavail_suffix, used, used_suffix, free, free_suffix);
 	screen_newline();
@@ -295,15 +295,15 @@
 		
 		uint64_t resmem;
-		char resmem_suffix;
-		order_suffix(task->resmem, &resmem, &resmem_suffix);
+		const char *resmem_suffix;
+		bin_order_suffix(task->resmem, &resmem, &resmem_suffix, true);
 		
 		uint64_t virtmem;
-		char virtmem_suffix;
-		order_suffix(task->virtmem, &virtmem, &virtmem_suffix);
-		
-		printf("%-8" PRIu64 " %7zu %9" PRIu64 "%c ",
+		const char *virtmem_suffix;
+		bin_order_suffix(task->virtmem, &virtmem, &virtmem_suffix, true);
+		
+		printf("%-8" PRIu64 " %7zu %7" PRIu64 "%s ",
 		    task->task_id, task->threads, resmem, resmem_suffix);
 		print_percent(perc->resmem, 2);
-		printf(" %8" PRIu64 "%c ", virtmem, virtmem_suffix);
+		printf(" %6" PRIu64 "%s ", virtmem, virtmem_suffix);
 		print_percent(perc->virtmem, 2);
 		puts(" ");
Index: uspace/app/vuhid/device.c
===================================================================
--- uspace/app/vuhid/device.c	(revision 72cd53d4a534c31423276f4e863f774083b559ee)
+++ uspace/app/vuhid/device.c	(revision d736fe38a9f7781f4f215851ebdd9bb698510061)
@@ -36,4 +36,5 @@
 #include "virthid.h"
 #include <errno.h>
+#include <stdio.h>
 #include <str.h>
 #include <assert.h>
