summaryrefslogtreecommitdiff
path: root/Kernel/Arch/x86/MSR.h
blob: 1acc1dfb41cdd7c92a34bf44ad5db70edaf95a45 (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
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Types.h>

#include <Kernel/Arch/x86/CPUID.h>

#include <AK/Platform.h>
VALIDATE_IS_X86()

namespace Kernel {

class MSR {
    uint32_t m_msr;

public:
    static bool have()
    {
        CPUID id(1);
        return (id.edx() & (1 << 5)) != 0;
    }

    MSR(const MSR&) = delete;
    MSR& operator=(const MSR&) = delete;

    MSR(uint32_t msr)
        : m_msr(msr)
    {
    }

    [[nodiscard]] u64 get()
    {
        u32 low, high;
        asm volatile("rdmsr"
                     : "=a"(low), "=d"(high)
                     : "c"(m_msr));
        return ((u64)high << 32) | low;
    }

    void set(u64 value)
    {
        u32 low = value & 0xffffffff;
        u32 high = value >> 32;
        asm volatile("wrmsr" ::"a"(low), "d"(high), "c"(m_msr));
    }
};

}