mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
The printk(), panic() and kernel.h update
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#ifndef _CTYPE_H
|
||||
#define _CTYPE_H
|
||||
|
||||
#define _DIGIT_MASK (1<<0)
|
||||
#define _ALPHA_MASK (1<<1)
|
||||
#define _LOWER_MASK (1<<2)
|
||||
#define _PUNCT_MASK (1<<3)
|
||||
#define _SPACE_MASK (1<<4)
|
||||
#define _CNTRL_MASK (1<<5)
|
||||
#define _PRINT_MASK (1<<6)
|
||||
#define _HEXDG_MASK (1<<7)
|
||||
|
||||
// A table for the ASCII character set
|
||||
extern const unsigned char _CTYPE_TABLE[256];
|
||||
|
||||
// Just a macro to make this easier
|
||||
#define _CTYPE_CHECK(_c, _mask) (_CTYPE_TABLE[(unsigned char)(_c)] & (_mask))
|
||||
|
||||
#define isalnum(_c) (isalpha((_c)) || isdigit((_c))
|
||||
#define isalpha(_c) _CTYPE_CHECK((_c), _ALPHA_MASK)
|
||||
#define islower(_c) _CTYPE_CHECK((_c), _LOWER_MASK)
|
||||
#define isupper(_c) (!islower((_c)) && isalpha((_c)))
|
||||
#define isdigit(_c) _CTYPE_CHECK((_c), _DIGIT_MASK)
|
||||
#define iscntrl(_c) _CTYPE_CHECK((_c), _CNTRL_MASK)
|
||||
#define isgraph(_c) (!isblank((_c)))
|
||||
#define isspace(_c) _CTYPE_CHECK((_c), _SPACE_MASK)
|
||||
#define isblank(_c) (iscntrl((_c)) || isspace((_c)))
|
||||
#define isprint(_c) _CTYPE_CHECK((_c), _PRINT_MASK)
|
||||
#define ispunct(_c) _CTYPE_CHECK((_c), _PUNCT_MASK)
|
||||
// Why is this one's name longer :((
|
||||
#define isxdigit(_c) _CTYPE_CHECK((_c), _HEXDG_MASK);
|
||||
|
||||
#endif
|
||||
+62
-114
@@ -1,137 +1,85 @@
|
||||
#ifndef _KERNEL_H
|
||||
#define _KERNEL_H
|
||||
|
||||
// Null definition
|
||||
#define NULL ((void*)0)
|
||||
#include <stdarg.h>
|
||||
#include <types.h>
|
||||
|
||||
/*
|
||||
* Virtual memory addresses
|
||||
*/
|
||||
#define USER_HEAP 0x10000000
|
||||
#define USER_SHARED 0xA0000000
|
||||
#define USER_STACK 0xBF000000
|
||||
#define KERNEL_BASE 0xC0000000
|
||||
#define KERNEL_SIZE 0x10000000
|
||||
#define KERNEL_HEAP_BASE 0xD0000000
|
||||
#define KERNEL_HEAP_SIZE 0x10000000
|
||||
#define KERNEL_SHARED 0xF0000000
|
||||
#define KERNEL_FRAMEBUF 0xC8000000
|
||||
|
||||
/*
|
||||
* Variable arguments
|
||||
*/
|
||||
//typedef struct { unsigned int gp_off; unsigned int fp_off; void *arg_overflow; void *reg_save; } va_list;
|
||||
typedef __builtin_va_list va_list;
|
||||
#define va_start(_ap, _last) __builtin_va_start(_ap, _last)
|
||||
#define va_end(_ap) __builtin_va_end(_ap)
|
||||
#define va_arg(_ap, _type) __builtin_va_arg(_ap, _type)
|
||||
|
||||
/*
|
||||
* Utility macros
|
||||
*/
|
||||
|
||||
// Assertions
|
||||
#define STATIC_ASSERT(_cond, _msg)\
|
||||
typedef int static_assert_##_msg[(_cond) ? 1 : -1]
|
||||
|
||||
// Array
|
||||
#define ARRAY_SIZE(_a) (sizeof((_a)) / sizeof((_a)[0]))
|
||||
#define array_sizeof ARRAY_SIZE
|
||||
#define container_of(_p, _t, _m) \
|
||||
((_t*)((char*)(_p) - offsetof(_t, _m)))
|
||||
|
||||
// Align
|
||||
#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1)
|
||||
#define ALIGN_DOWN(x, a) ((x) & ~((typeof(x))(a) - 1))
|
||||
#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
|
||||
#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
|
||||
#define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
|
||||
|
||||
// Bit
|
||||
#define BIT(_n) (1UL << (_n))
|
||||
#define BIT_MASK(_n) (BIT(_n) - 1)
|
||||
|
||||
// Math
|
||||
#define swap(_a, _b) ({ typeof(_a) _tmp = (_a); (_a) = (_b); (_b) = (_a); })
|
||||
#define min(_x, _y) ({ typeof(_x) __x = (_x); typeof(_y) __y = (_y); __x < __y ? __x : __y; })
|
||||
#define max(_x, _y) ({ typeof(_x) __x = (_x); typeof(_y) __y = (_y); __x > __y ? __x : __y; })
|
||||
#define ceil(_x, _y) ((((_x) + (_y)) (_y)) * (_y))
|
||||
#define floor(_x, _y) ((_x) - ((_x) % (_y)))
|
||||
#define dceil(_x, _y) (((_x) + (_y) - 1) / (_y))
|
||||
|
||||
// Misc
|
||||
#define fence() __asm__ volatile ("":::"memory")
|
||||
|
||||
/*
|
||||
* Attribute definitions
|
||||
*/
|
||||
#define __align(_b) __attribute__((aligned(_b)))
|
||||
#define __alias(symbol) __attribute__((alias(#symbol)))
|
||||
#define __align(x) __attribute__((aligned(x)))
|
||||
#define __printf(a, b) __attribute__((format(printf, a, b)))
|
||||
#define __scanf(a, b) __attribute__((format(scanf, a, b)))
|
||||
#define __align_largest __attribute__((aligned))
|
||||
#define __depricated __attribute__((depricated))
|
||||
#define __packed __attribute__((packed))
|
||||
#define __noreturn __attribute__((noreturn))
|
||||
#define __const __attribute__((const))
|
||||
#define __pure __attribute__((pure))
|
||||
#define __inline __attribute__((always_inline))
|
||||
#define __no_inline __attribute__((never_inline))
|
||||
#define __section(_s) __attribute__((section(_s)))
|
||||
#define __cold __attribute__((cold))
|
||||
#define __hot __attribute__((hot))
|
||||
#define __naked __attribute__((naked))
|
||||
#define __always_inline __attribute__((always_inline))
|
||||
#define noinline __attribute__((never_inline))
|
||||
|
||||
/*
|
||||
* Standard int types
|
||||
*/
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long uint64_t;
|
||||
typedef signed char int8_t;
|
||||
typedef signed short int16_t;
|
||||
typedef signed int int32_t;
|
||||
typedef signed long int64_t;
|
||||
#define ALIGN(_addr, _align) (((_addr) + (_align) - 1) & ~((_align) - 1))
|
||||
#define ALIGN_DOWN(_addr, _align) ((_addr) & ~((_align) - 1))
|
||||
#define BIT(_n) (1ULL << (_n))
|
||||
|
||||
typedef uint64_t size_t;
|
||||
typedef int64_t ssize_t;
|
||||
#define min(_a, _b) ((_a) < (_b) ? (_a) : (_b))
|
||||
#define max(_a, _b) ((_a) > (_b) ? (_a) : (_b))
|
||||
|
||||
typedef uint64_t uintptr_t;
|
||||
typedef int64_t intptr_t;
|
||||
#define fence() __asm__ volatile ("" ::: "memory")
|
||||
|
||||
#define KERN_SOH "\001"
|
||||
#define KERN_SOH_ASCII '\001'
|
||||
#define KERN_EMERG KERN_SOH "0"
|
||||
#define KERN_ALERT KERN_SOH "1"
|
||||
#define KERN_CRIT KERN_SOH "2"
|
||||
#define KERN_ERROR KERN_SOH "3"
|
||||
#define KERN_WARNING KERN_SOH "4"
|
||||
#define KERN_NOTICE KERN_SOH "5"
|
||||
#define KERN_INFO KERN_SOH "6"
|
||||
#define KERN_DEBUG KERN_SOH "7"
|
||||
#define KERN_DEFAULT ""
|
||||
|
||||
typedef uint64_t offset_t;
|
||||
#define LOGLEVEL_EMERG 0
|
||||
#define LOGLEVEL_ALERT 1
|
||||
#define LOGLEVEL_CRIT 2
|
||||
#define LOGLEVEL_ERROR 3
|
||||
#define LOGLEVEL_WARNING 4
|
||||
#define LOGLEVEL_NOTICE 5
|
||||
#define LOGLEVEL_INFO 6
|
||||
#define LOGLEVEL_DEBUG 7
|
||||
|
||||
/*
|
||||
* Boolean
|
||||
*/
|
||||
#define bool _Bool
|
||||
#define true 1
|
||||
#define false 0
|
||||
#ifndef LOGLEVEL_DEFAULT
|
||||
#define LOGLEVEL_DEFAULT LOGLEVEL_ERROR
|
||||
#endif
|
||||
|
||||
/*
|
||||
* System types
|
||||
*/
|
||||
typedef uint32_t pid_t;
|
||||
#ifndef CONSOLE_LOGLEVEL
|
||||
#define CONSOLE_LOGLEVEL 4
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Linker defined values
|
||||
*/
|
||||
extern uint64_t __kernel_start, __kernel_text_start, __kernel_rodata_start, __kernel_data_start;
|
||||
extern uint64_t __kernel_end, __kernel_text_end, __kernel_rodata_end, __kernel_data_end;
|
||||
extern uint8_t console_loglevel;
|
||||
|
||||
/*
|
||||
* Logging and errors
|
||||
* TODO: Use something other than debugf()
|
||||
*/
|
||||
#include <serial.h>
|
||||
#define printk debugf
|
||||
#define pr_dbg(_s) printk("[debug] %s\n", (_s))
|
||||
#define pr_wrn(_s) printk("[warning] %s\n", (_s))
|
||||
#define pr_err(_s) printk("[error] %s\n", (_s))
|
||||
#define panic(_s) do {\
|
||||
printk("[kernel] Panic! %s", (_s));\
|
||||
while (1) __asm__ volatile ("cli; hlt");\
|
||||
} while (0)
|
||||
#define BUG() panic("BUG()")
|
||||
#define WARN() pr_wrn("WARN()")
|
||||
#define BUG_ON(_e) ((_e) ? BUG() : NULL)
|
||||
#define WARN_ON(_e) ((_e) ? WARN() : NULL)
|
||||
// TODO: Add a ring buffer for the logs
|
||||
int printk(const char* fmt, ...);
|
||||
int vprintk(const char* fmt, va_list ap);
|
||||
|
||||
#define pr_emerg(fmt, ...) printk(KERN_EMERG fmt, ##__VA_ARGS__)
|
||||
#define pr_alert(fmt, ...) printk(KERN_ALERT fmt, ##__VA_ARGS__)
|
||||
#define pr_crit(fmt, ...) printk(KERN_CRIT fmt, ##__VA_ARGS__)
|
||||
#define pr_error(fmt, ...) printk(KERN_ERROR fmt, ##__VA_ARGS__)
|
||||
#define pr_warning(fmt, ...) printk(KERN_WARNING fmt, ##__VA_ARGS__)
|
||||
#define pr_notice(fmt, ...) printk(KERN_NOTICE fmt, ##__VA_ARGS__)
|
||||
#define pr_info(fmt, ...) printk(KERN_INFO fmt, ##__VA_ARGS__)
|
||||
#define pr_debug(fmt, ...) printk(KERN_DEBUG fmt, ##__VA_ARGS__)
|
||||
|
||||
#define PANIC_MSG_BUFSZ 1024
|
||||
|
||||
void panic(const char* fmt, ...) __noreturn __cold;
|
||||
void vpanic(const char* fmt, va_list ap)__noreturn __cold;
|
||||
|
||||
extern uintptr_t __kernel_start, __kernel_text_start, __kernel_rodata_start, __kernel_data_start;
|
||||
extern uintptr_t __kernel_end, __kernel_text_end, __kernel_rodata_end, __kernel_data_end;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
#define TO_VIRT(_ptr) ((void*)((uintptr_t)(_ptr) + bootloader.hhdm_offset))
|
||||
#define TO_PHYS(_ptr) ((void*)((uintptr_t)(_ptr) - bootloader.hhdm_offset))
|
||||
|
||||
#define PAGE_SIZE 0x1000
|
||||
#define PAGE_SIZE 4096
|
||||
#define PAGE_SIZE_LARGE 0x200000
|
||||
#define PAGE_SIZE_HUGE 0x40000000
|
||||
|
||||
|
||||
@@ -16,11 +16,11 @@ int serial_txem(int dev);
|
||||
void serial_send(int dev, char data);
|
||||
int serial_rcvd(int dev);
|
||||
char serial_recv(int dev);
|
||||
void serial_write(int dev, char* buf, size_t len);
|
||||
void serial_puts(int dev, char* s);
|
||||
void serial_write(int dev, const char* buf, size_t len);
|
||||
void serial_puts(int dev, const char* s);
|
||||
void serial_cls(int dev);
|
||||
|
||||
int debugf(char* fmt, ...);
|
||||
int vdebugf(char* fmt, va_list args);
|
||||
int debugf(const char* fmt, ...);
|
||||
int vdebugf(const char* fmt, va_list args);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef _STDARG_H
|
||||
#define _STDARG_H
|
||||
|
||||
typedef __builtin_va_list va_list;
|
||||
#define va_start(_ap, _last) __builtin_va_start(_ap, _last)
|
||||
#define va_end(_ap) __builtin_va_end(_ap)
|
||||
#define va_arg(_ap, _type) __builtin_va_arg(_ap, _type)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef _TYPES_H
|
||||
#define _TYPES_H
|
||||
|
||||
// NULL pointer
|
||||
#define NULL ((void*)0)
|
||||
|
||||
/*
|
||||
* Integral types
|
||||
*/
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long uint64_t;
|
||||
typedef signed char int8_t;
|
||||
typedef signed short int16_t;
|
||||
typedef signed int int32_t;
|
||||
typedef signed long int64_t;
|
||||
_Static_assert(sizeof(int8_t) == 1, "int8_t not 8 bits.");
|
||||
_Static_assert(sizeof(int16_t) == 2, "int16_t not 16 bits.");
|
||||
_Static_assert(sizeof(int32_t) == 4, "int32_t not 32 bits.");
|
||||
_Static_assert(sizeof(int64_t) == 8, "int64_t not 64 bits.");
|
||||
_Static_assert(sizeof(uint8_t) == 1, "uint8_t not 8 bits.");
|
||||
_Static_assert(sizeof(uint16_t) == 2, "uint16_t not 16 bits.");
|
||||
_Static_assert(sizeof(uint32_t) == 4, "uint32_t not 32 bits.");
|
||||
_Static_assert(sizeof(uint64_t) == 8, "uint64_t not 64 bits.");
|
||||
|
||||
// TODO: Add a static assertion here
|
||||
typedef uint64_t size_t;
|
||||
typedef int64_t ssize_t;
|
||||
typedef size_t offset_t;
|
||||
|
||||
typedef uint64_t uintptr_t;
|
||||
typedef int64_t intptr_t;
|
||||
_Static_assert(sizeof(intptr_t) == sizeof(void*), "intptr_t must match pointer size.");
|
||||
_Static_assert(sizeof(uintptr_t) == sizeof(void*), "uintptr_t must match pointer size.");
|
||||
|
||||
/*
|
||||
* Boolean
|
||||
*/
|
||||
#define bool _Bool
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
#endif
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <kernel.h>
|
||||
|
||||
int snprintf(char *buf, size_t n, char *fmt, ...);
|
||||
int vsnprintf(char* buf, size_t n, char* fmt, va_list ap);
|
||||
int snprintf(char *buf, size_t n, const char *fmt, ...);
|
||||
int vsnprintf(char* buf, size_t n, const char* fmt, va_list ap);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user