Triple fault enabling multitasking

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
goku
Member
Member
Posts: 29
Joined: Sat Oct 25, 2008 11:32 am
Location: until we meet again!!

Triple fault enabling multitasking

Post by goku »

I am enabling multitasking and the system triple faults when i try to switch to cloned directory after enabling paging.
Followed a similar post here

Here is the output from my bochslog:

Code: Select all

00045988034i[BIOS ] Booting from 0000:7c00
00083250000i[XGUI ] charmap update. Font Height is 16
00096750000i[XGUI ] charmap update. Font Height is 16
00139500000i[XGUI ] charmap update. Font Height is 16
00820201554i[CPU  ] CPU is in protected mode (active)
00820201554i[CPU  ] CS.d_b = 32 bit
00820201554i[CPU  ] SS.d_b = 32 bit
00820201554i[CPU  ] | EAX=c0083000  EBX=00026260  ECX=000b8000  EDX=000003d5
00820201554i[CPU  ] | ESP=00067d08  EBP=00067d18  ESI=000263cf  EDI=00026419
00820201554i[CPU  ] | IOPL=0 id vip vif ac vm RF nt of df IF tf SF zf af PF cf
00820201554i[CPU  ] | SEG selector     base    limit G D
00820201554i[CPU  ] | SEG sltr(index|ti|rpl)     base    limit G D
00820201554i[CPU  ] |  CS:0008( 0001| 0|  0) 00000000 000fffff 1 1
00820201554i[CPU  ] |  DS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00820201554i[CPU  ] |  SS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00820201554i[CPU  ] |  ES:0010( 0002| 0|  0) 00000000 000fffff 1 1
00820201554i[CPU  ] |  FS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00820201554i[CPU  ] |  GS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00820201554i[CPU  ] | EIP=00102b1d (00102b1d)
00820201554i[CPU  ] | CR0=0xe0000011 CR1=0 CR2=0x001080f0
00820201554i[CPU  ] | CR3=0xc0083000 CR4=0x00000000
00820201554e[CPU  ] exception(): 3rd (14) exception with no resolution, shutdown status is 00h, resetting


and "info idt" gives

Code: Select all

Interrupt Descriptor Table (base=0x00108080, limit=2047):
IDT[0x00]=32-Bit Interrupt Gate target=0x0008:0x0010004e, DPL=0
IDT[0x01]=32-Bit Interrupt Gate target=0x0008:0x00100058, DPL=0
IDT[0x02]=32-Bit Interrupt Gate target=0x0008:0x00100062, DPL=0
IDT[0x03]=32-Bit Interrupt Gate target=0x0008:0x0010006c, DPL=0
IDT[0x04]=32-Bit Interrupt Gate target=0x0008:0x00100076, DPL=0
IDT[0x05]=32-Bit Interrupt Gate target=0x0008:0x00100080, DPL=0
IDT[0x06]=32-Bit Interrupt Gate target=0x0008:0x0010008a, DPL=0
IDT[0x07]=32-Bit Interrupt Gate target=0x0008:0x00100094, DPL=0
IDT[0x08]=32-Bit Interrupt Gate target=0x0008:0x0010009e, DPL=0
IDT[0x09]=32-Bit Interrupt Gate target=0x0008:0x001000a6, DPL=0
IDT[0x0a]=32-Bit Interrupt Gate target=0x0008:0x001000b0, DPL=0
..........and so on....

and "info gdt"

Code: Select all

Global Descriptor Table (base=0x00108040, limit=39):
GDT[0x00]=??? descriptor hi=0x00000000, lo=0x00000000
GDT[0x01]=Code segment, laddr=00000000, limit=fffff * 4Kbytes, Execute/Read, 32-bit
GDT[0x02]=Data segment, laddr=00000000, limit=fffff * 4Kbytes, Read/Write, Accessed
GDT[0x03]=Code segment, laddr=00000000, limit=fffff * 4Kbytes, Execute/Read, 32-bit
GDT[0x04]=Data segment, laddr=00000000, limit=fffff * 4Kbytes, Read/Write
and "info tab" gives

Code: Select all

cr3: 0x0010c000
0x00000000-0x0018ffff -> 0x00000000-0x0018ffff
0xc0000000-0xc0ffffff -> 0x00190000-0x0118ffff
when the system faults bochs debugger gives the following at the terminal

Code: Select all

