1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12#pragma once
13
14#include <sel4/sel4.h>
15#include <stdint.h>
16#include <stdbool.h>
17#include <camkes/gdb/delegate_types.h>
18
19#define NO_BREAKPOINT -1
20#define USER_BREAKPOINT 0
21#define BREAKPOINT_INSTRUCTION 0xCC
22#define MAX_ARGS 20
23#define COMMAND_START                   1
24#define HEX_STRING                      16
25#define DEC_STRING                      10
26#define CHAR_HEX_SIZE                   2
27
28// Colour coding for response packets from GDB stub
29//#define GDB_RESPONSE_START      "\x1b[31m"
30//#define GDB_RESPONSE_END        "\x1b[0m"
31#define GDB_RESPONSE_START      ""
32#define GDB_RESPONSE_END        ""
33
34// Ok packet for GDB
35#define GDB_ACK                 "+"
36#define GDB_NACK                "-"
37#define x86_VALID_REGISTERS     10
38#define x86_GDB_REGISTERS       13
39#define x86_MAX_REGISTERS       16
40#define x86_INVALID_REGISTER    10
41#define x86_NUM_HW_BRK          4
42#define x86_SW_BREAK            0xCC
43
44#define HARDWARE_BREAKPOINT      0x1
45#define GENERAL_PROTECTION_FAULT 0xD
46
47typedef enum {
48    stop_none,
49    stop_sw_break,
50    stop_hw_break,
51    stop_step,
52    stop_watch
53} stop_reason_t;
54
55typedef enum {
56    gdb_SoftwareBreakpoint,
57    gdb_HardwareBreakpoint,
58    gdb_WriteWatchpoint,
59    gdb_ReadWatchpoint,
60    gdb_AccessWatchpoint
61} gdb_BreakpointType;
62
63#define GETCHAR_BUFSIZ 512
64
65typedef struct gdb_buffer {
66    uint32_t length;
67    uint32_t checksum_count;
68    uint32_t checksum_index;
69    char data[GETCHAR_BUFSIZ];
70} gdb_buffer_t;
71
72extern gdb_buffer_t buf;
73
74typedef struct {
75    /* Cap of currently selected thread in components cspace */
76    seL4_Word current_thread_tcb;
77    /* Current pc of the currently selected thread */
78    seL4_Word current_pc;
79    /* current thread's hw debugging step mode */
80    bool current_thread_step_mode;
81    /* Fault reason for the currently selected thread */
82    stop_reason_t stop_reason;
83    /* If fault was watch fault, then this is the address */
84    seL4_Word stop_watch_addr;
85    /* Callback function to wake thread's fault handler */
86    int (*sem_post)(void);
87} gdb_state_t;
88
89int delegate_write_memory(seL4_Word addr, seL4_Word length, delegate_mem_range_t data);
90int delegate_read_memory(seL4_Word addr, seL4_Word length, delegate_mem_range_t *data);
91void delegate_read_registers(seL4_Word tcb_cap, seL4_UserContext *registers);
92void delegate_read_register(seL4_Word tcb_cap, seL4_Word *reg, seL4_Word reg_num);
93int delegate_write_registers(seL4_Word tcb_cap, seL4_UserContext registers, int len);
94int delegate_write_register(seL4_Word tcb_cap, seL4_Word data, seL4_Word reg_num);
95int delegate_insert_break(seL4_Word tcb_cap, seL4_Word type, seL4_Word addr, seL4_Word size, seL4_Word rw);
96int delegate_remove_break(seL4_Word tcb_cap, seL4_Word type, seL4_Word addr, seL4_Word size, seL4_Word rw);
97int delegate_resume(seL4_Word tcb_cap);
98int delegate_step(seL4_Word tcb_cap);
99
100
101
102int handle_gdb(gdb_state_t *gdb_state);
103int gdb_handle_fault(gdb_state_t *gdb_state);