Index: kernel/generic/src/ipc/irq.c
===================================================================
--- kernel/generic/src/ipc/irq.c	(revision 439d0363bd7115a2ec68b17c6fa9daa8b4922dd8)
+++ kernel/generic/src/ipc/irq.c	(revision da1bafb8cf9a3b3be8ef21bc114daaa476a85190)
@@ -31,4 +31,5 @@
  * @{
  */
+
 /**
  * @file
@@ -67,4 +68,5 @@
  *   structure are finished. Because we hold the hash table lock, we prevent new
  *   IRQs from taking new references to the IRQ structure.
+ *
  */
 
@@ -81,5 +83,6 @@
 /** Free the top-half pseudocode.
  *
- * @param code		Pointer to the top-half pseudocode.
+ * @param code Pointer to the top-half pseudocode.
+ *
  */
 static void code_free(irq_code_t *code)
@@ -93,16 +96,13 @@
 /** Copy the top-half pseudocode from userspace into the kernel.
  *
- * @param ucode		Userspace address of the top-half pseudocode.
- *
- * @return		Kernel address of the copied pseudocode.
+ * @param ucode Userspace address of the top-half pseudocode.
+ *
+ * @return Kernel address of the copied pseudocode.
+ *
  */
 static irq_code_t *code_from_uspace(irq_code_t *ucode)
 {
-	irq_code_t *code;
-	irq_cmd_t *ucmds;
-	int rc;
-
-	code = malloc(sizeof(*code), 0);
-	rc = copy_from_uspace(code, ucode, sizeof(*code));
+	irq_code_t *code = malloc(sizeof(*code), 0);
+	int rc = copy_from_uspace(code, ucode, sizeof(*code));
 	if (rc != 0) {
 		free(code);
@@ -114,5 +114,6 @@
 		return NULL;
 	}
-	ucmds = code->cmds;
+	
+	irq_cmd_t *ucmds = code->cmds;
 	code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0);
 	rc = copy_from_uspace(code->cmds, ucmds,
@@ -123,5 +124,5 @@
 		return NULL;
 	}
-
+	
 	return code;
 }
@@ -141,8 +142,4 @@
     unative_t method, irq_code_t *ucode)
 {
-	ipl_t ipl;
-	irq_code_t *code;
-	irq_t *irq;
-	link_t *hlp;
 	unative_t key[] = {
 		(unative_t) inr,
@@ -150,16 +147,17 @@
 	};
 	
+	irq_code_t *code;
 	if (ucode) {
 		code = code_from_uspace(ucode);
 		if (!code)
 			return EBADMEM;
-	} else {
+	} else
 		code = NULL;
-	}
 	
 	/*
 	 * Allocate and populate the IRQ structure.
 	 */
-	irq = malloc(sizeof(irq_t), 0);
+	irq_t *irq = malloc(sizeof(irq_t), 0);
+	
 	irq_initialize(irq);
 	irq->devno = devno;
@@ -177,29 +175,30 @@
 	 * answerbox's list.
 	 */
-	ipl = interrupts_disable();
-	spinlock_lock(&irq_uspace_hash_table_lock);
-	hlp = hash_table_find(&irq_uspace_hash_table, key);
+	irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
+	
+	link_t *hlp = hash_table_find(&irq_uspace_hash_table, key);
 	if (hlp) {
-		irq_t *hirq __attribute__((unused))
-		    = hash_table_get_instance(hlp, irq_t, link);
+		irq_t *hirq = hash_table_get_instance(hlp, irq_t, link);
 		
 		/* hirq is locked */
-		spinlock_unlock(&hirq->lock);
+		irq_spinlock_unlock(&hirq->lock, false);
 		code_free(code);
-		spinlock_unlock(&irq_uspace_hash_table_lock);
+		irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
+		
 		free(irq);
-		interrupts_restore(ipl);
 		return EEXISTS;
 	}
 	
-	spinlock_lock(&irq->lock);  /* Not really necessary, but paranoid */
-	spinlock_lock(&box->irq_lock);
+	/* Locking is not really necessary, but paranoid */
+	irq_spinlock_lock(&irq->lock, false);
+	irq_spinlock_lock(&box->irq_lock, false);
+	
 	hash_table_insert(&irq_uspace_hash_table, key, &irq->link);
 	list_append(&irq->notif_cfg.link, &box->irq_head);
-	spinlock_unlock(&box->irq_lock);
-	spinlock_unlock(&irq->lock);
-	spinlock_unlock(&irq_uspace_hash_table_lock);
-	
-	interrupts_restore(ipl);
+	
+	irq_spinlock_unlock(&box->irq_lock, false);
+	irq_spinlock_unlock(&irq->lock, false);
+	irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
+	
 	return EOK;
 }
