home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / devddemo.zip / GDTMEM.C < prev    next >
Text File  |  1991-01-31  |  12KB  |  268 lines

  1. /******************************************************************************/
  2. /* Routines for managing GDT accessed memory                                  */
  3. /*                                                                            */
  4. /* -------------------------------------------------------------------------- */
  5. /*                                                                            */
  6. /* unsigned int get_gdt_slots(count,loc)                                      */
  7. /* int count;                                                                 */
  8. /* char *loc;                                                                 */
  9. /*                                                                            */
  10. /* This function will allocate count GDT selectors and store the selector     */
  11. /* values in the array pointed to by loc.  If successful, it returns 0.  If   */
  12. /* unsuccessful, it returns 1.                                                */
  13. /*                                                                            */
  14. /* -------------------------------------------------------------------------- */
  15. /*                                                                            */
  16. /* char *phys_to_gdt(loc,count,sel)                                           */
  17. /* _32bits loc;                                                              */
  18. /* unsigned count; (0 = 65536)                                                */
  19. /* unsigned sel;                                                              */
  20. /*                                                                            */
  21. /* This function will make a physical block of memory addressable via the GDT.*/
  22. /* It makes the GDT selector sel point to a valid descriptor for the memory   */
  23. /* block at loc of size count.  It returns a valid far pointer to the memory  */
  24. /* if successful, 0000:0000 if failure.                                       */
  25. /*                                                                            */
  26. /* -------------------------------------------------------------------------- */
  27. /*                                                                            */
  28. /* _32bits get_phys_addr(addr)                                               */
  29. /* char *addr;                                                                */
  30. /*                                                                            */
  31. /* This function will return the physical address of the virtual address      */
  32. /* passed as a paramter.  If the address is invalid, it returns 00000000.     */
  33. /*                                                                            */
  34. /* -------------------------------------------------------------------------- */
  35. /*                                                                            */
  36. /* char *alloc_gdt_mem(count,sel)                                             */
  37. /* unsigned count;                                                            */
  38. /* unsigned sel;                                                              */
  39. /*                                                                            */
  40. /* This function will allocate a block of memory from the free pool and make  */
  41. /* it addressable via the GDT.  The block size is specified by count and the  */
  42. /* GDT selector to use is sel.  NOTE: The GDT selector must have been         */
  43. /* previously allocated via get_gdt_slots.  The return value is a valid       */
  44. /* far pointer to the memory block if successful or 0000:0000 if failure.     */
  45. /*                                                                            */
  46. /* -------------------------------------------------------------------------- */
  47. /*                                                                            */
  48. /* char *alloc_mem(count)                                                     */
  49. /* unsigned count;                                                            */
  50. /*                                                                            */
  51. /* This function will allocate a block of memory from the free pool and       */
  52. /* return its physical address.  If the allocate fails, it returns 00000000   */
  53. /*                                                                            */
  54. /* -------------------------------------------------------------------------- */
  55. /*                                                                            */
  56. /* char *alloc_big_mem(count)                                                 */
  57. /* unsigned long count;                                                       */
  58. /*                                                                            */
  59. /* This function will allocate a big block of memory from the free pool and   */
  60. /* return its physical address.  If the allocate fails, it returns 00000000   */
  61. /*                                                                            */
  62. /* -------------------------------------------------------------------------- */
  63. /*                                                                            */
  64. /* int free_mem(address)                                                      */
  65. /* _32bits address;                                                          */
  66. /*                                                                            */
  67. /* This function will give the block of memory starting at address back to    */
  68. /* OS/2.  If the free is successful, the return value is 0.  If it fails,     */
  69. /* the return value is -1.                                                    */
  70. /*                                                                            */
  71. /* -------------------------------------------------------------------------- */
  72. /*                                                                            */
  73. /* int verify_acc(addr,size,acc_type)                                         */
  74. /* unsigned *addr;                                                            */
  75. /* unsigned size;                                                             */
  76. /* unsigned acc_type;                                                         */
  77. /*                                                                            */
  78. /* This function will verify the current LDT access of the segment.  It       */
  79. /* returns -1 if access is denied and 0 OK.                                   */
  80. /*                                                                            */
  81. /******************************************************************************/
  82. #include "demo.h"
  83.  
  84. word far get_gdt_slots(word count, _32bits loc)
  85. {
  86.   union cpu_regs in_regs,out_regs;
  87.  
  88.   in_regs.W.ES = loc._segadr.segment;      /* ES:DI point to the GDT slot    */
  89.   in_regs.W.DI = loc._segadr.offset;       /*  array to be filled by OS/2    */
  90.   in_regs.W.CX = count;                    /* How many GDT slots to get      */
  91.   in_regs.B.DL = devhlp_AllocGDTSelector;  /* The DevHlp command             */
  92.   in_regs.W.es_valid = TRUE;               /* Use the struc value of ES      */
  93.   in_regs.W.ds_valid = FALSE;              /* Use the register value of DS   */
  94.   dev_help(&in_regs,&out_regs);            /* GO                             */
  95.   if ((out_regs.W.flags & 0x0001) != 0) {  /* Check for failure              */
  96.      return(1);
  97.      }
  98.   return(0);                               /* Success return path            */
  99. }
  100.  
  101. _32bits far phys_to_gdt( _32bits loc, word count, word sel)
  102. {
  103.   union cpu_regs in_regs,out_regs;
  104.   _32bits pointer;
  105.  
  106.   in_regs.W.AX = loc._2words.high;         /* AX:BX hold the phys address    */
  107.   in_regs.W.BX = loc._2words.low;
  108.   in_regs.W.SI = sel;                      /* The selector to use            */
  109.   in_regs.W.CX = count;                    /* The size of the segment (0=64K)*/
  110.   in_regs.B.DL = devhlp_PhysToGDTSelector; /* The DevHlp command             */
  111.   in_regs.W.es_valid = FALSE;              /* Neither DS or ES in the struct */
  112.   in_regs.W.ds_valid = FALSE;              /*   is a valid selector          */
  113.   dev_help(&in_regs,&out_regs);            /* Do it                          */
  114.   if ((out_regs.W.flags & 0x0001) != 0) {  /* Check for failure              */
  115.      pointer.phys = 0L;                    /* Return a NULL pointer if fail  */
  116.      return(pointer);
  117.      }
  118.   pointer._2words.high = sel;              /* Return a valid pointer if OK   */
  119.   pointer._2words.low = 0;
  120.   return(pointer);
  121. }
  122.  
  123. _32bits far alloc_gdt_mem( word count, word sel)
  124. {
  125.   _32bits phys_addr;
  126.  
  127.   /* allocate the memory */
  128.   phys_addr = alloc_mem(count);
  129.   if (phys_addr.phys == 0L) {
  130.      return(phys_addr);
  131.      }
  132.  
  133.   /* Associate the GDT selector with the memory */
  134.   return(phys_to_gdt(phys_addr,count,sel));
  135.  
  136. }
  137.  
  138. _32bits far alloc_mem(word count)
  139. {
  140.   union cpu_regs in_regs,out_regs;
  141.   _32bits pointer;
  142.  
  143.   /* allocate the memory */
  144.   in_regs.W.AX = 0;
  145.   in_regs.W.BX = count;
  146.   in_regs.B.DH = 0;
  147.   in_regs.B.DL = devhlp_AllocPhys;
  148.   in_regs.W.es_valid = FALSE;
  149.   in_regs.W.ds_valid = FALSE;
  150.   dev_help(&in_regs,&out_regs);
  151.   if ((out_regs.W.flags & 0x0001) != 0) {
  152.      pointer.phys = 0L;
  153.      return(pointer);
  154.      }
  155.   pointer._2words.high = out_regs.W.AX;
  156.   pointer._2words.low = out_regs.W.BX;
  157.   return(pointer);
  158.  
  159. }
  160.  
  161. _32bits far alloc_big_mem( unsigned long count)
  162. {
  163.   union cpu_regs in_regs,out_regs;
  164.   _32bits temp;
  165.   _32bits pointer;
  166.  
  167.   temp.phys = count;
  168.  
  169.   /* allocate the memory */
  170.   in_regs.W.AX = temp._2words.high;
  171.   in_regs.W.BX = temp._2words.low;
  172.   in_regs.B.DH = 0;
  173.   in_regs.B.DL = devhlp_AllocPhys;
  174.   in_regs.W.es_valid = FALSE;
  175.   in_regs.W.ds_valid = FALSE;
  176.   dev_help(&in_regs,&out_regs);
  177.   if ((out_regs.W.flags & 0x0001) != 0) {
  178.      pointer.phys = 0L;
  179.      return(pointer);
  180.      }
  181.   pointer._2words.high = out_regs.W.AX;
  182.   pointer._2words.low = out_regs.W.BX;
  183.   return(pointer);
  184.  
  185. }
  186.  
  187. word far free_mem( _32bits address)
  188. {
  189.   union cpu_regs in_regs,out_regs;
  190.  
  191.   /* allocate the memory */
  192.   in_regs.W.AX = address._2words.high;
  193.   in_regs.W.BX = address._2words.low;
  194.   in_regs.B.DL = devhlp_FreePhys;
  195.   in_regs.W.es_valid = FALSE;
  196.   in_regs.W.ds_valid = FALSE;
  197.   dev_help(&in_regs,&out_regs);
  198.   if ((out_regs.W.flags & 0x0001) != 0) {
  199.      return(-1);
  200.      }
  201.   return(0);
  202.  
  203. }
  204.  
  205. _32bits far get_phys_addr( _32bits addr)
  206. {
  207.   union cpu_regs in_regs,out_regs;
  208.   _32bits pointer;
  209.  
  210.   /* allocate the memory */
  211.   in_regs.W.SI = addr._segadr.offset;
  212.   in_regs.W.DS = addr._segadr.segment;
  213.   in_regs.B.DL = devhlp_VirtToPhys;
  214.   in_regs.W.es_valid = FALSE;
  215.   in_regs.W.ds_valid = TRUE;
  216.   dev_help(&in_regs,&out_regs);
  217.   if ((out_regs.W.flags & 0x0001) != 0) {
  218.      pointer.phys = 0L;
  219.      return(pointer);
  220.      }
  221.   pointer._2words.high = out_regs.W.AX;
  222.   pointer._2words.low = out_regs.W.BX;
  223.   return(pointer);
  224.  
  225. }
  226.  
  227. _32bits far get_phys_addr1( _32bits addr)
  228. {
  229.   union cpu_regs in_regs,out_regs;
  230.   _32bits pointer;
  231.  
  232.   /* allocate the memory */
  233.   in_regs.W.SI = addr._segadr.offset;
  234.   in_regs.W.DS = addr._segadr.segment;
  235.   in_regs.B.DL = devhlp_VirtToPhys;
  236.   in_regs.W.es_valid = FALSE;
  237.   in_regs.W.ds_valid = TRUE;
  238.   dev_help1(&in_regs,&out_regs);
  239.   if ((out_regs.W.flags & 0x0001) != 0) {
  240.      pointer.phys = 0L;
  241.      return(pointer);
  242.      }
  243.   pointer._2words.high = out_regs.W.AX;
  244.   pointer._2words.low = out_regs.W.BX;
  245.   return(pointer);
  246.  
  247. }
  248.  
  249. word far verify_acc( _32bits addr, word size, word acc_type)
  250. {
  251.   union cpu_regs in_regs,out_regs;
  252.  
  253.   /* allocate the memory */
  254.   in_regs.W.CX = size;
  255.   in_regs.W.DI = addr._segadr.offset;
  256.   in_regs.W.AX = addr._segadr.segment;
  257.   in_regs.B.DH = (byte)acc_type;
  258.   in_regs.B.DL = devhlp_VerifyAccess;
  259.   in_regs.W.es_valid = FALSE;
  260.   in_regs.W.ds_valid = FALSE;
  261.   dev_help(&in_regs,&out_regs);
  262.   if ((out_regs.W.flags & 0x0001) != 0) {
  263.      return(-1);
  264.      }
  265.   return(0);
  266.  
  267. }
  268.