Rev 9590 | Rev 9593 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2288 | clevermous | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
8866 | rgimad | 3 | ;; Copyright (C) KolibriOS team 2004-2021. 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: 9591 $ |
||
9 | |||
10 | |||
11 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
12 | ;; IRQ0 HANDLER (TIMER INTERRUPT) ;; |
||
13 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
14 | |||
15 | |||
16 | align 32 |
||
17 | irq0: |
||
18 | pushad |
||
5788 | serge | 19 | mov ax, app_data |
20 | mov ds, ax |
||
2288 | clevermous | 21 | mov es, ax |
22 | inc [timer_ticks] |
||
23 | mov eax, [timer_ticks] |
||
24 | call playNote ; <<<--- Speaker driver |
||
25 | sub eax, [next_usage_update] |
||
26 | cmp eax, 100 |
||
27 | jb .nocounter |
||
28 | add [next_usage_update], 100 |
||
29 | call updatecputimes |
||
30 | .nocounter: |
||
31 | xor ecx, ecx ; send End Of Interrupt signal |
||
32 | call irq_eoi |
||
4700 | mario79 | 33 | |
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 |
||
3615 | clevermous | 47 | mov bl, SCHEDULE_ANY_PRIORITY |
2288 | clevermous | 48 | call find_next_task |
49 | jz .return ; the same task -> skip switch |
||
4700 | mario79 | 50 | |
2288 | clevermous | 51 | call do_change_task |
52 | .return: |
||
53 | popad |
||
54 | popfd |
||
55 | ret |
||
56 | |||
57 | uglobal |
||
58 | align 4 |
||
59 | ; far_jump: |
||
60 | ; .offs dd ? |
||
61 | ; .sel dw ? |
||
62 | context_counter dd 0 ;noname & halyavin |
||
63 | next_usage_update dd 0 |
||
64 | timer_ticks dd 0 |
||
65 | ; prev_slot dd ? |
||
66 | ; event_sched dd ? |
||
67 | endg |
||
68 | |||
69 | align 4 |
||
70 | update_counters: |
||
71 | mov edi, [TASK_BASE] |
||
72 | rdtsc |
||
73 | sub eax, [edi+TASKDATA.counter_add] ; time stamp counter add |
||
74 | add [edi+TASKDATA.counter_sum], eax ; counter sum |
||
75 | ret |
||
76 | align 4 |
||
77 | updatecputimes: |
||
8866 | rgimad | 78 | mov ecx, [thread_count] |
9591 | Doczom | 79 | mov edi, TASK_DATA |
80 | ;mov dword[edi+TASKDATA.cpu_usage], 0 |
||
81 | mov esi, SLOT_BASE |
||
2288 | clevermous | 82 | .newupdate: |
83 | xor eax, eax |
||
84 | xchg eax, [edi+TASKDATA.counter_sum] |
||
9590 | Doczom | 85 | ;mov [edi+TASKDATA.cpu_usage], eax |
9591 | Doczom | 86 | add edi, 0x20 |
87 | mov [esi+APPDATA.cpu_usage], eax |
||
88 | add esi, 0x100 ;sizeof.APPDATA |
||
2288 | clevermous | 89 | loop .newupdate |
90 | ret |
||
91 | |||
3539 | clevermous | 92 | ;TODO: Надо бы убрать использование do_change_task из V86... |
93 | ; и после этого перенести обработку TASKDATA.counter_add/sum в do_change_task |
||
2288 | clevermous | 94 | |
95 | align 4 |
||
96 | do_change_task: |
||
97 | ;param: |
||
98 | ; ebx = address of the APPDATA for incoming task (new) |
||
99 | ;warning: |
||
8869 | rgimad | 100 | ; [current_slot_idx] and [TASK_BASE] must be changed before (e.g. in find_next_task) |
2288 | clevermous | 101 | ; [current_slot] is the outcoming (old), and set here to a new value (ebx) |
102 | ;scratched: eax,ecx,esi |
||
103 | mov esi, ebx |
||
104 | xchg esi, [current_slot] |
||
105 | ; set new stack after saving old |
||
106 | mov [esi+APPDATA.saved_esp], esp |
||
107 | mov esp, [ebx+APPDATA.saved_esp] |
||
108 | ; set new thread io-map |
||
5788 | serge | 109 | mov eax, [ebx+APPDATA.io_map] |
110 | mov dword [page_tabs+((tss._io_map_0 and -4096) shr 10)], eax |
||
111 | mov eax, [ebx+APPDATA.io_map+4] |
||
112 | mov dword [page_tabs+((tss._io_map_1 and -4096) shr 10)], eax |
||
2288 | clevermous | 113 | ; set new thread memory-map |
5130 | serge | 114 | mov eax, [ebx+APPDATA.process] |
115 | cmp eax, [current_process] |
||
2288 | clevermous | 116 | je @f |
5130 | serge | 117 | mov [current_process], eax |
118 | mov eax, [eax+PROC.pdt_0_phys] |
||
2288 | clevermous | 119 | mov cr3, eax |
120 | @@: |
||
121 | ; set tss.esp0 |
||
122 | |||
5788 | serge | 123 | mov eax, [ebx+APPDATA.saved_esp0] |
124 | mov [tss._esp0], eax |
||
2288 | clevermous | 125 | |
126 | mov edx, [ebx+APPDATA.tls_base] |
||
127 | |||
128 | mov [tls_data_l+2], dx |
||
129 | shr edx, 16 |
||
130 | mov [tls_data_l+4], dl |
||
131 | mov [tls_data_l+7], dh |
||
132 | |||
133 | mov dx, app_tls |
||
134 | mov fs, dx |
||
5376 | serge | 135 | |
2288 | clevermous | 136 | ; set gs selector unconditionally |
5788 | serge | 137 | Mov ax, graph_data |
138 | Mov gs, ax |
||
7276 | dunkaist | 139 | ; TS flag is not triggered by AVX* instructions, therefore |
140 | ; we have to xsave/xrstor SIMD registers each task change |
||
141 | bt [cpu_caps+(CAPS_OSXSAVE/32)*4], CAPS_OSXSAVE mod 32 |
||
142 | jnc .no_xsave |
||
143 | mov ecx, [esi+APPDATA.fpu_state] |
||
144 | mov eax, [xsave_eax] |
||
145 | mov edx, [xsave_edx] |
||
146 | xsave [ecx] |
||
8869 | rgimad | 147 | mov ecx, [current_slot_idx] |
7276 | dunkaist | 148 | mov [fpu_owner], ecx |
149 | mov ecx, [current_slot] |
||
150 | mov ecx, [ecx+APPDATA.fpu_state] |
||
151 | xrstor [ecx] |
||
152 | .no_xsave: |
||
2288 | clevermous | 153 | ; set CR0.TS |
154 | cmp bh, byte[fpu_owner] ;bh == incoming task (new) |
||
155 | clts ;clear a task switch flag |
||
156 | je @f |
||
157 | mov eax, cr0 ;and set it again if the owner |
||
158 | or eax, CR0_TS ;of a fpu has changed |
||
159 | mov cr0, eax |
||
160 | @@: ; set context_counter (only for user pleasure ???) |
||
161 | inc [context_counter] ;noname & halyavin |
||
162 | ; set debug-registers, if it's necessary |
||
163 | test byte[ebx+APPDATA.dbg_state], 1 |
||
164 | jz @f |
||
165 | xor eax, eax |
||
166 | mov dr6, eax |
||
5130 | serge | 167 | lea esi, [ebx+APPDATA.dbg_regs] |
2288 | clevermous | 168 | cld |
169 | macro lodsReg [reg] { |
||
170 | lodsd |
||
171 | mov reg, eax |
||
172 | } lodsReg dr0, dr1, dr2, dr3, dr7 |
||
173 | purge lodsReg |
||
174 | @@: |
||
175 | ret |
||
176 | ;end. |
||
177 | |||
178 | |||
179 | |||
5344 | serge | 180 | |
3534 | clevermous | 181 | MAX_PRIORITY = 0 ; highest, used for kernel tasks |
182 | USER_PRIORITY = 1 ; default |
||
183 | IDLE_PRIORITY = 2 ; lowest, only IDLE thread goes here |
||
184 | NR_SCHED_QUEUES = 3 ; MUST equal IDLE_PRIORYTY + 1 |
||
185 | |||
186 | uglobal |
||
187 | ; [scheduler_current + i*4] = zero if there are no threads with priority i, |
||
188 | ; pointer to APPDATA of the current thread with priority i otherwise. |
||
189 | align 4 |
||
190 | scheduler_current rd NR_SCHED_QUEUES |
||
191 | endg |
||
192 | |||
193 | ; Add the given thread to the given priority list for the scheduler. |
||
194 | ; in: edx -> APPDATA, ecx = priority |
||
195 | proc scheduler_add_thread |
||
196 | ; 1. Acquire the lock. |
||
197 | spin_lock_irqsave SchedulerLock |
||
198 | ; 2. Store the priority in APPDATA structure. |
||
199 | mov [edx+APPDATA.priority], ecx |
||
200 | ; 3. There are two different cases: the given list is empty or not empty. |
||
201 | ; In first case, go to 6. Otherwise, advance to 4. |
||
202 | mov eax, [scheduler_current+ecx*4] |
||
203 | test eax, eax |
||
204 | jz .new_list |
||
205 | ; 4. Insert the new item immediately before the current item. |
||
206 | mov ecx, [eax+APPDATA.in_schedule.prev] |
||
207 | mov [edx+APPDATA.in_schedule.next], eax |
||
208 | mov [edx+APPDATA.in_schedule.prev], ecx |
||
209 | mov [eax+APPDATA.in_schedule.prev], edx |
||
210 | mov [ecx+APPDATA.in_schedule.next], edx |
||
211 | ; 5. Release the lock and return. |
||
212 | spin_unlock_irqrestore SchedulerLock |
||
213 | ret |
||
214 | .new_list: |
||
215 | ; 6. Initialize the list with one item and make it the current item. |
||
216 | mov [edx+APPDATA.in_schedule.next], edx |
||
217 | mov [edx+APPDATA.in_schedule.prev], edx |
||
218 | mov [scheduler_current+ecx*4], edx |
||
219 | ; 7. Release the lock and return. |
||
220 | spin_unlock_irqrestore SchedulerLock |
||
221 | ret |
||
222 | endp |
||
223 | |||
224 | ; Remove the given thread from the corresponding priority list for the scheduler. |
||
225 | ; in: edx -> APPDATA |
||
226 | proc scheduler_remove_thread |
||
227 | ; 1. Acquire the lock. |
||
228 | spin_lock_irqsave SchedulerLock |
||
229 | ; 2. Remove the item from the corresponding list. |
||
230 | mov eax, [edx+APPDATA.in_schedule.next] |
||
231 | mov ecx, [edx+APPDATA.in_schedule.prev] |
||
232 | mov [eax+APPDATA.in_schedule.prev], ecx |
||
233 | mov [ecx+APPDATA.in_schedule.next], eax |
||
234 | ; 3. If the given thread is the current item in the list, |
||
235 | ; advance the current item. |
||
236 | ; 3a. Check whether the given thread is the current item; |
||
237 | ; if no, skip the rest of this step. |
||
238 | mov ecx, [edx+APPDATA.priority] |
||
239 | cmp [scheduler_current+ecx*4], edx |
||
240 | jnz .return |
||
241 | ; 3b. Set the current item to eax; step 2 has set eax = next item. |
||
242 | mov [scheduler_current+ecx*4], eax |
||
243 | ; 3c. If there were only one item in the list, zero the current item. |
||
244 | cmp eax, edx |
||
245 | jnz .return |
||
246 | mov [scheduler_current+ecx*4], 0 |
||
247 | .return: |
||
248 | ; 4. Release the lock and return. |
||
249 | spin_unlock_irqrestore SchedulerLock |
||
250 | ret |
||
251 | endp |
||
252 | |||
3615 | clevermous | 253 | SCHEDULE_ANY_PRIORITY = 0 |
254 | SCHEDULE_HIGHER_PRIORITY = 1 |
||
3534 | clevermous | 255 | ;info: |
256 | ; Find next task to execute |
||
3615 | clevermous | 257 | ;in: |
258 | ; bl = SCHEDULE_ANY_PRIORITY: |
||
259 | ; consider threads with any priority |
||
260 | ; bl = SCHEDULE_HIGHER_PRIORITY: |
||
261 | ; consider only threads with strictly higher priority than the current one, |
||
262 | ; keep running the current thread if other ready threads have the same or lower priority |
||
3534 | clevermous | 263 | ;retval: |
264 | ; ebx = address of the APPDATA for the selected task (slot-base) |
||
265 | ; edi = address of the TASKDATA for the selected task |
||
266 | ; ZF = 1 if the task is the same |
||
267 | ;warning: |
||
8869 | rgimad | 268 | ; [current_slot_idx] = bh , [TASK_BASE] = edi -- as result |
3534 | clevermous | 269 | ; [current_slot] is not set to new value (ebx)!!! |
270 | ;scratched: eax,ecx |
||
271 | proc find_next_task |
||
272 | call update_counters |
||
273 | spin_lock_irqsave SchedulerLock |
||
3615 | clevermous | 274 | push NR_SCHED_QUEUES |
275 | ; If bl == SCHEDULE_ANY_PRIORITY = 0, loop over all NR_SCHED lists. |
||
276 | ; Otherwise, loop over first [APPDATA.priority] lists. |
||
277 | test bl, bl |
||
278 | jz .start |
||
279 | mov ebx, [current_slot] |
||
280 | mov edi, [TASK_BASE] |
||
281 | mov eax, [ebx+APPDATA.priority] |
||
282 | test eax, eax |
||
283 | jz .unlock_found |
||
284 | mov [esp], eax |
||
285 | .start: |
||
3534 | clevermous | 286 | xor ecx, ecx |
287 | .priority_loop: |
||
288 | mov ebx, [scheduler_current+ecx*4] |
||
289 | test ebx, ebx |
||
290 | jz .priority_next |
||
291 | .task_loop: |
||
292 | mov ebx, [ebx+APPDATA.in_schedule.next] |
||
293 | mov edi, ebx |
||
294 | shr edi, 3 |
||
8869 | rgimad | 295 | add edi, TASK_TABLE - (SLOT_BASE shr 3) |
3534 | clevermous | 296 | mov al, [edi+TASKDATA.state] |
297 | test al, al |
||
298 | jz .task_found ; state == 0 |
||
299 | cmp al, 5 |
||
300 | jne .task_next ; state == 1,2,3,4,9 |
||
301 | ; state == 5 |
||
302 | pushad ; more freedom for [APPDATA.wait_test] |
||
303 | call [ebx+APPDATA.wait_test] |
||
304 | mov [esp+28], eax |
||
305 | popad |
||
306 | or eax, eax |
||
307 | jnz @f |
||
308 | ; testing for timeout |
||
309 | mov eax, [timer_ticks] |
||
310 | sub eax, [ebx+APPDATA.wait_begin] |
||
311 | cmp eax, [ebx+APPDATA.wait_timeout] |
||
312 | jb .task_next |
||
313 | xor eax, eax |
||
314 | @@: |
||
315 | mov [ebx+APPDATA.wait_param], eax ; retval for wait |
||
8874 | rgimad | 316 | mov [edi+TASKDATA.state], TSTATE_RUNNING |
3534 | clevermous | 317 | .task_found: |
318 | mov [scheduler_current+ecx*4], ebx |
||
3617 | clevermous | 319 | ; If we have selected a thread with higher priority |
320 | ; AND rescheduling is due to IRQ, |
||
321 | ; turn the current scheduler list one entry back, |
||
322 | ; so the current thread will be next after high-priority thread is done. |
||
323 | mov ecx, [esp] |
||
324 | cmp ecx, NR_SCHED_QUEUES |
||
325 | jz .unlock_found |
||
326 | mov eax, [current_slot] |
||
327 | mov eax, [eax+APPDATA.in_schedule.prev] |
||
328 | mov [scheduler_current+ecx*4], eax |
||
3615 | clevermous | 329 | .unlock_found: |
330 | pop ecx |
||
3534 | clevermous | 331 | spin_unlock_irqrestore SchedulerLock |
332 | .found: |
||
8093 | dunkaist | 333 | ; the line below assumes APPDATA is 256 bytes long and SLOT_BASE is |
334 | ; aligned on 0x10000 |
||
8869 | rgimad | 335 | mov byte [current_slot_idx], bh |
3534 | clevermous | 336 | mov [TASK_BASE], edi |
337 | rdtsc ;call _rdtsc |
||
338 | mov [edi+TASKDATA.counter_add], eax; for next using update_counters |
||
339 | cmp ebx, [current_slot] |
||
340 | ret |
||
341 | .task_next: |
||
342 | cmp ebx, [scheduler_current+ecx*4] |
||
343 | jnz .task_loop |
||
344 | .priority_next: |
||
345 | inc ecx |
||
3615 | clevermous | 346 | cmp ecx, [esp] |
3534 | clevermous | 347 | jb .priority_loop |
3615 | clevermous | 348 | mov ebx, [current_slot] |
349 | mov edi, [TASK_BASE] |
||
350 | jmp .unlock_found |
||
3534 | clevermous | 351 | endp |
352 | |||
2288 | clevermous | 353 | if 0 |
354 | |||
355 | struc TIMER |
||
356 | { |
||
357 | .next dd ? |
||
358 | .exp_time dd ? |
||
359 | .func dd ? |
||
360 | .arg dd ? |
||
361 | } |
||
362 | |||
363 | |||
364 | uglobal |
||
365 | rdy_head rd 16 |
||
366 | endg |
||
367 | |||
368 | align 4 |
||
369 | pick_task: |
||
370 | |||
371 | xor eax, eax |
||
372 | .pick: |
||
373 | mov ebx, [rdy_head+eax*4] |
||
374 | test ebx, ebx |
||
375 | jz .next |
||
376 | |||
377 | mov [next_task], ebx |
||
378 | test [ebx+flags.billable] |
||
379 | jz @F |
||
380 | mov [bill_task], ebx |
||
381 | @@: |
||
382 | ret |
||
383 | .next: |
||
384 | inc eax |
||
385 | jmp .pick |
||
386 | |||
387 | ; param |
||
388 | ; eax= task |
||
389 | ; |
||
390 | ; retval |
||
391 | ; eax= task |
||
392 | ; ebx= queue |
||
393 | ; ecx= front if 1 or back if 0 |
||
394 | align 4 |
||
395 | shed: |
||
396 | cmp [eax+.tics_left], 0;signed compare |
||
397 | mov ebx, [eax+.priority] |
||
398 | setg ecx |
||
399 | jg @F |
||
400 | |||
401 | mov edx, [eax+.tics_quantum] |
||
402 | mov [eax+.ticks_left], edx |
||
403 | cmp ebx, (IDLE_PRIORITY-1) |
||
404 | je @F |
||
405 | inc ebx |
||
406 | @@: |
||
407 | ret |
||
408 | |||
409 | ; param |
||
410 | ; eax= task |
||
411 | align 4 |
||
412 | enqueue: |
||
413 | call shed;eax |
||
414 | cmp [rdy_head+ebx*4], 0 |
||
415 | jnz @F |
||
416 | |||
417 | mov [rdy_head+ebx*4], eax |
||
418 | mov [rdy_tail+ebx*4], eax |
||
419 | mov [eax+.next_ready], 0 |
||
420 | jmp .pick |
||
421 | @@: |
||
422 | test ecx, ecx |
||
423 | jz .back |
||
424 | |||
425 | mov ecx, [rdy_head+ebx*4] |
||
426 | mov [eax+.next_ready], ecx |
||
427 | mov [rdy_head+ebx*4], eax |
||
428 | jmp .pick |
||
429 | .back: |
||
430 | mov ecx, [rdy_tail+ebx*4] |
||
431 | mov [ecx+.next_ready], eax |
||
432 | mov [rdy_tail+ebx*4], eax |
||
433 | mov [eax+.next_ready], 0 |
||
434 | .pick: |
||
435 | call pick_proc;select next task |
||
436 | ret |
||
437 | |||
438 | end if---><---><<---> |