| 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           : Thierry Delisle | 
|---|
| 10 | // Created On       : Mon Nov 28 12:27:26 2016 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Fri Jul 21 22:28:11 2017 | 
|---|
| 13 | // Update Count     : 1 | 
|---|
| 14 | // | 
|---|
| 15 | // This  library is free  software; you  can redistribute  it and/or  modify it | 
|---|
| 16 | // under the terms of the GNU Lesser General Public License as published by the | 
|---|
| 17 | // Free Software  Foundation; either  version 2.1 of  the License, or  (at your | 
|---|
| 18 | // option) any later version. | 
|---|
| 19 | // | 
|---|
| 20 | // This library is distributed in the  hope that it will be useful, but WITHOUT | 
|---|
| 21 | // ANY  WARRANTY;  without even  the  implied  warranty  of MERCHANTABILITY  or | 
|---|
| 22 | // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License | 
|---|
| 23 | // for more details. | 
|---|
| 24 | // | 
|---|
| 25 | // You should  have received a  copy of the  GNU Lesser General  Public License | 
|---|
| 26 | // along  with this library. | 
|---|
| 27 | // | 
|---|
| 28 |  | 
|---|
| 29 | // This context switch routine depends on the fact that the stack of a new | 
|---|
| 30 | // thread has been set up to look like the thread has saved its context in | 
|---|
| 31 | // the normal manner. | 
|---|
| 32 | // | 
|---|
| 33 | // void CtxSwitch( machine_context *from, machine_context *to ); | 
|---|
| 34 |  | 
|---|
| 35 | // Offsets in the context structure. This needs to be synchronized with the | 
|---|
| 36 | // high level code a little better. | 
|---|
| 37 |  | 
|---|
| 38 | #define PTR_BYTE        8 | 
|---|
| 39 | #define SP_OFFSET       ( 0 * PTR_BYTE ) | 
|---|
| 40 | #define FP_OFFSET       ( 1 * PTR_BYTE ) | 
|---|
| 41 | #define PC_OFFSET       ( 2 * PTR_BYTE ) | 
|---|
| 42 |  | 
|---|
| 43 | .text | 
|---|
| 44 | .align 2 | 
|---|
| 45 | .globl  CtxSwitch | 
|---|
| 46 | CtxSwitch: | 
|---|
| 47 |  | 
|---|
| 48 | // Save floating & SSE control words on the stack. | 
|---|
| 49 |  | 
|---|
| 50 | subq   $8,%rsp | 
|---|
| 51 | stmxcsr 0(%rsp)         // 4 bytes | 
|---|
| 52 | fnstcw  4(%rsp)         // 2 bytes | 
|---|
| 53 |  | 
|---|
| 54 | // Save volatile registers on the stack. | 
|---|
| 55 |  | 
|---|
| 56 | pushq %r15 | 
|---|
| 57 | pushq %r14 | 
|---|
| 58 | pushq %r13 | 
|---|
| 59 | pushq %r12 | 
|---|
| 60 | pushq %rbx | 
|---|
| 61 |  | 
|---|
| 62 | // Save old context in the "from" area. | 
|---|
| 63 |  | 
|---|
| 64 | movq %rsp,SP_OFFSET(%rdi) | 
|---|
| 65 | movq %rbp,FP_OFFSET(%rdi) | 
|---|
| 66 |  | 
|---|
| 67 | // Load new context from the "to" area. | 
|---|
| 68 |  | 
|---|
| 69 | movq SP_OFFSET(%rsi),%rsp | 
|---|
| 70 | movq FP_OFFSET(%rsi),%rbp | 
|---|
| 71 |  | 
|---|
| 72 | // Load volatile registers from the stack. | 
|---|
| 73 |  | 
|---|
| 74 | popq %rbx | 
|---|
| 75 | popq %r12 | 
|---|
| 76 | popq %r13 | 
|---|
| 77 | popq %r14 | 
|---|
| 78 | popq %r15 | 
|---|
| 79 |  | 
|---|
| 80 | // Load floating & SSE control words from the stack. | 
|---|
| 81 |  | 
|---|
| 82 | fldcw   4(%rsp) | 
|---|
| 83 | ldmxcsr 0(%rsp) | 
|---|
| 84 | addq   $8,%rsp | 
|---|
| 85 |  | 
|---|
| 86 | // Return to thread. | 
|---|
| 87 |  | 
|---|
| 88 | ret | 
|---|
| 89 |  | 
|---|
| 90 | .text | 
|---|
| 91 | .align 2 | 
|---|
| 92 | .globl  CtxInvokeStub | 
|---|
| 93 | CtxInvokeStub: | 
|---|
| 94 | movq %rbx, %rdi | 
|---|
| 95 | jmp *%r12 | 
|---|
| 96 |  | 
|---|
| 97 | // Local Variables: // | 
|---|
| 98 | // mode: c // | 
|---|
| 99 | // tab-width: 4 // | 
|---|
| 100 | // End: // | 
|---|