source: libcaca/trunk/kernel/boot/idt.c @ 4158

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