2
0

cpu.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * Sparc CPU init helpers
  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 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 "cpu.h"
  20. #include "qemu/error-report.h"
  21. //#define DEBUG_FEATURES
  22. static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *cpu_model);
  23. /* CPUClass::reset() */
  24. static void sparc_cpu_reset(CPUState *s)
  25. {
  26. SPARCCPU *cpu = SPARC_CPU(s);
  27. SPARCCPUClass *scc = SPARC_CPU_GET_CLASS(cpu);
  28. CPUSPARCState *env = &cpu->env;
  29. scc->parent_reset(s);
  30. memset(env, 0, offsetof(CPUSPARCState, version));
  31. tlb_flush(s, 1);
  32. env->cwp = 0;
  33. #ifndef TARGET_SPARC64
  34. env->wim = 1;
  35. #endif
  36. env->regwptr = env->regbase + (env->cwp * 16);
  37. CC_OP = CC_OP_FLAGS;
  38. #if defined(CONFIG_USER_ONLY)
  39. #ifdef TARGET_SPARC64
  40. env->cleanwin = env->nwindows - 2;
  41. env->cansave = env->nwindows - 2;
  42. env->pstate = PS_RMO | PS_PEF | PS_IE;
  43. env->asi = 0x82; /* Primary no-fault */
  44. #endif
  45. #else
  46. #if !defined(TARGET_SPARC64)
  47. env->psret = 0;
  48. env->psrs = 1;
  49. env->psrps = 1;
  50. #endif
  51. #ifdef TARGET_SPARC64
  52. env->pstate = PS_PRIV|PS_RED|PS_PEF|PS_AG;
  53. env->hpstate = cpu_has_hypervisor(env) ? HS_PRIV : 0;
  54. env->tl = env->maxtl;
  55. cpu_tsptr(env)->tt = TT_POWER_ON_RESET;
  56. env->lsu = 0;
  57. #else
  58. env->mmuregs[0] &= ~(MMU_E | MMU_NF);
  59. env->mmuregs[0] |= env->def->mmu_bm;
  60. #endif
  61. env->pc = 0;
  62. env->npc = env->pc + 4;
  63. #endif
  64. env->cache_control = 0;
  65. }
  66. static bool sparc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
  67. {
  68. if (interrupt_request & CPU_INTERRUPT_HARD) {
  69. SPARCCPU *cpu = SPARC_CPU(cs);
  70. CPUSPARCState *env = &cpu->env;
  71. if (cpu_interrupts_enabled(env) && env->interrupt_index > 0) {
  72. int pil = env->interrupt_index & 0xf;
  73. int type = env->interrupt_index & 0xf0;
  74. if (type != TT_EXTINT || cpu_pil_allowed(env, pil)) {
  75. cs->exception_index = env->interrupt_index;
  76. sparc_cpu_do_interrupt(cs);
  77. return true;
  78. }
  79. }
  80. }
  81. return false;
  82. }
  83. static int cpu_sparc_register(SPARCCPU *cpu, const char *cpu_model)
  84. {
  85. CPUClass *cc = CPU_GET_CLASS(cpu);
  86. CPUSPARCState *env = &cpu->env;
  87. char *s = g_strdup(cpu_model);
  88. char *featurestr, *name = strtok(s, ",");
  89. sparc_def_t def1, *def = &def1;
  90. Error *err = NULL;
  91. if (cpu_sparc_find_by_name(def, name) < 0) {
  92. g_free(s);
  93. return -1;
  94. }
  95. env->def = g_new0(sparc_def_t, 1);
  96. memcpy(env->def, def, sizeof(*def));
  97. featurestr = strtok(NULL, ",");
  98. cc->parse_features(CPU(cpu), featurestr, &err);
  99. g_free(s);
  100. if (err) {
  101. error_report("%s", error_get_pretty(err));
  102. error_free(err);
  103. return -1;
  104. }
  105. env->version = def->iu_version;
  106. env->fsr = def->fpu_version;
  107. env->nwindows = def->nwindows;
  108. #if !defined(TARGET_SPARC64)
  109. env->mmuregs[0] |= def->mmu_version;
  110. cpu_sparc_set_id(env, 0);
  111. env->mxccregs[7] |= def->mxcc_version;
  112. #else
  113. env->mmu_version = def->mmu_version;
  114. env->maxtl = def->maxtl;
  115. env->version |= def->maxtl << 8;
  116. env->version |= def->nwindows - 1;
  117. #endif
  118. return 0;
  119. }
  120. SPARCCPU *cpu_sparc_init(const char *cpu_model)
  121. {
  122. SPARCCPU *cpu;
  123. cpu = SPARC_CPU(object_new(TYPE_SPARC_CPU));
  124. if (cpu_sparc_register(cpu, cpu_model) < 0) {
  125. object_unref(OBJECT(cpu));
  126. return NULL;
  127. }
  128. object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
  129. return cpu;
  130. }
  131. void cpu_sparc_set_id(CPUSPARCState *env, unsigned int cpu)
  132. {
  133. #if !defined(TARGET_SPARC64)
  134. env->mxccregs[7] = ((cpu + 8) & 0xf) << 24;
  135. #endif
  136. }
  137. static const sparc_def_t sparc_defs[] = {
  138. #ifdef TARGET_SPARC64
  139. {
  140. .name = "Fujitsu Sparc64",
  141. .iu_version = ((0x04ULL << 48) | (0x02ULL << 32) | (0ULL << 24)),
  142. .fpu_version = 0x00000000,
  143. .mmu_version = mmu_us_12,
  144. .nwindows = 4,
  145. .maxtl = 4,
  146. .features = CPU_DEFAULT_FEATURES,
  147. },
  148. {
  149. .name = "Fujitsu Sparc64 III",
  150. .iu_version = ((0x04ULL << 48) | (0x03ULL << 32) | (0ULL << 24)),
  151. .fpu_version = 0x00000000,
  152. .mmu_version = mmu_us_12,
  153. .nwindows = 5,
  154. .maxtl = 4,
  155. .features = CPU_DEFAULT_FEATURES,
  156. },
  157. {
  158. .name = "Fujitsu Sparc64 IV",
  159. .iu_version = ((0x04ULL << 48) | (0x04ULL << 32) | (0ULL << 24)),
  160. .fpu_version = 0x00000000,
  161. .mmu_version = mmu_us_12,
  162. .nwindows = 8,
  163. .maxtl = 5,
  164. .features = CPU_DEFAULT_FEATURES,
  165. },
  166. {
  167. .name = "Fujitsu Sparc64 V",
  168. .iu_version = ((0x04ULL << 48) | (0x05ULL << 32) | (0x51ULL << 24)),
  169. .fpu_version = 0x00000000,
  170. .mmu_version = mmu_us_12,
  171. .nwindows = 8,
  172. .maxtl = 5,
  173. .features = CPU_DEFAULT_FEATURES,
  174. },
  175. {
  176. .name = "TI UltraSparc I",
  177. .iu_version = ((0x17ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
  178. .fpu_version = 0x00000000,
  179. .mmu_version = mmu_us_12,
  180. .nwindows = 8,
  181. .maxtl = 5,
  182. .features = CPU_DEFAULT_FEATURES,
  183. },
  184. {
  185. .name = "TI UltraSparc II",
  186. .iu_version = ((0x17ULL << 48) | (0x11ULL << 32) | (0x20ULL << 24)),
  187. .fpu_version = 0x00000000,
  188. .mmu_version = mmu_us_12,
  189. .nwindows = 8,
  190. .maxtl = 5,
  191. .features = CPU_DEFAULT_FEATURES,
  192. },
  193. {
  194. .name = "TI UltraSparc IIi",
  195. .iu_version = ((0x17ULL << 48) | (0x12ULL << 32) | (0x91ULL << 24)),
  196. .fpu_version = 0x00000000,
  197. .mmu_version = mmu_us_12,
  198. .nwindows = 8,
  199. .maxtl = 5,
  200. .features = CPU_DEFAULT_FEATURES,
  201. },
  202. {
  203. .name = "TI UltraSparc IIe",
  204. .iu_version = ((0x17ULL << 48) | (0x13ULL << 32) | (0x14ULL << 24)),
  205. .fpu_version = 0x00000000,
  206. .mmu_version = mmu_us_12,
  207. .nwindows = 8,
  208. .maxtl = 5,
  209. .features = CPU_DEFAULT_FEATURES,
  210. },
  211. {
  212. .name = "Sun UltraSparc III",
  213. .iu_version = ((0x3eULL << 48) | (0x14ULL << 32) | (0x34ULL << 24)),
  214. .fpu_version = 0x00000000,
  215. .mmu_version = mmu_us_12,
  216. .nwindows = 8,
  217. .maxtl = 5,
  218. .features = CPU_DEFAULT_FEATURES,
  219. },
  220. {
  221. .name = "Sun UltraSparc III Cu",
  222. .iu_version = ((0x3eULL << 48) | (0x15ULL << 32) | (0x41ULL << 24)),
  223. .fpu_version = 0x00000000,
  224. .mmu_version = mmu_us_3,
  225. .nwindows = 8,
  226. .maxtl = 5,
  227. .features = CPU_DEFAULT_FEATURES,
  228. },
  229. {
  230. .name = "Sun UltraSparc IIIi",
  231. .iu_version = ((0x3eULL << 48) | (0x16ULL << 32) | (0x34ULL << 24)),
  232. .fpu_version = 0x00000000,
  233. .mmu_version = mmu_us_12,
  234. .nwindows = 8,
  235. .maxtl = 5,
  236. .features = CPU_DEFAULT_FEATURES,
  237. },
  238. {
  239. .name = "Sun UltraSparc IV",
  240. .iu_version = ((0x3eULL << 48) | (0x18ULL << 32) | (0x31ULL << 24)),
  241. .fpu_version = 0x00000000,
  242. .mmu_version = mmu_us_4,
  243. .nwindows = 8,
  244. .maxtl = 5,
  245. .features = CPU_DEFAULT_FEATURES,
  246. },
  247. {
  248. .name = "Sun UltraSparc IV+",
  249. .iu_version = ((0x3eULL << 48) | (0x19ULL << 32) | (0x22ULL << 24)),
  250. .fpu_version = 0x00000000,
  251. .mmu_version = mmu_us_12,
  252. .nwindows = 8,
  253. .maxtl = 5,
  254. .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_CMT,
  255. },
  256. {
  257. .name = "Sun UltraSparc IIIi+",
  258. .iu_version = ((0x3eULL << 48) | (0x22ULL << 32) | (0ULL << 24)),
  259. .fpu_version = 0x00000000,
  260. .mmu_version = mmu_us_3,
  261. .nwindows = 8,
  262. .maxtl = 5,
  263. .features = CPU_DEFAULT_FEATURES,
  264. },
  265. {
  266. .name = "Sun UltraSparc T1",
  267. /* defined in sparc_ifu_fdp.v and ctu.h */
  268. .iu_version = ((0x3eULL << 48) | (0x23ULL << 32) | (0x02ULL << 24)),
  269. .fpu_version = 0x00000000,
  270. .mmu_version = mmu_sun4v,
  271. .nwindows = 8,
  272. .maxtl = 6,
  273. .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
  274. | CPU_FEATURE_GL,
  275. },
  276. {
  277. .name = "Sun UltraSparc T2",
  278. /* defined in tlu_asi_ctl.v and n2_revid_cust.v */
  279. .iu_version = ((0x3eULL << 48) | (0x24ULL << 32) | (0x02ULL << 24)),
  280. .fpu_version = 0x00000000,
  281. .mmu_version = mmu_sun4v,
  282. .nwindows = 8,
  283. .maxtl = 6,
  284. .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_HYPV | CPU_FEATURE_CMT
  285. | CPU_FEATURE_GL,
  286. },
  287. {
  288. .name = "NEC UltraSparc I",
  289. .iu_version = ((0x22ULL << 48) | (0x10ULL << 32) | (0x40ULL << 24)),
  290. .fpu_version = 0x00000000,
  291. .mmu_version = mmu_us_12,
  292. .nwindows = 8,
  293. .maxtl = 5,
  294. .features = CPU_DEFAULT_FEATURES,
  295. },
  296. #else
  297. {
  298. .name = "Fujitsu MB86904",
  299. .iu_version = 0x04 << 24, /* Impl 0, ver 4 */
  300. .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
  301. .mmu_version = 0x04 << 24, /* Impl 0, ver 4 */
  302. .mmu_bm = 0x00004000,
  303. .mmu_ctpr_mask = 0x00ffffc0,
  304. .mmu_cxr_mask = 0x000000ff,
  305. .mmu_sfsr_mask = 0x00016fff,
  306. .mmu_trcr_mask = 0x00ffffff,
  307. .nwindows = 8,
  308. .features = CPU_DEFAULT_FEATURES,
  309. },
  310. {
  311. .name = "Fujitsu MB86907",
  312. .iu_version = 0x05 << 24, /* Impl 0, ver 5 */
  313. .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
  314. .mmu_version = 0x05 << 24, /* Impl 0, ver 5 */
  315. .mmu_bm = 0x00004000,
  316. .mmu_ctpr_mask = 0xffffffc0,
  317. .mmu_cxr_mask = 0x000000ff,
  318. .mmu_sfsr_mask = 0x00016fff,
  319. .mmu_trcr_mask = 0xffffffff,
  320. .nwindows = 8,
  321. .features = CPU_DEFAULT_FEATURES,
  322. },
  323. {
  324. .name = "TI MicroSparc I",
  325. .iu_version = 0x41000000,
  326. .fpu_version = 4 << 17,
  327. .mmu_version = 0x41000000,
  328. .mmu_bm = 0x00004000,
  329. .mmu_ctpr_mask = 0x007ffff0,
  330. .mmu_cxr_mask = 0x0000003f,
  331. .mmu_sfsr_mask = 0x00016fff,
  332. .mmu_trcr_mask = 0x0000003f,
  333. .nwindows = 7,
  334. .features = CPU_FEATURE_FLOAT | CPU_FEATURE_SWAP | CPU_FEATURE_MUL |
  335. CPU_FEATURE_DIV | CPU_FEATURE_FLUSH | CPU_FEATURE_FSQRT |
  336. CPU_FEATURE_FMUL,
  337. },
  338. {
  339. .name = "TI MicroSparc II",
  340. .iu_version = 0x42000000,
  341. .fpu_version = 4 << 17,
  342. .mmu_version = 0x02000000,
  343. .mmu_bm = 0x00004000,
  344. .mmu_ctpr_mask = 0x00ffffc0,
  345. .mmu_cxr_mask = 0x000000ff,
  346. .mmu_sfsr_mask = 0x00016fff,
  347. .mmu_trcr_mask = 0x00ffffff,
  348. .nwindows = 8,
  349. .features = CPU_DEFAULT_FEATURES,
  350. },
  351. {
  352. .name = "TI MicroSparc IIep",
  353. .iu_version = 0x42000000,
  354. .fpu_version = 4 << 17,
  355. .mmu_version = 0x04000000,
  356. .mmu_bm = 0x00004000,
  357. .mmu_ctpr_mask = 0x00ffffc0,
  358. .mmu_cxr_mask = 0x000000ff,
  359. .mmu_sfsr_mask = 0x00016bff,
  360. .mmu_trcr_mask = 0x00ffffff,
  361. .nwindows = 8,
  362. .features = CPU_DEFAULT_FEATURES,
  363. },
  364. {
  365. .name = "TI SuperSparc 40", /* STP1020NPGA */
  366. .iu_version = 0x41000000, /* SuperSPARC 2.x */
  367. .fpu_version = 0 << 17,
  368. .mmu_version = 0x00000800, /* SuperSPARC 2.x, no MXCC */
  369. .mmu_bm = 0x00002000,
  370. .mmu_ctpr_mask = 0xffffffc0,
  371. .mmu_cxr_mask = 0x0000ffff,
  372. .mmu_sfsr_mask = 0xffffffff,
  373. .mmu_trcr_mask = 0xffffffff,
  374. .nwindows = 8,
  375. .features = CPU_DEFAULT_FEATURES,
  376. },
  377. {
  378. .name = "TI SuperSparc 50", /* STP1020PGA */
  379. .iu_version = 0x40000000, /* SuperSPARC 3.x */
  380. .fpu_version = 0 << 17,
  381. .mmu_version = 0x01000800, /* SuperSPARC 3.x, no MXCC */
  382. .mmu_bm = 0x00002000,
  383. .mmu_ctpr_mask = 0xffffffc0,
  384. .mmu_cxr_mask = 0x0000ffff,
  385. .mmu_sfsr_mask = 0xffffffff,
  386. .mmu_trcr_mask = 0xffffffff,
  387. .nwindows = 8,
  388. .features = CPU_DEFAULT_FEATURES,
  389. },
  390. {
  391. .name = "TI SuperSparc 51",
  392. .iu_version = 0x40000000, /* SuperSPARC 3.x */
  393. .fpu_version = 0 << 17,
  394. .mmu_version = 0x01000000, /* SuperSPARC 3.x, MXCC */
  395. .mmu_bm = 0x00002000,
  396. .mmu_ctpr_mask = 0xffffffc0,
  397. .mmu_cxr_mask = 0x0000ffff,
  398. .mmu_sfsr_mask = 0xffffffff,
  399. .mmu_trcr_mask = 0xffffffff,
  400. .mxcc_version = 0x00000104,
  401. .nwindows = 8,
  402. .features = CPU_DEFAULT_FEATURES,
  403. },
  404. {
  405. .name = "TI SuperSparc 60", /* STP1020APGA */
  406. .iu_version = 0x40000000, /* SuperSPARC 3.x */
  407. .fpu_version = 0 << 17,
  408. .mmu_version = 0x01000800, /* SuperSPARC 3.x, no MXCC */
  409. .mmu_bm = 0x00002000,
  410. .mmu_ctpr_mask = 0xffffffc0,
  411. .mmu_cxr_mask = 0x0000ffff,
  412. .mmu_sfsr_mask = 0xffffffff,
  413. .mmu_trcr_mask = 0xffffffff,
  414. .nwindows = 8,
  415. .features = CPU_DEFAULT_FEATURES,
  416. },
  417. {
  418. .name = "TI SuperSparc 61",
  419. .iu_version = 0x44000000, /* SuperSPARC 3.x */
  420. .fpu_version = 0 << 17,
  421. .mmu_version = 0x01000000, /* SuperSPARC 3.x, MXCC */
  422. .mmu_bm = 0x00002000,
  423. .mmu_ctpr_mask = 0xffffffc0,
  424. .mmu_cxr_mask = 0x0000ffff,
  425. .mmu_sfsr_mask = 0xffffffff,
  426. .mmu_trcr_mask = 0xffffffff,
  427. .mxcc_version = 0x00000104,
  428. .nwindows = 8,
  429. .features = CPU_DEFAULT_FEATURES,
  430. },
  431. {
  432. .name = "TI SuperSparc II",
  433. .iu_version = 0x40000000, /* SuperSPARC II 1.x */
  434. .fpu_version = 0 << 17,
  435. .mmu_version = 0x08000000, /* SuperSPARC II 1.x, MXCC */
  436. .mmu_bm = 0x00002000,
  437. .mmu_ctpr_mask = 0xffffffc0,
  438. .mmu_cxr_mask = 0x0000ffff,
  439. .mmu_sfsr_mask = 0xffffffff,
  440. .mmu_trcr_mask = 0xffffffff,
  441. .mxcc_version = 0x00000104,
  442. .nwindows = 8,
  443. .features = CPU_DEFAULT_FEATURES,
  444. },
  445. {
  446. .name = "LEON2",
  447. .iu_version = 0xf2000000,
  448. .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
  449. .mmu_version = 0xf2000000,
  450. .mmu_bm = 0x00004000,
  451. .mmu_ctpr_mask = 0x007ffff0,
  452. .mmu_cxr_mask = 0x0000003f,
  453. .mmu_sfsr_mask = 0xffffffff,
  454. .mmu_trcr_mask = 0xffffffff,
  455. .nwindows = 8,
  456. .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_TA0_SHUTDOWN,
  457. },
  458. {
  459. .name = "LEON3",
  460. .iu_version = 0xf3000000,
  461. .fpu_version = 4 << 17, /* FPU version 4 (Meiko) */
  462. .mmu_version = 0xf3000000,
  463. .mmu_bm = 0x00000000,
  464. .mmu_ctpr_mask = 0xfffffffc,
  465. .mmu_cxr_mask = 0x000000ff,
  466. .mmu_sfsr_mask = 0xffffffff,
  467. .mmu_trcr_mask = 0xffffffff,
  468. .nwindows = 8,
  469. .features = CPU_DEFAULT_FEATURES | CPU_FEATURE_TA0_SHUTDOWN |
  470. CPU_FEATURE_ASR17 | CPU_FEATURE_CACHE_CTRL | CPU_FEATURE_POWERDOWN |
  471. CPU_FEATURE_CASA,
  472. },
  473. #endif
  474. };
  475. static const char * const feature_name[] = {
  476. "float",
  477. "float128",
  478. "swap",
  479. "mul",
  480. "div",
  481. "flush",
  482. "fsqrt",
  483. "fmul",
  484. "vis1",
  485. "vis2",
  486. "fsmuld",
  487. "hypv",
  488. "cmt",
  489. "gl",
  490. };
  491. static void print_features(FILE *f, fprintf_function cpu_fprintf,
  492. uint32_t features, const char *prefix)
  493. {
  494. unsigned int i;
  495. for (i = 0; i < ARRAY_SIZE(feature_name); i++) {
  496. if (feature_name[i] && (features & (1 << i))) {
  497. if (prefix) {
  498. (*cpu_fprintf)(f, "%s", prefix);
  499. }
  500. (*cpu_fprintf)(f, "%s ", feature_name[i]);
  501. }
  502. }
  503. }
  504. static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features)
  505. {
  506. unsigned int i;
  507. for (i = 0; i < ARRAY_SIZE(feature_name); i++) {
  508. if (feature_name[i] && !strcmp(flagname, feature_name[i])) {
  509. *features |= 1 << i;
  510. return;
  511. }
  512. }
  513. error_report("CPU feature %s not found", flagname);
  514. }
  515. static int cpu_sparc_find_by_name(sparc_def_t *cpu_def, const char *name)
  516. {
  517. unsigned int i;
  518. const sparc_def_t *def = NULL;
  519. for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
  520. if (strcasecmp(name, sparc_defs[i].name) == 0) {
  521. def = &sparc_defs[i];
  522. }
  523. }
  524. if (!def) {
  525. return -1;
  526. }
  527. memcpy(cpu_def, def, sizeof(*def));
  528. return 0;
  529. }
  530. static void sparc_cpu_parse_features(CPUState *cs, char *features,
  531. Error **errp)
  532. {
  533. SPARCCPU *cpu = SPARC_CPU(cs);
  534. sparc_def_t *cpu_def = cpu->env.def;
  535. char *featurestr;
  536. uint32_t plus_features = 0;
  537. uint32_t minus_features = 0;
  538. uint64_t iu_version;
  539. uint32_t fpu_version, mmu_version, nwindows;
  540. featurestr = features ? strtok(features, ",") : NULL;
  541. while (featurestr) {
  542. char *val;
  543. if (featurestr[0] == '+') {
  544. add_flagname_to_bitmaps(featurestr + 1, &plus_features);
  545. } else if (featurestr[0] == '-') {
  546. add_flagname_to_bitmaps(featurestr + 1, &minus_features);
  547. } else if ((val = strchr(featurestr, '='))) {
  548. *val = 0; val++;
  549. if (!strcmp(featurestr, "iu_version")) {
  550. char *err;
  551. iu_version = strtoll(val, &err, 0);
  552. if (!*val || *err) {
  553. error_setg(errp, "bad numerical value %s", val);
  554. return;
  555. }
  556. cpu_def->iu_version = iu_version;
  557. #ifdef DEBUG_FEATURES
  558. fprintf(stderr, "iu_version %" PRIx64 "\n", iu_version);
  559. #endif
  560. } else if (!strcmp(featurestr, "fpu_version")) {
  561. char *err;
  562. fpu_version = strtol(val, &err, 0);
  563. if (!*val || *err) {
  564. error_setg(errp, "bad numerical value %s", val);
  565. return;
  566. }
  567. cpu_def->fpu_version = fpu_version;
  568. #ifdef DEBUG_FEATURES
  569. fprintf(stderr, "fpu_version %x\n", fpu_version);
  570. #endif
  571. } else if (!strcmp(featurestr, "mmu_version")) {
  572. char *err;
  573. mmu_version = strtol(val, &err, 0);
  574. if (!*val || *err) {
  575. error_setg(errp, "bad numerical value %s", val);
  576. return;
  577. }
  578. cpu_def->mmu_version = mmu_version;
  579. #ifdef DEBUG_FEATURES
  580. fprintf(stderr, "mmu_version %x\n", mmu_version);
  581. #endif
  582. } else if (!strcmp(featurestr, "nwindows")) {
  583. char *err;
  584. nwindows = strtol(val, &err, 0);
  585. if (!*val || *err || nwindows > MAX_NWINDOWS ||
  586. nwindows < MIN_NWINDOWS) {
  587. error_setg(errp, "bad numerical value %s", val);
  588. return;
  589. }
  590. cpu_def->nwindows = nwindows;
  591. #ifdef DEBUG_FEATURES
  592. fprintf(stderr, "nwindows %d\n", nwindows);
  593. #endif
  594. } else {
  595. error_setg(errp, "unrecognized feature %s", featurestr);
  596. return;
  597. }
  598. } else {
  599. error_setg(errp, "feature string `%s' not in format "
  600. "(+feature|-feature|feature=xyz)", featurestr);
  601. return;
  602. }
  603. featurestr = strtok(NULL, ",");
  604. }
  605. cpu_def->features |= plus_features;
  606. cpu_def->features &= ~minus_features;
  607. #ifdef DEBUG_FEATURES
  608. print_features(stderr, fprintf, cpu_def->features, NULL);
  609. #endif
  610. }
  611. void sparc_cpu_list(FILE *f, fprintf_function cpu_fprintf)
  612. {
  613. unsigned int i;
  614. for (i = 0; i < ARRAY_SIZE(sparc_defs); i++) {
  615. (*cpu_fprintf)(f, "Sparc %16s IU " TARGET_FMT_lx
  616. " FPU %08x MMU %08x NWINS %d ",
  617. sparc_defs[i].name,
  618. sparc_defs[i].iu_version,
  619. sparc_defs[i].fpu_version,
  620. sparc_defs[i].mmu_version,
  621. sparc_defs[i].nwindows);
  622. print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES &
  623. ~sparc_defs[i].features, "-");
  624. print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES &
  625. sparc_defs[i].features, "+");
  626. (*cpu_fprintf)(f, "\n");
  627. }
  628. (*cpu_fprintf)(f, "Default CPU feature flags (use '-' to remove): ");
  629. print_features(f, cpu_fprintf, CPU_DEFAULT_FEATURES, NULL);
  630. (*cpu_fprintf)(f, "\n");
  631. (*cpu_fprintf)(f, "Available CPU feature flags (use '+' to add): ");
  632. print_features(f, cpu_fprintf, ~CPU_DEFAULT_FEATURES, NULL);
  633. (*cpu_fprintf)(f, "\n");
  634. (*cpu_fprintf)(f, "Numerical features (use '=' to set): iu_version "
  635. "fpu_version mmu_version nwindows\n");
  636. }
  637. static void cpu_print_cc(FILE *f, fprintf_function cpu_fprintf,
  638. uint32_t cc)
  639. {
  640. cpu_fprintf(f, "%c%c%c%c", cc & PSR_NEG ? 'N' : '-',
  641. cc & PSR_ZERO ? 'Z' : '-', cc & PSR_OVF ? 'V' : '-',
  642. cc & PSR_CARRY ? 'C' : '-');
  643. }
  644. #ifdef TARGET_SPARC64
  645. #define REGS_PER_LINE 4
  646. #else
  647. #define REGS_PER_LINE 8
  648. #endif
  649. void sparc_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
  650. int flags)
  651. {
  652. SPARCCPU *cpu = SPARC_CPU(cs);
  653. CPUSPARCState *env = &cpu->env;
  654. int i, x;
  655. cpu_fprintf(f, "pc: " TARGET_FMT_lx " npc: " TARGET_FMT_lx "\n", env->pc,
  656. env->npc);
  657. for (i = 0; i < 8; i++) {
  658. if (i % REGS_PER_LINE == 0) {
  659. cpu_fprintf(f, "%%g%d-%d:", i, i + REGS_PER_LINE - 1);
  660. }
  661. cpu_fprintf(f, " " TARGET_FMT_lx, env->gregs[i]);
  662. if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
  663. cpu_fprintf(f, "\n");
  664. }
  665. }
  666. for (x = 0; x < 3; x++) {
  667. for (i = 0; i < 8; i++) {
  668. if (i % REGS_PER_LINE == 0) {
  669. cpu_fprintf(f, "%%%c%d-%d: ",
  670. x == 0 ? 'o' : (x == 1 ? 'l' : 'i'),
  671. i, i + REGS_PER_LINE - 1);
  672. }
  673. cpu_fprintf(f, TARGET_FMT_lx " ", env->regwptr[i + x * 8]);
  674. if (i % REGS_PER_LINE == REGS_PER_LINE - 1) {
  675. cpu_fprintf(f, "\n");
  676. }
  677. }
  678. }
  679. for (i = 0; i < TARGET_DPREGS; i++) {
  680. if ((i & 3) == 0) {
  681. cpu_fprintf(f, "%%f%02d: ", i * 2);
  682. }
  683. cpu_fprintf(f, " %016" PRIx64, env->fpr[i].ll);
  684. if ((i & 3) == 3) {
  685. cpu_fprintf(f, "\n");
  686. }
  687. }
  688. #ifdef TARGET_SPARC64
  689. cpu_fprintf(f, "pstate: %08x ccr: %02x (icc: ", env->pstate,
  690. (unsigned)cpu_get_ccr(env));
  691. cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << PSR_CARRY_SHIFT);
  692. cpu_fprintf(f, " xcc: ");
  693. cpu_print_cc(f, cpu_fprintf, cpu_get_ccr(env) << (PSR_CARRY_SHIFT - 4));
  694. cpu_fprintf(f, ") asi: %02x tl: %d pil: %x\n", env->asi, env->tl,
  695. env->psrpil);
  696. cpu_fprintf(f, "cansave: %d canrestore: %d otherwin: %d wstate: %d "
  697. "cleanwin: %d cwp: %d\n",
  698. env->cansave, env->canrestore, env->otherwin, env->wstate,
  699. env->cleanwin, env->nwindows - 1 - env->cwp);
  700. cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx " fprs: "
  701. TARGET_FMT_lx "\n", env->fsr, env->y, env->fprs);
  702. #else
  703. cpu_fprintf(f, "psr: %08x (icc: ", cpu_get_psr(env));
  704. cpu_print_cc(f, cpu_fprintf, cpu_get_psr(env));
  705. cpu_fprintf(f, " SPE: %c%c%c) wim: %08x\n", env->psrs ? 'S' : '-',
  706. env->psrps ? 'P' : '-', env->psret ? 'E' : '-',
  707. env->wim);
  708. cpu_fprintf(f, "fsr: " TARGET_FMT_lx " y: " TARGET_FMT_lx "\n",
  709. env->fsr, env->y);
  710. #endif
  711. cpu_fprintf(f, "\n");
  712. }
  713. static void sparc_cpu_set_pc(CPUState *cs, vaddr value)
  714. {
  715. SPARCCPU *cpu = SPARC_CPU(cs);
  716. cpu->env.pc = value;
  717. cpu->env.npc = value + 4;
  718. }
  719. static void sparc_cpu_synchronize_from_tb(CPUState *cs, TranslationBlock *tb)
  720. {
  721. SPARCCPU *cpu = SPARC_CPU(cs);
  722. cpu->env.pc = tb->pc;
  723. cpu->env.npc = tb->cs_base;
  724. }
  725. static bool sparc_cpu_has_work(CPUState *cs)
  726. {
  727. SPARCCPU *cpu = SPARC_CPU(cs);
  728. CPUSPARCState *env = &cpu->env;
  729. return (cs->interrupt_request & CPU_INTERRUPT_HARD) &&
  730. cpu_interrupts_enabled(env);
  731. }
  732. static void sparc_cpu_realizefn(DeviceState *dev, Error **errp)
  733. {
  734. SPARCCPUClass *scc = SPARC_CPU_GET_CLASS(dev);
  735. #if defined(CONFIG_USER_ONLY)
  736. SPARCCPU *cpu = SPARC_CPU(dev);
  737. CPUSPARCState *env = &cpu->env;
  738. if ((env->def->features & CPU_FEATURE_FLOAT)) {
  739. env->def->features |= CPU_FEATURE_FLOAT128;
  740. }
  741. #endif
  742. qemu_init_vcpu(CPU(dev));
  743. scc->parent_realize(dev, errp);
  744. }
  745. static void sparc_cpu_initfn(Object *obj)
  746. {
  747. CPUState *cs = CPU(obj);
  748. SPARCCPU *cpu = SPARC_CPU(obj);
  749. CPUSPARCState *env = &cpu->env;
  750. cs->env_ptr = env;
  751. cpu_exec_init(env);
  752. if (tcg_enabled()) {
  753. gen_intermediate_code_init(env);
  754. }
  755. }
  756. static void sparc_cpu_uninitfn(Object *obj)
  757. {
  758. SPARCCPU *cpu = SPARC_CPU(obj);
  759. CPUSPARCState *env = &cpu->env;
  760. g_free(env->def);
  761. }
  762. static void sparc_cpu_class_init(ObjectClass *oc, void *data)
  763. {
  764. SPARCCPUClass *scc = SPARC_CPU_CLASS(oc);
  765. CPUClass *cc = CPU_CLASS(oc);
  766. DeviceClass *dc = DEVICE_CLASS(oc);
  767. scc->parent_realize = dc->realize;
  768. dc->realize = sparc_cpu_realizefn;
  769. scc->parent_reset = cc->reset;
  770. cc->reset = sparc_cpu_reset;
  771. cc->parse_features = sparc_cpu_parse_features;
  772. cc->has_work = sparc_cpu_has_work;
  773. cc->do_interrupt = sparc_cpu_do_interrupt;
  774. cc->cpu_exec_interrupt = sparc_cpu_exec_interrupt;
  775. cc->dump_state = sparc_cpu_dump_state;
  776. #if !defined(TARGET_SPARC64) && !defined(CONFIG_USER_ONLY)
  777. cc->memory_rw_debug = sparc_cpu_memory_rw_debug;
  778. #endif
  779. cc->set_pc = sparc_cpu_set_pc;
  780. cc->synchronize_from_tb = sparc_cpu_synchronize_from_tb;
  781. cc->gdb_read_register = sparc_cpu_gdb_read_register;
  782. cc->gdb_write_register = sparc_cpu_gdb_write_register;
  783. #ifdef CONFIG_USER_ONLY
  784. cc->handle_mmu_fault = sparc_cpu_handle_mmu_fault;
  785. #else
  786. cc->do_unassigned_access = sparc_cpu_unassigned_access;
  787. cc->do_unaligned_access = sparc_cpu_do_unaligned_access;
  788. cc->get_phys_page_debug = sparc_cpu_get_phys_page_debug;
  789. #endif
  790. #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
  791. cc->gdb_num_core_regs = 86;
  792. #else
  793. cc->gdb_num_core_regs = 72;
  794. #endif
  795. }
  796. static const TypeInfo sparc_cpu_type_info = {
  797. .name = TYPE_SPARC_CPU,
  798. .parent = TYPE_CPU,
  799. .instance_size = sizeof(SPARCCPU),
  800. .instance_init = sparc_cpu_initfn,
  801. .instance_finalize = sparc_cpu_uninitfn,
  802. .abstract = false,
  803. .class_size = sizeof(SPARCCPUClass),
  804. .class_init = sparc_cpu_class_init,
  805. };
  806. static void sparc_cpu_register_types(void)
  807. {
  808. type_register_static(&sparc_cpu_type_info);
  809. }
  810. type_init(sparc_cpu_register_types)