home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / MEMORIA / VIEWEXT.ZIP / HYPRSPAC.DOC < prev    next >
Encoding:
Text File  |  1990-05-02  |  37.3 KB  |  833 lines

  1.  
  2.  
  3.  
  4.                            THE LOW-DOWN ON LOADALL:
  5.                             EXCERPTS FROM THE BOOK
  6.  
  7.  
  8.                        THE HYPER-SPACE NAVIGATOR'S GUIDE
  9.                                       by
  10.                               Terrance E. Hodgins
  11.  
  12.                   copyright (C) 1990 by Terrance E. Hodgins,
  13.                              All rights reserved.
  14.  
  15.  
  16.                            Semi-Intelligent Systems
  17.                                  PO BOX 4492
  18.                             ALBUQUERQUE, NM 87196
  19.  
  20.                             Compuserve:  76416,553
  21.                      Internet:  76416.553@compuserve.com
  22.  
  23.  
  24.  
  25.  
  26.  
  27. This document uses the following trademarks:
  28.  
  29. AST is a registered trademark of AST Research, Inc.
  30.  
  31. IBM, PC-DOS, PC/XT, and PC/AT are registered trademarks of International Busi-
  32. ness Machines Corporation.
  33.  
  34. Intel is a registered trademark of Intel Corporation.
  35.  
  36. Lotus is a registered trademark of Lotus Development Corporation.
  37.  
  38. Microsoft, MS-DOS, Windows '286, and OS/2 are registered trademarks of  Micro-
  39. soft Corporation.
  40.  
  41. Semi-Intelligent  Systems,  The Hyper-Space Library,  Get-High,  HI-DOS,  High
  42. Code,  Xcode,  and  Mode Code are registered  trademarks  of  Semi-Intelligent
  43. Systems.
  44.  
  45. Unix is a registered trademark of AT&T, Inc.
  46.  
  47.  
  48.  
  49.  
  50.                             Disclaimer of Warranty
  51.  
  52.         TERRANCE E. HODGINS, AND SEMI-INTELLIGENT SYSTEMS, EXCLUDE ANY AND ALL
  53. IMPLIED WARRANTIES, INCLUDING WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  A
  54. PARTICULAR PURPOSE.
  55.  
  56.         NEITHER  TERRANCE E. HODGINS, NOR SEMI-INTELLIGENT SYSTEMS,  MAKE  ANY
  57. WARRANTY  OF REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO  THESE
  58. PROGRAMS,  THEIR QUALITY, PERFORMANCE, MERCHANTABILITY, OR FITNESS FOR A  PAR-
  59. TICULAR PURPOSE.
  60.  
  61.         NEITHER TERRANCE E. HODGINS, NOR SEMI-INTELLIGENT SYSTEMS, SHALL  HAVE
  62. ANY LIABILITY FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
  63. OR RESULTING FROM THE USE OR MODIFICATION OF THESE PROGRAMS.
  64.  
  65.         THE USE OF THE 80286 LOADALL INSTRUCTION IS INHERENTLY DANGEROUS,  AND
  66. CAN RESULT IN PROGRAM CRASHES, OR RUN-AWAY PROGRAMS, WHICH CAN ALTER,  DAMAGE,
  67. OR  DESTROY COMPUTER DATA, AND WHICH CAN DAMAGE OR DESTROY COMPUTER  HARDWARE.
  68. USE ONLY AT YOUR OWN RISK.
  69.  
  70.  
  71.  
  72.  
  73.  
  74.                               Introduction
  75.  
  76.  
  77.  
  78.         Yes,  there really is an unpublicized, almost secret,  instruction  in
  79. the 80286, which has the ability to do several supposedly impossible things.
  80.  
  81.         It is called Loadall.
  82.  
  83.         What  Loadall does is completely load all the registers of  the  80286
  84. from  a table starting at 80:0 in low memory.  I do mean ALL registers:  every
  85. register  you ever heard of, and a few you haven't, and also  the  "invisible"
  86. internal registers which are NOT OTHERWISE programmable.  Executing a  Loadall
  87. nearly completely re-defines the CPU's state.
  88.  
  89.         This  means  that  it is a great warp,  or  hyper-space,  instruction:
  90. executing  a Loadall will jump you to someplace new, and leave you  with  your
  91. choice  of  register contents, status and mode settings,  and  memory  segment
  92. mappings,  allowing  you  to have your segments anywhere  in  the  16-megabyte
  93. address  space  of the 80286.  Those of you who are familiar with Unix  and  C
  94. programming  will be immediately reminded of the "longjump" routine.   Loadall
  95. is the ultimate long-jump.
  96.  
  97.         This  is possible in REAL mode.  You do NOT have to go into  protected
  98. mode  to get at memory above 1 Megabyte on the AT.  Which also means that  you
  99. don't have to then go through all kinds of odd-ball gyrations to get back  out
  100. of  protected mode.  And better yet, this instruction will work in  both  REAL
  101. and PROTECTED mode.
  102.  
  103.         Intel  included the Loadall instruction in the 80286 for chip  testing
  104. (they  can  throw the CPU into any state, and see if it then does what  it  is
  105. supposed  to  do),  but there are much better uses for it  than  that  (in  my
  106. not-so-humble opinion).
  107.  
  108.         The power of being able to re-program ANY and ALL of the registers  of
  109. the  CPU with one single instruction opens up a whole new world of  possibili-
  110. ties.
  111.  
  112.         Including, but not limited to:
  113.  
  114.         getting  at  all  the memory in your machine at will, even  if  it  is
  115. addressed above 1 megabyte, from real mode.
  116.  
  117.         executing real-mode programs in ram above one megabyte.
  118.  
  119.         installing a second operating-system-like program, or command  proces-
  120.  
  121. sor,  or shell, in memory above 1 megabyte, and alternating between  that  and
  122. DOS.
  123.  
  124.         installing most of the guts of custom TSR's, shells, and  device-driv-
  125. ers in ram above 1 megabyte (freeing up precious base memory), leaving in  low
  126. memory only the stubs to call the code upstairs.
  127.  
  128.         writing  very  large programs, which are "split", and  have  half  the
  129. program  residing  in  the low-down 640K, and the other half  up  in  extended
  130. memory, and running in either real or protected mode.
  131.  
  132.         installing  large  protected-mode programs in extended  memory,  where
  133. they  will not conflict with, or crowd out DOS, and ping-ponging between  them
  134. and DOS.
  135.  
  136.         switching to protected mode.
  137.  
  138.         emulating  real mode from protected mode (tough, and full of  gotchas,
  139. but still worth mentioning).
  140.  
  141.         this is really off-the-wall, but possible: building automata that  use
  142. Loadall  to  warp from state to state, sort of like a computer game  of  Life,
  143. played in the twilight zone.
  144.  
  145.         ? use your imagination. The sky's the limit.
  146.  
  147.  
  148.  
  149.         This  instruction opens up so many possibilities (AND creates so  many
  150. problems)  for things like alternate operating systems, and alternate  shells,
  151. that live above 1 megabyte, in real mode or protected mode, that I foresee the
  152. need  for  a community library of "Hyper-Space" subroutines, which  can  still
  153. work properly even though some segments are in outer space, or the 80286 is in
  154. protected  mode.  I would be happy to collect these, and pass on the  best  of
  155. them with future distributions of this book and software.
  156.  
  157.  
  158.  
  159.  
  160.                                    LOADALL
  161.  
  162.  
  163.         Okay, so what IS the Loadall instruction?
  164.  
  165.         Simple:
  166.  
  167.         *** 0F 05 hex ***
  168.  
  169.         So how does it work?  Well, I've already told you the gist of it:  all
  170. CPU  registers  are loaded from a 51-word table of data that starts  at  80:0h
  171. (absolute  24-bit  address 800h).  This address is one thing  that  cannot  be
  172. changed or re-programmed. It's hard-wired into the chip, and that's that.  And
  173. that's unfortunate, because all versions of anybody's DOS earlier than version
  174. 3.3 use that area for critical system code.
  175.  
  176.         Loadall takes no operands, and is just a two-byte instruction. All the
  177. "operands" for the instruction are obtained from the table at 80:0h.
  178.  
  179.         Just put "db 0Fh, 05" in your code stream, and watch the fun. But  you
  180. had better get that table right before you do, or else... (crash).
  181.  
  182.  
  183.                 ** THE LOAD TABLE **
  184.  
  185. -----------------------------------------------------------
  186. Address         Size            CPU register
  187.                 (words)
  188. -----------------------------------------------------------
  189.  
  190. 800             3       unused  (?? I don't believe it.)
  191. 806             1       MSW (Machine Status Word)
  192. 808             7       unused  (?? I don't believe it.)
  193. 816             1       TR (Task Register)
  194. 818             1       Flag Word
  195. 81A             1       IP (Instruction Pointer)
  196. 81C             1       LDT (Local Descriptor Table)
  197.  
  198. 81E             1       DS (Data Segment, or DS Selector)
  199. 820             1       SS (Stack Segment, or SS Selector)
  200. 822             1       CS (Code Segment, or CS Selector)
  201. 824             1       ES (Extra Segment, or CS Selector)
  202.  
  203. 826             1       DI (Destination Index)
  204. 828             1       SI (Source Index)
  205. 82A             1       BP (Base Pointer)
  206. 82C             1       SP (Stack Pointer)
  207.  
  208. 82E             1       BX (Data Register BX)
  209. 830             1       DX (Data Register BX)
  210. 832             1       CX (Data Register BX)
  211. 834             1       AX (Data Register BX)
  212.  
  213. 836             3       ES Descriptor Cache
  214. 83C             3       CS Descriptor Cache
  215. 842             3       SS Descriptor Cache
  216. 848             3       DS Descriptor Cache
  217.  
  218. 84E             3       GDTR
  219.                         (Global-Descriptor-Table Register)
  220.  
  221. 854             3       LDTDC
  222.                         (Local-Descriptor-Table Descriptor Cache)
  223.  
  224. 85A             3       IDTR
  225.                         (Interrupt-Descriptor-Table Register)
  226.  
  227. 860             3       TSSDC
  228.                         (Task-State-Segment Descriptor Cache)
  229.  
  230. total =         33h words == 102. bytes
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.                       THE DESCRIPTOR CACHE ENTRIES
  238.  
  239.                       (DSDC, SSDC,CSDC, and ESDC)
  240.  
  241.  
  242.  
  243.         Wait a minute, forward-referencing again!  What's a descriptor? You've
  244. already used that word up above, and never defined it.
  245.  
  246.         Well  okay. A segment descriptor is a four-word structure of  informa-
  247. tion that describes a segment.  A descriptor gives a segment's size and 24-bit
  248. starting  address, and has a byte of encoded information, called  the  "access
  249. byte", that describes the characteristics of the segment (like whether it is a
  250. code segment or a data segment, writable or write-protected, and so on).   And
  251. the  desciptor  also has a dummy zero word for upward compatibility  with  the
  252. 80386.  Segment Descriptors are used in protected mode, but not in real mode.
  253.  
  254.         In  protected  mode,  when you want to use a segment  of  memory,  you
  255. reference  the  segment descriptor.  The 80286 looks into a table  or  two  of
  256. descriptors (which can be quite large, up to 16384 entries), to find the right
  257. entry, and find out what the segment is. If you had to do this every time  you
  258. referenced a memory variable, it would be terribly slow.  In order to  prevent
  259. this  overhead, saving the descriptor information for the current segments  in
  260. quickly-accessible CPU registers is a must. That's what the descriptor  caches
  261. are for.
  262.  
  263.         But  you said they aren't used in real mode, right?  Right. The  soft-
  264. ware  descriptor tables aren't.  But the hardware descriptor caches are.   The
  265. Intel  book on the 80286 seems mighty thin when it comes to telling  you  what
  266. the  protected-mode  hardware does while in real mode, but some  of  it  still
  267. works,  and  is  very important (the descriptor caches  in  particular).   The
  268. descriptor caches determine where your segments really are, whether in real or
  269. protected mode.
  270.  
  271.         In  real  mode, your segments are all normally 64 Kbytes in  size,  by
  272. default,  and  are always located in the lowest megabyte of  the  80286's  16-
  273. megabyte  address space.  When you want to access a segment, you just  load  a
  274. number for the start of the segment into the appropriate segment register, and
  275. then read or write that segment of memory.
  276.  
  277.         The  segment number that you load is the address scaled down  by  four
  278. bits,  so that it really addresses a memory address that is sixteen times  the
  279. number  you  gave  it.  You can address anywhere inside  that  64  Kbyte-sized
  280. window by using an offset.
  281.  
  282.         Since the segment registers are 16 bits in size, and have been  scaled
  283. by four bits, you have the equivalent of 20-bit addressing, and can address  a
  284. 1-megabyte sized area.  That's real mode.
  285.  
  286.         Did it ever occur to you that that 1-megabyte sized area might  itself
  287. just be appearing somewhere inside of an even larger area?
  288.  
  289.         And that the 1-megabyte-sized real-mode area is made to start at  zero
  290. when the 80286 chip is reset, but doesn't have to stay there forever?
  291.  
  292.         I  mean, if in protected mode, the hardware is there to address a  16-
  293. megabyte-sized  address space, well, that hardware doesn't just go  away  when
  294. you are in real mode, does it?  Or all just get turned off?
  295.  
  296.         No,  it doesn't.  As a matter of fact, it still works just  fine,  but
  297. you  weren't given any instructions for doing anything with that part  of  the
  298. hardware from real mode.  Or were you?
  299.  
  300.         Oh yes you were. It's called LOADALL.
  301.  
  302.         So  how  do the hardware desciptor caches work?  Well, they  hold  the
  303. information  that was read from a (software) descriptor in memory.  The  80286
  304. discards  the unused zero word, and keeps the rest.  When you address  memory,
  305. you  are actually using the segment addresses in the descriptor  cache  regis-
  306. ters, not what is in the segment registers.
  307.  
  308.         Perhaps you thought you were using the segment registers for  address-
  309. ing:  it sure looks like you do, because if you load something into a  segment
  310. register, you will then address the memory that the segment register is point-
  311. ing to.  What is happening invisibly in the background is that the correspond-
  312. ing  descriptor cache is being updated whenever you load a  segment  register,
  313. and then the descriptor cache is being used for the actual addressing.
  314.  
  315.         So  the segment descriptor caches, and not the segment registers,  are
  316. what  actually  control what goes out on the address lines,  and  hence,  what
  317. memory  you will really address. And the addresses in the  segment  descriptor
  318. caches are 24-bit addresses.  Now isn't that special?
  319.  
  320.         So if we can use Loadall to load anything we want to into the  segment
  321. descriptor  caches,  then we can address anywhere in the  16-megabyte  address
  322. space of the 80286, right?  Right.  You got it.
  323.  
  324.  
  325.         The contents of the descriptor cache entries in a Loadall table are:
  326.  
  327.          The  absolute 24-bit address for the start of the  segment,
  328.                  in  the usual Intel  lowest-byte-first  byte-order.
  329.                  That is, the bytes are: lowest, middle, highest.
  330.  
  331.  
  332.          An  access byte, customarily set to 92h or 93h.  This  byte
  333.                  is  encoded in the usual way that access bytes  are
  334.                  encoded in Global Descriptor Table entries (see the
  335.                  accompanying  charts).   This  byte  describes  the
  336.                  characteristics of the segment, like whether it  is
  337.                  code or data, and write-protected or not.
  338.  
  339.          A  16-bit segment limit.  This is the segment  size,  minus
  340.                  one.  FFFFh is equal to a full 64K.
  341.  
  342.         The addresses loaded into the descriptor caches must be 24-bit  "abso-
  343. lute"  or un-segmented "flat-space" versions of the segment  start  addresses.
  344. Thus,  a  segment  address of 3456h becomes an absolute  address  of  034560h.
  345. Remember  that segment addresses are ordinarily scaled down by 4 bits.  So  we
  346. have to scale them back up to get the 24-bit flatland equivalent.
  347.  
  348.         This  ordering is exactly backwards, word-order-wise, from  the  usual
  349. layout  of the descriptors used in the protected-mode tables like  the  Global
  350. Descriptor Table.  This is because the Loadall instruction is essentially just
  351. a  giant  POP-ALL instruction.  The word order is backwards  (really,  "Stack-
  352. wards"), but the byte order within words is not reversed.
  353.  
  354.         The addresses loaded into the descriptor caches must be 24-bit  "abso-
  355. lute"  or un-segmented "flat-space" versions of the segment  start  addresses.
  356. IE: a segment address of 3456h becomes an absolute address of 034560h.  Remem-
  357. ber  that segment addresses are ordinarily scaled down by 4 bits.  So we  have
  358. to scale them back up to get the 24-bit flatland equivalent.
  359.  
  360.         You will notice that there seems to be some duplication of information
  361. here:  you have a CS register slot in the Loadall table, which is loaded  with
  362. the  desired  code  segment start address, and you also have  a  Code  Segment
  363. Descriptor  Cache  entry, with an address slot which is loaded with  much  the
  364. same information.  The same is also true of DS, SS, and ES.
  365.  
  366.         They can't always be the same, because one is 16 bits, and one 24, and
  367. the 24-bit descriptor cache entry can specify the address down to the byte, in
  368. the full 16-megabyte address space, while the 16-bit segment register can only
  369. address on 16-byte boundaries, and can't address beyond 1 megabyte.
  370.  
  371.         So  if  they are different, which ones win out?  The  answer  is,  the
  372. Descriptor Caches.  They have to, because only the Descriptor Caches have  the
  373. whole  24-bit address necessary for addressing the entire 16-megabyte  address
  374.  
  375. space of the 80286.  Also because the Descriptor Caches are what are  actually
  376. wired  to the address lines.  In protected mode, the segment  registers  don't
  377. even get close to the address lines.
  378.  
  379.         But  watch  out: this gets tricky.  For a  simple  rule-of-thumb,  the
  380. proper  programming practice to follow is: in real mode, always keep them  the
  381. same.  That is, where the bits of the two overlap, keep them the same.  The CS
  382. register  really holds the equivalent to bits A3 to A19 of the 24-bit  address
  383. in the CS Descriptor Cache, so there is no way that you can "keep them all the
  384. same".  But you can keep those bits the same, and you will want to.
  385.  
  386.         Why?  Because, in real mode, certain operations will update a Descrip-
  387. tor Cache using the contents of the paired Segment Register.  Oh yeh?  Yeh.
  388.  
  389.         Example:
  390.  
  391.         Even  if the code segment entry "CS" in a Loadall table  is  blatantly
  392. wrong,  but the value in the Code Segment Descriptor Cache "CSDC" is  correct,
  393. and  the Instruction Pointer "IP" value is correct, and you do a Loadall,  the
  394. Loadall will still work, and you WILL run the code that you intended to be run
  395. after  the Loadall, but the program will crash at the first  jump  instruction
  396. after  the  Loadall.  Calls to subroutines will likewise crash if  the  CS  is
  397. wrong.
  398.  
  399.         The  jump  or call instruction causes updating of  the  CS  Descriptor
  400. Cache  contents, using the contents of the CS register, and the offset in  the
  401. jump  or  call instruction.  So your CS descriptor cache goes  from  right  to
  402. wrong,  without any further help from you.  That's why you have to "keep  them
  403. the same".
  404.  
  405.         (This  is just a simple rule of thumb.  Like all simple  rules,  there
  406. are exceptions, and the rules can be broken.  Breaking these rules doesn't buy
  407. you  anything, but you might note that this is simply a rule of thumb,  not  a
  408. Commandment From On High.)
  409.  
  410.         The same is also true of the other Segment Registers, and their match-
  411. ing Descriptor Caches, although the instructions that will cause updating will
  412. differ.   The  commonest operation that causes updating  of  these  descriptor
  413. caches is loading a new segment value into a segment register.
  414.  
  415.         Now obviously, not all the bits of the address in the Descriptor Cache
  416. will be updated by such operations.  The highest 4 bits cannot be updated from
  417. the  segment register, because there are no corresponding bits.  So what  does
  418. it do with them?  In real mode, the worst.  It clears them.  Try doing a  jump
  419. while  executing real-mode code upstairs, above 1 Megabyte, and you will  come
  420. crashing down out of the sky.  A simple jump in code located way upstairs will
  421. turn  into  a very long jump to the lowest megabyte of memory.   Probably  not
  422. what you had in mind, at all.  Far jumps and far calls are out of the question
  423. for the same reason.  Curiously, a call will not cause you to fall out of  the
  424. sky  like a jump will, so we can do reversed calls, by shoving  a  destination
  425.  
  426. address onto the stack, and then doing a return, even where a jump to the same
  427. place will crash us.
  428.  
  429.         When  you are executing code in real mode, above 1 megabyte  (plus  64
  430. K), your position is as precarious as that of Icarus flying towards the sun on
  431. wings held together with wax.  (More on that "plus 64 K" note later.)
  432.  
  433.         And  the  updating  of the  lowest four bits is an  open  question.  I
  434. always  set  my segments on 16-byte boundaries so I don't  get  burned  there.
  435. That  is, the four lowest bits of the 24-bit address are always  zero.   Thus,
  436. the  16-bit  segment settings in the segment registers will always  match  the
  437. values of the lowest 20 bits of the descriptor cache settings.
  438.  
  439.         Here's what these descriptor cache entries look like, in source  code,
  440. with a set of default values plugged in:
  441.  
  442. newESDC         dw      0,      9200h,  0FFFFh
  443. newCSDC         dw      0,      9200h,  0FFFFh
  444. newSSDC         dw      0,      9200h,  0FFFFh
  445. newDSDC         dw      0,      9200h,  0FFFFh
  446.  
  447.         The running program will replace those zeroes in the first and  second
  448. words of each entry with real addresses before doing the Loadall.
  449.  
  450.         The "92"'s are the access bytes, and mean: "this item is a  descriptor
  451. of  a data segment, it is valid, it has the highest possible  privilege  level
  452. (0), writing to it is okay, and it has not been accessed" (really, written to.
  453. A 'dirty' page, in virtual-memory-system parlance).
  454.  
  455.         Those  "FFFF"'s set up segments 64K in size. There's no point in  set-
  456. ting  them  any smaller, and a lot of grief to be gotten if you  do.  So  just
  457. always set them to "FFFF" in real mode.
  458.  
  459.  
  460.                        THOSE OTHER BIG REGISTERS
  461.  
  462.         GDTR    Global Descriptor Table Register
  463.         LDTDC   Local-Descriptor-Table Descriptor Cache
  464.         IDTR    Interrupt Descriptor Table Register
  465.         TSSDC   Task-State-Segment Descriptor Cache
  466.  
  467.         The format of the data for these registers is just about identical  to
  468. the  format  of the data in the Descriptor Caches, except for an  unused  byte
  469. (there is no access byte):
  470.  
  471.         an absolute 24-bit address for table start, in the
  472.                 usual Intel byte-order. That is, the bytes
  473.                 are: lowest, middle, highest.
  474.  
  475.         an "extra", or "trash", or "dummy" byte (pick your
  476.                 favorite name.)  Set to either FFh or 0.
  477.  
  478.         a 16-bit limit.  This is the table size, minus one.
  479.                 (FFFFh == a full 64K)
  480.  
  481.  
  482.        Set up the GDTR (Global Descriptor Table Register) and the IDTR (Inter-
  483. rupt  Descriptor Table Register) using the instructions "sgdt" and  "sidt"  --
  484. "store  global  descriptor table register", and  "store  interrupt  descriptor
  485. table register". These two instructions work in both real and protected mode.
  486.  
  487.        The  values that we get from them are somewhat goofy (especially  since
  488. we  are getting data about non-existent tables), but we use those values  any-
  489. way, just to keep the 80286 chip happy.
  490.  
  491.        The  LDTDC (Local-Descriptor-Table Descriptor Cache) is a real  nothing
  492. in real mode.  In real mode, there is no Local Descriptor-Table Descriptor  to
  493. cache.  We just set the LDTDC with an acceptable size, same as the GDTR (88h),
  494. and let it go at that.
  495.  
  496.        The  TSSDC  (Task-State-Segment Descriptor Cache) is  likewise  a  null
  497. register in real mode.  There is no Task State Segment to point to. Again,  we
  498. just  set  it up with a size that will keep the 80286 chip from  freaking  out
  499. (thinking that the segment is impossibly small), and let it go at that.
  500.  
  501.        Set up, just before doing a Loadall, these items will look like:
  502.  
  503. newGDTR         dw      D8A0h,  0FF00h, 88h
  504. newLDTDC        dw      0,      0FF0Eh, 88h
  505. newIDTR         dw      0,      0FF00h, 0FFFFh
  506. newTSSDC        dw      4000h,  0FF0Eh, 800h
  507.  
  508.  
  509.  
  510.                   AND ALL THOSE OTHER LITTLE REGISTERS
  511.  
  512.  
  513.         The  MSW  (Machine Status Word) is normally set to zero.  Only  the  4
  514. lowest  bits are even used.  The one super-important bit in this  register  is
  515. the mode bit.  Set it, and you warp into protected mode.  The other three bits
  516. are invalid and irrelevant if you are not in protected mode.  Zero this  word,
  517. unless you really intend to go into unreal mode.  Heaven help your program  if
  518. you  set it, and have not set up all the descriptor tables, and all  the  pro-
  519. tected-mode  registers, and cross-linked all the pointers to everything,  cor-
  520. rectly, first.  We will get into that can of worms later.
  521.  
  522.         Just for reference, here's what the bits are:
  523.  
  524.         D0      == PE Protected-Mode Enable  (yeh, this is IT.)
  525.         D1      == MP Monitor Process
  526.         D2      == EM Emulate Processor Extension
  527.         D3      == TS Task Switched
  528.  
  529.  
  530.         The  TR  (Task Register) is another register used  only  in  protected
  531. mode. It is used for keeping track of which task is running.  Not our  problem
  532. in real mode.  Zero it.
  533.  
  534.         The  Flag Word is the same old flag word that we are already  familiar
  535. with  from ordinary real-mode programming.  We just push the flags word  here,
  536. and  we've done it.  Or we can zero it.  None of our programs are going to  do
  537. anything as off-the-wall as a conditional jump right after a Loadall,  anyway,
  538. right?  Uh, right?  Why do I see you grinning?  12-dimensional Life, huh?
  539.  
  540.         The IP (Instruction Pointer) is critical.  This one really works.  The
  541. address we put here will, in combination with the address in the Code  Segment
  542. Descriptor Cache (CSDC), determine where we will start executing code  immedi-
  543. ately after the Loadall.  So this acts like a jump vector.  We set this up  in
  544. the  our programs, just before doing a Loadall, do determine where we will  go
  545. next.  Better get this one right.
  546.  
  547.         The  LDT  (Local Descriptor Table) is another null  register  in  real
  548. mode.  Zero it.
  549.  
  550.  
  551.         DS (Data Segment, or DS Selector)
  552.         SS (Stack Segment, or SS Selector)
  553.         CS (Code Segment, or CS Selector)
  554.         ES (Extra Segment, or CS Selector)
  555.         Set these up so that they contain the same number as bits A4 to A19 of
  556. the  corresponging Segment Descriptor Cache.  These work in  conjunction  with
  557. those.  Note that these must be correct.
  558.  
  559.  
  560.  
  561.         All  of the following registers are very straight-forward:  just  load
  562. them  with whatever you want the registers to have after the Loadall.  If  you
  563. are  not trying to carry values in these registers, you can just default  most
  564. all of them to zeroes.
  565.  
  566.         The stack pointer requires some care, as the stack is one of the  best
  567. ways to carry data into the beyond.  I generally stuff the stack just before a
  568. Loadall,  and then write the current stack pointer to the SP slot in the  Loa-
  569. dall table, so that I know that I have it right.
  570.  
  571.         DI (Destination Index)
  572.         SI (Source Index)
  573.         BP (Base Pointer)
  574.         SP (Stack Pointer)
  575.  
  576.         BX (Data Register BX)
  577.         DX (Data Register DX)
  578.         CX (Data Register CX)
  579.         AX (Accumulator AX)
  580.  
  581.  
  582.         And then these little curiosities: the two "dead" spots in the table.
  583.  
  584. 800     3 words         unused   (?? I don't believe it.)
  585. 808     7 words         unused   (?? I don't believe it.)
  586.  
  587.         Obviously, they are there for something.  They must load some  invisi-
  588. ble register or other.  The registers might be some very transient  registers,
  589. just for intermediate products, which may not be useful...
  590.  
  591.         Then  again,  considering how much we haven't been told so  far,  they
  592. might be good for something.  This is another area for future experimentation.
  593. In the mean time, zero them.
  594.  
  595.  
  596.                   AND A PRETTY-TOGETHER DEFAULT TABLE
  597.  
  598.  
  599.         So  here's  what  a  default  Loadall  table  looks  like.  Note  that
  600. "new_Reg_Buf" doesn't label any data item that we really use; it's the name of
  601. the whole table.
  602.  
  603. ;    LOADALL Register Load Table for new values to be loaded
  604. ;    into registers by a Loadall.
  605.  
  606. new_Reg_Buf     dw      3 dup (0)       ; unused space
  607. newMSW          dw      0
  608. newDead         dw      7 dup (0)       ; unused space
  609. newTR           dw      0
  610. newFlagWord     dw      0
  611. newIP           dw      offset after_ldall      ; * may chng
  612. newLDT          dw      0
  613.  
  614. newDS           dw      0       ; *chng
  615. newSS           dw      0       ; *chng
  616. newCS           dw      0       ; *chng
  617. newES           dw      0       ; *chng
  618.  
  619. newDI           dw      0
  620. newSI           dw      0
  621. newBP           dw      0
  622. newSP           dw      0       ; *chng
  623.  
  624. newBX           dw      0
  625. newDX           dw      0
  626. newCX           dw      0
  627. newAX           dw      0
  628.  
  629. newESDC         dw      0,      9300h,  0FFFFh  ; *chng
  630. newCSDC         dw      0,      9300h,  0FFFFh  ; *chng
  631. newSSDC         dw      0,      9300h,  0FFFFh  ; *chng
  632. newDSDC         dw      0,      9300h,  0FFFFh  ; *chng
  633.  
  634. newGDTR         dw      D8A0h,  0FF00h, 88h     ; @ 0D8A:0 *n
  635. newLDTDC        dw      0,      0FF0Eh, 88h     ; @ E000:0
  636. newIDTR         dw      0,      0FF00h, 0FFFFh  ; @ 0000:0 *n
  637. newTSSDC        dw      4000h,  0FF0Eh, 800h    ; @ E400:0
  638.  
  639.  
  640.         Those  "*chng" comments mean that those items MUST be changed  by  the
  641. running program before actually doing the Loadall. We cannot correctly default
  642. them in the sources because the correct values can only be determined at  run-
  643. time.
  644.  
  645.  
  646.  
  647.         The "*n" means that those values are not really in the default  tables
  648. in the sources: the running program uses the sgdt and sidt instructions to get
  649. those values and then plugs them into those two entries.  Just letting you see
  650. what they will look like. You could have anything in the original table there,
  651. because  the running program will over-write those items with  correct  values
  652. anyway.
  653.  
  654.         The "@ 0D8A:0" comments are just noting the addresses in those  items,
  655. in a more readable form.
  656.  
  657.  
  658.  
  659.  
  660.                         GATE A20 : Door to the Beyond
  661.  
  662.  
  663.         Before  we get heavy into the guts of actually using the  Loadall  in-
  664. struction, we need to touch on this item: Gate A20.  Loadall is almost useless
  665. without control of Gate A20.
  666.  
  667.         Gate  A20  is the gate on the motherboard of the AT  that  enables  or
  668. disables  the  4  highest  address  lines, A20 to A23.  In  order  to  be  PC-
  669. compatible, they are ordinarily disabled on an AT.  The pathetic PC could only
  670. address 1 megabyte of space, total, remember?  That's 20 bits.  If those lines
  671. are disabled, then addressing wraps to zero above FFFF:0010.  But if they  are
  672. enabled,  then addressing doesn't wrap, and you can address above 1  Megabyte.
  673. This  has nothing to do with protected mode.  Even if the 80286 were  in  pro-
  674. tected mode, it still couldn't address above 1 Megabyte without enabling  Gate
  675. A20.
  676.  
  677.         In  the part of the Hyper-Space Library freely distributed  with  this
  678. document  and the View-XM program are routines called "A20_on" and  "A20_off".
  679. They  need no arguments.  You just call them, and they will enable or  disable
  680. Gate A20.  Do not make a habit of turning Gate A20 on and just leaving it  on,
  681. as  rumor has it that some barbaric programmers from the bad-old days  made  a
  682. habit  of depending on address-wrapping, addressing something like  FFFF:0345h
  683. to  get at 0:0335h.  Ugh!  These subroutines also check whether Gate  A20  was
  684. already on before the call, and if so, leave it alone.
  685.  
  686.         This  leads us to a very interesting twist in the game:  what  if  you
  687. turn  on Gate A20, and load FFFFh into a segment register, like the DS  regis-
  688. ter,  and then address something like DS:0300h?  The answer is, you  will  ad-
  689. dress  beyond 1 megabyte, without either going into protected mode,  or  using
  690. Loadall  tricks.  The  PC can only address 1 Megabyte total, but  the  AT  can
  691. address 1 Megabyte, plus 64K, minus 16 bytes, in REAL mode, without Loadall.
  692.  
  693.         This a big part of the XMS driver specification.  That's the  eXtended
  694. Memory Specification (not to be confused with the "EMS" Expanded Memory Speci-
  695. fication).  The XMS driver accesses memory addressed above 1 Megabyte on AT's.
  696.  
  697.         You can write programs which use standardized calls to the XMS driver,
  698. and  expect that the program will work with anyone's XMS  driver.   Microsoft,
  699. Intel, Lotus, and AST Research (the authors) have put the XMS specification in
  700. the  public domain (although they retain the copyright), and it  is  currently
  701. supported  by them, and probably by many more companies that I don't know  of,
  702. so we should be seeing plenty of good XMS device-drivers around, and, in turn,
  703. programs using it.
  704.  
  705.         Furthermore,  Microsoft  will give you a copy of the XMS  driver,  and
  706. standard,  free,  if you write to them and ask for one.   Write  to  Microsoft
  707.  
  708. Corporation,  16011  NE 36th Way, Box 97017, Redmond WA  98073,  and  politely
  709. request a floppy copy of the XMS standard and driver.  Thank you, Microsoft.
  710.  
  711.         Since  you  want a nice, clean, non-colliding standard  way  for  your
  712. programs to be able to get at more ram, using the EMS and XMS standards is the
  713. only  good  way to go.  Throughout this book, we are going  to  support  those
  714. standards, and others, too.
  715.  
  716.         The  recommended  programming practice is to always  support  the  XMS
  717. standard, and use requests to the XMS driver to get at extended memory, rather
  718. than just brute-force doing it yourself, even though you can with Loadall,  so
  719. that your programs will not conflict with others.
  720.  
  721.         The PC world is already far too filled with gotchas and  incompatibli-
  722. ties,  and things that collide with other things, for us to be adding  to  the
  723. misery.
  724.  
  725.         The one thing that the XMS driver adds, that you will not have if  you
  726. just  take  over and use an area of extended memory yourself, is any  kind  of
  727. collision  prevention  or co-ordination between programs.  You won't  know  if
  728. another  program is already using that area, but the XMS driver will, as  long
  729. as the other program is also using the driver.  So everybody better be  adher-
  730. ing to the standard!
  731.  
  732.         On  the  other  hand, you would not be reading  this  book  about  the
  733. "secret"  Loadall  instruction if you were all that committed  to  ONLY  using
  734. "normal" standards, would you?  The trick is to support the standards, without
  735. being  constrained  by them.  This requires great care and thought  about  the
  736. consequences of any use of Loadall for "non-standard" activities.
  737.  
  738.         And so what do the XMS drivers use to get at the extended memory above
  739. the High-Memory Area?  Either going into protected mode, or Loadall.
  740.  
  741.  
  742.  
  743.  
  744.                     THE PROCEDURE FOR USING LOADALL
  745.                        (the ultra-safe procedure)
  746.  
  747.  
  748. 1.      Save  the  original machine state, so you have a state to  return  to.
  749. This information can be saved in a Loadall table, which is the most convenient
  750. form for later use.
  751.  
  752. 2.      Disable interrupts.  Just in case.  We want a clean copy of area 80.
  753.  
  754. 3.      Save  the 102-byte (33h words) block of data located at  80:0h.   Ver-
  755. sions  of DOS (both PC- and MS-) earlier than 3.3 use this area  for  critical
  756. system code, and as of DOS 3.3, RamDrive.Sys, and Himem.Sys use this area  for
  757. their own Loadall tables.
  758.  
  759. 4.      Re-enable  interrupts.   Let the clock ticks,  or  whatever,  through,
  760. while we do the following step.
  761.  
  762. 5.      Set  up  the new Loadall table (new_reg_buf), which  defines  the  new
  763. state we want to warp to.
  764.  
  765. 6.      Disable Interrupts.
  766.  
  767. 7.      Copy the new Loadall table to 80:0h.
  768.  
  769. 8.      Execute a Loadall.
  770.  
  771. 9.      Do  something  or other with your new machine state.   Read  or  write
  772. extended memory, run code upstairs, or whatever.
  773.  
  774. 10.     Copy the "old" Loadall table, containing the saved machine state, down
  775. to 80:0.
  776.  
  777. 11.     Do  another Loadall (Un-Loadall.)  This restores the original  machine
  778. state.
  779.  
  780. 12.     Copy the block of saved data back to 80:0h.
  781.  
  782. 13.     Re-enable interrupts.
  783.  
  784.         And you have done it.
  785.  
  786.  
  787.         This is the long, drawn-out method.  There are various short-cuts  and
  788. speedups possible.
  789.  
  790.         If all you have been doing is reading or writing extended memory,  for
  791. instance,  then  you  don't have to do the second loadall.   Just  changing  a
  792. segment register (loading a new value) will cause the corresponding Descriptor
  793. Cache  to drop its four highest address bits, restoring addressing to the  low
  794. megabyte.  See the sources for the program "View-XM" for more details on this.
  795.  
  796.         See the full text of The Hyper-Space Navigator's Guide for more.
  797.  
  798.  
  799.  
  800.  
  801.         The Hyper-Space Navigator's Guide book and software library is  avail-
  802. able from Semi-Intelligent Systems for $134.95, and is available on any common
  803. floppy format: 5.25" 360K or 1.2MB, or 3.5" 720K or 1.44MB.  If you order  it,
  804. please state your format preference.
  805.  
  806.         FULL source code in assembly and C is provided.
  807.  
  808.         The Hyper-Space Navigator's Guide gives the full low-down on  Loadall,
  809. and other extended-memory tricks, too:  the good, the bad, and the ugly.
  810.  
  811.         The  book comes with a library of subroutines designed  to  facilitate
  812. the use of extended memory, and includes numberous demo programs which do just
  813. about everything you can do with Loadall, including:
  814.  
  815.         reading and writing extended memory.
  816.  
  817.         running code up there, in both real and protected mode.
  818.  
  819.         going  into,  and running in, and then getting back out  of  protected
  820. mode, from within your own programs.
  821.  
  822.         writing  "split"  programs,  with a low-memory half, and  a  high-  or
  823. extended-memory half, with the second half in real or protected mode.
  824.  
  825.         installing  either real- or protected-mode "high code" inside  an  ex-
  826. tended-memory  ram-disk file, where it won't collide with anything  else,  and
  827. then using a TSR to launch directly into running the code from in there  (thus
  828. turning a piece of the ram-disk back into ram).
  829.  
  830.         Again,  full and complete source code, so that the demo programs  also
  831. supply  you  with hackable skeletons for quickly building your  own  programs,
  832. (without  the  many months of hair-tearing I went through to figure  out  this
  833. stuff).
  834.  
  835.