Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
444 serge 1
$Revision: 434 $
436 serge 2
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3
;;                                                              ;;
4
;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
5
;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa      ;;
6
;; Distributed under terms of the GNU General Public License    ;;
7
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8
 
1 ha 9
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
;; IRQ0 HANDLER (TIMER INTERRUPT) ;;
11
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12
 
101 poddubny 13
 
1 ha 14
align 32
15
irq0:
420 serge 16
        pushfd
17
        pushad
18
        mov   ax, app_data  ;
19
        mov   ds, ax
20
        mov   es, ax
1 ha 21
 
22
        inc   dword [timer_ticks]
23
 
24
        mov   eax, [timer_ticks]
22 poddubny 25
        call  playNote           ; <<<--- Speaker driver
26
 
1 ha 27
        cmp   eax,[next_usage_update]
28
        jb    .nocounter
29
        add   eax,100
30
        mov   [next_usage_update],eax
31
        call  updatecputimes
420 serge 32
.nocounter:
381 serge 33
        cmp   [DONT_SWITCH], byte 1
101 poddubny 34
        jne   .change_task
35
 
36
        mov   al,0x20   ; send End Of Interrupt signal
37
        mov   dx,0x20
38
        out   dx,al
39
 
381 serge 40
        mov   [DONT_SWITCH], byte 0
101 poddubny 41
 
420 serge 42
        popad
43
        popfd
44
        iretd
101 poddubny 45
 
420 serge 46
.change_task:
101 poddubny 47
        call  update_counters
48
 
49
        call  find_next_task
50
        mov   ecx, eax
51
 
52
        mov   al,0x20   ; send End Of Interrupt signal
53
        mov   dx,0x20
54
        out   dx,al
55
 
56
	test  ecx, ecx  ; if there is only one running process
57
        jnz   .return
58
 
59
        call  do_change_task
379 serge 60
 
420 serge 61
.return:
62
        popad
63
        popfd
64
        iretd
101 poddubny 65
 
66
 
67
align 4
68
change_task:
69
 
70
        pushfd
71
        cli
72
        pushad
73
 
74
        call  update_counters
421 serge 75
 
76
if 0
77
 
187 diamond 78
; \begin{Mario79}
79
        cmp     [dma_task_switched], 1
80
        jne     .find_next_task
81
        mov     [dma_task_switched], 0
82
        mov     ebx, [dma_process]
379 serge 83
        cmp     [CURRENT_TASK], ebx
187 diamond 84
        je      .return
85
        mov     edi, [dma_slot_ptr]
379 serge 86
        mov     [CURRENT_TASK], ebx
87
        mov     [TASK_BASE], edi
187 diamond 88
        jmp     @f
89
.find_next_task:
90
; \end{Mario79}
421 serge 91
 
92
end if
93
 
101 poddubny 94
        call  find_next_task
95
        test  eax, eax    ; the same task -> skip switch
96
        jnz    .return
187 diamond 97
@@:
381 serge 98
        mov   [DONT_SWITCH],byte 1
101 poddubny 99
        call  do_change_task
100
 
420 serge 101
.return:
101 poddubny 102
        popad
103
        popfd
104
        ret
105
 
106
 
107
uglobal
108
   align 4
109
   far_jump:
110
    .offs dd ?
111
    .sel  dw ?
112
   context_counter     dd ? ;noname & halyavin
113
   next_usage_update   dd ?
114
   timer_ticks         dd ?
115
   prev_slot           dd ?
116
   event_sched         dd ?
117
endg
118
 
119
 
120
update_counters:
379 serge 121
        mov   edi, [TASK_BASE]
115 poddubny 122
        mov   ebx, [edi+TASKDATA.counter_add] ; time stamp counter add
420 serge 123
        rdtsc
1 ha 124
        sub   eax, ebx
115 poddubny 125
        add   eax, [edi+TASKDATA.counter_sum] ; counter sum
126
        mov   [edi+TASKDATA.counter_sum], eax
101 poddubny 127
ret
1 ha 128
 
101 poddubny 129
 
130
; Find next task to execute
131
; result: ebx = number of the selected task
102 poddubny 132
;         eax = 1  if the task is the same
133
;         edi = address of the data for the task in ebx
134
;         [0x3000] = ebx and [0x3010] = edi
135
;         corrupts other regs
101 poddubny 136
find_next_task:
379 serge 137
        mov   ebx, [CURRENT_TASK]
138
        mov   edi, [TASK_BASE]
21 poddubny 139
        mov   [prev_slot], ebx
1 ha 140
 
420 serge 141
.waiting_for_termination:
142
.waiting_for_reuse:
143
.waiting_for_event:
144
.suspended:
379 serge 145
        cmp   ebx, [TASK_COUNT]
21 poddubny 146
        jb    @f
379 serge 147
        mov   edi, CURRENT_TASK
21 poddubny 148
        xor   ebx, ebx
427 serge 149
@@:
21 poddubny 150
 
1 ha 151
        add   edi,0x20
152
        inc   ebx
153
 
115 poddubny 154
        mov   al, byte [edi+TASKDATA.state]
101 poddubny 155
        test  al, al
156
        jz    .found
40 halyavin 157
	cmp   al, 1
158
	jz    .suspended
159
	cmp   al, 2
160
	jz    .suspended
1 ha 161
        cmp   al, 3
162
        je    .waiting_for_termination
163
        cmp   al, 4
164
        je    .waiting_for_termination
165
        cmp   al, 9
166
        je    .waiting_for_reuse
167
 
379 serge 168
        mov   [CURRENT_TASK],ebx
169
        mov   [TASK_BASE],edi
1 ha 170
 
21 poddubny 171
        cmp   al, 5
172
        jne   .noevents
