summaryrefslogtreecommitdiff
path: root/sos-code-article6/hwcore/cpu_context_switch.S
blob: 4499c91adff15eac01e03f33ffe04d6d4e2437af (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Copyright (C) 2005  David Decotigny
   Copyright (C) 2000-2004, The KOS team

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA. 
*/
#define ASM_SOURCE 1

         
.file "cpu_context_switch.S"

.text

	
.globl sos_cpu_context_switch
.type sos_cpu_context_switch, @function
sos_cpu_context_switch:
        // arg2= to_context    --    esp+64
        // arg1= from_context  --    esp+60
        // caller ip           --    esp+56
  pushf              // (eflags)     esp+52
  pushl %cs          // (cs)         esp+48
  pushl $resume_pc   // (ip)         esp+44
  pushl $0           // (error code) esp+40
  pushl %ebp         //              esp+36
  pushl %edi         //              esp+32
  pushl %esi         //              esp+28
  pushl %edx         //              esp+24
  pushl %ecx         //              esp+20
  pushl %ebx         //              esp+16
  pushl %eax         //              esp+12
  subl $2, %esp      // (alignment)  esp+10
  pushw %ss          //              esp+8
  pushw %ds          //              esp+6
  pushw %es          //              esp+4
  pushw %fs          //              esp+2
  pushw %gs          //              esp

  /*
   * Now that the original eax/ebx are stored, we can use them safely
   */
	
  /* Store the address of the saved context */
  movl  60(%esp), %ebx
  movl  %esp, (%ebx)

  /* This is the proper context switch ! We change the stack here */
  movl 64(%esp), %esp

  /* Restore the CPU context */
  popw %gs
  popw %fs
  popw %es
  popw %ds
  popw %ss
  addl $2,%esp
  popl %eax
  popl %ebx
  popl %ecx
  popl %edx
  popl %esi
  popl %edi
  popl %ebp
  addl $4, %esp /* Ignore "error code" */

  /* This restores the eflags, the cs and the eip registers */
  iret /* equivalent to: popfl ; ret */

resume_pc:
        // Same context as that when sos_cpu_context_switch got called
        // arg2= to_context    -- esp+8
        // arg1= from_context  -- esp+4
        // caller ip           -- esp
  ret



/* ------------------------- */
.globl sos_cpu_context_exit_to
.type sos_cpu_context_exit_to, @function
sos_cpu_context_exit_to:
        // arg3= reclaiming_arg  -- esp+12
        // arg2= reclaiming_func -- esp+8
        // arg1= to_context      -- esp+4
        // caller ip             -- esp

  /* Store the current SP in a temporary register */
  movl %esp, %eax

  /* This is the proper context switch ! We change the stack here */
  movl 4(%eax), %esp

  /* Call the reclaiming function (remember: the old frame address
     is stored in eax) */
  pushl 12(%eax)
  call  *8(%eax)
  addl  $4, %esp

  /* Restore the CPU context */
  popw %gs
  popw %fs
  popw %es
  popw %ds
  popw %ss
  addl $2,%esp
  popl %eax
  popl %ebx
  popl %ecx
  popl %edx
  popl %esi
  popl %edi
  popl %ebp
  addl $4, %esp /* Ignore "error code" */

  /* This restores the eflags, the cs and the eip registers */
  iret /* equivalent to: popfl ; ret */