From 0ab69a2bb835d242a4bb07e2294b28f04eb59a32 Mon Sep 17 00:00:00 2001
From: Norman Feske <norman.feske@genode-labs.com>
Date: Tue, 20 Jun 2023 17:53:41 +0200
Subject: [PATCH] base: de-duplicate src/lib/base/platform.cc

By splitting the 'init_capability_slab()' implementation to a separate
compilation unit 'capability_slab.cc', base-hw no longer needs a
customized version of 'lib/base/platform.cc'.

Related to issue #4784
---
 repos/base-fiasco/lib/mk/base-fiasco.mk       |   1 +
 repos/base-foc/lib/mk/base-foc.inc            |   1 +
 repos/base-hw/lib/mk/base-hw.inc              |   1 +
 repos/base-hw/src/lib/base/capability_slab.cc |  64 +++++++++
 repos/base-hw/src/lib/base/platform.cc        | 124 ------------------
 repos/base-linux/lib/mk/base-linux.inc        |   1 +
 repos/base-nova/lib/mk/base-nova.mk           |   1 +
 repos/base-okl4/lib/mk/base-okl4.mk           |   1 +
 repos/base-pistachio/lib/mk/base-pistachio.mk |   1 +
 repos/base-sel4/lib/mk/base-sel4.inc          |   2 +-
 .../base/src/include/base/internal/globals.h  |   1 +
 repos/base/src/lib/base/capability_slab.cc    |  16 +++
 repos/base/src/lib/base/platform.cc           |   1 +
 13 files changed, 90 insertions(+), 125 deletions(-)
 create mode 100644 repos/base-hw/src/lib/base/capability_slab.cc
 delete mode 100644 repos/base-hw/src/lib/base/platform.cc
 create mode 100644 repos/base/src/lib/base/capability_slab.cc

diff --git a/repos/base-fiasco/lib/mk/base-fiasco.mk b/repos/base-fiasco/lib/mk/base-fiasco.mk
index a98ceba70c..c6bbc6692d 100644
--- a/repos/base-fiasco/lib/mk/base-fiasco.mk
+++ b/repos/base-fiasco/lib/mk/base-fiasco.mk
@@ -4,5 +4,6 @@ LIBS += syscall-fiasco base-fiasco-common cxx timeout
 
 SRC_CC += thread_start.cc
 SRC_CC += cache.cc
+SRC_CC += capability_slab.cc
 SRC_CC += capability_space.cc
 SRC_CC += signal_transmitter.cc signal.cc
diff --git a/repos/base-foc/lib/mk/base-foc.inc b/repos/base-foc/lib/mk/base-foc.inc
index 9985e4e324..8bfafb3d32 100644
--- a/repos/base-foc/lib/mk/base-foc.inc
+++ b/repos/base-foc/lib/mk/base-foc.inc
@@ -4,6 +4,7 @@ LIBS   += base-foc-common syscall-foc cxx
 
 SRC_CC += cap_map_remove.cc cap_alloc.cc
 SRC_CC += cache.cc
+SRC_CC += capability_slab.cc
 SRC_CC += thread_start.cc
 SRC_CC += signal_transmitter.cc signal.cc
 SRC_CC += stack_area_addr.cc
diff --git a/repos/base-hw/lib/mk/base-hw.inc b/repos/base-hw/lib/mk/base-hw.inc
index cb71081d82..2ca7e48f26 100644
--- a/repos/base-hw/lib/mk/base-hw.inc
+++ b/repos/base-hw/lib/mk/base-hw.inc
@@ -3,6 +3,7 @@ include $(BASE_DIR)/lib/mk/base.inc
 SRC_CC += log.cc
 SRC_CC += thread_start.cc
 SRC_CC += capability.cc
+SRC_CC += capability_slab.cc
 SRC_CC += cache.cc
 SRC_CC += raw_write_string.cc
 SRC_CC += signal_receiver.cc
