ldst_helper.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  1. /*
  2. * Helpers for loads and stores
  3. *
  4. * Copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu/log.h"
  21. #include "qemu/range.h"
  22. #include "cpu.h"
  23. #include "tcg/tcg.h"
  24. #include "exec/helper-proto.h"
  25. #include "exec/exec-all.h"
  26. #include "exec/cputlb.h"
  27. #include "exec/page-protection.h"
  28. #include "exec/cpu_ldst.h"
  29. #ifdef CONFIG_USER_ONLY
  30. #include "user/page-protection.h"
  31. #endif
  32. #include "asi.h"
  33. //#define DEBUG_MMU
  34. //#define DEBUG_MXCC
  35. //#define DEBUG_UNASSIGNED
  36. //#define DEBUG_ASI
  37. //#define DEBUG_CACHE_CONTROL
  38. #ifdef DEBUG_MMU
  39. #define DPRINTF_MMU(fmt, ...) \
  40. do { printf("MMU: " fmt , ## __VA_ARGS__); } while (0)
  41. #else
  42. #define DPRINTF_MMU(fmt, ...) do {} while (0)
  43. #endif
  44. #ifdef DEBUG_MXCC
  45. #define DPRINTF_MXCC(fmt, ...) \
  46. do { printf("MXCC: " fmt , ## __VA_ARGS__); } while (0)
  47. #else
  48. #define DPRINTF_MXCC(fmt, ...) do {} while (0)
  49. #endif
  50. #ifdef DEBUG_ASI
  51. #define DPRINTF_ASI(fmt, ...) \
  52. do { printf("ASI: " fmt , ## __VA_ARGS__); } while (0)
  53. #endif
  54. #ifdef DEBUG_CACHE_CONTROL
  55. #define DPRINTF_CACHE_CONTROL(fmt, ...) \
  56. do { printf("CACHE_CONTROL: " fmt , ## __VA_ARGS__); } while (0)
  57. #else
  58. #define DPRINTF_CACHE_CONTROL(fmt, ...) do {} while (0)
  59. #endif
  60. #ifdef TARGET_SPARC64
  61. #ifndef TARGET_ABI32
  62. #define AM_CHECK(env1) ((env1)->pstate & PS_AM)
  63. #else
  64. #define AM_CHECK(env1) (1)
  65. #endif
  66. #endif
  67. #if defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
  68. /* Calculates TSB pointer value for fault page size
  69. * UltraSPARC IIi has fixed sizes (8k or 64k) for the page pointers
  70. * UA2005 holds the page size configuration in mmu_ctx registers */
  71. static uint64_t ultrasparc_tsb_pointer(CPUSPARCState *env,
  72. const SparcV9MMU *mmu, const int idx)
  73. {
  74. uint64_t tsb_register;
  75. int page_size;
  76. if (cpu_has_hypervisor(env)) {
  77. int tsb_index = 0;
  78. int ctx = mmu->tag_access & 0x1fffULL;
  79. uint64_t ctx_register = mmu->sun4v_ctx_config[ctx ? 1 : 0];
  80. tsb_index = idx;
  81. tsb_index |= ctx ? 2 : 0;
  82. page_size = idx ? ctx_register >> 8 : ctx_register;
  83. page_size &= 7;
  84. tsb_register = mmu->sun4v_tsb_pointers[tsb_index];
  85. } else {
  86. page_size = idx;
  87. tsb_register = mmu->tsb;
  88. }
  89. int tsb_split = (tsb_register & 0x1000ULL) ? 1 : 0;
  90. int tsb_size = tsb_register & 0xf;
  91. uint64_t tsb_base_mask = (~0x1fffULL) << tsb_size;
  92. /* move va bits to correct position,
  93. * the context bits will be masked out later */
  94. uint64_t va = mmu->tag_access >> (3 * page_size + 9);
  95. /* calculate tsb_base mask and adjust va if split is in use */
  96. if (tsb_split) {
  97. if (idx == 0) {
  98. va &= ~(1ULL << (13 + tsb_size));
  99. } else {
  100. va |= (1ULL << (13 + tsb_size));
  101. }
  102. tsb_base_mask <<= 1;
  103. }
  104. return ((tsb_register & tsb_base_mask) | (va & ~tsb_base_mask)) & ~0xfULL;
  105. }
  106. /* Calculates tag target register value by reordering bits
  107. in tag access register */
  108. static uint64_t ultrasparc_tag_target(uint64_t tag_access_register)
  109. {
  110. return ((tag_access_register & 0x1fff) << 48) | (tag_access_register >> 22);
  111. }
  112. static void replace_tlb_entry(SparcTLBEntry *tlb,
  113. uint64_t tlb_tag, uint64_t tlb_tte,
  114. CPUSPARCState *env)
  115. {
  116. target_ulong mask, size, va, offset;
  117. /* flush page range if translation is valid */
  118. if (TTE_IS_VALID(tlb->tte)) {
  119. CPUState *cs = env_cpu(env);
  120. size = 8192ULL << 3 * TTE_PGSIZE(tlb->tte);
  121. mask = 1ULL + ~size;
  122. va = tlb->tag & mask;
  123. for (offset = 0; offset < size; offset += TARGET_PAGE_SIZE) {
  124. tlb_flush_page(cs, va + offset);
  125. }
  126. }
  127. tlb->tag = tlb_tag;
  128. tlb->tte = tlb_tte;
  129. }
  130. static void demap_tlb(SparcTLBEntry *tlb, target_ulong demap_addr,
  131. const char *strmmu, CPUSPARCState *env1)
  132. {
  133. unsigned int i;
  134. target_ulong mask;
  135. uint64_t context;
  136. int is_demap_context = (demap_addr >> 6) & 1;
  137. /* demap context */
  138. switch ((demap_addr >> 4) & 3) {
  139. case 0: /* primary */
  140. context = env1->dmmu.mmu_primary_context;
  141. break;
  142. case 1: /* secondary */
  143. context = env1->dmmu.mmu_secondary_context;
  144. break;
  145. case 2: /* nucleus */
  146. context = 0;
  147. break;
  148. case 3: /* reserved */
  149. default:
  150. return;
  151. }
  152. for (i = 0; i < 64; i++) {
  153. if (TTE_IS_VALID(tlb[i].tte)) {
  154. if (is_demap_context) {
  155. /* will remove non-global entries matching context value */
  156. if (TTE_IS_GLOBAL(tlb[i].tte) ||
  157. !tlb_compare_context(&tlb[i], context)) {
  158. continue;
  159. }
  160. } else {
  161. /* demap page
  162. will remove any entry matching VA */
  163. mask = 0xffffffffffffe000ULL;
  164. mask <<= 3 * ((tlb[i].tte >> 61) & 3);
  165. if (!compare_masked(demap_addr, tlb[i].tag, mask)) {
  166. continue;
  167. }
  168. /* entry should be global or matching context value */
  169. if (!TTE_IS_GLOBAL(tlb[i].tte) &&
  170. !tlb_compare_context(&tlb[i], context)) {
  171. continue;
  172. }
  173. }
  174. replace_tlb_entry(&tlb[i], 0, 0, env1);
  175. #ifdef DEBUG_MMU
  176. DPRINTF_MMU("%s demap invalidated entry [%02u]\n", strmmu, i);
  177. dump_mmu(env1);
  178. #endif
  179. }
  180. }
  181. }
  182. static uint64_t sun4v_tte_to_sun4u(CPUSPARCState *env, uint64_t tag,
  183. uint64_t sun4v_tte)
  184. {
  185. uint64_t sun4u_tte;
  186. if (!(cpu_has_hypervisor(env) && (tag & TLB_UST1_IS_SUN4V_BIT))) {
  187. /* is already in the sun4u format */
  188. return sun4v_tte;
  189. }
  190. sun4u_tte = TTE_PA(sun4v_tte) | (sun4v_tte & TTE_VALID_BIT);
  191. sun4u_tte |= (sun4v_tte & 3ULL) << 61; /* TTE_PGSIZE */
  192. sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_NFO_BIT_UA2005, TTE_NFO_BIT);
  193. sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_USED_BIT_UA2005, TTE_USED_BIT);
  194. sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_W_OK_BIT_UA2005, TTE_W_OK_BIT);
  195. sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_SIDEEFFECT_BIT_UA2005,
  196. TTE_SIDEEFFECT_BIT);
  197. sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_PRIV_BIT_UA2005, TTE_PRIV_BIT);
  198. sun4u_tte |= CONVERT_BIT(sun4v_tte, TTE_LOCKED_BIT_UA2005, TTE_LOCKED_BIT);
  199. return sun4u_tte;
  200. }
  201. static void replace_tlb_1bit_lru(SparcTLBEntry *tlb,
  202. uint64_t tlb_tag, uint64_t tlb_tte,
  203. const char *strmmu, CPUSPARCState *env1,
  204. uint64_t addr)
  205. {
  206. unsigned int i, replace_used;
  207. tlb_tte = sun4v_tte_to_sun4u(env1, addr, tlb_tte);
  208. if (cpu_has_hypervisor(env1)) {
  209. uint64_t new_vaddr = tlb_tag & ~0x1fffULL;
  210. uint64_t new_size = 8192ULL << 3 * TTE_PGSIZE(tlb_tte);
  211. uint32_t new_ctx = tlb_tag & 0x1fffU;
  212. for (i = 0; i < 64; i++) {
  213. uint32_t ctx = tlb[i].tag & 0x1fffU;
  214. /* check if new mapping overlaps an existing one */
  215. if (new_ctx == ctx) {
  216. uint64_t vaddr = tlb[i].tag & ~0x1fffULL;
  217. uint64_t size = 8192ULL << 3 * TTE_PGSIZE(tlb[i].tte);
  218. if (ranges_overlap(new_vaddr, new_size, vaddr, size)) {
  219. DPRINTF_MMU("auto demap entry [%d] %lx->%lx\n", i, vaddr,
  220. new_vaddr);
  221. replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
  222. return;
  223. }
  224. }
  225. }
  226. }
  227. /* Try replacing invalid entry */
  228. for (i = 0; i < 64; i++) {
  229. if (!TTE_IS_VALID(tlb[i].tte)) {
  230. replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
  231. #ifdef DEBUG_MMU
  232. DPRINTF_MMU("%s lru replaced invalid entry [%i]\n", strmmu, i);
  233. dump_mmu(env1);
  234. #endif
  235. return;
  236. }
  237. }
  238. /* All entries are valid, try replacing unlocked entry */
  239. for (replace_used = 0; replace_used < 2; ++replace_used) {
  240. /* Used entries are not replaced on first pass */
  241. for (i = 0; i < 64; i++) {
  242. if (!TTE_IS_LOCKED(tlb[i].tte) && !TTE_IS_USED(tlb[i].tte)) {
  243. replace_tlb_entry(&tlb[i], tlb_tag, tlb_tte, env1);
  244. #ifdef DEBUG_MMU
  245. DPRINTF_MMU("%s lru replaced unlocked %s entry [%i]\n",
  246. strmmu, (replace_used ? "used" : "unused"), i);
  247. dump_mmu(env1);
  248. #endif
  249. return;
  250. }
  251. }
  252. /* Now reset used bit and search for unused entries again */
  253. for (i = 0; i < 64; i++) {
  254. TTE_SET_UNUSED(tlb[i].tte);
  255. }
  256. }
  257. #ifdef DEBUG_MMU
  258. DPRINTF_MMU("%s lru replacement: no free entries available, "
  259. "replacing the last one\n", strmmu);
  260. #endif
  261. /* corner case: the last entry is replaced anyway */
  262. replace_tlb_entry(&tlb[63], tlb_tag, tlb_tte, env1);
  263. }
  264. #endif
  265. #ifdef TARGET_SPARC64
  266. /* returns true if access using this ASI is to have address translated by MMU
  267. otherwise access is to raw physical address */
  268. /* TODO: check sparc32 bits */
  269. static inline int is_translating_asi(int asi)
  270. {
  271. /* Ultrasparc IIi translating asi
  272. - note this list is defined by cpu implementation
  273. */
  274. switch (asi) {
  275. case 0x04 ... 0x11:
  276. case 0x16 ... 0x19:
  277. case 0x1E ... 0x1F:
  278. case 0x24 ... 0x2C:
  279. case 0x70 ... 0x73:
  280. case 0x78 ... 0x79:
  281. case 0x80 ... 0xFF:
  282. return 1;
  283. default:
  284. return 0;
  285. }
  286. }
  287. static inline target_ulong address_mask(CPUSPARCState *env1, target_ulong addr)
  288. {
  289. if (AM_CHECK(env1)) {
  290. addr &= 0xffffffffULL;
  291. }
  292. return addr;
  293. }
  294. static inline target_ulong asi_address_mask(CPUSPARCState *env,
  295. int asi, target_ulong addr)
  296. {
  297. if (is_translating_asi(asi)) {
  298. addr = address_mask(env, addr);
  299. }
  300. return addr;
  301. }
  302. #ifndef CONFIG_USER_ONLY
  303. static inline void do_check_asi(CPUSPARCState *env, int asi, uintptr_t ra)
  304. {
  305. /* ASIs >= 0x80 are user mode.
  306. * ASIs >= 0x30 are hyper mode (or super if hyper is not available).
  307. * ASIs <= 0x2f are super mode.
  308. */
  309. if (asi < 0x80
  310. && !cpu_hypervisor_mode(env)
  311. && (!cpu_supervisor_mode(env)
  312. || (asi >= 0x30 && cpu_has_hypervisor(env)))) {
  313. cpu_raise_exception_ra(env, TT_PRIV_ACT, ra);
  314. }
  315. }
  316. #endif /* !CONFIG_USER_ONLY */
  317. #endif
  318. #if defined(TARGET_SPARC64) || !defined(CONFIG_USER_ONLY)
  319. static void do_check_align(CPUSPARCState *env, target_ulong addr,
  320. uint32_t align, uintptr_t ra)
  321. {
  322. if (addr & align) {
  323. cpu_raise_exception_ra(env, TT_UNALIGNED, ra);
  324. }
  325. }
  326. #endif
  327. #if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY) && \
  328. defined(DEBUG_MXCC)
  329. static void dump_mxcc(CPUSPARCState *env)
  330. {
  331. printf("mxccdata: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
  332. "\n",
  333. env->mxccdata[0], env->mxccdata[1],
  334. env->mxccdata[2], env->mxccdata[3]);
  335. printf("mxccregs: %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
  336. "\n"
  337. " %016" PRIx64 " %016" PRIx64 " %016" PRIx64 " %016" PRIx64
  338. "\n",
  339. env->mxccregs[0], env->mxccregs[1],
  340. env->mxccregs[2], env->mxccregs[3],
  341. env->mxccregs[4], env->mxccregs[5],
  342. env->mxccregs[6], env->mxccregs[7]);
  343. }
  344. #endif
  345. #if (defined(TARGET_SPARC64) || !defined(CONFIG_USER_ONLY)) \
  346. && defined(DEBUG_ASI)
  347. static void dump_asi(const char *txt, target_ulong addr, int asi, int size,
  348. uint64_t r1)
  349. {
  350. switch (size) {
  351. case 1:
  352. DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %02" PRIx64 "\n", txt,
  353. addr, asi, r1 & 0xff);
  354. break;
  355. case 2:
  356. DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %04" PRIx64 "\n", txt,
  357. addr, asi, r1 & 0xffff);
  358. break;
  359. case 4:
  360. DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %08" PRIx64 "\n", txt,
  361. addr, asi, r1 & 0xffffffff);
  362. break;
  363. case 8:
  364. DPRINTF_ASI("%s "TARGET_FMT_lx " asi 0x%02x = %016" PRIx64 "\n", txt,
  365. addr, asi, r1);
  366. break;
  367. }
  368. }
  369. #endif
  370. #ifndef CONFIG_USER_ONLY
  371. #ifndef TARGET_SPARC64
  372. static void sparc_raise_mmu_fault(CPUState *cs, hwaddr addr,
  373. bool is_write, bool is_exec, int is_asi,
  374. unsigned size, uintptr_t retaddr)
  375. {
  376. CPUSPARCState *env = cpu_env(cs);
  377. int fault_type;
  378. #ifdef DEBUG_UNASSIGNED
  379. if (is_asi) {
  380. printf("Unassigned mem %s access of %d byte%s to " HWADDR_FMT_plx
  381. " asi 0x%02x from " TARGET_FMT_lx "\n",
  382. is_exec ? "exec" : is_write ? "write" : "read", size,
  383. size == 1 ? "" : "s", addr, is_asi, env->pc);
  384. } else {
  385. printf("Unassigned mem %s access of %d byte%s to " HWADDR_FMT_plx
  386. " from " TARGET_FMT_lx "\n",
  387. is_exec ? "exec" : is_write ? "write" : "read", size,
  388. size == 1 ? "" : "s", addr, env->pc);
  389. }
  390. #endif
  391. /* Don't overwrite translation and access faults */
  392. fault_type = (env->mmuregs[3] & 0x1c) >> 2;
  393. if ((fault_type > 4) || (fault_type == 0)) {
  394. env->mmuregs[3] = 0; /* Fault status register */
  395. if (is_asi) {
  396. env->mmuregs[3] |= 1 << 16;
  397. }
  398. if (env->psrs) {
  399. env->mmuregs[3] |= 1 << 5;
  400. }
  401. if (is_exec) {
  402. env->mmuregs[3] |= 1 << 6;
  403. }
  404. if (is_write) {
  405. env->mmuregs[3] |= 1 << 7;
  406. }
  407. env->mmuregs[3] |= (5 << 2) | 2;
  408. /* SuperSPARC will never place instruction fault addresses in the FAR */
  409. if (!is_exec) {
  410. env->mmuregs[4] = addr; /* Fault address register */
  411. }
  412. }
  413. /* overflow (same type fault was not read before another fault) */
  414. if (fault_type == ((env->mmuregs[3] & 0x1c)) >> 2) {
  415. env->mmuregs[3] |= 1;
  416. }
  417. if ((env->mmuregs[0] & MMU_E) && !(env->mmuregs[0] & MMU_NF)) {
  418. int tt = is_exec ? TT_CODE_ACCESS : TT_DATA_ACCESS;
  419. cpu_raise_exception_ra(env, tt, retaddr);
  420. }
  421. /*
  422. * flush neverland mappings created during no-fault mode,
  423. * so the sequential MMU faults report proper fault types
  424. */
  425. if (env->mmuregs[0] & MMU_NF) {
  426. tlb_flush(cs);
  427. }
  428. }
  429. #else
  430. static void sparc_raise_mmu_fault(CPUState *cs, hwaddr addr,
  431. bool is_write, bool is_exec, int is_asi,
  432. unsigned size, uintptr_t retaddr)
  433. {
  434. CPUSPARCState *env = cpu_env(cs);
  435. #ifdef DEBUG_UNASSIGNED
  436. printf("Unassigned mem access to " HWADDR_FMT_plx " from " TARGET_FMT_lx
  437. "\n", addr, env->pc);
  438. #endif
  439. if (is_exec) { /* XXX has_hypervisor */
  440. if (env->lsu & (IMMU_E)) {
  441. cpu_raise_exception_ra(env, TT_CODE_ACCESS, retaddr);
  442. } else if (cpu_has_hypervisor(env) && !(env->hpstate & HS_PRIV)) {
  443. cpu_raise_exception_ra(env, TT_INSN_REAL_TRANSLATION_MISS, retaddr);
  444. }
  445. } else {
  446. if (env->lsu & (DMMU_E)) {
  447. cpu_raise_exception_ra(env, TT_DATA_ACCESS, retaddr);
  448. } else if (cpu_has_hypervisor(env) && !(env->hpstate & HS_PRIV)) {
  449. cpu_raise_exception_ra(env, TT_DATA_REAL_TRANSLATION_MISS, retaddr);
  450. }
  451. }
  452. }
  453. #endif
  454. #endif
  455. #ifndef TARGET_SPARC64
  456. #ifndef CONFIG_USER_ONLY
  457. /* Leon3 cache control */
  458. static void leon3_cache_control_st(CPUSPARCState *env, target_ulong addr,
  459. uint64_t val, int size)
  460. {
  461. DPRINTF_CACHE_CONTROL("st addr:%08x, val:%" PRIx64 ", size:%d\n",
  462. addr, val, size);
  463. if (size != 4) {
  464. DPRINTF_CACHE_CONTROL("32bits only\n");
  465. return;
  466. }
  467. switch (addr) {
  468. case 0x00: /* Cache control */
  469. /* These values must always be read as zeros */
  470. val &= ~CACHE_CTRL_FD;
  471. val &= ~CACHE_CTRL_FI;
  472. val &= ~CACHE_CTRL_IB;
  473. val &= ~CACHE_CTRL_IP;
  474. val &= ~CACHE_CTRL_DP;
  475. env->cache_control = val;
  476. break;
  477. case 0x04: /* Instruction cache configuration */
  478. case 0x08: /* Data cache configuration */
  479. /* Read Only */
  480. break;
  481. default:
  482. DPRINTF_CACHE_CONTROL("write unknown register %08x\n", addr);
  483. break;
  484. };
  485. }
  486. static uint64_t leon3_cache_control_ld(CPUSPARCState *env, target_ulong addr,
  487. int size)
  488. {
  489. uint64_t ret = 0;
  490. if (size != 4) {
  491. DPRINTF_CACHE_CONTROL("32bits only\n");
  492. return 0;
  493. }
  494. switch (addr) {
  495. case 0x00: /* Cache control */
  496. ret = env->cache_control;
  497. break;
  498. /* Configuration registers are read and only always keep those
  499. predefined values */
  500. case 0x04: /* Instruction cache configuration */
  501. ret = 0x10220000;
  502. break;
  503. case 0x08: /* Data cache configuration */
  504. ret = 0x18220000;
  505. break;
  506. default:
  507. DPRINTF_CACHE_CONTROL("read unknown register %08x\n", addr);
  508. break;
  509. };
  510. DPRINTF_CACHE_CONTROL("ld addr:%08x, ret:0x%" PRIx64 ", size:%d\n",
  511. addr, ret, size);
  512. return ret;
  513. }
  514. uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
  515. int asi, uint32_t memop)
  516. {
  517. int size = 1 << (memop & MO_SIZE);
  518. int sign = memop & MO_SIGN;
  519. CPUState *cs = env_cpu(env);
  520. uint64_t ret = 0;
  521. #if defined(DEBUG_MXCC) || defined(DEBUG_ASI)
  522. uint32_t last_addr = addr;
  523. #endif
  524. do_check_align(env, addr, size - 1, GETPC());
  525. switch (asi) {
  526. case ASI_M_MXCC: /* SuperSparc MXCC registers, or... */
  527. /* case ASI_LEON_CACHEREGS: Leon3 cache control */
  528. switch (addr) {
  529. case 0x00: /* Leon3 Cache Control */
  530. case 0x08: /* Leon3 Instruction Cache config */
  531. case 0x0C: /* Leon3 Date Cache config */
  532. if (env->def.features & CPU_FEATURE_CACHE_CTRL) {
  533. ret = leon3_cache_control_ld(env, addr, size);
  534. }
  535. break;
  536. case 0x01c00a00: /* MXCC control register */
  537. if (size == 8) {
  538. ret = env->mxccregs[3];
  539. } else {
  540. qemu_log_mask(LOG_UNIMP,
  541. "%08x: unimplemented access size: %d\n", addr,
  542. size);
  543. }
  544. break;
  545. case 0x01c00a04: /* MXCC control register */
  546. if (size == 4) {
  547. ret = env->mxccregs[3];
  548. } else {
  549. qemu_log_mask(LOG_UNIMP,
  550. "%08x: unimplemented access size: %d\n", addr,
  551. size);
  552. }
  553. break;
  554. case 0x01c00c00: /* Module reset register */
  555. if (size == 8) {
  556. ret = env->mxccregs[5];
  557. /* should we do something here? */
  558. } else {
  559. qemu_log_mask(LOG_UNIMP,
  560. "%08x: unimplemented access size: %d\n", addr,
  561. size);
  562. }
  563. break;
  564. case 0x01c00f00: /* MBus port address register */
  565. if (size == 8) {
  566. ret = env->mxccregs[7];
  567. } else {
  568. qemu_log_mask(LOG_UNIMP,
  569. "%08x: unimplemented access size: %d\n", addr,
  570. size);
  571. }
  572. break;
  573. default:
  574. qemu_log_mask(LOG_UNIMP,
  575. "%08x: unimplemented address, size: %d\n", addr,
  576. size);
  577. break;
  578. }
  579. DPRINTF_MXCC("asi = %d, size = %d, sign = %d, "
  580. "addr = %08x -> ret = %" PRIx64 ","
  581. "addr = %08x\n", asi, size, sign, last_addr, ret, addr);
  582. #ifdef DEBUG_MXCC
  583. dump_mxcc(env);
  584. #endif
  585. break;
  586. case ASI_M_FLUSH_PROBE: /* SuperSparc MMU probe */
  587. case ASI_LEON_MMUFLUSH: /* LEON3 MMU probe */
  588. {
  589. int mmulev;
  590. mmulev = (addr >> 8) & 15;
  591. if (mmulev > 4) {
  592. ret = 0;
  593. } else {
  594. ret = mmu_probe(env, addr, mmulev);
  595. }
  596. DPRINTF_MMU("mmu_probe: 0x%08x (lev %d) -> 0x%08" PRIx64 "\n",
  597. addr, mmulev, ret);
  598. }
  599. break;
  600. case ASI_M_MMUREGS: /* SuperSparc MMU regs */
  601. case ASI_LEON_MMUREGS: /* LEON3 MMU regs */
  602. {
  603. int reg = (addr >> 8) & 0x1f;
  604. ret = env->mmuregs[reg];
  605. if (reg == 3) { /* Fault status cleared on read */
  606. env->mmuregs[3] = 0;
  607. } else if (reg == 0x13) { /* Fault status read */
  608. ret = env->mmuregs[3];
  609. } else if (reg == 0x14) { /* Fault address read */
  610. ret = env->mmuregs[4];
  611. }
  612. DPRINTF_MMU("mmu_read: reg[%d] = 0x%08" PRIx64 "\n", reg, ret);
  613. }
  614. break;
  615. case ASI_M_TLBDIAG: /* Turbosparc ITLB Diagnostic */
  616. case ASI_M_DIAGS: /* Turbosparc DTLB Diagnostic */
  617. case ASI_M_IODIAG: /* Turbosparc IOTLB Diagnostic */
  618. break;
  619. case ASI_M_TXTC_TAG: /* SparcStation 5 I-cache tag */
  620. case ASI_M_TXTC_DATA: /* SparcStation 5 I-cache data */
  621. case ASI_M_DATAC_TAG: /* SparcStation 5 D-cache tag */
  622. case ASI_M_DATAC_DATA: /* SparcStation 5 D-cache data */
  623. break;
  624. case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
  625. {
  626. MemTxResult result;
  627. hwaddr access_addr = (hwaddr)addr | ((hwaddr)(asi & 0xf) << 32);
  628. switch (size) {
  629. case 1:
  630. ret = address_space_ldub(cs->as, access_addr,
  631. MEMTXATTRS_UNSPECIFIED, &result);
  632. break;
  633. case 2:
  634. ret = address_space_lduw(cs->as, access_addr,
  635. MEMTXATTRS_UNSPECIFIED, &result);
  636. break;
  637. default:
  638. case 4:
  639. ret = address_space_ldl(cs->as, access_addr,
  640. MEMTXATTRS_UNSPECIFIED, &result);
  641. break;
  642. case 8:
  643. ret = address_space_ldq(cs->as, access_addr,
  644. MEMTXATTRS_UNSPECIFIED, &result);
  645. break;
  646. }
  647. if (result != MEMTX_OK) {
  648. sparc_raise_mmu_fault(cs, access_addr, false, false, false,
  649. size, GETPC());
  650. }
  651. break;
  652. }
  653. case 0x30: /* Turbosparc secondary cache diagnostic */
  654. case 0x31: /* Turbosparc RAM snoop */
  655. case 0x32: /* Turbosparc page table descriptor diagnostic */
  656. case 0x39: /* data cache diagnostic register */
  657. ret = 0;
  658. break;
  659. case 0x38: /* SuperSPARC MMU Breakpoint Control Registers */
  660. {
  661. int reg = (addr >> 8) & 3;
  662. switch (reg) {
  663. case 0: /* Breakpoint Value (Addr) */
  664. ret = env->mmubpregs[reg];
  665. break;
  666. case 1: /* Breakpoint Mask */
  667. ret = env->mmubpregs[reg];
  668. break;
  669. case 2: /* Breakpoint Control */
  670. ret = env->mmubpregs[reg];
  671. break;
  672. case 3: /* Breakpoint Status */
  673. ret = env->mmubpregs[reg];
  674. env->mmubpregs[reg] = 0ULL;
  675. break;
  676. }
  677. DPRINTF_MMU("read breakpoint reg[%d] 0x%016" PRIx64 "\n", reg,
  678. ret);
  679. }
  680. break;
  681. case 0x49: /* SuperSPARC MMU Counter Breakpoint Value */
  682. ret = env->mmubpctrv;
  683. break;
  684. case 0x4a: /* SuperSPARC MMU Counter Breakpoint Control */
  685. ret = env->mmubpctrc;
  686. break;
  687. case 0x4b: /* SuperSPARC MMU Counter Breakpoint Status */
  688. ret = env->mmubpctrs;
  689. break;
  690. case 0x4c: /* SuperSPARC MMU Breakpoint Action */
  691. ret = env->mmubpaction;
  692. break;
  693. default:
  694. sparc_raise_mmu_fault(cs, addr, false, false, asi, size, GETPC());
  695. ret = 0;
  696. break;
  697. case ASI_USERDATA: /* User data access */
  698. case ASI_KERNELDATA: /* Supervisor data access */
  699. case ASI_USERTXT: /* User code access */
  700. case ASI_KERNELTXT: /* Supervisor code access */
  701. case ASI_P: /* Implicit primary context data access (v9 only?) */
  702. case ASI_M_BYPASS: /* MMU passthrough */
  703. case ASI_LEON_BYPASS: /* LEON MMU passthrough */
  704. /* These are always handled inline. */
  705. g_assert_not_reached();
  706. }
  707. if (sign) {
  708. switch (size) {
  709. case 1:
  710. ret = (int8_t) ret;
  711. break;
  712. case 2:
  713. ret = (int16_t) ret;
  714. break;
  715. case 4:
  716. ret = (int32_t) ret;
  717. break;
  718. default:
  719. break;
  720. }
  721. }
  722. #ifdef DEBUG_ASI
  723. dump_asi("read ", last_addr, asi, size, ret);
  724. #endif
  725. return ret;
  726. }
  727. void helper_st_asi(CPUSPARCState *env, target_ulong addr, uint64_t val,
  728. int asi, uint32_t memop)
  729. {
  730. int size = 1 << (memop & MO_SIZE);
  731. CPUState *cs = env_cpu(env);
  732. do_check_align(env, addr, size - 1, GETPC());
  733. switch (asi) {
  734. case ASI_M_MXCC: /* SuperSparc MXCC registers, or... */
  735. /* case ASI_LEON_CACHEREGS: Leon3 cache control */
  736. switch (addr) {
  737. case 0x00: /* Leon3 Cache Control */
  738. case 0x08: /* Leon3 Instruction Cache config */
  739. case 0x0C: /* Leon3 Date Cache config */
  740. if (env->def.features & CPU_FEATURE_CACHE_CTRL) {
  741. leon3_cache_control_st(env, addr, val, size);
  742. }
  743. break;
  744. case 0x01c00000: /* MXCC stream data register 0 */
  745. if (size == 8) {
  746. env->mxccdata[0] = val;
  747. } else {
  748. qemu_log_mask(LOG_UNIMP,
  749. "%08x: unimplemented access size: %d\n", addr,
  750. size);
  751. }
  752. break;
  753. case 0x01c00008: /* MXCC stream data register 1 */
  754. if (size == 8) {
  755. env->mxccdata[1] = val;
  756. } else {
  757. qemu_log_mask(LOG_UNIMP,
  758. "%08x: unimplemented access size: %d\n", addr,
  759. size);
  760. }
  761. break;
  762. case 0x01c00010: /* MXCC stream data register 2 */
  763. if (size == 8) {
  764. env->mxccdata[2] = val;
  765. } else {
  766. qemu_log_mask(LOG_UNIMP,
  767. "%08x: unimplemented access size: %d\n", addr,
  768. size);
  769. }
  770. break;
  771. case 0x01c00018: /* MXCC stream data register 3 */
  772. if (size == 8) {
  773. env->mxccdata[3] = val;
  774. } else {
  775. qemu_log_mask(LOG_UNIMP,
  776. "%08x: unimplemented access size: %d\n", addr,
  777. size);
  778. }
  779. break;
  780. case 0x01c00100: /* MXCC stream source */
  781. {
  782. int i;
  783. if (size == 8) {
  784. env->mxccregs[0] = val;
  785. } else {
  786. qemu_log_mask(LOG_UNIMP,
  787. "%08x: unimplemented access size: %d\n", addr,
  788. size);
  789. }
  790. for (i = 0; i < 4; i++) {
  791. MemTxResult result;
  792. hwaddr access_addr = (env->mxccregs[0] & 0xffffffffULL) + 8 * i;
  793. env->mxccdata[i] = address_space_ldq(cs->as,
  794. access_addr,
  795. MEMTXATTRS_UNSPECIFIED,
  796. &result);
  797. if (result != MEMTX_OK) {
  798. /* TODO: investigate whether this is the right behaviour */
  799. sparc_raise_mmu_fault(cs, access_addr, false, false,
  800. false, size, GETPC());
  801. }
  802. }
  803. break;
  804. }
  805. case 0x01c00200: /* MXCC stream destination */
  806. {
  807. int i;
  808. if (size == 8) {
  809. env->mxccregs[1] = val;
  810. } else {
  811. qemu_log_mask(LOG_UNIMP,
  812. "%08x: unimplemented access size: %d\n", addr,
  813. size);
  814. }
  815. for (i = 0; i < 4; i++) {
  816. MemTxResult result;
  817. hwaddr access_addr = (env->mxccregs[1] & 0xffffffffULL) + 8 * i;
  818. address_space_stq(cs->as, access_addr, env->mxccdata[i],
  819. MEMTXATTRS_UNSPECIFIED, &result);
  820. if (result != MEMTX_OK) {
  821. /* TODO: investigate whether this is the right behaviour */
  822. sparc_raise_mmu_fault(cs, access_addr, true, false,
  823. false, size, GETPC());
  824. }
  825. }
  826. break;
  827. }
  828. case 0x01c00a00: /* MXCC control register */
  829. if (size == 8) {
  830. env->mxccregs[3] = val;
  831. } else {
  832. qemu_log_mask(LOG_UNIMP,
  833. "%08x: unimplemented access size: %d\n", addr,
  834. size);
  835. }
  836. break;
  837. case 0x01c00a04: /* MXCC control register */
  838. if (size == 4) {
  839. env->mxccregs[3] = (env->mxccregs[3] & 0xffffffff00000000ULL)
  840. | val;
  841. } else {
  842. qemu_log_mask(LOG_UNIMP,
  843. "%08x: unimplemented access size: %d\n", addr,
  844. size);
  845. }
  846. break;
  847. case 0x01c00e00: /* MXCC error register */
  848. /* writing a 1 bit clears the error */
  849. if (size == 8) {
  850. env->mxccregs[6] &= ~val;
  851. } else {
  852. qemu_log_mask(LOG_UNIMP,
  853. "%08x: unimplemented access size: %d\n", addr,
  854. size);
  855. }
  856. break;
  857. case 0x01c00f00: /* MBus port address register */
  858. if (size == 8) {
  859. env->mxccregs[7] = val;
  860. } else {
  861. qemu_log_mask(LOG_UNIMP,
  862. "%08x: unimplemented access size: %d\n", addr,
  863. size);
  864. }
  865. break;
  866. default:
  867. qemu_log_mask(LOG_UNIMP,
  868. "%08x: unimplemented address, size: %d\n", addr,
  869. size);
  870. break;
  871. }
  872. DPRINTF_MXCC("asi = %d, size = %d, addr = %08x, val = %" PRIx64 "\n",
  873. asi, size, addr, val);
  874. #ifdef DEBUG_MXCC
  875. dump_mxcc(env);
  876. #endif
  877. break;
  878. case ASI_M_FLUSH_PROBE: /* SuperSparc MMU flush */
  879. case ASI_LEON_MMUFLUSH: /* LEON3 MMU flush */
  880. {
  881. int mmulev;
  882. mmulev = (addr >> 8) & 15;
  883. DPRINTF_MMU("mmu flush level %d\n", mmulev);
  884. switch (mmulev) {
  885. case 0: /* flush page */
  886. tlb_flush_page(cs, addr & 0xfffff000);
  887. break;
  888. case 1: /* flush segment (256k) */
  889. case 2: /* flush region (16M) */
  890. case 3: /* flush context (4G) */
  891. case 4: /* flush entire */
  892. tlb_flush(cs);
  893. break;
  894. default:
  895. break;
  896. }
  897. #ifdef DEBUG_MMU
  898. dump_mmu(env);
  899. #endif
  900. }
  901. break;
  902. case ASI_M_MMUREGS: /* write MMU regs */
  903. case ASI_LEON_MMUREGS: /* LEON3 write MMU regs */
  904. {
  905. int reg = (addr >> 8) & 0x1f;
  906. uint32_t oldreg;
  907. oldreg = env->mmuregs[reg];
  908. switch (reg) {
  909. case 0: /* Control Register */
  910. env->mmuregs[reg] = (env->mmuregs[reg] & 0xff000000) |
  911. (val & 0x00ffffff);
  912. /* Mappings generated during no-fault mode
  913. are invalid in normal mode. */
  914. if ((oldreg ^ env->mmuregs[reg])
  915. & (MMU_NF | env->def.mmu_bm)) {
  916. tlb_flush(cs);
  917. }
  918. break;
  919. case 1: /* Context Table Pointer Register */
  920. env->mmuregs[reg] = val & env->def.mmu_ctpr_mask;
  921. break;
  922. case 2: /* Context Register */
  923. env->mmuregs[reg] = val & env->def.mmu_cxr_mask;
  924. if (oldreg != env->mmuregs[reg]) {
  925. /* we flush when the MMU context changes because
  926. QEMU has no MMU context support */
  927. tlb_flush(cs);
  928. }
  929. break;
  930. case 3: /* Synchronous Fault Status Register with Clear */
  931. case 4: /* Synchronous Fault Address Register */
  932. break;
  933. case 0x10: /* TLB Replacement Control Register */
  934. env->mmuregs[reg] = val & env->def.mmu_trcr_mask;
  935. break;
  936. case 0x13: /* Synchronous Fault Status Register with Read
  937. and Clear */
  938. env->mmuregs[3] = val & env->def.mmu_sfsr_mask;
  939. break;
  940. case 0x14: /* Synchronous Fault Address Register */
  941. env->mmuregs[4] = val;
  942. break;
  943. default:
  944. env->mmuregs[reg] = val;
  945. break;
  946. }
  947. if (oldreg != env->mmuregs[reg]) {
  948. DPRINTF_MMU("mmu change reg[%d]: 0x%08x -> 0x%08x\n",
  949. reg, oldreg, env->mmuregs[reg]);
  950. }
  951. #ifdef DEBUG_MMU
  952. dump_mmu(env);
  953. #endif
  954. }
  955. break;
  956. case ASI_M_TLBDIAG: /* Turbosparc ITLB Diagnostic */
  957. case ASI_M_DIAGS: /* Turbosparc DTLB Diagnostic */
  958. case ASI_M_IODIAG: /* Turbosparc IOTLB Diagnostic */
  959. break;
  960. case ASI_M_TXTC_TAG: /* I-cache tag */
  961. case ASI_M_TXTC_DATA: /* I-cache data */
  962. case ASI_M_DATAC_TAG: /* D-cache tag */
  963. case ASI_M_DATAC_DATA: /* D-cache data */
  964. case ASI_M_FLUSH_PAGE: /* I/D-cache flush page */
  965. case ASI_M_FLUSH_SEG: /* I/D-cache flush segment */
  966. case ASI_M_FLUSH_REGION: /* I/D-cache flush region */
  967. case ASI_M_FLUSH_CTX: /* I/D-cache flush context */
  968. case ASI_M_FLUSH_USER: /* I/D-cache flush user */
  969. break;
  970. case 0x21 ... 0x2f: /* MMU passthrough, 0x100000000 to 0xfffffffff */
  971. {
  972. MemTxResult result;
  973. hwaddr access_addr = (hwaddr)addr | ((hwaddr)(asi & 0xf) << 32);
  974. switch (size) {
  975. case 1:
  976. address_space_stb(cs->as, access_addr, val,
  977. MEMTXATTRS_UNSPECIFIED, &result);
  978. break;
  979. case 2:
  980. address_space_stw(cs->as, access_addr, val,
  981. MEMTXATTRS_UNSPECIFIED, &result);
  982. break;
  983. case 4:
  984. default:
  985. address_space_stl(cs->as, access_addr, val,
  986. MEMTXATTRS_UNSPECIFIED, &result);
  987. break;
  988. case 8:
  989. address_space_stq(cs->as, access_addr, val,
  990. MEMTXATTRS_UNSPECIFIED, &result);
  991. break;
  992. }
  993. if (result != MEMTX_OK) {
  994. sparc_raise_mmu_fault(cs, access_addr, true, false, false,
  995. size, GETPC());
  996. }
  997. }
  998. break;
  999. case 0x30: /* store buffer tags or Turbosparc secondary cache diagnostic */
  1000. case 0x31: /* store buffer data, Ross RT620 I-cache flush or
  1001. Turbosparc snoop RAM */
  1002. case 0x32: /* store buffer control or Turbosparc page table
  1003. descriptor diagnostic */
  1004. case 0x36: /* I-cache flash clear */
  1005. case 0x37: /* D-cache flash clear */
  1006. break;
  1007. case 0x38: /* SuperSPARC MMU Breakpoint Control Registers*/
  1008. {
  1009. int reg = (addr >> 8) & 3;
  1010. switch (reg) {
  1011. case 0: /* Breakpoint Value (Addr) */
  1012. env->mmubpregs[reg] = (val & 0xfffffffffULL);
  1013. break;
  1014. case 1: /* Breakpoint Mask */
  1015. env->mmubpregs[reg] = (val & 0xfffffffffULL);
  1016. break;
  1017. case 2: /* Breakpoint Control */
  1018. env->mmubpregs[reg] = (val & 0x7fULL);
  1019. break;
  1020. case 3: /* Breakpoint Status */
  1021. env->mmubpregs[reg] = (val & 0xfULL);
  1022. break;
  1023. }
  1024. DPRINTF_MMU("write breakpoint reg[%d] 0x%016x\n", reg,
  1025. env->mmuregs[reg]);
  1026. }
  1027. break;
  1028. case 0x49: /* SuperSPARC MMU Counter Breakpoint Value */
  1029. env->mmubpctrv = val & 0xffffffff;
  1030. break;
  1031. case 0x4a: /* SuperSPARC MMU Counter Breakpoint Control */
  1032. env->mmubpctrc = val & 0x3;
  1033. break;
  1034. case 0x4b: /* SuperSPARC MMU Counter Breakpoint Status */
  1035. env->mmubpctrs = val & 0x3;
  1036. break;
  1037. case 0x4c: /* SuperSPARC MMU Breakpoint Action */
  1038. env->mmubpaction = val & 0x1fff;
  1039. break;
  1040. case ASI_USERTXT: /* User code access, XXX */
  1041. case ASI_KERNELTXT: /* Supervisor code access, XXX */
  1042. default:
  1043. sparc_raise_mmu_fault(cs, addr, true, false, asi, size, GETPC());
  1044. break;
  1045. case ASI_USERDATA: /* User data access */
  1046. case ASI_KERNELDATA: /* Supervisor data access */
  1047. case ASI_P:
  1048. case ASI_M_BYPASS: /* MMU passthrough */
  1049. case ASI_LEON_BYPASS: /* LEON MMU passthrough */
  1050. case ASI_M_BCOPY: /* Block copy, sta access */
  1051. case ASI_M_BFILL: /* Block fill, stda access */
  1052. /* These are always handled inline. */
  1053. g_assert_not_reached();
  1054. }
  1055. #ifdef DEBUG_ASI
  1056. dump_asi("write", addr, asi, size, val);
  1057. #endif
  1058. }
  1059. uint64_t helper_ld_code(CPUSPARCState *env, target_ulong addr, uint32_t oi)
  1060. {
  1061. MemOp mop = get_memop(oi);
  1062. uintptr_t ra = GETPC();
  1063. uint64_t ret;
  1064. switch (mop & MO_SIZE) {
  1065. case MO_8:
  1066. ret = cpu_ldb_code_mmu(env, addr, oi, ra);
  1067. if (mop & MO_SIGN) {
  1068. ret = (int8_t)ret;
  1069. }
  1070. break;
  1071. case MO_16:
  1072. ret = cpu_ldw_code_mmu(env, addr, oi, ra);
  1073. if ((mop & MO_BSWAP) != MO_TE) {
  1074. ret = bswap16(ret);
  1075. }
  1076. if (mop & MO_SIGN) {
  1077. ret = (int16_t)ret;
  1078. }
  1079. break;
  1080. case MO_32:
  1081. ret = cpu_ldl_code_mmu(env, addr, oi, ra);
  1082. if ((mop & MO_BSWAP) != MO_TE) {
  1083. ret = bswap32(ret);
  1084. }
  1085. if (mop & MO_SIGN) {
  1086. ret = (int32_t)ret;
  1087. }
  1088. break;
  1089. case MO_64:
  1090. ret = cpu_ldq_code_mmu(env, addr, oi, ra);
  1091. if ((mop & MO_BSWAP) != MO_TE) {
  1092. ret = bswap64(ret);
  1093. }
  1094. break;
  1095. default:
  1096. g_assert_not_reached();
  1097. }
  1098. return ret;
  1099. }
  1100. #endif /* CONFIG_USER_ONLY */
  1101. #else /* TARGET_SPARC64 */
  1102. #ifdef CONFIG_USER_ONLY
  1103. uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
  1104. int asi, uint32_t memop)
  1105. {
  1106. int size = 1 << (memop & MO_SIZE);
  1107. int sign = memop & MO_SIGN;
  1108. uint64_t ret = 0;
  1109. if (asi < 0x80) {
  1110. cpu_raise_exception_ra(env, TT_PRIV_ACT, GETPC());
  1111. }
  1112. do_check_align(env, addr, size - 1, GETPC());
  1113. addr = asi_address_mask(env, asi, addr);
  1114. switch (asi) {
  1115. case ASI_PNF: /* Primary no-fault */
  1116. case ASI_PNFL: /* Primary no-fault LE */
  1117. case ASI_SNF: /* Secondary no-fault */
  1118. case ASI_SNFL: /* Secondary no-fault LE */
  1119. if (!page_check_range(addr, size, PAGE_READ)) {
  1120. ret = 0;
  1121. break;
  1122. }
  1123. switch (size) {
  1124. case 1:
  1125. ret = cpu_ldub_data(env, addr);
  1126. break;
  1127. case 2:
  1128. ret = cpu_lduw_data(env, addr);
  1129. break;
  1130. case 4:
  1131. ret = cpu_ldl_data(env, addr);
  1132. break;
  1133. case 8:
  1134. ret = cpu_ldq_data(env, addr);
  1135. break;
  1136. default:
  1137. g_assert_not_reached();
  1138. }
  1139. break;
  1140. break;
  1141. case ASI_P: /* Primary */
  1142. case ASI_PL: /* Primary LE */
  1143. case ASI_S: /* Secondary */
  1144. case ASI_SL: /* Secondary LE */
  1145. /* These are always handled inline. */
  1146. g_assert_not_reached();
  1147. default:
  1148. cpu_raise_exception_ra(env, TT_DATA_ACCESS, GETPC());
  1149. }
  1150. /* Convert from little endian */
  1151. switch (asi) {
  1152. case ASI_PNFL: /* Primary no-fault LE */
  1153. case ASI_SNFL: /* Secondary no-fault LE */
  1154. switch (size) {
  1155. case 2:
  1156. ret = bswap16(ret);
  1157. break;
  1158. case 4:
  1159. ret = bswap32(ret);
  1160. break;
  1161. case 8:
  1162. ret = bswap64(ret);
  1163. break;
  1164. }
  1165. }
  1166. /* Convert to signed number */
  1167. if (sign) {
  1168. switch (size) {
  1169. case 1:
  1170. ret = (int8_t) ret;
  1171. break;
  1172. case 2:
  1173. ret = (int16_t) ret;
  1174. break;
  1175. case 4:
  1176. ret = (int32_t) ret;
  1177. break;
  1178. }
  1179. }
  1180. #ifdef DEBUG_ASI
  1181. dump_asi("read", addr, asi, size, ret);
  1182. #endif
  1183. return ret;
  1184. }
  1185. void helper_st_asi(CPUSPARCState *env, target_ulong addr, target_ulong val,
  1186. int asi, uint32_t memop)
  1187. {
  1188. int size = 1 << (memop & MO_SIZE);
  1189. #ifdef DEBUG_ASI
  1190. dump_asi("write", addr, asi, size, val);
  1191. #endif
  1192. if (asi < 0x80) {
  1193. cpu_raise_exception_ra(env, TT_PRIV_ACT, GETPC());
  1194. }
  1195. do_check_align(env, addr, size - 1, GETPC());
  1196. switch (asi) {
  1197. case ASI_P: /* Primary */
  1198. case ASI_PL: /* Primary LE */
  1199. case ASI_S: /* Secondary */
  1200. case ASI_SL: /* Secondary LE */
  1201. /* These are always handled inline. */
  1202. g_assert_not_reached();
  1203. case ASI_PNF: /* Primary no-fault, RO */
  1204. case ASI_SNF: /* Secondary no-fault, RO */
  1205. case ASI_PNFL: /* Primary no-fault LE, RO */
  1206. case ASI_SNFL: /* Secondary no-fault LE, RO */
  1207. default:
  1208. cpu_raise_exception_ra(env, TT_DATA_ACCESS, GETPC());
  1209. }
  1210. }
  1211. #else /* CONFIG_USER_ONLY */
  1212. uint64_t helper_ld_asi(CPUSPARCState *env, target_ulong addr,
  1213. int asi, uint32_t memop)
  1214. {
  1215. int size = 1 << (memop & MO_SIZE);
  1216. int sign = memop & MO_SIGN;
  1217. CPUState *cs = env_cpu(env);
  1218. uint64_t ret = 0;
  1219. #if defined(DEBUG_ASI)
  1220. target_ulong last_addr = addr;
  1221. #endif
  1222. asi &= 0xff;
  1223. do_check_asi(env, asi, GETPC());
  1224. do_check_align(env, addr, size - 1, GETPC());
  1225. addr = asi_address_mask(env, asi, addr);
  1226. switch (asi) {
  1227. case ASI_PNF:
  1228. case ASI_PNFL:
  1229. case ASI_SNF:
  1230. case ASI_SNFL:
  1231. {
  1232. MemOpIdx oi;
  1233. int idx = (env->pstate & PS_PRIV
  1234. ? (asi & 1 ? MMU_KERNEL_SECONDARY_IDX : MMU_KERNEL_IDX)
  1235. : (asi & 1 ? MMU_USER_SECONDARY_IDX : MMU_USER_IDX));
  1236. if (cpu_get_phys_page_nofault(env, addr, idx) == -1ULL) {
  1237. #ifdef DEBUG_ASI
  1238. dump_asi("read ", last_addr, asi, size, ret);
  1239. #endif
  1240. /* exception_index is set in get_physical_address_data. */
  1241. cpu_raise_exception_ra(env, cs->exception_index, GETPC());
  1242. }
  1243. oi = make_memop_idx(memop, idx);
  1244. switch (size) {
  1245. case 1:
  1246. ret = cpu_ldb_mmu(env, addr, oi, GETPC());
  1247. break;
  1248. case 2:
  1249. ret = cpu_ldw_mmu(env, addr, oi, GETPC());
  1250. break;
  1251. case 4:
  1252. ret = cpu_ldl_mmu(env, addr, oi, GETPC());
  1253. break;
  1254. case 8:
  1255. ret = cpu_ldq_mmu(env, addr, oi, GETPC());
  1256. break;
  1257. default:
  1258. g_assert_not_reached();
  1259. }
  1260. }
  1261. break;
  1262. case ASI_AIUP: /* As if user primary */
  1263. case ASI_AIUS: /* As if user secondary */
  1264. case ASI_AIUPL: /* As if user primary LE */
  1265. case ASI_AIUSL: /* As if user secondary LE */
  1266. case ASI_P: /* Primary */
  1267. case ASI_S: /* Secondary */
  1268. case ASI_PL: /* Primary LE */
  1269. case ASI_SL: /* Secondary LE */
  1270. case ASI_REAL: /* Bypass */
  1271. case ASI_REAL_IO: /* Bypass, non-cacheable */
  1272. case ASI_REAL_L: /* Bypass LE */
  1273. case ASI_REAL_IO_L: /* Bypass, non-cacheable LE */
  1274. case ASI_N: /* Nucleus */
  1275. case ASI_NL: /* Nucleus Little Endian (LE) */
  1276. case ASI_NUCLEUS_QUAD_LDD: /* Nucleus quad LDD 128 bit atomic */
  1277. case ASI_NUCLEUS_QUAD_LDD_L: /* Nucleus quad LDD 128 bit atomic LE */
  1278. case ASI_TWINX_AIUP: /* As if user primary, twinx */
  1279. case ASI_TWINX_AIUS: /* As if user secondary, twinx */
  1280. case ASI_TWINX_REAL: /* Real address, twinx */
  1281. case ASI_TWINX_AIUP_L: /* As if user primary, twinx, LE */
  1282. case ASI_TWINX_AIUS_L: /* As if user secondary, twinx, LE */
  1283. case ASI_TWINX_REAL_L: /* Real address, twinx, LE */
  1284. case ASI_TWINX_N: /* Nucleus, twinx */
  1285. case ASI_TWINX_NL: /* Nucleus, twinx, LE */
  1286. /* ??? From the UA2011 document; overlaps BLK_INIT_QUAD_LDD_* */
  1287. case ASI_TWINX_P: /* Primary, twinx */
  1288. case ASI_TWINX_PL: /* Primary, twinx, LE */
  1289. case ASI_TWINX_S: /* Secondary, twinx */
  1290. case ASI_TWINX_SL: /* Secondary, twinx, LE */
  1291. case ASI_MON_P:
  1292. case ASI_MON_S:
  1293. case ASI_MON_AIUP:
  1294. case ASI_MON_AIUS:
  1295. /* These are always handled inline. */
  1296. g_assert_not_reached();
  1297. case ASI_UPA_CONFIG: /* UPA config */
  1298. /* XXX */
  1299. break;
  1300. case ASI_LSU_CONTROL: /* LSU */
  1301. ret = env->lsu;
  1302. break;
  1303. case ASI_IMMU: /* I-MMU regs */
  1304. {
  1305. int reg = (addr >> 3) & 0xf;
  1306. switch (reg) {
  1307. case 0:
  1308. /* 0x00 I-TSB Tag Target register */
  1309. ret = ultrasparc_tag_target(env->immu.tag_access);
  1310. break;
  1311. case 3: /* SFSR */
  1312. ret = env->immu.sfsr;
  1313. break;
  1314. case 5: /* TSB access */
  1315. ret = env->immu.tsb;
  1316. break;
  1317. case 6:
  1318. /* 0x30 I-TSB Tag Access register */
  1319. ret = env->immu.tag_access;
  1320. break;
  1321. default:
  1322. sparc_raise_mmu_fault(cs, addr, false, false, 1, size, GETPC());
  1323. ret = 0;
  1324. }
  1325. break;
  1326. }
  1327. case ASI_IMMU_TSB_8KB_PTR: /* I-MMU 8k TSB pointer */
  1328. {
  1329. /* env->immuregs[5] holds I-MMU TSB register value
  1330. env->immuregs[6] holds I-MMU Tag Access register value */
  1331. ret = ultrasparc_tsb_pointer(env, &env->immu, 0);
  1332. break;
  1333. }
  1334. case ASI_IMMU_TSB_64KB_PTR: /* I-MMU 64k TSB pointer */
  1335. {
  1336. /* env->immuregs[5] holds I-MMU TSB register value
  1337. env->immuregs[6] holds I-MMU Tag Access register value */
  1338. ret = ultrasparc_tsb_pointer(env, &env->immu, 1);
  1339. break;
  1340. }
  1341. case ASI_ITLB_DATA_ACCESS: /* I-MMU data access */
  1342. {
  1343. int reg = (addr >> 3) & 0x3f;
  1344. ret = env->itlb[reg].tte;
  1345. break;
  1346. }
  1347. case ASI_ITLB_TAG_READ: /* I-MMU tag read */
  1348. {
  1349. int reg = (addr >> 3) & 0x3f;
  1350. ret = env->itlb[reg].tag;
  1351. break;
  1352. }
  1353. case ASI_DMMU: /* D-MMU regs */
  1354. {
  1355. int reg = (addr >> 3) & 0xf;
  1356. switch (reg) {
  1357. case 0:
  1358. /* 0x00 D-TSB Tag Target register */
  1359. ret = ultrasparc_tag_target(env->dmmu.tag_access);
  1360. break;
  1361. case 1: /* 0x08 Primary Context */
  1362. ret = env->dmmu.mmu_primary_context;
  1363. break;
  1364. case 2: /* 0x10 Secondary Context */
  1365. ret = env->dmmu.mmu_secondary_context;
  1366. break;
  1367. case 3: /* SFSR */
  1368. ret = env->dmmu.sfsr;
  1369. break;
  1370. case 4: /* 0x20 SFAR */
  1371. ret = env->dmmu.sfar;
  1372. break;
  1373. case 5: /* 0x28 TSB access */
  1374. ret = env->dmmu.tsb;
  1375. break;
  1376. case 6: /* 0x30 D-TSB Tag Access register */
  1377. ret = env->dmmu.tag_access;
  1378. break;
  1379. case 7:
  1380. ret = env->dmmu.virtual_watchpoint;
  1381. break;
  1382. case 8:
  1383. ret = env->dmmu.physical_watchpoint;
  1384. break;
  1385. default:
  1386. sparc_raise_mmu_fault(cs, addr, false, false, 1, size, GETPC());
  1387. ret = 0;
  1388. }
  1389. break;
  1390. }
  1391. case ASI_DMMU_TSB_8KB_PTR: /* D-MMU 8k TSB pointer */
  1392. {
  1393. /* env->dmmuregs[5] holds D-MMU TSB register value
  1394. env->dmmuregs[6] holds D-MMU Tag Access register value */
  1395. ret = ultrasparc_tsb_pointer(env, &env->dmmu, 0);
  1396. break;
  1397. }
  1398. case ASI_DMMU_TSB_64KB_PTR: /* D-MMU 64k TSB pointer */
  1399. {
  1400. /* env->dmmuregs[5] holds D-MMU TSB register value
  1401. env->dmmuregs[6] holds D-MMU Tag Access register value */
  1402. ret = ultrasparc_tsb_pointer(env, &env->dmmu, 1);
  1403. break;
  1404. }
  1405. case ASI_DTLB_DATA_ACCESS: /* D-MMU data access */
  1406. {
  1407. int reg = (addr >> 3) & 0x3f;
  1408. ret = env->dtlb[reg].tte;
  1409. break;
  1410. }
  1411. case ASI_DTLB_TAG_READ: /* D-MMU tag read */
  1412. {
  1413. int reg = (addr >> 3) & 0x3f;
  1414. ret = env->dtlb[reg].tag;
  1415. break;
  1416. }
  1417. case ASI_INTR_DISPATCH_STAT: /* Interrupt dispatch, RO */
  1418. break;
  1419. case ASI_INTR_RECEIVE: /* Interrupt data receive */
  1420. ret = env->ivec_status;
  1421. break;
  1422. case ASI_INTR_R: /* Incoming interrupt vector, RO */
  1423. {
  1424. int reg = (addr >> 4) & 0x3;
  1425. if (reg < 3) {
  1426. ret = env->ivec_data[reg];
  1427. }
  1428. break;
  1429. }
  1430. case ASI_SCRATCHPAD: /* UA2005 privileged scratchpad */
  1431. if (unlikely((addr >= 0x20) && (addr < 0x30))) {
  1432. /* Hyperprivileged access only */
  1433. sparc_raise_mmu_fault(cs, addr, false, false, 1, size, GETPC());
  1434. }
  1435. /* fall through */
  1436. case ASI_HYP_SCRATCHPAD: /* UA2005 hyperprivileged scratchpad */
  1437. {
  1438. unsigned int i = (addr >> 3) & 0x7;
  1439. ret = env->scratch[i];
  1440. break;
  1441. }
  1442. case ASI_MMU: /* UA2005 Context ID registers */
  1443. switch ((addr >> 3) & 0x3) {
  1444. case 1:
  1445. ret = env->dmmu.mmu_primary_context;
  1446. break;
  1447. case 2:
  1448. ret = env->dmmu.mmu_secondary_context;
  1449. break;
  1450. default:
  1451. sparc_raise_mmu_fault(cs, addr, true, false, 1, size, GETPC());
  1452. }
  1453. break;
  1454. case ASI_DCACHE_DATA: /* D-cache data */
  1455. case ASI_DCACHE_TAG: /* D-cache tag access */
  1456. case ASI_ESTATE_ERROR_EN: /* E-cache error enable */
  1457. case ASI_AFSR: /* E-cache asynchronous fault status */
  1458. case ASI_AFAR: /* E-cache asynchronous fault address */
  1459. case ASI_EC_TAG_DATA: /* E-cache tag data */
  1460. case ASI_IC_INSTR: /* I-cache instruction access */
  1461. case ASI_IC_TAG: /* I-cache tag access */
  1462. case ASI_IC_PRE_DECODE: /* I-cache predecode */
  1463. case ASI_IC_NEXT_FIELD: /* I-cache LRU etc. */
  1464. case ASI_EC_W: /* E-cache tag */
  1465. case ASI_EC_R: /* E-cache tag */
  1466. break;
  1467. case ASI_DMMU_TSB_DIRECT_PTR: /* D-MMU data pointer */
  1468. case ASI_ITLB_DATA_IN: /* I-MMU data in, WO */
  1469. case ASI_IMMU_DEMAP: /* I-MMU demap, WO */
  1470. case ASI_DTLB_DATA_IN: /* D-MMU data in, WO */
  1471. case ASI_DMMU_DEMAP: /* D-MMU demap, WO */
  1472. case ASI_INTR_W: /* Interrupt vector, WO */
  1473. default:
  1474. sparc_raise_mmu_fault(cs, addr, false, false, 1, size, GETPC());
  1475. ret = 0;
  1476. break;
  1477. }
  1478. /* Convert to signed number */
  1479. if (sign) {
  1480. switch (size) {
  1481. case 1:
  1482. ret = (int8_t) ret;
  1483. break;
  1484. case 2:
  1485. ret = (int16_t) ret;
  1486. break;
  1487. case 4:
  1488. ret = (int32_t) ret;
  1489. break;
  1490. default:
  1491. break;
  1492. }
  1493. }
  1494. #ifdef DEBUG_ASI
  1495. dump_asi("read ", last_addr, asi, size, ret);
  1496. #endif
  1497. return ret;
  1498. }
  1499. void helper_st_asi(CPUSPARCState *env, target_ulong addr, target_ulong val,
  1500. int asi, uint32_t memop)
  1501. {
  1502. int size = 1 << (memop & MO_SIZE);
  1503. CPUState *cs = env_cpu(env);
  1504. #ifdef DEBUG_ASI
  1505. dump_asi("write", addr, asi, size, val);
  1506. #endif
  1507. asi &= 0xff;
  1508. do_check_asi(env, asi, GETPC());
  1509. do_check_align(env, addr, size - 1, GETPC());
  1510. addr = asi_address_mask(env, asi, addr);
  1511. switch (asi) {
  1512. case ASI_AIUP: /* As if user primary */
  1513. case ASI_AIUS: /* As if user secondary */
  1514. case ASI_AIUPL: /* As if user primary LE */
  1515. case ASI_AIUSL: /* As if user secondary LE */
  1516. case ASI_P: /* Primary */
  1517. case ASI_S: /* Secondary */
  1518. case ASI_PL: /* Primary LE */
  1519. case ASI_SL: /* Secondary LE */
  1520. case ASI_REAL: /* Bypass */
  1521. case ASI_REAL_IO: /* Bypass, non-cacheable */
  1522. case ASI_REAL_L: /* Bypass LE */
  1523. case ASI_REAL_IO_L: /* Bypass, non-cacheable LE */
  1524. case ASI_N: /* Nucleus */
  1525. case ASI_NL: /* Nucleus Little Endian (LE) */
  1526. case ASI_NUCLEUS_QUAD_LDD: /* Nucleus quad LDD 128 bit atomic */
  1527. case ASI_NUCLEUS_QUAD_LDD_L: /* Nucleus quad LDD 128 bit atomic LE */
  1528. case ASI_TWINX_AIUP: /* As if user primary, twinx */
  1529. case ASI_TWINX_AIUS: /* As if user secondary, twinx */
  1530. case ASI_TWINX_REAL: /* Real address, twinx */
  1531. case ASI_TWINX_AIUP_L: /* As if user primary, twinx, LE */
  1532. case ASI_TWINX_AIUS_L: /* As if user secondary, twinx, LE */
  1533. case ASI_TWINX_REAL_L: /* Real address, twinx, LE */
  1534. case ASI_TWINX_N: /* Nucleus, twinx */
  1535. case ASI_TWINX_NL: /* Nucleus, twinx, LE */
  1536. /* ??? From the UA2011 document; overlaps BLK_INIT_QUAD_LDD_* */
  1537. case ASI_TWINX_P: /* Primary, twinx */
  1538. case ASI_TWINX_PL: /* Primary, twinx, LE */
  1539. case ASI_TWINX_S: /* Secondary, twinx */
  1540. case ASI_TWINX_SL: /* Secondary, twinx, LE */
  1541. /* These are always handled inline. */
  1542. g_assert_not_reached();
  1543. /* these ASIs have different functions on UltraSPARC-IIIi
  1544. * and UA2005 CPUs. Use the explicit numbers to avoid confusion
  1545. */
  1546. case 0x31:
  1547. case 0x32:
  1548. case 0x39:
  1549. case 0x3a:
  1550. if (cpu_has_hypervisor(env)) {
  1551. /* UA2005
  1552. * ASI_DMMU_CTX_ZERO_TSB_BASE_PS0
  1553. * ASI_DMMU_CTX_ZERO_TSB_BASE_PS1
  1554. * ASI_DMMU_CTX_NONZERO_TSB_BASE_PS0
  1555. * ASI_DMMU_CTX_NONZERO_TSB_BASE_PS1
  1556. */
  1557. int idx = ((asi & 2) >> 1) | ((asi & 8) >> 2);
  1558. env->dmmu.sun4v_tsb_pointers[idx] = val;
  1559. } else {
  1560. goto illegal_insn;
  1561. }
  1562. break;
  1563. case 0x33:
  1564. case 0x3b:
  1565. if (cpu_has_hypervisor(env)) {
  1566. /* UA2005
  1567. * ASI_DMMU_CTX_ZERO_CONFIG
  1568. * ASI_DMMU_CTX_NONZERO_CONFIG
  1569. */
  1570. env->dmmu.sun4v_ctx_config[(asi & 8) >> 3] = val;
  1571. } else {
  1572. goto illegal_insn;
  1573. }
  1574. break;
  1575. case 0x35:
  1576. case 0x36:
  1577. case 0x3d:
  1578. case 0x3e:
  1579. if (cpu_has_hypervisor(env)) {
  1580. /* UA2005
  1581. * ASI_IMMU_CTX_ZERO_TSB_BASE_PS0
  1582. * ASI_IMMU_CTX_ZERO_TSB_BASE_PS1
  1583. * ASI_IMMU_CTX_NONZERO_TSB_BASE_PS0
  1584. * ASI_IMMU_CTX_NONZERO_TSB_BASE_PS1
  1585. */
  1586. int idx = ((asi & 2) >> 1) | ((asi & 8) >> 2);
  1587. env->immu.sun4v_tsb_pointers[idx] = val;
  1588. } else {
  1589. goto illegal_insn;
  1590. }
  1591. break;
  1592. case 0x37:
  1593. case 0x3f:
  1594. if (cpu_has_hypervisor(env)) {
  1595. /* UA2005
  1596. * ASI_IMMU_CTX_ZERO_CONFIG
  1597. * ASI_IMMU_CTX_NONZERO_CONFIG
  1598. */
  1599. env->immu.sun4v_ctx_config[(asi & 8) >> 3] = val;
  1600. } else {
  1601. goto illegal_insn;
  1602. }
  1603. break;
  1604. case ASI_UPA_CONFIG: /* UPA config */
  1605. /* XXX */
  1606. return;
  1607. case ASI_LSU_CONTROL: /* LSU */
  1608. env->lsu = val & (DMMU_E | IMMU_E);
  1609. return;
  1610. case ASI_IMMU: /* I-MMU regs */
  1611. {
  1612. int reg = (addr >> 3) & 0xf;
  1613. uint64_t oldreg;
  1614. oldreg = env->immu.mmuregs[reg];
  1615. switch (reg) {
  1616. case 0: /* RO */
  1617. return;
  1618. case 1: /* Not in I-MMU */
  1619. case 2:
  1620. return;
  1621. case 3: /* SFSR */
  1622. if ((val & 1) == 0) {
  1623. val = 0; /* Clear SFSR */
  1624. }
  1625. env->immu.sfsr = val;
  1626. break;
  1627. case 4: /* RO */
  1628. return;
  1629. case 5: /* TSB access */
  1630. DPRINTF_MMU("immu TSB write: 0x%016" PRIx64 " -> 0x%016"
  1631. PRIx64 "\n", env->immu.tsb, val);
  1632. env->immu.tsb = val;
  1633. break;
  1634. case 6: /* Tag access */
  1635. env->immu.tag_access = val;
  1636. break;
  1637. case 7:
  1638. case 8:
  1639. return;
  1640. default:
  1641. sparc_raise_mmu_fault(cs, addr, true, false, 1, size, GETPC());
  1642. break;
  1643. }
  1644. if (oldreg != env->immu.mmuregs[reg]) {
  1645. DPRINTF_MMU("immu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
  1646. PRIx64 "\n", reg, oldreg, env->immuregs[reg]);
  1647. }
  1648. #ifdef DEBUG_MMU
  1649. dump_mmu(env);
  1650. #endif
  1651. return;
  1652. }
  1653. case ASI_ITLB_DATA_IN: /* I-MMU data in */
  1654. /* ignore real translation entries */
  1655. if (!(addr & TLB_UST1_IS_REAL_BIT)) {
  1656. replace_tlb_1bit_lru(env->itlb, env->immu.tag_access,
  1657. val, "immu", env, addr);
  1658. }
  1659. return;
  1660. case ASI_ITLB_DATA_ACCESS: /* I-MMU data access */
  1661. {
  1662. /* TODO: auto demap */
  1663. unsigned int i = (addr >> 3) & 0x3f;
  1664. /* ignore real translation entries */
  1665. if (!(addr & TLB_UST1_IS_REAL_BIT)) {
  1666. replace_tlb_entry(&env->itlb[i], env->immu.tag_access,
  1667. sun4v_tte_to_sun4u(env, addr, val), env);
  1668. }
  1669. #ifdef DEBUG_MMU
  1670. DPRINTF_MMU("immu data access replaced entry [%i]\n", i);
  1671. dump_mmu(env);
  1672. #endif
  1673. return;
  1674. }
  1675. case ASI_IMMU_DEMAP: /* I-MMU demap */
  1676. demap_tlb(env->itlb, addr, "immu", env);
  1677. return;
  1678. case ASI_DMMU: /* D-MMU regs */
  1679. {
  1680. int reg = (addr >> 3) & 0xf;
  1681. uint64_t oldreg;
  1682. oldreg = env->dmmu.mmuregs[reg];
  1683. switch (reg) {
  1684. case 0: /* RO */
  1685. case 4:
  1686. return;
  1687. case 3: /* SFSR */
  1688. if ((val & 1) == 0) {
  1689. val = 0; /* Clear SFSR, Fault address */
  1690. env->dmmu.sfar = 0;
  1691. }
  1692. env->dmmu.sfsr = val;
  1693. break;
  1694. case 1: /* Primary context */
  1695. env->dmmu.mmu_primary_context = val;
  1696. /* can be optimized to only flush MMU_USER_IDX
  1697. and MMU_KERNEL_IDX entries */
  1698. tlb_flush(cs);
  1699. break;
  1700. case 2: /* Secondary context */
  1701. env->dmmu.mmu_secondary_context = val;
  1702. /* can be optimized to only flush MMU_USER_SECONDARY_IDX
  1703. and MMU_KERNEL_SECONDARY_IDX entries */
  1704. tlb_flush(cs);
  1705. break;
  1706. case 5: /* TSB access */
  1707. DPRINTF_MMU("dmmu TSB write: 0x%016" PRIx64 " -> 0x%016"
  1708. PRIx64 "\n", env->dmmu.tsb, val);
  1709. env->dmmu.tsb = val;
  1710. break;
  1711. case 6: /* Tag access */
  1712. env->dmmu.tag_access = val;
  1713. break;
  1714. case 7: /* Virtual Watchpoint */
  1715. env->dmmu.virtual_watchpoint = val;
  1716. break;
  1717. case 8: /* Physical Watchpoint */
  1718. env->dmmu.physical_watchpoint = val;
  1719. break;
  1720. default:
  1721. sparc_raise_mmu_fault(cs, addr, true, false, 1, size, GETPC());
  1722. break;
  1723. }
  1724. if (oldreg != env->dmmu.mmuregs[reg]) {
  1725. DPRINTF_MMU("dmmu change reg[%d]: 0x%016" PRIx64 " -> 0x%016"
  1726. PRIx64 "\n", reg, oldreg, env->dmmuregs[reg]);
  1727. }
  1728. #ifdef DEBUG_MMU
  1729. dump_mmu(env);
  1730. #endif
  1731. return;
  1732. }
  1733. case ASI_DTLB_DATA_IN: /* D-MMU data in */
  1734. /* ignore real translation entries */
  1735. if (!(addr & TLB_UST1_IS_REAL_BIT)) {
  1736. replace_tlb_1bit_lru(env->dtlb, env->dmmu.tag_access,
  1737. val, "dmmu", env, addr);
  1738. }
  1739. return;
  1740. case ASI_DTLB_DATA_ACCESS: /* D-MMU data access */
  1741. {
  1742. unsigned int i = (addr >> 3) & 0x3f;
  1743. /* ignore real translation entries */
  1744. if (!(addr & TLB_UST1_IS_REAL_BIT)) {
  1745. replace_tlb_entry(&env->dtlb[i], env->dmmu.tag_access,
  1746. sun4v_tte_to_sun4u(env, addr, val), env);
  1747. }
  1748. #ifdef DEBUG_MMU
  1749. DPRINTF_MMU("dmmu data access replaced entry [%i]\n", i);
  1750. dump_mmu(env);
  1751. #endif
  1752. return;
  1753. }
  1754. case ASI_DMMU_DEMAP: /* D-MMU demap */
  1755. demap_tlb(env->dtlb, addr, "dmmu", env);
  1756. return;
  1757. case ASI_INTR_RECEIVE: /* Interrupt data receive */
  1758. env->ivec_status = val & 0x20;
  1759. return;
  1760. case ASI_SCRATCHPAD: /* UA2005 privileged scratchpad */
  1761. if (unlikely((addr >= 0x20) && (addr < 0x30))) {
  1762. /* Hyperprivileged access only */
  1763. sparc_raise_mmu_fault(cs, addr, true, false, 1, size, GETPC());
  1764. }
  1765. /* fall through */
  1766. case ASI_HYP_SCRATCHPAD: /* UA2005 hyperprivileged scratchpad */
  1767. {
  1768. unsigned int i = (addr >> 3) & 0x7;
  1769. env->scratch[i] = val;
  1770. return;
  1771. }
  1772. case ASI_MMU: /* UA2005 Context ID registers */
  1773. {
  1774. switch ((addr >> 3) & 0x3) {
  1775. case 1:
  1776. env->dmmu.mmu_primary_context = val;
  1777. env->immu.mmu_primary_context = val;
  1778. tlb_flush_by_mmuidx(cs,
  1779. (1 << MMU_USER_IDX) | (1 << MMU_KERNEL_IDX));
  1780. break;
  1781. case 2:
  1782. env->dmmu.mmu_secondary_context = val;
  1783. env->immu.mmu_secondary_context = val;
  1784. tlb_flush_by_mmuidx(cs,
  1785. (1 << MMU_USER_SECONDARY_IDX) |
  1786. (1 << MMU_KERNEL_SECONDARY_IDX));
  1787. break;
  1788. default:
  1789. sparc_raise_mmu_fault(cs, addr, true, false, 1, size, GETPC());
  1790. }
  1791. }
  1792. return;
  1793. case ASI_QUEUE: /* UA2005 CPU mondo queue */
  1794. case ASI_DCACHE_DATA: /* D-cache data */
  1795. case ASI_DCACHE_TAG: /* D-cache tag access */
  1796. case ASI_ESTATE_ERROR_EN: /* E-cache error enable */
  1797. case ASI_AFSR: /* E-cache asynchronous fault status */
  1798. case ASI_AFAR: /* E-cache asynchronous fault address */
  1799. case ASI_EC_TAG_DATA: /* E-cache tag data */
  1800. case ASI_IC_INSTR: /* I-cache instruction access */
  1801. case ASI_IC_TAG: /* I-cache tag access */
  1802. case ASI_IC_PRE_DECODE: /* I-cache predecode */
  1803. case ASI_IC_NEXT_FIELD: /* I-cache LRU etc. */
  1804. case ASI_EC_W: /* E-cache tag */
  1805. case ASI_EC_R: /* E-cache tag */
  1806. return;
  1807. case ASI_IMMU_TSB_8KB_PTR: /* I-MMU 8k TSB pointer, RO */
  1808. case ASI_IMMU_TSB_64KB_PTR: /* I-MMU 64k TSB pointer, RO */
  1809. case ASI_ITLB_TAG_READ: /* I-MMU tag read, RO */
  1810. case ASI_DMMU_TSB_8KB_PTR: /* D-MMU 8k TSB pointer, RO */
  1811. case ASI_DMMU_TSB_64KB_PTR: /* D-MMU 64k TSB pointer, RO */
  1812. case ASI_DMMU_TSB_DIRECT_PTR: /* D-MMU data pointer, RO */
  1813. case ASI_DTLB_TAG_READ: /* D-MMU tag read, RO */
  1814. case ASI_INTR_DISPATCH_STAT: /* Interrupt dispatch, RO */
  1815. case ASI_INTR_R: /* Incoming interrupt vector, RO */
  1816. case ASI_PNF: /* Primary no-fault, RO */
  1817. case ASI_SNF: /* Secondary no-fault, RO */
  1818. case ASI_PNFL: /* Primary no-fault LE, RO */
  1819. case ASI_SNFL: /* Secondary no-fault LE, RO */
  1820. default:
  1821. sparc_raise_mmu_fault(cs, addr, true, false, 1, size, GETPC());
  1822. return;
  1823. illegal_insn:
  1824. cpu_raise_exception_ra(env, TT_ILL_INSN, GETPC());
  1825. }
  1826. }
  1827. #endif /* CONFIG_USER_ONLY */
  1828. #endif /* TARGET_SPARC64 */
  1829. #if !defined(CONFIG_USER_ONLY)
  1830. void sparc_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
  1831. vaddr addr, unsigned size,
  1832. MMUAccessType access_type,
  1833. int mmu_idx, MemTxAttrs attrs,
  1834. MemTxResult response, uintptr_t retaddr)
  1835. {
  1836. bool is_write = access_type == MMU_DATA_STORE;
  1837. bool is_exec = access_type == MMU_INST_FETCH;
  1838. bool is_asi = false;
  1839. sparc_raise_mmu_fault(cs, physaddr, is_write, is_exec,
  1840. is_asi, size, retaddr);
  1841. }
  1842. #endif