[e3b9572] | 1 | #
|
---|
[df4ed85] | 2 | # Copyright (c) 2005 Ondrej Palkovsky
|
---|
[e3b9572] | 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 |
|
---|
[1f7cb3a] | 29 | #define IREGISTER_SPACE 72
|
---|
| 30 |
|
---|
| 31 | #define IOFFSET_RAX 0x0
|
---|
| 32 | #define IOFFSET_RCX 0x8
|
---|
| 33 | #define IOFFSET_RDX 0x10
|
---|
| 34 | #define IOFFSET_RSI 0x18
|
---|
| 35 | #define IOFFSET_RDI 0x20
|
---|
| 36 | #define IOFFSET_R8 0x28
|
---|
| 37 | #define IOFFSET_R9 0x30
|
---|
| 38 | #define IOFFSET_R10 0x38
|
---|
| 39 | #define IOFFSET_R11 0x40
|
---|
[e3b9572] | 40 |
|
---|
| 41 | # Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int has no error word
|
---|
| 42 | # and 1 means interrupt with error word
|
---|
| 43 | #define ERROR_WORD_INTERRUPT_LIST 0x00027D00
|
---|
| 44 |
|
---|
| 45 | #include <arch/pm.h>
|
---|
[fa2d382] | 46 | #include <arch/mm/page.h>
|
---|
[e3b9572] | 47 |
|
---|
| 48 | .text
|
---|
| 49 | .global interrupt_handlers
|
---|
[dd4d6b0] | 50 | .global syscall_entry
|
---|
[e3b9572] | 51 | .global panic_printf
|
---|
| 52 |
|
---|
| 53 | panic_printf:
|
---|
| 54 | movq $halt, (%rsp)
|
---|
| 55 | jmp printf
|
---|
| 56 |
|
---|
[36b209a] | 57 | .global cpuid
|
---|
[7df54df] | 58 | .global has_cpuid
|
---|
[134877d] | 59 | .global get_cycle
|
---|
[89344d85] | 60 | .global read_efer_flag
|
---|
| 61 | .global set_efer_flag
|
---|
[2b17f47] | 62 | .global memsetb
|
---|
| 63 | .global memsetw
|
---|
[e3c762cd] | 64 | .global memcpy
|
---|
| 65 | .global memcpy_from_uspace
|
---|
| 66 | .global memcpy_to_uspace
|
---|
| 67 | .global memcpy_from_uspace_failover_address
|
---|
| 68 | .global memcpy_to_uspace_failover_address
|
---|
| 69 |
|
---|
[2b17f47] | 70 | # Wrapper for generic memsetb
|
---|
| 71 | memsetb:
|
---|
| 72 | jmp _memsetb
|
---|
| 73 |
|
---|
| 74 | # Wrapper for generic memsetw
|
---|
| 75 | memsetw:
|
---|
| 76 | jmp _memsetw
|
---|
| 77 |
|
---|
[e3c762cd] | 78 | #define MEMCPY_DST %rdi
|
---|
| 79 | #define MEMCPY_SRC %rsi
|
---|
| 80 | #define MEMCPY_SIZE %rdx
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * Copy memory from/to userspace.
|
---|
| 84 | *
|
---|
| 85 | * This is almost conventional memcpy().
|
---|
| 86 | * The difference is that there is a failover part
|
---|
| 87 | * to where control is returned from a page fault if
|
---|
| 88 | * the page fault occurs during copy_from_uspace()
|
---|
| 89 | * or copy_to_uspace().
|
---|
| 90 | *
|
---|
| 91 | * @param MEMCPY_DST Destination address.
|
---|
| 92 | * @param MEMCPY_SRC Source address.
|
---|
| 93 | * @param MEMCPY_SIZE Number of bytes to copy.
|
---|
| 94 | *
|
---|
[da349da0] | 95 | * @retrun MEMCPY_DST on success, 0 on failure.
|
---|
[e3c762cd] | 96 | */
|
---|
| 97 | memcpy:
|
---|
| 98 | memcpy_from_uspace:
|
---|
| 99 | memcpy_to_uspace:
|
---|
[da349da0] | 100 | movq MEMCPY_DST, %rax
|
---|
[e3c762cd] | 101 |
|
---|
| 102 | movq MEMCPY_SIZE, %rcx
|
---|
| 103 | shrq $3, %rcx /* size / 8 */
|
---|
| 104 |
|
---|
| 105 | rep movsq /* copy as much as possible word by word */
|
---|
| 106 |
|
---|
| 107 | movq MEMCPY_SIZE, %rcx
|
---|
| 108 | andq $7, %rcx /* size % 8 */
|
---|
| 109 | jz 0f
|
---|
| 110 |
|
---|
| 111 | rep movsb /* copy the rest byte by byte */
|
---|
[89344d85] | 112 |
|
---|
[e3c762cd] | 113 | 0:
|
---|
| 114 | ret /* return MEMCPY_SRC, success */
|
---|
| 115 |
|
---|
| 116 | memcpy_from_uspace_failover_address:
|
---|
| 117 | memcpy_to_uspace_failover_address:
|
---|
| 118 | xorq %rax, %rax /* return 0, failure */
|
---|
| 119 | ret
|
---|
| 120 |
|
---|
[7df54df] | 121 | ## Determine CPUID support
|
---|
| 122 | #
|
---|
| 123 | # Return 0 in EAX if CPUID is not support, 1 if supported.
|
---|
| 124 | #
|
---|
| 125 | has_cpuid:
|
---|
| 126 | pushfq # store flags
|
---|
| 127 | popq %rax # read flags
|
---|
[d6dcdd2e] | 128 | movq %rax,%rdx # copy flags
|
---|
| 129 | btcl $21,%edx # swap the ID bit
|
---|
| 130 | pushq %rdx
|
---|
[7df54df] | 131 | popfq # propagate the change into flags
|
---|
| 132 | pushfq
|
---|
[d6dcdd2e] | 133 | popq %rdx # read flags
|
---|
[7df54df] | 134 | andl $(1<<21),%eax # interested only in ID bit
|
---|
[d6dcdd2e] | 135 | andl $(1<<21),%edx
|
---|
| 136 | xorl %edx,%eax # 0 if not supported, 1 if supported
|
---|
[7df54df] | 137 | ret
|
---|
| 138 |
|
---|
[89344d85] | 139 | cpuid:
|
---|
| 140 | movq %rbx, %r10 # we have to preserve rbx across function calls
|
---|
| 141 |
|
---|
| 142 | movl %edi,%eax # load the command into %eax
|
---|
| 143 |
|
---|
| 144 | cpuid
|
---|
| 145 | movl %eax,0(%rsi)
|
---|
| 146 | movl %ebx,4(%rsi)
|
---|
| 147 | movl %ecx,8(%rsi)
|
---|
| 148 | movl %edx,12(%rsi)
|
---|
| 149 |
|
---|
| 150 | movq %r10, %rbx
|
---|
| 151 | ret
|
---|
[7df54df] | 152 |
|
---|
[134877d] | 153 | get_cycle:
|
---|
[7df54df] | 154 | xorq %rax,%rax
|
---|
| 155 | rdtsc
|
---|
| 156 | ret
|
---|
[89344d85] | 157 |
|
---|
| 158 | set_efer_flag:
|
---|
| 159 | movq $0xc0000080, %rcx
|
---|
| 160 | rdmsr
|
---|
| 161 | btsl %edi, %eax
|
---|
| 162 | wrmsr
|
---|
| 163 | ret
|
---|
[7df54df] | 164 |
|
---|
[89344d85] | 165 | read_efer_flag:
|
---|
| 166 | movq $0xc0000080, %rcx
|
---|
| 167 | rdmsr
|
---|
| 168 | ret
|
---|
[7df54df] | 169 |
|
---|
[1f7cb3a] | 170 | # Push all volatile general purpose registers on stack
|
---|
[49a39c2] | 171 | .macro save_all_gpr
|
---|
| 172 | movq %rax, IOFFSET_RAX(%rsp)
|
---|
| 173 | movq %rcx, IOFFSET_RCX(%rsp)
|
---|
| 174 | movq %rdx, IOFFSET_RDX(%rsp)
|
---|
| 175 | movq %rsi, IOFFSET_RSI(%rsp)
|
---|
| 176 | movq %rdi, IOFFSET_RDI(%rsp)
|
---|
| 177 | movq %r8, IOFFSET_R8(%rsp)
|
---|
| 178 | movq %r9, IOFFSET_R9(%rsp)
|
---|
| 179 | movq %r10, IOFFSET_R10(%rsp)
|
---|
| 180 | movq %r11, IOFFSET_R11(%rsp)
|
---|
[e3b9572] | 181 | .endm
|
---|
| 182 |
|
---|
[49a39c2] | 183 | .macro restore_all_gpr
|
---|
| 184 | movq IOFFSET_RAX(%rsp), %rax
|
---|
| 185 | movq IOFFSET_RCX(%rsp), %rcx
|
---|
| 186 | movq IOFFSET_RDX(%rsp), %rdx
|
---|
| 187 | movq IOFFSET_RSI(%rsp), %rsi
|
---|
| 188 | movq IOFFSET_RDI(%rsp), %rdi
|
---|
| 189 | movq IOFFSET_R8(%rsp), %r8
|
---|
| 190 | movq IOFFSET_R9(%rsp), %r9
|
---|
| 191 | movq IOFFSET_R10(%rsp), %r10
|
---|
| 192 | movq IOFFSET_R11(%rsp), %r11
|
---|
[e3b9572] | 193 | .endm
|
---|
[8e0eb63] | 194 |
|
---|
[1f7cb3a] | 195 | #define INTERRUPT_ALIGN 128
|
---|
[8d25b44] | 196 |
|
---|
[e3b9572] | 197 | ## Declare interrupt handlers
|
---|
| 198 | #
|
---|
| 199 | # Declare interrupt handlers for n interrupt
|
---|
| 200 | # vectors starting at vector i.
|
---|
| 201 | #
|
---|
[8e0eb63] | 202 | # The handlers call exc_dispatch().
|
---|
[e3b9572] | 203 | #
|
---|
| 204 | .macro handler i n
|
---|
| 205 |
|
---|
[8e0eb63] | 206 | /*
|
---|
[296426ad] | 207 | * Choose between version with error code and version without error
|
---|
| 208 | * code. Both versions have to be of the same size. amd64 assembly is,
|
---|
| 209 | * however, a little bit tricky. For instance, subq $0x80, %rsp and
|
---|
| 210 | * subq $0x78, %rsp can result in two instructions with different
|
---|
| 211 | * op-code lengths.
|
---|
[e1be3b6] | 212 | * Therefore we align the interrupt handlers.
|
---|
[8e0eb63] | 213 | */
|
---|
| 214 |
|
---|
| 215 | .iflt \i-32
|
---|
| 216 | .if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
|
---|
| 217 | /*
|
---|
| 218 | * Version with error word.
|
---|
| 219 | */
|
---|
| 220 | subq $IREGISTER_SPACE, %rsp
|
---|
| 221 | .else
|
---|
| 222 | /*
|
---|
| 223 | * Version without error word,
|
---|
| 224 | */
|
---|
| 225 | subq $(IREGISTER_SPACE+8), %rsp
|
---|
| 226 | .endif
|
---|
| 227 | .else
|
---|
| 228 | /*
|
---|
| 229 | * Version without error word,
|
---|
| 230 | */
|
---|
| 231 | subq $(IREGISTER_SPACE+8), %rsp
|
---|
| 232 | .endif
|
---|
[e3b9572] | 233 |
|
---|
[8e0eb63] | 234 | save_all_gpr
|
---|
[e13daa5d] | 235 | cld
|
---|
[e3b9572] | 236 |
|
---|
[8e0eb63] | 237 | movq $(\i), %rdi # %rdi - first parameter
|
---|
| 238 | movq %rsp, %rsi # %rsi - pointer to istate
|
---|
| 239 | call exc_dispatch # exc_dispatch(i, istate)
|
---|
| 240 |
|
---|
[49a39c2] | 241 | restore_all_gpr
|
---|
| 242 | # $8 = Skip error word
|
---|
[8e0eb63] | 243 | addq $(IREGISTER_SPACE+8), %rsp
|
---|
[e3b9572] | 244 | iretq
|
---|
| 245 |
|
---|
[8d25b44] | 246 | .align INTERRUPT_ALIGN
|
---|
[e3b9572] | 247 | .if (\n-\i)-1
|
---|
| 248 | handler "(\i+1)",\n
|
---|
| 249 | .endif
|
---|
| 250 | .endm
|
---|
[8d25b44] | 251 |
|
---|
| 252 | .align INTERRUPT_ALIGN
|
---|
[e3b9572] | 253 | interrupt_handlers:
|
---|
| 254 | h_start:
|
---|
| 255 | handler 0 IDT_ITEMS
|
---|
| 256 | h_end:
|
---|
[dd4d6b0] | 257 |
|
---|
[296426ad] | 258 | ## Low-level syscall handler
|
---|
| 259 | #
|
---|
| 260 | # Registers on entry:
|
---|
| 261 | #
|
---|
| 262 | # @param rcx Userspace return address.
|
---|
| 263 | # @param r11 Userspace RLFAGS.
|
---|
| 264 | #
|
---|
| 265 | # @param rax Syscall number.
|
---|
| 266 | # @param rdi 1st syscall argument.
|
---|
| 267 | # @param rsi 2nd syscall argument.
|
---|
| 268 | # @param rdx 3rd syscall argument.
|
---|
| 269 | # @param r10 4th syscall argument. Used instead of RCX because the
|
---|
| 270 | # SYSCALL instruction clobbers it.
|
---|
| 271 | # @param r8 5th syscall argument.
|
---|
| 272 | # @param r9 6th syscall argument.
|
---|
| 273 | #
|
---|
| 274 | # @return Return value is in rax.
|
---|
| 275 | #
|
---|
[dd4d6b0] | 276 | syscall_entry:
|
---|
[296426ad] | 277 | swapgs # Switch to hidden gs
|
---|
| 278 | #
|
---|
| 279 | # %gs:0 Scratch space for this thread's user RSP
|
---|
| 280 | # %gs:8 Address to be used as this thread's kernel RSP
|
---|
| 281 | #
|
---|
| 282 | movq %rsp, %gs:0 # Save this thread's user RSP
|
---|
| 283 | movq %gs:8, %rsp # Set this thread's kernel RSP
|
---|
| 284 | swapgs # Switch back to remain consistent
|
---|
[6d9c49a] | 285 | sti
|
---|
[c7c0b89b] | 286 |
|
---|
[296426ad] | 287 | pushq %rcx
|
---|
| 288 | pushq %r11
|
---|
| 289 |
|
---|
| 290 | movq %r10, %rcx # Copy the 4th argument where it is expected
|
---|
| 291 | pushq %rax
|
---|
[dd4d6b0] | 292 | call syscall_handler
|
---|
[296426ad] | 293 | addq $8, %rsp
|
---|
[6d9c49a] | 294 |
|
---|
[37b451f7] | 295 | popq %r11
|
---|
| 296 | popq %rcx
|
---|
[296426ad] | 297 |
|
---|
| 298 | cli
|
---|
| 299 | swapgs
|
---|
| 300 | movq %gs:0, %rsp # Restore the user RSP
|
---|
| 301 | swapgs
|
---|
| 302 |
|
---|
[37b451f7] | 303 | sysretq
|
---|
[296426ad] | 304 |
|
---|
[e3b9572] | 305 | .data
|
---|
| 306 | .global interrupt_handler_size
|
---|
| 307 |
|
---|
[42744880] | 308 | interrupt_handler_size: .quad (h_end-h_start)/IDT_ITEMS
|
---|