Skip to content

Commit afa376d

Browse files
committed
lessons 8, 9, 10, entering 32-bit mode
1 parent 085510f commit afa376d

File tree

7 files changed

+193
-0
lines changed

7 files changed

+193
-0
lines changed

08-32bit-print/32bit-print.asm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[bits 32] ; using 32-bit protected mode
2+
3+
; this is how constants are defined
4+
VIDEO_MEMORY equ 0xb8000
5+
WHITE_OB_BLACK equ 0x0f ; the color byte for each character
6+
7+
print_string_pm:
8+
pusha
9+
mov edx, VIDEO_MEMORY
10+
11+
print_string_pm_loop:
12+
mov al, [ebx] ; [ebx] is the address of our character
13+
mov ah, WHITE_OB_BLACK
14+
15+
cmp al, 0 ; check if end of string
16+
je print_string_pm_done
17+
18+
mov [edx], ax ; store character + attribute in video memory
19+
add ebx, 1 ; next char
20+
add edx, 2 ; next video memory position
21+
22+
jmp print_string_pm_loop
23+
24+
print_string_pm_done:
25+
popa
26+
ret

08-32bit-print/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
*Concepts you may want to Google beforehand: 32-bit protected mode, VGA, video
2+
memory*
3+
4+
**Goal: Print on the screen when on 32-bit protected mode**
5+
6+
32-bit mode allows us to use 32 bit registers and memory addressing
7+
, protected memory, virtual memory and other advangades, but we will lose
8+
BIOS interrupts and we'll need to code the GDT (more on this later)
9+
10+
In this lesson we will write a print string routine by directly manipulating
11+
the VGA video memory instead of calling `int 0x10`. The VGA memory starts
12+
at address `0xb8000` and it has a text mode which is useful to avoid
13+
manipulating direct pixels.
14+
15+
The formula for accessing a specific character on the 80x25 grid is:
16+
17+
`0xb8000 + 2 * (row * 80 + col)`
18+
19+
That is, every character uses 2 bytes (one for the ASCII, another for
20+
color and such), and we see that the structure of the memory concatenates
21+
rows.
22+
23+
Open `32bit-print.asm` to see the code. It will always print the string
24+
on the top left of the screen, but soon we'll write higher level routines
25+
to replace it.
26+
27+
Unfortunately we cannot yet call this routine from the bootloader, because
28+
we still don't know how to write the GDT and enter protected mode. Once
29+
you have understood the code, jump to the next lesson.

09-32bit-gdt/32bit-gdt.asm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
gdt_start: ; don't remove the labels, they're needed to compute sizes and jumps
2+
; the GDT starts with a null 8-byte
3+
dd 0x0 ; 4 byte
4+
dd 0x0 ; 4 byte
5+
6+
; GDT for code segment. base = 0x00000000, length = 0xfffff
7+
; for flags, refer to os-dev.pdf document, page 36
8+
gdt_code:
9+
dw 0xffff ; segment length, bits 0-15
10+
dw 0x0 ; segment base, bits 0-15
11+
db 0x0 ; segment base, bits 16-23
12+
db 10011010b ; flags (8 bits)
13+
db 11001111b ; flags (4 bits) + segment length, bits 16-19
14+
db 0x0 ; segment base, bits 24-31
15+
16+
; GDT for data segment. base and length identical to code segment
17+
; some flags changed, again, refer to os-dev.pdf
18+
gdt_data:
19+
dw 0xffff
20+
dw 0x0
21+
db 0x0
22+
db 10010010b
23+
db 11001111b
24+
db 0x0
25+
26+
gdt_end:
27+
28+
; GDT descriptor
29+
gdt_descriptor:
30+
dw gdt_end - gdt_start - 1 ; size (16 bit), always one less of its true size
31+
dd gdt_start ; address (32 bit)
32+
33+
; define some constants for later use
34+
CODE_SEG equ gdt_code - gdt_start
35+
DATA_SEG equ gdt_data - gdt_start

