From bc3e8323bd0cd94acf35fc89b30fe66dc8d72696 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 26 Feb 2021 18:04:39 +0100 Subject: [PATCH] Add syscall.c|h|asm in kernel/ke (2) --- include/kernel/ke/syscall.h | 40 +++++++++++++++++++++++++++++ kaleid/kernel/ke/syscall.asm | 50 ++++++++++++++++++++++++++++++++++++ kaleid/kernel/ke/syscall.c | 43 +++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 include/kernel/ke/syscall.h create mode 100644 kaleid/kernel/ke/syscall.asm create mode 100644 kaleid/kernel/ke/syscall.c diff --git a/include/kernel/ke/syscall.h b/include/kernel/ke/syscall.h new file mode 100644 index 0000000..551e877 --- /dev/null +++ b/include/kernel/ke/syscall.h @@ -0,0 +1,40 @@ +//----------------------------------------------------------------------------// +// OS on Kaleid // +// // +// Desc: System call & userspace related functions // +// // +// // +// Copyright © 2018-2021 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + +#ifndef _KERNEL_H +#include +#endif + +#ifndef _KALKERN_SYSCALL_H +#define _KALKERN_SYSCALL_H + +//----------------------------------------------------------------------------// + +extern void KeJumpToUserspace(ulong args, void *entryPoint, void *stackAddr); + +void KeEnableSystemCalls(); + +//----------------------------------------------------------------------------// + +#endif diff --git a/kaleid/kernel/ke/syscall.asm b/kaleid/kernel/ke/syscall.asm new file mode 100644 index 0000000..b22ab79 --- /dev/null +++ b/kaleid/kernel/ke/syscall.asm @@ -0,0 +1,50 @@ +;=----------------------------------------------------------------------------=; +; OS on Kaleid ; +; ; +; Desc: System call & userspace related functions ; +; ; +; ; +; Copyright © 2018-2021 The OS/K Team ; +; ; +; This file is part of OS/K. ; +; ; +; OS/K is free software: you can redistribute it and/or modify ; +; it under the terms of the GNU General Public License as published by ; +; the Free Software Foundation, either version 3 of the License, or ; +; (at your option) any later version. ; +; ; +; OS/K is distributed in the hope that it will be useful, ; +; but WITHOUT ANY WARRANTY; without even the implied warranty of ; +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; +; GNU General Public License for more details. ; +; ; +; You should have received a copy of the GNU General Public License ; +; along with OS/K. If not, see . ; +;=----------------------------------------------------------------------------=; + +[BITS 64] + +%include "kaleid/kernel/ke/cpuf.inc" + +global KeJumpToUserspace + +KeJumpToUserspace: + + # rdi = user args + # rsi = entry point in user space + # rdx = user space stack + + mov rax, 0x1b ; Selector 0x18 (User Data) + RPL 3 + mov ds, ax + mov es, ax + + # Build a fake iret frame + push rax # Selector 0x18 (User Data) + RPL 3 + push rdx # User space stack + push 0x202 # rflags = interrupt enable + reserved bit + push 0x23 # Selector 0x20 (User Code) + RPL 3 + push rsi # Entry point in user space + + xor rax, rax + + iretq diff --git a/kaleid/kernel/ke/syscall.c b/kaleid/kernel/ke/syscall.c new file mode 100644 index 0000000..2a4174a --- /dev/null +++ b/kaleid/kernel/ke/syscall.c @@ -0,0 +1,43 @@ +//----------------------------------------------------------------------------// +// OS on Kaleid // +// // +// Desc: System call & userspace related functions // +// // +// // +// Copyright © 2018-2021 The OS/K Team // +// // +// This file is part of OS/K. // +// // +// OS/K is free software: you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation, either version 3 of the License, or // +// any later version. // +// // +// OS/K is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY//without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with OS/K. If not, see . // +//----------------------------------------------------------------------------// + +#include +#include +#include +#include + +static uchar SyscallHandler(ISRFrame_t *regs) +{ + DebugLog("Got a system call code %d !\n", regs->rdi); + + return EOK; +} + +void KeEnableSystemCalls() +{ + ulong flags = KePauseIRQs(); + KeRegisterISR(SyscallHandler, 0x80); + KeRestoreIRQs(flags); + KeEnableNMI(); +}