After switching to Protected Mode
, we no longer able to access the BIOS function. Because BIOS functions are available only in Real Mode
. In Protected Mode, the system has different memory management and security features, which prevent direct access to the BIOS interrupts used for hardware communication. Instead, hardware access in Protected Mode is typically handled through device drivers or by temporarily switching back to Real Mode if necessary.
Why BIOS Functions are Inaccessible in Protected Mode
1 Real Mode vs. Protected Mode:
- Real Mode: Offers direct access to memory and hardware. BIOS functions, which are a set of low-level routines stored in the ROM, can be accessed via software interrupts (e.g.,
INT 0x10
for video services). - Protected Mode: Introduces advanced features like virtual memory, privilege levels, and protection mechanisms. Direct hardware access is restricted to enhance system stability and security.
2 Interrupt Handling:
- In Real Mode, interrupts are handled through the Interrupt Vector Table (IVT) located at a fixed address in memory. BIOS routines use these interrupts to perform various system functions.
- In Protected Mode, the system uses the Interrupt Descriptor Table (IDT), which allows for more complex and protected handling of interrupts. The BIOS interrupt routines are not mapped into the IDT and are therefore not accessible.
How to Work with Hardware in Protected Mode
1 Use of Device Drivers:
- Modern operating systems use device drivers to manage hardware. These drivers operate in kernel mode and provide an interface for hardware interaction, abstracting the complexity from user applications.
2 Temporary Switch to Real Mode:
- For systems that still need to use BIOS functions after switching to Protected Mode, it's possible to switch back to Real Mode temporarily. This process involves saving the current CPU state, switching to Real Mode, calling the necessary BIOS function, and then returning to Protected Mode.
Example: Printing to VGA Text Buffer in Protected Mode
Even though BIOS functions are inaccessible, you can still directly manipulate hardware like the VGA text buffer in Protected Mode by understanding how memory is mapped. Here’s how you can print to the VGA text buffer:
Steps:
- Set Up Protected Mode: Initialize the Global Descriptor Table (GDT) and switch the CPU to Protected Mode.
- Access Video Memory: Use the appropriate segment descriptors to access video memory directly.
Assembly Code Example:
Here's a simplified example to print a character to the VGA text buffer in Protected Mode:
[BITS 16]
[ORG 0x7C00]
start:
cli ; Disable interrupts
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00 ; Initialize stack pointer
; Load GDT
lgdt [gdt_descriptor]
; Enable A20 line
in al, 0x92
or al, 2
out 0x92, al
; Switch to protected mode
mov eax, cr0
or eax, 1
mov cr0, eax
jmp CODE_SEG:init_pm
[BITS 32]
init_pm:
; Initialize data segments
mov ax, DATA_SEG
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
; Write "A" to VGA memory
mov byte [VIDEO_MEMORY], 'A' ; Character 'A'
mov byte [VIDEO_MEMORY + 1], 0x07 ; Attribute byte (white on black)
hang:
jmp hang
gdt_start:
gdt_null:
dd 0x0
dd 0x0
gdt_code:
dw 0xFFFF
dw 0x0
db 0x0
db 10011010b
db 11001111b
db 0x0
gdt_data:
dw 0xFFFF
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
VIDEO_MEMORY equ 0xB8000
times 510-($-$$) db 0
dw 0xAA55
Explanation:
1 GDT Setup:
- The Global Descriptor Table (GDT) defines the memory segments for code and data in Protected Mode.
2 Switch to Protected Mode:
- The CPU is switched to Protected Mode by setting the PE (Protection Enable) bit in the
cr0
register. - The
jmp
instruction ensures that the CPU fetches the next instruction in Protected Mode.
3 Accessing VGA Memory:
- VGA memory is mapped at
0xB8000
. Themov
instructions directly write to this memory to display characters. mov byte [VIDEO_MEMORY], 'A'
writes the character 'A' to the VGA memory.mov byte [VIDEO_MEMORY + 1], 0x07
sets the attribute byte (white text on a black background).