From 4aa74dbe2a35a45668b33d688f17b680c2232572 Mon Sep 17 00:00:00 2001 From: EmulatedSeasons <89668582+EmulatedSeasons@users.noreply.github.com> Date: Sun, 5 May 2024 01:08:00 -0400 Subject: improved makefile somewhat --- .gitignore | 6 +- isodir/boot/grub/grub.cfg | 9 + isodir/boot/limine/limine.cfg | 6 + kernel/arch/x86_64/crti.asm | 12 + kernel/arch/x86_64/crtn.asm | 9 + kernel/arch/x86_64/limine.h | 587 +++++++++++++++++++++++++++++++++++++++++ kernel/makefile | 35 ++- libc/README.txt | 5 + libc/abort.c | 11 + libc/include/stdio.h | 18 ++ libc/include/stdlib.h | 15 ++ libc/include/string.h | 20 ++ libc/include/sys/cdefs.h | 6 + libc/makefile | 79 ++++++ libc/memcmp.c | 13 + libc/memcpy.c | 9 + libc/memmove.c | 14 + libc/memset.c | 8 + libc/printf.c | 191 ++++++++++++++ libc/putchar.c | 9 + libc/puts.c | 5 + libc/strlen.c | 9 + libs/libck/README.txt | 5 - libs/libck/abort.c | 11 - libs/libck/include/stdio.h | 18 -- libs/libck/include/stdlib.h | 15 -- libs/libck/include/string.h | 20 -- libs/libck/include/sys/cdefs.h | 6 - libs/libck/makefile | 79 ------ libs/libck/memcmp.c | 13 - libs/libck/memcpy.c | 9 - libs/libck/memmove.c | 14 - libs/libck/memset.c | 8 - libs/libck/printf.c | 191 -------------- libs/libck/putchar.c | 9 - libs/libck/puts.c | 5 - libs/libck/strlen.c | 9 - makefile | 35 +-- 38 files changed, 1073 insertions(+), 450 deletions(-) create mode 100644 isodir/boot/grub/grub.cfg create mode 100644 isodir/boot/limine/limine.cfg create mode 100644 kernel/arch/x86_64/crti.asm create mode 100644 kernel/arch/x86_64/crtn.asm create mode 100644 kernel/arch/x86_64/limine.h create mode 100644 libc/README.txt create mode 100644 libc/abort.c create mode 100644 libc/include/stdio.h create mode 100644 libc/include/stdlib.h create mode 100644 libc/include/string.h create mode 100644 libc/include/sys/cdefs.h create mode 100644 libc/makefile create mode 100644 libc/memcmp.c create mode 100644 libc/memcpy.c create mode 100644 libc/memmove.c create mode 100644 libc/memset.c create mode 100644 libc/printf.c create mode 100644 libc/putchar.c create mode 100644 libc/puts.c create mode 100644 libc/strlen.c delete mode 100644 libs/libck/README.txt delete mode 100644 libs/libck/abort.c delete mode 100644 libs/libck/include/stdio.h delete mode 100644 libs/libck/include/stdlib.h delete mode 100644 libs/libck/include/string.h delete mode 100644 libs/libck/include/sys/cdefs.h delete mode 100644 libs/libck/makefile delete mode 100644 libs/libck/memcmp.c delete mode 100644 libs/libck/memcpy.c delete mode 100644 libs/libck/memmove.c delete mode 100644 libs/libck/memset.c delete mode 100644 libs/libck/printf.c delete mode 100644 libs/libck/putchar.c delete mode 100644 libs/libck/puts.c delete mode 100644 libs/libck/strlen.c diff --git a/.gitignore b/.gitignore index cc56a26..be76e0b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ # directories that have to exist but shouldn't be added to git -isodir/ sysroot/ build/ @@ -9,12 +8,15 @@ build/ *.o *.d *.a +*.sys +*.efi # i have no idea what this is for bx_enh_dbg.ini -# temporarily ignored cause i have no idea how to set up cmake with this and dont really have much else to use it for yet misc/ # not important make.png + +.vscode/ diff --git a/isodir/boot/grub/grub.cfg b/isodir/boot/grub/grub.cfg new file mode 100644 index 0000000..71b0917 --- /dev/null +++ b/isodir/boot/grub/grub.cfg @@ -0,0 +1,9 @@ +set timeout_style=menu +if [ "${timeout}" = 0 ]; then + #set timeout=10 # comment this original + set timeout=0 # add this +fi + +menuentry "emuos" { + multiboot /boot/kernel.bin +} \ No newline at end of file diff --git a/isodir/boot/limine/limine.cfg b/isodir/boot/limine/limine.cfg new file mode 100644 index 0000000..dd5e60b --- /dev/null +++ b/isodir/boot/limine/limine.cfg @@ -0,0 +1,6 @@ +TIMEOUT=5 + +:emuos + PROTOCOL=limine + KERNEL_PATH=boot:///boot/kernel.bin + KASLR=off \ No newline at end of file diff --git a/kernel/arch/x86_64/crti.asm b/kernel/arch/x86_64/crti.asm new file mode 100644 index 0000000..73ac499 --- /dev/null +++ b/kernel/arch/x86_64/crti.asm @@ -0,0 +1,12 @@ +; x86 crti.asm +section .init +global _init:function +_init: + push rbp + mov rbp, rsp + +section .fini +global _fini:function +_fini: + push rbp + mov rbp, rsp \ No newline at end of file diff --git a/kernel/arch/x86_64/crtn.asm b/kernel/arch/x86_64/crtn.asm new file mode 100644 index 0000000..322b3a2 --- /dev/null +++ b/kernel/arch/x86_64/crtn.asm @@ -0,0 +1,9 @@ +; x86 crtn.asm + +section .init + pop rbp + ret + +section .fini + pop rbp + ret \ No newline at end of file diff --git a/kernel/arch/x86_64/limine.h b/kernel/arch/x86_64/limine.h new file mode 100644 index 0000000..0e7e87a --- /dev/null +++ b/kernel/arch/x86_64/limine.h @@ -0,0 +1,587 @@ +/* BSD Zero Clause License */ + +/* Copyright (C) 2022-2024 mintsuki and contributors. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef LIMINE_H +#define LIMINE_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* Misc */ + +#ifdef LIMINE_NO_POINTERS +# define LIMINE_PTR(TYPE) uint64_t +#else +# define LIMINE_PTR(TYPE) TYPE +#endif + +#ifdef __GNUC__ +# define LIMINE_DEPRECATED __attribute__((__deprecated__)) +# define LIMINE_DEPRECATED_IGNORE_START \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +# define LIMINE_DEPRECATED_IGNORE_END \ + _Pragma("GCC diagnostic pop") +#else +# define LIMINE_DEPRECATED +# define LIMINE_DEPRECATED_IGNORE_START +# define LIMINE_DEPRECATED_IGNORE_END +#endif + +#define LIMINE_REQUESTS_START_MARKER \ + uint64_t limine_requests_start_marker[4] = { 0xf6b8f4b39de7d1ae, 0xfab91a6940fcb9cf, \ + 0x785c6ed015d3e316, 0x181e920a7852b9d9 }; +#define LIMINE_REQUESTS_END_MARKER \ + uint64_t limine_requests_end_marker[2] = { 0xadc0e0531bb10d03, 0x9572709f31764c62 }; + +#define LIMINE_REQUESTS_DELIMITER LIMINE_REQUESTS_END_MARKER + +#define LIMINE_BASE_REVISION(N) \ + uint64_t limine_base_revision[3] = { 0xf9562b2d5c95a6c8, 0x6a7b384944536bdc, (N) }; + +#define LIMINE_BASE_REVISION_SUPPORTED (limine_base_revision[2] == 0) + +#define LIMINE_COMMON_MAGIC 0xc7b1dd30df4c8b88, 0x0a82e883a194f07b + +struct limine_uuid { + uint32_t a; + uint16_t b; + uint16_t c; + uint8_t d[8]; +}; + +#define LIMINE_MEDIA_TYPE_GENERIC 0 +#define LIMINE_MEDIA_TYPE_OPTICAL 1 +#define LIMINE_MEDIA_TYPE_TFTP 2 + +struct limine_file { + uint64_t revision; + LIMINE_PTR(void *) address; + uint64_t size; + LIMINE_PTR(char *) path; + LIMINE_PTR(char *) cmdline; + uint32_t media_type; + uint32_t unused; + uint32_t tftp_ip; + uint32_t tftp_port; + uint32_t partition_index; + uint32_t mbr_disk_id; + struct limine_uuid gpt_disk_uuid; + struct limine_uuid gpt_part_uuid; + struct limine_uuid part_uuid; +}; + +/* Boot info */ + +#define LIMINE_BOOTLOADER_INFO_REQUEST { LIMINE_COMMON_MAGIC, 0xf55038d8e2a1202f, 0x279426fcf5f59740 } + +struct limine_bootloader_info_response { + uint64_t revision; + LIMINE_PTR(char *) name; + LIMINE_PTR(char *) version; +}; + +struct limine_bootloader_info_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_bootloader_info_response *) response; +}; + +/* Stack size */ + +#define LIMINE_STACK_SIZE_REQUEST { LIMINE_COMMON_MAGIC, 0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d } + +struct limine_stack_size_response { + uint64_t revision; +}; + +struct limine_stack_size_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_stack_size_response *) response; + uint64_t stack_size; +}; + +/* HHDM */ + +#define LIMINE_HHDM_REQUEST { LIMINE_COMMON_MAGIC, 0x48dcf1cb8ad2b852, 0x63984e959a98244b } + +struct limine_hhdm_response { + uint64_t revision; + uint64_t offset; +}; + +struct limine_hhdm_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_hhdm_response *) response; +}; + +/* Framebuffer */ + +#define LIMINE_FRAMEBUFFER_REQUEST { LIMINE_COMMON_MAGIC, 0x9d5827dcd881dd75, 0xa3148604f6fab11b } + +#define LIMINE_FRAMEBUFFER_RGB 1 + +struct limine_video_mode { + uint64_t pitch; + uint64_t width; + uint64_t height; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; +}; + +struct limine_framebuffer { + LIMINE_PTR(void *) address; + uint64_t width; + uint64_t height; + uint64_t pitch; + uint16_t bpp; + uint8_t memory_model; + uint8_t red_mask_size; + uint8_t red_mask_shift; + uint8_t green_mask_size; + uint8_t green_mask_shift; + uint8_t blue_mask_size; + uint8_t blue_mask_shift; + uint8_t unused[7]; + uint64_t edid_size; + LIMINE_PTR(void *) edid; + /* Response revision 1 */ + uint64_t mode_count; + LIMINE_PTR(struct limine_video_mode **) modes; +}; + +struct limine_framebuffer_response { + uint64_t revision; + uint64_t framebuffer_count; + LIMINE_PTR(struct limine_framebuffer **) framebuffers; +}; + +struct limine_framebuffer_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_framebuffer_response *) response; +}; + +/* Terminal */ + +#define LIMINE_TERMINAL_REQUEST { LIMINE_COMMON_MAGIC, 0xc8ac59310c2b0844, 0xa68d0c7265d38878 } + +#define LIMINE_TERMINAL_CB_DEC 10 +#define LIMINE_TERMINAL_CB_BELL 20 +#define LIMINE_TERMINAL_CB_PRIVATE_ID 30 +#define LIMINE_TERMINAL_CB_STATUS_REPORT 40 +#define LIMINE_TERMINAL_CB_POS_REPORT 50 +#define LIMINE_TERMINAL_CB_KBD_LEDS 60 +#define LIMINE_TERMINAL_CB_MODE 70 +#define LIMINE_TERMINAL_CB_LINUX 80 + +#define LIMINE_TERMINAL_CTX_SIZE ((uint64_t)(-1)) +#define LIMINE_TERMINAL_CTX_SAVE ((uint64_t)(-2)) +#define LIMINE_TERMINAL_CTX_RESTORE ((uint64_t)(-3)) +#define LIMINE_TERMINAL_FULL_REFRESH ((uint64_t)(-4)) + +/* Response revision 1 */ +#define LIMINE_TERMINAL_OOB_OUTPUT_GET ((uint64_t)(-10)) +#define LIMINE_TERMINAL_OOB_OUTPUT_SET ((uint64_t)(-11)) + +#define LIMINE_TERMINAL_OOB_OUTPUT_OCRNL (1 << 0) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFDEL (1 << 1) +#define LIMINE_TERMINAL_OOB_OUTPUT_OFILL (1 << 2) +#define LIMINE_TERMINAL_OOB_OUTPUT_OLCUC (1 << 3) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLCR (1 << 4) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONLRET (1 << 5) +#define LIMINE_TERMINAL_OOB_OUTPUT_ONOCR (1 << 6) +#define LIMINE_TERMINAL_OOB_OUTPUT_OPOST (1 << 7) + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_terminal; + +typedef void (*limine_terminal_write)(struct limine_terminal *, const char *, uint64_t); +typedef void (*limine_terminal_callback)(struct limine_terminal *, uint64_t, uint64_t, uint64_t, uint64_t); + +struct LIMINE_DEPRECATED limine_terminal { + uint64_t columns; + uint64_t rows; + LIMINE_PTR(struct limine_framebuffer *) framebuffer; +}; + +struct LIMINE_DEPRECATED limine_terminal_response { + uint64_t revision; + uint64_t terminal_count; + LIMINE_PTR(struct limine_terminal **) terminals; + LIMINE_PTR(limine_terminal_write) write; +}; + +struct LIMINE_DEPRECATED limine_terminal_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_terminal_response *) response; + LIMINE_PTR(limine_terminal_callback) callback; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* Paging mode */ + +#define LIMINE_PAGING_MODE_REQUEST { LIMINE_COMMON_MAGIC, 0x95c1a0edab0944cb, 0xa4e5cb3842f7488a } + +#if defined (__x86_64__) || defined (__i386__) +#define LIMINE_PAGING_MODE_X86_64_4LVL 0 +#define LIMINE_PAGING_MODE_X86_64_5LVL 1 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_X86_64_5LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_X86_64_4LVL +#elif defined (__aarch64__) +#define LIMINE_PAGING_MODE_AARCH64_4LVL 0 +#define LIMINE_PAGING_MODE_AARCH64_5LVL 1 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_AARCH64_5LVL +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_AARCH64_4LVL +#elif defined (__riscv) && (__riscv_xlen == 64) +#define LIMINE_PAGING_MODE_RISCV_SV39 0 +#define LIMINE_PAGING_MODE_RISCV_SV48 1 +#define LIMINE_PAGING_MODE_RISCV_SV57 2 +#define LIMINE_PAGING_MODE_MAX LIMINE_PAGING_MODE_RISCV_SV57 +#define LIMINE_PAGING_MODE_DEFAULT LIMINE_PAGING_MODE_RISCV_SV48 +#else +#error Unknown architecture +#endif + +struct limine_paging_mode_response { + uint64_t revision; + uint64_t mode; + uint64_t flags; +}; + +struct limine_paging_mode_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_paging_mode_response *) response; + uint64_t mode; + uint64_t flags; +}; + +/* 5-level paging */ + +#define LIMINE_5_LEVEL_PAGING_REQUEST { LIMINE_COMMON_MAGIC, 0x94469551da9b3192, 0xebe5e86db7382888 } + +LIMINE_DEPRECATED_IGNORE_START + +struct LIMINE_DEPRECATED limine_5_level_paging_response { + uint64_t revision; +}; + +struct LIMINE_DEPRECATED limine_5_level_paging_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_5_level_paging_response *) response; +}; + +LIMINE_DEPRECATED_IGNORE_END + +/* SMP */ + +#define LIMINE_SMP_REQUEST { LIMINE_COMMON_MAGIC, 0x95a67b819a1b857e, 0xa0b61b723b6a73e0 } + +struct limine_smp_info; + +typedef void (*limine_goto_address)(struct limine_smp_info *); + +#if defined (__x86_64__) || defined (__i386__) + +#define LIMINE_SMP_X2APIC (1 << 0) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t lapic_id; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint32_t flags; + uint32_t bsp_lapic_id; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__aarch64__) + +struct limine_smp_info { + uint32_t processor_id; + uint32_t gic_iface_no; + uint64_t mpidr; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_mpidr; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#elif defined (__riscv) && (__riscv_xlen == 64) + +struct limine_smp_info { + uint64_t processor_id; + uint64_t hartid; + uint64_t reserved; + LIMINE_PTR(limine_goto_address) goto_address; + uint64_t extra_argument; +}; + +struct limine_smp_response { + uint64_t revision; + uint64_t flags; + uint64_t bsp_hartid; + uint64_t cpu_count; + LIMINE_PTR(struct limine_smp_info **) cpus; +}; + +#else +#error Unknown architecture +#endif + +struct limine_smp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smp_response *) response; + uint64_t flags; +}; + +/* Memory map */ + +#define LIMINE_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x67cf3d9d378a806f, 0xe304acdfc50c3c62 } + +#define LIMINE_MEMMAP_USABLE 0 +#define LIMINE_MEMMAP_RESERVED 1 +#define LIMINE_MEMMAP_ACPI_RECLAIMABLE 2 +#define LIMINE_MEMMAP_ACPI_NVS 3 +#define LIMINE_MEMMAP_BAD_MEMORY 4 +#define LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE 5 +#define LIMINE_MEMMAP_KERNEL_AND_MODULES 6 +#define LIMINE_MEMMAP_FRAMEBUFFER 7 + +struct limine_memmap_entry { + uint64_t base; + uint64_t length; + uint64_t type; +}; + +struct limine_memmap_response { + uint64_t revision; + uint64_t entry_count; + LIMINE_PTR(struct limine_memmap_entry **) entries; +}; + +struct limine_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_memmap_response *) response; +}; + +/* Entry point */ + +#define LIMINE_ENTRY_POINT_REQUEST { LIMINE_COMMON_MAGIC, 0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a } + +typedef void (*limine_entry_point)(void); + +struct limine_entry_point_response { + uint64_t revision; +}; + +struct limine_entry_point_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_entry_point_response *) response; + LIMINE_PTR(limine_entry_point) entry; +}; + +/* Kernel File */ + +#define LIMINE_KERNEL_FILE_REQUEST { LIMINE_COMMON_MAGIC, 0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69 } + +struct limine_kernel_file_response { + uint64_t revision; + LIMINE_PTR(struct limine_file *) kernel_file; +}; + +struct limine_kernel_file_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_file_response *) response; +}; + +/* Module */ + +#define LIMINE_MODULE_REQUEST { LIMINE_COMMON_MAGIC, 0x3e7e279702be32af, 0xca1c4f3bd1280cee } + +#define LIMINE_INTERNAL_MODULE_REQUIRED (1 << 0) +#define LIMINE_INTERNAL_MODULE_COMPRESSED (1 << 1) + +struct limine_internal_module { + LIMINE_PTR(const char *) path; + LIMINE_PTR(const char *) cmdline; + uint64_t flags; +}; + +struct limine_module_response { + uint64_t revision; + uint64_t module_count; + LIMINE_PTR(struct limine_file **) modules; +}; + +struct limine_module_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_module_response *) response; + + /* Request revision 1 */ + uint64_t internal_module_count; + LIMINE_PTR(struct limine_internal_module **) internal_modules; +}; + +/* RSDP */ + +#define LIMINE_RSDP_REQUEST { LIMINE_COMMON_MAGIC, 0xc5e77b6b397e7b43, 0x27637845accdcf3c } + +struct limine_rsdp_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_rsdp_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_rsdp_response *) response; +}; + +/* SMBIOS */ + +#define LIMINE_SMBIOS_REQUEST { LIMINE_COMMON_MAGIC, 0x9e9046f11e095391, 0xaa4a520fefbde5ee } + +struct limine_smbios_response { + uint64_t revision; + LIMINE_PTR(void *) entry_32; + LIMINE_PTR(void *) entry_64; +}; + +struct limine_smbios_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_smbios_response *) response; +}; + +/* EFI system table */ + +#define LIMINE_EFI_SYSTEM_TABLE_REQUEST { LIMINE_COMMON_MAGIC, 0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc } + +struct limine_efi_system_table_response { + uint64_t revision; + LIMINE_PTR(void *) address; +}; + +struct limine_efi_system_table_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_system_table_response *) response; +}; + +/* EFI memory map */ + +#define LIMINE_EFI_MEMMAP_REQUEST { LIMINE_COMMON_MAGIC, 0x7df62a431d6872d5, 0xa4fcdfb3e57306c8 } + +struct limine_efi_memmap_response { + uint64_t revision; + LIMINE_PTR(void *) memmap; + uint64_t memmap_size; + uint64_t desc_size; + uint64_t desc_version; +}; + +struct limine_efi_memmap_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_efi_memmap_response *) response; +}; + +/* Boot time */ + +#define LIMINE_BOOT_TIME_REQUEST { LIMINE_COMMON_MAGIC, 0x502746e184c088aa, 0xfbc5ec83e6327893 } + +struct limine_boot_time_response { + uint64_t revision; + int64_t boot_time; +}; + +struct limine_boot_time_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_boot_time_response *) response; +}; + +/* Kernel address */ + +#define LIMINE_KERNEL_ADDRESS_REQUEST { LIMINE_COMMON_MAGIC, 0x71ba76863cc55f63, 0xb2644a48c516a487 } + +struct limine_kernel_address_response { + uint64_t revision; + uint64_t physical_base; + uint64_t virtual_base; +}; + +struct limine_kernel_address_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_kernel_address_response *) response; +}; + +/* Device Tree Blob */ + +#define LIMINE_DTB_REQUEST { LIMINE_COMMON_MAGIC, 0xb40ddb48fb54bac7, 0x545081493f81ffb7 } + +struct limine_dtb_response { + uint64_t revision; + LIMINE_PTR(void *) dtb_ptr; +}; + +struct limine_dtb_request { + uint64_t id[4]; + uint64_t revision; + LIMINE_PTR(struct limine_dtb_response *) response; +}; + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/kernel/makefile b/kernel/makefile index 7d02b32..b2a2660 100644 --- a/kernel/makefile +++ b/kernel/makefile @@ -1,19 +1,15 @@ # Kernel makefile -ifndef CFLAGS - CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude - CFLAGS += -isystem="/usr/include" -endif - -ifndef CXXFLAGS - CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude - CXXFLAGS += -Iinclude - CXXFLAGS += -isystem="/usr/include" -endif - -ifndef LDFLAGS - LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude - LDFLAGS += -isystem="/usr/include" +CFLAGS := -ffreestanding -Wall -Wextra -g -std=gnu99 -O2 -Iinclude +CXXFLAGS := -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -g -O2 -Iinclude +LDFLAGS := -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude +ASMFLAGS := + +ifeq ($(ARCH),x86_64) +CFLAGS += -mno-red-zone +CXXFLAGS += -mno-red-zone +LDFLAGS += -mno-red-zone +ASMFLAGS += -felf64 endif ifndef SYSROOT @@ -21,14 +17,13 @@ ifndef SYSROOT endif -LIBS = -nostdlib -lgcc -lck +LIBS = -nostdlib -lck -lgcc #Find all the source files C_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.c') CPP_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.cpp') HEADERS := $(shell find $(PWD) -type f -name '*.h') ASMFILES := $(shell find $(PWD) -type f -name '*.asm' ! -name 'crti.asm' ! -name 'crtn.asm') -GASFILES := $(wildcard *.s) CRTBEGIN := $(shell $(CXX) $(CXXFLAGS) -print-file-name=crtbegin.o) CRTEND := $(shell $(CXX) $(CXXFLAGS) -print-file-name=crtend.o) @@ -54,7 +49,7 @@ all: crti.o crtn.o kernel.bin kernel.bin: ${OBJECTS} $(info [INFO] Linking kernel) - $(CXX) ${LDFLAGS} -o ${PREFIX}/kernel.bin $(LINKLST) + $(CXX) ${LDFLAGS} -o $@ $(LINKLST) %.o: %.cpp $(info [INFO] Compiling $<) @@ -66,13 +61,13 @@ kernel.bin: ${OBJECTS} %.o: %.asm $(info [INFO] Assembling $<) - $(NASM) $< -felf32 -o $@ -g + $(NASM) $< $(ASMFLAGS) -o $@ -g crti.o: - $(NASM) arch/$(ARCH)/crti.asm -felf32 -o $@ + $(NASM) arch/$(ARCH)/crti.asm $(ASMFLAGS) -o $@ crtn.o: - $(NASM) arch/$(ARCH)/crtn.asm -felf32 -o $@ + $(NASM) arch/$(ARCH)/crtn.asm $(ASMFLAGS) -o $@ install-headers: cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include diff --git a/libc/README.txt b/libc/README.txt new file mode 100644 index 0000000..6806bd4 --- /dev/null +++ b/libc/README.txt @@ -0,0 +1,5 @@ +a temporary(?) libc for the kernel + +code based off meaty skeleton from osdev.org + +todo: \ No newline at end of file diff --git a/libc/abort.c b/libc/abort.c new file mode 100644 index 0000000..83873e8 --- /dev/null +++ b/libc/abort.c @@ -0,0 +1,11 @@ +#include +#include + +__attribute__((__noreturn__)) +void abort(void) { + // TODO: Add proper kernel panic. + printf("kernel: panic: abort()\n"); + asm volatile("hlt"); + while (1) { } + __builtin_unreachable(); +} \ No newline at end of file diff --git a/libc/include/stdio.h b/libc/include/stdio.h new file mode 100644 index 0000000..eb4e45b --- /dev/null +++ b/libc/include/stdio.h @@ -0,0 +1,18 @@ +#ifndef _STDIO_H +#define _STDIO_H 1 + +#define EOF (-1) + +#ifdef __cplusplus +extern "C" { +#endif + +int printf(const char* __restrict, ...); +int putchar(int); +int puts(const char*); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h new file mode 100644 index 0000000..1d1ee32 --- /dev/null +++ b/libc/include/stdlib.h @@ -0,0 +1,15 @@ +#ifndef _STDLIB_H +#define _STDLIB_H 1 + +#ifdef __cplusplus +extern "C" { +#endif + +__attribute__((__noreturn__)) +void abort(void); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libc/include/string.h b/libc/include/string.h new file mode 100644 index 0000000..4f6b5bd --- /dev/null +++ b/libc/include/string.h @@ -0,0 +1,20 @@ +#ifndef _STRING_H +#define _STRING_H 1 + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int memcmp(const void*, const void*, size_t); +void* memcpy(void* __restrict, const void* __restrict, size_t); +void* memmove(void*, const void*, size_t); +void* memset(void*, int, size_t); +size_t strlen(const char*); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h new file mode 100644 index 0000000..1e2c277 --- /dev/null +++ b/libc/include/sys/cdefs.h @@ -0,0 +1,6 @@ +#ifndef _SYS_CDEFS_H +#define _SYS_CDEFS_H 1 + + + +#endif \ No newline at end of file diff --git a/libc/makefile b/libc/makefile new file mode 100644 index 0000000..2e1912e --- /dev/null +++ b/libc/makefile @@ -0,0 +1,79 @@ +# libck makefile + +ifndef AR + AR = i686-elf-ar +endif + +ifndef CFLAGS + CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu11 -O2 -Iinclude + CFLAGS += --sysroot="$(SYSROOT)" + CFLAGS += -isystem="/usr/include" +endif + +ifndef CXXFLAGS + CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude + CXXFLAGS += -Iinclude + CXXFLAGS += --sysroot="$(SYSROOT)" + CXXFLAGS += -isystem="/usr/include" +endif + +ifndef LDFLAGS + LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude + LDFLAGS += --sysroot="$(SYSROOT)" + LDFLAGS += -isystem="/usr/include" +endif + +ifndef SYSROOT + $(error No sysroot specified) +endif + + +LIBS = -nostdlib -lgcc + +#Find all the source files +C_SOURCES := $(shell find $(PWD) -type f -name '*.c') +#CPP_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.cpp') +HEADERS := $(shell find $(PWD) -type f -name '*.h') +#ASMFILES := $(shell find $(PWD) -type f -name '*.asm') + +OBJECTS := $(patsubst %.c,%.o,$(C_SOURCES)) +# OBJECTS += $(patsubst %.cpp,%.o,$(CPP_SOURCES)) +# OBJECTS += $(patsubst %.asm,%.o,$(ASMFILES)) + +DEPFILES := $(patsubst %.c,%.d,$(C_SOURCES)) +# DEPFILES += $(patsubst %.cpp,%.d,$(CPP_SOURCES)) + +LINKLST += $(OBJECTS) +LINKLST += $(LIBS) + +-include $(DEPFILES) + +.PHONY: all install-headers install-lib clean + +all: libc.a install-lib + +libc.a: ${OBJECTS} + $(info [INFO] Linking $<) + $(AR) rcs $@ $(OBJECTS) + +%.o: %.cpp + $(info [INFO] Compiling $<) + $(CXX) ${CXXFLAGS} -MMD -MP -c $< -o $@ + +%.o: %.c + $(info [INFO] Compiling $<) + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + +%.o: %.asm + $(info [INFO] Assembling $<) + $(NASM) $< -felf32 -o $@ -g + +install-headers: + cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include + +install-lib: libck.a + cp -r $< $(SYSROOT)/usr/lib/. + +clean: + $(info [INFO] Cleaning) + $(RM) ${OBJECTS} $(DEPFILES) \ No newline at end of file diff --git a/libc/memcmp.c b/libc/memcmp.c new file mode 100644 index 0000000..93141e1 --- /dev/null +++ b/libc/memcmp.c @@ -0,0 +1,13 @@ +#include + +int memcmp(const void* aptr, const void* bptr, size_t size) { + const unsigned char* a = (const unsigned char*) aptr; + const unsigned char* b = (const unsigned char*) bptr; + for (size_t i = 0; i < size; i++) { + if (a[i] < b[i]) + return -1; + else if (b[i] < a[i]) + return 1; + } + return 0; +} \ No newline at end of file diff --git a/libc/memcpy.c b/libc/memcpy.c new file mode 100644 index 0000000..7e09be7 --- /dev/null +++ b/libc/memcpy.c @@ -0,0 +1,9 @@ +#include + +void* memcpy(void* restrict dstptr, const void* restrict srcptr, size_t size) { + unsigned char* dst = (unsigned char*) dstptr; + const unsigned char* src = (const unsigned char*) srcptr; + for (size_t i=0; i < size; i++) + dst[i] = src[i]; + return dstptr; +} \ No newline at end of file diff --git a/libc/memmove.c b/libc/memmove.c new file mode 100644 index 0000000..98c787f --- /dev/null +++ b/libc/memmove.c @@ -0,0 +1,14 @@ +#include + +void* memmove(void* dstptr, const void* srcptr, size_t size) { + unsigned char* dst = (unsigned char*) dstptr; + const unsigned char* src = (const unsigned char*) srcptr; + if (dst < src) { + for (size_t i = 0; i < size; i++) + dst[i] = src[i]; + } else { + for (size_t i = size; i != 0; i--) + dst[i-1] = src[i-1]; + } + return dstptr; +} \ No newline at end of file diff --git a/libc/memset.c b/libc/memset.c new file mode 100644 index 0000000..67dadfe --- /dev/null +++ b/libc/memset.c @@ -0,0 +1,8 @@ +#include + +void* memset(void* bufptr, int value, size_t size) { + unsigned char* buf = (unsigned char*) bufptr; + for (size_t i = 0; i < size; i++) + buf[i] = (unsigned char) value; + return bufptr; +} \ No newline at end of file diff --git a/libc/printf.c b/libc/printf.c new file mode 100644 index 0000000..72a4749 --- /dev/null +++ b/libc/printf.c @@ -0,0 +1,191 @@ +#include +#include +#include +#include +#include +#include + +static bool print(const char* data, size_t length) { + const unsigned char* bytes = (const unsigned char*) data; + for (size_t i = 0; i < length; i++) + if (putchar(bytes[i]) == EOF) + return false; + return true; +} + +// for %d and %x. +static const char* itoa(unsigned int num, char* str, unsigned int base) { + int cou = 0; // nt + + if (num == 0) { // 0 + str[cou] = '0'; + str[++cou] = '\0'; + return str; + } + + while (num) + { + unsigned int rem = (num % base); + // brings the number up to ascii 0 + the digit + if (rem > 9) { + str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 + } else { + str[cou] = rem + '0'; + } + num /= base; + ++cou; + } + + // reverse the string + for (int i = 0; i < cou; i++) { + char temp = str[i]; + str[i] = str[cou - i - 1]; + str[cou - 1] = temp; + } + + str[cou] = '\0'; + return str; +} + +// have no idea if there's a better way to do this. for %lx +static const char* itoa_u64(unsigned long long num, char* str, int base) { + int cou = 0; // nt + + if (num == 0) { // 0 + str[cou] = '0'; + str[++cou] = '\0'; + return str; + } + + while (num) + { + unsigned long long rem = num % base; + // brings the number up to ascii 0 + the digit + if (rem > 9) { + str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 + } else { + str[cou] = rem + '0'; + } + num /= base; + ++cou; + } + + // reverse the string + for (int i = 0; i < cou; i++) { + char temp = str[i]; + str[i] = str[cou - i - 1]; + str[cou - 1] = temp; + } + + str[cou] = '\0'; + return str; +} + +int printf(const char* restrict format, ...) { + va_list parameters; + va_start(parameters, format); + + int written = 0; + + while (*format != '\0') { + size_t maxrem = INT_MAX - written; + + if (format[0] != '%' || format[1] == '%') { + if (format[0] == '%') + format++; + size_t amount = 1; + while (format[amount] && format[amount] != '%') + amount++; + if (maxrem < amount) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(format, amount)) + return -1; + format += amount; + written += amount; + continue; + } + + const char* format_begun_at = format++; + + if (*format == 'c') { + format++; + char c = (char) va_arg(parameters, int /* char promotes to int */); + if (!maxrem) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(&c, sizeof(c))) + return -1; + written++; + } else if (*format == 'd') { + format++; + char str[20]; // probably a long enough buffer + unsigned int d = va_arg(parameters, unsigned int); + itoa(d, str, 10); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } else if (*format == 'x') { + format++; + char str[20]; // probably a long enough buffer + unsigned int d = va_arg(parameters, unsigned int); + itoa(d, str, 16); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } else if (*format == 'l') { // for %lx and others that start with l + format++; + if (*format == 'x') { + format++; + char str[20]; // probably a long enough buffer + unsigned long long d = va_arg(parameters, unsigned long long); + itoa_u64(d, str, 16); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } + } else if (*format == 's') { + format++; + const char* str = va_arg(parameters, const char*); + size_t len = strlen(str); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(str, len)) + return -1; + written += len; + } else { + format = format_begun_at; + size_t len = strlen(format); + if (maxrem < len) { + // TODO: Set errno to EOVERFLOW. + return -1; + } + if (!print(format, len)) + return -1; + written += len; + format += len; + } + } + + va_end(parameters); + return written; +} \ No newline at end of file diff --git a/libc/putchar.c b/libc/putchar.c new file mode 100644 index 0000000..c5a7976 --- /dev/null +++ b/libc/putchar.c @@ -0,0 +1,9 @@ +#include + +#include + +int putchar(int ic) { + char c = (char) ic; + terminal_write(&c, sizeof(c)); + return ic; +} \ No newline at end of file diff --git a/libc/puts.c b/libc/puts.c new file mode 100644 index 0000000..8c8c036 --- /dev/null +++ b/libc/puts.c @@ -0,0 +1,5 @@ +#include + +int puts(const char* string) { + return printf("%s\n", string); +} \ No newline at end of file diff --git a/libc/strlen.c b/libc/strlen.c new file mode 100644 index 0000000..441622a --- /dev/null +++ b/libc/strlen.c @@ -0,0 +1,9 @@ +#include + +size_t strlen(const char* str) { + size_t len = 0; + while (str[len]) { + ++len; + } + return len; +} \ No newline at end of file diff --git a/libs/libck/README.txt b/libs/libck/README.txt deleted file mode 100644 index 6806bd4..0000000 --- a/libs/libck/README.txt +++ /dev/null @@ -1,5 +0,0 @@ -a temporary(?) libc for the kernel - -code based off meaty skeleton from osdev.org - -todo: \ No newline at end of file diff --git a/libs/libck/abort.c b/libs/libck/abort.c deleted file mode 100644 index 83873e8..0000000 --- a/libs/libck/abort.c +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -__attribute__((__noreturn__)) -void abort(void) { - // TODO: Add proper kernel panic. - printf("kernel: panic: abort()\n"); - asm volatile("hlt"); - while (1) { } - __builtin_unreachable(); -} \ No newline at end of file diff --git a/libs/libck/include/stdio.h b/libs/libck/include/stdio.h deleted file mode 100644 index eb4e45b..0000000 --- a/libs/libck/include/stdio.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _STDIO_H -#define _STDIO_H 1 - -#define EOF (-1) - -#ifdef __cplusplus -extern "C" { -#endif - -int printf(const char* __restrict, ...); -int putchar(int); -int puts(const char*); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/libs/libck/include/stdlib.h b/libs/libck/include/stdlib.h deleted file mode 100644 index 1d1ee32..0000000 --- a/libs/libck/include/stdlib.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef _STDLIB_H -#define _STDLIB_H 1 - -#ifdef __cplusplus -extern "C" { -#endif - -__attribute__((__noreturn__)) -void abort(void); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/libs/libck/include/string.h b/libs/libck/include/string.h deleted file mode 100644 index 4f6b5bd..0000000 --- a/libs/libck/include/string.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef _STRING_H -#define _STRING_H 1 - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -int memcmp(const void*, const void*, size_t); -void* memcpy(void* __restrict, const void* __restrict, size_t); -void* memmove(void*, const void*, size_t); -void* memset(void*, int, size_t); -size_t strlen(const char*); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file diff --git a/libs/libck/include/sys/cdefs.h b/libs/libck/include/sys/cdefs.h deleted file mode 100644 index 1e2c277..0000000 --- a/libs/libck/include/sys/cdefs.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _SYS_CDEFS_H -#define _SYS_CDEFS_H 1 - - - -#endif \ No newline at end of file diff --git a/libs/libck/makefile b/libs/libck/makefile deleted file mode 100644 index 5f7b270..0000000 --- a/libs/libck/makefile +++ /dev/null @@ -1,79 +0,0 @@ -# libck makefile - -ifndef AR - AR = i686-elf-ar -endif - -ifndef CFLAGS - CFLAGS = -ffreestanding -Wall -Wextra -g -std=gnu11 -O2 -Iinclude - CFLAGS += --sysroot="$(SYSROOT)" - CFLAGS += -isystem="/usr/include" -endif - -ifndef CXXFLAGS - CXXFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -fno-rtti -D__is_kernel -g -O2 -Iinclude - CXXFLAGS += -Iinclude - CXXFLAGS += --sysroot="$(SYSROOT)" - CXXFLAGS += -isystem="/usr/include" -endif - -ifndef LDFLAGS - LDFLAGS = -T arch/$(ARCH)/linker.ld -ffreestanding -g -O2 -Iinclude - LDFLAGS += --sysroot="$(SYSROOT)" - LDFLAGS += -isystem="/usr/include" -endif - -ifndef SYSROOT - $(error No sysroot specified) -endif - - -LIBS = -nostdlib -lgcc - -#Find all the source files -C_SOURCES := $(shell find $(PWD) -type f -name '*.c') -#CPP_SOURCES := $(shell find $(PWD)/kernel -type f -name '*.cpp') -HEADERS := $(shell find $(PWD) -type f -name '*.h') -#ASMFILES := $(shell find $(PWD) -type f -name '*.asm') - -OBJECTS := $(patsubst %.c,%.o,$(C_SOURCES)) -# OBJECTS += $(patsubst %.cpp,%.o,$(CPP_SOURCES)) -# OBJECTS += $(patsubst %.asm,%.o,$(ASMFILES)) - -DEPFILES := $(patsubst %.c,%.d,$(C_SOURCES)) -# DEPFILES += $(patsubst %.cpp,%.d,$(CPP_SOURCES)) - -LINKLST += $(OBJECTS) -LINKLST += $(LIBS) - --include $(DEPFILES) - -.PHONY: all install-headers install-lib clean - -all: libck.a install-headers install-lib - -libck.a: ${OBJECTS} - $(info [INFO] Linking $<) - $(AR) rcs $@ $(OBJECTS) - -%.o: %.cpp - $(info [INFO] Compiling $<) - $(CXX) ${CXXFLAGS} -MMD -MP -c $< -o $@ - -%.o: %.c - $(info [INFO] Compiling $<) - $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ - -%.o: %.asm - $(info [INFO] Assembling $<) - $(NASM) $< -felf32 -o $@ -g - -install-headers: - cp -r --preserve=timestamps include/. $(SYSROOT)/usr/include - -install-lib: libck.a - cp -r $< $(SYSROOT)/usr/lib/. - -clean: - $(info [INFO] Cleaning) - $(RM) ${OBJECTS} $(DEPFILES) \ No newline at end of file diff --git a/libs/libck/memcmp.c b/libs/libck/memcmp.c deleted file mode 100644 index 93141e1..0000000 --- a/libs/libck/memcmp.c +++ /dev/null @@ -1,13 +0,0 @@ -#include - -int memcmp(const void* aptr, const void* bptr, size_t size) { - const unsigned char* a = (const unsigned char*) aptr; - const unsigned char* b = (const unsigned char*) bptr; - for (size_t i = 0; i < size; i++) { - if (a[i] < b[i]) - return -1; - else if (b[i] < a[i]) - return 1; - } - return 0; -} \ No newline at end of file diff --git a/libs/libck/memcpy.c b/libs/libck/memcpy.c deleted file mode 100644 index 7e09be7..0000000 --- a/libs/libck/memcpy.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -void* memcpy(void* restrict dstptr, const void* restrict srcptr, size_t size) { - unsigned char* dst = (unsigned char*) dstptr; - const unsigned char* src = (const unsigned char*) srcptr; - for (size_t i=0; i < size; i++) - dst[i] = src[i]; - return dstptr; -} \ No newline at end of file diff --git a/libs/libck/memmove.c b/libs/libck/memmove.c deleted file mode 100644 index 98c787f..0000000 --- a/libs/libck/memmove.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -void* memmove(void* dstptr, const void* srcptr, size_t size) { - unsigned char* dst = (unsigned char*) dstptr; - const unsigned char* src = (const unsigned char*) srcptr; - if (dst < src) { - for (size_t i = 0; i < size; i++) - dst[i] = src[i]; - } else { - for (size_t i = size; i != 0; i--) - dst[i-1] = src[i-1]; - } - return dstptr; -} \ No newline at end of file diff --git a/libs/libck/memset.c b/libs/libck/memset.c deleted file mode 100644 index 67dadfe..0000000 --- a/libs/libck/memset.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -void* memset(void* bufptr, int value, size_t size) { - unsigned char* buf = (unsigned char*) bufptr; - for (size_t i = 0; i < size; i++) - buf[i] = (unsigned char) value; - return bufptr; -} \ No newline at end of file diff --git a/libs/libck/printf.c b/libs/libck/printf.c deleted file mode 100644 index 72a4749..0000000 --- a/libs/libck/printf.c +++ /dev/null @@ -1,191 +0,0 @@ -#include -#include -#include -#include -#include -#include - -static bool print(const char* data, size_t length) { - const unsigned char* bytes = (const unsigned char*) data; - for (size_t i = 0; i < length; i++) - if (putchar(bytes[i]) == EOF) - return false; - return true; -} - -// for %d and %x. -static const char* itoa(unsigned int num, char* str, unsigned int base) { - int cou = 0; // nt - - if (num == 0) { // 0 - str[cou] = '0'; - str[++cou] = '\0'; - return str; - } - - while (num) - { - unsigned int rem = (num % base); - // brings the number up to ascii 0 + the digit - if (rem > 9) { - str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 - } else { - str[cou] = rem + '0'; - } - num /= base; - ++cou; - } - - // reverse the string - for (int i = 0; i < cou; i++) { - char temp = str[i]; - str[i] = str[cou - i - 1]; - str[cou - 1] = temp; - } - - str[cou] = '\0'; - return str; -} - -// have no idea if there's a better way to do this. for %lx -static const char* itoa_u64(unsigned long long num, char* str, int base) { - int cou = 0; // nt - - if (num == 0) { // 0 - str[cou] = '0'; - str[++cou] = '\0'; - return str; - } - - while (num) - { - unsigned long long rem = num % base; - // brings the number up to ascii 0 + the digit - if (rem > 9) { - str[cou] = rem + '7'; // 7 = 55 + 10(A) = 65 - } else { - str[cou] = rem + '0'; - } - num /= base; - ++cou; - } - - // reverse the string - for (int i = 0; i < cou; i++) { - char temp = str[i]; - str[i] = str[cou - i - 1]; - str[cou - 1] = temp; - } - - str[cou] = '\0'; - return str; -} - -int printf(const char* restrict format, ...) { - va_list parameters; - va_start(parameters, format); - - int written = 0; - - while (*format != '\0') { - size_t maxrem = INT_MAX - written; - - if (format[0] != '%' || format[1] == '%') { - if (format[0] == '%') - format++; - size_t amount = 1; - while (format[amount] && format[amount] != '%') - amount++; - if (maxrem < amount) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(format, amount)) - return -1; - format += amount; - written += amount; - continue; - } - - const char* format_begun_at = format++; - - if (*format == 'c') { - format++; - char c = (char) va_arg(parameters, int /* char promotes to int */); - if (!maxrem) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(&c, sizeof(c))) - return -1; - written++; - } else if (*format == 'd') { - format++; - char str[20]; // probably a long enough buffer - unsigned int d = va_arg(parameters, unsigned int); - itoa(d, str, 10); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } else if (*format == 'x') { - format++; - char str[20]; // probably a long enough buffer - unsigned int d = va_arg(parameters, unsigned int); - itoa(d, str, 16); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } else if (*format == 'l') { // for %lx and others that start with l - format++; - if (*format == 'x') { - format++; - char str[20]; // probably a long enough buffer - unsigned long long d = va_arg(parameters, unsigned long long); - itoa_u64(d, str, 16); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } - } else if (*format == 's') { - format++; - const char* str = va_arg(parameters, const char*); - size_t len = strlen(str); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(str, len)) - return -1; - written += len; - } else { - format = format_begun_at; - size_t len = strlen(format); - if (maxrem < len) { - // TODO: Set errno to EOVERFLOW. - return -1; - } - if (!print(format, len)) - return -1; - written += len; - format += len; - } - } - - va_end(parameters); - return written; -} \ No newline at end of file diff --git a/libs/libck/putchar.c b/libs/libck/putchar.c deleted file mode 100644 index c5a7976..0000000 --- a/libs/libck/putchar.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -#include - -int putchar(int ic) { - char c = (char) ic; - terminal_write(&c, sizeof(c)); - return ic; -} \ No newline at end of file diff --git a/libs/libck/puts.c b/libs/libck/puts.c deleted file mode 100644 index 8c8c036..0000000 --- a/libs/libck/puts.c +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int puts(const char* string) { - return printf("%s\n", string); -} \ No newline at end of file diff --git a/libs/libck/strlen.c b/libs/libck/strlen.c deleted file mode 100644 index 441622a..0000000 --- a/libs/libck/strlen.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -size_t strlen(const char* str) { - size_t len = 0; - while (str[len]) { - ++len; - } - return len; -} \ No newline at end of file diff --git a/makefile b/makefile index c52dd4c..21c698c 100644 --- a/makefile +++ b/makefile @@ -7,30 +7,33 @@ export SYSROOT = $(PWD)/sysroot # Variables for easy access of tools like gcc and nasm export CC = $(ARCH)-elf-gcc export CXX = $(ARCH)-elf-g++ -export LD = $(ARCH)-elf-ld +export AR = $(ARCH)-elf-ar export NASM = nasm -QEMU = qemu-system-x86_64 +QEMU = qemu-system-x86_64 #ASMFLAGS = -felf32 #CXXFLAGS := -ffreestanding -O2 -Wall -Wextra -fno-exceptions -fno-rtti #LDFLAGS := -ffreestanding -O2 -nostdlib -.PHONY: all kernel.bin grub multiboot_test clean +.PHONY: all grub clean build-all -all: libck.a kernel.bin grub +all: build-all grub -libck.a: - $(info [INFO] Building libck) - $(MAKE) -C ./libs/libck/ ARCH=$(ARCH) PREFIX=$(PWD) +build-all: kernel/kernel.bin libc.a -kernel.bin: +libc/libc.a: install-headers + $(info [INFO] Building libc) + $(MAKE) -C ./libc/ ARCH=$(ARCH) PREFIX=$(PWD) + +kernel/kernel.bin: libc/libc.a install-headers $(info [INFO] Building kernel) $(MAKE) -C ./kernel/ ARCH=$(ARCH) PREFIX=$(PWD) -grub: kernel.bin grub.cfg - grub-file --is-x86-multiboot $< - cp kernel.bin isodir/boot - cp grub.cfg isodir/boot/grub - grub-mkrescue -o $(OS_NAME).iso isodir +grub: build-all grub.cfg + xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin \ + -no-emul-boot -boot-load-size 4 -boot-info-table \ + --efi-boot boot/limine/limine-uefi-cd.bin \ + -efi-boot-part --efi-boot-image --protective-msdos-label \ + isodir -o emuos.iso qemu: grub $(QEMU) -no-shutdown -no-reboot --serial stdio -s -m 512 -hda $(OS_NAME).iso @@ -39,12 +42,12 @@ install: install-headers install-libraries install-headers: $(MAKE) -C ./kernel/ install-headers - $(MAKE) -C ./libs/libck/ install-headers + $(MAKE) -C ./libs/libc/ install-headers install-libraries: - $(MAKE) -C ./libs/libck/ install-lib + $(MAKE) -C ./libs/libc/ install-lib clean: -@$(MAKE) -C ./kernel/ clean - -@$(MAKE) -C ./libs/libck/ clean + -@$(MAKE) -C ./libs/libc/ clean -@$(RM) $(wildcard *.bin *.a) -- cgit v1.2.3-70-g09d2