source: trunk/hal/tsar_mips32/core/hal_switch.S @ 418

Last change on this file since 418 was 408, checked in by alain, 6 years ago

Fix several bugs in the fork() syscall.

File size: 7.5 KB
Line 
1/*
2 * hal_witch.S - CPU context switch function for TSAR-MIPS32
3 *
4 * Author  Alain Greiner    (2016)
5 *
6 * Copyright (c)  UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH.is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH.is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH.; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24    .section   .switch , "ax" , @progbits
25
26    .global  hal_do_cpu_switch
27    .global  hal_do_cpu_save
28
29    .set     noat
30    .set     noreorder
31
32#---------------------------------------------------------------------------------
33# The hal_do_cpu_switch() function makes the following assumptions:
34# - register $4 contains a pointer on the old thread context.
35# - register $5 contains a pointer on the new thread context.
36#---------------------------------------------------------------------------------
37hal_do_cpu_switch:
38
39    /* save old thread context */
40 
41    move    $26,   $4                  /* $26 <= ctx_old */
42
43    mfc0    $27,   $14             
44    sw      $27,   0*4($26)            /* save c0_epc to slot 0 */
45 
46    sw      $1,    1*4($26)         
47    sw      $2,    2*4($26)         
48    sw      $3,    3*4($26)         
49    sw      $4,    4*4($26)         
50    sw      $5,    5*4($26)         
51    sw      $6,    6*4($26)         
52    sw      $7,    7*4($26)         
53    sw      $8,    8*4($26)         
54    sw      $9,    9*4($26)         
55    sw      $10,  10*4($26)         
56    sw      $11,  11*4($26)         
57    sw      $12,  12*4($26)         
58    sw      $13,  13*4($26)         
59    sw      $14,  14*4($26)         
60    sw      $15,  15*4($26)         
61    sw      $16,  16*4($26)         
62    sw      $17,  17*4($26)         
63    sw      $18,  18*4($26)         
64    sw      $19,  19*4($26)         
65    sw      $20,  20*4($26)         
66    sw      $21,  21*4($26)         
67    sw      $22,  22*4($26)         
68    sw      $23,  23*4($26)         
69    sw      $24,  24*4($26)         
70    sw      $25,  25*4($26)         
71
72    mfhi    $27                     
73    sw      $27,  26*4($26)            /* save hi to slot 26 */
74    mflo    $27                     
75    sw      $27,  27*4($26)            /* save lo to slot 27 */
76
77    sw      $28,  28*4($26)         
78    sw      $29,  29*4($26)         
79    sw      $30,  30*4($26)         
80    sw      $31,  31*4($26)         
81
82    mfc0    $27,  $12               
83        sw      $27,  34*4($26)           /* save c0_sr to slot 34 */
84    mfc0    $27,  $4, 2             
85        sw      $27,  35*4($26)           /* save c0_th to slot 35 */
86
87        mfc2    $27,  $0               
88        sw      $27,  32*4($26)           /* save c2_ptpr to slot 32 */
89        mfc2    $27,  $1               
90        sw      $27,  33*4($26)           /* save c2_mode to slot 33 */
91
92    /* restore new thread context */
93 
94    move    $26,   $5                  /* $26 <= ctx_new */
95
96    lw      $27,   0*4($26)         
97    mtc0    $27,   $14                 /* restore C0_epc from slot 0 */
98
99    lw      $1,    1*4($26)         
100    lw      $2,    2*4($26)         
101    lw      $3,    3*4($26)         
102    lw      $4,    4*4($26)         
103    lw      $5,    5*4($26)         
104    lw      $6,    6*4($26)         
105    lw      $7,    7*4($26)         
106    lw      $8,    8*4($26)         
107    lw      $9,    9*4($26)         
108    lw      $10,  10*4($26)         
109    lw      $11,  11*4($26)         
110    lw      $12,  12*4($26)         
111    lw      $13,  13*4($26)         
112    lw      $14,  14*4($26)         
113    lw      $15,  15*4($26)         
114        lw      $16,  16*4($26)         
115        lw      $17,  17*4($26)         
116    lw      $18,  18*4($26)         
117    lw      $19,  19*4($26)         
118    lw      $20,  20*4($26)         
119    lw      $21,  21*4($26)         
120    lw      $22,  22*4($26)         
121    lw      $23,  23*4($26)         
122    lw      $24,  24*4($26)         
123    lw      $25,  25*4($26)         
124
125    lw      $27,  26*4($26)         
126    mthi    $27                        /* restore hi from slot 26 */
127    lw      $27,  27*4($26)         
128    mtlo    $27                        /* restore lo from slot 27 */
129
130        lw      $28,  28*4($26)         
131        lw      $29,  29*4($26)         
132        lw      $30,  30*4($26)         
133        lw      $31,  31*4($26)            /* restore ra from slot 31 */
134
135        lw      $27,  32*4($26)
136        mtc2    $27,  $0                   /* restore c2_ptpr from slot 32 */
137
138        lw      $27,  35*4($26)     
139    mtc0        $27,  $4, 2                /* restore c0_th from slot 35 */
140
141        lw      $27,  33*4($26)
142        lw      $26,  34*4($26)
143
144        mtc2    $27,  $1                   /* restore c2_mode from slot 33 */
145    mtc0        $26,  $12                  /* restore c0_sr from slot 34 */
146
147    sync                           
148
149    jr      $31                        /* return to caller */
150    nop
151
152#---------------------------------------------------------------------------------
153# The hal_do_cpu_save()function makes the following assumption:
154# - register $4 contains a pointer on the target thread context.
155#---------------------------------------------------------------------------------
156hal_do_cpu_save:
157
158    move    $26,  $4                  /* $26 <= &context */
159
160    mfc0    $27,  $14               
161    sw      $27,   0*4($26)           /* save c0_epc to slot 0 */
162
163    sw      $1,    1*4($26)         
164    sw      $2,    2*4($26)         
165    sw      $3,    3*4($26)         
166    sw      $4,    4*4($26)         
167    sw      $5,    5*4($26)         
168    sw      $6,    6*4($26)         
169    sw      $7,    7*4($26)         
170    sw      $8,    8*4($26)         
171    sw      $9,    9*4($26)         
172    sw      $10,  10*4($26)         
173    sw      $11,  11*4($26)         
174    sw      $12,  12*4($26)         
175    sw      $13,  13*4($26)         
176    sw      $14,  14*4($26)         
177    sw      $15,  15*4($26)         
178    sw      $16,  16*4($26)         
179    sw      $17,  17*4($26)         
180    sw      $18,  18*4($26)         
181    sw      $19,  19*4($26)         
182    sw      $20,  20*4($26)         
183    sw      $21,  21*4($26)         
184    sw      $22,  22*4($26)         
185    sw      $23,  23*4($26)         
186    sw      $24,  24*4($26)         
187    sw      $25,  25*4($26)
188         
189    mfhi    $27                     
190    sw      $27,  26*4($26)           /* save hi      to slot 26 */
191    mflo    $27                     
192    sw      $27,  27*4($26)           /* save lo      to slot 27 */
193
194    sw      $28,  28*4($26)           /* save gp      to slot 28 */
195    sw      $29,  29*4($26)           /* save sp      to slot 29 */
196    sw      $30,  30*4($26)           /* save s8      to slot 30 */
197    sw      $31,  31*4($26)           /* save ra      to slot 31 */
198
199        mfc2    $27,  $0               
200        sw      $27,  32*4($26)           /* save c2_ptpr to slot 32 */
201        mfc2    $27,  $1               
202        sw      $27,  33*4($26)           /* save c2_mode to slot 33 */
203    mfc0    $27,  $12             
204        sw      $27,  34*4($26)           /* save c0_sr   to slot 34 */
205    mfc0    $27,  $4, 2             
206        sw      $27,  35*4($26)           /* save c0_th   to slot 35 */
207
208    sync
209
210    jr      $31                       /* return to caller */
211    nop
212
213        .set reorder                   
214    .set at                         
215
Note: See TracBrowser for help on using the repository browser.