+ All Categories
Home > Documents > Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM...

Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM...

Date post: 13-Mar-2018
Category:
Upload: trandieu
View: 213 times
Download: 1 times
Share this document with a friend
7
/ Life isn't easy for the authors of commer cial DOS extenders these days. Their products must be capable of running on a completely standalone basis, taking full control of the 286, 386, and 486 proces sors in protected mode, and maintaining all the special CPU control registers, descriptor tables, and page tables. And their brainchildren must also be able to coexist harmoniously with memory managers or device drivers that imple ment any of the existing protocols or standards for extended memory allocation—be it top-down (INT 15h), bottom-up (VDISK), XMS (HJMEM.SYS), VCPI (QEMM-386 or 386MAX), or DPMI (Microsoft Windows 3.0). Developers of DOS extenders must also be masters of the various PC hardware architectures, with all their peculiarities and ramifica tions for mode switching, toggling of the A20 address line, and the like. This cer tainly isn't a job description I'd envy, even if it does carry its own unique rewards (the staff of Phar Lap Software has been growing, I suspect, as quickly as the Microsoft OS/2 development staff has been shrinking). But DOS extenders are a technologi cally fascinating topic, and fortunately events have conspired to place a low-cost test-bed for DOS extender experimenta tion at our disposal. I refer, of course, to the implementation of the DOS Protected Mode Interface (DPMI) in Windows 3.0. First, let's review how "real" DOS ex tenders work, and then we'll implement a simple little DPMI-based DOS extender of our own. THE TWO FACES OF A DOS EXTENDER A typical DOS extender that you might buy from Phar Lap or Rational Systems can be thought of as having two main sections: an initialization component and an interrupt handler component. The ini tialization portion of a DOS extender gains control of the machine when the DOS extender is loaded by DOS in real mode as a result of a user command or an INT Power Programming Creating a DPMI- Based DOS Extender Of Your Own DOS extenders are technologically fascinating and, luckily, the Windows 3.0. implementation of the DOS Protected Mode Interface gives us a low- cost test-bed for DOS extender experimentation. 21h, function 4Bh (EXEC) function call by another program. In most cases, the DOS extender and the protected-mode application (such as Mathematica, Auto CAD, or Lotus 1-2-3, Release 3.0) are bound together in the same .EXE file. However, the only part of the file visible to DOS is the DOS extender, because it's the only part described by the .EXE file header and relocation table. The initialization routine has a lot of work to do before the protected-mode application can start running. It must probe the environment and determine whether it's running on the bare hardware (aside from DOS) or in the presence of one of the several species of memory managers. It must allocate some extended memory and load the application's code and data. performing any necessary relocations and fixups. It must allocate additional con ventional memory (memory below 640K) for communication with MS-DOS and the ROM BIOS. It must build the neces sary global and local descriptor tables to support protected-mode addressing, which includes mapping some descriptors onto "magic" memory areas like the PSP, the environment block, the video refresh buffer, and the ROM BIOS data area, for the convenience of the application. It must switch the CPU from real mode into protected mode. And it must take full command of the CPU's interrupt subsystem by build ing an interrupt descriptor table, repro- gramming the 8259 interrupt controllers, and installing handlers for every type of interrupt that might occur in protected mode. From the DOS extender's point of view, there are three types of interrupts to worry about. The first group consists of the CPU faults or exceptions, usually caused by a program error but sometimes by a hard ware error (for example, a nonmaskable interrupt due to a RAM parity error) or an unexpected result on the numeric coproces sor. The second group consists oi the external hardware interrupts—signals that a peripheral device has received data, is ready to accept more data, or has encoun tered some other condition that needs attention. The third group are the soft ware interrupts, which occur when an application program executes an INT instruction. Control over software inter rupts is a particularly crucial aspect of DOS extender initialization, because it allows the DOS extender to function transparently by intercepting the applica tion's requests for ROM BIOS and MS- DOS services. The exact order in which the DOS extender will perform these initialization chores depends in part on the philosophy of the implementer and in part on the MARCH 12, 1991 PC MAGAZINE FFFl pcjs.org
Transcript
Page 1: Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM BIOS. It must build the ... grab bag of interrupt service routines. ning. and it's

/

Life isn't easy for the authors of commercial DOS extenders these days. Theirproducts must be capable of running ona completely standalone basis, taking fullcontrol of the 286, 386, and 486 processors in protected mode, and maintainingall the special CPU control registers,descriptor tables, and page tables. Andtheir brainchildren must also be able tocoexist harmoniously with memorymanagers or device drivers that implement any of the existing protocols orstandards for extended memoryallocation—be it top-down (INT 15h),bottom-up (VDISK), XMS (HJMEM.SYS),VCPI (QEMM-386 or 386MAX), or DPMI(Microsoft Windows 3.0). Developers ofDOS extenders must also be masters ofthe various PC hardware architectures,with all their peculiarities and ramifications for mode switching, toggling of theA20 address line, and the like. This certainly isn't a job description I'd envy,even if it does carry its own unique rewards(the staff of Phar Lap Software has beengrowing, I suspect, as quickly as theMicrosoft OS/2 development staff has beenshrinking).

But DOS extenders are a technologically fascinating topic, and fortunatelyevents have conspired to place a low-costtest-bed for DOS extender experimentation at our disposal. I refer, of course, tothe implementation of the DOS ProtectedMode Interface (DPMI) in Windows 3.0.First, let's review how "real" DOS extenders work, and then we'll implement asimple little DPMI-based DOS extenderof our own.

THE TWO FACES OF A DOS EXTENDERA typical DOS extender that you mightbuy from Phar Lap or Rational Systemscan be thought of as having two mainsections: an initialization component andan interrupt handler component. The initialization portion of a DOS extender gainscontrol of the machine when the DOSextender is loaded by DOS in real modeas a result of a user command or an INT

PowerProgrammingCreating a DPMI-Based DOS ExtenderOf Your Own

DOS extenders aretechnologicallyfascinating and, luckily,the Windows 3.0.implementation of theDOS Protected ModeInterface gives us a low-cost test-bed for DOSextender experimentation.