diff --git a/repos/base-hw/src/lib/base/capability_slab.cc b/repos/base-hw/src/lib/base/capability_slab.cc
new file mode 100644
index 0000000000..f28252983d
--- /dev/null
+++ b/repos/base-hw/src/lib/base/capability_slab.cc
@@ -0,0 +1,64 @@
+/*
+ * \brief  Capability slab management
+ * \author Norman Feske
+ * \date   2023-06-20
+ */
+
+/*
+ * Copyright (C) 2023 Genode Labs GmbH
+ *
+ * This file is part of the Genode OS framework, which is distributed
+ * under the terms of the GNU Affero General Public License version 3.
+ */
+
+#include <util/retry.h>
+#include <base/internal/globals.h>
+#include <base/internal/native_env.h>
+#include <hw_native_pd/client.h>
+
+using namespace Genode;
+
+
+static Parent                *parent_ptr;
+static Pd_session::Native_pd *native_pd_ptr;
+
+
+void Genode::init_cap_slab(Pd_session &pd, Parent &parent)
+{
+	static Hw_native_pd_client native_pd(pd.native_pd());
+
+	parent_ptr    = &parent;
+	native_pd_ptr = &native_pd;
+}
+
+
+void Genode::upgrade_capability_slab()
+{
+	if (!native_pd_ptr || !parent_ptr) {
+		error("missing call of 'init_cap_slab'");
+		return;
+	}
+
+	auto request_resources_from_parent = [&] (Ram_quota ram, Cap_quota caps)
+	{
+		/*
+		 * The call of 'resource_request' is handled synchronously by
+		 * 'Expanding_parent_client'.
+		 */
+		String<100> const args("ram_quota=", ram, ", cap_quota=", caps);
+		parent_ptr->resource_request(args.string());
+	};
+
+	retry<Genode::Out_of_caps>(
+		[&] () {
+			retry<Genode::Out_of_ram>(
+				[&] () {
+					native_pd_ptr->upgrade_cap_slab(); },
+				[&] () {
+					request_resources_from_parent(Ram_quota{8192}, Cap_quota{0});
+				});
+		},
+		[&] () {
+			request_resources_from_parent(Ram_quota{0}, Cap_quota{2});
+		});
+}
diff --git a/repos/base-hw/src/lib/base/platform.cc b/repos/base-hw/src/lib/base/platform.cc
deleted file mode 100644
index 06c36f622f..0000000000
--- a/repos/base-hw/src/lib/base/platform.cc
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * \brief  Environment initialization
- * \author Norman Feske
- * \author Christian Helmuth
- * \date   2006-07-27
- */
-
-/*
- * Copyright (C) 2006-2017 Genode Labs GmbH
- *
- * This file is part of the Genode OS framework, which is distributed
- * under the terms of the GNU Affero General Public License version 3.
- */
-
-#include <deprecated/env.h>
-#include <base/internal/platform.h>
-#include <base/internal/globals.h>
-#include <base/internal/native_env.h>
-#include <base/connection.h>
-#include <base/service.h>
-
-#include <hw_native_pd/client.h>
-
-using namespace Genode;
-
-static Platform *_platform_ptr;
-
-
-Env_deprecated *Genode::env_deprecated()
-{
-	if (!_platform_ptr) {
-		error("missing call of init_platform");
-		for (;;);
-	}
-
-	struct Impl : Env_deprecated, Noncopyable
-	{
-		Platform &_pf;
-
-		Impl(Platform &pf) : _pf(pf) { }
-
-		Parent                *parent()          override { return &_pf.parent; }
-		Cpu_session           *cpu_session()     override { return &_pf.cpu; }
-		Cpu_session_capability cpu_session_cap() override { return  _pf.cpu.rpc_cap(); }
-		Region_map            *rm_session()      override { return &_pf.rm; }
-		Pd_session            *pd_session()      override { return &_pf.pd; }
-		Pd_session_capability  pd_session_cap()  override { return  _pf.pd.rpc_cap(); }
-	};
-
-	static Impl impl { *_platform_ptr };
-
-	return &impl;
-}
-
-
-using Native_pd_capability = Genode::Capability<Genode::Pd_session::Native_pd>;
-static Native_pd_capability native_pd_cap;
-
-
-void Genode::init_parent_resource_requests(Genode::Env &env)
-{
-	/**
-	 * Catch up asynchronous resource request and notification
-	 * mechanism construction of the expanding parent environment
-	 */
-	using Parent = Expanding_parent_client;
-	static_cast<Parent*>(&env.parent())->init_fallback_signal_handling();
-	native_pd_cap = env.pd().native_pd();
-}
-
-
-void Genode::init_platform()
-{
-	static Genode::Platform platform;
-
-	init_log(platform.parent);
-	init_rpc_cap_alloc(platform.parent);
-	init_thread(platform.cpu, platform.rm);
-	init_thread_start(platform.pd.rpc_cap());
-	init_thread_bootstrap(platform.parent.main_thread_cap());
-
-	env_stack_area_ram_allocator = &platform.pd;
-	env_stack_area_region_map    = &platform.stack_area;
-
-	_platform_ptr = &platform;
-}
-
-
-void Genode::binary_ready_hook_for_platform() { }
-
-
-void Genode::upgrade_capability_slab()
-{
-	if (!native_pd_cap.valid() || !_platform_ptr) {
-		Genode::error("cannot upgrade capability slab, "
-		              "not initialized appropriatedly");
-		return;
-	}
-
-	auto request_resources_from_parent = [&] (Ram_quota ram, Cap_quota caps)
-	{
-		/*
-		 * The call of 'resource_request' is handled synchronously by
-		 * 'Expanding_parent_client'.
-		 */
-		String<100> const args("ram_quota=", ram, ", cap_quota=", caps);
-		_platform_ptr->parent.resource_request(args.string());
-	};
-
-	retry<Genode::Out_of_caps>(
-		[&] () {
-			retry<Genode::Out_of_ram>(
-				[&] () {
-					Genode::Hw_native_pd_client pd(native_pd_cap);
-					pd.upgrade_cap_slab();
-				},
-				[&] () {
-					request_resources_from_parent(Ram_quota{8192}, Cap_quota{0});
-				});
-		},
-		[&] () {
-			request_resources_from_parent(Ram_quota{0}, Cap_quota{2});
-		});
-}
diff --git a/repos/base-linux/lib/mk/base-linux.inc b/repos/base-linux/lib/mk/base-linux.inc
index acecf0a16d..1428b7f343 100644
--- a/repos/base-linux/lib/mk/base-linux.inc
+++ b/repos/base-linux/lib/mk/base-linux.inc
@@ -6,5 +6,6 @@
 include $(BASE_DIR)/lib/mk/base.inc
 
 SRC_CC += platform.cc
