summaryrefslogtreecommitdiff
path: root/sos-code-article5/hwcore/irq_wrappers.S
blob: a6284e1c7852407da0a8d6427c89031db3c8eacc (plain) (blame)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Copyright (C) 2004  The KOS Team
   Copyright (C) 1999  Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA. 
*/
#define ASM_SOURCE 1
         
.file "irq_wrappers.S"

.text

/** The address of the table of handlers (defined in irq.c) */
.extern sos_irq_handler_array

/** The address of the table of wrappers (defined below, and shared
   with irq.c */
.globl sos_irq_wrapper_array

/** The variable holding the nested level of the IRQ handlers */
.extern sos_irq_nested_level_counter

/* These pre-handlers are for IRQ (Master PIC) */
.irp id, 0,1,2,3,4,5,6,7

	.p2align 2, 0x90

	sos_irq_wrapper_\id:
	.type sos_irq_wrapper_\id,@function

		/*
		 * Backup the CPU context
		 */

		/* Fake error code */
		pushl $0

		/* Backup the actual context */
		pushl %ebp
		movl %esp, %ebp

		pushl %edi
		pushl %esi
		pushl %edx
		pushl %ecx
		pushl %ebx
		pushl %eax
		subl  $2,%esp
		pushw %ss
		pushw %ds
		pushw %es
		pushw %fs
		pushw %gs

		/*
		 * Increment IRQ nested level
		 */
		incl sos_irq_nested_level_counter

		/* Send EOI to PIC. See Intel 8259 datasheet
		   available on Kos website */	
		movb  $0x20, %al
		outb  %al, $0x20
	
		/*
		 * Call the handler with IRQ number as argument
		 */
		pushl $\id
		leal  sos_irq_handler_array,%edi
		call  *\id*4(%edi)
		addl  $4, %esp
	
		/*
		 * Decrement IRQ nested level
		 */
		cli  /* Just in case we messed up everything in the handler */
		subl $1, sos_irq_nested_level_counter

		/* sos_irq_nested_level_counter went below 0 ?! */
		jnc 2f
	
	1:      /* Yes:	Print fatal error message */
		pushl $msg_nested_level_overflow
		call sos_display_fatal_error
		addl $4, %esp ; jmp 1b
		/* Never returns */

	2:	/* No:	 all right ! */

		/* Restore the context */
		popw  %gs
		popw  %fs
		popw  %es
	        popw  %ds
		popw  %ss
		addl  $2,%esp
		popl  %eax
		popl  %ebx
		popl  %ecx
		popl  %edx
		popl  %esi
		popl  %edi
		popl  %ebp

		/* Remove fake error code */
		addl  $4, %esp

		iret
	.endr


/* These pre-handlers are for IRQ (Slave PIC) */
.irp id, 8,9,10,11,12,13,14,15

	.p2align 2, 0x90

	sos_irq_wrapper_\id:
	.type sos_irq_wrapper_\id,@function

		/*
		 * Backup the CPU context
		 */

		/* Fake error code */
		pushl $0

		/* Backup the actual context */
		pushl %ebp
		movl %esp, %ebp

		pushl %edi
		pushl %esi
		pushl %edx
		pushl %ecx
		pushl %ebx
		pushl %eax
		subl  $2,%esp
		pushw %ss
		pushw %ds
		pushw %es
		pushw %fs
		pushw %gs

		/*
		 * Increment IRQ nested level
		 */
		incl sos_irq_nested_level_counter

		/* Send EOI to PIC. See Intel 8259 datasheet
		   available on Kos website */	
		movb  $0x20, %al
		outb  %al, $0xa0
		outb  %al, $0x20

		/*
		 * Call the handler with IRQ number as argument
		 */
		pushl $\id
		leal  sos_irq_handler_array,%edi
		call  *\id*4(%edi)
		addl  $4, %esp

		/*
		 * Decrement IRQ nested level
		 */
		cli  /* Just in case we messed up everything in the handler */
		subl $1, sos_irq_nested_level_counter

		/* sos_irq_nested_level_counter went below 0 ?! */
		jnc 2f
		
	1:      /* Yes:	Print fatal error message */
		pushl $msg_nested_level_overflow
		call sos_display_fatal_error
		addl $4, %esp ; jmp 1b
		/* Never returns */

	2:	/* No:	 all right ! */

		/* Restore the context */
		popw  %gs
		popw  %fs
		popw  %es
	        popw  %ds
		popw  %ss
		addl  $2,%esp
		popl  %eax
		popl  %ebx
		popl  %ecx
		popl  %edx
		popl  %esi
		popl  %edi
		popl  %ebp

		/* Remove fake error code */
		addl  $4, %esp

		iret
	.endr

.section ".rodata"
msg_nested_level_overflow:
	.string "irq_wrappers.S: IRQ Nested level overflow ! System halted."

/* Build the sos_irq_wrapper_array, shared with irq.c */
.p2align 5, 0x0
sos_irq_wrapper_array:
	.irp id, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
		.long (sos_irq_wrapper_\id)
	.endr