source: mainline/kernel/arch/amd64/src/asm.S@ c7fbb90

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c7fbb90 was cb4f078, checked in by Martin Decky <martin@…>, 14 years ago

unify kernel byte string implementations

  • Property mode set to 100644
File size: 12.7 KB
Line 
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 */
65memcpy_from_uspace:
66memcpy_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
83memcpy_from_uspace_failover_address:
84memcpy_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*/
93has_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
116cpuid:
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
132set_efer_flag:
133 movl $0xc0000080, %ecx
134 rdmsr
135 btsl %edi, %eax
136 wrmsr
137 ret
138
139read_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
184int_\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
275interrupt_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 */
299syscall_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 cli
366
367 /*
368 * Restore registers needed for return via the SYSRET instruction and
369 * the clobbered preserved registers (i.e. RBP).
370 */
371 movq ISTATE_OFFSET_RBP(%rsp), %rbp
372 movq ISTATE_OFFSET_RCX(%rsp), %rcx
373 movq ISTATE_OFFSET_R11(%rsp), %r11
374 movq ISTATE_OFFSET_RSP(%rsp), %rsp
375
376 /*
377 * Clear the rest of the scratch registers to prevent information leak.
378 * The 32-bit XOR on the low GPRs actually clears the entire 64-bit
379 * register and the instruction is shorter.
380 */
381 xorl %edx, %edx
382 xorl %esi, %esi
383 xorl %edi, %edi
384 xorq %r8, %r8
385 xorq %r9, %r9
386 xorq %r10, %r10
387
388 sysretq
389
390/** Print Unicode character to EGA display.
391 *
392 * If CONFIG_EGA is undefined or CONFIG_FB is defined
393 * then this function does nothing.
394 *
395 * Since the EGA can only display Extended ASCII (usually
396 * ISO Latin 1) characters, some of the Unicode characters
397 * can be displayed in a wrong way. Only newline and backspace
398 * are interpreted, all other characters (even unprintable) are
399 * printed verbatim.
400 *
401 * @param %rdi Unicode character to be printed.
402 *
403 */
404early_putchar:
405
406#if ((defined(CONFIG_EGA)) && (!defined(CONFIG_FB)))
407
408 /* Prologue, save preserved registers */
409 pushq %rbp
410 movq %rsp, %rbp
411 pushq %rbx
412
413 movq %rdi, %rsi
414 movq $(PA2KA(0xb8000)), %rdi /* base of EGA text mode memory */
415 xorl %eax, %eax
416
417 /* Read bits 8 - 15 of the cursor address */
418 movw $0x3d4, %dx
419 movb $0xe, %al
420 outb %al, %dx
421
422 movw $0x3d5, %dx
423 inb %dx, %al
424 shl $8, %ax
425
426 /* Read bits 0 - 7 of the cursor address */
427 movw $0x3d4, %dx
428 movb $0xf, %al
429 outb %al, %dx
430
431 movw $0x3d5, %dx
432 inb %dx, %al
433
434 /* Sanity check for the cursor on screen */
435 cmp $2000, %ax
436 jb early_putchar_cursor_ok
437
438 movw $1998, %ax
439
440 early_putchar_cursor_ok:
441
442 movw %ax, %bx
443 shl $1, %rax
444 addq %rax, %rdi
445
446 movq %rsi, %rax
447
448 cmp $0x0a, %al
449 jne early_putchar_backspace
450
451 /* Interpret newline */
452
453 movw %bx, %ax /* %bx -> %dx:%ax */
454 xorw %dx, %dx
455
456 movw $80, %cx
457 idivw %cx, %ax /* %dx = %bx % 80 */
458
459 /* %bx <- %bx + 80 - (%bx % 80) */
460 addw %cx, %bx
461 subw %dx, %bx
462
463 jmp early_putchar_skip
464
465 early_putchar_backspace:
466
467 cmp $0x08, %al
468 jne early_putchar_print
469
470 /* Interpret backspace */
471
472 cmp $0x0000, %bx
473 je early_putchar_skip
474
475 dec %bx
476 jmp early_putchar_skip
477
478 early_putchar_print:
479
480 /* Print character */
481
482 movb $0x0e, %ah /* black background, yellow foreground */
483 stosw
484 inc %bx
485
486 early_putchar_skip:
487
488 /* Sanity check for the cursor on the last line */
489 cmp $2000, %bx
490 jb early_putchar_no_scroll
491
492 /* Scroll the screen (24 rows) */
493 movq $(PA2KA(0xb80a0)), %rsi
494 movq $(PA2KA(0xb8000)), %rdi
495 movl $480, %ecx
496 rep movsq
497
498 /* Clear the 24th row */
499 xorl %eax, %eax
500 movl $20, %ecx
501 rep stosq
502
503 /* Go to row 24 */
504 movw $1920, %bx
505
506 early_putchar_no_scroll:
507
508 /* Write bits 8 - 15 of the cursor address */
509 movw $0x3d4, %dx
510 movb $0xe, %al
511 outb %al, %dx
512
513 movw $0x3d5, %dx
514 movb %bh, %al
515 outb %al, %dx
516
517 /* Write bits 0 - 7 of the cursor address */
518 movw $0x3d4, %dx
519 movb $0xf, %al
520 outb %al, %dx
521
522 movw $0x3d5, %dx
523 movb %bl, %al
524 outb %al, %dx
525
526 /* Epilogue, restore preserved registers */
527 popq %rbx
528 leave
529
530#endif
531
532 ret
533
Note: See TracBrowser for help on using the repository browser.