GO32 - acces to the 32-bit DOS extender

This document describes the GO32 unit for the Free Pascal compiler under dos. It was donated by Thomas Schatzl (tom_at_work@geocities.com), for which my thanks. This unit was first written for dos by Florian Klaempfl.

Only the GO32V2 DPMI mode is discussed by me here due to the fact that new applications shouldn't be created with the older GO32V1 model. The go32v2 version is much more advanced and better. Additionally a lot of functions only work in DPMI mode anyway. I hope the following explanations and introductions aren't too confusing at all. If you notice an error or bug send it to the FPC mailing list or directly to me. So let's get started and happy and error free coding I wish you.... Thomas Schatzl, 25. August 1998

What is DPMI The dos Protected Mode Interface helps you with various aspects of protected mode programming. These are roughly divided into descriptor handling, access to dos memory, management of interrupts and exceptions, calls to real mode functions and other stuff. Additionally it automatically provides swapping to disk for memory intensive applications. A DPMI host (either a Windows dos box or CWSDPMI.EXE) provides these functions for your programs. Selectors and descriptors Descriptors are a bit like real mode segments; they describe (as the name implies) a memory area in protected mode. A descriptor contains information about segment length, its base address and the attributes of it (i.e. type, access rights, ...). These descriptors are stored internally in a so-called descriptor table, which is basically an array of such descriptors. Selectors are roughly an index into this table. Because these 'segments' can be up to 4 GB in size, 32 bits aren't sufficient anymore to describe a single memory location like in real mode. 48 bits are now needed to do this, a 32 bit address and a 16 bit sized selector. The GO32 unit provides the tseginfo record to store such a pointer. But due to the fact that most of the time data is stored and accessed in the %ds selector, FPC assumes that all pointers point to a memory location of this selector. So a single pointer is still only 32 bits in size. This value represents the offset from the data segment base address to this memory location. FPC specialities

The %ds and %es selector MUST always contain the same value or some system routines may crash when called. The %fs selector is preloaded with the DOSMEMSELECTOR variable at startup, and it MUST be restored after use, because again FPC relys on this for some functions. Luckily we asm programmers can still use the %gs selector for our own purposes, but for how long ?

dos memory access Dos memory is accessed by the predefined dosmemselector selector; the GO32 unit additionally provides some functions to help you with standard tasks, like copying memory from heap to dos memory and the likes. Because of this it is strongly recommened to use them, but you are still free to use the provided standard memory accessing functions which use 48 bit pointers. The third, but only thought for compatibility purposes, is using the mem[]-arrays. These arrays map the whole 1 Mb dos space. They shouldn't be used within new programs. To convert a segment:offset real mode address to a protected mode linear address you have to multiply the segment by 16 and add its offset. This linear address can be used in combination with the DOSMEMSELECTOR variable. I/O port access The I/O port access is done via the various , functions which are available. Additionally Free Pascal supports the Turbo Pascal PORT[]-arrays but it is by no means recommened to use them, because they're only for compatibility purposes. Processor access These are some functions to access various segment registers (%cs, %ds, %ss) which makes your work a bit easier. Interrupt redirection Interrupts are program interruption requests, which in one or another way get to the processor; there's a distinction between software and hardware interrupts. The former are explicitely called by an 'int' instruction and are a bit comparable to normal functions. Hardware interrupts come from external devices like the keyboard or mouse. Functions that handle hardware interrupts are called handlers. Handling interrupts with DPMI The interrupt functions are real-mode procedures; they normally can't be called in protected mode without the risk of an protection fault. So the DPMI host creates an interrupt descriptor table for the application. Initially all software interrupts (except for int 31h, 2Fh and 21h function 4Ch) or external hardware interrupts are simply directed to a handler that reflects the interrupt in real-mode, i.e. the DPMI host's default handlers switch the CPU to real-mode, issue the interrupt and switch back to protected mode. The contents of general registers and flags are passed to the real mode handler and the modified registers and flags are returned to the protected mode handler. Segment registers and stack pointer are not passed between modes. Protected mode interrupts vs. Real mode interrupts As mentioned before, there's a distinction between real mode interrupts and protected mode interrupts; the latter are protected mode programs, while the former must be real mode programs. To call a protected mode interrupt handler, an assembly 'int' call must be issued, while the other is called via the realintr() or intr() function. Consequently, a real mode interrupt then must either reside in dos memory (<1MB) or the application must allocate a real mode callback address via the get_rm_callback() function. Creating your own interrupt handlers Interrupt redirection with FPC pascal is done via the set_pm_interrupt() for protected mode interrupts or via the set_rm_interrupt() for real mode interrupts. Disabling interrupts The GO32 unit provides the two procedures disable() and enable() to disable and enable all interrupts. Hardware interrupts Hardware interrupts are generated by hardware devices when something unusual happens; this could be a keypress or a mouse move or any other action. This is done to minimize CPU time, else the CPU would have to check all installed hardware for data in a big loop (this method is called 'polling') and this would take much time. A standard IBM-PC has two interrupt controllers, that are responsible for these hardware interrupts: both allow up to 8 different interrupt sources (IRQs, interrupt requests). The second controller is connected to the first through IRQ 2 for compatibility reasons, e.g. if controller 1 gets an IRQ 2, he hands the IRQ over to controller 2. Because of this up to 15 different hardware interrupt sources can be handled. IRQ 0 through IRQ 7 are mapped to interrupts 8h to Fh and the second controller (IRQ 8 to 15) is mapped to interrupt 70h to 77h. All of the code and data touched by these handlers MUST be locked (via the various locking functions) to avoid page faults at interrupt time. Because hardware interrupts are called (as in real mode) with interrupts disabled, the handler has to enable them before it returns to normal program execution. Additionally a hardware interrupt must send an EOI (end of interrupt) command to the responsible controller; this is acomplished by sending the value 20h to port 20h (for the first controller) or A0h (for the second controller). The following example shows how to redirect the keyboard interrupt. Software interrupts

