Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch x86: improve error handling related to idt_vec_alloc()



details:   https://anonhg.NetBSD.org/src/rev/eebb38df1eb8
branches:  trunk
changeset: 1029140:eebb38df1eb8
user:      yamaguchi <yamaguchi%NetBSD.org@localhost>
date:      Thu Dec 23 02:07:21 2021 +0000

description:
x86: improve error handling related to idt_vec_alloc()

diffstat:

 sys/arch/amd64/amd64/db_interface.c |   5 +++--
 sys/arch/i386/i386/db_interface.c   |   5 +++--
 sys/arch/x86/x86/idt.c              |  15 +++++++++++----
 sys/arch/x86/x86/intr.c             |  10 +++++-----
 4 files changed, 22 insertions(+), 13 deletions(-)

diffs (161 lines):

diff -r dc5a83ded578 -r eebb38df1eb8 sys/arch/amd64/amd64/db_interface.c
--- a/sys/arch/amd64/amd64/db_interface.c       Wed Dec 22 21:45:02 2021 +0000
+++ b/sys/arch/amd64/amd64/db_interface.c       Thu Dec 23 02:07:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: db_interface.c,v 1.39 2021/02/23 07:13:51 mrg Exp $    */
+/*     $NetBSD: db_interface.c,v 1.40 2021/12/23 02:07:21 yamaguchi Exp $      */
 
 /*
  * Mach Operating System
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.39 2021/02/23 07:13:51 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.40 2021/12/23 02:07:21 yamaguchi Exp $");
 
 #include "opt_ddb.h"
 #include "opt_multiprocessor.h"
@@ -119,6 +119,7 @@
                handler = &Xintr_x2apic_ddbipi;
 #endif
        ddb_vec = idt_vec_alloc(iv, 0xf0, 0xff);
+       KASSERT(ddb_vec > 0);
        set_idtgate(&idt[ddb_vec], handler, 1, SDT_SYS386IGT, SEL_KPL,
            GSEL(GCODE_SEL, SEL_KPL));
 #else
diff -r dc5a83ded578 -r eebb38df1eb8 sys/arch/i386/i386/db_interface.c
--- a/sys/arch/i386/i386/db_interface.c Wed Dec 22 21:45:02 2021 +0000
+++ b/sys/arch/i386/i386/db_interface.c Thu Dec 23 02:07:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: db_interface.c,v 1.86 2021/02/23 07:13:52 mrg Exp $    */
+/*     $NetBSD: db_interface.c,v 1.87 2021/12/23 02:07:21 yamaguchi Exp $      */
 
 /*
  * Mach Operating System
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.86 2021/02/23 07:13:52 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.87 2021/12/23 02:07:21 yamaguchi Exp $");
 
 #include "opt_ddb.h"
 #include "opt_multiprocessor.h"
@@ -122,6 +122,7 @@
                handler = &Xintr_x2apic_ddbipi;
 #endif
        ddb_vec = idt_vec_alloc(iv, 0xf0, 0xff);
+       KASSERT(ddb_vec > 0);
        idt_vec_set(iv, ddb_vec, handler);
 #else
        /* Initialised as part of xen_ipi_init() */
diff -r dc5a83ded578 -r eebb38df1eb8 sys/arch/x86/x86/idt.c
--- a/sys/arch/x86/x86/idt.c    Wed Dec 22 21:45:02 2021 +0000
+++ b/sys/arch/x86/x86/idt.c    Thu Dec 23 02:07:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: idt.c,v 1.14 2020/07/14 15:59:21 para Exp $    */
+/*     $NetBSD: idt.c,v 1.15 2021/12/23 02:07:21 yamaguchi Exp $       */
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2009 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.14 2020/07/14 15:59:21 para Exp $");
+__KERNEL_RCSID(0, "$NetBSD: idt.c,v 1.15 2021/12/23 02:07:21 yamaguchi Exp $");
 
 #include "opt_pcpu_idt.h"
 
@@ -170,6 +170,9 @@
 
        KASSERT(mutex_owned(&cpu_lock) || !mp_online);
 
+       if (low < 0 || high >= __arraycount(iv->iv_allocmap))
+               return -1;
+
        for (vec = low; vec <= high; vec++) {
                if (idt_allocmap[vec] == 0) {
                        /* idt_vec_free() can be unlocked, so membar. */
@@ -178,7 +181,8 @@
                        return vec;
                }
        }
-       return 0;
+
+       return -1;
 }
 
 void
@@ -189,7 +193,7 @@
        KASSERT(mutex_owned(&cpu_lock) || !mp_online);
 
        result = idt_vec_alloc(iv, vec, vec);
-       if (result != vec) {
+       if (result < 0) {
                panic("%s: failed to reserve vec %d", __func__, vec);
        }
 }
@@ -201,6 +205,7 @@
        char *idt_allocmap __diagused = iv->iv_allocmap;
 
        KASSERT(idt_allocmap[vec] == 1);
+
        idt = iv->iv_idt;
        set_idtgate(&idt[vec], function, 0, SDT_SYS386IGT, SEL_KPL,
               GSEL(GCODE_SEL, SEL_KPL));
@@ -215,6 +220,8 @@
        idt_descriptor_t *idt;
        char *idt_allocmap = iv->iv_allocmap;
 
+       KASSERT(idt_allocmap[vec] == 1);
+
        idt = iv->iv_idt;
        unset_idtgate(&idt[vec]);
        idt_allocmap[vec] = 0;
diff -r dc5a83ded578 -r eebb38df1eb8 sys/arch/x86/x86/intr.c
--- a/sys/arch/x86/x86/intr.c   Wed Dec 22 21:45:02 2021 +0000
+++ b/sys/arch/x86/x86/intr.c   Thu Dec 23 02:07:21 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: intr.c,v 1.156 2021/10/07 12:52:27 msaitoh Exp $       */
+/*     $NetBSD: intr.c,v 1.157 2021/12/23 02:07:21 yamaguchi Exp $     */
 
 /*
  * Copyright (c) 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -133,7 +133,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.156 2021/10/07 12:52:27 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.157 2021/12/23 02:07:21 yamaguchi Exp $");
 
 #include "opt_intrdebug.h"
 #include "opt_multiprocessor.h"
@@ -609,7 +609,7 @@
                iv = idt_vec_ref(&ci->ci_idtvec);
                idtvec = idt_vec_alloc(iv, APIC_LEVEL(level), IDT_INTR_HIGH);
        }
-       if (idtvec == 0) {
+       if (idtvec < 0) {
                evcnt_detach(&ci->ci_isources[slot]->is_evcnt);
                ci->ci_isources[slot]->is_evname[0] = '\0';
                ci->ci_isources[slot] = NULL;
@@ -1799,7 +1799,7 @@
 
        x86_intr_calculatemasks(ci);
 
-       if (idt_vec > 0 && idt_vec_is_pcpu()) {
+       if (idt_vec_is_pcpu()) {
                idt_vec_free(&ci->ci_idtvec, idt_vec);
        } else  {
                /*
@@ -1898,7 +1898,7 @@
 
        old_idtvec = isp->is_idtvec;
 
-       if (isp->is_idtvec > 0 && idt_vec_is_pcpu()) {
+       if (idt_vec_is_pcpu()) {
                new_idtvec = idt_vec_alloc(&newci->ci_idtvec,
                    APIC_LEVEL(ih->ih_level), IDT_INTR_HIGH);
                if (new_idtvec == 0)



Home | Main Index | Thread Index | Old Index