[comp.os.os2.programmer] Info on Changing Stacks in protected mode

resnicks@netcom.COM (Steve Resnick) (03/29/91)

I saw this article in C User's Journal about multi-threaded C using 
setjmp/longjmp. This was something proposed to me a while ago, and thought
I'd try and work up my own code. After looking at the code I realized that
each thread is going to need a new stack. Assembler in hand, I write this
fairly generic assembler routine which launches the new thread with a new
stack. Everything works great until I run this under OS/2 rather than MS DOS.
OS/2 will not let me clear interrupts while changing SS:SP.  My question:
Is it necessary to disable interrupts while switching stacks in protected 
mode? If so, what is the appropriate meathod under OS/2? Is there a call
to prevent task switching/interrupt processing? Or, should it be implemented
as part of an IOPL segment? 

Thanx for your support :) 


Steve

-- 
-------------------------------------------------------------------------------
	resnicks@netcom.com, steve@camphq, IFNA:	1:143/105.0, 
USNail: 530 Lawrence Expressway, Suite 374 
        Sunnyvale, Ca 94086
- In real life: Steve Resnick. Flames, grammar and spelling errors >/dev/null
0x2b |~ 0x2b, THAT is the question.
The Asylum OS/2 BBS - (408)263-8017 12/2400,8,1 - Running Maximus CBCS 1.2
-------------------------------------------------------------------------------

kevinj@unicorn.cc.wwu.edu (Kevin Johnson) (03/30/91)

resnicks@netcom.COM (Steve Resnick) writes:


>I saw this article in C User's Journal about multi-threaded C using 
>setjmp/longjmp. This was something proposed to me a while ago, and thought
>I'd try and work up my own code. After looking at the code I realized that
>each thread is going to need a new stack. Assembler in hand, I write this
>fairly generic assembler routine which launches the new thread with a new
>stack. Everything works great until I run this under OS/2 rather than MS DOS.
>OS/2 will not let me clear interrupts while changing SS:SP.  My question:
>Is it necessary to disable interrupts while switching stacks in protected 
>mode? 

I always thought it was necessary, and that's how I do it.  Of course, I'm
not an expert on the issue.

>If so, what is the appropriate meathod under OS/2? Is there a call
>to prevent task switching/interrupt processing? Or, should it be implemented
>as part of an IOPL segment? 

Under OS/2, you will want the stack switching code to be in its own segment,
then make that segment have IOPL privileges. 

				--Kevin

dj@bragi.ctron.com (DJ Delorie) (03/30/91)

In article <1991Mar29.155840.2626@netcom.COM> resnicks@netcom.COM (Steve Resnick) writes:
>Is it necessary to disable interrupts while switching stacks in protected 
>mode? If so, what is the appropriate meathod under OS/2? Is there a call
>to prevent task switching/interrupt processing? Or, should it be implemented
>as part of an IOPL segment? 

The Intel programming manuals state that after any instruction that
loads the stack segment register (SS), interrupts will be temporarily
disabled until the completion of the next instruction.  Thus, you can
do this:

	mov	ss,ax
	mov	sp,bx

and interrupts will not be enabled between the two.

Note for 386's:  Intel recommends using the LSS opcode to prevent
page faults and other exceptions between the two above instructions.


DJ
dj@ctron.com

cur022%cluster@ukc.ac.uk (Bob Eager) (03/31/91)

In article <1991Mar29.155840.2626@netcom.COM>, resnicks@netcom.COM (Steve Resnick) writes:
> OS/2 will not let me clear interrupts while changing SS:SP.  My question:
> Is it necessary to disable interrupts while switching stacks in protected 
> mode?

Unless you have a very early (buggy) 8088 chip (unlikely on OS/2) you
don't need to disable interrupts anyway. I quote from the 80286
Programmer's Reference Manual: "Any move into SS will disable interrupts
until after the execution of the next instruction".
-------------------------+-------------------------------------------------
Bob Eager                | University of Kent at Canterbury
                         | +44 227 764000 ext 7589
-------------------------+-------------------------------------------------

tabu6@CCVAX.IASTATE.EDU (Adam Goldberg) (03/31/91)

In article <1991Mar29.183850.27619@unicorn.cc.wwu.edu>, kevinj@unicorn.cc.wwu.edu (Kevin Johnson) writes:
>resnicks@netcom.COM (Steve Resnick) writes:
>>OS/2 will not let me clear interrupts while changing SS:SP.  My question:
>>Is it necessary to disable interrupts while switching stacks in protected 
>>mode? 
>
>I always thought it was necessary, and that's how I do it.  Of course, I'm
>not an expert on the issue.

As part of the hardware in the 80x86 series, interrupts are disabled for
exactly one instruction after loading the SS, in order for you to load the SP. 
You need not do it yourself in protected mode or otherwise--Intel has already
done it for you, and its built into the processor.


+----------------------------------------------------------------------------+
+ Adam Goldberg                         Bitnet:   tabu6@ISUVAX.BITNET        +
+ Iowa State University                 Internet: tabu6@CCVAX.IASTATE.EDU    +
+          "It's simple!  Even a Pascal programmer could do it!"             +
+ "Remember: The sooner you fall behind, the more time you have to catch up" +
+----------------------------------------------------------------------------+

ralf+@cs.cmu.edu (Ralf Brown) (04/13/91)

In article <71689@microsoft.UUCP> donh@microsoft.UUCP (Don HACHERL) writes:
}In article <1991Mar29.155840.2626@netcom.COM> resnicks@netcom.COM (Steve Resnick) writes:
}>Everything works great until I run this under OS/2 rather than MS DOS.
}>OS/2 will not let me clear interrupts while changing SS:SP.  My question:
}>Is it necessary to disable interrupts while switching stacks in protected 
}
}Not only do you not need an IOPL segment, you don't even need to
}disable interrupts, under DOS or OS/2, because the chip will do it for
}you.  To quote from my handy Intel refernce manual "A MOV into SS
}instruction inhibits all interrupts until after the execution of the
}next instruction (which is presumably a MOV into SP instruction)."

The reason why everybody disables interrupts during a stack switch is that
the early 8088s had a microcode bug which caused interrupts to remain
enabled after a MOV into SS.  If your code can't run on an 8088/8086, you
won't need the CLI.
--
{backbone}!cs.cmu.edu!ralf  ARPA: RALF@CS.CMU.EDU   FIDO: Ralf Brown 1:129/3.1
BITnet: RALF%CS.CMU.EDU@CMUCCVMA   AT&Tnet: (412)268-3053 (school)   FAX: ask
DISCLAIMER?  Did  | It isn't what we don't know that gives us trouble, it's
I claim something?| what we know that ain't so.  --Will Rogers