+SRC_CC += capability_slab.cc
 
 LIBS += syscall-linux
diff --git a/repos/base-nova/lib/mk/base-nova.mk b/repos/base-nova/lib/mk/base-nova.mk
index 3fb92e156f..e3a08dfb07 100644
--- a/repos/base-nova/lib/mk/base-nova.mk
+++ b/repos/base-nova/lib/mk/base-nova.mk
@@ -4,6 +4,7 @@ LIBS   += base-nova-common cxx timeout
 SRC_CC += thread_start.cc
 SRC_CC += cache.cc
 SRC_CC += signal.cc
+SRC_CC += capability_slab.cc
 
 #
 # Prevent the compiler from deleting null pointer checks related to 'this == 0'
diff --git a/repos/base-okl4/lib/mk/base-okl4.mk b/repos/base-okl4/lib/mk/base-okl4.mk
index db7b34ae90..95ac1ce36f 100644
--- a/repos/base-okl4/lib/mk/base-okl4.mk
+++ b/repos/base-okl4/lib/mk/base-okl4.mk
@@ -3,6 +3,7 @@ include $(BASE_DIR)/lib/mk/base.inc
 LIBS   += base-okl4-common syscall-okl4 cxx timeout
 SRC_CC += thread_start.cc
 SRC_CC += cache.cc
