2
# Copyright (c) 2001-2004 Jakub Jermar
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
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.
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.
29
## very low and hardware-level functions
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
38
.global enable_l_apic_in_msr
39
.global interrupt_handlers
43
.global memcpy_from_uspace
44
.global memcpy_from_uspace_failover_address
45
.global memcpy_to_uspace
46
.global memcpy_to_uspace_failover_address
49
# Wrapper for generic memsetb
53
# Wrapper for generic memsetw
60
#define MEMCPY_SIZE 12
62
/** Copy memory to/from userspace.
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().
70
* @param MEMCPY_DST(%esp) Destination address.
71
* @param MEMCPY_SRC(%esp) Source address.
72
* @param MEMCPY_SIZE(%esp) Size.
74
* @return MEMCPY_DST(%esp) on success and 0 on failure.
79
movl %edi, %edx /* save %edi */
80
movl %esi, %eax /* save %esi */
82
movl MEMCPY_SIZE(%esp), %ecx
83
shrl $2, %ecx /* size / 4 */
85
movl MEMCPY_DST(%esp), %edi
86
movl MEMCPY_SRC(%esp), %esi
88
rep movsl /* copy whole words */
90
movl MEMCPY_SIZE(%esp), %ecx
91
andl $3, %ecx /* size % 4 */
94
rep movsb /* copy the rest byte by byte */
99
movl MEMCPY_DST(%esp), %eax /* MEMCPY_DST(%esp), success */
103
* We got here from as_page_fault() after the memory operations
104
* above had caused a page fault.
106
memcpy_from_uspace_failover_address:
107
memcpy_to_uspace_failover_address:
110
xorl %eax, %eax /* return 0, failure */
115
# Enable paging and write-back caching in CR0.
119
orl $(1 << 31), %edx # paging on
120
# clear Cache Disable and not Write Though
121
andl $~((1 << 30) | (1 << 29)), %edx
130
# Enable local APIC in MSR.
132
enable_l_apic_in_msr:
136
orl $(0xfee00000), %eax
145
and $0xffffbfff, %ecx
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.
159
.global sysenter_handler
162
pushl %ebp # remember user stack
163
pushl %edi # remember return user address
165
pushl %gs # remember TLS
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
180
addl $28, %esp # remove arguments from stack
182
pop %gs # restore TLS
184
pop %edx # prepare return EIP for SYSEXIT
185
pop %ecx # prepare userspace ESP for SYSEXIT
187
sysexit # return to userspace
190
## Declare interrupt handlers
192
# Declare interrupt handlers for n interrupt
193
# vectors starting at vector i.
195
# The handlers setup data segment registers
196
# and call exc_dispatch().
198
#define INTERRUPT_ALIGN 64
201
.ifeq \i - 0x30 # Syscall handler
208
# Push syscall arguments onto the stack
210
# NOTE: The idea behind the order of arguments passed in registers is to
211
# use all scratch registers first and preserved registers next.
212
# An optimized libc syscall wrapper can make use of this setup.
222
# we must fill the data segment registers
229
# syscall_handler(edx, ecx, ebx, esi, edi, ebp, eax)
232
addl $28, %esp # clean-up of parameters
243
* This macro distinguishes between two versions of ia32 exceptions.
244
* One version has error word and the other does not have it.
245
* The latter version fakes the error word on the stack so that the
246
* handlers and istate_t can be the same for both types.
249
.if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
251
* With error word, do nothing
255
* Version without error word,
261
* Version without error word,
275
# we must fill the data segment registers
284
call exc_dispatch # excdispatch(intnum, *istate)
285
addl $8, %esp # Clear arguments from stack
287
CLEAR_NT_FLAG # Modifies %ecx
298
addl $4, %esp # Skip error word, no matter whether real or fake.
302
.align INTERRUPT_ALIGN
304
handler "(\i + 1)", \n
308
# keep in sync with pm.h !!!
310
.align INTERRUPT_ALIGN
317
.global interrupt_handler_size
319
interrupt_handler_size: .long (h_end - h_start) / IDT_ITEMS