707,3 → 707,78 |
; mov esi,boot_sched_2 |
; call boot_log |
; ret |
|
; Three following procedures are used to guarantee that |
; some part of kernel code will not be terminated from outside |
; while it is running. |
; Note: they do not protect a thread from terminating due to errors inside |
; the thread; accessing a nonexisting memory would still terminate it. |
|
; First two procedures must be used in pair by thread-to-be-protected |
; to signal the beginning and the end of an important part. |
; It is OK to have nested areas. |
|
; The last procedure must be used by outside wanna-be-terminators; |
; if it is safe to terminate the given thread immediately, it returns eax=1; |
; otherwise, it returns eax=0 and notifies the target thread that it should |
; terminate itself when leaving a critical area (the last critical area if |
; they are nested). |
|
; Implementation. Those procedures use one dword in APPDATA for the thread, |
; APPDATA.terminate_protection. |
; * The upper bit is 1 during normal operations and 0 when terminate is requested. |
; * Other bits form a number = depth of critical regions, |
; plus 1 if the upper bit is 1. |
; * When this dword goes to zero, the thread should be destructed, |
; and the procedure in which it happened becomes responsible for destruction. |
|
; Enter critical area. Called by thread which wants to be protected. |
proc protect_from_terminate |
mov edx, [current_slot] |
; Atomically increment depth of critical areas and get the old value. |
mov eax, 1 |
lock xadd [edx+APPDATA.terminate_protection], eax |
; If the old value was zero, somebody has started to terminate us, |
; so we are destructing and cannot do anything protected. |
; Otherwise, return to the caller. |
test eax, eax |
jz @f |
ret |
@@: |
; Wait for somebody to finish us. |
call change_task |
jmp @b |
endp |
|
; Leave critical area. Called by thread which wants to be protected. |
proc unprotect_from_terminate |
mov edx, [current_slot] |
; Atomically decrement depth of critical areas. |
lock dec [edx+APPDATA.terminate_protection] |
; If the result of decrement is zero, somebody has requested termination, |
; but at that moment we were inside a critical area; terminate now. |
jz sys_end |
; Otherwise, return to the caller. |
ret |
endp |
|
; Request termination of thread identified by edx = SLOT_BASE + slot*256. |
; Called by anyone. |
proc request_terminate |
xor eax, eax ; set return value |
; Atomically clear the upper bit. If it was already zero, then |
; somebody has requested termination before us, so just exit. |
lock btr [edx+APPDATA.terminate_protection], 31 |
jnc .unsafe |
; Atomically decrement depth of critical areas. |
lock dec [edx+APPDATA.terminate_protection] |
; If the result of decrement is nonzero, the target thread is inside a |
; critical area; leave termination to leaving that area. |
jnz .unsafe |
; Otherwise, it is safe to kill the target now and the caller is responsible |
; for this. Return eax=1. |
inc eax |
.unsafe: |
ret |
endp |
|