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 2005 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#include <sys/types.h>
30#include <sys/systm.h>
31#include <sys/door.h>
32#include <sys/stack.h>
33#include <sys/errno.h>
34#include <sys/sysmacros.h>
35
36int
37door_finish_dispatch(caddr_t newsp)
38{
39	void lwp_setsp(klwp_t *, caddr_t);
40	void lwp_clear_uwin(void);
41
42	char bytes[MAX(MINFRAME, MINFRAME32)];
43	size_t count;
44	caddr_t biased_sp;
45
46	if (get_udatamodel() == DATAMODEL_NATIVE) {
47		count = MINFRAME;
48		biased_sp = newsp - STACK_BIAS;
49	} else {
50		count = MINFRAME32;
51		biased_sp = newsp;
52	}
53
54	/*
55	 * We carefully zero out the stack frame we're pointing %sp at.
56	 * This means that, upon returning to userland, the ins and locals
57	 * will be zeroed, instead of acquiring whatever garbage was on the
58	 * stack previously.  In particular, this makes sure %fp is NULL,
59	 * so that stack traces are properly terminated.
60	 */
61	bzero(bytes, count);
62	if (copyout(bytes, newsp, count) != 0)
63		return (E2BIG);
64
65	/*
66	 * There may be some user register windows stashed away
67	 * because our user thread stack wasn't available during some
68	 * kernel overflows. We don't care about this saved user
69	 * state since we are resetting our stack. Make sure we
70	 * don't try to push these registers out to our stack
71	 * later on when returning from this system call.
72	 *
73	 * We have guaranteed that no new user windows will be stored
74	 * to the pcb save area at this point since a door server
75	 * thread always does a full context switch (shuttle_switch,
76	 * shuttle_resume) before making itself available for a door
77	 * invocation.
78	 */
79	lwp_clear_uwin();
80	lwp_setsp(ttolwp(curthread), biased_sp);
81	return (0);
82}
83
84uintptr_t
85door_final_sp(uintptr_t resultsp, size_t align, int datamodel)
86{
87	size_t minframe = (datamodel == DATAMODEL_NATIVE)?
88	    MINFRAME : MINFRAME32;
89	return (P2ALIGN(resultsp - minframe, align));
90}
91