Subversion Repositories HelenOS

Rev

Rev 252 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
224 palkovsky 1
#
2
# Copyright (C) 2005 Ondrej Palkovsky
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
# - Redistributions of source code must retain the above copyright
10
#   notice, this list of conditions and the following disclaimer.
11
# - Redistributions in binary form must reproduce the above copyright
12
#   notice, this list of conditions and the following disclaimer in the
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
15
#   derived from this software without specific prior written permission.
16
#
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
19
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#
28
 
29
 
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
32
 
33
 
34
#define ERROR_WORD_INTERRUPT_LIST 0x00027D00
35
 
36
#define __ASM__
37
#include <arch/pm.h>
38
 
39
.text
40
.global interrupt_handlers
41
.global panic_printf
42
 
43
panic_printf:
44
	movq $halt, (%rsp)
45
	jmp printf
46
 
252 palkovsky 47
.global memcpy
48
memcpy:
49
	jmp _memcpy
50
 
51
.global cpuid
242 palkovsky 52
.global has_cpuid
53
.global rdtsc
251 palkovsky 54
.global read_efer_flag
55
.global set_efer_flag
56
 
252 palkovsky 57
 
257 palkovsky 58
# THIS IS USERSPACE CODE
59
.global utext
60
utext:
61
	xor %ax,%ax;
62
	mov %ax,%ds;
63
	mov %ax,%es;
64
	mov %ax,%fs;
65
	mov %ax,%gs;
66
0:
67
	int $48
68
	jmp 0b
69
	# not reached
70
utext_end:
71
 
72
.data
73
.global utext_size
74
utext_size:
75
	.long utext_end - utext 
76
 
77
 
242 palkovsky 78
## Determine CPUID support
79
#
80
# Return 0 in EAX if CPUID is not support, 1 if supported.
81
#
82
has_cpuid:
83
	pushq %rbx
84
 
85
	pushfq			# store flags
86
	popq %rax		# read flags
87
	movq %rax,%rbx		# copy flags
88
	btcl $21,%ebx		# swap the ID bit
89
	pushq %rbx
90
	popfq			# propagate the change into flags
91
	pushfq
92
	popq %rbx		# read flags	
93
	andl $(1<<21),%eax	# interested only in ID bit
94
	andl $(1<<21),%ebx
95
	xorl %ebx,%eax		# 0 if not supported, 1 if supported
96
 
97
	popq %rbx
98
	ret
99
 
251 palkovsky 100
cpuid:
101
	movq %rbx, %r10  # we have to preserve rbx across function calls
242 palkovsky 102
 
251 palkovsky 103
	movl %edi,%eax	# load the command into %eax
104
 
105
	cpuid	
106
	movl %eax,0(%rsi)
107
	movl %ebx,4(%rsi)
108
	movl %ecx,8(%rsi)
109
	movl %edx,12(%rsi)
110
 
111
	movq %r10, %rbx
112
	ret
113
 
242 palkovsky 114
rdtsc:
115
	xorq %rax,%rax
116
	rdtsc
117
	ret
251 palkovsky 118
 
119
set_efer_flag:
120
	movq $0xc0000080, %rcx
121
	rdmsr
122
	btsl %edi, %eax
123
	wrmsr
124
	ret
242 palkovsky 125
 
251 palkovsky 126
read_efer_flag:	
127
	movq $0xc0000080, %rcx
128
	rdmsr
129
	ret 		
242 palkovsky 130
 
224 palkovsky 131
# Push all general purpose registers on stack except %rbp, %rsp
132
.macro push_all_gpr
133
	pushq %rax 
134
	pushq %rbx
135
	pushq %rcx
136
	pushq %rdx
137
	pushq %rsi
138
	pushq %rdi
139
	pushq %r8
140
	pushq %r9
141
	pushq %r10
142
	pushq %r11
143
	pushq %r12
144
	pushq %r13
145
	pushq %r14
146
	pushq %r15
147
.endm
148
 
149
.macro pop_all_gpr
150
	popq %r15
151
	popq %r14
152
	popq %r13
153
	popq %r12
154
	popq %r11
155
	popq %r10
156
	popq %r9
157
	popq %r8
158
	popq %rdi
159
	popq %rsi
160
	popq %rdx
161
	popq %rcx
162
	popq %rbx
163
	popq %rax
164
.endm
165
 
166
## Declare interrupt handlers
167
#
168
# Declare interrupt handlers for n interrupt
169
# vectors starting at vector i.
170
#
171
# The handlers setup data segment registers
172
# and call trap_dispatcher().
173
#
174
.macro handler i n
175
	pushq %rbp
176
	movq %rsp,%rbp
177
 
178
	push_all_gpr
179
 
180
	# trap_dispatcher(i, stack)
181
	movq $(\i),%rdi   # %rdi - first parameter
182
	movq %rbp, %rsi
183
	addq $8, %rsi     # %rsi - second parameter - original stack
184
	call trap_dispatcher
185
 
186
# Test if this is interrupt with error word or not
187
	mov $\i,%cl;
188
	movl $1,%eax;
189
	test $0xe0,%cl;
190
	jnz 0f;
191
	and $0x1f,%cl;
192
	shl %cl,%eax;
193
	and $ERROR_WORD_INTERRUPT_LIST,%eax;
194
	jz 0f;
195
 
196
 
197
# Return with error word
198
	pop_all_gpr
199
 
200
	popq %rbp;
201
	add $8,%esp;    # Skip error word
202
	iretq
203
 
204
0:
205
# Return with no error word
206
	pop_all_gpr
207
 
208
	popq %rbp
209
	iretq
210
 
211
	.if (\n-\i)-1
212
	handler "(\i+1)",\n
213
	.endif
214
.endm
215
 
216
interrupt_handlers:
217
h_start:
218
	handler 0 IDT_ITEMS
219
#	handler 64 128	
220
#	handler 128 192
221
#	handler 192 256
222
h_end:
223
 
224
 
225
.data
226
.global interrupt_handler_size
227
 
228
interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS