| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Ondrej Palkovsky
|
|---|
| 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 | #include <arch/pm.h>
|
|---|
| 30 | #include <arch/mm/page.h>
|
|---|
| 31 |
|
|---|
| 32 | .text
|
|---|
| 33 | .global interrupt_handlers
|
|---|
| 34 | .global syscall_entry
|
|---|
| 35 | .global cpuid
|
|---|
| 36 | .global has_cpuid
|
|---|
| 37 | .global read_efer_flag
|
|---|
| 38 | .global set_efer_flag
|
|---|
| 39 | .global memcpy_from_uspace
|
|---|
| 40 | .global memcpy_to_uspace
|
|---|
| 41 | .global memcpy_from_uspace_failover_address
|
|---|
| 42 | .global memcpy_to_uspace_failover_address
|
|---|
| 43 | .global early_putchar
|
|---|
| 44 |
|
|---|
| 45 | #define MEMCPY_DST %rdi
|
|---|
| 46 | #define MEMCPY_SRC %rsi
|
|---|
| 47 | #define MEMCPY_SIZE %rdx
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Copy memory from/to 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 if
|
|---|
| 55 | * the page fault occurs during copy_from_uspace()
|
|---|
| 56 | * or copy_to_uspace().
|
|---|
| 57 | *
|
|---|
| 58 | * @param MEMCPY_DST Destination address.
|
|---|
| 59 | * @param MEMCPY_SRC Source address.
|
|---|
| 60 | * @param MEMCPY_SIZE Number of bytes to copy.
|
|---|
| 61 | *
|
|---|
| 62 | * @retrun MEMCPY_DST on success, 0 on failure.
|
|---|
| 63 | *
|
|---|
| 64 | */
|
|---|
| 65 | memcpy_from_uspace:
|
|---|
| 66 | memcpy_to_uspace:
|
|---|
| 67 | movq MEMCPY_DST, %rax
|
|---|
| 68 |
|
|---|
| 69 | movq MEMCPY_SIZE, %rcx
|
|---|
| 70 | shrq $3, %rcx /* size / 8 */
|
|---|
| 71 |
|
|---|
| 72 | rep movsq /* copy as much as possible word by word */
|
|---|
| 73 |
|
|---|
| 74 | movq MEMCPY_SIZE, %rcx
|
|---|
| 75 | andq $7, %rcx /* size % 8 */
|
|---|
| 76 | jz 0f
|
|---|
| 77 |
|
|---|
| 78 | rep movsb /* copy the rest byte by byte */
|
|---|
| 79 |
|
|---|
| 80 | 0:
|
|---|
| 81 | ret /* return MEMCPY_SRC, success */
|
|---|
| 82 |
|
|---|
| 83 | memcpy_from_uspace_failover_address:
|
|---|
| 84 | memcpy_to_uspace_failover_address:
|
|---|
| 85 | xorl %eax, %eax /* return 0, failure */
|
|---|
| 86 | ret
|
|---|
| 87 |
|
|---|
| 88 | /** Determine CPUID support
|
|---|
| 89 | *
|
|---|
| 90 | * @return 0 in EAX if CPUID is not support, 1 if supported.
|
|---|
| 91 | *
|
|---|
| 92 | */
|
|---|
| 93 | has_cpuid:
|
|---|
| 94 | /* Load RFLAGS */
|
|---|
| 95 | pushfq
|
|---|
| 96 | popq %rax
|
|---|
| 97 | movq %rax, %rdx
|
|---|
| 98 |
|
|---|
| 99 | /* Flip the ID bit */
|
|---|
| 100 | btcl $21, %edx
|
|---|
| 101 |
|
|---|
| 102 | /* Store RFLAGS */
|
|---|
| 103 | pushq %rdx
|
|---|
| 104 | popfq
|
|---|
| 105 | pushfq
|
|---|
| 106 |
|
|---|
| 107 | /* Get the ID bit again */
|
|---|
| 108 | popq %rdx
|
|---|
| 109 | andl $(1 << 21), %eax
|
|---|
| 110 | andl $(1 << 21), %edx
|
|---|
| 111 |
|
|---|
| 112 | /* 0 if not supported, 1 if supported */
|
|---|
| 113 | xorl %edx, %eax
|
|---|
| 114 | ret
|
|---|
| 115 |
|
|---|
| 116 | cpuid:
|
|---|
| 117 | /* Preserve %rbx across function calls */
|
|---|
| 118 | movq %rbx, %r10
|
|---|
| 119 |
|
|---|
| 120 | /* Load the command into %eax */
|
|---|
| 121 | movl %edi, %eax
|
|---|
| 122 |
|
|---|
| 123 | cpuid
|
|---|
| 124 | movl %eax, 0(%rsi)
|
|---|
| 125 | movl %ebx, 4(%rsi)
|
|---|
| 126 | movl %ecx, 8(%rsi)
|
|---|
| 127 | movl %edx, 12(%rsi)
|
|---|
| 128 |
|
|---|
| 129 | movq %r10, %rbx
|
|---|
| 130 | ret
|
|---|
| 131 |
|
|---|
| 132 | set_efer_flag:
|
|---|
| 133 | movl $0xc0000080, %ecx
|
|---|
| 134 | rdmsr
|
|---|
| 135 | btsl %edi, %eax
|
|---|
| 136 | wrmsr
|
|---|
| 137 | ret
|
|---|
| 138 |
|
|---|
| 139 | read_efer_flag:
|
|---|
| 140 | movl $0xc0000080, %ecx
|
|---|
| 141 | rdmsr
|
|---|
| 142 | ret
|
|---|
| 143 |
|
|---|
| 144 | #define ISTATE_OFFSET_RAX 0
|
|---|
| 145 | #define ISTATE_OFFSET_RBX 8
|
|---|
| 146 | #define ISTATE_OFFSET_RCX 16
|
|---|
| 147 | #define ISTATE_OFFSET_RDX 24
|
|---|
| 148 | #define ISTATE_OFFSET_RSI 32
|
|---|
| 149 | #define ISTATE_OFFSET_RDI 40
|
|---|
| 150 | #define ISTATE_OFFSET_RBP 48
|
|---|
| 151 | #define ISTATE_OFFSET_R8 56
|
|---|
| 152 | #define ISTATE_OFFSET_R9 64
|
|---|
| 153 | #define ISTATE_OFFSET_R10 72
|
|---|
| 154 | #define ISTATE_OFFSET_R11 80
|
|---|
| 155 | #define ISTATE_OFFSET_R12 88
|
|---|
| 156 | #define ISTATE_OFFSET_R13 96
|
|---|
| 157 | #define ISTATE_OFFSET_R14 104
|
|---|
| 158 | #define ISTATE_OFFSET_R15 112
|
|---|
| 159 | #define ISTATE_OFFSET_ALIGNMENT 120
|
|---|
| 160 | #define ISTATE_OFFSET_RBP_FRAME 128
|
|---|
| 161 | #define ISTATE_OFFSET_RIP_FRAME 136
|
|---|
| 162 | #define ISTATE_OFFSET_ERROR_WORD 144
|
|---|
| 163 | #define ISTATE_OFFSET_RIP 152
|
|---|
| 164 | #define ISTATE_OFFSET_CS 160
|
|---|
| 165 | #define ISTATE_OFFSET_RFLAGS 168
|
|---|
| 166 | #define ISTATE_OFFSET_RSP 176
|
|---|
| 167 | #define ISTATE_OFFSET_SS 184
|
|---|
| 168 |
|
|---|
| 169 | /*
|
|---|
| 170 | * Size of the istate structure without the hardware-saved part and without the
|
|---|
| 171 | * error word.
|
|---|
| 172 | */
|
|---|
| 173 | #define ISTATE_SOFT_SIZE 144
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int
|
|---|
| 177 | * has no error word and 1 means interrupt with error word
|
|---|
| 178 | *
|
|---|
| 179 | */
|
|---|
| 180 | #define ERROR_WORD_INTERRUPT_LIST 0x00027D00
|
|---|
| 181 |
|
|---|
| 182 | .macro handler i
|
|---|
| 183 | .global int_\i
|
|---|
| 184 | int_\i:
|
|---|
| 185 |
|
|---|
| 186 | /*
|
|---|
| 187 | * Choose between version with error code and version without error
|
|---|
| 188 | * code.
|
|---|
| 189 | */
|
|---|
| 190 |
|
|---|
| 191 | .iflt \i-32
|
|---|
| 192 | .if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
|
|---|
| 193 | /*
|
|---|
| 194 | * Version with error word.
|
|---|
| 195 | */
|
|---|
| 196 | subq $ISTATE_SOFT_SIZE, %rsp
|
|---|
| 197 | .else
|
|---|
| 198 | /*
|
|---|
| 199 | * Version without error word.
|
|---|
| 200 | */
|
|---|
| 201 | subq $(ISTATE_SOFT_SIZE + 8), %rsp
|
|---|
| 202 | .endif
|
|---|
| 203 | .else
|
|---|
| 204 | /*
|
|---|
| 205 | * Version without error word.
|
|---|
| 206 | */
|
|---|
| 207 | subq $(ISTATE_SOFT_SIZE + 8), %rsp
|
|---|
| 208 | .endif
|
|---|
| 209 |
|
|---|
| 210 | /*
|
|---|
| 211 | * Save the general purpose registers.
|
|---|
| 212 | */
|
|---|
| 213 | movq %rax, ISTATE_OFFSET_RAX(%rsp)
|
|---|
| 214 | movq %rbx, ISTATE_OFFSET_RBX(%rsp)
|
|---|
| 215 | movq %rcx, ISTATE_OFFSET_RCX(%rsp)
|
|---|
| 216 | movq %rdx, ISTATE_OFFSET_RDX(%rsp)
|
|---|
| 217 | movq %rsi, ISTATE_OFFSET_RSI(%rsp)
|
|---|
| 218 | movq %rdi, ISTATE_OFFSET_RDI(%rsp)
|
|---|
| 219 | movq %rbp, ISTATE_OFFSET_RBP(%rsp)
|
|---|
| 220 | movq %r8, ISTATE_OFFSET_R8(%rsp)
|
|---|
| 221 | movq %r9, ISTATE_OFFSET_R9(%rsp)
|
|---|
| 222 | movq %r10, ISTATE_OFFSET_R10(%rsp)
|
|---|
| 223 | movq %r11, ISTATE_OFFSET_R11(%rsp)
|
|---|
| 224 | movq %r12, ISTATE_OFFSET_R12(%rsp)
|
|---|
| 225 | movq %r13, ISTATE_OFFSET_R13(%rsp)
|
|---|
| 226 | movq %r14, ISTATE_OFFSET_R14(%rsp)
|
|---|
| 227 | movq %r15, ISTATE_OFFSET_R15(%rsp)
|
|---|
| 228 |
|
|---|
| 229 | /*
|
|---|
| 230 | * Imitate a regular stack frame linkage.
|
|---|
| 231 | * Stop stack traces here if we came from userspace.
|
|---|
| 232 | */
|
|---|
| 233 | xorl %edx, %edx
|
|---|
| 234 | cmpq $(GDT_SELECTOR(KTEXT_DES)), ISTATE_OFFSET_CS(%rsp)
|
|---|
| 235 | cmovnzq %rdx, %rbp
|
|---|
| 236 |
|
|---|
| 237 | movq %rbp, ISTATE_OFFSET_RBP_FRAME(%rsp)
|
|---|
| 238 | movq ISTATE_OFFSET_RIP(%rsp), %rax
|
|---|
| 239 | movq %rax, ISTATE_OFFSET_RIP_FRAME(%rsp)
|
|---|
| 240 | leaq ISTATE_OFFSET_RBP_FRAME(%rsp), %rbp
|
|---|
| 241 |
|
|---|
| 242 | movq $(\i), %rdi /* pass intnum in the first argument */
|
|---|
| 243 | movq %rsp, %rsi /* pass istate address in the second argument */
|
|---|
| 244 |
|
|---|
| 245 | cld
|
|---|
| 246 |
|
|---|
| 247 | /* Call exc_dispatch(i, istate) */
|
|---|
| 248 | call exc_dispatch
|
|---|
| 249 |
|
|---|
| 250 | /*
|
|---|
| 251 | * Restore all scratch registers and the preserved registers we have
|
|---|
| 252 | * clobbered in this handler (i.e. RBP).
|
|---|
| 253 | */
|
|---|
| 254 | movq ISTATE_OFFSET_RAX(%rsp), %rax
|
|---|
| 255 | movq ISTATE_OFFSET_RCX(%rsp), %rcx
|
|---|
| 256 | movq ISTATE_OFFSET_RDX(%rsp), %rdx
|
|---|
| 257 | movq ISTATE_OFFSET_RSI(%rsp), %rsi
|
|---|
| 258 | movq ISTATE_OFFSET_RDI(%rsp), %rdi
|
|---|
| 259 | movq ISTATE_OFFSET_RBP(%rsp), %rbp
|
|---|
| 260 | movq ISTATE_OFFSET_R8(%rsp), %r8
|
|---|
| 261 | movq ISTATE_OFFSET_R9(%rsp), %r9
|
|---|
| 262 | movq ISTATE_OFFSET_R10(%rsp), %r10
|
|---|
| 263 | movq ISTATE_OFFSET_R11(%rsp), %r11
|
|---|
| 264 |
|
|---|
| 265 | /* $8 = Skip error word */
|
|---|
| 266 | addq $(ISTATE_SOFT_SIZE + 8), %rsp
|
|---|
| 267 | iretq
|
|---|
| 268 | .endm
|
|---|
| 269 |
|
|---|
| 270 | #define LIST_0_63 \
|
|---|
| 271 | 0, 1, 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,\
|
|---|
| 272 | 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,\
|
|---|
| 273 | 53,54,55,56,57,58,59,60,61,62,63
|
|---|
| 274 |
|
|---|
| 275 | interrupt_handlers:
|
|---|
| 276 | .irp cnt, LIST_0_63
|
|---|
| 277 | handler \cnt
|
|---|
| 278 | .endr
|
|---|
| 279 |
|
|---|
| 280 | /** Low-level syscall handler
|
|---|
| 281 | *
|
|---|
| 282 | * Registers on entry:
|
|---|
| 283 | *
|
|---|
| 284 | * @param %rcx Userspace return address.
|
|---|
| 285 | * @param %r11 Userspace RLFAGS.
|
|---|
| 286 | *
|
|---|
| 287 | * @param %rax Syscall number.
|
|---|
| 288 | * @param %rdi 1st syscall argument.
|
|---|
| 289 | * @param %rsi 2nd syscall argument.
|
|---|
| 290 | * @param %rdx 3rd syscall argument.
|
|---|
| 291 | * @param %r10 4th syscall argument. Used instead of RCX because
|
|---|
| 292 | * the SYSCALL instruction clobbers it.
|
|---|
| 293 | * @param %r8 5th syscall argument.
|
|---|
| 294 | * @param %r9 6th syscall argument.
|
|---|
| 295 | *
|
|---|
| 296 | * @return Return value is in %rax.
|
|---|
| 297 | *
|
|---|
| 298 | */
|
|---|
| 299 | syscall_entry:
|
|---|
| 300 | /* Switch to hidden %gs */
|
|---|
| 301 | swapgs
|
|---|
| 302 |
|
|---|
| 303 | /*
|
|---|
| 304 | * %gs:0 Scratch space for this thread's user RSP
|
|---|
| 305 | * %gs:8 Address to be used as this thread's kernel RSP
|
|---|
| 306 | */
|
|---|
| 307 |
|
|---|
| 308 | movq %rsp, %gs:0 /* save this thread's user RSP */
|
|---|
| 309 | movq %gs:8, %rsp /* set this thread's kernel RSP */
|
|---|
| 310 |
|
|---|
| 311 | /*
|
|---|
| 312 | * Note that the space needed for the imitated istate structure has been
|
|---|
| 313 | * preallocated for us in thread_create_arch() and set in
|
|---|
| 314 | * before_thread_runs_arch().
|
|---|
| 315 | */
|
|---|
| 316 |
|
|---|
| 317 | /*
|
|---|
| 318 | * Save the general purpose registers and push the 7th argument (syscall
|
|---|
| 319 | * number) onto the stack. Note that the istate structure has a layout
|
|---|
| 320 | * which supports this.
|
|---|
| 321 | */
|
|---|
| 322 | movq %rax, ISTATE_OFFSET_RAX(%rsp) /* 7th argument, passed on stack */
|
|---|
| 323 | movq %rbx, ISTATE_OFFSET_RBX(%rsp) /* observability */
|
|---|
| 324 | movq %rcx, ISTATE_OFFSET_RCX(%rsp) /* userspace RIP */
|
|---|
| 325 | movq %rdx, ISTATE_OFFSET_RDX(%rsp) /* 3rd argument, observability */
|
|---|
| 326 | movq %rsi, ISTATE_OFFSET_RSI(%rsp) /* 2nd argument, observability */
|
|---|
| 327 | movq %rdi, ISTATE_OFFSET_RDI(%rsp) /* 1st argument, observability */
|
|---|
| 328 | movq %rbp, ISTATE_OFFSET_RBP(%rsp) /* need to preserve userspace RBP */
|
|---|
| 329 | movq %r8, ISTATE_OFFSET_R8(%rsp) /* 5th argument, observability */
|
|---|
| 330 | movq %r9, ISTATE_OFFSET_R9(%rsp) /* 6th argument, observability */
|
|---|
| 331 | movq %r10, ISTATE_OFFSET_R10(%rsp) /* 4th argument, observability */
|
|---|
| 332 | movq %r11, ISTATE_OFFSET_R11(%rsp) /* low 32 bits userspace RFLAGS */
|
|---|
| 333 | movq %r12, ISTATE_OFFSET_R12(%rsp) /* observability */
|
|---|
| 334 | movq %r13, ISTATE_OFFSET_R13(%rsp) /* observability */
|
|---|
| 335 | movq %r14, ISTATE_OFFSET_R14(%rsp) /* observability */
|
|---|
| 336 | movq %r15, ISTATE_OFFSET_R15(%rsp) /* observability */
|
|---|
| 337 |
|
|---|
| 338 | /*
|
|---|
| 339 | * Save the return address and the userspace stack on locations that
|
|---|
| 340 | * would normally be taken by them.
|
|---|
| 341 | */
|
|---|
| 342 | movq %gs:0, %rax
|
|---|
| 343 | movq %rax, ISTATE_OFFSET_RSP(%rsp)
|
|---|
| 344 | movq %rcx, ISTATE_OFFSET_RIP(%rsp)
|
|---|
| 345 |
|
|---|
| 346 | /*
|
|---|
| 347 | * Imitate a regular stack frame linkage.
|
|---|
| 348 | */
|
|---|
| 349 | movq $0, ISTATE_OFFSET_RBP_FRAME(%rsp)
|
|---|
| 350 | movq %rcx, ISTATE_OFFSET_RIP_FRAME(%rsp)
|
|---|
| 351 | leaq ISTATE_OFFSET_RBP_FRAME(%rsp), %rbp
|
|---|
| 352 |
|
|---|
| 353 | /* Switch back to normal %gs */
|
|---|
| 354 | swapgs
|
|---|
| 355 | sti
|
|---|
| 356 |
|
|---|
| 357 | /* Copy the 4th argument where it is expected */
|
|---|
| 358 | movq %r10, %rcx
|
|---|
| 359 |
|
|---|
| 360 | /*
|
|---|
| 361 | * Call syscall_handler() with the 7th argument passed on stack.
|
|---|
| 362 | */
|
|---|
| 363 | call syscall_handler
|
|---|
| 364 |
|
|---|
| 365 | /*
|
|---|
| 366 | * Test if the saved return address is canonical and not-kernel.
|
|---|
| 367 | * We do this by looking at the 16 most significant bits
|
|---|
| 368 | * of the saved return address (two bytes at offset 6).
|
|---|
| 369 | */
|
|---|
| 370 | testw $0xffff, ISTATE_OFFSET_RIP+6(%rsp)
|
|---|
| 371 | jnz bad_rip
|
|---|
| 372 |
|
|---|
| 373 | cli
|
|---|
| 374 |
|
|---|
| 375 | /*
|
|---|
| 376 | * Restore registers needed for return via the SYSRET instruction and
|
|---|
| 377 | * the clobbered preserved registers (i.e. RBP).
|
|---|
| 378 | */
|
|---|
| 379 | movq ISTATE_OFFSET_RBP(%rsp), %rbp
|
|---|
| 380 | movq ISTATE_OFFSET_RCX(%rsp), %rcx
|
|---|
| 381 | movq ISTATE_OFFSET_R11(%rsp), %r11
|
|---|
| 382 | movq ISTATE_OFFSET_RSP(%rsp), %rsp
|
|---|
| 383 |
|
|---|
| 384 | /*
|
|---|
| 385 | * Clear the rest of the scratch registers to prevent information leak.
|
|---|
| 386 | * The 32-bit XOR on the low GPRs actually clears the entire 64-bit
|
|---|
| 387 | * register and the instruction is shorter.
|
|---|
| 388 | */
|
|---|
| 389 | xorl %edx, %edx
|
|---|
| 390 | xorl %esi, %esi
|
|---|
| 391 | xorl %edi, %edi
|
|---|
| 392 | xorq %r8, %r8
|
|---|
| 393 | xorq %r9, %r9
|
|---|
| 394 | xorq %r10, %r10
|
|---|
| 395 |
|
|---|
| 396 | sysretq
|
|---|
| 397 |
|
|---|
| 398 | bad_rip:
|
|---|
| 399 | movq %rsp, %rdi
|
|---|
| 400 | movabs $bad_rip_msg, %rsi
|
|---|
| 401 | xorb %al, %al
|
|---|
| 402 | callq fault_from_uspace
|
|---|
| 403 | /* not reached */
|
|---|
| 404 |
|
|---|
| 405 | bad_rip_msg:
|
|---|
| 406 | .asciz "Invalid instruction pointer."
|
|---|
| 407 |
|
|---|
| 408 | /** Print Unicode character to EGA display.
|
|---|
| 409 | *
|
|---|
| 410 | * If CONFIG_EGA is undefined or CONFIG_FB is defined
|
|---|
| 411 | * then this function does nothing.
|
|---|
| 412 | *
|
|---|
| 413 | * Since the EGA can only display Extended ASCII (usually
|
|---|
| 414 | * ISO Latin 1) characters, some of the Unicode characters
|
|---|
| 415 | * can be displayed in a wrong way. Only newline and backspace
|
|---|
| 416 | * are interpreted, all other characters (even unprintable) are
|
|---|
| 417 | * printed verbatim.
|
|---|
| 418 | *
|
|---|
| 419 | * @param %rdi Unicode character to be printed.
|
|---|
| 420 | *
|
|---|
| 421 | */
|
|---|
| 422 | early_putchar:
|
|---|
| 423 |
|
|---|
| 424 | #if ((defined(CONFIG_EGA)) && (!defined(CONFIG_FB)))
|
|---|
| 425 |
|
|---|
| 426 | /* Prologue, save preserved registers */
|
|---|
| 427 | pushq %rbp
|
|---|
| 428 | movq %rsp, %rbp
|
|---|
| 429 | pushq %rbx
|
|---|
| 430 |
|
|---|
| 431 | movq %rdi, %rsi
|
|---|
| 432 | movq $(PA2KA(0xb8000)), %rdi /* base of EGA text mode memory */
|
|---|
| 433 | xorl %eax, %eax
|
|---|
| 434 |
|
|---|
| 435 | /* Read bits 8 - 15 of the cursor address */
|
|---|
| 436 | movw $0x3d4, %dx
|
|---|
| 437 | movb $0xe, %al
|
|---|
| 438 | outb %al, %dx
|
|---|
| 439 |
|
|---|
| 440 | movw $0x3d5, %dx
|
|---|
| 441 | inb %dx, %al
|
|---|
| 442 | shl $8, %ax
|
|---|
| 443 |
|
|---|
| 444 | /* Read bits 0 - 7 of the cursor address */
|
|---|
| 445 | movw $0x3d4, %dx
|
|---|
| 446 | movb $0xf, %al
|
|---|
| 447 | outb %al, %dx
|
|---|
| 448 |
|
|---|
| 449 | movw $0x3d5, %dx
|
|---|
| 450 | inb %dx, %al
|
|---|
| 451 |
|
|---|
| 452 | /* Sanity check for the cursor on screen */
|
|---|
| 453 | cmp $2000, %ax
|
|---|
| 454 | jb early_putchar_cursor_ok
|
|---|
| 455 |
|
|---|
| 456 | movw $1998, %ax
|
|---|
| 457 |
|
|---|
| 458 | early_putchar_cursor_ok:
|
|---|
| 459 |
|
|---|
| 460 | movw %ax, %bx
|
|---|
| 461 | shl $1, %rax
|
|---|
| 462 | addq %rax, %rdi
|
|---|
| 463 |
|
|---|
| 464 | movq %rsi, %rax
|
|---|
| 465 |
|
|---|
| 466 | cmp $0x0a, %al
|
|---|
| 467 | jne early_putchar_backspace
|
|---|
| 468 |
|
|---|
| 469 | /* Interpret newline */
|
|---|
| 470 |
|
|---|
| 471 | movw %bx, %ax /* %bx -> %dx:%ax */
|
|---|
| 472 | xorw %dx, %dx
|
|---|
| 473 |
|
|---|
| 474 | movw $80, %cx
|
|---|
| 475 | idivw %cx, %ax /* %dx = %bx % 80 */
|
|---|
| 476 |
|
|---|
| 477 | /* %bx <- %bx + 80 - (%bx % 80) */
|
|---|
| 478 | addw %cx, %bx
|
|---|
| 479 | subw %dx, %bx
|
|---|
| 480 |
|
|---|
| 481 | jmp early_putchar_skip
|
|---|
| 482 |
|
|---|
| 483 | early_putchar_backspace:
|
|---|
| 484 |
|
|---|
| 485 | cmp $0x08, %al
|
|---|
| 486 | jne early_putchar_print
|
|---|
| 487 |
|
|---|
| 488 | /* Interpret backspace */
|
|---|
| 489 |
|
|---|
| 490 | cmp $0x0000, %bx
|
|---|
| 491 | je early_putchar_skip
|
|---|
| 492 |
|
|---|
| 493 | dec %bx
|
|---|
| 494 | jmp early_putchar_skip
|
|---|
| 495 |
|
|---|
| 496 | early_putchar_print:
|
|---|
| 497 |
|
|---|
| 498 | /* Print character */
|
|---|
| 499 |
|
|---|
| 500 | movb $0x0e, %ah /* black background, yellow foreground */
|
|---|
| 501 | stosw
|
|---|
| 502 | inc %bx
|
|---|
| 503 |
|
|---|
| 504 | early_putchar_skip:
|
|---|
| 505 |
|
|---|
| 506 | /* Sanity check for the cursor on the last line */
|
|---|
| 507 | cmp $2000, %bx
|
|---|
| 508 | jb early_putchar_no_scroll
|
|---|
| 509 |
|
|---|
| 510 | /* Scroll the screen (24 rows) */
|
|---|
| 511 | movq $(PA2KA(0xb80a0)), %rsi
|
|---|
| 512 | movq $(PA2KA(0xb8000)), %rdi
|
|---|
| 513 | movl $480, %ecx
|
|---|
| 514 | rep movsq
|
|---|
| 515 |
|
|---|
| 516 | /* Clear the 24th row */
|
|---|
| 517 | xorl %eax, %eax
|
|---|
| 518 | movl $20, %ecx
|
|---|
| 519 | rep stosq
|
|---|
| 520 |
|
|---|
| 521 | /* Go to row 24 */
|
|---|
| 522 | movw $1920, %bx
|
|---|
| 523 |
|
|---|
| 524 | early_putchar_no_scroll:
|
|---|
| 525 |
|
|---|
| 526 | /* Write bits 8 - 15 of the cursor address */
|
|---|
| 527 | movw $0x3d4, %dx
|
|---|
| 528 | movb $0xe, %al
|
|---|
| 529 | outb %al, %dx
|
|---|
| 530 |
|
|---|
| 531 | movw $0x3d5, %dx
|
|---|
| 532 | movb %bh, %al
|
|---|
| 533 | outb %al, %dx
|
|---|
| 534 |
|
|---|
| 535 | /* Write bits 0 - 7 of the cursor address */
|
|---|
| 536 | movw $0x3d4, %dx
|
|---|
| 537 | movb $0xf, %al
|
|---|
| 538 | outb %al, %dx
|
|---|
| 539 |
|
|---|
| 540 | movw $0x3d5, %dx
|
|---|
| 541 | movb %bl, %al
|
|---|
| 542 | outb %al, %dx
|
|---|
| 543 |
|
|---|
| 544 | /* Epilogue, restore preserved registers */
|
|---|
| 545 | popq %rbx
|
|---|
| 546 | leave
|
|---|
| 547 |
|
|---|
| 548 | #endif
|
|---|
| 549 |
|
|---|
| 550 | ret
|
|---|
| 551 |
|
|---|