go32.txt 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. The GO32 unit.
  2. This chapter of the documentation describe the GO32 unit for the Free Pascal compiler under DOS.
  3. This unit was first written for DOS by Florian Klämpfl.
  4. This chapter is divided in three sections.
  5. The first section is an introduction to the GO32 unit. The second section lists the pre-defined constants, types and variables.
  6. The third section describes the functions which appear in the interface part of the GO32 unit.
  7. A lot of function descriptions were made by Thomas Schatzl, for which my thanks.
  8. Introduction
  9. These docs contain information about the GO32 unit. 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 former is much more advanced and better.
  10. Additionally a lot of functions only work in DPMI mode anyway.
  11. 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.
  12. So let's get started and happy and error free coding I wish you....
  13. Thomas Schatzl, 25. August 1998
  14. Protected mode memory organization
  15. What is DPMI
  16. 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.
  17. A DPMI host (either a Windows DOS box or CWSDPMI.EXE) provides these functions for your programs.
  18. Selectors and descriptors
  19. 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, ...).
  20. These descriptors are stored internally in a so-called descriptor table, which is basically an array of such descriptors.
  21. Selectors are roughly an index into this table.
  22. 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.
  23. 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.
  24. FPC specialities (some collected tips and hints)
  25. The %ds and %es selector MUST always contain the same value or some system routines may crash when called.
  26. 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.
  27. Luckily we asm programmers can still use the %gs selector for our own purposes, but for how long ?
  28. See also:
  29. tseginfo, dosmemselector, DOS memory access, get_cs(), get_ds(), get_ss()
  30. allocate_ldt_descriptors(), free_ldt_descriptors(), segment_to_descriptor()
  31. get_next_selector_increment_value(), get_segment_base_address(),
  32. set_segment_base_address(), set_segment_limit(),
  33. create_code_segment_alias_descriptor()
  34. DOS memory access
  35. 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.
  36. 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.
  37. 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.
  38. 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.
  39. See also:
  40. Selectors and descriptors, dosmemselector, dosmemget(), dosmemput(), dosmemmove(), dosmemfillchar(), dosmemfillword(), mem[]-arrays(), seg_move(), seg_fillchar(), seg_fillword()
  41. I/O port access
  42. The I/O port access is done via the various inport() and outport() functions which are available.
  43. 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.
  44. See also:
  45. outport*(), inport*(), PORT[]-arrays
  46. Processor access
  47. These are some functions to access various segment registers (%cs, %ds, %ss) which makes your work a bit easier.
  48. See also:
  49. Selectors and descriptors, get_cs(), get_ds(), get_ss()
  50. Interrupt redirection
  51. 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. These functions are called handlers.
  52. Handling interrupts with DPMI
  53. 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.
  54. Protected mode interrupts vs. Real mode interrupts
  55. 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.
  56. 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.
  57. 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.
  58. Creating own interrupt handlers
  59. 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
  60. interrupts.
  61. Disabling interrupts
  62. The GO32 unit provides the two procedures disable() and enable() to disable and enable all interrupts.
  63. Hardware interrupts
  64. 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.
  65. 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.
  66. Because of this up to 15 different hardware interrupt sources can be handled.
  67. 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.
  68. 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.
  69. 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).
  70. Software interrupts
  71. Ordinarily, a handler installed with set_pm_interrupt only services software interrupts that are executed in protected mode; real mode software interrupts can be redirected by set_rm_interrupt.
  72. See also
  73. set_rm_interrupt(), get_rm_interrupt(), set_pm_interrupt(), get_pm_interrupt(), lock_data(), lock_code(), enable(), disable(), outport*()
  74. Real mode callbacks
  75. 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.
  76. 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.
  77. 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 conditions:
  78. * interrupts disabled
  79. * %CS:%EIP = 48 bit pointer specified in the original call to get_rm_callback()
  80. * %DS:%ESI = 48 bit pointer to to real mode SS:SP
  81. * %ES:%EDI = 48 bit pointer of real mode register data structure
  82. * %SS:%ESP = locked protected mode stack
  83. * All other registers undefined
  84. 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.
  85. 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.
  86. All variables and code touched by the callback procedure MUST be locked to prevent page faults.
  87. See also:
  88. Segment and descriptors, tseginfo, trealregs, get_rm_callback(), free_rm_callback(), lock_code(), lock_data()
  89. Types, Variables and Constants
  90. Constants
  91. Constants returned by get_run_mode()
  92. Tells you under what memory environment (e.g. memory manager) the program currently runs.
  93. rm_unknown = 0; { unknown }
  94. rm_raw = 1; { raw (without HIMEM) }
  95. rm_xms = 2; { XMS (for example with HIMEM, without EMM386) }
  96. rm_vcpi = 3; { VCPI (for example HIMEM and EMM386) }
  97. rm_dpmi = 4; { DPMI (for example DOS box or 386Max) }
  98. Note: GO32V2 _always_ creates DPMI programs, so you need a suitable DPMI host like CWSDPMI.EXE or a Windows DOS box. So you don't need to check it, these constants are only useful in GO32V1 mode.
  99. Processor flags constants
  100. They are provided for a simple check with the flags identifier in the trealregs type. To check a single flag, simply do an AND operation with the flag you want to check. It's set if the result is the same as the flag value.
  101. const
  102. carryflag = $001;
  103. parityflag = $004;
  104. auxcarryflag = $010;
  105. zeroflag = $040;
  106. signflag = $080;
  107. trapflag = $100;
  108. interruptflag = $200;
  109. directionflag = $400;
  110. overflowflag = $800;
  111. Predefined types
  112. type
  113. tmeminfo = record
  114. available_memory : Longint;
  115. available_pages : Longint;
  116. available_lockable_pages : Longint;
  117. linear_space : Longint;
  118. unlocked_pages : Longint;
  119. available_physical_pages : Longint;
  120. total_physical_pages : Longint;
  121. free_linear_space : Longint;
  122. max_pages_in_paging_file : Longint;
  123. reserved : array[0..2] of Longint;
  124. end;
  125. Holds information about the memory allocation, etc.
  126. Record entry Description
  127. available_memory Largest available free block in bytes
  128. available_pages Maximum unlocked page allocation in pages
  129. available_lockable_pages Maximum locked page allocation in pages
  130. linear_space Linear address space size in pages
  131. unlocked_pages Total number of unlocked pages
  132. available_physical_pages Total number of free pages
  133. total_physical_pages Total number of physical pages
  134. free_linear_space Free linear address space in pages
  135. max_pages_in_paging_file Size of paging file/partition in pages
  136. 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.
  137. The size of the pages can be determined by the get_page_size() function.
  138. type
  139. trealregs = record
  140. case Integer of
  141. 1: { 32-bit } (EDI, ESI, EBP, Res, EBX, EDX, ECX, EAX: Longint;
  142. Flags, ES, DS, FS, GS, IP, CS, SP, SS: Word);
  143. 2: { 16-bit } (DI, DI2, SI, SI2, BP, BP2, R1, R2: Word;
  144. BX, BX2, DX, DX2, CX, CX2, AX, AX2: Word);
  145. 3: { 8-bit } (stuff: array[1..4] of Longint;
  146. BL, BH, BL2, BH2, DL, DH, DL2, DH2,
  147. CL, CH, CL2, CH2, AL, AH, AL2, AH2: Byte);
  148. 4: { Compat } (RealEDI, RealESI, RealEBP, RealRES,
  149. RealEBX, RealEDX, RealECX, RealEAX: Longint;
  150. RealFlags,
  151. RealES, RealDS, RealFS, RealGS,
  152. RealIP, RealCS, RealSP, RealSS: Word);
  153. end;
  154. registers = trealregs;
  155. These two types contain the data structure to pass register values to a interrupt handler or real mode callback.
  156. type
  157. tseginfo = record
  158. offset : Pointer;
  159. segment : Word;
  160. end;
  161. 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.
  162. See also:
  163. Selectors and descriptors, DOS memory access, Interrupt redirection
  164. Variables.
  165. var
  166. dosmemselector : Word;
  167. 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.
  168. var
  169. int31error : Word;
  170. This variable holds the result of a DPMI interrupt call. Any nonzero value must be treated as a critical failure.
  171. Functions and Procedures
  172. Descriptor handling
  173. function allocate_ldt_descriptors (count : Word) : Word;
  174. Allocates a number of new descriptors.
  175. Parameters:
  176. count - specifies the number of requested unique descriptors
  177. Return value:
  178. Base selector
  179. Notes:
  180. 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.
  181. 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 get_next_selector_increment_value() function.
  182. Errors:
  183. Check int31error variable
  184. Example:
  185. (* not my job *)
  186. See also:
  187. Selectors and descriptors, int31error, free_ldt_descriptors(), get_next_selector_increment_value(), segment_to_descriptor(), create_code_segment_alias_descriptor(), set_segment_limit(), set_segment_base_address()
  188. function free_ldt_descriptor (des : Word) : boolean;
  189. Frees a previously allocated descriptor.
  190. Parameters:
  191. des - The descriptor to be freed
  192. Return value:
  193. True if successful, false otherwise
  194. Notes:
  195. After this call this selector is invalid and must not be used for any memory operations anymore. Each descriptor allocated with allocate_ltd_descriptor() must be freed individually with this function, even if it was previously allocated as a part of a contiguous array of descriptors.
  196. Errors:
  197. Check int31error variable
  198. Example:
  199. (* not my job *)
  200. See also:
  201. Selectors and descriptors, int31error, allocate_ldt_descriptors(), get_next_selector_increment_value()
  202. function segment_to_descriptor (seg : Word) : Word;
  203. Maps a real mode segment (paragraph) address onto an descriptor that can be used by a protected mode program to access the same memory.
  204. Parameters:
  205. seg - the real mode segment you want the descriptor to
  206. Return values:
  207. Descriptor to real mode segment address.
  208. Notes:
  209. 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.
  210. 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 allocate_ldt_descriptors() and change the base address as necessary.
  211. Errors:
  212. Check int31error variable.
  213. Example:
  214. (* not my job *)
  215. See also:
  216. Selectors and descriptors, allocate_ldt_descriptors(), free_ldt_descriptor(), set_segment_base_address()
  217. function get_next_selector_increment_value : Word;
  218. Returns the selector increment value when allocating multiple subsequent descriptors via allocate_ldt_descriptors().
  219. Parameters:
  220. none
  221. Return value:
  222. Selector increment value
  223. Notes:
  224. Because allocate_ldt_descriptors() 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.
  225. Errors:
  226. Check int31error variable
  227. Example:
  228. (* not my job *)
  229. See also:
  230. Selectors and descriptors, int31error, allocate_ldt_descriptors(), free_ldt_descriptor()
  231. function get_segment_base_address (d : Word) : Longint;
  232. Returns the 32-bit linear base address from the descriptor table for the specified segment.
  233. Parameters:
  234. d - selector of the descriptor you want the base address
  235. Return values:
  236. Linear base address of specified descriptor
  237. Errors:
  238. Check int31error variable.
  239. Example:
  240. (* not my job *)
  241. See also:
  242. Selectors and descriptors, int31error, allocate_ldt_descriptors(), set_segment_base_address(), allocate_ldt_descriptors(), set_segment_limit(), get_segment_limit()
  243. function set_segment_base_address (d : Word; s : Longint) : boolean;
  244. Sets the 32-bit linear base address of a descriptor.
  245. Parameters:
  246. d - selector
  247. s - new base address of the descriptor
  248. Notes:
  249. none
  250. Errors:
  251. Check int31error variable
  252. Example:
  253. (* not my job *)
  254. See also:
  255. Selectors and descriptors, int31error, allocate_ldt_descriptors(), get_segment_base_address(), allocate_ldt_descriptors(), set_segment_limit(), get_segment_base_address(), get_segment_limit()
  256. function get_segment_limit (d : Word) : Longint;
  257. Returns a descriptors segment limit
  258. Parameters
  259. d - selector
  260. Return value:
  261. Limit of the descriptor in bytes
  262. Notes:
  263. none
  264. Errors:
  265. Returns zero if descriptor is invalid
  266. Example:
  267. (* not my job *)
  268. See also:
  269. Selectors and descriptors, allocate_ldt_descriptors(), set_segment_limit(), set_segment_base_address(), get_segment_base_address()
  270. function set_segment_limit (d : Word; s : Longint) : boolean;
  271. Sets the limit of a descriptor.
  272. Parameters:
  273. d - selector
  274. s - new limit of the descriptor
  275. Return values:
  276. Returns true if successful, else false.
  277. Notes:
  278. 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.
  279. Errors:
  280. Check int31error variable
  281. Example:
  282. (* not my job *)
  283. See also:
  284. Selectors and descriptors, int31error, allocate_ldt_descriptors(), set_segment_base_address(), get_segment_limit(), set_segment_limit()
  285. function set_descriptor_access_right (d : Word; w : Word) : Longint;
  286. Sets the access rights of a descriptor
  287. Parameters:
  288. d - selector
  289. w - new descriptor access rights
  290. Return values:
  291. ?? (* this is a bug. This function doesn't return anything useful *)
  292. Notes:
  293. none
  294. Errors:
  295. Check int31error variable
  296. See also:
  297. Selectors and descriptors, int31error, get_descriptor_access_rights()
  298. function get_descriptor_access_right (d : Word) : Longint;
  299. Gets the access rights of a descriptor
  300. Parameters:
  301. d - selector to descriptor
  302. Return value:
  303. Access rights bit field
  304. Errors:
  305. Check int31error variable
  306. Example:
  307. (* not my job *)
  308. See also:
  309. Selectors and descriptors, int31error, set_descriptor_access_rights()
  310. function create_code_segment_alias_descriptor (seg : Word) : Word;
  311. Creates a new descriptor that has the same base and limit as the specified descriptor.
  312. Parameters:
  313. seg - selector
  314. Return values:
  315. Data selector (alias)
  316. Notes:
  317. 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.
  318. Errors:
  319. Check int31error variable
  320. Example:
  321. (* not my job *)
  322. See also:
  323. Selectors and descriptors, int31error, allocate_ldt_descriptors(), set_segment_limit(), set_segment_base_address()
  324. Extended memory management services
  325. function get_meminfo (var meminfo : tmeminfo) : boolean;
  326. Returns information about the amount of available physical memory, linear address space, and disk space for page swapping.
  327. Parameters:
  328. meminfo - buffer to fill memory information into
  329. Return values:
  330. Due to an implementation bug this function always returns false, but it always succeeds.
  331. Notes:
  332. 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 get_page_size() function.
  333. Errors:
  334. Check the int31error variable
  335. Example:
  336. (* not my job *)
  337. See also:
  338. int31error, tmeminfo, get_page_size()
  339. function allocate_memory_block (size:Longint):Longint;
  340. Allocates a block of linear memory.
  341. Parameters:
  342. size - Size of requested linear memory block in bytes
  343. Returned values:
  344. ?? blockhandle - the memory handle to this memory block
  345. Linear address of the requested memory.
  346. Notes:
  347. 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 is used to free the memory block afterwards or use this handle for other purposes. So this block can't be deallocated and is henceforth unusuable !
  348. This function doesn't allocate any descriptors for this block, it's the applications resposibility to allocate and initialize for accessing this memory.
  349. Errors:
  350. Check int31error variable
  351. Example:
  352. (* not my job *)
  353. See also:
  354. free_memory_block()
  355. function free_memory_block (blockhandle : Longint) : boolean;
  356. Frees a previously allocated memory block
  357. Parameters:
  358. blockhandle - the handle to the memory area to free
  359. Return value:
  360. True if successful, false otherwise.
  361. Notes:
  362. Frees memory that was previously allocated with allocate_memory_block(). This function doesn't free any descriptors mapped to this block, it's the application's responsibility.
  363. Errors:
  364. Check int31error variable
  365. Example:
  366. (* not my job *)
  367. See also:
  368. allocate_memory_block()
  369. function request_linear_region (linearaddr, size : Longint; var blockhandle : Longint) : boolean;
  370. !!!!!! DOESN'T WORK AT ALL, AND WON'T IN THE FUTURE BECAUSE IT IS A DPMI 1.0 FUNCTION !!!!!!! (a good reason to skip this description)
  371. function map_device_in_memory_block (handle, offset, pagecount, device : Longint) : boolean;
  372. !!!!!! DOESN'T WORK AT ALL, AND WON'T IN THE FUTURE BECAUSE IT IS A DPMI 1.0 FUNCTION !!!!!!! (a good reason to skip this description)
  373. function get_linear_addr (phys_addr : Longint; size : Longint) : Longint;
  374. Converts a physical address into a linear address.
  375. Parameters:
  376. phys_addr - physical address of device
  377. size - size of region to map in bytes
  378. Return value:
  379. Linear address that can be used to access the physical memory.
  380. Notes:
  381. 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.
  382. Errors:
  383. Check int31error variable.
  384. Example:
  385. See also:
  386. Selectors and descriptors, DOS memory access, int31error, allocate_ldt_descriptors(), set_segment_limit(), set_segment_base_address()
  387. DOS memory management
  388. function global_dos_alloc (bytes : Longint) : Longint;
  389. Allocates a block of DOS real mode memory
  390. Parameters:
  391. bytes - size of requested real mode memory
  392. Return values:
  393. The high word of the returned value contains the selector to the allocated DOS memory block, the low word the corresponding real mode segment value. The offset value is always zero.
  394. Notes:
  395. 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.
  396. 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.
  397. 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.
  398. The returned selector should only be freed by a global_dos_free() call.
  399. Errors:
  400. Check int31error variable
  401. Example:
  402. (* not my job *)
  403. See also:
  404. Selectors and descriptors, DOS memory access, int31error, global_dos_free()
  405. function global_dos_free (selector : Word) : boolean;
  406. Frees a previously allocated DOS memory block
  407. Parameters:
  408. selector - selector to the DOS memory block
  409. Return value:
  410. True if successful, false otherwise
  411. Notes:
  412. 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 global_dos_alloc().
  413. Errors:
  414. Check int31error variable
  415. Example:
  416. (* not my job *)
  417. See also:
  418. Selectors and descriptors, DOS memory access, int31error, global_dos_alloc()
  419. procedure dosmemput (seg : Word; ofs : Word; var data; count : Longint);
  420. Copies heap data to DOS real mode memory.
  421. Parameters:
  422. seg - destination real mode segment
  423. ofs - destination real mode offset
  424. data - source
  425. count - number of bytes to copy
  426. Return value:
  427. none
  428. Notes:
  429. No range checking is performed.
  430. Errors:
  431. none
  432. Example:
  433. (* not my job *)
  434. See also:
  435. DOS memory access, dosmemget(), dosmemmove(), dosmemfillchar(), dosmemfillword(), seg_move(), seg_fillchar(), seg_fillword()
  436. procedure dosmemget (seg : Word; ofs : Word; var data; count : Longint);
  437. Copies data from the DOS memory onto the heap.
  438. Parameters:
  439. seg - source real mode segment
  440. ofs - source real mode offset
  441. data - destination
  442. count - number of bytes to copy
  443. Notes:
  444. No range checking is performed.
  445. Errors:
  446. none
  447. Example:
  448. (* not my job *)
  449. See also:
  450. DOS memory access, dosmemput(), dosmemmove(), dosmemfillchar(), dosmemfillword(), seg_move(), seg_fillchar(), seg_fillword()
  451. procedure dosmemmove (sseg, sofs, dseg, dofs : Word; count : Longint);
  452. Copies count bytes of data between two DOS real mode memory locations.
  453. Parameters:
  454. sseg - source real mode segment
  455. sofs - source real mode offset
  456. dseg - destination real mode segment
  457. dofs - destination real mode offset
  458. count - number of bytes to copy
  459. Return values:
  460. none
  461. Notes:
  462. No range check is performed in any way.
  463. Errors:
  464. none
  465. Example:
  466. (* not my job *)
  467. See also:
  468. Selectors and descriptors, DOS memory access, dosmemput(), dosmemget(), dosmemfillchar(), dosmemfillword(), seg_move(), seg_fillchar(), seg_fillword()
  469. procedure dosmemfillchar (seg, ofs : Word; count : Longint; c : char);
  470. Sets a region of DOS memory to a specific byte value
  471. Parameters:
  472. seg - real mode segment
  473. ofs - real mode offset
  474. count - number of bytes to set
  475. c - value to set memory to
  476. Return values:
  477. none
  478. Notes:
  479. No range check is performed.
  480. Errors:
  481. none
  482. Example:
  483. (* not my job *)
  484. See also:
  485. Selectors and descriptors, DOS memory access, dosmemput(), dosmemget(), dosmemmove(), dosmemfillword(), seg_move(), seg_fillchar(), seg_fillword()
  486. procedure dosmemfillword(seg, ofs : Word; count : Longint; w : Word);
  487. Sets a region of DOS memory to a specific word value
  488. Parameters:
  489. seg - real mode segment
  490. ofs - real mode offset
  491. count - number of words to set
  492. w - value to set memory to
  493. Return values:
  494. none
  495. Notes:
  496. No range check is performed.
  497. Errors:
  498. none
  499. Example:
  500. (* not my job *)
  501. See also:
  502. Selectors and descriptors, DOS memory access, dosmemput(), dosmemget(), dosmemmove(), dosmemfillchar(), seg_move(), seg_fillchar(), seg_fillword()
  503. Interrupt management services
  504. function get_rm_interrupt (vector : byte; var intaddr : tseginfo) : boolean;
  505. Returns the contents of the current machine's real mode interrupt vector for the specified interrupt.
  506. Parameters:
  507. vector - interrupt vector number
  508. intaddr - buffer to store real mode segment:offset address
  509. Return values:
  510. True if successful, false otherwise
  511. Notes:
  512. The returned address is a real mode segment address, which isn't valid in protected mode.
  513. Errors:
  514. Check int31error variable
  515. Example:
  516. (* not my job *)
  517. See also:
  518. Interrupt redirection, int31error, set_rm_interrupt(), set_pm_interrupt(), get_pm_interrupt()
  519. function set_rm_interrupt (vector : byte; const intaddr : tseginfo) : boolean;
  520. Sets a real mode interrupt handler
  521. Parameters:
  522. vector - the interrupt vector number to set
  523. intaddr - address of new interrupt vector
  524. Return values:
  525. True if successful, otherwise false.
  526. Notes:
  527. 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 get_rm_callback().
  528. Errors:
  529. Check int31error variable
  530. Example:
  531. (* not my job *)
  532. See also:
  533. Interrupt redirection, int31error, get_rm_interrupt(), set_pm_interrupt(), get_pm_interrupt(), get_rm_callback()
  534. function get_pm_interrupt (vector : byte; var intaddr : tseginfo) : boolean;
  535. Returns the address of a current protected mode interrupt handler
  536. Parameters:
  537. vector - interrupt handler number you want the address to
  538. intaddr - buffer to store address
  539. Return values:
  540. True if successful, false if not.
  541. Notes:
  542. The returned address is a protected mode selector:offset address.
  543. Errors:
  544. Check int31error variable
  545. Example:
  546. (* not my job *)
  547. See also:
  548. Interrupt redirection, int31error, set_pm_interrupt(), set_rm_interrupt(), get_rm_interrupt()
  549. function set_pm_interrupt (vector : byte; const intaddr : tseginfo) : boolean;
  550. Sets the address of the protected mode handler for an interrupt
  551. Parameters:
  552. vector - number of protected mode interrupt to set
  553. intaddr - selector:offset address to the interrupt vector
  554. Return values:
  555. True if successful, false otherwise.
  556. Notes:
  557. The address supplied must be a valid selector:offset protected mode address.
  558. Errors:
  559. Check int31error variable
  560. Example:
  561. (* not my job *)
  562. See also:
  563. Interrupt redirection, int31error, get_pm_interrupt(), set_rm_interrupt(), get_rm_interrupt()
  564. procedure disable;
  565. Disables all hardware interrupts by execution a CLI instruction.
  566. Parameters:
  567. none
  568. Return values:
  569. none
  570. Errors:
  571. none
  572. Example:
  573. (* not my job *)
  574. See also:
  575. Interrupt redirection, enable()
  576. procedure enable;
  577. Enables all hardware interrupts by executing a STI instruction.
  578. Parameters:
  579. none
  580. Return values:
  581. none
  582. Errors:
  583. none
  584. Example:
  585. (* not my job *)
  586. See also:
  587. Interrupt redirection, disable()
  588. Translation services
  589. function realintr(intnr : Word; var regs : trealregs) : boolean;
  590. Simulates an interrupt in real mode
  591. Parameters:
  592. intnr - interrupt number to issue in real mode
  593. regs - registers data structure
  594. Return values:
  595. The supplied registers data structure contains the values that were returned by the real mode interrupt.
  596. True if successful, false if not.
  597. Notes:
  598. 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.
  599. Errors:
  600. Check int31error variable
  601. Example:
  602. (* not my job *)
  603. See also:
  604. Interrupts, trealregs
  605. function get_rm_callback (pm_func : pointer; const reg : trealregs; var rmcb : tseginfo) : boolean;
  606. 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.
  607. Parameters:
  608. pm_func - pointer to the protected mode callback function
  609. reg - supplied registers structure
  610. rmcb - buffer to real mode address of callback function
  611. Return values:
  612. True if successful, otherwise false.
  613. Notes:
  614. 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
  615. protected mode program or notify the protected mode program of an event.
  616. The contents of the supplied regs structure is not valid after function call, but only at the time of the actual callback.
  617. Errors:
  618. Check int31error variable
  619. Example:
  620. (* not my job *)
  621. See also:
  622. Callbacks, int31error, free_rm_callback()
  623. function free_rm_callback (var intaddr : tseginfo) : boolean;
  624. Releases a real mode callback address that was previously allocated with the get_rm_callback() function.
  625. Parameters:
  626. intaddr - real mode address buffer returned by get_rm_callback()
  627. Return values:
  628. True if successful, false if not
  629. Notes:
  630. A callback should be released immediately when it is no longer used.
  631. Errors:
  632. Check int31error variable
  633. Example:
  634. (* not my job *)
  635. See also:
  636. Callbacks, int31error, set_rm_interrupt(), get_rm_callback()
  637. Page management
  638. function lock_linear_region (linearaddr, size : Longint) : boolean;
  639. Locks a memory region to prevent swapping of it
  640. Parameters:
  641. linearaddr - the linear address of the memory are to be locked
  642. size - size in bytes to be locked
  643. Return value:
  644. True if successful, false otherwise
  645. Notes:
  646. none
  647. Errors:
  648. Check int31error variable
  649. Example:
  650. (* not my job *)
  651. See also:
  652. DPMI, int31error, lock_data(), lock_code(), unlock_linear_region(), unlock_data(), unlock_code()
  653. function lock_data (var data; size : Longint) : boolean;
  654. Locks a memory range which resides in the data segment selector
  655. Parameters:
  656. data - address of data to be locked
  657. size - length of data to be locked
  658. Return values:
  659. True if successful, false otherwise
  660. Notes:
  661. none
  662. Errors:
  663. Check int31error variable
  664. Example:
  665. (* not my job *)
  666. See also:
  667. DPMI, int31error, lock_linear_region(), lock_code(), unlock_linear_region(), unlock_data(), unlock_code()
  668. function lock_code (functionaddr : pointer; size : Longint) : boolean;
  669. Locks a memory range which is in the code segment selector.
  670. Parameters:
  671. functionaddr - address of the function to be lockd
  672. size - size in bytes to be locked
  673. Return values:
  674. True if successful, false otherwise
  675. Errors:
  676. Check int31error variable
  677. Example:
  678. (* not my job *)
  679. See also:
  680. DPMI, int31error, lock_linear_region(), lock_data(), unlock_linear_region(), unlock_data(), unlock_code()
  681. function unlock_linear_region (linearaddr, size : Longint) : boolean;
  682. Unlocks a previously locked linear region range to allow it to be swapped out again if needed.
  683. Parameters:
  684. linearaddr - linear address of the memory to be unlocked
  685. size - size bytes to be unlocked
  686. Return values:
  687. True if successful, false otherwise
  688. Notes:
  689. none
  690. Errors:
  691. Check int31error variable
  692. Example:
  693. (* not my job *)
  694. See also:
  695. DPMI, int31error, unlock_data(), unlock_code(), lock_linear_region(), lock_data(), lock_code()
  696. function unlock_data (var data; size : Longint) : boolean;
  697. Unlocks a memory range which resides in the data segment selector.
  698. Paramters:
  699. data - address of memory to be unlocked
  700. size - size bytes to be unlocked
  701. Return values:
  702. True if successful, false otherwise
  703. Notes:
  704. none
  705. Errors:
  706. Check int31error variable
  707. Example:
  708. (* not my job *)
  709. See also:
  710. DPMI, int31error, unlock_linear_region(), unlock_code(), lock_linear_region(), lock_data(), lock_code()
  711. function unlock_code (functionaddr : pointer; size : Longint) : boolean;
  712. Unlocks a memory range which resides in the code segment selector.
  713. Parameters:
  714. functionaddr - address of function to be unlocked
  715. size - size bytes to be unlocked
  716. Return value:
  717. True if successful, false otherwise
  718. Notes:
  719. none
  720. Errors:
  721. Check int31error variable
  722. Example:
  723. (* not my job *)
  724. See also:
  725. DPMI, int31error, unlock_linear_region(), unlock_data(), lock_linear_region(), lock_data(), lock_code()
  726. function get_page_size : Longint;
  727. Returns the size of a single memory page
  728. Parameters:
  729. none
  730. Return value:
  731. Size of a single page in bytes
  732. Notes:
  733. The returned size is typically 4096 bytes.
  734. Errors:
  735. Check int31error variable
  736. Example:
  737. (* not my job *)
  738. See also:
  739. tmeminfo, get_meminfo()
  740. Miscellanous
  741. procedure seg_move (sseg : Word; source : Longint; dseg : Word; dest : Longint; count : Longint);
  742. Copies data between two memory locations
  743. Parameters:
  744. sseg - source selector
  745. source - source offset
  746. dseg - destination selector
  747. dest - destination offset
  748. count - size in bytes to copy
  749. Return values:
  750. none
  751. Notes:
  752. Overlapping is only checked if the source selector is equal to the destination selector. No range check is done.
  753. Errors:
  754. none
  755. Example:
  756. (* not my job *)
  757. See also:
  758. Selectors and descriptors, DOS memory access, seg_fillchar(), seg_fillword(), dosmemfillchar(), dosmemfillword(), dosmemget(), dosmemput(), dosmemmove()
  759. procedure seg_fillchar (seg : Word; ofs : Longint; count : Longint; c : char);
  760. Sets a memory area to a specific value.
  761. Parameters:
  762. seg - selector to memory area
  763. ofs - offset to memory
  764. count - number of bytes to set
  765. c - byte data which is set
  766. Return values:
  767. none
  768. Notes:
  769. No range check is done in any way.
  770. Errors:
  771. none
  772. Example:
  773. (* not my job *)
  774. See also:
  775. Selectors and descriptors, DOS memory access, seg_move(), seg_fillword(), dosmemfillchar(), dosmemfillword(), dosmemget(), dosmemput(), dosmemmove()
  776. procedure seg_fillword (seg : Word; ofs : Longint; count : Longint; w : Word);
  777. Sets a memory area to a specific value.
  778. Parameters:
  779. seg - selector to memory area
  780. ofs - offset to memory
  781. count - number of words to set
  782. w - word data which is set
  783. Return values:
  784. none
  785. Notes:
  786. No range check is done in any way.
  787. Errors:
  788. none
  789. Example:
  790. (* not my job *)
  791. See also:
  792. Selectors and descriptors, DOS memory access, seg_move(), seg_fillchar(), dosmemfillchar(), dosmemfillword(), dosmemget(), dosmemput(), dosmemmove()
  793. function get_cs : Word;
  794. Returns the cs selector
  795. Parameters:
  796. none
  797. Return values:
  798. The content of the cs segment register
  799. Errors:
  800. none
  801. Example:
  802. (* not my job *)
  803. See also:
  804. Selector and descriptors, Processor access, int31error, get_ds, get_ss
  805. function get_ds : Word;
  806. Returns the ds selector
  807. Parameters:
  808. none
  809. Return values:
  810. The content of the ds segment register
  811. Errors:
  812. none
  813. Example:
  814. (* not my job *)
  815. See also:
  816. Selector and descriptors, Processor access, int31error, get_cs, get_ss
  817. function get_ss : Word;
  818. Returns the ss selector
  819. Parameters:
  820. none
  821. Return values:
  822. The content of the ss segment register
  823. Errors:
  824. none
  825. Example:
  826. (* not my job *)
  827. See also:
  828. Selector and descriptors, Processor access, int31error, get_ds, get_cs
  829. function inportb (port : Word) : byte;
  830. function inportw (port : Word) : Word;
  831. function inportl (port : Word) : Longint;
  832. Reads data from the selected I/O port
  833. Parameters:
  834. port - the I/O port number which is read
  835. Return values:
  836. Current I/O port value
  837. Notes:
  838. The returned data is either be byte, word or longint sized, dependant of the function.
  839. Errors:
  840. none
  841. Example:
  842. (* not my job *)
  843. See also:
  844. I/O port access, outport*()
  845. procedure outportb (port : Word; data : byte);
  846. procedure outportw (port : Word; data : Word);
  847. procedure outportl (port : Word; data : Longint);
  848. Sends data to the specified I/O port
  849. Parameters:
  850. port - the I/O port number to send data to
  851. data - value sent to I/O port
  852. Return values:
  853. none
  854. Notes:
  855. The difference between these functions is the size of the sent data (either byte, word or longint sized)
  856. Errors:
  857. none
  858. Example:
  859. (* not my job *)
  860. See also:
  861. I/O port access, inport*()
  862. function get_run_mode : Word;
  863. Returns the current mode your application runs with
  864. Parameters:
  865. none
  866. Return values:
  867. One of the constants used by this function
  868. Notes:
  869. none
  870. Errors:
  871. none
  872. Example:
  873. (* not my job *)
  874. See also:
  875. DPMI, constants returned by get_run_mode