Skip to content

Commit

Permalink
CM: Pre-checks added before process syscalls
Browse files Browse the repository at this point in the history
Library functions must validate user input before making a system call.
Kernel functions assumes valid input from user.

Also includes:
* New system call to get OS errors
* Process functions moved to cm/process.c & cm.h
* New functions to set library mode errors.
  • Loading branch information
coderarjob committed Nov 30, 2024
1 parent 4fad7ae commit 7c7c01c
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 53 deletions.
34 changes: 34 additions & 0 deletions include/cm/cm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

#include <types.h>
#include <stdarg.h>
#ifdef KERNEL
#include <cm/osif.h>
#include <cm/syscall.h>
#else
#include <osif.h>
#include <syscall.h>
#endif

#define INVALID_HANDLE (-1)
#define HALT() for (;;)
Expand All @@ -23,3 +30,30 @@ INT vsnprintf (CHAR* dest, size_t size, const CHAR* fmt, va_list l);
* @return Nothing
**************************************************************************************************/
void cm_delay (UINT ms);

/***************************************************************************************************
* Process management
***************************************************************************************************/
INT cm_thread_create (void (*startLocation)(), bool isKernelMode);
INT cm_process_create (void* startLocation, SIZE binaryLengthBytes, bool isKernelMode);
bool cm_process_is_yield_requested();

static inline void cm_process_yield()
{
syscall (OSIF_SYSCALL_YIELD_PROCESS, 0, 0, 0, 0, 0);
}

static inline void cm_process_kill (UINT code)
{
syscall (OSIF_SYSCALL_KILL_PROCESS, code, 0, 0, 0, 0);
}

static inline U32 cm_process_get_pid()
{
return (U32)syscall (OSIF_SYSCALL_PROCESS_GETPID, 0, 0, 0, 0, 0);
}

static inline void* cm_process_get_datamem_start()
{
return (void*)syscall (OSIF_SYSCALL_PROCESS_GET_DATAMEM_START, 0, 0, 0, 0, 0);
}
44 changes: 44 additions & 0 deletions include/cm/err.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* ---------------------------------------------------------------------------
* Megha Operating System V2 - C MOS App library - Library errors
* ---------------------------------------------------------------------------
*/

#include <stdint.h>
#ifdef KERNEL
#include <cm/debug.h>
#include <cm/syscall.h>
#include <cm/osif.h>
#else
#include <debug.h>
#include <syscall.h>
#include <osif.h>
#endif

#define CM_FAILURE (-1)

typedef enum CMErrors {
// OS Errors start from 0 and must end before the start of library errors
LIBRARY_ERRORS_START = 100,
CM_ERR_INVALID_INPUT = 100,
} CMErrors;

extern uint32_t cm_error_num__;

static inline uint32_t cm_get_lib_error()
{
return cm_error_num__;
}

static inline uint32_t cm_get_os_error()
{
return (uint32_t)syscall (OSIF_SYSCALL_GET_OS_ERROR, 0, 0, 0, 0, 0);
}

/* Can be used to store an error code and return from a function */
#define CM_RETURN_ERROR__(errno, rval) \
do { \
ERROR ("Error %x.", errno); \
cm_error_num__ = errno; \
return rval; \
} while (0)
1 change: 1 addition & 0 deletions include/cm/osif.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef enum OSIF_SYSCALLS {
OSIF_SYSCALL_WINDOW_DESTORY = 11,
OSIF_SYSCALL_WINDOW_GET_WINDOW_FB = 12,
OSIF_SYSCALL_WINDOW_FLUSH_GRAPHICS = 13,
OSIF_SYSCALL_GET_OS_ERROR = 14,
} OSIF_SYSCALLS;

