diff --git a/.gitignore b/.gitignore index 7447d04..1dc9406 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,6 @@ dkms.conf /isodir serial.log *.cpio + +# Auxilar files +.vscode/ \ No newline at end of file diff --git a/kernel/arch/amd64/cpu/control.c b/kernel/arch/amd64/cpu/control.c index 41d8a73..634d88a 100644 --- a/kernel/arch/amd64/cpu/control.c +++ b/kernel/arch/amd64/cpu/control.c @@ -64,14 +64,14 @@ void cpu_interrupt_lock_force_release() { } void cpu_halt() { - asm volatile ("hlt"); + asm volatile("hlt"); } void cpu_full_halt() { - if (unlikely(!reflock_is_locked(&lock))) - reflock_acquire(&lock); - else - __cpu_disable_interrupts(); - lock.allow_force_unlock = false; - cpu_halt(); + if (unlikely(!reflock_is_locked(&lock))) + reflock_acquire(&lock); + else + __cpu_disable_interrupts(); + lock.allow_force_unlock = false; + cpu_halt(); } diff --git a/kernel/include/kreflock.h b/kernel/include/kreflock.h index 5da4ce0..ed3bcc6 100644 --- a/kernel/include/kreflock.h +++ b/kernel/include/kreflock.h @@ -34,11 +34,11 @@ typedef struct { } reflock_t; #define NEW_REFLOCK(_on_lock, _on_unlock, _strict, _allow_force) \ - { \ - _on_lock, _on_unlock, _strict, _allow_force, { \ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \ - } \ - } + { _on_lock, \ + _on_unlock, \ + _strict, \ + _allow_force, \ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } void reflock_make(reflock_t *lock); bool reflock_validate_magic(reflock_t *lock); diff --git a/kernel/include/kstdlib.h b/kernel/include/kstdlib.h index d1f270b..72b28ce 100644 --- a/kernel/include/kstdlib.h +++ b/kernel/include/kstdlib.h @@ -1,9 +1,75 @@ #ifndef __K_STDLIB #define __K_STDLIB +#include + #define unlikely(x) __builtin_expect(x, 0) #define likely(x) __builtin_expect(x, 1) +/** @brief Ignore unused variable/static function.*/ #define IGNORE_UNUSED(x) ((void)x) -#endif \ No newline at end of file +#define BITS_PER_BYTE 8 + +/** + * @brief Convert bits to bytes. + * + * @param [in] n - given number of bits. + * @return number of bytes needed for containing n bits. + */ +static inline usize bits_to_bytes(usize n) { + // assuming that a byte contains 8 bits + return (n + 7) >> 3; +} + +/** + * @brief Convert bytes to bits. + * + * @param [in] n - given number of bytes. + * @return number of bits in n bytes. + */ +static inline usize bytes_to_bits(usize n) { + // assuming that a byte contains 8 bits + return n << 3; +} + +/** + * @brief Get number of bits in value. + * + * @param [in] x - given value to count. + * @return number of bits in value. + */ +#define BITS_PER_TYPE(x) (bytes_to_bits(sizeof(x))) + +/** + * @brief Set the specific bit of given value. + * + * @param [out] value - given value to change. + * @param [in] pos - given bit position to set. + */ +static inline void set_bit(u32 *value, i32 pos) { + *value |= (1U << pos); +} + +/** + * @brief Clear the specific bit of given value. + * + * @param [out] value - given value to change. + * @param [in] pos - given bit position to clear. + */ +static inline void clear_bit(u32 *value, i32 pos) { + *value &= ~(1U << pos); +} + +/** + * @brief Get the specific bit of given value. + * + * @param [in] value - given value to test. + * @param [in] pos - given bit position to test. + * @return true if the bit is set, false otherwise. + */ +static inline bool test_bit(u32 value, i32 pos) { + return (value & (1U << pos)) != 0; +} + +#endif /* __K_STDLIB */ \ No newline at end of file diff --git a/kernel/include/ktypes.h b/kernel/include/ktypes.h new file mode 100644 index 0000000..2cc486c --- /dev/null +++ b/kernel/include/ktypes.h @@ -0,0 +1,31 @@ +#ifndef __K_TYPES +#define __K_TYPES + +#include +#include + +// unsigned types +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +// signed types +typedef int64_t i64; +typedef int32_t i32; +typedef int16_t i16; +typedef int8_t i8; + +// floating point types +typedef double f64; +typedef float f32; + +// other +typedef i64 ssize; +typedef u64 usize; + +// memory manager +typedef u32 phys_addr_t; +typedef u32 virt_addr_t; + +#endif /* __K_TYPES */ \ No newline at end of file diff --git a/kernel/include/sys/panic.h b/kernel/include/sys/panic.h index 29cda38..68cae99 100644 --- a/kernel/include/sys/panic.h +++ b/kernel/include/sys/panic.h @@ -4,7 +4,8 @@ #include void __attribute__((noreturn)) panic_int(uint8_t int_no, const char *msg); -void __attribute__((noreturn)) panic(const char *msg, const char *func, const int line); +void __attribute__((noreturn)) panic(const char *msg, const char *func, + const int line); #define PANIC(msg) panic(msg, __func__, __LINE__) diff --git a/kernel/kernel.c b/kernel/kernel.c index b85d800..d8349e1 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,7 +1,7 @@ #include "khal.h" -#include #include "kstring.h" #include "sys/panic.h" +#include int multiboot2_init(uint64_t *addr, uint32_t magic); diff --git a/kernel/klibc/printf.c b/kernel/klibc/printf.c index f49160c..d12376a 100644 --- a/kernel/klibc/printf.c +++ b/kernel/klibc/printf.c @@ -1,6 +1,6 @@ #include "khal.h" -#include #include +#include static char *itoa(uint64_t value, char *buf, uint8_t base) { char *ptr = buf; diff --git a/kernel/multiboot2.c b/kernel/multiboot2.c index 9a50fd0..48ebf3a 100644 --- a/kernel/multiboot2.c +++ b/kernel/multiboot2.c @@ -1,7 +1,7 @@ #include "khal.h" -#include #include "kstdlib.h" #include <3rd/multiboot2.h> +#include #define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit))) diff --git a/kernel/sys/panic.c b/kernel/sys/panic.c index 768d87c..04ccc16 100644 --- a/kernel/sys/panic.c +++ b/kernel/sys/panic.c @@ -7,7 +7,7 @@ void __attribute__((noreturn)) panic_int(uint8_t int_no, const char *msg) { cpu_interrupt_lock_acquire(); - + serial_printf("\n\rKernel panic (in interrupt) [INT=0x%02X] - %s\n", int_no, msg); cpu_state_print(); @@ -15,7 +15,8 @@ void __attribute__((noreturn)) panic_int(uint8_t int_no, const char *msg) { for (;;) cpu_halt(); } -void __attribute__((noreturn)) panic(const char *msg, const char *func, const int line) { +void __attribute__((noreturn)) panic(const char *msg, const char *func, + const int line) { cpu_interrupt_lock_acquire(); serial_printf("\n\rKernel panic - %s in %s:%d\n", msg, func, line);