home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / caway349.zip / MISC / LINEAR.ZIP / PAGING.C < prev    next >
C/C++ Source or Header  |  1995-10-27  |  3KB  |  122 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <dos.h>
  5. #include <string.h>
  6.  
  7. #include "paging.h"
  8.  
  9. typedef struct
  10.   {
  11.   unsigned long Present     : 1;
  12.   unsigned long ReadWrite   : 1;
  13.   unsigned long UserSystem  : 1;
  14.   unsigned long __Filler1__ : 2;
  15.   unsigned long Accessed    : 1;
  16.   unsigned long Dirty       : 1;
  17.   unsigned long __Filler2__ : 2;
  18.   unsigned long User        : 3;
  19.   unsigned long Addr        : 20;
  20.   }
  21. PAGE_DESCRIPTOR;
  22.  
  23. const PAGE_DESCRIPTOR NullPage={0,1,1,0,0,0,0,0,0};
  24. const PAGE_DESCRIPTOR * PageTable=(PAGE_DESCRIPTOR *)0xFFC00000LU;
  25.  
  26. #define PAGE_TABLE(Addr)   PageTable+((unsigned long)(Addr) >> 12)
  27. #define ENABLE_PAGE(Page)  Page.Present=1
  28. #define DISABLE_PAGE(Page) Page.Present=0
  29. #define PHYSICAL_ADDRESS   0x80000000
  30. #define PAGE_ADDRESS(Page,Ptr)  {\
  31.                                 (Page)=NullPage;\
  32.                                 (Page).Addr=((unsigned long)(Ptr)) >> 12;  \
  33.                                 }
  34.  
  35. extern int  EnableLinearVideo(void);
  36. extern void CloseLinearVideo(void);
  37. extern unsigned long LinearVideoAddr;
  38. extern unsigned long LinearVideoEnd;
  39. extern unsigned long CR2AddrSEL;
  40. extern unsigned long CR2AddrOFF;
  41.  
  42. void * GetLinearVideo(int VideoSize)
  43.   {
  44.   void * Ptr=NULL;
  45.   char far * Int01Ptr;
  46.   union REGS Regs;
  47.   int i,j;
  48.   PAGE_DESCRIPTOR * PagePtr;
  49.  
  50.   if (VideoSize<1)
  51.     return(Ptr);
  52.  
  53.  
  54. // Search CR2 image into the extender
  55.  
  56.  
  57.   Regs.w.ax=0x202;
  58.   Regs.h.bl=0x01;
  59.   int386(0x31,&Regs,&Regs);
  60.   if (Regs.w.cflag)
  61.     return(Ptr);
  62.  
  63.   Regs.x.ecx&=0xffff;
  64.  
  65.   Int01Ptr=MK_FP(Regs.x.ecx,Regs.x.edx);
  66.   i=0;
  67.   while(!(Int01Ptr[i+0]=='C' &&
  68.           Int01Ptr[i+1]=='a' &&
  69.           Int01Ptr[i+2]=='u' &&
  70.           Int01Ptr[i+3]=='s' &&
  71.           Int01Ptr[i+4]=='e' &&
  72.           Int01Ptr[i+5]=='W' &&
  73.           Int01Ptr[i+6]=='a' &&
  74.           Int01Ptr[i+7]=='y') && i<4096)
  75.     i++;
  76.  
  77. // Not found
  78.   if (i==4096)
  79.     return(Ptr);
  80.  
  81.   CR2AddrSEL=Regs.x.ecx;
  82.   CR2AddrOFF=(unsigned long)Regs.x.edx+(unsigned long)i-10;
  83.  
  84.  
  85. // Call asm exception init.
  86.  
  87.   if (EnableLinearVideo())
  88.     return(Ptr);
  89.  
  90. // Allocate a dummy physical mapping for having some page descriptors.
  91. // the physical address (0x80000000) is not important.
  92.  
  93.   LinearVideoAddr=0;
  94.  
  95.   Regs.w.ax=0x0800;
  96.   Regs.w.bx=PHYSICAL_ADDRESS >> 16;
  97.   Regs.w.cx=PHYSICAL_ADDRESS & 0xffff;
  98.   Regs.w.si=VideoSize << 4;
  99.   Regs.w.di=0;
  100.  
  101.   int386(0x31,&Regs,&Regs);
  102.  
  103.   if (!Regs.w.cflag)
  104.     {
  105.     Ptr=(void *)(((int)Regs.w.bx << 16)+(int)Regs.w.cx);
  106.     LinearVideoAddr=(unsigned long)Ptr;
  107.     LinearVideoEnd=(VideoSize << 20)+LinearVideoAddr-1;
  108.     PagePtr=PAGE_TABLE(Ptr);
  109.  
  110.  
  111.     // Map each 16 pages batch at 0xa0000 linear addr.
  112.     // Set all as 'not present'
  113.  
  114.     for(i=0;i<VideoSize << 8;i+=16)
  115.       for(j=0;j<16;j++)
  116.         PAGE_ADDRESS(PagePtr[i+j],0xa0000+4096*j);
  117.     }
  118.   else
  119.     CloseLinearVideo();
  120.   return(Ptr);
  121.   }
  122.