-
Notifications
You must be signed in to change notification settings - Fork 6
/
context.386
164 lines (124 loc) · 3.48 KB
/
context.386
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
;***************************************************************************
;
; Copyright (c) 1997, 1998 Timpanogas Research Group, Inc. All Rights
; Reserved.
;
; AUTHOR : Jeff V. Merkey
; FILE : CONTEXT.386
; DESCRIP : Context Switch Code for MANOS v1.0
; DATE : November 29, 1997
;
;
;**************************************************************************/
.486P ; select the processor
model flat
include kernel.inc
CGROUP GROUP _TEXT
DGROUP GROUP _DATA
ASSUME cs:_TEXT, ds:_DATA, es:_DATA, fs:_DATA
_DATA SEGMENT PUBLIC USE32 'DATA'
extrn ptable: dword
_DATA ENDS
_TEXT SEGMENT PUBLIC USE32 'CODE'
extrn preempt_timer : near
extrn get_running_process : near
;**************************************************************************/
;
; context_switch
;
;**************************************************************************/
align 16
public switch_process_context
switch_process_context proc
cli ;
pushfd ; ret
and dword ptr [esp], not 4000h ; flags
or dword ptr [esp], 200h ; ints on
push cs ; cs
push eax
push ebx
push ecx
push edx
mov eax, [esp + 16] ; cs
mov ebx, [esp + 20] ; flags
mov ecx, [esp + 24] ; ret
mov [esp + 24], ebx ; flags
mov [esp + 20], eax ; cs
mov [esp + 16], ecx ; ret
mov ebx, [esp + 28] ; curr -> ebx
mov edx, [esp + 32] ; target -> ecx
push esi
push edi
push ebp
push ds
push es
push fs
mov [ebx].stackPointer, esp
mov eax, [edx].stackPointer
or eax, eax
jnz continueSwitch
int 3
continueSwitch:
;
; unlock both source and target thread
;
mov [ebx].threadMutex, 0 ; clear current thread lock
mov [edx].threadMutex, 0 ; clear target thread lock
mov esp, eax ; switch stacks
add esp, 3 * 4
pop ebp
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
iretd
switch_process_context endp
;**************************************************************************/
;
; preemptive context switch
;
;**************************************************************************/
align 16
public preempt
preempt proc
push eax
push ebx
push ecx
push edx
push esi
push edi
push ebp
push ds
push es
push fs
call get_running_process
mov [eax].stackPointer, esp
mov ebx, eax
push ebx ; save current thread
call preempt_timer
pop ebx
or eax, eax
jz DontSwitchStacks
;
; unlock both source and target thread
;
mov [ebx].threadMutex, 0 ; clear current thread lock
mov [eax].threadMutex, 0 ; clear target thread lock
mov esp, [eax].stackPointer ; switch stacks
DontSwitchStacks:
pop fs
pop es
pop ds
pop ebp
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
iretd
preempt endp
_TEXT ENDS
END