Page 1 of 1

Kernel Stack Fault... can't find it.

Posted: Thu Jul 31, 2008 2:16 pm
by Stachelsk
Bootsector failing...

I fixed all my issues with triple faulting and the like and got my bootloader to get me into protected mode, but now I'm getting 'kernel stack fault (hardware reset)' messages in VMware. I know it doesn't have debugging capabilities (as far as I know)... but there's not much choice when you're developing on a mac.

Anyways, here is my code so far; problem is at the end of it all.


BOOT.ASM

Code: Select all

; --------------------------------------------------------------------------------
;	Celeriter Operating System
;
;	This bootsector boots into protected mode and prepares the system.
; --------------------------------------------------------------------------------

	[BITS 16]
	[ORG 0x7C00]

	JMP START

	%INCLUDE "DISPLAY.ASM"	
	%INCLUDE "GDT.ASM"
	
; --------------------------------------------------------------------------------
;  Code for the boot sector actually starts here, after the data.
; --------------------------------------------------------------------------------
START:
	[BITS 16]

	CLI
	CLD

; --------------------------------------------------------------------------------
;  Copy the global descriptor table to memory, starting at 0x1000.
; --------------------------------------------------------------------------------

	XOR AX, AX
	MOV DS, AX
	MOV SI, GDT

	MOV ES, AX
	MOV DI, 0x1000
	
	MOV CX, 3*8
	
COPY:
	LODSW
	STOSW
	LOOP COPY

; --------------------------------------------------------------------------------
;  Clear out DS register, enable A20, enable protected mode, and jump to it.
; --------------------------------------------------------------------------------
	MOV DS, CX

ENABLE_A20:
	CALL A20WAIT1
	MOV AL, 0xAD
	OUT 0x64, AL
	
	CALL A20WAIT1
	MOV AL, 0xD0
	OUT 0x64, AL
	
	CALL A20WAIT2
	IN AL, 0x60
	PUSH EAX
	
	CALL A20WAIT1
	POP EAX
	OR AL, 0x02
	OUT 0x60, AL
	
	CALL A20WAIT1
	MOV AL, 0xAE
	OUT 0x64, AL
	
	CALL A20WAIT1
	
ENABLE_PMODE:
	LGDT [GDT_D]

 	MOV CR0, EAX
 	OR AL, 0x01
 	MOV EAX, CR0
 
	JMP GDT_CODE:PMODE

A20WAIT1:
	IN AL, 0x64
	TEST AL, 0x02
	JNZ A20WAIT1
	RET

A20WAIT2:
	IN AL, 0x64
	TEST AL, 0x01
	JZ A20WAIT2
	RET

; --------------------------------------------------------------------------------
;  System is now in protected mode, update segment registers quickly.
; --------------------------------------------------------------------------------
PMODE:
	[BITS 32]
	
	MOV AX, 0x10
	MOV DS, AX
	MOV SS, AX
	
; --------------------------------------------------------------------------------
;  For now, a 4k stack is reserved at the beginning of memory.
; --------------------------------------------------------------------------------
	MOV ESP, 0x00001000

; --------------------------------------------------------------------------------
;  Print a message and let the user know all is well in protected land.
; --------------------------------------------------------------------------------
	CALL CLEAR_SCREEN
	
	MOV BYTE[0x000B8002], 'X'
	MOV BYTE[0x000B8003], 0x0F
	
HANG:
	JMP HANG

; --------------------------------------------------------------------------------
;  Needs to be at the end of the boot sector in order to work properly.
; --------------------------------------------------------------------------------
	TIMES 510-($-$$) DB 0x00
	SIGNATURE DW 0xAA55
GDT.ASM

Code: Select all

; --------------------------------------------------------------------------------
;	GDT File
;
;	Contains the global descriptor table and entries for protected mode.
; --------------------------------------------------------------------------------

; --------------------------------------------------------------------------------
;  Global descriptor table entries (copied to 0x00000000).
; --------------------------------------------------------------------------------
GDT:

GDT_NULL
GDT_NULL_ENTRY:
	DD  0x00000000
	DD  0x00000000

GDT_CODE EQU	$-GDT_NULL
GDT_CODE_ENTRY:
	DW	0xFFFF
	DW	0x0000
	DB	0x00
	DB	10011010b
	DB	11001111b
	DB	0x00

GDT_DATA EQU	$-GDT_NULL
GDT_DATA_ENTRY:
	DW	0xFFFF
	DW	0x0000
	DB	0x00
	DB	10010010b
	DB	11001111b
	DB	0x00

GDT_END:

