source: mainline/kernel/arch/ia32/src/asm.S@ 24b06199

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

cstyle
(no change in functionality)

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