From e736a456ba4d12621654a3f95c394d11fcaac243 Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Thu, 28 Nov 2013 23:28:33 +0100 Subject: [PATCH] kernel: Forbid implicit casts between spinlock and int32 --- headers/os/drivers/KernelExport.h | 14 +++++--- .../kernel/bus_managers/firewire/timer.cpp | 2 +- .../drivers/audio/ac97/sis7018/Device.cpp | 3 +- .../drivers/audio/ac97/sis7018/Device.h | 2 +- src/add-ons/kernel/drivers/audio/echo/util.c | 2 +- .../kernel/drivers/audio/ice1712/util.c | 2 +- .../drivers/disk/floppy/pc_floppy/floppy.c | 2 +- .../kernel/drivers/network/sis19x/DataRing.h | 4 +-- .../kernel/drivers/network/sis19x/Device.cpp | 3 +- .../kernel/drivers/network/sis19x/Device.h | 2 +- src/system/kernel/smp.cpp | 32 ++++++++++--------- 11 files changed, 38 insertions(+), 30 deletions(-) diff --git a/headers/os/drivers/KernelExport.h b/headers/os/drivers/KernelExport.h index de40d0c76d..4f7e8e2969 100644 --- a/headers/os/drivers/KernelExport.h +++ b/headers/os/drivers/KernelExport.h @@ -31,15 +31,19 @@ typedef ulong cpu_status; (spinlock)->count_low = 0; \ (spinlock)->count_high = 0; \ } while (false) -# define B_SPINLOCK_IS_LOCKED(spinlock) ((spinlock)->lock > 0) #else - typedef int32 spinlock; + typedef struct { + int32 lock; + } spinlock; -# define B_SPINLOCK_INITIALIZER 0 -# define B_INITIALIZE_SPINLOCK(lock) do { *(lock) = 0; } while (false) -# define B_SPINLOCK_IS_LOCKED(lock) (*(lock) > 0) +# define B_SPINLOCK_INITIALIZER { 0 } +# define B_INITIALIZE_SPINLOCK(spinlock) do { \ + (spinlock)->lock = 0; \ + } while (false) #endif +#define B_SPINLOCK_IS_LOCKED(spinlock) (atomic_get(&(spinlock)->lock) > 0) + typedef struct { int32 lock; } rw_spinlock; diff --git a/src/add-ons/kernel/bus_managers/firewire/timer.cpp b/src/add-ons/kernel/bus_managers/firewire/timer.cpp index 7299d9037b..4df8780784 100644 --- a/src/add-ons/kernel/bus_managers/firewire/timer.cpp +++ b/src/add-ons/kernel/bus_managers/firewire/timer.cpp @@ -188,7 +188,7 @@ initialize_timer(void) { sTimerCount = 0; sTimerNextId = 1; - sTimerSpinlock = 0; + B_INITIALIZE_SPINLOCK(&sTimerSpinlock); sTimerThread = spawn_kernel_thread(timer_thread, "firewire timer", 80, 0); sTimerSem = create_sem(0, "firewire timer"); diff --git a/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.cpp b/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.cpp index e432ec88b2..80c1ab4fad 100644 --- a/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.cpp +++ b/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.cpp @@ -22,13 +22,14 @@ Device::Device(Device::Info &DeviceInfo, pci_info &PCIInfo) fPCIInfo(PCIInfo), fInfo(DeviceInfo), fIOBase(0), - fHWSpinlock(0), fInterruptsNest(0), fBuffersReadySem(-1), fMixer(this), fPlaybackStream(this, false), fRecordStream(this, true) { + B_INITIALIZE_SPINLOCK(&fHWSpinlock); + fStatus = _ReserveDeviceOnBus(true); if (fStatus != B_OK) return; // InitCheck will handle the rest diff --git a/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.h b/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.h index 399161a33d..974d781d0a 100644 --- a/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.h +++ b/src/add-ons/kernel/drivers/audio/ac97/sis7018/Device.h @@ -88,7 +88,7 @@ private: pci_info fPCIInfo; Info& fInfo; int fIOBase; - int32 fHWSpinlock; + spinlock fHWSpinlock; int32 fInterruptsNest; sem_id fBuffersReadySem; diff --git a/src/add-ons/kernel/drivers/audio/echo/util.c b/src/add-ons/kernel/drivers/audio/echo/util.c index 950ad76135..a35107607f 100644 --- a/src/add-ons/kernel/drivers/audio/echo/util.c +++ b/src/add-ons/kernel/drivers/audio/echo/util.c @@ -34,7 +34,7 @@ #include "debug.h" #include "util.h" -spinlock slock = 0; +spinlock slock = B_SPINLOCK_INITIALIZER; uint32 round_to_pagesize(uint32 size); diff --git a/src/add-ons/kernel/drivers/audio/ice1712/util.c b/src/add-ons/kernel/drivers/audio/ice1712/util.c index c036e73fec..2feea676aa 100644 --- a/src/add-ons/kernel/drivers/audio/ice1712/util.c +++ b/src/add-ons/kernel/drivers/audio/ice1712/util.c @@ -16,7 +16,7 @@ #include "debug.h" #include "util.h" -spinlock slock = 0; +spinlock slock = B_SPINLOCK_INITIALIZER; uint32 round_to_pagesize(uint32 size); diff --git a/src/add-ons/kernel/drivers/disk/floppy/pc_floppy/floppy.c b/src/add-ons/kernel/drivers/disk/floppy/pc_floppy/floppy.c index e2f4ed7263..b78cda248d 100644 --- a/src/add-ons/kernel/drivers/disk/floppy/pc_floppy/floppy.c +++ b/src/add-ons/kernel/drivers/disk/floppy/pc_floppy/floppy.c @@ -222,7 +222,7 @@ init_driver(void) B_FULL_LOCK, B_READ_AREA|B_WRITE_AREA); if (master->buffer_area < B_OK) goto config_error2; - master->slock = 0; + B_INITIALIZE_SPINLOCK(&master->slock); master->isa = isa; if (install_io_interrupt_handler(master->irq, flo_intr, (void *)master, 0) < B_OK) goto config_error2; diff --git a/src/add-ons/kernel/drivers/network/sis19x/DataRing.h b/src/add-ons/kernel/drivers/network/sis19x/DataRing.h index af4ebfb625..a3e74aa7e0 100644 --- a/src/add-ons/kernel/drivers/network/sis19x/DataRing.h +++ b/src/add-ons/kernel/drivers/network/sis19x/DataRing.h @@ -43,7 +43,7 @@ private: bool fIsTx; status_t fStatus; area_id fArea; - int32 fSpinlock; + spinlock fSpinlock; sem_id fSemaphore; uint32 fHead; uint32 fTail; @@ -61,12 +61,12 @@ DataRing<__type, __count>::DataRing(Device* device, bool isTx) fIsTx(isTx), fStatus(B_NO_INIT), fArea(-1), - fSpinlock(0), fSemaphore(0), fHead(0), fTail(0), fDescriptors(NULL) { + B_INITIALIZE_SPINLOCK(&fSpinlock); memset(fBuffers, 0, sizeof(fBuffers)); } diff --git a/src/add-ons/kernel/drivers/network/sis19x/Device.cpp b/src/add-ons/kernel/drivers/network/sis19x/Device.cpp index feb94f512a..fb396eadbf 100644 --- a/src/add-ons/kernel/drivers/network/sis19x/Device.cpp +++ b/src/add-ons/kernel/drivers/network/sis19x/Device.cpp @@ -23,7 +23,6 @@ Device::Device(Device::Info &DeviceInfo, pci_info &PCIInfo) fPCIInfo(PCIInfo), fInfo(DeviceInfo), fIOBase(0), - fHWSpinlock(0), fInterruptsNest(0), fFrameSize(MaxFrameSize), fMII(this), @@ -36,6 +35,8 @@ Device::Device(Device::Info &DeviceInfo, pci_info &PCIInfo) { memset((struct timer*)this, 0, sizeof(struct timer)); + B_INITIALIZE_SPINLOCK(&fHWSpinlock); + uint32 cmdRegister = gPCIModule->read_pci_config(PCIInfo.bus, PCIInfo.device, PCIInfo.function, PCI_command, 2); TRACE_ALWAYS("cmdRegister:%#010x\n", cmdRegister); diff --git a/src/add-ons/kernel/drivers/network/sis19x/Device.h b/src/add-ons/kernel/drivers/network/sis19x/Device.h index 30aa6c5eb2..3d73b1326b 100644 --- a/src/add-ons/kernel/drivers/network/sis19x/Device.h +++ b/src/add-ons/kernel/drivers/network/sis19x/Device.h @@ -97,7 +97,7 @@ static int32 _TimerHandler(struct timer* timer); pci_info fPCIInfo; Info& fInfo; int fIOBase; - int32 fHWSpinlock; + spinlock fHWSpinlock; int32 fInterruptsNest; // interface and device infos diff --git a/src/system/kernel/smp.cpp b/src/system/kernel/smp.cpp index 0477b85129..cd25254668 100644 --- a/src/system/kernel/smp.cpp +++ b/src/system/kernel/smp.cpp @@ -1,4 +1,5 @@ /* + * Copyright 2013, Paweł Dziepak, pdziepak@quarnos.org. * Copyright 2008-2009, Ingo Weinhold, ingo_weinhold@gmx.de. * Copyright 2002-2010, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. @@ -26,6 +27,7 @@ #include #include #include +#include #if DEBUG_SPINLOCK_LATENCIES # include #endif @@ -350,7 +352,7 @@ acquire_spinlock(spinlock* lock) #else while (1) { uint32 count = 0; - while (atomic_get(lock) != 0) { + while (atomic_get(&lock->lock) != 0) { if (++count == SPINLOCK_DEADLOCK_COUNT) { panic("acquire_spinlock(): Failed to acquire spinlock %p " "for a long time!", lock); @@ -358,9 +360,9 @@ acquire_spinlock(spinlock* lock) } process_all_pending_ici(currentCPU); - cpu_wait(lock, 0); + cpu_wait(&lock->lock, 0); } - if (atomic_get_and_set((int32*)lock, 1) == 0) + if (atomic_get_and_set(&lock->lock, 1) == 0) break; } @@ -371,7 +373,7 @@ acquire_spinlock(spinlock* lock) } else { #if DEBUG_SPINLOCKS int32 oldValue; - oldValue = atomic_get_and_set((int32*)lock, 1); + oldValue = atomic_get_and_set(&lock->lock, 1); if (oldValue != 0) { panic("acquire_spinlock: attempt to acquire lock %p twice on " "non-SMP system (last caller: %p, value %" B_PRId32 ")", lock, @@ -404,23 +406,23 @@ acquire_spinlock_nocheck(spinlock *lock) #else while (1) { uint32 count = 0; - while (atomic_get(lock) != 0) { + while (atomic_get(&lock->lock) != 0) { if (++count == SPINLOCK_DEADLOCK_COUNT_NO_CHECK) { panic("acquire_spinlock(): Failed to acquire spinlock %p " "for a long time!", lock); count = 0; } - cpu_wait(lock, 0); + cpu_wait(&lock->lock, 0); } - if (atomic_get_and_set((int32*)lock, 1) == 0) + if (atomic_get_and_set(&lock->lock, 1) == 0) break; } #endif } else { #if DEBUG_SPINLOCKS - if (atomic_get_and_set((int32*)lock, 1) != 0) { + if (atomic_get_and_set(&lock->lock, 1) != 0) { panic("acquire_spinlock_nocheck: attempt to acquire lock %p twice " "on non-SMP system\n", lock); } @@ -447,7 +449,7 @@ acquire_spinlock_cpu(int32 currentCPU, spinlock *lock) #else while (1) { uint32 count = 0; - while (atomic_get(lock) != 0) { + while (atomic_get(&lock->lock) != 0) { if (++count == SPINLOCK_DEADLOCK_COUNT) { panic("acquire_spinlock_cpu(): Failed to acquire spinlock " "%p for a long time!", lock); @@ -455,9 +457,9 @@ acquire_spinlock_cpu(int32 currentCPU, spinlock *lock) } process_all_pending_ici(currentCPU); - cpu_wait(lock, 0); + cpu_wait(&lock->lock, 0); } - if (atomic_get_and_set((int32*)lock, 1) == 0) + if (atomic_get_and_set(&lock->lock, 1) == 0) break; } @@ -468,7 +470,7 @@ acquire_spinlock_cpu(int32 currentCPU, spinlock *lock) } else { #if DEBUG_SPINLOCKS int32 oldValue; - oldValue = atomic_get_and_set((int32*)lock, 1); + oldValue = atomic_get_and_set(&lock->lock, 1); if (oldValue != 0) { panic("acquire_spinlock_cpu(): attempt to acquire lock %p twice on " "non-SMP system (last caller: %p, value %" B_PRId32 ")", lock, @@ -506,10 +508,10 @@ release_spinlock(spinlock *lock) } } #elif DEBUG_SPINLOCKS - if (atomic_get_and_set((int32*)lock, 0) != 1) + if (atomic_get_and_set(&lock->lock, 0) != 1) panic("release_spinlock: lock %p was already released\n", lock); #else - atomic_set((int32*)lock, 0); + atomic_set(&lock->lock, 0); #endif } else { #if DEBUG_SPINLOCKS @@ -517,7 +519,7 @@ release_spinlock(spinlock *lock) panic("release_spinlock: attempt to release lock %p with " "interrupts enabled\n", lock); } - if (atomic_get_and_set((int32*)lock, 0) != 1) + if (atomic_get_and_set(&lock->lock, 0) != 1) panic("release_spinlock: lock %p was already released\n", lock); #endif #if DEBUG_SPINLOCK_LATENCIES