Assembly – sometimes why do we need to disable interrupts when A20 is enabled?

In some codes of osdev wiki for enabling the A20 line, we have cli interrupt command In others we don't have them

For example When the A20 line is set through the old keyboard controller method, the whole code is surrounded by a combination of CLI and STI I can imagine that this must happen because we use keyboard communication through the port, and keyboard interruption can also change the data on the port But is this true? I guessed right

enable_A20:
    cli

    call    a20wait
    mov     al,0xAD
    out     0x64,al

    call    a20wait
    mov     al,0xD0
    out     0x64,al

    call    a20wait2
    in      al,0x60
    push    eax

    call    a20wait
    mov     al,0xD1
    out     0x64,al

    call    a20wait
    pop     eax
    or      al,2
    out     0x60,0xAE
    out     0x64,al

    call    a20wait
    sti
    ret

a20wait:
    in      al,0x64
    test    al,2
    jnz     a20wait
    ret


a20wait2:
    in      al,1
    jz      a20wait2
    ret

Then in the code used to test the A20 line (if it is already active), the interrupt is disabled but never enabled I think it was a mistake not to enable them? In this case, I can imagine that we must disable interrupts, because interrupts may jump to the memory location we modified in this code, and everything will crash?

check_a20:
pushf
push ds
push es
push di
push si
cli

xor ax,ax ; ax = 0
mov es,ax

not ax ; ax = 0xFFFF
mov ds,ax

mov di,0x0500
mov si,0x0510

mov al,byte [es:di]
push ax

mov al,byte [ds:si]
push ax

mov byte [es:di],0x00
mov byte [ds:si],0xFF

cmp byte [es:di],0xFF

pop ax
mov byte [ds:si],al

pop ax
mov byte [es:di],al

mov ax,0
je check_a20__exit

mov ax,1

check_a20__exit:
pop si
pop di
pop es
pop ds
popf

ret

On the other hand, the segment of fast A20 gate does not contain any interrupt disable But we also communicate with the port (read and write) Therefore, if my guess about the keyboard controller is correct, will some interrupts also change the state of port 0x92 after we read it and before we write back? So basically we'll overwrite what the interrupt handler wants to change

fast_a20_gate:
in al,0x92
test al,2
jnz after
or al,2
and al,0xFE
out 0x92,al
after:

Is there any rule of thumb that can easily determine when cli interrupts are needed and when not? At present, I am completely lost in this decision, but I can copy what I can see

Solution

When you communicate with the keyboard controller, you certainly want to send the entire command to it as an uninterrupted sequence So you must stop anyone from disturbing you (!) You

fast_ a20_ Gate doesn't have that problem It's not a sequence, it's just a command - it's actually a bit If someone happens to disturb and flip this, you will still set it at the end

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>