1/*
2            Copyright Oliver Kowalke 2009.
3   Distributed under the Boost Software License, Version 1.0.
4      (See accompanying file LICENSE_1_0.txt or copy at
5          http://www.boost.org/LICENSE_1_0.txt)
6*/
7
8/*******************************************************
9 *                                                     *
10 *  -------------------------------------------------  *
11 *  |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  *
12 *  -------------------------------------------------  *
13 *  |  0  |  4  |  8  |  12 |  16 |  20 |  24 |  28 |  *
14 *  -------------------------------------------------  *
15 *  |    R13    |    R14    |    R15    |    R16    |  *
16 *  -------------------------------------------------  *
17 *  -------------------------------------------------  *
18 *  |  8  |  9  |  10 |  11 |  12 |  13 |  14 |  15 |  *
19 *  -------------------------------------------------  *
20 *  |  32 |  36 |  40 |  44 |  48 |  52 |  56 |  60 |  *
21 *  -------------------------------------------------  *
22 *  |    R17    |    R18    |     R19   |    R20    |  *
23 *  -------------------------------------------------  *
24 *  -------------------------------------------------  *
25 *  |  16 |  17 |  18 |  19 |  20 |  21 |  22 |  23 |  *
26 *  -------------------------------------------------  *
27 *  |  64 |  68 |  72 |  76 |  80 |  84 |  88 |  92 |  *
28 *  -------------------------------------------------  *
29 *  |    R21    |    R22    |    R23    |    R24    |  *
30 *  -------------------------------------------------  *
31 *  -------------------------------------------------  *
32 *  |  24 |  25 |  26 |  27 |  28 |  29 |  30 |  31 |  *
33 *  -------------------------------------------------  *
34 *  |  96 | 100 | 104 | 108 | 112 | 116 | 120 | 124 |  *
35 *  -------------------------------------------------  *
36 *  |    R25    |    R26    |    R27    |    R28    |  *
37 *  -------------------------------------------------  *
38 *  -------------------------------------------------  *
39 *  |  32 |  33 |  34 |  35 |  36 |  37 |  38 |  39 |  *
40 *  -------------------------------------------------  *
41 *  | 128 | 132 | 136 | 140 | 144 | 148 | 152 | 156 |  *
42 *  -------------------------------------------------  *
43 *  |    R29    |    R30    |    R31    |   hidden  |  *
44 *  -------------------------------------------------  *
45 *  -------------------------------------------------  *
46 *  |  40 |  41 |  42 |  43 |  44 |  45 |  46 |  47 |  *
47 *  -------------------------------------------------  *
48 *  | 160 | 164 | 168 | 172 | 176 | 180 | 184 | 188 |  *
49 *  -------------------------------------------------  *
50 *  |     CR    |     LR    |     PC    | back-chain|  *
51 *  -------------------------------------------------  *
52 *  -------------------------------------------------  *
53 *  |  48 |  49 |  50 |  51 |  52 |  53 |  54 |  55 |  *
54 *  -------------------------------------------------  *
55 *  | 192 | 196 | 200 | 204 | 208 | 212 | 216 | 220 |  *
56 *  -------------------------------------------------  *
57 *  |  cr saved |  lr saved |  compiler |   linker  |  *
58 *  -------------------------------------------------  *
59 *  -------------------------------------------------  *
60 *  |  56 |  57 |  58 |  59 |  60 |  61 |  62 |  63 |  *
61 *  -------------------------------------------------  *
62 *  | 224 | 228 | 232 | 236 | 240 | 244 | 248 | 252 |  *
63 *  -------------------------------------------------  *
64 *  |    FCTX   |    DATA   |           |           |  *
65 *  -------------------------------------------------  *
66 *                                                     *
67 *******************************************************/
68
69.text
70.align 2
71.globl _jump_fcontext
72
73_jump_fcontext:
74    ; reserve space on stack
75    subi  r1, r1, 184
76
77    std  r14, 8(r1)  ; save R14
78    std  r15, 16(r1)  ; save R15
79    std  r16, 24(r1)  ; save R16
80    std  r17, 32(r1)  ; save R17
81    std  r18, 40(r1)  ; save R18
82    std  r19, 48(r1)  ; save R19
83    std  r20, 56(r1)  ; save R20
84    std  r21, 64(r1)  ; save R21
85    std  r22, 72(r1)  ; save R22
86    std  r23, 80(r1)  ; save R23
87    std  r24, 88(r1)  ; save R24
88    std  r25, 96(r1)  ; save R25
89    std  r26, 104(r1)  ; save R26
90    std  r27, 112(r1)  ; save R27
91    std  r28, 120(r1)  ; save R28
92    std  r29, 128(r1)  ; save R29
93    std  r30, 136(r1)  ; save R30
94    std  r31, 144(r1)  ; save R31
95    std  r3,  152(r1)  ; save hidden
96
97    ; save CR
98    mfcr  r0
99    std   r0, 160(r1)
100    ; save LR
101    mflr  r0
102    std   r0, 168(r1)
103    ; save LR as PC
104    std   r0, 176(r1)
105
106    ; store RSP (pointing to context-data) in R6
107    mr  r6, r1
108
109    ; restore RSP (pointing to context-data) from R4
110    mr  r1, r4
111
112    ld  r14, 8(r1)  ; restore R14
113    ld  r15, 16(r1)  ; restore R15
114    ld  r16, 24(r1)  ; restore R16
115    ld  r17, 32(r1)  ; restore R17
116    ld  r18, 40(r1)  ; restore R18
117    ld  r19, 48(r1)  ; restore R19
118    ld  r20, 56(r1)  ; restore R20
119    ld  r21, 64(r1)  ; restore R21
120    ld  r22, 72(r1)  ; restore R22
121    ld  r23, 80(r1)  ; restore R23
122    ld  r24, 88(r1)  ; restore R24
123    ld  r25, 96(r1)  ; restore R25
124    ld  r26, 104(r1)  ; restore R26
125    ld  r27, 112(r1)  ; restore R27
126    ld  r28, 120(r1)  ; restore R28
127    ld  r29, 128(r1)  ; restore R29
128    ld  r30, 136(r1)  ; restore R30
129    ld  r31, 144(r1)  ; restore R31
130    ld  r3,  152(r1)  ; restore hidden
131
132    ; restore CR
133    ld  r0, 160(r1)
134    mtcr  r0
135    ; restore LR
136    ld  r0, 168(r1)
137    mtlr  r0
138
139    ; load PC
140    ld  r12, 176(r1)
141    ; restore CTR
142    mtctr  r12
143
144    ; adjust stack
145    addi  r1, r1, 184
146
147    ; zero in r3 indicates first jump to context-function
148    cmpdi r3, 0
149    beq use_entry_arg
150
151    ; return transfer_t
152    std  r6, 0(r3)
153    std  r5, 8(r3)
154
155    ; jump to context
156    bctr
157
158use_entry_arg:
159    ; copy transfer_t into transfer_fn arg registers
160    mr  r3, r6
161    mr  r4, r5
162
163    ; jump to context
164    bctr
165