Module Name: src Committed By: yamaguchi Date: Thu Dec 23 02:07:21 UTC 2021
Modified Files: src/sys/arch/amd64/amd64: db_interface.c src/sys/arch/i386/i386: db_interface.c src/sys/arch/x86/x86: idt.c intr.c Log Message: x86: improve error handling related to idt_vec_alloc() To generate a diff of this commit: cvs rdiff -u -r1.39 -r1.40 src/sys/arch/amd64/amd64/db_interface.c cvs rdiff -u -r1.86 -r1.87 src/sys/arch/i386/i386/db_interface.c cvs rdiff -u -r1.14 -r1.15 src/sys/arch/x86/x86/idt.c cvs rdiff -u -r1.156 -r1.157 src/sys/arch/x86/x86/intr.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/amd64/amd64/db_interface.c diff -u src/sys/arch/amd64/amd64/db_interface.c:1.39 src/sys/arch/amd64/amd64/db_interface.c:1.40 --- src/sys/arch/amd64/amd64/db_interface.c:1.39 Tue Feb 23 07:13:51 2021 +++ src/sys/arch/amd64/amd64/db_interface.c Thu Dec 23 02:07:21 2021 @@ -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 @@ db_machine_init(void) 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 Index: src/sys/arch/i386/i386/db_interface.c diff -u src/sys/arch/i386/i386/db_interface.c:1.86 src/sys/arch/i386/i386/db_interface.c:1.87 --- src/sys/arch/i386/i386/db_interface.c:1.86 Tue Feb 23 07:13:52 2021 +++ src/sys/arch/i386/i386/db_interface.c Thu Dec 23 02:07:21 2021 @@ -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 @@ db_machine_init(void) 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() */ Index: src/sys/arch/x86/x86/idt.c diff -u src/sys/arch/x86/x86/idt.c:1.14 src/sys/arch/x86/x86/idt.c:1.15 --- src/sys/arch/x86/x86/idt.c:1.14 Tue Jul 14 15:59:21 2020 +++ src/sys/arch/x86/x86/idt.c Thu Dec 23 02:07:21 2021 @@ -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 @@ idt_vec_alloc(struct idt_vec *iv, int lo 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 @@ idt_vec_alloc(struct idt_vec *iv, int lo return vec; } } - return 0; + + return -1; } void @@ -189,7 +193,7 @@ idt_vec_reserve(struct idt_vec *iv, int 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 @@ idt_vec_set(struct idt_vec *iv, int vec, 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_vec_free(struct idt_vec *iv, int vec 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; Index: src/sys/arch/x86/x86/intr.c diff -u src/sys/arch/x86/x86/intr.c:1.156 src/sys/arch/x86/x86/intr.c:1.157 --- src/sys/arch/x86/x86/intr.c:1.156 Thu Oct 7 12:52:27 2021 +++ src/sys/arch/x86/x86/intr.c Thu Dec 23 02:07:21 2021 @@ -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 @@ intr_allocate_slot(struct pic *pic, int 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 @@ intr_deactivate_xcall(void *arg1, void * 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 @@ intr_set_affinity(struct intrsource *isp 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)