Skip to content

Commit

Permalink
Types in app.h now act as adapters to kernel types
Browse files Browse the repository at this point in the history
Functions of the library would be responsible to convert between the
kernel and user types. Types exposed to the applications may not now
directly correspond to the types in the Kernel.
  • Loading branch information
coderarjob committed Oct 21, 2024
1 parent 04c0290 commit d072894
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
31 changes: 5 additions & 26 deletions include/applib/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,15 @@

#include <types.h>

// =============================================================================
// Kernel-User space interfaces (structures and emums)
//
// These will be same/similar to the Kernel types, defined in the Kernel headers, but can contain
// some changes to them suitable for user space.
// =============================================================================

// Represents the 'ProcessEvent' struct defined in 'process.h', but with some changes.
typedef struct IProcessEvent {
typedef struct ProcessEvent {
UINT event;
UINT data;
} IProcessEvent;

// Represents the 'ProcessFlags' struct defined in 'process.h', but with some changes.
typedef enum IProcessFlags {
IPROCESS_FLAGS_NONE = 0,
IPROCESS_FLAGS_KERNEL_PROCESS = (1 << 0),
IPROCESS_FLAGS_THREAD = (1 << 1),
} IProcessFlags;
} ProcessEvent;

// Represents the 'KernelEvents' struct defined in 'kernel.h', but with some changes.
typedef enum IAppEvents {
typedef enum AppEvents {
APP_EVENT_NONE = 0,
APP_EVENT_TICK = 1,
APP_EVENT_PROCCESS_YIELD_REQ = 2
} IAppEvents;
} AppEvents;
// =============================================================================

typedef enum SYSCALLS {
Expand All @@ -52,6 +35,7 @@ S32 syscall (SYSCALLS fn, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5);
INT sys_thread_create (void (*startLocation)(), bool isKernelMode);
INT sys_process_create (void* startLocation, SIZE binaryLengthBytes, bool isKernelMode);
UINT os_tick_microseconds();
bool sys_process_pop_event (U32 pid, ProcessEvent* e);

static inline void sys_yield()
{
Expand All @@ -73,11 +57,6 @@ static inline U32 sys_process_get_pid()
return (U32)syscall (SYSCALL_PROCESS_GETPID, 0, 0, 0, 0, 0);
}

static inline bool sys_process_pop_event (U32 pid, IProcessEvent* e)
{
return syscall (SYSCALL_POP_PROCESS_EVENT, pid, (PTR)e, 0, 0, 0);
}

static inline U32 sys_get_tickcount()
{
return (U32)syscall (SYSCALL_TIMER_GET_TICKCOUNT, 0, 0, 0, 0, 0);
Expand Down
19 changes: 15 additions & 4 deletions src/apps/applib/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <applib/app.h>
#include <config.h>
#include <kernel.h>
#include <process.h>

S32 syscall (SYSCALLS fn, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5)
{
Expand All @@ -31,9 +32,9 @@ S32 syscall (SYSCALLS fn, U32 arg1, U32 arg2, U32 arg3, U32 arg4, U32 arg5)

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

return syscall (SYSCALL_CREATE_PROCESS, (U32)startLocation, binaryLengthBytes, (U32)flags, 0,
Expand All @@ -42,9 +43,9 @@ INT sys_process_create (void* startLocation, SIZE binaryLengthBytes, bool isKern

INT sys_thread_create (void (*startLocation)(), bool isKernelMode)
{
IProcessFlags flags = IPROCESS_FLAGS_THREAD;
KProcessFlags flags = PROCESS_FLAGS_THREAD;
if (isKernelMode) {
flags |= IPROCESS_FLAGS_KERNEL_PROCESS;
flags |= PROCESS_FLAGS_KERNEL_PROCESS;
}
return syscall (SYSCALL_CREATE_PROCESS, (U32)startLocation, 0, (U32)flags, 0, 0);
}
Expand All @@ -55,6 +56,16 @@ UINT os_tick_microseconds()
return KERNEL_TICK_COUNT_TO_MICROSEC (tick);
}

bool sys_process_pop_event (U32 pid, ProcessEvent* e)
{
KProcessEvent evnt = { 0 };
if (syscall (SYSCALL_POP_PROCESS_EVENT, pid, (PTR)&evnt, 0, 0, 0)) {
e->data = evnt.data;
e->event = evnt.event;
}
return false;
}

// This is the entry point for all processes.
__attribute__ ((section (".entry.text")))
void proc_start()
Expand Down
2 changes: 1 addition & 1 deletion src/apps/mpdemo.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static void thread1();
static bool shouldYield()
{
UINT pid = sys_process_get_pid();
IProcessEvent e = {0};
ProcessEvent e = {0};
sys_process_pop_event (pid, &e);
return (e.event == APP_EVENT_PROCCESS_YIELD_REQ);
}
Expand Down

0 comments on commit d072894

Please sign in to comment.