1 /*
2  * Copyright (c) 2020 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <debug/coredump.h>
9 
10 #define ARCH_HDR_VER			1
11 
12 uint32_t z_arm_coredump_fault_sp;
13 
14 struct arm_arch_block {
15 	struct {
16 		uint32_t	r0;
17 		uint32_t	r1;
18 		uint32_t	r2;
19 		uint32_t	r3;
20 		uint32_t	r12;
21 		uint32_t	lr;
22 		uint32_t	pc;
23 		uint32_t	xpsr;
24 		uint32_t	sp;
25 	} r;
26 } __packed;
27 
28 /*
29  * This might be too large for stack space if defined
30  * inside function. So do it here.
31  */
32 static struct arm_arch_block arch_blk;
33 
arch_coredump_info_dump(const z_arch_esf_t * esf)34 void arch_coredump_info_dump(const z_arch_esf_t *esf)
35 {
36 	struct coredump_arch_hdr_t hdr = {
37 		.id = COREDUMP_ARCH_HDR_ID,
38 		.hdr_version = ARCH_HDR_VER,
39 		.num_bytes = sizeof(arch_blk),
40 	};
41 
42 	/* Nothing to process */
43 	if (esf == NULL) {
44 		return;
45 	}
46 
47 	(void)memset(&arch_blk, 0, sizeof(arch_blk));
48 
49 	/*
50 	 * 17 registers expected by GDB.
51 	 * Not all are in ESF but the GDB stub
52 	 * will need to send all 17 as one packet.
53 	 * The stub will need to send undefined
54 	 * for registers not presented in coredump.
55 	 */
56 	arch_blk.r.r0 = esf->basic.r0;
57 	arch_blk.r.r1 = esf->basic.r1;
58 	arch_blk.r.r2 = esf->basic.r2;
59 	arch_blk.r.r3 = esf->basic.r3;
60 	arch_blk.r.r12 = esf->basic.ip;
61 	arch_blk.r.lr = esf->basic.lr;
62 	arch_blk.r.pc = esf->basic.pc;
63 	arch_blk.r.xpsr = esf->basic.xpsr;
64 
65 	arch_blk.r.sp = z_arm_coredump_fault_sp;
66 
67 	/* Send for output */
68 	coredump_buffer_output((uint8_t *)&hdr, sizeof(hdr));
69 	coredump_buffer_output((uint8_t *)&arch_blk, sizeof(arch_blk));
70 }
71 
arch_coredump_tgt_code_get(void)72 uint16_t arch_coredump_tgt_code_get(void)
73 {
74 	return COREDUMP_TGT_ARM_CORTEX_M;
75 }
76