Module Name:    src
Committed By:   cherry
Date:           Mon Oct  8 08:05:08 UTC 2018

Modified Files:
        src/sys/arch/x86/x86: i8259.c intr.c ioapic.c

Log Message:
Clean up XEN specific stuff from the apic code, and move to intr.c

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/x86/x86/i8259.c
cvs rdiff -u -r1.133 -r1.134 src/sys/arch/x86/x86/intr.c
cvs rdiff -u -r1.58 -r1.59 src/sys/arch/x86/x86/ioapic.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/i8259.c
diff -u src/sys/arch/x86/x86/i8259.c:1.20 src/sys/arch/x86/x86/i8259.c:1.21
--- src/sys/arch/x86/x86/i8259.c:1.20	Sun Oct  7 16:36:36 2018
+++ src/sys/arch/x86/x86/i8259.c	Mon Oct  8 08:05:08 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: i8259.c,v 1.20 2018/10/07 16:36:36 cherry Exp $	*/
+/*	$NetBSD: i8259.c,v 1.21 2018/10/08 08:05:08 cherry Exp $	*/
 
 /*
  * Copyright 2002 (c) Wasabi Systems, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.20 2018/10/07 16:36:36 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.21 2018/10/08 08:05:08 cherry Exp $");
 
 #include <sys/param.h> 
 #include <sys/systm.h>
@@ -88,9 +88,6 @@ __KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.
 #include <machine/pic.h>
 #include <machine/i8259.h>
 
-#ifdef XEN
-#include <xen/evtchn.h>
-#endif
 
 #ifndef __x86_64__
 #include "mca.h"
@@ -102,7 +99,6 @@ __KERNEL_RCSID(0, "$NetBSD: i8259.c,v 1.
 static void i8259_hwmask(struct pic *, int);
 static void i8259_hwunmask(struct pic *, int);
 static void i8259_setup(struct pic *, struct cpu_info *, int, int, int);
-static void i8259_unsetup(struct pic *, struct cpu_info *, int, int, int);
 static void i8259_reinit_irqs(void);
 
 unsigned i8259_imen;
@@ -119,7 +115,7 @@ struct pic i8259_pic = {
 	.pic_hwmask = i8259_hwmask,
 	.pic_hwunmask = i8259_hwunmask,
 	.pic_addroute = i8259_setup,
-	.pic_delroute = i8259_unsetup,
+	.pic_delroute = i8259_setup,
 	.pic_level_stubs = legacy_stubs,
 	.pic_edge_stubs = legacy_stubs,
 };
@@ -256,62 +252,10 @@ static void
 i8259_setup(struct pic *pic, struct cpu_info *ci,
     int pin, int idtvec, int type)
 {
-#if defined(XEN)
-	/*
-	 * This is kludgy, and not the right place, but we can't bind
-	 * before the routing has been set to the appropriate 'vector'.
-	 * in x86/intr.c, this is done after idt_vec_set(), where this
-	 * would have been more appropriate to put this.
-	 */
-
-	int port, irq;
-	irq = vect2irq[idtvec];
-	KASSERT(irq != 0);
-	if (irq2port[irq] != 0) {
-		/* 
-		 * Shared interrupt - we can't rebind.
-		 *  The port is shared instead.
-		 */
-		return;
-	}
-	
-	port = bind_pirq_to_evtch(irq);
-	KASSERT(port < NR_EVENT_CHANNELS);
-	KASSERT(port >= 0);
-
-	KASSERT(irq2port[irq] == 0);
-	irq2port[irq] = port + 1;
-
-	xen_atomic_set_bit(&ci->ci_evtmask[0], port);
-#else
 	if (CPU_IS_PRIMARY(ci))
 		i8259_reinit_irqs();
-#endif
 }
 
