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