Reputation: 794
I am trying to understand the basics of operating systems and found a course on it in OCW (named 6.828). I found the code of the bootloader in the labs of the course, I tried but did not understand the following part of the code:
# Enable A20:
# For backwards compatibility with the earliest PCs, physical
# address line 20 is tied low, so that addresses higher than
# 1MB wrap around to zero by default. This code undoes this.
seta20.1:
inb $0x64,%al # Wait for not busy
testb $0x2,%al
jnz seta20.1
movb $0xd1,%al # 0xd1 -> port 0x64
outb %al,$0x64
seta20.2:
inb $0x64,%al # Wait for not busy
testb $0x2,%al
jnz seta20.2
movb $0xdf,%al # 0xdf -> port 0x60
outb %al,$0x60
How are we checking if the port 0x64 is busy or not and why this port is used for enabling the A20 bit? Further to make the chip run into 32 bit mode configuration of GDT is done as follows:
# Switch from real to protected mode, using a bootstrap GDT
# and segment translation that makes virtual addresses
# identical to their physical addresses, so that the
# effective memory map does not change during the switch.
lgdt gdtdesc
# Bootstrap GDT
.p2align 2 # force 4 byte alignment
gdt:
SEG_NULL # null seg
SEG(STA_X|STA_R, 0x0, 0xffffffff) # code seg
SEG(STA_W, 0x0, 0xffffffff) # data seg
gdtdesc:
.word 0x17 # sizeof(gdt) - 1
.long gdt # address gdt
What does the above code do? What is the meaning of lines starting with "."? Further, there appear to be two formats for the assembly code .S and .asm what is the difference between the two?
Upvotes: 2
Views: 1612
Reputation: 25874
How are we checking if the port 0x64 is busy or not and why this port is used for enabling the A20 bit?
You check 3rd bit from that port. In more detail:
inb $0x64,%al ; read byte from port 0x64
testb $0x2,%al ; check 3rd bit
jnz seta20.1 ; if not 0, check again
Why is this port and not another is just a matter of decision. It could be any number. It's not actually relevant. For more details you can check this.
What does the above code do?
Prepares to switch from real mode to protected mode.
What is the meaning of lines starting with "."?
Those are assembler directives. They don't generate code but can be used for other things. For example .p2align
is used to align data/code to some byte multiples, .word
declares a word (in this case with value 0x0017), etc...
there appear to be two formats for the assembly code .S and .asm what is the difference between the two?
They are just extensions (don't confuse with file format). They actually mean nothing. Code files are text files.
Upvotes: 2
Reputation: 58802
How are we checking if the port 0x64 is busy or not?
It's right there. If bit #1
(value 2
) is set the port is busy.
why this port is used for enabling the A20 bit
Historical reasons. It was easy to wire this functionality into the keyboard controller. Just like the cpu reset line.
What does the above code do?
Sets up the GDT with three descriptors: null
, code
and data
. I won't explain here what the GDT is and how protected mode segmentation works. I hope it is covered in the tutorial, or else consult osdev.org.
What is the meaning of lines starting with "."?
Those are directives for the assembler, they instruct it to do things. Such as .word
instructs it to emit a word with a given value. Consult the assembler's manual for details.
there appear to be two formats for the assembly code .S and .asm
There are 2 (or rather 3, counting .s
and .S
as separate) common extensions for assembly files. The .s
versions are generally for the gnu toolchain, while .asm
is for others. There are way more than two formats, pretty much one per assembler. These are just the commonly used extensions for the files, they don't really define the contents.
PS: Looks like you are a beginner with assembly, which is fine, but maybe you should not start with low-level OS stuff right away until you have gathered some experience.
Upvotes: 8