21h, function 4Bh (EXEC) function callby another program. In most cases, theDOS extender and the protected-modeapplication (such as Mathematica, AutoCAD, or Lotus 1-2-3, Release 3.0) arebound together in the same .EXE file.However, the only part of the file visibleto DOS is the DOS extender, because it'sthe only part described by the .EXE fileheader and relocation table.

The initialization routine has a lot ofwork to do before the protected-modeapplication can start running. It must probethe environment and determine whetherit's running on the bare hardware (asidefrom DOS) or in the presence of one ofthe several species of memory managers.It must allocate some extended memoryand load the application's code and data.performing any necessary relocations andfixups. It must allocate additional conventional memory (memory below 640K)for communication with MS-DOS andthe ROM BIOS. It must build the necessary global and local descriptor tables tosupport protected-mode addressing, which

includes mapping some descriptors onto"magic" memory areas like the PSP, theenvironment block, the video refresh buffer,and the ROM BIOS data area, for theconvenience of the application. It mustswitch the CPU from real mode into protectedmode. And it must take full command ofthe CPU's interrupt subsystem by building an interrupt descriptor table, repro-gramming the 8259 interrupt controllers,and installing handlers for every type ofinterrupt that might occur in protectedmode.

From the DOS extender's point of view,there are three types of interrupts to worryabout. The first group consists of the CPUfaults or exceptions, usually caused by aprogram error but sometimes by a hardware error (for example, a nonmaskableinterrupt due to a RAM parity error) or anunexpected result on the numeric coprocessor. The second group consists oi theexternal hardware interrupts—signals thata peripheral device has received data, isready to accept more data, or has encountered some other condition that needsattention. The third group are the software interrupts, which occur when anapplication program executes an INTinstruction. Control over software interrupts is a particularly crucial aspect ofDOS extender initialization, because itallows the DOS extender to functiontransparently by intercepting the application's requests for ROM BIOS and MS-DOS services.

The exact order in which the DOSextender will perform these initializationchores depends in part on the philosophyof the implementer and in part on the

MARCH 12, 1991 PC MAGAZINE FFFl

pcjs.org

Page 2: Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM BIOS. It must build the ... grab bag of interrupt service routines. ning. and it's

7 Til!

Power Programming

environment in which the DOS extenderwill run. For example, XMS and VCPImemory managers require that the DOSextender make its memory allocation callsin real mode, while requests to a DPMIhost for memory must be made in protected mode. Similarly, techniques for modeswitching differ with the PC architecture(PC/AT, PS/2, and so forth) and with thepresence or absence of XMS, VCPI, andDPMI. The only things we can say forsure about the initialization sequence arethat a minimal global descriptor table (GDT)must be constructed in real mode and thatthe CPU must be in protected mode before control passes to the entry point ofthe actual application program.

Once the application is launched, theDOS extender's initialization section isdead code; the memory it occupies can bereclaimed and reused. The application'scode now becomes the center of the action. The remaining parts of the DOSextender are activated only upon theoccurrence of one of the three kinds ofinterrupts listed earlier. In this respect,

the DOS extender is much like a real-mode terminate-and-stay-resident (TSR)program. But the DOS extender's statusafter initialization is somewhat more slippery than that of your average TSR.

From MS-DOS's perspective, the DOSextender is the only program that's run-

The fully initializedDOS extender can be

thought of simply as agrab bag of interruptservice routines.

ning. and it's a perfectly ordinary program at that: it lives in the memory thatMS-DOS allocated for it, and it makesnormal MS-DOS function calls. From theprotected-mode application's point of view,the DOS extender is nearly invisible (infact, the more invisible, the better^ theDOS extender); the application requests

MS-DOS and ROM BIOS services byexecuting the usual software interrupts,and somehow the right things happen—eventhough the interrupts are being executedin protected mode and the application ispassing virtual addresses of data and bufferslying in extended memory.

Looking at the DOS extender from theinside out (the viewpoint of its author),the fully initialized DOS extender can bethought of rather simply as a grab bag ofinterrupt service routines.

When a CPU fault or exception occurs, the DOS extender usually has littlerecourse but to terminate the protectedmode application, unless the applicationhas explicitly registered its own handlerfor the interrupt. If the application hasgone so far awry as to cause a stack underflow or general protection (GP) fault, forexample, it's obviously ailing and willonly cause more trouble if it's allowed tocontinue. (Interpreters and debuggers areobvious exceptions.)

External hardware interrupts are treateddifferently. If the protected-mode application hasn't registered a handler, theinterrupt is "reflected" to the originalreal-mode owner of the interrupt. In otherwords, the DOS extender fields the inter-

TINYDOSX.ASM

t i t l e TiNYDOSX DPMI -3ased Tiny DOS Ex tende rpage 5 5 , 1 3 2

; TIHYDOSX.ASM; c o p y r i g h t { C )

T i n y D P M I - B a s e d D o s E x t e n d e r1 9 9 0 Z i i - D a v i s C o i r . m u n i c a t l . c n 5

; PC Magaz ine * R a y D u n c a n

s t d i r .s t d o u ts t d e r r

e q ue q u

fl ; s t a n d a r d i n p u t h a n d l e1 ; s t a n d a r d o u t p u t h a n d l e2 ; s t a n d a r d e r r o r h a n d l eo d h ; A S C I I c a r r i a g e r e t u r n

I f e q u 0 a h ; A S C I I l i n e f a e c

DGROUP g r o u p DATA

DATA segment. w o r d p u b l i c ' D A T A '

:nodesw dd g ; f a r p o i n t e r t o D P M I m o d e; sw i t ch en t r y po i n t

i n t fi d v dd e ; a d d r e s s o f p r e v i o u sGP fault handleri n t 2 1 v dd 0 ; a d d r e s s o f p r e v i o u s

; i n t 2 1 H h a n d l e r

r e a l s e gr s a l s e l

d wd w 0 ; s e l e c t o r f o r r e a l m o d e b u f f e r

qof -nsg d b c r , l f - I fdb • T I i ; y d 0 5 X : g e n e r a l p r o t e c t i o n . a u l t ldb c r . l f

gpf:nsg_ l e n e q u S - g p f T i s g

