kernel: add c++ support

This commit is contained in:
2023-03-20 20:41:39 +00:00
parent a4d850cc03
commit 2bfb6bcd78
41 changed files with 333 additions and 38 deletions

View File

@@ -3,6 +3,10 @@
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define BITS_PER_WORD (8 * sizeof(unsigned long))
#define __DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#define BITMAP_WORDS(nbits) __DIV_ROUND_UP(nbits, BITS_PER_WORD)
@@ -23,4 +27,8 @@ extern unsigned int bitmap_highest_clear(unsigned long *map, unsigned long nbits
extern unsigned int bitmap_lowest_set(unsigned long *map, unsigned long nbits);
extern unsigned int bitmap_lowest_clear(unsigned long *map, unsigned long nbits);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -14,7 +14,7 @@
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
@@ -25,6 +25,10 @@
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* if your custom structure contains a btree_node_t (i.e. it can be part of a btree),
you can use this macro to convert a btree_node_t* to a your_type*
@@ -94,8 +98,8 @@
} \
\
btree_insert_fixup(tree, &node->container_node_member); \
}
}
/* defines a node insertion function.
this function should be used for trees with complex node keys that cannot be directly compared.
a comparator for your keys must be supplied.
@@ -114,9 +118,9 @@
Which implements the following:
return -1 if a < b
return 0 if a == b
return 1 if a > b
return -1 if a < b
return 0 if a == b
return 1 if a > b
You would use the following call to generate an insert function for a tree with this node type:
@@ -370,4 +374,8 @@ static inline unsigned short btree_height(btree_node_t *node)
return node->b_height;
}
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -18,6 +18,10 @@
#include <socks/locks.h>
#include <socks/status.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum console_flags {
/* console is only used during the boot process. the console
will be automatically de-registered when the first
@@ -42,4 +46,8 @@ extern kern_status_t console_unregister(console_t *con);
extern void console_write(console_t *con, const char *s, unsigned int len);
extern int console_read(console_t *con, char *s, unsigned int len);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -4,6 +4,10 @@
#include <socks/machine/cpu.h>
#include <socks/sched.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum cpu_flags {
CPU_ONLINE = 0x01u,
} cpu_flags_t;
@@ -30,4 +34,8 @@ extern void cpu_set_online(unsigned int cpu_id);
extern unsigned int cpu_get_highest_available(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -4,6 +4,10 @@
#include <socks/compiler.h>
#include <socks/machine/init.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef int (*initcall_t)(void);
#define INITLEVEL_EARLY 0
@@ -30,4 +34,8 @@ extern void print_kernel_banner(void);
extern int do_initcalls(void);
extern int start_initlevel(int level);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -4,6 +4,10 @@
#include <socks/compiler.h>
#include <socks/machine/hwlock.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef __aligned(8) ml_hwlock_t spin_lock_t;
#define SPIN_LOCK_INIT ML_HWLOCK_INIT
@@ -11,4 +15,8 @@ typedef __aligned(8) ml_hwlock_t spin_lock_t;
#define spin_lock_irqsave(lck, flags) ml_hwlock_lock_irqsave(lck, flags);
#define spin_unlock_irqrestore(lck, flags) ml_hwlock_unlock_irqrestore(lck, flags);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -14,7 +14,7 @@
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
@@ -26,6 +26,10 @@
#include <limits.h>
#include <socks/types.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MEMBLOCK_INIT_MEMORY_REGION_COUNT 128
#define MEMBLOCK_INIT_RESERVED_REGION_COUNT 128
@@ -50,7 +54,7 @@
if you don't want to use an upper bound, pass UINTPTR_MAX.
EXAMPLE: to iterate through all memory regions (with no bounds):
memblock_iter_t it;
for_each_mem_region (&it, 0x0, UINTPTR_MAX) { ... }
@@ -80,7 +84,7 @@
if you don't want to use an upper bound, pass UINTPTR_MAX.
EXAMPLE: to iterate through all reserved memory regions (with no bounds):
memblock_iter_t it;
for_each_reserved_mem_region (&it, 0x0, UINTPTR_MAX) { ... }
@@ -319,4 +323,8 @@ extern void __next_memory_region(memblock_iter_t *it, \
memblock_type_t *type_a, memblock_type_t *type_b,
phys_addr_t start, phys_addr_t end);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -6,6 +6,10 @@
#include <socks/vm.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#define OBJECT_MAGIC 0xBADDCAFE
#define OBJECT_NAME_MAX 64
@@ -19,7 +23,7 @@ typedef enum object_type_flags {
typedef struct object_ops {
kern_status_t(*open)(struct object *obj);
kern_status_t(*close)(struct object *obj);
kern_status_t(*delete)(struct object *obj);
kern_status_t(*destroy)(struct object *obj);
kern_status_t(*query_name)(struct object *obj, char out[OBJECT_NAME_MAX]);
kern_status_t(*parse)(struct object *obj, const char *path, struct object **out);
kern_status_t(*get_named)(struct object *obj, const char *name, struct object **out);
@@ -89,4 +93,8 @@ extern bool object_is_set(object_t *obj);
extern void init_set_objects(void);
extern void init_global_namespace(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -4,6 +4,10 @@
#include <socks/status.h>
#include <socks/compiler.h>
#ifdef __cplusplus
extern "C" {
#endif
#define DEFINE_PERCPU_VAR(type, name) \
__section(".data.percpu") type name
@@ -14,4 +18,8 @@
extern kern_status_t init_per_cpu_areas(void);
extern void *__percpu_get(void *var);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -10,6 +10,10 @@
#define PFN(x) ((x) >> VM_PAGE_SHIFT)
#ifdef __cplusplus
extern "C" {
#endif
typedef ml_pmap_t pmap_t;
typedef ml_pfn_t pfn_t;
@@ -30,4 +34,8 @@ extern kern_status_t pmap_add_block(pmap_t pmap, void *p, pfn_t pfn, size_t len,
extern kern_status_t pmap_remove(pmap_t pmap, void *p);
extern kern_status_t pmap_remove_range(pmap_t pmap, void *p, size_t len);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -3,7 +3,15 @@
#include <socks/console.h>
#ifdef __cplusplus
extern "C" {
#endif
extern void early_printk_init(console_t *con);
extern int printk(const char *format, ...);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -4,6 +4,10 @@
#include <socks/libc/string.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define QUEUE_CONTAINER(t, m, v) ((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0))
#define QUEUE_INIT ((queue_t){ .q_first = NULL, .q_last = NULL })
@@ -51,4 +55,8 @@ extern queue_entry_t *queue_pop_back(queue_t *q);
extern void queue_delete(queue_t *q, queue_entry_t *entry);
extern void queue_delete_all(queue_t *q);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -11,6 +11,10 @@
#define TASK_NAME_MAX 64
#define PRIO_MAX 32
#ifdef __cplusplus
extern "C" {
#endif
typedef enum task_state {
TASK_RUNNING,
TASK_STOPPED,
@@ -73,7 +77,7 @@ extern kern_status_t sched_init(void);
extern void runqueue_init(runqueue_t *rq);
extern task_t *task_alloc(void);
static inline task_t *task_ref(task_t *task) { return object_data(object_ref(object_header(task))); }
static inline task_t *task_ref(task_t *task) { return (task_t *)object_data(object_ref(object_header(task))); }
static inline void task_deref(task_t *task) { object_deref(object_header(task)); }
extern task_t *task_from_pid(unsigned int pid);
extern task_t *kernel_task(void);
@@ -90,4 +94,8 @@ static inline void task_unlock_irqrestore(task_t *task, unsigned long flags)
extern thread_t *thread_alloc(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,6 +1,14 @@
#ifndef SOCKS_TEST_H_
#define SOCKS_TEST_H_
#ifdef __cplusplus
extern "C" {
#endif
extern int run_all_tests(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -20,6 +20,10 @@
device, a serial port, etc.
*/
#ifdef __cplusplus
extern "C" {
#endif
/* opaque context pointer for use by the tty driver */
typedef void *tty_driver_ctx_t;
@@ -84,4 +88,8 @@ extern void tty_destroy(tty_t *tty);
extern int tty_read(tty_t *tty, char *s, unsigned long len);
extern int tty_write(tty_t *tty, const char *s, unsigned long len);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -5,6 +5,10 @@
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
extern void data_size_to_string(size_t value, char *out, size_t outsz);
static inline bool power_of_2(size_t x) { return (x > 0 && (x & (x - 1)) == 0); }
static inline unsigned long long div64_pow2(unsigned long long x, unsigned long long y)
@@ -17,4 +21,8 @@ static inline unsigned long long absdiff64(unsigned long long x, unsigned long l
return x > y ? y - x : x - y;
}
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -8,6 +8,10 @@
#include <socks/locks.h>
#include <socks/machine/vm.h>
#ifdef __cplusplus
extern "C" {
#endif
/* maximum number of NUMA nodes */
#define VM_MAX_NODES 64
/* maximum number of memory zones per node */
@@ -185,8 +189,9 @@ typedef struct vm_slab {
when freeing:
- s_free should be set to the index of the object being freed.
- s_freelist[s_free] should be set to the previous value of s_free.
this is commented as it as flexible arrays are not supported in c++.
*/
unsigned int s_freelist[];
//unsigned int s_freelist[];
} vm_slab_t;
typedef struct vm_page {
@@ -212,9 +217,7 @@ typedef struct vm_page {
/* owner-specific data */
union {
struct {
vm_slab_t *p_slab;
};
vm_slab_t *p_slab;
};
} __attribute__((aligned(2 * sizeof(unsigned long)))) vm_page_t;
@@ -287,4 +290,8 @@ extern void vm_sparse_init(void);
extern vm_page_t *vm_page_get_sparse(phys_addr_t addr);
extern size_t vm_page_get_pfn_sparse(vm_page_t *pg);
#ifdef __cplusplus
}
#endif
#endif