Subversion Repositories HelenOS-historic

Rev

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

Rev 955 Rev 1021
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
#define IREGISTER_SPACE 120
-
 
30
 
-
 
31
#define IOFFSET_RAX 0x0
-
 
32
#define IOFFSET_RBX 0x8
-
 
33
#define IOFFSET_RCX 0x10
-
 
34
#define IOFFSET_RDX 0x18
-
 
35
#define IOFFSET_RSI 0x20
-
 
36
#define IOFFSET_RDI 0x28
-
 
37
#define IOFFSET_R8 0x30
-
 
38
#define IOFFSET_R9 0x38
-
 
39
#define IOFFSET_R10 0x40
-
 
40
#define IOFFSET_R11 0x48
-
 
41
#define IOFFSET_R12 0x50
-
 
42
#define IOFFSET_R13 0x58
-
 
43
#define IOFFSET_R14 0x60
-
 
44
#define IOFFSET_R15 0x68
-
 
45
#define IOFFSET_RBP 0x70
29
 
46
 
30
#  Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word
47
#  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
48
# and 1 means interrupt with error word
32
 
-
 
33
	
-
 
34
#define ERROR_WORD_INTERRUPT_LIST 0x00027D00
49
#define ERROR_WORD_INTERRUPT_LIST 0x00027D00
35
 
50
 
36
#include <arch/pm.h>
51
#include <arch/pm.h>
37
#include <arch/context_offset.h>
-
 
38
#include <arch/mm/page.h>
52
#include <arch/mm/page.h>
39
	
53
	
40
.text
54
.text
41
.global interrupt_handlers
55
.global interrupt_handlers
42
.global syscall_entry
56
.global syscall_entry
43
.global panic_printf
57
.global panic_printf
44
 
58
 
45
panic_printf:
59
panic_printf:
46
	movq $halt, (%rsp)
60
	movq $halt, (%rsp)
47
	jmp printf
61
	jmp printf
48
 
62
 
49
.global memcpy
63
.global memcpy
50
memcpy:
64
memcpy:
51
	jmp _memcpy
65
	jmp _memcpy
52
	
66
	
53
.global cpuid
67
.global cpuid
54
.global has_cpuid
68
.global has_cpuid
55
.global rdtsc
69
.global rdtsc
56
.global read_efer_flag
70
.global read_efer_flag
57
.global set_efer_flag
71
.global set_efer_flag
58
	
72
	
59
## Determine CPUID support
73
## Determine CPUID support
60
#
74
#
61
# Return 0 in EAX if CPUID is not support, 1 if supported.
75
# Return 0 in EAX if CPUID is not support, 1 if supported.
62
#
76
#
63
has_cpuid:
77
has_cpuid:
64
	pushfq			# store flags
78
	pushfq			# store flags
65
	popq %rax		# read flags
79
	popq %rax		# read flags
66
	movq %rax,%rdx		# copy flags
80
	movq %rax,%rdx		# copy flags
67
	btcl $21,%edx		# swap the ID bit
81
	btcl $21,%edx		# swap the ID bit
68
	pushq %rdx
82
	pushq %rdx
69
	popfq			# propagate the change into flags
83
	popfq			# propagate the change into flags
70
	pushfq
84
	pushfq
71
	popq %rdx		# read flags	
85
	popq %rdx		# read flags	
72
	andl $(1<<21),%eax	# interested only in ID bit
86
	andl $(1<<21),%eax	# interested only in ID bit
73
	andl $(1<<21),%edx
87
	andl $(1<<21),%edx
74
	xorl %edx,%eax		# 0 if not supported, 1 if supported
88
	xorl %edx,%eax		# 0 if not supported, 1 if supported
75
	ret
89
	ret
76
 
90
 
77
cpuid:
91
cpuid:
78
	movq %rbx, %r10  # we have to preserve rbx across function calls
92
	movq %rbx, %r10  # we have to preserve rbx across function calls
79
 
93
 
80
	movl %edi,%eax	# load the command into %eax
94
	movl %edi,%eax	# load the command into %eax
81
 
95
 
82
	cpuid	
96
	cpuid	
83
	movl %eax,0(%rsi)
97
	movl %eax,0(%rsi)
84
	movl %ebx,4(%rsi)
98
	movl %ebx,4(%rsi)
85
	movl %ecx,8(%rsi)
99
	movl %ecx,8(%rsi)
86
	movl %edx,12(%rsi)
100
	movl %edx,12(%rsi)
87
 
101
 
88
	movq %r10, %rbx
102
	movq %r10, %rbx
89
	ret
103
	ret
90
 
104
 
91
rdtsc:
105
rdtsc:
92
	xorq %rax,%rax
106
	xorq %rax,%rax
93
	rdtsc
107
	rdtsc
94
	ret
108
	ret
95
 
109
 
96
set_efer_flag:
110
set_efer_flag:
97
	movq $0xc0000080, %rcx
111
	movq $0xc0000080, %rcx
98
	rdmsr
112
	rdmsr
99
	btsl %edi, %eax
113
	btsl %edi, %eax
100
	wrmsr
114
	wrmsr
101
	ret
