summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/shared/comm/i386-stub-glue.c
blob: 96105e8a21f5d2c9fd83134b018abebb8d03507a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (c) 2016.
 * Chris Johns <chrisj@rtems.org>
 *
 * This software is Copyright (C) 1998 by T.sqware - all rights limited
 * It is provided in to the public domain "as is", can be freely modified
 * as far as this copyight notice is kept unchanged, but does not imply
 * an endorsement by T.sqware of the product in which it is included.
 */

#include <bsp.h>
#include <bsp/irq-generic.h>
#include <libchip/serial.h>

#include "../../../../../../../bsps/shared/dev/serial/legacy-console.h"

int  putDebugChar(int ch);     /* write a single character      */
int  getDebugChar(void);       /* read and return a single char */

/* Check is any characters received are a ^C */
int i386_gdb_uart_ctrl_c_check(void);

/* Raw interrupt handler. */
void i386_gdb_uart_isr(void);

/* assign an exception handler */
void exceptionHandler(int, void (*handler)(void));

/* User supplied remote debug option. */
extern int remote_debug;

/* Current uart and port used by the gdb stub */
static int          uart_current;
static int          uart_vector;
static console_tbl* port_current;

/*
 * Interrupt service routine for all, it does it check whether ^C is received
 * if yes it will flip TF bit before returning.
 *
 * Note: it should be installed as raw interrupt handler.
 *
 * Warning: I do not like the use of the global data, I am not
 *          sure if this is SMP safe.
 */
int i386_gdb_uart_isr_regsav[4] RTEMS_UNUSED;
__asm__ (".p2align 4");
__asm__ (".text");
__asm__ (".globl i386_gdb_uart_isr");
__asm__ ("i386_gdb_uart_isr:");
__asm__ ("    pusha");                                       /* Push all */
__asm__ ("    call  i386_gdb_uart_ctrl_c_check");            /* Look for ^C */
__asm__ ("    movl  %eax, i386_gdb_uart_isr_regsav");        /* Save eax */
__asm__ ("    popa");                                        /* Pop all */
__asm__ ("    xchgl %eax, i386_gdb_uart_isr_regsav");        /* Exchange eax */
__asm__ ("    cmpl  $0, %eax");                              /* 1 == ^C */
__asm__ ("    je    i386_gdb_uart_isr_1");                   /* branch if 0 */
__asm__ ("    movl  %ebx, i386_gdb_uart_isr_regsav + 4");    /* Save ebx */
__asm__ ("    movl  %edx, i386_gdb_uart_isr_regsav + 8");    /* Save edx */
__asm__ ("    popl  %ebx");                                  /* Pop eip */
__asm__ ("    popl  %edx");                                  /* Pop cs */
__asm__ ("    popl  %eax");                                  /* Pop flags */
__asm__ ("    orl   $0x100, %eax");                          /* Modify it */
__asm__ ("    pushl %eax");                                  /* Push it back */
__asm__ ("    pushl %edx");                                  /* Push cs */
__asm__ ("    pushl %ebx");                                  /* Push eip */
__asm__ ("    movl  i386_gdb_uart_isr_regsav + 4, %ebx");    /* Restore ebx */
__asm__ ("    movl  i386_gdb_uart_isr_regsav + 8, %edx");    /* Restore edx */
__asm__ ("i386_gdb_uart_isr_1:");
__asm__ ("    movl  i386_gdb_uart_isr_regsav, %eax");        /* Restore eax */
__asm__ ("    iret");                                        /* Done */

static int gdb_hello_index;
static const char *const gdb_hello = "+";

static inline uint8_t BSP_i8259a_irq_in_service_reg(uint32_t ioport)
{
  uint8_t isr;
  outport_byte(ioport, PIC_OCW3_SEL | PIC_OCW3_RR | PIC_OCW3_RIS);
  inport_byte(ioport, isr);
  outport_byte(ioport, PIC_OCW3_SEL | PIC_OCW3_RR);
  return isr;
}

static inline void BSP_irq_ack_at_i8259a(const int irqLine)
{
  uint8_t slave_isr = 0;
  if (irqLine >= 8) {
   outport_byte(PIC_SLAVE_COMMAND_IO_PORT, PIC_EOI);
   slave_isr = BSP_i8259a_irq_in_service_reg(PIC_SLAVE_COMMAND_IO_PORT);
  }

  /*
   * Only issue the EOI to the master if there are no more interrupts in
   * service for the slave. i8259a data sheet page 18, The Special Fully Nested
   * Mode, b.
   */
  if (slave_isr == 0)
    outport_byte(PIC_MASTER_COMMAND_IO_PORT, PIC_EOI);
}

