Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
2455 mario79 3
;; Copyright (C) KolibriOS team 2004-2012. All rights reserved. ;;
2288 clevermous 4
;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa      ;;
5
;; Distributed under terms of the GNU General Public License    ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
$Revision: 3539 $
9
 
10
 
11
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12
;; IRQ0 HANDLER (TIMER INTERRUPT) ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
15
 
16
align 32
17
irq0:
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
24
        sub     eax, [next_usage_update]
25
        cmp     eax, 100
26
        jb      .nocounter
27
        add     [next_usage_update], 100
28
        call    updatecputimes
29
  .nocounter:
30
        xor     ecx, ecx        ; send End Of Interrupt signal
31
        call    irq_eoi
3504 mario79 32
;        btr     dword[DONT_SWITCH], 0
33
;        jc      .return
2288 clevermous 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
40
 
41
align 4
42
change_task:
43
        pushfd
44
        cli
45
        pushad
46
if 0
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
58
.find_next_task:
59
; \end{Mario79}
60
end if
61
        call    find_next_task
62
        jz      .return  ; the same task -> skip switch
63
  @@:
3504 mario79 64
;        mov     byte[DONT_SWITCH], 1
2288 clevermous 65
        call    do_change_task
66
  .return:
67
        popad
68
        popfd
69
        ret
70
 
71
uglobal
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 ?
81
endg
82
 
83
align 4
84
update_counters:
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:
92
        mov     ecx, [TASK_COUNT]
93
        mov     edi, TASK_DATA
94
  .newupdate:
95
        xor     eax, eax
96
        xchg    eax, [edi+TASKDATA.counter_sum]
97
        mov     [edi+TASKDATA.cpu_usage], eax
98
        add     edi, 0x20
99
        loop    .newupdate
100
        ret
101
 
3539 clevermous 102
;TODO: Надо бы убрать использование do_change_task из V86...
103
; и после этого перенести обработку TASKDATA.counter_add/sum в do_change_task
2288 clevermous 104
 
105
align 4
106
do_change_task:
107
;param:
108
;   ebx = address of the APPDATA for incoming task (new)
109
;warning:
110
;   [CURRENT_TASK] and [TASK_BASE] must be changed before (e.g. in find_next_task)
111
;   [current_slot] is the outcoming (old), and set here to a new value (ebx)
112
;scratched: eax,ecx,esi
113
        mov     esi, ebx
114
        xchg    esi, [current_slot]
115
; set new stack after saving old
116
        mov     [esi+APPDATA.saved_esp], esp
117
        mov     esp, [ebx+APPDATA.saved_esp]
118
; set new thread io-map
119
        Mov     dword [page_tabs+((tss._io_map_0 and -4096) shr 10)],eax,[ebx+APPDATA.io_map]
120
        Mov     dword [page_tabs+((tss._io_map_1 and -4096) shr 10)],eax,[ebx+APPDATA.io_map+4]
121
; set new thread memory-map
122
        mov     ecx, APPDATA.dir_table
123
        mov     eax, [ebx+ecx]      ;offset>0x7F
124
        cmp     eax, [esi+ecx]      ;offset>0x7F
125
        je      @f
126
        mov     cr3, eax
127
@@:
128
; set tss.esp0
129
 
130
        Mov     [tss._esp0],eax,[ebx+APPDATA.saved_esp0]
131
 
132
        mov     edx, [ebx+APPDATA.tls_base]
133
        cmp     edx, [esi+APPDATA.tls_base]
134
        je      @f
135
 
136
        mov     [tls_data_l+2], dx
137
        shr     edx, 16
138
        mov     [tls_data_l+4], dl
139
        mov     [tls_data_l+7], dh
140
 
141
        mov     dx, app_tls
142
        mov     fs, dx
143
@@:
144
; set gs selector unconditionally
145
        Mov     gs,ax,graph_data
146
      ; set CR0.TS
147
        cmp     bh, byte[fpu_owner] ;bh == incoming task (new)
148
        clts                        ;clear a task switch flag
149
        je      @f
150
        mov     eax, cr0            ;and set it again if the owner
151
        or      eax, CR0_TS         ;of a fpu has changed
152
        mov     cr0, eax
153
  @@: ; set context_counter (only for user pleasure ???)
154
        inc     [context_counter]   ;noname & halyavin
155
      ; set debug-registers, if it's necessary
156
        test    byte[ebx+APPDATA.dbg_state], 1
157
        jz      @f
158
        xor     eax, eax
159
        mov     dr6, eax
160
        lea     esi, [ebx+ecx+APPDATA.dbg_regs-APPDATA.dir_table];offset>0x7F
161
        cld
162
  macro lodsReg [reg] {
163
        lodsd
164
        mov     reg, eax
165
  }     lodsReg dr0, dr1, dr2, dr3, dr7
166
  purge lodsReg
167
  @@:
168
        ret
169
;end.
170
 
171
 
172
 
2381 hidnplayr 173
struct  MUTEX_WAITER
174
        list    LHEAD
175
        task    dd ?
176
ends
2288 clevermous 177
 
178
;void  __fastcall mutex_init(struct mutex *lock)
179
 
180
align 4
181
mutex_init:
2381 hidnplayr 182
        mov     [ecx+MUTEX.lhead.next], ecx
183
        mov     [ecx+MUTEX.lhead.prev], ecx
2288 clevermous 184
        mov     [ecx+MUTEX.count], 1
185
        ret
186
 
187
 
188
;void  __fastcall mutex_lock(struct mutex *lock)
189
 
190
align 4
191
mutex_lock:
192
 
193
        dec     [ecx+MUTEX.count]
194
        jns     .done
195
 
196
        pushfd
197
        cli
198
 
2381 hidnplayr 199
        sub     esp, sizeof.MUTEX_WAITER
2288 clevermous 200
 
201
        list_add_tail esp, ecx      ;esp= new waiter, ecx= list head
202
 
203
        mov     edx, [TASK_BASE]
204
        mov     [esp+MUTEX_WAITER.task], edx
205
 
206
.forever:
207
 
208
        mov     eax, -1
209
        xchg    eax, [ecx+MUTEX.count]
210
        dec     eax
211
        jz      @F
212
 
213
        mov     [edx+TASKDATA.state], 1
214
        call    change_task
215
        jmp     .forever
216
@@:
217
        mov     edx, [esp+MUTEX_WAITER.list.next]
218
        mov     eax, [esp+MUTEX_WAITER.list.prev]
219
 
220
        mov     [eax+MUTEX_WAITER.list.next], edx
221
        mov     [edx+MUTEX_WAITER.list.prev], eax
2381 hidnplayr 222
        cmp     [ecx+MUTEX.lhead.next], ecx
2288 clevermous 223
        jne     @F
224
 
225
        mov     [ecx+MUTEX.count], 0
226
@@:
2381 hidnplayr 227
        add     esp, sizeof.MUTEX_WAITER
2288 clevermous 228
 
229
        popfd
230
.done:
231
        ret
232
 
233
;void  __fastcall mutex_unlock(struct mutex *lock)
234
 
235
align 4
236
mutex_unlock:
237
 
238
        pushfd
239
        cli
240
 
2381 hidnplayr 241
        mov     eax, [ecx+MUTEX.lhead.next]
2288 clevermous 242
        cmp     eax, ecx
243
        mov     [ecx+MUTEX.count], 1
244
        je      @F
245
 
246
        mov     eax, [eax+MUTEX_WAITER.task]
247
        mov     [eax+TASKDATA.state], 0
248
@@:
249
        popfd
250
        ret
251
 
252
 
253
purge MUTEX_WAITER
254