Rev 8869 | Rev 9590 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8869 | Rev 8874 | ||
---|---|---|---|
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2021. 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: 8869 $ |
8 | $Revision: 8874 $ |
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 | pushad |
18 | pushad |
19 | mov ax, app_data |
19 | mov ax, app_data |
20 | mov ds, ax |
20 | mov ds, ax |
21 | mov es, ax |
21 | mov es, ax |
22 | inc [timer_ticks] |
22 | inc [timer_ticks] |
23 | mov eax, [timer_ticks] |
23 | mov eax, [timer_ticks] |
24 | call playNote ; <<<--- Speaker driver |
24 | call playNote ; <<<--- Speaker driver |
25 | sub eax, [next_usage_update] |
25 | sub eax, [next_usage_update] |
26 | cmp eax, 100 |
26 | cmp eax, 100 |
27 | jb .nocounter |
27 | jb .nocounter |
28 | add [next_usage_update], 100 |
28 | add [next_usage_update], 100 |
29 | call updatecputimes |
29 | call updatecputimes |
30 | .nocounter: |
30 | .nocounter: |
31 | xor ecx, ecx ; send End Of Interrupt signal |
31 | xor ecx, ecx ; send End Of Interrupt signal |
32 | call irq_eoi |
32 | call irq_eoi |
33 | 33 | ||
34 | mov bl, SCHEDULE_ANY_PRIORITY |
34 | mov bl, SCHEDULE_ANY_PRIORITY |
35 | call find_next_task |
35 | call find_next_task |
36 | jz .return ; if there is only one running process |
36 | jz .return ; if there is only one running process |
37 | call do_change_task |
37 | call do_change_task |
38 | .return: |
38 | .return: |
39 | popad |
39 | popad |
40 | iretd |
40 | iretd |
41 | 41 | ||
42 | align 4 |
42 | align 4 |
43 | change_task: |
43 | change_task: |
44 | pushfd |
44 | pushfd |
45 | cli |
45 | cli |
46 | pushad |
46 | pushad |
47 | mov bl, SCHEDULE_ANY_PRIORITY |
47 | mov bl, SCHEDULE_ANY_PRIORITY |
48 | call find_next_task |
48 | call find_next_task |
49 | jz .return ; the same task -> skip switch |
49 | jz .return ; the same task -> skip switch |
50 | 50 | ||
51 | call do_change_task |
51 | call do_change_task |
52 | .return: |
52 | .return: |
53 | popad |
53 | popad |
54 | popfd |
54 | popfd |
55 | ret |
55 | ret |
56 | 56 | ||
57 | uglobal |
57 | uglobal |
58 | align 4 |
58 | align 4 |
59 | ; far_jump: |
59 | ; far_jump: |
60 | ; .offs dd ? |
60 | ; .offs dd ? |
61 | ; .sel dw ? |
61 | ; .sel dw ? |
62 | context_counter dd 0 ;noname & halyavin |
62 | context_counter dd 0 ;noname & halyavin |
63 | next_usage_update dd 0 |
63 | next_usage_update dd 0 |
64 | timer_ticks dd 0 |
64 | timer_ticks dd 0 |
65 | ; prev_slot dd ? |
65 | ; prev_slot dd ? |
66 | ; event_sched dd ? |
66 | ; event_sched dd ? |
67 | endg |
67 | endg |
68 | 68 | ||
69 | align 4 |
69 | align 4 |
70 | update_counters: |
70 | update_counters: |
71 | mov edi, [TASK_BASE] |
71 | mov edi, [TASK_BASE] |
72 | rdtsc |
72 | rdtsc |
73 | sub eax, [edi+TASKDATA.counter_add] ; time stamp counter add |
73 | sub eax, [edi+TASKDATA.counter_add] ; time stamp counter add |
74 | add [edi+TASKDATA.counter_sum], eax ; counter sum |
74 | add [edi+TASKDATA.counter_sum], eax ; counter sum |
75 | ret |
75 | ret |
76 | align 4 |
76 | align 4 |
77 | updatecputimes: |
77 | updatecputimes: |
78 | mov ecx, [thread_count] |
78 | mov ecx, [thread_count] |
79 | mov edi, TASK_DATA |
79 | mov edi, TASK_DATA |
80 | .newupdate: |
80 | .newupdate: |
81 | xor eax, eax |
81 | xor eax, eax |
82 | xchg eax, [edi+TASKDATA.counter_sum] |
82 | xchg eax, [edi+TASKDATA.counter_sum] |
83 | mov [edi+TASKDATA.cpu_usage], eax |
83 | mov [edi+TASKDATA.cpu_usage], eax |
84 | add edi, 0x20 |
84 | add edi, 0x20 |
85 | loop .newupdate |
85 | loop .newupdate |
86 | ret |
86 | ret |
87 | 87 | ||
88 | ;TODO: Надо бы убрать использование do_change_task из V86... |
88 | ;TODO: Надо бы убрать использование do_change_task из V86... |
89 | ; и после этого перенести обработку TASKDATA.counter_add/sum в do_change_task |
89 | ; и после этого перенести обработку TASKDATA.counter_add/sum в do_change_task |
90 | 90 | ||
91 | align 4 |
91 | align 4 |
92 | do_change_task: |
92 | do_change_task: |
93 | ;param: |
93 | ;param: |
94 | ; ebx = address of the APPDATA for incoming task (new) |
94 | ; ebx = address of the APPDATA for incoming task (new) |
95 | ;warning: |
95 | ;warning: |
96 | ; [current_slot_idx] and [TASK_BASE] must be changed before (e.g. in find_next_task) |
96 | ; [current_slot_idx] and [TASK_BASE] must be changed before (e.g. in find_next_task) |
97 | ; [current_slot] is the outcoming (old), and set here to a new value (ebx) |
97 | ; [current_slot] is the outcoming (old), and set here to a new value (ebx) |
98 | ;scratched: eax,ecx,esi |
98 | ;scratched: eax,ecx,esi |
99 | mov esi, ebx |
99 | mov esi, ebx |
100 | xchg esi, [current_slot] |
100 | xchg esi, [current_slot] |
101 | ; set new stack after saving old |
101 | ; set new stack after saving old |
102 | mov [esi+APPDATA.saved_esp], esp |
102 | mov [esi+APPDATA.saved_esp], esp |
103 | mov esp, [ebx+APPDATA.saved_esp] |
103 | mov esp, [ebx+APPDATA.saved_esp] |
104 | ; set new thread io-map |
104 | ; set new thread io-map |
105 | mov eax, [ebx+APPDATA.io_map] |
105 | mov eax, [ebx+APPDATA.io_map] |
106 | mov dword [page_tabs+((tss._io_map_0 and -4096) shr 10)], eax |
106 | mov dword [page_tabs+((tss._io_map_0 and -4096) shr 10)], eax |
107 | mov eax, [ebx+APPDATA.io_map+4] |
107 | mov eax, [ebx+APPDATA.io_map+4] |
108 | mov dword [page_tabs+((tss._io_map_1 and -4096) shr 10)], eax |
108 | mov dword [page_tabs+((tss._io_map_1 and -4096) shr 10)], eax |
109 | ; set new thread memory-map |
109 | ; set new thread memory-map |
110 | mov eax, [ebx+APPDATA.process] |
110 | mov eax, [ebx+APPDATA.process] |
111 | cmp eax, [current_process] |
111 | cmp eax, [current_process] |
112 | je @f |
112 | je @f |
113 | mov [current_process], eax |
113 | mov [current_process], eax |
114 | mov eax, [eax+PROC.pdt_0_phys] |
114 | mov eax, [eax+PROC.pdt_0_phys] |
115 | mov cr3, eax |
115 | mov cr3, eax |
116 | @@: |
116 | @@: |
117 | ; set tss.esp0 |
117 | ; set tss.esp0 |
118 | 118 | ||
119 | mov eax, [ebx+APPDATA.saved_esp0] |
119 | mov eax, [ebx+APPDATA.saved_esp0] |
120 | mov [tss._esp0], eax |
120 | mov [tss._esp0], eax |
121 | 121 | ||
122 | mov edx, [ebx+APPDATA.tls_base] |
122 | mov edx, [ebx+APPDATA.tls_base] |
123 | 123 | ||
124 | mov [tls_data_l+2], dx |
124 | mov [tls_data_l+2], dx |
125 | shr edx, 16 |
125 | shr edx, 16 |
126 | mov [tls_data_l+4], dl |
126 | mov [tls_data_l+4], dl |
127 | mov [tls_data_l+7], dh |
127 | mov [tls_data_l+7], dh |
128 | 128 | ||
129 | mov dx, app_tls |
129 | mov dx, app_tls |
130 | mov fs, dx |
130 | mov fs, dx |
131 | 131 | ||
132 | ; set gs selector unconditionally |
132 | ; set gs selector unconditionally |
133 | Mov ax, graph_data |
133 | Mov ax, graph_data |
134 | Mov gs, ax |
134 | Mov gs, ax |
135 | ; TS flag is not triggered by AVX* instructions, therefore |
135 | ; TS flag is not triggered by AVX* instructions, therefore |
136 | ; we have to xsave/xrstor SIMD registers each task change |
136 | ; we have to xsave/xrstor SIMD registers each task change |
137 | bt [cpu_caps+(CAPS_OSXSAVE/32)*4], CAPS_OSXSAVE mod 32 |
137 | bt [cpu_caps+(CAPS_OSXSAVE/32)*4], CAPS_OSXSAVE mod 32 |
138 | jnc .no_xsave |
138 | jnc .no_xsave |
139 | mov ecx, [esi+APPDATA.fpu_state] |
139 | mov ecx, [esi+APPDATA.fpu_state] |
140 | mov eax, [xsave_eax] |
140 | mov eax, [xsave_eax] |
141 | mov edx, [xsave_edx] |
141 | mov edx, [xsave_edx] |
142 | xsave [ecx] |
142 | xsave [ecx] |
143 | mov ecx, [current_slot_idx] |
143 | mov ecx, [current_slot_idx] |
144 | mov [fpu_owner], ecx |
144 | mov [fpu_owner], ecx |
145 | mov ecx, [current_slot] |
145 | mov ecx, [current_slot] |
146 | mov ecx, [ecx+APPDATA.fpu_state] |
146 | mov ecx, [ecx+APPDATA.fpu_state] |
147 | xrstor [ecx] |
147 | xrstor [ecx] |
148 | .no_xsave: |
148 | .no_xsave: |
149 | ; set CR0.TS |
149 | ; set CR0.TS |
150 | cmp bh, byte[fpu_owner] ;bh == incoming task (new) |
150 | cmp bh, byte[fpu_owner] ;bh == incoming task (new) |
151 | clts ;clear a task switch flag |
151 | clts ;clear a task switch flag |
152 | je @f |
152 | je @f |
153 | mov eax, cr0 ;and set it again if the owner |
153 | mov eax, cr0 ;and set it again if the owner |
154 | or eax, CR0_TS ;of a fpu has changed |
154 | or eax, CR0_TS ;of a fpu has changed |
155 | mov cr0, eax |
155 | mov cr0, eax |
156 | @@: ; set context_counter (only for user pleasure ???) |
156 | @@: ; set context_counter (only for user pleasure ???) |
157 | inc [context_counter] ;noname & halyavin |
157 | inc [context_counter] ;noname & halyavin |
158 | ; set debug-registers, if it's necessary |
158 | ; set debug-registers, if it's necessary |
159 | test byte[ebx+APPDATA.dbg_state], 1 |
159 | test byte[ebx+APPDATA.dbg_state], 1 |
160 | jz @f |
160 | jz @f |
161 | xor eax, eax |
161 | xor eax, eax |
162 | mov dr6, eax |
162 | mov dr6, eax |
163 | lea esi, [ebx+APPDATA.dbg_regs] |
163 | lea esi, [ebx+APPDATA.dbg_regs] |
164 | cld |
164 | cld |
165 | macro lodsReg [reg] { |
165 | macro lodsReg [reg] { |
166 | lodsd |
166 | lodsd |
167 | mov reg, eax |
167 | mov reg, eax |
168 | } lodsReg dr0, dr1, dr2, dr3, dr7 |
168 | } lodsReg dr0, dr1, dr2, dr3, dr7 |
169 | purge lodsReg |
169 | purge lodsReg |
170 | @@: |
170 | @@: |
171 | ret |
171 | ret |
172 | ;end. |
172 | ;end. |
173 | 173 | ||
174 | 174 | ||
175 | 175 | ||
176 | 176 | ||
177 | MAX_PRIORITY = 0 ; highest, used for kernel tasks |
177 | MAX_PRIORITY = 0 ; highest, used for kernel tasks |
178 | USER_PRIORITY = 1 ; default |
178 | USER_PRIORITY = 1 ; default |
179 | IDLE_PRIORITY = 2 ; lowest, only IDLE thread goes here |
179 | IDLE_PRIORITY = 2 ; lowest, only IDLE thread goes here |
180 | NR_SCHED_QUEUES = 3 ; MUST equal IDLE_PRIORYTY + 1 |
180 | NR_SCHED_QUEUES = 3 ; MUST equal IDLE_PRIORYTY + 1 |
181 | 181 | ||
182 | uglobal |
182 | uglobal |
183 | ; [scheduler_current + i*4] = zero if there are no threads with priority i, |
183 | ; [scheduler_current + i*4] = zero if there are no threads with priority i, |
184 | ; pointer to APPDATA of the current thread with priority i otherwise. |
184 | ; pointer to APPDATA of the current thread with priority i otherwise. |
185 | align 4 |
185 | align 4 |
186 | scheduler_current rd NR_SCHED_QUEUES |
186 | scheduler_current rd NR_SCHED_QUEUES |
187 | endg |
187 | endg |
188 | 188 | ||
189 | ; Add the given thread to the given priority list for the scheduler. |
189 | ; Add the given thread to the given priority list for the scheduler. |
190 | ; in: edx -> APPDATA, ecx = priority |
190 | ; in: edx -> APPDATA, ecx = priority |
191 | proc scheduler_add_thread |
191 | proc scheduler_add_thread |
192 | ; 1. Acquire the lock. |
192 | ; 1. Acquire the lock. |
193 | spin_lock_irqsave SchedulerLock |
193 | spin_lock_irqsave SchedulerLock |
194 | ; 2. Store the priority in APPDATA structure. |
194 | ; 2. Store the priority in APPDATA structure. |
195 | mov [edx+APPDATA.priority], ecx |
195 | mov [edx+APPDATA.priority], ecx |
196 | ; 3. There are two different cases: the given list is empty or not empty. |
196 | ; 3. There are two different cases: the given list is empty or not empty. |
197 | ; In first case, go to 6. Otherwise, advance to 4. |
197 | ; In first case, go to 6. Otherwise, advance to 4. |
198 | mov eax, [scheduler_current+ecx*4] |
198 | mov eax, [scheduler_current+ecx*4] |
199 | test eax, eax |
199 | test eax, eax |
200 | jz .new_list |
200 | jz .new_list |
201 | ; 4. Insert the new item immediately before the current item. |
201 | ; 4. Insert the new item immediately before the current item. |
202 | mov ecx, [eax+APPDATA.in_schedule.prev] |
202 | mov ecx, [eax+APPDATA.in_schedule.prev] |
203 | mov [edx+APPDATA.in_schedule.next], eax |
203 | mov [edx+APPDATA.in_schedule.next], eax |
204 | mov [edx+APPDATA.in_schedule.prev], ecx |
204 | mov [edx+APPDATA.in_schedule.prev], ecx |
205 | mov [eax+APPDATA.in_schedule.prev], edx |
205 | mov [eax+APPDATA.in_schedule.prev], edx |
206 | mov [ecx+APPDATA.in_schedule.next], edx |
206 | mov [ecx+APPDATA.in_schedule.next], edx |
207 | ; 5. Release the lock and return. |
207 | ; 5. Release the lock and return. |
208 | spin_unlock_irqrestore SchedulerLock |
208 | spin_unlock_irqrestore SchedulerLock |
209 | ret |
209 | ret |
210 | .new_list: |
210 | .new_list: |
211 | ; 6. Initialize the list with one item and make it the current item. |
211 | ; 6. Initialize the list with one item and make it the current item. |
212 | mov [edx+APPDATA.in_schedule.next], edx |
212 | mov [edx+APPDATA.in_schedule.next], edx |
213 | mov [edx+APPDATA.in_schedule.prev], edx |
213 | mov [edx+APPDATA.in_schedule.prev], edx |
214 | mov [scheduler_current+ecx*4], edx |
214 | mov [scheduler_current+ecx*4], edx |
215 | ; 7. Release the lock and return. |
215 | ; 7. Release the lock and return. |
216 | spin_unlock_irqrestore SchedulerLock |
216 | spin_unlock_irqrestore SchedulerLock |
217 | ret |
217 | ret |
218 | endp |
218 | endp |
219 | 219 | ||
220 | ; Remove the given thread from the corresponding priority list for the scheduler. |
220 | ; Remove the given thread from the corresponding priority list for the scheduler. |
221 | ; in: edx -> APPDATA |
221 | ; in: edx -> APPDATA |
222 | proc scheduler_remove_thread |
222 | proc scheduler_remove_thread |
223 | ; 1. Acquire the lock. |
223 | ; 1. Acquire the lock. |
224 | spin_lock_irqsave SchedulerLock |
224 | spin_lock_irqsave SchedulerLock |
225 | ; 2. Remove the item from the corresponding list. |
225 | ; 2. Remove the item from the corresponding list. |
226 | mov eax, [edx+APPDATA.in_schedule.next] |
226 | mov eax, [edx+APPDATA.in_schedule.next] |
227 | mov ecx, [edx+APPDATA.in_schedule.prev] |
227 | mov ecx, [edx+APPDATA.in_schedule.prev] |
228 | mov [eax+APPDATA.in_schedule.prev], ecx |
228 | mov [eax+APPDATA.in_schedule.prev], ecx |
229 | mov [ecx+APPDATA.in_schedule.next], eax |
229 | mov [ecx+APPDATA.in_schedule.next], eax |
230 | ; 3. If the given thread is the current item in the list, |
230 | ; 3. If the given thread is the current item in the list, |
231 | ; advance the current item. |
231 | ; advance the current item. |
232 | ; 3a. Check whether the given thread is the current item; |
232 | ; 3a. Check whether the given thread is the current item; |
233 | ; if no, skip the rest of this step. |
233 | ; if no, skip the rest of this step. |
234 | mov ecx, [edx+APPDATA.priority] |
234 | mov ecx, [edx+APPDATA.priority] |
235 | cmp [scheduler_current+ecx*4], edx |
235 | cmp [scheduler_current+ecx*4], edx |
236 | jnz .return |
236 | jnz .return |
237 | ; 3b. Set the current item to eax; step 2 has set eax = next item. |
237 | ; 3b. Set the current item to eax; step 2 has set eax = next item. |
238 | mov [scheduler_current+ecx*4], eax |
238 | mov [scheduler_current+ecx*4], eax |
239 | ; 3c. If there were only one item in the list, zero the current item. |
239 | ; 3c. If there were only one item in the list, zero the current item. |
240 | cmp eax, edx |
240 | cmp eax, edx |
241 | jnz .return |
241 | jnz .return |
242 | mov [scheduler_current+ecx*4], 0 |
242 | mov [scheduler_current+ecx*4], 0 |
243 | .return: |
243 | .return: |
244 | ; 4. Release the lock and return. |
244 | ; 4. Release the lock and return. |
245 | spin_unlock_irqrestore SchedulerLock |
245 | spin_unlock_irqrestore SchedulerLock |
246 | ret |
246 | ret |
247 | endp |
247 | endp |
248 | 248 | ||
249 | SCHEDULE_ANY_PRIORITY = 0 |
249 | SCHEDULE_ANY_PRIORITY = 0 |
250 | SCHEDULE_HIGHER_PRIORITY = 1 |
250 | SCHEDULE_HIGHER_PRIORITY = 1 |
251 | ;info: |
251 | ;info: |
252 | ; Find next task to execute |
252 | ; Find next task to execute |
253 | ;in: |
253 | ;in: |
254 | ; bl = SCHEDULE_ANY_PRIORITY: |
254 | ; bl = SCHEDULE_ANY_PRIORITY: |
255 | ; consider threads with any priority |
255 | ; consider threads with any priority |
256 | ; bl = SCHEDULE_HIGHER_PRIORITY: |
256 | ; bl = SCHEDULE_HIGHER_PRIORITY: |
257 | ; consider only threads with strictly higher priority than the current one, |
257 | ; consider only threads with strictly higher priority than the current one, |
258 | ; keep running the current thread if other ready threads have the same or lower priority |
258 | ; keep running the current thread if other ready threads have the same or lower priority |
259 | ;retval: |
259 | ;retval: |
260 | ; ebx = address of the APPDATA for the selected task (slot-base) |
260 | ; ebx = address of the APPDATA for the selected task (slot-base) |
261 | ; edi = address of the TASKDATA for the selected task |
261 | ; edi = address of the TASKDATA for the selected task |
262 | ; ZF = 1 if the task is the same |
262 | ; ZF = 1 if the task is the same |
263 | ;warning: |
263 | ;warning: |
264 | ; [current_slot_idx] = bh , [TASK_BASE] = edi -- as result |
264 | ; [current_slot_idx] = bh , [TASK_BASE] = edi -- as result |
265 | ; [current_slot] is not set to new value (ebx)!!! |
265 | ; [current_slot] is not set to new value (ebx)!!! |
266 | ;scratched: eax,ecx |
266 | ;scratched: eax,ecx |
267 | proc find_next_task |
267 | proc find_next_task |
268 | call update_counters |
268 | call update_counters |
269 | spin_lock_irqsave SchedulerLock |
269 | spin_lock_irqsave SchedulerLock |
270 | push NR_SCHED_QUEUES |
270 | push NR_SCHED_QUEUES |
271 | ; If bl == SCHEDULE_ANY_PRIORITY = 0, loop over all NR_SCHED lists. |
271 | ; If bl == SCHEDULE_ANY_PRIORITY = 0, loop over all NR_SCHED lists. |
272 | ; Otherwise, loop over first [APPDATA.priority] lists. |
272 | ; Otherwise, loop over first [APPDATA.priority] lists. |
273 | test bl, bl |
273 | test bl, bl |
274 | jz .start |
274 | jz .start |
275 | mov ebx, [current_slot] |
275 | mov ebx, [current_slot] |
276 | mov edi, [TASK_BASE] |
276 | mov edi, [TASK_BASE] |
277 | mov eax, [ebx+APPDATA.priority] |
277 | mov eax, [ebx+APPDATA.priority] |
278 | test eax, eax |
278 | test eax, eax |
279 | jz .unlock_found |
279 | jz .unlock_found |
280 | mov [esp], eax |
280 | mov [esp], eax |
281 | .start: |
281 | .start: |
282 | xor ecx, ecx |
282 | xor ecx, ecx |
283 | .priority_loop: |
283 | .priority_loop: |
284 | mov ebx, [scheduler_current+ecx*4] |
284 | mov ebx, [scheduler_current+ecx*4] |
285 | test ebx, ebx |
285 | test ebx, ebx |
286 | jz .priority_next |
286 | jz .priority_next |
287 | .task_loop: |
287 | .task_loop: |
288 | mov ebx, [ebx+APPDATA.in_schedule.next] |
288 | mov ebx, [ebx+APPDATA.in_schedule.next] |
289 | mov edi, ebx |
289 | mov edi, ebx |
290 | shr edi, 3 |
290 | shr edi, 3 |
291 | add edi, TASK_TABLE - (SLOT_BASE shr 3) |
291 | add edi, TASK_TABLE - (SLOT_BASE shr 3) |
292 | mov al, [edi+TASKDATA.state] |
292 | mov al, [edi+TASKDATA.state] |
293 | test al, al |
293 | test al, al |
294 | jz .task_found ; state == 0 |
294 | jz .task_found ; state == 0 |
295 | cmp al, 5 |
295 | cmp al, 5 |
296 | jne .task_next ; state == 1,2,3,4,9 |
296 | jne .task_next ; state == 1,2,3,4,9 |
297 | ; state == 5 |
297 | ; state == 5 |
298 | pushad ; more freedom for [APPDATA.wait_test] |
298 | pushad ; more freedom for [APPDATA.wait_test] |
299 | call [ebx+APPDATA.wait_test] |
299 | call [ebx+APPDATA.wait_test] |
300 | mov [esp+28], eax |
300 | mov [esp+28], eax |
301 | popad |
301 | popad |
302 | or eax, eax |
302 | or eax, eax |
303 | jnz @f |
303 | jnz @f |
304 | ; testing for timeout |
304 | ; testing for timeout |
305 | mov eax, [timer_ticks] |
305 | mov eax, [timer_ticks] |
306 | sub eax, [ebx+APPDATA.wait_begin] |
306 | sub eax, [ebx+APPDATA.wait_begin] |
307 | cmp eax, [ebx+APPDATA.wait_timeout] |
307 | cmp eax, [ebx+APPDATA.wait_timeout] |
308 | jb .task_next |
308 | jb .task_next |
309 | xor eax, eax |
309 | xor eax, eax |
310 | @@: |
310 | @@: |
311 | mov [ebx+APPDATA.wait_param], eax ; retval for wait |
311 | mov [ebx+APPDATA.wait_param], eax ; retval for wait |
312 | mov [edi+TASKDATA.state], 0 |
312 | mov [edi+TASKDATA.state], TSTATE_RUNNING |
313 | .task_found: |
313 | .task_found: |
314 | mov [scheduler_current+ecx*4], ebx |
314 | mov [scheduler_current+ecx*4], ebx |
315 | ; If we have selected a thread with higher priority |
315 | ; If we have selected a thread with higher priority |
316 | ; AND rescheduling is due to IRQ, |
316 | ; AND rescheduling is due to IRQ, |
317 | ; turn the current scheduler list one entry back, |
317 | ; turn the current scheduler list one entry back, |
318 | ; so the current thread will be next after high-priority thread is done. |
318 | ; so the current thread will be next after high-priority thread is done. |
319 | mov ecx, [esp] |
319 | mov ecx, [esp] |
320 | cmp ecx, NR_SCHED_QUEUES |
320 | cmp ecx, NR_SCHED_QUEUES |
321 | jz .unlock_found |
321 | jz .unlock_found |
322 | mov eax, [current_slot] |
322 | mov eax, [current_slot] |
323 | mov eax, [eax+APPDATA.in_schedule.prev] |
323 | mov eax, [eax+APPDATA.in_schedule.prev] |
324 | mov [scheduler_current+ecx*4], eax |
324 | mov [scheduler_current+ecx*4], eax |
325 | .unlock_found: |
325 | .unlock_found: |
326 | pop ecx |
326 | pop ecx |
327 | spin_unlock_irqrestore SchedulerLock |
327 | spin_unlock_irqrestore SchedulerLock |
328 | .found: |
328 | .found: |
329 | ; the line below assumes APPDATA is 256 bytes long and SLOT_BASE is |
329 | ; the line below assumes APPDATA is 256 bytes long and SLOT_BASE is |
330 | ; aligned on 0x10000 |
330 | ; aligned on 0x10000 |
331 | mov byte [current_slot_idx], bh |
331 | mov byte [current_slot_idx], bh |
332 | mov [TASK_BASE], edi |
332 | mov [TASK_BASE], edi |
333 | rdtsc ;call _rdtsc |
333 | rdtsc ;call _rdtsc |
334 | mov [edi+TASKDATA.counter_add], eax; for next using update_counters |
334 | mov [edi+TASKDATA.counter_add], eax; for next using update_counters |
335 | cmp ebx, [current_slot] |
335 | cmp ebx, [current_slot] |
336 | ret |
336 | ret |
337 | .task_next: |
337 | .task_next: |
338 | cmp ebx, [scheduler_current+ecx*4] |
338 | cmp ebx, [scheduler_current+ecx*4] |
339 | jnz .task_loop |
339 | jnz .task_loop |
340 | .priority_next: |
340 | .priority_next: |
341 | inc ecx |
341 | inc ecx |
342 | cmp ecx, [esp] |
342 | cmp ecx, [esp] |
343 | jb .priority_loop |
343 | jb .priority_loop |
344 | mov ebx, [current_slot] |
344 | mov ebx, [current_slot] |
345 | mov edi, [TASK_BASE] |
345 | mov edi, [TASK_BASE] |
346 | jmp .unlock_found |
346 | jmp .unlock_found |
347 | endp |
347 | endp |
348 | 348 | ||
349 | if 0 |
349 | if 0 |
350 | 350 | ||
351 | struc TIMER |
351 | struc TIMER |
352 | { |
352 | { |
353 | .next dd ? |
353 | .next dd ? |
354 | .exp_time dd ? |
354 | .exp_time dd ? |
355 | .func dd ? |
355 | .func dd ? |
356 | .arg dd ? |
356 | .arg dd ? |
357 | } |
357 | } |
358 | 358 | ||
359 | 359 | ||
360 | uglobal |
360 | uglobal |
361 | rdy_head rd 16 |
361 | rdy_head rd 16 |
362 | endg |
362 | endg |
363 | 363 | ||
364 | align 4 |
364 | align 4 |
365 | pick_task: |
365 | pick_task: |
366 | 366 | ||
367 | xor eax, eax |
367 | xor eax, eax |
368 | .pick: |
368 | .pick: |
369 | mov ebx, [rdy_head+eax*4] |
369 | mov ebx, [rdy_head+eax*4] |
370 | test ebx, ebx |
370 | test ebx, ebx |
371 | jz .next |
371 | jz .next |
372 | 372 | ||
373 | mov [next_task], ebx |
373 | mov [next_task], ebx |
374 | test [ebx+flags.billable] |
374 | test [ebx+flags.billable] |
375 | jz @F |
375 | jz @F |
376 | mov [bill_task], ebx |
376 | mov [bill_task], ebx |
377 | @@: |
377 | @@: |
378 | ret |
378 | ret |
379 | .next: |
379 | .next: |
380 | inc eax |
380 | inc eax |
381 | jmp .pick |
381 | jmp .pick |
382 | 382 | ||
383 | ; param |
383 | ; param |
384 | ; eax= task |
384 | ; eax= task |
385 | ; |
385 | ; |
386 | ; retval |
386 | ; retval |
387 | ; eax= task |
387 | ; eax= task |
388 | ; ebx= queue |
388 | ; ebx= queue |
389 | ; ecx= front if 1 or back if 0 |
389 | ; ecx= front if 1 or back if 0 |
390 | align 4 |
390 | align 4 |
391 | shed: |
391 | shed: |
392 | cmp [eax+.tics_left], 0;signed compare |
392 | cmp [eax+.tics_left], 0;signed compare |
393 | mov ebx, [eax+.priority] |
393 | mov ebx, [eax+.priority] |
394 | setg ecx |
394 | setg ecx |
395 | jg @F |
395 | jg @F |
396 | 396 | ||
397 | mov edx, [eax+.tics_quantum] |
397 | mov edx, [eax+.tics_quantum] |
398 | mov [eax+.ticks_left], edx |
398 | mov [eax+.ticks_left], edx |
399 | cmp ebx, (IDLE_PRIORITY-1) |
399 | cmp ebx, (IDLE_PRIORITY-1) |
400 | je @F |
400 | je @F |
401 | inc ebx |
401 | inc ebx |
402 | @@: |
402 | @@: |
403 | ret |
403 | ret |
404 | 404 | ||
405 | ; param |
405 | ; param |
406 | ; eax= task |
406 | ; eax= task |
407 | align 4 |
407 | align 4 |
408 | enqueue: |
408 | enqueue: |
409 | call shed;eax |
409 | call shed;eax |
410 | cmp [rdy_head+ebx*4], 0 |
410 | cmp [rdy_head+ebx*4], 0 |
411 | jnz @F |
411 | jnz @F |
412 | 412 | ||
413 | mov [rdy_head+ebx*4], eax |
413 | mov [rdy_head+ebx*4], eax |
414 | mov [rdy_tail+ebx*4], eax |
414 | mov [rdy_tail+ebx*4], eax |
415 | mov [eax+.next_ready], 0 |
415 | mov [eax+.next_ready], 0 |
416 | jmp .pick |
416 | jmp .pick |
417 | @@: |
417 | @@: |
418 | test ecx, ecx |
418 | test ecx, ecx |
419 | jz .back |
419 | jz .back |
420 | 420 | ||
421 | mov ecx, [rdy_head+ebx*4] |
421 | mov ecx, [rdy_head+ebx*4] |
422 | mov [eax+.next_ready], ecx |
422 | mov [eax+.next_ready], ecx |
423 | mov [rdy_head+ebx*4], eax |
423 | mov [rdy_head+ebx*4], eax |
424 | jmp .pick |
424 | jmp .pick |
425 | .back: |
425 | .back: |
426 | mov ecx, [rdy_tail+ebx*4] |
426 | mov ecx, [rdy_tail+ebx*4] |
427 | mov [ecx+.next_ready], eax |
427 | mov [ecx+.next_ready], eax |
428 | mov [rdy_tail+ebx*4], eax |
428 | mov [rdy_tail+ebx*4], eax |
429 | mov [eax+.next_ready], 0 |
429 | mov [eax+.next_ready], 0 |
430 | .pick: |
430 | .pick: |
431 | call pick_proc;select next task |
431 | call pick_proc;select next task |
432 | ret |
432 | ret |
433 | 433 | ||
434 | end if---><---><<---> |
434 | end if---><---><<---> |