; --------------------------------------------------------------------------------
;  Global descriptor table.
; --------------------------------------------------------------------------------
GDT_D:
	DW GDT_END - GDT - 1
	DD 0x00001000
DISPLAY.ASM

Code: Select all

; --------------------------------------------------------------------------------
;	General purpose display routines
;
;	Display routines for clearing the display, writing text, et cetera.
; --------------------------------------------------------------------------------

CLEAR_SCREEN:
	MOV BYTE[DS:0x000B8000], 'X'					;Crashes
	MOV BYTE[DS:0x000B8001], 0x0F					;Here
	PUSH AX
	POP AX
	RET
Whenever I try to call CLEAR_SCREEN from BOOT.ASM after my stack and everything is set, I get a kernel stack fault right where the movs are. If I comment out the movs, the bootloader works as expected. Really confused.


Edit: Memory map (so far) if it helps...

Code: Select all

; --------------------------------------------------------------------------------
;  Celeriter's memory map
;
;  Assuming the processor made it to protected mode.
; --------------------------------------------------------------------------------

0x0000 ---------------------- 0x0000

	        System Stack
			 4096 Bytes

0x1000 ---------------------- 0x1000

	     16 GDT/IDT Entries
	         0128 Bytes

0x1080 ---------------------- 0x1080

			Kernel Image
			 ???? Bytes
			 
0x???? ---------------------- 0x????

Re: Kernel Stack Fault... can't find it.

Posted: Thu Jul 31, 2008 2:27 pm
by Combuster
For starters, use bochs. Gives far more useful information about crashing kernels.

Re: Kernel Stack Fault... can't find it.

Posted: Thu Jul 31, 2008 2:50 pm
by Stachelsk
Keeps repeating this several times over in log...

00032334644e[CPU0 ] write_virtual_checks(): write beyond limit, r/w

Trying to debug still... here's what I get with a successful launch (after removing the movs in DISPLAY.ASM)
00032315893e[KBD ] kbd_ctrl_to_kbd(): got value of 0x03
00032332358e[DEV ] write to port 0x0061 with len 2 ignored
00037220000i[CTRL ] quit_sim called with exit code 0

I wonder if A20 isn't being enabled properly and the memory is wrapping around... hmm.

After enabling A20 on the fast gate as well as through the keyboard...
00032451889i[CPU0 ] WARNING: HLT instruction with IF=0!

Enabling A20 via port 0x92 did something... Bochs no longer has a ton of errors in the log and VMware doesn't crash, but halts instead...

Re: Kernel Stack Fault... can't find it.

Posted: Thu Jul 31, 2008 3:27 pm
by AJ
Hi,

IIRC, that Bochs error does indicate wrapping memory. ESP causing a "write beyond limit" would perhaps indicate that your ESP is at zero and is wrapping downwards (as ESP is decremented on a write).

Can you post the entire Bochs register dump from:

Code: Select all

00032334644e[CPU0 ] write_virtual_checks(): write beyond limit, r/w


...to the end of the output (so we can see ESP, EIP, etc...)?

Cheers,
Adam

Re: Kernel Stack Fault... can't find it.

Posted: Thu Jul 31, 2008 3:29 pm
by AJ
Oh - wait. I've just seen that your first CALL (CALL A20WAIT1) seems to be before you have even initialised ESP, so it must be zeroed and is then wrapping below 0 bytes (to 0xFFFE).

Cheers,
Adam

Re: Kernel Stack Fault... can't find it.

Posted: Thu Jul 31, 2008 3:38 pm
by Stachelsk
Setup a stack at the beginning of the bootsector as such...

Code: Select all

START:
	CLI
	XOR AX, AX
	MOV DS, AX
	MOV SS, AX
	MOV SP, 0x7C00
As soon as I switch to protected mode, I change the location of my stack like so...

Code: Select all

	[BITS 32]

PMODE:
	MOV AX, GDT_DATA
	MOV DS, AX
	MOV SS, AX
	
	MOV ESP, 0x1000
