Page 1 of 1

loader - triple-faulting the cpu on pmode

Posted: Sat Sep 21, 2002 11:35 am
by dronkit
Hello,
This is a great forum, so i'll take a chance here ;)

I know this is an all-time question, but i can't find the error.

I'm developing an OS from scratch like zillions of people as i see.. ;)
anyway, i have problems getting pmode to work, i guess i'm loading some registers with wrong values and the cpu ends up triple-faulting.
Here's my second-stage loader: (i haven't post the first-stage loader or my kernel because i know for sure the first one works, and i haven't used the latter yet ;))

.org 0
.text
.globl _start
.code16
Checksum:      .long 0x04031980    /* Simple sanity check */
               .long 0x28021981   /* Simple 4-byte fill */
HexTable:      .ascii "0123456789ABCDEF"
RegDump:         .asciz "Dumping CPU registers:"
RAX:            .asciz "AX="
RBX:            .asciz "BX="
RCX:            .asciz "CX="
RDX:            .asciz "DX="
RCS:            .asciz "CS="
RIP:            .asciz "IP="
RDS:            .asciz "DS="
RES:            .asciz "ES="
RSS:            .asciz "SS="
RSP:            .asciz "SP="
RBP:            .asciz "BP="
RSI:            .asciz "SI="
RDI:            .asciz "DI="
RFS:            .asciz "FS="
RGS:            .asciz "GS="

M1:            .asciz "Second stage boot loader starting..."
M2:            .asciz "Setting IDT..."
MA20K:         .asciz "Enabling A20 line via AT keyboard..."
MA20_INT15:      .asciz "Enabling A20 line via INT 0x15..."
M4:            .asciz "Turning paging on..."
M5:            .asciz "Setting GDT..."
M6:            .asciz "Going into protected mode..."

.2ndstage:
   /* Turn off floppy motor */
   movw $0x3f2, %dx
   xorw %ax, %ax
   outb %al, %dx

   movw $0xB800, %ax
   movw %ax, %gs
A20_ON:
   movw $MA20_INT15, %si
   call kprintln
   movw $0x2401, %ax
   int $0x15
   jnc A20_OK

fallback_to_kbdcontroller:
   movw $MA20K, %si
   call kprintln
.l1:
   in $0x64, %al
   and $2, %al
   jnz .l1

.l2:
   in $0x64, %al
   and $1, %al
   jnz .l2

   in $0x60, %al

   movb $0x32, %gs:52 /* '2' */

   movw $0xd1, %ax
   outb %al, $0x64

.l3:
   in $0x64, %al
   andb $0x2, %al
   jnz .l3

   movw $0xdf, %ax
   outb %al, $0x60

   movb $0x33, %gs:54 /* '3' */

.l4:
in $0x64, %al
and $2, %al
jnz .l4

A20_OK:
prog8259:
   movw $0x11, %ax      /* ICW1 */
   movw $0x20, %dx
   outb %al, %dx         /* Init: step 1 */

   movw $0xA0, %dx
   outb %al, %dx         /* Init: step 2 */

   movw $0x21, %dx      /* ICW2: route IRQs 0...7 to INTs 20h...27h */
   movw $0x20, %ax
   outb %al, %dx

   movw $0xA1, %dx      /* ...IRQs 8...15 to INTs 28h...2Fh */
   movw $0x28, %ax
   outb %al, %dx

   movb $0x34, %gs:56    /* '4' */

   movw $0x4, %ax         /* ICW3 */
   movw $0x21, %dx
   outb %al, %dx

   movw $0x2, %ax
   movw $0xA1, %dx
   outb %al, %dx

   movw $0x1, %ax         /* ICW4 */
   movw $0x21, %dx
   outb %al, %dx
   movw $0xA1, %dx
   outb %al, %dx

   movb $0x35, %gs:58    /* '5' */
   cli
   lgdt gdtptr
   movl %cr0, %eax
   orb $1, %al               /* Go into PM!! */
   movl %eax, %cr0
   .short start32
   .short 0x08
.code32
start32:
   /* Setup data selector. */
   movl $0x10, %eax
   movl %eax, %ds
   movl %eax, %es
   movl %eax, %fs
   /* Setup new 32 bits stack. */
   movl %eax, %ss
   movl $0xFFFF, %esp
   /* Setup video selector. */
   movl $0x18, %eax
   movl %eax, %gs
   movb $0x38, %gs:64 /* '8' */
jmp .
gdtptr:
   .short (4 * 8) - 1
   .long gdt
gdt:
   .long 0
   .long 0
   .short 0xFFFF
   .short 0x0
   .byte 0x0
   .byte 0x9A
   .byte 0xCF
   .byte 0x0

   /* Kernel Data descriptor */
   .short 0xFFFF
   .short 0
   .byte 0
   .byte 0x92
   .byte 0xCF
   .byte 0

   /* Video descriptor */
   .short 0xFFFF
   .short 0
   .byte 0
   .byte 0x92
   .byte 0xCF
   .byte 0
   
gdt_end:

any comments will be appreciated. thank you.

Re:loader - triple-faulting the cpu on pmode

Posted: Sat Sep 21, 2002 12:14 pm
by dronkit
forgot to say: the second stage loader is loaded at 0:7c00 and jump there.
cs=es=ss=0 ds=07c0 sp=07c0

Re:loader - triple-faulting the cpu on pmode

Posted: Sat Sep 21, 2002 12:21 pm
by Tim
You mean you're overwriting the boot sector with the second stage loader? That the boot sector and second stage are both at 0000:7c00?

Re:loader - triple-faulting the cpu on pmode

Posted: Sat Sep 21, 2002 12:25 pm
by dronkit
that's right. although my earlier post was incorrect. these are my registers once i jmp to 2nd stage: bc=7c00 cs=07c0 ds=07c0 es=0 ss=0 sp=7bf8

Re:loader - triple-faulting the cpu on pmode

Posted: Sat Sep 21, 2002 12:28 pm
by dronkit
bc should be bx. Anyway, if you think i'm overwriting the code i'm executing... not ;) I first relocate myself (well, the code ;)) to 0:0600

Re:loader - triple-faulting the cpu on pmode

Posted: Sat Sep 21, 2002 1:20 pm
by dronkit
ok, i've been reading some posting's here.. some changes to the code above:

the relocation address for the 1st stage loader is now at 0:1000 instead of 0:0600. "Tim Robinson" recommends not to overwrite anything below this address. so, just in case ;)

other changes:
lgdt gdtptr
is now
lgdt 0x7c00 + gdtptr
and the jump is now changed to
ljmp $0x8,$0x7c00 + start32

still not working though