Page 1 of 1

Help me with PMODE!

Posted: Thu Apr 08, 2004 11:00 pm
by matgaw
Hi! I've started some time ago writing my operating system. I have this bootsector:

[BITS 16]       ; We need 16-bit intructions for Real mode

[ORG 0x7C00]    ; The BIOS loads the boot sector into memory location 0x7C00

mov ax,cs
mov ds,ax
mov es,ax

mov ax,0x10
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax

reset_drive:
mov ax,0
mov dl,0
int 13h
jc reset_drive

read: ; load kernel to 1000h
mov ax,1000h
mov es,ax
mov bx,0

mov ah,2
mov al,10; sectors ===========
mov ch,0 ; cylinder  - 0
mov cl,2 ; first sect - 2
mov dh,0 ; head - 0
mov dl,0 ; drive  - 0
int 13h ; read!

jc read

jmp 1000h:0000 ; start loaded code

times 510-($-$$) db 0           ; Fill up the file with zeros

        dw 0AA55h                ; Boot sector identifyer
And it works.

And I have some small kernel that starts protected mode, but it isn't working. What is wrong? Computer resets.

[BITS 16]
[ORG 0x1000]

        cli                     ; Disable interrupts, we want to be alone

        xor ax, ax
        mov ds, ax              ; Set DS-register to 0 - used by lgdt

        lgdt [gdtr]         ; Load the GDT descriptor

        mov eax, cr0            ; Copy the contents of CR0 into EAX
        or eax, 1               ; Set bit 0
        mov cr0, eax            ; Copy the contents of EAX into CR0

        jmp CODE_SEL:clear_pipe      ; Jump to code segment, offset clear_pipe


[BITS 32]                       ; We now need 32-bit instructions
clear_pipe:
        mov ax, 10h             ; Save data segment identifyer
        mov ds, ax              ; Move a valid data segment into the data segment register
        mov ss, ax              ; Move a valid data segment into the stack segment register
        mov esp, 090000h        ; Move the stack pointer to 090000h


        mov byte [ds:0B8000h], 'P'      ; Move the ASCII-code of 'P' into first video memory
        mov byte [ds:0B8001h], 1Bh      ; Assign a color code

hang:
jmp hang

  gdtr:
      lim dw gdt_end - gdt - 1                        ; GDT limit
          dd gdt                                      ; (GDT base gets set above)

     gdt:

     NULL        equ $-gdt                            ; Null Segment
          dw 0                                        ; Limit 15:0
          dw 0                                        ; Base 15:0
          db 0                                        ; Base 23:16
          db 0                                        ; Type
          db 0                                        ; Limit 19:16, flags
          db 0                                        ; Base 31:24

     CODE_SEL    equ $-gdt                            ; Code segment, read/execute, nonconforming
     gdt1:
          dw 0xFFFF                                   ; Limit 0xFFFFF
          dw 0                                        ; (base gets set above)
          db 0
          db 0x9A                                     ; Present, ring 0, code, non-conforming, readable
          db 0xCF                                     ; Page-granular, 32-bit
          db 0

     DATA_SEL    equ $-gdt                            ; Data segment, read/write, expand down
     gdt2:
          dw 0xFFFF                                   ; Limit 0xFFFFF
          dw 0                                        ; (base gets set above)
          db 0
          db 0x92                                     ; Present, ring 0, data, expand-up, writable
          db 0xCF                                     ; Page-granular, 32-bit
          db 0

     gdt_end:

RE:Help me with PMODE!

Posted: Thu Apr 08, 2004 11:00 pm
by JAAman
my first thought is theres something strange here:

in your boot sector you load DS&ES w/CS but immediately with 10h? (why 10h?)
this isnt a problem but does show your not following your values but it not a problem because there are no memory references till you change it again in your second-stage

your ORG is flawed also:
you start with 7C00 but it wont always be that (not a prob because there are no direct references)

then you have ORG 1000! but your jumping to 1000:0000 so offset is 0 not 1000 and your jump then jumps to absolute 1000+clear_pipe but your code is at 10000+clear_pipe

RE:Help me with PMODE!

Posted: Fri Apr 09, 2004 11:00 pm
by matgaw
Yes, you was right! This 10h was strange. I don't know from where I haved it. Now the code is like this, but computer resets too:

bootsect.asm:

[BITS 16]       ; We need 16-bit intructions for Real mode

[ORG 0x7C00]    ; The BIOS loads the boot sector into memory location 0x7C00

mov ax,cs
mov ds,ax
mov es,ax

reset_drive: ; resetujemy floppy disk drive
mov ax,0
mov dl,0
int 13h
jc reset_drive

read: ; load kernel
mov ax,1000h ; load into 1000h:0000
mov es,ax
mov bx,0

mov ah,2
mov al,10; sectors ===========
mov ch,0 ; cylinder  - 0
mov cl,2 ; first sector - 2
mov dh,0 ; head - 0
mov dl,0 ; drive  - 0
int 13h ; read

jc read

jmp 1000h:0000

times 510-($-$$) db 0           ; Fill up the file with zeros

        dw 0AA55h                ; Boot sector identifyer

Is everything all right now in bootsector? It works, I've tested.

But now my kernel, computer resets..... :
[BITS 16]
[ORG 0]

