Subversion Repositories HelenOS-historic

Rev

Rev 717 | Rev 803 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 717 Rev 799
1
#
1
#
2
# Copyright (C) 2005 Ondrej Palkovsky
2
# Copyright (C) 2005 Ondrej Palkovsky
3
# All rights reserved.
3
# All rights reserved.
4
#
4
#
5
# Redistribution and use in source and binary forms, with or without
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
6
# modification, are permitted provided that the following conditions
7
# are met:
7
# are met:
8
#
8
#
9
# - Redistributions of source code must retain the above copyright
9
# - Redistributions of source code must retain the above copyright
10
#   notice, this list of conditions and the following disclaimer.
10
#   notice, this list of conditions and the following disclaimer.
11
# - Redistributions in binary form must reproduce the above copyright
11
# - Redistributions in binary form must reproduce the above copyright
12
#   notice, this list of conditions and the following disclaimer in the
12
#   notice, this list of conditions and the following disclaimer in the
13
#   documentation and/or other materials provided with the distribution.
13
#   documentation and/or other materials provided with the distribution.
14
# - The name of the author may not be used to endorse or promote products
14
# - The name of the author may not be used to endorse or promote products
15
#   derived from this software without specific prior written permission.
15
#   derived from this software without specific prior written permission.
16
#
16
#
17
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#
27
#
28
 
28
 
29
 
29
 
30
#  Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word
30
#  Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word
31
# and 1 means interrupt with error word
31
# and 1 means interrupt with error word
32
 
32
 
33
	
33
	
34
#define ERROR_WORD_INTERRUPT_LIST 0x00027D00
34
#define ERROR_WORD_INTERRUPT_LIST 0x00027D00
35
 
35
 
36
#include <arch/pm.h>
36
#include <arch/pm.h>
-
 
37
#include <arch/context_offset.h>
37
	
38
	
38
.text
39
.text
39
.global interrupt_handlers
40
.global interrupt_handlers
40
.global panic_printf
41
.global panic_printf
41
 
42
 
42
panic_printf:
43
panic_printf:
43
	movq $halt, (%rsp)
44
	movq $halt, (%rsp)
44
	jmp printf
45
	jmp printf
45
 
46
 
46
.global memcpy
47
.global memcpy
47
memcpy:
48
memcpy:
48
	jmp _memcpy
49
	jmp _memcpy
49
	
50
	
50
.global cpuid
51
.global cpuid
51
.global has_cpuid
52
.global has_cpuid
52
.global rdtsc
53
.global rdtsc
53
.global read_efer_flag
54
.global read_efer_flag
54
.global set_efer_flag
55
.global set_efer_flag
55
	
56
	
56
## Determine CPUID support
57
## Determine CPUID support
57
#
58
#
58
# Return 0 in EAX if CPUID is not support, 1 if supported.
59
# Return 0 in EAX if CPUID is not support, 1 if supported.
59
#
60
#
60
has_cpuid:
61
has_cpuid:
61
	pushfq			# store flags
62
	pushfq			# store flags
62
	popq %rax		# read flags
63
	popq %rax		# read flags
63
	movq %rax,%rdx		# copy flags
64
	movq %rax,%rdx		# copy flags
64
	btcl $21,%edx		# swap the ID bit
65
	btcl $21,%edx		# swap the ID bit
65
	pushq %rdx
66
	pushq %rdx
66
	popfq			# propagate the change into flags
67
	popfq			# propagate the change into flags
67
	pushfq
68
	pushfq
68
	popq %rdx		# read flags	
69
	popq %rdx		# read flags	
69
	andl $(1<<21),%eax	# interested only in ID bit
70
	andl $(1<<21),%eax	# interested only in ID bit
70
	andl $(1<<21),%edx
71
	andl $(1<<21),%edx
71
	xorl %edx,%eax		# 0 if not supported, 1 if supported
72
	xorl %edx,%eax		# 0 if not supported, 1 if supported
72
	ret
73
	ret
73
 
74
 
