home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RSCOPE2.ZIP / PHYSMEM.ASM < prev    next >
Assembly Source File  |  1988-12-04  |  9KB  |  280 lines

  1. ;**********************************************************************
  2. ;*                                                                    *
  3. ;*                         FARPOINT SOFTWARE                          *
  4. ;*                         -----------------                          *
  5. ;*                     physmem.asm * version 1.11                     *
  6. ;*                                                                    *
  7. ;*                                                                    *
  8. ;*  A device driver whose purpose is to provide a means for an        *
  9. ;*  application to access physical memory addresses by calling        *
  10. ;*  PhysToUVirt for it.                                               *
  11. ;*                                                                    *
  12. ;*                                                                    *
  13. ;*  The calling sequence to this driver is as follows (Large Model):  *
  14. ;*                                                                    *
  15. ;*    ULONG ulPhys;                                                   *
  16. ;*    void *pvVirt;                                                   *
  17. ;*    USHORT action;                                                  *
  18. ;*    HFILE handle;                                                   *
  19. ;*                                                                    *
  20. ;*    /* ulPhys contains the physical address and pvVirt is the       *
  21. ;*        variable to receive the virtual address               */    *
  22. ;*                                                                    *
  23. ;*    DosOpen (&"PHYSMEM$", &handle, &action, 0L, 0, 1, 0x0040, 0L);  *
  24. ;*    DosDevIOCtl (&pvVirt, &ulPhys, 0x40, 0x80, handle);             *
  25. ;*                                                                    *
  26. ;*    /*                                                              *
  27. ;*        .                                                           *
  28. ;*        .                                                           *
  29. ;*        .                                                           *
  30. ;*       Use the memory via pvVirt                                    *
  31. ;*        .                                                           *
  32. ;*        .                                                           *
  33. ;*        .                                                           *
  34. ;*                   */                                               *
  35. ;*                                                                    *
  36. ;*    DosDevIOCtl (&pvVirt, &ulPhys, 0x41, 0x80, handle);             *
  37. ;*    DosClose (handle);                                              *
  38. ;*                                                                    *
  39. ;* ------------------------------------------------------------------ *
  40. ;*                                                                    *
  41. ;* Other functions:                                                   *
  42. ;*                                                                    *
  43. ;*    "DosDevIOCtl (&pvVirt, &ulPhys, 0x42, 0x80, handle);"           *
  44. ;*                                                                    *
  45. ;*      returns with pointers set to point to the Global              *
  46. ;*       Descriptor Table.                                            *
  47. ;*                                                                    *
  48. ;*                                                                    *
  49. ;*    "DosDevIOCtl (&pvVirt, &ulPhys, 0x43, 0x80, handle);"           *
  50. ;*                                                                    *
  51. ;*      returns with pointers set to point to the Interrupt           *
  52. ;*       Descriptor Table.                                            *
  53. ;*                                                                    *
  54. ;**********************************************************************
  55.  
  56. .286p
  57. .SEQ
  58.  
  59.     extrn    DosWrite:far
  60.  
  61. ;-------------------------------------------
  62.  
  63. ADATA    SEGMENT    PARA PUBLIC 'AUTO'
  64.  
  65. nexthdr        dd    0FFFFFFFFh        ;pointer to next device driver
  66. devattr        dw    8140h             ;attribute flags
  67. stratof        dw    offset strategy   ;offset of strategy routine entry
  68. reserv1        dw    0
  69. devname        db    'PHYSMEM$'        ;device name for "DosOpen"
  70. reserv2        db    8 dup (0)
  71.  
  72. devhelp        dd    0         ;this is where we save the DevHelp pointer
  73.  
  74. gdt_phys    dw    3 dup (0)         ;scratch memory for GDT
  75. idt_phys    dw    3 dup (0)         ;scratch memory for IDT
  76.  
  77. end_of_data    label    byte              ;the rest isn't needed after init
  78.  
  79. initmsg        db    0Dh,0Ah
  80.         db    'Absolute Physical Memory Access Device Driver',0Dh,0Ah
  81.         db    'Version 1.11 * Copyright (c) 1988, Farpoint Software',0Dh,0Ah
  82.         db    0Dh,0Ah
  83. initmsglen    equ    $-offset initmsg
  84. byteswritten    dw    0
  85.  
  86. ADATA    ENDS
  87.  
  88. DGROUP    GROUP    ADATA
  89.  
  90. ;-------------------------------------------
  91.  
  92. CODE    SEGMENT    PARA    'CODE'
  93.  
  94.     ASSUME    CS:CODE,DS:ADATA
  95.  
  96. strategy    proc    far
  97.  
  98. ;examine command code in req packet
  99.  
  100.     mov    al,es:[bx+2]
  101.     cmp    al,0                   ;"initialize" command
  102.     je    initializej
  103.     cmp    al,16                  ;"IOCtl" command
  104.     je    generic_ioctl
  105.  
  106. ;if none of the above, execute default stuff
  107.  
  108.     mov    word ptr es:[bx+3],0100h    ;set the "done" flag
  109.     ret
  110.  
  111. initializej:    jmp    initialize
  112.  
  113. ; * * * generic IOCtl command * * *
  114.  
  115. generic_ioctl:
  116.  
  117.     ;check the function code:
  118.  
  119.     mov    al,es:[bx+14]
  120.     cmp    al,40h                 ;translate physical to virtual
  121.     je    acquire
  122.     cmp    al,41h                 ;release virtual selector acquired
  123.                                ; previously with 40h, 42h, or 43h
  124.     je    releasej
  125.     cmp    al,42h                 ;get global descriptor table
  126.     je    acquire_gdt
  127.     cmp    al,43h                 ;get local descriptor table
  128.     je    acquire_idt
  129.  
  130.     ;return error if not recognized
  131.  
  132.     mov    word ptr es:[bx+3],0C10Ch
  133.     ret
  134.  
  135. releasej:
  136.     jmp    release
  137.  
  138. acquire:
  139.     push    es
  140.     push    bx
  141.     push    ds
  142.     lds    si,dword ptr es:[bx+15]    ;read physical address from
  143.                                    ; the request packet
  144.     mov    ax,[si+2]
  145.     mov    bx,[si]
  146.     pop    ds
  147.     mov    cx,0
  148.     mov    dh,0                   ;request type 0, get selector for
  149.                                ; readable / executable segment
  150.     mov    dl,17h             ;index for PhysToUVirt
  151.     call    devhelp
  152.     jnc    acquire_good
  153.  
  154.     ;error exit
  155.  
  156.     pop    bx
  157.     pop    es
  158.     mov    word ptr es:[bx+3],0C113h
  159.     ret
  160.  
  161.     ;no error, return virtual address
  162.  
  163. acquire_good:
  164.     mov    ax,es
  165.     mov    dx,bx
  166.     pop    bx
  167.     pop    es
  168.     push    ds
  169.     lds    si,dword ptr es:[bx+19]    ;put result into request packet
  170.     mov    [si+2],ax
  171.     mov    [si],dx
  172.     pop    ds
  173.  
  174.     mov    word ptr es:[bx+3],0100h    ;set the "done" flag
  175.     ret
  176.  
  177. acquire_gdt:
  178.  
  179.     sgdt    fword ptr gdt_phys     ;store GDT register - this instruction
  180.                                ; can only be executed in ring zero
  181.     push    es
  182.     les    si,dword ptr es:[bx+15]    ;put physical address into packet
  183.     mov    ax,gdt_phys+2
  184.     mov    es:[si],ax
  185.     mov    ax,gdt_phys+4
  186.     and    ax,00FFh
  187.     mov    es:[si+2],ax
  188.     pop    es
  189.  
  190.     jmp    acquire                ;proceed with translation to virtual
  191.  
  192. acquire_idt:
  193.  
  194.     sidt    fword ptr idt_phys     ;store IDT register - this instruction
  195.                                ; can only be executed in ring zero
  196.     push    es
  197.     les    si,dword ptr es:[bx+15]    ;put physical address into packet
  198.     mov    ax,idt_phys+2
  199.     mov    es:[si],ax
  200.     mov    ax,idt_phys+4
  201.     and    ax,00FFh
  202.     mov    es:[si+2],ax
  203.     pop    es
  204.  
  205.     jmp    acquire                ;proceed with translation to virtual
  206.  
  207. release:
  208.     push    es
  209.     push    bx
  210.     push    ds
  211.     lds    si,dword ptr es:[bx+19]    ;get virtual address to release
  212.     mov    ax,[si+2]
  213.     pop    ds
  214.     mov    bx,0
  215.     mov    cx,0
  216.     mov    dh,2                   ;request type 2, free selector
  217.     mov    dl,17h             ;index for PhysToUVirt
  218.     call    devhelp
  219.     jnc    release_good
  220.  
  221.     ;error exit
  222.  
  223.     pop    bx
  224.     pop    es
  225.     mov    word ptr es:[bx+3],0C113h
  226.     ret
  227.  
  228.     ;no error
  229.  
  230. release_good:
  231.     pop    bx
  232.     pop    es
  233.     mov    word ptr es:[bx+3],0100h    ;set the "done" flag
  234.     ret
  235.  
  236. end_of_code    label    byte           ;code after this point is needed
  237.                            ; only at initialization time
  238.  
  239. ; * * * initialization command * * *
  240.  
  241. initialize:
  242.  
  243.     ;save "DevHlp" call address
  244.  
  245.     mov    ax,es:[bx+14]
  246.     mov    word ptr devhelp,ax
  247.     mov    ax,es:[bx+16]
  248.     mov    word ptr devhelp+2,ax
  249.  
  250.     ;display message
  251.  
  252.     push    1
  253.     push    ds
  254.     push    offset initmsg
  255.     push    initmsglen
  256.     push    ds
  257.     push    offset byteswritten
  258.     call    DosWrite
  259.  
  260.     ;set ending offsets
  261.  
  262.     mov    word ptr es:[bx+14],offset end_of_code
  263.     mov    word ptr es:[bx+16],offset end_of_data
  264.  
  265.     ;set other req packet fields
  266.  
  267.     mov    word ptr es:[bx+18],0
  268.     mov    word ptr es:[bx+20],0
  269.  
  270.     ;set status and exit
  271.  
  272.     mov    word ptr es:[bx+3],0100h    ;"done"
  273.     ret
  274.  
  275. strategy    endp
  276.  
  277. CODE    ENDS
  278.  
  279.     end
  280.