Subversion Repositories Kolibri OS

Rev

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