From 2bfb6bcd7837339a3ef8dbb5dd4323d93395ff90 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Mon, 20 Mar 2023 20:41:39 +0000 Subject: [PATCH] kernel: add c++ support --- Makefile | 11 +++++++++-- arch/user/include/arch/stdcon.h | 8 ++++++++ arch/user/include/socks/machine/cpu.h | 8 ++++++++ arch/user/include/socks/machine/hwlock.h | 8 ++++++++ arch/user/include/socks/machine/init.h | 8 ++++++++ arch/user/include/socks/machine/vm.h | 8 ++++++++ arch/x86_64/acpi/{apic.c => apic.cpp} | 11 +++++++---- arch/x86_64/include/arch/acpi.h | 12 ++++++++++-- arch/x86_64/include/arch/e820.h | 8 ++++++++ arch/x86_64/include/arch/gdt.h | 8 ++++++++ arch/x86_64/include/arch/irq.h | 8 ++++++++ arch/x86_64/include/arch/msr.h | 8 ++++++++ arch/x86_64/include/arch/paging.h | 8 ++++++++ arch/x86_64/include/arch/pit.h | 8 ++++++++ arch/x86_64/include/arch/ports.h | 8 ++++++++ arch/x86_64/include/arch/serial.h | 8 ++++++++ arch/x86_64/include/arch/vgacon.h | 8 ++++++++ arch/x86_64/include/socks/machine/cpu.h | 8 ++++++++ arch/x86_64/include/socks/machine/hwlock.h | 8 ++++++++ arch/x86_64/include/socks/machine/init.h | 8 ++++++++ include/socks/bitmap.h | 8 ++++++++ include/socks/btree.h | 20 ++++++++++++++------ include/socks/console.h | 8 ++++++++ include/socks/cpu.h | 8 ++++++++ include/socks/init.h | 8 ++++++++ include/socks/locks.h | 8 ++++++++ include/socks/memblock.h | 14 +++++++++++--- include/socks/object.h | 10 +++++++++- include/socks/percpu.h | 8 ++++++++ include/socks/pmap.h | 8 ++++++++ include/socks/printk.h | 8 ++++++++ include/socks/queue.h | 8 ++++++++ include/socks/sched.h | 10 +++++++++- include/socks/test.h | 8 ++++++++ include/socks/tty.h | 8 ++++++++ include/socks/util.h | 8 ++++++++ include/socks/vm.h | 15 +++++++++++---- obj/object.c | 18 +++++++++--------- tools/make/gcc-host.mk | 2 ++ tools/make/generate_compile_commands.py | 4 ++-- vm/cache.c | 12 ++++++++---- 41 files changed, 333 insertions(+), 38 deletions(-) rename arch/x86_64/acpi/{apic.c => apic.cpp} (92%) diff --git a/Makefile b/Makefile index 34d70b2..03ce1a6 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,8 @@ include arch/$(SOCKS_ARCH)/config.mk KERNEL_SRC_DIRS := init kernel vm ds util obj sched test KERNEL_C_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.c)) -KERNEL_OBJ := $(addprefix $(BUILD_DIR)/,$(KERNEL_C_FILES:.c=.o)) +KERNEL_CXX_FILES := $(foreach dir,$(KERNEL_SRC_DIRS),$(wildcard $(dir)/*.cpp)) +KERNEL_OBJ := $(addprefix $(BUILD_DIR)/,$(KERNEL_C_FILES:.c=.o) $(KERNEL_CXX_FILES:.cpp=.o)) #################################### # Kernel libc source files @@ -34,9 +35,10 @@ LIBC_OBJ := $(addprefix $(BUILD_DIR)/,$(LIBC_C_FILES:.c=.o)) BUILD_ID := $(shell tools/generate_build_id.py --arch $(SOCKS_ARCH)) -CFLAGS := $(CFLAGS) -DBUILD_ID=\"$(BUILD_ID)\" -g -Wall -Werror -pedantic \ +CFLAGS := $(CFLAGS) -DBUILD_ID=\"$(BUILD_ID)\" -g -Wall -Werror -pedantic -fPIC \ -Iinclude -Iarch/$(SOCKS_ARCH)/include -Ilibc/include -Wno-language-extension-token +CXXFLAGS := $(CFLAGS) $(CXXFLAGS) ASMFLAGS := $(ASMFLAGS) -DBUILD_ID=\"$(BUILD_ID)\" LDFLAGS := $(LDFLAGS) -g -lgcc @@ -64,6 +66,11 @@ $(BUILD_DIR)/%.o: %.c @mkdir -p $(@D) @$(CC) $< -o $@ -c $(CFLAGS) $(ARCH_CFLAGS) -MMD +$(BUILD_DIR)/%.o: %.cpp + @printf " \033[1;32mCXX\033[0m \033[35m$(KERNEL_EXEC)\033[0m/$<\n" + @mkdir -p $(@D) + @$(CXX) $< -o $@ -c $(CXXFLAGS) $(ARCH_CXXFLAGS) -MMD + clean: @printf " \033[1;93mRM\033[0m Deleting build files.\n" @rm -rf $(BUILD_DIR) diff --git a/arch/user/include/arch/stdcon.h b/arch/user/include/arch/stdcon.h index b35d005..c027acd 100644 --- a/arch/user/include/arch/stdcon.h +++ b/arch/user/include/arch/stdcon.h @@ -1,6 +1,14 @@ #ifndef ARCH_STDCON_H_ #define ARCH_STDCON_H_ +#ifdef __cplusplus +extern "C" { +#endif + extern void stdcon_init(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/user/include/socks/machine/cpu.h b/arch/user/include/socks/machine/cpu.h index 36f90dc..3614ef6 100644 --- a/arch/user/include/socks/machine/cpu.h +++ b/arch/user/include/socks/machine/cpu.h @@ -1,6 +1,10 @@ #ifndef SOCKS_USER_CPU_H_ #define SOCKS_USER_CPU_H_ +#ifdef __cplusplus +extern "C" { +#endif + typedef struct ml_cpu_block { int cpu_reserved; } ml_cpu_block; @@ -12,4 +16,8 @@ extern int ml_cpu_block_use(ml_cpu_block *p); extern void ml_halt_cpu(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/user/include/socks/machine/hwlock.h b/arch/user/include/socks/machine/hwlock.h index 6ba328b..da285a7 100644 --- a/arch/user/include/socks/machine/hwlock.h +++ b/arch/user/include/socks/machine/hwlock.h @@ -3,6 +3,10 @@ #define ML_HWLOCK_INIT (0) +#ifdef __cplusplus +extern "C" { +#endif + typedef int ml_hwlock_t; extern void ml_hwlock_lock(ml_hwlock_t *lck); @@ -11,4 +15,8 @@ extern void ml_hwlock_unlock(ml_hwlock_t *lck); extern void ml_hwlock_lock_irqsave(ml_hwlock_t *lck, unsigned long *flags); extern void ml_hwlock_unlock_irqrestore(ml_hwlock_t *lck, unsigned long flags); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/user/include/socks/machine/init.h b/arch/user/include/socks/machine/init.h index f5c94f3..ea02a1d 100644 --- a/arch/user/include/socks/machine/init.h +++ b/arch/user/include/socks/machine/init.h @@ -3,6 +3,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + #define __X2(x) #x #define __X(x) __X2(x) @@ -18,4 +22,8 @@ extern int ml_init(uintptr_t arg); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/user/include/socks/machine/vm.h b/arch/user/include/socks/machine/vm.h index d841b4c..d0ce470 100644 --- a/arch/user/include/socks/machine/vm.h +++ b/arch/user/include/socks/machine/vm.h @@ -3,6 +3,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + extern uintptr_t __pagemap_base(void); extern uintptr_t __pagemap_limit(void); @@ -23,4 +27,8 @@ extern uintptr_t __pagemap_limit(void); #define VM_PAGE_MIN_ORDER VM_PAGE_4K #define VM_PAGE_MAX_ORDER VM_PAGE_8M +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/acpi/apic.c b/arch/x86_64/acpi/apic.cpp similarity index 92% rename from arch/x86_64/acpi/apic.c rename to arch/x86_64/acpi/apic.cpp index b8627af..d0523b7 100644 --- a/arch/x86_64/acpi/apic.c +++ b/arch/x86_64/acpi/apic.cpp @@ -15,7 +15,10 @@ static uint32_t *lapic_base; -extern int check_apic(void); +extern "C" { + /* defined in apic_ctrl.S */ + extern int check_apic(void); +} static uintptr_t apic_get_base(void) { @@ -78,7 +81,7 @@ kern_status_t local_apic_enable(void) } apic_set_base(apic_get_base()); - lapic_base = find_lapic(madt); + lapic_base = (uint32_t *)find_lapic(madt); local_apic_write(0xF0, local_apic_read(0xF0) | 0x100); @@ -104,11 +107,11 @@ static void init_all_ioapic(void) kern_status_t apic_init(void) { - static int bsp_id = -1; + static unsigned int bsp_id = (unsigned int)-1; /* the bootstrap processor will be the first one ro call apic_init(). it is responsible for initialising the I/O APICs */ - if (bsp_id == -1) { + if (bsp_id == (unsigned int)-1) { bsp_id = this_cpu(); } diff --git a/arch/x86_64/include/arch/acpi.h b/arch/x86_64/include/arch/acpi.h index 6535e49..4612c02 100644 --- a/arch/x86_64/include/arch/acpi.h +++ b/arch/x86_64/include/arch/acpi.h @@ -5,6 +5,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define ACPI_MADT_LAPIC 0x00 #define ACPI_MADT_IOAPIC 0x01 #define ACPI_MADT_LAPIC_OVERRIDE 0x05 @@ -56,12 +60,12 @@ struct acpi_sdt { struct acpi_rsdt { struct acpi_sdt r_header; - uint32_t r_tables[]; + uint32_t r_tables[1]; } __packed; struct acpi_xsdt { struct acpi_sdt x_header; - uint64_t x_tables[]; + uint64_t x_tables[1]; } __packed; struct acpi_madt { @@ -95,4 +99,8 @@ extern kern_status_t smp_init(void); extern struct acpi_sdt *acpi_find_sdt(uint32_t sig); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/e820.h b/arch/x86_64/include/arch/e820.h index bb8a239..c9eb15c 100644 --- a/arch/x86_64/include/arch/e820.h +++ b/arch/x86_64/include/arch/e820.h @@ -4,6 +4,14 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + extern void e820_scan(multiboot_memory_map_t *mmap, size_t len); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/gdt.h b/arch/x86_64/include/arch/gdt.h index 0429a61..21f9add 100644 --- a/arch/x86_64/include/arch/gdt.h +++ b/arch/x86_64/include/arch/gdt.h @@ -4,6 +4,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define NR_GDT_ENTRIES 5 #define GDT_A_PRESENT (1 << 7) @@ -39,4 +43,8 @@ struct gdt_ptr { extern int gdt_init(struct gdt *gdt, struct gdt_ptr *gdtp); extern int gdt_load(struct gdt_ptr *gdtp); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/irq.h b/arch/x86_64/include/arch/irq.h index 65bc3d2..056ccde 100644 --- a/arch/x86_64/include/arch/irq.h +++ b/arch/x86_64/include/arch/irq.h @@ -5,6 +5,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define NR_IDT_ENTRIES 48 typedef enum irq_vector { @@ -68,4 +72,8 @@ extern int idt_load(struct idt_ptr *idtp); extern void hook_irq(irq_vector_t vec, irq_hook_t *hook); extern void unhook_irq(irq_vector_t vec, irq_hook_t *hook); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/msr.h b/arch/x86_64/include/arch/msr.h index fa5fd3a..89ec3fb 100644 --- a/arch/x86_64/include/arch/msr.h +++ b/arch/x86_64/include/arch/msr.h @@ -5,8 +5,16 @@ #define MSR_GS_BASE 0xC0000101 +#ifdef __cplusplus +extern "C" { +#endif + /* defined in cpu_ctrl.S */ extern uint64_t rdmsr(uint32_t id); extern void wrmsr(uint32_t id, uint64_t val); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/paging.h b/arch/x86_64/include/arch/paging.h index d3fd4e0..1ce6ca4 100644 --- a/arch/x86_64/include/arch/paging.h +++ b/arch/x86_64/include/arch/paging.h @@ -4,6 +4,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define PTE_PRESENT 0x01ULL #define PTE_RW 0x02ULL #define PTE_USR 0x04ULL @@ -52,4 +56,8 @@ typedef enum page_size { extern int gigabyte_pages(void); extern int enable_nx(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/pit.h b/arch/x86_64/include/arch/pit.h index f5559a6..511e0e5 100644 --- a/arch/x86_64/include/arch/pit.h +++ b/arch/x86_64/include/arch/pit.h @@ -1,8 +1,16 @@ #ifndef ARCH_PIT_H_ #define ARCH_PIT_H_ +#ifdef __cplusplus +extern "C" { +#endif + extern void pit_start(unsigned int hz); extern void pit_stop(void); extern void pit_wait(unsigned int ticks); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/ports.h b/arch/x86_64/include/arch/ports.h index 13854bb..6473a28 100644 --- a/arch/x86_64/include/arch/ports.h +++ b/arch/x86_64/include/arch/ports.h @@ -3,6 +3,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + extern uint8_t inportb(uint16_t port); extern uint16_t inportw(uint16_t port); extern uint32_t inportl(uint16_t port); @@ -12,4 +16,8 @@ extern void outportl(uint16_t port, uint32_t data); extern void outportsw(uint16_t port, void *data, uint32_t size); extern void inportsw(uint16_t port, unsigned char *data, unsigned long size); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/serial.h b/arch/x86_64/include/arch/serial.h index f19f226..ac58933 100644 --- a/arch/x86_64/include/arch/serial.h +++ b/arch/x86_64/include/arch/serial.h @@ -1,6 +1,10 @@ #ifndef ARCH_SERIAL_H_ #define ARCH_SERIAL_H_ +#ifdef __cplusplus +extern "C" { +#endif + #define SERIAL_PORT_A 0x3F8 #define SERIAL_PORT_B 0x2F8 #define SERIAL_PORT_C 0x3E8 @@ -14,4 +18,8 @@ extern char serial_recv_byte(int device); extern int serial_rcvd(int device); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/arch/vgacon.h b/arch/x86_64/include/arch/vgacon.h index 90ba5a4..dd89eff 100644 --- a/arch/x86_64/include/arch/vgacon.h +++ b/arch/x86_64/include/arch/vgacon.h @@ -1,6 +1,14 @@ #ifndef ARCH_VGACON_H_ #define ARCH_VGACON_H_ +#ifdef __cplusplus +extern "C" { +#endif + extern void vgacon_init(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/socks/machine/cpu.h b/arch/x86_64/include/socks/machine/cpu.h index b082f8b..a6ca452 100644 --- a/arch/x86_64/include/socks/machine/cpu.h +++ b/arch/x86_64/include/socks/machine/cpu.h @@ -4,6 +4,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define ml_cpu_block_get_id(p) ((p)->c_cpu_id) typedef struct ml_cpu_block { @@ -28,4 +32,8 @@ extern int ml_cpu_block_use(ml_cpu_block *p); extern void ml_halt_cpu(void); extern ml_cpu_block *ml_this_cpu(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/socks/machine/hwlock.h b/arch/x86_64/include/socks/machine/hwlock.h index fa86265..c85d21c 100644 --- a/arch/x86_64/include/socks/machine/hwlock.h +++ b/arch/x86_64/include/socks/machine/hwlock.h @@ -3,6 +3,10 @@ #define ML_HWLOCK_INIT (0) +#ifdef __cplusplus +extern "C" { +#endif + typedef int ml_hwlock_t; extern void ml_hwlock_lock(ml_hwlock_t *lck); @@ -11,4 +15,8 @@ extern void ml_hwlock_unlock(ml_hwlock_t *lck); extern void ml_hwlock_lock_irqsave(ml_hwlock_t *lck, unsigned long *flags); extern void ml_hwlock_unlock_irqrestore(ml_hwlock_t *lck, unsigned long flags); +#ifdef __cplusplus +} +#endif + #endif diff --git a/arch/x86_64/include/socks/machine/init.h b/arch/x86_64/include/socks/machine/init.h index d95555e..b425439 100644 --- a/arch/x86_64/include/socks/machine/init.h +++ b/arch/x86_64/include/socks/machine/init.h @@ -3,6 +3,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + #define __X2(x) #x #define __X(x) __X2(x) @@ -12,4 +16,8 @@ extern int ml_init(uintptr_t arg); +#ifdef __cplusplus +} +#endif + #endif diff --git a/include/socks/bitmap.h b/include/socks/bitmap.h index c6203c9..4b393f0 100644 --- a/include/socks/bitmap.h +++ b/include/socks/bitmap.h @@ -3,6 +3,10 @@ #include +#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 diff --git a/include/socks/btree.h b/include/socks/btree.h index 67f025f..1367ca0 100644 --- a/include/socks/btree.h +++ b/include/socks/btree.h @@ -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 +#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 diff --git a/include/socks/console.h b/include/socks/console.h index 7d12eaf..a16aea6 100644 --- a/include/socks/console.h +++ b/include/socks/console.h @@ -18,6 +18,10 @@ #include #include +#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 diff --git a/include/socks/cpu.h b/include/socks/cpu.h index 5f11372..6bf1830 100644 --- a/include/socks/cpu.h +++ b/include/socks/cpu.h @@ -4,6 +4,10 @@ #include #include +#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 diff --git a/include/socks/init.h b/include/socks/init.h index c9e2e6f..f3b1e34 100644 --- a/include/socks/init.h +++ b/include/socks/init.h @@ -4,6 +4,10 @@ #include #include +#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 diff --git a/include/socks/locks.h b/include/socks/locks.h index 1a8f419..d35138b 100644 --- a/include/socks/locks.h +++ b/include/socks/locks.h @@ -4,6 +4,10 @@ #include #include +#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 diff --git a/include/socks/memblock.h b/include/socks/memblock.h index 977f13e..d4a77a1 100644 --- a/include/socks/memblock.h +++ b/include/socks/memblock.h @@ -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 #include +#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 diff --git a/include/socks/object.h b/include/socks/object.h index 7d8c4dc..d049085 100644 --- a/include/socks/object.h +++ b/include/socks/object.h @@ -6,6 +6,10 @@ #include #include +#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 diff --git a/include/socks/percpu.h b/include/socks/percpu.h index 0b7663e..cc8e98d 100644 --- a/include/socks/percpu.h +++ b/include/socks/percpu.h @@ -4,6 +4,10 @@ #include #include +#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 diff --git a/include/socks/pmap.h b/include/socks/pmap.h index b225023..9eac14c 100644 --- a/include/socks/pmap.h +++ b/include/socks/pmap.h @@ -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 diff --git a/include/socks/printk.h b/include/socks/printk.h index 1b28d48..1714e1d 100644 --- a/include/socks/printk.h +++ b/include/socks/printk.h @@ -3,7 +3,15 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + extern void early_printk_init(console_t *con); extern int printk(const char *format, ...); +#ifdef __cplusplus +} +#endif + #endif diff --git a/include/socks/queue.h b/include/socks/queue.h index c3adb2c..92596f3 100644 --- a/include/socks/queue.h +++ b/include/socks/queue.h @@ -4,6 +4,10 @@ #include #include +#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 diff --git a/include/socks/sched.h b/include/socks/sched.h index f346105..8197a59 100644 --- a/include/socks/sched.h +++ b/include/socks/sched.h @@ -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 diff --git a/include/socks/test.h b/include/socks/test.h index 7511ba4..5b921c1 100644 --- a/include/socks/test.h +++ b/include/socks/test.h @@ -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 diff --git a/include/socks/tty.h b/include/socks/tty.h index 4038e83..2ab6b5e 100644 --- a/include/socks/tty.h +++ b/include/socks/tty.h @@ -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 diff --git a/include/socks/util.h b/include/socks/util.h index 6ab5748..e48114c 100644 --- a/include/socks/util.h +++ b/include/socks/util.h @@ -5,6 +5,10 @@ #include #include +#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 diff --git a/include/socks/vm.h b/include/socks/vm.h index 254285e..f82a018 100644 --- a/include/socks/vm.h +++ b/include/socks/vm.h @@ -8,6 +8,10 @@ #include #include +#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 diff --git a/obj/object.c b/obj/object.c index ace0861..bd5be76 100644 --- a/obj/object.c +++ b/obj/object.c @@ -24,10 +24,10 @@ kern_status_t object_type_register(object_type_t *p) p->ob_cache.c_name = p->ob_name; p->ob_cache.c_obj_size = sizeof(object_t) + p->ob_size; p->ob_cache.c_page_order = VM_PAGE_16K; - + vm_cache_init(&p->ob_cache); p->ob_flags |= OBJTYPE_INIT; - + return KERN_OK; } @@ -46,7 +46,7 @@ object_t *object_create(object_type_t *type) if (!(type->ob_flags & OBJTYPE_INIT)) { return NULL; } - + vm_cache_t *cache = &type->ob_cache; object_t *obj = vm_cache_alloc(cache, 0); if (!obj) { @@ -58,7 +58,7 @@ object_t *object_create(object_type_t *type) obj->ob_magic = OBJECT_MAGIC; obj->ob_refcount = 1; obj->ob_handles = 0; - + return obj; } @@ -72,7 +72,7 @@ void object_deref(object_t *obj) { unsigned long flags; spin_lock_irqsave(&obj->ob_lock, &flags); - + if (obj->ob_refcount == 0) { spin_unlock_irqrestore(&obj->ob_lock, flags); return; @@ -85,8 +85,8 @@ void object_deref(object_t *obj) return; } - if (HAS_OP(obj, delete)) { - obj->ob_type->ob_ops.delete(obj); + if (HAS_OP(obj, destroy)) { + obj->ob_type->ob_ops.destroy(obj); } vm_cache_free(&obj->ob_type->ob_cache, obj); @@ -120,7 +120,7 @@ object_t *object_header(void *p) kern_status_t object_get_child_named(object_t *obj, const char *name, object_t **out) { kern_status_t status = KERN_UNSUPPORTED; - + if (HAS_OP(obj, get_named)) { status = obj->ob_type->ob_ops.get_named(obj, name, out); } @@ -131,7 +131,7 @@ kern_status_t object_get_child_named(object_t *obj, const char *name, object_t * kern_status_t object_get_child_at(object_t *obj, size_t at, object_t **out) { kern_status_t status = KERN_UNSUPPORTED; - + if (HAS_OP(obj, get_at)) { status = obj->ob_type->ob_ops.get_at(obj, at, out); } diff --git a/tools/make/gcc-host.mk b/tools/make/gcc-host.mk index df3bb86..2057cb6 100644 --- a/tools/make/gcc-host.mk +++ b/tools/make/gcc-host.mk @@ -1,7 +1,9 @@ LD := gcc CC := gcc +CXX := g++ ASM := gcc CFLAGS := -Wall -Werror -pedantic +CXXFLAGS := $(CFLAGS) ASMFLAGS := $(CFLAGS) LDFLAGS := -O2 diff --git a/tools/make/generate_compile_commands.py b/tools/make/generate_compile_commands.py index f7c2ac9..9e623b1 100755 --- a/tools/make/generate_compile_commands.py +++ b/tools/make/generate_compile_commands.py @@ -5,7 +5,7 @@ import os def is_source_file(filepath): - return filepath.endswith('.c') or filepath.endswith('.h') or filepath.endswith('.S') + return filepath.endswith('.c') or filepath.endswith('.cpp') or filepath.endswith('.h') or filepath.endswith('.S') dir_stack = [] rootdir = os.getcwd() @@ -30,7 +30,7 @@ for line in make_output_lines: dir_stack.pop() continue; - if 'gcc' not in line_parts[0]: + if 'gcc' not in line_parts[0] and 'g++' not in line_parts[0]: continue src_file = '' diff --git a/vm/cache.c b/vm/cache.c index f4d1b54..72dd8b2 100644 --- a/vm/cache.c +++ b/vm/cache.c @@ -106,11 +106,12 @@ static vm_slab_t *alloc_slab(vm_cache_t *cache, vm_flags_t flags) slab_hdr->s_obj_allocated = 0; slab_hdr->s_free = 0; + unsigned int *freelist = (unsigned int *)(slab_hdr + 1); for (unsigned int i = 0; i < cache->c_obj_count; i++) { - slab_hdr->s_freelist[i] = i + 1; + freelist[i] = i + 1; } - slab_hdr->s_freelist[cache->c_obj_count - 1] = FREELIST_END; + freelist[cache->c_obj_count - 1] = FREELIST_END; vm_page_foreach (slab_page, i) { i->p_slab = slab_hdr; @@ -131,7 +132,8 @@ static unsigned int slab_allocate_slot(vm_slab_t *slab) } unsigned int slot = slab->s_free; - slab->s_free = slab->s_freelist[slab->s_free]; + unsigned int *freelist = (unsigned int *)(slab + 1); + slab->s_free = freelist[slab->s_free]; slab->s_obj_allocated++; return slot; @@ -140,8 +142,10 @@ static unsigned int slab_allocate_slot(vm_slab_t *slab) static void slab_free_slot(vm_slab_t *slab, unsigned int slot) { unsigned int next = slab->s_free; + unsigned int *freelist = (unsigned int *)(slab + 1); + slab->s_free = slot; - slab->s_freelist[slot] = next; + freelist[slot] = next; slab->s_obj_allocated--; }