summaryrefslogtreecommitdiffhomepage
path: root/dev
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-08-31 20:33:57 +0200
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-08-31 20:33:57 +0200
commit21a0081ac9d9a8abe66cc6a248b5363768de1dc9 (patch)
treea96f53d3eff8ec4e7e44cfa4610f38c664a1fefe /dev
parenta8eacc4a5d7f89b497b8be552491dba26fa68162 (diff)
[IMP] Fixed many stuff in user-mode switch, working on finishing it and
adding timer interrupts. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev')
-rw-r--r--dev/ZBA/amd64-efi.make2
-rw-r--r--dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx4
-rw-r--r--dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm70
-rw-r--r--dev/ZKA/HALKit/AMD64/HalKernelMain.cxx86
-rw-r--r--dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm27
-rw-r--r--dev/ZKA/HALKit/AMD64/Processor.hxx16
-rw-r--r--dev/ZKA/Sources/DLLMain.cxx4
-rw-r--r--dev/ZKA/Sources/UserProcessScheduler.cxx2
8 files changed, 144 insertions, 67 deletions
diff --git a/dev/ZBA/amd64-efi.make b/dev/ZBA/amd64-efi.make
index 733c8474..6eac7584 100644
--- a/dev/ZBA/amd64-efi.make
+++ b/dev/ZBA/amd64-efi.make
@@ -36,7 +36,7 @@ EMU_FLAGS=-net none -m 12G -M q35 \
file=fat:rw:Sources/Root/,index=2,format=raw \
-drive id=disk_2,file=$(IMG_2),if=none \
-device ahci,id=ahci \
- -device ide-hd,drive=disk_2,bus=ahci.0 -d int
+ -device ide-hd,drive=disk_2,bus=ahci.0 -d int -no-reboot
LD_FLAGS=-e Main --subsystem=10
diff --git a/dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx b/dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx
index f022b5e5..9d9b546b 100644
--- a/dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalCoreMPScheduler.cxx
@@ -174,8 +174,6 @@ namespace Kernel::HAL
/***********************************************************************************/
Void mp_get_cores(VoidPtr vendor_ptr) noexcept
{
- kSMPAware = false;
-
if (!vendor_ptr)
return;
@@ -184,6 +182,8 @@ namespace Kernel::HAL
kMADTBlock = reinterpret_cast<MADT_TABLE*>(kRawMADT);
+ kSMPAware = false;
+
if (kMADTBlock)
{
SizeT index = 0;
diff --git a/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm b/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm
index fb5e9b19..847c609e 100644
--- a/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm
+++ b/dev/ZKA/HALKit/AMD64/HalInterruptAPI.asm
@@ -14,18 +14,16 @@
%define kInterruptId 0x21
%macro IntExp 1
-global __NEW_INT_%1
-__NEW_INT_%1:
+global __ZKA_INT_%1
+__ZKA_INT_%1:
cld
-
iretq
%endmacro
%macro IntNormal 1
-global __NEW_INT_%1
-__NEW_INT_%1:
+global __ZKA_INT_%1
+__ZKA_INT_%1:
cld
-
iretq
%endmacro
@@ -52,7 +50,7 @@ IntNormal 4
IntNormal 5
;; Invalid opcode interrupt
-__NEW_INT_6:
+__ZKA_INT_6:
cli
push rax
@@ -73,7 +71,7 @@ IntExp 11
IntExp 12
-__NEW_INT_13:
+__ZKA_INT_13:
cli
push rax
@@ -86,7 +84,7 @@ __NEW_INT_13:
sti
iretq
-__NEW_INT_14:
+__ZKA_INT_14:
cli
push rax
@@ -144,7 +142,7 @@ IntNormal 49
[extern hal_system_call_enter]
[extern hal_Kernel_call_enter]
-__NEW_INT_50:
+__ZKA_INT_50:
cli
push rcx
@@ -160,7 +158,7 @@ __NEW_INT_50:
sti
iretq
-__NEW_INT_51:
+__ZKA_INT_51:
cli
push rcx
@@ -194,7 +192,7 @@ GRAN_4K equ 1 << 7
SZ_32 equ 1 << 6
LONG_MODE equ 1 << 5
-__NEW_INT_52:
+__ZKA_INT_52:
cli
jmp hal_on_ap_startup
sti
@@ -235,17 +233,63 @@ rt_reload_segments:
ret
global hal_load_idt
+global hal_user_code_start
hal_load_idt:
lidt [rcx]
sti
ret
+[global hal_switch_to_user_code]
+
+hal_switch_to_user_code:
+ ; Enable SCE that enables sysret and syscall
+ mov rcx, 0xc0000082
+ wrmsr
+ mov rcx, 0xc0000080
+ rdmsr
+ or eax, 1
+ wrmsr
+ mov rcx, 0xc0000081
+ rdmsr
+ mov edx, 0x00180008
+ wrmsr
+
+ mov rbx, 0x28
+ mov ds, rbx
+
+ mov rbx, 0x28
+ mov fs, rbx
+
+ mov rbx, 0x28
+ mov gs, rbx
+
+ mov rbx, 0x28
+ mov es, rbx
+
+ mov rsp, [hal_user_code_stack_end]
+ mov rcx, hal_user_code_start
+
+ mov r11, 0x0202
+
+ o64 sysret
+
+hal_user_code_start:
+L0:
+ nop
+ jmp $
+
+section .bss
+
+hal_user_code_stack:
+ resb 4096*4
+hal_user_code_stack_end:
+
section .data
kInterruptVectorTable:
%assign i 0
%rep 256
- dq __NEW_INT_%+i
+ dq __ZKA_INT_%+i
%assign i i+1
%endrep
diff --git a/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx b/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx
index 31a47f00..da77a13c 100644
--- a/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx
+++ b/dev/ZKA/HALKit/AMD64/HalKernelMain.cxx
@@ -34,6 +34,12 @@ struct HEAP_ALLOC_INFO final
Kernel::Size fTheSz;
};
+struct CREATE_THREAD_INFO final
+{
+ Kernel::MainKind fMain;
+ Kernel::Char fName[kPefNameLen];
+};
+
struct PROCESS_BLOCK_INFO final
{
THREAD_INFORMATION_BLOCK* fTIB;
@@ -56,16 +62,18 @@ namespace Kernel::HAL
} // namespace Kernel::HAL
/* GDT. */
-STATIC Kernel::HAL::Detail::NewOSGDT cGdt = {
- {0, 0, 0, 0x00, 0x00, 0}, // null entry
- {0, 0, 0, 0x9a, 0xaf, 0}, // Kernel code
- {0, 0, 0, 0x92, 0xaf, 0}, // Kernel data
- {0, 0, 0, 0x00, 0x00, 0}, // null entry
- {0, 0, 0, 0x9a, 0xaf, 0}, // user code
- {0, 0, 0, 0x92, 0xaf, 0}, // user data
+STATIC Kernel::HAL::Detail::ZKA_GDT cGdt = {
+ {0, 0, 0, 0x00, 0x00, 0}, // Null entry
+ {0, 0, 0, 0x9A, 0xA0, 0}, // Kernel code
+ {0, 0, 0, 0x92, 0xA0, 0}, // Kernel data
+ {0, 0, 0, 0x00, 0x00, 0}, // Null entry
+ {0, 0, 0, 0xFA, 0xA0, 0}, // User code
+ {0, 0, 0, 0xF2, 0xA0, 0}, // User data
};
Kernel::Void hal_real_init(Kernel::Void) noexcept;
+
+EXTERN_C void hal_user_code_start(void);
EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void);
EXTERN_C void hal_init_platform(
@@ -106,7 +114,7 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
Kernel::HAL::RegisterGDT gdtBase;
gdtBase.Base = reinterpret_cast<Kernel::UIntPtr>(&cGdt);
- gdtBase.Limit = sizeof(Kernel::HAL::Detail::NewOSGDT) - 1;
+ gdtBase.Limit = sizeof(Kernel::HAL::Detail::ZKA_GDT) - 1;
CONST Kernel::HAL::GDTLoader cGDT;
cGDT.Load(gdtBase);
@@ -122,23 +130,24 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
// Register the basic system calls.
- constexpr auto cTlsInterrupt = 0x11;
- constexpr auto cTlsInstallInterrupt = 0x12;
- constexpr auto cNewInterrupt = 0x13;
- constexpr auto cDeleteInterrupt = 0x14;
- constexpr auto cExitInterrupt = 0x15;
- constexpr auto cLastExitInterrupt = 0x16;
- constexpr auto cCatalogOpen = 0x17;
- constexpr auto cForkRead = 0x18;
- constexpr auto cForkWrite = 0x19;
- constexpr auto cCatalogClose = 0x20;
- constexpr auto cCatalogRemove = 0x21;
- constexpr auto cCatalogCreate = 0x22;
- constexpr auto cRebootInterrupt = 0x23;
- constexpr auto cShutdownInterrupt = 0x24;
- constexpr auto cLPCSendMsg = 0x25;
- constexpr auto cLPCOpenMsg = 0x26;
- constexpr auto cLPCCloseMsg = 0x27;
+ constexpr auto cTlsInterrupt = 0x11;
+ constexpr auto cTlsInstallInterrupt = 0x12;
+ constexpr auto cNewInterrupt = 0x13;
+ constexpr auto cDeleteInterrupt = 0x14;
+ constexpr auto cExitInterrupt = 0x15;
+ constexpr auto cLastExitInterrupt = 0x16;
+ constexpr auto cCatalogOpen = 0x17;
+ constexpr auto cForkRead = 0x18;
+ constexpr auto cForkWrite = 0x19;
+ constexpr auto cCatalogClose = 0x20;
+ constexpr auto cCatalogRemove = 0x21;
+ constexpr auto cCatalogCreate = 0x22;
+ constexpr auto cRebootInterrupt = 0x23;
+ constexpr auto cShutdownInterrupt = 0x24;
+ constexpr auto cLPCSendMsg = 0x25;
+ constexpr auto cLPCOpenMsg = 0x26;
+ constexpr auto cLPCCloseMsg = 0x27;
+ constexpr auto cCreateThreadInterrupt = 0x28;
kSyscalls[cTlsInterrupt].fProc = [](Kernel::VoidPtr rdx) -> void {
if (tls_check_syscall_impl(rdx) == false)
@@ -179,6 +188,16 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
rt_install_tib(rdxPb->fTIB, rdxPb->fGIB);
};
+ kSyscalls[cCreateThreadInterrupt].fProc = [](Kernel::VoidPtr rdx) -> void {
+ CREATE_THREAD_INFO* rdxPb = reinterpret_cast<CREATE_THREAD_INFO*>(rdx);
+
+ if (!rdxPb)
+ return;
+
+ // install the fTIB and fGIB.
+ Kernel::sched_execute_thread(rdxPb->fMain, rdxPb->fName);
+ };
+
kSyscalls[cExitInterrupt].fProc = [](Kernel::VoidPtr rdx) -> void {
PROCESS_EXIT_INFO* rdxEi = reinterpret_cast<PROCESS_EXIT_INFO*>(rdx);
@@ -208,14 +227,15 @@ Kernel::Void hal_real_init(Kernel::Void) noexcept
pow.Shutdown();
};
- kSyscalls[cTlsInterrupt].fHooked = true;
- kSyscalls[cTlsInstallInterrupt].fHooked = true;
- kSyscalls[cDeleteInterrupt].fHooked = true;
- kSyscalls[cNewInterrupt].fHooked = true;
- kSyscalls[cExitInterrupt].fHooked = true;
- kSyscalls[cLastExitInterrupt].fHooked = true;
- kSyscalls[cShutdownInterrupt].fHooked = true;
- kSyscalls[cRebootInterrupt].fHooked = true;
+ kSyscalls[cTlsInterrupt].fHooked = true;
+ kSyscalls[cTlsInstallInterrupt].fHooked = true;
+ kSyscalls[cDeleteInterrupt].fHooked = true;
+ kSyscalls[cNewInterrupt].fHooked = true;
+ kSyscalls[cExitInterrupt].fHooked = true;
+ kSyscalls[cLastExitInterrupt].fHooked = true;
+ kSyscalls[cShutdownInterrupt].fHooked = true;
+ kSyscalls[cRebootInterrupt].fHooked = true;
+ kSyscalls[cCreateThreadInterrupt].fHooked = true;
if (kHandoverHeader->f_MultiProcessingEnabled)
Kernel::HAL::mp_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr);
diff --git a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm
index 98dbfa02..f20ffcee 100644
--- a/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm
+++ b/dev/ZKA/HALKit/AMD64/HalMPContextSwitch.asm
@@ -22,7 +22,7 @@ section .text
mp_do_context_switch:
mov r11, rdx
mov r12, rcx
-
+
; Enable SCE that enables sysret and syscall
mov rcx, 0xc0000082
wrmsr
@@ -35,17 +35,26 @@ mp_do_context_switch:
mov edx, 0x00180008
wrmsr
- mov rcx, r11
- mov rdx, r12
- mov r11, 0x202
+ mov rbx, 0x28
+ mov ds, rbx
+
+ mov rbx, 0x28
+ mov fs, rbx
+
+ mov rbx, 0x28
+ mov gs, rbx
+
+ mov rbx, 0x28
+ mov es, rbx
+
+ ;; Swap registers, since it's the other way around.
+
+ mov rcx, r12 ;; code ptr
+ mov rsp, [r11] ;; stack ptr
+ mov r11, 0x0202
;; rcx and rdx already set.
o64 sysret
- ret
-
-mp_do_context_switch_fail:
- jmp $
-
;; @brief Gets the current stack frame.
mp_get_current_context:
diff --git a/dev/ZKA/HALKit/AMD64/Processor.hxx b/dev/ZKA/HALKit/AMD64/Processor.hxx
index 94490e4e..68bcbec9 100644
--- a/dev/ZKA/HALKit/AMD64/Processor.hxx
+++ b/dev/ZKA/HALKit/AMD64/Processor.hxx
@@ -263,7 +263,7 @@ namespace Kernel::HAL
@brief Global descriptor table entry, either null, code or data.
*/
- struct PACKED NewOSGDTRecord final
+ struct PACKED ZKA_GDT_ENTRY final
{
UInt16 fLimit0;
UInt16 fBase0;
@@ -273,14 +273,14 @@ namespace Kernel::HAL
UInt8 fBase2;
};
- struct PACKED ALIGN(0x1000) NewOSGDT final
+ struct PACKED ALIGN(0x1000) ZKA_GDT final
{
- NewOSGDTRecord fNull;
- NewOSGDTRecord fKernCode;
- NewOSGDTRecord fKernData;
- NewOSGDTRecord fUserNull;
- NewOSGDTRecord fUserCode;
- NewOSGDTRecord fUserData;
+ ZKA_GDT_ENTRY fNull;
+ ZKA_GDT_ENTRY fKernCode;
+ ZKA_GDT_ENTRY fKernData;
+ ZKA_GDT_ENTRY fUserNull;
+ ZKA_GDT_ENTRY fUserCode;
+ ZKA_GDT_ENTRY fUserData;
};
} // namespace Detail
diff --git a/dev/ZKA/Sources/DLLMain.cxx b/dev/ZKA/Sources/DLLMain.cxx
index e8436411..e4352a00 100644
--- a/dev/ZKA/Sources/DLLMain.cxx
+++ b/dev/ZKA/Sources/DLLMain.cxx
@@ -39,6 +39,8 @@ EXTERN Kernel::Property cKernelVersion;
STATIC CG::UI_WINDOW_STRUCT* cKernelWnd = nullptr;
+EXTERN_C void hal_switch_to_user_code(void);
+
namespace Kernel::Detail
{
/// @brief Filesystem auto formatter, additional checks are also done by the class.
@@ -213,6 +215,8 @@ EXTERN_C Kernel::Void ke_dll_entrypoint(Kernel::Void)
CG::CGDrawStringToWnd(cKernelWnd, kSysDrv, 20, 10 + (FONT_SIZE_X * Kernel::rt_string_len("newoskrnl.dll: Missing catalog: ")), RGB(0, 0, 0));
}
+ hal_switch_to_user_code();
+
while (Yes)
{
Kernel::ProcessHelper::StartScheduling();
diff --git a/dev/ZKA/Sources/UserProcessScheduler.cxx b/dev/ZKA/Sources/UserProcessScheduler.cxx
index b762f1cd..2d018b33 100644
--- a/dev/ZKA/Sources/UserProcessScheduler.cxx
+++ b/dev/ZKA/Sources/UserProcessScheduler.cxx
@@ -341,7 +341,7 @@ namespace Kernel
kcout << process.Name << ": will be runned.\r";
// tell helper to find a core to schedule on.
- if (!ProcessHelper::Switch(process.Image, process.StackReserve, process.StackFrame,
+ if (!ProcessHelper::Switch(process.Image, &process.StackReserve[process.StackSize - 1], process.StackFrame,
process.ProcessId))
{
process.Crash();