1. makefile: added --fno-stack-protector to prevent gcc from fancy behaviors.

2. bochs config: configured memory to be 256MB and added magic breakpoint.
3. wrote complete print library for x86 kernel(including scrolling).
4. x86 kernel now detects information passed by grub2.
5. removed redundant source file.
This commit is contained in:
HyperAssembler 2015-01-31 01:05:53 -08:00
parent a40f0ffc0f
commit 250299c378
30 changed files with 305 additions and 1144 deletions

View File

@ -2,7 +2,7 @@
plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, gameport=1
config_interface: win32config
display_library: win32
memory: host=256, guest=512
memory: host=256, guest=256
romimage: file="bochs/BIOS-bochs-latest"
vgaromimage: file="bochs/VGABIOS-lgpl-latest"
boot: cdrom

View File

@ -2,9 +2,10 @@
plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, gameport=1
config_interface: win32config
display_library: win32, options="gui_debug"
memory: host=256, guest=512
memory: host=256, guest=256
romimage: file="bochs/BIOS-bochs-latest"
vgaromimage: file="bochs/VGABIOS-lgpl-latest"
boot: cdrom
floppy_bootsig_check: disabled=0
# no floppya
@ -24,6 +25,7 @@ print_timestamps: enabled=0
port_e9_hack: enabled=0
private_colormap: enabled=0
clock: sync=none, time0=local, rtc_sync=0
magic_break: enabled=1
# no cmosimage
# no loader
log: -

View File

@ -4,14 +4,14 @@ LD = ld
#x86 vars
C_SRC_PATH_32 = x86/src/c
ASM_SRC_PATH_32 = x86/src/asm
C_FLAGS_32 = -m32 -c -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
C_FLAGS_32 = -m32 -c -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
ASM_FLAGS_32 = -f elf32 -I $(ASM_SRC_PATH_32)/
LD_FLAGS_32 = -melf_i386
LD_SCRIPT_32 = build/link32.ld
#x64 vars
C_SRC_PATH_64 = x64/src/c
ASM_SRC_PATH_64 = x64/src/asm
C_FLAGS_64 = -m64 -c -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
C_FLAGS_64 = -m64 -c -fno-stack-protector -fno-builtin -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -masm=intel -Wall -Wextra
ASM_FLAGS_64 = -f elf64 -I $(ASM_SRC_PATH_64)/
LD_FLAGS_64 = -melf_x86_64
LD_SCRIPT_64 = build/link64.ld

View File

@ -1,58 +0,0 @@
#ifndef _KeCPUStruct32_h_
#define _KeCPUStruct32_h_
#include "KeDef.h"
#pragma pack(push)
#pragma pack(1)
typedef struct {
ULONG16 LimitLow;
ULONG16 BaseLow;
ULONG8 BaseMid;
ULONG8 Attr1;
ULONG8 LimitHigh_Attr2;
ULONG8 BaseHigh;
} IAGDT32,*PIAGDT32;
typedef IAGDT32 IALDT32,*PIALDT32;
typedef struct {
ULONG16 OffsetLow;
ULONG16 Selector;
ULONG8 Count;
ULONG8 Attr;
ULONG16 OffsetHigh;
} IAGATE32,*PIAGATE32;
typedef struct {
ULONG32 BackLine;
ULONG32 esp0;
ULONG32 ss0;
ULONG32 esp1;
ULONG32 ss1;
ULONG32 esp2;
ULONG32 ss2;
ULONG32 cr3;
ULONG32 eip;
ULONG32 flags;
ULONG32 eax;
ULONG32 ecx;
ULONG32 edx;
ULONG32 ebx;
ULONG32 esp;
ULONG32 ebp;
ULONG32 esi;
ULONG32 edi;
ULONG32 es;
ULONG32 cs;
ULONG32 ss;
ULONG32 ds;
ULONG32 fs;
ULONG32 gs;
ULONG32 LDT;
ULONG16 Trap;
ULONG16 IOBase;
} IATSS32,*PIATSS32;
#pragma pack(pop)
#endif

View File

@ -1,16 +0,0 @@
#ifndef _KeGlobalVariables_h_
#define _KeGlobalVariables_h_
#include "KeDef.h"
#include "KeCPUStruct32.h"
#include "KeProcess32.h"
ULONG8 KeGDTPtr[6];
IAGDT32 KeGDT[GDT_DESCRIPTOR_NUMBER];
ULONG8 KeIDTPtr[6];
IATSS32 KeTSS;
IAGATE32 KeIDT[IDT_GATE_NUMBER];
PhProcess PStartProcess;
hPrcocess KeUserProcessTable[MAXIMUM_PROCESS_NUMBER];
hPrcocess KeKernelProcessTable[MAXIMUM_PROCESS_NUMBER];
#endif
//Global Variables

View File

@ -1,29 +0,0 @@
#include "KeGraph32.h"
#include "KeDef.h"
#include "KeGlobalVariables.h"
#include "KeKernelStruct32.h"
ULONG32 KeGetCurrentGraphRow( _IN_ ULONG32 GraphPosition)
{
return (GraphPosition/2*DOSGraphColumn);
}
ULONG32 KeCheckGraphRowForPrint(_IN_ ULONG32 GraphPosition)
{
if(GraphPosition>=DOSGraphMaxBytes)
{
GraphPosition = _asm_KeGraphRowOverflow(GraphPosition);
}
return GraphPosition;
}
ULONG32 KeCheckGraphRowForReturn(_IN_ ULONG32 GraphPosition)
{
ULONG32 GraphRow = GraphPosition/(2*DOSGraphColumn);
if(GraphRow >= (DOSGraphRow-1))
{
GraphPosition = _asm_KeGraphRowOverflow(GraphPosition);
GraphPosition = _asm_KeSetGraphPosition32(23,0);
}
return GraphPosition;
}

View File

@ -1,19 +0,0 @@
#ifndef _KeGraph32_h_
#define _KeGraph32_h_
#include "KeDef.h"
#include "KeGlobalVariables.h"
#define DOSGraphColumn 80
#define DOSGraphRow 25
#define DOSGraphMaxBytes 4000
#define DOSGraphBase 0xb8000
VOID HYPKERNELAPI _asm_KePrintHex32(_IN_ ULONG32 Hex);
VOID HYPKERNELAPI _asm_KePrintStr32(_IN_ PCHAR OffsetStr);
ULONG32 HYPKERNELAPI _asm_KeSetGraphPosition32(_IN_ ULONG32 Row, _IN_ ULONG32 Column);
VOID HYPKERNELAPI _asm_KeClearGraph32(VOID);
ULONG32 HYPKERNELAPI _asm_KeGraphRowOverflow(_IN_ ULONG32 GraphPosition);
ULONG32 HYPKERNELAPI KeCheckGraphRowForPrint(_IN_ ULONG32 GraphPosition);
ULONG32 HYPKERNELAPI KeCheckGraphRowForReturn(_IN_ ULONG32 GraphPosition);
ULONG32 HYPKERNELAPI _asm_KeGetCurrentGraphPosition32(VOID);
ULONG32 HYPKERNELAPI KeGetCurrentGraphRow(_IN_ ULONG32 GraphPosition);
#endif

View File

