Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
431 serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2465 Serge 3
;; Copyright (C) KolibriOS team 2004-2012. 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: 2465 $
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
1055 Galkov 32
        btr     dword[DONT_SWITCH], 0
33
        jc      .return
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
465 serge 46
if 0
1055 Galkov 47
; \begin{Mario79} ; <- must be refractoried, if used...
48
        cmp     [dma_task_switched], 1
49
        jne     .find_next_task
50
        mov     [dma_task_switched], 0
51
        mov     ebx, [dma_process]
52
        cmp     [CURRENT_TASK], ebx
53
        je      .return
54
        mov     edi, [dma_slot_ptr]
55
        mov     [CURRENT_TASK], ebx
56
        mov     [TASK_BASE], edi
57
        jmp     @f
187 diamond 58
.find_next_task:
59
; \end{Mario79}
465 serge 60
end if
1055 Galkov 61
        call    find_next_task
62
        jz      .return  ; the same task -> skip switch
2434 Serge 63
  @@:
64
        mov     byte[DONT_SWITCH], 1
1055 Galkov 65
        call    do_change_task
66
  .return:
67
        popad
68
        popfd
69
        ret
465 serge 70
 
101 poddubny 71
uglobal
1055 Galkov 72
align 4
73
;  far_jump:
74
;   .offs dd ?
75
;   .sel  dw ?
76
   context_counter     dd 0 ;noname & halyavin
77
   next_usage_update   dd 0
78
   timer_ticks         dd 0
79
;  prev_slot           dd ?
80
;  event_sched         dd ?
101 poddubny 81
endg
82
 
1055 Galkov 83
align 4
101 poddubny 84
update_counters:
1055 Galkov 85
        mov     edi, [TASK_BASE]
86
        rdtsc
87
        sub     eax, [edi+TASKDATA.counter_add] ; time stamp counter add
88
        add     [edi+TASKDATA.counter_sum], eax ; counter sum
89
        ret
90
align 4
91
updatecputimes:
2434 Serge 92
        xor     eax, eax
93
        xchg    eax, [idleuse]
94
        mov     [idleusesec], eax
1055 Galkov 95
        mov     ecx, [TASK_COUNT]
96
        mov     edi, TASK_DATA
97
  .newupdate:
2434 Serge 98
        xor     eax, eax
99
        xchg    eax, [edi+TASKDATA.counter_sum]
100
        mov     [edi+TASKDATA.cpu_usage], eax
101
        add     edi, 0x20
1055 Galkov 102
        loop    .newupdate
103
        ret
1 ha 104
 
1055 Galkov 105
align 4
101 poddubny 106
find_next_task:
1055 Galkov 107
;info:
108
;   Find next task to execute
109
;retval:
110
;   ebx = address of the APPDATA for the selected task (slot-base)
111
;   esi = previous slot-base ([current_slot] at the begin)
112
;   edi = address of the TASKDATA for the selected task
113
;   ZF  = 1  if the task is the same
114
;warning:
115
;   [CURRENT_TASK] = bh , [TASK_BASE] = edi -- as result
116
;   [current_slot] is not set to new value (ebx)!!!
117
;scratched: eax,ecx
118
        call    update_counters ; edi := [TASK_BASE]
119
        Mov     esi, ebx, [current_slot]
120
  .loop:
2434 Serge 121
        cmp     bh, [TASK_COUNT]
1055 Galkov 122
        jb      @f
123
        xor     bh, bh
2434 Serge 124
        mov     edi, CURRENT_TASK
125
  @@:
126
        inc     bh       ; ebx += APPDATA.size
127
        add     edi, 0x20; edi += TASKDATA.size
1055 Galkov 128
        mov     al, [edi+TASKDATA.state]
129
        test    al, al
130
        jz      .found   ; state == 0
131
        cmp     al, 5
132
        jne     .loop    ; state == 1,2,3,4,9
133
      ; state == 5
134
        pushad  ; more freedom for [APPDATA.wait_test]
135
        call    [ebx+APPDATA.wait_test]
2434 Serge 136
        mov     [esp+28], eax
1055 Galkov 137
        popad
2434 Serge 138
        or      eax, eax
1055 Galkov 139
        jnz     @f
140
      ; testing for timeout
141
        mov     ecx, [timer_ticks]
142
        sub     ecx, [ebx+APPDATA.wait_begin]
143
        cmp     ecx, [ebx+APPDATA.wait_timeout]
144
        jb      .loop
2434 Serge 145
  @@:
146
        mov     [ebx+APPDATA.wait_param], eax  ; retval for wait
1055 Galkov 147
        mov     [edi+TASKDATA.state], 0
148
  .found:
2434 Serge 149
        mov     [CURRENT_TASK], bh
150
        mov     [TASK_BASE], edi
1055 Galkov 151
        rdtsc   ;call  _rdtsc
2434 Serge 152
        mov     [edi+TASKDATA.counter_add], eax; for next using update_counters
1055 Galkov 153
        cmp     ebx, esi ;esi - previous slot-base
154
        ret
