irqret.inc 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. /* IRQ_RETURN_DS <cs_offset>
  2. *
  3. * Load DS/ES/FS/GS for the context we are about to IRET to, in a stub that
  4. * switched them to the kernel data selector (0x10) to run C code.
  5. *
  6. * Why: an IRET to a less privileged level nulls DS/ES/FS/GS whose DPL is
  7. * lower than the new CPL. Coming back to ring 3 with 0x10 (DPL 0) therefore
  8. * leaves the process with a *null* DS, and user code #GPs on its first data
  9. * access (even a simple stack-buffer store through a pointer). QEMU does not
  10. * model this nulling, so it only shows up on real hardware.
  11. *
  12. * So: ring-3 contexts get the user data selector (0x23), ring-0 contexts keep
  13. * 0x10. <cs_offset> is the byte offset of the saved CS in the current frame
  14. * (pusha is 32 bytes, then the CPU's EIP/CS/EFLAGS: offset 36). Clobbers %cx,
  15. * which the caller restores via popa.
  16. */
  17. .macro IRQ_RETURN_DS cs_offset
  18. movw \cs_offset(%esp), %cx
  19. andw $3, %cx
  20. cmpw $3, %cx
  21. jne 1f
  22. movw $0x23, %cx
  23. jmp 2f
  24. 1:
  25. movw $0x10, %cx
  26. 2:
  27. movw %cx, %ds
  28. movw %cx, %es
  29. movw %cx, %fs
  30. movw %cx, %gs
  31. .endm