1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00

Some logical changes and improvements

This commit is contained in:
Adrien Bourmault 2019-05-13 15:18:00 +02:00
parent 7e3d6c4817
commit 3a42a811d2
9 changed files with 66 additions and 54 deletions

View File

@ -210,7 +210,7 @@ gdb: all
ddd: all
@qemu-system-x86_64 -m 64M -hda $(BUILDDIR)/bin/disk.img -no-reboot -soundhw pcspk \
-no-shutdown -d cpu_reset,guest_errors,pcall,int -s -S 2> $(BUILDDIR)/qemu.log &
-no-shutdown -d cpu_reset,guest_errors,pcall,int -s 2> $(BUILDDIR)/qemu.log &
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 64 > kaleid64_disasm.asm
@ndisasm $(BINDIR)/kaleid -o 0x00100000 -b 32 > kaleid32_disasm.asm
@ddd

View File

@ -162,8 +162,8 @@ _loader64:
call write
call temporize ; clear the pipeline
call InitStack
jmp InitStack
AfterInitStack:
;; Launch the kernel !
mov bl, 0x0F
mov esi, GoKernel

View File

@ -118,21 +118,26 @@ CheckA20:
[BITS 64]
; ---------------------------------------------------------------------------- ;
; Initilizes the stack ;
; ;
; Not a function because it wipes the stack ;) ;
; ---------------------------------------------------------------------------- ;
; XXX : MAKE A PAGE GUARD FOR THE STACK
InitStack:
xor rax, rax ; "Fill pattern"
push rcx
push rdi
;; Begin address to fill and length
mov qword [realKernelEnd], kernelEnd
mov qword [newKernelEnd], KERNEL_STACK
mov qword [newStackEnd], KERNEL_STACK
mov qword [newKernelEnd], kernelEnd
mov qword [kernelEnd], qword 0xbad0bad
mov rdi, kernelEnd + 16
mov rcx, KERNEL_STACK - kernelEnd - 16 * 2
; The Stack can begin at kernelEnd + 16 in order to not overwrite the
; kernel by pushing
; We must stop before the return address in the current stack so -16 in rcx
mov rcx, (KERNEL_STACK - (kernelEnd + 16)) ; The Stack can begin at
; kernelEnd + 16 in order to not overwrite the
; kernel by pushing values (grows downward)
;; XXX : align the stack to 16bytes
;; If bit 0 is on, fill one byte
sar rcx, 1 ; Shift bit 0 into CY
@ -148,9 +153,10 @@ InitStack:
sar rcx, 1 ; Shift bit 2 into CY
jnc $ + 3
stosd
;; RCX now equals the number of qwords to fill
repnz stosq ; Finish by writing RCX qwords.
pop rdi
pop rcx
ret
jmp AfterInitStack

View File

@ -27,12 +27,12 @@
extern kernelEnd
extern _bss
global newKernelEnd
global realKernelEnd
global newStackEnd
[section .text]
KERNEL_STACK equ kernelEnd + 16 + 4096 * 2 * 1024 ; 8MB of stack
KERNEL_STACK equ (kernelEnd + 16) + 16 * 1024 ; 16KB of stack
newKernelEnd dq 0x0
realKernelEnd dq 0x0
newStackEnd dq 0x0
[section .rodata]
;; GDT WITH DOC

View File

@ -48,6 +48,7 @@ struct BootInfo_t
void *kernelAddr;
void *codeSegment;
void *kernelEndAddr;
void *stackEndAddr; // stack begins 16B after kernelEndAddr
} btldr;
// Informations about drives

View File

@ -33,6 +33,7 @@ void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg)
{
extern ulong MB_header;
extern ulong newKernelEnd;
extern ulong newStackEnd;
// We need the multiboot structure
KalAlwaysAssert(mbi);
@ -44,6 +45,7 @@ void BtInitBootInfo(multiboot_info_t *mbi, void *codeSeg)
BtLoaderInfo.grubName = (char*)(ulong)(mbi->boot_loader_name);
BtLoaderInfo.kernelAddr = (void*)&MB_header;
BtLoaderInfo.kernelEndAddr = (void*)newKernelEnd;
BtLoaderInfo.stackEndAddr = (void*)newStackEnd;
BtLoaderInfo.codeSegment = codeSeg;
BtLoaderInfo.valid = 1;
}

