1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DEBUG_GDBSTUB_H_
8 #define ZEPHYR_INCLUDE_DEBUG_GDBSTUB_H_
9 
10 /* Map from CPU exceptions to GDB  */
11 #define GDB_EXCEPTION_INVALID_INSTRUCTION   4UL
12 #define GDB_EXCEPTION_BREAKPOINT            5UL
13 #define GDB_EXCEPTION_MEMORY_FAULT          7UL
14 #define GDB_EXCEPTION_DIVIDE_ERROR          8UL
15 #define GDB_EXCEPTION_INVALID_MEMORY        11UL
16 #define GDB_EXCEPTION_OVERFLOW              16UL
17 
18 /* Access permissions for memory regions */
19 #define GDB_MEM_REGION_NO_ACCESS            0UL
20 #define GDB_MEM_REGION_READ                 BIT(0)
21 #define GDB_MEM_REGION_WRITE                BIT(1)
22 
23 #define GDB_MEM_REGION_RO \
24 	(GDB_MEM_REGION_READ)
25 
26 #define GDB_MEM_REGION_RW \
27 	(GDB_MEM_REGION_READ | GDB_MEM_REGION_WRITE)
28 
29 /** Describe one memory region */
30 struct gdb_mem_region {
31 	/** Start address of a memory region */
32 	uintptr_t start;
33 
34 	/** End address of a memory region */
35 	uintptr_t end;
36 
37 	/** Memory region attributes */
38 	uint16_t  attributes;
39 
40 	/** Read/write alignment, 0 if using default alignment */
41 	uint8_t   alignment;
42 };
43 
44 /**
45  * Memory region descriptions used for GDB memory access.
46  *
47  * This array specifies which region of memory GDB can access
48  * with read/write attributes. This is used to restrict
49  * memory read/write in GDB stub to memory that can be
50  * legally accessed without resulting in memory faults.
51  */
52 extern const struct gdb_mem_region gdb_mem_region_array[];
53 
54 /**
55  * Number of Memory Regions.
56  *
57  * Number of elements in gdb_mem_region_array[];
58  */
59 extern const size_t gdb_mem_num_regions;
60 
61 /**
62  * @brief Convert a binary array into string representation.
63  *
64  * Note that this is similar to bin2hex() but does not force
65  * a null character at the end of the hexadecimal output buffer.
66  *
67  * @param buf     The binary array to convert
68  * @param buflen  The length of the binary array to convert
69  * @param hex     Address of where to store the string representation.
70  * @param hexlen  Size of the storage area for string representation.
71  *
72  * @return The length of the converted string, or 0 if an error occurred.
73  */
74 size_t gdb_bin2hex(const uint8_t *buf, size_t buflen,
75 		   char *hex, size_t hexlen);
76 
77 
78 /**
79  * @brief Check if a memory block can be read.
80  *
81  * This checks if the specified memory block can be read.
82  *
83  * @param[in]  addr  Starting address of the memory block
84  * @param[in]  len   Size of memory block
85  * @param[out] align Read alignment of region
86  *
87  * @return True if memory block can be read, false otherwise.
88  */
89 bool gdb_mem_can_read(const uintptr_t addr, const size_t len, uint8_t *align);
90 
91 /**
92  * @brief Check if a memory block can be written into.
93  *
94  * This checks if the specified memory block can be written into.
95  *
96  * @param[in]  addr  Starting address of the memory block
97  * @param[in]  len   Size of memory block
98  * @param[out] align Write alignment of region
99  *
100  * @return True if GDB stub can write to the block, false otherwise.
101  */
102 bool gdb_mem_can_write(const uintptr_t addr, const size_t len, uint8_t *align);
103 
104 #endif
105