summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2013-05-19 15:28:13 +0200
committerRichard Braun <rbraun@sceen.net>2013-05-19 15:31:42 +0200
commit660d612852c124cf03a63a1b4d1f338a43ffaddb (patch)
tree7db40458fefe86b4eb59d85a4ea8c07666e54455
parent9aef0adf731222e73b684e137aa4451417610db9 (diff)
kern/cpumap: new module
-rw-r--r--Makefrag.am2
-rw-r--r--kern/cpumap.c64
-rw-r--r--kern/cpumap.h138
-rw-r--r--kern/error.h1
-rw-r--r--kern/kernel.c2
5 files changed, 207 insertions, 0 deletions
diff --git a/Makefrag.am b/Makefrag.am
index 26134b0a..e4a09e27 100644
--- a/Makefrag.am
+++ b/Makefrag.am
@@ -8,6 +8,8 @@ x15_SOURCES += \
kern/condition.c \
kern/condition.h \
kern/config.h \
+ kern/cpumap.c \
+ kern/cpumap.h \
kern/error.h \
kern/init.h \
kern/kernel.c \
diff --git a/kern/cpumap.c b/kern/cpumap.c
new file mode 100644
index 00000000..a76450b7
--- /dev/null
+++ b/kern/cpumap.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2013 Richard Braun.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <kern/bitmap.h>
+#include <kern/cpumap.h>
+#include <kern/error.h>
+#include <kern/kmem.h>
+#include <kern/stddef.h>
+
+static struct kmem_cache cpumap_cache;
+
+void
+cpumap_setup(void)
+{
+ kmem_cache_init(&cpumap_cache, "cpumap", sizeof(struct cpumap),
+ 0, NULL, NULL, NULL, 0);
+}
+
+int
+cpumap_create(struct cpumap **cpumapp)
+{
+ struct cpumap *cpumap;
+
+ cpumap = kmem_cache_alloc(&cpumap_cache);
+
+ if (cpumap == NULL)
+ return ERROR_NOMEM;
+
+ *cpumapp = cpumap;
+ return 0;
+}
+
+void
+cpumap_destroy(struct cpumap *cpumap)
+{
+ kmem_cache_free(&cpumap_cache, cpumap);
+}
+
+int
+cpumap_check(const struct cpumap *cpumap)
+{
+ int index;
+
+ index = bitmap_find_first(cpumap->cpus, cpu_count());
+
+ if (index == -1)
+ return ERROR_INVAL;
+
+ return 0;
+}
diff --git a/kern/cpumap.h b/kern/cpumap.h
new file mode 100644
index 00000000..710eb08f
--- /dev/null
+++ b/kern/cpumap.h
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2013 Richard Braun.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * Processors represented as bit maps.
+ *
+ * This module acts as a convenient frontend for the bitmap and kmem modules.
+ * Since the size of a CPU map varies with the maximum number of processors
+ * that can be managed by the system, maps must not be allocated from the
+ * stack.
+ */
+
+#ifndef _KERN_CPUMAP_H
+#define _KERN_CPUMAP_H
+
+#include <kern/bitmap.h>
+
+struct cpumap {
+ BITMAP_DECLARE(cpus, MAX_CPUS);
+};
+
+static inline void
+cpumap_zero(struct cpumap *cpumap)
+{
+ bitmap_zero(cpumap->cpus, MAX_CPUS);
+}
+
+static inline void
+cpumap_fill(struct cpumap *cpumap)
+{
+ bitmap_fill(cpumap->cpus, MAX_CPUS);
+}
+
+static inline void
+cpumap_copy(struct cpumap *dest, const struct cpumap *src)
+{
+ bitmap_copy(dest->cpus, src->cpus, MAX_CPUS);
+}
+
+static inline void
+cpumap_set(struct cpumap *cpumap, int index)
+{
+ bitmap_set(cpumap->cpus, index);
+}
+
+static inline void
+cpumap_set_atomic(struct cpumap *cpumap, int index)
+{
+ bitmap_set_atomic(cpumap->cpus, index);
+}
+
+static inline void
+cpumap_clear(struct cpumap *cpumap, int index)
+{
+ bitmap_clear(cpumap->cpus, index);
+}
+
+static inline void
+cpumap_clear_atomic(struct cpumap *cpumap, int index)
+{
+ bitmap_clear_atomic(cpumap->cpus, index);
+}
+
+static inline int
+cpumap_test(const struct cpumap *cpumap, int index)
+{
+ return bitmap_test(cpumap->cpus, index);
+}
+
+static inline int
+cpumap_find_next(const struct cpumap *cpumap, int index)
+{
+ return bitmap_find_next(cpumap->cpus, MAX_CPUS, index);
+}
+
+static inline int
+cpumap_find_first(const struct cpumap *cpumap)
+{
+ return bitmap_find_first(cpumap->cpus, MAX_CPUS);
+}
+
+static inline int
+cpumap_find_next_zero(const struct cpumap *cpumap, int index)
+{
+ return bitmap_find_next_zero(cpumap->cpus, MAX_CPUS, index);
+}
+
+static inline int
+cpumap_find_first_zero(const struct cpumap *cpumap)
+{
+ return bitmap_find_first_zero(cpumap->cpus, MAX_CPUS);
+}
+
+#define cpumap_for_each(cpumap, index) \
+ bitmap_for_each((cpumap)->cpus, MAX_CPUS, index)
+
+#define cpumap_for_each_zero(cpumap, index) \
+ bitmap_for_each_zero((cpumap)->cpus, MAX_CPUS, index)
+
+/*
+ * Initialize the cpumap module.
+ */
+void cpumap_setup(void);
+
+/*
+ * Allocate a CPU map.
+ *
+ * The new map is uninitialized.
+ */
+int cpumap_create(struct cpumap **cpumapp);
+
+/*
+ * Release a CPU map.
+ */
+void cpumap_destroy(struct cpumap *cpumap);
+
+/*
+ * Check the validity of a CPU map.
+ *
+ * If the map doesn't identify at least one managed processor, return
+ * ERROR_INVAL.
+ */
+int cpumap_check(const struct cpumap *cpumap);
+
+#endif /* _KERN_CPUMAP_H */
diff --git a/kern/error.h b/kern/error.h
index 93de8423..6cf18b7e 100644
--- a/kern/error.h
+++ b/kern/error.h
@@ -20,5 +20,6 @@
#define ERROR_NOMEM 1
#define ERROR_AGAIN 2
+#define ERROR_INVAL 3
#endif /* _KERN_ERROR_H */
diff --git a/kern/kernel.c b/kern/kernel.c
index 1acd5308..afeb1719 100644
--- a/kern/kernel.c
+++ b/kern/kernel.c
@@ -15,6 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <kern/cpumap.h>
#include <kern/init.h>
#include <kern/kernel.h>
#include <kern/llsync.h>
@@ -32,6 +33,7 @@ kernel_main(void)
cpu_intr_enable();
/* Initialize the kernel */
+ cpumap_setup();
task_setup();
thread_setup();
llsync_setup();