Page 1 of 1

Task switching issues

Posted: Fri Dec 03, 2021 11:51 am
by iProgramInCpp
Hi! I've restarted development of my OS (with the bare minimum of features) and I kind of need help again with task switching. It crashes randomly unless I push the task switching asm code 400kb using a resb (Probably some invalid address its using)

Source: https://github.com/iProgramMC/OSProject

Here's what I get

Code: Select all

Triple fault
CPU Reset (CPU 0)
EAX=001026fe EBX=001c6c64 ECX=00205ecc EDX=0000012b
ESI=0000020f EDI=000b851a EBP=00205f4c ESP=001026fe
EIP=00102717 EFL=00000016 [----AP-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9a00 DPL=0 CS32 [-R-]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     00102630 00000017
IDT=     00106040 00000800
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=00000008 CCD=00205ed4 CCO=ADDL
EFER=0000000000000000
FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000
Triple fault
CPU Reset (CPU 0)
EAX=000f5e06 EBX=00000000 ECX=00000000 EDX=00000cf9
ESI=00000000 EDI=00100000 EBP=00000000 ESP=00000fb4
EIP=000f1732 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f5e80 00000037
IDT=     000f5ebe 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000
DR6=ffff0ff0 DR7=00000400
CCS=000f5ec8 CCD=0000a134 CCO=SUBL
EFER=0000000000000000
FCW=037f FSW=0000 [ST=0] FTW=00 MXCSR=00001f80
FPR0=0000000000000000 0000 FPR1=0000000000000000 0000
FPR2=0000000000000000 0000 FPR3=0000000000000000 0000
FPR4=0000000000000000 0000 FPR5=0000000000000000 0000
FPR6=0000000000000000 0000 FPR7=0000000000000000 0000
XMM00=00000000000000000000000000000000 XMM01=00000000000000000000000000000000
XMM02=00000000000000000000000000000000 XMM03=00000000000000000000000000000000
XMM04=00000000000000000000000000000000 XMM05=00000000000000000000000000000000
XMM06=00000000000000000000000000000000 XMM07=00000000000000000000000000000000
and I can see that's on the iretd instruction

Re: Task switching issues

Posted: Mon Dec 06, 2021 8:48 am
by iProgramInCpp
Looked into the issue a bit more but I still can't find the error. I'm kind of lost right now

Re: Task switching issues

Posted: Mon Dec 06, 2021 11:14 am
by iProgramInCpp
Ok, I tracked down the issue. You see, in task.c I have a variable named CPUSaveState* g_saveStateToRestore = NULL; that I use in RestoreKernelTask. Using a debugging tool I have I can find that the address is .bss:29F978. Then I make reference of it in ktask.asm (I use NASM to compile asm files) and the code looks something like this:

Code: Select all

extern g_saveStateToRestore

OnStartedNewTask:
OnStartedNewKernelTask:
    ;cli
    ; restore the registers now
    mov esp, [g_saveStateToRestore]
The problem is that the value used by the asm's g_saveStateToRestore does NOT have the same address as the one inside the C file. Instead it's been moved 0x100 bytes forward to .bss:29FA78. It's really strange.

FYI: The dumps:

Code: Select all

.bss:0029F978 ; CPUSaveState_0 *g_saveStateToRestore
.bss:0029F978 g_saveStateToRestore dd ?               ; DATA XREF: RestoreKernelTask+6↑w
.bss:0029F978                                         ; RestoreStandardTask+C↑w
              ... (0x100 bytes later)
.bss:0029FA78 dword_29FA78    dd ?                    ; DATA XREF: OnStartedNewTask↑r

Re: Task switching issues

Posted: Mon Dec 06, 2021 12:53 pm
by iProgramInCpp
Guess the decompiler was the issue, haha. Anyways I tracked it down to my memory allocator of all things starting allocation too early (at 0x200000, but the kernel bss section went till 0x2A0000).