@ -1,85 +0,0 @@
#include "KeDef.h"
#include "KeGlobalVariables.h"
#include "KeGraph32.h"
#include "KeMemory32.h"
#include "KeIO32.h"
#include "KeKernelStruct32.h"
#include "KeProcess32.h"
VOID HYPKERNELAPI KeSystemInit(VOID);
VOID HYPKERNELAPI KeRawDelay(ULONG32 time);
VOID ProcessA(VOID);
char TempStack[PROCESS_STACK_MAX_SIZE];
VOID KeSystemInit(VOID)
{
ULONG32 i;
KeRawDelay(3500);
_asm_KeClearGraph32();
_asm_KeSetGraphPosition32(KeGetCurrentGraphRow(_asm_KeGetCurrentGraphPosition32()),28);
KeRawDelay(500);
_asm_KePrintStr32("=Hyper Operating System=\n");
KeRawDelay(500);
_asm_KePrintStr32("New GDT Loaded\n");
KeRawDelay(500);
_asm_KePrintStr32("Kernel Entry Entered.\n");
KeRawDelay(500);
_asm_KePrintStr32("Initializing Interrupt...\n");
KeRawDelay(500);
KeInitInterrupt();
_asm_KePrintStr32("Interrupt Initialization Completed.\n");
_asm_KePrintStr32("Testing Scroll...\n");
KeRawDelay(1000);
for(i=0;i<=30;i++)
{
_asm_KePrintStr32("This is text #");
_asm_KePrintHex32(i);
_asm_KePrintStr32(", ");
_asm_KePrintHex32(30-i);
_asm_KePrintStr32(" left.\n");
KeRawDelay(50);
}
_asm_KePrintStr32("Scroll Test Completed.\n");
KeRawDelay(500);
_asm_KePrintStr32("Initializing Process Configurations...\n");
KeInitProcess();
KeRawDelay(500);
_asm_KePrintStr32("Initializing Process Completed.\n");
KeRawDelay(500);
_asm_KePrintStr32("Try to run a process.\n");
KeCreateProcess(0,&ProcessA,&TempStack[0],Process_Priviliege_Kernel);
shit: goto shit;
}
#define PTEBase 0x00045000
#define PDEBase 0x00046000
VOID ProcessA(VOID)
{
ULONG32 i = 0;
while(i<1000)
{
_asm_KePrintHex32(i);
KeRawDelay(250);
i++;
}
}
VOID ConstructPagingTable()
{
PULONG32 pos = (PULONG32)0x0000000;
}
VOID KeRawDelay(ULONG32 time)
{
ULONG32 i,j;
for(i=0;i<=time;i++)
{
for(j=0;j<=10000;j++)
{
j=j;
}
}
}

View File

@ -1,53 +0,0 @@
#ifndef _KeKernelStruct32_h_
#define _KeKernelStruct32_h_
#include "KeDef.h"
#pragma pack(push)
#pragma pack(4)
typedef struct {
ULONG32 Base; //+0
ULONG32 Limit; //+4
ULONG32 AVL; //+8
ULONG32 DB; // +12
ULONG32 P; //+16
ULONG32 DPL; // +20
ULONG32 G; //+24
ULONG32 S; //+28
ULONG32 Type; //+32
} KeGDTDescriptor32, *PKeGDTDescriptor32;
typedef struct {
ULONG32 D;
ULONG32 Offset;
ULONG32 Selector;
ULONG32 DPL;
ULONG32 P;
ULONG32 GateType;
} KeIDTDescriptor32, *PKeIDTDescriptor32;
typedef struct
{
ULONG32 P;
ULONG32 RW;
ULONG32 US;
ULONG32 PWT;
ULONG32 PCD;
ULONG32 A;
ULONG32 PS;
ULONG32 Address;
} KePDE4KDescriptor32, *PKePDE4KDescriptor32;
typedef struct {
ULONG32 P;
ULONG32 RW;
ULONG32 US;
ULONG32 PWT;
ULONG32 PCD;
ULONG32 A;
ULONG32 D;
ULONG32 PS;
ULONG32 G;
ULONG32 Address;
} KePTE4KDescriptor32, *PKePTE4KDescriptor32;
#pragma pack(pop)
#endif

View File

@ -1,217 +0,0 @@
#include "KeMemory32.h"
#include "KeDef.h"
#include "KeKernelStruct32.h"
#include "KeGraph32.h"
PVOID HYPKERNELAPI KeMemorySet(_IN_ PVOID Source, _IN_ BYTE Value, _IN_ ULONG32 Length)
{
PBYTE src = (PBYTE)Source;
while (Length > 0)
{
_asm_KePrintHex32(Length);
_asm_KePrintStr32("\n");
*src = Value;
src++;
Length--;
};
return 0;
}
PVOID HYPKERNELAPI KeMemoryCopy(_IN_ PVOID src, _IN_ PVOID dst, _IN_ ULONG32 size)
{
ULONG32 i = 0;
for (i = 0; i < size; i++)
{
*((PCHAR)dst + i) = *((PCHAR)src + i);
}
return dst;
}
VOID HYPKERNELAPI KeInitializeWritePDE4KDescriptor32(_OUT_ PKePDE4KDescriptor32 Desc, _IN_ ULONG32 P, _IN_ ULONG32 RW, _IN_ ULONG32 US, _IN_ ULONG32 PWT, _IN_ ULONG32 PCD, _IN_ ULONG32 A, _IN_ ULONG32 PS, _IN_ ULONG32 Address)
{
if (Desc == NULL)
return;
Desc->P = P;
Desc->Address = Address;
Desc->A = A;
Desc->PS = PS;
Desc->US = US;
Desc->RW = RW;
Desc->PWT = PWT;
Desc->PCD = PCD;
return;
}
VOID HYPKERNELAPI KeWritePDE4K32(_IN_ PKePDE4KDescriptor32 Desc, _IN_ PVOID Dst)
{
if (Desc == NULL || Dst == NULL)
return;
ULONG32 temp = 0;
temp = Desc->Address;
temp = (temp & 0xfffff000);
temp = temp + Desc->P;
temp = temp + Desc->RW;
temp = temp + Desc->US;
temp = temp + Desc->PWT;
temp = temp + Desc->PCD;
temp = temp + Desc->A;
temp = temp + Desc->PS;
*(PULONG32)Dst = temp;
return;
}
VOID HYPKERNELAPI KeInitializeWritePTE4KDescriptor32(_OUT_ PKePTE4KDescriptor32 Desc, _IN_ ULONG32 P, _IN_ ULONG32 RW, _IN_ ULONG32 US, _IN_ ULONG32 PWT, _IN_ ULONG32 PCD, _IN_ ULONG32 A, _IN_ ULONG32 D, _IN_ ULONG32 PS, _IN_ ULONG32 G, _IN_ ULONG32 Address)
{
if (Desc == NULL)
return;
Desc->P = P;
Desc->Address = Address;
Desc->A = A;
Desc->US = US;
Desc->RW = RW;
Desc->PWT = PWT;
Desc->PCD = PCD;
Desc->PS = PS;
Desc->G = G;
Desc->D = D;
return;
}
VOID HYPKERNELAPI KeGetPDEInfo( _OUT_ PKePDE4KDescriptor32 pDesc, _IN_ PVOID PDEPtr)
{
if (pDesc == NULL || PDEPtr == NULL)
return;
ULONG32 temp = *(PULONG32)PDEPtr;
pDesc->Address = (temp >> 12) << 12;
pDesc->A = temp & KeWritePDE4K32_A_1;
pDesc->P = temp & KeWritePDE4K32_P_1;
pDesc->PCD = temp & KeWritePDE4K32_PCD_1;
pDesc->PS = temp & KeWritePDE4K32_PS_1;
pDesc->US = temp & KeWritePDE4K32_US_1;
pDesc->RW = temp & KeWritePDE4K32_RW_1;
pDesc->PWT = temp & KeWritePDE4K32_PWT_1;
return;
}
VOID HYPKERNELAPI KeGetPTEInfo( _OUT_ PKePTE4KDescriptor32 pDesc, _IN_ PVOID PTEPtr)
{
if (pDesc == NULL || PTEPtr == NULL)
return;
ULONG32 temp = *(PULONG32)PTEPtr;
pDesc->Address = (temp >> 12) << 12;
pDesc->P = temp & KeWritePTE4K32_P_1;
pDesc->D = temp & KeWritePTE4K32_D_1;
pDesc->US = temp & KeWritePTE4K32_US_1;
pDesc->PCD = temp & KeWritePTE4K32_PCD_1;
pDesc->PWT = temp & KeWritePTE4K32_PWT_1;
pDesc->PS = temp & KeWritePTE4K32_PS_1;
pDesc->RW = temp & KeWritePTE4K32_RW_1;
pDesc->G = temp & KeWritePTE4K32_G_1;
return;
}
VOID HYPKERNELAPI KeWritePTE4K32( _IN_ PKePTE4KDescriptor32 Desc, _IN_ PVOID Dst)
{
if (Desc == NULL || Dst == NULL)
return;
ULONG32 temp = 0;
temp = Desc->Address;
temp = (temp & 0xfffff000);
temp += Desc->P;
temp += Desc->RW;
temp += Desc->US;
temp += Desc->PWT;
temp += Desc->PCD;
temp += Desc->A;
temp += Desc->D;
temp += Desc->PS;
temp += Desc->G;
*(PULONG32)Dst = temp;
}
ULONG32 HYPKERNELAPI KeGetPDEIndex4K32( _IN_ ULONG32 VirtualAddress)
{
return VirtualAddress >> 22;
}
ULONG32 HYPKERNELAPI KeGetPTEIndex4K32( _IN_ ULONG32 VirtualAddress)
{
return (VirtualAddress << 10) >> 22;
}
ULONG32 HYPKERNELAPI MmMapVirtualAddress4K32(_IN_ PVOID PDEBase, _IN_ ULONG32 PhysicalAddress, _IN_ ULONG32 VirtualAddress, _IN_ PKePTE4KDescriptor32 PTEDesc, _IN_ _OPTIONAL_ PKePDE4KDescriptor32 PDEDesc)
{
//Physical Address and Virtual Address will be trimmed to be 4K-aligned
//Please assign proper address for PDEDesc
KePDE4KDescriptor32 PDE;
ULONG32 PDEIndex = KeGetPDEIndex4K32(VirtualAddress);
ULONG32 PTEIndex = KeGetPTEIndex4K32(VirtualAddress);
KeGetPDEInfo(&PDE, (PVOID)(PDEIndex*4 + (ULONG32)PDEBase));
if (PDE.P == KeWritePDE4K32_P_0)
{
KeWritePDE4K32(PDEDesc, (PVOID)(PDEIndex * 4 + (ULONG32)PDEBase));
}
ULONG32 PTEBase = (*(PULONG32)((ULONG32)PDEBase + PDEIndex * 4) >> 12) << 12;
PVOID Target = (PVOID)((*(PULONG32)(PTEBase + PTEIndex * 4) >> 12) << 12);
KePTE4KDescriptor32 PTE;
KeGetPTEInfo(&PTE, Target);
PTEDesc->Address = VirtualAddress;
KeWritePTE4K32(PTEDesc, Target);
return 0;
}
ULONG32 HYPKERNELAPI KeGetBit(_IN_ PVOID Source, _IN_ ULONG32 Position)
{
if (!Source)
return 0;
ULONG32 Quotient = Position / 8;
ULONG32 Remainder = Position % 8;
PULONG8 CurrentPos = (PULONG8)Source;
ULONG8 Value;
while (Quotient > 0)
{
CurrentPos++;
Quotient--;
}
Value = *CurrentPos;
Value = Value << (7 - Remainder);
Value = Value >> 7;
return Value;
}
ULONG32 HYPKERNELAPI KeSetBit(_IN_ PVOID Source, _IN_ ULONG32 Position, _IN_ ULONG32 Value)
{
if (!Source || Position < 0 || (Value != 0 && Value != 1))
return 1;
ULONG32 Quotient = Position / 8;
ULONG32 Remainder = Position % 8;
PULONG8 CurrentPos = (PULONG8)Source;
while (Quotient > 0)
{
CurrentPos++;
Quotient--;
}
if (Value == 1)
{
ULONG8 temp = 1;
temp = temp << Remainder;
*CurrentPos = *CurrentPos | temp;
}
else
{
ULONG8 temp = 0xfe;
while (Remainder > 0)
{
temp = temp << 1;
temp++;
Remainder--;
}
*CurrentPos = *CurrentPos & temp;
}
return 0;
}

