Subversion Repositories Kolibri OS

Rev

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

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