(0).[3097666632] [0x00101b1d] 0008:00102b1d (unk. ctxt): add eax, 0x00000001       ; 83c001
Next at t=3097666633
(0) [0xfffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b         ; ea5be000f0
I am refering to JamesM tutorial and using gcc, ld, nasm, bochs for development and testing

Thanks.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Triple fault enabling multitasking

Post by Combuster »

CR3=0xc0083000
Start with putting a physical address in CR3
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
goku
Member
Member
Posts: 29
Joined: Sat Oct 25, 2008 11:32 am
Location: until we meet again!!

Re: Triple fault enabling multitasking

Post by goku »

Thanks. I wasn't loading the correct physical address into cr3.

But now i am getting a page fault while executing the move_stack function

Code: Select all

void init_tasking()
{
  asm volatile("cli");  
  move_stack((void*)0xE0000000, 0x2000);
 
  //initialize the first task(kernel task
  current_task = ready_queue = (task_t*)kmalloc(sizeof(task_t));
  current_task->id = next_pid++;
  current_task->esp = current_task->ebp = 0;
  current_task->eip = 0;
  current_task->page_directory = current_directory;
  current_task->next = 0;
  
  monitor_write("\ndone init_tasking()");
  asm volatile("sti");
}

void move_stack(void *new_stack_start, u32int size)
{
  u32int i;
  for( i = (u32int)new_stack_start; i >= ((u32int)new_stack_start-size); i -= 0x1000 )
  {
    alloc_frame( get_page(i, 1, current_directory), 0, 1);
  }
  u32int pd_addr;
  asm volatile("mov %%cr3, %0" : "=r" (pd_addr));
  asm volatile("mov %0, %%cr3" :: "r" (pd_addr));
  
  u32int old_stack_pointer;
  asm volatile("mov %%esp, %0" : "=r" (old_stack_pointer));
  u32int old_base_pointer;
  asm volatile("mov %%ebp, %0" : "=r" (old_base_pointer));
  
  u32int offset = (u32int)new_stack_start - initial_esp;
  
  u32int new_stack_pointer = old_stack_pointer + offset;
  u32int new_base_pointer = old_base_pointer + offset;
  
  memcpy((void*)new_stack_pointer, (void*)old_stack_pointer, initial_esp-old_stack_pointer);
  
  //bactrace through the original stack
  for(i = (u32int)new_stack_start; i > (u32int)new_stack_start-size; i -= 4)
  {
    u32int tmp = *(u32int*)i;
    
    if((old_stack_pointer < tmp) && (tmp <initial_esp))
    {
      tmp = tmp + offset;
      u32int *tmp2 = (u32int *)i;
      *tmp2 = tmp;
    }
  }
  
  asm volatile("mov %0, %%esp" :: "r" (new_stack_pointer));
  asm volatile("mov %0, %%ebp" :: "r" (new_base_pointer));
}
User avatar
goku
Member
Member
Posts: 29
Joined: Sat Oct 25, 2008 11:32 am
Location: until we meet again!!

Re: Triple fault enabling multitasking

Post by goku »

some more info.

The page fault occurs at the following instruction

Code: Select all

memcpy((void*)new_stack_pointer, (void*)old_stack_pointer, initial_esp-old_stack_pointer);
The display message i am getting on the screen

Code: Select all

interrupt handled : 0xe
Page Fault
Page fault! ( present read-only ) at 0x0xe005f14e - EIP: 0x100332
Also i used the bochs internal debugger but i cant really understand all the parts of the following

Code: Select all

<bochs:92> s
Next at t=147759916
(0) [0x0010092a] 0008:0010092a (unk. ctxt): add eax, eax              ; 01c0

So if someone could explain the significance of the various part of the above statements(..apart from the assembly instruction ofcourse.. :)..and my  page fault problem )

Cheers.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Triple fault enabling multitasking

Post by Combuster »

<bochs:92> s
<bochs:92>
Command line, with the number of the command
s
the command for single step. Execute this instruction and stop before the next.
Next at t=147759916
clock count. If you don't use the realtime clock, you can use that to track the time when something happens

(0) [0x0010092a] 0008:0010092a (unk. ctxt): add eax, eax ; 01c0
(0) [0x0010092a] 0008:0010092a
Instruction pointer
(unk. ctxt):
usually not interesting
add eax, eax
the next opcode that is to be executed
; 01c0
hex dump of that opcode.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
goku
Member
Member
Posts: 29
Joined: Sat Oct 25, 2008 11:32 am
Location: until we meet again!!

Re: Triple fault enabling multitasking

Post by goku »

Thanks Combuster for the explanation. But i am still stuck with the page fault problem. I do not understand when the following code executes the pages shuold have been allocated.

Code: Select all

for( i = (u32int)new_stack_start; i >= ((u32int)new_stack_start-size); i -= 0x1000 )
  {
    alloc_frame( get_page(i, 1, current_directory), 0, 1);
  }
so memset should just copy the contents of the old stack to the new location..

Code: Select all

memcpy((void*)new_stack_pointer, (void*)old_stack_pointer, initial_esp-old_stack_pointer);
instead it just page faults :x
I was getting a page fault problem while setting up the kernel heap(heap thread). I wonder if it has something to do the kernel heap. I am following JamesM tutorial. I am attaching my source file.

Cheers.
Attachments
src.rar
(31.22 KiB) Downloaded 95 times
User avatar
goku
Member
Member
Posts: 29
Joined: Sat Oct 25, 2008 11:32 am
Location: until we meet again!!

Re: Triple fault enabling multitasking

Post by goku »

Hi

Figured out the problem. Turn out that i was not pushing initial esp on to the stack before calling main as a result the move_stack function which was passed the initial esp of the original stack was garbage. Thanks for the help... :)

Cheers.
Post Reply