Archives
- April 2025
- March 2025
- February 2025
- January 2025
- December 2024
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- June 2021
- May 2021
- April 2021
- March 2021
- February 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020
- August 2020
- July 2020
- June 2020
- May 2020
- April 2020
- March 2020
- February 2020
- January 2020
- December 2019
- November 2019
- October 2019
- September 2019
- August 2019
- July 2019
- June 2019
- May 2019
- April 2019
- March 2019
- February 2019
- January 2019
- December 2018
- November 2018
- October 2018
- August 2018
- July 2018
- June 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- December 2017
- November 2017
- October 2017
- August 2017
- July 2017
- June 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- January 2011
- November 2010
- October 2010
- August 2010
- July 2010
HIMEM.SYS, unreal mode, and LOADALL
The previous post talked about real mode on 286+ processors which behaves more like a slightly modified variant of protected mode rather than the old 8088/8086 processors. Real mode with non-compatible selector bases or limits is usually called unreal mode or big real mode. Even though Intel never clearly documented the behavior in detail, it is more or less set in stone thanks to our dear friend, backwards compatibility.
That’s because unreal mode had some rather prominent users—one of them was Microsoft’s HIMEM.SYS, used on nearly every PC in the 1990s. While disassembling HIMEM.SYS might be instructive, it would be a waste of time in this case, since Microsoft published the full source code to several HIMEM.SYS 2.x versions. That way, the evolution of the code can be easily traced.
HIMEM.SYS version 2.00 from July 1988 was quite straightforward and used BIOS INT 15h, function 87h to move data to and from extended memory above the 1MB line. However, that function isn’t particularly efficient. On 286 systems, it must reset the CPU, which isn’t fast. On 386 systems, assuming that the BIOS even implemented a 386-specific method without resetting the CPU, it still may have been slow. What’s worse, the BIOS block move had a tendency to block hardware interrupts for too long (a significant problem for serial port communications, notably modems).
In August 1988, HIMEM.SYS version 2.03 implemented a new method of copying memory on 386 compatible processors, using unreal mode (or Big Real mode as it’s called in the HIMEM source) when the processor isn’t already in protected (or more specifically, V86) mode. The method is remarkably simple: A simple string copy instruction is used, copying doublewords where possible, with ESI/EDI offsets beyond 1MB and ES and DS loaded with 0. That of course does not work in standard real mode. However, HIMEM.SYS installs a handler for interrupt 13 (General Protection Fault, or #GP). The #GP handler simply enters protected mode, loads a descriptor with zero base and 4GB limit into ES/DS, and immediately exits protected mode again. The memory move is then automatically restarted. Interrupts are disabled only for very brief periods of time, hence the danger of losing interrupts is very low.
A noteworthy side effect of this method is that the extended selector limits remain in effect, even when HIMEM.SYS is not being actively used. That means the next time extended memory is accessed, there will be no #GP and the memory move will succeed. Running with extended ES/DS selector limits does not cause harm to DOS applications. If some application switches to protected mode and sets the standard 64KB selector limits on the way back, HIMEM.SYS will simply extend the limits again the next time it is called.
The above method was obviously not usable on 286 systems. However, HIMEM.SYS version 2.06 added a new code path which avoided the BIOS thanks to the well-undocumented LOADALL instruction. LOADALL can be used to load the entire internal CPU state, including hidden registers. HIMEM.SYS modified the hidden segment base registers to point to the source or target memory location above 1MB. The processor was never switched out of real mode. The block move was done with interrupts enabled, although if an interrupt actually occurred, the selector bases had to be reprogrammed and the move restarted. The LOADALL method was then the only 286 code path, because V86 mode was not applicable (in other words, the CPU was always in real mode when executing HIMEM.SYS and LOADALL could be used).
Why wasn’t LOADALL used for the 386 code path in HIMEM.SYS? It simply wasn’t necessary because switching to protected mode and back avoided the need for undocumented instructions. Indeed, later processors (Pentium and above) removed LOADALL. A similar effect might still be achieved through SMM (System Management Mode), but only in a model-specific manner.
One of the biggest difficulties HIMEM.SYS implementors faced was the lack of standardized A20 line control. The PC/AT BIOS could (and had to) control the A20 line, but this functionality was unfortunately not exposed. PS/2 systems and various clones used other, more efficient A20 control methods, not necessarily PC/AT compatible. HIMEM.SYS contained a large chunk of code detecting the host type and installing a system specific A20 handler. HIMEM.SYS version 2.06 already contained four different handlers (PC/AT, PS/2, HP Vectra, and AT&T 6300) and later versions only added to that number.
The methods used by HIMEM.SYS were also employed by Microsoft Windows. Memory management product from competing companies (Qualitas, Quarterdeck, etc.) often used similar methods.
4 Responses to HIMEM.SYS, unreal mode, and LOADALL
I’m sorry for the offtopic, but can the aforementioned source code for HIMEM.SYS be found anywhere? I would like to study the code and its evolution, yet my searches didn’t bring much success, for now.
Try http://cd.textfiles.com/1stcanadian/utils/xms_spec/ for example. This package was published by Microsoft and was once easy to find on any programming-related BBS 🙂
himem.sys 2.06 probably lives in this archive, at least I unpacked it with “unar” utility and it mentions loadall/286
https://archives.scovetta.com/pub/power_programming/MICROSFT/XMS20.ARC
Pingback: Curious Instructions | OS/2 Museum
This site uses Akismet to reduce spam. Learn how your comment data is processed.