74
cpuid:
75
cpuid:
75
	movq %rbx, %r10  # we have to preserve rbx across function calls
76
	movq %rbx, %r10  # we have to preserve rbx across function calls
76
 
77
 
77
	movl %edi,%eax	# load the command into %eax
78
	movl %edi,%eax	# load the command into %eax
78
 
79
 
79
	cpuid	
80
	cpuid	
80
	movl %eax,0(%rsi)
81
	movl %eax,0(%rsi)
81
	movl %ebx,4(%rsi)
82
	movl %ebx,4(%rsi)
82
	movl %ecx,8(%rsi)
83
	movl %ecx,8(%rsi)
83
	movl %edx,12(%rsi)
84
	movl %edx,12(%rsi)
84
 
85
 
85
	movq %r10, %rbx
86
	movq %r10, %rbx
86
	ret
87
	ret
87
 
88
 
88
rdtsc:
89
rdtsc:
89
	xorq %rax,%rax
90
	xorq %rax,%rax
90
	rdtsc
91
	rdtsc
91
	ret
92
	ret
92
 
93
 
93
set_efer_flag:
94
set_efer_flag:
94
	movq $0xc0000080, %rcx
95
	movq $0xc0000080, %rcx
95
	rdmsr
96
	rdmsr
96
	btsl %edi, %eax
97
	btsl %edi, %eax
97
	wrmsr
98
	wrmsr
98
	ret
99
	ret
99
	
100
	
100
read_efer_flag:	
101
read_efer_flag:	
101
	movq $0xc0000080, %rcx
102
	movq $0xc0000080, %rcx
102
	rdmsr
103
	rdmsr
103
	ret 		
104
	ret 		
104
 
105
 
105
# Push all general purpose registers on stack except %rbp, %rsp
106
# Push all general purpose registers on stack except %rbp, %rsp
106
.macro push_all_gpr
107
.macro save_all_gpr
-
 
108
	movq %rbp, IOFFSET_RBP(%rsp)
107
	pushq %rax 
109
	movq %rax, IOFFSET_RAX(%rsp)
108
	pushq %rbx
110
	movq %rbx, IOFFSET_RBX(%rsp)
109
	pushq %rcx
111
	movq %rcx, IOFFSET_RCX(%rsp)
110
	pushq %rdx
112
	movq %rdx, IOFFSET_RDX(%rsp)
111
	pushq %rsi
113
	movq %rsi, IOFFSET_RSI(%rsp)
112
	pushq %rdi
114
	movq %rdi, IOFFSET_RDI(%rsp)
113
	pushq %r8
115
	movq %r8, IOFFSET_R8(%rsp)
114
	pushq %r9
116
	movq %r9, IOFFSET_R9(%rsp)
115
	pushq %r10
117
	movq %r10, IOFFSET_R10(%rsp)
116
	pushq %r11
118
	movq %r11, IOFFSET_R11(%rsp)
117
	pushq %r12
119
	movq %r12, IOFFSET_R12(%rsp)
118
	pushq %r13
120
	movq %r13, IOFFSET_R13(%rsp)
119
	pushq %r14
121
	movq %r14, IOFFSET_R14(%rsp)
120
	pushq %r15
122
	movq %r15, IOFFSET_R15(%rsp)
121
.endm
123
.endm
122
 
124
 
123
.macro pop_all_gpr
125
.macro restore_all_gpr
124
	popq %r15
126
	movq IOFFSET_RBP(%rsp), %rbp
125
	popq %r14
127
	movq IOFFSET_RAX(%rsp), %rax
126
	popq %r13
128
	movq IOFFSET_RBX(%rsp), %rbx
127
	popq %r12
129
	movq IOFFSET_RCX(%rsp), %rcx
128
	popq %r11
130
	movq IOFFSET_RDX(%rsp), %rdx
129
	popq %r10
131
	movq IOFFSET_RSI(%rsp), %rsi
130
	popq %r9
132
	movq IOFFSET_RDI(%rsp), %rdi