Ordinarily, a handler installed with only services software interrupts that are executed in protected mode; real mode software interrupts can be redirected by .

Executing software interrupts Simply execute a realintr() call with the desired interrupt number and the supplied register data structure. But some of these interrupts require you to supply them a pointer to a buffer where they can store data to or obtain data from in memory. These interrupts are real mode functions and so they only can access the first Mb of linear address space, not FPC's data segment. For this reason FPC supplies a pre-initialized dos memory location within the GO32 unit. This buffer is internally used for dos functions too and so it's contents may change when calling other procedures. It's size can be obtained with and it's linear address via . Another way is to allocate a completely new dos memory area via the function for your use and supply its real mode address. Real mode callbacks

The callback mechanism can be thought of as the converse of calling a real mode procedure (i.e. interrupt), which allows your program to pass information to a real mode program, or obtain services from it in a manner that's transparent to the real mode program. In order to make a real mode callback available, you must first get the real mode callback address of your procedure and the selector and offset of a register data structure. This real mode callback address (this is a segment:offset address) can be passed to a real mode program via a software interrupt, a dos memory block or any other convenient mechanism. When the real mode program calls the callback (via a far call), the DPMI host saves the registers contents in the supplied register data structure, switches into protected mode, and enters the callback routine with the following settings:

  • interrupts disabled
  • %CS:%EIP = 48 bit pointer specified in the original call to
  • %DS:%ESI = 48 bit pointer to to real mode SS:SP
  • %ES:%EDI = 48 bit pointer of real mode register data structure.
  • %SS:%ESP = locked protected mode stack
  • All other registers undefined

The callback procedure can then extract its parameters from the real mode register data structure and/or copy parameters from the real mode stack to the protected mode stack. Recall that the segment register fields of the real mode register data structure contain segment or paragraph addresses that are not valid in protected mode. Far pointers passed in the real mode register data structure must be translated to virtual addresses before they can be used with a protected mode program. The callback procedure exits by executing an IRET with the address of the real mode register data structure in %ES:%EDI, passing information back to the real mode caller by modifying the contents of the real mode register data structure and/or manipulating the contents of the real mode stack. The callback procedure is responsible for setting the proper address for resumption of real mode execution into the real mode register data structure; typically, this is accomplished by extracting the return address from the real mode stack and placing it into the %CS:%EIP fields of the real mode register data structure. After the IRET, the DPMI host switches the CPU back into real mode, loads ALL registers with the contents of the real mode register data structure, and finally returns control to the real mode program. All variables and code touched by the callback procedure MUST be locked to prevent page faults.

