Rev 3615 | Rev 4700 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2288 | clevermous | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
2455 | mario79 | 3 | ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;; |
2288 | clevermous | 4 | ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa ;; |
5 | ;; Distributed under terms of the GNU General Public License ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
7 | |||
8 | $Revision: 3617 $ |
||
9 | |||
10 | |||
11 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
12 | ;; IRQ0 HANDLER (TIMER INTERRUPT) ;; |
||
13 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
14 | |||
15 | |||
16 | align 32 |
||
17 | irq0: |
||
18 | pushad |
||
19 | Mov ds, ax, app_data |
||
20 | mov es, ax |
||
21 | inc [timer_ticks] |
||
22 | mov eax, [timer_ticks] |
||
23 | call playNote ; <<<--- Speaker driver |
||
24 | sub eax, [next_usage_update] |
||
25 | cmp eax, 100 |
||
26 | jb .nocounter |
||
27 | add [next_usage_update], 100 |
||
28 | call updatecputimes |
||
29 | .nocounter: |
||
30 | xor ecx, ecx ; send End Of Interrupt signal |
||
31 | call irq_eoi |
||
3504 | mario79 | 32 | ; btr dword[DONT_SWITCH], 0 |
33 | ; jc .return |
||
3615 | clevermous | 34 | mov bl, SCHEDULE_ANY_PRIORITY |
2288 | clevermous | 35 | call find_next_task |
36 | jz .return ; if there is only one running process |
||
37 | call do_change_task |
||
38 | .return: |
||
39 | popad |
||
40 | iretd |
||
41 | |||
42 | align 4 |
||
43 | change_task: |
||
44 | pushfd |
||
45 | cli |
||
46 | pushad |
||
47 | if 0 |
||
48 | ; \begin{Mario79} ; <- must be refractoried, if used... |
||
49 | cmp [dma_task_switched], 1 |
||
50 | jne .find_next_task |
||
51 | mov [dma_task_switched], 0 |
||
52 | mov ebx, [dma_process] |
||
53 | cmp [CURRENT_TASK], ebx |
||
54 | je .return |
||
55 | mov edi, [dma_slot_ptr] |
||
56 | mov [CURRENT_TASK], ebx |
||
57 | mov [TASK_BASE], edi |
||
58 | jmp @f |
||
59 | .find_next_task: |
||
60 | ; \end{Mario79} |
||
61 | end if |
||
3615 | clevermous | 62 | mov bl, SCHEDULE_ANY_PRIORITY |
2288 | clevermous | 63 | call find_next_task |
64 | jz .return ; the same task -> skip switch |
||
65 | @@: |
||
3504 | mario79 | 66 | ; mov byte[DONT_SWITCH], 1 |
2288 | clevermous | 67 | call do_change_task |
68 | .return: |
||
69 | popad |
||
70 | popfd |
||
71 | ret |
||
72 | |||
73 | uglobal |
||
74 | align 4 |
||
75 | ; far_jump: |
||
76 | ; .offs dd ? |
||
77 | ; .sel dw ? |
||
78 | context_counter dd 0 ;noname & halyavin |
||
79 | next_usage_update dd 0 |
||
80 | timer_ticks dd 0 |
||
81 | ; prev_slot dd ? |
||
82 | ; event_sched dd ? |
||
83 | endg |
||
84 | |||
85 | align 4 |
||
86 | update_counters: |
||
87 | mov edi, [TASK_BASE] |
||
88 | rdtsc |
||
89 | sub eax, [edi+TASKDATA.counter_add] ; time stamp counter add |
||
90 | add [edi+TASKDATA.counter_sum], eax ; counter sum |
||
91 | ret |
||
92 | align 4 |
||
93 | updatecputimes: |
||
94 | mov ecx, [TASK_COUNT] |
||
95 | mov edi, TASK_DATA |
||
96 | .newupdate: |
||
97 | xor eax, eax |
||
98 | xchg eax, [edi+TASKDATA.counter_sum] |
||
99 | mov [edi+TASKDATA.cpu_usage], eax |
||
100 | add edi, 0x20 |
||
101 | loop .newupdate |
||
102 | ret |
||
103 | |||
3539 | clevermous | 104 | ;TODO: Надо бы убрать использование do_change_task из V86... |
105 | ; и после этого перенести обработку TASKDATA.counter_add/sum в do_change_task |
||
2288 | clevermous | 106 | |
107 | align 4 |
||
108 | do_change_task: |
||
109 | ;param: |
||
110 | ; ebx = address of the APPDATA for incoming task (new) |
||
111 | ;warning: |
||
112 | ; [CURRENT_TASK] and [TASK_BASE] must be changed before (e.g. in find_next_task) |
||
113 | ; [current_slot] is the outcoming (old), and set here to a new value (ebx) |
||
114 | ;scratched: eax,ecx,esi |
||
115 | mov esi, ebx |
||
116 | xchg esi, [current_slot] |
||
117 | ; set new stack after saving old |
||
118 | mov [esi+APPDATA.saved_esp], esp |
||
119 | mov esp, [ebx+APPDATA.saved_esp] |
||
120 | ; set new thread io-map |
||
121 | Mov dword [page_tabs+((tss._io_map_0 and -4096) shr 10)],eax,[ebx+APPDATA.io_map] |
||
122 | Mov dword [page_tabs+((tss._io_map_1 and -4096) shr 10)],eax,[ebx+APPDATA.io_map+4] |
||
123 | ; set new thread memory-map |
||
124 | mov ecx, APPDATA.dir_table |
||
125 | mov eax, [ebx+ecx] ;offset>0x7F |
||
126 | cmp eax, [esi+ecx] ;offset>0x7F |
||
127 | je @f |
||
128 | mov cr3, eax |
||
129 | @@: |
||
130 | ; set tss.esp0 |
||
131 | |||
132 | Mov [tss._esp0],eax,[ebx+APPDATA.saved_esp0] |
||
133 | |||
134 | mov edx, [ebx+APPDATA.tls_base] |
||
135 | cmp edx, [esi+APPDATA.tls_base] |
||
136 | je @f |
||
137 | |||
138 | mov [tls_data_l+2], dx |
||
139 | shr edx, 16 |
||
140 | mov [tls_data_l+4], dl |
||
141 | mov [tls_data_l+7], dh |
||
142 | |||
143 | mov dx, app_tls |
||
144 | mov fs, dx |
||
145 | @@: |
||
146 | ; set gs selector unconditionally |
||
147 | Mov gs,ax,graph_data |
||
148 | ; set CR0.TS |
||
149 | cmp bh, byte[fpu_owner] ;bh == incoming task (new) |
||
150 | clts ;clear a task switch flag |
||
151 | je @f |
||
152 | mov eax, cr0 ;and set it again if the owner |
||
153 | or eax, CR0_TS ;of a fpu has changed |
||
154 | mov cr0, eax |
||
155 | @@: ; set context_counter (only for user pleasure ???) |
||
156 | inc [context_counter] ;noname & halyavin |
||
157 | ; set debug-registers, if it's necessary |
||
158 | test byte[ebx+APPDATA.dbg_state], 1 |
||
159 | jz @f |
||
160 | xor eax, eax |
||
161 | mov dr6, eax |
||
162 | lea esi, [ebx+ecx+APPDATA.dbg_regs-APPDATA.dir_table];offset>0x7F |
||
163 | cld |
||
164 | macro lodsReg [reg] { |
||
165 | lodsd |
||
166 | mov reg, eax |
||
167 | } lodsReg dr0, dr1, dr2, dr3, dr7 |
||
168 | purge lodsReg |
||
169 | @@: |
||
170 | ret |
||
171 | ;end. |
||
172 | |||
173 | |||
174 | |||
2381 | hidnplayr | 175 | struct MUTEX_WAITER |
176 | list LHEAD |
||
177 | task dd ? |
||
178 | ends |
||
2288 | clevermous | 179 | |
180 | ;void __fastcall mutex_init(struct mutex *lock) |
||
181 | |||
182 | align 4 |
||
183 | mutex_init: |
||
2381 | hidnplayr | 184 | mov [ecx+MUTEX.lhead.next], ecx |
185 | mov [ecx+MUTEX.lhead.prev], ecx |
||
2288 | clevermous | 186 | mov [ecx+MUTEX.count], 1 |
187 | ret |
||
188 | |||
189 | |||
190 | ;void __fastcall mutex_lock(struct mutex *lock) |
||
191 | |||
192 | align 4 |
||
193 | mutex_lock: |
||
194 | |||
195 | dec [ecx+MUTEX.count] |
||
196 | jns .done |
||
197 | |||
198 | pushfd |
||
199 | cli |
||
200 | |||
2381 | hidnplayr | 201 | sub esp, sizeof.MUTEX_WAITER |
2288 | clevermous | 202 | |
203 | list_add_tail esp, ecx ;esp= new waiter, ecx= list head |
||
204 | |||
205 | mov edx, [TASK_BASE] |
||
206 | mov [esp+MUTEX_WAITER.task], edx |
||
207 | |||
208 | .forever: |
||
209 | |||
210 | mov eax, -1 |
||
211 | xchg eax, [ecx+MUTEX.count] |
||
212 | dec eax |
||
213 | jz @F |
||
214 | |||
215 | mov [edx+TASKDATA.state], 1 |
||
216 | call change_task |
||
217 | jmp .forever |
||
218 | @@: |
||
219 | mov edx, [esp+MUTEX_WAITER.list.next] |
||
220 | mov eax, [esp+MUTEX_WAITER.list.prev] |
||
221 | |||
222 | mov [eax+MUTEX_WAITER.list.next], edx |
||
223 | mov [edx+MUTEX_WAITER.list.prev], eax |
||
2381 | hidnplayr | 224 | cmp [ecx+MUTEX.lhead.next], ecx |
2288 | clevermous | 225 | jne @F |
226 | |||
227 | mov [ecx+MUTEX.count], 0 |
||
228 | @@: |
||
2381 | hidnplayr | 229 | add esp, sizeof.MUTEX_WAITER |
2288 | clevermous | 230 | |
231 | popfd |
||
232 | .done: |
||
233 | ret |
||
234 | |||
235 | ;void __fastcall mutex_unlock(struct mutex *lock) |
||
236 | |||
237 | align 4 |
||
238 | mutex_unlock: |
||
239 | |||
240 | pushfd |
||
241 | cli |
||
242 | |||
2381 | hidnplayr | 243 | mov eax, [ecx+MUTEX.lhead.next] |
2288 | clevermous | 244 | cmp eax, ecx |
245 | mov [ecx+MUTEX.count], 1 |
||
246 | je @F |
||
247 | |||
248 | mov eax, [eax+MUTEX_WAITER.task] |
||
249 | mov [eax+TASKDATA.state], 0 |
||
250 | @@: |
||
251 | popfd |
||
252 | ret |