2BH cpu s ta tus flags22H ES24 H DS26H FS30K CS2AH IP (CS:1P ignored I

■ 2CH CS DPUr function I2EH S? (£S:5P-3 to havt

i 38H ss hos t supp ly a i

! save protected r.oce DX; savo protected code KS1; cave protected node Es

; I n t 21H d i spa tch t ab le

abmsg_ len equ

r e g D I l a b e lr e g s D I d dr e g S l l a b e lr e g E S i d dr e g B P l a b e lr e g E 3 P d d

ddr e g B X l A b e lr e g E Q X d dr e g D X l a b e lr e g E D X d dr e g C X l a b e lr e g E C X d dr e g A X l a b e l

c r , l l , l £'T1NYD05X:c r , l fS -abmsg

w o r d

word0

3

0

ew o r dBw o r d

u n s u p p o r t e d D O S f u n c

r e a l n o d e r e g i s t ef o r D P M I t r a n s i a tC3H DI or EDI

flCH ( re10K 3X

14H DX or EDX

18H CX or ECX

* " d w1 fxn 00H er ro ina te

o f f s e t " T E X T i f x n u l h ; (Ml BIB char mput+ec i ioo f f s e t T E X T: f x n 3 2 h ; fxn 02H char outputoffset TEXTl f XBflSll ; fxn Ci l !o f f s e t T E X T i f x n 3 4 h ; fxn Si l l aux outputo f f s e t T E X T: £ x n 0 5 h ; f xn BSH or in te r ou tpu t

; fxn 0 6tio f f s e t T E X T: f x n 0 7 h ; fxn 0 7Ho f f s e t T E X T: f x n 0 8 h ; fxn flBH ;har input r.c echc

dw o f f s e t T = X T: s b o r to f f s e t T E X T: a b o r tc ' f s e t T E X T S f x n B b b i n p u t s t a t u so f f s o " " ' E X T: a b o r t ; fxn BCHo f f s e t T B X l i f x n fl d h : i x n e o no f f s e t T E X Ti f x r . O e h ; fxn OEHo f f s e t T E X T: n b o r to f f s e t T £ X T: a b c r t : fxn 10!io f f s e t T E X T: a b o r t ; fxn 118o f f s e t t e x t t a b o r to f f s e t T E X T: a b o r t : fxn 13Ho f f s e t T E X T: a b o r to f f s e t T E X T: a b o r t ; fxn 1SHo ' f s e t t e x t : a b o r t ; fxr. 16H

dw o f f s e t T E X T: a b o r t ; fxn 17Ho f f s e t T E X T i a b o r to f f s e t T E X T: f x n l 9 h ; fxn 19H g e t c u r r e n t d r . v eo f f s e t T E X Ts a b o r t

; fxn 1BH g e t c u r . d r i v e d t t ao f f s e t T E X T: f x n l c h ; fxn 1CH g e t d r i v e d a t ao f f s e t T E X T: s b o r t ; fxn 1DKo f f s e t T E X T: a b o r to f f s e t T E X T r a b o f .

dw O f f s e t T E X T: o b o r to f f s e t T E X T: a b o r to f f s e t T E X T: a b o r tc f L s e t T E X Ti a b o r t ; fxn 23Ho f f s e t T E X Ts o b e r t

dw c f f 3 e t T E X T: a b o r tdw o f f s e t T E X T: a b o r tdw o f f s e t T E X T: a b o r t ; fxn 27H

o f f s e t T E X T i a b c r to f f s e t T E X T i a b o r t ; fxn 29K

dw o f f s e t T E X T: f x n 2 a h ; fxn 2AH get deteo f f s e t T E X T: f x n ? b ho f f s e t T E X Ts f x n J c d ; fxn 2CH g a t t i r . e

Figure 1: Here's the source code for a skeleton DPMI 0.9-based DOS extender that can be linked with small-model C programs.

Plffl PC MAGAZINE MARCH 12, 1991 pcjs.org

Page 3: Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM BIOS. It must build the ... grab bag of interrupt service routines. ning. and it's

Power Programming

rupt, saves the CPU state, switches theGPU into real mode, and reissues the interruptwith an INT instruction. When the real-mode handler executes an interrupt return(IRET), the DOS extender recovers control, switches the CPU back into protectedmode, restores the CPU state, and thenissues its own IRET so that the application can continue.

Which brings us to the last category ofinterrupt that the DOS extender must dispose of: interrupts that are explicit MS-DOS or ROM BIOS function requests bythe application. There are a number ofdifferent software interrupts that theDOS extender must be prepared to copewith: MS-DOS's INT 20h through INT2Fh, the ROM BIOS video driver INTlOh, serial port driver INT 14h, keyboarddriver INT 16h, printer driver INT I7h,Microsoft Mouse driver INT 33h, and soon. Each one of these interrupts providesa pathway to a host of different subfunc-tions, typically selected by a value in registerAH. For instance, more than 80 functions(both documented and undocumented) aredefined for INT 21h. As I explained in thelast column, these functions can be di

vided into four classes:■ functions that require little more thana mode switch before passing them downto MS-DOS or the ROM BIOS;■ functions that address application buffersand therefore require data movement andaddress translation before they can bereissued to MS-DOS or the ROM BIOS;■ functions that must be completely re-

In any event, the DOSextender's handling ofan MS-DOS or ROMBIOS service request is

easy to visualize.

placed to make them meaningful in protected mode; and■ function calls that are unique to theDOS extender itself and provide specialservices that have no equivalents in MS-DOS or the ROM BIOS.

There is also an implicit fifth class offunctions, which I didn't mention previ

ously: the MS-DOS functions that theDOS extender author simply chooses notto support because they're either toodangerous or not worth the hassle. TheFCB file functions, the direct disk I/Ofunctions, and some of the undocumentedDOS functions that vary tremendouslyfrom one version of DOS to another aregood examples.

In any event, the DOS extender's handlingof an MS-DOS or ROM' BIOS servicerequest is easy to visualize. An umbrellaroutine is entered first; it saves the CPUflags and all the general and segmentregisters for later reference. The umbrellaroutine then decodes the function requestby using the function number in AH as anindex into a jump table, and then passescontrol to a handler that is specific to thefunction type. Functions that pass allparameters in registers and don't reference data in extended memory can fallthrough to another umbrella handler thatperforms the necessary mode switchingand reissues the interrupt in real mode.Nearly all functions that pass addressesof buffers or pass parameters by reference must be handled on an individualbasis, because there's regrettably littlesymmetry of structures or register usageacross DOS and ROM BIOS functioncalls. Of course, memory management

1 T I N Y D O S X . A S M 2 o f 4 1d w o f f s e t _ T E X T : f x n 2 d h ; fxn 2DH set t ine d w o f f s e t _ T E X T : a b o r t ; f x n 6 A Hd w o f f s e t T E X T i f x n 2 e h ; f x n 2 E I I s e t v e r i f y fl a g d w o f f s e t T E X T : a b o r t ; f x n 6 B Hd w o f f s e t T E X T : a b o r t ; fxn 2r t l d w o f f s e t ~ T E X T : e r r o r ; f x n C C I id w o f f s e t T E X T : f x n 3 0 h ; fxn 30H get DOS version d w o f f s e t T E X T : a b o r t ; f x n 6 D Hd w o f f o e t T E X T : a b o r t : fxn 3IH d w o f f s e t ~ T E X T : a b o r t ; f x n 6 E Hd w o f f s e t T E X T : a b o r t ; f xn 32 i ! d w o f f s e t T E X T : a b o r t ; f x n 6 F Hd w o f f a o t _ T E X T : f x n 3 3 h ; f x n 3 3 K g e t / s e t b r e a k fl a gd w o f f s e t T E X T : a b o r t ; fxn 34a D ATA e n d sd w o f f s e t _ T E X T : a b o r t ; fxn 35Hd w o f f s e t _ T E X T : f x n 2 6 h ; f x n 3 6 k g e t d r i v e i n f o _TEXT segment byte publ ic 'CODE'd w o f f s e t T E X T : a b o r t ; fxn 37Hd w o f f s e t ~ T E X T : e r r o r ; fxn 38R assure cs: TEXT,ds;DGROUPd w o f f s e t J T E X T : f x i l 3 9 h ; f x n 3 9 h c r e a t e d i r e c t o r yd w o f f s e t T E X T : f x n 3 a h ; f x n 3 A R d e l e t e d i r e c t o r y i n i t i a l i z a t i o n r o u t i n e f o r t h e T i n y D O S E x t e n d e r . F i r s t w e t e s t f c rd w O f f s e t _ T E X T : f x n 3 b h ; f x n 3 B H s e l e c t d i r e c t o r y the presence of a DPMI hont, got. the address of the mode switch entryd w o f f s e t _ T 3 X T : £ x n 3 c h ; f x n 3 c h c r e a t e fi l e p o i n t , a n d r e q u e s t t h e s w i t c h t o p r o t e c t e d m o d e . T h e n w e i n s t a l ld w o f f s e t T E X T : f x n 3 d h ; fxn 3DH open fi le a h a n d l e r f o r G P f a u l t s t o c i r c u m v e n t t h e K i n 3 b r a i n - d a r a c e d d i a l o g

box, and allocate so=te memory be-low 1 MB to use an a buffer ford w o f f s e t T E X T : f x n 3 e h ; f x n 3 E H c l o s e fi l ed w o f f s e t T E X T : f x n 3 f h ; f x n 3 F K r e a d fi l e co r r. u . i i caL ioR w i t h DOS. F ina l l y we i ns ta l l ou r own I n t 21H hand le rd w o f f s e t T E X T : f x n 4 6 h ; f x n « H w r i t e f i l e so we car. serv ice Dos ca l l s f roa i the pro tec ted mode app l ica t ion .d w o f f s e t T E X T ! f x n 4 i h ; f x n 4 1 H d e l e t e fi l ed w o f f s e t _ T E X T : f x n 4 2 h ; fxn 42H see* p u b l i c i n i t d O B Xd w o f f s e t T E X T : f x n 4 3 h j f x n 4 3 H g a t / s e t a t t r i b u t e sd w o f f s e t T E X T ; e r r o r ; fxn 44Hd w o f f s e t T E X T : f x n 4 5 h ; fxn 45H dup handle : n a v a x , 1 6 o 7 h ; g e t a d d r e s s o f D P K Id w o f f s e t T E X T : f X n « f c i n t 2 f h ; m o d e s w i t c h e n t r y p o i n t

□ r a x , a x j b a i l o u t i f n o S P K Id w o f f s e t T E X T : f x n ( 7 h ; f x n 4 7 H g e t c o r. d i r e c t o r yd w o f f s e t _ T £ X T : e r r o r ; fxn 4fiH j r . : i n i t 9d w o f f s e t _ T E X T : e r r o r ; fxn 4911 B O V w o r d p t r m o d e s w . d i ; s a v e f a r p o i n t e r t o

r t o v w o r d p t r m o d e s w + 2 , e s ; D P M I e n t r y p o i n td w o f f s e t _ T E X T : e r r o r ; fxn 4A.Hd w o f f s e t T E X T i e r r o r ; fxn 4BHd w o f f s e t T E X T : f x n 4 c h ; fxn 4CH terminate r a ' J b x , s i ; a l l o c a t e D P M I p r i v a t e d a t a

m o v a h , 4 B h j a r e a b e l o w 1 y . = b o u n d a r yd w o f f s e t j T E X T : a b o r t ; fxn 4DHd w o f f s e t T E X T . - e r r o r ; fxn 4EH i n t 2 1 bd w o f f s e t T E X T : e r r o rd w o f f s e t _ T E X T : a b o r t

; fxn 4FH; fxn 50K

j c i n i t S ; j u m p , a l l o c a t i o n f a i l e dd w o f f s e t ~ T E X T t a b o r td w o f f s e t _ T E X T : a b o r td w o f f s e t ~ T E X T : a b o r td w o f f s e t T E X T : f x n 5 4 h

; fxn 51H; fxn 52K; fxn 53K; f x n 5 4 8 g e t v e r i f y fl a g

W o n e s . a x ; p a s s s e g m e n t o f d a t a a r e am o v a x , 0 ; b i t 0 - 0 i n d i c a t e s I t - b i t a p pc a l l m o d o s w ■ s w i t c h t o p r o t e c t e d m o d e

c w o f f s e t " T S X T - . a b o r td w o f f s e t T E X T ; e r r o rd w O f f s e t _ T 5 X T : f x n S 7 h

; fxn 55H; fxn 5GH; f x n S 7 h g e t / s o t fi l e d a t e

I B O V a x , G 2 0 2 h ; y e t a d d r e s s o f p r e v i o u sr o v b l . fl d h t o w n e r o f O P f a u l t v e c t o ri n t l l h

d w o f f s e t T E X T : e r r o r ; fxn Sen O O V w o r d p t r i n t O r i v , d x ; s a v e a s f a r p o i n t e rm o v w o r d p t r i n t 0 d v + 2 , c xd w o f f s e t T E X T : a b o r t ; f xn 59 ! l

- d w o f f s e t J T E X T i f x n S e h ■; fxn 5A.H create te-p f i led w o f f s e t T E X T : f x n S b h ; f x n 5 B H c r e a t e u n i q u e fi l e B O V a x , B 2 B 3 h ; I n s t a l l o u r G ? f a u l t , t w n c l e rd w o f f s e t J T E X T : f x n $ c h ; f xn SCH lock /un lockd w o f f s e t T E X T : a b o r t ; fxn 5DH m a v c x , c s : C X : D X =■ h a n d l e r a d d r e s sd w o f f s e t T E X T i e r r o rd w o f f s e t T E X T : e r r o r

; fxn Seh; fxn 5EH

m o v d x , o f f s e t _ _ T £ X T: y p f i s rd w o f f s e t _ T E X T : s b o r td w o f f s e t ~ T E X T : a b o r t '

; fxn 6ZH; f x n S l H j c i n i t 9 ; j u m p , c o u l d n ' t i n s t j ! 1

d w o f f s e t T E X T : a b o r td w o f f s e t ~ T E X T : e r r o rd w o f f s e t _ T E X T : a b o r td w o f f s e t J T E X T i e r r o r

; fxn CZI1; fxn 63H; fxn 64H; fxn 651i

m o v a x , P 1 0 i ) h ; a l l o c a t e 6 4 K B b u f f e r i nm o v b x , l 0 0 H h ; c o n v e n t i c n a ' m e m o r y f o ri n t 3 i i , ; c o m m u n i c a t i o n w i t h D O S

d w o f f s e t _ T E X T : a r r o r ; fxn 66K i c i n i t S ; j u m p , a l l o c a t i o n f a i l e dd w o f f s e t " ~ T E X T : e r r o r ; fxn 67n

m o v r e a l s e g . n x ■ g a v e s e g m e n t o f b l o c kd w o f f s e t T K X T i t x t i S s h ; f xn 6SH co i rm i t fi l e

m o v r e a l s e l , t i x . 3 a v e s e l e c t o r f o r b l o c kd w o f f s e t _ T E X T t a b o r t ; fxn 69H fi o v a x , 0 2 B 4 h ; g e t a d d r e . i s o f p r e v i o u s

MARCH 12. 1991 PC MAGAZINE EI3

pcjs.org

Page 4: Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM BIOS. It must build the ... grab bag of interrupt service routines. ning. and it's

Power Programming

function calls must be trapped and serviced entirely within the DOS extender,and this can be a fair amount of work initself (particularly if the DOS extendersupports virtual memory).

One more function that bears mentioning is INT 21h, function 4Ch. whichan application calls to terminate itself.When the DOS extender sees this function request, it must deallocate its (andthe application's) extended memory, release the interrupt subsystem, and removeany other little tendrils it may have inserted elsewhere. This is tricky business,because the DOS extender—which hasbeen playing the role of a little protected-mode operating system that uses DOS asa file system slave—must quietly andgracefully put everything back exactly asit was before the DOS extender was loaded,leaving the system completely stable. Finally,the DOS extender must switch the CPUinto real mode for the last time and itselfcall INT 21 h, function 4Ch, so that DOSknows to free all conventional memory,file handles, and other resources that wereassigned to the DOS extender.

A SIMPLE DPMI-BASED DOS EXTENDERI always find source code much moreenlightening than windy explanations, soI've illustrated the foregoing discussionof DOS extenders with TINYDOSX. ASM(Figure 1). a skeleton DOS extender thatyou can use in your own programs.TINYDOSX relies only on the existence

Memory managementfunction calls must be

trapped and servicedentirely within the DOSextender, which can bea fair amount of work.

of a DPMI 0.9 host, such as the one foundin Windows 3.0; it can be linked into anysmall-model C program and will causethat program to execute in protectedmode—provided you don't call any runtime library functions that include self

modifying code, use segment registersfor scratch storage, perform segment register arithmetic, or in general execute anyinstruction that will result in a generalprotection fault. Although Windows 3.0includes a DOS extender of its own,TINYDOSX doesn't use it and will runjust as well in other DPMI. Version 0.9,environments (at least theoretically; noother such environments exist for testingat this time).

The initialization portion of TINYDOSX,embodied in the routine 1N-ITDOSX, isstraightforward. To keep things simple,we allow the C application to get controlfirst in real mode, and require it to explicitly call the DOS extender, rather than theother way around. INITDOSX first callsINT 2Fh. function 1687h to find out whethera DPMI host is present, and if so, theaddress of the mode switch entry point. Ifa DPMI host is not found. INITDOSXbails out with an error message', otherwise, it proceeds to allocate the privatedata area required by the DPMI host andthen requests the switch into protectedmode.

Once it's running in protected-mode,INITDOSX installs protected mode handlers for MS-DOS INT 21 h and for generalprotection faults (so that Windows won't

TINYDOSX.ASM

bl,21h31hword ptr int21v,dxword ptr int21v+2,c:

ax,S205hbl,21h

install our Int 21H belli

CX:DX - handler address■ TEXT:doacall

• Interrupt service routine for GP faults. Entered by a far cl; from DPHI host with CS:IP, flags, CPU error code on stack.; He force transfer to our error message routine by changingj return address in the stack frame.

p ; p o i n t C S : I P i n s t a c k f r .p , s ? ; G P f a u l t e r r o r m e s s a g eord ptr [bp+S],offset _TF.XT:gpferr

.a routine gains control after the GPFISR returns tosimply displays an error r.essace and terminates clo;i 3's -Application has violated system integrity di;

b l , a h ; f u n c t i o n n u m b e r ■ 2b h , b hb x , 6 f ha b o r tb x , b x[ c l i s p a t c h + b x l

; f u n c t i o n n o - t o e b i n . ?; y e s , b a i l o u t; n o , b r a n c h t h r o u g h t a b l'■ t o f u n c t i o n h a n d l a r

d x . o f f s e t D G R O U P : a b : r . 5 c; u n s u p p o r t e d D O S £ u r . c t i o; d i s p l a y e r r o r M s s a f j e

c a t , a b t n s g _ l o n

a h , 4 0 b2 1 ba x , 4 c fl l h ; a n d e x i t t o D O S2 1 h

o r d p t r [ b p + 6 ] , l

x r fl l h :xnOSli:xn03h:

Th-:- DOSCAU routine is tho runtime portion of the TinyIt traps Int 21H requests in protected code and perfomnode switching, data movement, and address translationfunction-by-function basis. Anything doscall dceen't ■■it either fails by setting the carry flag arvd returnln;it aborts t.lin current program. In particular, all FCB-

DOS Extenda:

;d. Whethe interrupt handlers ipassed dour, to the DPMI

■ unhooked and the fumtost bo that all other |:ated.

£xn2chiixn^rlh:fxn2eh:fxn30h:fxn33h:ixil36h:Exn3eh:fxn42h:txn4Gh:fxnlfih :

u p p c r t e d D O • „ : f u n c t i o nC a r r y fl a g i n s t a c k

r a m e t o i n c i c a t eu n c t i o n f a i l e d. d a x - e r r c r cod<s fcri n v a l i d f u i e t i o n n u m b e r "u r n t o a p p l i c a t i o n

r m o n b a n d l i r g f o r e n t i r e l y- e g i B L u r - b a s a d f u n c t i o n si c t i c i 3 1 H : c h a r i n p u t - e c h oi c t i o r i 3 2 H : c h a r o u t p u t- . c t i c n fl 3 n : n u x i n p u t- .Ot icn [ !■ ' . [ ! : a u x o u t p u ta c t i o n B 5 H : p r i n t e r o u t p u tn o t i o n 0 6 H : r a w c o n s o l e I / On o t i o n 0 7 H : r a w i n p u t n o e cn o t i o n B 6 H s c h a r i n p u t n o en c t i o n O E K : i n p u t s t a t u s•-Ct io i l ODH: d i s k r e s e tn c t i o n O E H : s e l e c t d i s kn c t i o n 1 S H : g e t c u r r e n t d r iI C t i o n 1 B H : g e t c u r . d r i v e- i c t i o n I C i i : o e t d r i v e d a t an o t i o n 2 A H : t r e t d a t e- i c t i o n ? S H : s a t d a t en c t i o n 2 C H : g e t t l l t Wn c t i o n 2 D H : s e t t i m * -n o t i o n 2 E H : s e t v e r i f y fl a gn c t i o n 3 3 H : g e t D O S v e r s i o nn c t i o n 3 3 H : g e t / n e t b r e a k fn c t i o n 3 6 H : g e t d r i v e i n f on c t i o n 3 E H : c l o s e fi l en c t i o n 4 2 H : 31= okn c t i o n 4 S H : d u p h a n d l e -n c t i c r . 4 6 ! ! : r e d i r e c t h a r . d l on o t i c e 5 4 K j a w t v e r i f y t I c . cn o t i o n S 7 H s g o t / w t fi l e d c tn c t i o n 5 C H : l o c k / u n l o c kn c t i o r . 6 8 H : c o m m i t fi l e

e r a l r e c i s t e

FTiTl PC MAGAZINE MARCH 12, 1991 pcjs.org

Page 5: Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM BIOS. It must build the ... grab bag of interrupt service routines. ning. and it's

Power Programming

TINYDOSX.ASM

m o v rregDG, axr e g D X , Q

c a l lc a l l l o a d r e g s

d x . p r o t D Xm o v e s . p r o t E Si r e t

p o p b xc a l l s a v e r e g s

a x , r e a l s e gm o v r e g D S , a xm o v r e g D X , Cc a l 1m o v c x . t a q C X

de

d i . p r c t D Xm o v d s , r e n l s e ;

d x . p r o t D X

a x r r e i l s e gg D S e

r e a i dl o a d r a g e

, p rS 3 , p r o t E S

mov d s , r e a l s o l

e l d@@2: l o d s b

o r a l , a lj n z e@2

p o p d sc a l l l o a d r e g s

s i . p r o t s le s . p r o t E S

i r e t

f x n fl O h

• t r a n s f e r t o D O SI l o a d g c - n o r a l r e g i s t e r s £ f l a g s; r e t u r n t o a p p l i c a t i o n

; c o m m o n h a n d l i n g f o r f u n c t i o n sp a s s i n g A S C I T Z n d d r i r . D S : D K

1 f u n c t i o n 3 9 H : c r e a t e d i r e c t o r y■ u n c t i o n 3 A H i d e l e t e d i r e c t o r y' ; f u n c t i o n 3 B H I s e l e c t d i r e c t o r yj f u n c t i o n 3 C H : c r e a t e fi l e; f u n c t i o n 3 D ! ! : O p e n• f u n c t i o n 4 1 B 1 d e l e t e f i l eI f u n c t i o n 4 3 K : g e t / s e t a t t r i b u t e s; f u n c t i o n S A H : c r e a t e t e m p fi l e .; f u n c t i o n S B H : c r e a t e u n i q u e fi l e

\ u n l o a d g e n e r a l r e g i s t e r s; E 3 : D I - v i r t u a l a d d r e s s o f

r e a l m o d e b u f f e r' ; d s i S I - v i r t u a l a d d r e s s o f

p r o t e c t e d m o d e b u f f e r' j c o p y A S C I I S s t r i n g t o; r e a l m o d e b u f f e r; r e a c h e d n u l l y e t ?

c o p ya d d r s

t r a n s f e rload gen

i C t h aa l

t o r d a t a 3 t

a l r o g i a t e

l o d e h J

f e r

; f u n c t i o n 3 F H i r e a d fi l e; r e s t o r e B X; u n l o a d g e n e r a l r e g i s t e r s; s e t a d d r e s s o f r e a l m o d e b; i n t o r e g i a t e r d a t a s t r u c t u

' ; C X = a c t u a l l e n g t h o f d a t a■ E s : D l ■ v i r t u a l a d d r e s s

p r o t e c t e d n o d e

; D 5 : 5 I - v i r t u a l a d d r e s s o fr e a l m o d e b u f f e r

; c o p y d a t a f r o m r e a l m o d e; b u f f e r t o p r o t e c t e d m o d e b u f f e r; r e s t o r e D S — o u r D G R O U P

j l o a d g e n e r a l r e g i s t e r s; r e s t o r e p r o t e c t e d m o d e D X , E S

; r e t u r n t o a p p l i c a t i o n

; f u n c t i o n 4 e m w r i t e fi l e■ u n l o a d g e n e r a l r e g i s t e r s; E S i D I = v i r t u a l a d d r e s s o f; r e a l m o d e b u f f e r; D S i S I - v i r t u a l a d d r e s s o f

p r o t e c t e d m o d e b u f f e rI c o p y d a t a t o r e a l m o d e b u f f e r; c a t a d d r e s s o f r e a l m o d e b u f f e r• I n t o r e g i s t e r d a t a s t r u c t u r e

: t r a n s f e r t o m s - d o s\ l o a d g e n e r a l r e g i s t e r s 6 fl a g s; r e s t o r e p r o t e c t e d m o d e D X , E S

; r e t u r n t o a p p l i c a t i o n

; f u n c t i o n 4 7 H : g e t d i r e c t o r y

; u n l o a d g e n e r a l r e g i s t e r s; s e t a d d r e s s o f r e a l m o d e b u f f e r; i n t o r e g i a t e r d a t a s t r u c t u r e

; t r a n s f e r t o k s - D O S; E S i D I — v i r t u a l a d d r e s s c £

p r o t e c t e d m o d e b u f f e r

; D S : S I = v i r t u a l a d d r e s s c f' ; r e a l m o d e b u f f e r

; c o p y A S C I I Z s t r i n g f r o m r e a l; m o d e b u f f e r t o p r o t m o d e b u f f e r' ; f o u n d n u l l c h a r a c t e r y e t ?; n o , c o p y a n o t h e r c h a r a c t e r; r e s t o r e D S - o u r D G P. c U P

a l e g i si c t a d m o d e S I ,

t u r n t o a p p l i c a t i o n

POP b x - s a v e - o - u - n c o d e

nov"1 a x , G 2 fl 3 h ; r e s t o r e o l d G P f a u l t h a n d l e rb l . C d he x , w o r d p t r

m o v d x , w o r d p t r

m o v3 1 ha x , C 2 B 5 h ; r e s t o r e o l d I n t 2 1 H h a n d l e rb l , 2 1 h

- n o v e x , w o r d p t r i n t 2 1 v » 2-nov d x , w o r d p t r i n t 2 1 v

m o v3 i ha x , C 1 C 1 h ; r e l e a s e r e a l m o d e b u f f e r

m o v d x . r o a l u e lI n t 3 lb ■ c h a i n t o D P M I I n t 2 1 H h a n d li i - . t 2 1 h ; f o r c l e a n u p a n d t e r m i n a t i o n

; g e n e r a l f a l l t h i j o u g h p o i n t- ( u s e f u l d u r i n g - d e b u g g i n g )

b x ; r e s t o r e r e g i s t e r B X; c h a i n t o p r e v I n t 7 1 1 ! o w n e r

a c a l l e n -

S a v e g e n r e g i s t e rt i n s v i a

H O T u n l

i n t oD P M Ia d - 2 d

r e g A X , a xr o g B X , b x

e g C X , cr e g i x , d x

r e g D I , d ir e g 3 F , b pp r c t D X , d x

p r c t E S . e s

; L o a d g e n e r a l

p o p

: r o ( n r e a l m o d e d a t ay v l o a d e d b e c a u s e t h_ n p r o t e c t e d m o d e .

b p , s pc p u F L A G SI b p + 6 ]a x , r e g A Xb x , r e g B Xe x , r e g c xd x . r e g D Xs i , r e g s id i , r e g D Ib p , r e g E P

u p d a t e C P U fl a g e i ns t a c k f r a m e t o r e t uD O S f u n c t i o n s t a t u el o a d g e n e r a l r e g i s t

i o a d r e

; . C a l l

] S e n d p

t h e D P M a t i o n f u n c t i o n 0 3 3 0 H t o s i m u l a t e a r e a l mI B t r a n s f e r r i n g c o n t r o l t o M S - D O S , p a u s it o ' t h e r e a l m o d e r e g i s t e r s t r u c t u r e ' r e g a

D P M I F u n c t i o n 0 3 0 P Hs o f t w a r e i n t e r r u p t 2 1 Hfl a g s ( b i t B w u f e t b e ? )n o . o f s t a c k , w o r d s t o c cE S : D I = a d d r e s s o f r e a lm o d e r e g i s t e r s t r u c t u r e

o f f s e t D G R O U P sDOS D P M I

rea l dos endp

TEXT ends

blow our little programming blunders outof the water with its totally uninforma-tive "Application has violated system integrity" dialog box). INITDOSX alsoallocates a 64K area of conventional memorythat the INT 21h handler can later use topass data back and forth to MS-DOS.Finally, INITDOSX returns control to theC application, which continues its execution in protected mode.

When the C application requests anMS-DOS service, the protected-mode INT21h handler, named DOSCALL, receivescontrol. DOSCALL saves the flags and

registers, then branches through the tableDfsPATCH to the appropriate subroutine. As you'll notice, I've stubbed outmost of the less-common DOS functionsto either return an error or abort the application. The functions that TINYDOSXsupports, however, are relayed to DOSusing the DPMI translation function"Simulate Real Mode Interrupt." Use ofthis translation function, rather than thespeedier DPMI "raw mode switch" function, eliminates all sorts of messy problems that are best left to the imaginationand experiments of adventurous readers.

Naturally, DOSCALL monitors for thefateful INT 21 h. function 4Ch. and cleansup after itself accordingly.

Assuming DPMI 0.9 as our platformand linking TINYDOSX directly into theprotected-mode application allows us totake some shortcuts that would never sufficein a commercial-grade DOS extender.First, we don't have to build our ownloader for the protected-mode application; since TINYDOSX is linked into theapplication, DOS loads TINYDOSX andthe application as a single unit. Second,we don't have to allocate any memory

KEFl PC MAGAZINE MARCH 12, 1991

iWMIJpWPI.

pcjs.org

Page 6: Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM BIOS. It must build the ... grab bag of interrupt service routines. ning. and it's

Power Programming

TINYDOSX.C

T E S T D O S X . C i l l u s t r a t e s u a e o f t h e D P M I - b a s e dDOS Extender TINYDOSX to display a message in protected mode

c o p y r i g h t ( c ) 1 9 9 8 z i f f D a v i s c o m m u n i c a t i o n sPC Magazine * Ray DuncanBui ld wi th Microsof t c 6 .0 SMALL MODEL as fo l lows:

Execute under windows 3.0 in the DOS Box only I

CEEijj

p i x y ;

*/( i n c l u d e < s t d i o . h >

u n s i g n e d e x t e r n p a s c a l I n i t D o s X ( v o i d ) ;Ln()

{uns igned savecs, saveDS;

; store real mode CS; and DS fo r d i sp l ay

p r i n t f < " \ n H e l l o , r e a l m o d e w o r l d : \ t c s = % 0 4 x h D S - % 0 4 x h - ,savecs, saveDS);

i f < I n i t D o s X ( ) ) / / a t t e m p t m o d e s w i t c h<p u t s ( " \ n D P M T. i n i t i a l i z a t i o n f a i l e d . 1 ' ) ;e x i t { 1 ) ;

}; s to re p ro tec ted mode cs; and DS fo r d i sp l ay

p r i n t f ( " \ n H e l l o , p r o t e c t e d m o d e w o r l d l \ t c s « % 0 4 x h D S = % 0 4 x h \ nsavecs, saveDS);

_ a s m m o v a h , 4 c h ; e x i t d i r e c t l y t o D O S t o a v o i da s m i n t 2 1 h ; G P f a u l t i n R T L c l e a n u p c o d e

Figure 2: This is the source code for a simple protected-mode C application that can be linkedwith TINYDOSX.ASM, listed in Figure 1.

for the application or build any descriptors; we get these services "free" whenDPMI creates code and data selectors duringthe initial switch to protected mode. Third,we need to support only those DOS services in our DOS extender that we knowour application is actually going to use;we don't have the obligation to translateevery known (and unknown) DOS function for protected mode the way a "real"DOS extender does.

Suppose we wanted to turn TINYDOSXinto a not-so-tiny, more robust DOSextender—where would we start? We'dhave to enlarge the support for INT 21hfunctions to include (at minimum) all thedocumented MS-DOS services. We'd needto support the immense battery of ROMBIOS services (most of which, luckily,are register-based anyway) and probably,in addition, the Microsoft Mouse INT33h and the NETBIOS interfaces. We'dwant to add more sophisticated facilitiesfor installation of interrupt handlers bythe application. And last but not least,we'd be obligated to support most of thedifferent C memory models, a somewhatthorny chore. My first impression of theway to do this would be to incorporate aloader for "segmented .EXE" (also called

"new .EXE") files into our DOS extender, build the application with the Microsoft Segmented Linker, and bind our DOSextender into the .EXE file as the "real-mode stub.''

TRYING OUT TINYDOSXTESTDOSX.C, a simple protected-modeC application that can be linked withTINYDOSX.ASM, is shown in Figure 2.To create the executable version of TEST-DOSX, enter the following commands:

MASM /Mx TINYDOSX;CL TESTDOSX.C TINYDOSX

Be sure that you are using Microsoft 6.0and compiling for the small model. Theresulting application, TESTDOSX.EXE,must be run in one of Windows 3.0'sDOS boxes so that it has access to DPMIservices.

THE IN-BOXPlease send your questions, comments,and suggestions to me at any of the following e-mail addresses:PCMagNet: 72241,52MCI Mail: rduncanB I X : r d u n c a n B

MARCH 12. 1991 PC MAGAZINE EETB

Computers, FCCClass Af Class Br

and You —or When is it

better to get aB than an A?

You need to know the differencebetween computers that meet theFCC class B radio frequency emissions standards and those that meetonly the Class A standards.Computers emit radio signals in theiroperation. Because these signalsmay cause interference to radio andtelevision reception, the marketingand the use of computers is regulated by the Federal CommunicationsCommission. Under federal rules,computer users are responsible forremedying interference, includinginterference in neighboring homes.Computers certified by the FCC asmeeting the Class B standard areless likely to cause interference toradio and TV reception than thosethat have been verified by the manufacturer or importer to the Class Astandards. Only Class B certifiedcomputers may be advertised, sold,or leased tor use in residences. Asimilar regulatory program applies inCanada.Buyers seeking computers for use inhomes (including offices at home)should shop for computers andperipherals which have been Class Bcertified. These devices carry a labelwith an FCC ll) number Both newand tised diss A verified devicesmay be sold only for use in commercial and industrial locations.Signals from computers are morelikely to be masked by electricalnoise from other equipment in suchan environment. These areas arealso likely to have fewer radios andTVs. Accordingly, equipment marketed only for use in these locationsmay meet the less rigorous Class Astandard. Class B certified equipment may be marketed for tise inresidences as well as commercialand industrial locations.As you shop for a computer tor usein your home, look for the FCC classification in the specifications or askyour vendor to recommend only machines that have been certified to theClass B limits. TV viewers and radiolisteners in your home and in neighboring homes will be glad you did.

!;:

pcjs.org

Page 7: Power Programming - PCjsarchive.pcjs.org/pubs/pc/magazines/pcmag/PCMAG-1991-03/PCMAG-1991...the ROM BIOS. It must build the ... grab bag of interrupt service routines. ning. and it's

^ofv

K t - m ^ W^

fee LCO

5'ub dS'j'D^O

]/to\J ^-i,/J.e^-x-t c*Sijt-^e—&*—

ShA>zi±\

eh.jp.

Vo&C

(6^jo; /"<Jf"j m7^(jy.} ^a

\ fW ^ i - - * ]

pcjs.org


Recommended