| 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-i386.S --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // Created On       : Tue Dec 6 12:27:26 2016
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Fri Jul 21 22:29:25 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        4
 | 
|---|
| 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 |         // Copy the "from" context argument from the stack to register eax
 | 
|---|
| 49 |         // Return address is at 0(%esp), with parameters following
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         movl 4(%esp),%eax
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         // Save floating & SSE control words on the stack.
 | 
|---|
| 54 | 
 | 
|---|
| 55 |         sub    $8,%esp
 | 
|---|
| 56 |         stmxcsr 0(%esp)         // 4 bytes
 | 
|---|
| 57 |         fnstcw  4(%esp)         // 2 bytes
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         // Save volatile registers on the stack.
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         pushl %ebx
 | 
|---|
| 62 |         pushl %edi
 | 
|---|
| 63 |         pushl %esi
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         // Save old context in the "from" area.
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         movl %esp,SP_OFFSET(%eax)
 | 
|---|
| 68 |         movl %ebp,FP_OFFSET(%eax)
 | 
|---|
| 69 | //      movl 4(%ebp),%ebx       // save previous eip for debugger
 | 
|---|
| 70 | //      movl %ebx,PC_OFFSET(%eax)
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         // Copy the "to" context argument from the stack to register eax
 | 
|---|
| 73 |         // Having pushed three words (= 12 bytes) on the stack, the
 | 
|---|
| 74 |         // argument is now at 8 + 12 = 20(%esp)
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         movl 28(%esp),%eax
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         // Load new context from the "to" area.
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         movl SP_OFFSET(%eax),%esp
 | 
|---|
| 81 |         movl FP_OFFSET(%eax),%ebp
 | 
|---|
| 82 | 
 | 
|---|
| 83 |         // Load volatile registers from the stack.
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         popl %esi
 | 
|---|
| 86 |         popl %edi
 | 
|---|
| 87 |         popl %ebx
 | 
|---|
| 88 | 
 | 
|---|
| 89 |         // Load floating & SSE control words from the stack.
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         fldcw   4(%esp)
 | 
|---|
| 92 |         ldmxcsr 0(%esp)
 | 
|---|
| 93 |         add    $8,%esp
 | 
|---|
| 94 | 
 | 
|---|
| 95 |         // Return to thread.
 | 
|---|
| 96 | 
 | 
|---|
| 97 |         ret
 | 
|---|
| 98 | 
 | 
|---|
| 99 | // Local Variables: //
 | 
|---|
| 100 | // mode: c //
 | 
|---|
| 101 | // tab-width: 4 //
 | 
|---|
| 102 | // End: //
 | 
|---|