1 ;****************************************************************************
3 ;* SciTech OS Portability Manager Library
5 ;* ========================================================================
7 ;* The contents of this file are subject to the SciTech MGL Public
8 ;* License Version 1.0 (the "License"); you may not use this file
9 ;* except in compliance with the License. You may obtain a copy of
10 ;* the License at http://www.scitechsoft.com/mgl-license.txt
12 ;* Software distributed under the License is distributed on an
13 ;* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 ;* implied. See the License for the specific language governing
15 ;* rights and limitations under the License.
17 ;* The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
19 ;* The Initial Developer of the Original Code is SciTech Software, Inc.
20 ;* All Rights Reserved.
22 ;* ========================================================================
24 ;* Language: 80386 Assembler, TASM 4.0 or NASM
25 ;* Environment: IBM PC Real mode and 16/32 bit protected mode
27 ;* Description: Low level assembly support for the PM library specific to
30 ;****************************************************************************
34 include "scitech.mac" ; Memory model macros
36 header _pmdos ; Set up memory model
58 RMREGS = (rmregs_s PTR es:bx)
66 RMSREGS = (rmsregs_s PTR es:bx)
71 cextern _PM_savedDS,USHORT
72 cextern _PM_VXD_off,UINT
73 cextern _PM_VXD_sel,UINT
75 cextern _PM_haveCauseWay,UINT
78 intel_id db "GenuineIntel" ; Intel vendor ID
80 PMHELP_GETPDB EQU 0026h
81 PMHELP_FLUSHTLB EQU 0027h
87 begcodeseg _pmdos ; Start of code segment
91 ;----------------------------------------------------------------------------
92 ; void PM_callRealMode(unsigned s,unsigned o, RMREGS *regs,
94 ;----------------------------------------------------------------------------
95 ; Calls a real mode procedure, loading the appropriate registers values
96 ; from the passed in structures. Only the DS and ES register are loaded
97 ; from the SREGS structure.
98 ;----------------------------------------------------------------------------
99 cprocstart PM_callRealMode
101 ARG s:WORD, o:WORD, regs:DWORD, sregs:DWORD
103 LOCAL addr:DWORD, bxVal:WORD, esVal:WORD, flags:WORD = LocalSize
109 mov ax,[o] ; Build the address to call in 'addr'
116 mov ds,ax ; DS := passed in value
122 mov ax,[RMREGS.ax] ; AX := passed in value
123 mov cx,[RMREGS.cx] ; CX := passed in value
124 mov dx,[RMREGS.dx] ; DX := passed in value
125 mov si,[RMREGS.si] ; SI := passed in value
126 mov di,[RMREGS.di] ; DI := passed in value
129 pop es ; ES := passed in value
130 mov bx,[bxVal] ; BX := passed in value
132 call [addr] ; Call the specified routine
134 pushf ; Save flags for later
144 pop [RMSREGS.ds] ; Save value of DS
146 pop [RMSREGS.es] ; Save value of ES
148 mov [RMREGS.ax],ax ; Save value of AX
149 mov [RMREGS.cx],cx ; Save value of CX
150 mov [RMREGS.dx],dx ; Save value of DX
151 mov [RMREGS.si],si ; Save value of SI
152 mov [RMREGS.di],di ; Save value of DI
153 mov ax,[flags] ; Return flags
154 and ax,1h ; Isolate carry flag
155 mov [RMREGS.cflag],ax ; Save carry flag status
157 mov [RMREGS.bx],ax ; Save value of BX
168 ;----------------------------------------------------------------------------
169 ; void PM_segread(PMSREGS *sregs)
170 ;----------------------------------------------------------------------------
171 ; Read the current value of all segment registers
172 ;----------------------------------------------------------------------------
173 cprocstartdll16 PM_segread
193 ; Create a table of the 256 different interrupt calls that we can jump
204 %assign intno intno + 1
224 ;----------------------------------------------------------------------------
225 ; _PM_genInt - Generate the appropriate interrupt
226 ;----------------------------------------------------------------------------
232 mov ebx,[UINT esp+12] ; EBX := interrupt number
234 mov bx,sp ; Make sure ESP is zeroed
235 mov bx,[UINT ss:bx+6] ; BX := interrupt number
237 mov _ax,offset intTable ; Point to interrupt generation table
238 shl _bx,2 ; _BX := index into table
239 add _ax,_bx ; _AX := pointer to interrupt code
241 xchg eax,[esp+4] ; Restore eax, and set for int
244 xchg ax,[ss:bx+2] ; Restore ax, and set for int
246 pop _bx ; restore _bx
251 ;----------------------------------------------------------------------------
252 ; int PM_int386x(int intno, PMREGS *in, PMREGS *out,PMSREGS *sregs)
253 ;----------------------------------------------------------------------------
254 ; Issues a software interrupt in protected mode. This routine has been
255 ; written to allow user programs to load CS and DS with different values
256 ; other than the default.
257 ;----------------------------------------------------------------------------
258 cprocstartdll16 PM_int386x
260 ARG intno:UINT, inptr:DPTR, outptr:DPTR, sregs:DPTR
262 LOCAL flags:UINT, sv_ds:UINT, sv_esi:ULONG = LocalSize
266 push es ; Save segment registers
270 _lds _si,[sregs] ; DS:_SI -> Load segment registers
273 mov [sv_ds],_bx ; Save value of user DS on stack
277 _lds _si,[inptr] ; Load CPU registers
285 push ds ; Save value of DS
286 push _bp ; Some interrupts trash this!
287 clc ; Generate the interrupt
289 mov ds,[WORD sv_ds] ; Set value of user's DS selector
291 pop _bp ; Pop intno from stack (flags unchanged)
292 pop _bp ; Restore value of stack frame pointer
293 pop ds ; Restore value of DS
295 pushf ; Save flags for later
297 push esi ; Save ESI for later
299 push ds ; Save DS for later
302 _lds _si,[outptr] ; Save CPU registers
311 mov _bx,[flags] ; Return flags
312 and ebx,1h ; Isolate carry flag
313 mov [_si+24],ebx ; Save carry flag status
315 _lds _si,[sregs] ; Save segment registers
318 mov [_si+6],bx ; Get returned DS from stack
322 pop gs ; Restore segment registers
332 _PM_savedDS dw _DATA ; Saved value of DS
335 ;----------------------------------------------------------------------------
336 ; void PM_saveDS(void)
337 ;----------------------------------------------------------------------------
338 ; Save the value of DS into a section of the code segment, so that we can
339 ; quickly load this value at a later date in the PM_loadDS() routine from
340 ; inside interrupt handlers etc. The method to do this is different
341 ; depending on the DOS extender being used.
342 ;----------------------------------------------------------------------------
343 cprocstartdll16 PM_saveDS
346 mov [_PM_savedDS],ds ; Store away in data segment
352 ;----------------------------------------------------------------------------
353 ; void PM_loadDS(void)
354 ;----------------------------------------------------------------------------
355 ; Routine to load the DS register with the default value for the current
356 ; DOS extender. Only the DS register is loaded, not the ES register, so
357 ; if you wish to call C code, you will need to also load the ES register
358 ; in 32 bit protected mode.
359 ;----------------------------------------------------------------------------
360 cprocstartdll16 PM_loadDS
362 mov ds,[cs:_PM_savedDS] ; We can access the proper DS through CS
369 ;----------------------------------------------------------------------------
370 ; ibool DPMI_allocateCallback(void (*pmcode)(), void *rmregs, long *RMCB)
371 ;----------------------------------------------------------------------------
372 cprocstart _DPMI_allocateCallback
374 ARG pmcode:CPTR, rmregs:DPTR, RMCB:DPTR
382 mov esi,[pmcode] ; DS:ESI -> protected mode code to call
383 mov edi,[rmregs] ; ES:EDI -> real mode register buffer
384 mov ax,303h ; AX := allocate realmode callback function
386 mov eax,0 ; Return failure!
392 mov [es:eax],ecx ; Return real mode address
393 mov eax,1 ; Return success!
402 ;----------------------------------------------------------------------------
403 ; void DPMI_freeCallback(long RMCB)
404 ;----------------------------------------------------------------------------
405 cprocstart _DPMI_freeCallback
412 mov dx,[WORD RMCB] ; CX:DX := real mode callback
423 ; Macro to delay briefly to ensure that enough time has elapsed between
424 ; successive I/O accesses so that the device being accessed can respond
425 ; to both accesses even on a very fast PC.
451 ;----------------------------------------------------------------------------
452 ; uchar _PM_readCMOS(int index)
453 ;----------------------------------------------------------------------------
454 ; Read the value of a specific CMOS register. We do this with both
455 ; normal interrupts and NMI disabled.
456 ;----------------------------------------------------------------------------
457 cprocstart _PM_readCMOS
465 or al,80h ; Add disable NMI flag
473 out 70h,al ; Re-enable NMI
475 mov al,ah ; Return value in AL
482 ;----------------------------------------------------------------------------
483 ; void _PM_writeCMOS(int index,uchar value)
484 ;----------------------------------------------------------------------------
485 ; Read the value of a specific CMOS register. We do this with both
486 ; normal interrupts and NMI disabled.
487 ;----------------------------------------------------------------------------
488 cprocstart _PM_writeCMOS
490 ARG index:UINT, value:UCHAR
496 or al,80h ; Add disable NMI flag
504 out 70h,al ; Re-enable NMI
514 ;----------------------------------------------------------------------------
515 ; int _PM_pagingEnabled(void)
516 ;----------------------------------------------------------------------------
517 ; Returns 1 if paging is enabled, 0 if not or -1 if not at ring 0
518 ;----------------------------------------------------------------------------
519 cprocstart _PM_pagingEnabled
526 cmp [UINT _PM_haveCauseWay],0
531 mov eax,cr0 ; Load CR0
532 shr eax,31 ; Isolate paging enabled bit
538 ;----------------------------------------------------------------------------
539 ; _PM_getPDB - Return the Page Table Directory Base address
540 ;----------------------------------------------------------------------------
541 cprocstart _PM_getPDB
547 cmp [UINT _PM_haveCauseWay],0
551 ; Call VxD if running at ring 3 in a DOS box
553 cmp [WORD _PM_VXD_sel],0
555 mov eax,PMHELP_GETPDB
557 call far dword [_PM_VXD_off]
559 call [FCPTR _PM_VXD_off]
574 ;----------------------------------------------------------------------------
575 ; PM_flushTLB - Flush the Translation Lookaside buffer
576 ;----------------------------------------------------------------------------
577 cprocstart PM_flushTLB
583 cmp [UINT _PM_haveCauseWay],0
587 ; Call VxD if running at ring 3 in a DOS box
589 cmp [WORD _PM_VXD_sel],0
591 mov eax,PMHELP_FLUSHTLB
593 call far dword [_PM_VXD_off]
595 call [FCPTR _PM_VXD_off]
601 wbinvd ; Flush the CPU cache
603 mov cr3,eax ; Flush the TLB
611 ;----------------------------------------------------------------------------
612 ; void _PM_VxDCall(VXD_regs far *r,uint off,uint sel);
613 ;----------------------------------------------------------------------------
614 cprocstart _PM_VxDCall
616 ARG r:DPTR, off:UINT, sel:UINT
620 ; Load all registers from the registers structure
628 mov ebx,[ebx+4] ; Trashes BX structure pointer!
630 ; Call the VxD entry point (on stack)
638 ; Save all registers back in the structure
640 push ebx ; Push EBX onto stack for later
647 pop [DWORD ebx+4] ; Save value of EBX from stack