@@ -207,29 +206,27 @@
 /** Unregister task from IRQ notification.
  *
- * @param box		Answerbox associated with the notification.
- * @param inr		IRQ number.
- * @param devno		Device number.
+ * @param box   Answerbox associated with the notification.
+ * @param inr   IRQ number.
+ * @param devno Device number.
+ *
  */
 int ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno)
 {
-	ipl_t ipl;
 	unative_t key[] = {
 		(unative_t) inr,
 		(unative_t) devno
 	};
-	link_t *lnk;
-	irq_t *irq;
-
-	ipl = interrupts_disable();
-	spinlock_lock(&irq_uspace_hash_table_lock);
-	lnk = hash_table_find(&irq_uspace_hash_table, key);
+	
+	irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
+	link_t *lnk = hash_table_find(&irq_uspace_hash_table, key);
 	if (!lnk) {
-		spinlock_unlock(&irq_uspace_hash_table_lock);
-		interrupts_restore(ipl);
+		irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
 		return ENOENT;
 	}
-	irq = hash_table_get_instance(lnk, irq_t, link);
+	
+	irq_t *irq = hash_table_get_instance(lnk, irq_t, link);
+	
 	/* irq is locked */
-	spinlock_lock(&box->irq_lock);
+	irq_spinlock_lock(&box->irq_lock, false);
 	
 	ASSERT(irq->notif_cfg.answerbox == box);
@@ -237,8 +234,8 @@
 	/* Free up the pseudo code and associated structures. */
 	code_free(irq->notif_cfg.code);
-
-	/* Remove the IRQ from the answerbox's list. */ 
+	
+	/* Remove the IRQ from the answerbox's list. */
 	list_remove(&irq->notif_cfg.link);
-
+	
 	/*
 	 * We need to drop the IRQ lock now because hash_table_remove() will try
@@ -248,19 +245,17 @@
 	 * the meantime.
 	 */
-	spinlock_unlock(&irq->lock);
-
+	irq_spinlock_unlock(&irq->lock, false);
+	
 	/* Remove the IRQ from the uspace IRQ hash table. */
 	hash_table_remove(&irq_uspace_hash_table, key, 2);
 	
-	spinlock_unlock(&irq_uspace_hash_table_lock);
-	spinlock_unlock(&box->irq_lock);
+	irq_spinlock_unlock(&box->irq_lock, false);
+	irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
 	
 	/* Free up the IRQ structure. */
 	free(irq);
 	
-	interrupts_restore(ipl);
 	return EOK;
 }
-
 
 /** Disconnect all IRQ notifications from an answerbox.
@@ -270,35 +265,32 @@
  * send notifications to it.
  *
- * @param box		Answerbox for which we want to carry out the cleanup.
+ * @param box Answerbox for which we want to carry out the cleanup.
+ *
  */
 void ipc_irq_cleanup(answerbox_t *box)
 {
-	ipl_t ipl;
-	
 loop:
-	ipl = interrupts_disable();
-	spinlock_lock(&irq_uspace_hash_table_lock);
-	spinlock_lock(&box->irq_lock);
+	irq_spinlock_lock(&irq_uspace_hash_table_lock, true);
+	irq_spinlock_lock(&box->irq_lock, false);
 	
 	while (box->irq_head.next != &box->irq_head) {
-		link_t *cur = box->irq_head.next;
-		irq_t *irq;
 		DEADLOCK_PROBE_INIT(p_irqlock);
-		unative_t key[2];
-		
-		irq = list_get_instance(cur, irq_t, notif_cfg.link);
-		if (!spinlock_trylock(&irq->lock)) {
+		
+		irq_t *irq = list_get_instance(box->irq_head.next, irq_t,
+		    notif_cfg.link);
+		
+		if (!irq_spinlock_trylock(&irq->lock)) {
 			/*
 			 * Avoid deadlock by trying again.
 			 */
-			spinlock_unlock(&box->irq_lock);
-			spinlock_unlock(&irq_uspace_hash_table_lock);
-			interrupts_restore(ipl);
+			irq_spinlock_unlock(&box->irq_lock, false);
+			irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
 			DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD);
 			goto loop;
 		}
+		
+		unative_t key[2];
 		key[0] = irq->inr;
 		key[1] = irq->devno;
-		
 		
 		ASSERT(irq->notif_cfg.answerbox == box);
@@ -317,5 +309,5 @@
 		 * didn't drop the hash table lock in the meantime.
 		 */
-		spinlock_unlock(&irq->lock);
+		irq_spinlock_unlock(&irq->lock, false);
 		
 		/* Remove from the hash table. */
@@ -325,22 +317,22 @@
 	}
 	
-	spinlock_unlock(&box->irq_lock);
-	spinlock_unlock(&irq_uspace_hash_table_lock);
-	interrupts_restore(ipl);
+	irq_spinlock_unlock(&box->irq_lock, false);
+	irq_spinlock_unlock(&irq_uspace_hash_table_lock, true);
 }
 
 /** Add a call to the proper answerbox queue.
  *
- * Assume irq->lock is locked.
- *
- * @param irq		IRQ structure referencing the target answerbox.
- * @param call		IRQ notification call.
+ * Assume irq->lock is locked and interrupts disabled.
+ *
+ * @param irq  IRQ structure referencing the target answerbox.
+ * @param call IRQ notification call.
+ *
  */
 static void send_call(irq_t *irq, call_t *call)
 {
-	spinlock_lock(&irq->notif_cfg.answerbox->irq_lock);
+	irq_spinlock_lock(&irq->notif_cfg.answerbox->irq_lock, false);
 	list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs);
