1 | /* |
---|
2 | * Copyright (c) 2016. |
---|
3 | * Chris Johns <chrisj@rtems.org> |
---|
4 | * |
---|
5 | * This software is Copyright (C) 1998 by T.sqware - all rights limited |
---|
6 | * It is provided in to the public domain "as is", can be freely modified |
---|
7 | * as far as this copyight notice is kept unchanged, but does not imply |
---|
8 | * an endorsement by T.sqware of the product in which it is included. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <bsp.h> |
---|
12 | #include <bsp/irq-generic.h> |
---|
13 | #include <libchip/serial.h> |
---|
14 | |
---|
15 | #include "../../../shared/console_private.h" |
---|
16 | |
---|
17 | int putDebugChar(int ch); /* write a single character */ |
---|
18 | int getDebugChar(void); /* read and return a single char */ |
---|
19 | |
---|
20 | /* Check is any characters received are a ^C */ |
---|
21 | int i386_gdb_uart_ctrl_c_check(void); |
---|
22 | |
---|
23 | /* Raw interrupt handler. */ |
---|
24 | void i386_gdb_uart_isr(void); |
---|
25 | |
---|
26 | /* assign an exception handler */ |
---|
27 | void exceptionHandler(int, void (*handler)(void)); |
---|
28 | |
---|
29 | /* User supplied remote debug option. */ |
---|
30 | extern int remote_debug; |
---|
31 | |
---|
32 | /* Current uart and port used by the gdb stub */ |
---|
33 | static int uart_current; |
---|
34 | static int uart_vector; |
---|
35 | static console_tbl* port_current; |
---|
36 | |
---|
37 | /* |
---|
38 | * Interrupt service routine for all, it does it check whether ^C is received |
---|
39 | * if yes it will flip TF bit before returning. |
---|
40 | * |
---|
41 | * Note: it should be installed as raw interrupt handler. |
---|
42 | * |
---|
43 | * Warning: I do not like the use of the global data, I am not |
---|
44 | * sure if this is SMP safe. |
---|
45 | */ |
---|
46 | int i386_gdb_uart_isr_regsav[4] RTEMS_UNUSED; |
---|
47 | __asm__ (".p2align 4"); |
---|
48 | __asm__ (".text"); |
---|
49 | __asm__ (".globl i386_gdb_uart_isr"); |
---|
50 | __asm__ ("i386_gdb_uart_isr:"); |
---|
51 | __asm__ (" pusha"); /* Push all */ |
---|
52 | __asm__ (" call i386_gdb_uart_ctrl_c_check"); /* Look for ^C */ |
---|
53 | __asm__ (" movl %eax, i386_gdb_uart_isr_regsav"); /* Save eax */ |
---|
54 | __asm__ (" popa"); /* Pop all */ |
---|
55 | __asm__ (" xchgl %eax, i386_gdb_uart_isr_regsav"); /* Exchange eax */ |
---|
56 | __asm__ (" cmpl $0, %eax"); /* 1 == ^C */ |
---|
57 | __asm__ (" je i386_gdb_uart_isr_1"); /* branch if 0 */ |
---|
58 | __asm__ (" movl %ebx, i386_gdb_uart_isr_regsav + 4"); /* Save ebx */ |
---|
59 | __asm__ (" movl %edx, i386_gdb_uart_isr_regsav + 8"); /* Save edx */ |
---|
60 | __asm__ (" popl %ebx"); /* Pop eip */ |
---|
61 | __asm__ (" popl %edx"); /* Pop cs */ |
---|
62 | __asm__ (" popl %eax"); /* Pop flags */ |
---|
63 | __asm__ (" orl $0x100, %eax"); /* Modify it */ |
---|
64 | __asm__ (" pushl %eax"); /* Push it back */ |
---|
65 | __asm__ (" pushl %edx"); /* Push cs */ |
---|
66 | __asm__ (" pushl %ebx"); /* Push eip */ |
---|
67 | __asm__ (" movl i386_gdb_uart_isr_regsav + 4, %ebx"); /* Restore ebx */ |
---|
68 | __asm__ (" movl i386_gdb_uart_isr_regsav + 8, %edx"); /* Restore edx */ |
---|
69 | __asm__ ("i386_gdb_uart_isr_1:"); |
---|
70 | __asm__ (" movl i386_gdb_uart_isr_regsav, %eax"); /* Restore eax */ |
---|
71 | __asm__ (" iret"); /* Done */ |
---|
72 | |
---|
73 | static int gdb_hello_index; |
---|
74 | static const char *const gdb_hello = "+"; |
---|
75 | |
---|
76 | static inline uint8_t BSP_i8259a_irq_in_service_reg(uint32_t ioport) |
---|
77 | { |
---|
78 | uint8_t isr; |
---|
79 | outport_byte(ioport, PIC_OCW3_SEL | PIC_OCW3_RR | PIC_OCW3_RIS); |
---|
80 | inport_byte(ioport, isr); |
---|
81 | outport_byte(ioport, PIC_OCW3_SEL | PIC_OCW3_RR); |
---|
82 | return isr; |
---|
83 | } |
---|
84 | |
---|
85 | static inline void BSP_irq_ack_at_i8259a(const int irqLine) |
---|
86 | { |
---|
87 | uint8_t slave_isr = 0; |
---|
88 | if (irqLine >= 8) { |
---|
89 | outport_byte(PIC_SLAVE_COMMAND_IO_PORT, PIC_EOI); |
---|
90 | slave_isr = BSP_i8259a_irq_in_service_reg(PIC_SLAVE_COMMAND_IO_PORT); |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | * Only issue the EOI to the master if there are no more interrupts in |
---|
95 | * service for the slave. i8259a data sheet page 18, The Special Fully Nested |
---|
96 | * Mode, b. |
---|
97 | */ |
---|
98 | if (slave_isr == 0) |
---|
99 | outport_byte(PIC_MASTER_COMMAND_IO_PORT, PIC_EOI); |
---|
100 | } |
---|
101 | |
---|
102 | int i386_gdb_uart_ctrl_c_check(void) |
---|
103 | { |
---|
104 | BSP_irq_ack_at_i8259a(uart_vector); |
---|
105 | if (port_current) { |
---|
106 | int c = 0; |
---|
107 | while (c >= 0) { |
---|
108 | c = port_current->pDeviceFns->deviceRead(uart_current); |
---|
109 | if (c == 3) { |
---|
110 | gdb_hello_index = 0; |
---|
111 | return 1; |
---|
112 | } else if (gdb_hello[gdb_hello_index] == (char) c) { |
---|
113 | ++gdb_hello_index; |
---|
114 | if (gdb_hello[gdb_hello_index] == '\0') { |
---|
115 | gdb_hello_index = 0; |
---|
116 | return 1; |
---|
117 | } |
---|
118 | } else { |
---|
119 | gdb_hello_index = 0; |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | return 0; |
---|
124 | } |
---|
125 | |
---|
126 | static void |
---|
127 | nop(const rtems_raw_irq_connect_data* notused) |
---|
128 | { |
---|
129 | } |
---|
130 | |
---|
131 | static int |
---|
132 | isOn(const rtems_raw_irq_connect_data* notused) |
---|
133 | { |
---|
134 | return 1; |
---|
135 | } |
---|
136 | |
---|
137 | int i386_stub_glue_uart(void) |
---|
138 | { |
---|
139 | if (port_current == NULL) |
---|
140 | return -1; |
---|
141 | return uart_current; |
---|
142 | } |
---|
143 | |
---|
144 | /* |
---|
145 | * Initialize glue code linking i386-stub with the rest of |
---|
146 | * the system |
---|
147 | */ |
---|
148 | void |
---|
149 | i386_stub_glue_init(int uart) |
---|
150 | { |
---|
151 | rtems_device_minor_number minor = (rtems_device_minor_number) uart; |
---|
152 | |
---|
153 | port_current = console_find_console_entry(NULL, 0, &minor); |
---|
154 | |
---|
155 | if (port_current == NULL) { |
---|
156 | printk("GDB: invalid minor number for UART\n"); |
---|
157 | return; |
---|
158 | } |
---|
159 | |
---|
160 | uart_current = uart; |
---|
161 | |
---|
162 | /* Intialise the UART, assuming polled drivers */ |
---|
163 | port_current->pDeviceFns->deviceInitialize(uart); |
---|
164 | } |
---|
165 | |
---|
166 | static void BSP_uart_on(const rtems_raw_irq_connect_data* used) |
---|
167 | { |
---|
168 | bsp_interrupt_vector_enable(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
169 | } |
---|
170 | |
---|
171 | static void BSP_uart_off(const rtems_raw_irq_connect_data* used) |
---|
172 | { |
---|
173 | bsp_interrupt_vector_disable(used->idtIndex - BSP_IRQ_VECTOR_BASE); |
---|
174 | } |
---|
175 | |
---|
176 | /* |
---|
177 | * In order to have a possibility to break into |
---|
178 | * running program, one has to call this function |
---|
179 | */ |
---|
180 | void i386_stub_glue_init_breakin(void) |
---|
181 | { |
---|
182 | rtems_raw_irq_connect_data uart_raw_irq_data; |
---|
183 | |
---|
184 | if (port_current == NULL) { |
---|
185 | printk("GDB: no port initialised\n"); |
---|
186 | return; |
---|
187 | } |
---|
188 | |
---|
189 | if ((port_current->ulIntVector == 0) || (port_current->ulIntVector > 16)) { |
---|
190 | printk("GDB: no UART interrupt support\n"); |
---|
191 | } |
---|
192 | else { |
---|
193 | uart_vector = port_current->ulIntVector; |
---|
194 | uart_raw_irq_data.idtIndex = port_current->ulIntVector + BSP_IRQ_VECTOR_BASE; |
---|
195 | |
---|
196 | if (!i386_get_current_idt_entry(&uart_raw_irq_data)) { |
---|
197 | printk("GBD: cannot get idt entry\n"); |
---|
198 | rtems_fatal_error_occurred(1); |
---|
199 | } |
---|
200 | |
---|
201 | if (!i386_delete_idt_entry(&uart_raw_irq_data)) { |
---|
202 | printk("GDB: cannot delete idt entry\n"); |
---|
203 | rtems_fatal_error_occurred(1); |
---|
204 | } |
---|
205 | |
---|
206 | uart_raw_irq_data.on = BSP_uart_on; |
---|
207 | uart_raw_irq_data.off = BSP_uart_off; |
---|
208 | |
---|
209 | /* Install ISR */ |
---|
210 | uart_raw_irq_data.idtIndex = port_current->ulIntVector + BSP_IRQ_VECTOR_BASE; |
---|
211 | uart_raw_irq_data.hdl = i386_gdb_uart_isr; |
---|
212 | |
---|
213 | if (!i386_set_idt_entry (&uart_raw_irq_data)) { |
---|
214 | printk("GDB: raw exception handler connection failed\n"); |
---|
215 | rtems_fatal_error_occurred(1); |
---|
216 | } |
---|
217 | |
---|
218 | /* Enable interrupts, this is a bit of a hack because we |
---|
219 | * have to know the device but there is no other call. */ |
---|
220 | (*port_current->setRegister)(port_current->ulCtrlPort1, 1, 0x01); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | int |
---|
225 | putDebugChar(int ch) |
---|
226 | { |
---|
227 | if (port_current != NULL) { |
---|
228 | port_current->pDeviceFns->deviceWritePolled(uart_current, ch); |
---|
229 | } |
---|
230 | return 1; |
---|
231 | } |
---|
232 | |
---|
233 | int getDebugChar(void) |
---|
234 | { |
---|
235 | int c = -1; |
---|
236 | |
---|
237 | if (port_current != NULL) { |
---|
238 | while (c < 0) |
---|
239 | c = port_current->pDeviceFns->deviceRead(uart_current); |
---|
240 | } |
---|
241 | |
---|
242 | return c; |
---|
243 | } |
---|
244 | |
---|
245 | void exceptionHandler(int vector, void (*handler)(void)) |
---|
246 | { |
---|
247 | rtems_raw_irq_connect_data excep_raw_irq_data; |
---|
248 | |
---|
249 | excep_raw_irq_data.idtIndex = vector; |
---|
250 | |
---|
251 | if(!i386_get_current_idt_entry(&excep_raw_irq_data)) |
---|
252 | { |
---|
253 | printk("GDB: cannot get idt entry\n"); |
---|
254 | rtems_fatal_error_occurred(1); |
---|
255 | } |
---|
256 | |
---|
257 | if(!i386_delete_idt_entry(&excep_raw_irq_data)) |
---|
258 | { |
---|
259 | printk("GDB: cannot delete idt entry\n"); |
---|
260 | rtems_fatal_error_occurred(1); |
---|
261 | } |
---|
262 | |
---|
263 | excep_raw_irq_data.on = nop; |
---|
264 | excep_raw_irq_data.off = nop; |
---|
265 | excep_raw_irq_data.isOn = isOn; |
---|
266 | excep_raw_irq_data.hdl = handler; |
---|
267 | |
---|
268 | if (!i386_set_idt_entry (&excep_raw_irq_data)) { |
---|
269 | printk("GDB: raw exception handler connection failed\n"); |
---|
270 | rtems_fatal_error_occurred(1); |
---|
271 | } |
---|
272 | return; |
---|
273 | } |
---|