mov ax,cs
mov ds,ax
mov es,ax


        cli                     ; Disable interrupts, we want to be alone

        xor ax, ax
        mov ds, ax              ; Set DS-register to 0 - used by lgdt

        lgdt [gdtr]         ; Load the GDT descriptor

        mov eax, cr0            ; Copy the contents of CR0 into EAX
        or eax, 1               ; Set bit 0
        mov cr0, eax            ; Copy the contents of EAX into CR0

        jmp CODE_SEL:clear_pipe      ; Jump to code segment, offset clear_pipe


[BITS 32]                       ; We now need 32-bit instructions
clear_pipe:
        mov ax, 10h             ; Save data segment identifyer
        mov ds, ax              ; Move a valid data segment into the data segment register
        mov ss, ax              ; Move a valid data segment into the stack segment register
        mov esp, 090000h        ; Move the stack pointer to 090000h


        mov byte [ds:0B8000h], 'P'      ; Move the ASCII-code of 'P' into first video memory
        mov byte [ds:0B8001h], 1Bh      ; Assign a color code

hang:
jmp hang

  gdtr:
      lim dw gdt_end - gdt - 1                        ; GDT limit
          dd gdt                                      ; (GDT base gets set above)

     gdt:

     NULL        equ $-gdt                            ; Null Segment
          dw 0                                        ; Limit 15:0
          dw 0                                        ; Base 15:0
          db 0                                        ; Base 23:16
          db 0                                        ; Type
          db 0                                        ; Limit 19:16, flags
          db 0                                        ; Base 31:24

     CODE_SEL    equ $-gdt                            ; Code segment, read/execute, nonconforming
     gdt1:
          dw 0xFFFF                                   ; Limit 0xFFFFF
          dw 0                                        ; (base gets set above)
          db 0
          db 0x9A                                     ; Present, ring 0, code, non-conforming, readable
          db 0xCF                                     ; Page-granular, 32-bit
          db 0

     DATA_SEL    equ $-gdt                            ; Data segment, read/write, expand down
     gdt2:
          dw 0xFFFF                                   ; Limit 0xFFFFF
          dw 0                                        ; (base gets set above)
          db 0
          db 0x92                                     ; Present, ring 0, data, expand-up, writable
          db 0xCF                                     ; Page-granular, 32-bit
          db 0

     gdt_end:

all of the code before entering PM mode is working, I've tested. [ORG 0x10000] don't work too. Please, help me, because I can't enter PM mode, so I can't start writing anything!

RE:Help me with PMODE!

Posted: Fri Apr 09, 2004 11:00 pm
by pkd
You have still got your read location wrong

read: ; load kernel
mov ax,1000h ; load into 1000h:0000
mov es,ax
mov bx,0

should be

mov

RE:Help me with PMODE!

Posted: Fri Apr 09, 2004 11:00 pm
by pkd
Sorry about my last message didnt see the mov es,ax

But After having a better look it appears to me that your error is in the

jmp Code_Sel:clearPipe

clear_pipe will have the offset in the segment 1000h where you want the offset in your code selector instead of an offset of (eg 20h)
the offset of your code is 10020h (note 20h is just used as an example i havent
calculated it.)

Personnaly I would move your setup GDT & PMODE into the boot sector and just load the 32 bit code into 1000h:0000h then you know the jump would be

jmp dword  0x10:0x00010000

RE:Help me with PMODE!

Posted: Fri Apr 09, 2004 11:00 pm
by matgaw
Ok, when I insert this code to start PM into bootsector, it works!

But, I think that I don't understand something in this PM. Why 0x10:something? What is this 0x10 :P?

And the second thing, can you write me some veeery small program in NASM (all, [BITS 32] [ORG something] too, whole NASM file!) that will in this kernel write some char ex. 'S', and hang?

Must I load a new GDT when I start kernel? Or maybe I must set up some registers? I don't know! Please, help me!

RE:Help me with PMODE!

Posted: Sat Apr 10, 2004 11:00 pm
by pkd
Hi again,

Ok the (0x10) is the same as your code selector (its the offset of your descriptor in the GDT)

You must set up a GDT in order to use PMODE. so that it knows where in memory it is jumping to or where your data is. If an invalid address or selector is use it will cause an exception error and halt (or triple fault and reset if you have know exception handling)

this code should write 'A' to the screen once in PMode
once DS is set to your data Selector

mov  0x41
mov  edi,0xb8000  ;location of screen Memory
mov  [edi],al

Hope this helps
pkd

RE:Help me with PMODE!

Posted: Sat Apr 10, 2004 11:00 pm
by pkd
I just double checked your source and found i did show the wrong selector,

your CODE Selector should be 0x08  (ie jmp 0x08:0x000...)

and your DATA Selector       0x10

This Probably means your computer was freezing after the jump (in ERROR)

ok bye

pkd

RE:Help me with PMODE!

Posted: Sun Apr 11, 2004 11:00 pm
by matgaw
THANKS!!! YOU'RE GREAT!!!

Now it works, and I can start writing my real kernel now! And - I understand some basics of PM. I've readed a lot of that, but I don't like ASM, and not always I unterstand All!

Thanks! YEAH, Now I can start!

RE:Help me with PMODE!

Posted: Tue Apr 13, 2004 11:00 pm
by pkd
np :D