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