home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / sim / ppc / vm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-21  |  30.3 KB  |  1,163 lines

  1. /*  This file is part of the program psim.
  2.  
  3.     Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19.     */
  20.  
  21.  
  22. #ifndef _VM_C_
  23. #define _VM_C_
  24.  
  25. #include "basics.h"
  26. #include "registers.h"
  27. #include "device.h"
  28. #include "corefile.h"
  29. #include "vm.h"
  30. #include "interrupts.h"
  31. #include "mon.h"
  32.  
  33. /* OEA vs VEA
  34.  
  35.    For the VEA model, the VM layer is almost transparent.  It's only
  36.    purpose is to maintain separate core_map's for the instruction
  37.    and data address spaces.  This being so that writes to instruction
  38.    space or execution of a data space is prevented.
  39.  
  40.    For the OEA model things are more complex.  The reason for separate
  41.    instruction and data models becomes crucial.  The OEA model is
  42.    built out of three parts.  An instruction map, a data map and an
  43.    underlying structure that provides access to the VM data kept in
  44.    main memory. */
  45.  
  46.  
  47. /* OEA data structures:
  48.  
  49.    The OEA model maintains internal data structures that shadow the
  50.    semantics of the various OEA VM registers (BAT, SR, etc).  This
  51.    allows a simple efficient model of the VM to be implemented.
  52.  
  53.    Consistency between OEA registers and this model's internal data
  54.    structures is maintained by updating the structures at
  55.    `synchronization' points.  Of particular note is that (at the time
  56.    of writing) the memory data types for BAT registers are rebuilt
  57.    when ever the processor moves between problem and system states.
  58.  
  59.    Unpacked values are stored in the OEA so that they correctly align
  60.    to where they will be needed by the PTE address. */
  61.  
  62.  
  63. /* Protection table:
  64.  
  65.    Matrix of processor state, type of access and validity */
  66.  
  67. typedef enum {
  68.   om_supervisor_state,
  69.   om_problem_state,
  70.   nr_om_modes
  71. } om_processor_modes;
  72.  
  73. typedef enum {
  74.   om_data_read, om_data_write,
  75.   om_instruction_read, om_access_any,
  76.   nr_om_access_types
  77. } om_access_types;
  78.  
  79. static int om_valid_access[2][4][nr_om_access_types] = {
  80.   /* read, write, instruction, any */
  81.   /* K bit == 0 */
  82.   { /*r  w  i  a       pp */
  83.     { 1, 1, 1, 1 }, /* 00 */
  84.     { 1, 1, 1, 1 }, /* 01 */
  85.     { 1, 1, 1, 1 }, /* 10 */
  86.     { 1, 0, 1, 1 }, /* 11 */
  87.   },
  88.   /* K bit == 1  or P bit valid */
  89.   { /*r  w  i  a       pp */
  90.     { 0, 0, 0, 0 }, /* 00 */
  91.     { 1, 0, 1, 1 }, /* 01 */
  92.     { 1, 1, 1, 1 }, /* 10 */
  93.     { 1, 0, 1, 1 }, /* 11 */
  94.   }
  95. };
  96.  
  97.  
  98. /* Bat translation:
  99.  
  100.    The bat data structure only contains information on valid BAT
  101.    translations for the current processor mode and type of access. */
  102.  
  103. typedef struct _om_bat {
  104.   unsigned_word block_effective_page_index;
  105.   unsigned_word block_effective_page_index_mask;
  106.   unsigned_word block_length_mask;
  107.   unsigned_word block_real_page_number;
  108.   int protection_bits;
  109. } om_bat;
  110.  
  111. enum _nr_om_bat_registers {
  112.   nr_om_bat_registers = 4
  113. };
  114.  
  115. typedef struct _om_bats {
  116.   int nr_valid_bat_registers;
  117.   om_bat bat[nr_om_bat_registers];
  118. } om_bats;
  119.  
  120.  
  121. /* Segment TLB:
  122.  
  123.    In this model the 32 and 64 bit segment tables are treated in very
  124.    similar ways.  The 32bit segment registers are treated as a
  125.    simplification of the 64bit segment tlb */
  126.  
  127. enum _om_segment_tlb_constants {
  128. #if (WITH_TARGET_WORD_BITSIZE == 64)
  129.   sizeof_segment_table_entry_group = 128,
  130.   sizeof_segment_table_entry = 16,
  131. #endif
  132.   om_segment_tlb_index_start_bit = 32,
  133.   om_segment_tlb_index_stop_bit = 35,
  134.   nr_om_segment_tlb_entries = 16,
  135.   nr_om_segment_tlb_constants
  136. };
  137.  
  138. typedef struct _om_segment_tlb_entry {
  139.   int key[nr_om_modes];
  140.   om_access_types invalid_access; /* set to instruction if no_execute bit */
  141.   unsigned_word masked_virtual_segment_id; /* aligned ready for pte addr */
  142. #if (WITH_TARGET_WORD_BITSIZE == 64)
  143.   int is_valid;
  144.   unsigned_word masked_effective_segment_id;
  145. #endif
  146. } om_segment_tlb_entry;
  147.  
  148. typedef struct _om_segment_tlb {
  149.   om_segment_tlb_entry entry[nr_om_segment_tlb_entries];
  150. } om_segment_tlb;
  151.  
  152.  
  153. /* Page TLB:
  154.  
  155.    This OEA model includes a small direct map Page TLB.  The tlb is to
  156.    cut down on the need for the OEA to perform walks of the page hash
  157.    table. */
  158.  
  159. enum _om_page_tlb_constants {
  160.   om_page_tlb_index_start_bit = 46,
  161.   om_page_tlb_index_stop_bit = 51,
  162.   nr_om_page_tlb_entries = 64,
  163. #if (WITH_TARGET_WORD_BITSIZE == 64)
  164.   sizeof_pte_group = 128,
  165.   sizeof_pte = 16,
  166. #endif
  167. #if (WITH_TARGET_WORD_BITSIZE == 32)
  168.   sizeof_pte_group = 64,
  169.   sizeof_pte = 8,
  170. #endif
  171.   nr_om_page_tlb_constants
  172. };
  173.  
  174. typedef struct _om_page_tlb_entry {
  175.   int protection;
  176.   int changed;
  177.   unsigned_word real_address_of_pte_1;
  178.   unsigned_word masked_virtual_segment_id;
  179.   unsigned_word masked_page;
  180.   unsigned_word masked_real_page_number;
  181. } om_page_tlb_entry;
  182.  
  183. typedef struct _om_page_tlb {
  184.   om_page_tlb_entry entry[nr_om_page_tlb_entries];
  185. } om_page_tlb;
  186.  
  187.  
  188. /* memory translation:
  189.  
  190.    OEA memory translation possibly involves BAT, SR, TLB and HTAB
  191.    information*/
  192.  
  193. typedef struct _om_map {
  194.  
  195.   /* local cache of register values */
  196.   int is_relocate;
  197.   int is_problem_state;
  198.  
  199.   /* block address translation */
  200.   om_bats *bat_registers;
  201.  
  202.   /* failing that, translate ea to va using segment tlb */
  203. #if (WITH_TARGET_WORD_BITSIZE == 64)
  204.   unsigned_word real_address_of_segment_table;
  205. #endif
  206.   om_segment_tlb *segment_tlb;
  207.  
  208.   /* then va to ra using hashed page table and tlb */
  209.   unsigned_word real_address_of_page_table;
  210.   unsigned_word page_table_hash_mask;
  211.   om_page_tlb *page_tlb;
  212.  
  213.   /* physical memory for fetching page table entries */
  214.   core_map *physical;
  215.  
  216.   /* address xor for PPC endian */
  217.   unsigned xor[WITH_XOR_ENDIAN];
  218.  
  219. } om_map;
  220.  
  221.  
  222. /* VM objects:
  223.  
  224.    External objects defined by vm.h */
  225.  
  226. struct _vm_instruction_map {
  227.   /* real memory for last part */
  228.   core_map *code;
  229.   /* translate effective to real */
  230.   om_map translation;
  231. };
  232.  
  233. struct _vm_data_map {
  234.   /* translate effective to real */
  235.   om_map translation;
  236.   /* real memory for translated address */
  237.   core_map *read;
  238.   core_map *write;
  239. };
  240.  
  241.  
  242. /* VM:
  243.  
  244.    Underlying memory object.  For the VEA this is just the
  245.    core_map. For OEA it is the instruction and data memory
  246.    translation's */
  247.  
  248. struct _vm {
  249.  
  250.   /* OEA: base address registers */
  251.   om_bats ibats;
  252.   om_bats dbats;
  253.  
  254.   /* OEA: segment registers */
  255.   om_segment_tlb segment_tlb;
  256.  
  257.   /* OEA: translation lookaside buffers */
  258.   om_page_tlb instruction_tlb;
  259.   om_page_tlb data_tlb;
  260.  
  261.   /* real memory */
  262.   core *physical;
  263.  
  264.   /* memory maps */
  265.   vm_instruction_map instruction_map;
  266.   vm_data_map data_map;
  267.  
  268. };
  269.  
  270.  
  271. /* OEA Support procedures */
  272.  
  273.  
  274. STATIC_INLINE_VM\
  275. (unsigned_word)
  276. om_segment_tlb_index(unsigned_word ea)
  277. {
  278.   unsigned_word index = EXTRACTED(ea,
  279.                   om_segment_tlb_index_start_bit,
  280.                   om_segment_tlb_index_stop_bit);
  281.   return index;
  282. }
  283.  
  284. STATIC_INLINE_VM\
  285. (unsigned_word)
  286. om_page_tlb_index(unsigned_word ea)
  287. {
  288.   unsigned_word index = EXTRACTED(ea,
  289.                   om_page_tlb_index_start_bit,
  290.                   om_page_tlb_index_stop_bit);
  291.   return index;
  292. }
  293.  
  294. STATIC_INLINE_VM\
  295. (unsigned_word)
  296. om_hash_page(unsigned_word masked_vsid,
  297.          unsigned_word ea)
  298. {
  299.   unsigned_word extracted_ea = EXTRACTED(ea, 36, 51);
  300. #if (WITH_TARGET_WORD_BITSIZE == 32)
  301.   return masked_vsid ^ INSERTED32(extracted_ea, 7, 31-6);
  302. #endif
  303. #if (WITH_TARGET_WORD_BITSIZE == 64)
  304.   return masked_vsid ^ INSERTED64(extracted_ea, 18, 63-7);
  305. #endif
  306. }
  307.  
  308. STATIC_INLINE_VM\
  309. (unsigned_word)
  310. om_pte_0_api(unsigned_word pte_0)
  311. {
  312. #if (WITH_TARGET_WORD_BITSIZE == 32)
  313.   return EXTRACTED32(pte_0, 26, 31);
  314. #endif
  315. #if (WITH_TARGET_WORD_BITSIZE == 64)
  316.   return EXTRACTED64(pte_0, 52, 56);
  317. #endif
  318. }
  319.  
  320. STATIC_INLINE_VM\
  321. (unsigned_word)
  322. om_pte_0_hash(unsigned_word pte_0)
  323. {
  324. #if (WITH_TARGET_WORD_BITSIZE == 32)
  325.   return EXTRACTED32(pte_0, 25, 25);
  326. #endif
  327. #if (WITH_TARGET_WORD_BITSIZE == 64)
  328.   return EXTRACTED64(pte_0, 62, 62);
  329. #endif
  330. }
  331.  
  332. STATIC_INLINE_VM\
  333. (int)
  334. om_pte_0_valid(unsigned_word pte_0)
  335. {
  336. #if (WITH_TARGET_WORD_BITSIZE == 32)
  337.   return MASKED32(pte_0, 0, 0) != 0;
  338. #endif
  339. #if (WITH_TARGET_WORD_BITSIZE == 64)
  340.   return MASKED64(pte_0, 63, 63) != 0;
  341. #endif
  342. }
  343.  
  344. STATIC_INLINE_VM\
  345. (unsigned_word)
  346. om_ea_masked_page(unsigned_word ea)
  347. {
  348.   return MASKED(ea, 36, 51);
  349. }
  350.  
  351. STATIC_INLINE_VM\
  352. (unsigned_word)
  353. om_ea_masked_byte(unsigned_word ea)
  354. {
  355.   return MASKED(ea, 52, 63);
  356. }
  357.  
  358. STATIC_INLINE_VM\
  359. (unsigned_word)
  360. om_pte_0_masked_vsid(unsigned_word pte_0)
  361. {
  362.   return INSERTED32(EXTRACTED32(pte_0, 1, 24), 7-5, 31-6);
  363. }
  364.  
  365. STATIC_INLINE_VM\
  366. (unsigned_word)
  367. om_pte_1_pp(unsigned_word pte_1)
  368. {
  369.   return MASKED(pte_1, 62, 63); /*PP*/
  370. }
  371.  
  372. STATIC_INLINE_VM\
  373. (int)
  374. om_pte_1_referenced(unsigned_word pte_1)
  375. {
  376.   return EXTRACTED(pte_1, 55, 55);
  377. }
  378.  
  379. STATIC_INLINE_VM\
  380. (int)
  381. om_pte_1_changed(unsigned_word pte_1)
  382. {
  383.   return EXTRACTED(pte_1, 56, 56);
  384. }
  385.  
  386. STATIC_INLINE_VM\
  387. (int)
  388. om_pte_1_masked_rpn(unsigned_word pte_1)
  389. {
  390.   return MASKED(pte_1, 0, 51); /*RPN*/
  391. }
  392.  
  393. STATIC_INLINE_VM\
  394. (unsigned_word)
  395. om_ea_api(unsigned_word ea)
  396. {
  397.   return EXTRACTED(ea, 36, 41);
  398. }
  399.  
  400.  
  401. /* Page and Segment table read/write operators, these need to still
  402.    account for the PPC's XOR operation */
  403.  
  404. STATIC_INLINE_VM\
  405. (unsigned_word)
  406. om_read_word(om_map *map,
  407.          unsigned_word ra,
  408.          cpu *processor,
  409.          unsigned_word cia)
  410. {
  411.   if (WITH_XOR_ENDIAN)
  412.     ra ^= map->xor[sizeof(instruction_word) - 1];
  413.   return core_map_read_word(map->physical, ra, processor, cia);
  414. }
  415.  
  416. STATIC_INLINE_VM\
  417. (void)
  418. om_write_word(om_map *map,
  419.           unsigned_word ra,
  420.           unsigned_word val,
  421.           cpu *processor,
  422.           unsigned_word cia)
  423. {
  424.   if (WITH_XOR_ENDIAN)
  425.     ra ^= map->xor[sizeof(instruction_word) - 1];
  426.   core_map_write_word(map->physical, ra, val, processor, cia);
  427. }
  428.  
  429.  
  430. /* Bring things into existance */
  431.  
  432. INLINE_VM\
  433. (vm *)
  434. vm_create(core *physical)
  435. {
  436.   vm *virtual;
  437.  
  438.   /* internal checks */
  439.   if (nr_om_segment_tlb_entries
  440.       != (1 << (om_segment_tlb_index_stop_bit
  441.         - om_segment_tlb_index_start_bit + 1)))
  442.     error("new_vm() - internal error with om_segment constants\n");
  443.   if (nr_om_page_tlb_entries
  444.       != (1 << (om_page_tlb_index_stop_bit
  445.         - om_page_tlb_index_start_bit + 1)))
  446.     error("new_vm() - internal error with om_page constants\n");
  447.  
  448.   /* create the new vm register file */
  449.   virtual = ZALLOC(vm);
  450.  
  451.   /* set up core */
  452.   virtual->physical = physical;
  453.  
  454.   /* set up the address decoders */
  455.   virtual->instruction_map.translation.bat_registers = &virtual->ibats;
  456.   virtual->instruction_map.translation.segment_tlb = &virtual->segment_tlb;
  457.   virtual->instruction_map.translation.page_tlb = &virtual->instruction_tlb;
  458.   virtual->instruction_map.translation.is_relocate = 0;
  459.   virtual->instruction_map.translation.is_problem_state = 0;
  460.   virtual->instruction_map.translation.physical = core_readable(physical);
  461.   virtual->instruction_map.code = core_readable(physical);
  462.  
  463.   virtual->data_map.translation.bat_registers = &virtual->dbats;
  464.   virtual->data_map.translation.segment_tlb = &virtual->segment_tlb;
  465.   virtual->data_map.translation.page_tlb = &virtual->data_tlb;
  466.   virtual->data_map.translation.is_relocate = 0;
  467.   virtual->data_map.translation.is_problem_state = 0;
  468.   virtual->data_map.translation.physical = core_readable(physical);
  469.   virtual->data_map.read = core_readable(physical);
  470.   virtual->data_map.write = core_writeable(physical);
  471.  
  472.   return virtual;
  473. }
  474.  
  475.  
  476. STATIC_INLINE_VM\
  477. (om_bat *)
  478. om_effective_to_bat(om_map *map,
  479.             unsigned_word ea)
  480. {
  481.   int curr_bat = 0;
  482.   om_bats *bats = map->bat_registers;
  483.   int nr_bats = bats->nr_valid_bat_registers;
  484.  
  485.   for (curr_bat = 0; curr_bat < nr_bats; curr_bat++) {
  486.     om_bat *bat = bats->bat + curr_bat;
  487.     if ((ea & bat->block_effective_page_index_mask)
  488.     != bat->block_effective_page_index)
  489.       continue;
  490.     return bat;
  491.   }
  492.  
  493.   return NULL;
  494. }
  495.  
  496.  
  497. STATIC_INLINE_VM\
  498. (om_segment_tlb_entry *)
  499. om_effective_to_virtual(om_map *map, 
  500.             unsigned_word ea,
  501.             cpu *processor,
  502.             unsigned_word cia)
  503. {
  504.   /* first try the segment tlb */
  505.   om_segment_tlb_entry *segment_tlb_entry = (map->segment_tlb->entry
  506.                          + om_segment_tlb_index(ea));
  507.  
  508. #if (WITH_TARGET_WORD_BITSIZE == 32)
  509.   return segment_tlb_entry;
  510. #endif
  511.  
  512. #if (WITH_TARGET_WORD_BITSIZE == 64)
  513.   if (segment_tlb_entry->is_valid
  514.       && (segment_tlb_entry->masked_effective_segment_id == MASKED(ea, 0, 35))) {
  515.     error("fixme - is there a need to update any bits\n");
  516.     return segment_tlb_entry;
  517.   }
  518.  
  519.   /* drats, segment tlb missed */
  520.   {
  521.     unsigned_word segment_id_hash = ea;
  522.     int current_hash = 0;
  523.     for (current_hash = 0; current_hash < 2; current_hash += 1) {
  524.       unsigned_word segment_table_entry_group =
  525.     (map->real_address_of_segment_table
  526.      | (MASKED64(segment_id_hash, 31, 35) >> (56-35)));
  527.       unsigned_word segment_table_entry;
  528.       for (segment_table_entry = segment_table_entry_group;
  529.        segment_table_entry < (segment_table_entry_group
  530.                   + sizeof_segment_table_entry_group);
  531.        segment_table_entry += sizeof_segment_table_entry) {
  532.     /* byte order? */
  533.     unsigned_word segment_table_entry_dword_0 =
  534.       om_read_word(map->physical, segment_table_entry, processor, cia);
  535.     unsigned_word segment_table_entry_dword_1 =
  536.       om_read_word(map->physical, segment_table_entry + 8,
  537.                processor, cia);
  538.     int is_valid = MASKED64(segment_table_entry_dword_0, 56, 56) != 0;
  539.     unsigned_word masked_effective_segment_id =
  540.       MASKED64(segment_table_entry_dword_0, 0, 35);
  541.     if (is_valid && masked_effective_segment_id == MASKED64(ea, 0, 35)) {
  542.       /* don't permit some things */
  543.       if (MASKED64(segment_table_entry_dword_0, 57, 57))
  544.         error("om_effective_to_virtual() - T=1 in STE not supported\n");
  545.       /* update segment tlb */
  546.       segment_tlb_entry->is_valid = is_valid;
  547.       segment_tlb_entry->masked_effective_segment_id =
  548.         masked_effective_segment_id;
  549.       segment_tlb_entry->key[om_supervisor_state] =
  550.         EXTRACTED64(segment_table_entry_dword_0, 58, 58);
  551.       segment_tlb_entry->key[om_problem_state] =
  552.         EXTRACTED64(segment_table_entry_dword_0, 59, 59);
  553.       segment_tlb_entry->invalid_access =
  554.         (MASKED64(segment_table_entry_dword_0, 60, 60)
  555.          ? om_instruction_read
  556.          : om_access_any);
  557.       segment_tlb_entry->masked_virtual_segment_id =
  558.         INSERTED64(EXTRACTED64(segment_table_entry_dword_1, 0, 51),
  559.                18-13, 63-7); /* align ready for pte addr */
  560.       return segment_tlb_entry;
  561.     }
  562.       }
  563.       segment_id_hash = ~segment_id_hash;
  564.     }
  565.   }
  566.   return NULL;
  567. #endif
  568. }
  569.  
  570.  
  571.  
  572. STATIC_INLINE_VM\
  573. (om_page_tlb_entry *)
  574. om_virtual_to_real(om_map *map, 
  575.            unsigned_word ea,
  576.            om_segment_tlb_entry *segment_tlb_entry,
  577.            om_access_types access,
  578.            cpu *processor,
  579.            unsigned_word cia)
  580. {
  581.   om_page_tlb_entry *page_tlb_entry = (map->page_tlb->entry
  582.                        + om_page_tlb_index(ea));
  583.  
  584.   /* is it a tlb hit? */
  585.   if ((page_tlb_entry->masked_virtual_segment_id
  586.        == segment_tlb_entry->masked_virtual_segment_id)
  587.       && (page_tlb_entry->masked_page
  588.       == om_ea_masked_page(ea))) {
  589.     TRACE(trace_vm, ("ea=0x%lx - tlb hit - tlb=0x%lx\n",
  590.            (long)ea, (long)page_tlb_entry));
  591.     return page_tlb_entry;
  592.   }
  593.       
  594.   /* drats, it is a tlb miss */
  595.   {
  596.     unsigned_word page_hash =
  597.       om_hash_page(segment_tlb_entry->masked_virtual_segment_id, ea);
  598.     int current_hash;
  599.     for (current_hash = 0; current_hash < 2; current_hash += 1) {
  600.       unsigned_word real_address_of_pte_group =
  601.     (map->real_address_of_page_table
  602.      | (page_hash & map->page_table_hash_mask));
  603.       unsigned_word real_address_of_pte_0;
  604.       TRACE(trace_vm,
  605.         ("ea=0x%lx - htab search - pteg=0x%lx htab=0x%lx mask=0x%lx hash=0x%lx\n",
  606.          (long)ea, (long)real_address_of_pte_group,
  607.          map->real_address_of_page_table,
  608.          map->page_table_hash_mask,
  609.          page_hash));
  610.       for (real_address_of_pte_0 = real_address_of_pte_group;
  611.        real_address_of_pte_0 < (real_address_of_pte_group
  612.                     + sizeof_pte_group);
  613.        real_address_of_pte_0 += sizeof_pte) {
  614.     unsigned_word pte_0 = om_read_word(map,
  615.                        real_address_of_pte_0,
  616.                        processor, cia);
  617.     /* did we hit? */
  618.     if (om_pte_0_valid(pte_0)
  619.         && (current_hash == om_pte_0_hash(pte_0))
  620.         && (segment_tlb_entry->masked_virtual_segment_id
  621.         == om_pte_0_masked_vsid(pte_0))
  622.         && (om_ea_api(ea) == om_pte_0_api(pte_0))) {
  623.       unsigned_word real_address_of_pte_1 = (real_address_of_pte_0
  624.                          + sizeof_pte / 2);
  625.       unsigned_word pte_1 = om_read_word(map,
  626.                          real_address_of_pte_1,
  627.                          processor, cia);
  628.       page_tlb_entry->protection = om_pte_1_pp(pte_1);
  629.       page_tlb_entry->changed = om_pte_1_changed(pte_1);
  630.       page_tlb_entry->masked_virtual_segment_id = segment_tlb_entry->masked_virtual_segment_id;
  631.       page_tlb_entry->masked_page = om_ea_masked_page(ea);
  632.       page_tlb_entry->masked_real_page_number = om_pte_1_masked_rpn(pte_1);
  633.       page_tlb_entry->real_address_of_pte_1 = real_address_of_pte_1;
  634.       if (!om_pte_1_referenced(pte_1)) {
  635.         om_write_word(map,
  636.               real_address_of_pte_1,
  637.               pte_1 | BIT(55),
  638.               processor, cia);
  639.         TRACE(trace_vm,
  640.           ("ea=0x%lx - htab hit - set ref - tlb=0x%lx &pte1=0x%lx\n",
  641.            (long)ea, (long)page_tlb_entry, (long)real_address_of_pte_1));
  642.       }
  643.       else {
  644.         TRACE(trace_vm,
  645.           ("ea=0x%lx - htab hit - tlb=0x%lx &pte1=0x%lx\n",
  646.            (long)ea, (long)page_tlb_entry, (long)real_address_of_pte_1));
  647.       }
  648.       return page_tlb_entry;
  649.     }
  650.       }
  651.       page_hash = ~page_hash; /*???*/
  652.     }
  653.   }
  654.   return NULL;
  655. }
  656.  
  657.  
  658. STATIC_INLINE_VM\
  659. (void)
  660. om_interrupt(cpu *processor,
  661.          unsigned_word cia,
  662.          unsigned_word ea,
  663.          om_access_types access,
  664.          storage_interrupt_reasons reason)
  665. {
  666.   switch (access) {
  667.   case om_data_read:
  668.     data_storage_interrupt(processor, cia, ea, reason, 0/*!is_store*/);
  669.     break;
  670.   case om_data_write:
  671.     data_storage_interrupt(processor, cia, ea, reason, 1/*is_store*/);
  672.     break;
  673.   case om_instruction_read:
  674.     instruction_storage_interrupt(processor, cia, reason);
  675.     break;
  676.   default:
  677.     error("om_interrupt - unexpected access type %d, cia=0x%x, ea=0x%x\n",
  678.       access, cia, ea);
  679.   }
  680. }
  681.  
  682.  
  683. STATIC_INLINE_VM\
  684. (unsigned_word)
  685. om_translate_effective_to_real(om_map *map,
  686.                    unsigned_word ea,
  687.                    om_access_types access,
  688.                    cpu *processor,
  689.                    unsigned_word cia,
  690.                    int abort)
  691. {
  692.   om_bat *bat = NULL;
  693.   om_segment_tlb_entry *segment_tlb_entry = NULL;
  694.   om_page_tlb_entry *page_tlb_entry = NULL;
  695.   unsigned_word ra;
  696.  
  697.   if (!map->is_relocate) {
  698.     ra = ea;
  699.     TRACE(trace_vm, ("ea=0x%lx - direct map - ra=0x%lx\n",
  700.              (long)ea, (long)ra));
  701.     return ra;
  702.   }
  703.  
  704.   /* match with BAT? */
  705.   bat = om_effective_to_bat(map, ea);
  706.   if (bat != NULL) {
  707.     if (!om_valid_access[1][bat->protection_bits][access]) {
  708.       TRACE(trace_vm, ("ea=0x%lx - bat access violation\n", (long)ea));
  709.       if (abort)
  710.     om_interrupt(processor, cia, ea, access,
  711.              protection_violation_storage_interrupt);
  712.       else
  713.     return MASK(0, 63);
  714.     }
  715.  
  716.     ra = ((ea & bat->block_length_mask) | bat->block_real_page_number);
  717.     TRACE(trace_vm, ("ea=0x%lx - bat translation - ra=0x%lx\n",
  718.              (long)ea, (long)ra));
  719.     return ra;
  720.   }
  721.  
  722.   /* translate ea to va using segment map */
  723.   segment_tlb_entry = om_effective_to_virtual(map, ea, processor, cia);
  724. #if (WITH_TARGET_WORD_BITSIZE == 64)
  725.   if (segment_tlb_entry == NULL) {
  726.     TRACE(trace_vm, ("ea=0x%lx - segment tlb miss\n", (long)ea));
  727.     if (abort)
  728.       om_interrupt(processor, cia, ea, access,
  729.            segment_table_miss_storage_interrupt);
  730.     else
  731.       return MASK(0, 63);
  732.   }
  733. #endif
  734.   /* check for invalid segment access type */
  735.   if (segment_tlb_entry->invalid_access == access) {
  736.     TRACE(trace_vm, ("ea=0x%lx - segment access invalid\n", (long)ea));
  737.     if (abort)
  738.       om_interrupt(processor, cia, ea, access,
  739.            protection_violation_storage_interrupt);
  740.     else
  741.       return MASK(0, 63);
  742.   }
  743.  
  744.   /* lookup in PTE */
  745.   page_tlb_entry = om_virtual_to_real(map, ea, segment_tlb_entry,
  746.                       access,
  747.                       processor, cia);
  748.   if (page_tlb_entry == NULL) {
  749.     TRACE(trace_vm, ("ea=0x%lx - page tlb miss\n", (long)ea));
  750.     if (abort)
  751.       om_interrupt(processor, cia, ea, access,
  752.            hash_table_miss_storage_interrupt);
  753.     else
  754.       return MASK(0, 63);
  755.   }
  756.   if (!(om_valid_access
  757.     [segment_tlb_entry->key[map->is_problem_state]]
  758.     [page_tlb_entry->protection]
  759.     [access])) {
  760.     TRACE(trace_vm, ("ea=0x%lx - page tlb access violation\n", (long)ea));
  761.     if (abort)
  762.       om_interrupt(processor, cia, ea, access,
  763.            protection_violation_storage_interrupt);
  764.     else
  765.       return MASK(0, 63);
  766.   }
  767.  
  768.   /* update change bit as needed */
  769.   if (access == om_data_write &&!page_tlb_entry->changed) {
  770.     unsigned_word pte_1 = om_read_word(map,
  771.                        page_tlb_entry->real_address_of_pte_1,
  772.                        processor, cia);
  773.     om_write_word(map,
  774.           page_tlb_entry->real_address_of_pte_1,
  775.           pte_1 | BIT(56),
  776.           processor, cia);
  777.     TRACE(trace_vm, ("ea=0x%lx - set change bit - tlb=0x%lx &pte1=0x%lx\n",
  778.              (long)ea, (long)page_tlb_entry,
  779.              (long)page_tlb_entry->real_address_of_pte_1));
  780.   }
  781.  
  782.   ra = (page_tlb_entry->masked_real_page_number | om_ea_masked_byte(ea));
  783.   TRACE(trace_vm, ("ea=0x%lx - page translation - ra=0x%lx\n",
  784.            (long)ea, (long)ra));
  785.   return ra;
  786. }
  787.  
  788.  
  789. /*
  790.  * Definition of operations for memory management
  791.  */
  792.  
  793.  
  794. /* rebuild all the relevant bat information */
  795. STATIC_INLINE_VM\
  796. (void)
  797. om_unpack_bat(om_bat *bat,
  798.           spreg ubat,
  799.           spreg lbat)
  800. {
  801.   /* for extracting out the offset within a page */
  802.   bat->block_length_mask = ((MASKED(ubat, 51, 61) << (17-2))
  803.                 | MASK(63-17+1, 63));
  804.  
  805.   /* for checking the effective page index */
  806.   bat->block_effective_page_index = MASKED(ubat, 0, 46);
  807.   bat->block_effective_page_index_mask = ~bat->block_length_mask;
  808.  
  809.   /* protection information */
  810.   bat->protection_bits = EXTRACTED(lbat, 62, 63);
  811.   bat->block_real_page_number = MASKED(lbat, 0, 46);
  812. }
  813.  
  814.  
  815. /* rebuild the given bat table */
  816. STATIC_INLINE_VM\
  817. (void)
  818. om_unpack_bats(om_bats *bats,
  819.            spreg *raw_bats,
  820.            msreg msr)
  821. {
  822.   int i;
  823.   bats->nr_valid_bat_registers = 0;
  824.   for (i = 0; i < nr_om_bat_registers*2; i += 2) {
  825.     spreg ubat = raw_bats[i];
  826.     spreg lbat = raw_bats[i+1];
  827.     if ((msr & msr_problem_state)
  828.     ? EXTRACTED(ubat, 63, 63)
  829.     : EXTRACTED(ubat, 62, 62)) {
  830.       om_unpack_bat(&bats->bat[bats->nr_valid_bat_registers],
  831.             ubat, lbat);
  832.       bats->nr_valid_bat_registers += 1;
  833.     }
  834.   }
  835. }
  836.  
  837.  
  838. #if (WITH_TARGET_WORD_BITSIZE == 32)
  839. STATIC_INLINE_VM\
  840. (void)
  841. om_unpack_sr(vm *virtual,
  842.          sreg *srs,
  843.          int which_sr)
  844. {
  845.   om_segment_tlb_entry *segment_tlb_entry = 0;
  846.   sreg new_sr_value = 0;
  847.  
  848.   /* check register in range */
  849.   if (which_sr < 0 || which_sr > nr_om_segment_tlb_entries)
  850.     error("om_set_sr: segment register out of bounds\n");
  851.  
  852.   /* get the working values */
  853.   segment_tlb_entry = &virtual->segment_tlb.entry[which_sr];  
  854.   new_sr_value = srs[which_sr];
  855.   
  856.   /* do we support this */
  857.   if (MASKED32(new_sr_value, 0, 0))
  858.     error("om_ser_sr(): unsupported value of T in segment register %d\n",
  859.       which_sr);
  860.  
  861.   /* update info */
  862.   segment_tlb_entry->key[om_supervisor_state] = EXTRACTED32(new_sr_value, 1, 1);
  863.   segment_tlb_entry->key[om_problem_state] = EXTRACTED32(new_sr_value, 2, 2);
  864.   segment_tlb_entry->invalid_access = (MASKED32(new_sr_value, 3, 3)
  865.                        ? om_instruction_read
  866.                        : om_access_any);
  867.   segment_tlb_entry->masked_virtual_segment_id =
  868.     INSERTED32(EXTRACTED32(new_sr_value, 8, 31),
  869.            7-5, 31-6); /* align ready for pte address */
  870. }
  871. #endif
  872.  
  873.  
  874. #if (WITH_TARGET_WORD_BITSIZE == 32)
  875. STATIC_INLINE_VM\
  876. (void)
  877. om_unpack_srs(vm *virtual,
  878.           sreg *srs)
  879. {
  880.   int which_sr;
  881.   for (which_sr = 0; which_sr < nr_om_segment_tlb_entries; which_sr++) {
  882.     om_unpack_sr(virtual, srs, which_sr);
  883.   }
  884. }
  885. #endif
  886.  
  887.  
  888. /* Rebuild all the data structures for the new context as specifed by
  889.    the passed registers */
  890. INLINE_VM\
  891. (void)
  892. vm_synchronize_context(vm *virtual,
  893.                spreg *sprs,
  894.                sreg *srs,
  895.                msreg msr)
  896. {
  897.  
  898.   /* enable/disable translation */
  899.   int problem_state = (msr & msr_problem_state) != 0;
  900.   int data_relocate = (msr & msr_data_relocate) != 0;
  901.   int instruction_relocate = (msr & msr_instruction_relocate) != 0;
  902.   int little_endian = (msr & msr_little_endian_mode) != 0;
  903.  
  904.   unsigned_word page_table_hash_mask;
  905.   unsigned_word real_address_of_page_table;
  906.  
  907.   /* update current processor mode */
  908.   virtual->instruction_map.translation.is_relocate = instruction_relocate;
  909.   virtual->instruction_map.translation.is_problem_state = problem_state;
  910.   virtual->data_map.translation.is_relocate = data_relocate;
  911.   virtual->data_map.translation.is_problem_state = problem_state;
  912.  
  913.   /* update bat registers for the new context */
  914.   om_unpack_bats(&virtual->ibats, &sprs[spr_ibat0u], msr);
  915.   om_unpack_bats(&virtual->dbats, &sprs[spr_dbat0u], msr);
  916.  
  917.   /* unpack SDR1 - the storage description register 1 */
  918. #if (WITH_TARGET_WORD_BITSIZE == 64)
  919.   real_address_of_page_table = MASKED64(sprs[spr_sdr1], 0, 45);
  920.   page_table_hash_mask = MASK64(18+28-EXTRACTED64(sprs[spr_sdr1], 59, 63),
  921.                 63-7);
  922. #endif
  923. #if (WITH_TARGET_WORD_BITSIZE == 32)
  924.   real_address_of_page_table = MASKED32(sprs[spr_sdr1], 0, 15);
  925.   page_table_hash_mask = (INSERTED32(EXTRACTED32(sprs[spr_sdr1], 23, 31),
  926.                      7, 7+9-1)
  927.               | MASK32(7+9, 31-6));
  928. #endif
  929.   virtual->instruction_map.translation.real_address_of_page_table = real_address_of_page_table;
  930.   virtual->instruction_map.translation.page_table_hash_mask = page_table_hash_mask;
  931.   virtual->data_map.translation.real_address_of_page_table = real_address_of_page_table;
  932.   virtual->data_map.translation.page_table_hash_mask = page_table_hash_mask;
  933.  
  934.  
  935.   /* unpack the segment tlb registers */
  936. #if (WITH_TARGET_WORD_BITSIZE == 32)
  937.   om_unpack_srs(virtual, srs);
  938. #endif
  939.  
  940.   /* set up the XOR registers if the current endian mode conflicts
  941.      with what is in the MSR */
  942.   if (WITH_XOR_ENDIAN) {
  943.     int i = 1;
  944.     unsigned mask;
  945.     if ((little_endian && CURRENT_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
  946.     || (!little_endian && CURRENT_TARGET_BYTE_ORDER == BIG_ENDIAN))
  947.       mask = 0;
  948.     else
  949.       mask = WITH_XOR_ENDIAN - 1;
  950.     while (i - 1 < WITH_XOR_ENDIAN) {
  951.       virtual->instruction_map.translation.xor[i-1] = mask;
  952.       virtual->data_map.translation.xor[i-1] =  mask;
  953.       mask = (mask << 1) & (WITH_XOR_ENDIAN - 1);
  954.       i = i * 2;
  955.     }
  956.   }
  957.   else {
  958.     /* don't allow the processor to change endian modes */
  959.     if ((little_endian && CURRENT_TARGET_BYTE_ORDER != LITTLE_ENDIAN)
  960.     || (!little_endian && CURRENT_TARGET_BYTE_ORDER != BIG_ENDIAN))
  961.       error("vm_synchronize_context() - unsuported change of byte order\n");
  962.   }
  963. }
  964.  
  965. /* update vm data structures due to a TLB operation */
  966.  
  967. INLINE_VM\
  968. (void)
  969. vm_page_tlb_invalidate_entry(vm *memory,
  970.                  unsigned_word ea)
  971. {
  972.   int i = om_page_tlb_index(ea);
  973.   memory->instruction_tlb.entry[i].masked_virtual_segment_id = MASK(0, 63);
  974.   memory->data_tlb.entry[i].masked_virtual_segment_id = MASK(0, 63);
  975.   TRACE(trace_vm, ("ea=0x%lx - tlb invalidate entry\n", (long)ea));
  976. }
  977.  
  978. INLINE_VM\
  979. (void)
  980. vm_page_tlb_invalidate_all(vm *memory)
  981. {
  982.   int i;
  983.   for (i = 0; i < nr_om_page_tlb_entries; i++) {
  984.     memory->instruction_tlb.entry[i].masked_virtual_segment_id = MASK(0, 63);
  985.     memory->data_tlb.entry[i].masked_virtual_segment_id = MASK(0, 63);
  986.   }
  987.   TRACE(trace_vm, ("tlb invalidate all\n"));
  988. }
  989.  
  990.  
  991.  
  992. INLINE_VM\
  993. (vm_data_map *)
  994. vm_create_data_map(vm *memory)
  995. {
  996.   return &memory->data_map;
  997. }
  998.  
  999.  
  1000. INLINE_VM\
  1001. (vm_instruction_map *)
  1002. vm_create_instruction_map(vm *memory)
  1003. {
  1004.   return &memory->instruction_map;
  1005. }
  1006.  
  1007.  
  1008. STATIC_INLINE_VM\
  1009. (unsigned_word)
  1010. vm_translate(om_map *map,
  1011.          unsigned_word ea,
  1012.          om_access_types access,
  1013.          cpu *processor,
  1014.          unsigned_word cia,
  1015.          int abort)
  1016. {
  1017.   switch (CURRENT_ENVIRONMENT) {
  1018.   case USER_ENVIRONMENT:
  1019.   case VIRTUAL_ENVIRONMENT:
  1020.     return ea;
  1021.   case OPERATING_ENVIRONMENT:
  1022.     return om_translate_effective_to_real(map, ea, access,
  1023.                       processor, cia,
  1024.                       abort);
  1025.   default:
  1026.     error("vm_translate() - unknown environment\n");
  1027.     return 0;
  1028.   }
  1029. }
  1030.  
  1031.  
  1032. INLINE_VM\
  1033. (unsigned_word)
  1034. vm_real_data_addr(vm_data_map *map,
  1035.           unsigned_word ea,
  1036.           int is_read,
  1037.           cpu *processor,
  1038.           unsigned_word cia)
  1039. {
  1040.   return vm_translate(&map->translation,
  1041.               ea,
  1042.               is_read ? om_data_read : om_data_write,
  1043.               processor,
  1044.               cia,
  1045.               1); /*abort*/
  1046. }
  1047.  
  1048.  
  1049. INLINE_VM\
  1050. (unsigned_word)
  1051. vm_real_instruction_addr(vm_instruction_map *map,
  1052.              cpu *processor,
  1053.              unsigned_word cia)
  1054. {
  1055.   return vm_translate(&map->translation,
  1056.               cia,
  1057.               om_instruction_read,
  1058.               processor,
  1059.               cia,
  1060.               1); /*abort*/
  1061. }
  1062.  
  1063. INLINE_VM\
  1064. (instruction_word)
  1065. vm_instruction_map_read(vm_instruction_map *map,
  1066.             cpu *processor,
  1067.             unsigned_word cia)
  1068. {
  1069.   unsigned_word ra = vm_real_instruction_addr(map, processor, cia);
  1070.   ASSERT((cia & 0x3) == 0); /* always aligned */
  1071.   if (WITH_XOR_ENDIAN)
  1072.     ra ^= map->translation.xor[sizeof(instruction_word) - 1];
  1073.   return core_map_read_4(map->code, ra, processor, cia);
  1074. }
  1075.  
  1076.  
  1077. INLINE_VM\
  1078. (int)
  1079. vm_data_map_read_buffer(vm_data_map *map,
  1080.             void *target,
  1081.             unsigned_word addr,
  1082.             unsigned nr_bytes)
  1083. {
  1084.   unsigned count;
  1085.   for (count = 0; count < nr_bytes; count++) {
  1086.     unsigned_1 byte;
  1087.     unsigned_word ea = addr + count;
  1088.     unsigned_word ra = vm_translate(&map->translation,
  1089.                     ea, om_data_read,
  1090.                     NULL, /*processor*/
  1091.                     0, /*cia*/
  1092.                     0); /*dont-abort*/
  1093.     if (ra == MASK(0, 63))
  1094.       break;
  1095.     if (WITH_XOR_ENDIAN)
  1096.       ra ^= map->translation.xor[0];
  1097.     if (core_map_read_buffer(map->read, &byte, ra, sizeof(byte))
  1098.     != sizeof(byte))
  1099.       break;
  1100.     ((unsigned_1*)target)[count] = T2H_1(byte);
  1101.   }
  1102.   return count;
  1103. }
  1104.  
  1105.  
  1106. INLINE_VM\
  1107. (int)
  1108. vm_data_map_write_buffer(vm_data_map *map,
  1109.              const void *source,
  1110.              unsigned_word addr,
  1111.              unsigned nr_bytes,
  1112.              int violate_read_only_section)
  1113. {
  1114.   unsigned count;
  1115.   unsigned_1 byte;
  1116.   for (count = 0; count < nr_bytes; count++) {
  1117.     unsigned_word ea = addr + count;
  1118.     unsigned_word ra = vm_translate(&map->translation,
  1119.                     ea, om_data_write,
  1120.                     NULL/*processor*/,
  1121.                     0, /*cia*/
  1122.                     0); /*dont-abort*/
  1123.     if (ra == MASK(0, 63))
  1124.       break;
  1125.     if (WITH_XOR_ENDIAN)
  1126.       ra ^= map->translation.xor[0];
  1127.     byte = T2H_1(((unsigned_1*)source)[count]);
  1128.     if (core_map_write_buffer((violate_read_only_section
  1129.                    ? map->read
  1130.                    : map->write),
  1131.                   &byte, ra, sizeof(byte)) != sizeof(byte))
  1132.       break;
  1133.   }
  1134.   return count;
  1135. }
  1136.  
  1137.  
  1138. /* define the read/write 1/2/4/8/word functions */
  1139.  
  1140. #define N 1
  1141. #include "vm_n.h"
  1142. #undef N
  1143.  
  1144. #define N 2
  1145. #include "vm_n.h"
  1146. #undef N
  1147.  
  1148. #define N 4
  1149. #include "vm_n.h"
  1150. #undef N
  1151.  
  1152. #define N 8
  1153. #include "vm_n.h"
  1154. #undef N
  1155.  
  1156. #define N word
  1157. #include "vm_n.h"
  1158. #undef N
  1159.  
  1160.  
  1161.  
  1162. #endif /* _VM_C_ */
  1163.