home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mint104s.zoo / mint.src / memprot.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  32KB  |  1,148 lines

  1. /*
  2.  * Copyright 1991,1992,1993 Atari Corporation.
  3.  * All rights reserved.
  4.  */
  5.  
  6. /*
  7.  * page-table data structures
  8.  *
  9.  *
  10.  * The root pointer points to a list of pointers to top-level pointer tables.
  11.  * 
  12.  * Each entry in a pointer table points to another pointer table or to
  13.  * a page table, or is a page descriptor.
  14.  * 
  15.  * Since, initially, the logical address space is the physical address space,
  16.  * we only need to worry about 26MB plus 32K for I/O space.
  17.  * 
  18.  * Since we want some pages to be supervisor-accessible, but we don't want
  19.  * a whole separate table for that, we use long-format descriptors.
  20.  * 
  21.  * Initial memory map:
  22.  * 
  23.  * 0     - membot: S (supervisor only)
  24.  * membot     - memtop: P (protected TPA)
  25.  * memtop     - phystop: G (screen)
  26.  * phystop     - $00E00000: bus error
  27.  * $00E00000- $00E3FFFF: G (ROM)
  28.  * $00E40000- $00FF7FFF: bus error
  29.  * $00FF8000- $00FFFFFF: G (mostly S: I/O space, but that's done in hardware)
  30.  * $01000000- ramtop: P
  31.  * ramtop     - $7FFFFFFF: G (A32/D32 VME, cacheable)
  32.  * $80000000- $FEFFFFFF: G (A32/D32 VME, non cacheable)
  33.  * $FFxxxxxx          just like $00xxxxxx.
  34.  * 
  35.  * Here's a final choice of layouts: IS=0, PS=13 (8K), TIA=4, TIB=4, TIC=4,
  36.  * TID=7.  This lets us map out entire unused megabytes at level C, and gives
  37.  * us an 8K page size, which is the largest the '040 can deal with.
  38.  * 
  39.  * This code implements 4+4+4+7, as follows:
  40.  * 
  41.  * tbl_a
  42.  *     0 -> tbl_b0
  43.  *     1-7 -> Cacheable direct (VME) page descriptors
  44.  *     8-E -> Non-cacheable direct (VME) page descriptors
  45.  *     F -> tbl_bf
  46.  * 
  47.  * tbl_b0 table: 16 entries (assumes only 16MB of TT RAM)
  48.  *     0 -> tbl_c00 (16MB of ST RAM address space)
  49.  *     1 -> tbl_c01 (16MB of TT RAM address space)
  50.  *     2-F -> cacheable direct (VME) page descriptors
  51.  * 
  52.  * tbl_bF table: 16 entries (deals with $FF mapping to $00)
  53.  *     0-E -> Non-cacheable direct (VME) page descriptors
  54.  *     F -> tbl_c00 (16MB of ST RAM address space, repeated here as $FF)
  55.  * 
  56.  * tbl_c00 table: ST RAM address space (example assuming 4MB ST RAM)
  57.  *     0-3 -> RAM page tables
  58.  *     4-D -> invalid
  59.  *     E -> direct map, cache enable (ROM)
  60.  *     F -> direct map, cache inhibit (I/O)
  61.  * 
  62.  * For each 16MB containing any TT RAM, there's a tbl_c.  Within those,
  63.  * for each MB that actually has TT RAM, there's another table, containing
  64.  * 128 RAM page tables.  Where there isn't RAM, there are "global"
  65.  * pages, to let the hardware bus error or not as it sees fit.
  66.  * 
  67.  * One RAM page table is allocated per megabyte of real RAM; each table has
  68.  * 128 entries, which is 8K per page.  For a TT with 4MB ST RAM and 4MB TT RAM
  69.  * that's 8K in page tables.  You can cut this down by not allocating page
  70.  * tables for which the entire megabyte is not accessible (i.e. it's all
  71.  * private memory and it's not YOUR private memory).
  72.  * 
  73.  * You have one of these per process.  When somebody loads into G or S memory
  74.  * or leaves it, you have to go through the page tables of every process
  75.  * updating S bits (for S) and DT (for G) bits.
  76.  * 
  77.  * The top levels are small & easy so replicating them once per process
  78.  * doesn't really hurt us.
  79.  * 
  80.  */
  81.  
  82. #include "mint.h"
  83.  
  84. #if 0
  85. #define MP_DEBUG(x) DEBUG(x)
  86. #else
  87. #define MP_DEBUG(x)
  88. #endif
  89.  
  90. void *memset P_((void *s, int ucharfill, unsigned long size));
  91. static void _dump_tree P_((long_desc tbl, int level));
  92.  
  93. extern int debug_level;        /* see debug.c */
  94.  
  95. /*
  96.  * You can turn this whole module off, and the stuff in context.s,
  97.  * by setting no_mem_prot to 1.
  98.  */
  99.  
  100. int no_mem_prot;
  101. long page_table_size;
  102.  
  103. /*
  104.  * PMMU stuff
  105.  */
  106. #if 1
  107. #define flush_pmmu(start, end) __asm("pflusha")
  108. #else
  109. /* in cpu.spp is a new "cpush" function that can selectively flush
  110.  * the cache
  111.  */
  112. #define flush_pmmu(start, len) cpush((void *)(start), (long)(len))
  113. #endif
  114.  
  115. /*
  116.  * This is one global TC register that is copied into every process'
  117.  * context, even though it never changes.  It's also used by the
  118.  * functions that dump page tables.
  119.  */
  120.  
  121. tc_reg tc;
  122.  
  123. /* mint_top_* get used in mem.c also */
  124. ulong mint_top_tt;
  125. ulong mint_top_st;
  126.  
  127. int tt_mbytes;        /* number of megabytds of TT RAM */
  128.  
  129. /*
  130.  * global_mode_table: one byte per page in the system.  Initially all pages
  131.  * are set to "global" but then the TPA pages are set to "invalid" in
  132.  * init_mem.  This has to be allocated and initialized in init_tables,
  133.  * when you know how much memory there is.  You need a byte per page,
  134.  * from zero to the end of TT RAM, including the space between STRAM
  135.  * and TTRAM.  That is, you need 16MB/pagesize plus (tt_mbytes/pagesize)
  136.  * bytes here.
  137.  */
  138.  
  139. unsigned char *global_mode_table;
  140.  
  141. /*
  142.  * prototype descriptors; field u1 must be all ones, other u? are zero.
  143.  * This is just the first long of a full descriptor; the ".page_type" part
  144.  * of the union.  These are initialized by init_tables.
  145.  *
  146.  * The proto_page_type table yields the value to stuff into the page_type
  147.  * field of a new process' page table.  It is the "non-owner" mode for
  148.  * a page with the corresponding value in global_mode_table.
  149.  */
  150.  
  151. page_type g_page;
  152. page_type g_ci_page;
  153. page_type s_page;
  154. page_type readable_page;
  155. page_type invalid_page;
  156. page_type page_ptr;
  157.  
  158. page_type *proto_page_type[] =
  159.     { &invalid_page, &g_page, &s_page, &readable_page, &invalid_page };
  160. /*    private         global    super    private/read    invalid */
  161.  
  162. /*
  163.  * Init_tables: called sometime in initialization.  We set up some
  164.  * constants here, but that's all.  The first new_proc call will set up the
  165.  * page table for the root process and switch it in; from then on, we're
  166.  * always under some process' control.
  167.  * 
  168.  * The master page-mode table is initialized here, and some constants like
  169.  * the size needed for future page tables.
  170.  *
  171.  * One important constant initialized here is page_table_size, which is
  172.  * the amount of memory required per page table.  new_proc allocates
  173.  * this much memory for each process' page table.  This number will be
  174.  * 1K/megabyte plus page table overhead.  There are TBL_PAGES_OFFS
  175.  * tables at TBL_SIZE_BYTES each before the main tables begin; then
  176.  * there is 1024 bytes per megabyte of memory being mapped.
  177.  */
  178.  
  179. void
  180. init_tables()
  181. {
  182.     int n_megabytes;
  183.     long global_mode_table_size;
  184.  
  185.     if (no_mem_prot) return;
  186.  
  187.     TRACE(("init_tables"));
  188.  
  189. #define phys_top_tt (*(ulong *)0x5a4L)
  190.     if (phys_top_tt == 0x01000000L) mint_top_tt = 0;
  191.     else mint_top_tt = phys_top_tt;
  192.  
  193. #define phys_top_st (*(ulong *)0x42eL)
  194.     mint_top_st = phys_top_st;
  195.  
  196.     if (mint_top_tt)
  197.         tt_mbytes = (int) ((mint_top_tt - 0x01000000L) / ONE_MEG);
  198.     else
  199.         tt_mbytes = 0;
  200.  
  201.     n_megabytes = (int) ((mint_top_st / ONE_MEG) + tt_mbytes);
  202.  
  203.     /*
  204.      * page table size: room for A table, B0 table, BF table, STRAM C
  205.      * table, one TTRAM C table per 16MB (or fraction) of TTRAM, and 1024
  206.      * bytes per megabyte.
  207.      */
  208.  
  209.     page_table_size = (4L * TBL_SIZE_BYTES) +
  210.               (((tt_mbytes+15L)/16L) * TBL_SIZE_BYTES) +
  211.               (n_megabytes*1024L);
  212.  
  213.     global_mode_table_size = ((SIXTEEN_MEG / QUANTUM) +
  214.                 (((ulong)tt_mbytes * ONE_MEG) / QUANTUM));
  215.  
  216.     global_mode_table = kmalloc(global_mode_table_size);
  217.  
  218.     assert(global_mode_table);
  219.  
  220.     TRACELOW(("mint_top_st is $%lx; mint_top_tt is $%lx, n_megabytes is %d",
  221.     mint_top_st, mint_top_tt, n_megabytes));
  222.     TRACELOW(("page_table_size is %ld, global_mode_table_size %ld",
  223.         page_table_size,
  224.         global_mode_table_size));
  225.  
  226.     g_page.limit = 0x7fff;    /* set nonzero fields: disabled limit */
  227.     g_page.unused1 = 0x3f;    /* ones in this reserved field */
  228.     g_page.unused2 = 0;
  229.     g_page.s = 0;
  230.     g_page.unused3 = 0;
  231.     g_page.ci = 0;
  232.     g_page.unused4 = 0;
  233.     g_page.m = 1;        /* set m and u to 1 so CPU won't do writes */
  234.     g_page.u = 1;
  235.     g_page.wp = 0;        /* not write-protected */
  236.     g_page.dt = 1;        /* descriptor type 1: page descriptor */
  237.  
  238.     g_ci_page = g_page;
  239.     g_ci_page.ci = 1;
  240.  
  241.     readable_page = g_page;    /* a page which is globally readable */
  242.     readable_page.wp = 1;    /* but write protected */
  243.  
  244.     s_page = g_page;        /* a page which is globally accessible */
  245.     s_page.s = 1;        /* if you're supervisor */
  246.  
  247.     invalid_page = g_page;
  248.     invalid_page.dt = 0;
  249.  
  250.     page_ptr = g_page;
  251.     page_ptr.m = 0;        /* this must be zero in page pointers */
  252.     page_ptr.dt = 3;
  253.  
  254.     tc.enable = 1;
  255.     tc.zeros = 0;
  256.     tc.sre = 0;
  257.     tc.fcl = 0;
  258.     tc.is = 0;
  259.     tc.tia = 4;
  260.     tc.tib = 4;
  261.     tc.tic = 4;
  262.     tc.tid = 7;            /* 0+4+4+4+7+13 == 32 */
  263.     tc.ps = 13;            /* 8K page size */
  264.  
  265.     /* set the whole global_mode_table to "global" */
  266.     memset(global_mode_table,PROT_G,global_mode_table_size);
  267. }
  268.  
  269. /*
  270.  * mark_region: mark a region of memory as having a particular type.
  271.  * The arguments are the memory region in question and the new type.
  272.  * If the new type is zero then the old type is preserved.  The
  273.  * type of each page is kept in a global place for this purpose,
  274.  * among others.
  275.  *
  276.  * The types are:
  277.  *  0    private
  278.  *  1    global
  279.  *  2    private, but super-accessible
  280.  *  3    private, but world readable
  281.  *  4   invalid
  282.  *
  283.  
  284. The idea is this:
  285.  
  286.     for (each process) {
  287.     if (you're an owner or you're special) {
  288.         set up owner modes
  289.     }
  290.     else {
  291.         set up non-owner modes
  292.     }
  293.  
  294.     mark_pages(pagetbl,start,len,modes);
  295.     }
  296.  
  297.  */
  298.  
  299. /*
  300.             invalid---v
  301.           private/gr---v  |
  302.             super-------v  |  |
  303.         global-------v  |  |  |
  304.     private-------v     |  |  |  |
  305.               |     |  |  |  |
  306. */
  307. ushort other_dt[] = { 0, 1, 1, 1, 0 };
  308. ushort other_s[] = {  0, 0, 1, 0, 0 };
  309. ushort other_wp[] = { 0, 0, 0, 1, 0 };
  310.  
  311.  
  312. /*
  313.  * get_page_cookie: return a cookie representing the protection status
  314.  * of some memory.
  315.  *
  316.  * Returns ((wp << 3) | (s << 2) | (dt) | 0x8000) when it wins.
  317.  * Returns 1 if the pages are not all controlled, 0 if they're not all the same.
  318.  */
  319.  
  320. static short
  321. get_page_cookie(long_desc *base_tbl,ulong start,ulong len)
  322. {
  323.     int b_index, c_index, d_index;
  324.     long_desc *tbl;
  325.     int dt, s, wp;
  326.  
  327.     if (start < mint_top_st) {
  328.     /* start is in ST RAM; fail if not entirely in ST RAM */
  329.     if (start+len > mint_top_st) {
  330.         return 1;
  331.     }
  332.     }
  333.     else if (start >= 0x01000000L && start < mint_top_tt) {
  334.     /* start is in TT RAM; fail if not entirely in TT RAM */
  335.     if (start+len > mint_top_tt) {
  336.         return 1;
  337.     }
  338.     }
  339.  
  340.     b_index = (int)(start >> LOG2_16_MEG);
  341.     c_index = (int)(start >> LOG2_ONE_MEG) & 0xf;
  342.     d_index = (int)(start >> LOG2_EIGHT_K) & 0x7f;
  343.  
  344.     tbl = &(base_tbl[0].
  345.         tbl_address[b_index].
  346.         tbl_address[c_index].
  347.             tbl_address[d_index]);
  348.  
  349.     dt = tbl->page_type.dt;
  350.     wp = tbl->page_type.wp;
  351.     s = tbl->page_type.s;
  352.  
  353.     /*
  354.      * This can be optimized: while in a single megabyte, a quick loop on
  355.      * successive long_desc's will work; similarly, while in a single 16MB,
  356.      * an outer loop on the same C-level table works.
  357.      */
  358.  
  359.     while (len) {
  360.     /*
  361.      * a_index is always zero.  Only the first 256MB is mapped.
  362.      * b_index is the 16MB number of the page.
  363.      * c_index is the 1MB number of that page within the 16MB (0-15)
  364.      * d_index is the 8K number within that 1MB (0-127).
  365.      */
  366.     b_index = (int)(start >> LOG2_16_MEG);
  367.     c_index = (int)(start >> LOG2_ONE_MEG) & 0xf;
  368.     d_index = (int)(start >> LOG2_EIGHT_K) & 0x7f;
  369.  
  370.     tbl = base_tbl;            /* tbl := start of A table  */
  371.     tbl = tbl[0].tbl_address;    /*           B table  */
  372.     tbl = tbl[b_index].tbl_address;    /*           C table  */
  373.     tbl = tbl[c_index].tbl_address;    /*           D table  */
  374.     tbl = &tbl[d_index];        /* tbl := addr of page entry */
  375.  
  376.     if ((tbl->page_type.dt != dt) ||
  377.         (tbl->page_type.s != s) ||
  378.         (tbl->page_type.wp != wp)) {
  379.             /* fail because it's not all the same protection */
  380.         return 0;
  381.     }
  382.     len -= EIGHT_K;
  383.     start += EIGHT_K;
  384.     }
  385.     /* we passed -- all the pages in question have the same prot. status */
  386.     return (wp << 3) | (s << 2) | dt | 0x8000;
  387. }
  388.  
  389. static void
  390. mark_pages(long_desc *base_tbl,ulong start,ulong len,
  391.         ushort dt_val, ushort s_val, ushort wp_val, PROC *proc)
  392. {
  393.     int b_index, c_index, d_index;
  394.     long_desc *tbl;
  395.     ulong oldstart, oldlen;
  396.  
  397.     if (no_mem_prot) return;
  398.  
  399.     oldstart = start;
  400.     oldlen = len;
  401.  
  402. #ifdef MEMPROT_SHORTCUT
  403.     /*
  404.      * Take a shortcut here: we're done if first page of the region is
  405.      * already right. We duplicate the top of the "while" loop to do it,
  406.      * but what the heck.
  407.      */
  408.     /* I don't think this shortcut is a good idea, since while we
  409.      * are doing Mshrink or Srealloc we may very well have a region
  410.      * with mixed page types -- ERS
  411.      */
  412.  
  413.     b_index = (start >> LOG2_16_MEG);
  414.     c_index = (start >> LOG2_ONE_MEG) & 0xf;
  415.     d_index = (start >> LOG2_EIGHT_K) & 0x7f;
  416.  
  417.     tbl = &(base_tbl[0].
  418.         tbl_address[b_index].
  419.         tbl_address[c_index].
  420.             tbl_address[d_index]);
  421.  
  422.     if (tbl->page_type.dt == dt_val &&
  423.     tbl->page_type.s == s_val &&
  424.     tbl->page_type.wp == wp_val) {
  425. /*
  426.         TRACE(("mark_pages a:0 b:%d c:%d d:%d (same)",
  427.             b_index,c_index,d_index));
  428. */
  429.         return;
  430.     }
  431.  
  432. #endif /* MEMPROT_SHORTCUT */
  433. /*
  434.     MP_DEBUG(("mark_pages a:0 b:%d c:%d d:%d (diff)",b_index,c_index,d_index));
  435. */
  436.  
  437.     /*
  438.      * This can be optimized: while in a single megabyte, a quick loop on
  439.      * successive long_desc's will work; similarly, while in a single 16MB,
  440.      * an outer loop on the same C-level table works.
  441.      */
  442.  
  443.     while (len) {
  444.     /*
  445.      * a_index is always zero.  Only the first 256MB is mapped.
  446.      * b_index is the 16MB number of the page.
  447.      * c_index is the 1MB number of that page within the 16MB (0-15)
  448.      * d_index is the 8K number within that 1MB (0-127).
  449.      */
  450.     b_index = (int)(start >> LOG2_16_MEG);
  451.     c_index = (int)(start >> LOG2_ONE_MEG) & 0xf;
  452.     d_index = (int)(start >> LOG2_EIGHT_K) & 0x7f;
  453.  
  454.     tbl = base_tbl;            /* tbl := start of A table  */
  455.     tbl = tbl[0].tbl_address;    /*           B table  */
  456.     tbl = tbl[b_index].tbl_address;    /*           C table  */
  457.     tbl = tbl[c_index].tbl_address;    /*           D table  */
  458.     tbl = &tbl[d_index];        /* tbl := addr of page entry */
  459.  
  460.     tbl->page_type.dt = dt_val;
  461.     tbl->page_type.s = s_val;
  462.     tbl->page_type.wp = wp_val;
  463.     len -= EIGHT_K;
  464.     start += EIGHT_K;
  465.     }
  466.  
  467. #if 0
  468.     /*
  469.      * Flush the mmu address translation cache only if we changed curproc's
  470.      * table.
  471.      */
  472.  
  473.     if (proc == curproc) flush_pmmu(oldstart, oldlen);
  474. #else
  475.     flush_pmmu(oldstart, oldlen);
  476. #endif
  477. }
  478.  
  479. /* get_prot_mode(r): returns the type of protection region r
  480.  * has
  481.  */
  482.  
  483. int
  484. get_prot_mode(r)
  485.     MEMREGION *r;
  486. {
  487.     ulong start = r->loc;
  488.  
  489.     if (no_mem_prot)
  490.         return PROT_G;
  491.     return global_mode_table[(start >> 13)];
  492. }
  493.  
  494. void
  495. mark_region(region,mode)
  496. MEMREGION *region;
  497. short mode;
  498. {
  499.     ulong start = region->loc;
  500.     ulong len = region->len;
  501.     ulong i;
  502.     ushort dt_val, s_val, wp_val;
  503.     PROC *proc;
  504.     MEMREGION **mr;
  505.     int change;
  506.  
  507.     if (no_mem_prot) return;
  508.  
  509.     MP_DEBUG(("mark_region %lx len %lx mode %d",start,len,mode));
  510.     
  511.     if (mode == PROT_NOCHANGE) {
  512.     mode = global_mode_table[(start >> 13)];
  513.     }
  514.  
  515.     if (global_mode_table[(start >> 13)] == mode) {
  516.         change = 0;
  517.     }
  518.     else change = 1;
  519.  
  520.     /* mark the global page table */
  521.  
  522.     memset(&global_mode_table[start >> 13],mode,(len >> 13));
  523.  
  524.     for (proc = proclist; proc; proc = proc->gl_next) {
  525.     assert(proc->page_table);
  526.     if (mode == PROT_I || mode == PROT_G) {
  527.         /* everybody gets the same flags */
  528.         goto notowner;
  529.     }
  530.     if (proc->memflags & F_OS_SPECIAL) {
  531.         /* you're special; you get owner flags */
  532.         MP_DEBUG(("mark_region: pid %d is an OS special!",proc->pid));
  533.         goto owner;
  534.     }
  535.     if (0 != (mr = proc->mem)) {
  536.         for (i = 0; i < proc->num_reg; i++, mr++) {
  537.         if (*mr == region) {
  538.             MP_DEBUG(("mark_region: pid %d is an owner",proc->pid));
  539. owner:
  540.             dt_val = 1;
  541.             s_val = 0;
  542.             wp_val = 0;
  543.             goto gotvals;
  544.         }
  545.         }
  546.     }
  547.  
  548. notowner:
  549.  
  550. /* if you get here you're not an owner, or mode is G or I */
  551.     MP_DEBUG(("mark_region: pid %d gets non-owner modes",proc->pid));
  552.  
  553.     dt_val = other_dt[mode];
  554.     s_val = other_s[mode];
  555.     wp_val = other_wp[mode];
  556.  
  557. gotvals:
  558.     mark_pages(proc->page_table,start,len,dt_val,s_val,wp_val,proc);
  559.     }
  560. }
  561.  
  562. /*
  563.  * prot_temp: temporarily alter curproc's access to memory.
  564.  * Pass in a -1 to give curproc global access; returns a cookie.  Call
  565.  * again with that cookie to return the memory to the old mode.
  566.  * There should be no context switches or memory protection changes
  567.  * in the meantime.
  568.  *
  569.  * If called with mode == -1, returns...
  570.  *    -1 if mem prot is off -- no error, no action.
  571.  *     0 if the pages are not all the same.
  572.  *     1 if the pages are not all controlled by the page tables.
  573.  *
  574.  * When mode != -1, returns...
  575.  *    0 for success (should never fail).  There is little checking.
  576.  * Calling with mode == 0 or 1 results in zero to spoof success, but in fact
  577.  * this is an error.  Mode is only really valid if (mode & 0x8000).
  578.  */
  579.  
  580. int
  581. prot_temp(loc,len,mode)
  582. ulong loc;
  583. ulong len;
  584. int mode;
  585. {
  586.     int cookie;
  587.  
  588.     if (no_mem_prot) return -1;
  589.  
  590.     /* round start down to the previous page and len up to the next one. */
  591.     loc &= ~MASKBITS;
  592.     len = ROUND(len);
  593.  
  594.     if (mode == 0 || mode == 1) return 0;    /* do nothing */
  595.     if (mode == -1) {
  596.     cookie = get_page_cookie(curproc->page_table,loc,len);
  597.  
  598.     /* if not all controlled, return status */
  599.     if (cookie == 0 || cookie == 1) return cookie;
  600.  
  601.     mark_pages(curproc->page_table,loc,len,1,0,0,curproc);
  602.  
  603.     return cookie;
  604.     }
  605.     else {
  606.     mark_pages(curproc->page_table,loc,len,
  607.             mode&3,mode&4,mode&8,curproc);
  608.     return 0;
  609.     }
  610. }
  611.  
  612. /*
  613.  * init_page_table: fill in the page table for the indicated process. The
  614.  * master page map is consulted for the modes of all pages, and the memory
  615.  * region data structures are consulted to see if this process is the owner
  616.  * of any of those tables.
  617.  *
  618.  * This also sets crp and tc in both ctxts of the process.  If this is the
  619.  * first call, then the CPU tc is cleared, the TT0 and TT1 regs are zapped,
  620.  * and then this proc's crp and tc are loaded into it.
  621.  */
  622.  
  623. static short mmu_is_set_up = 0;
  624.  
  625. void
  626. init_page_table(proc)
  627. PROC *proc;
  628. {
  629.     long_desc *tptr;
  630.     long_desc *tbl_a;        /* top-level table */
  631.     long_desc *tbl_b0;        /* second level, handles $0 nybble */
  632.     long_desc *tbl_bf;        /* handles $F nybble */
  633.     long_desc *tbl_c;        /* temp pointer to start of 16MB */
  634.     ulong p, q, r;
  635.     ulong i, j, k;
  636.     int g;
  637.     MEMREGION **mr;
  638.  
  639.     if (no_mem_prot) return;
  640.  
  641.     if (proc->pid)
  642.         TRACELOW(("init_page_table(proc=%lx, pid %d)",proc,proc->pid));
  643.  
  644.     assert(proc && proc->page_table);
  645.  
  646.     tptr = proc->page_table;
  647.     tbl_a = tptr;
  648.     tptr += TBL_SIZE;
  649.     tbl_b0 = tptr;
  650.     tptr += TBL_SIZE;
  651.     tbl_bf = tptr;
  652.     tptr += TBL_SIZE;
  653.  
  654.     /*
  655.      * table A indexes by the first nybble: $0 and $F refer to their tables,
  656.      * $1-$7 are uncontrolled, cacheable; $8-$E are uncontrolled, ci.
  657.      */
  658.  
  659.     tbl_a[0].page_type = page_ptr;
  660.     tbl_a[0].tbl_address = tbl_b0;
  661.  
  662.     for (i=1; i<0xf; i++) {
  663.     if (i < 8)  tbl_a[i].page_type = g_page;
  664.     else        tbl_a[i].page_type = g_ci_page;
  665.     tbl_a[i].tbl_address = (long_desc *)(i << 28);
  666.     }
  667.  
  668.     /* $F entry of table A refers to table BF */
  669.     tbl_a[0xf].page_type = page_ptr;
  670.     tbl_a[0xf].tbl_address = tbl_bf;
  671.  
  672.     /*
  673.      * table B0: entry 0 is $00, the 16MB of ST address space.
  674.      */
  675.  
  676.     tbl_b0[0].page_type = page_ptr;
  677.     tbl_b0[0].tbl_address = tptr;
  678.     tbl_c = tptr;
  679.     tptr += TBL_SIZE;
  680.  
  681.     /* for each megabyte that is RAM, allocate a table */
  682.     for (i = 0, k = 0, p = 0; p < mint_top_st; i++, p += 0x00100000L) {
  683.     tbl_c[i].page_type = page_ptr;
  684.     tbl_c[i].tbl_address = tptr;
  685.  
  686.     /* for each page in this megabyte, write a page entry */
  687.     for (q = p, j = 0; j < 128; j++, q += 0x2000, k++) {
  688.         tptr->page_type = *proto_page_type[global_mode_table[k]];
  689.         tptr->tbl_address = (long_desc *)q;
  690.         tptr++;
  691.     }
  692.     }
  693.  
  694.     /* now for each megabyte from mint_top_st to ROM, mark global */
  695.     for ( ; p < 0x00E00000L; i++, p += 0x00100000L) {
  696.     tbl_c[i].page_type = g_page;
  697.     tbl_c[i].tbl_address = (long_desc *)p;
  698.     }
  699.  
  700.     /* fill in the E and F tables: 00Ex is ROM, 00Fx is I/O  */
  701.     tbl_c[i].page_type = g_page;
  702.     tbl_c[i].tbl_address = (long_desc *)p;
  703.     i++, p += 0x00100000L;
  704.     tbl_c[i].page_type = g_ci_page;
  705.     tbl_c[i].tbl_address = (long_desc *)p;
  706.  
  707.         /* Done with tbl_c for 0th 16MB; go on to TT RAM */
  708.  
  709. /* 
  710.     structure:
  711.  
  712.     for (i = each 16MB that has any TT RAM in it)
  713.     allocate a table tbl_c, point tbl_b0[i] at it
  714.     for (j = each 1MB that is RAM)
  715.         allocate a table, point tbl_c[j] at it
  716.         for (k = each page in the megabyte)
  717.         fill in tbl_c[j][k] with page entry from global_mode_table
  718.     for (j = the rest of the 16MB)
  719.         set tbl_c[j] to "global, cacheable"
  720.  
  721.     for (i = the rest of the 16MBs from here to $7F)
  722.     set tbl_b0[i] to "global, cacheable"
  723.  
  724.     for (i = the rest of the 16MBs from $80 up to but not incl. $FF)
  725.     set tbl_b0[i] to "global, not cacheable"
  726. */
  727.  
  728.     /* i counts 16MBs */
  729.     for (i = 1, p = 0x01000000L, g = 2048;
  730.      p < mint_top_tt;
  731.      p += SIXTEEN_MEG, i++) {
  732.         tbl_b0[i].page_type = page_ptr;
  733.         tbl_b0[i].tbl_address = tptr;
  734.         tbl_c = tptr;
  735.         tptr += TBL_SIZE;
  736.  
  737.         /* j counts MBs */
  738.         for (j = 0, q = p; j < 16 && q < mint_top_tt; q += ONE_MEG, j++) {
  739.         tbl_c[j].page_type = page_ptr;
  740.         tbl_c[j].tbl_address = tptr;
  741.         /* k counts pages (8K) */
  742.         for (r = q, k = 0; k < 128; k++, r += 0x2000, g++) {
  743.             tptr->page_type = *proto_page_type[global_mode_table[g]];
  744.             tptr->tbl_address = (long_desc *)r;
  745.             tptr++;
  746.         }
  747.         }
  748.         for ( ; j < 16; j++, q += ONE_MEG) {
  749.         /* fill in the rest of this 16MB */
  750.         tbl_c[j].page_type = g_page;
  751.         tbl_c[j].tbl_address = (long_desc *)q;
  752.         }
  753.     }
  754.  
  755.     /* fill in the rest of $00-$0F as cacheable */
  756.     for ( ; i < 16; i++, p += SIXTEEN_MEG) {
  757.     tbl_b0[i].page_type = g_page;
  758.     tbl_b0[i].tbl_address = (long_desc *)p;
  759.     }
  760.  
  761.     /* done with TT RAM in table b0; do table bf */
  762.  
  763.     /*
  764.      * Table BF: translates addresses starting with $F.  First 15 are
  765.      * uncontrolled, cacheable; last one translates $FF, which
  766.      * which shadows $00 (the 16MB ST address space).  The rest
  767.      * are uncontrolled, not cacheable.
  768.      *
  769.      * The table address of the copy has a 1 in the low (unused) bit, which
  770.      * is a signal to the table dumper not to dump this, as it's a copy
  771.      * of tbl_b0[0].
  772.      */
  773.  
  774.     for (i=0; i<0xf; i++) {
  775.     tbl_bf[i].page_type = g_ci_page;
  776.     tbl_bf[i].tbl_address = (long_desc *)((i << 24) | 0xf0000000L);
  777.     }
  778.     tbl_bf[0xf] = tbl_b0[0];
  779.     *(ulong *)(&(tbl_bf[0xf].tbl_address)) |= 1;
  780.  
  781.     proc->ctxt[0].crp.limit = 0x7fff;    /* disable limit function */
  782.     proc->ctxt[0].crp.dt = 3;        /* points to valid 8-byte entries */
  783.     proc->ctxt[0].crp.tbl_address = tbl_a;
  784.     proc->ctxt[1].crp = proc->ctxt[0].crp;
  785.     proc->ctxt[0].tc = tc;
  786.     proc->ctxt[1].tc = tc;
  787.  
  788.     /*
  789.      * OK, memory tables are now there as if you're a non-owner of every
  790.      * page.  Now for each region you ARE an owner of, mark with owner
  791.      * modes.
  792.      */
  793.  
  794.     mr = proc->mem;
  795.     for (i=0; i < proc->num_reg; i++, mr++) {
  796.     if (*mr) {
  797.             mark_pages(proc->page_table,(*mr)->loc,(*mr)->len,1,0,0,proc);
  798.         }
  799.     }
  800.  
  801.     if (!mmu_is_set_up) {
  802.     set_mmu(proc->ctxt[0].crp,proc->ctxt[0].tc);
  803.     mmu_is_set_up = 1;
  804.     }
  805. }
  806.  
  807. /*
  808.  * This routine is called when procfs detects that a process wants to be an
  809.  * OS SPECIAL.  The AES, SCRENMGR, and DESKTOP do this, and so does FSMGDOS
  810.  * and possibly some other stuff. It has to re-mark every page in that
  811.  * process' page table based on its new special status. The "special
  812.  * status" is "you get global access to all of memory" and "everybody
  813.  * gets Super access to yours."  It is the caller's responsibility
  814.  * to set proc's memflags, usually to (F_OS_SPECIAL | F_PROT_S).
  815.  */
  816.  
  817. void
  818. mem_prot_special(proc)
  819. PROC *proc;
  820. {
  821.     MEMREGION **mr;
  822.     int i;
  823.  
  824.     if (no_mem_prot) return;
  825.  
  826.     TRACE(("mem_prot_special(pid %d)",proc->pid));
  827.  
  828.     /*
  829.      * This marks ALL memory, allocated or not, as accessible. When memory
  830.      * is freed even F_OS_SPECIAL processes lose access to it. So one or
  831.      * the other of these is a bug, depending on how you want it to work.
  832.      */
  833.     mark_pages(proc->page_table,0,mint_top_st,1,0,0,proc);
  834.     if (mint_top_tt) {
  835.     mark_pages(proc->page_table,
  836.             0x01000000L,
  837.             mint_top_tt - 0x01000000L,
  838.             1,0,0,
  839.             proc);
  840.     }
  841.  
  842.     /*
  843.      * In addition, mark all the pages the process already owns as "super"
  844.      * in all other processes.  Thus the "special" process can access all
  845.      * of memory, and any process can access the "special" process' memory
  846.      * when in super mode.
  847.      */
  848.  
  849.     mr = proc->mem;
  850.  
  851.     for (i=0; i < proc->num_reg; i++, mr++) {
  852.     if (*mr) {
  853.         mark_region(*mr,PROT_S);
  854.     }
  855.     }
  856. }
  857.     
  858. /*----------------------------------------------------------------------------
  859.  * DEBUGGING SECTION
  860.  *--------------------------------------------------------------------------*/
  861.  
  862. static void
  863. _dump_tree(tbl,level)
  864. long_desc tbl;
  865. int level;
  866. {
  867.     int i, j;
  868.     long_desc *p;
  869.     static char spaces[9] = "        ";
  870.  
  871.     /* print the level and display the table descriptor */
  872.     FORCE("\r%s s:%x wp:%x dt:%x a:%08lx",
  873.     &spaces[8-(level*2)],
  874.     tbl.page_type.s,
  875.     tbl.page_type.wp,
  876.     tbl.page_type.dt,
  877.     tbl.tbl_address);
  878.  
  879.     if (tbl.page_type.dt == 3) {
  880.     if (level == 0) {
  881.         j = (1 << tc.tia);
  882.     }
  883.     else if (level == 1) {
  884.         j = (1 << tc.tib);
  885.     }
  886.     else if (level == 2) {
  887.         j = (1 << tc.tic);
  888.     }
  889.     else {
  890.         j = (1 << tc.tid);
  891.     }
  892.  
  893.     /* don't show table if it's the duplicate */
  894.     if ((ulong)tbl.tbl_address & 1) return;
  895.  
  896.     ++level;
  897.     p = tbl.tbl_address;
  898.     for (i=0; i<j; i++, p++) {
  899.         _dump_tree(*p,level);
  900.     }
  901.     }
  902. }
  903.  
  904. static char modesym[] = { 'p', 'g', 's', 'r', 'i' };
  905.  
  906. void
  907. QUICKDUMP()
  908. {
  909.     char outstr[33];
  910.     ulong i, j, end;
  911.  
  912.     if (no_mem_prot) return;
  913.  
  914.     FORCE("STRAM global table:");
  915.     outstr[32] = '\0';
  916.     end = mint_top_st / QUANTUM;
  917.     for (i = 0; i < end; i += 32) {
  918.     for (j=0; j<32; j++) {
  919.         outstr[j] = modesym[global_mode_table[j+i]];
  920.     }
  921.     FORCE("%08lx: %s",i*8192L,outstr);
  922.     }
  923.  
  924.     if (mint_top_tt) {
  925.     FORCE("TTRAM global table:");
  926.     end = mint_top_tt / QUANTUM;
  927.     for (i = 2048; i < end; i += 32) {
  928.         for (j=0; j<32; j++) {
  929.         outstr[j] = modesym[global_mode_table[j+i]];
  930.         }
  931.         FORCE("%08lx: %s",i*8192L,outstr);
  932.     }
  933.     }
  934. }
  935.  
  936. const char *berr_msg[] = { 
  937. /*  "........." */
  938.     "private  ",
  939.     "global   ",    /* turned into "hardware" for violation reports */
  940.     "super    ",
  941.     "readable ",
  942.     "free     ",
  943.     "hardware "        /* used when the memory is not controlled by us */
  944. };
  945.  
  946. void
  947. report_buserr()
  948. {
  949.     long_desc *tbl;
  950.     const char *vmsg = NULL;
  951.     short mode;
  952.     ulong aa, pc;
  953.     char alertbuf[5*32+16];    /* enough for an alert */
  954.     char *aptr;
  955.  
  956.     if (no_mem_prot) return;
  957.  
  958.     tbl = (long_desc *)curproc->exception_tbl;
  959.     aa = curproc->exception_addr;
  960.     pc = curproc->exception_pc;
  961.     if ((mint_top_tt && aa < mint_top_tt) || (aa < mint_top_st)) {
  962.     mode = global_mode_table[(curproc->exception_addr >> 13)];
  963.     if (mode == PROT_G) {
  964.         /* page is global: obviously a hardware bus error */
  965.         mode = 5;
  966.     }
  967.     }
  968.     else {
  969.     /* (addr is > mint_top_tt) set mode = 5 so we don't look for owners */
  970.         mode = 5;
  971.     }
  972.     vmsg = berr_msg[mode];
  973.  
  974.     /* construct an AES alert box for this error:
  975.     | PROCESS  "buserrxx"  KILLED: |
  976.     | MEMORY VIOLATION.  (PID 000) |
  977.     |                              |
  978.     | Type: ......... PC: pc...... |
  979.     | Addr: ........  BP: ........ |
  980.     */
  981.  
  982.     /* we play games to get around 128-char max for ksprintf */
  983.     ksprintf(alertbuf,"[1][ PROCESS  \"%s\"  KILLED: |",curproc->name);
  984.     aptr = alertbuf + strlen(alertbuf);
  985.     ksprintf(aptr," MEMORY VIOLATION.  (PID %03d) | |",curproc->pid);
  986.     aptr = alertbuf + strlen(alertbuf);
  987.     ksprintf(aptr," Type: %s PC: %08lx |",vmsg,pc);
  988.     aptr = alertbuf + strlen(alertbuf);
  989.     ksprintf(aptr," Addr: %08lx  BP: %08lx ][ OK ]",aa,curproc->base);
  990.     if (!_ALERT(alertbuf)) {
  991.         /* this will call _alert again, but it will just fail again */
  992.         ALERT("MEMORY VIOLATION: type=%s AA=%lx PC=%lx BP=%lx",
  993.             vmsg,aa,pc,curproc->base);
  994.     }
  995.         
  996.     if (curproc->pid == 0 || curproc->memflags & F_OS_SPECIAL) {
  997.     /* the system is so thoroughly hosed that anything we try will
  998.      * likely cause another bus error; so let's just hang up
  999.      */
  1000.     FATAL("Operating system killed");
  1001.     }
  1002. }
  1003.  
  1004. /*
  1005.  * big_mem_dump is a biggie: for each page in the system, it
  1006.  * displays the PID of the (first) owner and the protection mode.
  1007.  * The output has three chars per page, and eight chars per line.
  1008.  * The first page of a region is marked with the mode, and the
  1009.  * rest with a space.
  1010.  *
  1011.  * Logic:
  1012.     for (mp = *core; mp; mp++) {
  1013.     for (each page of this region) {
  1014.         if (start of line) {
  1015.         output line starter;
  1016.         }
  1017.         if (start of region) {
  1018.         output mode of this page;
  1019.         determine owner;
  1020.         output owner;
  1021.         }
  1022.         else {
  1023.         output space;
  1024.         output owner;
  1025.         }
  1026.         }
  1027.     }
  1028.  */
  1029.  
  1030. void
  1031. BIG_MEM_DUMP(bigone,proc)
  1032. int bigone;
  1033. PROC *proc;
  1034. {
  1035. #ifndef NO_DEBUG_INFO
  1036.     char linebuf[128];
  1037.     char *lp = linebuf;
  1038.     MEMREGION *mp, **mr, **map;
  1039.     PROC *p;
  1040.     ulong loc;
  1041.     short owner = 0;
  1042.     short i;
  1043.     short first;
  1044.  
  1045.  
  1046.   if (no_mem_prot) return;
  1047.  
  1048.   for (map = core; map != 0; ((map == core) ? (map = alt) : (map = 0))) {
  1049.     FORCE("Annotated memory dump for %s",(map == core ? "core" : "alt"));
  1050.     first = 1;
  1051.     *linebuf = '\0';
  1052.     for (mp = *map; mp; mp = mp->next) {
  1053.     for (loc = mp->loc; loc < (mp->loc + mp->len); loc += EIGHT_K) {
  1054.         if (first || ((loc & 0x1ffff) == 0)) {
  1055.         if (*linebuf) FORCE(linebuf);
  1056.         ksprintf(linebuf,"\r%08lx: ",loc);
  1057.         lp = &linebuf[11];
  1058.         first = 0;
  1059.         }
  1060.         if (loc == mp->loc) {
  1061.         *lp++ = modesym[global_mode_table[loc / EIGHT_K]];
  1062.  
  1063.         for (p = proclist; p; p = p->gl_next) {
  1064.             if (p->mem) {
  1065.             mr = p->mem;
  1066.             for (i=0; i < p->num_reg; i++, mr++) {
  1067.                 if (*mr == mp) {
  1068.                 owner = p->pid;
  1069.                 goto gotowner;
  1070.                 }
  1071.             }
  1072.             }
  1073.         }
  1074.         owner = 000;
  1075. gotowner:
  1076.         ksprintf(lp,"%03d",owner);
  1077.         lp += 3;
  1078.         }
  1079.         else {
  1080.         *lp++ = ' ';
  1081.         *lp++ = '-';
  1082.         *lp++ = '-';
  1083.         *lp++ = '-';
  1084.         *lp = '\0';    /* string is always null-terminated */
  1085.         }
  1086.         }
  1087.     }
  1088.     FORCE(linebuf);
  1089.   }
  1090.  
  1091.     if (bigone) {
  1092.     long_desc tbl;
  1093.  
  1094.     /* fill in tbl with the only parts used at the top level */
  1095.     tbl.page_type.dt = proc->ctxt[CURRENT].crp.dt;
  1096.     tbl.tbl_address = proc->ctxt[CURRENT].crp.tbl_address;
  1097.     _dump_tree(tbl,0);
  1098.     }
  1099. #endif /* NO_DEBUG_INFO */
  1100. }
  1101.  
  1102.  
  1103. /*
  1104.  * Can the process "p" access the "nbytes" long
  1105.  * block of memory starting at "start"?
  1106.  * If it would be a legal access, the current
  1107.  * process is given temporary access via
  1108.  * prot_temp.
  1109.  * Returns a cookie like the one prot_temp
  1110.  * returns; if the process shouldn't have
  1111.  * access to the memory, returns 1.
  1112.  *
  1113.  * BUG: should actually read p's page table to
  1114.  * determine access
  1115.  */
  1116.  
  1117. int
  1118. mem_access_for(p, start, nbytes)
  1119.     PROC *p;
  1120.     ulong start;
  1121.      long nbytes;
  1122. {
  1123.     MEMREGION **mr;
  1124.     int i;
  1125.  
  1126.     if (no_mem_prot) return -1;
  1127.     if (start >= (ulong)p && start+nbytes <= (ulong)(p+1))
  1128.         return -1;
  1129.     if (p == rootproc)
  1130.         goto win_and_mark;
  1131.  
  1132.     mr = p->mem;
  1133.     if (mr) {
  1134.         for (i = 0; i < p->num_reg; i++, mr++) {
  1135.         if (*mr) {
  1136.             if (((*mr)->loc <= start) &&
  1137.             ((*mr)->loc + (*mr)->len >= start + nbytes))
  1138.                 goto win_and_mark;
  1139.         }
  1140.         }
  1141.     }
  1142.  
  1143.     return 0;    /* we don't own this memory */
  1144.  
  1145. win_and_mark:
  1146.     return prot_temp(start, nbytes, -1);
  1147. }
  1148.