2 * Copyright (c) 2002 Jonathan Mini <mini@freebsd.org>.
3 * Copyright (c) 2001 Daniel Eischen <deischen@freebsd.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Neither the name of the author nor the names of its contributors
12 * may be used to endorse or promote products derived from this software
13 * without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY DANIEL EISCHEN AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <machine/asm.h>
31 __FBSDID("$FreeBSD$");
34 * Where do we define these?
36 #define MC_SIZE 640 /* sizeof mcontext_t */
37 #define UC_MC_OFFSET 16 /* offset to mcontext from ucontext */
38 #define UC_MC_LEN_OFFSET 96 /* offset to mc_len from mcontext */
39 #define MC_LEN_OFFSET 80 /* offset to mc_len from mcontext */
40 #define MC_FP_REGS_OFFSET 96 /* offset to FP regs from mcontext */
41 #define MC_FP_CW_OFFSET 96 /* offset to FP control word */
42 #define MC_OWNEDFP_OFFSET 88 /* offset to mc_ownedfp from mcontext */
43 #define KM_STACK_SP_OFFSET 36 /* offset to km_stack.ss_sp */
44 #define KM_STACK_SIZE_OFFSET 40 /* offset to km_stack.ss_sp */
45 #define KM_FUNC_OFFSET 32 /* offset to km_func */
48 * int uts_to_thread(struct kse_thr_mailbox *tdp,
49 * struct kse_thr_mailbox **curthreadp);
51 * Does not return on success, returns -1 otherwise.
54 movl 4(%esp), %edx /* get address of kse_thr_mailbox */
55 /* .. ucontext_t is at offset 0 */
56 cmpl $0, %edx /* check for null pointer */
60 1: cmpl $MC_SIZE, UC_MC_LEN_OFFSET(%edx) /* is context valid? */
62 movl $-1, %eax /* bzzzt, invalid context */
64 2: movl 8(%esp), %ecx /* get address of curthreadp */
65 movl %edx, %ebx /* save the pointer for later */
67 * From here on, we don't touch the old stack.
69 addl $UC_MC_OFFSET, %edx /* add offset to mcontext */
78 movl 72(%edx), %esp /* switch to context defined stack */
79 subl $4, %esp /* leave space for the return address */
80 movl 60(%edx), %eax /* put return address at top of stack */
82 cmpl $0, MC_OWNEDFP_OFFSET(%edx) /* are FP regs valid? */
84 frstor MC_FP_REGS_OFFSET(%edx) /* restore FP regs */
87 fldcw MC_FP_CW_OFFSET(%edx)
88 4: movl 48(%edx), %eax /* restore ax, bx, cx, dx */
89 pushl 68(%edx) /* flags on stack */
90 pushl 36(%edx) /* %ebx on stack */
91 pushl 44(%edx) /* %ecx on stack */
92 movl 40(%edx), %edx /* %edx */
94 * all registers are now moved out of mailbox,
95 * it's safe to set current thread pointer
98 popl %ecx /* %ecx off stack */
99 pop %ebx /* %ebx off stack */
100 popf /* flags off stack */
101 5: ret /* %eip off stack */
104 * int thread_to_uts(struct kse_thr_mailbox *tm, struct kse_mailbox *km);
106 * Does not return on success, returns -1 otherwise.
109 movl 4(%esp), %eax /* get address of context */
110 cmpl $0, %eax /* check for null pointer */
114 1: pushl %edx /* save value of edx */
115 movl %eax, %edx /* get address of context */
116 addl $UC_MC_OFFSET, %edx /* add offset to mcontext */
125 movl $0, 48(%edx) /* store successful return in eax */
126 popl %eax /* get saved value of edx */
127 movl %eax, 40(%edx) /* save edx */
129 movl (%esp), %eax /* get return address */
130 movl %eax, 60(%edx) /* save return address */
133 * Don't save floating point registers here.
135 * This is an explicit call to get the current context, so
136 * the caller is done with the floating point registers.
137 * Contexts formed by involuntary switches, such as signal delivery,
138 * have floating point registers saved by the kernel.
140 fnstcw MC_FP_CW_OFFSET(%edx)
141 movl $0, MC_OWNEDFP_OFFSET(%edx) /* no FP */
142 pushfl /* get eflags */
144 movl %eax, 68(%edx) /* store eflags */
145 movl %esp, %eax /* setcontext pushes the return */
146 addl $4, %eax /* address onto the top of the */
147 movl %eax, 72(%edx) /* stack; account for this */
148 movl $MC_SIZE, MC_LEN_OFFSET(%edx) /* context is now valid */
149 movl 8(%esp), %edx /* get address of mailbox */
150 movl KM_STACK_SP_OFFSET(%edx), %eax /* get bottom of stack */
151 addl KM_STACK_SIZE_OFFSET(%edx), %eax /* add length */
152 movl %eax, %esp /* switch to the uts's stack */
153 pushl %edx /* push the address of the mailbox */
154 pushl KM_FUNC_OFFSET(%edx) /* .. the uts can return to itself */
155 pushl KM_FUNC_OFFSET(%edx) /* push the address of the uts func */