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