Skip to content

Commit 6d641e0

Browse files
committed
Fixed PIT support.
1 parent efc5d73 commit 6d641e0

File tree

10 files changed

+121
-52
lines changed

10 files changed

+121
-52
lines changed

arch/idt.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ static uint16_t idt_size = 0x800;
1313
static uint8_t test_success = 0;
1414
static uint32_t test_timeout = 0x1000;
1515

16+
extern void _set_idtr();
17+
1618
void __idt_default_handler();
1719
void __idt_test_handler();
1820

21+
static uint8_t __idt_setup = 0;
22+
1923
void idt_init()
2024
{
2125
idt_location = 0x402000;
2226
mprint("Location: 0x%x\n", idt_location);
2327
idtr_location = 0x401F00;
2428
mprint("IDTR location: 0x%x\n", idtr_location);
29+
__idt_setup = 1;
2530
for(uint8_t i = 0; i < 255; i++)
2631
{
2732
idt_register_interrupt(i, (uint32_t)&__idt_default_handler);
@@ -45,7 +50,7 @@ void idt_init()
4550
}
4651
}
4752
if(!test_success)
48-
panic("Link test failed.");
53+
panic("IDT link is offline (timeout).");
4954
return;
5055
}
5156

@@ -56,36 +61,19 @@ void __idt_test_handler()
5661
INT_END;
5762
}
5863

59-
void __idt_default_handler()
60-
{
61-
IRQ_START;
62-
panic("Unhandled interrupt!\n");
63-
//send_eoi(16);
64-
IRQ_END;
65-
}
66-
6764
void idt_register_interrupt(uint8_t i, uint32_t callback)
6865
{
69-
idt_descriptor desc = {0};
70-
desc.offset_0_15 = (uint16_t)(callback & 0x0000ffff);
71-
desc.selector = 0x8;
72-
desc.zero = 0;
73-
desc.type_attr = 0 | IDT_32BIT_INTERRUPT_GATE | IDT_PRESENT;
74-
desc.offset_16_31 = (uint16_t)(callback >> 16);
75-
/*mprint("Descriptor: id%d offset 0x%x, orig 0x%x\n",
76-
i,
77-
desc.offset_16_31 << 16 | desc.offset_0_15,
78-
(uint32_t)callback);*/
79-
add_idt_descriptor(i, desc);
66+
if(!__idt_setup) panic("Invalid IDT!");
67+
*(uint16_t*)(idt_location + 8*i + 0) = (uint16_t)(callback & 0x0000ffff);
68+
*(uint16_t*)(idt_location + 8*i + 2) = (uint16_t)0x8;
69+
*(uint8_t*) (idt_location + 8*i + 4) = 0x00;
70+
*(uint8_t*) (idt_location + 8*i + 5) = 0x8e;//0 | IDT_32BIT_INTERRUPT_GATE | IDT_PRESENT;
71+
*(uint16_t*)(idt_location + 8*i + 6) = (uint16_t)((callback & 0xffff0000) >> 16);
72+
if(test_success) mprint("Registered INT#%d\n", i);
73+
return;
8074
}
8175

8276
void add_idt_descriptor(uint8_t id, idt_descriptor desc)
8377
{
84-
*(uint16_t*)(idt_location + sizeof(idt_descriptor)*id + 0) = desc.offset_0_15;
85-
*(uint16_t*)(idt_location + sizeof(idt_descriptor)*id + 2) = desc.selector;
86-
*(uint8_t*) (idt_location + sizeof(idt_descriptor)*id + 4) = desc.zero;
87-
*(uint8_t*) (idt_location + sizeof(idt_descriptor)*id + 5) = desc.type_attr;
88-
*(uint16_t*)(idt_location + sizeof(idt_descriptor)*id + 6) = desc.offset_16_31;
89-
if(test_success) mprint("Registered INT#%d\n", id);
90-
return;
78+
panic("Deprecated function called!");
9179
}

boot.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ stack_top:
2121

