Skip to content

Commit

Permalink
Now kearly_printf only defined for DEBUG builds
Browse files Browse the repository at this point in the history
* kdebu_* & INFO, ERROR, ... etc macros defined only for DEBUG &
  PORT_E9_ENABLED builds.
* Multiple minor updates to fix 'unused variable' build errors.
* kdebug.c file is now arch independent.
* syscall updates
  • Loading branch information
coderarjob committed Nov 16, 2024
1 parent 269bfe6 commit 13240ed
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 35 deletions.
15 changes: 0 additions & 15 deletions include/disp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,4 @@ void kdisp_init ();
**************************************************************************************************/
void kdisp_putc (CHAR c);

INT kearly_printf (const CHAR *fmt, ...);
INT kearly_snprintf (CHAR *dest, size_t size, const CHAR *fmt, ...);
INT kearly_vsnprintf (CHAR *dest, size_t size, const CHAR *fmt, va_list l);

/***************************************************************************************************
* Moves to the next line and prints formatted input on the screen.
*
* @return For description for `kearly_vsnprintf`.
**************************************************************************************************/
#define kearly_println(...) kearly_printf ("\n" __VA_ARGS__)

/* Message are printed on screen and if configured also on the debug console. */
void kdisp_importantPrint(char *fmt, ...);
void kdisp_show_call_trace();

#endif // DISPTEXT_H
28 changes: 23 additions & 5 deletions include/kdebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <types.h>
#include <buildcheck.h>
#include <disp.h>
#include <stdarg.h>

typedef enum KernelDebugLogType {
KDEBUG_LOG_TYPE_INFO,
Expand All @@ -19,13 +19,15 @@ typedef enum KernelDebugLogType {
/* Prints formatted string to 0xE9 port and can optionally print to vga
* buffer.
*/
#if defined(DEBUG) && defined(PORT_E9_ENABLED)
void kdebug_printf_ndu (const CHAR* fmt, ...);
void kdebug_log_ndu (KernelDebugLogType type, const char* func, UINT line, char* fmt, ...);

#if defined(PORT_E9_ENABLED)
void kdebug_printf_ndu (const CHAR* fmt, ...);
void kdebug_log_ndu (KernelDebugLogType type, const char* func, UINT line, char* fmt, ...);

#define kdebug_printf(...) kdebug_printf_ndu (__VA_ARGS__)

#define INFO(...) kdebug_log_ndu (KDEBUG_LOG_TYPE_INFO, __func__, __LINE__, __VA_ARGS__)

#define FUNC_ENTRY(...) \
kdebug_log_ndu (KDEBUG_LOG_TYPE_FUNC, __func__, __LINE__, "" __VA_ARGS__)
#define ERROR(...) kdebug_log_ndu (KDEBUG_LOG_TYPE_ERROR, __func__, __LINE__, __VA_ARGS__)
Expand All @@ -36,7 +38,23 @@ void kdebug_log_ndu (KernelDebugLogType type, const char* func, UINT line, char*
#define FUNC_ENTRY(...) (void)0
#define ERROR(...) (void)0
#define WARN(...) (void)0
#endif // DEBUG && PORT_E9_ENABLED
#endif

#if defined(DEBUG)
INT kearly_printf (const CHAR* fmt, ...);
#else
#define kearly_printf(...) (void)0
#endif // DEBUG

INT kearly_snprintf (CHAR* dest, size_t size, const CHAR* fmt, ...);
INT kearly_vsnprintf (CHAR* dest, size_t size, const CHAR* fmt, va_list l);

/***************************************************************************************************
* Moves to the next line and prints formatted input on the screen.
*
* @return For description for `kearly_vsnprintf`.
**************************************************************************************************/
#define kearly_println(...) kearly_printf ("\n" __VA_ARGS__)

/***************************************************************************************************
* Moves to the next line and prints formatted input on the screen and E9 port.
Expand Down
7 changes: 7 additions & 0 deletions src/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ set(KERNEL_SOURCES
LIST(APPEND KERNEL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/compositor.c)
endif()

if (MOS_BUILD_MODE STREQUAL "DEBUG")
LIST(APPEND KERNEL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/printk.c)
if (MOS_PORT_E9_ENABLED)
LIST(APPEND KERNEL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/kdebug.c)
endif()
endif()

compile_lib(
NAME kernel
SOURCES ${KERNEL_SOURCES}
Expand Down
3 changes: 3 additions & 0 deletions src/kernel/drivers/x86/pc/ps2kb.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ INTERRUPT_HANDLER (kb_interrupt)
void kb_interrupt_handler (InterruptFrame* frame)
{
(void)frame;

#if defined(DEBUG)
U8 scancode = (U8)ps2_no_wait_read (PS2_DATA_PORT);
kearly_println ("Keyboard ISR: Scancode: %x", scancode);
#endif
pic_send_eoi (PIC_IRQ_KEYBOARD);
}
2 changes: 1 addition & 1 deletion src/kernel/x86/kdebug.c → src/kernel/kdebug.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* ---------------------------------------------------------------------------
* Megha Operating System V2 - x86 Kernel - Debug console
* Megha Operating System V2 - Cross Platform Kernel - Debug console
*
* Uses the port 0xE9 hack to output characters to the linux Qemu console.
* ---------------------------------------------------------------------------
Expand Down
21 changes: 12 additions & 9 deletions src/kernel/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@
* Dated: 5th September 2021
*/

#include <disp.h>
#include <types.h>
#include <moslimits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <utils.h>
#include <kernel.h>
#include <kdebug.h>

typedef enum IntTypes
{
INTEGER, LONGINT, LONGLONGINT, ADDRESS
} IntTypes;

static void s_prnstr (const CHAR *str);
static void s_itoa (CHAR **dest, S64 *size, U64 num, U32 base);
static void s_itoa (CHAR** dest, S64* size, U64 num, U32 base);
static bool s_convert (CHAR **dest, S64 *size, IntTypes inttype, CHAR c, va_list *l);
static IntTypes s_readtype (const CHAR **fmt, CHAR *c);
static U64 s_readint (IntTypes inttype, va_list *l);

#if defined(DEBUG)
static void s_prnstr (const CHAR *str);

static void s_prnstr (const CHAR *str)
{
for (;*str;str++)
kdisp_putc (*str);
}

/***************************************************************************************************
* Prints on screen the arguments in the format specified.
*
Expand All @@ -55,6 +63,7 @@ INT kearly_printf (const CHAR *fmt, ...)
s_prnstr (buffer);
return len;
}
#endif

/***************************************************************************************************
* Writes to a string pointer the arguments in the format specified.
Expand Down Expand Up @@ -266,9 +275,3 @@ static void s_itoa (CHAR **dest, S64 *size, U64 num, U32 base)
(*size)--;
}
}

static void s_prnstr (const CHAR *str)
{
for (;*str;str++)
kdisp_putc (*str);
}
1 change: 0 additions & 1 deletion src/kernel/x86/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ set(KERNEL_X86_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/gdt.c
${CMAKE_CURRENT_SOURCE_DIR}/idt.c
${CMAKE_CURRENT_SOURCE_DIR}/interrupts.c
${CMAKE_CURRENT_SOURCE_DIR}/kdebug.c
${CMAKE_CURRENT_SOURCE_DIR}/kernel.c
${CMAKE_CURRENT_SOURCE_DIR}/paging.c
${CMAKE_CURRENT_SOURCE_DIR}/pmm.c
Expand Down
25 changes: 21 additions & 4 deletions src/kernel/x86/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ typedef struct SystemcallFrame {
U32 ss;
} __attribute__ ((packed)) SystemcallFrame;

#if defined(DEBUG)
void ksys_console_writeln (SystemcallFrame frame, char* fmt, char* text);
#if !defined(GRAPHICS_MODE_ENABLED)
void ksys_console_setcolor (SystemcallFrame frame, U8 bg, U8 fg);
void ksys_console_setposition (SystemcallFrame frame, U8 row, U8 col);
#endif // !GRAPHICS_MODE_ENABLED
#endif // DEBUG
INT ksys_createProcess (SystemcallFrame frame, void* processStartAddress, SIZE binLengthBytes,
KProcessFlags flags);
void ksys_yieldProcess (SystemcallFrame frame, U32 ebx, U32 ecx, U32 edx, U32 esi, U32 edi);
void ksys_killProcess (SystemcallFrame frame, UINT exitCode);
void ksys_console_setcolor (SystemcallFrame frame, U8 bg, U8 fg);
void ksys_console_setposition (SystemcallFrame frame, U8 row, U8 col);
bool ksys_processPopEvent (SystemcallFrame frame, OSIF_ProcessEvent* const e);
U32 ksys_process_getPID (SystemcallFrame frame);
U32 ksys_get_tickcount (SystemcallFrame frame);
Expand All @@ -54,20 +58,29 @@ static INT s_handleInvalidSystemCall();
#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#pragma GCC diagnostic ignored "-Wpedantic"
void* g_syscall_table[] = {
#if defined(DEBUG)
&ksys_console_writeln, // 0
#else
&s_handleInvalidSystemCall, // 0
#endif
&ksys_createProcess, // 1
&ksys_yieldProcess, // 2
&ksys_killProcess, // 3
#if defined(DEBUG) && !defined(GRAPHICS_MODE_ENABLED)
&ksys_console_setcolor, // 4
&ksys_console_setposition, // 5
#else
&s_handleInvalidSystemCall, // 4
&s_handleInvalidSystemCall, // 5
#endif
&ksys_processPopEvent, // 6
&ksys_process_getPID, // 7
&ksys_get_tickcount, // 8
&ksys_get_tickcount, // 8
&ksys_process_getDataMemoryStart, // 9
#ifdef GRAPHICS_MODE_ENABLED
&ksys_window_createWindow, // 10
&ksys_window_destoryWindow, // 11
&ksys_getWindowFB, // 12
&ksys_getWindowFB, // 12
&ksys_window_graphics_flush_all, // 13
#else
&s_handleInvalidSystemCall, // 10
Expand Down Expand Up @@ -179,13 +192,15 @@ static INT s_handleInvalidSystemCall()
}
#pragma GCC diagnostic pop

#if defined(DEBUG)
void ksys_console_writeln (SystemcallFrame frame, char* fmt, char* text)
{
FUNC_ENTRY ("Frame return address: %x:%px, fmt: %px text: %px", frame.cs, frame.eip, fmt, text);
(void)frame;
kearly_printf (fmt, text);
}

#if !defined(GRAPHICS_MODE_ENABLED)
void ksys_console_setcolor (SystemcallFrame frame, U8 bg, U8 fg)
{
FUNC_ENTRY ("Frame return address: %x:%px, fg: %x bg: %x", frame.cs, frame.eip, fg, bg);
Expand All @@ -199,6 +214,8 @@ void ksys_console_setposition (SystemcallFrame frame, U8 row, U8 col)
(void)frame;
kdisp_ioctl (DISP_SETCOORDS, row, col);
}
#endif // !GRAPHICS_MODE_ENABLED
#endif // DEBUG

INT ksys_createProcess (SystemcallFrame frame, void* processStartAddress, SIZE binLengthBytes,
KProcessFlags flags)
Expand Down

0 comments on commit 13240ed

Please sign in to comment.