Thunking with DOSCALL1

DOSCALL1.DLL is an external extension of the OS/2 kernel that provides several translations for standard Dos* API calls.

The following entry points are probably useful for people who have to deal with thunking, i.e. translation of 0:32 pointers to 16:16 and vice versa.

Back in the old days of OS/2 2.x/3.x, this pointer conversion was a rather simple job. Some clever programmers invented a fast mechanism to convert pointers between 16 and 32 bit, by use of the local descriptor table (LDT). The LDT was initialized to contain direct translations for the first 512MB of memory, i.e. the first 64K were mapped to LDT entry 0 (invalid, though!), the second 64k to entry 1, third to 2 and so on. Since there are 8192 LDT entries, this sums up to 8192 * 64K = 512MB of addressable memory. The following formulae applied: flataddr = ((((selector&0x1fff) << 3) | 0x0007) << 16) | offset and selector = (flataddr >> 3) >> 16 offset = flataddr & 0xffff

The formulae are written here for readability, in assembler they turn out to be pretty efficiently codeable.

Unfortunately, this no longer works with newer kernels which allow user processes to get more than 512MB of memory; thus some sort of compatibility to old 16 bit apps has been dropped. It is now no longer guaranteed to 32 bit apps that there is a 1:1 relation between used 32 bit and 16:16 addresses through LDT, so simple arithmetic probably will no longer apply. This is particularly difficult if some 32 bit application has to transfer data to some legacy 16 bit app. This is what the following API functions are good for.

An application for these functions is described in the DDK header file  which explains how to create handcoded thunks.

Synopsis
void far* DOSFLATTOSEL(ULONG eax)

Description
This converts a 32 bit flat address to a 16:16 far pointer to be usable by 16 bit code. This routine is callable from 16 bit code. The result is returned in eax (selector=high word, offset=low word).

Parameters
eax (register) The flat address to be converted. This argument is not passed on the stack.

Synopsis
ULONG DOSSELTOFLAT(void far* eax)

Description
This converts a 16:16 far pointer to a 32 bit flat address to be usable by 32 bit code. This routine is callable from 16 bit code. The result is returned in eax.

Parameters
eax (register) The pointer to be converted (selector=high word, offset=lowword). This argument is not passed on the stack.

Synopsis
ULONG Dos32FlatToSel(ULONG eax)

Description
This converts a 32 bit flat address to a 16:16 far pointer to be usable by 16 bit code. This routine is callable from 32 bit code. The result is returned in eax (selector=high word, offset=low word).

Parameters

 * eax (register) : The flat address to be converted. This argument is not passed on the stack.

Synopsis
ULONG Dos32SelToFlat(ULONG eax)

Description
This converts a 16:16 far pointer to a 32 bit flat address to be usable by 32 bit code. This routine is callable from 32 bit code. The result is returned in eax.

Parameters
This argument is not passed on the stack.
 * eax (register) : The pointer to be converted (selector=high word, offset=lowword).