Code still fails however. Still getting a kernel stack fault in VMware, Bochs log is pasted below.
00000000000i[ ] Bochs x86 Emulator 2.3.7
00000000000i[ ] Build from CVS snapshot, on June 3, 2008
00000000000i[ ] System configuration
00000000000i[ ] processors: 1 (cores=1, HT threads=1)
00000000000i[ ] A20 line support: yes
00000000000i[ ] APIC support: yes
00000000000i[ ] CPU configuration
00000000000i[ ] level: 6
00000000000i[ ] SMP support: no
00000000000i[ ] FPU support: yes
00000000000i[ ] MMX support: yes
00000000000i[ ] SSE support: 2
00000000000i[ ] CLFLUSH support: yes
00000000000i[ ] VME support: yes
00000000000i[ ] 3dnow! support: no
00000000000i[ ] PAE support: yes
00000000000i[ ] PGE support: yes
00000000000i[ ] PSE support: yes
00000000000i[ ] x86-64 support: yes
00000000000i[ ] SEP support: yes
00000000000i[ ] MWAIT support: no
00000000000i[ ] XSAVE support: no
00000000000i[ ] AES support: no
00000000000i[ ] Optimization configuration
00000000000i[ ] Guest2HostTLB support: yes
00000000000i[ ] RepeatSpeedups support: yes
00000000000i[ ] Icache support: yes
00000000000i[ ] Trace cache support: yes
00000000000i[ ] Fast function calls: yes
00000000000i[ ] Devices configuration
00000000000i[ ] ACPI support: yes
00000000000i[ ] NE2000 support: yes
00000000000i[ ] PCI support: yes
00000000000i[ ] SB16 support: yes
00000000000i[ ] USB support: yes
00000000000i[ ] VGA extension support: vbe cirrus
00000000000i[MEM0 ] allocated memory at 0x1d00000. after alignment, vector=0x1d00000
00000000000i[MEM0 ] 32.00MB
00000000000i[MEM0 ] rom at 0xfffe0000/131072 ('/Users/Tyler/Downloads/bochs-2.3.7/BIOS-bochs-latest')
00000000000i[MEM0 ] rom at 0xc0000/38400 ('/Users/Tyler/Downloads/bochs-2.3.7/VGABIOS-lgpl-latest')
00000000000i[APIC?] set APIC ID to 0
00000000000i[APIC0] 80686
00000000000i[APIC0] local apic in CPU apicid=00 initializing
00000000000i[ ] lt_dlhandle is 0xe5dfa0
00000000000i[PLGIN] loaded plugin libbx_unmapped.la
00000000000i[ ] lt_dlhandle is 0xe5e070
00000000000i[PLGIN] loaded plugin libbx_biosdev.la
00000000000i[ ] lt_dlhandle is 0xe5e3c0
00000000000i[PLGIN] loaded plugin libbx_cmos.la
00000000000i[ ] lt_dlhandle is 0xe5e730
00000000000i[PLGIN] loaded plugin libbx_dma.la
00000000000i[ ] lt_dlhandle is 0xe5ea50
00000000000i[PLGIN] loaded plugin libbx_pic.la
00000000000i[ ] lt_dlhandle is 0xe5ee40
00000000000i[PLGIN] loaded plugin libbx_vga.la
00000000000i[ ] lt_dlhandle is 0xe5f120
00000000000i[PLGIN] loaded plugin libbx_floppy.la
00000000000i[ ] lt_dlhandle is 0xe5f520
00000000000i[PLGIN] loaded plugin libbx_harddrv.la
00000000000i[ ] lt_dlhandle is 0xe5f640
00000000000i[PLGIN] loaded plugin libbx_keyboard.la
00000000000i[ ] lt_dlhandle is 0xe5f680
00000000000i[PLGIN] loaded plugin libbx_serial.la
00000000000i[ ] lt_dlhandle is 0xe5fc50
00000000000i[PLGIN] loaded plugin libbx_parallel.la
00000000000i[ ] lt_dlhandle is 0xe5ff10
00000000000i[PLGIN] loaded plugin libbx_extfpuirq.la
00000000000i[ ] lt_dlhandle is 0xe60270
00000000000i[PLGIN] loaded plugin libbx_gameport.la
00000000000i[ ] lt_dlhandle is 0xe60450
00000000000i[PLGIN] loaded plugin libbx_speaker.la
00000000000i[ ] lt_dlhandle is 0xe607e0
00000000000i[PLGIN] loaded plugin libbx_pci.la
00000000000i[ ] lt_dlhandle is 0xe609c0
00000000000i[PLGIN] loaded plugin libbx_pci2isa.la
00000000000i[ ] lt_dlhandle is 0xe60c20
00000000000i[PLGIN] loaded plugin libbx_pci_ide.la
00000000000i[ ] lt_dlhandle is 0xe60d60
00000000000i[PLGIN] loaded plugin libbx_acpi.la
00000000000i[IOAP ] initializing I/O APIC
00000000000i[IOAP ] set APIC ID to 1
00000000000i[MEM0 ] Register memory access handlers: 0xfec00000 - 0xfec00fff
00000000000i[CMOS ] Using local time for initial clock
00000000000i[CMOS ] Setting initial clock to: Thu Jul 31 19:17:08 2008 (time0=1217546228)
00000000000i[DMA ] channel 4 used by cascade
00000000000i[DMA ] channel 2 used by Floppy Drive
00000000000i[FDD ] fd0: 'os.img' ro=0, h=2,t=80,spt=18
00000000000i[PCI ] 440FX Host bridge present at device 0, function 0
00000000000i[PCI ] PIIX3 PCI-to-ISA bridge present at device 1, function 0
00000000000i[MEM0 ] Register memory access handlers: 0x000a0000 - 0x000bffff
00000000000i[MEM0 ] Register memory access handlers: 0xe0000000 - 0xe07fffff
00000000000i[CLVGA] VBE Bochs Display Extension Enabled
00000000000i[CLVGA] interval=300000
00000000000i[PLGIN] init_mem of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'serial' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'parallel' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'gameport' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'speaker' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] init_mem of 'acpi' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'harddrv' plugin device by virtual method
00000000000i[HD ] Using boot sequence floppy, none, none
00000000000i[HD ] Floppy boot signature check is enabled
00000000000i[PLGIN] init_dev of 'keyboard' plugin device by virtual method
00000000000i[KBD ] will paste characters every 1000 keyboard ticks
00000000000i[PLGIN] init_dev of 'serial' plugin device by virtual method
00000000000i[SER ] com1 at 0x03f8 irq 4
00000000000i[PLGIN] init_dev of 'parallel' plugin device by virtual method
00000000000i[PAR ] parallel port 1 at 0x0378 irq 7
00000000000i[PLGIN] init_dev of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'gameport' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'speaker' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'pci_ide' plugin device by virtual method
00000000000i[PCI ] PIIX3 PCI IDE controller present at device 1, function 1
00000000000i[PLGIN] init_dev of 'acpi' plugin device by virtual method
00000000000i[PCI ] ACPI Controller present at device 1, function 3
00000000000i[PLGIN] register state of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] register state of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] register state of 'serial' plugin device by virtual method
00000000000i[PLGIN] register state of 'parallel' plugin device by virtual method
00000000000i[PLGIN] register state of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] register state of 'gameport' plugin device by virtual method
00000000000i[PLGIN] register state of 'speaker' plugin device by virtual method
00000000000i[PLGIN] register state of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] register state of 'acpi' plugin device by virtual method
00000000000i[SYS ] bx_pc_system_c::Reset(HARDWARE) called
00000000000i[CPU0 ] cpu hardware reset
00000000000i[APIC0] local apic in CPU 0 initializing
00000000000i[PLGIN] reset of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] reset of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] reset of 'serial' plugin device by virtual method
00000000000i[PLGIN] reset of 'parallel' plugin device by virtual method
00000000000i[PLGIN] reset of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] reset of 'gameport' plugin device by virtual method
00000000000i[PLGIN] reset of 'speaker' plugin device by virtual method
00000000000i[PLGIN] reset of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] reset of 'acpi' plugin device by virtual method
00000003302i[BIOS ] $Revision: 1.209 $ $Date: 2008/06/02 20:08:10 $
00000320071i[KBD ] reset-disable command received
00000443645i[VBIOS] VGABios $Id: vgabios.c,v 1.67 2008/01/27 09:44:12 vruppert Exp $
00000443716i[CLVGA] VBE known Display Interface b0c0
00000443748i[CLVGA] VBE known Display Interface b0c4
00000446673i[VBIOS] VBE Bios $Id: vbe.c,v 1.60 2008/03/02 07:47:21 vruppert Exp $
00000769674i[BIOS ] Starting rombios32
00000770501i[BIOS ] ram_size=0x02000000
00000870157i[BIOS ] Found 1 cpu(s)
00000886458i[BIOS ] bios_table_addr: 0x000fb778 end=0x000fcc00
00000886525i[PCI ] 440FX PMC write to PAM register 59 (TLB Flush)
00001345332i[PCI ] 440FX PMC write to PAM register 59 (TLB Flush)
00001804819i[P2I ] PCI IRQ routing: PIRQA# set to 0x0b
00001804866i[P2I ] PCI IRQ routing: PIRQB# set to 0x09
00001804913i[P2I ] PCI IRQ routing: PIRQC# set to 0x0b
00001804960i[P2I ] PCI IRQ routing: PIRQD# set to 0x09
00001804976i[P2I ] write: ELCR2 = 0x0a
00001805913i[BIOS ] PIIX3 init: elcr=00 0a
00001826168i[BIOS ] PCI: bus=0 devfn=0x00: vendor_id=0x8086 device_id=0x1237
00001829405i[BIOS ] PCI: bus=0 devfn=0x08: vendor_id=0x8086 device_id=0x7000
00001832138i[BIOS ] PCI: bus=0 devfn=0x09: vendor_id=0x8086 device_id=0x7010
00001832599i[PIDE ] new BM-DMA address: 0xc000
00001833482i[BIOS ] region 4: 0x0000c000
00001836058i[BIOS ] PCI: bus=0 devfn=0x0b: vendor_id=0x8086 device_id=0x7113
00001836558i[ACPI ] new irq line = 11
00001836595i[ACPI ] new PM base address: 0xb000
00001836657i[ACPI ] new SM base address: 0xb100
00001837125i[CPU0 ] Enter to System Management Mode
00001837135i[CPU0 ] RSM: Resuming from System Management Mode
00001837169i[PCI ] setting SMRAM control register to 0x4a
00001837451i[PCI ] setting SMRAM control register to 0x0a
00001860732i[BIOS ] MP table addr=0x000fb850 MPC table addr=0x000fb780 size=0xd0
00001862935i[BIOS ] SMBIOS table addr=0x000fb860
00001865979i[BIOS ] ACPI tables: RSDP addr=0x000fb970 ACPI DATA addr=0x01ff0000 size=0x9d8
00001885512i[PCI ] 440FX PMC write to PAM register 59 (TLB Flush)
00001886524i[BIOS ] bios_table_cur_addr: 0x000fb994
00032315724i[BIOS ] Booting from 0000:7c00
00032315932e[CPU0 ] write_virtual_checks(): write beyond limit, r/w
00032315932i[CPU0 ] CPU is in protected mode (active)
00032315932i[CPU0 ] CS.d_b = 32 bit
00032315932i[CPU0 ] SS.d_b = 32 bit
00032315932i[CPU0 ] EFER = 0x00000000
00032315932i[CPU0 ] | RAX=00000000ffffffff RBX=0000000000000000
00032315932i[CPU0 ] | RCX=0000000000000064 RDX=0000000000000000
00032315932i[CPU0 ] | RSP=0000000000000ffc RBP=0000000000000000
00032315932i[CPU0 ] | RSI=00000000000b8000 RDI=0000000000081030
00032315932i[CPU0 ] | R8=0000000000000000 R9=0000000000000000
00032315932i[CPU0 ] | R10=0000000000000000 R11=0000000000000000
00032315932i[CPU0 ] | R12=0000000000000000 R13=0000000000000000
00032315932i[CPU0 ] | R14=0000000000000000 R15=0000000000000000
00032315932i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af PF cf
00032315932i[CPU0 ] | SEG selector base limit G D
00032315932i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00032315932i[CPU0 ] | CS:0008( 0001| 0| 0) 00000000 000fffff 1 1
00032315932i[CPU0 ] | DS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00032315932i[CPU0 ] | SS:0010( 0002| 0| 0) 00000000 000fffff 1 1
00032315932i[CPU0 ] | ES:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00032315932i[CPU0 ] | FS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00032315932i[CPU0 ] | GS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00032315932i[CPU0 ] | MSR_FS_BASE:0000000000000000
00032315932i[CPU0 ] | MSR_GS_BASE:0000000000000000
00032315932i[CPU0 ] | RIP=0000000000007cd3 (0000000000007cd3)
00032315932i[CPU0 ] | CR0=0x60000011 CR1=0x0 CR2=0x0000000000000000
00032315932i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00032315932i[CPU0 ] >> rep stosd dword ptr es:[edi], eax : F3AB
00032315932e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00032315932i[SYS ] bx_pc_system_c::Reset(SOFTWARE) called
00032315932i[CPU0 ] >> rep stosd dword ptr es:[edi], eax : F3AB
00032315932e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting

Code: Select all

CLEAR_SCREEN:
	MOV ESI, 0x00000000
	MOV AX, GDT_VIDEO
	MOV DS, AX
	
	MOV EAX, 0xFFFFFFFF
	MOV CX, 100
-----REP STOSD-----
	
	RET
Ahh, when I try to set the ES register along with DS/SS after entering protected mode, Bochs freezes and produces no errors or anything. STOSW uses the ES register to copy data, so that's why it's triple faulting there. I think I understand why it's dying on me, but I have no fix... :(

Re: Kernel Stack Fault... can't find it.

Posted: Fri Aug 01, 2008 12:45 am
by jnc100
Stachelsk wrote:STOSW uses the ES register to copy data
It also uses EDI as the destination, rather than ESI.

Regards,
John.