Subversion Repositories HelenOS

Rev

Rev 3022 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3022 Rev 4055
1
#
1
#
2
# Copyright (c) 2001-2004 Jakub Jermar
2
# Copyright (c) 2001-2004 Jakub Jermar
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
## very low and hardware-level functions
29
## very low and hardware-level functions
30
 
30
 
31
# Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error
31
# Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error
32
# word and 1 means interrupt with error word
32
# word and 1 means interrupt with error word
33
#define ERROR_WORD_INTERRUPT_LIST 0x00027d00
33
#define ERROR_WORD_INTERRUPT_LIST 0x00027d00
34
 
34
 
35
.text
35
.text
36
 
36
 
37
.global paging_on
37
.global paging_on
38
.global enable_l_apic_in_msr
38
.global enable_l_apic_in_msr
39
.global interrupt_handlers
39
.global interrupt_handlers
-
 
40
.global memsetb
-
 
41
.global memsetw
40
.global memcpy
42
.global memcpy
41
.global memcpy_from_uspace
43
.global memcpy_from_uspace
42
.global memcpy_from_uspace_failover_address
44
.global memcpy_from_uspace_failover_address
43
.global memcpy_to_uspace
45
.global memcpy_to_uspace
44
.global memcpy_to_uspace_failover_address
46
.global memcpy_to_uspace_failover_address
45
 
47
 
46
 
48
 
-
 
49
# Wrapper for generic memsetb
-
 
50
memsetb:
-
 
51
	jmp _memsetb
-
 
52
 
-
 
53
# Wrapper for generic memsetw
-
 
54
memsetw:
-
 
55
	jmp _memsetw
-
 
56
 
-
 
57
 
47
#define MEMCPY_DST	4
58
#define MEMCPY_DST	4
48
#define MEMCPY_SRC	8
59
#define MEMCPY_SRC	8
49
#define MEMCPY_SIZE	12
60
#define MEMCPY_SIZE	12
50
 
61
 
51
/** Copy memory to/from userspace.
62
/** Copy memory to/from userspace.
52
 *
63
 *
53
 * This is almost conventional memcpy().
64
 * This is almost conventional memcpy().
54
 * The difference is that there is a failover part
65
 * The difference is that there is a failover part
55
 * to where control is returned from a page fault
66
 * to where control is returned from a page fault
56
 * if the page fault occurs during copy_from_uspace()
67
 * if the page fault occurs during copy_from_uspace()
57
 * or copy_to_uspace().
68
 * or copy_to_uspace().
58
 *
69
 *
59
 * @param MEMCPY_DST(%esp)	Destination address.
70
 * @param MEMCPY_DST(%esp)	Destination address.
60
 * @param MEMCPY_SRC(%esp)	Source address.
71
 * @param MEMCPY_SRC(%esp)	Source address.
61
 * @param MEMCPY_SIZE(%esp)	Size.
72
 * @param MEMCPY_SIZE(%esp)	Size.
62
 *
73
 *
63
 * @return MEMCPY_SRC(%esp) on success and 0 on failure.
74
 * @return MEMCPY_DST(%esp) on success and 0 on failure.
64
 */
75
 */
65
memcpy:
76
memcpy:
66
memcpy_from_uspace:
77
memcpy_from_uspace:
67
memcpy_to_uspace:
78
memcpy_to_uspace:
68
	movl %edi, %edx			/* save %edi */
79
	movl %edi, %edx			/* save %edi */
69
	movl %esi, %eax			/* save %esi */
80
	movl %esi, %eax			/* save %esi */
70
	
81
	
71
	movl MEMCPY_SIZE(%esp), %ecx
82
	movl MEMCPY_SIZE(%esp), %ecx
72
	shrl $2, %ecx			/* size / 4 */
83
	shrl $2, %ecx			/* size / 4 */
73
	
84
	
74
	movl MEMCPY_DST(%esp), %edi
85
	movl MEMCPY_DST(%esp), %edi
75
	movl MEMCPY_SRC(%esp), %esi
86
	movl MEMCPY_SRC(%esp), %esi
76
	
87
	
77
	rep movsl			/* copy whole words */
88
	rep movsl			/* copy whole words */
78
 
89
 
79
	movl MEMCPY_SIZE(%esp), %ecx
90
	movl MEMCPY_SIZE(%esp), %ecx
80
	andl $3, %ecx			/* size % 4 */
91
	andl $3, %ecx			/* size % 4 */
81
	jz 0f
92
	jz 0f
82
	
93
	
83
	rep movsb			/* copy the rest byte by byte */
94
	rep movsb			/* copy the rest byte by byte */
84
 
95
 
85
0:
96
0:
86
	movl %edx, %edi
97
	movl %edx, %edi
87
	movl %eax, %esi
98
	movl %eax, %esi
88
	movl MEMCPY_SRC(%esp), %eax	/* MEMCPY_SRC(%esp), success */
99
	movl MEMCPY_DST(%esp), %eax	/* MEMCPY_DST(%esp), success */
89
	ret
100
	ret
90
	
101
	
91
/*
102
/*
92
 * We got here from as_page_fault() after the memory operations
103
 * We got here from as_page_fault() after the memory operations
93
 * above had caused a page fault.
104
 * above had caused a page fault.
94
 */
105
 */
95
memcpy_from_uspace_failover_address:
106
memcpy_from_uspace_failover_address:
96
memcpy_to_uspace_failover_address:
107
memcpy_to_uspace_failover_address:
97
	movl %edx, %edi
108
	movl %edx, %edi
98
	movl %eax, %esi
109
	movl %eax, %esi
99
	xorl %eax, %eax			/* return 0, failure */
110
	xorl %eax, %eax			/* return 0, failure */
100
	ret
111
	ret
101
 
112
 
102
## Turn paging on
113
## Turn paging on
103
#
114
#
104
# Enable paging and write-back caching in CR0.
115
# Enable paging and write-back caching in CR0.
105
#
116
#
106
paging_on:
117
paging_on:
107
	movl %cr0, %edx
118
	movl %cr0, %edx
108
	orl $(1 << 31), %edx		# paging on
119
	orl $(1 << 31), %edx		# paging on
109
	# clear Cache Disable and not Write Though
120
	# clear Cache Disable and not Write Though
110
	andl $~((1 << 30) | (1 << 29)), %edx
121
	andl $~((1 << 30) | (1 << 29)), %edx
111
	movl %edx,%cr0
122
	movl %edx,%cr0
112
	jmp 0f
123
	jmp 0f
113
0:
124
0:
114
	ret
125
	ret
115
 
126
 
116
 
127
 
117
## Enable local APIC
128
## Enable local APIC
118
#
129
#
119
# Enable local APIC in MSR.
130
# Enable local APIC in MSR.
120
#
131
#
121
enable_l_apic_in_msr:
132
enable_l_apic_in_msr:
122
	movl $0x1b, %ecx
133
	movl $0x1b, %ecx
123
	rdmsr
134
	rdmsr
124
	orl $(1 << 11), %eax
135
	orl $(1 << 11), %eax
125
	orl $(0xfee00000), %eax
136
	orl $(0xfee00000), %eax
126
	wrmsr
137
	wrmsr
127
	ret
138
	ret
128
 
139
 
129
# Clear nested flag
140
# Clear nested flag
130
# overwrites %ecx
141
# overwrites %ecx
131
.macro CLEAR_NT_FLAG
142
.macro CLEAR_NT_FLAG
132
	pushfl
143
	pushfl
133
	pop %ecx
144
	pop %ecx
134
	and $0xffffbfff, %ecx
145
	and $0xffffbfff, %ecx
135
	push %ecx
146
	push %ecx
136
	popfl
147
	popfl
137
.endm	
148
.endm	
138
 
149
 
-
 
150
/*
-
 
151
 * The SYSENTER syscall mechanism can be used for syscalls with
-
 
152
 * four or fewer arguments. To pass these four arguments, we
-
 
153
 * use four registers: EDX, ECX, EBX, ESI. The syscall number
-
 
154
 * is passed in EAX. We use EDI to remember the return address
-
 
155
 * and EBP to remember the stack. The INT-based syscall mechanism
-
 
156
 * can actually handle six arguments plus the syscall number
-
 
157
 * entirely in registers.
-
 
158
 */
-
 
159
.global sysenter_handler
-
 
160
sysenter_handler:
-
 
161
	sti
-
 
162
	pushl %ebp	# remember user stack
-
 
163
	pushl %edi	# remember return user address
-
 
164
 
-
 
165
	pushl %gs	# remember TLS
-
 
166
 
-
 
167
	pushl %eax	# syscall number
-
 
168
	subl $8, %esp	# unused sixth and fifth argument
-
 
169
	pushl %esi	# fourth argument
-
 
170
	pushl %ebx	# third argument
-
 
171
	pushl %ecx	# second argument
-
 
172
	pushl %edx	# first argument
-
 
173
 
-
 
174
	movw $16, %ax
-
 
175
	movw %ax, %ds
-
 
176
	movw %ax, %es
-
 
177
 
-
 
178
	cld
-
 
179
	call syscall_handler
-
 
180
	addl $28, %esp	# remove arguments from stack
-
 
181
 
-
 
182
	pop %gs		# restore TLS
-
 
183
 
-
 
184
	pop %edx	# prepare return EIP for SYSEXIT
-
 
185
	pop %ecx	# prepare userspace ESP for SYSEXIT
-
 
186
 
-
 
187
	sysexit		# return to userspace
-
 
188
 
-
 
189
 
139
## Declare interrupt handlers
190
## Declare interrupt handlers
140
#
191
#
141
# Declare interrupt handlers for n interrupt
192
# Declare interrupt handlers for n interrupt
142
# vectors starting at vector i.
193
# vectors starting at vector i.
143
#
194
#
144
# The handlers setup data segment registers
195
# The handlers setup data segment registers
145
# and call exc_dispatch().
196
# and call exc_dispatch().
146
#
197
#
147
#define INTERRUPT_ALIGN 64
198
#define INTERRUPT_ALIGN 64
148
.macro handler i n
199
.macro handler i n
149
 
200
 
150
.ifeq \i - 0x30     # Syscall handler
201
.ifeq \i - 0x30     # Syscall handler
151
	pushl %ds
202
	pushl %ds
152
	pushl %es
203
	pushl %es
153
	pushl %fs
204
	pushl %fs
154
	pushl %gs
205
	pushl %gs
155
 
206
 
156
	#
207
	#
157
	# Push syscall arguments onto the stack
208
	# Push syscall arguments onto the stack
158
	#
209
	#
159
	# NOTE: The idea behind the order of arguments passed in registers is to
210
	# NOTE: The idea behind the order of arguments passed in registers is to
160
	#	use all scratch registers first and preserved registers next.
211
	#	use all scratch registers first and preserved registers next.
161
	#	An optimized libc syscall wrapper can make use of this setup.
212
	#	An optimized libc syscall wrapper can make use of this setup.
162
	#
213
	#
163
	pushl %eax
214
	pushl %eax
164
	pushl %ebp
215
	pushl %ebp
165
	pushl %edi
216
	pushl %edi
166
	pushl %esi
217
	pushl %esi
167
	pushl %ebx
218
	pushl %ebx
168
	pushl %ecx
219
	pushl %ecx
169
	pushl %edx
220
	pushl %edx
170
	
221
	
171
	# we must fill the data segment registers
222
	# we must fill the data segment registers
172
	movw $16, %ax
223
	movw $16, %ax
173
	movw %ax, %ds
224
	movw %ax, %ds
174
	movw %ax, %es
225
	movw %ax, %es
175
	
226
	
176
	cld
227
	cld
177
	sti
228
	sti
178
	# syscall_handler(edx, ecx, ebx, esi, edi, ebp, eax)
229
	# syscall_handler(edx, ecx, ebx, esi, edi, ebp, eax)
179
	call syscall_handler   
230
	call syscall_handler   
180
	cli
231
	cli
181
	addl $28, %esp         # clean-up of parameters
232
	addl $28, %esp         # clean-up of parameters
182
	
233
	
183
	popl %gs
234
	popl %gs
184
	popl %fs
235
	popl %fs
185
	popl %es
236
	popl %es
186
	popl %ds
237
	popl %ds
187
	
238
	
188
	CLEAR_NT_FLAG
239
	CLEAR_NT_FLAG
189
	iret
240
	iret
190
.else	
241
.else	
191
	/*
242
	/*
192
	 * This macro distinguishes between two versions of ia32 exceptions.
243
	 * This macro distinguishes between two versions of ia32 exceptions.
193
	 * One version has error word and the other does not have it.
244
	 * One version has error word and the other does not have it.
194
	 * The latter version fakes the error word on the stack so that the
245
	 * The latter version fakes the error word on the stack so that the
195
	 * handlers and istate_t can be the same for both types.
246
	 * handlers and istate_t can be the same for both types.
196
	 */
247
	 */
197
	.iflt \i - 32
