flat assembler
Message board for the users of flat assembler.

Index > OS Construction > General protection fault at multitasking :-(

Author
Thread Post new topic Reply to topic
flash



Joined: 11 Mar 2006
Posts: 55
Location: Cuba
flash 05 Nov 2012, 13:08
Hello, i am testing code from Christopher Giese tutorials. I am trying to make my own version of multitask but i am stucked at General Protection Fault over segment descriptor 0, somewhere while second task is excecuting. I chek the code line by line to be sure it does not break the Intel's developer manual secuence. I have no LDT and at the moment of exception, the GDT at memory looks fine(i am using qemu). ¿Anyone have any idea, experience or criticism about what is wrong with my code?
¡Thank's!
Code:
use16
format binary as 'img'
org 7C00h
                cli

                mov     ah,0x02                 
                mov     al,0x4
                mov     ch,0x00                 
                mov     cl,0x02                 
                mov     dh,0x00                 
                mov     dl,0x80                 
                mov     bx,0x8000               
                mov     es,bx
                mov     bx,0x0000
            @@: int     0x13                   
                jc      @b

            @@: mov     ax,0x2401               
                int     0x15
                jc      @b

                cli
                lgdt    fword[gdtr]            
                mov     eax,cr0
                or      al,1                    
                mov     cr0,eax
                jmp     8:protectedMode         
use32
 protectedMode: jmp     80000h                 

align 4
           gdt:
                dq 0000000000000000h            
                dq 00cf9a000000ffffh            
                dq 00cf92000000ffffh

                dw 67h
                dw tss0 and 0ffffh
                db (tss0 shr 16) and 0xff
                dw 8089h
                db (tss0 shr 24) and 0xff

                dw 67h
                dw tss1 and 0ffffh
                db (tss1 shr 16) and 0xff
                dw 8089h
                db (tss1 shr 24) and 0xff

                dw 67h
                dw tss2 and 0ffffh
                db (tss2 shr 16) and 0xff
                dw 8089h
                db (tss2 shr 24) and 0xff
          .end:

          gdtr: dw (gdt.end-gdt)
                dd gdt

times 510-($-$$) db 0
                 dw 0aa55h

org 80000h
                mov     ax,16
                mov     ds,ax
                mov     ss,ax
                mov     es,ax
                mov     fs,ax
                mov     gs,ax

                mov     eax,task1               ; Inicializar el tss1 para utilizar
                mov     [tss1.eip],eax          ; la dirección task1 como punto de entrada del hilo
                mov     eax,esp                 ; Dedicar un espacio en la pila
                sub     esp,1024                ; 1K por debajo de la tarea inicial
                mov     [tss1.esp],eax

                mov     eax,task2               ; Inicializar el tss2 para utilizar
                mov     [tss2.eip],eax          ; la dirección task2 como punto de entrada del hilo
                mov     eax,esp                 ; Dedicar un espacio en la pila
                sub     esp,2048                ; 2K por debajo de la tarea inicial
                mov     [tss2.esp],eax

                mov     ecx,256*2               ; Utilizar los primeros 2K de RAM como IDT
                xor     edi,edi
                xor     eax,eax
                repz    stosd

                mov     edi,13*8                ; Defnir la interrupción 13
                mov     esi,int13               ; Don't do that at home!! ¡Se están "olvidando" las excepciones! Smile
                mov     ecx,2
                repz    movsd

                mov     edi,32*8                ; Defnir la interrupción 32 (IRQ 0)
                mov     esi,irq0                ; Don't do that at home!! ¡Se están "olvidando" las excepciones! Smile
                mov     ecx,2
                repz    movsd

                mov     al,0bh
                out     70h,al
                in      al,71h
                or      al,64
                mov     ah,al
                mov     al,0bh
                out     70h,al
                mov     al,ah
                out     71h,al
                mov     al,0x11
                out     0x20,al
                out     0xA0,al
                mov     al,0x20
                out     0x21,al
                mov     al,0x28
                out     0xA1,al
                mov     al,0x4
                out     0x21,al
                mov     al,0x2
                out     0xA1,al
                mov     al,1
                out     0x21,al
                out     0xA1,al

                mov     al,0xfe                 ; activar solo la irq0
                out     0x21,al                 ; en el maestro
                inc     al
                out     0xA1,al                 ; y ninguna en el esclavo

                lidt    [idtr]

                mov     ax,24                   ; Utilizar el primer descriptor de TSS para el hilo actual
                ltr     ax

                sti
                jmp     $

         task1: mov     esi,msg1
                mov     ah,01001111b
                mov     edi,[pos]
                call    Print
                mov     [pos],edi
                mov     eax,[seconds]
            @@: cmp     eax,[seconds]
                je      @b
                jmp     task1

         task2: mov     esi,msg2
                mov     ah,01110100b
                mov     edi,[pos]
                call    Print
                mov     [pos],edi
                mov     eax,[seconds]
            @@: cmp     eax,[seconds]
                je      @b
                jmp     task2

         timer: dec     [ticks]
                jnz     @f
                inc     [seconds]
                mov     [ticks],18
            @@: mov     [gdt+4*8+5], byte 89h
                mov     [gdt+5*8+5], byte 89h
                mov     al,20h
                out     20h,al
                not     [mux]
                jz      @f
                jmp     32:0
             @@:jmp     40:0

         Print: mov     ebx,esi
                cmp     edi,160*25
                jb      @f
                mov     ecx,(160*25-160)/4
                mov     esi,0xb8000+160
                mov     edi,0xb8000
                repz    movsd
                mov     esi,ebx
            @@: add     edi,0xb8000
            @@: mov     al,[esi]
                cmp     al,0
                je      @f
                mov     [edi],ax
                add     edi,2
                inc     esi
                jmp     @b
            @@: sub     edi,0xb8000
                ret

           gpe: cli
                mov     ah,01001110b
                mov     esi,msg_gpe
                mov     edi,0xb8000+160*24
            @@: mov     al,[esi]
                cmp     al,0
                je      @f
                mov     [edi],ax
                inc     esi
                add     edi,2
                jmp     @b
            @@: pop     ebx
                mov     ecx,32
            @@: mov     al,'0'
                shl     ebx,1
                jnc     @f
                inc     al
            @@: mov     [edi],ax
                add     edi,2
                loop    @b
                jmp     $

align 4
         int13: dw gpe and 0ffffh       ; int 13
                dw 8
                db 0
                db 8eh
                dw gpe shr 16

          irq0: dw timer and 0ffffh     ; int 32 (IRQ0)
                dw 8
                db 0
                db 8eh
                dw timer shr 16

          idtr: dw 256*8
                dd 0

align 4
          tss0: dw 0, 0                 ; backlink
                dd 0                    ; ESP0
                dw 0, 0                 ; SS0, reservado
                dd 0                    ; ESP1
                dw 0, 0                 ; SS1, reservado
                dd 0                    ; ESP2
                dw 0, 0                 ; SS2, reservado
                dd 0, 0, 0              ; CR3, EIP, EFLAGS
                dd 0, 0, 0, 0           ; EAX, ECX, EDX, EBX
                dd 0, 0, 0, 0           ; ESP, EBP, ESI, EDI
                dw 0, 0                 ; ES, reservado
                dw 0, 0                 ; CS, reservado
                dw 0, 0                 ; SS, reservado
                dw 0, 0                 ; DS, reservado
                dw 0, 0                 ; FS, reservado
                dw 0, 0                 ; GS, reservado
                dw 0, 0                 ; LDT, reservado
                dw 0, 0                 ; depuración, mapa de permisos de puerto

align 4
          tss1: dw 0, 0                 ; backlink
                dd 0                    ; ESP0
                dw 0, 0                 ; SS0, reservado
                dd 0                    ; ESP1
                dw 0, 0                 ; SS1, reservado
                dd 0                    ; ESP2
                dw 0, 0                 ; SS2, reservado
                dd 0                    ; CR3
          .eip: dd 0, 0x200             ; EIP, EFLAGS (IF = 1, para activar las interrupciones enmascarables en la tarea)
                dd 0, 0, 0, 0           ; EAX, ECX, EDX, EBX
          .esp: dd 0, 0, 0, 0           ; ESP, EBP, ESI, EDI
                dw 16, 0                ; ES, reservado
                dw 8, 0                 ; CS, reservado
                dw 16, 0                ; SS, reservado
                dw 16, 0                ; DS, reservado
                dw 16, 0                ; FS, reservado
                dw 16, 0                ; GS, reservado
                dw 0, 0                 ; LDT, reservado
                dw 0, 0                 ; depuración, mapa de permisos de puerto

align 4
          tss2: dw 0, 0                 ; backlink
                dd 0                    ; ESP0
                dw 0, 0                 ; SS0, reservado
                dd 0                    ; ESP1
                dw 0, 0                 ; SS1, reservado
                dd 0                    ; ESP2
                dw 0, 0                 ; SS2, reservado
                dd 0                    ; CR3
          .eip: dd 0, 0x200             ; EIP, EFLAGS (IF = 1, para activar las interrupciones enmascarables en la tarea)
                dd 0, 0, 0, 0           ; EAX, ECX, EDX, EBX
          .esp: dd 0, 0, 0, 0           ; ESP, EBP, ESI, EDI
                dw 16, 0                ; ES, reservado
                dw 8, 0                 ; CS, reservado
                dw 16, 0                ; SS, reservado
                dw 16, 0                ; DS, reservado
                dw 16, 0                ; FS, reservado
                dw 16, 0                ; GS, reservado
                dw 0, 0                 ; LDT, reservado
                dw 0, 0                 ; depuración, mapa de permisos de puerto

            pos dd 0
        msg_gpe db 'Error de protecci',162,'n general: ',0
           msg1 db 'UNO',0
           msg2 db 'DOS',0
            mux db 0
        seconds dd 0
          ticks db 18

times (4*512)-($-$$) db 0
 

    

_________________
i don't hate goto
Post 05 Nov 2012, 13:08
View user's profile Send private message Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 11 Nov 2012, 05:15
first off I'm still reading your code so excuse the partial response.

you should setup a stack before invoking the "int" instruction (or any other stack-using instruction). you can easily place at at the start of the boot sector (0x7C00).

a tip (it's not a bug): you don't need to move esp (or a constant) into eax before you write it into memory:

Code:
                mov     eax,esp                 ; Dedicar un espacio en la pila 
                sub     esp,1024                ; 1K por debajo de la tarea inicial 
                mov     [tss1.esp],eax     

this will work just fine:
Code:
                mov     [tss1.esp],esp
                sub     esp,1024                ; 1K por debajo de la tarea inicial     

this applies to constants as well but there are instruction-encoding caveats associated with that.

BAiC

_________________
byte me.
Post 11 Nov 2012, 05:15
View user's profile Send private message Visit poster's website Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 11 Nov 2012, 05:44
it is highly recommended to use CLD (Clear Direction Flag), or STD (Set Direction Flag), prior to each invocation of the string instructions. this is from the "Print" function:

Code:
                mov     ecx,(160*25-160)/4 
                mov     esi,0xb8000+160 
                mov     edi,0xb8000 
                repz    movsd     


also, you don't need repz (repeat while zero/equal), nor should you use, for the movs instruction. the movs instruction is not dependent upon on the zero flag nor is the repeat prefix. the encoding for 'rep' is identical to 'repe'/'repz'. this particular code block needs the direction flag to be clear (execute a CLD).
Code:
                mov     edi,[pos] 
                call    Print 
                mov     [pos],edi     

I recommend putting the EDI-load and EDI-store instruction into the Print procedure.


BAiC

_________________
byte me.


Last edited by BAiC on 11 Nov 2012, 08:13; edited 1 time in total
Post 11 Nov 2012, 05:44
View user's profile Send private message Visit poster's website Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 11 Nov 2012, 06:49
[edited] I'm sorry but this wasn't the cause... I got ahead of myself.

BAiC

_________________
byte me.


Last edited by BAiC on 11 Nov 2012, 08:00; edited 2 times in total
Post 11 Nov 2012, 06:49
View user's profile Send private message Visit poster's website Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 11 Nov 2012, 06:56
I forgot to mention... you don't provide an ESP at any point. you really should explicitly declare a Stack Pointer and preferably at the boot.
Post 11 Nov 2012, 06:56
View user's profile Send private message Visit poster's website Reply with quote
flash



Joined: 11 Mar 2006
Posts: 55
Location: Cuba
flash 11 Nov 2012, 21:54
¡Hi BAiC! thank's for your all your considerations. There are a lot of "scrappy" things left behind on my translation from Christopher Giese tutorials. But none related to protection fault Sad
Anyway, i am just interested on any kind of multitask/multithread. If you know about any other examples about hardware or software context switch i will glad to review it. The thing is i am trying to run James Molloy's Tutorial kernel but the mutitask is buggy Sad And other examples here at the forum are to complex(Menuet, Kolibri...) or to simple or incomplete.
¡Thank's!
Post 11 Nov 2012, 21:54
View user's profile Send private message Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 12 Nov 2012, 01:49
Quote:

And other examples here at the forum are to complex(Menuet, Kolibri...) or to simple or incomplete.

so what's your idea of "complete".. the definitions of 'simple' and 'complete' are very close to being mutually exclusive ideas.

by the way; is the hardware mechanism something you really want? performing a task switch with an explicit software algorithm is more academically descriptive (easier to learn from).

BAiC
Post 12 Nov 2012, 01:49
View user's profile Send private message Visit poster's website Reply with quote
flash



Joined: 11 Mar 2006
Posts: 55
Location: Cuba
flash 15 Dec 2012, 14:56
Hi BAiC, i am testing processor tss features, but it seems to shun me Sad. Any way i just want to use tss to store data in order to make a task switch. As far as i know it's not possible to avoid it usage and i want to learn to merge tss with software switch.
Post 15 Dec 2012, 14:56
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.