I've stumbled upon some weird behaviour.
I get a triple fault immediately after I long jump to 0x08:some_label, even though I have disabled interrupts.
The message I get from Bochs' log files is
interrupt(): gate descriptor is not valid sys seg, which after some investigation turns out to be related to interrupts, but I have disabled them...
I thought it might have been from my GDT having wrong set-up. Tried reversing endian-ness but still gives me this...
I found a thread on stackoverflow which describes the same problem as mine, however the solution isn't helpful as I do not have problems with far jump addresses:
http://stackoverflow.com/questions/1298 ... ected-mode Here's the output from Bochs:
Code:
00017996185i[BIOS ] Booting from 0000:7c00
00018222607e[CPU0 ] check_cs(0x0008): conforming code seg descriptor dpl > cpl, dpl=3, cpl=0
00018222607e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x0d)
00018222607e[CPU0 ] interrupt(): gate descriptor is not valid sys seg (vector=0x08)
00018222607i[CPU0 ] CPU is in protected mode (active)
00018222607i[CPU0 ] CS.d_b = 16 bit
00018222607i[CPU0 ] SS.d_b = 16 bit
00018222607i[CPU0 ] EFER = 0x00000000
00018222607i[CPU0 ] | RAX=0000000060000011 RBX=0000000000000000
00018222607i[CPU0 ] | RCX=0000000000090002 RDX=0000000000000000
00018222607i[CPU0 ] | RSP=000000000000ffff RBP=0000000000000000
00018222607i[CPU0 ] | RSI=00000000000e0000 RDI=0000000000000002
00018222607i[CPU0 ] | R8=0000000000000000 R9=0000000000000000
00018222607i[CPU0 ] | R10=0000000000000000 R11=0000000000000000
00018222607i[CPU0 ] | R12=0000000000000000 R13=0000000000000000
00018222607i[CPU0 ] | R14=0000000000000000 R15=0000000000000000
00018222607i[CPU0 ] | IOPL=0 id vip vif ac vm RF nt of df if tf sf zf af PF cf
00018222607i[CPU0 ] | SEG selector base limit G D
00018222607i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00018222607i[CPU0 ] | CS:0500( 0004| 0| 0) 00005000 0000ffff 0 0
00018222607i[CPU0 ] | DS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00018222607i[CPU0 ] | SS:9000( 0005| 0| 0) 00090000 0000ffff 0 0
00018222607i[CPU0 ] | ES:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00018222607i[CPU0 ] | FS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00018222607i[CPU0 ] | GS:0000( 0005| 0| 0) 00000000 0000ffff 0 0
00018222607i[CPU0 ] | MSR_FS_BASE:0000000000000000
00018222607i[CPU0 ] | MSR_GS_BASE:0000000000000000
00018222607i[CPU0 ] | RIP=000000000000004f (000000000000004f)
00018222607i[CPU0 ] | CR0=0x60000011 CR2=0x0000000000000000
00018222607i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
00018222607i[CPU0 ] 0x000000000000004f>> jmp far 0008:0554 : EA54050800
00018222607e[CPU0 ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting
00018222607i[SYS ] bx_pc_system_c::Reset(HARDWARE) called
00018222607i[CPU0 ] cpu hardware reset
And my stage two bootloader code:
Code:
.code16
.section .data
upper_mem: .word 0, 0, 0, 0
gdt: .quad 0x0000000000000000, 0x00CF9A000000FFFF, 0x00CF92000000FFFF
gdtr: .word 0, 0, 0
.section .text
.globl boot1
.globl upper_mem /* extern void *upper_mem asm("upper_mem"); */
.globl gdtr; /* extern void *gdtr asm("gdtr"); */
.globl gdt_flush;
boot1:
cli
/* re-set the stack and segments */
xorw %ax, %ax
movw %ax, %es
movw %ax, %ds
movw $0x9000, %ax
movw %ax, %ss
movw $0xFFFF, %sp
sti
/* enable A20 line */
movw $0x2401, %ax
int $0x15
/* load the kernel */
/* set up for protected mode */
cli
/* install global descriptor table */
gdt_install:
movw $23, gdtr /* sizeof (gdt) */
movl $1, %edi
movw $0, gdtr(, %edi, 2)
movl $2, %edi
movw gdt, %ax
movw %ax, gdtr(, %edi, 2)
/* apply the global descriptor table */
gdt_flush:
/* enter protected mode */
lgdt gdtr
movl %cr0, %eax
orl $1, %eax
movl %eax, %cr0
ljmp $0x08, $protected_mode
.code32
protected_mode:
/* update segments */
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %gs
movw %ax, %fs
movw %ax, %ss
movl $0x0001000, %esp
//call kmain
/* NOTREACHED */
halt:
hlt
jmp halt