09-32bit-gdt/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
*Concepts you may want to Google beforehand: GDT*
2+
3+
**Goals: program the GDT**
4+
5+
Remember segmentation from lesson 6? The offset was left shifted
6+
to address an extra level of indirection.
7+
8+
In 32-bit mode, segmentation works differently. Now, the offset becomes an
9+
index to a segment descriptor (SD) in the GDT. This descriptor defines
10+
the base address (32 bits), the size (20 bits) and some flags, like
11+
readonly, permissions, etc. To add confusion, the data structures are split,
12+
so open the os-dev.pdf file and check out the figure on page 34 or the
13+
Wikipedia page for the GDT.
14+
15+
The easiest way to program the GDT is to define two segments, one for code
16+
and another for data. These can overlap which means there is no memory protection,
17+
but it's good enough to boot, we'll fix this later with a higher language.
18+
19+
As a curiosity, the first GDT entry must be `0x00` to make sure that the
20+
programmer didn't make any mistakes managing addresses.
21+
22+
Furthermore, since the CPU needs to know how long the GDT is, we'll use
23+
a meta structure called the "GDT descriptor" with the size (16b) and address
24+
(32b) of our actual GDT.
25+
26+
Let's directly jump to the GDT code in assembly. Again, to understand
27+
all the segment flags, refer to the os-dev.pdf document. The theory for
28+
this lesson is quite complex.
29+
30+
In the next lesson we will make the switch to 32-bit protected mode
31+
and test our code from these lessons.

10-32bit-enter/32bit-main.asm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[org 0x7c00] ; bootloader offset
2+
mov bp, 0x9000 ; set the stack
3+
mov sp, bp
4+
5+
mov bx, MSG_REAL_MODE
6+
call print ; This will be written after the BIOS messages
7+
8+
call switch_to_pm
9+
jmp $ ; this will actually never be executed
10+
11+
%include "../05-bootsector-functions-strings/boot_sect_print.asm"
12+
%include "../09-32bit-gdt/32bit-gdt.asm"
13+
%include "../08-32bit-print/32bit-print.asm"
14+
%include "32bit-switch.asm"
15+
16+
[bits 32]
17+
BEGIN_PM: ; after the switch we will get here
18+
mov ebx, MSG_PROT_MODE
19+
call print_string_pm ; Note that this will be written at the top left corner
20+
jmp $
21+
22+
MSG_REAL_MODE db "Started in 16-bit real mode", 0
23+
MSG_PROT_MODE db "Loaded 32-bit protected mode", 0
24+
25+
; bootsector
26+
times 510-($-$$) db 0
27+
dw 0xaa55

10-32bit-enter/32bit-switch.asm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[bits 16]
2+
switch_to_pm:
3+
cli ; 1. disable interrupts
4+
lgdt [gdt_descriptor] ; 2. load the GDT descriptor
5+
mov eax, cr0
6+
or eax, 0x1 ; 3. set 32-bit mode bit in cr0
7+
mov cr0, eax
8+
jmp CODE_SEG:init_pm ; 4. far jump by using a different segment
9+
10+
[bits 32]
11+
init_pm: ; we are now using 32-bit instructions
12+
mov ax, DATA_SEG ; 5. update the segment registers
13+
mov ds, ax
14+
mov ss, ax
15+
mov es, ax
16+
mov fs, ax
17+
mov gs, ax
18+
19+
mov ebp, 0x90000 ; 6. update the stack right at the top of the free space
20+
mov esp, ebp
21+
22+
call BEGIN_PM ; 7. Call a well-known label with useful code

10-32bit-enter/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
*Concepts you may want to Google beforehand: interrupts, pipelining*
2+
3+
**Goal: Enter 32-bit protected mode and test our code from previous lessons**
4+
5+
To jump into 32-bit mode:
6+
7+
1. Disable interrupts
8+
2. Load our GDT
9+
3. Set a bit on the CPU control register `cr0`
10+
4. Flush the CPU pipeline by issuing a carefully crafted far jump
11+
5. Update all the segment registers
12+
6. Update the stack
13+
7. Call to a well-known label which contains the first useful code in 32 bits
14+
15+
We will encapsulate this process on the file `32bit-switch.asm`. Open it
16+
and take a look at the code.
17+
18+
After entering 32-bit mode, we will call `BEGIN_PM` which is the entry point
19+
for our actual useful code (e.g. kernel code, etc). You can read the code
20+
at `32bit-main.asm`. Compile and run this last file and you will see the two
21+
messages on the screen.
22+
23+
Congratulations! Our next step will be to write a simple kernel

0 commit comments

Comments
 (0)