4 #include <minix/config.h>
5 #include <minix/const.h>
6 #include <machine/asm.h>
7 #include <machine/interrupt.h>
8 #include <machine/vm.h>
10 #include "kernel/const.h"
12 #include <machine/multiboot.h>
15 /* Easy way to make functions */
17 /* Make a function of the form func(arg) */
19 #define STACKARG 8(%ebp)
21 #define ARG_EAX_ACTION(FUNCTION, ACTION) ;\
30 /* Make a function of the form ret = func() */
31 #define ARG_EAX_RETURN(FUNCTION, EXPR) ;\
39 /* Make a function of the form ret = func() */
40 #define ARG_EAX_SET(FUNCTION, DEST) ;\
46 jmp 0f /* a jump is required for some sets */ ;\
50 /* Make a function of the form ret = func() */
51 #define ARG_AX_SET(FUNCTION, DEST) ;\
57 jmp 0f /* a jump is required for some sets */ ;\
62 * This file contains a number of assembly code utility routines needed by the
70 /*===========================================================================*/
72 /*===========================================================================*/
74 * PUBLIC void phys_insw(Port_t port, phys_bytes buf, size_t count);
75 * Input an array from an I/O port. Absolute address version of insw().
77 /* transfer data from (disk controller) port to memory */
84 mov 8(%ebp), %edx /* port to read from */
85 mov 12(%ebp), %edi /* destination addr */
86 mov 16(%ebp), %ecx /* byte count */
87 shr $1, %ecx /* word count */
88 rep insw /* input many words */
94 /*===========================================================================*/
96 /*===========================================================================*/
98 * PUBLIC void phys_insb(Port_t port, phys_bytes buf, size_t count);
99 * Input an array from an I/O port. Absolute address version of insb().
101 /* transfer data from (disk controller) port to memory byte by byte */
108 mov 8(%ebp), %edx /* port to read from */
109 mov 12(%ebp), %edi /* destination addr */
110 mov 16(%ebp), %ecx /* byte count */
111 rep insb /* input many bytes */
117 /*===========================================================================*/
119 /*===========================================================================*/
121 * PUBLIC void phys_outsw(Port_t port, phys_bytes buf, size_t count);
122 * Output an array to an I/O port. Absolute address version of outsw().
124 /* transfer data from memory to (disk controller) port */
131 mov 8(%ebp), %edx /* port to write to */
132 mov 12(%ebp), %esi /* source addr */
133 mov 16(%ebp), %ecx /* byte count */
134 shr $1, %ecx /* word count */
135 rep outsw /* output many words */
141 /*===========================================================================*/
143 /*===========================================================================*/
145 * PUBLIC void phys_outsb(Port_t port, phys_bytes buf, size_t count);
146 * Output an array to an I/O port. Absolute address version of outsb().
148 /* transfer data from memory to (disk controller) port byte by byte */
155 mov 8(%ebp), %edx /* port to write to */
156 mov 12(%ebp), %esi /* source addr */
157 mov 16(%ebp), %ecx /* byte count */
158 rep outsb /* output many bytes */
164 /*===========================================================================*/
166 /*===========================================================================*/
168 * PUBLIC phys_bytes phys_copy(phys_bytes source, phys_bytes destination,
169 * phys_bytes bytecount);
170 * Copy a block of data from anywhere to anywhere in physical memory.
172 /* es edi esi eip src dst len */
185 cmp $10, %eax /* avoid align overhead for small counts */
187 mov %esi, %ecx /* align source, hope target is too */
189 and $3, %ecx /* count for alignment */
192 rep movsb (%esi), (%edi)
194 shr $2, %ecx /* count of dwords */
196 rep movsl (%esi), (%edi)
199 xchg %eax, %ecx /* remainder */
201 rep movsb (%esi), (%edi)
203 mov $0, %eax /* 0 means: no fault */
204 LABEL(phys_copy_fault) /* kernel can send us here */
210 LABEL(phys_copy_fault_in_kernel) /* kernel can send us here */
218 /*===========================================================================*/
219 /* copy_msg_from_user */
220 /*===========================================================================*/
222 * int copy_msg_from_user(message * user_mbuf, message * dst);
224 * Copies a message of 36 bytes from user process space to a kernel buffer. This
225 * function assumes that the process address space is installed (cr3 loaded).
227 * This function from the callers point of view either succeeds or returns an
228 * error which gives the caller a chance to respond accordingly. In fact it
229 * either succeeds or if it generates a pagefault, general protection or other
230 * exception, the trap handler has to redirect the execution to
231 * __user_copy_msg_pointer_failure where the error is reported to the caller
232 * without resolving the pagefault. It is not kernel's problem to deal with
233 * wrong pointers from userspace and the caller should return an error to
234 * userspace as if wrong values or request were passed to the kernel
236 ENTRY(copy_msg_from_user)
237 /* load the source pointer */
239 /* load the destination pointer */
242 /* mov 0*4(%ecx), %eax
243 mov %eax, 0*4(%edx) */
261 LABEL(__copy_msg_from_user_end)
265 /*===========================================================================*/
266 /* copy_msg_to_user */
267 /*===========================================================================*/
269 * void copy_msg_to_user(message * src, message * user_mbuf);
271 * Copies a message of 36 bytes to user process space from a kernel buffer.
273 * All the other copy_msg_from_user() comments apply here as well!
275 ENTRY(copy_msg_to_user)
276 /* load the source pointer */
278 /* load the destination pointer */
300 LABEL(__copy_msg_to_user_end)
305 * if a function from a selected set of copies from or to userspace fails, it is
306 * because of a wrong pointer supplied by the userspace. We have to clean up and
307 * and return -1 to indicated that something wrong has happend. The place it was
308 * called from has to handle this situation. The exception handler redirect us
309 * here to continue, clean up and report the error
311 ENTRY(__user_copy_msg_pointer_failure)
315 /*===========================================================================*/
317 /*===========================================================================*/
319 * PUBLIC void phys_memset(phys_bytes source, unsigned long pattern,
320 * phys_bytes bytecount);
321 * Fill a block of physical memory with pattern.
338 /* Any remaining bytes? */
352 LABEL(memset_fault) /* kernel can send us here */
353 mov $0, %eax /* 0 means: no fault */
359 LABEL(memset_fault_in_kernel) /* kernel can send us here */
366 /*===========================================================================*/
367 /* x86_triplefault */
368 /*===========================================================================*/
370 * PUBLIC void x86_triplefault();
371 * Reset the system by loading IDT with offset 0 and interrupting.
373 ENTRY(x86_triplefault)
375 int $3 /* anything goes, the 386 will not like it */
382 /*===========================================================================*/
384 /*===========================================================================*/
386 * PUBLIC void halt_cpu(void);
387 * reanables interrupts and puts the cpu in the halts state. Once an interrupt
388 * is handled the execution resumes by disabling interrupts and continues
392 hlt /* interrupts enabled only after this instruction is executed! */
394 * interrupt handlers make sure that the interrupts are disabled when we
395 * get here so we take only _one_ interrupt after halting the CPU
399 /*===========================================================================*/
401 /*===========================================================================*/
403 * PUBLIC unsigned long read_cpu_flags(void);
404 * Read CPU status flags from C.
406 ENTRY(read_cpu_flags)
427 /*===========================================================================*/
429 /*===========================================================================*/
431 /* non-waiting FPU initialization */
440 /* store status word (non-waiting) */
444 /* DO NOT CHANGE THE OPERAND!!! gas2ack does not handle it yet */
448 /*===========================================================================*/
450 /*===========================================================================*/
458 /*===========================================================================*/
460 /*===========================================================================*/
468 /* Shared exception handler for both fxrstor and frstor. */
469 ENTRY(__frstor_failure)
473 /* Read/write control registers */
474 ARG_EAX_RETURN(read_cr0, %cr0);
475 ARG_EAX_RETURN(read_cr2, %cr2);
476 ARG_EAX_RETURN(read_cr3, %cr3);
477 ARG_EAX_RETURN(read_cr4, %cr4);
478 ARG_EAX_SET(write_cr4, %cr4);
479 ARG_EAX_SET(write_cr0, %cr0);
480 ARG_EAX_SET(write_cr3, %cr3);
482 /* Read/write various descriptor tables */
483 ARG_EAX_ACTION(x86_ltr, ltr STACKARG );
484 ARG_EAX_ACTION(x86_lidt, lidtl (%eax));
485 ARG_EAX_ACTION(x86_lgdt, lgdt (%eax));
486 ARG_EAX_ACTION(x86_lldt, lldt STACKARG);
487 ARG_EAX_ACTION(x86_sgdt, sgdt (%eax));
488 ARG_EAX_ACTION(x86_sidt, sidt (%eax));
491 ARG_AX_SET(x86_load_ds, %ds)
492 ARG_AX_SET(x86_load_es, %es)
493 ARG_AX_SET(x86_load_fs, %fs)
494 ARG_AX_SET(x86_load_gs, %gs)
495 ARG_AX_SET(x86_load_ss, %ss)
498 ARG_EAX_ACTION(fnsave, fnsave (%eax) ; fwait);
499 ARG_EAX_ACTION(fxsave, fxsave (%eax));
500 ARG_EAX_ACTION(fnstcw, fnstcw (%eax));
503 ARG_EAX_ACTION(i386_invlpg, invlpg (%eax));
505 ENTRY(x86_load_kerncs)
509 jmp $KERN_CS_SELECTOR, $newcs
515 * Read the Model Specific Register (MSR) of IA32 architecture
517 * void ia32_msr_read(u32_t reg, u32_t * hi, u32_t * lo)
534 * Write the Model Specific Register (MSR) of IA32 architecture
536 * void ia32_msr_write(u32_t reg, u32_t hi, u32_t lo)
538 ENTRY(ia32_msr_write)
550 /*===========================================================================*/
551 /* __switch_address_space */
552 /*===========================================================================*/
553 /* PUBLIC void __switch_address_space(struct proc *p, struct ** ptproc)
555 * sets the %cr3 register to the supplied value if it is not already set to the
556 * same value in which case it would only result in an extra TLB flush which is
559 ENTRY(__switch_address_space)
560 /* read the process pointer */
562 /* get the new cr3 value */
563 movl P_CR3(%edx), %eax
564 /* test if the new cr3 != NULL */
569 * test if the cr3 is loaded with the current value to avoid unnecessary
582 /* acknowledge just the master PIC */
583 ENTRY(eoi_8259_master)
584 movb $END_OF_INT, %al
588 /* we have to acknowledge both PICs */
589 ENTRY(eoi_8259_slave)
590 movb $END_OF_INT, %al
595 /* in some cases we need to force TLB update, reloading cr3 does the trick */
603 /*===========================================================================*/
605 /*===========================================================================*/
606 /* PUBLIC int smp_get_htt(void); */
607 /* return true if the processor is hyper-threaded. */
617 /* FIXME don't use the byte code */
618 .byte 0x0f, 0xa2 /* opcode for cpuid */
627 /*===========================================================================*/
628 /* smp_get_num_htt */
629 /*===========================================================================*/
630 /* PUBLIC int smp_get_num_htt(void); */
631 /* Get the number of hyper-threaded processor cores */
632 ENTRY(smp_get_num_htt)
641 /* FIXME don't use the byte code */
642 .byte 0x0f, 0xa2 /* opcode for cpuid */
651 /*===========================================================================*/
653 /*===========================================================================*/
654 /* PUBLIC int smp_get_cores(void); */
655 /* Get the number of cores. */
667 /* FIXME don't use the byte code */
668 .byte 0x0f, 0xa2 /* opcode for cpuid */
676 /*===========================================================================*/
677 /* arch_spinlock_lock */
678 /*===========================================================================*/
679 /* void arch_spinlock_lock (u32_t *lock_data)
681 * while (test_and_set(lock_data) == 1)
682 * while (*lock_data == 1)
685 * eax register is clobbered.
687 ENTRY(arch_spinlock_lock)
711 /*===========================================================================*/
712 /* arch_spinlock_unlock */
713 /*===========================================================================*/
714 /* * void arch_spinlock_unlock (unsigned int *lockp) */
715 /* spin lock release routine. */
716 ENTRY(arch_spinlock_unlock)
723 #endif /* CONFIG_SMP */
725 /*===========================================================================*/
727 /*===========================================================================*/
728 /* PUBLIC void mfence (void); */
729 /* architecture specific memory barrier routine. */
734 /*===========================================================================*/
736 /*===========================================================================*/
737 /* PUBLIC void arch_pause (void); */
738 /* architecture specific pause routine. */
743 /*===========================================================================*/
745 /*===========================================================================*/
746 /* PUBLIC u16_t cpuid(void) */
751 ENTRY(interrupts_enable)
755 ENTRY(interrupts_disable)
761 * void switch_k_stack(void * esp, void (* continuation)(void));
763 * sets the current stack pointer to the given value and continues execution at
766 ENTRY(switch_k_stack)
767 /* get the arguments from the stack */
770 mov $0, %ebp /* reset %ebp for stack trace */
771 mov %ecx, %esp /* set the new stack */
772 jmp *%eax /* and jump to the continuation */