idt.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* idt.c - routines for constructing IDT fot the GDB stub */
  2. /*
  3. * Copyright (C) 2006 Lubomir Kundrak
  4. *
  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.
  9. *
  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.
  14. *
  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.
  18. */
  19. #include <grub/machine/memory.h>
  20. #include <grub/misc.h>
  21. #include <grub/cpu/gdb.h>
  22. #include <grub/gdb.h>
  23. static struct grub_cpu_interrupt_gate grub_gdb_idt[GRUB_GDB_LAST_TRAP + 1]
  24. __attribute__ ((aligned(16)));
  25. /* Sets up a gate descriptor in the IDT table. */
  26. static void
  27. grub_idt_gate (struct grub_cpu_interrupt_gate *gate, void (*offset) (void),
  28. grub_uint16_t selector, grub_uint8_t type, grub_uint8_t dpl)
  29. {
  30. gate->offset_lo = (int) offset & 0xffff;
  31. gate->selector = selector & 0xffff;
  32. gate->unused = 0;
  33. gate->gate = (type & 0x1f) | ((dpl & 0x3) << 5) | 0x80;
  34. gate->offset_hi = ((int) offset >> 16) & 0xffff;
  35. }
  36. static struct grub_cpu_idt_descriptor grub_gdb_orig_idt_desc
  37. __attribute__ ((aligned(16)));
  38. static struct grub_cpu_idt_descriptor grub_gdb_idt_desc
  39. __attribute__ ((aligned(16)));
  40. /* Set up interrupt and trap handler descriptors in IDT. */
  41. void
  42. grub_gdb_idtinit (void)
  43. {
  44. int i;
  45. grub_uint16_t seg;
  46. asm volatile ("xorl %%eax, %%eax\n"
  47. "mov %%cs, %%ax\n" :"=a" (seg));
  48. for (i = 0; i <= GRUB_GDB_LAST_TRAP; i++)
  49. {
  50. grub_idt_gate (&grub_gdb_idt[i],
  51. grub_gdb_trapvec[i], seg,
  52. GRUB_CPU_TRAP_GATE, 0);
  53. }
  54. grub_gdb_idt_desc.base = (grub_addr_t) grub_gdb_idt;
  55. grub_gdb_idt_desc.limit = sizeof (grub_gdb_idt) - 1;
  56. asm volatile ("sidt %0" : : "m" (grub_gdb_orig_idt_desc));
  57. asm volatile ("lidt %0" : : "m" (grub_gdb_idt_desc));
  58. }
  59. void
  60. grub_gdb_idtrestore (void)
  61. {
  62. asm volatile ("lidt %0" : : "m" (grub_gdb_orig_idt_desc));
  63. }
  64. void
  65. grub_gdb_breakpoint (void)
  66. {
  67. asm volatile ("int $3");
  68. }