2222
# az elf-ben a .text szekcio tartalmazza a kodot amit futtatni fogunk
2323
.section .text
24+
25+
.global __idt_default_handler
26+
.type __idt_default_handler, @function
27+
__idt_default_handler:
28+
pushal
29+
mov $0x20, %al
30+
mov $0x20, %dx
31+
out %al, %dx
32+
#call _test
33+
popal
34+
iretl
35+
2436
.global _set_gdtr
2537
.type _set_gdtr, @function
2638
_set_gdtr:

display/textmode/dispi_textmode.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ inline uint16_t __textmode_create_entry(char c, uint8_t color)
2020

2121
static inline void __textmode_scrollup()
2222
{
23-
uint16_t nullentry = __textmode_create_entry(' ', __textmode_make_color(d.con.fgcol, d.con.bgcol));
2423
for(int y = 0; y < TEXTMODE_HEIGHT; y++)
2524
{
2625
memcpy(VGA_MEMORY + y*TEXTMODE_WIDTH*2 ,

include/hal.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
#define INT_END asm volatile("popa"); \
99
asm volatile("iret");
1010

11-
#define IRQ_START asm volatile("add $12, %esp");\
12-
asm volatile("pushal");
1311

14-
#define IRQ_END asm volatile("popal"); \
15-
asm volatile("iretl");
12+
13+
#define IRQ_START asm volatile("pusha");
14+
15+
#define IRQ_END asm volatile("popa"); \
16+
asm volatile("iret");
17+
18+
1619
extern void hal_init();
1720

1821
extern void send_eoi(uint8_t irq);

include/pic.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,72 @@
1111

1212
#include <stdint.h>
1313

14+
//-----------------------------------------------
15+
// Controller Registers
16+
//-----------------------------------------------
17+
18+
// PIC 1 register port addresses
19+
#define PIC1_REG_COMMAND 0x20
20+
#define PIC1_REG_STATUS 0x20
21+
#define PIC1_REG_DATA 0x21
22+
#define PIC1_REG_IMR 0x21
23+
24+
// PIC 2 register port addresses
25+
#define PIC2_REG_COMMAND 0xA0
26+
#define PIC2_REG_STATUS 0xA0
27+
#define PIC2_REG_DATA 0xA1
28+
#define PIC2_REG_IMR 0xA1
29+
30+
//-----------------------------------------------
31+
// Initialization Command Bit Masks
32+
//-----------------------------------------------
33+
34+
// Initialization Control Word 1 bit masks
35+
#define PIC_ICW1_MASK_IC4 0x1 //00000001
36+
#define PIC_ICW1_MASK_SNGL 0x2 //00000010
37+
#define PIC_ICW1_MASK_ADI 0x4 //00000100
38+
#define PIC_ICW1_MASK_LTIM 0x8 //00001000
39+
#define PIC_ICW1_MASK_INIT 0x10 //00010000
40+
41+
// Initialization Control Words 2 and 3 do not require bit masks
42+
43+
// Initialization Control Word 4 bit masks
44+
#define PIC_ICW4_MASK_UPM 0x1 //00000001
45+
#define PIC_ICW4_MASK_AEOI 0x2 //00000010
46+
#define PIC_ICW4_MASK_MS 0x4 //00000100
47+
#define PIC_ICW4_MASK_BUF 0x8 //00001000
48+
#define PIC_ICW4_MASK_SFNM 0x10 //00010000
49+
50+
//-----------------------------------------------
51+
// Initialization Command 1 control bits
52+
//-----------------------------------------------
53+
54+
#define PIC_ICW1_IC4_EXPECT 1 //1
55+
#define PIC_ICW1_IC4_NO 0 //0
56+
#define PIC_ICW1_SNGL_YES 2 //10
57+
#define PIC_ICW1_SNGL_NO 0 //00
58+
#define PIC_ICW1_ADI_CALLINTERVAL4 4 //100
59+
#define PIC_ICW1_ADI_CALLINTERVAL8 0 //000
60+
#define PIC_ICW1_LTIM_LEVELTRIGGERED 8 //1000
61+
#define PIC_ICW1_LTIM_EDGETRIGGERED 0 //0000
62+
#define PIC_ICW1_INIT_YES 0x10 //10000
63+
#define PIC_ICW1_INIT_NO 0 //00000
64+
65+
//-----------------------------------------------
66+
// Initialization Command 4 control bits
67+
//-----------------------------------------------
68+
69+
#define PIC_ICW4_UPM_86MODE 1 //1
70+
#define PIC_ICW4_UPM_MCSMODE 0 //0
71+
#define PIC_ICW4_AEOI_AUTOEOI 2 //10
72+
#define PIC_ICW4_AEOI_NOAUTOEOI 0 //0
73+
#define PIC_ICW4_MS_BUFFERMASTER 4 //100
74+
#define PIC_ICW4_MS_BUFFERSLAVE 0 //0
75+
#define PIC_ICW4_BUF_MODEYES 8 //1000
76+
#define PIC_ICW4_BUF_MODENO 0 //0
77+
#define PIC_ICW4_SFNM_NESTEDMODE 0x10 //10000
78+
#define PIC_ICW4_SFNM_NOTNESTED 0 //a binary 2
79+
1480
extern void pic_init();
1581
extern void pic_send_eoi(uint8_t irq);
1682

kernel.o

152 Bytes
Binary file not shown.

kernel/hal.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,10 @@ void set_int(int i, uint32_t addr)
2727
uint8_t inportb(uint16_t portid)
2828
{
2929
uint8_t ret;
30-
asm volatile("push %eax");
31-
asm volatile("push %edx");
32-
asm volatile("movw %0, %%dx":"=a"(portid));
33-
asm volatile("in %dx, %al");
34-
asm volatile("movb %%al, %0":"=a"(ret));
35-
asm volatile("pop %edx");
36-
asm volatile("pop %eax");
30+
asm volatile("inb %%dx, %%al":"=a"(ret):"d"(portid));
3731
return ret;
3832
}
3933
void outportb(uint16_t portid, uint8_t value)
4034
{
41-
asm volatile("push %eax");
42-
asm volatile("push %edx");
43-
asm volatile("mov %0, %%al":"=a"(value));
44-
asm volatile("mov %0, %%dx":"=a"(portid));
45-
asm volatile("out %al, %dx");
46-
asm volatile("pop %edx");
47-
asm volatile("pop %eax");
35+
asm volatile("outb %%al, %%dx": :"d" (portid), "a" (value));
4836
}

kernel/pic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ void pic_init()
3333
outportb(PIC_SLAVE_DATA, 0x01);
3434

3535
mprint("Resetting masks\n");
36-
outportb(PIC_MASTER_DATA, m1);
37-
outportb(PIC_SLAVE_DATA, m2);
36+
outportb(PIC_MASTER_DATA, 0);
37+
outportb(PIC_SLAVE_DATA, 0);
3838
mprint("Init done.\n");
3939
}
4040

kernel/pit.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ MODULE("PIT");
88

99
void pit_irq()
1010
{
11-
IRQ_START;
12-
mprint("PIT IRQ!\n");
11+
asm volatile("add $0x1c, %esp");
12+
asm volatile("pusha");
13+
//mprint("PIT IRQ!\n");
1314
send_eoi(0);
14-
IRQ_END;
15+
//asm volatile("outb %%al, %%dx": :"a"(0x20),"d"(0x20));
16+
asm volatile("popa");
17+
asm volatile("iret");
1518
}
1619

1720
static inline void __pit_send_cmd(uint8_t cmd)
@@ -56,7 +59,8 @@ static void pit_start_counter (uint32_t freq, uint8_t counter, uint8_t mode) {
5659

5760
void pit_init()
5861
{
59-
set_int(32, (uint32_t)pit_irq);
62+
mprint("Registering IRQ#0=INT#32 as PIT_IRQ\n");
63+
set_int(32, pit_irq);
6064
pit_start_counter (200,PIT_OCW_COUNTER_0, PIT_OCW_MODE_SQUAREWAVEGEN);
6165
mprint("Init done.\n");
6266
}

main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,14 @@ void kernel_main()
4747
/* Setup memory manager */
4848
/* Setup paging. */
4949
/* Enable tasking. */
50+
#ifdef __cplusplus
51+
mprint("C++ version, may cause malfunction!\n");
52+
#endif
5053
panic("Reached end of main(), but no init was started.");
54+
for(;;);
55+
}
56+
57+
void _test()
58+
{
59+
mprint("Unhandled IRQ\n");
5160
}

0 commit comments

Comments
 (0)