View File

@ -1,64 +0,0 @@
#ifndef _KeMemory32_h_
#define _KeMemory32_h_
#include "KeDef.h"
#include "KeGlobalVariables.h"
#include "KeKernelStruct32.h"
#include "KeCPUStruct32.h"
#define KeVirtualAddressToPhysicalAddress(SegmentBase,VirtualAddress) (ULONG32)((ULONG32)SegmentBase+(ULONG32)(VirtualAddress))
PVOID HYPKERNELAPI KeMemorySet(_IN_ PVOID Source,_IN_ BYTE Value,_IN_ ULONG32 Length);
PVOID HYPKERNELAPI _asm_KeMemoryCopy(_IN_ PVOID src,_IN_ PVOID dst,_IN_ ULONG32 size);
PVOID HYPKERNELAPI KeMemoryCopy(_IN_ PVOID src,_IN_ PVOID dst,_IN_ ULONG32 size);
VOID HYPKERNELAPI KeInitializeWritePDE4KDescriptor32(_OUT_ PKePDE4KDescriptor32 Desc,_IN_ ULONG32 P,_IN_ ULONG32 RW, _IN_ ULONG32 US,_IN_ ULONG32 PWT,_IN_ ULONG32 PCD,_IN_ ULONG32 A,_IN_ ULONG32 PS,_IN_ ULONG32 Address);
VOID HYPKERNELAPI KeWritePDE4K32(_IN_ PKePDE4KDescriptor32 Desc, _IN_ PVOID Dst);
VOID HYPKERNELAPI KeInitializeWritePTE4KDescriptor32(_OUT_ PKePTE4KDescriptor32 Desc,_IN_ ULONG32 P,_IN_ ULONG32 RW, _IN_ ULONG32 US,_IN_ ULONG32 PWT,_IN_ ULONG32 PCD,_IN_ ULONG32 A,_IN_ ULONG32 D,_IN_ ULONG32 PS,_IN_ ULONG32 G,_IN_ ULONG32 Address);
VOID HYPKERNELAPI KeWritePTE4K32(_IN_ PKePTE4KDescriptor32 Desc,_IN_ PVOID Dst );
VOID HYPKERNELAPI KeGetPDEInfo(_OUT_ PKePDE4KDescriptor32 pDesc, _IN_ PVOID PDEPtr);
VOID HYPKERNELAPI KeGetPTEInfo(_OUT_ PKePTE4KDescriptor32 pDesc, _IN_ PVOID PTEPtr);
ULONG32 HYPKERNELAPI KeGetPDEIndex4K32(_IN_ ULONG32 Virtual_Address);
ULONG32 HYPKERNELAPI KeGetPTEIndex4K32(_IN_ ULONG32 Virtual_Address);
ULONG32 HYPKERNELAPI MmMapVirtualAddress4K32(_IN_ PVOID PDEPtr, _IN_ ULONG32 PhysicalAddress, _IN_ ULONG32 VirtualAddress, _IN_ PKePTE4KDescriptor32 PTEDesc, _IN_ _OPTIONAL_ PKePDE4KDescriptor32 PDEDesc);
ULONG32 HYPKERNELAPI KeSetBit(_IN_ PVOID Source, _IN_ ULONG32 Position, _IN_ ULONG32 Value);
ULONG32 HYPKERNELAPI KeGetBit(_IN_ PVOID Source, _IN_ ULONG32 Position);
#define KeWritePDE4K32_UNDEFINED_BIT 3
#define KeWritePDE4K32_P_0 0 // 0 for not present
#define KeWritePDE4K32_P_1 1 // 1 for Present
#define KeWritePDE4K32_RW_0 0 // 0 for ReadOnly
#define KeWritePDE4K32_RW_1 2 // 1 for ReadWrite
#define KeWritePDE4K32_US_0 0 // 0 for Superuser
#define KeWritePDE4K32_US_1 4 // 1 for All
#define KeWritePDE4K32_PWT_1 8 // Write back
#define KeWritePDE4K32_PWT_0 0 // Write Through
#define KeWritePDE4K32_PCD_0 0 // 0 for Cache Enabled
#define KeWritePDE4K32_PCD_1 16 // 1 for Cache Disabled
#define KeWritePDE4K32_A_0 0 //0 for not accessed
#define KeWritePDE4K32_A_1 32 //1 for accessed
#define KeWritePDE4K32_PS_0 0 //0 for 4K pages
#define KeWritePDE4K32_PS_1 128 //1 for 4M pages
#define KeWritePTE4K32_UNDEFINED_BIT 3
#define KeWritePTE4K32_P_0 0 // 0 for not present
#define KeWritePTE4K32_P_1 1 // 1 for present
#define KeWritePTE4K32_RW_0 0 // 0 for readonly
#define KeWritePTE4K32_RW_1 2 // 1 for readwrite
#define KeWritePTE4K32_US_0 0 // 0 for superuser
#define KeWritePTE4K32_US_1 4 // 1 for all
#define KeWritePTE4K32_PWT_0 0 // 0 for write through
#define KeWritePTE4K32_PWT_1 8 // 1 for write back
#define KeWritePTE4K32_PCD_0 0 // 0 for cache enabled
#define KeWritePTE4K32_PCD_1 16 // 1 for cache disabled
#define KeWritePTE4K32_A_0 0 // not accessed
#define KeWritePTE4K32_A_1 32 // accessed
#define KeWritePTE4K32_D_0 0 // Dirty - set for has been written to
#define KeWritePTE4K32_D_1 64 // Dirty - set for has been written to
#define KeWritePTE4K32_PS_0 0 // should be 0
#define KeWritePTE4K32_PS_1 128 //
#define KeWritePTE4K32_G_0 0 // Global Not Set
#define KeWritePTE4K32_G_1 256 // Global Set
#endif

View File

@ -1,60 +0,0 @@
#include "KeProcess32.h"
#include "KeDef.h"
#include "KeMemory32.h"
#include "KeKernelStruct32.h"
#include "KeIO32.h"
#include "KeGraph32.h"
VOID KeInitProcess(VOID)
{
//TSS
KeGDTDescriptor32 desc;
KeMemorySet((PVOID)&KeTSS,0,sizeof(IATSS32));
KeTSS.ss0 = GDT_SELECTOR_DATA;
//DESC INIT
KeInitializeKeGDTDescriptor32(&desc, (ULONG32)&KeTSS, sizeof(IATSS32)-1, KeWriteGDT_G_0,KeWriteGDT_P_1 , KeWriteGDT_S_0, KeWriteGDT_Type_System_32BitsTSSAvailable, KeWriteGDT_DPL_0, KeWriteGDT_DB_1, KeWriteGDT_AVL_0);
KeWriteGDT(GDT_INDEX_TSS, &KeGDT[0], &desc);
KeTSS.IOBase = sizeof(IATSS32)-1;
_asm_KeLoadTSS(GDT_SELECTOR_TSS);
}
VOID KeCreateProcess(_IN_ ULONG32 ProcessID, _IN_ PVOID ProcessAddress, _IN_ PVOID ProcessStack, _IN_ PROCESSPRIVILEGE Privilege)
{
PhProcess process;
if(Privilege == Process_Priviliege_Kernel)
{
process = &KeKernelProcessTable[ProcessID];
process->ProcessID = ProcessID;
process->Privilege = Privilege;
process->RestartStack.cs = GDT_SELECTOR_CODE; //(LDT_SELECTOR_CODE & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.ds = GDT_SELECTOR_DATA; //(LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.es = GDT_SELECTOR_DATA; //(LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.fs = GDT_SELECTOR_DATA; //(LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.ss = GDT_SELECTOR_DATA; // (LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.gs = GDT_SELECTOR_GRAPH; // (GDT_SELECTOR_GRAPH & SA_RPL_MASK) | RPL_TASK;
}
else
{
process = &KeUserProcessTable[ProcessID];
process->ProcessID = ProcessID;
process->Privilege = Privilege;
process->RestartStack.cs =GDT_SELECTOR_USER_CODE; //(LDT_SELECTOR_CODE & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.ds =GDT_SELECTOR_USER_DATA; //(LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.es =GDT_SELECTOR_USER_DATA; //(LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.fs =GDT_SELECTOR_USER_DATA; //(LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.ss =GDT_SELECTOR_USER_DATA; // (LDT_SELECTOR_DATA & SA_RPL_MASK & SA_TI_MASK) | SA_TIL | RPL_TASK;
process->RestartStack.gs =GDT_SELECTOR_GRAPH; // (GDT_SELECTOR_GRAPH & SA_RPL_MASK) | RPL_TASK;
}
process->RestartStack.eip = (ULONG32)ProcessAddress;
process->RestartStack.esp = (ULONG32)ProcessStack + PROCESS_STACK_MAX_SIZE;
process->RestartStack.eFlags = 0x1202;
// process->LDTSelector = GDT_SELECTOR_LDT;
// KeMemoryCopy(&KeGDT[GDT_INDEX_CODE],&process->ProcessLDT[LDT_INDEX_CODE],sizeof(GDT_DESCRIPTOR));
// process->ProcessLDT[LDT_INDEX_CODE].Attr1 = DA_C | PRIVILEGE_TASK << 5;
// KeMemoryCopy(&KeGDT[GDT_INDEX_DATA],&process->ProcessLDT[LDT_INDEX_DATA],sizeof(GDT_DESCRIPTOR));
// process->ProcessLDT[LDT_INDEX_DATA].Attr1 = DA_DRW | PRIVILEGE_TASK << 5;
PStartProcess = process;
_asm_Restart();
}

View File

@ -1,52 +0,0 @@
#ifndef _KeProcess32_h_
#define _KeProcess32_h_
#include "KeDef.h"
#include "KeKernelStruct32.h"
#pragma pack(push)
#pragma pack(4)
typedef struct {
ULONG32 gs;
ULONG32 fs;
ULONG32 es;
ULONG32 ds;
ULONG32 edi;
ULONG32 esi;
ULONG32 ebp;
ULONG32 Kernel_esp;
ULONG32 ebx;
ULONG32 edx;
ULONG32 ecx;
ULONG32 eax;
ULONG32 ReturnAddress;
ULONG32 eip;
ULONG32 cs;
ULONG32 eFlags;
ULONG32 esp;
ULONG32 ss;
} hProcess_RestartStack,*PhProcess_RestartStack;
typedef ULONG32 PROCESSPRIVILEGE;
#define Process_Priviliege_User 0x01
#define Process_Priviliege_Kernel 0x00
typedef struct {
hProcess_RestartStack RestartStack;
//ULONG16 LDTSelector;
//LDT_DESCRIPTOR ProcessLDT[LDT_DESCRIPTOR_NUMBER];
ULONG32 ProcessID;
PCHAR ProcessName;
PROCESSPRIVILEGE Privilege;
} hPrcocess,*PhProcess;
#pragma pack(pop)
VOID HYPKERNELAPI KeCreateProcess(_IN_ ULONG32 ProcessID, _IN_ PVOID ProcessAddress, _IN_ PVOID ProcessStack, _IN_ PROCESSPRIVILEGE Privilege);
VOID HYPKERNELAPI KeInitProcess(VOID);
VOID HYPKERNELAPI _asm_KeLoadTSS(_IN_ ULONG32 Selector_TSS);
VOID HYPKERNELAPI _asm_Restart(VOID);
#endif

