xref: /minix/minix/kernel/arch/i386/sconst.h (revision 83133719)
1 #ifndef __SCONST_H__
2 #define __SCONST_H__
3 
4 #include "kernel/const.h"
5 #include "kernel/procoffsets.h"
6 
7 /*
8  * offset to current process pointer right after trap, we assume we always have
9  * error code on the stack
10  */
11 #define CURR_PROC_PTR		20
12 
13 /*
14  * tests whether the interrupt was triggered in kernel. If so, jump to the
15  * label. Displacement tell the macro ha far is the CS value saved by the trap
16  * from the current %esp. The kernel code segment selector has the lower 3 bits
17  * zeroed
18  */
19 #define TEST_INT_IN_KERNEL(displ, label)	\
20 	cmpl	$KERN_CS_SELECTOR, displ(%esp)	;\
21 	je	label				;
22 
23 /*
24  * saves the basic interrupt context (no error code) to the process structure
25  *
26  * displ is the displacement of %esp from the original stack after trap
27  * pptr is the process structure pointer
28  * tmp is an available temporary register
29  */
30 #define SAVE_TRAP_CTX(displ, pptr, tmp)			\
31 	movl	(0 + displ)(%esp), tmp			;\
32 	movl	tmp, PCREG(pptr)			;\
33 	movl	(4 + displ)(%esp), tmp			;\
34 	movl	tmp, CSREG(pptr)			;\
35 	movl	(8 + displ)(%esp), tmp			;\
36 	movl	tmp, PSWREG(pptr)			;\
37 	movl	(12 + displ)(%esp), tmp			;\
38 	movl	tmp, SPREG(pptr)
39 
40 /*
41  * restore kernel segments. %cs is already set and %fs, %gs are not used */
42 #define RESTORE_KERNEL_SEGS	\
43 	mov	$KERN_DS_SELECTOR, %si	;\
44 	mov	%si, %ds	;\
45 	mov	%si, %es	;\
46 	movw	$0, %si		;\
47 	mov	%si, %gs	;\
48 	mov	%si, %fs	;
49 
50 #define SAVE_GP_REGS(pptr)	\
51 	mov	%eax, AXREG(pptr)		;\
52 	mov	%ecx, CXREG(pptr)		;\
53 	mov	%edx, DXREG(pptr)		;\
54 	mov	%ebx, BXREG(pptr)		;\
55 	mov	%esi, SIREG(pptr)		;\
56 	mov	%edi, DIREG(pptr)		;
57 
58 #define RESTORE_GP_REGS(pptr)	\
59 	movl	AXREG(pptr), %eax		;\
60 	movl	CXREG(pptr), %ecx		;\
61 	movl	DXREG(pptr), %edx		;\
62 	movl	BXREG(pptr), %ebx		;\
63 	movl	SIREG(pptr), %esi		;\
64 	movl	DIREG(pptr), %edi		;
65 
66 /*
67  * save the context of the interrupted process to the structure in the process
68  * table. It pushses the %ebp to stack to get a scratch register. After %esi is
69  * saved, we can use it to get the saved %ebp from stack and save it to the
70  * final location
71  *
72  * displ is the stack displacement. In case of an exception, there are two extra
73  * value on the stack - error code and the exception number
74  */
75 #define SAVE_PROCESS_CTX(displ, trapcode) \
76 								\
77 	cld /* set the direction flag to a known state */	;\
78 								\
79 	push	%ebp					;\
80 							;\
81 	movl	(CURR_PROC_PTR + 4 + displ)(%esp), %ebp	;\
82 							\
83 	SAVE_GP_REGS(%ebp)				;\
84         movl	$trapcode, P_KERN_TRAP_STYLE(%ebp)	;\
85 	pop	%esi			/* get the orig %ebp and save it */ ;\
86 	mov	%esi, BPREG(%ebp)			;\
87 							\
88 	RESTORE_KERNEL_SEGS				;\
89 	SAVE_TRAP_CTX(displ, %ebp, %esi)		;
90 
91 /*
92  * clear the IF flag in eflags which are stored somewhere in memory, e.g. on
93  * stack. iret or popf will load the new value later
94  */
95 #define CLEAR_IF(where)	\
96 	mov	where, %eax						;\
97 	andl	$0xfffffdff, %eax					;\
98 	mov	%eax, where						;
99 
100 #endif /* __SCONST_H__ */
101