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