155
;TODO: Надо бы убрать использование do_change_task из V86...
156
; и после этого перенести обработку TASKDATA.counter_add/sum в do_change_task
1 ha 157
 
1055 Galkov 158
align 4
101 poddubny 159
do_change_task:
1055 Galkov 160
;param:
161
;   ebx = address of the APPDATA for incoming task (new)
162
;warning:
163
;   [CURRENT_TASK] and [TASK_BASE] must be changed before (e.g. in find_next_task)
164
;   [current_slot] is the outcoming (old), and set here to a new value (ebx)
165
;scratched: eax,ecx,esi
2434 Serge 166
        mov     esi, ebx
167
        xchg    esi, [current_slot]
1220 serge 168
; set new stack after saving old
1055 Galkov 169
        mov     [esi+APPDATA.saved_esp], esp
170
        mov     esp, [ebx+APPDATA.saved_esp]
1220 serge 171
; set new thread io-map
1055 Galkov 172
        Mov     dword [page_tabs+((tss._io_map_0 and -4096) shr 10)],eax,[ebx+APPDATA.io_map]
173
        Mov     dword [page_tabs+((tss._io_map_1 and -4096) shr 10)],eax,[ebx+APPDATA.io_map+4]
1220 serge 174
; set new thread memory-map
1055 Galkov 175
        mov     ecx, APPDATA.dir_table
176
        mov     eax, [ebx+ecx]      ;offset>0x7F
177
        cmp     eax, [esi+ecx]      ;offset>0x7F
178
        je      @f
179
        mov     cr3, eax
1220 serge 180
@@:
181
; set tss.esp0
182
 
1055 Galkov 183
        Mov     [tss._esp0],eax,[ebx+APPDATA.saved_esp0]
1220 serge 184
 
2434 Serge 185
        mov     edx, [ebx+APPDATA.tls_base]
186
        cmp     edx, [esi+APPDATA.tls_base]
187
        je      @f
1220 serge 188
 
2434 Serge 189
        mov     [tls_data_l+2], dx
190
        shr     edx, 16
191
        mov     [tls_data_l+4], dl
192
        mov     [tls_data_l+7], dh
1220 serge 193
 
2434 Serge 194
        mov     dx, app_tls
195
        mov     fs, dx
1298 diamond 196
@@:
1220 serge 197
; set gs selector unconditionally
1055 Galkov 198
        Mov     gs,ax,graph_data
199
      ; set CR0.TS
200
        cmp     bh, byte[fpu_owner] ;bh == incoming task (new)
201
        clts                        ;clear a task switch flag
202
        je      @f
203
        mov     eax, cr0            ;and set it again if the owner
204
        or      eax, CR0_TS         ;of a fpu has changed
205
        mov     cr0, eax
206
  @@: ; set context_counter (only for user pleasure ???)
207
        inc     [context_counter]   ;noname & halyavin
208
      ; set debug-registers, if it's necessary
209
        test    byte[ebx+APPDATA.dbg_state], 1
210
        jz      @f
211
        xor     eax, eax
212
        mov     dr6, eax
2434 Serge 213
        lea     esi, [ebx+ecx+APPDATA.dbg_regs-APPDATA.dir_table];offset>0x7F
1055 Galkov 214
        cld
215
  macro lodsReg [reg] {
216
        lodsd
2434 Serge 217
        mov     reg, eax
1055 Galkov 218
  }     lodsReg dr0, dr1, dr2, dr3, dr7
219
  purge lodsReg
2434 Serge 220
  @@:
221
        ret
1055 Galkov 222
;end.
465 serge 223
 
1434 serge 224
 
225
 
2434 Serge 226
struct  MUTEX_WAITER
227
        list    LHEAD
228
        task    dd ?
229
ends
1434 serge 230
 
231
;void  __fastcall mutex_init(struct mutex *lock)
232
 
233
align 4
234
mutex_init:
2434 Serge 235
        mov     [ecx+MUTEX.lhead.next], ecx
236
        mov     [ecx+MUTEX.lhead.prev], ecx
237
        mov     [ecx+MUTEX.count], 1
1434 serge 238
        ret
239
 
240
 
241
;void  __fastcall mutex_lock(struct mutex *lock)
242
 
243
align 4
244
mutex_lock:
245
 
2434 Serge 246
        dec     [ecx+MUTEX.count]
247
        jns     .done
1434 serge 248
 
249
        pushfd
250
        cli
251
 
2434 Serge 252
        sub     esp, sizeof.MUTEX_WAITER
1434 serge 253
 
2143 serge 254
        list_add_tail esp, ecx      ;esp= new waiter, ecx= list head
1434 serge 255
 
2434 Serge 256
        mov     edx, [TASK_BASE]
257
        mov     [esp+MUTEX_WAITER.task], edx
1434 serge 258
 
259
.forever:
260
 
2434 Serge 261
        mov     eax, -1
262
        xchg    eax, [ecx+MUTEX.count]
263
        dec     eax
264
        jz      @F
1434 serge 265
 
