Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ dkms.conf
/isodir
serial.log
*.cpio

# Auxilar files
.vscode/
14 changes: 7 additions & 7 deletions kernel/arch/amd64/cpu/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
10 changes: 5 additions & 5 deletions kernel/include/kreflock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
68 changes: 67 additions & 1 deletion kernel/include/kstdlib.h
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions kernel/include/ktypes.h
Original file line number Diff line number Diff line change
@@ -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 */
3 changes: 2 additions & 1 deletion kernel/include/sys/panic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
2 changes: 1 addition & 1 deletion kernel/kernel.c
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
2 changes: 1 addition & 1 deletion kernel/klibc/printf.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion kernel/multiboot2.c
Original file line number Diff line number Diff line change
@@ -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)))

Expand Down
5 changes: 3 additions & 2 deletions kernel/sys/panic.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

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

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);
Expand Down
Loading