View File

@ -26,7 +26,7 @@ mov fs,ax
mov gs,ax
mov ds,ax
pop eax
mov ebp,esp
mov esp,ebp
pop ebp
ret

View File

@ -17,28 +17,7 @@ dd 0
dd 0
dd GRUB_ENTRY_ADDR
%include "pm.inc"
;GDT to load
DESC_VOID: Descriptor 0,0,0
DESC_GRAPH: Descriptor 0b8000h,0xffff,DA_DRW | DA_DPL3
DESC_FLAT_C: Descriptor 0,0xfffff,DA_CR | DA_32 | DA_LIMIT_4K
DESC_FLAT_RW: Descriptor 0,0xfffff,DA_DRW | DA_32 | DA_LIMIT_4K
GDT_END:
GDT_LENGTH equ GDT_END - DESC_VOID - 1
GDT_PTR:
dw GDT_LENGTH
dd DESC_VOID
;SELECTORS
SLCT_CODE_0 equ DESC_FLAT_C - DESC_VOID
SLCT_GRAPH_0 equ DESC_GRAPH - DESC_VOID
SLCT_DATA_0 equ DESC_FLAT_RW - DESC_VOID
;stack
times 1024 db 0
times 4096 db 0
kernel_stack:
GRUB_ENTRY_ADDR:
@ -55,17 +34,7 @@ mov esp,eax
push dword 0
popfd
lgdt [GDT_PTR]
jmp SLCT_CODE_0:reload_cs
reload_cs:
mov ax,SLCT_DATA_0
mov ss,ax
mov ds,ax
mov es,ax
mov fs,ax
mov ax,SLCT_GRAPH_0
mov gs,ax
push ebx
;jmp since we are not coming back here :D
call hk_main
add esp,4

View File

