source: mainline/kernel/arch/ia32/src/asm.S@ 4e91239

lfn serial ticket/834-toolchain-update topic/fix-logger-deadlock topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e91239 was 4e91239, checked in by Jakub Jermar <jakub@…>, 16 years ago

Move comment to a better location.

  • Property mode set to 100644
File size: 13.7 KB
Line 
1/*
2 * Copyright (c) 2001 Jakub Jermar
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 *
31 */
32
33#include <arch/pm.h>
34#include <arch/mm/page.h>
35
36.text
37.global paging_on
38.global enable_l_apic_in_msr
39.global memsetb
40.global memsetw
41.global memcpy
42.global memcpy_from_uspace
43.global memcpy_from_uspace_failover_address
44.global memcpy_to_uspace
45.global memcpy_to_uspace_failover_address
46.global early_putchar
47
48/* Wrapper for generic memsetb */
49memsetb:
50 jmp _memsetb
51
52/* Wrapper for generic memsetw */
53memsetw:
54 jmp _memsetw
55
56#define MEMCPY_DST 4
57#define MEMCPY_SRC 8
58#define MEMCPY_SIZE 12
59
60/** Copy memory to/from userspace.
61 *
62 * This is almost conventional memcpy().
63 * The difference is that there is a failover part
64 * to where control is returned from a page fault
65 * if the page fault occurs during copy_from_uspace()
66 * or copy_to_uspace().
67 *
68 * @param MEMCPY_DST(%esp) Destination address.
69 * @param MEMCPY_SRC(%esp) Source address.
70 * @param MEMCPY_SIZE(%esp) Size.
71 *
72 * @return MEMCPY_DST(%esp) on success and 0 on failure.
73 *
74 */
75memcpy:
76memcpy_from_uspace:
77memcpy_to_uspace:
78 movl %edi, %edx /* save %edi */
79 movl %esi, %eax /* save %esi */
80
81 movl MEMCPY_SIZE(%esp), %ecx
82 shrl $2, %ecx /* size / 4 */
83
84 movl MEMCPY_DST(%esp), %edi
85 movl MEMCPY_SRC(%esp), %esi
86
87 /* Copy whole words */
88 rep movsl
89
90 movl MEMCPY_SIZE(%esp), %ecx
91 andl $3, %ecx /* size % 4 */
92 jz 0f
93
94 /* Copy the rest byte by byte */
95 rep movsb
96
97 0:
98
99 movl %edx, %edi
100 movl %eax, %esi
101
102 /* MEMCPY_DST(%esp), success */
103 movl MEMCPY_DST(%esp), %eax
104 ret
105
106/*
107 * We got here from as_page_fault() after the memory operations
108 * above had caused a page fault.
109 */
110memcpy_from_uspace_failover_address:
111memcpy_to_uspace_failover_address:
112 movl %edx, %edi
113 movl %eax, %esi
114
115 /* Return 0, failure */
116 xorl %eax, %eax
117 ret
118
119/** Turn paging on
120 *
121 * Enable paging and write-back caching in CR0.
122 *
123 */
124paging_on:
125 movl %cr0, %edx
126 orl $(1 << 31), %edx /* paging on */
127
128 /* Clear Cache Disable and not Write Though */
129 andl $~((1 << 30) | (1 << 29)), %edx
130 movl %edx, %cr0
131 jmp 0f
132
133 0:
134 ret
135
136/** Enable local APIC
137 *
138 * Enable local APIC in MSR.
139 *
140 */
141enable_l_apic_in_msr:
142 movl $0x1b, %ecx
143 rdmsr
144 orl $(1 << 11), %eax
145 orl $(0xfee00000), %eax
146 wrmsr
147 ret
148
149/** Clear nested flag
150 *
151 */
152.macro CLEAR_NT_FLAG
153 pushfl
154 andl $0xffffbfff, (%esp)
155 popfl
156.endm
157
158#define ISTATE_OFFSET_EDX 0
159#define ISTATE_OFFSET_ECX 4
160#define ISTATE_OFFSET_EBX 8
161#define ISTATE_OFFSET_ESI 12
162#define ISTATE_OFFSET_EDI 16
163#define ISTATE_OFFSET_EBP 20
164#define ISTATE_OFFSET_EAX 24
165#define ISTATE_OFFSET_EBP_FRAME 28
166#define ISTATE_OFFSET_EIP_FRAME 32
167#define ISTATE_OFFSET_GS 36
168#define ISTATE_OFFSET_FS 40
169#define ISTATE_OFFSET_ES 44
170#define ISTATE_OFFSET_DS 48
171#define ISTATE_OFFSET_ERROR_WORD 52
172#define ISTATE_OFFSET_EIP 56
173#define ISTATE_OFFSET_CS 60
174#define ISTATE_OFFSET_EFLAGS 64
175#define ISTATE_OFFSET_ESP 68
176#define ISTATE_OFFSET_SS 72
177
178/*
179 * Size of the istate structure without the hardware-saved part
180 * and without the error word.
181 */
182#define ISTATE_SOFT_SIZE 52
183
184/*
185 * Size of the entire istate structure including the error word and the
186 * hardware-saved part.
187 */
188#define ISTATE_REAL_SIZE (ISTATE_SOFT_SIZE + 24)
189
190/*
191 * The SYSENTER syscall mechanism can be used for syscalls with
192 * four or fewer arguments. To pass these four arguments, we
193 * use four registers: EDX, ECX, EBX, ESI. The syscall number
194 * is passed in EAX. We use EDI to remember the return address
195 * and EBP to remember the stack. The INT-based syscall mechanism
196 * can actually handle six arguments plus the syscall number
197 * entirely in registers.
198 */
199.global sysenter_handler
200sysenter_handler:
201 sti
202 subl $(ISTATE_REAL_SIZE), %esp
203
204 /*
205 * Save the return address and the userspace stack in the istate
206 * structure on locations that would normally be taken by them.
207 */
208 movl %ebp, ISTATE_OFFSET_ESP(%esp)
209 movl %edi, ISTATE_OFFSET_EIP(%esp)
210
211 /*
212 * Push syscall arguments onto the stack
213 */
214 movl %eax, ISTATE_OFFSET_EAX(%esp)
215 movl %ebx, ISTATE_OFFSET_EBX(%esp)
216 movl %ecx, ISTATE_OFFSET_ECX(%esp)
217 movl %edx, ISTATE_OFFSET_EDX(%esp)
218 movl %esi, ISTATE_OFFSET_ESI(%esp)
219 movl %edi, ISTATE_OFFSET_EDI(%esp) /* observability; not needed */
220 movl %ebp, ISTATE_OFFSET_EBP(%esp) /* observability; not needed */
221
222 /*
223 * Fake up the stack trace linkage.
224 */
225 movl %edi, ISTATE_OFFSET_EIP_FRAME(%esp)
226 movl $0, ISTATE_OFFSET_EBP_FRAME(%esp)
227 leal ISTATE_OFFSET_EBP_FRAME(%esp), %ebp
228
229 /*
230 * Save TLS.
231 */
232 movl %gs, %edx
233 movl %edx, ISTATE_OFFSET_GS(%esp)
234
235 /*
236 * Switch to kernel selectors.
237 */
238 movw $16, %ax
239 movw %ax, %ds
240 movw %ax, %es
241
242 cld
243 call syscall_handler
244
245 /*
246 * Restore TLS.
247 */
248 movl ISTATE_OFFSET_GS(%esp), %edx
249 movl %edx, %gs
250
251 /*
252 * Prepare return address and userspace stack for SYSEXIT.
253 */
254 movl ISTATE_OFFSET_EIP(%esp), %edx
255 movl ISTATE_OFFSET_ESP(%esp), %ecx
256
257 addl $(ISTATE_REAL_SIZE), %esp
258
259 sysexit /* return to userspace */
260
261/*
262 * This is the legacy syscall handler using the interrupt mechanism.
263 */
264.global int_syscall
265int_syscall:
266 subl $(ISTATE_SOFT_SIZE + 4), %esp
267
268 /*
269 * Push syscall arguments onto the stack
270 *
271 * NOTE: The idea behind the order of arguments passed
272 * in registers is to use all scratch registers
273 * first and preserved registers next. An optimized
274 * libc syscall wrapper can make use of this setup.
275 * The istate structure is arranged in the way to support
276 * this idea.
277 *
278 */
279 movl %eax, ISTATE_OFFSET_EAX(%esp)
280 movl %ebx, ISTATE_OFFSET_EBX(%esp)
281 movl %ecx, ISTATE_OFFSET_ECX(%esp)
282 movl %edx, ISTATE_OFFSET_EDX(%esp)
283 movl %edi, ISTATE_OFFSET_EDI(%esp)
284 movl %esi, ISTATE_OFFSET_ESI(%esp)
285 movl %ebp, ISTATE_OFFSET_EBP(%esp)
286
287 /*
288 * Save the selector registers.
289 */
290 movl %gs, %ecx
291 movl %fs, %edx
292
293 movl %ecx, ISTATE_OFFSET_GS(%esp)
294 movl %edx, ISTATE_OFFSET_FS(%esp)
295
296 movl %es, %ecx
297 movl %ds, %edx
298
299 movl %ecx, ISTATE_OFFSET_ES(%esp)
300 movl %edx, ISTATE_OFFSET_DS(%esp)
301
302 /*
303 * Switch to kernel selectors.
304 */
305 movl $16, %eax
306 movl %eax, %ds
307 movl %eax, %es
308
309 movl $0, ISTATE_OFFSET_EBP_FRAME(%esp)
310 movl ISTATE_OFFSET_EIP(%esp), %eax
311 movl %eax, ISTATE_OFFSET_EIP_FRAME(%esp)
312 leal ISTATE_OFFSET_EBP_FRAME(%esp), %ebp
313
314 cld
315 sti
316
317 /* Call syscall_handler(edx, ecx, ebx, esi, edi, ebp, eax) */
318 call syscall_handler
319
320 CLEAR_NT_FLAG
321
322 /*
323 * Restore the selector registers.
324 */
325 movl ISTATE_OFFSET_GS(%esp), %ecx
326 movl ISTATE_OFFSET_FS(%esp), %edx
327
328 movl %ecx, %gs
329 movl %edx, %fs
330
331 movl ISTATE_OFFSET_ES(%esp), %ecx
332 movl ISTATE_OFFSET_DS(%esp), %edx
333
334 movl %ecx, %es
335 movl %edx, %ds
336
337 /*
338 * Restore the preserved registers the handler cloberred itself
339 * (i.e. EBP).
340 */
341 movl ISTATE_OFFSET_EBP(%esp), %ebp
342
343 addl $(ISTATE_SOFT_SIZE + 4), %esp
344 iret
345
346/**
347 * Mask for interrupts 0 - 31 (bits 0 - 31) where 0 means that int
348 * has no error word and 1 means interrupt with error word
349 *
350 */
351#define ERROR_WORD_INTERRUPT_LIST 0x00027d00
352
353/** Declare interrupt handlers
354 *
355 * Declare interrupt handlers for n interrupt
356 * vectors starting at vector i.
357 *
358 */
359.macro handler i
360.global int_\i
361int_\i:
362 /*
363 * This macro distinguishes between two versions of ia32
364 * exceptions. One version has error word and the other
365 * does not have it. The latter version fakes the error
366 * word on the stack so that the handlers and istate_t
367 * can be the same for both types.
368 */
369 .iflt \i - 32
370 .if (1 << \i) & ERROR_WORD_INTERRUPT_LIST
371 /*
372 * Exception with error word: do nothing
373 */
374 .else
375 /*
376 * Exception without error word: fake up one
377 */
378 pushl $0
379 .endif
380 .else
381 /*
382 * Interrupt: fake up one
383 */
384 pushl $0
385 .endif
386
387 subl $ISTATE_SOFT_SIZE, %esp
388
389 /*
390 * Save the general purpose registers.
391 */
392 movl %eax, ISTATE_OFFSET_EAX(%esp)
393 movl %ebx, ISTATE_OFFSET_EBX(%esp)
394 movl %ecx, ISTATE_OFFSET_ECX(%esp)
395 movl %edx, ISTATE_OFFSET_EDX(%esp)
396 movl %edi, ISTATE_OFFSET_EDI(%esp)
397 movl %esi, ISTATE_OFFSET_ESI(%esp)
398 movl %ebp, ISTATE_OFFSET_EBP(%esp)
399
400 /*
401 * Save the selector registers.
402 */
403 movl %gs, %eax
404 movl %fs, %ebx
405 movl %es, %ecx
406 movl %ds, %edx
407
408 movl %eax, ISTATE_OFFSET_GS(%esp)
409 movl %ebx, ISTATE_OFFSET_FS(%esp)
410 movl %ecx, ISTATE_OFFSET_ES(%esp)
411 movl %edx, ISTATE_OFFSET_DS(%esp)
412
413 /*
414 * Switch to kernel selectors.
415 */
416 movl $16, %eax
417 movl %eax, %ds
418 movl %eax, %es
419
420 /*
421 * Imitate a regular stack frame linkage.
422 * Stop stack traces here if we came from userspace.
423 */
424 cmpl $8, ISTATE_OFFSET_CS(%esp)
425 jz 0f
426 xorl %ebp, %ebp
427
428 0:
429
430 movl %ebp, ISTATE_OFFSET_EBP_FRAME(%esp)
431 movl ISTATE_OFFSET_EIP(%esp), %eax
432 movl %eax, ISTATE_OFFSET_EIP_FRAME(%esp)
433 leal ISTATE_OFFSET_EBP_FRAME(%esp), %ebp
434
435 cld
436
437 pushl %esp /* pass istate address */
438 pushl $(\i) /* pass intnum */
439
440 /* Call exc_dispatch(intnum, istate) */
441 call exc_dispatch
442
443 addl $8, %esp /* clear arguments from the stack */
444
445 CLEAR_NT_FLAG
446
447 /*
448 * Restore the selector registers.
449 */
450 movl ISTATE_OFFSET_GS(%esp), %eax
451 movl ISTATE_OFFSET_FS(%esp), %ebx
452 movl ISTATE_OFFSET_ES(%esp), %ecx
453 movl ISTATE_OFFSET_DS(%esp), %edx
454
455 movl %eax, %gs
456 movl %ebx, %fs
457 movl %ecx, %es
458 movl %edx, %ds
459
460 /*
461 * Restore the scratch registers and the preserved
462 * registers the handler cloberred itself
463 * (i.e. EBX and EBP).
464 */
465 movl ISTATE_OFFSET_EAX(%esp), %eax
466 movl ISTATE_OFFSET_EBX(%esp), %ebx
467 movl ISTATE_OFFSET_ECX(%esp), %ecx
468 movl ISTATE_OFFSET_EDX(%esp), %edx
469 movl ISTATE_OFFSET_EBP(%esp), %ebp
470
471 addl $(ISTATE_SOFT_SIZE + 4), %esp
472 iret
473.endm
474
475#define LIST_0_63 \
476 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,\
477 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,\
478 53,54,55,56,57,58,59,60,61,62,63
479
480interrupt_handlers:
481.irp cnt, LIST_0_63
482 handler \cnt
483.endr
484
485/** Print Unicode character to EGA display.
486 *
487 * If CONFIG_EGA is undefined or CONFIG_FB is defined
488 * then this function does nothing.
489 *
490 * Since the EGA can only display Extended ASCII (usually
491 * ISO Latin 1) characters, some of the Unicode characters
492 * can be displayed in a wrong way. Only newline and backspace
493 * are interpreted, all other characters (even unprintable) are
494 * printed verbatim.
495 *
496 * @param %ebp+0x08 Unicode character to be printed.
497 *
498 */
499early_putchar:
500
501#if ((defined(CONFIG_EGA)) && (!defined(CONFIG_FB)))
502
503 /* Prologue, save preserved registers */
504 pushl %ebp
505 movl %esp, %ebp
506 pushl %ebx
507 pushl %esi
508 pushl %edi
509
510 movl $(PA2KA(0xb8000)), %edi /* base of EGA text mode memory */
511 xorl %eax, %eax
512
513 /* Read bits 8 - 15 of the cursor address */
514 movw $0x3d4, %dx
515 movb $0xe, %al
516 outb %al, %dx
517
518 movw $0x3d5, %dx
519 inb %dx, %al
520 shl $8, %ax
521
522 /* Read bits 0 - 7 of the cursor address */
523 movw $0x3d4, %dx
524 movb $0xf, %al
525 outb %al, %dx
526
527 movw $0x3d5, %dx
528 inb %dx, %al
529
530 /* Sanity check for the cursor on screen */
531 cmp $2000, %ax
532 jb early_putchar_cursor_ok
533
534 movw $1998, %ax
535
536 early_putchar_cursor_ok:
537
538 movw %ax, %bx
539 shl $1, %eax
540 addl %eax, %edi
541
542 movl 0x08(%ebp), %eax
543
544 cmp $0x0a, %al
545 jne early_putchar_backspace
546
547 /* Interpret newline */
548
549 movw %bx, %ax /* %bx -> %dx:%ax */
550 xorw %dx, %dx
551
552 movw $80, %cx
553 idivw %cx, %ax /* %dx = %bx % 80 */
554
555 /* %bx <- %bx + 80 - (%bx % 80) */
556 addw %cx, %bx
557 subw %dx, %bx
558
559 jmp early_putchar_skip
560
561 early_putchar_backspace:
562
563 cmp $0x08, %al
564 jne early_putchar_print
565
566 /* Interpret backspace */
567
568 cmp $0x0000, %bx
569 je early_putchar_skip
570
571 dec %bx
572 jmp early_putchar_skip
573
574 early_putchar_print:
575
576 /* Print character */
577
578 movb $0x0e, %ah /* black background, yellow foreground */
579 stosw
580 inc %bx
581
582 early_putchar_skip:
583
584 /* Sanity check for the cursor on the last line */
585 cmp $2000, %bx
586 jb early_putchar_no_scroll
587
588 /* Scroll the screen (24 rows) */
589 movl $(PA2KA(0xb80a0)), %esi
590 movl $(PA2KA(0xb8000)), %edi
591 movl $960, %ecx
592 rep movsl
593
594 /* Clear the 24th row */
595 xorl %eax, %eax
596 movl $40, %ecx
597 rep stosl
598
599 /* Go to row 24 */
600 movw $1920, %bx
601
602 early_putchar_no_scroll:
603
604 /* Write bits 8 - 15 of the cursor address */
605 movw $0x3d4, %dx
606 movb $0xe, %al
607 outb %al, %dx
608
609 movw $0x3d5, %dx
610 movb %bh, %al
611 outb %al, %dx
612
613 /* Write bits 0 - 7 of the cursor address */
614 movw $0x3d4, %dx
615 movb $0xf, %al
616 outb %al, %dx
617
618 movw $0x3d5, %dx
619 movb %bl, %al
620 outb %al, %dx
621
622 /* Epilogue, restore preserved registers */
623 popl %edi
624 popl %esi
625 popl %ebx
626 leave
627
628#endif
629
630 ret
631
Note: See TracBrowser for help on using the repository browser.