-	spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock);
-		
+	irq_spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock, false);
+	
 	waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST);
 }
@@ -348,16 +340,14 @@
 /** Apply the top-half pseudo code to find out whether to accept the IRQ or not.
  *
- * @param irq		IRQ structure.
- *
- * @return		IRQ_ACCEPT if the interrupt is accepted by the
- *			pseudocode. IRQ_DECLINE otherwise.
+ * @param irq IRQ structure.
+ *
+ * @return IRQ_ACCEPT if the interrupt is accepted by the
+ *         pseudocode, IRQ_DECLINE otherwise.
+ *
  */
 irq_ownership_t ipc_irq_top_half_claim(irq_t *irq)
 {
-	unsigned int i;
-	unative_t dstval;
 	irq_code_t *code = irq->notif_cfg.code;
-	unative_t *scratch = irq->notif_cfg.scratch;
-
+	uint32_t *scratch = irq->notif_cfg.scratch;
 	
 	if (!irq->notif_cfg.notify)
@@ -367,10 +357,13 @@
 		return IRQ_DECLINE;
 	
+	size_t i;
 	for (i = 0; i < code->cmdcount; i++) {
-		unsigned int srcarg = code->cmds[i].srcarg;
-		unsigned int dstarg = code->cmds[i].dstarg;
+		uint32_t dstval;
+		uintptr_t srcarg = code->cmds[i].srcarg;
+		uintptr_t dstarg = code->cmds[i].dstarg;
 		
 		if (srcarg >= IPC_CALL_LEN)
 			break;
+		
 		if (dstarg >= IPC_CALL_LEN)
 			break;
@@ -405,5 +398,5 @@
 			break;
 		case CMD_BTEST:
-			if (srcarg && dstarg) {
+			if ((srcarg) && (dstarg)) {
 				dstval = scratch[srcarg] & code->cmds[i].value;
 				scratch[dstarg] = dstval;
@@ -411,5 +404,5 @@
 			break;
 		case CMD_PREDICATE:
-			if (srcarg && !scratch[srcarg]) {
+			if ((srcarg) && (!scratch[srcarg])) {
 				i += code->cmds[i].value;
 				continue;
@@ -427,19 +420,17 @@
 }
 
-
 /* IRQ top-half handler.
  *
  * We expect interrupts to be disabled and the irq->lock already held.
  *
- * @param irq		IRQ structure.
+ * @param irq IRQ structure.
+ *
  */
 void ipc_irq_top_half_handler(irq_t *irq)
 {
 	ASSERT(irq);
-
+	
 	if (irq->notif_cfg.answerbox) {
-		call_t *call;
-
-		call = ipc_call_alloc(FRAME_ATOMIC);
+		call_t *call = ipc_call_alloc(FRAME_ATOMIC);
 		if (!call)
 			return;
@@ -448,5 +439,5 @@
 		/* Put a counter to the message */
 		call->priv = ++irq->notif_cfg.counter;
-
+		
 		/* Set up args */
 		IPC_SET_METHOD(call->data, irq->notif_cfg.method);
@@ -456,5 +447,5 @@
 		IPC_SET_ARG4(call->data, irq->notif_cfg.scratch[4]);
 		IPC_SET_ARG5(call->data, irq->notif_cfg.scratch[5]);
-
+		
 		send_call(irq, call);
 	}
@@ -463,28 +454,28 @@
 /** Send notification message.
  *
- * @param irq		IRQ structure.
- * @param a1		Driver-specific payload argument.
- * @param a2		Driver-specific payload argument.
- * @param a3		Driver-specific payload argument.
- * @param a4		Driver-specific payload argument.
- * @param a5		Driver-specific payload argument.
+ * @param irq IRQ structure.
+ * @param a1  Driver-specific payload argument.
+ * @param a2  Driver-specific payload argument.
+ * @param a3  Driver-specific payload argument.
+ * @param a4  Driver-specific payload argument.
+ * @param a5  Driver-specific payload argument.
+ *
  */
 void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3,
     unative_t a4, unative_t a5)
 {
-	call_t *call;
-
-	spinlock_lock(&irq->lock);
-
+	irq_spinlock_lock(&irq->lock, true);
+	
 	if (irq->notif_cfg.answerbox) {
-		call = ipc_call_alloc(FRAME_ATOMIC);
+		call_t *call = ipc_call_alloc(FRAME_ATOMIC);
 		if (!call) {
-			spinlock_unlock(&irq->lock);
+			irq_spinlock_unlock(&irq->lock, true);
 			return;
 		}
+		
 		call->flags |= IPC_CALL_NOTIF;
 		/* Put a counter to the message */
 		call->priv = ++irq->notif_cfg.counter;
-
+		
 		IPC_SET_METHOD(call->data, irq->notif_cfg.method);
 		IPC_SET_ARG1(call->data, a1);
@@ -496,5 +487,6 @@
 		send_call(irq, call);
 	}
-	spinlock_unlock(&irq->lock);
+	
+	irq_spinlock_unlock(&irq->lock, true);
 }
 
