idt.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2011-2013 Kevin Lange
  5. * Copyright (C) 2015 Dale Weiler
  6. *
  7. * Interrupt Descriptor Tables
  8. *
  9. */
  10. #include <kernel/system.h>
  11. #include <kernel/logging.h>
  12. typedef struct {
  13. uint16_t base_low;
  14. uint16_t sel;
  15. uint8_t zero;
  16. uint8_t flags;
  17. uint16_t base_high;
  18. } __attribute__((packed)) idt_entry_t;
  19. typedef struct {
  20. uint16_t limit;
  21. uintptr_t base;
  22. } __attribute__((packed)) idt_pointer_t;
  23. /* In the future we may need to put a lock on the access of this */
  24. static struct {
  25. idt_entry_t entries[256];
  26. idt_pointer_t pointer;
  27. } idt __attribute__((used));
  28. #define ENTRY(X) (idt.entries[(X)])
  29. typedef void (*idt_gate_t)(void);
  30. extern void idt_load(uintptr_t);
  31. void idt_set_gate(uint8_t num, idt_gate_t base, uint16_t sel, uint8_t flags) {
  32. ENTRY(num).base_low = ((uintptr_t)base & 0xFFFF);
  33. ENTRY(num).base_high = ((uintptr_t)base >> 16) & 0xFFFF;
  34. ENTRY(num).sel = sel;
  35. ENTRY(num).zero = 0;
  36. ENTRY(num).flags = flags | 0x60;
  37. }
  38. void idt_install(void) {
  39. idt_pointer_t * idtp = &idt.pointer;
  40. idtp->limit = sizeof idt.entries - 1;
  41. idtp->base = (uintptr_t)&ENTRY(0);
  42. memset(&ENTRY(0), 0, sizeof idt.entries);
  43. idt_load((uintptr_t)idtp);
  44. }