| 1 | //                               -*- Mode: Asm -*- | 
|---|
| 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. | 
|---|
| 20 | // | 
|---|
| 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. | 
|---|
| 25 | // | 
|---|
| 26 | // You should  have received a  copy of the  GNU Lesser General  Public License | 
|---|
| 27 | // along  with this library. | 
|---|
| 28 | // | 
|---|
| 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 |  | 
|---|
| 49 | // Save floating & SSE control words on the stack. | 
|---|
| 50 |  | 
|---|
| 51 | subq   $8,%rsp | 
|---|
| 52 | stmxcsr 0(%rsp)         // 4 bytes | 
|---|
| 53 | fnstcw  4(%rsp)         // 2 bytes | 
|---|
| 54 |  | 
|---|
| 55 | // Save volatile registers on the stack. | 
|---|
| 56 |  | 
|---|
| 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 | 
|---|
| 80 |  | 
|---|
| 81 | // Load floating & SSE control words from the stack. | 
|---|
| 82 |  | 
|---|
| 83 | fldcw   4(%rsp) | 
|---|
| 84 | ldmxcsr 0(%rsp) | 
|---|
| 85 | addq   $8,%rsp | 
|---|
| 86 |  | 
|---|
| 87 | // Return to thread. | 
|---|
| 88 |  | 
|---|
| 89 | ret | 
|---|
| 90 |  | 
|---|
| 91 | .text | 
|---|
| 92 | .align 2 | 
|---|
| 93 | .globl  CtxInvokeStub | 
|---|
| 94 | CtxInvokeStub: | 
|---|
| 95 | movq %rbx, %rdi | 
|---|
| 96 | jmp *%r12 | 
|---|
| 97 |  | 
|---|
| 98 | .text | 
|---|
| 99 | .align 2 | 
|---|
| 100 | .globl  CtxGet | 
|---|
| 101 | CtxGet: | 
|---|
| 102 | movq %rsp,SP_OFFSET(%rdi) | 
|---|
| 103 | movq %rbp,FP_OFFSET(%rdi) | 
|---|
| 104 |  | 
|---|
| 105 | ret | 
|---|
| 106 |  | 
|---|
| 107 | // Local Variables: // | 
|---|
| 108 | // mode: c // | 
|---|
| 109 | // tab-width: 4 // | 
|---|
| 110 | // End: // | 
|---|