home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / caway349.zip / SOURCE / ALL / LDT.ASM < prev    next >
Assembly Source File  |  1995-06-13  |  9KB  |  442 lines

  1. ;-------------------------------------------------------------------------------
  2. ;
  3. ;Allocate block of descriptor entries.
  4. ;
  5. ;On Entry:-
  6. ;
  7. ;CX    - Number of descriptors to allocate.
  8. ;
  9. ;On Exit:-
  10. ;
  11. ;AX    - Base selector.
  12. ;
  13. RawGetDescriptors proc near
  14.     call    RAWCopyCheck
  15.     ;
  16.     pushm    ebx,ecx,edx,esi,edi,ds,es
  17.     mov    ax,KernalDS        ;make our data addresable.
  18.     mov    ds,ax
  19.     assume ds:_cwRaw
  20.     mov    ax,KernalZero    ;make LDT memory addresable.
  21.     mov    es,ax
  22.     mov    edi,MDTLinear+4    ;point to descriptor map.
  23.     movzx    edx,cx
  24.     mov    ecx,8192
  25.     xor    al,al
  26.     cld
  27.     dec    edx
  28.     jnz    @@0
  29.     ;
  30.     ;Search for single selector.
  31.     ;
  32.     repne    scasb        ;find a zero.
  33.     jnz    @@8
  34.     dec    edi
  35.     inc    al
  36.     mov    es:[edi],al
  37.     jmp    @@1
  38.     ;
  39.     ;search for multiple selectors.
  40.     ;
  41. @@0:    repne    scasb        ;find a zero.
  42.     jnz    @@8
  43.     cmp    edx,ecx
  44.     jnc    @@8
  45.     pushm    ecx,edi
  46.     mov    ecx,edx
  47.     repe    scasb        ;check for string of zero's
  48.     popm    ecx,edi
  49.     jnz    @@0
  50.     dec    edi
  51.     push    edi
  52.     mov    ecx,edx
  53.     inc    ecx
  54.     inc    al
  55.     rep    stosb        ;mark all entries as used.
  56.     pop    edi
  57. @@1:    sub    edi,MDTLinear+4
  58.     shl    edi,3
  59.     push    edi
  60.     add    edi,MDTLinear
  61.     xor    eax,eax
  62.     mov    es:d[edi],eax
  63.     mov    es:d[edi+4],(DescPresent+DescMemory+DescRWData+DescPL3) shl 8
  64.     or    edx,edx
  65.     jz    @@2
  66.     mov    ecx,edx
  67.     add    ecx,ecx
  68.     mov    esi,edi
  69.     add    edi,8
  70.     pushm    ds,es
  71.     pop    ds
  72.     rep    movsd
  73.     pop    ds
  74.     ;
  75. @@2:    pop    eax
  76.     or    ax,3+4        ;RPL=3, TI=1
  77.     clc
  78.     jmp    @@9
  79.     ;
  80. @@8:    stc
  81.     ;
  82. @@9:    popm    ebx,ecx,edx,esi,edi,ds,es
  83.     ret
  84.     assume ds:_cwDPMIEMU
  85. RawGetDescriptors endp
  86.  
  87.  
  88. ;-------------------------------------------------------------------------------
  89. ;
  90. ;Release a previously allocated descriptor entry.
  91. ;
  92. ;On Entry:-
  93. ;
  94. ;BX    - Selector for descriptor to release.
  95. ;
  96. RawRelDescriptor proc near
  97.     call    RAWCopyCheck
  98.     ;
  99.     ;Check segment registers for value we're releasing and clear
  100.     ;if found.
  101.     ;
  102.     pushm    eax,ebx,ecx
  103.     and    ebx,0ffffh-7
  104.     xor    ecx,ecx
  105.     xor    eax,eax
  106.     mov    ax,ds
  107.     and    eax,not 7
  108.     cmp    eax,ebx
  109.     jnz    @@0
  110.     mov    ds,cx
  111. @@0:    mov    ax,es
  112.     and    eax,not 7
  113.     cmp    eax,ebx
  114.     jnz    @@1
  115.     mov    es,cx
  116. @@1:    mov    ax,fs
  117.     and    eax,not 7
  118.     cmp    eax,ebx
  119.     jnz    @@2
  120.     mov    fs,cx
  121. @@2:    mov    ax,gs
  122.     and    eax,not 7
  123.     cmp    eax,ebx
  124.     jnz    @@3
  125.     mov    gs,cx
  126. @@3:    popm    eax,ebx,ecx
  127.     ;
  128.     pushm    eax,esi,ds,es
  129.     mov    ax,KernalDS        ;make our data addresable.
  130.     mov    ds,ax
  131.     assume ds:_cwRaw
  132.     mov    ax,KernalZero    ;make LDT addresable.
  133.     mov    es,ax
  134.     movzx    esi,bx        ;Get selector to use.
  135.     shr    esi,3        ;/8 for descriptor number.
  136.     add    esi,MDTLinear+4
  137.     mov    es:b[esi],0        ;mark this entry as free.
  138.     movzx    esi,bx
  139.     and    esi,not 7
  140.     add    esi,MDTLinear
  141.     xor    eax,eax
  142.     mov    es:d[esi],eax
  143.     mov    es:d[esi+4],eax
  144.     ;
  145.     clc
  146.     popm    eax,esi,ds,es
  147.     ret
  148.     assume ds:_cwDPMIEMU
  149. RawRelDescriptor endp
  150.  
  151.  
  152. ;-------------------------------------------------------------------------------
  153. ;
  154. ;Create/return a protected mode selector that maps a real mode segment.
  155. ;
  156. ;On Entry:
  157. ;
  158. ;BX    - Real mode segment address to map.
  159. ;
  160. ;On Exit:
  161. ;
  162. ;AX    - Protected mode selector.
  163. ;
  164. ;Note: Multiple calls with the same value will nearly always return the same
  165. ;selector depending on how many selectors have been allocated with this routine.
  166. ;
  167. RawReal2ProtSel proc    near
  168.     call    RAWCopyCheck
  169.     ;
  170.     pushm    ebx,ecx,edx,esi,edi,ds
  171.     mov    ax,DpmiEmuDS        ;make our data addresable.
  172.     mov    ds,ax
  173.     movzx    ebx,bx        ;Get the segment value.
  174.     shl    ebx,4        ;convert to linear.
  175.     mov    esi,offset Real2ProtList    ;point to list of selectors.
  176.     mov    ecx,128
  177. @@0:    cmp    w[esi],0        ;end of the list?
  178.     jz    @@New
  179.     pushm    ebx,ecx
  180.     mov    bx,[esi]
  181.     call    RawGetSelBase
  182.     shl    ecx,16
  183.     mov    cx,dx
  184.     mov    edx,ecx
  185.     popm    ebx,ecx
  186.     cmp    edx,ebx        ;same base?
  187.     jz    @@GotOne
  188.     add    esi,2
  189.     dec    ecx
  190.     jnz    @@0
  191. ;
  192. ;Have to allocate a new descriptor.
  193. ;
  194. @@New:    push    ecx
  195.     mov    cx,1
  196.     call    RawGetDescriptors    ;get a descriptor.
  197.     pop    ecx
  198.     jc    @@9
  199.     pushm    eax,ebx,ecx
  200.     mov    dx,bx
  201.     shr    ebx,16
  202.     mov    cx,bx
  203.     mov    bx,ax
  204.     call    RawSetSelBase
  205.     popm    eax,ebx,ecx
  206.     pushm    eax,ebx,ecx
  207.     mov    dx,-1
  208.     xor    cx,cx
  209.     mov    bx,ax
  210.     call    RawSetSelLimit
  211.     popm    eax,ebx,ecx
  212.     or    ecx,ecx        ;did we reach the end of the list?
  213.     jz    @@Done
  214.     mov    [esi],ax
  215.     jmp    @@Done
  216. ;
  217. ;Found a previous selector with right base so use that.
  218. ;
  219. @@GotOne:    mov    ax,[esi]
  220. ;
  221. ;Now exit with succesful value.
  222. ;
  223. @@Done:    clc
  224. @@9:    popm    ebx,ecx,edx,esi,edi,ds
  225.     ret
  226. Real2ProtList    dw 128 dup (0)
  227. RawReal2ProtSel endp
  228.  
  229.  
  230. ;-------------------------------------------------------------------------------
  231. ;
  232. ;Set selector type.
  233. ;
  234. ;On Entry:-
  235. ;
  236. ;BX    - Selector.
  237. ;CL    - Type.
  238. ;CH    - Extended type.
  239. ;
  240. RawSetSelType    proc    near
  241.     call    RAWCopyCheck
  242.     ;
  243.     pushm    eax,esi,ds,es
  244.     mov    ax,KernalDS        ;make our data addresable.
  245.     mov    ds,ax
  246.     assume ds:_cwRaw
  247.     mov    ax,KernalZero    ;make LDT addresable.
  248.     mov    es,ax
  249.     movzx    esi,bx        ;Get the selector.
  250.     and    si,0fff8h        ;lose RPL & TI.
  251.     add    esi,MDTLinear    ;offset into descriptor table.
  252.     mov    es:b[esi+5],cl
  253.     and    ch,11110000b
  254.     and    es:b[esi+6],00001111b
  255.     or    es:b[esi+6],ch
  256.     ;
  257.     clc
  258.     popm    eax,esi,ds,es
  259.     ret
  260.     assume ds:_cwDPMIEMU
  261. RawSetSelType    endp
  262.  
  263.  
  264. ;-------------------------------------------------------------------------------
  265. ;
  266. ;Set selector limit.
  267. ;
  268. ;On Entry:-
  269. ;
  270. ;BX    - Selector.
  271. ;CX:DX    - Limit.
  272. ;
  273. RawSetSelLimit    proc    near
  274.     call    RAWCopyCheck
  275.     ;
  276.     pushm    eax,ecx,esi,ds,es
  277.     mov    ax,KernalDS        ;make our data addresable.
  278.     mov    ds,ax
  279.     assume ds:_cwRaw
  280.     mov    ax,KernalZero    ;make LDT addresable.
  281.     mov    es,ax
  282.     movzx    esi,bx        ;Get the selector.
  283.     and    si,0ffffh-7        ;lose RPL & TI.
  284.     add    esi,MDTLinear    ;offset into descriptor table.
  285.     shl    ecx,16
  286.     mov    cx,dx
  287.     xor    al,al
  288.     cmp    ecx,100000h      ; see if we need to set g bit
  289.     jc    @@2
  290.     shr    ecx,12           ; div by 4096
  291.     or    al,80h           ; set g bit
  292. @@2:    mov    es:[esi],cx        ;store low word of limit.
  293.     shr    ecx,16
  294.     or    cl,al
  295.     and    es:b[esi+6],01110000b    ;lose limit & G bit.
  296.     or    es:[esi+6],cl        ;store high bits of limit and G bit.
  297.     ;
  298.     clc
  299.     popm    eax,ecx,esi,ds,es
  300.     ret
  301.     assume ds:_cwDPMIEMU
  302. RawSetSelLimit    endp
  303.  
  304.  
  305. ;-------------------------------------------------------------------------------
  306. ;
  307. ;Set selector linear base address.
  308. ;
  309. ;On Entry:-
  310. ;
  311. ;BX    - Selector.
  312. ;CX:DX    - Linear base.
  313. ;
  314. RawSetSelBase    proc    near
  315.     call    RAWCopyCheck
  316.     ;
  317.     pushm    eax,esi,ds,es
  318.     mov    ax,KernalDS        ;make our data addressable.
  319.     mov    ds,ax
  320.     assume ds:_cwRaw
  321.     mov    ax,KernalZero    ;make LDT addressable.
  322.     mov    es,ax
  323.     movzx    esi,bx        ;Get the selector.
  324.     and    si,0ffffh-7        ;lose RPL & TI.
  325.     add    esi,MDTLinear    ;offset into descriptor table.
  326.     mov    es:[esi+4],cl        ;base mid.
  327.     mov    es:[esi+7],ch        ;base high.
  328.     mov    es:[esi+2],dx        ;base low.
  329.     clc
  330.     popm    eax,esi,ds,es
  331.     ret
  332.     assume ds:_cwDPMIEMU
  333. RawSetSelBase    endp
  334.  
  335.  
  336. ;-------------------------------------------------------------------------------
  337. ;
  338. ;Get selector linear base address.
  339. ;
  340. ;On Entry:-
  341. ;
  342. ;BX    - Selector.
  343. ;
  344. ;On Exit:-
  345. ;
  346. ;CX:DX    - Linear base.
  347. ;
  348. RawGetSelBase    proc    near
  349.     call    RAWCopyCheck
  350.     ;
  351.     pushm    eax,esi,ds,es
  352.     mov    ax,KernalDS        ;make our data addresable.
  353.     mov    ds,ax
  354.     assume ds:_cwRaw
  355.     mov    ax,KernalZero    ;make LDT addresable.
  356.     mov    es,ax
  357.     movzx    esi,bx        ;Get the selector.
  358.     and    si,0fff8h        ;lose RPL & TI.
  359.     add    esi,MDTLinear    ;offset into descriptor table.
  360.     mov    cl,es:[esi+4]        ;base mid.
  361.     mov    ch,es:[esi+7]        ;base high.
  362.     mov    dx,es:[esi+2]        ;base low.
  363.     clc
  364.     popm    eax,esi,ds,es
  365.     ret
  366.     assume ds:_cwDPMIEMU
  367. RawGetSelBase    endp
  368.  
  369.  
  370. ;-------------------------------------------------------------------------------
  371. ;
  372. ;Copy buffer into descriptor.
  373. ;
  374. ;On Entry:-
  375. ;
  376. ;BX    - Selector.
  377. ;ES:[E]DI    - Buffer.
  378. ;
  379. RawBPutDescriptor proc near
  380.     call    RAWCopyCheck
  381.     ;
  382.     pushm    eax,ecx,esi,edi,ds,es
  383.     mov    ax,KernalDS        ;make our data addresable.
  384.     mov    ds,ax
  385.     assume ds:_cwRaw
  386.     movzx    esi,bx        ;Get the selector.
  387.     and    si,0fff8h        ;lose RPL & TI.
  388.     add    esi,MDTLinear    ;offset into descriptor table.
  389.     test    RawSystemFlags,1
  390.     jz    @@Use32
  391.     movzx    edi,di
  392. @@Use32:    mov    ax,KernalZero    ;make LDT addresable.
  393.     mov    ds,ax
  394.     pushm    esi,ds,edi,es    ;swap them round.
  395.     popm    edi,es,esi,ds
  396.     cld
  397.     movsd
  398.     movsd
  399.     ;
  400.     clc
  401.     popm    eax,ecx,esi,edi,ds,es
  402.     ret
  403.     assume ds:_cwDPMIEMU
  404. RawBPutDescriptor endp
  405.  
  406.  
  407. ;-------------------------------------------------------------------------------
  408. ;
  409. ;Copy descriptor into buffer.
  410. ;
  411. ;On Entry:-
  412. ;
  413. ;BX    - Selector.
  414. ;ES:[E]DI    - Buffer.
  415. ;
  416. RawBGetDescriptor proc near
  417.     call    RAWCopyCheck
  418.     ;
  419.     pushm    eax,ecx,esi,edi,ds
  420.     mov    ax,KernalDS        ;make our data addresable.
  421.     mov    ds,ax
  422.     assume ds:_cwRaw
  423.     movzx    esi,bx        ;Get the selector.
  424.     and    si,0fff8h        ;lose RPL & TI.
  425.     add    esi,MDTLinear    ;offset into descriptor table.
  426.     test    RawSystemFlags,1
  427.     jz    @@Use32
  428.     movzx    edi,di
  429. @@Use32:    mov    ax,KernalZero    ;make LDT addresable.
  430.     mov    ds,ax
  431.     cld
  432.     movsd
  433.     movsd
  434.     ;
  435.     clc
  436.     popm    eax,ecx,esi,edi,ds
  437.     ret
  438. RawBGetDescriptor endp
  439.  
  440.  
  441.  
  442.