173
        call  get_event_for_app
174
        test  eax, eax
175
        jz    .waiting_for_event
176
        mov   [event_sched], eax
115 poddubny 177
        mov   [edi+TASKDATA.state], byte 0
420 serge 178
.noevents:
179
.found:
379 serge 180
        mov   [CURRENT_TASK],ebx
181
        mov   [TASK_BASE],edi
420 serge 182
        rdtsc                     ;call  _rdtsc
115 poddubny 183
        mov   [edi+TASKDATA.counter_add],eax
1 ha 184
 
420 serge 185
        mov esi, [prev_slot]
101 poddubny 186
	xor   eax, eax
420 serge 187
        cmp   ebx, esi
101 poddubny 188
        sete  al
189
ret
190
 
420 serge 191
; param
192
;  ebx = incoming task
193
;  esi = outcomig task
194
 
101 poddubny 195
do_change_task:
420 serge 196
 
197
        shl ebx, 8
198
        add ebx, SLOT_BASE
199
        mov [current_slot], ebx
200
        shl esi, 8
201
        add esi, SLOT_BASE
202
 
203
        mov [esi+APPDATA.saved_esp], esp
204
        mov esp, [ebx+APPDATA.saved_esp]
205
 
430 serge 206
; set thread io map
207
 
208
        mov ecx, [ebx+APPDATA.io_map]
209
        mov edx, [ebx+APPDATA.io_map+4]
210
        mov dword [page_tabs+((tss._io_map_0 and -4096) shr 10)], ecx
211
        mov dword [page_tabs+((tss._io_map_1 and -4096) shr 10)], edx
212
 
420 serge 213
        mov eax, [ebx+APPDATA.dir_table]
214
        mov cr3, eax
215
        mov ebx, [ebx+APPDATA.pl0_stack]
216
        add ebx, RING0_STACK_SIZE
430 serge 217
        mov [tss._esp0], ebx
420 serge 218
        mov ecx, cr0
219
        or ecx, CR0_TS   ;set task switch flag
220
        mov cr0, ecx
9 halyavin 221
        inc   [context_counter] ;noname & halyavin
1 ha 222
 
420 serge 223
        ret
8 poddubny 224
 
1 ha 225
align 4
226
updatecputimes:
227
 
228
        mov  eax,[idleuse]
229
        mov  [idleusesec],eax
230
        mov  [idleuse],dword 0
379 serge 231
        mov  ecx, [TASK_COUNT]
232
        mov  edi, TASK_DATA
420 serge 233
.newupdate:
115 poddubny 234
        mov  ebx,[edi+TASKDATA.counter_sum]
235
        mov  [edi+TASKDATA.cpu_usage],ebx
236
        mov  [edi+TASKDATA.counter_sum],dword 0
1 ha 237
        add  edi,0x20
238
        dec  ecx
239
        jnz  .newupdate
240
 
241
        ret
427 serge 242
 
243
if 0
244
 
432 serge 245
 
246
struc TIMER
247
{
248
  .next      dd ?
249
  .exp_time  dd ?
250
  .func      dd ?
251
  .arg       dd ?
252
}
253
 
254
 
255
 
256
 
257
 
258
 
259
 
260
 
261
 
427 serge 262
MAX_PROIRITY         0   ; highest, used for kernel tasks
263
MAX_USER_PRIORITY    0   ; highest priority for user processes
264
USER_PRIORITY        7   ; default (should correspond to nice 0)
265
MIN_USER_PRIORITY   14   ; minimum priority for user processes
266
IDLE_PRIORITY       15   ; lowest, only IDLE process goes here
267
NR_SCHED_QUEUES     16   ; MUST equal IDLE_PRIORYTY + 1
268
 
269
rdy_head   rd 16
270
 
271
 
272
align 4
273
pick_task:
274
 
275
           xor eax, eax
276
.pick:
277
           mov ebx, [rdy_head+eax*4]
278
           test ebx, ebx
279
           jz .next
280
 
281
           mov [next_task], ebx
282
           test [ebx+flags.billable]
283
           jz @F
284
           mov [bill_task], ebx
285
@@:
286
           ret
287
.next:
288
           inc eax
289
           jmp .pick
290
 
291
 
292
; param
293
;  eax= task
294
;
295
; retval
296
;  eax= task
297
;  ebx= queue
298
;  ecx= front if 1 or back if 0
299
 
300
align 4
301
shed:
302
           cmp [eax+.tics_left], 0 ;signed compare
303
           mov ebx, [eax+.priority]
304
           setg ecx
305
           jg @F
306
 
307
           mov edx, [eax+.tics_quantum]
308
           mov [eax+.ticks_left], edx
309
           cmp ebx, (IDLE_PRIORITY-1)
310
           je @F
311
           inc ebx
312
@@:
313
           ret
314
 
315
; param
316
;  eax= task
317
 
318
align 4
319
enqueue:
320
          call shed  ;eax
321
          cmp [rdy_head+ebx*4],0
322
          jnz @F
323
 
324
          mov [rdy_head+ebx*4], eax
325
          mov [rdy_tail+ebx*4], eax
326
          mov [eax+.next_ready], 0
327
          jmp .pick
328
@@:
329
          test ecx, ecx
330
          jz .back
331
 
332
          mov ecx, [rdy_head+ebx*4]
333
          mov [eax+.next_ready], ecx
334
          mov [rdy_head+ebx*4], eax
335
          jmp .pick
336
.back:
337
          mov ecx, [rdy_tail+ebx*4]
338
          mov [ecx+.next_ready], eax
339
          mov [rdy_tail+ebx*4], eax
340
          mov [eax+.next_ready], 0
341
.pick:
342
          call pick_proc     ;select next task
343
          ret
344
 
345
end if
346