| 1 |
/* |
| 2 |
* libcaca Colour ASCII-Art library |
| 3 |
* Copyright (c) 2006 Sam Hocevar <[email protected]> |
| 4 |
* 2009 Jean-Yves Lamoureux <[email protected]> |
| 5 |
* All Rights Reserved |
| 6 |
* |
| 7 |
* This library is free software. It comes without any warranty, to |
| 8 |
* the extent permitted by applicable law. You can redistribute it |
| 9 |
* and/or modify it under the terms of the Do What the Fuck You Want |
| 10 |
* to Public License, Version 2, as published by Sam Hocevar. See |
| 11 |
* http://www.wtfpl.net/ for more details. |
| 12 |
*/ |
| 13 |
|
| 14 |
/* |
| 15 |
*'freely' inspired by http://jojo.ouvaton.org/dossiers/boot_sector/tutorial02.html |
| 16 |
* (actually, that's mostly copied, with minor compilation fixes) |
| 17 |
*/ |
| 18 |
#include "kernel.h" |
| 19 |
#include "klibc.h" |
| 20 |
|
| 21 |
#define IDTBASE 0 /* GDT Physical address */ |
| 22 |
#define IDTSIZE 256 /* Max descriptor count */ |
| 23 |
#define INTGATE 0x8E00 /* Interruptions */ |
| 24 |
#define TRAPGATE 0x8F00 /* Syscalls */ |
| 25 |
#define TASKGATE 0x8500 /* Task switching */ |
| 26 |
|
| 27 |
/* segment descriptor */ |
| 28 |
struct idtdesc |
| 29 |
{ |
| 30 |
u16 offset0_15; |
| 31 |
u16 select; |
| 32 |
u16 type; |
| 33 |
u16 offset16_31; |
| 34 |
} __attribute__ ((packed)); |
| 35 |
|
| 36 |
/* IDTR register */ |
| 37 |
struct idtr |
| 38 |
{ |
| 39 |
u16 limite; |
| 40 |
u32 base; |
| 41 |
} __attribute__ ((packed)); |
| 42 |
|
| 43 |
struct idtr kidtr; |
| 44 |
|
| 45 |
/* IDT table */ |
| 46 |
struct idtdesc kidt[IDTSIZE] = { {0, 0, 0, 0} }; |
| 47 |
|
| 48 |
/* pointer on a free IDT entry */ |
| 49 |
unsigned int kidtptr = 0; |
| 50 |
|
| 51 |
void default_int(void); |
| 52 |
void k_int0(void); |
| 53 |
void k_int1(void); |
| 54 |
void k_int2(void); |
| 55 |
void k_int3(void); |
| 56 |
void k_int4(void); |
| 57 |
void k_int5(void); |
| 58 |
void k_int6(void); |
| 59 |
void k_int7(void); |
| 60 |
void k_int8(void); |
| 61 |
void k_int9(void); |
| 62 |
void k_int10(void); |
| 63 |
void k_int11(void); |
| 64 |
void k_int12(void); |
| 65 |
void k_int13(void); |
| 66 |
void k_int14(void); |
| 67 |
void k_int15(void); |
| 68 |
void k_int16(void); |
| 69 |
void k_int17(void); |
| 70 |
void k_int18(void); |
| 71 |
void k_irq0(void); |
| 72 |
void k_irq1(void); |
| 73 |
void k_irq2(void); |
| 74 |
void k_irq3(void); |
| 75 |
void k_irq4(void); |
| 76 |
void k_irq5(void); |
| 77 |
void k_irq6(void); |
| 78 |
void k_irq7(void); |
| 79 |
void k_irq8(void); |
| 80 |
|
| 81 |
|
| 82 |
void init_idt_desc(u32 offset, u16 select, u16 type, struct idtdesc *desc) |
| 83 |
{ |
| 84 |
desc->offset0_15 = (offset & 0xffff); |
| 85 |
desc->select = select; |
| 86 |
desc->type = type; |
| 87 |
desc->offset16_31 = (offset & 0xffff0000) >> 16; |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
void add_idt_desc(struct idtdesc desc) |
| 93 |
{ |
| 94 |
kidt[kidtptr++] = desc; |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
void init_idt(void) |
| 100 |
{ |
| 101 |
struct idtdesc desc; |
| 102 |
int i; |
| 103 |
|
| 104 |
|
| 105 |
for (i = 0; i < IDTSIZE; i++) |
| 106 |
{ |
| 107 |
init_idt_desc((u32) default_int, 0x08, INTGATE, &desc); |
| 108 |
add_idt_desc(desc); |
| 109 |
} |
| 110 |
|
| 111 |
init_idt_desc((u32) k_int0, 0x08, INTGATE, &kidt[0]); |
| 112 |
init_idt_desc((u32) k_int1, 0x08, INTGATE, &kidt[1]); |
| 113 |
init_idt_desc((u32) k_int2, 0x08, INTGATE, &kidt[2]); |
| 114 |
init_idt_desc((u32) k_int3, 0x08, INTGATE, &kidt[3]); |
| 115 |
init_idt_desc((u32) k_int4, 0x08, INTGATE, &kidt[4]); |
| 116 |
init_idt_desc((u32) k_int5, 0x08, INTGATE, &kidt[5]); |
| 117 |
init_idt_desc((u32) k_int6, 0x08, INTGATE, &kidt[6]); |
| 118 |
init_idt_desc((u32) k_int7, 0x08, INTGATE, &kidt[7]); |
| 119 |
init_idt_desc((u32) k_int8, 0x08, INTGATE, &kidt[8]); |
| 120 |
init_idt_desc((u32) k_int9, 0x08, INTGATE, &kidt[9]); |
| 121 |
init_idt_desc((u32) k_int10, 0x08, INTGATE, &kidt[10]); |
| 122 |
init_idt_desc((u32) k_int11, 0x08, INTGATE, &kidt[11]); |
| 123 |
init_idt_desc((u32) k_int12, 0x08, INTGATE, &kidt[12]); |
| 124 |
init_idt_desc((u32) k_int13, 0x08, INTGATE, &kidt[13]); |
| 125 |
init_idt_desc((u32) k_int14, 0x08, INTGATE, &kidt[14]); |
| 126 |
init_idt_desc((u32) k_int15, 0x08, INTGATE, &kidt[15]); |
| 127 |
init_idt_desc((u32) k_int16, 0x08, INTGATE, &kidt[16]); |
| 128 |
init_idt_desc((u32) k_int17, 0x08, INTGATE, &kidt[17]); |
| 129 |
init_idt_desc((u32) k_int18, 0x08, INTGATE, &kidt[18]); |
| 130 |
|
| 131 |
init_idt_desc((u32) k_irq0, 0x08, INTGATE, &kidt[32]); |
| 132 |
init_idt_desc((u32) k_irq1, 0x08, INTGATE, &kidt[33]); |
| 133 |
init_idt_desc((u32) k_irq2, 0x08, INTGATE, &kidt[34]); |
| 134 |
init_idt_desc((u32) k_irq3, 0x08, INTGATE, &kidt[35]); |
| 135 |
init_idt_desc((u32) k_irq4, 0x08, INTGATE, &kidt[36]); |
| 136 |
init_idt_desc((u32) k_irq5, 0x08, INTGATE, &kidt[37]); |
| 137 |
init_idt_desc((u32) k_irq6, 0x08, INTGATE, &kidt[38]); |
| 138 |
init_idt_desc((u32) k_irq7, 0x08, INTGATE, &kidt[39]); |
| 139 |
init_idt_desc((u32) k_irq8, 0x08, INTGATE, &kidt[40]); |
| 140 |
|
| 141 |
kidtr.limite = IDTSIZE * 8; |
| 142 |
kidtr.base = IDTBASE; |
| 143 |
|
| 144 |
memcpy((void *)kidtr.base, kidt, kidtr.limite); |
| 145 |
#define printf(format, ...) { char __str[255]; sprintf (__str, format, __VA_ARGS__); print(__str);} |
| 146 |
|
| 147 |
printf("Loading IDT from 0x%x\n", kidtr); |
| 148 |
#undef printf |
| 149 |
|
| 150 |
asm("lidtl (kidtr)"); |
| 151 |
} |