i386.asm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;; i386.asm -- assembly support.
  2. ;;
  3. ;; QuickThreads -- Threads-building toolkit.
  4. ;; Copyright (c) 2001, 2006 Free Software Foundation, Inc.
  5. ;;
  6. ;; Permission to use, copy, modify and distribute this software and
  7. ;; its documentation for any purpose and without fee is hereby
  8. ;; granted, provided that the above copyright notice and this notice
  9. ;; appear in all copies. This software is provided as a
  10. ;; proof-of-concept and for demonstration purposes; there is no
  11. ;; representation about the suitability of this software for any
  12. ;; purpose.
  13. ;; NOTE: double-labeled `_name' and `name' for System V compatability.
  14. ;; NOTE: Comment lines start like this one, or with '//' ONLY. Sorry!
  15. ;; Callee-save: %esi, %edi, %ebx, %ebp
  16. ;; Caller-save: %eax, %ecx
  17. ;; Can't tell: %edx (seems to work w/o saving it.)
  18. ;;
  19. ;; Assignment:
  20. ;;
  21. ;; See ``i386.h'' for the somewhat unconventional stack layout.
  22. .386p
  23. .model flat
  24. .code
  25. public _qt_abort
  26. public qt_abort
  27. public _qt_block
  28. public qt_block
  29. public _qt_blocki
  30. public qt_blocki
  31. ;; These all have the type signature
  32. ;;
  33. ;; void *blocking (helper, arg0, arg1, new)
  34. ;;
  35. ;; On procedure entry, the helper is at 4(sp), args at 8(sp) and
  36. ;; 12(sp) and the new thread's sp at 16(sp). It *appears* that the
  37. ;; calling convention for the 8X86 requires the caller to save all
  38. ;; floating-point registers, this makes our life easy.
  39. ;; Halt the currently-running thread. Save it's callee-save regs on
  40. ;; to the stack, 32 bytes. Switch to the new stack (next == 16+32(sp))
  41. ;; and call the user function (f == 4+32(sp) with arguments: old sp
  42. ;; arg1 (8+32(sp)) and arg2 (12+32(sp)). When the user function is
  43. ;; done, restore the new thread's state and return.
  44. ;;
  45. ;; `qt_abort' is (currently) an alias for `qt_block' because most of
  46. ;; the work is shared. We could save the insns up to `qt_common' by
  47. ;; replicating, but w/o replicating we need an inital subtract (to
  48. ;; offset the stack as if it had been a qt_block) and then a jump
  49. ;; to qt_common. For the cost of a jump, we might as well just do
  50. ;; all the work.
  51. ;;
  52. ;; The helper function (4(sp)) can return a void* that is returned
  53. ;; by the call to `qt_blockk{,i}'. Since we don't touch %eax in
  54. ;; between, we get that ``for free''.
  55. _qt_abort:
  56. qt_abort:
  57. _qt_block:
  58. qt_block:
  59. _qt_blocki:
  60. qt_blocki:
  61. push ebp ; Save callee-save, sp-=4.
  62. push esi ; Save callee-save, sp-=4.
  63. push edi ; Save callee-save, sp-=4.
  64. push ebx ; Save callee-save, sp-=4.
  65. mov eax, esp ; Remember old stack pointer.
  66. qt_common:
  67. mov esp, [esp+32] ; Move to new thread.
  68. push [eax+28] ; Push arg 2.
  69. push [eax+24] ; Push arg 1.
  70. push eax ; Push arg 0.
  71. mov ebx, [eax+20] ; Get function to call.
  72. call ebx ; Call f.
  73. add esp, 12 ; Pop args.
  74. pop ebx ; Restore callee-save, sp+=4.
  75. pop edi ; Restore callee-save, sp+=4.
  76. pop esi ; Restore callee-save, sp+=4.
  77. pop ebp ; Restore callee-save, sp+=4.
  78. ret ; Resume the stopped function.
  79. hlt
  80. ;; Start a varargs thread.
  81. public _qt_vstart
  82. public qt_vstart
  83. _qt_vstart:
  84. qt_vstart:
  85. push edi ; Push `pt' arg to `startup'.
  86. call ebp ; Call `startup'.
  87. pop eax ; Clean up the stack.
  88. call ebx ; Call the user's function.
  89. push eax ; Push return from user's.
  90. push edi ; Push `pt' arg to `cleanup'.
  91. call esi ; Call `cleanup'.
  92. hlt ; `cleanup' never returns.
  93. end