mirror of
https://github.com/danbulant/Cosmos
synced 2026-05-21 21:38:52 +00:00
Working context switching but far from stable. Changes needed to be made to the internals of cosmos to include mutex's in vital components (Heap, Thread Manager, ect). Context switching currently only switches the general registers and does not switch the MMX context, however this will be added in the future. Also when a thread currently dies it takes the whole system down, patches to the IDT handlers needed to be made to prevent system lockup in the future
72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using IL2CPU.API.Attribs;
|
|
|
|
namespace Cosmos.Core.Processing
|
|
{
|
|
public static unsafe class ProcessorScheduler
|
|
{
|
|
public static void Initialize()
|
|
{
|
|
var context = new ProcessContext.Context();
|
|
context.type = ProcessContext.Context_Type.PROCESS;
|
|
context.tid = ProcessContext.m_NextCID++;
|
|
//context.name = "Boot";
|
|
context.esp = 0;
|
|
context.stacktop = 0;
|
|
context.cr3 = 0;
|
|
context.state = ProcessContext.Thread_State.ALIVE;
|
|
context.old_state = ProcessContext.Thread_State.ALIVE;
|
|
context.arg = 0;
|
|
context.priority = 0;
|
|
context.age = 0;
|
|
context.parent = 0;
|
|
ProcessContext.m_ContextList = context;
|
|
ProcessContext.m_CurrentContext = context;
|
|
|
|
IOPort counter0 = new IOPort(0x40);
|
|
IOPort cmd = new IOPort(0x43);
|
|
|
|
int divisor = 1193182 / 20;
|
|
cmd.Byte = (0x06 | 0x30);
|
|
counter0.Byte = (byte)divisor;
|
|
counter0.Byte = (byte)(divisor >> 8);
|
|
|
|
IOPort pA1 = new IOPort(0xA1);
|
|
IOPort p21 = new IOPort(0xA1);
|
|
pA1.Byte = 0x00;
|
|
p21.Byte = 0x00;
|
|
}
|
|
|
|
public static void EntryPoint()
|
|
{
|
|
ProcessContext.m_CurrentContext.entry?.Invoke();
|
|
ProcessContext.m_CurrentContext.paramentry?.Invoke(ProcessContext.m_CurrentContext.param);
|
|
ProcessContext.m_CurrentContext.state = ProcessContext.Thread_State.DEAD;
|
|
while(true) { } // remove from thread pool later
|
|
}
|
|
|
|
public static int interruptCount;
|
|
|
|
public static void SwitchTask()
|
|
{
|
|
interruptCount++;
|
|
if (ProcessContext.m_CurrentContext != null)
|
|
{
|
|
ProcessContext.m_CurrentContext.esp = INTs.mStackContext;
|
|
if (ProcessContext.m_CurrentContext.next != null)
|
|
{
|
|
ProcessContext.m_CurrentContext = ProcessContext.m_CurrentContext.next;
|
|
}
|
|
else
|
|
{
|
|
ProcessContext.m_CurrentContext = ProcessContext.m_ContextList;
|
|
}
|
|
INTs.mStackContext = ProcessContext.m_CurrentContext.esp;
|
|
}
|
|
Global.PIC.EoiMaster();
|
|
Global.PIC.EoiSlave();
|
|
}
|
|
}
|
|
}
|