From fd6f8e299d5287528d2d2ec2a1883c1c9cc17d3d Mon Sep 17 00:00:00 2001
From: Armin Luntzer <armin.luntzer@univie.ac.at>
Date: Thu, 30 Mar 2017 18:02:23 +0200
Subject: [PATCH] use same convention as in linux to define init calls

---
 include/kernel/init.h   | 44 ++++++++++++++++++++++++++++++++++++-----
 include/kernel/module.h | 16 ++++++++++++---
 2 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/include/kernel/init.h b/include/kernel/init.h
index 1ea1119..0e64d8d 100644
--- a/include/kernel/init.h
+++ b/include/kernel/init.h
@@ -5,12 +5,46 @@
 #ifndef _KERNEL_INIT_H_
 #define _KERNEL_INIT_H_
 
-typedef int (*initcall_t)(void);
+typedef int  (*initcall_t)(void);
+typedef void (*exitcall_t)(void);
+
+
+/**
+ *
+ * We piggy-back on the (linux-style) initcall sections that BCC and
+ * GCC for the MPPB already have specified in their default linker scripts
+ * until we add our own.
+ *
+ * Since _call_initcalls() is executed by the libgloss bootup-code, use
+ * the initcalls in init/main.c to set up our system. Conventions are the same
+ * as in linux.
+ *
+ * They don't have sections for exitcalls, but we don't really need those for
+ * now.
+ */
+
+#define __define_initcall(fn, id)					\
+        static initcall_t __initcall_##fn __attribute((used))		\
+        __attribute__((__section__(".initcall" #id ".init"))) = fn;
+
+#if 0
+#define __exitcall(fn)							\
+        static exitcall_t __exitcall_##fn __attribute((used))		\
+        __attribute__((__section__(".exitcall.exit"))) = fn;
+#else
+
+#define __exitcall(fn)
+
+#endif
+
+#define core_initcall(fn)               __define_initcall(fn, 1)
+#define postcore_initcall(fn)           __define_initcall(fn, 2)
+#define arch_initcall(fn)               __define_initcall(fn, 3)
+#define subsys_initcall(fn)             __define_initcall(fn, 4)
+#define fs_initcall(fn)                 __define_initcall(fn, 5)
+#define device_initcall(fn)             __define_initcall(fn, 6)
+#define late_initcall(fn)               __define_initcall(fn, 7)
 
-/* TODO: initcalls on startup for compiled-in modules */
-#define define_initcall(fn) \
-        static initcall_t _initcall_##fn __attribute__((used)) \
-        __attribute__((__section__(".initcall"))) = fn;
 
 void setup_arch(void);
 
diff --git a/include/kernel/module.h b/include/kernel/module.h
index f651c3d..1ed00cd 100644
--- a/include/kernel/module.h
+++ b/include/kernel/module.h
@@ -5,13 +5,23 @@
 #include <kernel/elf.h>
 
 
+#ifdef MODULE
 
-#define module_init(initfunc)   \
+#define module_init(initfunc)					\
         int _module_init(void) __attribute__((alias(#initfunc)));
 
-#define module_exit(exitfunc)   \
+#define module_exit(exitfunc)					\
         int _module_exit(void) __attribute__((alias(#exitfunc)));
 
+#else /* MODULE */
+
+
+#define module_init(initfunc) device_initcall(initfunc);
+
+#define module_exit(exitfunc) __exitcall(exitfunc);
+
+
+#endif /* MODULE */
 
 
 struct module_section {
@@ -36,7 +46,7 @@ struct elf_module {
 	unsigned int align;
 
 	Elf_Ehdr *ehdr;		/* coincides with start of module image */
-	
+
 	size_t size;
 
 	struct module_section *sec;
-- 
GitLab