xref: /titanic_50/usr/src/cmd/mdb/sparc/kmdb/kmdb_makecontext.c (revision da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Context-saving routine used for pipelines.  Designed for use only
31  * with kmdb_setcontext, and with the assumption that func() will never
32  * return.
33  */
34 
35 #include <strings.h>
36 #include <ucontext.h>
37 #include <sys/types.h>
38 #include <sys/stack.h>
39 
40 #include <kmdb/kmdb_context_impl.h>
41 #include <mdb/mdb_kreg.h>
42 
43 void
44 kmdb_makecontext(ucontext_t *ucp, void (*func)(void *), void *arg, caddr_t stk,
45     size_t stksize)
46 {
47 	/*
48 	 * Top-of-stack must be rounded down to STACK_ALIGN and
49 	 * there must be a minimum frame for the register window.
50 	 */
51 	uintptr_t stack = (((uintptr_t)stk + stksize - 1) &
52 	    ~(STACK_ALIGN - 1)) - SA(MINFRAME);
53 
54 	/* clear the top stack frame */
55 	bzero((void *)stack, SA(MINFRAME));
56 
57 	/* fill in registers of interest */
58 	ucp->uc_mcontext.gregs[REG_PC] = (greg_t)func;
59 	ucp->uc_mcontext.gregs[REG_nPC] = (greg_t)func + 4;
60 	ucp->uc_mcontext.gregs[REG_O0] = (greg_t)arg;
61 	ucp->uc_mcontext.gregs[REG_SP] = (greg_t)(stack - STACK_BIAS);
62 	ucp->uc_mcontext.gregs[REG_O7] = NULL;
63 	ucp->uc_mcontext.gregs[REG_G7] = NULL;
64 }
65