return value: Unknown runmode return value: raw (without HIMEM) return value: XMS (with HIMEM, without EMM386) return value: VCPI (with HIMEM and EMM386) return value: DPMI (e.g. dos box or 386Max) Check for carry flag in Check for parity flag in Check for auxiliary carry flag in Check for zero flag in Check for sign flag in Check for trap flag in Check for interrupt flag in Check for direction flag in Check for overflow flag in Memory information record

tmeminfo Holds information about the memory allocation, etc.

NOTE: The value of a field is -1 (0ffffffffh) if the value is unknown, it's only guaranteed, that available_memory contains a valid value. The size of the pages can be determined by the get_page_size() function.

Largest available free block in bytes. Maximum unlocked page allocation in pages Maximum locked page allocation in pages. Linear address space size in pages. Total number of unlocked pages. Total number of free pages. Total number of physical pages. Free linear address space in pages. Size of paging file/partition in pages Record describing all processor registers The trealregs type contains the data structure to pass register values to a interrupt handler or real mode callback. Alias for Record to store 48-bits pointer

This record is used to store a full 48-bit pointer. This may be either a protected mode selector:offset address or in real mode a segment:offset address, depending on application.

See also: Selectors and descriptors, dos memory access, Interrupt redirection

Offset in segment Segment Selector to DOS memory Selector to the dos memory. The whole dos memory is automatically mapped to this single descriptor at startup. This selector is the recommened way to access dos memory. DPMI interrupt call result This variable holds the result of a DPMI interrupt call. Any nonzero value must be treated as a critical failure. Allocate a number of descriptors

Allocates a number of new descriptors.

Parameters:

count:\
specifies the number of requested unique descriptors.

Return value: The base selector.

Notes: The descriptors allocated must be initialized by the application with other function calls. This function returns descriptors with a limit and size value set to zero. If more than one descriptor was requested, the function returns a base selector referencing the first of a contiguous array of descriptors. The selector values for subsequent descriptors in the array can be calculated by adding the value returned by the function.
Check the variable.
Allocate a block of linear memory

Allocates a block of linear memory.

Parameters:

size:
Size of requested linear memory block in bytes.

Returned values: blockhandle - the memory handle to this memory block. Linear address of the requested memory.

warning According to my DPMI docs this function is not implemented correctly. Normally you should also get a blockhandle to this block after successful operation. This handle can then be used to free the memory block afterwards or use this handle for other purposes. Since the function isn't implemented correctly, and doesn't return a blockhandle, the block can't be deallocated and is hence unusuable ! This function doesn't allocate any descriptors for this block, it's the applications resposibility to allocate and initialize for accessing this memory.
Check the variable.
Copy data from DOS to to heap

Copies data from the pre-allocated dos memory transfer buffer to the heap.

Parameters:

addr
data to copy to.
len
number of bytes to copy to heap.

Notes: Can only be used in conjunction with the dos memory transfer buffer.

Check the variable.
Copy data from heap to DOS memory

Copies data from heap to the pre-allocated dos memory buffer.

Parameters:

addr
data to copy from.
len
number of bytes to copy to dos memory buffer.

Notes: This function fails if you try to copy more bytes than the transfer buffer is in size. It can only be used in conjunction with the transfer buffer.

Check the variable.
Create new descriptor from existing descriptor

Creates a new descriptor that has the same base and limit as the specified descriptor.

Parameters:

seg
Descriptor.

Return values: The data selector (alias).

Notes: In effect, the function returns a copy of the descriptor. The descriptor alias returned by this function will not track changes to the original descriptor. In other words, if an alias is created with this function, and the base or limit of the original segment is then changed, the two descriptors will no longer map the same memory.

Check the variable.
Disable hardware interrupts

Disables all hardware interrupts by execution a CLI instruction.

None.
Fill a region of DOS memory with a specific byte-sized value

Sets a region of dos memory to a specific byte value.

Parameters:

seg
real mode segment.
ofs
real mode offset.
count
number of bytes to set.
c
value to set memory to.

Notes: No range check is performed.

None.
Fill a region of DOS memory with a specific word-sized value

Sets a region of dos memory to a specific word value.

Parameters:

seg
real mode segment.
ofs
real mode offset.
count
number of words to set.
w
value to set memory to.

Notes: No range check is performed.

None.
Copy data from DOS memory to the heap.

Copies data from the dos memory onto the heap.

Parameters:

seg
source real mode segment.
ofs
source real mode offset.
data
destination.
count
number of bytes to copy.

Notes: No range checking is performed.

For an example, see .

None.
Move data between 2 DOS real mode memory locations

Copies count bytes of data between two dos real mode memory locations.

Parameters:

sseg
source real mode segment.
sofs
source real mode offset.
dseg
destination real mode segment.
dofs
destination real mode offset.
count
number of bytes to copy.

Notes: No range check is performed in any way.

None. , ,
For an example, see . Copy data from the heap to DOS real mode memory

Copies heap data to dos real mode memory.

Parameters:

seg
destination real mode segment.
ofs
destination real mode offset.
data
source.
count
number of bytes to copy.

Notes: No range checking is performed.

For an example, see .

None. ,
Enable hardware interrupts Enables all hardware interrupts by executing a STI instruction. None. Free a descriptor

Frees a previously allocated descriptor.

Parameters:

des
The descriptor to be freed.

Return value: True if successful, False otherwise. Notes: After this call this selector is invalid and must not be used for any memory operations anymore. Each descriptor allocated with must be freed individually with this function, even if it was previously allocated as a part of a contiguous array of descriptors.

For an example, see .

Check the variable.
Free allocated memory block

Frees a previously allocated memory block.

Parameters:

blockhandle
the handle to the memory area to free.

Return value: True if successful, false otherwise. Notes: Frees memory that was previously allocated with . This function doesn't free any descriptors mapped to this block, it's the application's responsibility.

Check variable.
Release real mode callback.

Releases a real mode callback address that was previously allocated with the function.

Parameters:

intaddr
real mode address buffer returned by .

Return values: True if successful, False if not

For an example, see .

Check the variable.
Get CS selector

Returns the cs selector.

Return value: The content of the cs segment register.

For an example, see .

None.
Get descriptor's access rights

Gets the access rights of a descriptor.

Parameters:

d
selector to descriptor.

Return value: Access rights bit field.

Check the variable.
Get DS Selector

Returns the ds selector.

Return values: The content of the ds segment register.

None.
Convert physical to linear address

Converts a physical address into a linear address.

Parameters:

phys_addr
physical address of device.
size
Size of region to map in bytes.

Return value: Linear address that can be used to access the physical memory. Notes: It's the applications resposibility to allocate and set up a descriptor for access to the memory. This function shouldn't be used to map real mode addresses.

Check the variable.
Return information on the available memory

Returns information about the amount of available physical memory, linear address space, and disk space for page swapping.

Parameters:

meminfo
buffer to fill memory information into.

Return values: Due to an implementation bug this function always returns False, but it always succeeds.

Notes: Only the first field of the returned structure is guaranteed to contain a valid value. Any fields that are not supported by the DPMI host will be set by the host to -1 (0FFFFFFFFH) to indicate that the information is not available. The size of the pages used by the DPMI host can be obtained with the function.
Check the variable.
Return selector increment value

Returns the selector increment value when allocating multiple subsequent descriptors via .

Return value: Selector increment value.

Notes: Because only returns the selector for the first descriptor and so the value returned by this function can be used to calculate the selectors for subsequent descriptors in the array.
Check the variable.
Return the page size

Returns the size of a single memory page.

Return value: Size of a single page in bytes.

The returned size is typically 4096 bytes.

For an example, see .

Check the variable.
Return protected mode interrupt handler

Returns the address of a current protected mode interrupt handler.

Parameters:

vector
interrupt handler number you want the address to.
intaddr
buffer to store address.

Return values: True if successful, False if not.

The returned address is a protected mode selector:offset address.

For an example, see .

Check the variable.
Return real mode callback