131
	popq %r8
133
	movq IOFFSET_R8(%rsp), %r8
132
	popq %rdi
134
	movq IOFFSET_R9(%rsp), %r9
-
 
135
	movq IOFFSET_R10(%rsp), %r10
133
	popq %rsi
136
	movq IOFFSET_R11(%rsp), %r11
134
	popq %rdx
137
	movq IOFFSET_R12(%rsp), %r12
135
	popq %rcx
138
	movq IOFFSET_R13(%rsp), %r13
136
	popq %rbx
139
	movq IOFFSET_R14(%rsp), %r14
137
	popq %rax
140
	movq IOFFSET_R15(%rsp), %r15
138
.endm
141
.endm
139
	
142
	
140
## Declare interrupt handlers
143
## Declare interrupt handlers
141
#
144
#
142
# Declare interrupt handlers for n interrupt
145
# Declare interrupt handlers for n interrupt
143
# vectors starting at vector i.
146
# vectors starting at vector i.
144
#
147
#
145
# The handlers setup data segment registers
148
# The handlers setup data segment registers
146
# and call exc_dispatch().
149
# and call exc_dispatch().
147
#
150
#
148
.macro handler i n
151
.macro handler i n
149
	pushq %rbp
-
 
150
	movq %rsp,%rbp
152
	subq $IREGISTER_SPACE, %rsp
151
	
-
 
152
	push_all_gpr
153
	save_all_gpr
153
 
154
 
154
	movq $(\i),%rdi   # %rdi - first parameter
155
	movq $(\i),%rdi   # %rdi - first parameter
155
	movq %rbp, %rsi
-
 
156
	addq $8, %rsi     # %rsi - second parameter - original stack
156
	movq %rsp, %rsi   # %rsi - pointer to interrupt_context
157
	call exc_dispatch 	# exc_dispatch(i, stack)
157
	call exc_dispatch 	# exc_dispatch(i, stack)
158
 
158
 
159
# Test if this is interrupt with error word or not
159
# Test if this is interrupt with error word or not
160
	mov $\i,%cl;
160
	mov $\i,%cl;
161
	movl $1,%eax;
161
	movl $1,%eax;
162
	test $0xe0,%cl;
162
	test $0xe0,%cl;
163
	jnz 0f;
163
	jnz 0f;
164
	and $0x1f,%cl;
164
	and $0x1f,%cl;
165
	shl %cl,%eax;
165
	shl %cl,%eax;
166
	and $ERROR_WORD_INTERRUPT_LIST,%eax;
166
	and $ERROR_WORD_INTERRUPT_LIST,%eax;
167
	jz 0f;
167
	jz 0f;
168
 
168
 
169
 
169
 
170
# Return with error word
170
# Return with error word
171
	pop_all_gpr
171
	restore_all_gpr
172
	
-
 
173
	popq %rbp;
172
	# $8 = Skip error word
174
	addq $8,%rsp;    # Skip error word
173
	addq $IREGISTER_SPACE + 0x8, %rsp
175
	iretq
174
	iretq
176
 
175
 
177
0:
176
0:
178
# Return with no error word
177
# Return with no error word
179
	pop_all_gpr
178
	restore_all_gpr
180
	
-
 
181
	popq %rbp
179
	addq $IREGISTER_SPACE, %rsp
182
	iretq
180
	iretq
183
 
181
 
184
	.if (\n-\i)-1
182
	.if (\n-\i)-1
185
	handler "(\i+1)",\n
183
	handler "(\i+1)",\n
186
	.endif
184
	.endif
187
.endm
185
.endm
188
	
186
	
189
interrupt_handlers:
187
interrupt_handlers:
190
h_start:
188
h_start:
191
	handler 0 IDT_ITEMS
189
	handler 0 IDT_ITEMS
192
h_end:
190
h_end:
193
	
191
	
194
.data
192
.data
195
.global interrupt_handler_size
193
.global interrupt_handler_size
196
 
194
 
197
interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS
195
interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS
198
 
196