source: trunk/hal/tsar_mips32/core/hal_kentry.S @ 420

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

Fix a bug in hal_kentry.S : the "uzone" pointer in the thread descriptor
must not be modified in case of interrupt.

File size: 13.9 KB
Line 
1/*
2 * hal_kentry.S - Interrupt / Exception / Syscall kernel entry point for MIPS32
3 *
4 * AUthors   Ghassan Almaless (2007,2008,2009,2010,2011,2012)
5 *           Mohamed Lamine Karaoui (2015)
6 *           Alain Greiner (2017)
7 *
8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH.
11 *
12 * ALMOS-MKH is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#define      UZ_MODE         0                 
27#define      UZ_AT           1
28#define      UZ_V0           2
29#define      UZ_V1           3
30#define      UZ_A0           4
31#define      UZ_A1           5
32#define      UZ_A2           6
33#define      UZ_A3           7
34#define      UZ_T0           8
35#define      UZ_T1           9
36#define      UZ_T2           10
37#define      UZ_T3           11
38#define      UZ_T4           12
39#define      UZ_T5           13
40#define      UZ_T6           14
41#define      UZ_T7           15
42#define      UZ_S0           16
43#define      UZ_S1           17
44#define      UZ_S2           18
45#define      UZ_S3           19
46#define      UZ_S4           20
47#define      UZ_S5           21
48#define      UZ_S6           22
49#define      UZ_S7           23
50#define      UZ_T8           24
51#define      UZ_T9           25
52
53#define      UZ_LO           26
54#define      UZ_HI           27
55
56#define      UZ_GP           28
57#define      UZ_SP           29
58#define      UZ_S8           30
59#define      UZ_RA           31
60#define      UZ_PTPR         32
61#define      UZ_EPC          33
62#define      UZ_SR           34
63#define      UZ_TH           35
64#define      UZ_CR           36
65
66#define      UZ_REGS         37
67
68#include <kernel_config.h>
69
70        .section   .kentry, "ax", @progbits
71
72        .extern    hal_do_interrupt
73        .extern    hal_do_exception
74        .extern    hal_do_syscall
75    .extern    puts
76    .extern    putx
77    .extern    putl
78
79        .org       0x180
80
81        .global    hal_kentry_enter
82    .global    hal_kentry_eret
83
84        .set       noat
85        .set       noreorder
86
87#------------------------------------------------------------------------------------
88# Kernel Entry point for Interrupt / Exception / Syscall
89# The c2_dext and c2_iext CP2 registers must have been previously set
90# to "local_cxy", because the kernel run with MMU desactivated.
91#------------------------------------------------------------------------------------
92
93hal_kentry_enter:
94
95        mfc0    $26,    $12                 # get c0_sr
96        andi    $26,    $26,  0x10          # test User Mode bit
97        beq     $26,    $0,       kernel_mode   # jump if core already in kernel
98        ori     $27,    $0,       0x3           # $27 <= code for MMU OFF
99       
100#------------------------------------------------------------------------------------
101# This code is executed when the core is in user mode,
102# to handle a syscall, an interrupt, or an user exception.
103# - save current c2_mode in $26.
104# - set MMU OFF.
105# - copy user stack pointer in $27 to be saved in uzone.
106# - set kernel stack pointer in $29 == top_kernel_stack(this).
107
108user_mode:
109
110    mfc2    $26,    $1                  # $26 <= c2_mode
111        mtc2    $27,    $1                              # set MMU OFF
112    move    $27,    $29                 # $27 <= user stack pointer
113        mfc0    $29,    $4,   2             # get pointer on thread descriptor from c0_th
114    addi    $29,    $29,  CONFIG_THREAD_DESC_SIZE
115    addi    $29,    $29,  -8            # $29 <= kernel stack pointer
116    j       unified_mode
117    nop
118
119#------------------------------------------------------------------------------------
120# This code is executed when the core is already in kernel mode,
121# after a syscall, to handle an interrupt.
122# - save current c2_mode in $26.
123# - set MMU OFF.
124# - copy current kernel stack pointer in $27.
125
126kernel_mode:
127
128    mfc2    $26,    $1                  # $26 <= c2_mode
129        mtc2    $27,    $1                              # set MMU OFF
130    move    $27,    $29                 # $27 <= current kernel stack pointer
131
132#------------------------------------------------------------------------------------   
133# This code is executed in both modes (user or kernel):
134# The assumptions are:
135# - c2_mode contains the MMU OFF value.
136# - $26 contains the previous c2_mode value.
137# - $27 contains the previous sp value (can be usp or ksp).
138# - $29 contains the curren kernel stack pointer.
139# We execute the following actions:
140# - decrement $29 to allocate an uzone in kernel stack
141# - save relevant GPR, CP0 and CP2 registers to uzone.
142# - set the SR in kernel mode: IRQ disabled, clear exl.
143
144unified_mode:
145
146        addiu   $29,    $29,  -(UZ_REGS*4)      # allocate uzone in kernel stack
147
148        sw      $1,         (UZ_AT*4)($29)
149        sw      $2,     (UZ_V0*4)($29)
150        sw      $3,     (UZ_V1*4)($29)
151        sw      $4,     (UZ_A0*4)($29)
152        sw      $5,     (UZ_A1*4)($29)
153        sw      $6,     (UZ_A2*4)($29)
154        sw      $7,     (UZ_A3*4)($29)
155        sw      $8,     (UZ_T0*4)($29)
156        sw      $9,     (UZ_T1*4)($29)
157        sw      $10,    (UZ_T2*4)($29)
158        sw      $11,    (UZ_T3*4)($29)
159        sw      $12,    (UZ_T4*4)($29)
160        sw      $13,    (UZ_T5*4)($29)
161        sw      $14,    (UZ_T6*4)($29)
162        sw      $15,    (UZ_T7*4)($29)
163        sw      $16,    (UZ_S0*4)($29)
164        sw      $17,    (UZ_S1*4)($29)
165        sw          $18,        (UZ_S2*4)($29)
166        sw          $19,        (UZ_S3*4)($29)
167        sw          $20,        (UZ_S4*4)($29)
168        sw          $21,        (UZ_S5*4)($29)
169        sw          $22,        (UZ_S6*4)($29)
170        sw          $23,        (UZ_S7*4)($29)
171        sw      $24,    (UZ_T8*4)($29)
172        sw      $25,    (UZ_T9*4)($29)
173
174        mflo    $1
175        sw      $1,     (UZ_LO*4)($29)      # save lo
176        mflo    $1
177        sw      $1,     (UZ_HI*4)($29)      # save hi
178
179        sw          $28,        (UZ_GP*4)($29)      # save gp
180        sw          $27,        (UZ_SP*4)($29)      # save previous sp (can be usp or ksp)
181        sw          $30,        (UZ_S8*4)($29)      # save s8
182        sw          $31,        (UZ_RA*4)($29)      # save ra
183
184        mfc0    $1,     $14
185        sw      $1,     (UZ_EPC*4)($29)     # save c0_epc
186        mfc0    $1,         $12
187        sw          $1,     (UZ_SR*4)($29)              # save c0_sr
188        mfc0    $1,     $4,  2
189        sw      $1,         (UZ_TH*4)($29)              # save c0_th
190        mfc0    $1,     $13   
191        sw      $1,         (UZ_CR*4)($29)              # save c0_cr
192        mfc2    $1,     $0
193        sw      $1,     (UZ_PTPR*4)($29)        # save c2_ptpr
194
195    sw      $26,    (UZ_MODE*4)($29)    # save previous c2_mode (can be user or kernel)
196
197    mfc0    $3,     $12
198        srl         $3,     $3,   5
199        sll     $3,         $3,   5                 # reset 5 LSB bits
200        mtc0    $3,         $12                         # set new c0_sr
201
202#-----------------------
203#if CONFIG_KENTRY_DEBUG
204
205    # display "enter" message
206    la      $4,     msg_enter
207    jal     puts
208    nop
209    move    $4,     $29
210    jal     putx
211    nop
212    la      $4,     msg_cycle
213    jal     puts
214    nop
215    jal     hal_time_stamp
216    nop
217    move    $4,     $2
218    jal     putx
219    nop
220    la      $4,     msg_crlf
221    jal     puts
222    nop   
223    # display saved SP value
224    la      $4,     msg_sp
225    jal     puts
226    nop
227    lw      $4,         (UZ_SP*4)($29)
228    jal     putx
229    nop
230    la      $4,     msg_crlf
231    jal     puts
232    nop   
233    # display saved RA value
234    la      $4,     msg_ra
235    jal     puts
236    nop
237    lw      $4,         (UZ_RA*4)($29)
238    jal     putx
239    nop
240    la      $4,     msg_crlf
241    jal     puts
242    nop   
243    # display saved TH value
244    la      $4,     msg_th
245    jal     puts
246    nop
247    lw      $4,         (UZ_TH*4)($29)
248    jal     putx
249    nop
250    la      $4,     msg_crlf
251    jal     puts
252    nop   
253    # display saved EPC value
254    la      $4,     msg_epc
255    jal     puts
256    nop
257    lw      $4,         (UZ_EPC*4)($29)
258    jal     putx
259    nop
260    la      $4,     msg_crlf
261    jal     puts
262    nop   
263    # display saved MODE value
264    la      $4,     msg_mode
265    jal     puts
266    nop
267    lw      $4,         (UZ_MODE*4)($29)
268    jal     putx
269    nop
270    la      $4,     msg_crlf
271    jal     puts
272    nop   
273    # display saved V0 value
274    la      $4,     msg_v0
275    jal     puts
276    nop
277    lw      $4,         (UZ_V0*4)($29)
278    jal     putx
279    nop
280    la      $4,     msg_crlf
281    jal     puts
282    nop   
283
284#endif
285#-----
286   
287#------------------------------------------------------------------------------------
288# This code calls the relevant Interrupt / Exception / Syscall handler,
289# depending on XCODE in CP0_CR.
290# In case of syscall or exception, it set the "uzone" pointer in thread descriptor,
291# that is used by the hal_do_syscall() & hal_do_exception() functions.
292# WARNING: This "uzone" pointer is NOT modified in case of interrupt,
293# because syscalls can be interrupted, and we want preserve this pointer.
294
295        mfc0    $17,    $13                 # $17 <= CR
296        andi    $17,    $17,   0x3F         # $17 <= XCODE
297        ori         $8,     $0,   0x20
298    beq     $8,     $17,  cause_sys     # go to syscall handler
299    nop
300        beq     $17,    $0,       cause_int     # go to interrupt handler
301    nop
302
303cause_excp:
304        mfc0    $4,     $4,   2             # get pointer on thread desc
305    sw      $29,    8($4)               # update uzone pointer in thread desc
306        jal     hal_do_exception            # call exception handler
307        nop
308        j       kentry_exit                 # jump to kentry_exit
309    nop
310
311cause_sys:
312        mfc0    $4,     $4,   2             # get pointer on thread desc
313    sw      $29,    8($4)               # update uzone pointer in thread desc
314        jal     hal_do_syscall              # call syscall handler                 
315    nop
316        j           kentry_exit                 # jump to kentry_exit
317        nop
318       
319cause_int:
320        jal     hal_do_interrupt            # call interrupt handler
321    nop
322
323# -----------------------------------------------------------------------------------
324# Kernel exit
325# - All registers saved in the uzone are restored, using the pointer on uzone,
326#   that is contained in $29.
327# - The "uzone" field in thread descriptor, that has beeen modified at kernel entry
328#   is restored from value contained in the uzone[UZ_SP] slot.
329# -----------------------------------------------------------------------------------
330
331kentry_exit:
332
333#----------------------
334#if CONFIG_KENTRY_DEBUG
335
336    # display "exit" message
337    la      $4,     msg_exit
338    jal     puts
339    nop
340    move    $4,     $29
341    jal     putx
342    nop
343    la      $4,     msg_cycle
344    jal     puts
345    nop
346    jal     hal_time_stamp
347    nop
348    move    $4,     $2
349    jal     putx
350    nop
351    la      $4,     msg_crlf
352    jal     puts
353    nop   
354    # display saved SP value
355    la      $4,     msg_sp
356    jal     puts
357    nop
358    lw      $4,         (UZ_SP*4)($29)
359    jal     putx
360    nop
361    la      $4,     msg_crlf
362    jal     puts
363    nop   
364    # display saved RA value
365    la      $4,     msg_ra
366    jal     puts
367    nop
368    lw      $4,         (UZ_RA*4)($29)
369    jal     putx
370    nop
371    la      $4,     msg_crlf
372    jal     puts
373    nop   
374    # display saved TH value
375    la      $4,     msg_th
376    jal     puts
377    nop
378    lw      $4,         (UZ_TH*4)($29)
379    jal     putx
380    nop
381    la      $4,     msg_crlf
382    jal     puts
383    nop   
384    # display saved EPC value
385    la      $4,     msg_epc
386    jal     puts
387    nop
388    lw      $4,         (UZ_EPC*4)($29)
389    jal     putx
390    nop
391    la      $4,     msg_crlf
392    jal     puts
393    nop   
394    # display saved MODE value
395    la      $4,     msg_mode
396    jal     puts
397    nop
398    lw      $4,         (UZ_MODE*4)($29)
399    jal     putx
400    nop
401    la      $4,     msg_crlf
402    jal     puts
403    nop   
404    # display saved V0 value
405    la      $4,     msg_v0
406    jal     puts
407    nop
408    lw      $4,         (UZ_V0*4)($29)
409    jal     putx
410    nop
411    la      $4,     msg_crlf
412    jal     puts
413    nop   
414       
415#endif
416#-----
417   
418        # restore registers from uzone
419        or          $27,    $0, $29             # $27 <= ksp (contains &uzone)
420
421        lw          $1,     (UZ_EPC*4)($27)           
422        mtc0    $1,         $14                         # restore c0_epc from uzone
423        lw          $1,     (UZ_SR*4)($27)
424        mtc0    $1,     $12                             # restore c0_sr from uzone
425
426        lw          $26,    (UZ_HI*4)($27)
427        mthi    $26                                         # restore hi from uzone
428        lw          $26,    (UZ_LO*4)($27)
429        mtlo    $26                                         # restore lo from uzone
430
431        lw          $1,     (UZ_AT*4)($27)             
432    lw      $2,     (UZ_V0*4)($27)
433    lw      $3,     (UZ_V1*4)($27)
434        lw          $4,     (UZ_A0*4)($27)
435        lw          $5,     (UZ_A1*4)($27)
436        lw          $6,     (UZ_A2*4)($27)
437        lw          $7,     (UZ_A3*4)($27)
438        lw          $8,     (UZ_T0*4)($27)
439        lw          $9,     (UZ_T1*4)($27)
440        lw          $10,    (UZ_T2*4)($27)
441        lw          $11,    (UZ_T3*4)($27)
442        lw          $12,    (UZ_T4*4)($27)
443        lw          $13,    (UZ_T5*4)($27)
444        lw          $14,    (UZ_T6*4)($27)
445        lw          $15,    (UZ_T7*4)($27)
446        lw          $16,    (UZ_S0*4)($27)
447        lw          $17,    (UZ_S1*4)($27)
448        lw          $18,        (UZ_S2*4)($27)
449        lw          $19,    (UZ_S3*4)($27)
450        lw          $20,        (UZ_S4*4)($27)
451        lw          $21,        (UZ_S5*4)($27)
452        lw          $22,        (UZ_S6*4)($27)
453        lw          $23,        (UZ_S7*4)($27)
454        lw          $24,    (UZ_T8*4)($27)
455        lw          $25,    (UZ_T9*4)($27)     
456
457        lw          $28,        (UZ_GP*4)($27)      # restore gp_28 from uzone
458        lw          $29,        (UZ_SP*4)($27)          # restore sp_29 from uzone
459        lw          $30,        (UZ_S8*4)($27)      # restore s8_30 from uzone
460        lw          $31,        (UZ_RA*4)($27)      # restore ra_31 from uzone
461
462        lw          $26,    (UZ_MODE*4)($27)   
463    mtc2    $26,    $1                  # restore CP2_MODE from uzone
464
465# -----------------------------------------------------------------------------------
466# eret function
467# -----------------------------------------------------------------------------------
468
469hal_kentry_eret:
470    eret                                # jump to EPC, reset EXL bit
471
472    .set reorder
473    .set at
474
475#------------------------------------------------------------------------------------
476    .section .kdata
477
478msg_sp:
479    .align 2
480    .asciiz "- UZ_SP   = "
481msg_ra:
482    .align 2
483    .asciiz "- UZ_RA   = "
484msg_epc:
485    .align 2
486    .asciiz "- UZ_EPC  = "
487msg_th:
488    .align 2
489    .asciiz "- UZ_TH   = "
490msg_mode:
491    .align 2
492    .asciiz "- UZ_MODE = "
493msg_v0:
494    .align 2
495    .asciiz "- UZ_V0   = "
496msg_crlf:
497    .align 2
498    .asciiz "\n"
499msg_enter:
500    .align 2
501    .asciiz "\nenter kernel : &uzone = "
502msg_exit:
503    .align 2
504    .asciiz "\nexit kernel : &uzone = "
505msg_cycle:
506    .align 2
507    .asciiz " / cycle = "
508
Note: See TracBrowser for help on using the repository browser.