1 | #
|
---|
2 | # Copyright (C) 2001-2004 Jakub Jermar
|
---|
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 |
|
---|
31 | # Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word
|
---|
32 | # and 1 means interrupt with error word
|
---|
33 | #define ERROR_WORD_INTERRUPT_LIST 0x00027D00
|
---|
34 |
|
---|
35 | .text
|
---|
36 |
|
---|
37 | .global enable_l_apic_in_msr
|
---|
38 | .global interrupt_handlers
|
---|
39 | .global memcpy
|
---|
40 | .global memcpy_from_uspace
|
---|
41 | .global memcpy_from_uspace_failover_address
|
---|
42 | .global memcpy_to_uspace
|
---|
43 | .global memcpy_to_uspace_failover_address
|
---|
44 |
|
---|
45 |
|
---|
46 | #define MEMCPY_DST 4
|
---|
47 | #define MEMCPY_SRC 8
|
---|
48 | #define MEMCPY_SIZE 12
|
---|
49 |
|
---|
50 | /** Copy memory to/from userspace.
|
---|
51 | *
|
---|
52 | * This is almost conventional memcpy().
|
---|
53 | * The difference is that there is a failover part
|
---|
54 | * to where control is returned from a page fault
|
---|
55 | * if the page fault occurs during copy_from_uspace()
|
---|
56 | * or copy_to_uspace().
|
---|
57 | *
|
---|
58 | * @param MEMCPY_DST(%esp) Destination address.
|
---|
59 | * @param MEMCPY_SRC(%esp) Source address.
|
---|
60 | * @param MEMCPY_SIZE(%esp) Size.
|
---|
61 | *
|
---|
62 | * @return MEMCPY_SRC(%esp) on success and 0 on failure.
|
---|
63 | */
|
---|
64 | memcpy:
|
---|
65 | memcpy_from_uspace:
|
---|
66 | memcpy_to_uspace:
|
---|
67 | movl %edi, %edx /* save %edi */
|
---|
68 | movl %esi, %eax /* save %esi */
|
---|
69 |
|
---|
70 | movl MEMCPY_SIZE(%esp), %ecx
|
---|
71 | shrl $2, %ecx /* size / 4 */
|
---|
72 |
|
---|
73 | movl MEMCPY_DST(%esp), %edi
|
---|
74 | movl MEMCPY_SRC(%esp), %esi
|
---|
75 |
|
---|
76 | rep movsl /* copy as much as possible word by word */
|
---|
77 |
|
---|
78 | movl MEMCPY_SIZE(%esp), %ecx
|
---|
79 | andl $3, %ecx /* size % 4 */
|
---|
80 | jz 0f
|
---|
81 |
|
---|
82 | rep movsb /* copy the rest byte by byte */
|
---|
83 |
|
---|
84 | 0:
|
---|
85 | movl %edx, %edi
|
---|
86 | movl %eax, %esi
|
---|
87 | movl MEMCPY_SRC(%esp), %eax /* MEMCPY_SRC(%esp), success */
|
---|
88 | ret
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * We got here from as_page_fault() after the memory operations
|
---|
92 | * above had caused a page fault.
|
---|
93 | */
|
---|
94 | memcpy_from_uspace_failover_address:
|
---|
95 | memcpy_to_uspace_failover_address:
|
---|
96 | movl %edx, %edi
|
---|
97 | movl %eax, %esi
|
---|
98 | xorl %eax, %eax /* return 0, failure */
|
---|
99 | ret
|
---|
100 |
|
---|
101 |
|
---|
102 | ## Enable local APIC
|
---|
103 | #
|
---|
104 | # Enable local APIC in MSR.
|
---|
105 | #
|
---|
106 | enable_l_apic_in_msr:
|
---|
107 | push %eax
|
---|
108 |
|
---|
109 | movl $0x1b, %ecx
|
---|
110 | rdmsr
|
---|
111 | orl $(1<<11),%eax
|
---|
112 | orl $(0xfee00000),%eax
|
---|
113 | wrmsr
|
---|
114 |
|
---|
115 | pop %eax
|
---|
116 | ret
|
---|
117 |
|
---|
118 | # Clear nested flag
|
---|
119 | # overwrites %ecx
|
---|
120 | .macro CLEAR_NT_FLAG
|
---|
121 | pushfl
|
---|
122 | pop %ecx
|
---|
123 | and $0xffffbfff,%ecx
|
---|
124 | push %ecx
|
---|
125 | popfl
|
---|
126 | .endm
|
---|
127 |
|
---|
128 | ## Declare interrupt handlers
|
---|
129 | #
|
---|
130 | # Declare interrupt handlers for n interrupt
|
---|
131 | # vectors starting at vector i.
|
---|
132 | #
|
---|
133 | # The handlers setup data segment registers
|
---|
134 | # and call exc_dispatch().
|
---|
135 | #
|
---|
136 | #define INTERRUPT_ALIGN 64
|
---|
137 | .macro handler i n
|
---|
138 |
|
---|
139 | .ifeq \i-0x30 # Syscall handler
|
---|
140 | push %ds
|
---|
141 | push %es
|
---|
142 | push %fs
|
---|
143 | push %gs
|
---|
144 |
|
---|
145 | # Push arguments on stack
|
---|
146 | push %edi
|
---|
147 | push %esi
|
---|
148 | push %edx
|
---|
149 | push %ecx
|
---|
150 | push %eax
|
---|
151 |
|
---|
152 | # we must fill the data segment registers
|
---|
153 | movw $16,%ax
|
---|
154 | movw %ax,%ds
|
---|
155 | movw %ax,%es
|
---|
156 |
|
---|
157 | sti
|
---|
158 |
|
---|
159 | call syscall_handler # syscall_handler(ax,cx,dx,si,di)
|
---|
160 | cli
|
---|
161 | addl $20, %esp # clean-up of parameters
|
---|
162 |
|
---|
163 | pop %gs
|
---|
164 | pop %fs
|
---|
165 | pop %es
|
---|
166 | pop %ds
|
---|
167 |
|
---|
168 | CLEAR_NT_FLAG
|
---|
169 | iret
|
---|
170 | .else
|
---|
171 | /*
|
---|
172 | * This macro distinguishes between two versions of ia32 exceptions.
|
---|
173 | * One version has error word and the other does not have it.
|
---|
174 | * The latter version fakes the error word on the stack so that the
|
---|
175 | * handlers and istate_t can be the same for both types.
|
---|
176 | */
|
---|
177 | .iflt \i-32
|
---|
178 | .if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
|
---|
179 | /*
|
---|
180 | * With error word, do nothing
|
---|
181 | */
|
---|
182 | .else
|
---|
183 | /*
|
---|
184 | * Version without error word,
|
---|
185 | */
|
---|
186 | subl $4, %esp
|
---|
187 | .endif
|
---|
188 | .else
|
---|
189 | /*
|
---|
190 | * Version without error word,
|
---|
191 | */
|
---|
192 | subl $4, %esp
|
---|
193 | .endif
|
---|
194 |
|
---|
195 | push %ds
|
---|
196 | push %es
|
---|
197 | push %fs
|
---|
198 | push %gs
|
---|
199 |
|
---|
200 | #ifdef CONFIG_DEBUG_ALLREGS
|
---|
201 | push %ebx
|
---|
202 | push %ebp
|
---|
203 | push %edi
|
---|
204 | push %esi
|
---|
205 | #else
|
---|
206 | sub $16, %esp
|
---|
207 | #endif
|
---|
208 | push %edx
|
---|
209 | push %ecx
|
---|
210 | push %eax
|
---|
211 |
|
---|
212 | # we must fill the data segment registers
|
---|
213 | movw $16,%ax
|
---|
214 | movw %ax,%ds
|
---|
215 | movw %ax,%es
|
---|
216 |
|
---|
217 | pushl %esp # *istate
|
---|
218 | pushl $(\i) # intnum
|
---|
219 | call exc_dispatch # excdispatch(intnum, *istate)
|
---|
220 | addl $8,%esp # Clear arguments from stack
|
---|
221 |
|
---|
222 | CLEAR_NT_FLAG # Modifies %ecx
|
---|
223 |
|
---|
224 | pop %eax
|
---|
225 | pop %ecx
|
---|
226 | pop %edx
|
---|
227 | #ifdef CONFIG_DEBUG_ALLREGS
|
---|
228 | pop %esi
|
---|
229 | pop %edi
|
---|
230 | pop %ebp
|
---|
231 | pop %ebx
|
---|
232 | #else
|
---|
233 | add $16, %esp
|
---|
234 | #endif
|
---|
235 |
|
---|
236 | pop %gs
|
---|
237 | pop %fs
|
---|
238 | pop %es
|
---|
239 | pop %ds
|
---|
240 |
|
---|
241 | addl $4,%esp # Skip error word, no matter whether real or fake.
|
---|
242 | iret
|
---|
243 | .endif
|
---|
244 |
|
---|
245 | .align INTERRUPT_ALIGN
|
---|
246 | .if (\n-\i)-1
|
---|
247 | handler "(\i+1)",\n
|
---|
248 | .endif
|
---|
249 | .endm
|
---|
250 |
|
---|
251 | # keep in sync with pm.h !!!
|
---|
252 | IDT_ITEMS=64
|
---|
253 | .align INTERRUPT_ALIGN
|
---|
254 | interrupt_handlers:
|
---|
255 | h_start:
|
---|
256 | handler 0 IDT_ITEMS
|
---|
257 | h_end:
|
---|
258 |
|
---|
259 | .data
|
---|
260 | .global interrupt_handler_size
|
---|
261 |
|
---|
262 | interrupt_handler_size: .long (h_end-h_start)/IDT_ITEMS
|
---|