-static void
-i8259_unsetup(struct pic *pic, struct cpu_info *ci,
-    int pin, int idtvec, int type)
-{
-#if defined(XEN)
-	int port, irq;
-	irq = vect2irq[idtvec];
-	port = unbind_pirq_from_evtch(irq);
-
-	KASSERT(port < NR_EVENT_CHANNELS);
-
-	/* XXX: This is problematic for shared interrupts */
-	KASSERT(irq2port[irq] != 0);
-	irq2port[irq] = 0;
-
-	xen_atomic_clear_bit(&ci->ci_evtmask[0], port);
-#else
-	if (CPU_IS_PRIMARY(ci))
-		i8259_reinit_irqs();
-#endif
-}
-
-
 void
 i8259_reinit(void)
 {

Index: src/sys/arch/x86/x86/intr.c
diff -u src/sys/arch/x86/x86/intr.c:1.133 src/sys/arch/x86/x86/intr.c:1.134
--- src/sys/arch/x86/x86/intr.c:1.133	Sun Oct  7 05:23:01 2018
+++ src/sys/arch/x86/x86/intr.c	Mon Oct  8 08:05:08 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.133 2018/10/07 05:23:01 cherry Exp $	*/
+/*	$NetBSD: intr.c,v 1.134 2018/10/08 08:05:08 cherry Exp $	*/
 
 /*
  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.133 2018/10/07 05:23:01 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.134 2018/10/08 08:05:08 cherry Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -1278,12 +1278,24 @@ intr_establish_xname(int legacy_irq, str
 
 	vector = xen_vec_alloc(gsi);
 
-	extern struct cpu_info phycpu_info_primary; /* XXX */
-	struct cpu_info *ci = &phycpu_info_primary;
-	pic->pic_addroute(pic, ci, pin, vector, type);
-
-	evtchn = irq2port[vect2irq[vector]];
-	KASSERT(evtchn > 0);
+	if (irq2port[gsi] == 0) {
+		extern struct cpu_info phycpu_info_primary; /* XXX */
+		struct cpu_info *ci = &phycpu_info_primary;
+
+		pic->pic_addroute(pic, ci, pin, vector, type);
+
+		evtchn = bind_pirq_to_evtch(gsi);
+		KASSERT(evtchn > 0);
+		KASSERT(evtchn < NR_EVENT_CHANNELS);
+		irq2port[gsi] = evtchn + 1;
+		xen_atomic_set_bit(&ci->ci_evtmask[0], evtchn);
+	} else {
+		/*
+		 * Shared interrupt - we can't rebind.
+		 * The port is shared instead.
+		 */
+		evtchn = irq2port[gsi];
+	}
 
 	pih = pirq_establish(gsi, evtchn, handler, arg, level,
 			     intrstr, xname);
@@ -1337,7 +1349,31 @@ intr_disestablish(struct intrhand *ih)
 		return;
 	}
 #if defined(DOM0OPS)
-	pirq_disestablish((struct pintrhand *)ih);
+	/* 
+	 * Cache state, to prevent a use after free situation with
+	 * ih.
+	 */
+
+	struct pintrhand *pih = (struct pintrhand *)ih;
+
+	int pirq = pih->pirq;
+	int port = pih->evtch;
+	KASSERT(irq2port[pirq] != 0);
+
+	pirq_disestablish(pih);
+
+	if (evtsource[port] == NULL) {
+			/*
+			 * Last handler was removed by
+			 * event_remove_handler().
+			 *
+			 * We can safely unbind the pirq now.
+			 */
+
+			port = unbind_pirq_from_evtch(pirq);
+			KASSERT(port == pih->evtch);
+			irq2port[pirq] = 0;
+	}
 #endif
 	return;
 #endif /* XEN */

Index: src/sys/arch/x86/x86/ioapic.c
diff -u src/sys/arch/x86/x86/ioapic.c:1.58 src/sys/arch/x86/x86/ioapic.c:1.59
--- src/sys/arch/x86/x86/ioapic.c:1.58	Sun Oct  7 16:36:36 2018
+++ src/sys/arch/x86/x86/ioapic.c	Mon Oct  8 08:05:08 2018
@@ -1,4 +1,4 @@
-/* 	$NetBSD: ioapic.c,v 1.58 2018/10/07 16:36:36 cherry Exp $	*/
+/* 	$NetBSD: ioapic.c,v 1.59 2018/10/08 08:05:08 cherry Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2009 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ioapic.c,v 1.58 2018/10/07 16:36:36 cherry Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ioapic.c,v 1.59 2018/10/08 08:05:08 cherry Exp $");
 
 #include "opt_ddb.h"
 
@@ -564,37 +564,6 @@ ioapic_addroute(struct pic *pic, struct 
 	pp->ip_vector = idtvec;
 	pp->ip_cpu = ci;
 	apic_set_redir(sc, pin, idtvec, ci);
-
-#if defined(XEN)
-	/*
-	 * This is kludgy, and not the right place, but we can't bind
-	 * before the routing has been set to the appropriate 'vector'.
-	 * in x86/intr.c, this is done after idt_vec_set(), where this
-	 * would have been more appropriate to put this.
-	 */
-
-	int port, irq;
-	irq = vect2irq[idtvec];
-	KASSERT(irq != 0);
-
-	if (irq2port[irq] != 0) {
-		/* 
-		 * Shared interrupt - we can't rebind.
-		 *  The port is shared instead.
-		 */
-		return;
-	}
-
-	port = bind_pirq_to_evtch(irq);
-	KASSERT(port < NR_EVENT_CHANNELS);
-	KASSERT(port >= 0);
-
-	KASSERT(irq2port[irq] == 0);
-	irq2port[irq] = port + 1;
-
-	xen_atomic_set_bit(&ci->ci_evtmask[0], port);
-#endif
-
 }
 
 static void
@@ -603,22 +572,6 @@ ioapic_delroute(struct pic *pic, struct 
 {
 
 	ioapic_hwmask(pic, pin);
-
-#if defined(XEN)
-	int port, irq;
-	irq = vect2irq[idtvec];
-	port = unbind_pirq_from_evtch(irq);
-
-	KASSERT(port < NR_EVENT_CHANNELS);
-
-	/* XXX: This is problematic for shared interrupts */
-	KASSERT(irq2port[irq] != 0);
-	irq2port[irq] = 0;
-
-	xen_atomic_clear_bit(&ci->ci_evtmask[0], port);
-
-#endif
-
 }
 
 #ifdef DDB

Reply via email to