Skip to content

Commit

Permalink
fix: spinlock
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <[email protected]>
  • Loading branch information
MRNIU committed Jan 7, 2025
1 parent 42c1eaa commit 27a5314
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
18 changes: 9 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@ repos:
# - id: clang-tidy
# args:
# - --checks=.clang-tidy
# - -p=/root/SimpleKernel/build_aarch64/compiler_commands.json
# - -p=/root/SimpleKernel/build_riscv64/compiler_commands.json
# - -extra-arg=--std=c++2b
# - --fix
# - --header-filter=^(/root/SimpleKernel/build_aarch64/src/).*
# - --exclude-header-filter=^(?/root/SimpleKernel/build_aarch64/src/arch/x86_64|/root/SimpleKernel/build_aarch64/src/arch/aarch64).*
# - --header-filter=^(/root/SimpleKernel/build_riscv64/src/).*
# - --exclude-header-filter=^(?/root/SimpleKernel/build_riscv64/src/arch/x86_64|/root/SimpleKernel/build_riscv64/src/arch/aarch64).*
# - id: clang-tidy
# args:
# - --checks=.clang-tidy
# - -p=/root/SimpleKernel/build_aarch64/build_x86_64/compiler_commands.json
# - -p=/root/SimpleKernel/build_riscv64/build_x86_64/compiler_commands.json
# - -extra-arg=--std=c++2b
# - --fix
# - --header-filter=^(?/root/SimpleKernel/build_aarch64/src/).*
# - --exclude-header-filter=^(?/root/SimpleKernel/build_aarch64/src/arch/riscv_64|/root/SimpleKernel/build_aarch64/src/arch/aarch64).*
# - --header-filter=^(?/root/SimpleKernel/build_riscv64/src/).*
# - --exclude-header-filter=^(?/root/SimpleKernel/build_riscv64/src/arch/riscv_64|/root/SimpleKernel/build_riscv64/src/arch/aarch64).*
# - id: clang-tidy
# args:
# - --checks=.clang-tidy
# - -p=/root/SimpleKernel/build_aarch64/build_aarch64/compiler_commands.json
# - -p=/root/SimpleKernel/build_riscv64/build_aarch64/compiler_commands.json
# - -extra-arg=--std=c++2b
# - --fix
# - --header-filter=^(?/root/SimpleKernel/build_aarch64/src/).*
# - --exclude-header-filter=^(?/root/SimpleKernel/build_aarch64/src/arch/riscv64|/root/SimpleKernel/build_aarch64/src/arch/x86_64).*
# - --header-filter=^(?/root/SimpleKernel/build_riscv64/src/).*
# - --exclude-header-filter=^(?/root/SimpleKernel/build_riscv64/src/arch/riscv64|/root/SimpleKernel/build_riscv64/src/arch/x86_64).*

- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.10.0
Expand Down
9 changes: 2 additions & 7 deletions src/kernel/arch/riscv64/arch_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ auto ArchInit(uint32_t argc, const uint8_t *argv) -> uint32_t {
printf("boot hart id: %d\n", argc);
printf("dtb info addr: %p\n", argv);

for (auto i = 0; i < 8; i++) {
g_per_cpu[i].core_id_ = SIZE_MAX;
}

// 将 core id 保存到 tp 寄存器
cpu_io::Tp::Write(argc);
g_per_cpu[argc].core_id_ = argc;
GetCurrentCore().core_id_ = argc;

Singleton<KernelFdt>::GetInstance() =
KernelFdt(reinterpret_cast<uint64_t>(argv));
Expand Down Expand Up @@ -99,8 +95,7 @@ auto ArchInit(uint32_t argc, const uint8_t *argv) -> uint32_t {

auto ArchInitSMP(uint32_t argc, const uint8_t *) -> uint32_t {
cpu_io::Tp::Write(argc);
g_per_cpu[argc].core_id_ = argc;
GetCurrentCore().core_id_ = argc;
Singleton<BasicInfo>::GetInstance().core_count++;

return 0;
}
2 changes: 1 addition & 1 deletion src/kernel/arch/x86_64/arch_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ auto ArchInit(uint32_t argc, const uint8_t *argv) -> uint32_t {
throw;
}

g_per_cpu[cpu_io::GetCurrentCoreId()].core_id_ = cpu_io::GetCurrentCoreId();
GetCurrentCore().core_id_ = cpu_io::GetCurrentCoreId();

Singleton<BasicInfo>::GetInstance() = BasicInfo(argc, argv);
Singleton<BasicInfo>::GetInstance().core_count++;
Expand Down
10 changes: 9 additions & 1 deletion src/kernel/include/per_cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef SIMPLEKERNEL_SRC_KERNEL_INCLUDE_PER_CPU_HPP_
#define SIMPLEKERNEL_SRC_KERNEL_INCLUDE_PER_CPU_HPP_

#include <cpu_io.h>
#include <unistd.h>

#include <array>
Expand All @@ -25,6 +26,8 @@
#include <cstdint>
#include <cstdlib>

#include "singleton.hpp"

class PerCpu {
public:
/// 最大 CPU 数
Expand Down Expand Up @@ -55,6 +58,11 @@ class PerCpu {
};

/// per cpu 数据
static std::array<PerCpu, PerCpu::kMaxCoreCount> g_per_cpu{};
// static std::array<PerCpu, PerCpu::kMaxCoreCount> g_per_cpu{};

static __always_inline auto GetCurrentCore() -> PerCpu & {
return Singleton<std::array<PerCpu, PerCpu::kMaxCoreCount>>::GetInstance()
[cpu_io::GetCurrentCoreId()];
}

#endif /* SIMPLEKERNEL_SRC_KERNEL_INCLUDE_PER_CPU_HPP_ */
13 changes: 6 additions & 7 deletions src/kernel/include/spinlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ class SpinLock {

DisableInterrupt();

if (g_per_cpu[GetCurrentCoreId()].noff_ == 0) {
g_per_cpu[GetCurrentCoreId()].intr_enable_ = old;
if (GetCurrentCore().noff_ == 0) {
GetCurrentCore().intr_enable_ = old;
}
g_per_cpu[GetCurrentCoreId()].noff_ += 1;
GetCurrentCore().noff_ += 1;
}

/**
Expand All @@ -130,13 +130,12 @@ class SpinLock {
printf("RestoreInterruptsNested - interruptible\n");
}

if (g_per_cpu[GetCurrentCoreId()].noff_ < 1) {
if (GetCurrentCore().noff_ < 1) {
printf("RestoreInterruptsNested\n");
}
g_per_cpu[GetCurrentCoreId()].noff_ -= 1;
GetCurrentCore().noff_ -= 1;

if ((g_per_cpu[GetCurrentCoreId()].noff_ == 0) &&
(g_per_cpu[GetCurrentCoreId()].intr_enable_)) {
if ((GetCurrentCore().noff_ == 0) && (GetCurrentCore().intr_enable_)) {
EnableInterrupt();
}
}
Expand Down
2 changes: 0 additions & 2 deletions test/unit_test/spinlock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <thread>
#include <vector>

// static std::array<PerCpu, PerCpu::kMaxCoreCount> g_per_cpu{};

class TestableSpinLock : public SpinLock {
public:
using SpinLock::SpinLock;
Expand Down

0 comments on commit 27a5314

Please sign in to comment.