;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; targ-switch.s - Strata contex switch primative operations
; 
; Copyright (c) 2000, 2004 - Dan Williams and Jack W. Davidson
;
; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


section .data
frag_jmp_addr    dd    0
junk             dd    0

section .text

extern strata_build_main, hashtable_get_default, intercept_sigaction, syscall_watch_lookup, targ_exec

BITS32

  global targ_start
  global strata_stop
  global targ_reenter

; Macros

; These offsets are based on targ-build.c defines
; NOTE: the offsets assume 32 bit int, if that changes, these
; values must change.
%define STRATA_FROMFRAG(base) [base+106]
%define STRATA_REDIRECT(base) [base+126]
%define STRATA_SYSCALL(base)  [base+126]
;; This technically causes a segfault, not an illegal instruction
;; but I'm unable to find a "standard" illegal instruction for x86
%define ILLEGAL_INSN          mov eax, [0]

; targ_start -
; This function is called by by the application immediately after strata_get_start_fn, and it is used to first transfer
; control from the application to strata. It does this by (basically) doing
; the same things that occur doing a fragment trampoline.
targ_start:

  pop eax	; pop the return address, this is the first instruction to execute, and will be passed to s_b_m()

  sub esp, 28	; this is the code for a trampoline with the next fragment stored in eax, and no from-frag
  push dword [esp]
  pushad
  pushfd
  push dword 0
  push eax
  ; We are simulating a call, but we don't want to return here,
  ; instead we return to targ_exec
  push targ_exec
  jmp strata_build_main

  ; Matainance note: targ_reenter is no
  ; longer used. The entire context save
  ; has been pushed into the trampoline
targ_reenter:
  ILLEGAL_INSN


; 
; see strata_init for details
;
; strata_stop - function that returns it's own address so that it can be found w/o writing dynamic linker ickyiness.
;
strata_stop:
	call strata_stop_L1
strata_stop_L1:
	pop eax
	sub eax, 5
	ret