Index: kernel/arch/ia32/src/drivers/i8254.c
===================================================================
--- kernel/arch/ia32/src/drivers/i8254.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia32/src/drivers/i8254.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -69,5 +69,13 @@
 static void i8254_irq_handler(irq_t *irq, void *arg, ...)
 {
+	/*
+	 * This IRQ is responsible for kernel preemption.
+	 * Nevertheless, we are now holding a spinlock which prevents
+	 * preemption. For this particular IRQ, we don't need the
+	 * lock. We just release it, call clock() and then reacquire it again.
+	 */
+	spinlock_unlock(&irq->lock);
 	clock();
+	spinlock_lock(&irq->lock);
 }
 
@@ -137,5 +145,7 @@
 	o2 |= inb(CLK_PORT1) << 8;
 
-	CPU->delay_loop_const = ((MAGIC_NUMBER*LOOPS)/1000) / ((t1-t2)-(o1-o2)) + (((MAGIC_NUMBER*LOOPS)/1000) % ((t1-t2)-(o1-o2)) ? 1 : 0);
+	CPU->delay_loop_const =
+	    ((MAGIC_NUMBER * LOOPS) / 1000) / ((t1 - t2) - (o1 - o2)) +
+	    (((MAGIC_NUMBER * LOOPS) / 1000) % ((t1 - t2) - (o1 - o2)) ? 1 : 0);
 
 	clk1 = get_cycle();
Index: kernel/arch/ia32/src/drivers/i8259.c
===================================================================
--- kernel/arch/ia32/src/drivers/i8259.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia32/src/drivers/i8259.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -87,5 +87,5 @@
 
 	pic_disable_irqs(0xffff);		/* disable all irq's */
-	pic_enable_irqs(1<<IRQ_PIC1);		/* but enable pic1 */
+	pic_enable_irqs(1 << IRQ_PIC1);		/* but enable pic1 */
 }
 
@@ -120,6 +120,6 @@
 void pic_eoi(void)
 {
-	outb(0x20,0x20);
-	outb(0xa0,0x20);
+	outb(0x20, 0x20);
+	outb(0xa0, 0x20);
 }
 
Index: kernel/arch/ia32/src/ia32.c
===================================================================
--- kernel/arch/ia32/src/ia32.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia32/src/ia32.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -119,7 +119,7 @@
 		memory_print_map();
 		
-		#ifdef CONFIG_SMP
+#ifdef CONFIG_SMP
 		acpi_init();
-		#endif /* CONFIG_SMP */
+#endif /* CONFIG_SMP */
 	}
 }
Index: kernel/arch/ia32/src/interrupt.c
===================================================================
--- kernel/arch/ia32/src/interrupt.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia32/src/interrupt.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -141,11 +141,10 @@
 {
 	uint32_t mxcsr;
-	asm
-	(
+	asm (
 		"stmxcsr %0;\n"
 		:"=m"(mxcsr)
 	);
 	fault_if_from_uspace(istate, "SIMD FP exception(19), MXCSR: %#zx",
-			     (unative_t)mxcsr);
+	    (unative_t)mxcsr);
 
 	decode_istate(istate);
Index: kernel/arch/ia32/src/smp/smp.c
===================================================================
--- kernel/arch/ia32/src/smp/smp.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia32/src/smp/smp.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -113,6 +113,6 @@
 	 * Set the warm-reset vector to the real-mode address of 4K-aligned ap_boot()
 	 */
-	*((uint16_t *) (PA2KA(0x467+0))) =  ((uintptr_t) ap_boot) >> 4;	/* segment */
-	*((uint16_t *) (PA2KA(0x467+2))) =  0;				/* offset */
+	*((uint16_t *) (PA2KA(0x467 + 0))) =  ((uintptr_t) ap_boot) >> 4;	/* segment */
+	*((uint16_t *) (PA2KA(0x467 + 2))) =  0;				/* offset */
 	
 	/*
@@ -120,6 +120,6 @@
 	 * BIOS will not do the POST after the INIT signal.
 	 */
-	outb(0x70,0xf);
-	outb(0x71,0xa);
+	outb(0x70, 0xf);
+	outb(0x71, 0xa);
 
 	pic_disable_irqs(0xffff);
Index: kernel/arch/ia32xen/src/ia32xen.c
===================================================================
--- kernel/arch/ia32xen/src/ia32xen.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia32xen/src/ia32xen.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -163,7 +163,7 @@
 		memory_print_map();
 		
-		#ifdef CONFIG_SMP
+#ifdef CONFIG_SMP
 		acpi_init();
-		#endif /* CONFIG_SMP */
+#endif /* CONFIG_SMP */
 	}
 }
