From f059bc06d5cc4838ae7525e65fb1e0e039bbb502 Mon Sep 17 00:00:00 2001 From: CleverMouse Date: Mon, 27 May 2013 09:02:35 +0000 Subject: [PATCH] priorities in scheduler git-svn-id: svn://kolibrios.org@3534 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/blkdev/cd_drv.inc | 14 ++ kernel/trunk/blkdev/flp_drv.inc | 18 +++ kernel/trunk/bus/usb/init.inc | 1 - kernel/trunk/core/fpu.inc | 2 +- kernel/trunk/core/irq.inc | 1 - kernel/trunk/core/memory.inc | 5 - kernel/trunk/core/sched.inc | 197 ++++++++++++++++++--------- kernel/trunk/core/sys32.inc | 50 +++---- kernel/trunk/core/taskman.inc | 57 +++----- kernel/trunk/core/timers.inc | 25 ++++ kernel/trunk/data32.inc | 3 +- kernel/trunk/gui/event.inc | 2 +- kernel/trunk/gui/window.inc | 1 + kernel/trunk/hid/keyboard.inc | 9 +- kernel/trunk/hid/mousedrv.inc | 1 + kernel/trunk/kernel.asm | 227 +++++++++++++++++++------------- kernel/trunk/kernel32.inc | 26 ++-- kernel/trunk/network/stack.inc | 6 + kernel/trunk/network/udp.inc | 2 - 19 files changed, 398 insertions(+), 249 deletions(-) diff --git a/kernel/trunk/blkdev/cd_drv.inc b/kernel/trunk/blkdev/cd_drv.inc index 15f5de2486..b1359e07e4 100644 --- a/kernel/trunk/blkdev/cd_drv.inc +++ b/kernel/trunk/blkdev/cd_drv.inc @@ -710,6 +710,20 @@ EjectMedium: ;* ChannelNumber - íîìåð êàíàëà; * ;* DiskNumber - íîìåð äèñêà íà êàíàëå. * ;************************************************* +proc check_ATAPI_device_event_has_work? + mov eax, [timer_ticks] + sub eax, [timer_ATAPI_check] + cmp eax, 100 + jb .no +.yes: + xor eax, eax + inc eax + ret +.no: + xor eax, eax + ret +endp + align 4 check_ATAPI_device_event: pusha diff --git a/kernel/trunk/blkdev/flp_drv.inc b/kernel/trunk/blkdev/flp_drv.inc index 805663ce97..bfb69972f6 100644 --- a/kernel/trunk/blkdev/flp_drv.inc +++ b/kernel/trunk/blkdev/flp_drv.inc @@ -284,6 +284,24 @@ save_timer_fdd_motor: ;***************************************** ;* ÏÐÎÂÅÐÊÀ ÇÀÄÅÐÆÊÈ ÂÛÊËÞ×ÅÍÈß ÌÎÒÎÐÀ * ;***************************************** +proc check_fdd_motor_status_has_work? + cmp [flp_status], 0 + jnz .yes + cmp [fdd_motor_status], 0 + jz .no + mov eax, [timer_ticks] + sub eax, [timer_fdd_motor] + cmp eax, 500 + jb .no +.yes: + xor eax, eax + inc eax + ret +.no: + xor eax, eax + ret +endp + align 4 check_fdd_motor_status: cmp [fdd_motor_status], 0 diff --git a/kernel/trunk/bus/usb/init.inc b/kernel/trunk/bus/usb/init.inc index 20eb6d72e5..d18d936be4 100644 --- a/kernel/trunk/bus/usb/init.inc +++ b/kernel/trunk/bus/usb/init.inc @@ -163,7 +163,6 @@ proc usb_wakeup_if_needed test ebx, ebx jz usb_wakeup.nothing usb_wakeup: - mov [check_idle_semaphore], 5 ; we really, really need a normal scheduler xor edx, edx mov eax, [usb_event] mov ebx, [eax+EVENT.id] diff --git a/kernel/trunk/core/fpu.inc b/kernel/trunk/core/fpu.inc index 25ef7ef4c0..8ff107e2a3 100644 --- a/kernel/trunk/core/fpu.inc +++ b/kernel/trunk/core/fpu.inc @@ -179,5 +179,5 @@ except_7: ;#NM exception handler iret iglobal - fpu_owner dd 0 + fpu_owner dd 2 endg diff --git a/kernel/trunk/core/irq.inc b/kernel/trunk/core/irq.inc index 18fde5a5d5..d2197546b7 100644 --- a/kernel/trunk/core/irq.inc +++ b/kernel/trunk/core/irq.inc @@ -267,7 +267,6 @@ align 16 .fail: inc [irq_failed+ebp*4] .exit: - mov [check_idle_semaphore], 5 mov ecx, ebp call irq_eoi diff --git a/kernel/trunk/core/memory.inc b/kernel/trunk/core/memory.inc index 1b428d6799..171d09227e 100644 --- a/kernel/trunk/core/memory.inc +++ b/kernel/trunk/core/memory.inc @@ -1136,11 +1136,6 @@ proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword mov eax, [dst_slot] shl eax, 8 or [eax+SLOT_BASE+0xA8], dword 0x40 - cmp dword [check_idle_semaphore], 20 - jge .ipc_no_cis - - mov dword [check_idle_semaphore], 5 -.ipc_no_cis: push 0 jmp .ret .no_pid: diff --git a/kernel/trunk/core/sched.inc b/kernel/trunk/core/sched.inc index 7907ca8940..02d6e98baf 100644 --- a/kernel/trunk/core/sched.inc +++ b/kernel/trunk/core/sched.inc @@ -89,9 +89,6 @@ update_counters: ret align 4 updatecputimes: - xor eax, eax - xchg eax, [idleuse] - mov [idleusesec], eax mov ecx, [TASK_COUNT] mov edi, TASK_DATA .newupdate: @@ -102,56 +99,6 @@ updatecputimes: loop .newupdate ret -align 4 -find_next_task: -;info: -; Find next task to execute -;retval: -; ebx = address of the APPDATA for the selected task (slot-base) -; esi = previous slot-base ([current_slot] at the begin) -; edi = address of the TASKDATA for the selected task -; ZF = 1 if the task is the same -;warning: -; [CURRENT_TASK] = bh , [TASK_BASE] = edi -- as result -; [current_slot] is not set to new value (ebx)!!! -;scratched: eax,ecx - call update_counters ; edi := [TASK_BASE] - Mov esi, ebx, [current_slot] - .loop: - cmp bh, [TASK_COUNT] - jb @f - xor bh, bh - mov edi, CURRENT_TASK - @@: - inc bh ; ebx += APPDATA.size - add edi, 0x20; edi += TASKDATA.size - mov al, [edi+TASKDATA.state] - test al, al - jz .found ; state == 0 - cmp al, 5 - jne .loop ; state == 1,2,3,4,9 - ; state == 5 - pushad ; more freedom for [APPDATA.wait_test] - call [ebx+APPDATA.wait_test] - mov [esp+28], eax - popad - or eax, eax - jnz @f - ; testing for timeout - mov ecx, [timer_ticks] - sub ecx, [ebx+APPDATA.wait_begin] - cmp ecx, [ebx+APPDATA.wait_timeout] - jb .loop - @@: - mov [ebx+APPDATA.wait_param], eax ; retval for wait - mov [edi+TASKDATA.state], 0 - .found: - mov [CURRENT_TASK], bh - mov [TASK_BASE], edi - rdtsc ;call _rdtsc - mov [edi+TASKDATA.counter_add], eax; for next using update_counters - cmp ebx, esi ;esi - previous slot-base - ret ;TODO: Íàäî áû óáðàòü èñïîëüçîâàíèå do_change_task èç V86... ; è ïîñëå ýòîãî ïåðåíåñòè îáðàáîòêó TASKDATA.counter_add/sum â do_change_task @@ -305,6 +252,143 @@ mutex_unlock: purge MUTEX_WAITER +MAX_PRIORITY = 0 ; highest, used for kernel tasks +USER_PRIORITY = 1 ; default +IDLE_PRIORITY = 2 ; lowest, only IDLE thread goes here +NR_SCHED_QUEUES = 3 ; MUST equal IDLE_PRIORYTY + 1 + +uglobal +; [scheduler_current + i*4] = zero if there are no threads with priority i, +; pointer to APPDATA of the current thread with priority i otherwise. +align 4 +scheduler_current rd NR_SCHED_QUEUES +endg + +; Add the given thread to the given priority list for the scheduler. +; in: edx -> APPDATA, ecx = priority +proc scheduler_add_thread +; 1. Acquire the lock. + spin_lock_irqsave SchedulerLock +; 2. Store the priority in APPDATA structure. + mov [edx+APPDATA.priority], ecx +; 3. There are two different cases: the given list is empty or not empty. +; In first case, go to 6. Otherwise, advance to 4. + mov eax, [scheduler_current+ecx*4] + test eax, eax + jz .new_list +; 4. Insert the new item immediately before the current item. + mov ecx, [eax+APPDATA.in_schedule.prev] + mov [edx+APPDATA.in_schedule.next], eax + mov [edx+APPDATA.in_schedule.prev], ecx + mov [eax+APPDATA.in_schedule.prev], edx + mov [ecx+APPDATA.in_schedule.next], edx +; 5. Release the lock and return. + spin_unlock_irqrestore SchedulerLock + ret +.new_list: +; 6. Initialize the list with one item and make it the current item. + mov [edx+APPDATA.in_schedule.next], edx + mov [edx+APPDATA.in_schedule.prev], edx + mov [scheduler_current+ecx*4], edx +; 7. Release the lock and return. + spin_unlock_irqrestore SchedulerLock + ret +endp + +; Remove the given thread from the corresponding priority list for the scheduler. +; in: edx -> APPDATA +proc scheduler_remove_thread +; 1. Acquire the lock. + spin_lock_irqsave SchedulerLock +; 2. Remove the item from the corresponding list. + mov eax, [edx+APPDATA.in_schedule.next] + mov ecx, [edx+APPDATA.in_schedule.prev] + mov [eax+APPDATA.in_schedule.prev], ecx + mov [ecx+APPDATA.in_schedule.next], eax +; 3. If the given thread is the current item in the list, +; advance the current item. +; 3a. Check whether the given thread is the current item; +; if no, skip the rest of this step. + mov ecx, [edx+APPDATA.priority] + cmp [scheduler_current+ecx*4], edx + jnz .return +; 3b. Set the current item to eax; step 2 has set eax = next item. + mov [scheduler_current+ecx*4], eax +; 3c. If there were only one item in the list, zero the current item. + cmp eax, edx + jnz .return + mov [scheduler_current+ecx*4], 0 +.return: +; 4. Release the lock and return. + spin_unlock_irqrestore SchedulerLock + ret +endp + +;info: +; Find next task to execute +;retval: +; ebx = address of the APPDATA for the selected task (slot-base) +; edi = address of the TASKDATA for the selected task +; ZF = 1 if the task is the same +;warning: +; [CURRENT_TASK] = bh , [TASK_BASE] = edi -- as result +; [current_slot] is not set to new value (ebx)!!! +;scratched: eax,ecx +proc find_next_task + call update_counters + spin_lock_irqsave SchedulerLock + xor ecx, ecx +.priority_loop: + mov ebx, [scheduler_current+ecx*4] + test ebx, ebx + jz .priority_next +.task_loop: + mov ebx, [ebx+APPDATA.in_schedule.next] + mov edi, ebx + shr edi, 3 + add edi, CURRENT_TASK - (SLOT_BASE shr 3) + mov al, [edi+TASKDATA.state] + test al, al + jz .task_found ; state == 0 + cmp al, 5 + jne .task_next ; state == 1,2,3,4,9 + ; state == 5 + pushad ; more freedom for [APPDATA.wait_test] + call [ebx+APPDATA.wait_test] + mov [esp+28], eax + popad + or eax, eax + jnz @f + ; testing for timeout + mov eax, [timer_ticks] + sub eax, [ebx+APPDATA.wait_begin] + cmp eax, [ebx+APPDATA.wait_timeout] + jb .task_next + xor eax, eax +@@: + mov [ebx+APPDATA.wait_param], eax ; retval for wait + mov [edi+TASKDATA.state], 0 +.task_found: + mov [scheduler_current+ecx*4], ebx + spin_unlock_irqrestore SchedulerLock +.found: + mov [CURRENT_TASK], bh + mov [TASK_BASE], edi + rdtsc ;call _rdtsc + mov [edi+TASKDATA.counter_add], eax; for next using update_counters + cmp ebx, [current_slot] + ret +.task_next: + cmp ebx, [scheduler_current+ecx*4] + jnz .task_loop +.priority_next: + inc ecx + cmp ecx, NR_SCHED_QUEUES + jb .priority_loop + hlt + jmp $-1 +endp + if 0 struc TIMER @@ -316,13 +400,6 @@ struc TIMER } -MAX_PROIRITY 0 ; highest, used for kernel tasks -MAX_USER_PRIORITY 0 ; highest priority for user processes -USER_PRIORITY 7 ; default (should correspond to nice 0) -MIN_USER_PRIORITY 14 ; minimum priority for user processes -IDLE_PRIORITY 15 ; lowest, only IDLE process goes here -NR_SCHED_QUEUES 16 ; MUST equal IDLE_PRIORYTY + 1 - uglobal rdy_head rd 16 endg diff --git a/kernel/trunk/core/sys32.inc b/kernel/trunk/core/sys32.inc index ed0b61fce3..94b2a465b0 100644 --- a/kernel/trunk/core/sys32.inc +++ b/kernel/trunk/core/sys32.inc @@ -159,6 +159,7 @@ exc_c: ; call show_error_parameters ;; only ONE using, inline ??? ;mov edx, [TASK_BASE] mov [edx + TASKDATA.state], byte 4 ; terminate + call wakeup_osloop jmp change_task ; stack - here it does not matter at all, SEE: core/shed.inc .debug: ; we are debugged process, notify debugger and suspend ourself @@ -261,36 +262,31 @@ show_error_parameters: align 4 -set_application_table_status: - push eax +lock_application_table: + push eax ecx edx + mov ecx, application_table_mutex + call mutex_lock mov eax, [CURRENT_TASK] shl eax, 5 add eax, CURRENT_TASK+TASKDATA.pid mov eax, [eax] - mov [application_table_status], eax + mov [application_table_owner], eax - pop eax + pop edx ecx eax ret align 4 -clear_application_table_status: - push eax +unlock_application_table: + push eax ecx edx - mov eax, [CURRENT_TASK] - shl eax, 5 - add eax, CURRENT_TASK+TASKDATA.pid - mov eax, [eax] + mov [application_table_owner], 0 + mov ecx, application_table_mutex + call mutex_unlock - cmp eax, [application_table_status] - jne apptsl1 - xor eax, eax - mov [application_table_status], eax - apptsl1: - - pop eax + pop edx ecx eax ret @@ -338,17 +334,11 @@ terminate: ; terminate application mov [CURRENT_TASK+esi+TASKDATA.state], 9 ret @@: + lea edx, [SLOT_BASE+esi] + call scheduler_remove_thread ;mov esi,process_terminating ;call sys_msg_board_str -@@: - cli - cmp [application_table_status], 0 - je term9 - sti - call change_task - jmp @b -term9: - call set_application_table_status + call lock_application_table ; if the process is in V86 mode... mov eax, [.slot] @@ -391,11 +381,11 @@ term9: stdcall destroy_app_space, [SLOT_BASE+eax+APPDATA.dir_table], [SLOT_BASE+eax+APPDATA.dlls_list_ptr] mov esi, [.slot] - cmp [fpu_owner], esi ; if user fpu last -> fpu user = 1 + cmp [fpu_owner], esi ; if user fpu last -> fpu user = 2 jne @F - mov [fpu_owner], 1 - mov eax, [256+SLOT_BASE+APPDATA.fpu_state] + mov [fpu_owner], 2 + mov eax, [256*2+SLOT_BASE+APPDATA.fpu_state] clts bt [cpu_caps], CAPS_SSE jnc .no_SSE @@ -688,7 +678,7 @@ term9: mov [MOUSE_BACKGROUND], byte 0; no mouse background mov [DONT_DRAW_MOUSE], byte 0; draw mouse - and [application_table_status], 0 + call unlock_application_table ;mov esi,process_terminated ;call sys_msg_board_str add esp, 4 diff --git a/kernel/trunk/core/taskman.inc b/kernel/trunk/core/taskman.inc index 4ade26374a..48160baaf5 100644 --- a/kernel/trunk/core/taskman.inc +++ b/kernel/trunk/core/taskman.inc @@ -90,7 +90,7 @@ proc fs_execute stdcall set_cursor, [def_cursor_clock] mov [handle], eax mov [redrawmouse_unconditional], 1 - call __sys_draw_pointer + call wakeup_osloop popad @@: mov [flags], edx @@ -152,19 +152,7 @@ proc fs_execute test eax, eax jz .err_hdr -.wait_lock: - cmp [application_table_status], 0 - je .get_lock - call change_task - jmp .wait_lock - -.get_lock: - mov eax, 1 - xchg eax, [application_table_status] - test eax, eax - jnz .wait_lock - - call set_application_table_status + call lock_application_table call get_new_process_place test eax, eax @@ -246,9 +234,8 @@ end if mov eax, [save_cr3] call set_cr3 - xor ebx, ebx - mov [application_table_status], ebx;unlock application_table_status mutex mov eax, [process_number];set result + call unlock_application_table jmp .final @@ -259,8 +246,7 @@ end if .err_hdr: stdcall kernel_free, [file_base] .err_file: - xor eax, eax - mov [application_table_status], eax + call unlock_application_table mov eax, esi .final: cmp [SCR_MODE], word 0x13 @@ -268,7 +254,7 @@ end if pushad stdcall set_cursor, [handle] mov [redrawmouse_unconditional], 1 - call __sys_draw_pointer + call wakeup_osloop popad @@: ret @@ -898,19 +884,7 @@ proc new_sys_threads mov [app_path], eax ;mov esi,new_process_loading ;call sys_msg_board_str -.wait_lock: - cmp [application_table_status], 0 - je .get_lock - call change_task - jmp .wait_lock - -.get_lock: - mov eax, 1 - xchg eax, [application_table_status] - test eax, eax - jnz .wait_lock - - call set_application_table_status + call lock_application_table call get_new_process_place test eax, eax @@ -967,14 +941,13 @@ proc new_sys_threads ;mov esi,new_process_running ;call sys_msg_board_str ;output information about succefull startup - xor eax, eax - mov [application_table_status], eax ;unlock application_table_status mutex mov eax, [process_number] ;set result + call unlock_application_table ret .failed: xor eax, eax .failed1: - mov [application_table_status], eax + call unlock_application_table dec eax ;-1 ret endp @@ -1148,6 +1121,7 @@ proc set_app_params stdcall,slot:dword, params:dword,\ mov eax, [esi+0x08] ;app_eip mov [ebx+REG_EIP], eax;app_entry mov [ebx+REG_CS], dword app_code + mov ecx, USER_PRIORITY mov eax, [CURRENT_TASK] shl eax, 8 ; created by kernel? cmp [SLOT_BASE+eax+APPDATA.dir_table], sys_pgdir - OS_BASE @@ -1155,6 +1129,7 @@ proc set_app_params stdcall,slot:dword, params:dword,\ cmp [app_path], 0 ; it is a thread? jnz @f mov [ebx+REG_CS], dword os_code ; kernel thread + mov ecx, MAX_PRIORITY @@: mov [ebx+REG_EFLAGS], dword EFL_IOPL1+EFL_IF @@ -1162,20 +1137,22 @@ proc set_app_params stdcall,slot:dword, params:dword,\ mov [ebx+REG_APP_ESP], eax;app_stack mov [ebx+REG_SS], dword app_data - lea ecx, [ebx+REG_RET] + lea edx, [ebx+REG_RET] mov ebx, [slot] shl ebx, 5 - mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], ecx + mov [ebx*8+SLOT_BASE+APPDATA.saved_esp], edx - xor ecx, ecx; process state - running + xor edx, edx; process state - running ; set if debuggee test byte [flags], 1 jz .no_debug - inc ecx ; process state - suspended + inc edx ; process state - suspended mov eax, [CURRENT_TASK] mov [SLOT_BASE+ebx*8+APPDATA.debugger_slot], eax .no_debug: - mov [CURRENT_TASK+ebx+TASKDATA.state], cl + mov [CURRENT_TASK+ebx+TASKDATA.state], dl + lea edx, [SLOT_BASE+ebx*8] + call scheduler_add_thread ;mov esi,new_process_running ;call sys_msg_board_str ;output information about succefull startup ret diff --git a/kernel/trunk/core/timers.inc b/kernel/trunk/core/timers.inc index 45315f7155..ecf62e1ddd 100644 --- a/kernel/trunk/core/timers.inc +++ b/kernel/trunk/core/timers.inc @@ -203,3 +203,28 @@ check_timers: call unlock_timer_list ; 4. Return. ret + +; This is a simplified version of check_timers that does not call anything, +; just checks whether check_timers should do something. +proc check_timers_has_work? + pushf + cli + mov eax, [timer_list+TIMER.Next] +.loop: + cmp eax, timer_list + jz .done_nowork + mov edx, [timer_ticks] + sub edx, [eax+TIMER.Time] + jns .done_haswork + mov eax, [eax+TIMER.Next] + jmp .loop +.done_nowork: + popf + xor eax, eax + ret +.done_haswork: + popf + xor eax, eax + inc eax + ret +endp diff --git a/kernel/trunk/data32.inc b/kernel/trunk/data32.inc index 670741c6fd..661bbd7af0 100644 --- a/kernel/trunk/data32.inc +++ b/kernel/trunk/data32.inc @@ -397,7 +397,8 @@ current_slot rd 1 ; status hd1_status rd 1 ; 0 - free : other - pid -application_table_status rd 1 ; 0 - free : other - pid +application_table_owner rd 1 ; 0 - free : other - pid +application_table_mutex MUTEX ; device addresses mididp rd 1 diff --git a/kernel/trunk/gui/event.inc b/kernel/trunk/gui/event.inc index f25e8387aa..3ae3a60bf2 100644 --- a/kernel/trunk/gui/event.inc +++ b/kernel/trunk/gui/event.inc @@ -165,7 +165,6 @@ raise_event: ;; EXPORT use ; esi - event data (=0 => skip) ;scratched: ebx,ecx,esi,edi call NotDummyTest ; not returned for fail !!! - mov [check_idle_semaphore], 5 or esi, esi jz @f lea edi, [ebx+EVENT.code] @@ -590,6 +589,7 @@ align 4 cmp edx, 0xFFFF ;-ID for Minimize-Button of Form jne .result mov [window_minimize], 1 + call wakeup_osloop dec byte[BTN_COUNT] jmp .loop ;-------------------------------------- diff --git a/kernel/trunk/gui/window.inc b/kernel/trunk/gui/window.inc index 4137c05c5c..d37347df33 100644 --- a/kernel/trunk/gui/window.inc +++ b/kernel/trunk/gui/window.inc @@ -1629,6 +1629,7 @@ window._.sys_set_window: ;///////////////////////////////////////////////////// ; Otherwise the user can see cursor specified by f.37.5 from another window. ; He will be really unhappy! He is terrible in rage - usually he throws stones! mov [redrawmouse_unconditional], 1 + call wakeup_osloop ; NOTE: commented out since doesn't provide necessary functionality ; anyway, to be reworked ; mov eax, [timer_ticks] ; [0xfdf0] diff --git a/kernel/trunk/hid/keyboard.inc b/kernel/trunk/hid/keyboard.inc index 078a5c7d9a..87b2a33454 100644 --- a/kernel/trunk/hid/keyboard.inc +++ b/kernel/trunk/hid/keyboard.inc @@ -339,6 +339,7 @@ send_scancode: jne .noctrlaltdel mov [ctrl_alt_del], 1 + call wakeup_osloop .noctrlaltdel: test dl, VKEY_CONTROL ; ctrl on ? jz @f @@ -490,7 +491,6 @@ send_scancode: mov [KEY_COUNT], al mov [KEY_COUNT+eax], bl .exit.irq1: - mov [check_idle_semaphore], 5 ret ;--------------------------------------------------------------------- set_lights: @@ -526,9 +526,14 @@ ps2_set_lights: ret 8 ;// mike.dld ] -check_lights_state: +proc check_lights_state_has_work? mov al, [kb_lights] cmp al, [old_kb_lights] + ret +endp + +check_lights_state: + call check_lights_state_has_work? jz .nothing mov [old_kb_lights], al call set_lights diff --git a/kernel/trunk/hid/mousedrv.inc b/kernel/trunk/hid/mousedrv.inc index 8b8588c7ab..3a45ec1ad5 100644 --- a/kernel/trunk/hid/mousedrv.inc +++ b/kernel/trunk/hid/mousedrv.inc @@ -531,6 +531,7 @@ align 4 mov [mouse_active], 1 mov eax, [timer_ticks] mov [mouse_timer_ticks], eax + call wakeup_osloop ret endp ;----------------------------------------------------------------------------- diff --git a/kernel/trunk/kernel.asm b/kernel/trunk/kernel.asm index c1e331696e..e8bbae0141 100644 --- a/kernel/trunk/kernel.asm +++ b/kernel/trunk/kernel.asm @@ -348,6 +348,9 @@ high_code: mov ecx, unpack_mutex call mutex_init + mov ecx, application_table_mutex + call mutex_init + ; SAVE REAL MODE VARIABLES mov ax, [BOOT_VAR + BOOT_IDE_BASE_ADDR] mov [IDEContrRegsBaseAddr], ax @@ -523,7 +526,7 @@ no_mode_0x12: ;lidt [idtreg] call init_kernel_heap - stdcall kernel_alloc, RING0_STACK_SIZE+512 + stdcall kernel_alloc, (RING0_STACK_SIZE+512) * 2 mov [os_stack_seg], eax lea esp, [eax+RING0_STACK_SIZE] @@ -603,10 +606,6 @@ no_mode_0x12: xor eax, eax inc eax - mov [CURRENT_TASK], eax ;dword 1 - mov [TASK_COUNT], eax ;dword 1 - mov [TASK_BASE], dword TASK_DATA - mov [current_slot], SLOT_BASE+256 ; set background @@ -621,48 +620,28 @@ no_mode_0x12: mov esi, boot_setostask call boot_log - xor eax, eax - mov dword [SLOT_BASE+APPDATA.fpu_state], fpu_data - mov dword [SLOT_BASE+APPDATA.exc_handler], eax - mov dword [SLOT_BASE+APPDATA.except_mask], eax + mov edx, SLOT_BASE+256 + mov ebx, [os_stack_seg] + add ebx, 0x2000 + call setup_os_slot + mov dword [edx], 'IDLE' + sub [edx+APPDATA.saved_esp], 4 + mov eax, [edx+APPDATA.saved_esp] + mov dword [eax], idle_thread + mov ecx, IDLE_PRIORITY + call scheduler_add_thread - ; name for OS/IDLE process + mov edx, SLOT_BASE+256*2 + mov ebx, [os_stack_seg] + call setup_os_slot + mov dword [edx], 'OS' + xor ecx, ecx + call scheduler_add_thread - mov dword [SLOT_BASE+256+APPDATA.app_name], dword 'OS/I' - mov dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE ' - mov edi, [os_stack_seg] - mov dword [SLOT_BASE+256+APPDATA.pl0_stack], edi - add edi, 0x2000-512 - mov dword [SLOT_BASE+256+APPDATA.fpu_state], edi - mov dword [SLOT_BASE+256+APPDATA.saved_esp0], edi; just for case - mov dword [SLOT_BASE+256+APPDATA.terminate_protection], 80000001h - - mov esi, fpu_data - mov ecx, 512/4 - cld - rep movsd - - mov dword [SLOT_BASE+256+APPDATA.exc_handler], eax - mov dword [SLOT_BASE+256+APPDATA.except_mask], eax - - mov ebx, SLOT_BASE+256+APP_OBJ_OFFSET - mov dword [SLOT_BASE+256+APPDATA.fd_obj], ebx - mov dword [SLOT_BASE+256+APPDATA.bk_obj], ebx - - mov dword [SLOT_BASE+256+APPDATA.cur_dir], sysdir_path - mov dword [SLOT_BASE+256+APPDATA.tls_base], eax - - ; task list - mov dword [TASK_DATA+TASKDATA.mem_start], eax; process base address - inc eax - mov dword [CURRENT_TASK], eax - mov dword [TASK_COUNT], eax - mov [current_slot], SLOT_BASE+256 - mov [TASK_BASE], dword TASK_DATA - mov byte[TASK_DATA+TASKDATA.wnd_number], al ; on screen number - mov dword [TASK_DATA+TASKDATA.pid], eax ; process id number - - mov [SLOT_BASE + 256 + APPDATA.dir_table], sys_pgdir - OS_BASE + mov dword [CURRENT_TASK], 2 + mov dword [TASK_COUNT], 2 + mov dword [current_slot], SLOT_BASE + 256*2 + mov dword [TASK_BASE], CURRENT_TASK + 32*2 stdcall kernel_alloc, 0x10000/8 mov edi, eax @@ -828,8 +807,8 @@ end if call init_display mov eax, [def_cursor] - mov [SLOT_BASE+APPDATA.cursor], eax mov [SLOT_BASE+APPDATA.cursor+256], eax + mov [SLOT_BASE+APPDATA.cursor+256*2], eax ; PRINT CPU FREQUENCY @@ -947,10 +926,6 @@ first_app_found: cli - ;mov [TASK_COUNT],dword 2 - push 1 - pop dword [CURRENT_TASK] ; set OS task fisrt - ; SET KEYBOARD PARAMETERS mov al, 0xf6 ; reset keyboard, scan enabled call kb_write @@ -1068,7 +1043,7 @@ end if mov [timer_ticks_enable], 1 ; for cd driver sti - call change_task +; call change_task jmp osloop @@ -1097,6 +1072,43 @@ boot_log: ret +; in: edx -> APPDATA for OS/IDLE slot +; in: ebx = stack base +proc setup_os_slot + xor eax, eax + mov ecx, 256/4 + mov edi, edx + rep stosd + + mov dword [edx+APPDATA.pl0_stack], ebx + lea edi, [ebx+0x2000-512] + mov dword [edx+APPDATA.fpu_state], edi + mov dword [edx+APPDATA.saved_esp0], edi + mov dword [edx+APPDATA.saved_esp], edi + mov dword [edx+APPDATA.terminate_protection], 1 ; make unkillable + + mov esi, fpu_data + mov ecx, 512/4 + cld + rep movsd + + lea eax, [edx+APP_OBJ_OFFSET] + mov dword [edx+APPDATA.fd_obj], eax + mov dword [edx+APPDATA.bk_obj], eax + + mov dword [edx+APPDATA.cur_dir], sysdir_path + + mov [edx + APPDATA.dir_table], sys_pgdir - OS_BASE + + mov eax, edx + shr eax, 3 + add eax, CURRENT_TASK - (SLOT_BASE shr 3) + mov [eax+TASKDATA.wnd_number], dh + mov byte [eax+TASKDATA.pid], dh + + ret +endp + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; ; MAIN OS LOOP START ; @@ -1104,14 +1116,21 @@ boot_log: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; align 32 osloop: + mov edx, osloop_has_work? + xor ecx, ecx + call Wait_events + xor eax, eax + xchg eax, [osloop_nonperiodic_work] + test eax, eax + jz .no_periodic ; call [draw_pointer] call __sys_draw_pointer call window_check_events call mouse_check_events call checkmisc call checkVga_N13 +.no_periodic: call stack_handler - call checkidle call check_fdd_motor_status call check_ATAPI_device_event call check_lights_state @@ -1122,37 +1141,45 @@ osloop: ; MAIN OS LOOP END ; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -align 4 -checkidle: - pushad - call change_task - jmp idle_loop_entry - idle_loop: - cmp eax, [idlemem] ; eax == [timer_ticks] - jne idle_exit - rdtsc ;call _rdtsc - mov ecx, eax - hlt - rdtsc ;call _rdtsc - sub eax, ecx - add [idleuse], eax - idle_loop_entry: - mov eax, [timer_ticks]; eax = [timer_ticks] - cmp [check_idle_semaphore], 0 - je idle_loop - dec [check_idle_semaphore] - idle_exit: - mov [idlemem], eax ; eax == [timer_ticks] - popad +proc osloop_has_work? + cmp [osloop_nonperiodic_work], 0 + jnz .yes + call stack_handler_has_work? + jnz .yes + call check_fdd_motor_status_has_work? + jnz .yes + call check_ATAPI_device_event_has_work? + jnz .yes + call check_lights_state_has_work? + jnz .yes + call check_timers_has_work? + jnz .yes +.no: + xor eax, eax ret +.yes: + xor eax, eax + inc eax + ret +endp + +proc wakeup_osloop + mov [osloop_nonperiodic_work], 1 + ret +endp uglobal - idlemem dd 0x0 - idleuse dd 0x0 - idleusesec dd 0x0 - check_idle_semaphore dd 0x0 +align 4 +osloop_nonperiodic_work dd ? endg +align 4 +idle_thread: + sti +idle_loop: + hlt + jmp idle_loop + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -1200,7 +1227,7 @@ reserve_irqs_ports: iglobal - process_number dd 0x1 + process_number dd 0x2 endg set_variables: @@ -1217,6 +1244,7 @@ set_variables: mov ax, [BOOT_VAR+BOOT_X_RES] shr ax, 1 mov [MOUSE_X], eax + call wakeup_osloop xor eax, eax mov [BTN_ADDR], dword BUTTON_INFO ; address of button list @@ -2007,6 +2035,7 @@ sys_end: mov eax, [TASK_BASE] mov [eax+TASKDATA.state], 3; terminate this program + call wakeup_osloop waitterm: ; wait here for termination mov ebx, 100 @@ -2040,6 +2069,7 @@ restore_default_cursor_before_killing: mov [current_cursor], esi @@: mov [redrawmouse_unconditional], 1 + call wakeup_osloop popfd ret ;------------------------------------------------------------------------------ @@ -2092,6 +2122,7 @@ sysfn_shutdown: ; 18.9 = system shutdown mov eax, [TASK_COUNT] mov [SYS_SHUTDOWN], al mov [shutdown_processes], eax + call wakeup_osloop and dword [esp+32], 0 exit_for_anyone: ret @@ -2137,11 +2168,12 @@ sysfn_terminate: ; 18.2 = TERMINATE ;-------------------------------------- ;call MEM_Heap_Lock ;guarantee that process isn't working with heap mov [ecx], byte 3; clear possible i40's + call wakeup_osloop ;call MEM_Heap_UnLock - cmp edx, [application_table_status]; clear app table stat + cmp edx, [application_table_owner]; clear app table stat jne noatsc - and [application_table_status], 0 + call unlock_application_table noatsc: noprocessterminate: add esp, 4 @@ -2150,14 +2182,7 @@ noprocessterminate: sysfn_terminate2: ;lock application_table_status mutex .table_status: - cli - cmp [application_table_status], 0 - je .stf - sti - call change_task - jmp .table_status -.stf: - call set_application_table_status + call lock_application_table mov eax, ecx call pid_to_slot test eax, eax @@ -2165,12 +2190,12 @@ sysfn_terminate2: mov ecx, eax cli call sysfn_terminate - and [application_table_status], 0 + call unlock_application_table sti and dword [esp+32], 0 ret .not_found: - mov [application_table_status], 0 + call unlock_application_table or dword [esp+32], -1 ret ;------------------------------------------------------------------------------ @@ -2219,6 +2244,7 @@ sysfn_activate: ; 18.3 = ACTIVATE WINDOW @@: ;------------------------------------- mov [window_minimize], 2; restore window if minimized + call wakeup_osloop movzx esi, word [WIN_STACK + ecx*2] cmp esi, [TASK_COUNT] @@ -2234,7 +2260,7 @@ sysfn_activate: ; 18.3 = ACTIVATE WINDOW ret ;------------------------------------------------------------------------------ sysfn_getidletime: ; 18.4 = GET IDLETIME - mov eax, [idleusesec] + mov eax, [CURRENT_TASK+32+TASKDATA.cpu_usage] mov [esp+32], eax ret ;------------------------------------------------------------------------------ @@ -2276,6 +2302,7 @@ sysfn_sound_flag: ; 18.8 = get/set sound_flag ;------------------------------------------------------------------------------ sysfn_minimize: ; 18.10 = minimize window mov [window_minimize], 1 + call wakeup_osloop ret ;------------------------------------------------------------------------------ align 4 @@ -2338,6 +2365,7 @@ sysfn_centermouse: ; 18.15 = mouse centered mov eax, [Screen_Max_Y] shr eax, 1 mov [MOUSE_Y], ax + call wakeup_osloop ; ret ;* mouse centered - end code- Mario79 xor eax, eax @@ -2382,6 +2410,7 @@ sysfn_mouse_acceleration: ; 18.19 = set/get mouse features cmp dx, word[Screen_Max_X] ja .end mov [MOUSE_X], edx + call wakeup_osloop ret .set_mouse_button: ; cmp ecx,5 ; set mouse button features @@ -2389,6 +2418,7 @@ sysfn_mouse_acceleration: ; 18.19 = set/get mouse features jnz .end mov [BTN_DOWN], dl mov [mouse_active], 1 + call wakeup_osloop .end: ret ;------------------------------------------------------------------------------ @@ -2848,6 +2878,7 @@ nosb8: mov [draw_data+32 + RECT.bottom], edx inc byte[REDRAW_BACKGROUND] + call wakeup_osloop ;-------------------------------------- align 4 .exit: @@ -2874,6 +2905,7 @@ force_redraw_background: mov [draw_data+32 + RECT.bottom], ebx pop ebx eax inc byte[REDRAW_BACKGROUND] + call wakeup_osloop ret ;------------------------------------------------------------------------------ align 4 @@ -3532,6 +3564,7 @@ markz: @@: add edx, 0x20 loop markz + call wakeup_osloop ;-------------------------------------- align 4 @@: @@ -3683,6 +3716,7 @@ align 4 align 4 @@: add byte[REDRAW_BACKGROUND], dl + call wakeup_osloop jmp newdw8 ;-------------------------------------- align 4 @@ -3704,6 +3738,7 @@ align 4 cmp dword [esp], 1 jne nobgrd inc byte[REDRAW_BACKGROUND] + call wakeup_osloop ;-------------------------------------- align 4 newdw8: @@ -4697,11 +4732,17 @@ endg jbe @f mov word [msg_board_pos], 10 @@: +end if +if 0 + pusha + mov al, bl + mov edx, 402h + out dx, al + popa end if inc ecx and ecx, msg_board_data_size - 1 mov [msg_board_count], ecx - mov [check_idle_semaphore], 5 ret .smbl1: cmp eax, 2 diff --git a/kernel/trunk/kernel32.inc b/kernel/trunk/kernel32.inc index e0efa79d24..5d0afb4c7e 100644 --- a/kernel/trunk/kernel32.inc +++ b/kernel/trunk/kernel32.inc @@ -132,19 +132,21 @@ struct APPDATA wnd_shape_scale dd ? ;+132 dd ? ;+136 mem_size dd ? ;+140 - saved_box BOX - ipc_start dd ? - ipc_size dd ? - event_mask dd ? - debugger_slot dd ? - terminate_protection dd ? - keyboard_mode db ? + saved_box BOX ;+144 + ipc_start dd ? ;+160 + ipc_size dd ? ;+164 + event_mask dd ? ;+168 + debugger_slot dd ? ;+172 + terminate_protection dd ? ;+176 + keyboard_mode db ? ;+180 rb 3 - dir_table dd ? - dbg_event_mem dd ? - dbg_regs DBG_REGS - wnd_caption dd ? - wnd_clientbox BOX + dir_table dd ? ;+184 + dbg_event_mem dd ? ;+188 + dbg_regs DBG_REGS ;+192 + wnd_caption dd ? ;+212 + wnd_clientbox BOX ;+216 + priority dd ? ;+232 + in_schedule LHEAD ;+236 ends diff --git a/kernel/trunk/network/stack.inc b/kernel/trunk/network/stack.inc index 4a82ce3408..779f9ffa94 100644 --- a/kernel/trunk/network/stack.inc +++ b/kernel/trunk/network/stack.inc @@ -191,6 +191,12 @@ stack_init: mov [last_1sTick], al ret +; Wakeup every tick. +proc stack_handler_has_work? + mov eax, [timer_ticks] + cmp eax, [last_1hsTick] + ret +endp ;*************************************************************************** diff --git a/kernel/trunk/network/udp.inc b/kernel/trunk/network/udp.inc index 81929764bd..b8a0c37865 100644 --- a/kernel/trunk/network/udp.inc +++ b/kernel/trunk/network/udp.inc @@ -146,8 +146,6 @@ proc udp_rx stdcall shl ecx, 8 or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event - mov [check_idle_semaphore], 200 - .exit: pop eax call freeBuff ; Discard the packet