Index: kernel/arch/ia32/src/asm.S
===================================================================
--- kernel/arch/ia32/src/asm.S	(revision 7e956eb53e39a5b6b5da0e92b2e789a596d7fc45)
+++ kernel/arch/ia32/src/asm.S	(revision 6c383b0276be208b47b6cf306a8fcdbe62d15436)
@@ -29,7 +29,7 @@
 ## very low and hardware-level functions
 
-# Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word
-# and 1 means interrupt with error word
-#define ERROR_WORD_INTERRUPT_LIST 0x00027D00
+# Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error
+# word and 1 means interrupt with error word
+#define ERROR_WORD_INTERRUPT_LIST 0x00027d00
 
 .text
@@ -66,25 +66,25 @@
 memcpy_from_uspace:
 memcpy_to_uspace:
-	movl %edi, %edx				/* save %edi */
-	movl %esi, %eax				/* save %esi */
+	movl %edi, %edx			/* save %edi */
+	movl %esi, %eax			/* save %esi */
 	
 	movl MEMCPY_SIZE(%esp), %ecx
-	shrl $2, %ecx				/* size / 4 */
+	shrl $2, %ecx			/* size / 4 */
 	
 	movl MEMCPY_DST(%esp), %edi
 	movl MEMCPY_SRC(%esp), %esi
 	
-	rep movsl				/* copy as much as possible word by word */
+	rep movsl			/* copy whole words */
 
 	movl MEMCPY_SIZE(%esp), %ecx
-	andl $3, %ecx				/* size % 4 */
+	andl $3, %ecx			/* size % 4 */
 	jz 0f
 	
-	rep movsb				/* copy the rest byte by byte */
+	rep movsb			/* copy the rest byte by byte */
 
 0:
 	movl %edx, %edi
 	movl %eax, %esi
-	movl MEMCPY_SRC(%esp), %eax		/* MEMCPY_SRC(%esp), success */
+	movl MEMCPY_SRC(%esp), %eax	/* MEMCPY_SRC(%esp), success */
 	ret
 	
@@ -97,5 +97,5 @@
 	movl %edx, %edi
 	movl %eax, %esi
-	xorl %eax, %eax				/* return 0, failure */
+	xorl %eax, %eax			/* return 0, failure */
 	ret
 
@@ -105,7 +105,8 @@
 #
 paging_on:
-	movl %cr0,%edx
-	orl $(1<<31),%edx		# paging on
-	andl $~((1<<30)|(1<<29)),%edx	# clear Cache Disable and not Write Though
+	movl %cr0, %edx
+	orl $(1 << 31), %edx		# paging on
+	# clear Cache Disable and not Write Though
+	andl $~((1 << 30) | (1 << 29)), %edx
 	movl %edx,%cr0
 	jmp 0f
@@ -119,13 +120,9 @@
 #
 enable_l_apic_in_msr:
-	push %eax
-
 	movl $0x1b, %ecx
 	rdmsr
-	orl $(1<<11),%eax
-	orl $(0xfee00000),%eax
+	orl $(1 << 11), %eax
+	orl $(0xfee00000), %eax
 	wrmsr
-
-	pop %eax
 	ret
 
@@ -135,5 +132,5 @@
 	pushfl
 	pop %ecx
-	and $0xffffbfff,%ecx
+	and $0xffffbfff, %ecx
 	push %ecx
 	popfl
@@ -151,32 +148,40 @@
 .macro handler i n
 
-.ifeq \i-0x30     # Syscall handler
-	push %ds
-	push %es
-	push %fs
-	push %gs
-
-	# Push arguments on stack
-	push %edi
-	push %esi
-	push %edx
-	push %ecx
-	push %eax
+.ifeq \i - 0x30     # Syscall handler
+	pushl %ds
+	pushl %es
+	pushl %fs
+	pushl %gs
+
+	#
+	# Push syscall arguments onto the stack
+	#
+	# NOTE: The idea behind the order of arguments passed in registers is to
+	#	use all scratch registers first and preserved registers next.
+	#	An optimized libc syscall wrapper can make use of this setup.
+	#
+	pushl %eax
+	pushl %ebp
+	pushl %edi
+	pushl %esi
+	pushl %ebx
+	pushl %ecx
+	pushl %edx
 	
 	# we must fill the data segment registers