Index: kernel/arch/ia32xen/src/smp/apic.c
===================================================================
--- kernel/arch/ia32xen/src/smp/apic.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia32xen/src/smp/apic.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -152,5 +152,5 @@
 	 */
 	idreg.value = io_apic_read(IOAPICID);
-	if ((1<<idreg.apic_id) & apic_id_mask) {	/* see if IO APIC ID is used already */
+	if ((1 << idreg.apic_id) & apic_id_mask) {	/* see if IO APIC ID is used already */
 		for (i = 0; i < APIC_ID_COUNT; i++) {
 			if (!((1<<i) & apic_id_mask)) {
@@ -303,5 +303,5 @@
 		 * If this is not 82489DX-based l_apic we must send two STARTUP IPI's.
 		 */
-		for (i = 0; i<2; i++) {
+		for (i = 0; i < 2; i++) {
 			icr.lo = l_apic[ICRlo];
 			icr.delmod = DELMOD_STARTUP;
@@ -402,6 +402,6 @@
 	/* Program Logical Destination Register. */
 	ldr.value = l_apic[LDR];
-	if (CPU->id < sizeof(CPU->id)*8)	/* size in bits */
-		ldr.id = (1<<CPU->id);
+	if (CPU->id < sizeof(CPU->id) * 8)	/* size in bits */
+		ldr.id = (1 << CPU->id);
 	l_apic[LDR] = ldr.value;
 	
@@ -508,6 +508,6 @@
 		dlvr = DELMOD_LOWPRI;
 
-	reg.lo = io_apic_read(IOREDTBL + pin*2);
-	reg.hi = io_apic_read(IOREDTBL + pin*2 + 1);
+	reg.lo = io_apic_read(IOREDTBL + pin * 2);
+	reg.hi = io_apic_read(IOREDTBL + pin * 2 + 1);
 	
 	reg.dest = dest;
@@ -518,6 +518,6 @@
 	reg.intvec = v;
 
-	io_apic_write(IOREDTBL + pin*2, reg.lo);
-	io_apic_write(IOREDTBL + pin*2 + 1, reg.hi);
+	io_apic_write(IOREDTBL + pin * 2, reg.lo);
+	io_apic_write(IOREDTBL + pin * 2 + 1, reg.hi);
 }
 
@@ -540,5 +540,5 @@
 			pin = smp_irq_to_pin(i);
 			if (pin != -1) {
-				reg.lo = io_apic_read(IOREDTBL + pin*2);
+				reg.lo = io_apic_read(IOREDTBL + pin * 2);
 				reg.masked = true;
 				io_apic_write(IOREDTBL + pin*2, reg.lo);
@@ -567,5 +567,5 @@
 			pin = smp_irq_to_pin(i);
 			if (pin != -1) {
-				reg.lo = io_apic_read(IOREDTBL + pin*2);
+				reg.lo = io_apic_read(IOREDTBL + pin * 2);
 				reg.masked = false;
 				io_apic_write(IOREDTBL + pin*2, reg.lo);
Index: kernel/arch/ia64/src/drivers/it.c
===================================================================
--- kernel/arch/ia64/src/drivers/it.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia64/src/drivers/it.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -114,6 +114,12 @@
 	itm_write(m);
 	srlz_d();				/* propagate changes */
-	
+
+	/*
+	 * We are holding a lock which prevents preemption.
+	 * Release the lock, call clock() and reacquire the lock again.
+	 */
+	spinlock_unlock(&irq->lock);	
 	clock();
+	spinlock_lock(&irq->lock);
 }
 
Index: kernel/arch/ia64/src/ia64.c
===================================================================
--- kernel/arch/ia64/src/ia64.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/ia64/src/ia64.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -82,6 +82,4 @@
 	
 }
-
-
 
 void arch_post_mm_init(void)
Index: kernel/arch/mips32/src/interrupt.c
===================================================================
--- kernel/arch/mips32/src/interrupt.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/mips32/src/interrupt.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -114,5 +114,12 @@
 	nextcount = cp0_count_read() + cp0_compare_value - drift;
 	cp0_compare_write(nextcount);
+
+	/*
+	 * We are holding a lock which prevents preemption.
+	 * Release the lock, call clock() and reacquire the lock again.
+	 */
+	spinlock_unlock(&irq->lock);
 	clock();
+	spinlock_lock(&irq->lock);
 	
 	if (virtual_timer_fnc != NULL)
Index: kernel/arch/sparc64/src/drivers/tick.c
===================================================================
--- kernel/arch/sparc64/src/drivers/tick.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/sparc64/src/drivers/tick.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -100,6 +100,6 @@
 		CPU->missed_clock_ticks++;
 	}
-	CPU->arch.next_tick_cmpr = tick_read() + (CPU->arch.clock_frequency /
-		HZ) - drift;
+	CPU->arch.next_tick_cmpr = tick_read() +
+	    (CPU->arch.clock_frequency / HZ) - drift;
 	tick_compare_write(CPU->arch.next_tick_cmpr);
 	clock();
Index: kernel/arch/sparc64/src/sparc64.c
===================================================================
--- kernel/arch/sparc64/src/sparc64.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/arch/sparc64/src/sparc64.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -137,5 +137,5 @@
 {
 	uint64_t stop = tick_read() + (uint64_t) usec * (uint64_t)
-		CPU->arch.clock_frequency / 1000000;
+	    CPU->arch.clock_frequency / 1000000;
 
 	while (tick_read() < stop)
@@ -147,7 +147,7 @@
 {
 	switch_to_userspace((uintptr_t) kernel_uarg->uspace_entry,
-		((uintptr_t) kernel_uarg->uspace_stack) + STACK_SIZE
-		- (ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT) + STACK_BIAS),
-		(uintptr_t) kernel_uarg->uspace_uarg);
+	    ((uintptr_t) kernel_uarg->uspace_stack) + STACK_SIZE
+	    - (ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT) + STACK_BIAS),
+	    (uintptr_t) kernel_uarg->uspace_uarg);
 
 	for (;;)
Index: kernel/generic/include/mm/as.h
===================================================================
--- kernel/generic/include/mm/as.h	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/generic/include/mm/as.h	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -168,5 +168,7 @@
 	/** Containing address space. */
 	as_t *as;		
-	/** Flags related to the memory represented by the address space area. */
+	/**
+	 * Flags related to the memory represented by the address space area.
+	 */
 	int flags;
 	/** Attributes related to the address space area itself. */
@@ -180,6 +182,6 @@
 
 	/**
-	 * If the address space area has been shared, this pointer will reference
-	 * the share info structure.
+	 * If the address space area has been shared, this pointer will
+	 * reference the share info structure.
 	 */
 	share_info_t *sh_info;
Index: kernel/generic/src/ddi/ddi.c
===================================================================
--- kernel/generic/src/ddi/ddi.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/generic/src/ddi/ddi.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -131,6 +131,6 @@
 	parea = btree_search(&parea_btree, (btree_key_t) pf, &nodep);
 	if (!parea || parea->frames < pages || ((flags & AS_AREA_CACHEABLE) &&
-		!parea->cacheable) || (!(flags & AS_AREA_CACHEABLE) &&
-		parea->cacheable)) {
+	    !parea->cacheable) || (!(flags & AS_AREA_CACHEABLE) &&
+	    parea->cacheable)) {
 		/*
 		 * This physical memory area cannot be mapped.
@@ -235,10 +235,10 @@
  * @return 0 on success, otherwise it returns error code found in errno.h
  */ 
-unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, unative_t
-	pages, unative_t flags)
+unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base,
+    unative_t pages, unative_t flags)
 {
 	return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base,
-		FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
-		(count_t) pages, (int) flags);
+	    FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE),
+	    (count_t) pages, (int) flags);
 }
 
