summaryrefslogtreecommitdiff
path: root/libcpu/ia32/context_gcc.S
blob: 65b6a0cdba8853bc639a4af2fd70ef16a36ab3fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-09-15     QiuYi        The first version
 * 2006-10-09     Bernard      add rt_hw_context_switch_to implementation
 */

 /**
 * @addtogroup ia32
 */
/*@{*/

/*
 * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
 */
.globl rt_hw_context_switch
rt_hw_context_switch:
    pushfl                  /*pushed eflags*/
/*
 *  add by [email protected] 2009-10-14
 *      When we return again the esp should no be change.
 *  The old code change the esp to esp-4 :-(.
 *  A protection fault maybe occure for img created by some compiler,eg.gcc in the fedor-11
 *      -------------------------------------------------------------------------
 *  entry           old code            new code
 *  EIP ->return esp    EIP             FLAGS ->return esp
 *  ...         FLAGS    ->retern esp       CS
 *              CS              EIP
 *              EIP
 */
    popl %eax   /*get flags*/
    popl %ebx   /*get eip*/
    pushl %eax  /*push flags*/
    push %cs    /*push cs*/
    pushl %ebx  /*push eip*/

/*-------------------------------------------------------------------
 */

    /*push %cs*/                /*push cs register*/
    /*pushl 0x8(%esp)*/         /*pushed eip register*/

    pushl $0                /*fill irqno*/
    push %ds                /*push ds register*/
    push %es                /*push es register*/
    pushal                  /*push eax,ecx,edx,ebx,esp,ebp,esp,edi registers*/

    /*movl 0x40(%esp), %eax*/   /*to thread TCB*/
    /*movl 0x3c(%esp), %ebx*/   /*from thread TCB*/
    movl 0x3c(%esp), %eax   /*to thread TCB*/
    movl 0x38(%esp), %ebx   /*from thread TCB*/

    movl %esp, (%ebx)       /*store esp in preempted tasks TCB*/
    movl (%eax), %esp       /*get new task stack pointer*/

    popal                   /*restore new task TCB*/
    pop %es
    pop %ds
    add $4,%esp             /*skip irqno*/
    iret

/*
 * void rt_hw_context_switch_to(rt_uint32 to);
 */
.globl rt_hw_context_switch_to
rt_hw_context_switch_to:
    push %ebp
    movl %esp, %ebp

    movl 0x8(%ebp), %eax    /* to thread TCB */
    movl (%eax), %esp       /* get new task stack pointer */

    popal                   /* restore new task TCB*/
    pop %es
    pop %ds
    add $4, %esp            /* skip irqno */
    iret