115
	ret
102
	
116
	
103
read_efer_flag:	
117
read_efer_flag:	
104
	movq $0xc0000080, %rcx
118
	movq $0xc0000080, %rcx
105
	rdmsr
119
	rdmsr
106
	ret 		
120
	ret 		
107
 
121
 
108
# Push all general purpose registers on stack except %rbp, %rsp
122
# Push all general purpose registers on stack except %rbp, %rsp
109
.macro save_all_gpr
123
.macro save_all_gpr
110
	movq %rbp, IOFFSET_RBP(%rsp)
124
	movq %rbp, IOFFSET_RBP(%rsp)
111
	movq %rax, IOFFSET_RAX(%rsp)
125
	movq %rax, IOFFSET_RAX(%rsp)
112
	movq %rbx, IOFFSET_RBX(%rsp)
126
	movq %rbx, IOFFSET_RBX(%rsp)
113
	movq %rcx, IOFFSET_RCX(%rsp)
127
	movq %rcx, IOFFSET_RCX(%rsp)
114
	movq %rdx, IOFFSET_RDX(%rsp)
128
	movq %rdx, IOFFSET_RDX(%rsp)
115
	movq %rsi, IOFFSET_RSI(%rsp)
129
	movq %rsi, IOFFSET_RSI(%rsp)
116
	movq %rdi, IOFFSET_RDI(%rsp)
130
	movq %rdi, IOFFSET_RDI(%rsp)
117
	movq %r8, IOFFSET_R8(%rsp)
131
	movq %r8, IOFFSET_R8(%rsp)
118
	movq %r9, IOFFSET_R9(%rsp)
132
	movq %r9, IOFFSET_R9(%rsp)
119
	movq %r10, IOFFSET_R10(%rsp)
133
	movq %r10, IOFFSET_R10(%rsp)
120
	movq %r11, IOFFSET_R11(%rsp)
134
	movq %r11, IOFFSET_R11(%rsp)
121
	movq %r12, IOFFSET_R12(%rsp)
135
	movq %r12, IOFFSET_R12(%rsp)
122
	movq %r13, IOFFSET_R13(%rsp)
136
	movq %r13, IOFFSET_R13(%rsp)
123
	movq %r14, IOFFSET_R14(%rsp)
137
	movq %r14, IOFFSET_R14(%rsp)
124
	movq %r15, IOFFSET_R15(%rsp)
138
	movq %r15, IOFFSET_R15(%rsp)
125
.endm
139
.endm
126
 
140
 
127
.macro restore_all_gpr
141
.macro restore_all_gpr
128
	movq IOFFSET_RBP(%rsp), %rbp
142
	movq IOFFSET_RBP(%rsp), %rbp
129
	movq IOFFSET_RAX(%rsp), %rax
143
	movq IOFFSET_RAX(%rsp), %rax
130
	movq IOFFSET_RBX(%rsp), %rbx
144
	movq IOFFSET_RBX(%rsp), %rbx
131
	movq IOFFSET_RCX(%rsp), %rcx
145
	movq IOFFSET_RCX(%rsp), %rcx
132
	movq IOFFSET_RDX(%rsp), %rdx
146
	movq IOFFSET_RDX(%rsp), %rdx
133
	movq IOFFSET_RSI(%rsp), %rsi
147
	movq IOFFSET_RSI(%rsp), %rsi
134
	movq IOFFSET_RDI(%rsp), %rdi
148
	movq IOFFSET_RDI(%rsp), %rdi
135
	movq IOFFSET_R8(%rsp), %r8
149
	movq IOFFSET_R8(%rsp), %r8
136
	movq IOFFSET_R9(%rsp), %r9
150
	movq IOFFSET_R9(%rsp), %r9
137
	movq IOFFSET_R10(%rsp), %r10
151
	movq IOFFSET_R10(%rsp), %r10
138
	movq IOFFSET_R11(%rsp), %r11
152
	movq IOFFSET_R11(%rsp), %r11
139
	movq IOFFSET_R12(%rsp), %r12
153
	movq IOFFSET_R12(%rsp), %r12
140
	movq IOFFSET_R13(%rsp), %r13
154
	movq IOFFSET_R13(%rsp), %r13
141
	movq IOFFSET_R14(%rsp), %r14
155
	movq IOFFSET_R14(%rsp), %r14
142
	movq IOFFSET_R15(%rsp), %r15
156
	movq IOFFSET_R15(%rsp), %r15
143
.endm
157
.endm
144
	
158
 
145
## Declare interrupt handlers
159
## Declare interrupt handlers
146
#
160
#
147
# Declare interrupt handlers for n interrupt
161
# Declare interrupt handlers for n interrupt
148
# vectors starting at vector i.
162
# vectors starting at vector i.
149
#
163
#
150
# The handlers setup data segment registers
-
 
151
# and call exc_dispatch().
164
# The handlers call exc_dispatch().
152
#
165
#
153
.macro handler i n
166
.macro handler i n
154
	subq $IREGISTER_SPACE, %rsp
-
 
155
	save_all_gpr
-
 
156
 
167
 
-
 
