Subversion Repositories Kolibri OS

Rev

Rev 794 | Rev 1055 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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