248
	.iflt \i - 32
198
		.if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
249
		.if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
199
			/* 
250
			/* 
200
			 * With error word, do nothing
251
			 * With error word, do nothing
201
			 */
252
			 */
202
                .else
253
                .else
203
                        /*
254
                        /*
204
                         * Version without error word,
255
                         * Version without error word,
205
                         */
256
                         */
206
			subl $4, %esp
257
			subl $4, %esp
207
                .endif
258
                .endif
208
        .else
259
        .else
209
                /*
260
                /*
210
                 * Version without error word,
261
                 * Version without error word,
211
                 */
262
                 */
212
		subl $4, %esp
263
		subl $4, %esp
213
	.endif
264
	.endif
214
	
265
	
215
	pushl %ds
266
	pushl %ds
216
	pushl %es
267
	pushl %es
217
	pushl %fs
268
	pushl %fs
218
	pushl %gs
269
	pushl %gs
219
 
270
 
220
#ifdef CONFIG_DEBUG_ALLREGS
-
 
221
	pushl %ebx
-
 
222
	pushl %ebp
-
 
223
	pushl %edi
-
 
224
	pushl %esi
-
 
225
#else
-
 
226
	subl $16, %esp
-
 
227
#endif
-
 
228
	pushl %edx
271
	pushl %edx
229
	pushl %ecx
272
	pushl %ecx
230
	pushl %eax
273
	pushl %eax
231
	
274
	
232
	# we must fill the data segment registers
275
	# we must fill the data segment registers
233
	movw $16, %ax
276
	movw $16, %ax
234
	movw %ax, %ds
277
	movw %ax, %ds
235
	movw %ax, %es
278
	movw %ax, %es
236
 
279
 
237
	cld
280
	cld
238
 
281
 
239
	pushl %esp          # *istate
282
	pushl %esp          # *istate
240
	pushl $(\i)         # intnum
283
	pushl $(\i)         # intnum
241
	call exc_dispatch   # excdispatch(intnum, *istate)
284
	call exc_dispatch   # excdispatch(intnum, *istate)
242
	addl $8, %esp       # Clear arguments from stack
285
	addl $8, %esp       # Clear arguments from stack
243
 
286
 
244
	CLEAR_NT_FLAG # Modifies %ecx
287
	CLEAR_NT_FLAG # Modifies %ecx
245
	
288
	
246
	popl %eax
289
	popl %eax
247
	popl %ecx
290
	popl %ecx
248
	popl %edx
291
	popl %edx
249
#ifdef CONFIG_DEBUG_ALLREGS
-
 
250
	popl %esi
-
 
251
	popl %edi
-
 
252
	popl %ebp
-
 
253
	popl %ebx
-
 
254
#else
-
 
255
	addl $16, %esp
-
 
256
#endif	
-
 
257
	
292
	
258
	popl %gs
293
	popl %gs
259
	popl %fs
294
	popl %fs
260
	popl %es
295
	popl %es
261
	popl %ds
296
	popl %ds
262
 
297
 
263
	addl $4, %esp	# Skip error word, no matter whether real or fake.
298
	addl $4, %esp	# Skip error word, no matter whether real or fake.
264
	iret
299
	iret
265
.endif
300
.endif
266
 
301
 
267
	.align INTERRUPT_ALIGN
302
	.align INTERRUPT_ALIGN
268
	.if (\n- \i) - 1
303
	.if (\n- \i) - 1
269
	handler "(\i + 1)", \n
304
	handler "(\i + 1)", \n
270
	.endif
305
	.endif
271
.endm
306
.endm
272
 
307
 
273
# keep in sync with pm.h !!!
308
# keep in sync with pm.h !!!
274
IDT_ITEMS = 64
309
IDT_ITEMS = 64
275
.align INTERRUPT_ALIGN
310
.align INTERRUPT_ALIGN
276
interrupt_handlers:
311
interrupt_handlers:
277
h_start:
312
h_start:
278
	handler 0 IDT_ITEMS
313
	handler 0 IDT_ITEMS
279
h_end:
314
h_end:
280
 
315
 
281
.data
316
.data
282
.global interrupt_handler_size
317
.global interrupt_handler_size
283
 
318
 
284
interrupt_handler_size: .long (h_end - h_start) / IDT_ITEMS
319
interrupt_handler_size: .long (h_end - h_start) / IDT_ITEMS
285
 
320