-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
208 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ | |
void Device::CPU::Halt() | ||
{ | ||
for (;;) | ||
asm("hlt"); | ||
asm volatile("hlt"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef _SYSCALLHANDLER_H | ||
#define _SYSCALLHANDLER_H | ||
|
||
#include <nk/singleton.h> | ||
#include <nk/vector.h> | ||
#include <sys/syscall.h> | ||
#include <kernel/interrupts.h> | ||
|
||
namespace Kernel | ||
{ | ||
typedef uint32_t (*syscall_t)(void *); | ||
|
||
class SyscallHandler : public InterruptHandler | ||
{ | ||
DECLARE_SINGLETON(SyscallHandler) | ||
private: | ||
nk::Vector<syscall_t> syscalls; | ||
|
||
public: | ||
void Register(); | ||
|
||
private: | ||
void HandleInterrupt(unsigned int interrupt, RegisterStates *regs) override; | ||
|
||
void AddSyscall(uint32_t number, syscall_t call); | ||
}; | ||
|
||
}; // namespace Kernel | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <kernel/syscallhandler.h> | ||
#include <kernel/stack.h> | ||
#include <tasks/thread.h> | ||
|
||
namespace Kernel | ||
{ | ||
DEFINE_SINGLETON(SyscallHandler) | ||
|
||
uint32_t syscall_print(void *param) | ||
{ | ||
printf("%s", param); | ||
return 0; | ||
} | ||
|
||
uint32_t syscall_texit(void *param) | ||
{ | ||
uint32_t exit_code = *(uint32_t *)(param); | ||
printf("UserThread %d exited with exit code %d\n", Thread::current->id, exit_code); | ||
Thread::current->Kill(); | ||
return 0; | ||
} | ||
|
||
SyscallHandler::SyscallHandler() | ||
{ | ||
} | ||
|
||
void SyscallHandler::Register() | ||
{ | ||
AddSyscall(SYS_TEXIT, syscall_texit); | ||
AddSyscall(SYS_PRINT, syscall_print); | ||
Interrupts::AddHandler(0x80, this); | ||
} | ||
|
||
void SyscallHandler::HandleInterrupt(unsigned int, RegisterStates *regs) | ||
{ | ||
// When the interrupt is called, ESP is saved to the regs state. | ||
// At that point however, it is already the kernel's ESP0. The | ||
// user-mode stack pointer was pushed to the stack by the CPU. | ||
// The value at ESP+12 therefore gets us the address of where | ||
// the user thread's stack is located. There, the syscall params | ||
// are stored. | ||
auto user_stack_ptr = *(uint32_t *)(regs->esp + 12); | ||
|
||
// For easy access to the struct, we create ourselves a "virtual" | ||
// stack | ||
Stack stack((void *)user_stack_ptr); | ||
stack.Pop(); // The return pointer - we don't need that | ||
uint32_t syscall_num = stack.Pop(); // Syscall number | ||
void *param_ptr = (void *)stack.Pop(); // Pointer to the param struct | ||
uint32_t *retval = (uint32_t *)stack.GetStackPtr(); // Stack now points at the retval ptr | ||
|
||
auto syscall_ptr = syscalls.At(syscall_num); | ||
if (syscall_ptr == nullptr) | ||
{ | ||
*retval = -1; | ||
printf("warn: Thread %d attempted invalid syscall %x\n", Thread::current->id, syscall_num); | ||
return; | ||
} | ||
else | ||
{ | ||
*retval = syscall_ptr(param_ptr); | ||
} | ||
} | ||
|
||
void SyscallHandler::AddSyscall(uint32_t number, syscall_t call) | ||
{ | ||
syscalls.Reserve(number); | ||
syscalls.At(number) = call; | ||
} | ||
|
||
} // namespace Kernel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef _SYS_SYSCALL_H | ||
#define _SYS_SYSCALL_H | ||
|
||
#include <stdint.h> | ||
|
||
#define SYS_INVAL 0 | ||
#define SYS_TEXIT 1 | ||
#define SYS_PRINT 2 | ||
|
||
#ifdef __cplusplus | ||
extern "C" | ||
{ | ||
#endif | ||
|
||
typedef struct syscall_invoke | ||
{ | ||
uint32_t number; | ||
const void *params; | ||
uint32_t retval; | ||
} syscall_invoke_t; | ||
|
||
uint32_t syscall(uint32_t number, const void *params); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
Oops, something went wrong.