@@ -259,5 +259,5 @@
 		
 	return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id,
-		(uintptr_t) arg.ioaddr, (size_t) arg.size);
+	    (uintptr_t) arg.ioaddr, (size_t) arg.size);
 }
 
@@ -272,5 +272,5 @@
 unative_t sys_preempt_control(int enable)
 {
-        if (! cap_get(TASK) & CAP_PREEMPT_CONTROL)
+        if (!cap_get(TASK) & CAP_PREEMPT_CONTROL)
                 return EPERM;
         if (enable)
Index: kernel/generic/src/ddi/irq.c
===================================================================
--- kernel/generic/src/ddi/irq.c	(revision 80bcaed11c2e767559657092d6c13d1a7fdd449c)
+++ kernel/generic/src/ddi/irq.c	(revision f619ec11294d6774460c8c5c50473bf484647d12)
@@ -303,8 +303,8 @@
 	spinlock_lock(&irq->lock);
 	if (devno == -1) {
-		/* Invoked by irq_dispatch(). */
+		/* Invoked by irq_dispatch_and_lock(). */
 		rv = ((irq->inr == inr) && (irq->claim() == IRQ_ACCEPT));
 	} else {
-		/* Invoked by irq_find(). */
+		/* Invoked by irq_find_and_lock(). */
 		rv = ((irq->inr == inr) && (irq->devno == devno));
 	}
@@ -362,8 +362,8 @@
 	spinlock_lock(&irq->lock);
 	if (devno == -1) {
-		/* Invoked by irq_dispatch() */
+		/* Invoked by irq_dispatch_and_lock() */
 		rv = (irq->claim() == IRQ_ACCEPT);
 	} else {
-		/* Invoked by irq_find() */
+		/* Invoked by irq_find_and_lock() */
 		rv = (irq->devno == devno);
 	}
