add new asm which doesn't work yet
This commit is contained in:
@ -28,7 +28,7 @@ Thread_start:
|
|||||||
|
|
||||||
;; NOTE: New code with pusha/popa, restores all registers as I use this not only for first start
|
;; NOTE: New code with pusha/popa, restores all registers as I use this not only for first start
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
;; *ESP
|
;; ESP
|
||||||
;; SP --> RET ADDR
|
;; SP --> RET ADDR
|
||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
@ -79,22 +79,38 @@ Thread_switch:
|
|||||||
; * Hier muss Code eingefuegt werden
|
; * Hier muss Code eingefuegt werden
|
||||||
; *
|
; *
|
||||||
|
|
||||||
;; NOTE: New code with pusha/popa
|
;; NOTE: The thread switching works like this:
|
||||||
;; == High address ==
|
;; 1. Prev thread is running, pit interrupt triggers preemption
|
||||||
;; *ESP_NEXT
|
;; 2. CPU switches to interrupt/scheduler stack and calls Thread_switch,
|
||||||
;; *ESP_PREV
|
;; two arguments and the return address are pushed before the call,
|
||||||
;; SP --> RET ADDR
|
;; the return address leads back to the prev thread
|
||||||
;; == Low address ==
|
;; 3. Prev registers are pushed to prev stack
|
||||||
|
;; 4. Next registers are popped from next stack, the esp now points
|
||||||
|
;; to the return address it pointed to when the next thread was
|
||||||
|
;; exited in the past
|
||||||
|
;; 5. Return follows the return address to the next stack
|
||||||
|
|
||||||
push eax ; Backup eax
|
;; NOTE: New code with pusha/popa
|
||||||
|
;; 1. Backup the current esp to the prev stack
|
||||||
|
;; 2. Switch to the prev stack and pusha, pushf
|
||||||
|
;; 3. Update the prev stack esp so popf, popa works on
|
||||||
|
;; the correct address
|
||||||
|
;; 4. Jump back to the scheduler stack with the esp from 1.
|
||||||
|
;; 5. Jump to the next stack and popf, popa
|
||||||
|
|
||||||
|
push eax ; Backup eax because we use it later to temporarily store the esp to jump back to
|
||||||
|
; the interrupt stack
|
||||||
;; == High address == ; Scheduler stack
|
;; == High address == ; Scheduler stack
|
||||||
;; *ESP_NEXT
|
;; ESP_NEXT
|
||||||
;; + 0x8 *ESP_PREV
|
;; + 0x8 *ESP_PREV
|
||||||
;; RET ADDR
|
;; RET ADDR
|
||||||
;; SP --> EAX
|
;; SP --> EAX
|
||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
mov eax, [esp + 0x8]
|
mov eax, [esp + 0x8] ; Could probably leave this out as this value already is in eax because it' the first
|
||||||
|
; function parameter
|
||||||
|
|
||||||
|
mov eax, [eax] ; EAX points to the address of the thread's eax beacause we called with a pointer
|
||||||
;; == High address == ; Previous thread stack (thread that was running when the interrupt came)
|
;; == High address == ; Previous thread stack (thread that was running when the interrupt came)
|
||||||
;; OLD
|
;; OLD
|
||||||
;; THREAD
|
;; THREAD
|
||||||
@ -103,23 +119,6 @@ Thread_switch:
|
|||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
sub eax, 0x28
|
sub eax, 0x28
|
||||||
;; == High address ==
|
|
||||||
;; OLD
|
|
||||||
;; THREAD
|
|
||||||
;; STACK
|
|
||||||
;; 0x0 RET ADDR
|
|
||||||
;; 0x4
|
|
||||||
;; 0x8
|
|
||||||
;; 0xc
|
|
||||||
;; 0x10
|
|
||||||
;; 0x14
|
|
||||||
;; 0x18
|
|
||||||
;; 0x1c
|
|
||||||
;; 0x20
|
|
||||||
;; 0x24
|
|
||||||
;; EAX ->
|
|
||||||
;; == Low address ==
|
|
||||||
|
|
||||||
mov [eax], esp ; Current esp to old thread stack, 0x24 is the amount pusha, pushf change the esp
|
mov [eax], esp ; Current esp to old thread stack, 0x24 is the amount pusha, pushf change the esp
|
||||||
; We save it, push the current registers to the old threads stack, return and restore
|
; We save it, push the current registers to the old threads stack, return and restore
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
@ -139,15 +138,15 @@ Thread_switch:
|
|||||||
;; EAX -> ESP (Points not to RET ADDR but the EAX we pushed as backup!!!)
|
;; EAX -> ESP (Points not to RET ADDR but the EAX we pushed as backup!!!)
|
||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
;; BUG: Not the correct value
|
|
||||||
pop eax ; ESP still points to the EAX we pushed before
|
pop eax ; ESP still points to the EAX we pushed before
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
;; *ESP_NEXT
|
;; ESP_NEXT
|
||||||
;; *ESP_PREV
|
;; *ESP_PREV
|
||||||
;; SP --> RET ADDR
|
;; SP --> RET ADDR
|
||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
mov esp, [esp + 0x4]
|
mov esp, [esp + 0x4]
|
||||||
|
mov esp, [esp]
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
;; OLD
|
;; OLD
|
||||||
;; THREAD
|
;; THREAD
|
||||||
@ -166,51 +165,59 @@ Thread_switch:
|
|||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
pusha ; Save current registers to stack
|
pusha ; Save current registers to stack
|
||||||
;; == High address ==
|
|
||||||
;; OLD
|
|
||||||
;; THREAD
|
|
||||||
;; STACK
|
|
||||||
;; RET ADDR
|
|
||||||
;; EAX
|
|
||||||
;; ECX
|
|
||||||
;; EDX
|
|
||||||
;; EBX
|
|
||||||
;; ESP
|
|
||||||
;; EBP
|
|
||||||
;; ESI
|
|
||||||
;; EDI
|
|
||||||
;; SP -->
|
|
||||||
;; ESP
|
|
||||||
;; == Low address ==
|
|
||||||
|
|
||||||
pushf
|
pushf
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
;; OLD
|
;; OLD
|
||||||
;; THREAD
|
;; THREAD
|
||||||
;; STACK
|
;; STACK
|
||||||
;; RET ADDR
|
;; RET ADDR
|
||||||
;; EAX
|
;; 0x4 EAX
|
||||||
;; ECX
|
;; 0x8 ECX
|
||||||
;; EDX
|
;; 0xc EDX
|
||||||
;; EBX
|
;; 0x10 EBX
|
||||||
;; ESP
|
;; 0x14 ESP
|
||||||
;; EBP
|
;; 0x18 EBP
|
||||||
;; ESI
|
;; 0x1c ESI
|
||||||
;; EDI
|
;; 0x20 EDI
|
||||||
;; EFLAGS
|
;; SP --> EFLAGS
|
||||||
;; SP --> ESP
|
;; 0x28 ESP
|
||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
|
sub esp, 0x4
|
||||||
pop esp ; The POP ESP instruction increments the stack pointer (ESP)
|
pop esp ; The POP ESP instruction increments the stack pointer (ESP)
|
||||||
; before data at the old top of stack is written into the destination.
|
add esp, 0x4 ; before data at the old top of stack is written into the destination.
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
;; *ESP_NEXT
|
;; ESP_NEXT
|
||||||
;; *ESP_PREV
|
;; *ESP_PREV
|
||||||
;; RET ADDR
|
;; SP --> RET ADDR
|
||||||
;; SP -->
|
|
||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
mov esp, [esp + 0xc] ; Move to next coroutines stack
|
mov eax, [esp + 0x4] ; EAX is backed up and free to use now
|
||||||
|
mov ebx, eax ; Duplicate because we dereference eax later
|
||||||
|
mov eax, [eax] ; EAX points to prev stack
|
||||||
|
sub eax, 0x24 ; EAX points to EFLAGS that we pushf'd on prev stack
|
||||||
|
;; == High address ==
|
||||||
|
;; OLD
|
||||||
|
;; THREAD
|
||||||
|
;; STACK
|
||||||
|
;; RET ADDR
|
||||||
|
;; 0x4 EAX
|
||||||
|
;; 0x8 ECX
|
||||||
|
;; 0xc EDX
|
||||||
|
;; 0x10 EBX
|
||||||
|
;; 0x14 ESP
|
||||||
|
;; 0x18 EBP
|
||||||
|
;; 0x1c ESI
|
||||||
|
;; 0x20 EDI
|
||||||
|
;; EAX -> EFLAGS
|
||||||
|
;; == Low address ==
|
||||||
|
|
||||||
|
mov [ebx], eax ; Change prev thread's esp to point to the new bottom of stack
|
||||||
|
; so popf, popa works when prev thread becomes active again
|
||||||
|
|
||||||
|
;; ============================================================
|
||||||
|
|
||||||
|
mov esp, [esp + 0x8] ; Move to next coroutines stack
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
;; NEW
|
;; NEW
|
||||||
;; THREAD
|
;; THREAD
|
||||||
@ -228,21 +235,6 @@ Thread_switch:
|
|||||||
;; == Low address ==
|
;; == Low address ==
|
||||||
|
|
||||||
popf ; Load new registers from stack
|
popf ; Load new registers from stack
|
||||||
;; == High address ==
|
|
||||||
;; NEW
|
|
||||||
;; THREAD
|
|
||||||
;; STACK
|
|
||||||
;; RET ADDR
|
|
||||||
;; EAX
|
|
||||||
;; ECX
|
|
||||||
;; EDX
|
|
||||||
;; EBX
|
|
||||||
;; ESP
|
|
||||||
;; EBP
|
|
||||||
;; ESI
|
|
||||||
;; SP --> EDI
|
|
||||||
;; == Low address ==
|
|
||||||
|
|
||||||
popa
|
popa
|
||||||
;; == High address ==
|
;; == High address ==
|
||||||
;; NEW
|
;; NEW
|
||||||
|
|||||||
Reference in New Issue
Block a user