mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-07-20 11:41:44 +00:00
Update kernel
This commit is contained in:
306
Core/Lock.cpp
306
Core/Lock.cpp
@@ -27,52 +27,67 @@
|
||||
#endif
|
||||
|
||||
#ifdef PRINT_BACKTRACE
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wframe-address"
|
||||
|
||||
void PrintStacktrace(LockClass::SpinLockData *Lock)
|
||||
{
|
||||
if (KernelSymbolTable)
|
||||
{
|
||||
struct StackFrame
|
||||
{
|
||||
uintptr_t BasePointer;
|
||||
uintptr_t ReturnAddress;
|
||||
};
|
||||
if (KernelSymbolTable)
|
||||
{
|
||||
struct StackFrame
|
||||
{
|
||||
uintptr_t BasePointer;
|
||||
uintptr_t ReturnAddress;
|
||||
};
|
||||
|
||||
char DbgAttempt[1024] = "\0";
|
||||
char DbgHolder[1024] = "\0";
|
||||
// char DbgAttempt[1024] = "\0";
|
||||
// char DbgHolder[1024] = "\0";
|
||||
|
||||
StackFrame *FrameAttempt = (StackFrame *)Lock->StackPointerAttempt.load();
|
||||
StackFrame *FrameHolder = (StackFrame *)Lock->StackPointerHolder.load();
|
||||
std::string DbgAttempt = "\0";
|
||||
std::string DbgHolder = "\0";
|
||||
|
||||
while (Memory::Virtual().Check(FrameAttempt))
|
||||
{
|
||||
sprintf(DbgAttempt + strlen(DbgAttempt), "%s<-", KernelSymbolTable->GetSymbolFromAddress(FrameAttempt->ReturnAddress));
|
||||
FrameAttempt = (StackFrame *)FrameAttempt->BasePointer;
|
||||
}
|
||||
StackFrame *FrameAttempt = (StackFrame *)Lock->StackPointerAttempt.load();
|
||||
StackFrame *FrameHolder = (StackFrame *)Lock->StackPointerHolder.load();
|
||||
|
||||
while (Memory::Virtual().Check(FrameHolder))
|
||||
{
|
||||
sprintf(DbgHolder + strlen(DbgHolder), "%s<-", KernelSymbolTable->GetSymbolFromAddress(FrameHolder->ReturnAddress));
|
||||
FrameHolder = (StackFrame *)FrameHolder->BasePointer;
|
||||
}
|
||||
while (Memory::Virtual().Check(FrameAttempt))
|
||||
{
|
||||
DbgAttempt.concat(KernelSymbolTable->GetSymbolFromAddress(FrameAttempt->ReturnAddress));
|
||||
DbgAttempt.concat("<-");
|
||||
FrameAttempt = (StackFrame *)FrameAttempt->BasePointer;
|
||||
}
|
||||
debug("Attempt: %s", DbgAttempt.c_str());
|
||||
|
||||
debug("Attempt: %s", DbgAttempt);
|
||||
debug("Holder: %s", DbgHolder);
|
||||
while (Memory::Virtual().Check(FrameHolder))
|
||||
{
|
||||
DbgHolder.concat(KernelSymbolTable->GetSymbolFromAddress(FrameHolder->ReturnAddress));
|
||||
DbgHolder.concat("<-");
|
||||
FrameHolder = (StackFrame *)FrameHolder->BasePointer;
|
||||
}
|
||||
|
||||
// debug("\t\t%s<-%s<-%s<-%s<-%s<-%s<-%s<-%s<-%s<-%s",
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(0)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(1)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(2)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(3)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(4)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(5)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(6)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(7)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(8)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(9)));
|
||||
}
|
||||
debug("Holder: %s", DbgHolder.c_str());
|
||||
|
||||
// debug("\t\t%s<-%s<-%s<-%s<-%s<-%s<-%s<-%s<-%s<-%s",
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(0)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(1)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(2)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(3)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(4)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(5)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(6)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(7)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(8)),
|
||||
// KernelSymbolTable->GetSymbolFromAddress((uintptr_t)__builtin_return_address(9)));
|
||||
}
|
||||
}
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG
|
||||
#define DEADLOCK_TIMEOUT 0x100000
|
||||
#define DEADLOCK_TIMEOUT_DEBUGGER 0x1000
|
||||
#else
|
||||
#define DEADLOCK_TIMEOUT 0x10000000
|
||||
#define DEADLOCK_TIMEOUT_DEBUGGER 0x100000
|
||||
#endif
|
||||
|
||||
bool ForceUnlock = false;
|
||||
@@ -80,161 +95,168 @@ std::atomic_size_t LocksCount = 0;
|
||||
|
||||
size_t GetLocksCount() { return LocksCount.load(); }
|
||||
|
||||
void LockClass::DeadLock(SpinLockData Lock)
|
||||
void LockClass::Yield()
|
||||
{
|
||||
if (ForceUnlock)
|
||||
{
|
||||
warn("Unlocking lock '%s' which it was held by '%s'...", Lock.AttemptingToGet, Lock.CurrentHolder);
|
||||
this->DeadLocks = 0;
|
||||
this->Unlock();
|
||||
return;
|
||||
}
|
||||
if (CPU::Interrupts(CPU::Check) &&
|
||||
TaskManager &&
|
||||
!TaskManager->IsPanic())
|
||||
{
|
||||
TaskManager->Yield();
|
||||
}
|
||||
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
long CCore = 0xdead;
|
||||
if (CoreData != nullptr)
|
||||
CCore = CoreData->ID;
|
||||
CPU::Pause();
|
||||
}
|
||||
|
||||
warn("Potential deadlock in lock '%s' held by '%s'! %ld %s in queue. Interrupts are %s. Core %ld held by %ld. (%ld times happened)",
|
||||
Lock.AttemptingToGet, Lock.CurrentHolder,
|
||||
Lock.Count, Lock.Count > 1 ? "locks" : "lock",
|
||||
CPU::Interrupts(CPU::Check) ? "enabled" : "disabled",
|
||||
CCore, Lock.Core, this->DeadLocks);
|
||||
void LockClass::DeadLock(SpinLockData &Lock)
|
||||
{
|
||||
if (ForceUnlock)
|
||||
{
|
||||
warn("Unlocking lock '%s' which it was held by '%s'...",
|
||||
Lock.AttemptingToGet, Lock.CurrentHolder);
|
||||
this->DeadLocks = 0;
|
||||
this->Unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
long CCore = 0xdead;
|
||||
if (CoreData != nullptr)
|
||||
CCore = CoreData->ID;
|
||||
|
||||
warn("Potential deadlock in lock '%s' held by '%s'! %ld %s in queue. Interrupts are %s. Core %ld held by %ld. (%ld times happened)",
|
||||
Lock.AttemptingToGet, Lock.CurrentHolder,
|
||||
Lock.Count, Lock.Count > 1 ? "locks" : "lock",
|
||||
CPU::Interrupts(CPU::Check) ? "enabled" : "disabled",
|
||||
CCore, Lock.Core, this->DeadLocks);
|
||||
|
||||
#ifdef PRINT_BACKTRACE
|
||||
PrintStacktrace(&Lock);
|
||||
PrintStacktrace(&Lock);
|
||||
#endif
|
||||
|
||||
// TODO: Print on screen too.
|
||||
// TODO: Print on screen too.
|
||||
|
||||
this->DeadLocks++;
|
||||
this->DeadLocks++;
|
||||
|
||||
if (Config.UnlockDeadLock && this->DeadLocks.load() > 10)
|
||||
{
|
||||
warn("Unlocking lock '%s' to prevent deadlock. (this is enabled in the kernel config)", Lock.AttemptingToGet);
|
||||
this->DeadLocks = 0;
|
||||
this->Unlock();
|
||||
}
|
||||
if (Config.UnlockDeadLock && this->DeadLocks.load() > 10)
|
||||
{
|
||||
warn("Unlocking lock '%s' to prevent deadlock. (this is enabled in the kernel config)", Lock.AttemptingToGet);
|
||||
this->DeadLocks = 0;
|
||||
this->Unlock();
|
||||
}
|
||||
|
||||
if (TaskManager && !TaskManager->IsPanic())
|
||||
TaskManager->Schedule();
|
||||
this->Yield();
|
||||
}
|
||||
|
||||
int LockClass::Lock(const char *FunctionName)
|
||||
{
|
||||
LockData.AttemptingToGet = FunctionName;
|
||||
LockData.StackPointerAttempt = (uintptr_t)__builtin_frame_address(0);
|
||||
LockData.AttemptingToGet = FunctionName;
|
||||
LockData.StackPointerAttempt = (uintptr_t)__builtin_frame_address(0);
|
||||
|
||||
Retry:
|
||||
int i = 0;
|
||||
while (IsLocked.exchange(true, std::memory_order_acquire) && ++i < (DebuggerIsAttached ? 0x100000 : 0x10000000))
|
||||
{
|
||||
/* FIXME */
|
||||
// if (TaskManager && !TaskManager->IsPanic())
|
||||
// TaskManager->Schedule();
|
||||
// else
|
||||
CPU::Pause();
|
||||
}
|
||||
int i = 0;
|
||||
while (IsLocked.exchange(true, std::memory_order_acquire) &&
|
||||
++i < (DebuggerIsAttached ? DEADLOCK_TIMEOUT_DEBUGGER : DEADLOCK_TIMEOUT))
|
||||
{
|
||||
this->Yield();
|
||||
}
|
||||
|
||||
if (i >= (DebuggerIsAttached ? 0x100000 : 0x10000000))
|
||||
{
|
||||
DeadLock(LockData);
|
||||
goto Retry;
|
||||
}
|
||||
if (i >= (DebuggerIsAttached ? DEADLOCK_TIMEOUT_DEBUGGER : DEADLOCK_TIMEOUT))
|
||||
{
|
||||
DeadLock(LockData);
|
||||
goto Retry;
|
||||
}
|
||||
|
||||
LockData.Count++;
|
||||
LockData.CurrentHolder = FunctionName;
|
||||
LockData.StackPointerHolder = (uintptr_t)__builtin_frame_address(0);
|
||||
LockData.Count++;
|
||||
LockData.CurrentHolder = FunctionName;
|
||||
LockData.StackPointerHolder = (uintptr_t)__builtin_frame_address(0);
|
||||
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
if (CoreData != nullptr)
|
||||
LockData.Core = CoreData->ID;
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
if (CoreData != nullptr)
|
||||
LockData.Core = CoreData->ID;
|
||||
|
||||
LocksCount++;
|
||||
LocksCount++;
|
||||
|
||||
__sync;
|
||||
return 0;
|
||||
__sync;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LockClass::Unlock()
|
||||
{
|
||||
__sync;
|
||||
__sync;
|
||||
|
||||
IsLocked.store(false, std::memory_order_release);
|
||||
LockData.Count--;
|
||||
LocksCount--;
|
||||
IsLocked.store(false, std::memory_order_release);
|
||||
LockData.Count--;
|
||||
LocksCount--;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LockClass::TimeoutDeadLock(SpinLockData Lock, uint64_t Timeout)
|
||||
void LockClass::TimeoutDeadLock(SpinLockData &Lock, uint64_t Timeout)
|
||||
{
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
long CCore = 0xdead;
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
long CCore = 0xdead;
|
||||
|
||||
if (CoreData != nullptr)
|
||||
CCore = CoreData->ID;
|
||||
if (CoreData != nullptr)
|
||||
CCore = CoreData->ID;
|
||||
|
||||
uint64_t Counter = TimeManager->GetCounter();
|
||||
uint64_t Counter = TimeManager->GetCounter();
|
||||
|
||||
warn("Potential deadlock in lock '%s' held by '%s'! %ld %s in queue. Interrupts are %s. Core %ld held by %ld. Timeout in %ld (%ld ticks remaining).",
|
||||
Lock.AttemptingToGet, Lock.CurrentHolder,
|
||||
Lock.Count, Lock.Count > 1 ? "locks" : "lock",
|
||||
CPU::Interrupts(CPU::Check) ? "enabled" : "disabled",
|
||||
CCore, Lock.Core, Timeout, Timeout - Counter);
|
||||
warn("Potential deadlock in lock '%s' held by '%s'! %ld %s in queue. Interrupts are %s. Core %ld held by %ld. Timeout in %ld (%ld ticks remaining).",
|
||||
Lock.AttemptingToGet, Lock.CurrentHolder,
|
||||
Lock.Count, Lock.Count > 1 ? "locks" : "lock",
|
||||
CPU::Interrupts(CPU::Check) ? "enabled" : "disabled",
|
||||
CCore, Lock.Core, Timeout, Timeout - Counter);
|
||||
|
||||
#ifdef PRINT_BACKTRACE
|
||||
PrintStacktrace(&Lock);
|
||||
PrintStacktrace(&Lock);
|
||||
#endif
|
||||
|
||||
if (Timeout < Counter)
|
||||
{
|
||||
warn("Unlocking lock '%s' because of timeout. (%ld < %ld)", Lock.AttemptingToGet, Timeout, Counter);
|
||||
this->Unlock();
|
||||
}
|
||||
if (Timeout < Counter)
|
||||
{
|
||||
warn("Unlocking lock '%s' because of timeout. (%ld < %ld)",
|
||||
Lock.AttemptingToGet, Timeout, Counter);
|
||||
this->Unlock();
|
||||
}
|
||||
|
||||
if (TaskManager && !TaskManager->IsPanic())
|
||||
TaskManager->Schedule();
|
||||
this->Yield();
|
||||
}
|
||||
|
||||
int LockClass::TimeoutLock(const char *FunctionName, uint64_t Timeout)
|
||||
{
|
||||
if (!TimeManager)
|
||||
return Lock(FunctionName);
|
||||
if (!TimeManager)
|
||||
return Lock(FunctionName);
|
||||
|
||||
LockData.AttemptingToGet = FunctionName;
|
||||
LockData.StackPointerAttempt = (uintptr_t)__builtin_frame_address(0);
|
||||
LockData.AttemptingToGet = FunctionName;
|
||||
LockData.StackPointerAttempt = (uintptr_t)__builtin_frame_address(0);
|
||||
|
||||
std::atomic_uint64_t Target = 0;
|
||||
std::atomic_uint64_t Target = 0;
|
||||
Retry:
|
||||
int i = 0;
|
||||
while (IsLocked.exchange(true, std::memory_order_acquire) && ++i < (DebuggerIsAttached ? 0x100000 : 0x10000000))
|
||||
{
|
||||
/* FIXME */
|
||||
// if (TaskManager && !TaskManager->IsPanic())
|
||||
// TaskManager->Schedule();
|
||||
// else
|
||||
CPU::Pause();
|
||||
}
|
||||
int i = 0;
|
||||
while (IsLocked.exchange(true, std::memory_order_acquire) &&
|
||||
++i < (DebuggerIsAttached ? DEADLOCK_TIMEOUT_DEBUGGER : DEADLOCK_TIMEOUT))
|
||||
{
|
||||
this->Yield();
|
||||
}
|
||||
|
||||
if (i >= (DebuggerIsAttached ? 0x100000 : 0x10000000))
|
||||
{
|
||||
if (Target.load() == 0)
|
||||
Target.store(TimeManager->CalculateTarget(Timeout, Time::Units::Milliseconds));
|
||||
TimeoutDeadLock(LockData, Target.load());
|
||||
goto Retry;
|
||||
}
|
||||
if (i >= (DebuggerIsAttached ? DEADLOCK_TIMEOUT_DEBUGGER : DEADLOCK_TIMEOUT))
|
||||
{
|
||||
if (Target.load() == 0)
|
||||
Target.store(TimeManager->CalculateTarget(Timeout,
|
||||
Time::Units::Milliseconds));
|
||||
TimeoutDeadLock(LockData, Target.load());
|
||||
goto Retry;
|
||||
}
|
||||
|
||||
LockData.Count++;
|
||||
LockData.CurrentHolder = FunctionName;
|
||||
LockData.StackPointerHolder = (uintptr_t)__builtin_frame_address(0);
|
||||
LockData.Count++;
|
||||
LockData.CurrentHolder = FunctionName;
|
||||
LockData.StackPointerHolder = (uintptr_t)__builtin_frame_address(0);
|
||||
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
if (CoreData != nullptr)
|
||||
LockData.Core = CoreData->ID;
|
||||
CPUData *CoreData = GetCurrentCPU();
|
||||
if (CoreData != nullptr)
|
||||
LockData.Core = CoreData->ID;
|
||||
|
||||
LocksCount++;
|
||||
LocksCount++;
|
||||
|
||||
__sync;
|
||||
return 0;
|
||||
__sync;
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user