/src/rt/arch/i386/context.cpp

http://github.com/jruderman/rust · C++ · 73 lines · 48 code · 17 blank · 8 comment · 1 complexity · af0b2bcaa4d23a78716b22e0e9b824d9 MD5 · raw file

  1. #include "context.h"
  2. #include "../../rust_globals.h"
  3. extern "C" uint32_t CDECL swap_registers(registers_t *oregs,
  4. registers_t *regs)
  5. asm ("swap_registers");
  6. context::context()
  7. {
  8. assert((void*)&regs == (void*)this);
  9. }
  10. void context::swap(context &out)
  11. {
  12. swap_registers(&out.regs, &regs);
  13. }
  14. void context::call(void *f, void *arg, void *stack) {
  15. // Get the current context, which we will then modify to call the
  16. // given function.
  17. swap(*this);
  18. // set up the trampoline frame
  19. uint32_t *sp = (uint32_t *)stack;
  20. // Shift the stack pointer so the alignment works out right.
  21. sp = align_down(sp) - 3;
  22. *--sp = (uint32_t)arg;
  23. // The final return address. 0 indicates the bottom of the stack
  24. *--sp = 0;
  25. regs.esp = (uint32_t)sp;
  26. regs.eip = (uint32_t)f;
  27. // Last base pointer on the stack should be 0
  28. regs.ebp = 0;
  29. }
  30. #if 0
  31. // This is some useful code to check how the registers struct got
  32. // layed out in memory.
  33. int main() {
  34. registers_t regs;
  35. printf("Register offsets\n");
  36. #define REG(r) \
  37. printf(" %6s: +%ld\n", #r, (intptr_t)&regs.r - (intptr_t)&regs);
  38. REG(eax);
  39. REG(ebx);
  40. REG(ecx);
  41. REG(edx);
  42. REG(ebp);
  43. REG(esi);
  44. REG(edi);
  45. REG(esp);
  46. REG(cs);
  47. REG(ds);
  48. REG(ss);
  49. REG(es);
  50. REG(fs);
  51. REG(gs);
  52. REG(eflags);
  53. REG(eip);
  54. return 0;
  55. }
  56. #endif