2434 Serge 266
        mov     [edx+TASKDATA.state], 1
267
        call    change_task
268
        jmp     .forever
1434 serge 269
@@:
2434 Serge 270
        mov     edx, [esp+MUTEX_WAITER.list.next]
271
        mov     eax, [esp+MUTEX_WAITER.list.prev]
1434 serge 272
 
2434 Serge 273
        mov     [eax+MUTEX_WAITER.list.next], edx
274
        mov     [edx+MUTEX_WAITER.list.prev], eax
275
        cmp     [ecx+MUTEX.lhead.next], ecx
276
        jne     @F
1434 serge 277
 
2434 Serge 278
        mov     [ecx+MUTEX.count], 0
1434 serge 279
@@:
2434 Serge 280
        add     esp, sizeof.MUTEX_WAITER
1434 serge 281
 
282
        popfd
283
.done:
284
        ret
285
 
286
;void  __fastcall mutex_unlock(struct mutex *lock)
287
 
288
align 4
289
mutex_unlock:
290
 
291
        pushfd
292
        cli
293
 
2434 Serge 294
        mov     eax, [ecx+MUTEX.lhead.next]
295
        cmp     eax, ecx
296
        mov     [ecx+MUTEX.count], 1
297
        je      @F
1434 serge 298
 
2434 Serge 299
        mov     eax, [eax+MUTEX_WAITER.task]
300
        mov     [eax+TASKDATA.state], 0
1434 serge 301
@@:
302
        popfd
303
        ret
304
 
305
 
306
purge MUTEX_WAITER
307
 
465 serge 308
if 0
1434 serge 309
 
465 serge 310
struc TIMER
311
{
312
  .next      dd ?
313
  .exp_time  dd ?
314
  .func      dd ?
1055 Galkov 315
  .arg       dd ?
465 serge 316
}
317
 
1276 Lrz 318
 
1055 Galkov 319
MAX_PROIRITY         0   ; highest, used for kernel tasks
320
MAX_USER_PRIORITY    0   ; highest priority for user processes
321
USER_PRIORITY        7   ; default (should correspond to nice 0)
322
MIN_USER_PRIORITY   14   ; minimum priority for user processes
323
IDLE_PRIORITY       15   ; lowest, only IDLE process goes here
324
NR_SCHED_QUEUES     16   ; MUST equal IDLE_PRIORYTY + 1
465 serge 325
 
1276 Lrz 326
uglobal
465 serge 327
rdy_head   rd 16
1276 Lrz 328
endg
465 serge 329
 
330
align 4
331
pick_task:
332
 
2434 Serge 333
        xor     eax, eax
1055 Galkov 334
  .pick:
2434 Serge 335
        mov     ebx, [rdy_head+eax*4]
336
        test    ebx, ebx
337
        jz      .next
465 serge 338
 
2434 Serge 339
        mov     [next_task], ebx
340
        test    [ebx+flags.billable]
341
        jz      @F
342
        mov     [bill_task], ebx
1055 Galkov 343
  @@:
2434 Serge 344
        ret
1055 Galkov 345
  .next:
2434 Serge 346
        inc     eax
347
        jmp     .pick
465 serge 348
 
349
; param
350
;  eax= task
351
;
352
; retval
353
;  eax= task
354
;  ebx= queue
355
;  ecx= front if 1 or back if 0
356
align 4
357
shed:
2434 Serge 358
        cmp     [eax+.tics_left], 0;signed compare
359
        mov     ebx, [eax+.priority]
360
        setg    ecx
361
        jg      @F
465 serge 362
 
2434 Serge 363
        mov     edx, [eax+.tics_quantum]
364
        mov     [eax+.ticks_left], edx
365
        cmp     ebx, (IDLE_PRIORITY-1)
366
        je      @F
367
        inc     ebx
1055 Galkov 368
  @@:
2434 Serge 369
        ret
465 serge 370
 
371
; param
372
;  eax= task
373
align 4
374
enqueue:
2434 Serge 375
        call    shed;eax
376
        cmp     [rdy_head+ebx*4], 0
377
        jnz     @F
465 serge 378
 
2434 Serge 379
        mov     [rdy_head+ebx*4], eax
380
        mov     [rdy_tail+ebx*4], eax
381
        mov     [eax+.next_ready], 0
382
        jmp     .pick
1055 Galkov 383
  @@:
2434 Serge 384
        test    ecx, ecx
385
        jz      .back
465 serge 386
 
2434 Serge 387
        mov     ecx, [rdy_head+ebx*4]
388
        mov     [eax+.next_ready], ecx
389
        mov     [rdy_head+ebx*4], eax
390
        jmp     .pick
1055 Galkov 391
  .back:
2434 Serge 392
        mov     ecx, [rdy_tail+ebx*4]
393
        mov     [ecx+.next_ready], eax
394
        mov     [rdy_tail+ebx*4], eax
395
        mov     [eax+.next_ready], 0
1055 Galkov 396
  .pick:
2434 Serge 397
        call    pick_proc;select next task
398
        ret
465 serge 399
 
400
end if