Add config and host platform headers in runtime_primitives

Only track whether we're on an 32- or 64-bit x86 host and set frame size
depending on host platform. Use the new config in runtime_library
assembly functions rather than hard-coded constants.

Bug: 232598137, 278926583
Test: mm && berberis_host_tests
Change-Id: I1e484b0f6d648d07887a7b257e9be5a014806251
diff --git a/runtime/runtime_library_x86_64.cc b/runtime/runtime_library_x86_64.cc
index 1479bac..808e257 100644
--- a/runtime/runtime_library_x86_64.cc
+++ b/runtime/runtime_library_x86_64.cc
@@ -18,6 +18,7 @@
 
 #include "berberis/base/checks.h"
 #include "berberis/guest_state/guest_state.h"
+#include "berberis/runtime_primitives/config.h"
 
 // Perform all the steps needed to exit generated code except return, which is
 // up to the users of this macro. The users of this macro may choose to perform
@@ -50,9 +51,7 @@
       ::[InsnAddr] "p"(offsetof(berberis::ThreadState, cpu.insn_addr)), \
       [Residence] "p"(offsetof(berberis::ThreadState, residence)),      \
       [OutsideGeneratedCode] "J"(berberis::kOutsideGeneratedCode),      \
-      [FrameSizeAtTranslatedCode] "J"(8))
-// TODO(b/278926583): create and use
-// berberis::config::kFrameSizeAtTranslatedCode instead of "8"
+      [FrameSizeAtTranslatedCode] "J"(berberis::config::kFrameSizeAtTranslatedCode))
 // clang-format on
 
 namespace berberis {
@@ -172,9 +171,7 @@
       ::[InsnAddr] "p"(offsetof(ThreadState, cpu.insn_addr)),
       [Residence] "p"(offsetof(ThreadState, residence)),
       [InsideGeneratedCode] "J"(kInsideGeneratedCode),
-      [FrameSizeAtTranslatedCode] "J"(8));
-  // TODO(b/278926583): create and use
-  // berberis::config::kFrameSizeAtTranslatedCode instead of "8"
+      [FrameSizeAtTranslatedCode] "J"(config::kFrameSizeAtTranslatedCode));
   // clang-format on
 }
 
diff --git a/runtime_primitives/include/berberis/runtime_primitives/config.h b/runtime_primitives/include/berberis/runtime_primitives/config.h
new file mode 100644
index 0000000..9a5551d
--- /dev/null
+++ b/runtime_primitives/include/berberis/runtime_primitives/config.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_RUNTIME_PRIMITIVES_CONFIG_H_
+#define BERBERIS_RUNTIME_PRIMITIVES_CONFIG_H_
+
+#include <cstdint>
+
+#include "berberis/runtime_primitives/platform.h"
+
+namespace berberis::config {
+
+// Size of the stack frame allocated in translated code prologue.
+// As translated code ('slow') prologue executes much less frequently than
+// region ('fast') prologue, it makes sense to allocate a frame there that
+// suits most regions. Outstanding regions will expand it in their prologue.
+// Assume the stack is properly aligned when entering translated code.
+// TODO(b/232598137): If we discover that most regions don't need stack frame
+// at all, then we might want to avoid extra altering of stack pointer in
+// translated code prologue and keep stack misaligned. Then we'll need a
+// kStackMisalignAtTranslatedCode config variable.
+// TODO(b/232598137): 12 is what we get on x86-32 after stack alignment, update
+// with, say, 90-percentile of (dynamic) frame size.
+static constexpr uint32_t kFrameSizeAtTranslatedCode = host_platform::kIsX86_32 ? 12u : 8u;
+
+}  // namespace berberis::config
+
+#endif  // BERBERIS_RUNTIME_PRIMITIVES_CONFIG_H_
diff --git a/runtime_primitives/include/berberis/runtime_primitives/platform.h b/runtime_primitives/include/berberis/runtime_primitives/platform.h
new file mode 100644
index 0000000..006498a
--- /dev/null
+++ b/runtime_primitives/include/berberis/runtime_primitives/platform.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef BERBERIS_RUNTIME_PRIMITIVES_PLATFORM_H_
+#define BERBERIS_RUNTIME_PRIMITIVES_PLATFORM_H_
+
+namespace berberis::host_platform {
+
+#if defined(__i386__) && !defined(__x32__)
+static const bool kIsX86_32 = true;
+static const bool kIsX86_64 = false;
+#endif
+#if defined(__x86_64__) || defined(__x32__)
+static const bool kIsX86_32 = false;
+static const bool kIsX86_64 = true;
+#endif
+static const bool kIsX86 = (kIsX86_32 || kIsX86_64);
+
+}  // namespace berberis::host_platform
+
+#endif  // BERBERIS_RUNTIME_PRIMITIVES_PLATFORM_H_