-	movw $16,%ax
-	movw %ax,%ds
-	movw %ax,%es
+	movw $16, %ax
+	movw %ax, %ds
+	movw %ax, %es
 	
 	sti
-	
-	call syscall_handler   # syscall_handler(ax,cx,dx,si,di)
+	# syscall_handler(edx, ecx, ebx, esi, edi, ebp, eax)
+	call syscall_handler   
 	cli
-	addl $20, %esp         # clean-up of parameters
-	
-	pop %gs
-	pop %fs
-	pop %es
-	pop %ds
+	addl $28, %esp         # clean-up of parameters
+	
+	popl %gs
+	popl %fs
+	popl %es
+	popl %ds
 	
 	CLEAR_NT_FLAG
@@ -189,5 +194,5 @@
 	 * handlers and istate_t can be the same for both types.
 	 */
-	.iflt \i-32
+	.iflt \i - 32
 		.if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
 			/* 
@@ -207,62 +212,62 @@
 	.endif
 	
-	push %ds
-	push %es
-	push %fs
-	push %gs
+	pushl %ds
+	pushl %es
+	pushl %fs
+	pushl %gs
 
 #ifdef CONFIG_DEBUG_ALLREGS
-	push %ebx
-	push %ebp
-	push %edi
-	push %esi
+	pushl %ebx
+	pushl %ebp
+	pushl %edi
+	pushl %esi
 #else
-	sub $16, %esp
+	subl $16, %esp
 #endif
-	push %edx
-	push %ecx
-	push %eax
+	pushl %edx
+	pushl %ecx
+	pushl %eax
 	
 	# we must fill the data segment registers
-	movw $16,%ax
-	movw %ax,%ds
-	movw %ax,%es
+	movw $16, %ax
+	movw %ax, %ds
+	movw %ax, %es
 
 	pushl %esp          # *istate
 	pushl $(\i)         # intnum
 	call exc_dispatch   # excdispatch(intnum, *istate)
-	addl $8,%esp        # Clear arguments from stack
+	addl $8, %esp       # Clear arguments from stack
 
 	CLEAR_NT_FLAG # Modifies %ecx
 	
-	pop %eax
-	pop %ecx
-	pop %edx
+	popl %eax
+	popl %ecx
+	popl %edx
 #ifdef CONFIG_DEBUG_ALLREGS
-	pop %esi
-	pop %edi
-	pop %ebp
-	pop %ebx
+	popl %esi
+	popl %edi
+	popl %ebp
+	popl %ebx
 #else
-	add $16, %esp
+	addl $16, %esp
 #endif	
 	
-	pop %gs
-	pop %fs
-	pop %es
-	pop %ds
-
-	addl $4,%esp	# Skip error word, no matter whether real or fake.
+	popl %gs
+	popl %fs
+	popl %es
+	popl %ds
+
+	addl $4, %esp	# Skip error word, no matter whether real or fake.
 	iret
 .endif
 
 	.align INTERRUPT_ALIGN
-	.if (\n-\i)-1
-	handler "(\i+1)",\n
+	.if (\n- \i) - 1
+	handler "(\i + 1)", \n
 	.endif
 .endm
 
 # keep in sync with pm.h !!!
-IDT_ITEMS=64
+IDT_ITEMS = 64
 .align INTERRUPT_ALIGN
 interrupt_handlers:
@@ -274,3 +279,3 @@
 .global interrupt_handler_size
 
-interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS
+interrupt_handler_size: .long (h_end - h_start) / IDT_ITEMS
Index: uspace/lib/libc/arch/ia32/Makefile.inc
===================================================================
--- uspace/lib/libc/arch/ia32/Makefile.inc	(revision 7e956eb53e39a5b6b5da0e92b2e789a596d7fc45)
+++ uspace/lib/libc/arch/ia32/Makefile.inc	(revision 6c383b0276be208b47b6cf306a8fcdbe62d15436)
@@ -33,5 +33,5 @@
 TOOLCHAIN_DIR = /usr/local/i686/bin
 
-ARCH_SOURCES += arch/$(ARCH)/src/syscall.c \
+ARCH_SOURCES += arch/$(ARCH)/src/syscall.S \
 		arch/$(ARCH)/src/fibril.S \
 		arch/$(ARCH)/src/tls.c
Index: uspace/lib/libc/arch/ia32/src/fibril.S
===================================================================
--- uspace/lib/libc/arch/ia32/src/fibril.S	(revision 7e956eb53e39a5b6b5da0e92b2e789a596d7fc45)
+++ uspace/lib/libc/arch/ia32/src/fibril.S	(revision 6c383b0276be208b47b6cf306a8fcdbe62d15436)
@@ -75,9 +75,9 @@
 
 	# Set thread local storage
-	pushl %edi
-	movl 24(%eax), %eax   # Set arg1 to TLS addr
-	movl $1, %edi         # Syscall 1
+	pushl %edx
+	movl 24(%eax), %edx   # Set arg1 to TLS addr
+	movl $1, %eax         # Syscall SYS_TLS_SET
 	int $0x30
-	popl %edi
+	popl %edx
 	
 	xorl %eax,%eax		# context_restore returns 0
Index: uspace/lib/libc/arch/ia32/src/syscall.S
===================================================================
--- uspace/lib/libc/arch/ia32/src/syscall.S	(revision 6c383b0276be208b47b6cf306a8fcdbe62d15436)
+++ uspace/lib/libc/arch/ia32/src/syscall.S	(revision 6c383b0276be208b47b6cf306a8fcdbe62d15436)
@@ -0,0 +1,56 @@
+#
+# 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.
+#
+
+.text
+
+/** Syscall wrapper.
+ *
+ * Mind the order of arguments. First two arguments and the syscall number go to
+ * scratch registers. An optimized version of this wrapper for fewer arguments
+ * could benefit from this and not save unused registers on the stack.
+ */
+.global __syscall
+__syscall:
+	pushl %ebx
+	pushl %esi
+	pushl %edi
+	pushl %ebp
+	movl 20(%esp), %edx	# First argument.
+	movl 24(%esp), %ecx	# Second argument.
+	movl 28(%esp), %ebx	# Third argument.
+	movl 32(%esp), %esi	# Fourth argument.
+	movl 36(%esp), %edi	# Fifth argument.
+	movl 40(%esp), %ebp	# Sixth argument.
+	movl 44(%esp), %eax	# Syscall number.
+	int $0x30
+	popl %ebp
+	popl %edi
+	popl %esi
+	popl %ebx
+	ret
+
Index: pace/lib/libc/arch/ia32/src/syscall.c
===================================================================
--- uspace/lib/libc/arch/ia32/src/syscall.c	(revision 7e956eb53e39a5b6b5da0e92b2e789a596d7fc45)
+++ 	(revision )
@@ -1,59 +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.
- */
-
-/** @addtogroup libcia32 ia32
- * @brief	ia32 architecture dependent parts of libc
- * @ingroup lc
- * @{
- */
-/** @file
- */
-
-#include <thread.h>
-#include <libc.h>
-
-sysarg_t __syscall(const sysarg_t p1, const sysarg_t p2, const sysarg_t p3, 
-		   const sysarg_t p4, const syscall_t id)
-{
-	sysarg_t ret;
-	
-	asm volatile (
-		"int $0x30\n"
-		: "=a" (ret)
-		: "a" (p1),
-		  "c" (p2),
-		  "d" (p3),
-		  "S" (p4),
-		  "D" (id)
-	);
-	
-	return ret;
-}
-
-/** @}
- */
