minix 3.1 context switching jmp code need help

142 views
Skip to first unread message

zhang jin

unread,
Nov 19, 2023, 3:47:20 AM11/19/23
to minix3
i am reading minix3.1 text book, the save procedure in the book on page 712 tests
whether the caller enter the kernel for the first time (from user to kernel) or later
(from kernel to kernel).
on line 6638 jmp RETADR-P_STACKBASE(eax)

The RETADR = 24
P_STACKBASE = 0
eax is essentially esp before testing k_reenter for kernel stack switch
so this statement is essentially

jmp 24-0(esp)

how does this work? it looks rather odd to me

Code:
06613
06614 !*===========================================================================*
06615 !* save *
06616 !*===========================================================================*
06617 ! Save for protected mode.
06618 ! This is much simpler than for 8086 mode, because the stack already points
06619 ! into the process table, or has already been switched to the kernel stack.
06620
06621 .align 16
06622 save:
06623 cld ! set direction flag to a known value
06624 pushad ! save "general" registers
06625 o16 push ds ! save ds
06626 o16 push es ! save es
06627 o16 push fs ! save fs
06628 o16 push gs ! save gs
06629 mov dx, ss ! ss is kernel data segment
06630 mov ds, dx ! load rest of kernel segments
06631 mov es, dx ! kernel does not use fs, gs
06632 mov eax, esp ! prepare to return
06633 incb (_k_reenter) ! from -1 if not reentering
06634 jnz set_restart1 ! stack is already kernel stack
06635 mov esp, k_stktop
06636 push _restart ! build return address for int handler
06637 xor ebp, ebp ! for stacktrace
06638 jmp RETADR-P_STACKBASE(eax)
06639
06640 .align 4
06641 set_restart1:
06642 push restart1
06643 jmp RETADR-P_STACKBASE(eax)
06644

Chris

unread,
Nov 19, 2023, 4:28:00 PM11/19/23
to minix3
It's basically returning to the code that called save() by jumping to the return address instead of using ret, which would affect the stack pointer.

EAX = old ESP, which has a full stack frame(as defined in kernel/type.h) at the top. The initial part of this save routine was building this saved stack frame.

P_STACKBASE and RETADR are defined in kernel/sconst.h, and are constants for accessing parts of that stack frame from assembler code.

If I calculated correctly, RETADR would have the value 0x22, but that's kind of irrelevant.

The JMP command uses the offset RETADR into EAX to point to the return address of the caller. This is close to the AT&T assembly syntax addressing scheme, in which(for example) mov 4(%esp), %eax means "move the dword at *(esp + 4) to eax".
Reply all
Reply to author
Forward
0 new messages