Skip to main content

needs_swapgs

Function needs_swapgs 

Source
fn needs_swapgs(cs: u16) -> bool
Expand description

Determine whether swapgs is needed at interrupt/exception entry.

In the normal case, code_segment & 3 == 3 (Ring 3) means we need swapgs. However, between swapgs and iretq in elf_ring3_trampoline, CS is still Ring 0 but IA32_GS_BASE is already the user value (0). If iretq itself faults, the exception handler sees CS=Ring 0 but GS=user : the simple ring check misses this. Reading IA32_GS_BASE via rdmsr catches both cases.

Cost: ~20-30 cycles for the rdmsr : acceptable in exception paths (not used for high-frequency IRQ handlers where IF=0 prevents firing in the swapgs→iretq window).