@ -1,324 +0,0 @@
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; pm.inc
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Forrest Yu, 2005
; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; 描述符图示
; 图示一
;
; ------ ┏━━┳━━┓高地址
; 7
; ┣━━┫
;
; 字节 7
;
; ┣━━┫
; 0
; ------ ┣━━╋━━┫
; 7 G
; ┣━━╉──┨
; 6 D
; ┣━━╉──┨
; 5 0
; ┣━━╉──┨
; 4 AVL┃
; 字节 6 ┣━━╉──┨
; 3
; ┣━━┫
; 2
; ┣━━┫
; 1
; ┣━━┫
; 0
; ------ ┣━━╋━━┫
; 7 P
; ┣━━╉──┨
; 6
; ┣━━┫ DPL┃
; 5
; ┣━━╉──┨
; 4 S
; 字节 5 ┣━━╉──┨
; 3
; ┣━━┫ T
; 2 Y
; ┣━━┫ P
; 1 E
; ┣━━┫
; 0
; ------ ┣━━╋━━┫
; 23
; ┣━━┫
; 22
; ┣━━┫
;
; 字节
; 2, 3, 4
; ┣━━┫
; 1
; ┣━━┫
; 0
; ------ ┣━━╋━━┫
; 15
; ┣━━┫
; 14
; ┣━━┫
;
; 字节 0,1
;
; ┣━━┫
; 1
; ┣━━┫
; 0
; ------ ┗━━┻━━┛低地址
;
; 图示二
; 高地址………………………………………………………………………低地址
; | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
; |7654321076543210765432107654321076543210765432107654321076543210| <- 8 字节
; |--------========--------========--------========--------========|
; ┏━━━┳━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
; ┃31..24 (见下图) 段基址(23..0) 段界限(15..0)
;
; 基址2┃③│②│ ①┃基址1b│ 基址1a 段界限1
; ┣━━━╋━━━┳━━━╋━━━━━━━━━━━╋━━━━━━━┫
; %6 %5 %4 %3 %2 %1
; ┗━━━┻━━━┻━━━┻━━━┻━━━━━━━┻━━━━━━━┛
; \_________
; \__________________
; \________________________________________________
; \
; ┏━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┓
; 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
; ┣━━╋━━╋━━╋━━╋━━┻━━┻━━┻━━╋━━╋━━┻━━╋━━╋━━┻━━┻━━┻━━┫
; G D 0 AVL┃ 段界限 2 (19..16) P DPL S TYPE
; ┣━━┻━━┻━━┻━━╋━━━━━━━━━━━╋━━┻━━━━━┻━━┻━━━━━━━━━━━┫
; : 属性 2 : 段界限 2 : 属性1
; ┗━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━┛
; 高地址 低地址
;
;
; 说明:
;
; (1) P: 存在(Present)位。
; P=1 表示描述符对地址转换是有效的,或者说该描述符所描述的段存在,即在内存中;
; P=0 表示描述符对地址转换无效,即该段不存在。使用该描述符进行内存访问时会引起异常。
;
; (2) DPL: 表示描述符特权级(Descriptor Privilege level)共2位。它规定了所描述段的特权级用于特权检查以决定对该段能否访问。
;
; (3) S: 说明描述符的类型。
; 对于存储段描述符而言S=1,以区别与系统段描述符和门描述符(S=0)
;
; (4) TYPE: 说明存储段描述符所描述的存储段的具体属性。
;
;
; 数据段类型 类型值 说明
; ----------------------------------
; 0 只读
; 1 只读、已访问
; 2 /
; 3 /写、已访问
; 4 只读、向下扩展
; 5 只读、向下扩展、已访问
; 6 /写、向下扩展
; 7 /写、向下扩展、已访问
;
;
; 类型值 说明
; 代码段类型 ----------------------------------
; 8 只执行
; 9 只执行、已访问
; A 执行/
; B 执行/读、已访问
; C 只执行、一致码段
; D 只执行、一致码段、已访问
; E 执行/读、一致码段
; F 执行/读、一致码段、已访问
;
;
; 系统段类型 类型编码 说明
; ----------------------------------
; 0 <未定义>
; 1 可用286TSS
; 2 LDT
; 3 忙的286TSS
; 4 286调用门
; 5 任务门
; 6 286中断门
; 7 286陷阱门
; 8 未定义
; 9 可用386TSS
; A <未定义>
; B 忙的386TSS
; C 386调用门
; D <未定义>
; E 386中断门
; F 386陷阱门
;
; (5) G: 段界限粒度(Granularity)位。
; G=0 表示界限粒度为字节;
; G=1 表示界限粒度为4K 字节。
; 注意,界限粒度只对段界限有效,对段基地址无效,段基地址总是以字节为单位。
;
; (6) D: D位是一个很特殊的位在描述可执行段、向下扩展数据段或由SS寄存器寻址的段(通常是堆栈段)的三种描述符中的意义各不相同。
; 在描述可执行段的描述符中D位决定了指令使用的地址及操作数所默认的大小。
; D=1表示默认情况下指令使用32位地址及32位或8位操作数这样的代码段也称为32位代码段
; D=0 表示默认情况下使用16位地址及16位或8位操作数这样的代码段也称为16位代码段它与80286兼容。可以使用地址大小前缀和操作数大小前缀分别改变默认的地址或操作数的大小。
; 在向下扩展数据段的描述符中D位决定段的上部边界。
; D=1表示段的上部界限为4G
; D=0表示段的上部界限为64K这是为了与80286兼容。
; 在描述由SS寄存器寻址的段描述符中D位决定隐式的堆栈访问指令(如PUSH和POP指令)使用何种堆栈指针寄存器。
; D=1表示使用32位堆栈指针寄存器ESP
; D=0表示使用16位堆栈指针寄存器SP这与80286兼容。
;
; (7) AVL: 软件可利用位。80386对该位的使用未左规定Intel公司也保证今后开发生产的处理器只要与80386兼容就不会对该位的使用做任何定义或规定。
;
;----------------------------------------------------------------------------
; 描述符类型值说明
; 其中:
; DA_ : Descriptor Attribute
; D : 数据段
; C : 代码段
; S : 系统段
; R : 只读
; RW : 读写
; A : 已访问
; 其它 : 可按照字面意思理解
;----------------------------------------------------------------------------
DA_32 EQU 4000h ; 32 位段
DA_LIMIT_4K EQU 8000h ; 段界限粒度为 4K 字节
DA_DPL0 EQU 00h ; DPL = 0
DA_DPL1 EQU 20h ; DPL = 1
DA_DPL2 EQU 40h ; DPL = 2
DA_DPL3 EQU 60h ; DPL = 3
;----------------------------------------------------------------------------
; 存储段描述符类型值说明
;----------------------------------------------------------------------------
DA_DR EQU 90h ; 存在的只读数据段类型值
DA_DRW EQU 92h ; 存在的可读写数据段属性值
DA_DRWA EQU 93h ; 存在的已访问可读写数据段类型值
DA_C EQU 98h ; 存在的只执行代码段属性值
DA_CR EQU 9Ah ; 存在的可执行可读代码段属性值
DA_CCO EQU 9Ch ; 存在的只执行一致代码段属性值
DA_CCOR EQU 9Eh ; 存在的可执行可读一致代码段属性值
;----------------------------------------------------------------------------
; 系统段描述符类型值说明
;----------------------------------------------------------------------------
DA_LDT EQU 82h ; 局部描述符表段类型值
DA_TaskGate EQU 85h ; 任务门类型值
DA_386TSS EQU 89h ; 可用 386 任务状态段类型值
DA_386CGate EQU 8Ch ; 386 调用门类型值
DA_386IGate EQU 8Eh ; 386 中断门类型值
DA_386TGate EQU 8Fh ; 386 陷阱门类型值
;----------------------------------------------------------------------------
; 选择子图示:
; ┏━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┳━━┓
; 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
; ┣━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━┻━━╋━━╋━━┻━━┫
; 描述符索引 TI RPL
; ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━┻━━━━━┛
;
; RPL(Requested Privilege Level): 请求特权级,用于特权检查。
;
; TI(Table Indicator): 引用描述符表指示位
; TI=0 指示从全局描述符表GDT中读取描述符
; TI=1 指示从局部描述符表LDT中读取描述符。
;
;----------------------------------------------------------------------------
; 选择子类型值说明
; 其中:
; SA_ : Selector Attribute
SA_RPL0 EQU 0 ;
SA_RPL1 EQU 1 ; RPL
SA_RPL2 EQU 2 ;
SA_RPL3 EQU 3 ;
SA_TIG EQU 0 ; ┓TI
SA_TIL EQU 4 ;
;----------------------------------------------------------------------------
;----------------------------------------------------------------------------
; 分页机制使用的常量说明
;----------------------------------------------------------------------------
PG_P EQU 1 ; 页存在属性位
PG_RWR EQU 0 ; R/W 属性位值, /执行
PG_RWW EQU 2 ; R/W 属性位值, //执行
PG_USS EQU 0 ; U/S 属性位值, 系统级
PG_USU EQU 4 ; U/S 属性位值, 用户级
;----------------------------------------------------------------------------
; =========================================
; FLAGS - Intel 8086 Family Flags Register
; =========================================
;
; |11|10|F|E|D|C|B|A|9|8|7|6|5|4|3|2|1|0|
; | | | | | | | | | | | | | | | | | '--- CF……Carry Flag
; | | | | | | | | | | | | | | | | '--- 1
; | | | | | | | | | | | | | | | '--- PF……Parity Flag
; | | | | | | | | | | | | | | '--- 0
; | | | | | | | | | | | | | '--- AF……Auxiliary Flag
; | | | | | | | | | | | | '--- 0
; | | | | | | | | | | | '--- ZF……Zero Flag
; | | | | | | | | | | '--- SF……Sign Flag
; | | | | | | | | | '--- TF……Trap Flag (Single Step)
; | | | | | | | | '--- IF……Interrupt Flag
; | | | | | | | '--- DF……Direction Flag
; | | | | | | '--- OF……Overflow flag
; | | | | '----- IOPL……I/O Privilege Level (286+ only)
; | | | '----- NT……Nested Task Flag (286+ only)
; | | '----- 0
; | '----- RF……Resume Flag (386+ only)
; '------ VM……Virtual Mode Flag (386+ only)
;
; : see PUSHF POPF STI CLI STD CLD
;
; ------------------------------------------------------------------------------------------------------
;
; 描述符
; usage: Descriptor Base, Limit, Attr
; Base: dd
; Limit: dd (low 20 bits available)
; Attr: dw (lower 4 bits of higher byte are always 0)
%macro Descriptor 3
dw %2 & 0FFFFh ; 段界限 1 (2 字节)
dw %1 & 0FFFFh ; 段基址 1 (2 字节)
db (%1 >> 16) & 0FFh ; 段基址 2 (1 字节)
dw ((%2 >> 8) & 0F00h) | (%3 & 0F0FFh) ; 属性 1 + 段界限 2 + 属性 2 (2 字节)
db (%1 >> 24) & 0FFh ; 段基址 3 (1 字节)
%endmacro ; 8 字节
;
;
; usage: Gate Selector, Offset, DCount, Attr
; Selector: dw
; Offset: dd
; DCount: db
; Attr: db
%macro Gate 4
dw (%2 & 0FFFFh) ; 偏移 1 (2 字节)
dw %1 ; 选择子 (2 字节)
dw (%3 & 1Fh) | ((%4 << 8) & 0FF00h) ; 属性 (2 字节)
dw ((%2 >> 16) & 0FFFFh) ; 偏移 2 (2 字节)
%endmacro ; 8 字节
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -2,34 +2,41 @@
#include "kdef.h"
#include "multiboot.h"
#include "mem.h"
#include "print.h"
uint8 g_gdt[8 * 8];
gdt_ptr g_gdt_ptr;
uint8 g_idt[8 * 256];
idt_ptr g_idt_ptr;
extern uint32 text_pos;
extern word *kernel_stack;
void HYPKERNEL32 print_str(char *str)
extern int32* kernel_stack;
void HYPKERNEL32 hk_delay(uint32 i)
{
uint8 *gram = (uint8 *) 0x0b8000;
while (*str != 0)
uint32 j = 100000;
while(i--)
{
*gram = (uint8) *str;
gram++;
*gram = 7;
gram++;
str++;
while(j > 0)
{
j--;
}
}
return;
}
int32 HYPKERNEL32 hk_main(multiboot_info_t *multiboot_info)
void HYPKERNEL32 hk_main(multiboot_info_t const * const multiboot_info)
{
multiboot_info->mods_addr;
//initialize text position
uint32 i = 0;
text_pos = 0;
hk_print_str("Welcome to HYP OS! Please wait while we are gathering information...\n\n");
segment_descriptor desc_dummy = {.DPL = 0, .Pr = 0, .x64 = 0, .Sys = 0, .type = 0, .Sz = 0, .limit = 0, .Gr = 0, .base = 0, .Avl = 0};
segment_descriptor desc = {.Gr = 1, .Pr = 1, .Sz = 1, .Avl = 0, .Sys = 1, .x64 = 0, .base = 0, .limit = 0xFFFFF};
hk_print_str("*Setting up GDT...\n\n");
//dummy descriptor
hk_set_segment_descriptor(&g_gdt[0], &desc_dummy);
//ring 0 code seg, non-conforming
@ -48,35 +55,97 @@ int32 HYPKERNEL32 hk_main(multiboot_info_t *multiboot_info)
hk_set_segment_descriptor(&g_gdt[32], &desc);
g_gdt_ptr.limit = 8 * 8 - 1;
g_gdt_ptr.base = (uint32) g_gdt;
hk_load_gdt(&g_gdt_ptr, SEGMENT_SELECTOR(1, 0), SEGMENT_SELECTOR(3, 0));
if(multiboot_info->mods_count != 1)
//check memory, definitely < 32 so we assume that
//TODO: Add check for flags and update print functions to handle 64bit int
hk_print_str("*Checking memory information...\n");
BOCHS_MAGIC_BREAKPOINT;
if(multiboot_info->flags & (1 << 6))
{
print_str("No module loaded...");
multiboot_memory_map_t const *mem_map = (multiboot_memory_map_t *) multiboot_info->mmap_addr;
uint32 const mem_map_size = multiboot_info->mmap_length / sizeof(multiboot_memory_map_t);
hk_print_str("BaseAddr - Length - Type\n");
uint32 total_available_mem = 0;
uint32 total_reserved_mem = 0;
for (i = 0; i < mem_map_size; i++)
{
hk_print_hex((uint32)((mem_map + i)->addr));
hk_print_str(" - ");
hk_print_hex((uint32)((mem_map + i)->len));
hk_print_str(" - ");
hk_print_hex((mem_map + i)->type);
hk_print_str("\n");
if((mem_map + i)->type == MULTIBOOT_MEMORY_RESERVED)
{
total_reserved_mem += (uint32) ((mem_map + i)->len);
}
else if ((mem_map + i)->type == MULTIBOOT_MEMORY_AVAILABLE)
{
total_available_mem += (uint32) ((mem_map + i)->len);
}
}
hk_print_str("Total available memory: ");
hk_print_int(total_available_mem);
hk_print_str("B = ");
hk_print_int(total_available_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_available_mem/1024/1024);
hk_print_str("MB\n");
hk_print_str("Total reserved memory: ");
hk_print_int(total_reserved_mem);
hk_print_str("B = ");
hk_print_int(total_reserved_mem/1024);
hk_print_str("KB = ");
hk_print_int(total_reserved_mem/1024/1024);
hk_print_str("MB\n\n");
}
else
{
struct multiboot_mod_list* mod_list = (struct multiboot_mod_list*)multiboot_info->mods_addr;
print_str(" - Loaded Module");
print_str((char*)mod_list->cmdline);
hk_print_str("Memory information is currently unavailable.\n\n");
}
x66:
goto x66;
char *msg_x64_not_supported = "x64 not supported by CPU, continue in x86.";
char *msg_x64_supported = "x64 supported by CPU.";
//check modules
hk_print_str("*Checking loaded kernel modules...\n");
if(multiboot_info->flags & (1 << 3))
{
multiboot_module_t const * mods_list = (multiboot_module_t *)multiboot_info->mods_addr;
uint32 const mods_count = multiboot_info->mods_count;
hk_print_int(mods_count);
hk_print_str(" module(s) loaded:\n");
hk_print_str("Name - StartAddr - EndAddr\n");
for (i = 0; i < mods_count; i++)
{
hk_print_str((char *) (mods_list + i)->cmdline);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_start);
hk_print_str(" - ");
hk_print_hex((mods_list + i)->mod_end);
hk_print_str("\n");
}
hk_print_str("\n");
}
else
{
hk_print_str("Module information is currently unavailable.\n\n");
}
//detect architecture
hk_print_str("*Checking architecture...\n");
if (hk_support_x64() == 0)
{
print_str(msg_x64_not_supported);
hk_print_str("Arch: x86.\n\nInformation obtained. Countinue to x86 mode...");
x86:
goto x86;
}
else
{
print_str(msg_x64_supported);
hk_print_str("Arch: x86_64.\n\nInformation obtained. Countinue to x86_64 mode...");
}
//Setup x64
x64:
goto x64;
}

