summaryrefslogtreecommitdiff
path: root/kernel/src/arch/x86_64/drivers/pvclock.cpp
blob: db191207626eb3d1fbbcc87ee87978c42ef803ec (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define BILLION 1000000000UL
#include <cstdint>
#include <drivers/acpi.hpp>
#include <arch/x86_64/drivers/pvclock.hpp>
#include <arch/x86_64/cpu_local.hpp>
#include <utils/gobject.hpp>
#include <klibc/stdio.hpp>
#include <arch/x86_64/assembly.hpp>
#include <generic/pmm.hpp>
#include <generic/hhdm.hpp>

#include <generic/arch.hpp>

void drivers::pvclock::init() {
    std::uint32_t a,b,c,d;

    assembly::cpuid(0x40000000,0,&a,&b,&c,&d);
    if(b != 0x4b4d564b || d != 0x4d || c != 0x564b4d56)
        return;

    assembly::cpuid(0x40000001,0,&a,&b,&c,&d);
    if(!(a & (1 << 3)))
        return;

    x86_64::cpu_data()->pvclock_buffer = (void*)(pmm::freelist::alloc_4k() + etc::hhdm());

    assembly::wrmsr(0x4b564d01,((std::uint64_t)x86_64::cpu_data()->pvclock_buffer - etc::hhdm()) | 1);
}

void drivers::pvclock::bsp_init() {
    std::uint32_t a,b,c,d;

    assembly::cpuid(0x40000000,0,&a,&b,&c,&d);
    if(b != 0x4b4d564b || d != 0x4d || c != 0x564b4d56)
        return;

    assembly::cpuid(0x40000001,0,&a,&b,&c,&d);
    if(!(a & (1 << 3)))
        return;

    pvclock::init();

    drivers::pvclock_timer* timer = new drivers::pvclock_timer;
    time::setup_timer(timer);

    log("pvclock", "pointer to buffer 0x%p",x86_64::cpu_data()->pvclock_buffer);
}

namespace drivers {
    
    std::uint64_t pvclock_timer::current_nano() {
        auto* info = (pvclock_vcpu_time_info*)CPU_LOCAL_READ(pvclock_buffer);
        uint32_t version;
        uint64_t time0;

        do {
            version = info->version;
            std::atomic_thread_fence(std::memory_order_acquire);

            uint32_t lo, hi;
            asm volatile ("rdtsc" : "=a"(lo), "=d"(hi) : : "memory");
            uint64_t tsc_v = (static_cast<uint64_t>(hi) << 32) | lo;

            uint64_t delta = tsc_v - info->tsc_timestamp;
            
            if (info->tsc_shift >= 0)
                delta <<= info->tsc_shift;
            else
                delta >>= -info->tsc_shift;

            uint64_t scaled_delta = (static_cast<unsigned __int128>(delta) * info->tsc_to_system_mul) >> 32;
            
            time0 = info->system_time + scaled_delta;

            std::atomic_thread_fence(std::memory_order_acquire);
        } while ((info->version & 1) || (info->version != version));

        return time0;
    }


    void pvclock_timer::sleep(std::uint64_t us) {
        std::uint64_t current = this->current_nano();
        std::uint64_t end_ns = us * 1000;
        std::uint64_t target = current + end_ns;
        
        while(this->current_nano() < target) {
            arch::pause();
        }
    }

    int pvclock_timer::get_priority() {
        return 999999999; 
    }
}