rwindow.c revision 83366
182906Sjake/*-
282906Sjake * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
382906Sjake *
482906Sjake * Redistribution and use in source and binary forms, with or without
582906Sjake * modification, are permitted provided that the following conditions
682906Sjake * are met:
782906Sjake * 1. Redistributions of source code must retain the above copyright
882906Sjake *    notice, this list of conditions and the following disclaimer.
982906Sjake * 2. Redistributions in binary form must reproduce the above copyright
1082906Sjake *    notice, this list of conditions and the following disclaimer in the
1182906Sjake *    documentation and/or other materials provided with the distribution.
1282906Sjake * 3. Berkeley Software Design Inc's name may not be used to endorse or
1382906Sjake *    promote products derived from this software without specific prior
1482906Sjake *    written permission.
1582906Sjake *
1682906Sjake * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
1782906Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1882906Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1982906Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
2082906Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2182906Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2282906Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2382906Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2482906Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2582906Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2682906Sjake * SUCH DAMAGE.
2782906Sjake *
2882906Sjake *	from: BSDI: trap.c,v 1.17.2.9 1999/10/19 15:29:52 cp Exp
2982906Sjake * $FreeBSD: head/sys/sparc64/sparc64/rwindow.c 83366 2001-09-12 08:38:13Z julian $
3082906Sjake */
3182906Sjake
3282906Sjake#include <sys/param.h>
3382906Sjake#include <sys/systm.h>
3482906Sjake#include <sys/ktr.h>
3582906Sjake#include <sys/proc.h>
3682906Sjake#include <sys/user.h>
3782906Sjake
3882906Sjake#include <machine/frame.h>
3982906Sjake
4082906Sjakeint
4183366Sjulianrwindow_load(struct thread *td, struct trapframe *tf, int n)
4282906Sjake{
4382906Sjake	struct rwindow rw;
4482906Sjake	u_long usp;
4582906Sjake	int error;
4682906Sjake	int i;
4782906Sjake
4882906Sjake	/*
4982906Sjake	 * In case current window is still only on-chip, push it out;
5082906Sjake	 * if it cannot get all the way out, we cannot continue either.
5182906Sjake	 */
5283366Sjulian	if ((error = rwindow_save(td)) != 0)
5382906Sjake		return (error);
5482906Sjake	usp = tf->tf_out[6];
5583366Sjulian	CTR3(KTR_TRAP, "rwindow_load: td=%p (%s) n=%d",
5683366Sjulian	    td, td->td_proc->p_comm, n);
5782906Sjake	for (i = 0; i < n; i++) {
5882906Sjake		CTR1(KTR_TRAP, "rwindow_load: usp=%#lx", usp);
5982906Sjake		usp += SPOFF;
6082906Sjake		error = copyin((void *)usp, &rw, sizeof rw);
6182906Sjake		usp = rw.rw_in[6];
6282906Sjake	}
6382906Sjake	CTR1(KTR_TRAP, "rwindow_load: error=%d", error);
6482906Sjake	return (error);
6582906Sjake}
6682906Sjake
6782906Sjakeint
6883366Sjulianrwindow_save(struct thread *td)
6982906Sjake{
7082906Sjake	struct rwindow *rw;
7182906Sjake	struct pcb *pcb;
7282906Sjake	u_long *ausp;
7382906Sjake	u_long usp;
7482906Sjake	int error;
7582906Sjake	int i;
7682906Sjake
7782906Sjake	flushw();
7883366Sjulian	pcb = td->td_pcb;
7982906Sjake	i = pcb->pcb_nsaved;
8083366Sjulian	CTR3(KTR_TRAP, "rwindow_save: td=%p (%s) nsaved=%d", td,
8183366Sjulian	    td->td_proc->p_comm, i);
8282906Sjake	if (i == 0)
8382906Sjake		return (0);
8482906Sjake	ausp = pcb->pcb_rwsp;
8582906Sjake	rw = pcb->pcb_rw;
8682906Sjake	error = 0;
8782906Sjake	do {
8882906Sjake		usp = *ausp;
8982906Sjake		CTR1(KTR_TRAP, "rwindow_save: usp=%#lx", usp);
9082906Sjake		usp += SPOFF;
9182906Sjake		error = copyout(rw, (void *)usp, sizeof *rw);
9282906Sjake		if (error)
9382906Sjake			break;
9482906Sjake		ausp++;
9582906Sjake		rw++;
9682906Sjake	} while (--i > 0);
9782906Sjake	CTR1(KTR_TRAP, "rwindow_save: error=%d", error);
9882906Sjake	if (error == 0)
9982906Sjake		pcb->pcb_nsaved = 0;
10082906Sjake	return (error);
10182906Sjake}
102