Cosmos/source/Cosmos.Core/Processing/ProcessContext.cs
Og-Rok 5040d049e3 Merge remote-tracking branch 'origin/OgRok-Multitasking' into OgRok-Multitasking
# Conflicts:
#	source/Cosmos.Core/Processing/ProcessContext.cs
#	source/Cosmos.Core/Processing/ProcessorScheduler.cs
#	source/Cosmos.HAL2/Global.cs
2018-04-04 01:28:54 +01:00

160 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace Cosmos.Core.Processing
{
public static unsafe class ProcessContext
{
public enum Thread_State
{
ALIVE = 0,
DEAD = 1,
WAITING_SLEEP = 2,
WAITING_SEMAPHORE = 3,
PAUSED = 4
}
public enum Context_Type
{
THREAD = 0,
PROCESS = 1
}
public class Context
{
public Context next;
public Context_Type type;
public uint tid;
public string name;
public uint esp;
public uint stacktop;
public System.Threading.ThreadStart entry;
public System.Threading.ParameterizedThreadStart paramentry;
public uint cr3;
public Thread_State state;
public Thread_State old_state;
public object param;
public uint arg;
public uint priority;
public uint age;
public uint parent;
}
public const uint STACK_SIZE = 4096;
public static uint m_NextCID;
public static Context m_CurrentContext;
public static Context m_ContextList;
public static Context GetContext(int tid)
{
/*for(int i = 0; i < m_ContextList.Count; i++)
{
if(m_ContextList[i].tid == tid)
{
return m_ContextList[i];
}
}*/
Context ctx = m_ContextList;
while(ctx.next != null)
{
ctx = ctx.next;
}
return null;
}
public static uint* SetupStack(uint* stack)
{
uint origin = (uint)stack;
*--stack = 0xFFFFFFFF; // trash
*--stack = 0xFFFFFFFF; // trash
*--stack = 0xFFFFFFFF; // trash
*--stack = 0xFFFFFFFF; // trash
<<<<<<< HEAD
=======
/*for(int i = 0; i < 512 / 4; i++)
{
*--stack = 0; // MMX
}*/ // nope, just not going to bother today
for(int i = args.Length - 1; i >= 0; i++) // will push arguments when we patch in the object utils
{
*--stack = ObjUtilities.GetPointer(args[i]);
}
>>>>>>> origin/OgRok-Multitasking
*--stack = 0x10; // ss ?
*--stack = 0x00000202; // eflags
*--stack = 0x8; // cs
*--stack = ObjUtilities.GetEntryPoint(); // eip
*--stack = 0; // error
*--stack = 0; // int
*--stack = 0; // eax
*--stack = 0; // ebx
*--stack = 0; // ecx
*--stack = 0; // offset
*--stack = 0; // edx
*--stack = 0; // esi
*--stack = 0; // edi
*--stack = origin; //ebp
*--stack = 0x10; // ds
*--stack = 0x10; // fs
*--stack = 0x10; // es
*--stack = 0x10; // gs
return stack;
}
public static uint StartContext(string name, System.Threading.ThreadStart entry, Context_Type type)
{
Context context = new Context();
context.type = type;
context.tid = m_NextCID++;
context.name = name;
context.stacktop = GCImplementation.AllocNewObject(4096);
context.esp = (uint)SetupStack((uint*)(context.stacktop + 4000));
context.state = Thread_State.ALIVE;
context.entry = entry;
if (type == Context_Type.PROCESS)
{
context.parent = 0;
}
else
{
context.parent = m_CurrentContext.tid;
}
Context ctx = m_ContextList;
while (ctx.next != null)
{
ctx = ctx.next;
}
ctx.next = context;
return context.tid;
}
public static uint StartContext(string name, System.Threading.ParameterizedThreadStart entry, Context_Type type, object param)
{
Context context = new Context();
context.type = type;
context.tid = m_NextCID++;
context.name = name;
context.stacktop = GCImplementation.AllocNewObject(4096);
context.esp = (uint)SetupStack((uint*)(context.stacktop + 4000));
context.state = Thread_State.ALIVE;
context.paramentry = entry;
context.param = param;
if (type == Context_Type.PROCESS)
{
context.parent = 0;
}
else
{
context.parent = m_CurrentContext.tid;
}
Context ctx = m_ContextList;
while (ctx.next != null)
{
ctx = ctx.next;
}
ctx.next = context;
return context.tid;
}
}
}