summaryrefslogtreecommitdiff
path: root/kernel/src/arch/x86_64/cpu/lapic.hpp
blob: d2380af7527f96e3ee64c088b7d1bc4ed8fdb89f (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

#include <cstdint>

#pragma once

#include <generic/paging.hpp>
#include <generic/time.hpp>
#include <generic/hhdm.hpp>
#include <utils/gobject.hpp>
#include <arch/x86_64/assembly.hpp>
#include <klibc/stdio.hpp>
namespace x86_64 {

        inline int is_lapic_init = 0;
        inline int is_x2apic = 0;

        class lapic {

                static inline std::uint64_t base() {
                    return (std::uint64_t)((assembly::rdmsr(0x1B) & 0xFFFFF000) + etc::hhdm());
                }

                static inline std::uint32_t read(std::uint32_t reg) {
                    if(is_x2apic) {
                        return assembly::rdmsr(0x800 + (reg >> 4));
                    } else {
                        return *(volatile std::uint32_t*)(base() + reg); 
                    }
                }

                static inline void write(std::uint32_t reg,std::uint32_t value) {
                    if(is_x2apic) {
                        assembly::wrmsr(0x800 + (reg >> 4), value);
                    } else {
                        *(volatile std::uint32_t*)(base() + reg) = value;
                    }
                }

            public:

                static std::uint32_t id();
                static void eoi();
                static void off();
                static void tick(std::uint64_t tick);
                static std::uint64_t init(std::uint32_t us);
            };
};