+SRC_CC += capability_slab.cc
 SRC_CC += capability_space.cc
 SRC_CC += signal_transmitter.cc
 SRC_CC += signal.cc
diff --git a/repos/base-pistachio/lib/mk/base-pistachio.mk b/repos/base-pistachio/lib/mk/base-pistachio.mk
index 90c7de5c7e..a4a558c3dd 100644
--- a/repos/base-pistachio/lib/mk/base-pistachio.mk
+++ b/repos/base-pistachio/lib/mk/base-pistachio.mk
@@ -4,6 +4,7 @@ LIBS   += base-pistachio-common syscall-pistachio cxx timeout
 
 SRC_CC += thread_start.cc
 SRC_CC += cache.cc
+SRC_CC += capability_slab.cc
 SRC_CC += capability_space.cc
 SRC_CC += signal_transmitter.cc
 SRC_CC += signal.cc
diff --git a/repos/base-sel4/lib/mk/base-sel4.inc b/repos/base-sel4/lib/mk/base-sel4.inc
index b59644222b..5d7dff8b13 100644
--- a/repos/base-sel4/lib/mk/base-sel4.inc
+++ b/repos/base-sel4/lib/mk/base-sel4.inc
@@ -1,6 +1,6 @@
 include $(BASE_DIR)/lib/mk/base.inc
 
-SRC_CC += capability_space.cc
+SRC_CC += capability_space.cc capability_slab.cc
 SRC_CC += thread_start.cc thread_init.cc
 SRC_CC += cache.cc
 SRC_CC += signal_transmitter.cc signal.cc
diff --git a/repos/base/src/include/base/internal/globals.h b/repos/base/src/include/base/internal/globals.h
index c28ac27291..045f486f6d 100644
--- a/repos/base/src/include/base/internal/globals.h
+++ b/repos/base/src/include/base/internal/globals.h
@@ -31,6 +31,7 @@ namespace Genode {
 	void init_exception_handling(Env &);
 	void init_signal_transmitter(Env &);
 	void init_signal_receiver(Pd_session &, Parent &);
+	void init_cap_slab(Pd_session &, Parent &);
 	void init_cxx_heap(Env &);
 	void init_cxx_guard();
 	void init_ldso_phdr(Env &);
diff --git a/repos/base/src/lib/base/capability_slab.cc b/repos/base/src/lib/base/capability_slab.cc
new file mode 100644
index 0000000000..13db351958
--- /dev/null
+++ b/repos/base/src/lib/base/capability_slab.cc
@@ -0,0 +1,16 @@
+/*
+ * \brief  Capability slab init for kernels without an expandable cap space
+ * \author Norman Feske
+ * \date   2023-06-20
+ */
+
+/*
+ * Copyright (C) 2023 Genode Labs GmbH
+ *
+ * This file is part of the Genode OS framework, which is distributed
+ * under the terms of the GNU Affero General Public License version 3.
+ */
+
+#include <base/internal/globals.h>
+
+void Genode::init_cap_slab(Pd_session &, Parent &) { }
diff --git a/repos/base/src/lib/base/platform.cc b/repos/base/src/lib/base/platform.cc
index 2ced35d45b..5057da26ff 100644
--- a/repos/base/src/lib/base/platform.cc
+++ b/repos/base/src/lib/base/platform.cc
@@ -67,6 +67,7 @@ void Genode::init_platform()
 
 	init_log(platform.parent);
 	init_rpc_cap_alloc(platform.parent);
+	init_cap_slab(platform.pd, platform.parent);
 	init_thread(platform.cpu, platform.rm);
 	init_thread_start(platform.pd.rpc_cap());
 	init_thread_bootstrap(platform.parent.main_thread_cap());