1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/thread_isolation.h"
7 #if BUILDFLAG(ENABLE_THREAD_ISOLATION)
10 #include <sys/utsname.h>
13 #include "base/allocator/partition_allocator/src/partition_alloc/thread_isolation/alignment.h"
14 #include "base/check.h"
15 #include "base/check_op.h"
16 #include "base/memory/page_size.h"
17 #include "base/metrics/histogram_functions.h"
18 #include "base/no_destructor.h"
19 #include "third_party/abseil-cpp/absl/base/attributes.h"
21 extern int pkey_alloc(unsigned int flags,
22 unsigned int access_rights) ABSL_ATTRIBUTE_WEAK;
26 bool KernelHasPkruFix() {
27 // PKU was broken on Linux kernels before 5.13 (see
28 // https://lore.kernel.org/all/20210623121456.399107624@linutronix.de/).
29 // A fix is also included in the 5.4.182 and 5.10.103 versions ("x86/fpu:
30 // Correct pkru/xstate inconsistency" by Brian Geffon <bgeffon@google.com>).
31 // Thus check the kernel version we are running on, and bail out if does not
33 struct utsname uname_buffer;
34 CHECK_EQ(0, uname(&uname_buffer));
35 int kernel, major, minor;
36 // Conservatively return if the release does not match the format we expect.
37 if (sscanf(uname_buffer.release, "%d.%d.%d", &kernel, &major, &minor) != 3) {
40 return kernel > 5 || (kernel == 5 && major >= 13) || // anything >= 5.13
41 (kernel == 5 && major == 4 && minor >= 182) || // 5.4 >= 5.4.182
42 (kernel == 5 && major == 10 && minor >= 103); // 5.10 >= 5.10.103
45 int PkeyAlloc(int access_rights) {
50 static bool kernel_has_pkru_fix = KernelHasPkruFix();
51 if (!kernel_has_pkru_fix) {
55 return pkey_alloc(0, access_rights);
60 asm volatile(".byte 0x0f,0x01,0xee\n" : "=a"(pkru) : "c"(0), "d"(0));
64 void Wrpkru(uint32_t pkru) {
65 asm volatile(".byte 0x0f,0x01,0xef\n" : : "a"(pkru), "c"(0), "d"(0));
68 static constexpr uint32_t kBitsPerPkey = 2;
70 void PkeyDisableWriteAccess(int pkey) {
71 #ifdef PKEY_DISABLE_WRITE
72 uint32_t pkru = Rdpkru();
73 uint32_t disable_write = static_cast<uint32_t>(PKEY_DISABLE_WRITE)
74 << (static_cast<uint32_t>(pkey) * kBitsPerPkey);
75 Wrpkru(pkru | disable_write);
83 void ThreadIsolationData::InitializeBeforeThreadCreation() {
84 bool page_size_mismatch = PA_THREAD_ISOLATED_ALIGN_SZ < base::GetPageSize();
85 base::UmaHistogramBoolean("V8.CFIPageSizeMismatch", page_size_mismatch);
86 if (page_size_mismatch) {
87 // We write-protect global variables and need to align and pad them to (a
88 // multiple of) the OS page size. But since page size is not a compile time
89 // constant, check at runtime that our value was large enough.
97 allocator->Initialize(pkey);
98 PkeyDisableWriteAccess(pkey);
101 bool ThreadIsolationData::Initialized() const {
105 ThreadIsolationData& GetThreadIsolationData() {
106 static ThreadIsolationData thread_isolation_data;
107 DCHECK_EQ((reinterpret_cast<size_t>(&thread_isolation_data) %
108 PA_THREAD_ISOLATED_ALIGN_SZ),
110 return thread_isolation_data;
115 #endif // BUILDFLAG(ENABLE_THREAD_ISOLATION)