summaryrefslogtreecommitdiff
path: root/kernel/src/arch/x86_64/schedule_timer.cpp
blob: 97882c7190ed6e818c05b96846d2b712559d813a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <arch/x86_64/schedule_timer.hpp>
#include <arch/x86_64/cpu/idt.hpp>
#include <arch/x86_64/cpu/xapic.hpp>
#include <arch/x86_64/cpu_local.hpp>
#include <generic/scheduling.hpp>
#include <klibc/stdio.hpp>
#include <generic/mp.hpp>
#include <generic/time.hpp>
#include <generic/lock/spinlock.hpp>
#include <atomic>

extern "C" void scheduler_timer_asm();

int is_timer_off = 0;
std::atomic<int> stfu_cpus = 0;

extern "C" void timer_tick(x86_64::idt::int_frame_t* ctx) {

    if(is_timer_off) {
        stfu_cpus++;
        arch::hcf();
    }

    process::schedule((void*)ctx);
    static std::atomic<int> i = 0;
    klibc::printf("timer tick %lli %d\r", i++,x86_64::cpu_data()->cpu);
    x86_64::apic::eoi();
}

void x86_64::schedule_timer::off() {
    is_timer_off = 1;

    if(mp::cpu_count() <= 1)
        return;

    std::uint32_t timeout = 10;

    klibc::printf("Poweroff: Waiting for all cpus to done work\r\n"); // used from poweroff so
    while(stfu_cpus != (std::int32_t)(mp::cpu_count() - 1)) {
        arch::memory_barrier();

        if(--timeout == 0) {
            klibc::printf("Poweroff: Can't wait longer, forching them to be disabled\r\n");
            x86_64::lapic::off();
            return;
        }

        if(time::timer) {
            time::timer->sleep(50000);
        } else {
            arch::pause();
        }
    }
}

void x86_64::schedule_timer::init() {
    x86_64::idt::set_entry((std::uint64_t)scheduler_timer_asm,32, 0x8E, 2);
}