View File

@ -1,7 +1,7 @@
#include "kdef.h"
#include "mem.h"
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_descriptor* const seg_desc)
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, segment_descriptor const * const seg_desc)
{
if (gdt == NULL || seg_desc == NULL)
return -1;
@ -19,7 +19,7 @@ int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_desc
return 0;
}
int32 HYPKERNEL32 hk_set_interrupt_gate(uint8* const dst, const interrupt_gate* int_gate)
int32 HYPKERNEL32 hk_set_interrupt_gate(uint8* const dst, interrupt_gate const * int_gate)
{
if(dst == NULL || int_gate == NULL)
return -1;
@ -34,7 +34,7 @@ int32 HYPKERNEL32 hk_set_interrupt_gate(uint8* const dst, const interrupt_gate*
return 0;
}
int32 HYPKERNEL32 hk_set_trap_gate(uint8* const dst, const trap_gate* tr_gate)
int32 HYPKERNEL32 hk_set_trap_gate(uint8* const dst, trap_gate const * tr_gate)
{
if(dst == NULL || tr_gate == NULL)
return -1;
@ -49,7 +49,7 @@ int32 HYPKERNEL32 hk_set_trap_gate(uint8* const dst, const trap_gate* tr_gate)
return 0;
}
int32 HYPKERNEL32 hk_set_task_gate(uint8* const dst, const task_gate* int_gate)
int32 HYPKERNEL32 hk_set_task_gate(uint8* const dst, task_gate const * int_gate)
{
if(dst == NULL || int_gate == NULL)
return -1;
@ -64,30 +64,30 @@ int32 HYPKERNEL32 hk_set_task_gate(uint8* const dst, const task_gate* int_gate)
return 0;
}
int32 HYPKERNEL32 hk_set_page_table_entry_32(uint32* dest,uint32 base,uint32 flags)
void HYPKERNEL32 hk_mem_cpy(void* src, void* dst, uint32 size)
{
if (dest == NULL)
return -1;
*dest = (base & 0xFFFFF000) + (flags & 0x0FFF);
return 0;
if (src == NULL || dst == NULL)
return;
char* cSrc = (char*)src;
char* cDst = (char*)dst;
while (size--)
*(cDst++) = *(cSrc++);
return;
}
int32 HYPKERNEL32 hk_set_page_directory_entry_32(uint32* dest, uint32 base, uint32 flags)
void HYPKERNEL32 hk_mem_move(void* src, void* dst, uint32 size)
{
if (dest == NULL)
return -1;
*dest = (base & 0xFFFFF000) + (flags & 0x0FFF);
return 0;
}
int32 HYPKERNEL32 hk_map_physcial_address_32(uint32* page_directory_base, uint32 physical_addr, uint32 virtual_addr, uint32 flags)
{
if (page_directory_base == NULL)
return -1;
uint32 pde_idx = virtual_addr >> 22;
uint32 pde = *(page_directory_base + pde_idx * 4);
//uint32 page_table_base = ;
//uint32 pte_idx = ;
return 0;
}
if (src == NULL || dst == NULL)
return;
char* cSrc = (char*)src;
char* cDst = (char*)dst;
if (cSrc >= cDst)
{
return hk_mem_cpy(src,dst,size);
}
cSrc += size;
cDst += size;
while (size--)
*(--cDst) = *(--cSrc);
return;
}

View File

@ -55,11 +55,12 @@ typedef struct __attribute__ ((packed))
uint32 base;
} idt_ptr;
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, const segment_descriptor* const seg_desc);
extern void HYPKERNEL32 hk_load_gdt(const gdt_ptr* const ptr, const uint16 sel_code, const uint16 sel_data);
int32 HYPKERNEL32 hk_set_interrupt_gate(uint8* const dst, const interrupt_gate* int_gate);
int32 HYPKERNEL32 hk_set_trap_gate(uint8* const dst, const trap_gate* tr_gate);
int32 HYPKERNEL32 hk_set_task_gate(uint8* const dst, const task_gate* int_gate);
int32 HYPKERNEL32 hk_support_x64(void);
int32 HYPKERNEL32 hk_set_segment_descriptor(uint8* const gdt, segment_descriptor const * const seg_desc);
extern void HYPKERNEL32 hk_load_gdt(gdt_ptr const * const ptr, uint16 const sel_code, uint16 const sel_data);
int32 HYPKERNEL32 hk_set_interrupt_gate(uint8* const dst, interrupt_gate const * int_gate);
int32 HYPKERNEL32 hk_set_trap_gate(uint8* const dst, trap_gate const * tr_gate);
int32 HYPKERNEL32 hk_set_task_gate(uint8* const dst, task_gate const * int_gate);
void HYPKERNEL32 hk_mem_cpy(void* src, void* dst, uint32 size);
void HYPKERNEL32 hk_mem_move(void* src, void* dst, uint32 size);
extern int32 HYPKERNEL32 hk_support_x64(void);
#endif

