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

#include <AK/Types.h>

#include <Kernel/Arch/x86/ASM_wrapper.h>
#include <Kernel/Arch/x86/Processor.h>
#include <Kernel/Sections.h>

namespace Kernel {

#define XCR_XFEATURE_ENABLED_MASK 0

UNMAP_AFTER_INIT u64 read_xcr0()
{
    u32 eax, edx;
    asm volatile("xgetbv"
                 : "=a"(eax), "=d"(edx)
                 : "c"(XCR_XFEATURE_ENABLED_MASK));
    return eax + ((u64)edx << 32);
}

UNMAP_AFTER_INIT void write_xcr0(u64 value)
{
    u32 eax = value;
    u32 edx = value >> 32;
    asm volatile("xsetbv" ::"a"(eax), "d"(edx), "c"(XCR_XFEATURE_ENABLED_MASK));
}

void stac()
{
    if (!Processor::current().has_feature(CPUFeature::SMAP))
        return;
    asm volatile("stac" ::
                     : "cc");
}

void clac()
{
    if (!Processor::current().has_feature(CPUFeature::SMAP))
        return;
    asm volatile("clac" ::
                     : "cc");
}

}