Source-Changes-HG archive

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

[src/trunk]: src/sys Simplify acpi_enter_sleep_state() and guard it against N...



details:   https://anonhg.NetBSD.org/src/rev/c19e9bf94fa1
branches:  trunk
changeset: 758166:c19e9bf94fa1
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Sun Oct 24 07:53:04 2010 +0000

description:
Simplify acpi_enter_sleep_state() and guard it against NULL pointer
dereferences. Try to avoid referencing the global acpi_softc, which should
really be static or at least internal to acpi(4).

diffstat:

 sys/arch/amd64/amd64/machdep.c |   9 +++------
 sys/arch/i386/i386/machdep.c   |   9 +++------
 sys/dev/acpi/acpi.c            |  21 +++++++++++++++++----
 sys/dev/acpi/acpi_apm.c        |  11 +++++------
 sys/dev/acpi/acpivar.h         |   4 ++--
 5 files changed, 30 insertions(+), 24 deletions(-)

diffs (197 lines):

diff -r 5ce3f2484f60 -r c19e9bf94fa1 sys/arch/amd64/amd64/machdep.c
--- a/sys/arch/amd64/amd64/machdep.c    Sun Oct 24 06:57:04 2010 +0000
+++ b/sys/arch/amd64/amd64/machdep.c    Sun Oct 24 07:53:04 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: machdep.c,v 1.153 2010/10/21 11:28:34 yamt Exp $       */
+/*     $NetBSD: machdep.c,v 1.154 2010/10/24 07:53:05 jruoho Exp $     */
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008
@@ -107,7 +107,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.153 2010/10/21 11:28:34 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.154 2010/10/24 07:53:05 jruoho Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -699,10 +699,7 @@
         if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
 #ifndef XEN
 #if NACPICA > 0
-               if (acpi_softc != NULL) {
-                       acpi_enter_sleep_state(acpi_softc, ACPI_STATE_S5);
-                       printf("WARNING: ACPI powerdown failed!\n");
-               }
+               acpi_enter_sleep_state(ACPI_STATE_S5);
 #endif
 #else /* XEN */
                HYPERVISOR_shutdown();
diff -r 5ce3f2484f60 -r c19e9bf94fa1 sys/arch/i386/i386/machdep.c
--- a/sys/arch/i386/i386/machdep.c      Sun Oct 24 06:57:04 2010 +0000
+++ b/sys/arch/i386/i386/machdep.c      Sun Oct 24 07:53:04 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: machdep.c,v 1.695 2010/10/21 11:17:55 yamt Exp $       */
+/*     $NetBSD: machdep.c,v 1.696 2010/10/24 07:53:04 jruoho Exp $     */
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.695 2010/10/21 11:17:55 yamt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.696 2010/10/24 07:53:04 jruoho Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_ibcs2.h"
@@ -923,10 +923,7 @@
                }
 #endif
 #if NACPICA > 0
