summaryrefslogtreecommitdiffstats
path: root/arch/x86/msr/Msr.cpp
blob: 19dd1d60d055edfed890278364dd332f92e62e27 (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * Copyright (c) 2026 Alexey Gavrilov <alexey.gavrilov@mail.com>
 *
 * This file is part of ObjectiveOS.
 *
 * ObjectiveOS is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * ObjectiveOS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ObjectiveOS. If not, see <https://www.gnu.org/licenses/>. 
 */

#include "Msr.h"
#include "../cpuid/Cpuid.h"

Msr::Msr()
{
    cpuid::VersionInfo vinfo;
    cpuid::Executor executor;

    executor.cpuid(vinfo);

    msrSupport = vinfo.edx & cpuid::VersionInfo::EdxFlags::MSR;
};

bool Msr::rdmsr(MsrCommand *cmd)
{
    if(!msrSupport or cmd == nullptr) return true;

    uint64 rdmsrResult = 0;
    uint32 regs[2];
    asm(
        "mov %2, %%ecx\n\t"
        "rdmsr\n\t"
        "mov %%eax, %0\n\t"
        "mov %%edx, %1\n\t"
        : "=&r"(regs[0]), "=&r"(regs[1])
        : "r"(cmd->getCommand()));

    MemoryOperations::copy(regs, &rdmsrResult, sizeof(uint64));
    cmd->decode(rdmsrResult);

    return false;
};

bool Msr::wrmsr(MsrCommand *cmd)
{
    if(!msrSupport or cmd == nullptr) return true;

    uint32 regs[2];
    uint64 msr = cmd->getMsr();

    MemoryOperations::copy(&msr, regs, sizeof(uint64));

    asm(
        "mov %0, %%eax\n\t"
        "mov %1, %%edx\n\t"
        "mov %2, %%ecx\n\t"
        "wrmsr\n\t"
        : "=r"(regs[0]), "=r"(regs[1])
        : "r"(cmd->getCommand()));

    return false;
};

Ia32ApicBase::Ia32ApicBase()
{
    cpuid::Executor executor;
    cpuid::VersionInfo verInfo;
    cpuid::ExtendedAddressSize addrSize;
    executor.cpuid(verInfo);
    executor.cpuid(addrSize);

    if(verInfo.isValid
        and verInfo.edx & cpuid::VersionInfo::EdxFlags::PAE
        and !addrSize.isValid)
    {
        maxPhyAddr = 36;
    }
    else if(!addrSize.isValid)
    {
        maxPhyAddr = 32;
    }
    else if(addrSize.isValid)
    {
        maxPhyAddr = addrSize.eax.physicalAddressBits;
    }
};

uint32 Ia32ApicBase::getCommand()
{
    return 0x1b;
};

void Ia32ApicBase::decode(uint64 rdmsrResult)
{
    bspFlag = rdmsrResult & BSP_FLAG;
    enableFlag = rdmsrResult & APIC_ENABLE_FLAG;
    setBaseField(rdmsrResult);
};

uint64 Ia32ApicBase::getMsr()
{
    uint64 msr = 0;

    MemoryOperations::copy(&baseField, &msr, sizeof(uint64));
    msr |= bspFlag << 8;
    msr |= enableFlag << 11;

    return msr;
};

void Ia32ApicBase::setBaseField(uint64 fieldAddr)
{
    uint64 mask = BASE_FIELD_MASK_LOW | 
                    ((UINT64_HIGH<< (maxPhyAddr - 32)) ^ UINT64_HIGH);

    baseField = fieldAddr & mask;
};

void Ia32ApicBase::setBspFlag(bool flag)
{
    bspFlag = flag;
};

void Ia32ApicBase::setEnableFlag(bool flag)
{
    enableFlag = flag;
};