home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / library / cpu / vu_xm1c / hyprspac.doc < prev    next >
Text File  |  1990-05-30  |  41KB  |  892 lines

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