blob: 60bb649069b649d5a196e9706eb8a16e8d11c6ee (
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;
log("poweroff", "waiting for all cpus to done work"); // used from poweroff so
while(stfu_cpus != (std::int32_t)(mp::cpu_count() - 1)) {
arch::memory_barrier();
if(--timeout == 0) {
log("poweroff", "poweroff: Can't wait longer, forcing them to be disabled");
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);
}
|