Skip to content

Commit 0c79544

Browse files
authored
Interrupts (#89)
* Fix and improve PIC8259 code. * Detect spurious interrupts. * Dispatch interrupts to the application layer. * Initialize PIT8253 and enable timer interrupt.
1 parent ae359e8 commit 0c79544

File tree

26 files changed

+744
-133
lines changed

26 files changed

+744
-133
lines changed

include/jinue/shared/asm/i686.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#define JINUE_KLIMIT 0xc0000000
5858

5959
/** interrupt vector for system call software interrupt */
60-
#define JINUE_I686_SYSCALL_IRQ 0x80
60+
#define JINUE_I686_SYSCALL_INTERRUPT 0x80
6161

6262
/** slow/safe interrupt-based system call implementation */
6363
#define JINUE_I686_HOWSYSCALL_INTERRUPT 0
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2024 Philippe Aubertin.
3+
* All rights reserved.
4+
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* 3. Neither the name of the author nor the names of other contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
21+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
24+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef JINUE_KERNEL_APPLICATION_ASM_TICKS_H
33+
#define JINUE_KERNEL_APPLICATION_ASM_TICKS_H
34+
35+
#define TICKS_PER_SECOND 100
36+
37+
#endif
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2024 Philippe Aubertin.
3+
* All rights reserved.
4+
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* 3. Neither the name of the author nor the names of other contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
21+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
24+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef JINUE_KERNEL_APPLICATION_INTERRUPTS_H
33+
#define JINUE_KERNEL_APPLICATION_INTERRUPTS_H
34+
35+
void hardware_interrupt(int irq);
36+
37+
void spurious_interrupt(void);
38+
39+
void tick_interrupt(void);
40+
41+
#endif

include/kernel/infrastructure/i686/asm/eflags.h

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
#define EFLAGS_ALWAYS_1 (1<<1)
3636

37+
#define EFLAGS_IF (1<<9)
38+
3739
#define EFLAGS_ID (1<<21)
3840

3941
#endif

include/kernel/infrastructure/i686/drivers/asm/pic8259.h

+17-2
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,25 @@
5959
/** ICW4 bit 0: 8086/8088 mode (1) or MCS-80/85 mode (0) */
6060
#define PIC8259_ICW4_UPM (1<<0)
6161

62-
/** ICW4 bit 1: Auto EOI*/
62+
/** ICW4 bit 1: Auto EOI */
6363
#define PIC8259_ICW4_AEOI (1<<1)
6464

65+
/** ICW4 bit 4: special (1) or "regular" (0) fully nested mode */
66+
#define PIC8259_ICW4_SFNM (1<<4)
67+
6568
/** OCW2: non-specific EOI command */
66-
#define PIC8259_EOI 0x20
69+
#define PIC8259_OCW2_EOI 0x20
70+
71+
/** OCW3: read ISR (1) or IRR (0) when RR is also set */
72+
#define PIC8259_OCW3_RIS (1<<0)
73+
74+
/** OCW3: read register command when set */
75+
#define PIC8259_OCW3_RR (1<<1)
76+
77+
/** OCW3: always 1 to select OCW3, otherwise it's OCW2 */
78+
#define PIC8259_OCW3_1 (1<<3)
79+
80+
/** OCW3: read Interrupt Service Register (ISR) */
81+
#define PIC8259_OCW3_READ_ISR (PIC8259_OCW3_1 | PIC8259_OCW3_RR | PIC8259_OCW3_RIS)
6782

6883
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (C) 2024 Philippe Aubertin.
3+
* All rights reserved.
4+
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* 3. Neither the name of the author nor the names of other contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
21+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
24+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_DRIVERS_ASM_PIT8253_H
33+
#define JINUE_KERNEL_INFRASTRUCTURE_I686_DRIVERS_ASM_PIT8253_H
34+
35+
/* I/O addresses */
36+
37+
#define PIT8253_IO_BASE 0x40
38+
39+
#define PIT8253_IO_COUNTER0 (PIT8253_IO_BASE + 0)
40+
41+
#define PIT8253_IO_COUNTER1 (PIT8253_IO_BASE + 1)
42+
43+
#define PIT8253_IO_COUNTER2 (PIT8253_IO_BASE + 2)
44+
45+
#define PIT8253_IO_CW_REG (PIT8253_IO_BASE + 3)
46+
47+
/* Individual flag definitions */
48+
49+
/** BCD (1) or binary (0) counter selection */
50+
#define PIT8253_CW_BCD (1<<0)
51+
52+
#define PIT8253_CW_M0 (1<<1)
53+
54+
#define PIT8253_CW_M1 (1<<2)
55+
56+
#define PIT8253_CW_M2 (1<<3)
57+
58+
#define PIT8253_CW_RL0 (1<<4)
59+
60+
#define PIT8253_CW_RL1 (1<<5)
61+
62+
#define PIT8253_CW_SC0 (1<<6)
63+
64+
#define PIT8253_CW_SC1 (1<<7)
65+
66+
/* Combined flags - select counter */
67+
68+
#define PIT8253_CW_COUNTER0 0
69+
70+
#define PIT8253_CW_COUNTER1 PIT8253_CW_SC0
71+
72+
#define PIT8253_CW_COUNTER2 PIT8253_CW_SC1
73+
74+
/* Combined flags - read/load */
75+
76+
#define PIT8253_CW_LOAD_LSB_MSB (PIT8253_CW_RL1 | PIT8253_CW_RL0)
77+
78+
/* Combined flags - mode */
79+
80+
/** Mode 0: interrupt on terminal count */
81+
#define PIT8253_CW_MODE0 0
82+
83+
/** Mode 1: programmable one-shot */
84+
#define PIT8253_CW_MODE1 PIT8253_CW_M0
85+
86+
/** Mode 2: rate generator */
87+
#define PIT8253_CW_MODE2 PIT8253_CW_M1
88+
89+
/** Mode 3: square wave rate generator */
90+
#define PIT8253_CW_MODE3 (PIT8253_CW_M1 | PIT8253_CW_M0)
91+
92+
/** Mode 4: software-triggered strobe */
93+
#define PIT8253_CW_MODE4 PIT8253_CW_M2
94+
95+
/** Mode 5: hardware-triggered strobe */
96+
#define PIT8253_CW_MODE5 (PIT8253_CW_M2 | PIT8253_CW_M0)
97+
98+
/* Frequency parameters */
99+
100+
/** Numerator of input frequency in MHz */
101+
#define PIT8253_FREQ_N 105
102+
103+
/** Denominator of input frequency in MHz */
104+
#define PIT8253_FREQ_D 88
105+
106+
#endif

include/kernel/infrastructure/i686/iodelay.h include/kernel/infrastructure/i686/drivers/iodelay.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_IODELAY_H
33-
#define JINUE_KERNEL_INFRASTRUCTURE_I686_IODELAY_H
32+
#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_DRIVERS_IODELAY_H
33+
#define JINUE_KERNEL_INFRASTRUCTURE_I686_DRIVERS_IODELAY_H
3434

3535
void iodelay(void);
3636

include/kernel/infrastructure/i686/drivers/pic8259.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 Philippe Aubertin.
2+
* Copyright (C) 2019-2024 Philippe Aubertin.
33
* All rights reserved.
44
55
* Redistribution and use in source and binary forms, with or without
@@ -33,13 +33,16 @@
3333
#define JINUE_KERNEL_INFRASTRUCTURE_I686_DRIVERS_PIC8259_H
3434

3535
#include <kernel/infrastructure/i686/drivers/asm/pic8259.h>
36+
#include <stdbool.h>
3637

3738
void pic8259_init();
3839

3940
void pic8259_mask(int irq);
4041

4142
void pic8259_unmask(int irq);
4243

43-
void pic8259_ack(int irq);
44+
void pic8259_eoi(int irq);
45+
46+
bool pic8259_is_spurious(int irq);
4447

4548
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2024 Philippe Aubertin.
3+
* All rights reserved.
4+
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* 3. Neither the name of the author nor the names of other contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
21+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
24+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef JINUE_KERNEL_INFRASTRUCTURE_I686_DRIVERS_PIT8253_H
33+
#define JINUE_KERNEL_INFRASTRUCTURE_I686_DRIVERS_PIT8253_H
34+
35+
#include <kernel/infrastructure/i686/drivers/asm/pit8253.h>
36+
37+
void pit8253_init(void);
38+
39+
#endif

0 commit comments

Comments
 (0)