-               if (acpi_softc != NULL) {
-                       acpi_enter_sleep_state(acpi_softc, ACPI_STATE_S5);
-                       printf("WARNING: ACPI powerdown failed!\n");
-               }
+               acpi_enter_sleep_state(ACPI_STATE_S5);
 #endif
 #if NAPMBIOS > 0 && !defined(APM_NO_POWEROFF)
                /* turn off, if we can.  But try to turn disk off and
diff -r 5ce3f2484f60 -r c19e9bf94fa1 sys/dev/acpi/acpi.c
--- a/sys/dev/acpi/acpi.c       Sun Oct 24 06:57:04 2010 +0000
+++ b/sys/dev/acpi/acpi.c       Sun Oct 24 07:53:04 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: acpi.c,v 1.221 2010/10/12 19:10:50 gsutre Exp $        */
+/*     $NetBSD: acpi.c,v 1.222 2010/10/24 07:53:04 jruoho Exp $        */
 
 /*-
  * Copyright (c) 2003, 2007 The NetBSD Foundation, Inc.
@@ -100,7 +100,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.221 2010/10/12 19:10:50 gsutre Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.222 2010/10/24 07:53:04 jruoho Exp $");
 
 #include "opt_acpi.h"
 #include "opt_pcifixup.h"
@@ -262,6 +262,7 @@
 
        once = 1;
        func = NULL;
+       acpi_softc = NULL;
        initialized = false;
 
        mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
@@ -372,6 +373,11 @@
 acpi_disable(void)
 {
 
+       if (acpi_softc == NULL)
+               return;
+
+       KASSERT(acpi_active != 0);
+
        if (AcpiGbl_FADT.SmiCommand != 0)
                AcpiDisable();
 }
@@ -1354,14 +1360,21 @@
 }
 
 void
-acpi_enter_sleep_state(struct acpi_softc *sc, int state)
+acpi_enter_sleep_state(int state)
 {
+       struct acpi_softc *sc = acpi_softc;
        ACPI_STATUS rv;
        int err;
 
+       if (acpi_softc == NULL)
+               return;
+
        if (state == sc->sc_sleepstate)
                return;
 
+       if (state < ACPI_STATE_S0 || state > ACPI_STATE_S5)
+               return;
+
        aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
 
        switch (state) {
@@ -1608,7 +1621,7 @@
        if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
                return EINVAL;
 
-       acpi_enter_sleep_state(sc, t);
+       acpi_enter_sleep_state(t);
 
        return 0;
 }
diff -r 5ce3f2484f60 -r c19e9bf94fa1 sys/dev/acpi/acpi_apm.c
--- a/sys/dev/acpi/acpi_apm.c   Sun Oct 24 06:57:04 2010 +0000
+++ b/sys/dev/acpi/acpi_apm.c   Sun Oct 24 07:53:04 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: acpi_apm.c,v 1.19 2010/04/27 08:37:07 jruoho Exp $     */
+/*     $NetBSD: acpi_apm.c,v 1.20 2010/10/24 07:53:04 jruoho Exp $     */
 
 /*-
  * Copyright (c) 2006 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_apm.c,v 1.19 2010/04/27 08:37:07 jruoho Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_apm.c,v 1.20 2010/10/24 07:53:04 jruoho Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -97,7 +97,7 @@
 static int acpiapm_node = CTL_EOL, standby_node = CTL_EOL;
 
 struct acpi_softc;
-extern void acpi_enter_sleep_state(struct acpi_softc *, int);
+extern void acpi_enter_sleep_state(int);
 static int acpiapm_match(device_t, cfdata_t , void *);
 static void acpiapm_attach(device_t, device_t, void *);
 static int sysctl_state(SYSCTLFN_PROTO);
@@ -232,7 +232,6 @@
 static int
 acpiapm_set_powstate(void *opaque, u_int devid, u_int powstat)
 {
-       struct acpi_softc *sc = device_private((device_t)opaque);
 
        if (devid != APM_DEV_ALLDEVS)
                return APM_ERR_UNRECOG_DEV;
@@ -241,11 +240,11 @@
        case APM_SYS_READY:
                break;
        case APM_SYS_STANDBY:
-               acpi_enter_sleep_state(sc, get_state_value(standby_state));
+               acpi_enter_sleep_state(get_state_value(standby_state));
                resumed = 1;
                break;
        case APM_SYS_SUSPEND:
-               acpi_enter_sleep_state(sc, get_state_value(suspend_state));
+               acpi_enter_sleep_state(get_state_value(suspend_state));
                resumed = 1;
                break;
        case APM_SYS_OFF:
diff -r 5ce3f2484f60 -r c19e9bf94fa1 sys/dev/acpi/acpivar.h
--- a/sys/dev/acpi/acpivar.h    Sun Oct 24 06:57:04 2010 +0000
+++ b/sys/dev/acpi/acpivar.h    Sun Oct 24 07:53:04 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: acpivar.h,v 1.63 2010/09/24 07:48:59 gsutre Exp $      */
+/*     $NetBSD: acpivar.h,v 1.64 2010/10/24 07:53:04 jruoho Exp $      */
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -309,7 +309,7 @@
 /*
  * Sleep state transition.
  */
-void                   acpi_enter_sleep_state(struct acpi_softc *, int);
+void                   acpi_enter_sleep_state(int);
 
 /*
  * MADT.



Home | Main Index | Thread Index | Old Index