1 /* idt.c - routines for constructing IDT fot the GDB stub */
3 * Copyright (C) 2006 Lubomir Kundrak
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <grub/machine/memory.h>
21 #include <grub/misc.h>
22 #include <grub/cpu/gdb.h>
25 static struct grub_cpu_interrupt_gate grub_gdb_idt
[GRUB_GDB_LAST_TRAP
+ 1]
26 __attribute__ ((aligned(16)));
28 /* Sets up a gate descriptor in the IDT table. */
30 grub_idt_gate (struct grub_cpu_interrupt_gate
*gate
, void (*offset
) (void),
31 grub_uint16_t selector
, grub_uint8_t type
, grub_uint8_t dpl
)
33 gate
->offset_lo
= (int) offset
& 0xffff;
34 gate
->selector
= selector
& 0xffff;
36 gate
->gate
= (type
& 0x1f) | ((dpl
& 0x3) << 5) | 0x80;
37 gate
->offset_hi
= ((int) offset
>> 16) & 0xffff;
40 static struct grub_cpu_idt_descriptor grub_gdb_orig_idt_desc
41 __attribute__ ((aligned(16)));
42 static struct grub_cpu_idt_descriptor grub_gdb_idt_desc
43 __attribute__ ((aligned(16)));
45 /* Set up interrupt and trap handler descriptors in IDT. */
47 grub_gdb_idtinit (void)
52 asm volatile ("xorl %%eax, %%eax\n"
53 "mov %%cs, %%ax\n" :"=a" (seg
));
55 for (i
= 0; i
<= GRUB_GDB_LAST_TRAP
; i
++)
57 grub_idt_gate (&grub_gdb_idt
[i
],
58 grub_gdb_trapvec
[i
], seg
,
59 GRUB_CPU_TRAP_GATE
, 0);
62 grub_gdb_idt_desc
.base
= (grub_addr_t
) grub_gdb_idt
;
63 grub_gdb_idt_desc
.limit
= sizeof (grub_gdb_idt
) - 1;
64 asm volatile ("sidt %0" : : "m" (grub_gdb_orig_idt_desc
));
65 asm volatile ("lidt %0" : : "m" (grub_gdb_idt_desc
));
69 grub_gdb_idtrestore (void)
71 asm volatile ("lidt %0" : : "m" (grub_gdb_orig_idt_desc
));
75 grub_gdb_breakpoint (void)
77 asm volatile ("int $3");