View File

@ -1,5 +1,26 @@
#ifndef _MULTIBOOT_H_
#define _MULTIBOOT_H_
/* multiboot.h - Multiboot header file. */
/* Copyright (C) 1999,2003,2007,2008,2009 Free Software Foundation, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
* DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef MULTIBOOT_HEADER
#define MULTIBOOT_HEADER 1
/* How many bytes from the start of the file we search for the header. */
#define MULTIBOOT_SEARCH 8192
@ -69,6 +90,8 @@
/* Is there video information? */
#define MULTIBOOT_INFO_VIDEO_INFO 0x00000800
#ifndef ASM_FILE
typedef unsigned short multiboot_uint16_t;
typedef unsigned int multiboot_uint32_t;
typedef unsigned long long multiboot_uint64_t;
@ -195,4 +218,6 @@ struct multiboot_mod_list
};
typedef struct multiboot_mod_list multiboot_module_t;
#endif /* ! ASM_FILE */
#endif /* ! MULTIBOOT_HEADER */

126
x86/src/c/print.c Normal file
View File

@ -0,0 +1,126 @@
#include "kdef.h"
#include "type.h"
#include "mem.h"
#include "print.h"
uint32 text_pos;
uint32 HYPKERNEL32 hk_str_len(char const * str)
{
uint32 length = 0;
if(str == NULL)
return 0;
while(*str != 0)
{
str++;
length++;
}
return length;
}
uint32 HYPKERNEL32 hk_str_cmp(char const * str1,char const * str2)
{
if(str1 == NULL || str2 == NULL)
return 0;
uint32 length = hk_str_len(str1);
if(length != hk_str_len(str2))
return 0;
while(length--)
{
if(*(str1+length) != *(str2+length))
return 0;
}
return 1;
}
void HYPKERNEL32 hk_print_scroll()
{
hk_mem_move((void*)(0xb8000 + get_pos(1,0) * 2), (void*)(0xb8000 + get_pos(0,0) * 2), (80*24)*2);
return;
}
void HYPKERNEL32 hk_print_str(char const *str)
{
if(str == NULL)
return;
while (*str != 0)
{
if(*str == '\n')
{
text_pos = 80 * (get_row(text_pos) + 1);
if(text_pos > 80 * 25 - 1)
{
//can't hold
hk_print_scroll();
text_pos = 80 * 24;
}
str++;
}
else
{
if (text_pos > 80 * 25 - 1)
{
//can't hold
hk_print_scroll();
text_pos = 80 * 24;
}
*((char*)(0xb8000) + text_pos*2) = *str;
*((char*)(0xb8000) + text_pos*2 + 1) = 7;
str++;
text_pos++;
}
}
return;
}
void HYPKERNEL32 hk_print_int(int32 number)
{
char arr[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint32 index = 10;
uint32 isNegative = 0;
uint32 const div = 10;
if (number < 0)
{
isNegative = 1;
number *= -1;
}
while (1)
{
uint32 quo = number / div;
uint32 rmd = number % div;
number = quo;
arr[index] = (char) ('0' + rmd);
index--;
if (number == 0)
break;
}
if (isNegative)
{
arr[index] = '-';
hk_print_str(&(arr[index]));
}
else
hk_print_str(&(arr[index+1]));
return;
}
void HYPKERNEL32 hk_print_hex(uint32 number)
{
const char lookup_table[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char arr[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
uint32 index = 9;
uint32 const div = 16;
while (1)
{
uint32 quo = number / div;
uint32 rmd = number % div;
number = quo;
arr[index--] = lookup_table[rmd];
if (number == 0)
break;
}
arr[index--] = 'x';
arr[index] = '0';
hk_print_str(&(arr[index]));
return;
};

16
x86/src/c/print.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef _PRINT_H_
#define _PRINT_H_
#include "type.h"
#include "kdef.h"
#define get_column(pos) (pos % 80)
#define get_row(pos) (pos / 80)
#define get_pos(row,col) ((row) * 80 + (col))
void HYPKERNEL32 hk_print_hex(uint32 number);
void HYPKERNEL32 hk_print_int(int32 number);
void HYPKERNEL32 hk_print_str(char const *str);
uint32 HYPKERNEL32 hk_str_len(char const * str);
uint32 HYPKERNEL32 hk_str_cmp(char const * str1,char const * str2);
#endif

View File

@ -8,8 +8,4 @@ typedef int int32;
typedef short int16;
typedef long long int64;
typedef char int8;
typedef uint8 byte;
typedef uint16 word;
typedef uint32 dword;
typedef uint64 qword;
#endif

View File

@ -1,31 +0,0 @@
#include "type32.h"
#include "grub.h"
#include "mem32.h"
#include "kdef32.h"
uint64 g_gdt[32];
gdt_ptr g_gdt_ptr;
uint32 HYPKERNEL32 hk_main(multiboot_info_t* multibootInfo)
{
int32 gdtIdx = 0;
//empty segment
hk_set_segment_descriptor(&g_gdt[gdtIdx++], 0x0, 0x0, 0x0, 0x0);
//ring 0 code segment
hk_set_segment_descriptor(&g_gdt[gdtIdx++], 0x0, 0xFFFFF, 0x9A, 0xC);
//ring 3 code segment
hk_set_segment_descriptor(&g_gdt[gdtIdx++], 0x0, 0xFFFFF, 0xFA, 0xC);
//ring 0 data
hk_set_segment_descriptor(&g_gdt[gdtIdx++], 0x0, 0xFFFFF, 0x92, 0xC);
//ring 3 data
hk_set_segment_descriptor(&g_gdt[gdtIdx++], 0x0, 0xFFFFF, 0xF2, 0xC);
g_gdt_ptr.limit = 32*8-1;
g_gdt_ptr.base = g_gdt;
//setup new gdt
//setup paging
//setup interrupt
//setup process
return 0;
}

View File

View File

@ -1,2 +0,0 @@

View File

@ -1,10 +0,0 @@
#ifndef _IO32_H_
#define _IO32_H_
#include "type32.h"
extern void hk_write_port(uint16 port, uint16 data);
extern void hk_read_port(uint16 port, uint16 data);
#endif

View File

@ -1,11 +0,0 @@
#ifndef _HKDEF32_H_
#define _HKDEF32_H_
#define HYPKERNEL32 __cdecl
#define NULL ((void*)0)
#define GDT_SELECTOR(Index,RPL) (((Index) << 3) + (RPL))
#define HKERNEL_ADDR (0x01000000)
#endif

View File

View File

@ -1,12 +0,0 @@
#ifndef _UTIL_H_
#define _UTIL_H_
#include "io32.h"
#include "kdef32.h"
int32 HYPKERNEL32 hk_clear_bit(void* dst, uint32 bit);
int32 HYPKERNEL32 hk_get_bit(void* dst, uint32 bit);
int32 HYPKERNEL32 hk_toggle_bit(void* dst, uint32 bit);
int32 HYPKERNEL32 hk_memcpy(void* src, void* dst, uint32 size);
int32 HYPKERNEL32 hk_memmove(void* src, void* dst, uint32 size);
#endif