Subversion Repositories HelenOS

Rev

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

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