stack fault

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
Johnny

stack fault

Post by Johnny »

As soon as I entered PM, I've got stack fault in VMWare
In Bochs, no response, nothing happened..

Here is my very simple source code to enter PM

Code: Select all

; TODO
; 1. Load Kernel at 0x10000
; 2. Enter to the PM
; 3. Jump to Kernel

; 2004-06-15 changed for nasm


[BITS 16]
ORG 0x0000

start:
   db   0x66
   lgdt   [gdt_desc]

   mov   eax,cr0
   or   al,1
   mov   cr0,eax

   jmp   dword 0x0008:0x1000+next


[BITS 32]
next:
   nop
   nop
   
   cli
   mov   ax,10h
   mov   ds,ax
   mov   es,ax
   mov   fs,ax
   mov   gs,ax
   mov   ss,ax
   mov   esp,(2*1024*1024)
   sti
   
   mov   byte [0x0b8000],'X'
   mov   byte [0x0b8001],0x1b

hang:
   jmp hang

   ;IMPL ME
   ;Enter to the Kernel
   ;call   _test 

gdt_desc:
      dw   0x0004*0x08
      dd   gdt+0x1000
;gdt_48:;
;   dw   0x0004*0x08      ; gdt limit=4 max GDT entries
;   dd   BIG_GDT+0x7c00   ; gdt base

;------------------------------------------------------------------------------
; NULL DESCRIPTER
;------------------------------------------------------------------------------
ALIGN   4
gdt   db      0,0,0,0
   db      0,0,0,0

;------------------------------------------------------------------------------
; DESCRIPTER #1
; Index 08h
; Base 0 / Limit FFFFFFFFh
; Code/Readable/Nonconforming
; Level 0
;------------------------------------------------------------------------------
   dw      0ffffh      ; 0-15   Limit
   dw      0h      ; 16-31   Base
   db      0h      ; 0-7   Base
   db      10011010b   ; 8-15   Type Privilege Present
   db      11001111b   ; 16-23   Limit Attr Granularity
   db      0h      ; 24-31 Base

;------------------------------------------------------------------------------
; DESCRIPTER #2
; Index 10h
; Base 0 / Limit FFFFFFFFh
; Data/
; Level 0
;------------------------------------------------------------------------------
   dw      0ffffh      ; 0-15   Limit
   dw      0h      ; 16-31   Base
   db      0h      ; 0-7   Base
   db      10010010b   ; 8-15   Type Privilege Present
   db      11001111b   ; 16-23   Limit Attr Granularity
   db      0h      ; 24-31 Base

gdt_size   equ   $ - gdt ;in bytes

   END
My bootloader just load this code at 0x1000 and then jump.
What is expeted is to print 'X' out..

In VMWare It is printed out and then Stack Fault
In Bochs no print out and hang...

What is the problem...?
I think stack is set up well...
umm...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:stack fault

Post by Brendan »

Hi,

Could be that interrupts are disabled while you turn protected mode on, and there's no IDT after interrupts are enabled again.

Code: Select all

[BITS 16]
ORG 0x0

start:
   lgdt [gdt_desc]
   cli
   mov   eax,cr0
   or   al,1
   mov   cr0,eax
   jmp   dword 0x0008:next

[BITS 32]
next:
   mov   ax,10h
   mov   ds,ax
   mov   es,ax
   mov   fs,ax
   mov   gs,ax
   mov   ss,ax
   mov   esp,(2*1024*1024)
   
   mov   byte [0x0b8000],'X'
   mov   byte [0x0b8001],0x1b

hang:
   jmp hang

;------------------------------------------------------------------------------
; NULL DESCRIPTOR and gdt_desc
;------------------------------------------------------------------------------
ALIGN   16
gdt:
gdt_desc:
      dw   0x0004*0x08
      dd   gdt
ALIGN   16

{snipped - other descriptors same as before}
I've also messed with the ORG - you'd need to use "jmp 0x0:0x1000" instead of "jmp 0x100:0x0" in your boot sector if you want to keep the different ORG...

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Johnny

Re:stack fault

Post by Johnny »

You're right..
It was because of interrupt..

After I made sti comment, The error was gone..

What do I need to do then?
Do I need to set IDT?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:stack fault

Post by Brendan »

Hi,
Johnny wrote: Do I need to set IDT?
Yes. When an interrupt occurs the CPU looks at the IDT to determine how to handle the interrupt (what to put in CS and EIP to start the interrupt handler).

Without an IDT the CPU won't be able to handle IRQ's, exceptions or software interrupts (if used).

Also the CPU uses interrupts 0 to 31 for exceptions - this can't be changed. There's 2 chips called the PIC chips (Programmable Interrupt Controllers) that determine which interrupt is used for each IRQ (8 IRQs are connected to each PIC chip). By default the IRQs connected to the first PIC chip use interrupts 8 to 15, which conflicts with interrupts used for the CPU's exceptions.

There's a page explaining how to avoid this mess by re-programming the PIC chips:
http://www.osdev.org/osfaq2/index.php/C ... e%20PIC%3F


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply