;       SCCSID = @(#)tkstubb1.asm	6.1 90/11/17
SCCSID	equ	"@(#)tkstubb1.asm	13.55 90/09/17"

;
;	IBM/Microsoft Confidential
;
;	Copyright (c) IBM Corporation 1987, 1989
;	Copyright (c) Microsoft Corporation 1987, 1989
;

INCL_ERROR_H	EQU	1		; for error.inc	 EQUates
INCL_ERROR2_H	EQU	1		; for error2.inc EQUates

.xlist
include dosseg.inc
include basemaca.inc
include fnotask.inc
include osmaca.inc
include mi.inc
include tib.inc
include protmode.inc
include seldesc.inc
include bseerr.inc
include tkexport.inc
include osconst.inc
include pgexport.inc
include vmexport.inc
include selxport.inc
include except.inc
include sigdef.inc
include dossymc.inc
include curdir.inc
include dirent.inc
include find.inc
include screen.inc
include sem.inc
include infoseg.inc
include kmode.inc
include semxport.inc
include tkptda.inc
include proc.inc
.list
	CpuMode 386
	ModName tkstubb1, FNO_TKSTUBB1_ASM

	ReferFlat	_pTCBCur,DWORD
	ReferFlat	_pPTDACur,DWORD

	EXTRN16 Bmp_Free
	EXTRN16 GetTCBReqPkt
	EXTRN16 UsrArgFault

	EXTRN	w_EnterCritSec:NEAR
	EXTRN	w_ExitCritSec:NEAR
	EXTRN	w_HoldSignal:NEAR

	EXTRN32 _TKExit
	EXTRN32 _TKSuDWord
	EXTRN32 _tkSchedNextRet


	FARCODE HIGH32
_TEXT	SEGMENT

;***EP	TKUsrArgFault - C callable interface to UsrArgFault
;
;	ENTRY:	None
;
;	EXIT:	None
;
;	See Trap286.asm for details of UsrArgFault
	ASSUME DS:FLAT, ES:FLAT, SS:NOTHING
Procedure _TKUsrArgFault,near
	SaveReg	   <ds,es,ebp,ebx,esi,edi>
	CALL16	UsrArgFault
	RestoreReg <edi,esi,ebx,ebp,es,ds>
	RETC
EndProc _TKUsrArgFault


;***EP	_TKDisable - C callable, cli and return old eflags
;
;	ENTRY	None
;
;	EXIT	EAX - old flags
;		Interrupt Flag cleared
;
;	FS and GS must be preserved.

Procedure _TKDisable
	ASSUME DS:FLAT, ES:FLAT, SS:NOTHING
	pushfd
	pop	eax
	cli
	RETC
EndProc _TKDisable


;***EP	_TKEnable - C callable, equivalent to masm popf
;
;	ENTRY	Flg	Flags to restore
;
;	EXIT	Flg copied to eflags
Procedure _TKEnable,,,,esp
ArgVar nbl_flags,dword
	ASSUME DS:FLAT, ES:FLAT, SS:NOTHING
	push	[nbl_flags]		; push new flags
	popfd				; restore flags
	RETC
EndProc _TKEnable


;***LP	_tkGetTCBReqPkt - Get Threads Request Packet
;
;	This procedure is a C-callable thunk to the procedure
;	GetTCBReqPkt.
;
;	ENTRY	((SS:ESP)+4) = thread's tcb pointer
;	EXIT	NONE
;	USES	EAX, Flags

Procedure _tkGetTCBReqPkt,near
	ASSUME	DS:FLAT, ES:FLAT, SS:NOTHING

	ArgVar	pTCB,DWORD		; Thread's TCB

	EnterProc
	SaveReg <ebx,esi,edi,es,ds>
	DosContext ds
	CALL16	GetTCBReqPkt		; Get TCB Request packet
	mov	esi,pTCB
	FlatContext ds
	or	ax,ax
	jnz	short grp1
	or	ds:[esi].TCBReqPktFlg,TK_RP_ALLOCATED
	mov	ds:[esi].TCBReqPkt.Offst,dx
	mov	ds:[esi].TCBReqPkt.Segmt,cx
    ASSUME	DS:FLAT
grp1:	RestoreReg <ds,es,edi,esi,ebx>
	LeaveProc
	RETC

EndProc _tkGetTCBReqPkt

;***LP	_tkFreeTCBReqPkt - Free Threads Request Packet
;
;	This procedure is a C-callable thunk to the procedure
;	FreeTCBReqPkt.
;
;	ENTRY	((SS:ESP)+4) = thread's tcb pointer
;	EXIT	NONE
;	USES	EAX, Flags
;
;	WARNING: depends on Bmp_Free to not modify EBX or DS.

Procedure _tkFreeTCBReqPkt,near
	ASSUME	DS:FLAT, ES:FLAT, SS:NOTHING

	ArgVar	pTCB,DWORD		; Thread's TCB

	EnterProc
	SaveReg <esi,edi,es>
	mov	esi,pTCB
	or	eax,eax
	test	ds:[esi].TCBReqPktFlg,TK_RP_ALLOCATED
	jz	short frp1
	mov	es,ds:[esi].TCBReqPkt.Segmt
	mov	di,ds:[esi].TCBReqPkt.Offst
	CALL16	Bmp_Free
frp1:	RestoreReg <es,edi,esi>
	LeaveProc
	RETC

EndProc _tkFreeTCBReqPkt


;***LP	_tkThreadStart - return address for an external thread
;
;	This procedure is where execution begins after the return
;	from _PGSwitchContext for an external thread.  This procedure
;	returns directly to user space.
;
;	ENTRY	NONE
;	EXIT	NONE
;	USES	EAX, EBX, ECX, EDX, EDI, ESI, EBP, DS, ES, Flags

Procedure _tkThreadStart,near
	ASSUME	DS:FLAT, ES:FLAT, SS:NOTHING
ifndef STDCALL
	add	esp,4			; Clean stack for PGSwitchContext
endif

	CCALL32 _tkSchedNextRet		; Finish SchedNext processing

;	Every external child thread comes to thread_start.

Entry	thread_start,near

;
; Initialize pexchain field of TIB
;
	mov	edi, [_pTCBCur]
	mov	esi, [edi].TCBptib
	add	esi, tib_pexchain

	CCALL32 _TKSuDWord <-1, esi>

	KMExitKmodeCallGate 0,noretval,PASCAL	; Exit to user space

EndProc _tkThreadStart


;***LP	_tkIThreadStart - return address for an internal thread
;
;	This procedure is where execution begins after the return
;	from _PGSwitchContext for an internal thread.  This procedure
;	returns to the start address specified by the caller of
;	TKCreateThread.
;
;	ENTRY	NONE
;	EXIT	NONE
;	USES	EAX, EBX, ECX, EDX, EDI, ESI, EBP, DS, ES, Flags

Procedure _tkIThreadStart,near
	ASSUME	DS:FLAT, ES:FLAT, SS:NOTHING
ifndef STDCALL
	add	esp,4			; Clean stack for PGSwitchContext
endif

	CCALL32 _tkSchedNextRet		; Finish SchedNext processing
	mov	eax,_pTCBCur		; (EAX) = TCB pointer
	test	[eax].TCBForcedActions,TK_FF_EXIT ; Stillborn thread?
	jnz	short its1		;  yes, skip ahead
	xor	ebp,ebp			; No frame set up
	RETC

its1:	xor	eax,eax			; Set exit code
	CCALL32 _TKExit <eax,eax>	; Cause thread to exit
ifdef MISCSTRICT
	Panic	"Return from TKExit() in internal thread."
endif

EndProc _tkIThreadStart



_TEXT	ENDS


	FARCODE HIGH3
HIGH3CODE	SEGMENT

;***LP	EnterCritSecHoldSig - enter critical section and hold signals
;
;	ENTRY	None
;
;	EXIT	None
;		Calls w_EnterCritSec and w_HoldSignal
;
;	USES	ALL except DS,ES,EBP

Procedure EnterCritSecHoldSig,<hybrid,far16>
	EnterProc
	SaveReg <ds,es,edx,ebp>
	call	w_EnterCritSec		; Enter Critical section
	mov	ax,1
	call	w_HoldSignal		; Hold signals
	RestoreReg <ebp,edx,es,ds>
	LeaveProc
	RETC
EndProc EnterCritSecHoldSig


;***LP	ExitCritSecUnHoldSig - exit critical section and re-enable signals
;
;	ENTRY	None
;
;	EXIT	None
;		Calls w_ExitCritSec and w_HoldSignal
;
;	USES	Flags

Procedure ExitCritSecUnHoldSig,<hybrid,far16>
	pushad
	SaveReg <ds,es>
	call	w_ExitCritSec		; Exit Critical section
	xor	ax,ax
	call	w_HoldSignal		; Re-enable signals
	RestoreReg <es,ds>
	popad
	RETC
EndProc ExitCritSecUnHoldSig

HIGH3CODE ENDS

CINIT_DATA	SEGMENT
	public	SysInitProc
SysInitProc	DB	"sysinit",0	; sysinit ptda's name
CINIT_DATA	ENDS

	END
