os161-1.99
 All Data Structures
switchframe.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #include <types.h>
00031 #include <lib.h>
00032 #include <thread.h>
00033 #include <threadprivate.h>
00034 
00035 #include "switchframe.h"
00036 
00037 /* in threadstart.S */
00038 extern void mips_threadstart(/* arguments are in unusual registers */);
00039 
00040 
00041 /*
00042  * Function to initialize the switchframe of a new thread, which is
00043  * *not* the one that is currently running.
00044  *
00045  * The new thread should, when it is run the first time, end up calling
00046  * thread_startup(entrypoint, data1, data2).
00047  *
00048  * We arrange for this by creating a phony switchframe for
00049  * switchframe_switch() to switch to. The only trouble is that the
00050  * switchframe doesn't include the argument registers a0-a3. So we
00051  * store the arguments in the s* registers, and use a bit of asm
00052  * (mips_threadstart) to move them and then jump to thread_startup.
00053  */
00054 void 
00055 switchframe_init(struct thread *thread,
00056                  void (*entrypoint)(void *data1, unsigned long data2),
00057                  void *data1, unsigned long data2)
00058 {
00059         vaddr_t stacktop;
00060         struct switchframe *sf;
00061 
00062         /*
00063          * MIPS stacks grow down. t_stack is just a hunk of memory, so
00064          * get the other end of it. Then set up a switchframe on the
00065          * top of the stack.
00066          */
00067         stacktop = ((vaddr_t)thread->t_stack) + STACK_SIZE;
00068         sf = ((struct switchframe *) stacktop) - 1;
00069 
00070         /* Zero out the switchframe. */
00071         bzero(sf, sizeof(*sf));
00072 
00073         /*
00074          * Now set the important parts: pass through the three arguments,
00075          * and set the return address register to the place we want 
00076          * execution to begin.
00077          *
00078          * Thus, when switchframe_switch does its "j ra", it will
00079          * actually jump to mips_threadstart, which will move the
00080          * arguments into the right register and jump to
00081          * thread_startup().
00082          *
00083          * Note that this means that when we call switchframe_switch()
00084          * in thread_switch(), we may not come back out the same way
00085          * in the next thread. (Though we will come back out the same
00086          * way when we later come back to the same thread again.)
00087          *
00088          * This has implications for code at the bottom of
00089          * thread_switch, described in thread.c.
00090          */
00091         sf->sf_s0 = (uint32_t)entrypoint;
00092         sf->sf_s1 = (uint32_t)data1;
00093         sf->sf_s2 = (uint32_t)data2;
00094         sf->sf_ra = (uint32_t)mips_threadstart;
00095 
00096         /* Set ->t_context, and we're done. */
00097         thread->t_context = sf;
00098 }
 All Data Structures