]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
plat/common/x86: Add `gs_base`/`k_gs_base` MSR's read/write methods
authorSergiu Moga <sergiu@unikraft.io>
Thu, 23 Nov 2023 12:41:28 +0000 (14:41 +0200)
committerSergiu Moga <sergiu@unikraft.io>
Fri, 24 Nov 2023 17:16:10 +0000 (19:16 +0200)
Implement basic methods to be able to read/write from/to the
GS_BASE and KERNEL_GS_BASE MSR's, as well as from an offset relative
to the former's value.

Co-authored-by: Marco Schlumpp <marco@unikraft.io>
Signed-off-by: Marco Schlumpp <marco@unikraft.io>
Signed-off-by: Sergiu Moga <sergiu@unikraft.io>
plat/common/include/x86/gsbase.h [new file with mode: 0644]

diff --git a/plat/common/include/x86/gsbase.h b/plat/common/include/x86/gsbase.h
new file mode 100644 (file)
index 0000000..9aaea3a
--- /dev/null
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the BSD-3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ */
+
+#include <x86/cpu.h>
+#include <uk/essentials.h>
+
+/* TODO: Use wrgsbase/rdgsbase instead, as they are faster */
+static inline void wrgsbase(__uptr gsbase)
+{
+       wrmsrl(X86_MSR_GS_BASE, gsbase);
+}
+
+static inline __uptr rdgsbase(void)
+{
+       return rdmsrl(X86_MSR_GS_BASE);
+}
+
+static inline void wrkgsbase(__uptr kgsbase)
+{
+       wrmsrl(X86_MSR_KERNEL_GS_BASE, kgsbase);
+}
+
+static inline __uptr rdkgsbase(void)
+{
+       return rdmsrl(X86_MSR_KERNEL_GS_BASE);
+}
+
+static inline void wrgsbase8(__u8 val, __off offset)
+{
+       __asm__ __volatile__(
+               "movb   %1, %%gs:%0\n"
+               : "=m"(*(__off *)offset)
+               : "r"(val)
+       );
+}
+
+static inline void wrgsbase16(__u16 val, __off offset)
+{
+       __asm__ __volatile__(
+               "movw   %1, %%gs:%0\n"
+               : "=m"(*(__off *)offset)
+               : "r"(val)
+       );
+}
+
+static inline void wrgsbase32(__u32 val, __off offset)
+{
+       __asm__ __volatile__(
+               "movl   %1, %%gs:%0\n"
+               : "=m"(*(__off *)offset)
+               : "r"(val)
+       );
+}
+
+static inline void wrgsbase64(__u64 val, __off offset)
+{
+       __asm__ __volatile__(
+               "movq   %1, %%gs:%0\n"
+               : "=m"(*(__off *)offset)
+               : "r"(val)
+       );
+}
+
+static inline __u8 rdgsbase8(__off offset)
+{
+       __u8 val;
+
+       __asm__ __volatile__(
+               "movb   %%gs:%1, %0\n"
+               : "=r"(val)
+               : "m"(*(__off *)offset)
+       );
+
+       return val;
+}
+
+static inline __u16 rdgsbase16(__off offset)
+{
+       __u16 val;
+
+       __asm__ __volatile__(
+               "movw   %%gs:%1, %0\n"
+               : "=r"(val)
+               : "m"(*(__off *)offset)
+       );
+
+       return val;
+}
+
+static inline __u32 rdgsbase32(__off offset)
+{
+       __u32 val;
+
+       __asm__ __volatile__(
+               "movl   %%gs:%1, %0\n"
+               : "=r"(val)
+               : "m"(*(__off *)offset)
+       );
+
+       return val;
+}
+
+static inline __u64 rdgsbase64(__off offset)
+{
+       __u64 val;
+
+       __asm__ __volatile__(
+               "movq   %%gs:%1, %0\n"
+               : "=r"(val)
+               : "m"(*(__off *)offset)
+       );
+
+       return val;
+}