vm.c 31 KB

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