Subversion Repositories Kolibri OS

Rev

Rev 5565 | Rev 5984 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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