irqret.inc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 selectors 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. * Ring-3 contexts get the user data selector (0x23). Ring-0 contexts get
  13. * 0x10 for DS/ES/FS and 0x28 for GS: GS is the per-CPU data selector whose
  14. * base points at this CPU's struct cpu, and this_cpu()/current_proc() read
  15. * through it. <cs_offset> is the byte offset of the saved CS in the current
  16. * frame (pusha is 32 bytes, then the CPU's EIP/CS/EFLAGS: offset 36).
  17. * Clobbers %cx, which the caller restores via popa.
  18. */
  19. .macro IRQ_RETURN_DS cs_offset
  20. movw \cs_offset(%esp), %cx
  21. andw $3, %cx
  22. cmpw $3, %cx
  23. jne 1f
  24. movw $0x23, %cx
  25. movw %cx, %ds
  26. movw %cx, %es
  27. movw %cx, %fs
  28. movw %cx, %gs
  29. jmp 2f
  30. 1:
  31. movw $0x10, %cx
  32. movw %cx, %ds
  33. movw %cx, %es
  34. movw %cx, %fs
  35. movw $0x28, %cx
  36. movw %cx, %gs
  37. 2:
  38. .endm