int i386_gdb_uart_ctrl_c_check(void)
{
  BSP_irq_ack_at_i8259a(uart_vector);
  if (port_current) {
    int c = 0;
    while (c >= 0) {
      c = port_current->pDeviceFns->deviceRead(uart_current);
      if (c == 3) {
        gdb_hello_index = 0;
        return 1;
      } else if (gdb_hello[gdb_hello_index] == (char) c) {
        ++gdb_hello_index;
        if (gdb_hello[gdb_hello_index] == '\0') {
          gdb_hello_index = 0;
          return 1;
        }
      } else {
        gdb_hello_index = 0;
      }
    }
  }
  return 0;
}

static void
nop(const rtems_raw_irq_connect_data* notused)
{
}

static int
isOn(const rtems_raw_irq_connect_data* notused)
{
  return 1;
}

int i386_stub_glue_uart(void)
{
  if (port_current == NULL)
    return -1;
  return uart_current;
}

/*
 * Initialize glue code linking i386-stub with the rest of
 * the system
 */
void
i386_stub_glue_init(int uart)
{
  rtems_device_minor_number minor = (rtems_device_minor_number) uart;

  port_current = console_find_console_entry(NULL, 0, &minor);

  if (port_current == NULL) {
    printk("GDB: invalid minor number for UART\n");
    return;
  }

  uart_current = uart;

  /* Intialise the UART, assuming polled drivers */
  port_current->pDeviceFns->deviceInitialize(uart);
}

static void BSP_uart_on(const rtems_raw_irq_connect_data* used)
{
  bsp_interrupt_vector_enable(used->idtIndex - BSP_IRQ_VECTOR_BASE);
}

static void BSP_uart_off(const rtems_raw_irq_connect_data* used)
{
  bsp_interrupt_vector_disable(used->idtIndex - BSP_IRQ_VECTOR_BASE);
}

/*
 * In order to have a possibility to break into
 * running program, one has to call this function
 */
void i386_stub_glue_init_breakin(void)
{
  rtems_raw_irq_connect_data uart_raw_irq_data;

  if (port_current == NULL) {
    printk("GDB: no port initialised\n");
    return;
  }

  if ((port_current->ulIntVector == 0) || (port_current->ulIntVector > 16)) {
    printk("GDB: no UART interrupt support\n");
  }
  else {
    uart_vector = port_current->ulIntVector;
    uart_raw_irq_data.idtIndex = port_current->ulIntVector + BSP_IRQ_VECTOR_BASE;

    if (!i386_get_current_idt_entry(&uart_raw_irq_data)) {
      printk("GBD: cannot get idt entry\n");
      rtems_fatal_error_occurred(1);
    }

    if (!i386_delete_idt_entry(&uart_raw_irq_data)) {
      printk("GDB: cannot delete idt entry\n");
      rtems_fatal_error_occurred(1);
    }

    uart_raw_irq_data.on  = BSP_uart_on;
    uart_raw_irq_data.off = BSP_uart_off;

    /* Install ISR  */
    uart_raw_irq_data.idtIndex = port_current->ulIntVector + BSP_IRQ_VECTOR_BASE;
    uart_raw_irq_data.hdl = i386_gdb_uart_isr;

    if (!i386_set_idt_entry (&uart_raw_irq_data)) {
      printk("GDB: raw exception handler connection failed\n");
      rtems_fatal_error_occurred(1);
    }

    /* Enable interrupts, this is a bit of a hack because we
     * have to know the device but there is no other call. */
    (*port_current->setRegister)(port_current->ulCtrlPort1, 1, 0x01);
  }
}

int
putDebugChar(int ch)
{
  if (port_current != NULL) {
    port_current->pDeviceFns->deviceWritePolled(uart_current, ch);
  }
  return 1;
}

int getDebugChar(void)
{
  int c = -1;

  if (port_current != NULL) {
    while (c < 0)
      c = port_current->pDeviceFns->deviceRead(uart_current);
  }

  return c;
}

void exceptionHandler(int vector, void (*handler)(void))
{
  rtems_raw_irq_connect_data excep_raw_irq_data;

  excep_raw_irq_data.idtIndex = vector;

  if(!i386_get_current_idt_entry(&excep_raw_irq_data))
    {
      printk("GDB: cannot get idt entry\n");
      rtems_fatal_error_occurred(1);
    }

  if(!i386_delete_idt_entry(&excep_raw_irq_data))
    {
      printk("GDB: cannot delete idt entry\n");
      rtems_fatal_error_occurred(1);
    }

  excep_raw_irq_data.on = nop;
  excep_raw_irq_data.off = nop;
  excep_raw_irq_data.isOn = isOn;
  excep_raw_irq_data.hdl = handler;

  if (!i386_set_idt_entry (&excep_raw_irq_data)) {
      printk("GDB: raw exception handler connection failed\n");
      rtems_fatal_error_occurred(1);
    }
  return;
}