168
	/*
157
	movq $(\i),%rdi   # %rdi - first parameter
169
	 * Choose between version with error code and version without error code.
158
	movq %rsp, %rsi   # %rsi - pointer to interrupt_context
170
	 * Both versions have to be of the same size. amd64 assembly is, however,
-
 
171
	 * a little bit tricky. For instance, subq $0x80, %rsp and subq $0x78, %rsp
159
	call exc_dispatch 	# exc_dispatch(i, stack)
172
	 * can result in two instructions with different op-code lengths.
-
 
173
	 * Therefore, pay special attention to the extra NOP's that serve as
-
 
174
	 * a necessary fill.
-
 
175
	 */
160
 
176
 
-
 
177
	.iflt \i-32
-
 
178
		.if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
-
 
179
			/*
161
# Test if this is interrupt with error word or not
180
			 * Version with error word.
162
	mov $\i,%cl;
181
			 */
-
 
182
			subq $IREGISTER_SPACE, %rsp
-
 
183
			nop
-
 
184
			nop
-
 
185
			nop
163
	movl $1,%eax;
186
		.else
-
 
187
			/*
164
	test $0xe0,%cl;
188
			 * Version without error word,
165
	jnz 0f;
189
			 */
-
 
190
			subq $(IREGISTER_SPACE+8), %rsp
166
	and $0x1f,%cl;
191
		.endif
167
	shl %cl,%eax;
192
	.else
-
 
193
		/*
-
 
194
		 * Version without error word,
-
 
195
		 */
168
	and $ERROR_WORD_INTERRUPT_LIST,%eax;
196
		subq $(IREGISTER_SPACE+8), %rsp
169
	jz 0f;
197
	.endif	
170
 
198
 
-
 
199
	save_all_gpr
171
 
200
 
-
 
201
	movq $(\i), %rdi   	# %rdi - first parameter
172
# Return with error word
202
	movq %rsp, %rsi   	# %rsi - pointer to istate
-
 
203
	call exc_dispatch 	# exc_dispatch(i, istate)
-
 
204
	
173
	restore_all_gpr
205
	restore_all_gpr
174
	# $8 = Skip error word
206
	# $8 = Skip error word
175
	addq $IREGISTER_SPACE + 0x8, %rsp
-
 
176
	iretq
-
 
177
 
-
 
178
0:
-
 
179
# Return with no error word
-
 
180
	restore_all_gpr
-
 
181
	addq $IREGISTER_SPACE, %rsp
207
	addq $(IREGISTER_SPACE+8), %rsp
182
	iretq
208
	iretq
183
 
209
 
184
	.if (\n-\i)-1
210
	.if (\n-\i)-1
185
	handler "(\i+1)",\n
211
	handler "(\i+1)",\n
186
	.endif
212
	.endif
187
.endm
213
.endm
188
	
214
	
189
interrupt_handlers:
215
interrupt_handlers:
190
h_start:
216
h_start:
191
	handler 0 IDT_ITEMS
217
	handler 0 IDT_ITEMS
192
h_end:
218
h_end:
193
 
219
 
194
	
220
	
195
syscall_entry:
221
syscall_entry:
196
	# Switch to hidden gs	
222
	# Switch to hidden gs	
197
	swapgs
223
	swapgs
198
	# %gs:0 now points to pointer to stack page
224
	# %gs:0 now points to pointer to stack page
199
	mov %gs:0, %r10     # We have a ptr to stack page in r10
225
	mov %gs:0, %r10     # We have a ptr to stack page in r10
200
	addq $PAGE_SIZE-16, %r10 # We need some space to store old %sp
226
	addq $PAGE_SIZE-16, %r10 # We need some space to store old %sp
201
	
227
	
202
	movq %rsp, 0(%r10)  # Save old stack pointer to stack
228
	movq %rsp, 0(%r10)  # Save old stack pointer to stack
203
	movq %r10, %rsp     # Change to new stack
229
	movq %r10, %rsp     # Change to new stack
204
	pushq %rcx          # Return address
230
	pushq %rcx          # Return address
205
	pushq %r11          # Save flags
231
	pushq %r11          # Save flags
206
 
232
 
207
	# Switch back to remain consistent
233
	# Switch back to remain consistent
208
	swapgs 
234
	swapgs 
209
 
235
 
210
	sti
236
	sti
211
	movq %r9, %rcx      # Exchange last parameter as a third
237
	movq %r9, %rcx      # Exchange last parameter as a third
212
	call syscall_handler
238
	call syscall_handler
213
	cli                 # We will be touching stack pointer
239
	cli                 # We will be touching stack pointer
214
		
240
		
215
	popq %r11
241
	popq %r11
216
	popq %rcx
242
	popq %rcx
217
	movq 0(%rsp), %rsp
243
	movq 0(%rsp), %rsp
218
	sysretq
244
	sysretq
219
		
245
		
220
.data
246
.data
221
.global interrupt_handler_size
247
.global interrupt_handler_size
222
 
248
 
223
interrupt_handler_size: .quad (h_end-h_start)/IDT_ITEMS
249
interrupt_handler_size: .quad (h_end-h_start)/IDT_ITEMS
224
 
250