Returns a unique real mode segment:offset address, known as a "real mode callback," that will transfer control from real mode to a protected mode procedure.

Parameters:

pm_func
pointer to the protected mode callback function.
reg
supplied registers structure.
rmcb
buffer to real mode address of callback function.

Return values: True if successful, otherwise False.

Callback addresses obtained with this function can be passed by a protected mode program for example to an interrupt handler, device driver, or TSR, so that the real mode program can call procedures within the protected mode program or notify the protected mode program of an event. The contents of the supplied regs structure is not valid after function call, but only at the time of the actual callback.
Check the variable.
Get real mode interrupt vector

Returns the contents of the current machine's real mode interrupt vector for the specified interrupt.

Parameters:

vector
interrupt vector number.
intaddr
buffer to store real mode segment:offset address.

Return values: True if successful, False otherwise.

The returned address is a real mode segment address, which isn't valid in protected mode.
Check the variable.
Return current run mode

Returns the current mode your application runs with.

Return values: One of the constants used by this function.

None. constants returned by
Return base address from descriptor table

Returns the 32-bit linear base address from the descriptor table for the specified segment.

Parameters:

d
selector of the descriptor you want the base address of.

Return values: Linear base address of specified descriptor.

For an example, see .

Check the variable.
Return segment limite from descriptor

Returns a descriptors segment limit.

Parameters:

d
selector.

Return value: Limit of the descriptor in bytes.

Returns zero if descriptor is invalid.
Return SS selector

Returns the ss selector.

Return values: The content of the ss segment register.

None.
Allocate DOS real mode memory

Allocates a block of dos real mode memory.

Parameters:

bytes
size of requested real mode memory.

Return values: The low word of the returned value contains the selector to the allocated dos memory block, the high word the corresponding real mode segment value. The offset value is always zero. This function allocates memory from dos memory pool, i.e. memory below the 1 MB boundary that is controlled by dos. Such memory blocks are typically used to exchange data with real mode programs, TSRs, or device drivers. The function returns both the real mode segment base address of the block and one descriptor that can be used by protected mode applications to access the block. This function should only used for temporary buffers to get real mode information (e.g. interrupts that need a data structure in ES:(E)DI), because every single block needs an unique selector. The returned selector should only be freed by a call.

Check the variable.
Free DOS memory block

Frees a previously allocated dos memory block.

Parameters:

selector
selector to the dos memory block.

Return value: True if successful, False otherwise.

The descriptor allocated for the memory block is automatically freed and hence invalid for further use. This function should only be used for memory allocated by .

For an example, see .

Check the variable.
Read byte from I/O port

Reads 1 byte from the selected I/O port.

Parameters:

port
the I/O port number which is read.

Return values: Current I/O port value.

None.
Read longint from I/O port

Reads 1 longint from the selected I/O port.

Parameters:

port
the I/O port number which is read.

Return values: Current I/O port value.

None.
Read word from I/O port

Reads 1 word from the selected I/O port.

Parameters:

port
the I/O port number which is read.

Return values: Current I/O port value.

None.
Lock code memory range

Locks a memory range which is in the code segment selector.

Parameters:

functionaddr
address of the function to be locked.
size
size in bytes to be locked.

Return values: True if successful, False otherwise.

For an example, see .

Check the variable.
Lock data memory range

Locks a memory range which resides in the data segment selector.

Parameters:

data
address of data to be locked.
size
length of data to be locked.

Return values: True if successful, False otherwise.

For an example, see .

Check the variable.
Lock linear memory region

Locks a memory region to prevent swapping of it.

Parameters:

linearaddr
the linear address of the memory are to be locked.
size
size in bytes to be locked.

Return value: True if successful, False otherwise.

Check the variable.
Write byte to I/O port

Sends 1 byte of data to the specified I/O port.

Parameters:

port
the I/O port number to send data to.
data
value sent to I/O port.

Return values: None.

None.
Write longint to I/O port

Sends 1 longint of data to the specified I/O port.

Parameters:

port
the I/O port number to send data to.
data
value sent to I/O port.

Return values: None.

For an example, see .

None.
Write word to I/O port

Sends 1 word of data to the specified I/O port.

Parameters:

port
the I/O port number to send data to.
data
value sent to I/O port.

Return values: None.

For an example, see .

None.
Simulate interrupt

Simulates an interrupt in real mode.

Parameters:

intnr
interrupt number to issue in real mode.
regs
registers data structure.

Return values: The supplied registers data structure contains the values that were returned by the real mode interrupt. True if successful, False if not.

The function transfers control to the address specified by the real mode interrupt vector of intnr. The real mode handler must return by executing an IRET.
Check the variable.
Fill segment with byte value

Sets a memory area to a specific value.

Parameters:

seg
selector to memory area.
ofs
offset to memory.
count
number of bytes to set.
c
byte data which is set.

Return values: None.

Notes: No range check is done in any way.

None.
Fill segment with word value

Sets a memory area to a specific value.

Parameters:

seg
selector to memory area.
ofs
offset to memory.
count
number of words to set.
w
word data which is set.

Return values: None.

Notes: No range check is done in any way.

For an example, see .

None.
Map segment address to descriptor

Maps a real mode segment (paragraph) address onto an descriptor that can be used by a protected mode program to access the same memory.

Parameters:

seg
the real mode segment you want the descriptor to.

Return values: Descriptor to real mode segment address.

The returned descriptors limit will be set to 64 kB. Multiple calls to this function with the same segment address will return the same selector. Descriptors created by this function can never be modified or freed. Programs which need to examine various real mode addresses using the same selector should use the function and change the base address as necessary.

For an example, see .

Check the variable.
Move data between 2 locations

Copies data between two memory locations.

Parameters:

sseg
source selector.
source
source offset.
dseg
destination selector.
dest
destination offset.
count
size in bytes to copy.

Return values: None.

Overlapping is only checked if the source selector is equal to the destination selector. No range check is done.

For an example, see .

None.
Set descriptor access rights

Sets the access rights of a descriptor.

Parameters:

d
selector.
w
new descriptor access rights.
Check the variable.
Set protected mode interrupt handler

Sets the address of the protected mode handler for an interrupt.

Parameters:

vector
number of protected mode interrupt to set.
intaddr
selector:offset address to the interrupt vector.

Return values: True if successful, False otherwise.

The address supplied must be a valid selector:offset protected mode address.
Check the variable.
Set real mode interrupt handler

Sets a real mode interrupt handler.

Parameters:

vector
the interrupt vector number to set.
intaddr
address of new interrupt vector.

Return values: True if successful, otherwise False.

The address supplied MUST be a real mode segment address, not a selector:offset address. So the interrupt handler must either reside in dos memory (below 1 Mb boundary) or the application must allocate a real mode callback address with .
Check the variable.
Set descriptor's base address

Sets the 32-bit linear base address of a descriptor.

Parameters:

d
selector.
s
new base address of the descriptor.
Check the variable.
Set descriptor limit

Sets the limit of a descriptor.

Parameters:

d
selector.
s
new limit of the descriptor.

Return values: Returns True if successful, else False.

The new limit specified must be the byte length of the segment - 1. Segment limits bigger than or equal to 1MB must be page aligned, they must have the lower 12 bits set.

For an example, see .

Check the variable.
Return DOS transfer memory buffer size

Returns the size of the pre-allocated dos memory buffer.

Return values: The size of the pre-allocated dos memory buffer. This block always seems to be 16k in size, but don't rely on this.

None.
Return offset of DOS transfer buffer transfer_buffer returns the offset of the transfer buffer. None. Unlock code segment

Unlocks a memory range which resides in the code segment selector.

Parameters:

functionaddr
address of function to be unlocked.
size
size bytes to be unlocked.

Return value: True if successful, False otherwise.

For an example, see .

Check the variable.
Unlock data segment

Unlocks a memory range which resides in the data segment selector.

Parameters:

data
address of memory to be unlocked.
size
size bytes to be unlocked.

Return values: True if successful, False otherwise.

For an example, see .

Check the variable.
Unlock linear memory region

Unlocks a previously locked linear region range to allow it to be swapped out again if needed.

Parameters:

linearaddr
linear address of the memory to be unlocked.
size
size bytes to be unlocked.

Return values: True if successful, False otherwise.

Check the variable.