~cypheon/xos

xos/atpic.c -rw-r--r-- 3.3 KiB
567db604 — Johann Rudloff Implement scanning and allocation of physical memory. 7 years ago
                                                                                
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
#include <sys/atpic.h>
#include <sys/io.h>
#include <sys/memory.h>
#include <sys/types.h>

#define DEBUG
#include <debug.h>

struct idtr {
  uint16_t limit;
  uint32_t base;
} __attribute__ ((__packed__));

struct idt_desc {
  uint16_t offset_lo;
  uint16_t selector;
  uint8_t reserved;
  uint8_t flags;
  uint16_t offset_hi;
} __attribute__ ((__packed__));

struct trap_frame {
  uint32_t edi;
  uint32_t esi;
  uint32_t ebp;
  uint32_t esp;
  uint32_t ebx;
  uint32_t edx;
  uint32_t ecx;
  uint32_t eax;
  uint32_t intr;
} __attribute__ ((__packed__));

#define IDT_FLAG_PRESENT (1 << 7)
#define IDT_FLAG_DPL0 (0)
#define IDT_FLAG_DPL3 (1 << 6 | 1 << 5)
#define IDT_FLAG_S (1 << 4)
#define IDT_FLAG_GATE_TYPE_INT (0xe)
#define IDT_FLAG_GATE_TYPE_TRAP (0xf)

static struct idt_desc make_idt_desc(uint16_t selector, uint32_t offset, uint8_t flags) {
  struct idt_desc r = {
    offset_lo: (offset & 0xffff),
    offset_hi: ((offset >> 16) & 0xffff),
    selector: selector,
    reserved: 0,
    flags: flags,
  };
  return r;
}

void atpic_int_entry(struct trap_frame *frame) {
  uint32_t intnum = frame->intr;

  if (intnum > 1) {
    khexprint("received interrupt", intnum);
  }
  if (intnum == 1) {
    // keyboard
    uint8_t c, s;
    do {
      c = inb(0x60);
      s = inb(0x64);
      khexprint("key", c);
      /*khexprint("status", s);*/
    } while (s & 0x01);
  }

  if (intnum > 0x07) {
    outb(0xa0, 0x20);
  }
  outb(0x20, 0x20);
}

extern void handle_interrupt(void);
extern void syscall_intr_handler(void);

#define DECL_INTR_HANDLER(i) extern void atpic_intr_handler##i(void);
DECL_INTR_HANDLER(0)
DECL_INTR_HANDLER(1)
DECL_INTR_HANDLER(2)
DECL_INTR_HANDLER(3)
DECL_INTR_HANDLER(4)
DECL_INTR_HANDLER(5)
DECL_INTR_HANDLER(6)
DECL_INTR_HANDLER(7)
DECL_INTR_HANDLER(8)
DECL_INTR_HANDLER(9)
DECL_INTR_HANDLER(10)
DECL_INTR_HANDLER(11)
DECL_INTR_HANDLER(12)
DECL_INTR_HANDLER(13)
DECL_INTR_HANDLER(14)
DECL_INTR_HANDLER(15)

#define USE_INTR_HANDLER(intr) idt[0x20 + (intr)] = make_idt_desc(0x08, (uint32_t)atpic_intr_handler##intr, IDT_FLAG_PRESENT | IDT_FLAG_DPL0 | IDT_FLAG_GATE_TYPE_INT);
void setup_idt(void) {
  struct idt_desc *idt = kmalloc(8 * 256);
  for (int i=0; i<256; ++i) {
    idt[i] = make_idt_desc(0x08, (uint32_t)handle_interrupt, IDT_FLAG_PRESENT | IDT_FLAG_DPL0 | IDT_FLAG_GATE_TYPE_INT);
  }
  USE_INTR_HANDLER(0);
  USE_INTR_HANDLER(1);
  USE_INTR_HANDLER(2);
  USE_INTR_HANDLER(3);
  USE_INTR_HANDLER(4);
  USE_INTR_HANDLER(5);
  USE_INTR_HANDLER(6);
  USE_INTR_HANDLER(7);
  USE_INTR_HANDLER(8);
  USE_INTR_HANDLER(9);
  USE_INTR_HANDLER(10);
  USE_INTR_HANDLER(11);
  USE_INTR_HANDLER(12);
  USE_INTR_HANDLER(13);
  USE_INTR_HANDLER(14);
  USE_INTR_HANDLER(15);

  idt[0x80] = make_idt_desc(0x08, (uint32_t)syscall_intr_handler, IDT_FLAG_PRESENT | IDT_FLAG_DPL3 | IDT_FLAG_GATE_TYPE_INT);

  struct idtr idt_load;
  idt_load.limit = 8 * 256 - 1;
  idt_load.base = (uint32_t)idt;

  outb(0x20,0x11);
  io_wait();
  outb(0xa0,0x11);
  io_wait();

  outb(0x21,0x20);
  io_wait();
  outb(0xa1,0x28);
  io_wait();

  outb(0x21,4);
  io_wait();
  outb(0xa1,2);
  io_wait();

  outb(0x21,1);
  io_wait();
  outb(0xa1,1);
  io_wait();

  outb(0x21,0xfd);
  io_wait();
  outb(0xa1,0xffj);
  io_wait();
  /*outb(0x21,0x00);*/
  /*io_wait();*/
  /*outb(0xa1,0x00);*/
  /*io_wait();*/

  asm volatile (
      "lidt %0\n"
      "sti"
      :
      : "m"(idt_load)
      : "memory", "eax");
}