cpu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright(c) 2019-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qemu/qemu-print.h"
  19. #include "cpu.h"
  20. #include "internal.h"
  21. #include "exec/exec-all.h"
  22. #include "qapi/error.h"
  23. #include "hw/qdev-properties.h"
  24. #include "fpu/softfloat-helpers.h"
  25. #include "tcg/tcg.h"
  26. #include "exec/gdbstub.h"
  27. static void hexagon_v67_cpu_init(Object *obj) { }
  28. static void hexagon_v68_cpu_init(Object *obj) { }
  29. static void hexagon_v69_cpu_init(Object *obj) { }
  30. static void hexagon_v71_cpu_init(Object *obj) { }
  31. static void hexagon_v73_cpu_init(Object *obj) { }
  32. static void hexagon_cpu_list_entry(gpointer data, gpointer user_data)
  33. {
  34. ObjectClass *oc = data;
  35. char *name = g_strdup(object_class_get_name(oc));
  36. if (g_str_has_suffix(name, HEXAGON_CPU_TYPE_SUFFIX)) {
  37. name[strlen(name) - strlen(HEXAGON_CPU_TYPE_SUFFIX)] = '\0';
  38. }
  39. qemu_printf(" %s\n", name);
  40. g_free(name);
  41. }
  42. void hexagon_cpu_list(void)
  43. {
  44. GSList *list;
  45. list = object_class_get_list_sorted(TYPE_HEXAGON_CPU, false);
  46. qemu_printf("Available CPUs:\n");
  47. g_slist_foreach(list, hexagon_cpu_list_entry, NULL);
  48. g_slist_free(list);
  49. }
  50. static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
  51. {
  52. ObjectClass *oc;
  53. char *typename;
  54. char **cpuname;
  55. cpuname = g_strsplit(cpu_model, ",", 1);
  56. typename = g_strdup_printf(HEXAGON_CPU_TYPE_NAME("%s"), cpuname[0]);
  57. oc = object_class_by_name(typename);
  58. g_strfreev(cpuname);
  59. g_free(typename);
  60. if (!oc || !object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU) ||
  61. object_class_is_abstract(oc)) {
  62. return NULL;
  63. }
  64. return oc;
  65. }
  66. static Property hexagon_lldb_compat_property =
  67. DEFINE_PROP_BOOL("lldb-compat", HexagonCPU, lldb_compat, false);
  68. static Property hexagon_lldb_stack_adjust_property =
  69. DEFINE_PROP_UNSIGNED("lldb-stack-adjust", HexagonCPU, lldb_stack_adjust,
  70. 0, qdev_prop_uint32, target_ulong);
  71. static Property hexagon_short_circuit_property =
  72. DEFINE_PROP_BOOL("short-circuit", HexagonCPU, short_circuit, true);
  73. const char * const hexagon_regnames[TOTAL_PER_THREAD_REGS] = {
  74. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  75. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  76. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  77. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  78. "sa0", "lc0", "sa1", "lc1", "p3_0", "c5", "m0", "m1",
  79. "usr", "pc", "ugp", "gp", "cs0", "cs1", "c14", "c15",
  80. "c16", "c17", "c18", "c19", "pkt_cnt", "insn_cnt", "hvx_cnt", "c23",
  81. "c24", "c25", "c26", "c27", "c28", "c29", "c30", "c31",
  82. };
  83. /*
  84. * One of the main debugging techniques is to use "-d cpu" and compare against
  85. * LLDB output when single stepping. However, the target and qemu put the
  86. * stacks at different locations. This is used to compensate so the diff is
  87. * cleaner.
  88. */
  89. static target_ulong adjust_stack_ptrs(CPUHexagonState *env, target_ulong addr)
  90. {
  91. HexagonCPU *cpu = env_archcpu(env);
  92. target_ulong stack_adjust = cpu->lldb_stack_adjust;
  93. target_ulong stack_start = env->stack_start;
  94. target_ulong stack_size = 0x10000;
  95. if (stack_adjust == 0) {
  96. return addr;
  97. }
  98. if (stack_start + 0x1000 >= addr && addr >= (stack_start - stack_size)) {
  99. return addr - stack_adjust;
  100. }
  101. return addr;
  102. }
  103. /* HEX_REG_P3_0_ALIASED (aka C4) is an alias for the predicate registers */
  104. static target_ulong read_p3_0(CPUHexagonState *env)
  105. {
  106. int32_t control_reg = 0;
  107. int i;
  108. for (i = NUM_PREGS - 1; i >= 0; i--) {
  109. control_reg <<= 8;
  110. control_reg |= env->pred[i] & 0xff;
  111. }
  112. return control_reg;
  113. }
  114. static void print_reg(FILE *f, CPUHexagonState *env, int regnum)
  115. {
  116. target_ulong value;
  117. if (regnum == HEX_REG_P3_0_ALIASED) {
  118. value = read_p3_0(env);
  119. } else {
  120. value = regnum < 32 ? adjust_stack_ptrs(env, env->gpr[regnum])
  121. : env->gpr[regnum];
  122. }
  123. qemu_fprintf(f, " %s = 0x" TARGET_FMT_lx "\n",
  124. hexagon_regnames[regnum], value);
  125. }
  126. static void print_vreg(FILE *f, CPUHexagonState *env, int regnum,
  127. bool skip_if_zero)
  128. {
  129. if (skip_if_zero) {
  130. bool nonzero_found = false;
  131. for (int i = 0; i < MAX_VEC_SIZE_BYTES; i++) {
  132. if (env->VRegs[regnum].ub[i] != 0) {
  133. nonzero_found = true;
  134. break;
  135. }
  136. }
  137. if (!nonzero_found) {
  138. return;
  139. }
  140. }
  141. qemu_fprintf(f, " v%d = ( ", regnum);
  142. qemu_fprintf(f, "0x%02x", env->VRegs[regnum].ub[MAX_VEC_SIZE_BYTES - 1]);
  143. for (int i = MAX_VEC_SIZE_BYTES - 2; i >= 0; i--) {
  144. qemu_fprintf(f, ", 0x%02x", env->VRegs[regnum].ub[i]);
  145. }
  146. qemu_fprintf(f, " )\n");
  147. }
  148. void hexagon_debug_vreg(CPUHexagonState *env, int regnum)
  149. {
  150. print_vreg(stdout, env, regnum, false);
  151. }
  152. static void print_qreg(FILE *f, CPUHexagonState *env, int regnum,
  153. bool skip_if_zero)
  154. {
  155. if (skip_if_zero) {
  156. bool nonzero_found = false;
  157. for (int i = 0; i < MAX_VEC_SIZE_BYTES / 8; i++) {
  158. if (env->QRegs[regnum].ub[i] != 0) {
  159. nonzero_found = true;
  160. break;
  161. }
  162. }
  163. if (!nonzero_found) {
  164. return;
  165. }
  166. }
  167. qemu_fprintf(f, " q%d = ( ", regnum);
  168. qemu_fprintf(f, "0x%02x",
  169. env->QRegs[regnum].ub[MAX_VEC_SIZE_BYTES / 8 - 1]);
  170. for (int i = MAX_VEC_SIZE_BYTES / 8 - 2; i >= 0; i--) {
  171. qemu_fprintf(f, ", 0x%02x", env->QRegs[regnum].ub[i]);
  172. }
  173. qemu_fprintf(f, " )\n");
  174. }
  175. void hexagon_debug_qreg(CPUHexagonState *env, int regnum)
  176. {
  177. print_qreg(stdout, env, regnum, false);
  178. }
  179. static void hexagon_dump(CPUHexagonState *env, FILE *f, int flags)
  180. {
  181. HexagonCPU *cpu = env_archcpu(env);
  182. if (cpu->lldb_compat) {
  183. /*
  184. * When comparing with LLDB, it doesn't step through single-cycle
  185. * hardware loops the same way. So, we just skip them here
  186. */
  187. if (env->gpr[HEX_REG_PC] == env->last_pc_dumped) {
  188. return;
  189. }
  190. env->last_pc_dumped = env->gpr[HEX_REG_PC];
  191. }
  192. qemu_fprintf(f, "General Purpose Registers = {\n");
  193. for (int i = 0; i < 32; i++) {
  194. print_reg(f, env, i);
  195. }
  196. print_reg(f, env, HEX_REG_SA0);
  197. print_reg(f, env, HEX_REG_LC0);
  198. print_reg(f, env, HEX_REG_SA1);
  199. print_reg(f, env, HEX_REG_LC1);
  200. print_reg(f, env, HEX_REG_M0);
  201. print_reg(f, env, HEX_REG_M1);
  202. print_reg(f, env, HEX_REG_USR);
  203. print_reg(f, env, HEX_REG_P3_0_ALIASED);
  204. print_reg(f, env, HEX_REG_GP);
  205. print_reg(f, env, HEX_REG_UGP);
  206. print_reg(f, env, HEX_REG_PC);
  207. #ifdef CONFIG_USER_ONLY
  208. /*
  209. * Not modelled in user mode, print junk to minimize the diff's
  210. * with LLDB output
  211. */
  212. qemu_fprintf(f, " cause = 0x000000db\n");
  213. qemu_fprintf(f, " badva = 0x00000000\n");
  214. qemu_fprintf(f, " cs0 = 0x00000000\n");
  215. qemu_fprintf(f, " cs1 = 0x00000000\n");
  216. #else
  217. print_reg(f, env, HEX_REG_CAUSE);
  218. print_reg(f, env, HEX_REG_BADVA);
  219. print_reg(f, env, HEX_REG_CS0);
  220. print_reg(f, env, HEX_REG_CS1);
  221. #endif
  222. qemu_fprintf(f, "}\n");
  223. if (flags & CPU_DUMP_FPU) {
  224. qemu_fprintf(f, "Vector Registers = {\n");
  225. for (int i = 0; i < NUM_VREGS; i++) {
  226. print_vreg(f, env, i, true);
  227. }
  228. for (int i = 0; i < NUM_QREGS; i++) {
  229. print_qreg(f, env, i, true);
  230. }
  231. qemu_fprintf(f, "}\n");
  232. }
  233. }
  234. static void hexagon_dump_state(CPUState *cs, FILE *f, int flags)
  235. {
  236. HexagonCPU *cpu = HEXAGON_CPU(cs);
  237. CPUHexagonState *env = &cpu->env;
  238. hexagon_dump(env, f, flags);
  239. }
  240. void hexagon_debug(CPUHexagonState *env)
  241. {
  242. hexagon_dump(env, stdout, CPU_DUMP_FPU);
  243. }
  244. static void hexagon_cpu_set_pc(CPUState *cs, vaddr value)
  245. {
  246. HexagonCPU *cpu = HEXAGON_CPU(cs);
  247. CPUHexagonState *env = &cpu->env;
  248. env->gpr[HEX_REG_PC] = value;
  249. }
  250. static vaddr hexagon_cpu_get_pc(CPUState *cs)
  251. {
  252. HexagonCPU *cpu = HEXAGON_CPU(cs);
  253. CPUHexagonState *env = &cpu->env;
  254. return env->gpr[HEX_REG_PC];
  255. }
  256. static void hexagon_cpu_synchronize_from_tb(CPUState *cs,
  257. const TranslationBlock *tb)
  258. {
  259. HexagonCPU *cpu = HEXAGON_CPU(cs);
  260. CPUHexagonState *env = &cpu->env;
  261. tcg_debug_assert(!(cs->tcg_cflags & CF_PCREL));
  262. env->gpr[HEX_REG_PC] = tb->pc;
  263. }
  264. static bool hexagon_cpu_has_work(CPUState *cs)
  265. {
  266. return true;
  267. }
  268. static void hexagon_restore_state_to_opc(CPUState *cs,
  269. const TranslationBlock *tb,
  270. const uint64_t *data)
  271. {
  272. HexagonCPU *cpu = HEXAGON_CPU(cs);
  273. CPUHexagonState *env = &cpu->env;
  274. env->gpr[HEX_REG_PC] = data[0];
  275. }
  276. static void hexagon_cpu_reset_hold(Object *obj)
  277. {
  278. CPUState *cs = CPU(obj);
  279. HexagonCPU *cpu = HEXAGON_CPU(cs);
  280. HexagonCPUClass *mcc = HEXAGON_CPU_GET_CLASS(cpu);
  281. CPUHexagonState *env = &cpu->env;
  282. if (mcc->parent_phases.hold) {
  283. mcc->parent_phases.hold(obj);
  284. }
  285. set_default_nan_mode(1, &env->fp_status);
  286. set_float_detect_tininess(float_tininess_before_rounding, &env->fp_status);
  287. }
  288. static void hexagon_cpu_disas_set_info(CPUState *s, disassemble_info *info)
  289. {
  290. info->print_insn = print_insn_hexagon;
  291. }
  292. static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
  293. {
  294. CPUState *cs = CPU(dev);
  295. HexagonCPUClass *mcc = HEXAGON_CPU_GET_CLASS(dev);
  296. Error *local_err = NULL;
  297. cpu_exec_realizefn(cs, &local_err);
  298. if (local_err != NULL) {
  299. error_propagate(errp, local_err);
  300. return;
  301. }
  302. gdb_register_coprocessor(cs, hexagon_hvx_gdb_read_register,
  303. hexagon_hvx_gdb_write_register,
  304. NUM_VREGS + NUM_QREGS,
  305. "hexagon-hvx.xml", 0);
  306. qemu_init_vcpu(cs);
  307. cpu_reset(cs);
  308. mcc->parent_realize(dev, errp);
  309. }
  310. static void hexagon_cpu_init(Object *obj)
  311. {
  312. HexagonCPU *cpu = HEXAGON_CPU(obj);
  313. cpu_set_cpustate_pointers(cpu);
  314. qdev_property_add_static(DEVICE(obj), &hexagon_lldb_compat_property);
  315. qdev_property_add_static(DEVICE(obj), &hexagon_lldb_stack_adjust_property);
  316. qdev_property_add_static(DEVICE(obj), &hexagon_short_circuit_property);
  317. }
  318. #include "hw/core/tcg-cpu-ops.h"
  319. static const struct TCGCPUOps hexagon_tcg_ops = {
  320. .initialize = hexagon_translate_init,
  321. .synchronize_from_tb = hexagon_cpu_synchronize_from_tb,
  322. .restore_state_to_opc = hexagon_restore_state_to_opc,
  323. };
  324. static void hexagon_cpu_class_init(ObjectClass *c, void *data)
  325. {
  326. HexagonCPUClass *mcc = HEXAGON_CPU_CLASS(c);
  327. CPUClass *cc = CPU_CLASS(c);
  328. DeviceClass *dc = DEVICE_CLASS(c);
  329. ResettableClass *rc = RESETTABLE_CLASS(c);
  330. device_class_set_parent_realize(dc, hexagon_cpu_realize,
  331. &mcc->parent_realize);
  332. resettable_class_set_parent_phases(rc, NULL, hexagon_cpu_reset_hold, NULL,
  333. &mcc->parent_phases);
  334. cc->class_by_name = hexagon_cpu_class_by_name;
  335. cc->has_work = hexagon_cpu_has_work;
  336. cc->dump_state = hexagon_dump_state;
  337. cc->set_pc = hexagon_cpu_set_pc;
  338. cc->get_pc = hexagon_cpu_get_pc;
  339. cc->gdb_read_register = hexagon_gdb_read_register;
  340. cc->gdb_write_register = hexagon_gdb_write_register;
  341. cc->gdb_num_core_regs = TOTAL_PER_THREAD_REGS;
  342. cc->gdb_stop_before_watchpoint = true;
  343. cc->gdb_core_xml_file = "hexagon-core.xml";
  344. cc->disas_set_info = hexagon_cpu_disas_set_info;
  345. cc->tcg_ops = &hexagon_tcg_ops;
  346. }
  347. #define DEFINE_CPU(type_name, initfn) \
  348. { \
  349. .name = type_name, \
  350. .parent = TYPE_HEXAGON_CPU, \
  351. .instance_init = initfn \
  352. }
  353. static const TypeInfo hexagon_cpu_type_infos[] = {
  354. {
  355. .name = TYPE_HEXAGON_CPU,
  356. .parent = TYPE_CPU,
  357. .instance_size = sizeof(HexagonCPU),
  358. .instance_init = hexagon_cpu_init,
  359. .abstract = true,
  360. .class_size = sizeof(HexagonCPUClass),
  361. .class_init = hexagon_cpu_class_init,
  362. },
  363. DEFINE_CPU(TYPE_HEXAGON_CPU_V67, hexagon_v67_cpu_init),
  364. DEFINE_CPU(TYPE_HEXAGON_CPU_V68, hexagon_v68_cpu_init),
  365. DEFINE_CPU(TYPE_HEXAGON_CPU_V69, hexagon_v69_cpu_init),
  366. DEFINE_CPU(TYPE_HEXAGON_CPU_V71, hexagon_v71_cpu_init),
  367. DEFINE_CPU(TYPE_HEXAGON_CPU_V73, hexagon_v73_cpu_init),
  368. };
  369. DEFINE_TYPES(hexagon_cpu_type_infos)