1 /*
2  * Copyright (c) 2022 Huawei Technologies SASU
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <zephyr/debug/coredump.h>
9 
10 /* Identify the version of this block (in case of architecture changes).
11  * To be interpreted by the target architecture specific block parser.
12  */
13 #define ARCH_HDR_VER			1
14 
15 /* Structure to store the architecture registers passed arch_coredump_info_dump
16  * As callee saved registers are not provided in struct arch_esf structure in Zephyr
17  * we just need 22 registers.
18  */
19 struct arm64_arch_block {
20 	struct {
21 		uint64_t x0;
22 		uint64_t x1;
23 		uint64_t x2;
24 		uint64_t x3;
25 		uint64_t x4;
26 		uint64_t x5;
27 		uint64_t x6;
28 		uint64_t x7;
29 		uint64_t x8;
30 		uint64_t x9;
31 		uint64_t x10;
32 		uint64_t x11;
33 		uint64_t x12;
34 		uint64_t x13;
35 		uint64_t x14;
36 		uint64_t x15;
37 		uint64_t x16;
38 		uint64_t x17;
39 		uint64_t x18;
40 		uint64_t lr;
41 		uint64_t spsr;
42 		uint64_t elr;
43 	} r;
44 } __packed;
45 
46 
47 /*
48  * Register block takes up too much stack space
49  * if defined within function. So define it here.
50  */
51 static struct arm64_arch_block arch_blk;
52 
arch_coredump_info_dump(const struct arch_esf * esf)53 void arch_coredump_info_dump(const struct arch_esf *esf)
54 {
55 	/* Target architecture information header */
56 	/* Information just relevant to the python parser */
57 	struct coredump_arch_hdr_t hdr = {
58 		.id = COREDUMP_ARCH_HDR_ID,
59 		.hdr_version = ARCH_HDR_VER,
60 		.num_bytes = sizeof(arch_blk),
61 	};
62 
63 	/* Nothing to process */
64 	if (esf == NULL) {
65 		return;
66 	}
67 
68 	(void)memset(&arch_blk, 0, sizeof(arch_blk));
69 
70 	/*
71 	 * Copies the thread registers to a memory block that will be printed out
72 	 * The thread registers are already provided by structure struct arch_esf
73 	 */
74 	arch_blk.r.x0 = esf->x0;
75 	arch_blk.r.x1 = esf->x1;
76 	arch_blk.r.x2 = esf->x2;
77 	arch_blk.r.x3 = esf->x3;
78 	arch_blk.r.x4 = esf->x4;
79 	arch_blk.r.x5 = esf->x5;
80 	arch_blk.r.x6 = esf->x6;
81 	arch_blk.r.x7 = esf->x7;
82 	arch_blk.r.x8 = esf->x8;
83 	arch_blk.r.x9 = esf->x9;
84 	arch_blk.r.x10 = esf->x10;
85 	arch_blk.r.x11 = esf->x11;
86 	arch_blk.r.x12 = esf->x12;
87 	arch_blk.r.x13 = esf->x13;
88 	arch_blk.r.x14 = esf->x14;
89 	arch_blk.r.x15 = esf->x15;
90 	arch_blk.r.x16 = esf->x16;
91 	arch_blk.r.x17 = esf->x17;
92 	arch_blk.r.x18 = esf->x18;
93 	arch_blk.r.lr = esf->lr;
94 	arch_blk.r.spsr = esf->spsr;
95 	arch_blk.r.elr = esf->elr;
96 
97 	/* Send for output */
98 	coredump_buffer_output((uint8_t *)&hdr, sizeof(hdr));
99 	coredump_buffer_output((uint8_t *)&arch_blk, sizeof(arch_blk));
100 }
101 
arch_coredump_tgt_code_get(void)102 uint16_t arch_coredump_tgt_code_get(void)
103 {
104 	return COREDUMP_TGT_ARM64;
105 }
106