typedef enum OSIF_ProcessEvents {
Expand Down
24 changes: 0 additions & 24 deletions include/cm/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,18 @@
#endif

S32 syscall (OSIF_SYSCALLS fn, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5);
INT cm_thread_create (void (*startLocation)(), bool isKernelMode);
INT cm_process_create (void* startLocation, SIZE binaryLengthBytes, bool isKernelMode);
UINT cm_get_tick_period_us();
bool cm_process_is_yield_requested();
bool cm_process_is_child_exited (UINT* exitCode);

static inline UINT cm_tickcount_to_microsec(UINT tick)
{
return ((tick)*cm_get_tick_period_us());
}

static inline void cm_process_yield()
{
syscall (OSIF_SYSCALL_YIELD_PROCESS, 0, 0, 0, 0, 0);
}

static inline void cm_process_kill(UINT code)
{
syscall (OSIF_SYSCALL_KILL_PROCESS, code, 0, 0, 0, 0);
}

static inline void cm_putstr (char* text)
{
syscall (OSIF_SYSCALL_CONSOLE_WRITELN, (PTR)text, 0, 0, 0, 0);
}

static inline U32 cm_process_get_pid()
{
return (U32)syscall (OSIF_SYSCALL_PROCESS_GETPID, 0, 0, 0, 0, 0);
}

static inline U32 cm_get_tickcount()
{
return (U32)syscall (OSIF_SYSCALL_TIMER_GET_TICKCOUNT, 0, 0, 0, 0, 0);
Expand All @@ -63,11 +44,6 @@ static inline U32 cm_window_destory (Handle h)
return (U32)syscall (OSIF_SYSCALL_WINDOW_DESTORY, (U32)h, 0, 0, 0, 0);
}

static inline void* cm_process_get_datamem_start()
{
return (void*)syscall (OSIF_SYSCALL_PROCESS_GET_DATAMEM_START, 0, 0, 0, 0, 0);
}

static inline void* cm_window_flush_graphics()
{
return (void*)syscall (OSIF_SYSCALL_WINDOW_FLUSH_GRAPHICS, 0, 0, 0, 0, 0);
Expand Down
1 change: 1 addition & 0 deletions src/cm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ compile_lib(
# C library to talk to the OS and helpful/common functions for use by application programs.
# ------------------------------------------------------------------------
set(APPLIB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/process.c
${CMAKE_CURRENT_SOURCE_DIR}/syscalls.c
${CMAKE_CURRENT_SOURCE_DIR}/printf.c
${CMAKE_CURRENT_SOURCE_DIR}/debug.c
Expand Down
3 changes: 3 additions & 0 deletions src/cm/cm.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <cm/syscall.h>
#include <cm/debug.h>

/* Variable to store Library error*/
uint32_t cm_error_num__;

#define cm_MICRODEC_TO_TICK_COUNT(us) ((us) / cm_get_tick_period_us())

void event_handler_NDU_()
Expand Down
53 changes: 53 additions & 0 deletions src/cm/process.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* -------------------------------------------------------------------------------------------------
* Megha Operating System V2 - C MOS App Library - Process management
* -------------------------------------------------------------------------------------------------
*/

#include <process.h>
#include <cm/syscall.h>
#include <cm/err.h>

INT cm_process_create (void* startLocation, SIZE binaryLengthBytes, bool isKernelMode)
{
if (!startLocation || binaryLengthBytes == 0) {
CM_RETURN_ERROR__ (CM_ERR_INVALID_INPUT, CM_FAILURE);
}

KProcessFlags flags = PROCESS_FLAGS_NONE;
if (isKernelMode) {
flags |= PROCESS_FLAGS_KERNEL_PROCESS;
}

INT pid = syscall (OSIF_SYSCALL_CREATE_PROCESS, (U32)startLocation, binaryLengthBytes,
(U32)flags, 0, 0);
if (pid < 0) {
CM_RETURN_ERROR__ (cm_get_os_error(), CM_FAILURE);
}
return pid;
}

INT cm_thread_create (void (*startLocation)(), bool isKernelMode)
{
if (!startLocation) {
CM_RETURN_ERROR__ (CM_ERR_INVALID_INPUT, CM_FAILURE);
}

KProcessFlags flags = PROCESS_FLAGS_THREAD;
if (isKernelMode) {
flags |= PROCESS_FLAGS_KERNEL_PROCESS;
}

INT pid = syscall (OSIF_SYSCALL_CREATE_PROCESS, (U32)startLocation, 0, (U32)flags, 0, 0);
if (pid < 0) {
CM_RETURN_ERROR__ (cm_get_os_error(), CM_FAILURE);
}
return pid;
}

bool cm_process_is_yield_requested()
{
volatile OSIF_ProcessEvent e = { 0 };
cm_process_pop_event ((OSIF_ProcessEvent*)&e);
return (e.event == OSIF_PROCESS_EVENT_PROCCESS_YIELD_REQ);
}
29 changes: 0 additions & 29 deletions src/cm/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include <cm/syscall.h>
#include <config.h>
#include <kernel.h>
#include <process.h>

S32 syscall (OSIF_SYSCALLS fn, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5)
{
Expand All @@ -30,33 +28,6 @@ S32 syscall (OSIF_SYSCALLS fn, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5)
return retval;
}

INT cm_process_create (void* startLocation, SIZE binaryLengthBytes, bool isKernelMode)
{
KProcessFlags flags = PROCESS_FLAGS_NONE;
if (isKernelMode) {
flags |= PROCESS_FLAGS_KERNEL_PROCESS;
}

return syscall (OSIF_SYSCALL_CREATE_PROCESS, (U32)startLocation, binaryLengthBytes, (U32)flags,
0, 0);
}

INT cm_thread_create (void (*startLocation)(), bool isKernelMode)
{
KProcessFlags flags = PROCESS_FLAGS_THREAD;
if (isKernelMode) {
flags |= PROCESS_FLAGS_KERNEL_PROCESS;
}
return syscall (OSIF_SYSCALL_CREATE_PROCESS, (U32)startLocation, 0, (U32)flags, 0, 0);
}

bool cm_process_is_yield_requested()
{
volatile OSIF_ProcessEvent e = { 0 };
cm_process_pop_event ((OSIF_ProcessEvent*)&e);
return (e.event == OSIF_PROCESS_EVENT_PROCCESS_YIELD_REQ);
}

UINT cm_get_tick_period_us()
{
return CONFIG_TICK_PERIOD_MICROSEC;
Expand Down
8 changes: 8 additions & 0 deletions src/kernel/x86/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ bool ksys_processPopEvent (SystemcallFrame frame, OSIF_ProcessEvent* const e);
U32 ksys_process_getPID (SystemcallFrame frame);
U32 ksys_get_tickcount (SystemcallFrame frame);
PTR ksys_process_getDataMemoryStart (SystemcallFrame frame);
U32 sys_get_os_error (SystemcallFrame frame);

#ifdef GRAPHICS_MODE_ENABLED
Handle ksys_window_createWindow (SystemcallFrame frame, const char* winTitle);
Expand All @@ -57,25 +58,30 @@ 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_process_getDataMemoryStart, // 9
//---------------------------
#ifdef GRAPHICS_MODE_ENABLED
&ksys_window_createWindow, // 10
&ksys_window_destoryWindow, // 11
Expand All @@ -87,6 +93,8 @@ void* g_syscall_table[] = {
&s_handleInvalidSystemCall, // 12
&s_handleInvalidSystemCall, // 13
#endif
//---------------------------
&sys_get_os_error, // 14
};
#pragma GCC diagnostic pop

Expand Down

0 comments on commit 7c7c01c

Please sign in to comment.