Merge pull request #30 from alkuzin/feature/add-some-auxilar-functions

Feature/add some auxilar functions
This commit is contained in:
Aren Elchinyan 2025-01-16 12:09:26 +03:00 committed by GitHub
commit e1e463d8d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 121 additions and 19 deletions

3
.gitignore vendored
View File

@ -57,3 +57,6 @@ dkms.conf
/isodir
serial.log
*.cpio
# Auxilar files
.vscode/

View File

@ -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();
}

View File

@ -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);

View File

@ -1,9 +1,75 @@
#ifndef __K_STDLIB
#define __K_STDLIB
#include <ktypes.h>
#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
#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 */

31
kernel/include/ktypes.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef __K_TYPES
#define __K_TYPES
#include <stdbool.h>
#include <stdint.h>
// 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 */

View File

@ -4,7 +4,8 @@
#include <stdint.h>
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__)

View File

@ -1,7 +1,7 @@
#include "khal.h"
#include <stdint.h>
#include "kstring.h"
#include "sys/panic.h"
#include <stdint.h>
int multiboot2_init(uint64_t *addr, uint32_t magic);

View File

@ -1,6 +1,6 @@
#include "khal.h"
#include <stdint.h>
#include <stdarg.h>
#include <stdint.h>
static char *itoa(uint64_t value, char *buf, uint8_t base) {
char *ptr = buf;

View File

@ -1,7 +1,7 @@
#include "khal.h"
#include <stdint.h>
#include "kstdlib.h"
#include <3rd/multiboot2.h>
#include <stdint.h>
#define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit)))

View File

@ -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);