home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_09 / GENEVA4B.LZH / MINT / 1.12 / PROTECT.TXT < prev    next >
Text File  |  1995-10-18  |  7KB  |  196 lines

  1. Appendix A: Memory Protection
  2.  
  3.  
  4. Memory protection works on a per-process basis.  Unless it uses the special
  5. mode bits in Mxalloc(), a process' entire text+data+bss and any memory it
  6. allocates will be of the same type.  There are four types of memory:
  7.  
  8.     1. Private.  Only the process itself (and the OS) can use the memory.
  9.  
  10.     2. Global.  This memory is totally unprotected.
  11.  
  12.     3. Super.  This memory can be accessed by anybody from Super mode.
  13.  
  14.     4. Private/readable.  Anybody can read, nobody else can write.
  15.  
  16. By default, all processes load into private memory.  If a program
  17. terminates and remains resident with the Ptermres() system call, then
  18. its private memory will be converted to Super memory; that's because
  19. TSRs won't work in private memory. Some TSRs may in fact need to be
  20. set to load into global memory (if they provide services for user
  21. mode programs).  There are other programs which need to grant wider access
  22. to their memory space: for example, the Gulam CLI is
  23. one, because Gulam's children can call Gulam through a pointer found in the
  24. system variable space.  But Gulam is a bad choice of shell anyway, because
  25. you can't run more than one of them.  Current versions of STeno also must
  26. grant wider access, because it passes data to STalker using shared memory;
  27. private/readable is adequate for that.
  28.  
  29. Programs that load before MiNT does (i.e. in the AUTO folder) will find
  30. themselves in global memory.  This is the easiest way to be sure that
  31. your TSRs will go on working: RAMdisks, FOLDR100, CACHE080, etc. all
  32. work when loaded that way.
  33.  
  34. Notice that a process can grant wider access to its own address space, but
  35. it can't change the access permissions of any other process' space. If you
  36. set the PRGFLAGS of some process so it loads into global memory, that
  37. doesn't mean it can access anybody else's memory, it just means that
  38. anybody can access ITS memory.
  39.  
  40. ----------------------------------------------------------------------------
  41.  
  42. PRGFLAGS for protection:
  43.  
  44.     The second-least-significant nybble of PRGFLAGS is the default mode
  45. for your program's memory protection.
  46.  
  47.     0x00: private memory.  Only you and the AES can touch it.
  48.     0x10: Global: anybody can touch it.
  49.     0x20: super: anybody who is supervisor can touch it.
  50.     0x30: private/readable: anybody can read, nobody else can write.
  51.  
  52.     Other values are reserved for Atari use and/or future modes.
  53.  
  54. You can use PRGFLAGS.PRG to change the PRGFLAGS value for any program. From
  55. the shell, say (for example) "prgflags 0xf0 0x10 myprog.prg" to set
  56. "myprog.prg" to "global" (0x10).  The way you use PRGFLAGS is to give it
  57. two numbers, then the program name(s) to operate on.  The first number is
  58. taken as a mask, and the second as a value: bits which are one in the mask
  59. will be set to the corresponding bits in the value; bits which are zero in
  60. the mask will not be changed.  Thus the example sets bits 4-7 to "0001"
  61. and leaves all other bits alone.
  62.  
  63. You can specify the protection you want on a region when you Malloc it:
  64. call Mxalloc(size,mode), where the least-significant nybble of "mode"
  65. means what it always has, and the next four bits are the desired
  66. protection mode:
  67.  
  68.     0x00    default (whatever's in your PRGFLAGS)
  69.     0x10    private
  70.     0x20    global
  71.     0x30    super
  72.     0x40    readable
  73.  
  74.     0x4000    NO-FREE (see below)
  75.  
  76. ----------------------------------------------------------------------------
  77.  
  78. You can use Fopen() on u:\proc\*.<pid> to get a file handle to a process.
  79. Then you can use Fcntl() to get and set things about that process.
  80.  
  81.     #include <osbind.h>
  82.     #include <mintbind.h>
  83.     #include <filesys.h>    /* for PBASEADDR and other constants */
  84.     #include <uproc.h>    /* for struct PROCESS and other things */
  85.  
  86.     long baseaddr;
  87.     long ctxtsize;
  88.     long procaddr;
  89.     long flags;
  90.     char buf[14];
  91.     int fd;
  92.     PROCESS proc;        /* structure from uproc.h */
  93.     CONTEXT ctxt;
  94.  
  95.     sprintf(buf,"u:\\proc\\*.%03d",(int)Pgetpid());
  96.     fd = Fopen(buf,2);
  97.  
  98.     Fcntl(fd,&baseaddr,PBASEADDR);
  99.         Puts the basepage address of the process in arg.
  100.  
  101.     Fcntl(fd,&ctxtsize,PCTXTSIZE)
  102.         Puts the size of a MiNT context at arg.
  103.  
  104.     Fcntl(fd,&procaddr,PPROCADDR)
  105.         Puts the address of the MiNT process structure for the process
  106.         in arg.  To read the process structure do this:
  107.  
  108.         Fseek(procaddr,fd,0);
  109.         Fread(fd,sizeof(proc),&proc);
  110.  
  111.         To read the user context (registers, etc.) do this:
  112.  
  113.         Fseek(procaddr-ctxtsize,fd,0);
  114.         Fread(fd,sizeof(ctxt),&ctxt);
  115.  
  116.         To read any part of the memory owned by the process, do this:
  117.  
  118.         Fseek(addr,fd,0);
  119.         Fread(fd,size,buffer);
  120.  
  121.         This Fread will only work if the region you want to read is in
  122.         fact owned by the process that 'fd' refers to.  If the process
  123.         terminates, then the handle becomes invalid.
  124.  
  125.         If you Fopen process zero (MiNT itself) then you can Fseek to
  126.         any address that is managed by MiNT and read or write there.
  127.         This is for debuggers and similarly dangerous tools.
  128.  
  129.     Fcntl(fd,&flags,PGETFLAGS)
  130.         Puts the low 16 bits of a process' PRGFLAGS into 'flags'.
  131.  
  132.     Fcntl(fd,&flags,PSETFLAGS)
  133.         Sets the low 16 bits of a process' PRGFLAGS from 'flags'.
  134.  
  135. See also Appendix B (Debugging) for other uses for U:\PROC.
  136. ----------------------------------------------------------------------------
  137.  
  138. To review, here are all the defined bits in the PRGFLAGS field of PRG file
  139. headers, and what they mean:
  140.  
  141.     BITS    MEANING
  142.     31-28    This number, plus one, times 128K, is the minimum amount
  143.         of alternative RAM that is acceptable.  Used when there
  144.         is more ST RAM than alternative RAM.
  145.  
  146.     27-13    Reserved; currently unused. All programs should use 0 here.
  147.  
  148.     12    Shared text bit. If set, the operating system will allow
  149.         the text segment of the program to be shared, i.e. if
  150.         3 copies of the program are running, there are 3 data and
  151.         bss areas but only 1 text segment is shared amongst
  152.         the 3 copies. SET THIS BIT ONLY IF YOUR PROGRAM CONTAINS
  153.         NO ABSOLUTE REFERENCES TO DATA OR BSS, i.e. if it was
  154.         compiled to use a base register for data and bss. Very
  155.         few existing programs can use this.
  156.  
  157.     11-8    Reserved; currently unused. All programs should use 0 here.
  158.  
  159.     7-4    This is the default memory-protection mode:
  160.         0=private, 1=global, 2=super, 3=world-readable.
  161.  
  162.     3    Reserved; currently unused. All programs should use 0 here.
  163.  
  164.     2    When 1, Malloc calls may be satisfied from alternative RAM.
  165.  
  166.     1    When 1, the program may load into alternative RAM.
  167.  
  168.     0    When 1, the program loads faster: the heap is not cleared.
  169.  
  170. And in Mxalloc's "mode" argument...
  171.  
  172.           BITS    MEANING
  173.  
  174.         ALTERNATIVE RAM ELIGIBILITY
  175.     0-2    0: ST RAM only
  176.         1: Alternative RAM only
  177.         2: Either, ST RAM preferred
  178.         3: Either, alternative RAM preferred
  179.  
  180.     3:    Reserved
  181.  
  182.         PROTECTION MODE
  183.     4-7    0: Default (from your PRGFLAGS)
  184.         1: Private
  185.         2: Global
  186.         3: Super
  187.         4: World-readable
  188.         other values are undefined and reserved.
  189.  
  190.     14    NO-FREE MODE
  191.         When set, this bit means "if the owner of this region
  192.         terminates, don't free this region. Instead, let MiNT
  193.         inherit it, so it'll never be freed."  This is a special
  194.         mode meant for the OS only, and may not remain available
  195.         to user processes.
  196.