| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // CtxSwitch-x86_64.S -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Peter A. Buhr | 
|---|
| 10 | // Created On       : Mon Aug 10 08:10:26 2020 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Sat Oct 24 14:36:25 2020 | 
|---|
| 13 | // Update Count     : 10 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | // The context switch routine requires the initial the stack of a thread to | 
|---|
| 17 | // look like the thread has saved its context in the normal manner. | 
|---|
| 18 |  | 
|---|
| 19 | // Offsets must synchronized with the __stack_context_t in invoke.h. | 
|---|
| 20 |  | 
|---|
| 21 | #define PTR_BYTE        8 | 
|---|
| 22 | #define SP_OFFSET       ( 0 * PTR_BYTE ) | 
|---|
| 23 | #define FP_OFFSET       ( 1 * PTR_BYTE ) | 
|---|
| 24 |  | 
|---|
| 25 | // Context switch between coroutines/tasks. | 
|---|
| 26 | //   void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) ; | 
|---|
| 27 | // Arguments "from" in register rdi, "to" in register rsi. | 
|---|
| 28 |  | 
|---|
| 29 | .file "CtxSwitch-x86_64.S" | 
|---|
| 30 | .text | 
|---|
| 31 | .align 2 | 
|---|
| 32 | .global __cfactx_switch | 
|---|
| 33 | .type __cfactx_switch, @function | 
|---|
| 34 | __cfactx_switch: | 
|---|
| 35 |  | 
|---|
| 36 | // Save volatile registers on the stack. | 
|---|
| 37 |  | 
|---|
| 38 | pushq %r15 | 
|---|
| 39 | pushq %r14 | 
|---|
| 40 | pushq %r13 | 
|---|
| 41 | pushq %r12 | 
|---|
| 42 | pushq %rbx | 
|---|
| 43 |  | 
|---|
| 44 | // Save old context in the "from" area. | 
|---|
| 45 |  | 
|---|
| 46 | movq %rsp,SP_OFFSET(%rdi) | 
|---|
| 47 | movq %rbp,FP_OFFSET(%rdi) | 
|---|
| 48 |  | 
|---|
| 49 | // Load new context from the "to" area. | 
|---|
| 50 |  | 
|---|
| 51 | movq SP_OFFSET(%rsi),%rsp | 
|---|
| 52 | movq FP_OFFSET(%rsi),%rbp | 
|---|
| 53 |  | 
|---|
| 54 | // Load volatile registers from the stack. | 
|---|
| 55 |  | 
|---|
| 56 | popq %rbx | 
|---|
| 57 | popq %r12 | 
|---|
| 58 | popq %r13 | 
|---|
| 59 | popq %r14 | 
|---|
| 60 | popq %r15 | 
|---|
| 61 |  | 
|---|
| 62 | // Return to thread. | 
|---|
| 63 |  | 
|---|
| 64 | ret | 
|---|
| 65 | .size __cfactx_switch, .-__cfactx_switch | 
|---|
| 66 |  | 
|---|
| 67 | // Stub to create new stacks which can be context switched to | 
|---|
| 68 | //   void __cfactx_invoke_stub( void ); | 
|---|
| 69 |  | 
|---|
| 70 | .text | 
|---|
| 71 | .align 2 | 
|---|
| 72 | .global __cfactx_invoke_stub | 
|---|
| 73 | .type __cfactx_invoke_stub, @function | 
|---|
| 74 | __cfactx_invoke_stub: | 
|---|
| 75 | movq %rbx, %rdi                                         // move main and this to first two arguments | 
|---|
| 76 | movq %r12, %rsi | 
|---|
| 77 | jmp *%r13                                                       // jmp to invoke | 
|---|
| 78 | .size __cfactx_invoke_stub, .-__cfactx_invoke_stub | 
|---|
| 79 |  | 
|---|
| 80 | // Local Variables: // | 
|---|
| 81 | // mode: asm // | 
|---|
| 82 | // tab-width: 4 // | 
|---|
| 83 | // End: // | 
|---|