View File

@ -45,7 +45,6 @@ static Lock_t _heap_lock = ExINITLOCK(KLOCK_SPINLOCK);
void MmInitHeap(void)
{
assert(_heap_end == NULL);
MmIsHeapSmashed();
// Get the first available zone address
_heap_start = MmGetFirstAvailZone((void*)0);
@ -82,7 +81,6 @@ void MmUnlockHeap(void)
//
size_t MmGetHeapSize(void)
{
MmIsHeapSmashed();
return (size_t)_heap_end - (size_t)_heap_start;
}
@ -91,7 +89,7 @@ size_t MmGetHeapSize(void)
//
size_t MmGetMaxHeapSize(void)
{
MmIsHeapSmashed();
return _heap_max;
}
@ -100,7 +98,7 @@ size_t MmGetMaxHeapSize(void)
//
error_t MmSetMaxHeapSize(size_t new)
{
MmIsHeapSmashed();
if (new > MmGetAvailZoneSize((void *)_heap_start)) {
return ENOMEM;
}
@ -119,7 +117,7 @@ error_t MmSetMaxHeapSize(size_t new)
//
error_t MmGrowHeap(size_t req)
{
MmIsHeapSmashed();
assert(req % alignof(QWORD) == 0);
if ((size_t)_heap_end + req > (size_t)_heap_start + _heap_max) {
@ -137,7 +135,7 @@ error_t MmGrowHeap(size_t req)
//
error_t MmShrinkHeap(size_t req)
{
MmIsHeapSmashed();
assert(req % alignof(QWORD) == 0);
if (req > (size_t)_heap_end - (size_t)_heap_start) {
@ -150,12 +148,4 @@ error_t MmShrinkHeap(size_t req)
return EOK;
}
//
// The incarnate paranoia
//
void MmIsHeapSmashed(void)
{
ulong *heapStart = BtLoaderInfo.kernelEndAddr + 8;
if (*heapStart != 0xbad00badbad00bad)
KeStartPanic("Heap has been smashed !\n");
}

View File

@ -107,7 +107,7 @@ static error_t InitMemoryMap(void)
(memoryMap.freeRamSize + memoryMap.nonfreeRamSize) / MB);*/
// Magic value in memory to prevent smashing
ulong * heapStart = BtLoaderInfo.kernelEndAddr + 8;
ulong * heapStart = BtLoaderInfo.stackEndAddr + 8;
*heapStart = 0xbad00badbad00bad;
return EOK;
@ -117,7 +117,7 @@ size_t MmGetAvailZoneSize(void *start) {
uint i;
// Because the kernel is the kernel
if (start < BtLoaderInfo.kernelEndAddr + 16)
if (start < BtLoaderInfo.stackEndAddr + 16)
return 0;
// Search the zone where the start address is
@ -142,8 +142,8 @@ void *MmGetFirstAvailZone(void *start) {
void *current = 0;
// Because the kernel is the kernel
if ((ulong)start < (ulong)BtLoaderInfo.kernelEndAddr+16) {
return MmGetFirstAvailZone(BtLoaderInfo.kernelEndAddr+16);
if ((ulong)start < (ulong)BtLoaderInfo.stackEndAddr+16) {
return MmGetFirstAvailZone(BtLoaderInfo.stackEndAddr+16);
}
// Search the zone where the start address is

View File

@ -80,8 +80,6 @@ error_t CmdMemMap(int argc, char **argv, char *cmdline)
return EOK;
}
extern ulong realKernelEnd;
error_t CmdMemUsage(int argc, char **argv, char *cmdline)
{
size_t stack_cur;
@ -97,10 +95,10 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline)
KeRestoreIRQs(flags);
img_diff = realKernelEnd - (size_t)BtLoaderInfo.kernelAddr;
stack_diff = (size_t)BtLoaderInfo.kernelEndAddr - (realKernelEnd + 16);
img_diff = (size_t)BtLoaderInfo.kernelEndAddr - (size_t)BtLoaderInfo.kernelAddr;
stack_diff = (size_t)BtLoaderInfo.stackEndAddr - ((size_t)BtLoaderInfo.kernelEndAddr + 16);
heap_diff = (size_t)heap_end - (size_t)heap_start;
KernLog("Kernel image\n");
KernLog("\tstarts at:\t\t%p (%4luMB + %4luKB + %4luB)\n",
@ -110,10 +108,10 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline)
_ADDR_TO_B((size_t)BtLoaderInfo.kernelAddr));
KernLog("\tends at:\t\t%p (%4luMB + %4luKB + %4luB)\n",
realKernelEnd,
_ADDR_TO_MB(realKernelEnd),
_ADDR_TO_KB(realKernelEnd),
_ADDR_TO_B(realKernelEnd));
(size_t)BtLoaderInfo.kernelEndAddr,
_ADDR_TO_MB((size_t)BtLoaderInfo.kernelEndAddr),
_ADDR_TO_KB((size_t)BtLoaderInfo.kernelEndAddr),
_ADDR_TO_B((size_t)BtLoaderInfo.kernelEndAddr));
KernLog("\tsize:\t\t\t%4luMB + %4luKB + %4luB (%p)\n",
_ADDR_TO_MB(img_diff),
@ -125,27 +123,27 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline)
KernLog("\tstarts at:\t\t%p (%4luMB + %4luKB + %4luB)\n",
BtLoaderInfo.kernelEndAddr,
_ADDR_TO_MB((size_t)BtLoaderInfo.kernelEndAddr),
_ADDR_TO_KB((size_t)BtLoaderInfo.kernelEndAddr),
_ADDR_TO_B((size_t)BtLoaderInfo.kernelEndAddr));
_ADDR_TO_MB((size_t)BtLoaderInfo.stackEndAddr),
_ADDR_TO_KB((size_t)BtLoaderInfo.stackEndAddr),
_ADDR_TO_B((size_t)BtLoaderInfo.stackEndAddr));
BARRIER();
char var;
(void)var;
stack_cur = (size_t)BtLoaderInfo.kernelEndAddr - (size_t)&var;
stack_cur = (size_t)BtLoaderInfo.stackEndAddr - (size_t)&var;
KernLog("\tends at:\t\t%p (%4luMB + %4luKB + %4luB)\n",
stack_cur,
_ADDR_TO_MB(stack_cur),
_ADDR_TO_KB(stack_cur),
_ADDR_TO_B(stack_cur));
(size_t)&var,
_ADDR_TO_MB((size_t)&var),
_ADDR_TO_KB((size_t)&var),
_ADDR_TO_B((size_t)&var));
KernLog("\tmin addr:\t\t%p (%4luMB + %4luKB + %4luB)\n",
realKernelEnd+1,
_ADDR_TO_MB(realKernelEnd+1),
_ADDR_TO_KB(realKernelEnd+1),
_ADDR_TO_B(realKernelEnd+1));
(size_t)BtLoaderInfo.kernelEndAddr+16,
_ADDR_TO_MB((size_t)BtLoaderInfo.kernelEndAddr+16),
_ADDR_TO_KB((size_t)BtLoaderInfo.kernelEndAddr+16),
_ADDR_TO_B((size_t)BtLoaderInfo.kernelEndAddr+16));
KernLog("\tsize (cur):\t\t%4luMB + %4luKB + %4luB (%p)\n",
_ADDR_TO_MB(stack_cur),
@ -206,6 +204,19 @@ error_t CmdDie(int argc, char **argv, char *cmdline)
return EOK;
}
error_t CmdPF(int argc, char **argv, char *cmdline)
{
*((char*)0xDEADBEEF0) = 1;
return EOK;
}
error_t CmdShell(int argc, char **argv, char *cmdline)
{
extern void KeStartShell(void);
KeStartShell();
return EOK;
}
//----------------------------------------------------------------------------//
Command_t cmdtable[] =
@ -218,7 +229,9 @@ Command_t cmdtable[] =
{ "march", CmdStarWars, "Play the Imperial March"},
{ "mmap", CmdMemMap, "Show memory map" },
{ "musage", CmdMemUsage, "Show memory statistics" },
{ "pfault", CmdPF, "Provokes a PF" },
{ "pstest", CmdPsTest, "Scheduler test routine" },
{ "shell", CmdShell, "New shell instance" },
{ "quit", CmdQuit, "Alias for 'exit'" },
{ NULL, NULL, NULL }
};